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>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_reference.h"
35 #include "got_repository.h"
36 #include "got_worktree.h"
38 #include "got_commit_graph.h"
39 #include "got_blame.h"
42 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
47 const struct got_error *(*cmd_main)(int, char *[]);
48 void (*cmd_usage)(void);
49 const char *cmd_descr;
52 __dead static void usage(void);
53 __dead static void usage_checkout(void);
54 __dead static void usage_log(void);
55 __dead static void usage_diff(void);
56 __dead static void usage_blame(void);
58 static const struct got_error* cmd_checkout(int, char *[]);
59 static const struct got_error* cmd_log(int, char *[]);
60 static const struct got_error* cmd_diff(int, char *[]);
61 static const struct got_error* cmd_blame(int, char *[]);
63 static const struct got_error* cmd_status(int, char *[]);
66 static struct cmd got_commands[] = {
67 { "checkout", cmd_checkout, usage_checkout,
68 "check out a new work tree from a repository" },
69 { "log", cmd_log, usage_log,
70 "show repository history" },
71 { "diff", cmd_diff, usage_diff,
72 "compare files and directories" },
73 { "blame", cmd_blame, usage_blame,
74 " show when lines in a file were changed" },
76 { "status", cmd_status, usage_status,
77 "show modification status of files" },
82 main(int argc, char *argv[])
89 setlocale(LC_ALL, "");
91 while ((ch = getopt(argc, argv, "h")) != -1) {
109 for (i = 0; i < nitems(got_commands); i++) {
110 const struct got_error *error;
112 cmd = &got_commands[i];
114 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
118 got_commands[i].cmd_usage();
120 error = got_commands[i].cmd_main(argc, argv);
122 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
129 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
138 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
139 "Available commands:\n", getprogname());
140 for (i = 0; i < nitems(got_commands); i++) {
141 struct cmd *cmd = &got_commands[i];
142 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
150 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
151 "[worktree-path]\n", getprogname());
156 checkout_progress(void *arg, const char *path)
158 char *worktree_path = arg;
160 while (path[0] == '/')
163 printf("A %s/%s\n", worktree_path, path);
166 static const struct got_error *
167 cmd_checkout(int argc, char *argv[])
169 const struct got_error *error = NULL;
170 struct got_repository *repo = NULL;
171 struct got_reference *head_ref = NULL;
172 struct got_worktree *worktree = NULL;
173 char *repo_path = NULL;
174 char *worktree_path = NULL;
175 const char *path_prefix = "";
178 while ((ch = getopt(argc, argv, "p:")) != -1) {
181 path_prefix = optarg;
193 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
198 char *cwd, *base, *dotgit;
199 repo_path = realpath(argv[0], NULL);
200 if (repo_path == NULL)
201 return got_error_from_errno();
202 cwd = getcwd(NULL, 0);
204 error = got_error_from_errno();
208 base = basename(path_prefix);
210 base = basename(repo_path);
212 error = got_error_from_errno();
215 dotgit = strstr(base, ".git");
218 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
219 error = got_error_from_errno();
224 } else if (argc == 2) {
225 repo_path = realpath(argv[0], NULL);
226 if (repo_path == NULL) {
227 error = got_error_from_errno();
230 worktree_path = realpath(argv[1], NULL);
231 if (worktree_path == NULL) {
232 error = got_error_from_errno();
238 error = got_repo_open(&repo, repo_path);
241 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
245 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
249 error = got_worktree_open(&worktree, worktree_path);
253 error = got_worktree_checkout_files(worktree, head_ref, repo,
254 checkout_progress, worktree_path);
258 printf("Checked out %s\n", worktree_path);
259 printf("Now shut up and hack\n");
267 static const struct got_error *
268 print_patch(struct got_commit_object *commit, struct got_object_id *id,
269 struct got_repository *repo)
271 const struct got_error *err = NULL;
272 struct got_tree_object *tree1 = NULL, *tree2;
273 struct got_object_qid *qid;
275 err = got_object_open_as_tree(&tree2, repo, commit->tree_id);
279 qid = SIMPLEQ_FIRST(&commit->parent_ids);
281 struct got_commit_object *pcommit;
283 err = got_object_open_as_commit(&pcommit, repo, qid->id);
287 err = got_object_open_as_tree(&tree1, repo, pcommit->tree_id);
288 got_object_commit_close(pcommit);
293 err = got_diff_tree(tree1, tree2, repo, stdout);
295 got_object_tree_close(tree1);
296 got_object_tree_close(tree2);
301 get_datestr(time_t *time, char *datebuf)
303 char *p, *s = ctime_r(time, datebuf);
310 static const struct got_error *
311 print_commit(struct got_commit_object *commit, struct got_object_id *id,
312 struct got_repository *repo, int show_patch)
314 const struct got_error *err = NULL;
315 char *id_str, *datestr, *logmsg0, *logmsg, *line;
317 time_t author_time, committer_time;
319 err = got_object_id_str(&id_str, id);
323 author_time = mktime(&commit->tm_author);
324 committer_time = mktime(&commit->tm_committer);
326 /* This would express the date in committer's timezone. */
327 author_time += commit->tm_author.tm_gmtoff;
328 committer_time += commit->tm_committer.tm_gmtoff;
331 printf("-----------------------------------------------\n");
332 printf("commit %s\n", id_str);
334 datestr = get_datestr(&author_time, datebuf);
335 printf("author: %s %s UTC\n", commit->author, datestr);
336 if (strcmp(commit->author, commit->committer) != 0 ||
337 author_time != committer_time) {
338 datestr = get_datestr(&committer_time, datebuf);
339 printf("committer: %s %s UTC\n", commit->committer, datestr);
341 if (commit->nparents > 1) {
342 struct got_object_qid *qid;
344 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
345 err = got_object_id_str(&id_str, qid->id);
348 printf("parent %d: %s\n", n++, id_str);
353 logmsg0 = strdup(commit->logmsg);
355 return got_error_from_errno();
359 line = strsep(&logmsg, "\n");
361 printf(" %s\n", line);
366 err = print_patch(commit, id, repo);
374 static const struct got_error *
375 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
376 struct got_repository *repo, char *path, int show_patch, int limit,
377 int first_parent_traversal)
379 const struct got_error *err;
380 struct got_commit_graph *graph;
381 int ncommits, found_obj = 0;
382 int is_root_path = (strcmp(path, "/") == 0);
384 err = got_commit_graph_open(&graph, root_id, first_parent_traversal,
388 err = got_commit_graph_iter_start(graph, root_id);
392 struct got_commit_object *commit;
393 struct got_object_id *id;
395 err = got_commit_graph_iter_next(&id, graph);
397 if (err->code == GOT_ERR_ITER_COMPLETED) {
401 if (err->code != GOT_ERR_ITER_NEED_MORE)
403 err = got_commit_graph_fetch_commits(&ncommits,
413 err = got_object_open_as_commit(&commit, repo, id);
417 struct got_object *obj;
418 struct got_object_qid *pid;
421 err = got_object_open_by_path(&obj, repo, id, path);
423 got_object_commit_close(commit);
424 if (err->code == GOT_ERR_NO_OBJ && found_obj) {
426 * History of the path stops here
427 * on the current commit's branch.
428 * Keep logging on other branches.
437 pid = SIMPLEQ_FIRST(&commit->parent_ids);
439 struct got_object *pobj;
440 err = got_object_open_by_path(&pobj, repo,
443 if (err->code != GOT_ERR_NO_OBJ) {
444 got_object_close(obj);
445 got_object_commit_close(commit);
451 struct got_object_id *id, *pid;
452 id = got_object_get_id(obj);
454 err = got_error_from_errno();
455 got_object_close(obj);
458 pid = got_object_get_id(pobj);
460 err = got_error_from_errno();
462 got_object_close(obj);
463 got_object_close(pobj);
467 (got_object_id_cmp(id, pid) != 0);
468 got_object_close(pobj);
473 got_object_close(obj);
475 got_object_commit_close(commit);
479 err = print_commit(commit, id, repo, show_patch);
480 got_object_commit_close(commit);
481 if (err || (limit && --limit == 0))
483 } while (ncommits > 0);
485 got_commit_graph_close(graph);
492 fprintf(stderr, "usage: %s log [-c commit] [-f] [ -l N ] [-p] "
493 "[-r repository-path] [path]\n", getprogname());
497 static const struct got_error *
498 cmd_log(int argc, char *argv[])
500 const struct got_error *error;
501 struct got_repository *repo = NULL;
502 struct got_object_id *id = NULL;
503 struct got_object *obj = NULL;
504 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
505 char *start_commit = NULL;
507 int show_patch = 0, limit = 0, first_parent_traversal = 0;
511 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
516 while ((ch = getopt(argc, argv, "pc:l:fr:")) != -1) {
522 start_commit = optarg;
525 limit = strtonum(optarg, 1, INT_MAX, &errstr);
527 err(1, "-l option %s", errstr);
530 first_parent_traversal = 1;
533 repo_path = realpath(optarg, NULL);
534 if (repo_path == NULL)
549 path = strdup(argv[0]);
553 return got_error_from_errno();
555 cwd = getcwd(NULL, 0);
557 error = got_error_from_errno();
560 if (repo_path == NULL) {
561 repo_path = strdup(cwd);
562 if (repo_path == NULL) {
563 error = got_error_from_errno();
568 error = got_repo_open(&repo, repo_path);
572 if (start_commit == NULL) {
573 struct got_reference *head_ref;
574 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
577 error = got_ref_resolve(&id, repo, head_ref);
578 got_ref_close(head_ref);
581 error = got_object_open(&obj, repo, id);
583 struct got_reference *ref;
584 error = got_ref_open(&ref, repo, start_commit);
586 error = got_ref_resolve(&id, repo, ref);
590 error = got_object_open(&obj, repo, id);
595 error = got_object_open_by_id_str(&obj, repo,
599 id = got_object_get_id(obj);
601 error = got_error_from_errno();
606 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
607 error = got_error(GOT_ERR_OBJ_TYPE);
611 error = got_repo_map_path(&in_repo_path, repo, path);
619 error = print_commits(obj, id, repo, path, show_patch,
620 limit, first_parent_traversal);
626 got_object_close(obj);
629 const struct got_error *repo_error;
630 repo_error = got_repo_close(repo);
640 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
645 static const struct got_error *
646 cmd_diff(int argc, char *argv[])
648 const struct got_error *error;
649 struct got_repository *repo = NULL;
650 struct got_object_id *id1 = NULL, *id2 = NULL;
651 struct got_object *obj1 = NULL, *obj2 = NULL;
652 char *repo_path = NULL;
653 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
657 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
662 while ((ch = getopt(argc, argv, "")) != -1) {
674 usage_diff(); /* TODO show local worktree changes */
675 } else if (argc == 2) {
676 repo_path = getcwd(NULL, 0);
677 if (repo_path == NULL)
678 return got_error_from_errno();
679 obj_id_str1 = argv[0];
680 obj_id_str2 = argv[1];
681 } else if (argc == 3) {
682 repo_path = realpath(argv[0], NULL);
683 if (repo_path == NULL)
684 return got_error_from_errno();
685 obj_id_str1 = argv[1];
686 obj_id_str2 = argv[2];
690 error = got_repo_open(&repo, repo_path);
695 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
697 id1 = got_object_get_id(obj1);
699 error = got_error_from_errno();
704 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
706 id2 = got_object_get_id(obj2);
708 error = got_error_from_errno();
713 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
714 error = got_error(GOT_ERR_OBJ_TYPE);
718 switch (got_object_get_type(obj1)) {
719 case GOT_OBJ_TYPE_BLOB:
720 error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
722 case GOT_OBJ_TYPE_TREE:
723 error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
725 case GOT_OBJ_TYPE_COMMIT:
726 error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
729 error = got_error(GOT_ERR_OBJ_TYPE);
734 got_object_close(obj1);
736 got_object_close(obj2);
742 const struct got_error *repo_error;
743 repo_error = got_repo_close(repo);
753 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
758 static const struct got_error *
759 cmd_blame(int argc, char *argv[])
761 const struct got_error *error;
762 struct got_repository *repo = NULL;
763 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
764 struct got_object_id *commit_id = NULL;
765 char *commit_id_str = NULL;
769 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
774 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
777 commit_id_str = optarg;
780 repo_path = realpath(optarg, NULL);
781 if (repo_path == NULL)
798 cwd = getcwd(NULL, 0);
800 error = got_error_from_errno();
803 if (repo_path == NULL) {
804 repo_path = strdup(cwd);
805 if (repo_path == NULL) {
806 error = got_error_from_errno();
811 error = got_repo_open(&repo, repo_path);
815 error = got_repo_map_path(&in_repo_path, repo, path);
819 if (commit_id_str == NULL) {
820 struct got_reference *head_ref;
821 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
824 error = got_ref_resolve(&commit_id, repo, head_ref);
825 got_ref_close(head_ref);
829 struct got_object *obj;
830 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
833 commit_id = got_object_get_id(obj);
834 if (commit_id == NULL)
835 error = got_error_from_errno();
836 got_object_close(obj);
839 error = got_blame(in_repo_path, commit_id, repo, stdout);
846 const struct got_error *repo_error;
847 repo_error = got_repo_close(repo);
855 static const struct got_error *
856 cmd_status(int argc __unused, char *argv[] __unused)
858 git_repository *repo = NULL;
859 git_status_list *status;
860 git_status_options statusopts;
865 if (git_repository_open_ext(&repo, ".", 0, NULL))
866 errx(1, "git_repository_open: %s", giterr_last()->message);
868 if (git_repository_is_bare(repo))
869 errx(1, "bar repository");
871 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
872 errx(1, "git_status_init_options: %s", giterr_last()->message);
874 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
875 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
876 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
877 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
879 if (git_status_list_new(&status, repo, &statusopts))
880 errx(1, "git_status_list_new: %s", giterr_last()->message);
882 for (i = 0; i < git_status_list_entrycount(status); i++) {
883 const git_status_entry *se;
885 se = git_status_byindex(status, i);
886 switch (se->status) {
887 case GIT_STATUS_WT_NEW:
888 printf("? %s\n", se->index_to_workdir->new_file.path);
890 case GIT_STATUS_WT_MODIFIED:
891 printf("M %s\n", se->index_to_workdir->new_file.path);
893 case GIT_STATUS_WT_DELETED:
894 printf("R %s\n", se->index_to_workdir->new_file.path);
896 case GIT_STATUS_WT_RENAMED:
897 printf("m %s -> %s\n",
898 se->index_to_workdir->old_file.path,
899 se->index_to_workdir->new_file.path);
901 case GIT_STATUS_WT_TYPECHANGE:
902 printf("t %s\n", se->index_to_workdir->new_file.path);
904 case GIT_STATUS_INDEX_NEW:
905 printf("A %s\n", se->head_to_index->new_file.path);
907 case GIT_STATUS_INDEX_MODIFIED:
908 printf("M %s\n", se->head_to_index->old_file.path);
910 case GIT_STATUS_INDEX_DELETED:
911 printf("R %s\n", se->head_to_index->old_file.path);
913 case GIT_STATUS_INDEX_RENAMED:
914 printf("m %s -> %s\n",
915 se->head_to_index->old_file.path,
916 se->head_to_index->new_file.path);
918 case GIT_STATUS_INDEX_TYPECHANGE:
919 printf("t %s\n", se->head_to_index->old_file.path);
921 case GIT_STATUS_CURRENT:
927 git_status_list_free(status);
928 git_repository_free(repo);
929 git_libgit2_shutdown();