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, unsigned char status, const char *path)
183 char *worktree_path = arg;
185 while (path[0] == '/')
188 printf("%c %s/%s\n", status, 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);
279 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
282 error = got_worktree_open(&worktree, worktree_path);
286 error = got_worktree_checkout_files(worktree, 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,
309 got_object_commit_get_tree_id(commit));
313 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
315 struct got_commit_object *pcommit;
317 err = got_object_open_as_commit(&pcommit, repo, qid->id);
321 err = got_object_open_as_tree(&tree1, repo,
322 got_object_commit_get_tree_id(pcommit));
323 got_object_commit_close(pcommit);
327 err = got_object_id_str(&id_str1, qid->id);
332 err = got_object_id_str(&id_str2, id);
336 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
337 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
340 got_object_tree_close(tree1);
341 got_object_tree_close(tree2);
348 get_datestr(time_t *time, char *datebuf)
350 char *p, *s = ctime_r(time, datebuf);
357 static const struct got_error *
358 print_commit(struct got_commit_object *commit, struct got_object_id *id,
359 struct got_repository *repo, int show_patch, int diff_context)
361 const struct got_error *err = NULL;
362 char *id_str, *datestr, *logmsg0, *logmsg, *line;
364 time_t committer_time;
365 const char *author, *committer;
367 err = got_object_id_str(&id_str, id);
371 printf("-----------------------------------------------\n");
372 printf("commit %s\n", id_str);
374 printf("from: %s\n", got_object_commit_get_author(commit));
375 committer_time = got_object_commit_get_committer_time(commit);
376 datestr = get_datestr(&committer_time, datebuf);
377 printf("date: %s UTC\n", datestr);
378 author = got_object_commit_get_author(commit);
379 committer = got_object_commit_get_committer(commit);
380 if (strcmp(author, committer) != 0)
381 printf("via: %s\n", committer);
382 if (got_object_commit_get_nparents(commit) > 1) {
383 const struct got_object_id_queue *parent_ids;
384 struct got_object_qid *qid;
386 parent_ids = got_object_commit_get_parent_ids(commit);
387 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
388 err = got_object_id_str(&id_str, qid->id);
391 printf("parent %d: %s\n", n++, id_str);
396 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
398 return got_error_from_errno();
402 line = strsep(&logmsg, "\n");
404 printf(" %s\n", line);
409 err = print_patch(commit, id, diff_context, repo);
418 static const struct got_error *
419 print_commits(struct got_object_id *root_id, struct got_repository *repo,
420 char *path, int show_patch, int diff_context, int limit,
421 int first_parent_traversal)
423 const struct got_error *err;
424 struct got_commit_graph *graph;
426 err = got_commit_graph_open(&graph, root_id, path,
427 first_parent_traversal, repo);
430 err = got_commit_graph_iter_start(graph, root_id, repo);
434 struct got_commit_object *commit;
435 struct got_object_id *id;
437 if (sigint_received || sigpipe_received)
440 err = got_commit_graph_iter_next(&id, graph);
442 if (err->code == GOT_ERR_ITER_COMPLETED) {
446 if (err->code != GOT_ERR_ITER_NEED_MORE)
448 err = got_commit_graph_fetch_commits(graph, 1, repo);
457 err = got_object_open_as_commit(&commit, repo, id);
460 err = print_commit(commit, id, repo, show_patch, diff_context);
461 got_object_commit_close(commit);
462 if (err || (limit && --limit == 0))
466 got_commit_graph_close(graph);
473 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
474 "[-r repository-path] [path]\n", getprogname());
478 static const struct got_error *
479 cmd_log(int argc, char *argv[])
481 const struct got_error *error;
482 struct got_repository *repo = NULL;
483 struct got_commit_object *commit = NULL;
484 struct got_object_id *id = NULL;
485 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
486 char *start_commit = NULL;
487 int diff_context = 3, ch;
488 int show_patch = 0, limit = 0, first_parent_traversal = 0;
492 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
497 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
503 start_commit = optarg;
506 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
509 err(1, "-C option %s", errstr);
512 limit = strtonum(optarg, 1, INT_MAX, &errstr);
514 err(1, "-l option %s", errstr);
517 first_parent_traversal = 1;
520 repo_path = realpath(optarg, NULL);
521 if (repo_path == NULL)
536 path = strdup(argv[0]);
540 return got_error_from_errno();
542 cwd = getcwd(NULL, 0);
544 error = got_error_from_errno();
547 if (repo_path == NULL) {
548 repo_path = strdup(cwd);
549 if (repo_path == NULL) {
550 error = got_error_from_errno();
555 error = got_repo_open(&repo, repo_path);
559 if (start_commit == NULL) {
560 struct got_reference *head_ref;
561 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
564 error = got_ref_resolve(&id, repo, head_ref);
565 got_ref_close(head_ref);
568 error = got_object_open_as_commit(&commit, repo, id);
570 struct got_reference *ref;
571 error = got_ref_open(&ref, repo, start_commit);
573 error = got_ref_resolve(&id, repo, ref);
577 error = got_object_open_as_commit(&commit, repo, id);
581 if (commit == NULL) {
582 error = got_object_resolve_id_str(&id, repo,
591 error = got_repo_map_path(&in_repo_path, repo, path, 1);
599 error = print_commits(id, repo, path, show_patch,
600 diff_context, limit, first_parent_traversal);
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 char *repo_path = NULL;
629 struct got_object_id *id1 = NULL, *id2 = NULL;
630 char *id_str1 = NULL, *id_str2 = NULL;
632 int diff_context = 3, ch;
636 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
641 while ((ch = getopt(argc, argv, "C:")) != -1) {
644 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
646 err(1, "-C option %s", errstr);
658 usage_diff(); /* TODO show local worktree changes */
659 } else if (argc == 2) {
660 repo_path = getcwd(NULL, 0);
661 if (repo_path == NULL)
662 return got_error_from_errno();
665 } else if (argc == 3) {
666 repo_path = realpath(argv[0], NULL);
667 if (repo_path == NULL)
668 return got_error_from_errno();
674 error = got_repo_open(&repo, repo_path);
679 error = got_object_resolve_id_str(&id1, repo, id_str1);
683 error = got_object_resolve_id_str(&id2, repo, id_str2);
687 error = got_object_get_type(&type1, repo, id1);
691 error = got_object_get_type(&type2, repo, id2);
695 if (type1 != type2) {
696 error = got_error(GOT_ERR_OBJ_TYPE);
701 case GOT_OBJ_TYPE_BLOB:
702 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
703 diff_context, repo, stdout);
705 case GOT_OBJ_TYPE_TREE:
706 error = got_diff_objects_as_trees(id1, id2, "", "",
707 diff_context, repo, stdout);
709 case GOT_OBJ_TYPE_COMMIT:
710 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
712 error = got_diff_objects_as_commits(id1, id2, diff_context,
716 error = got_error(GOT_ERR_OBJ_TYPE);
723 const struct got_error *repo_error;
724 repo_error = got_repo_close(repo);
734 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
739 static const struct got_error *
740 cmd_blame(int argc, char *argv[])
742 const struct got_error *error;
743 struct got_repository *repo = NULL;
744 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
745 struct got_object_id *commit_id = NULL;
746 char *commit_id_str = NULL;
750 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
755 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
758 commit_id_str = optarg;
761 repo_path = realpath(optarg, NULL);
762 if (repo_path == NULL)
779 cwd = getcwd(NULL, 0);
781 error = got_error_from_errno();
784 if (repo_path == NULL) {
785 repo_path = strdup(cwd);
786 if (repo_path == NULL) {
787 error = got_error_from_errno();
792 error = got_repo_open(&repo, repo_path);
796 error = got_repo_map_path(&in_repo_path, repo, path, 1);
800 if (commit_id_str == NULL) {
801 struct got_reference *head_ref;
802 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
805 error = got_ref_resolve(&commit_id, repo, head_ref);
806 got_ref_close(head_ref);
810 error = got_object_resolve_id_str(&commit_id, repo,
816 error = got_blame(in_repo_path, commit_id, repo, stdout);
823 const struct got_error *repo_error;
824 repo_error = got_repo_close(repo);
834 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
840 static const struct got_error *
841 print_tree(const char *path, struct got_object_id *commit_id,
842 int show_ids, struct got_repository *repo)
844 const struct got_error *err = NULL;
845 struct got_object_id *tree_id = NULL;
846 struct got_tree_object *tree = NULL;
847 const struct got_tree_entries *entries;
848 struct got_tree_entry *te;
850 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
854 err = got_object_open_as_tree(&tree, repo, tree_id);
857 entries = got_object_tree_get_entries(tree);
858 te = SIMPLEQ_FIRST(&entries->head);
862 if (sigint_received || sigpipe_received)
867 err = got_object_id_str(&id_str, te->id);
870 if (asprintf(&id, "%s ", id_str) == -1) {
871 err = got_error_from_errno();
877 printf("%s%s%s\n", id ? id : "",
878 te->name, S_ISDIR(te->mode) ? "/" : "");
879 te = SIMPLEQ_NEXT(te, entry);
884 got_object_tree_close(tree);
889 static const struct got_error *
890 cmd_tree(int argc, char *argv[])
892 const struct got_error *error;
893 struct got_repository *repo = NULL;
894 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
895 struct got_object_id *commit_id = NULL;
896 char *commit_id_str = NULL;
901 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
906 while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
909 commit_id_str = optarg;
912 repo_path = realpath(optarg, NULL);
913 if (repo_path == NULL)
935 cwd = getcwd(NULL, 0);
937 error = got_error_from_errno();
940 if (repo_path == NULL) {
941 repo_path = strdup(cwd);
942 if (repo_path == NULL) {
943 error = got_error_from_errno();
948 error = got_repo_open(&repo, repo_path);
952 error = got_repo_map_path(&in_repo_path, repo, path, 1);
956 if (commit_id_str == NULL) {
957 struct got_reference *head_ref;
958 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
961 error = got_ref_resolve(&commit_id, repo, head_ref);
962 got_ref_close(head_ref);
966 error = got_object_resolve_id_str(&commit_id, repo,
972 error = print_tree(in_repo_path, commit_id, show_ids, repo);
979 const struct got_error *repo_error;
980 repo_error = got_repo_close(repo);
988 static const struct got_error *
989 cmd_status(int argc __unused, char *argv[] __unused)
991 git_repository *repo = NULL;
992 git_status_list *status;
993 git_status_options statusopts;
998 if (git_repository_open_ext(&repo, ".", 0, NULL))
999 errx(1, "git_repository_open: %s", giterr_last()->message);
1001 if (git_repository_is_bare(repo))
1002 errx(1, "bar repository");
1004 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
1005 errx(1, "git_status_init_options: %s", giterr_last()->message);
1007 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
1008 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
1009 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
1010 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1012 if (git_status_list_new(&status, repo, &statusopts))
1013 errx(1, "git_status_list_new: %s", giterr_last()->message);
1015 for (i = 0; i < git_status_list_entrycount(status); i++) {
1016 const git_status_entry *se;
1018 se = git_status_byindex(status, i);
1019 switch (se->status) {
1020 case GIT_STATUS_WT_NEW:
1021 printf("? %s\n", se->index_to_workdir->new_file.path);
1023 case GIT_STATUS_WT_MODIFIED:
1024 printf("M %s\n", se->index_to_workdir->new_file.path);
1026 case GIT_STATUS_WT_DELETED:
1027 printf("R %s\n", se->index_to_workdir->new_file.path);
1029 case GIT_STATUS_WT_RENAMED:
1030 printf("m %s -> %s\n",
1031 se->index_to_workdir->old_file.path,
1032 se->index_to_workdir->new_file.path);
1034 case GIT_STATUS_WT_TYPECHANGE:
1035 printf("t %s\n", se->index_to_workdir->new_file.path);
1037 case GIT_STATUS_INDEX_NEW:
1038 printf("A %s\n", se->head_to_index->new_file.path);
1040 case GIT_STATUS_INDEX_MODIFIED:
1041 printf("M %s\n", se->head_to_index->old_file.path);
1043 case GIT_STATUS_INDEX_DELETED:
1044 printf("R %s\n", se->head_to_index->old_file.path);
1046 case GIT_STATUS_INDEX_RENAMED:
1047 printf("m %s -> %s\n",
1048 se->head_to_index->old_file.path,
1049 se->head_to_index->new_file.path);
1051 case GIT_STATUS_INDEX_TYPECHANGE:
1052 printf("t %s\n", se->head_to_index->old_file.path);
1054 case GIT_STATUS_CURRENT:
1060 git_status_list_free(status);
1061 git_repository_free(repo);
1062 git_libgit2_shutdown();