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 struct got_fetch_progress_arg {
973 char last_scaled_size[FMT_SCALED_STRSIZE];
978 static const struct got_error *
979 fetch_progress(void *arg, const char *message, off_t packfile_size,
980 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
982 struct got_fetch_progress_arg *a = arg;
983 char scaled_size[FMT_SCALED_STRSIZE];
984 int p_indexed, p_resolved;
985 int print_size = 0, print_indexed = 0, print_resolved = 0;
987 if (message && message[0] != '\0' && message[0] != '\n') {
988 printf("\rserver: %s", message);
989 if (strchr(message, '\n') == 0)
994 if (packfile_size > 0 || nobj_indexed > 0) {
995 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
996 (a->last_scaled_size[0] == '\0' ||
997 strcmp(scaled_size, a->last_scaled_size)) != 0) {
999 if (strlcpy(a->last_scaled_size, scaled_size,
1000 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1001 return got_error(GOT_ERR_NO_SPACE);
1003 if (nobj_indexed > 0) {
1004 p_indexed = (nobj_indexed * 100) / nobj_total;
1005 if (p_indexed != a->last_p_indexed) {
1006 a->last_p_indexed = p_indexed;
1011 if (nobj_resolved > 0) {
1012 p_resolved = (nobj_resolved * 100) /
1013 (nobj_total - nobj_loose);
1014 if (p_resolved != a->last_p_resolved) {
1015 a->last_p_resolved = p_resolved;
1023 if (print_size || print_indexed || print_resolved)
1026 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1028 printf("; indexing %d%%", p_indexed);
1030 printf("; resolving deltas %d%%", p_resolved);
1031 if (print_size || print_indexed || print_resolved)
1034 if (nobj_indexed > 0 && nobj_indexed == nobj_total &&
1035 nobj_resolved == nobj_total - nobj_loose)
1036 printf("\nWriting pack index...\n");
1041 static const struct got_error *
1042 cmd_clone(int argc, char *argv[])
1044 const struct got_error *err = NULL;
1045 const char *uri, *branch_filter, *dirname;
1046 char *proto, *host, *port, *repo_name, *server_path;
1047 char *default_destdir = NULL, *id_str = NULL;
1048 const char *repo_path;
1049 struct got_repository *repo = NULL;
1050 struct got_pathlist_head refs, symrefs;
1051 struct got_pathlist_entry *pe;
1052 struct got_object_id *pack_hash = NULL;
1053 int ch, fetchfd = -1;
1054 struct got_fetch_progress_arg fpa;
1057 TAILQ_INIT(&symrefs);
1059 while ((ch = getopt(argc, argv, "b:")) != -1) {
1062 branch_filter = optarg;
1079 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1080 &repo_name, argv[0]);
1084 if (dirname == NULL) {
1085 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1086 err = got_error_from_errno("asprintf");
1089 repo_path = default_destdir;
1091 repo_path = dirname;
1093 err = got_path_mkdir(repo_path);
1097 err = got_repo_init(repo_path);
1101 err = got_repo_open(&repo, repo_path, NULL);
1105 err = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1109 printf("Connected to %s:%s\n", host, port);
1111 fpa.last_scaled_size[0] = '\0';
1112 fpa.last_p_indexed = -1;
1113 fpa.last_p_resolved = -1;
1114 err = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1115 repo, fetch_progress, &fpa);
1119 err = got_object_id_str(&id_str, pack_hash);
1122 printf("Fetched %s.pack\n", id_str);
1125 /* Set up references provided with the pack file. */
1126 TAILQ_FOREACH(pe, &refs, entry) {
1127 const char *refname = pe->path;
1128 struct got_object_id *id = pe->data;
1129 struct got_reference *ref;
1132 err = got_ref_alloc(&ref, refname, id);
1137 err = got_object_id_str(&id_str, id);
1140 printf("%s: %s\n", got_ref_get_name(ref), id_str);
1143 err = got_ref_write(ref, repo);
1149 /* Set the HEAD reference if the server provided one. */
1150 TAILQ_FOREACH(pe, &symrefs, entry) {
1151 struct got_reference *symref, *target_ref;
1152 const char *refname = pe->path;
1153 const char *target = pe->data;
1155 if (strcmp(refname, GOT_REF_HEAD) != 0)
1158 err = got_ref_open(&target_ref, repo, target, 0);
1160 if (err->code == GOT_ERR_NOT_REF)
1165 err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1166 got_ref_close(target_ref);
1170 printf("Setting %s to %s\n", GOT_REF_HEAD,
1171 got_ref_get_symref_target(symref));
1173 err = got_ref_write(symref, repo);
1174 got_ref_close(symref);
1179 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1180 err = got_error_from_errno("close");
1182 got_repo_close(repo);
1183 TAILQ_FOREACH(pe, &refs, entry) {
1184 free((void *)pe->path);
1187 got_pathlist_free(&refs);
1188 TAILQ_FOREACH(pe, &symrefs, entry) {
1189 free((void *)pe->path);
1192 got_pathlist_free(&symrefs);
1199 free(default_destdir);
1203 static const struct got_error *
1204 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1206 static char msg[512];
1207 const char *branch_name;
1209 if (got_ref_is_symbolic(ref))
1210 branch_name = got_ref_get_symref_target(ref);
1212 branch_name = got_ref_get_name(ref);
1214 if (strncmp("refs/heads/", branch_name, 11) == 0)
1217 snprintf(msg, sizeof(msg),
1218 "target commit is not contained in branch '%s'; "
1219 "the branch to use must be specified with -b; "
1220 "if necessary a new branch can be created for "
1221 "this commit with 'got branch -c %s BRANCH_NAME'",
1222 branch_name, commit_id_str);
1224 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1227 static const struct got_error *
1228 cmd_checkout(int argc, char *argv[])
1230 const struct got_error *error = NULL;
1231 struct got_repository *repo = NULL;
1232 struct got_reference *head_ref = NULL;
1233 struct got_worktree *worktree = NULL;
1234 char *repo_path = NULL;
1235 char *worktree_path = NULL;
1236 const char *path_prefix = "";
1237 const char *branch_name = GOT_REF_HEAD;
1238 char *commit_id_str = NULL;
1239 int ch, same_path_prefix, allow_nonempty = 0;
1240 struct got_pathlist_head paths;
1241 struct got_checkout_progress_arg cpa;
1245 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1248 branch_name = optarg;
1251 commit_id_str = strdup(optarg);
1252 if (commit_id_str == NULL)
1253 return got_error_from_errno("strdup");
1259 path_prefix = optarg;
1271 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1272 "unveil", NULL) == -1)
1276 char *cwd, *base, *dotgit;
1277 repo_path = realpath(argv[0], NULL);
1278 if (repo_path == NULL)
1279 return got_error_from_errno2("realpath", argv[0]);
1280 cwd = getcwd(NULL, 0);
1282 error = got_error_from_errno("getcwd");
1285 if (path_prefix[0]) {
1286 base = basename(path_prefix);
1288 error = got_error_from_errno2("basename",
1293 base = basename(repo_path);
1295 error = got_error_from_errno2("basename",
1300 dotgit = strstr(base, ".git");
1303 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1304 error = got_error_from_errno("asprintf");
1309 } else if (argc == 2) {
1310 repo_path = realpath(argv[0], NULL);
1311 if (repo_path == NULL) {
1312 error = got_error_from_errno2("realpath", argv[0]);
1315 worktree_path = realpath(argv[1], NULL);
1316 if (worktree_path == NULL) {
1317 if (errno != ENOENT) {
1318 error = got_error_from_errno2("realpath",
1322 worktree_path = strdup(argv[1]);
1323 if (worktree_path == NULL) {
1324 error = got_error_from_errno("strdup");
1331 got_path_strip_trailing_slashes(repo_path);
1332 got_path_strip_trailing_slashes(worktree_path);
1334 error = got_repo_open(&repo, repo_path, NULL);
1338 /* Pre-create work tree path for unveil(2) */
1339 error = got_path_mkdir(worktree_path);
1341 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1342 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1344 if (!allow_nonempty &&
1345 !got_path_dir_is_empty(worktree_path)) {
1346 error = got_error_path(worktree_path,
1347 GOT_ERR_DIR_NOT_EMPTY);
1352 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1356 error = got_ref_open(&head_ref, repo, branch_name, 0);
1360 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1361 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1364 error = got_worktree_open(&worktree, worktree_path);
1368 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1372 if (!same_path_prefix) {
1373 error = got_error(GOT_ERR_PATH_PREFIX);
1377 if (commit_id_str) {
1378 struct got_object_id *commit_id;
1379 error = got_repo_match_object_id(&commit_id, NULL,
1380 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1383 error = check_linear_ancestry(commit_id,
1384 got_worktree_get_base_commit_id(worktree), 0, repo);
1385 if (error != NULL) {
1387 if (error->code == GOT_ERR_ANCESTRY) {
1388 error = checkout_ancestry_error(
1389 head_ref, commit_id_str);
1393 error = check_same_branch(commit_id, head_ref, NULL, repo);
1395 if (error->code == GOT_ERR_ANCESTRY) {
1396 error = checkout_ancestry_error(
1397 head_ref, commit_id_str);
1401 error = got_worktree_set_base_commit_id(worktree, repo,
1408 error = got_pathlist_append(&paths, "", NULL);
1411 cpa.worktree_path = worktree_path;
1412 cpa.had_base_commit_ref_error = 0;
1413 error = got_worktree_checkout_files(worktree, &paths, repo,
1414 checkout_progress, &cpa, check_cancelled, NULL);
1418 printf("Now shut up and hack\n");
1419 if (cpa.had_base_commit_ref_error)
1420 show_worktree_base_ref_warning();
1422 got_pathlist_free(&paths);
1423 free(commit_id_str);
1425 free(worktree_path);
1432 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1437 static const struct got_error *
1438 update_progress(void *arg, unsigned char status, const char *path)
1440 int *did_something = arg;
1442 if (status == GOT_STATUS_EXISTS ||
1443 status == GOT_STATUS_BASE_REF_ERR)
1448 /* Base commit bump happens silently. */
1449 if (status == GOT_STATUS_BUMP_BASE)
1452 while (path[0] == '/')
1454 printf("%c %s\n", status, path);
1458 static const struct got_error *
1459 switch_head_ref(struct got_reference *head_ref,
1460 struct got_object_id *commit_id, struct got_worktree *worktree,
1461 struct got_repository *repo)
1463 const struct got_error *err = NULL;
1465 int ref_has_moved = 0;
1467 /* Trivial case: switching between two different references. */
1468 if (strcmp(got_ref_get_name(head_ref),
1469 got_worktree_get_head_ref_name(worktree)) != 0) {
1470 printf("Switching work tree from %s to %s\n",
1471 got_worktree_get_head_ref_name(worktree),
1472 got_ref_get_name(head_ref));
1473 return got_worktree_set_head_ref(worktree, head_ref);
1476 err = check_linear_ancestry(commit_id,
1477 got_worktree_get_base_commit_id(worktree), 0, repo);
1479 if (err->code != GOT_ERR_ANCESTRY)
1486 /* Switching to a rebased branch with the same reference name. */
1487 err = got_object_id_str(&base_id_str,
1488 got_worktree_get_base_commit_id(worktree));
1491 printf("Reference %s now points at a different branch\n",
1492 got_worktree_get_head_ref_name(worktree));
1493 printf("Switching work tree from %s to %s\n", base_id_str,
1494 got_worktree_get_head_ref_name(worktree));
1498 static const struct got_error *
1499 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1501 const struct got_error *err;
1504 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1508 return got_error(GOT_ERR_REBASING);
1510 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1514 return got_error(GOT_ERR_HISTEDIT_BUSY);
1519 static const struct got_error *
1520 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1521 char *argv[], struct got_worktree *worktree)
1523 const struct got_error *err = NULL;
1530 return got_error_from_errno("strdup");
1531 return got_pathlist_append(paths, path, NULL);
1534 for (i = 0; i < argc; i++) {
1535 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1538 err = got_pathlist_append(paths, path, NULL);
1548 static const struct got_error *
1549 cmd_update(int argc, char *argv[])
1551 const struct got_error *error = NULL;
1552 struct got_repository *repo = NULL;
1553 struct got_worktree *worktree = NULL;
1554 char *worktree_path = NULL;
1555 struct got_object_id *commit_id = NULL;
1556 char *commit_id_str = NULL;
1557 const char *branch_name = NULL;
1558 struct got_reference *head_ref = NULL;
1559 struct got_pathlist_head paths;
1560 struct got_pathlist_entry *pe;
1561 int ch, did_something = 0;
1565 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1568 branch_name = optarg;
1571 commit_id_str = strdup(optarg);
1572 if (commit_id_str == NULL)
1573 return got_error_from_errno("strdup");
1585 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1586 "unveil", NULL) == -1)
1589 worktree_path = getcwd(NULL, 0);
1590 if (worktree_path == NULL) {
1591 error = got_error_from_errno("getcwd");
1594 error = got_worktree_open(&worktree, worktree_path);
1598 error = check_rebase_or_histedit_in_progress(worktree);
1602 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1607 error = apply_unveil(got_repo_get_path(repo), 0,
1608 got_worktree_get_root_path(worktree));
1612 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1616 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1617 got_worktree_get_head_ref_name(worktree), 0);
1620 if (commit_id_str == NULL) {
1621 error = got_ref_resolve(&commit_id, repo, head_ref);
1624 error = got_object_id_str(&commit_id_str, commit_id);
1628 error = got_repo_match_object_id(&commit_id, NULL,
1629 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1630 free(commit_id_str);
1631 commit_id_str = NULL;
1634 error = got_object_id_str(&commit_id_str, commit_id);
1640 struct got_object_id *head_commit_id;
1641 TAILQ_FOREACH(pe, &paths, entry) {
1642 if (pe->path_len == 0)
1644 error = got_error_msg(GOT_ERR_BAD_PATH,
1645 "switching between branches requires that "
1646 "the entire work tree gets updated");
1649 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1652 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1654 free(head_commit_id);
1657 error = check_same_branch(commit_id, head_ref, NULL, repo);
1660 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1664 error = check_linear_ancestry(commit_id,
1665 got_worktree_get_base_commit_id(worktree), 0, repo);
1666 if (error != NULL) {
1667 if (error->code == GOT_ERR_ANCESTRY)
1668 error = got_error(GOT_ERR_BRANCH_MOVED);
1671 error = check_same_branch(commit_id, head_ref, NULL, repo);
1676 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1678 error = got_worktree_set_base_commit_id(worktree, repo,
1684 error = got_worktree_checkout_files(worktree, &paths, repo,
1685 update_progress, &did_something, check_cancelled, NULL);
1690 printf("Updated to commit %s\n", commit_id_str);
1692 printf("Already up-to-date\n");
1694 free(worktree_path);
1695 TAILQ_FOREACH(pe, &paths, entry)
1696 free((char *)pe->path);
1697 got_pathlist_free(&paths);
1699 free(commit_id_str);
1703 static const struct got_error *
1704 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1705 const char *path, int diff_context, int ignore_whitespace,
1706 struct got_repository *repo)
1708 const struct got_error *err = NULL;
1709 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1712 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1717 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1721 while (path[0] == '/')
1723 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1724 ignore_whitespace, stdout);
1727 got_object_blob_close(blob1);
1728 got_object_blob_close(blob2);
1732 static const struct got_error *
1733 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1734 const char *path, int diff_context, int ignore_whitespace,
1735 struct got_repository *repo)
1737 const struct got_error *err = NULL;
1738 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1739 struct got_diff_blob_output_unidiff_arg arg;
1742 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1747 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1751 arg.diff_context = diff_context;
1752 arg.ignore_whitespace = ignore_whitespace;
1753 arg.outfile = stdout;
1754 while (path[0] == '/')
1756 err = got_diff_tree(tree1, tree2, path, path, repo,
1757 got_diff_blob_output_unidiff, &arg, 1);
1760 got_object_tree_close(tree1);
1762 got_object_tree_close(tree2);
1766 static const struct got_error *
1767 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1768 const char *path, int diff_context, struct got_repository *repo)
1770 const struct got_error *err = NULL;
1771 struct got_commit_object *pcommit = NULL;
1772 char *id_str1 = NULL, *id_str2 = NULL;
1773 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1774 struct got_object_qid *qid;
1776 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1778 err = got_object_open_as_commit(&pcommit, repo,
1784 if (path && path[0] != '\0') {
1786 err = got_object_id_by_path(&obj_id2, repo, id, path);
1789 err = got_object_id_str(&id_str2, obj_id2);
1795 err = got_object_id_by_path(&obj_id1, repo,
1801 err = got_object_id_str(&id_str1, obj_id1);
1807 err = got_object_get_type(&obj_type, repo, obj_id2);
1812 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1814 case GOT_OBJ_TYPE_BLOB:
1815 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1818 case GOT_OBJ_TYPE_TREE:
1819 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1823 err = got_error(GOT_ERR_OBJ_TYPE);
1829 obj_id2 = got_object_commit_get_tree_id(commit);
1830 err = got_object_id_str(&id_str2, obj_id2);
1833 obj_id1 = got_object_commit_get_tree_id(pcommit);
1834 err = got_object_id_str(&id_str1, obj_id1);
1837 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1838 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1844 got_object_commit_close(pcommit);
1849 get_datestr(time_t *time, char *datebuf)
1851 struct tm mytm, *tm;
1854 tm = gmtime_r(time, &mytm);
1857 s = asctime_r(tm, datebuf);
1860 p = strchr(s, '\n');
1866 static const struct got_error *
1867 match_logmsg(int *have_match, struct got_object_id *id,
1868 struct got_commit_object *commit, regex_t *regex)
1870 const struct got_error *err = NULL;
1871 regmatch_t regmatch;
1872 char *id_str = NULL, *logmsg = NULL;
1876 err = got_object_id_str(&id_str, id);
1880 err = got_object_commit_get_logmsg(&logmsg, commit);
1884 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1892 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1894 static const struct got_error *
1895 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1896 struct got_repository *repo, const char *path, int show_patch,
1897 int diff_context, struct got_reflist_head *refs)
1899 const struct got_error *err = NULL;
1900 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1902 time_t committer_time;
1903 const char *author, *committer;
1904 char *refs_str = NULL;
1905 struct got_reflist_entry *re;
1907 SIMPLEQ_FOREACH(re, refs, entry) {
1910 struct got_tag_object *tag = NULL;
1913 name = got_ref_get_name(re->ref);
1914 if (strcmp(name, GOT_REF_HEAD) == 0)
1916 if (strncmp(name, "refs/", 5) == 0)
1918 if (strncmp(name, "got/", 4) == 0)
1920 if (strncmp(name, "heads/", 6) == 0)
1922 if (strncmp(name, "remotes/", 8) == 0)
1924 if (strncmp(name, "tags/", 5) == 0) {
1925 err = got_object_open_as_tag(&tag, repo, re->id);
1927 if (err->code != GOT_ERR_OBJ_TYPE)
1929 /* Ref points at something other than a tag. */
1934 cmp = got_object_id_cmp(tag ?
1935 got_object_tag_get_object_id(tag) : re->id, id);
1937 got_object_tag_close(tag);
1941 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1943 err = got_error_from_errno("asprintf");
1949 err = got_object_id_str(&id_str, id);
1953 printf(GOT_COMMIT_SEP_STR);
1954 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1955 refs_str ? refs_str : "", refs_str ? ")" : "");
1960 printf("from: %s\n", got_object_commit_get_author(commit));
1961 committer_time = got_object_commit_get_committer_time(commit);
1962 datestr = get_datestr(&committer_time, datebuf);
1964 printf("date: %s UTC\n", datestr);
1965 author = got_object_commit_get_author(commit);
1966 committer = got_object_commit_get_committer(commit);
1967 if (strcmp(author, committer) != 0)
1968 printf("via: %s\n", committer);
1969 if (got_object_commit_get_nparents(commit) > 1) {
1970 const struct got_object_id_queue *parent_ids;
1971 struct got_object_qid *qid;
1973 parent_ids = got_object_commit_get_parent_ids(commit);
1974 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1975 err = got_object_id_str(&id_str, qid->id);
1978 printf("parent %d: %s\n", n++, id_str);
1983 err = got_object_commit_get_logmsg(&logmsg0, commit);
1989 line = strsep(&logmsg, "\n");
1991 printf(" %s\n", line);
1996 err = print_patch(commit, id, path, diff_context, repo);
2001 if (fflush(stdout) != 0 && err == NULL)
2002 err = got_error_from_errno("fflush");
2006 static const struct got_error *
2007 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2008 const char *path, int show_patch, const char *search_pattern,
2009 int diff_context, int limit, int log_branches,
2010 struct got_reflist_head *refs)
2012 const struct got_error *err;
2013 struct got_commit_graph *graph;
2017 if (search_pattern &&
2018 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2019 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2021 err = got_commit_graph_open(&graph, path, !log_branches);
2024 err = got_commit_graph_iter_start(graph, root_id, repo,
2025 check_cancelled, NULL);
2029 struct got_commit_object *commit;
2030 struct got_object_id *id;
2032 if (sigint_received || sigpipe_received)
2035 err = got_commit_graph_iter_next(&id, graph, repo,
2036 check_cancelled, NULL);
2038 if (err->code == GOT_ERR_ITER_COMPLETED)
2045 err = got_object_open_as_commit(&commit, repo, id);
2049 if (search_pattern) {
2050 err = match_logmsg(&have_match, id, commit, ®ex);
2052 got_object_commit_close(commit);
2055 if (have_match == 0) {
2056 got_object_commit_close(commit);
2061 err = print_commit(commit, id, repo, path, show_patch,
2062 diff_context, refs);
2063 got_object_commit_close(commit);
2064 if (err || (limit && --limit == 0))
2070 got_commit_graph_close(graph);
2077 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2078 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2083 get_default_log_limit(void)
2085 const char *got_default_log_limit;
2089 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2090 if (got_default_log_limit == NULL)
2092 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2098 static const struct got_error *
2099 cmd_log(int argc, char *argv[])
2101 const struct got_error *error;
2102 struct got_repository *repo = NULL;
2103 struct got_worktree *worktree = NULL;
2104 struct got_commit_object *commit = NULL;
2105 struct got_object_id *id = NULL;
2106 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2107 const char *start_commit = NULL, *search_pattern = NULL;
2108 int diff_context = -1, ch;
2109 int show_patch = 0, limit = 0, log_branches = 0;
2111 struct got_reflist_head refs;
2113 SIMPLEQ_INIT(&refs);
2116 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2122 limit = get_default_log_limit();
2124 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2130 start_commit = optarg;
2133 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2136 err(1, "-C option %s", errstr);
2139 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2141 err(1, "-l option %s", errstr);
2147 repo_path = realpath(optarg, NULL);
2148 if (repo_path == NULL)
2149 return got_error_from_errno2("realpath",
2151 got_path_strip_trailing_slashes(repo_path);
2154 search_pattern = optarg;
2165 if (diff_context == -1)
2167 else if (!show_patch)
2168 errx(1, "-C reguires -p");
2170 cwd = getcwd(NULL, 0);
2172 error = got_error_from_errno("getcwd");
2176 error = got_worktree_open(&worktree, cwd);
2177 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2184 error = got_error_from_errno("strdup");
2187 } else if (argc == 1) {
2189 error = got_worktree_resolve_path(&path, worktree,
2194 path = strdup(argv[0]);
2196 error = got_error_from_errno("strdup");
2203 if (repo_path == NULL) {
2204 repo_path = worktree ?
2205 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2207 if (repo_path == NULL) {
2208 error = got_error_from_errno("strdup");
2212 error = got_repo_open(&repo, repo_path, NULL);
2216 error = apply_unveil(got_repo_get_path(repo), 1,
2217 worktree ? got_worktree_get_root_path(worktree) : NULL);
2221 if (start_commit == NULL) {
2222 struct got_reference *head_ref;
2223 error = got_ref_open(&head_ref, repo,
2224 worktree ? got_worktree_get_head_ref_name(worktree)
2228 error = got_ref_resolve(&id, repo, head_ref);
2229 got_ref_close(head_ref);
2232 error = got_object_open_as_commit(&commit, repo, id);
2234 struct got_reference *ref;
2235 error = got_ref_open(&ref, repo, start_commit, 0);
2236 if (error == NULL) {
2238 error = got_ref_resolve(&id, repo, ref);
2242 error = got_object_get_type(&obj_type, repo, id);
2245 if (obj_type == GOT_OBJ_TYPE_TAG) {
2246 struct got_tag_object *tag;
2247 error = got_object_open_as_tag(&tag, repo, id);
2250 if (got_object_tag_get_object_type(tag) !=
2251 GOT_OBJ_TYPE_COMMIT) {
2252 got_object_tag_close(tag);
2253 error = got_error(GOT_ERR_OBJ_TYPE);
2257 id = got_object_id_dup(
2258 got_object_tag_get_object_id(tag));
2260 error = got_error_from_errno(
2261 "got_object_id_dup");
2262 got_object_tag_close(tag);
2265 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2266 error = got_error(GOT_ERR_OBJ_TYPE);
2269 error = got_object_open_as_commit(&commit, repo, id);
2273 if (commit == NULL) {
2274 error = got_repo_match_object_id_prefix(&id,
2275 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2284 const char *prefix = got_worktree_get_path_prefix(worktree);
2286 if (asprintf(&p, "%s%s%s", prefix,
2287 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2288 error = got_error_from_errno("asprintf");
2291 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2294 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2299 path = in_repo_path;
2302 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2306 error = print_commits(id, repo, path, show_patch, search_pattern,
2307 diff_context, limit, log_branches, &refs);
2314 got_worktree_close(worktree);
2316 const struct got_error *repo_error;
2317 repo_error = got_repo_close(repo);
2321 got_ref_list_free(&refs);
2328 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2329 "[-w] [object1 object2 | path]\n", getprogname());
2333 struct print_diff_arg {
2334 struct got_repository *repo;
2335 struct got_worktree *worktree;
2340 int ignore_whitespace;
2343 static const struct got_error *
2344 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2345 const char *path, struct got_object_id *blob_id,
2346 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2347 int dirfd, const char *de_name)
2349 struct print_diff_arg *a = arg;
2350 const struct got_error *err = NULL;
2351 struct got_blob_object *blob1 = NULL;
2354 char *abspath = NULL, *label1 = NULL;
2357 if (a->diff_staged) {
2358 if (staged_status != GOT_STATUS_MODIFY &&
2359 staged_status != GOT_STATUS_ADD &&
2360 staged_status != GOT_STATUS_DELETE)
2363 if (staged_status == GOT_STATUS_DELETE)
2365 if (status == GOT_STATUS_NONEXISTENT)
2366 return got_error_set_errno(ENOENT, path);
2367 if (status != GOT_STATUS_MODIFY &&
2368 status != GOT_STATUS_ADD &&
2369 status != GOT_STATUS_DELETE &&
2370 status != GOT_STATUS_CONFLICT)
2374 if (!a->header_shown) {
2375 printf("diff %s %s%s\n", a->id_str,
2376 got_worktree_get_root_path(a->worktree),
2377 a->diff_staged ? " (staged changes)" : "");
2378 a->header_shown = 1;
2381 if (a->diff_staged) {
2382 const char *label1 = NULL, *label2 = NULL;
2383 switch (staged_status) {
2384 case GOT_STATUS_MODIFY:
2388 case GOT_STATUS_ADD:
2391 case GOT_STATUS_DELETE:
2395 return got_error(GOT_ERR_FILE_STATUS);
2397 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2398 label1, label2, a->diff_context, a->ignore_whitespace,
2402 if (staged_status == GOT_STATUS_ADD ||
2403 staged_status == GOT_STATUS_MODIFY) {
2405 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2409 err = got_object_id_str(&id_str, staged_blob_id);
2412 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2413 err = got_error_from_errno("asprintf");
2418 } else if (status != GOT_STATUS_ADD) {
2419 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2424 if (status != GOT_STATUS_DELETE) {
2425 if (asprintf(&abspath, "%s/%s",
2426 got_worktree_get_root_path(a->worktree), path) == -1) {
2427 err = got_error_from_errno("asprintf");
2432 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2434 err = got_error_from_errno2("openat", abspath);
2438 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2440 err = got_error_from_errno2("open", abspath);
2444 if (fstat(fd, &sb) == -1) {
2445 err = got_error_from_errno2("fstat", abspath);
2448 f2 = fdopen(fd, "r");
2450 err = got_error_from_errno2("fdopen", abspath);
2457 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2458 a->diff_context, a->ignore_whitespace, stdout);
2461 got_object_blob_close(blob1);
2462 if (f2 && fclose(f2) == EOF && err == NULL)
2463 err = got_error_from_errno("fclose");
2464 if (fd != -1 && close(fd) == -1 && err == NULL)
2465 err = got_error_from_errno("close");
2470 static const struct got_error *
2471 cmd_diff(int argc, char *argv[])
2473 const struct got_error *error;
2474 struct got_repository *repo = NULL;
2475 struct got_worktree *worktree = NULL;
2476 char *cwd = NULL, *repo_path = NULL;
2477 struct got_object_id *id1 = NULL, *id2 = NULL;
2478 const char *id_str1 = NULL, *id_str2 = NULL;
2479 char *label1 = NULL, *label2 = NULL;
2481 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2486 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2491 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2494 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2497 err(1, "-C option %s", errstr);
2500 repo_path = realpath(optarg, NULL);
2501 if (repo_path == NULL)
2502 return got_error_from_errno2("realpath",
2504 got_path_strip_trailing_slashes(repo_path);
2510 ignore_whitespace = 1;
2521 cwd = getcwd(NULL, 0);
2523 error = got_error_from_errno("getcwd");
2526 error = got_worktree_open(&worktree, cwd);
2527 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2530 if (worktree == NULL) {
2531 error = got_error(GOT_ERR_NOT_WORKTREE);
2536 "-r option can't be used when diffing a work tree");
2537 repo_path = strdup(got_worktree_get_repo_path(worktree));
2538 if (repo_path == NULL) {
2539 error = got_error_from_errno("strdup");
2543 error = got_worktree_resolve_path(&path, worktree,
2550 error = got_error_from_errno("strdup");
2554 } else if (argc == 2) {
2556 errx(1, "-s option can't be used when diffing "
2557 "objects in repository");
2560 if (worktree && repo_path == NULL) {
2562 strdup(got_worktree_get_repo_path(worktree));
2563 if (repo_path == NULL) {
2564 error = got_error_from_errno("strdup");
2571 if (repo_path == NULL) {
2572 repo_path = getcwd(NULL, 0);
2573 if (repo_path == NULL)
2574 return got_error_from_errno("getcwd");
2577 error = got_repo_open(&repo, repo_path, NULL);
2582 error = apply_unveil(got_repo_get_path(repo), 1,
2583 worktree ? got_worktree_get_root_path(worktree) : NULL);
2588 struct print_diff_arg arg;
2589 struct got_pathlist_head paths;
2594 error = got_object_id_str(&id_str,
2595 got_worktree_get_base_commit_id(worktree));
2599 arg.worktree = worktree;
2600 arg.diff_context = diff_context;
2601 arg.id_str = id_str;
2602 arg.header_shown = 0;
2603 arg.diff_staged = diff_staged;
2604 arg.ignore_whitespace = ignore_whitespace;
2606 error = got_pathlist_append(&paths, path, NULL);
2610 error = got_worktree_status(worktree, &paths, repo, print_diff,
2611 &arg, check_cancelled, NULL);
2613 got_pathlist_free(&paths);
2617 error = got_repo_match_object_id(&id1, &label1, id_str1,
2618 GOT_OBJ_TYPE_ANY, 1, repo);
2622 error = got_repo_match_object_id(&id2, &label2, id_str2,
2623 GOT_OBJ_TYPE_ANY, 1, repo);
2627 error = got_object_get_type(&type1, repo, id1);
2631 error = got_object_get_type(&type2, repo, id2);
2635 if (type1 != type2) {
2636 error = got_error(GOT_ERR_OBJ_TYPE);
2641 case GOT_OBJ_TYPE_BLOB:
2642 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2643 diff_context, ignore_whitespace, repo, stdout);
2645 case GOT_OBJ_TYPE_TREE:
2646 error = got_diff_objects_as_trees(id1, id2, "", "",
2647 diff_context, ignore_whitespace, repo, stdout);
2649 case GOT_OBJ_TYPE_COMMIT:
2650 printf("diff %s %s\n", label1, label2);
2651 error = got_diff_objects_as_commits(id1, id2, diff_context,
2652 ignore_whitespace, repo, stdout);
2655 error = got_error(GOT_ERR_OBJ_TYPE);
2664 got_worktree_close(worktree);
2666 const struct got_error *repo_error;
2667 repo_error = got_repo_close(repo);
2678 "usage: %s blame [-c commit] [-r repository-path] path\n",
2687 char datebuf[11]; /* YYYY-MM-DD + NUL */
2690 struct blame_cb_args {
2691 struct blame_line *lines;
2695 off_t *line_offsets;
2697 struct got_repository *repo;
2700 static const struct got_error *
2701 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2703 const struct got_error *err = NULL;
2704 struct blame_cb_args *a = arg;
2705 struct blame_line *bline;
2707 size_t linesize = 0;
2708 struct got_commit_object *commit = NULL;
2711 time_t committer_time;
2713 if (nlines != a->nlines ||
2714 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2715 return got_error(GOT_ERR_RANGE);
2717 if (sigint_received)
2718 return got_error(GOT_ERR_ITER_COMPLETED);
2721 return NULL; /* no change in this commit */
2723 /* Annotate this line. */
2724 bline = &a->lines[lineno - 1];
2725 if (bline->annotated)
2727 err = got_object_id_str(&bline->id_str, id);
2731 err = got_object_open_as_commit(&commit, a->repo, id);
2735 bline->committer = strdup(got_object_commit_get_committer(commit));
2736 if (bline->committer == NULL) {
2737 err = got_error_from_errno("strdup");
2741 committer_time = got_object_commit_get_committer_time(commit);
2742 if (localtime_r(&committer_time, &tm) == NULL)
2743 return got_error_from_errno("localtime_r");
2744 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2745 &tm) >= sizeof(bline->datebuf)) {
2746 err = got_error(GOT_ERR_NO_SPACE);
2749 bline->annotated = 1;
2751 /* Print lines annotated so far. */
2752 bline = &a->lines[a->lineno_cur - 1];
2753 if (!bline->annotated)
2756 offset = a->line_offsets[a->lineno_cur - 1];
2757 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2758 err = got_error_from_errno("fseeko");
2762 while (bline->annotated) {
2763 char *smallerthan, *at, *nl, *committer;
2766 if (getline(&line, &linesize, a->f) == -1) {
2768 err = got_error_from_errno("getline");
2772 committer = bline->committer;
2773 smallerthan = strchr(committer, '<');
2774 if (smallerthan && smallerthan[1] != '\0')
2775 committer = smallerthan + 1;
2776 at = strchr(committer, '@');
2779 len = strlen(committer);
2781 committer[8] = '\0';
2783 nl = strchr(line, '\n');
2786 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2787 bline->id_str, bline->datebuf, committer, line);
2790 bline = &a->lines[a->lineno_cur - 1];
2794 got_object_commit_close(commit);
2799 static const struct got_error *
2800 cmd_blame(int argc, char *argv[])
2802 const struct got_error *error;
2803 struct got_repository *repo = NULL;
2804 struct got_worktree *worktree = NULL;
2805 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2806 struct got_object_id *obj_id = NULL;
2807 struct got_object_id *commit_id = NULL;
2808 struct got_blob_object *blob = NULL;
2809 char *commit_id_str = NULL;
2810 struct blame_cb_args bca;
2811 int ch, obj_type, i;
2814 memset(&bca, 0, sizeof(bca));
2817 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2822 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2825 commit_id_str = optarg;
2828 repo_path = realpath(optarg, NULL);
2829 if (repo_path == NULL)
2830 return got_error_from_errno2("realpath",
2832 got_path_strip_trailing_slashes(repo_path);
2848 cwd = getcwd(NULL, 0);
2850 error = got_error_from_errno("getcwd");
2853 if (repo_path == NULL) {
2854 error = got_worktree_open(&worktree, cwd);
2855 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2861 strdup(got_worktree_get_repo_path(worktree));
2862 if (repo_path == NULL)
2863 error = got_error_from_errno("strdup");
2867 repo_path = strdup(cwd);
2868 if (repo_path == NULL) {
2869 error = got_error_from_errno("strdup");
2875 error = got_repo_open(&repo, repo_path, NULL);
2879 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2884 const char *prefix = got_worktree_get_path_prefix(worktree);
2885 char *p, *worktree_subdir = cwd +
2886 strlen(got_worktree_get_root_path(worktree));
2887 if (asprintf(&p, "%s%s%s%s%s",
2888 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2889 worktree_subdir, worktree_subdir[0] ? "/" : "",
2891 error = got_error_from_errno("asprintf");
2894 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2897 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2902 if (commit_id_str == NULL) {
2903 struct got_reference *head_ref;
2904 error = got_ref_open(&head_ref, repo, worktree ?
2905 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2908 error = got_ref_resolve(&commit_id, repo, head_ref);
2909 got_ref_close(head_ref);
2913 error = got_repo_match_object_id(&commit_id, NULL,
2914 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2919 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2923 error = got_object_get_type(&obj_type, repo, obj_id);
2927 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2928 error = got_error(GOT_ERR_OBJ_TYPE);
2932 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2935 bca.f = got_opentemp();
2936 if (bca.f == NULL) {
2937 error = got_error_from_errno("got_opentemp");
2940 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2941 &bca.line_offsets, bca.f, blob);
2942 if (error || bca.nlines == 0)
2945 /* Don't include \n at EOF in the blame line count. */
2946 if (bca.line_offsets[bca.nlines - 1] == filesize)
2949 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2950 if (bca.lines == NULL) {
2951 error = got_error_from_errno("calloc");
2955 bca.nlines_prec = 0;
2963 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2964 check_cancelled, NULL);
2972 got_object_blob_close(blob);
2974 got_worktree_close(worktree);
2976 const struct got_error *repo_error;
2977 repo_error = got_repo_close(repo);
2982 for (i = 0; i < bca.nlines; i++) {
2983 struct blame_line *bline = &bca.lines[i];
2984 free(bline->id_str);
2985 free(bline->committer);
2989 free(bca.line_offsets);
2990 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2991 error = got_error_from_errno("fclose");
2999 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3005 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3006 const char *root_path)
3008 int is_root_path = (strcmp(path, root_path) == 0);
3009 const char *modestr = "";
3010 mode_t mode = got_tree_entry_get_mode(te);
3012 path += strlen(root_path);
3013 while (path[0] == '/')
3016 if (got_object_tree_entry_is_submodule(te))
3018 else if (S_ISLNK(mode))
3020 else if (S_ISDIR(mode))
3022 else if (mode & S_IXUSR)
3025 printf("%s%s%s%s%s\n", id ? id : "", path,
3026 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3029 static const struct got_error *
3030 print_tree(const char *path, struct got_object_id *commit_id,
3031 int show_ids, int recurse, const char *root_path,
3032 struct got_repository *repo)
3034 const struct got_error *err = NULL;
3035 struct got_object_id *tree_id = NULL;
3036 struct got_tree_object *tree = NULL;
3039 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3043 err = got_object_open_as_tree(&tree, repo, tree_id);
3046 nentries = got_object_tree_get_nentries(tree);
3047 for (i = 0; i < nentries; i++) {
3048 struct got_tree_entry *te;
3051 if (sigint_received || sigpipe_received)
3054 te = got_object_tree_get_entry(tree, i);
3057 err = got_object_id_str(&id_str,
3058 got_tree_entry_get_id(te));
3061 if (asprintf(&id, "%s ", id_str) == -1) {
3062 err = got_error_from_errno("asprintf");
3068 print_entry(te, id, path, root_path);
3071 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3073 if (asprintf(&child_path, "%s%s%s", path,
3074 path[0] == '/' && path[1] == '\0' ? "" : "/",
3075 got_tree_entry_get_name(te)) == -1) {
3076 err = got_error_from_errno("asprintf");
3079 err = print_tree(child_path, commit_id, show_ids, 1,
3088 got_object_tree_close(tree);
3093 static const struct got_error *
3094 cmd_tree(int argc, char *argv[])
3096 const struct got_error *error;
3097 struct got_repository *repo = NULL;
3098 struct got_worktree *worktree = NULL;
3100 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3101 struct got_object_id *commit_id = NULL;
3102 char *commit_id_str = NULL;
3103 int show_ids = 0, recurse = 0;
3107 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3112 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3115 commit_id_str = optarg;
3118 repo_path = realpath(optarg, NULL);
3119 if (repo_path == NULL)
3120 return got_error_from_errno2("realpath",
3122 got_path_strip_trailing_slashes(repo_path);
3146 cwd = getcwd(NULL, 0);
3148 error = got_error_from_errno("getcwd");
3151 if (repo_path == NULL) {
3152 error = got_worktree_open(&worktree, cwd);
3153 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3159 strdup(got_worktree_get_repo_path(worktree));
3160 if (repo_path == NULL)
3161 error = got_error_from_errno("strdup");
3165 repo_path = strdup(cwd);
3166 if (repo_path == NULL) {
3167 error = got_error_from_errno("strdup");
3173 error = got_repo_open(&repo, repo_path, NULL);
3177 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3183 char *p, *worktree_subdir = cwd +
3184 strlen(got_worktree_get_root_path(worktree));
3185 if (asprintf(&p, "%s/%s",
3186 got_worktree_get_path_prefix(worktree),
3187 worktree_subdir) == -1) {
3188 error = got_error_from_errno("asprintf");
3191 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3198 if (in_repo_path == NULL) {
3199 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3204 if (commit_id_str == NULL) {
3205 struct got_reference *head_ref;
3206 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3209 error = got_ref_resolve(&commit_id, repo, head_ref);
3210 got_ref_close(head_ref);
3214 error = got_repo_match_object_id(&commit_id, NULL,
3215 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3220 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3221 in_repo_path, repo);
3228 got_worktree_close(worktree);
3230 const struct got_error *repo_error;
3231 repo_error = got_repo_close(repo);
3241 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3245 static const struct got_error *
3246 print_status(void *arg, unsigned char status, unsigned char staged_status,
3247 const char *path, struct got_object_id *blob_id,
3248 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3249 int dirfd, const char *de_name)
3251 if (status == staged_status && (status == GOT_STATUS_DELETE))
3252 status = GOT_STATUS_NO_CHANGE;
3253 printf("%c%c %s\n", status, staged_status, path);
3257 static const struct got_error *
3258 cmd_status(int argc, char *argv[])
3260 const struct got_error *error = NULL;
3261 struct got_repository *repo = NULL;
3262 struct got_worktree *worktree = NULL;
3264 struct got_pathlist_head paths;
3265 struct got_pathlist_entry *pe;
3270 while ((ch = getopt(argc, argv, "")) != -1) {
3282 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3286 cwd = getcwd(NULL, 0);
3288 error = got_error_from_errno("getcwd");
3292 error = got_worktree_open(&worktree, cwd);
3296 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3301 error = apply_unveil(got_repo_get_path(repo), 1,
3302 got_worktree_get_root_path(worktree));
3306 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3310 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3311 check_cancelled, NULL);
3313 TAILQ_FOREACH(pe, &paths, entry)
3314 free((char *)pe->path);
3315 got_pathlist_free(&paths);
3324 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3329 static const struct got_error *
3330 list_refs(struct got_repository *repo)
3332 static const struct got_error *err = NULL;
3333 struct got_reflist_head refs;
3334 struct got_reflist_entry *re;
3336 SIMPLEQ_INIT(&refs);
3337 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3341 SIMPLEQ_FOREACH(re, &refs, entry) {
3343 refstr = got_ref_to_str(re->ref);
3345 return got_error_from_errno("got_ref_to_str");
3346 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3350 got_ref_list_free(&refs);
3354 static const struct got_error *
3355 delete_ref(struct got_repository *repo, const char *refname)
3357 const struct got_error *err = NULL;
3358 struct got_reference *ref;
3360 err = got_ref_open(&ref, repo, refname, 0);
3364 err = got_ref_delete(ref, repo);
3369 static const struct got_error *
3370 add_ref(struct got_repository *repo, const char *refname, const char *target)
3372 const struct got_error *err = NULL;
3373 struct got_object_id *id;
3374 struct got_reference *ref = NULL;
3377 * Don't let the user create a reference name with a leading '-'.
3378 * While technically a valid reference name, this case is usually
3379 * an unintended typo.
3381 if (refname[0] == '-')
3382 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3384 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3387 struct got_reference *target_ref;
3389 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3391 err = got_ref_open(&target_ref, repo, target, 0);
3394 err = got_ref_resolve(&id, repo, target_ref);
3395 got_ref_close(target_ref);
3400 err = got_ref_alloc(&ref, refname, id);
3404 err = got_ref_write(ref, repo);
3412 static const struct got_error *
3413 add_symref(struct got_repository *repo, const char *refname, const char *target)
3415 const struct got_error *err = NULL;
3416 struct got_reference *ref = NULL;
3417 struct got_reference *target_ref = NULL;
3420 * Don't let the user create a reference name with a leading '-'.
3421 * While technically a valid reference name, this case is usually
3422 * an unintended typo.
3424 if (refname[0] == '-')
3425 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3427 err = got_ref_open(&target_ref, repo, target, 0);
3431 err = got_ref_alloc_symref(&ref, refname, target_ref);
3435 err = got_ref_write(ref, repo);
3438 got_ref_close(target_ref);
3444 static const struct got_error *
3445 cmd_ref(int argc, char *argv[])
3447 const struct got_error *error = NULL;
3448 struct got_repository *repo = NULL;
3449 struct got_worktree *worktree = NULL;
3450 char *cwd = NULL, *repo_path = NULL;
3451 int ch, do_list = 0, create_symref = 0;
3452 const char *delref = NULL;
3454 /* TODO: Add -s option for adding symbolic references. */
3455 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3461 repo_path = realpath(optarg, NULL);
3462 if (repo_path == NULL)
3463 return got_error_from_errno2("realpath",
3465 got_path_strip_trailing_slashes(repo_path);
3479 if (do_list && delref)
3480 errx(1, "-l and -d options are mutually exclusive\n");
3485 if (do_list || delref) {
3487 errx(1, "-s option cannot be used together with the "
3488 "-l or -d options");
3491 } else if (argc != 2)
3496 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3500 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3501 "sendfd unveil", NULL) == -1)
3505 cwd = getcwd(NULL, 0);
3507 error = got_error_from_errno("getcwd");
3511 if (repo_path == NULL) {
3512 error = got_worktree_open(&worktree, cwd);
3513 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3519 strdup(got_worktree_get_repo_path(worktree));
3520 if (repo_path == NULL)
3521 error = got_error_from_errno("strdup");
3525 repo_path = strdup(cwd);
3526 if (repo_path == NULL) {
3527 error = got_error_from_errno("strdup");
3533 error = got_repo_open(&repo, repo_path, NULL);
3537 error = apply_unveil(got_repo_get_path(repo), do_list,
3538 worktree ? got_worktree_get_root_path(worktree) : NULL);
3543 error = list_refs(repo);
3545 error = delete_ref(repo, delref);
3546 else if (create_symref)
3547 error = add_symref(repo, argv[0], argv[1]);
3549 error = add_ref(repo, argv[0], argv[1]);
3552 got_repo_close(repo);
3554 got_worktree_close(worktree);
3564 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3565 "[name]\n", getprogname());
3569 static const struct got_error *
3570 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3571 struct got_reference *ref)
3573 const struct got_error *err = NULL;
3574 const char *refname, *marker = " ";
3577 refname = got_ref_get_name(ref);
3578 if (worktree && strcmp(refname,
3579 got_worktree_get_head_ref_name(worktree)) == 0) {
3580 struct got_object_id *id = NULL;
3582 err = got_ref_resolve(&id, repo, ref);
3585 if (got_object_id_cmp(id,
3586 got_worktree_get_base_commit_id(worktree)) == 0)
3593 if (strncmp(refname, "refs/heads/", 11) == 0)
3595 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3598 refstr = got_ref_to_str(ref);
3600 return got_error_from_errno("got_ref_to_str");
3602 printf("%s%s: %s\n", marker, refname, refstr);
3607 static const struct got_error *
3608 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3610 const char *refname;
3612 if (worktree == NULL)
3613 return got_error(GOT_ERR_NOT_WORKTREE);
3615 refname = got_worktree_get_head_ref_name(worktree);
3617 if (strncmp(refname, "refs/heads/", 11) == 0)
3619 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3622 printf("%s\n", refname);
3627 static const struct got_error *
3628 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3630 static const struct got_error *err = NULL;
3631 struct got_reflist_head refs;
3632 struct got_reflist_entry *re;
3633 struct got_reference *temp_ref = NULL;
3634 int rebase_in_progress, histedit_in_progress;
3636 SIMPLEQ_INIT(&refs);
3639 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3644 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3649 if (rebase_in_progress || histedit_in_progress) {
3650 err = got_ref_open(&temp_ref, repo,
3651 got_worktree_get_head_ref_name(worktree), 0);
3654 list_branch(repo, worktree, temp_ref);
3655 got_ref_close(temp_ref);
3659 err = got_ref_list(&refs, repo, "refs/heads",
3660 got_ref_cmp_by_name, NULL);
3664 SIMPLEQ_FOREACH(re, &refs, entry)
3665 list_branch(repo, worktree, re->ref);
3667 got_ref_list_free(&refs);
3671 static const struct got_error *
3672 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3673 const char *branch_name)
3675 const struct got_error *err = NULL;
3676 struct got_reference *ref = NULL;
3679 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3680 return got_error_from_errno("asprintf");
3682 err = got_ref_open(&ref, repo, refname, 0);
3687 strcmp(got_worktree_get_head_ref_name(worktree),
3688 got_ref_get_name(ref)) == 0) {
3689 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3690 "will not delete this work tree's current branch");
3694 err = got_ref_delete(ref, repo);
3702 static const struct got_error *
3703 add_branch(struct got_repository *repo, const char *branch_name,
3704 struct got_object_id *base_commit_id)
3706 const struct got_error *err = NULL;
3707 struct got_reference *ref = NULL;
3708 char *base_refname = NULL, *refname = NULL;
3711 * Don't let the user create a branch name with a leading '-'.
3712 * While technically a valid reference name, this case is usually
3713 * an unintended typo.
3715 if (branch_name[0] == '-')
3716 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3718 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3719 err = got_error_from_errno("asprintf");
3723 err = got_ref_open(&ref, repo, refname, 0);
3725 err = got_error(GOT_ERR_BRANCH_EXISTS);
3727 } else if (err->code != GOT_ERR_NOT_REF)
3730 err = got_ref_alloc(&ref, refname, base_commit_id);
3734 err = got_ref_write(ref, repo);
3743 static const struct got_error *
3744 cmd_branch(int argc, char *argv[])
3746 const struct got_error *error = NULL;
3747 struct got_repository *repo = NULL;
3748 struct got_worktree *worktree = NULL;
3749 char *cwd = NULL, *repo_path = NULL;
3750 int ch, do_list = 0, do_show = 0, do_update = 1;
3751 const char *delref = NULL, *commit_id_arg = NULL;
3752 struct got_reference *ref = NULL;
3753 struct got_pathlist_head paths;
3754 struct got_pathlist_entry *pe;
3755 struct got_object_id *commit_id = NULL;
3756 char *commit_id_str = NULL;
3760 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3763 commit_id_arg = optarg;
3769 repo_path = realpath(optarg, NULL);
3770 if (repo_path == NULL)
3771 return got_error_from_errno2("realpath",
3773 got_path_strip_trailing_slashes(repo_path);
3787 if (do_list && delref)
3788 errx(1, "-l and -d options are mutually exclusive\n");
3793 if (!do_list && !delref && argc == 0)
3796 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3797 errx(1, "-c option can only be used when creating a branch");
3799 if (do_list || delref) {
3802 } else if (!do_show && argc != 1)
3806 if (do_list || do_show) {
3807 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3811 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3812 "sendfd unveil", NULL) == -1)
3816 cwd = getcwd(NULL, 0);
3818 error = got_error_from_errno("getcwd");
3822 if (repo_path == NULL) {
3823 error = got_worktree_open(&worktree, cwd);
3824 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3830 strdup(got_worktree_get_repo_path(worktree));
3831 if (repo_path == NULL)
3832 error = got_error_from_errno("strdup");
3836 repo_path = strdup(cwd);
3837 if (repo_path == NULL) {
3838 error = got_error_from_errno("strdup");
3844 error = got_repo_open(&repo, repo_path, NULL);
3848 error = apply_unveil(got_repo_get_path(repo), do_list,
3849 worktree ? got_worktree_get_root_path(worktree) : NULL);
3854 error = show_current_branch(repo, worktree);
3856 error = list_branches(repo, worktree);
3858 error = delete_branch(repo, worktree, delref);
3860 if (commit_id_arg == NULL)
3861 commit_id_arg = worktree ?
3862 got_worktree_get_head_ref_name(worktree) :
3864 error = got_repo_match_object_id(&commit_id, NULL,
3865 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3868 error = add_branch(repo, argv[0], commit_id);
3871 if (worktree && do_update) {
3872 int did_something = 0;
3873 char *branch_refname = NULL;
3875 error = got_object_id_str(&commit_id_str, commit_id);
3878 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3882 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3884 error = got_error_from_errno("asprintf");
3887 error = got_ref_open(&ref, repo, branch_refname, 0);
3888 free(branch_refname);
3891 error = switch_head_ref(ref, commit_id, worktree,
3895 error = got_worktree_set_base_commit_id(worktree, repo,
3899 error = got_worktree_checkout_files(worktree, &paths,
3900 repo, update_progress, &did_something,
3901 check_cancelled, NULL);
3905 printf("Updated to commit %s\n", commit_id_str);
3912 got_repo_close(repo);
3914 got_worktree_close(worktree);
3918 free(commit_id_str);
3919 TAILQ_FOREACH(pe, &paths, entry)
3920 free((char *)pe->path);
3921 got_pathlist_free(&paths);
3930 "usage: %s tag [-c commit] [-r repository] [-l] "
3931 "[-m message] name\n", getprogname());
3936 static const struct got_error *
3937 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3939 const struct got_error *err = NULL;
3940 struct got_reflist_entry *re, *se, *new;
3941 struct got_object_id *re_id, *se_id;
3942 struct got_tag_object *re_tag, *se_tag;
3943 time_t re_time, se_time;
3945 SIMPLEQ_FOREACH(re, tags, entry) {
3946 se = SIMPLEQ_FIRST(sorted);
3948 err = got_reflist_entry_dup(&new, re);
3951 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3954 err = got_ref_resolve(&re_id, repo, re->ref);
3957 err = got_object_open_as_tag(&re_tag, repo, re_id);
3961 re_time = got_object_tag_get_tagger_time(re_tag);
3962 got_object_tag_close(re_tag);
3966 err = got_ref_resolve(&se_id, repo, re->ref);
3969 err = got_object_open_as_tag(&se_tag, repo, se_id);
3973 se_time = got_object_tag_get_tagger_time(se_tag);
3974 got_object_tag_close(se_tag);
3976 if (se_time > re_time) {
3977 err = got_reflist_entry_dup(&new, re);
3980 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3983 se = SIMPLEQ_NEXT(se, entry);
3992 static const struct got_error *
3993 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3995 static const struct got_error *err = NULL;
3996 struct got_reflist_head refs;
3997 struct got_reflist_entry *re;
3999 SIMPLEQ_INIT(&refs);
4001 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4005 SIMPLEQ_FOREACH(re, &refs, entry) {
4006 const char *refname;
4007 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4011 struct got_object_id *id;
4012 struct got_tag_object *tag;
4013 struct got_commit_object *commit = NULL;
4015 refname = got_ref_get_name(re->ref);
4016 if (strncmp(refname, "refs/tags/", 10) != 0)
4019 refstr = got_ref_to_str(re->ref);
4020 if (refstr == NULL) {
4021 err = got_error_from_errno("got_ref_to_str");
4024 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4027 err = got_ref_resolve(&id, repo, re->ref);
4030 err = got_object_open_as_tag(&tag, repo, id);
4032 if (err->code != GOT_ERR_OBJ_TYPE) {
4036 /* "lightweight" tag */
4037 err = got_object_open_as_commit(&commit, repo, id);
4042 tagger = got_object_commit_get_committer(commit);
4044 got_object_commit_get_committer_time(commit);
4045 err = got_object_id_str(&id_str, id);
4051 tagger = got_object_tag_get_tagger(tag);
4052 tagger_time = got_object_tag_get_tagger_time(tag);
4053 err = got_object_id_str(&id_str,
4054 got_object_tag_get_object_id(tag));
4058 printf("from: %s\n", tagger);
4059 datestr = get_datestr(&tagger_time, datebuf);
4061 printf("date: %s UTC\n", datestr);
4063 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4065 switch (got_object_tag_get_object_type(tag)) {
4066 case GOT_OBJ_TYPE_BLOB:
4067 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4070 case GOT_OBJ_TYPE_TREE:
4071 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4074 case GOT_OBJ_TYPE_COMMIT:
4075 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4078 case GOT_OBJ_TYPE_TAG:
4079 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4088 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4091 got_object_commit_close(commit);
4093 tagmsg0 = strdup(got_object_tag_get_message(tag));
4094 got_object_tag_close(tag);
4095 if (tagmsg0 == NULL) {
4096 err = got_error_from_errno("strdup");
4103 line = strsep(&tagmsg, "\n");
4105 printf(" %s\n", line);
4110 got_ref_list_free(&refs);
4114 static const struct got_error *
4115 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4116 const char *tag_name, const char *repo_path)
4118 const struct got_error *err = NULL;
4119 char *template = NULL, *initial_content = NULL;
4120 char *editor = NULL;
4123 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4124 err = got_error_from_errno("asprintf");
4128 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4129 commit_id_str, tag_name) == -1) {
4130 err = got_error_from_errno("asprintf");
4134 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4138 dprintf(fd, initial_content);
4141 err = get_editor(&editor);
4144 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4146 free(initial_content);
4150 /* Editor is done; we can now apply unveil(2) */
4152 err = apply_unveil(repo_path, 0, NULL);
4161 static const struct got_error *
4162 add_tag(struct got_repository *repo, const char *tag_name,
4163 const char *commit_arg, const char *tagmsg_arg)
4165 const struct got_error *err = NULL;
4166 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4167 char *label = NULL, *commit_id_str = NULL;
4168 struct got_reference *ref = NULL;
4169 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4170 char *tagmsg_path = NULL, *tag_id_str = NULL;
4171 int preserve_tagmsg = 0;
4174 * Don't let the user create a tag name with a leading '-'.
4175 * While technically a valid reference name, this case is usually
4176 * an unintended typo.
4178 if (tag_name[0] == '-')
4179 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4181 err = get_author(&tagger, repo);
4185 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4186 GOT_OBJ_TYPE_COMMIT, 1, repo);
4190 err = got_object_id_str(&commit_id_str, commit_id);
4194 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4195 refname = strdup(tag_name);
4196 if (refname == NULL) {
4197 err = got_error_from_errno("strdup");
4201 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4202 err = got_error_from_errno("asprintf");
4206 err = got_ref_open(&ref, repo, refname, 0);
4208 err = got_error(GOT_ERR_TAG_EXISTS);
4210 } else if (err->code != GOT_ERR_NOT_REF)
4213 if (tagmsg_arg == NULL) {
4214 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4215 tag_name, got_repo_get_path(repo));
4217 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4218 tagmsg_path != NULL)
4219 preserve_tagmsg = 1;
4224 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4225 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4228 preserve_tagmsg = 1;
4232 err = got_ref_alloc(&ref, refname, tag_id);
4235 preserve_tagmsg = 1;
4239 err = got_ref_write(ref, repo);
4242 preserve_tagmsg = 1;
4246 err = got_object_id_str(&tag_id_str, tag_id);
4249 preserve_tagmsg = 1;
4252 printf("Created tag %s\n", tag_id_str);
4254 if (preserve_tagmsg) {
4255 fprintf(stderr, "%s: tag message preserved in %s\n",
4256 getprogname(), tagmsg_path);
4257 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4258 err = got_error_from_errno2("unlink", tagmsg_path);
4263 free(commit_id_str);
4271 static const struct got_error *
4272 cmd_tag(int argc, char *argv[])
4274 const struct got_error *error = NULL;
4275 struct got_repository *repo = NULL;
4276 struct got_worktree *worktree = NULL;
4277 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4278 char *gitconfig_path = NULL;
4279 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4280 int ch, do_list = 0;
4282 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4285 commit_id_arg = optarg;
4291 repo_path = realpath(optarg, NULL);
4292 if (repo_path == NULL)
4293 return got_error_from_errno2("realpath",
4295 got_path_strip_trailing_slashes(repo_path);
4310 if (commit_id_arg != NULL)
4311 errx(1, "-c option can only be used when creating a tag");
4313 errx(1, "-l and -m options are mutually exclusive");
4316 } else if (argc != 1)
4323 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4327 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4328 "sendfd unveil", NULL) == -1)
4332 cwd = getcwd(NULL, 0);
4334 error = got_error_from_errno("getcwd");
4338 if (repo_path == NULL) {
4339 error = got_worktree_open(&worktree, cwd);
4340 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4346 strdup(got_worktree_get_repo_path(worktree));
4347 if (repo_path == NULL)
4348 error = got_error_from_errno("strdup");
4352 repo_path = strdup(cwd);
4353 if (repo_path == NULL) {
4354 error = got_error_from_errno("strdup");
4361 error = got_repo_open(&repo, repo_path, NULL);
4364 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4367 error = list_tags(repo, worktree);
4369 error = get_gitconfig_path(&gitconfig_path);
4372 error = got_repo_open(&repo, repo_path, gitconfig_path);
4377 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4382 if (commit_id_arg == NULL) {
4383 struct got_reference *head_ref;
4384 struct got_object_id *commit_id;
4385 error = got_ref_open(&head_ref, repo,
4386 worktree ? got_worktree_get_head_ref_name(worktree)
4390 error = got_ref_resolve(&commit_id, repo, head_ref);
4391 got_ref_close(head_ref);
4394 error = got_object_id_str(&commit_id_str, commit_id);
4400 error = add_tag(repo, tag_name,
4401 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4405 got_repo_close(repo);
4407 got_worktree_close(worktree);
4410 free(gitconfig_path);
4411 free(commit_id_str);
4418 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4423 static const struct got_error *
4424 add_progress(void *arg, unsigned char status, const char *path)
4426 while (path[0] == '/')
4428 printf("%c %s\n", status, path);
4432 static const struct got_error *
4433 cmd_add(int argc, char *argv[])
4435 const struct got_error *error = NULL;
4436 struct got_repository *repo = NULL;
4437 struct got_worktree *worktree = NULL;
4439 struct got_pathlist_head paths;
4440 struct got_pathlist_entry *pe;
4441 int ch, can_recurse = 0, no_ignores = 0;
4445 while ((ch = getopt(argc, argv, "IR")) != -1) {
4463 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4470 cwd = getcwd(NULL, 0);
4472 error = got_error_from_errno("getcwd");
4476 error = got_worktree_open(&worktree, cwd);
4480 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4485 error = apply_unveil(got_repo_get_path(repo), 1,
4486 got_worktree_get_root_path(worktree));
4490 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4494 if (!can_recurse && no_ignores) {
4495 error = got_error_msg(GOT_ERR_BAD_PATH,
4496 "disregarding ignores requires -R option");
4504 TAILQ_FOREACH(pe, &paths, entry) {
4505 if (asprintf(&ondisk_path, "%s/%s",
4506 got_worktree_get_root_path(worktree),
4508 error = got_error_from_errno("asprintf");
4511 if (lstat(ondisk_path, &sb) == -1) {
4512 if (errno == ENOENT) {
4516 error = got_error_from_errno2("lstat",
4522 if (S_ISDIR(sb.st_mode)) {
4523 error = got_error_msg(GOT_ERR_BAD_PATH,
4524 "adding directories requires -R option");
4530 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4531 NULL, repo, no_ignores);
4534 got_repo_close(repo);
4536 got_worktree_close(worktree);
4537 TAILQ_FOREACH(pe, &paths, entry)
4538 free((char *)pe->path);
4539 got_pathlist_free(&paths);
4547 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4552 static const struct got_error *
4553 print_remove_status(void *arg, unsigned char status,
4554 unsigned char staged_status, const char *path)
4556 while (path[0] == '/')
4558 if (status == GOT_STATUS_NONEXISTENT)
4560 if (status == staged_status && (status == GOT_STATUS_DELETE))
4561 status = GOT_STATUS_NO_CHANGE;
4562 printf("%c%c %s\n", status, staged_status, path);
4566 static const struct got_error *
4567 cmd_remove(int argc, char *argv[])
4569 const struct got_error *error = NULL;
4570 struct got_worktree *worktree = NULL;
4571 struct got_repository *repo = NULL;
4573 struct got_pathlist_head paths;
4574 struct got_pathlist_entry *pe;
4575 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4579 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4582 delete_local_mods = 1;
4600 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4607 cwd = getcwd(NULL, 0);
4609 error = got_error_from_errno("getcwd");
4612 error = got_worktree_open(&worktree, cwd);
4616 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4621 error = apply_unveil(got_repo_get_path(repo), 1,
4622 got_worktree_get_root_path(worktree));
4626 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4633 TAILQ_FOREACH(pe, &paths, entry) {
4634 if (asprintf(&ondisk_path, "%s/%s",
4635 got_worktree_get_root_path(worktree),
4637 error = got_error_from_errno("asprintf");
4640 if (lstat(ondisk_path, &sb) == -1) {
4641 if (errno == ENOENT) {
4645 error = got_error_from_errno2("lstat",
4651 if (S_ISDIR(sb.st_mode)) {
4652 error = got_error_msg(GOT_ERR_BAD_PATH,
4653 "removing directories requires -R option");
4659 error = got_worktree_schedule_delete(worktree, &paths,
4660 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4663 got_repo_close(repo);
4665 got_worktree_close(worktree);
4666 TAILQ_FOREACH(pe, &paths, entry)
4667 free((char *)pe->path);
4668 got_pathlist_free(&paths);
4676 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4677 "path ...\n", getprogname());
4681 static const struct got_error *
4682 revert_progress(void *arg, unsigned char status, const char *path)
4684 if (status == GOT_STATUS_UNVERSIONED)
4687 while (path[0] == '/')
4689 printf("%c %s\n", status, path);
4693 struct choose_patch_arg {
4694 FILE *patch_script_file;
4698 static const struct got_error *
4699 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4700 int nchanges, const char *action)
4703 size_t linesize = 0;
4707 case GOT_STATUS_ADD:
4708 printf("A %s\n%s this addition? [y/n] ", path, action);
4710 case GOT_STATUS_DELETE:
4711 printf("D %s\n%s this deletion? [y/n] ", path, action);
4713 case GOT_STATUS_MODIFY:
4714 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4715 return got_error_from_errno("fseek");
4716 printf(GOT_COMMIT_SEP_STR);
4717 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4719 if (ferror(patch_file))
4720 return got_error_from_errno("getline");
4721 printf(GOT_COMMIT_SEP_STR);
4722 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4723 path, n, nchanges, action);
4726 return got_error_path(path, GOT_ERR_FILE_STATUS);
4732 static const struct got_error *
4733 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4734 FILE *patch_file, int n, int nchanges)
4736 const struct got_error *err = NULL;
4738 size_t linesize = 0;
4741 struct choose_patch_arg *a = arg;
4743 *choice = GOT_PATCH_CHOICE_NONE;
4745 if (a->patch_script_file) {
4747 err = show_change(status, path, patch_file, n, nchanges,
4751 linelen = getline(&line, &linesize, a->patch_script_file);
4752 if (linelen == -1) {
4753 if (ferror(a->patch_script_file))
4754 return got_error_from_errno("getline");
4757 nl = strchr(line, '\n');
4760 if (strcmp(line, "y") == 0) {
4761 *choice = GOT_PATCH_CHOICE_YES;
4763 } else if (strcmp(line, "n") == 0) {
4764 *choice = GOT_PATCH_CHOICE_NO;
4766 } else if (strcmp(line, "q") == 0 &&
4767 status == GOT_STATUS_MODIFY) {
4768 *choice = GOT_PATCH_CHOICE_QUIT;
4771 printf("invalid response '%s'\n", line);
4776 while (resp != 'y' && resp != 'n' && resp != 'q') {
4777 err = show_change(status, path, patch_file, n, nchanges,
4784 if (status == GOT_STATUS_MODIFY) {
4785 if (resp != 'y' && resp != 'n' && resp != 'q') {
4786 printf("invalid response '%c'\n", resp);
4789 } else if (resp != 'y' && resp != 'n') {
4790 printf("invalid response '%c'\n", resp);
4796 *choice = GOT_PATCH_CHOICE_YES;
4797 else if (resp == 'n')
4798 *choice = GOT_PATCH_CHOICE_NO;
4799 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4800 *choice = GOT_PATCH_CHOICE_QUIT;
4806 static const struct got_error *
4807 cmd_revert(int argc, char *argv[])
4809 const struct got_error *error = NULL;
4810 struct got_worktree *worktree = NULL;
4811 struct got_repository *repo = NULL;
4812 char *cwd = NULL, *path = NULL;
4813 struct got_pathlist_head paths;
4814 struct got_pathlist_entry *pe;
4815 int ch, can_recurse = 0, pflag = 0;
4816 FILE *patch_script_file = NULL;
4817 const char *patch_script_path = NULL;
4818 struct choose_patch_arg cpa;
4822 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4828 patch_script_path = optarg;
4843 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4844 "unveil", NULL) == -1)
4849 if (patch_script_path && !pflag)
4850 errx(1, "-F option can only be used together with -p option");
4852 cwd = getcwd(NULL, 0);
4854 error = got_error_from_errno("getcwd");
4857 error = got_worktree_open(&worktree, cwd);
4861 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4866 if (patch_script_path) {
4867 patch_script_file = fopen(patch_script_path, "r");
4868 if (patch_script_file == NULL) {
4869 error = got_error_from_errno2("fopen",
4874 error = apply_unveil(got_repo_get_path(repo), 1,
4875 got_worktree_get_root_path(worktree));
4879 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4886 TAILQ_FOREACH(pe, &paths, entry) {
4887 if (asprintf(&ondisk_path, "%s/%s",
4888 got_worktree_get_root_path(worktree),
4890 error = got_error_from_errno("asprintf");
4893 if (lstat(ondisk_path, &sb) == -1) {
4894 if (errno == ENOENT) {
4898 error = got_error_from_errno2("lstat",
4904 if (S_ISDIR(sb.st_mode)) {
4905 error = got_error_msg(GOT_ERR_BAD_PATH,
4906 "reverting directories requires -R option");
4912 cpa.patch_script_file = patch_script_file;
4913 cpa.action = "revert";
4914 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4915 pflag ? choose_patch : NULL, &cpa, repo);
4917 if (patch_script_file && fclose(patch_script_file) == EOF &&
4919 error = got_error_from_errno2("fclose", patch_script_path);
4921 got_repo_close(repo);
4923 got_worktree_close(worktree);
4932 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4937 struct collect_commit_logmsg_arg {
4938 const char *cmdline_log;
4940 const char *worktree_path;
4941 const char *branch_name;
4942 const char *repo_path;
4947 static const struct got_error *
4948 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4951 char *initial_content = NULL;
4952 struct got_pathlist_entry *pe;
4953 const struct got_error *err = NULL;
4954 char *template = NULL;
4955 struct collect_commit_logmsg_arg *a = arg;
4959 /* if a message was specified on the command line, just use it */
4960 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4961 len = strlen(a->cmdline_log) + 1;
4962 *logmsg = malloc(len + 1);
4963 if (*logmsg == NULL)
4964 return got_error_from_errno("malloc");
4965 strlcpy(*logmsg, a->cmdline_log, len);
4969 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4970 return got_error_from_errno("asprintf");
4972 if (asprintf(&initial_content,
4973 "\n# changes to be committed on branch %s:\n",
4974 a->branch_name) == -1)
4975 return got_error_from_errno("asprintf");
4977 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4981 dprintf(fd, initial_content);
4983 TAILQ_FOREACH(pe, commitable_paths, entry) {
4984 struct got_commitable *ct = pe->data;
4985 dprintf(fd, "# %c %s\n",
4986 got_commitable_get_status(ct),
4987 got_commitable_get_path(ct));
4991 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4993 free(initial_content);
4996 /* Editor is done; we can now apply unveil(2) */
4998 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5007 static const struct got_error *
5008 cmd_commit(int argc, char *argv[])
5010 const struct got_error *error = NULL;
5011 struct got_worktree *worktree = NULL;
5012 struct got_repository *repo = NULL;
5013 char *cwd = NULL, *id_str = NULL;
5014 struct got_object_id *id = NULL;
5015 const char *logmsg = NULL;
5016 struct collect_commit_logmsg_arg cl_arg;
5017 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5018 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5019 struct got_pathlist_head paths;
5022 cl_arg.logmsg_path = NULL;
5024 while ((ch = getopt(argc, argv, "m:")) != -1) {
5039 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5040 "unveil", NULL) == -1)
5043 cwd = getcwd(NULL, 0);
5045 error = got_error_from_errno("getcwd");
5048 error = got_worktree_open(&worktree, cwd);
5052 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5055 if (rebase_in_progress) {
5056 error = got_error(GOT_ERR_REBASING);
5060 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5065 error = get_gitconfig_path(&gitconfig_path);
5068 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5073 error = get_author(&author, repo);
5078 * unveil(2) traverses exec(2); if an editor is used we have
5079 * to apply unveil after the log message has been written.
5081 if (logmsg == NULL || strlen(logmsg) == 0)
5082 error = get_editor(&editor);
5084 error = apply_unveil(got_repo_get_path(repo), 0,
5085 got_worktree_get_root_path(worktree));
5089 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5093 cl_arg.editor = editor;
5094 cl_arg.cmdline_log = logmsg;
5095 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5096 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5097 if (!histedit_in_progress) {
5098 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5099 error = got_error(GOT_ERR_COMMIT_BRANCH);
5102 cl_arg.branch_name += 11;
5104 cl_arg.repo_path = got_repo_get_path(repo);
5105 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5106 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5108 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5109 cl_arg.logmsg_path != NULL)
5110 preserve_logmsg = 1;
5114 error = got_object_id_str(&id_str, id);
5117 printf("Created commit %s\n", id_str);
5119 if (preserve_logmsg) {
5120 fprintf(stderr, "%s: log message preserved in %s\n",
5121 getprogname(), cl_arg.logmsg_path);
5122 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5124 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5125 free(cl_arg.logmsg_path);
5127 got_repo_close(repo);
5129 got_worktree_close(worktree);
5132 free(gitconfig_path);
5139 usage_cherrypick(void)
5141 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5145 static const struct got_error *
5146 cmd_cherrypick(int argc, char *argv[])
5148 const struct got_error *error = NULL;
5149 struct got_worktree *worktree = NULL;
5150 struct got_repository *repo = NULL;
5151 char *cwd = NULL, *commit_id_str = NULL;
5152 struct got_object_id *commit_id = NULL;
5153 struct got_commit_object *commit = NULL;
5154 struct got_object_qid *pid;
5155 struct got_reference *head_ref = NULL;
5156 int ch, did_something = 0;
5158 while ((ch = getopt(argc, argv, "")) != -1) {
5170 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5171 "unveil", NULL) == -1)
5177 cwd = getcwd(NULL, 0);
5179 error = got_error_from_errno("getcwd");
5182 error = got_worktree_open(&worktree, cwd);
5186 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5191 error = apply_unveil(got_repo_get_path(repo), 0,
5192 got_worktree_get_root_path(worktree));
5196 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5197 GOT_OBJ_TYPE_COMMIT, repo);
5198 if (error != NULL) {
5199 struct got_reference *ref;
5200 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5202 error = got_ref_open(&ref, repo, argv[0], 0);
5205 error = got_ref_resolve(&commit_id, repo, ref);
5210 error = got_object_id_str(&commit_id_str, commit_id);
5214 error = got_ref_open(&head_ref, repo,
5215 got_worktree_get_head_ref_name(worktree), 0);
5219 error = check_same_branch(commit_id, head_ref, NULL, repo);
5221 if (error->code != GOT_ERR_ANCESTRY)
5225 error = got_error(GOT_ERR_SAME_BRANCH);
5229 error = got_object_open_as_commit(&commit, repo, commit_id);
5232 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5233 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5234 commit_id, repo, update_progress, &did_something, check_cancelled,
5240 printf("Merged commit %s\n", commit_id_str);
5243 got_object_commit_close(commit);
5244 free(commit_id_str);
5246 got_ref_close(head_ref);
5248 got_worktree_close(worktree);
5250 got_repo_close(repo);
5257 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5261 static const struct got_error *
5262 cmd_backout(int argc, char *argv[])
5264 const struct got_error *error = NULL;
5265 struct got_worktree *worktree = NULL;
5266 struct got_repository *repo = NULL;
5267 char *cwd = NULL, *commit_id_str = NULL;
5268 struct got_object_id *commit_id = NULL;
5269 struct got_commit_object *commit = NULL;
5270 struct got_object_qid *pid;
5271 struct got_reference *head_ref = NULL;
5272 int ch, did_something = 0;
5274 while ((ch = getopt(argc, argv, "")) != -1) {
5286 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5287 "unveil", NULL) == -1)
5293 cwd = getcwd(NULL, 0);
5295 error = got_error_from_errno("getcwd");
5298 error = got_worktree_open(&worktree, cwd);
5302 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5307 error = apply_unveil(got_repo_get_path(repo), 0,
5308 got_worktree_get_root_path(worktree));
5312 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5313 GOT_OBJ_TYPE_COMMIT, repo);
5314 if (error != NULL) {
5315 struct got_reference *ref;
5316 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5318 error = got_ref_open(&ref, repo, argv[0], 0);
5321 error = got_ref_resolve(&commit_id, repo, ref);
5326 error = got_object_id_str(&commit_id_str, commit_id);
5330 error = got_ref_open(&head_ref, repo,
5331 got_worktree_get_head_ref_name(worktree), 0);
5335 error = check_same_branch(commit_id, head_ref, NULL, repo);
5339 error = got_object_open_as_commit(&commit, repo, commit_id);
5342 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5344 error = got_error(GOT_ERR_ROOT_COMMIT);
5348 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5349 update_progress, &did_something, check_cancelled, NULL);
5354 printf("Backed out commit %s\n", commit_id_str);
5357 got_object_commit_close(commit);
5358 free(commit_id_str);
5360 got_ref_close(head_ref);
5362 got_worktree_close(worktree);
5364 got_repo_close(repo);
5371 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5377 trim_logmsg(char *logmsg, int limit)
5382 len = strlen(logmsg);
5386 nl = strchr(logmsg, '\n');
5391 static const struct got_error *
5392 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5394 const struct got_error *err;
5395 char *logmsg0 = NULL;
5398 err = got_object_commit_get_logmsg(&logmsg0, commit);
5403 while (isspace((unsigned char)s[0]))
5406 *logmsg = strdup(s);
5407 if (*logmsg == NULL) {
5408 err = got_error_from_errno("strdup");
5412 trim_logmsg(*logmsg, limit);
5418 static const struct got_error *
5419 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5421 const struct got_error *err;
5422 struct got_commit_object *commit = NULL;
5423 char *id_str = NULL, *logmsg = NULL;
5425 err = got_object_open_as_commit(&commit, repo, id);
5429 err = got_object_id_str(&id_str, id);
5435 err = get_short_logmsg(&logmsg, 42, commit);
5439 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5442 got_object_commit_close(commit);
5447 static const struct got_error *
5448 show_rebase_progress(struct got_commit_object *commit,
5449 struct got_object_id *old_id, struct got_object_id *new_id)
5451 const struct got_error *err;
5452 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5454 err = got_object_id_str(&old_id_str, old_id);
5459 err = got_object_id_str(&new_id_str, new_id);
5464 old_id_str[12] = '\0';
5466 new_id_str[12] = '\0';
5468 err = get_short_logmsg(&logmsg, 42, commit);
5472 printf("%s -> %s: %s\n", old_id_str,
5473 new_id_str ? new_id_str : "no-op change", logmsg);
5481 static const struct got_error *
5482 rebase_progress(void *arg, unsigned char status, const char *path)
5484 unsigned char *rebase_status = arg;
5486 while (path[0] == '/')
5488 printf("%c %s\n", status, path);
5490 if (*rebase_status == GOT_STATUS_CONFLICT)
5492 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5493 *rebase_status = status;
5497 static const struct got_error *
5498 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5499 struct got_reference *branch, struct got_reference *new_base_branch,
5500 struct got_reference *tmp_branch, struct got_repository *repo)
5502 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5503 return got_worktree_rebase_complete(worktree, fileindex,
5504 new_base_branch, tmp_branch, branch, repo);
5507 static const struct got_error *
5508 rebase_commit(struct got_pathlist_head *merged_paths,
5509 struct got_worktree *worktree, struct got_fileindex *fileindex,
5510 struct got_reference *tmp_branch,
5511 struct got_object_id *commit_id, struct got_repository *repo)
5513 const struct got_error *error;
5514 struct got_commit_object *commit;
5515 struct got_object_id *new_commit_id;
5517 error = got_object_open_as_commit(&commit, repo, commit_id);
5521 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5522 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5524 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5526 error = show_rebase_progress(commit, commit_id, NULL);
5528 error = show_rebase_progress(commit, commit_id, new_commit_id);
5529 free(new_commit_id);
5532 got_object_commit_close(commit);
5536 struct check_path_prefix_arg {
5537 const char *path_prefix;
5542 static const struct got_error *
5543 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5544 struct got_blob_object *blob2, struct got_object_id *id1,
5545 struct got_object_id *id2, const char *path1, const char *path2,
5546 mode_t mode1, mode_t mode2, struct got_repository *repo)
5548 struct check_path_prefix_arg *a = arg;
5550 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5551 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5552 return got_error(a->errcode);
5557 static const struct got_error *
5558 check_path_prefix(struct got_object_id *parent_id,
5559 struct got_object_id *commit_id, const char *path_prefix,
5560 int errcode, struct got_repository *repo)
5562 const struct got_error *err;
5563 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5564 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5565 struct check_path_prefix_arg cpp_arg;
5567 if (got_path_is_root_dir(path_prefix))
5570 err = got_object_open_as_commit(&commit, repo, commit_id);
5574 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5578 err = got_object_open_as_tree(&tree1, repo,
5579 got_object_commit_get_tree_id(parent_commit));
5583 err = got_object_open_as_tree(&tree2, repo,
5584 got_object_commit_get_tree_id(commit));
5588 cpp_arg.path_prefix = path_prefix;
5589 while (cpp_arg.path_prefix[0] == '/')
5590 cpp_arg.path_prefix++;
5591 cpp_arg.len = strlen(cpp_arg.path_prefix);
5592 cpp_arg.errcode = errcode;
5593 err = got_diff_tree(tree1, tree2, "", "", repo,
5594 check_path_prefix_in_diff, &cpp_arg, 0);
5597 got_object_tree_close(tree1);
5599 got_object_tree_close(tree2);
5601 got_object_commit_close(commit);
5603 got_object_commit_close(parent_commit);
5607 static const struct got_error *
5608 collect_commits(struct got_object_id_queue *commits,
5609 struct got_object_id *initial_commit_id,
5610 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5611 const char *path_prefix, int path_prefix_errcode,
5612 struct got_repository *repo)
5614 const struct got_error *err = NULL;
5615 struct got_commit_graph *graph = NULL;
5616 struct got_object_id *parent_id = NULL;
5617 struct got_object_qid *qid;
5618 struct got_object_id *commit_id = initial_commit_id;
5620 err = got_commit_graph_open(&graph, "/", 1);
5624 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5625 check_cancelled, NULL);
5628 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5629 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5630 check_cancelled, NULL);
5632 if (err->code == GOT_ERR_ITER_COMPLETED) {
5633 err = got_error_msg(GOT_ERR_ANCESTRY,
5634 "ran out of commits to rebase before "
5635 "youngest common ancestor commit has "
5640 err = check_path_prefix(parent_id, commit_id,
5641 path_prefix, path_prefix_errcode, repo);
5645 err = got_object_qid_alloc(&qid, commit_id);
5648 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5649 commit_id = parent_id;
5653 got_commit_graph_close(graph);
5657 static const struct got_error *
5658 cmd_rebase(int argc, char *argv[])
5660 const struct got_error *error = NULL;
5661 struct got_worktree *worktree = NULL;
5662 struct got_repository *repo = NULL;
5663 struct got_fileindex *fileindex = NULL;
5665 struct got_reference *branch = NULL;
5666 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5667 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5668 struct got_object_id *resume_commit_id = NULL;
5669 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5670 struct got_commit_object *commit = NULL;
5671 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5672 int histedit_in_progress = 0;
5673 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5674 struct got_object_id_queue commits;
5675 struct got_pathlist_head merged_paths;
5676 const struct got_object_id_queue *parent_ids;
5677 struct got_object_qid *qid, *pid;
5679 SIMPLEQ_INIT(&commits);
5680 TAILQ_INIT(&merged_paths);
5682 while ((ch = getopt(argc, argv, "ac")) != -1) {
5688 continue_rebase = 1;
5700 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5701 "unveil", NULL) == -1)
5704 if (abort_rebase && continue_rebase)
5706 else if (abort_rebase || continue_rebase) {
5709 } else if (argc != 1)
5712 cwd = getcwd(NULL, 0);
5714 error = got_error_from_errno("getcwd");
5717 error = got_worktree_open(&worktree, cwd);
5721 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5726 error = apply_unveil(got_repo_get_path(repo), 0,
5727 got_worktree_get_root_path(worktree));
5731 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5735 if (histedit_in_progress) {
5736 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5740 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5746 if (!rebase_in_progress) {
5747 error = got_error(GOT_ERR_NOT_REBASING);
5750 error = got_worktree_rebase_continue(&resume_commit_id,
5751 &new_base_branch, &tmp_branch, &branch, &fileindex,
5755 printf("Switching work tree to %s\n",
5756 got_ref_get_symref_target(new_base_branch));
5757 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5758 new_base_branch, update_progress, &did_something);
5761 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5762 goto done; /* nothing else to do */
5765 if (continue_rebase) {
5766 if (!rebase_in_progress) {
5767 error = got_error(GOT_ERR_NOT_REBASING);
5770 error = got_worktree_rebase_continue(&resume_commit_id,
5771 &new_base_branch, &tmp_branch, &branch, &fileindex,
5776 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5777 resume_commit_id, repo);
5781 yca_id = got_object_id_dup(resume_commit_id);
5782 if (yca_id == NULL) {
5783 error = got_error_from_errno("got_object_id_dup");
5787 error = got_ref_open(&branch, repo, argv[0], 0);
5792 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5796 if (!continue_rebase) {
5797 struct got_object_id *base_commit_id;
5799 base_commit_id = got_worktree_get_base_commit_id(worktree);
5800 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5801 base_commit_id, branch_head_commit_id, repo,
5802 check_cancelled, NULL);
5805 if (yca_id == NULL) {
5806 error = got_error_msg(GOT_ERR_ANCESTRY,
5807 "specified branch shares no common ancestry "
5808 "with work tree's branch");
5812 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5814 if (error->code != GOT_ERR_ANCESTRY)
5818 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5819 "specified branch resolves to a commit which "
5820 "is already contained in work tree's branch");
5823 error = got_worktree_rebase_prepare(&new_base_branch,
5824 &tmp_branch, &fileindex, worktree, branch, repo);
5829 commit_id = branch_head_commit_id;
5830 error = got_object_open_as_commit(&commit, repo, commit_id);
5834 parent_ids = got_object_commit_get_parent_ids(commit);
5835 pid = SIMPLEQ_FIRST(parent_ids);
5837 if (!continue_rebase) {
5839 error = got_worktree_rebase_abort(worktree, fileindex,
5840 repo, new_base_branch, update_progress,
5844 printf("Rebase of %s aborted\n",
5845 got_ref_get_name(branch));
5847 error = got_error(GOT_ERR_EMPTY_REBASE);
5850 error = collect_commits(&commits, commit_id, pid->id,
5851 yca_id, got_worktree_get_path_prefix(worktree),
5852 GOT_ERR_REBASE_PATH, repo);
5853 got_object_commit_close(commit);
5858 if (SIMPLEQ_EMPTY(&commits)) {
5859 if (continue_rebase) {
5860 error = rebase_complete(worktree, fileindex,
5861 branch, new_base_branch, tmp_branch, repo);
5864 /* Fast-forward the reference of the branch. */
5865 struct got_object_id *new_head_commit_id;
5867 error = got_ref_resolve(&new_head_commit_id, repo,
5871 error = got_object_id_str(&id_str, new_head_commit_id);
5872 printf("Forwarding %s to commit %s\n",
5873 got_ref_get_name(branch), id_str);
5875 error = got_ref_change_ref(branch,
5876 new_head_commit_id);
5883 SIMPLEQ_FOREACH(qid, &commits, entry) {
5884 commit_id = qid->id;
5885 parent_id = pid ? pid->id : yca_id;
5888 error = got_worktree_rebase_merge_files(&merged_paths,
5889 worktree, fileindex, parent_id, commit_id, repo,
5890 rebase_progress, &rebase_status, check_cancelled, NULL);
5894 if (rebase_status == GOT_STATUS_CONFLICT) {
5895 error = show_rebase_merge_conflict(qid->id, repo);
5898 got_worktree_rebase_pathlist_free(&merged_paths);
5902 error = rebase_commit(&merged_paths, worktree, fileindex,
5903 tmp_branch, commit_id, repo);
5904 got_worktree_rebase_pathlist_free(&merged_paths);
5909 if (rebase_status == GOT_STATUS_CONFLICT) {
5910 error = got_worktree_rebase_postpone(worktree, fileindex);
5913 error = got_error_msg(GOT_ERR_CONFLICTS,
5914 "conflicts must be resolved before rebasing can continue");
5916 error = rebase_complete(worktree, fileindex, branch,
5917 new_base_branch, tmp_branch, repo);
5919 got_object_id_queue_free(&commits);
5920 free(branch_head_commit_id);
5921 free(resume_commit_id);
5924 got_object_commit_close(commit);
5926 got_ref_close(branch);
5927 if (new_base_branch)
5928 got_ref_close(new_base_branch);
5930 got_ref_close(tmp_branch);
5932 got_worktree_close(worktree);
5934 got_repo_close(repo);
5939 usage_histedit(void)
5941 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5946 #define GOT_HISTEDIT_PICK 'p'
5947 #define GOT_HISTEDIT_EDIT 'e'
5948 #define GOT_HISTEDIT_FOLD 'f'
5949 #define GOT_HISTEDIT_DROP 'd'
5950 #define GOT_HISTEDIT_MESG 'm'
5952 static struct got_histedit_cmd {
5956 } got_histedit_cmds[] = {
5957 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5958 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5959 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5961 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5962 { GOT_HISTEDIT_MESG, "mesg",
5963 "single-line log message for commit above (open editor if empty)" },
5966 struct got_histedit_list_entry {
5967 TAILQ_ENTRY(got_histedit_list_entry) entry;
5968 struct got_object_id *commit_id;
5969 const struct got_histedit_cmd *cmd;
5972 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5974 static const struct got_error *
5975 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5976 FILE *f, struct got_repository *repo)
5978 const struct got_error *err = NULL;
5979 char *logmsg = NULL, *id_str = NULL;
5980 struct got_commit_object *commit = NULL;
5983 err = got_object_open_as_commit(&commit, repo, commit_id);
5987 err = get_short_logmsg(&logmsg, 34, commit);
5991 err = got_object_id_str(&id_str, commit_id);
5995 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5997 err = got_ferror(f, GOT_ERR_IO);
6000 got_object_commit_close(commit);
6006 static const struct got_error *
6007 histedit_write_commit_list(struct got_object_id_queue *commits,
6008 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6010 const struct got_error *err = NULL;
6011 struct got_object_qid *qid;
6013 if (SIMPLEQ_EMPTY(commits))
6014 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6016 SIMPLEQ_FOREACH(qid, commits, entry) {
6017 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6021 if (edit_logmsg_only) {
6022 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6024 err = got_ferror(f, GOT_ERR_IO);
6033 static const struct got_error *
6034 write_cmd_list(FILE *f, const char *branch_name,
6035 struct got_object_id_queue *commits)
6037 const struct got_error *err = NULL;
6040 struct got_object_qid *qid;
6042 qid = SIMPLEQ_FIRST(commits);
6043 err = got_object_id_str(&id_str, qid->id);
6048 "# Editing the history of branch '%s' starting at\n"
6050 "# Commits will be processed in order from top to "
6051 "bottom of this file.\n", branch_name, id_str);
6053 err = got_ferror(f, GOT_ERR_IO);
6057 n = fprintf(f, "# Available histedit commands:\n");
6059 err = got_ferror(f, GOT_ERR_IO);
6063 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6064 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6065 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6068 err = got_ferror(f, GOT_ERR_IO);
6077 static const struct got_error *
6078 histedit_syntax_error(int lineno)
6080 static char msg[42];
6083 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6085 if (ret == -1 || ret >= sizeof(msg))
6086 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6088 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6091 static const struct got_error *
6092 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6093 char *logmsg, struct got_repository *repo)
6095 const struct got_error *err;
6096 struct got_commit_object *folded_commit = NULL;
6097 char *id_str, *folded_logmsg = NULL;
6099 err = got_object_id_str(&id_str, hle->commit_id);
6103 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6107 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6110 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6111 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6112 folded_logmsg) == -1) {
6113 err = got_error_from_errno("asprintf");
6117 got_object_commit_close(folded_commit);
6119 free(folded_logmsg);
6123 static struct got_histedit_list_entry *
6124 get_folded_commits(struct got_histedit_list_entry *hle)
6126 struct got_histedit_list_entry *prev, *folded = NULL;
6128 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6129 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6130 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6131 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6133 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6139 static const struct got_error *
6140 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6141 struct got_repository *repo)
6143 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6144 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6145 const struct got_error *err = NULL;
6146 struct got_commit_object *commit = NULL;
6148 struct got_histedit_list_entry *folded = NULL;
6150 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6154 folded = get_folded_commits(hle);
6156 while (folded != hle) {
6157 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6158 folded = TAILQ_NEXT(folded, entry);
6161 err = append_folded_commit_msg(&new_msg, folded,
6167 folded = TAILQ_NEXT(folded, entry);
6171 err = got_object_id_str(&id_str, hle->commit_id);
6174 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6177 if (asprintf(&new_msg,
6178 "%s\n# original log message of commit %s: %s",
6179 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6180 err = got_error_from_errno("asprintf");
6186 err = got_object_id_str(&id_str, hle->commit_id);
6190 err = got_opentemp_named_fd(&logmsg_path, &fd,
6191 GOT_TMPDIR_STR "/got-logmsg");
6195 dprintf(fd, logmsg);
6198 err = get_editor(&editor);
6202 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6204 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6206 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6209 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6210 err = got_error_from_errno2("unlink", logmsg_path);
6216 got_object_commit_close(commit);
6220 static const struct got_error *
6221 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6222 FILE *f, struct got_repository *repo)
6224 const struct got_error *err = NULL;
6225 char *line = NULL, *p, *end;
6229 const struct got_histedit_cmd *cmd;
6230 struct got_object_id *commit_id = NULL;
6231 struct got_histedit_list_entry *hle = NULL;
6234 len = getline(&line, &size, f);
6236 const struct got_error *getline_err;
6239 getline_err = got_error_from_errno("getline");
6240 err = got_ferror(f, getline_err->code);
6245 while (isspace((unsigned char)p[0]))
6247 if (p[0] == '#' || p[0] == '\0') {
6253 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6254 cmd = &got_histedit_cmds[i];
6255 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6256 isspace((unsigned char)p[strlen(cmd->name)])) {
6257 p += strlen(cmd->name);
6260 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6265 if (i == nitems(got_histedit_cmds)) {
6266 err = histedit_syntax_error(lineno);
6269 while (isspace((unsigned char)p[0]))
6271 if (cmd->code == GOT_HISTEDIT_MESG) {
6272 if (hle == NULL || hle->logmsg != NULL) {
6273 err = got_error(GOT_ERR_HISTEDIT_CMD);
6277 err = histedit_edit_logmsg(hle, repo);
6281 hle->logmsg = strdup(p);
6282 if (hle->logmsg == NULL) {
6283 err = got_error_from_errno("strdup");
6292 while (end[0] && !isspace((unsigned char)end[0]))
6296 err = got_object_resolve_id_str(&commit_id, repo, p);
6298 /* override error code */
6299 err = histedit_syntax_error(lineno);
6303 hle = malloc(sizeof(*hle));
6305 err = got_error_from_errno("malloc");
6309 hle->commit_id = commit_id;
6314 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6322 static const struct got_error *
6323 histedit_check_script(struct got_histedit_list *histedit_cmds,
6324 struct got_object_id_queue *commits, struct got_repository *repo)
6326 const struct got_error *err = NULL;
6327 struct got_object_qid *qid;
6328 struct got_histedit_list_entry *hle;
6329 static char msg[92];
6332 if (TAILQ_EMPTY(histedit_cmds))
6333 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6334 "histedit script contains no commands");
6335 if (SIMPLEQ_EMPTY(commits))
6336 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6338 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6339 struct got_histedit_list_entry *hle2;
6340 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6343 if (got_object_id_cmp(hle->commit_id,
6344 hle2->commit_id) != 0)
6346 err = got_object_id_str(&id_str, hle->commit_id);
6349 snprintf(msg, sizeof(msg), "commit %s is listed "
6350 "more than once in histedit script", id_str);
6352 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6356 SIMPLEQ_FOREACH(qid, commits, entry) {
6357 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6358 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6362 err = got_object_id_str(&id_str, qid->id);
6365 snprintf(msg, sizeof(msg),
6366 "commit %s missing from histedit script", id_str);
6368 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6372 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6373 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6374 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6375 "last commit in histedit script cannot be folded");
6380 static const struct got_error *
6381 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6382 const char *path, struct got_object_id_queue *commits,
6383 struct got_repository *repo)
6385 const struct got_error *err = NULL;
6389 err = get_editor(&editor);
6393 if (spawn_editor(editor, path) == -1) {
6394 err = got_error_from_errno("failed spawning editor");
6398 f = fopen(path, "r");
6400 err = got_error_from_errno("fopen");
6403 err = histedit_parse_list(histedit_cmds, f, repo);
6407 err = histedit_check_script(histedit_cmds, commits, repo);
6409 if (f && fclose(f) != 0 && err == NULL)
6410 err = got_error_from_errno("fclose");
6415 static const struct got_error *
6416 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6417 struct got_object_id_queue *, const char *, const char *,
6418 struct got_repository *);
6420 static const struct got_error *
6421 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6422 struct got_object_id_queue *commits, const char *branch_name,
6423 int edit_logmsg_only, struct got_repository *repo)
6425 const struct got_error *err;
6429 err = got_opentemp_named(&path, &f, "got-histedit");
6433 err = write_cmd_list(f, branch_name, commits);
6437 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6441 if (edit_logmsg_only) {
6443 err = histedit_parse_list(histedit_cmds, f, repo);
6445 if (fclose(f) != 0) {
6446 err = got_error_from_errno("fclose");
6450 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6452 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6453 err->code != GOT_ERR_HISTEDIT_CMD)
6455 err = histedit_edit_list_retry(histedit_cmds, err,
6456 commits, path, branch_name, repo);
6460 if (f && fclose(f) != 0 && err == NULL)
6461 err = got_error_from_errno("fclose");
6462 if (path && unlink(path) != 0 && err == NULL)
6463 err = got_error_from_errno2("unlink", path);
6468 static const struct got_error *
6469 histedit_save_list(struct got_histedit_list *histedit_cmds,
6470 struct got_worktree *worktree, struct got_repository *repo)
6472 const struct got_error *err = NULL;
6475 struct got_histedit_list_entry *hle;
6476 struct got_commit_object *commit = NULL;
6478 err = got_worktree_get_histedit_script_path(&path, worktree);
6482 f = fopen(path, "w");
6484 err = got_error_from_errno2("fopen", path);
6487 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6488 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6494 int n = fprintf(f, "%c %s\n",
6495 GOT_HISTEDIT_MESG, hle->logmsg);
6497 err = got_ferror(f, GOT_ERR_IO);
6503 if (f && fclose(f) != 0 && err == NULL)
6504 err = got_error_from_errno("fclose");
6507 got_object_commit_close(commit);
6512 histedit_free_list(struct got_histedit_list *histedit_cmds)
6514 struct got_histedit_list_entry *hle;
6516 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6517 TAILQ_REMOVE(histedit_cmds, hle, entry);
6522 static const struct got_error *
6523 histedit_load_list(struct got_histedit_list *histedit_cmds,
6524 const char *path, struct got_repository *repo)
6526 const struct got_error *err = NULL;
6529 f = fopen(path, "r");
6531 err = got_error_from_errno2("fopen", path);
6535 err = histedit_parse_list(histedit_cmds, f, repo);
6537 if (f && fclose(f) != 0 && err == NULL)
6538 err = got_error_from_errno("fclose");
6542 static const struct got_error *
6543 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6544 const struct got_error *edit_err, struct got_object_id_queue *commits,
6545 const char *path, const char *branch_name, struct got_repository *repo)
6547 const struct got_error *err = NULL, *prev_err = edit_err;
6550 while (resp != 'c' && resp != 'r' && resp != 'a') {
6551 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6552 "or (a)bort: ", getprogname(), prev_err->msg);
6557 histedit_free_list(histedit_cmds);
6558 err = histedit_run_editor(histedit_cmds, path, commits,
6561 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6562 err->code != GOT_ERR_HISTEDIT_CMD)
6569 } else if (resp == 'r') {
6570 histedit_free_list(histedit_cmds);
6571 err = histedit_edit_script(histedit_cmds,
6572 commits, branch_name, 0, repo);
6574 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6575 err->code != GOT_ERR_HISTEDIT_CMD)
6582 } else if (resp == 'a') {
6583 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6586 printf("invalid response '%c'\n", resp);
6592 static const struct got_error *
6593 histedit_complete(struct got_worktree *worktree,
6594 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6595 struct got_reference *branch, struct got_repository *repo)
6597 printf("Switching work tree to %s\n",
6598 got_ref_get_symref_target(branch));
6599 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6603 static const struct got_error *
6604 show_histedit_progress(struct got_commit_object *commit,
6605 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6607 const struct got_error *err;
6608 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6610 err = got_object_id_str(&old_id_str, hle->commit_id);
6615 err = got_object_id_str(&new_id_str, new_id);
6620 old_id_str[12] = '\0';
6622 new_id_str[12] = '\0';
6625 logmsg = strdup(hle->logmsg);
6626 if (logmsg == NULL) {
6627 err = got_error_from_errno("strdup");
6630 trim_logmsg(logmsg, 42);
6632 err = get_short_logmsg(&logmsg, 42, commit);
6637 switch (hle->cmd->code) {
6638 case GOT_HISTEDIT_PICK:
6639 case GOT_HISTEDIT_EDIT:
6640 printf("%s -> %s: %s\n", old_id_str,
6641 new_id_str ? new_id_str : "no-op change", logmsg);
6643 case GOT_HISTEDIT_DROP:
6644 case GOT_HISTEDIT_FOLD:
6645 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6657 static const struct got_error *
6658 histedit_commit(struct got_pathlist_head *merged_paths,
6659 struct got_worktree *worktree, struct got_fileindex *fileindex,
6660 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6661 struct got_repository *repo)
6663 const struct got_error *err;
6664 struct got_commit_object *commit;
6665 struct got_object_id *new_commit_id;
6667 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6668 && hle->logmsg == NULL) {
6669 err = histedit_edit_logmsg(hle, repo);
6674 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6678 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6679 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6682 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6684 err = show_histedit_progress(commit, hle, NULL);
6686 err = show_histedit_progress(commit, hle, new_commit_id);
6687 free(new_commit_id);
6690 got_object_commit_close(commit);
6694 static const struct got_error *
6695 histedit_skip_commit(struct got_histedit_list_entry *hle,
6696 struct got_worktree *worktree, struct got_repository *repo)
6698 const struct got_error *error;
6699 struct got_commit_object *commit;
6701 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6706 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6710 error = show_histedit_progress(commit, hle, NULL);
6711 got_object_commit_close(commit);
6715 static const struct got_error *
6716 check_local_changes(void *arg, unsigned char status,
6717 unsigned char staged_status, const char *path,
6718 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6719 struct got_object_id *commit_id, int dirfd, const char *de_name)
6721 int *have_local_changes = arg;
6724 case GOT_STATUS_ADD:
6725 case GOT_STATUS_DELETE:
6726 case GOT_STATUS_MODIFY:
6727 case GOT_STATUS_CONFLICT:
6728 *have_local_changes = 1;
6729 return got_error(GOT_ERR_CANCELLED);
6734 switch (staged_status) {
6735 case GOT_STATUS_ADD:
6736 case GOT_STATUS_DELETE:
6737 case GOT_STATUS_MODIFY:
6738 *have_local_changes = 1;
6739 return got_error(GOT_ERR_CANCELLED);
6747 static const struct got_error *
6748 cmd_histedit(int argc, char *argv[])
6750 const struct got_error *error = NULL;
6751 struct got_worktree *worktree = NULL;
6752 struct got_fileindex *fileindex = NULL;
6753 struct got_repository *repo = NULL;
6755 struct got_reference *branch = NULL;
6756 struct got_reference *tmp_branch = NULL;
6757 struct got_object_id *resume_commit_id = NULL;
6758 struct got_object_id *base_commit_id = NULL;
6759 struct got_object_id *head_commit_id = NULL;
6760 struct got_commit_object *commit = NULL;
6761 int ch, rebase_in_progress = 0, did_something;
6762 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6763 int edit_logmsg_only = 0;
6764 const char *edit_script_path = NULL;
6765 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6766 struct got_object_id_queue commits;
6767 struct got_pathlist_head merged_paths;
6768 const struct got_object_id_queue *parent_ids;
6769 struct got_object_qid *pid;
6770 struct got_histedit_list histedit_cmds;
6771 struct got_histedit_list_entry *hle;
6773 SIMPLEQ_INIT(&commits);
6774 TAILQ_INIT(&histedit_cmds);
6775 TAILQ_INIT(&merged_paths);
6777 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6786 edit_script_path = optarg;
6789 edit_logmsg_only = 1;
6801 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6802 "unveil", NULL) == -1)
6805 if (abort_edit && continue_edit)
6806 errx(1, "histedit's -a and -c options are mutually exclusive");
6807 if (edit_script_path && edit_logmsg_only)
6808 errx(1, "histedit's -F and -m options are mutually exclusive");
6809 if (abort_edit && edit_logmsg_only)
6810 errx(1, "histedit's -a and -m options are mutually exclusive");
6811 if (continue_edit && edit_logmsg_only)
6812 errx(1, "histedit's -c and -m options are mutually exclusive");
6817 * This command cannot apply unveil(2) in all cases because the
6818 * user may choose to run an editor to edit the histedit script
6819 * and to edit individual commit log messages.
6820 * unveil(2) traverses exec(2); if an editor is used we have to
6821 * apply unveil after edit script and log messages have been written.
6822 * XXX TODO: Make use of unveil(2) where possible.
6825 cwd = getcwd(NULL, 0);
6827 error = got_error_from_errno("getcwd");
6830 error = got_worktree_open(&worktree, cwd);
6834 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6839 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6842 if (rebase_in_progress) {
6843 error = got_error(GOT_ERR_REBASING);
6847 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6851 if (edit_in_progress && edit_logmsg_only) {
6852 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6853 "histedit operation is in progress in this "
6854 "work tree and must be continued or aborted "
6855 "before the -m option can be used");
6859 if (edit_in_progress && abort_edit) {
6860 error = got_worktree_histedit_continue(&resume_commit_id,
6861 &tmp_branch, &branch, &base_commit_id, &fileindex,
6865 printf("Switching work tree to %s\n",
6866 got_ref_get_symref_target(branch));
6867 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6868 branch, base_commit_id, update_progress, &did_something);
6871 printf("Histedit of %s aborted\n",
6872 got_ref_get_symref_target(branch));
6873 goto done; /* nothing else to do */
6874 } else if (abort_edit) {
6875 error = got_error(GOT_ERR_NOT_HISTEDIT);
6879 if (continue_edit) {
6882 if (!edit_in_progress) {
6883 error = got_error(GOT_ERR_NOT_HISTEDIT);
6887 error = got_worktree_get_histedit_script_path(&path, worktree);
6891 error = histedit_load_list(&histedit_cmds, path, repo);
6896 error = got_worktree_histedit_continue(&resume_commit_id,
6897 &tmp_branch, &branch, &base_commit_id, &fileindex,
6902 error = got_ref_resolve(&head_commit_id, repo, branch);
6906 error = got_object_open_as_commit(&commit, repo,
6910 parent_ids = got_object_commit_get_parent_ids(commit);
6911 pid = SIMPLEQ_FIRST(parent_ids);
6913 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6916 error = collect_commits(&commits, head_commit_id, pid->id,
6917 base_commit_id, got_worktree_get_path_prefix(worktree),
6918 GOT_ERR_HISTEDIT_PATH, repo);
6919 got_object_commit_close(commit);
6924 if (edit_in_progress) {
6925 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6929 error = got_ref_open(&branch, repo,
6930 got_worktree_get_head_ref_name(worktree), 0);
6934 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6935 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6936 "will not edit commit history of a branch outside "
6937 "the \"refs/heads/\" reference namespace");
6941 error = got_ref_resolve(&head_commit_id, repo, branch);
6942 got_ref_close(branch);
6947 error = got_object_open_as_commit(&commit, repo,
6951 parent_ids = got_object_commit_get_parent_ids(commit);
6952 pid = SIMPLEQ_FIRST(parent_ids);
6954 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6957 error = collect_commits(&commits, head_commit_id, pid->id,
6958 got_worktree_get_base_commit_id(worktree),
6959 got_worktree_get_path_prefix(worktree),
6960 GOT_ERR_HISTEDIT_PATH, repo);
6961 got_object_commit_close(commit);
6966 if (SIMPLEQ_EMPTY(&commits)) {
6967 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6971 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6972 &base_commit_id, &fileindex, worktree, repo);
6976 if (edit_script_path) {
6977 error = histedit_load_list(&histedit_cmds,
6978 edit_script_path, repo);
6980 got_worktree_histedit_abort(worktree, fileindex,
6981 repo, branch, base_commit_id,
6982 update_progress, &did_something);
6986 const char *branch_name;
6987 branch_name = got_ref_get_symref_target(branch);
6988 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6990 error = histedit_edit_script(&histedit_cmds, &commits,
6991 branch_name, edit_logmsg_only, repo);
6993 got_worktree_histedit_abort(worktree, fileindex,
6994 repo, branch, base_commit_id,
6995 update_progress, &did_something);
7001 error = histedit_save_list(&histedit_cmds, worktree,
7004 got_worktree_histedit_abort(worktree, fileindex,
7005 repo, branch, base_commit_id,
7006 update_progress, &did_something);
7012 error = histedit_check_script(&histedit_cmds, &commits, repo);
7016 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7017 if (resume_commit_id) {
7018 if (got_object_id_cmp(hle->commit_id,
7019 resume_commit_id) != 0)
7022 resume_commit_id = NULL;
7023 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7024 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7025 error = histedit_skip_commit(hle, worktree,
7030 struct got_pathlist_head paths;
7031 int have_changes = 0;
7034 error = got_pathlist_append(&paths, "", NULL);
7037 error = got_worktree_status(worktree, &paths,
7038 repo, check_local_changes, &have_changes,
7039 check_cancelled, NULL);
7040 got_pathlist_free(&paths);
7042 if (error->code != GOT_ERR_CANCELLED)
7044 if (sigint_received || sigpipe_received)
7048 error = histedit_commit(NULL, worktree,
7049 fileindex, tmp_branch, hle, repo);
7053 error = got_object_open_as_commit(
7054 &commit, repo, hle->commit_id);
7057 error = show_histedit_progress(commit,
7059 got_object_commit_close(commit);
7068 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7069 error = histedit_skip_commit(hle, worktree, repo);
7075 error = got_object_open_as_commit(&commit, repo,
7079 parent_ids = got_object_commit_get_parent_ids(commit);
7080 pid = SIMPLEQ_FIRST(parent_ids);
7082 error = got_worktree_histedit_merge_files(&merged_paths,
7083 worktree, fileindex, pid->id, hle->commit_id, repo,
7084 rebase_progress, &rebase_status, check_cancelled, NULL);
7087 got_object_commit_close(commit);
7090 if (rebase_status == GOT_STATUS_CONFLICT) {
7091 error = show_rebase_merge_conflict(hle->commit_id,
7095 got_worktree_rebase_pathlist_free(&merged_paths);
7099 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7101 error = got_object_id_str(&id_str, hle->commit_id);
7104 printf("Stopping histedit for amending commit %s\n",
7107 got_worktree_rebase_pathlist_free(&merged_paths);
7108 error = got_worktree_histedit_postpone(worktree,
7113 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7114 error = histedit_skip_commit(hle, worktree, repo);
7120 error = histedit_commit(&merged_paths, worktree, fileindex,
7121 tmp_branch, hle, repo);
7122 got_worktree_rebase_pathlist_free(&merged_paths);
7127 if (rebase_status == GOT_STATUS_CONFLICT) {
7128 error = got_worktree_histedit_postpone(worktree, fileindex);
7131 error = got_error_msg(GOT_ERR_CONFLICTS,
7132 "conflicts must be resolved before histedit can continue");
7134 error = histedit_complete(worktree, fileindex, tmp_branch,
7137 got_object_id_queue_free(&commits);
7138 histedit_free_list(&histedit_cmds);
7139 free(head_commit_id);
7140 free(base_commit_id);
7141 free(resume_commit_id);
7143 got_object_commit_close(commit);
7145 got_ref_close(branch);
7147 got_ref_close(tmp_branch);
7149 got_worktree_close(worktree);
7151 got_repo_close(repo);
7156 usage_integrate(void)
7158 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7162 static const struct got_error *
7163 cmd_integrate(int argc, char *argv[])
7165 const struct got_error *error = NULL;
7166 struct got_repository *repo = NULL;
7167 struct got_worktree *worktree = NULL;
7168 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7169 const char *branch_arg = NULL;
7170 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7171 struct got_fileindex *fileindex = NULL;
7172 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7173 int ch, did_something = 0;
7175 while ((ch = getopt(argc, argv, "")) != -1) {
7188 branch_arg = argv[0];
7190 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7191 "unveil", NULL) == -1)
7194 cwd = getcwd(NULL, 0);
7196 error = got_error_from_errno("getcwd");
7200 error = got_worktree_open(&worktree, cwd);
7204 error = check_rebase_or_histedit_in_progress(worktree);
7208 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7213 error = apply_unveil(got_repo_get_path(repo), 0,
7214 got_worktree_get_root_path(worktree));
7218 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7219 error = got_error_from_errno("asprintf");
7223 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7224 &base_branch_ref, worktree, refname, repo);
7228 refname = strdup(got_ref_get_name(branch_ref));
7229 if (refname == NULL) {
7230 error = got_error_from_errno("strdup");
7231 got_worktree_integrate_abort(worktree, fileindex, repo,
7232 branch_ref, base_branch_ref);
7235 base_refname = strdup(got_ref_get_name(base_branch_ref));
7236 if (base_refname == NULL) {
7237 error = got_error_from_errno("strdup");
7238 got_worktree_integrate_abort(worktree, fileindex, repo,
7239 branch_ref, base_branch_ref);
7243 error = got_ref_resolve(&commit_id, repo, branch_ref);
7247 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7251 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7252 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7253 "specified branch has already been integrated");
7254 got_worktree_integrate_abort(worktree, fileindex, repo,
7255 branch_ref, base_branch_ref);
7259 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7261 if (error->code == GOT_ERR_ANCESTRY)
7262 error = got_error(GOT_ERR_REBASE_REQUIRED);
7263 got_worktree_integrate_abort(worktree, fileindex, repo,
7264 branch_ref, base_branch_ref);
7268 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7269 branch_ref, base_branch_ref, update_progress, &did_something,
7270 check_cancelled, NULL);
7274 printf("Integrated %s into %s\n", refname, base_refname);
7277 got_repo_close(repo);
7279 got_worktree_close(worktree);
7281 free(base_commit_id);
7291 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7292 "[file-path ...]\n",
7297 static const struct got_error *
7298 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7299 const char *path, struct got_object_id *blob_id,
7300 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7301 int dirfd, const char *de_name)
7303 const struct got_error *err = NULL;
7304 char *id_str = NULL;
7306 if (staged_status != GOT_STATUS_ADD &&
7307 staged_status != GOT_STATUS_MODIFY &&
7308 staged_status != GOT_STATUS_DELETE)
7311 if (staged_status == GOT_STATUS_ADD ||
7312 staged_status == GOT_STATUS_MODIFY)
7313 err = got_object_id_str(&id_str, staged_blob_id);
7315 err = got_object_id_str(&id_str, blob_id);
7319 printf("%s %c %s\n", id_str, staged_status, path);
7324 static const struct got_error *
7325 cmd_stage(int argc, char *argv[])
7327 const struct got_error *error = NULL;
7328 struct got_repository *repo = NULL;
7329 struct got_worktree *worktree = NULL;
7331 struct got_pathlist_head paths;
7332 struct got_pathlist_entry *pe;
7333 int ch, list_stage = 0, pflag = 0;
7334 FILE *patch_script_file = NULL;
7335 const char *patch_script_path = NULL;
7336 struct choose_patch_arg cpa;
7340 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7349 patch_script_path = optarg;
7361 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7362 "unveil", NULL) == -1)
7365 if (list_stage && (pflag || patch_script_path))
7366 errx(1, "-l option cannot be used with other options");
7367 if (patch_script_path && !pflag)
7368 errx(1, "-F option can only be used together with -p option");
7370 cwd = getcwd(NULL, 0);
7372 error = got_error_from_errno("getcwd");
7376 error = got_worktree_open(&worktree, cwd);
7380 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7385 if (patch_script_path) {
7386 patch_script_file = fopen(patch_script_path, "r");
7387 if (patch_script_file == NULL) {
7388 error = got_error_from_errno2("fopen",
7393 error = apply_unveil(got_repo_get_path(repo), 0,
7394 got_worktree_get_root_path(worktree));
7398 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7403 error = got_worktree_status(worktree, &paths, repo,
7404 print_stage, NULL, check_cancelled, NULL);
7406 cpa.patch_script_file = patch_script_file;
7407 cpa.action = "stage";
7408 error = got_worktree_stage(worktree, &paths,
7409 pflag ? NULL : print_status, NULL,
7410 pflag ? choose_patch : NULL, &cpa, repo);
7413 if (patch_script_file && fclose(patch_script_file) == EOF &&
7415 error = got_error_from_errno2("fclose", patch_script_path);
7417 got_repo_close(repo);
7419 got_worktree_close(worktree);
7420 TAILQ_FOREACH(pe, &paths, entry)
7421 free((char *)pe->path);
7422 got_pathlist_free(&paths);
7430 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7431 "[file-path ...]\n",
7437 static const struct got_error *
7438 cmd_unstage(int argc, char *argv[])
7440 const struct got_error *error = NULL;
7441 struct got_repository *repo = NULL;
7442 struct got_worktree *worktree = NULL;
7444 struct got_pathlist_head paths;
7445 struct got_pathlist_entry *pe;
7446 int ch, did_something = 0, pflag = 0;
7447 FILE *patch_script_file = NULL;
7448 const char *patch_script_path = NULL;
7449 struct choose_patch_arg cpa;
7453 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7459 patch_script_path = optarg;
7471 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7472 "unveil", NULL) == -1)
7475 if (patch_script_path && !pflag)
7476 errx(1, "-F option can only be used together with -p option");
7478 cwd = getcwd(NULL, 0);
7480 error = got_error_from_errno("getcwd");
7484 error = got_worktree_open(&worktree, cwd);
7488 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7493 if (patch_script_path) {
7494 patch_script_file = fopen(patch_script_path, "r");
7495 if (patch_script_file == NULL) {
7496 error = got_error_from_errno2("fopen",
7502 error = apply_unveil(got_repo_get_path(repo), 0,
7503 got_worktree_get_root_path(worktree));
7507 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7511 cpa.patch_script_file = patch_script_file;
7512 cpa.action = "unstage";
7513 error = got_worktree_unstage(worktree, &paths, update_progress,
7514 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7516 if (patch_script_file && fclose(patch_script_file) == EOF &&
7518 error = got_error_from_errno2("fclose", patch_script_path);
7520 got_repo_close(repo);
7522 got_worktree_close(worktree);
7523 TAILQ_FOREACH(pe, &paths, entry)
7524 free((char *)pe->path);
7525 got_pathlist_free(&paths);
7533 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7534 "arg1 [arg2 ...]\n", getprogname());
7538 static const struct got_error *
7539 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7541 const struct got_error *err;
7542 struct got_blob_object *blob;
7544 err = got_object_open_as_blob(&blob, repo, id, 8192);
7548 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7549 got_object_blob_close(blob);
7553 static const struct got_error *
7554 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7556 const struct got_error *err;
7557 struct got_tree_object *tree;
7560 err = got_object_open_as_tree(&tree, repo, id);
7564 nentries = got_object_tree_get_nentries(tree);
7565 for (i = 0; i < nentries; i++) {
7566 struct got_tree_entry *te;
7568 if (sigint_received || sigpipe_received)
7570 te = got_object_tree_get_entry(tree, i);
7571 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7574 fprintf(outfile, "%s %.7o %s\n", id_str,
7575 got_tree_entry_get_mode(te),
7576 got_tree_entry_get_name(te));
7580 got_object_tree_close(tree);
7584 static const struct got_error *
7585 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7587 const struct got_error *err;
7588 struct got_commit_object *commit;
7589 const struct got_object_id_queue *parent_ids;
7590 struct got_object_qid *pid;
7591 char *id_str = NULL;
7592 const char *logmsg = NULL;
7594 err = got_object_open_as_commit(&commit, repo, id);
7598 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7602 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7603 parent_ids = got_object_commit_get_parent_ids(commit);
7604 fprintf(outfile, "numparents %d\n",
7605 got_object_commit_get_nparents(commit));
7606 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7608 err = got_object_id_str(&pid_str, pid->id);
7611 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7614 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7615 got_object_commit_get_author(commit),
7616 got_object_commit_get_author_time(commit));
7618 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7619 got_object_commit_get_author(commit),
7620 got_object_commit_get_committer_time(commit));
7622 logmsg = got_object_commit_get_logmsg_raw(commit);
7623 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7624 fprintf(outfile, "%s", logmsg);
7627 got_object_commit_close(commit);
7631 static const struct got_error *
7632 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7634 const struct got_error *err;
7635 struct got_tag_object *tag;
7636 char *id_str = NULL;
7637 const char *tagmsg = NULL;
7639 err = got_object_open_as_tag(&tag, repo, id);
7643 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7647 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7649 switch (got_object_tag_get_object_type(tag)) {
7650 case GOT_OBJ_TYPE_BLOB:
7651 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7652 GOT_OBJ_LABEL_BLOB);
7654 case GOT_OBJ_TYPE_TREE:
7655 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7656 GOT_OBJ_LABEL_TREE);
7658 case GOT_OBJ_TYPE_COMMIT:
7659 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7660 GOT_OBJ_LABEL_COMMIT);
7662 case GOT_OBJ_TYPE_TAG:
7663 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7670 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7671 got_object_tag_get_name(tag));
7673 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7674 got_object_tag_get_tagger(tag),
7675 got_object_tag_get_tagger_time(tag));
7677 tagmsg = got_object_tag_get_message(tag);
7678 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7679 fprintf(outfile, "%s", tagmsg);
7682 got_object_tag_close(tag);
7686 static const struct got_error *
7687 cmd_cat(int argc, char *argv[])
7689 const struct got_error *error;
7690 struct got_repository *repo = NULL;
7691 struct got_worktree *worktree = NULL;
7692 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7693 const char *commit_id_str = NULL;
7694 struct got_object_id *id = NULL, *commit_id = NULL;
7695 int ch, obj_type, i, force_path = 0;
7698 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7703 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7706 commit_id_str = optarg;
7709 repo_path = realpath(optarg, NULL);
7710 if (repo_path == NULL)
7711 return got_error_from_errno2("realpath",
7713 got_path_strip_trailing_slashes(repo_path);
7727 cwd = getcwd(NULL, 0);
7729 error = got_error_from_errno("getcwd");
7732 error = got_worktree_open(&worktree, cwd);
7733 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7736 if (repo_path == NULL) {
7738 got_worktree_get_repo_path(worktree));
7739 if (repo_path == NULL) {
7740 error = got_error_from_errno("strdup");
7746 if (repo_path == NULL) {
7747 repo_path = getcwd(NULL, 0);
7748 if (repo_path == NULL)
7749 return got_error_from_errno("getcwd");
7752 error = got_repo_open(&repo, repo_path, NULL);
7757 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7761 if (commit_id_str == NULL)
7762 commit_id_str = GOT_REF_HEAD;
7763 error = got_repo_match_object_id(&commit_id, NULL,
7764 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7768 for (i = 0; i < argc; i++) {
7770 error = got_object_id_by_path(&id, repo, commit_id,
7775 error = got_repo_match_object_id(&id, &label, argv[i],
7776 GOT_OBJ_TYPE_ANY, 0, repo);
7778 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7779 error->code != GOT_ERR_NOT_REF)
7781 error = got_object_id_by_path(&id, repo,
7782 commit_id, argv[i]);
7788 error = got_object_get_type(&obj_type, repo, id);
7793 case GOT_OBJ_TYPE_BLOB:
7794 error = cat_blob(id, repo, stdout);
7796 case GOT_OBJ_TYPE_TREE:
7797 error = cat_tree(id, repo, stdout);
7799 case GOT_OBJ_TYPE_COMMIT:
7800 error = cat_commit(id, repo, stdout);
7802 case GOT_OBJ_TYPE_TAG:
7803 error = cat_tag(id, repo, stdout);
7806 error = got_error(GOT_ERR_OBJ_TYPE);
7821 got_worktree_close(worktree);
7823 const struct got_error *repo_error;
7824 repo_error = got_repo_close(repo);