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,
309 got_object_commit_get_tree_id(commit));
310 if (err)
311 return err;
313 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
314 if (qid != NULL) {
315 struct got_commit_object *pcommit;
317 err = got_object_open_as_commit(&pcommit, repo, qid->id);
318 if (err)
319 return err;
321 err = got_object_open_as_tree(&tree1, repo,
322 got_object_commit_get_tree_id(pcommit));
323 got_object_commit_close(pcommit);
324 if (err)
325 return err;
327 err = got_object_id_str(&id_str1, qid->id);
328 if (err)
329 return err;
332 err = got_object_id_str(&id_str2, id);
333 if (err)
334 goto done;
336 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
337 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
338 done:
339 if (tree1)
340 got_object_tree_close(tree1);
341 got_object_tree_close(tree2);
342 free(id_str1);
343 free(id_str2);
344 return err;
347 static char *
348 get_datestr(time_t *time, char *datebuf)
350 char *p, *s = ctime_r(time, datebuf);
351 p = strchr(s, '\n');
352 if (p)
353 *p = '\0';
354 return s;
357 static const struct got_error *
358 print_commit(struct got_commit_object *commit, struct got_object_id *id,
359 struct got_repository *repo, int show_patch, int diff_context)
361 const struct got_error *err = NULL;
362 char *id_str, *datestr, *logmsg0, *logmsg, *line;
363 char datebuf[26];
364 time_t committer_time;
365 const char *author, *committer;
367 err = got_object_id_str(&id_str, id);
368 if (err)
369 return err;
371 printf("-----------------------------------------------\n");
372 printf("commit %s\n", id_str);
373 free(id_str);
374 printf("from: %s\n", got_object_commit_get_author(commit));
375 committer_time = got_object_commit_get_committer_time(commit);
376 datestr = get_datestr(&committer_time, datebuf);
377 printf("date: %s UTC\n", datestr);
378 author = got_object_commit_get_author(commit);
379 committer = got_object_commit_get_committer(commit);
380 if (strcmp(author, committer) != 0)
381 printf("via: %s\n", committer);
382 if (got_object_commit_get_nparents(commit) > 1) {
383 const struct got_object_id_queue *parent_ids;
384 struct got_object_qid *qid;
385 int n = 1;
386 parent_ids = got_object_commit_get_parent_ids(commit);
387 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
388 err = got_object_id_str(&id_str, qid->id);
389 if (err)
390 return err;
391 printf("parent %d: %s\n", n++, id_str);
392 free(id_str);
396 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
397 if (logmsg0 == NULL)
398 return got_error_from_errno();
400 logmsg = logmsg0;
401 do {
402 line = strsep(&logmsg, "\n");
403 if (line)
404 printf(" %s\n", line);
405 } while (line);
406 free(logmsg0);
408 if (show_patch) {
409 err = print_patch(commit, id, diff_context, repo);
410 if (err == 0)
411 printf("\n");
414 fflush(stdout);
415 return err;
418 static const struct got_error *
419 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
420 struct got_repository *repo, char *path, int show_patch, int diff_context,
421 int limit, int first_parent_traversal)
423 const struct got_error *err;
424 struct got_commit_graph *graph;
426 err = got_commit_graph_open(&graph, root_id, path,
427 first_parent_traversal, repo);
428 if (err)
429 return err;
430 err = got_commit_graph_iter_start(graph, root_id, repo);
431 if (err)
432 goto done;
433 while (1) {
434 struct got_commit_object *commit;
435 struct got_object_id *id;
437 if (sigint_received || sigpipe_received)
438 break;
440 err = got_commit_graph_iter_next(&id, graph);
441 if (err) {
442 if (err->code == GOT_ERR_ITER_COMPLETED) {
443 err = NULL;
444 break;
446 if (err->code != GOT_ERR_ITER_NEED_MORE)
447 break;
448 err = got_commit_graph_fetch_commits(graph, 1, repo);
449 if (err)
450 break;
451 else
452 continue;
454 if (id == NULL)
455 break;
457 err = got_object_open_as_commit(&commit, repo, id);
458 if (err)
459 break;
460 err = print_commit(commit, id, repo, show_patch, diff_context);
461 got_object_commit_close(commit);
462 if (err || (limit && --limit == 0))
463 break;
465 done:
466 got_commit_graph_close(graph);
467 return err;
470 __dead static void
471 usage_log(void)
473 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
474 "[-r repository-path] [path]\n", getprogname());
475 exit(1);
478 static const struct got_error *
479 cmd_log(int argc, char *argv[])
481 const struct got_error *error;
482 struct got_repository *repo = NULL;
483 struct got_object_id *id = NULL;
484 struct got_object *obj = NULL;
485 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
486 char *start_commit = NULL;
487 int diff_context = 3, ch;
488 int show_patch = 0, limit = 0, first_parent_traversal = 0;
489 const char *errstr;
491 #ifndef PROFILE
492 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
493 == -1)
494 err(1, "pledge");
495 #endif
497 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
498 switch (ch) {
499 case 'p':
500 show_patch = 1;
501 break;
502 case 'c':
503 start_commit = optarg;
504 break;
505 case 'C':
506 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
507 &errstr);
508 if (errstr != NULL)
509 err(1, "-C option %s", errstr);
510 break;
511 case 'l':
512 limit = strtonum(optarg, 1, INT_MAX, &errstr);
513 if (errstr != NULL)
514 err(1, "-l option %s", errstr);
515 break;
516 case 'f':
517 first_parent_traversal = 1;
518 break;
519 case 'r':
520 repo_path = realpath(optarg, NULL);
521 if (repo_path == NULL)
522 err(1, "-r option");
523 break;
524 default:
525 usage();
526 /* NOTREACHED */
530 argc -= optind;
531 argv += optind;
533 if (argc == 0)
534 path = strdup("");
535 else if (argc == 1)
536 path = strdup(argv[0]);
537 else
538 usage_log();
539 if (path == NULL)
540 return got_error_from_errno();
542 cwd = getcwd(NULL, 0);
543 if (cwd == NULL) {
544 error = got_error_from_errno();
545 goto done;
547 if (repo_path == NULL) {
548 repo_path = strdup(cwd);
549 if (repo_path == NULL) {
550 error = got_error_from_errno();
551 goto done;
555 error = got_repo_open(&repo, repo_path);
556 if (error != NULL)
557 goto done;
559 if (start_commit == NULL) {
560 struct got_reference *head_ref;
561 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
562 if (error != NULL)
563 return error;
564 error = got_ref_resolve(&id, repo, head_ref);
565 got_ref_close(head_ref);
566 if (error != NULL)
567 return error;
568 error = got_object_open(&obj, repo, id);
569 } else {
570 struct got_reference *ref;
571 error = got_ref_open(&ref, repo, start_commit);
572 if (error == NULL) {
573 error = got_ref_resolve(&id, repo, ref);
574 got_ref_close(ref);
575 if (error != NULL)
576 return error;
577 error = got_object_open(&obj, repo, id);
578 if (error != NULL)
579 return error;
581 if (obj == NULL) {
582 error = got_object_open_by_id_str(&obj, repo,
583 start_commit);
584 if (error != NULL)
585 return error;
586 id = got_object_id_dup(got_object_get_id(obj));
587 if (id == NULL)
588 error = got_error_from_errno();
591 if (error != NULL)
592 goto done;
593 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
594 error = got_error(GOT_ERR_OBJ_TYPE);
595 goto done;
598 error = got_repo_map_path(&in_repo_path, repo, path, 1);
599 if (error != NULL)
600 goto done;
601 if (in_repo_path) {
602 free(path);
603 path = in_repo_path;
606 error = print_commits(obj, id, repo, path, show_patch,
607 diff_context, limit, first_parent_traversal);
608 done:
609 free(path);
610 free(repo_path);
611 free(cwd);
612 if (obj)
613 got_object_close(obj);
614 free(id);
615 if (repo) {
616 const struct got_error *repo_error;
617 repo_error = got_repo_close(repo);
618 if (error == NULL)
619 error = repo_error;
621 return error;
624 __dead static void
625 usage_diff(void)
627 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
628 "object1 object2\n", getprogname());
629 exit(1);
632 static const struct got_error *
633 cmd_diff(int argc, char *argv[])
635 const struct got_error *error;
636 struct got_repository *repo = NULL;
637 struct got_object *obj1 = NULL, *obj2 = NULL;
638 char *repo_path = NULL;
639 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
640 int diff_context = 3, ch;
641 const char *errstr;
643 #ifndef PROFILE
644 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
645 == -1)
646 err(1, "pledge");
647 #endif
649 while ((ch = getopt(argc, argv, "C:")) != -1) {
650 switch (ch) {
651 case 'C':
652 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
653 if (errstr != NULL)
654 err(1, "-C option %s", errstr);
655 break;
656 default:
657 usage();
658 /* NOTREACHED */
662 argc -= optind;
663 argv += optind;
665 if (argc == 0) {
666 usage_diff(); /* TODO show local worktree changes */
667 } else if (argc == 2) {
668 repo_path = getcwd(NULL, 0);
669 if (repo_path == NULL)
670 return got_error_from_errno();
671 obj_id_str1 = argv[0];
672 obj_id_str2 = argv[1];
673 } else if (argc == 3) {
674 repo_path = realpath(argv[0], NULL);
675 if (repo_path == NULL)
676 return got_error_from_errno();
677 obj_id_str1 = argv[1];
678 obj_id_str2 = argv[2];
679 } else
680 usage_diff();
682 error = got_repo_open(&repo, repo_path);
683 free(repo_path);
684 if (error != NULL)
685 goto done;
687 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
688 if (error)
689 goto done;
691 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
692 if (error)
693 goto done;
695 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
696 error = got_error(GOT_ERR_OBJ_TYPE);
697 goto done;
700 switch (got_object_get_type(obj1)) {
701 case GOT_OBJ_TYPE_BLOB:
702 error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
703 diff_context, repo, stdout);
704 break;
705 case GOT_OBJ_TYPE_TREE:
706 error = got_diff_objects_as_trees(obj1, obj2, "", "",
707 diff_context, repo, stdout);
708 break;
709 case GOT_OBJ_TYPE_COMMIT:
710 printf("diff %s %s\n", obj_id_str1 ? obj_id_str1 : "/dev/null",
711 obj_id_str2);
712 error = got_diff_objects_as_commits(obj1, obj2, diff_context,
713 repo, stdout);
714 break;
715 default:
716 error = got_error(GOT_ERR_OBJ_TYPE);
719 done:
720 if (obj1)
721 got_object_close(obj1);
722 if (obj2)
723 got_object_close(obj2);
724 if (repo) {
725 const struct got_error *repo_error;
726 repo_error = got_repo_close(repo);
727 if (error == NULL)
728 error = repo_error;
730 return error;
733 __dead static void
734 usage_blame(void)
736 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
737 getprogname());
738 exit(1);
741 static const struct got_error *
742 cmd_blame(int argc, char *argv[])
744 const struct got_error *error;
745 struct got_repository *repo = NULL;
746 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
747 struct got_object_id *commit_id = NULL;
748 char *commit_id_str = NULL;
749 int ch;
751 #ifndef PROFILE
752 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
753 == -1)
754 err(1, "pledge");
755 #endif
757 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
758 switch (ch) {
759 case 'c':
760 commit_id_str = optarg;
761 break;
762 case 'r':
763 repo_path = realpath(optarg, NULL);
764 if (repo_path == NULL)
765 err(1, "-r option");
766 break;
767 default:
768 usage();
769 /* NOTREACHED */
773 argc -= optind;
774 argv += optind;
776 if (argc == 1)
777 path = argv[0];
778 else
779 usage_blame();
781 cwd = getcwd(NULL, 0);
782 if (cwd == NULL) {
783 error = got_error_from_errno();
784 goto done;
786 if (repo_path == NULL) {
787 repo_path = strdup(cwd);
788 if (repo_path == NULL) {
789 error = got_error_from_errno();
790 goto done;
794 error = got_repo_open(&repo, repo_path);
795 if (error != NULL)
796 goto done;
798 error = got_repo_map_path(&in_repo_path, repo, path, 1);
799 if (error != NULL)
800 goto done;
802 if (commit_id_str == NULL) {
803 struct got_reference *head_ref;
804 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
805 if (error != NULL)
806 goto done;
807 error = got_ref_resolve(&commit_id, repo, head_ref);
808 got_ref_close(head_ref);
809 if (error != NULL)
810 goto done;
811 } else {
812 struct got_object *obj;
813 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
814 if (error != NULL)
815 goto done;
816 commit_id = got_object_id_dup(got_object_get_id(obj));
817 if (commit_id == NULL)
818 error = got_error_from_errno();
819 got_object_close(obj);
822 error = got_blame(in_repo_path, commit_id, repo, stdout);
823 done:
824 free(in_repo_path);
825 free(repo_path);
826 free(cwd);
827 free(commit_id);
828 if (repo) {
829 const struct got_error *repo_error;
830 repo_error = got_repo_close(repo);
831 if (error == NULL)
832 error = repo_error;
834 return error;
837 __dead static void
838 usage_tree(void)
840 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
841 getprogname());
842 exit(1);
846 static const struct got_error *
847 print_tree(const char *path, struct got_object_id *commit_id,
848 int show_ids, struct got_repository *repo)
850 const struct got_error *err = NULL;
851 struct got_object_id *tree_id = NULL;
852 struct got_tree_object *tree = NULL;
853 const struct got_tree_entries *entries;
854 struct got_tree_entry *te;
856 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
857 if (err)
858 goto done;
860 err = got_object_open_as_tree(&tree, repo, tree_id);
861 if (err)
862 goto done;
863 entries = got_object_tree_get_entries(tree);
864 te = SIMPLEQ_FIRST(&entries->head);
865 while (te) {
866 char *id = NULL;
868 if (sigint_received || sigpipe_received)
869 break;
871 if (show_ids) {
872 char *id_str;
873 err = got_object_id_str(&id_str, te->id);
874 if (err)
875 goto done;
876 if (asprintf(&id, "%s ", id_str) == -1) {
877 err = got_error_from_errno();
878 free(id_str);
879 goto done;
881 free(id_str);
883 printf("%s%s%s\n", id ? id : "",
884 te->name, S_ISDIR(te->mode) ? "/" : "");
885 te = SIMPLEQ_NEXT(te, entry);
886 free(id);
888 done:
889 if (tree)
890 got_object_tree_close(tree);
891 free(tree_id);
892 return err;
895 static const struct got_error *
896 cmd_tree(int argc, char *argv[])
898 const struct got_error *error;
899 struct got_repository *repo = NULL;
900 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
901 struct got_object_id *commit_id = NULL;
902 char *commit_id_str = NULL;
903 int show_ids = 0;
904 int ch;
906 #ifndef PROFILE
907 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
908 == -1)
909 err(1, "pledge");
910 #endif
912 while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
913 switch (ch) {
914 case 'c':
915 commit_id_str = optarg;
916 break;
917 case 'r':
918 repo_path = realpath(optarg, NULL);
919 if (repo_path == NULL)
920 err(1, "-r option");
921 break;
922 case 'i':
923 show_ids = 1;
924 break;
925 default:
926 usage();
927 /* NOTREACHED */
931 argc -= optind;
932 argv += optind;
934 if (argc == 1)
935 path = argv[0];
936 else if (argc > 1)
937 usage_tree();
938 else
939 path = "/";
941 cwd = getcwd(NULL, 0);
942 if (cwd == NULL) {
943 error = got_error_from_errno();
944 goto done;
946 if (repo_path == NULL) {
947 repo_path = strdup(cwd);
948 if (repo_path == NULL) {
949 error = got_error_from_errno();
950 goto done;
954 error = got_repo_open(&repo, repo_path);
955 if (error != NULL)
956 goto done;
958 error = got_repo_map_path(&in_repo_path, repo, path, 1);
959 if (error != NULL)
960 goto done;
962 if (commit_id_str == NULL) {
963 struct got_reference *head_ref;
964 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
965 if (error != NULL)
966 goto done;
967 error = got_ref_resolve(&commit_id, repo, head_ref);
968 got_ref_close(head_ref);
969 if (error != NULL)
970 goto done;
971 } else {
972 struct got_object *obj;
973 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
974 if (error != NULL)
975 goto done;
976 commit_id = got_object_id_dup(got_object_get_id(obj));
977 if (commit_id == NULL)
978 error = got_error_from_errno();
979 got_object_close(obj);
982 error = print_tree(in_repo_path, commit_id, show_ids, repo);
983 done:
984 free(in_repo_path);
985 free(repo_path);
986 free(cwd);
987 free(commit_id);
988 if (repo) {
989 const struct got_error *repo_error;
990 repo_error = got_repo_close(repo);
991 if (error == NULL)
992 error = repo_error;
994 return error;
997 #ifdef notyet
998 static const struct got_error *
999 cmd_status(int argc __unused, char *argv[] __unused)
1001 git_repository *repo = NULL;
1002 git_status_list *status;
1003 git_status_options statusopts;
1004 size_t i;
1006 git_libgit2_init();
1008 if (git_repository_open_ext(&repo, ".", 0, NULL))
1009 errx(1, "git_repository_open: %s", giterr_last()->message);
1011 if (git_repository_is_bare(repo))
1012 errx(1, "bar repository");
1014 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
1015 errx(1, "git_status_init_options: %s", giterr_last()->message);
1017 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
1018 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
1019 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
1020 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1022 if (git_status_list_new(&status, repo, &statusopts))
1023 errx(1, "git_status_list_new: %s", giterr_last()->message);
1025 for (i = 0; i < git_status_list_entrycount(status); i++) {
1026 const git_status_entry *se;
1028 se = git_status_byindex(status, i);
1029 switch (se->status) {
1030 case GIT_STATUS_WT_NEW:
1031 printf("? %s\n", se->index_to_workdir->new_file.path);
1032 break;
1033 case GIT_STATUS_WT_MODIFIED:
1034 printf("M %s\n", se->index_to_workdir->new_file.path);
1035 break;
1036 case GIT_STATUS_WT_DELETED:
1037 printf("R %s\n", se->index_to_workdir->new_file.path);
1038 break;
1039 case GIT_STATUS_WT_RENAMED:
1040 printf("m %s -> %s\n",
1041 se->index_to_workdir->old_file.path,
1042 se->index_to_workdir->new_file.path);
1043 break;
1044 case GIT_STATUS_WT_TYPECHANGE:
1045 printf("t %s\n", se->index_to_workdir->new_file.path);
1046 break;
1047 case GIT_STATUS_INDEX_NEW:
1048 printf("A %s\n", se->head_to_index->new_file.path);
1049 break;
1050 case GIT_STATUS_INDEX_MODIFIED:
1051 printf("M %s\n", se->head_to_index->old_file.path);
1052 break;
1053 case GIT_STATUS_INDEX_DELETED:
1054 printf("R %s\n", se->head_to_index->old_file.path);
1055 break;
1056 case GIT_STATUS_INDEX_RENAMED:
1057 printf("m %s -> %s\n",
1058 se->head_to_index->old_file.path,
1059 se->head_to_index->new_file.path);
1060 break;
1061 case GIT_STATUS_INDEX_TYPECHANGE:
1062 printf("t %s\n", se->head_to_index->old_file.path);
1063 break;
1064 case GIT_STATUS_CURRENT:
1065 default:
1066 break;
1070 git_status_list_free(status);
1071 git_repository_free(repo);
1072 git_libgit2_shutdown();
1074 return 0;
1076 #endif