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 printf("from: %s\n", commit->author);
335 datestr = get_datestr(&committer_time, datebuf);
336 printf("date: %s UTC\n", datestr);
337 if (strcmp(commit->author, commit->committer) != 0)
338 printf("via: %s\n", commit->committer);
339 if (commit->nparents > 1) {
340 struct got_object_qid *qid;
341 int n = 1;
342 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
343 err = got_object_id_str(&id_str, qid->id);
344 if (err)
345 return err;
346 printf("parent %d: %s\n", n++, id_str);
347 free(id_str);
351 logmsg0 = strdup(commit->logmsg);
352 if (logmsg0 == NULL)
353 return got_error_from_errno();
355 logmsg = logmsg0;
356 do {
357 line = strsep(&logmsg, "\n");
358 if (line)
359 printf(" %s\n", line);
360 } while (line);
361 free(logmsg0);
363 if (show_patch) {
364 err = print_patch(commit, id, repo);
365 if (err == 0)
366 printf("\n");
369 return err;
372 static const struct got_error *
373 detect_change(int *changed, struct got_object_id *commit_id,
374 struct got_object *obj, const char *path, struct got_repository *repo)
376 const struct got_error *err = NULL;
377 struct got_object_id *id, *pid;
378 struct got_object *pobj;
380 err = got_object_open_by_path(&pobj, repo, commit_id, path);
381 if (err) {
382 if (err->code != GOT_ERR_NO_OBJ)
383 return err;
384 *changed = 1;
385 return NULL;
388 id = got_object_get_id(obj);
389 if (id == NULL)
390 return got_error_from_errno();
391 pid = got_object_get_id(pobj);
392 if (pid == NULL) {
393 err = got_error_from_errno();
394 free(id);
395 return err;
398 *changed = (got_object_id_cmp(id, pid) != 0);
399 got_object_close(pobj);
400 free(id);
401 free(pid);
402 return NULL;
405 static const struct got_error *
406 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
407 struct got_repository *repo, char *path, int show_patch, int limit,
408 int first_parent_traversal)
410 const struct got_error *err;
411 struct got_commit_graph *graph;
412 int ncommits, found_obj = 0;
413 int is_root_path = (strcmp(path, "/") == 0);
415 err = got_commit_graph_open(&graph, root_id, first_parent_traversal,
416 repo);
417 if (err)
418 return err;
419 err = got_commit_graph_iter_start(graph, root_id);
420 if (err)
421 goto done;
422 do {
423 struct got_commit_object *commit;
424 struct got_object_id *id;
426 err = got_commit_graph_iter_next(&id, graph);
427 if (err) {
428 if (err->code == GOT_ERR_ITER_COMPLETED) {
429 err = NULL;
430 break;
432 if (err->code != GOT_ERR_ITER_NEED_MORE)
433 break;
434 err = got_commit_graph_fetch_commits(&ncommits,
435 graph, 1, repo);
436 if (err)
437 break;
438 else
439 continue;
441 if (id == NULL)
442 break;
444 err = got_object_open_as_commit(&commit, repo, id);
445 if (err)
446 break;
447 if (!is_root_path) {
448 struct got_object *obj;
449 struct got_object_qid *pid;
450 int changed = 0;
452 err = got_object_open_by_path(&obj, repo, id, path);
453 if (err) {
454 got_object_commit_close(commit);
455 if (err->code == GOT_ERR_NO_OBJ && found_obj) {
456 /*
457 * History of the path stops here
458 * on the current commit's branch.
459 * Keep logging on other branches.
460 */
461 err = NULL;
462 continue;
464 break;
466 found_obj = 1;
468 pid = SIMPLEQ_FIRST(&commit->parent_ids);
469 if (pid) {
470 err = detect_change(&changed, pid->id, obj,
471 path, repo);
472 if (err) {
473 got_object_close(obj);
474 got_object_commit_close(commit);
475 break;
478 got_object_close(obj);
479 if (!changed) {
480 got_object_commit_close(commit);
481 continue;
484 err = print_commit(commit, id, repo, show_patch);
485 got_object_commit_close(commit);
486 if (err || (limit && --limit == 0))
487 break;
488 } while (ncommits > 0);
489 done:
490 got_commit_graph_close(graph);
491 return err;
494 __dead static void
495 usage_log(void)
497 fprintf(stderr, "usage: %s log [-c commit] [-f] [ -l N ] [-p] "
498 "[-r repository-path] [path]\n", getprogname());
499 exit(1);
502 static const struct got_error *
503 cmd_log(int argc, char *argv[])
505 const struct got_error *error;
506 struct got_repository *repo = NULL;
507 struct got_object_id *id = NULL;
508 struct got_object *obj = NULL;
509 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
510 char *start_commit = NULL;
511 int ch;
512 int show_patch = 0, limit = 0, first_parent_traversal = 0;
513 const char *errstr;
515 #ifndef PROFILE
516 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
517 == -1)
518 err(1, "pledge");
519 #endif
521 while ((ch = getopt(argc, argv, "pc:l:fr:")) != -1) {
522 switch (ch) {
523 case 'p':
524 show_patch = 1;
525 break;
526 case 'c':
527 start_commit = optarg;
528 break;
529 case 'l':
530 limit = strtonum(optarg, 1, INT_MAX, &errstr);
531 if (errstr != NULL)
532 err(1, "-l option %s", errstr);
533 break;
534 case 'f':
535 first_parent_traversal = 1;
536 break;
537 case 'r':
538 repo_path = realpath(optarg, NULL);
539 if (repo_path == NULL)
540 err(1, "-r option");
541 break;
542 default:
543 usage();
544 /* NOTREACHED */
548 argc -= optind;
549 argv += optind;
551 if (argc == 0)
552 path = strdup("");
553 else if (argc == 1)
554 path = strdup(argv[0]);
555 else
556 usage_log();
557 if (path == NULL)
558 return got_error_from_errno();
560 cwd = getcwd(NULL, 0);
561 if (cwd == NULL) {
562 error = got_error_from_errno();
563 goto done;
565 if (repo_path == NULL) {
566 repo_path = strdup(cwd);
567 if (repo_path == NULL) {
568 error = got_error_from_errno();
569 goto done;
573 error = got_repo_open(&repo, repo_path);
574 if (error != NULL)
575 goto done;
577 if (start_commit == NULL) {
578 struct got_reference *head_ref;
579 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
580 if (error != NULL)
581 return error;
582 error = got_ref_resolve(&id, repo, head_ref);
583 got_ref_close(head_ref);
584 if (error != NULL)
585 return error;
586 error = got_object_open(&obj, repo, id);
587 } else {
588 struct got_reference *ref;
589 error = got_ref_open(&ref, repo, start_commit);
590 if (error == NULL) {
591 error = got_ref_resolve(&id, repo, ref);
592 got_ref_close(ref);
593 if (error != NULL)
594 return error;
595 error = got_object_open(&obj, repo, id);
596 if (error != NULL)
597 return error;
599 if (obj == NULL) {
600 error = got_object_open_by_id_str(&obj, repo,
601 start_commit);
602 if (error != NULL)
603 return error;
604 id = got_object_get_id(obj);
605 if (id == NULL)
606 error = got_error_from_errno();
609 if (error != NULL)
610 goto done;
611 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
612 error = got_error(GOT_ERR_OBJ_TYPE);
613 goto done;
616 error = got_repo_map_path(&in_repo_path, repo, path);
617 if (error != NULL)
618 goto done;
619 if (in_repo_path) {
620 free(path);
621 path = in_repo_path;
624 error = print_commits(obj, id, repo, path, show_patch,
625 limit, first_parent_traversal);
626 done:
627 free(path);
628 free(repo_path);
629 free(cwd);
630 if (obj)
631 got_object_close(obj);
632 free(id);
633 if (repo) {
634 const struct got_error *repo_error;
635 repo_error = got_repo_close(repo);
636 if (error == NULL)
637 error = repo_error;
639 return error;
642 __dead static void
643 usage_diff(void)
645 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
646 getprogname());
647 exit(1);
650 static const struct got_error *
651 cmd_diff(int argc, char *argv[])
653 const struct got_error *error;
654 struct got_repository *repo = NULL;
655 struct got_object_id *id1 = NULL, *id2 = NULL;
656 struct got_object *obj1 = NULL, *obj2 = NULL;
657 char *repo_path = NULL;
658 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
659 int ch;
661 #ifndef PROFILE
662 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
663 == -1)
664 err(1, "pledge");
665 #endif
667 while ((ch = getopt(argc, argv, "")) != -1) {
668 switch (ch) {
669 default:
670 usage();
671 /* NOTREACHED */
675 argc -= optind;
676 argv += optind;
678 if (argc == 0) {
679 usage_diff(); /* TODO show local worktree changes */
680 } else if (argc == 2) {
681 repo_path = getcwd(NULL, 0);
682 if (repo_path == NULL)
683 return got_error_from_errno();
684 obj_id_str1 = argv[0];
685 obj_id_str2 = argv[1];
686 } else if (argc == 3) {
687 repo_path = realpath(argv[0], NULL);
688 if (repo_path == NULL)
689 return got_error_from_errno();
690 obj_id_str1 = argv[1];
691 obj_id_str2 = argv[2];
692 } else
693 usage_diff();
695 error = got_repo_open(&repo, repo_path);
696 free(repo_path);
697 if (error != NULL)
698 goto done;
700 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
701 if (error == NULL) {
702 id1 = got_object_get_id(obj1);
703 if (id1 == NULL)
704 error = got_error_from_errno();
706 if (error != NULL)
707 goto done;
709 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
710 if (error == NULL) {
711 id2 = got_object_get_id(obj2);
712 if (id2 == NULL)
713 error = got_error_from_errno();
715 if (error != NULL)
716 goto done;
718 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
719 error = got_error(GOT_ERR_OBJ_TYPE);
720 goto done;
723 switch (got_object_get_type(obj1)) {
724 case GOT_OBJ_TYPE_BLOB:
725 error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
726 repo, stdout);
727 break;
728 case GOT_OBJ_TYPE_TREE:
729 error = got_diff_objects_as_trees(obj1, obj2, "", "", repo,
730 stdout);
731 break;
732 case GOT_OBJ_TYPE_COMMIT:
733 error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
734 break;
735 default:
736 error = got_error(GOT_ERR_OBJ_TYPE);
739 done:
740 if (obj1)
741 got_object_close(obj1);
742 if (obj2)
743 got_object_close(obj2);
744 if (id1)
745 free(id1);
746 if (id2)
747 free(id2);
748 if (repo) {
749 const struct got_error *repo_error;
750 repo_error = got_repo_close(repo);
751 if (error == NULL)
752 error = repo_error;
754 return error;
757 __dead static void
758 usage_blame(void)
760 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
761 getprogname());
762 exit(1);
765 static const struct got_error *
766 cmd_blame(int argc, char *argv[])
768 const struct got_error *error;
769 struct got_repository *repo = NULL;
770 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
771 struct got_object_id *commit_id = NULL;
772 char *commit_id_str = NULL;
773 int ch;
775 #ifndef PROFILE
776 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
777 == -1)
778 err(1, "pledge");
779 #endif
781 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
782 switch (ch) {
783 case 'c':
784 commit_id_str = optarg;
785 break;
786 case 'r':
787 repo_path = realpath(optarg, NULL);
788 if (repo_path == NULL)
789 err(1, "-r option");
790 break;
791 default:
792 usage();
793 /* NOTREACHED */
797 argc -= optind;
798 argv += optind;
800 if (argc == 1)
801 path = argv[0];
802 else
803 usage_blame();
805 cwd = getcwd(NULL, 0);
806 if (cwd == NULL) {
807 error = got_error_from_errno();
808 goto done;
810 if (repo_path == NULL) {
811 repo_path = strdup(cwd);
812 if (repo_path == NULL) {
813 error = got_error_from_errno();
814 goto done;
818 error = got_repo_open(&repo, repo_path);
819 if (error != NULL)
820 goto done;
822 error = got_repo_map_path(&in_repo_path, repo, path);
823 if (error != NULL)
824 goto done;
826 if (commit_id_str == NULL) {
827 struct got_reference *head_ref;
828 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
829 if (error != NULL)
830 goto done;
831 error = got_ref_resolve(&commit_id, repo, head_ref);
832 got_ref_close(head_ref);
833 if (error != NULL)
834 goto done;
835 } else {
836 struct got_object *obj;
837 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
838 if (error != NULL)
839 goto done;
840 commit_id = got_object_get_id(obj);
841 if (commit_id == NULL)
842 error = got_error_from_errno();
843 got_object_close(obj);
846 error = got_blame(in_repo_path, commit_id, repo, stdout);
847 done:
848 free(in_repo_path);
849 free(repo_path);
850 free(cwd);
851 free(commit_id);
852 if (repo) {
853 const struct got_error *repo_error;
854 repo_error = got_repo_close(repo);
855 if (error == NULL)
856 error = repo_error;
858 return error;
861 #ifdef notyet
862 static const struct got_error *
863 cmd_status(int argc __unused, char *argv[] __unused)
865 git_repository *repo = NULL;
866 git_status_list *status;
867 git_status_options statusopts;
868 size_t i;
870 git_libgit2_init();
872 if (git_repository_open_ext(&repo, ".", 0, NULL))
873 errx(1, "git_repository_open: %s", giterr_last()->message);
875 if (git_repository_is_bare(repo))
876 errx(1, "bar repository");
878 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
879 errx(1, "git_status_init_options: %s", giterr_last()->message);
881 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
882 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
883 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
884 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
886 if (git_status_list_new(&status, repo, &statusopts))
887 errx(1, "git_status_list_new: %s", giterr_last()->message);
889 for (i = 0; i < git_status_list_entrycount(status); i++) {
890 const git_status_entry *se;
892 se = git_status_byindex(status, i);
893 switch (se->status) {
894 case GIT_STATUS_WT_NEW:
895 printf("? %s\n", se->index_to_workdir->new_file.path);
896 break;
897 case GIT_STATUS_WT_MODIFIED:
898 printf("M %s\n", se->index_to_workdir->new_file.path);
899 break;
900 case GIT_STATUS_WT_DELETED:
901 printf("R %s\n", se->index_to_workdir->new_file.path);
902 break;
903 case GIT_STATUS_WT_RENAMED:
904 printf("m %s -> %s\n",
905 se->index_to_workdir->old_file.path,
906 se->index_to_workdir->new_file.path);
907 break;
908 case GIT_STATUS_WT_TYPECHANGE:
909 printf("t %s\n", se->index_to_workdir->new_file.path);
910 break;
911 case GIT_STATUS_INDEX_NEW:
912 printf("A %s\n", se->head_to_index->new_file.path);
913 break;
914 case GIT_STATUS_INDEX_MODIFIED:
915 printf("M %s\n", se->head_to_index->old_file.path);
916 break;
917 case GIT_STATUS_INDEX_DELETED:
918 printf("R %s\n", se->head_to_index->old_file.path);
919 break;
920 case GIT_STATUS_INDEX_RENAMED:
921 printf("m %s -> %s\n",
922 se->head_to_index->old_file.path,
923 se->head_to_index->new_file.path);
924 break;
925 case GIT_STATUS_INDEX_TYPECHANGE:
926 printf("t %s\n", se->head_to_index->old_file.path);
927 break;
928 case GIT_STATUS_CURRENT:
929 default:
930 break;
934 git_status_list_free(status);
935 git_repository_free(repo);
936 git_libgit2_shutdown();
938 return 0;
940 #endif