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>
21 #include <err.h>
22 #include <errno.h>
23 #include <locale.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <libgen.h>
30 #include "got_error.h"
31 #include "got_object.h"
32 #include "got_refs.h"
33 #include "got_repository.h"
34 #include "got_worktree.h"
35 #include "got_diff.h"
37 #ifndef nitems
38 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
39 #endif
41 struct cmd {
42 const char *cmd_name;
43 const struct got_error *(*cmd_main)(int, char *[]);
44 void (*cmd_usage)(void);
45 const char *cmd_descr;
46 };
48 __dead void usage(void);
49 __dead void usage_checkout(void);
50 __dead void usage_log(void);
51 __dead void usage_diff(void);
53 const struct got_error* cmd_checkout(int, char *[]);
54 const struct got_error* cmd_log(int, char *[]);
55 const struct got_error* cmd_diff(int, char *[]);
56 const struct got_error* cmd_status(int, char *[]);
58 struct cmd got_commands[] = {
59 { "checkout", cmd_checkout, usage_checkout,
60 "check out a new work tree from a repository" },
61 { "log", cmd_log, usage_log,
62 "show repository history" },
63 { "diff", cmd_diff, usage_diff,
64 "compare files and directories" },
65 #ifdef notyet
66 { "status", cmd_status, usage_status,
67 "show modification status of files" },
68 #endif
69 };
71 int
72 main(int argc, char *argv[])
73 {
74 struct cmd *cmd;
75 unsigned int i;
76 int ch;
77 int hflag = 0;
79 setlocale(LC_ALL, "");
81 while ((ch = getopt(argc, argv, "h")) != -1) {
82 switch (ch) {
83 case 'h':
84 hflag = 1;
85 break;
86 default:
87 usage();
88 /* NOTREACHED */
89 }
90 }
92 argc -= optind;
93 argv += optind;
94 optind = 0;
96 if (argc <= 0)
97 usage();
99 for (i = 0; i < nitems(got_commands); i++) {
100 const struct got_error *error;
102 cmd = &got_commands[i];
104 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
105 continue;
107 if (hflag)
108 got_commands[i].cmd_usage();
110 error = got_commands[i].cmd_main(argc, argv);
111 if (error) {
112 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
113 return 1;
116 return 0;
119 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
120 return 1;
123 __dead void
124 usage(void)
126 int i;
128 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
129 "Available commands:\n", getprogname());
130 for (i = 0; i < nitems(got_commands); i++) {
131 struct cmd *cmd = &got_commands[i];
132 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
134 exit(1);
137 __dead void
138 usage_checkout(void)
140 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
141 "[worktree-path]\n", getprogname());
142 exit(1);
145 static void
146 checkout_progress(void *arg, const char *path)
148 char *worktree_path = arg;
150 while (path[0] == '/')
151 path++;
153 printf("A %s/%s\n", worktree_path, path);
156 const struct got_error *
157 cmd_checkout(int argc, char *argv[])
159 const struct got_error *error = NULL;
160 struct got_repository *repo = NULL;
161 struct got_reference *head_ref = NULL;
162 struct got_worktree *worktree = NULL;
163 char *repo_path = NULL;
164 char *worktree_path = NULL;
165 const char *path_prefix = "";
166 int ch;
168 while ((ch = getopt(argc, argv, "p:")) != -1) {
169 switch (ch) {
170 case 'p':
171 path_prefix = optarg;
172 break;
173 default:
174 usage();
175 /* NOTREACHED */
179 argc -= optind;
180 argv += optind;
182 #ifndef PROFILE
183 if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
184 err(1, "pledge");
185 #endif
186 if (argc == 1) {
187 char *cwd, *base, *dotgit;
188 repo_path = realpath(argv[0], NULL);
189 if (repo_path == NULL)
190 return got_error_from_errno();
191 cwd = getcwd(NULL, 0);
192 if (cwd == NULL) {
193 error = got_error_from_errno();
194 goto done;
196 if (path_prefix[0])
197 base = basename(path_prefix);
198 else
199 base = basename(repo_path);
200 if (base == NULL) {
201 error = got_error_from_errno();
202 goto done;
204 dotgit = strstr(base, ".git");
205 if (dotgit)
206 *dotgit = '\0';
207 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
208 error = got_error_from_errno();
209 free(cwd);
210 goto done;
212 free(cwd);
213 } else if (argc == 2) {
214 repo_path = realpath(argv[0], NULL);
215 if (repo_path == NULL) {
216 error = got_error_from_errno();
217 goto done;
219 worktree_path = realpath(argv[1], NULL);
220 if (worktree_path == NULL) {
221 error = got_error_from_errno();
222 goto done;
224 } else
225 usage_checkout();
227 error = got_repo_open(&repo, repo_path);
228 free(repo_path);
229 if (error != NULL)
230 goto done;
231 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
232 if (error != NULL)
233 goto done;
235 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
236 if (error != NULL)
237 goto done;
239 error = got_worktree_open(&worktree, worktree_path);
240 if (error != NULL)
241 goto done;
243 error = got_worktree_checkout_files(worktree, head_ref, repo,
244 checkout_progress, worktree_path);
245 if (error != NULL)
246 goto done;
248 printf("checked out %s\n", worktree_path);
250 done:
251 free(repo_path);
252 free(worktree_path);
253 return error;
256 static const struct got_error *
257 print_patch(struct got_commit_object *commit, struct got_object_id *id,
258 struct got_repository *repo)
260 const struct got_error *err = NULL;
261 struct got_tree_object *tree1 = NULL, *tree2;
262 struct got_object *obj;
263 struct got_parent_id *pid;
265 err = got_object_open(&obj, repo, commit->tree_id);
266 if (err)
267 return err;
269 err = got_object_tree_open(&tree2, repo, obj);
270 got_object_close(obj);
271 if (err)
272 return err;
274 pid = SIMPLEQ_FIRST(&commit->parent_ids);
275 if (pid != NULL) {
276 struct got_commit_object *pcommit;
278 err = got_object_open(&obj, repo, pid->id);
279 if (err)
280 return err;
282 err = got_object_commit_open(&pcommit, repo, obj);
283 got_object_close(obj);
284 if (err)
285 return err;
287 err = got_object_open(&obj, repo, pcommit->tree_id);
288 got_object_commit_close(pcommit);
289 if (err)
290 return err;
291 err = got_object_tree_open(&tree1, repo, obj);
292 got_object_close(obj);
293 if (err)
294 return err;
297 err = got_diff_tree(tree1, tree2, repo, stdout);
298 if (tree1)
299 got_object_tree_close(tree1);
300 got_object_tree_close(tree2);
301 return err;
304 static const struct got_error *
305 print_commit(struct got_commit_object *commit, struct got_object_id *id,
306 struct got_repository *repo, int show_patch)
308 const struct got_error *err = NULL;
309 char *buf;
311 err = got_object_id_str(&buf, id);
312 if (err)
313 return err;
315 printf("-----------------------------------------------\n");
316 printf("commit: %s\n", buf);
317 printf("Author: %s\n", commit->author);
318 printf("\n%s\n", commit->logmsg);
320 if (show_patch) {
321 err = print_patch(commit, id, repo);
322 if (err == 0)
323 printf("\n");
326 free(buf);
327 return err;
330 struct commit_queue_entry {
331 TAILQ_ENTRY(commit_queue_entry) entry;
332 struct got_object_id *id;
333 struct got_commit_object *commit;
334 };
336 static const struct got_error *
337 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
338 struct got_repository *repo, int show_patch, int limit)
340 const struct got_error *err;
341 struct got_commit_object *root_commit;
342 TAILQ_HEAD(, commit_queue_entry) commits;
343 struct commit_queue_entry *entry;
345 TAILQ_INIT(&commits);
347 err = got_object_commit_open(&root_commit, repo, root_obj);
348 if (err)
349 return err;
351 entry = calloc(1, sizeof(*entry));
352 if (entry == NULL)
353 return got_error_from_errno();
354 entry->id = got_object_id_dup(root_id);
355 if (entry->id == NULL) {
356 err = got_error_from_errno();
357 free(entry);
358 return err;
360 entry->commit = root_commit;
361 TAILQ_INSERT_HEAD(&commits, entry, entry);
363 while (!TAILQ_EMPTY(&commits)) {
364 struct got_parent_id *pid;
366 entry = TAILQ_FIRST(&commits);
367 err = print_commit(entry->commit, entry->id, repo, show_patch);
368 if (err) {
369 while (!TAILQ_EMPTY(&commits)) {
370 entry = TAILQ_FIRST(&commits);
371 TAILQ_REMOVE(&commits, entry, entry);
372 got_object_commit_close(entry->commit);
373 free(entry->id);
374 free(entry);
376 break;
379 if (limit && --limit == 0)
380 break;
382 SIMPLEQ_FOREACH(pid, &entry->commit->parent_ids, entry) {
383 struct got_object *obj;
384 struct got_commit_object *pcommit;
385 struct commit_queue_entry *pentry;
387 err = got_object_open(&obj, repo, pid->id);
388 if (err)
389 break;
390 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
391 err = got_error(GOT_ERR_OBJ_TYPE);
392 break;
395 err = got_object_commit_open(&pcommit, repo, obj);
396 got_object_close(obj);
397 if (err)
398 break;
400 pentry = calloc(1, sizeof(*pentry));
401 if (pentry == NULL) {
402 err = got_error_from_errno();
403 got_object_commit_close(pcommit);
404 break;
406 pentry->id = got_object_id_dup(pid->id);
407 if (pentry->id == NULL) {
408 err = got_error_from_errno();
409 got_object_commit_close(pcommit);
410 break;
412 pentry->commit = pcommit;
413 TAILQ_INSERT_TAIL(&commits, pentry, entry);
416 TAILQ_REMOVE(&commits, entry, entry);
417 got_object_commit_close(entry->commit);
418 free(entry->id);
419 free(entry);
422 return err;
425 __dead void
426 usage_log(void)
428 fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
429 "[repository-path]\n", getprogname());
430 exit(1);
433 const struct got_error *
434 cmd_log(int argc, char *argv[])
436 const struct got_error *error;
437 struct got_repository *repo;
438 struct got_object_id *id = NULL;
439 struct got_object *obj;
440 char *repo_path = NULL;
441 char *start_commit = NULL;
442 int ch;
443 int show_patch = 0, limit = 0;
444 const char *errstr;
446 #ifndef PROFILE
447 if (pledge("stdio rpath wpath cpath", NULL) == -1)
448 err(1, "pledge");
449 #endif
451 while ((ch = getopt(argc, argv, "pc:l:")) != -1) {
452 switch (ch) {
453 case 'p':
454 show_patch = 1;
455 break;
456 case 'c':
457 start_commit = optarg;
458 break;
459 case 'l':
460 limit = strtonum(optarg, 1, INT_MAX, &errstr);
461 if (errstr != NULL)
462 err(1, "-l option %s", errstr);
463 break;
464 default:
465 usage();
466 /* NOTREACHED */
470 argc -= optind;
471 argv += optind;
473 if (argc == 0) {
474 repo_path = getcwd(NULL, 0);
475 if (repo_path == NULL)
476 return got_error_from_errno();
477 } else if (argc == 1) {
478 repo_path = realpath(argv[0], NULL);
479 if (repo_path == NULL)
480 return got_error_from_errno();
481 } else
482 usage_log();
484 error = got_repo_open(&repo, repo_path);
485 free(repo_path);
486 if (error != NULL)
487 return error;
489 if (start_commit == NULL) {
490 struct got_reference *head_ref;
491 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
492 if (error != NULL)
493 return error;
494 error = got_ref_resolve(&id, repo, head_ref);
495 got_ref_close(head_ref);
496 if (error != NULL)
497 return error;
498 error = got_object_open(&obj, repo, id);
499 } else {
500 error = got_object_open_by_id_str(&obj, repo, start_commit);
501 if (error == NULL) {
502 id = got_object_get_id(obj);
503 if (id == NULL)
504 error = got_error_from_errno();
507 if (error != NULL)
508 return error;
509 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
510 error = print_commits(obj, id, repo, show_patch, limit);
511 else
512 error = got_error(GOT_ERR_OBJ_TYPE);
513 got_object_close(obj);
514 free(id);
515 got_repo_close(repo);
516 return error;
519 static const struct got_error *
520 diff_blobs(struct got_object *obj1, struct got_object *obj2,
521 struct got_repository *repo)
523 const struct got_error *err;
524 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
526 err = got_object_blob_open(&blob1, repo, obj1, 8192);
527 if (err)
528 goto done;
529 err = got_object_blob_open(&blob2, repo, obj2, 81992);
530 if (err)
531 goto done;
533 err = got_diff_blob(blob1, blob2, NULL, NULL, stdout);
534 done:
535 if (blob1)
536 got_object_blob_close(blob1);
537 if (blob2)
538 got_object_blob_close(blob2);
539 return err;
542 static const struct got_error *
543 diff_trees(struct got_object *obj1, struct got_object *obj2,
544 struct got_repository *repo)
546 const struct got_error *err;
547 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
549 err = got_object_tree_open(&tree1, repo, obj1);
550 if (err)
551 goto done;
552 err = got_object_tree_open(&tree2, repo, obj2);
553 if (err)
554 goto done;
556 err = got_diff_tree(tree1, tree2, repo, stdout);
557 done:
558 if (tree1)
559 got_object_tree_close(tree1);
560 if (tree2)
561 got_object_tree_close(tree2);
562 return err;
565 static const struct got_error *
566 diff_commits(struct got_object *obj1, struct got_object *obj2,
567 struct got_repository *repo)
569 const struct got_error *err;
570 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
571 struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
573 err = got_object_commit_open(&commit1, repo, obj1);
574 if (err)
575 goto done;
576 err = got_object_commit_open(&commit2, repo, obj2);
577 if (err)
578 goto done;
580 err = got_object_open(&tree_obj1, repo, commit1->tree_id);
581 if (err)
582 goto done;
583 err = got_object_open(&tree_obj2, repo, commit2->tree_id);
584 if (err)
585 goto done;
587 err = diff_trees(tree_obj1, tree_obj2, repo);
588 done:
589 if (tree_obj1)
590 got_object_close(tree_obj1);
591 if (tree_obj2)
592 got_object_close(tree_obj2);
593 if (commit1)
594 got_object_commit_close(commit1);
595 if (commit2)
596 got_object_commit_close(commit2);
597 return err;
601 __dead void
602 usage_diff(void)
604 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
605 getprogname());
606 exit(1);
609 const struct got_error *
610 cmd_diff(int argc, char *argv[])
612 const struct got_error *error;
613 struct got_repository *repo = NULL;
614 struct got_object_id *id1 = NULL, *id2 = NULL;
615 struct got_object *obj1 = NULL, *obj2 = NULL;
616 char *repo_path = NULL;
617 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
618 int ch;
620 #ifndef PROFILE
621 if (pledge("stdio rpath wpath cpath", NULL) == -1)
622 err(1, "pledge");
623 #endif
625 while ((ch = getopt(argc, argv, "")) != -1) {
626 switch (ch) {
627 default:
628 usage();
629 /* NOTREACHED */
633 argc -= optind;
634 argv += optind;
636 if (argc == 0) {
637 usage_diff(); /* TODO show local worktree changes */
638 } else if (argc == 2) {
639 repo_path = getcwd(NULL, 0);
640 if (repo_path == NULL)
641 return got_error_from_errno();
642 obj_id_str1 = argv[0];
643 obj_id_str2 = argv[1];
644 } else if (argc == 3) {
645 repo_path = realpath(argv[0], NULL);
646 if (repo_path == NULL)
647 return got_error_from_errno();
648 obj_id_str1 = argv[1];
649 obj_id_str2 = argv[2];
650 } else
651 usage_diff();
653 error = got_repo_open(&repo, repo_path);
654 free(repo_path);
655 if (error != NULL)
656 goto done;
658 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
659 if (error == NULL) {
660 id1 = got_object_get_id(obj1);
661 if (id1 == NULL)
662 error = got_error_from_errno();
664 if (error != NULL)
665 goto done;
667 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
668 if (error == NULL) {
669 id2 = got_object_get_id(obj2);
670 if (id2 == NULL)
671 error = got_error_from_errno();
673 if (error != NULL)
674 goto done;
676 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
677 error = got_error(GOT_ERR_OBJ_TYPE);
678 goto done;
681 switch (got_object_get_type(obj1)) {
682 case GOT_OBJ_TYPE_BLOB:
683 error = diff_blobs(obj1, obj2, repo);
684 break;
685 case GOT_OBJ_TYPE_TREE:
686 error = diff_trees(obj1, obj2, repo);
687 break;
688 case GOT_OBJ_TYPE_COMMIT:
689 error = diff_commits(obj1, obj2, repo);
690 break;
691 default:
692 error = got_error(GOT_ERR_OBJ_TYPE);
695 done:
696 if (obj1)
697 got_object_close(obj1);
698 if (obj2)
699 got_object_close(obj2);
700 if (id1)
701 free(id1);
702 if (id2)
703 free(id2);
704 if (repo)
705 got_repo_close(repo);
706 return error;
709 #ifdef notyet
710 const struct got_error *
711 cmd_status(int argc __unused, char *argv[] __unused)
713 git_repository *repo = NULL;
714 git_status_list *status;
715 git_status_options statusopts;
716 size_t i;
718 git_libgit2_init();
720 if (git_repository_open_ext(&repo, ".", 0, NULL))
721 errx(1, "git_repository_open: %s", giterr_last()->message);
723 if (git_repository_is_bare(repo))
724 errx(1, "bar repository");
726 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
727 errx(1, "git_status_init_options: %s", giterr_last()->message);
729 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
730 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
731 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
732 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
734 if (git_status_list_new(&status, repo, &statusopts))
735 errx(1, "git_status_list_new: %s", giterr_last()->message);
737 for (i = 0; i < git_status_list_entrycount(status); i++) {
738 const git_status_entry *se;
740 se = git_status_byindex(status, i);
741 switch (se->status) {
742 case GIT_STATUS_WT_NEW:
743 printf("? %s\n", se->index_to_workdir->new_file.path);
744 break;
745 case GIT_STATUS_WT_MODIFIED:
746 printf("M %s\n", se->index_to_workdir->new_file.path);
747 break;
748 case GIT_STATUS_WT_DELETED:
749 printf("R %s\n", se->index_to_workdir->new_file.path);
750 break;
751 case GIT_STATUS_WT_RENAMED:
752 printf("m %s -> %s\n",
753 se->index_to_workdir->old_file.path,
754 se->index_to_workdir->new_file.path);
755 break;
756 case GIT_STATUS_WT_TYPECHANGE:
757 printf("t %s\n", se->index_to_workdir->new_file.path);
758 break;
759 case GIT_STATUS_INDEX_NEW:
760 printf("A %s\n", se->head_to_index->new_file.path);
761 break;
762 case GIT_STATUS_INDEX_MODIFIED:
763 printf("M %s\n", se->head_to_index->old_file.path);
764 break;
765 case GIT_STATUS_INDEX_DELETED:
766 printf("R %s\n", se->head_to_index->old_file.path);
767 break;
768 case GIT_STATUS_INDEX_RENAMED:
769 printf("m %s -> %s\n",
770 se->head_to_index->old_file.path,
771 se->head_to_index->new_file.path);
772 break;
773 case GIT_STATUS_INDEX_TYPECHANGE:
774 printf("t %s\n", se->head_to_index->old_file.path);
775 break;
776 case GIT_STATUS_CURRENT:
777 default:
778 break;
782 git_status_list_free(status);
783 git_repository_free(repo);
784 git_libgit2_shutdown();
786 return 0;
788 #endif