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 const 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 = optarg;
404 break;
405 default:
406 usage();
407 /* NOTREACHED */
411 argc -= optind;
412 argv += optind;
414 #ifndef PROFILE
415 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
416 == -1)
417 err(1, "pledge");
418 #endif
419 if (argc == 0) {
420 worktree_path = getcwd(NULL, 0);
421 if (worktree_path == NULL) {
422 error = got_error_from_errno();
423 goto done;
425 } else if (argc == 1) {
426 worktree_path = realpath(argv[0], NULL);
427 if (worktree_path == NULL) {
428 error = got_error_from_errno();
429 goto done;
431 } else
432 usage_update();
434 error = got_worktree_open(&worktree, worktree_path);
435 if (error != NULL)
436 goto done;
438 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
439 if (error != NULL)
440 goto done;
442 if (commit_id_str == NULL) {
443 struct got_reference *head_ref;
444 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
445 if (error != NULL)
446 goto done;
447 error = got_ref_resolve(&commit_id, repo, head_ref);
448 if (error != NULL)
449 goto done;
450 } else {
451 error = got_object_resolve_id_str(&commit_id, repo,
452 commit_id_str);
453 if (error != NULL)
454 goto done;
457 error = check_ancestry(worktree, commit_id, repo);
458 if (error != NULL)
459 goto done;
461 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
462 commit_id) != 0) {
463 error = got_worktree_set_base_commit_id(worktree, repo,
464 commit_id);
465 if (error)
466 goto done;
469 error = got_worktree_checkout_files(worktree, repo,
470 update_progress, NULL, checkout_cancel, NULL);
471 if (error != NULL)
472 goto done;
473 done:
474 free(worktree_path);
475 free(commit_id);
476 return error;
479 static const struct got_error *
480 print_patch(struct got_commit_object *commit, struct got_object_id *id,
481 int diff_context, struct got_repository *repo)
483 const struct got_error *err = NULL;
484 struct got_tree_object *tree1 = NULL, *tree2;
485 struct got_object_qid *qid;
486 char *id_str1 = NULL, *id_str2;
488 err = got_object_open_as_tree(&tree2, repo,
489 got_object_commit_get_tree_id(commit));
490 if (err)
491 return err;
493 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
494 if (qid != NULL) {
495 struct got_commit_object *pcommit;
497 err = got_object_open_as_commit(&pcommit, repo, qid->id);
498 if (err)
499 return err;
501 err = got_object_open_as_tree(&tree1, repo,
502 got_object_commit_get_tree_id(pcommit));
503 got_object_commit_close(pcommit);
504 if (err)
505 return err;
507 err = got_object_id_str(&id_str1, qid->id);
508 if (err)
509 return err;
512 err = got_object_id_str(&id_str2, id);
513 if (err)
514 goto done;
516 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
517 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
518 done:
519 if (tree1)
520 got_object_tree_close(tree1);
521 got_object_tree_close(tree2);
522 free(id_str1);
523 free(id_str2);
524 return err;
527 static char *
528 get_datestr(time_t *time, char *datebuf)
530 char *p, *s = ctime_r(time, datebuf);
531 p = strchr(s, '\n');
532 if (p)
533 *p = '\0';
534 return s;
537 static const struct got_error *
538 print_commit(struct got_commit_object *commit, struct got_object_id *id,
539 struct got_repository *repo, int show_patch, int diff_context)
541 const struct got_error *err = NULL;
542 char *id_str, *datestr, *logmsg0, *logmsg, *line;
543 char datebuf[26];
544 time_t committer_time;
545 const char *author, *committer;
547 err = got_object_id_str(&id_str, id);
548 if (err)
549 return err;
551 printf("-----------------------------------------------\n");
552 printf("commit %s\n", id_str);
553 free(id_str);
554 printf("from: %s\n", got_object_commit_get_author(commit));
555 committer_time = got_object_commit_get_committer_time(commit);
556 datestr = get_datestr(&committer_time, datebuf);
557 printf("date: %s UTC\n", datestr);
558 author = got_object_commit_get_author(commit);
559 committer = got_object_commit_get_committer(commit);
560 if (strcmp(author, committer) != 0)
561 printf("via: %s\n", committer);
562 if (got_object_commit_get_nparents(commit) > 1) {
563 const struct got_object_id_queue *parent_ids;
564 struct got_object_qid *qid;
565 int n = 1;
566 parent_ids = got_object_commit_get_parent_ids(commit);
567 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
568 err = got_object_id_str(&id_str, qid->id);
569 if (err)
570 return err;
571 printf("parent %d: %s\n", n++, id_str);
572 free(id_str);
576 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
577 if (logmsg0 == NULL)
578 return got_error_from_errno();
580 logmsg = logmsg0;
581 do {
582 line = strsep(&logmsg, "\n");
583 if (line)
584 printf(" %s\n", line);
585 } while (line);
586 free(logmsg0);
588 if (show_patch) {
589 err = print_patch(commit, id, diff_context, repo);
590 if (err == 0)
591 printf("\n");
594 fflush(stdout);
595 return err;
598 static const struct got_error *
599 print_commits(struct got_object_id *root_id, struct got_repository *repo,
600 char *path, int show_patch, int diff_context, int limit,
601 int first_parent_traversal)
603 const struct got_error *err;
604 struct got_commit_graph *graph;
606 err = got_commit_graph_open(&graph, root_id, path,
607 first_parent_traversal, repo);
608 if (err)
609 return err;
610 err = got_commit_graph_iter_start(graph, root_id, repo);
611 if (err)
612 goto done;
613 while (1) {
614 struct got_commit_object *commit;
615 struct got_object_id *id;
617 if (sigint_received || sigpipe_received)
618 break;
620 err = got_commit_graph_iter_next(&id, graph);
621 if (err) {
622 if (err->code == GOT_ERR_ITER_COMPLETED) {
623 err = NULL;
624 break;
626 if (err->code != GOT_ERR_ITER_NEED_MORE)
627 break;
628 err = got_commit_graph_fetch_commits(graph, 1, repo);
629 if (err)
630 break;
631 else
632 continue;
634 if (id == NULL)
635 break;
637 err = got_object_open_as_commit(&commit, repo, id);
638 if (err)
639 break;
640 err = print_commit(commit, id, repo, show_patch, diff_context);
641 got_object_commit_close(commit);
642 if (err || (limit && --limit == 0))
643 break;
645 done:
646 got_commit_graph_close(graph);
647 return err;
650 __dead static void
651 usage_log(void)
653 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
654 "[-r repository-path] [path]\n", getprogname());
655 exit(1);
658 static const struct got_error *
659 cmd_log(int argc, char *argv[])
661 const struct got_error *error;
662 struct got_repository *repo = NULL;
663 struct got_commit_object *commit = NULL;
664 struct got_object_id *id = NULL;
665 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
666 char *start_commit = NULL;
667 int diff_context = 3, ch;
668 int show_patch = 0, limit = 0, first_parent_traversal = 0;
669 const char *errstr;
671 #ifndef PROFILE
672 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
673 == -1)
674 err(1, "pledge");
675 #endif
677 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
678 switch (ch) {
679 case 'p':
680 show_patch = 1;
681 break;
682 case 'c':
683 start_commit = optarg;
684 break;
685 case 'C':
686 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
687 &errstr);
688 if (errstr != NULL)
689 err(1, "-C option %s", errstr);
690 break;
691 case 'l':
692 limit = strtonum(optarg, 1, INT_MAX, &errstr);
693 if (errstr != NULL)
694 err(1, "-l option %s", errstr);
695 break;
696 case 'f':
697 first_parent_traversal = 1;
698 break;
699 case 'r':
700 repo_path = realpath(optarg, NULL);
701 if (repo_path == NULL)
702 err(1, "-r option");
703 break;
704 default:
705 usage();
706 /* NOTREACHED */
710 argc -= optind;
711 argv += optind;
713 if (argc == 0)
714 path = strdup("");
715 else if (argc == 1)
716 path = strdup(argv[0]);
717 else
718 usage_log();
719 if (path == NULL)
720 return got_error_from_errno();
722 cwd = getcwd(NULL, 0);
723 if (cwd == NULL) {
724 error = got_error_from_errno();
725 goto done;
727 if (repo_path == NULL) {
728 repo_path = strdup(cwd);
729 if (repo_path == NULL) {
730 error = got_error_from_errno();
731 goto done;
735 error = got_repo_open(&repo, repo_path);
736 if (error != NULL)
737 goto done;
739 if (start_commit == NULL) {
740 struct got_reference *head_ref;
741 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
742 if (error != NULL)
743 return error;
744 error = got_ref_resolve(&id, repo, head_ref);
745 got_ref_close(head_ref);
746 if (error != NULL)
747 return error;
748 error = got_object_open_as_commit(&commit, repo, id);
749 } else {
750 struct got_reference *ref;
751 error = got_ref_open(&ref, repo, start_commit);
752 if (error == NULL) {
753 error = got_ref_resolve(&id, repo, ref);
754 got_ref_close(ref);
755 if (error != NULL)
756 return error;
757 error = got_object_open_as_commit(&commit, repo, id);
758 if (error != NULL)
759 return error;
761 if (commit == NULL) {
762 error = got_object_resolve_id_str(&id, repo,
763 start_commit);
764 if (error != NULL)
765 return error;
768 if (error != NULL)
769 goto done;
771 error = got_repo_map_path(&in_repo_path, repo, path, 1);
772 if (error != NULL)
773 goto done;
774 if (in_repo_path) {
775 free(path);
776 path = in_repo_path;
779 error = print_commits(id, repo, path, show_patch,
780 diff_context, limit, first_parent_traversal);
781 done:
782 free(path);
783 free(repo_path);
784 free(cwd);
785 free(id);
786 if (repo) {
787 const struct got_error *repo_error;
788 repo_error = got_repo_close(repo);
789 if (error == NULL)
790 error = repo_error;
792 return error;
795 __dead static void
796 usage_diff(void)
798 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
799 "object1 object2\n", getprogname());
800 exit(1);
803 static const struct got_error *
804 cmd_diff(int argc, char *argv[])
806 const struct got_error *error;
807 struct got_repository *repo = NULL;
808 char *repo_path = NULL;
809 struct got_object_id *id1 = NULL, *id2 = NULL;
810 char *id_str1 = NULL, *id_str2 = NULL;
811 int type1, type2;
812 int diff_context = 3, ch;
813 const char *errstr;
815 #ifndef PROFILE
816 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
817 == -1)
818 err(1, "pledge");
819 #endif
821 while ((ch = getopt(argc, argv, "C:")) != -1) {
822 switch (ch) {
823 case 'C':
824 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
825 if (errstr != NULL)
826 err(1, "-C option %s", errstr);
827 break;
828 default:
829 usage();
830 /* NOTREACHED */
834 argc -= optind;
835 argv += optind;
837 if (argc == 0) {
838 usage_diff(); /* TODO show local worktree changes */
839 } else if (argc == 2) {
840 repo_path = getcwd(NULL, 0);
841 if (repo_path == NULL)
842 return got_error_from_errno();
843 id_str1 = argv[0];
844 id_str2 = argv[1];
845 } else if (argc == 3) {
846 repo_path = realpath(argv[0], NULL);
847 if (repo_path == NULL)
848 return got_error_from_errno();
849 id_str1 = argv[1];
850 id_str2 = argv[2];
851 } else
852 usage_diff();
854 error = got_repo_open(&repo, repo_path);
855 free(repo_path);
856 if (error != NULL)
857 goto done;
859 error = got_object_resolve_id_str(&id1, repo, id_str1);
860 if (error)
861 goto done;
863 error = got_object_resolve_id_str(&id2, repo, id_str2);
864 if (error)
865 goto done;
867 error = got_object_get_type(&type1, repo, id1);
868 if (error)
869 goto done;
871 error = got_object_get_type(&type2, repo, id2);
872 if (error)
873 goto done;
875 if (type1 != type2) {
876 error = got_error(GOT_ERR_OBJ_TYPE);
877 goto done;
880 switch (type1) {
881 case GOT_OBJ_TYPE_BLOB:
882 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
883 diff_context, repo, stdout);
884 break;
885 case GOT_OBJ_TYPE_TREE:
886 error = got_diff_objects_as_trees(id1, id2, "", "",
887 diff_context, repo, stdout);
888 break;
889 case GOT_OBJ_TYPE_COMMIT:
890 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
891 id_str2);
892 error = got_diff_objects_as_commits(id1, id2, diff_context,
893 repo, stdout);
894 break;
895 default:
896 error = got_error(GOT_ERR_OBJ_TYPE);
899 done:
900 free(id1);
901 free(id2);
902 if (repo) {
903 const struct got_error *repo_error;
904 repo_error = got_repo_close(repo);
905 if (error == NULL)
906 error = repo_error;
908 return error;
911 __dead static void
912 usage_blame(void)
914 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
915 getprogname());
916 exit(1);
919 static const struct got_error *
920 cmd_blame(int argc, char *argv[])
922 const struct got_error *error;
923 struct got_repository *repo = NULL;
924 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
925 struct got_object_id *commit_id = NULL;
926 char *commit_id_str = NULL;
927 int ch;
929 #ifndef PROFILE
930 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
931 == -1)
932 err(1, "pledge");
933 #endif
935 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
936 switch (ch) {
937 case 'c':
938 commit_id_str = optarg;
939 break;
940 case 'r':
941 repo_path = realpath(optarg, NULL);
942 if (repo_path == NULL)
943 err(1, "-r option");
944 break;
945 default:
946 usage();
947 /* NOTREACHED */
951 argc -= optind;
952 argv += optind;
954 if (argc == 1)
955 path = argv[0];
956 else
957 usage_blame();
959 cwd = getcwd(NULL, 0);
960 if (cwd == NULL) {
961 error = got_error_from_errno();
962 goto done;
964 if (repo_path == NULL) {
965 repo_path = strdup(cwd);
966 if (repo_path == NULL) {
967 error = got_error_from_errno();
968 goto done;
972 error = got_repo_open(&repo, repo_path);
973 if (error != NULL)
974 goto done;
976 error = got_repo_map_path(&in_repo_path, repo, path, 1);
977 if (error != NULL)
978 goto done;
980 if (commit_id_str == NULL) {
981 struct got_reference *head_ref;
982 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
983 if (error != NULL)
984 goto done;
985 error = got_ref_resolve(&commit_id, repo, head_ref);
986 got_ref_close(head_ref);
987 if (error != NULL)
988 goto done;
989 } else {
990 error = got_object_resolve_id_str(&commit_id, repo,
991 commit_id_str);
992 if (error != NULL)
993 goto done;
996 error = got_blame(in_repo_path, commit_id, repo, stdout);
997 done:
998 free(in_repo_path);
999 free(repo_path);
1000 free(cwd);
1001 free(commit_id);
1002 if (repo) {
1003 const struct got_error *repo_error;
1004 repo_error = got_repo_close(repo);
1005 if (error == NULL)
1006 error = repo_error;
1008 return error;
1011 __dead static void
1012 usage_tree(void)
1014 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
1015 getprogname());
1016 exit(1);
1020 static const struct got_error *
1021 print_tree(const char *path, struct got_object_id *commit_id,
1022 int show_ids, struct got_repository *repo)
1024 const struct got_error *err = NULL;
1025 struct got_object_id *tree_id = NULL;
1026 struct got_tree_object *tree = NULL;
1027 const struct got_tree_entries *entries;
1028 struct got_tree_entry *te;
1030 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1031 if (err)
1032 goto done;
1034 err = got_object_open_as_tree(&tree, repo, tree_id);
1035 if (err)
1036 goto done;
1037 entries = got_object_tree_get_entries(tree);
1038 te = SIMPLEQ_FIRST(&entries->head);
1039 while (te) {
1040 char *id = NULL;
1042 if (sigint_received || sigpipe_received)
1043 break;
1045 if (show_ids) {
1046 char *id_str;
1047 err = got_object_id_str(&id_str, te->id);
1048 if (err)
1049 goto done;
1050 if (asprintf(&id, "%s ", id_str) == -1) {
1051 err = got_error_from_errno();
1052 free(id_str);
1053 goto done;
1055 free(id_str);
1057 printf("%s%s%s\n", id ? id : "",
1058 te->name, S_ISDIR(te->mode) ? "/" : "");
1059 te = SIMPLEQ_NEXT(te, entry);
1060 free(id);
1062 done:
1063 if (tree)
1064 got_object_tree_close(tree);
1065 free(tree_id);
1066 return err;
1069 static const struct got_error *
1070 cmd_tree(int argc, char *argv[])
1072 const struct got_error *error;
1073 struct got_repository *repo = NULL;
1074 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1075 struct got_object_id *commit_id = NULL;
1076 char *commit_id_str = NULL;
1077 int show_ids = 0;
1078 int ch;
1080 #ifndef PROFILE
1081 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
1082 == -1)
1083 err(1, "pledge");
1084 #endif
1086 while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
1087 switch (ch) {
1088 case 'c':
1089 commit_id_str = optarg;
1090 break;
1091 case 'r':
1092 repo_path = realpath(optarg, NULL);
1093 if (repo_path == NULL)
1094 err(1, "-r option");
1095 break;
1096 case 'i':
1097 show_ids = 1;
1098 break;
1099 default:
1100 usage();
1101 /* NOTREACHED */
1105 argc -= optind;
1106 argv += optind;
1108 if (argc == 1)
1109 path = argv[0];
1110 else if (argc > 1)
1111 usage_tree();
1112 else
1113 path = "/";
1115 cwd = getcwd(NULL, 0);
1116 if (cwd == NULL) {
1117 error = got_error_from_errno();
1118 goto done;
1120 if (repo_path == NULL) {
1121 repo_path = strdup(cwd);
1122 if (repo_path == NULL) {
1123 error = got_error_from_errno();
1124 goto done;
1128 error = got_repo_open(&repo, repo_path);
1129 if (error != NULL)
1130 goto done;
1132 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1133 if (error != NULL)
1134 goto done;
1136 if (commit_id_str == NULL) {
1137 struct got_reference *head_ref;
1138 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1139 if (error != NULL)
1140 goto done;
1141 error = got_ref_resolve(&commit_id, repo, head_ref);
1142 got_ref_close(head_ref);
1143 if (error != NULL)
1144 goto done;
1145 } else {
1146 error = got_object_resolve_id_str(&commit_id, repo,
1147 commit_id_str);
1148 if (error != NULL)
1149 goto done;
1152 error = print_tree(in_repo_path, commit_id, show_ids, repo);
1153 done:
1154 free(in_repo_path);
1155 free(repo_path);
1156 free(cwd);
1157 free(commit_id);
1158 if (repo) {
1159 const struct got_error *repo_error;
1160 repo_error = got_repo_close(repo);
1161 if (error == NULL)
1162 error = repo_error;
1164 return error;
1167 #ifdef notyet
1168 static const struct got_error *
1169 cmd_status(int argc __unused, char *argv[] __unused)
1171 git_repository *repo = NULL;
1172 git_status_list *status;
1173 git_status_options statusopts;
1174 size_t i;
1176 git_libgit2_init();
1178 if (git_repository_open_ext(&repo, ".", 0, NULL))
1179 errx(1, "git_repository_open: %s", giterr_last()->message);
1181 if (git_repository_is_bare(repo))
1182 errx(1, "bar repository");
1184 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
1185 errx(1, "git_status_init_options: %s", giterr_last()->message);
1187 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
1188 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
1189 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
1190 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1192 if (git_status_list_new(&status, repo, &statusopts))
1193 errx(1, "git_status_list_new: %s", giterr_last()->message);
1195 for (i = 0; i < git_status_list_entrycount(status); i++) {
1196 const git_status_entry *se;
1198 se = git_status_byindex(status, i);
1199 switch (se->status) {
1200 case GIT_STATUS_WT_NEW:
1201 printf("? %s\n", se->index_to_workdir->new_file.path);
1202 break;
1203 case GIT_STATUS_WT_MODIFIED:
1204 printf("M %s\n", se->index_to_workdir->new_file.path);
1205 break;
1206 case GIT_STATUS_WT_DELETED:
1207 printf("R %s\n", se->index_to_workdir->new_file.path);
1208 break;
1209 case GIT_STATUS_WT_RENAMED:
1210 printf("m %s -> %s\n",
1211 se->index_to_workdir->old_file.path,
1212 se->index_to_workdir->new_file.path);
1213 break;
1214 case GIT_STATUS_WT_TYPECHANGE:
1215 printf("t %s\n", se->index_to_workdir->new_file.path);
1216 break;
1217 case GIT_STATUS_INDEX_NEW:
1218 printf("A %s\n", se->head_to_index->new_file.path);
1219 break;
1220 case GIT_STATUS_INDEX_MODIFIED:
1221 printf("M %s\n", se->head_to_index->old_file.path);
1222 break;
1223 case GIT_STATUS_INDEX_DELETED:
1224 printf("R %s\n", se->head_to_index->old_file.path);
1225 break;
1226 case GIT_STATUS_INDEX_RENAMED:
1227 printf("m %s -> %s\n",
1228 se->head_to_index->old_file.path,
1229 se->head_to_index->new_file.path);
1230 break;
1231 case GIT_STATUS_INDEX_TYPECHANGE:
1232 printf("t %s\n", se->head_to_index->old_file.path);
1233 break;
1234 case GIT_STATUS_CURRENT:
1235 default:
1236 break;
1240 git_status_list_free(status);
1241 git_repository_free(repo);
1242 git_libgit2_shutdown();
1244 return 0;
1246 #endif