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 = argv[0];
189 cwd = getcwd(NULL, 0);
190 if (cwd == NULL)
191 err(1, "getcwd");
192 if (path_prefix[0])
193 base = basename(path_prefix);
194 else
195 base = basename(repo_path);
196 if (base == NULL)
197 err(1, "basename");
198 dotgit = strstr(base, ".git");
199 if (dotgit)
200 *dotgit = '\0';
201 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
202 error = got_error_from_errno();
203 free(cwd);
204 return error;
206 free(cwd);
207 } else if (argc == 2) {
208 repo_path = argv[0];
209 worktree_path = strdup(argv[1]);
210 if (worktree_path == NULL)
211 return got_error_from_errno();
212 } else
213 usage_checkout();
215 error = got_repo_open(&repo, repo_path);
216 if (error != NULL)
217 goto done;
218 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
219 if (error != NULL)
220 goto done;
222 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
223 if (error != NULL)
224 goto done;
226 error = got_worktree_open(&worktree, worktree_path);
227 if (error != NULL)
228 goto done;
230 error = got_worktree_checkout_files(worktree, head_ref, repo,
231 checkout_progress, worktree_path);
232 if (error != NULL)
233 goto done;
235 printf("checked out %s\n", worktree_path);
237 done:
238 free(worktree_path);
239 return error;
242 static const struct got_error *
243 print_patch(struct got_commit_object *commit, struct got_object_id *id,
244 struct got_repository *repo)
246 const struct got_error *err = NULL;
247 struct got_tree_object *tree1 = NULL, *tree2;
248 struct got_object *obj;
249 struct got_parent_id *pid;
251 err = got_object_open(&obj, repo, commit->tree_id);
252 if (err)
253 return err;
255 err = got_object_tree_open(&tree2, repo, obj);
256 got_object_close(obj);
257 if (err)
258 return err;
260 pid = SIMPLEQ_FIRST(&commit->parent_ids);
261 if (pid != NULL) {
262 struct got_commit_object *pcommit;
264 err = got_object_open(&obj, repo, pid->id);
265 if (err)
266 return err;
268 err = got_object_commit_open(&pcommit, repo, obj);
269 got_object_close(obj);
270 if (err)
271 return err;
273 err = got_object_open(&obj, repo, pcommit->tree_id);
274 got_object_commit_close(pcommit);
275 if (err)
276 return err;
277 err = got_object_tree_open(&tree1, repo, obj);
278 got_object_close(obj);
279 if (err)
280 return err;
283 err = got_diff_tree(tree1, tree2, repo, stdout);
284 if (tree1)
285 got_object_tree_close(tree1);
286 got_object_tree_close(tree2);
287 return err;
290 static const struct got_error *
291 print_commit(struct got_commit_object *commit, struct got_object_id *id,
292 struct got_repository *repo, int show_patch)
294 const struct got_error *err = NULL;
295 char *buf;
297 err = got_object_id_str(&buf, id);
298 if (err)
299 return err;
301 printf("-----------------------------------------------\n");
302 printf("commit: %s\n", buf);
303 printf("Author: %s\n", commit->author);
304 printf("\n%s\n", commit->logmsg);
306 if (show_patch) {
307 err = print_patch(commit, id, repo);
308 if (err == 0)
309 printf("\n");
312 free(buf);
313 return err;
316 struct commit_queue_entry {
317 TAILQ_ENTRY(commit_queue_entry) entry;
318 struct got_object_id *id;
319 struct got_commit_object *commit;
320 };
322 static const struct got_error *
323 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
324 struct got_repository *repo, int show_patch, int limit)
326 const struct got_error *err;
327 struct got_commit_object *root_commit;
328 TAILQ_HEAD(, commit_queue_entry) commits;
329 struct commit_queue_entry *entry;
331 TAILQ_INIT(&commits);
333 err = got_object_commit_open(&root_commit, repo, root_obj);
334 if (err)
335 return err;
337 entry = calloc(1, sizeof(*entry));
338 if (entry == NULL)
339 return got_error_from_errno();
340 entry->id = got_object_id_dup(root_id);
341 if (entry->id == NULL) {
342 err = got_error_from_errno();
343 free(entry);
344 return err;
346 entry->commit = root_commit;
347 TAILQ_INSERT_HEAD(&commits, entry, entry);
349 while (!TAILQ_EMPTY(&commits)) {
350 struct got_parent_id *pid;
352 entry = TAILQ_FIRST(&commits);
353 err = print_commit(entry->commit, entry->id, repo, show_patch);
354 if (err) {
355 while (!TAILQ_EMPTY(&commits)) {
356 entry = TAILQ_FIRST(&commits);
357 TAILQ_REMOVE(&commits, entry, entry);
358 got_object_commit_close(entry->commit);
359 free(entry->id);
360 free(entry);
362 break;
365 if (limit && --limit == 0)
366 break;
368 SIMPLEQ_FOREACH(pid, &entry->commit->parent_ids, entry) {
369 struct got_object *obj;
370 struct got_commit_object *pcommit;
371 struct commit_queue_entry *pentry;
373 err = got_object_open(&obj, repo, pid->id);
374 if (err)
375 break;
376 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
377 err = got_error(GOT_ERR_OBJ_TYPE);
378 break;
381 err = got_object_commit_open(&pcommit, repo, obj);
382 got_object_close(obj);
383 if (err)
384 break;
386 pentry = calloc(1, sizeof(*pentry));
387 if (pentry == NULL) {
388 err = got_error_from_errno();
389 got_object_commit_close(pcommit);
390 break;
392 pentry->id = got_object_id_dup(pid->id);
393 if (pentry->id == NULL) {
394 err = got_error_from_errno();
395 got_object_commit_close(pcommit);
396 break;
398 pentry->commit = pcommit;
399 TAILQ_INSERT_TAIL(&commits, pentry, entry);
402 TAILQ_REMOVE(&commits, entry, entry);
403 got_object_commit_close(entry->commit);
404 free(entry->id);
405 free(entry);
408 return err;
411 __dead void
412 usage_log(void)
414 fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
415 "[repository-path]\n", getprogname());
416 exit(1);
419 const struct got_error *
420 cmd_log(int argc, char *argv[])
422 const struct got_error *error;
423 struct got_repository *repo;
424 struct got_object_id *id = NULL;
425 struct got_object *obj;
426 char *repo_path = NULL;
427 char *start_commit = NULL;
428 int ch;
429 int show_patch = 0, limit = 0;
430 const char *errstr;
432 #ifndef PROFILE
433 if (pledge("stdio rpath wpath cpath", NULL) == -1)
434 err(1, "pledge");
435 #endif
437 while ((ch = getopt(argc, argv, "pc:l:")) != -1) {
438 switch (ch) {
439 case 'p':
440 show_patch = 1;
441 break;
442 case 'c':
443 start_commit = optarg;
444 break;
445 case 'l':
446 limit = strtonum(optarg, 1, INT_MAX, &errstr);
447 if (errstr != NULL)
448 err(1, "-l option %s", errstr);
449 break;
450 default:
451 usage();
452 /* NOTREACHED */
456 argc -= optind;
457 argv += optind;
459 if (argc == 0) {
460 repo_path = getcwd(NULL, 0);
461 if (repo_path == NULL)
462 err(1, "getcwd");
463 } else if (argc == 1) {
464 repo_path = argv[0];
465 } else
466 usage_log();
468 error = got_repo_open(&repo, repo_path);
469 if (error != NULL)
470 return error;
472 if (start_commit == NULL) {
473 struct got_reference *head_ref;
474 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
475 if (error != NULL)
476 return error;
477 error = got_ref_resolve(&id, repo, head_ref);
478 got_ref_close(head_ref);
479 if (error != NULL)
480 return error;
481 error = got_object_open(&obj, repo, id);
482 } else {
483 error = got_object_open_by_id_str(&obj, repo, start_commit);
484 if (error == NULL) {
485 id = got_object_get_id(obj);
486 if (id == NULL)
487 error = got_error_from_errno();
490 if (error != NULL)
491 return error;
492 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
493 error = print_commits(obj, id, repo, show_patch, limit);
494 else
495 error = got_error(GOT_ERR_OBJ_TYPE);
496 got_object_close(obj);
497 free(id);
498 got_repo_close(repo);
499 return error;
502 static const struct got_error *
503 diff_blobs(struct got_object *obj1, struct got_object *obj2,
504 struct got_repository *repo)
506 const struct got_error *err;
507 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
509 err = got_object_blob_open(&blob1, repo, obj1, 8192);
510 if (err)
511 goto done;
512 err = got_object_blob_open(&blob2, repo, obj2, 81992);
513 if (err)
514 goto done;
516 err = got_diff_blob(blob1, blob2, NULL, NULL, stdout);
517 done:
518 if (blob1)
519 got_object_blob_close(blob1);
520 if (blob2)
521 got_object_blob_close(blob2);
522 return err;
525 static const struct got_error *
526 diff_trees(struct got_object *obj1, struct got_object *obj2,
527 struct got_repository *repo)
529 const struct got_error *err;
530 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
532 err = got_object_tree_open(&tree1, repo, obj1);
533 if (err)
534 goto done;
535 err = got_object_tree_open(&tree2, repo, obj2);
536 if (err)
537 goto done;
539 err = got_diff_tree(tree1, tree2, repo, stdout);
540 done:
541 if (tree1)
542 got_object_tree_close(tree1);
543 if (tree2)
544 got_object_tree_close(tree2);
545 return err;
548 static const struct got_error *
549 diff_commits(struct got_object *obj1, struct got_object *obj2,
550 struct got_repository *repo)
552 const struct got_error *err;
553 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
554 struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
556 err = got_object_commit_open(&commit1, repo, obj1);
557 if (err)
558 goto done;
559 err = got_object_commit_open(&commit2, repo, obj2);
560 if (err)
561 goto done;
563 err = got_object_open(&tree_obj1, repo, commit1->tree_id);
564 if (err)
565 goto done;
566 err = got_object_open(&tree_obj2, repo, commit2->tree_id);
567 if (err)
568 goto done;
570 err = diff_trees(tree_obj1, tree_obj2, repo);
571 done:
572 if (tree_obj1)
573 got_object_close(tree_obj1);
574 if (tree_obj2)
575 got_object_close(tree_obj2);
576 if (commit1)
577 got_object_commit_close(commit1);
578 if (commit2)
579 got_object_commit_close(commit2);
580 return err;
584 __dead void
585 usage_diff(void)
587 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
588 getprogname());
589 exit(1);
592 const struct got_error *
593 cmd_diff(int argc, char *argv[])
595 const struct got_error *error;
596 struct got_repository *repo = NULL;
597 struct got_object_id *id1 = NULL, *id2 = NULL;
598 struct got_object *obj1 = NULL, *obj2 = NULL;
599 char *repo_path = NULL;
600 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
601 int ch;
603 #ifndef PROFILE
604 if (pledge("stdio rpath wpath cpath", NULL) == -1)
605 err(1, "pledge");
606 #endif
608 while ((ch = getopt(argc, argv, "")) != -1) {
609 switch (ch) {
610 default:
611 usage();
612 /* NOTREACHED */
616 argc -= optind;
617 argv += optind;
619 if (argc == 0) {
620 usage_diff(); /* TODO show local worktree changes */
621 } else if (argc == 2) {
622 repo_path = getcwd(NULL, 0);
623 if (repo_path == NULL)
624 err(1, "getcwd");
625 obj_id_str1 = argv[0];
626 obj_id_str2 = argv[1];
627 } else if (argc == 3) {
628 repo_path = argv[0];
629 obj_id_str1 = argv[1];
630 obj_id_str2 = argv[2];
631 } else
632 usage_diff();
634 error = got_repo_open(&repo, repo_path);
635 if (error != NULL)
636 goto done;
638 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
639 if (error == NULL) {
640 id1 = got_object_get_id(obj1);
641 if (id1 == NULL)
642 error = got_error_from_errno();
644 if (error != NULL)
645 goto done;
647 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
648 if (error == NULL) {
649 id2 = got_object_get_id(obj2);
650 if (id2 == NULL)
651 error = got_error_from_errno();
653 if (error != NULL)
654 goto done;
656 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
657 error = got_error(GOT_ERR_OBJ_TYPE);
658 goto done;
661 switch (got_object_get_type(obj1)) {
662 case GOT_OBJ_TYPE_BLOB:
663 error = diff_blobs(obj1, obj2, repo);
664 break;
665 case GOT_OBJ_TYPE_TREE:
666 error = diff_trees(obj1, obj2, repo);
667 break;
668 case GOT_OBJ_TYPE_COMMIT:
669 error = diff_commits(obj1, obj2, repo);
670 break;
671 default:
672 error = got_error(GOT_ERR_OBJ_TYPE);
675 done:
676 if (obj1)
677 got_object_close(obj1);
678 if (obj2)
679 got_object_close(obj2);
680 if (id1)
681 free(id1);
682 if (id2)
683 free(id2);
684 if (repo)
685 got_repo_close(repo);
686 return error;
689 #ifdef notyet
690 const struct got_error *
691 cmd_status(int argc __unused, char *argv[] __unused)
693 git_repository *repo = NULL;
694 git_status_list *status;
695 git_status_options statusopts;
696 size_t i;
698 git_libgit2_init();
700 if (git_repository_open_ext(&repo, ".", 0, NULL))
701 errx(1, "git_repository_open: %s", giterr_last()->message);
703 if (git_repository_is_bare(repo))
704 errx(1, "bar repository");
706 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
707 errx(1, "git_status_init_options: %s", giterr_last()->message);
709 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
710 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
711 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
712 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
714 if (git_status_list_new(&status, repo, &statusopts))
715 errx(1, "git_status_list_new: %s", giterr_last()->message);
717 for (i = 0; i < git_status_list_entrycount(status); i++) {
718 const git_status_entry *se;
720 se = git_status_byindex(status, i);
721 switch (se->status) {
722 case GIT_STATUS_WT_NEW:
723 printf("? %s\n", se->index_to_workdir->new_file.path);
724 break;
725 case GIT_STATUS_WT_MODIFIED:
726 printf("M %s\n", se->index_to_workdir->new_file.path);
727 break;
728 case GIT_STATUS_WT_DELETED:
729 printf("R %s\n", se->index_to_workdir->new_file.path);
730 break;
731 case GIT_STATUS_WT_RENAMED:
732 printf("m %s -> %s\n",
733 se->index_to_workdir->old_file.path,
734 se->index_to_workdir->new_file.path);
735 break;
736 case GIT_STATUS_WT_TYPECHANGE:
737 printf("t %s\n", se->index_to_workdir->new_file.path);
738 break;
739 case GIT_STATUS_INDEX_NEW:
740 printf("A %s\n", se->head_to_index->new_file.path);
741 break;
742 case GIT_STATUS_INDEX_MODIFIED:
743 printf("M %s\n", se->head_to_index->old_file.path);
744 break;
745 case GIT_STATUS_INDEX_DELETED:
746 printf("R %s\n", se->head_to_index->old_file.path);
747 break;
748 case GIT_STATUS_INDEX_RENAMED:
749 printf("m %s -> %s\n",
750 se->head_to_index->old_file.path,
751 se->head_to_index->new_file.path);
752 break;
753 case GIT_STATUS_INDEX_TYPECHANGE:
754 printf("t %s\n", se->head_to_index->old_file.path);
755 break;
756 case GIT_STATUS_CURRENT:
757 default:
758 break;
762 git_status_list_free(status);
763 git_repository_free(repo);
764 git_libgit2_shutdown();
766 return 0;
768 #endif