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;
322 err = got_object_id_str(&id_str, id);
326 printf("-----------------------------------------------\n");
327 printf("commit %s\n", id_str);
329 printf("from: %s\n", commit->author);
330 datestr = get_datestr(&commit->committer_time, datebuf);
331 printf("date: %s UTC\n", datestr);
332 if (strcmp(commit->author, commit->committer) != 0)
333 printf("via: %s\n", commit->committer);
334 if (commit->nparents > 1) {
335 struct got_object_qid *qid;
337 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
338 err = got_object_id_str(&id_str, qid->id);
341 printf("parent %d: %s\n", n++, id_str);
346 logmsg0 = strdup(commit->logmsg);
348 return got_error_from_errno();
352 line = strsep(&logmsg, "\n");
354 printf(" %s\n", line);
359 err = print_patch(commit, id, diff_context, repo);
368 static const struct got_error *
369 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
370 struct got_repository *repo, char *path, int show_patch, int diff_context,
371 int limit, int first_parent_traversal)
373 const struct got_error *err;
374 struct got_commit_graph *graph;
376 err = got_commit_graph_open(&graph, root_id, path,
377 first_parent_traversal, repo);
380 err = got_commit_graph_iter_start(graph, root_id, repo);
384 struct got_commit_object *commit;
385 struct got_object_id *id;
387 err = got_commit_graph_iter_next(&id, graph);
389 if (err->code == GOT_ERR_ITER_COMPLETED) {
393 if (err->code != GOT_ERR_ITER_NEED_MORE)
395 err = got_commit_graph_fetch_commits(graph, 1, repo);
404 err = got_object_open_as_commit(&commit, repo, id);
407 err = print_commit(commit, id, repo, show_patch, diff_context);
408 got_object_commit_close(commit);
409 if (err || (limit && --limit == 0))
413 got_commit_graph_close(graph);
420 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
421 "[-r repository-path] [path]\n", getprogname());
425 static const struct got_error *
426 cmd_log(int argc, char *argv[])
428 const struct got_error *error;
429 struct got_repository *repo = NULL;
430 struct got_object_id *id = NULL;
431 struct got_object *obj = NULL;
432 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
433 char *start_commit = NULL;
434 int diff_context = 3, ch;
435 int show_patch = 0, limit = 0, first_parent_traversal = 0;
439 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
444 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
450 start_commit = optarg;
453 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
456 err(1, "-C option %s", errstr);
459 limit = strtonum(optarg, 1, INT_MAX, &errstr);
461 err(1, "-l option %s", errstr);
464 first_parent_traversal = 1;
467 repo_path = realpath(optarg, NULL);
468 if (repo_path == NULL)
483 path = strdup(argv[0]);
487 return got_error_from_errno();
489 cwd = getcwd(NULL, 0);
491 error = got_error_from_errno();
494 if (repo_path == NULL) {
495 repo_path = strdup(cwd);
496 if (repo_path == NULL) {
497 error = got_error_from_errno();
502 error = got_repo_open(&repo, repo_path);
506 if (start_commit == NULL) {
507 struct got_reference *head_ref;
508 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
511 error = got_ref_resolve(&id, repo, head_ref);
512 got_ref_close(head_ref);
515 error = got_object_open(&obj, repo, id);
517 struct got_reference *ref;
518 error = got_ref_open(&ref, repo, start_commit);
520 error = got_ref_resolve(&id, repo, ref);
524 error = got_object_open(&obj, repo, id);
529 error = got_object_open_by_id_str(&obj, repo,
533 id = got_object_id_dup(got_object_get_id(obj));
535 error = got_error_from_errno();
540 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
541 error = got_error(GOT_ERR_OBJ_TYPE);
545 error = got_repo_map_path(&in_repo_path, repo, path, 1);
553 error = print_commits(obj, id, repo, path, show_patch,
554 diff_context, limit, first_parent_traversal);
560 got_object_close(obj);
563 const struct got_error *repo_error;
564 repo_error = got_repo_close(repo);
574 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
575 "object1 object2\n", getprogname());
579 static const struct got_error *
580 cmd_diff(int argc, char *argv[])
582 const struct got_error *error;
583 struct got_repository *repo = NULL;
584 struct got_object *obj1 = NULL, *obj2 = NULL;
585 char *repo_path = NULL;
586 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
587 int diff_context = 3, ch;
591 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
596 while ((ch = getopt(argc, argv, "C:")) != -1) {
599 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
601 err(1, "-C option %s", errstr);
613 usage_diff(); /* TODO show local worktree changes */
614 } else if (argc == 2) {
615 repo_path = getcwd(NULL, 0);
616 if (repo_path == NULL)
617 return got_error_from_errno();
618 obj_id_str1 = argv[0];
619 obj_id_str2 = argv[1];
620 } else if (argc == 3) {
621 repo_path = realpath(argv[0], NULL);
622 if (repo_path == NULL)
623 return got_error_from_errno();
624 obj_id_str1 = argv[1];
625 obj_id_str2 = argv[2];
629 error = got_repo_open(&repo, repo_path);
634 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
638 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
642 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
643 error = got_error(GOT_ERR_OBJ_TYPE);
647 switch (got_object_get_type(obj1)) {
648 case GOT_OBJ_TYPE_BLOB:
649 error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
650 diff_context, repo, stdout);
652 case GOT_OBJ_TYPE_TREE:
653 error = got_diff_objects_as_trees(obj1, obj2, "", "",
654 diff_context, repo, stdout);
656 case GOT_OBJ_TYPE_COMMIT:
657 error = got_diff_objects_as_commits(obj1, obj2, diff_context,
661 error = got_error(GOT_ERR_OBJ_TYPE);
666 got_object_close(obj1);
668 got_object_close(obj2);
670 const struct got_error *repo_error;
671 repo_error = got_repo_close(repo);
681 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
686 static const struct got_error *
687 cmd_blame(int argc, char *argv[])
689 const struct got_error *error;
690 struct got_repository *repo = NULL;
691 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
692 struct got_object_id *commit_id = NULL;
693 char *commit_id_str = NULL;
697 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
702 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
705 commit_id_str = optarg;
708 repo_path = realpath(optarg, NULL);
709 if (repo_path == NULL)
726 cwd = getcwd(NULL, 0);
728 error = got_error_from_errno();
731 if (repo_path == NULL) {
732 repo_path = strdup(cwd);
733 if (repo_path == NULL) {
734 error = got_error_from_errno();
739 error = got_repo_open(&repo, repo_path);
743 error = got_repo_map_path(&in_repo_path, repo, path, 1);
747 if (commit_id_str == NULL) {
748 struct got_reference *head_ref;
749 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
752 error = got_ref_resolve(&commit_id, repo, head_ref);
753 got_ref_close(head_ref);
757 struct got_object *obj;
758 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
761 commit_id = got_object_id_dup(got_object_get_id(obj));
762 if (commit_id == NULL)
763 error = got_error_from_errno();
764 got_object_close(obj);
767 error = got_blame(in_repo_path, commit_id, repo, stdout);
774 const struct got_error *repo_error;
775 repo_error = got_repo_close(repo);
785 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
791 static const struct got_error *
792 print_tree(const char *path, struct got_object_id *commit_id,
793 int show_ids, struct got_repository *repo)
795 const struct got_error *err = NULL;
796 struct got_object_id *tree_id = NULL;
797 struct got_tree_object *tree = NULL;
798 const struct got_tree_entries *entries;
799 struct got_tree_entry *te;
801 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
805 err = got_object_open_as_tree(&tree, repo, tree_id);
808 entries = got_object_tree_get_entries(tree);
809 te = SIMPLEQ_FIRST(&entries->head);
814 err = got_object_id_str(&id_str, te->id);
817 if (asprintf(&id, "%s ", id_str) == -1) {
818 err = got_error_from_errno();
824 printf("%s%s%s\n", id ? id : "",
825 te->name, S_ISDIR(te->mode) ? "/" : "");
826 te = SIMPLEQ_NEXT(te, entry);
831 got_object_tree_close(tree);
836 static const struct got_error *
837 cmd_tree(int argc, char *argv[])
839 const struct got_error *error;
840 struct got_repository *repo = NULL;
841 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
842 struct got_object_id *commit_id = NULL;
843 char *commit_id_str = NULL;
848 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
853 while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
856 commit_id_str = optarg;
859 repo_path = realpath(optarg, NULL);
860 if (repo_path == NULL)
882 cwd = getcwd(NULL, 0);
884 error = got_error_from_errno();
887 if (repo_path == NULL) {
888 repo_path = strdup(cwd);
889 if (repo_path == NULL) {
890 error = got_error_from_errno();
895 error = got_repo_open(&repo, repo_path);
899 error = got_repo_map_path(&in_repo_path, repo, path, 1);
903 if (commit_id_str == NULL) {
904 struct got_reference *head_ref;
905 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
908 error = got_ref_resolve(&commit_id, repo, head_ref);
909 got_ref_close(head_ref);
913 struct got_object *obj;
914 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
917 commit_id = got_object_id_dup(got_object_get_id(obj));
918 if (commit_id == NULL)
919 error = got_error_from_errno();
920 got_object_close(obj);
923 error = print_tree(in_repo_path, commit_id, show_ids, repo);
930 const struct got_error *repo_error;
931 repo_error = got_repo_close(repo);
939 static const struct got_error *
940 cmd_status(int argc __unused, char *argv[] __unused)
942 git_repository *repo = NULL;
943 git_status_list *status;
944 git_status_options statusopts;
949 if (git_repository_open_ext(&repo, ".", 0, NULL))
950 errx(1, "git_repository_open: %s", giterr_last()->message);
952 if (git_repository_is_bare(repo))
953 errx(1, "bar repository");
955 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
956 errx(1, "git_status_init_options: %s", giterr_last()->message);
958 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
959 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
960 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
961 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
963 if (git_status_list_new(&status, repo, &statusopts))
964 errx(1, "git_status_list_new: %s", giterr_last()->message);
966 for (i = 0; i < git_status_list_entrycount(status); i++) {
967 const git_status_entry *se;
969 se = git_status_byindex(status, i);
970 switch (se->status) {
971 case GIT_STATUS_WT_NEW:
972 printf("? %s\n", se->index_to_workdir->new_file.path);
974 case GIT_STATUS_WT_MODIFIED:
975 printf("M %s\n", se->index_to_workdir->new_file.path);
977 case GIT_STATUS_WT_DELETED:
978 printf("R %s\n", se->index_to_workdir->new_file.path);
980 case GIT_STATUS_WT_RENAMED:
981 printf("m %s -> %s\n",
982 se->index_to_workdir->old_file.path,
983 se->index_to_workdir->new_file.path);
985 case GIT_STATUS_WT_TYPECHANGE:
986 printf("t %s\n", se->index_to_workdir->new_file.path);
988 case GIT_STATUS_INDEX_NEW:
989 printf("A %s\n", se->head_to_index->new_file.path);
991 case GIT_STATUS_INDEX_MODIFIED:
992 printf("M %s\n", se->head_to_index->old_file.path);
994 case GIT_STATUS_INDEX_DELETED:
995 printf("R %s\n", se->head_to_index->old_file.path);
997 case GIT_STATUS_INDEX_RENAMED:
998 printf("m %s -> %s\n",
999 se->head_to_index->old_file.path,
1000 se->head_to_index->new_file.path);
1002 case GIT_STATUS_INDEX_TYPECHANGE:
1003 printf("t %s\n", se->head_to_index->old_file.path);
1005 case GIT_STATUS_CURRENT:
1011 git_status_list_free(status);
1012 git_repository_free(repo);
1013 git_libgit2_shutdown();