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(repo_path);
393 got_path_strip_trailing_slashes(worktree_path);
395 error = got_repo_open(&repo, repo_path);
399 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
403 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
407 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
408 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
411 error = got_worktree_open(&worktree, worktree_path);
415 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
419 if (!same_path_prefix) {
420 error = got_error(GOT_ERR_PATH_PREFIX);
425 struct got_object_id *commit_id;
426 error = got_object_resolve_id_str(&commit_id, repo,
430 error = check_ancestry(worktree, commit_id, repo);
435 error = got_worktree_set_base_commit_id(worktree, repo,
442 error = got_worktree_checkout_files(worktree, "", repo,
443 checkout_progress, worktree_path, check_cancelled, NULL);
447 printf("Now shut up and hack\n");
459 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
465 update_progress(void *arg, unsigned char status, const char *path)
467 int *did_something = arg;
469 if (status == GOT_STATUS_EXISTS)
473 while (path[0] == '/')
475 printf("%c %s\n", status, path);
478 static const struct got_error *
479 cmd_update(int argc, char *argv[])
481 const struct got_error *error = NULL;
482 struct got_repository *repo = NULL;
483 struct got_worktree *worktree = NULL;
484 char *worktree_path = NULL, *path = NULL;
485 struct got_object_id *commit_id = NULL;
486 char *commit_id_str = NULL;
487 int ch, did_something = 0;
489 while ((ch = getopt(argc, argv, "c:")) != -1) {
492 commit_id_str = strdup(optarg);
493 if (commit_id_str == NULL)
494 return got_error_from_errno();
506 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
507 "unveil", NULL) == -1)
510 worktree_path = getcwd(NULL, 0);
511 if (worktree_path == NULL) {
512 error = got_error_from_errno();
515 error = got_worktree_open(&worktree, worktree_path);
522 error = got_error_from_errno();
525 } else if (argc == 1) {
526 error = got_worktree_resolve_path(&path, worktree, argv[0]);
532 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
536 error = apply_unveil(got_repo_get_path(repo), 0,
537 got_worktree_get_root_path(worktree), 0);
541 if (commit_id_str == NULL) {
542 struct got_reference *head_ref;
543 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
546 error = got_ref_resolve(&commit_id, repo, head_ref);
549 error = got_object_id_str(&commit_id_str, commit_id);
553 error = got_object_resolve_id_str(&commit_id, repo,
559 error = check_ancestry(worktree, commit_id, repo);
563 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
565 error = got_worktree_set_base_commit_id(worktree, repo,
571 error = got_worktree_checkout_files(worktree, path, repo,
572 update_progress, &did_something, check_cancelled, NULL);
577 printf("Updated to commit %s\n", commit_id_str);
579 printf("Already up-to-date\n");
588 static const struct got_error *
589 print_patch(struct got_commit_object *commit, struct got_object_id *id,
590 int diff_context, struct got_repository *repo)
592 const struct got_error *err = NULL;
593 struct got_tree_object *tree1 = NULL, *tree2;
594 struct got_object_qid *qid;
595 char *id_str1 = NULL, *id_str2;
597 err = got_object_open_as_tree(&tree2, repo,
598 got_object_commit_get_tree_id(commit));
602 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
604 struct got_commit_object *pcommit;
606 err = got_object_open_as_commit(&pcommit, repo, qid->id);
610 err = got_object_open_as_tree(&tree1, repo,
611 got_object_commit_get_tree_id(pcommit));
612 got_object_commit_close(pcommit);
616 err = got_object_id_str(&id_str1, qid->id);
621 err = got_object_id_str(&id_str2, id);
625 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
626 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
629 got_object_tree_close(tree1);
630 got_object_tree_close(tree2);
637 get_datestr(time_t *time, char *datebuf)
639 char *p, *s = ctime_r(time, datebuf);
646 static const struct got_error *
647 print_commit(struct got_commit_object *commit, struct got_object_id *id,
648 struct got_repository *repo, int show_patch, int diff_context,
649 struct got_reflist_head *refs)
651 const struct got_error *err = NULL;
652 char *id_str, *datestr, *logmsg0, *logmsg, *line;
654 time_t committer_time;
655 const char *author, *committer;
656 char *refs_str = NULL;
657 struct got_reflist_entry *re;
659 SIMPLEQ_FOREACH(re, refs, entry) {
662 if (got_object_id_cmp(re->id, id) != 0)
664 name = got_ref_get_name(re->ref);
665 if (strcmp(name, GOT_REF_HEAD) == 0)
667 if (strncmp(name, "refs/", 5) == 0)
669 if (strncmp(name, "got/", 4) == 0)
671 if (strncmp(name, "heads/", 6) == 0)
673 if (strncmp(name, "remotes/", 8) == 0)
676 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
678 err = got_error_from_errno();
684 err = got_object_id_str(&id_str, id);
688 printf("-----------------------------------------------\n");
689 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
690 refs_str ? refs_str : "", refs_str ? ")" : "");
695 printf("from: %s\n", got_object_commit_get_author(commit));
696 committer_time = got_object_commit_get_committer_time(commit);
697 datestr = get_datestr(&committer_time, datebuf);
698 printf("date: %s UTC\n", datestr);
699 author = got_object_commit_get_author(commit);
700 committer = got_object_commit_get_committer(commit);
701 if (strcmp(author, committer) != 0)
702 printf("via: %s\n", committer);
703 if (got_object_commit_get_nparents(commit) > 1) {
704 const struct got_object_id_queue *parent_ids;
705 struct got_object_qid *qid;
707 parent_ids = got_object_commit_get_parent_ids(commit);
708 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
709 err = got_object_id_str(&id_str, qid->id);
712 printf("parent %d: %s\n", n++, id_str);
717 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
719 return got_error_from_errno();
723 line = strsep(&logmsg, "\n");
725 printf(" %s\n", line);
730 err = print_patch(commit, id, diff_context, repo);
735 if (fflush(stdout) != 0 && err == NULL)
736 err = got_error_from_errno();
740 static const struct got_error *
741 print_commits(struct got_object_id *root_id, struct got_repository *repo,
742 char *path, int show_patch, int diff_context, int limit,
743 int first_parent_traversal, struct got_reflist_head *refs)
745 const struct got_error *err;
746 struct got_commit_graph *graph;
748 err = got_commit_graph_open(&graph, root_id, path,
749 first_parent_traversal, repo);
752 err = got_commit_graph_iter_start(graph, root_id, repo);
756 struct got_commit_object *commit;
757 struct got_object_id *id;
759 if (sigint_received || sigpipe_received)
762 err = got_commit_graph_iter_next(&id, graph);
764 if (err->code == GOT_ERR_ITER_COMPLETED) {
768 if (err->code != GOT_ERR_ITER_NEED_MORE)
770 err = got_commit_graph_fetch_commits(graph, 1, repo);
779 err = got_object_open_as_commit(&commit, repo, id);
782 err = print_commit(commit, id, repo, show_patch, diff_context,
784 got_object_commit_close(commit);
785 if (err || (limit && --limit == 0))
789 got_commit_graph_close(graph);
796 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
797 "[-r repository-path] [path]\n", getprogname());
801 static const struct got_error *
802 cmd_log(int argc, char *argv[])
804 const struct got_error *error;
805 struct got_repository *repo = NULL;
806 struct got_worktree *worktree = NULL;
807 struct got_commit_object *commit = NULL;
808 struct got_object_id *id = NULL;
809 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
810 char *start_commit = NULL;
811 int diff_context = 3, ch;
812 int show_patch = 0, limit = 0, first_parent_traversal = 0;
814 struct got_reflist_head refs;
819 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
825 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
831 start_commit = optarg;
834 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
837 err(1, "-C option %s", errstr);
840 limit = strtonum(optarg, 1, INT_MAX, &errstr);
842 err(1, "-l option %s", errstr);
845 first_parent_traversal = 1;
848 repo_path = realpath(optarg, NULL);
849 if (repo_path == NULL)
851 got_path_strip_trailing_slashes(repo_path);
862 cwd = getcwd(NULL, 0);
864 error = got_error_from_errno();
868 error = got_worktree_open(&worktree, cwd);
869 if (error && error->code != GOT_ERR_NOT_WORKTREE)
876 error = got_error_from_errno();
879 } else if (argc == 1) {
881 error = got_worktree_resolve_path(&path, worktree,
886 path = strdup(argv[0]);
888 error = got_error_from_errno();
895 if (repo_path == NULL) {
896 repo_path = worktree ?
897 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
899 if (repo_path == NULL) {
900 error = got_error_from_errno();
904 error = got_repo_open(&repo, repo_path);
908 error = apply_unveil(got_repo_get_path(repo), 1,
909 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
913 if (start_commit == NULL) {
914 struct got_reference *head_ref;
915 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
918 error = got_ref_resolve(&id, repo, head_ref);
919 got_ref_close(head_ref);
922 error = got_object_open_as_commit(&commit, repo, id);
924 struct got_reference *ref;
925 error = got_ref_open(&ref, repo, start_commit);
928 error = got_ref_resolve(&id, repo, ref);
932 error = got_object_get_type(&obj_type, repo, id);
935 if (obj_type == GOT_OBJ_TYPE_TAG) {
936 struct got_tag_object *tag;
937 error = got_object_open_as_tag(&tag, repo, id);
940 if (got_object_tag_get_object_type(tag) !=
941 GOT_OBJ_TYPE_COMMIT) {
942 got_object_tag_close(tag);
943 error = got_error(GOT_ERR_OBJ_TYPE);
947 id = got_object_id_dup(
948 got_object_tag_get_object_id(tag));
950 error = got_error_from_errno();
951 got_object_tag_close(tag);
954 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
955 error = got_error(GOT_ERR_OBJ_TYPE);
958 error = got_object_open_as_commit(&commit, repo, id);
962 if (commit == NULL) {
963 error = got_object_resolve_id_str(&id, repo,
972 error = got_repo_map_path(&in_repo_path, repo, path, 1);
980 error = got_ref_list(&refs, repo);
984 error = print_commits(id, repo, path, show_patch,
985 diff_context, limit, first_parent_traversal, &refs);
992 got_worktree_close(worktree);
994 const struct got_error *repo_error;
995 repo_error = got_repo_close(repo);
999 got_ref_list_free(&refs);
1006 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1007 "[object1 object2 | path]\n", getprogname());
1011 struct print_diff_arg {
1012 struct got_repository *repo;
1013 struct got_worktree *worktree;
1019 static const struct got_error *
1020 print_diff(void *arg, unsigned char status, const char *path,
1021 struct got_object_id *id)
1023 struct print_diff_arg *a = arg;
1024 const struct got_error *err = NULL;
1025 struct got_blob_object *blob1 = NULL;
1027 char *abspath = NULL;
1030 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1031 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1034 if (!a->header_shown) {
1035 printf("diff %s %s\n", a->id_str,
1036 got_worktree_get_root_path(a->worktree));
1037 a->header_shown = 1;
1040 if (status != GOT_STATUS_ADD) {
1041 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1047 if (status != GOT_STATUS_DELETE) {
1048 if (asprintf(&abspath, "%s/%s",
1049 got_worktree_get_root_path(a->worktree), path) == -1) {
1050 err = got_error_from_errno();
1054 f2 = fopen(abspath, "r");
1056 err = got_error_from_errno();
1059 if (lstat(abspath, &sb) == -1) {
1060 err = got_error_from_errno();
1066 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1070 got_object_blob_close(blob1);
1071 if (f2 && fclose(f2) != 0 && err == NULL)
1072 err = got_error_from_errno();
1077 static const struct got_error *
1078 cmd_diff(int argc, char *argv[])
1080 const struct got_error *error;
1081 struct got_repository *repo = NULL;
1082 struct got_worktree *worktree = NULL;
1083 char *cwd = NULL, *repo_path = NULL;
1084 struct got_object_id *id1 = NULL, *id2 = NULL;
1085 char *id_str1 = NULL, *id_str2 = NULL;
1087 int diff_context = 3, ch;
1092 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1097 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1100 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1102 err(1, "-C option %s", errstr);
1105 repo_path = realpath(optarg, NULL);
1106 if (repo_path == NULL)
1107 err(1, "-r option");
1108 got_path_strip_trailing_slashes(repo_path);
1119 cwd = getcwd(NULL, 0);
1121 error = got_error_from_errno();
1124 error = got_worktree_open(&worktree, cwd);
1125 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1128 if (worktree == NULL) {
1129 error = got_error(GOT_ERR_NOT_WORKTREE);
1134 "-r option can't be used when diffing a work tree");
1135 repo_path = strdup(got_worktree_get_repo_path(worktree));
1136 if (repo_path == NULL) {
1137 error = got_error_from_errno();
1141 error = got_worktree_resolve_path(&path, worktree,
1148 error = got_error_from_errno();
1152 } else if (argc == 2) {
1158 if (repo_path == NULL) {
1159 repo_path = getcwd(NULL, 0);
1160 if (repo_path == NULL)
1161 return got_error_from_errno();
1164 error = got_repo_open(&repo, repo_path);
1169 error = apply_unveil(got_repo_get_path(repo), 1,
1170 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1175 struct print_diff_arg arg;
1177 error = got_object_id_str(&id_str,
1178 got_worktree_get_base_commit_id(worktree));
1182 arg.worktree = worktree;
1183 arg.diff_context = diff_context;
1184 arg.id_str = id_str;
1185 arg.header_shown = 0;
1187 error = got_worktree_status(worktree, path, repo, print_diff,
1188 &arg, check_cancelled, NULL);
1193 error = got_object_resolve_id_str(&id1, repo, id_str1);
1197 error = got_object_resolve_id_str(&id2, repo, id_str2);
1201 error = got_object_get_type(&type1, repo, id1);
1205 error = got_object_get_type(&type2, repo, id2);
1209 if (type1 != type2) {
1210 error = got_error(GOT_ERR_OBJ_TYPE);
1215 case GOT_OBJ_TYPE_BLOB:
1216 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1217 diff_context, repo, stdout);
1219 case GOT_OBJ_TYPE_TREE:
1220 error = got_diff_objects_as_trees(id1, id2, "", "",
1221 diff_context, repo, stdout);
1223 case GOT_OBJ_TYPE_COMMIT:
1224 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1226 error = got_diff_objects_as_commits(id1, id2, diff_context,
1230 error = got_error(GOT_ERR_OBJ_TYPE);
1238 got_worktree_close(worktree);
1240 const struct got_error *repo_error;
1241 repo_error = got_repo_close(repo);
1252 "usage: %s blame [-c commit] [-r repository-path] path\n",
1257 static const struct got_error *
1258 cmd_blame(int argc, char *argv[])
1260 const struct got_error *error;
1261 struct got_repository *repo = NULL;
1262 struct got_worktree *worktree = NULL;
1263 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1264 struct got_object_id *commit_id = NULL;
1265 char *commit_id_str = NULL;
1269 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1274 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1277 commit_id_str = optarg;
1280 repo_path = realpath(optarg, NULL);
1281 if (repo_path == NULL)
1282 err(1, "-r option");
1283 got_path_strip_trailing_slashes(repo_path);
1299 cwd = getcwd(NULL, 0);
1301 error = got_error_from_errno();
1304 if (repo_path == NULL) {
1305 error = got_worktree_open(&worktree, cwd);
1306 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1312 strdup(got_worktree_get_repo_path(worktree));
1313 if (repo_path == NULL)
1314 error = got_error_from_errno();
1318 repo_path = strdup(cwd);
1319 if (repo_path == NULL) {
1320 error = got_error_from_errno();
1326 error = got_repo_open(&repo, repo_path);
1330 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1335 const char *prefix = got_worktree_get_path_prefix(worktree);
1336 char *p, *worktree_subdir = cwd +
1337 strlen(got_worktree_get_root_path(worktree));
1338 if (asprintf(&p, "%s%s%s%s%s",
1339 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1340 worktree_subdir, worktree_subdir[0] ? "/" : "",
1342 error = got_error_from_errno();
1345 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1348 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1353 if (commit_id_str == NULL) {
1354 struct got_reference *head_ref;
1355 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1358 error = got_ref_resolve(&commit_id, repo, head_ref);
1359 got_ref_close(head_ref);
1363 error = got_object_resolve_id_str(&commit_id, repo,
1369 error = got_blame(in_repo_path, commit_id, repo, stdout);
1376 got_worktree_close(worktree);
1378 const struct got_error *repo_error;
1379 repo_error = got_repo_close(repo);
1390 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1396 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1397 const char *root_path)
1399 int is_root_path = (strcmp(path, root_path) == 0);
1401 path += strlen(root_path);
1402 while (path[0] == '/')
1405 printf("%s%s%s%s%s\n", id ? id : "", path,
1406 is_root_path ? "" : "/", te->name,
1407 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1410 static const struct got_error *
1411 print_tree(const char *path, struct got_object_id *commit_id,
1412 int show_ids, int recurse, const char *root_path,
1413 struct got_repository *repo)
1415 const struct got_error *err = NULL;
1416 struct got_object_id *tree_id = NULL;
1417 struct got_tree_object *tree = NULL;
1418 const struct got_tree_entries *entries;
1419 struct got_tree_entry *te;
1421 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1425 err = got_object_open_as_tree(&tree, repo, tree_id);
1428 entries = got_object_tree_get_entries(tree);
1429 te = SIMPLEQ_FIRST(&entries->head);
1433 if (sigint_received || sigpipe_received)
1438 err = got_object_id_str(&id_str, te->id);
1441 if (asprintf(&id, "%s ", id_str) == -1) {
1442 err = got_error_from_errno();
1448 print_entry(te, id, path, root_path);
1451 if (recurse && S_ISDIR(te->mode)) {
1453 if (asprintf(&child_path, "%s%s%s", path,
1454 path[0] == '/' && path[1] == '\0' ? "" : "/",
1456 err = got_error_from_errno();
1459 err = print_tree(child_path, commit_id, show_ids, 1,
1466 te = SIMPLEQ_NEXT(te, entry);
1470 got_object_tree_close(tree);
1475 static const struct got_error *
1476 cmd_tree(int argc, char *argv[])
1478 const struct got_error *error;
1479 struct got_repository *repo = NULL;
1480 struct got_worktree *worktree = NULL;
1482 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1483 struct got_object_id *commit_id = NULL;
1484 char *commit_id_str = NULL;
1485 int show_ids = 0, recurse = 0;
1489 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1494 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1497 commit_id_str = optarg;
1500 repo_path = realpath(optarg, NULL);
1501 if (repo_path == NULL)
1502 err(1, "-r option");
1503 got_path_strip_trailing_slashes(repo_path);
1527 cwd = getcwd(NULL, 0);
1529 error = got_error_from_errno();
1532 if (repo_path == NULL) {
1533 error = got_worktree_open(&worktree, cwd);
1534 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1540 strdup(got_worktree_get_repo_path(worktree));
1541 if (repo_path == NULL)
1542 error = got_error_from_errno();
1546 repo_path = strdup(cwd);
1547 if (repo_path == NULL) {
1548 error = got_error_from_errno();
1554 error = got_repo_open(&repo, repo_path);
1558 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1564 char *p, *worktree_subdir = cwd +
1565 strlen(got_worktree_get_root_path(worktree));
1566 if (asprintf(&p, "%s/%s",
1567 got_worktree_get_path_prefix(worktree),
1568 worktree_subdir) == -1) {
1569 error = got_error_from_errno();
1572 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1579 if (in_repo_path == NULL) {
1580 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1585 if (commit_id_str == NULL) {
1586 struct got_reference *head_ref;
1587 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1590 error = got_ref_resolve(&commit_id, repo, head_ref);
1591 got_ref_close(head_ref);
1595 error = got_object_resolve_id_str(&commit_id, repo,
1601 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1602 in_repo_path, repo);
1609 got_worktree_close(worktree);
1611 const struct got_error *repo_error;
1612 repo_error = got_repo_close(repo);
1622 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1626 static const struct got_error *
1627 print_status(void *arg, unsigned char status, const char *path,
1628 struct got_object_id *id)
1630 printf("%c %s\n", status, path);
1634 static const struct got_error *
1635 cmd_status(int argc, char *argv[])
1637 const struct got_error *error = NULL;
1638 struct got_repository *repo = NULL;
1639 struct got_worktree *worktree = NULL;
1640 char *cwd = NULL, *path = NULL;
1643 while ((ch = getopt(argc, argv, "")) != -1) {
1655 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1659 cwd = getcwd(NULL, 0);
1661 error = got_error_from_errno();
1665 error = got_worktree_open(&worktree, cwd);
1672 error = got_error_from_errno();
1675 } else if (argc == 1) {
1676 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1682 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1686 error = apply_unveil(got_repo_get_path(repo), 1,
1687 got_worktree_get_root_path(worktree), 0);
1691 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1692 check_cancelled, NULL);
1703 "usage: %s ref [-r repository] -l | -d name | name object\n",
1708 static const struct got_error *
1709 list_refs(struct got_repository *repo)
1711 static const struct got_error *err = NULL;
1712 struct got_reflist_head refs;
1713 struct got_reflist_entry *re;
1715 SIMPLEQ_INIT(&refs);
1716 err = got_ref_list(&refs, repo);
1720 SIMPLEQ_FOREACH(re, &refs, entry) {
1722 refstr = got_ref_to_str(re->ref);
1724 return got_error_from_errno();
1725 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1729 got_ref_list_free(&refs);
1733 static const struct got_error *
1734 delete_ref(struct got_repository *repo, const char *refname)
1736 const struct got_error *err = NULL;
1737 struct got_reference *ref;
1739 err = got_ref_open(&ref, repo, refname);
1743 err = got_ref_delete(ref, repo);
1748 static const struct got_error *
1749 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1751 const struct got_error *err = NULL;
1752 struct got_object_id *id;
1753 struct got_reference *ref = NULL;
1755 err = got_object_resolve_id_str(&id, repo, id_str);
1759 err = got_ref_alloc(&ref, refname, id);
1763 err = got_ref_write(ref, repo);
1771 static const struct got_error *
1772 cmd_ref(int argc, char *argv[])
1774 const struct got_error *error = NULL;
1775 struct got_repository *repo = NULL;
1776 struct got_worktree *worktree = NULL;
1777 char *cwd = NULL, *repo_path = NULL;
1778 int ch, do_list = 0;
1779 const char *delref = NULL;
1781 /* TODO: Add -s option for adding symbolic references. */
1782 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1788 repo_path = realpath(optarg, NULL);
1789 if (repo_path == NULL)
1790 err(1, "-r option");
1791 got_path_strip_trailing_slashes(repo_path);
1802 if (do_list && delref)
1803 errx(1, "-l and -d options are mutually exclusive\n");
1808 if (do_list || delref) {
1811 } else if (argc != 2)
1816 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1820 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1821 "sendfd unveil", NULL) == -1)
1825 cwd = getcwd(NULL, 0);
1827 error = got_error_from_errno();
1831 if (repo_path == NULL) {
1832 error = got_worktree_open(&worktree, cwd);
1833 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1839 strdup(got_worktree_get_repo_path(worktree));
1840 if (repo_path == NULL)
1841 error = got_error_from_errno();
1845 repo_path = strdup(cwd);
1846 if (repo_path == NULL) {
1847 error = got_error_from_errno();
1853 error = got_repo_open(&repo, repo_path);
1857 error = apply_unveil(got_repo_get_path(repo), do_list,
1858 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1863 error = list_refs(repo);
1865 error = delete_ref(repo, delref);
1867 error = add_ref(repo, argv[0], argv[1]);
1870 got_repo_close(repo);
1872 got_worktree_close(worktree);
1881 fprintf(stderr, "usage: %s add file-path\n", getprogname());
1885 static const struct got_error *
1886 cmd_add(int argc, char *argv[])
1888 const struct got_error *error = NULL;
1889 struct got_repository *repo = NULL;
1890 struct got_worktree *worktree = NULL;
1891 char *cwd = NULL, *path = NULL, *relpath = NULL;
1894 while ((ch = getopt(argc, argv, "")) != -1) {
1908 path = realpath(argv[0], NULL);
1910 error = got_error_from_errno();
1913 got_path_strip_trailing_slashes(path);
1915 cwd = getcwd(NULL, 0);
1917 error = got_error_from_errno();
1920 error = got_worktree_open(&worktree, cwd);
1924 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1928 error = apply_unveil(got_repo_get_path(repo), 1,
1929 got_worktree_get_root_path(worktree), 0);
1933 error = got_worktree_schedule_add(worktree, path, print_status, NULL,
1939 got_repo_close(repo);
1941 got_worktree_close(worktree);
1951 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1955 static const struct got_error *
1956 cmd_rm(int argc, char *argv[])
1958 const struct got_error *error = NULL;
1959 struct got_worktree *worktree = NULL;
1960 struct got_repository *repo = NULL;
1961 char *cwd = NULL, *path = NULL;
1962 int ch, delete_local_mods = 0;
1964 while ((ch = getopt(argc, argv, "f")) != -1) {
1967 delete_local_mods = 1;
1981 path = realpath(argv[0], NULL);
1983 error = got_error_from_errno();
1986 got_path_strip_trailing_slashes(path);
1988 cwd = getcwd(NULL, 0);
1990 error = got_error_from_errno();
1993 error = got_worktree_open(&worktree, cwd);
1997 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2001 error = apply_unveil(got_repo_get_path(repo), 1,
2002 got_worktree_get_root_path(worktree), 0);
2006 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2007 print_status, NULL, repo);
2012 got_repo_close(repo);
2014 got_worktree_close(worktree);
2023 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2028 revert_progress(void *arg, unsigned char status, const char *path)
2030 while (path[0] == '/')
2032 printf("%c %s\n", status, path);
2035 static const struct got_error *
2036 cmd_revert(int argc, char *argv[])
2038 const struct got_error *error = NULL;
2039 struct got_worktree *worktree = NULL;
2040 struct got_repository *repo = NULL;
2041 char *cwd = NULL, *path = NULL;
2044 while ((ch = getopt(argc, argv, "")) != -1) {
2058 path = realpath(argv[0], NULL);
2060 error = got_error_from_errno();
2063 got_path_strip_trailing_slashes(path);
2065 cwd = getcwd(NULL, 0);
2067 error = got_error_from_errno();
2070 error = got_worktree_open(&worktree, cwd);
2074 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2078 error = apply_unveil(got_repo_get_path(repo), 1,
2079 got_worktree_get_root_path(worktree), 0);
2083 error = got_worktree_revert(worktree, path,
2084 revert_progress, NULL, repo);
2089 got_repo_close(repo);
2091 got_worktree_close(worktree);
2100 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2104 static const struct got_error *
2105 cmd_commit(int argc, char *argv[])
2107 const struct got_error *error = NULL;
2108 struct got_worktree *worktree = NULL;
2109 struct got_repository *repo = NULL;
2110 char *cwd = NULL, *path = NULL, *id_str = NULL;
2111 struct got_object_id *id = NULL;
2112 const char *logmsg = "<no log message was specified>";
2113 const char *got_author = getenv("GOT_AUTHOR");
2116 while ((ch = getopt(argc, argv, "m:")) != -1) {
2131 path = realpath(argv[0], NULL);
2133 error = got_error_from_errno();
2136 got_path_strip_trailing_slashes(path);
2137 } else if (argc != 0)
2140 if (got_author == NULL) {
2141 /* TODO: Look current user up in password database */
2142 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2146 cwd = getcwd(NULL, 0);
2148 error = got_error_from_errno();
2151 error = got_worktree_open(&worktree, cwd);
2155 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2159 error = apply_unveil(got_repo_get_path(repo), 0,
2160 got_worktree_get_root_path(worktree), 0);
2164 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2165 logmsg, print_status, NULL, repo);
2169 error = got_object_id_str(&id_str, id);
2172 printf("created commit %s\n", id_str);
2175 got_repo_close(repo);
2177 got_worktree_close(worktree);