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 __dead static void
178 usage_checkout(void)
180 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
181 "[worktree-path]\n", getprogname());
182 exit(1);
185 static void
186 checkout_progress(void *arg, unsigned char status, const char *path)
188 char *worktree_path = arg;
190 while (path[0] == '/')
191 path++;
193 printf("%c %s/%s\n", status, worktree_path, path);
196 static const struct got_error *
197 checkout_cancel(void *arg)
199 if (sigint_received || sigpipe_received)
200 return got_error(GOT_ERR_CANCELLED);
201 return NULL;
204 static const struct got_error *
205 cmd_checkout(int argc, char *argv[])
207 const struct got_error *error = NULL;
208 struct got_repository *repo = NULL;
209 struct got_reference *head_ref = NULL;
210 struct got_worktree *worktree = NULL;
211 char *repo_path = NULL;
212 char *worktree_path = NULL;
213 const char *path_prefix = "";
214 int ch, same_path_prefix;
216 while ((ch = getopt(argc, argv, "p:")) != -1) {
217 switch (ch) {
218 case 'p':
219 path_prefix = optarg;
220 break;
221 default:
222 usage();
223 /* NOTREACHED */
227 argc -= optind;
228 argv += optind;
230 #ifndef PROFILE
231 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
232 NULL) == -1)
233 err(1, "pledge");
234 #endif
235 if (argc == 1) {
236 char *cwd, *base, *dotgit;
237 repo_path = realpath(argv[0], NULL);
238 if (repo_path == NULL)
239 return got_error_from_errno();
240 cwd = getcwd(NULL, 0);
241 if (cwd == NULL) {
242 error = got_error_from_errno();
243 goto done;
245 if (path_prefix[0])
246 base = basename(path_prefix);
247 else
248 base = basename(repo_path);
249 if (base == NULL) {
250 error = got_error_from_errno();
251 goto done;
253 dotgit = strstr(base, ".git");
254 if (dotgit)
255 *dotgit = '\0';
256 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
257 error = got_error_from_errno();
258 free(cwd);
259 goto done;
261 free(cwd);
262 } else if (argc == 2) {
263 repo_path = realpath(argv[0], NULL);
264 if (repo_path == NULL) {
265 error = got_error_from_errno();
266 goto done;
268 worktree_path = realpath(argv[1], NULL);
269 if (worktree_path == NULL) {
270 error = got_error_from_errno();
271 goto done;
273 } else
274 usage_checkout();
276 if (unveil(repo_path, "r") != 0 ||
277 unveil(worktree_path, "rwc") != 0 ||
278 unveil("/tmp", "rwc") != 0) {
279 error = got_error_from_errno();
280 goto done;
282 error = got_privsep_unveil_exec_helpers();
283 if (error != NULL)
284 goto done;
286 if (unveil(NULL, NULL) != 0) {
287 error = got_error_from_errno();
288 goto done;
291 error = got_repo_open(&repo, repo_path);
292 if (error != NULL)
293 goto done;
294 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
295 if (error != NULL)
296 goto done;
298 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
299 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
300 goto done;
302 error = got_worktree_open(&worktree, worktree_path);
303 if (error != NULL)
304 goto done;
306 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
307 path_prefix);
308 if (error != NULL)
309 goto done;
310 if (!same_path_prefix) {
311 error = got_error(GOT_ERR_PATH_PREFIX);
312 goto done;
315 error = got_worktree_checkout_files(worktree, repo,
316 checkout_progress, worktree_path, checkout_cancel, NULL);
317 if (error != NULL)
318 goto done;
320 printf("Now shut up and hack\n");
322 done:
323 free(repo_path);
324 free(worktree_path);
325 return error;
328 __dead static void
329 usage_update(void)
331 fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
332 getprogname());
333 exit(1);
336 static void
337 update_progress(void *arg, unsigned char status, const char *path)
339 if (status == GOT_STATUS_EXISTS)
340 return;
342 while (path[0] == '/')
343 path++;
344 printf("%c %s\n", status, path);
347 static const struct got_error *
348 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
349 struct got_repository *repo)
351 const struct got_error *err;
352 struct got_reference *head_ref = NULL;
353 struct got_object_id *head_commit_id = NULL;
354 struct got_commit_graph *graph = NULL;
356 head_ref = got_worktree_get_head_ref(worktree);
357 if (head_ref == NULL)
358 return got_error_from_errno();
360 /* TODO: Check the reflog. The head ref may have been rebased. */
361 err = got_ref_resolve(&head_commit_id, repo, head_ref);
362 if (err)
363 goto done;
365 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
366 if (err)
367 goto done;
369 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
370 if (err)
371 goto done;
372 while (1) {
373 struct got_object_id *id;
375 if (sigint_received || sigpipe_received)
376 break;
378 err = got_commit_graph_iter_next(&id, graph);
379 if (err) {
380 if (err->code == GOT_ERR_ITER_COMPLETED) {
381 err = got_error(GOT_ERR_ANCESTRY);
382 break;
384 if (err->code != GOT_ERR_ITER_NEED_MORE)
385 break;
386 err = got_commit_graph_fetch_commits(graph, 1, repo);
387 if (err)
388 break;
389 else
390 continue;
392 if (id == NULL)
393 break;
394 if (got_object_id_cmp(id, commit_id) == 0)
395 break;
397 done:
398 if (head_ref)
399 got_ref_close(head_ref);
400 if (graph)
401 got_commit_graph_close(graph);
402 return err;
405 static const struct got_error *
406 cmd_update(int argc, char *argv[])
408 const struct got_error *error = NULL;
409 struct got_repository *repo = NULL;
410 struct got_worktree *worktree = NULL;
411 char *worktree_path = NULL;
412 struct got_object_id *commit_id = NULL;
413 char *commit_id_str = NULL;
414 int ch;
416 while ((ch = getopt(argc, argv, "c:")) != -1) {
417 switch (ch) {
418 case 'c':
419 commit_id_str = strdup(optarg);
420 if (commit_id_str == NULL)
421 return got_error_from_errno();
422 break;
423 default:
424 usage();
425 /* NOTREACHED */
429 argc -= optind;
430 argv += optind;
432 #ifndef PROFILE
433 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
434 == -1)
435 err(1, "pledge");
436 #endif
437 if (argc == 0) {
438 worktree_path = getcwd(NULL, 0);
439 if (worktree_path == NULL) {
440 error = got_error_from_errno();
441 goto done;
443 } else if (argc == 1) {
444 worktree_path = realpath(argv[0], NULL);
445 if (worktree_path == NULL) {
446 error = got_error_from_errno();
447 goto done;
449 } else
450 usage_update();
452 error = got_worktree_open(&worktree, worktree_path);
453 if (error != NULL)
454 goto done;
456 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
457 if (error != NULL)
458 goto done;
460 if (commit_id_str == NULL) {
461 struct got_reference *head_ref;
462 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
463 if (error != NULL)
464 goto done;
465 error = got_ref_resolve(&commit_id, repo, head_ref);
466 if (error != NULL)
467 goto done;
468 error = got_object_id_str(&commit_id_str, commit_id);
469 if (error != NULL)
470 goto done;
471 } else {
472 error = got_object_resolve_id_str(&commit_id, repo,
473 commit_id_str);
474 if (error != NULL)
475 goto done;
478 error = check_ancestry(worktree, commit_id, repo);
479 if (error != NULL)
480 goto done;
482 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
483 commit_id) != 0) {
484 error = got_worktree_set_base_commit_id(worktree, repo,
485 commit_id);
486 if (error)
487 goto done;
490 error = got_worktree_checkout_files(worktree, repo,
491 update_progress, NULL, checkout_cancel, NULL);
492 if (error != NULL)
493 goto done;
495 printf("Updated to commit %s\n", commit_id_str);
496 done:
497 free(worktree_path);
498 free(commit_id);
499 free(commit_id_str);
500 return error;
503 static const struct got_error *
504 print_patch(struct got_commit_object *commit, struct got_object_id *id,
505 int diff_context, struct got_repository *repo)
507 const struct got_error *err = NULL;
508 struct got_tree_object *tree1 = NULL, *tree2;
509 struct got_object_qid *qid;
510 char *id_str1 = NULL, *id_str2;
512 err = got_object_open_as_tree(&tree2, repo,
513 got_object_commit_get_tree_id(commit));
514 if (err)
515 return err;
517 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
518 if (qid != NULL) {
519 struct got_commit_object *pcommit;
521 err = got_object_open_as_commit(&pcommit, repo, qid->id);
522 if (err)
523 return err;
525 err = got_object_open_as_tree(&tree1, repo,
526 got_object_commit_get_tree_id(pcommit));
527 got_object_commit_close(pcommit);
528 if (err)
529 return err;
531 err = got_object_id_str(&id_str1, qid->id);
532 if (err)
533 return err;
536 err = got_object_id_str(&id_str2, id);
537 if (err)
538 goto done;
540 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
541 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
542 done:
543 if (tree1)
544 got_object_tree_close(tree1);
545 got_object_tree_close(tree2);
546 free(id_str1);
547 free(id_str2);
548 return err;
551 static char *
552 get_datestr(time_t *time, char *datebuf)
554 char *p, *s = ctime_r(time, datebuf);
555 p = strchr(s, '\n');
556 if (p)
557 *p = '\0';
558 return s;
561 static const struct got_error *
562 print_commit(struct got_commit_object *commit, struct got_object_id *id,
563 struct got_repository *repo, int show_patch, int diff_context)
565 const struct got_error *err = NULL;
566 char *id_str, *datestr, *logmsg0, *logmsg, *line;
567 char datebuf[26];
568 time_t committer_time;
569 const char *author, *committer;
571 err = got_object_id_str(&id_str, id);
572 if (err)
573 return err;
575 printf("-----------------------------------------------\n");
576 printf("commit %s\n", id_str);
577 free(id_str);
578 printf("from: %s\n", got_object_commit_get_author(commit));
579 committer_time = got_object_commit_get_committer_time(commit);
580 datestr = get_datestr(&committer_time, datebuf);
581 printf("date: %s UTC\n", datestr);
582 author = got_object_commit_get_author(commit);
583 committer = got_object_commit_get_committer(commit);
584 if (strcmp(author, committer) != 0)
585 printf("via: %s\n", committer);
586 if (got_object_commit_get_nparents(commit) > 1) {
587 const struct got_object_id_queue *parent_ids;
588 struct got_object_qid *qid;
589 int n = 1;
590 parent_ids = got_object_commit_get_parent_ids(commit);
591 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
592 err = got_object_id_str(&id_str, qid->id);
593 if (err)
594 return err;
595 printf("parent %d: %s\n", n++, id_str);
596 free(id_str);
600 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
601 if (logmsg0 == NULL)
602 return got_error_from_errno();
604 logmsg = logmsg0;
605 do {
606 line = strsep(&logmsg, "\n");
607 if (line)
608 printf(" %s\n", line);
609 } while (line);
610 free(logmsg0);
612 if (show_patch) {
613 err = print_patch(commit, id, diff_context, repo);
614 if (err == 0)
615 printf("\n");
618 fflush(stdout);
619 return err;
622 static const struct got_error *
623 print_commits(struct got_object_id *root_id, struct got_repository *repo,
624 char *path, int show_patch, int diff_context, int limit,
625 int first_parent_traversal)
627 const struct got_error *err;
628 struct got_commit_graph *graph;
630 err = got_commit_graph_open(&graph, root_id, path,
631 first_parent_traversal, repo);
632 if (err)
633 return err;
634 err = got_commit_graph_iter_start(graph, root_id, repo);
635 if (err)
636 goto done;
637 while (1) {
638 struct got_commit_object *commit;
639 struct got_object_id *id;
641 if (sigint_received || sigpipe_received)
642 break;
644 err = got_commit_graph_iter_next(&id, graph);
645 if (err) {
646 if (err->code == GOT_ERR_ITER_COMPLETED) {
647 err = NULL;
648 break;
650 if (err->code != GOT_ERR_ITER_NEED_MORE)
651 break;
652 err = got_commit_graph_fetch_commits(graph, 1, repo);
653 if (err)
654 break;
655 else
656 continue;
658 if (id == NULL)
659 break;
661 err = got_object_open_as_commit(&commit, repo, id);
662 if (err)
663 break;
664 err = print_commit(commit, id, repo, show_patch, diff_context);
665 got_object_commit_close(commit);
666 if (err || (limit && --limit == 0))
667 break;
669 done:
670 got_commit_graph_close(graph);
671 return err;
674 __dead static void
675 usage_log(void)
677 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
678 "[-r repository-path] [path]\n", getprogname());
679 exit(1);
682 static const struct got_error *
683 cmd_log(int argc, char *argv[])
685 const struct got_error *error;
686 struct got_repository *repo = NULL;
687 struct got_commit_object *commit = NULL;
688 struct got_object_id *id = NULL;
689 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
690 char *start_commit = NULL;
691 int diff_context = 3, ch;
692 int show_patch = 0, limit = 0, first_parent_traversal = 0;
693 const char *errstr;
695 #ifndef PROFILE
696 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
697 == -1)
698 err(1, "pledge");
699 #endif
701 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
702 switch (ch) {
703 case 'p':
704 show_patch = 1;
705 break;
706 case 'c':
707 start_commit = optarg;
708 break;
709 case 'C':
710 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
711 &errstr);
712 if (errstr != NULL)
713 err(1, "-C option %s", errstr);
714 break;
715 case 'l':
716 limit = strtonum(optarg, 1, INT_MAX, &errstr);
717 if (errstr != NULL)
718 err(1, "-l option %s", errstr);
719 break;
720 case 'f':
721 first_parent_traversal = 1;
722 break;
723 case 'r':
724 repo_path = realpath(optarg, NULL);
725 if (repo_path == NULL)
726 err(1, "-r option");
727 break;
728 default:
729 usage();
730 /* NOTREACHED */
734 argc -= optind;
735 argv += optind;
737 if (argc == 0)
738 path = strdup("");
739 else if (argc == 1)
740 path = strdup(argv[0]);
741 else
742 usage_log();
743 if (path == NULL)
744 return got_error_from_errno();
746 cwd = getcwd(NULL, 0);
747 if (cwd == NULL) {
748 error = got_error_from_errno();
749 goto done;
751 if (repo_path == NULL) {
752 repo_path = strdup(cwd);
753 if (repo_path == NULL) {
754 error = got_error_from_errno();
755 goto done;
759 error = got_repo_open(&repo, repo_path);
760 if (error != NULL)
761 goto done;
763 if (start_commit == NULL) {
764 struct got_reference *head_ref;
765 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
766 if (error != NULL)
767 return error;
768 error = got_ref_resolve(&id, repo, head_ref);
769 got_ref_close(head_ref);
770 if (error != NULL)
771 return error;
772 error = got_object_open_as_commit(&commit, repo, id);
773 } else {
774 struct got_reference *ref;
775 error = got_ref_open(&ref, repo, start_commit);
776 if (error == NULL) {
777 error = got_ref_resolve(&id, repo, ref);
778 got_ref_close(ref);
779 if (error != NULL)
780 return error;
781 error = got_object_open_as_commit(&commit, repo, id);
782 if (error != NULL)
783 return error;
785 if (commit == NULL) {
786 error = got_object_resolve_id_str(&id, repo,
787 start_commit);
788 if (error != NULL)
789 return error;
792 if (error != NULL)
793 goto done;
795 error = got_repo_map_path(&in_repo_path, repo, path, 1);
796 if (error != NULL)
797 goto done;
798 if (in_repo_path) {
799 free(path);
800 path = in_repo_path;
803 error = print_commits(id, repo, path, show_patch,
804 diff_context, limit, first_parent_traversal);
805 done:
806 free(path);
807 free(repo_path);
808 free(cwd);
809 free(id);
810 if (repo) {
811 const struct got_error *repo_error;
812 repo_error = got_repo_close(repo);
813 if (error == NULL)
814 error = repo_error;
816 return error;
819 __dead static void
820 usage_diff(void)
822 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
823 "object1 object2\n", getprogname());
824 exit(1);
827 static const struct got_error *
828 cmd_diff(int argc, char *argv[])
830 const struct got_error *error;
831 struct got_repository *repo = NULL;
832 char *repo_path = NULL;
833 struct got_object_id *id1 = NULL, *id2 = NULL;
834 char *id_str1 = NULL, *id_str2 = NULL;
835 int type1, type2;
836 int diff_context = 3, ch;
837 const char *errstr;
839 #ifndef PROFILE
840 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
841 == -1)
842 err(1, "pledge");
843 #endif
845 while ((ch = getopt(argc, argv, "C:")) != -1) {
846 switch (ch) {
847 case 'C':
848 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
849 if (errstr != NULL)
850 err(1, "-C option %s", errstr);
851 break;
852 default:
853 usage();
854 /* NOTREACHED */
858 argc -= optind;
859 argv += optind;
861 if (argc == 0) {
862 usage_diff(); /* TODO show local worktree changes */
863 } else if (argc == 2) {
864 repo_path = getcwd(NULL, 0);
865 if (repo_path == NULL)
866 return got_error_from_errno();
867 id_str1 = argv[0];
868 id_str2 = argv[1];
869 } else if (argc == 3) {
870 repo_path = realpath(argv[0], NULL);
871 if (repo_path == NULL)
872 return got_error_from_errno();
873 id_str1 = argv[1];
874 id_str2 = argv[2];
875 } else
876 usage_diff();
878 error = got_repo_open(&repo, repo_path);
879 free(repo_path);
880 if (error != NULL)
881 goto done;
883 error = got_object_resolve_id_str(&id1, repo, id_str1);
884 if (error)
885 goto done;
887 error = got_object_resolve_id_str(&id2, repo, id_str2);
888 if (error)
889 goto done;
891 error = got_object_get_type(&type1, repo, id1);
892 if (error)
893 goto done;
895 error = got_object_get_type(&type2, repo, id2);
896 if (error)
897 goto done;
899 if (type1 != type2) {
900 error = got_error(GOT_ERR_OBJ_TYPE);
901 goto done;
904 switch (type1) {
905 case GOT_OBJ_TYPE_BLOB:
906 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
907 diff_context, repo, stdout);
908 break;
909 case GOT_OBJ_TYPE_TREE:
910 error = got_diff_objects_as_trees(id1, id2, "", "",
911 diff_context, repo, stdout);
912 break;
913 case GOT_OBJ_TYPE_COMMIT:
914 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
915 id_str2);
916 error = got_diff_objects_as_commits(id1, id2, diff_context,
917 repo, stdout);
918 break;
919 default:
920 error = got_error(GOT_ERR_OBJ_TYPE);
923 done:
924 free(id1);
925 free(id2);
926 if (repo) {
927 const struct got_error *repo_error;
928 repo_error = got_repo_close(repo);
929 if (error == NULL)
930 error = repo_error;
932 return error;
935 __dead static void
936 usage_blame(void)
938 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
939 getprogname());
940 exit(1);
943 static const struct got_error *
944 cmd_blame(int argc, char *argv[])
946 const struct got_error *error;
947 struct got_repository *repo = NULL;
948 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
949 struct got_object_id *commit_id = NULL;
950 char *commit_id_str = NULL;
951 int ch;
953 #ifndef PROFILE
954 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
955 == -1)
956 err(1, "pledge");
957 #endif
959 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
960 switch (ch) {
961 case 'c':
962 commit_id_str = optarg;
963 break;
964 case 'r':
965 repo_path = realpath(optarg, NULL);
966 if (repo_path == NULL)
967 err(1, "-r option");
968 break;
969 default:
970 usage();
971 /* NOTREACHED */
975 argc -= optind;
976 argv += optind;
978 if (argc == 1)
979 path = argv[0];
980 else
981 usage_blame();
983 cwd = getcwd(NULL, 0);
984 if (cwd == NULL) {
985 error = got_error_from_errno();
986 goto done;
988 if (repo_path == NULL) {
989 repo_path = strdup(cwd);
990 if (repo_path == NULL) {
991 error = got_error_from_errno();
992 goto done;
996 error = got_repo_open(&repo, repo_path);
997 if (error != NULL)
998 goto done;
1000 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1001 if (error != NULL)
1002 goto done;
1004 if (commit_id_str == NULL) {
1005 struct got_reference *head_ref;
1006 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1007 if (error != NULL)
1008 goto done;
1009 error = got_ref_resolve(&commit_id, repo, head_ref);
1010 got_ref_close(head_ref);
1011 if (error != NULL)
1012 goto done;
1013 } else {
1014 error = got_object_resolve_id_str(&commit_id, repo,
1015 commit_id_str);
1016 if (error != NULL)
1017 goto done;
1020 error = got_blame(in_repo_path, commit_id, repo, stdout);
1021 done:
1022 free(in_repo_path);
1023 free(repo_path);
1024 free(cwd);
1025 free(commit_id);
1026 if (repo) {
1027 const struct got_error *repo_error;
1028 repo_error = got_repo_close(repo);
1029 if (error == NULL)
1030 error = repo_error;
1032 return error;
1035 __dead static void
1036 usage_tree(void)
1038 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
1039 getprogname());
1040 exit(1);
1044 static const struct got_error *
1045 print_tree(const char *path, struct got_object_id *commit_id,
1046 int show_ids, struct got_repository *repo)
1048 const struct got_error *err = NULL;
1049 struct got_object_id *tree_id = NULL;
1050 struct got_tree_object *tree = NULL;
1051 const struct got_tree_entries *entries;
1052 struct got_tree_entry *te;
1054 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1055 if (err)
1056 goto done;
1058 err = got_object_open_as_tree(&tree, repo, tree_id);
1059 if (err)
1060 goto done;
1061 entries = got_object_tree_get_entries(tree);
1062 te = SIMPLEQ_FIRST(&entries->head);
1063 while (te) {
1064 char *id = NULL;
1066 if (sigint_received || sigpipe_received)
1067 break;
1069 if (show_ids) {
1070 char *id_str;
1071 err = got_object_id_str(&id_str, te->id);
1072 if (err)
1073 goto done;
1074 if (asprintf(&id, "%s ", id_str) == -1) {
1075 err = got_error_from_errno();
1076 free(id_str);
1077 goto done;
1079 free(id_str);
1081 printf("%s%s%s\n", id ? id : "",
1082 te->name, S_ISDIR(te->mode) ? "/" : "");
1083 te = SIMPLEQ_NEXT(te, entry);
1084 free(id);
1086 done:
1087 if (tree)
1088 got_object_tree_close(tree);
1089 free(tree_id);
1090 return err;
1093 static const struct got_error *
1094 cmd_tree(int argc, char *argv[])
1096 const struct got_error *error;
1097 struct got_repository *repo = NULL;
1098 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1099 struct got_object_id *commit_id = NULL;
1100 char *commit_id_str = NULL;
1101 int show_ids = 0;
1102 int ch;
1104 #ifndef PROFILE
1105 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
1106 == -1)
1107 err(1, "pledge");
1108 #endif
1110 while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
1111 switch (ch) {
1112 case 'c':
1113 commit_id_str = optarg;
1114 break;
1115 case 'r':
1116 repo_path = realpath(optarg, NULL);
1117 if (repo_path == NULL)
1118 err(1, "-r option");
1119 break;
1120 case 'i':
1121 show_ids = 1;
1122 break;
1123 default:
1124 usage();
1125 /* NOTREACHED */
1129 argc -= optind;
1130 argv += optind;
1132 if (argc == 1)
1133 path = argv[0];
1134 else if (argc > 1)
1135 usage_tree();
1136 else
1137 path = "/";
1139 cwd = getcwd(NULL, 0);
1140 if (cwd == NULL) {
1141 error = got_error_from_errno();
1142 goto done;
1144 if (repo_path == NULL) {
1145 repo_path = strdup(cwd);
1146 if (repo_path == NULL) {
1147 error = got_error_from_errno();
1148 goto done;
1152 error = got_repo_open(&repo, repo_path);
1153 if (error != NULL)
1154 goto done;
1156 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1157 if (error != NULL)
1158 goto done;
1160 if (commit_id_str == NULL) {
1161 struct got_reference *head_ref;
1162 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1163 if (error != NULL)
1164 goto done;
1165 error = got_ref_resolve(&commit_id, repo, head_ref);
1166 got_ref_close(head_ref);
1167 if (error != NULL)
1168 goto done;
1169 } else {
1170 error = got_object_resolve_id_str(&commit_id, repo,
1171 commit_id_str);
1172 if (error != NULL)
1173 goto done;
1176 error = print_tree(in_repo_path, commit_id, show_ids, repo);
1177 done:
1178 free(in_repo_path);
1179 free(repo_path);
1180 free(cwd);
1181 free(commit_id);
1182 if (repo) {
1183 const struct got_error *repo_error;
1184 repo_error = got_repo_close(repo);
1185 if (error == NULL)
1186 error = repo_error;
1188 return error;
1191 #ifdef notyet
1192 static const struct got_error *
1193 cmd_status(int argc __unused, char *argv[] __unused)
1195 git_repository *repo = NULL;
1196 git_status_list *status;
1197 git_status_options statusopts;
1198 size_t i;
1200 git_libgit2_init();
1202 if (git_repository_open_ext(&repo, ".", 0, NULL))
1203 errx(1, "git_repository_open: %s", giterr_last()->message);
1205 if (git_repository_is_bare(repo))
1206 errx(1, "bar repository");
1208 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
1209 errx(1, "git_status_init_options: %s", giterr_last()->message);
1211 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
1212 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
1213 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
1214 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1216 if (git_status_list_new(&status, repo, &statusopts))
1217 errx(1, "git_status_list_new: %s", giterr_last()->message);
1219 for (i = 0; i < git_status_list_entrycount(status); i++) {
1220 const git_status_entry *se;
1222 se = git_status_byindex(status, i);
1223 switch (se->status) {
1224 case GIT_STATUS_WT_NEW:
1225 printf("? %s\n", se->index_to_workdir->new_file.path);
1226 break;
1227 case GIT_STATUS_WT_MODIFIED:
1228 printf("M %s\n", se->index_to_workdir->new_file.path);
1229 break;
1230 case GIT_STATUS_WT_DELETED:
1231 printf("R %s\n", se->index_to_workdir->new_file.path);
1232 break;
1233 case GIT_STATUS_WT_RENAMED:
1234 printf("m %s -> %s\n",
1235 se->index_to_workdir->old_file.path,
1236 se->index_to_workdir->new_file.path);
1237 break;
1238 case GIT_STATUS_WT_TYPECHANGE:
1239 printf("t %s\n", se->index_to_workdir->new_file.path);
1240 break;
1241 case GIT_STATUS_INDEX_NEW:
1242 printf("A %s\n", se->head_to_index->new_file.path);
1243 break;
1244 case GIT_STATUS_INDEX_MODIFIED:
1245 printf("M %s\n", se->head_to_index->old_file.path);
1246 break;
1247 case GIT_STATUS_INDEX_DELETED:
1248 printf("R %s\n", se->head_to_index->old_file.path);
1249 break;
1250 case GIT_STATUS_INDEX_RENAMED:
1251 printf("m %s -> %s\n",
1252 se->head_to_index->old_file.path,
1253 se->head_to_index->new_file.path);
1254 break;
1255 case GIT_STATUS_INDEX_TYPECHANGE:
1256 printf("t %s\n", se->head_to_index->old_file.path);
1257 break;
1258 case GIT_STATUS_CURRENT:
1259 default:
1260 break;
1264 git_status_list_free(status);
1265 git_repository_free(repo);
1266 git_libgit2_shutdown();
1268 return 0;
1270 #endif