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>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_reference.h"
37 #include "got_repository.h"
38 #include "got_worktree.h"
40 #include "got_commit_graph.h"
41 #include "got_blame.h"
42 #include "got_privsep.h"
46 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
49 static volatile sig_atomic_t sigint_received;
50 static volatile sig_atomic_t sigpipe_received;
53 catch_sigint(int signo)
59 catch_sigpipe(int signo)
67 const struct got_error *(*cmd_main)(int, char *[]);
68 void (*cmd_usage)(void);
69 const char *cmd_descr;
72 __dead static void usage(void);
73 __dead static void usage_checkout(void);
74 __dead static void usage_update(void);
75 __dead static void usage_log(void);
76 __dead static void usage_diff(void);
77 __dead static void usage_blame(void);
78 __dead static void usage_tree(void);
79 __dead static void usage_status(void);
80 __dead static void usage_ref(void);
81 __dead static void usage_add(void);
82 __dead static void usage_rm(void);
83 __dead static void usage_revert(void);
84 __dead static void usage_commit(void);
86 static const struct got_error* cmd_checkout(int, char *[]);
87 static const struct got_error* cmd_update(int, char *[]);
88 static const struct got_error* cmd_log(int, char *[]);
89 static const struct got_error* cmd_diff(int, char *[]);
90 static const struct got_error* cmd_blame(int, char *[]);
91 static const struct got_error* cmd_tree(int, char *[]);
92 static const struct got_error* cmd_status(int, char *[]);
93 static const struct got_error* cmd_ref(int, char *[]);
94 static const struct got_error* cmd_add(int, char *[]);
95 static const struct got_error* cmd_rm(int, char *[]);
96 static const struct got_error* cmd_revert(int, char *[]);
97 static const struct got_error* cmd_commit(int, char *[]);
99 static struct cmd got_commands[] = {
100 { "checkout", cmd_checkout, usage_checkout,
101 "check out a new work tree from a repository" },
102 { "update", cmd_update, usage_update,
103 "update a work tree to a different commit" },
104 { "log", cmd_log, usage_log,
105 "show repository history" },
106 { "diff", cmd_diff, usage_diff,
107 "compare files and directories" },
108 { "blame", cmd_blame, usage_blame,
109 "show when lines in a file were changed" },
110 { "tree", cmd_tree, usage_tree,
111 "list files and directories in repository" },
112 { "status", cmd_status, usage_status,
113 "show modification status of files" },
114 { "ref", cmd_ref, usage_ref,
115 "manage references in repository" },
116 { "add", cmd_add, usage_add,
117 "add a new file to version control" },
118 { "rm", cmd_rm, usage_rm,
119 "remove a versioned file" },
120 { "revert", cmd_revert, usage_revert,
121 "revert uncommitted changes" },
122 { "commit", cmd_commit, usage_commit,
123 "write changes from work tree to repository" },
127 main(int argc, char *argv[])
134 setlocale(LC_CTYPE, "");
136 while ((ch = getopt(argc, argv, "h")) != -1) {
154 signal(SIGINT, catch_sigint);
155 signal(SIGPIPE, catch_sigpipe);
157 for (i = 0; i < nitems(got_commands); i++) {
158 const struct got_error *error;
160 cmd = &got_commands[i];
162 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
166 got_commands[i].cmd_usage();
168 error = got_commands[i].cmd_main(argc, argv);
169 if (error && !(sigint_received || sigpipe_received)) {
170 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
177 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
186 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
187 "Available commands:\n", getprogname());
188 for (i = 0; i < nitems(got_commands); i++) {
189 struct cmd *cmd = &got_commands[i];
190 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
195 static const struct got_error *
196 apply_unveil(const char *repo_path, int repo_read_only,
197 const char *worktree_path, int create_worktree)
199 const struct got_error *error;
201 if (create_worktree) {
202 /* Pre-create work tree path to avoid unveiling its parents. */
203 error = got_path_mkdir(worktree_path);
204 if (error && (error->code != GOT_ERR_ERRNO || errno != EISDIR))
208 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
209 return got_error_prefix_errno2("unveil", repo_path);
211 if (worktree_path && unveil(worktree_path, "rwc") != 0)
212 return got_error_prefix_errno2("unveil", worktree_path);
214 if (unveil("/tmp", "rwc") != 0)
215 return got_error_prefix_errno2("unveil", "/tmp");
217 error = got_privsep_unveil_exec_helpers();
221 if (unveil(NULL, NULL) != 0)
222 return got_error_prefix_errno("unveil");
230 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
231 "[worktree-path]\n", getprogname());
236 checkout_progress(void *arg, unsigned char status, const char *path)
238 char *worktree_path = arg;
240 while (path[0] == '/')
243 printf("%c %s/%s\n", status, worktree_path, path);
246 static const struct got_error *
247 check_cancelled(void *arg)
249 if (sigint_received || sigpipe_received)
250 return got_error(GOT_ERR_CANCELLED);
254 static const struct got_error *
255 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
256 struct got_repository *repo)
258 const struct got_error *err;
259 struct got_reference *head_ref = NULL;
260 struct got_object_id *head_commit_id = NULL;
261 struct got_commit_graph *graph = NULL;
263 err = got_ref_open(&head_ref, repo,
264 got_worktree_get_head_ref_name(worktree));
268 /* TODO: Check the reflog. The head ref may have been rebased. */
269 err = got_ref_resolve(&head_commit_id, repo, head_ref);
273 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
277 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
281 struct got_object_id *id;
283 if (sigint_received || sigpipe_received)
286 err = got_commit_graph_iter_next(&id, graph);
288 if (err->code == GOT_ERR_ITER_COMPLETED) {
289 err = got_error(GOT_ERR_ANCESTRY);
292 if (err->code != GOT_ERR_ITER_NEED_MORE)
294 err = got_commit_graph_fetch_commits(graph, 1, repo);
302 if (got_object_id_cmp(id, commit_id) == 0)
307 got_ref_close(head_ref);
309 got_commit_graph_close(graph);
314 static const struct got_error *
315 cmd_checkout(int argc, char *argv[])
317 const struct got_error *error = NULL;
318 struct got_repository *repo = NULL;
319 struct got_reference *head_ref = NULL;
320 struct got_worktree *worktree = NULL;
321 char *repo_path = NULL;
322 char *worktree_path = NULL;
323 const char *path_prefix = "";
324 char *commit_id_str = NULL;
325 int ch, same_path_prefix;
327 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
330 commit_id_str = strdup(optarg);
331 if (commit_id_str == NULL)
332 return got_error_prefix_errno("strdup");
335 path_prefix = optarg;
347 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
348 "unveil", NULL) == -1)
352 char *cwd, *base, *dotgit;
353 repo_path = realpath(argv[0], NULL);
354 if (repo_path == NULL)
355 return got_error_prefix_errno2("realpath", argv[0]);
356 cwd = getcwd(NULL, 0);
358 error = got_error_prefix_errno("getcwd");
361 if (path_prefix[0]) {
362 base = basename(path_prefix);
364 error = got_error_prefix_errno2("basename",
369 base = basename(repo_path);
371 error = got_error_prefix_errno2("basename",
376 dotgit = strstr(base, ".git");
379 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
380 error = got_error_prefix_errno("asprintf");
385 } else if (argc == 2) {
386 repo_path = realpath(argv[0], NULL);
387 if (repo_path == NULL) {
388 error = got_error_prefix_errno2("realpath", argv[0]);
391 worktree_path = realpath(argv[1], NULL);
392 if (worktree_path == NULL) {
393 error = got_error_prefix_errno2("realpath", argv[1]);
399 got_path_strip_trailing_slashes(repo_path);
400 got_path_strip_trailing_slashes(worktree_path);
402 error = got_repo_open(&repo, repo_path);
406 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
410 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
414 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
415 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
418 error = got_worktree_open(&worktree, worktree_path);
422 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
426 if (!same_path_prefix) {
427 error = got_error(GOT_ERR_PATH_PREFIX);
432 struct got_object_id *commit_id;
433 error = got_object_resolve_id_str(&commit_id, repo,
437 error = check_ancestry(worktree, commit_id, repo);
442 error = got_worktree_set_base_commit_id(worktree, repo,
449 error = got_worktree_checkout_files(worktree, "", repo,
450 checkout_progress, worktree_path, check_cancelled, NULL);
454 printf("Now shut up and hack\n");
466 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
472 update_progress(void *arg, unsigned char status, const char *path)
474 int *did_something = arg;
476 if (status == GOT_STATUS_EXISTS)
480 while (path[0] == '/')
482 printf("%c %s\n", status, path);
485 static const struct got_error *
486 cmd_update(int argc, char *argv[])
488 const struct got_error *error = NULL;
489 struct got_repository *repo = NULL;
490 struct got_worktree *worktree = NULL;
491 char *worktree_path = NULL, *path = NULL;
492 struct got_object_id *commit_id = NULL;
493 char *commit_id_str = NULL;
494 int ch, did_something = 0;
496 while ((ch = getopt(argc, argv, "c:")) != -1) {
499 commit_id_str = strdup(optarg);
500 if (commit_id_str == NULL)
501 return got_error_prefix_errno("strdup");
513 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
514 "unveil", NULL) == -1)
517 worktree_path = getcwd(NULL, 0);
518 if (worktree_path == NULL) {
519 error = got_error_prefix_errno("getcwd");
522 error = got_worktree_open(&worktree, worktree_path);
529 error = got_error_prefix_errno("strdup");
532 } else if (argc == 1) {
533 error = got_worktree_resolve_path(&path, worktree, argv[0]);
539 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
543 error = apply_unveil(got_repo_get_path(repo), 0,
544 got_worktree_get_root_path(worktree), 0);
548 if (commit_id_str == NULL) {
549 struct got_reference *head_ref;
550 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
553 error = got_ref_resolve(&commit_id, repo, head_ref);
556 error = got_object_id_str(&commit_id_str, commit_id);
560 error = got_object_resolve_id_str(&commit_id, repo,
566 error = check_ancestry(worktree, commit_id, repo);
570 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
572 error = got_worktree_set_base_commit_id(worktree, repo,
578 error = got_worktree_checkout_files(worktree, path, repo,
579 update_progress, &did_something, check_cancelled, NULL);
584 printf("Updated to commit %s\n", commit_id_str);
586 printf("Already up-to-date\n");
595 static const struct got_error *
596 print_patch(struct got_commit_object *commit, struct got_object_id *id,
597 int diff_context, struct got_repository *repo)
599 const struct got_error *err = NULL;
600 struct got_tree_object *tree1 = NULL, *tree2;
601 struct got_object_qid *qid;
602 char *id_str1 = NULL, *id_str2;
604 err = got_object_open_as_tree(&tree2, repo,
605 got_object_commit_get_tree_id(commit));
609 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
611 struct got_commit_object *pcommit;
613 err = got_object_open_as_commit(&pcommit, repo, qid->id);
617 err = got_object_open_as_tree(&tree1, repo,
618 got_object_commit_get_tree_id(pcommit));
619 got_object_commit_close(pcommit);
623 err = got_object_id_str(&id_str1, qid->id);
628 err = got_object_id_str(&id_str2, id);
632 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
633 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
636 got_object_tree_close(tree1);
637 got_object_tree_close(tree2);
644 get_datestr(time_t *time, char *datebuf)
646 char *p, *s = ctime_r(time, datebuf);
653 static const struct got_error *
654 print_commit(struct got_commit_object *commit, struct got_object_id *id,
655 struct got_repository *repo, int show_patch, int diff_context,
656 struct got_reflist_head *refs)
658 const struct got_error *err = NULL;
659 char *id_str, *datestr, *logmsg0, *logmsg, *line;
661 time_t committer_time;
662 const char *author, *committer;
663 char *refs_str = NULL;
664 struct got_reflist_entry *re;
666 SIMPLEQ_FOREACH(re, refs, entry) {
669 if (got_object_id_cmp(re->id, id) != 0)
671 name = got_ref_get_name(re->ref);
672 if (strcmp(name, GOT_REF_HEAD) == 0)
674 if (strncmp(name, "refs/", 5) == 0)
676 if (strncmp(name, "got/", 4) == 0)
678 if (strncmp(name, "heads/", 6) == 0)
680 if (strncmp(name, "remotes/", 8) == 0)
683 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
685 err = got_error_prefix_errno("asprintf");
691 err = got_object_id_str(&id_str, id);
695 printf("-----------------------------------------------\n");
696 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
697 refs_str ? refs_str : "", refs_str ? ")" : "");
702 printf("from: %s\n", got_object_commit_get_author(commit));
703 committer_time = got_object_commit_get_committer_time(commit);
704 datestr = get_datestr(&committer_time, datebuf);
705 printf("date: %s UTC\n", datestr);
706 author = got_object_commit_get_author(commit);
707 committer = got_object_commit_get_committer(commit);
708 if (strcmp(author, committer) != 0)
709 printf("via: %s\n", committer);
710 if (got_object_commit_get_nparents(commit) > 1) {
711 const struct got_object_id_queue *parent_ids;
712 struct got_object_qid *qid;
714 parent_ids = got_object_commit_get_parent_ids(commit);
715 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
716 err = got_object_id_str(&id_str, qid->id);
719 printf("parent %d: %s\n", n++, id_str);
724 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
726 return got_error_prefix_errno("strdup");
730 line = strsep(&logmsg, "\n");
732 printf(" %s\n", line);
737 err = print_patch(commit, id, diff_context, repo);
742 if (fflush(stdout) != 0 && err == NULL)
743 err = got_error_prefix_errno("fflush");
747 static const struct got_error *
748 print_commits(struct got_object_id *root_id, struct got_repository *repo,
749 char *path, int show_patch, int diff_context, int limit,
750 int first_parent_traversal, struct got_reflist_head *refs)
752 const struct got_error *err;
753 struct got_commit_graph *graph;
755 err = got_commit_graph_open(&graph, root_id, path,
756 first_parent_traversal, repo);
759 err = got_commit_graph_iter_start(graph, root_id, repo);
763 struct got_commit_object *commit;
764 struct got_object_id *id;
766 if (sigint_received || sigpipe_received)
769 err = got_commit_graph_iter_next(&id, graph);
771 if (err->code == GOT_ERR_ITER_COMPLETED) {
775 if (err->code != GOT_ERR_ITER_NEED_MORE)
777 err = got_commit_graph_fetch_commits(graph, 1, repo);
786 err = got_object_open_as_commit(&commit, repo, id);
789 err = print_commit(commit, id, repo, show_patch, diff_context,
791 got_object_commit_close(commit);
792 if (err || (limit && --limit == 0))
796 got_commit_graph_close(graph);
803 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
804 "[-r repository-path] [path]\n", getprogname());
808 static const struct got_error *
809 cmd_log(int argc, char *argv[])
811 const struct got_error *error;
812 struct got_repository *repo = NULL;
813 struct got_worktree *worktree = NULL;
814 struct got_commit_object *commit = NULL;
815 struct got_object_id *id = NULL;
816 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
817 char *start_commit = NULL;
818 int diff_context = 3, ch;
819 int show_patch = 0, limit = 0, first_parent_traversal = 0;
821 struct got_reflist_head refs;
826 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
832 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
838 start_commit = optarg;
841 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
844 err(1, "-C option %s", errstr);
847 limit = strtonum(optarg, 1, INT_MAX, &errstr);
849 err(1, "-l option %s", errstr);
852 first_parent_traversal = 1;
855 repo_path = realpath(optarg, NULL);
856 if (repo_path == NULL)
858 got_path_strip_trailing_slashes(repo_path);
869 cwd = getcwd(NULL, 0);
871 error = got_error_prefix_errno("getcwd");
875 error = got_worktree_open(&worktree, cwd);
876 if (error && error->code != GOT_ERR_NOT_WORKTREE)
883 error = got_error_prefix_errno("strdup");
886 } else if (argc == 1) {
888 error = got_worktree_resolve_path(&path, worktree,
893 path = strdup(argv[0]);
895 error = got_error_prefix_errno("strdup");
902 if (repo_path == NULL) {
903 repo_path = worktree ?
904 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
906 if (repo_path == NULL) {
907 error = got_error_prefix_errno("strdup");
911 error = got_repo_open(&repo, repo_path);
915 error = apply_unveil(got_repo_get_path(repo), 1,
916 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
920 if (start_commit == NULL) {
921 struct got_reference *head_ref;
922 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
925 error = got_ref_resolve(&id, repo, head_ref);
926 got_ref_close(head_ref);
929 error = got_object_open_as_commit(&commit, repo, id);
931 struct got_reference *ref;
932 error = got_ref_open(&ref, repo, start_commit);
935 error = got_ref_resolve(&id, repo, ref);
939 error = got_object_get_type(&obj_type, repo, id);
942 if (obj_type == GOT_OBJ_TYPE_TAG) {
943 struct got_tag_object *tag;
944 error = got_object_open_as_tag(&tag, repo, id);
947 if (got_object_tag_get_object_type(tag) !=
948 GOT_OBJ_TYPE_COMMIT) {
949 got_object_tag_close(tag);
950 error = got_error(GOT_ERR_OBJ_TYPE);
954 id = got_object_id_dup(
955 got_object_tag_get_object_id(tag));
957 error = got_error_prefix_errno(
958 "got_object_id_dup");
959 got_object_tag_close(tag);
962 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
963 error = got_error(GOT_ERR_OBJ_TYPE);
966 error = got_object_open_as_commit(&commit, repo, id);
970 if (commit == NULL) {
971 error = got_object_resolve_id_str(&id, repo,
980 error = got_repo_map_path(&in_repo_path, repo, path, 1);
988 error = got_ref_list(&refs, repo);
992 error = print_commits(id, repo, path, show_patch,
993 diff_context, limit, first_parent_traversal, &refs);
1000 got_worktree_close(worktree);
1002 const struct got_error *repo_error;
1003 repo_error = got_repo_close(repo);
1007 got_ref_list_free(&refs);
1014 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1015 "[object1 object2 | path]\n", getprogname());
1019 struct print_diff_arg {
1020 struct got_repository *repo;
1021 struct got_worktree *worktree;
1027 static const struct got_error *
1028 print_diff(void *arg, unsigned char status, const char *path,
1029 struct got_object_id *id)
1031 struct print_diff_arg *a = arg;
1032 const struct got_error *err = NULL;
1033 struct got_blob_object *blob1 = NULL;
1035 char *abspath = NULL;
1038 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1039 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1042 if (!a->header_shown) {
1043 printf("diff %s %s\n", a->id_str,
1044 got_worktree_get_root_path(a->worktree));
1045 a->header_shown = 1;
1048 if (status != GOT_STATUS_ADD) {
1049 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1055 if (status != GOT_STATUS_DELETE) {
1056 if (asprintf(&abspath, "%s/%s",
1057 got_worktree_get_root_path(a->worktree), path) == -1) {
1058 err = got_error_prefix_errno("asprintf");
1062 f2 = fopen(abspath, "r");
1064 err = got_error_prefix_errno2("fopen", abspath);
1067 if (lstat(abspath, &sb) == -1) {
1068 err = got_error_prefix_errno2("lstat", abspath);
1074 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1078 got_object_blob_close(blob1);
1079 if (f2 && fclose(f2) != 0 && err == NULL)
1080 err = got_error_prefix_errno("fclose");
1085 static const struct got_error *
1086 cmd_diff(int argc, char *argv[])
1088 const struct got_error *error;
1089 struct got_repository *repo = NULL;
1090 struct got_worktree *worktree = NULL;
1091 char *cwd = NULL, *repo_path = NULL;
1092 struct got_object_id *id1 = NULL, *id2 = NULL;
1093 char *id_str1 = NULL, *id_str2 = NULL;
1095 int diff_context = 3, ch;
1100 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1105 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1108 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1110 err(1, "-C option %s", errstr);
1113 repo_path = realpath(optarg, NULL);
1114 if (repo_path == NULL)
1115 err(1, "-r option");
1116 got_path_strip_trailing_slashes(repo_path);
1127 cwd = getcwd(NULL, 0);
1129 error = got_error_prefix_errno("getcwd");
1132 error = got_worktree_open(&worktree, cwd);
1133 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1136 if (worktree == NULL) {
1137 error = got_error(GOT_ERR_NOT_WORKTREE);
1142 "-r option can't be used when diffing a work tree");
1143 repo_path = strdup(got_worktree_get_repo_path(worktree));
1144 if (repo_path == NULL) {
1145 error = got_error_prefix_errno("strdup");
1149 error = got_worktree_resolve_path(&path, worktree,
1156 error = got_error_prefix_errno("strdup");
1160 } else if (argc == 2) {
1166 if (repo_path == NULL) {
1167 repo_path = getcwd(NULL, 0);
1168 if (repo_path == NULL)
1169 return got_error_prefix_errno("getcwd");
1172 error = got_repo_open(&repo, repo_path);
1177 error = apply_unveil(got_repo_get_path(repo), 1,
1178 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1183 struct print_diff_arg arg;
1185 error = got_object_id_str(&id_str,
1186 got_worktree_get_base_commit_id(worktree));
1190 arg.worktree = worktree;
1191 arg.diff_context = diff_context;
1192 arg.id_str = id_str;
1193 arg.header_shown = 0;
1195 error = got_worktree_status(worktree, path, repo, print_diff,
1196 &arg, check_cancelled, NULL);
1201 error = got_object_resolve_id_str(&id1, repo, id_str1);
1205 error = got_object_resolve_id_str(&id2, repo, id_str2);
1209 error = got_object_get_type(&type1, repo, id1);
1213 error = got_object_get_type(&type2, repo, id2);
1217 if (type1 != type2) {
1218 error = got_error(GOT_ERR_OBJ_TYPE);
1223 case GOT_OBJ_TYPE_BLOB:
1224 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1225 diff_context, repo, stdout);
1227 case GOT_OBJ_TYPE_TREE:
1228 error = got_diff_objects_as_trees(id1, id2, "", "",
1229 diff_context, repo, stdout);
1231 case GOT_OBJ_TYPE_COMMIT:
1232 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1234 error = got_diff_objects_as_commits(id1, id2, diff_context,
1238 error = got_error(GOT_ERR_OBJ_TYPE);
1246 got_worktree_close(worktree);
1248 const struct got_error *repo_error;
1249 repo_error = got_repo_close(repo);
1260 "usage: %s blame [-c commit] [-r repository-path] path\n",
1265 static const struct got_error *
1266 cmd_blame(int argc, char *argv[])
1268 const struct got_error *error;
1269 struct got_repository *repo = NULL;
1270 struct got_worktree *worktree = NULL;
1271 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1272 struct got_object_id *commit_id = NULL;
1273 char *commit_id_str = NULL;
1277 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1282 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1285 commit_id_str = optarg;
1288 repo_path = realpath(optarg, NULL);
1289 if (repo_path == NULL)
1290 err(1, "-r option");
1291 got_path_strip_trailing_slashes(repo_path);
1307 cwd = getcwd(NULL, 0);
1309 error = got_error_prefix_errno("getcwd");
1312 if (repo_path == NULL) {
1313 error = got_worktree_open(&worktree, cwd);
1314 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1320 strdup(got_worktree_get_repo_path(worktree));
1321 if (repo_path == NULL)
1322 error = got_error_prefix_errno("strdup");
1326 repo_path = strdup(cwd);
1327 if (repo_path == NULL) {
1328 error = got_error_prefix_errno("strdup");
1334 error = got_repo_open(&repo, repo_path);
1338 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1343 const char *prefix = got_worktree_get_path_prefix(worktree);
1344 char *p, *worktree_subdir = cwd +
1345 strlen(got_worktree_get_root_path(worktree));
1346 if (asprintf(&p, "%s%s%s%s%s",
1347 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1348 worktree_subdir, worktree_subdir[0] ? "/" : "",
1350 error = got_error_prefix_errno("asprintf");
1353 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1356 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1361 if (commit_id_str == NULL) {
1362 struct got_reference *head_ref;
1363 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1366 error = got_ref_resolve(&commit_id, repo, head_ref);
1367 got_ref_close(head_ref);
1371 error = got_object_resolve_id_str(&commit_id, repo,
1377 error = got_blame(in_repo_path, commit_id, repo, stdout);
1384 got_worktree_close(worktree);
1386 const struct got_error *repo_error;
1387 repo_error = got_repo_close(repo);
1398 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1404 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1405 const char *root_path)
1407 int is_root_path = (strcmp(path, root_path) == 0);
1409 path += strlen(root_path);
1410 while (path[0] == '/')
1413 printf("%s%s%s%s%s\n", id ? id : "", path,
1414 is_root_path ? "" : "/", te->name,
1415 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1418 static const struct got_error *
1419 print_tree(const char *path, struct got_object_id *commit_id,
1420 int show_ids, int recurse, const char *root_path,
1421 struct got_repository *repo)
1423 const struct got_error *err = NULL;
1424 struct got_object_id *tree_id = NULL;
1425 struct got_tree_object *tree = NULL;
1426 const struct got_tree_entries *entries;
1427 struct got_tree_entry *te;
1429 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1433 err = got_object_open_as_tree(&tree, repo, tree_id);
1436 entries = got_object_tree_get_entries(tree);
1437 te = SIMPLEQ_FIRST(&entries->head);
1441 if (sigint_received || sigpipe_received)
1446 err = got_object_id_str(&id_str, te->id);
1449 if (asprintf(&id, "%s ", id_str) == -1) {
1450 err = got_error_prefix_errno("asprintf");
1456 print_entry(te, id, path, root_path);
1459 if (recurse && S_ISDIR(te->mode)) {
1461 if (asprintf(&child_path, "%s%s%s", path,
1462 path[0] == '/' && path[1] == '\0' ? "" : "/",
1464 err = got_error_prefix_errno("asprintf");
1467 err = print_tree(child_path, commit_id, show_ids, 1,
1474 te = SIMPLEQ_NEXT(te, entry);
1478 got_object_tree_close(tree);
1483 static const struct got_error *
1484 cmd_tree(int argc, char *argv[])
1486 const struct got_error *error;
1487 struct got_repository *repo = NULL;
1488 struct got_worktree *worktree = NULL;
1490 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1491 struct got_object_id *commit_id = NULL;
1492 char *commit_id_str = NULL;
1493 int show_ids = 0, recurse = 0;
1497 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1502 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1505 commit_id_str = optarg;
1508 repo_path = realpath(optarg, NULL);
1509 if (repo_path == NULL)
1510 err(1, "-r option");
1511 got_path_strip_trailing_slashes(repo_path);
1535 cwd = getcwd(NULL, 0);
1537 error = got_error_prefix_errno("getcwd");
1540 if (repo_path == NULL) {
1541 error = got_worktree_open(&worktree, cwd);
1542 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1548 strdup(got_worktree_get_repo_path(worktree));
1549 if (repo_path == NULL)
1550 error = got_error_prefix_errno("strdup");
1554 repo_path = strdup(cwd);
1555 if (repo_path == NULL) {
1556 error = got_error_prefix_errno("strdup");
1562 error = got_repo_open(&repo, repo_path);
1566 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1572 char *p, *worktree_subdir = cwd +
1573 strlen(got_worktree_get_root_path(worktree));
1574 if (asprintf(&p, "%s/%s",
1575 got_worktree_get_path_prefix(worktree),
1576 worktree_subdir) == -1) {
1577 error = got_error_prefix_errno("asprintf");
1580 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1587 if (in_repo_path == NULL) {
1588 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1593 if (commit_id_str == NULL) {
1594 struct got_reference *head_ref;
1595 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1598 error = got_ref_resolve(&commit_id, repo, head_ref);
1599 got_ref_close(head_ref);
1603 error = got_object_resolve_id_str(&commit_id, repo,
1609 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1610 in_repo_path, repo);
1617 got_worktree_close(worktree);
1619 const struct got_error *repo_error;
1620 repo_error = got_repo_close(repo);
1630 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1634 static const struct got_error *
1635 print_status(void *arg, unsigned char status, const char *path,
1636 struct got_object_id *id)
1638 printf("%c %s\n", status, path);
1642 static const struct got_error *
1643 cmd_status(int argc, char *argv[])
1645 const struct got_error *error = NULL;
1646 struct got_repository *repo = NULL;
1647 struct got_worktree *worktree = NULL;
1648 char *cwd = NULL, *path = NULL;
1651 while ((ch = getopt(argc, argv, "")) != -1) {
1663 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1667 cwd = getcwd(NULL, 0);
1669 error = got_error_prefix_errno("getcwd");
1673 error = got_worktree_open(&worktree, cwd);
1680 error = got_error_prefix_errno("strdup");
1683 } else if (argc == 1) {
1684 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1690 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1694 error = apply_unveil(got_repo_get_path(repo), 1,
1695 got_worktree_get_root_path(worktree), 0);
1699 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1700 check_cancelled, NULL);
1711 "usage: %s ref [-r repository] -l | -d name | name object\n",
1716 static const struct got_error *
1717 list_refs(struct got_repository *repo)
1719 static const struct got_error *err = NULL;
1720 struct got_reflist_head refs;
1721 struct got_reflist_entry *re;
1723 SIMPLEQ_INIT(&refs);
1724 err = got_ref_list(&refs, repo);
1728 SIMPLEQ_FOREACH(re, &refs, entry) {
1730 refstr = got_ref_to_str(re->ref);
1732 return got_error_prefix_errno("got_ref_to_str");
1733 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1737 got_ref_list_free(&refs);
1741 static const struct got_error *
1742 delete_ref(struct got_repository *repo, const char *refname)
1744 const struct got_error *err = NULL;
1745 struct got_reference *ref;
1747 err = got_ref_open(&ref, repo, refname);
1751 err = got_ref_delete(ref, repo);
1756 static const struct got_error *
1757 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1759 const struct got_error *err = NULL;
1760 struct got_object_id *id;
1761 struct got_reference *ref = NULL;
1763 err = got_object_resolve_id_str(&id, repo, id_str);
1767 err = got_ref_alloc(&ref, refname, id);
1771 err = got_ref_write(ref, repo);
1779 static const struct got_error *
1780 cmd_ref(int argc, char *argv[])
1782 const struct got_error *error = NULL;
1783 struct got_repository *repo = NULL;
1784 struct got_worktree *worktree = NULL;
1785 char *cwd = NULL, *repo_path = NULL;
1786 int ch, do_list = 0;
1787 const char *delref = NULL;
1789 /* TODO: Add -s option for adding symbolic references. */
1790 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1796 repo_path = realpath(optarg, NULL);
1797 if (repo_path == NULL)
1798 err(1, "-r option");
1799 got_path_strip_trailing_slashes(repo_path);
1810 if (do_list && delref)
1811 errx(1, "-l and -d options are mutually exclusive\n");
1816 if (do_list || delref) {
1819 } else if (argc != 2)
1824 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1828 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1829 "sendfd unveil", NULL) == -1)
1833 cwd = getcwd(NULL, 0);
1835 error = got_error_prefix_errno("getcwd");
1839 if (repo_path == NULL) {
1840 error = got_worktree_open(&worktree, cwd);
1841 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1847 strdup(got_worktree_get_repo_path(worktree));
1848 if (repo_path == NULL)
1849 error = got_error_prefix_errno("strdup");
1853 repo_path = strdup(cwd);
1854 if (repo_path == NULL) {
1855 error = got_error_prefix_errno("strdup");
1861 error = got_repo_open(&repo, repo_path);
1865 error = apply_unveil(got_repo_get_path(repo), do_list,
1866 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1871 error = list_refs(repo);
1873 error = delete_ref(repo, delref);
1875 error = add_ref(repo, argv[0], argv[1]);
1878 got_repo_close(repo);
1880 got_worktree_close(worktree);
1889 fprintf(stderr, "usage: %s add file-path\n", getprogname());
1893 static const struct got_error *
1894 cmd_add(int argc, char *argv[])
1896 const struct got_error *error = NULL;
1897 struct got_repository *repo = NULL;
1898 struct got_worktree *worktree = NULL;
1899 char *cwd = NULL, *path = NULL, *relpath = NULL;
1902 while ((ch = getopt(argc, argv, "")) != -1) {
1916 path = realpath(argv[0], NULL);
1918 error = got_error_prefix_errno2("realpath", argv[0]);
1921 got_path_strip_trailing_slashes(path);
1923 cwd = getcwd(NULL, 0);
1925 error = got_error_prefix_errno("getcwd");
1928 error = got_worktree_open(&worktree, cwd);
1932 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1936 error = apply_unveil(got_repo_get_path(repo), 1,
1937 got_worktree_get_root_path(worktree), 0);
1941 error = got_worktree_schedule_add(worktree, path, print_status, NULL,
1947 got_repo_close(repo);
1949 got_worktree_close(worktree);
1959 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1963 static const struct got_error *
1964 cmd_rm(int argc, char *argv[])
1966 const struct got_error *error = NULL;
1967 struct got_worktree *worktree = NULL;
1968 struct got_repository *repo = NULL;
1969 char *cwd = NULL, *path = NULL;
1970 int ch, delete_local_mods = 0;
1972 while ((ch = getopt(argc, argv, "f")) != -1) {
1975 delete_local_mods = 1;
1989 path = realpath(argv[0], NULL);
1991 error = got_error_prefix_errno2("realpath", argv[0]);
1994 got_path_strip_trailing_slashes(path);
1996 cwd = getcwd(NULL, 0);
1998 error = got_error_prefix_errno("getcwd");
2001 error = got_worktree_open(&worktree, cwd);
2005 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2009 error = apply_unveil(got_repo_get_path(repo), 1,
2010 got_worktree_get_root_path(worktree), 0);
2014 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2015 print_status, NULL, repo);
2020 got_repo_close(repo);
2022 got_worktree_close(worktree);
2031 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2036 revert_progress(void *arg, unsigned char status, const char *path)
2038 while (path[0] == '/')
2040 printf("%c %s\n", status, path);
2043 static const struct got_error *
2044 cmd_revert(int argc, char *argv[])
2046 const struct got_error *error = NULL;
2047 struct got_worktree *worktree = NULL;
2048 struct got_repository *repo = NULL;
2049 char *cwd = NULL, *path = NULL;
2052 while ((ch = getopt(argc, argv, "")) != -1) {
2066 path = realpath(argv[0], NULL);
2068 error = got_error_prefix_errno2("realpath", argv[0]);
2071 got_path_strip_trailing_slashes(path);
2073 cwd = getcwd(NULL, 0);
2075 error = got_error_prefix_errno("getcwd");
2078 error = got_worktree_open(&worktree, cwd);
2082 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2086 error = apply_unveil(got_repo_get_path(repo), 1,
2087 got_worktree_get_root_path(worktree), 0);
2091 error = got_worktree_revert(worktree, path,
2092 revert_progress, NULL, repo);
2097 got_repo_close(repo);
2099 got_worktree_close(worktree);
2108 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2112 static const struct got_error *
2113 cmd_commit(int argc, char *argv[])
2115 const struct got_error *error = NULL;
2116 struct got_worktree *worktree = NULL;
2117 struct got_repository *repo = NULL;
2118 char *cwd = NULL, *path = NULL, *id_str = NULL;
2119 struct got_object_id *id = NULL;
2120 const char *logmsg = "<no log message was specified>";
2121 const char *got_author = getenv("GOT_AUTHOR");
2124 while ((ch = getopt(argc, argv, "m:")) != -1) {
2139 path = realpath(argv[0], NULL);
2141 error = got_error_prefix_errno2("realpath", argv[0]);
2144 got_path_strip_trailing_slashes(path);
2145 } else if (argc != 0)
2148 if (got_author == NULL) {
2149 /* TODO: Look current user up in password database */
2150 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2154 cwd = getcwd(NULL, 0);
2156 error = got_error_prefix_errno("getcwd");
2159 error = got_worktree_open(&worktree, cwd);
2163 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2167 error = apply_unveil(got_repo_get_path(repo), 0,
2168 got_worktree_get_root_path(worktree), 0);
2172 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2173 logmsg, print_status, NULL, repo);
2177 error = got_object_id_str(&id_str, id);
2180 printf("created commit %s\n", id_str);
2183 got_repo_close(repo);
2185 got_worktree_close(worktree);