2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018 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);
79 static const struct got_error* cmd_checkout(int, char *[]);
80 static const struct got_error* cmd_update(int, char *[]);
81 static const struct got_error* cmd_log(int, char *[]);
82 static const struct got_error* cmd_diff(int, char *[]);
83 static const struct got_error* cmd_blame(int, char *[]);
84 static const struct got_error* cmd_tree(int, char *[]);
86 static const struct got_error* cmd_status(int, char *[]);
89 static struct cmd got_commands[] = {
90 { "checkout", cmd_checkout, usage_checkout,
91 "check out a new work tree from a repository" },
92 { "update", cmd_update, usage_update,
93 "update a work tree to a different commit" },
94 { "log", cmd_log, usage_log,
95 "show repository history" },
96 { "diff", cmd_diff, usage_diff,
97 "compare files and directories" },
98 { "blame", cmd_blame, usage_blame,
99 " show when lines in a file were changed" },
100 { "tree", cmd_tree, usage_tree,
101 " list files and directories in repository" },
103 { "status", cmd_status, usage_status,
104 "show modification status of files" },
109 main(int argc, char *argv[])
116 setlocale(LC_ALL, "");
118 while ((ch = getopt(argc, argv, "h")) != -1) {
136 signal(SIGINT, catch_sigint);
137 signal(SIGPIPE, catch_sigpipe);
139 for (i = 0; i < nitems(got_commands); i++) {
140 const struct got_error *error;
142 cmd = &got_commands[i];
144 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
148 got_commands[i].cmd_usage();
150 error = got_commands[i].cmd_main(argc, argv);
151 if (error && !(sigint_received || sigpipe_received)) {
152 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
159 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
168 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
169 "Available commands:\n", getprogname());
170 for (i = 0; i < nitems(got_commands); i++) {
171 struct cmd *cmd = &got_commands[i];
172 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
177 static const struct got_error *
178 apply_unveil(const char *repo_path, const char *worktree_path)
180 const struct got_error *error;
182 if (repo_path && unveil(repo_path, "r") != 0)
183 return got_error_from_errno();
185 if (worktree_path && unveil(worktree_path, "rwc") != 0)
186 return got_error_from_errno();
188 if (unveil("/tmp", "rwc") != 0)
189 return got_error_from_errno();
191 error = got_privsep_unveil_exec_helpers();
195 if (unveil(NULL, NULL) != 0)
196 return got_error_from_errno();
204 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
205 "[worktree-path]\n", getprogname());
210 checkout_progress(void *arg, unsigned char status, const char *path)
212 char *worktree_path = arg;
214 while (path[0] == '/')
217 printf("%c %s/%s\n", status, worktree_path, path);
220 static const struct got_error *
221 checkout_cancel(void *arg)
223 if (sigint_received || sigpipe_received)
224 return got_error(GOT_ERR_CANCELLED);
228 static const struct got_error *
229 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
230 struct got_repository *repo)
232 const struct got_error *err;
233 struct got_reference *head_ref = NULL;
234 struct got_object_id *head_commit_id = NULL;
235 struct got_commit_graph *graph = NULL;
237 head_ref = got_worktree_get_head_ref(worktree);
238 if (head_ref == NULL)
239 return got_error_from_errno();
241 /* TODO: Check the reflog. The head ref may have been rebased. */
242 err = got_ref_resolve(&head_commit_id, repo, head_ref);
246 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
250 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
254 struct got_object_id *id;
256 if (sigint_received || sigpipe_received)
259 err = got_commit_graph_iter_next(&id, graph);
261 if (err->code == GOT_ERR_ITER_COMPLETED) {
262 err = got_error(GOT_ERR_ANCESTRY);
265 if (err->code != GOT_ERR_ITER_NEED_MORE)
267 err = got_commit_graph_fetch_commits(graph, 1, repo);
275 if (got_object_id_cmp(id, commit_id) == 0)
280 got_ref_close(head_ref);
282 got_commit_graph_close(graph);
287 static const struct got_error *
288 cmd_checkout(int argc, char *argv[])
290 const struct got_error *error = NULL;
291 struct got_repository *repo = NULL;
292 struct got_reference *head_ref = NULL;
293 struct got_worktree *worktree = NULL;
294 char *repo_path = NULL;
295 char *worktree_path = NULL;
296 const char *path_prefix = "";
297 char *commit_id_str = NULL;
298 int ch, same_path_prefix;
300 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
303 commit_id_str = strdup(optarg);
304 if (commit_id_str == NULL)
305 return got_error_from_errno();
308 path_prefix = optarg;
320 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
325 char *cwd, *base, *dotgit;
326 repo_path = realpath(argv[0], NULL);
327 if (repo_path == NULL)
328 return got_error_from_errno();
329 cwd = getcwd(NULL, 0);
331 error = got_error_from_errno();
335 base = basename(path_prefix);
337 base = basename(repo_path);
339 error = got_error_from_errno();
342 dotgit = strstr(base, ".git");
345 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
346 error = got_error_from_errno();
351 } else if (argc == 2) {
352 repo_path = realpath(argv[0], NULL);
353 if (repo_path == NULL) {
354 error = got_error_from_errno();
357 worktree_path = realpath(argv[1], NULL);
358 if (worktree_path == NULL) {
359 error = got_error_from_errno();
365 error = apply_unveil(repo_path, worktree_path);
369 error = got_repo_open(&repo, repo_path);
373 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
377 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
378 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
381 error = got_worktree_open(&worktree, worktree_path);
385 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
389 if (!same_path_prefix) {
390 error = got_error(GOT_ERR_PATH_PREFIX);
395 struct got_object_id *commit_id;
396 error = got_object_resolve_id_str(&commit_id, repo,
400 error = check_ancestry(worktree, commit_id, repo);
405 error = got_worktree_set_base_commit_id(worktree, repo,
412 error = got_worktree_checkout_files(worktree, repo,
413 checkout_progress, worktree_path, checkout_cancel, NULL);
417 printf("Now shut up and hack\n");
429 fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
435 update_progress(void *arg, unsigned char status, const char *path)
437 if (status == GOT_STATUS_EXISTS)
440 while (path[0] == '/')
442 printf("%c %s\n", status, path);
445 static const struct got_error *
446 cmd_update(int argc, char *argv[])
448 const struct got_error *error = NULL;
449 struct got_repository *repo = NULL;
450 struct got_worktree *worktree = NULL;
451 char *worktree_path = NULL;
452 struct got_object_id *commit_id = NULL;
453 char *commit_id_str = NULL;
456 while ((ch = getopt(argc, argv, "c:")) != -1) {
459 commit_id_str = strdup(optarg);
460 if (commit_id_str == NULL)
461 return got_error_from_errno();
473 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
478 worktree_path = getcwd(NULL, 0);
479 if (worktree_path == NULL) {
480 error = got_error_from_errno();
483 } else if (argc == 1) {
484 worktree_path = realpath(argv[0], NULL);
485 if (worktree_path == NULL) {
486 error = got_error_from_errno();
492 error = got_worktree_open(&worktree, worktree_path);
496 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
500 error = apply_unveil(got_repo_get_path(repo), worktree_path);
504 if (commit_id_str == NULL) {
505 struct got_reference *head_ref;
506 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
509 error = got_ref_resolve(&commit_id, repo, head_ref);
512 error = got_object_id_str(&commit_id_str, commit_id);
516 error = got_object_resolve_id_str(&commit_id, repo,
522 error = check_ancestry(worktree, commit_id, repo);
526 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
528 error = got_worktree_set_base_commit_id(worktree, repo,
534 error = got_worktree_checkout_files(worktree, repo,
535 update_progress, NULL, checkout_cancel, NULL);
539 printf("Updated to commit %s\n", commit_id_str);
547 static const struct got_error *
548 print_patch(struct got_commit_object *commit, struct got_object_id *id,
549 int diff_context, struct got_repository *repo)
551 const struct got_error *err = NULL;
552 struct got_tree_object *tree1 = NULL, *tree2;
553 struct got_object_qid *qid;
554 char *id_str1 = NULL, *id_str2;
556 err = got_object_open_as_tree(&tree2, repo,
557 got_object_commit_get_tree_id(commit));
561 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
563 struct got_commit_object *pcommit;
565 err = got_object_open_as_commit(&pcommit, repo, qid->id);
569 err = got_object_open_as_tree(&tree1, repo,
570 got_object_commit_get_tree_id(pcommit));
571 got_object_commit_close(pcommit);
575 err = got_object_id_str(&id_str1, qid->id);
580 err = got_object_id_str(&id_str2, id);
584 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
585 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
588 got_object_tree_close(tree1);
589 got_object_tree_close(tree2);
596 get_datestr(time_t *time, char *datebuf)
598 char *p, *s = ctime_r(time, datebuf);
605 static const struct got_error *
606 print_commit(struct got_commit_object *commit, struct got_object_id *id,
607 struct got_repository *repo, int show_patch, int diff_context)
609 const struct got_error *err = NULL;
610 char *id_str, *datestr, *logmsg0, *logmsg, *line;
612 time_t committer_time;
613 const char *author, *committer;
615 err = got_object_id_str(&id_str, id);
619 printf("-----------------------------------------------\n");
620 printf("commit %s\n", id_str);
622 printf("from: %s\n", got_object_commit_get_author(commit));
623 committer_time = got_object_commit_get_committer_time(commit);
624 datestr = get_datestr(&committer_time, datebuf);
625 printf("date: %s UTC\n", datestr);
626 author = got_object_commit_get_author(commit);
627 committer = got_object_commit_get_committer(commit);
628 if (strcmp(author, committer) != 0)
629 printf("via: %s\n", committer);
630 if (got_object_commit_get_nparents(commit) > 1) {
631 const struct got_object_id_queue *parent_ids;
632 struct got_object_qid *qid;
634 parent_ids = got_object_commit_get_parent_ids(commit);
635 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
636 err = got_object_id_str(&id_str, qid->id);
639 printf("parent %d: %s\n", n++, id_str);
644 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
646 return got_error_from_errno();
650 line = strsep(&logmsg, "\n");
652 printf(" %s\n", line);
657 err = print_patch(commit, id, diff_context, repo);
666 static const struct got_error *
667 print_commits(struct got_object_id *root_id, struct got_repository *repo,
668 char *path, int show_patch, int diff_context, int limit,
669 int first_parent_traversal)
671 const struct got_error *err;
672 struct got_commit_graph *graph;
674 err = got_commit_graph_open(&graph, root_id, path,
675 first_parent_traversal, repo);
678 err = got_commit_graph_iter_start(graph, root_id, repo);
682 struct got_commit_object *commit;
683 struct got_object_id *id;
685 if (sigint_received || sigpipe_received)
688 err = got_commit_graph_iter_next(&id, graph);
690 if (err->code == GOT_ERR_ITER_COMPLETED) {
694 if (err->code != GOT_ERR_ITER_NEED_MORE)
696 err = got_commit_graph_fetch_commits(graph, 1, repo);
705 err = got_object_open_as_commit(&commit, repo, id);
708 err = print_commit(commit, id, repo, show_patch, diff_context);
709 got_object_commit_close(commit);
710 if (err || (limit && --limit == 0))
714 got_commit_graph_close(graph);
721 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
722 "[-r repository-path] [path]\n", getprogname());
726 static const struct got_error *
727 cmd_log(int argc, char *argv[])
729 const struct got_error *error;
730 struct got_repository *repo = NULL;
731 struct got_commit_object *commit = NULL;
732 struct got_object_id *id = NULL;
733 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
734 char *start_commit = NULL;
735 int diff_context = 3, ch;
736 int show_patch = 0, limit = 0, first_parent_traversal = 0;
740 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
746 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
752 start_commit = optarg;
755 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
758 err(1, "-C option %s", errstr);
761 limit = strtonum(optarg, 1, INT_MAX, &errstr);
763 err(1, "-l option %s", errstr);
766 first_parent_traversal = 1;
769 repo_path = realpath(optarg, NULL);
770 if (repo_path == NULL)
785 path = strdup(argv[0]);
789 return got_error_from_errno();
791 cwd = getcwd(NULL, 0);
793 error = got_error_from_errno();
796 if (repo_path == NULL) {
797 repo_path = strdup(cwd);
798 if (repo_path == NULL) {
799 error = got_error_from_errno();
804 error = apply_unveil(repo_path, NULL);
808 error = got_repo_open(&repo, repo_path);
812 if (start_commit == NULL) {
813 struct got_reference *head_ref;
814 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
817 error = got_ref_resolve(&id, repo, head_ref);
818 got_ref_close(head_ref);
821 error = got_object_open_as_commit(&commit, repo, id);
823 struct got_reference *ref;
824 error = got_ref_open(&ref, repo, start_commit);
826 error = got_ref_resolve(&id, repo, ref);
830 error = got_object_open_as_commit(&commit, repo, id);
834 if (commit == NULL) {
835 error = got_object_resolve_id_str(&id, repo,
844 error = got_repo_map_path(&in_repo_path, repo, path, 1);
852 error = print_commits(id, repo, path, show_patch,
853 diff_context, limit, first_parent_traversal);
860 const struct got_error *repo_error;
861 repo_error = got_repo_close(repo);
871 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
872 "object1 object2\n", getprogname());
876 static const struct got_error *
877 cmd_diff(int argc, char *argv[])
879 const struct got_error *error;
880 struct got_repository *repo = NULL;
881 char *repo_path = NULL;
882 struct got_object_id *id1 = NULL, *id2 = NULL;
883 char *id_str1 = NULL, *id_str2 = NULL;
885 int diff_context = 3, ch;
889 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
894 while ((ch = getopt(argc, argv, "C:")) != -1) {
897 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
899 err(1, "-C option %s", errstr);
911 usage_diff(); /* TODO show local worktree changes */
912 } else if (argc == 2) {
913 repo_path = getcwd(NULL, 0);
914 if (repo_path == NULL)
915 return got_error_from_errno();
918 } else if (argc == 3) {
919 repo_path = realpath(argv[0], NULL);
920 if (repo_path == NULL)
921 return got_error_from_errno();
927 error = apply_unveil(repo_path, NULL);
931 error = got_repo_open(&repo, repo_path);
936 error = got_object_resolve_id_str(&id1, repo, id_str1);
940 error = got_object_resolve_id_str(&id2, repo, id_str2);
944 error = got_object_get_type(&type1, repo, id1);
948 error = got_object_get_type(&type2, repo, id2);
952 if (type1 != type2) {
953 error = got_error(GOT_ERR_OBJ_TYPE);
958 case GOT_OBJ_TYPE_BLOB:
959 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
960 diff_context, repo, stdout);
962 case GOT_OBJ_TYPE_TREE:
963 error = got_diff_objects_as_trees(id1, id2, "", "",
964 diff_context, repo, stdout);
966 case GOT_OBJ_TYPE_COMMIT:
967 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
969 error = got_diff_objects_as_commits(id1, id2, diff_context,
973 error = got_error(GOT_ERR_OBJ_TYPE);
980 const struct got_error *repo_error;
981 repo_error = got_repo_close(repo);
991 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
996 static const struct got_error *
997 cmd_blame(int argc, char *argv[])
999 const struct got_error *error;
1000 struct got_repository *repo = NULL;
1001 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1002 struct got_object_id *commit_id = NULL;
1003 char *commit_id_str = NULL;
1007 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1012 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1015 commit_id_str = optarg;
1018 repo_path = realpath(optarg, NULL);
1019 if (repo_path == NULL)
1020 err(1, "-r option");
1036 cwd = getcwd(NULL, 0);
1038 error = got_error_from_errno();
1041 if (repo_path == NULL) {
1042 repo_path = strdup(cwd);
1043 if (repo_path == NULL) {
1044 error = got_error_from_errno();
1049 error = apply_unveil(repo_path, NULL);
1053 error = got_repo_open(&repo, repo_path);
1057 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1061 if (commit_id_str == NULL) {
1062 struct got_reference *head_ref;
1063 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1066 error = got_ref_resolve(&commit_id, repo, head_ref);
1067 got_ref_close(head_ref);
1071 error = got_object_resolve_id_str(&commit_id, repo,
1077 error = got_blame(in_repo_path, commit_id, repo, stdout);
1084 const struct got_error *repo_error;
1085 repo_error = got_repo_close(repo);
1096 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1102 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1103 const char *root_path)
1105 int is_root_path = (strcmp(path, root_path) == 0);
1107 path += strlen(root_path);
1108 while (path[0] == '/')
1111 printf("%s%s%s%s%s\n", id ? id : "", path,
1112 is_root_path ? "" : "/",
1113 te->name, S_ISDIR(te->mode) ? "/" : "");
1116 static const struct got_error *
1117 print_tree(const char *path, struct got_object_id *commit_id,
1118 int show_ids, int recurse, const char *root_path,
1119 struct got_repository *repo)
1121 const struct got_error *err = NULL;
1122 struct got_object_id *tree_id = NULL;
1123 struct got_tree_object *tree = NULL;
1124 const struct got_tree_entries *entries;
1125 struct got_tree_entry *te;
1127 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1131 err = got_object_open_as_tree(&tree, repo, tree_id);
1134 entries = got_object_tree_get_entries(tree);
1135 te = SIMPLEQ_FIRST(&entries->head);
1139 if (sigint_received || sigpipe_received)
1144 err = got_object_id_str(&id_str, te->id);
1147 if (asprintf(&id, "%s ", id_str) == -1) {
1148 err = got_error_from_errno();
1154 print_entry(te, id, path, root_path);
1157 if (recurse && S_ISDIR(te->mode)) {
1159 if (asprintf(&child_path, "%s%s%s", path,
1160 path[0] == '/' && path[1] == '\0' ? "" : "/",
1162 err = got_error_from_errno();
1165 err = print_tree(child_path, commit_id, show_ids, 1,
1172 te = SIMPLEQ_NEXT(te, entry);
1176 got_object_tree_close(tree);
1181 static const struct got_error *
1182 cmd_tree(int argc, char *argv[])
1184 const struct got_error *error;
1185 struct got_repository *repo = NULL;
1186 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1187 struct got_object_id *commit_id = NULL;
1188 char *commit_id_str = NULL;
1189 int show_ids = 0, recurse = 0;
1193 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1198 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1201 commit_id_str = optarg;
1204 repo_path = realpath(optarg, NULL);
1205 if (repo_path == NULL)
1206 err(1, "-r option");
1230 cwd = getcwd(NULL, 0);
1232 error = got_error_from_errno();
1235 if (repo_path == NULL) {
1236 repo_path = strdup(cwd);
1237 if (repo_path == NULL) {
1238 error = got_error_from_errno();
1243 error = apply_unveil(repo_path, NULL);
1247 error = got_repo_open(&repo, repo_path);
1251 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1255 if (commit_id_str == NULL) {
1256 struct got_reference *head_ref;
1257 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1260 error = got_ref_resolve(&commit_id, repo, head_ref);
1261 got_ref_close(head_ref);
1265 error = got_object_resolve_id_str(&commit_id, repo,
1271 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1272 in_repo_path, repo);
1279 const struct got_error *repo_error;
1280 repo_error = got_repo_close(repo);
1288 static const struct got_error *
1289 cmd_status(int argc __unused, char *argv[] __unused)
1291 git_repository *repo = NULL;
1292 git_status_list *status;
1293 git_status_options statusopts;
1298 if (git_repository_open_ext(&repo, ".", 0, NULL))
1299 errx(1, "git_repository_open: %s", giterr_last()->message);
1301 if (git_repository_is_bare(repo))
1302 errx(1, "bar repository");
1304 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
1305 errx(1, "git_status_init_options: %s", giterr_last()->message);
1307 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
1308 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
1309 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
1310 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1312 if (git_status_list_new(&status, repo, &statusopts))
1313 errx(1, "git_status_list_new: %s", giterr_last()->message);
1315 for (i = 0; i < git_status_list_entrycount(status); i++) {
1316 const git_status_entry *se;
1318 se = git_status_byindex(status, i);
1319 switch (se->status) {
1320 case GIT_STATUS_WT_NEW:
1321 printf("? %s\n", se->index_to_workdir->new_file.path);
1323 case GIT_STATUS_WT_MODIFIED:
1324 printf("M %s\n", se->index_to_workdir->new_file.path);
1326 case GIT_STATUS_WT_DELETED:
1327 printf("R %s\n", se->index_to_workdir->new_file.path);
1329 case GIT_STATUS_WT_RENAMED:
1330 printf("m %s -> %s\n",
1331 se->index_to_workdir->old_file.path,
1332 se->index_to_workdir->new_file.path);
1334 case GIT_STATUS_WT_TYPECHANGE:
1335 printf("t %s\n", se->index_to_workdir->new_file.path);
1337 case GIT_STATUS_INDEX_NEW:
1338 printf("A %s\n", se->head_to_index->new_file.path);
1340 case GIT_STATUS_INDEX_MODIFIED:
1341 printf("M %s\n", se->head_to_index->old_file.path);
1343 case GIT_STATUS_INDEX_DELETED:
1344 printf("R %s\n", se->head_to_index->old_file.path);
1346 case GIT_STATUS_INDEX_RENAMED:
1347 printf("m %s -> %s\n",
1348 se->head_to_index->old_file.path,
1349 se->head_to_index->new_file.path);
1351 case GIT_STATUS_INDEX_TYPECHANGE:
1352 printf("t %s\n", se->head_to_index->old_file.path);
1354 case GIT_STATUS_CURRENT:
1360 git_status_list_free(status);
1361 git_repository_free(repo);
1362 git_libgit2_shutdown();