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 /* TODO: Check the reflog. The head ref may have been rebased. */
345 err = got_ref_resolve(&head_commit_id, repo, head_ref);
346 if (err)
347 goto done;
349 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
350 if (err)
351 goto done;
353 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
354 if (err)
355 goto done;
356 while (1) {
357 struct got_object_id *id;
359 if (sigint_received || sigpipe_received)
360 break;
362 err = got_commit_graph_iter_next(&id, graph);
363 if (err) {
364 if (err->code == GOT_ERR_ITER_COMPLETED) {
365 err = got_error(GOT_ERR_ANCESTRY);
366 break;
368 if (err->code != GOT_ERR_ITER_NEED_MORE)
369 break;
370 err = got_commit_graph_fetch_commits(graph, 1, repo);
371 if (err)
372 break;
373 else
374 continue;
376 if (id == NULL)
377 break;
378 if (got_object_id_cmp(id, commit_id) == 0)
379 break;
381 done:
382 if (head_ref)
383 got_ref_close(head_ref);
384 if (graph)
385 got_commit_graph_close(graph);
386 return err;
389 static const struct got_error *
390 cmd_update(int argc, char *argv[])
392 const struct got_error *error = NULL;
393 struct got_repository *repo = NULL;
394 struct got_worktree *worktree = NULL;
395 char *worktree_path = NULL;
396 struct got_object_id *commit_id = NULL;
397 char *commit_id_str = NULL;
398 int ch;
400 while ((ch = getopt(argc, argv, "c:")) != -1) {
401 switch (ch) {
402 case 'c':
403 commit_id_str = strdup(optarg);
404 if (commit_id_str == NULL)
405 return got_error_from_errno();
406 break;
407 default:
408 usage();
409 /* NOTREACHED */
413 argc -= optind;
414 argv += optind;
416 #ifndef PROFILE
417 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
418 == -1)
419 err(1, "pledge");
420 #endif
421 if (argc == 0) {
422 worktree_path = getcwd(NULL, 0);
423 if (worktree_path == NULL) {
424 error = got_error_from_errno();
425 goto done;
427 } else if (argc == 1) {
428 worktree_path = realpath(argv[0], NULL);
429 if (worktree_path == NULL) {
430 error = got_error_from_errno();
431 goto done;
433 } else
434 usage_update();
436 error = got_worktree_open(&worktree, worktree_path);
437 if (error != NULL)
438 goto done;
440 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
441 if (error != NULL)
442 goto done;
444 if (commit_id_str == NULL) {
445 struct got_reference *head_ref;
446 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
447 if (error != NULL)
448 goto done;
449 error = got_ref_resolve(&commit_id, repo, head_ref);
450 if (error != NULL)
451 goto done;
452 error = got_object_id_str(&commit_id_str, commit_id);
453 if (error != NULL)
454 goto done;
455 } else {
456 error = got_object_resolve_id_str(&commit_id, repo,
457 commit_id_str);
458 if (error != NULL)
459 goto done;
462 error = check_ancestry(worktree, commit_id, repo);
463 if (error != NULL)
464 goto done;
466 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
467 commit_id) != 0) {
468 error = got_worktree_set_base_commit_id(worktree, repo,
469 commit_id);
470 if (error)
471 goto done;
474 error = got_worktree_checkout_files(worktree, repo,
475 update_progress, NULL, checkout_cancel, NULL);
476 if (error != NULL)
477 goto done;
479 printf("Updated to commit %s\n", commit_id_str);
480 done:
481 free(worktree_path);
482 free(commit_id);
483 free(commit_id_str);
484 return error;
487 static const struct got_error *
488 print_patch(struct got_commit_object *commit, struct got_object_id *id,
489 int diff_context, struct got_repository *repo)
491 const struct got_error *err = NULL;
492 struct got_tree_object *tree1 = NULL, *tree2;
493 struct got_object_qid *qid;
494 char *id_str1 = NULL, *id_str2;
496 err = got_object_open_as_tree(&tree2, repo,
497 got_object_commit_get_tree_id(commit));
498 if (err)
499 return err;
501 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
502 if (qid != NULL) {
503 struct got_commit_object *pcommit;
505 err = got_object_open_as_commit(&pcommit, repo, qid->id);
506 if (err)
507 return err;
509 err = got_object_open_as_tree(&tree1, repo,
510 got_object_commit_get_tree_id(pcommit));
511 got_object_commit_close(pcommit);
512 if (err)
513 return err;
515 err = got_object_id_str(&id_str1, qid->id);
516 if (err)
517 return err;
520 err = got_object_id_str(&id_str2, id);
521 if (err)
522 goto done;
524 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
525 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
526 done:
527 if (tree1)
528 got_object_tree_close(tree1);
529 got_object_tree_close(tree2);
530 free(id_str1);
531 free(id_str2);
532 return err;
535 static char *
536 get_datestr(time_t *time, char *datebuf)
538 char *p, *s = ctime_r(time, datebuf);
539 p = strchr(s, '\n');
540 if (p)
541 *p = '\0';
542 return s;
545 static const struct got_error *
546 print_commit(struct got_commit_object *commit, struct got_object_id *id,
547 struct got_repository *repo, int show_patch, int diff_context)
549 const struct got_error *err = NULL;
550 char *id_str, *datestr, *logmsg0, *logmsg, *line;
551 char datebuf[26];
552 time_t committer_time;
553 const char *author, *committer;
555 err = got_object_id_str(&id_str, id);
556 if (err)
557 return err;
559 printf("-----------------------------------------------\n");
560 printf("commit %s\n", id_str);
561 free(id_str);
562 printf("from: %s\n", got_object_commit_get_author(commit));
563 committer_time = got_object_commit_get_committer_time(commit);
564 datestr = get_datestr(&committer_time, datebuf);
565 printf("date: %s UTC\n", datestr);
566 author = got_object_commit_get_author(commit);
567 committer = got_object_commit_get_committer(commit);
568 if (strcmp(author, committer) != 0)
569 printf("via: %s\n", committer);
570 if (got_object_commit_get_nparents(commit) > 1) {
571 const struct got_object_id_queue *parent_ids;
572 struct got_object_qid *qid;
573 int n = 1;
574 parent_ids = got_object_commit_get_parent_ids(commit);
575 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
576 err = got_object_id_str(&id_str, qid->id);
577 if (err)
578 return err;
579 printf("parent %d: %s\n", n++, id_str);
580 free(id_str);
584 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
585 if (logmsg0 == NULL)
586 return got_error_from_errno();
588 logmsg = logmsg0;
589 do {
590 line = strsep(&logmsg, "\n");
591 if (line)
592 printf(" %s\n", line);
593 } while (line);
594 free(logmsg0);
596 if (show_patch) {
597 err = print_patch(commit, id, diff_context, repo);
598 if (err == 0)
599 printf("\n");
602 fflush(stdout);
603 return err;
606 static const struct got_error *
607 print_commits(struct got_object_id *root_id, struct got_repository *repo,
608 char *path, int show_patch, int diff_context, int limit,
609 int first_parent_traversal)
611 const struct got_error *err;
612 struct got_commit_graph *graph;
614 err = got_commit_graph_open(&graph, root_id, path,
615 first_parent_traversal, repo);
616 if (err)
617 return err;
618 err = got_commit_graph_iter_start(graph, root_id, repo);
619 if (err)
620 goto done;
621 while (1) {
622 struct got_commit_object *commit;
623 struct got_object_id *id;
625 if (sigint_received || sigpipe_received)
626 break;
628 err = got_commit_graph_iter_next(&id, graph);
629 if (err) {
630 if (err->code == GOT_ERR_ITER_COMPLETED) {
631 err = NULL;
632 break;
634 if (err->code != GOT_ERR_ITER_NEED_MORE)
635 break;
636 err = got_commit_graph_fetch_commits(graph, 1, repo);
637 if (err)
638 break;
639 else
640 continue;
642 if (id == NULL)
643 break;
645 err = got_object_open_as_commit(&commit, repo, id);
646 if (err)
647 break;
648 err = print_commit(commit, id, repo, show_patch, diff_context);
649 got_object_commit_close(commit);
650 if (err || (limit && --limit == 0))
651 break;
653 done:
654 got_commit_graph_close(graph);
655 return err;
658 __dead static void
659 usage_log(void)
661 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
662 "[-r repository-path] [path]\n", getprogname());
663 exit(1);
666 static const struct got_error *
667 cmd_log(int argc, char *argv[])
669 const struct got_error *error;
670 struct got_repository *repo = NULL;
671 struct got_commit_object *commit = NULL;
672 struct got_object_id *id = NULL;
673 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
674 char *start_commit = NULL;
675 int diff_context = 3, ch;
676 int show_patch = 0, limit = 0, first_parent_traversal = 0;
677 const char *errstr;
679 #ifndef PROFILE
680 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
681 == -1)
682 err(1, "pledge");
683 #endif
685 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
686 switch (ch) {
687 case 'p':
688 show_patch = 1;
689 break;
690 case 'c':
691 start_commit = optarg;
692 break;
693 case 'C':
694 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
695 &errstr);
696 if (errstr != NULL)
697 err(1, "-C option %s", errstr);
698 break;
699 case 'l':
700 limit = strtonum(optarg, 1, INT_MAX, &errstr);
701 if (errstr != NULL)
702 err(1, "-l option %s", errstr);
703 break;
704 case 'f':
705 first_parent_traversal = 1;
706 break;
707 case 'r':
708 repo_path = realpath(optarg, NULL);
709 if (repo_path == NULL)
710 err(1, "-r option");
711 break;
712 default:
713 usage();
714 /* NOTREACHED */
718 argc -= optind;
719 argv += optind;
721 if (argc == 0)
722 path = strdup("");
723 else if (argc == 1)
724 path = strdup(argv[0]);
725 else
726 usage_log();
727 if (path == NULL)
728 return got_error_from_errno();
730 cwd = getcwd(NULL, 0);
731 if (cwd == NULL) {
732 error = got_error_from_errno();
733 goto done;
735 if (repo_path == NULL) {
736 repo_path = strdup(cwd);
737 if (repo_path == NULL) {
738 error = got_error_from_errno();
739 goto done;
743 error = got_repo_open(&repo, repo_path);
744 if (error != NULL)
745 goto done;
747 if (start_commit == NULL) {
748 struct got_reference *head_ref;
749 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
750 if (error != NULL)
751 return error;
752 error = got_ref_resolve(&id, repo, head_ref);
753 got_ref_close(head_ref);
754 if (error != NULL)
755 return error;
756 error = got_object_open_as_commit(&commit, repo, id);
757 } else {
758 struct got_reference *ref;
759 error = got_ref_open(&ref, repo, start_commit);
760 if (error == NULL) {
761 error = got_ref_resolve(&id, repo, ref);
762 got_ref_close(ref);
763 if (error != NULL)
764 return error;
765 error = got_object_open_as_commit(&commit, repo, id);
766 if (error != NULL)
767 return error;
769 if (commit == NULL) {
770 error = got_object_resolve_id_str(&id, repo,
771 start_commit);
772 if (error != NULL)
773 return error;
776 if (error != NULL)
777 goto done;
779 error = got_repo_map_path(&in_repo_path, repo, path, 1);
780 if (error != NULL)
781 goto done;
782 if (in_repo_path) {
783 free(path);
784 path = in_repo_path;
787 error = print_commits(id, repo, path, show_patch,
788 diff_context, limit, first_parent_traversal);
789 done:
790 free(path);
791 free(repo_path);
792 free(cwd);
793 free(id);
794 if (repo) {
795 const struct got_error *repo_error;
796 repo_error = got_repo_close(repo);
797 if (error == NULL)
798 error = repo_error;
800 return error;
803 __dead static void
804 usage_diff(void)
806 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
807 "object1 object2\n", getprogname());
808 exit(1);
811 static const struct got_error *
812 cmd_diff(int argc, char *argv[])
814 const struct got_error *error;
815 struct got_repository *repo = NULL;
816 char *repo_path = NULL;
817 struct got_object_id *id1 = NULL, *id2 = NULL;
818 char *id_str1 = NULL, *id_str2 = NULL;
819 int type1, type2;
820 int diff_context = 3, ch;
821 const char *errstr;
823 #ifndef PROFILE
824 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
825 == -1)
826 err(1, "pledge");
827 #endif
829 while ((ch = getopt(argc, argv, "C:")) != -1) {
830 switch (ch) {
831 case 'C':
832 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
833 if (errstr != NULL)
834 err(1, "-C option %s", errstr);
835 break;
836 default:
837 usage();
838 /* NOTREACHED */
842 argc -= optind;
843 argv += optind;
845 if (argc == 0) {
846 usage_diff(); /* TODO show local worktree changes */
847 } else if (argc == 2) {
848 repo_path = getcwd(NULL, 0);
849 if (repo_path == NULL)
850 return got_error_from_errno();
851 id_str1 = argv[0];
852 id_str2 = argv[1];
853 } else if (argc == 3) {
854 repo_path = realpath(argv[0], NULL);
855 if (repo_path == NULL)
856 return got_error_from_errno();
857 id_str1 = argv[1];
858 id_str2 = argv[2];
859 } else
860 usage_diff();
862 error = got_repo_open(&repo, repo_path);
863 free(repo_path);
864 if (error != NULL)
865 goto done;
867 error = got_object_resolve_id_str(&id1, repo, id_str1);
868 if (error)
869 goto done;
871 error = got_object_resolve_id_str(&id2, repo, id_str2);
872 if (error)
873 goto done;
875 error = got_object_get_type(&type1, repo, id1);
876 if (error)
877 goto done;
879 error = got_object_get_type(&type2, repo, id2);
880 if (error)
881 goto done;
883 if (type1 != type2) {
884 error = got_error(GOT_ERR_OBJ_TYPE);
885 goto done;
888 switch (type1) {
889 case GOT_OBJ_TYPE_BLOB:
890 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
891 diff_context, repo, stdout);
892 break;
893 case GOT_OBJ_TYPE_TREE:
894 error = got_diff_objects_as_trees(id1, id2, "", "",
895 diff_context, repo, stdout);
896 break;
897 case GOT_OBJ_TYPE_COMMIT:
898 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
899 id_str2);
900 error = got_diff_objects_as_commits(id1, id2, diff_context,
901 repo, stdout);
902 break;
903 default:
904 error = got_error(GOT_ERR_OBJ_TYPE);
907 done:
908 free(id1);
909 free(id2);
910 if (repo) {
911 const struct got_error *repo_error;
912 repo_error = got_repo_close(repo);
913 if (error == NULL)
914 error = repo_error;
916 return error;
919 __dead static void
920 usage_blame(void)
922 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
923 getprogname());
924 exit(1);
927 static const struct got_error *
928 cmd_blame(int argc, char *argv[])
930 const struct got_error *error;
931 struct got_repository *repo = NULL;
932 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
933 struct got_object_id *commit_id = NULL;
934 char *commit_id_str = NULL;
935 int ch;
937 #ifndef PROFILE
938 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
939 == -1)
940 err(1, "pledge");
941 #endif
943 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
944 switch (ch) {
945 case 'c':
946 commit_id_str = optarg;
947 break;
948 case 'r':
949 repo_path = realpath(optarg, NULL);
950 if (repo_path == NULL)
951 err(1, "-r option");
952 break;
953 default:
954 usage();
955 /* NOTREACHED */
959 argc -= optind;
960 argv += optind;
962 if (argc == 1)
963 path = argv[0];
964 else
965 usage_blame();
967 cwd = getcwd(NULL, 0);
968 if (cwd == NULL) {
969 error = got_error_from_errno();
970 goto done;
972 if (repo_path == NULL) {
973 repo_path = strdup(cwd);
974 if (repo_path == NULL) {
975 error = got_error_from_errno();
976 goto done;
980 error = got_repo_open(&repo, repo_path);
981 if (error != NULL)
982 goto done;
984 error = got_repo_map_path(&in_repo_path, repo, path, 1);
985 if (error != NULL)
986 goto done;
988 if (commit_id_str == NULL) {
989 struct got_reference *head_ref;
990 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
991 if (error != NULL)
992 goto done;
993 error = got_ref_resolve(&commit_id, repo, head_ref);
994 got_ref_close(head_ref);
995 if (error != NULL)
996 goto done;
997 } else {
998 error = got_object_resolve_id_str(&commit_id, repo,
999 commit_id_str);
1000 if (error != NULL)
1001 goto done;
1004 error = got_blame(in_repo_path, commit_id, repo, stdout);
1005 done:
1006 free(in_repo_path);
1007 free(repo_path);
1008 free(cwd);
1009 free(commit_id);
1010 if (repo) {
1011 const struct got_error *repo_error;
1012 repo_error = got_repo_close(repo);
1013 if (error == NULL)
1014 error = repo_error;
1016 return error;
1019 __dead static void
1020 usage_tree(void)
1022 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
1023 getprogname());
1024 exit(1);
1028 static const struct got_error *
1029 print_tree(const char *path, struct got_object_id *commit_id,
1030 int show_ids, struct got_repository *repo)
1032 const struct got_error *err = NULL;
1033 struct got_object_id *tree_id = NULL;
1034 struct got_tree_object *tree = NULL;
1035 const struct got_tree_entries *entries;
1036 struct got_tree_entry *te;
1038 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1039 if (err)
1040 goto done;
1042 err = got_object_open_as_tree(&tree, repo, tree_id);
1043 if (err)
1044 goto done;
1045 entries = got_object_tree_get_entries(tree);
1046 te = SIMPLEQ_FIRST(&entries->head);
1047 while (te) {
1048 char *id = NULL;
1050 if (sigint_received || sigpipe_received)
1051 break;
1053 if (show_ids) {
1054 char *id_str;
1055 err = got_object_id_str(&id_str, te->id);
1056 if (err)
1057 goto done;
1058 if (asprintf(&id, "%s ", id_str) == -1) {
1059 err = got_error_from_errno();
1060 free(id_str);
1061 goto done;
1063 free(id_str);
1065 printf("%s%s%s\n", id ? id : "",
1066 te->name, S_ISDIR(te->mode) ? "/" : "");
1067 te = SIMPLEQ_NEXT(te, entry);
1068 free(id);
1070 done:
1071 if (tree)
1072 got_object_tree_close(tree);
1073 free(tree_id);
1074 return err;
1077 static const struct got_error *
1078 cmd_tree(int argc, char *argv[])
1080 const struct got_error *error;
1081 struct got_repository *repo = NULL;
1082 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1083 struct got_object_id *commit_id = NULL;
1084 char *commit_id_str = NULL;
1085 int show_ids = 0;
1086 int ch;
1088 #ifndef PROFILE
1089 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
1090 == -1)
1091 err(1, "pledge");
1092 #endif
1094 while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
1095 switch (ch) {
1096 case 'c':
1097 commit_id_str = optarg;
1098 break;
1099 case 'r':
1100 repo_path = realpath(optarg, NULL);
1101 if (repo_path == NULL)
1102 err(1, "-r option");
1103 break;
1104 case 'i':
1105 show_ids = 1;
1106 break;
1107 default:
1108 usage();
1109 /* NOTREACHED */
1113 argc -= optind;
1114 argv += optind;
1116 if (argc == 1)
1117 path = argv[0];
1118 else if (argc > 1)
1119 usage_tree();
1120 else
1121 path = "/";
1123 cwd = getcwd(NULL, 0);
1124 if (cwd == NULL) {
1125 error = got_error_from_errno();
1126 goto done;
1128 if (repo_path == NULL) {
1129 repo_path = strdup(cwd);
1130 if (repo_path == NULL) {
1131 error = got_error_from_errno();
1132 goto done;
1136 error = got_repo_open(&repo, repo_path);
1137 if (error != NULL)
1138 goto done;
1140 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1141 if (error != NULL)
1142 goto done;
1144 if (commit_id_str == NULL) {
1145 struct got_reference *head_ref;
1146 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1147 if (error != NULL)
1148 goto done;
1149 error = got_ref_resolve(&commit_id, repo, head_ref);
1150 got_ref_close(head_ref);
1151 if (error != NULL)
1152 goto done;
1153 } else {
1154 error = got_object_resolve_id_str(&commit_id, repo,
1155 commit_id_str);
1156 if (error != NULL)
1157 goto done;
1160 error = print_tree(in_repo_path, commit_id, show_ids, repo);
1161 done:
1162 free(in_repo_path);
1163 free(repo_path);
1164 free(cwd);
1165 free(commit_id);
1166 if (repo) {
1167 const struct got_error *repo_error;
1168 repo_error = got_repo_close(repo);
1169 if (error == NULL)
1170 error = repo_error;
1172 return error;
1175 #ifdef notyet
1176 static const struct got_error *
1177 cmd_status(int argc __unused, char *argv[] __unused)
1179 git_repository *repo = NULL;
1180 git_status_list *status;
1181 git_status_options statusopts;
1182 size_t i;
1184 git_libgit2_init();
1186 if (git_repository_open_ext(&repo, ".", 0, NULL))
1187 errx(1, "git_repository_open: %s", giterr_last()->message);
1189 if (git_repository_is_bare(repo))
1190 errx(1, "bar repository");
1192 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
1193 errx(1, "git_status_init_options: %s", giterr_last()->message);
1195 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
1196 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
1197 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
1198 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1200 if (git_status_list_new(&status, repo, &statusopts))
1201 errx(1, "git_status_list_new: %s", giterr_last()->message);
1203 for (i = 0; i < git_status_list_entrycount(status); i++) {
1204 const git_status_entry *se;
1206 se = git_status_byindex(status, i);
1207 switch (se->status) {
1208 case GIT_STATUS_WT_NEW:
1209 printf("? %s\n", se->index_to_workdir->new_file.path);
1210 break;
1211 case GIT_STATUS_WT_MODIFIED:
1212 printf("M %s\n", se->index_to_workdir->new_file.path);
1213 break;
1214 case GIT_STATUS_WT_DELETED:
1215 printf("R %s\n", se->index_to_workdir->new_file.path);
1216 break;
1217 case GIT_STATUS_WT_RENAMED:
1218 printf("m %s -> %s\n",
1219 se->index_to_workdir->old_file.path,
1220 se->index_to_workdir->new_file.path);
1221 break;
1222 case GIT_STATUS_WT_TYPECHANGE:
1223 printf("t %s\n", se->index_to_workdir->new_file.path);
1224 break;
1225 case GIT_STATUS_INDEX_NEW:
1226 printf("A %s\n", se->head_to_index->new_file.path);
1227 break;
1228 case GIT_STATUS_INDEX_MODIFIED:
1229 printf("M %s\n", se->head_to_index->old_file.path);
1230 break;
1231 case GIT_STATUS_INDEX_DELETED:
1232 printf("R %s\n", se->head_to_index->old_file.path);
1233 break;
1234 case GIT_STATUS_INDEX_RENAMED:
1235 printf("m %s -> %s\n",
1236 se->head_to_index->old_file.path,
1237 se->head_to_index->new_file.path);
1238 break;
1239 case GIT_STATUS_INDEX_TYPECHANGE:
1240 printf("t %s\n", se->head_to_index->old_file.path);
1241 break;
1242 case GIT_STATUS_CURRENT:
1243 default:
1244 break;
1248 git_status_list_free(status);
1249 git_repository_free(repo);
1250 git_libgit2_shutdown();
1252 return 0;
1254 #endif