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"
45 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
48 static volatile sig_atomic_t sigint_received;
49 static volatile sig_atomic_t sigpipe_received;
52 catch_sigint(int signo)
58 catch_sigpipe(int signo)
66 const struct got_error *(*cmd_main)(int, char *[]);
67 void (*cmd_usage)(void);
68 const char *cmd_descr;
71 __dead static void usage(void);
72 __dead static void usage_checkout(void);
73 __dead static void usage_update(void);
74 __dead static void usage_log(void);
75 __dead static void usage_diff(void);
76 __dead static void usage_blame(void);
77 __dead static void usage_tree(void);
78 __dead static void usage_status(void);
79 __dead static void usage_ref(void);
80 __dead static void usage_add(void);
82 static const struct got_error* cmd_checkout(int, char *[]);
83 static const struct got_error* cmd_update(int, char *[]);
84 static const struct got_error* cmd_log(int, char *[]);
85 static const struct got_error* cmd_diff(int, char *[]);
86 static const struct got_error* cmd_blame(int, char *[]);
87 static const struct got_error* cmd_tree(int, char *[]);
88 static const struct got_error* cmd_status(int, char *[]);
89 static const struct got_error* cmd_ref(int, char *[]);
90 static const struct got_error* cmd_add(int, char *[]);
92 static struct cmd got_commands[] = {
93 { "checkout", cmd_checkout, usage_checkout,
94 "check out a new work tree from a repository" },
95 { "update", cmd_update, usage_update,
96 "update a work tree to a different commit" },
97 { "log", cmd_log, usage_log,
98 "show repository history" },
99 { "diff", cmd_diff, usage_diff,
100 "compare files and directories" },
101 { "blame", cmd_blame, usage_blame,
102 " show when lines in a file were changed" },
103 { "tree", cmd_tree, usage_tree,
104 " list files and directories in repository" },
105 { "status", cmd_status, usage_status,
106 "show modification status of files" },
107 { "ref", cmd_ref, usage_ref,
108 "manage references in repository" },
109 { "add", cmd_add, usage_add,
110 "add a new file to version control" },
114 main(int argc, char *argv[])
121 setlocale(LC_CTYPE, "");
123 while ((ch = getopt(argc, argv, "h")) != -1) {
141 signal(SIGINT, catch_sigint);
142 signal(SIGPIPE, catch_sigpipe);
144 for (i = 0; i < nitems(got_commands); i++) {
145 const struct got_error *error;
147 cmd = &got_commands[i];
149 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
153 got_commands[i].cmd_usage();
155 error = got_commands[i].cmd_main(argc, argv);
156 if (error && !(sigint_received || sigpipe_received)) {
157 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
164 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
173 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
174 "Available commands:\n", getprogname());
175 for (i = 0; i < nitems(got_commands); i++) {
176 struct cmd *cmd = &got_commands[i];
177 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
182 static const struct got_error *
183 apply_unveil(const char *repo_path, int repo_read_only,
184 const char *worktree_path)
186 const struct got_error *error;
188 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
189 return got_error_from_errno();
191 if (worktree_path && unveil(worktree_path, "rwc") != 0)
192 return got_error_from_errno();
194 if (unveil("/tmp", "rwc") != 0)
195 return got_error_from_errno();
197 error = got_privsep_unveil_exec_helpers();
201 if (unveil(NULL, NULL) != 0)
202 return got_error_from_errno();
210 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
211 "[worktree-path]\n", getprogname());
216 checkout_progress(void *arg, unsigned char status, const char *path)
218 char *worktree_path = arg;
220 while (path[0] == '/')
223 printf("%c %s/%s\n", status, worktree_path, path);
226 static const struct got_error *
227 check_cancelled(void *arg)
229 if (sigint_received || sigpipe_received)
230 return got_error(GOT_ERR_CANCELLED);
234 static const struct got_error *
235 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
236 struct got_repository *repo)
238 const struct got_error *err;
239 struct got_reference *head_ref = NULL;
240 struct got_object_id *head_commit_id = NULL;
241 struct got_commit_graph *graph = NULL;
243 head_ref = got_worktree_get_head_ref(worktree);
244 if (head_ref == NULL)
245 return got_error_from_errno();
247 /* TODO: Check the reflog. The head ref may have been rebased. */
248 err = got_ref_resolve(&head_commit_id, repo, head_ref);
252 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
256 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
260 struct got_object_id *id;
262 if (sigint_received || sigpipe_received)
265 err = got_commit_graph_iter_next(&id, graph);
267 if (err->code == GOT_ERR_ITER_COMPLETED) {
268 err = got_error(GOT_ERR_ANCESTRY);
271 if (err->code != GOT_ERR_ITER_NEED_MORE)
273 err = got_commit_graph_fetch_commits(graph, 1, repo);
281 if (got_object_id_cmp(id, commit_id) == 0)
286 got_ref_close(head_ref);
288 got_commit_graph_close(graph);
293 static const struct got_error *
294 cmd_checkout(int argc, char *argv[])
296 const struct got_error *error = NULL;
297 struct got_repository *repo = NULL;
298 struct got_reference *head_ref = NULL;
299 struct got_worktree *worktree = NULL;
300 char *repo_path = NULL;
301 char *worktree_path = NULL;
302 const char *path_prefix = "";
303 char *commit_id_str = NULL;
304 int ch, same_path_prefix;
306 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
309 commit_id_str = strdup(optarg);
310 if (commit_id_str == NULL)
311 return got_error_from_errno();
314 path_prefix = optarg;
326 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
327 "unveil", NULL) == -1)
331 char *cwd, *base, *dotgit;
332 repo_path = realpath(argv[0], NULL);
333 if (repo_path == NULL)
334 return got_error_from_errno();
335 cwd = getcwd(NULL, 0);
337 error = got_error_from_errno();
341 base = basename(path_prefix);
343 base = basename(repo_path);
345 error = got_error_from_errno();
348 dotgit = strstr(base, ".git");
351 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
352 error = got_error_from_errno();
357 } else if (argc == 2) {
358 repo_path = realpath(argv[0], NULL);
359 if (repo_path == NULL) {
360 error = got_error_from_errno();
363 worktree_path = realpath(argv[1], NULL);
364 if (worktree_path == NULL) {
365 error = got_error_from_errno();
371 error = apply_unveil(repo_path, 0, worktree_path);
375 error = got_repo_open(&repo, repo_path);
379 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
383 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
384 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
387 error = got_worktree_open(&worktree, worktree_path);
391 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
395 if (!same_path_prefix) {
396 error = got_error(GOT_ERR_PATH_PREFIX);
401 struct got_object_id *commit_id;
402 error = got_object_resolve_id_str(&commit_id, repo,
406 error = check_ancestry(worktree, commit_id, repo);
411 error = got_worktree_set_base_commit_id(worktree, repo,
418 error = got_worktree_checkout_files(worktree, repo,
419 checkout_progress, worktree_path, check_cancelled, NULL);
423 printf("Now shut up and hack\n");
435 fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
441 update_progress(void *arg, unsigned char status, const char *path)
443 int *did_something = arg;
445 if (status == GOT_STATUS_EXISTS)
449 while (path[0] == '/')
451 printf("%c %s\n", status, path);
454 static const struct got_error *
455 cmd_update(int argc, char *argv[])
457 const struct got_error *error = NULL;
458 struct got_repository *repo = NULL;
459 struct got_worktree *worktree = NULL;
460 char *worktree_path = NULL;
461 struct got_object_id *commit_id = NULL;
462 char *commit_id_str = NULL;
463 int ch, did_something = 0;
465 while ((ch = getopt(argc, argv, "c:")) != -1) {
468 commit_id_str = strdup(optarg);
469 if (commit_id_str == NULL)
470 return got_error_from_errno();
482 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
483 "unveil", NULL) == -1)
487 worktree_path = getcwd(NULL, 0);
488 if (worktree_path == NULL) {
489 error = got_error_from_errno();
492 } else if (argc == 1) {
493 worktree_path = realpath(argv[0], NULL);
494 if (worktree_path == NULL) {
495 error = got_error_from_errno();
501 error = got_worktree_open(&worktree, worktree_path);
505 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
509 error = apply_unveil(got_repo_get_path(repo), 0,
510 got_worktree_get_root_path(worktree));
514 if (commit_id_str == NULL) {
515 struct got_reference *head_ref;
516 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
519 error = got_ref_resolve(&commit_id, repo, head_ref);
522 error = got_object_id_str(&commit_id_str, commit_id);
526 error = got_object_resolve_id_str(&commit_id, repo,
532 error = check_ancestry(worktree, commit_id, repo);
536 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
538 error = got_worktree_set_base_commit_id(worktree, repo,
544 error = got_worktree_checkout_files(worktree, repo,
545 update_progress, &did_something, check_cancelled, NULL);
550 printf("Updated to commit %s\n", commit_id_str);
552 printf("Already up-to-date\n");
560 static const struct got_error *
561 print_patch(struct got_commit_object *commit, struct got_object_id *id,
562 int diff_context, struct got_repository *repo)
564 const struct got_error *err = NULL;
565 struct got_tree_object *tree1 = NULL, *tree2;
566 struct got_object_qid *qid;
567 char *id_str1 = NULL, *id_str2;
569 err = got_object_open_as_tree(&tree2, repo,
570 got_object_commit_get_tree_id(commit));
574 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
576 struct got_commit_object *pcommit;
578 err = got_object_open_as_commit(&pcommit, repo, qid->id);
582 err = got_object_open_as_tree(&tree1, repo,
583 got_object_commit_get_tree_id(pcommit));
584 got_object_commit_close(pcommit);
588 err = got_object_id_str(&id_str1, qid->id);
593 err = got_object_id_str(&id_str2, id);
597 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
598 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
601 got_object_tree_close(tree1);
602 got_object_tree_close(tree2);
609 get_datestr(time_t *time, char *datebuf)
611 char *p, *s = ctime_r(time, datebuf);
618 static const struct got_error *
619 print_commit(struct got_commit_object *commit, struct got_object_id *id,
620 struct got_repository *repo, int show_patch, int diff_context,
621 struct got_reflist_head *refs)
623 const struct got_error *err = NULL;
624 char *id_str, *datestr, *logmsg0, *logmsg, *line;
626 time_t committer_time;
627 const char *author, *committer;
628 char *refs_str = NULL;
629 struct got_reflist_entry *re;
631 SIMPLEQ_FOREACH(re, refs, entry) {
634 if (got_object_id_cmp(re->id, id) != 0)
636 name = got_ref_get_name(re->ref);
637 if (strcmp(name, GOT_REF_HEAD) == 0)
639 if (strncmp(name, "refs/", 5) == 0)
641 if (strncmp(name, "got/", 4) == 0)
643 if (strncmp(name, "heads/", 6) == 0)
645 if (strncmp(name, "remotes/", 8) == 0)
648 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
650 err = got_error_from_errno();
656 err = got_object_id_str(&id_str, id);
660 printf("-----------------------------------------------\n");
661 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
662 refs_str ? refs_str : "", refs_str ? ")" : "");
667 printf("from: %s\n", got_object_commit_get_author(commit));
668 committer_time = got_object_commit_get_committer_time(commit);
669 datestr = get_datestr(&committer_time, datebuf);
670 printf("date: %s UTC\n", datestr);
671 author = got_object_commit_get_author(commit);
672 committer = got_object_commit_get_committer(commit);
673 if (strcmp(author, committer) != 0)
674 printf("via: %s\n", committer);
675 if (got_object_commit_get_nparents(commit) > 1) {
676 const struct got_object_id_queue *parent_ids;
677 struct got_object_qid *qid;
679 parent_ids = got_object_commit_get_parent_ids(commit);
680 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
681 err = got_object_id_str(&id_str, qid->id);
684 printf("parent %d: %s\n", n++, id_str);
689 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
691 return got_error_from_errno();
695 line = strsep(&logmsg, "\n");
697 printf(" %s\n", line);
702 err = print_patch(commit, id, diff_context, repo);
707 if (fflush(stdout) != 0 && err == NULL)
708 err = got_error_from_errno();
712 static const struct got_error *
713 print_commits(struct got_object_id *root_id, struct got_repository *repo,
714 char *path, int show_patch, int diff_context, int limit,
715 int first_parent_traversal, struct got_reflist_head *refs)
717 const struct got_error *err;
718 struct got_commit_graph *graph;
720 err = got_commit_graph_open(&graph, root_id, path,
721 first_parent_traversal, repo);
724 err = got_commit_graph_iter_start(graph, root_id, repo);
728 struct got_commit_object *commit;
729 struct got_object_id *id;
731 if (sigint_received || sigpipe_received)
734 err = got_commit_graph_iter_next(&id, graph);
736 if (err->code == GOT_ERR_ITER_COMPLETED) {
740 if (err->code != GOT_ERR_ITER_NEED_MORE)
742 err = got_commit_graph_fetch_commits(graph, 1, repo);
751 err = got_object_open_as_commit(&commit, repo, id);
754 err = print_commit(commit, id, repo, show_patch, diff_context,
756 got_object_commit_close(commit);
757 if (err || (limit && --limit == 0))
761 got_commit_graph_close(graph);
768 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
769 "[-r repository-path] [path]\n", getprogname());
773 static const struct got_error *
774 cmd_log(int argc, char *argv[])
776 const struct got_error *error;
777 struct got_repository *repo = NULL;
778 struct got_worktree *worktree = NULL;
779 struct got_commit_object *commit = NULL;
780 struct got_object_id *id = NULL;
781 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
782 char *start_commit = NULL;
783 int diff_context = 3, ch;
784 int show_patch = 0, limit = 0, first_parent_traversal = 0;
786 struct got_reflist_head refs;
791 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
797 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
803 start_commit = optarg;
806 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
809 err(1, "-C option %s", errstr);
812 limit = strtonum(optarg, 1, INT_MAX, &errstr);
814 err(1, "-l option %s", errstr);
817 first_parent_traversal = 1;
820 repo_path = realpath(optarg, NULL);
821 if (repo_path == NULL)
833 cwd = getcwd(NULL, 0);
835 error = got_error_from_errno();
839 error = got_worktree_open(&worktree, cwd);
840 if (error && error->code != GOT_ERR_NOT_WORKTREE)
847 error = got_error_from_errno();
850 } else if (argc == 1) {
852 error = got_worktree_resolve_path(&path, worktree,
857 path = strdup(argv[0]);
859 error = got_error_from_errno();
866 repo_path = worktree ?
867 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
868 if (repo_path == NULL) {
869 error = got_error_from_errno();
873 error = apply_unveil(repo_path, 1,
874 worktree ? got_worktree_get_root_path(worktree) : NULL);
878 error = got_repo_open(&repo, repo_path);
882 if (start_commit == NULL) {
883 struct got_reference *head_ref;
884 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
887 error = got_ref_resolve(&id, repo, head_ref);
888 got_ref_close(head_ref);
891 error = got_object_open_as_commit(&commit, repo, id);
893 struct got_reference *ref;
894 error = got_ref_open(&ref, repo, start_commit);
897 error = got_ref_resolve(&id, repo, ref);
901 error = got_object_get_type(&obj_type, repo, id);
904 if (obj_type == GOT_OBJ_TYPE_TAG) {
905 struct got_tag_object *tag;
906 error = got_object_open_as_tag(&tag, repo, id);
909 if (got_object_tag_get_object_type(tag) !=
910 GOT_OBJ_TYPE_COMMIT) {
911 got_object_tag_close(tag);
912 error = got_error(GOT_ERR_OBJ_TYPE);
916 id = got_object_id_dup(
917 got_object_tag_get_object_id(tag));
919 error = got_error_from_errno();
920 got_object_tag_close(tag);
923 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
924 error = got_error(GOT_ERR_OBJ_TYPE);
927 error = got_object_open_as_commit(&commit, repo, id);
931 if (commit == NULL) {
932 error = got_object_resolve_id_str(&id, repo,
941 error = got_repo_map_path(&in_repo_path, repo, path, 1);
949 error = got_ref_list(&refs, repo);
953 error = print_commits(id, repo, path, show_patch,
954 diff_context, limit, first_parent_traversal, &refs);
961 got_worktree_close(worktree);
963 const struct got_error *repo_error;
964 repo_error = got_repo_close(repo);
968 got_ref_list_free(&refs);
975 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
976 "[object1 object2 | path]\n", getprogname());
980 struct print_diff_arg {
981 struct got_repository *repo;
982 struct got_worktree *worktree;
988 static const struct got_error *
989 print_diff(void *arg, unsigned char status, const char *path,
990 struct got_object_id *id)
992 struct print_diff_arg *a = arg;
993 const struct got_error *err = NULL;
994 struct got_blob_object *blob1 = NULL;
996 char *abspath = NULL;
999 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD)
1002 if (!a->header_shown) {
1003 printf("diff %s %s\n", a->id_str,
1004 got_worktree_get_root_path(a->worktree));
1005 a->header_shown = 1;
1008 if (status == GOT_STATUS_MODIFY) {
1009 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1015 if (asprintf(&abspath, "%s/%s",
1016 got_worktree_get_root_path(a->worktree), path) == -1) {
1017 err = got_error_from_errno();
1021 f2 = fopen(abspath, "r");
1023 err = got_error_from_errno();
1026 if (lstat(abspath, &sb) == -1) {
1027 err = got_error_from_errno();
1031 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1035 got_object_blob_close(blob1);
1036 if (f2 && fclose(f2) != 0 && err == NULL)
1037 err = got_error_from_errno();
1042 static const struct got_error *
1043 cmd_diff(int argc, char *argv[])
1045 const struct got_error *error;
1046 struct got_repository *repo = NULL;
1047 struct got_worktree *worktree = NULL;
1048 char *cwd = NULL, *repo_path = NULL;
1049 struct got_object_id *id1 = NULL, *id2 = NULL;
1050 char *id_str1 = NULL, *id_str2 = NULL;
1052 int diff_context = 3, ch;
1057 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1062 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1065 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1067 err(1, "-C option %s", errstr);
1070 repo_path = realpath(optarg, NULL);
1071 if (repo_path == NULL)
1072 err(1, "-r option");
1083 cwd = getcwd(NULL, 0);
1085 error = got_error_from_errno();
1088 error = got_worktree_open(&worktree, cwd);
1089 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1092 if (worktree == NULL) {
1093 error = got_error(GOT_ERR_NOT_WORKTREE);
1098 "-r option can't be used when diffing a work tree");
1099 repo_path = strdup(got_worktree_get_repo_path(worktree));
1100 if (repo_path == NULL) {
1101 error = got_error_from_errno();
1105 error = got_worktree_resolve_path(&path, worktree,
1112 error = got_error_from_errno();
1116 } else if (argc == 2) {
1122 if (repo_path == NULL) {
1123 repo_path = getcwd(NULL, 0);
1124 if (repo_path == NULL)
1125 return got_error_from_errno();
1128 error = apply_unveil(repo_path, 1,
1129 worktree ? got_worktree_get_root_path(worktree) : NULL);
1133 error = got_repo_open(&repo, repo_path);
1139 struct print_diff_arg arg;
1141 error = got_object_id_str(&id_str,
1142 got_worktree_get_base_commit_id(worktree));
1146 arg.worktree = worktree;
1147 arg.diff_context = diff_context;
1148 arg.id_str = id_str;
1149 arg.header_shown = 0;
1151 error = got_worktree_status(worktree, path, repo, print_diff,
1152 &arg, check_cancelled, NULL);
1157 error = got_object_resolve_id_str(&id1, repo, id_str1);
1161 error = got_object_resolve_id_str(&id2, repo, id_str2);
1165 error = got_object_get_type(&type1, repo, id1);
1169 error = got_object_get_type(&type2, repo, id2);
1173 if (type1 != type2) {
1174 error = got_error(GOT_ERR_OBJ_TYPE);
1179 case GOT_OBJ_TYPE_BLOB:
1180 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1181 diff_context, repo, stdout);
1183 case GOT_OBJ_TYPE_TREE:
1184 error = got_diff_objects_as_trees(id1, id2, "", "",
1185 diff_context, repo, stdout);
1187 case GOT_OBJ_TYPE_COMMIT:
1188 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1190 error = got_diff_objects_as_commits(id1, id2, diff_context,
1194 error = got_error(GOT_ERR_OBJ_TYPE);
1202 got_worktree_close(worktree);
1204 const struct got_error *repo_error;
1205 repo_error = got_repo_close(repo);
1216 "usage: %s blame [-c commit] [-r repository-path] path\n",
1221 static const struct got_error *
1222 cmd_blame(int argc, char *argv[])
1224 const struct got_error *error;
1225 struct got_repository *repo = NULL;
1226 struct got_worktree *worktree = NULL;
1227 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1228 struct got_object_id *commit_id = NULL;
1229 char *commit_id_str = NULL;
1233 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1238 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1241 commit_id_str = optarg;
1244 repo_path = realpath(optarg, NULL);
1245 if (repo_path == NULL)
1246 err(1, "-r option");
1262 cwd = getcwd(NULL, 0);
1264 error = got_error_from_errno();
1267 if (repo_path == NULL) {
1268 error = got_worktree_open(&worktree, cwd);
1269 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1275 strdup(got_worktree_get_repo_path(worktree));
1276 if (repo_path == NULL)
1277 error = got_error_from_errno();
1281 repo_path = strdup(cwd);
1282 if (repo_path == NULL) {
1283 error = got_error_from_errno();
1289 error = apply_unveil(repo_path, 1, NULL);
1293 error = got_repo_open(&repo, repo_path);
1298 const char *prefix = got_worktree_get_path_prefix(worktree);
1299 char *p, *worktree_subdir = cwd +
1300 strlen(got_worktree_get_root_path(worktree));
1301 if (asprintf(&p, "%s%s%s%s%s",
1302 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1303 worktree_subdir, worktree_subdir[0] ? "/" : "",
1305 error = got_error_from_errno();
1308 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1311 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1316 if (commit_id_str == NULL) {
1317 struct got_reference *head_ref;
1318 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1321 error = got_ref_resolve(&commit_id, repo, head_ref);
1322 got_ref_close(head_ref);
1326 error = got_object_resolve_id_str(&commit_id, repo,
1332 error = got_blame(in_repo_path, commit_id, repo, stdout);
1339 got_worktree_close(worktree);
1341 const struct got_error *repo_error;
1342 repo_error = got_repo_close(repo);
1353 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1359 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1360 const char *root_path)
1362 int is_root_path = (strcmp(path, root_path) == 0);
1364 path += strlen(root_path);
1365 while (path[0] == '/')
1368 printf("%s%s%s%s%s\n", id ? id : "", path,
1369 is_root_path ? "" : "/", te->name,
1370 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1373 static const struct got_error *
1374 print_tree(const char *path, struct got_object_id *commit_id,
1375 int show_ids, int recurse, const char *root_path,
1376 struct got_repository *repo)
1378 const struct got_error *err = NULL;
1379 struct got_object_id *tree_id = NULL;
1380 struct got_tree_object *tree = NULL;
1381 const struct got_tree_entries *entries;
1382 struct got_tree_entry *te;
1384 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1388 err = got_object_open_as_tree(&tree, repo, tree_id);
1391 entries = got_object_tree_get_entries(tree);
1392 te = SIMPLEQ_FIRST(&entries->head);
1396 if (sigint_received || sigpipe_received)
1401 err = got_object_id_str(&id_str, te->id);
1404 if (asprintf(&id, "%s ", id_str) == -1) {
1405 err = got_error_from_errno();
1411 print_entry(te, id, path, root_path);
1414 if (recurse && S_ISDIR(te->mode)) {
1416 if (asprintf(&child_path, "%s%s%s", path,
1417 path[0] == '/' && path[1] == '\0' ? "" : "/",
1419 err = got_error_from_errno();
1422 err = print_tree(child_path, commit_id, show_ids, 1,
1429 te = SIMPLEQ_NEXT(te, entry);
1433 got_object_tree_close(tree);
1438 static const struct got_error *
1439 cmd_tree(int argc, char *argv[])
1441 const struct got_error *error;
1442 struct got_repository *repo = NULL;
1443 struct got_worktree *worktree = NULL;
1445 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1446 struct got_object_id *commit_id = NULL;
1447 char *commit_id_str = NULL;
1448 int show_ids = 0, recurse = 0;
1452 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1457 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1460 commit_id_str = optarg;
1463 repo_path = realpath(optarg, NULL);
1464 if (repo_path == NULL)
1465 err(1, "-r option");
1489 cwd = getcwd(NULL, 0);
1491 error = got_error_from_errno();
1494 if (repo_path == NULL) {
1495 error = got_worktree_open(&worktree, cwd);
1496 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1502 strdup(got_worktree_get_repo_path(worktree));
1503 if (repo_path == NULL)
1504 error = got_error_from_errno();
1508 repo_path = strdup(cwd);
1509 if (repo_path == NULL) {
1510 error = got_error_from_errno();
1516 error = apply_unveil(repo_path, 1, NULL);
1520 error = got_repo_open(&repo, repo_path);
1526 char *p, *worktree_subdir = cwd +
1527 strlen(got_worktree_get_root_path(worktree));
1528 if (asprintf(&p, "%s/%s",
1529 got_worktree_get_path_prefix(worktree),
1530 worktree_subdir) == -1) {
1531 error = got_error_from_errno();
1534 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1541 if (in_repo_path == NULL) {
1542 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1547 if (commit_id_str == NULL) {
1548 struct got_reference *head_ref;
1549 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1552 error = got_ref_resolve(&commit_id, repo, head_ref);
1553 got_ref_close(head_ref);
1557 error = got_object_resolve_id_str(&commit_id, repo,
1563 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1564 in_repo_path, repo);
1571 got_worktree_close(worktree);
1573 const struct got_error *repo_error;
1574 repo_error = got_repo_close(repo);
1584 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1588 static const struct got_error *
1589 print_status(void *arg, unsigned char status, const char *path,
1590 struct got_object_id *id)
1592 printf("%c %s\n", status, path);
1596 static const struct got_error *
1597 cmd_status(int argc, char *argv[])
1599 const struct got_error *error = NULL;
1600 struct got_repository *repo = NULL;
1601 struct got_worktree *worktree = NULL;
1602 char *cwd = NULL, *path = NULL;
1605 while ((ch = getopt(argc, argv, "")) != -1) {
1617 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1621 cwd = getcwd(NULL, 0);
1623 error = got_error_from_errno();
1627 error = got_worktree_open(&worktree, cwd);
1634 error = got_error_from_errno();
1637 } else if (argc == 1) {
1638 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1644 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1648 error = apply_unveil(got_repo_get_path(repo), 1,
1649 got_worktree_get_root_path(worktree));
1653 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1654 check_cancelled, NULL);
1665 "usage: %s ref [-r repository] -l | -d name | name object\n",
1670 static const struct got_error *
1671 list_refs(struct got_repository *repo)
1673 static const struct got_error *err = NULL;
1674 struct got_reflist_head refs;
1675 struct got_reflist_entry *re;
1677 SIMPLEQ_INIT(&refs);
1678 err = got_ref_list(&refs, repo);
1682 SIMPLEQ_FOREACH(re, &refs, entry) {
1684 refstr = got_ref_to_str(re->ref);
1686 return got_error_from_errno();
1687 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1691 got_ref_list_free(&refs);
1695 static const struct got_error *
1696 delete_ref(struct got_repository *repo, const char *refname)
1698 const struct got_error *err = NULL;
1699 struct got_reference *ref;
1701 err = got_ref_open(&ref, repo, refname);
1705 err = got_ref_delete(ref, repo);
1710 static const struct got_error *
1711 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1713 const struct got_error *err = NULL;
1714 struct got_object_id *id;
1715 struct got_reference *ref = NULL;
1717 err = got_object_resolve_id_str(&id, repo, id_str);
1721 err = got_ref_alloc(&ref, refname, id);
1725 err = got_ref_write(ref, repo);
1733 static const struct got_error *
1734 cmd_ref(int argc, char *argv[])
1736 const struct got_error *error = NULL;
1737 struct got_repository *repo = NULL;
1738 struct got_worktree *worktree = NULL;
1739 char *cwd = NULL, *repo_path = NULL;
1740 int ch, do_list = 0;
1741 const char *delref = NULL;
1743 /* TODO: Add -s option for adding symbolic references. */
1744 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1750 repo_path = realpath(optarg, NULL);
1751 if (repo_path == NULL)
1752 err(1, "-r option");
1763 if (do_list && delref)
1764 errx(1, "-l and -d options are mutually exclusive\n");
1769 if (do_list || delref) {
1772 } else if (argc != 2)
1777 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1781 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1782 "sendfd unveil", NULL) == -1)
1786 cwd = getcwd(NULL, 0);
1788 error = got_error_from_errno();
1792 if (repo_path == NULL) {
1793 error = got_worktree_open(&worktree, cwd);
1794 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1800 strdup(got_worktree_get_repo_path(worktree));
1801 if (repo_path == NULL)
1802 error = got_error_from_errno();
1806 repo_path = strdup(cwd);
1807 if (repo_path == NULL) {
1808 error = got_error_from_errno();
1814 error = apply_unveil(repo_path, do_list,
1815 worktree ? got_worktree_get_root_path(worktree) : NULL);
1819 error = got_repo_open(&repo, repo_path);
1824 error = list_refs(repo);
1826 error = delete_ref(repo, delref);
1828 error = add_ref(repo, argv[0], argv[1]);
1831 got_repo_close(repo);
1833 got_worktree_close(worktree);
1842 fprintf(stderr, "usage: %s add file-path\n", getprogname());
1846 static const struct got_error *
1847 cmd_add(int argc, char *argv[])
1849 const struct got_error *error = NULL;
1850 struct got_worktree *worktree = NULL;
1851 char *cwd = NULL, *path = NULL, *relpath = NULL;
1854 while ((ch = getopt(argc, argv, "")) != -1) {
1868 path = realpath(argv[0], NULL);
1870 error = got_error_from_errno();
1874 cwd = getcwd(NULL, 0);
1876 error = got_error_from_errno();
1879 error = got_worktree_open(&worktree, cwd);
1883 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
1887 error = got_worktree_schedule_add(&relpath, worktree, path);
1890 printf("%c %s\n", GOT_STATUS_ADD, relpath);
1893 got_worktree_close(worktree);