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>
20 #include <sys/types.h>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_reference.h"
37 #include "got_repository.h"
38 #include "got_worktree.h"
40 #include "got_commit_graph.h"
41 #include "got_blame.h"
44 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
47 static volatile sig_atomic_t sigint_received;
48 static volatile sig_atomic_t sigpipe_received;
51 catch_sigint(int signo)
57 catch_sigpipe(int signo)
65 const struct got_error *(*cmd_main)(int, char *[]);
66 void (*cmd_usage)(void);
67 const char *cmd_descr;
70 __dead static void usage(void);
71 __dead static void usage_checkout(void);
72 __dead static void usage_log(void);
73 __dead static void usage_diff(void);
74 __dead static void usage_blame(void);
75 __dead static void usage_tree(void);
77 static const struct got_error* cmd_checkout(int, char *[]);
78 static const struct got_error* cmd_log(int, char *[]);
79 static const struct got_error* cmd_diff(int, char *[]);
80 static const struct got_error* cmd_blame(int, char *[]);
81 static const struct got_error* cmd_tree(int, char *[]);
83 static const struct got_error* cmd_status(int, char *[]);
86 static struct cmd got_commands[] = {
87 { "checkout", cmd_checkout, usage_checkout,
88 "check out a new work tree from a repository" },
89 { "log", cmd_log, usage_log,
90 "show repository history" },
91 { "diff", cmd_diff, usage_diff,
92 "compare files and directories" },
93 { "blame", cmd_blame, usage_blame,
94 " show when lines in a file were changed" },
95 { "tree", cmd_tree, usage_tree,
96 " list files and directories in repository" },
98 { "status", cmd_status, usage_status,
99 "show modification status of files" },
104 main(int argc, char *argv[])
111 setlocale(LC_ALL, "");
113 while ((ch = getopt(argc, argv, "h")) != -1) {
131 signal(SIGINT, catch_sigint);
132 signal(SIGPIPE, catch_sigpipe);
134 for (i = 0; i < nitems(got_commands); i++) {
135 const struct got_error *error;
137 cmd = &got_commands[i];
139 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
143 got_commands[i].cmd_usage();
145 error = got_commands[i].cmd_main(argc, argv);
147 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
154 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
163 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
164 "Available commands:\n", getprogname());
165 for (i = 0; i < nitems(got_commands); i++) {
166 struct cmd *cmd = &got_commands[i];
167 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
175 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
176 "[worktree-path]\n", getprogname());
181 checkout_progress(void *arg, const char *path)
183 char *worktree_path = arg;
185 while (path[0] == '/')
188 printf("A %s/%s\n", worktree_path, path);
191 static const struct got_error *
192 checkout_cancel(void *arg)
194 if (sigint_received || sigpipe_received)
195 return got_error(GOT_ERR_CANCELLED);
199 static const struct got_error *
200 cmd_checkout(int argc, char *argv[])
202 const struct got_error *error = NULL;
203 struct got_repository *repo = NULL;
204 struct got_reference *head_ref = NULL;
205 struct got_worktree *worktree = NULL;
206 char *repo_path = NULL;
207 char *worktree_path = NULL;
208 const char *path_prefix = "";
211 while ((ch = getopt(argc, argv, "p:")) != -1) {
214 path_prefix = optarg;
226 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
231 char *cwd, *base, *dotgit;
232 repo_path = realpath(argv[0], NULL);
233 if (repo_path == NULL)
234 return got_error_from_errno();
235 cwd = getcwd(NULL, 0);
237 error = got_error_from_errno();
241 base = basename(path_prefix);
243 base = basename(repo_path);
245 error = got_error_from_errno();
248 dotgit = strstr(base, ".git");
251 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
252 error = got_error_from_errno();
257 } else if (argc == 2) {
258 repo_path = realpath(argv[0], NULL);
259 if (repo_path == NULL) {
260 error = got_error_from_errno();
263 worktree_path = realpath(argv[1], NULL);
264 if (worktree_path == NULL) {
265 error = got_error_from_errno();
271 error = got_repo_open(&repo, repo_path);
274 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
278 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
282 error = got_worktree_open(&worktree, worktree_path);
286 error = got_worktree_checkout_files(worktree, head_ref, repo,
287 checkout_progress, worktree_path, checkout_cancel, NULL);
291 printf("Now shut up and hack\n");
299 static const struct got_error *
300 print_patch(struct got_commit_object *commit, struct got_object_id *id,
301 int diff_context, struct got_repository *repo)
303 const struct got_error *err = NULL;
304 struct got_tree_object *tree1 = NULL, *tree2;
305 struct got_object_qid *qid;
307 err = got_object_open_as_tree(&tree2, repo, commit->tree_id);
311 qid = SIMPLEQ_FIRST(&commit->parent_ids);
313 struct got_commit_object *pcommit;
315 err = got_object_open_as_commit(&pcommit, repo, qid->id);
319 err = got_object_open_as_tree(&tree1, repo, pcommit->tree_id);
320 got_object_commit_close(pcommit);
325 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
327 got_object_tree_close(tree1);
328 got_object_tree_close(tree2);
333 get_datestr(time_t *time, char *datebuf)
335 char *p, *s = ctime_r(time, datebuf);
342 static const struct got_error *
343 print_commit(struct got_commit_object *commit, struct got_object_id *id,
344 struct got_repository *repo, int show_patch, int diff_context)
346 const struct got_error *err = NULL;
347 char *id_str, *datestr, *logmsg0, *logmsg, *line;
350 err = got_object_id_str(&id_str, id);
354 printf("-----------------------------------------------\n");
355 printf("commit %s\n", id_str);
357 printf("from: %s\n", commit->author);
358 datestr = get_datestr(&commit->committer_time, datebuf);
359 printf("date: %s UTC\n", datestr);
360 if (strcmp(commit->author, commit->committer) != 0)
361 printf("via: %s\n", commit->committer);
362 if (commit->nparents > 1) {
363 struct got_object_qid *qid;
365 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
366 err = got_object_id_str(&id_str, qid->id);
369 printf("parent %d: %s\n", n++, id_str);
374 logmsg0 = strdup(commit->logmsg);
376 return got_error_from_errno();
380 line = strsep(&logmsg, "\n");
382 printf(" %s\n", line);
387 err = print_patch(commit, id, diff_context, repo);
396 static const struct got_error *
397 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
398 struct got_repository *repo, char *path, int show_patch, int diff_context,
399 int limit, int first_parent_traversal)
401 const struct got_error *err;
402 struct got_commit_graph *graph;
404 err = got_commit_graph_open(&graph, root_id, path,
405 first_parent_traversal, repo);
408 err = got_commit_graph_iter_start(graph, root_id, repo);
412 struct got_commit_object *commit;
413 struct got_object_id *id;
415 err = got_commit_graph_iter_next(&id, graph);
417 if (err->code == GOT_ERR_ITER_COMPLETED) {
421 if (err->code != GOT_ERR_ITER_NEED_MORE)
423 err = got_commit_graph_fetch_commits(graph, 1, repo);
432 err = got_object_open_as_commit(&commit, repo, id);
435 err = print_commit(commit, id, repo, show_patch, diff_context);
436 got_object_commit_close(commit);
437 if (err || (limit && --limit == 0))
441 got_commit_graph_close(graph);
448 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
449 "[-r repository-path] [path]\n", getprogname());
453 static const struct got_error *
454 cmd_log(int argc, char *argv[])
456 const struct got_error *error;
457 struct got_repository *repo = NULL;
458 struct got_object_id *id = NULL;
459 struct got_object *obj = NULL;
460 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
461 char *start_commit = NULL;
462 int diff_context = 3, ch;
463 int show_patch = 0, limit = 0, first_parent_traversal = 0;
467 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
472 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
478 start_commit = optarg;
481 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
484 err(1, "-C option %s", errstr);
487 limit = strtonum(optarg, 1, INT_MAX, &errstr);
489 err(1, "-l option %s", errstr);
492 first_parent_traversal = 1;
495 repo_path = realpath(optarg, NULL);
496 if (repo_path == NULL)
511 path = strdup(argv[0]);
515 return got_error_from_errno();
517 cwd = getcwd(NULL, 0);
519 error = got_error_from_errno();
522 if (repo_path == NULL) {
523 repo_path = strdup(cwd);
524 if (repo_path == NULL) {
525 error = got_error_from_errno();
530 error = got_repo_open(&repo, repo_path);
534 if (start_commit == NULL) {
535 struct got_reference *head_ref;
536 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
539 error = got_ref_resolve(&id, repo, head_ref);
540 got_ref_close(head_ref);
543 error = got_object_open(&obj, repo, id);
545 struct got_reference *ref;
546 error = got_ref_open(&ref, repo, start_commit);
548 error = got_ref_resolve(&id, repo, ref);
552 error = got_object_open(&obj, repo, id);
557 error = got_object_open_by_id_str(&obj, repo,
561 id = got_object_id_dup(got_object_get_id(obj));
563 error = got_error_from_errno();
568 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
569 error = got_error(GOT_ERR_OBJ_TYPE);
573 error = got_repo_map_path(&in_repo_path, repo, path, 1);
581 error = print_commits(obj, id, repo, path, show_patch,
582 diff_context, limit, first_parent_traversal);
588 got_object_close(obj);
591 const struct got_error *repo_error;
592 repo_error = got_repo_close(repo);
602 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
603 "object1 object2\n", getprogname());
607 static const struct got_error *
608 cmd_diff(int argc, char *argv[])
610 const struct got_error *error;
611 struct got_repository *repo = NULL;
612 struct got_object *obj1 = NULL, *obj2 = NULL;
613 char *repo_path = NULL;
614 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
615 int diff_context = 3, ch;
619 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
624 while ((ch = getopt(argc, argv, "C:")) != -1) {
627 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
629 err(1, "-C option %s", errstr);
641 usage_diff(); /* TODO show local worktree changes */
642 } else if (argc == 2) {
643 repo_path = getcwd(NULL, 0);
644 if (repo_path == NULL)
645 return got_error_from_errno();
646 obj_id_str1 = argv[0];
647 obj_id_str2 = argv[1];
648 } else if (argc == 3) {
649 repo_path = realpath(argv[0], NULL);
650 if (repo_path == NULL)
651 return got_error_from_errno();
652 obj_id_str1 = argv[1];
653 obj_id_str2 = argv[2];
657 error = got_repo_open(&repo, repo_path);
662 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
666 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
670 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
671 error = got_error(GOT_ERR_OBJ_TYPE);
675 switch (got_object_get_type(obj1)) {
676 case GOT_OBJ_TYPE_BLOB:
677 error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
678 diff_context, repo, stdout);
680 case GOT_OBJ_TYPE_TREE:
681 error = got_diff_objects_as_trees(obj1, obj2, "", "",
682 diff_context, repo, stdout);
684 case GOT_OBJ_TYPE_COMMIT:
685 error = got_diff_objects_as_commits(obj1, obj2, diff_context,
689 error = got_error(GOT_ERR_OBJ_TYPE);
694 got_object_close(obj1);
696 got_object_close(obj2);
698 const struct got_error *repo_error;
699 repo_error = got_repo_close(repo);
709 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
714 static const struct got_error *
715 cmd_blame(int argc, char *argv[])
717 const struct got_error *error;
718 struct got_repository *repo = NULL;
719 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
720 struct got_object_id *commit_id = NULL;
721 char *commit_id_str = NULL;
725 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
730 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
733 commit_id_str = optarg;
736 repo_path = realpath(optarg, NULL);
737 if (repo_path == NULL)
754 cwd = getcwd(NULL, 0);
756 error = got_error_from_errno();
759 if (repo_path == NULL) {
760 repo_path = strdup(cwd);
761 if (repo_path == NULL) {
762 error = got_error_from_errno();
767 error = got_repo_open(&repo, repo_path);
771 error = got_repo_map_path(&in_repo_path, repo, path, 1);
775 if (commit_id_str == NULL) {
776 struct got_reference *head_ref;
777 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
780 error = got_ref_resolve(&commit_id, repo, head_ref);
781 got_ref_close(head_ref);
785 struct got_object *obj;
786 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
789 commit_id = got_object_id_dup(got_object_get_id(obj));
790 if (commit_id == NULL)
791 error = got_error_from_errno();
792 got_object_close(obj);
795 error = got_blame(in_repo_path, commit_id, repo, stdout);
802 const struct got_error *repo_error;
803 repo_error = got_repo_close(repo);
813 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
819 static const struct got_error *
820 print_tree(const char *path, struct got_object_id *commit_id,
821 int show_ids, struct got_repository *repo)
823 const struct got_error *err = NULL;
824 struct got_object_id *tree_id = NULL;
825 struct got_tree_object *tree = NULL;
826 const struct got_tree_entries *entries;
827 struct got_tree_entry *te;
829 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
833 err = got_object_open_as_tree(&tree, repo, tree_id);
836 entries = got_object_tree_get_entries(tree);
837 te = SIMPLEQ_FIRST(&entries->head);
842 err = got_object_id_str(&id_str, te->id);
845 if (asprintf(&id, "%s ", id_str) == -1) {
846 err = got_error_from_errno();
852 printf("%s%s%s\n", id ? id : "",
853 te->name, S_ISDIR(te->mode) ? "/" : "");
854 te = SIMPLEQ_NEXT(te, entry);
859 got_object_tree_close(tree);
864 static const struct got_error *
865 cmd_tree(int argc, char *argv[])
867 const struct got_error *error;
868 struct got_repository *repo = NULL;
869 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
870 struct got_object_id *commit_id = NULL;
871 char *commit_id_str = NULL;
876 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
881 while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
884 commit_id_str = optarg;
887 repo_path = realpath(optarg, NULL);
888 if (repo_path == NULL)
910 cwd = getcwd(NULL, 0);
912 error = got_error_from_errno();
915 if (repo_path == NULL) {
916 repo_path = strdup(cwd);
917 if (repo_path == NULL) {
918 error = got_error_from_errno();
923 error = got_repo_open(&repo, repo_path);
927 error = got_repo_map_path(&in_repo_path, repo, path, 1);
931 if (commit_id_str == NULL) {
932 struct got_reference *head_ref;
933 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
936 error = got_ref_resolve(&commit_id, repo, head_ref);
937 got_ref_close(head_ref);
941 struct got_object *obj;
942 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
945 commit_id = got_object_id_dup(got_object_get_id(obj));
946 if (commit_id == NULL)
947 error = got_error_from_errno();
948 got_object_close(obj);
951 error = print_tree(in_repo_path, commit_id, show_ids, repo);
958 const struct got_error *repo_error;
959 repo_error = got_repo_close(repo);
967 static const struct got_error *
968 cmd_status(int argc __unused, char *argv[] __unused)
970 git_repository *repo = NULL;
971 git_status_list *status;
972 git_status_options statusopts;
977 if (git_repository_open_ext(&repo, ".", 0, NULL))
978 errx(1, "git_repository_open: %s", giterr_last()->message);
980 if (git_repository_is_bare(repo))
981 errx(1, "bar repository");
983 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
984 errx(1, "git_status_init_options: %s", giterr_last()->message);
986 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
987 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
988 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
989 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
991 if (git_status_list_new(&status, repo, &statusopts))
992 errx(1, "git_status_list_new: %s", giterr_last()->message);
994 for (i = 0; i < git_status_list_entrycount(status); i++) {
995 const git_status_entry *se;
997 se = git_status_byindex(status, i);
998 switch (se->status) {
999 case GIT_STATUS_WT_NEW:
1000 printf("? %s\n", se->index_to_workdir->new_file.path);
1002 case GIT_STATUS_WT_MODIFIED:
1003 printf("M %s\n", se->index_to_workdir->new_file.path);
1005 case GIT_STATUS_WT_DELETED:
1006 printf("R %s\n", se->index_to_workdir->new_file.path);
1008 case GIT_STATUS_WT_RENAMED:
1009 printf("m %s -> %s\n",
1010 se->index_to_workdir->old_file.path,
1011 se->index_to_workdir->new_file.path);
1013 case GIT_STATUS_WT_TYPECHANGE:
1014 printf("t %s\n", se->index_to_workdir->new_file.path);
1016 case GIT_STATUS_INDEX_NEW:
1017 printf("A %s\n", se->head_to_index->new_file.path);
1019 case GIT_STATUS_INDEX_MODIFIED:
1020 printf("M %s\n", se->head_to_index->old_file.path);
1022 case GIT_STATUS_INDEX_DELETED:
1023 printf("R %s\n", se->head_to_index->old_file.path);
1025 case GIT_STATUS_INDEX_RENAMED:
1026 printf("m %s -> %s\n",
1027 se->head_to_index->old_file.path,
1028 se->head_to_index->new_file.path);
1030 case GIT_STATUS_INDEX_TYPECHANGE:
1031 printf("t %s\n", se->head_to_index->old_file.path);
1033 case GIT_STATUS_CURRENT:
1039 git_status_list_free(status);
1040 git_repository_free(repo);
1041 git_libgit2_shutdown();