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 char *repo_path = NULL;
426 struct got_object_id *commit_id = NULL;
427 char *commit_id_str = NULL;
430 while ((ch = getopt(argc, argv, "c:")) != -1) {
433 commit_id_str = strdup(optarg);
434 if (commit_id_str == NULL)
435 return got_error_from_errno();
447 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
452 worktree_path = getcwd(NULL, 0);
453 if (worktree_path == NULL) {
454 error = got_error_from_errno();
457 } else if (argc == 1) {
458 worktree_path = realpath(argv[0], NULL);
459 if (worktree_path == NULL) {
460 error = got_error_from_errno();
466 error = got_worktree_open(&worktree, worktree_path);
470 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
474 repo_path = got_repo_get_path(repo);
475 if (repo_path == NULL) {
476 error = got_error_from_errno();
480 error = apply_unveil(repo_path, worktree_path);
484 if (commit_id_str == NULL) {
485 struct got_reference *head_ref;
486 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
489 error = got_ref_resolve(&commit_id, repo, head_ref);
492 error = got_object_id_str(&commit_id_str, commit_id);
496 error = got_object_resolve_id_str(&commit_id, repo,
502 error = check_ancestry(worktree, commit_id, repo);
506 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
508 error = got_worktree_set_base_commit_id(worktree, repo,
514 error = got_worktree_checkout_files(worktree, repo,
515 update_progress, NULL, checkout_cancel, NULL);
519 printf("Updated to commit %s\n", commit_id_str);
528 static const struct got_error *
529 print_patch(struct got_commit_object *commit, struct got_object_id *id,
530 int diff_context, struct got_repository *repo)
532 const struct got_error *err = NULL;
533 struct got_tree_object *tree1 = NULL, *tree2;
534 struct got_object_qid *qid;
535 char *id_str1 = NULL, *id_str2;
537 err = got_object_open_as_tree(&tree2, repo,
538 got_object_commit_get_tree_id(commit));
542 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
544 struct got_commit_object *pcommit;
546 err = got_object_open_as_commit(&pcommit, repo, qid->id);
550 err = got_object_open_as_tree(&tree1, repo,
551 got_object_commit_get_tree_id(pcommit));
552 got_object_commit_close(pcommit);
556 err = got_object_id_str(&id_str1, qid->id);
561 err = got_object_id_str(&id_str2, id);
565 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
566 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
569 got_object_tree_close(tree1);
570 got_object_tree_close(tree2);
577 get_datestr(time_t *time, char *datebuf)
579 char *p, *s = ctime_r(time, datebuf);
586 static const struct got_error *
587 print_commit(struct got_commit_object *commit, struct got_object_id *id,
588 struct got_repository *repo, int show_patch, int diff_context)
590 const struct got_error *err = NULL;
591 char *id_str, *datestr, *logmsg0, *logmsg, *line;
593 time_t committer_time;
594 const char *author, *committer;
596 err = got_object_id_str(&id_str, id);
600 printf("-----------------------------------------------\n");
601 printf("commit %s\n", id_str);
603 printf("from: %s\n", got_object_commit_get_author(commit));
604 committer_time = got_object_commit_get_committer_time(commit);
605 datestr = get_datestr(&committer_time, datebuf);
606 printf("date: %s UTC\n", datestr);
607 author = got_object_commit_get_author(commit);
608 committer = got_object_commit_get_committer(commit);
609 if (strcmp(author, committer) != 0)
610 printf("via: %s\n", committer);
611 if (got_object_commit_get_nparents(commit) > 1) {
612 const struct got_object_id_queue *parent_ids;
613 struct got_object_qid *qid;
615 parent_ids = got_object_commit_get_parent_ids(commit);
616 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
617 err = got_object_id_str(&id_str, qid->id);
620 printf("parent %d: %s\n", n++, id_str);
625 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
627 return got_error_from_errno();
631 line = strsep(&logmsg, "\n");
633 printf(" %s\n", line);
638 err = print_patch(commit, id, diff_context, repo);
647 static const struct got_error *
648 print_commits(struct got_object_id *root_id, struct got_repository *repo,
649 char *path, int show_patch, int diff_context, int limit,
650 int first_parent_traversal)
652 const struct got_error *err;
653 struct got_commit_graph *graph;
655 err = got_commit_graph_open(&graph, root_id, path,
656 first_parent_traversal, repo);
659 err = got_commit_graph_iter_start(graph, root_id, repo);
663 struct got_commit_object *commit;
664 struct got_object_id *id;
666 if (sigint_received || sigpipe_received)
669 err = got_commit_graph_iter_next(&id, graph);
671 if (err->code == GOT_ERR_ITER_COMPLETED) {
675 if (err->code != GOT_ERR_ITER_NEED_MORE)
677 err = got_commit_graph_fetch_commits(graph, 1, repo);
686 err = got_object_open_as_commit(&commit, repo, id);
689 err = print_commit(commit, id, repo, show_patch, diff_context);
690 got_object_commit_close(commit);
691 if (err || (limit && --limit == 0))
695 got_commit_graph_close(graph);
702 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
703 "[-r repository-path] [path]\n", getprogname());
707 static const struct got_error *
708 cmd_log(int argc, char *argv[])
710 const struct got_error *error;
711 struct got_repository *repo = NULL;
712 struct got_commit_object *commit = NULL;
713 struct got_object_id *id = NULL;
714 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
715 char *start_commit = NULL;
716 int diff_context = 3, ch;
717 int show_patch = 0, limit = 0, first_parent_traversal = 0;
721 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
727 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
733 start_commit = optarg;
736 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
739 err(1, "-C option %s", errstr);
742 limit = strtonum(optarg, 1, INT_MAX, &errstr);
744 err(1, "-l option %s", errstr);
747 first_parent_traversal = 1;
750 repo_path = realpath(optarg, NULL);
751 if (repo_path == NULL)
766 path = strdup(argv[0]);
770 return got_error_from_errno();
772 cwd = getcwd(NULL, 0);
774 error = got_error_from_errno();
777 if (repo_path == NULL) {
778 repo_path = strdup(cwd);
779 if (repo_path == NULL) {
780 error = got_error_from_errno();
785 error = apply_unveil(repo_path, NULL);
789 error = got_repo_open(&repo, repo_path);
793 if (start_commit == NULL) {
794 struct got_reference *head_ref;
795 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
798 error = got_ref_resolve(&id, repo, head_ref);
799 got_ref_close(head_ref);
802 error = got_object_open_as_commit(&commit, repo, id);
804 struct got_reference *ref;
805 error = got_ref_open(&ref, repo, start_commit);
807 error = got_ref_resolve(&id, repo, ref);
811 error = got_object_open_as_commit(&commit, repo, id);
815 if (commit == NULL) {
816 error = got_object_resolve_id_str(&id, repo,
825 error = got_repo_map_path(&in_repo_path, repo, path, 1);
833 error = print_commits(id, repo, path, show_patch,
834 diff_context, limit, first_parent_traversal);
841 const struct got_error *repo_error;
842 repo_error = got_repo_close(repo);
852 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
853 "object1 object2\n", getprogname());
857 static const struct got_error *
858 cmd_diff(int argc, char *argv[])
860 const struct got_error *error;
861 struct got_repository *repo = NULL;
862 char *repo_path = NULL;
863 struct got_object_id *id1 = NULL, *id2 = NULL;
864 char *id_str1 = NULL, *id_str2 = NULL;
866 int diff_context = 3, ch;
870 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
875 while ((ch = getopt(argc, argv, "C:")) != -1) {
878 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
880 err(1, "-C option %s", errstr);
892 usage_diff(); /* TODO show local worktree changes */
893 } else if (argc == 2) {
894 repo_path = getcwd(NULL, 0);
895 if (repo_path == NULL)
896 return got_error_from_errno();
899 } else if (argc == 3) {
900 repo_path = realpath(argv[0], NULL);
901 if (repo_path == NULL)
902 return got_error_from_errno();
908 error = apply_unveil(repo_path, NULL);
912 error = got_repo_open(&repo, repo_path);
917 error = got_object_resolve_id_str(&id1, repo, id_str1);
921 error = got_object_resolve_id_str(&id2, repo, id_str2);
925 error = got_object_get_type(&type1, repo, id1);
929 error = got_object_get_type(&type2, repo, id2);
933 if (type1 != type2) {
934 error = got_error(GOT_ERR_OBJ_TYPE);
939 case GOT_OBJ_TYPE_BLOB:
940 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
941 diff_context, repo, stdout);
943 case GOT_OBJ_TYPE_TREE:
944 error = got_diff_objects_as_trees(id1, id2, "", "",
945 diff_context, repo, stdout);
947 case GOT_OBJ_TYPE_COMMIT:
948 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
950 error = got_diff_objects_as_commits(id1, id2, diff_context,
954 error = got_error(GOT_ERR_OBJ_TYPE);
961 const struct got_error *repo_error;
962 repo_error = got_repo_close(repo);
972 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
977 static const struct got_error *
978 cmd_blame(int argc, char *argv[])
980 const struct got_error *error;
981 struct got_repository *repo = NULL;
982 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
983 struct got_object_id *commit_id = NULL;
984 char *commit_id_str = NULL;
988 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
993 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
996 commit_id_str = optarg;
999 repo_path = realpath(optarg, NULL);
1000 if (repo_path == NULL)
1001 err(1, "-r option");
1017 cwd = getcwd(NULL, 0);
1019 error = got_error_from_errno();
1022 if (repo_path == NULL) {
1023 repo_path = strdup(cwd);
1024 if (repo_path == NULL) {
1025 error = got_error_from_errno();
1030 error = apply_unveil(repo_path, NULL);
1034 error = got_repo_open(&repo, repo_path);
1038 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1042 if (commit_id_str == NULL) {
1043 struct got_reference *head_ref;
1044 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1047 error = got_ref_resolve(&commit_id, repo, head_ref);
1048 got_ref_close(head_ref);
1052 error = got_object_resolve_id_str(&commit_id, repo,
1058 error = got_blame(in_repo_path, commit_id, repo, stdout);
1065 const struct got_error *repo_error;
1066 repo_error = got_repo_close(repo);
1076 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
1082 static const struct got_error *
1083 print_tree(const char *path, struct got_object_id *commit_id,
1084 int show_ids, struct got_repository *repo)
1086 const struct got_error *err = NULL;
1087 struct got_object_id *tree_id = NULL;
1088 struct got_tree_object *tree = NULL;
1089 const struct got_tree_entries *entries;
1090 struct got_tree_entry *te;
1092 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1096 err = got_object_open_as_tree(&tree, repo, tree_id);
1099 entries = got_object_tree_get_entries(tree);
1100 te = SIMPLEQ_FIRST(&entries->head);
1104 if (sigint_received || sigpipe_received)
1109 err = got_object_id_str(&id_str, te->id);
1112 if (asprintf(&id, "%s ", id_str) == -1) {
1113 err = got_error_from_errno();
1119 printf("%s%s%s\n", id ? id : "",
1120 te->name, S_ISDIR(te->mode) ? "/" : "");
1121 te = SIMPLEQ_NEXT(te, entry);
1126 got_object_tree_close(tree);
1131 static const struct got_error *
1132 cmd_tree(int argc, char *argv[])
1134 const struct got_error *error;
1135 struct got_repository *repo = NULL;
1136 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1137 struct got_object_id *commit_id = NULL;
1138 char *commit_id_str = NULL;
1143 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1148 while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
1151 commit_id_str = optarg;
1154 repo_path = realpath(optarg, NULL);
1155 if (repo_path == NULL)
1156 err(1, "-r option");
1177 cwd = getcwd(NULL, 0);
1179 error = got_error_from_errno();
1182 if (repo_path == NULL) {
1183 repo_path = strdup(cwd);
1184 if (repo_path == NULL) {
1185 error = got_error_from_errno();
1190 error = apply_unveil(repo_path, NULL);
1194 error = got_repo_open(&repo, repo_path);
1198 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1202 if (commit_id_str == NULL) {
1203 struct got_reference *head_ref;
1204 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1207 error = got_ref_resolve(&commit_id, repo, head_ref);
1208 got_ref_close(head_ref);
1212 error = got_object_resolve_id_str(&commit_id, repo,
1218 error = print_tree(in_repo_path, commit_id, show_ids, repo);
1225 const struct got_error *repo_error;
1226 repo_error = got_repo_close(repo);
1234 static const struct got_error *
1235 cmd_status(int argc __unused, char *argv[] __unused)
1237 git_repository *repo = NULL;
1238 git_status_list *status;
1239 git_status_options statusopts;
1244 if (git_repository_open_ext(&repo, ".", 0, NULL))
1245 errx(1, "git_repository_open: %s", giterr_last()->message);
1247 if (git_repository_is_bare(repo))
1248 errx(1, "bar repository");
1250 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
1251 errx(1, "git_status_init_options: %s", giterr_last()->message);
1253 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
1254 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
1255 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
1256 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1258 if (git_status_list_new(&status, repo, &statusopts))
1259 errx(1, "git_status_list_new: %s", giterr_last()->message);
1261 for (i = 0; i < git_status_list_entrycount(status); i++) {
1262 const git_status_entry *se;
1264 se = git_status_byindex(status, i);
1265 switch (se->status) {
1266 case GIT_STATUS_WT_NEW:
1267 printf("? %s\n", se->index_to_workdir->new_file.path);
1269 case GIT_STATUS_WT_MODIFIED:
1270 printf("M %s\n", se->index_to_workdir->new_file.path);
1272 case GIT_STATUS_WT_DELETED:
1273 printf("R %s\n", se->index_to_workdir->new_file.path);
1275 case GIT_STATUS_WT_RENAMED:
1276 printf("m %s -> %s\n",
1277 se->index_to_workdir->old_file.path,
1278 se->index_to_workdir->new_file.path);
1280 case GIT_STATUS_WT_TYPECHANGE:
1281 printf("t %s\n", se->index_to_workdir->new_file.path);
1283 case GIT_STATUS_INDEX_NEW:
1284 printf("A %s\n", se->head_to_index->new_file.path);
1286 case GIT_STATUS_INDEX_MODIFIED:
1287 printf("M %s\n", se->head_to_index->old_file.path);
1289 case GIT_STATUS_INDEX_DELETED:
1290 printf("R %s\n", se->head_to_index->old_file.path);
1292 case GIT_STATUS_INDEX_RENAMED:
1293 printf("m %s -> %s\n",
1294 se->head_to_index->old_file.path,
1295 se->head_to_index->new_file.path);
1297 case GIT_STATUS_INDEX_TYPECHANGE:
1298 printf("t %s\n", se->head_to_index->old_file.path);
1300 case GIT_STATUS_CURRENT:
1306 git_status_list_free(status);
1307 git_repository_free(repo);
1308 git_libgit2_shutdown();