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"
42 #include "got_privsep.h"
44 #ifndef nitems
45 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
46 #endif
48 static volatile sig_atomic_t sigint_received;
49 static volatile sig_atomic_t sigpipe_received;
51 static void
52 catch_sigint(int signo)
53 {
54 sigint_received = 1;
55 }
57 static void
58 catch_sigpipe(int signo)
59 {
60 sigpipe_received = 1;
61 }
64 struct cmd {
65 const char *cmd_name;
66 const struct got_error *(*cmd_main)(int, char *[]);
67 void (*cmd_usage)(void);
68 const char *cmd_descr;
69 };
71 __dead static void usage(void);
72 __dead static void usage_checkout(void);
73 __dead static void usage_update(void);
74 __dead static void usage_log(void);
75 __dead static void usage_diff(void);
76 __dead static void usage_blame(void);
77 __dead static void usage_tree(void);
79 static const struct got_error* cmd_checkout(int, char *[]);
80 static const struct got_error* cmd_update(int, char *[]);
81 static const struct got_error* cmd_log(int, char *[]);
82 static const struct got_error* cmd_diff(int, char *[]);
83 static const struct got_error* cmd_blame(int, char *[]);
84 static const struct got_error* cmd_tree(int, char *[]);
85 #ifdef notyet
86 static const struct got_error* cmd_status(int, char *[]);
87 #endif
89 static struct cmd got_commands[] = {
90 { "checkout", cmd_checkout, usage_checkout,
91 "check out a new work tree from a repository" },
92 { "update", cmd_update, usage_update,
93 "update a work tree to a different commit" },
94 { "log", cmd_log, usage_log,
95 "show repository history" },
96 { "diff", cmd_diff, usage_diff,
97 "compare files and directories" },
98 { "blame", cmd_blame, usage_blame,
99 " show when lines in a file were changed" },
100 { "tree", cmd_tree, usage_tree,
101 " list files and directories in repository" },
102 #ifdef notyet
103 { "status", cmd_status, usage_status,
104 "show modification status of files" },
105 #endif
106 };
108 int
109 main(int argc, char *argv[])
111 struct cmd *cmd;
112 unsigned int i;
113 int ch;
114 int hflag = 0;
116 setlocale(LC_ALL, "");
118 while ((ch = getopt(argc, argv, "h")) != -1) {
119 switch (ch) {
120 case 'h':
121 hflag = 1;
122 break;
123 default:
124 usage();
125 /* NOTREACHED */
129 argc -= optind;
130 argv += optind;
131 optind = 0;
133 if (argc <= 0)
134 usage();
136 signal(SIGINT, catch_sigint);
137 signal(SIGPIPE, catch_sigpipe);
139 for (i = 0; i < nitems(got_commands); i++) {
140 const struct got_error *error;
142 cmd = &got_commands[i];
144 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
145 continue;
147 if (hflag)
148 got_commands[i].cmd_usage();
150 error = got_commands[i].cmd_main(argc, argv);
151 if (error && !(sigint_received || sigpipe_received)) {
152 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
153 return 1;
156 return 0;
159 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
160 return 1;
163 __dead static void
164 usage(void)
166 int i;
168 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
169 "Available commands:\n", getprogname());
170 for (i = 0; i < nitems(got_commands); i++) {
171 struct cmd *cmd = &got_commands[i];
172 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
174 exit(1);
177 static const struct got_error *
178 apply_unveil(const char *repo_path, const char *worktree_path)
180 const struct got_error *error;
182 if (repo_path && unveil(repo_path, "r") != 0)
183 return got_error_from_errno();
185 if (worktree_path && unveil(worktree_path, "rwc") != 0)
186 return got_error_from_errno();
188 if (unveil("/tmp", "rwc") != 0)
189 return got_error_from_errno();
191 error = got_privsep_unveil_exec_helpers();
192 if (error != NULL)
193 return error;
195 if (unveil(NULL, NULL) != 0)
196 return got_error_from_errno();
198 return NULL;
201 __dead static void
202 usage_checkout(void)
204 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
205 "[worktree-path]\n", getprogname());
206 exit(1);
209 static void
210 checkout_progress(void *arg, unsigned char status, const char *path)
212 char *worktree_path = arg;
214 while (path[0] == '/')
215 path++;
217 printf("%c %s/%s\n", status, worktree_path, path);
220 static const struct got_error *
221 checkout_cancel(void *arg)
223 if (sigint_received || sigpipe_received)
224 return got_error(GOT_ERR_CANCELLED);
225 return NULL;
228 static const struct got_error *
229 cmd_checkout(int argc, char *argv[])
231 const struct got_error *error = NULL;
232 struct got_repository *repo = NULL;
233 struct got_reference *head_ref = NULL;
234 struct got_worktree *worktree = NULL;
235 char *repo_path = NULL;
236 char *worktree_path = NULL;
237 const char *path_prefix = "";
238 int ch, same_path_prefix;
240 while ((ch = getopt(argc, argv, "p:")) != -1) {
241 switch (ch) {
242 case 'p':
243 path_prefix = optarg;
244 break;
245 default:
246 usage();
247 /* NOTREACHED */
251 argc -= optind;
252 argv += optind;
254 #ifndef PROFILE
255 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
256 NULL) == -1)
257 err(1, "pledge");
258 #endif
259 if (argc == 1) {
260 char *cwd, *base, *dotgit;
261 repo_path = realpath(argv[0], NULL);
262 if (repo_path == NULL)
263 return got_error_from_errno();
264 cwd = getcwd(NULL, 0);
265 if (cwd == NULL) {
266 error = got_error_from_errno();
267 goto done;
269 if (path_prefix[0])
270 base = basename(path_prefix);
271 else
272 base = basename(repo_path);
273 if (base == NULL) {
274 error = got_error_from_errno();
275 goto done;
277 dotgit = strstr(base, ".git");
278 if (dotgit)
279 *dotgit = '\0';
280 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
281 error = got_error_from_errno();
282 free(cwd);
283 goto done;
285 free(cwd);
286 } else if (argc == 2) {
287 repo_path = realpath(argv[0], NULL);
288 if (repo_path == NULL) {
289 error = got_error_from_errno();
290 goto done;
292 worktree_path = realpath(argv[1], NULL);
293 if (worktree_path == NULL) {
294 error = got_error_from_errno();
295 goto done;
297 } else
298 usage_checkout();
300 error = apply_unveil(repo_path, worktree_path);
301 if (error)
302 goto done;
304 error = got_repo_open(&repo, repo_path);
305 if (error != NULL)
306 goto done;
307 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
308 if (error != NULL)
309 goto done;
311 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
312 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
313 goto done;
315 error = got_worktree_open(&worktree, worktree_path);
316 if (error != NULL)
317 goto done;
319 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
320 path_prefix);
321 if (error != NULL)
322 goto done;
323 if (!same_path_prefix) {
324 error = got_error(GOT_ERR_PATH_PREFIX);
325 goto done;
328 error = got_worktree_checkout_files(worktree, repo,
329 checkout_progress, worktree_path, checkout_cancel, NULL);
330 if (error != NULL)
331 goto done;
333 printf("Now shut up and hack\n");
335 done:
336 free(repo_path);
337 free(worktree_path);
338 return error;
341 __dead static void
342 usage_update(void)
344 fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
345 getprogname());
346 exit(1);
349 static void
350 update_progress(void *arg, unsigned char status, const char *path)
352 if (status == GOT_STATUS_EXISTS)
353 return;
355 while (path[0] == '/')
356 path++;
357 printf("%c %s\n", status, path);
360 static const struct got_error *
361 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
362 struct got_repository *repo)
364 const struct got_error *err;
365 struct got_reference *head_ref = NULL;
366 struct got_object_id *head_commit_id = NULL;
367 struct got_commit_graph *graph = NULL;
369 head_ref = got_worktree_get_head_ref(worktree);
370 if (head_ref == NULL)
371 return got_error_from_errno();
373 /* TODO: Check the reflog. The head ref may have been rebased. */
374 err = got_ref_resolve(&head_commit_id, repo, head_ref);
375 if (err)
376 goto done;
378 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
379 if (err)
380 goto done;
382 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
383 if (err)
384 goto done;
385 while (1) {
386 struct got_object_id *id;
388 if (sigint_received || sigpipe_received)
389 break;
391 err = got_commit_graph_iter_next(&id, graph);
392 if (err) {
393 if (err->code == GOT_ERR_ITER_COMPLETED) {
394 err = got_error(GOT_ERR_ANCESTRY);
395 break;
397 if (err->code != GOT_ERR_ITER_NEED_MORE)
398 break;
399 err = got_commit_graph_fetch_commits(graph, 1, repo);
400 if (err)
401 break;
402 else
403 continue;
405 if (id == NULL)
406 break;
407 if (got_object_id_cmp(id, commit_id) == 0)
408 break;
410 done:
411 if (head_ref)
412 got_ref_close(head_ref);
413 if (graph)
414 got_commit_graph_close(graph);
415 return err;
418 static const struct got_error *
419 cmd_update(int argc, char *argv[])
421 const struct got_error *error = NULL;
422 struct got_repository *repo = NULL;
423 struct got_worktree *worktree = NULL;
424 char *worktree_path = NULL;
425 struct got_object_id *commit_id = NULL;
426 char *commit_id_str = NULL;
427 int ch;
429 while ((ch = getopt(argc, argv, "c:")) != -1) {
430 switch (ch) {
431 case 'c':
432 commit_id_str = strdup(optarg);
433 if (commit_id_str == NULL)
434 return got_error_from_errno();
435 break;
436 default:
437 usage();
438 /* NOTREACHED */
442 argc -= optind;
443 argv += optind;
445 #ifndef PROFILE
446 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
447 NULL) == -1)
448 err(1, "pledge");
449 #endif
450 if (argc == 0) {
451 worktree_path = getcwd(NULL, 0);
452 if (worktree_path == NULL) {
453 error = got_error_from_errno();
454 goto done;
456 } else if (argc == 1) {
457 worktree_path = realpath(argv[0], NULL);
458 if (worktree_path == NULL) {
459 error = got_error_from_errno();
460 goto done;
462 } else
463 usage_update();
465 error = got_worktree_open(&worktree, worktree_path);
466 if (error != NULL)
467 goto done;
469 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
470 if (error != NULL)
471 goto done;
473 error = apply_unveil(got_repo_get_path(repo), worktree_path);
474 if (error)
475 goto done;
477 if (commit_id_str == NULL) {
478 struct got_reference *head_ref;
479 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
480 if (error != NULL)
481 goto done;
482 error = got_ref_resolve(&commit_id, repo, head_ref);
483 if (error != NULL)
484 goto done;
485 error = got_object_id_str(&commit_id_str, commit_id);
486 if (error != NULL)
487 goto done;
488 } else {
489 error = got_object_resolve_id_str(&commit_id, repo,
490 commit_id_str);
491 if (error != NULL)
492 goto done;
495 error = check_ancestry(worktree, commit_id, repo);
496 if (error != NULL)
497 goto done;
499 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
500 commit_id) != 0) {
501 error = got_worktree_set_base_commit_id(worktree, repo,
502 commit_id);
503 if (error)
504 goto done;
507 error = got_worktree_checkout_files(worktree, repo,
508 update_progress, NULL, checkout_cancel, NULL);
509 if (error != NULL)
510 goto done;
512 printf("Updated to commit %s\n", commit_id_str);
513 done:
514 free(worktree_path);
515 free(commit_id);
516 free(commit_id_str);
517 return error;
520 static const struct got_error *
521 print_patch(struct got_commit_object *commit, struct got_object_id *id,
522 int diff_context, struct got_repository *repo)
524 const struct got_error *err = NULL;
525 struct got_tree_object *tree1 = NULL, *tree2;
526 struct got_object_qid *qid;
527 char *id_str1 = NULL, *id_str2;
529 err = got_object_open_as_tree(&tree2, repo,
530 got_object_commit_get_tree_id(commit));
531 if (err)
532 return err;
534 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
535 if (qid != NULL) {
536 struct got_commit_object *pcommit;
538 err = got_object_open_as_commit(&pcommit, repo, qid->id);
539 if (err)
540 return err;
542 err = got_object_open_as_tree(&tree1, repo,
543 got_object_commit_get_tree_id(pcommit));
544 got_object_commit_close(pcommit);
545 if (err)
546 return err;
548 err = got_object_id_str(&id_str1, qid->id);
549 if (err)
550 return err;
553 err = got_object_id_str(&id_str2, id);
554 if (err)
555 goto done;
557 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
558 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
559 done:
560 if (tree1)
561 got_object_tree_close(tree1);
562 got_object_tree_close(tree2);
563 free(id_str1);
564 free(id_str2);
565 return err;
568 static char *
569 get_datestr(time_t *time, char *datebuf)
571 char *p, *s = ctime_r(time, datebuf);
572 p = strchr(s, '\n');
573 if (p)
574 *p = '\0';
575 return s;
578 static const struct got_error *
579 print_commit(struct got_commit_object *commit, struct got_object_id *id,
580 struct got_repository *repo, int show_patch, int diff_context)
582 const struct got_error *err = NULL;
583 char *id_str, *datestr, *logmsg0, *logmsg, *line;
584 char datebuf[26];
585 time_t committer_time;
586 const char *author, *committer;
588 err = got_object_id_str(&id_str, id);
589 if (err)
590 return err;
592 printf("-----------------------------------------------\n");
593 printf("commit %s\n", id_str);
594 free(id_str);
595 printf("from: %s\n", got_object_commit_get_author(commit));
596 committer_time = got_object_commit_get_committer_time(commit);
597 datestr = get_datestr(&committer_time, datebuf);
598 printf("date: %s UTC\n", datestr);
599 author = got_object_commit_get_author(commit);
600 committer = got_object_commit_get_committer(commit);
601 if (strcmp(author, committer) != 0)
602 printf("via: %s\n", committer);
603 if (got_object_commit_get_nparents(commit) > 1) {
604 const struct got_object_id_queue *parent_ids;
605 struct got_object_qid *qid;
606 int n = 1;
607 parent_ids = got_object_commit_get_parent_ids(commit);
608 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
609 err = got_object_id_str(&id_str, qid->id);
610 if (err)
611 return err;
612 printf("parent %d: %s\n", n++, id_str);
613 free(id_str);
617 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
618 if (logmsg0 == NULL)
619 return got_error_from_errno();
621 logmsg = logmsg0;
622 do {
623 line = strsep(&logmsg, "\n");
624 if (line)
625 printf(" %s\n", line);
626 } while (line);
627 free(logmsg0);
629 if (show_patch) {
630 err = print_patch(commit, id, diff_context, repo);
631 if (err == 0)
632 printf("\n");
635 fflush(stdout);
636 return err;
639 static const struct got_error *
640 print_commits(struct got_object_id *root_id, struct got_repository *repo,
641 char *path, int show_patch, int diff_context, int limit,
642 int first_parent_traversal)
644 const struct got_error *err;
645 struct got_commit_graph *graph;
647 err = got_commit_graph_open(&graph, root_id, path,
648 first_parent_traversal, repo);
649 if (err)
650 return err;
651 err = got_commit_graph_iter_start(graph, root_id, repo);
652 if (err)
653 goto done;
654 while (1) {
655 struct got_commit_object *commit;
656 struct got_object_id *id;
658 if (sigint_received || sigpipe_received)
659 break;
661 err = got_commit_graph_iter_next(&id, graph);
662 if (err) {
663 if (err->code == GOT_ERR_ITER_COMPLETED) {
664 err = NULL;
665 break;
667 if (err->code != GOT_ERR_ITER_NEED_MORE)
668 break;
669 err = got_commit_graph_fetch_commits(graph, 1, repo);
670 if (err)
671 break;
672 else
673 continue;
675 if (id == NULL)
676 break;
678 err = got_object_open_as_commit(&commit, repo, id);
679 if (err)
680 break;
681 err = print_commit(commit, id, repo, show_patch, diff_context);
682 got_object_commit_close(commit);
683 if (err || (limit && --limit == 0))
684 break;
686 done:
687 got_commit_graph_close(graph);
688 return err;
691 __dead static void
692 usage_log(void)
694 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
695 "[-r repository-path] [path]\n", getprogname());
696 exit(1);
699 static const struct got_error *
700 cmd_log(int argc, char *argv[])
702 const struct got_error *error;
703 struct got_repository *repo = NULL;
704 struct got_commit_object *commit = NULL;
705 struct got_object_id *id = NULL;
706 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
707 char *start_commit = NULL;
708 int diff_context = 3, ch;
709 int show_patch = 0, limit = 0, first_parent_traversal = 0;
710 const char *errstr;
712 #ifndef PROFILE
713 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
714 NULL)
715 == -1)
716 err(1, "pledge");
717 #endif
719 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
720 switch (ch) {
721 case 'p':
722 show_patch = 1;
723 break;
724 case 'c':
725 start_commit = optarg;
726 break;
727 case 'C':
728 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
729 &errstr);
730 if (errstr != NULL)
731 err(1, "-C option %s", errstr);
732 break;
733 case 'l':
734 limit = strtonum(optarg, 1, INT_MAX, &errstr);
735 if (errstr != NULL)
736 err(1, "-l option %s", errstr);
737 break;
738 case 'f':
739 first_parent_traversal = 1;
740 break;
741 case 'r':
742 repo_path = realpath(optarg, NULL);
743 if (repo_path == NULL)
744 err(1, "-r option");
745 break;
746 default:
747 usage();
748 /* NOTREACHED */
752 argc -= optind;
753 argv += optind;
755 if (argc == 0)
756 path = strdup("");
757 else if (argc == 1)
758 path = strdup(argv[0]);
759 else
760 usage_log();
761 if (path == NULL)
762 return got_error_from_errno();
764 cwd = getcwd(NULL, 0);
765 if (cwd == NULL) {
766 error = got_error_from_errno();
767 goto done;
769 if (repo_path == NULL) {
770 repo_path = strdup(cwd);
771 if (repo_path == NULL) {
772 error = got_error_from_errno();
773 goto done;
777 error = apply_unveil(repo_path, NULL);
778 if (error)
779 goto done;
781 error = got_repo_open(&repo, repo_path);
782 if (error != NULL)
783 goto done;
785 if (start_commit == NULL) {
786 struct got_reference *head_ref;
787 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
788 if (error != NULL)
789 return error;
790 error = got_ref_resolve(&id, repo, head_ref);
791 got_ref_close(head_ref);
792 if (error != NULL)
793 return error;
794 error = got_object_open_as_commit(&commit, repo, id);
795 } else {
796 struct got_reference *ref;
797 error = got_ref_open(&ref, repo, start_commit);
798 if (error == NULL) {
799 error = got_ref_resolve(&id, repo, ref);
800 got_ref_close(ref);
801 if (error != NULL)
802 return error;
803 error = got_object_open_as_commit(&commit, repo, id);
804 if (error != NULL)
805 return error;
807 if (commit == NULL) {
808 error = got_object_resolve_id_str(&id, repo,
809 start_commit);
810 if (error != NULL)
811 return error;
814 if (error != NULL)
815 goto done;
817 error = got_repo_map_path(&in_repo_path, repo, path, 1);
818 if (error != NULL)
819 goto done;
820 if (in_repo_path) {
821 free(path);
822 path = in_repo_path;
825 error = print_commits(id, repo, path, show_patch,
826 diff_context, limit, first_parent_traversal);
827 done:
828 free(path);
829 free(repo_path);
830 free(cwd);
831 free(id);
832 if (repo) {
833 const struct got_error *repo_error;
834 repo_error = got_repo_close(repo);
835 if (error == NULL)
836 error = repo_error;
838 return error;
841 __dead static void
842 usage_diff(void)
844 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
845 "object1 object2\n", getprogname());
846 exit(1);
849 static const struct got_error *
850 cmd_diff(int argc, char *argv[])
852 const struct got_error *error;
853 struct got_repository *repo = NULL;
854 char *repo_path = NULL;
855 struct got_object_id *id1 = NULL, *id2 = NULL;
856 char *id_str1 = NULL, *id_str2 = NULL;
857 int type1, type2;
858 int diff_context = 3, ch;
859 const char *errstr;
861 #ifndef PROFILE
862 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
863 NULL) == -1)
864 err(1, "pledge");
865 #endif
867 while ((ch = getopt(argc, argv, "C:")) != -1) {
868 switch (ch) {
869 case 'C':
870 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
871 if (errstr != NULL)
872 err(1, "-C option %s", errstr);
873 break;
874 default:
875 usage();
876 /* NOTREACHED */
880 argc -= optind;
881 argv += optind;
883 if (argc == 0) {
884 usage_diff(); /* TODO show local worktree changes */
885 } else if (argc == 2) {
886 repo_path = getcwd(NULL, 0);
887 if (repo_path == NULL)
888 return got_error_from_errno();
889 id_str1 = argv[0];
890 id_str2 = argv[1];
891 } else if (argc == 3) {
892 repo_path = realpath(argv[0], NULL);
893 if (repo_path == NULL)
894 return got_error_from_errno();
895 id_str1 = argv[1];
896 id_str2 = argv[2];
897 } else
898 usage_diff();
900 error = apply_unveil(repo_path, NULL);
901 if (error)
902 goto done;
904 error = got_repo_open(&repo, repo_path);
905 free(repo_path);
906 if (error != NULL)
907 goto done;
909 error = got_object_resolve_id_str(&id1, repo, id_str1);
910 if (error)
911 goto done;
913 error = got_object_resolve_id_str(&id2, repo, id_str2);
914 if (error)
915 goto done;
917 error = got_object_get_type(&type1, repo, id1);
918 if (error)
919 goto done;
921 error = got_object_get_type(&type2, repo, id2);
922 if (error)
923 goto done;
925 if (type1 != type2) {
926 error = got_error(GOT_ERR_OBJ_TYPE);
927 goto done;
930 switch (type1) {
931 case GOT_OBJ_TYPE_BLOB:
932 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
933 diff_context, repo, stdout);
934 break;
935 case GOT_OBJ_TYPE_TREE:
936 error = got_diff_objects_as_trees(id1, id2, "", "",
937 diff_context, repo, stdout);
938 break;
939 case GOT_OBJ_TYPE_COMMIT:
940 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
941 id_str2);
942 error = got_diff_objects_as_commits(id1, id2, diff_context,
943 repo, stdout);
944 break;
945 default:
946 error = got_error(GOT_ERR_OBJ_TYPE);
949 done:
950 free(id1);
951 free(id2);
952 if (repo) {
953 const struct got_error *repo_error;
954 repo_error = got_repo_close(repo);
955 if (error == NULL)
956 error = repo_error;
958 return error;
961 __dead static void
962 usage_blame(void)
964 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
965 getprogname());
966 exit(1);
969 static const struct got_error *
970 cmd_blame(int argc, char *argv[])
972 const struct got_error *error;
973 struct got_repository *repo = NULL;
974 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
975 struct got_object_id *commit_id = NULL;
976 char *commit_id_str = NULL;
977 int ch;
979 #ifndef PROFILE
980 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
981 NULL) == -1)
982 err(1, "pledge");
983 #endif
985 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
986 switch (ch) {
987 case 'c':
988 commit_id_str = optarg;
989 break;
990 case 'r':
991 repo_path = realpath(optarg, NULL);
992 if (repo_path == NULL)
993 err(1, "-r option");
994 break;
995 default:
996 usage();
997 /* NOTREACHED */
1001 argc -= optind;
1002 argv += optind;
1004 if (argc == 1)
1005 path = argv[0];
1006 else
1007 usage_blame();
1009 cwd = getcwd(NULL, 0);
1010 if (cwd == NULL) {
1011 error = got_error_from_errno();
1012 goto done;
1014 if (repo_path == NULL) {
1015 repo_path = strdup(cwd);
1016 if (repo_path == NULL) {
1017 error = got_error_from_errno();
1018 goto done;
1022 error = apply_unveil(repo_path, NULL);
1023 if (error)
1024 goto done;
1026 error = got_repo_open(&repo, repo_path);
1027 if (error != NULL)
1028 goto done;
1030 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1031 if (error != NULL)
1032 goto done;
1034 if (commit_id_str == NULL) {
1035 struct got_reference *head_ref;
1036 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1037 if (error != NULL)
1038 goto done;
1039 error = got_ref_resolve(&commit_id, repo, head_ref);
1040 got_ref_close(head_ref);
1041 if (error != NULL)
1042 goto done;
1043 } else {
1044 error = got_object_resolve_id_str(&commit_id, repo,
1045 commit_id_str);
1046 if (error != NULL)
1047 goto done;
1050 error = got_blame(in_repo_path, commit_id, repo, stdout);
1051 done:
1052 free(in_repo_path);
1053 free(repo_path);
1054 free(cwd);
1055 free(commit_id);
1056 if (repo) {
1057 const struct got_error *repo_error;
1058 repo_error = got_repo_close(repo);
1059 if (error == NULL)
1060 error = repo_error;
1062 return error;
1065 __dead static void
1066 usage_tree(void)
1068 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
1069 getprogname());
1070 exit(1);
1074 static const struct got_error *
1075 print_tree(const char *path, struct got_object_id *commit_id,
1076 int show_ids, struct got_repository *repo)
1078 const struct got_error *err = NULL;
1079 struct got_object_id *tree_id = NULL;
1080 struct got_tree_object *tree = NULL;
1081 const struct got_tree_entries *entries;
1082 struct got_tree_entry *te;
1084 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1085 if (err)
1086 goto done;
1088 err = got_object_open_as_tree(&tree, repo, tree_id);
1089 if (err)
1090 goto done;
1091 entries = got_object_tree_get_entries(tree);
1092 te = SIMPLEQ_FIRST(&entries->head);
1093 while (te) {
1094 char *id = NULL;
1096 if (sigint_received || sigpipe_received)
1097 break;
1099 if (show_ids) {
1100 char *id_str;
1101 err = got_object_id_str(&id_str, te->id);
1102 if (err)
1103 goto done;
1104 if (asprintf(&id, "%s ", id_str) == -1) {
1105 err = got_error_from_errno();
1106 free(id_str);
1107 goto done;
1109 free(id_str);
1111 printf("%s%s%s\n", id ? id : "",
1112 te->name, S_ISDIR(te->mode) ? "/" : "");
1113 te = SIMPLEQ_NEXT(te, entry);
1114 free(id);
1116 done:
1117 if (tree)
1118 got_object_tree_close(tree);
1119 free(tree_id);
1120 return err;
1123 static const struct got_error *
1124 cmd_tree(int argc, char *argv[])
1126 const struct got_error *error;
1127 struct got_repository *repo = NULL;
1128 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1129 struct got_object_id *commit_id = NULL;
1130 char *commit_id_str = NULL;
1131 int show_ids = 0;
1132 int ch;
1134 #ifndef PROFILE
1135 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1136 NULL) == -1)
1137 err(1, "pledge");
1138 #endif
1140 while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
1141 switch (ch) {
1142 case 'c':
1143 commit_id_str = optarg;
1144 break;
1145 case 'r':
1146 repo_path = realpath(optarg, NULL);
1147 if (repo_path == NULL)
1148 err(1, "-r option");
1149 break;
1150 case 'i':
1151 show_ids = 1;
1152 break;
1153 default:
1154 usage();
1155 /* NOTREACHED */
1159 argc -= optind;
1160 argv += optind;
1162 if (argc == 1)
1163 path = argv[0];
1164 else if (argc > 1)
1165 usage_tree();
1166 else
1167 path = "/";
1169 cwd = getcwd(NULL, 0);
1170 if (cwd == NULL) {
1171 error = got_error_from_errno();
1172 goto done;
1174 if (repo_path == NULL) {
1175 repo_path = strdup(cwd);
1176 if (repo_path == NULL) {
1177 error = got_error_from_errno();
1178 goto done;
1182 error = apply_unveil(repo_path, NULL);
1183 if (error)
1184 goto done;
1186 error = got_repo_open(&repo, repo_path);
1187 if (error != NULL)
1188 goto done;
1190 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1191 if (error != NULL)
1192 goto done;
1194 if (commit_id_str == NULL) {
1195 struct got_reference *head_ref;
1196 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1197 if (error != NULL)
1198 goto done;
1199 error = got_ref_resolve(&commit_id, repo, head_ref);
1200 got_ref_close(head_ref);
1201 if (error != NULL)
1202 goto done;
1203 } else {
1204 error = got_object_resolve_id_str(&commit_id, repo,
1205 commit_id_str);
1206 if (error != NULL)
1207 goto done;
1210 error = print_tree(in_repo_path, commit_id, show_ids, repo);
1211 done:
1212 free(in_repo_path);
1213 free(repo_path);
1214 free(cwd);
1215 free(commit_id);
1216 if (repo) {
1217 const struct got_error *repo_error;
1218 repo_error = got_repo_close(repo);
1219 if (error == NULL)
1220 error = repo_error;
1222 return error;
1225 #ifdef notyet
1226 static const struct got_error *
1227 cmd_status(int argc __unused, char *argv[] __unused)
1229 git_repository *repo = NULL;
1230 git_status_list *status;
1231 git_status_options statusopts;
1232 size_t i;
1234 git_libgit2_init();
1236 if (git_repository_open_ext(&repo, ".", 0, NULL))
1237 errx(1, "git_repository_open: %s", giterr_last()->message);
1239 if (git_repository_is_bare(repo))
1240 errx(1, "bar repository");
1242 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
1243 errx(1, "git_status_init_options: %s", giterr_last()->message);
1245 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
1246 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
1247 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
1248 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1250 if (git_status_list_new(&status, repo, &statusopts))
1251 errx(1, "git_status_list_new: %s", giterr_last()->message);
1253 for (i = 0; i < git_status_list_entrycount(status); i++) {
1254 const git_status_entry *se;
1256 se = git_status_byindex(status, i);
1257 switch (se->status) {
1258 case GIT_STATUS_WT_NEW:
1259 printf("? %s\n", se->index_to_workdir->new_file.path);
1260 break;
1261 case GIT_STATUS_WT_MODIFIED:
1262 printf("M %s\n", se->index_to_workdir->new_file.path);
1263 break;
1264 case GIT_STATUS_WT_DELETED:
1265 printf("R %s\n", se->index_to_workdir->new_file.path);
1266 break;
1267 case GIT_STATUS_WT_RENAMED:
1268 printf("m %s -> %s\n",
1269 se->index_to_workdir->old_file.path,
1270 se->index_to_workdir->new_file.path);
1271 break;
1272 case GIT_STATUS_WT_TYPECHANGE:
1273 printf("t %s\n", se->index_to_workdir->new_file.path);
1274 break;
1275 case GIT_STATUS_INDEX_NEW:
1276 printf("A %s\n", se->head_to_index->new_file.path);
1277 break;
1278 case GIT_STATUS_INDEX_MODIFIED:
1279 printf("M %s\n", se->head_to_index->old_file.path);
1280 break;
1281 case GIT_STATUS_INDEX_DELETED:
1282 printf("R %s\n", se->head_to_index->old_file.path);
1283 break;
1284 case GIT_STATUS_INDEX_RENAMED:
1285 printf("m %s -> %s\n",
1286 se->head_to_index->old_file.path,
1287 se->head_to_index->new_file.path);
1288 break;
1289 case GIT_STATUS_INDEX_TYPECHANGE:
1290 printf("t %s\n", se->head_to_index->old_file.path);
1291 break;
1292 case GIT_STATUS_CURRENT:
1293 default:
1294 break;
1298 git_status_list_free(status);
1299 git_repository_free(repo);
1300 git_libgit2_shutdown();
1302 return 0;
1304 #endif