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("Now shut up and hack\n");
265 done:
266 free(repo_path);
267 free(worktree_path);
268 return error;
271 static const struct got_error *
272 print_patch(struct got_commit_object *commit, struct got_object_id *id,
273 int diff_context, struct got_repository *repo)
275 const struct got_error *err = NULL;
276 struct got_tree_object *tree1 = NULL, *tree2;
277 struct got_object_qid *qid;
279 err = got_object_open_as_tree(&tree2, repo, commit->tree_id);
280 if (err)
281 return err;
283 qid = SIMPLEQ_FIRST(&commit->parent_ids);
284 if (qid != NULL) {
285 struct got_commit_object *pcommit;
287 err = got_object_open_as_commit(&pcommit, repo, qid->id);
288 if (err)
289 return err;
291 err = got_object_open_as_tree(&tree1, repo, pcommit->tree_id);
292 got_object_commit_close(pcommit);
293 if (err)
294 return err;
297 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
298 if (tree1)
299 got_object_tree_close(tree1);
300 got_object_tree_close(tree2);
301 return err;
304 static char *
305 get_datestr(time_t *time, char *datebuf)
307 char *p, *s = ctime_r(time, datebuf);
308 p = strchr(s, '\n');
309 if (p)
310 *p = '\0';
311 return s;
314 static const struct got_error *
315 print_commit(struct got_commit_object *commit, struct got_object_id *id,
316 struct got_repository *repo, int show_patch, int diff_context)
318 const struct got_error *err = NULL;
319 char *id_str, *datestr, *logmsg0, *logmsg, *line;
320 char datebuf[26];
322 err = got_object_id_str(&id_str, id);
323 if (err)
324 return err;
326 printf("-----------------------------------------------\n");
327 printf("commit %s\n", id_str);
328 free(id_str);
329 printf("from: %s\n", commit->author);
330 datestr = get_datestr(&commit->committer_time, datebuf);
331 printf("date: %s UTC\n", datestr);
332 if (strcmp(commit->author, commit->committer) != 0)
333 printf("via: %s\n", commit->committer);
334 if (commit->nparents > 1) {
335 struct got_object_qid *qid;
336 int n = 1;
337 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
338 err = got_object_id_str(&id_str, qid->id);
339 if (err)
340 return err;
341 printf("parent %d: %s\n", n++, id_str);
342 free(id_str);
346 logmsg0 = strdup(commit->logmsg);
347 if (logmsg0 == NULL)
348 return got_error_from_errno();
350 logmsg = logmsg0;
351 do {
352 line = strsep(&logmsg, "\n");
353 if (line)
354 printf(" %s\n", line);
355 } while (line);
356 free(logmsg0);
358 if (show_patch) {
359 err = print_patch(commit, id, diff_context, repo);
360 if (err == 0)
361 printf("\n");
364 fflush(stdout);
365 return err;
368 static const struct got_error *
369 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
370 struct got_repository *repo, char *path, int show_patch, int diff_context,
371 int limit, int first_parent_traversal)
373 const struct got_error *err;
374 struct got_commit_graph *graph;
376 err = got_commit_graph_open(&graph, root_id, path,
377 first_parent_traversal, repo);
378 if (err)
379 return err;
380 err = got_commit_graph_iter_start(graph, root_id, repo);
381 if (err)
382 goto done;
383 while (1) {
384 struct got_commit_object *commit;
385 struct got_object_id *id;
387 err = got_commit_graph_iter_next(&id, graph);
388 if (err) {
389 if (err->code == GOT_ERR_ITER_COMPLETED) {
390 err = NULL;
391 break;
393 if (err->code != GOT_ERR_ITER_NEED_MORE)
394 break;
395 err = got_commit_graph_fetch_commits(graph, 1, repo);
396 if (err)
397 break;
398 else
399 continue;
401 if (id == NULL)
402 break;
404 err = got_object_open_as_commit(&commit, repo, id);
405 if (err)
406 break;
407 err = print_commit(commit, id, repo, show_patch, diff_context);
408 got_object_commit_close(commit);
409 if (err || (limit && --limit == 0))
410 break;
412 done:
413 got_commit_graph_close(graph);
414 return err;
417 __dead static void
418 usage_log(void)
420 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
421 "[-r repository-path] [path]\n", getprogname());
422 exit(1);
425 static const struct got_error *
426 cmd_log(int argc, char *argv[])
428 const struct got_error *error;
429 struct got_repository *repo = NULL;
430 struct got_object_id *id = NULL;
431 struct got_object *obj = NULL;
432 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
433 char *start_commit = NULL;
434 int diff_context = 3, ch;
435 int show_patch = 0, limit = 0, first_parent_traversal = 0;
436 const char *errstr;
438 #ifndef PROFILE
439 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
440 == -1)
441 err(1, "pledge");
442 #endif
444 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
445 switch (ch) {
446 case 'p':
447 show_patch = 1;
448 break;
449 case 'c':
450 start_commit = optarg;
451 break;
452 case 'C':
453 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
454 &errstr);
455 if (errstr != NULL)
456 err(1, "-C option %s", errstr);
457 break;
458 case 'l':
459 limit = strtonum(optarg, 1, INT_MAX, &errstr);
460 if (errstr != NULL)
461 err(1, "-l option %s", errstr);
462 break;
463 case 'f':
464 first_parent_traversal = 1;
465 break;
466 case 'r':
467 repo_path = realpath(optarg, NULL);
468 if (repo_path == NULL)
469 err(1, "-r option");
470 break;
471 default:
472 usage();
473 /* NOTREACHED */
477 argc -= optind;
478 argv += optind;
480 if (argc == 0)
481 path = strdup("");
482 else if (argc == 1)
483 path = strdup(argv[0]);
484 else
485 usage_log();
486 if (path == NULL)
487 return got_error_from_errno();
489 cwd = getcwd(NULL, 0);
490 if (cwd == NULL) {
491 error = got_error_from_errno();
492 goto done;
494 if (repo_path == NULL) {
495 repo_path = strdup(cwd);
496 if (repo_path == NULL) {
497 error = got_error_from_errno();
498 goto done;
502 error = got_repo_open(&repo, repo_path);
503 if (error != NULL)
504 goto done;
506 if (start_commit == NULL) {
507 struct got_reference *head_ref;
508 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
509 if (error != NULL)
510 return error;
511 error = got_ref_resolve(&id, repo, head_ref);
512 got_ref_close(head_ref);
513 if (error != NULL)
514 return error;
515 error = got_object_open(&obj, repo, id);
516 } else {
517 struct got_reference *ref;
518 error = got_ref_open(&ref, repo, start_commit);
519 if (error == NULL) {
520 error = got_ref_resolve(&id, repo, ref);
521 got_ref_close(ref);
522 if (error != NULL)
523 return error;
524 error = got_object_open(&obj, repo, id);
525 if (error != NULL)
526 return error;
528 if (obj == NULL) {
529 error = got_object_open_by_id_str(&obj, repo,
530 start_commit);
531 if (error != NULL)
532 return error;
533 id = got_object_id_dup(got_object_get_id(obj));
534 if (id == NULL)
535 error = got_error_from_errno();
538 if (error != NULL)
539 goto done;
540 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
541 error = got_error(GOT_ERR_OBJ_TYPE);
542 goto done;
545 error = got_repo_map_path(&in_repo_path, repo, path, 1);
546 if (error != NULL)
547 goto done;
548 if (in_repo_path) {
549 free(path);
550 path = in_repo_path;
553 error = print_commits(obj, id, repo, path, show_patch,
554 diff_context, limit, first_parent_traversal);
555 done:
556 free(path);
557 free(repo_path);
558 free(cwd);
559 if (obj)
560 got_object_close(obj);
561 free(id);
562 if (repo) {
563 const struct got_error *repo_error;
564 repo_error = got_repo_close(repo);
565 if (error == NULL)
566 error = repo_error;
568 return error;
571 __dead static void
572 usage_diff(void)
574 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
575 "object1 object2\n", getprogname());
576 exit(1);
579 static const struct got_error *
580 cmd_diff(int argc, char *argv[])
582 const struct got_error *error;
583 struct got_repository *repo = NULL;
584 struct got_object *obj1 = NULL, *obj2 = NULL;
585 char *repo_path = NULL;
586 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
587 int diff_context = 3, ch;
588 const char *errstr;
590 #ifndef PROFILE
591 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
592 == -1)
593 err(1, "pledge");
594 #endif
596 while ((ch = getopt(argc, argv, "C:")) != -1) {
597 switch (ch) {
598 case 'C':
599 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
600 if (errstr != NULL)
601 err(1, "-C option %s", errstr);
602 break;
603 default:
604 usage();
605 /* NOTREACHED */
609 argc -= optind;
610 argv += optind;
612 if (argc == 0) {
613 usage_diff(); /* TODO show local worktree changes */
614 } else if (argc == 2) {
615 repo_path = getcwd(NULL, 0);
616 if (repo_path == NULL)
617 return got_error_from_errno();
618 obj_id_str1 = argv[0];
619 obj_id_str2 = argv[1];
620 } else if (argc == 3) {
621 repo_path = realpath(argv[0], NULL);
622 if (repo_path == NULL)
623 return got_error_from_errno();
624 obj_id_str1 = argv[1];
625 obj_id_str2 = argv[2];
626 } else
627 usage_diff();
629 error = got_repo_open(&repo, repo_path);
630 free(repo_path);
631 if (error != NULL)
632 goto done;
634 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
635 if (error)
636 goto done;
638 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
639 if (error)
640 goto done;
642 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
643 error = got_error(GOT_ERR_OBJ_TYPE);
644 goto done;
647 switch (got_object_get_type(obj1)) {
648 case GOT_OBJ_TYPE_BLOB:
649 error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
650 diff_context, repo, stdout);
651 break;
652 case GOT_OBJ_TYPE_TREE:
653 error = got_diff_objects_as_trees(obj1, obj2, "", "",
654 diff_context, repo, stdout);
655 break;
656 case GOT_OBJ_TYPE_COMMIT:
657 error = got_diff_objects_as_commits(obj1, obj2, diff_context,
658 repo, stdout);
659 break;
660 default:
661 error = got_error(GOT_ERR_OBJ_TYPE);
664 done:
665 if (obj1)
666 got_object_close(obj1);
667 if (obj2)
668 got_object_close(obj2);
669 if (repo) {
670 const struct got_error *repo_error;
671 repo_error = got_repo_close(repo);
672 if (error == NULL)
673 error = repo_error;
675 return error;
678 __dead static void
679 usage_blame(void)
681 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
682 getprogname());
683 exit(1);
686 static const struct got_error *
687 cmd_blame(int argc, char *argv[])
689 const struct got_error *error;
690 struct got_repository *repo = NULL;
691 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
692 struct got_object_id *commit_id = NULL;
693 char *commit_id_str = NULL;
694 int ch;
696 #ifndef PROFILE
697 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
698 == -1)
699 err(1, "pledge");
700 #endif
702 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
703 switch (ch) {
704 case 'c':
705 commit_id_str = optarg;
706 break;
707 case 'r':
708 repo_path = realpath(optarg, NULL);
709 if (repo_path == NULL)
710 err(1, "-r option");
711 break;
712 default:
713 usage();
714 /* NOTREACHED */
718 argc -= optind;
719 argv += optind;
721 if (argc == 1)
722 path = argv[0];
723 else
724 usage_blame();
726 cwd = getcwd(NULL, 0);
727 if (cwd == NULL) {
728 error = got_error_from_errno();
729 goto done;
731 if (repo_path == NULL) {
732 repo_path = strdup(cwd);
733 if (repo_path == NULL) {
734 error = got_error_from_errno();
735 goto done;
739 error = got_repo_open(&repo, repo_path);
740 if (error != NULL)
741 goto done;
743 error = got_repo_map_path(&in_repo_path, repo, path, 1);
744 if (error != NULL)
745 goto done;
747 if (commit_id_str == NULL) {
748 struct got_reference *head_ref;
749 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
750 if (error != NULL)
751 goto done;
752 error = got_ref_resolve(&commit_id, repo, head_ref);
753 got_ref_close(head_ref);
754 if (error != NULL)
755 goto done;
756 } else {
757 struct got_object *obj;
758 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
759 if (error != NULL)
760 goto done;
761 commit_id = got_object_id_dup(got_object_get_id(obj));
762 if (commit_id == NULL)
763 error = got_error_from_errno();
764 got_object_close(obj);
767 error = got_blame(in_repo_path, commit_id, repo, stdout);
768 done:
769 free(in_repo_path);
770 free(repo_path);
771 free(cwd);
772 free(commit_id);
773 if (repo) {
774 const struct got_error *repo_error;
775 repo_error = got_repo_close(repo);
776 if (error == NULL)
777 error = repo_error;
779 return error;
782 __dead static void
783 usage_tree(void)
785 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
786 getprogname());
787 exit(1);
791 static const struct got_error *
792 print_tree(const char *path, struct got_object_id *commit_id,
793 int show_ids, struct got_repository *repo)
795 const struct got_error *err = NULL;
796 struct got_object_id *tree_id = NULL;
797 struct got_tree_object *tree = NULL;
798 const struct got_tree_entries *entries;
799 struct got_tree_entry *te;
801 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
802 if (err)
803 goto done;
805 err = got_object_open_as_tree(&tree, repo, tree_id);
806 if (err)
807 goto done;
808 entries = got_object_tree_get_entries(tree);
809 te = SIMPLEQ_FIRST(&entries->head);
810 while (te) {
811 char *id = NULL;
812 if (show_ids) {
813 char *id_str;
814 err = got_object_id_str(&id_str, te->id);
815 if (err)
816 goto done;
817 if (asprintf(&id, "%s ", id_str) == -1) {
818 err = got_error_from_errno();
819 free(id_str);
820 goto done;
822 free(id_str);
824 printf("%s%s%s\n", id ? id : "",
825 te->name, S_ISDIR(te->mode) ? "/" : "");
826 te = SIMPLEQ_NEXT(te, entry);
827 free(id);
829 done:
830 if (tree)
831 got_object_tree_close(tree);
832 free(tree_id);
833 return err;
836 static const struct got_error *
837 cmd_tree(int argc, char *argv[])
839 const struct got_error *error;
840 struct got_repository *repo = NULL;
841 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
842 struct got_object_id *commit_id = NULL;
843 char *commit_id_str = NULL;
844 int show_ids = 0;
845 int ch;
847 #ifndef PROFILE
848 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
849 == -1)
850 err(1, "pledge");
851 #endif
853 while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
854 switch (ch) {
855 case 'c':
856 commit_id_str = optarg;
857 break;
858 case 'r':
859 repo_path = realpath(optarg, NULL);
860 if (repo_path == NULL)
861 err(1, "-r option");
862 break;
863 case 'i':
864 show_ids = 1;
865 break;
866 default:
867 usage();
868 /* NOTREACHED */
872 argc -= optind;
873 argv += optind;
875 if (argc == 1)
876 path = argv[0];
877 else if (argc > 1)
878 usage_tree();
879 else
880 path = "/";
882 cwd = getcwd(NULL, 0);
883 if (cwd == NULL) {
884 error = got_error_from_errno();
885 goto done;
887 if (repo_path == NULL) {
888 repo_path = strdup(cwd);
889 if (repo_path == NULL) {
890 error = got_error_from_errno();
891 goto done;
895 error = got_repo_open(&repo, repo_path);
896 if (error != NULL)
897 goto done;
899 error = got_repo_map_path(&in_repo_path, repo, path, 1);
900 if (error != NULL)
901 goto done;
903 if (commit_id_str == NULL) {
904 struct got_reference *head_ref;
905 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
906 if (error != NULL)
907 goto done;
908 error = got_ref_resolve(&commit_id, repo, head_ref);
909 got_ref_close(head_ref);
910 if (error != NULL)
911 goto done;
912 } else {
913 struct got_object *obj;
914 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
915 if (error != NULL)
916 goto done;
917 commit_id = got_object_id_dup(got_object_get_id(obj));
918 if (commit_id == NULL)
919 error = got_error_from_errno();
920 got_object_close(obj);
923 error = print_tree(in_repo_path, commit_id, show_ids, repo);
924 done:
925 free(in_repo_path);
926 free(repo_path);
927 free(cwd);
928 free(commit_id);
929 if (repo) {
930 const struct got_error *repo_error;
931 repo_error = got_repo_close(repo);
932 if (error == NULL)
933 error = repo_error;
935 return error;
938 #ifdef notyet
939 static const struct got_error *
940 cmd_status(int argc __unused, char *argv[] __unused)
942 git_repository *repo = NULL;
943 git_status_list *status;
944 git_status_options statusopts;
945 size_t i;
947 git_libgit2_init();
949 if (git_repository_open_ext(&repo, ".", 0, NULL))
950 errx(1, "git_repository_open: %s", giterr_last()->message);
952 if (git_repository_is_bare(repo))
953 errx(1, "bar repository");
955 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
956 errx(1, "git_status_init_options: %s", giterr_last()->message);
958 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
959 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
960 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
961 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
963 if (git_status_list_new(&status, repo, &statusopts))
964 errx(1, "git_status_list_new: %s", giterr_last()->message);
966 for (i = 0; i < git_status_list_entrycount(status); i++) {
967 const git_status_entry *se;
969 se = git_status_byindex(status, i);
970 switch (se->status) {
971 case GIT_STATUS_WT_NEW:
972 printf("? %s\n", se->index_to_workdir->new_file.path);
973 break;
974 case GIT_STATUS_WT_MODIFIED:
975 printf("M %s\n", se->index_to_workdir->new_file.path);
976 break;
977 case GIT_STATUS_WT_DELETED:
978 printf("R %s\n", se->index_to_workdir->new_file.path);
979 break;
980 case GIT_STATUS_WT_RENAMED:
981 printf("m %s -> %s\n",
982 se->index_to_workdir->old_file.path,
983 se->index_to_workdir->new_file.path);
984 break;
985 case GIT_STATUS_WT_TYPECHANGE:
986 printf("t %s\n", se->index_to_workdir->new_file.path);
987 break;
988 case GIT_STATUS_INDEX_NEW:
989 printf("A %s\n", se->head_to_index->new_file.path);
990 break;
991 case GIT_STATUS_INDEX_MODIFIED:
992 printf("M %s\n", se->head_to_index->old_file.path);
993 break;
994 case GIT_STATUS_INDEX_DELETED:
995 printf("R %s\n", se->head_to_index->old_file.path);
996 break;
997 case GIT_STATUS_INDEX_RENAMED:
998 printf("m %s -> %s\n",
999 se->head_to_index->old_file.path,
1000 se->head_to_index->new_file.path);
1001 break;
1002 case GIT_STATUS_INDEX_TYPECHANGE:
1003 printf("t %s\n", se->head_to_index->old_file.path);
1004 break;
1005 case GIT_STATUS_CURRENT:
1006 default:
1007 break;
1011 git_status_list_free(status);
1012 git_repository_free(repo);
1013 git_libgit2_shutdown();
1015 return 0;
1017 #endif