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"
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", NULL) == -1)
187 char *cwd, *base, *dotgit;
189 cwd = getcwd(NULL, 0);
193 base = basename(path_prefix);
195 base = basename(repo_path);
198 dotgit = strstr(base, ".git");
201 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
202 error = got_error_from_errno();
207 } else if (argc == 2) {
209 worktree_path = strdup(argv[1]);
210 if (worktree_path == NULL)
211 return got_error_from_errno();
215 error = got_repo_open(&repo, repo_path);
218 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
222 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
226 error = got_worktree_open(&worktree, worktree_path);
230 error = got_worktree_checkout_files(worktree, head_ref, repo,
231 checkout_progress, worktree_path);
235 printf("checked out %s\n", worktree_path);
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);
255 err = got_object_tree_open(&tree2, repo, obj);
256 got_object_close(obj);
260 pid = SIMPLEQ_FIRST(&commit->parent_ids);
262 struct got_commit_object *pcommit;
264 err = got_object_open(&obj, repo, pid->id);
268 err = got_object_commit_open(&pcommit, repo, obj);
269 got_object_close(obj);
273 err = got_object_open(&obj, repo, pcommit->tree_id);
274 got_object_commit_close(pcommit);
277 err = got_object_tree_open(&tree1, repo, obj);
278 got_object_close(obj);
283 err = got_diff_tree(tree1, tree2, repo, stdout);
285 got_object_tree_close(tree1);
286 got_object_tree_close(tree2);
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;
297 err = got_object_id_str(&buf, id);
301 printf("-----------------------------------------------\n");
302 printf("commit: %s\n", buf);
303 printf("Author: %s\n", commit->author);
304 printf("\n%s\n", commit->logmsg);
307 err = print_patch(commit, id, repo);
316 struct commit_queue_entry {
317 TAILQ_ENTRY(commit_queue_entry) entry;
318 struct got_object_id *id;
319 struct got_commit_object *commit;
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);
337 entry = calloc(1, sizeof(*entry));
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();
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);
355 while (!TAILQ_EMPTY(&commits)) {
356 entry = TAILQ_FIRST(&commits);
357 TAILQ_REMOVE(&commits, entry, entry);
358 got_object_commit_close(entry->commit);
365 if (limit && --limit == 0)
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);
376 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
377 err = got_error(GOT_ERR_OBJ_TYPE);
381 err = got_object_commit_open(&pcommit, repo, obj);
382 got_object_close(obj);
386 pentry = calloc(1, sizeof(*pentry));
387 if (pentry == NULL) {
388 err = got_error_from_errno();
389 got_object_commit_close(pcommit);
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);
398 pentry->commit = pcommit;
399 TAILQ_INSERT_TAIL(&commits, pentry, entry);
402 TAILQ_REMOVE(&commits, entry, entry);
403 got_object_commit_close(entry->commit);
414 fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
415 "[repository-path]\n", getprogname());
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;
429 int show_patch = 0, limit = 0;
433 if (pledge("stdio rpath wpath cpath", NULL) == -1)
437 while ((ch = getopt(argc, argv, "pc:l:")) != -1) {
443 start_commit = optarg;
446 limit = strtonum(optarg, 1, INT_MAX, &errstr);
448 err(1, "-l option %s", errstr);
460 repo_path = getcwd(NULL, 0);
461 if (repo_path == NULL)
463 } else if (argc == 1) {
468 error = got_repo_open(&repo, repo_path);
472 if (start_commit == NULL) {
473 struct got_reference *head_ref;
474 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
477 error = got_ref_resolve(&id, repo, head_ref);
478 got_ref_close(head_ref);
481 error = got_object_open(&obj, repo, id);
483 error = got_object_open_by_id_str(&obj, repo, start_commit);
485 id = got_object_get_id(obj);
487 error = got_error_from_errno();
492 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
493 error = print_commits(obj, id, repo, show_patch, limit);
495 error = got_error(GOT_ERR_OBJ_TYPE);
496 got_object_close(obj);
498 got_repo_close(repo);
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);
512 err = got_object_blob_open(&blob2, repo, obj2, 81992);
516 err = got_diff_blob(blob1, blob2, NULL, NULL, stdout);
519 got_object_blob_close(blob1);
521 got_object_blob_close(blob2);
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);
535 err = got_object_tree_open(&tree2, repo, obj2);
539 err = got_diff_tree(tree1, tree2, repo, stdout);
542 got_object_tree_close(tree1);
544 got_object_tree_close(tree2);
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);
559 err = got_object_commit_open(&commit2, repo, obj2);
563 err = got_object_open(&tree_obj1, repo, commit1->tree_id);
566 err = got_object_open(&tree_obj2, repo, commit2->tree_id);
570 err = diff_trees(tree_obj1, tree_obj2, repo);
573 got_object_close(tree_obj1);
575 got_object_close(tree_obj2);
577 got_object_commit_close(commit1);
579 got_object_commit_close(commit2);
587 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
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;
604 if (pledge("stdio rpath wpath cpath", NULL) == -1)
608 while ((ch = getopt(argc, argv, "")) != -1) {
620 usage_diff(); /* TODO show local worktree changes */
621 } else if (argc == 2) {
622 repo_path = getcwd(NULL, 0);
623 if (repo_path == NULL)
625 obj_id_str1 = argv[0];
626 obj_id_str2 = argv[1];
627 } else if (argc == 3) {
629 obj_id_str1 = argv[1];
630 obj_id_str2 = argv[2];
634 error = got_repo_open(&repo, repo_path);
638 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
640 id1 = got_object_get_id(obj1);
642 error = got_error_from_errno();
647 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
649 id2 = got_object_get_id(obj2);
651 error = got_error_from_errno();
656 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
657 error = got_error(GOT_ERR_OBJ_TYPE);
661 switch (got_object_get_type(obj1)) {
662 case GOT_OBJ_TYPE_BLOB:
663 error = diff_blobs(obj1, obj2, repo);
665 case GOT_OBJ_TYPE_TREE:
666 error = diff_trees(obj1, obj2, repo);
668 case GOT_OBJ_TYPE_COMMIT:
669 error = diff_commits(obj1, obj2, repo);
672 error = got_error(GOT_ERR_OBJ_TYPE);
677 got_object_close(obj1);
679 got_object_close(obj2);
685 got_repo_close(repo);
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;
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);
725 case GIT_STATUS_WT_MODIFIED:
726 printf("M %s\n", se->index_to_workdir->new_file.path);
728 case GIT_STATUS_WT_DELETED:
729 printf("R %s\n", se->index_to_workdir->new_file.path);
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);
736 case GIT_STATUS_WT_TYPECHANGE:
737 printf("t %s\n", se->index_to_workdir->new_file.path);
739 case GIT_STATUS_INDEX_NEW:
740 printf("A %s\n", se->head_to_index->new_file.path);
742 case GIT_STATUS_INDEX_MODIFIED:
743 printf("M %s\n", se->head_to_index->old_file.path);
745 case GIT_STATUS_INDEX_DELETED:
746 printf("R %s\n", se->head_to_index->old_file.path);
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);
753 case GIT_STATUS_INDEX_TYPECHANGE:
754 printf("t %s\n", se->head_to_index->old_file.path);
756 case GIT_STATUS_CURRENT:
762 git_status_list_free(status);
763 git_repository_free(repo);
764 git_libgit2_shutdown();