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);
1068 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
1074 static const struct got_error *
1075 print_tree(const char *path, struct got_object_id *commit_id,
1076 int show_ids, struct got_repository *repo)
1078 const struct got_error *err = NULL;
1079 struct got_object_id *tree_id = NULL;
1080 struct got_tree_object *tree = NULL;
1081 const struct got_tree_entries *entries;
1082 struct got_tree_entry *te;
1084 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1088 err = got_object_open_as_tree(&tree, repo, tree_id);
1091 entries = got_object_tree_get_entries(tree);
1092 te = SIMPLEQ_FIRST(&entries->head);
1096 if (sigint_received || sigpipe_received)
1101 err = got_object_id_str(&id_str, te->id);
1104 if (asprintf(&id, "%s ", id_str) == -1) {
1105 err = got_error_from_errno();
1111 printf("%s%s%s\n", id ? id : "",
1112 te->name, S_ISDIR(te->mode) ? "/" : "");
1113 te = SIMPLEQ_NEXT(te, entry);
1118 got_object_tree_close(tree);
1123 static const struct got_error *
1124 cmd_tree(int argc, char *argv[])
1126 const struct got_error *error;
1127 struct got_repository *repo = NULL;
1128 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1129 struct got_object_id *commit_id = NULL;
1130 char *commit_id_str = NULL;
1135 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1140 while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
1143 commit_id_str = optarg;
1146 repo_path = realpath(optarg, NULL);
1147 if (repo_path == NULL)
1148 err(1, "-r option");
1169 cwd = getcwd(NULL, 0);
1171 error = got_error_from_errno();
1174 if (repo_path == NULL) {
1175 repo_path = strdup(cwd);
1176 if (repo_path == NULL) {
1177 error = got_error_from_errno();
1182 error = apply_unveil(repo_path, NULL);
1186 error = got_repo_open(&repo, repo_path);
1190 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1194 if (commit_id_str == NULL) {
1195 struct got_reference *head_ref;
1196 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1199 error = got_ref_resolve(&commit_id, repo, head_ref);
1200 got_ref_close(head_ref);
1204 error = got_object_resolve_id_str(&commit_id, repo,
1210 error = print_tree(in_repo_path, commit_id, show_ids, repo);
1217 const struct got_error *repo_error;
1218 repo_error = got_repo_close(repo);
1226 static const struct got_error *
1227 cmd_status(int argc __unused, char *argv[] __unused)
1229 git_repository *repo = NULL;
1230 git_status_list *status;
1231 git_status_options statusopts;
1236 if (git_repository_open_ext(&repo, ".", 0, NULL))
1237 errx(1, "git_repository_open: %s", giterr_last()->message);
1239 if (git_repository_is_bare(repo))
1240 errx(1, "bar repository");
1242 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
1243 errx(1, "git_status_init_options: %s", giterr_last()->message);
1245 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
1246 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
1247 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
1248 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1250 if (git_status_list_new(&status, repo, &statusopts))
1251 errx(1, "git_status_list_new: %s", giterr_last()->message);
1253 for (i = 0; i < git_status_list_entrycount(status); i++) {
1254 const git_status_entry *se;
1256 se = git_status_byindex(status, i);
1257 switch (se->status) {
1258 case GIT_STATUS_WT_NEW:
1259 printf("? %s\n", se->index_to_workdir->new_file.path);
1261 case GIT_STATUS_WT_MODIFIED:
1262 printf("M %s\n", se->index_to_workdir->new_file.path);
1264 case GIT_STATUS_WT_DELETED:
1265 printf("R %s\n", se->index_to_workdir->new_file.path);
1267 case GIT_STATUS_WT_RENAMED:
1268 printf("m %s -> %s\n",
1269 se->index_to_workdir->old_file.path,
1270 se->index_to_workdir->new_file.path);
1272 case GIT_STATUS_WT_TYPECHANGE:
1273 printf("t %s\n", se->index_to_workdir->new_file.path);
1275 case GIT_STATUS_INDEX_NEW:
1276 printf("A %s\n", se->head_to_index->new_file.path);
1278 case GIT_STATUS_INDEX_MODIFIED:
1279 printf("M %s\n", se->head_to_index->old_file.path);
1281 case GIT_STATUS_INDEX_DELETED:
1282 printf("R %s\n", se->head_to_index->old_file.path);
1284 case GIT_STATUS_INDEX_RENAMED:
1285 printf("m %s -> %s\n",
1286 se->head_to_index->old_file.path,
1287 se->head_to_index->new_file.path);
1289 case GIT_STATUS_INDEX_TYPECHANGE:
1290 printf("t %s\n", se->head_to_index->old_file.path);
1292 case GIT_STATUS_CURRENT:
1298 git_status_list_free(status);
1299 git_repository_free(repo);
1300 git_libgit2_shutdown();