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>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_reference.h"
36 #include "got_repository.h"
37 #include "got_worktree.h"
39 #include "got_commit_graph.h"
40 #include "got_blame.h"
43 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
48 const struct got_error *(*cmd_main)(int, char *[]);
49 void (*cmd_usage)(void);
50 const char *cmd_descr;
53 __dead static void usage(void);
54 __dead static void usage_checkout(void);
55 __dead static void usage_log(void);
56 __dead static void usage_diff(void);
57 __dead static void usage_blame(void);
58 __dead static void usage_tree(void);
60 static const struct got_error* cmd_checkout(int, char *[]);
61 static const struct got_error* cmd_log(int, char *[]);
62 static const struct got_error* cmd_diff(int, char *[]);
63 static const struct got_error* cmd_blame(int, char *[]);
64 static const struct got_error* cmd_tree(int, char *[]);
66 static const struct got_error* cmd_status(int, char *[]);
69 static struct cmd got_commands[] = {
70 { "checkout", cmd_checkout, usage_checkout,
71 "check out a new work tree from a repository" },
72 { "log", cmd_log, usage_log,
73 "show repository history" },
74 { "diff", cmd_diff, usage_diff,
75 "compare files and directories" },
76 { "blame", cmd_blame, usage_blame,
77 " show when lines in a file were changed" },
78 { "tree", cmd_tree, usage_tree,
79 " list files and directories in repository" },
81 { "status", cmd_status, usage_status,
82 "show modification status of files" },
87 main(int argc, char *argv[])
94 setlocale(LC_ALL, "");
96 while ((ch = getopt(argc, argv, "h")) != -1) {
114 for (i = 0; i < nitems(got_commands); i++) {
115 const struct got_error *error;
117 cmd = &got_commands[i];
119 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
123 got_commands[i].cmd_usage();
125 error = got_commands[i].cmd_main(argc, argv);
127 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
134 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
143 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
144 "Available commands:\n", getprogname());
145 for (i = 0; i < nitems(got_commands); i++) {
146 struct cmd *cmd = &got_commands[i];
147 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
155 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
156 "[worktree-path]\n", getprogname());
161 checkout_progress(void *arg, const char *path)
163 char *worktree_path = arg;
165 while (path[0] == '/')
168 printf("A %s/%s\n", worktree_path, path);
171 static const struct got_error *
172 cmd_checkout(int argc, char *argv[])
174 const struct got_error *error = NULL;
175 struct got_repository *repo = NULL;
176 struct got_reference *head_ref = NULL;
177 struct got_worktree *worktree = NULL;
178 char *repo_path = NULL;
179 char *worktree_path = NULL;
180 const char *path_prefix = "";
183 while ((ch = getopt(argc, argv, "p:")) != -1) {
186 path_prefix = optarg;
198 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
203 char *cwd, *base, *dotgit;
204 repo_path = realpath(argv[0], NULL);
205 if (repo_path == NULL)
206 return got_error_from_errno();
207 cwd = getcwd(NULL, 0);
209 error = got_error_from_errno();
213 base = basename(path_prefix);
215 base = basename(repo_path);
217 error = got_error_from_errno();
220 dotgit = strstr(base, ".git");
223 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
224 error = got_error_from_errno();
229 } else if (argc == 2) {
230 repo_path = realpath(argv[0], NULL);
231 if (repo_path == NULL) {
232 error = got_error_from_errno();
235 worktree_path = realpath(argv[1], NULL);
236 if (worktree_path == NULL) {
237 error = got_error_from_errno();
243 error = got_repo_open(&repo, repo_path);
246 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
250 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
254 error = got_worktree_open(&worktree, worktree_path);
258 error = got_worktree_checkout_files(worktree, head_ref, repo,
259 checkout_progress, worktree_path);
263 printf("Now shut up and hack\n");
271 static const struct got_error *
272 print_patch(struct got_commit_object *commit, struct got_object_id *id,
273 int diff_context, struct got_repository *repo)
275 const struct got_error *err = NULL;
276 struct got_tree_object *tree1 = NULL, *tree2;
277 struct got_object_qid *qid;
279 err = got_object_open_as_tree(&tree2, repo, commit->tree_id);
283 qid = SIMPLEQ_FIRST(&commit->parent_ids);
285 struct got_commit_object *pcommit;
287 err = got_object_open_as_commit(&pcommit, repo, qid->id);
291 err = got_object_open_as_tree(&tree1, repo, pcommit->tree_id);
292 got_object_commit_close(pcommit);
297 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
299 got_object_tree_close(tree1);
300 got_object_tree_close(tree2);
305 get_datestr(time_t *time, char *datebuf)
307 char *p, *s = ctime_r(time, datebuf);
314 static const struct got_error *
315 print_commit(struct got_commit_object *commit, struct got_object_id *id,
316 struct got_repository *repo, int show_patch, int diff_context)
318 const struct got_error *err = NULL;
319 char *id_str, *datestr, *logmsg0, *logmsg, *line;
321 time_t author_time, committer_time;
323 err = got_object_id_str(&id_str, id);
327 author_time = mktime(&commit->tm_author);
328 committer_time = mktime(&commit->tm_committer);
330 /* This would express the date in committer's timezone. */
331 author_time += commit->tm_author.tm_gmtoff;
332 committer_time += commit->tm_committer.tm_gmtoff;
335 printf("-----------------------------------------------\n");
336 printf("commit %s\n", id_str);
338 printf("from: %s\n", commit->author);
339 datestr = get_datestr(&committer_time, datebuf);
340 printf("date: %s UTC\n", datestr);
341 if (strcmp(commit->author, commit->committer) != 0)
342 printf("via: %s\n", commit->committer);
343 if (commit->nparents > 1) {
344 struct got_object_qid *qid;
346 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
347 err = got_object_id_str(&id_str, qid->id);
350 printf("parent %d: %s\n", n++, id_str);
355 logmsg0 = strdup(commit->logmsg);
357 return got_error_from_errno();
361 line = strsep(&logmsg, "\n");
363 printf(" %s\n", line);
368 err = print_patch(commit, id, diff_context, repo);
377 static const struct got_error *
378 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
379 struct got_repository *repo, char *path, int show_patch, int diff_context,
380 int limit, int first_parent_traversal)
382 const struct got_error *err;
383 struct got_commit_graph *graph;
385 err = got_commit_graph_open(&graph, root_id, path,
386 first_parent_traversal, repo);
389 err = got_commit_graph_iter_start(graph, root_id, repo);
393 struct got_commit_object *commit;
394 struct got_object_id *id;
396 err = got_commit_graph_iter_next(&id, graph);
398 if (err->code == GOT_ERR_ITER_COMPLETED) {
402 if (err->code != GOT_ERR_ITER_NEED_MORE)
404 err = got_commit_graph_fetch_commits(graph, 1, repo);
413 err = got_object_open_as_commit(&commit, repo, id);
416 err = print_commit(commit, id, repo, show_patch, diff_context);
417 got_object_commit_close(commit);
418 if (err || (limit && --limit == 0))
422 got_commit_graph_close(graph);
429 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
430 "[-r repository-path] [path]\n", getprogname());
434 static const struct got_error *
435 cmd_log(int argc, char *argv[])
437 const struct got_error *error;
438 struct got_repository *repo = NULL;
439 struct got_object_id *id = NULL;
440 struct got_object *obj = NULL;
441 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
442 char *start_commit = NULL;
443 int diff_context = 3, ch;
444 int show_patch = 0, limit = 0, first_parent_traversal = 0;
448 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
453 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
459 start_commit = optarg;
462 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
465 err(1, "-C option %s", errstr);
468 limit = strtonum(optarg, 1, INT_MAX, &errstr);
470 err(1, "-l option %s", errstr);
473 first_parent_traversal = 1;
476 repo_path = realpath(optarg, NULL);
477 if (repo_path == NULL)
492 path = strdup(argv[0]);
496 return got_error_from_errno();
498 cwd = getcwd(NULL, 0);
500 error = got_error_from_errno();
503 if (repo_path == NULL) {
504 repo_path = strdup(cwd);
505 if (repo_path == NULL) {
506 error = got_error_from_errno();
511 error = got_repo_open(&repo, repo_path);
515 if (start_commit == NULL) {
516 struct got_reference *head_ref;
517 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
520 error = got_ref_resolve(&id, repo, head_ref);
521 got_ref_close(head_ref);
524 error = got_object_open(&obj, repo, id);
526 struct got_reference *ref;
527 error = got_ref_open(&ref, repo, start_commit);
529 error = got_ref_resolve(&id, repo, ref);
533 error = got_object_open(&obj, repo, id);
538 error = got_object_open_by_id_str(&obj, repo,
542 id = got_object_id_dup(got_object_get_id(obj));
544 error = got_error_from_errno();
549 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
550 error = got_error(GOT_ERR_OBJ_TYPE);
554 error = got_repo_map_path(&in_repo_path, repo, path, 1);
562 error = print_commits(obj, id, repo, path, show_patch,
563 diff_context, limit, first_parent_traversal);
569 got_object_close(obj);
572 const struct got_error *repo_error;
573 repo_error = got_repo_close(repo);
583 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
584 "object1 object2\n", getprogname());
588 static const struct got_error *
589 cmd_diff(int argc, char *argv[])
591 const struct got_error *error;
592 struct got_repository *repo = NULL;
593 struct got_object *obj1 = NULL, *obj2 = NULL;
594 char *repo_path = NULL;
595 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
596 int diff_context = 3, ch;
600 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
605 while ((ch = getopt(argc, argv, "C:")) != -1) {
608 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
610 err(1, "-C option %s", errstr);
622 usage_diff(); /* TODO show local worktree changes */
623 } else if (argc == 2) {
624 repo_path = getcwd(NULL, 0);
625 if (repo_path == NULL)
626 return got_error_from_errno();
627 obj_id_str1 = argv[0];
628 obj_id_str2 = argv[1];
629 } else if (argc == 3) {
630 repo_path = realpath(argv[0], NULL);
631 if (repo_path == NULL)
632 return got_error_from_errno();
633 obj_id_str1 = argv[1];
634 obj_id_str2 = argv[2];
638 error = got_repo_open(&repo, repo_path);
643 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
647 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
651 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
652 error = got_error(GOT_ERR_OBJ_TYPE);
656 switch (got_object_get_type(obj1)) {
657 case GOT_OBJ_TYPE_BLOB:
658 error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
659 diff_context, repo, stdout);
661 case GOT_OBJ_TYPE_TREE:
662 error = got_diff_objects_as_trees(obj1, obj2, "", "",
663 diff_context, repo, stdout);
665 case GOT_OBJ_TYPE_COMMIT:
666 error = got_diff_objects_as_commits(obj1, obj2, diff_context,
670 error = got_error(GOT_ERR_OBJ_TYPE);
675 got_object_close(obj1);
677 got_object_close(obj2);
679 const struct got_error *repo_error;
680 repo_error = got_repo_close(repo);
690 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
695 static const struct got_error *
696 cmd_blame(int argc, char *argv[])
698 const struct got_error *error;
699 struct got_repository *repo = NULL;
700 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
701 struct got_object_id *commit_id = NULL;
702 char *commit_id_str = NULL;
706 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
711 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
714 commit_id_str = optarg;
717 repo_path = realpath(optarg, NULL);
718 if (repo_path == NULL)
735 cwd = getcwd(NULL, 0);
737 error = got_error_from_errno();
740 if (repo_path == NULL) {
741 repo_path = strdup(cwd);
742 if (repo_path == NULL) {
743 error = got_error_from_errno();
748 error = got_repo_open(&repo, repo_path);
752 error = got_repo_map_path(&in_repo_path, repo, path, 1);
756 if (commit_id_str == NULL) {
757 struct got_reference *head_ref;
758 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
761 error = got_ref_resolve(&commit_id, repo, head_ref);
762 got_ref_close(head_ref);
766 struct got_object *obj;
767 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
770 commit_id = got_object_id_dup(got_object_get_id(obj));
771 if (commit_id == NULL)
772 error = got_error_from_errno();
773 got_object_close(obj);
776 error = got_blame(in_repo_path, commit_id, repo, stdout);
783 const struct got_error *repo_error;
784 repo_error = got_repo_close(repo);
794 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
800 static const struct got_error *
801 print_tree(const char *path, struct got_object_id *commit_id,
802 int show_ids, struct got_repository *repo)
804 const struct got_error *err = NULL;
805 struct got_object_id *tree_id = NULL;
806 struct got_tree_object *tree = NULL;
807 const struct got_tree_entries *entries;
808 struct got_tree_entry *te;
810 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
814 err = got_object_open_as_tree(&tree, repo, tree_id);
817 entries = got_object_tree_get_entries(tree);
818 te = SIMPLEQ_FIRST(&entries->head);
823 err = got_object_id_str(&id_str, te->id);
826 if (asprintf(&id, "%s ", id_str) == -1) {
827 err = got_error_from_errno();
833 printf("%s%s%s\n", id ? id : "",
834 te->name, S_ISDIR(te->mode) ? "/" : "");
835 te = SIMPLEQ_NEXT(te, entry);
840 got_object_tree_close(tree);
845 static const struct got_error *
846 cmd_tree(int argc, char *argv[])
848 const struct got_error *error;
849 struct got_repository *repo = NULL;
850 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
851 struct got_object_id *commit_id = NULL;
852 char *commit_id_str = NULL;
857 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
862 while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
865 commit_id_str = optarg;
868 repo_path = realpath(optarg, NULL);
869 if (repo_path == NULL)
891 cwd = getcwd(NULL, 0);
893 error = got_error_from_errno();
896 if (repo_path == NULL) {
897 repo_path = strdup(cwd);
898 if (repo_path == NULL) {
899 error = got_error_from_errno();
904 error = got_repo_open(&repo, repo_path);
908 error = got_repo_map_path(&in_repo_path, repo, path, 1);
912 if (commit_id_str == NULL) {
913 struct got_reference *head_ref;
914 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
917 error = got_ref_resolve(&commit_id, repo, head_ref);
918 got_ref_close(head_ref);
922 struct got_object *obj;
923 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
926 commit_id = got_object_id_dup(got_object_get_id(obj));
927 if (commit_id == NULL)
928 error = got_error_from_errno();
929 got_object_close(obj);
932 error = print_tree(in_repo_path, commit_id, show_ids, repo);
939 const struct got_error *repo_error;
940 repo_error = got_repo_close(repo);
948 static const struct got_error *
949 cmd_status(int argc __unused, char *argv[] __unused)
951 git_repository *repo = NULL;
952 git_status_list *status;
953 git_status_options statusopts;
958 if (git_repository_open_ext(&repo, ".", 0, NULL))
959 errx(1, "git_repository_open: %s", giterr_last()->message);
961 if (git_repository_is_bare(repo))
962 errx(1, "bar repository");
964 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
965 errx(1, "git_status_init_options: %s", giterr_last()->message);
967 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
968 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
969 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
970 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
972 if (git_status_list_new(&status, repo, &statusopts))
973 errx(1, "git_status_list_new: %s", giterr_last()->message);
975 for (i = 0; i < git_status_list_entrycount(status); i++) {
976 const git_status_entry *se;
978 se = git_status_byindex(status, i);
979 switch (se->status) {
980 case GIT_STATUS_WT_NEW:
981 printf("? %s\n", se->index_to_workdir->new_file.path);
983 case GIT_STATUS_WT_MODIFIED:
984 printf("M %s\n", se->index_to_workdir->new_file.path);
986 case GIT_STATUS_WT_DELETED:
987 printf("R %s\n", se->index_to_workdir->new_file.path);
989 case GIT_STATUS_WT_RENAMED:
990 printf("m %s -> %s\n",
991 se->index_to_workdir->old_file.path,
992 se->index_to_workdir->new_file.path);
994 case GIT_STATUS_WT_TYPECHANGE:
995 printf("t %s\n", se->index_to_workdir->new_file.path);
997 case GIT_STATUS_INDEX_NEW:
998 printf("A %s\n", se->head_to_index->new_file.path);
1000 case GIT_STATUS_INDEX_MODIFIED:
1001 printf("M %s\n", se->head_to_index->old_file.path);
1003 case GIT_STATUS_INDEX_DELETED:
1004 printf("R %s\n", se->head_to_index->old_file.path);
1006 case GIT_STATUS_INDEX_RENAMED:
1007 printf("m %s -> %s\n",
1008 se->head_to_index->old_file.path,
1009 se->head_to_index->new_file.path);
1011 case GIT_STATUS_INDEX_TYPECHANGE:
1012 printf("t %s\n", se->head_to_index->old_file.path);
1014 case GIT_STATUS_CURRENT:
1020 git_status_list_free(status);
1021 git_repository_free(repo);
1022 git_libgit2_shutdown();