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("Checked out %s\n", worktree_path);
264 printf("Now shut up and hack\n");
272 static const struct got_error *
273 print_patch(struct got_commit_object *commit, struct got_object_id *id,
274 int diff_context, struct got_repository *repo)
276 const struct got_error *err = NULL;
277 struct got_tree_object *tree1 = NULL, *tree2;
278 struct got_object_qid *qid;
280 err = got_object_open_as_tree(&tree2, repo, commit->tree_id);
284 qid = SIMPLEQ_FIRST(&commit->parent_ids);
286 struct got_commit_object *pcommit;
288 err = got_object_open_as_commit(&pcommit, repo, qid->id);
292 err = got_object_open_as_tree(&tree1, repo, pcommit->tree_id);
293 got_object_commit_close(pcommit);
298 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
300 got_object_tree_close(tree1);
301 got_object_tree_close(tree2);
306 get_datestr(time_t *time, char *datebuf)
308 char *p, *s = ctime_r(time, datebuf);
315 static const struct got_error *
316 print_commit(struct got_commit_object *commit, struct got_object_id *id,
317 struct got_repository *repo, int show_patch, int diff_context)
319 const struct got_error *err = NULL;
320 char *id_str, *datestr, *logmsg0, *logmsg, *line;
322 time_t author_time, committer_time;
324 err = got_object_id_str(&id_str, id);
328 author_time = mktime(&commit->tm_author);
329 committer_time = mktime(&commit->tm_committer);
331 /* This would express the date in committer's timezone. */
332 author_time += commit->tm_author.tm_gmtoff;
333 committer_time += commit->tm_committer.tm_gmtoff;
336 printf("-----------------------------------------------\n");
337 printf("commit %s\n", id_str);
339 printf("from: %s\n", commit->author);
340 datestr = get_datestr(&committer_time, datebuf);
341 printf("date: %s UTC\n", datestr);
342 if (strcmp(commit->author, commit->committer) != 0)
343 printf("via: %s\n", commit->committer);
344 if (commit->nparents > 1) {
345 struct got_object_qid *qid;
347 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
348 err = got_object_id_str(&id_str, qid->id);
351 printf("parent %d: %s\n", n++, id_str);
356 logmsg0 = strdup(commit->logmsg);
358 return got_error_from_errno();
362 line = strsep(&logmsg, "\n");
364 printf(" %s\n", line);
369 err = print_patch(commit, id, diff_context, repo);
378 static const struct got_error *
379 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
380 struct got_repository *repo, char *path, int show_patch, int diff_context,
381 int limit, int first_parent_traversal)
383 const struct got_error *err;
384 struct got_commit_graph *graph;
386 err = got_commit_graph_open(&graph, root_id, path,
387 first_parent_traversal, repo);
390 err = got_commit_graph_iter_start(graph, root_id, repo);
394 struct got_commit_object *commit;
395 struct got_object_id *id;
397 err = got_commit_graph_iter_next(&id, graph);
399 if (err->code == GOT_ERR_ITER_COMPLETED) {
403 if (err->code != GOT_ERR_ITER_NEED_MORE)
405 err = got_commit_graph_fetch_commits(graph, 1, repo);
414 err = got_object_open_as_commit(&commit, repo, id);
417 err = print_commit(commit, id, repo, show_patch, diff_context);
418 got_object_commit_close(commit);
419 if (err || (limit && --limit == 0))
423 got_commit_graph_close(graph);
430 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
431 "[-r repository-path] [path]\n", getprogname());
435 static const struct got_error *
436 cmd_log(int argc, char *argv[])
438 const struct got_error *error;
439 struct got_repository *repo = NULL;
440 struct got_object_id *id = NULL;
441 struct got_object *obj = NULL;
442 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
443 char *start_commit = NULL;
444 int diff_context = 3, ch;
445 int show_patch = 0, limit = 0, first_parent_traversal = 0;
449 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
454 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
460 start_commit = optarg;
463 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
466 err(1, "-C option %s", errstr);
469 limit = strtonum(optarg, 1, INT_MAX, &errstr);
471 err(1, "-l option %s", errstr);
474 first_parent_traversal = 1;
477 repo_path = realpath(optarg, NULL);
478 if (repo_path == NULL)
493 path = strdup(argv[0]);
497 return got_error_from_errno();
499 cwd = getcwd(NULL, 0);
501 error = got_error_from_errno();
504 if (repo_path == NULL) {
505 repo_path = strdup(cwd);
506 if (repo_path == NULL) {
507 error = got_error_from_errno();
512 error = got_repo_open(&repo, repo_path);
516 if (start_commit == NULL) {
517 struct got_reference *head_ref;
518 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
521 error = got_ref_resolve(&id, repo, head_ref);
522 got_ref_close(head_ref);
525 error = got_object_open(&obj, repo, id);
527 struct got_reference *ref;
528 error = got_ref_open(&ref, repo, start_commit);
530 error = got_ref_resolve(&id, repo, ref);
534 error = got_object_open(&obj, repo, id);
539 error = got_object_open_by_id_str(&obj, repo,
543 id = got_object_id_dup(got_object_get_id(obj));
545 error = got_error_from_errno();
550 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
551 error = got_error(GOT_ERR_OBJ_TYPE);
555 error = got_repo_map_path(&in_repo_path, repo, path);
563 error = print_commits(obj, id, repo, path, show_patch,
564 diff_context, limit, first_parent_traversal);
570 got_object_close(obj);
573 const struct got_error *repo_error;
574 repo_error = got_repo_close(repo);
584 fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
585 "object1 object2\n", getprogname());
589 static const struct got_error *
590 cmd_diff(int argc, char *argv[])
592 const struct got_error *error;
593 struct got_repository *repo = NULL;
594 struct got_object *obj1 = NULL, *obj2 = NULL;
595 char *repo_path = NULL;
596 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
597 int diff_context = 3, ch;
601 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
606 while ((ch = getopt(argc, argv, "C:")) != -1) {
609 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
611 err(1, "-C option %s", errstr);
623 usage_diff(); /* TODO show local worktree changes */
624 } else if (argc == 2) {
625 repo_path = getcwd(NULL, 0);
626 if (repo_path == NULL)
627 return got_error_from_errno();
628 obj_id_str1 = argv[0];
629 obj_id_str2 = argv[1];
630 } else if (argc == 3) {
631 repo_path = realpath(argv[0], NULL);
632 if (repo_path == NULL)
633 return got_error_from_errno();
634 obj_id_str1 = argv[1];
635 obj_id_str2 = argv[2];
639 error = got_repo_open(&repo, repo_path);
644 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
648 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
652 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
653 error = got_error(GOT_ERR_OBJ_TYPE);
657 switch (got_object_get_type(obj1)) {
658 case GOT_OBJ_TYPE_BLOB:
659 error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
660 diff_context, repo, stdout);
662 case GOT_OBJ_TYPE_TREE:
663 error = got_diff_objects_as_trees(obj1, obj2, "", "",
664 diff_context, repo, stdout);
666 case GOT_OBJ_TYPE_COMMIT:
667 error = got_diff_objects_as_commits(obj1, obj2, diff_context,
671 error = got_error(GOT_ERR_OBJ_TYPE);
676 got_object_close(obj1);
678 got_object_close(obj2);
680 const struct got_error *repo_error;
681 repo_error = got_repo_close(repo);
691 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
696 static const struct got_error *
697 cmd_blame(int argc, char *argv[])
699 const struct got_error *error;
700 struct got_repository *repo = NULL;
701 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
702 struct got_object_id *commit_id = NULL;
703 char *commit_id_str = NULL;
707 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
712 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
715 commit_id_str = optarg;
718 repo_path = realpath(optarg, NULL);
719 if (repo_path == NULL)
736 cwd = getcwd(NULL, 0);
738 error = got_error_from_errno();
741 if (repo_path == NULL) {
742 repo_path = strdup(cwd);
743 if (repo_path == NULL) {
744 error = got_error_from_errno();
749 error = got_repo_open(&repo, repo_path);
753 error = got_repo_map_path(&in_repo_path, repo, path);
757 if (commit_id_str == NULL) {
758 struct got_reference *head_ref;
759 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
762 error = got_ref_resolve(&commit_id, repo, head_ref);
763 got_ref_close(head_ref);
767 struct got_object *obj;
768 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
771 commit_id = got_object_id_dup(got_object_get_id(obj));
772 if (commit_id == NULL)
773 error = got_error_from_errno();
774 got_object_close(obj);
777 error = got_blame(in_repo_path, commit_id, repo, stdout);
784 const struct got_error *repo_error;
785 repo_error = got_repo_close(repo);
795 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
801 static const struct got_error *
802 print_tree(const char *path, struct got_object_id *commit_id,
803 int show_ids, struct got_repository *repo)
805 const struct got_error *err = NULL;
806 struct got_object_id *tree_id = NULL;
807 struct got_tree_object *tree = NULL;
808 const struct got_tree_entries *entries;
809 struct got_tree_entry *te;
811 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
815 err = got_object_open_as_tree(&tree, repo, tree_id);
818 entries = got_object_tree_get_entries(tree);
819 te = SIMPLEQ_FIRST(&entries->head);
824 err = got_object_id_str(&id_str, te->id);
827 if (asprintf(&id, "%s ", id_str) == -1) {
828 err = got_error_from_errno();
834 printf("%s%s%s\n", id ? id : "",
835 te->name, S_ISDIR(te->mode) ? "/" : "");
836 te = SIMPLEQ_NEXT(te, entry);
841 got_object_tree_close(tree);
846 static const struct got_error *
847 cmd_tree(int argc, char *argv[])
849 const struct got_error *error;
850 struct got_repository *repo = NULL;
851 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
852 struct got_object_id *commit_id = NULL;
853 char *commit_id_str = NULL;
858 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
863 while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
866 commit_id_str = optarg;
869 repo_path = realpath(optarg, NULL);
870 if (repo_path == NULL)
892 cwd = getcwd(NULL, 0);
894 error = got_error_from_errno();
897 if (repo_path == NULL) {
898 repo_path = strdup(cwd);
899 if (repo_path == NULL) {
900 error = got_error_from_errno();
905 error = got_repo_open(&repo, repo_path);
909 error = got_repo_map_path(&in_repo_path, repo, path);
913 if (commit_id_str == NULL) {
914 struct got_reference *head_ref;
915 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
918 error = got_ref_resolve(&commit_id, repo, head_ref);
919 got_ref_close(head_ref);
923 struct got_object *obj;
924 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
927 commit_id = got_object_id_dup(got_object_get_id(obj));
928 if (commit_id == NULL)
929 error = got_error_from_errno();
930 got_object_close(obj);
933 error = print_tree(in_repo_path, commit_id, show_ids, repo);
940 const struct got_error *repo_error;
941 repo_error = got_repo_close(repo);
949 static const struct got_error *
950 cmd_status(int argc __unused, char *argv[] __unused)
952 git_repository *repo = NULL;
953 git_status_list *status;
954 git_status_options statusopts;
959 if (git_repository_open_ext(&repo, ".", 0, NULL))
960 errx(1, "git_repository_open: %s", giterr_last()->message);
962 if (git_repository_is_bare(repo))
963 errx(1, "bar repository");
965 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
966 errx(1, "git_status_init_options: %s", giterr_last()->message);
968 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
969 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
970 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
971 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
973 if (git_status_list_new(&status, repo, &statusopts))
974 errx(1, "git_status_list_new: %s", giterr_last()->message);
976 for (i = 0; i < git_status_list_entrycount(status); i++) {
977 const git_status_entry *se;
979 se = git_status_byindex(status, i);
980 switch (se->status) {
981 case GIT_STATUS_WT_NEW:
982 printf("? %s\n", se->index_to_workdir->new_file.path);
984 case GIT_STATUS_WT_MODIFIED:
985 printf("M %s\n", se->index_to_workdir->new_file.path);
987 case GIT_STATUS_WT_DELETED:
988 printf("R %s\n", se->index_to_workdir->new_file.path);
990 case GIT_STATUS_WT_RENAMED:
991 printf("m %s -> %s\n",
992 se->index_to_workdir->old_file.path,
993 se->index_to_workdir->new_file.path);
995 case GIT_STATUS_WT_TYPECHANGE:
996 printf("t %s\n", se->index_to_workdir->new_file.path);
998 case GIT_STATUS_INDEX_NEW:
999 printf("A %s\n", se->head_to_index->new_file.path);
1001 case GIT_STATUS_INDEX_MODIFIED:
1002 printf("M %s\n", se->head_to_index->old_file.path);
1004 case GIT_STATUS_INDEX_DELETED:
1005 printf("R %s\n", se->head_to_index->old_file.path);
1007 case GIT_STATUS_INDEX_RENAMED:
1008 printf("m %s -> %s\n",
1009 se->head_to_index->old_file.path,
1010 se->head_to_index->new_file.path);
1012 case GIT_STATUS_INDEX_TYPECHANGE:
1013 printf("t %s\n", se->head_to_index->old_file.path);
1015 case GIT_STATUS_CURRENT:
1021 git_status_list_free(status);
1022 git_repository_free(repo);
1023 git_libgit2_shutdown();