Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 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 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
230 struct got_repository *repo)
232 const struct got_error *err;
233 struct got_reference *head_ref = NULL;
234 struct got_object_id *head_commit_id = NULL;
235 struct got_commit_graph *graph = NULL;
237 head_ref = got_worktree_get_head_ref(worktree);
238 if (head_ref == NULL)
239 return got_error_from_errno();
241 /* TODO: Check the reflog. The head ref may have been rebased. */
242 err = got_ref_resolve(&head_commit_id, repo, head_ref);
243 if (err)
244 goto done;
246 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
247 if (err)
248 goto done;
250 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
251 if (err)
252 goto done;
253 while (1) {
254 struct got_object_id *id;
256 if (sigint_received || sigpipe_received)
257 break;
259 err = got_commit_graph_iter_next(&id, graph);
260 if (err) {
261 if (err->code == GOT_ERR_ITER_COMPLETED) {
262 err = got_error(GOT_ERR_ANCESTRY);
263 break;
265 if (err->code != GOT_ERR_ITER_NEED_MORE)
266 break;
267 err = got_commit_graph_fetch_commits(graph, 1, repo);
268 if (err)
269 break;
270 else
271 continue;
273 if (id == NULL)
274 break;
275 if (got_object_id_cmp(id, commit_id) == 0)
276 break;
278 done:
279 if (head_ref)
280 got_ref_close(head_ref);
281 if (graph)
282 got_commit_graph_close(graph);
283 return err;
287 static const struct got_error *
288 cmd_checkout(int argc, char *argv[])
290 const struct got_error *error = NULL;
291 struct got_repository *repo = NULL;
292 struct got_reference *head_ref = NULL;
293 struct got_worktree *worktree = NULL;
294 char *repo_path = NULL;
295 char *worktree_path = NULL;
296 const char *path_prefix = "";
297 char *commit_id_str = NULL;
298 int ch, same_path_prefix;
300 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
301 switch (ch) {
302 case 'c':
303 commit_id_str = strdup(optarg);
304 if (commit_id_str == NULL)
305 return got_error_from_errno();
306 break;
307 case 'p':
308 path_prefix = optarg;
309 break;
310 default:
311 usage();
312 /* NOTREACHED */
316 argc -= optind;
317 argv += optind;
319 #ifndef PROFILE
320 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
321 NULL) == -1)
322 err(1, "pledge");
323 #endif
324 if (argc == 1) {
325 char *cwd, *base, *dotgit;
326 repo_path = realpath(argv[0], NULL);
327 if (repo_path == NULL)
328 return got_error_from_errno();
329 cwd = getcwd(NULL, 0);
330 if (cwd == NULL) {
331 error = got_error_from_errno();
332 goto done;
334 if (path_prefix[0])
335 base = basename(path_prefix);
336 else
337 base = basename(repo_path);
338 if (base == NULL) {
339 error = got_error_from_errno();
340 goto done;
342 dotgit = strstr(base, ".git");
343 if (dotgit)
344 *dotgit = '\0';
345 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
346 error = got_error_from_errno();
347 free(cwd);
348 goto done;
350 free(cwd);
351 } else if (argc == 2) {
352 repo_path = realpath(argv[0], NULL);
353 if (repo_path == NULL) {
354 error = got_error_from_errno();
355 goto done;
357 worktree_path = realpath(argv[1], NULL);
358 if (worktree_path == NULL) {
359 error = got_error_from_errno();
360 goto done;
362 } else
363 usage_checkout();
365 error = apply_unveil(repo_path, worktree_path);
366 if (error)
367 goto done;
369 error = got_repo_open(&repo, repo_path);
370 if (error != NULL)
371 goto done;
373 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
374 if (error != NULL)
375 goto done;
377 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
378 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
379 goto done;
381 error = got_worktree_open(&worktree, worktree_path);
382 if (error != NULL)
383 goto done;
385 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
386 path_prefix);
387 if (error != NULL)
388 goto done;
389 if (!same_path_prefix) {
390 error = got_error(GOT_ERR_PATH_PREFIX);
391 goto done;
394 if (commit_id_str) {
395 struct got_object_id *commit_id;
396 error = got_object_resolve_id_str(&commit_id, repo,
397 commit_id_str);
398 if (error != NULL)
399 goto done;
400 error = check_ancestry(worktree, commit_id, repo);
401 if (error != NULL) {
402 free(commit_id);
403 goto done;
405 error = got_worktree_set_base_commit_id(worktree, repo,
406 commit_id);
407 free(commit_id);
408 if (error)
409 goto done;
412 error = got_worktree_checkout_files(worktree, repo,
413 checkout_progress, worktree_path, checkout_cancel, NULL);
414 if (error != NULL)
415 goto done;
417 printf("Now shut up and hack\n");
419 done:
420 free(commit_id_str);
421 free(repo_path);
422 free(worktree_path);
423 return error;
426 __dead static void
427 usage_update(void)
429 fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
430 getprogname());
431 exit(1);
434 static void
435 update_progress(void *arg, unsigned char status, const char *path)
437 int *did_something = arg;
439 if (status == GOT_STATUS_EXISTS)
440 return;
442 *did_something = 1;
443 while (path[0] == '/')
444 path++;
445 printf("%c %s\n", status, path);
448 static const struct got_error *
449 cmd_update(int argc, char *argv[])
451 const struct got_error *error = NULL;
452 struct got_repository *repo = NULL;
453 struct got_worktree *worktree = NULL;
454 char *worktree_path = NULL;
455 struct got_object_id *commit_id = NULL;
456 char *commit_id_str = NULL;
457 int ch, did_something = 0;
459 while ((ch = getopt(argc, argv, "c:")) != -1) {
460 switch (ch) {
461 case 'c':
462 commit_id_str = strdup(optarg);
463 if (commit_id_str == NULL)
464 return got_error_from_errno();
465 break;
466 default:
467 usage();
468 /* NOTREACHED */
472 argc -= optind;
473 argv += optind;
475 #ifndef PROFILE
476 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
477 NULL) == -1)
478 err(1, "pledge");
479 #endif
480 if (argc == 0) {
481 worktree_path = getcwd(NULL, 0);
482 if (worktree_path == NULL) {
483 error = got_error_from_errno();
484 goto done;
486 } else if (argc == 1) {
487 worktree_path = realpath(argv[0], NULL);
488 if (worktree_path == NULL) {
489 error = got_error_from_errno();
490 goto done;
492 } else
493 usage_update();
495 error = got_worktree_open(&worktree, worktree_path);
496 if (error != NULL)
497 goto done;
499 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
500 if (error != NULL)
501 goto done;
503 error = apply_unveil(got_repo_get_path(repo), worktree_path);
504 if (error)
505 goto done;
507 if (commit_id_str == NULL) {
508 struct got_reference *head_ref;
509 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
510 if (error != NULL)
511 goto done;
512 error = got_ref_resolve(&commit_id, repo, head_ref);
513 if (error != NULL)
514 goto done;
515 error = got_object_id_str(&commit_id_str, commit_id);
516 if (error != NULL)
517 goto done;
518 } else {
519 error = got_object_resolve_id_str(&commit_id, repo,
520 commit_id_str);
521 if (error != NULL)
522 goto done;
525 error = check_ancestry(worktree, commit_id, repo);
526 if (error != NULL)
527 goto done;
529 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
530 commit_id) != 0) {
531 error = got_worktree_set_base_commit_id(worktree, repo,
532 commit_id);
533 if (error)
534 goto done;
537 error = got_worktree_checkout_files(worktree, repo,
538 update_progress, &did_something, checkout_cancel, NULL);
539 if (error != NULL)
540 goto done;
542 if (did_something)
543 printf("Updated to commit %s\n", commit_id_str);
544 else
545 printf("Already up-to-date\n");
546 done:
547 free(worktree_path);
548 free(commit_id);
549 free(commit_id_str);
550 return error;
553 static const struct got_error *
554 print_patch(struct got_commit_object *commit, struct got_object_id *id,
555 int diff_context, struct got_repository *repo)
557 const struct got_error *err = NULL;
558 struct got_tree_object *tree1 = NULL, *tree2;
559 struct got_object_qid *qid;
560 char *id_str1 = NULL, *id_str2;
562 err = got_object_open_as_tree(&tree2, repo,
563 got_object_commit_get_tree_id(commit));
564 if (err)
565 return err;
567 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
568 if (qid != NULL) {
569 struct got_commit_object *pcommit;
571 err = got_object_open_as_commit(&pcommit, repo, qid->id);
572 if (err)
573 return err;
575 err = got_object_open_as_tree(&tree1, repo,
576 got_object_commit_get_tree_id(pcommit));
577 got_object_commit_close(pcommit);
578 if (err)
579 return err;
581 err = got_object_id_str(&id_str1, qid->id);
582 if (err)
583 return err;
586 err = got_object_id_str(&id_str2, id);
587 if (err)
588 goto done;
590 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
591 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
592 done:
593 if (tree1)
594 got_object_tree_close(tree1);
595 got_object_tree_close(tree2);
596 free(id_str1);
597 free(id_str2);
598 return err;
601 static char *
602 get_datestr(time_t *time, char *datebuf)
604 char *p, *s = ctime_r(time, datebuf);
605 p = strchr(s, '\n');
606 if (p)
607 *p = '\0';
608 return s;
611 static const struct got_error *
612 print_commit(struct got_commit_object *commit, struct got_object_id *id,
613 struct got_repository *repo, int show_patch, int diff_context,
614 struct got_reflist_head *refs)
616 const struct got_error *err = NULL;
617 char *id_str, *datestr, *logmsg0, *logmsg, *line;
618 char datebuf[26];
619 time_t committer_time;
620 const char *author, *committer;
621 char *refs_str = NULL;
622 struct got_reflist_entry *re;
624 SIMPLEQ_FOREACH(re, refs, entry) {
625 char *s;
626 const char *name;
627 if (got_object_id_cmp(re->id, id) != 0)
628 continue;
629 name = got_ref_get_name(re->ref);
630 if (strncmp(name, "refs/", 5) == 0)
631 name += 5;
632 if (strncmp(name, "heads/", 6) == 0)
633 name += 6;
634 s = refs_str;
635 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
636 name) == -1) {
637 err = got_error_from_errno();
638 free(s);
639 break;
641 free(s);
643 err = got_object_id_str(&id_str, id);
644 if (err)
645 return err;
647 printf("-----------------------------------------------\n");
648 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
649 refs_str ? refs_str : "", refs_str ? ")" : "");
650 free(id_str);
651 printf("from: %s\n", got_object_commit_get_author(commit));
652 committer_time = got_object_commit_get_committer_time(commit);
653 datestr = get_datestr(&committer_time, datebuf);
654 printf("date: %s UTC\n", datestr);
655 author = got_object_commit_get_author(commit);
656 committer = got_object_commit_get_committer(commit);
657 if (strcmp(author, committer) != 0)
658 printf("via: %s\n", committer);
659 if (got_object_commit_get_nparents(commit) > 1) {
660 const struct got_object_id_queue *parent_ids;
661 struct got_object_qid *qid;
662 int n = 1;
663 parent_ids = got_object_commit_get_parent_ids(commit);
664 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
665 err = got_object_id_str(&id_str, qid->id);
666 if (err)
667 return err;
668 printf("parent %d: %s\n", n++, id_str);
669 free(id_str);
673 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
674 if (logmsg0 == NULL)
675 return got_error_from_errno();
677 logmsg = logmsg0;
678 do {
679 line = strsep(&logmsg, "\n");
680 if (line)
681 printf(" %s\n", line);
682 } while (line);
683 free(logmsg0);
685 if (show_patch) {
686 err = print_patch(commit, id, diff_context, repo);
687 if (err == 0)
688 printf("\n");
691 fflush(stdout);
692 return err;
695 static const struct got_error *
696 print_commits(struct got_object_id *root_id, struct got_repository *repo,
697 char *path, int show_patch, int diff_context, int limit,
698 int first_parent_traversal, struct got_reflist_head *refs)
700 const struct got_error *err;
701 struct got_commit_graph *graph;
703 err = got_commit_graph_open(&graph, root_id, path,
704 first_parent_traversal, repo);
705 if (err)
706 return err;
707 err = got_commit_graph_iter_start(graph, root_id, repo);
708 if (err)
709 goto done;
710 while (1) {
711 struct got_commit_object *commit;
712 struct got_object_id *id;
714 if (sigint_received || sigpipe_received)
715 break;
717 err = got_commit_graph_iter_next(&id, graph);
718 if (err) {
719 if (err->code == GOT_ERR_ITER_COMPLETED) {
720 err = NULL;
721 break;
723 if (err->code != GOT_ERR_ITER_NEED_MORE)
724 break;
725 err = got_commit_graph_fetch_commits(graph, 1, repo);
726 if (err)
727 break;
728 else
729 continue;
731 if (id == NULL)
732 break;
734 err = got_object_open_as_commit(&commit, repo, id);
735 if (err)
736 break;
737 err = print_commit(commit, id, repo, show_patch, diff_context,
738 refs);
739 got_object_commit_close(commit);
740 if (err || (limit && --limit == 0))
741 break;
743 done:
744 got_commit_graph_close(graph);
745 return err;
748 __dead static void
749 usage_log(void)
751 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
752 "[-r repository-path] [path]\n", getprogname());
753 exit(1);
756 static const struct got_error *
757 cmd_log(int argc, char *argv[])
759 const struct got_error *error;
760 struct got_repository *repo = NULL;
761 struct got_commit_object *commit = NULL;
762 struct got_object_id *id = NULL;
763 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
764 char *start_commit = NULL;
765 int diff_context = 3, ch;
766 int show_patch = 0, limit = 0, first_parent_traversal = 0;
767 const char *errstr;
768 struct got_reflist_head refs;
770 #ifndef PROFILE
771 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
772 NULL)
773 == -1)
774 err(1, "pledge");
775 #endif
777 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
778 switch (ch) {
779 case 'p':
780 show_patch = 1;
781 break;
782 case 'c':
783 start_commit = optarg;
784 break;
785 case 'C':
786 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
787 &errstr);
788 if (errstr != NULL)
789 err(1, "-C option %s", errstr);
790 break;
791 case 'l':
792 limit = strtonum(optarg, 1, INT_MAX, &errstr);
793 if (errstr != NULL)
794 err(1, "-l option %s", errstr);
795 break;
796 case 'f':
797 first_parent_traversal = 1;
798 break;
799 case 'r':
800 repo_path = realpath(optarg, NULL);
801 if (repo_path == NULL)
802 err(1, "-r option");
803 break;
804 default:
805 usage();
806 /* NOTREACHED */
810 argc -= optind;
811 argv += optind;
813 if (argc == 0)
814 path = strdup("");
815 else if (argc == 1)
816 path = strdup(argv[0]);
817 else
818 usage_log();
819 if (path == NULL)
820 return got_error_from_errno();
822 cwd = getcwd(NULL, 0);
823 if (cwd == NULL) {
824 error = got_error_from_errno();
825 goto done;
827 if (repo_path == NULL) {
828 repo_path = strdup(cwd);
829 if (repo_path == NULL) {
830 error = got_error_from_errno();
831 goto done;
835 error = apply_unveil(repo_path, NULL);
836 if (error)
837 goto done;
839 error = got_repo_open(&repo, repo_path);
840 if (error != NULL)
841 goto done;
843 if (start_commit == NULL) {
844 struct got_reference *head_ref;
845 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
846 if (error != NULL)
847 return error;
848 error = got_ref_resolve(&id, repo, head_ref);
849 got_ref_close(head_ref);
850 if (error != NULL)
851 return error;
852 error = got_object_open_as_commit(&commit, repo, id);
853 } else {
854 struct got_reference *ref;
855 error = got_ref_open(&ref, repo, start_commit);
856 if (error == NULL) {
857 int obj_type;
858 error = got_ref_resolve(&id, repo, ref);
859 got_ref_close(ref);
860 if (error != NULL)
861 goto done;
862 error = got_object_get_type(&obj_type, repo, id);
863 if (error != NULL)
864 goto done;
865 if (obj_type == GOT_OBJ_TYPE_TAG) {
866 struct got_tag_object *tag;
867 error = got_object_open_as_tag(&tag, repo, id);
868 if (error != NULL)
869 goto done;
870 if (got_object_tag_get_object_type(tag) !=
871 GOT_OBJ_TYPE_COMMIT) {
872 got_object_tag_close(tag);
873 error = got_error(GOT_ERR_OBJ_TYPE);
874 goto done;
876 free(id);
877 id = got_object_id_dup(
878 got_object_tag_get_object_id(tag));
879 if (id == NULL)
880 error = got_error_from_errno();
881 got_object_tag_close(tag);
882 if (error)
883 goto done;
884 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
885 error = got_error(GOT_ERR_OBJ_TYPE);
886 goto done;
888 error = got_object_open_as_commit(&commit, repo, id);
889 if (error != NULL)
890 goto done;
892 if (commit == NULL) {
893 error = got_object_resolve_id_str(&id, repo,
894 start_commit);
895 if (error != NULL)
896 return error;
899 if (error != NULL)
900 goto done;
902 error = got_repo_map_path(&in_repo_path, repo, path, 1);
903 if (error != NULL)
904 goto done;
905 if (in_repo_path) {
906 free(path);
907 path = in_repo_path;
910 SIMPLEQ_INIT(&refs);
911 error = got_ref_list(&refs, repo);
912 if (error)
913 goto done;
915 error = print_commits(id, repo, path, show_patch,
916 diff_context, limit, first_parent_traversal, &refs);
917 done:
918 free(path);
919 free(repo_path);
920 free(cwd);
921 free(id);
922 if (repo) {
923 const struct got_error *repo_error;
924 repo_error = got_repo_close(repo);
925 if (error == NULL)
926 error = repo_error;
928 return error;
931 __dead static void
932 usage_diff(void)
934 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
935 "object1 object2\n", getprogname());
936 exit(1);
939 static const struct got_error *
940 cmd_diff(int argc, char *argv[])
942 const struct got_error *error;
943 struct got_repository *repo = NULL;
944 char *repo_path = NULL;
945 struct got_object_id *id1 = NULL, *id2 = NULL;
946 char *id_str1 = NULL, *id_str2 = NULL;
947 int type1, type2;
948 int diff_context = 3, ch;
949 const char *errstr;
951 #ifndef PROFILE
952 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
953 NULL) == -1)
954 err(1, "pledge");
955 #endif
957 while ((ch = getopt(argc, argv, "C:")) != -1) {
958 switch (ch) {
959 case 'C':
960 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
961 if (errstr != NULL)
962 err(1, "-C option %s", errstr);
963 break;
964 default:
965 usage();
966 /* NOTREACHED */
970 argc -= optind;
971 argv += optind;
973 if (argc == 0) {
974 usage_diff(); /* TODO show local worktree changes */
975 } else if (argc == 2) {
976 repo_path = getcwd(NULL, 0);
977 if (repo_path == NULL)
978 return got_error_from_errno();
979 id_str1 = argv[0];
980 id_str2 = argv[1];
981 } else if (argc == 3) {
982 repo_path = realpath(argv[0], NULL);
983 if (repo_path == NULL)
984 return got_error_from_errno();
985 id_str1 = argv[1];
986 id_str2 = argv[2];
987 } else
988 usage_diff();
990 error = apply_unveil(repo_path, NULL);
991 if (error)
992 goto done;
994 error = got_repo_open(&repo, repo_path);
995 free(repo_path);
996 if (error != NULL)
997 goto done;
999 error = got_object_resolve_id_str(&id1, repo, id_str1);
1000 if (error)
1001 goto done;
1003 error = got_object_resolve_id_str(&id2, repo, id_str2);
1004 if (error)
1005 goto done;
1007 error = got_object_get_type(&type1, repo, id1);
1008 if (error)
1009 goto done;
1011 error = got_object_get_type(&type2, repo, id2);
1012 if (error)
1013 goto done;
1015 if (type1 != type2) {
1016 error = got_error(GOT_ERR_OBJ_TYPE);
1017 goto done;
1020 switch (type1) {
1021 case GOT_OBJ_TYPE_BLOB:
1022 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1023 diff_context, repo, stdout);
1024 break;
1025 case GOT_OBJ_TYPE_TREE:
1026 error = got_diff_objects_as_trees(id1, id2, "", "",
1027 diff_context, repo, stdout);
1028 break;
1029 case GOT_OBJ_TYPE_COMMIT:
1030 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1031 id_str2);
1032 error = got_diff_objects_as_commits(id1, id2, diff_context,
1033 repo, stdout);
1034 break;
1035 default:
1036 error = got_error(GOT_ERR_OBJ_TYPE);
1039 done:
1040 free(id1);
1041 free(id2);
1042 if (repo) {
1043 const struct got_error *repo_error;
1044 repo_error = got_repo_close(repo);
1045 if (error == NULL)
1046 error = repo_error;
1048 return error;
1051 __dead static void
1052 usage_blame(void)
1054 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1055 getprogname());
1056 exit(1);
1059 static const struct got_error *
1060 cmd_blame(int argc, char *argv[])
1062 const struct got_error *error;
1063 struct got_repository *repo = NULL;
1064 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1065 struct got_object_id *commit_id = NULL;
1066 char *commit_id_str = NULL;
1067 int ch;
1069 #ifndef PROFILE
1070 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1071 NULL) == -1)
1072 err(1, "pledge");
1073 #endif
1075 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1076 switch (ch) {
1077 case 'c':
1078 commit_id_str = optarg;
1079 break;
1080 case 'r':
1081 repo_path = realpath(optarg, NULL);
1082 if (repo_path == NULL)
1083 err(1, "-r option");
1084 break;
1085 default:
1086 usage();
1087 /* NOTREACHED */
1091 argc -= optind;
1092 argv += optind;
1094 if (argc == 1)
1095 path = argv[0];
1096 else
1097 usage_blame();
1099 cwd = getcwd(NULL, 0);
1100 if (cwd == NULL) {
1101 error = got_error_from_errno();
1102 goto done;
1104 if (repo_path == NULL) {
1105 repo_path = strdup(cwd);
1106 if (repo_path == NULL) {
1107 error = got_error_from_errno();
1108 goto done;
1112 error = apply_unveil(repo_path, NULL);
1113 if (error)
1114 goto done;
1116 error = got_repo_open(&repo, repo_path);
1117 if (error != NULL)
1118 goto done;
1120 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1121 if (error != NULL)
1122 goto done;
1124 if (commit_id_str == NULL) {
1125 struct got_reference *head_ref;
1126 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1127 if (error != NULL)
1128 goto done;
1129 error = got_ref_resolve(&commit_id, repo, head_ref);
1130 got_ref_close(head_ref);
1131 if (error != NULL)
1132 goto done;
1133 } else {
1134 error = got_object_resolve_id_str(&commit_id, repo,
1135 commit_id_str);
1136 if (error != NULL)
1137 goto done;
1140 error = got_blame(in_repo_path, commit_id, repo, stdout);
1141 done:
1142 free(in_repo_path);
1143 free(repo_path);
1144 free(cwd);
1145 free(commit_id);
1146 if (repo) {
1147 const struct got_error *repo_error;
1148 repo_error = got_repo_close(repo);
1149 if (error == NULL)
1150 error = repo_error;
1152 return error;
1155 __dead static void
1156 usage_tree(void)
1158 fprintf(stderr,
1159 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1160 getprogname());
1161 exit(1);
1164 static void
1165 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1166 const char *root_path)
1168 int is_root_path = (strcmp(path, root_path) == 0);
1170 path += strlen(root_path);
1171 while (path[0] == '/')
1172 path++;
1174 printf("%s%s%s%s%s\n", id ? id : "", path,
1175 is_root_path ? "" : "/",
1176 te->name, S_ISDIR(te->mode) ? "/" : "");
1179 static const struct got_error *
1180 print_tree(const char *path, struct got_object_id *commit_id,
1181 int show_ids, int recurse, const char *root_path,
1182 struct got_repository *repo)
1184 const struct got_error *err = NULL;
1185 struct got_object_id *tree_id = NULL;
1186 struct got_tree_object *tree = NULL;
1187 const struct got_tree_entries *entries;
1188 struct got_tree_entry *te;
1190 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1191 if (err)
1192 goto done;
1194 err = got_object_open_as_tree(&tree, repo, tree_id);
1195 if (err)
1196 goto done;
1197 entries = got_object_tree_get_entries(tree);
1198 te = SIMPLEQ_FIRST(&entries->head);
1199 while (te) {
1200 char *id = NULL;
1202 if (sigint_received || sigpipe_received)
1203 break;
1205 if (show_ids) {
1206 char *id_str;
1207 err = got_object_id_str(&id_str, te->id);
1208 if (err)
1209 goto done;
1210 if (asprintf(&id, "%s ", id_str) == -1) {
1211 err = got_error_from_errno();
1212 free(id_str);
1213 goto done;
1215 free(id_str);
1217 print_entry(te, id, path, root_path);
1218 free(id);
1220 if (recurse && S_ISDIR(te->mode)) {
1221 char *child_path;
1222 if (asprintf(&child_path, "%s%s%s", path,
1223 path[0] == '/' && path[1] == '\0' ? "" : "/",
1224 te->name) == -1) {
1225 err = got_error_from_errno();
1226 goto done;
1228 err = print_tree(child_path, commit_id, show_ids, 1,
1229 root_path, repo);
1230 free(child_path);
1231 if (err)
1232 goto done;
1235 te = SIMPLEQ_NEXT(te, entry);
1237 done:
1238 if (tree)
1239 got_object_tree_close(tree);
1240 free(tree_id);
1241 return err;
1244 static const struct got_error *
1245 cmd_tree(int argc, char *argv[])
1247 const struct got_error *error;
1248 struct got_repository *repo = NULL;
1249 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1250 struct got_object_id *commit_id = NULL;
1251 char *commit_id_str = NULL;
1252 int show_ids = 0, recurse = 0;
1253 int ch;
1255 #ifndef PROFILE
1256 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1257 NULL) == -1)
1258 err(1, "pledge");
1259 #endif
1261 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1262 switch (ch) {
1263 case 'c':
1264 commit_id_str = optarg;
1265 break;
1266 case 'r':
1267 repo_path = realpath(optarg, NULL);
1268 if (repo_path == NULL)
1269 err(1, "-r option");
1270 break;
1271 case 'i':
1272 show_ids = 1;
1273 break;
1274 case 'R':
1275 recurse = 1;
1276 break;
1277 default:
1278 usage();
1279 /* NOTREACHED */
1283 argc -= optind;
1284 argv += optind;
1286 if (argc == 1)
1287 path = argv[0];
1288 else if (argc > 1)
1289 usage_tree();
1290 else
1291 path = "/";
1293 cwd = getcwd(NULL, 0);
1294 if (cwd == NULL) {
1295 error = got_error_from_errno();
1296 goto done;
1298 if (repo_path == NULL) {
1299 repo_path = strdup(cwd);
1300 if (repo_path == NULL) {
1301 error = got_error_from_errno();
1302 goto done;
1306 error = apply_unveil(repo_path, NULL);
1307 if (error)
1308 goto done;
1310 error = got_repo_open(&repo, repo_path);
1311 if (error != NULL)
1312 goto done;
1314 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1315 if (error != NULL)
1316 goto done;
1318 if (commit_id_str == NULL) {
1319 struct got_reference *head_ref;
1320 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1321 if (error != NULL)
1322 goto done;
1323 error = got_ref_resolve(&commit_id, repo, head_ref);
1324 got_ref_close(head_ref);
1325 if (error != NULL)
1326 goto done;
1327 } else {
1328 error = got_object_resolve_id_str(&commit_id, repo,
1329 commit_id_str);
1330 if (error != NULL)
1331 goto done;
1334 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1335 in_repo_path, repo);
1336 done:
1337 free(in_repo_path);
1338 free(repo_path);
1339 free(cwd);
1340 free(commit_id);
1341 if (repo) {
1342 const struct got_error *repo_error;
1343 repo_error = got_repo_close(repo);
1344 if (error == NULL)
1345 error = repo_error;
1347 return error;
1350 #ifdef notyet
1351 static const struct got_error *
1352 cmd_status(int argc __unused, char *argv[] __unused)
1354 git_repository *repo = NULL;
1355 git_status_list *status;
1356 git_status_options statusopts;
1357 size_t i;
1359 git_libgit2_init();
1361 if (git_repository_open_ext(&repo, ".", 0, NULL))
1362 errx(1, "git_repository_open: %s", giterr_last()->message);
1364 if (git_repository_is_bare(repo))
1365 errx(1, "bar repository");
1367 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
1368 errx(1, "git_status_init_options: %s", giterr_last()->message);
1370 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
1371 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
1372 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
1373 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1375 if (git_status_list_new(&status, repo, &statusopts))
1376 errx(1, "git_status_list_new: %s", giterr_last()->message);
1378 for (i = 0; i < git_status_list_entrycount(status); i++) {
1379 const git_status_entry *se;
1381 se = git_status_byindex(status, i);
1382 switch (se->status) {
1383 case GIT_STATUS_WT_NEW:
1384 printf("? %s\n", se->index_to_workdir->new_file.path);
1385 break;
1386 case GIT_STATUS_WT_MODIFIED:
1387 printf("M %s\n", se->index_to_workdir->new_file.path);
1388 break;
1389 case GIT_STATUS_WT_DELETED:
1390 printf("R %s\n", se->index_to_workdir->new_file.path);
1391 break;
1392 case GIT_STATUS_WT_RENAMED:
1393 printf("m %s -> %s\n",
1394 se->index_to_workdir->old_file.path,
1395 se->index_to_workdir->new_file.path);
1396 break;
1397 case GIT_STATUS_WT_TYPECHANGE:
1398 printf("t %s\n", se->index_to_workdir->new_file.path);
1399 break;
1400 case GIT_STATUS_INDEX_NEW:
1401 printf("A %s\n", se->head_to_index->new_file.path);
1402 break;
1403 case GIT_STATUS_INDEX_MODIFIED:
1404 printf("M %s\n", se->head_to_index->old_file.path);
1405 break;
1406 case GIT_STATUS_INDEX_DELETED:
1407 printf("R %s\n", se->head_to_index->old_file.path);
1408 break;
1409 case GIT_STATUS_INDEX_RENAMED:
1410 printf("m %s -> %s\n",
1411 se->head_to_index->old_file.path,
1412 se->head_to_index->new_file.path);
1413 break;
1414 case GIT_STATUS_INDEX_TYPECHANGE:
1415 printf("t %s\n", se->head_to_index->old_file.path);
1416 break;
1417 case GIT_STATUS_CURRENT:
1418 default:
1419 break;
1423 git_status_list_free(status);
1424 git_repository_free(repo);
1425 git_libgit2_shutdown();
1427 return 0;
1429 #endif