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];
321 time_t author_time, committer_time;
323 err = got_object_id_str(&id_str, id);
324 if (err)
325 return err;
327 author_time = mktime(&commit->tm_author);
328 committer_time = mktime(&commit->tm_committer);
329 #if 0
330 /* This would express the date in committer's timezone. */
331 author_time += commit->tm_author.tm_gmtoff;
332 committer_time += commit->tm_committer.tm_gmtoff;
333 #endif
335 printf("-----------------------------------------------\n");
336 printf("commit %s\n", id_str);
337 free(id_str);
338 printf("from: %s\n", commit->author);
339 datestr = get_datestr(&committer_time, datebuf);
340 printf("date: %s UTC\n", datestr);
341 if (strcmp(commit->author, commit->committer) != 0)
342 printf("via: %s\n", commit->committer);
343 if (commit->nparents > 1) {
344 struct got_object_qid *qid;
345 int n = 1;
346 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
347 err = got_object_id_str(&id_str, qid->id);
348 if (err)
349 return err;
350 printf("parent %d: %s\n", n++, id_str);
351 free(id_str);
355 logmsg0 = strdup(commit->logmsg);
356 if (logmsg0 == NULL)
357 return got_error_from_errno();
359 logmsg = logmsg0;
360 do {
361 line = strsep(&logmsg, "\n");
362 if (line)
363 printf(" %s\n", line);
364 } while (line);
365 free(logmsg0);
367 if (show_patch) {
368 err = print_patch(commit, id, diff_context, repo);
369 if (err == 0)
370 printf("\n");
373 fflush(stdout);
374 return err;
377 static const struct got_error *
378 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
379 struct got_repository *repo, char *path, int show_patch, int diff_context,
380 int limit, int first_parent_traversal)
382 const struct got_error *err;
383 struct got_commit_graph *graph;
385 err = got_commit_graph_open(&graph, root_id, path,
386 first_parent_traversal, repo);
387 if (err)
388 return err;
389 err = got_commit_graph_iter_start(graph, root_id, repo);
390 if (err)
391 goto done;
392 while (1) {
393 struct got_commit_object *commit;
394 struct got_object_id *id;
396 err = got_commit_graph_iter_next(&id, graph);
397 if (err) {
398 if (err->code == GOT_ERR_ITER_COMPLETED) {
399 err = NULL;
400 break;
402 if (err->code != GOT_ERR_ITER_NEED_MORE)
403 break;
404 err = got_commit_graph_fetch_commits(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 err = print_commit(commit, id, repo, show_patch, diff_context);
417 got_object_commit_close(commit);
418 if (err || (limit && --limit == 0))
419 break;
421 done:
422 got_commit_graph_close(graph);
423 return err;
426 __dead static void
427 usage_log(void)
429 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
430 "[-r repository-path] [path]\n", getprogname());
431 exit(1);
434 static const struct got_error *
435 cmd_log(int argc, char *argv[])
437 const struct got_error *error;
438 struct got_repository *repo = NULL;
439 struct got_object_id *id = NULL;
440 struct got_object *obj = NULL;
441 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
442 char *start_commit = NULL;
443 int diff_context = 3, ch;
444 int show_patch = 0, limit = 0, first_parent_traversal = 0;
445 const char *errstr;
447 #ifndef PROFILE
448 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
449 == -1)
450 err(1, "pledge");
451 #endif
453 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
454 switch (ch) {
455 case 'p':
456 show_patch = 1;
457 break;
458 case 'c':
459 start_commit = optarg;
460 break;
461 case 'C':
462 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
463 &errstr);
464 if (errstr != NULL)
465 err(1, "-C option %s", errstr);
466 break;
467 case 'l':
468 limit = strtonum(optarg, 1, INT_MAX, &errstr);
469 if (errstr != NULL)
470 err(1, "-l option %s", errstr);
471 break;
472 case 'f':
473 first_parent_traversal = 1;
474 break;
475 case 'r':
476 repo_path = realpath(optarg, NULL);
477 if (repo_path == NULL)
478 err(1, "-r option");
479 break;
480 default:
481 usage();
482 /* NOTREACHED */
486 argc -= optind;
487 argv += optind;
489 if (argc == 0)
490 path = strdup("");
491 else if (argc == 1)
492 path = strdup(argv[0]);
493 else
494 usage_log();
495 if (path == NULL)
496 return got_error_from_errno();
498 cwd = getcwd(NULL, 0);
499 if (cwd == NULL) {
500 error = got_error_from_errno();
501 goto done;
503 if (repo_path == NULL) {
504 repo_path = strdup(cwd);
505 if (repo_path == NULL) {
506 error = got_error_from_errno();
507 goto done;
511 error = got_repo_open(&repo, repo_path);
512 if (error != NULL)
513 goto done;
515 if (start_commit == NULL) {
516 struct got_reference *head_ref;
517 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
518 if (error != NULL)
519 return error;
520 error = got_ref_resolve(&id, repo, head_ref);
521 got_ref_close(head_ref);
522 if (error != NULL)
523 return error;
524 error = got_object_open(&obj, repo, id);
525 } else {
526 struct got_reference *ref;
527 error = got_ref_open(&ref, repo, start_commit);
528 if (error == NULL) {
529 error = got_ref_resolve(&id, repo, ref);
530 got_ref_close(ref);
531 if (error != NULL)
532 return error;
533 error = got_object_open(&obj, repo, id);
534 if (error != NULL)
535 return error;
537 if (obj == NULL) {
538 error = got_object_open_by_id_str(&obj, repo,
539 start_commit);
540 if (error != NULL)
541 return error;
542 id = got_object_id_dup(got_object_get_id(obj));
543 if (id == NULL)
544 error = got_error_from_errno();
547 if (error != NULL)
548 goto done;
549 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
550 error = got_error(GOT_ERR_OBJ_TYPE);
551 goto done;
554 error = got_repo_map_path(&in_repo_path, repo, path, 1);
555 if (error != NULL)
556 goto done;
557 if (in_repo_path) {
558 free(path);
559 path = in_repo_path;
562 error = print_commits(obj, id, repo, path, show_patch,
563 diff_context, limit, first_parent_traversal);
564 done:
565 free(path);
566 free(repo_path);
567 free(cwd);
568 if (obj)
569 got_object_close(obj);
570 free(id);
571 if (repo) {
572 const struct got_error *repo_error;
573 repo_error = got_repo_close(repo);
574 if (error == NULL)
575 error = repo_error;
577 return error;
580 __dead static void
581 usage_diff(void)
583 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
584 "object1 object2\n", getprogname());
585 exit(1);
588 static const struct got_error *
589 cmd_diff(int argc, char *argv[])
591 const struct got_error *error;
592 struct got_repository *repo = NULL;
593 struct got_object *obj1 = NULL, *obj2 = NULL;
594 char *repo_path = NULL;
595 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
596 int diff_context = 3, ch;
597 const char *errstr;
599 #ifndef PROFILE
600 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
601 == -1)
602 err(1, "pledge");
603 #endif
605 while ((ch = getopt(argc, argv, "C:")) != -1) {
606 switch (ch) {
607 case 'C':
608 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
609 if (errstr != NULL)
610 err(1, "-C option %s", errstr);
611 break;
612 default:
613 usage();
614 /* NOTREACHED */
618 argc -= optind;
619 argv += optind;
621 if (argc == 0) {
622 usage_diff(); /* TODO show local worktree changes */
623 } else if (argc == 2) {
624 repo_path = getcwd(NULL, 0);
625 if (repo_path == NULL)
626 return got_error_from_errno();
627 obj_id_str1 = argv[0];
628 obj_id_str2 = argv[1];
629 } else if (argc == 3) {
630 repo_path = realpath(argv[0], NULL);
631 if (repo_path == NULL)
632 return got_error_from_errno();
633 obj_id_str1 = argv[1];
634 obj_id_str2 = argv[2];
635 } else
636 usage_diff();
638 error = got_repo_open(&repo, repo_path);
639 free(repo_path);
640 if (error != NULL)
641 goto done;
643 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
644 if (error)
645 goto done;
647 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
648 if (error)
649 goto done;
651 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
652 error = got_error(GOT_ERR_OBJ_TYPE);
653 goto done;
656 switch (got_object_get_type(obj1)) {
657 case GOT_OBJ_TYPE_BLOB:
658 error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
659 diff_context, repo, stdout);
660 break;
661 case GOT_OBJ_TYPE_TREE:
662 error = got_diff_objects_as_trees(obj1, obj2, "", "",
663 diff_context, repo, stdout);
664 break;
665 case GOT_OBJ_TYPE_COMMIT:
666 error = got_diff_objects_as_commits(obj1, obj2, diff_context,
667 repo, stdout);
668 break;
669 default:
670 error = got_error(GOT_ERR_OBJ_TYPE);
673 done:
674 if (obj1)
675 got_object_close(obj1);
676 if (obj2)
677 got_object_close(obj2);
678 if (repo) {
679 const struct got_error *repo_error;
680 repo_error = got_repo_close(repo);
681 if (error == NULL)
682 error = repo_error;
684 return error;
687 __dead static void
688 usage_blame(void)
690 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
691 getprogname());
692 exit(1);
695 static const struct got_error *
696 cmd_blame(int argc, char *argv[])
698 const struct got_error *error;
699 struct got_repository *repo = NULL;
700 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
701 struct got_object_id *commit_id = NULL;
702 char *commit_id_str = NULL;
703 int ch;
705 #ifndef PROFILE
706 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
707 == -1)
708 err(1, "pledge");
709 #endif
711 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
712 switch (ch) {
713 case 'c':
714 commit_id_str = optarg;
715 break;
716 case 'r':
717 repo_path = realpath(optarg, NULL);
718 if (repo_path == NULL)
719 err(1, "-r option");
720 break;
721 default:
722 usage();
723 /* NOTREACHED */
727 argc -= optind;
728 argv += optind;
730 if (argc == 1)
731 path = argv[0];
732 else
733 usage_blame();
735 cwd = getcwd(NULL, 0);
736 if (cwd == NULL) {
737 error = got_error_from_errno();
738 goto done;
740 if (repo_path == NULL) {
741 repo_path = strdup(cwd);
742 if (repo_path == NULL) {
743 error = got_error_from_errno();
744 goto done;
748 error = got_repo_open(&repo, repo_path);
749 if (error != NULL)
750 goto done;
752 error = got_repo_map_path(&in_repo_path, repo, path, 1);
753 if (error != NULL)
754 goto done;
756 if (commit_id_str == NULL) {
757 struct got_reference *head_ref;
758 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
759 if (error != NULL)
760 goto done;
761 error = got_ref_resolve(&commit_id, repo, head_ref);
762 got_ref_close(head_ref);
763 if (error != NULL)
764 goto done;
765 } else {
766 struct got_object *obj;
767 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
768 if (error != NULL)
769 goto done;
770 commit_id = got_object_id_dup(got_object_get_id(obj));
771 if (commit_id == NULL)
772 error = got_error_from_errno();
773 got_object_close(obj);
776 error = got_blame(in_repo_path, commit_id, repo, stdout);
777 done:
778 free(in_repo_path);
779 free(repo_path);
780 free(cwd);
781 free(commit_id);
782 if (repo) {
783 const struct got_error *repo_error;
784 repo_error = got_repo_close(repo);
785 if (error == NULL)
786 error = repo_error;
788 return error;
791 __dead static void
792 usage_tree(void)
794 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
795 getprogname());
796 exit(1);
800 static const struct got_error *
801 print_tree(const char *path, struct got_object_id *commit_id,
802 int show_ids, struct got_repository *repo)
804 const struct got_error *err = NULL;
805 struct got_object_id *tree_id = NULL;
806 struct got_tree_object *tree = NULL;
807 const struct got_tree_entries *entries;
808 struct got_tree_entry *te;
810 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
811 if (err)
812 goto done;
814 err = got_object_open_as_tree(&tree, repo, tree_id);
815 if (err)
816 goto done;
817 entries = got_object_tree_get_entries(tree);
818 te = SIMPLEQ_FIRST(&entries->head);
819 while (te) {
820 char *id = NULL;
821 if (show_ids) {
822 char *id_str;
823 err = got_object_id_str(&id_str, te->id);
824 if (err)
825 goto done;
826 if (asprintf(&id, "%s ", id_str) == -1) {
827 err = got_error_from_errno();
828 free(id_str);
829 goto done;
831 free(id_str);
833 printf("%s%s%s\n", id ? id : "",
834 te->name, S_ISDIR(te->mode) ? "/" : "");
835 te = SIMPLEQ_NEXT(te, entry);
836 free(id);
838 done:
839 if (tree)
840 got_object_tree_close(tree);
841 free(tree_id);
842 return err;
845 static const struct got_error *
846 cmd_tree(int argc, char *argv[])
848 const struct got_error *error;
849 struct got_repository *repo = NULL;
850 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
851 struct got_object_id *commit_id = NULL;
852 char *commit_id_str = NULL;
853 int show_ids = 0;
854 int ch;
856 #ifndef PROFILE
857 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
858 == -1)
859 err(1, "pledge");
860 #endif
862 while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
863 switch (ch) {
864 case 'c':
865 commit_id_str = optarg;
866 break;
867 case 'r':
868 repo_path = realpath(optarg, NULL);
869 if (repo_path == NULL)
870 err(1, "-r option");
871 break;
872 case 'i':
873 show_ids = 1;
874 break;
875 default:
876 usage();
877 /* NOTREACHED */
881 argc -= optind;
882 argv += optind;
884 if (argc == 1)
885 path = argv[0];
886 else if (argc > 1)
887 usage_tree();
888 else
889 path = "/";
891 cwd = getcwd(NULL, 0);
892 if (cwd == NULL) {
893 error = got_error_from_errno();
894 goto done;
896 if (repo_path == NULL) {
897 repo_path = strdup(cwd);
898 if (repo_path == NULL) {
899 error = got_error_from_errno();
900 goto done;
904 error = got_repo_open(&repo, repo_path);
905 if (error != NULL)
906 goto done;
908 error = got_repo_map_path(&in_repo_path, repo, path, 1);
909 if (error != NULL)
910 goto done;
912 if (commit_id_str == NULL) {
913 struct got_reference *head_ref;
914 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
915 if (error != NULL)
916 goto done;
917 error = got_ref_resolve(&commit_id, repo, head_ref);
918 got_ref_close(head_ref);
919 if (error != NULL)
920 goto done;
921 } else {
922 struct got_object *obj;
923 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
924 if (error != NULL)
925 goto done;
926 commit_id = got_object_id_dup(got_object_get_id(obj));
927 if (commit_id == NULL)
928 error = got_error_from_errno();
929 got_object_close(obj);
932 error = print_tree(in_repo_path, commit_id, show_ids, repo);
933 done:
934 free(in_repo_path);
935 free(repo_path);
936 free(cwd);
937 free(commit_id);
938 if (repo) {
939 const struct got_error *repo_error;
940 repo_error = got_repo_close(repo);
941 if (error == NULL)
942 error = repo_error;
944 return error;
947 #ifdef notyet
948 static const struct got_error *
949 cmd_status(int argc __unused, char *argv[] __unused)
951 git_repository *repo = NULL;
952 git_status_list *status;
953 git_status_options statusopts;
954 size_t i;
956 git_libgit2_init();
958 if (git_repository_open_ext(&repo, ".", 0, NULL))
959 errx(1, "git_repository_open: %s", giterr_last()->message);
961 if (git_repository_is_bare(repo))
962 errx(1, "bar repository");
964 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
965 errx(1, "git_status_init_options: %s", giterr_last()->message);
967 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
968 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
969 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
970 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
972 if (git_status_list_new(&status, repo, &statusopts))
973 errx(1, "git_status_list_new: %s", giterr_last()->message);
975 for (i = 0; i < git_status_list_entrycount(status); i++) {
976 const git_status_entry *se;
978 se = git_status_byindex(status, i);
979 switch (se->status) {
980 case GIT_STATUS_WT_NEW:
981 printf("? %s\n", se->index_to_workdir->new_file.path);
982 break;
983 case GIT_STATUS_WT_MODIFIED:
984 printf("M %s\n", se->index_to_workdir->new_file.path);
985 break;
986 case GIT_STATUS_WT_DELETED:
987 printf("R %s\n", se->index_to_workdir->new_file.path);
988 break;
989 case GIT_STATUS_WT_RENAMED:
990 printf("m %s -> %s\n",
991 se->index_to_workdir->old_file.path,
992 se->index_to_workdir->new_file.path);
993 break;
994 case GIT_STATUS_WT_TYPECHANGE:
995 printf("t %s\n", se->index_to_workdir->new_file.path);
996 break;
997 case GIT_STATUS_INDEX_NEW:
998 printf("A %s\n", se->head_to_index->new_file.path);
999 break;
1000 case GIT_STATUS_INDEX_MODIFIED:
1001 printf("M %s\n", se->head_to_index->old_file.path);
1002 break;
1003 case GIT_STATUS_INDEX_DELETED:
1004 printf("R %s\n", se->head_to_index->old_file.path);
1005 break;
1006 case GIT_STATUS_INDEX_RENAMED:
1007 printf("m %s -> %s\n",
1008 se->head_to_index->old_file.path,
1009 se->head_to_index->new_file.path);
1010 break;
1011 case GIT_STATUS_INDEX_TYPECHANGE:
1012 printf("t %s\n", se->head_to_index->old_file.path);
1013 break;
1014 case GIT_STATUS_CURRENT:
1015 default:
1016 break;
1020 git_status_list_free(status);
1021 git_repository_free(repo);
1022 git_libgit2_shutdown();
1024 return 0;
1026 #endif