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 *error = 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) {
1079 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1080 &repo_name, argv[0]);
1085 if (strcmp(proto, "git") == 0) {
1086 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1087 "sendfd dns inet unveil", NULL) == -1)
1089 } else if (strcmp(proto, "git+ssh") == 0 ||
1090 strcmp(proto, "ssh") == 0) {
1091 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1092 "sendfd unveil", NULL) == -1)
1094 } else if (strcmp(proto, "http") == 0 ||
1095 strcmp(proto, "git+http") == 0) {
1096 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1099 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1103 if (dirname == NULL) {
1104 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1105 error = got_error_from_errno("asprintf");
1108 repo_path = default_destdir;
1110 repo_path = dirname;
1112 error = got_path_mkdir(repo_path);
1116 error = got_repo_init(repo_path);
1120 error = got_repo_open(&repo, repo_path, NULL);
1124 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1125 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1126 error = got_error_from_errno2("unveil",
1127 GOT_FETCH_PATH_SSH);
1131 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1135 error = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1139 printf("Connected to %s:%s\n", host, port);
1141 fpa.last_scaled_size[0] = '\0';
1142 fpa.last_p_indexed = -1;
1143 fpa.last_p_resolved = -1;
1144 error = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1145 repo, fetch_progress, &fpa);
1149 error = got_object_id_str(&id_str, pack_hash);
1152 printf("Fetched %s.pack\n", id_str);
1155 /* Set up references provided with the pack file. */
1156 TAILQ_FOREACH(pe, &refs, entry) {
1157 const char *refname = pe->path;
1158 struct got_object_id *id = pe->data;
1159 struct got_reference *ref;
1162 error = got_ref_alloc(&ref, refname, id);
1167 error = got_object_id_str(&id_str, id);
1170 printf("%s: %s\n", got_ref_get_name(ref), id_str);
1173 error = got_ref_write(ref, repo);
1179 /* Set the HEAD reference if the server provided one. */
1180 TAILQ_FOREACH(pe, &symrefs, entry) {
1181 struct got_reference *symref, *target_ref;
1182 const char *refname = pe->path;
1183 const char *target = pe->data;
1185 if (strcmp(refname, GOT_REF_HEAD) != 0)
1188 error = got_ref_open(&target_ref, repo, target, 0);
1190 if (error->code == GOT_ERR_NOT_REF)
1195 error = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1196 got_ref_close(target_ref);
1200 printf("Setting %s to %s\n", GOT_REF_HEAD,
1201 got_ref_get_symref_target(symref));
1203 error = got_ref_write(symref, repo);
1204 got_ref_close(symref);
1209 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1210 error = got_error_from_errno("close");
1212 got_repo_close(repo);
1213 TAILQ_FOREACH(pe, &refs, entry) {
1214 free((void *)pe->path);
1217 got_pathlist_free(&refs);
1218 TAILQ_FOREACH(pe, &symrefs, entry) {
1219 free((void *)pe->path);
1222 got_pathlist_free(&symrefs);
1229 free(default_destdir);
1233 static const struct got_error *
1234 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1236 static char msg[512];
1237 const char *branch_name;
1239 if (got_ref_is_symbolic(ref))
1240 branch_name = got_ref_get_symref_target(ref);
1242 branch_name = got_ref_get_name(ref);
1244 if (strncmp("refs/heads/", branch_name, 11) == 0)
1247 snprintf(msg, sizeof(msg),
1248 "target commit is not contained in branch '%s'; "
1249 "the branch to use must be specified with -b; "
1250 "if necessary a new branch can be created for "
1251 "this commit with 'got branch -c %s BRANCH_NAME'",
1252 branch_name, commit_id_str);
1254 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1257 static const struct got_error *
1258 cmd_checkout(int argc, char *argv[])
1260 const struct got_error *error = NULL;
1261 struct got_repository *repo = NULL;
1262 struct got_reference *head_ref = NULL;
1263 struct got_worktree *worktree = NULL;
1264 char *repo_path = NULL;
1265 char *worktree_path = NULL;
1266 const char *path_prefix = "";
1267 const char *branch_name = GOT_REF_HEAD;
1268 char *commit_id_str = NULL;
1269 int ch, same_path_prefix, allow_nonempty = 0;
1270 struct got_pathlist_head paths;
1271 struct got_checkout_progress_arg cpa;
1275 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1278 branch_name = optarg;
1281 commit_id_str = strdup(optarg);
1282 if (commit_id_str == NULL)
1283 return got_error_from_errno("strdup");
1289 path_prefix = optarg;
1301 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1302 "unveil", NULL) == -1)
1306 char *cwd, *base, *dotgit;
1307 repo_path = realpath(argv[0], NULL);
1308 if (repo_path == NULL)
1309 return got_error_from_errno2("realpath", argv[0]);
1310 cwd = getcwd(NULL, 0);
1312 error = got_error_from_errno("getcwd");
1315 if (path_prefix[0]) {
1316 base = basename(path_prefix);
1318 error = got_error_from_errno2("basename",
1323 base = basename(repo_path);
1325 error = got_error_from_errno2("basename",
1330 dotgit = strstr(base, ".git");
1333 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1334 error = got_error_from_errno("asprintf");
1339 } else if (argc == 2) {
1340 repo_path = realpath(argv[0], NULL);
1341 if (repo_path == NULL) {
1342 error = got_error_from_errno2("realpath", argv[0]);
1345 worktree_path = realpath(argv[1], NULL);
1346 if (worktree_path == NULL) {
1347 if (errno != ENOENT) {
1348 error = got_error_from_errno2("realpath",
1352 worktree_path = strdup(argv[1]);
1353 if (worktree_path == NULL) {
1354 error = got_error_from_errno("strdup");
1361 got_path_strip_trailing_slashes(repo_path);
1362 got_path_strip_trailing_slashes(worktree_path);
1364 error = got_repo_open(&repo, repo_path, NULL);
1368 /* Pre-create work tree path for unveil(2) */
1369 error = got_path_mkdir(worktree_path);
1371 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1372 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1374 if (!allow_nonempty &&
1375 !got_path_dir_is_empty(worktree_path)) {
1376 error = got_error_path(worktree_path,
1377 GOT_ERR_DIR_NOT_EMPTY);
1382 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1386 error = got_ref_open(&head_ref, repo, branch_name, 0);
1390 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1391 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1394 error = got_worktree_open(&worktree, worktree_path);
1398 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1402 if (!same_path_prefix) {
1403 error = got_error(GOT_ERR_PATH_PREFIX);
1407 if (commit_id_str) {
1408 struct got_object_id *commit_id;
1409 error = got_repo_match_object_id(&commit_id, NULL,
1410 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1413 error = check_linear_ancestry(commit_id,
1414 got_worktree_get_base_commit_id(worktree), 0, repo);
1415 if (error != NULL) {
1417 if (error->code == GOT_ERR_ANCESTRY) {
1418 error = checkout_ancestry_error(
1419 head_ref, commit_id_str);
1423 error = check_same_branch(commit_id, head_ref, NULL, repo);
1425 if (error->code == GOT_ERR_ANCESTRY) {
1426 error = checkout_ancestry_error(
1427 head_ref, commit_id_str);
1431 error = got_worktree_set_base_commit_id(worktree, repo,
1438 error = got_pathlist_append(&paths, "", NULL);
1441 cpa.worktree_path = worktree_path;
1442 cpa.had_base_commit_ref_error = 0;
1443 error = got_worktree_checkout_files(worktree, &paths, repo,
1444 checkout_progress, &cpa, check_cancelled, NULL);
1448 printf("Now shut up and hack\n");
1449 if (cpa.had_base_commit_ref_error)
1450 show_worktree_base_ref_warning();
1452 got_pathlist_free(&paths);
1453 free(commit_id_str);
1455 free(worktree_path);
1462 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1467 static const struct got_error *
1468 update_progress(void *arg, unsigned char status, const char *path)
1470 int *did_something = arg;
1472 if (status == GOT_STATUS_EXISTS ||
1473 status == GOT_STATUS_BASE_REF_ERR)
1478 /* Base commit bump happens silently. */
1479 if (status == GOT_STATUS_BUMP_BASE)
1482 while (path[0] == '/')
1484 printf("%c %s\n", status, path);
1488 static const struct got_error *
1489 switch_head_ref(struct got_reference *head_ref,
1490 struct got_object_id *commit_id, struct got_worktree *worktree,
1491 struct got_repository *repo)
1493 const struct got_error *err = NULL;
1495 int ref_has_moved = 0;
1497 /* Trivial case: switching between two different references. */
1498 if (strcmp(got_ref_get_name(head_ref),
1499 got_worktree_get_head_ref_name(worktree)) != 0) {
1500 printf("Switching work tree from %s to %s\n",
1501 got_worktree_get_head_ref_name(worktree),
1502 got_ref_get_name(head_ref));
1503 return got_worktree_set_head_ref(worktree, head_ref);
1506 err = check_linear_ancestry(commit_id,
1507 got_worktree_get_base_commit_id(worktree), 0, repo);
1509 if (err->code != GOT_ERR_ANCESTRY)
1516 /* Switching to a rebased branch with the same reference name. */
1517 err = got_object_id_str(&base_id_str,
1518 got_worktree_get_base_commit_id(worktree));
1521 printf("Reference %s now points at a different branch\n",
1522 got_worktree_get_head_ref_name(worktree));
1523 printf("Switching work tree from %s to %s\n", base_id_str,
1524 got_worktree_get_head_ref_name(worktree));
1528 static const struct got_error *
1529 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1531 const struct got_error *err;
1534 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1538 return got_error(GOT_ERR_REBASING);
1540 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1544 return got_error(GOT_ERR_HISTEDIT_BUSY);
1549 static const struct got_error *
1550 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1551 char *argv[], struct got_worktree *worktree)
1553 const struct got_error *err = NULL;
1560 return got_error_from_errno("strdup");
1561 return got_pathlist_append(paths, path, NULL);
1564 for (i = 0; i < argc; i++) {
1565 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1568 err = got_pathlist_append(paths, path, NULL);
1578 static const struct got_error *
1579 cmd_update(int argc, char *argv[])
1581 const struct got_error *error = NULL;
1582 struct got_repository *repo = NULL;
1583 struct got_worktree *worktree = NULL;
1584 char *worktree_path = NULL;
1585 struct got_object_id *commit_id = NULL;
1586 char *commit_id_str = NULL;
1587 const char *branch_name = NULL;
1588 struct got_reference *head_ref = NULL;
1589 struct got_pathlist_head paths;
1590 struct got_pathlist_entry *pe;
1591 int ch, did_something = 0;
1595 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1598 branch_name = optarg;
1601 commit_id_str = strdup(optarg);
1602 if (commit_id_str == NULL)
1603 return got_error_from_errno("strdup");
1615 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1616 "unveil", NULL) == -1)
1619 worktree_path = getcwd(NULL, 0);
1620 if (worktree_path == NULL) {
1621 error = got_error_from_errno("getcwd");
1624 error = got_worktree_open(&worktree, worktree_path);
1628 error = check_rebase_or_histedit_in_progress(worktree);
1632 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1637 error = apply_unveil(got_repo_get_path(repo), 0,
1638 got_worktree_get_root_path(worktree));
1642 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1646 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1647 got_worktree_get_head_ref_name(worktree), 0);
1650 if (commit_id_str == NULL) {
1651 error = got_ref_resolve(&commit_id, repo, head_ref);
1654 error = got_object_id_str(&commit_id_str, commit_id);
1658 error = got_repo_match_object_id(&commit_id, NULL,
1659 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1660 free(commit_id_str);
1661 commit_id_str = NULL;
1664 error = got_object_id_str(&commit_id_str, commit_id);
1670 struct got_object_id *head_commit_id;
1671 TAILQ_FOREACH(pe, &paths, entry) {
1672 if (pe->path_len == 0)
1674 error = got_error_msg(GOT_ERR_BAD_PATH,
1675 "switching between branches requires that "
1676 "the entire work tree gets updated");
1679 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1682 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1684 free(head_commit_id);
1687 error = check_same_branch(commit_id, head_ref, NULL, repo);
1690 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1694 error = check_linear_ancestry(commit_id,
1695 got_worktree_get_base_commit_id(worktree), 0, repo);
1696 if (error != NULL) {
1697 if (error->code == GOT_ERR_ANCESTRY)
1698 error = got_error(GOT_ERR_BRANCH_MOVED);
1701 error = check_same_branch(commit_id, head_ref, NULL, repo);
1706 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1708 error = got_worktree_set_base_commit_id(worktree, repo,
1714 error = got_worktree_checkout_files(worktree, &paths, repo,
1715 update_progress, &did_something, check_cancelled, NULL);
1720 printf("Updated to commit %s\n", commit_id_str);
1722 printf("Already up-to-date\n");
1724 free(worktree_path);
1725 TAILQ_FOREACH(pe, &paths, entry)
1726 free((char *)pe->path);
1727 got_pathlist_free(&paths);
1729 free(commit_id_str);
1733 static const struct got_error *
1734 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1735 const char *path, int diff_context, int ignore_whitespace,
1736 struct got_repository *repo)
1738 const struct got_error *err = NULL;
1739 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1742 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1747 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1751 while (path[0] == '/')
1753 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1754 ignore_whitespace, stdout);
1757 got_object_blob_close(blob1);
1758 got_object_blob_close(blob2);
1762 static const struct got_error *
1763 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1764 const char *path, int diff_context, int ignore_whitespace,
1765 struct got_repository *repo)
1767 const struct got_error *err = NULL;
1768 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1769 struct got_diff_blob_output_unidiff_arg arg;
1772 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1777 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1781 arg.diff_context = diff_context;
1782 arg.ignore_whitespace = ignore_whitespace;
1783 arg.outfile = stdout;
1784 while (path[0] == '/')
1786 err = got_diff_tree(tree1, tree2, path, path, repo,
1787 got_diff_blob_output_unidiff, &arg, 1);
1790 got_object_tree_close(tree1);
1792 got_object_tree_close(tree2);
1796 static const struct got_error *
1797 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1798 const char *path, int diff_context, struct got_repository *repo)
1800 const struct got_error *err = NULL;
1801 struct got_commit_object *pcommit = NULL;
1802 char *id_str1 = NULL, *id_str2 = NULL;
1803 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1804 struct got_object_qid *qid;
1806 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1808 err = got_object_open_as_commit(&pcommit, repo,
1814 if (path && path[0] != '\0') {
1816 err = got_object_id_by_path(&obj_id2, repo, id, path);
1819 err = got_object_id_str(&id_str2, obj_id2);
1825 err = got_object_id_by_path(&obj_id1, repo,
1831 err = got_object_id_str(&id_str1, obj_id1);
1837 err = got_object_get_type(&obj_type, repo, obj_id2);
1842 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1844 case GOT_OBJ_TYPE_BLOB:
1845 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1848 case GOT_OBJ_TYPE_TREE:
1849 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1853 err = got_error(GOT_ERR_OBJ_TYPE);
1859 obj_id2 = got_object_commit_get_tree_id(commit);
1860 err = got_object_id_str(&id_str2, obj_id2);
1863 obj_id1 = got_object_commit_get_tree_id(pcommit);
1864 err = got_object_id_str(&id_str1, obj_id1);
1867 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1868 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1874 got_object_commit_close(pcommit);
1879 get_datestr(time_t *time, char *datebuf)
1881 struct tm mytm, *tm;
1884 tm = gmtime_r(time, &mytm);
1887 s = asctime_r(tm, datebuf);
1890 p = strchr(s, '\n');
1896 static const struct got_error *
1897 match_logmsg(int *have_match, struct got_object_id *id,
1898 struct got_commit_object *commit, regex_t *regex)
1900 const struct got_error *err = NULL;
1901 regmatch_t regmatch;
1902 char *id_str = NULL, *logmsg = NULL;
1906 err = got_object_id_str(&id_str, id);
1910 err = got_object_commit_get_logmsg(&logmsg, commit);
1914 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1922 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1924 static const struct got_error *
1925 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1926 struct got_repository *repo, const char *path, int show_patch,
1927 int diff_context, struct got_reflist_head *refs)
1929 const struct got_error *err = NULL;
1930 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1932 time_t committer_time;
1933 const char *author, *committer;
1934 char *refs_str = NULL;
1935 struct got_reflist_entry *re;
1937 SIMPLEQ_FOREACH(re, refs, entry) {
1940 struct got_tag_object *tag = NULL;
1943 name = got_ref_get_name(re->ref);
1944 if (strcmp(name, GOT_REF_HEAD) == 0)
1946 if (strncmp(name, "refs/", 5) == 0)
1948 if (strncmp(name, "got/", 4) == 0)
1950 if (strncmp(name, "heads/", 6) == 0)
1952 if (strncmp(name, "remotes/", 8) == 0)
1954 if (strncmp(name, "tags/", 5) == 0) {
1955 err = got_object_open_as_tag(&tag, repo, re->id);
1957 if (err->code != GOT_ERR_OBJ_TYPE)
1959 /* Ref points at something other than a tag. */
1964 cmp = got_object_id_cmp(tag ?
1965 got_object_tag_get_object_id(tag) : re->id, id);
1967 got_object_tag_close(tag);
1971 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1973 err = got_error_from_errno("asprintf");
1979 err = got_object_id_str(&id_str, id);
1983 printf(GOT_COMMIT_SEP_STR);
1984 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1985 refs_str ? refs_str : "", refs_str ? ")" : "");
1990 printf("from: %s\n", got_object_commit_get_author(commit));
1991 committer_time = got_object_commit_get_committer_time(commit);
1992 datestr = get_datestr(&committer_time, datebuf);
1994 printf("date: %s UTC\n", datestr);
1995 author = got_object_commit_get_author(commit);
1996 committer = got_object_commit_get_committer(commit);
1997 if (strcmp(author, committer) != 0)
1998 printf("via: %s\n", committer);
1999 if (got_object_commit_get_nparents(commit) > 1) {
2000 const struct got_object_id_queue *parent_ids;
2001 struct got_object_qid *qid;
2003 parent_ids = got_object_commit_get_parent_ids(commit);
2004 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2005 err = got_object_id_str(&id_str, qid->id);
2008 printf("parent %d: %s\n", n++, id_str);
2013 err = got_object_commit_get_logmsg(&logmsg0, commit);
2019 line = strsep(&logmsg, "\n");
2021 printf(" %s\n", line);
2026 err = print_patch(commit, id, path, diff_context, repo);
2031 if (fflush(stdout) != 0 && err == NULL)
2032 err = got_error_from_errno("fflush");
2036 static const struct got_error *
2037 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2038 const char *path, int show_patch, const char *search_pattern,
2039 int diff_context, int limit, int log_branches,
2040 struct got_reflist_head *refs)
2042 const struct got_error *err;
2043 struct got_commit_graph *graph;
2047 if (search_pattern &&
2048 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2049 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2051 err = got_commit_graph_open(&graph, path, !log_branches);
2054 err = got_commit_graph_iter_start(graph, root_id, repo,
2055 check_cancelled, NULL);
2059 struct got_commit_object *commit;
2060 struct got_object_id *id;
2062 if (sigint_received || sigpipe_received)
2065 err = got_commit_graph_iter_next(&id, graph, repo,
2066 check_cancelled, NULL);
2068 if (err->code == GOT_ERR_ITER_COMPLETED)
2075 err = got_object_open_as_commit(&commit, repo, id);
2079 if (search_pattern) {
2080 err = match_logmsg(&have_match, id, commit, ®ex);
2082 got_object_commit_close(commit);
2085 if (have_match == 0) {
2086 got_object_commit_close(commit);
2091 err = print_commit(commit, id, repo, path, show_patch,
2092 diff_context, refs);
2093 got_object_commit_close(commit);
2094 if (err || (limit && --limit == 0))
2100 got_commit_graph_close(graph);
2107 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2108 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2113 get_default_log_limit(void)
2115 const char *got_default_log_limit;
2119 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2120 if (got_default_log_limit == NULL)
2122 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2128 static const struct got_error *
2129 cmd_log(int argc, char *argv[])
2131 const struct got_error *error;
2132 struct got_repository *repo = NULL;
2133 struct got_worktree *worktree = NULL;
2134 struct got_commit_object *commit = NULL;
2135 struct got_object_id *id = NULL;
2136 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2137 const char *start_commit = NULL, *search_pattern = NULL;
2138 int diff_context = -1, ch;
2139 int show_patch = 0, limit = 0, log_branches = 0;
2141 struct got_reflist_head refs;
2143 SIMPLEQ_INIT(&refs);
2146 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2152 limit = get_default_log_limit();
2154 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2160 start_commit = optarg;
2163 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2166 err(1, "-C option %s", errstr);
2169 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2171 err(1, "-l option %s", errstr);
2177 repo_path = realpath(optarg, NULL);
2178 if (repo_path == NULL)
2179 return got_error_from_errno2("realpath",
2181 got_path_strip_trailing_slashes(repo_path);
2184 search_pattern = optarg;
2195 if (diff_context == -1)
2197 else if (!show_patch)
2198 errx(1, "-C reguires -p");
2200 cwd = getcwd(NULL, 0);
2202 error = got_error_from_errno("getcwd");
2206 error = got_worktree_open(&worktree, cwd);
2207 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2214 error = got_error_from_errno("strdup");
2217 } else if (argc == 1) {
2219 error = got_worktree_resolve_path(&path, worktree,
2224 path = strdup(argv[0]);
2226 error = got_error_from_errno("strdup");
2233 if (repo_path == NULL) {
2234 repo_path = worktree ?
2235 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2237 if (repo_path == NULL) {
2238 error = got_error_from_errno("strdup");
2242 error = got_repo_open(&repo, repo_path, NULL);
2246 error = apply_unveil(got_repo_get_path(repo), 1,
2247 worktree ? got_worktree_get_root_path(worktree) : NULL);
2251 if (start_commit == NULL) {
2252 struct got_reference *head_ref;
2253 error = got_ref_open(&head_ref, repo,
2254 worktree ? got_worktree_get_head_ref_name(worktree)
2258 error = got_ref_resolve(&id, repo, head_ref);
2259 got_ref_close(head_ref);
2262 error = got_object_open_as_commit(&commit, repo, id);
2264 struct got_reference *ref;
2265 error = got_ref_open(&ref, repo, start_commit, 0);
2266 if (error == NULL) {
2268 error = got_ref_resolve(&id, repo, ref);
2272 error = got_object_get_type(&obj_type, repo, id);
2275 if (obj_type == GOT_OBJ_TYPE_TAG) {
2276 struct got_tag_object *tag;
2277 error = got_object_open_as_tag(&tag, repo, id);
2280 if (got_object_tag_get_object_type(tag) !=
2281 GOT_OBJ_TYPE_COMMIT) {
2282 got_object_tag_close(tag);
2283 error = got_error(GOT_ERR_OBJ_TYPE);
2287 id = got_object_id_dup(
2288 got_object_tag_get_object_id(tag));
2290 error = got_error_from_errno(
2291 "got_object_id_dup");
2292 got_object_tag_close(tag);
2295 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2296 error = got_error(GOT_ERR_OBJ_TYPE);
2299 error = got_object_open_as_commit(&commit, repo, id);
2303 if (commit == NULL) {
2304 error = got_repo_match_object_id_prefix(&id,
2305 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2314 const char *prefix = got_worktree_get_path_prefix(worktree);
2316 if (asprintf(&p, "%s%s%s", prefix,
2317 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2318 error = got_error_from_errno("asprintf");
2321 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2324 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2329 path = in_repo_path;
2332 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2336 error = print_commits(id, repo, path, show_patch, search_pattern,
2337 diff_context, limit, log_branches, &refs);
2344 got_worktree_close(worktree);
2346 const struct got_error *repo_error;
2347 repo_error = got_repo_close(repo);
2351 got_ref_list_free(&refs);
2358 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2359 "[-w] [object1 object2 | path]\n", getprogname());
2363 struct print_diff_arg {
2364 struct got_repository *repo;
2365 struct got_worktree *worktree;
2370 int ignore_whitespace;
2373 static const struct got_error *
2374 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2375 const char *path, struct got_object_id *blob_id,
2376 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2377 int dirfd, const char *de_name)
2379 struct print_diff_arg *a = arg;
2380 const struct got_error *err = NULL;
2381 struct got_blob_object *blob1 = NULL;
2384 char *abspath = NULL, *label1 = NULL;
2387 if (a->diff_staged) {
2388 if (staged_status != GOT_STATUS_MODIFY &&
2389 staged_status != GOT_STATUS_ADD &&
2390 staged_status != GOT_STATUS_DELETE)
2393 if (staged_status == GOT_STATUS_DELETE)
2395 if (status == GOT_STATUS_NONEXISTENT)
2396 return got_error_set_errno(ENOENT, path);
2397 if (status != GOT_STATUS_MODIFY &&
2398 status != GOT_STATUS_ADD &&
2399 status != GOT_STATUS_DELETE &&
2400 status != GOT_STATUS_CONFLICT)
2404 if (!a->header_shown) {
2405 printf("diff %s %s%s\n", a->id_str,
2406 got_worktree_get_root_path(a->worktree),
2407 a->diff_staged ? " (staged changes)" : "");
2408 a->header_shown = 1;
2411 if (a->diff_staged) {
2412 const char *label1 = NULL, *label2 = NULL;
2413 switch (staged_status) {
2414 case GOT_STATUS_MODIFY:
2418 case GOT_STATUS_ADD:
2421 case GOT_STATUS_DELETE:
2425 return got_error(GOT_ERR_FILE_STATUS);
2427 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2428 label1, label2, a->diff_context, a->ignore_whitespace,
2432 if (staged_status == GOT_STATUS_ADD ||
2433 staged_status == GOT_STATUS_MODIFY) {
2435 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2439 err = got_object_id_str(&id_str, staged_blob_id);
2442 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2443 err = got_error_from_errno("asprintf");
2448 } else if (status != GOT_STATUS_ADD) {
2449 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2454 if (status != GOT_STATUS_DELETE) {
2455 if (asprintf(&abspath, "%s/%s",
2456 got_worktree_get_root_path(a->worktree), path) == -1) {
2457 err = got_error_from_errno("asprintf");
2462 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2464 err = got_error_from_errno2("openat", abspath);
2468 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2470 err = got_error_from_errno2("open", abspath);
2474 if (fstat(fd, &sb) == -1) {
2475 err = got_error_from_errno2("fstat", abspath);
2478 f2 = fdopen(fd, "r");
2480 err = got_error_from_errno2("fdopen", abspath);
2487 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2488 a->diff_context, a->ignore_whitespace, stdout);
2491 got_object_blob_close(blob1);
2492 if (f2 && fclose(f2) == EOF && err == NULL)
2493 err = got_error_from_errno("fclose");
2494 if (fd != -1 && close(fd) == -1 && err == NULL)
2495 err = got_error_from_errno("close");
2500 static const struct got_error *
2501 cmd_diff(int argc, char *argv[])
2503 const struct got_error *error;
2504 struct got_repository *repo = NULL;
2505 struct got_worktree *worktree = NULL;
2506 char *cwd = NULL, *repo_path = NULL;
2507 struct got_object_id *id1 = NULL, *id2 = NULL;
2508 const char *id_str1 = NULL, *id_str2 = NULL;
2509 char *label1 = NULL, *label2 = NULL;
2511 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2516 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2521 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2524 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2527 err(1, "-C option %s", errstr);
2530 repo_path = realpath(optarg, NULL);
2531 if (repo_path == NULL)
2532 return got_error_from_errno2("realpath",
2534 got_path_strip_trailing_slashes(repo_path);
2540 ignore_whitespace = 1;
2551 cwd = getcwd(NULL, 0);
2553 error = got_error_from_errno("getcwd");
2556 error = got_worktree_open(&worktree, cwd);
2557 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2560 if (worktree == NULL) {
2561 error = got_error(GOT_ERR_NOT_WORKTREE);
2566 "-r option can't be used when diffing a work tree");
2567 repo_path = strdup(got_worktree_get_repo_path(worktree));
2568 if (repo_path == NULL) {
2569 error = got_error_from_errno("strdup");
2573 error = got_worktree_resolve_path(&path, worktree,
2580 error = got_error_from_errno("strdup");
2584 } else if (argc == 2) {
2586 errx(1, "-s option can't be used when diffing "
2587 "objects in repository");
2590 if (worktree && repo_path == NULL) {
2592 strdup(got_worktree_get_repo_path(worktree));
2593 if (repo_path == NULL) {
2594 error = got_error_from_errno("strdup");
2601 if (repo_path == NULL) {
2602 repo_path = getcwd(NULL, 0);
2603 if (repo_path == NULL)
2604 return got_error_from_errno("getcwd");
2607 error = got_repo_open(&repo, repo_path, NULL);
2612 error = apply_unveil(got_repo_get_path(repo), 1,
2613 worktree ? got_worktree_get_root_path(worktree) : NULL);
2618 struct print_diff_arg arg;
2619 struct got_pathlist_head paths;
2624 error = got_object_id_str(&id_str,
2625 got_worktree_get_base_commit_id(worktree));
2629 arg.worktree = worktree;
2630 arg.diff_context = diff_context;
2631 arg.id_str = id_str;
2632 arg.header_shown = 0;
2633 arg.diff_staged = diff_staged;
2634 arg.ignore_whitespace = ignore_whitespace;
2636 error = got_pathlist_append(&paths, path, NULL);
2640 error = got_worktree_status(worktree, &paths, repo, print_diff,
2641 &arg, check_cancelled, NULL);
2643 got_pathlist_free(&paths);
2647 error = got_repo_match_object_id(&id1, &label1, id_str1,
2648 GOT_OBJ_TYPE_ANY, 1, repo);
2652 error = got_repo_match_object_id(&id2, &label2, id_str2,
2653 GOT_OBJ_TYPE_ANY, 1, repo);
2657 error = got_object_get_type(&type1, repo, id1);
2661 error = got_object_get_type(&type2, repo, id2);
2665 if (type1 != type2) {
2666 error = got_error(GOT_ERR_OBJ_TYPE);
2671 case GOT_OBJ_TYPE_BLOB:
2672 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2673 diff_context, ignore_whitespace, repo, stdout);
2675 case GOT_OBJ_TYPE_TREE:
2676 error = got_diff_objects_as_trees(id1, id2, "", "",
2677 diff_context, ignore_whitespace, repo, stdout);
2679 case GOT_OBJ_TYPE_COMMIT:
2680 printf("diff %s %s\n", label1, label2);
2681 error = got_diff_objects_as_commits(id1, id2, diff_context,
2682 ignore_whitespace, repo, stdout);
2685 error = got_error(GOT_ERR_OBJ_TYPE);
2694 got_worktree_close(worktree);
2696 const struct got_error *repo_error;
2697 repo_error = got_repo_close(repo);
2708 "usage: %s blame [-c commit] [-r repository-path] path\n",
2717 char datebuf[11]; /* YYYY-MM-DD + NUL */
2720 struct blame_cb_args {
2721 struct blame_line *lines;
2725 off_t *line_offsets;
2727 struct got_repository *repo;
2730 static const struct got_error *
2731 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2733 const struct got_error *err = NULL;
2734 struct blame_cb_args *a = arg;
2735 struct blame_line *bline;
2737 size_t linesize = 0;
2738 struct got_commit_object *commit = NULL;
2741 time_t committer_time;
2743 if (nlines != a->nlines ||
2744 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2745 return got_error(GOT_ERR_RANGE);
2747 if (sigint_received)
2748 return got_error(GOT_ERR_ITER_COMPLETED);
2751 return NULL; /* no change in this commit */
2753 /* Annotate this line. */
2754 bline = &a->lines[lineno - 1];
2755 if (bline->annotated)
2757 err = got_object_id_str(&bline->id_str, id);
2761 err = got_object_open_as_commit(&commit, a->repo, id);
2765 bline->committer = strdup(got_object_commit_get_committer(commit));
2766 if (bline->committer == NULL) {
2767 err = got_error_from_errno("strdup");
2771 committer_time = got_object_commit_get_committer_time(commit);
2772 if (localtime_r(&committer_time, &tm) == NULL)
2773 return got_error_from_errno("localtime_r");
2774 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2775 &tm) >= sizeof(bline->datebuf)) {
2776 err = got_error(GOT_ERR_NO_SPACE);
2779 bline->annotated = 1;
2781 /* Print lines annotated so far. */
2782 bline = &a->lines[a->lineno_cur - 1];
2783 if (!bline->annotated)
2786 offset = a->line_offsets[a->lineno_cur - 1];
2787 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2788 err = got_error_from_errno("fseeko");
2792 while (bline->annotated) {
2793 char *smallerthan, *at, *nl, *committer;
2796 if (getline(&line, &linesize, a->f) == -1) {
2798 err = got_error_from_errno("getline");
2802 committer = bline->committer;
2803 smallerthan = strchr(committer, '<');
2804 if (smallerthan && smallerthan[1] != '\0')
2805 committer = smallerthan + 1;
2806 at = strchr(committer, '@');
2809 len = strlen(committer);
2811 committer[8] = '\0';
2813 nl = strchr(line, '\n');
2816 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2817 bline->id_str, bline->datebuf, committer, line);
2820 bline = &a->lines[a->lineno_cur - 1];
2824 got_object_commit_close(commit);
2829 static const struct got_error *
2830 cmd_blame(int argc, char *argv[])
2832 const struct got_error *error;
2833 struct got_repository *repo = NULL;
2834 struct got_worktree *worktree = NULL;
2835 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2836 struct got_object_id *obj_id = NULL;
2837 struct got_object_id *commit_id = NULL;
2838 struct got_blob_object *blob = NULL;
2839 char *commit_id_str = NULL;
2840 struct blame_cb_args bca;
2841 int ch, obj_type, i;
2844 memset(&bca, 0, sizeof(bca));
2847 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2852 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2855 commit_id_str = optarg;
2858 repo_path = realpath(optarg, NULL);
2859 if (repo_path == NULL)
2860 return got_error_from_errno2("realpath",
2862 got_path_strip_trailing_slashes(repo_path);
2878 cwd = getcwd(NULL, 0);
2880 error = got_error_from_errno("getcwd");
2883 if (repo_path == NULL) {
2884 error = got_worktree_open(&worktree, cwd);
2885 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2891 strdup(got_worktree_get_repo_path(worktree));
2892 if (repo_path == NULL)
2893 error = got_error_from_errno("strdup");
2897 repo_path = strdup(cwd);
2898 if (repo_path == NULL) {
2899 error = got_error_from_errno("strdup");
2905 error = got_repo_open(&repo, repo_path, NULL);
2909 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2914 const char *prefix = got_worktree_get_path_prefix(worktree);
2915 char *p, *worktree_subdir = cwd +
2916 strlen(got_worktree_get_root_path(worktree));
2917 if (asprintf(&p, "%s%s%s%s%s",
2918 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2919 worktree_subdir, worktree_subdir[0] ? "/" : "",
2921 error = got_error_from_errno("asprintf");
2924 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2927 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2932 if (commit_id_str == NULL) {
2933 struct got_reference *head_ref;
2934 error = got_ref_open(&head_ref, repo, worktree ?
2935 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2938 error = got_ref_resolve(&commit_id, repo, head_ref);
2939 got_ref_close(head_ref);
2943 error = got_repo_match_object_id(&commit_id, NULL,
2944 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2949 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2953 error = got_object_get_type(&obj_type, repo, obj_id);
2957 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2958 error = got_error(GOT_ERR_OBJ_TYPE);
2962 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2965 bca.f = got_opentemp();
2966 if (bca.f == NULL) {
2967 error = got_error_from_errno("got_opentemp");
2970 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2971 &bca.line_offsets, bca.f, blob);
2972 if (error || bca.nlines == 0)
2975 /* Don't include \n at EOF in the blame line count. */
2976 if (bca.line_offsets[bca.nlines - 1] == filesize)
2979 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2980 if (bca.lines == NULL) {
2981 error = got_error_from_errno("calloc");
2985 bca.nlines_prec = 0;
2993 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2994 check_cancelled, NULL);
3002 got_object_blob_close(blob);
3004 got_worktree_close(worktree);
3006 const struct got_error *repo_error;
3007 repo_error = got_repo_close(repo);
3012 for (i = 0; i < bca.nlines; i++) {
3013 struct blame_line *bline = &bca.lines[i];
3014 free(bline->id_str);
3015 free(bline->committer);
3019 free(bca.line_offsets);
3020 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3021 error = got_error_from_errno("fclose");
3029 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3035 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3036 const char *root_path)
3038 int is_root_path = (strcmp(path, root_path) == 0);
3039 const char *modestr = "";
3040 mode_t mode = got_tree_entry_get_mode(te);
3042 path += strlen(root_path);
3043 while (path[0] == '/')
3046 if (got_object_tree_entry_is_submodule(te))
3048 else if (S_ISLNK(mode))
3050 else if (S_ISDIR(mode))
3052 else if (mode & S_IXUSR)
3055 printf("%s%s%s%s%s\n", id ? id : "", path,
3056 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3059 static const struct got_error *
3060 print_tree(const char *path, struct got_object_id *commit_id,
3061 int show_ids, int recurse, const char *root_path,
3062 struct got_repository *repo)
3064 const struct got_error *err = NULL;
3065 struct got_object_id *tree_id = NULL;
3066 struct got_tree_object *tree = NULL;
3069 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3073 err = got_object_open_as_tree(&tree, repo, tree_id);
3076 nentries = got_object_tree_get_nentries(tree);
3077 for (i = 0; i < nentries; i++) {
3078 struct got_tree_entry *te;
3081 if (sigint_received || sigpipe_received)
3084 te = got_object_tree_get_entry(tree, i);
3087 err = got_object_id_str(&id_str,
3088 got_tree_entry_get_id(te));
3091 if (asprintf(&id, "%s ", id_str) == -1) {
3092 err = got_error_from_errno("asprintf");
3098 print_entry(te, id, path, root_path);
3101 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3103 if (asprintf(&child_path, "%s%s%s", path,
3104 path[0] == '/' && path[1] == '\0' ? "" : "/",
3105 got_tree_entry_get_name(te)) == -1) {
3106 err = got_error_from_errno("asprintf");
3109 err = print_tree(child_path, commit_id, show_ids, 1,
3118 got_object_tree_close(tree);
3123 static const struct got_error *
3124 cmd_tree(int argc, char *argv[])
3126 const struct got_error *error;
3127 struct got_repository *repo = NULL;
3128 struct got_worktree *worktree = NULL;
3130 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3131 struct got_object_id *commit_id = NULL;
3132 char *commit_id_str = NULL;
3133 int show_ids = 0, recurse = 0;
3137 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3142 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3145 commit_id_str = optarg;
3148 repo_path = realpath(optarg, NULL);
3149 if (repo_path == NULL)
3150 return got_error_from_errno2("realpath",
3152 got_path_strip_trailing_slashes(repo_path);
3176 cwd = getcwd(NULL, 0);
3178 error = got_error_from_errno("getcwd");
3181 if (repo_path == NULL) {
3182 error = got_worktree_open(&worktree, cwd);
3183 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3189 strdup(got_worktree_get_repo_path(worktree));
3190 if (repo_path == NULL)
3191 error = got_error_from_errno("strdup");
3195 repo_path = strdup(cwd);
3196 if (repo_path == NULL) {
3197 error = got_error_from_errno("strdup");
3203 error = got_repo_open(&repo, repo_path, NULL);
3207 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3213 char *p, *worktree_subdir = cwd +
3214 strlen(got_worktree_get_root_path(worktree));
3215 if (asprintf(&p, "%s/%s",
3216 got_worktree_get_path_prefix(worktree),
3217 worktree_subdir) == -1) {
3218 error = got_error_from_errno("asprintf");
3221 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3228 if (in_repo_path == NULL) {
3229 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3234 if (commit_id_str == NULL) {
3235 struct got_reference *head_ref;
3236 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3239 error = got_ref_resolve(&commit_id, repo, head_ref);
3240 got_ref_close(head_ref);
3244 error = got_repo_match_object_id(&commit_id, NULL,
3245 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3250 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3251 in_repo_path, repo);
3258 got_worktree_close(worktree);
3260 const struct got_error *repo_error;
3261 repo_error = got_repo_close(repo);
3271 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3275 static const struct got_error *
3276 print_status(void *arg, unsigned char status, unsigned char staged_status,
3277 const char *path, struct got_object_id *blob_id,
3278 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3279 int dirfd, const char *de_name)
3281 if (status == staged_status && (status == GOT_STATUS_DELETE))
3282 status = GOT_STATUS_NO_CHANGE;
3283 printf("%c%c %s\n", status, staged_status, path);
3287 static const struct got_error *
3288 cmd_status(int argc, char *argv[])
3290 const struct got_error *error = NULL;
3291 struct got_repository *repo = NULL;
3292 struct got_worktree *worktree = NULL;
3294 struct got_pathlist_head paths;
3295 struct got_pathlist_entry *pe;
3300 while ((ch = getopt(argc, argv, "")) != -1) {
3312 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3316 cwd = getcwd(NULL, 0);
3318 error = got_error_from_errno("getcwd");
3322 error = got_worktree_open(&worktree, cwd);
3326 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3331 error = apply_unveil(got_repo_get_path(repo), 1,
3332 got_worktree_get_root_path(worktree));
3336 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3340 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3341 check_cancelled, NULL);
3343 TAILQ_FOREACH(pe, &paths, entry)
3344 free((char *)pe->path);
3345 got_pathlist_free(&paths);
3354 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3359 static const struct got_error *
3360 list_refs(struct got_repository *repo)
3362 static const struct got_error *err = NULL;
3363 struct got_reflist_head refs;
3364 struct got_reflist_entry *re;
3366 SIMPLEQ_INIT(&refs);
3367 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3371 SIMPLEQ_FOREACH(re, &refs, entry) {
3373 refstr = got_ref_to_str(re->ref);
3375 return got_error_from_errno("got_ref_to_str");
3376 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3380 got_ref_list_free(&refs);
3384 static const struct got_error *
3385 delete_ref(struct got_repository *repo, const char *refname)
3387 const struct got_error *err = NULL;
3388 struct got_reference *ref;
3390 err = got_ref_open(&ref, repo, refname, 0);
3394 err = got_ref_delete(ref, repo);
3399 static const struct got_error *
3400 add_ref(struct got_repository *repo, const char *refname, const char *target)
3402 const struct got_error *err = NULL;
3403 struct got_object_id *id;
3404 struct got_reference *ref = NULL;
3407 * Don't let the user create a reference name with a leading '-'.
3408 * While technically a valid reference name, this case is usually
3409 * an unintended typo.
3411 if (refname[0] == '-')
3412 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3414 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3417 struct got_reference *target_ref;
3419 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3421 err = got_ref_open(&target_ref, repo, target, 0);
3424 err = got_ref_resolve(&id, repo, target_ref);
3425 got_ref_close(target_ref);
3430 err = got_ref_alloc(&ref, refname, id);
3434 err = got_ref_write(ref, repo);
3442 static const struct got_error *
3443 add_symref(struct got_repository *repo, const char *refname, const char *target)
3445 const struct got_error *err = NULL;
3446 struct got_reference *ref = NULL;
3447 struct got_reference *target_ref = NULL;
3450 * Don't let the user create a reference name with a leading '-'.
3451 * While technically a valid reference name, this case is usually
3452 * an unintended typo.
3454 if (refname[0] == '-')
3455 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3457 err = got_ref_open(&target_ref, repo, target, 0);
3461 err = got_ref_alloc_symref(&ref, refname, target_ref);
3465 err = got_ref_write(ref, repo);
3468 got_ref_close(target_ref);
3474 static const struct got_error *
3475 cmd_ref(int argc, char *argv[])
3477 const struct got_error *error = NULL;
3478 struct got_repository *repo = NULL;
3479 struct got_worktree *worktree = NULL;
3480 char *cwd = NULL, *repo_path = NULL;
3481 int ch, do_list = 0, create_symref = 0;
3482 const char *delref = NULL;
3484 /* TODO: Add -s option for adding symbolic references. */
3485 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3491 repo_path = realpath(optarg, NULL);
3492 if (repo_path == NULL)
3493 return got_error_from_errno2("realpath",
3495 got_path_strip_trailing_slashes(repo_path);
3509 if (do_list && delref)
3510 errx(1, "-l and -d options are mutually exclusive\n");
3515 if (do_list || delref) {
3517 errx(1, "-s option cannot be used together with the "
3518 "-l or -d options");
3521 } else if (argc != 2)
3526 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3530 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3531 "sendfd unveil", NULL) == -1)
3535 cwd = getcwd(NULL, 0);
3537 error = got_error_from_errno("getcwd");
3541 if (repo_path == NULL) {
3542 error = got_worktree_open(&worktree, cwd);
3543 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3549 strdup(got_worktree_get_repo_path(worktree));
3550 if (repo_path == NULL)
3551 error = got_error_from_errno("strdup");
3555 repo_path = strdup(cwd);
3556 if (repo_path == NULL) {
3557 error = got_error_from_errno("strdup");
3563 error = got_repo_open(&repo, repo_path, NULL);
3567 error = apply_unveil(got_repo_get_path(repo), do_list,
3568 worktree ? got_worktree_get_root_path(worktree) : NULL);
3573 error = list_refs(repo);
3575 error = delete_ref(repo, delref);
3576 else if (create_symref)
3577 error = add_symref(repo, argv[0], argv[1]);
3579 error = add_ref(repo, argv[0], argv[1]);
3582 got_repo_close(repo);
3584 got_worktree_close(worktree);
3594 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3595 "[name]\n", getprogname());
3599 static const struct got_error *
3600 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3601 struct got_reference *ref)
3603 const struct got_error *err = NULL;
3604 const char *refname, *marker = " ";
3607 refname = got_ref_get_name(ref);
3608 if (worktree && strcmp(refname,
3609 got_worktree_get_head_ref_name(worktree)) == 0) {
3610 struct got_object_id *id = NULL;
3612 err = got_ref_resolve(&id, repo, ref);
3615 if (got_object_id_cmp(id,
3616 got_worktree_get_base_commit_id(worktree)) == 0)
3623 if (strncmp(refname, "refs/heads/", 11) == 0)
3625 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3628 refstr = got_ref_to_str(ref);
3630 return got_error_from_errno("got_ref_to_str");
3632 printf("%s%s: %s\n", marker, refname, refstr);
3637 static const struct got_error *
3638 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3640 const char *refname;
3642 if (worktree == NULL)
3643 return got_error(GOT_ERR_NOT_WORKTREE);
3645 refname = got_worktree_get_head_ref_name(worktree);
3647 if (strncmp(refname, "refs/heads/", 11) == 0)
3649 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3652 printf("%s\n", refname);
3657 static const struct got_error *
3658 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3660 static const struct got_error *err = NULL;
3661 struct got_reflist_head refs;
3662 struct got_reflist_entry *re;
3663 struct got_reference *temp_ref = NULL;
3664 int rebase_in_progress, histedit_in_progress;
3666 SIMPLEQ_INIT(&refs);
3669 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3674 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3679 if (rebase_in_progress || histedit_in_progress) {
3680 err = got_ref_open(&temp_ref, repo,
3681 got_worktree_get_head_ref_name(worktree), 0);
3684 list_branch(repo, worktree, temp_ref);
3685 got_ref_close(temp_ref);
3689 err = got_ref_list(&refs, repo, "refs/heads",
3690 got_ref_cmp_by_name, NULL);
3694 SIMPLEQ_FOREACH(re, &refs, entry)
3695 list_branch(repo, worktree, re->ref);
3697 got_ref_list_free(&refs);
3701 static const struct got_error *
3702 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3703 const char *branch_name)
3705 const struct got_error *err = NULL;
3706 struct got_reference *ref = NULL;
3709 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3710 return got_error_from_errno("asprintf");
3712 err = got_ref_open(&ref, repo, refname, 0);
3717 strcmp(got_worktree_get_head_ref_name(worktree),
3718 got_ref_get_name(ref)) == 0) {
3719 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3720 "will not delete this work tree's current branch");
3724 err = got_ref_delete(ref, repo);
3732 static const struct got_error *
3733 add_branch(struct got_repository *repo, const char *branch_name,
3734 struct got_object_id *base_commit_id)
3736 const struct got_error *err = NULL;
3737 struct got_reference *ref = NULL;
3738 char *base_refname = NULL, *refname = NULL;
3741 * Don't let the user create a branch name with a leading '-'.
3742 * While technically a valid reference name, this case is usually
3743 * an unintended typo.
3745 if (branch_name[0] == '-')
3746 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3748 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3749 err = got_error_from_errno("asprintf");
3753 err = got_ref_open(&ref, repo, refname, 0);
3755 err = got_error(GOT_ERR_BRANCH_EXISTS);
3757 } else if (err->code != GOT_ERR_NOT_REF)
3760 err = got_ref_alloc(&ref, refname, base_commit_id);
3764 err = got_ref_write(ref, repo);
3773 static const struct got_error *
3774 cmd_branch(int argc, char *argv[])
3776 const struct got_error *error = NULL;
3777 struct got_repository *repo = NULL;
3778 struct got_worktree *worktree = NULL;
3779 char *cwd = NULL, *repo_path = NULL;
3780 int ch, do_list = 0, do_show = 0, do_update = 1;
3781 const char *delref = NULL, *commit_id_arg = NULL;
3782 struct got_reference *ref = NULL;
3783 struct got_pathlist_head paths;
3784 struct got_pathlist_entry *pe;
3785 struct got_object_id *commit_id = NULL;
3786 char *commit_id_str = NULL;
3790 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3793 commit_id_arg = optarg;
3799 repo_path = realpath(optarg, NULL);
3800 if (repo_path == NULL)
3801 return got_error_from_errno2("realpath",
3803 got_path_strip_trailing_slashes(repo_path);
3817 if (do_list && delref)
3818 errx(1, "-l and -d options are mutually exclusive\n");
3823 if (!do_list && !delref && argc == 0)
3826 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3827 errx(1, "-c option can only be used when creating a branch");
3829 if (do_list || delref) {
3832 } else if (!do_show && argc != 1)
3836 if (do_list || do_show) {
3837 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3841 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3842 "sendfd unveil", NULL) == -1)
3846 cwd = getcwd(NULL, 0);
3848 error = got_error_from_errno("getcwd");
3852 if (repo_path == NULL) {
3853 error = got_worktree_open(&worktree, cwd);
3854 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3860 strdup(got_worktree_get_repo_path(worktree));
3861 if (repo_path == NULL)
3862 error = got_error_from_errno("strdup");
3866 repo_path = strdup(cwd);
3867 if (repo_path == NULL) {
3868 error = got_error_from_errno("strdup");
3874 error = got_repo_open(&repo, repo_path, NULL);
3878 error = apply_unveil(got_repo_get_path(repo), do_list,
3879 worktree ? got_worktree_get_root_path(worktree) : NULL);
3884 error = show_current_branch(repo, worktree);
3886 error = list_branches(repo, worktree);
3888 error = delete_branch(repo, worktree, delref);
3890 if (commit_id_arg == NULL)
3891 commit_id_arg = worktree ?
3892 got_worktree_get_head_ref_name(worktree) :
3894 error = got_repo_match_object_id(&commit_id, NULL,
3895 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3898 error = add_branch(repo, argv[0], commit_id);
3901 if (worktree && do_update) {
3902 int did_something = 0;
3903 char *branch_refname = NULL;
3905 error = got_object_id_str(&commit_id_str, commit_id);
3908 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3912 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3914 error = got_error_from_errno("asprintf");
3917 error = got_ref_open(&ref, repo, branch_refname, 0);
3918 free(branch_refname);
3921 error = switch_head_ref(ref, commit_id, worktree,
3925 error = got_worktree_set_base_commit_id(worktree, repo,
3929 error = got_worktree_checkout_files(worktree, &paths,
3930 repo, update_progress, &did_something,
3931 check_cancelled, NULL);
3935 printf("Updated to commit %s\n", commit_id_str);
3942 got_repo_close(repo);
3944 got_worktree_close(worktree);
3948 free(commit_id_str);
3949 TAILQ_FOREACH(pe, &paths, entry)
3950 free((char *)pe->path);
3951 got_pathlist_free(&paths);
3960 "usage: %s tag [-c commit] [-r repository] [-l] "
3961 "[-m message] name\n", getprogname());
3966 static const struct got_error *
3967 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3969 const struct got_error *err = NULL;
3970 struct got_reflist_entry *re, *se, *new;
3971 struct got_object_id *re_id, *se_id;
3972 struct got_tag_object *re_tag, *se_tag;
3973 time_t re_time, se_time;
3975 SIMPLEQ_FOREACH(re, tags, entry) {
3976 se = SIMPLEQ_FIRST(sorted);
3978 err = got_reflist_entry_dup(&new, re);
3981 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3984 err = got_ref_resolve(&re_id, repo, re->ref);
3987 err = got_object_open_as_tag(&re_tag, repo, re_id);
3991 re_time = got_object_tag_get_tagger_time(re_tag);
3992 got_object_tag_close(re_tag);
3996 err = got_ref_resolve(&se_id, repo, re->ref);
3999 err = got_object_open_as_tag(&se_tag, repo, se_id);
4003 se_time = got_object_tag_get_tagger_time(se_tag);
4004 got_object_tag_close(se_tag);
4006 if (se_time > re_time) {
4007 err = got_reflist_entry_dup(&new, re);
4010 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4013 se = SIMPLEQ_NEXT(se, entry);
4022 static const struct got_error *
4023 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4025 static const struct got_error *err = NULL;
4026 struct got_reflist_head refs;
4027 struct got_reflist_entry *re;
4029 SIMPLEQ_INIT(&refs);
4031 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4035 SIMPLEQ_FOREACH(re, &refs, entry) {
4036 const char *refname;
4037 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4041 struct got_object_id *id;
4042 struct got_tag_object *tag;
4043 struct got_commit_object *commit = NULL;
4045 refname = got_ref_get_name(re->ref);
4046 if (strncmp(refname, "refs/tags/", 10) != 0)
4049 refstr = got_ref_to_str(re->ref);
4050 if (refstr == NULL) {
4051 err = got_error_from_errno("got_ref_to_str");
4054 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4057 err = got_ref_resolve(&id, repo, re->ref);
4060 err = got_object_open_as_tag(&tag, repo, id);
4062 if (err->code != GOT_ERR_OBJ_TYPE) {
4066 /* "lightweight" tag */
4067 err = got_object_open_as_commit(&commit, repo, id);
4072 tagger = got_object_commit_get_committer(commit);
4074 got_object_commit_get_committer_time(commit);
4075 err = got_object_id_str(&id_str, id);
4081 tagger = got_object_tag_get_tagger(tag);
4082 tagger_time = got_object_tag_get_tagger_time(tag);
4083 err = got_object_id_str(&id_str,
4084 got_object_tag_get_object_id(tag));
4088 printf("from: %s\n", tagger);
4089 datestr = get_datestr(&tagger_time, datebuf);
4091 printf("date: %s UTC\n", datestr);
4093 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4095 switch (got_object_tag_get_object_type(tag)) {
4096 case GOT_OBJ_TYPE_BLOB:
4097 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4100 case GOT_OBJ_TYPE_TREE:
4101 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4104 case GOT_OBJ_TYPE_COMMIT:
4105 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4108 case GOT_OBJ_TYPE_TAG:
4109 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4118 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4121 got_object_commit_close(commit);
4123 tagmsg0 = strdup(got_object_tag_get_message(tag));
4124 got_object_tag_close(tag);
4125 if (tagmsg0 == NULL) {
4126 err = got_error_from_errno("strdup");
4133 line = strsep(&tagmsg, "\n");
4135 printf(" %s\n", line);
4140 got_ref_list_free(&refs);
4144 static const struct got_error *
4145 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4146 const char *tag_name, const char *repo_path)
4148 const struct got_error *err = NULL;
4149 char *template = NULL, *initial_content = NULL;
4150 char *editor = NULL;
4153 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4154 err = got_error_from_errno("asprintf");
4158 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4159 commit_id_str, tag_name) == -1) {
4160 err = got_error_from_errno("asprintf");
4164 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4168 dprintf(fd, initial_content);
4171 err = get_editor(&editor);
4174 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4176 free(initial_content);
4180 /* Editor is done; we can now apply unveil(2) */
4182 err = apply_unveil(repo_path, 0, NULL);
4191 static const struct got_error *
4192 add_tag(struct got_repository *repo, const char *tag_name,
4193 const char *commit_arg, const char *tagmsg_arg)
4195 const struct got_error *err = NULL;
4196 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4197 char *label = NULL, *commit_id_str = NULL;
4198 struct got_reference *ref = NULL;
4199 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4200 char *tagmsg_path = NULL, *tag_id_str = NULL;
4201 int preserve_tagmsg = 0;
4204 * Don't let the user create a tag name with a leading '-'.
4205 * While technically a valid reference name, this case is usually
4206 * an unintended typo.
4208 if (tag_name[0] == '-')
4209 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4211 err = get_author(&tagger, repo);
4215 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4216 GOT_OBJ_TYPE_COMMIT, 1, repo);
4220 err = got_object_id_str(&commit_id_str, commit_id);
4224 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4225 refname = strdup(tag_name);
4226 if (refname == NULL) {
4227 err = got_error_from_errno("strdup");
4231 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4232 err = got_error_from_errno("asprintf");
4236 err = got_ref_open(&ref, repo, refname, 0);
4238 err = got_error(GOT_ERR_TAG_EXISTS);
4240 } else if (err->code != GOT_ERR_NOT_REF)
4243 if (tagmsg_arg == NULL) {
4244 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4245 tag_name, got_repo_get_path(repo));
4247 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4248 tagmsg_path != NULL)
4249 preserve_tagmsg = 1;
4254 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4255 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4258 preserve_tagmsg = 1;
4262 err = got_ref_alloc(&ref, refname, tag_id);
4265 preserve_tagmsg = 1;
4269 err = got_ref_write(ref, repo);
4272 preserve_tagmsg = 1;
4276 err = got_object_id_str(&tag_id_str, tag_id);
4279 preserve_tagmsg = 1;
4282 printf("Created tag %s\n", tag_id_str);
4284 if (preserve_tagmsg) {
4285 fprintf(stderr, "%s: tag message preserved in %s\n",
4286 getprogname(), tagmsg_path);
4287 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4288 err = got_error_from_errno2("unlink", tagmsg_path);
4293 free(commit_id_str);
4301 static const struct got_error *
4302 cmd_tag(int argc, char *argv[])
4304 const struct got_error *error = NULL;
4305 struct got_repository *repo = NULL;
4306 struct got_worktree *worktree = NULL;
4307 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4308 char *gitconfig_path = NULL;
4309 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4310 int ch, do_list = 0;
4312 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4315 commit_id_arg = optarg;
4321 repo_path = realpath(optarg, NULL);
4322 if (repo_path == NULL)
4323 return got_error_from_errno2("realpath",
4325 got_path_strip_trailing_slashes(repo_path);
4340 if (commit_id_arg != NULL)
4341 errx(1, "-c option can only be used when creating a tag");
4343 errx(1, "-l and -m options are mutually exclusive");
4346 } else if (argc != 1)
4353 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4357 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4358 "sendfd unveil", NULL) == -1)
4362 cwd = getcwd(NULL, 0);
4364 error = got_error_from_errno("getcwd");
4368 if (repo_path == NULL) {
4369 error = got_worktree_open(&worktree, cwd);
4370 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4376 strdup(got_worktree_get_repo_path(worktree));
4377 if (repo_path == NULL)
4378 error = got_error_from_errno("strdup");
4382 repo_path = strdup(cwd);
4383 if (repo_path == NULL) {
4384 error = got_error_from_errno("strdup");
4391 error = got_repo_open(&repo, repo_path, NULL);
4394 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4397 error = list_tags(repo, worktree);
4399 error = get_gitconfig_path(&gitconfig_path);
4402 error = got_repo_open(&repo, repo_path, gitconfig_path);
4407 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4412 if (commit_id_arg == NULL) {
4413 struct got_reference *head_ref;
4414 struct got_object_id *commit_id;
4415 error = got_ref_open(&head_ref, repo,
4416 worktree ? got_worktree_get_head_ref_name(worktree)
4420 error = got_ref_resolve(&commit_id, repo, head_ref);
4421 got_ref_close(head_ref);
4424 error = got_object_id_str(&commit_id_str, commit_id);
4430 error = add_tag(repo, tag_name,
4431 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4435 got_repo_close(repo);
4437 got_worktree_close(worktree);
4440 free(gitconfig_path);
4441 free(commit_id_str);
4448 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4453 static const struct got_error *
4454 add_progress(void *arg, unsigned char status, const char *path)
4456 while (path[0] == '/')
4458 printf("%c %s\n", status, path);
4462 static const struct got_error *
4463 cmd_add(int argc, char *argv[])
4465 const struct got_error *error = NULL;
4466 struct got_repository *repo = NULL;
4467 struct got_worktree *worktree = NULL;
4469 struct got_pathlist_head paths;
4470 struct got_pathlist_entry *pe;
4471 int ch, can_recurse = 0, no_ignores = 0;
4475 while ((ch = getopt(argc, argv, "IR")) != -1) {
4493 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4500 cwd = getcwd(NULL, 0);
4502 error = got_error_from_errno("getcwd");
4506 error = got_worktree_open(&worktree, cwd);
4510 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4515 error = apply_unveil(got_repo_get_path(repo), 1,
4516 got_worktree_get_root_path(worktree));
4520 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4524 if (!can_recurse && no_ignores) {
4525 error = got_error_msg(GOT_ERR_BAD_PATH,
4526 "disregarding ignores requires -R option");
4534 TAILQ_FOREACH(pe, &paths, entry) {
4535 if (asprintf(&ondisk_path, "%s/%s",
4536 got_worktree_get_root_path(worktree),
4538 error = got_error_from_errno("asprintf");
4541 if (lstat(ondisk_path, &sb) == -1) {
4542 if (errno == ENOENT) {
4546 error = got_error_from_errno2("lstat",
4552 if (S_ISDIR(sb.st_mode)) {
4553 error = got_error_msg(GOT_ERR_BAD_PATH,
4554 "adding directories requires -R option");
4560 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4561 NULL, repo, no_ignores);
4564 got_repo_close(repo);
4566 got_worktree_close(worktree);
4567 TAILQ_FOREACH(pe, &paths, entry)
4568 free((char *)pe->path);
4569 got_pathlist_free(&paths);
4577 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4582 static const struct got_error *
4583 print_remove_status(void *arg, unsigned char status,
4584 unsigned char staged_status, const char *path)
4586 while (path[0] == '/')
4588 if (status == GOT_STATUS_NONEXISTENT)
4590 if (status == staged_status && (status == GOT_STATUS_DELETE))
4591 status = GOT_STATUS_NO_CHANGE;
4592 printf("%c%c %s\n", status, staged_status, path);
4596 static const struct got_error *
4597 cmd_remove(int argc, char *argv[])
4599 const struct got_error *error = NULL;
4600 struct got_worktree *worktree = NULL;
4601 struct got_repository *repo = NULL;
4603 struct got_pathlist_head paths;
4604 struct got_pathlist_entry *pe;
4605 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4609 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4612 delete_local_mods = 1;
4630 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4637 cwd = getcwd(NULL, 0);
4639 error = got_error_from_errno("getcwd");
4642 error = got_worktree_open(&worktree, cwd);
4646 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4651 error = apply_unveil(got_repo_get_path(repo), 1,
4652 got_worktree_get_root_path(worktree));
4656 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4663 TAILQ_FOREACH(pe, &paths, entry) {
4664 if (asprintf(&ondisk_path, "%s/%s",
4665 got_worktree_get_root_path(worktree),
4667 error = got_error_from_errno("asprintf");
4670 if (lstat(ondisk_path, &sb) == -1) {
4671 if (errno == ENOENT) {
4675 error = got_error_from_errno2("lstat",
4681 if (S_ISDIR(sb.st_mode)) {
4682 error = got_error_msg(GOT_ERR_BAD_PATH,
4683 "removing directories requires -R option");
4689 error = got_worktree_schedule_delete(worktree, &paths,
4690 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4693 got_repo_close(repo);
4695 got_worktree_close(worktree);
4696 TAILQ_FOREACH(pe, &paths, entry)
4697 free((char *)pe->path);
4698 got_pathlist_free(&paths);
4706 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4707 "path ...\n", getprogname());
4711 static const struct got_error *
4712 revert_progress(void *arg, unsigned char status, const char *path)
4714 if (status == GOT_STATUS_UNVERSIONED)
4717 while (path[0] == '/')
4719 printf("%c %s\n", status, path);
4723 struct choose_patch_arg {
4724 FILE *patch_script_file;
4728 static const struct got_error *
4729 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4730 int nchanges, const char *action)
4733 size_t linesize = 0;
4737 case GOT_STATUS_ADD:
4738 printf("A %s\n%s this addition? [y/n] ", path, action);
4740 case GOT_STATUS_DELETE:
4741 printf("D %s\n%s this deletion? [y/n] ", path, action);
4743 case GOT_STATUS_MODIFY:
4744 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4745 return got_error_from_errno("fseek");
4746 printf(GOT_COMMIT_SEP_STR);
4747 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4749 if (ferror(patch_file))
4750 return got_error_from_errno("getline");
4751 printf(GOT_COMMIT_SEP_STR);
4752 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4753 path, n, nchanges, action);
4756 return got_error_path(path, GOT_ERR_FILE_STATUS);
4762 static const struct got_error *
4763 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4764 FILE *patch_file, int n, int nchanges)
4766 const struct got_error *err = NULL;
4768 size_t linesize = 0;
4771 struct choose_patch_arg *a = arg;
4773 *choice = GOT_PATCH_CHOICE_NONE;
4775 if (a->patch_script_file) {
4777 err = show_change(status, path, patch_file, n, nchanges,
4781 linelen = getline(&line, &linesize, a->patch_script_file);
4782 if (linelen == -1) {
4783 if (ferror(a->patch_script_file))
4784 return got_error_from_errno("getline");
4787 nl = strchr(line, '\n');
4790 if (strcmp(line, "y") == 0) {
4791 *choice = GOT_PATCH_CHOICE_YES;
4793 } else if (strcmp(line, "n") == 0) {
4794 *choice = GOT_PATCH_CHOICE_NO;
4796 } else if (strcmp(line, "q") == 0 &&
4797 status == GOT_STATUS_MODIFY) {
4798 *choice = GOT_PATCH_CHOICE_QUIT;
4801 printf("invalid response '%s'\n", line);
4806 while (resp != 'y' && resp != 'n' && resp != 'q') {
4807 err = show_change(status, path, patch_file, n, nchanges,
4814 if (status == GOT_STATUS_MODIFY) {
4815 if (resp != 'y' && resp != 'n' && resp != 'q') {
4816 printf("invalid response '%c'\n", resp);
4819 } else if (resp != 'y' && resp != 'n') {
4820 printf("invalid response '%c'\n", resp);
4826 *choice = GOT_PATCH_CHOICE_YES;
4827 else if (resp == 'n')
4828 *choice = GOT_PATCH_CHOICE_NO;
4829 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4830 *choice = GOT_PATCH_CHOICE_QUIT;
4836 static const struct got_error *
4837 cmd_revert(int argc, char *argv[])
4839 const struct got_error *error = NULL;
4840 struct got_worktree *worktree = NULL;
4841 struct got_repository *repo = NULL;
4842 char *cwd = NULL, *path = NULL;
4843 struct got_pathlist_head paths;
4844 struct got_pathlist_entry *pe;
4845 int ch, can_recurse = 0, pflag = 0;
4846 FILE *patch_script_file = NULL;
4847 const char *patch_script_path = NULL;
4848 struct choose_patch_arg cpa;
4852 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4858 patch_script_path = optarg;
4873 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4874 "unveil", NULL) == -1)
4879 if (patch_script_path && !pflag)
4880 errx(1, "-F option can only be used together with -p option");
4882 cwd = getcwd(NULL, 0);
4884 error = got_error_from_errno("getcwd");
4887 error = got_worktree_open(&worktree, cwd);
4891 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4896 if (patch_script_path) {
4897 patch_script_file = fopen(patch_script_path, "r");
4898 if (patch_script_file == NULL) {
4899 error = got_error_from_errno2("fopen",
4904 error = apply_unveil(got_repo_get_path(repo), 1,
4905 got_worktree_get_root_path(worktree));
4909 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4916 TAILQ_FOREACH(pe, &paths, entry) {
4917 if (asprintf(&ondisk_path, "%s/%s",
4918 got_worktree_get_root_path(worktree),
4920 error = got_error_from_errno("asprintf");
4923 if (lstat(ondisk_path, &sb) == -1) {
4924 if (errno == ENOENT) {
4928 error = got_error_from_errno2("lstat",
4934 if (S_ISDIR(sb.st_mode)) {
4935 error = got_error_msg(GOT_ERR_BAD_PATH,
4936 "reverting directories requires -R option");
4942 cpa.patch_script_file = patch_script_file;
4943 cpa.action = "revert";
4944 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4945 pflag ? choose_patch : NULL, &cpa, repo);
4947 if (patch_script_file && fclose(patch_script_file) == EOF &&
4949 error = got_error_from_errno2("fclose", patch_script_path);
4951 got_repo_close(repo);
4953 got_worktree_close(worktree);
4962 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4967 struct collect_commit_logmsg_arg {
4968 const char *cmdline_log;
4970 const char *worktree_path;
4971 const char *branch_name;
4972 const char *repo_path;
4977 static const struct got_error *
4978 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4981 char *initial_content = NULL;
4982 struct got_pathlist_entry *pe;
4983 const struct got_error *err = NULL;
4984 char *template = NULL;
4985 struct collect_commit_logmsg_arg *a = arg;
4989 /* if a message was specified on the command line, just use it */
4990 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4991 len = strlen(a->cmdline_log) + 1;
4992 *logmsg = malloc(len + 1);
4993 if (*logmsg == NULL)
4994 return got_error_from_errno("malloc");
4995 strlcpy(*logmsg, a->cmdline_log, len);
4999 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5000 return got_error_from_errno("asprintf");
5002 if (asprintf(&initial_content,
5003 "\n# changes to be committed on branch %s:\n",
5004 a->branch_name) == -1)
5005 return got_error_from_errno("asprintf");
5007 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5011 dprintf(fd, initial_content);
5013 TAILQ_FOREACH(pe, commitable_paths, entry) {
5014 struct got_commitable *ct = pe->data;
5015 dprintf(fd, "# %c %s\n",
5016 got_commitable_get_status(ct),
5017 got_commitable_get_path(ct));
5021 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5023 free(initial_content);
5026 /* Editor is done; we can now apply unveil(2) */
5028 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5037 static const struct got_error *
5038 cmd_commit(int argc, char *argv[])
5040 const struct got_error *error = NULL;
5041 struct got_worktree *worktree = NULL;
5042 struct got_repository *repo = NULL;
5043 char *cwd = NULL, *id_str = NULL;
5044 struct got_object_id *id = NULL;
5045 const char *logmsg = NULL;
5046 struct collect_commit_logmsg_arg cl_arg;
5047 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5048 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5049 struct got_pathlist_head paths;
5052 cl_arg.logmsg_path = NULL;
5054 while ((ch = getopt(argc, argv, "m:")) != -1) {
5069 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5070 "unveil", NULL) == -1)
5073 cwd = getcwd(NULL, 0);
5075 error = got_error_from_errno("getcwd");
5078 error = got_worktree_open(&worktree, cwd);
5082 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5085 if (rebase_in_progress) {
5086 error = got_error(GOT_ERR_REBASING);
5090 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5095 error = get_gitconfig_path(&gitconfig_path);
5098 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5103 error = get_author(&author, repo);
5108 * unveil(2) traverses exec(2); if an editor is used we have
5109 * to apply unveil after the log message has been written.
5111 if (logmsg == NULL || strlen(logmsg) == 0)
5112 error = get_editor(&editor);
5114 error = apply_unveil(got_repo_get_path(repo), 0,
5115 got_worktree_get_root_path(worktree));
5119 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5123 cl_arg.editor = editor;
5124 cl_arg.cmdline_log = logmsg;
5125 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5126 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5127 if (!histedit_in_progress) {
5128 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5129 error = got_error(GOT_ERR_COMMIT_BRANCH);
5132 cl_arg.branch_name += 11;
5134 cl_arg.repo_path = got_repo_get_path(repo);
5135 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5136 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5138 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5139 cl_arg.logmsg_path != NULL)
5140 preserve_logmsg = 1;
5144 error = got_object_id_str(&id_str, id);
5147 printf("Created commit %s\n", id_str);
5149 if (preserve_logmsg) {
5150 fprintf(stderr, "%s: log message preserved in %s\n",
5151 getprogname(), cl_arg.logmsg_path);
5152 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5154 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5155 free(cl_arg.logmsg_path);
5157 got_repo_close(repo);
5159 got_worktree_close(worktree);
5162 free(gitconfig_path);
5169 usage_cherrypick(void)
5171 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5175 static const struct got_error *
5176 cmd_cherrypick(int argc, char *argv[])
5178 const struct got_error *error = NULL;
5179 struct got_worktree *worktree = NULL;
5180 struct got_repository *repo = NULL;
5181 char *cwd = NULL, *commit_id_str = NULL;
5182 struct got_object_id *commit_id = NULL;
5183 struct got_commit_object *commit = NULL;
5184 struct got_object_qid *pid;
5185 struct got_reference *head_ref = NULL;
5186 int ch, did_something = 0;
5188 while ((ch = getopt(argc, argv, "")) != -1) {
5200 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5201 "unveil", NULL) == -1)
5207 cwd = getcwd(NULL, 0);
5209 error = got_error_from_errno("getcwd");
5212 error = got_worktree_open(&worktree, cwd);
5216 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5221 error = apply_unveil(got_repo_get_path(repo), 0,
5222 got_worktree_get_root_path(worktree));
5226 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5227 GOT_OBJ_TYPE_COMMIT, repo);
5228 if (error != NULL) {
5229 struct got_reference *ref;
5230 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5232 error = got_ref_open(&ref, repo, argv[0], 0);
5235 error = got_ref_resolve(&commit_id, repo, ref);
5240 error = got_object_id_str(&commit_id_str, commit_id);
5244 error = got_ref_open(&head_ref, repo,
5245 got_worktree_get_head_ref_name(worktree), 0);
5249 error = check_same_branch(commit_id, head_ref, NULL, repo);
5251 if (error->code != GOT_ERR_ANCESTRY)
5255 error = got_error(GOT_ERR_SAME_BRANCH);
5259 error = got_object_open_as_commit(&commit, repo, commit_id);
5262 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5263 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5264 commit_id, repo, update_progress, &did_something, check_cancelled,
5270 printf("Merged commit %s\n", commit_id_str);
5273 got_object_commit_close(commit);
5274 free(commit_id_str);
5276 got_ref_close(head_ref);
5278 got_worktree_close(worktree);
5280 got_repo_close(repo);
5287 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5291 static const struct got_error *
5292 cmd_backout(int argc, char *argv[])
5294 const struct got_error *error = NULL;
5295 struct got_worktree *worktree = NULL;
5296 struct got_repository *repo = NULL;
5297 char *cwd = NULL, *commit_id_str = NULL;
5298 struct got_object_id *commit_id = NULL;
5299 struct got_commit_object *commit = NULL;
5300 struct got_object_qid *pid;
5301 struct got_reference *head_ref = NULL;
5302 int ch, did_something = 0;
5304 while ((ch = getopt(argc, argv, "")) != -1) {
5316 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5317 "unveil", NULL) == -1)
5323 cwd = getcwd(NULL, 0);
5325 error = got_error_from_errno("getcwd");
5328 error = got_worktree_open(&worktree, cwd);
5332 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5337 error = apply_unveil(got_repo_get_path(repo), 0,
5338 got_worktree_get_root_path(worktree));
5342 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5343 GOT_OBJ_TYPE_COMMIT, repo);
5344 if (error != NULL) {
5345 struct got_reference *ref;
5346 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5348 error = got_ref_open(&ref, repo, argv[0], 0);
5351 error = got_ref_resolve(&commit_id, repo, ref);
5356 error = got_object_id_str(&commit_id_str, commit_id);
5360 error = got_ref_open(&head_ref, repo,
5361 got_worktree_get_head_ref_name(worktree), 0);
5365 error = check_same_branch(commit_id, head_ref, NULL, repo);
5369 error = got_object_open_as_commit(&commit, repo, commit_id);
5372 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5374 error = got_error(GOT_ERR_ROOT_COMMIT);
5378 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5379 update_progress, &did_something, check_cancelled, NULL);
5384 printf("Backed out commit %s\n", commit_id_str);
5387 got_object_commit_close(commit);
5388 free(commit_id_str);
5390 got_ref_close(head_ref);
5392 got_worktree_close(worktree);
5394 got_repo_close(repo);
5401 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5407 trim_logmsg(char *logmsg, int limit)
5412 len = strlen(logmsg);
5416 nl = strchr(logmsg, '\n');
5421 static const struct got_error *
5422 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5424 const struct got_error *err;
5425 char *logmsg0 = NULL;
5428 err = got_object_commit_get_logmsg(&logmsg0, commit);
5433 while (isspace((unsigned char)s[0]))
5436 *logmsg = strdup(s);
5437 if (*logmsg == NULL) {
5438 err = got_error_from_errno("strdup");
5442 trim_logmsg(*logmsg, limit);
5448 static const struct got_error *
5449 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5451 const struct got_error *err;
5452 struct got_commit_object *commit = NULL;
5453 char *id_str = NULL, *logmsg = NULL;
5455 err = got_object_open_as_commit(&commit, repo, id);
5459 err = got_object_id_str(&id_str, id);
5465 err = get_short_logmsg(&logmsg, 42, commit);
5469 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5472 got_object_commit_close(commit);
5477 static const struct got_error *
5478 show_rebase_progress(struct got_commit_object *commit,
5479 struct got_object_id *old_id, struct got_object_id *new_id)
5481 const struct got_error *err;
5482 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5484 err = got_object_id_str(&old_id_str, old_id);
5489 err = got_object_id_str(&new_id_str, new_id);
5494 old_id_str[12] = '\0';
5496 new_id_str[12] = '\0';
5498 err = get_short_logmsg(&logmsg, 42, commit);
5502 printf("%s -> %s: %s\n", old_id_str,
5503 new_id_str ? new_id_str : "no-op change", logmsg);
5511 static const struct got_error *
5512 rebase_progress(void *arg, unsigned char status, const char *path)
5514 unsigned char *rebase_status = arg;
5516 while (path[0] == '/')
5518 printf("%c %s\n", status, path);
5520 if (*rebase_status == GOT_STATUS_CONFLICT)
5522 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5523 *rebase_status = status;
5527 static const struct got_error *
5528 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5529 struct got_reference *branch, struct got_reference *new_base_branch,
5530 struct got_reference *tmp_branch, struct got_repository *repo)
5532 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5533 return got_worktree_rebase_complete(worktree, fileindex,
5534 new_base_branch, tmp_branch, branch, repo);
5537 static const struct got_error *
5538 rebase_commit(struct got_pathlist_head *merged_paths,
5539 struct got_worktree *worktree, struct got_fileindex *fileindex,
5540 struct got_reference *tmp_branch,
5541 struct got_object_id *commit_id, struct got_repository *repo)
5543 const struct got_error *error;
5544 struct got_commit_object *commit;
5545 struct got_object_id *new_commit_id;
5547 error = got_object_open_as_commit(&commit, repo, commit_id);
5551 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5552 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5554 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5556 error = show_rebase_progress(commit, commit_id, NULL);
5558 error = show_rebase_progress(commit, commit_id, new_commit_id);
5559 free(new_commit_id);
5562 got_object_commit_close(commit);
5566 struct check_path_prefix_arg {
5567 const char *path_prefix;
5572 static const struct got_error *
5573 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5574 struct got_blob_object *blob2, struct got_object_id *id1,
5575 struct got_object_id *id2, const char *path1, const char *path2,
5576 mode_t mode1, mode_t mode2, struct got_repository *repo)
5578 struct check_path_prefix_arg *a = arg;
5580 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5581 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5582 return got_error(a->errcode);
5587 static const struct got_error *
5588 check_path_prefix(struct got_object_id *parent_id,
5589 struct got_object_id *commit_id, const char *path_prefix,
5590 int errcode, struct got_repository *repo)
5592 const struct got_error *err;
5593 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5594 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5595 struct check_path_prefix_arg cpp_arg;
5597 if (got_path_is_root_dir(path_prefix))
5600 err = got_object_open_as_commit(&commit, repo, commit_id);
5604 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5608 err = got_object_open_as_tree(&tree1, repo,
5609 got_object_commit_get_tree_id(parent_commit));
5613 err = got_object_open_as_tree(&tree2, repo,
5614 got_object_commit_get_tree_id(commit));
5618 cpp_arg.path_prefix = path_prefix;
5619 while (cpp_arg.path_prefix[0] == '/')
5620 cpp_arg.path_prefix++;
5621 cpp_arg.len = strlen(cpp_arg.path_prefix);
5622 cpp_arg.errcode = errcode;
5623 err = got_diff_tree(tree1, tree2, "", "", repo,
5624 check_path_prefix_in_diff, &cpp_arg, 0);
5627 got_object_tree_close(tree1);
5629 got_object_tree_close(tree2);
5631 got_object_commit_close(commit);
5633 got_object_commit_close(parent_commit);
5637 static const struct got_error *
5638 collect_commits(struct got_object_id_queue *commits,
5639 struct got_object_id *initial_commit_id,
5640 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5641 const char *path_prefix, int path_prefix_errcode,
5642 struct got_repository *repo)
5644 const struct got_error *err = NULL;
5645 struct got_commit_graph *graph = NULL;
5646 struct got_object_id *parent_id = NULL;
5647 struct got_object_qid *qid;
5648 struct got_object_id *commit_id = initial_commit_id;
5650 err = got_commit_graph_open(&graph, "/", 1);
5654 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5655 check_cancelled, NULL);
5658 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5659 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5660 check_cancelled, NULL);
5662 if (err->code == GOT_ERR_ITER_COMPLETED) {
5663 err = got_error_msg(GOT_ERR_ANCESTRY,
5664 "ran out of commits to rebase before "
5665 "youngest common ancestor commit has "
5670 err = check_path_prefix(parent_id, commit_id,
5671 path_prefix, path_prefix_errcode, repo);
5675 err = got_object_qid_alloc(&qid, commit_id);
5678 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5679 commit_id = parent_id;
5683 got_commit_graph_close(graph);
5687 static const struct got_error *
5688 cmd_rebase(int argc, char *argv[])
5690 const struct got_error *error = NULL;
5691 struct got_worktree *worktree = NULL;
5692 struct got_repository *repo = NULL;
5693 struct got_fileindex *fileindex = NULL;
5695 struct got_reference *branch = NULL;
5696 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5697 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5698 struct got_object_id *resume_commit_id = NULL;
5699 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5700 struct got_commit_object *commit = NULL;
5701 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5702 int histedit_in_progress = 0;
5703 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5704 struct got_object_id_queue commits;
5705 struct got_pathlist_head merged_paths;
5706 const struct got_object_id_queue *parent_ids;
5707 struct got_object_qid *qid, *pid;
5709 SIMPLEQ_INIT(&commits);
5710 TAILQ_INIT(&merged_paths);
5712 while ((ch = getopt(argc, argv, "ac")) != -1) {
5718 continue_rebase = 1;
5730 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5731 "unveil", NULL) == -1)
5734 if (abort_rebase && continue_rebase)
5736 else if (abort_rebase || continue_rebase) {
5739 } else if (argc != 1)
5742 cwd = getcwd(NULL, 0);
5744 error = got_error_from_errno("getcwd");
5747 error = got_worktree_open(&worktree, cwd);
5751 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5756 error = apply_unveil(got_repo_get_path(repo), 0,
5757 got_worktree_get_root_path(worktree));
5761 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5765 if (histedit_in_progress) {
5766 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5770 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5776 if (!rebase_in_progress) {
5777 error = got_error(GOT_ERR_NOT_REBASING);
5780 error = got_worktree_rebase_continue(&resume_commit_id,
5781 &new_base_branch, &tmp_branch, &branch, &fileindex,
5785 printf("Switching work tree to %s\n",
5786 got_ref_get_symref_target(new_base_branch));
5787 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5788 new_base_branch, update_progress, &did_something);
5791 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5792 goto done; /* nothing else to do */
5795 if (continue_rebase) {
5796 if (!rebase_in_progress) {
5797 error = got_error(GOT_ERR_NOT_REBASING);
5800 error = got_worktree_rebase_continue(&resume_commit_id,
5801 &new_base_branch, &tmp_branch, &branch, &fileindex,
5806 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5807 resume_commit_id, repo);
5811 yca_id = got_object_id_dup(resume_commit_id);
5812 if (yca_id == NULL) {
5813 error = got_error_from_errno("got_object_id_dup");
5817 error = got_ref_open(&branch, repo, argv[0], 0);
5822 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5826 if (!continue_rebase) {
5827 struct got_object_id *base_commit_id;
5829 base_commit_id = got_worktree_get_base_commit_id(worktree);
5830 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5831 base_commit_id, branch_head_commit_id, repo,
5832 check_cancelled, NULL);
5835 if (yca_id == NULL) {
5836 error = got_error_msg(GOT_ERR_ANCESTRY,
5837 "specified branch shares no common ancestry "
5838 "with work tree's branch");
5842 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5844 if (error->code != GOT_ERR_ANCESTRY)
5848 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5849 "specified branch resolves to a commit which "
5850 "is already contained in work tree's branch");
5853 error = got_worktree_rebase_prepare(&new_base_branch,
5854 &tmp_branch, &fileindex, worktree, branch, repo);
5859 commit_id = branch_head_commit_id;
5860 error = got_object_open_as_commit(&commit, repo, commit_id);
5864 parent_ids = got_object_commit_get_parent_ids(commit);
5865 pid = SIMPLEQ_FIRST(parent_ids);
5867 if (!continue_rebase) {
5869 error = got_worktree_rebase_abort(worktree, fileindex,
5870 repo, new_base_branch, update_progress,
5874 printf("Rebase of %s aborted\n",
5875 got_ref_get_name(branch));
5877 error = got_error(GOT_ERR_EMPTY_REBASE);
5880 error = collect_commits(&commits, commit_id, pid->id,
5881 yca_id, got_worktree_get_path_prefix(worktree),
5882 GOT_ERR_REBASE_PATH, repo);
5883 got_object_commit_close(commit);
5888 if (SIMPLEQ_EMPTY(&commits)) {
5889 if (continue_rebase) {
5890 error = rebase_complete(worktree, fileindex,
5891 branch, new_base_branch, tmp_branch, repo);
5894 /* Fast-forward the reference of the branch. */
5895 struct got_object_id *new_head_commit_id;
5897 error = got_ref_resolve(&new_head_commit_id, repo,
5901 error = got_object_id_str(&id_str, new_head_commit_id);
5902 printf("Forwarding %s to commit %s\n",
5903 got_ref_get_name(branch), id_str);
5905 error = got_ref_change_ref(branch,
5906 new_head_commit_id);
5913 SIMPLEQ_FOREACH(qid, &commits, entry) {
5914 commit_id = qid->id;
5915 parent_id = pid ? pid->id : yca_id;
5918 error = got_worktree_rebase_merge_files(&merged_paths,
5919 worktree, fileindex, parent_id, commit_id, repo,
5920 rebase_progress, &rebase_status, check_cancelled, NULL);
5924 if (rebase_status == GOT_STATUS_CONFLICT) {
5925 error = show_rebase_merge_conflict(qid->id, repo);
5928 got_worktree_rebase_pathlist_free(&merged_paths);
5932 error = rebase_commit(&merged_paths, worktree, fileindex,
5933 tmp_branch, commit_id, repo);
5934 got_worktree_rebase_pathlist_free(&merged_paths);
5939 if (rebase_status == GOT_STATUS_CONFLICT) {
5940 error = got_worktree_rebase_postpone(worktree, fileindex);
5943 error = got_error_msg(GOT_ERR_CONFLICTS,
5944 "conflicts must be resolved before rebasing can continue");
5946 error = rebase_complete(worktree, fileindex, branch,
5947 new_base_branch, tmp_branch, repo);
5949 got_object_id_queue_free(&commits);
5950 free(branch_head_commit_id);
5951 free(resume_commit_id);
5954 got_object_commit_close(commit);
5956 got_ref_close(branch);
5957 if (new_base_branch)
5958 got_ref_close(new_base_branch);
5960 got_ref_close(tmp_branch);
5962 got_worktree_close(worktree);
5964 got_repo_close(repo);
5969 usage_histedit(void)
5971 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5976 #define GOT_HISTEDIT_PICK 'p'
5977 #define GOT_HISTEDIT_EDIT 'e'
5978 #define GOT_HISTEDIT_FOLD 'f'
5979 #define GOT_HISTEDIT_DROP 'd'
5980 #define GOT_HISTEDIT_MESG 'm'
5982 static struct got_histedit_cmd {
5986 } got_histedit_cmds[] = {
5987 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5988 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5989 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5991 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5992 { GOT_HISTEDIT_MESG, "mesg",
5993 "single-line log message for commit above (open editor if empty)" },
5996 struct got_histedit_list_entry {
5997 TAILQ_ENTRY(got_histedit_list_entry) entry;
5998 struct got_object_id *commit_id;
5999 const struct got_histedit_cmd *cmd;
6002 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6004 static const struct got_error *
6005 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6006 FILE *f, struct got_repository *repo)
6008 const struct got_error *err = NULL;
6009 char *logmsg = NULL, *id_str = NULL;
6010 struct got_commit_object *commit = NULL;
6013 err = got_object_open_as_commit(&commit, repo, commit_id);
6017 err = get_short_logmsg(&logmsg, 34, commit);
6021 err = got_object_id_str(&id_str, commit_id);
6025 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6027 err = got_ferror(f, GOT_ERR_IO);
6030 got_object_commit_close(commit);
6036 static const struct got_error *
6037 histedit_write_commit_list(struct got_object_id_queue *commits,
6038 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6040 const struct got_error *err = NULL;
6041 struct got_object_qid *qid;
6043 if (SIMPLEQ_EMPTY(commits))
6044 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6046 SIMPLEQ_FOREACH(qid, commits, entry) {
6047 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6051 if (edit_logmsg_only) {
6052 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6054 err = got_ferror(f, GOT_ERR_IO);
6063 static const struct got_error *
6064 write_cmd_list(FILE *f, const char *branch_name,
6065 struct got_object_id_queue *commits)
6067 const struct got_error *err = NULL;
6070 struct got_object_qid *qid;
6072 qid = SIMPLEQ_FIRST(commits);
6073 err = got_object_id_str(&id_str, qid->id);
6078 "# Editing the history of branch '%s' starting at\n"
6080 "# Commits will be processed in order from top to "
6081 "bottom of this file.\n", branch_name, id_str);
6083 err = got_ferror(f, GOT_ERR_IO);
6087 n = fprintf(f, "# Available histedit commands:\n");
6089 err = got_ferror(f, GOT_ERR_IO);
6093 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6094 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6095 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6098 err = got_ferror(f, GOT_ERR_IO);
6107 static const struct got_error *
6108 histedit_syntax_error(int lineno)
6110 static char msg[42];
6113 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6115 if (ret == -1 || ret >= sizeof(msg))
6116 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6118 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6121 static const struct got_error *
6122 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6123 char *logmsg, struct got_repository *repo)
6125 const struct got_error *err;
6126 struct got_commit_object *folded_commit = NULL;
6127 char *id_str, *folded_logmsg = NULL;
6129 err = got_object_id_str(&id_str, hle->commit_id);
6133 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6137 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6140 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6141 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6142 folded_logmsg) == -1) {
6143 err = got_error_from_errno("asprintf");
6147 got_object_commit_close(folded_commit);
6149 free(folded_logmsg);
6153 static struct got_histedit_list_entry *
6154 get_folded_commits(struct got_histedit_list_entry *hle)
6156 struct got_histedit_list_entry *prev, *folded = NULL;
6158 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6159 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6160 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6161 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6163 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6169 static const struct got_error *
6170 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6171 struct got_repository *repo)
6173 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6174 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6175 const struct got_error *err = NULL;
6176 struct got_commit_object *commit = NULL;
6178 struct got_histedit_list_entry *folded = NULL;
6180 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6184 folded = get_folded_commits(hle);
6186 while (folded != hle) {
6187 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6188 folded = TAILQ_NEXT(folded, entry);
6191 err = append_folded_commit_msg(&new_msg, folded,
6197 folded = TAILQ_NEXT(folded, entry);
6201 err = got_object_id_str(&id_str, hle->commit_id);
6204 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6207 if (asprintf(&new_msg,
6208 "%s\n# original log message of commit %s: %s",
6209 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6210 err = got_error_from_errno("asprintf");
6216 err = got_object_id_str(&id_str, hle->commit_id);
6220 err = got_opentemp_named_fd(&logmsg_path, &fd,
6221 GOT_TMPDIR_STR "/got-logmsg");
6225 dprintf(fd, logmsg);
6228 err = get_editor(&editor);
6232 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6234 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6236 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6239 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6240 err = got_error_from_errno2("unlink", logmsg_path);
6246 got_object_commit_close(commit);
6250 static const struct got_error *
6251 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6252 FILE *f, struct got_repository *repo)
6254 const struct got_error *err = NULL;
6255 char *line = NULL, *p, *end;
6259 const struct got_histedit_cmd *cmd;
6260 struct got_object_id *commit_id = NULL;
6261 struct got_histedit_list_entry *hle = NULL;
6264 len = getline(&line, &size, f);
6266 const struct got_error *getline_err;
6269 getline_err = got_error_from_errno("getline");
6270 err = got_ferror(f, getline_err->code);
6275 while (isspace((unsigned char)p[0]))
6277 if (p[0] == '#' || p[0] == '\0') {
6283 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6284 cmd = &got_histedit_cmds[i];
6285 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6286 isspace((unsigned char)p[strlen(cmd->name)])) {
6287 p += strlen(cmd->name);
6290 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6295 if (i == nitems(got_histedit_cmds)) {
6296 err = histedit_syntax_error(lineno);
6299 while (isspace((unsigned char)p[0]))
6301 if (cmd->code == GOT_HISTEDIT_MESG) {
6302 if (hle == NULL || hle->logmsg != NULL) {
6303 err = got_error(GOT_ERR_HISTEDIT_CMD);
6307 err = histedit_edit_logmsg(hle, repo);
6311 hle->logmsg = strdup(p);
6312 if (hle->logmsg == NULL) {
6313 err = got_error_from_errno("strdup");
6322 while (end[0] && !isspace((unsigned char)end[0]))
6326 err = got_object_resolve_id_str(&commit_id, repo, p);
6328 /* override error code */
6329 err = histedit_syntax_error(lineno);
6333 hle = malloc(sizeof(*hle));
6335 err = got_error_from_errno("malloc");
6339 hle->commit_id = commit_id;
6344 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6352 static const struct got_error *
6353 histedit_check_script(struct got_histedit_list *histedit_cmds,
6354 struct got_object_id_queue *commits, struct got_repository *repo)
6356 const struct got_error *err = NULL;
6357 struct got_object_qid *qid;
6358 struct got_histedit_list_entry *hle;
6359 static char msg[92];
6362 if (TAILQ_EMPTY(histedit_cmds))
6363 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6364 "histedit script contains no commands");
6365 if (SIMPLEQ_EMPTY(commits))
6366 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6368 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6369 struct got_histedit_list_entry *hle2;
6370 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6373 if (got_object_id_cmp(hle->commit_id,
6374 hle2->commit_id) != 0)
6376 err = got_object_id_str(&id_str, hle->commit_id);
6379 snprintf(msg, sizeof(msg), "commit %s is listed "
6380 "more than once in histedit script", id_str);
6382 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6386 SIMPLEQ_FOREACH(qid, commits, entry) {
6387 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6388 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6392 err = got_object_id_str(&id_str, qid->id);
6395 snprintf(msg, sizeof(msg),
6396 "commit %s missing from histedit script", id_str);
6398 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6402 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6403 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6404 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6405 "last commit in histedit script cannot be folded");
6410 static const struct got_error *
6411 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6412 const char *path, struct got_object_id_queue *commits,
6413 struct got_repository *repo)
6415 const struct got_error *err = NULL;
6419 err = get_editor(&editor);
6423 if (spawn_editor(editor, path) == -1) {
6424 err = got_error_from_errno("failed spawning editor");
6428 f = fopen(path, "r");
6430 err = got_error_from_errno("fopen");
6433 err = histedit_parse_list(histedit_cmds, f, repo);
6437 err = histedit_check_script(histedit_cmds, commits, repo);
6439 if (f && fclose(f) != 0 && err == NULL)
6440 err = got_error_from_errno("fclose");
6445 static const struct got_error *
6446 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6447 struct got_object_id_queue *, const char *, const char *,
6448 struct got_repository *);
6450 static const struct got_error *
6451 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6452 struct got_object_id_queue *commits, const char *branch_name,
6453 int edit_logmsg_only, struct got_repository *repo)
6455 const struct got_error *err;
6459 err = got_opentemp_named(&path, &f, "got-histedit");
6463 err = write_cmd_list(f, branch_name, commits);
6467 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6471 if (edit_logmsg_only) {
6473 err = histedit_parse_list(histedit_cmds, f, repo);
6475 if (fclose(f) != 0) {
6476 err = got_error_from_errno("fclose");
6480 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6482 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6483 err->code != GOT_ERR_HISTEDIT_CMD)
6485 err = histedit_edit_list_retry(histedit_cmds, err,
6486 commits, path, branch_name, repo);
6490 if (f && fclose(f) != 0 && err == NULL)
6491 err = got_error_from_errno("fclose");
6492 if (path && unlink(path) != 0 && err == NULL)
6493 err = got_error_from_errno2("unlink", path);
6498 static const struct got_error *
6499 histedit_save_list(struct got_histedit_list *histedit_cmds,
6500 struct got_worktree *worktree, struct got_repository *repo)
6502 const struct got_error *err = NULL;
6505 struct got_histedit_list_entry *hle;
6506 struct got_commit_object *commit = NULL;
6508 err = got_worktree_get_histedit_script_path(&path, worktree);
6512 f = fopen(path, "w");
6514 err = got_error_from_errno2("fopen", path);
6517 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6518 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6524 int n = fprintf(f, "%c %s\n",
6525 GOT_HISTEDIT_MESG, hle->logmsg);
6527 err = got_ferror(f, GOT_ERR_IO);
6533 if (f && fclose(f) != 0 && err == NULL)
6534 err = got_error_from_errno("fclose");
6537 got_object_commit_close(commit);
6542 histedit_free_list(struct got_histedit_list *histedit_cmds)
6544 struct got_histedit_list_entry *hle;
6546 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6547 TAILQ_REMOVE(histedit_cmds, hle, entry);
6552 static const struct got_error *
6553 histedit_load_list(struct got_histedit_list *histedit_cmds,
6554 const char *path, struct got_repository *repo)
6556 const struct got_error *err = NULL;
6559 f = fopen(path, "r");
6561 err = got_error_from_errno2("fopen", path);
6565 err = histedit_parse_list(histedit_cmds, f, repo);
6567 if (f && fclose(f) != 0 && err == NULL)
6568 err = got_error_from_errno("fclose");
6572 static const struct got_error *
6573 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6574 const struct got_error *edit_err, struct got_object_id_queue *commits,
6575 const char *path, const char *branch_name, struct got_repository *repo)
6577 const struct got_error *err = NULL, *prev_err = edit_err;
6580 while (resp != 'c' && resp != 'r' && resp != 'a') {
6581 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6582 "or (a)bort: ", getprogname(), prev_err->msg);
6587 histedit_free_list(histedit_cmds);
6588 err = histedit_run_editor(histedit_cmds, path, commits,
6591 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6592 err->code != GOT_ERR_HISTEDIT_CMD)
6599 } else if (resp == 'r') {
6600 histedit_free_list(histedit_cmds);
6601 err = histedit_edit_script(histedit_cmds,
6602 commits, branch_name, 0, repo);
6604 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6605 err->code != GOT_ERR_HISTEDIT_CMD)
6612 } else if (resp == 'a') {
6613 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6616 printf("invalid response '%c'\n", resp);
6622 static const struct got_error *
6623 histedit_complete(struct got_worktree *worktree,
6624 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6625 struct got_reference *branch, struct got_repository *repo)
6627 printf("Switching work tree to %s\n",
6628 got_ref_get_symref_target(branch));
6629 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6633 static const struct got_error *
6634 show_histedit_progress(struct got_commit_object *commit,
6635 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6637 const struct got_error *err;
6638 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6640 err = got_object_id_str(&old_id_str, hle->commit_id);
6645 err = got_object_id_str(&new_id_str, new_id);
6650 old_id_str[12] = '\0';
6652 new_id_str[12] = '\0';
6655 logmsg = strdup(hle->logmsg);
6656 if (logmsg == NULL) {
6657 err = got_error_from_errno("strdup");
6660 trim_logmsg(logmsg, 42);
6662 err = get_short_logmsg(&logmsg, 42, commit);
6667 switch (hle->cmd->code) {
6668 case GOT_HISTEDIT_PICK:
6669 case GOT_HISTEDIT_EDIT:
6670 printf("%s -> %s: %s\n", old_id_str,
6671 new_id_str ? new_id_str : "no-op change", logmsg);
6673 case GOT_HISTEDIT_DROP:
6674 case GOT_HISTEDIT_FOLD:
6675 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6687 static const struct got_error *
6688 histedit_commit(struct got_pathlist_head *merged_paths,
6689 struct got_worktree *worktree, struct got_fileindex *fileindex,
6690 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6691 struct got_repository *repo)
6693 const struct got_error *err;
6694 struct got_commit_object *commit;
6695 struct got_object_id *new_commit_id;
6697 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6698 && hle->logmsg == NULL) {
6699 err = histedit_edit_logmsg(hle, repo);
6704 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6708 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6709 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6712 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6714 err = show_histedit_progress(commit, hle, NULL);
6716 err = show_histedit_progress(commit, hle, new_commit_id);
6717 free(new_commit_id);
6720 got_object_commit_close(commit);
6724 static const struct got_error *
6725 histedit_skip_commit(struct got_histedit_list_entry *hle,
6726 struct got_worktree *worktree, struct got_repository *repo)
6728 const struct got_error *error;
6729 struct got_commit_object *commit;
6731 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6736 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6740 error = show_histedit_progress(commit, hle, NULL);
6741 got_object_commit_close(commit);
6745 static const struct got_error *
6746 check_local_changes(void *arg, unsigned char status,
6747 unsigned char staged_status, const char *path,
6748 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6749 struct got_object_id *commit_id, int dirfd, const char *de_name)
6751 int *have_local_changes = arg;
6754 case GOT_STATUS_ADD:
6755 case GOT_STATUS_DELETE:
6756 case GOT_STATUS_MODIFY:
6757 case GOT_STATUS_CONFLICT:
6758 *have_local_changes = 1;
6759 return got_error(GOT_ERR_CANCELLED);
6764 switch (staged_status) {
6765 case GOT_STATUS_ADD:
6766 case GOT_STATUS_DELETE:
6767 case GOT_STATUS_MODIFY:
6768 *have_local_changes = 1;
6769 return got_error(GOT_ERR_CANCELLED);
6777 static const struct got_error *
6778 cmd_histedit(int argc, char *argv[])
6780 const struct got_error *error = NULL;
6781 struct got_worktree *worktree = NULL;
6782 struct got_fileindex *fileindex = NULL;
6783 struct got_repository *repo = NULL;
6785 struct got_reference *branch = NULL;
6786 struct got_reference *tmp_branch = NULL;
6787 struct got_object_id *resume_commit_id = NULL;
6788 struct got_object_id *base_commit_id = NULL;
6789 struct got_object_id *head_commit_id = NULL;
6790 struct got_commit_object *commit = NULL;
6791 int ch, rebase_in_progress = 0, did_something;
6792 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6793 int edit_logmsg_only = 0;
6794 const char *edit_script_path = NULL;
6795 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6796 struct got_object_id_queue commits;
6797 struct got_pathlist_head merged_paths;
6798 const struct got_object_id_queue *parent_ids;
6799 struct got_object_qid *pid;
6800 struct got_histedit_list histedit_cmds;
6801 struct got_histedit_list_entry *hle;
6803 SIMPLEQ_INIT(&commits);
6804 TAILQ_INIT(&histedit_cmds);
6805 TAILQ_INIT(&merged_paths);
6807 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6816 edit_script_path = optarg;
6819 edit_logmsg_only = 1;
6831 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6832 "unveil", NULL) == -1)
6835 if (abort_edit && continue_edit)
6836 errx(1, "histedit's -a and -c options are mutually exclusive");
6837 if (edit_script_path && edit_logmsg_only)
6838 errx(1, "histedit's -F and -m options are mutually exclusive");
6839 if (abort_edit && edit_logmsg_only)
6840 errx(1, "histedit's -a and -m options are mutually exclusive");
6841 if (continue_edit && edit_logmsg_only)
6842 errx(1, "histedit's -c and -m options are mutually exclusive");
6847 * This command cannot apply unveil(2) in all cases because the
6848 * user may choose to run an editor to edit the histedit script
6849 * and to edit individual commit log messages.
6850 * unveil(2) traverses exec(2); if an editor is used we have to
6851 * apply unveil after edit script and log messages have been written.
6852 * XXX TODO: Make use of unveil(2) where possible.
6855 cwd = getcwd(NULL, 0);
6857 error = got_error_from_errno("getcwd");
6860 error = got_worktree_open(&worktree, cwd);
6864 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6869 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6872 if (rebase_in_progress) {
6873 error = got_error(GOT_ERR_REBASING);
6877 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6881 if (edit_in_progress && edit_logmsg_only) {
6882 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6883 "histedit operation is in progress in this "
6884 "work tree and must be continued or aborted "
6885 "before the -m option can be used");
6889 if (edit_in_progress && abort_edit) {
6890 error = got_worktree_histedit_continue(&resume_commit_id,
6891 &tmp_branch, &branch, &base_commit_id, &fileindex,
6895 printf("Switching work tree to %s\n",
6896 got_ref_get_symref_target(branch));
6897 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6898 branch, base_commit_id, update_progress, &did_something);
6901 printf("Histedit of %s aborted\n",
6902 got_ref_get_symref_target(branch));
6903 goto done; /* nothing else to do */
6904 } else if (abort_edit) {
6905 error = got_error(GOT_ERR_NOT_HISTEDIT);
6909 if (continue_edit) {
6912 if (!edit_in_progress) {
6913 error = got_error(GOT_ERR_NOT_HISTEDIT);
6917 error = got_worktree_get_histedit_script_path(&path, worktree);
6921 error = histedit_load_list(&histedit_cmds, path, repo);
6926 error = got_worktree_histedit_continue(&resume_commit_id,
6927 &tmp_branch, &branch, &base_commit_id, &fileindex,
6932 error = got_ref_resolve(&head_commit_id, repo, branch);
6936 error = got_object_open_as_commit(&commit, repo,
6940 parent_ids = got_object_commit_get_parent_ids(commit);
6941 pid = SIMPLEQ_FIRST(parent_ids);
6943 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6946 error = collect_commits(&commits, head_commit_id, pid->id,
6947 base_commit_id, got_worktree_get_path_prefix(worktree),
6948 GOT_ERR_HISTEDIT_PATH, repo);
6949 got_object_commit_close(commit);
6954 if (edit_in_progress) {
6955 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6959 error = got_ref_open(&branch, repo,
6960 got_worktree_get_head_ref_name(worktree), 0);
6964 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6965 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6966 "will not edit commit history of a branch outside "
6967 "the \"refs/heads/\" reference namespace");
6971 error = got_ref_resolve(&head_commit_id, repo, branch);
6972 got_ref_close(branch);
6977 error = got_object_open_as_commit(&commit, repo,
6981 parent_ids = got_object_commit_get_parent_ids(commit);
6982 pid = SIMPLEQ_FIRST(parent_ids);
6984 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6987 error = collect_commits(&commits, head_commit_id, pid->id,
6988 got_worktree_get_base_commit_id(worktree),
6989 got_worktree_get_path_prefix(worktree),
6990 GOT_ERR_HISTEDIT_PATH, repo);
6991 got_object_commit_close(commit);
6996 if (SIMPLEQ_EMPTY(&commits)) {
6997 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7001 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7002 &base_commit_id, &fileindex, worktree, repo);
7006 if (edit_script_path) {
7007 error = histedit_load_list(&histedit_cmds,
7008 edit_script_path, repo);
7010 got_worktree_histedit_abort(worktree, fileindex,
7011 repo, branch, base_commit_id,
7012 update_progress, &did_something);
7016 const char *branch_name;
7017 branch_name = got_ref_get_symref_target(branch);
7018 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7020 error = histedit_edit_script(&histedit_cmds, &commits,
7021 branch_name, edit_logmsg_only, repo);
7023 got_worktree_histedit_abort(worktree, fileindex,
7024 repo, branch, base_commit_id,
7025 update_progress, &did_something);
7031 error = histedit_save_list(&histedit_cmds, worktree,
7034 got_worktree_histedit_abort(worktree, fileindex,
7035 repo, branch, base_commit_id,
7036 update_progress, &did_something);
7042 error = histedit_check_script(&histedit_cmds, &commits, repo);
7046 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7047 if (resume_commit_id) {
7048 if (got_object_id_cmp(hle->commit_id,
7049 resume_commit_id) != 0)
7052 resume_commit_id = NULL;
7053 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7054 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7055 error = histedit_skip_commit(hle, worktree,
7060 struct got_pathlist_head paths;
7061 int have_changes = 0;
7064 error = got_pathlist_append(&paths, "", NULL);
7067 error = got_worktree_status(worktree, &paths,
7068 repo, check_local_changes, &have_changes,
7069 check_cancelled, NULL);
7070 got_pathlist_free(&paths);
7072 if (error->code != GOT_ERR_CANCELLED)
7074 if (sigint_received || sigpipe_received)
7078 error = histedit_commit(NULL, worktree,
7079 fileindex, tmp_branch, hle, repo);
7083 error = got_object_open_as_commit(
7084 &commit, repo, hle->commit_id);
7087 error = show_histedit_progress(commit,
7089 got_object_commit_close(commit);
7098 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7099 error = histedit_skip_commit(hle, worktree, repo);
7105 error = got_object_open_as_commit(&commit, repo,
7109 parent_ids = got_object_commit_get_parent_ids(commit);
7110 pid = SIMPLEQ_FIRST(parent_ids);
7112 error = got_worktree_histedit_merge_files(&merged_paths,
7113 worktree, fileindex, pid->id, hle->commit_id, repo,
7114 rebase_progress, &rebase_status, check_cancelled, NULL);
7117 got_object_commit_close(commit);
7120 if (rebase_status == GOT_STATUS_CONFLICT) {
7121 error = show_rebase_merge_conflict(hle->commit_id,
7125 got_worktree_rebase_pathlist_free(&merged_paths);
7129 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7131 error = got_object_id_str(&id_str, hle->commit_id);
7134 printf("Stopping histedit for amending commit %s\n",
7137 got_worktree_rebase_pathlist_free(&merged_paths);
7138 error = got_worktree_histedit_postpone(worktree,
7143 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7144 error = histedit_skip_commit(hle, worktree, repo);
7150 error = histedit_commit(&merged_paths, worktree, fileindex,
7151 tmp_branch, hle, repo);
7152 got_worktree_rebase_pathlist_free(&merged_paths);
7157 if (rebase_status == GOT_STATUS_CONFLICT) {
7158 error = got_worktree_histedit_postpone(worktree, fileindex);
7161 error = got_error_msg(GOT_ERR_CONFLICTS,
7162 "conflicts must be resolved before histedit can continue");
7164 error = histedit_complete(worktree, fileindex, tmp_branch,
7167 got_object_id_queue_free(&commits);
7168 histedit_free_list(&histedit_cmds);
7169 free(head_commit_id);
7170 free(base_commit_id);
7171 free(resume_commit_id);
7173 got_object_commit_close(commit);
7175 got_ref_close(branch);
7177 got_ref_close(tmp_branch);
7179 got_worktree_close(worktree);
7181 got_repo_close(repo);
7186 usage_integrate(void)
7188 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7192 static const struct got_error *
7193 cmd_integrate(int argc, char *argv[])
7195 const struct got_error *error = NULL;
7196 struct got_repository *repo = NULL;
7197 struct got_worktree *worktree = NULL;
7198 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7199 const char *branch_arg = NULL;
7200 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7201 struct got_fileindex *fileindex = NULL;
7202 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7203 int ch, did_something = 0;
7205 while ((ch = getopt(argc, argv, "")) != -1) {
7218 branch_arg = argv[0];
7220 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7221 "unveil", NULL) == -1)
7224 cwd = getcwd(NULL, 0);
7226 error = got_error_from_errno("getcwd");
7230 error = got_worktree_open(&worktree, cwd);
7234 error = check_rebase_or_histedit_in_progress(worktree);
7238 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7243 error = apply_unveil(got_repo_get_path(repo), 0,
7244 got_worktree_get_root_path(worktree));
7248 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7249 error = got_error_from_errno("asprintf");
7253 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7254 &base_branch_ref, worktree, refname, repo);
7258 refname = strdup(got_ref_get_name(branch_ref));
7259 if (refname == NULL) {
7260 error = got_error_from_errno("strdup");
7261 got_worktree_integrate_abort(worktree, fileindex, repo,
7262 branch_ref, base_branch_ref);
7265 base_refname = strdup(got_ref_get_name(base_branch_ref));
7266 if (base_refname == NULL) {
7267 error = got_error_from_errno("strdup");
7268 got_worktree_integrate_abort(worktree, fileindex, repo,
7269 branch_ref, base_branch_ref);
7273 error = got_ref_resolve(&commit_id, repo, branch_ref);
7277 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7281 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7282 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7283 "specified branch has already been integrated");
7284 got_worktree_integrate_abort(worktree, fileindex, repo,
7285 branch_ref, base_branch_ref);
7289 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7291 if (error->code == GOT_ERR_ANCESTRY)
7292 error = got_error(GOT_ERR_REBASE_REQUIRED);
7293 got_worktree_integrate_abort(worktree, fileindex, repo,
7294 branch_ref, base_branch_ref);
7298 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7299 branch_ref, base_branch_ref, update_progress, &did_something,
7300 check_cancelled, NULL);
7304 printf("Integrated %s into %s\n", refname, base_refname);
7307 got_repo_close(repo);
7309 got_worktree_close(worktree);
7311 free(base_commit_id);
7321 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7322 "[file-path ...]\n",
7327 static const struct got_error *
7328 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7329 const char *path, struct got_object_id *blob_id,
7330 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7331 int dirfd, const char *de_name)
7333 const struct got_error *err = NULL;
7334 char *id_str = NULL;
7336 if (staged_status != GOT_STATUS_ADD &&
7337 staged_status != GOT_STATUS_MODIFY &&
7338 staged_status != GOT_STATUS_DELETE)
7341 if (staged_status == GOT_STATUS_ADD ||
7342 staged_status == GOT_STATUS_MODIFY)
7343 err = got_object_id_str(&id_str, staged_blob_id);
7345 err = got_object_id_str(&id_str, blob_id);
7349 printf("%s %c %s\n", id_str, staged_status, path);
7354 static const struct got_error *
7355 cmd_stage(int argc, char *argv[])
7357 const struct got_error *error = NULL;
7358 struct got_repository *repo = NULL;
7359 struct got_worktree *worktree = NULL;
7361 struct got_pathlist_head paths;
7362 struct got_pathlist_entry *pe;
7363 int ch, list_stage = 0, pflag = 0;
7364 FILE *patch_script_file = NULL;
7365 const char *patch_script_path = NULL;
7366 struct choose_patch_arg cpa;
7370 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7379 patch_script_path = optarg;
7391 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7392 "unveil", NULL) == -1)
7395 if (list_stage && (pflag || patch_script_path))
7396 errx(1, "-l option cannot be used with other options");
7397 if (patch_script_path && !pflag)
7398 errx(1, "-F option can only be used together with -p option");
7400 cwd = getcwd(NULL, 0);
7402 error = got_error_from_errno("getcwd");
7406 error = got_worktree_open(&worktree, cwd);
7410 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7415 if (patch_script_path) {
7416 patch_script_file = fopen(patch_script_path, "r");
7417 if (patch_script_file == NULL) {
7418 error = got_error_from_errno2("fopen",
7423 error = apply_unveil(got_repo_get_path(repo), 0,
7424 got_worktree_get_root_path(worktree));
7428 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7433 error = got_worktree_status(worktree, &paths, repo,
7434 print_stage, NULL, check_cancelled, NULL);
7436 cpa.patch_script_file = patch_script_file;
7437 cpa.action = "stage";
7438 error = got_worktree_stage(worktree, &paths,
7439 pflag ? NULL : print_status, NULL,
7440 pflag ? choose_patch : NULL, &cpa, repo);
7443 if (patch_script_file && fclose(patch_script_file) == EOF &&
7445 error = got_error_from_errno2("fclose", patch_script_path);
7447 got_repo_close(repo);
7449 got_worktree_close(worktree);
7450 TAILQ_FOREACH(pe, &paths, entry)
7451 free((char *)pe->path);
7452 got_pathlist_free(&paths);
7460 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7461 "[file-path ...]\n",
7467 static const struct got_error *
7468 cmd_unstage(int argc, char *argv[])
7470 const struct got_error *error = NULL;
7471 struct got_repository *repo = NULL;
7472 struct got_worktree *worktree = NULL;
7474 struct got_pathlist_head paths;
7475 struct got_pathlist_entry *pe;
7476 int ch, did_something = 0, pflag = 0;
7477 FILE *patch_script_file = NULL;
7478 const char *patch_script_path = NULL;
7479 struct choose_patch_arg cpa;
7483 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7489 patch_script_path = optarg;
7501 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7502 "unveil", NULL) == -1)
7505 if (patch_script_path && !pflag)
7506 errx(1, "-F option can only be used together with -p option");
7508 cwd = getcwd(NULL, 0);
7510 error = got_error_from_errno("getcwd");
7514 error = got_worktree_open(&worktree, cwd);
7518 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7523 if (patch_script_path) {
7524 patch_script_file = fopen(patch_script_path, "r");
7525 if (patch_script_file == NULL) {
7526 error = got_error_from_errno2("fopen",
7532 error = apply_unveil(got_repo_get_path(repo), 0,
7533 got_worktree_get_root_path(worktree));
7537 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7541 cpa.patch_script_file = patch_script_file;
7542 cpa.action = "unstage";
7543 error = got_worktree_unstage(worktree, &paths, update_progress,
7544 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7546 if (patch_script_file && fclose(patch_script_file) == EOF &&
7548 error = got_error_from_errno2("fclose", patch_script_path);
7550 got_repo_close(repo);
7552 got_worktree_close(worktree);
7553 TAILQ_FOREACH(pe, &paths, entry)
7554 free((char *)pe->path);
7555 got_pathlist_free(&paths);
7563 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7564 "arg1 [arg2 ...]\n", getprogname());
7568 static const struct got_error *
7569 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7571 const struct got_error *err;
7572 struct got_blob_object *blob;
7574 err = got_object_open_as_blob(&blob, repo, id, 8192);
7578 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7579 got_object_blob_close(blob);
7583 static const struct got_error *
7584 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7586 const struct got_error *err;
7587 struct got_tree_object *tree;
7590 err = got_object_open_as_tree(&tree, repo, id);
7594 nentries = got_object_tree_get_nentries(tree);
7595 for (i = 0; i < nentries; i++) {
7596 struct got_tree_entry *te;
7598 if (sigint_received || sigpipe_received)
7600 te = got_object_tree_get_entry(tree, i);
7601 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7604 fprintf(outfile, "%s %.7o %s\n", id_str,
7605 got_tree_entry_get_mode(te),
7606 got_tree_entry_get_name(te));
7610 got_object_tree_close(tree);
7614 static const struct got_error *
7615 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7617 const struct got_error *err;
7618 struct got_commit_object *commit;
7619 const struct got_object_id_queue *parent_ids;
7620 struct got_object_qid *pid;
7621 char *id_str = NULL;
7622 const char *logmsg = NULL;
7624 err = got_object_open_as_commit(&commit, repo, id);
7628 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7632 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7633 parent_ids = got_object_commit_get_parent_ids(commit);
7634 fprintf(outfile, "numparents %d\n",
7635 got_object_commit_get_nparents(commit));
7636 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7638 err = got_object_id_str(&pid_str, pid->id);
7641 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7644 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7645 got_object_commit_get_author(commit),
7646 got_object_commit_get_author_time(commit));
7648 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7649 got_object_commit_get_author(commit),
7650 got_object_commit_get_committer_time(commit));
7652 logmsg = got_object_commit_get_logmsg_raw(commit);
7653 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7654 fprintf(outfile, "%s", logmsg);
7657 got_object_commit_close(commit);
7661 static const struct got_error *
7662 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7664 const struct got_error *err;
7665 struct got_tag_object *tag;
7666 char *id_str = NULL;
7667 const char *tagmsg = NULL;
7669 err = got_object_open_as_tag(&tag, repo, id);
7673 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7677 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7679 switch (got_object_tag_get_object_type(tag)) {
7680 case GOT_OBJ_TYPE_BLOB:
7681 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7682 GOT_OBJ_LABEL_BLOB);
7684 case GOT_OBJ_TYPE_TREE:
7685 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7686 GOT_OBJ_LABEL_TREE);
7688 case GOT_OBJ_TYPE_COMMIT:
7689 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7690 GOT_OBJ_LABEL_COMMIT);
7692 case GOT_OBJ_TYPE_TAG:
7693 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7700 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7701 got_object_tag_get_name(tag));
7703 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7704 got_object_tag_get_tagger(tag),
7705 got_object_tag_get_tagger_time(tag));
7707 tagmsg = got_object_tag_get_message(tag);
7708 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7709 fprintf(outfile, "%s", tagmsg);
7712 got_object_tag_close(tag);
7716 static const struct got_error *
7717 cmd_cat(int argc, char *argv[])
7719 const struct got_error *error;
7720 struct got_repository *repo = NULL;
7721 struct got_worktree *worktree = NULL;
7722 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7723 const char *commit_id_str = NULL;
7724 struct got_object_id *id = NULL, *commit_id = NULL;
7725 int ch, obj_type, i, force_path = 0;
7728 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7733 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7736 commit_id_str = optarg;
7739 repo_path = realpath(optarg, NULL);
7740 if (repo_path == NULL)
7741 return got_error_from_errno2("realpath",
7743 got_path_strip_trailing_slashes(repo_path);
7757 cwd = getcwd(NULL, 0);
7759 error = got_error_from_errno("getcwd");
7762 error = got_worktree_open(&worktree, cwd);
7763 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7766 if (repo_path == NULL) {
7768 got_worktree_get_repo_path(worktree));
7769 if (repo_path == NULL) {
7770 error = got_error_from_errno("strdup");
7776 if (repo_path == NULL) {
7777 repo_path = getcwd(NULL, 0);
7778 if (repo_path == NULL)
7779 return got_error_from_errno("getcwd");
7782 error = got_repo_open(&repo, repo_path, NULL);
7787 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7791 if (commit_id_str == NULL)
7792 commit_id_str = GOT_REF_HEAD;
7793 error = got_repo_match_object_id(&commit_id, NULL,
7794 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7798 for (i = 0; i < argc; i++) {
7800 error = got_object_id_by_path(&id, repo, commit_id,
7805 error = got_repo_match_object_id(&id, &label, argv[i],
7806 GOT_OBJ_TYPE_ANY, 0, repo);
7808 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7809 error->code != GOT_ERR_NOT_REF)
7811 error = got_object_id_by_path(&id, repo,
7812 commit_id, argv[i]);
7818 error = got_object_get_type(&obj_type, repo, id);
7823 case GOT_OBJ_TYPE_BLOB:
7824 error = cat_blob(id, repo, stdout);
7826 case GOT_OBJ_TYPE_TREE:
7827 error = cat_tree(id, repo, stdout);
7829 case GOT_OBJ_TYPE_COMMIT:
7830 error = cat_commit(id, repo, stdout);
7832 case GOT_OBJ_TYPE_TAG:
7833 error = cat_tag(id, repo, stdout);
7836 error = got_error(GOT_ERR_OBJ_TYPE);
7851 got_worktree_close(worktree);
7853 const struct got_error *repo_error;
7854 repo_error = got_repo_close(repo);