Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 *
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.
8 *
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.
16 */
18 #include <sys/queue.h>
19 #include <sys/limits.h>
20 #include <sys/types.h>
22 #include <err.h>
23 #include <errno.h>
24 #include <locale.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <libgen.h>
30 #include <time.h>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_reference.h"
35 #include "got_repository.h"
36 #include "got_worktree.h"
37 #include "got_diff.h"
38 #include "got_commit_graph.h"
39 #include "got_blame.h"
41 #ifndef nitems
42 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
43 #endif
45 struct cmd {
46 const char *cmd_name;
47 const struct got_error *(*cmd_main)(int, char *[]);
48 void (*cmd_usage)(void);
49 const char *cmd_descr;
50 };
52 __dead static void usage(void);
53 __dead static void usage_checkout(void);
54 __dead static void usage_log(void);
55 __dead static void usage_diff(void);
56 __dead static void usage_blame(void);
58 static const struct got_error* cmd_checkout(int, char *[]);
59 static const struct got_error* cmd_log(int, char *[]);
60 static const struct got_error* cmd_diff(int, char *[]);
61 static const struct got_error* cmd_blame(int, char *[]);
62 #ifdef notyet
63 static const struct got_error* cmd_status(int, char *[]);
64 #endif
66 static struct cmd got_commands[] = {
67 { "checkout", cmd_checkout, usage_checkout,
68 "check out a new work tree from a repository" },
69 { "log", cmd_log, usage_log,
70 "show repository history" },
71 { "diff", cmd_diff, usage_diff,
72 "compare files and directories" },
73 { "blame", cmd_blame, usage_blame,
74 " show when lines in a file were changed" },
75 #ifdef notyet
76 { "status", cmd_status, usage_status,
77 "show modification status of files" },
78 #endif
79 };
81 int
82 main(int argc, char *argv[])
83 {
84 struct cmd *cmd;
85 unsigned int i;
86 int ch;
87 int hflag = 0;
89 setlocale(LC_ALL, "");
91 while ((ch = getopt(argc, argv, "h")) != -1) {
92 switch (ch) {
93 case 'h':
94 hflag = 1;
95 break;
96 default:
97 usage();
98 /* NOTREACHED */
99 }
102 argc -= optind;
103 argv += optind;
104 optind = 0;
106 if (argc <= 0)
107 usage();
109 for (i = 0; i < nitems(got_commands); i++) {
110 const struct got_error *error;
112 cmd = &got_commands[i];
114 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
115 continue;
117 if (hflag)
118 got_commands[i].cmd_usage();
120 error = got_commands[i].cmd_main(argc, argv);
121 if (error) {
122 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
123 return 1;
126 return 0;
129 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
130 return 1;
133 __dead static void
134 usage(void)
136 int i;
138 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
139 "Available commands:\n", getprogname());
140 for (i = 0; i < nitems(got_commands); i++) {
141 struct cmd *cmd = &got_commands[i];
142 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
144 exit(1);
147 __dead static void
148 usage_checkout(void)
150 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
151 "[worktree-path]\n", getprogname());
152 exit(1);
155 static void
156 checkout_progress(void *arg, const char *path)
158 char *worktree_path = arg;
160 while (path[0] == '/')
161 path++;
163 printf("A %s/%s\n", worktree_path, path);
166 static const struct got_error *
167 cmd_checkout(int argc, char *argv[])
169 const struct got_error *error = NULL;
170 struct got_repository *repo = NULL;
171 struct got_reference *head_ref = NULL;
172 struct got_worktree *worktree = NULL;
173 char *repo_path = NULL;
174 char *worktree_path = NULL;
175 const char *path_prefix = "";
176 int ch;
178 while ((ch = getopt(argc, argv, "p:")) != -1) {
179 switch (ch) {
180 case 'p':
181 path_prefix = optarg;
182 break;
183 default:
184 usage();
185 /* NOTREACHED */
189 argc -= optind;
190 argv += optind;
192 #ifndef PROFILE
193 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
194 == -1)
195 err(1, "pledge");
196 #endif
197 if (argc == 1) {
198 char *cwd, *base, *dotgit;
199 repo_path = realpath(argv[0], NULL);
200 if (repo_path == NULL)
201 return got_error_from_errno();
202 cwd = getcwd(NULL, 0);
203 if (cwd == NULL) {
204 error = got_error_from_errno();
205 goto done;
207 if (path_prefix[0])
208 base = basename(path_prefix);
209 else
210 base = basename(repo_path);
211 if (base == NULL) {
212 error = got_error_from_errno();
213 goto done;
215 dotgit = strstr(base, ".git");
216 if (dotgit)
217 *dotgit = '\0';
218 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
219 error = got_error_from_errno();
220 free(cwd);
221 goto done;
223 free(cwd);
224 } else if (argc == 2) {
225 repo_path = realpath(argv[0], NULL);
226 if (repo_path == NULL) {
227 error = got_error_from_errno();
228 goto done;
230 worktree_path = realpath(argv[1], NULL);
231 if (worktree_path == NULL) {
232 error = got_error_from_errno();
233 goto done;
235 } else
236 usage_checkout();
238 error = got_repo_open(&repo, repo_path);
239 if (error != NULL)
240 goto done;
241 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
242 if (error != NULL)
243 goto done;
245 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
246 if (error != NULL)
247 goto done;
249 error = got_worktree_open(&worktree, worktree_path);
250 if (error != NULL)
251 goto done;
253 error = got_worktree_checkout_files(worktree, head_ref, repo,
254 checkout_progress, worktree_path);
255 if (error != NULL)
256 goto done;
258 printf("Checked out %s\n", worktree_path);
259 printf("Now shut up and hack\n");
261 done:
262 free(repo_path);
263 free(worktree_path);
264 return error;
267 static const struct got_error *
268 print_patch(struct got_commit_object *commit, struct got_object_id *id,
269 struct got_repository *repo)
271 const struct got_error *err = NULL;
272 struct got_tree_object *tree1 = NULL, *tree2;
273 struct got_object_qid *qid;
275 err = got_object_open_as_tree(&tree2, repo, commit->tree_id);
276 if (err)
277 return err;
279 qid = SIMPLEQ_FIRST(&commit->parent_ids);
280 if (qid != NULL) {
281 struct got_commit_object *pcommit;
283 err = got_object_open_as_commit(&pcommit, repo, qid->id);
284 if (err)
285 return err;
287 err = got_object_open_as_tree(&tree1, repo, pcommit->tree_id);
288 got_object_commit_close(pcommit);
289 if (err)
290 return err;
293 err = got_diff_tree(tree1, tree2, "", "", repo, stdout);
294 if (tree1)
295 got_object_tree_close(tree1);
296 got_object_tree_close(tree2);
297 return err;
300 static char *
301 get_datestr(time_t *time, char *datebuf)
303 char *p, *s = ctime_r(time, datebuf);
304 p = strchr(s, '\n');
305 if (p)
306 *p = '\0';
307 return s;
310 static const struct got_error *
311 print_commit(struct got_commit_object *commit, struct got_object_id *id,
312 struct got_repository *repo, int show_patch)
314 const struct got_error *err = NULL;
315 char *id_str, *datestr, *logmsg0, *logmsg, *line;
316 char datebuf[26];
317 time_t author_time, committer_time;
319 err = got_object_id_str(&id_str, id);
320 if (err)
321 return err;
323 author_time = mktime(&commit->tm_author);
324 committer_time = mktime(&commit->tm_committer);
325 #if 0
326 /* This would express the date in committer's timezone. */
327 author_time += commit->tm_author.tm_gmtoff;
328 committer_time += commit->tm_committer.tm_gmtoff;
329 #endif
331 printf("-----------------------------------------------\n");
332 printf("commit %s\n", id_str);
333 free(id_str);
334 datestr = get_datestr(&author_time, datebuf);
335 printf("author: %s %s UTC\n", commit->author, datestr);
336 if (strcmp(commit->author, commit->committer) != 0 ||
337 author_time != committer_time) {
338 datestr = get_datestr(&committer_time, datebuf);
339 printf("committer: %s %s UTC\n", commit->committer, datestr);
341 if (commit->nparents > 1) {
342 struct got_object_qid *qid;
343 int n = 1;
344 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
345 err = got_object_id_str(&id_str, qid->id);
346 if (err)
347 return err;
348 printf("parent %d: %s\n", n++, id_str);
349 free(id_str);
353 logmsg0 = strdup(commit->logmsg);
354 if (logmsg0 == NULL)
355 return got_error_from_errno();
357 logmsg = logmsg0;
358 do {
359 line = strsep(&logmsg, "\n");
360 if (line)
361 printf(" %s\n", line);
362 } while (line);
363 free(logmsg0);
365 if (show_patch) {
366 err = print_patch(commit, id, repo);
367 if (err == 0)
368 printf("\n");
371 return err;
374 static const struct got_error *
375 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
376 struct got_repository *repo, char *path, int show_patch, int limit,
377 int first_parent_traversal)
379 const struct got_error *err;
380 struct got_commit_graph *graph;
381 int ncommits, found_obj = 0;
382 int is_root_path = (strcmp(path, "/") == 0);
384 err = got_commit_graph_open(&graph, root_id, first_parent_traversal,
385 repo);
386 if (err)
387 return err;
388 err = got_commit_graph_iter_start(graph, root_id);
389 if (err)
390 goto done;
391 do {
392 struct got_commit_object *commit;
393 struct got_object_id *id;
395 err = got_commit_graph_iter_next(&id, graph);
396 if (err) {
397 if (err->code == GOT_ERR_ITER_COMPLETED) {
398 err = NULL;
399 break;
401 if (err->code != GOT_ERR_ITER_NEED_MORE)
402 break;
403 err = got_commit_graph_fetch_commits(&ncommits,
404 graph, 1, repo);
405 if (err)
406 break;
407 else
408 continue;
410 if (id == NULL)
411 break;
413 err = got_object_open_as_commit(&commit, repo, id);
414 if (err)
415 break;
416 if (!is_root_path) {
417 struct got_object *obj;
418 struct got_object_qid *pid;
419 int changed = 0;
421 err = got_object_open_by_path(&obj, repo, id, path);
422 if (err) {
423 got_object_commit_close(commit);
424 if (err->code == GOT_ERR_NO_OBJ && found_obj) {
425 /*
426 * History of the path stops here
427 * on the current commit's branch.
428 * Keep logging on other branches.
429 */
430 err = NULL;
431 continue;
433 break;
435 found_obj = 1;
437 pid = SIMPLEQ_FIRST(&commit->parent_ids);
438 if (pid != NULL) {
439 struct got_object *pobj;
440 err = got_object_open_by_path(&pobj, repo,
441 pid->id, path);
442 if (err) {
443 if (err->code != GOT_ERR_NO_OBJ) {
444 got_object_close(obj);
445 got_object_commit_close(commit);
446 break;
448 err = NULL;
449 changed = 1;
450 } else {
451 struct got_object_id *id, *pid;
452 id = got_object_get_id(obj);
453 if (id == NULL) {
454 err = got_error_from_errno();
455 got_object_close(obj);
456 break;
458 pid = got_object_get_id(pobj);
459 if (pid == NULL) {
460 err = got_error_from_errno();
461 free(id);
462 got_object_close(obj);
463 got_object_close(pobj);
464 break;
466 changed =
467 (got_object_id_cmp(id, pid) != 0);
468 got_object_close(pobj);
469 free(id);
470 free(pid);
473 got_object_close(obj);
474 if (!changed) {
475 got_object_commit_close(commit);
476 continue;
479 err = print_commit(commit, id, repo, show_patch);
480 got_object_commit_close(commit);
481 if (err || (limit && --limit == 0))
482 break;
483 } while (ncommits > 0);
484 done:
485 got_commit_graph_close(graph);
486 return err;
489 __dead static void
490 usage_log(void)
492 fprintf(stderr, "usage: %s log [-c commit] [-f] [ -l N ] [-p] "
493 "[-r repository-path] [path]\n", getprogname());
494 exit(1);
497 static const struct got_error *
498 cmd_log(int argc, char *argv[])
500 const struct got_error *error;
501 struct got_repository *repo = NULL;
502 struct got_object_id *id = NULL;
503 struct got_object *obj = NULL;
504 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
505 char *start_commit = NULL;
506 int ch;
507 int show_patch = 0, limit = 0, first_parent_traversal = 0;
508 const char *errstr;
510 #ifndef PROFILE
511 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
512 == -1)
513 err(1, "pledge");
514 #endif
516 while ((ch = getopt(argc, argv, "pc:l:fr:")) != -1) {
517 switch (ch) {
518 case 'p':
519 show_patch = 1;
520 break;
521 case 'c':
522 start_commit = optarg;
523 break;
524 case 'l':
525 limit = strtonum(optarg, 1, INT_MAX, &errstr);
526 if (errstr != NULL)
527 err(1, "-l option %s", errstr);
528 break;
529 case 'f':
530 first_parent_traversal = 1;
531 break;
532 case 'r':
533 repo_path = realpath(optarg, NULL);
534 if (repo_path == NULL)
535 err(1, "-r option");
536 break;
537 default:
538 usage();
539 /* NOTREACHED */
543 argc -= optind;
544 argv += optind;
546 if (argc == 0)
547 path = strdup("");
548 else if (argc == 1)
549 path = strdup(argv[0]);
550 else
551 usage_log();
552 if (path == NULL)
553 return got_error_from_errno();
555 cwd = getcwd(NULL, 0);
556 if (cwd == NULL) {
557 error = got_error_from_errno();
558 goto done;
560 if (repo_path == NULL) {
561 repo_path = strdup(cwd);
562 if (repo_path == NULL) {
563 error = got_error_from_errno();
564 goto done;
568 error = got_repo_open(&repo, repo_path);
569 if (error != NULL)
570 goto done;
572 if (start_commit == NULL) {
573 struct got_reference *head_ref;
574 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
575 if (error != NULL)
576 return error;
577 error = got_ref_resolve(&id, repo, head_ref);
578 got_ref_close(head_ref);
579 if (error != NULL)
580 return error;
581 error = got_object_open(&obj, repo, id);
582 } else {
583 struct got_reference *ref;
584 error = got_ref_open(&ref, repo, start_commit);
585 if (error == NULL) {
586 error = got_ref_resolve(&id, repo, ref);
587 got_ref_close(ref);
588 if (error != NULL)
589 return error;
590 error = got_object_open(&obj, repo, id);
591 if (error != NULL)
592 return error;
594 if (obj == NULL) {
595 error = got_object_open_by_id_str(&obj, repo,
596 start_commit);
597 if (error != NULL)
598 return error;
599 id = got_object_get_id(obj);
600 if (id == NULL)
601 error = got_error_from_errno();
604 if (error != NULL)
605 goto done;
606 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
607 error = got_error(GOT_ERR_OBJ_TYPE);
608 goto done;
611 error = got_repo_map_path(&in_repo_path, repo, path);
612 if (error != NULL)
613 goto done;
614 if (in_repo_path) {
615 free(path);
616 path = in_repo_path;
619 error = print_commits(obj, id, repo, path, show_patch,
620 limit, first_parent_traversal);
621 done:
622 free(path);
623 free(repo_path);
624 free(cwd);
625 if (obj)
626 got_object_close(obj);
627 free(id);
628 if (repo) {
629 const struct got_error *repo_error;
630 repo_error = got_repo_close(repo);
631 if (error == NULL)
632 error = repo_error;
634 return error;
637 __dead static void
638 usage_diff(void)
640 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
641 getprogname());
642 exit(1);
645 static const struct got_error *
646 cmd_diff(int argc, char *argv[])
648 const struct got_error *error;
649 struct got_repository *repo = NULL;
650 struct got_object_id *id1 = NULL, *id2 = NULL;
651 struct got_object *obj1 = NULL, *obj2 = NULL;
652 char *repo_path = NULL;
653 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
654 int ch;
656 #ifndef PROFILE
657 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
658 == -1)
659 err(1, "pledge");
660 #endif
662 while ((ch = getopt(argc, argv, "")) != -1) {
663 switch (ch) {
664 default:
665 usage();
666 /* NOTREACHED */
670 argc -= optind;
671 argv += optind;
673 if (argc == 0) {
674 usage_diff(); /* TODO show local worktree changes */
675 } else if (argc == 2) {
676 repo_path = getcwd(NULL, 0);
677 if (repo_path == NULL)
678 return got_error_from_errno();
679 obj_id_str1 = argv[0];
680 obj_id_str2 = argv[1];
681 } else if (argc == 3) {
682 repo_path = realpath(argv[0], NULL);
683 if (repo_path == NULL)
684 return got_error_from_errno();
685 obj_id_str1 = argv[1];
686 obj_id_str2 = argv[2];
687 } else
688 usage_diff();
690 error = got_repo_open(&repo, repo_path);
691 free(repo_path);
692 if (error != NULL)
693 goto done;
695 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
696 if (error == NULL) {
697 id1 = got_object_get_id(obj1);
698 if (id1 == NULL)
699 error = got_error_from_errno();
701 if (error != NULL)
702 goto done;
704 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
705 if (error == NULL) {
706 id2 = got_object_get_id(obj2);
707 if (id2 == NULL)
708 error = got_error_from_errno();
710 if (error != NULL)
711 goto done;
713 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
714 error = got_error(GOT_ERR_OBJ_TYPE);
715 goto done;
718 switch (got_object_get_type(obj1)) {
719 case GOT_OBJ_TYPE_BLOB:
720 error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
721 repo, stdout);
722 break;
723 case GOT_OBJ_TYPE_TREE:
724 error = got_diff_objects_as_trees(obj1, obj2, "", "", repo,
725 stdout);
726 break;
727 case GOT_OBJ_TYPE_COMMIT:
728 error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
729 break;
730 default:
731 error = got_error(GOT_ERR_OBJ_TYPE);
734 done:
735 if (obj1)
736 got_object_close(obj1);
737 if (obj2)
738 got_object_close(obj2);
739 if (id1)
740 free(id1);
741 if (id2)
742 free(id2);
743 if (repo) {
744 const struct got_error *repo_error;
745 repo_error = got_repo_close(repo);
746 if (error == NULL)
747 error = repo_error;
749 return error;
752 __dead static void
753 usage_blame(void)
755 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
756 getprogname());
757 exit(1);
760 static const struct got_error *
761 cmd_blame(int argc, char *argv[])
763 const struct got_error *error;
764 struct got_repository *repo = NULL;
765 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
766 struct got_object_id *commit_id = NULL;
767 char *commit_id_str = NULL;
768 int ch;
770 #ifndef PROFILE
771 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
772 == -1)
773 err(1, "pledge");
774 #endif
776 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
777 switch (ch) {
778 case 'c':
779 commit_id_str = optarg;
780 break;
781 case 'r':
782 repo_path = realpath(optarg, NULL);
783 if (repo_path == NULL)
784 err(1, "-r option");
785 break;
786 default:
787 usage();
788 /* NOTREACHED */
792 argc -= optind;
793 argv += optind;
795 if (argc == 1)
796 path = argv[0];
797 else
798 usage_blame();
800 cwd = getcwd(NULL, 0);
801 if (cwd == NULL) {
802 error = got_error_from_errno();
803 goto done;
805 if (repo_path == NULL) {
806 repo_path = strdup(cwd);
807 if (repo_path == NULL) {
808 error = got_error_from_errno();
809 goto done;
813 error = got_repo_open(&repo, repo_path);
814 if (error != NULL)
815 goto done;
817 error = got_repo_map_path(&in_repo_path, repo, path);
818 if (error != NULL)
819 goto done;
821 if (commit_id_str == NULL) {
822 struct got_reference *head_ref;
823 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
824 if (error != NULL)
825 goto done;
826 error = got_ref_resolve(&commit_id, repo, head_ref);
827 got_ref_close(head_ref);
828 if (error != NULL)
829 goto done;
830 } else {
831 struct got_object *obj;
832 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
833 if (error != NULL)
834 goto done;
835 commit_id = got_object_get_id(obj);
836 if (commit_id == NULL)
837 error = got_error_from_errno();
838 got_object_close(obj);
841 error = got_blame(in_repo_path, commit_id, repo, stdout);
842 done:
843 free(in_repo_path);
844 free(repo_path);
845 free(cwd);
846 free(commit_id);
847 if (repo) {
848 const struct got_error *repo_error;
849 repo_error = got_repo_close(repo);
850 if (error == NULL)
851 error = repo_error;
853 return error;
856 #ifdef notyet
857 static const struct got_error *
858 cmd_status(int argc __unused, char *argv[] __unused)
860 git_repository *repo = NULL;
861 git_status_list *status;
862 git_status_options statusopts;
863 size_t i;
865 git_libgit2_init();
867 if (git_repository_open_ext(&repo, ".", 0, NULL))
868 errx(1, "git_repository_open: %s", giterr_last()->message);
870 if (git_repository_is_bare(repo))
871 errx(1, "bar repository");
873 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
874 errx(1, "git_status_init_options: %s", giterr_last()->message);
876 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
877 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
878 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
879 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
881 if (git_status_list_new(&status, repo, &statusopts))
882 errx(1, "git_status_list_new: %s", giterr_last()->message);
884 for (i = 0; i < git_status_list_entrycount(status); i++) {
885 const git_status_entry *se;
887 se = git_status_byindex(status, i);
888 switch (se->status) {
889 case GIT_STATUS_WT_NEW:
890 printf("? %s\n", se->index_to_workdir->new_file.path);
891 break;
892 case GIT_STATUS_WT_MODIFIED:
893 printf("M %s\n", se->index_to_workdir->new_file.path);
894 break;
895 case GIT_STATUS_WT_DELETED:
896 printf("R %s\n", se->index_to_workdir->new_file.path);
897 break;
898 case GIT_STATUS_WT_RENAMED:
899 printf("m %s -> %s\n",
900 se->index_to_workdir->old_file.path,
901 se->index_to_workdir->new_file.path);
902 break;
903 case GIT_STATUS_WT_TYPECHANGE:
904 printf("t %s\n", se->index_to_workdir->new_file.path);
905 break;
906 case GIT_STATUS_INDEX_NEW:
907 printf("A %s\n", se->head_to_index->new_file.path);
908 break;
909 case GIT_STATUS_INDEX_MODIFIED:
910 printf("M %s\n", se->head_to_index->old_file.path);
911 break;
912 case GIT_STATUS_INDEX_DELETED:
913 printf("R %s\n", se->head_to_index->old_file.path);
914 break;
915 case GIT_STATUS_INDEX_RENAMED:
916 printf("m %s -> %s\n",
917 se->head_to_index->old_file.path,
918 se->head_to_index->new_file.path);
919 break;
920 case GIT_STATUS_INDEX_TYPECHANGE:
921 printf("t %s\n", se->head_to_index->old_file.path);
922 break;
923 case GIT_STATUS_CURRENT:
924 default:
925 break;
929 git_status_list_free(status);
930 git_repository_free(repo);
931 git_libgit2_shutdown();
933 return 0;
935 #endif