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);
81 __dead static void usage_rm(void);
82 __dead static void usage_revert(void);
83 __dead static void usage_commit(void);
85 static const struct got_error* cmd_checkout(int, char *[]);
86 static const struct got_error* cmd_update(int, char *[]);
87 static const struct got_error* cmd_log(int, char *[]);
88 static const struct got_error* cmd_diff(int, char *[]);
89 static const struct got_error* cmd_blame(int, char *[]);
90 static const struct got_error* cmd_tree(int, char *[]);
91 static const struct got_error* cmd_status(int, char *[]);
92 static const struct got_error* cmd_ref(int, char *[]);
93 static const struct got_error* cmd_add(int, char *[]);
94 static const struct got_error* cmd_rm(int, char *[]);
95 static const struct got_error* cmd_revert(int, char *[]);
96 static const struct got_error* cmd_commit(int, char *[]);
98 static struct cmd got_commands[] = {
99 { "checkout", cmd_checkout, usage_checkout,
100 "check out a new work tree from a repository" },
101 { "update", cmd_update, usage_update,
102 "update a work tree to a different commit" },
103 { "log", cmd_log, usage_log,
104 "show repository history" },
105 { "diff", cmd_diff, usage_diff,
106 "compare files and directories" },
107 { "blame", cmd_blame, usage_blame,
108 "show when lines in a file were changed" },
109 { "tree", cmd_tree, usage_tree,
110 "list files and directories in repository" },
111 { "status", cmd_status, usage_status,
112 "show modification status of files" },
113 { "ref", cmd_ref, usage_ref,
114 "manage references in repository" },
115 { "add", cmd_add, usage_add,
116 "add a new file to version control" },
117 { "rm", cmd_rm, usage_rm,
118 "remove a versioned file" },
119 { "revert", cmd_revert, usage_revert,
120 "revert uncommitted changes" },
121 { "commit", cmd_commit, usage_commit,
122 "create blob from file (WIP; can't create commits yet)" },
126 main(int argc, char *argv[])
133 setlocale(LC_CTYPE, "");
135 while ((ch = getopt(argc, argv, "h")) != -1) {
153 signal(SIGINT, catch_sigint);
154 signal(SIGPIPE, catch_sigpipe);
156 for (i = 0; i < nitems(got_commands); i++) {
157 const struct got_error *error;
159 cmd = &got_commands[i];
161 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
165 got_commands[i].cmd_usage();
167 error = got_commands[i].cmd_main(argc, argv);
168 if (error && !(sigint_received || sigpipe_received)) {
169 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
176 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
185 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
186 "Available commands:\n", getprogname());
187 for (i = 0; i < nitems(got_commands); i++) {
188 struct cmd *cmd = &got_commands[i];
189 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
194 static const struct got_error *
195 apply_unveil(const char *repo_path, int repo_read_only,
196 const char *worktree_path)
198 const struct got_error *error;
200 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
201 return got_error_from_errno();
203 if (worktree_path && unveil(worktree_path, "rwc") != 0)
204 return got_error_from_errno();
206 if (unveil("/tmp", "rwc") != 0)
207 return got_error_from_errno();
209 error = got_privsep_unveil_exec_helpers();
213 if (unveil(NULL, NULL) != 0)
214 return got_error_from_errno();
222 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
223 "[worktree-path]\n", getprogname());
228 checkout_progress(void *arg, unsigned char status, const char *path)
230 char *worktree_path = arg;
232 while (path[0] == '/')
235 printf("%c %s/%s\n", status, worktree_path, path);
238 static const struct got_error *
239 check_cancelled(void *arg)
241 if (sigint_received || sigpipe_received)
242 return got_error(GOT_ERR_CANCELLED);
246 static const struct got_error *
247 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
248 struct got_repository *repo)
250 const struct got_error *err;
251 struct got_reference *head_ref = NULL;
252 struct got_object_id *head_commit_id = NULL;
253 struct got_commit_graph *graph = NULL;
255 head_ref = got_worktree_get_head_ref(worktree);
256 if (head_ref == NULL)
257 return got_error_from_errno();
259 /* TODO: Check the reflog. The head ref may have been rebased. */
260 err = got_ref_resolve(&head_commit_id, repo, head_ref);
264 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
268 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
272 struct got_object_id *id;
274 if (sigint_received || sigpipe_received)
277 err = got_commit_graph_iter_next(&id, graph);
279 if (err->code == GOT_ERR_ITER_COMPLETED) {
280 err = got_error(GOT_ERR_ANCESTRY);
283 if (err->code != GOT_ERR_ITER_NEED_MORE)
285 err = got_commit_graph_fetch_commits(graph, 1, repo);
293 if (got_object_id_cmp(id, commit_id) == 0)
298 got_ref_close(head_ref);
300 got_commit_graph_close(graph);
305 static const struct got_error *
306 cmd_checkout(int argc, char *argv[])
308 const struct got_error *error = NULL;
309 struct got_repository *repo = NULL;
310 struct got_reference *head_ref = NULL;
311 struct got_worktree *worktree = NULL;
312 char *repo_path = NULL;
313 char *worktree_path = NULL;
314 const char *path_prefix = "";
315 char *commit_id_str = NULL;
316 int ch, same_path_prefix;
318 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
321 commit_id_str = strdup(optarg);
322 if (commit_id_str == NULL)
323 return got_error_from_errno();
326 path_prefix = optarg;
338 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
339 "unveil", NULL) == -1)
343 char *cwd, *base, *dotgit;
344 repo_path = realpath(argv[0], NULL);
345 if (repo_path == NULL)
346 return got_error_from_errno();
347 cwd = getcwd(NULL, 0);
349 error = got_error_from_errno();
353 base = basename(path_prefix);
355 base = basename(repo_path);
357 error = got_error_from_errno();
360 dotgit = strstr(base, ".git");
363 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
364 error = got_error_from_errno();
369 } else if (argc == 2) {
370 repo_path = realpath(argv[0], NULL);
371 if (repo_path == NULL) {
372 error = got_error_from_errno();
375 worktree_path = realpath(argv[1], NULL);
376 if (worktree_path == NULL) {
377 error = got_error_from_errno();
383 error = got_repo_open(&repo, repo_path);
387 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
391 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
395 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
396 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
399 error = got_worktree_open(&worktree, worktree_path);
403 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
407 if (!same_path_prefix) {
408 error = got_error(GOT_ERR_PATH_PREFIX);
413 struct got_object_id *commit_id;
414 error = got_object_resolve_id_str(&commit_id, repo,
418 error = check_ancestry(worktree, commit_id, repo);
423 error = got_worktree_set_base_commit_id(worktree, repo,
430 error = got_worktree_checkout_files(worktree, "", repo,
431 checkout_progress, worktree_path, check_cancelled, NULL);
435 printf("Now shut up and hack\n");
447 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
453 update_progress(void *arg, unsigned char status, const char *path)
455 int *did_something = arg;
457 if (status == GOT_STATUS_EXISTS)
461 while (path[0] == '/')
463 printf("%c %s\n", status, path);
466 static const struct got_error *
467 cmd_update(int argc, char *argv[])
469 const struct got_error *error = NULL;
470 struct got_repository *repo = NULL;
471 struct got_worktree *worktree = NULL;
472 char *worktree_path = NULL, *path = NULL;
473 struct got_object_id *commit_id = NULL;
474 char *commit_id_str = NULL;
475 int ch, did_something = 0;
477 while ((ch = getopt(argc, argv, "c:")) != -1) {
480 commit_id_str = strdup(optarg);
481 if (commit_id_str == NULL)
482 return got_error_from_errno();
494 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
495 "unveil", NULL) == -1)
498 worktree_path = getcwd(NULL, 0);
499 if (worktree_path == NULL) {
500 error = got_error_from_errno();
503 error = got_worktree_open(&worktree, worktree_path);
510 error = got_error_from_errno();
513 } else if (argc == 1) {
514 error = got_worktree_resolve_path(&path, worktree, argv[0]);
520 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
524 error = apply_unveil(got_repo_get_path(repo), 0,
525 got_worktree_get_root_path(worktree));
529 if (commit_id_str == NULL) {
530 struct got_reference *head_ref;
531 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
534 error = got_ref_resolve(&commit_id, repo, head_ref);
537 error = got_object_id_str(&commit_id_str, commit_id);
541 error = got_object_resolve_id_str(&commit_id, repo,
547 error = check_ancestry(worktree, commit_id, repo);
551 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
553 error = got_worktree_set_base_commit_id(worktree, repo,
559 error = got_worktree_checkout_files(worktree, path, repo,
560 update_progress, &did_something, check_cancelled, NULL);
565 printf("Updated to commit %s\n", commit_id_str);
567 printf("Already up-to-date\n");
576 static const struct got_error *
577 print_patch(struct got_commit_object *commit, struct got_object_id *id,
578 int diff_context, struct got_repository *repo)
580 const struct got_error *err = NULL;
581 struct got_tree_object *tree1 = NULL, *tree2;
582 struct got_object_qid *qid;
583 char *id_str1 = NULL, *id_str2;
585 err = got_object_open_as_tree(&tree2, repo,
586 got_object_commit_get_tree_id(commit));
590 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
592 struct got_commit_object *pcommit;
594 err = got_object_open_as_commit(&pcommit, repo, qid->id);
598 err = got_object_open_as_tree(&tree1, repo,
599 got_object_commit_get_tree_id(pcommit));
600 got_object_commit_close(pcommit);
604 err = got_object_id_str(&id_str1, qid->id);
609 err = got_object_id_str(&id_str2, id);
613 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
614 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
617 got_object_tree_close(tree1);
618 got_object_tree_close(tree2);
625 get_datestr(time_t *time, char *datebuf)
627 char *p, *s = ctime_r(time, datebuf);
634 static const struct got_error *
635 print_commit(struct got_commit_object *commit, struct got_object_id *id,
636 struct got_repository *repo, int show_patch, int diff_context,
637 struct got_reflist_head *refs)
639 const struct got_error *err = NULL;
640 char *id_str, *datestr, *logmsg0, *logmsg, *line;
642 time_t committer_time;
643 const char *author, *committer;
644 char *refs_str = NULL;
645 struct got_reflist_entry *re;
647 SIMPLEQ_FOREACH(re, refs, entry) {
650 if (got_object_id_cmp(re->id, id) != 0)
652 name = got_ref_get_name(re->ref);
653 if (strcmp(name, GOT_REF_HEAD) == 0)
655 if (strncmp(name, "refs/", 5) == 0)
657 if (strncmp(name, "got/", 4) == 0)
659 if (strncmp(name, "heads/", 6) == 0)
661 if (strncmp(name, "remotes/", 8) == 0)
664 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
666 err = got_error_from_errno();
672 err = got_object_id_str(&id_str, id);
676 printf("-----------------------------------------------\n");
677 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
678 refs_str ? refs_str : "", refs_str ? ")" : "");
683 printf("from: %s\n", got_object_commit_get_author(commit));
684 committer_time = got_object_commit_get_committer_time(commit);
685 datestr = get_datestr(&committer_time, datebuf);
686 printf("date: %s UTC\n", datestr);
687 author = got_object_commit_get_author(commit);
688 committer = got_object_commit_get_committer(commit);
689 if (strcmp(author, committer) != 0)
690 printf("via: %s\n", committer);
691 if (got_object_commit_get_nparents(commit) > 1) {
692 const struct got_object_id_queue *parent_ids;
693 struct got_object_qid *qid;
695 parent_ids = got_object_commit_get_parent_ids(commit);
696 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
697 err = got_object_id_str(&id_str, qid->id);
700 printf("parent %d: %s\n", n++, id_str);
705 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
707 return got_error_from_errno();
711 line = strsep(&logmsg, "\n");
713 printf(" %s\n", line);
718 err = print_patch(commit, id, diff_context, repo);
723 if (fflush(stdout) != 0 && err == NULL)
724 err = got_error_from_errno();
728 static const struct got_error *
729 print_commits(struct got_object_id *root_id, struct got_repository *repo,
730 char *path, int show_patch, int diff_context, int limit,
731 int first_parent_traversal, struct got_reflist_head *refs)
733 const struct got_error *err;
734 struct got_commit_graph *graph;
736 err = got_commit_graph_open(&graph, root_id, path,
737 first_parent_traversal, repo);
740 err = got_commit_graph_iter_start(graph, root_id, repo);
744 struct got_commit_object *commit;
745 struct got_object_id *id;
747 if (sigint_received || sigpipe_received)
750 err = got_commit_graph_iter_next(&id, graph);
752 if (err->code == GOT_ERR_ITER_COMPLETED) {
756 if (err->code != GOT_ERR_ITER_NEED_MORE)
758 err = got_commit_graph_fetch_commits(graph, 1, repo);
767 err = got_object_open_as_commit(&commit, repo, id);
770 err = print_commit(commit, id, repo, show_patch, diff_context,
772 got_object_commit_close(commit);
773 if (err || (limit && --limit == 0))
777 got_commit_graph_close(graph);
784 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
785 "[-r repository-path] [path]\n", getprogname());
789 static const struct got_error *
790 cmd_log(int argc, char *argv[])
792 const struct got_error *error;
793 struct got_repository *repo = NULL;
794 struct got_worktree *worktree = NULL;
795 struct got_commit_object *commit = NULL;
796 struct got_object_id *id = NULL;
797 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
798 char *start_commit = NULL;
799 int diff_context = 3, ch;
800 int show_patch = 0, limit = 0, first_parent_traversal = 0;
802 struct got_reflist_head refs;
807 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
813 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
819 start_commit = optarg;
822 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
825 err(1, "-C option %s", errstr);
828 limit = strtonum(optarg, 1, INT_MAX, &errstr);
830 err(1, "-l option %s", errstr);
833 first_parent_traversal = 1;
836 repo_path = realpath(optarg, NULL);
837 if (repo_path == NULL)
849 cwd = getcwd(NULL, 0);
851 error = got_error_from_errno();
855 error = got_worktree_open(&worktree, cwd);
856 if (error && error->code != GOT_ERR_NOT_WORKTREE)
863 error = got_error_from_errno();
866 } else if (argc == 1) {
868 error = got_worktree_resolve_path(&path, worktree,
873 path = strdup(argv[0]);
875 error = got_error_from_errno();
882 repo_path = worktree ?
883 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
884 if (repo_path == NULL) {
885 error = got_error_from_errno();
889 error = got_repo_open(&repo, repo_path);
893 error = apply_unveil(got_repo_get_path(repo), 1,
894 worktree ? got_worktree_get_root_path(worktree) : NULL);
898 if (start_commit == NULL) {
899 struct got_reference *head_ref;
900 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
903 error = got_ref_resolve(&id, repo, head_ref);
904 got_ref_close(head_ref);
907 error = got_object_open_as_commit(&commit, repo, id);
909 struct got_reference *ref;
910 error = got_ref_open(&ref, repo, start_commit);
913 error = got_ref_resolve(&id, repo, ref);
917 error = got_object_get_type(&obj_type, repo, id);
920 if (obj_type == GOT_OBJ_TYPE_TAG) {
921 struct got_tag_object *tag;
922 error = got_object_open_as_tag(&tag, repo, id);
925 if (got_object_tag_get_object_type(tag) !=
926 GOT_OBJ_TYPE_COMMIT) {
927 got_object_tag_close(tag);
928 error = got_error(GOT_ERR_OBJ_TYPE);
932 id = got_object_id_dup(
933 got_object_tag_get_object_id(tag));
935 error = got_error_from_errno();
936 got_object_tag_close(tag);
939 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
940 error = got_error(GOT_ERR_OBJ_TYPE);
943 error = got_object_open_as_commit(&commit, repo, id);
947 if (commit == NULL) {
948 error = got_object_resolve_id_str(&id, repo,
957 error = got_repo_map_path(&in_repo_path, repo, path, 1);
965 error = got_ref_list(&refs, repo);
969 error = print_commits(id, repo, path, show_patch,
970 diff_context, limit, first_parent_traversal, &refs);
977 got_worktree_close(worktree);
979 const struct got_error *repo_error;
980 repo_error = got_repo_close(repo);
984 got_ref_list_free(&refs);
991 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
992 "[object1 object2 | path]\n", getprogname());
996 struct print_diff_arg {
997 struct got_repository *repo;
998 struct got_worktree *worktree;
1004 static const struct got_error *
1005 print_diff(void *arg, unsigned char status, const char *path,
1006 struct got_object_id *id)
1008 struct print_diff_arg *a = arg;
1009 const struct got_error *err = NULL;
1010 struct got_blob_object *blob1 = NULL;
1012 char *abspath = NULL;
1015 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1016 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1019 if (!a->header_shown) {
1020 printf("diff %s %s\n", a->id_str,
1021 got_worktree_get_root_path(a->worktree));
1022 a->header_shown = 1;
1025 if (status != GOT_STATUS_ADD) {
1026 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1032 if (status != GOT_STATUS_DELETE) {
1033 if (asprintf(&abspath, "%s/%s",
1034 got_worktree_get_root_path(a->worktree), path) == -1) {
1035 err = got_error_from_errno();
1039 f2 = fopen(abspath, "r");
1041 err = got_error_from_errno();
1044 if (lstat(abspath, &sb) == -1) {
1045 err = got_error_from_errno();
1051 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1055 got_object_blob_close(blob1);
1056 if (f2 && fclose(f2) != 0 && err == NULL)
1057 err = got_error_from_errno();
1062 static const struct got_error *
1063 cmd_diff(int argc, char *argv[])
1065 const struct got_error *error;
1066 struct got_repository *repo = NULL;
1067 struct got_worktree *worktree = NULL;
1068 char *cwd = NULL, *repo_path = NULL;
1069 struct got_object_id *id1 = NULL, *id2 = NULL;
1070 char *id_str1 = NULL, *id_str2 = NULL;
1072 int diff_context = 3, ch;
1077 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1082 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1085 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1087 err(1, "-C option %s", errstr);
1090 repo_path = realpath(optarg, NULL);
1091 if (repo_path == NULL)
1092 err(1, "-r option");
1103 cwd = getcwd(NULL, 0);
1105 error = got_error_from_errno();
1108 error = got_worktree_open(&worktree, cwd);
1109 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1112 if (worktree == NULL) {
1113 error = got_error(GOT_ERR_NOT_WORKTREE);
1118 "-r option can't be used when diffing a work tree");
1119 repo_path = strdup(got_worktree_get_repo_path(worktree));
1120 if (repo_path == NULL) {
1121 error = got_error_from_errno();
1125 error = got_worktree_resolve_path(&path, worktree,
1132 error = got_error_from_errno();
1136 } else if (argc == 2) {
1142 if (repo_path == NULL) {
1143 repo_path = getcwd(NULL, 0);
1144 if (repo_path == NULL)
1145 return got_error_from_errno();
1148 error = got_repo_open(&repo, repo_path);
1153 error = apply_unveil(got_repo_get_path(repo), 1,
1154 worktree ? got_worktree_get_root_path(worktree) : NULL);
1159 struct print_diff_arg arg;
1161 error = got_object_id_str(&id_str,
1162 got_worktree_get_base_commit_id(worktree));
1166 arg.worktree = worktree;
1167 arg.diff_context = diff_context;
1168 arg.id_str = id_str;
1169 arg.header_shown = 0;
1171 error = got_worktree_status(worktree, path, repo, print_diff,
1172 &arg, check_cancelled, NULL);
1177 error = got_object_resolve_id_str(&id1, repo, id_str1);
1181 error = got_object_resolve_id_str(&id2, repo, id_str2);
1185 error = got_object_get_type(&type1, repo, id1);
1189 error = got_object_get_type(&type2, repo, id2);
1193 if (type1 != type2) {
1194 error = got_error(GOT_ERR_OBJ_TYPE);
1199 case GOT_OBJ_TYPE_BLOB:
1200 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1201 diff_context, repo, stdout);
1203 case GOT_OBJ_TYPE_TREE:
1204 error = got_diff_objects_as_trees(id1, id2, "", "",
1205 diff_context, repo, stdout);
1207 case GOT_OBJ_TYPE_COMMIT:
1208 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1210 error = got_diff_objects_as_commits(id1, id2, diff_context,
1214 error = got_error(GOT_ERR_OBJ_TYPE);
1222 got_worktree_close(worktree);
1224 const struct got_error *repo_error;
1225 repo_error = got_repo_close(repo);
1236 "usage: %s blame [-c commit] [-r repository-path] path\n",
1241 static const struct got_error *
1242 cmd_blame(int argc, char *argv[])
1244 const struct got_error *error;
1245 struct got_repository *repo = NULL;
1246 struct got_worktree *worktree = NULL;
1247 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1248 struct got_object_id *commit_id = NULL;
1249 char *commit_id_str = NULL;
1253 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1258 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1261 commit_id_str = optarg;
1264 repo_path = realpath(optarg, NULL);
1265 if (repo_path == NULL)
1266 err(1, "-r option");
1282 cwd = getcwd(NULL, 0);
1284 error = got_error_from_errno();
1287 if (repo_path == NULL) {
1288 error = got_worktree_open(&worktree, cwd);
1289 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1295 strdup(got_worktree_get_repo_path(worktree));
1296 if (repo_path == NULL)
1297 error = got_error_from_errno();
1301 repo_path = strdup(cwd);
1302 if (repo_path == NULL) {
1303 error = got_error_from_errno();
1309 error = got_repo_open(&repo, repo_path);
1313 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
1318 const char *prefix = got_worktree_get_path_prefix(worktree);
1319 char *p, *worktree_subdir = cwd +
1320 strlen(got_worktree_get_root_path(worktree));
1321 if (asprintf(&p, "%s%s%s%s%s",
1322 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1323 worktree_subdir, worktree_subdir[0] ? "/" : "",
1325 error = got_error_from_errno();
1328 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1331 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1336 if (commit_id_str == NULL) {
1337 struct got_reference *head_ref;
1338 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1341 error = got_ref_resolve(&commit_id, repo, head_ref);
1342 got_ref_close(head_ref);
1346 error = got_object_resolve_id_str(&commit_id, repo,
1352 error = got_blame(in_repo_path, commit_id, repo, stdout);
1359 got_worktree_close(worktree);
1361 const struct got_error *repo_error;
1362 repo_error = got_repo_close(repo);
1373 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1379 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1380 const char *root_path)
1382 int is_root_path = (strcmp(path, root_path) == 0);
1384 path += strlen(root_path);
1385 while (path[0] == '/')
1388 printf("%s%s%s%s%s\n", id ? id : "", path,
1389 is_root_path ? "" : "/", te->name,
1390 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1393 static const struct got_error *
1394 print_tree(const char *path, struct got_object_id *commit_id,
1395 int show_ids, int recurse, const char *root_path,
1396 struct got_repository *repo)
1398 const struct got_error *err = NULL;
1399 struct got_object_id *tree_id = NULL;
1400 struct got_tree_object *tree = NULL;
1401 const struct got_tree_entries *entries;
1402 struct got_tree_entry *te;
1404 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1408 err = got_object_open_as_tree(&tree, repo, tree_id);
1411 entries = got_object_tree_get_entries(tree);
1412 te = SIMPLEQ_FIRST(&entries->head);
1416 if (sigint_received || sigpipe_received)
1421 err = got_object_id_str(&id_str, te->id);
1424 if (asprintf(&id, "%s ", id_str) == -1) {
1425 err = got_error_from_errno();
1431 print_entry(te, id, path, root_path);
1434 if (recurse && S_ISDIR(te->mode)) {
1436 if (asprintf(&child_path, "%s%s%s", path,
1437 path[0] == '/' && path[1] == '\0' ? "" : "/",
1439 err = got_error_from_errno();
1442 err = print_tree(child_path, commit_id, show_ids, 1,
1449 te = SIMPLEQ_NEXT(te, entry);
1453 got_object_tree_close(tree);
1458 static const struct got_error *
1459 cmd_tree(int argc, char *argv[])
1461 const struct got_error *error;
1462 struct got_repository *repo = NULL;
1463 struct got_worktree *worktree = NULL;
1465 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1466 struct got_object_id *commit_id = NULL;
1467 char *commit_id_str = NULL;
1468 int show_ids = 0, recurse = 0;
1472 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1477 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1480 commit_id_str = optarg;
1483 repo_path = realpath(optarg, NULL);
1484 if (repo_path == NULL)
1485 err(1, "-r option");
1509 cwd = getcwd(NULL, 0);
1511 error = got_error_from_errno();
1514 if (repo_path == NULL) {
1515 error = got_worktree_open(&worktree, cwd);
1516 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1522 strdup(got_worktree_get_repo_path(worktree));
1523 if (repo_path == NULL)
1524 error = got_error_from_errno();
1528 repo_path = strdup(cwd);
1529 if (repo_path == NULL) {
1530 error = got_error_from_errno();
1536 error = got_repo_open(&repo, repo_path);
1540 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
1546 char *p, *worktree_subdir = cwd +
1547 strlen(got_worktree_get_root_path(worktree));
1548 if (asprintf(&p, "%s/%s",
1549 got_worktree_get_path_prefix(worktree),
1550 worktree_subdir) == -1) {
1551 error = got_error_from_errno();
1554 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1561 if (in_repo_path == NULL) {
1562 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1567 if (commit_id_str == NULL) {
1568 struct got_reference *head_ref;
1569 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1572 error = got_ref_resolve(&commit_id, repo, head_ref);
1573 got_ref_close(head_ref);
1577 error = got_object_resolve_id_str(&commit_id, repo,
1583 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1584 in_repo_path, repo);
1591 got_worktree_close(worktree);
1593 const struct got_error *repo_error;
1594 repo_error = got_repo_close(repo);
1604 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1608 static const struct got_error *
1609 print_status(void *arg, unsigned char status, const char *path,
1610 struct got_object_id *id)
1612 printf("%c %s\n", status, path);
1616 static const struct got_error *
1617 cmd_status(int argc, char *argv[])
1619 const struct got_error *error = NULL;
1620 struct got_repository *repo = NULL;
1621 struct got_worktree *worktree = NULL;
1622 char *cwd = NULL, *path = NULL;
1625 while ((ch = getopt(argc, argv, "")) != -1) {
1637 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1641 cwd = getcwd(NULL, 0);
1643 error = got_error_from_errno();
1647 error = got_worktree_open(&worktree, cwd);
1654 error = got_error_from_errno();
1657 } else if (argc == 1) {
1658 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1664 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1668 error = apply_unveil(got_repo_get_path(repo), 1,
1669 got_worktree_get_root_path(worktree));
1673 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1674 check_cancelled, NULL);
1685 "usage: %s ref [-r repository] -l | -d name | name object\n",
1690 static const struct got_error *
1691 list_refs(struct got_repository *repo)
1693 static const struct got_error *err = NULL;
1694 struct got_reflist_head refs;
1695 struct got_reflist_entry *re;
1697 SIMPLEQ_INIT(&refs);
1698 err = got_ref_list(&refs, repo);
1702 SIMPLEQ_FOREACH(re, &refs, entry) {
1704 refstr = got_ref_to_str(re->ref);
1706 return got_error_from_errno();
1707 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1711 got_ref_list_free(&refs);
1715 static const struct got_error *
1716 delete_ref(struct got_repository *repo, const char *refname)
1718 const struct got_error *err = NULL;
1719 struct got_reference *ref;
1721 err = got_ref_open(&ref, repo, refname);
1725 err = got_ref_delete(ref, repo);
1730 static const struct got_error *
1731 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1733 const struct got_error *err = NULL;
1734 struct got_object_id *id;
1735 struct got_reference *ref = NULL;
1737 err = got_object_resolve_id_str(&id, repo, id_str);
1741 err = got_ref_alloc(&ref, refname, id);
1745 err = got_ref_write(ref, repo);
1753 static const struct got_error *
1754 cmd_ref(int argc, char *argv[])
1756 const struct got_error *error = NULL;
1757 struct got_repository *repo = NULL;
1758 struct got_worktree *worktree = NULL;
1759 char *cwd = NULL, *repo_path = NULL;
1760 int ch, do_list = 0;
1761 const char *delref = NULL;
1763 /* TODO: Add -s option for adding symbolic references. */
1764 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1770 repo_path = realpath(optarg, NULL);
1771 if (repo_path == NULL)
1772 err(1, "-r option");
1783 if (do_list && delref)
1784 errx(1, "-l and -d options are mutually exclusive\n");
1789 if (do_list || delref) {
1792 } else if (argc != 2)
1797 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1801 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1802 "sendfd unveil", NULL) == -1)
1806 cwd = getcwd(NULL, 0);
1808 error = got_error_from_errno();
1812 if (repo_path == NULL) {
1813 error = got_worktree_open(&worktree, cwd);
1814 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1820 strdup(got_worktree_get_repo_path(worktree));
1821 if (repo_path == NULL)
1822 error = got_error_from_errno();
1826 repo_path = strdup(cwd);
1827 if (repo_path == NULL) {
1828 error = got_error_from_errno();
1834 error = got_repo_open(&repo, repo_path);
1838 error = apply_unveil(got_repo_get_path(repo), do_list,
1839 worktree ? got_worktree_get_root_path(worktree) : NULL);
1844 error = list_refs(repo);
1846 error = delete_ref(repo, delref);
1848 error = add_ref(repo, argv[0], argv[1]);
1851 got_repo_close(repo);
1853 got_worktree_close(worktree);
1862 fprintf(stderr, "usage: %s add file-path\n", getprogname());
1866 static const struct got_error *
1867 cmd_add(int argc, char *argv[])
1869 const struct got_error *error = NULL;
1870 struct got_repository *repo = NULL;
1871 struct got_worktree *worktree = NULL;
1872 char *cwd = NULL, *path = NULL, *relpath = NULL;
1875 while ((ch = getopt(argc, argv, "")) != -1) {
1889 path = realpath(argv[0], NULL);
1891 error = got_error_from_errno();
1895 cwd = getcwd(NULL, 0);
1897 error = got_error_from_errno();
1900 error = got_worktree_open(&worktree, cwd);
1904 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1908 error = apply_unveil(got_repo_get_path(repo), 1,
1909 got_worktree_get_root_path(worktree));
1913 error = got_worktree_schedule_add(worktree, path, print_status, NULL,
1919 got_repo_close(repo);
1921 got_worktree_close(worktree);
1931 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1935 static const struct got_error *
1936 cmd_rm(int argc, char *argv[])
1938 const struct got_error *error = NULL;
1939 struct got_worktree *worktree = NULL;
1940 struct got_repository *repo = NULL;
1941 char *cwd = NULL, *path = NULL;
1942 int ch, delete_local_mods = 0;
1944 while ((ch = getopt(argc, argv, "f")) != -1) {
1947 delete_local_mods = 1;
1961 path = realpath(argv[0], NULL);
1963 error = got_error_from_errno();
1967 cwd = getcwd(NULL, 0);
1969 error = got_error_from_errno();
1972 error = got_worktree_open(&worktree, cwd);
1976 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1980 error = apply_unveil(got_repo_get_path(repo), 1,
1981 got_worktree_get_root_path(worktree));
1985 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
1986 print_status, NULL, repo);
1991 got_repo_close(repo);
1993 got_worktree_close(worktree);
2002 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2007 revert_progress(void *arg, unsigned char status, const char *path)
2009 while (path[0] == '/')
2011 printf("%c %s\n", status, path);
2014 static const struct got_error *
2015 cmd_revert(int argc, char *argv[])
2017 const struct got_error *error = NULL;
2018 struct got_worktree *worktree = NULL;
2019 struct got_repository *repo = NULL;
2020 char *cwd = NULL, *path = NULL;
2023 while ((ch = getopt(argc, argv, "")) != -1) {
2037 path = realpath(argv[0], NULL);
2039 error = got_error_from_errno();
2043 cwd = getcwd(NULL, 0);
2045 error = got_error_from_errno();
2048 error = got_worktree_open(&worktree, cwd);
2052 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2056 error = apply_unveil(got_repo_get_path(repo), 1,
2057 got_worktree_get_root_path(worktree));
2061 error = got_worktree_revert(worktree, path,
2062 revert_progress, NULL, repo);
2067 got_repo_close(repo);
2069 got_worktree_close(worktree);
2078 fprintf(stderr, "usage: %s commit file-path\n", getprogname());
2082 static const struct got_error *
2083 cmd_commit(int argc, char *argv[])
2085 const struct got_error *error = NULL;
2086 struct got_worktree *worktree = NULL;
2087 struct got_repository *repo = NULL;
2088 char *cwd = NULL, *path = NULL, *id_str = NULL;
2089 struct got_object_id *id = NULL;
2090 const char *logmsg = "<no log message was specified>";
2093 while ((ch = getopt(argc, argv, "m:")) != -1) {
2108 path = realpath(argv[0], NULL);
2110 error = got_error_from_errno();
2113 } else if (argc != 0)
2117 cwd = getcwd(NULL, 0);
2119 error = got_error_from_errno();
2122 error = got_worktree_open(&worktree, cwd);
2126 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2130 error = apply_unveil(got_repo_get_path(repo), 0,
2131 got_worktree_get_root_path(worktree));
2135 error = got_worktree_commit(&id, worktree, path, logmsg, repo);
2139 error = got_object_id_str(&id_str, id);
2142 printf("created commit %s\n", id_str);
2145 got_repo_close(repo);
2147 got_worktree_close(worktree);