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_update(void);
73 __dead static void usage_log(void);
74 __dead static void usage_diff(void);
75 __dead static void usage_blame(void);
76 __dead static void usage_tree(void);
78 static const struct got_error* cmd_checkout(int, char *[]);
79 static const struct got_error* cmd_update(int, char *[]);
80 static const struct got_error* cmd_log(int, char *[]);
81 static const struct got_error* cmd_diff(int, char *[]);
82 static const struct got_error* cmd_blame(int, char *[]);
83 static const struct got_error* cmd_tree(int, char *[]);
84 #ifdef notyet
85 static const struct got_error* cmd_status(int, char *[]);
86 #endif
88 static struct cmd got_commands[] = {
89 { "checkout", cmd_checkout, usage_checkout,
90 "check out a new work tree from a repository" },
91 { "update", cmd_update, usage_update,
92 "update a work tree to a different commit" },
93 { "log", cmd_log, usage_log,
94 "show repository history" },
95 { "diff", cmd_diff, usage_diff,
96 "compare files and directories" },
97 { "blame", cmd_blame, usage_blame,
98 " show when lines in a file were changed" },
99 { "tree", cmd_tree, usage_tree,
100 " list files and directories in repository" },
101 #ifdef notyet
102 { "status", cmd_status, usage_status,
103 "show modification status of files" },
104 #endif
105 };
107 int
108 main(int argc, char *argv[])
110 struct cmd *cmd;
111 unsigned int i;
112 int ch;
113 int hflag = 0;
115 setlocale(LC_ALL, "");
117 while ((ch = getopt(argc, argv, "h")) != -1) {
118 switch (ch) {
119 case 'h':
120 hflag = 1;
121 break;
122 default:
123 usage();
124 /* NOTREACHED */
128 argc -= optind;
129 argv += optind;
130 optind = 0;
132 if (argc <= 0)
133 usage();
135 signal(SIGINT, catch_sigint);
136 signal(SIGPIPE, catch_sigpipe);
138 for (i = 0; i < nitems(got_commands); i++) {
139 const struct got_error *error;
141 cmd = &got_commands[i];
143 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
144 continue;
146 if (hflag)
147 got_commands[i].cmd_usage();
149 error = got_commands[i].cmd_main(argc, argv);
150 if (error && !(sigint_received || sigpipe_received)) {
151 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
152 return 1;
155 return 0;
158 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
159 return 1;
162 __dead static void
163 usage(void)
165 int i;
167 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
168 "Available commands:\n", getprogname());
169 for (i = 0; i < nitems(got_commands); i++) {
170 struct cmd *cmd = &got_commands[i];
171 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
173 exit(1);
176 __dead static void
177 usage_checkout(void)
179 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
180 "[worktree-path]\n", getprogname());
181 exit(1);
184 static void
185 checkout_progress(void *arg, unsigned char status, const char *path)
187 char *worktree_path = arg;
189 while (path[0] == '/')
190 path++;
192 printf("%c %s/%s\n", status, worktree_path, path);
195 static const struct got_error *
196 checkout_cancel(void *arg)
198 if (sigint_received || sigpipe_received)
199 return got_error(GOT_ERR_CANCELLED);
200 return NULL;
203 static const struct got_error *
204 cmd_checkout(int argc, char *argv[])
206 const struct got_error *error = NULL;
207 struct got_repository *repo = NULL;
208 struct got_reference *head_ref = NULL;
209 struct got_worktree *worktree = NULL;
210 char *repo_path = NULL;
211 char *worktree_path = NULL;
212 const char *path_prefix = "";
213 int ch, same_path_prefix;
215 while ((ch = getopt(argc, argv, "p:")) != -1) {
216 switch (ch) {
217 case 'p':
218 path_prefix = optarg;
219 break;
220 default:
221 usage();
222 /* NOTREACHED */
226 argc -= optind;
227 argv += optind;
229 #ifndef PROFILE
230 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
231 == -1)
232 err(1, "pledge");
233 #endif
234 if (argc == 1) {
235 char *cwd, *base, *dotgit;
236 repo_path = realpath(argv[0], NULL);
237 if (repo_path == NULL)
238 return got_error_from_errno();
239 cwd = getcwd(NULL, 0);
240 if (cwd == NULL) {
241 error = got_error_from_errno();
242 goto done;
244 if (path_prefix[0])
245 base = basename(path_prefix);
246 else
247 base = basename(repo_path);
248 if (base == NULL) {
249 error = got_error_from_errno();
250 goto done;
252 dotgit = strstr(base, ".git");
253 if (dotgit)
254 *dotgit = '\0';
255 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
256 error = got_error_from_errno();
257 free(cwd);
258 goto done;
260 free(cwd);
261 } else if (argc == 2) {
262 repo_path = realpath(argv[0], NULL);
263 if (repo_path == NULL) {
264 error = got_error_from_errno();
265 goto done;
267 worktree_path = realpath(argv[1], NULL);
268 if (worktree_path == NULL) {
269 error = got_error_from_errno();
270 goto done;
272 } else
273 usage_checkout();
275 error = got_repo_open(&repo, repo_path);
276 if (error != NULL)
277 goto done;
278 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
279 if (error != NULL)
280 goto done;
282 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
283 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
284 goto done;
286 error = got_worktree_open(&worktree, worktree_path);
287 if (error != NULL)
288 goto done;
290 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
291 path_prefix);
292 if (error != NULL)
293 goto done;
294 if (!same_path_prefix) {
295 error = got_error(GOT_ERR_PATH_PREFIX);
296 goto done;
299 error = got_worktree_checkout_files(worktree, repo,
300 checkout_progress, worktree_path, checkout_cancel, NULL);
301 if (error != NULL)
302 goto done;
304 printf("Now shut up and hack\n");
306 done:
307 free(repo_path);
308 free(worktree_path);
309 return error;
312 __dead static void
313 usage_update(void)
315 fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
316 getprogname());
317 exit(1);
320 static void
321 update_progress(void *arg, unsigned char status, const char *path)
323 if (status == GOT_STATUS_EXISTS)
324 return;
326 while (path[0] == '/')
327 path++;
328 printf("%c %s\n", status, path);
331 static const struct got_error *
332 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
333 struct got_repository *repo)
335 const struct got_error *err;
336 struct got_reference *head_ref = NULL;
337 struct got_object_id *head_commit_id = NULL;
338 struct got_commit_graph *graph = NULL;
340 head_ref = got_worktree_get_head_ref(worktree);
341 if (head_ref == NULL)
342 return got_error_from_errno();
344 err = got_ref_resolve(&head_commit_id, repo, head_ref);
345 if (err)
346 goto done;
348 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
349 if (err)
350 goto done;
352 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
353 if (err)
354 goto done;
355 while (1) {
356 struct got_object_id *id;
358 if (sigint_received || sigpipe_received)
359 break;
361 err = got_commit_graph_iter_next(&id, graph);
362 if (err) {
363 if (err->code == GOT_ERR_ITER_COMPLETED) {
364 err = got_error(GOT_ERR_ANCESTRY);
365 break;
367 if (err->code != GOT_ERR_ITER_NEED_MORE)
368 break;
369 err = got_commit_graph_fetch_commits(graph, 1, repo);
370 if (err)
371 break;
372 else
373 continue;
375 if (id == NULL)
376 break;
377 if (got_object_id_cmp(id, commit_id) == 0)
378 break;
380 done:
381 if (head_ref)
382 got_ref_close(head_ref);
383 if (graph)
384 got_commit_graph_close(graph);
385 return err;
388 static const struct got_error *
389 cmd_update(int argc, char *argv[])
391 const struct got_error *error = NULL;
392 struct got_repository *repo = NULL;
393 struct got_worktree *worktree = NULL;
394 char *worktree_path = NULL;
395 struct got_object_id *commit_id = NULL;
396 const char *commit_id_str = NULL;
397 int ch;
399 while ((ch = getopt(argc, argv, "c:")) != -1) {
400 switch (ch) {
401 case 'c':
402 commit_id_str = optarg;
403 break;
404 default:
405 usage();
406 /* NOTREACHED */
410 argc -= optind;
411 argv += optind;
413 #ifndef PROFILE
414 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
415 == -1)
416 err(1, "pledge");
417 #endif
418 if (argc == 0) {
419 worktree_path = getcwd(NULL, 0);
420 if (worktree_path == NULL) {
421 error = got_error_from_errno();
422 goto done;
424 } else if (argc == 1) {
425 worktree_path = realpath(argv[0], NULL);
426 if (worktree_path == NULL) {
427 error = got_error_from_errno();
428 goto done;
430 } else
431 usage_update();
433 error = got_worktree_open(&worktree, worktree_path);
434 if (error != NULL)
435 goto done;
437 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
438 if (error != NULL)
439 goto done;
441 if (commit_id_str == NULL) {
442 struct got_reference *head_ref;
443 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
444 if (error != NULL)
445 goto done;
446 error = got_ref_resolve(&commit_id, repo, head_ref);
447 if (error != NULL)
448 goto done;
449 } else {
450 error = got_object_resolve_id_str(&commit_id, repo,
451 commit_id_str);
452 if (error != NULL)
453 goto done;
456 error = check_ancestry(worktree, commit_id, repo);
457 if (error != NULL)
458 goto done;
460 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
461 commit_id) != 0) {
462 error = got_worktree_set_base_commit_id(worktree, repo,
463 commit_id);
464 if (error)
465 goto done;
468 error = got_worktree_checkout_files(worktree, repo,
469 update_progress, NULL, checkout_cancel, NULL);
470 if (error != NULL)
471 goto done;
472 done:
473 free(worktree_path);
474 free(commit_id);
475 return error;
478 static const struct got_error *
479 print_patch(struct got_commit_object *commit, struct got_object_id *id,
480 int diff_context, struct got_repository *repo)
482 const struct got_error *err = NULL;
483 struct got_tree_object *tree1 = NULL, *tree2;
484 struct got_object_qid *qid;
485 char *id_str1 = NULL, *id_str2;
487 err = got_object_open_as_tree(&tree2, repo,
488 got_object_commit_get_tree_id(commit));
489 if (err)
490 return err;
492 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
493 if (qid != NULL) {
494 struct got_commit_object *pcommit;
496 err = got_object_open_as_commit(&pcommit, repo, qid->id);
497 if (err)
498 return err;
500 err = got_object_open_as_tree(&tree1, repo,
501 got_object_commit_get_tree_id(pcommit));
502 got_object_commit_close(pcommit);
503 if (err)
504 return err;
506 err = got_object_id_str(&id_str1, qid->id);
507 if (err)
508 return err;
511 err = got_object_id_str(&id_str2, id);
512 if (err)
513 goto done;
515 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
516 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
517 done:
518 if (tree1)
519 got_object_tree_close(tree1);
520 got_object_tree_close(tree2);
521 free(id_str1);
522 free(id_str2);
523 return err;
526 static char *
527 get_datestr(time_t *time, char *datebuf)
529 char *p, *s = ctime_r(time, datebuf);
530 p = strchr(s, '\n');
531 if (p)
532 *p = '\0';
533 return s;
536 static const struct got_error *
537 print_commit(struct got_commit_object *commit, struct got_object_id *id,
538 struct got_repository *repo, int show_patch, int diff_context)
540 const struct got_error *err = NULL;
541 char *id_str, *datestr, *logmsg0, *logmsg, *line;
542 char datebuf[26];
543 time_t committer_time;
544 const char *author, *committer;
546 err = got_object_id_str(&id_str, id);
547 if (err)
548 return err;
550 printf("-----------------------------------------------\n");
551 printf("commit %s\n", id_str);
552 free(id_str);
553 printf("from: %s\n", got_object_commit_get_author(commit));
554 committer_time = got_object_commit_get_committer_time(commit);
555 datestr = get_datestr(&committer_time, datebuf);
556 printf("date: %s UTC\n", datestr);
557 author = got_object_commit_get_author(commit);
558 committer = got_object_commit_get_committer(commit);
559 if (strcmp(author, committer) != 0)
560 printf("via: %s\n", committer);
561 if (got_object_commit_get_nparents(commit) > 1) {
562 const struct got_object_id_queue *parent_ids;
563 struct got_object_qid *qid;
564 int n = 1;
565 parent_ids = got_object_commit_get_parent_ids(commit);
566 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
567 err = got_object_id_str(&id_str, qid->id);
568 if (err)
569 return err;
570 printf("parent %d: %s\n", n++, id_str);
571 free(id_str);
575 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
576 if (logmsg0 == NULL)
577 return got_error_from_errno();
579 logmsg = logmsg0;
580 do {
581 line = strsep(&logmsg, "\n");
582 if (line)
583 printf(" %s\n", line);
584 } while (line);
585 free(logmsg0);
587 if (show_patch) {
588 err = print_patch(commit, id, diff_context, repo);
589 if (err == 0)
590 printf("\n");
593 fflush(stdout);
594 return err;
597 static const struct got_error *
598 print_commits(struct got_object_id *root_id, struct got_repository *repo,
599 char *path, int show_patch, int diff_context, int limit,
600 int first_parent_traversal)
602 const struct got_error *err;
603 struct got_commit_graph *graph;
605 err = got_commit_graph_open(&graph, root_id, path,
606 first_parent_traversal, repo);
607 if (err)
608 return err;
609 err = got_commit_graph_iter_start(graph, root_id, repo);
610 if (err)
611 goto done;
612 while (1) {
613 struct got_commit_object *commit;
614 struct got_object_id *id;
616 if (sigint_received || sigpipe_received)
617 break;
619 err = got_commit_graph_iter_next(&id, graph);
620 if (err) {
621 if (err->code == GOT_ERR_ITER_COMPLETED) {
622 err = NULL;
623 break;
625 if (err->code != GOT_ERR_ITER_NEED_MORE)
626 break;
627 err = got_commit_graph_fetch_commits(graph, 1, repo);
628 if (err)
629 break;
630 else
631 continue;
633 if (id == NULL)
634 break;
636 err = got_object_open_as_commit(&commit, repo, id);
637 if (err)
638 break;
639 err = print_commit(commit, id, repo, show_patch, diff_context);
640 got_object_commit_close(commit);
641 if (err || (limit && --limit == 0))
642 break;
644 done:
645 got_commit_graph_close(graph);
646 return err;
649 __dead static void
650 usage_log(void)
652 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
653 "[-r repository-path] [path]\n", getprogname());
654 exit(1);
657 static const struct got_error *
658 cmd_log(int argc, char *argv[])
660 const struct got_error *error;
661 struct got_repository *repo = NULL;
662 struct got_commit_object *commit = NULL;
663 struct got_object_id *id = NULL;
664 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
665 char *start_commit = NULL;
666 int diff_context = 3, ch;
667 int show_patch = 0, limit = 0, first_parent_traversal = 0;
668 const char *errstr;
670 #ifndef PROFILE
671 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
672 == -1)
673 err(1, "pledge");
674 #endif
676 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
677 switch (ch) {
678 case 'p':
679 show_patch = 1;
680 break;
681 case 'c':
682 start_commit = optarg;
683 break;
684 case 'C':
685 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
686 &errstr);
687 if (errstr != NULL)
688 err(1, "-C option %s", errstr);
689 break;
690 case 'l':
691 limit = strtonum(optarg, 1, INT_MAX, &errstr);
692 if (errstr != NULL)
693 err(1, "-l option %s", errstr);
694 break;
695 case 'f':
696 first_parent_traversal = 1;
697 break;
698 case 'r':
699 repo_path = realpath(optarg, NULL);
700 if (repo_path == NULL)
701 err(1, "-r option");
702 break;
703 default:
704 usage();
705 /* NOTREACHED */
709 argc -= optind;
710 argv += optind;
712 if (argc == 0)
713 path = strdup("");
714 else if (argc == 1)
715 path = strdup(argv[0]);
716 else
717 usage_log();
718 if (path == NULL)
719 return got_error_from_errno();
721 cwd = getcwd(NULL, 0);
722 if (cwd == NULL) {
723 error = got_error_from_errno();
724 goto done;
726 if (repo_path == NULL) {
727 repo_path = strdup(cwd);
728 if (repo_path == NULL) {
729 error = got_error_from_errno();
730 goto done;
734 error = got_repo_open(&repo, repo_path);
735 if (error != NULL)
736 goto done;
738 if (start_commit == NULL) {
739 struct got_reference *head_ref;
740 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
741 if (error != NULL)
742 return error;
743 error = got_ref_resolve(&id, repo, head_ref);
744 got_ref_close(head_ref);
745 if (error != NULL)
746 return error;
747 error = got_object_open_as_commit(&commit, repo, id);
748 } else {
749 struct got_reference *ref;
750 error = got_ref_open(&ref, repo, start_commit);
751 if (error == NULL) {
752 error = got_ref_resolve(&id, repo, ref);
753 got_ref_close(ref);
754 if (error != NULL)
755 return error;
756 error = got_object_open_as_commit(&commit, repo, id);
757 if (error != NULL)
758 return error;
760 if (commit == NULL) {
761 error = got_object_resolve_id_str(&id, repo,
762 start_commit);
763 if (error != NULL)
764 return error;
767 if (error != NULL)
768 goto done;
770 error = got_repo_map_path(&in_repo_path, repo, path, 1);
771 if (error != NULL)
772 goto done;
773 if (in_repo_path) {
774 free(path);
775 path = in_repo_path;
778 error = print_commits(id, repo, path, show_patch,
779 diff_context, limit, first_parent_traversal);
780 done:
781 free(path);
782 free(repo_path);
783 free(cwd);
784 free(id);
785 if (repo) {
786 const struct got_error *repo_error;
787 repo_error = got_repo_close(repo);
788 if (error == NULL)
789 error = repo_error;
791 return error;
794 __dead static void
795 usage_diff(void)
797 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
798 "object1 object2\n", getprogname());
799 exit(1);
802 static const struct got_error *
803 cmd_diff(int argc, char *argv[])
805 const struct got_error *error;
806 struct got_repository *repo = NULL;
807 char *repo_path = NULL;
808 struct got_object_id *id1 = NULL, *id2 = NULL;
809 char *id_str1 = NULL, *id_str2 = NULL;
810 int type1, type2;
811 int diff_context = 3, ch;
812 const char *errstr;
814 #ifndef PROFILE
815 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
816 == -1)
817 err(1, "pledge");
818 #endif
820 while ((ch = getopt(argc, argv, "C:")) != -1) {
821 switch (ch) {
822 case 'C':
823 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
824 if (errstr != NULL)
825 err(1, "-C option %s", errstr);
826 break;
827 default:
828 usage();
829 /* NOTREACHED */
833 argc -= optind;
834 argv += optind;
836 if (argc == 0) {
837 usage_diff(); /* TODO show local worktree changes */
838 } else if (argc == 2) {
839 repo_path = getcwd(NULL, 0);
840 if (repo_path == NULL)
841 return got_error_from_errno();
842 id_str1 = argv[0];
843 id_str2 = argv[1];
844 } else if (argc == 3) {
845 repo_path = realpath(argv[0], NULL);
846 if (repo_path == NULL)
847 return got_error_from_errno();
848 id_str1 = argv[1];
849 id_str2 = argv[2];
850 } else
851 usage_diff();
853 error = got_repo_open(&repo, repo_path);
854 free(repo_path);
855 if (error != NULL)
856 goto done;
858 error = got_object_resolve_id_str(&id1, repo, id_str1);
859 if (error)
860 goto done;
862 error = got_object_resolve_id_str(&id2, repo, id_str2);
863 if (error)
864 goto done;
866 error = got_object_get_type(&type1, repo, id1);
867 if (error)
868 goto done;
870 error = got_object_get_type(&type2, repo, id2);
871 if (error)
872 goto done;
874 if (type1 != type2) {
875 error = got_error(GOT_ERR_OBJ_TYPE);
876 goto done;
879 switch (type1) {
880 case GOT_OBJ_TYPE_BLOB:
881 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
882 diff_context, repo, stdout);
883 break;
884 case GOT_OBJ_TYPE_TREE:
885 error = got_diff_objects_as_trees(id1, id2, "", "",
886 diff_context, repo, stdout);
887 break;
888 case GOT_OBJ_TYPE_COMMIT:
889 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
890 id_str2);
891 error = got_diff_objects_as_commits(id1, id2, diff_context,
892 repo, stdout);
893 break;
894 default:
895 error = got_error(GOT_ERR_OBJ_TYPE);
898 done:
899 free(id1);
900 free(id2);
901 if (repo) {
902 const struct got_error *repo_error;
903 repo_error = got_repo_close(repo);
904 if (error == NULL)
905 error = repo_error;
907 return error;
910 __dead static void
911 usage_blame(void)
913 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
914 getprogname());
915 exit(1);
918 static const struct got_error *
919 cmd_blame(int argc, char *argv[])
921 const struct got_error *error;
922 struct got_repository *repo = NULL;
923 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
924 struct got_object_id *commit_id = NULL;
925 char *commit_id_str = NULL;
926 int ch;
928 #ifndef PROFILE
929 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
930 == -1)
931 err(1, "pledge");
932 #endif
934 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
935 switch (ch) {
936 case 'c':
937 commit_id_str = optarg;
938 break;
939 case 'r':
940 repo_path = realpath(optarg, NULL);
941 if (repo_path == NULL)
942 err(1, "-r option");
943 break;
944 default:
945 usage();
946 /* NOTREACHED */
950 argc -= optind;
951 argv += optind;
953 if (argc == 1)
954 path = argv[0];
955 else
956 usage_blame();
958 cwd = getcwd(NULL, 0);
959 if (cwd == NULL) {
960 error = got_error_from_errno();
961 goto done;
963 if (repo_path == NULL) {
964 repo_path = strdup(cwd);
965 if (repo_path == NULL) {
966 error = got_error_from_errno();
967 goto done;
971 error = got_repo_open(&repo, repo_path);
972 if (error != NULL)
973 goto done;
975 error = got_repo_map_path(&in_repo_path, repo, path, 1);
976 if (error != NULL)
977 goto done;
979 if (commit_id_str == NULL) {
980 struct got_reference *head_ref;
981 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
982 if (error != NULL)
983 goto done;
984 error = got_ref_resolve(&commit_id, repo, head_ref);
985 got_ref_close(head_ref);
986 if (error != NULL)
987 goto done;
988 } else {
989 error = got_object_resolve_id_str(&commit_id, repo,
990 commit_id_str);
991 if (error != NULL)
992 goto done;
995 error = got_blame(in_repo_path, commit_id, repo, stdout);
996 done:
997 free(in_repo_path);
998 free(repo_path);
999 free(cwd);
1000 free(commit_id);
1001 if (repo) {
1002 const struct got_error *repo_error;
1003 repo_error = got_repo_close(repo);
1004 if (error == NULL)
1005 error = repo_error;
1007 return error;
1010 __dead static void
1011 usage_tree(void)
1013 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
1014 getprogname());
1015 exit(1);
1019 static const struct got_error *
1020 print_tree(const char *path, struct got_object_id *commit_id,
1021 int show_ids, struct got_repository *repo)
1023 const struct got_error *err = NULL;
1024 struct got_object_id *tree_id = NULL;
1025 struct got_tree_object *tree = NULL;
1026 const struct got_tree_entries *entries;
1027 struct got_tree_entry *te;
1029 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1030 if (err)
1031 goto done;
1033 err = got_object_open_as_tree(&tree, repo, tree_id);
1034 if (err)
1035 goto done;
1036 entries = got_object_tree_get_entries(tree);
1037 te = SIMPLEQ_FIRST(&entries->head);
1038 while (te) {
1039 char *id = NULL;
1041 if (sigint_received || sigpipe_received)
1042 break;
1044 if (show_ids) {
1045 char *id_str;
1046 err = got_object_id_str(&id_str, te->id);
1047 if (err)
1048 goto done;
1049 if (asprintf(&id, "%s ", id_str) == -1) {
1050 err = got_error_from_errno();
1051 free(id_str);
1052 goto done;
1054 free(id_str);
1056 printf("%s%s%s\n", id ? id : "",
1057 te->name, S_ISDIR(te->mode) ? "/" : "");
1058 te = SIMPLEQ_NEXT(te, entry);
1059 free(id);
1061 done:
1062 if (tree)
1063 got_object_tree_close(tree);
1064 free(tree_id);
1065 return err;
1068 static const struct got_error *
1069 cmd_tree(int argc, char *argv[])
1071 const struct got_error *error;
1072 struct got_repository *repo = NULL;
1073 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1074 struct got_object_id *commit_id = NULL;
1075 char *commit_id_str = NULL;
1076 int show_ids = 0;
1077 int ch;
1079 #ifndef PROFILE
1080 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
1081 == -1)
1082 err(1, "pledge");
1083 #endif
1085 while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
1086 switch (ch) {
1087 case 'c':
1088 commit_id_str = optarg;
1089 break;
1090 case 'r':
1091 repo_path = realpath(optarg, NULL);
1092 if (repo_path == NULL)
1093 err(1, "-r option");
1094 break;
1095 case 'i':
1096 show_ids = 1;
1097 break;
1098 default:
1099 usage();
1100 /* NOTREACHED */
1104 argc -= optind;
1105 argv += optind;
1107 if (argc == 1)
1108 path = argv[0];
1109 else if (argc > 1)
1110 usage_tree();
1111 else
1112 path = "/";
1114 cwd = getcwd(NULL, 0);
1115 if (cwd == NULL) {
1116 error = got_error_from_errno();
1117 goto done;
1119 if (repo_path == NULL) {
1120 repo_path = strdup(cwd);
1121 if (repo_path == NULL) {
1122 error = got_error_from_errno();
1123 goto done;
1127 error = got_repo_open(&repo, repo_path);
1128 if (error != NULL)
1129 goto done;
1131 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1132 if (error != NULL)
1133 goto done;
1135 if (commit_id_str == NULL) {
1136 struct got_reference *head_ref;
1137 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1138 if (error != NULL)
1139 goto done;
1140 error = got_ref_resolve(&commit_id, repo, head_ref);
1141 got_ref_close(head_ref);
1142 if (error != NULL)
1143 goto done;
1144 } else {
1145 error = got_object_resolve_id_str(&commit_id, repo,
1146 commit_id_str);
1147 if (error != NULL)
1148 goto done;
1151 error = print_tree(in_repo_path, commit_id, show_ids, repo);
1152 done:
1153 free(in_repo_path);
1154 free(repo_path);
1155 free(cwd);
1156 free(commit_id);
1157 if (repo) {
1158 const struct got_error *repo_error;
1159 repo_error = got_repo_close(repo);
1160 if (error == NULL)
1161 error = repo_error;
1163 return error;
1166 #ifdef notyet
1167 static const struct got_error *
1168 cmd_status(int argc __unused, char *argv[] __unused)
1170 git_repository *repo = NULL;
1171 git_status_list *status;
1172 git_status_options statusopts;
1173 size_t i;
1175 git_libgit2_init();
1177 if (git_repository_open_ext(&repo, ".", 0, NULL))
1178 errx(1, "git_repository_open: %s", giterr_last()->message);
1180 if (git_repository_is_bare(repo))
1181 errx(1, "bar repository");
1183 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
1184 errx(1, "git_status_init_options: %s", giterr_last()->message);
1186 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
1187 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
1188 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
1189 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1191 if (git_status_list_new(&status, repo, &statusopts))
1192 errx(1, "git_status_list_new: %s", giterr_last()->message);
1194 for (i = 0; i < git_status_list_entrycount(status); i++) {
1195 const git_status_entry *se;
1197 se = git_status_byindex(status, i);
1198 switch (se->status) {
1199 case GIT_STATUS_WT_NEW:
1200 printf("? %s\n", se->index_to_workdir->new_file.path);
1201 break;
1202 case GIT_STATUS_WT_MODIFIED:
1203 printf("M %s\n", se->index_to_workdir->new_file.path);
1204 break;
1205 case GIT_STATUS_WT_DELETED:
1206 printf("R %s\n", se->index_to_workdir->new_file.path);
1207 break;
1208 case GIT_STATUS_WT_RENAMED:
1209 printf("m %s -> %s\n",
1210 se->index_to_workdir->old_file.path,
1211 se->index_to_workdir->new_file.path);
1212 break;
1213 case GIT_STATUS_WT_TYPECHANGE:
1214 printf("t %s\n", se->index_to_workdir->new_file.path);
1215 break;
1216 case GIT_STATUS_INDEX_NEW:
1217 printf("A %s\n", se->head_to_index->new_file.path);
1218 break;
1219 case GIT_STATUS_INDEX_MODIFIED:
1220 printf("M %s\n", se->head_to_index->old_file.path);
1221 break;
1222 case GIT_STATUS_INDEX_DELETED:
1223 printf("R %s\n", se->head_to_index->old_file.path);
1224 break;
1225 case GIT_STATUS_INDEX_RENAMED:
1226 printf("m %s -> %s\n",
1227 se->head_to_index->old_file.path,
1228 se->head_to_index->new_file.path);
1229 break;
1230 case GIT_STATUS_INDEX_TYPECHANGE:
1231 printf("t %s\n", se->head_to_index->old_file.path);
1232 break;
1233 case GIT_STATUS_CURRENT:
1234 default:
1235 break;
1239 git_status_list_free(status);
1240 git_repository_free(repo);
1241 git_libgit2_shutdown();
1243 return 0;
1245 #endif