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 [target-directory]\n",
819 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
820 "[-p prefix] repository-path [worktree-path]\n", getprogname());
825 show_worktree_base_ref_warning(void)
827 fprintf(stderr, "%s: warning: could not create a reference "
828 "to the work tree's base commit; the commit could be "
829 "garbage-collected by Git; making the repository "
830 "writable and running 'got update' will prevent this\n",
834 struct got_checkout_progress_arg {
835 const char *worktree_path;
836 int had_base_commit_ref_error;
839 static const struct got_error *
840 checkout_progress(void *arg, unsigned char status, const char *path)
842 struct got_checkout_progress_arg *a = arg;
844 /* Base commit bump happens silently. */
845 if (status == GOT_STATUS_BUMP_BASE)
848 if (status == GOT_STATUS_BASE_REF_ERR) {
849 a->had_base_commit_ref_error = 1;
853 while (path[0] == '/')
856 printf("%c %s/%s\n", status, a->worktree_path, path);
860 static const struct got_error *
861 check_cancelled(void *arg)
863 if (sigint_received || sigpipe_received)
864 return got_error(GOT_ERR_CANCELLED);
868 static const struct got_error *
869 check_linear_ancestry(struct got_object_id *commit_id,
870 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
871 struct got_repository *repo)
873 const struct got_error *err = NULL;
874 struct got_object_id *yca_id;
876 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
877 commit_id, base_commit_id, repo, check_cancelled, NULL);
882 return got_error(GOT_ERR_ANCESTRY);
885 * Require a straight line of history between the target commit
886 * and the work tree's base commit.
888 * Non-linear situations such as this require a rebase:
890 * (commit) D F (base_commit)
898 * 'got update' only handles linear cases:
899 * Update forwards in time: A (base/yca) - B - C - D (commit)
900 * Update backwards in time: D (base) - C - B - A (commit/yca)
902 if (allow_forwards_in_time_only) {
903 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
904 return got_error(GOT_ERR_ANCESTRY);
905 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
906 got_object_id_cmp(base_commit_id, yca_id) != 0)
907 return got_error(GOT_ERR_ANCESTRY);
913 static const struct got_error *
914 check_same_branch(struct got_object_id *commit_id,
915 struct got_reference *head_ref, struct got_object_id *yca_id,
916 struct got_repository *repo)
918 const struct got_error *err = NULL;
919 struct got_commit_graph *graph = NULL;
920 struct got_object_id *head_commit_id = NULL;
921 int is_same_branch = 0;
923 err = got_ref_resolve(&head_commit_id, repo, head_ref);
927 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
931 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
936 err = got_commit_graph_open(&graph, "/", 1);
940 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
941 check_cancelled, NULL);
946 struct got_object_id *id;
947 err = got_commit_graph_iter_next(&id, graph, repo,
948 check_cancelled, NULL);
950 if (err->code == GOT_ERR_ITER_COMPLETED)
956 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
958 if (got_object_id_cmp(id, commit_id) == 0) {
966 got_commit_graph_close(graph);
967 free(head_commit_id);
968 if (!err && !is_same_branch)
969 err = got_error(GOT_ERR_ANCESTRY);
973 struct got_fetch_progress_arg {
974 char last_scaled_size[FMT_SCALED_STRSIZE];
979 static const struct got_error *
980 fetch_progress(void *arg, const char *message, off_t packfile_size,
981 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
983 struct got_fetch_progress_arg *a = arg;
984 char scaled_size[FMT_SCALED_STRSIZE];
985 int p_indexed, p_resolved;
986 int print_size = 0, print_indexed = 0, print_resolved = 0;
988 if (message && message[0] != '\0' && message[0] != '\n') {
989 printf("\rserver: %s", message);
990 if (strchr(message, '\n') == 0)
995 if (packfile_size > 0 || nobj_indexed > 0) {
996 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
997 (a->last_scaled_size[0] == '\0' ||
998 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1000 if (strlcpy(a->last_scaled_size, scaled_size,
1001 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1002 return got_error(GOT_ERR_NO_SPACE);
1004 if (nobj_indexed > 0) {
1005 p_indexed = (nobj_indexed * 100) / nobj_total;
1006 if (p_indexed != a->last_p_indexed) {
1007 a->last_p_indexed = p_indexed;
1012 if (nobj_resolved > 0) {
1013 p_resolved = (nobj_resolved * 100) /
1014 (nobj_total - nobj_loose);
1015 if (p_resolved != a->last_p_resolved) {
1016 a->last_p_resolved = p_resolved;
1024 if (print_size || print_indexed || print_resolved)
1027 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1029 printf("; indexing %d%%", p_indexed);
1031 printf("; resolving deltas %d%%", p_resolved);
1032 if (print_size || print_indexed || print_resolved)
1035 if (nobj_indexed > 0 && nobj_indexed == nobj_total &&
1036 nobj_resolved == nobj_total - nobj_loose)
1037 printf("\nWriting pack index...\n");
1042 static const struct got_error *
1043 cmd_clone(int argc, char *argv[])
1045 const struct got_error *err = NULL;
1046 const char *uri, *dirname;
1047 char *proto, *host, *port, *repo_name, *server_path;
1048 char *default_destdir = NULL, *id_str = NULL;
1049 const char *repo_path;
1050 struct got_repository *repo = NULL;
1051 struct got_pathlist_head refs, symrefs;
1052 struct got_pathlist_entry *pe;
1053 struct got_object_id *pack_hash = NULL;
1054 int ch, fetchfd = -1;
1055 struct got_fetch_progress_arg fpa;
1058 TAILQ_INIT(&symrefs);
1060 while ((ch = getopt(argc, argv, "")) != -1) {
1078 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1079 &repo_name, argv[0]);
1083 if (dirname == NULL) {
1084 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1085 err = got_error_from_errno("asprintf");
1088 repo_path = default_destdir;
1090 repo_path = dirname;
1092 err = got_path_mkdir(repo_path);
1096 err = got_repo_init(repo_path);
1100 err = got_repo_open(&repo, repo_path, NULL);
1104 err = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1108 printf("Connected to %s:%s\n", host, port);
1110 fpa.last_scaled_size[0] = '\0';
1111 fpa.last_p_indexed = -1;
1112 fpa.last_p_resolved = -1;
1113 err = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1114 repo, fetch_progress, &fpa);
1118 err = got_object_id_str(&id_str, pack_hash);
1121 printf("Fetched %s.pack\n", id_str);
1124 /* Set up references provided with the pack file. */
1125 TAILQ_FOREACH(pe, &refs, entry) {
1126 const char *refname = pe->path;
1127 struct got_object_id *id = pe->data;
1128 struct got_reference *ref;
1131 err = got_ref_alloc(&ref, refname, id);
1136 err = got_object_id_str(&id_str, id);
1139 printf("%s: %s\n", got_ref_get_name(ref), id_str);
1142 err = got_ref_write(ref, repo);
1148 /* Set the HEAD reference if the server provided one. */
1149 TAILQ_FOREACH(pe, &symrefs, entry) {
1150 struct got_reference *symref, *target_ref;
1151 const char *refname = pe->path;
1152 const char *target = pe->data;
1154 if (strcmp(refname, GOT_REF_HEAD) != 0)
1157 err = got_ref_open(&target_ref, repo, target, 0);
1159 if (err->code == GOT_ERR_NOT_REF)
1164 err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1165 got_ref_close(target_ref);
1169 printf("Setting %s to %s\n", GOT_REF_HEAD,
1170 got_ref_get_symref_target(symref));
1172 err = got_ref_write(symref, repo);
1173 got_ref_close(symref);
1178 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1179 err = got_error_from_errno("close");
1181 got_repo_close(repo);
1182 TAILQ_FOREACH(pe, &refs, entry) {
1183 free((void *)pe->path);
1186 got_pathlist_free(&refs);
1187 TAILQ_FOREACH(pe, &symrefs, entry) {
1188 free((void *)pe->path);
1191 got_pathlist_free(&symrefs);
1198 free(default_destdir);
1202 static const struct got_error *
1203 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1205 static char msg[512];
1206 const char *branch_name;
1208 if (got_ref_is_symbolic(ref))
1209 branch_name = got_ref_get_symref_target(ref);
1211 branch_name = got_ref_get_name(ref);
1213 if (strncmp("refs/heads/", branch_name, 11) == 0)
1216 snprintf(msg, sizeof(msg),
1217 "target commit is not contained in branch '%s'; "
1218 "the branch to use must be specified with -b; "
1219 "if necessary a new branch can be created for "
1220 "this commit with 'got branch -c %s BRANCH_NAME'",
1221 branch_name, commit_id_str);
1223 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1226 static const struct got_error *
1227 cmd_checkout(int argc, char *argv[])
1229 const struct got_error *error = NULL;
1230 struct got_repository *repo = NULL;
1231 struct got_reference *head_ref = NULL;
1232 struct got_worktree *worktree = NULL;
1233 char *repo_path = NULL;
1234 char *worktree_path = NULL;
1235 const char *path_prefix = "";
1236 const char *branch_name = GOT_REF_HEAD;
1237 char *commit_id_str = NULL;
1238 int ch, same_path_prefix, allow_nonempty = 0;
1239 struct got_pathlist_head paths;
1240 struct got_checkout_progress_arg cpa;
1244 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1247 branch_name = optarg;
1250 commit_id_str = strdup(optarg);
1251 if (commit_id_str == NULL)
1252 return got_error_from_errno("strdup");
1258 path_prefix = optarg;
1270 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1271 "unveil", NULL) == -1)
1275 char *cwd, *base, *dotgit;
1276 repo_path = realpath(argv[0], NULL);
1277 if (repo_path == NULL)
1278 return got_error_from_errno2("realpath", argv[0]);
1279 cwd = getcwd(NULL, 0);
1281 error = got_error_from_errno("getcwd");
1284 if (path_prefix[0]) {
1285 base = basename(path_prefix);
1287 error = got_error_from_errno2("basename",
1292 base = basename(repo_path);
1294 error = got_error_from_errno2("basename",
1299 dotgit = strstr(base, ".git");
1302 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1303 error = got_error_from_errno("asprintf");
1308 } else if (argc == 2) {
1309 repo_path = realpath(argv[0], NULL);
1310 if (repo_path == NULL) {
1311 error = got_error_from_errno2("realpath", argv[0]);
1314 worktree_path = realpath(argv[1], NULL);
1315 if (worktree_path == NULL) {
1316 if (errno != ENOENT) {
1317 error = got_error_from_errno2("realpath",
1321 worktree_path = strdup(argv[1]);
1322 if (worktree_path == NULL) {
1323 error = got_error_from_errno("strdup");
1330 got_path_strip_trailing_slashes(repo_path);
1331 got_path_strip_trailing_slashes(worktree_path);
1333 error = got_repo_open(&repo, repo_path, NULL);
1337 /* Pre-create work tree path for unveil(2) */
1338 error = got_path_mkdir(worktree_path);
1340 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1341 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1343 if (!allow_nonempty &&
1344 !got_path_dir_is_empty(worktree_path)) {
1345 error = got_error_path(worktree_path,
1346 GOT_ERR_DIR_NOT_EMPTY);
1351 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1355 error = got_ref_open(&head_ref, repo, branch_name, 0);
1359 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1360 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1363 error = got_worktree_open(&worktree, worktree_path);
1367 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1371 if (!same_path_prefix) {
1372 error = got_error(GOT_ERR_PATH_PREFIX);
1376 if (commit_id_str) {
1377 struct got_object_id *commit_id;
1378 error = got_repo_match_object_id(&commit_id, NULL,
1379 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1382 error = check_linear_ancestry(commit_id,
1383 got_worktree_get_base_commit_id(worktree), 0, repo);
1384 if (error != NULL) {
1386 if (error->code == GOT_ERR_ANCESTRY) {
1387 error = checkout_ancestry_error(
1388 head_ref, commit_id_str);
1392 error = check_same_branch(commit_id, head_ref, NULL, repo);
1394 if (error->code == GOT_ERR_ANCESTRY) {
1395 error = checkout_ancestry_error(
1396 head_ref, commit_id_str);
1400 error = got_worktree_set_base_commit_id(worktree, repo,
1407 error = got_pathlist_append(&paths, "", NULL);
1410 cpa.worktree_path = worktree_path;
1411 cpa.had_base_commit_ref_error = 0;
1412 error = got_worktree_checkout_files(worktree, &paths, repo,
1413 checkout_progress, &cpa, check_cancelled, NULL);
1417 printf("Now shut up and hack\n");
1418 if (cpa.had_base_commit_ref_error)
1419 show_worktree_base_ref_warning();
1421 got_pathlist_free(&paths);
1422 free(commit_id_str);
1424 free(worktree_path);
1431 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1436 static const struct got_error *
1437 update_progress(void *arg, unsigned char status, const char *path)
1439 int *did_something = arg;
1441 if (status == GOT_STATUS_EXISTS ||
1442 status == GOT_STATUS_BASE_REF_ERR)
1447 /* Base commit bump happens silently. */
1448 if (status == GOT_STATUS_BUMP_BASE)
1451 while (path[0] == '/')
1453 printf("%c %s\n", status, path);
1457 static const struct got_error *
1458 switch_head_ref(struct got_reference *head_ref,
1459 struct got_object_id *commit_id, struct got_worktree *worktree,
1460 struct got_repository *repo)
1462 const struct got_error *err = NULL;
1464 int ref_has_moved = 0;
1466 /* Trivial case: switching between two different references. */
1467 if (strcmp(got_ref_get_name(head_ref),
1468 got_worktree_get_head_ref_name(worktree)) != 0) {
1469 printf("Switching work tree from %s to %s\n",
1470 got_worktree_get_head_ref_name(worktree),
1471 got_ref_get_name(head_ref));
1472 return got_worktree_set_head_ref(worktree, head_ref);
1475 err = check_linear_ancestry(commit_id,
1476 got_worktree_get_base_commit_id(worktree), 0, repo);
1478 if (err->code != GOT_ERR_ANCESTRY)
1485 /* Switching to a rebased branch with the same reference name. */
1486 err = got_object_id_str(&base_id_str,
1487 got_worktree_get_base_commit_id(worktree));
1490 printf("Reference %s now points at a different branch\n",
1491 got_worktree_get_head_ref_name(worktree));
1492 printf("Switching work tree from %s to %s\n", base_id_str,
1493 got_worktree_get_head_ref_name(worktree));
1497 static const struct got_error *
1498 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1500 const struct got_error *err;
1503 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1507 return got_error(GOT_ERR_REBASING);
1509 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1513 return got_error(GOT_ERR_HISTEDIT_BUSY);
1518 static const struct got_error *
1519 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1520 char *argv[], struct got_worktree *worktree)
1522 const struct got_error *err = NULL;
1529 return got_error_from_errno("strdup");
1530 return got_pathlist_append(paths, path, NULL);
1533 for (i = 0; i < argc; i++) {
1534 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1537 err = got_pathlist_append(paths, path, NULL);
1547 static const struct got_error *
1548 cmd_update(int argc, char *argv[])
1550 const struct got_error *error = NULL;
1551 struct got_repository *repo = NULL;
1552 struct got_worktree *worktree = NULL;
1553 char *worktree_path = NULL;
1554 struct got_object_id *commit_id = NULL;
1555 char *commit_id_str = NULL;
1556 const char *branch_name = NULL;
1557 struct got_reference *head_ref = NULL;
1558 struct got_pathlist_head paths;
1559 struct got_pathlist_entry *pe;
1560 int ch, did_something = 0;
1564 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1567 branch_name = optarg;
1570 commit_id_str = strdup(optarg);
1571 if (commit_id_str == NULL)
1572 return got_error_from_errno("strdup");
1584 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1585 "unveil", NULL) == -1)
1588 worktree_path = getcwd(NULL, 0);
1589 if (worktree_path == NULL) {
1590 error = got_error_from_errno("getcwd");
1593 error = got_worktree_open(&worktree, worktree_path);
1597 error = check_rebase_or_histedit_in_progress(worktree);
1601 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1606 error = apply_unveil(got_repo_get_path(repo), 0,
1607 got_worktree_get_root_path(worktree));
1611 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1615 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1616 got_worktree_get_head_ref_name(worktree), 0);
1619 if (commit_id_str == NULL) {
1620 error = got_ref_resolve(&commit_id, repo, head_ref);
1623 error = got_object_id_str(&commit_id_str, commit_id);
1627 error = got_repo_match_object_id(&commit_id, NULL,
1628 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1629 free(commit_id_str);
1630 commit_id_str = NULL;
1633 error = got_object_id_str(&commit_id_str, commit_id);
1639 struct got_object_id *head_commit_id;
1640 TAILQ_FOREACH(pe, &paths, entry) {
1641 if (pe->path_len == 0)
1643 error = got_error_msg(GOT_ERR_BAD_PATH,
1644 "switching between branches requires that "
1645 "the entire work tree gets updated");
1648 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1651 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1653 free(head_commit_id);
1656 error = check_same_branch(commit_id, head_ref, NULL, repo);
1659 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1663 error = check_linear_ancestry(commit_id,
1664 got_worktree_get_base_commit_id(worktree), 0, repo);
1665 if (error != NULL) {
1666 if (error->code == GOT_ERR_ANCESTRY)
1667 error = got_error(GOT_ERR_BRANCH_MOVED);
1670 error = check_same_branch(commit_id, head_ref, NULL, repo);
1675 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1677 error = got_worktree_set_base_commit_id(worktree, repo,
1683 error = got_worktree_checkout_files(worktree, &paths, repo,
1684 update_progress, &did_something, check_cancelled, NULL);
1689 printf("Updated to commit %s\n", commit_id_str);
1691 printf("Already up-to-date\n");
1693 free(worktree_path);
1694 TAILQ_FOREACH(pe, &paths, entry)
1695 free((char *)pe->path);
1696 got_pathlist_free(&paths);
1698 free(commit_id_str);
1702 static const struct got_error *
1703 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1704 const char *path, int diff_context, int ignore_whitespace,
1705 struct got_repository *repo)
1707 const struct got_error *err = NULL;
1708 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1711 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1716 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1720 while (path[0] == '/')
1722 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1723 ignore_whitespace, stdout);
1726 got_object_blob_close(blob1);
1727 got_object_blob_close(blob2);
1731 static const struct got_error *
1732 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1733 const char *path, int diff_context, int ignore_whitespace,
1734 struct got_repository *repo)
1736 const struct got_error *err = NULL;
1737 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1738 struct got_diff_blob_output_unidiff_arg arg;
1741 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1746 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1750 arg.diff_context = diff_context;
1751 arg.ignore_whitespace = ignore_whitespace;
1752 arg.outfile = stdout;
1753 while (path[0] == '/')
1755 err = got_diff_tree(tree1, tree2, path, path, repo,
1756 got_diff_blob_output_unidiff, &arg, 1);
1759 got_object_tree_close(tree1);
1761 got_object_tree_close(tree2);
1765 static const struct got_error *
1766 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1767 const char *path, int diff_context, struct got_repository *repo)
1769 const struct got_error *err = NULL;
1770 struct got_commit_object *pcommit = NULL;
1771 char *id_str1 = NULL, *id_str2 = NULL;
1772 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1773 struct got_object_qid *qid;
1775 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1777 err = got_object_open_as_commit(&pcommit, repo,
1783 if (path && path[0] != '\0') {
1785 err = got_object_id_by_path(&obj_id2, repo, id, path);
1788 err = got_object_id_str(&id_str2, obj_id2);
1794 err = got_object_id_by_path(&obj_id1, repo,
1800 err = got_object_id_str(&id_str1, obj_id1);
1806 err = got_object_get_type(&obj_type, repo, obj_id2);
1811 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1813 case GOT_OBJ_TYPE_BLOB:
1814 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1817 case GOT_OBJ_TYPE_TREE:
1818 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1822 err = got_error(GOT_ERR_OBJ_TYPE);
1828 obj_id2 = got_object_commit_get_tree_id(commit);
1829 err = got_object_id_str(&id_str2, obj_id2);
1832 obj_id1 = got_object_commit_get_tree_id(pcommit);
1833 err = got_object_id_str(&id_str1, obj_id1);
1836 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1837 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1843 got_object_commit_close(pcommit);
1848 get_datestr(time_t *time, char *datebuf)
1850 struct tm mytm, *tm;
1853 tm = gmtime_r(time, &mytm);
1856 s = asctime_r(tm, datebuf);
1859 p = strchr(s, '\n');
1865 static const struct got_error *
1866 match_logmsg(int *have_match, struct got_object_id *id,
1867 struct got_commit_object *commit, regex_t *regex)
1869 const struct got_error *err = NULL;
1870 regmatch_t regmatch;
1871 char *id_str = NULL, *logmsg = NULL;
1875 err = got_object_id_str(&id_str, id);
1879 err = got_object_commit_get_logmsg(&logmsg, commit);
1883 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1891 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1893 static const struct got_error *
1894 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1895 struct got_repository *repo, const char *path, int show_patch,
1896 int diff_context, struct got_reflist_head *refs)
1898 const struct got_error *err = NULL;
1899 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1901 time_t committer_time;
1902 const char *author, *committer;
1903 char *refs_str = NULL;
1904 struct got_reflist_entry *re;
1906 SIMPLEQ_FOREACH(re, refs, entry) {
1909 struct got_tag_object *tag = NULL;
1912 name = got_ref_get_name(re->ref);
1913 if (strcmp(name, GOT_REF_HEAD) == 0)
1915 if (strncmp(name, "refs/", 5) == 0)
1917 if (strncmp(name, "got/", 4) == 0)
1919 if (strncmp(name, "heads/", 6) == 0)
1921 if (strncmp(name, "remotes/", 8) == 0)
1923 if (strncmp(name, "tags/", 5) == 0) {
1924 err = got_object_open_as_tag(&tag, repo, re->id);
1926 if (err->code != GOT_ERR_OBJ_TYPE)
1928 /* Ref points at something other than a tag. */
1933 cmp = got_object_id_cmp(tag ?
1934 got_object_tag_get_object_id(tag) : re->id, id);
1936 got_object_tag_close(tag);
1940 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1942 err = got_error_from_errno("asprintf");
1948 err = got_object_id_str(&id_str, id);
1952 printf(GOT_COMMIT_SEP_STR);
1953 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1954 refs_str ? refs_str : "", refs_str ? ")" : "");
1959 printf("from: %s\n", got_object_commit_get_author(commit));
1960 committer_time = got_object_commit_get_committer_time(commit);
1961 datestr = get_datestr(&committer_time, datebuf);
1963 printf("date: %s UTC\n", datestr);
1964 author = got_object_commit_get_author(commit);
1965 committer = got_object_commit_get_committer(commit);
1966 if (strcmp(author, committer) != 0)
1967 printf("via: %s\n", committer);
1968 if (got_object_commit_get_nparents(commit) > 1) {
1969 const struct got_object_id_queue *parent_ids;
1970 struct got_object_qid *qid;
1972 parent_ids = got_object_commit_get_parent_ids(commit);
1973 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1974 err = got_object_id_str(&id_str, qid->id);
1977 printf("parent %d: %s\n", n++, id_str);
1982 err = got_object_commit_get_logmsg(&logmsg0, commit);
1988 line = strsep(&logmsg, "\n");
1990 printf(" %s\n", line);
1995 err = print_patch(commit, id, path, diff_context, repo);
2000 if (fflush(stdout) != 0 && err == NULL)
2001 err = got_error_from_errno("fflush");
2005 static const struct got_error *
2006 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2007 const char *path, int show_patch, const char *search_pattern,
2008 int diff_context, int limit, int log_branches,
2009 struct got_reflist_head *refs)
2011 const struct got_error *err;
2012 struct got_commit_graph *graph;
2016 if (search_pattern &&
2017 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2018 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2020 err = got_commit_graph_open(&graph, path, !log_branches);
2023 err = got_commit_graph_iter_start(graph, root_id, repo,
2024 check_cancelled, NULL);
2028 struct got_commit_object *commit;
2029 struct got_object_id *id;
2031 if (sigint_received || sigpipe_received)
2034 err = got_commit_graph_iter_next(&id, graph, repo,
2035 check_cancelled, NULL);
2037 if (err->code == GOT_ERR_ITER_COMPLETED)
2044 err = got_object_open_as_commit(&commit, repo, id);
2048 if (search_pattern) {
2049 err = match_logmsg(&have_match, id, commit, ®ex);
2051 got_object_commit_close(commit);
2054 if (have_match == 0) {
2055 got_object_commit_close(commit);
2060 err = print_commit(commit, id, repo, path, show_patch,
2061 diff_context, refs);
2062 got_object_commit_close(commit);
2063 if (err || (limit && --limit == 0))
2069 got_commit_graph_close(graph);
2076 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2077 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2082 get_default_log_limit(void)
2084 const char *got_default_log_limit;
2088 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2089 if (got_default_log_limit == NULL)
2091 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2097 static const struct got_error *
2098 cmd_log(int argc, char *argv[])
2100 const struct got_error *error;
2101 struct got_repository *repo = NULL;
2102 struct got_worktree *worktree = NULL;
2103 struct got_commit_object *commit = NULL;
2104 struct got_object_id *id = NULL;
2105 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2106 const char *start_commit = NULL, *search_pattern = NULL;
2107 int diff_context = -1, ch;
2108 int show_patch = 0, limit = 0, log_branches = 0;
2110 struct got_reflist_head refs;
2112 SIMPLEQ_INIT(&refs);
2115 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2121 limit = get_default_log_limit();
2123 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2129 start_commit = optarg;
2132 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2135 err(1, "-C option %s", errstr);
2138 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2140 err(1, "-l option %s", errstr);
2146 repo_path = realpath(optarg, NULL);
2147 if (repo_path == NULL)
2148 return got_error_from_errno2("realpath",
2150 got_path_strip_trailing_slashes(repo_path);
2153 search_pattern = optarg;
2164 if (diff_context == -1)
2166 else if (!show_patch)
2167 errx(1, "-C reguires -p");
2169 cwd = getcwd(NULL, 0);
2171 error = got_error_from_errno("getcwd");
2175 error = got_worktree_open(&worktree, cwd);
2176 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2183 error = got_error_from_errno("strdup");
2186 } else if (argc == 1) {
2188 error = got_worktree_resolve_path(&path, worktree,
2193 path = strdup(argv[0]);
2195 error = got_error_from_errno("strdup");
2202 if (repo_path == NULL) {
2203 repo_path = worktree ?
2204 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2206 if (repo_path == NULL) {
2207 error = got_error_from_errno("strdup");
2211 error = got_repo_open(&repo, repo_path, NULL);
2215 error = apply_unveil(got_repo_get_path(repo), 1,
2216 worktree ? got_worktree_get_root_path(worktree) : NULL);
2220 if (start_commit == NULL) {
2221 struct got_reference *head_ref;
2222 error = got_ref_open(&head_ref, repo,
2223 worktree ? got_worktree_get_head_ref_name(worktree)
2227 error = got_ref_resolve(&id, repo, head_ref);
2228 got_ref_close(head_ref);
2231 error = got_object_open_as_commit(&commit, repo, id);
2233 struct got_reference *ref;
2234 error = got_ref_open(&ref, repo, start_commit, 0);
2235 if (error == NULL) {
2237 error = got_ref_resolve(&id, repo, ref);
2241 error = got_object_get_type(&obj_type, repo, id);
2244 if (obj_type == GOT_OBJ_TYPE_TAG) {
2245 struct got_tag_object *tag;
2246 error = got_object_open_as_tag(&tag, repo, id);
2249 if (got_object_tag_get_object_type(tag) !=
2250 GOT_OBJ_TYPE_COMMIT) {
2251 got_object_tag_close(tag);
2252 error = got_error(GOT_ERR_OBJ_TYPE);
2256 id = got_object_id_dup(
2257 got_object_tag_get_object_id(tag));
2259 error = got_error_from_errno(
2260 "got_object_id_dup");
2261 got_object_tag_close(tag);
2264 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2265 error = got_error(GOT_ERR_OBJ_TYPE);
2268 error = got_object_open_as_commit(&commit, repo, id);
2272 if (commit == NULL) {
2273 error = got_repo_match_object_id_prefix(&id,
2274 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2283 const char *prefix = got_worktree_get_path_prefix(worktree);
2285 if (asprintf(&p, "%s%s%s", prefix,
2286 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2287 error = got_error_from_errno("asprintf");
2290 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2293 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2298 path = in_repo_path;
2301 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2305 error = print_commits(id, repo, path, show_patch, search_pattern,
2306 diff_context, limit, log_branches, &refs);
2313 got_worktree_close(worktree);
2315 const struct got_error *repo_error;
2316 repo_error = got_repo_close(repo);
2320 got_ref_list_free(&refs);
2327 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2328 "[-w] [object1 object2 | path]\n", getprogname());
2332 struct print_diff_arg {
2333 struct got_repository *repo;
2334 struct got_worktree *worktree;
2339 int ignore_whitespace;
2342 static const struct got_error *
2343 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2344 const char *path, struct got_object_id *blob_id,
2345 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2346 int dirfd, const char *de_name)
2348 struct print_diff_arg *a = arg;
2349 const struct got_error *err = NULL;
2350 struct got_blob_object *blob1 = NULL;
2353 char *abspath = NULL, *label1 = NULL;
2356 if (a->diff_staged) {
2357 if (staged_status != GOT_STATUS_MODIFY &&
2358 staged_status != GOT_STATUS_ADD &&
2359 staged_status != GOT_STATUS_DELETE)
2362 if (staged_status == GOT_STATUS_DELETE)
2364 if (status == GOT_STATUS_NONEXISTENT)
2365 return got_error_set_errno(ENOENT, path);
2366 if (status != GOT_STATUS_MODIFY &&
2367 status != GOT_STATUS_ADD &&
2368 status != GOT_STATUS_DELETE &&
2369 status != GOT_STATUS_CONFLICT)
2373 if (!a->header_shown) {
2374 printf("diff %s %s%s\n", a->id_str,
2375 got_worktree_get_root_path(a->worktree),
2376 a->diff_staged ? " (staged changes)" : "");
2377 a->header_shown = 1;
2380 if (a->diff_staged) {
2381 const char *label1 = NULL, *label2 = NULL;
2382 switch (staged_status) {
2383 case GOT_STATUS_MODIFY:
2387 case GOT_STATUS_ADD:
2390 case GOT_STATUS_DELETE:
2394 return got_error(GOT_ERR_FILE_STATUS);
2396 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2397 label1, label2, a->diff_context, a->ignore_whitespace,
2401 if (staged_status == GOT_STATUS_ADD ||
2402 staged_status == GOT_STATUS_MODIFY) {
2404 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2408 err = got_object_id_str(&id_str, staged_blob_id);
2411 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2412 err = got_error_from_errno("asprintf");
2417 } else if (status != GOT_STATUS_ADD) {
2418 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2423 if (status != GOT_STATUS_DELETE) {
2424 if (asprintf(&abspath, "%s/%s",
2425 got_worktree_get_root_path(a->worktree), path) == -1) {
2426 err = got_error_from_errno("asprintf");
2431 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2433 err = got_error_from_errno2("openat", abspath);
2437 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2439 err = got_error_from_errno2("open", abspath);
2443 if (fstat(fd, &sb) == -1) {
2444 err = got_error_from_errno2("fstat", abspath);
2447 f2 = fdopen(fd, "r");
2449 err = got_error_from_errno2("fdopen", abspath);
2456 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2457 a->diff_context, a->ignore_whitespace, stdout);
2460 got_object_blob_close(blob1);
2461 if (f2 && fclose(f2) == EOF && err == NULL)
2462 err = got_error_from_errno("fclose");
2463 if (fd != -1 && close(fd) == -1 && err == NULL)
2464 err = got_error_from_errno("close");
2469 static const struct got_error *
2470 cmd_diff(int argc, char *argv[])
2472 const struct got_error *error;
2473 struct got_repository *repo = NULL;
2474 struct got_worktree *worktree = NULL;
2475 char *cwd = NULL, *repo_path = NULL;
2476 struct got_object_id *id1 = NULL, *id2 = NULL;
2477 const char *id_str1 = NULL, *id_str2 = NULL;
2478 char *label1 = NULL, *label2 = NULL;
2480 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2485 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2490 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2493 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2496 err(1, "-C option %s", errstr);
2499 repo_path = realpath(optarg, NULL);
2500 if (repo_path == NULL)
2501 return got_error_from_errno2("realpath",
2503 got_path_strip_trailing_slashes(repo_path);
2509 ignore_whitespace = 1;
2520 cwd = getcwd(NULL, 0);
2522 error = got_error_from_errno("getcwd");
2525 error = got_worktree_open(&worktree, cwd);
2526 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2529 if (worktree == NULL) {
2530 error = got_error(GOT_ERR_NOT_WORKTREE);
2535 "-r option can't be used when diffing a work tree");
2536 repo_path = strdup(got_worktree_get_repo_path(worktree));
2537 if (repo_path == NULL) {
2538 error = got_error_from_errno("strdup");
2542 error = got_worktree_resolve_path(&path, worktree,
2549 error = got_error_from_errno("strdup");
2553 } else if (argc == 2) {
2555 errx(1, "-s option can't be used when diffing "
2556 "objects in repository");
2559 if (worktree && repo_path == NULL) {
2561 strdup(got_worktree_get_repo_path(worktree));
2562 if (repo_path == NULL) {
2563 error = got_error_from_errno("strdup");
2570 if (repo_path == NULL) {
2571 repo_path = getcwd(NULL, 0);
2572 if (repo_path == NULL)
2573 return got_error_from_errno("getcwd");
2576 error = got_repo_open(&repo, repo_path, NULL);
2581 error = apply_unveil(got_repo_get_path(repo), 1,
2582 worktree ? got_worktree_get_root_path(worktree) : NULL);
2587 struct print_diff_arg arg;
2588 struct got_pathlist_head paths;
2593 error = got_object_id_str(&id_str,
2594 got_worktree_get_base_commit_id(worktree));
2598 arg.worktree = worktree;
2599 arg.diff_context = diff_context;
2600 arg.id_str = id_str;
2601 arg.header_shown = 0;
2602 arg.diff_staged = diff_staged;
2603 arg.ignore_whitespace = ignore_whitespace;
2605 error = got_pathlist_append(&paths, path, NULL);
2609 error = got_worktree_status(worktree, &paths, repo, print_diff,
2610 &arg, check_cancelled, NULL);
2612 got_pathlist_free(&paths);
2616 error = got_repo_match_object_id(&id1, &label1, id_str1,
2617 GOT_OBJ_TYPE_ANY, 1, repo);
2621 error = got_repo_match_object_id(&id2, &label2, id_str2,
2622 GOT_OBJ_TYPE_ANY, 1, repo);
2626 error = got_object_get_type(&type1, repo, id1);
2630 error = got_object_get_type(&type2, repo, id2);
2634 if (type1 != type2) {
2635 error = got_error(GOT_ERR_OBJ_TYPE);
2640 case GOT_OBJ_TYPE_BLOB:
2641 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2642 diff_context, ignore_whitespace, repo, stdout);
2644 case GOT_OBJ_TYPE_TREE:
2645 error = got_diff_objects_as_trees(id1, id2, "", "",
2646 diff_context, ignore_whitespace, repo, stdout);
2648 case GOT_OBJ_TYPE_COMMIT:
2649 printf("diff %s %s\n", label1, label2);
2650 error = got_diff_objects_as_commits(id1, id2, diff_context,
2651 ignore_whitespace, repo, stdout);
2654 error = got_error(GOT_ERR_OBJ_TYPE);
2663 got_worktree_close(worktree);
2665 const struct got_error *repo_error;
2666 repo_error = got_repo_close(repo);
2677 "usage: %s blame [-c commit] [-r repository-path] path\n",
2686 char datebuf[11]; /* YYYY-MM-DD + NUL */
2689 struct blame_cb_args {
2690 struct blame_line *lines;
2694 off_t *line_offsets;
2696 struct got_repository *repo;
2699 static const struct got_error *
2700 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2702 const struct got_error *err = NULL;
2703 struct blame_cb_args *a = arg;
2704 struct blame_line *bline;
2706 size_t linesize = 0;
2707 struct got_commit_object *commit = NULL;
2710 time_t committer_time;
2712 if (nlines != a->nlines ||
2713 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2714 return got_error(GOT_ERR_RANGE);
2716 if (sigint_received)
2717 return got_error(GOT_ERR_ITER_COMPLETED);
2720 return NULL; /* no change in this commit */
2722 /* Annotate this line. */
2723 bline = &a->lines[lineno - 1];
2724 if (bline->annotated)
2726 err = got_object_id_str(&bline->id_str, id);
2730 err = got_object_open_as_commit(&commit, a->repo, id);
2734 bline->committer = strdup(got_object_commit_get_committer(commit));
2735 if (bline->committer == NULL) {
2736 err = got_error_from_errno("strdup");
2740 committer_time = got_object_commit_get_committer_time(commit);
2741 if (localtime_r(&committer_time, &tm) == NULL)
2742 return got_error_from_errno("localtime_r");
2743 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2744 &tm) >= sizeof(bline->datebuf)) {
2745 err = got_error(GOT_ERR_NO_SPACE);
2748 bline->annotated = 1;
2750 /* Print lines annotated so far. */
2751 bline = &a->lines[a->lineno_cur - 1];
2752 if (!bline->annotated)
2755 offset = a->line_offsets[a->lineno_cur - 1];
2756 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2757 err = got_error_from_errno("fseeko");
2761 while (bline->annotated) {
2762 char *smallerthan, *at, *nl, *committer;
2765 if (getline(&line, &linesize, a->f) == -1) {
2767 err = got_error_from_errno("getline");
2771 committer = bline->committer;
2772 smallerthan = strchr(committer, '<');
2773 if (smallerthan && smallerthan[1] != '\0')
2774 committer = smallerthan + 1;
2775 at = strchr(committer, '@');
2778 len = strlen(committer);
2780 committer[8] = '\0';
2782 nl = strchr(line, '\n');
2785 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2786 bline->id_str, bline->datebuf, committer, line);
2789 bline = &a->lines[a->lineno_cur - 1];
2793 got_object_commit_close(commit);
2798 static const struct got_error *
2799 cmd_blame(int argc, char *argv[])
2801 const struct got_error *error;
2802 struct got_repository *repo = NULL;
2803 struct got_worktree *worktree = NULL;
2804 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2805 struct got_object_id *obj_id = NULL;
2806 struct got_object_id *commit_id = NULL;
2807 struct got_blob_object *blob = NULL;
2808 char *commit_id_str = NULL;
2809 struct blame_cb_args bca;
2810 int ch, obj_type, i;
2813 memset(&bca, 0, sizeof(bca));
2816 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2821 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2824 commit_id_str = optarg;
2827 repo_path = realpath(optarg, NULL);
2828 if (repo_path == NULL)
2829 return got_error_from_errno2("realpath",
2831 got_path_strip_trailing_slashes(repo_path);
2847 cwd = getcwd(NULL, 0);
2849 error = got_error_from_errno("getcwd");
2852 if (repo_path == NULL) {
2853 error = got_worktree_open(&worktree, cwd);
2854 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2860 strdup(got_worktree_get_repo_path(worktree));
2861 if (repo_path == NULL)
2862 error = got_error_from_errno("strdup");
2866 repo_path = strdup(cwd);
2867 if (repo_path == NULL) {
2868 error = got_error_from_errno("strdup");
2874 error = got_repo_open(&repo, repo_path, NULL);
2878 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2883 const char *prefix = got_worktree_get_path_prefix(worktree);
2884 char *p, *worktree_subdir = cwd +
2885 strlen(got_worktree_get_root_path(worktree));
2886 if (asprintf(&p, "%s%s%s%s%s",
2887 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2888 worktree_subdir, worktree_subdir[0] ? "/" : "",
2890 error = got_error_from_errno("asprintf");
2893 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2896 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2901 if (commit_id_str == NULL) {
2902 struct got_reference *head_ref;
2903 error = got_ref_open(&head_ref, repo, worktree ?
2904 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2907 error = got_ref_resolve(&commit_id, repo, head_ref);
2908 got_ref_close(head_ref);
2912 error = got_repo_match_object_id(&commit_id, NULL,
2913 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2918 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2922 error = got_object_get_type(&obj_type, repo, obj_id);
2926 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2927 error = got_error(GOT_ERR_OBJ_TYPE);
2931 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2934 bca.f = got_opentemp();
2935 if (bca.f == NULL) {
2936 error = got_error_from_errno("got_opentemp");
2939 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2940 &bca.line_offsets, bca.f, blob);
2941 if (error || bca.nlines == 0)
2944 /* Don't include \n at EOF in the blame line count. */
2945 if (bca.line_offsets[bca.nlines - 1] == filesize)
2948 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2949 if (bca.lines == NULL) {
2950 error = got_error_from_errno("calloc");
2954 bca.nlines_prec = 0;
2962 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2963 check_cancelled, NULL);
2971 got_object_blob_close(blob);
2973 got_worktree_close(worktree);
2975 const struct got_error *repo_error;
2976 repo_error = got_repo_close(repo);
2981 for (i = 0; i < bca.nlines; i++) {
2982 struct blame_line *bline = &bca.lines[i];
2983 free(bline->id_str);
2984 free(bline->committer);
2988 free(bca.line_offsets);
2989 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2990 error = got_error_from_errno("fclose");
2998 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3004 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3005 const char *root_path)
3007 int is_root_path = (strcmp(path, root_path) == 0);
3008 const char *modestr = "";
3009 mode_t mode = got_tree_entry_get_mode(te);
3011 path += strlen(root_path);
3012 while (path[0] == '/')
3015 if (got_object_tree_entry_is_submodule(te))
3017 else if (S_ISLNK(mode))
3019 else if (S_ISDIR(mode))
3021 else if (mode & S_IXUSR)
3024 printf("%s%s%s%s%s\n", id ? id : "", path,
3025 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3028 static const struct got_error *
3029 print_tree(const char *path, struct got_object_id *commit_id,
3030 int show_ids, int recurse, const char *root_path,
3031 struct got_repository *repo)
3033 const struct got_error *err = NULL;
3034 struct got_object_id *tree_id = NULL;
3035 struct got_tree_object *tree = NULL;
3038 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3042 err = got_object_open_as_tree(&tree, repo, tree_id);
3045 nentries = got_object_tree_get_nentries(tree);
3046 for (i = 0; i < nentries; i++) {
3047 struct got_tree_entry *te;
3050 if (sigint_received || sigpipe_received)
3053 te = got_object_tree_get_entry(tree, i);
3056 err = got_object_id_str(&id_str,
3057 got_tree_entry_get_id(te));
3060 if (asprintf(&id, "%s ", id_str) == -1) {
3061 err = got_error_from_errno("asprintf");
3067 print_entry(te, id, path, root_path);
3070 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3072 if (asprintf(&child_path, "%s%s%s", path,
3073 path[0] == '/' && path[1] == '\0' ? "" : "/",
3074 got_tree_entry_get_name(te)) == -1) {
3075 err = got_error_from_errno("asprintf");
3078 err = print_tree(child_path, commit_id, show_ids, 1,
3087 got_object_tree_close(tree);
3092 static const struct got_error *
3093 cmd_tree(int argc, char *argv[])
3095 const struct got_error *error;
3096 struct got_repository *repo = NULL;
3097 struct got_worktree *worktree = NULL;
3099 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3100 struct got_object_id *commit_id = NULL;
3101 char *commit_id_str = NULL;
3102 int show_ids = 0, recurse = 0;
3106 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3111 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3114 commit_id_str = optarg;
3117 repo_path = realpath(optarg, NULL);
3118 if (repo_path == NULL)
3119 return got_error_from_errno2("realpath",
3121 got_path_strip_trailing_slashes(repo_path);
3145 cwd = getcwd(NULL, 0);
3147 error = got_error_from_errno("getcwd");
3150 if (repo_path == NULL) {
3151 error = got_worktree_open(&worktree, cwd);
3152 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3158 strdup(got_worktree_get_repo_path(worktree));
3159 if (repo_path == NULL)
3160 error = got_error_from_errno("strdup");
3164 repo_path = strdup(cwd);
3165 if (repo_path == NULL) {
3166 error = got_error_from_errno("strdup");
3172 error = got_repo_open(&repo, repo_path, NULL);
3176 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3182 char *p, *worktree_subdir = cwd +
3183 strlen(got_worktree_get_root_path(worktree));
3184 if (asprintf(&p, "%s/%s",
3185 got_worktree_get_path_prefix(worktree),
3186 worktree_subdir) == -1) {
3187 error = got_error_from_errno("asprintf");
3190 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3197 if (in_repo_path == NULL) {
3198 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3203 if (commit_id_str == NULL) {
3204 struct got_reference *head_ref;
3205 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3208 error = got_ref_resolve(&commit_id, repo, head_ref);
3209 got_ref_close(head_ref);
3213 error = got_repo_match_object_id(&commit_id, NULL,
3214 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3219 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3220 in_repo_path, repo);
3227 got_worktree_close(worktree);
3229 const struct got_error *repo_error;
3230 repo_error = got_repo_close(repo);
3240 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3244 static const struct got_error *
3245 print_status(void *arg, unsigned char status, unsigned char staged_status,
3246 const char *path, struct got_object_id *blob_id,
3247 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3248 int dirfd, const char *de_name)
3250 if (status == staged_status && (status == GOT_STATUS_DELETE))
3251 status = GOT_STATUS_NO_CHANGE;
3252 printf("%c%c %s\n", status, staged_status, path);
3256 static const struct got_error *
3257 cmd_status(int argc, char *argv[])
3259 const struct got_error *error = NULL;
3260 struct got_repository *repo = NULL;
3261 struct got_worktree *worktree = NULL;
3263 struct got_pathlist_head paths;
3264 struct got_pathlist_entry *pe;
3269 while ((ch = getopt(argc, argv, "")) != -1) {
3281 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3285 cwd = getcwd(NULL, 0);
3287 error = got_error_from_errno("getcwd");
3291 error = got_worktree_open(&worktree, cwd);
3295 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3300 error = apply_unveil(got_repo_get_path(repo), 1,
3301 got_worktree_get_root_path(worktree));
3305 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3309 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3310 check_cancelled, NULL);
3312 TAILQ_FOREACH(pe, &paths, entry)
3313 free((char *)pe->path);
3314 got_pathlist_free(&paths);
3323 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3328 static const struct got_error *
3329 list_refs(struct got_repository *repo)
3331 static const struct got_error *err = NULL;
3332 struct got_reflist_head refs;
3333 struct got_reflist_entry *re;
3335 SIMPLEQ_INIT(&refs);
3336 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3340 SIMPLEQ_FOREACH(re, &refs, entry) {
3342 refstr = got_ref_to_str(re->ref);
3344 return got_error_from_errno("got_ref_to_str");
3345 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3349 got_ref_list_free(&refs);
3353 static const struct got_error *
3354 delete_ref(struct got_repository *repo, const char *refname)
3356 const struct got_error *err = NULL;
3357 struct got_reference *ref;
3359 err = got_ref_open(&ref, repo, refname, 0);
3363 err = got_ref_delete(ref, repo);
3368 static const struct got_error *
3369 add_ref(struct got_repository *repo, const char *refname, const char *target)
3371 const struct got_error *err = NULL;
3372 struct got_object_id *id;
3373 struct got_reference *ref = NULL;
3376 * Don't let the user create a reference name with a leading '-'.
3377 * While technically a valid reference name, this case is usually
3378 * an unintended typo.
3380 if (refname[0] == '-')
3381 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3383 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3386 struct got_reference *target_ref;
3388 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3390 err = got_ref_open(&target_ref, repo, target, 0);
3393 err = got_ref_resolve(&id, repo, target_ref);
3394 got_ref_close(target_ref);
3399 err = got_ref_alloc(&ref, refname, id);
3403 err = got_ref_write(ref, repo);
3411 static const struct got_error *
3412 add_symref(struct got_repository *repo, const char *refname, const char *target)
3414 const struct got_error *err = NULL;
3415 struct got_reference *ref = NULL;
3416 struct got_reference *target_ref = NULL;
3419 * Don't let the user create a reference name with a leading '-'.
3420 * While technically a valid reference name, this case is usually
3421 * an unintended typo.
3423 if (refname[0] == '-')
3424 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3426 err = got_ref_open(&target_ref, repo, target, 0);
3430 err = got_ref_alloc_symref(&ref, refname, target_ref);
3434 err = got_ref_write(ref, repo);
3437 got_ref_close(target_ref);
3443 static const struct got_error *
3444 cmd_ref(int argc, char *argv[])
3446 const struct got_error *error = NULL;
3447 struct got_repository *repo = NULL;
3448 struct got_worktree *worktree = NULL;
3449 char *cwd = NULL, *repo_path = NULL;
3450 int ch, do_list = 0, create_symref = 0;
3451 const char *delref = NULL;
3453 /* TODO: Add -s option for adding symbolic references. */
3454 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3460 repo_path = realpath(optarg, NULL);
3461 if (repo_path == NULL)
3462 return got_error_from_errno2("realpath",
3464 got_path_strip_trailing_slashes(repo_path);
3478 if (do_list && delref)
3479 errx(1, "-l and -d options are mutually exclusive\n");
3484 if (do_list || delref) {
3486 errx(1, "-s option cannot be used together with the "
3487 "-l or -d options");
3490 } else if (argc != 2)
3495 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3499 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3500 "sendfd unveil", NULL) == -1)
3504 cwd = getcwd(NULL, 0);
3506 error = got_error_from_errno("getcwd");
3510 if (repo_path == NULL) {
3511 error = got_worktree_open(&worktree, cwd);
3512 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3518 strdup(got_worktree_get_repo_path(worktree));
3519 if (repo_path == NULL)
3520 error = got_error_from_errno("strdup");
3524 repo_path = strdup(cwd);
3525 if (repo_path == NULL) {
3526 error = got_error_from_errno("strdup");
3532 error = got_repo_open(&repo, repo_path, NULL);
3536 error = apply_unveil(got_repo_get_path(repo), do_list,
3537 worktree ? got_worktree_get_root_path(worktree) : NULL);
3542 error = list_refs(repo);
3544 error = delete_ref(repo, delref);
3545 else if (create_symref)
3546 error = add_symref(repo, argv[0], argv[1]);
3548 error = add_ref(repo, argv[0], argv[1]);
3551 got_repo_close(repo);
3553 got_worktree_close(worktree);
3563 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3564 "[name]\n", getprogname());
3568 static const struct got_error *
3569 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3570 struct got_reference *ref)
3572 const struct got_error *err = NULL;
3573 const char *refname, *marker = " ";
3576 refname = got_ref_get_name(ref);
3577 if (worktree && strcmp(refname,
3578 got_worktree_get_head_ref_name(worktree)) == 0) {
3579 struct got_object_id *id = NULL;
3581 err = got_ref_resolve(&id, repo, ref);
3584 if (got_object_id_cmp(id,
3585 got_worktree_get_base_commit_id(worktree)) == 0)
3592 if (strncmp(refname, "refs/heads/", 11) == 0)
3594 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3597 refstr = got_ref_to_str(ref);
3599 return got_error_from_errno("got_ref_to_str");
3601 printf("%s%s: %s\n", marker, refname, refstr);
3606 static const struct got_error *
3607 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3609 const char *refname;
3611 if (worktree == NULL)
3612 return got_error(GOT_ERR_NOT_WORKTREE);
3614 refname = got_worktree_get_head_ref_name(worktree);
3616 if (strncmp(refname, "refs/heads/", 11) == 0)
3618 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3621 printf("%s\n", refname);
3626 static const struct got_error *
3627 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3629 static const struct got_error *err = NULL;
3630 struct got_reflist_head refs;
3631 struct got_reflist_entry *re;
3632 struct got_reference *temp_ref = NULL;
3633 int rebase_in_progress, histedit_in_progress;
3635 SIMPLEQ_INIT(&refs);
3638 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3643 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3648 if (rebase_in_progress || histedit_in_progress) {
3649 err = got_ref_open(&temp_ref, repo,
3650 got_worktree_get_head_ref_name(worktree), 0);
3653 list_branch(repo, worktree, temp_ref);
3654 got_ref_close(temp_ref);
3658 err = got_ref_list(&refs, repo, "refs/heads",
3659 got_ref_cmp_by_name, NULL);
3663 SIMPLEQ_FOREACH(re, &refs, entry)
3664 list_branch(repo, worktree, re->ref);
3666 got_ref_list_free(&refs);
3670 static const struct got_error *
3671 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3672 const char *branch_name)
3674 const struct got_error *err = NULL;
3675 struct got_reference *ref = NULL;
3678 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3679 return got_error_from_errno("asprintf");
3681 err = got_ref_open(&ref, repo, refname, 0);
3686 strcmp(got_worktree_get_head_ref_name(worktree),
3687 got_ref_get_name(ref)) == 0) {
3688 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3689 "will not delete this work tree's current branch");
3693 err = got_ref_delete(ref, repo);
3701 static const struct got_error *
3702 add_branch(struct got_repository *repo, const char *branch_name,
3703 struct got_object_id *base_commit_id)
3705 const struct got_error *err = NULL;
3706 struct got_reference *ref = NULL;
3707 char *base_refname = NULL, *refname = NULL;
3710 * Don't let the user create a branch name with a leading '-'.
3711 * While technically a valid reference name, this case is usually
3712 * an unintended typo.
3714 if (branch_name[0] == '-')
3715 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3717 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3718 err = got_error_from_errno("asprintf");
3722 err = got_ref_open(&ref, repo, refname, 0);
3724 err = got_error(GOT_ERR_BRANCH_EXISTS);
3726 } else if (err->code != GOT_ERR_NOT_REF)
3729 err = got_ref_alloc(&ref, refname, base_commit_id);
3733 err = got_ref_write(ref, repo);
3742 static const struct got_error *
3743 cmd_branch(int argc, char *argv[])
3745 const struct got_error *error = NULL;
3746 struct got_repository *repo = NULL;
3747 struct got_worktree *worktree = NULL;
3748 char *cwd = NULL, *repo_path = NULL;
3749 int ch, do_list = 0, do_show = 0, do_update = 1;
3750 const char *delref = NULL, *commit_id_arg = NULL;
3751 struct got_reference *ref = NULL;
3752 struct got_pathlist_head paths;
3753 struct got_pathlist_entry *pe;
3754 struct got_object_id *commit_id = NULL;
3755 char *commit_id_str = NULL;
3759 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3762 commit_id_arg = optarg;
3768 repo_path = realpath(optarg, NULL);
3769 if (repo_path == NULL)
3770 return got_error_from_errno2("realpath",
3772 got_path_strip_trailing_slashes(repo_path);
3786 if (do_list && delref)
3787 errx(1, "-l and -d options are mutually exclusive\n");
3792 if (!do_list && !delref && argc == 0)
3795 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3796 errx(1, "-c option can only be used when creating a branch");
3798 if (do_list || delref) {
3801 } else if (!do_show && argc != 1)
3805 if (do_list || do_show) {
3806 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3810 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3811 "sendfd unveil", NULL) == -1)
3815 cwd = getcwd(NULL, 0);
3817 error = got_error_from_errno("getcwd");
3821 if (repo_path == NULL) {
3822 error = got_worktree_open(&worktree, cwd);
3823 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3829 strdup(got_worktree_get_repo_path(worktree));
3830 if (repo_path == NULL)
3831 error = got_error_from_errno("strdup");
3835 repo_path = strdup(cwd);
3836 if (repo_path == NULL) {
3837 error = got_error_from_errno("strdup");
3843 error = got_repo_open(&repo, repo_path, NULL);
3847 error = apply_unveil(got_repo_get_path(repo), do_list,
3848 worktree ? got_worktree_get_root_path(worktree) : NULL);
3853 error = show_current_branch(repo, worktree);
3855 error = list_branches(repo, worktree);
3857 error = delete_branch(repo, worktree, delref);
3859 if (commit_id_arg == NULL)
3860 commit_id_arg = worktree ?
3861 got_worktree_get_head_ref_name(worktree) :
3863 error = got_repo_match_object_id(&commit_id, NULL,
3864 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3867 error = add_branch(repo, argv[0], commit_id);
3870 if (worktree && do_update) {
3871 int did_something = 0;
3872 char *branch_refname = NULL;
3874 error = got_object_id_str(&commit_id_str, commit_id);
3877 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3881 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3883 error = got_error_from_errno("asprintf");
3886 error = got_ref_open(&ref, repo, branch_refname, 0);
3887 free(branch_refname);
3890 error = switch_head_ref(ref, commit_id, worktree,
3894 error = got_worktree_set_base_commit_id(worktree, repo,
3898 error = got_worktree_checkout_files(worktree, &paths,
3899 repo, update_progress, &did_something,
3900 check_cancelled, NULL);
3904 printf("Updated to commit %s\n", commit_id_str);
3911 got_repo_close(repo);
3913 got_worktree_close(worktree);
3917 free(commit_id_str);
3918 TAILQ_FOREACH(pe, &paths, entry)
3919 free((char *)pe->path);
3920 got_pathlist_free(&paths);
3929 "usage: %s tag [-c commit] [-r repository] [-l] "
3930 "[-m message] name\n", getprogname());
3935 static const struct got_error *
3936 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3938 const struct got_error *err = NULL;
3939 struct got_reflist_entry *re, *se, *new;
3940 struct got_object_id *re_id, *se_id;
3941 struct got_tag_object *re_tag, *se_tag;
3942 time_t re_time, se_time;
3944 SIMPLEQ_FOREACH(re, tags, entry) {
3945 se = SIMPLEQ_FIRST(sorted);
3947 err = got_reflist_entry_dup(&new, re);
3950 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3953 err = got_ref_resolve(&re_id, repo, re->ref);
3956 err = got_object_open_as_tag(&re_tag, repo, re_id);
3960 re_time = got_object_tag_get_tagger_time(re_tag);
3961 got_object_tag_close(re_tag);
3965 err = got_ref_resolve(&se_id, repo, re->ref);
3968 err = got_object_open_as_tag(&se_tag, repo, se_id);
3972 se_time = got_object_tag_get_tagger_time(se_tag);
3973 got_object_tag_close(se_tag);
3975 if (se_time > re_time) {
3976 err = got_reflist_entry_dup(&new, re);
3979 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3982 se = SIMPLEQ_NEXT(se, entry);
3991 static const struct got_error *
3992 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3994 static const struct got_error *err = NULL;
3995 struct got_reflist_head refs;
3996 struct got_reflist_entry *re;
3998 SIMPLEQ_INIT(&refs);
4000 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4004 SIMPLEQ_FOREACH(re, &refs, entry) {
4005 const char *refname;
4006 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4010 struct got_object_id *id;
4011 struct got_tag_object *tag;
4012 struct got_commit_object *commit = NULL;
4014 refname = got_ref_get_name(re->ref);
4015 if (strncmp(refname, "refs/tags/", 10) != 0)
4018 refstr = got_ref_to_str(re->ref);
4019 if (refstr == NULL) {
4020 err = got_error_from_errno("got_ref_to_str");
4023 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4026 err = got_ref_resolve(&id, repo, re->ref);
4029 err = got_object_open_as_tag(&tag, repo, id);
4031 if (err->code != GOT_ERR_OBJ_TYPE) {
4035 /* "lightweight" tag */
4036 err = got_object_open_as_commit(&commit, repo, id);
4041 tagger = got_object_commit_get_committer(commit);
4043 got_object_commit_get_committer_time(commit);
4044 err = got_object_id_str(&id_str, id);
4050 tagger = got_object_tag_get_tagger(tag);
4051 tagger_time = got_object_tag_get_tagger_time(tag);
4052 err = got_object_id_str(&id_str,
4053 got_object_tag_get_object_id(tag));
4057 printf("from: %s\n", tagger);
4058 datestr = get_datestr(&tagger_time, datebuf);
4060 printf("date: %s UTC\n", datestr);
4062 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4064 switch (got_object_tag_get_object_type(tag)) {
4065 case GOT_OBJ_TYPE_BLOB:
4066 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4069 case GOT_OBJ_TYPE_TREE:
4070 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4073 case GOT_OBJ_TYPE_COMMIT:
4074 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4077 case GOT_OBJ_TYPE_TAG:
4078 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4087 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4090 got_object_commit_close(commit);
4092 tagmsg0 = strdup(got_object_tag_get_message(tag));
4093 got_object_tag_close(tag);
4094 if (tagmsg0 == NULL) {
4095 err = got_error_from_errno("strdup");
4102 line = strsep(&tagmsg, "\n");
4104 printf(" %s\n", line);
4109 got_ref_list_free(&refs);
4113 static const struct got_error *
4114 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4115 const char *tag_name, const char *repo_path)
4117 const struct got_error *err = NULL;
4118 char *template = NULL, *initial_content = NULL;
4119 char *editor = NULL;
4122 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4123 err = got_error_from_errno("asprintf");
4127 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4128 commit_id_str, tag_name) == -1) {
4129 err = got_error_from_errno("asprintf");
4133 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4137 dprintf(fd, initial_content);
4140 err = get_editor(&editor);
4143 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4145 free(initial_content);
4149 /* Editor is done; we can now apply unveil(2) */
4151 err = apply_unveil(repo_path, 0, NULL);
4160 static const struct got_error *
4161 add_tag(struct got_repository *repo, const char *tag_name,
4162 const char *commit_arg, const char *tagmsg_arg)
4164 const struct got_error *err = NULL;
4165 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4166 char *label = NULL, *commit_id_str = NULL;
4167 struct got_reference *ref = NULL;
4168 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4169 char *tagmsg_path = NULL, *tag_id_str = NULL;
4170 int preserve_tagmsg = 0;
4173 * Don't let the user create a tag name with a leading '-'.
4174 * While technically a valid reference name, this case is usually
4175 * an unintended typo.
4177 if (tag_name[0] == '-')
4178 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4180 err = get_author(&tagger, repo);
4184 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4185 GOT_OBJ_TYPE_COMMIT, 1, repo);
4189 err = got_object_id_str(&commit_id_str, commit_id);
4193 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4194 refname = strdup(tag_name);
4195 if (refname == NULL) {
4196 err = got_error_from_errno("strdup");
4200 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4201 err = got_error_from_errno("asprintf");
4205 err = got_ref_open(&ref, repo, refname, 0);
4207 err = got_error(GOT_ERR_TAG_EXISTS);
4209 } else if (err->code != GOT_ERR_NOT_REF)
4212 if (tagmsg_arg == NULL) {
4213 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4214 tag_name, got_repo_get_path(repo));
4216 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4217 tagmsg_path != NULL)
4218 preserve_tagmsg = 1;
4223 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4224 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4227 preserve_tagmsg = 1;
4231 err = got_ref_alloc(&ref, refname, tag_id);
4234 preserve_tagmsg = 1;
4238 err = got_ref_write(ref, repo);
4241 preserve_tagmsg = 1;
4245 err = got_object_id_str(&tag_id_str, tag_id);
4248 preserve_tagmsg = 1;
4251 printf("Created tag %s\n", tag_id_str);
4253 if (preserve_tagmsg) {
4254 fprintf(stderr, "%s: tag message preserved in %s\n",
4255 getprogname(), tagmsg_path);
4256 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4257 err = got_error_from_errno2("unlink", tagmsg_path);
4262 free(commit_id_str);
4270 static const struct got_error *
4271 cmd_tag(int argc, char *argv[])
4273 const struct got_error *error = NULL;
4274 struct got_repository *repo = NULL;
4275 struct got_worktree *worktree = NULL;
4276 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4277 char *gitconfig_path = NULL;
4278 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4279 int ch, do_list = 0;
4281 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4284 commit_id_arg = optarg;
4290 repo_path = realpath(optarg, NULL);
4291 if (repo_path == NULL)
4292 return got_error_from_errno2("realpath",
4294 got_path_strip_trailing_slashes(repo_path);
4309 if (commit_id_arg != NULL)
4310 errx(1, "-c option can only be used when creating a tag");
4312 errx(1, "-l and -m options are mutually exclusive");
4315 } else if (argc != 1)
4322 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4326 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4327 "sendfd unveil", NULL) == -1)
4331 cwd = getcwd(NULL, 0);
4333 error = got_error_from_errno("getcwd");
4337 if (repo_path == NULL) {
4338 error = got_worktree_open(&worktree, cwd);
4339 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4345 strdup(got_worktree_get_repo_path(worktree));
4346 if (repo_path == NULL)
4347 error = got_error_from_errno("strdup");
4351 repo_path = strdup(cwd);
4352 if (repo_path == NULL) {
4353 error = got_error_from_errno("strdup");
4360 error = got_repo_open(&repo, repo_path, NULL);
4363 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4366 error = list_tags(repo, worktree);
4368 error = get_gitconfig_path(&gitconfig_path);
4371 error = got_repo_open(&repo, repo_path, gitconfig_path);
4376 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4381 if (commit_id_arg == NULL) {
4382 struct got_reference *head_ref;
4383 struct got_object_id *commit_id;
4384 error = got_ref_open(&head_ref, repo,
4385 worktree ? got_worktree_get_head_ref_name(worktree)
4389 error = got_ref_resolve(&commit_id, repo, head_ref);
4390 got_ref_close(head_ref);
4393 error = got_object_id_str(&commit_id_str, commit_id);
4399 error = add_tag(repo, tag_name,
4400 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4404 got_repo_close(repo);
4406 got_worktree_close(worktree);
4409 free(gitconfig_path);
4410 free(commit_id_str);
4417 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4422 static const struct got_error *
4423 add_progress(void *arg, unsigned char status, const char *path)
4425 while (path[0] == '/')
4427 printf("%c %s\n", status, path);
4431 static const struct got_error *
4432 cmd_add(int argc, char *argv[])
4434 const struct got_error *error = NULL;
4435 struct got_repository *repo = NULL;
4436 struct got_worktree *worktree = NULL;
4438 struct got_pathlist_head paths;
4439 struct got_pathlist_entry *pe;
4440 int ch, can_recurse = 0, no_ignores = 0;
4444 while ((ch = getopt(argc, argv, "IR")) != -1) {
4462 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4469 cwd = getcwd(NULL, 0);
4471 error = got_error_from_errno("getcwd");
4475 error = got_worktree_open(&worktree, cwd);
4479 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4484 error = apply_unveil(got_repo_get_path(repo), 1,
4485 got_worktree_get_root_path(worktree));
4489 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4493 if (!can_recurse && no_ignores) {
4494 error = got_error_msg(GOT_ERR_BAD_PATH,
4495 "disregarding ignores requires -R option");
4503 TAILQ_FOREACH(pe, &paths, entry) {
4504 if (asprintf(&ondisk_path, "%s/%s",
4505 got_worktree_get_root_path(worktree),
4507 error = got_error_from_errno("asprintf");
4510 if (lstat(ondisk_path, &sb) == -1) {
4511 if (errno == ENOENT) {
4515 error = got_error_from_errno2("lstat",
4521 if (S_ISDIR(sb.st_mode)) {
4522 error = got_error_msg(GOT_ERR_BAD_PATH,
4523 "adding directories requires -R option");
4529 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4530 NULL, repo, no_ignores);
4533 got_repo_close(repo);
4535 got_worktree_close(worktree);
4536 TAILQ_FOREACH(pe, &paths, entry)
4537 free((char *)pe->path);
4538 got_pathlist_free(&paths);
4546 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4551 static const struct got_error *
4552 print_remove_status(void *arg, unsigned char status,
4553 unsigned char staged_status, const char *path)
4555 while (path[0] == '/')
4557 if (status == GOT_STATUS_NONEXISTENT)
4559 if (status == staged_status && (status == GOT_STATUS_DELETE))
4560 status = GOT_STATUS_NO_CHANGE;
4561 printf("%c%c %s\n", status, staged_status, path);
4565 static const struct got_error *
4566 cmd_remove(int argc, char *argv[])
4568 const struct got_error *error = NULL;
4569 struct got_worktree *worktree = NULL;
4570 struct got_repository *repo = NULL;
4572 struct got_pathlist_head paths;
4573 struct got_pathlist_entry *pe;
4574 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4578 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4581 delete_local_mods = 1;
4599 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4606 cwd = getcwd(NULL, 0);
4608 error = got_error_from_errno("getcwd");
4611 error = got_worktree_open(&worktree, cwd);
4615 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4620 error = apply_unveil(got_repo_get_path(repo), 1,
4621 got_worktree_get_root_path(worktree));
4625 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4632 TAILQ_FOREACH(pe, &paths, entry) {
4633 if (asprintf(&ondisk_path, "%s/%s",
4634 got_worktree_get_root_path(worktree),
4636 error = got_error_from_errno("asprintf");
4639 if (lstat(ondisk_path, &sb) == -1) {
4640 if (errno == ENOENT) {
4644 error = got_error_from_errno2("lstat",
4650 if (S_ISDIR(sb.st_mode)) {
4651 error = got_error_msg(GOT_ERR_BAD_PATH,
4652 "removing directories requires -R option");
4658 error = got_worktree_schedule_delete(worktree, &paths,
4659 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4662 got_repo_close(repo);
4664 got_worktree_close(worktree);
4665 TAILQ_FOREACH(pe, &paths, entry)
4666 free((char *)pe->path);
4667 got_pathlist_free(&paths);
4675 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4676 "path ...\n", getprogname());
4680 static const struct got_error *
4681 revert_progress(void *arg, unsigned char status, const char *path)
4683 if (status == GOT_STATUS_UNVERSIONED)
4686 while (path[0] == '/')
4688 printf("%c %s\n", status, path);
4692 struct choose_patch_arg {
4693 FILE *patch_script_file;
4697 static const struct got_error *
4698 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4699 int nchanges, const char *action)
4702 size_t linesize = 0;
4706 case GOT_STATUS_ADD:
4707 printf("A %s\n%s this addition? [y/n] ", path, action);
4709 case GOT_STATUS_DELETE:
4710 printf("D %s\n%s this deletion? [y/n] ", path, action);
4712 case GOT_STATUS_MODIFY:
4713 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4714 return got_error_from_errno("fseek");
4715 printf(GOT_COMMIT_SEP_STR);
4716 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4718 if (ferror(patch_file))
4719 return got_error_from_errno("getline");
4720 printf(GOT_COMMIT_SEP_STR);
4721 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4722 path, n, nchanges, action);
4725 return got_error_path(path, GOT_ERR_FILE_STATUS);
4731 static const struct got_error *
4732 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4733 FILE *patch_file, int n, int nchanges)
4735 const struct got_error *err = NULL;
4737 size_t linesize = 0;
4740 struct choose_patch_arg *a = arg;
4742 *choice = GOT_PATCH_CHOICE_NONE;
4744 if (a->patch_script_file) {
4746 err = show_change(status, path, patch_file, n, nchanges,
4750 linelen = getline(&line, &linesize, a->patch_script_file);
4751 if (linelen == -1) {
4752 if (ferror(a->patch_script_file))
4753 return got_error_from_errno("getline");
4756 nl = strchr(line, '\n');
4759 if (strcmp(line, "y") == 0) {
4760 *choice = GOT_PATCH_CHOICE_YES;
4762 } else if (strcmp(line, "n") == 0) {
4763 *choice = GOT_PATCH_CHOICE_NO;
4765 } else if (strcmp(line, "q") == 0 &&
4766 status == GOT_STATUS_MODIFY) {
4767 *choice = GOT_PATCH_CHOICE_QUIT;
4770 printf("invalid response '%s'\n", line);
4775 while (resp != 'y' && resp != 'n' && resp != 'q') {
4776 err = show_change(status, path, patch_file, n, nchanges,
4783 if (status == GOT_STATUS_MODIFY) {
4784 if (resp != 'y' && resp != 'n' && resp != 'q') {
4785 printf("invalid response '%c'\n", resp);
4788 } else if (resp != 'y' && resp != 'n') {
4789 printf("invalid response '%c'\n", resp);
4795 *choice = GOT_PATCH_CHOICE_YES;
4796 else if (resp == 'n')
4797 *choice = GOT_PATCH_CHOICE_NO;
4798 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4799 *choice = GOT_PATCH_CHOICE_QUIT;
4805 static const struct got_error *
4806 cmd_revert(int argc, char *argv[])
4808 const struct got_error *error = NULL;
4809 struct got_worktree *worktree = NULL;
4810 struct got_repository *repo = NULL;
4811 char *cwd = NULL, *path = NULL;
4812 struct got_pathlist_head paths;
4813 struct got_pathlist_entry *pe;
4814 int ch, can_recurse = 0, pflag = 0;
4815 FILE *patch_script_file = NULL;
4816 const char *patch_script_path = NULL;
4817 struct choose_patch_arg cpa;
4821 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4827 patch_script_path = optarg;
4842 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4843 "unveil", NULL) == -1)
4848 if (patch_script_path && !pflag)
4849 errx(1, "-F option can only be used together with -p option");
4851 cwd = getcwd(NULL, 0);
4853 error = got_error_from_errno("getcwd");
4856 error = got_worktree_open(&worktree, cwd);
4860 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4865 if (patch_script_path) {
4866 patch_script_file = fopen(patch_script_path, "r");
4867 if (patch_script_file == NULL) {
4868 error = got_error_from_errno2("fopen",
4873 error = apply_unveil(got_repo_get_path(repo), 1,
4874 got_worktree_get_root_path(worktree));
4878 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4885 TAILQ_FOREACH(pe, &paths, entry) {
4886 if (asprintf(&ondisk_path, "%s/%s",
4887 got_worktree_get_root_path(worktree),
4889 error = got_error_from_errno("asprintf");
4892 if (lstat(ondisk_path, &sb) == -1) {
4893 if (errno == ENOENT) {
4897 error = got_error_from_errno2("lstat",
4903 if (S_ISDIR(sb.st_mode)) {
4904 error = got_error_msg(GOT_ERR_BAD_PATH,
4905 "reverting directories requires -R option");
4911 cpa.patch_script_file = patch_script_file;
4912 cpa.action = "revert";
4913 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4914 pflag ? choose_patch : NULL, &cpa, repo);
4916 if (patch_script_file && fclose(patch_script_file) == EOF &&
4918 error = got_error_from_errno2("fclose", patch_script_path);
4920 got_repo_close(repo);
4922 got_worktree_close(worktree);
4931 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4936 struct collect_commit_logmsg_arg {
4937 const char *cmdline_log;
4939 const char *worktree_path;
4940 const char *branch_name;
4941 const char *repo_path;
4946 static const struct got_error *
4947 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4950 char *initial_content = NULL;
4951 struct got_pathlist_entry *pe;
4952 const struct got_error *err = NULL;
4953 char *template = NULL;
4954 struct collect_commit_logmsg_arg *a = arg;
4958 /* if a message was specified on the command line, just use it */
4959 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4960 len = strlen(a->cmdline_log) + 1;
4961 *logmsg = malloc(len + 1);
4962 if (*logmsg == NULL)
4963 return got_error_from_errno("malloc");
4964 strlcpy(*logmsg, a->cmdline_log, len);
4968 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4969 return got_error_from_errno("asprintf");
4971 if (asprintf(&initial_content,
4972 "\n# changes to be committed on branch %s:\n",
4973 a->branch_name) == -1)
4974 return got_error_from_errno("asprintf");
4976 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4980 dprintf(fd, initial_content);
4982 TAILQ_FOREACH(pe, commitable_paths, entry) {
4983 struct got_commitable *ct = pe->data;
4984 dprintf(fd, "# %c %s\n",
4985 got_commitable_get_status(ct),
4986 got_commitable_get_path(ct));
4990 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4992 free(initial_content);
4995 /* Editor is done; we can now apply unveil(2) */
4997 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5006 static const struct got_error *
5007 cmd_commit(int argc, char *argv[])
5009 const struct got_error *error = NULL;
5010 struct got_worktree *worktree = NULL;
5011 struct got_repository *repo = NULL;
5012 char *cwd = NULL, *id_str = NULL;
5013 struct got_object_id *id = NULL;
5014 const char *logmsg = NULL;
5015 struct collect_commit_logmsg_arg cl_arg;
5016 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5017 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5018 struct got_pathlist_head paths;
5021 cl_arg.logmsg_path = NULL;
5023 while ((ch = getopt(argc, argv, "m:")) != -1) {
5038 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5039 "unveil", NULL) == -1)
5042 cwd = getcwd(NULL, 0);
5044 error = got_error_from_errno("getcwd");
5047 error = got_worktree_open(&worktree, cwd);
5051 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5054 if (rebase_in_progress) {
5055 error = got_error(GOT_ERR_REBASING);
5059 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5064 error = get_gitconfig_path(&gitconfig_path);
5067 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5072 error = get_author(&author, repo);
5077 * unveil(2) traverses exec(2); if an editor is used we have
5078 * to apply unveil after the log message has been written.
5080 if (logmsg == NULL || strlen(logmsg) == 0)
5081 error = get_editor(&editor);
5083 error = apply_unveil(got_repo_get_path(repo), 0,
5084 got_worktree_get_root_path(worktree));
5088 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5092 cl_arg.editor = editor;
5093 cl_arg.cmdline_log = logmsg;
5094 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5095 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5096 if (!histedit_in_progress) {
5097 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5098 error = got_error(GOT_ERR_COMMIT_BRANCH);
5101 cl_arg.branch_name += 11;
5103 cl_arg.repo_path = got_repo_get_path(repo);
5104 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5105 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5107 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5108 cl_arg.logmsg_path != NULL)
5109 preserve_logmsg = 1;
5113 error = got_object_id_str(&id_str, id);
5116 printf("Created commit %s\n", id_str);
5118 if (preserve_logmsg) {
5119 fprintf(stderr, "%s: log message preserved in %s\n",
5120 getprogname(), cl_arg.logmsg_path);
5121 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5123 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5124 free(cl_arg.logmsg_path);
5126 got_repo_close(repo);
5128 got_worktree_close(worktree);
5131 free(gitconfig_path);
5138 usage_cherrypick(void)
5140 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5144 static const struct got_error *
5145 cmd_cherrypick(int argc, char *argv[])
5147 const struct got_error *error = NULL;
5148 struct got_worktree *worktree = NULL;
5149 struct got_repository *repo = NULL;
5150 char *cwd = NULL, *commit_id_str = NULL;
5151 struct got_object_id *commit_id = NULL;
5152 struct got_commit_object *commit = NULL;
5153 struct got_object_qid *pid;
5154 struct got_reference *head_ref = NULL;
5155 int ch, did_something = 0;
5157 while ((ch = getopt(argc, argv, "")) != -1) {
5169 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5170 "unveil", NULL) == -1)
5176 cwd = getcwd(NULL, 0);
5178 error = got_error_from_errno("getcwd");
5181 error = got_worktree_open(&worktree, cwd);
5185 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5190 error = apply_unveil(got_repo_get_path(repo), 0,
5191 got_worktree_get_root_path(worktree));
5195 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5196 GOT_OBJ_TYPE_COMMIT, repo);
5197 if (error != NULL) {
5198 struct got_reference *ref;
5199 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5201 error = got_ref_open(&ref, repo, argv[0], 0);
5204 error = got_ref_resolve(&commit_id, repo, ref);
5209 error = got_object_id_str(&commit_id_str, commit_id);
5213 error = got_ref_open(&head_ref, repo,
5214 got_worktree_get_head_ref_name(worktree), 0);
5218 error = check_same_branch(commit_id, head_ref, NULL, repo);
5220 if (error->code != GOT_ERR_ANCESTRY)
5224 error = got_error(GOT_ERR_SAME_BRANCH);
5228 error = got_object_open_as_commit(&commit, repo, commit_id);
5231 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5232 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5233 commit_id, repo, update_progress, &did_something, check_cancelled,
5239 printf("Merged commit %s\n", commit_id_str);
5242 got_object_commit_close(commit);
5243 free(commit_id_str);
5245 got_ref_close(head_ref);
5247 got_worktree_close(worktree);
5249 got_repo_close(repo);
5256 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5260 static const struct got_error *
5261 cmd_backout(int argc, char *argv[])
5263 const struct got_error *error = NULL;
5264 struct got_worktree *worktree = NULL;
5265 struct got_repository *repo = NULL;
5266 char *cwd = NULL, *commit_id_str = NULL;
5267 struct got_object_id *commit_id = NULL;
5268 struct got_commit_object *commit = NULL;
5269 struct got_object_qid *pid;
5270 struct got_reference *head_ref = NULL;
5271 int ch, did_something = 0;
5273 while ((ch = getopt(argc, argv, "")) != -1) {
5285 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5286 "unveil", NULL) == -1)
5292 cwd = getcwd(NULL, 0);
5294 error = got_error_from_errno("getcwd");
5297 error = got_worktree_open(&worktree, cwd);
5301 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5306 error = apply_unveil(got_repo_get_path(repo), 0,
5307 got_worktree_get_root_path(worktree));
5311 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5312 GOT_OBJ_TYPE_COMMIT, repo);
5313 if (error != NULL) {
5314 struct got_reference *ref;
5315 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5317 error = got_ref_open(&ref, repo, argv[0], 0);
5320 error = got_ref_resolve(&commit_id, repo, ref);
5325 error = got_object_id_str(&commit_id_str, commit_id);
5329 error = got_ref_open(&head_ref, repo,
5330 got_worktree_get_head_ref_name(worktree), 0);
5334 error = check_same_branch(commit_id, head_ref, NULL, repo);
5338 error = got_object_open_as_commit(&commit, repo, commit_id);
5341 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5343 error = got_error(GOT_ERR_ROOT_COMMIT);
5347 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5348 update_progress, &did_something, check_cancelled, NULL);
5353 printf("Backed out commit %s\n", commit_id_str);
5356 got_object_commit_close(commit);
5357 free(commit_id_str);
5359 got_ref_close(head_ref);
5361 got_worktree_close(worktree);
5363 got_repo_close(repo);
5370 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5376 trim_logmsg(char *logmsg, int limit)
5381 len = strlen(logmsg);
5385 nl = strchr(logmsg, '\n');
5390 static const struct got_error *
5391 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5393 const struct got_error *err;
5394 char *logmsg0 = NULL;
5397 err = got_object_commit_get_logmsg(&logmsg0, commit);
5402 while (isspace((unsigned char)s[0]))
5405 *logmsg = strdup(s);
5406 if (*logmsg == NULL) {
5407 err = got_error_from_errno("strdup");
5411 trim_logmsg(*logmsg, limit);
5417 static const struct got_error *
5418 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5420 const struct got_error *err;
5421 struct got_commit_object *commit = NULL;
5422 char *id_str = NULL, *logmsg = NULL;
5424 err = got_object_open_as_commit(&commit, repo, id);
5428 err = got_object_id_str(&id_str, id);
5434 err = get_short_logmsg(&logmsg, 42, commit);
5438 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5441 got_object_commit_close(commit);
5446 static const struct got_error *
5447 show_rebase_progress(struct got_commit_object *commit,
5448 struct got_object_id *old_id, struct got_object_id *new_id)
5450 const struct got_error *err;
5451 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5453 err = got_object_id_str(&old_id_str, old_id);
5458 err = got_object_id_str(&new_id_str, new_id);
5463 old_id_str[12] = '\0';
5465 new_id_str[12] = '\0';
5467 err = get_short_logmsg(&logmsg, 42, commit);
5471 printf("%s -> %s: %s\n", old_id_str,
5472 new_id_str ? new_id_str : "no-op change", logmsg);
5480 static const struct got_error *
5481 rebase_progress(void *arg, unsigned char status, const char *path)
5483 unsigned char *rebase_status = arg;
5485 while (path[0] == '/')
5487 printf("%c %s\n", status, path);
5489 if (*rebase_status == GOT_STATUS_CONFLICT)
5491 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5492 *rebase_status = status;
5496 static const struct got_error *
5497 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5498 struct got_reference *branch, struct got_reference *new_base_branch,
5499 struct got_reference *tmp_branch, struct got_repository *repo)
5501 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5502 return got_worktree_rebase_complete(worktree, fileindex,
5503 new_base_branch, tmp_branch, branch, repo);
5506 static const struct got_error *
5507 rebase_commit(struct got_pathlist_head *merged_paths,
5508 struct got_worktree *worktree, struct got_fileindex *fileindex,
5509 struct got_reference *tmp_branch,
5510 struct got_object_id *commit_id, struct got_repository *repo)
5512 const struct got_error *error;
5513 struct got_commit_object *commit;
5514 struct got_object_id *new_commit_id;
5516 error = got_object_open_as_commit(&commit, repo, commit_id);
5520 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5521 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5523 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5525 error = show_rebase_progress(commit, commit_id, NULL);
5527 error = show_rebase_progress(commit, commit_id, new_commit_id);
5528 free(new_commit_id);
5531 got_object_commit_close(commit);
5535 struct check_path_prefix_arg {
5536 const char *path_prefix;
5541 static const struct got_error *
5542 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5543 struct got_blob_object *blob2, struct got_object_id *id1,
5544 struct got_object_id *id2, const char *path1, const char *path2,
5545 mode_t mode1, mode_t mode2, struct got_repository *repo)
5547 struct check_path_prefix_arg *a = arg;
5549 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5550 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5551 return got_error(a->errcode);
5556 static const struct got_error *
5557 check_path_prefix(struct got_object_id *parent_id,
5558 struct got_object_id *commit_id, const char *path_prefix,
5559 int errcode, struct got_repository *repo)
5561 const struct got_error *err;
5562 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5563 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5564 struct check_path_prefix_arg cpp_arg;
5566 if (got_path_is_root_dir(path_prefix))
5569 err = got_object_open_as_commit(&commit, repo, commit_id);
5573 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5577 err = got_object_open_as_tree(&tree1, repo,
5578 got_object_commit_get_tree_id(parent_commit));
5582 err = got_object_open_as_tree(&tree2, repo,
5583 got_object_commit_get_tree_id(commit));
5587 cpp_arg.path_prefix = path_prefix;
5588 while (cpp_arg.path_prefix[0] == '/')
5589 cpp_arg.path_prefix++;
5590 cpp_arg.len = strlen(cpp_arg.path_prefix);
5591 cpp_arg.errcode = errcode;
5592 err = got_diff_tree(tree1, tree2, "", "", repo,
5593 check_path_prefix_in_diff, &cpp_arg, 0);
5596 got_object_tree_close(tree1);
5598 got_object_tree_close(tree2);
5600 got_object_commit_close(commit);
5602 got_object_commit_close(parent_commit);
5606 static const struct got_error *
5607 collect_commits(struct got_object_id_queue *commits,
5608 struct got_object_id *initial_commit_id,
5609 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5610 const char *path_prefix, int path_prefix_errcode,
5611 struct got_repository *repo)
5613 const struct got_error *err = NULL;
5614 struct got_commit_graph *graph = NULL;
5615 struct got_object_id *parent_id = NULL;
5616 struct got_object_qid *qid;
5617 struct got_object_id *commit_id = initial_commit_id;
5619 err = got_commit_graph_open(&graph, "/", 1);
5623 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5624 check_cancelled, NULL);
5627 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5628 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5629 check_cancelled, NULL);
5631 if (err->code == GOT_ERR_ITER_COMPLETED) {
5632 err = got_error_msg(GOT_ERR_ANCESTRY,
5633 "ran out of commits to rebase before "
5634 "youngest common ancestor commit has "
5639 err = check_path_prefix(parent_id, commit_id,
5640 path_prefix, path_prefix_errcode, repo);
5644 err = got_object_qid_alloc(&qid, commit_id);
5647 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5648 commit_id = parent_id;
5652 got_commit_graph_close(graph);
5656 static const struct got_error *
5657 cmd_rebase(int argc, char *argv[])
5659 const struct got_error *error = NULL;
5660 struct got_worktree *worktree = NULL;
5661 struct got_repository *repo = NULL;
5662 struct got_fileindex *fileindex = NULL;
5664 struct got_reference *branch = NULL;
5665 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5666 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5667 struct got_object_id *resume_commit_id = NULL;
5668 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5669 struct got_commit_object *commit = NULL;
5670 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5671 int histedit_in_progress = 0;
5672 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5673 struct got_object_id_queue commits;
5674 struct got_pathlist_head merged_paths;
5675 const struct got_object_id_queue *parent_ids;
5676 struct got_object_qid *qid, *pid;
5678 SIMPLEQ_INIT(&commits);
5679 TAILQ_INIT(&merged_paths);
5681 while ((ch = getopt(argc, argv, "ac")) != -1) {
5687 continue_rebase = 1;
5699 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5700 "unveil", NULL) == -1)
5703 if (abort_rebase && continue_rebase)
5705 else if (abort_rebase || continue_rebase) {
5708 } else if (argc != 1)
5711 cwd = getcwd(NULL, 0);
5713 error = got_error_from_errno("getcwd");
5716 error = got_worktree_open(&worktree, cwd);
5720 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5725 error = apply_unveil(got_repo_get_path(repo), 0,
5726 got_worktree_get_root_path(worktree));
5730 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5734 if (histedit_in_progress) {
5735 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5739 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5745 if (!rebase_in_progress) {
5746 error = got_error(GOT_ERR_NOT_REBASING);
5749 error = got_worktree_rebase_continue(&resume_commit_id,
5750 &new_base_branch, &tmp_branch, &branch, &fileindex,
5754 printf("Switching work tree to %s\n",
5755 got_ref_get_symref_target(new_base_branch));
5756 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5757 new_base_branch, update_progress, &did_something);
5760 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5761 goto done; /* nothing else to do */
5764 if (continue_rebase) {
5765 if (!rebase_in_progress) {
5766 error = got_error(GOT_ERR_NOT_REBASING);
5769 error = got_worktree_rebase_continue(&resume_commit_id,
5770 &new_base_branch, &tmp_branch, &branch, &fileindex,
5775 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5776 resume_commit_id, repo);
5780 yca_id = got_object_id_dup(resume_commit_id);
5781 if (yca_id == NULL) {
5782 error = got_error_from_errno("got_object_id_dup");
5786 error = got_ref_open(&branch, repo, argv[0], 0);
5791 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5795 if (!continue_rebase) {
5796 struct got_object_id *base_commit_id;
5798 base_commit_id = got_worktree_get_base_commit_id(worktree);
5799 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5800 base_commit_id, branch_head_commit_id, repo,
5801 check_cancelled, NULL);
5804 if (yca_id == NULL) {
5805 error = got_error_msg(GOT_ERR_ANCESTRY,
5806 "specified branch shares no common ancestry "
5807 "with work tree's branch");
5811 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5813 if (error->code != GOT_ERR_ANCESTRY)
5817 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5818 "specified branch resolves to a commit which "
5819 "is already contained in work tree's branch");
5822 error = got_worktree_rebase_prepare(&new_base_branch,
5823 &tmp_branch, &fileindex, worktree, branch, repo);
5828 commit_id = branch_head_commit_id;
5829 error = got_object_open_as_commit(&commit, repo, commit_id);
5833 parent_ids = got_object_commit_get_parent_ids(commit);
5834 pid = SIMPLEQ_FIRST(parent_ids);
5836 if (!continue_rebase) {
5838 error = got_worktree_rebase_abort(worktree, fileindex,
5839 repo, new_base_branch, update_progress,
5843 printf("Rebase of %s aborted\n",
5844 got_ref_get_name(branch));
5846 error = got_error(GOT_ERR_EMPTY_REBASE);
5849 error = collect_commits(&commits, commit_id, pid->id,
5850 yca_id, got_worktree_get_path_prefix(worktree),
5851 GOT_ERR_REBASE_PATH, repo);
5852 got_object_commit_close(commit);
5857 if (SIMPLEQ_EMPTY(&commits)) {
5858 if (continue_rebase) {
5859 error = rebase_complete(worktree, fileindex,
5860 branch, new_base_branch, tmp_branch, repo);
5863 /* Fast-forward the reference of the branch. */
5864 struct got_object_id *new_head_commit_id;
5866 error = got_ref_resolve(&new_head_commit_id, repo,
5870 error = got_object_id_str(&id_str, new_head_commit_id);
5871 printf("Forwarding %s to commit %s\n",
5872 got_ref_get_name(branch), id_str);
5874 error = got_ref_change_ref(branch,
5875 new_head_commit_id);
5882 SIMPLEQ_FOREACH(qid, &commits, entry) {
5883 commit_id = qid->id;
5884 parent_id = pid ? pid->id : yca_id;
5887 error = got_worktree_rebase_merge_files(&merged_paths,
5888 worktree, fileindex, parent_id, commit_id, repo,
5889 rebase_progress, &rebase_status, check_cancelled, NULL);
5893 if (rebase_status == GOT_STATUS_CONFLICT) {
5894 error = show_rebase_merge_conflict(qid->id, repo);
5897 got_worktree_rebase_pathlist_free(&merged_paths);
5901 error = rebase_commit(&merged_paths, worktree, fileindex,
5902 tmp_branch, commit_id, repo);
5903 got_worktree_rebase_pathlist_free(&merged_paths);
5908 if (rebase_status == GOT_STATUS_CONFLICT) {
5909 error = got_worktree_rebase_postpone(worktree, fileindex);
5912 error = got_error_msg(GOT_ERR_CONFLICTS,
5913 "conflicts must be resolved before rebasing can continue");
5915 error = rebase_complete(worktree, fileindex, branch,
5916 new_base_branch, tmp_branch, repo);
5918 got_object_id_queue_free(&commits);
5919 free(branch_head_commit_id);
5920 free(resume_commit_id);
5923 got_object_commit_close(commit);
5925 got_ref_close(branch);
5926 if (new_base_branch)
5927 got_ref_close(new_base_branch);
5929 got_ref_close(tmp_branch);
5931 got_worktree_close(worktree);
5933 got_repo_close(repo);
5938 usage_histedit(void)
5940 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5945 #define GOT_HISTEDIT_PICK 'p'
5946 #define GOT_HISTEDIT_EDIT 'e'
5947 #define GOT_HISTEDIT_FOLD 'f'
5948 #define GOT_HISTEDIT_DROP 'd'
5949 #define GOT_HISTEDIT_MESG 'm'
5951 static struct got_histedit_cmd {
5955 } got_histedit_cmds[] = {
5956 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5957 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5958 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5960 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5961 { GOT_HISTEDIT_MESG, "mesg",
5962 "single-line log message for commit above (open editor if empty)" },
5965 struct got_histedit_list_entry {
5966 TAILQ_ENTRY(got_histedit_list_entry) entry;
5967 struct got_object_id *commit_id;
5968 const struct got_histedit_cmd *cmd;
5971 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5973 static const struct got_error *
5974 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5975 FILE *f, struct got_repository *repo)
5977 const struct got_error *err = NULL;
5978 char *logmsg = NULL, *id_str = NULL;
5979 struct got_commit_object *commit = NULL;
5982 err = got_object_open_as_commit(&commit, repo, commit_id);
5986 err = get_short_logmsg(&logmsg, 34, commit);
5990 err = got_object_id_str(&id_str, commit_id);
5994 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5996 err = got_ferror(f, GOT_ERR_IO);
5999 got_object_commit_close(commit);
6005 static const struct got_error *
6006 histedit_write_commit_list(struct got_object_id_queue *commits,
6007 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6009 const struct got_error *err = NULL;
6010 struct got_object_qid *qid;
6012 if (SIMPLEQ_EMPTY(commits))
6013 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6015 SIMPLEQ_FOREACH(qid, commits, entry) {
6016 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6020 if (edit_logmsg_only) {
6021 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6023 err = got_ferror(f, GOT_ERR_IO);
6032 static const struct got_error *
6033 write_cmd_list(FILE *f, const char *branch_name,
6034 struct got_object_id_queue *commits)
6036 const struct got_error *err = NULL;
6039 struct got_object_qid *qid;
6041 qid = SIMPLEQ_FIRST(commits);
6042 err = got_object_id_str(&id_str, qid->id);
6047 "# Editing the history of branch '%s' starting at\n"
6049 "# Commits will be processed in order from top to "
6050 "bottom of this file.\n", branch_name, id_str);
6052 err = got_ferror(f, GOT_ERR_IO);
6056 n = fprintf(f, "# Available histedit commands:\n");
6058 err = got_ferror(f, GOT_ERR_IO);
6062 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6063 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6064 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6067 err = got_ferror(f, GOT_ERR_IO);
6076 static const struct got_error *
6077 histedit_syntax_error(int lineno)
6079 static char msg[42];
6082 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6084 if (ret == -1 || ret >= sizeof(msg))
6085 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6087 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6090 static const struct got_error *
6091 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6092 char *logmsg, struct got_repository *repo)
6094 const struct got_error *err;
6095 struct got_commit_object *folded_commit = NULL;
6096 char *id_str, *folded_logmsg = NULL;
6098 err = got_object_id_str(&id_str, hle->commit_id);
6102 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6106 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6109 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6110 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6111 folded_logmsg) == -1) {
6112 err = got_error_from_errno("asprintf");
6116 got_object_commit_close(folded_commit);
6118 free(folded_logmsg);
6122 static struct got_histedit_list_entry *
6123 get_folded_commits(struct got_histedit_list_entry *hle)
6125 struct got_histedit_list_entry *prev, *folded = NULL;
6127 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6128 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6129 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6130 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6132 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6138 static const struct got_error *
6139 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6140 struct got_repository *repo)
6142 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6143 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6144 const struct got_error *err = NULL;
6145 struct got_commit_object *commit = NULL;
6147 struct got_histedit_list_entry *folded = NULL;
6149 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6153 folded = get_folded_commits(hle);
6155 while (folded != hle) {
6156 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6157 folded = TAILQ_NEXT(folded, entry);
6160 err = append_folded_commit_msg(&new_msg, folded,
6166 folded = TAILQ_NEXT(folded, entry);
6170 err = got_object_id_str(&id_str, hle->commit_id);
6173 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6176 if (asprintf(&new_msg,
6177 "%s\n# original log message of commit %s: %s",
6178 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6179 err = got_error_from_errno("asprintf");
6185 err = got_object_id_str(&id_str, hle->commit_id);
6189 err = got_opentemp_named_fd(&logmsg_path, &fd,
6190 GOT_TMPDIR_STR "/got-logmsg");
6194 dprintf(fd, logmsg);
6197 err = get_editor(&editor);
6201 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6203 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6205 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6208 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6209 err = got_error_from_errno2("unlink", logmsg_path);
6215 got_object_commit_close(commit);
6219 static const struct got_error *
6220 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6221 FILE *f, struct got_repository *repo)
6223 const struct got_error *err = NULL;
6224 char *line = NULL, *p, *end;
6228 const struct got_histedit_cmd *cmd;
6229 struct got_object_id *commit_id = NULL;
6230 struct got_histedit_list_entry *hle = NULL;
6233 len = getline(&line, &size, f);
6235 const struct got_error *getline_err;
6238 getline_err = got_error_from_errno("getline");
6239 err = got_ferror(f, getline_err->code);
6244 while (isspace((unsigned char)p[0]))
6246 if (p[0] == '#' || p[0] == '\0') {
6252 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6253 cmd = &got_histedit_cmds[i];
6254 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6255 isspace((unsigned char)p[strlen(cmd->name)])) {
6256 p += strlen(cmd->name);
6259 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6264 if (i == nitems(got_histedit_cmds)) {
6265 err = histedit_syntax_error(lineno);
6268 while (isspace((unsigned char)p[0]))
6270 if (cmd->code == GOT_HISTEDIT_MESG) {
6271 if (hle == NULL || hle->logmsg != NULL) {
6272 err = got_error(GOT_ERR_HISTEDIT_CMD);
6276 err = histedit_edit_logmsg(hle, repo);
6280 hle->logmsg = strdup(p);
6281 if (hle->logmsg == NULL) {
6282 err = got_error_from_errno("strdup");
6291 while (end[0] && !isspace((unsigned char)end[0]))
6295 err = got_object_resolve_id_str(&commit_id, repo, p);
6297 /* override error code */
6298 err = histedit_syntax_error(lineno);
6302 hle = malloc(sizeof(*hle));
6304 err = got_error_from_errno("malloc");
6308 hle->commit_id = commit_id;
6313 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6321 static const struct got_error *
6322 histedit_check_script(struct got_histedit_list *histedit_cmds,
6323 struct got_object_id_queue *commits, struct got_repository *repo)
6325 const struct got_error *err = NULL;
6326 struct got_object_qid *qid;
6327 struct got_histedit_list_entry *hle;
6328 static char msg[92];
6331 if (TAILQ_EMPTY(histedit_cmds))
6332 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6333 "histedit script contains no commands");
6334 if (SIMPLEQ_EMPTY(commits))
6335 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6337 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6338 struct got_histedit_list_entry *hle2;
6339 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6342 if (got_object_id_cmp(hle->commit_id,
6343 hle2->commit_id) != 0)
6345 err = got_object_id_str(&id_str, hle->commit_id);
6348 snprintf(msg, sizeof(msg), "commit %s is listed "
6349 "more than once in histedit script", id_str);
6351 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6355 SIMPLEQ_FOREACH(qid, commits, entry) {
6356 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6357 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6361 err = got_object_id_str(&id_str, qid->id);
6364 snprintf(msg, sizeof(msg),
6365 "commit %s missing from histedit script", id_str);
6367 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6371 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6372 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6373 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6374 "last commit in histedit script cannot be folded");
6379 static const struct got_error *
6380 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6381 const char *path, struct got_object_id_queue *commits,
6382 struct got_repository *repo)
6384 const struct got_error *err = NULL;
6388 err = get_editor(&editor);
6392 if (spawn_editor(editor, path) == -1) {
6393 err = got_error_from_errno("failed spawning editor");
6397 f = fopen(path, "r");
6399 err = got_error_from_errno("fopen");
6402 err = histedit_parse_list(histedit_cmds, f, repo);
6406 err = histedit_check_script(histedit_cmds, commits, repo);
6408 if (f && fclose(f) != 0 && err == NULL)
6409 err = got_error_from_errno("fclose");
6414 static const struct got_error *
6415 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6416 struct got_object_id_queue *, const char *, const char *,
6417 struct got_repository *);
6419 static const struct got_error *
6420 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6421 struct got_object_id_queue *commits, const char *branch_name,
6422 int edit_logmsg_only, struct got_repository *repo)
6424 const struct got_error *err;
6428 err = got_opentemp_named(&path, &f, "got-histedit");
6432 err = write_cmd_list(f, branch_name, commits);
6436 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6440 if (edit_logmsg_only) {
6442 err = histedit_parse_list(histedit_cmds, f, repo);
6444 if (fclose(f) != 0) {
6445 err = got_error_from_errno("fclose");
6449 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6451 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6452 err->code != GOT_ERR_HISTEDIT_CMD)
6454 err = histedit_edit_list_retry(histedit_cmds, err,
6455 commits, path, branch_name, repo);
6459 if (f && fclose(f) != 0 && err == NULL)
6460 err = got_error_from_errno("fclose");
6461 if (path && unlink(path) != 0 && err == NULL)
6462 err = got_error_from_errno2("unlink", path);
6467 static const struct got_error *
6468 histedit_save_list(struct got_histedit_list *histedit_cmds,
6469 struct got_worktree *worktree, struct got_repository *repo)
6471 const struct got_error *err = NULL;
6474 struct got_histedit_list_entry *hle;
6475 struct got_commit_object *commit = NULL;
6477 err = got_worktree_get_histedit_script_path(&path, worktree);
6481 f = fopen(path, "w");
6483 err = got_error_from_errno2("fopen", path);
6486 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6487 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6493 int n = fprintf(f, "%c %s\n",
6494 GOT_HISTEDIT_MESG, hle->logmsg);
6496 err = got_ferror(f, GOT_ERR_IO);
6502 if (f && fclose(f) != 0 && err == NULL)
6503 err = got_error_from_errno("fclose");
6506 got_object_commit_close(commit);
6511 histedit_free_list(struct got_histedit_list *histedit_cmds)
6513 struct got_histedit_list_entry *hle;
6515 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6516 TAILQ_REMOVE(histedit_cmds, hle, entry);
6521 static const struct got_error *
6522 histedit_load_list(struct got_histedit_list *histedit_cmds,
6523 const char *path, struct got_repository *repo)
6525 const struct got_error *err = NULL;
6528 f = fopen(path, "r");
6530 err = got_error_from_errno2("fopen", path);
6534 err = histedit_parse_list(histedit_cmds, f, repo);
6536 if (f && fclose(f) != 0 && err == NULL)
6537 err = got_error_from_errno("fclose");
6541 static const struct got_error *
6542 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6543 const struct got_error *edit_err, struct got_object_id_queue *commits,
6544 const char *path, const char *branch_name, struct got_repository *repo)
6546 const struct got_error *err = NULL, *prev_err = edit_err;
6549 while (resp != 'c' && resp != 'r' && resp != 'a') {
6550 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6551 "or (a)bort: ", getprogname(), prev_err->msg);
6556 histedit_free_list(histedit_cmds);
6557 err = histedit_run_editor(histedit_cmds, path, commits,
6560 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6561 err->code != GOT_ERR_HISTEDIT_CMD)
6568 } else if (resp == 'r') {
6569 histedit_free_list(histedit_cmds);
6570 err = histedit_edit_script(histedit_cmds,
6571 commits, branch_name, 0, repo);
6573 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6574 err->code != GOT_ERR_HISTEDIT_CMD)
6581 } else if (resp == 'a') {
6582 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6585 printf("invalid response '%c'\n", resp);
6591 static const struct got_error *
6592 histedit_complete(struct got_worktree *worktree,
6593 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6594 struct got_reference *branch, struct got_repository *repo)
6596 printf("Switching work tree to %s\n",
6597 got_ref_get_symref_target(branch));
6598 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6602 static const struct got_error *
6603 show_histedit_progress(struct got_commit_object *commit,
6604 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6606 const struct got_error *err;
6607 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6609 err = got_object_id_str(&old_id_str, hle->commit_id);
6614 err = got_object_id_str(&new_id_str, new_id);
6619 old_id_str[12] = '\0';
6621 new_id_str[12] = '\0';
6624 logmsg = strdup(hle->logmsg);
6625 if (logmsg == NULL) {
6626 err = got_error_from_errno("strdup");
6629 trim_logmsg(logmsg, 42);
6631 err = get_short_logmsg(&logmsg, 42, commit);
6636 switch (hle->cmd->code) {
6637 case GOT_HISTEDIT_PICK:
6638 case GOT_HISTEDIT_EDIT:
6639 printf("%s -> %s: %s\n", old_id_str,
6640 new_id_str ? new_id_str : "no-op change", logmsg);
6642 case GOT_HISTEDIT_DROP:
6643 case GOT_HISTEDIT_FOLD:
6644 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6656 static const struct got_error *
6657 histedit_commit(struct got_pathlist_head *merged_paths,
6658 struct got_worktree *worktree, struct got_fileindex *fileindex,
6659 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6660 struct got_repository *repo)
6662 const struct got_error *err;
6663 struct got_commit_object *commit;
6664 struct got_object_id *new_commit_id;
6666 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6667 && hle->logmsg == NULL) {
6668 err = histedit_edit_logmsg(hle, repo);
6673 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6677 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6678 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6681 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6683 err = show_histedit_progress(commit, hle, NULL);
6685 err = show_histedit_progress(commit, hle, new_commit_id);
6686 free(new_commit_id);
6689 got_object_commit_close(commit);
6693 static const struct got_error *
6694 histedit_skip_commit(struct got_histedit_list_entry *hle,
6695 struct got_worktree *worktree, struct got_repository *repo)
6697 const struct got_error *error;
6698 struct got_commit_object *commit;
6700 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6705 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6709 error = show_histedit_progress(commit, hle, NULL);
6710 got_object_commit_close(commit);
6714 static const struct got_error *
6715 check_local_changes(void *arg, unsigned char status,
6716 unsigned char staged_status, const char *path,
6717 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6718 struct got_object_id *commit_id, int dirfd, const char *de_name)
6720 int *have_local_changes = arg;
6723 case GOT_STATUS_ADD:
6724 case GOT_STATUS_DELETE:
6725 case GOT_STATUS_MODIFY:
6726 case GOT_STATUS_CONFLICT:
6727 *have_local_changes = 1;
6728 return got_error(GOT_ERR_CANCELLED);
6733 switch (staged_status) {
6734 case GOT_STATUS_ADD:
6735 case GOT_STATUS_DELETE:
6736 case GOT_STATUS_MODIFY:
6737 *have_local_changes = 1;
6738 return got_error(GOT_ERR_CANCELLED);
6746 static const struct got_error *
6747 cmd_histedit(int argc, char *argv[])
6749 const struct got_error *error = NULL;
6750 struct got_worktree *worktree = NULL;
6751 struct got_fileindex *fileindex = NULL;
6752 struct got_repository *repo = NULL;
6754 struct got_reference *branch = NULL;
6755 struct got_reference *tmp_branch = NULL;
6756 struct got_object_id *resume_commit_id = NULL;
6757 struct got_object_id *base_commit_id = NULL;
6758 struct got_object_id *head_commit_id = NULL;
6759 struct got_commit_object *commit = NULL;
6760 int ch, rebase_in_progress = 0, did_something;
6761 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6762 int edit_logmsg_only = 0;
6763 const char *edit_script_path = NULL;
6764 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6765 struct got_object_id_queue commits;
6766 struct got_pathlist_head merged_paths;
6767 const struct got_object_id_queue *parent_ids;
6768 struct got_object_qid *pid;
6769 struct got_histedit_list histedit_cmds;
6770 struct got_histedit_list_entry *hle;
6772 SIMPLEQ_INIT(&commits);
6773 TAILQ_INIT(&histedit_cmds);
6774 TAILQ_INIT(&merged_paths);
6776 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6785 edit_script_path = optarg;
6788 edit_logmsg_only = 1;
6800 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6801 "unveil", NULL) == -1)
6804 if (abort_edit && continue_edit)
6805 errx(1, "histedit's -a and -c options are mutually exclusive");
6806 if (edit_script_path && edit_logmsg_only)
6807 errx(1, "histedit's -F and -m options are mutually exclusive");
6808 if (abort_edit && edit_logmsg_only)
6809 errx(1, "histedit's -a and -m options are mutually exclusive");
6810 if (continue_edit && edit_logmsg_only)
6811 errx(1, "histedit's -c and -m options are mutually exclusive");
6816 * This command cannot apply unveil(2) in all cases because the
6817 * user may choose to run an editor to edit the histedit script
6818 * and to edit individual commit log messages.
6819 * unveil(2) traverses exec(2); if an editor is used we have to
6820 * apply unveil after edit script and log messages have been written.
6821 * XXX TODO: Make use of unveil(2) where possible.
6824 cwd = getcwd(NULL, 0);
6826 error = got_error_from_errno("getcwd");
6829 error = got_worktree_open(&worktree, cwd);
6833 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6838 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6841 if (rebase_in_progress) {
6842 error = got_error(GOT_ERR_REBASING);
6846 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6850 if (edit_in_progress && edit_logmsg_only) {
6851 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6852 "histedit operation is in progress in this "
6853 "work tree and must be continued or aborted "
6854 "before the -m option can be used");
6858 if (edit_in_progress && abort_edit) {
6859 error = got_worktree_histedit_continue(&resume_commit_id,
6860 &tmp_branch, &branch, &base_commit_id, &fileindex,
6864 printf("Switching work tree to %s\n",
6865 got_ref_get_symref_target(branch));
6866 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6867 branch, base_commit_id, update_progress, &did_something);
6870 printf("Histedit of %s aborted\n",
6871 got_ref_get_symref_target(branch));
6872 goto done; /* nothing else to do */
6873 } else if (abort_edit) {
6874 error = got_error(GOT_ERR_NOT_HISTEDIT);
6878 if (continue_edit) {
6881 if (!edit_in_progress) {
6882 error = got_error(GOT_ERR_NOT_HISTEDIT);
6886 error = got_worktree_get_histedit_script_path(&path, worktree);
6890 error = histedit_load_list(&histedit_cmds, path, repo);
6895 error = got_worktree_histedit_continue(&resume_commit_id,
6896 &tmp_branch, &branch, &base_commit_id, &fileindex,
6901 error = got_ref_resolve(&head_commit_id, repo, branch);
6905 error = got_object_open_as_commit(&commit, repo,
6909 parent_ids = got_object_commit_get_parent_ids(commit);
6910 pid = SIMPLEQ_FIRST(parent_ids);
6912 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6915 error = collect_commits(&commits, head_commit_id, pid->id,
6916 base_commit_id, got_worktree_get_path_prefix(worktree),
6917 GOT_ERR_HISTEDIT_PATH, repo);
6918 got_object_commit_close(commit);
6923 if (edit_in_progress) {
6924 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6928 error = got_ref_open(&branch, repo,
6929 got_worktree_get_head_ref_name(worktree), 0);
6933 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6934 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6935 "will not edit commit history of a branch outside "
6936 "the \"refs/heads/\" reference namespace");
6940 error = got_ref_resolve(&head_commit_id, repo, branch);
6941 got_ref_close(branch);
6946 error = got_object_open_as_commit(&commit, repo,
6950 parent_ids = got_object_commit_get_parent_ids(commit);
6951 pid = SIMPLEQ_FIRST(parent_ids);
6953 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6956 error = collect_commits(&commits, head_commit_id, pid->id,
6957 got_worktree_get_base_commit_id(worktree),
6958 got_worktree_get_path_prefix(worktree),
6959 GOT_ERR_HISTEDIT_PATH, repo);
6960 got_object_commit_close(commit);
6965 if (SIMPLEQ_EMPTY(&commits)) {
6966 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6970 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6971 &base_commit_id, &fileindex, worktree, repo);
6975 if (edit_script_path) {
6976 error = histedit_load_list(&histedit_cmds,
6977 edit_script_path, repo);
6979 got_worktree_histedit_abort(worktree, fileindex,
6980 repo, branch, base_commit_id,
6981 update_progress, &did_something);
6985 const char *branch_name;
6986 branch_name = got_ref_get_symref_target(branch);
6987 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6989 error = histedit_edit_script(&histedit_cmds, &commits,
6990 branch_name, edit_logmsg_only, repo);
6992 got_worktree_histedit_abort(worktree, fileindex,
6993 repo, branch, base_commit_id,
6994 update_progress, &did_something);
7000 error = histedit_save_list(&histedit_cmds, worktree,
7003 got_worktree_histedit_abort(worktree, fileindex,
7004 repo, branch, base_commit_id,
7005 update_progress, &did_something);
7011 error = histedit_check_script(&histedit_cmds, &commits, repo);
7015 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7016 if (resume_commit_id) {
7017 if (got_object_id_cmp(hle->commit_id,
7018 resume_commit_id) != 0)
7021 resume_commit_id = NULL;
7022 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7023 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7024 error = histedit_skip_commit(hle, worktree,
7029 struct got_pathlist_head paths;
7030 int have_changes = 0;
7033 error = got_pathlist_append(&paths, "", NULL);
7036 error = got_worktree_status(worktree, &paths,
7037 repo, check_local_changes, &have_changes,
7038 check_cancelled, NULL);
7039 got_pathlist_free(&paths);
7041 if (error->code != GOT_ERR_CANCELLED)
7043 if (sigint_received || sigpipe_received)
7047 error = histedit_commit(NULL, worktree,
7048 fileindex, tmp_branch, hle, repo);
7052 error = got_object_open_as_commit(
7053 &commit, repo, hle->commit_id);
7056 error = show_histedit_progress(commit,
7058 got_object_commit_close(commit);
7067 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7068 error = histedit_skip_commit(hle, worktree, repo);
7074 error = got_object_open_as_commit(&commit, repo,
7078 parent_ids = got_object_commit_get_parent_ids(commit);
7079 pid = SIMPLEQ_FIRST(parent_ids);
7081 error = got_worktree_histedit_merge_files(&merged_paths,
7082 worktree, fileindex, pid->id, hle->commit_id, repo,
7083 rebase_progress, &rebase_status, check_cancelled, NULL);
7086 got_object_commit_close(commit);
7089 if (rebase_status == GOT_STATUS_CONFLICT) {
7090 error = show_rebase_merge_conflict(hle->commit_id,
7094 got_worktree_rebase_pathlist_free(&merged_paths);
7098 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7100 error = got_object_id_str(&id_str, hle->commit_id);
7103 printf("Stopping histedit for amending commit %s\n",
7106 got_worktree_rebase_pathlist_free(&merged_paths);
7107 error = got_worktree_histedit_postpone(worktree,
7112 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7113 error = histedit_skip_commit(hle, worktree, repo);
7119 error = histedit_commit(&merged_paths, worktree, fileindex,
7120 tmp_branch, hle, repo);
7121 got_worktree_rebase_pathlist_free(&merged_paths);
7126 if (rebase_status == GOT_STATUS_CONFLICT) {
7127 error = got_worktree_histedit_postpone(worktree, fileindex);
7130 error = got_error_msg(GOT_ERR_CONFLICTS,
7131 "conflicts must be resolved before histedit can continue");
7133 error = histedit_complete(worktree, fileindex, tmp_branch,
7136 got_object_id_queue_free(&commits);
7137 histedit_free_list(&histedit_cmds);
7138 free(head_commit_id);
7139 free(base_commit_id);
7140 free(resume_commit_id);
7142 got_object_commit_close(commit);
7144 got_ref_close(branch);
7146 got_ref_close(tmp_branch);
7148 got_worktree_close(worktree);
7150 got_repo_close(repo);
7155 usage_integrate(void)
7157 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7161 static const struct got_error *
7162 cmd_integrate(int argc, char *argv[])
7164 const struct got_error *error = NULL;
7165 struct got_repository *repo = NULL;
7166 struct got_worktree *worktree = NULL;
7167 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7168 const char *branch_arg = NULL;
7169 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7170 struct got_fileindex *fileindex = NULL;
7171 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7172 int ch, did_something = 0;
7174 while ((ch = getopt(argc, argv, "")) != -1) {
7187 branch_arg = argv[0];
7189 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7190 "unveil", NULL) == -1)
7193 cwd = getcwd(NULL, 0);
7195 error = got_error_from_errno("getcwd");
7199 error = got_worktree_open(&worktree, cwd);
7203 error = check_rebase_or_histedit_in_progress(worktree);
7207 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7212 error = apply_unveil(got_repo_get_path(repo), 0,
7213 got_worktree_get_root_path(worktree));
7217 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7218 error = got_error_from_errno("asprintf");
7222 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7223 &base_branch_ref, worktree, refname, repo);
7227 refname = strdup(got_ref_get_name(branch_ref));
7228 if (refname == NULL) {
7229 error = got_error_from_errno("strdup");
7230 got_worktree_integrate_abort(worktree, fileindex, repo,
7231 branch_ref, base_branch_ref);
7234 base_refname = strdup(got_ref_get_name(base_branch_ref));
7235 if (base_refname == NULL) {
7236 error = got_error_from_errno("strdup");
7237 got_worktree_integrate_abort(worktree, fileindex, repo,
7238 branch_ref, base_branch_ref);
7242 error = got_ref_resolve(&commit_id, repo, branch_ref);
7246 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7250 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7251 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7252 "specified branch has already been integrated");
7253 got_worktree_integrate_abort(worktree, fileindex, repo,
7254 branch_ref, base_branch_ref);
7258 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7260 if (error->code == GOT_ERR_ANCESTRY)
7261 error = got_error(GOT_ERR_REBASE_REQUIRED);
7262 got_worktree_integrate_abort(worktree, fileindex, repo,
7263 branch_ref, base_branch_ref);
7267 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7268 branch_ref, base_branch_ref, update_progress, &did_something,
7269 check_cancelled, NULL);
7273 printf("Integrated %s into %s\n", refname, base_refname);
7276 got_repo_close(repo);
7278 got_worktree_close(worktree);
7280 free(base_commit_id);
7290 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7291 "[file-path ...]\n",
7296 static const struct got_error *
7297 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7298 const char *path, struct got_object_id *blob_id,
7299 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7300 int dirfd, const char *de_name)
7302 const struct got_error *err = NULL;
7303 char *id_str = NULL;
7305 if (staged_status != GOT_STATUS_ADD &&
7306 staged_status != GOT_STATUS_MODIFY &&
7307 staged_status != GOT_STATUS_DELETE)
7310 if (staged_status == GOT_STATUS_ADD ||
7311 staged_status == GOT_STATUS_MODIFY)
7312 err = got_object_id_str(&id_str, staged_blob_id);
7314 err = got_object_id_str(&id_str, blob_id);
7318 printf("%s %c %s\n", id_str, staged_status, path);
7323 static const struct got_error *
7324 cmd_stage(int argc, char *argv[])
7326 const struct got_error *error = NULL;
7327 struct got_repository *repo = NULL;
7328 struct got_worktree *worktree = NULL;
7330 struct got_pathlist_head paths;
7331 struct got_pathlist_entry *pe;
7332 int ch, list_stage = 0, pflag = 0;
7333 FILE *patch_script_file = NULL;
7334 const char *patch_script_path = NULL;
7335 struct choose_patch_arg cpa;
7339 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7348 patch_script_path = optarg;
7360 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7361 "unveil", NULL) == -1)
7364 if (list_stage && (pflag || patch_script_path))
7365 errx(1, "-l option cannot be used with other options");
7366 if (patch_script_path && !pflag)
7367 errx(1, "-F option can only be used together with -p option");
7369 cwd = getcwd(NULL, 0);
7371 error = got_error_from_errno("getcwd");
7375 error = got_worktree_open(&worktree, cwd);
7379 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7384 if (patch_script_path) {
7385 patch_script_file = fopen(patch_script_path, "r");
7386 if (patch_script_file == NULL) {
7387 error = got_error_from_errno2("fopen",
7392 error = apply_unveil(got_repo_get_path(repo), 0,
7393 got_worktree_get_root_path(worktree));
7397 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7402 error = got_worktree_status(worktree, &paths, repo,
7403 print_stage, NULL, check_cancelled, NULL);
7405 cpa.patch_script_file = patch_script_file;
7406 cpa.action = "stage";
7407 error = got_worktree_stage(worktree, &paths,
7408 pflag ? NULL : print_status, NULL,
7409 pflag ? choose_patch : NULL, &cpa, repo);
7412 if (patch_script_file && fclose(patch_script_file) == EOF &&
7414 error = got_error_from_errno2("fclose", patch_script_path);
7416 got_repo_close(repo);
7418 got_worktree_close(worktree);
7419 TAILQ_FOREACH(pe, &paths, entry)
7420 free((char *)pe->path);
7421 got_pathlist_free(&paths);
7429 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7430 "[file-path ...]\n",
7436 static const struct got_error *
7437 cmd_unstage(int argc, char *argv[])
7439 const struct got_error *error = NULL;
7440 struct got_repository *repo = NULL;
7441 struct got_worktree *worktree = NULL;
7443 struct got_pathlist_head paths;
7444 struct got_pathlist_entry *pe;
7445 int ch, did_something = 0, pflag = 0;
7446 FILE *patch_script_file = NULL;
7447 const char *patch_script_path = NULL;
7448 struct choose_patch_arg cpa;
7452 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7458 patch_script_path = optarg;
7470 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7471 "unveil", NULL) == -1)
7474 if (patch_script_path && !pflag)
7475 errx(1, "-F option can only be used together with -p option");
7477 cwd = getcwd(NULL, 0);
7479 error = got_error_from_errno("getcwd");
7483 error = got_worktree_open(&worktree, cwd);
7487 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7492 if (patch_script_path) {
7493 patch_script_file = fopen(patch_script_path, "r");
7494 if (patch_script_file == NULL) {
7495 error = got_error_from_errno2("fopen",
7501 error = apply_unveil(got_repo_get_path(repo), 0,
7502 got_worktree_get_root_path(worktree));
7506 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7510 cpa.patch_script_file = patch_script_file;
7511 cpa.action = "unstage";
7512 error = got_worktree_unstage(worktree, &paths, update_progress,
7513 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7515 if (patch_script_file && fclose(patch_script_file) == EOF &&
7517 error = got_error_from_errno2("fclose", patch_script_path);
7519 got_repo_close(repo);
7521 got_worktree_close(worktree);
7522 TAILQ_FOREACH(pe, &paths, entry)
7523 free((char *)pe->path);
7524 got_pathlist_free(&paths);
7532 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7533 "arg1 [arg2 ...]\n", getprogname());
7537 static const struct got_error *
7538 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7540 const struct got_error *err;
7541 struct got_blob_object *blob;
7543 err = got_object_open_as_blob(&blob, repo, id, 8192);
7547 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7548 got_object_blob_close(blob);
7552 static const struct got_error *
7553 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7555 const struct got_error *err;
7556 struct got_tree_object *tree;
7559 err = got_object_open_as_tree(&tree, repo, id);
7563 nentries = got_object_tree_get_nentries(tree);
7564 for (i = 0; i < nentries; i++) {
7565 struct got_tree_entry *te;
7567 if (sigint_received || sigpipe_received)
7569 te = got_object_tree_get_entry(tree, i);
7570 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7573 fprintf(outfile, "%s %.7o %s\n", id_str,
7574 got_tree_entry_get_mode(te),
7575 got_tree_entry_get_name(te));
7579 got_object_tree_close(tree);
7583 static const struct got_error *
7584 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7586 const struct got_error *err;
7587 struct got_commit_object *commit;
7588 const struct got_object_id_queue *parent_ids;
7589 struct got_object_qid *pid;
7590 char *id_str = NULL;
7591 const char *logmsg = NULL;
7593 err = got_object_open_as_commit(&commit, repo, id);
7597 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7601 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7602 parent_ids = got_object_commit_get_parent_ids(commit);
7603 fprintf(outfile, "numparents %d\n",
7604 got_object_commit_get_nparents(commit));
7605 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7607 err = got_object_id_str(&pid_str, pid->id);
7610 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7613 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7614 got_object_commit_get_author(commit),
7615 got_object_commit_get_author_time(commit));
7617 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7618 got_object_commit_get_author(commit),
7619 got_object_commit_get_committer_time(commit));
7621 logmsg = got_object_commit_get_logmsg_raw(commit);
7622 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7623 fprintf(outfile, "%s", logmsg);
7626 got_object_commit_close(commit);
7630 static const struct got_error *
7631 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7633 const struct got_error *err;
7634 struct got_tag_object *tag;
7635 char *id_str = NULL;
7636 const char *tagmsg = NULL;
7638 err = got_object_open_as_tag(&tag, repo, id);
7642 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7646 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7648 switch (got_object_tag_get_object_type(tag)) {
7649 case GOT_OBJ_TYPE_BLOB:
7650 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7651 GOT_OBJ_LABEL_BLOB);
7653 case GOT_OBJ_TYPE_TREE:
7654 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7655 GOT_OBJ_LABEL_TREE);
7657 case GOT_OBJ_TYPE_COMMIT:
7658 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7659 GOT_OBJ_LABEL_COMMIT);
7661 case GOT_OBJ_TYPE_TAG:
7662 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7669 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7670 got_object_tag_get_name(tag));
7672 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7673 got_object_tag_get_tagger(tag),
7674 got_object_tag_get_tagger_time(tag));
7676 tagmsg = got_object_tag_get_message(tag);
7677 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7678 fprintf(outfile, "%s", tagmsg);
7681 got_object_tag_close(tag);
7685 static const struct got_error *
7686 cmd_cat(int argc, char *argv[])
7688 const struct got_error *error;
7689 struct got_repository *repo = NULL;
7690 struct got_worktree *worktree = NULL;
7691 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7692 const char *commit_id_str = NULL;
7693 struct got_object_id *id = NULL, *commit_id = NULL;
7694 int ch, obj_type, i, force_path = 0;
7697 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7702 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7705 commit_id_str = optarg;
7708 repo_path = realpath(optarg, NULL);
7709 if (repo_path == NULL)
7710 return got_error_from_errno2("realpath",
7712 got_path_strip_trailing_slashes(repo_path);
7726 cwd = getcwd(NULL, 0);
7728 error = got_error_from_errno("getcwd");
7731 error = got_worktree_open(&worktree, cwd);
7732 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7735 if (repo_path == NULL) {
7737 got_worktree_get_repo_path(worktree));
7738 if (repo_path == NULL) {
7739 error = got_error_from_errno("strdup");
7745 if (repo_path == NULL) {
7746 repo_path = getcwd(NULL, 0);
7747 if (repo_path == NULL)
7748 return got_error_from_errno("getcwd");
7751 error = got_repo_open(&repo, repo_path, NULL);
7756 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7760 if (commit_id_str == NULL)
7761 commit_id_str = GOT_REF_HEAD;
7762 error = got_repo_match_object_id(&commit_id, NULL,
7763 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7767 for (i = 0; i < argc; i++) {
7769 error = got_object_id_by_path(&id, repo, commit_id,
7774 error = got_repo_match_object_id(&id, &label, argv[i],
7775 GOT_OBJ_TYPE_ANY, 0, repo);
7777 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7778 error->code != GOT_ERR_NOT_REF)
7780 error = got_object_id_by_path(&id, repo,
7781 commit_id, argv[i]);
7787 error = got_object_get_type(&obj_type, repo, id);
7792 case GOT_OBJ_TYPE_BLOB:
7793 error = cat_blob(id, repo, stdout);
7795 case GOT_OBJ_TYPE_TREE:
7796 error = cat_tree(id, repo, stdout);
7798 case GOT_OBJ_TYPE_COMMIT:
7799 error = cat_commit(id, repo, stdout);
7801 case GOT_OBJ_TYPE_TAG:
7802 error = cat_tag(id, repo, stdout);
7805 error = got_error(GOT_ERR_OBJ_TYPE);
7820 got_worktree_close(worktree);
7822 const struct got_error *repo_error;
7823 repo_error = got_repo_close(repo);