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>
22 #include <err.h>
23 #include <errno.h>
24 #include <locale.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <libgen.h>
30 #include <time.h>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_reference.h"
35 #include "got_repository.h"
36 #include "got_worktree.h"
37 #include "got_diff.h"
38 #include "got_commit_graph.h"
39 #include "got_blame.h"
41 #ifndef nitems
42 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
43 #endif
45 struct cmd {
46 const char *cmd_name;
47 const struct got_error *(*cmd_main)(int, char *[]);
48 void (*cmd_usage)(void);
49 const char *cmd_descr;
50 };
52 __dead static void usage(void);
53 __dead static void usage_checkout(void);
54 __dead static void usage_log(void);
55 __dead static void usage_diff(void);
56 __dead static void usage_blame(void);
58 static const struct got_error* cmd_checkout(int, char *[]);
59 static const struct got_error* cmd_log(int, char *[]);
60 static const struct got_error* cmd_diff(int, char *[]);
61 static const struct got_error* cmd_blame(int, char *[]);
62 #ifdef notyet
63 static const struct got_error* cmd_status(int, char *[]);
64 #endif
66 static struct cmd got_commands[] = {
67 { "checkout", cmd_checkout, usage_checkout,
68 "check out a new work tree from a repository" },
69 { "log", cmd_log, usage_log,
70 "show repository history" },
71 { "diff", cmd_diff, usage_diff,
72 "compare files and directories" },
73 { "blame", cmd_blame, usage_blame,
74 " show when lines in a file were changed" },
75 #ifdef notyet
76 { "status", cmd_status, usage_status,
77 "show modification status of files" },
78 #endif
79 };
81 int
82 main(int argc, char *argv[])
83 {
84 struct cmd *cmd;
85 unsigned int i;
86 int ch;
87 int hflag = 0;
89 setlocale(LC_ALL, "");
91 while ((ch = getopt(argc, argv, "h")) != -1) {
92 switch (ch) {
93 case 'h':
94 hflag = 1;
95 break;
96 default:
97 usage();
98 /* NOTREACHED */
99 }
102 argc -= optind;
103 argv += optind;
104 optind = 0;
106 if (argc <= 0)
107 usage();
109 for (i = 0; i < nitems(got_commands); i++) {
110 const struct got_error *error;
112 cmd = &got_commands[i];
114 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
115 continue;
117 if (hflag)
118 got_commands[i].cmd_usage();
120 error = got_commands[i].cmd_main(argc, argv);
121 if (error) {
122 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
123 return 1;
126 return 0;
129 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
130 return 1;
133 __dead static void
134 usage(void)
136 int i;
138 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
139 "Available commands:\n", getprogname());
140 for (i = 0; i < nitems(got_commands); i++) {
141 struct cmd *cmd = &got_commands[i];
142 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
144 exit(1);
147 __dead static void
148 usage_checkout(void)
150 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
151 "[worktree-path]\n", getprogname());
152 exit(1);
155 static void
156 checkout_progress(void *arg, const char *path)
158 char *worktree_path = arg;
160 while (path[0] == '/')
161 path++;
163 printf("A %s/%s\n", worktree_path, path);
166 static const struct got_error *
167 cmd_checkout(int argc, char *argv[])
169 const struct got_error *error = NULL;
170 struct got_repository *repo = NULL;
171 struct got_reference *head_ref = NULL;
172 struct got_worktree *worktree = NULL;
173 char *repo_path = NULL;
174 char *worktree_path = NULL;
175 const char *path_prefix = "";
176 int ch;
178 while ((ch = getopt(argc, argv, "p:")) != -1) {
179 switch (ch) {
180 case 'p':
181 path_prefix = optarg;
182 break;
183 default:
184 usage();
185 /* NOTREACHED */
189 argc -= optind;
190 argv += optind;
192 #ifndef PROFILE
193 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
194 err(1, "pledge");
195 #endif
196 if (argc == 1) {
197 char *cwd, *base, *dotgit;
198 repo_path = realpath(argv[0], NULL);
199 if (repo_path == NULL)
200 return got_error_from_errno();
201 cwd = getcwd(NULL, 0);
202 if (cwd == NULL) {
203 error = got_error_from_errno();
204 goto done;
206 if (path_prefix[0])
207 base = basename(path_prefix);
208 else
209 base = basename(repo_path);
210 if (base == NULL) {
211 error = got_error_from_errno();
212 goto done;
214 dotgit = strstr(base, ".git");
215 if (dotgit)
216 *dotgit = '\0';
217 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
218 error = got_error_from_errno();
219 free(cwd);
220 goto done;
222 free(cwd);
223 } else if (argc == 2) {
224 repo_path = realpath(argv[0], NULL);
225 if (repo_path == NULL) {
226 error = got_error_from_errno();
227 goto done;
229 worktree_path = realpath(argv[1], NULL);
230 if (worktree_path == NULL) {
231 error = got_error_from_errno();
232 goto done;
234 } else
235 usage_checkout();
237 error = got_repo_open(&repo, repo_path);
238 if (error != NULL)
239 goto done;
240 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
241 if (error != NULL)
242 goto done;
244 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
245 if (error != NULL)
246 goto done;
248 error = got_worktree_open(&worktree, worktree_path);
249 if (error != NULL)
250 goto done;
252 error = got_worktree_checkout_files(worktree, head_ref, repo,
253 checkout_progress, worktree_path);
254 if (error != NULL)
255 goto done;
257 printf("Checked out %s\n", worktree_path);
258 printf("Now shut up and hack\n");
260 done:
261 free(repo_path);
262 free(worktree_path);
263 return error;
266 static const struct got_error *
267 print_patch(struct got_commit_object *commit, struct got_object_id *id,
268 struct got_repository *repo)
270 const struct got_error *err = NULL;
271 struct got_tree_object *tree1 = NULL, *tree2;
272 struct got_object *obj;
273 struct got_object_qid *qid;
275 err = got_object_open(&obj, repo, commit->tree_id);
276 if (err)
277 return err;
279 err = got_object_tree_open(&tree2, repo, obj);
280 got_object_close(obj);
281 if (err)
282 return err;
284 qid = SIMPLEQ_FIRST(&commit->parent_ids);
285 if (qid != NULL) {
286 struct got_commit_object *pcommit;
288 err = got_object_open(&obj, repo, qid->id);
289 if (err)
290 return err;
292 err = got_object_commit_open(&pcommit, repo, obj);
293 got_object_close(obj);
294 if (err)
295 return err;
297 err = got_object_open(&obj, repo, pcommit->tree_id);
298 got_object_commit_close(pcommit);
299 if (err)
300 return err;
301 err = got_object_tree_open(&tree1, repo, obj);
302 got_object_close(obj);
303 if (err)
304 return err;
307 err = got_diff_tree(tree1, tree2, repo, stdout);
308 if (tree1)
309 got_object_tree_close(tree1);
310 got_object_tree_close(tree2);
311 return err;
314 static char *
315 get_datestr(time_t *time, char *datebuf)
317 char *p, *s = ctime_r(time, datebuf);
318 p = strchr(s, '\n');
319 if (p)
320 *p = '\0';
321 return s;
324 static const struct got_error *
325 print_commit(struct got_commit_object *commit, struct got_object_id *id,
326 struct got_repository *repo, int show_patch)
328 const struct got_error *err = NULL;
329 char *id_str, *datestr, *logmsg, *line;
330 char datebuf[26];
331 time_t author_time, committer_time;
333 err = got_object_id_str(&id_str, id);
334 if (err)
335 return err;
337 author_time = mktime(&commit->tm_author);
338 committer_time = mktime(&commit->tm_committer);
339 #if 0
340 /* This would express the date in committer's timezone. */
341 author_time += commit->tm_author.tm_gmtoff;
342 committer_time += commit->tm_committer.tm_gmtoff;
343 #endif
345 printf("-----------------------------------------------\n");
346 printf("commit %s\n", id_str);
347 free(id_str);
348 datestr = get_datestr(&author_time, datebuf);
349 printf("author: %s %s UTC\n", commit->author, datestr);
350 if (strcmp(commit->author, commit->committer) != 0 ||
351 author_time != committer_time) {
352 datestr = get_datestr(&committer_time, datebuf);
353 printf("committer: %s %s UTC\n", commit->committer, datestr);
355 if (commit->nparents > 1) {
356 struct got_object_qid *qid;
357 int n = 1;
358 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
359 err = got_object_id_str(&id_str, qid->id);
360 if (err)
361 return err;
362 printf("parent %d: %s\n", n++, id_str);
363 free(id_str);
367 logmsg = strdup(commit->logmsg);
368 if (logmsg == NULL)
369 return got_error_from_errno();
371 do {
372 line = strsep(&logmsg, "\n");
373 if (line)
374 printf(" %s\n", line);
375 } while (line);
376 free(logmsg);
378 if (show_patch) {
379 err = print_patch(commit, id, repo);
380 if (err == 0)
381 printf("\n");
384 return err;
387 static const struct got_error *
388 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
389 struct got_repository *repo, int show_patch, int limit,
390 int first_parent_traversal)
392 const struct got_error *err;
393 struct got_commit_graph *graph;
394 int ncommits;
396 err = got_commit_graph_open(&graph, root_id, first_parent_traversal,
397 repo);
398 if (err)
399 return err;
400 err = got_commit_graph_iter_start(graph, root_id);
401 if (err)
402 return err;
403 do {
404 struct got_commit_object *commit;
405 struct got_object_id *id;
407 err = got_commit_graph_iter_next(&id, graph);
408 if (err) {
409 if (err->code == GOT_ERR_ITER_COMPLETED) {
410 err = NULL;
411 break;
413 if (err->code != GOT_ERR_ITER_NEED_MORE)
414 break;
415 err = got_commit_graph_fetch_commits(&ncommits,
416 graph, 1, repo);
417 if (err)
418 break;
419 else
420 continue;
422 if (id == NULL)
423 break;
425 err = got_object_open_as_commit(&commit, repo, id);
426 if (err)
427 return err;
428 err = print_commit(commit, id, repo, show_patch);
429 got_object_commit_close(commit);
430 if (err || (limit && --limit == 0))
431 break;
432 } while (ncommits > 0);
434 got_commit_graph_close(graph);
435 return err;
438 __dead static void
439 usage_log(void)
441 fprintf(stderr, "usage: %s log [-c commit] [-f] [ -l N ] [-p] "
442 "[repository-path]\n", getprogname());
443 exit(1);
446 static const struct got_error *
447 cmd_log(int argc, char *argv[])
449 const struct got_error *error;
450 struct got_repository *repo;
451 struct got_object_id *id = NULL;
452 struct got_object *obj = NULL;
453 char *repo_path = NULL;
454 char *start_commit = NULL;
455 int ch;
456 int show_patch = 0, limit = 0, first_parent_traversal = 0;
457 const char *errstr;
459 #ifndef PROFILE
460 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
461 err(1, "pledge");
462 #endif
464 while ((ch = getopt(argc, argv, "pc:l:f")) != -1) {
465 switch (ch) {
466 case 'p':
467 show_patch = 1;
468 break;
469 case 'c':
470 start_commit = optarg;
471 break;
472 case 'l':
473 limit = strtonum(optarg, 1, INT_MAX, &errstr);
474 if (errstr != NULL)
475 err(1, "-l option %s", errstr);
476 break;
477 case 'f':
478 first_parent_traversal = 1;
479 break;
480 default:
481 usage();
482 /* NOTREACHED */
486 argc -= optind;
487 argv += optind;
489 if (argc == 0) {
490 repo_path = getcwd(NULL, 0);
491 if (repo_path == NULL)
492 return got_error_from_errno();
493 } else if (argc == 1) {
494 repo_path = realpath(argv[0], NULL);
495 if (repo_path == NULL)
496 return got_error_from_errno();
497 } else
498 usage_log();
500 error = got_repo_open(&repo, repo_path);
501 free(repo_path);
502 if (error != NULL)
503 return error;
505 if (start_commit == NULL) {
506 struct got_reference *head_ref;
507 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
508 if (error != NULL)
509 return error;
510 error = got_ref_resolve(&id, repo, head_ref);
511 got_ref_close(head_ref);
512 if (error != NULL)
513 return error;
514 error = got_object_open(&obj, repo, id);
515 } else {
516 struct got_reference *ref;
517 error = got_ref_open(&ref, repo, start_commit);
518 if (error == NULL) {
519 error = got_ref_resolve(&id, repo, ref);
520 got_ref_close(ref);
521 if (error != NULL)
522 return error;
523 error = got_object_open(&obj, repo, id);
524 if (error != NULL)
525 return error;
527 if (obj == NULL) {
528 error = got_object_open_by_id_str(&obj, repo,
529 start_commit);
530 if (error != NULL)
531 return error;
532 id = got_object_get_id(obj);
533 if (id == NULL)
534 error = got_error_from_errno();
537 if (error != NULL)
538 return error;
539 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
540 error = print_commits(obj, id, repo, show_patch, limit,
541 first_parent_traversal);
542 else
543 error = got_error(GOT_ERR_OBJ_TYPE);
544 got_object_close(obj);
545 free(id);
546 got_repo_close(repo);
547 return error;
550 __dead static void
551 usage_diff(void)
553 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
554 getprogname());
555 exit(1);
558 static const struct got_error *
559 cmd_diff(int argc, char *argv[])
561 const struct got_error *error;
562 struct got_repository *repo = NULL;
563 struct got_object_id *id1 = NULL, *id2 = NULL;
564 struct got_object *obj1 = NULL, *obj2 = NULL;
565 char *repo_path = NULL;
566 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
567 int ch;
569 #ifndef PROFILE
570 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
571 err(1, "pledge");
572 #endif
574 while ((ch = getopt(argc, argv, "")) != -1) {
575 switch (ch) {
576 default:
577 usage();
578 /* NOTREACHED */
582 argc -= optind;
583 argv += optind;
585 if (argc == 0) {
586 usage_diff(); /* TODO show local worktree changes */
587 } else if (argc == 2) {
588 repo_path = getcwd(NULL, 0);
589 if (repo_path == NULL)
590 return got_error_from_errno();
591 obj_id_str1 = argv[0];
592 obj_id_str2 = argv[1];
593 } else if (argc == 3) {
594 repo_path = realpath(argv[0], NULL);
595 if (repo_path == NULL)
596 return got_error_from_errno();
597 obj_id_str1 = argv[1];
598 obj_id_str2 = argv[2];
599 } else
600 usage_diff();
602 error = got_repo_open(&repo, repo_path);
603 free(repo_path);
604 if (error != NULL)
605 goto done;
607 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
608 if (error == NULL) {
609 id1 = got_object_get_id(obj1);
610 if (id1 == NULL)
611 error = got_error_from_errno();
613 if (error != NULL)
614 goto done;
616 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
617 if (error == NULL) {
618 id2 = got_object_get_id(obj2);
619 if (id2 == NULL)
620 error = got_error_from_errno();
622 if (error != NULL)
623 goto done;
625 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
626 error = got_error(GOT_ERR_OBJ_TYPE);
627 goto done;
630 switch (got_object_get_type(obj1)) {
631 case GOT_OBJ_TYPE_BLOB:
632 error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
633 break;
634 case GOT_OBJ_TYPE_TREE:
635 error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
636 break;
637 case GOT_OBJ_TYPE_COMMIT:
638 error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
639 break;
640 default:
641 error = got_error(GOT_ERR_OBJ_TYPE);
644 done:
645 if (obj1)
646 got_object_close(obj1);
647 if (obj2)
648 got_object_close(obj2);
649 if (id1)
650 free(id1);
651 if (id2)
652 free(id2);
653 if (repo)
654 got_repo_close(repo);
655 return error;
658 __dead static void
659 usage_blame(void)
661 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
662 getprogname());
663 exit(1);
666 static const struct got_error *
667 cmd_blame(int argc, char *argv[])
669 const struct got_error *error;
670 struct got_repository *repo = NULL;
671 char *repo_path = NULL;
672 char *path = NULL;
673 struct got_object_id *commit_id = NULL;
674 char *commit_id_str = NULL;
675 int ch;
677 #ifndef PROFILE
678 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
679 err(1, "pledge");
680 #endif
682 while ((ch = getopt(argc, argv, "c:")) != -1) {
683 switch (ch) {
684 case 'c':
685 commit_id_str = optarg;
686 break;
687 default:
688 usage();
689 /* NOTREACHED */
693 argc -= optind;
694 argv += optind;
696 if (argc == 0) {
697 usage_blame();
698 } else if (argc == 1) {
699 repo_path = getcwd(NULL, 0);
700 if (repo_path == NULL)
701 return got_error_from_errno();
702 path = argv[0];
703 } else if (argc == 2) {
704 repo_path = realpath(argv[0], NULL);
705 if (repo_path == NULL)
706 return got_error_from_errno();
707 path = argv[1];
708 } else
709 usage_blame();
711 error = got_repo_open(&repo, repo_path);
712 free(repo_path);
713 if (error != NULL)
714 goto done;
716 if (commit_id_str == NULL) {
717 struct got_reference *head_ref;
718 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
719 if (error != NULL)
720 return error;
721 error = got_ref_resolve(&commit_id, repo, head_ref);
722 got_ref_close(head_ref);
723 if (error != NULL)
724 return error;
725 } else {
726 struct got_object *obj;
727 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
728 if (error != NULL)
729 return error;
730 commit_id = got_object_get_id(obj);
731 if (commit_id == NULL)
732 error = got_error_from_errno();
733 got_object_close(obj);
736 error = got_blame(path, commit_id, repo, stdout);
737 done:
738 free(commit_id);
739 if (repo)
740 got_repo_close(repo);
741 return error;
744 #ifdef notyet
745 static const struct got_error *
746 cmd_status(int argc __unused, char *argv[] __unused)
748 git_repository *repo = NULL;
749 git_status_list *status;
750 git_status_options statusopts;
751 size_t i;
753 git_libgit2_init();
755 if (git_repository_open_ext(&repo, ".", 0, NULL))
756 errx(1, "git_repository_open: %s", giterr_last()->message);
758 if (git_repository_is_bare(repo))
759 errx(1, "bar repository");
761 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
762 errx(1, "git_status_init_options: %s", giterr_last()->message);
764 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
765 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
766 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
767 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
769 if (git_status_list_new(&status, repo, &statusopts))
770 errx(1, "git_status_list_new: %s", giterr_last()->message);
772 for (i = 0; i < git_status_list_entrycount(status); i++) {
773 const git_status_entry *se;
775 se = git_status_byindex(status, i);
776 switch (se->status) {
777 case GIT_STATUS_WT_NEW:
778 printf("? %s\n", se->index_to_workdir->new_file.path);
779 break;
780 case GIT_STATUS_WT_MODIFIED:
781 printf("M %s\n", se->index_to_workdir->new_file.path);
782 break;
783 case GIT_STATUS_WT_DELETED:
784 printf("R %s\n", se->index_to_workdir->new_file.path);
785 break;
786 case GIT_STATUS_WT_RENAMED:
787 printf("m %s -> %s\n",
788 se->index_to_workdir->old_file.path,
789 se->index_to_workdir->new_file.path);
790 break;
791 case GIT_STATUS_WT_TYPECHANGE:
792 printf("t %s\n", se->index_to_workdir->new_file.path);
793 break;
794 case GIT_STATUS_INDEX_NEW:
795 printf("A %s\n", se->head_to_index->new_file.path);
796 break;
797 case GIT_STATUS_INDEX_MODIFIED:
798 printf("M %s\n", se->head_to_index->old_file.path);
799 break;
800 case GIT_STATUS_INDEX_DELETED:
801 printf("R %s\n", se->head_to_index->old_file.path);
802 break;
803 case GIT_STATUS_INDEX_RENAMED:
804 printf("m %s -> %s\n",
805 se->head_to_index->old_file.path,
806 se->head_to_index->new_file.path);
807 break;
808 case GIT_STATUS_INDEX_TYPECHANGE:
809 printf("t %s\n", se->head_to_index->old_file.path);
810 break;
811 case GIT_STATUS_CURRENT:
812 default:
813 break;
817 git_status_list_free(status);
818 git_repository_free(repo);
819 git_libgit2_shutdown();
821 return 0;
823 #endif