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);
146 if (error && !(sigint_received || sigpipe_received)) {
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;
306 char *id_str1 = NULL, *id_str2;
308 err = got_object_open_as_tree(&tree2, repo, commit->tree_id);
312 qid = SIMPLEQ_FIRST(&commit->parent_ids);
314 struct got_commit_object *pcommit;
316 err = got_object_open_as_commit(&pcommit, repo, qid->id);
320 err = got_object_open_as_tree(&tree1, repo, pcommit->tree_id);
321 got_object_commit_close(pcommit);
325 err = got_object_id_str(&id_str1, qid->id);
330 err = got_object_id_str(&id_str2, id);
334 printf("diff: %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
335 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
338 got_object_tree_close(tree1);
339 got_object_tree_close(tree2);
346 get_datestr(time_t *time, char *datebuf)
348 char *p, *s = ctime_r(time, datebuf);
355 static const struct got_error *
356 print_commit(struct got_commit_object *commit, struct got_object_id *id,
357 struct got_repository *repo, int show_patch, int diff_context)
359 const struct got_error *err = NULL;
360 char *id_str, *datestr, *logmsg0, *logmsg, *line;
363 err = got_object_id_str(&id_str, id);
367 printf("-----------------------------------------------\n");
368 printf("commit %s\n", id_str);
370 printf("from: %s\n", commit->author);
371 datestr = get_datestr(&commit->committer_time, datebuf);
372 printf("date: %s UTC\n", datestr);
373 if (strcmp(commit->author, commit->committer) != 0)
374 printf("via: %s\n", commit->committer);
375 if (commit->nparents > 1) {
376 struct got_object_qid *qid;
378 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
379 err = got_object_id_str(&id_str, qid->id);
382 printf("parent %d: %s\n", n++, id_str);
387 logmsg0 = strdup(commit->logmsg);
389 return got_error_from_errno();
393 line = strsep(&logmsg, "\n");
395 printf(" %s\n", line);
400 err = print_patch(commit, id, diff_context, repo);
409 static const struct got_error *
410 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
411 struct got_repository *repo, char *path, int show_patch, int diff_context,
412 int limit, int first_parent_traversal)
414 const struct got_error *err;
415 struct got_commit_graph *graph;
417 err = got_commit_graph_open(&graph, root_id, path,
418 first_parent_traversal, repo);
421 err = got_commit_graph_iter_start(graph, root_id, repo);
425 struct got_commit_object *commit;
426 struct got_object_id *id;
428 if (sigint_received || sigpipe_received)
431 err = got_commit_graph_iter_next(&id, graph);
433 if (err->code == GOT_ERR_ITER_COMPLETED) {
437 if (err->code != GOT_ERR_ITER_NEED_MORE)
439 err = got_commit_graph_fetch_commits(graph, 1, repo);
448 err = got_object_open_as_commit(&commit, repo, id);
451 err = print_commit(commit, id, repo, show_patch, diff_context);
452 got_object_commit_close(commit);
453 if (err || (limit && --limit == 0))
457 got_commit_graph_close(graph);
464 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
465 "[-r repository-path] [path]\n", getprogname());
469 static const struct got_error *
470 cmd_log(int argc, char *argv[])
472 const struct got_error *error;
473 struct got_repository *repo = NULL;
474 struct got_object_id *id = NULL;
475 struct got_object *obj = NULL;
476 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
477 char *start_commit = NULL;
478 int diff_context = 3, ch;
479 int show_patch = 0, limit = 0, first_parent_traversal = 0;
483 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
488 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
494 start_commit = optarg;
497 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
500 err(1, "-C option %s", errstr);
503 limit = strtonum(optarg, 1, INT_MAX, &errstr);
505 err(1, "-l option %s", errstr);
508 first_parent_traversal = 1;
511 repo_path = realpath(optarg, NULL);
512 if (repo_path == NULL)
527 path = strdup(argv[0]);
531 return got_error_from_errno();
533 cwd = getcwd(NULL, 0);
535 error = got_error_from_errno();
538 if (repo_path == NULL) {
539 repo_path = strdup(cwd);
540 if (repo_path == NULL) {
541 error = got_error_from_errno();
546 error = got_repo_open(&repo, repo_path);
550 if (start_commit == NULL) {
551 struct got_reference *head_ref;
552 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
555 error = got_ref_resolve(&id, repo, head_ref);
556 got_ref_close(head_ref);
559 error = got_object_open(&obj, repo, id);
561 struct got_reference *ref;
562 error = got_ref_open(&ref, repo, start_commit);
564 error = got_ref_resolve(&id, repo, ref);
568 error = got_object_open(&obj, repo, id);
573 error = got_object_open_by_id_str(&obj, repo,
577 id = got_object_id_dup(got_object_get_id(obj));
579 error = got_error_from_errno();
584 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
585 error = got_error(GOT_ERR_OBJ_TYPE);
589 error = got_repo_map_path(&in_repo_path, repo, path, 1);
597 error = print_commits(obj, id, repo, path, show_patch,
598 diff_context, limit, first_parent_traversal);
604 got_object_close(obj);
607 const struct got_error *repo_error;
608 repo_error = got_repo_close(repo);
618 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
619 "object1 object2\n", getprogname());
623 static const struct got_error *
624 cmd_diff(int argc, char *argv[])
626 const struct got_error *error;
627 struct got_repository *repo = NULL;
628 struct got_object *obj1 = NULL, *obj2 = NULL;
629 char *repo_path = NULL;
630 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
631 int diff_context = 3, ch;
635 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
640 while ((ch = getopt(argc, argv, "C:")) != -1) {
643 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
645 err(1, "-C option %s", errstr);
657 usage_diff(); /* TODO show local worktree changes */
658 } else if (argc == 2) {
659 repo_path = getcwd(NULL, 0);
660 if (repo_path == NULL)
661 return got_error_from_errno();
662 obj_id_str1 = argv[0];
663 obj_id_str2 = argv[1];
664 } else if (argc == 3) {
665 repo_path = realpath(argv[0], NULL);
666 if (repo_path == NULL)
667 return got_error_from_errno();
668 obj_id_str1 = argv[1];
669 obj_id_str2 = argv[2];
673 error = got_repo_open(&repo, repo_path);
678 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
682 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
686 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
687 error = got_error(GOT_ERR_OBJ_TYPE);
691 switch (got_object_get_type(obj1)) {
692 case GOT_OBJ_TYPE_BLOB:
693 error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
694 diff_context, repo, stdout);
696 case GOT_OBJ_TYPE_TREE:
697 error = got_diff_objects_as_trees(obj1, obj2, "", "",
698 diff_context, repo, stdout);
700 case GOT_OBJ_TYPE_COMMIT:
701 printf("diff: %s %s\n", obj_id_str1 ? obj_id_str1 : "/dev/null",
703 error = got_diff_objects_as_commits(obj1, obj2, diff_context,
707 error = got_error(GOT_ERR_OBJ_TYPE);
712 got_object_close(obj1);
714 got_object_close(obj2);
716 const struct got_error *repo_error;
717 repo_error = got_repo_close(repo);
727 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
732 static const struct got_error *
733 cmd_blame(int argc, char *argv[])
735 const struct got_error *error;
736 struct got_repository *repo = NULL;
737 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
738 struct got_object_id *commit_id = NULL;
739 char *commit_id_str = NULL;
743 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
748 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
751 commit_id_str = optarg;
754 repo_path = realpath(optarg, NULL);
755 if (repo_path == NULL)
772 cwd = getcwd(NULL, 0);
774 error = got_error_from_errno();
777 if (repo_path == NULL) {
778 repo_path = strdup(cwd);
779 if (repo_path == NULL) {
780 error = got_error_from_errno();
785 error = got_repo_open(&repo, repo_path);
789 error = got_repo_map_path(&in_repo_path, repo, path, 1);
793 if (commit_id_str == NULL) {
794 struct got_reference *head_ref;
795 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
798 error = got_ref_resolve(&commit_id, repo, head_ref);
799 got_ref_close(head_ref);
803 struct got_object *obj;
804 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
807 commit_id = got_object_id_dup(got_object_get_id(obj));
808 if (commit_id == NULL)
809 error = got_error_from_errno();
810 got_object_close(obj);
813 error = got_blame(in_repo_path, commit_id, repo, stdout);
820 const struct got_error *repo_error;
821 repo_error = got_repo_close(repo);
831 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
837 static const struct got_error *
838 print_tree(const char *path, struct got_object_id *commit_id,
839 int show_ids, struct got_repository *repo)
841 const struct got_error *err = NULL;
842 struct got_object_id *tree_id = NULL;
843 struct got_tree_object *tree = NULL;
844 const struct got_tree_entries *entries;
845 struct got_tree_entry *te;
847 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
851 err = got_object_open_as_tree(&tree, repo, tree_id);
854 entries = got_object_tree_get_entries(tree);
855 te = SIMPLEQ_FIRST(&entries->head);
859 if (sigint_received || sigpipe_received)
864 err = got_object_id_str(&id_str, te->id);
867 if (asprintf(&id, "%s ", id_str) == -1) {
868 err = got_error_from_errno();
874 printf("%s%s%s\n", id ? id : "",
875 te->name, S_ISDIR(te->mode) ? "/" : "");
876 te = SIMPLEQ_NEXT(te, entry);
881 got_object_tree_close(tree);
886 static const struct got_error *
887 cmd_tree(int argc, char *argv[])
889 const struct got_error *error;
890 struct got_repository *repo = NULL;
891 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
892 struct got_object_id *commit_id = NULL;
893 char *commit_id_str = NULL;
898 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
903 while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
906 commit_id_str = optarg;
909 repo_path = realpath(optarg, NULL);
910 if (repo_path == NULL)
932 cwd = getcwd(NULL, 0);
934 error = got_error_from_errno();
937 if (repo_path == NULL) {
938 repo_path = strdup(cwd);
939 if (repo_path == NULL) {
940 error = got_error_from_errno();
945 error = got_repo_open(&repo, repo_path);
949 error = got_repo_map_path(&in_repo_path, repo, path, 1);
953 if (commit_id_str == NULL) {
954 struct got_reference *head_ref;
955 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
958 error = got_ref_resolve(&commit_id, repo, head_ref);
959 got_ref_close(head_ref);
963 struct got_object *obj;
964 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
967 commit_id = got_object_id_dup(got_object_get_id(obj));
968 if (commit_id == NULL)
969 error = got_error_from_errno();
970 got_object_close(obj);
973 error = print_tree(in_repo_path, commit_id, show_ids, repo);
980 const struct got_error *repo_error;
981 repo_error = got_repo_close(repo);
989 static const struct got_error *
990 cmd_status(int argc __unused, char *argv[] __unused)
992 git_repository *repo = NULL;
993 git_status_list *status;
994 git_status_options statusopts;
999 if (git_repository_open_ext(&repo, ".", 0, NULL))
1000 errx(1, "git_repository_open: %s", giterr_last()->message);
1002 if (git_repository_is_bare(repo))
1003 errx(1, "bar repository");
1005 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
1006 errx(1, "git_status_init_options: %s", giterr_last()->message);
1008 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
1009 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
1010 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
1011 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1013 if (git_status_list_new(&status, repo, &statusopts))
1014 errx(1, "git_status_list_new: %s", giterr_last()->message);
1016 for (i = 0; i < git_status_list_entrycount(status); i++) {
1017 const git_status_entry *se;
1019 se = git_status_byindex(status, i);
1020 switch (se->status) {
1021 case GIT_STATUS_WT_NEW:
1022 printf("? %s\n", se->index_to_workdir->new_file.path);
1024 case GIT_STATUS_WT_MODIFIED:
1025 printf("M %s\n", se->index_to_workdir->new_file.path);
1027 case GIT_STATUS_WT_DELETED:
1028 printf("R %s\n", se->index_to_workdir->new_file.path);
1030 case GIT_STATUS_WT_RENAMED:
1031 printf("m %s -> %s\n",
1032 se->index_to_workdir->old_file.path,
1033 se->index_to_workdir->new_file.path);
1035 case GIT_STATUS_WT_TYPECHANGE:
1036 printf("t %s\n", se->index_to_workdir->new_file.path);
1038 case GIT_STATUS_INDEX_NEW:
1039 printf("A %s\n", se->head_to_index->new_file.path);
1041 case GIT_STATUS_INDEX_MODIFIED:
1042 printf("M %s\n", se->head_to_index->old_file.path);
1044 case GIT_STATUS_INDEX_DELETED:
1045 printf("R %s\n", se->head_to_index->old_file.path);
1047 case GIT_STATUS_INDEX_RENAMED:
1048 printf("m %s -> %s\n",
1049 se->head_to_index->old_file.path,
1050 se->head_to_index->new_file.path);
1052 case GIT_STATUS_INDEX_TYPECHANGE:
1053 printf("t %s\n", se->head_to_index->old_file.path);
1055 case GIT_STATUS_CURRENT:
1061 git_status_list_free(status);
1062 git_repository_free(repo);
1063 git_libgit2_shutdown();