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_from_errno();
211 if (worktree_path && unveil(worktree_path, "rwc") != 0)
212 return got_error_from_errno();
214 if (unveil("/tmp", "rwc") != 0)
215 return got_error_from_errno();
217 error = got_privsep_unveil_exec_helpers();
221 if (unveil(NULL, NULL) != 0)
222 return got_error_from_errno();
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_from_errno();
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_from_errno();
356 cwd = getcwd(NULL, 0);
358 error = got_error_from_errno();
362 base = basename(path_prefix);
364 base = basename(repo_path);
366 error = got_error_from_errno();
369 dotgit = strstr(base, ".git");
372 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
373 error = got_error_from_errno();
378 } else if (argc == 2) {
379 repo_path = realpath(argv[0], NULL);
380 if (repo_path == NULL) {
381 error = got_error_from_errno();
384 worktree_path = realpath(argv[1], NULL);
385 if (worktree_path == NULL) {
386 error = got_error_from_errno();
392 got_path_strip_trailing_slashes(worktree_path);
394 error = got_repo_open(&repo, repo_path);
398 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
402 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
406 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
407 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
410 error = got_worktree_open(&worktree, worktree_path);
414 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
418 if (!same_path_prefix) {
419 error = got_error(GOT_ERR_PATH_PREFIX);
424 struct got_object_id *commit_id;
425 error = got_object_resolve_id_str(&commit_id, repo,
429 error = check_ancestry(worktree, commit_id, repo);
434 error = got_worktree_set_base_commit_id(worktree, repo,
441 error = got_worktree_checkout_files(worktree, "", repo,
442 checkout_progress, worktree_path, check_cancelled, NULL);
446 printf("Now shut up and hack\n");
458 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
464 update_progress(void *arg, unsigned char status, const char *path)
466 int *did_something = arg;
468 if (status == GOT_STATUS_EXISTS)
472 while (path[0] == '/')
474 printf("%c %s\n", status, path);
477 static const struct got_error *
478 cmd_update(int argc, char *argv[])
480 const struct got_error *error = NULL;
481 struct got_repository *repo = NULL;
482 struct got_worktree *worktree = NULL;
483 char *worktree_path = NULL, *path = NULL;
484 struct got_object_id *commit_id = NULL;
485 char *commit_id_str = NULL;
486 int ch, did_something = 0;
488 while ((ch = getopt(argc, argv, "c:")) != -1) {
491 commit_id_str = strdup(optarg);
492 if (commit_id_str == NULL)
493 return got_error_from_errno();
505 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
506 "unveil", NULL) == -1)
509 worktree_path = getcwd(NULL, 0);
510 if (worktree_path == NULL) {
511 error = got_error_from_errno();
514 error = got_worktree_open(&worktree, worktree_path);
521 error = got_error_from_errno();
524 } else if (argc == 1) {
525 error = got_worktree_resolve_path(&path, worktree, argv[0]);
531 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
535 error = apply_unveil(got_repo_get_path(repo), 0,
536 got_worktree_get_root_path(worktree), 0);
540 if (commit_id_str == NULL) {
541 struct got_reference *head_ref;
542 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
545 error = got_ref_resolve(&commit_id, repo, head_ref);
548 error = got_object_id_str(&commit_id_str, commit_id);
552 error = got_object_resolve_id_str(&commit_id, repo,
558 error = check_ancestry(worktree, commit_id, repo);
562 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
564 error = got_worktree_set_base_commit_id(worktree, repo,
570 error = got_worktree_checkout_files(worktree, path, repo,
571 update_progress, &did_something, check_cancelled, NULL);
576 printf("Updated to commit %s\n", commit_id_str);
578 printf("Already up-to-date\n");
587 static const struct got_error *
588 print_patch(struct got_commit_object *commit, struct got_object_id *id,
589 int diff_context, struct got_repository *repo)
591 const struct got_error *err = NULL;
592 struct got_tree_object *tree1 = NULL, *tree2;
593 struct got_object_qid *qid;
594 char *id_str1 = NULL, *id_str2;
596 err = got_object_open_as_tree(&tree2, repo,
597 got_object_commit_get_tree_id(commit));
601 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
603 struct got_commit_object *pcommit;
605 err = got_object_open_as_commit(&pcommit, repo, qid->id);
609 err = got_object_open_as_tree(&tree1, repo,
610 got_object_commit_get_tree_id(pcommit));
611 got_object_commit_close(pcommit);
615 err = got_object_id_str(&id_str1, qid->id);
620 err = got_object_id_str(&id_str2, id);
624 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
625 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
628 got_object_tree_close(tree1);
629 got_object_tree_close(tree2);
636 get_datestr(time_t *time, char *datebuf)
638 char *p, *s = ctime_r(time, datebuf);
645 static const struct got_error *
646 print_commit(struct got_commit_object *commit, struct got_object_id *id,
647 struct got_repository *repo, int show_patch, int diff_context,
648 struct got_reflist_head *refs)
650 const struct got_error *err = NULL;
651 char *id_str, *datestr, *logmsg0, *logmsg, *line;
653 time_t committer_time;
654 const char *author, *committer;
655 char *refs_str = NULL;
656 struct got_reflist_entry *re;
658 SIMPLEQ_FOREACH(re, refs, entry) {
661 if (got_object_id_cmp(re->id, id) != 0)
663 name = got_ref_get_name(re->ref);
664 if (strcmp(name, GOT_REF_HEAD) == 0)
666 if (strncmp(name, "refs/", 5) == 0)
668 if (strncmp(name, "got/", 4) == 0)
670 if (strncmp(name, "heads/", 6) == 0)
672 if (strncmp(name, "remotes/", 8) == 0)
675 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
677 err = got_error_from_errno();
683 err = got_object_id_str(&id_str, id);
687 printf("-----------------------------------------------\n");
688 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
689 refs_str ? refs_str : "", refs_str ? ")" : "");
694 printf("from: %s\n", got_object_commit_get_author(commit));
695 committer_time = got_object_commit_get_committer_time(commit);
696 datestr = get_datestr(&committer_time, datebuf);
697 printf("date: %s UTC\n", datestr);
698 author = got_object_commit_get_author(commit);
699 committer = got_object_commit_get_committer(commit);
700 if (strcmp(author, committer) != 0)
701 printf("via: %s\n", committer);
702 if (got_object_commit_get_nparents(commit) > 1) {
703 const struct got_object_id_queue *parent_ids;
704 struct got_object_qid *qid;
706 parent_ids = got_object_commit_get_parent_ids(commit);
707 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
708 err = got_object_id_str(&id_str, qid->id);
711 printf("parent %d: %s\n", n++, id_str);
716 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
718 return got_error_from_errno();
722 line = strsep(&logmsg, "\n");
724 printf(" %s\n", line);
729 err = print_patch(commit, id, diff_context, repo);
734 if (fflush(stdout) != 0 && err == NULL)
735 err = got_error_from_errno();
739 static const struct got_error *
740 print_commits(struct got_object_id *root_id, struct got_repository *repo,
741 char *path, int show_patch, int diff_context, int limit,
742 int first_parent_traversal, struct got_reflist_head *refs)
744 const struct got_error *err;
745 struct got_commit_graph *graph;
747 err = got_commit_graph_open(&graph, root_id, path,
748 first_parent_traversal, repo);
751 err = got_commit_graph_iter_start(graph, root_id, repo);
755 struct got_commit_object *commit;
756 struct got_object_id *id;
758 if (sigint_received || sigpipe_received)
761 err = got_commit_graph_iter_next(&id, graph);
763 if (err->code == GOT_ERR_ITER_COMPLETED) {
767 if (err->code != GOT_ERR_ITER_NEED_MORE)
769 err = got_commit_graph_fetch_commits(graph, 1, repo);
778 err = got_object_open_as_commit(&commit, repo, id);
781 err = print_commit(commit, id, repo, show_patch, diff_context,
783 got_object_commit_close(commit);
784 if (err || (limit && --limit == 0))
788 got_commit_graph_close(graph);
795 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
796 "[-r repository-path] [path]\n", getprogname());
800 static const struct got_error *
801 cmd_log(int argc, char *argv[])
803 const struct got_error *error;
804 struct got_repository *repo = NULL;
805 struct got_worktree *worktree = NULL;
806 struct got_commit_object *commit = NULL;
807 struct got_object_id *id = NULL;
808 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
809 char *start_commit = NULL;
810 int diff_context = 3, ch;
811 int show_patch = 0, limit = 0, first_parent_traversal = 0;
813 struct got_reflist_head refs;
818 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
824 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
830 start_commit = optarg;
833 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
836 err(1, "-C option %s", errstr);
839 limit = strtonum(optarg, 1, INT_MAX, &errstr);
841 err(1, "-l option %s", errstr);
844 first_parent_traversal = 1;
847 repo_path = realpath(optarg, NULL);
848 if (repo_path == NULL)
860 cwd = getcwd(NULL, 0);
862 error = got_error_from_errno();
866 error = got_worktree_open(&worktree, cwd);
867 if (error && error->code != GOT_ERR_NOT_WORKTREE)
874 error = got_error_from_errno();
877 } else if (argc == 1) {
879 error = got_worktree_resolve_path(&path, worktree,
884 path = strdup(argv[0]);
886 error = got_error_from_errno();
893 repo_path = worktree ?
894 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
895 if (repo_path == NULL) {
896 error = got_error_from_errno();
900 error = got_repo_open(&repo, repo_path);
904 error = apply_unveil(got_repo_get_path(repo), 1,
905 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
909 if (start_commit == NULL) {
910 struct got_reference *head_ref;
911 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
914 error = got_ref_resolve(&id, repo, head_ref);
915 got_ref_close(head_ref);
918 error = got_object_open_as_commit(&commit, repo, id);
920 struct got_reference *ref;
921 error = got_ref_open(&ref, repo, start_commit);
924 error = got_ref_resolve(&id, repo, ref);
928 error = got_object_get_type(&obj_type, repo, id);
931 if (obj_type == GOT_OBJ_TYPE_TAG) {
932 struct got_tag_object *tag;
933 error = got_object_open_as_tag(&tag, repo, id);
936 if (got_object_tag_get_object_type(tag) !=
937 GOT_OBJ_TYPE_COMMIT) {
938 got_object_tag_close(tag);
939 error = got_error(GOT_ERR_OBJ_TYPE);
943 id = got_object_id_dup(
944 got_object_tag_get_object_id(tag));
946 error = got_error_from_errno();
947 got_object_tag_close(tag);
950 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
951 error = got_error(GOT_ERR_OBJ_TYPE);
954 error = got_object_open_as_commit(&commit, repo, id);
958 if (commit == NULL) {
959 error = got_object_resolve_id_str(&id, repo,
968 error = got_repo_map_path(&in_repo_path, repo, path, 1);
976 error = got_ref_list(&refs, repo);
980 error = print_commits(id, repo, path, show_patch,
981 diff_context, limit, first_parent_traversal, &refs);
988 got_worktree_close(worktree);
990 const struct got_error *repo_error;
991 repo_error = got_repo_close(repo);
995 got_ref_list_free(&refs);
1002 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1003 "[object1 object2 | path]\n", getprogname());
1007 struct print_diff_arg {
1008 struct got_repository *repo;
1009 struct got_worktree *worktree;
1015 static const struct got_error *
1016 print_diff(void *arg, unsigned char status, const char *path,
1017 struct got_object_id *id)
1019 struct print_diff_arg *a = arg;
1020 const struct got_error *err = NULL;
1021 struct got_blob_object *blob1 = NULL;
1023 char *abspath = NULL;
1026 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1027 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1030 if (!a->header_shown) {
1031 printf("diff %s %s\n", a->id_str,
1032 got_worktree_get_root_path(a->worktree));
1033 a->header_shown = 1;
1036 if (status != GOT_STATUS_ADD) {
1037 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1043 if (status != GOT_STATUS_DELETE) {
1044 if (asprintf(&abspath, "%s/%s",
1045 got_worktree_get_root_path(a->worktree), path) == -1) {
1046 err = got_error_from_errno();
1050 f2 = fopen(abspath, "r");
1052 err = got_error_from_errno();
1055 if (lstat(abspath, &sb) == -1) {
1056 err = got_error_from_errno();
1062 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1066 got_object_blob_close(blob1);
1067 if (f2 && fclose(f2) != 0 && err == NULL)
1068 err = got_error_from_errno();
1073 static const struct got_error *
1074 cmd_diff(int argc, char *argv[])
1076 const struct got_error *error;
1077 struct got_repository *repo = NULL;
1078 struct got_worktree *worktree = NULL;
1079 char *cwd = NULL, *repo_path = NULL;
1080 struct got_object_id *id1 = NULL, *id2 = NULL;
1081 char *id_str1 = NULL, *id_str2 = NULL;
1083 int diff_context = 3, ch;
1088 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1093 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1096 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1098 err(1, "-C option %s", errstr);
1101 repo_path = realpath(optarg, NULL);
1102 if (repo_path == NULL)
1103 err(1, "-r option");
1114 cwd = getcwd(NULL, 0);
1116 error = got_error_from_errno();
1119 error = got_worktree_open(&worktree, cwd);
1120 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1123 if (worktree == NULL) {
1124 error = got_error(GOT_ERR_NOT_WORKTREE);
1129 "-r option can't be used when diffing a work tree");
1130 repo_path = strdup(got_worktree_get_repo_path(worktree));
1131 if (repo_path == NULL) {
1132 error = got_error_from_errno();
1136 error = got_worktree_resolve_path(&path, worktree,
1143 error = got_error_from_errno();
1147 } else if (argc == 2) {
1153 if (repo_path == NULL) {
1154 repo_path = getcwd(NULL, 0);
1155 if (repo_path == NULL)
1156 return got_error_from_errno();
1159 error = got_repo_open(&repo, repo_path);
1164 error = apply_unveil(got_repo_get_path(repo), 1,
1165 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1170 struct print_diff_arg arg;
1172 error = got_object_id_str(&id_str,
1173 got_worktree_get_base_commit_id(worktree));
1177 arg.worktree = worktree;
1178 arg.diff_context = diff_context;
1179 arg.id_str = id_str;
1180 arg.header_shown = 0;
1182 error = got_worktree_status(worktree, path, repo, print_diff,
1183 &arg, check_cancelled, NULL);
1188 error = got_object_resolve_id_str(&id1, repo, id_str1);
1192 error = got_object_resolve_id_str(&id2, repo, id_str2);
1196 error = got_object_get_type(&type1, repo, id1);
1200 error = got_object_get_type(&type2, repo, id2);
1204 if (type1 != type2) {
1205 error = got_error(GOT_ERR_OBJ_TYPE);
1210 case GOT_OBJ_TYPE_BLOB:
1211 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1212 diff_context, repo, stdout);
1214 case GOT_OBJ_TYPE_TREE:
1215 error = got_diff_objects_as_trees(id1, id2, "", "",
1216 diff_context, repo, stdout);
1218 case GOT_OBJ_TYPE_COMMIT:
1219 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1221 error = got_diff_objects_as_commits(id1, id2, diff_context,
1225 error = got_error(GOT_ERR_OBJ_TYPE);
1233 got_worktree_close(worktree);
1235 const struct got_error *repo_error;
1236 repo_error = got_repo_close(repo);
1247 "usage: %s blame [-c commit] [-r repository-path] path\n",
1252 static const struct got_error *
1253 cmd_blame(int argc, char *argv[])
1255 const struct got_error *error;
1256 struct got_repository *repo = NULL;
1257 struct got_worktree *worktree = NULL;
1258 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1259 struct got_object_id *commit_id = NULL;
1260 char *commit_id_str = NULL;
1264 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1269 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1272 commit_id_str = optarg;
1275 repo_path = realpath(optarg, NULL);
1276 if (repo_path == NULL)
1277 err(1, "-r option");
1293 cwd = getcwd(NULL, 0);
1295 error = got_error_from_errno();
1298 if (repo_path == NULL) {
1299 error = got_worktree_open(&worktree, cwd);
1300 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1306 strdup(got_worktree_get_repo_path(worktree));
1307 if (repo_path == NULL)
1308 error = got_error_from_errno();
1312 repo_path = strdup(cwd);
1313 if (repo_path == NULL) {
1314 error = got_error_from_errno();
1320 error = got_repo_open(&repo, repo_path);
1324 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1329 const char *prefix = got_worktree_get_path_prefix(worktree);
1330 char *p, *worktree_subdir = cwd +
1331 strlen(got_worktree_get_root_path(worktree));
1332 if (asprintf(&p, "%s%s%s%s%s",
1333 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1334 worktree_subdir, worktree_subdir[0] ? "/" : "",
1336 error = got_error_from_errno();
1339 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1342 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1347 if (commit_id_str == NULL) {
1348 struct got_reference *head_ref;
1349 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1352 error = got_ref_resolve(&commit_id, repo, head_ref);
1353 got_ref_close(head_ref);
1357 error = got_object_resolve_id_str(&commit_id, repo,
1363 error = got_blame(in_repo_path, commit_id, repo, stdout);
1370 got_worktree_close(worktree);
1372 const struct got_error *repo_error;
1373 repo_error = got_repo_close(repo);
1384 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1390 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1391 const char *root_path)
1393 int is_root_path = (strcmp(path, root_path) == 0);
1395 path += strlen(root_path);
1396 while (path[0] == '/')
1399 printf("%s%s%s%s%s\n", id ? id : "", path,
1400 is_root_path ? "" : "/", te->name,
1401 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1404 static const struct got_error *
1405 print_tree(const char *path, struct got_object_id *commit_id,
1406 int show_ids, int recurse, const char *root_path,
1407 struct got_repository *repo)
1409 const struct got_error *err = NULL;
1410 struct got_object_id *tree_id = NULL;
1411 struct got_tree_object *tree = NULL;
1412 const struct got_tree_entries *entries;
1413 struct got_tree_entry *te;
1415 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1419 err = got_object_open_as_tree(&tree, repo, tree_id);
1422 entries = got_object_tree_get_entries(tree);
1423 te = SIMPLEQ_FIRST(&entries->head);
1427 if (sigint_received || sigpipe_received)
1432 err = got_object_id_str(&id_str, te->id);
1435 if (asprintf(&id, "%s ", id_str) == -1) {
1436 err = got_error_from_errno();
1442 print_entry(te, id, path, root_path);
1445 if (recurse && S_ISDIR(te->mode)) {
1447 if (asprintf(&child_path, "%s%s%s", path,
1448 path[0] == '/' && path[1] == '\0' ? "" : "/",
1450 err = got_error_from_errno();
1453 err = print_tree(child_path, commit_id, show_ids, 1,
1460 te = SIMPLEQ_NEXT(te, entry);
1464 got_object_tree_close(tree);
1469 static const struct got_error *
1470 cmd_tree(int argc, char *argv[])
1472 const struct got_error *error;
1473 struct got_repository *repo = NULL;
1474 struct got_worktree *worktree = NULL;
1476 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1477 struct got_object_id *commit_id = NULL;
1478 char *commit_id_str = NULL;
1479 int show_ids = 0, recurse = 0;
1483 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1488 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1491 commit_id_str = optarg;
1494 repo_path = realpath(optarg, NULL);
1495 if (repo_path == NULL)
1496 err(1, "-r option");
1520 cwd = getcwd(NULL, 0);
1522 error = got_error_from_errno();
1525 if (repo_path == NULL) {
1526 error = got_worktree_open(&worktree, cwd);
1527 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1533 strdup(got_worktree_get_repo_path(worktree));
1534 if (repo_path == NULL)
1535 error = got_error_from_errno();
1539 repo_path = strdup(cwd);
1540 if (repo_path == NULL) {
1541 error = got_error_from_errno();
1547 error = got_repo_open(&repo, repo_path);
1551 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1557 char *p, *worktree_subdir = cwd +
1558 strlen(got_worktree_get_root_path(worktree));
1559 if (asprintf(&p, "%s/%s",
1560 got_worktree_get_path_prefix(worktree),
1561 worktree_subdir) == -1) {
1562 error = got_error_from_errno();
1565 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1572 if (in_repo_path == NULL) {
1573 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1578 if (commit_id_str == NULL) {
1579 struct got_reference *head_ref;
1580 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1583 error = got_ref_resolve(&commit_id, repo, head_ref);
1584 got_ref_close(head_ref);
1588 error = got_object_resolve_id_str(&commit_id, repo,
1594 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1595 in_repo_path, repo);
1602 got_worktree_close(worktree);
1604 const struct got_error *repo_error;
1605 repo_error = got_repo_close(repo);
1615 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1619 static const struct got_error *
1620 print_status(void *arg, unsigned char status, const char *path,
1621 struct got_object_id *id)
1623 printf("%c %s\n", status, path);
1627 static const struct got_error *
1628 cmd_status(int argc, char *argv[])
1630 const struct got_error *error = NULL;
1631 struct got_repository *repo = NULL;
1632 struct got_worktree *worktree = NULL;
1633 char *cwd = NULL, *path = NULL;
1636 while ((ch = getopt(argc, argv, "")) != -1) {
1648 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1652 cwd = getcwd(NULL, 0);
1654 error = got_error_from_errno();
1658 error = got_worktree_open(&worktree, cwd);
1665 error = got_error_from_errno();
1668 } else if (argc == 1) {
1669 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1675 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1679 error = apply_unveil(got_repo_get_path(repo), 1,
1680 got_worktree_get_root_path(worktree), 0);
1684 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1685 check_cancelled, NULL);
1696 "usage: %s ref [-r repository] -l | -d name | name object\n",
1701 static const struct got_error *
1702 list_refs(struct got_repository *repo)
1704 static const struct got_error *err = NULL;
1705 struct got_reflist_head refs;
1706 struct got_reflist_entry *re;
1708 SIMPLEQ_INIT(&refs);
1709 err = got_ref_list(&refs, repo);
1713 SIMPLEQ_FOREACH(re, &refs, entry) {
1715 refstr = got_ref_to_str(re->ref);
1717 return got_error_from_errno();
1718 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1722 got_ref_list_free(&refs);
1726 static const struct got_error *
1727 delete_ref(struct got_repository *repo, const char *refname)
1729 const struct got_error *err = NULL;
1730 struct got_reference *ref;
1732 err = got_ref_open(&ref, repo, refname);
1736 err = got_ref_delete(ref, repo);
1741 static const struct got_error *
1742 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1744 const struct got_error *err = NULL;
1745 struct got_object_id *id;
1746 struct got_reference *ref = NULL;
1748 err = got_object_resolve_id_str(&id, repo, id_str);
1752 err = got_ref_alloc(&ref, refname, id);
1756 err = got_ref_write(ref, repo);
1764 static const struct got_error *
1765 cmd_ref(int argc, char *argv[])
1767 const struct got_error *error = NULL;
1768 struct got_repository *repo = NULL;
1769 struct got_worktree *worktree = NULL;
1770 char *cwd = NULL, *repo_path = NULL;
1771 int ch, do_list = 0;
1772 const char *delref = NULL;
1774 /* TODO: Add -s option for adding symbolic references. */
1775 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1781 repo_path = realpath(optarg, NULL);
1782 if (repo_path == NULL)
1783 err(1, "-r option");
1794 if (do_list && delref)
1795 errx(1, "-l and -d options are mutually exclusive\n");
1800 if (do_list || delref) {
1803 } else if (argc != 2)
1808 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1812 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1813 "sendfd unveil", NULL) == -1)
1817 cwd = getcwd(NULL, 0);
1819 error = got_error_from_errno();
1823 if (repo_path == NULL) {
1824 error = got_worktree_open(&worktree, cwd);
1825 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1831 strdup(got_worktree_get_repo_path(worktree));
1832 if (repo_path == NULL)
1833 error = got_error_from_errno();
1837 repo_path = strdup(cwd);
1838 if (repo_path == NULL) {
1839 error = got_error_from_errno();
1845 error = got_repo_open(&repo, repo_path);
1849 error = apply_unveil(got_repo_get_path(repo), do_list,
1850 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1855 error = list_refs(repo);
1857 error = delete_ref(repo, delref);
1859 error = add_ref(repo, argv[0], argv[1]);
1862 got_repo_close(repo);
1864 got_worktree_close(worktree);
1873 fprintf(stderr, "usage: %s add file-path\n", getprogname());
1877 static const struct got_error *
1878 cmd_add(int argc, char *argv[])
1880 const struct got_error *error = NULL;
1881 struct got_repository *repo = NULL;
1882 struct got_worktree *worktree = NULL;
1883 char *cwd = NULL, *path = NULL, *relpath = NULL;
1886 while ((ch = getopt(argc, argv, "")) != -1) {
1900 path = realpath(argv[0], NULL);
1902 error = got_error_from_errno();
1906 cwd = getcwd(NULL, 0);
1908 error = got_error_from_errno();
1911 error = got_worktree_open(&worktree, cwd);
1915 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1919 error = apply_unveil(got_repo_get_path(repo), 1,
1920 got_worktree_get_root_path(worktree), 0);
1924 error = got_worktree_schedule_add(worktree, path, print_status, NULL,
1930 got_repo_close(repo);
1932 got_worktree_close(worktree);
1942 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1946 static const struct got_error *
1947 cmd_rm(int argc, char *argv[])
1949 const struct got_error *error = NULL;
1950 struct got_worktree *worktree = NULL;
1951 struct got_repository *repo = NULL;
1952 char *cwd = NULL, *path = NULL;
1953 int ch, delete_local_mods = 0;
1955 while ((ch = getopt(argc, argv, "f")) != -1) {
1958 delete_local_mods = 1;
1972 path = realpath(argv[0], NULL);
1974 error = got_error_from_errno();
1978 cwd = getcwd(NULL, 0);
1980 error = got_error_from_errno();
1983 error = got_worktree_open(&worktree, cwd);
1987 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1991 error = apply_unveil(got_repo_get_path(repo), 1,
1992 got_worktree_get_root_path(worktree), 0);
1996 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
1997 print_status, NULL, repo);
2002 got_repo_close(repo);
2004 got_worktree_close(worktree);
2013 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2018 revert_progress(void *arg, unsigned char status, const char *path)
2020 while (path[0] == '/')
2022 printf("%c %s\n", status, path);
2025 static const struct got_error *
2026 cmd_revert(int argc, char *argv[])
2028 const struct got_error *error = NULL;
2029 struct got_worktree *worktree = NULL;
2030 struct got_repository *repo = NULL;
2031 char *cwd = NULL, *path = NULL;
2034 while ((ch = getopt(argc, argv, "")) != -1) {
2048 path = realpath(argv[0], NULL);
2050 error = got_error_from_errno();
2054 cwd = getcwd(NULL, 0);
2056 error = got_error_from_errno();
2059 error = got_worktree_open(&worktree, cwd);
2063 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2067 error = apply_unveil(got_repo_get_path(repo), 1,
2068 got_worktree_get_root_path(worktree), 0);
2072 error = got_worktree_revert(worktree, path,
2073 revert_progress, NULL, repo);
2078 got_repo_close(repo);
2080 got_worktree_close(worktree);
2089 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2093 static const struct got_error *
2094 cmd_commit(int argc, char *argv[])
2096 const struct got_error *error = NULL;
2097 struct got_worktree *worktree = NULL;
2098 struct got_repository *repo = NULL;
2099 char *cwd = NULL, *path = NULL, *id_str = NULL;
2100 struct got_object_id *id = NULL;
2101 const char *logmsg = "<no log message was specified>";
2102 const char *got_author = getenv("GOT_AUTHOR");
2105 while ((ch = getopt(argc, argv, "m:")) != -1) {
2120 path = realpath(argv[0], NULL);
2122 error = got_error_from_errno();
2125 } else if (argc != 0)
2128 if (got_author == NULL) {
2129 /* TODO: Look current user up in password database */
2130 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2134 cwd = getcwd(NULL, 0);
2136 error = got_error_from_errno();
2139 error = got_worktree_open(&worktree, cwd);
2143 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2147 error = apply_unveil(got_repo_get_path(repo), 0,
2148 got_worktree_get_root_path(worktree), 0);
2152 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2153 logmsg, print_status, NULL, repo);
2157 error = got_object_id_str(&id_str, id);
2160 printf("created commit %s\n", id_str);
2163 got_repo_close(repo);
2165 got_worktree_close(worktree);