2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
19 #include <sys/types.h>
21 #include <sys/param.h>
38 #include "got_version.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_opentemp.h"
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 static volatile sig_atomic_t sigint_received;
57 static volatile sig_atomic_t sigpipe_received;
60 catch_sigint(int signo)
66 catch_sigpipe(int signo)
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 const char *cmd_alias;
79 __dead static void usage(int);
80 __dead static void usage_init(void);
81 __dead static void usage_import(void);
82 __dead static void usage_checkout(void);
83 __dead static void usage_update(void);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_status(void);
89 __dead static void usage_ref(void);
90 __dead static void usage_branch(void);
91 __dead static void usage_tag(void);
92 __dead static void usage_add(void);
93 __dead static void usage_remove(void);
94 __dead static void usage_revert(void);
95 __dead static void usage_commit(void);
96 __dead static void usage_cherrypick(void);
97 __dead static void usage_backout(void);
98 __dead static void usage_rebase(void);
99 __dead static void usage_histedit(void);
100 __dead static void usage_stage(void);
101 __dead static void usage_unstage(void);
102 __dead static void usage_cat(void);
104 static const struct got_error* cmd_init(int, char *[]);
105 static const struct got_error* cmd_import(int, char *[]);
106 static const struct got_error* cmd_checkout(int, char *[]);
107 static const struct got_error* cmd_update(int, char *[]);
108 static const struct got_error* cmd_log(int, char *[]);
109 static const struct got_error* cmd_diff(int, char *[]);
110 static const struct got_error* cmd_blame(int, char *[]);
111 static const struct got_error* cmd_tree(int, char *[]);
112 static const struct got_error* cmd_status(int, char *[]);
113 static const struct got_error* cmd_ref(int, char *[]);
114 static const struct got_error* cmd_branch(int, char *[]);
115 static const struct got_error* cmd_tag(int, char *[]);
116 static const struct got_error* cmd_add(int, char *[]);
117 static const struct got_error* cmd_remove(int, char *[]);
118 static const struct got_error* cmd_revert(int, char *[]);
119 static const struct got_error* cmd_commit(int, char *[]);
120 static const struct got_error* cmd_cherrypick(int, char *[]);
121 static const struct got_error* cmd_backout(int, char *[]);
122 static const struct got_error* cmd_rebase(int, char *[]);
123 static const struct got_error* cmd_histedit(int, char *[]);
124 static const struct got_error* cmd_stage(int, char *[]);
125 static const struct got_error* cmd_unstage(int, char *[]);
126 static const struct got_error* cmd_cat(int, char *[]);
128 static struct got_cmd got_commands[] = {
129 { "init", cmd_init, usage_init, "in" },
130 { "import", cmd_import, usage_import, "im" },
131 { "checkout", cmd_checkout, usage_checkout, "co" },
132 { "update", cmd_update, usage_update, "up" },
133 { "log", cmd_log, usage_log, "" },
134 { "diff", cmd_diff, usage_diff, "di" },
135 { "blame", cmd_blame, usage_blame, "bl" },
136 { "tree", cmd_tree, usage_tree, "tr" },
137 { "status", cmd_status, usage_status, "st" },
138 { "ref", cmd_ref, usage_ref, "" },
139 { "branch", cmd_branch, usage_branch, "br" },
140 { "tag", cmd_tag, usage_tag, "" },
141 { "add", cmd_add, usage_add, "" },
142 { "remove", cmd_remove, usage_remove, "rm" },
143 { "revert", cmd_revert, usage_revert, "rv" },
144 { "commit", cmd_commit, usage_commit, "ci" },
145 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
146 { "backout", cmd_backout, usage_backout, "bo" },
147 { "rebase", cmd_rebase, usage_rebase, "rb" },
148 { "histedit", cmd_histedit, usage_histedit, "he" },
149 { "stage", cmd_stage, usage_stage, "sg" },
150 { "unstage", cmd_unstage, usage_unstage, "ug" },
151 { "cat", cmd_cat, usage_cat, "" },
159 fprintf(stderr, "commands:");
160 for (i = 0; i < nitems(got_commands); i++) {
161 struct got_cmd *cmd = &got_commands[i];
162 fprintf(stderr, " %s", cmd->cmd_name);
168 main(int argc, char *argv[])
173 int hflag = 0, Vflag = 0;
175 setlocale(LC_CTYPE, "");
177 while ((ch = getopt(argc, argv, "hV")) != -1) {
196 got_version_print_str();
203 signal(SIGINT, catch_sigint);
204 signal(SIGPIPE, catch_sigpipe);
206 for (i = 0; i < nitems(got_commands); i++) {
207 const struct got_error *error;
209 cmd = &got_commands[i];
211 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
212 strcmp(cmd->cmd_alias, argv[0]) != 0)
216 got_commands[i].cmd_usage();
218 error = got_commands[i].cmd_main(argc, argv);
219 if (error && !(sigint_received || sigpipe_received)) {
220 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
227 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
235 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
242 static const struct got_error *
243 get_editor(char **abspath)
245 const struct got_error *err = NULL;
250 editor = getenv("VISUAL");
252 editor = getenv("EDITOR");
255 err = got_path_find_prog(abspath, editor);
260 if (*abspath == NULL) {
261 *abspath = strdup("/bin/ed");
262 if (*abspath == NULL)
263 return got_error_from_errno("strdup");
269 static const struct got_error *
270 apply_unveil(const char *repo_path, int repo_read_only,
271 const char *worktree_path)
273 const struct got_error *err;
276 if (unveil("gmon.out", "rwc") != 0)
277 return got_error_from_errno2("unveil", "gmon.out");
279 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
280 return got_error_from_errno2("unveil", repo_path);
282 if (worktree_path && unveil(worktree_path, "rwc") != 0)
283 return got_error_from_errno2("unveil", worktree_path);
285 if (unveil("/tmp", "rwc") != 0)
286 return got_error_from_errno2("unveil", "/tmp");
288 err = got_privsep_unveil_exec_helpers();
292 if (unveil(NULL, NULL) != 0)
293 return got_error_from_errno("unveil");
301 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
305 static const struct got_error *
306 cmd_init(int argc, char *argv[])
308 const struct got_error *error = NULL;
309 char *repo_path = NULL;
312 while ((ch = getopt(argc, argv, "")) != -1) {
324 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
330 repo_path = strdup(argv[0]);
331 if (repo_path == NULL)
332 return got_error_from_errno("strdup");
334 got_path_strip_trailing_slashes(repo_path);
336 error = got_path_mkdir(repo_path);
338 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
341 error = apply_unveil(repo_path, 0, NULL);
345 error = got_repo_init(repo_path);
357 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
358 "[-r repository-path] [-I pattern] path\n", getprogname());
363 spawn_editor(const char *editor, const char *file)
366 sig_t sighup, sigint, sigquit;
369 sighup = signal(SIGHUP, SIG_IGN);
370 sigint = signal(SIGINT, SIG_IGN);
371 sigquit = signal(SIGQUIT, SIG_IGN);
373 switch (pid = fork()) {
377 execl(editor, editor, file, (char *)NULL);
381 while (waitpid(pid, &st, 0) == -1)
386 (void)signal(SIGHUP, sighup);
387 (void)signal(SIGINT, sigint);
388 (void)signal(SIGQUIT, sigquit);
390 if (!WIFEXITED(st)) {
395 return WEXITSTATUS(st);
398 static const struct got_error *
399 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
400 const char *initial_content)
402 const struct got_error *err = NULL;
406 int content_changed = 0;
411 if (stat(logmsg_path, &st) == -1)
412 return got_error_from_errno2("stat", logmsg_path);
414 if (spawn_editor(editor, logmsg_path) == -1)
415 return got_error_from_errno("failed spawning editor");
417 if (stat(logmsg_path, &st2) == -1)
418 return got_error_from_errno("stat");
420 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
421 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
422 "no changes made to commit message, aborting");
424 *logmsg = malloc(st2.st_size + 1);
426 return got_error_from_errno("malloc");
430 fp = fopen(logmsg_path, "r");
432 err = got_error_from_errno("fopen");
435 while (fgets(buf, sizeof(buf), fp) != NULL) {
436 if (!content_changed && strcmp(buf, initial_content) != 0)
438 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
439 continue; /* remove comments and leading empty lines */
440 len = strlcat(*logmsg, buf, st2.st_size);
444 while (len > 0 && (*logmsg)[len - 1] == '\n') {
445 (*logmsg)[len - 1] = '\0';
449 if (len == 0 || !content_changed)
450 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
451 "commit message cannot be empty, aborting");
460 static const struct got_error *
461 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
462 const char *branch_name)
464 char *initial_content = NULL, *logmsg_path = NULL;
465 const struct got_error *err = NULL;
468 if (asprintf(&initial_content,
469 "\n# %s to be imported to branch %s\n", path_dir,
471 return got_error_from_errno("asprintf");
473 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
477 dprintf(fd, initial_content);
480 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
482 free(initial_content);
487 static const struct got_error *
488 import_progress(void *arg, const char *path)
490 printf("A %s\n", path);
494 static const struct got_error *
495 get_author(char **author, struct got_repository *repo)
497 const struct got_error *err = NULL;
498 const char *got_author, *name, *email;
502 name = got_repo_get_gitconfig_author_name(repo);
503 email = got_repo_get_gitconfig_author_email(repo);
505 if (asprintf(author, "%s <%s>", name, email) == -1)
506 return got_error_from_errno("asprintf");
510 got_author = getenv("GOT_AUTHOR");
511 if (got_author == NULL) {
512 name = got_repo_get_global_gitconfig_author_name(repo);
513 email = got_repo_get_global_gitconfig_author_email(repo);
515 if (asprintf(author, "%s <%s>", name, email) == -1)
516 return got_error_from_errno("asprintf");
519 /* TODO: Look up user in password database? */
520 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
523 *author = strdup(got_author);
525 return got_error_from_errno("strdup");
528 * Really dumb email address check; we're only doing this to
529 * avoid git's object parser breaking on commits we create.
531 while (*got_author && *got_author != '<')
533 if (*got_author != '<') {
534 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
537 while (*got_author && *got_author != '@')
539 if (*got_author != '@') {
540 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
543 while (*got_author && *got_author != '>')
545 if (*got_author != '>')
546 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
555 static const struct got_error *
556 get_gitconfig_path(char **gitconfig_path)
558 const char *homedir = getenv("HOME");
560 *gitconfig_path = NULL;
562 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
563 return got_error_from_errno("asprintf");
569 static const struct got_error *
570 cmd_import(int argc, char *argv[])
572 const struct got_error *error = NULL;
573 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
574 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
575 const char *branch_name = "master";
576 char *refname = NULL, *id_str = NULL;
577 struct got_repository *repo = NULL;
578 struct got_reference *branch_ref = NULL, *head_ref = NULL;
579 struct got_object_id *new_commit_id = NULL;
581 struct got_pathlist_head ignores;
582 struct got_pathlist_entry *pe;
584 TAILQ_INIT(&ignores);
586 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
589 branch_name = optarg;
592 logmsg = strdup(optarg);
593 if (logmsg == NULL) {
594 error = got_error_from_errno("strdup");
599 repo_path = realpath(optarg, NULL);
600 if (repo_path == NULL) {
601 error = got_error_from_errno("realpath");
606 if (optarg[0] == '\0')
608 error = got_pathlist_insert(&pe, &ignores, optarg,
623 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
631 if (repo_path == NULL) {
632 repo_path = getcwd(NULL, 0);
633 if (repo_path == NULL)
634 return got_error_from_errno("getcwd");
636 got_path_strip_trailing_slashes(repo_path);
637 error = get_gitconfig_path(&gitconfig_path);
640 error = got_repo_open(&repo, repo_path, gitconfig_path);
644 error = get_author(&author, repo);
648 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
649 error = got_error_from_errno("asprintf");
653 error = got_ref_open(&branch_ref, repo, refname, 0);
655 if (error->code != GOT_ERR_NOT_REF)
658 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
659 "import target branch already exists");
663 path_dir = realpath(argv[0], NULL);
664 if (path_dir == NULL) {
665 error = got_error_from_errno("realpath");
668 got_path_strip_trailing_slashes(path_dir);
671 * unveil(2) traverses exec(2); if an editor is used we have
672 * to apply unveil after the log message has been written.
674 if (logmsg == NULL || strlen(logmsg) == 0) {
675 error = get_editor(&editor);
679 error = collect_import_msg(&logmsg, editor, path_dir, refname);
684 if (unveil(path_dir, "r") != 0)
685 return got_error_from_errno2("unveil", path_dir);
687 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
691 error = got_repo_import(&new_commit_id, path_dir, logmsg,
692 author, &ignores, repo, import_progress, NULL);
696 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
700 error = got_ref_write(branch_ref, repo);
704 error = got_object_id_str(&id_str, new_commit_id);
708 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
710 if (error->code != GOT_ERR_NOT_REF)
713 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
718 error = got_ref_write(head_ref, repo);
723 printf("Created branch %s with commit %s\n",
724 got_ref_get_name(branch_ref), id_str);
733 free(gitconfig_path);
735 got_ref_close(branch_ref);
737 got_ref_close(head_ref);
744 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
745 "[-p prefix] repository-path [worktree-path]\n", getprogname());
749 static const struct got_error *
750 checkout_progress(void *arg, unsigned char status, const char *path)
752 char *worktree_path = arg;
754 /* Base commit bump happens silently. */
755 if (status == GOT_STATUS_BUMP_BASE)
758 while (path[0] == '/')
761 printf("%c %s/%s\n", status, worktree_path, path);
765 static const struct got_error *
766 check_cancelled(void *arg)
768 if (sigint_received || sigpipe_received)
769 return got_error(GOT_ERR_CANCELLED);
773 static const struct got_error *
774 check_linear_ancestry(struct got_object_id *commit_id,
775 struct got_object_id *base_commit_id, struct got_repository *repo)
777 const struct got_error *err = NULL;
778 struct got_object_id *yca_id;
780 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
781 commit_id, base_commit_id, repo, check_cancelled, NULL);
786 return got_error(GOT_ERR_ANCESTRY);
789 * Require a straight line of history between the target commit
790 * and the work tree's base commit.
792 * Non-linear situations such as this require a rebase:
794 * (commit) D F (base_commit)
802 * 'got update' only handles linear cases:
803 * Update forwards in time: A (base/yca) - B - C - D (commit)
804 * Update backwards in time: D (base) - C - B - A (commit/yca)
806 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
807 got_object_id_cmp(base_commit_id, yca_id) != 0)
808 return got_error(GOT_ERR_ANCESTRY);
814 static const struct got_error *
815 check_same_branch(struct got_object_id *commit_id,
816 struct got_reference *head_ref, struct got_object_id *yca_id,
817 struct got_repository *repo)
819 const struct got_error *err = NULL;
820 struct got_commit_graph *graph = NULL;
821 struct got_object_id *head_commit_id = NULL;
822 int is_same_branch = 0;
824 err = got_ref_resolve(&head_commit_id, repo, head_ref);
828 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
832 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
837 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
841 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
842 check_cancelled, NULL);
847 struct got_object_id *id;
848 err = got_commit_graph_iter_next(&id, graph);
850 if (err->code == GOT_ERR_ITER_COMPLETED) {
853 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
855 err = got_commit_graph_fetch_commits(graph, 1,
856 repo, check_cancelled, NULL);
862 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
864 if (got_object_id_cmp(id, commit_id) == 0) {
872 got_commit_graph_close(graph);
873 free(head_commit_id);
874 if (!err && !is_same_branch)
875 err = got_error(GOT_ERR_ANCESTRY);
879 static const struct got_error *
880 resolve_commit_arg(struct got_object_id **commit_id,
881 const char *commit_id_arg, struct got_repository *repo)
883 const struct got_error *err;
884 struct got_reference *ref;
885 struct got_tag_object *tag;
887 err = got_repo_object_match_tag(&tag, commit_id_arg,
888 GOT_OBJ_TYPE_COMMIT, repo);
890 *commit_id = got_object_id_dup(
891 got_object_tag_get_object_id(tag));
892 if (*commit_id == NULL)
893 err = got_error_from_errno("got_object_id_dup");
894 got_object_tag_close(tag);
896 } else if (err->code != GOT_ERR_NO_OBJ)
899 err = got_ref_open(&ref, repo, commit_id_arg, 0);
901 err = got_ref_resolve(commit_id, repo, ref);
904 if (err->code != GOT_ERR_NOT_REF)
906 err = got_repo_match_object_id_prefix(commit_id,
907 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
912 static const struct got_error *
913 cmd_checkout(int argc, char *argv[])
915 const struct got_error *error = NULL;
916 struct got_repository *repo = NULL;
917 struct got_reference *head_ref = NULL;
918 struct got_worktree *worktree = NULL;
919 char *repo_path = NULL;
920 char *worktree_path = NULL;
921 const char *path_prefix = "";
922 const char *branch_name = GOT_REF_HEAD;
923 char *commit_id_str = NULL;
924 int ch, same_path_prefix;
925 struct got_pathlist_head paths;
929 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
932 branch_name = optarg;
935 commit_id_str = strdup(optarg);
936 if (commit_id_str == NULL)
937 return got_error_from_errno("strdup");
940 path_prefix = optarg;
952 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
953 "unveil", NULL) == -1)
957 char *cwd, *base, *dotgit;
958 repo_path = realpath(argv[0], NULL);
959 if (repo_path == NULL)
960 return got_error_from_errno2("realpath", argv[0]);
961 cwd = getcwd(NULL, 0);
963 error = got_error_from_errno("getcwd");
966 if (path_prefix[0]) {
967 base = basename(path_prefix);
969 error = got_error_from_errno2("basename",
974 base = basename(repo_path);
976 error = got_error_from_errno2("basename",
981 dotgit = strstr(base, ".git");
984 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
985 error = got_error_from_errno("asprintf");
990 } else if (argc == 2) {
991 repo_path = realpath(argv[0], NULL);
992 if (repo_path == NULL) {
993 error = got_error_from_errno2("realpath", argv[0]);
996 worktree_path = realpath(argv[1], NULL);
997 if (worktree_path == NULL) {
998 if (errno != ENOENT) {
999 error = got_error_from_errno2("realpath",
1003 worktree_path = strdup(argv[1]);
1004 if (worktree_path == NULL) {
1005 error = got_error_from_errno("strdup");
1012 got_path_strip_trailing_slashes(repo_path);
1013 got_path_strip_trailing_slashes(worktree_path);
1015 error = got_repo_open(&repo, repo_path, NULL);
1019 /* Pre-create work tree path for unveil(2) */
1020 error = got_path_mkdir(worktree_path);
1022 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1023 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1025 if (!got_path_dir_is_empty(worktree_path)) {
1026 error = got_error_path(worktree_path,
1027 GOT_ERR_DIR_NOT_EMPTY);
1032 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1036 error = got_ref_open(&head_ref, repo, branch_name, 0);
1040 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1041 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1044 error = got_worktree_open(&worktree, worktree_path);
1048 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1052 if (!same_path_prefix) {
1053 error = got_error(GOT_ERR_PATH_PREFIX);
1057 if (commit_id_str) {
1058 struct got_object_id *commit_id;
1059 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1062 error = check_linear_ancestry(commit_id,
1063 got_worktree_get_base_commit_id(worktree), repo);
1064 if (error != NULL) {
1068 error = check_same_branch(commit_id, head_ref, NULL, repo);
1071 error = got_worktree_set_base_commit_id(worktree, repo,
1078 error = got_pathlist_append(&paths, "", NULL);
1081 error = got_worktree_checkout_files(worktree, &paths, repo,
1082 checkout_progress, worktree_path, check_cancelled, NULL);
1086 printf("Now shut up and hack\n");
1089 got_pathlist_free(&paths);
1090 free(commit_id_str);
1092 free(worktree_path);
1099 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1104 static const struct got_error *
1105 update_progress(void *arg, unsigned char status, const char *path)
1107 int *did_something = arg;
1109 if (status == GOT_STATUS_EXISTS)
1114 /* Base commit bump happens silently. */
1115 if (status == GOT_STATUS_BUMP_BASE)
1118 while (path[0] == '/')
1120 printf("%c %s\n", status, path);
1124 static const struct got_error *
1125 switch_head_ref(struct got_reference *head_ref,
1126 struct got_object_id *commit_id, struct got_worktree *worktree,
1127 struct got_repository *repo)
1129 const struct got_error *err = NULL;
1131 int ref_has_moved = 0;
1133 /* Trivial case: switching between two different references. */
1134 if (strcmp(got_ref_get_name(head_ref),
1135 got_worktree_get_head_ref_name(worktree)) != 0) {
1136 printf("Switching work tree from %s to %s\n",
1137 got_worktree_get_head_ref_name(worktree),
1138 got_ref_get_name(head_ref));
1139 return got_worktree_set_head_ref(worktree, head_ref);
1142 err = check_linear_ancestry(commit_id,
1143 got_worktree_get_base_commit_id(worktree), repo);
1145 if (err->code != GOT_ERR_ANCESTRY)
1152 /* Switching to a rebased branch with the same reference name. */
1153 err = got_object_id_str(&base_id_str,
1154 got_worktree_get_base_commit_id(worktree));
1157 printf("Reference %s now points at a different branch\n",
1158 got_worktree_get_head_ref_name(worktree));
1159 printf("Switching work tree from %s to %s\n", base_id_str,
1160 got_worktree_get_head_ref_name(worktree));
1164 static const struct got_error *
1165 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1167 const struct got_error *err;
1170 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1174 return got_error(GOT_ERR_REBASING);
1176 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1180 return got_error(GOT_ERR_HISTEDIT_BUSY);
1185 static const struct got_error *
1186 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1187 char *argv[], struct got_worktree *worktree)
1189 const struct got_error *err = NULL;
1196 return got_error_from_errno("strdup");
1197 return got_pathlist_append(paths, path, NULL);
1200 for (i = 0; i < argc; i++) {
1201 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1204 err = got_pathlist_append(paths, path, NULL);
1214 static const struct got_error *
1215 cmd_update(int argc, char *argv[])
1217 const struct got_error *error = NULL;
1218 struct got_repository *repo = NULL;
1219 struct got_worktree *worktree = NULL;
1220 char *worktree_path = NULL;
1221 struct got_object_id *commit_id = NULL;
1222 char *commit_id_str = NULL;
1223 const char *branch_name = NULL;
1224 struct got_reference *head_ref = NULL;
1225 struct got_pathlist_head paths;
1226 struct got_pathlist_entry *pe;
1227 int ch, did_something = 0;
1231 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1234 branch_name = optarg;
1237 commit_id_str = strdup(optarg);
1238 if (commit_id_str == NULL)
1239 return got_error_from_errno("strdup");
1251 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1252 "unveil", NULL) == -1)
1255 worktree_path = getcwd(NULL, 0);
1256 if (worktree_path == NULL) {
1257 error = got_error_from_errno("getcwd");
1260 error = got_worktree_open(&worktree, worktree_path);
1264 error = check_rebase_or_histedit_in_progress(worktree);
1268 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1273 error = apply_unveil(got_repo_get_path(repo), 0,
1274 got_worktree_get_root_path(worktree));
1278 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1282 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1283 got_worktree_get_head_ref_name(worktree), 0);
1286 if (commit_id_str == NULL) {
1287 error = got_ref_resolve(&commit_id, repo, head_ref);
1290 error = got_object_id_str(&commit_id_str, commit_id);
1294 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1295 free(commit_id_str);
1296 commit_id_str = NULL;
1299 error = got_object_id_str(&commit_id_str, commit_id);
1305 struct got_object_id *head_commit_id;
1306 TAILQ_FOREACH(pe, &paths, entry) {
1307 if (pe->path_len == 0)
1309 error = got_error_msg(GOT_ERR_BAD_PATH,
1310 "switching between branches requires that "
1311 "the entire work tree gets updated");
1314 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1317 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1318 free(head_commit_id);
1321 error = check_same_branch(commit_id, head_ref, NULL, repo);
1324 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1328 error = check_linear_ancestry(commit_id,
1329 got_worktree_get_base_commit_id(worktree), repo);
1330 if (error != NULL) {
1331 if (error->code == GOT_ERR_ANCESTRY)
1332 error = got_error(GOT_ERR_BRANCH_MOVED);
1335 error = check_same_branch(commit_id, head_ref, NULL, repo);
1340 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1342 error = got_worktree_set_base_commit_id(worktree, repo,
1348 error = got_worktree_checkout_files(worktree, &paths, repo,
1349 update_progress, &did_something, check_cancelled, NULL);
1354 printf("Updated to commit %s\n", commit_id_str);
1356 printf("Already up-to-date\n");
1358 free(worktree_path);
1359 TAILQ_FOREACH(pe, &paths, entry)
1360 free((char *)pe->path);
1361 got_pathlist_free(&paths);
1363 free(commit_id_str);
1367 static const struct got_error *
1368 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1369 const char *path, int diff_context, struct got_repository *repo)
1371 const struct got_error *err = NULL;
1372 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1375 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1380 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1384 while (path[0] == '/')
1386 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1390 got_object_blob_close(blob1);
1391 got_object_blob_close(blob2);
1395 static const struct got_error *
1396 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1397 const char *path, int diff_context, struct got_repository *repo)
1399 const struct got_error *err = NULL;
1400 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1401 struct got_diff_blob_output_unidiff_arg arg;
1404 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1409 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1413 arg.diff_context = diff_context;
1414 arg.outfile = stdout;
1415 while (path[0] == '/')
1417 err = got_diff_tree(tree1, tree2, path, path, repo,
1418 got_diff_blob_output_unidiff, &arg, 1);
1421 got_object_tree_close(tree1);
1422 got_object_tree_close(tree2);
1426 static const struct got_error *
1427 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1428 const char *path, int diff_context, struct got_repository *repo)
1430 const struct got_error *err = NULL;
1431 struct got_commit_object *pcommit = NULL;
1432 char *id_str1 = NULL, *id_str2 = NULL;
1433 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1434 struct got_object_qid *qid;
1436 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1438 err = got_object_open_as_commit(&pcommit, repo,
1444 if (path && path[0] != '\0') {
1446 err = got_object_id_by_path(&obj_id2, repo, id, path);
1449 err = got_object_id_str(&id_str2, obj_id2);
1455 err = got_object_id_by_path(&obj_id1, repo,
1461 err = got_object_id_str(&id_str1, obj_id1);
1467 err = got_object_get_type(&obj_type, repo, obj_id2);
1472 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1474 case GOT_OBJ_TYPE_BLOB:
1475 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1478 case GOT_OBJ_TYPE_TREE:
1479 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1483 err = got_error(GOT_ERR_OBJ_TYPE);
1489 obj_id2 = got_object_commit_get_tree_id(commit);
1490 err = got_object_id_str(&id_str2, obj_id2);
1493 obj_id1 = got_object_commit_get_tree_id(pcommit);
1494 err = got_object_id_str(&id_str1, obj_id1);
1497 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1498 err = diff_trees(obj_id1, obj_id2, "", diff_context, repo);
1505 got_object_commit_close(pcommit);
1510 get_datestr(time_t *time, char *datebuf)
1512 struct tm mytm, *tm;
1515 tm = gmtime_r(time, &mytm);
1518 s = asctime_r(tm, datebuf);
1521 p = strchr(s, '\n');
1527 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1529 static const struct got_error *
1530 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1531 struct got_repository *repo, const char *path, int show_patch,
1532 int diff_context, struct got_reflist_head *refs)
1534 const struct got_error *err = NULL;
1535 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1537 time_t committer_time;
1538 const char *author, *committer;
1539 char *refs_str = NULL;
1540 struct got_reflist_entry *re;
1542 SIMPLEQ_FOREACH(re, refs, entry) {
1545 struct got_tag_object *tag = NULL;
1548 name = got_ref_get_name(re->ref);
1549 if (strcmp(name, GOT_REF_HEAD) == 0)
1551 if (strncmp(name, "refs/", 5) == 0)
1553 if (strncmp(name, "got/", 4) == 0)
1555 if (strncmp(name, "heads/", 6) == 0)
1557 if (strncmp(name, "remotes/", 8) == 0)
1559 if (strncmp(name, "tags/", 5) == 0) {
1560 err = got_object_open_as_tag(&tag, repo, re->id);
1562 if (err->code != GOT_ERR_OBJ_TYPE)
1564 /* Ref points at something other than a tag. */
1569 cmp = got_object_id_cmp(tag ?
1570 got_object_tag_get_object_id(tag) : re->id, id);
1572 got_object_tag_close(tag);
1576 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1578 err = got_error_from_errno("asprintf");
1584 err = got_object_id_str(&id_str, id);
1588 printf(GOT_COMMIT_SEP_STR);
1589 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1590 refs_str ? refs_str : "", refs_str ? ")" : "");
1595 printf("from: %s\n", got_object_commit_get_author(commit));
1596 committer_time = got_object_commit_get_committer_time(commit);
1597 datestr = get_datestr(&committer_time, datebuf);
1599 printf("date: %s UTC\n", datestr);
1600 author = got_object_commit_get_author(commit);
1601 committer = got_object_commit_get_committer(commit);
1602 if (strcmp(author, committer) != 0)
1603 printf("via: %s\n", committer);
1604 if (got_object_commit_get_nparents(commit) > 1) {
1605 const struct got_object_id_queue *parent_ids;
1606 struct got_object_qid *qid;
1608 parent_ids = got_object_commit_get_parent_ids(commit);
1609 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1610 err = got_object_id_str(&id_str, qid->id);
1613 printf("parent %d: %s\n", n++, id_str);
1618 err = got_object_commit_get_logmsg(&logmsg0, commit);
1624 line = strsep(&logmsg, "\n");
1626 printf(" %s\n", line);
1631 err = print_patch(commit, id, path, diff_context, repo);
1636 if (fflush(stdout) != 0 && err == NULL)
1637 err = got_error_from_errno("fflush");
1641 static const struct got_error *
1642 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1643 char *path, int show_patch, int diff_context, int limit,
1644 int first_parent_traversal, struct got_reflist_head *refs)
1646 const struct got_error *err;
1647 struct got_commit_graph *graph;
1649 err = got_commit_graph_open(&graph, root_id, path,
1650 first_parent_traversal, repo);
1653 err = got_commit_graph_iter_start(graph, root_id, repo,
1654 check_cancelled, NULL);
1658 struct got_commit_object *commit;
1659 struct got_object_id *id;
1661 if (sigint_received || sigpipe_received)
1664 err = got_commit_graph_iter_next(&id, graph);
1666 if (err->code == GOT_ERR_ITER_COMPLETED) {
1670 if (err->code != GOT_ERR_ITER_NEED_MORE)
1672 err = got_commit_graph_fetch_commits(graph, 1, repo,
1673 check_cancelled, NULL);
1682 err = got_object_open_as_commit(&commit, repo, id);
1685 err = print_commit(commit, id, repo, path, show_patch,
1686 diff_context, refs);
1687 got_object_commit_close(commit);
1688 if (err || (limit && --limit == 0))
1692 got_commit_graph_close(graph);
1699 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1700 "[-r repository-path] [path]\n", getprogname());
1705 get_default_log_limit(void)
1707 const char *got_default_log_limit;
1711 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1712 if (got_default_log_limit == NULL)
1714 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1720 static const struct got_error *
1721 cmd_log(int argc, char *argv[])
1723 const struct got_error *error;
1724 struct got_repository *repo = NULL;
1725 struct got_worktree *worktree = NULL;
1726 struct got_commit_object *commit = NULL;
1727 struct got_object_id *id = NULL;
1728 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1729 char *start_commit = NULL;
1730 int diff_context = 3, ch;
1731 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1733 struct got_reflist_head refs;
1735 SIMPLEQ_INIT(&refs);
1738 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1744 limit = get_default_log_limit();
1746 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1752 start_commit = optarg;
1755 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1758 err(1, "-C option %s", errstr);
1761 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1763 err(1, "-l option %s", errstr);
1766 first_parent_traversal = 1;
1769 repo_path = realpath(optarg, NULL);
1770 if (repo_path == NULL)
1771 err(1, "-r option");
1772 got_path_strip_trailing_slashes(repo_path);
1783 cwd = getcwd(NULL, 0);
1785 error = got_error_from_errno("getcwd");
1789 error = got_worktree_open(&worktree, cwd);
1790 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1797 error = got_error_from_errno("strdup");
1800 } else if (argc == 1) {
1802 error = got_worktree_resolve_path(&path, worktree,
1807 path = strdup(argv[0]);
1809 error = got_error_from_errno("strdup");
1816 if (repo_path == NULL) {
1817 repo_path = worktree ?
1818 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1820 if (repo_path == NULL) {
1821 error = got_error_from_errno("strdup");
1825 error = got_repo_open(&repo, repo_path, NULL);
1829 error = apply_unveil(got_repo_get_path(repo), 1,
1830 worktree ? got_worktree_get_root_path(worktree) : NULL);
1834 if (start_commit == NULL) {
1835 struct got_reference *head_ref;
1836 error = got_ref_open(&head_ref, repo,
1837 worktree ? got_worktree_get_head_ref_name(worktree)
1841 error = got_ref_resolve(&id, repo, head_ref);
1842 got_ref_close(head_ref);
1845 error = got_object_open_as_commit(&commit, repo, id);
1847 struct got_reference *ref;
1848 error = got_ref_open(&ref, repo, start_commit, 0);
1849 if (error == NULL) {
1851 error = got_ref_resolve(&id, repo, ref);
1855 error = got_object_get_type(&obj_type, repo, id);
1858 if (obj_type == GOT_OBJ_TYPE_TAG) {
1859 struct got_tag_object *tag;
1860 error = got_object_open_as_tag(&tag, repo, id);
1863 if (got_object_tag_get_object_type(tag) !=
1864 GOT_OBJ_TYPE_COMMIT) {
1865 got_object_tag_close(tag);
1866 error = got_error(GOT_ERR_OBJ_TYPE);
1870 id = got_object_id_dup(
1871 got_object_tag_get_object_id(tag));
1873 error = got_error_from_errno(
1874 "got_object_id_dup");
1875 got_object_tag_close(tag);
1878 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1879 error = got_error(GOT_ERR_OBJ_TYPE);
1882 error = got_object_open_as_commit(&commit, repo, id);
1886 if (commit == NULL) {
1887 error = got_repo_match_object_id_prefix(&id,
1888 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1897 const char *prefix = got_worktree_get_path_prefix(worktree);
1899 if (asprintf(&p, "%s%s%s", prefix,
1900 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1901 error = got_error_from_errno("asprintf");
1904 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1907 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1912 path = in_repo_path;
1915 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1919 error = print_commits(id, repo, path, show_patch,
1920 diff_context, limit, first_parent_traversal, &refs);
1927 got_worktree_close(worktree);
1929 const struct got_error *repo_error;
1930 repo_error = got_repo_close(repo);
1934 got_ref_list_free(&refs);
1941 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1942 "[object1 object2 | path]\n", getprogname());
1946 struct print_diff_arg {
1947 struct got_repository *repo;
1948 struct got_worktree *worktree;
1955 static const struct got_error *
1956 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1957 const char *path, struct got_object_id *blob_id,
1958 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1960 struct print_diff_arg *a = arg;
1961 const struct got_error *err = NULL;
1962 struct got_blob_object *blob1 = NULL;
1964 char *abspath = NULL, *label1 = NULL;
1967 if (a->diff_staged) {
1968 if (staged_status != GOT_STATUS_MODIFY &&
1969 staged_status != GOT_STATUS_ADD &&
1970 staged_status != GOT_STATUS_DELETE)
1973 if (staged_status == GOT_STATUS_DELETE)
1975 if (status == GOT_STATUS_NONEXISTENT)
1976 return got_error_set_errno(ENOENT, path);
1977 if (status != GOT_STATUS_MODIFY &&
1978 status != GOT_STATUS_ADD &&
1979 status != GOT_STATUS_DELETE &&
1980 status != GOT_STATUS_CONFLICT)
1984 if (!a->header_shown) {
1985 printf("diff %s %s%s\n", a->id_str,
1986 got_worktree_get_root_path(a->worktree),
1987 a->diff_staged ? " (staged changes)" : "");
1988 a->header_shown = 1;
1991 if (a->diff_staged) {
1992 const char *label1 = NULL, *label2 = NULL;
1993 switch (staged_status) {
1994 case GOT_STATUS_MODIFY:
1998 case GOT_STATUS_ADD:
2001 case GOT_STATUS_DELETE:
2005 return got_error(GOT_ERR_FILE_STATUS);
2007 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2008 label1, label2, a->diff_context, a->repo, stdout);
2011 if (staged_status == GOT_STATUS_ADD ||
2012 staged_status == GOT_STATUS_MODIFY) {
2014 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2018 err = got_object_id_str(&id_str, staged_blob_id);
2021 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2022 err = got_error_from_errno("asprintf");
2027 } else if (status != GOT_STATUS_ADD) {
2028 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2033 if (status != GOT_STATUS_DELETE) {
2034 if (asprintf(&abspath, "%s/%s",
2035 got_worktree_get_root_path(a->worktree), path) == -1) {
2036 err = got_error_from_errno("asprintf");
2040 f2 = fopen(abspath, "r");
2042 err = got_error_from_errno2("fopen", abspath);
2045 if (lstat(abspath, &sb) == -1) {
2046 err = got_error_from_errno2("lstat", abspath);
2052 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2053 a->diff_context, stdout);
2056 got_object_blob_close(blob1);
2057 if (f2 && fclose(f2) != 0 && err == NULL)
2058 err = got_error_from_errno("fclose");
2063 static const struct got_error *
2064 match_object_id(struct got_object_id **id, char **label,
2065 const char *id_str, int obj_type, int resolve_tags,
2066 struct got_repository *repo)
2068 const struct got_error *err;
2069 struct got_tag_object *tag;
2070 struct got_reference *ref = NULL;
2076 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2079 *id = got_object_id_dup(
2080 got_object_tag_get_object_id(tag));
2082 err = got_error_from_errno("got_object_id_dup");
2083 else if (asprintf(label, "refs/tags/%s",
2084 got_object_tag_get_name(tag)) == -1) {
2085 err = got_error_from_errno("asprintf");
2089 got_object_tag_close(tag);
2091 } else if (err->code != GOT_ERR_NO_OBJ)
2095 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2097 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2099 err = got_ref_open(&ref, repo, id_str, 0);
2102 *label = strdup(got_ref_get_name(ref));
2103 if (*label == NULL) {
2104 err = got_error_from_errno("strdup");
2107 err = got_ref_resolve(id, repo, ref);
2109 err = got_object_id_str(label, *id);
2110 if (*label == NULL) {
2111 err = got_error_from_errno("strdup");
2122 static const struct got_error *
2123 cmd_diff(int argc, char *argv[])
2125 const struct got_error *error;
2126 struct got_repository *repo = NULL;
2127 struct got_worktree *worktree = NULL;
2128 char *cwd = NULL, *repo_path = NULL;
2129 struct got_object_id *id1 = NULL, *id2 = NULL;
2130 const char *id_str1 = NULL, *id_str2 = NULL;
2131 char *label1 = NULL, *label2 = NULL;
2133 int diff_context = 3, diff_staged = 0, ch;
2138 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2143 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
2146 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2148 err(1, "-C option %s", errstr);
2151 repo_path = realpath(optarg, NULL);
2152 if (repo_path == NULL)
2153 err(1, "-r option");
2154 got_path_strip_trailing_slashes(repo_path);
2168 cwd = getcwd(NULL, 0);
2170 error = got_error_from_errno("getcwd");
2173 error = got_worktree_open(&worktree, cwd);
2174 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2177 if (worktree == NULL) {
2178 error = got_error(GOT_ERR_NOT_WORKTREE);
2183 "-r option can't be used when diffing a work tree");
2184 repo_path = strdup(got_worktree_get_repo_path(worktree));
2185 if (repo_path == NULL) {
2186 error = got_error_from_errno("strdup");
2190 error = got_worktree_resolve_path(&path, worktree,
2197 error = got_error_from_errno("strdup");
2201 } else if (argc == 2) {
2203 errx(1, "-s option can't be used when diffing "
2204 "objects in repository");
2207 if (worktree && repo_path == NULL) {
2209 strdup(got_worktree_get_repo_path(worktree));
2210 if (repo_path == NULL) {
2211 error = got_error_from_errno("strdup");
2218 if (repo_path == NULL) {
2219 repo_path = getcwd(NULL, 0);
2220 if (repo_path == NULL)
2221 return got_error_from_errno("getcwd");
2224 error = got_repo_open(&repo, repo_path, NULL);
2229 error = apply_unveil(got_repo_get_path(repo), 1,
2230 worktree ? got_worktree_get_root_path(worktree) : NULL);
2235 struct print_diff_arg arg;
2236 struct got_pathlist_head paths;
2241 error = got_object_id_str(&id_str,
2242 got_worktree_get_base_commit_id(worktree));
2246 arg.worktree = worktree;
2247 arg.diff_context = diff_context;
2248 arg.id_str = id_str;
2249 arg.header_shown = 0;
2250 arg.diff_staged = diff_staged;
2252 error = got_pathlist_append(&paths, path, NULL);
2256 error = got_worktree_status(worktree, &paths, repo, print_diff,
2257 &arg, check_cancelled, NULL);
2259 got_pathlist_free(&paths);
2263 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2268 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2273 error = got_object_get_type(&type1, repo, id1);
2277 error = got_object_get_type(&type2, repo, id2);
2281 if (type1 != type2) {
2282 error = got_error(GOT_ERR_OBJ_TYPE);
2287 case GOT_OBJ_TYPE_BLOB:
2288 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2289 diff_context, repo, stdout);
2291 case GOT_OBJ_TYPE_TREE:
2292 error = got_diff_objects_as_trees(id1, id2, "", "",
2293 diff_context, repo, stdout);
2295 case GOT_OBJ_TYPE_COMMIT:
2296 printf("diff %s %s\n", label1, label2);
2297 error = got_diff_objects_as_commits(id1, id2, diff_context,
2301 error = got_error(GOT_ERR_OBJ_TYPE);
2311 got_worktree_close(worktree);
2313 const struct got_error *repo_error;
2314 repo_error = got_repo_close(repo);
2325 "usage: %s blame [-c commit] [-r repository-path] path\n",
2334 char datebuf[9]; /* YY-MM-DD + NUL */
2337 struct blame_cb_args {
2338 struct blame_line *lines;
2342 off_t *line_offsets;
2344 struct got_repository *repo;
2347 static const struct got_error *
2348 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2350 const struct got_error *err = NULL;
2351 struct blame_cb_args *a = arg;
2352 struct blame_line *bline;
2354 size_t linesize = 0;
2355 struct got_commit_object *commit = NULL;
2358 time_t committer_time;
2360 if (nlines != a->nlines ||
2361 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2362 return got_error(GOT_ERR_RANGE);
2364 if (sigint_received)
2365 return got_error(GOT_ERR_ITER_COMPLETED);
2368 return NULL; /* no change in this commit */
2370 /* Annotate this line. */
2371 bline = &a->lines[lineno - 1];
2372 if (bline->annotated)
2374 err = got_object_id_str(&bline->id_str, id);
2378 err = got_object_open_as_commit(&commit, a->repo, id);
2382 bline->committer = strdup(got_object_commit_get_committer(commit));
2383 if (bline->committer == NULL) {
2384 err = got_error_from_errno("strdup");
2388 committer_time = got_object_commit_get_committer_time(commit);
2389 if (localtime_r(&committer_time, &tm) == NULL)
2390 return got_error_from_errno("localtime_r");
2391 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2392 &tm) >= sizeof(bline->datebuf)) {
2393 err = got_error(GOT_ERR_NO_SPACE);
2396 bline->annotated = 1;
2398 /* Print lines annotated so far. */
2399 bline = &a->lines[a->lineno_cur - 1];
2400 if (!bline->annotated)
2403 offset = a->line_offsets[a->lineno_cur - 1];
2404 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2405 err = got_error_from_errno("fseeko");
2409 while (bline->annotated) {
2410 char *smallerthan, *at, *nl, *committer;
2413 if (getline(&line, &linesize, a->f) == -1) {
2415 err = got_error_from_errno("getline");
2419 committer = bline->committer;
2420 smallerthan = strchr(committer, '<');
2421 if (smallerthan && smallerthan[1] != '\0')
2422 committer = smallerthan + 1;
2423 at = strchr(committer, '@');
2426 len = strlen(committer);
2428 committer[8] = '\0';
2430 nl = strchr(line, '\n');
2433 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2434 bline->id_str, bline->datebuf, committer, line);
2437 bline = &a->lines[a->lineno_cur - 1];
2441 got_object_commit_close(commit);
2446 static const struct got_error *
2447 cmd_blame(int argc, char *argv[])
2449 const struct got_error *error;
2450 struct got_repository *repo = NULL;
2451 struct got_worktree *worktree = NULL;
2452 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2453 struct got_object_id *obj_id = NULL;
2454 struct got_object_id *commit_id = NULL;
2455 struct got_blob_object *blob = NULL;
2456 char *commit_id_str = NULL;
2457 struct blame_cb_args bca;
2458 int ch, obj_type, i;
2461 memset(&bca, 0, sizeof(bca));
2464 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2469 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2472 commit_id_str = optarg;
2475 repo_path = realpath(optarg, NULL);
2476 if (repo_path == NULL)
2477 err(1, "-r option");
2478 got_path_strip_trailing_slashes(repo_path);
2494 cwd = getcwd(NULL, 0);
2496 error = got_error_from_errno("getcwd");
2499 if (repo_path == NULL) {
2500 error = got_worktree_open(&worktree, cwd);
2501 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2507 strdup(got_worktree_get_repo_path(worktree));
2508 if (repo_path == NULL)
2509 error = got_error_from_errno("strdup");
2513 repo_path = strdup(cwd);
2514 if (repo_path == NULL) {
2515 error = got_error_from_errno("strdup");
2521 error = got_repo_open(&repo, repo_path, NULL);
2525 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2530 const char *prefix = got_worktree_get_path_prefix(worktree);
2531 char *p, *worktree_subdir = cwd +
2532 strlen(got_worktree_get_root_path(worktree));
2533 if (asprintf(&p, "%s%s%s%s%s",
2534 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2535 worktree_subdir, worktree_subdir[0] ? "/" : "",
2537 error = got_error_from_errno("asprintf");
2540 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2543 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2548 if (commit_id_str == NULL) {
2549 struct got_reference *head_ref;
2550 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2553 error = got_ref_resolve(&commit_id, repo, head_ref);
2554 got_ref_close(head_ref);
2558 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2563 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2566 if (obj_id == NULL) {
2567 error = got_error(GOT_ERR_NO_OBJ);
2571 error = got_object_get_type(&obj_type, repo, obj_id);
2575 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2576 error = got_error(GOT_ERR_OBJ_TYPE);
2580 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2583 bca.f = got_opentemp();
2584 if (bca.f == NULL) {
2585 error = got_error_from_errno("got_opentemp");
2588 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2589 &bca.line_offsets, bca.f, blob);
2590 if (error || bca.nlines == 0)
2593 /* Don't include \n at EOF in the blame line count. */
2594 if (bca.line_offsets[bca.nlines - 1] == filesize)
2597 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2598 if (bca.lines == NULL) {
2599 error = got_error_from_errno("calloc");
2603 bca.nlines_prec = 0;
2611 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2612 check_cancelled, NULL);
2622 got_object_blob_close(blob);
2624 got_worktree_close(worktree);
2626 const struct got_error *repo_error;
2627 repo_error = got_repo_close(repo);
2632 for (i = 0; i < bca.nlines; i++) {
2633 struct blame_line *bline = &bca.lines[i];
2634 free(bline->id_str);
2635 free(bline->committer);
2639 free(bca.line_offsets);
2640 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2641 error = got_error_from_errno("fclose");
2649 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2655 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2656 const char *root_path)
2658 int is_root_path = (strcmp(path, root_path) == 0);
2659 const char *modestr = "";
2661 path += strlen(root_path);
2662 while (path[0] == '/')
2665 if (got_object_tree_entry_is_submodule(te))
2667 else if (S_ISLNK(te->mode))
2669 else if (S_ISDIR(te->mode))
2671 else if (te->mode & S_IXUSR)
2674 printf("%s%s%s%s%s\n", id ? id : "", path,
2675 is_root_path ? "" : "/", te->name, modestr);
2678 static const struct got_error *
2679 print_tree(const char *path, struct got_object_id *commit_id,
2680 int show_ids, int recurse, const char *root_path,
2681 struct got_repository *repo)
2683 const struct got_error *err = NULL;
2684 struct got_object_id *tree_id = NULL;
2685 struct got_tree_object *tree = NULL;
2686 const struct got_tree_entries *entries;
2687 struct got_tree_entry *te;
2689 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2693 err = got_object_open_as_tree(&tree, repo, tree_id);
2696 entries = got_object_tree_get_entries(tree);
2697 te = SIMPLEQ_FIRST(&entries->head);
2701 if (sigint_received || sigpipe_received)
2706 err = got_object_id_str(&id_str, te->id);
2709 if (asprintf(&id, "%s ", id_str) == -1) {
2710 err = got_error_from_errno("asprintf");
2716 print_entry(te, id, path, root_path);
2719 if (recurse && S_ISDIR(te->mode)) {
2721 if (asprintf(&child_path, "%s%s%s", path,
2722 path[0] == '/' && path[1] == '\0' ? "" : "/",
2724 err = got_error_from_errno("asprintf");
2727 err = print_tree(child_path, commit_id, show_ids, 1,
2734 te = SIMPLEQ_NEXT(te, entry);
2738 got_object_tree_close(tree);
2743 static const struct got_error *
2744 cmd_tree(int argc, char *argv[])
2746 const struct got_error *error;
2747 struct got_repository *repo = NULL;
2748 struct got_worktree *worktree = NULL;
2750 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2751 struct got_object_id *commit_id = NULL;
2752 char *commit_id_str = NULL;
2753 int show_ids = 0, recurse = 0;
2757 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2762 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2765 commit_id_str = optarg;
2768 repo_path = realpath(optarg, NULL);
2769 if (repo_path == NULL)
2770 err(1, "-r option");
2771 got_path_strip_trailing_slashes(repo_path);
2795 cwd = getcwd(NULL, 0);
2797 error = got_error_from_errno("getcwd");
2800 if (repo_path == NULL) {
2801 error = got_worktree_open(&worktree, cwd);
2802 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2808 strdup(got_worktree_get_repo_path(worktree));
2809 if (repo_path == NULL)
2810 error = got_error_from_errno("strdup");
2814 repo_path = strdup(cwd);
2815 if (repo_path == NULL) {
2816 error = got_error_from_errno("strdup");
2822 error = got_repo_open(&repo, repo_path, NULL);
2826 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2832 char *p, *worktree_subdir = cwd +
2833 strlen(got_worktree_get_root_path(worktree));
2834 if (asprintf(&p, "%s/%s",
2835 got_worktree_get_path_prefix(worktree),
2836 worktree_subdir) == -1) {
2837 error = got_error_from_errno("asprintf");
2840 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2847 if (in_repo_path == NULL) {
2848 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2853 if (commit_id_str == NULL) {
2854 struct got_reference *head_ref;
2855 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2858 error = got_ref_resolve(&commit_id, repo, head_ref);
2859 got_ref_close(head_ref);
2863 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2868 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2869 in_repo_path, repo);
2876 got_worktree_close(worktree);
2878 const struct got_error *repo_error;
2879 repo_error = got_repo_close(repo);
2889 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2893 static const struct got_error *
2894 print_status(void *arg, unsigned char status, unsigned char staged_status,
2895 const char *path, struct got_object_id *blob_id,
2896 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2898 if (status == staged_status && (status == GOT_STATUS_DELETE))
2899 status = GOT_STATUS_NO_CHANGE;
2900 printf("%c%c %s\n", status, staged_status, path);
2904 static const struct got_error *
2905 cmd_status(int argc, char *argv[])
2907 const struct got_error *error = NULL;
2908 struct got_repository *repo = NULL;
2909 struct got_worktree *worktree = NULL;
2911 struct got_pathlist_head paths;
2912 struct got_pathlist_entry *pe;
2917 while ((ch = getopt(argc, argv, "")) != -1) {
2929 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2933 cwd = getcwd(NULL, 0);
2935 error = got_error_from_errno("getcwd");
2939 error = got_worktree_open(&worktree, cwd);
2943 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2948 error = apply_unveil(got_repo_get_path(repo), 1,
2949 got_worktree_get_root_path(worktree));
2953 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2957 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2958 check_cancelled, NULL);
2960 TAILQ_FOREACH(pe, &paths, entry)
2961 free((char *)pe->path);
2962 got_pathlist_free(&paths);
2971 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2976 static const struct got_error *
2977 list_refs(struct got_repository *repo)
2979 static const struct got_error *err = NULL;
2980 struct got_reflist_head refs;
2981 struct got_reflist_entry *re;
2983 SIMPLEQ_INIT(&refs);
2984 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2988 SIMPLEQ_FOREACH(re, &refs, entry) {
2990 refstr = got_ref_to_str(re->ref);
2992 return got_error_from_errno("got_ref_to_str");
2993 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2997 got_ref_list_free(&refs);
3001 static const struct got_error *
3002 delete_ref(struct got_repository *repo, const char *refname)
3004 const struct got_error *err = NULL;
3005 struct got_reference *ref;
3007 err = got_ref_open(&ref, repo, refname, 0);
3011 err = got_ref_delete(ref, repo);
3016 static const struct got_error *
3017 add_ref(struct got_repository *repo, const char *refname, const char *target)
3019 const struct got_error *err = NULL;
3020 struct got_object_id *id;
3021 struct got_reference *ref = NULL;
3024 * Don't let the user create a reference named '-'.
3025 * While technically a valid reference name, this case is usually
3026 * an unintended typo.
3028 if (refname[0] == '-' && refname[1] == '\0')
3029 return got_error(GOT_ERR_BAD_REF_NAME);
3031 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3034 struct got_reference *target_ref;
3036 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3038 err = got_ref_open(&target_ref, repo, target, 0);
3041 err = got_ref_resolve(&id, repo, target_ref);
3042 got_ref_close(target_ref);
3047 err = got_ref_alloc(&ref, refname, id);
3051 err = got_ref_write(ref, repo);
3059 static const struct got_error *
3060 add_symref(struct got_repository *repo, const char *refname, const char *target)
3062 const struct got_error *err = NULL;
3063 struct got_reference *ref = NULL;
3064 struct got_reference *target_ref = NULL;
3067 * Don't let the user create a reference named '-'.
3068 * While technically a valid reference name, this case is usually
3069 * an unintended typo.
3071 if (refname[0] == '-' && refname[1] == '\0')
3072 return got_error(GOT_ERR_BAD_REF_NAME);
3074 err = got_ref_open(&target_ref, repo, target, 0);
3078 err = got_ref_alloc_symref(&ref, refname, target_ref);
3082 err = got_ref_write(ref, repo);
3085 got_ref_close(target_ref);
3091 static const struct got_error *
3092 cmd_ref(int argc, char *argv[])
3094 const struct got_error *error = NULL;
3095 struct got_repository *repo = NULL;
3096 struct got_worktree *worktree = NULL;
3097 char *cwd = NULL, *repo_path = NULL;
3098 int ch, do_list = 0, create_symref = 0;
3099 const char *delref = NULL;
3101 /* TODO: Add -s option for adding symbolic references. */
3102 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3108 repo_path = realpath(optarg, NULL);
3109 if (repo_path == NULL)
3110 err(1, "-r option");
3111 got_path_strip_trailing_slashes(repo_path);
3125 if (do_list && delref)
3126 errx(1, "-l and -d options are mutually exclusive\n");
3131 if (do_list || delref) {
3133 errx(1, "-s option cannot be used together with the "
3134 "-l or -d options");
3137 } else if (argc != 2)
3142 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3146 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3147 "sendfd unveil", NULL) == -1)
3151 cwd = getcwd(NULL, 0);
3153 error = got_error_from_errno("getcwd");
3157 if (repo_path == NULL) {
3158 error = got_worktree_open(&worktree, cwd);
3159 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3165 strdup(got_worktree_get_repo_path(worktree));
3166 if (repo_path == NULL)
3167 error = got_error_from_errno("strdup");
3171 repo_path = strdup(cwd);
3172 if (repo_path == NULL) {
3173 error = got_error_from_errno("strdup");
3179 error = got_repo_open(&repo, repo_path, NULL);
3183 error = apply_unveil(got_repo_get_path(repo), do_list,
3184 worktree ? got_worktree_get_root_path(worktree) : NULL);
3189 error = list_refs(repo);
3191 error = delete_ref(repo, delref);
3192 else if (create_symref)
3193 error = add_symref(repo, argv[0], argv[1]);
3195 error = add_ref(repo, argv[0], argv[1]);
3198 got_repo_close(repo);
3200 got_worktree_close(worktree);
3210 "usage: %s branch [-r repository] -l | -d name | "
3211 "name [commit]\n", getprogname());
3215 static const struct got_error *
3216 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3217 struct got_reference *ref)
3219 const struct got_error *err = NULL;
3220 const char *refname, *marker = " ";
3223 refname = got_ref_get_name(ref);
3224 if (worktree && strcmp(refname,
3225 got_worktree_get_head_ref_name(worktree)) == 0) {
3226 struct got_object_id *id = NULL;
3228 err = got_ref_resolve(&id, repo, ref);
3231 if (got_object_id_cmp(id,
3232 got_worktree_get_base_commit_id(worktree)) == 0)
3239 if (strncmp(refname, "refs/heads/", 11) == 0)
3241 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3244 refstr = got_ref_to_str(ref);
3246 return got_error_from_errno("got_ref_to_str");
3248 printf("%s%s: %s\n", marker, refname, refstr);
3253 static const struct got_error *
3254 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3256 static const struct got_error *err = NULL;
3257 struct got_reflist_head refs;
3258 struct got_reflist_entry *re;
3259 struct got_reference *temp_ref = NULL;
3260 int rebase_in_progress, histedit_in_progress;
3262 SIMPLEQ_INIT(&refs);
3265 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3270 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3275 if (rebase_in_progress || histedit_in_progress) {
3276 err = got_ref_open(&temp_ref, repo,
3277 got_worktree_get_head_ref_name(worktree), 0);
3280 list_branch(repo, worktree, temp_ref);
3281 got_ref_close(temp_ref);
3285 err = got_ref_list(&refs, repo, "refs/heads",
3286 got_ref_cmp_by_name, NULL);
3290 SIMPLEQ_FOREACH(re, &refs, entry)
3291 list_branch(repo, worktree, re->ref);
3293 got_ref_list_free(&refs);
3297 static const struct got_error *
3298 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3299 const char *branch_name)
3301 const struct got_error *err = NULL;
3302 struct got_reference *ref = NULL;
3305 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3306 return got_error_from_errno("asprintf");
3308 err = got_ref_open(&ref, repo, refname, 0);
3313 strcmp(got_worktree_get_head_ref_name(worktree),
3314 got_ref_get_name(ref)) == 0) {
3315 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3316 "will not delete this work tree's current branch");
3320 err = got_ref_delete(ref, repo);
3328 static const struct got_error *
3329 add_branch(struct got_repository *repo, const char *branch_name,
3330 const char *base_branch)
3332 const struct got_error *err = NULL;
3333 struct got_object_id *id = NULL;
3335 struct got_reference *ref = NULL;
3336 char *base_refname = NULL, *refname = NULL;
3339 * Don't let the user create a branch named '-'.
3340 * While technically a valid reference name, this case is usually
3341 * an unintended typo.
3343 if (branch_name[0] == '-' && branch_name[1] == '\0')
3344 return got_error(GOT_ERR_BAD_REF_NAME);
3346 err = match_object_id(&id, &label, base_branch,
3347 GOT_OBJ_TYPE_COMMIT, 1, repo);
3351 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3352 err = got_error_from_errno("asprintf");
3356 err = got_ref_open(&ref, repo, refname, 0);
3358 err = got_error(GOT_ERR_BRANCH_EXISTS);
3360 } else if (err->code != GOT_ERR_NOT_REF)
3363 err = got_ref_alloc(&ref, refname, id);
3367 err = got_ref_write(ref, repo);
3377 static const struct got_error *
3378 cmd_branch(int argc, char *argv[])
3380 const struct got_error *error = NULL;
3381 struct got_repository *repo = NULL;
3382 struct got_worktree *worktree = NULL;
3383 char *cwd = NULL, *repo_path = NULL;
3384 int ch, do_list = 0;
3385 const char *delref = NULL;
3387 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3393 repo_path = realpath(optarg, NULL);
3394 if (repo_path == NULL)
3395 err(1, "-r option");
3396 got_path_strip_trailing_slashes(repo_path);
3407 if (do_list && delref)
3408 errx(1, "-l and -d options are mutually exclusive\n");
3413 if (do_list || delref) {
3416 } else if (argc < 1 || argc > 2)
3421 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3425 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3426 "sendfd unveil", NULL) == -1)
3430 cwd = getcwd(NULL, 0);
3432 error = got_error_from_errno("getcwd");
3436 if (repo_path == NULL) {
3437 error = got_worktree_open(&worktree, cwd);
3438 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3444 strdup(got_worktree_get_repo_path(worktree));
3445 if (repo_path == NULL)
3446 error = got_error_from_errno("strdup");
3450 repo_path = strdup(cwd);
3451 if (repo_path == NULL) {
3452 error = got_error_from_errno("strdup");
3458 error = got_repo_open(&repo, repo_path, NULL);
3462 error = apply_unveil(got_repo_get_path(repo), do_list,
3463 worktree ? got_worktree_get_root_path(worktree) : NULL);
3468 error = list_branches(repo, worktree);
3470 error = delete_branch(repo, worktree, delref);
3472 const char *base_branch;
3474 base_branch = worktree ?
3475 got_worktree_get_head_ref_name(worktree) :
3477 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3480 base_branch = argv[1];
3481 error = add_branch(repo, argv[0], base_branch);
3485 got_repo_close(repo);
3487 got_worktree_close(worktree);
3498 "usage: %s tag [-r repository] | -l | "
3499 "[-m message] name [commit]\n", getprogname());
3504 static const struct got_error *
3505 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3507 const struct got_error *err = NULL;
3508 struct got_reflist_entry *re, *se, *new;
3509 struct got_object_id *re_id, *se_id;
3510 struct got_tag_object *re_tag, *se_tag;
3511 time_t re_time, se_time;
3513 SIMPLEQ_FOREACH(re, tags, entry) {
3514 se = SIMPLEQ_FIRST(sorted);
3516 err = got_reflist_entry_dup(&new, re);
3519 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3522 err = got_ref_resolve(&re_id, repo, re->ref);
3525 err = got_object_open_as_tag(&re_tag, repo, re_id);
3529 re_time = got_object_tag_get_tagger_time(re_tag);
3530 got_object_tag_close(re_tag);
3534 err = got_ref_resolve(&se_id, repo, re->ref);
3537 err = got_object_open_as_tag(&se_tag, repo, se_id);
3541 se_time = got_object_tag_get_tagger_time(se_tag);
3542 got_object_tag_close(se_tag);
3544 if (se_time > re_time) {
3545 err = got_reflist_entry_dup(&new, re);
3548 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3551 se = SIMPLEQ_NEXT(se, entry);
3560 static const struct got_error *
3561 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3562 struct got_reference *ref2)
3564 const struct got_error *err = NULL;
3565 struct got_repository *repo = arg;
3566 struct got_object_id *id1, *id2 = NULL;
3567 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3568 time_t time1, time2;
3572 err = got_ref_resolve(&id1, repo, ref1);
3575 err = got_object_open_as_tag(&tag1, repo, id1);
3579 err = got_ref_resolve(&id2, repo, ref2);
3582 err = got_object_open_as_tag(&tag2, repo, id2);
3586 time1 = got_object_tag_get_tagger_time(tag1);
3587 time2 = got_object_tag_get_tagger_time(tag2);
3589 /* Put latest tags first. */
3592 else if (time1 > time2)
3595 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3600 got_object_tag_close(tag1);
3602 got_object_tag_close(tag2);
3606 static const struct got_error *
3607 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3609 static const struct got_error *err = NULL;
3610 struct got_reflist_head refs;
3611 struct got_reflist_entry *re;
3613 SIMPLEQ_INIT(&refs);
3615 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3619 SIMPLEQ_FOREACH(re, &refs, entry) {
3620 const char *refname;
3621 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3624 struct got_object_id *id;
3625 struct got_tag_object *tag;
3627 refname = got_ref_get_name(re->ref);
3628 if (strncmp(refname, "refs/tags/", 10) != 0)
3631 refstr = got_ref_to_str(re->ref);
3632 if (refstr == NULL) {
3633 err = got_error_from_errno("got_ref_to_str");
3636 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3639 err = got_ref_resolve(&id, repo, re->ref);
3642 err = got_object_open_as_tag(&tag, repo, id);
3646 printf("from: %s\n", got_object_tag_get_tagger(tag));
3647 tagger_time = got_object_tag_get_tagger_time(tag);
3648 datestr = get_datestr(&tagger_time, datebuf);
3650 printf("date: %s UTC\n", datestr);
3651 err = got_object_id_str(&id_str,
3652 got_object_tag_get_object_id(tag));
3655 switch (got_object_tag_get_object_type(tag)) {
3656 case GOT_OBJ_TYPE_BLOB:
3657 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3659 case GOT_OBJ_TYPE_TREE:
3660 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3662 case GOT_OBJ_TYPE_COMMIT:
3663 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3665 case GOT_OBJ_TYPE_TAG:
3666 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3672 tagmsg0 = strdup(got_object_tag_get_message(tag));
3673 got_object_tag_close(tag);
3674 if (tagmsg0 == NULL) {
3675 err = got_error_from_errno("strdup");
3681 line = strsep(&tagmsg, "\n");
3683 printf(" %s\n", line);
3688 got_ref_list_free(&refs);
3692 static const struct got_error *
3693 get_tag_message(char **tagmsg, const char *commit_id_str,
3694 const char *tag_name, const char *repo_path)
3696 const struct got_error *err = NULL;
3697 char *template = NULL, *initial_content = NULL;
3698 char *tagmsg_path = NULL, *editor = NULL;
3701 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3702 err = got_error_from_errno("asprintf");
3706 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3707 commit_id_str, tag_name) == -1) {
3708 err = got_error_from_errno("asprintf");
3712 err = got_opentemp_named_fd(&tagmsg_path, &fd, template);
3716 dprintf(fd, initial_content);
3719 err = get_editor(&editor);
3722 err = edit_logmsg(tagmsg, editor, tagmsg_path, initial_content);
3724 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3725 unlink(tagmsg_path);
3729 free(initial_content);
3733 /* Editor is done; we can now apply unveil(2) */
3735 err = apply_unveil(repo_path, 0, NULL);
3744 static const struct got_error *
3745 add_tag(struct got_repository *repo, const char *tag_name,
3746 const char *commit_arg, const char *tagmsg_arg)
3748 const struct got_error *err = NULL;
3749 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3750 char *label = NULL, *commit_id_str = NULL;
3751 struct got_reference *ref = NULL;
3752 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3755 * Don't let the user create a tag named '-'.
3756 * While technically a valid reference name, this case is usually
3757 * an unintended typo.
3759 if (tag_name[0] == '-' && tag_name[1] == '\0')
3760 return got_error(GOT_ERR_BAD_REF_NAME);
3762 err = get_author(&tagger, repo);
3766 err = match_object_id(&commit_id, &label, commit_arg,
3767 GOT_OBJ_TYPE_COMMIT, 1, repo);
3771 err = got_object_id_str(&commit_id_str, commit_id);
3775 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3776 refname = strdup(tag_name);
3777 if (refname == NULL) {
3778 err = got_error_from_errno("strdup");
3782 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3783 err = got_error_from_errno("asprintf");
3787 err = got_ref_open(&ref, repo, refname, 0);
3789 err = got_error(GOT_ERR_TAG_EXISTS);
3791 } else if (err->code != GOT_ERR_NOT_REF)
3794 if (tagmsg_arg == NULL) {
3795 err = get_tag_message(&tagmsg, commit_id_str,
3796 tag_name, got_repo_get_path(repo));
3801 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3802 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3806 err = got_ref_alloc(&ref, refname, tag_id);
3810 err = got_ref_write(ref, repo);
3814 err = got_object_id_str(&tag_id_str, tag_id);
3815 printf("Created tag %s\n", tag_id_str);
3822 free(commit_id_str);
3829 static const struct got_error *
3830 cmd_tag(int argc, char *argv[])
3832 const struct got_error *error = NULL;
3833 struct got_repository *repo = NULL;
3834 struct got_worktree *worktree = NULL;
3835 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3836 char *gitconfig_path = NULL;
3837 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3838 int ch, do_list = 0;
3840 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3846 repo_path = realpath(optarg, NULL);
3847 if (repo_path == NULL)
3848 err(1, "-r option");
3849 got_path_strip_trailing_slashes(repo_path);
3865 errx(1, "-l and -m options are mutually exclusive\n");
3868 } else if (argc < 1 || argc > 2)
3871 commit_id_arg = argv[1];
3876 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3880 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3881 "sendfd unveil", NULL) == -1)
3885 cwd = getcwd(NULL, 0);
3887 error = got_error_from_errno("getcwd");
3891 if (repo_path == NULL) {
3892 error = got_worktree_open(&worktree, cwd);
3893 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3899 strdup(got_worktree_get_repo_path(worktree));
3900 if (repo_path == NULL)
3901 error = got_error_from_errno("strdup");
3905 repo_path = strdup(cwd);
3906 if (repo_path == NULL) {
3907 error = got_error_from_errno("strdup");
3914 error = got_repo_open(&repo, repo_path, NULL);
3917 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3920 error = list_tags(repo, worktree);
3922 error = get_gitconfig_path(&gitconfig_path);
3925 error = got_repo_open(&repo, repo_path, gitconfig_path);
3930 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3935 if (commit_id_arg == NULL) {
3936 struct got_reference *head_ref;
3937 struct got_object_id *commit_id;
3938 error = got_ref_open(&head_ref, repo,
3939 worktree ? got_worktree_get_head_ref_name(worktree)
3943 error = got_ref_resolve(&commit_id, repo, head_ref);
3944 got_ref_close(head_ref);
3947 error = got_object_id_str(&commit_id_str, commit_id);
3953 error = add_tag(repo, tag_name,
3954 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
3958 got_repo_close(repo);
3960 got_worktree_close(worktree);
3963 free(gitconfig_path);
3964 free(commit_id_str);
3971 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3975 static const struct got_error *
3976 cmd_add(int argc, char *argv[])
3978 const struct got_error *error = NULL;
3979 struct got_repository *repo = NULL;
3980 struct got_worktree *worktree = NULL;
3982 struct got_pathlist_head paths;
3983 struct got_pathlist_entry *pe;
3988 while ((ch = getopt(argc, argv, "")) != -1) {
4000 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4007 cwd = getcwd(NULL, 0);
4009 error = got_error_from_errno("getcwd");
4013 error = got_worktree_open(&worktree, cwd);
4017 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4022 error = apply_unveil(got_repo_get_path(repo), 1,
4023 got_worktree_get_root_path(worktree));
4027 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4031 error = got_worktree_schedule_add(worktree, &paths, print_status,
4035 got_repo_close(repo);
4037 got_worktree_close(worktree);
4038 TAILQ_FOREACH(pe, &paths, entry)
4039 free((char *)pe->path);
4040 got_pathlist_free(&paths);
4048 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4052 static const struct got_error *
4053 print_remove_status(void *arg, unsigned char status,
4054 unsigned char staged_status, const char *path,
4055 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4056 struct got_object_id *commit_id)
4058 if (status == GOT_STATUS_NONEXISTENT)
4060 if (status == staged_status && (status == GOT_STATUS_DELETE))
4061 status = GOT_STATUS_NO_CHANGE;
4062 printf("%c%c %s\n", status, staged_status, path);
4066 static const struct got_error *
4067 cmd_remove(int argc, char *argv[])
4069 const struct got_error *error = NULL;
4070 struct got_worktree *worktree = NULL;
4071 struct got_repository *repo = NULL;
4073 struct got_pathlist_head paths;
4074 struct got_pathlist_entry *pe;
4075 int ch, delete_local_mods = 0;
4079 while ((ch = getopt(argc, argv, "f")) != -1) {
4082 delete_local_mods = 1;
4094 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4101 cwd = getcwd(NULL, 0);
4103 error = got_error_from_errno("getcwd");
4106 error = got_worktree_open(&worktree, cwd);
4110 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4115 error = apply_unveil(got_repo_get_path(repo), 1,
4116 got_worktree_get_root_path(worktree));
4120 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4124 error = got_worktree_schedule_delete(worktree, &paths,
4125 delete_local_mods, print_remove_status, NULL, repo);
4130 got_repo_close(repo);
4132 got_worktree_close(worktree);
4133 TAILQ_FOREACH(pe, &paths, entry)
4134 free((char *)pe->path);
4135 got_pathlist_free(&paths);
4143 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4144 "path ...\n", getprogname());
4148 static const struct got_error *
4149 revert_progress(void *arg, unsigned char status, const char *path)
4151 while (path[0] == '/')
4153 printf("%c %s\n", status, path);
4157 struct choose_patch_arg {
4158 FILE *patch_script_file;
4162 static const struct got_error *
4163 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4164 int nchanges, const char *action)
4167 size_t linesize = 0;
4171 case GOT_STATUS_ADD:
4172 printf("A %s\n%s this addition? [y/n] ", path, action);
4174 case GOT_STATUS_DELETE:
4175 printf("D %s\n%s this deletion? [y/n] ", path, action);
4177 case GOT_STATUS_MODIFY:
4178 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4179 return got_error_from_errno("fseek");
4180 printf(GOT_COMMIT_SEP_STR);
4181 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4183 if (ferror(patch_file))
4184 return got_error_from_errno("getline");
4185 printf(GOT_COMMIT_SEP_STR);
4186 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4187 path, n, nchanges, action);
4190 return got_error_path(path, GOT_ERR_FILE_STATUS);
4196 static const struct got_error *
4197 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4198 FILE *patch_file, int n, int nchanges)
4200 const struct got_error *err = NULL;
4202 size_t linesize = 0;
4205 struct choose_patch_arg *a = arg;
4207 *choice = GOT_PATCH_CHOICE_NONE;
4209 if (a->patch_script_file) {
4211 err = show_change(status, path, patch_file, n, nchanges,
4215 linelen = getline(&line, &linesize, a->patch_script_file);
4216 if (linelen == -1) {
4217 if (ferror(a->patch_script_file))
4218 return got_error_from_errno("getline");
4221 nl = strchr(line, '\n');
4224 if (strcmp(line, "y") == 0) {
4225 *choice = GOT_PATCH_CHOICE_YES;
4227 } else if (strcmp(line, "n") == 0) {
4228 *choice = GOT_PATCH_CHOICE_NO;
4230 } else if (strcmp(line, "q") == 0 &&
4231 status == GOT_STATUS_MODIFY) {
4232 *choice = GOT_PATCH_CHOICE_QUIT;
4235 printf("invalid response '%s'\n", line);
4240 while (resp != 'y' && resp != 'n' && resp != 'q') {
4241 err = show_change(status, path, patch_file, n, nchanges,
4248 if (status == GOT_STATUS_MODIFY) {
4249 if (resp != 'y' && resp != 'n' && resp != 'q') {
4250 printf("invalid response '%c'\n", resp);
4253 } else if (resp != 'y' && resp != 'n') {
4254 printf("invalid response '%c'\n", resp);
4260 *choice = GOT_PATCH_CHOICE_YES;
4261 else if (resp == 'n')
4262 *choice = GOT_PATCH_CHOICE_NO;
4263 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4264 *choice = GOT_PATCH_CHOICE_QUIT;
4270 static const struct got_error *
4271 cmd_revert(int argc, char *argv[])
4273 const struct got_error *error = NULL;
4274 struct got_worktree *worktree = NULL;
4275 struct got_repository *repo = NULL;
4276 char *cwd = NULL, *path = NULL;
4277 struct got_pathlist_head paths;
4278 struct got_pathlist_entry *pe;
4279 int ch, can_recurse = 0, pflag = 0;
4280 FILE *patch_script_file = NULL;
4281 const char *patch_script_path = NULL;
4282 struct choose_patch_arg cpa;
4286 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4292 patch_script_path = optarg;
4307 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4308 "unveil", NULL) == -1)
4313 if (patch_script_path && !pflag)
4314 errx(1, "-F option can only be used together with -p option");
4316 cwd = getcwd(NULL, 0);
4318 error = got_error_from_errno("getcwd");
4321 error = got_worktree_open(&worktree, cwd);
4325 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4330 if (patch_script_path) {
4331 patch_script_file = fopen(patch_script_path, "r");
4332 if (patch_script_file == NULL) {
4333 error = got_error_from_errno2("fopen",
4338 error = apply_unveil(got_repo_get_path(repo), 1,
4339 got_worktree_get_root_path(worktree));
4343 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4350 TAILQ_FOREACH(pe, &paths, entry) {
4351 if (asprintf(&ondisk_path, "%s/%s",
4352 got_worktree_get_root_path(worktree),
4354 error = got_error_from_errno("asprintf");
4357 if (lstat(ondisk_path, &sb) == -1) {
4358 if (errno == ENOENT) {
4362 error = got_error_from_errno2("lstat",
4368 if (S_ISDIR(sb.st_mode)) {
4369 error = got_error_msg(GOT_ERR_BAD_PATH,
4370 "reverting directories requires -R option");
4376 cpa.patch_script_file = patch_script_file;
4377 cpa.action = "revert";
4378 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4379 pflag ? choose_patch : NULL, &cpa, repo);
4383 if (patch_script_file && fclose(patch_script_file) == EOF &&
4385 error = got_error_from_errno2("fclose", patch_script_path);
4387 got_repo_close(repo);
4389 got_worktree_close(worktree);
4398 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4403 struct collect_commit_logmsg_arg {
4404 const char *cmdline_log;
4406 const char *worktree_path;
4407 const char *branch_name;
4408 const char *repo_path;
4413 static const struct got_error *
4414 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4417 char *initial_content = NULL;
4418 struct got_pathlist_entry *pe;
4419 const struct got_error *err = NULL;
4420 char *template = NULL;
4421 struct collect_commit_logmsg_arg *a = arg;
4425 /* if a message was specified on the command line, just use it */
4426 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4427 len = strlen(a->cmdline_log) + 1;
4428 *logmsg = malloc(len + 1);
4429 if (*logmsg == NULL)
4430 return got_error_from_errno("malloc");
4431 strlcpy(*logmsg, a->cmdline_log, len);
4435 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4436 return got_error_from_errno("asprintf");
4438 if (asprintf(&initial_content,
4439 "\n# changes to be committed on branch %s:\n",
4440 a->branch_name) == -1)
4441 return got_error_from_errno("asprintf");
4443 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4447 dprintf(fd, initial_content);
4449 TAILQ_FOREACH(pe, commitable_paths, entry) {
4450 struct got_commitable *ct = pe->data;
4451 dprintf(fd, "# %c %s\n",
4452 got_commitable_get_status(ct),
4453 got_commitable_get_path(ct));
4457 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4459 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
4460 unlink(a->logmsg_path);
4461 free(a->logmsg_path);
4462 a->logmsg_path = NULL;
4464 free(initial_content);
4467 /* Editor is done; we can now apply unveil(2) */
4469 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4478 static const struct got_error *
4479 cmd_commit(int argc, char *argv[])
4481 const struct got_error *error = NULL;
4482 struct got_worktree *worktree = NULL;
4483 struct got_repository *repo = NULL;
4484 char *cwd = NULL, *id_str = NULL;
4485 struct got_object_id *id = NULL;
4486 const char *logmsg = NULL;
4487 struct collect_commit_logmsg_arg cl_arg;
4488 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4489 int ch, rebase_in_progress, histedit_in_progress;
4490 struct got_pathlist_head paths;
4493 cl_arg.logmsg_path = NULL;
4495 while ((ch = getopt(argc, argv, "m:")) != -1) {
4510 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4511 "unveil", NULL) == -1)
4514 cwd = getcwd(NULL, 0);
4516 error = got_error_from_errno("getcwd");
4519 error = got_worktree_open(&worktree, cwd);
4523 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4526 if (rebase_in_progress) {
4527 error = got_error(GOT_ERR_REBASING);
4531 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4536 error = get_gitconfig_path(&gitconfig_path);
4539 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4544 error = get_author(&author, repo);
4549 * unveil(2) traverses exec(2); if an editor is used we have
4550 * to apply unveil after the log message has been written.
4552 if (logmsg == NULL || strlen(logmsg) == 0)
4553 error = get_editor(&editor);
4555 error = apply_unveil(got_repo_get_path(repo), 0,
4556 got_worktree_get_root_path(worktree));
4560 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4564 cl_arg.editor = editor;
4565 cl_arg.cmdline_log = logmsg;
4566 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4567 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4568 if (!histedit_in_progress) {
4569 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4570 error = got_error(GOT_ERR_COMMIT_BRANCH);
4573 cl_arg.branch_name += 11;
4575 cl_arg.repo_path = got_repo_get_path(repo);
4576 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4577 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4579 if (cl_arg.logmsg_path)
4580 fprintf(stderr, "%s: log message preserved in %s\n",
4581 getprogname(), cl_arg.logmsg_path);
4585 if (cl_arg.logmsg_path)
4586 unlink(cl_arg.logmsg_path);
4588 error = got_object_id_str(&id_str, id);
4591 printf("Created commit %s\n", id_str);
4593 free(cl_arg.logmsg_path);
4595 got_repo_close(repo);
4597 got_worktree_close(worktree);
4600 free(gitconfig_path);
4607 usage_cherrypick(void)
4609 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4613 static const struct got_error *
4614 cmd_cherrypick(int argc, char *argv[])
4616 const struct got_error *error = NULL;
4617 struct got_worktree *worktree = NULL;
4618 struct got_repository *repo = NULL;
4619 char *cwd = NULL, *commit_id_str = NULL;
4620 struct got_object_id *commit_id = NULL;
4621 struct got_commit_object *commit = NULL;
4622 struct got_object_qid *pid;
4623 struct got_reference *head_ref = NULL;
4624 int ch, did_something = 0;
4626 while ((ch = getopt(argc, argv, "")) != -1) {
4638 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4639 "unveil", NULL) == -1)
4645 cwd = getcwd(NULL, 0);
4647 error = got_error_from_errno("getcwd");
4650 error = got_worktree_open(&worktree, cwd);
4654 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4659 error = apply_unveil(got_repo_get_path(repo), 0,
4660 got_worktree_get_root_path(worktree));
4664 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4665 GOT_OBJ_TYPE_COMMIT, repo);
4666 if (error != NULL) {
4667 struct got_reference *ref;
4668 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4670 error = got_ref_open(&ref, repo, argv[0], 0);
4673 error = got_ref_resolve(&commit_id, repo, ref);
4678 error = got_object_id_str(&commit_id_str, commit_id);
4682 error = got_ref_open(&head_ref, repo,
4683 got_worktree_get_head_ref_name(worktree), 0);
4687 error = check_same_branch(commit_id, head_ref, NULL, repo);
4689 if (error->code != GOT_ERR_ANCESTRY)
4693 error = got_error(GOT_ERR_SAME_BRANCH);
4697 error = got_object_open_as_commit(&commit, repo, commit_id);
4700 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4701 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4702 commit_id, repo, update_progress, &did_something, check_cancelled,
4708 printf("Merged commit %s\n", commit_id_str);
4711 got_object_commit_close(commit);
4712 free(commit_id_str);
4714 got_ref_close(head_ref);
4716 got_worktree_close(worktree);
4718 got_repo_close(repo);
4725 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4729 static const struct got_error *
4730 cmd_backout(int argc, char *argv[])
4732 const struct got_error *error = NULL;
4733 struct got_worktree *worktree = NULL;
4734 struct got_repository *repo = NULL;
4735 char *cwd = NULL, *commit_id_str = NULL;
4736 struct got_object_id *commit_id = NULL;
4737 struct got_commit_object *commit = NULL;
4738 struct got_object_qid *pid;
4739 struct got_reference *head_ref = NULL;
4740 int ch, did_something = 0;
4742 while ((ch = getopt(argc, argv, "")) != -1) {
4754 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4755 "unveil", NULL) == -1)
4761 cwd = getcwd(NULL, 0);
4763 error = got_error_from_errno("getcwd");
4766 error = got_worktree_open(&worktree, cwd);
4770 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4775 error = apply_unveil(got_repo_get_path(repo), 0,
4776 got_worktree_get_root_path(worktree));
4780 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4781 GOT_OBJ_TYPE_COMMIT, repo);
4782 if (error != NULL) {
4783 struct got_reference *ref;
4784 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4786 error = got_ref_open(&ref, repo, argv[0], 0);
4789 error = got_ref_resolve(&commit_id, repo, ref);
4794 error = got_object_id_str(&commit_id_str, commit_id);
4798 error = got_ref_open(&head_ref, repo,
4799 got_worktree_get_head_ref_name(worktree), 0);
4803 error = check_same_branch(commit_id, head_ref, NULL, repo);
4807 error = got_object_open_as_commit(&commit, repo, commit_id);
4810 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4812 error = got_error(GOT_ERR_ROOT_COMMIT);
4816 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4817 update_progress, &did_something, check_cancelled, NULL);
4822 printf("Backed out commit %s\n", commit_id_str);
4825 got_object_commit_close(commit);
4826 free(commit_id_str);
4828 got_ref_close(head_ref);
4830 got_worktree_close(worktree);
4832 got_repo_close(repo);
4839 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4845 trim_logmsg(char *logmsg, int limit)
4850 len = strlen(logmsg);
4854 nl = strchr(logmsg, '\n');
4859 static const struct got_error *
4860 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4862 const struct got_error *err;
4863 char *logmsg0 = NULL;
4866 err = got_object_commit_get_logmsg(&logmsg0, commit);
4871 while (isspace((unsigned char)s[0]))
4874 *logmsg = strdup(s);
4875 if (*logmsg == NULL) {
4876 err = got_error_from_errno("strdup");
4880 trim_logmsg(*logmsg, limit);
4886 static const struct got_error *
4887 show_rebase_progress(struct got_commit_object *commit,
4888 struct got_object_id *old_id, struct got_object_id *new_id)
4890 const struct got_error *err;
4891 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4893 err = got_object_id_str(&old_id_str, old_id);
4898 err = got_object_id_str(&new_id_str, new_id);
4903 old_id_str[12] = '\0';
4905 new_id_str[12] = '\0';
4907 err = get_short_logmsg(&logmsg, 42, commit);
4911 printf("%s -> %s: %s\n", old_id_str,
4912 new_id_str ? new_id_str : "no-op change", logmsg);
4919 static const struct got_error *
4920 rebase_progress(void *arg, unsigned char status, const char *path)
4922 unsigned char *rebase_status = arg;
4924 while (path[0] == '/')
4926 printf("%c %s\n", status, path);
4928 if (*rebase_status == GOT_STATUS_CONFLICT)
4930 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4931 *rebase_status = status;
4935 static const struct got_error *
4936 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4937 struct got_reference *branch, struct got_reference *new_base_branch,
4938 struct got_reference *tmp_branch, struct got_repository *repo)
4940 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4941 return got_worktree_rebase_complete(worktree, fileindex,
4942 new_base_branch, tmp_branch, branch, repo);
4945 static const struct got_error *
4946 rebase_commit(struct got_pathlist_head *merged_paths,
4947 struct got_worktree *worktree, struct got_fileindex *fileindex,
4948 struct got_reference *tmp_branch,
4949 struct got_object_id *commit_id, struct got_repository *repo)
4951 const struct got_error *error;
4952 struct got_commit_object *commit;
4953 struct got_object_id *new_commit_id;
4955 error = got_object_open_as_commit(&commit, repo, commit_id);
4959 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4960 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4962 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4964 error = show_rebase_progress(commit, commit_id, NULL);
4966 error = show_rebase_progress(commit, commit_id, new_commit_id);
4967 free(new_commit_id);
4970 got_object_commit_close(commit);
4974 struct check_path_prefix_arg {
4975 const char *path_prefix;
4980 static const struct got_error *
4981 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4982 struct got_blob_object *blob2, struct got_object_id *id1,
4983 struct got_object_id *id2, const char *path1, const char *path2,
4984 struct got_repository *repo)
4986 struct check_path_prefix_arg *a = arg;
4988 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4989 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4990 return got_error(a->errcode);
4995 static const struct got_error *
4996 check_path_prefix(struct got_object_id *parent_id,
4997 struct got_object_id *commit_id, const char *path_prefix,
4998 int errcode, struct got_repository *repo)
5000 const struct got_error *err;
5001 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5002 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5003 struct check_path_prefix_arg cpp_arg;
5005 if (got_path_is_root_dir(path_prefix))
5008 err = got_object_open_as_commit(&commit, repo, commit_id);
5012 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5016 err = got_object_open_as_tree(&tree1, repo,
5017 got_object_commit_get_tree_id(parent_commit));
5021 err = got_object_open_as_tree(&tree2, repo,
5022 got_object_commit_get_tree_id(commit));
5026 cpp_arg.path_prefix = path_prefix;
5027 while (cpp_arg.path_prefix[0] == '/')
5028 cpp_arg.path_prefix++;
5029 cpp_arg.len = strlen(cpp_arg.path_prefix);
5030 cpp_arg.errcode = errcode;
5031 err = got_diff_tree(tree1, tree2, "", "", repo,
5032 check_path_prefix_in_diff, &cpp_arg, 0);
5035 got_object_tree_close(tree1);
5037 got_object_tree_close(tree2);
5039 got_object_commit_close(commit);
5041 got_object_commit_close(parent_commit);
5045 static const struct got_error *
5046 collect_commits(struct got_object_id_queue *commits,
5047 struct got_object_id *initial_commit_id,
5048 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5049 const char *path_prefix, int path_prefix_errcode,
5050 struct got_repository *repo)
5052 const struct got_error *err = NULL;
5053 struct got_commit_graph *graph = NULL;
5054 struct got_object_id *parent_id = NULL;
5055 struct got_object_qid *qid;
5056 struct got_object_id *commit_id = initial_commit_id;
5058 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5062 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5063 check_cancelled, NULL);
5066 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5067 err = got_commit_graph_iter_next(&parent_id, graph);
5069 if (err->code == GOT_ERR_ITER_COMPLETED) {
5070 err = got_error_msg(GOT_ERR_ANCESTRY,
5071 "ran out of commits to rebase before "
5072 "youngest common ancestor commit has "
5075 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5077 err = got_commit_graph_fetch_commits(graph, 1, repo,
5078 check_cancelled, NULL);
5082 err = check_path_prefix(parent_id, commit_id,
5083 path_prefix, path_prefix_errcode, repo);
5087 err = got_object_qid_alloc(&qid, commit_id);
5090 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5091 commit_id = parent_id;
5095 got_commit_graph_close(graph);
5099 static const struct got_error *
5100 cmd_rebase(int argc, char *argv[])
5102 const struct got_error *error = NULL;
5103 struct got_worktree *worktree = NULL;
5104 struct got_repository *repo = NULL;
5105 struct got_fileindex *fileindex = NULL;
5107 struct got_reference *branch = NULL;
5108 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5109 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5110 struct got_object_id *resume_commit_id = NULL;
5111 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5112 struct got_commit_object *commit = NULL;
5113 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5114 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5115 struct got_object_id_queue commits;
5116 struct got_pathlist_head merged_paths;
5117 const struct got_object_id_queue *parent_ids;
5118 struct got_object_qid *qid, *pid;
5120 SIMPLEQ_INIT(&commits);
5121 TAILQ_INIT(&merged_paths);
5123 while ((ch = getopt(argc, argv, "ac")) != -1) {
5129 continue_rebase = 1;
5141 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5142 "unveil", NULL) == -1)
5145 if (abort_rebase && continue_rebase)
5147 else if (abort_rebase || continue_rebase) {
5150 } else if (argc != 1)
5153 cwd = getcwd(NULL, 0);
5155 error = got_error_from_errno("getcwd");
5158 error = got_worktree_open(&worktree, cwd);
5162 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5167 error = apply_unveil(got_repo_get_path(repo), 0,
5168 got_worktree_get_root_path(worktree));
5172 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5178 if (!rebase_in_progress) {
5179 error = got_error(GOT_ERR_NOT_REBASING);
5182 error = got_worktree_rebase_continue(&resume_commit_id,
5183 &new_base_branch, &tmp_branch, &branch, &fileindex,
5187 printf("Switching work tree to %s\n",
5188 got_ref_get_symref_target(new_base_branch));
5189 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5190 new_base_branch, update_progress, &did_something);
5193 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5194 goto done; /* nothing else to do */
5197 if (continue_rebase) {
5198 if (!rebase_in_progress) {
5199 error = got_error(GOT_ERR_NOT_REBASING);
5202 error = got_worktree_rebase_continue(&resume_commit_id,
5203 &new_base_branch, &tmp_branch, &branch, &fileindex,
5208 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5209 resume_commit_id, repo);
5213 yca_id = got_object_id_dup(resume_commit_id);
5214 if (yca_id == NULL) {
5215 error = got_error_from_errno("got_object_id_dup");
5219 error = got_ref_open(&branch, repo, argv[0], 0);
5224 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5228 if (!continue_rebase) {
5229 struct got_object_id *base_commit_id;
5231 base_commit_id = got_worktree_get_base_commit_id(worktree);
5232 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5233 base_commit_id, branch_head_commit_id, repo,
5234 check_cancelled, NULL);
5237 if (yca_id == NULL) {
5238 error = got_error_msg(GOT_ERR_ANCESTRY,
5239 "specified branch shares no common ancestry "
5240 "with work tree's branch");
5244 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5246 if (error->code != GOT_ERR_ANCESTRY)
5250 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5251 "specified branch resolves to a commit which "
5252 "is already contained in work tree's branch");
5255 error = got_worktree_rebase_prepare(&new_base_branch,
5256 &tmp_branch, &fileindex, worktree, branch, repo);
5261 commit_id = branch_head_commit_id;
5262 error = got_object_open_as_commit(&commit, repo, commit_id);
5266 parent_ids = got_object_commit_get_parent_ids(commit);
5267 pid = SIMPLEQ_FIRST(parent_ids);
5269 if (!continue_rebase) {
5271 error = got_worktree_rebase_abort(worktree, fileindex,
5272 repo, new_base_branch, update_progress,
5276 printf("Rebase of %s aborted\n",
5277 got_ref_get_name(branch));
5279 error = got_error(GOT_ERR_EMPTY_REBASE);
5282 error = collect_commits(&commits, commit_id, pid->id,
5283 yca_id, got_worktree_get_path_prefix(worktree),
5284 GOT_ERR_REBASE_PATH, repo);
5285 got_object_commit_close(commit);
5290 if (SIMPLEQ_EMPTY(&commits)) {
5291 if (continue_rebase)
5292 error = rebase_complete(worktree, fileindex,
5293 branch, new_base_branch, tmp_branch, repo);
5295 error = got_error(GOT_ERR_EMPTY_REBASE);
5300 SIMPLEQ_FOREACH(qid, &commits, entry) {
5301 commit_id = qid->id;
5302 parent_id = pid ? pid->id : yca_id;
5305 error = got_worktree_rebase_merge_files(&merged_paths,
5306 worktree, fileindex, parent_id, commit_id, repo,
5307 rebase_progress, &rebase_status, check_cancelled, NULL);
5311 if (rebase_status == GOT_STATUS_CONFLICT) {
5312 got_worktree_rebase_pathlist_free(&merged_paths);
5316 error = rebase_commit(&merged_paths, worktree, fileindex,
5317 tmp_branch, commit_id, repo);
5318 got_worktree_rebase_pathlist_free(&merged_paths);
5323 if (rebase_status == GOT_STATUS_CONFLICT) {
5324 error = got_worktree_rebase_postpone(worktree, fileindex);
5327 error = got_error_msg(GOT_ERR_CONFLICTS,
5328 "conflicts must be resolved before rebasing can continue");
5330 error = rebase_complete(worktree, fileindex, branch,
5331 new_base_branch, tmp_branch, repo);
5333 got_object_id_queue_free(&commits);
5334 free(branch_head_commit_id);
5335 free(resume_commit_id);
5338 got_object_commit_close(commit);
5340 got_ref_close(branch);
5341 if (new_base_branch)
5342 got_ref_close(new_base_branch);
5344 got_ref_close(tmp_branch);
5346 got_worktree_close(worktree);
5348 got_repo_close(repo);
5353 usage_histedit(void)
5355 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5360 #define GOT_HISTEDIT_PICK 'p'
5361 #define GOT_HISTEDIT_EDIT 'e'
5362 #define GOT_HISTEDIT_FOLD 'f'
5363 #define GOT_HISTEDIT_DROP 'd'
5364 #define GOT_HISTEDIT_MESG 'm'
5366 static struct got_histedit_cmd {
5370 } got_histedit_cmds[] = {
5371 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5372 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5373 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5374 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5375 { GOT_HISTEDIT_MESG, "mesg",
5376 "single-line log message for commit above (open editor if empty)" },
5379 struct got_histedit_list_entry {
5380 TAILQ_ENTRY(got_histedit_list_entry) entry;
5381 struct got_object_id *commit_id;
5382 const struct got_histedit_cmd *cmd;
5385 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5387 static const struct got_error *
5388 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5389 FILE *f, struct got_repository *repo)
5391 const struct got_error *err = NULL;
5392 char *logmsg = NULL, *id_str = NULL;
5393 struct got_commit_object *commit = NULL;
5396 err = got_object_open_as_commit(&commit, repo, commit_id);
5400 err = get_short_logmsg(&logmsg, 34, commit);
5404 err = got_object_id_str(&id_str, commit_id);
5408 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5410 err = got_ferror(f, GOT_ERR_IO);
5413 got_object_commit_close(commit);
5419 static const struct got_error *
5420 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5421 struct got_repository *repo)
5423 const struct got_error *err = NULL;
5424 struct got_object_qid *qid;
5426 if (SIMPLEQ_EMPTY(commits))
5427 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5429 SIMPLEQ_FOREACH(qid, commits, entry) {
5430 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5439 static const struct got_error *
5440 write_cmd_list(FILE *f)
5442 const struct got_error *err = NULL;
5445 n = fprintf(f, "# Available histedit commands:\n");
5447 return got_ferror(f, GOT_ERR_IO);
5449 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5450 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5451 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5454 err = got_ferror(f, GOT_ERR_IO);
5458 n = fprintf(f, "# Commits will be processed in order from top to "
5459 "bottom of this file.\n");
5461 return got_ferror(f, GOT_ERR_IO);
5465 static const struct got_error *
5466 histedit_syntax_error(int lineno)
5468 static char msg[42];
5471 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5473 if (ret == -1 || ret >= sizeof(msg))
5474 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5476 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5479 static const struct got_error *
5480 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5481 char *logmsg, struct got_repository *repo)
5483 const struct got_error *err;
5484 struct got_commit_object *folded_commit = NULL;
5485 char *id_str, *folded_logmsg = NULL;
5487 err = got_object_id_str(&id_str, hle->commit_id);
5491 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5495 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5498 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5499 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5500 folded_logmsg) == -1) {
5501 err = got_error_from_errno("asprintf");
5506 got_object_commit_close(folded_commit);
5508 free(folded_logmsg);
5512 static struct got_histedit_list_entry *
5513 get_folded_commits(struct got_histedit_list_entry *hle)
5515 struct got_histedit_list_entry *prev, *folded = NULL;
5517 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5518 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5519 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5520 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5522 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5528 static const struct got_error *
5529 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5530 struct got_repository *repo)
5532 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5533 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5534 const struct got_error *err = NULL;
5535 struct got_commit_object *commit = NULL;
5537 struct got_histedit_list_entry *folded = NULL;
5539 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5543 folded = get_folded_commits(hle);
5545 while (folded != hle) {
5546 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5547 folded = TAILQ_NEXT(folded, entry);
5550 err = append_folded_commit_msg(&new_msg, folded,
5556 folded = TAILQ_NEXT(folded, entry);
5560 err = got_object_id_str(&id_str, hle->commit_id);
5563 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5566 if (asprintf(&new_msg,
5567 "%s\n# original log message of commit %s: %s",
5568 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5569 err = got_error_from_errno("asprintf");
5575 err = got_object_id_str(&id_str, hle->commit_id);
5579 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5583 dprintf(fd, logmsg);
5586 err = get_editor(&editor);
5590 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5592 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5594 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5597 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5598 err = got_error_from_errno2("unlink", logmsg_path);
5604 got_object_commit_close(commit);
5608 static const struct got_error *
5609 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5610 FILE *f, struct got_repository *repo)
5612 const struct got_error *err = NULL;
5613 char *line = NULL, *p, *end;
5617 const struct got_histedit_cmd *cmd;
5618 struct got_object_id *commit_id = NULL;
5619 struct got_histedit_list_entry *hle = NULL;
5622 len = getline(&line, &size, f);
5624 const struct got_error *getline_err;
5627 getline_err = got_error_from_errno("getline");
5628 err = got_ferror(f, getline_err->code);
5633 while (isspace((unsigned char)p[0]))
5635 if (p[0] == '#' || p[0] == '\0') {
5641 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5642 cmd = &got_histedit_cmds[i];
5643 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5644 isspace((unsigned char)p[strlen(cmd->name)])) {
5645 p += strlen(cmd->name);
5648 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5653 if (i == nitems(got_histedit_cmds)) {
5654 err = histedit_syntax_error(lineno);
5657 while (isspace((unsigned char)p[0]))
5659 if (cmd->code == GOT_HISTEDIT_MESG) {
5660 if (hle == NULL || hle->logmsg != NULL) {
5661 err = got_error(GOT_ERR_HISTEDIT_CMD);
5665 err = histedit_edit_logmsg(hle, repo);
5669 hle->logmsg = strdup(p);
5670 if (hle->logmsg == NULL) {
5671 err = got_error_from_errno("strdup");
5680 while (end[0] && !isspace((unsigned char)end[0]))
5684 err = got_object_resolve_id_str(&commit_id, repo, p);
5686 /* override error code */
5687 err = histedit_syntax_error(lineno);
5691 hle = malloc(sizeof(*hle));
5693 err = got_error_from_errno("malloc");
5697 hle->commit_id = commit_id;
5702 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5710 static const struct got_error *
5711 histedit_check_script(struct got_histedit_list *histedit_cmds,
5712 struct got_object_id_queue *commits, struct got_repository *repo)
5714 const struct got_error *err = NULL;
5715 struct got_object_qid *qid;
5716 struct got_histedit_list_entry *hle;
5717 static char msg[80];
5720 if (TAILQ_EMPTY(histedit_cmds))
5721 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5722 "histedit script contains no commands");
5723 if (SIMPLEQ_EMPTY(commits))
5724 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5726 SIMPLEQ_FOREACH(qid, commits, entry) {
5727 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5728 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5732 err = got_object_id_str(&id_str, qid->id);
5735 snprintf(msg, sizeof(msg),
5736 "commit %s missing from histedit script", id_str);
5738 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5742 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5743 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5744 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5745 "last commit in histedit script cannot be folded");
5750 static const struct got_error *
5751 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5752 const char *path, struct got_object_id_queue *commits,
5753 struct got_repository *repo)
5755 const struct got_error *err = NULL;
5759 err = get_editor(&editor);
5763 if (spawn_editor(editor, path) == -1) {
5764 err = got_error_from_errno("failed spawning editor");
5768 f = fopen(path, "r");
5770 err = got_error_from_errno("fopen");
5773 err = histedit_parse_list(histedit_cmds, f, repo);
5777 err = histedit_check_script(histedit_cmds, commits, repo);
5779 if (f && fclose(f) != 0 && err == NULL)
5780 err = got_error_from_errno("fclose");
5785 static const struct got_error *
5786 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5787 struct got_object_id_queue *, const char *, struct got_repository *);
5789 static const struct got_error *
5790 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5791 struct got_object_id_queue *commits, struct got_repository *repo)
5793 const struct got_error *err;
5797 err = got_opentemp_named(&path, &f, "got-histedit");
5801 err = write_cmd_list(f);
5805 err = histedit_write_commit_list(commits, f, repo);
5809 if (fclose(f) != 0) {
5810 err = got_error_from_errno("fclose");
5815 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5817 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5818 err->code != GOT_ERR_HISTEDIT_CMD)
5820 err = histedit_edit_list_retry(histedit_cmds, err,
5821 commits, path, repo);
5824 if (f && fclose(f) != 0 && err == NULL)
5825 err = got_error_from_errno("fclose");
5826 if (path && unlink(path) != 0 && err == NULL)
5827 err = got_error_from_errno2("unlink", path);
5832 static const struct got_error *
5833 histedit_save_list(struct got_histedit_list *histedit_cmds,
5834 struct got_worktree *worktree, struct got_repository *repo)
5836 const struct got_error *err = NULL;
5839 struct got_histedit_list_entry *hle;
5840 struct got_commit_object *commit = NULL;
5842 err = got_worktree_get_histedit_script_path(&path, worktree);
5846 f = fopen(path, "w");
5848 err = got_error_from_errno2("fopen", path);
5851 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5852 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5858 int n = fprintf(f, "%c %s\n",
5859 GOT_HISTEDIT_MESG, hle->logmsg);
5861 err = got_ferror(f, GOT_ERR_IO);
5867 if (f && fclose(f) != 0 && err == NULL)
5868 err = got_error_from_errno("fclose");
5871 got_object_commit_close(commit);
5876 histedit_free_list(struct got_histedit_list *histedit_cmds)
5878 struct got_histedit_list_entry *hle;
5880 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5881 TAILQ_REMOVE(histedit_cmds, hle, entry);
5886 static const struct got_error *
5887 histedit_load_list(struct got_histedit_list *histedit_cmds,
5888 const char *path, struct got_repository *repo)
5890 const struct got_error *err = NULL;
5893 f = fopen(path, "r");
5895 err = got_error_from_errno2("fopen", path);
5899 err = histedit_parse_list(histedit_cmds, f, repo);
5901 if (f && fclose(f) != 0 && err == NULL)
5902 err = got_error_from_errno("fclose");
5906 static const struct got_error *
5907 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5908 const struct got_error *edit_err, struct got_object_id_queue *commits,
5909 const char *path, struct got_repository *repo)
5911 const struct got_error *err = NULL, *prev_err = edit_err;
5914 while (resp != 'c' && resp != 'r' && resp != 'a') {
5915 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5916 "or (a)bort: ", getprogname(), prev_err->msg);
5921 histedit_free_list(histedit_cmds);
5922 err = histedit_run_editor(histedit_cmds, path, commits,
5925 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5926 err->code != GOT_ERR_HISTEDIT_CMD)
5933 } else if (resp == 'r') {
5934 histedit_free_list(histedit_cmds);
5935 err = histedit_edit_script(histedit_cmds,
5938 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5939 err->code != GOT_ERR_HISTEDIT_CMD)
5946 } else if (resp == 'a') {
5947 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5950 printf("invalid response '%c'\n", resp);
5956 static const struct got_error *
5957 histedit_complete(struct got_worktree *worktree,
5958 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5959 struct got_reference *branch, struct got_repository *repo)
5961 printf("Switching work tree to %s\n",
5962 got_ref_get_symref_target(branch));
5963 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5967 static const struct got_error *
5968 show_histedit_progress(struct got_commit_object *commit,
5969 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5971 const struct got_error *err;
5972 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5974 err = got_object_id_str(&old_id_str, hle->commit_id);
5979 err = got_object_id_str(&new_id_str, new_id);
5984 old_id_str[12] = '\0';
5986 new_id_str[12] = '\0';
5989 logmsg = strdup(hle->logmsg);
5990 if (logmsg == NULL) {
5991 err = got_error_from_errno("strdup");
5994 trim_logmsg(logmsg, 42);
5996 err = get_short_logmsg(&logmsg, 42, commit);
6001 switch (hle->cmd->code) {
6002 case GOT_HISTEDIT_PICK:
6003 case GOT_HISTEDIT_EDIT:
6004 printf("%s -> %s: %s\n", old_id_str,
6005 new_id_str ? new_id_str : "no-op change", logmsg);
6007 case GOT_HISTEDIT_DROP:
6008 case GOT_HISTEDIT_FOLD:
6009 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6022 static const struct got_error *
6023 histedit_commit(struct got_pathlist_head *merged_paths,
6024 struct got_worktree *worktree, struct got_fileindex *fileindex,
6025 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6026 struct got_repository *repo)
6028 const struct got_error *err;
6029 struct got_commit_object *commit;
6030 struct got_object_id *new_commit_id;
6032 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6033 && hle->logmsg == NULL) {
6034 err = histedit_edit_logmsg(hle, repo);
6039 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6043 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6044 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6047 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6049 err = show_histedit_progress(commit, hle, NULL);
6051 err = show_histedit_progress(commit, hle, new_commit_id);
6052 free(new_commit_id);
6055 got_object_commit_close(commit);
6059 static const struct got_error *
6060 histedit_skip_commit(struct got_histedit_list_entry *hle,
6061 struct got_worktree *worktree, struct got_repository *repo)
6063 const struct got_error *error;
6064 struct got_commit_object *commit;
6066 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6071 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6075 error = show_histedit_progress(commit, hle, NULL);
6076 got_object_commit_close(commit);
6080 static const struct got_error *
6081 cmd_histedit(int argc, char *argv[])
6083 const struct got_error *error = NULL;
6084 struct got_worktree *worktree = NULL;
6085 struct got_fileindex *fileindex = NULL;
6086 struct got_repository *repo = NULL;
6088 struct got_reference *branch = NULL;
6089 struct got_reference *tmp_branch = NULL;
6090 struct got_object_id *resume_commit_id = NULL;
6091 struct got_object_id *base_commit_id = NULL;
6092 struct got_object_id *head_commit_id = NULL;
6093 struct got_commit_object *commit = NULL;
6094 int ch, rebase_in_progress = 0, did_something;
6095 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6096 const char *edit_script_path = NULL;
6097 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6098 struct got_object_id_queue commits;
6099 struct got_pathlist_head merged_paths;
6100 const struct got_object_id_queue *parent_ids;
6101 struct got_object_qid *pid;
6102 struct got_histedit_list histedit_cmds;
6103 struct got_histedit_list_entry *hle;
6105 SIMPLEQ_INIT(&commits);
6106 TAILQ_INIT(&histedit_cmds);
6107 TAILQ_INIT(&merged_paths);
6109 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6118 edit_script_path = optarg;
6130 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6131 "unveil", NULL) == -1)
6134 if (abort_edit && continue_edit)
6140 * This command cannot apply unveil(2) in all cases because the
6141 * user may choose to run an editor to edit the histedit script
6142 * and to edit individual commit log messages.
6143 * unveil(2) traverses exec(2); if an editor is used we have to
6144 * apply unveil after edit script and log messages have been written.
6145 * XXX TODO: Make use of unveil(2) where possible.
6148 cwd = getcwd(NULL, 0);
6150 error = got_error_from_errno("getcwd");
6153 error = got_worktree_open(&worktree, cwd);
6157 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6162 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6165 if (rebase_in_progress) {
6166 error = got_error(GOT_ERR_REBASING);
6170 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6174 if (edit_in_progress && abort_edit) {
6175 error = got_worktree_histedit_continue(&resume_commit_id,
6176 &tmp_branch, &branch, &base_commit_id, &fileindex,
6180 printf("Switching work tree to %s\n",
6181 got_ref_get_symref_target(branch));
6182 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6183 branch, base_commit_id, update_progress, &did_something);
6186 printf("Histedit of %s aborted\n",
6187 got_ref_get_symref_target(branch));
6188 goto done; /* nothing else to do */
6189 } else if (abort_edit) {
6190 error = got_error(GOT_ERR_NOT_HISTEDIT);
6194 if (continue_edit) {
6197 if (!edit_in_progress) {
6198 error = got_error(GOT_ERR_NOT_HISTEDIT);
6202 error = got_worktree_get_histedit_script_path(&path, worktree);
6206 error = histedit_load_list(&histedit_cmds, path, repo);
6211 error = got_worktree_histedit_continue(&resume_commit_id,
6212 &tmp_branch, &branch, &base_commit_id, &fileindex,
6217 error = got_ref_resolve(&head_commit_id, repo, branch);
6221 error = got_object_open_as_commit(&commit, repo,
6225 parent_ids = got_object_commit_get_parent_ids(commit);
6226 pid = SIMPLEQ_FIRST(parent_ids);
6228 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6231 error = collect_commits(&commits, head_commit_id, pid->id,
6232 base_commit_id, got_worktree_get_path_prefix(worktree),
6233 GOT_ERR_HISTEDIT_PATH, repo);
6234 got_object_commit_close(commit);
6239 if (edit_in_progress) {
6240 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6244 error = got_ref_open(&branch, repo,
6245 got_worktree_get_head_ref_name(worktree), 0);
6249 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6250 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6251 "will not edit commit history of a branch outside "
6252 "the \"refs/heads/\" reference namespace");
6256 error = got_ref_resolve(&head_commit_id, repo, branch);
6257 got_ref_close(branch);
6262 error = got_object_open_as_commit(&commit, repo,
6266 parent_ids = got_object_commit_get_parent_ids(commit);
6267 pid = SIMPLEQ_FIRST(parent_ids);
6269 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6272 error = collect_commits(&commits, head_commit_id, pid->id,
6273 got_worktree_get_base_commit_id(worktree),
6274 got_worktree_get_path_prefix(worktree),
6275 GOT_ERR_HISTEDIT_PATH, repo);
6276 got_object_commit_close(commit);
6281 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6282 &base_commit_id, &fileindex, worktree, repo);
6286 if (edit_script_path) {
6287 error = histedit_load_list(&histedit_cmds,
6288 edit_script_path, repo);
6290 got_worktree_histedit_abort(worktree, fileindex,
6291 repo, branch, base_commit_id,
6292 update_progress, &did_something);
6296 error = histedit_edit_script(&histedit_cmds, &commits,
6299 got_worktree_histedit_abort(worktree, fileindex,
6300 repo, branch, base_commit_id,
6301 update_progress, &did_something);
6307 error = histedit_save_list(&histedit_cmds, worktree,
6310 got_worktree_histedit_abort(worktree, fileindex,
6311 repo, branch, base_commit_id,
6312 update_progress, &did_something);
6318 error = histedit_check_script(&histedit_cmds, &commits, repo);
6322 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6323 if (resume_commit_id) {
6324 if (got_object_id_cmp(hle->commit_id,
6325 resume_commit_id) != 0)
6328 resume_commit_id = NULL;
6329 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6330 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6331 error = histedit_skip_commit(hle, worktree,
6334 error = histedit_commit(NULL, worktree,
6335 fileindex, tmp_branch, hle, repo);
6342 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6343 error = histedit_skip_commit(hle, worktree, repo);
6349 error = got_object_open_as_commit(&commit, repo,
6353 parent_ids = got_object_commit_get_parent_ids(commit);
6354 pid = SIMPLEQ_FIRST(parent_ids);
6356 error = got_worktree_histedit_merge_files(&merged_paths,
6357 worktree, fileindex, pid->id, hle->commit_id, repo,
6358 rebase_progress, &rebase_status, check_cancelled, NULL);
6361 got_object_commit_close(commit);
6364 if (rebase_status == GOT_STATUS_CONFLICT) {
6365 got_worktree_rebase_pathlist_free(&merged_paths);
6369 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6371 error = got_object_id_str(&id_str, hle->commit_id);
6374 printf("Stopping histedit for amending commit %s\n",
6377 got_worktree_rebase_pathlist_free(&merged_paths);
6378 error = got_worktree_histedit_postpone(worktree,
6383 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6384 error = histedit_skip_commit(hle, worktree, repo);
6390 error = histedit_commit(&merged_paths, worktree, fileindex,
6391 tmp_branch, hle, repo);
6392 got_worktree_rebase_pathlist_free(&merged_paths);
6397 if (rebase_status == GOT_STATUS_CONFLICT) {
6398 error = got_worktree_histedit_postpone(worktree, fileindex);
6401 error = got_error_msg(GOT_ERR_CONFLICTS,
6402 "conflicts must be resolved before rebasing can continue");
6404 error = histedit_complete(worktree, fileindex, tmp_branch,
6407 got_object_id_queue_free(&commits);
6408 histedit_free_list(&histedit_cmds);
6409 free(head_commit_id);
6410 free(base_commit_id);
6411 free(resume_commit_id);
6413 got_object_commit_close(commit);
6415 got_ref_close(branch);
6417 got_ref_close(tmp_branch);
6419 got_worktree_close(worktree);
6421 got_repo_close(repo);
6428 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6429 "[file-path ...]\n",
6434 static const struct got_error *
6435 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6436 const char *path, struct got_object_id *blob_id,
6437 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6439 const struct got_error *err = NULL;
6440 char *id_str = NULL;
6442 if (staged_status != GOT_STATUS_ADD &&
6443 staged_status != GOT_STATUS_MODIFY &&
6444 staged_status != GOT_STATUS_DELETE)
6447 if (staged_status == GOT_STATUS_ADD ||
6448 staged_status == GOT_STATUS_MODIFY)
6449 err = got_object_id_str(&id_str, staged_blob_id);
6451 err = got_object_id_str(&id_str, blob_id);
6455 printf("%s %c %s\n", id_str, staged_status, path);
6460 static const struct got_error *
6461 cmd_stage(int argc, char *argv[])
6463 const struct got_error *error = NULL;
6464 struct got_repository *repo = NULL;
6465 struct got_worktree *worktree = NULL;
6467 struct got_pathlist_head paths;
6468 struct got_pathlist_entry *pe;
6469 int ch, list_stage = 0, pflag = 0;
6470 FILE *patch_script_file = NULL;
6471 const char *patch_script_path = NULL;
6472 struct choose_patch_arg cpa;
6476 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6485 patch_script_path = optarg;
6497 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6498 "unveil", NULL) == -1)
6501 if (list_stage && (pflag || patch_script_path))
6502 errx(1, "-l option cannot be used with other options");
6503 if (patch_script_path && !pflag)
6504 errx(1, "-F option can only be used together with -p option");
6506 cwd = getcwd(NULL, 0);
6508 error = got_error_from_errno("getcwd");
6512 error = got_worktree_open(&worktree, cwd);
6516 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6521 if (patch_script_path) {
6522 patch_script_file = fopen(patch_script_path, "r");
6523 if (patch_script_file == NULL) {
6524 error = got_error_from_errno2("fopen",
6529 error = apply_unveil(got_repo_get_path(repo), 0,
6530 got_worktree_get_root_path(worktree));
6534 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6539 error = got_worktree_status(worktree, &paths, repo,
6540 print_stage, NULL, check_cancelled, NULL);
6542 cpa.patch_script_file = patch_script_file;
6543 cpa.action = "stage";
6544 error = got_worktree_stage(worktree, &paths,
6545 pflag ? NULL : print_status, NULL,
6546 pflag ? choose_patch : NULL, &cpa, repo);
6549 if (patch_script_file && fclose(patch_script_file) == EOF &&
6551 error = got_error_from_errno2("fclose", patch_script_path);
6553 got_repo_close(repo);
6555 got_worktree_close(worktree);
6556 TAILQ_FOREACH(pe, &paths, entry)
6557 free((char *)pe->path);
6558 got_pathlist_free(&paths);
6566 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6567 "[file-path ...]\n",
6573 static const struct got_error *
6574 cmd_unstage(int argc, char *argv[])
6576 const struct got_error *error = NULL;
6577 struct got_repository *repo = NULL;
6578 struct got_worktree *worktree = NULL;
6580 struct got_pathlist_head paths;
6581 struct got_pathlist_entry *pe;
6582 int ch, did_something = 0, pflag = 0;
6583 FILE *patch_script_file = NULL;
6584 const char *patch_script_path = NULL;
6585 struct choose_patch_arg cpa;
6589 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6595 patch_script_path = optarg;
6607 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6608 "unveil", NULL) == -1)
6611 if (patch_script_path && !pflag)
6612 errx(1, "-F option can only be used together with -p option");
6614 cwd = getcwd(NULL, 0);
6616 error = got_error_from_errno("getcwd");
6620 error = got_worktree_open(&worktree, cwd);
6624 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6629 if (patch_script_path) {
6630 patch_script_file = fopen(patch_script_path, "r");
6631 if (patch_script_file == NULL) {
6632 error = got_error_from_errno2("fopen",
6638 error = apply_unveil(got_repo_get_path(repo), 0,
6639 got_worktree_get_root_path(worktree));
6643 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6647 cpa.patch_script_file = patch_script_file;
6648 cpa.action = "unstage";
6649 error = got_worktree_unstage(worktree, &paths, update_progress,
6650 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6652 if (patch_script_file && fclose(patch_script_file) == EOF &&
6654 error = got_error_from_errno2("fclose", patch_script_path);
6656 got_repo_close(repo);
6658 got_worktree_close(worktree);
6659 TAILQ_FOREACH(pe, &paths, entry)
6660 free((char *)pe->path);
6661 got_pathlist_free(&paths);
6669 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6670 "arg1 [arg2 ...]\n", getprogname());
6674 static const struct got_error *
6675 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6677 const struct got_error *err;
6678 struct got_blob_object *blob;
6680 err = got_object_open_as_blob(&blob, repo, id, 8192);
6684 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6685 got_object_blob_close(blob);
6689 static const struct got_error *
6690 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6692 const struct got_error *err;
6693 struct got_tree_object *tree;
6694 const struct got_tree_entries *entries;
6695 struct got_tree_entry *te;
6697 err = got_object_open_as_tree(&tree, repo, id);
6701 entries = got_object_tree_get_entries(tree);
6702 te = SIMPLEQ_FIRST(&entries->head);
6705 if (sigint_received || sigpipe_received)
6707 err = got_object_id_str(&id_str, te->id);
6710 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6712 te = SIMPLEQ_NEXT(te, entry);
6715 got_object_tree_close(tree);
6719 static const struct got_error *
6720 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6722 const struct got_error *err;
6723 struct got_commit_object *commit;
6724 const struct got_object_id_queue *parent_ids;
6725 struct got_object_qid *pid;
6726 char *id_str = NULL;
6727 const char *logmsg = NULL;
6729 err = got_object_open_as_commit(&commit, repo, id);
6733 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6737 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6738 parent_ids = got_object_commit_get_parent_ids(commit);
6739 fprintf(outfile, "numparents %d\n",
6740 got_object_commit_get_nparents(commit));
6741 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6743 err = got_object_id_str(&pid_str, pid->id);
6746 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6749 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6750 got_object_commit_get_author(commit),
6751 got_object_commit_get_author_time(commit));
6753 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6754 got_object_commit_get_author(commit),
6755 got_object_commit_get_committer_time(commit));
6757 logmsg = got_object_commit_get_logmsg_raw(commit);
6758 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6759 fprintf(outfile, "%s", logmsg);
6762 got_object_commit_close(commit);
6766 static const struct got_error *
6767 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6769 const struct got_error *err;
6770 struct got_tag_object *tag;
6771 char *id_str = NULL;
6772 const char *tagmsg = NULL;
6774 err = got_object_open_as_tag(&tag, repo, id);
6778 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6782 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6784 switch (got_object_tag_get_object_type(tag)) {
6785 case GOT_OBJ_TYPE_BLOB:
6786 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6787 GOT_OBJ_LABEL_BLOB);
6789 case GOT_OBJ_TYPE_TREE:
6790 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6791 GOT_OBJ_LABEL_TREE);
6793 case GOT_OBJ_TYPE_COMMIT:
6794 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6795 GOT_OBJ_LABEL_COMMIT);
6797 case GOT_OBJ_TYPE_TAG:
6798 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6805 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
6806 got_object_tag_get_name(tag));
6808 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
6809 got_object_tag_get_tagger(tag),
6810 got_object_tag_get_tagger_time(tag));
6812 tagmsg = got_object_tag_get_message(tag);
6813 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
6814 fprintf(outfile, "%s", tagmsg);
6817 got_object_tag_close(tag);
6821 static const struct got_error *
6822 cmd_cat(int argc, char *argv[])
6824 const struct got_error *error;
6825 struct got_repository *repo = NULL;
6826 struct got_worktree *worktree = NULL;
6827 char *cwd = NULL, *repo_path = NULL, *label = NULL;
6828 const char *commit_id_str = NULL;
6829 struct got_object_id *id = NULL, *commit_id = NULL;
6830 int ch, obj_type, i, force_path = 0;
6833 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6838 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
6841 commit_id_str = optarg;
6844 repo_path = realpath(optarg, NULL);
6845 if (repo_path == NULL)
6846 err(1, "-r option");
6847 got_path_strip_trailing_slashes(repo_path);
6861 cwd = getcwd(NULL, 0);
6863 error = got_error_from_errno("getcwd");
6866 error = got_worktree_open(&worktree, cwd);
6867 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6870 if (repo_path == NULL) {
6872 got_worktree_get_repo_path(worktree));
6873 if (repo_path == NULL) {
6874 error = got_error_from_errno("strdup");
6880 if (repo_path == NULL) {
6881 repo_path = getcwd(NULL, 0);
6882 if (repo_path == NULL)
6883 return got_error_from_errno("getcwd");
6886 error = got_repo_open(&repo, repo_path, NULL);
6891 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6895 if (commit_id_str == NULL)
6896 commit_id_str = GOT_REF_HEAD;
6897 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
6901 for (i = 0; i < argc; i++) {
6903 error = got_object_id_by_path(&id, repo, commit_id,
6908 error = match_object_id(&id, &label, argv[i],
6909 GOT_OBJ_TYPE_ANY, 0, repo);
6911 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
6912 error->code != GOT_ERR_NOT_REF)
6914 error = got_object_id_by_path(&id, repo,
6915 commit_id, argv[i]);
6921 error = got_object_get_type(&obj_type, repo, id);
6926 case GOT_OBJ_TYPE_BLOB:
6927 error = cat_blob(id, repo, stdout);
6929 case GOT_OBJ_TYPE_TREE:
6930 error = cat_tree(id, repo, stdout);
6932 case GOT_OBJ_TYPE_COMMIT:
6933 error = cat_commit(id, repo, stdout);
6935 case GOT_OBJ_TYPE_TAG:
6936 error = cat_tag(id, repo, stdout);
6939 error = got_error(GOT_ERR_OBJ_TYPE);
6955 got_worktree_close(worktree);
6957 const struct got_error *repo_error;
6958 repo_error = got_repo_close(repo);