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;
307 time_t time1 = 0, time2;
309 time2 = got_object_commit_get_committer_time(commit);
311 err = got_object_open_as_tree(&tree2, repo,
312 got_object_commit_get_tree_id(commit));
313 if (err)
314 return err;
316 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
317 if (qid != NULL) {
318 struct got_commit_object *pcommit;
320 err = got_object_open_as_commit(&pcommit, repo, qid->id);
321 if (err)
322 return err;
324 time1 = got_object_commit_get_committer_time(pcommit);
325 err = got_object_open_as_tree(&tree1, repo,
326 got_object_commit_get_tree_id(pcommit));
327 got_object_commit_close(pcommit);
328 if (err)
329 return err;
331 err = got_object_id_str(&id_str1, qid->id);
332 if (err)
333 return err;
336 err = got_object_id_str(&id_str2, id);
337 if (err)
338 goto done;
340 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
341 err = got_diff_tree(tree1, tree2, "", "", time1, time2, diff_context,
342 repo, stdout);
343 done:
344 if (tree1)
345 got_object_tree_close(tree1);
346 got_object_tree_close(tree2);
347 free(id_str1);
348 free(id_str2);
349 return err;
352 static char *
353 get_datestr(time_t *time, char *datebuf)
355 char *p, *s = ctime_r(time, datebuf);
356 p = strchr(s, '\n');
357 if (p)
358 *p = '\0';
359 return s;
362 static const struct got_error *
363 print_commit(struct got_commit_object *commit, struct got_object_id *id,
364 struct got_repository *repo, int show_patch, int diff_context)
366 const struct got_error *err = NULL;
367 char *id_str, *datestr, *logmsg0, *logmsg, *line;
368 char datebuf[26];
369 time_t committer_time;
370 const char *author, *committer;
372 err = got_object_id_str(&id_str, id);
373 if (err)
374 return err;
376 printf("-----------------------------------------------\n");
377 printf("commit %s\n", id_str);
378 free(id_str);
379 printf("from: %s\n", got_object_commit_get_author(commit));
380 committer_time = got_object_commit_get_committer_time(commit);
381 datestr = get_datestr(&committer_time, datebuf);
382 printf("date: %s UTC\n", datestr);
383 author = got_object_commit_get_author(commit);
384 committer = got_object_commit_get_committer(commit);
385 if (strcmp(author, committer) != 0)
386 printf("via: %s\n", committer);
387 if (got_object_commit_get_nparents(commit) > 1) {
388 const struct got_object_id_queue *parent_ids;
389 struct got_object_qid *qid;
390 int n = 1;
391 parent_ids = got_object_commit_get_parent_ids(commit);
392 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
393 err = got_object_id_str(&id_str, qid->id);
394 if (err)
395 return err;
396 printf("parent %d: %s\n", n++, id_str);
397 free(id_str);
401 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
402 if (logmsg0 == NULL)
403 return got_error_from_errno();
405 logmsg = logmsg0;
406 do {
407 line = strsep(&logmsg, "\n");
408 if (line)
409 printf(" %s\n", line);
410 } while (line);
411 free(logmsg0);
413 if (show_patch) {
414 err = print_patch(commit, id, diff_context, repo);
415 if (err == 0)
416 printf("\n");
419 fflush(stdout);
420 return err;
423 static const struct got_error *
424 print_commits(struct got_object_id *root_id, struct got_repository *repo,
425 char *path, int show_patch, int diff_context, int limit,
426 int first_parent_traversal)
428 const struct got_error *err;
429 struct got_commit_graph *graph;
431 err = got_commit_graph_open(&graph, root_id, path,
432 first_parent_traversal, repo);
433 if (err)
434 return err;
435 err = got_commit_graph_iter_start(graph, root_id, repo);
436 if (err)
437 goto done;
438 while (1) {
439 struct got_commit_object *commit;
440 struct got_object_id *id;
442 if (sigint_received || sigpipe_received)
443 break;
445 err = got_commit_graph_iter_next(&id, graph);
446 if (err) {
447 if (err->code == GOT_ERR_ITER_COMPLETED) {
448 err = NULL;
449 break;
451 if (err->code != GOT_ERR_ITER_NEED_MORE)
452 break;
453 err = got_commit_graph_fetch_commits(graph, 1, repo);
454 if (err)
455 break;
456 else
457 continue;
459 if (id == NULL)
460 break;
462 err = got_object_open_as_commit(&commit, repo, id);
463 if (err)
464 break;
465 err = print_commit(commit, id, repo, show_patch, diff_context);
466 got_object_commit_close(commit);
467 if (err || (limit && --limit == 0))
468 break;
470 done:
471 got_commit_graph_close(graph);
472 return err;
475 __dead static void
476 usage_log(void)
478 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
479 "[-r repository-path] [path]\n", getprogname());
480 exit(1);
483 static const struct got_error *
484 cmd_log(int argc, char *argv[])
486 const struct got_error *error;
487 struct got_repository *repo = NULL;
488 struct got_commit_object *commit = NULL;
489 struct got_object_id *id = NULL;
490 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
491 char *start_commit = NULL;
492 int diff_context = 3, ch;
493 int show_patch = 0, limit = 0, first_parent_traversal = 0;
494 const char *errstr;
496 #ifndef PROFILE
497 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
498 == -1)
499 err(1, "pledge");
500 #endif
502 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
503 switch (ch) {
504 case 'p':
505 show_patch = 1;
506 break;
507 case 'c':
508 start_commit = optarg;
509 break;
510 case 'C':
511 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
512 &errstr);
513 if (errstr != NULL)
514 err(1, "-C option %s", errstr);
515 break;
516 case 'l':
517 limit = strtonum(optarg, 1, INT_MAX, &errstr);
518 if (errstr != NULL)
519 err(1, "-l option %s", errstr);
520 break;
521 case 'f':
522 first_parent_traversal = 1;
523 break;
524 case 'r':
525 repo_path = realpath(optarg, NULL);
526 if (repo_path == NULL)
527 err(1, "-r option");
528 break;
529 default:
530 usage();
531 /* NOTREACHED */
535 argc -= optind;
536 argv += optind;
538 if (argc == 0)
539 path = strdup("");
540 else if (argc == 1)
541 path = strdup(argv[0]);
542 else
543 usage_log();
544 if (path == NULL)
545 return got_error_from_errno();
547 cwd = getcwd(NULL, 0);
548 if (cwd == NULL) {
549 error = got_error_from_errno();
550 goto done;
552 if (repo_path == NULL) {
553 repo_path = strdup(cwd);
554 if (repo_path == NULL) {
555 error = got_error_from_errno();
556 goto done;
560 error = got_repo_open(&repo, repo_path);
561 if (error != NULL)
562 goto done;
564 if (start_commit == NULL) {
565 struct got_reference *head_ref;
566 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
567 if (error != NULL)
568 return error;
569 error = got_ref_resolve(&id, repo, head_ref);
570 got_ref_close(head_ref);
571 if (error != NULL)
572 return error;
573 error = got_object_open_as_commit(&commit, repo, id);
574 } else {
575 struct got_reference *ref;
576 error = got_ref_open(&ref, repo, start_commit);
577 if (error == NULL) {
578 error = got_ref_resolve(&id, repo, ref);
579 got_ref_close(ref);
580 if (error != NULL)
581 return error;
582 error = got_object_open_as_commit(&commit, repo, id);
583 if (error != NULL)
584 return error;
586 if (commit == NULL) {
587 error = got_object_resolve_id_str(&id, repo,
588 start_commit);
589 if (error != NULL)
590 return error;
593 if (error != NULL)
594 goto done;
596 error = got_repo_map_path(&in_repo_path, repo, path, 1);
597 if (error != NULL)
598 goto done;
599 if (in_repo_path) {
600 free(path);
601 path = in_repo_path;
604 error = print_commits(id, repo, path, show_patch,
605 diff_context, limit, first_parent_traversal);
606 done:
607 free(path);
608 free(repo_path);
609 free(cwd);
610 free(id);
611 if (repo) {
612 const struct got_error *repo_error;
613 repo_error = got_repo_close(repo);
614 if (error == NULL)
615 error = repo_error;
617 return error;
620 __dead static void
621 usage_diff(void)
623 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
624 "object1 object2\n", getprogname());
625 exit(1);
628 static const struct got_error *
629 cmd_diff(int argc, char *argv[])
631 const struct got_error *error;
632 struct got_repository *repo = NULL;
633 char *repo_path = NULL;
634 struct got_object_id *id1 = NULL, *id2 = NULL;
635 char *id_str1 = NULL, *id_str2 = NULL;
636 int type1, type2;
637 int diff_context = 3, ch;
638 const char *errstr;
640 #ifndef PROFILE
641 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
642 == -1)
643 err(1, "pledge");
644 #endif
646 while ((ch = getopt(argc, argv, "C:")) != -1) {
647 switch (ch) {
648 case 'C':
649 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
650 if (errstr != NULL)
651 err(1, "-C option %s", errstr);
652 break;
653 default:
654 usage();
655 /* NOTREACHED */
659 argc -= optind;
660 argv += optind;
662 if (argc == 0) {
663 usage_diff(); /* TODO show local worktree changes */
664 } else if (argc == 2) {
665 repo_path = getcwd(NULL, 0);
666 if (repo_path == NULL)
667 return got_error_from_errno();
668 id_str1 = argv[0];
669 id_str2 = argv[1];
670 } else if (argc == 3) {
671 repo_path = realpath(argv[0], NULL);
672 if (repo_path == NULL)
673 return got_error_from_errno();
674 id_str1 = argv[1];
675 id_str2 = argv[2];
676 } else
677 usage_diff();
679 error = got_repo_open(&repo, repo_path);
680 free(repo_path);
681 if (error != NULL)
682 goto done;
684 error = got_object_resolve_id_str(&id1, repo, id_str1);
685 if (error)
686 goto done;
688 error = got_object_resolve_id_str(&id2, repo, id_str2);
689 if (error)
690 goto done;
692 error = got_object_get_type(&type1, repo, id1);
693 if (error)
694 goto done;
696 error = got_object_get_type(&type2, repo, id2);
697 if (error)
698 goto done;
700 if (type1 != type2) {
701 error = got_error(GOT_ERR_OBJ_TYPE);
702 goto done;
705 switch (type1) {
706 case GOT_OBJ_TYPE_BLOB:
707 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
708 0, 0, diff_context, repo, stdout);
709 break;
710 case GOT_OBJ_TYPE_TREE:
711 error = got_diff_objects_as_trees(id1, id2, "", "",
712 0, 0, diff_context, repo, stdout);
713 break;
714 case GOT_OBJ_TYPE_COMMIT:
715 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
716 id_str2);
717 error = got_diff_objects_as_commits(id1, id2, diff_context,
718 repo, stdout);
719 break;
720 default:
721 error = got_error(GOT_ERR_OBJ_TYPE);
724 done:
725 free(id1);
726 free(id2);
727 if (repo) {
728 const struct got_error *repo_error;
729 repo_error = got_repo_close(repo);
730 if (error == NULL)
731 error = repo_error;
733 return error;
736 __dead static void
737 usage_blame(void)
739 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
740 getprogname());
741 exit(1);
744 static const struct got_error *
745 cmd_blame(int argc, char *argv[])
747 const struct got_error *error;
748 struct got_repository *repo = NULL;
749 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
750 struct got_object_id *commit_id = NULL;
751 char *commit_id_str = NULL;
752 int ch;
754 #ifndef PROFILE
755 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
756 == -1)
757 err(1, "pledge");
758 #endif
760 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
761 switch (ch) {
762 case 'c':
763 commit_id_str = optarg;
764 break;
765 case 'r':
766 repo_path = realpath(optarg, NULL);
767 if (repo_path == NULL)
768 err(1, "-r option");
769 break;
770 default:
771 usage();
772 /* NOTREACHED */
776 argc -= optind;
777 argv += optind;
779 if (argc == 1)
780 path = argv[0];
781 else
782 usage_blame();
784 cwd = getcwd(NULL, 0);
785 if (cwd == NULL) {
786 error = got_error_from_errno();
787 goto done;
789 if (repo_path == NULL) {
790 repo_path = strdup(cwd);
791 if (repo_path == NULL) {
792 error = got_error_from_errno();
793 goto done;
797 error = got_repo_open(&repo, repo_path);
798 if (error != NULL)
799 goto done;
801 error = got_repo_map_path(&in_repo_path, repo, path, 1);
802 if (error != NULL)
803 goto done;
805 if (commit_id_str == NULL) {
806 struct got_reference *head_ref;
807 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
808 if (error != NULL)
809 goto done;
810 error = got_ref_resolve(&commit_id, repo, head_ref);
811 got_ref_close(head_ref);
812 if (error != NULL)
813 goto done;
814 } else {
815 error = got_object_resolve_id_str(&commit_id, repo,
816 commit_id_str);
817 if (error != NULL)
818 goto done;
821 error = got_blame(in_repo_path, commit_id, repo, stdout);
822 done:
823 free(in_repo_path);
824 free(repo_path);
825 free(cwd);
826 free(commit_id);
827 if (repo) {
828 const struct got_error *repo_error;
829 repo_error = got_repo_close(repo);
830 if (error == NULL)
831 error = repo_error;
833 return error;
836 __dead static void
837 usage_tree(void)
839 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
840 getprogname());
841 exit(1);
845 static const struct got_error *
846 print_tree(const char *path, struct got_object_id *commit_id,
847 int show_ids, struct got_repository *repo)
849 const struct got_error *err = NULL;
850 struct got_object_id *tree_id = NULL;
851 struct got_tree_object *tree = NULL;
852 const struct got_tree_entries *entries;
853 struct got_tree_entry *te;
855 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
856 if (err)
857 goto done;
859 err = got_object_open_as_tree(&tree, repo, tree_id);
860 if (err)
861 goto done;
862 entries = got_object_tree_get_entries(tree);
863 te = SIMPLEQ_FIRST(&entries->head);
864 while (te) {
865 char *id = NULL;
867 if (sigint_received || sigpipe_received)
868 break;
870 if (show_ids) {
871 char *id_str;
872 err = got_object_id_str(&id_str, te->id);
873 if (err)
874 goto done;
875 if (asprintf(&id, "%s ", id_str) == -1) {
876 err = got_error_from_errno();
877 free(id_str);
878 goto done;
880 free(id_str);
882 printf("%s%s%s\n", id ? id : "",
883 te->name, S_ISDIR(te->mode) ? "/" : "");
884 te = SIMPLEQ_NEXT(te, entry);
885 free(id);
887 done:
888 if (tree)
889 got_object_tree_close(tree);
890 free(tree_id);
891 return err;
894 static const struct got_error *
895 cmd_tree(int argc, char *argv[])
897 const struct got_error *error;
898 struct got_repository *repo = NULL;
899 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
900 struct got_object_id *commit_id = NULL;
901 char *commit_id_str = NULL;
902 int show_ids = 0;
903 int ch;
905 #ifndef PROFILE
906 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
907 == -1)
908 err(1, "pledge");
909 #endif
911 while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
912 switch (ch) {
913 case 'c':
914 commit_id_str = optarg;
915 break;
916 case 'r':
917 repo_path = realpath(optarg, NULL);
918 if (repo_path == NULL)
919 err(1, "-r option");
920 break;
921 case 'i':
922 show_ids = 1;
923 break;
924 default:
925 usage();
926 /* NOTREACHED */
930 argc -= optind;
931 argv += optind;
933 if (argc == 1)
934 path = argv[0];
935 else if (argc > 1)
936 usage_tree();
937 else
938 path = "/";
940 cwd = getcwd(NULL, 0);
941 if (cwd == NULL) {
942 error = got_error_from_errno();
943 goto done;
945 if (repo_path == NULL) {
946 repo_path = strdup(cwd);
947 if (repo_path == NULL) {
948 error = got_error_from_errno();
949 goto done;
953 error = got_repo_open(&repo, repo_path);
954 if (error != NULL)
955 goto done;
957 error = got_repo_map_path(&in_repo_path, repo, path, 1);
958 if (error != NULL)
959 goto done;
961 if (commit_id_str == NULL) {
962 struct got_reference *head_ref;
963 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
964 if (error != NULL)
965 goto done;
966 error = got_ref_resolve(&commit_id, repo, head_ref);
967 got_ref_close(head_ref);
968 if (error != NULL)
969 goto done;
970 } else {
971 error = got_object_resolve_id_str(&commit_id, repo,
972 commit_id_str);
973 if (error != NULL)
974 goto done;
977 error = print_tree(in_repo_path, commit_id, show_ids, repo);
978 done:
979 free(in_repo_path);
980 free(repo_path);
981 free(cwd);
982 free(commit_id);
983 if (repo) {
984 const struct got_error *repo_error;
985 repo_error = got_repo_close(repo);
986 if (error == NULL)
987 error = repo_error;
989 return error;
992 #ifdef notyet
993 static const struct got_error *
994 cmd_status(int argc __unused, char *argv[] __unused)
996 git_repository *repo = NULL;
997 git_status_list *status;
998 git_status_options statusopts;
999 size_t i;
1001 git_libgit2_init();
1003 if (git_repository_open_ext(&repo, ".", 0, NULL))
1004 errx(1, "git_repository_open: %s", giterr_last()->message);
1006 if (git_repository_is_bare(repo))
1007 errx(1, "bar repository");
1009 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
1010 errx(1, "git_status_init_options: %s", giterr_last()->message);
1012 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
1013 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
1014 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
1015 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1017 if (git_status_list_new(&status, repo, &statusopts))
1018 errx(1, "git_status_list_new: %s", giterr_last()->message);
1020 for (i = 0; i < git_status_list_entrycount(status); i++) {
1021 const git_status_entry *se;
1023 se = git_status_byindex(status, i);
1024 switch (se->status) {
1025 case GIT_STATUS_WT_NEW:
1026 printf("? %s\n", se->index_to_workdir->new_file.path);
1027 break;
1028 case GIT_STATUS_WT_MODIFIED:
1029 printf("M %s\n", se->index_to_workdir->new_file.path);
1030 break;
1031 case GIT_STATUS_WT_DELETED:
1032 printf("R %s\n", se->index_to_workdir->new_file.path);
1033 break;
1034 case GIT_STATUS_WT_RENAMED:
1035 printf("m %s -> %s\n",
1036 se->index_to_workdir->old_file.path,
1037 se->index_to_workdir->new_file.path);
1038 break;
1039 case GIT_STATUS_WT_TYPECHANGE:
1040 printf("t %s\n", se->index_to_workdir->new_file.path);
1041 break;
1042 case GIT_STATUS_INDEX_NEW:
1043 printf("A %s\n", se->head_to_index->new_file.path);
1044 break;
1045 case GIT_STATUS_INDEX_MODIFIED:
1046 printf("M %s\n", se->head_to_index->old_file.path);
1047 break;
1048 case GIT_STATUS_INDEX_DELETED:
1049 printf("R %s\n", se->head_to_index->old_file.path);
1050 break;
1051 case GIT_STATUS_INDEX_RENAMED:
1052 printf("m %s -> %s\n",
1053 se->head_to_index->old_file.path,
1054 se->head_to_index->new_file.path);
1055 break;
1056 case GIT_STATUS_INDEX_TYPECHANGE:
1057 printf("t %s\n", se->head_to_index->old_file.path);
1058 break;
1059 case GIT_STATUS_CURRENT:
1060 default:
1061 break;
1065 git_status_list_free(status);
1066 git_repository_free(repo);
1067 git_libgit2_shutdown();
1069 return 0;
1071 #endif