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 "write changes from work tree to repository" },
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 err = got_ref_open(&head_ref, repo,
256 got_worktree_get_head_ref_name(worktree));
260 /* TODO: Check the reflog. The head ref may have been rebased. */
261 err = got_ref_resolve(&head_commit_id, repo, head_ref);
265 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
269 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
273 struct got_object_id *id;
275 if (sigint_received || sigpipe_received)
278 err = got_commit_graph_iter_next(&id, graph);
280 if (err->code == GOT_ERR_ITER_COMPLETED) {
281 err = got_error(GOT_ERR_ANCESTRY);
284 if (err->code != GOT_ERR_ITER_NEED_MORE)
286 err = got_commit_graph_fetch_commits(graph, 1, repo);
294 if (got_object_id_cmp(id, commit_id) == 0)
299 got_ref_close(head_ref);
301 got_commit_graph_close(graph);
306 static const struct got_error *
307 cmd_checkout(int argc, char *argv[])
309 const struct got_error *error = NULL;
310 struct got_repository *repo = NULL;
311 struct got_reference *head_ref = NULL;
312 struct got_worktree *worktree = NULL;
313 char *repo_path = NULL;
314 char *worktree_path = NULL;
315 const char *path_prefix = "";
316 char *commit_id_str = NULL;
317 int ch, same_path_prefix, x;
319 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
322 commit_id_str = strdup(optarg);
323 if (commit_id_str == NULL)
324 return got_error_from_errno();
327 path_prefix = optarg;
339 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
340 "unveil", NULL) == -1)
344 char *cwd, *base, *dotgit;
345 repo_path = realpath(argv[0], NULL);
346 if (repo_path == NULL)
347 return got_error_from_errno();
348 cwd = getcwd(NULL, 0);
350 error = got_error_from_errno();
354 base = basename(path_prefix);
356 base = basename(repo_path);
358 error = got_error_from_errno();
361 dotgit = strstr(base, ".git");
364 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
365 error = got_error_from_errno();
370 } else if (argc == 2) {
371 repo_path = realpath(argv[0], NULL);
372 if (repo_path == NULL) {
373 error = got_error_from_errno();
376 worktree_path = realpath(argv[1], NULL);
377 if (worktree_path == NULL) {
378 error = got_error_from_errno();
384 if (worktree_path[x = strlen(worktree_path) - 1] == '/')
385 worktree_path[x] = '\0';
387 error = got_repo_open(&repo, repo_path);
391 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
395 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
399 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
400 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
403 error = got_worktree_open(&worktree, worktree_path);
407 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
411 if (!same_path_prefix) {
412 error = got_error(GOT_ERR_PATH_PREFIX);
417 struct got_object_id *commit_id;
418 error = got_object_resolve_id_str(&commit_id, repo,
422 error = check_ancestry(worktree, commit_id, repo);
427 error = got_worktree_set_base_commit_id(worktree, repo,
434 error = got_worktree_checkout_files(worktree, "", repo,
435 checkout_progress, worktree_path, check_cancelled, NULL);
439 printf("Now shut up and hack\n");
451 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
457 update_progress(void *arg, unsigned char status, const char *path)
459 int *did_something = arg;
461 if (status == GOT_STATUS_EXISTS)
465 while (path[0] == '/')
467 printf("%c %s\n", status, path);
470 static const struct got_error *
471 cmd_update(int argc, char *argv[])
473 const struct got_error *error = NULL;
474 struct got_repository *repo = NULL;
475 struct got_worktree *worktree = NULL;
476 char *worktree_path = NULL, *path = NULL;
477 struct got_object_id *commit_id = NULL;
478 char *commit_id_str = NULL;
479 int ch, did_something = 0;
481 while ((ch = getopt(argc, argv, "c:")) != -1) {
484 commit_id_str = strdup(optarg);
485 if (commit_id_str == NULL)
486 return got_error_from_errno();
498 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
499 "unveil", NULL) == -1)
502 worktree_path = getcwd(NULL, 0);
503 if (worktree_path == NULL) {
504 error = got_error_from_errno();
507 error = got_worktree_open(&worktree, worktree_path);
514 error = got_error_from_errno();
517 } else if (argc == 1) {
518 error = got_worktree_resolve_path(&path, worktree, argv[0]);
524 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
528 error = apply_unveil(got_repo_get_path(repo), 0,
529 got_worktree_get_root_path(worktree));
533 if (commit_id_str == NULL) {
534 struct got_reference *head_ref;
535 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
538 error = got_ref_resolve(&commit_id, repo, head_ref);
541 error = got_object_id_str(&commit_id_str, commit_id);
545 error = got_object_resolve_id_str(&commit_id, repo,
551 error = check_ancestry(worktree, commit_id, repo);
555 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
557 error = got_worktree_set_base_commit_id(worktree, repo,
563 error = got_worktree_checkout_files(worktree, path, repo,
564 update_progress, &did_something, check_cancelled, NULL);
569 printf("Updated to commit %s\n", commit_id_str);
571 printf("Already up-to-date\n");
580 static const struct got_error *
581 print_patch(struct got_commit_object *commit, struct got_object_id *id,
582 int diff_context, struct got_repository *repo)
584 const struct got_error *err = NULL;
585 struct got_tree_object *tree1 = NULL, *tree2;
586 struct got_object_qid *qid;
587 char *id_str1 = NULL, *id_str2;
589 err = got_object_open_as_tree(&tree2, repo,
590 got_object_commit_get_tree_id(commit));
594 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
596 struct got_commit_object *pcommit;
598 err = got_object_open_as_commit(&pcommit, repo, qid->id);
602 err = got_object_open_as_tree(&tree1, repo,
603 got_object_commit_get_tree_id(pcommit));
604 got_object_commit_close(pcommit);
608 err = got_object_id_str(&id_str1, qid->id);
613 err = got_object_id_str(&id_str2, id);
617 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
618 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
621 got_object_tree_close(tree1);
622 got_object_tree_close(tree2);
629 get_datestr(time_t *time, char *datebuf)
631 char *p, *s = ctime_r(time, datebuf);
638 static const struct got_error *
639 print_commit(struct got_commit_object *commit, struct got_object_id *id,
640 struct got_repository *repo, int show_patch, int diff_context,
641 struct got_reflist_head *refs)
643 const struct got_error *err = NULL;
644 char *id_str, *datestr, *logmsg0, *logmsg, *line;
646 time_t committer_time;
647 const char *author, *committer;
648 char *refs_str = NULL;
649 struct got_reflist_entry *re;
651 SIMPLEQ_FOREACH(re, refs, entry) {
654 if (got_object_id_cmp(re->id, id) != 0)
656 name = got_ref_get_name(re->ref);
657 if (strcmp(name, GOT_REF_HEAD) == 0)
659 if (strncmp(name, "refs/", 5) == 0)
661 if (strncmp(name, "got/", 4) == 0)
663 if (strncmp(name, "heads/", 6) == 0)
665 if (strncmp(name, "remotes/", 8) == 0)
668 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
670 err = got_error_from_errno();
676 err = got_object_id_str(&id_str, id);
680 printf("-----------------------------------------------\n");
681 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
682 refs_str ? refs_str : "", refs_str ? ")" : "");
687 printf("from: %s\n", got_object_commit_get_author(commit));
688 committer_time = got_object_commit_get_committer_time(commit);
689 datestr = get_datestr(&committer_time, datebuf);
690 printf("date: %s UTC\n", datestr);
691 author = got_object_commit_get_author(commit);
692 committer = got_object_commit_get_committer(commit);
693 if (strcmp(author, committer) != 0)
694 printf("via: %s\n", committer);
695 if (got_object_commit_get_nparents(commit) > 1) {
696 const struct got_object_id_queue *parent_ids;
697 struct got_object_qid *qid;
699 parent_ids = got_object_commit_get_parent_ids(commit);
700 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
701 err = got_object_id_str(&id_str, qid->id);
704 printf("parent %d: %s\n", n++, id_str);
709 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
711 return got_error_from_errno();
715 line = strsep(&logmsg, "\n");
717 printf(" %s\n", line);
722 err = print_patch(commit, id, diff_context, repo);
727 if (fflush(stdout) != 0 && err == NULL)
728 err = got_error_from_errno();
732 static const struct got_error *
733 print_commits(struct got_object_id *root_id, struct got_repository *repo,
734 char *path, int show_patch, int diff_context, int limit,
735 int first_parent_traversal, struct got_reflist_head *refs)
737 const struct got_error *err;
738 struct got_commit_graph *graph;
740 err = got_commit_graph_open(&graph, root_id, path,
741 first_parent_traversal, repo);
744 err = got_commit_graph_iter_start(graph, root_id, repo);
748 struct got_commit_object *commit;
749 struct got_object_id *id;
751 if (sigint_received || sigpipe_received)
754 err = got_commit_graph_iter_next(&id, graph);
756 if (err->code == GOT_ERR_ITER_COMPLETED) {
760 if (err->code != GOT_ERR_ITER_NEED_MORE)
762 err = got_commit_graph_fetch_commits(graph, 1, repo);
771 err = got_object_open_as_commit(&commit, repo, id);
774 err = print_commit(commit, id, repo, show_patch, diff_context,
776 got_object_commit_close(commit);
777 if (err || (limit && --limit == 0))
781 got_commit_graph_close(graph);
788 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
789 "[-r repository-path] [path]\n", getprogname());
793 static const struct got_error *
794 cmd_log(int argc, char *argv[])
796 const struct got_error *error;
797 struct got_repository *repo = NULL;
798 struct got_worktree *worktree = NULL;
799 struct got_commit_object *commit = NULL;
800 struct got_object_id *id = NULL;
801 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
802 char *start_commit = NULL;
803 int diff_context = 3, ch;
804 int show_patch = 0, limit = 0, first_parent_traversal = 0;
806 struct got_reflist_head refs;
811 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
817 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
823 start_commit = optarg;
826 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
829 err(1, "-C option %s", errstr);
832 limit = strtonum(optarg, 1, INT_MAX, &errstr);
834 err(1, "-l option %s", errstr);
837 first_parent_traversal = 1;
840 repo_path = realpath(optarg, NULL);
841 if (repo_path == NULL)
853 cwd = getcwd(NULL, 0);
855 error = got_error_from_errno();
859 error = got_worktree_open(&worktree, cwd);
860 if (error && error->code != GOT_ERR_NOT_WORKTREE)
867 error = got_error_from_errno();
870 } else if (argc == 1) {
872 error = got_worktree_resolve_path(&path, worktree,
877 path = strdup(argv[0]);
879 error = got_error_from_errno();
886 repo_path = worktree ?
887 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
888 if (repo_path == NULL) {
889 error = got_error_from_errno();
893 error = got_repo_open(&repo, repo_path);
897 error = apply_unveil(got_repo_get_path(repo), 1,
898 worktree ? got_worktree_get_root_path(worktree) : NULL);
902 if (start_commit == NULL) {
903 struct got_reference *head_ref;
904 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
907 error = got_ref_resolve(&id, repo, head_ref);
908 got_ref_close(head_ref);
911 error = got_object_open_as_commit(&commit, repo, id);
913 struct got_reference *ref;
914 error = got_ref_open(&ref, repo, start_commit);
917 error = got_ref_resolve(&id, repo, ref);
921 error = got_object_get_type(&obj_type, repo, id);
924 if (obj_type == GOT_OBJ_TYPE_TAG) {
925 struct got_tag_object *tag;
926 error = got_object_open_as_tag(&tag, repo, id);
929 if (got_object_tag_get_object_type(tag) !=
930 GOT_OBJ_TYPE_COMMIT) {
931 got_object_tag_close(tag);
932 error = got_error(GOT_ERR_OBJ_TYPE);
936 id = got_object_id_dup(
937 got_object_tag_get_object_id(tag));
939 error = got_error_from_errno();
940 got_object_tag_close(tag);
943 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
944 error = got_error(GOT_ERR_OBJ_TYPE);
947 error = got_object_open_as_commit(&commit, repo, id);
951 if (commit == NULL) {
952 error = got_object_resolve_id_str(&id, repo,
961 error = got_repo_map_path(&in_repo_path, repo, path, 1);
969 error = got_ref_list(&refs, repo);
973 error = print_commits(id, repo, path, show_patch,
974 diff_context, limit, first_parent_traversal, &refs);
981 got_worktree_close(worktree);
983 const struct got_error *repo_error;
984 repo_error = got_repo_close(repo);
988 got_ref_list_free(&refs);
995 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
996 "[object1 object2 | path]\n", getprogname());
1000 struct print_diff_arg {
1001 struct got_repository *repo;
1002 struct got_worktree *worktree;
1008 static const struct got_error *
1009 print_diff(void *arg, unsigned char status, const char *path,
1010 struct got_object_id *id)
1012 struct print_diff_arg *a = arg;
1013 const struct got_error *err = NULL;
1014 struct got_blob_object *blob1 = NULL;
1016 char *abspath = NULL;
1019 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1020 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1023 if (!a->header_shown) {
1024 printf("diff %s %s\n", a->id_str,
1025 got_worktree_get_root_path(a->worktree));
1026 a->header_shown = 1;
1029 if (status != GOT_STATUS_ADD) {
1030 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1036 if (status != GOT_STATUS_DELETE) {
1037 if (asprintf(&abspath, "%s/%s",
1038 got_worktree_get_root_path(a->worktree), path) == -1) {
1039 err = got_error_from_errno();
1043 f2 = fopen(abspath, "r");
1045 err = got_error_from_errno();
1048 if (lstat(abspath, &sb) == -1) {
1049 err = got_error_from_errno();
1055 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1059 got_object_blob_close(blob1);
1060 if (f2 && fclose(f2) != 0 && err == NULL)
1061 err = got_error_from_errno();
1066 static const struct got_error *
1067 cmd_diff(int argc, char *argv[])
1069 const struct got_error *error;
1070 struct got_repository *repo = NULL;
1071 struct got_worktree *worktree = NULL;
1072 char *cwd = NULL, *repo_path = NULL;
1073 struct got_object_id *id1 = NULL, *id2 = NULL;
1074 char *id_str1 = NULL, *id_str2 = NULL;
1076 int diff_context = 3, ch;
1081 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1086 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1089 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1091 err(1, "-C option %s", errstr);
1094 repo_path = realpath(optarg, NULL);
1095 if (repo_path == NULL)
1096 err(1, "-r option");
1107 cwd = getcwd(NULL, 0);
1109 error = got_error_from_errno();
1112 error = got_worktree_open(&worktree, cwd);
1113 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1116 if (worktree == NULL) {
1117 error = got_error(GOT_ERR_NOT_WORKTREE);
1122 "-r option can't be used when diffing a work tree");
1123 repo_path = strdup(got_worktree_get_repo_path(worktree));
1124 if (repo_path == NULL) {
1125 error = got_error_from_errno();
1129 error = got_worktree_resolve_path(&path, worktree,
1136 error = got_error_from_errno();
1140 } else if (argc == 2) {
1146 if (repo_path == NULL) {
1147 repo_path = getcwd(NULL, 0);
1148 if (repo_path == NULL)
1149 return got_error_from_errno();
1152 error = got_repo_open(&repo, repo_path);
1157 error = apply_unveil(got_repo_get_path(repo), 1,
1158 worktree ? got_worktree_get_root_path(worktree) : NULL);
1163 struct print_diff_arg arg;
1165 error = got_object_id_str(&id_str,
1166 got_worktree_get_base_commit_id(worktree));
1170 arg.worktree = worktree;
1171 arg.diff_context = diff_context;
1172 arg.id_str = id_str;
1173 arg.header_shown = 0;
1175 error = got_worktree_status(worktree, path, repo, print_diff,
1176 &arg, check_cancelled, NULL);
1181 error = got_object_resolve_id_str(&id1, repo, id_str1);
1185 error = got_object_resolve_id_str(&id2, repo, id_str2);
1189 error = got_object_get_type(&type1, repo, id1);
1193 error = got_object_get_type(&type2, repo, id2);
1197 if (type1 != type2) {
1198 error = got_error(GOT_ERR_OBJ_TYPE);
1203 case GOT_OBJ_TYPE_BLOB:
1204 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1205 diff_context, repo, stdout);
1207 case GOT_OBJ_TYPE_TREE:
1208 error = got_diff_objects_as_trees(id1, id2, "", "",
1209 diff_context, repo, stdout);
1211 case GOT_OBJ_TYPE_COMMIT:
1212 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1214 error = got_diff_objects_as_commits(id1, id2, diff_context,
1218 error = got_error(GOT_ERR_OBJ_TYPE);
1226 got_worktree_close(worktree);
1228 const struct got_error *repo_error;
1229 repo_error = got_repo_close(repo);
1240 "usage: %s blame [-c commit] [-r repository-path] path\n",
1245 static const struct got_error *
1246 cmd_blame(int argc, char *argv[])
1248 const struct got_error *error;
1249 struct got_repository *repo = NULL;
1250 struct got_worktree *worktree = NULL;
1251 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1252 struct got_object_id *commit_id = NULL;
1253 char *commit_id_str = NULL;
1257 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1262 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1265 commit_id_str = optarg;
1268 repo_path = realpath(optarg, NULL);
1269 if (repo_path == NULL)
1270 err(1, "-r option");
1286 cwd = getcwd(NULL, 0);
1288 error = got_error_from_errno();
1291 if (repo_path == NULL) {
1292 error = got_worktree_open(&worktree, cwd);
1293 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1299 strdup(got_worktree_get_repo_path(worktree));
1300 if (repo_path == NULL)
1301 error = got_error_from_errno();
1305 repo_path = strdup(cwd);
1306 if (repo_path == NULL) {
1307 error = got_error_from_errno();
1313 error = got_repo_open(&repo, repo_path);
1317 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
1322 const char *prefix = got_worktree_get_path_prefix(worktree);
1323 char *p, *worktree_subdir = cwd +
1324 strlen(got_worktree_get_root_path(worktree));
1325 if (asprintf(&p, "%s%s%s%s%s",
1326 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1327 worktree_subdir, worktree_subdir[0] ? "/" : "",
1329 error = got_error_from_errno();
1332 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1335 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1340 if (commit_id_str == NULL) {
1341 struct got_reference *head_ref;
1342 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1345 error = got_ref_resolve(&commit_id, repo, head_ref);
1346 got_ref_close(head_ref);
1350 error = got_object_resolve_id_str(&commit_id, repo,
1356 error = got_blame(in_repo_path, commit_id, repo, stdout);
1363 got_worktree_close(worktree);
1365 const struct got_error *repo_error;
1366 repo_error = got_repo_close(repo);
1377 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1383 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1384 const char *root_path)
1386 int is_root_path = (strcmp(path, root_path) == 0);
1388 path += strlen(root_path);
1389 while (path[0] == '/')
1392 printf("%s%s%s%s%s\n", id ? id : "", path,
1393 is_root_path ? "" : "/", te->name,
1394 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1397 static const struct got_error *
1398 print_tree(const char *path, struct got_object_id *commit_id,
1399 int show_ids, int recurse, const char *root_path,
1400 struct got_repository *repo)
1402 const struct got_error *err = NULL;
1403 struct got_object_id *tree_id = NULL;
1404 struct got_tree_object *tree = NULL;
1405 const struct got_tree_entries *entries;
1406 struct got_tree_entry *te;
1408 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1412 err = got_object_open_as_tree(&tree, repo, tree_id);
1415 entries = got_object_tree_get_entries(tree);
1416 te = SIMPLEQ_FIRST(&entries->head);
1420 if (sigint_received || sigpipe_received)
1425 err = got_object_id_str(&id_str, te->id);
1428 if (asprintf(&id, "%s ", id_str) == -1) {
1429 err = got_error_from_errno();
1435 print_entry(te, id, path, root_path);
1438 if (recurse && S_ISDIR(te->mode)) {
1440 if (asprintf(&child_path, "%s%s%s", path,
1441 path[0] == '/' && path[1] == '\0' ? "" : "/",
1443 err = got_error_from_errno();
1446 err = print_tree(child_path, commit_id, show_ids, 1,
1453 te = SIMPLEQ_NEXT(te, entry);
1457 got_object_tree_close(tree);
1462 static const struct got_error *
1463 cmd_tree(int argc, char *argv[])
1465 const struct got_error *error;
1466 struct got_repository *repo = NULL;
1467 struct got_worktree *worktree = NULL;
1469 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1470 struct got_object_id *commit_id = NULL;
1471 char *commit_id_str = NULL;
1472 int show_ids = 0, recurse = 0;
1476 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1481 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1484 commit_id_str = optarg;
1487 repo_path = realpath(optarg, NULL);
1488 if (repo_path == NULL)
1489 err(1, "-r option");
1513 cwd = getcwd(NULL, 0);
1515 error = got_error_from_errno();
1518 if (repo_path == NULL) {
1519 error = got_worktree_open(&worktree, cwd);
1520 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1526 strdup(got_worktree_get_repo_path(worktree));
1527 if (repo_path == NULL)
1528 error = got_error_from_errno();
1532 repo_path = strdup(cwd);
1533 if (repo_path == NULL) {
1534 error = got_error_from_errno();
1540 error = got_repo_open(&repo, repo_path);
1544 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
1550 char *p, *worktree_subdir = cwd +
1551 strlen(got_worktree_get_root_path(worktree));
1552 if (asprintf(&p, "%s/%s",
1553 got_worktree_get_path_prefix(worktree),
1554 worktree_subdir) == -1) {
1555 error = got_error_from_errno();
1558 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1565 if (in_repo_path == NULL) {
1566 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1571 if (commit_id_str == NULL) {
1572 struct got_reference *head_ref;
1573 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1576 error = got_ref_resolve(&commit_id, repo, head_ref);
1577 got_ref_close(head_ref);
1581 error = got_object_resolve_id_str(&commit_id, repo,
1587 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1588 in_repo_path, repo);
1595 got_worktree_close(worktree);
1597 const struct got_error *repo_error;
1598 repo_error = got_repo_close(repo);
1608 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1612 static const struct got_error *
1613 print_status(void *arg, unsigned char status, const char *path,
1614 struct got_object_id *id)
1616 printf("%c %s\n", status, path);
1620 static const struct got_error *
1621 cmd_status(int argc, char *argv[])
1623 const struct got_error *error = NULL;
1624 struct got_repository *repo = NULL;
1625 struct got_worktree *worktree = NULL;
1626 char *cwd = NULL, *path = NULL;
1629 while ((ch = getopt(argc, argv, "")) != -1) {
1641 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1645 cwd = getcwd(NULL, 0);
1647 error = got_error_from_errno();
1651 error = got_worktree_open(&worktree, cwd);
1658 error = got_error_from_errno();
1661 } else if (argc == 1) {
1662 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1668 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1672 error = apply_unveil(got_repo_get_path(repo), 1,
1673 got_worktree_get_root_path(worktree));
1677 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1678 check_cancelled, NULL);
1689 "usage: %s ref [-r repository] -l | -d name | name object\n",
1694 static const struct got_error *
1695 list_refs(struct got_repository *repo)
1697 static const struct got_error *err = NULL;
1698 struct got_reflist_head refs;
1699 struct got_reflist_entry *re;
1701 SIMPLEQ_INIT(&refs);
1702 err = got_ref_list(&refs, repo);
1706 SIMPLEQ_FOREACH(re, &refs, entry) {
1708 refstr = got_ref_to_str(re->ref);
1710 return got_error_from_errno();
1711 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1715 got_ref_list_free(&refs);
1719 static const struct got_error *
1720 delete_ref(struct got_repository *repo, const char *refname)
1722 const struct got_error *err = NULL;
1723 struct got_reference *ref;
1725 err = got_ref_open(&ref, repo, refname);
1729 err = got_ref_delete(ref, repo);
1734 static const struct got_error *
1735 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1737 const struct got_error *err = NULL;
1738 struct got_object_id *id;
1739 struct got_reference *ref = NULL;
1741 err = got_object_resolve_id_str(&id, repo, id_str);
1745 err = got_ref_alloc(&ref, refname, id);
1749 err = got_ref_write(ref, repo);
1757 static const struct got_error *
1758 cmd_ref(int argc, char *argv[])
1760 const struct got_error *error = NULL;
1761 struct got_repository *repo = NULL;
1762 struct got_worktree *worktree = NULL;
1763 char *cwd = NULL, *repo_path = NULL;
1764 int ch, do_list = 0;
1765 const char *delref = NULL;
1767 /* TODO: Add -s option for adding symbolic references. */
1768 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1774 repo_path = realpath(optarg, NULL);
1775 if (repo_path == NULL)
1776 err(1, "-r option");
1787 if (do_list && delref)
1788 errx(1, "-l and -d options are mutually exclusive\n");
1793 if (do_list || delref) {
1796 } else if (argc != 2)
1801 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1805 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1806 "sendfd unveil", NULL) == -1)
1810 cwd = getcwd(NULL, 0);
1812 error = got_error_from_errno();
1816 if (repo_path == NULL) {
1817 error = got_worktree_open(&worktree, cwd);
1818 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1824 strdup(got_worktree_get_repo_path(worktree));
1825 if (repo_path == NULL)
1826 error = got_error_from_errno();
1830 repo_path = strdup(cwd);
1831 if (repo_path == NULL) {
1832 error = got_error_from_errno();
1838 error = got_repo_open(&repo, repo_path);
1842 error = apply_unveil(got_repo_get_path(repo), do_list,
1843 worktree ? got_worktree_get_root_path(worktree) : NULL);
1848 error = list_refs(repo);
1850 error = delete_ref(repo, delref);
1852 error = add_ref(repo, argv[0], argv[1]);
1855 got_repo_close(repo);
1857 got_worktree_close(worktree);
1866 fprintf(stderr, "usage: %s add file-path\n", getprogname());
1870 static const struct got_error *
1871 cmd_add(int argc, char *argv[])
1873 const struct got_error *error = NULL;
1874 struct got_repository *repo = NULL;
1875 struct got_worktree *worktree = NULL;
1876 char *cwd = NULL, *path = NULL, *relpath = NULL;
1879 while ((ch = getopt(argc, argv, "")) != -1) {
1893 path = realpath(argv[0], NULL);
1895 error = got_error_from_errno();
1899 cwd = getcwd(NULL, 0);
1901 error = got_error_from_errno();
1904 error = got_worktree_open(&worktree, cwd);
1908 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1912 error = apply_unveil(got_repo_get_path(repo), 1,
1913 got_worktree_get_root_path(worktree));
1917 error = got_worktree_schedule_add(worktree, path, print_status, NULL,
1923 got_repo_close(repo);
1925 got_worktree_close(worktree);
1935 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1939 static const struct got_error *
1940 cmd_rm(int argc, char *argv[])
1942 const struct got_error *error = NULL;
1943 struct got_worktree *worktree = NULL;
1944 struct got_repository *repo = NULL;
1945 char *cwd = NULL, *path = NULL;
1946 int ch, delete_local_mods = 0;
1948 while ((ch = getopt(argc, argv, "f")) != -1) {
1951 delete_local_mods = 1;
1965 path = realpath(argv[0], NULL);
1967 error = got_error_from_errno();
1971 cwd = getcwd(NULL, 0);
1973 error = got_error_from_errno();
1976 error = got_worktree_open(&worktree, cwd);
1980 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1984 error = apply_unveil(got_repo_get_path(repo), 1,
1985 got_worktree_get_root_path(worktree));
1989 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
1990 print_status, NULL, repo);
1995 got_repo_close(repo);
1997 got_worktree_close(worktree);
2006 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2011 revert_progress(void *arg, unsigned char status, const char *path)
2013 while (path[0] == '/')
2015 printf("%c %s\n", status, path);
2018 static const struct got_error *
2019 cmd_revert(int argc, char *argv[])
2021 const struct got_error *error = NULL;
2022 struct got_worktree *worktree = NULL;
2023 struct got_repository *repo = NULL;
2024 char *cwd = NULL, *path = NULL;
2027 while ((ch = getopt(argc, argv, "")) != -1) {
2041 path = realpath(argv[0], NULL);
2043 error = got_error_from_errno();
2047 cwd = getcwd(NULL, 0);
2049 error = got_error_from_errno();
2052 error = got_worktree_open(&worktree, cwd);
2056 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2060 error = apply_unveil(got_repo_get_path(repo), 1,
2061 got_worktree_get_root_path(worktree));
2065 error = got_worktree_revert(worktree, path,
2066 revert_progress, NULL, repo);
2071 got_repo_close(repo);
2073 got_worktree_close(worktree);
2082 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2086 static const struct got_error *
2087 cmd_commit(int argc, char *argv[])
2089 const struct got_error *error = NULL;
2090 struct got_worktree *worktree = NULL;
2091 struct got_repository *repo = NULL;
2092 char *cwd = NULL, *path = NULL, *id_str = NULL;
2093 struct got_object_id *id = NULL;
2094 const char *logmsg = "<no log message was specified>";
2095 const char *got_author = getenv("GOT_AUTHOR");
2098 while ((ch = getopt(argc, argv, "m:")) != -1) {
2113 path = realpath(argv[0], NULL);
2115 error = got_error_from_errno();
2118 } else if (argc != 0)
2121 if (got_author == NULL) {
2122 /* TODO: Look current user up in password database */
2123 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2127 cwd = getcwd(NULL, 0);
2129 error = got_error_from_errno();
2132 error = got_worktree_open(&worktree, cwd);
2136 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2140 error = apply_unveil(got_repo_get_path(repo), 0,
2141 got_worktree_get_root_path(worktree));
2145 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2146 logmsg, print_status, NULL, repo);
2150 error = got_object_id_str(&id_str, id);
2153 printf("created commit %s\n", id_str);
2156 got_repo_close(repo);
2158 got_worktree_close(worktree);