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 <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <libgen.h>
32 #include <time.h>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_reference.h"
37 #include "got_repository.h"
38 #include "got_worktree.h"
39 #include "got_diff.h"
40 #include "got_commit_graph.h"
41 #include "got_blame.h"
43 #ifndef nitems
44 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
45 #endif
47 static volatile sig_atomic_t sigint_received;
48 static volatile sig_atomic_t sigpipe_received;
50 static void
51 catch_sigint(int signo)
52 {
53 sigint_received = 1;
54 }
56 static void
57 catch_sigpipe(int signo)
58 {
59 sigpipe_received = 1;
60 }
63 struct cmd {
64 const char *cmd_name;
65 const struct got_error *(*cmd_main)(int, char *[]);
66 void (*cmd_usage)(void);
67 const char *cmd_descr;
68 };
70 __dead static void usage(void);
71 __dead static void usage_checkout(void);
72 __dead static void usage_log(void);
73 __dead static void usage_diff(void);
74 __dead static void usage_blame(void);
75 __dead static void usage_tree(void);
77 static const struct got_error* cmd_checkout(int, char *[]);
78 static const struct got_error* cmd_log(int, char *[]);
79 static const struct got_error* cmd_diff(int, char *[]);
80 static const struct got_error* cmd_blame(int, char *[]);
81 static const struct got_error* cmd_tree(int, char *[]);
82 #ifdef notyet
83 static const struct got_error* cmd_status(int, char *[]);
84 #endif
86 static struct cmd got_commands[] = {
87 { "checkout", cmd_checkout, usage_checkout,
88 "check out a new work tree from a repository" },
89 { "log", cmd_log, usage_log,
90 "show repository history" },
91 { "diff", cmd_diff, usage_diff,
92 "compare files and directories" },
93 { "blame", cmd_blame, usage_blame,
94 " show when lines in a file were changed" },
95 { "tree", cmd_tree, usage_tree,
96 " list files and directories in repository" },
97 #ifdef notyet
98 { "status", cmd_status, usage_status,
99 "show modification status of files" },
100 #endif
101 };
103 int
104 main(int argc, char *argv[])
106 struct cmd *cmd;
107 unsigned int i;
108 int ch;
109 int hflag = 0;
111 setlocale(LC_ALL, "");
113 while ((ch = getopt(argc, argv, "h")) != -1) {
114 switch (ch) {
115 case 'h':
116 hflag = 1;
117 break;
118 default:
119 usage();
120 /* NOTREACHED */
124 argc -= optind;
125 argv += optind;
126 optind = 0;
128 if (argc <= 0)
129 usage();
131 signal(SIGINT, catch_sigint);
132 signal(SIGPIPE, catch_sigpipe);
134 for (i = 0; i < nitems(got_commands); i++) {
135 const struct got_error *error;
137 cmd = &got_commands[i];
139 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
140 continue;
142 if (hflag)
143 got_commands[i].cmd_usage();
145 error = got_commands[i].cmd_main(argc, argv);
146 if (error && !(sigint_received || sigpipe_received)) {
147 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
148 return 1;
151 return 0;
154 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
155 return 1;
158 __dead static void
159 usage(void)
161 int i;
163 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
164 "Available commands:\n", getprogname());
165 for (i = 0; i < nitems(got_commands); i++) {
166 struct cmd *cmd = &got_commands[i];
167 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
169 exit(1);
172 __dead static void
173 usage_checkout(void)
175 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
176 "[worktree-path]\n", getprogname());
177 exit(1);
180 static void
181 checkout_progress(void *arg, const char *path)
183 char *worktree_path = arg;
185 while (path[0] == '/')
186 path++;
188 printf("A %s/%s\n", worktree_path, path);
191 static const struct got_error *
192 checkout_cancel(void *arg)
194 if (sigint_received || sigpipe_received)
195 return got_error(GOT_ERR_CANCELLED);
196 return NULL;
199 static const struct got_error *
200 cmd_checkout(int argc, char *argv[])
202 const struct got_error *error = NULL;
203 struct got_repository *repo = NULL;
204 struct got_reference *head_ref = NULL;
205 struct got_worktree *worktree = NULL;
206 char *repo_path = NULL;
207 char *worktree_path = NULL;
208 const char *path_prefix = "";
209 int ch;
211 while ((ch = getopt(argc, argv, "p:")) != -1) {
212 switch (ch) {
213 case 'p':
214 path_prefix = optarg;
215 break;
216 default:
217 usage();
218 /* NOTREACHED */
222 argc -= optind;
223 argv += optind;
225 #ifndef PROFILE
226 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
227 == -1)
228 err(1, "pledge");
229 #endif
230 if (argc == 1) {
231 char *cwd, *base, *dotgit;
232 repo_path = realpath(argv[0], NULL);
233 if (repo_path == NULL)
234 return got_error_from_errno();
235 cwd = getcwd(NULL, 0);
236 if (cwd == NULL) {
237 error = got_error_from_errno();
238 goto done;
240 if (path_prefix[0])
241 base = basename(path_prefix);
242 else
243 base = basename(repo_path);
244 if (base == NULL) {
245 error = got_error_from_errno();
246 goto done;
248 dotgit = strstr(base, ".git");
249 if (dotgit)
250 *dotgit = '\0';
251 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
252 error = got_error_from_errno();
253 free(cwd);
254 goto done;
256 free(cwd);
257 } else if (argc == 2) {
258 repo_path = realpath(argv[0], NULL);
259 if (repo_path == NULL) {
260 error = got_error_from_errno();
261 goto done;
263 worktree_path = realpath(argv[1], NULL);
264 if (worktree_path == NULL) {
265 error = got_error_from_errno();
266 goto done;
268 } else
269 usage_checkout();
271 error = got_repo_open(&repo, repo_path);
272 if (error != NULL)
273 goto done;
274 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
275 if (error != NULL)
276 goto done;
278 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
279 if (error != NULL)
280 goto done;
282 error = got_worktree_open(&worktree, worktree_path);
283 if (error != NULL)
284 goto done;
286 error = got_worktree_checkout_files(worktree, head_ref, repo,
287 checkout_progress, worktree_path, checkout_cancel, NULL);
288 if (error != NULL)
289 goto done;
291 printf("Now shut up and hack\n");
293 done:
294 free(repo_path);
295 free(worktree_path);
296 return error;
299 static const struct got_error *
300 print_patch(struct got_commit_object *commit, struct got_object_id *id,
301 int diff_context, struct got_repository *repo)
303 const struct got_error *err = NULL;
304 struct got_tree_object *tree1 = NULL, *tree2;
305 struct got_object_qid *qid;
307 err = got_object_open_as_tree(&tree2, repo, commit->tree_id);
308 if (err)
309 return err;
311 qid = SIMPLEQ_FIRST(&commit->parent_ids);
312 if (qid != NULL) {
313 struct got_commit_object *pcommit;
315 err = got_object_open_as_commit(&pcommit, repo, qid->id);
316 if (err)
317 return err;
319 err = got_object_open_as_tree(&tree1, repo, pcommit->tree_id);
320 got_object_commit_close(pcommit);
321 if (err)
322 return err;
325 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
326 if (tree1)
327 got_object_tree_close(tree1);
328 got_object_tree_close(tree2);
329 return err;
332 static char *
333 get_datestr(time_t *time, char *datebuf)
335 char *p, *s = ctime_r(time, datebuf);
336 p = strchr(s, '\n');
337 if (p)
338 *p = '\0';
339 return s;
342 static const struct got_error *
343 print_commit(struct got_commit_object *commit, struct got_object_id *id,
344 struct got_repository *repo, int show_patch, int diff_context)
346 const struct got_error *err = NULL;
347 char *id_str, *datestr, *logmsg0, *logmsg, *line;
348 char datebuf[26];
350 err = got_object_id_str(&id_str, id);
351 if (err)
352 return err;
354 printf("-----------------------------------------------\n");
355 printf("commit %s\n", id_str);
356 free(id_str);
357 printf("from: %s\n", commit->author);
358 datestr = get_datestr(&commit->committer_time, datebuf);
359 printf("date: %s UTC\n", datestr);
360 if (strcmp(commit->author, commit->committer) != 0)
361 printf("via: %s\n", commit->committer);
362 if (commit->nparents > 1) {
363 struct got_object_qid *qid;
364 int n = 1;
365 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
366 err = got_object_id_str(&id_str, qid->id);
367 if (err)
368 return err;
369 printf("parent %d: %s\n", n++, id_str);
370 free(id_str);
374 logmsg0 = strdup(commit->logmsg);
375 if (logmsg0 == NULL)
376 return got_error_from_errno();
378 logmsg = logmsg0;
379 do {
380 line = strsep(&logmsg, "\n");
381 if (line)
382 printf(" %s\n", line);
383 } while (line);
384 free(logmsg0);
386 if (show_patch) {
387 err = print_patch(commit, id, diff_context, repo);
388 if (err == 0)
389 printf("\n");
392 fflush(stdout);
393 return err;
396 static const struct got_error *
397 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
398 struct got_repository *repo, char *path, int show_patch, int diff_context,
399 int limit, int first_parent_traversal)
401 const struct got_error *err;
402 struct got_commit_graph *graph;
404 err = got_commit_graph_open(&graph, root_id, path,
405 first_parent_traversal, repo);
406 if (err)
407 return err;
408 err = got_commit_graph_iter_start(graph, root_id, repo);
409 if (err)
410 goto done;
411 while (1) {
412 struct got_commit_object *commit;
413 struct got_object_id *id;
415 if (sigint_received || sigpipe_received)
416 break;
418 err = got_commit_graph_iter_next(&id, graph);
419 if (err) {
420 if (err->code == GOT_ERR_ITER_COMPLETED) {
421 err = NULL;
422 break;
424 if (err->code != GOT_ERR_ITER_NEED_MORE)
425 break;
426 err = got_commit_graph_fetch_commits(graph, 1, repo);
427 if (err)
428 break;
429 else
430 continue;
432 if (id == NULL)
433 break;
435 err = got_object_open_as_commit(&commit, repo, id);
436 if (err)
437 break;
438 err = print_commit(commit, id, repo, show_patch, diff_context);
439 got_object_commit_close(commit);
440 if (err || (limit && --limit == 0))
441 break;
443 done:
444 got_commit_graph_close(graph);
445 return err;
448 __dead static void
449 usage_log(void)
451 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
452 "[-r repository-path] [path]\n", getprogname());
453 exit(1);
456 static const struct got_error *
457 cmd_log(int argc, char *argv[])
459 const struct got_error *error;
460 struct got_repository *repo = NULL;
461 struct got_object_id *id = NULL;
462 struct got_object *obj = NULL;
463 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
464 char *start_commit = NULL;
465 int diff_context = 3, ch;
466 int show_patch = 0, limit = 0, first_parent_traversal = 0;
467 const char *errstr;
469 #ifndef PROFILE
470 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
471 == -1)
472 err(1, "pledge");
473 #endif
475 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
476 switch (ch) {
477 case 'p':
478 show_patch = 1;
479 break;
480 case 'c':
481 start_commit = optarg;
482 break;
483 case 'C':
484 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
485 &errstr);
486 if (errstr != NULL)
487 err(1, "-C option %s", errstr);
488 break;
489 case 'l':
490 limit = strtonum(optarg, 1, INT_MAX, &errstr);
491 if (errstr != NULL)
492 err(1, "-l option %s", errstr);
493 break;
494 case 'f':
495 first_parent_traversal = 1;
496 break;
497 case 'r':
498 repo_path = realpath(optarg, NULL);
499 if (repo_path == NULL)
500 err(1, "-r option");
501 break;
502 default:
503 usage();
504 /* NOTREACHED */
508 argc -= optind;
509 argv += optind;
511 if (argc == 0)
512 path = strdup("");
513 else if (argc == 1)
514 path = strdup(argv[0]);
515 else
516 usage_log();
517 if (path == NULL)
518 return got_error_from_errno();
520 cwd = getcwd(NULL, 0);
521 if (cwd == NULL) {
522 error = got_error_from_errno();
523 goto done;
525 if (repo_path == NULL) {
526 repo_path = strdup(cwd);
527 if (repo_path == NULL) {
528 error = got_error_from_errno();
529 goto done;
533 error = got_repo_open(&repo, repo_path);
534 if (error != NULL)
535 goto done;
537 if (start_commit == NULL) {
538 struct got_reference *head_ref;
539 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
540 if (error != NULL)
541 return error;
542 error = got_ref_resolve(&id, repo, head_ref);
543 got_ref_close(head_ref);
544 if (error != NULL)
545 return error;
546 error = got_object_open(&obj, repo, id);
547 } else {
548 struct got_reference *ref;
549 error = got_ref_open(&ref, repo, start_commit);
550 if (error == NULL) {
551 error = got_ref_resolve(&id, repo, ref);
552 got_ref_close(ref);
553 if (error != NULL)
554 return error;
555 error = got_object_open(&obj, repo, id);
556 if (error != NULL)
557 return error;
559 if (obj == NULL) {
560 error = got_object_open_by_id_str(&obj, repo,
561 start_commit);
562 if (error != NULL)
563 return error;
564 id = got_object_id_dup(got_object_get_id(obj));
565 if (id == NULL)
566 error = got_error_from_errno();
569 if (error != NULL)
570 goto done;
571 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
572 error = got_error(GOT_ERR_OBJ_TYPE);
573 goto done;
576 error = got_repo_map_path(&in_repo_path, repo, path, 1);
577 if (error != NULL)
578 goto done;
579 if (in_repo_path) {
580 free(path);
581 path = in_repo_path;
584 error = print_commits(obj, id, repo, path, show_patch,
585 diff_context, limit, first_parent_traversal);
586 done:
587 free(path);
588 free(repo_path);
589 free(cwd);
590 if (obj)
591 got_object_close(obj);
592 free(id);
593 if (repo) {
594 const struct got_error *repo_error;
595 repo_error = got_repo_close(repo);
596 if (error == NULL)
597 error = repo_error;
599 return error;
602 __dead static void
603 usage_diff(void)
605 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
606 "object1 object2\n", getprogname());
607 exit(1);
610 static const struct got_error *
611 cmd_diff(int argc, char *argv[])
613 const struct got_error *error;
614 struct got_repository *repo = NULL;
615 struct got_object *obj1 = NULL, *obj2 = NULL;
616 char *repo_path = NULL;
617 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
618 int diff_context = 3, ch;
619 const char *errstr;
621 #ifndef PROFILE
622 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
623 == -1)
624 err(1, "pledge");
625 #endif
627 while ((ch = getopt(argc, argv, "C:")) != -1) {
628 switch (ch) {
629 case 'C':
630 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
631 if (errstr != NULL)
632 err(1, "-C option %s", errstr);
633 break;
634 default:
635 usage();
636 /* NOTREACHED */
640 argc -= optind;
641 argv += optind;
643 if (argc == 0) {
644 usage_diff(); /* TODO show local worktree changes */
645 } else if (argc == 2) {
646 repo_path = getcwd(NULL, 0);
647 if (repo_path == NULL)
648 return got_error_from_errno();
649 obj_id_str1 = argv[0];
650 obj_id_str2 = argv[1];
651 } else if (argc == 3) {
652 repo_path = realpath(argv[0], NULL);
653 if (repo_path == NULL)
654 return got_error_from_errno();
655 obj_id_str1 = argv[1];
656 obj_id_str2 = argv[2];
657 } else
658 usage_diff();
660 error = got_repo_open(&repo, repo_path);
661 free(repo_path);
662 if (error != NULL)
663 goto done;
665 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
666 if (error)
667 goto done;
669 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
670 if (error)
671 goto done;
673 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
674 error = got_error(GOT_ERR_OBJ_TYPE);
675 goto done;
678 switch (got_object_get_type(obj1)) {
679 case GOT_OBJ_TYPE_BLOB:
680 error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
681 diff_context, repo, stdout);
682 break;
683 case GOT_OBJ_TYPE_TREE:
684 error = got_diff_objects_as_trees(obj1, obj2, "", "",
685 diff_context, repo, stdout);
686 break;
687 case GOT_OBJ_TYPE_COMMIT:
688 printf("diff: %s %s\n", obj_id_str1 ? obj_id_str1 : "/dev/null",
689 obj_id_str2);
690 error = got_diff_objects_as_commits(obj1, obj2, diff_context,
691 repo, stdout);
692 break;
693 default:
694 error = got_error(GOT_ERR_OBJ_TYPE);
697 done:
698 if (obj1)
699 got_object_close(obj1);
700 if (obj2)
701 got_object_close(obj2);
702 if (repo) {
703 const struct got_error *repo_error;
704 repo_error = got_repo_close(repo);
705 if (error == NULL)
706 error = repo_error;
708 return error;
711 __dead static void
712 usage_blame(void)
714 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
715 getprogname());
716 exit(1);
719 static const struct got_error *
720 cmd_blame(int argc, char *argv[])
722 const struct got_error *error;
723 struct got_repository *repo = NULL;
724 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
725 struct got_object_id *commit_id = NULL;
726 char *commit_id_str = NULL;
727 int ch;
729 #ifndef PROFILE
730 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
731 == -1)
732 err(1, "pledge");
733 #endif
735 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
736 switch (ch) {
737 case 'c':
738 commit_id_str = optarg;
739 break;
740 case 'r':
741 repo_path = realpath(optarg, NULL);
742 if (repo_path == NULL)
743 err(1, "-r option");
744 break;
745 default:
746 usage();
747 /* NOTREACHED */
751 argc -= optind;
752 argv += optind;
754 if (argc == 1)
755 path = argv[0];
756 else
757 usage_blame();
759 cwd = getcwd(NULL, 0);
760 if (cwd == NULL) {
761 error = got_error_from_errno();
762 goto done;
764 if (repo_path == NULL) {
765 repo_path = strdup(cwd);
766 if (repo_path == NULL) {
767 error = got_error_from_errno();
768 goto done;
772 error = got_repo_open(&repo, repo_path);
773 if (error != NULL)
774 goto done;
776 error = got_repo_map_path(&in_repo_path, repo, path, 1);
777 if (error != NULL)
778 goto done;
780 if (commit_id_str == NULL) {
781 struct got_reference *head_ref;
782 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
783 if (error != NULL)
784 goto done;
785 error = got_ref_resolve(&commit_id, repo, head_ref);
786 got_ref_close(head_ref);
787 if (error != NULL)
788 goto done;
789 } else {
790 struct got_object *obj;
791 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
792 if (error != NULL)
793 goto done;
794 commit_id = got_object_id_dup(got_object_get_id(obj));
795 if (commit_id == NULL)
796 error = got_error_from_errno();
797 got_object_close(obj);
800 error = got_blame(in_repo_path, commit_id, repo, stdout);
801 done:
802 free(in_repo_path);
803 free(repo_path);
804 free(cwd);
805 free(commit_id);
806 if (repo) {
807 const struct got_error *repo_error;
808 repo_error = got_repo_close(repo);
809 if (error == NULL)
810 error = repo_error;
812 return error;
815 __dead static void
816 usage_tree(void)
818 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
819 getprogname());
820 exit(1);
824 static const struct got_error *
825 print_tree(const char *path, struct got_object_id *commit_id,
826 int show_ids, struct got_repository *repo)
828 const struct got_error *err = NULL;
829 struct got_object_id *tree_id = NULL;
830 struct got_tree_object *tree = NULL;
831 const struct got_tree_entries *entries;
832 struct got_tree_entry *te;
834 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
835 if (err)
836 goto done;
838 err = got_object_open_as_tree(&tree, repo, tree_id);
839 if (err)
840 goto done;
841 entries = got_object_tree_get_entries(tree);
842 te = SIMPLEQ_FIRST(&entries->head);
843 while (te) {
844 char *id = NULL;
846 if (sigint_received || sigpipe_received)
847 break;
849 if (show_ids) {
850 char *id_str;
851 err = got_object_id_str(&id_str, te->id);
852 if (err)
853 goto done;
854 if (asprintf(&id, "%s ", id_str) == -1) {
855 err = got_error_from_errno();
856 free(id_str);
857 goto done;
859 free(id_str);
861 printf("%s%s%s\n", id ? id : "",
862 te->name, S_ISDIR(te->mode) ? "/" : "");
863 te = SIMPLEQ_NEXT(te, entry);
864 free(id);
866 done:
867 if (tree)
868 got_object_tree_close(tree);
869 free(tree_id);
870 return err;
873 static const struct got_error *
874 cmd_tree(int argc, char *argv[])
876 const struct got_error *error;
877 struct got_repository *repo = NULL;
878 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
879 struct got_object_id *commit_id = NULL;
880 char *commit_id_str = NULL;
881 int show_ids = 0;
882 int ch;
884 #ifndef PROFILE
885 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
886 == -1)
887 err(1, "pledge");
888 #endif
890 while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
891 switch (ch) {
892 case 'c':
893 commit_id_str = optarg;
894 break;
895 case 'r':
896 repo_path = realpath(optarg, NULL);
897 if (repo_path == NULL)
898 err(1, "-r option");
899 break;
900 case 'i':
901 show_ids = 1;
902 break;
903 default:
904 usage();
905 /* NOTREACHED */
909 argc -= optind;
910 argv += optind;
912 if (argc == 1)
913 path = argv[0];
914 else if (argc > 1)
915 usage_tree();
916 else
917 path = "/";
919 cwd = getcwd(NULL, 0);
920 if (cwd == NULL) {
921 error = got_error_from_errno();
922 goto done;
924 if (repo_path == NULL) {
925 repo_path = strdup(cwd);
926 if (repo_path == NULL) {
927 error = got_error_from_errno();
928 goto done;
932 error = got_repo_open(&repo, repo_path);
933 if (error != NULL)
934 goto done;
936 error = got_repo_map_path(&in_repo_path, repo, path, 1);
937 if (error != NULL)
938 goto done;
940 if (commit_id_str == NULL) {
941 struct got_reference *head_ref;
942 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
943 if (error != NULL)
944 goto done;
945 error = got_ref_resolve(&commit_id, repo, head_ref);
946 got_ref_close(head_ref);
947 if (error != NULL)
948 goto done;
949 } else {
950 struct got_object *obj;
951 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
952 if (error != NULL)
953 goto done;
954 commit_id = got_object_id_dup(got_object_get_id(obj));
955 if (commit_id == NULL)
956 error = got_error_from_errno();
957 got_object_close(obj);
960 error = print_tree(in_repo_path, commit_id, show_ids, repo);
961 done:
962 free(in_repo_path);
963 free(repo_path);
964 free(cwd);
965 free(commit_id);
966 if (repo) {
967 const struct got_error *repo_error;
968 repo_error = got_repo_close(repo);
969 if (error == NULL)
970 error = repo_error;
972 return error;
975 #ifdef notyet
976 static const struct got_error *
977 cmd_status(int argc __unused, char *argv[] __unused)
979 git_repository *repo = NULL;
980 git_status_list *status;
981 git_status_options statusopts;
982 size_t i;
984 git_libgit2_init();
986 if (git_repository_open_ext(&repo, ".", 0, NULL))
987 errx(1, "git_repository_open: %s", giterr_last()->message);
989 if (git_repository_is_bare(repo))
990 errx(1, "bar repository");
992 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
993 errx(1, "git_status_init_options: %s", giterr_last()->message);
995 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
996 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
997 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
998 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1000 if (git_status_list_new(&status, repo, &statusopts))
1001 errx(1, "git_status_list_new: %s", giterr_last()->message);
1003 for (i = 0; i < git_status_list_entrycount(status); i++) {
1004 const git_status_entry *se;
1006 se = git_status_byindex(status, i);
1007 switch (se->status) {
1008 case GIT_STATUS_WT_NEW:
1009 printf("? %s\n", se->index_to_workdir->new_file.path);
1010 break;
1011 case GIT_STATUS_WT_MODIFIED:
1012 printf("M %s\n", se->index_to_workdir->new_file.path);
1013 break;
1014 case GIT_STATUS_WT_DELETED:
1015 printf("R %s\n", se->index_to_workdir->new_file.path);
1016 break;
1017 case GIT_STATUS_WT_RENAMED:
1018 printf("m %s -> %s\n",
1019 se->index_to_workdir->old_file.path,
1020 se->index_to_workdir->new_file.path);
1021 break;
1022 case GIT_STATUS_WT_TYPECHANGE:
1023 printf("t %s\n", se->index_to_workdir->new_file.path);
1024 break;
1025 case GIT_STATUS_INDEX_NEW:
1026 printf("A %s\n", se->head_to_index->new_file.path);
1027 break;
1028 case GIT_STATUS_INDEX_MODIFIED:
1029 printf("M %s\n", se->head_to_index->old_file.path);
1030 break;
1031 case GIT_STATUS_INDEX_DELETED:
1032 printf("R %s\n", se->head_to_index->old_file.path);
1033 break;
1034 case GIT_STATUS_INDEX_RENAMED:
1035 printf("m %s -> %s\n",
1036 se->head_to_index->old_file.path,
1037 se->head_to_index->new_file.path);
1038 break;
1039 case GIT_STATUS_INDEX_TYPECHANGE:
1040 printf("t %s\n", se->head_to_index->old_file.path);
1041 break;
1042 case GIT_STATUS_CURRENT:
1043 default:
1044 break;
1048 git_status_list_free(status);
1049 git_repository_free(repo);
1050 git_libgit2_shutdown();
1052 return 0;
1054 #endif