2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
18 #include <sys/queue.h>
19 #include <sys/limits.h>
30 #include "got_error.h"
31 #include "got_object.h"
32 #include "got_reference.h"
33 #include "got_repository.h"
34 #include "got_worktree.h"
38 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
43 const struct got_error *(*cmd_main)(int, char *[]);
44 void (*cmd_usage)(void);
45 const char *cmd_descr;
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" },
66 { "status", cmd_status, usage_status,
67 "show modification status of files" },
72 main(int argc, char *argv[])
79 setlocale(LC_ALL, "");
81 while ((ch = getopt(argc, argv, "h")) != -1) {
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])))
108 got_commands[i].cmd_usage();
110 error = got_commands[i].cmd_main(argc, argv);
112 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
119 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
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);
140 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
141 "[worktree-path]\n", getprogname());
146 checkout_progress(void *arg, const char *path)
148 char *worktree_path = arg;
150 while (path[0] == '/')
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 = "";
168 while ((ch = getopt(argc, argv, "p:")) != -1) {
171 path_prefix = optarg;
183 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -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);
193 error = got_error_from_errno();
197 base = basename(path_prefix);
199 base = basename(repo_path);
201 error = got_error_from_errno();
204 dotgit = strstr(base, ".git");
207 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
208 error = got_error_from_errno();
213 } else if (argc == 2) {
214 repo_path = realpath(argv[0], NULL);
215 if (repo_path == NULL) {
216 error = got_error_from_errno();
219 worktree_path = realpath(argv[1], NULL);
220 if (worktree_path == NULL) {
221 error = got_error_from_errno();
227 error = got_repo_open(&repo, repo_path);
231 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
235 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
239 error = got_worktree_open(&worktree, worktree_path);
243 error = got_worktree_checkout_files(worktree, head_ref, repo,
244 checkout_progress, worktree_path);
248 printf("checked out %s\n", worktree_path);
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);
269 err = got_object_tree_open(&tree2, repo, obj);
270 got_object_close(obj);
274 pid = SIMPLEQ_FIRST(&commit->parent_ids);
276 struct got_commit_object *pcommit;
278 err = got_object_open(&obj, repo, pid->id);
282 err = got_object_commit_open(&pcommit, repo, obj);
283 got_object_close(obj);
287 err = got_object_open(&obj, repo, pcommit->tree_id);
288 got_object_commit_close(pcommit);
291 err = got_object_tree_open(&tree1, repo, obj);
292 got_object_close(obj);
297 err = got_diff_tree(tree1, tree2, repo, stdout);
299 got_object_tree_close(tree1);
300 got_object_tree_close(tree2);
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;
311 err = got_object_id_str(&buf, id);
315 printf("-----------------------------------------------\n");
316 printf("commit: %s\n", buf);
317 printf("Author: %s\n", commit->author);
318 if (strcmp(commit->author, commit->committer) != 0)
319 printf("Committer: %s\n", commit->committer);
320 printf("\n%s\n", commit->logmsg);
323 err = print_patch(commit, id, repo);
332 struct commit_queue_entry {
333 TAILQ_ENTRY(commit_queue_entry) entry;
334 struct got_object_id *id;
335 struct got_commit_object *commit;
338 static const struct got_error *
339 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
340 struct got_repository *repo, int show_patch, int limit)
342 const struct got_error *err;
343 struct got_commit_object *root_commit;
344 TAILQ_HEAD(, commit_queue_entry) commits;
345 struct commit_queue_entry *entry;
347 TAILQ_INIT(&commits);
349 err = got_object_commit_open(&root_commit, repo, root_obj);
353 entry = calloc(1, sizeof(*entry));
355 return got_error_from_errno();
356 entry->id = got_object_id_dup(root_id);
357 if (entry->id == NULL) {
358 err = got_error_from_errno();
362 entry->commit = root_commit;
363 TAILQ_INSERT_HEAD(&commits, entry, entry);
365 while (!TAILQ_EMPTY(&commits)) {
366 struct got_parent_id *pid;
367 struct got_object *obj;
368 struct got_commit_object *pcommit;
369 struct commit_queue_entry *pentry;
371 entry = TAILQ_FIRST(&commits);
373 err = print_commit(entry->commit, entry->id, repo, show_patch);
377 if (limit && --limit == 0)
380 if (entry->commit->nparents == 0)
383 /* Follow the first parent (TODO: handle merge commits). */
384 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
385 err = got_object_open(&obj, repo, pid->id);
388 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
389 err = got_error(GOT_ERR_OBJ_TYPE);
393 err = got_object_commit_open(&pcommit, repo, obj);
394 got_object_close(obj);
398 pentry = calloc(1, sizeof(*pentry));
399 if (pentry == NULL) {
400 err = got_error_from_errno();
401 got_object_commit_close(pcommit);
404 pentry->id = got_object_id_dup(pid->id);
405 if (pentry->id == NULL) {
406 err = got_error_from_errno();
407 got_object_commit_close(pcommit);
410 pentry->commit = pcommit;
411 TAILQ_INSERT_TAIL(&commits, pentry, entry);
413 TAILQ_REMOVE(&commits, entry, entry);
414 got_object_commit_close(entry->commit);
419 while (!TAILQ_EMPTY(&commits)) {
420 entry = TAILQ_FIRST(&commits);
421 TAILQ_REMOVE(&commits, entry, entry);
422 got_object_commit_close(entry->commit);
433 fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
434 "[repository-path]\n", getprogname());
438 const struct got_error *
439 cmd_log(int argc, char *argv[])
441 const struct got_error *error;
442 struct got_repository *repo;
443 struct got_object_id *id = NULL;
444 struct got_object *obj;
445 char *repo_path = NULL;
446 char *start_commit = NULL;
448 int show_patch = 0, limit = 0;
452 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
456 while ((ch = getopt(argc, argv, "pc:l:")) != -1) {
462 start_commit = optarg;
465 limit = strtonum(optarg, 1, INT_MAX, &errstr);
467 err(1, "-l option %s", errstr);
479 repo_path = getcwd(NULL, 0);
480 if (repo_path == NULL)
481 return got_error_from_errno();
482 } else if (argc == 1) {
483 repo_path = realpath(argv[0], NULL);
484 if (repo_path == NULL)
485 return got_error_from_errno();
489 error = got_repo_open(&repo, repo_path);
494 if (start_commit == NULL) {
495 struct got_reference *head_ref;
496 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
499 error = got_ref_resolve(&id, repo, head_ref);
500 got_ref_close(head_ref);
503 error = got_object_open(&obj, repo, id);
505 error = got_object_open_by_id_str(&obj, repo, start_commit);
507 id = got_object_get_id(obj);
509 error = got_error_from_errno();
514 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
515 error = print_commits(obj, id, repo, show_patch, limit);
517 error = got_error(GOT_ERR_OBJ_TYPE);
518 got_object_close(obj);
520 got_repo_close(repo);
524 static const struct got_error *
525 diff_blobs(struct got_object *obj1, struct got_object *obj2,
526 struct got_repository *repo)
528 const struct got_error *err;
529 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
531 err = got_object_blob_open(&blob1, repo, obj1, 8192);
534 err = got_object_blob_open(&blob2, repo, obj2, 81992);
538 err = got_diff_blob(blob1, blob2, NULL, NULL, stdout);
541 got_object_blob_close(blob1);
543 got_object_blob_close(blob2);
547 static const struct got_error *
548 diff_trees(struct got_object *obj1, struct got_object *obj2,
549 struct got_repository *repo)
551 const struct got_error *err;
552 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
554 err = got_object_tree_open(&tree1, repo, obj1);
557 err = got_object_tree_open(&tree2, repo, obj2);
561 err = got_diff_tree(tree1, tree2, repo, stdout);
564 got_object_tree_close(tree1);
566 got_object_tree_close(tree2);
570 static const struct got_error *
571 diff_commits(struct got_object *obj1, struct got_object *obj2,
572 struct got_repository *repo)
574 const struct got_error *err;
575 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
576 struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
578 err = got_object_commit_open(&commit1, repo, obj1);
581 err = got_object_commit_open(&commit2, repo, obj2);
585 err = got_object_open(&tree_obj1, repo, commit1->tree_id);
588 err = got_object_open(&tree_obj2, repo, commit2->tree_id);
592 err = diff_trees(tree_obj1, tree_obj2, repo);
595 got_object_close(tree_obj1);
597 got_object_close(tree_obj2);
599 got_object_commit_close(commit1);
601 got_object_commit_close(commit2);
609 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
614 const struct got_error *
615 cmd_diff(int argc, char *argv[])
617 const struct got_error *error;
618 struct got_repository *repo = NULL;
619 struct got_object_id *id1 = NULL, *id2 = NULL;
620 struct got_object *obj1 = NULL, *obj2 = NULL;
621 char *repo_path = NULL;
622 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
626 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
630 while ((ch = getopt(argc, argv, "")) != -1) {
642 usage_diff(); /* TODO show local worktree changes */
643 } else if (argc == 2) {
644 repo_path = getcwd(NULL, 0);
645 if (repo_path == NULL)
646 return got_error_from_errno();
647 obj_id_str1 = argv[0];
648 obj_id_str2 = argv[1];
649 } else if (argc == 3) {
650 repo_path = realpath(argv[0], NULL);
651 if (repo_path == NULL)
652 return got_error_from_errno();
653 obj_id_str1 = argv[1];
654 obj_id_str2 = argv[2];
658 error = got_repo_open(&repo, repo_path);
663 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
665 id1 = got_object_get_id(obj1);
667 error = got_error_from_errno();
672 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
674 id2 = got_object_get_id(obj2);
676 error = got_error_from_errno();
681 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
682 error = got_error(GOT_ERR_OBJ_TYPE);
686 switch (got_object_get_type(obj1)) {
687 case GOT_OBJ_TYPE_BLOB:
688 error = diff_blobs(obj1, obj2, repo);
690 case GOT_OBJ_TYPE_TREE:
691 error = diff_trees(obj1, obj2, repo);
693 case GOT_OBJ_TYPE_COMMIT:
694 error = diff_commits(obj1, obj2, repo);
697 error = got_error(GOT_ERR_OBJ_TYPE);
702 got_object_close(obj1);
704 got_object_close(obj2);
710 got_repo_close(repo);
715 const struct got_error *
716 cmd_status(int argc __unused, char *argv[] __unused)
718 git_repository *repo = NULL;
719 git_status_list *status;
720 git_status_options statusopts;
725 if (git_repository_open_ext(&repo, ".", 0, NULL))
726 errx(1, "git_repository_open: %s", giterr_last()->message);
728 if (git_repository_is_bare(repo))
729 errx(1, "bar repository");
731 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
732 errx(1, "git_status_init_options: %s", giterr_last()->message);
734 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
735 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
736 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
737 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
739 if (git_status_list_new(&status, repo, &statusopts))
740 errx(1, "git_status_list_new: %s", giterr_last()->message);
742 for (i = 0; i < git_status_list_entrycount(status); i++) {
743 const git_status_entry *se;
745 se = git_status_byindex(status, i);
746 switch (se->status) {
747 case GIT_STATUS_WT_NEW:
748 printf("? %s\n", se->index_to_workdir->new_file.path);
750 case GIT_STATUS_WT_MODIFIED:
751 printf("M %s\n", se->index_to_workdir->new_file.path);
753 case GIT_STATUS_WT_DELETED:
754 printf("R %s\n", se->index_to_workdir->new_file.path);
756 case GIT_STATUS_WT_RENAMED:
757 printf("m %s -> %s\n",
758 se->index_to_workdir->old_file.path,
759 se->index_to_workdir->new_file.path);
761 case GIT_STATUS_WT_TYPECHANGE:
762 printf("t %s\n", se->index_to_workdir->new_file.path);
764 case GIT_STATUS_INDEX_NEW:
765 printf("A %s\n", se->head_to_index->new_file.path);
767 case GIT_STATUS_INDEX_MODIFIED:
768 printf("M %s\n", se->head_to_index->old_file.path);
770 case GIT_STATUS_INDEX_DELETED:
771 printf("R %s\n", se->head_to_index->old_file.path);
773 case GIT_STATUS_INDEX_RENAMED:
774 printf("m %s -> %s\n",
775 se->head_to_index->old_file.path,
776 se->head_to_index->new_file.path);
778 case GIT_STATUS_INDEX_TYPECHANGE:
779 printf("t %s\n", se->head_to_index->old_file.path);
781 case GIT_STATUS_CURRENT:
787 git_status_list_free(status);
788 git_repository_free(repo);
789 git_libgit2_shutdown();