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/limits.h>
20 #include <sys/types.h>
22 #include <sys/param.h>
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_reference.h"
40 #include "got_repository.h"
42 #include "got_worktree.h"
44 #include "got_commit_graph.h"
45 #include "got_blame.h"
46 #include "got_privsep.h"
47 #include "got_opentemp.h"
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 static volatile sig_atomic_t sigint_received;
54 static volatile sig_atomic_t sigpipe_received;
57 catch_sigint(int signo)
63 catch_sigpipe(int signo)
71 const struct got_error *(*cmd_main)(int, char *[]);
72 void (*cmd_usage)(void);
73 const char *cmd_descr;
76 __dead static void usage(void);
77 __dead static void usage_init(void);
78 __dead static void usage_checkout(void);
79 __dead static void usage_update(void);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_status(void);
85 __dead static void usage_ref(void);
86 __dead static void usage_add(void);
87 __dead static void usage_rm(void);
88 __dead static void usage_revert(void);
89 __dead static void usage_commit(void);
90 __dead static void usage_cherrypick(void);
91 __dead static void usage_backout(void);
93 static const struct got_error* cmd_init(int, char *[]);
94 static const struct got_error* cmd_checkout(int, char *[]);
95 static const struct got_error* cmd_update(int, char *[]);
96 static const struct got_error* cmd_log(int, char *[]);
97 static const struct got_error* cmd_diff(int, char *[]);
98 static const struct got_error* cmd_blame(int, char *[]);
99 static const struct got_error* cmd_tree(int, char *[]);
100 static const struct got_error* cmd_status(int, char *[]);
101 static const struct got_error* cmd_ref(int, char *[]);
102 static const struct got_error* cmd_add(int, char *[]);
103 static const struct got_error* cmd_rm(int, char *[]);
104 static const struct got_error* cmd_revert(int, char *[]);
105 static const struct got_error* cmd_commit(int, char *[]);
106 static const struct got_error* cmd_cherrypick(int, char *[]);
107 static const struct got_error* cmd_backout(int, char *[]);
109 static struct cmd got_commands[] = {
110 { "init", cmd_init, usage_init,
111 "create a new empty repository" },
112 { "checkout", cmd_checkout, usage_checkout,
113 "check out a new work tree from a repository" },
114 { "update", cmd_update, usage_update,
115 "update a work tree to a different commit" },
116 { "log", cmd_log, usage_log,
117 "show repository history" },
118 { "diff", cmd_diff, usage_diff,
119 "compare files and directories" },
120 { "blame", cmd_blame, usage_blame,
121 "show when lines in a file were changed" },
122 { "tree", cmd_tree, usage_tree,
123 "list files and directories in repository" },
124 { "status", cmd_status, usage_status,
125 "show modification status of files" },
126 { "ref", cmd_ref, usage_ref,
127 "manage references in repository" },
128 { "add", cmd_add, usage_add,
129 "add new files to version control" },
130 { "rm", cmd_rm, usage_rm,
131 "remove a versioned file" },
132 { "revert", cmd_revert, usage_revert,
133 "revert uncommitted changes" },
134 { "commit", cmd_commit, usage_commit,
135 "write changes from work tree to repository" },
136 { "cherrypick", cmd_cherrypick, usage_cherrypick,
137 "merge a single commit from another branch into a work tree" },
138 { "backout", cmd_backout, usage_backout,
139 "reverse-merge changes from a commit into a work tree" },
143 main(int argc, char *argv[])
150 setlocale(LC_CTYPE, "");
152 while ((ch = getopt(argc, argv, "h")) != -1) {
170 signal(SIGINT, catch_sigint);
171 signal(SIGPIPE, catch_sigpipe);
173 for (i = 0; i < nitems(got_commands); i++) {
174 const struct got_error *error;
176 cmd = &got_commands[i];
178 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
182 got_commands[i].cmd_usage();
184 error = got_commands[i].cmd_main(argc, argv);
185 if (error && !(sigint_received || sigpipe_received)) {
186 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
193 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
202 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
203 "Available commands:\n", getprogname());
204 for (i = 0; i < nitems(got_commands); i++) {
205 struct cmd *cmd = &got_commands[i];
206 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
211 static const struct got_error *
212 get_editor(char **abspath)
214 const struct got_error *err = NULL;
217 editor = getenv("VISUAL");
219 editor = getenv("EDITOR");
222 err = got_path_find_prog(abspath, editor);
227 if (*abspath == NULL) {
228 *abspath = strdup("/bin/ed");
229 if (*abspath == NULL)
230 return got_error_from_errno("strdup");
236 static const struct got_error *
237 apply_unveil(const char *repo_path, int repo_read_only,
238 const char *worktree_path, int create_worktree)
240 const struct got_error *err;
242 if (create_worktree) {
243 /* Pre-create work tree path to avoid unveiling its parents. */
244 err = got_path_mkdir(worktree_path);
246 if (errno == EEXIST) {
247 if (got_path_dir_is_empty(worktree_path)) {
251 err = got_error_path(worktree_path,
252 GOT_ERR_DIR_NOT_EMPTY);
256 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
260 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
261 return got_error_from_errno2("unveil", repo_path);
263 if (worktree_path && unveil(worktree_path, "rwc") != 0)
264 return got_error_from_errno2("unveil", worktree_path);
266 if (unveil("/tmp", "rwc") != 0)
267 return got_error_from_errno2("unveil", "/tmp");
269 err = got_privsep_unveil_exec_helpers();
273 if (unveil(NULL, NULL) != 0)
274 return got_error_from_errno("unveil");
282 fprintf(stderr, "usage: %s init path\n", getprogname());
286 static const struct got_error *
287 cmd_init(int argc, char *argv[])
289 const struct got_error *error = NULL;
290 char *repo_path = NULL;
293 while ((ch = getopt(argc, argv, "")) != -1) {
305 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
311 repo_path = strdup(argv[0]);
312 if (repo_path == NULL)
313 return got_error_from_errno("strdup");
315 got_path_strip_trailing_slashes(repo_path);
317 error = got_path_mkdir(repo_path);
319 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
322 error = apply_unveil(repo_path, 0, NULL, 0);
326 error = got_repo_init(repo_path);
338 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
339 "[-p prefix] repository-path [worktree-path]\n", getprogname());
344 checkout_progress(void *arg, unsigned char status, const char *path)
346 char *worktree_path = arg;
348 /* Base commit bump happens silently. */
349 if (status == GOT_STATUS_BUMP_BASE)
352 while (path[0] == '/')
355 printf("%c %s/%s\n", status, worktree_path, path);
358 static const struct got_error *
359 check_cancelled(void *arg)
361 if (sigint_received || sigpipe_received)
362 return got_error(GOT_ERR_CANCELLED);
366 static const struct got_error *
367 check_linear_ancestry(struct got_object_id *commit_id,
368 struct got_object_id *base_commit_id, struct got_repository *repo)
370 const struct got_error *err = NULL;
371 struct got_object_id *yca_id;
373 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
374 commit_id, base_commit_id, repo);
379 return got_error(GOT_ERR_ANCESTRY);
382 * Require a straight line of history between the target commit
383 * and the work tree's base commit.
385 * Non-linear situations such as this require a rebase:
387 * (commit) D F (base_commit)
395 * 'got update' only handles linear cases:
396 * Update forwards in time: A (base/yca) - B - C - D (commit)
397 * Update backwards in time: D (base) - C - B - A (commit/yca)
399 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
400 got_object_id_cmp(base_commit_id, yca_id) != 0)
401 return got_error(GOT_ERR_ANCESTRY);
407 static const struct got_error *
408 check_same_branch(struct got_object_id *commit_id,
409 struct got_reference *head_ref, struct got_repository *repo)
411 const struct got_error *err = NULL;
412 struct got_commit_graph *graph = NULL;
413 struct got_object_id *head_commit_id = NULL;
414 int is_same_branch = 0;
416 err = got_ref_resolve(&head_commit_id, repo, head_ref);
420 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
424 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
429 struct got_object_id *id;
430 err = got_commit_graph_iter_next(&id, graph);
432 if (err->code == GOT_ERR_ITER_COMPLETED) {
436 else if (err->code != GOT_ERR_ITER_NEED_MORE)
438 err = got_commit_graph_fetch_commits(graph, 1,
445 if (got_object_id_cmp(id, commit_id) == 0) {
453 got_commit_graph_close(graph);
454 free(head_commit_id);
455 if (!err && !is_same_branch)
456 err = got_error(GOT_ERR_ANCESTRY);
460 static const struct got_error *
461 cmd_checkout(int argc, char *argv[])
463 const struct got_error *error = NULL;
464 struct got_repository *repo = NULL;
465 struct got_reference *head_ref = NULL;
466 struct got_worktree *worktree = NULL;
467 char *repo_path = NULL;
468 char *worktree_path = NULL;
469 const char *path_prefix = "";
470 const char *branch_name = GOT_REF_HEAD;
471 char *commit_id_str = NULL;
472 int ch, same_path_prefix;
474 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
477 branch_name = optarg;
480 commit_id_str = strdup(optarg);
481 if (commit_id_str == NULL)
482 return got_error_from_errno("strdup");
485 path_prefix = optarg;
497 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
498 "unveil", NULL) == -1)
502 char *cwd, *base, *dotgit;
503 repo_path = realpath(argv[0], NULL);
504 if (repo_path == NULL)
505 return got_error_from_errno2("realpath", argv[0]);
506 cwd = getcwd(NULL, 0);
508 error = got_error_from_errno("getcwd");
511 if (path_prefix[0]) {
512 base = basename(path_prefix);
514 error = got_error_from_errno2("basename",
519 base = basename(repo_path);
521 error = got_error_from_errno2("basename",
526 dotgit = strstr(base, ".git");
529 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
530 error = got_error_from_errno("asprintf");
535 } else if (argc == 2) {
536 repo_path = realpath(argv[0], NULL);
537 if (repo_path == NULL) {
538 error = got_error_from_errno2("realpath", argv[0]);
541 worktree_path = realpath(argv[1], NULL);
542 if (worktree_path == NULL) {
543 error = got_error_from_errno2("realpath", argv[1]);
549 got_path_strip_trailing_slashes(repo_path);
550 got_path_strip_trailing_slashes(worktree_path);
552 error = got_repo_open(&repo, repo_path);
556 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
560 error = got_ref_open(&head_ref, repo, branch_name, 0);
564 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
565 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
568 error = got_worktree_open(&worktree, worktree_path);
572 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
576 if (!same_path_prefix) {
577 error = got_error(GOT_ERR_PATH_PREFIX);
582 struct got_object_id *commit_id;
583 error = got_object_resolve_id_str(&commit_id, repo,
587 error = check_linear_ancestry(commit_id,
588 got_worktree_get_base_commit_id(worktree), repo);
593 error = check_same_branch(commit_id, head_ref, repo);
596 error = got_worktree_set_base_commit_id(worktree, repo,
603 error = got_worktree_checkout_files(worktree, "", repo,
604 checkout_progress, worktree_path, check_cancelled, NULL);
608 printf("Now shut up and hack\n");
620 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
626 update_progress(void *arg, unsigned char status, const char *path)
628 int *did_something = arg;
630 if (status == GOT_STATUS_EXISTS)
635 /* Base commit bump happens silently. */
636 if (status == GOT_STATUS_BUMP_BASE)
639 while (path[0] == '/')
641 printf("%c %s\n", status, path);
644 static const struct got_error *
645 switch_head_ref(struct got_reference *head_ref,
646 struct got_object_id *commit_id, struct got_worktree *worktree,
647 struct got_repository *repo)
649 const struct got_error *err = NULL;
651 int ref_has_moved = 0;
653 /* Trivial case: switching between two different references. */
654 if (strcmp(got_ref_get_name(head_ref),
655 got_worktree_get_head_ref_name(worktree)) != 0) {
656 printf("Switching work tree from %s to %s\n",
657 got_worktree_get_head_ref_name(worktree),
658 got_ref_get_name(head_ref));
659 return got_worktree_set_head_ref(worktree, head_ref);
662 err = check_linear_ancestry(commit_id,
663 got_worktree_get_base_commit_id(worktree), repo);
665 if (err->code != GOT_ERR_ANCESTRY)
672 /* Switching to a rebased branch with the same reference name. */
673 err = got_object_id_str(&base_id_str,
674 got_worktree_get_base_commit_id(worktree));
677 printf("Reference %s now points at a different branch\n",
678 got_worktree_get_head_ref_name(worktree));
679 printf("Switching work tree from %s to %s\n", base_id_str,
680 got_worktree_get_head_ref_name(worktree));
684 static const struct got_error *
685 cmd_update(int argc, char *argv[])
687 const struct got_error *error = NULL;
688 struct got_repository *repo = NULL;
689 struct got_worktree *worktree = NULL;
690 char *worktree_path = NULL, *path = NULL;
691 struct got_object_id *commit_id = NULL;
692 char *commit_id_str = NULL;
693 const char *branch_name = NULL;
694 struct got_reference *head_ref = NULL;
695 int ch, did_something = 0;
697 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
700 branch_name = optarg;
703 commit_id_str = strdup(optarg);
704 if (commit_id_str == NULL)
705 return got_error_from_errno("strdup");
717 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
718 "unveil", NULL) == -1)
721 worktree_path = getcwd(NULL, 0);
722 if (worktree_path == NULL) {
723 error = got_error_from_errno("getcwd");
726 error = got_worktree_open(&worktree, worktree_path);
733 error = got_error_from_errno("strdup");
736 } else if (argc == 1) {
737 error = got_worktree_resolve_path(&path, worktree, argv[0]);
743 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
747 error = apply_unveil(got_repo_get_path(repo), 0,
748 got_worktree_get_root_path(worktree), 0);
752 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
753 got_worktree_get_head_ref_name(worktree), 0);
756 if (commit_id_str == NULL) {
757 error = got_ref_resolve(&commit_id, repo, head_ref);
760 error = got_object_id_str(&commit_id_str, commit_id);
764 error = got_object_resolve_id_str(&commit_id, repo,
771 struct got_object_id *head_commit_id;
772 if (strlen(path) != 0) {
773 fprintf(stderr, "%s: switching between branches "
774 "requires that the entire work tree "
775 "gets updated, not just '%s'\n",
776 getprogname(), path);
777 error = got_error(GOT_ERR_BAD_PATH);
780 error = got_ref_resolve(&head_commit_id, repo, head_ref);
783 error = check_linear_ancestry(commit_id, head_commit_id, repo);
784 free(head_commit_id);
787 error = check_same_branch(commit_id, head_ref, repo);
790 error = switch_head_ref(head_ref, commit_id, worktree, repo);
794 error = check_linear_ancestry(commit_id,
795 got_worktree_get_base_commit_id(worktree), repo);
797 if (error->code == GOT_ERR_ANCESTRY)
798 error = got_error(GOT_ERR_BRANCH_MOVED);
801 error = check_same_branch(commit_id, head_ref, repo);
806 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
808 error = got_worktree_set_base_commit_id(worktree, repo,
814 error = got_worktree_checkout_files(worktree, path, repo,
815 update_progress, &did_something, check_cancelled, NULL);
820 printf("Updated to commit %s\n", commit_id_str);
822 printf("Already up-to-date\n");
831 static const struct got_error *
832 print_patch(struct got_commit_object *commit, struct got_object_id *id,
833 int diff_context, struct got_repository *repo)
835 const struct got_error *err = NULL;
836 struct got_tree_object *tree1 = NULL, *tree2;
837 struct got_object_qid *qid;
838 char *id_str1 = NULL, *id_str2;
839 struct got_diff_blob_output_unidiff_arg arg;
841 err = got_object_open_as_tree(&tree2, repo,
842 got_object_commit_get_tree_id(commit));
846 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
848 struct got_commit_object *pcommit;
850 err = got_object_open_as_commit(&pcommit, repo, qid->id);
854 err = got_object_open_as_tree(&tree1, repo,
855 got_object_commit_get_tree_id(pcommit));
856 got_object_commit_close(pcommit);
860 err = got_object_id_str(&id_str1, qid->id);
865 err = got_object_id_str(&id_str2, id);
869 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
870 arg.diff_context = diff_context;
871 arg.outfile = stdout;
872 err = got_diff_tree(tree1, tree2, "", "", repo,
873 got_diff_blob_output_unidiff, &arg);
876 got_object_tree_close(tree1);
877 got_object_tree_close(tree2);
884 get_datestr(time_t *time, char *datebuf)
886 char *p, *s = ctime_r(time, datebuf);
893 static const struct got_error *
894 print_commit(struct got_commit_object *commit, struct got_object_id *id,
895 struct got_repository *repo, int show_patch, int diff_context,
896 struct got_reflist_head *refs)
898 const struct got_error *err = NULL;
899 char *id_str, *datestr, *logmsg0, *logmsg, *line;
901 time_t committer_time;
902 const char *author, *committer;
903 char *refs_str = NULL;
904 struct got_reflist_entry *re;
906 SIMPLEQ_FOREACH(re, refs, entry) {
909 if (got_object_id_cmp(re->id, id) != 0)
911 name = got_ref_get_name(re->ref);
912 if (strcmp(name, GOT_REF_HEAD) == 0)
914 if (strncmp(name, "refs/", 5) == 0)
916 if (strncmp(name, "got/", 4) == 0)
918 if (strncmp(name, "heads/", 6) == 0)
920 if (strncmp(name, "remotes/", 8) == 0)
923 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
925 err = got_error_from_errno("asprintf");
931 err = got_object_id_str(&id_str, id);
935 printf("-----------------------------------------------\n");
936 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
937 refs_str ? refs_str : "", refs_str ? ")" : "");
942 printf("from: %s\n", got_object_commit_get_author(commit));
943 committer_time = got_object_commit_get_committer_time(commit);
944 datestr = get_datestr(&committer_time, datebuf);
945 printf("date: %s UTC\n", datestr);
946 author = got_object_commit_get_author(commit);
947 committer = got_object_commit_get_committer(commit);
948 if (strcmp(author, committer) != 0)
949 printf("via: %s\n", committer);
950 if (got_object_commit_get_nparents(commit) > 1) {
951 const struct got_object_id_queue *parent_ids;
952 struct got_object_qid *qid;
954 parent_ids = got_object_commit_get_parent_ids(commit);
955 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
956 err = got_object_id_str(&id_str, qid->id);
959 printf("parent %d: %s\n", n++, id_str);
964 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
966 return got_error_from_errno("strdup");
970 line = strsep(&logmsg, "\n");
972 printf(" %s\n", line);
977 err = print_patch(commit, id, diff_context, repo);
982 if (fflush(stdout) != 0 && err == NULL)
983 err = got_error_from_errno("fflush");
987 static const struct got_error *
988 print_commits(struct got_object_id *root_id, struct got_repository *repo,
989 char *path, int show_patch, int diff_context, int limit,
990 int first_parent_traversal, struct got_reflist_head *refs)
992 const struct got_error *err;
993 struct got_commit_graph *graph;
995 err = got_commit_graph_open(&graph, root_id, path,
996 first_parent_traversal, repo);
999 err = got_commit_graph_iter_start(graph, root_id, repo);
1003 struct got_commit_object *commit;
1004 struct got_object_id *id;
1006 if (sigint_received || sigpipe_received)
1009 err = got_commit_graph_iter_next(&id, graph);
1011 if (err->code == GOT_ERR_ITER_COMPLETED) {
1015 if (err->code != GOT_ERR_ITER_NEED_MORE)
1017 err = got_commit_graph_fetch_commits(graph, 1, repo);
1026 err = got_object_open_as_commit(&commit, repo, id);
1029 err = print_commit(commit, id, repo, show_patch, diff_context,
1031 got_object_commit_close(commit);
1032 if (err || (limit && --limit == 0))
1036 got_commit_graph_close(graph);
1043 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1044 "[-r repository-path] [path]\n", getprogname());
1048 static const struct got_error *
1049 cmd_log(int argc, char *argv[])
1051 const struct got_error *error;
1052 struct got_repository *repo = NULL;
1053 struct got_worktree *worktree = NULL;
1054 struct got_commit_object *commit = NULL;
1055 struct got_object_id *id = NULL;
1056 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1057 char *start_commit = NULL;
1058 int diff_context = 3, ch;
1059 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1061 struct got_reflist_head refs;
1063 SIMPLEQ_INIT(&refs);
1066 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1072 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1075 first_parent_traversal = 1;
1081 start_commit = optarg;
1084 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1087 err(1, "-C option %s", errstr);
1090 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1092 err(1, "-l option %s", errstr);
1095 repo_path = realpath(optarg, NULL);
1096 if (repo_path == NULL)
1097 err(1, "-r option");
1098 got_path_strip_trailing_slashes(repo_path);
1109 cwd = getcwd(NULL, 0);
1111 error = got_error_from_errno("getcwd");
1115 error = got_worktree_open(&worktree, cwd);
1116 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1123 error = got_error_from_errno("strdup");
1126 } else if (argc == 1) {
1128 error = got_worktree_resolve_path(&path, worktree,
1133 path = strdup(argv[0]);
1135 error = got_error_from_errno("strdup");
1142 if (repo_path == NULL) {
1143 repo_path = worktree ?
1144 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1146 if (repo_path == NULL) {
1147 error = got_error_from_errno("strdup");
1151 error = got_repo_open(&repo, repo_path);
1155 error = apply_unveil(got_repo_get_path(repo), 1,
1156 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1160 if (start_commit == NULL) {
1161 struct got_reference *head_ref;
1162 error = got_ref_open(&head_ref, repo,
1163 worktree ? got_worktree_get_head_ref_name(worktree)
1167 error = got_ref_resolve(&id, repo, head_ref);
1168 got_ref_close(head_ref);
1171 error = got_object_open_as_commit(&commit, repo, id);
1173 struct got_reference *ref;
1174 error = got_ref_open(&ref, repo, start_commit, 0);
1175 if (error == NULL) {
1177 error = got_ref_resolve(&id, repo, ref);
1181 error = got_object_get_type(&obj_type, repo, id);
1184 if (obj_type == GOT_OBJ_TYPE_TAG) {
1185 struct got_tag_object *tag;
1186 error = got_object_open_as_tag(&tag, repo, id);
1189 if (got_object_tag_get_object_type(tag) !=
1190 GOT_OBJ_TYPE_COMMIT) {
1191 got_object_tag_close(tag);
1192 error = got_error(GOT_ERR_OBJ_TYPE);
1196 id = got_object_id_dup(
1197 got_object_tag_get_object_id(tag));
1199 error = got_error_from_errno(
1200 "got_object_id_dup");
1201 got_object_tag_close(tag);
1204 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1205 error = got_error(GOT_ERR_OBJ_TYPE);
1208 error = got_object_open_as_commit(&commit, repo, id);
1212 if (commit == NULL) {
1213 error = got_object_resolve_id_str(&id, repo,
1222 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1227 path = in_repo_path;
1230 error = got_ref_list(&refs, repo);
1234 error = print_commits(id, repo, path, show_patch,
1235 diff_context, limit, first_parent_traversal, &refs);
1242 got_worktree_close(worktree);
1244 const struct got_error *repo_error;
1245 repo_error = got_repo_close(repo);
1249 got_ref_list_free(&refs);
1256 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1257 "[object1 object2 | path]\n", getprogname());
1261 struct print_diff_arg {
1262 struct got_repository *repo;
1263 struct got_worktree *worktree;
1269 static const struct got_error *
1270 print_diff(void *arg, unsigned char status, const char *path,
1271 struct got_object_id *blob_id, struct got_object_id *commit_id)
1273 struct print_diff_arg *a = arg;
1274 const struct got_error *err = NULL;
1275 struct got_blob_object *blob1 = NULL;
1277 char *abspath = NULL;
1280 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1281 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1284 if (!a->header_shown) {
1285 printf("diff %s %s\n", a->id_str,
1286 got_worktree_get_root_path(a->worktree));
1287 a->header_shown = 1;
1290 if (status != GOT_STATUS_ADD) {
1291 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1297 if (status != GOT_STATUS_DELETE) {
1298 if (asprintf(&abspath, "%s/%s",
1299 got_worktree_get_root_path(a->worktree), path) == -1) {
1300 err = got_error_from_errno("asprintf");
1304 f2 = fopen(abspath, "r");
1306 err = got_error_from_errno2("fopen", abspath);
1309 if (lstat(abspath, &sb) == -1) {
1310 err = got_error_from_errno2("lstat", abspath);
1316 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1320 got_object_blob_close(blob1);
1321 if (f2 && fclose(f2) != 0 && err == NULL)
1322 err = got_error_from_errno("fclose");
1327 static const struct got_error *
1328 cmd_diff(int argc, char *argv[])
1330 const struct got_error *error;
1331 struct got_repository *repo = NULL;
1332 struct got_worktree *worktree = NULL;
1333 char *cwd = NULL, *repo_path = NULL;
1334 struct got_object_id *id1 = NULL, *id2 = NULL;
1335 const char *id_str1 = NULL, *id_str2 = NULL;
1336 char *label1 = NULL, *label2 = NULL;
1338 int diff_context = 3, ch;
1343 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1348 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1351 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1353 err(1, "-C option %s", errstr);
1356 repo_path = realpath(optarg, NULL);
1357 if (repo_path == NULL)
1358 err(1, "-r option");
1359 got_path_strip_trailing_slashes(repo_path);
1370 cwd = getcwd(NULL, 0);
1372 error = got_error_from_errno("getcwd");
1375 error = got_worktree_open(&worktree, cwd);
1376 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1379 if (worktree == NULL) {
1380 error = got_error(GOT_ERR_NOT_WORKTREE);
1385 "-r option can't be used when diffing a work tree");
1386 repo_path = strdup(got_worktree_get_repo_path(worktree));
1387 if (repo_path == NULL) {
1388 error = got_error_from_errno("strdup");
1392 error = got_worktree_resolve_path(&path, worktree,
1399 error = got_error_from_errno("strdup");
1403 } else if (argc == 2) {
1406 if (worktree && repo_path == NULL) {
1408 strdup(got_worktree_get_repo_path(worktree));
1409 if (repo_path == NULL) {
1410 error = got_error_from_errno("strdup");
1417 if (repo_path == NULL) {
1418 repo_path = getcwd(NULL, 0);
1419 if (repo_path == NULL)
1420 return got_error_from_errno("getcwd");
1423 error = got_repo_open(&repo, repo_path);
1428 error = apply_unveil(got_repo_get_path(repo), 1,
1429 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1434 struct print_diff_arg arg;
1436 error = got_object_id_str(&id_str,
1437 got_worktree_get_base_commit_id(worktree));
1441 arg.worktree = worktree;
1442 arg.diff_context = diff_context;
1443 arg.id_str = id_str;
1444 arg.header_shown = 0;
1446 error = got_worktree_status(worktree, path, repo, print_diff,
1447 &arg, check_cancelled, NULL);
1452 error = got_object_resolve_id_str(&id1, repo, id_str1);
1454 struct got_reference *ref;
1455 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1457 error = got_ref_open(&ref, repo, id_str1, 0);
1460 label1 = strdup(got_ref_get_name(ref));
1461 if (label1 == NULL) {
1462 error = got_error_from_errno("strdup");
1465 error = got_ref_resolve(&id1, repo, ref);
1470 label1 = strdup(id_str1);
1471 if (label1 == NULL) {
1472 error = got_error_from_errno("strdup");
1477 error = got_object_resolve_id_str(&id2, repo, id_str2);
1479 struct got_reference *ref;
1480 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1482 error = got_ref_open(&ref, repo, id_str2, 0);
1485 label2 = strdup(got_ref_get_name(ref));
1486 if (label2 == NULL) {
1487 error = got_error_from_errno("strdup");
1490 error = got_ref_resolve(&id2, repo, ref);
1495 label2 = strdup(id_str2);
1496 if (label2 == NULL) {
1497 error = got_error_from_errno("strdup");
1502 error = got_object_get_type(&type1, repo, id1);
1506 error = got_object_get_type(&type2, repo, id2);
1510 if (type1 != type2) {
1511 error = got_error(GOT_ERR_OBJ_TYPE);
1516 case GOT_OBJ_TYPE_BLOB:
1517 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1518 diff_context, repo, stdout);
1520 case GOT_OBJ_TYPE_TREE:
1521 error = got_diff_objects_as_trees(id1, id2, "", "",
1522 diff_context, repo, stdout);
1524 case GOT_OBJ_TYPE_COMMIT:
1525 printf("diff %s %s\n", label1, label2);
1526 error = got_diff_objects_as_commits(id1, id2, diff_context,
1530 error = got_error(GOT_ERR_OBJ_TYPE);
1540 got_worktree_close(worktree);
1542 const struct got_error *repo_error;
1543 repo_error = got_repo_close(repo);
1554 "usage: %s blame [-c commit] [-r repository-path] path\n",
1559 static const struct got_error *
1560 cmd_blame(int argc, char *argv[])
1562 const struct got_error *error;
1563 struct got_repository *repo = NULL;
1564 struct got_worktree *worktree = NULL;
1565 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1566 struct got_object_id *commit_id = NULL;
1567 char *commit_id_str = NULL;
1571 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1576 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1579 commit_id_str = optarg;
1582 repo_path = realpath(optarg, NULL);
1583 if (repo_path == NULL)
1584 err(1, "-r option");
1585 got_path_strip_trailing_slashes(repo_path);
1601 cwd = getcwd(NULL, 0);
1603 error = got_error_from_errno("getcwd");
1606 if (repo_path == NULL) {
1607 error = got_worktree_open(&worktree, cwd);
1608 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1614 strdup(got_worktree_get_repo_path(worktree));
1615 if (repo_path == NULL)
1616 error = got_error_from_errno("strdup");
1620 repo_path = strdup(cwd);
1621 if (repo_path == NULL) {
1622 error = got_error_from_errno("strdup");
1628 error = got_repo_open(&repo, repo_path);
1632 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1637 const char *prefix = got_worktree_get_path_prefix(worktree);
1638 char *p, *worktree_subdir = cwd +
1639 strlen(got_worktree_get_root_path(worktree));
1640 if (asprintf(&p, "%s%s%s%s%s",
1641 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1642 worktree_subdir, worktree_subdir[0] ? "/" : "",
1644 error = got_error_from_errno("asprintf");
1647 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1650 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1655 if (commit_id_str == NULL) {
1656 struct got_reference *head_ref;
1657 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1660 error = got_ref_resolve(&commit_id, repo, head_ref);
1661 got_ref_close(head_ref);
1665 error = got_object_resolve_id_str(&commit_id, repo,
1671 error = got_blame(in_repo_path, commit_id, repo, stdout);
1678 got_worktree_close(worktree);
1680 const struct got_error *repo_error;
1681 repo_error = got_repo_close(repo);
1692 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1698 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1699 const char *root_path)
1701 int is_root_path = (strcmp(path, root_path) == 0);
1703 path += strlen(root_path);
1704 while (path[0] == '/')
1707 printf("%s%s%s%s%s\n", id ? id : "", path,
1708 is_root_path ? "" : "/", te->name,
1709 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1712 static const struct got_error *
1713 print_tree(const char *path, struct got_object_id *commit_id,
1714 int show_ids, int recurse, const char *root_path,
1715 struct got_repository *repo)
1717 const struct got_error *err = NULL;
1718 struct got_object_id *tree_id = NULL;
1719 struct got_tree_object *tree = NULL;
1720 const struct got_tree_entries *entries;
1721 struct got_tree_entry *te;
1723 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1727 err = got_object_open_as_tree(&tree, repo, tree_id);
1730 entries = got_object_tree_get_entries(tree);
1731 te = SIMPLEQ_FIRST(&entries->head);
1735 if (sigint_received || sigpipe_received)
1740 err = got_object_id_str(&id_str, te->id);
1743 if (asprintf(&id, "%s ", id_str) == -1) {
1744 err = got_error_from_errno("asprintf");
1750 print_entry(te, id, path, root_path);
1753 if (recurse && S_ISDIR(te->mode)) {
1755 if (asprintf(&child_path, "%s%s%s", path,
1756 path[0] == '/' && path[1] == '\0' ? "" : "/",
1758 err = got_error_from_errno("asprintf");
1761 err = print_tree(child_path, commit_id, show_ids, 1,
1768 te = SIMPLEQ_NEXT(te, entry);
1772 got_object_tree_close(tree);
1777 static const struct got_error *
1778 cmd_tree(int argc, char *argv[])
1780 const struct got_error *error;
1781 struct got_repository *repo = NULL;
1782 struct got_worktree *worktree = NULL;
1784 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1785 struct got_object_id *commit_id = NULL;
1786 char *commit_id_str = NULL;
1787 int show_ids = 0, recurse = 0;
1791 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1796 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1799 commit_id_str = optarg;
1802 repo_path = realpath(optarg, NULL);
1803 if (repo_path == NULL)
1804 err(1, "-r option");
1805 got_path_strip_trailing_slashes(repo_path);
1829 cwd = getcwd(NULL, 0);
1831 error = got_error_from_errno("getcwd");
1834 if (repo_path == NULL) {
1835 error = got_worktree_open(&worktree, cwd);
1836 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1842 strdup(got_worktree_get_repo_path(worktree));
1843 if (repo_path == NULL)
1844 error = got_error_from_errno("strdup");
1848 repo_path = strdup(cwd);
1849 if (repo_path == NULL) {
1850 error = got_error_from_errno("strdup");
1856 error = got_repo_open(&repo, repo_path);
1860 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1866 char *p, *worktree_subdir = cwd +
1867 strlen(got_worktree_get_root_path(worktree));
1868 if (asprintf(&p, "%s/%s",
1869 got_worktree_get_path_prefix(worktree),
1870 worktree_subdir) == -1) {
1871 error = got_error_from_errno("asprintf");
1874 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1881 if (in_repo_path == NULL) {
1882 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1887 if (commit_id_str == NULL) {
1888 struct got_reference *head_ref;
1889 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1892 error = got_ref_resolve(&commit_id, repo, head_ref);
1893 got_ref_close(head_ref);
1897 error = got_object_resolve_id_str(&commit_id, repo,
1903 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1904 in_repo_path, repo);
1911 got_worktree_close(worktree);
1913 const struct got_error *repo_error;
1914 repo_error = got_repo_close(repo);
1924 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1928 static const struct got_error *
1929 print_status(void *arg, unsigned char status, const char *path,
1930 struct got_object_id *blob_id, struct got_object_id *commit_id)
1932 printf("%c %s\n", status, path);
1936 static const struct got_error *
1937 cmd_status(int argc, char *argv[])
1939 const struct got_error *error = NULL;
1940 struct got_repository *repo = NULL;
1941 struct got_worktree *worktree = NULL;
1942 char *cwd = NULL, *path = NULL;
1945 while ((ch = getopt(argc, argv, "")) != -1) {
1957 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1961 cwd = getcwd(NULL, 0);
1963 error = got_error_from_errno("getcwd");
1967 error = got_worktree_open(&worktree, cwd);
1974 error = got_error_from_errno("strdup");
1977 } else if (argc == 1) {
1978 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1984 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1988 error = apply_unveil(got_repo_get_path(repo), 1,
1989 got_worktree_get_root_path(worktree), 0);
1993 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1994 check_cancelled, NULL);
2005 "usage: %s ref [-r repository] -l | -d name | name target\n",
2010 static const struct got_error *
2011 list_refs(struct got_repository *repo)
2013 static const struct got_error *err = NULL;
2014 struct got_reflist_head refs;
2015 struct got_reflist_entry *re;
2017 SIMPLEQ_INIT(&refs);
2018 err = got_ref_list(&refs, repo);
2022 SIMPLEQ_FOREACH(re, &refs, entry) {
2024 refstr = got_ref_to_str(re->ref);
2026 return got_error_from_errno("got_ref_to_str");
2027 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2031 got_ref_list_free(&refs);
2035 static const struct got_error *
2036 delete_ref(struct got_repository *repo, const char *refname)
2038 const struct got_error *err = NULL;
2039 struct got_reference *ref;
2041 err = got_ref_open(&ref, repo, refname, 0);
2045 err = got_ref_delete(ref, repo);
2050 static const struct got_error *
2051 add_ref(struct got_repository *repo, const char *refname, const char *target)
2053 const struct got_error *err = NULL;
2054 struct got_object_id *id;
2055 struct got_reference *ref = NULL;
2057 err = got_object_resolve_id_str(&id, repo, target);
2059 struct got_reference *target_ref;
2061 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2063 err = got_ref_open(&target_ref, repo, target, 0);
2066 err = got_ref_resolve(&id, repo, target_ref);
2067 got_ref_close(target_ref);
2072 err = got_ref_alloc(&ref, refname, id);
2076 err = got_ref_write(ref, repo);
2084 static const struct got_error *
2085 cmd_ref(int argc, char *argv[])
2087 const struct got_error *error = NULL;
2088 struct got_repository *repo = NULL;
2089 struct got_worktree *worktree = NULL;
2090 char *cwd = NULL, *repo_path = NULL;
2091 int ch, do_list = 0;
2092 const char *delref = NULL;
2094 /* TODO: Add -s option for adding symbolic references. */
2095 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2101 repo_path = realpath(optarg, NULL);
2102 if (repo_path == NULL)
2103 err(1, "-r option");
2104 got_path_strip_trailing_slashes(repo_path);
2115 if (do_list && delref)
2116 errx(1, "-l and -d options are mutually exclusive\n");
2121 if (do_list || delref) {
2124 } else if (argc != 2)
2129 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2133 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2134 "sendfd unveil", NULL) == -1)
2138 cwd = getcwd(NULL, 0);
2140 error = got_error_from_errno("getcwd");
2144 if (repo_path == NULL) {
2145 error = got_worktree_open(&worktree, cwd);
2146 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2152 strdup(got_worktree_get_repo_path(worktree));
2153 if (repo_path == NULL)
2154 error = got_error_from_errno("strdup");
2158 repo_path = strdup(cwd);
2159 if (repo_path == NULL) {
2160 error = got_error_from_errno("strdup");
2166 error = got_repo_open(&repo, repo_path);
2170 error = apply_unveil(got_repo_get_path(repo), do_list,
2171 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2176 error = list_refs(repo);
2178 error = delete_ref(repo, delref);
2180 error = add_ref(repo, argv[0], argv[1]);
2183 got_repo_close(repo);
2185 got_worktree_close(worktree);
2194 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2198 static const struct got_error *
2199 cmd_add(int argc, char *argv[])
2201 const struct got_error *error = NULL;
2202 struct got_repository *repo = NULL;
2203 struct got_worktree *worktree = NULL;
2205 struct got_pathlist_head paths;
2206 struct got_pathlist_entry *pe;
2211 while ((ch = getopt(argc, argv, "")) != -1) {
2225 /* make sure each file exists before doing anything halfway */
2226 for (x = 0; x < argc; x++) {
2227 char *path = realpath(argv[x], NULL);
2229 error = got_error_from_errno2("realpath", argv[x]);
2235 cwd = getcwd(NULL, 0);
2237 error = got_error_from_errno("getcwd");
2241 error = got_worktree_open(&worktree, cwd);
2245 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2249 error = apply_unveil(got_repo_get_path(repo), 1,
2250 got_worktree_get_root_path(worktree), 0);
2254 for (x = 0; x < argc; x++) {
2255 char *path = realpath(argv[x], NULL);
2257 error = got_error_from_errno2("realpath", argv[x]);
2261 got_path_strip_trailing_slashes(path);
2262 error = got_pathlist_insert(&pe, &paths, path, NULL);
2268 error = got_worktree_schedule_add(worktree, &paths, print_status,
2272 got_repo_close(repo);
2274 got_worktree_close(worktree);
2275 TAILQ_FOREACH(pe, &paths, entry)
2276 free((char *)pe->path);
2277 got_pathlist_free(&paths);
2285 fprintf(stderr, "usage: %s rm [-f] file-path ...\n", getprogname());
2289 static const struct got_error *
2290 cmd_rm(int argc, char *argv[])
2292 const struct got_error *error = NULL;
2293 struct got_worktree *worktree = NULL;
2294 struct got_repository *repo = NULL;
2296 struct got_pathlist_head paths;
2297 struct got_pathlist_entry *pe;
2298 int ch, i, delete_local_mods = 0;
2302 while ((ch = getopt(argc, argv, "f")) != -1) {
2305 delete_local_mods = 1;
2319 /* make sure each file exists before doing anything halfway */
2320 for (i = 0; i < argc; i++) {
2321 char *path = realpath(argv[i], NULL);
2323 error = got_error_from_errno2("realpath", argv[i]);
2329 cwd = getcwd(NULL, 0);
2331 error = got_error_from_errno("getcwd");
2334 error = got_worktree_open(&worktree, cwd);
2338 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2342 error = apply_unveil(got_repo_get_path(repo), 1,
2343 got_worktree_get_root_path(worktree), 0);
2347 for (i = 0; i < argc; i++) {
2348 char *path = realpath(argv[i], NULL);
2350 error = got_error_from_errno2("realpath", argv[i]);
2354 got_path_strip_trailing_slashes(path);
2355 error = got_pathlist_insert(&pe, &paths, path, NULL);
2361 error = got_worktree_schedule_delete(worktree, &paths,
2362 delete_local_mods, print_status, NULL, repo);
2367 got_repo_close(repo);
2369 got_worktree_close(worktree);
2370 TAILQ_FOREACH(pe, &paths, entry)
2371 free((char *)pe->path);
2372 got_pathlist_free(&paths);
2380 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2385 revert_progress(void *arg, unsigned char status, const char *path)
2387 while (path[0] == '/')
2389 printf("%c %s\n", status, path);
2392 static const struct got_error *
2393 cmd_revert(int argc, char *argv[])
2395 const struct got_error *error = NULL;
2396 struct got_worktree *worktree = NULL;
2397 struct got_repository *repo = NULL;
2398 char *cwd = NULL, *path = NULL;
2399 struct got_pathlist_head paths;
2400 struct got_pathlist_entry *pe;
2405 while ((ch = getopt(argc, argv, "")) != -1) {
2419 for (i = 0; i < argc; i++) {
2420 char *path = realpath(argv[i], NULL);
2422 error = got_error_from_errno2("realpath", argv[i]);
2426 got_path_strip_trailing_slashes(path);
2427 error = got_pathlist_insert(&pe, &paths, path, NULL);
2434 cwd = getcwd(NULL, 0);
2436 error = got_error_from_errno("getcwd");
2439 error = got_worktree_open(&worktree, cwd);
2443 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2447 error = apply_unveil(got_repo_get_path(repo), 1,
2448 got_worktree_get_root_path(worktree), 0);
2452 error = got_worktree_revert(worktree, &paths,
2453 revert_progress, NULL, repo);
2458 got_repo_close(repo);
2460 got_worktree_close(worktree);
2469 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2474 spawn_editor(const char *editor, const char *file)
2477 sig_t sighup, sigint, sigquit;
2480 sighup = signal(SIGHUP, SIG_IGN);
2481 sigint = signal(SIGINT, SIG_IGN);
2482 sigquit = signal(SIGQUIT, SIG_IGN);
2484 switch (pid = fork()) {
2488 execl(editor, editor, file, (char *)NULL);
2492 while (waitpid(pid, &st, 0) == -1)
2497 (void)signal(SIGHUP, sighup);
2498 (void)signal(SIGINT, sigint);
2499 (void)signal(SIGQUIT, sigquit);
2501 if (!WIFEXITED(st)) {
2506 return WEXITSTATUS(st);
2509 struct collect_commit_logmsg_arg {
2510 const char *cmdline_log;
2512 const char *worktree_path;
2513 const char *branch_name;
2514 const char *repo_path;
2519 static const struct got_error *
2520 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2523 char *initial_content = NULL;
2524 struct got_pathlist_entry *pe;
2525 const struct got_error *err = NULL;
2526 char *template = NULL;
2527 struct collect_commit_logmsg_arg *a = arg;
2529 struct stat st, st2;
2532 int fd, content_changed = 0;
2534 /* if a message was specified on the command line, just use it */
2535 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2536 len = strlen(a->cmdline_log) + 1;
2537 *logmsg = malloc(len + 1);
2538 if (*logmsg == NULL)
2539 return got_error_from_errno("malloc");
2540 strlcpy(*logmsg, a->cmdline_log, len);
2544 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2545 return got_error_from_errno("asprintf");
2547 if (asprintf(&initial_content,
2548 "\n# changes to be committed on branch %s:\n",
2549 a->branch_name) == -1)
2550 return got_error_from_errno("asprintf");
2552 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2556 dprintf(fd, initial_content);
2558 TAILQ_FOREACH(pe, commitable_paths, entry) {
2559 struct got_commitable *ct = pe->data;
2560 dprintf(fd, "# %c %s\n",
2561 got_commitable_get_status(ct),
2562 got_commitable_get_path(ct));
2566 if (stat(a->logmsg_path, &st) == -1) {
2567 err = got_error_from_errno2("stat", a->logmsg_path);
2571 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2572 err = got_error_from_errno("failed spawning editor");
2576 if (stat(a->logmsg_path, &st2) == -1) {
2577 err = got_error_from_errno("stat");
2581 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2582 unlink(a->logmsg_path);
2583 free(a->logmsg_path);
2584 a->logmsg_path = NULL;
2585 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2586 "no changes made to commit message, aborting");
2590 *logmsg = malloc(st2.st_size + 1);
2591 if (*logmsg == NULL) {
2592 err = got_error_from_errno("malloc");
2595 (*logmsg)[0] = '\0';
2598 fp = fopen(a->logmsg_path, "r");
2600 err = got_error_from_errno("fopen");
2603 while (fgets(buf, sizeof(buf), fp) != NULL) {
2604 if (!content_changed && strcmp(buf, initial_content) != 0)
2605 content_changed = 1;
2606 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2607 continue; /* remove comments and leading empty lines */
2608 len = strlcat(*logmsg, buf, st2.st_size);
2612 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2613 (*logmsg)[len - 1] = '\0';
2617 if (len == 0 || !content_changed) {
2618 unlink(a->logmsg_path);
2619 free(a->logmsg_path);
2620 a->logmsg_path = NULL;
2621 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2622 "commit message cannot be empty, aborting");
2626 free(initial_content);
2629 /* Editor is done; we can now apply unveil(2) */
2631 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2635 static const struct got_error *
2636 cmd_commit(int argc, char *argv[])
2638 const struct got_error *error = NULL;
2639 struct got_worktree *worktree = NULL;
2640 struct got_repository *repo = NULL;
2641 char *cwd = NULL, *path = NULL, *id_str = NULL;
2642 struct got_object_id *id = NULL;
2643 const char *logmsg = NULL;
2644 const char *got_author = getenv("GOT_AUTHOR");
2645 struct collect_commit_logmsg_arg cl_arg;
2646 char *editor = NULL;
2649 while ((ch = getopt(argc, argv, "m:")) != -1) {
2664 path = realpath(argv[0], NULL);
2666 error = got_error_from_errno2("realpath", argv[0]);
2669 got_path_strip_trailing_slashes(path);
2670 } else if (argc != 0)
2673 if (got_author == NULL) {
2674 /* TODO: Look current user up in password database */
2675 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2679 cwd = getcwd(NULL, 0);
2681 error = got_error_from_errno("getcwd");
2684 error = got_worktree_open(&worktree, cwd);
2688 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2693 * unveil(2) traverses exec(2); if an editor is used we have
2694 * to apply unveil after the log message has been written.
2696 if (logmsg == NULL || strlen(logmsg) == 0)
2697 error = get_editor(&editor);
2699 error = apply_unveil(got_repo_get_path(repo), 0,
2700 got_worktree_get_root_path(worktree), 0);
2704 cl_arg.editor = editor;
2705 cl_arg.cmdline_log = logmsg;
2706 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2707 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2708 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2709 cl_arg.branch_name += 5;
2710 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2711 cl_arg.branch_name += 6;
2712 cl_arg.repo_path = got_repo_get_path(repo);
2713 cl_arg.logmsg_path = NULL;
2714 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2715 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2717 if (cl_arg.logmsg_path)
2718 fprintf(stderr, "%s: log message preserved in %s\n",
2719 getprogname(), cl_arg.logmsg_path);
2723 if (cl_arg.logmsg_path)
2724 unlink(cl_arg.logmsg_path);
2726 error = got_object_id_str(&id_str, id);
2729 printf("Created commit %s\n", id_str);
2732 got_repo_close(repo);
2734 got_worktree_close(worktree);
2743 usage_cherrypick(void)
2745 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2749 static const struct got_error *
2750 cmd_cherrypick(int argc, char *argv[])
2752 const struct got_error *error = NULL;
2753 struct got_worktree *worktree = NULL;
2754 struct got_repository *repo = NULL;
2755 char *cwd = NULL, *commit_id_str = NULL;
2756 struct got_object_id *commit_id = NULL;
2757 struct got_commit_object *commit = NULL;
2758 struct got_object_qid *pid;
2759 struct got_reference *head_ref = NULL;
2760 int ch, did_something = 0;
2762 while ((ch = getopt(argc, argv, "")) != -1) {
2776 cwd = getcwd(NULL, 0);
2778 error = got_error_from_errno("getcwd");
2781 error = got_worktree_open(&worktree, cwd);
2785 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2789 error = apply_unveil(got_repo_get_path(repo), 0,
2790 got_worktree_get_root_path(worktree), 0);
2794 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2795 if (error != NULL) {
2796 struct got_reference *ref;
2797 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2799 error = got_ref_open(&ref, repo, argv[0], 0);
2802 error = got_ref_resolve(&commit_id, repo, ref);
2807 error = got_object_id_str(&commit_id_str, commit_id);
2811 error = got_ref_open(&head_ref, repo,
2812 got_worktree_get_head_ref_name(worktree), 0);
2816 error = check_same_branch(commit_id, head_ref, repo);
2818 if (error->code != GOT_ERR_ANCESTRY)
2822 error = got_error(GOT_ERR_SAME_BRANCH);
2826 error = got_object_open_as_commit(&commit, repo, commit_id);
2829 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2830 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
2831 commit_id, repo, update_progress, &did_something, check_cancelled,
2837 printf("Merged commit %s\n", commit_id_str);
2840 got_object_commit_close(commit);
2841 free(commit_id_str);
2843 got_ref_close(head_ref);
2845 got_worktree_close(worktree);
2847 got_repo_close(repo);
2854 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
2858 static const struct got_error *
2859 cmd_backout(int argc, char *argv[])
2861 const struct got_error *error = NULL;
2862 struct got_worktree *worktree = NULL;
2863 struct got_repository *repo = NULL;
2864 char *cwd = NULL, *commit_id_str = NULL;
2865 struct got_object_id *commit_id = NULL;
2866 struct got_commit_object *commit = NULL;
2867 struct got_object_qid *pid;
2868 struct got_reference *head_ref = NULL;
2869 int ch, did_something = 0;
2871 while ((ch = getopt(argc, argv, "")) != -1) {
2885 cwd = getcwd(NULL, 0);
2887 error = got_error_from_errno("getcwd");
2890 error = got_worktree_open(&worktree, cwd);
2894 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2898 error = apply_unveil(got_repo_get_path(repo), 0,
2899 got_worktree_get_root_path(worktree), 0);
2903 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2904 if (error != NULL) {
2905 struct got_reference *ref;
2906 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2908 error = got_ref_open(&ref, repo, argv[0], 0);
2911 error = got_ref_resolve(&commit_id, repo, ref);
2916 error = got_object_id_str(&commit_id_str, commit_id);
2920 error = got_ref_open(&head_ref, repo,
2921 got_worktree_get_head_ref_name(worktree), 0);
2925 error = check_same_branch(commit_id, head_ref, repo);
2929 error = got_object_open_as_commit(&commit, repo, commit_id);
2932 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2934 error = got_error(GOT_ERR_ROOT_COMMIT);
2938 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
2939 update_progress, &did_something, check_cancelled, NULL);
2944 printf("Backed out commit %s\n", commit_id_str);
2947 got_object_commit_close(commit);
2948 free(commit_id_str);
2950 got_ref_close(head_ref);
2952 got_worktree_close(worktree);
2954 got_repo_close(repo);