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>
21 #include <sys/stat.h>
23 #include <err.h>
24 #include <errno.h>
25 #include <locale.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <libgen.h>
31 #include <time.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_reference.h"
36 #include "got_repository.h"
37 #include "got_worktree.h"
38 #include "got_diff.h"
39 #include "got_commit_graph.h"
40 #include "got_blame.h"
42 #ifndef nitems
43 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
44 #endif
46 struct cmd {
47 const char *cmd_name;
48 const struct got_error *(*cmd_main)(int, char *[]);
49 void (*cmd_usage)(void);
50 const char *cmd_descr;
51 };
53 __dead static void usage(void);
54 __dead static void usage_checkout(void);
55 __dead static void usage_log(void);
56 __dead static void usage_diff(void);
57 __dead static void usage_blame(void);
58 __dead static void usage_tree(void);
60 static const struct got_error* cmd_checkout(int, char *[]);
61 static const struct got_error* cmd_log(int, char *[]);
62 static const struct got_error* cmd_diff(int, char *[]);
63 static const struct got_error* cmd_blame(int, char *[]);
64 static const struct got_error* cmd_tree(int, char *[]);
65 #ifdef notyet
66 static const struct got_error* cmd_status(int, char *[]);
67 #endif
69 static struct cmd got_commands[] = {
70 { "checkout", cmd_checkout, usage_checkout,
71 "check out a new work tree from a repository" },
72 { "log", cmd_log, usage_log,
73 "show repository history" },
74 { "diff", cmd_diff, usage_diff,
75 "compare files and directories" },
76 { "blame", cmd_blame, usage_blame,
77 " show when lines in a file were changed" },
78 { "tree", cmd_tree, usage_tree,
79 " list files and directories in repository" },
80 #ifdef notyet
81 { "status", cmd_status, usage_status,
82 "show modification status of files" },
83 #endif
84 };
86 int
87 main(int argc, char *argv[])
88 {
89 struct cmd *cmd;
90 unsigned int i;
91 int ch;
92 int hflag = 0;
94 setlocale(LC_ALL, "");
96 while ((ch = getopt(argc, argv, "h")) != -1) {
97 switch (ch) {
98 case 'h':
99 hflag = 1;
100 break;
101 default:
102 usage();
103 /* NOTREACHED */
107 argc -= optind;
108 argv += optind;
109 optind = 0;
111 if (argc <= 0)
112 usage();
114 for (i = 0; i < nitems(got_commands); i++) {
115 const struct got_error *error;
117 cmd = &got_commands[i];
119 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
120 continue;
122 if (hflag)
123 got_commands[i].cmd_usage();
125 error = got_commands[i].cmd_main(argc, argv);
126 if (error) {
127 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
128 return 1;
131 return 0;
134 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
135 return 1;
138 __dead static void
139 usage(void)
141 int i;
143 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
144 "Available commands:\n", getprogname());
145 for (i = 0; i < nitems(got_commands); i++) {
146 struct cmd *cmd = &got_commands[i];
147 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
149 exit(1);
152 __dead static void
153 usage_checkout(void)
155 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
156 "[worktree-path]\n", getprogname());
157 exit(1);
160 static void
161 checkout_progress(void *arg, const char *path)
163 char *worktree_path = arg;
165 while (path[0] == '/')
166 path++;
168 printf("A %s/%s\n", worktree_path, path);
171 static const struct got_error *
172 cmd_checkout(int argc, char *argv[])
174 const struct got_error *error = NULL;
175 struct got_repository *repo = NULL;
176 struct got_reference *head_ref = NULL;
177 struct got_worktree *worktree = NULL;
178 char *repo_path = NULL;
179 char *worktree_path = NULL;
180 const char *path_prefix = "";
181 int ch;
183 while ((ch = getopt(argc, argv, "p:")) != -1) {
184 switch (ch) {
185 case 'p':
186 path_prefix = optarg;
187 break;
188 default:
189 usage();
190 /* NOTREACHED */
194 argc -= optind;
195 argv += optind;
197 #ifndef PROFILE
198 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
199 == -1)
200 err(1, "pledge");
201 #endif
202 if (argc == 1) {
203 char *cwd, *base, *dotgit;
204 repo_path = realpath(argv[0], NULL);
205 if (repo_path == NULL)
206 return got_error_from_errno();
207 cwd = getcwd(NULL, 0);
208 if (cwd == NULL) {
209 error = got_error_from_errno();
210 goto done;
212 if (path_prefix[0])
213 base = basename(path_prefix);
214 else
215 base = basename(repo_path);
216 if (base == NULL) {
217 error = got_error_from_errno();
218 goto done;
220 dotgit = strstr(base, ".git");
221 if (dotgit)
222 *dotgit = '\0';
223 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
224 error = got_error_from_errno();
225 free(cwd);
226 goto done;
228 free(cwd);
229 } else if (argc == 2) {
230 repo_path = realpath(argv[0], NULL);
231 if (repo_path == NULL) {
232 error = got_error_from_errno();
233 goto done;
235 worktree_path = realpath(argv[1], NULL);
236 if (worktree_path == NULL) {
237 error = got_error_from_errno();
238 goto done;
240 } else
241 usage_checkout();
243 error = got_repo_open(&repo, repo_path);
244 if (error != NULL)
245 goto done;
246 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
247 if (error != NULL)
248 goto done;
250 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
251 if (error != NULL)
252 goto done;
254 error = got_worktree_open(&worktree, worktree_path);
255 if (error != NULL)
256 goto done;
258 error = got_worktree_checkout_files(worktree, head_ref, repo,
259 checkout_progress, worktree_path);
260 if (error != NULL)
261 goto done;
263 printf("Checked out %s\n", worktree_path);
264 printf("Now shut up and hack\n");
266 done:
267 free(repo_path);
268 free(worktree_path);
269 return error;
272 static const struct got_error *
273 print_patch(struct got_commit_object *commit, struct got_object_id *id,
274 int diff_context, struct got_repository *repo)
276 const struct got_error *err = NULL;
277 struct got_tree_object *tree1 = NULL, *tree2;
278 struct got_object_qid *qid;
280 err = got_object_open_as_tree(&tree2, repo, commit->tree_id);
281 if (err)
282 return err;
284 qid = SIMPLEQ_FIRST(&commit->parent_ids);
285 if (qid != NULL) {
286 struct got_commit_object *pcommit;
288 err = got_object_open_as_commit(&pcommit, repo, qid->id);
289 if (err)
290 return err;
292 err = got_object_open_as_tree(&tree1, repo, pcommit->tree_id);
293 got_object_commit_close(pcommit);
294 if (err)
295 return err;
298 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
299 if (tree1)
300 got_object_tree_close(tree1);
301 got_object_tree_close(tree2);
302 return err;
305 static char *
306 get_datestr(time_t *time, char *datebuf)
308 char *p, *s = ctime_r(time, datebuf);
309 p = strchr(s, '\n');
310 if (p)
311 *p = '\0';
312 return s;
315 static const struct got_error *
316 print_commit(struct got_commit_object *commit, struct got_object_id *id,
317 struct got_repository *repo, int show_patch, int diff_context)
319 const struct got_error *err = NULL;
320 char *id_str, *datestr, *logmsg0, *logmsg, *line;
321 char datebuf[26];
322 time_t author_time, committer_time;
324 err = got_object_id_str(&id_str, id);
325 if (err)
326 return err;
328 author_time = mktime(&commit->tm_author);
329 committer_time = mktime(&commit->tm_committer);
330 #if 0
331 /* This would express the date in committer's timezone. */
332 author_time += commit->tm_author.tm_gmtoff;
333 committer_time += commit->tm_committer.tm_gmtoff;
334 #endif
336 printf("-----------------------------------------------\n");
337 printf("commit %s\n", id_str);
338 free(id_str);
339 printf("from: %s\n", commit->author);
340 datestr = get_datestr(&committer_time, datebuf);
341 printf("date: %s UTC\n", datestr);
342 if (strcmp(commit->author, commit->committer) != 0)
343 printf("via: %s\n", commit->committer);
344 if (commit->nparents > 1) {
345 struct got_object_qid *qid;
346 int n = 1;
347 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
348 err = got_object_id_str(&id_str, qid->id);
349 if (err)
350 return err;
351 printf("parent %d: %s\n", n++, id_str);
352 free(id_str);
356 logmsg0 = strdup(commit->logmsg);
357 if (logmsg0 == NULL)
358 return got_error_from_errno();
360 logmsg = logmsg0;
361 do {
362 line = strsep(&logmsg, "\n");
363 if (line)
364 printf(" %s\n", line);
365 } while (line);
366 free(logmsg0);
368 if (show_patch) {
369 err = print_patch(commit, id, diff_context, repo);
370 if (err == 0)
371 printf("\n");
374 fflush(stdout);
375 return err;
378 static const struct got_error *
379 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
380 struct got_repository *repo, char *path, int show_patch, int diff_context,
381 int limit, int first_parent_traversal)
383 const struct got_error *err;
384 struct got_commit_graph *graph;
386 err = got_commit_graph_open(&graph, root_id, path,
387 first_parent_traversal, repo);
388 if (err)
389 return err;
390 err = got_commit_graph_iter_start(graph, root_id, repo);
391 if (err)
392 goto done;
393 while (1) {
394 struct got_commit_object *commit;
395 struct got_object_id *id;
397 err = got_commit_graph_iter_next(&id, graph);
398 if (err) {
399 if (err->code == GOT_ERR_ITER_COMPLETED) {
400 err = NULL;
401 break;
403 if (err->code != GOT_ERR_ITER_NEED_MORE)
404 break;
405 err = got_commit_graph_fetch_commits(graph, 1, repo);
406 if (err)
407 break;
408 else
409 continue;
411 if (id == NULL)
412 break;
414 err = got_object_open_as_commit(&commit, repo, id);
415 if (err)
416 break;
417 err = print_commit(commit, id, repo, show_patch, diff_context);
418 got_object_commit_close(commit);
419 if (err || (limit && --limit == 0))
420 break;
422 done:
423 got_commit_graph_close(graph);
424 return err;
427 __dead static void
428 usage_log(void)
430 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
431 "[-r repository-path] [path]\n", getprogname());
432 exit(1);
435 static const struct got_error *
436 cmd_log(int argc, char *argv[])
438 const struct got_error *error;
439 struct got_repository *repo = NULL;
440 struct got_object_id *id = NULL;
441 struct got_object *obj = NULL;
442 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
443 char *start_commit = NULL;
444 int diff_context = 3, ch;
445 int show_patch = 0, limit = 0, first_parent_traversal = 0;
446 const char *errstr;
448 #ifndef PROFILE
449 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
450 == -1)
451 err(1, "pledge");
452 #endif
454 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
455 switch (ch) {
456 case 'p':
457 show_patch = 1;
458 break;
459 case 'c':
460 start_commit = optarg;
461 break;
462 case 'C':
463 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
464 &errstr);
465 if (errstr != NULL)
466 err(1, "-C option %s", errstr);
467 break;
468 case 'l':
469 limit = strtonum(optarg, 1, INT_MAX, &errstr);
470 if (errstr != NULL)
471 err(1, "-l option %s", errstr);
472 break;
473 case 'f':
474 first_parent_traversal = 1;
475 break;
476 case 'r':
477 repo_path = realpath(optarg, NULL);
478 if (repo_path == NULL)
479 err(1, "-r option");
480 break;
481 default:
482 usage();
483 /* NOTREACHED */
487 argc -= optind;
488 argv += optind;
490 if (argc == 0)
491 path = strdup("");
492 else if (argc == 1)
493 path = strdup(argv[0]);
494 else
495 usage_log();
496 if (path == NULL)
497 return got_error_from_errno();
499 cwd = getcwd(NULL, 0);
500 if (cwd == NULL) {
501 error = got_error_from_errno();
502 goto done;
504 if (repo_path == NULL) {
505 repo_path = strdup(cwd);
506 if (repo_path == NULL) {
507 error = got_error_from_errno();
508 goto done;
512 error = got_repo_open(&repo, repo_path);
513 if (error != NULL)
514 goto done;
516 if (start_commit == NULL) {
517 struct got_reference *head_ref;
518 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
519 if (error != NULL)
520 return error;
521 error = got_ref_resolve(&id, repo, head_ref);
522 got_ref_close(head_ref);
523 if (error != NULL)
524 return error;
525 error = got_object_open(&obj, repo, id);
526 } else {
527 struct got_reference *ref;
528 error = got_ref_open(&ref, repo, start_commit);
529 if (error == NULL) {
530 error = got_ref_resolve(&id, repo, ref);
531 got_ref_close(ref);
532 if (error != NULL)
533 return error;
534 error = got_object_open(&obj, repo, id);
535 if (error != NULL)
536 return error;
538 if (obj == NULL) {
539 error = got_object_open_by_id_str(&obj, repo,
540 start_commit);
541 if (error != NULL)
542 return error;
543 id = got_object_id_dup(got_object_get_id(obj));
544 if (id == NULL)
545 error = got_error_from_errno();
548 if (error != NULL)
549 goto done;
550 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
551 error = got_error(GOT_ERR_OBJ_TYPE);
552 goto done;
555 error = got_repo_map_path(&in_repo_path, repo, path);
556 if (error != NULL)
557 goto done;
558 if (in_repo_path) {
559 free(path);
560 path = in_repo_path;
563 error = print_commits(obj, id, repo, path, show_patch,
564 diff_context, limit, first_parent_traversal);
565 done:
566 free(path);
567 free(repo_path);
568 free(cwd);
569 if (obj)
570 got_object_close(obj);
571 free(id);
572 if (repo) {
573 const struct got_error *repo_error;
574 repo_error = got_repo_close(repo);
575 if (error == NULL)
576 error = repo_error;
578 return error;
581 __dead static void
582 usage_diff(void)
584 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
585 "object1 object2\n", getprogname());
586 exit(1);
589 static const struct got_error *
590 cmd_diff(int argc, char *argv[])
592 const struct got_error *error;
593 struct got_repository *repo = NULL;
594 struct got_object *obj1 = NULL, *obj2 = NULL;
595 char *repo_path = NULL;
596 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
597 int diff_context = 3, ch;
598 const char *errstr;
600 #ifndef PROFILE
601 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
602 == -1)
603 err(1, "pledge");
604 #endif
606 while ((ch = getopt(argc, argv, "C:")) != -1) {
607 switch (ch) {
608 case 'C':
609 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
610 if (errstr != NULL)
611 err(1, "-C option %s", errstr);
612 break;
613 default:
614 usage();
615 /* NOTREACHED */
619 argc -= optind;
620 argv += optind;
622 if (argc == 0) {
623 usage_diff(); /* TODO show local worktree changes */
624 } else if (argc == 2) {
625 repo_path = getcwd(NULL, 0);
626 if (repo_path == NULL)
627 return got_error_from_errno();
628 obj_id_str1 = argv[0];
629 obj_id_str2 = argv[1];
630 } else if (argc == 3) {
631 repo_path = realpath(argv[0], NULL);
632 if (repo_path == NULL)
633 return got_error_from_errno();
634 obj_id_str1 = argv[1];
635 obj_id_str2 = argv[2];
636 } else
637 usage_diff();
639 error = got_repo_open(&repo, repo_path);
640 free(repo_path);
641 if (error != NULL)
642 goto done;
644 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
645 if (error)
646 goto done;
648 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
649 if (error)
650 goto done;
652 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
653 error = got_error(GOT_ERR_OBJ_TYPE);
654 goto done;
657 switch (got_object_get_type(obj1)) {
658 case GOT_OBJ_TYPE_BLOB:
659 error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
660 diff_context, repo, stdout);
661 break;
662 case GOT_OBJ_TYPE_TREE:
663 error = got_diff_objects_as_trees(obj1, obj2, "", "",
664 diff_context, repo, stdout);
665 break;
666 case GOT_OBJ_TYPE_COMMIT:
667 error = got_diff_objects_as_commits(obj1, obj2, diff_context,
668 repo, stdout);
669 break;
670 default:
671 error = got_error(GOT_ERR_OBJ_TYPE);
674 done:
675 if (obj1)
676 got_object_close(obj1);
677 if (obj2)
678 got_object_close(obj2);
679 if (repo) {
680 const struct got_error *repo_error;
681 repo_error = got_repo_close(repo);
682 if (error == NULL)
683 error = repo_error;
685 return error;
688 __dead static void
689 usage_blame(void)
691 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
692 getprogname());
693 exit(1);
696 static const struct got_error *
697 cmd_blame(int argc, char *argv[])
699 const struct got_error *error;
700 struct got_repository *repo = NULL;
701 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
702 struct got_object_id *commit_id = NULL;
703 char *commit_id_str = NULL;
704 int ch;
706 #ifndef PROFILE
707 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
708 == -1)
709 err(1, "pledge");
710 #endif
712 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
713 switch (ch) {
714 case 'c':
715 commit_id_str = optarg;
716 break;
717 case 'r':
718 repo_path = realpath(optarg, NULL);
719 if (repo_path == NULL)
720 err(1, "-r option");
721 break;
722 default:
723 usage();
724 /* NOTREACHED */
728 argc -= optind;
729 argv += optind;
731 if (argc == 1)
732 path = argv[0];
733 else
734 usage_blame();
736 cwd = getcwd(NULL, 0);
737 if (cwd == NULL) {
738 error = got_error_from_errno();
739 goto done;
741 if (repo_path == NULL) {
742 repo_path = strdup(cwd);
743 if (repo_path == NULL) {
744 error = got_error_from_errno();
745 goto done;
749 error = got_repo_open(&repo, repo_path);
750 if (error != NULL)
751 goto done;
753 error = got_repo_map_path(&in_repo_path, repo, path);
754 if (error != NULL)
755 goto done;
757 if (commit_id_str == NULL) {
758 struct got_reference *head_ref;
759 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
760 if (error != NULL)
761 goto done;
762 error = got_ref_resolve(&commit_id, repo, head_ref);
763 got_ref_close(head_ref);
764 if (error != NULL)
765 goto done;
766 } else {
767 struct got_object *obj;
768 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
769 if (error != NULL)
770 goto done;
771 commit_id = got_object_id_dup(got_object_get_id(obj));
772 if (commit_id == NULL)
773 error = got_error_from_errno();
774 got_object_close(obj);
777 error = got_blame(in_repo_path, commit_id, repo, stdout);
778 done:
779 free(in_repo_path);
780 free(repo_path);
781 free(cwd);
782 free(commit_id);
783 if (repo) {
784 const struct got_error *repo_error;
785 repo_error = got_repo_close(repo);
786 if (error == NULL)
787 error = repo_error;
789 return error;
792 __dead static void
793 usage_tree(void)
795 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
796 getprogname());
797 exit(1);
801 static const struct got_error *
802 print_tree(const char *path, struct got_object_id *commit_id,
803 int show_ids, struct got_repository *repo)
805 const struct got_error *err = NULL;
806 struct got_object_id *tree_id = NULL;
807 struct got_tree_object *tree = NULL;
808 const struct got_tree_entries *entries;
809 struct got_tree_entry *te;
811 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
812 if (err)
813 goto done;
815 err = got_object_open_as_tree(&tree, repo, tree_id);
816 if (err)
817 goto done;
818 entries = got_object_tree_get_entries(tree);
819 te = SIMPLEQ_FIRST(&entries->head);
820 while (te) {
821 char *id = NULL;
822 if (show_ids) {
823 char *id_str;
824 err = got_object_id_str(&id_str, te->id);
825 if (err)
826 goto done;
827 if (asprintf(&id, "%s ", id_str) == -1) {
828 err = got_error_from_errno();
829 free(id_str);
830 goto done;
832 free(id_str);
834 printf("%s%s%s\n", id ? id : "",
835 te->name, S_ISDIR(te->mode) ? "/" : "");
836 te = SIMPLEQ_NEXT(te, entry);
837 free(id);
839 done:
840 if (tree)
841 got_object_tree_close(tree);
842 free(tree_id);
843 return err;
846 static const struct got_error *
847 cmd_tree(int argc, char *argv[])
849 const struct got_error *error;
850 struct got_repository *repo = NULL;
851 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
852 struct got_object_id *commit_id = NULL;
853 char *commit_id_str = NULL;
854 int show_ids = 0;
855 int ch;
857 #ifndef PROFILE
858 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
859 == -1)
860 err(1, "pledge");
861 #endif
863 while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
864 switch (ch) {
865 case 'c':
866 commit_id_str = optarg;
867 break;
868 case 'r':
869 repo_path = realpath(optarg, NULL);
870 if (repo_path == NULL)
871 err(1, "-r option");
872 break;
873 case 'i':
874 show_ids = 1;
875 break;
876 default:
877 usage();
878 /* NOTREACHED */
882 argc -= optind;
883 argv += optind;
885 if (argc == 1)
886 path = argv[0];
887 else if (argc > 1)
888 usage_tree();
889 else
890 path = "/";
892 cwd = getcwd(NULL, 0);
893 if (cwd == NULL) {
894 error = got_error_from_errno();
895 goto done;
897 if (repo_path == NULL) {
898 repo_path = strdup(cwd);
899 if (repo_path == NULL) {
900 error = got_error_from_errno();
901 goto done;
905 error = got_repo_open(&repo, repo_path);
906 if (error != NULL)
907 goto done;
909 error = got_repo_map_path(&in_repo_path, repo, path);
910 if (error != NULL)
911 goto done;
913 if (commit_id_str == NULL) {
914 struct got_reference *head_ref;
915 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
916 if (error != NULL)
917 goto done;
918 error = got_ref_resolve(&commit_id, repo, head_ref);
919 got_ref_close(head_ref);
920 if (error != NULL)
921 goto done;
922 } else {
923 struct got_object *obj;
924 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
925 if (error != NULL)
926 goto done;
927 commit_id = got_object_id_dup(got_object_get_id(obj));
928 if (commit_id == NULL)
929 error = got_error_from_errno();
930 got_object_close(obj);
933 error = print_tree(in_repo_path, commit_id, show_ids, repo);
934 done:
935 free(in_repo_path);
936 free(repo_path);
937 free(cwd);
938 free(commit_id);
939 if (repo) {
940 const struct got_error *repo_error;
941 repo_error = got_repo_close(repo);
942 if (error == NULL)
943 error = repo_error;
945 return error;
948 #ifdef notyet
949 static const struct got_error *
950 cmd_status(int argc __unused, char *argv[] __unused)
952 git_repository *repo = NULL;
953 git_status_list *status;
954 git_status_options statusopts;
955 size_t i;
957 git_libgit2_init();
959 if (git_repository_open_ext(&repo, ".", 0, NULL))
960 errx(1, "git_repository_open: %s", giterr_last()->message);
962 if (git_repository_is_bare(repo))
963 errx(1, "bar repository");
965 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
966 errx(1, "git_status_init_options: %s", giterr_last()->message);
968 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
969 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
970 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
971 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
973 if (git_status_list_new(&status, repo, &statusopts))
974 errx(1, "git_status_list_new: %s", giterr_last()->message);
976 for (i = 0; i < git_status_list_entrycount(status); i++) {
977 const git_status_entry *se;
979 se = git_status_byindex(status, i);
980 switch (se->status) {
981 case GIT_STATUS_WT_NEW:
982 printf("? %s\n", se->index_to_workdir->new_file.path);
983 break;
984 case GIT_STATUS_WT_MODIFIED:
985 printf("M %s\n", se->index_to_workdir->new_file.path);
986 break;
987 case GIT_STATUS_WT_DELETED:
988 printf("R %s\n", se->index_to_workdir->new_file.path);
989 break;
990 case GIT_STATUS_WT_RENAMED:
991 printf("m %s -> %s\n",
992 se->index_to_workdir->old_file.path,
993 se->index_to_workdir->new_file.path);
994 break;
995 case GIT_STATUS_WT_TYPECHANGE:
996 printf("t %s\n", se->index_to_workdir->new_file.path);
997 break;
998 case GIT_STATUS_INDEX_NEW:
999 printf("A %s\n", se->head_to_index->new_file.path);
1000 break;
1001 case GIT_STATUS_INDEX_MODIFIED:
1002 printf("M %s\n", se->head_to_index->old_file.path);
1003 break;
1004 case GIT_STATUS_INDEX_DELETED:
1005 printf("R %s\n", se->head_to_index->old_file.path);
1006 break;
1007 case GIT_STATUS_INDEX_RENAMED:
1008 printf("m %s -> %s\n",
1009 se->head_to_index->old_file.path,
1010 se->head_to_index->new_file.path);
1011 break;
1012 case GIT_STATUS_INDEX_TYPECHANGE:
1013 printf("t %s\n", se->head_to_index->old_file.path);
1014 break;
1015 case GIT_STATUS_CURRENT:
1016 default:
1017 break;
1021 git_status_list_free(status);
1022 git_repository_free(repo);
1023 git_libgit2_shutdown();
1025 return 0;
1027 #endif