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 cmd_checkout(int argc, char *argv[])
231 const struct got_error *error = NULL;
232 struct got_repository *repo = NULL;
233 struct got_reference *head_ref = NULL;
234 struct got_worktree *worktree = NULL;
235 char *repo_path = NULL;
236 char *worktree_path = NULL;
237 const char *path_prefix = "";
238 int ch, same_path_prefix;
240 while ((ch = getopt(argc, argv, "p:")) != -1) {
243 path_prefix = optarg;
255 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
260 char *cwd, *base, *dotgit;
261 repo_path = realpath(argv[0], NULL);
262 if (repo_path == NULL)
263 return got_error_from_errno();
264 cwd = getcwd(NULL, 0);
266 error = got_error_from_errno();
270 base = basename(path_prefix);
272 base = basename(repo_path);
274 error = got_error_from_errno();
277 dotgit = strstr(base, ".git");
280 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
281 error = got_error_from_errno();
286 } else if (argc == 2) {
287 repo_path = realpath(argv[0], NULL);
288 if (repo_path == NULL) {
289 error = got_error_from_errno();
292 worktree_path = realpath(argv[1], NULL);
293 if (worktree_path == NULL) {
294 error = got_error_from_errno();
300 error = apply_unveil(repo_path, worktree_path);
304 error = got_repo_open(&repo, repo_path);
307 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
311 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
312 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
315 error = got_worktree_open(&worktree, worktree_path);
319 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
323 if (!same_path_prefix) {
324 error = got_error(GOT_ERR_PATH_PREFIX);
328 error = got_worktree_checkout_files(worktree, repo,
329 checkout_progress, worktree_path, checkout_cancel, NULL);
333 printf("Now shut up and hack\n");
344 fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
350 update_progress(void *arg, unsigned char status, const char *path)
352 if (status == GOT_STATUS_EXISTS)
355 while (path[0] == '/')
357 printf("%c %s\n", status, path);
360 static const struct got_error *
361 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
362 struct got_repository *repo)
364 const struct got_error *err;
365 struct got_reference *head_ref = NULL;
366 struct got_object_id *head_commit_id = NULL;
367 struct got_commit_graph *graph = NULL;
369 head_ref = got_worktree_get_head_ref(worktree);
370 if (head_ref == NULL)
371 return got_error_from_errno();
373 /* TODO: Check the reflog. The head ref may have been rebased. */
374 err = got_ref_resolve(&head_commit_id, repo, head_ref);
378 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
382 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
386 struct got_object_id *id;
388 if (sigint_received || sigpipe_received)
391 err = got_commit_graph_iter_next(&id, graph);
393 if (err->code == GOT_ERR_ITER_COMPLETED) {
394 err = got_error(GOT_ERR_ANCESTRY);
397 if (err->code != GOT_ERR_ITER_NEED_MORE)
399 err = got_commit_graph_fetch_commits(graph, 1, repo);
407 if (got_object_id_cmp(id, commit_id) == 0)
412 got_ref_close(head_ref);
414 got_commit_graph_close(graph);
418 static const struct got_error *
419 cmd_update(int argc, char *argv[])
421 const struct got_error *error = NULL;
422 struct got_repository *repo = NULL;
423 struct got_worktree *worktree = NULL;
424 char *worktree_path = NULL;
425 struct got_object_id *commit_id = NULL;
426 char *commit_id_str = NULL;
429 while ((ch = getopt(argc, argv, "c:")) != -1) {
432 commit_id_str = strdup(optarg);
433 if (commit_id_str == NULL)
434 return got_error_from_errno();
446 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
451 worktree_path = getcwd(NULL, 0);
452 if (worktree_path == NULL) {
453 error = got_error_from_errno();
456 } else if (argc == 1) {
457 worktree_path = realpath(argv[0], NULL);
458 if (worktree_path == NULL) {
459 error = got_error_from_errno();
465 error = got_worktree_open(&worktree, worktree_path);
469 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
473 error = apply_unveil(got_repo_get_path(repo), worktree_path);
477 if (commit_id_str == NULL) {
478 struct got_reference *head_ref;
479 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
482 error = got_ref_resolve(&commit_id, repo, head_ref);
485 error = got_object_id_str(&commit_id_str, commit_id);
489 error = got_object_resolve_id_str(&commit_id, repo,
495 error = check_ancestry(worktree, commit_id, repo);
499 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
501 error = got_worktree_set_base_commit_id(worktree, repo,
507 error = got_worktree_checkout_files(worktree, repo,
508 update_progress, NULL, checkout_cancel, NULL);
512 printf("Updated to commit %s\n", commit_id_str);
520 static const struct got_error *
521 print_patch(struct got_commit_object *commit, struct got_object_id *id,
522 int diff_context, struct got_repository *repo)
524 const struct got_error *err = NULL;
525 struct got_tree_object *tree1 = NULL, *tree2;
526 struct got_object_qid *qid;
527 char *id_str1 = NULL, *id_str2;
529 err = got_object_open_as_tree(&tree2, repo,
530 got_object_commit_get_tree_id(commit));
534 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
536 struct got_commit_object *pcommit;
538 err = got_object_open_as_commit(&pcommit, repo, qid->id);
542 err = got_object_open_as_tree(&tree1, repo,
543 got_object_commit_get_tree_id(pcommit));
544 got_object_commit_close(pcommit);
548 err = got_object_id_str(&id_str1, qid->id);
553 err = got_object_id_str(&id_str2, id);
557 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
558 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
561 got_object_tree_close(tree1);
562 got_object_tree_close(tree2);
569 get_datestr(time_t *time, char *datebuf)
571 char *p, *s = ctime_r(time, datebuf);
578 static const struct got_error *
579 print_commit(struct got_commit_object *commit, struct got_object_id *id,
580 struct got_repository *repo, int show_patch, int diff_context)
582 const struct got_error *err = NULL;
583 char *id_str, *datestr, *logmsg0, *logmsg, *line;
585 time_t committer_time;
586 const char *author, *committer;
588 err = got_object_id_str(&id_str, id);
592 printf("-----------------------------------------------\n");
593 printf("commit %s\n", id_str);
595 printf("from: %s\n", got_object_commit_get_author(commit));
596 committer_time = got_object_commit_get_committer_time(commit);
597 datestr = get_datestr(&committer_time, datebuf);
598 printf("date: %s UTC\n", datestr);
599 author = got_object_commit_get_author(commit);
600 committer = got_object_commit_get_committer(commit);
601 if (strcmp(author, committer) != 0)
602 printf("via: %s\n", committer);
603 if (got_object_commit_get_nparents(commit) > 1) {
604 const struct got_object_id_queue *parent_ids;
605 struct got_object_qid *qid;
607 parent_ids = got_object_commit_get_parent_ids(commit);
608 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
609 err = got_object_id_str(&id_str, qid->id);
612 printf("parent %d: %s\n", n++, id_str);
617 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
619 return got_error_from_errno();
623 line = strsep(&logmsg, "\n");
625 printf(" %s\n", line);
630 err = print_patch(commit, id, diff_context, repo);
639 static const struct got_error *
640 print_commits(struct got_object_id *root_id, struct got_repository *repo,
641 char *path, int show_patch, int diff_context, int limit,
642 int first_parent_traversal)
644 const struct got_error *err;
645 struct got_commit_graph *graph;
647 err = got_commit_graph_open(&graph, root_id, path,
648 first_parent_traversal, repo);
651 err = got_commit_graph_iter_start(graph, root_id, repo);
655 struct got_commit_object *commit;
656 struct got_object_id *id;
658 if (sigint_received || sigpipe_received)
661 err = got_commit_graph_iter_next(&id, graph);
663 if (err->code == GOT_ERR_ITER_COMPLETED) {
667 if (err->code != GOT_ERR_ITER_NEED_MORE)
669 err = got_commit_graph_fetch_commits(graph, 1, repo);
678 err = got_object_open_as_commit(&commit, repo, id);
681 err = print_commit(commit, id, repo, show_patch, diff_context);
682 got_object_commit_close(commit);
683 if (err || (limit && --limit == 0))
687 got_commit_graph_close(graph);
694 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
695 "[-r repository-path] [path]\n", getprogname());
699 static const struct got_error *
700 cmd_log(int argc, char *argv[])
702 const struct got_error *error;
703 struct got_repository *repo = NULL;
704 struct got_commit_object *commit = NULL;
705 struct got_object_id *id = NULL;
706 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
707 char *start_commit = NULL;
708 int diff_context = 3, ch;
709 int show_patch = 0, limit = 0, first_parent_traversal = 0;
713 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
719 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
725 start_commit = optarg;
728 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
731 err(1, "-C option %s", errstr);
734 limit = strtonum(optarg, 1, INT_MAX, &errstr);
736 err(1, "-l option %s", errstr);
739 first_parent_traversal = 1;
742 repo_path = realpath(optarg, NULL);
743 if (repo_path == NULL)
758 path = strdup(argv[0]);
762 return got_error_from_errno();
764 cwd = getcwd(NULL, 0);
766 error = got_error_from_errno();
769 if (repo_path == NULL) {
770 repo_path = strdup(cwd);
771 if (repo_path == NULL) {
772 error = got_error_from_errno();
777 error = apply_unveil(repo_path, NULL);
781 error = got_repo_open(&repo, repo_path);
785 if (start_commit == NULL) {
786 struct got_reference *head_ref;
787 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
790 error = got_ref_resolve(&id, repo, head_ref);
791 got_ref_close(head_ref);
794 error = got_object_open_as_commit(&commit, repo, id);
796 struct got_reference *ref;
797 error = got_ref_open(&ref, repo, start_commit);
799 error = got_ref_resolve(&id, repo, ref);
803 error = got_object_open_as_commit(&commit, repo, id);
807 if (commit == NULL) {
808 error = got_object_resolve_id_str(&id, repo,
817 error = got_repo_map_path(&in_repo_path, repo, path, 1);
825 error = print_commits(id, repo, path, show_patch,
826 diff_context, limit, first_parent_traversal);
833 const struct got_error *repo_error;
834 repo_error = got_repo_close(repo);
844 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
845 "object1 object2\n", getprogname());
849 static const struct got_error *
850 cmd_diff(int argc, char *argv[])
852 const struct got_error *error;
853 struct got_repository *repo = NULL;
854 char *repo_path = NULL;
855 struct got_object_id *id1 = NULL, *id2 = NULL;
856 char *id_str1 = NULL, *id_str2 = NULL;
858 int diff_context = 3, ch;
862 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
867 while ((ch = getopt(argc, argv, "C:")) != -1) {
870 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
872 err(1, "-C option %s", errstr);
884 usage_diff(); /* TODO show local worktree changes */
885 } else if (argc == 2) {
886 repo_path = getcwd(NULL, 0);
887 if (repo_path == NULL)
888 return got_error_from_errno();
891 } else if (argc == 3) {
892 repo_path = realpath(argv[0], NULL);
893 if (repo_path == NULL)
894 return got_error_from_errno();
900 error = apply_unveil(repo_path, NULL);
904 error = got_repo_open(&repo, repo_path);
909 error = got_object_resolve_id_str(&id1, repo, id_str1);
913 error = got_object_resolve_id_str(&id2, repo, id_str2);
917 error = got_object_get_type(&type1, repo, id1);
921 error = got_object_get_type(&type2, repo, id2);
925 if (type1 != type2) {
926 error = got_error(GOT_ERR_OBJ_TYPE);
931 case GOT_OBJ_TYPE_BLOB:
932 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
933 diff_context, repo, stdout);
935 case GOT_OBJ_TYPE_TREE:
936 error = got_diff_objects_as_trees(id1, id2, "", "",
937 diff_context, repo, stdout);
939 case GOT_OBJ_TYPE_COMMIT:
940 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
942 error = got_diff_objects_as_commits(id1, id2, diff_context,
946 error = got_error(GOT_ERR_OBJ_TYPE);
953 const struct got_error *repo_error;
954 repo_error = got_repo_close(repo);
964 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
969 static const struct got_error *
970 cmd_blame(int argc, char *argv[])
972 const struct got_error *error;
973 struct got_repository *repo = NULL;
974 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
975 struct got_object_id *commit_id = NULL;
976 char *commit_id_str = NULL;
980 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
985 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
988 commit_id_str = optarg;
991 repo_path = realpath(optarg, NULL);
992 if (repo_path == NULL)
1009 cwd = getcwd(NULL, 0);
1011 error = got_error_from_errno();
1014 if (repo_path == NULL) {
1015 repo_path = strdup(cwd);
1016 if (repo_path == NULL) {
1017 error = got_error_from_errno();
1022 error = apply_unveil(repo_path, NULL);
1026 error = got_repo_open(&repo, repo_path);
1030 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1034 if (commit_id_str == NULL) {
1035 struct got_reference *head_ref;
1036 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1039 error = got_ref_resolve(&commit_id, repo, head_ref);
1040 got_ref_close(head_ref);
1044 error = got_object_resolve_id_str(&commit_id, repo,
1050 error = got_blame(in_repo_path, commit_id, repo, stdout);
1057 const struct got_error *repo_error;
1058 repo_error = got_repo_close(repo);
1069 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1075 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1076 const char *root_path)
1078 int is_root_path = (strcmp(path, root_path) == 0);
1080 path += strlen(root_path);
1081 while (path[0] == '/')
1084 printf("%s%s%s%s%s\n", id ? id : "", path,
1085 is_root_path ? "" : "/",
1086 te->name, S_ISDIR(te->mode) ? "/" : "");
1089 static const struct got_error *
1090 print_tree(const char *path, struct got_object_id *commit_id,
1091 int show_ids, int recurse, const char *root_path,
1092 struct got_repository *repo)
1094 const struct got_error *err = NULL;
1095 struct got_object_id *tree_id = NULL;
1096 struct got_tree_object *tree = NULL;
1097 const struct got_tree_entries *entries;
1098 struct got_tree_entry *te;
1100 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1104 err = got_object_open_as_tree(&tree, repo, tree_id);
1107 entries = got_object_tree_get_entries(tree);
1108 te = SIMPLEQ_FIRST(&entries->head);
1112 if (sigint_received || sigpipe_received)
1117 err = got_object_id_str(&id_str, te->id);
1120 if (asprintf(&id, "%s ", id_str) == -1) {
1121 err = got_error_from_errno();
1127 print_entry(te, id, path, root_path);
1130 if (recurse && S_ISDIR(te->mode)) {
1132 if (asprintf(&child_path, "%s%s%s", path,
1133 path[0] == '/' && path[1] == '\0' ? "" : "/",
1135 err = got_error_from_errno();
1138 err = print_tree(child_path, commit_id, show_ids, 1,
1145 te = SIMPLEQ_NEXT(te, entry);
1149 got_object_tree_close(tree);
1154 static const struct got_error *
1155 cmd_tree(int argc, char *argv[])
1157 const struct got_error *error;
1158 struct got_repository *repo = NULL;
1159 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1160 struct got_object_id *commit_id = NULL;
1161 char *commit_id_str = NULL;
1162 int show_ids = 0, recurse = 0;
1166 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1171 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1174 commit_id_str = optarg;
1177 repo_path = realpath(optarg, NULL);
1178 if (repo_path == NULL)
1179 err(1, "-r option");
1203 cwd = getcwd(NULL, 0);
1205 error = got_error_from_errno();
1208 if (repo_path == NULL) {
1209 repo_path = strdup(cwd);
1210 if (repo_path == NULL) {
1211 error = got_error_from_errno();
1216 error = apply_unveil(repo_path, NULL);
1220 error = got_repo_open(&repo, repo_path);
1224 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1228 if (commit_id_str == NULL) {
1229 struct got_reference *head_ref;
1230 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1233 error = got_ref_resolve(&commit_id, repo, head_ref);
1234 got_ref_close(head_ref);
1238 error = got_object_resolve_id_str(&commit_id, repo,
1244 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1245 in_repo_path, repo);
1252 const struct got_error *repo_error;
1253 repo_error = got_repo_close(repo);
1261 static const struct got_error *
1262 cmd_status(int argc __unused, char *argv[] __unused)
1264 git_repository *repo = NULL;
1265 git_status_list *status;
1266 git_status_options statusopts;
1271 if (git_repository_open_ext(&repo, ".", 0, NULL))
1272 errx(1, "git_repository_open: %s", giterr_last()->message);
1274 if (git_repository_is_bare(repo))
1275 errx(1, "bar repository");
1277 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
1278 errx(1, "git_status_init_options: %s", giterr_last()->message);
1280 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
1281 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
1282 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
1283 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1285 if (git_status_list_new(&status, repo, &statusopts))
1286 errx(1, "git_status_list_new: %s", giterr_last()->message);
1288 for (i = 0; i < git_status_list_entrycount(status); i++) {
1289 const git_status_entry *se;
1291 se = git_status_byindex(status, i);
1292 switch (se->status) {
1293 case GIT_STATUS_WT_NEW:
1294 printf("? %s\n", se->index_to_workdir->new_file.path);
1296 case GIT_STATUS_WT_MODIFIED:
1297 printf("M %s\n", se->index_to_workdir->new_file.path);
1299 case GIT_STATUS_WT_DELETED:
1300 printf("R %s\n", se->index_to_workdir->new_file.path);
1302 case GIT_STATUS_WT_RENAMED:
1303 printf("m %s -> %s\n",
1304 se->index_to_workdir->old_file.path,
1305 se->index_to_workdir->new_file.path);
1307 case GIT_STATUS_WT_TYPECHANGE:
1308 printf("t %s\n", se->index_to_workdir->new_file.path);
1310 case GIT_STATUS_INDEX_NEW:
1311 printf("A %s\n", se->head_to_index->new_file.path);
1313 case GIT_STATUS_INDEX_MODIFIED:
1314 printf("M %s\n", se->head_to_index->old_file.path);
1316 case GIT_STATUS_INDEX_DELETED:
1317 printf("R %s\n", se->head_to_index->old_file.path);
1319 case GIT_STATUS_INDEX_RENAMED:
1320 printf("m %s -> %s\n",
1321 se->head_to_index->old_file.path,
1322 se->head_to_index->new_file.path);
1324 case GIT_STATUS_INDEX_TYPECHANGE:
1325 printf("t %s\n", se->head_to_index->old_file.path);
1327 case GIT_STATUS_CURRENT:
1333 git_status_list_free(status);
1334 git_repository_free(repo);
1335 git_libgit2_shutdown();