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;
306 char *id_str1 = NULL, *id_str2;
308 err = got_object_open_as_tree(&tree2, repo, commit->tree_id);
309 if (err)
310 return err;
312 qid = SIMPLEQ_FIRST(&commit->parent_ids);
313 if (qid != NULL) {
314 struct got_commit_object *pcommit;
316 err = got_object_open_as_commit(&pcommit, repo, qid->id);
317 if (err)
318 return err;
320 err = got_object_open_as_tree(&tree1, repo, pcommit->tree_id);
321 got_object_commit_close(pcommit);
322 if (err)
323 return err;
325 err = got_object_id_str(&id_str1, qid->id);
326 if (err)
327 return err;
330 err = got_object_id_str(&id_str2, id);
331 if (err)
332 goto done;
334 printf("diff: %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
335 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
336 done:
337 if (tree1)
338 got_object_tree_close(tree1);
339 got_object_tree_close(tree2);
340 free(id_str1);
341 free(id_str2);
342 return err;
345 static char *
346 get_datestr(time_t *time, char *datebuf)
348 char *p, *s = ctime_r(time, datebuf);
349 p = strchr(s, '\n');
350 if (p)
351 *p = '\0';
352 return s;
355 static const struct got_error *
356 print_commit(struct got_commit_object *commit, struct got_object_id *id,
357 struct got_repository *repo, int show_patch, int diff_context)
359 const struct got_error *err = NULL;
360 char *id_str, *datestr, *logmsg0, *logmsg, *line;
361 char datebuf[26];
363 err = got_object_id_str(&id_str, id);
364 if (err)
365 return err;
367 printf("-----------------------------------------------\n");
368 printf("commit %s\n", id_str);
369 free(id_str);
370 printf("from: %s\n", commit->author);
371 datestr = get_datestr(&commit->committer_time, datebuf);
372 printf("date: %s UTC\n", datestr);
373 if (strcmp(commit->author, commit->committer) != 0)
374 printf("via: %s\n", commit->committer);
375 if (commit->nparents > 1) {
376 struct got_object_qid *qid;
377 int n = 1;
378 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
379 err = got_object_id_str(&id_str, qid->id);
380 if (err)
381 return err;
382 printf("parent %d: %s\n", n++, id_str);
383 free(id_str);
387 logmsg0 = strdup(commit->logmsg);
388 if (logmsg0 == NULL)
389 return got_error_from_errno();
391 logmsg = logmsg0;
392 do {
393 line = strsep(&logmsg, "\n");
394 if (line)
395 printf(" %s\n", line);
396 } while (line);
397 free(logmsg0);
399 if (show_patch) {
400 err = print_patch(commit, id, diff_context, repo);
401 if (err == 0)
402 printf("\n");
405 fflush(stdout);
406 return err;
409 static const struct got_error *
410 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
411 struct got_repository *repo, char *path, int show_patch, int diff_context,
412 int limit, int first_parent_traversal)
414 const struct got_error *err;
415 struct got_commit_graph *graph;
417 err = got_commit_graph_open(&graph, root_id, path,
418 first_parent_traversal, repo);
419 if (err)
420 return err;
421 err = got_commit_graph_iter_start(graph, root_id, repo);
422 if (err)
423 goto done;
424 while (1) {
425 struct got_commit_object *commit;
426 struct got_object_id *id;
428 if (sigint_received || sigpipe_received)
429 break;
431 err = got_commit_graph_iter_next(&id, graph);
432 if (err) {
433 if (err->code == GOT_ERR_ITER_COMPLETED) {
434 err = NULL;
435 break;
437 if (err->code != GOT_ERR_ITER_NEED_MORE)
438 break;
439 err = got_commit_graph_fetch_commits(graph, 1, repo);
440 if (err)
441 break;
442 else
443 continue;
445 if (id == NULL)
446 break;
448 err = got_object_open_as_commit(&commit, repo, id);
449 if (err)
450 break;
451 err = print_commit(commit, id, repo, show_patch, diff_context);
452 got_object_commit_close(commit);
453 if (err || (limit && --limit == 0))
454 break;
456 done:
457 got_commit_graph_close(graph);
458 return err;
461 __dead static void
462 usage_log(void)
464 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
465 "[-r repository-path] [path]\n", getprogname());
466 exit(1);
469 static const struct got_error *
470 cmd_log(int argc, char *argv[])
472 const struct got_error *error;
473 struct got_repository *repo = NULL;
474 struct got_object_id *id = NULL;
475 struct got_object *obj = NULL;
476 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
477 char *start_commit = NULL;
478 int diff_context = 3, ch;
479 int show_patch = 0, limit = 0, first_parent_traversal = 0;
480 const char *errstr;
482 #ifndef PROFILE
483 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
484 == -1)
485 err(1, "pledge");
486 #endif
488 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
489 switch (ch) {
490 case 'p':
491 show_patch = 1;
492 break;
493 case 'c':
494 start_commit = optarg;
495 break;
496 case 'C':
497 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
498 &errstr);
499 if (errstr != NULL)
500 err(1, "-C option %s", errstr);
501 break;
502 case 'l':
503 limit = strtonum(optarg, 1, INT_MAX, &errstr);
504 if (errstr != NULL)
505 err(1, "-l option %s", errstr);
506 break;
507 case 'f':
508 first_parent_traversal = 1;
509 break;
510 case 'r':
511 repo_path = realpath(optarg, NULL);
512 if (repo_path == NULL)
513 err(1, "-r option");
514 break;
515 default:
516 usage();
517 /* NOTREACHED */
521 argc -= optind;
522 argv += optind;
524 if (argc == 0)
525 path = strdup("");
526 else if (argc == 1)
527 path = strdup(argv[0]);
528 else
529 usage_log();
530 if (path == NULL)
531 return got_error_from_errno();
533 cwd = getcwd(NULL, 0);
534 if (cwd == NULL) {
535 error = got_error_from_errno();
536 goto done;
538 if (repo_path == NULL) {
539 repo_path = strdup(cwd);
540 if (repo_path == NULL) {
541 error = got_error_from_errno();
542 goto done;
546 error = got_repo_open(&repo, repo_path);
547 if (error != NULL)
548 goto done;
550 if (start_commit == NULL) {
551 struct got_reference *head_ref;
552 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
553 if (error != NULL)
554 return error;
555 error = got_ref_resolve(&id, repo, head_ref);
556 got_ref_close(head_ref);
557 if (error != NULL)
558 return error;
559 error = got_object_open(&obj, repo, id);
560 } else {
561 struct got_reference *ref;
562 error = got_ref_open(&ref, repo, start_commit);
563 if (error == NULL) {
564 error = got_ref_resolve(&id, repo, ref);
565 got_ref_close(ref);
566 if (error != NULL)
567 return error;
568 error = got_object_open(&obj, repo, id);
569 if (error != NULL)
570 return error;
572 if (obj == NULL) {
573 error = got_object_open_by_id_str(&obj, repo,
574 start_commit);
575 if (error != NULL)
576 return error;
577 id = got_object_id_dup(got_object_get_id(obj));
578 if (id == NULL)
579 error = got_error_from_errno();
582 if (error != NULL)
583 goto done;
584 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
585 error = got_error(GOT_ERR_OBJ_TYPE);
586 goto done;
589 error = got_repo_map_path(&in_repo_path, repo, path, 1);
590 if (error != NULL)
591 goto done;
592 if (in_repo_path) {
593 free(path);
594 path = in_repo_path;
597 error = print_commits(obj, id, repo, path, show_patch,
598 diff_context, limit, first_parent_traversal);
599 done:
600 free(path);
601 free(repo_path);
602 free(cwd);
603 if (obj)
604 got_object_close(obj);
605 free(id);
606 if (repo) {
607 const struct got_error *repo_error;
608 repo_error = got_repo_close(repo);
609 if (error == NULL)
610 error = repo_error;
612 return error;
615 __dead static void
616 usage_diff(void)
618 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
619 "object1 object2\n", getprogname());
620 exit(1);
623 static const struct got_error *
624 cmd_diff(int argc, char *argv[])
626 const struct got_error *error;
627 struct got_repository *repo = NULL;
628 struct got_object *obj1 = NULL, *obj2 = NULL;
629 char *repo_path = NULL;
630 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
631 int diff_context = 3, ch;
632 const char *errstr;
634 #ifndef PROFILE
635 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
636 == -1)
637 err(1, "pledge");
638 #endif
640 while ((ch = getopt(argc, argv, "C:")) != -1) {
641 switch (ch) {
642 case 'C':
643 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
644 if (errstr != NULL)
645 err(1, "-C option %s", errstr);
646 break;
647 default:
648 usage();
649 /* NOTREACHED */
653 argc -= optind;
654 argv += optind;
656 if (argc == 0) {
657 usage_diff(); /* TODO show local worktree changes */
658 } else if (argc == 2) {
659 repo_path = getcwd(NULL, 0);
660 if (repo_path == NULL)
661 return got_error_from_errno();
662 obj_id_str1 = argv[0];
663 obj_id_str2 = argv[1];
664 } else if (argc == 3) {
665 repo_path = realpath(argv[0], NULL);
666 if (repo_path == NULL)
667 return got_error_from_errno();
668 obj_id_str1 = argv[1];
669 obj_id_str2 = argv[2];
670 } else
671 usage_diff();
673 error = got_repo_open(&repo, repo_path);
674 free(repo_path);
675 if (error != NULL)
676 goto done;
678 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
679 if (error)
680 goto done;
682 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
683 if (error)
684 goto done;
686 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
687 error = got_error(GOT_ERR_OBJ_TYPE);
688 goto done;
691 switch (got_object_get_type(obj1)) {
692 case GOT_OBJ_TYPE_BLOB:
693 error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
694 diff_context, repo, stdout);
695 break;
696 case GOT_OBJ_TYPE_TREE:
697 error = got_diff_objects_as_trees(obj1, obj2, "", "",
698 diff_context, repo, stdout);
699 break;
700 case GOT_OBJ_TYPE_COMMIT:
701 printf("diff: %s %s\n", obj_id_str1 ? obj_id_str1 : "/dev/null",
702 obj_id_str2);
703 error = got_diff_objects_as_commits(obj1, obj2, diff_context,
704 repo, stdout);
705 break;
706 default:
707 error = got_error(GOT_ERR_OBJ_TYPE);
710 done:
711 if (obj1)
712 got_object_close(obj1);
713 if (obj2)
714 got_object_close(obj2);
715 if (repo) {
716 const struct got_error *repo_error;
717 repo_error = got_repo_close(repo);
718 if (error == NULL)
719 error = repo_error;
721 return error;
724 __dead static void
725 usage_blame(void)
727 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
728 getprogname());
729 exit(1);
732 static const struct got_error *
733 cmd_blame(int argc, char *argv[])
735 const struct got_error *error;
736 struct got_repository *repo = NULL;
737 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
738 struct got_object_id *commit_id = NULL;
739 char *commit_id_str = NULL;
740 int ch;
742 #ifndef PROFILE
743 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
744 == -1)
745 err(1, "pledge");
746 #endif
748 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
749 switch (ch) {
750 case 'c':
751 commit_id_str = optarg;
752 break;
753 case 'r':
754 repo_path = realpath(optarg, NULL);
755 if (repo_path == NULL)
756 err(1, "-r option");
757 break;
758 default:
759 usage();
760 /* NOTREACHED */
764 argc -= optind;
765 argv += optind;
767 if (argc == 1)
768 path = argv[0];
769 else
770 usage_blame();
772 cwd = getcwd(NULL, 0);
773 if (cwd == NULL) {
774 error = got_error_from_errno();
775 goto done;
777 if (repo_path == NULL) {
778 repo_path = strdup(cwd);
779 if (repo_path == NULL) {
780 error = got_error_from_errno();
781 goto done;
785 error = got_repo_open(&repo, repo_path);
786 if (error != NULL)
787 goto done;
789 error = got_repo_map_path(&in_repo_path, repo, path, 1);
790 if (error != NULL)
791 goto done;
793 if (commit_id_str == NULL) {
794 struct got_reference *head_ref;
795 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
796 if (error != NULL)
797 goto done;
798 error = got_ref_resolve(&commit_id, repo, head_ref);
799 got_ref_close(head_ref);
800 if (error != NULL)
801 goto done;
802 } else {
803 struct got_object *obj;
804 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
805 if (error != NULL)
806 goto done;
807 commit_id = got_object_id_dup(got_object_get_id(obj));
808 if (commit_id == NULL)
809 error = got_error_from_errno();
810 got_object_close(obj);
813 error = got_blame(in_repo_path, commit_id, repo, stdout);
814 done:
815 free(in_repo_path);
816 free(repo_path);
817 free(cwd);
818 free(commit_id);
819 if (repo) {
820 const struct got_error *repo_error;
821 repo_error = got_repo_close(repo);
822 if (error == NULL)
823 error = repo_error;
825 return error;
828 __dead static void
829 usage_tree(void)
831 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
832 getprogname());
833 exit(1);
837 static const struct got_error *
838 print_tree(const char *path, struct got_object_id *commit_id,
839 int show_ids, struct got_repository *repo)
841 const struct got_error *err = NULL;
842 struct got_object_id *tree_id = NULL;
843 struct got_tree_object *tree = NULL;
844 const struct got_tree_entries *entries;
845 struct got_tree_entry *te;
847 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
848 if (err)
849 goto done;
851 err = got_object_open_as_tree(&tree, repo, tree_id);
852 if (err)
853 goto done;
854 entries = got_object_tree_get_entries(tree);
855 te = SIMPLEQ_FIRST(&entries->head);
856 while (te) {
857 char *id = NULL;
859 if (sigint_received || sigpipe_received)
860 break;
862 if (show_ids) {
863 char *id_str;
864 err = got_object_id_str(&id_str, te->id);
865 if (err)
866 goto done;
867 if (asprintf(&id, "%s ", id_str) == -1) {
868 err = got_error_from_errno();
869 free(id_str);
870 goto done;
872 free(id_str);
874 printf("%s%s%s\n", id ? id : "",
875 te->name, S_ISDIR(te->mode) ? "/" : "");
876 te = SIMPLEQ_NEXT(te, entry);
877 free(id);
879 done:
880 if (tree)
881 got_object_tree_close(tree);
882 free(tree_id);
883 return err;
886 static const struct got_error *
887 cmd_tree(int argc, char *argv[])
889 const struct got_error *error;
890 struct got_repository *repo = NULL;
891 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
892 struct got_object_id *commit_id = NULL;
893 char *commit_id_str = NULL;
894 int show_ids = 0;
895 int ch;
897 #ifndef PROFILE
898 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
899 == -1)
900 err(1, "pledge");
901 #endif
903 while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
904 switch (ch) {
905 case 'c':
906 commit_id_str = optarg;
907 break;
908 case 'r':
909 repo_path = realpath(optarg, NULL);
910 if (repo_path == NULL)
911 err(1, "-r option");
912 break;
913 case 'i':
914 show_ids = 1;
915 break;
916 default:
917 usage();
918 /* NOTREACHED */
922 argc -= optind;
923 argv += optind;
925 if (argc == 1)
926 path = argv[0];
927 else if (argc > 1)
928 usage_tree();
929 else
930 path = "/";
932 cwd = getcwd(NULL, 0);
933 if (cwd == NULL) {
934 error = got_error_from_errno();
935 goto done;
937 if (repo_path == NULL) {
938 repo_path = strdup(cwd);
939 if (repo_path == NULL) {
940 error = got_error_from_errno();
941 goto done;
945 error = got_repo_open(&repo, repo_path);
946 if (error != NULL)
947 goto done;
949 error = got_repo_map_path(&in_repo_path, repo, path, 1);
950 if (error != NULL)
951 goto done;
953 if (commit_id_str == NULL) {
954 struct got_reference *head_ref;
955 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
956 if (error != NULL)
957 goto done;
958 error = got_ref_resolve(&commit_id, repo, head_ref);
959 got_ref_close(head_ref);
960 if (error != NULL)
961 goto done;
962 } else {
963 struct got_object *obj;
964 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
965 if (error != NULL)
966 goto done;
967 commit_id = got_object_id_dup(got_object_get_id(obj));
968 if (commit_id == NULL)
969 error = got_error_from_errno();
970 got_object_close(obj);
973 error = print_tree(in_repo_path, commit_id, show_ids, repo);
974 done:
975 free(in_repo_path);
976 free(repo_path);
977 free(cwd);
978 free(commit_id);
979 if (repo) {
980 const struct got_error *repo_error;
981 repo_error = got_repo_close(repo);
982 if (error == NULL)
983 error = repo_error;
985 return error;
988 #ifdef notyet
989 static const struct got_error *
990 cmd_status(int argc __unused, char *argv[] __unused)
992 git_repository *repo = NULL;
993 git_status_list *status;
994 git_status_options statusopts;
995 size_t i;
997 git_libgit2_init();
999 if (git_repository_open_ext(&repo, ".", 0, NULL))
1000 errx(1, "git_repository_open: %s", giterr_last()->message);
1002 if (git_repository_is_bare(repo))
1003 errx(1, "bar repository");
1005 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
1006 errx(1, "git_status_init_options: %s", giterr_last()->message);
1008 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
1009 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
1010 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
1011 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1013 if (git_status_list_new(&status, repo, &statusopts))
1014 errx(1, "git_status_list_new: %s", giterr_last()->message);
1016 for (i = 0; i < git_status_list_entrycount(status); i++) {
1017 const git_status_entry *se;
1019 se = git_status_byindex(status, i);
1020 switch (se->status) {
1021 case GIT_STATUS_WT_NEW:
1022 printf("? %s\n", se->index_to_workdir->new_file.path);
1023 break;
1024 case GIT_STATUS_WT_MODIFIED:
1025 printf("M %s\n", se->index_to_workdir->new_file.path);
1026 break;
1027 case GIT_STATUS_WT_DELETED:
1028 printf("R %s\n", se->index_to_workdir->new_file.path);
1029 break;
1030 case GIT_STATUS_WT_RENAMED:
1031 printf("m %s -> %s\n",
1032 se->index_to_workdir->old_file.path,
1033 se->index_to_workdir->new_file.path);
1034 break;
1035 case GIT_STATUS_WT_TYPECHANGE:
1036 printf("t %s\n", se->index_to_workdir->new_file.path);
1037 break;
1038 case GIT_STATUS_INDEX_NEW:
1039 printf("A %s\n", se->head_to_index->new_file.path);
1040 break;
1041 case GIT_STATUS_INDEX_MODIFIED:
1042 printf("M %s\n", se->head_to_index->old_file.path);
1043 break;
1044 case GIT_STATUS_INDEX_DELETED:
1045 printf("R %s\n", se->head_to_index->old_file.path);
1046 break;
1047 case GIT_STATUS_INDEX_RENAMED:
1048 printf("m %s -> %s\n",
1049 se->head_to_index->old_file.path,
1050 se->head_to_index->new_file.path);
1051 break;
1052 case GIT_STATUS_INDEX_TYPECHANGE:
1053 printf("t %s\n", se->head_to_index->old_file.path);
1054 break;
1055 case GIT_STATUS_CURRENT:
1056 default:
1057 break;
1061 git_status_list_free(status);
1062 git_repository_free(repo);
1063 git_libgit2_shutdown();
1065 return 0;
1067 #endif