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 char *repo_path = NULL;
426 struct got_object_id *commit_id = NULL;
427 char *commit_id_str = NULL;
428 int ch;
430 while ((ch = getopt(argc, argv, "c:")) != -1) {
431 switch (ch) {
432 case 'c':
433 commit_id_str = strdup(optarg);
434 if (commit_id_str == NULL)
435 return got_error_from_errno();
436 break;
437 default:
438 usage();
439 /* NOTREACHED */
443 argc -= optind;
444 argv += optind;
446 #ifndef PROFILE
447 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
448 NULL) == -1)
449 err(1, "pledge");
450 #endif
451 if (argc == 0) {
452 worktree_path = getcwd(NULL, 0);
453 if (worktree_path == NULL) {
454 error = got_error_from_errno();
455 goto done;
457 } else if (argc == 1) {
458 worktree_path = realpath(argv[0], NULL);
459 if (worktree_path == NULL) {
460 error = got_error_from_errno();
461 goto done;
463 } else
464 usage_update();
466 error = got_worktree_open(&worktree, worktree_path);
467 if (error != NULL)
468 goto done;
470 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
471 if (error != NULL)
472 goto done;
474 repo_path = got_repo_get_path(repo);
475 if (repo_path == NULL) {
476 error = got_error_from_errno();
477 goto done;
480 error = apply_unveil(repo_path, worktree_path);
481 if (error)
482 goto done;
484 if (commit_id_str == NULL) {
485 struct got_reference *head_ref;
486 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
487 if (error != NULL)
488 goto done;
489 error = got_ref_resolve(&commit_id, repo, head_ref);
490 if (error != NULL)
491 goto done;
492 error = got_object_id_str(&commit_id_str, commit_id);
493 if (error != NULL)
494 goto done;
495 } else {
496 error = got_object_resolve_id_str(&commit_id, repo,
497 commit_id_str);
498 if (error != NULL)
499 goto done;
502 error = check_ancestry(worktree, commit_id, repo);
503 if (error != NULL)
504 goto done;
506 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
507 commit_id) != 0) {
508 error = got_worktree_set_base_commit_id(worktree, repo,
509 commit_id);
510 if (error)
511 goto done;
514 error = got_worktree_checkout_files(worktree, repo,
515 update_progress, NULL, checkout_cancel, NULL);
516 if (error != NULL)
517 goto done;
519 printf("Updated to commit %s\n", commit_id_str);
520 done:
521 free(worktree_path);
522 free(commit_id);
523 free(commit_id_str);
524 free(repo_path);
525 return error;
528 static const struct got_error *
529 print_patch(struct got_commit_object *commit, struct got_object_id *id,
530 int diff_context, struct got_repository *repo)
532 const struct got_error *err = NULL;
533 struct got_tree_object *tree1 = NULL, *tree2;
534 struct got_object_qid *qid;
535 char *id_str1 = NULL, *id_str2;
537 err = got_object_open_as_tree(&tree2, repo,
538 got_object_commit_get_tree_id(commit));
539 if (err)
540 return err;
542 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
543 if (qid != NULL) {
544 struct got_commit_object *pcommit;
546 err = got_object_open_as_commit(&pcommit, repo, qid->id);
547 if (err)
548 return err;
550 err = got_object_open_as_tree(&tree1, repo,
551 got_object_commit_get_tree_id(pcommit));
552 got_object_commit_close(pcommit);
553 if (err)
554 return err;
556 err = got_object_id_str(&id_str1, qid->id);
557 if (err)
558 return err;
561 err = got_object_id_str(&id_str2, id);
562 if (err)
563 goto done;
565 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
566 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
567 done:
568 if (tree1)
569 got_object_tree_close(tree1);
570 got_object_tree_close(tree2);
571 free(id_str1);
572 free(id_str2);
573 return err;
576 static char *
577 get_datestr(time_t *time, char *datebuf)
579 char *p, *s = ctime_r(time, datebuf);
580 p = strchr(s, '\n');
581 if (p)
582 *p = '\0';
583 return s;
586 static const struct got_error *
587 print_commit(struct got_commit_object *commit, struct got_object_id *id,
588 struct got_repository *repo, int show_patch, int diff_context)
590 const struct got_error *err = NULL;
591 char *id_str, *datestr, *logmsg0, *logmsg, *line;
592 char datebuf[26];
593 time_t committer_time;
594 const char *author, *committer;
596 err = got_object_id_str(&id_str, id);
597 if (err)
598 return err;
600 printf("-----------------------------------------------\n");
601 printf("commit %s\n", id_str);
602 free(id_str);
603 printf("from: %s\n", got_object_commit_get_author(commit));
604 committer_time = got_object_commit_get_committer_time(commit);
605 datestr = get_datestr(&committer_time, datebuf);
606 printf("date: %s UTC\n", datestr);
607 author = got_object_commit_get_author(commit);
608 committer = got_object_commit_get_committer(commit);
609 if (strcmp(author, committer) != 0)
610 printf("via: %s\n", committer);
611 if (got_object_commit_get_nparents(commit) > 1) {
612 const struct got_object_id_queue *parent_ids;
613 struct got_object_qid *qid;
614 int n = 1;
615 parent_ids = got_object_commit_get_parent_ids(commit);
616 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
617 err = got_object_id_str(&id_str, qid->id);
618 if (err)
619 return err;
620 printf("parent %d: %s\n", n++, id_str);
621 free(id_str);
625 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
626 if (logmsg0 == NULL)
627 return got_error_from_errno();
629 logmsg = logmsg0;
630 do {
631 line = strsep(&logmsg, "\n");
632 if (line)
633 printf(" %s\n", line);
634 } while (line);
635 free(logmsg0);
637 if (show_patch) {
638 err = print_patch(commit, id, diff_context, repo);
639 if (err == 0)
640 printf("\n");
643 fflush(stdout);
644 return err;
647 static const struct got_error *
648 print_commits(struct got_object_id *root_id, struct got_repository *repo,
649 char *path, int show_patch, int diff_context, int limit,
650 int first_parent_traversal)
652 const struct got_error *err;
653 struct got_commit_graph *graph;
655 err = got_commit_graph_open(&graph, root_id, path,
656 first_parent_traversal, repo);
657 if (err)
658 return err;
659 err = got_commit_graph_iter_start(graph, root_id, repo);
660 if (err)
661 goto done;
662 while (1) {
663 struct got_commit_object *commit;
664 struct got_object_id *id;
666 if (sigint_received || sigpipe_received)
667 break;
669 err = got_commit_graph_iter_next(&id, graph);
670 if (err) {
671 if (err->code == GOT_ERR_ITER_COMPLETED) {
672 err = NULL;
673 break;
675 if (err->code != GOT_ERR_ITER_NEED_MORE)
676 break;
677 err = got_commit_graph_fetch_commits(graph, 1, repo);
678 if (err)
679 break;
680 else
681 continue;
683 if (id == NULL)
684 break;
686 err = got_object_open_as_commit(&commit, repo, id);
687 if (err)
688 break;
689 err = print_commit(commit, id, repo, show_patch, diff_context);
690 got_object_commit_close(commit);
691 if (err || (limit && --limit == 0))
692 break;
694 done:
695 got_commit_graph_close(graph);
696 return err;
699 __dead static void
700 usage_log(void)
702 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
703 "[-r repository-path] [path]\n", getprogname());
704 exit(1);
707 static const struct got_error *
708 cmd_log(int argc, char *argv[])
710 const struct got_error *error;
711 struct got_repository *repo = NULL;
712 struct got_commit_object *commit = NULL;
713 struct got_object_id *id = NULL;
714 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
715 char *start_commit = NULL;
716 int diff_context = 3, ch;
717 int show_patch = 0, limit = 0, first_parent_traversal = 0;
718 const char *errstr;
720 #ifndef PROFILE
721 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
722 NULL)
723 == -1)
724 err(1, "pledge");
725 #endif
727 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
728 switch (ch) {
729 case 'p':
730 show_patch = 1;
731 break;
732 case 'c':
733 start_commit = optarg;
734 break;
735 case 'C':
736 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
737 &errstr);
738 if (errstr != NULL)
739 err(1, "-C option %s", errstr);
740 break;
741 case 'l':
742 limit = strtonum(optarg, 1, INT_MAX, &errstr);
743 if (errstr != NULL)
744 err(1, "-l option %s", errstr);
745 break;
746 case 'f':
747 first_parent_traversal = 1;
748 break;
749 case 'r':
750 repo_path = realpath(optarg, NULL);
751 if (repo_path == NULL)
752 err(1, "-r option");
753 break;
754 default:
755 usage();
756 /* NOTREACHED */
760 argc -= optind;
761 argv += optind;
763 if (argc == 0)
764 path = strdup("");
765 else if (argc == 1)
766 path = strdup(argv[0]);
767 else
768 usage_log();
769 if (path == NULL)
770 return got_error_from_errno();
772 cwd = getcwd(NULL, 0);
773 if (cwd == NULL) {
774 error = got_error_from_errno();
775 goto done;
777 if (repo_path == NULL) {
778 repo_path = strdup(cwd);
779 if (repo_path == NULL) {
780 error = got_error_from_errno();
781 goto done;
785 error = apply_unveil(repo_path, NULL);
786 if (error)
787 goto done;
789 error = got_repo_open(&repo, repo_path);
790 if (error != NULL)
791 goto done;
793 if (start_commit == NULL) {
794 struct got_reference *head_ref;
795 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
796 if (error != NULL)
797 return error;
798 error = got_ref_resolve(&id, repo, head_ref);
799 got_ref_close(head_ref);
800 if (error != NULL)
801 return error;
802 error = got_object_open_as_commit(&commit, repo, id);
803 } else {
804 struct got_reference *ref;
805 error = got_ref_open(&ref, repo, start_commit);
806 if (error == NULL) {
807 error = got_ref_resolve(&id, repo, ref);
808 got_ref_close(ref);
809 if (error != NULL)
810 return error;
811 error = got_object_open_as_commit(&commit, repo, id);
812 if (error != NULL)
813 return error;
815 if (commit == NULL) {
816 error = got_object_resolve_id_str(&id, repo,
817 start_commit);
818 if (error != NULL)
819 return error;
822 if (error != NULL)
823 goto done;
825 error = got_repo_map_path(&in_repo_path, repo, path, 1);
826 if (error != NULL)
827 goto done;
828 if (in_repo_path) {
829 free(path);
830 path = in_repo_path;
833 error = print_commits(id, repo, path, show_patch,
834 diff_context, limit, first_parent_traversal);
835 done:
836 free(path);
837 free(repo_path);
838 free(cwd);
839 free(id);
840 if (repo) {
841 const struct got_error *repo_error;
842 repo_error = got_repo_close(repo);
843 if (error == NULL)
844 error = repo_error;
846 return error;
849 __dead static void
850 usage_diff(void)
852 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
853 "object1 object2\n", getprogname());
854 exit(1);
857 static const struct got_error *
858 cmd_diff(int argc, char *argv[])
860 const struct got_error *error;
861 struct got_repository *repo = NULL;
862 char *repo_path = NULL;
863 struct got_object_id *id1 = NULL, *id2 = NULL;
864 char *id_str1 = NULL, *id_str2 = NULL;
865 int type1, type2;
866 int diff_context = 3, ch;
867 const char *errstr;
869 #ifndef PROFILE
870 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
871 NULL) == -1)
872 err(1, "pledge");
873 #endif
875 while ((ch = getopt(argc, argv, "C:")) != -1) {
876 switch (ch) {
877 case 'C':
878 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
879 if (errstr != NULL)
880 err(1, "-C option %s", errstr);
881 break;
882 default:
883 usage();
884 /* NOTREACHED */
888 argc -= optind;
889 argv += optind;
891 if (argc == 0) {
892 usage_diff(); /* TODO show local worktree changes */
893 } else if (argc == 2) {
894 repo_path = getcwd(NULL, 0);
895 if (repo_path == NULL)
896 return got_error_from_errno();
897 id_str1 = argv[0];
898 id_str2 = argv[1];
899 } else if (argc == 3) {
900 repo_path = realpath(argv[0], NULL);
901 if (repo_path == NULL)
902 return got_error_from_errno();
903 id_str1 = argv[1];
904 id_str2 = argv[2];
905 } else
906 usage_diff();
908 error = apply_unveil(repo_path, NULL);
909 if (error)
910 goto done;
912 error = got_repo_open(&repo, repo_path);
913 free(repo_path);
914 if (error != NULL)
915 goto done;
917 error = got_object_resolve_id_str(&id1, repo, id_str1);
918 if (error)
919 goto done;
921 error = got_object_resolve_id_str(&id2, repo, id_str2);
922 if (error)
923 goto done;
925 error = got_object_get_type(&type1, repo, id1);
926 if (error)
927 goto done;
929 error = got_object_get_type(&type2, repo, id2);
930 if (error)
931 goto done;
933 if (type1 != type2) {
934 error = got_error(GOT_ERR_OBJ_TYPE);
935 goto done;
938 switch (type1) {
939 case GOT_OBJ_TYPE_BLOB:
940 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
941 diff_context, repo, stdout);
942 break;
943 case GOT_OBJ_TYPE_TREE:
944 error = got_diff_objects_as_trees(id1, id2, "", "",
945 diff_context, repo, stdout);
946 break;
947 case GOT_OBJ_TYPE_COMMIT:
948 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
949 id_str2);
950 error = got_diff_objects_as_commits(id1, id2, diff_context,
951 repo, stdout);
952 break;
953 default:
954 error = got_error(GOT_ERR_OBJ_TYPE);
957 done:
958 free(id1);
959 free(id2);
960 if (repo) {
961 const struct got_error *repo_error;
962 repo_error = got_repo_close(repo);
963 if (error == NULL)
964 error = repo_error;
966 return error;
969 __dead static void
970 usage_blame(void)
972 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
973 getprogname());
974 exit(1);
977 static const struct got_error *
978 cmd_blame(int argc, char *argv[])
980 const struct got_error *error;
981 struct got_repository *repo = NULL;
982 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
983 struct got_object_id *commit_id = NULL;
984 char *commit_id_str = NULL;
985 int ch;
987 #ifndef PROFILE
988 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
989 NULL) == -1)
990 err(1, "pledge");
991 #endif
993 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
994 switch (ch) {
995 case 'c':
996 commit_id_str = optarg;
997 break;
998 case 'r':
999 repo_path = realpath(optarg, NULL);
1000 if (repo_path == NULL)
1001 err(1, "-r option");
1002 break;
1003 default:
1004 usage();
1005 /* NOTREACHED */
1009 argc -= optind;
1010 argv += optind;
1012 if (argc == 1)
1013 path = argv[0];
1014 else
1015 usage_blame();
1017 cwd = getcwd(NULL, 0);
1018 if (cwd == NULL) {
1019 error = got_error_from_errno();
1020 goto done;
1022 if (repo_path == NULL) {
1023 repo_path = strdup(cwd);
1024 if (repo_path == NULL) {
1025 error = got_error_from_errno();
1026 goto done;
1030 error = apply_unveil(repo_path, NULL);
1031 if (error)
1032 goto done;
1034 error = got_repo_open(&repo, repo_path);
1035 if (error != NULL)
1036 goto done;
1038 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1039 if (error != NULL)
1040 goto done;
1042 if (commit_id_str == NULL) {
1043 struct got_reference *head_ref;
1044 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1045 if (error != NULL)
1046 goto done;
1047 error = got_ref_resolve(&commit_id, repo, head_ref);
1048 got_ref_close(head_ref);
1049 if (error != NULL)
1050 goto done;
1051 } else {
1052 error = got_object_resolve_id_str(&commit_id, repo,
1053 commit_id_str);
1054 if (error != NULL)
1055 goto done;
1058 error = got_blame(in_repo_path, commit_id, repo, stdout);
1059 done:
1060 free(in_repo_path);
1061 free(repo_path);
1062 free(cwd);
1063 free(commit_id);
1064 if (repo) {
1065 const struct got_error *repo_error;
1066 repo_error = got_repo_close(repo);
1067 if (error == NULL)
1068 error = repo_error;
1070 return error;
1073 __dead static void
1074 usage_tree(void)
1076 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
1077 getprogname());
1078 exit(1);
1082 static const struct got_error *
1083 print_tree(const char *path, struct got_object_id *commit_id,
1084 int show_ids, struct got_repository *repo)
1086 const struct got_error *err = NULL;
1087 struct got_object_id *tree_id = NULL;
1088 struct got_tree_object *tree = NULL;
1089 const struct got_tree_entries *entries;
1090 struct got_tree_entry *te;
1092 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1093 if (err)
1094 goto done;
1096 err = got_object_open_as_tree(&tree, repo, tree_id);
1097 if (err)
1098 goto done;
1099 entries = got_object_tree_get_entries(tree);
1100 te = SIMPLEQ_FIRST(&entries->head);
1101 while (te) {
1102 char *id = NULL;
1104 if (sigint_received || sigpipe_received)
1105 break;
1107 if (show_ids) {
1108 char *id_str;
1109 err = got_object_id_str(&id_str, te->id);
1110 if (err)
1111 goto done;
1112 if (asprintf(&id, "%s ", id_str) == -1) {
1113 err = got_error_from_errno();
1114 free(id_str);
1115 goto done;
1117 free(id_str);
1119 printf("%s%s%s\n", id ? id : "",
1120 te->name, S_ISDIR(te->mode) ? "/" : "");
1121 te = SIMPLEQ_NEXT(te, entry);
1122 free(id);
1124 done:
1125 if (tree)
1126 got_object_tree_close(tree);
1127 free(tree_id);
1128 return err;
1131 static const struct got_error *
1132 cmd_tree(int argc, char *argv[])
1134 const struct got_error *error;
1135 struct got_repository *repo = NULL;
1136 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1137 struct got_object_id *commit_id = NULL;
1138 char *commit_id_str = NULL;
1139 int show_ids = 0;
1140 int ch;
1142 #ifndef PROFILE
1143 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1144 NULL) == -1)
1145 err(1, "pledge");
1146 #endif
1148 while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
1149 switch (ch) {
1150 case 'c':
1151 commit_id_str = optarg;
1152 break;
1153 case 'r':
1154 repo_path = realpath(optarg, NULL);
1155 if (repo_path == NULL)
1156 err(1, "-r option");
1157 break;
1158 case 'i':
1159 show_ids = 1;
1160 break;
1161 default:
1162 usage();
1163 /* NOTREACHED */
1167 argc -= optind;
1168 argv += optind;
1170 if (argc == 1)
1171 path = argv[0];
1172 else if (argc > 1)
1173 usage_tree();
1174 else
1175 path = "/";
1177 cwd = getcwd(NULL, 0);
1178 if (cwd == NULL) {
1179 error = got_error_from_errno();
1180 goto done;
1182 if (repo_path == NULL) {
1183 repo_path = strdup(cwd);
1184 if (repo_path == NULL) {
1185 error = got_error_from_errno();
1186 goto done;
1190 error = apply_unveil(repo_path, NULL);
1191 if (error)
1192 goto done;
1194 error = got_repo_open(&repo, repo_path);
1195 if (error != NULL)
1196 goto done;
1198 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1199 if (error != NULL)
1200 goto done;
1202 if (commit_id_str == NULL) {
1203 struct got_reference *head_ref;
1204 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1205 if (error != NULL)
1206 goto done;
1207 error = got_ref_resolve(&commit_id, repo, head_ref);
1208 got_ref_close(head_ref);
1209 if (error != NULL)
1210 goto done;
1211 } else {
1212 error = got_object_resolve_id_str(&commit_id, repo,
1213 commit_id_str);
1214 if (error != NULL)
1215 goto done;
1218 error = print_tree(in_repo_path, commit_id, show_ids, repo);
1219 done:
1220 free(in_repo_path);
1221 free(repo_path);
1222 free(cwd);
1223 free(commit_id);
1224 if (repo) {
1225 const struct got_error *repo_error;
1226 repo_error = got_repo_close(repo);
1227 if (error == NULL)
1228 error = repo_error;
1230 return error;
1233 #ifdef notyet
1234 static const struct got_error *
1235 cmd_status(int argc __unused, char *argv[] __unused)
1237 git_repository *repo = NULL;
1238 git_status_list *status;
1239 git_status_options statusopts;
1240 size_t i;
1242 git_libgit2_init();
1244 if (git_repository_open_ext(&repo, ".", 0, NULL))
1245 errx(1, "git_repository_open: %s", giterr_last()->message);
1247 if (git_repository_is_bare(repo))
1248 errx(1, "bar repository");
1250 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
1251 errx(1, "git_status_init_options: %s", giterr_last()->message);
1253 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
1254 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
1255 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
1256 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1258 if (git_status_list_new(&status, repo, &statusopts))
1259 errx(1, "git_status_list_new: %s", giterr_last()->message);
1261 for (i = 0; i < git_status_list_entrycount(status); i++) {
1262 const git_status_entry *se;
1264 se = git_status_byindex(status, i);
1265 switch (se->status) {
1266 case GIT_STATUS_WT_NEW:
1267 printf("? %s\n", se->index_to_workdir->new_file.path);
1268 break;
1269 case GIT_STATUS_WT_MODIFIED:
1270 printf("M %s\n", se->index_to_workdir->new_file.path);
1271 break;
1272 case GIT_STATUS_WT_DELETED:
1273 printf("R %s\n", se->index_to_workdir->new_file.path);
1274 break;
1275 case GIT_STATUS_WT_RENAMED:
1276 printf("m %s -> %s\n",
1277 se->index_to_workdir->old_file.path,
1278 se->index_to_workdir->new_file.path);
1279 break;
1280 case GIT_STATUS_WT_TYPECHANGE:
1281 printf("t %s\n", se->index_to_workdir->new_file.path);
1282 break;
1283 case GIT_STATUS_INDEX_NEW:
1284 printf("A %s\n", se->head_to_index->new_file.path);
1285 break;
1286 case GIT_STATUS_INDEX_MODIFIED:
1287 printf("M %s\n", se->head_to_index->old_file.path);
1288 break;
1289 case GIT_STATUS_INDEX_DELETED:
1290 printf("R %s\n", se->head_to_index->old_file.path);
1291 break;
1292 case GIT_STATUS_INDEX_RENAMED:
1293 printf("m %s -> %s\n",
1294 se->head_to_index->old_file.path,
1295 se->head_to_index->new_file.path);
1296 break;
1297 case GIT_STATUS_INDEX_TYPECHANGE:
1298 printf("t %s\n", se->head_to_index->old_file.path);
1299 break;
1300 case GIT_STATUS_CURRENT:
1301 default:
1302 break;
1306 git_status_list_free(status);
1307 git_repository_free(repo);
1308 git_libgit2_shutdown();
1310 return 0;
1312 #endif