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_CTYPE, "");
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 if (strncmp(name, "remotes/", 8) == 0)
635 name += 8;
636 s = refs_str;
637 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
638 name) == -1) {
639 err = got_error_from_errno();
640 free(s);
641 break;
643 free(s);
645 err = got_object_id_str(&id_str, id);
646 if (err)
647 return err;
649 printf("-----------------------------------------------\n");
650 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
651 refs_str ? refs_str : "", refs_str ? ")" : "");
652 free(id_str);
653 printf("from: %s\n", got_object_commit_get_author(commit));
654 committer_time = got_object_commit_get_committer_time(commit);
655 datestr = get_datestr(&committer_time, datebuf);
656 printf("date: %s UTC\n", datestr);
657 author = got_object_commit_get_author(commit);
658 committer = got_object_commit_get_committer(commit);
659 if (strcmp(author, committer) != 0)
660 printf("via: %s\n", committer);
661 if (got_object_commit_get_nparents(commit) > 1) {
662 const struct got_object_id_queue *parent_ids;
663 struct got_object_qid *qid;
664 int n = 1;
665 parent_ids = got_object_commit_get_parent_ids(commit);
666 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
667 err = got_object_id_str(&id_str, qid->id);
668 if (err)
669 return err;
670 printf("parent %d: %s\n", n++, id_str);
671 free(id_str);
675 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
676 if (logmsg0 == NULL)
677 return got_error_from_errno();
679 logmsg = logmsg0;
680 do {
681 line = strsep(&logmsg, "\n");
682 if (line)
683 printf(" %s\n", line);
684 } while (line);
685 free(logmsg0);
687 if (show_patch) {
688 err = print_patch(commit, id, diff_context, repo);
689 if (err == 0)
690 printf("\n");
693 fflush(stdout);
694 return err;
697 static const struct got_error *
698 print_commits(struct got_object_id *root_id, struct got_repository *repo,
699 char *path, int show_patch, int diff_context, int limit,
700 int first_parent_traversal, struct got_reflist_head *refs)
702 const struct got_error *err;
703 struct got_commit_graph *graph;
705 err = got_commit_graph_open(&graph, root_id, path,
706 first_parent_traversal, repo);
707 if (err)
708 return err;
709 err = got_commit_graph_iter_start(graph, root_id, repo);
710 if (err)
711 goto done;
712 while (1) {
713 struct got_commit_object *commit;
714 struct got_object_id *id;
716 if (sigint_received || sigpipe_received)
717 break;
719 err = got_commit_graph_iter_next(&id, graph);
720 if (err) {
721 if (err->code == GOT_ERR_ITER_COMPLETED) {
722 err = NULL;
723 break;
725 if (err->code != GOT_ERR_ITER_NEED_MORE)
726 break;
727 err = got_commit_graph_fetch_commits(graph, 1, repo);
728 if (err)
729 break;
730 else
731 continue;
733 if (id == NULL)
734 break;
736 err = got_object_open_as_commit(&commit, repo, id);
737 if (err)
738 break;
739 err = print_commit(commit, id, repo, show_patch, diff_context,
740 refs);
741 got_object_commit_close(commit);
742 if (err || (limit && --limit == 0))
743 break;
745 done:
746 got_commit_graph_close(graph);
747 return err;
750 __dead static void
751 usage_log(void)
753 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
754 "[-r repository-path] [path]\n", getprogname());
755 exit(1);
758 static const struct got_error *
759 cmd_log(int argc, char *argv[])
761 const struct got_error *error;
762 struct got_repository *repo = NULL;
763 struct got_commit_object *commit = NULL;
764 struct got_object_id *id = NULL;
765 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
766 char *start_commit = NULL;
767 int diff_context = 3, ch;
768 int show_patch = 0, limit = 0, first_parent_traversal = 0;
769 const char *errstr;
770 struct got_reflist_head refs;
772 #ifndef PROFILE
773 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
774 NULL)
775 == -1)
776 err(1, "pledge");
777 #endif
779 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
780 switch (ch) {
781 case 'p':
782 show_patch = 1;
783 break;
784 case 'c':
785 start_commit = optarg;
786 break;
787 case 'C':
788 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
789 &errstr);
790 if (errstr != NULL)
791 err(1, "-C option %s", errstr);
792 break;
793 case 'l':
794 limit = strtonum(optarg, 1, INT_MAX, &errstr);
795 if (errstr != NULL)
796 err(1, "-l option %s", errstr);
797 break;
798 case 'f':
799 first_parent_traversal = 1;
800 break;
801 case 'r':
802 repo_path = realpath(optarg, NULL);
803 if (repo_path == NULL)
804 err(1, "-r option");
805 break;
806 default:
807 usage();
808 /* NOTREACHED */
812 argc -= optind;
813 argv += optind;
815 if (argc == 0)
816 path = strdup("");
817 else if (argc == 1)
818 path = strdup(argv[0]);
819 else
820 usage_log();
821 if (path == NULL)
822 return got_error_from_errno();
824 cwd = getcwd(NULL, 0);
825 if (cwd == NULL) {
826 error = got_error_from_errno();
827 goto done;
829 if (repo_path == NULL) {
830 repo_path = strdup(cwd);
831 if (repo_path == NULL) {
832 error = got_error_from_errno();
833 goto done;
837 error = apply_unveil(repo_path, NULL);
838 if (error)
839 goto done;
841 error = got_repo_open(&repo, repo_path);
842 if (error != NULL)
843 goto done;
845 if (start_commit == NULL) {
846 struct got_reference *head_ref;
847 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
848 if (error != NULL)
849 return error;
850 error = got_ref_resolve(&id, repo, head_ref);
851 got_ref_close(head_ref);
852 if (error != NULL)
853 return error;
854 error = got_object_open_as_commit(&commit, repo, id);
855 } else {
856 struct got_reference *ref;
857 error = got_ref_open(&ref, repo, start_commit);
858 if (error == NULL) {
859 int obj_type;
860 error = got_ref_resolve(&id, repo, ref);
861 got_ref_close(ref);
862 if (error != NULL)
863 goto done;
864 error = got_object_get_type(&obj_type, repo, id);
865 if (error != NULL)
866 goto done;
867 if (obj_type == GOT_OBJ_TYPE_TAG) {
868 struct got_tag_object *tag;
869 error = got_object_open_as_tag(&tag, repo, id);
870 if (error != NULL)
871 goto done;
872 if (got_object_tag_get_object_type(tag) !=
873 GOT_OBJ_TYPE_COMMIT) {
874 got_object_tag_close(tag);
875 error = got_error(GOT_ERR_OBJ_TYPE);
876 goto done;
878 free(id);
879 id = got_object_id_dup(
880 got_object_tag_get_object_id(tag));
881 if (id == NULL)
882 error = got_error_from_errno();
883 got_object_tag_close(tag);
884 if (error)
885 goto done;
886 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
887 error = got_error(GOT_ERR_OBJ_TYPE);
888 goto done;
890 error = got_object_open_as_commit(&commit, repo, id);
891 if (error != NULL)
892 goto done;
894 if (commit == NULL) {
895 error = got_object_resolve_id_str(&id, repo,
896 start_commit);
897 if (error != NULL)
898 return error;
901 if (error != NULL)
902 goto done;
904 error = got_repo_map_path(&in_repo_path, repo, path, 1);
905 if (error != NULL)
906 goto done;
907 if (in_repo_path) {
908 free(path);
909 path = in_repo_path;
912 SIMPLEQ_INIT(&refs);
913 error = got_ref_list(&refs, repo);
914 if (error)
915 goto done;
917 error = print_commits(id, repo, path, show_patch,
918 diff_context, limit, first_parent_traversal, &refs);
919 done:
920 free(path);
921 free(repo_path);
922 free(cwd);
923 free(id);
924 if (repo) {
925 const struct got_error *repo_error;
926 repo_error = got_repo_close(repo);
927 if (error == NULL)
928 error = repo_error;
930 return error;
933 __dead static void
934 usage_diff(void)
936 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
937 "object1 object2\n", getprogname());
938 exit(1);
941 static const struct got_error *
942 cmd_diff(int argc, char *argv[])
944 const struct got_error *error;
945 struct got_repository *repo = NULL;
946 char *repo_path = NULL;
947 struct got_object_id *id1 = NULL, *id2 = NULL;
948 char *id_str1 = NULL, *id_str2 = NULL;
949 int type1, type2;
950 int diff_context = 3, ch;
951 const char *errstr;
953 #ifndef PROFILE
954 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
955 NULL) == -1)
956 err(1, "pledge");
957 #endif
959 while ((ch = getopt(argc, argv, "C:")) != -1) {
960 switch (ch) {
961 case 'C':
962 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
963 if (errstr != NULL)
964 err(1, "-C option %s", errstr);
965 break;
966 default:
967 usage();
968 /* NOTREACHED */
972 argc -= optind;
973 argv += optind;
975 if (argc == 0) {
976 usage_diff(); /* TODO show local worktree changes */
977 } else if (argc == 2) {
978 repo_path = getcwd(NULL, 0);
979 if (repo_path == NULL)
980 return got_error_from_errno();
981 id_str1 = argv[0];
982 id_str2 = argv[1];
983 } else if (argc == 3) {
984 repo_path = realpath(argv[0], NULL);
985 if (repo_path == NULL)
986 return got_error_from_errno();
987 id_str1 = argv[1];
988 id_str2 = argv[2];
989 } else
990 usage_diff();
992 error = apply_unveil(repo_path, NULL);
993 if (error)
994 goto done;
996 error = got_repo_open(&repo, repo_path);
997 free(repo_path);
998 if (error != NULL)
999 goto done;
1001 error = got_object_resolve_id_str(&id1, repo, id_str1);
1002 if (error)
1003 goto done;
1005 error = got_object_resolve_id_str(&id2, repo, id_str2);
1006 if (error)
1007 goto done;
1009 error = got_object_get_type(&type1, repo, id1);
1010 if (error)
1011 goto done;
1013 error = got_object_get_type(&type2, repo, id2);
1014 if (error)
1015 goto done;
1017 if (type1 != type2) {
1018 error = got_error(GOT_ERR_OBJ_TYPE);
1019 goto done;
1022 switch (type1) {
1023 case GOT_OBJ_TYPE_BLOB:
1024 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1025 diff_context, repo, stdout);
1026 break;
1027 case GOT_OBJ_TYPE_TREE:
1028 error = got_diff_objects_as_trees(id1, id2, "", "",
1029 diff_context, repo, stdout);
1030 break;
1031 case GOT_OBJ_TYPE_COMMIT:
1032 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1033 id_str2);
1034 error = got_diff_objects_as_commits(id1, id2, diff_context,
1035 repo, stdout);
1036 break;
1037 default:
1038 error = got_error(GOT_ERR_OBJ_TYPE);
1041 done:
1042 free(id1);
1043 free(id2);
1044 if (repo) {
1045 const struct got_error *repo_error;
1046 repo_error = got_repo_close(repo);
1047 if (error == NULL)
1048 error = repo_error;
1050 return error;
1053 __dead static void
1054 usage_blame(void)
1056 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1057 getprogname());
1058 exit(1);
1061 static const struct got_error *
1062 cmd_blame(int argc, char *argv[])
1064 const struct got_error *error;
1065 struct got_repository *repo = NULL;
1066 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1067 struct got_object_id *commit_id = NULL;
1068 char *commit_id_str = NULL;
1069 int ch;
1071 #ifndef PROFILE
1072 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1073 NULL) == -1)
1074 err(1, "pledge");
1075 #endif
1077 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1078 switch (ch) {
1079 case 'c':
1080 commit_id_str = optarg;
1081 break;
1082 case 'r':
1083 repo_path = realpath(optarg, NULL);
1084 if (repo_path == NULL)
1085 err(1, "-r option");
1086 break;
1087 default:
1088 usage();
1089 /* NOTREACHED */
1093 argc -= optind;
1094 argv += optind;
1096 if (argc == 1)
1097 path = argv[0];
1098 else
1099 usage_blame();
1101 cwd = getcwd(NULL, 0);
1102 if (cwd == NULL) {
1103 error = got_error_from_errno();
1104 goto done;
1106 if (repo_path == NULL) {
1107 repo_path = strdup(cwd);
1108 if (repo_path == NULL) {
1109 error = got_error_from_errno();
1110 goto done;
1114 error = apply_unveil(repo_path, NULL);
1115 if (error)
1116 goto done;
1118 error = got_repo_open(&repo, repo_path);
1119 if (error != NULL)
1120 goto done;
1122 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1123 if (error != NULL)
1124 goto done;
1126 if (commit_id_str == NULL) {
1127 struct got_reference *head_ref;
1128 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1129 if (error != NULL)
1130 goto done;
1131 error = got_ref_resolve(&commit_id, repo, head_ref);
1132 got_ref_close(head_ref);
1133 if (error != NULL)
1134 goto done;
1135 } else {
1136 error = got_object_resolve_id_str(&commit_id, repo,
1137 commit_id_str);
1138 if (error != NULL)
1139 goto done;
1142 error = got_blame(in_repo_path, commit_id, repo, stdout);
1143 done:
1144 free(in_repo_path);
1145 free(repo_path);
1146 free(cwd);
1147 free(commit_id);
1148 if (repo) {
1149 const struct got_error *repo_error;
1150 repo_error = got_repo_close(repo);
1151 if (error == NULL)
1152 error = repo_error;
1154 return error;
1157 __dead static void
1158 usage_tree(void)
1160 fprintf(stderr,
1161 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1162 getprogname());
1163 exit(1);
1166 static void
1167 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1168 const char *root_path)
1170 int is_root_path = (strcmp(path, root_path) == 0);
1172 path += strlen(root_path);
1173 while (path[0] == '/')
1174 path++;
1176 printf("%s%s%s%s%s\n", id ? id : "", path,
1177 is_root_path ? "" : "/",
1178 te->name, S_ISDIR(te->mode) ? "/" : "");
1181 static const struct got_error *
1182 print_tree(const char *path, struct got_object_id *commit_id,
1183 int show_ids, int recurse, const char *root_path,
1184 struct got_repository *repo)
1186 const struct got_error *err = NULL;
1187 struct got_object_id *tree_id = NULL;
1188 struct got_tree_object *tree = NULL;
1189 const struct got_tree_entries *entries;
1190 struct got_tree_entry *te;
1192 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1193 if (err)
1194 goto done;
1196 err = got_object_open_as_tree(&tree, repo, tree_id);
1197 if (err)
1198 goto done;
1199 entries = got_object_tree_get_entries(tree);
1200 te = SIMPLEQ_FIRST(&entries->head);
1201 while (te) {
1202 char *id = NULL;
1204 if (sigint_received || sigpipe_received)
1205 break;
1207 if (show_ids) {
1208 char *id_str;
1209 err = got_object_id_str(&id_str, te->id);
1210 if (err)
1211 goto done;
1212 if (asprintf(&id, "%s ", id_str) == -1) {
1213 err = got_error_from_errno();
1214 free(id_str);
1215 goto done;
1217 free(id_str);
1219 print_entry(te, id, path, root_path);
1220 free(id);
1222 if (recurse && S_ISDIR(te->mode)) {
1223 char *child_path;
1224 if (asprintf(&child_path, "%s%s%s", path,
1225 path[0] == '/' && path[1] == '\0' ? "" : "/",
1226 te->name) == -1) {
1227 err = got_error_from_errno();
1228 goto done;
1230 err = print_tree(child_path, commit_id, show_ids, 1,
1231 root_path, repo);
1232 free(child_path);
1233 if (err)
1234 goto done;
1237 te = SIMPLEQ_NEXT(te, entry);
1239 done:
1240 if (tree)
1241 got_object_tree_close(tree);
1242 free(tree_id);
1243 return err;
1246 static const struct got_error *
1247 cmd_tree(int argc, char *argv[])
1249 const struct got_error *error;
1250 struct got_repository *repo = NULL;
1251 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1252 struct got_object_id *commit_id = NULL;
1253 char *commit_id_str = NULL;
1254 int show_ids = 0, recurse = 0;
1255 int ch;
1257 #ifndef PROFILE
1258 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1259 NULL) == -1)
1260 err(1, "pledge");
1261 #endif
1263 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1264 switch (ch) {
1265 case 'c':
1266 commit_id_str = optarg;
1267 break;
1268 case 'r':
1269 repo_path = realpath(optarg, NULL);
1270 if (repo_path == NULL)
1271 err(1, "-r option");
1272 break;
1273 case 'i':
1274 show_ids = 1;
1275 break;
1276 case 'R':
1277 recurse = 1;
1278 break;
1279 default:
1280 usage();
1281 /* NOTREACHED */
1285 argc -= optind;
1286 argv += optind;
1288 if (argc == 1)
1289 path = argv[0];
1290 else if (argc > 1)
1291 usage_tree();
1292 else
1293 path = "/";
1295 cwd = getcwd(NULL, 0);
1296 if (cwd == NULL) {
1297 error = got_error_from_errno();
1298 goto done;
1300 if (repo_path == NULL) {
1301 repo_path = strdup(cwd);
1302 if (repo_path == NULL) {
1303 error = got_error_from_errno();
1304 goto done;
1308 error = apply_unveil(repo_path, NULL);
1309 if (error)
1310 goto done;
1312 error = got_repo_open(&repo, repo_path);
1313 if (error != NULL)
1314 goto done;
1316 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1317 if (error != NULL)
1318 goto done;
1320 if (commit_id_str == NULL) {
1321 struct got_reference *head_ref;
1322 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1323 if (error != NULL)
1324 goto done;
1325 error = got_ref_resolve(&commit_id, repo, head_ref);
1326 got_ref_close(head_ref);
1327 if (error != NULL)
1328 goto done;
1329 } else {
1330 error = got_object_resolve_id_str(&commit_id, repo,
1331 commit_id_str);
1332 if (error != NULL)
1333 goto done;
1336 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1337 in_repo_path, repo);
1338 done:
1339 free(in_repo_path);
1340 free(repo_path);
1341 free(cwd);
1342 free(commit_id);
1343 if (repo) {
1344 const struct got_error *repo_error;
1345 repo_error = got_repo_close(repo);
1346 if (error == NULL)
1347 error = repo_error;
1349 return error;
1352 #ifdef notyet
1353 static const struct got_error *
1354 cmd_status(int argc __unused, char *argv[] __unused)
1356 git_repository *repo = NULL;
1357 git_status_list *status;
1358 git_status_options statusopts;
1359 size_t i;
1361 git_libgit2_init();
1363 if (git_repository_open_ext(&repo, ".", 0, NULL))
1364 errx(1, "git_repository_open: %s", giterr_last()->message);
1366 if (git_repository_is_bare(repo))
1367 errx(1, "bar repository");
1369 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
1370 errx(1, "git_status_init_options: %s", giterr_last()->message);
1372 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
1373 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
1374 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
1375 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1377 if (git_status_list_new(&status, repo, &statusopts))
1378 errx(1, "git_status_list_new: %s", giterr_last()->message);
1380 for (i = 0; i < git_status_list_entrycount(status); i++) {
1381 const git_status_entry *se;
1383 se = git_status_byindex(status, i);
1384 switch (se->status) {
1385 case GIT_STATUS_WT_NEW:
1386 printf("? %s\n", se->index_to_workdir->new_file.path);
1387 break;
1388 case GIT_STATUS_WT_MODIFIED:
1389 printf("M %s\n", se->index_to_workdir->new_file.path);
1390 break;
1391 case GIT_STATUS_WT_DELETED:
1392 printf("R %s\n", se->index_to_workdir->new_file.path);
1393 break;
1394 case GIT_STATUS_WT_RENAMED:
1395 printf("m %s -> %s\n",
1396 se->index_to_workdir->old_file.path,
1397 se->index_to_workdir->new_file.path);
1398 break;
1399 case GIT_STATUS_WT_TYPECHANGE:
1400 printf("t %s\n", se->index_to_workdir->new_file.path);
1401 break;
1402 case GIT_STATUS_INDEX_NEW:
1403 printf("A %s\n", se->head_to_index->new_file.path);
1404 break;
1405 case GIT_STATUS_INDEX_MODIFIED:
1406 printf("M %s\n", se->head_to_index->old_file.path);
1407 break;
1408 case GIT_STATUS_INDEX_DELETED:
1409 printf("R %s\n", se->head_to_index->old_file.path);
1410 break;
1411 case GIT_STATUS_INDEX_RENAMED:
1412 printf("m %s -> %s\n",
1413 se->head_to_index->old_file.path,
1414 se->head_to_index->new_file.path);
1415 break;
1416 case GIT_STATUS_INDEX_TYPECHANGE:
1417 printf("t %s\n", se->head_to_index->old_file.path);
1418 break;
1419 case GIT_STATUS_CURRENT:
1420 default:
1421 break;
1425 git_status_list_free(status);
1426 git_repository_free(repo);
1427 git_libgit2_shutdown();
1429 return 0;
1431 #endif