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", NULL) == -1)
197 char *cwd, *base, *dotgit;
198 repo_path = realpath(argv[0], NULL);
199 if (repo_path == NULL)
200 return got_error_from_errno();
201 cwd = getcwd(NULL, 0);
203 error = got_error_from_errno();
207 base = basename(path_prefix);
209 base = basename(repo_path);
211 error = got_error_from_errno();
214 dotgit = strstr(base, ".git");
217 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
218 error = got_error_from_errno();
223 } else if (argc == 2) {
224 repo_path = realpath(argv[0], NULL);
225 if (repo_path == NULL) {
226 error = got_error_from_errno();
229 worktree_path = realpath(argv[1], NULL);
230 if (worktree_path == NULL) {
231 error = got_error_from_errno();
237 error = got_repo_open(&repo, repo_path);
240 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
244 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
248 error = got_worktree_open(&worktree, worktree_path);
252 error = got_worktree_checkout_files(worktree, head_ref, repo,
253 checkout_progress, worktree_path);
257 printf("Checked out %s\n", worktree_path);
258 printf("Now shut up and hack\n");
266 static const struct got_error *
267 print_patch(struct got_commit_object *commit, struct got_object_id *id,
268 struct got_repository *repo)
270 const struct got_error *err = NULL;
271 struct got_tree_object *tree1 = NULL, *tree2;
272 struct got_object_qid *qid;
274 err = got_object_open_as_tree(&tree2, repo, commit->tree_id);
278 qid = SIMPLEQ_FIRST(&commit->parent_ids);
280 struct got_commit_object *pcommit;
282 err = got_object_open_as_commit(&pcommit, repo, qid->id);
286 err = got_object_open_as_tree(&tree1, repo, pcommit->tree_id);
287 got_object_commit_close(pcommit);
292 err = got_diff_tree(tree1, tree2, repo, stdout);
294 got_object_tree_close(tree1);
295 got_object_tree_close(tree2);
300 get_datestr(time_t *time, char *datebuf)
302 char *p, *s = ctime_r(time, datebuf);
309 static const struct got_error *
310 print_commit(struct got_commit_object *commit, struct got_object_id *id,
311 struct got_repository *repo, int show_patch)
313 const struct got_error *err = NULL;
314 char *id_str, *datestr, *logmsg0, *logmsg, *line;
316 time_t author_time, committer_time;
318 err = got_object_id_str(&id_str, id);
322 author_time = mktime(&commit->tm_author);
323 committer_time = mktime(&commit->tm_committer);
325 /* This would express the date in committer's timezone. */
326 author_time += commit->tm_author.tm_gmtoff;
327 committer_time += commit->tm_committer.tm_gmtoff;
330 printf("-----------------------------------------------\n");
331 printf("commit %s\n", id_str);
333 datestr = get_datestr(&author_time, datebuf);
334 printf("author: %s %s UTC\n", commit->author, datestr);
335 if (strcmp(commit->author, commit->committer) != 0 ||
336 author_time != committer_time) {
337 datestr = get_datestr(&committer_time, datebuf);
338 printf("committer: %s %s UTC\n", commit->committer, datestr);
340 if (commit->nparents > 1) {
341 struct got_object_qid *qid;
343 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
344 err = got_object_id_str(&id_str, qid->id);
347 printf("parent %d: %s\n", n++, id_str);
352 logmsg0 = strdup(commit->logmsg);
354 return got_error_from_errno();
358 line = strsep(&logmsg, "\n");
360 printf(" %s\n", line);
365 err = print_patch(commit, id, repo);
373 static const struct got_error *
374 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
375 struct got_repository *repo, char *path, int show_patch, int limit,
376 int first_parent_traversal)
378 const struct got_error *err;
379 struct got_commit_graph *graph;
380 int ncommits, found_obj = 0;
381 int is_root_path = (strcmp(path, "/") == 0);
383 err = got_commit_graph_open(&graph, root_id, first_parent_traversal,
387 err = got_commit_graph_iter_start(graph, root_id);
391 struct got_commit_object *commit;
392 struct got_object_id *id;
394 err = got_commit_graph_iter_next(&id, graph);
396 if (err->code == GOT_ERR_ITER_COMPLETED) {
400 if (err->code != GOT_ERR_ITER_NEED_MORE)
402 err = got_commit_graph_fetch_commits(&ncommits,
412 err = got_object_open_as_commit(&commit, repo, id);
416 struct got_object *obj;
417 struct got_object_qid *pid;
420 err = got_object_open_by_path(&obj, repo, id, path);
422 got_object_commit_close(commit);
423 if (err->code == GOT_ERR_NO_OBJ && found_obj) {
425 * History of the path stops here
426 * on the current commit's branch.
427 * Keep logging on other branches.
436 pid = SIMPLEQ_FIRST(&commit->parent_ids);
438 struct got_object *pobj;
439 err = got_object_open_by_path(&pobj, repo,
442 if (err->code != GOT_ERR_NO_OBJ) {
443 got_object_close(obj);
444 got_object_commit_close(commit);
450 struct got_object_id *id, *pid;
451 id = got_object_get_id(obj);
453 err = got_error_from_errno();
454 got_object_close(obj);
457 pid = got_object_get_id(pobj);
459 err = got_error_from_errno();
461 got_object_close(obj);
462 got_object_close(pobj);
466 (got_object_id_cmp(id, pid) != 0);
467 got_object_close(pobj);
472 got_object_close(obj);
474 got_object_commit_close(commit);
478 err = print_commit(commit, id, repo, show_patch);
479 got_object_commit_close(commit);
480 if (err || (limit && --limit == 0))
482 } while (ncommits > 0);
484 got_commit_graph_close(graph);
491 fprintf(stderr, "usage: %s log [-c commit] [-f] [ -l N ] [-p] "
492 "[-r repository-path] [path]\n", getprogname());
496 static const struct got_error *
497 cmd_log(int argc, char *argv[])
499 const struct got_error *error;
500 struct got_repository *repo = NULL;
501 struct got_object_id *id = NULL;
502 struct got_object *obj = NULL;
503 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
504 char *start_commit = NULL;
506 int show_patch = 0, limit = 0, first_parent_traversal = 0;
510 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
514 while ((ch = getopt(argc, argv, "pc:l:fr:")) != -1) {
520 start_commit = optarg;
523 limit = strtonum(optarg, 1, INT_MAX, &errstr);
525 err(1, "-l option %s", errstr);
528 first_parent_traversal = 1;
531 repo_path = realpath(optarg, NULL);
532 if (repo_path == NULL)
547 path = strdup(argv[0]);
551 return got_error_from_errno();
553 cwd = getcwd(NULL, 0);
555 error = got_error_from_errno();
558 if (repo_path == NULL) {
559 repo_path = strdup(cwd);
560 if (repo_path == NULL) {
561 error = got_error_from_errno();
566 error = got_repo_open(&repo, repo_path);
570 if (start_commit == NULL) {
571 struct got_reference *head_ref;
572 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
575 error = got_ref_resolve(&id, repo, head_ref);
576 got_ref_close(head_ref);
579 error = got_object_open(&obj, repo, id);
581 struct got_reference *ref;
582 error = got_ref_open(&ref, repo, start_commit);
584 error = got_ref_resolve(&id, repo, ref);
588 error = got_object_open(&obj, repo, id);
593 error = got_object_open_by_id_str(&obj, repo,
597 id = got_object_get_id(obj);
599 error = got_error_from_errno();
604 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
605 error = got_error(GOT_ERR_OBJ_TYPE);
609 error = got_repo_map_path(&in_repo_path, repo, path);
617 error = print_commits(obj, id, repo, path, show_patch,
618 limit, first_parent_traversal);
624 got_object_close(obj);
627 got_repo_close(repo);
634 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
639 static const struct got_error *
640 cmd_diff(int argc, char *argv[])
642 const struct got_error *error;
643 struct got_repository *repo = NULL;
644 struct got_object_id *id1 = NULL, *id2 = NULL;
645 struct got_object *obj1 = NULL, *obj2 = NULL;
646 char *repo_path = NULL;
647 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
651 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
655 while ((ch = getopt(argc, argv, "")) != -1) {
667 usage_diff(); /* TODO show local worktree changes */
668 } else if (argc == 2) {
669 repo_path = getcwd(NULL, 0);
670 if (repo_path == NULL)
671 return got_error_from_errno();
672 obj_id_str1 = argv[0];
673 obj_id_str2 = argv[1];
674 } else if (argc == 3) {
675 repo_path = realpath(argv[0], NULL);
676 if (repo_path == NULL)
677 return got_error_from_errno();
678 obj_id_str1 = argv[1];
679 obj_id_str2 = argv[2];
683 error = got_repo_open(&repo, repo_path);
688 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
690 id1 = got_object_get_id(obj1);
692 error = got_error_from_errno();
697 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
699 id2 = got_object_get_id(obj2);
701 error = got_error_from_errno();
706 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
707 error = got_error(GOT_ERR_OBJ_TYPE);
711 switch (got_object_get_type(obj1)) {
712 case GOT_OBJ_TYPE_BLOB:
713 error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
715 case GOT_OBJ_TYPE_TREE:
716 error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
718 case GOT_OBJ_TYPE_COMMIT:
719 error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
722 error = got_error(GOT_ERR_OBJ_TYPE);
727 got_object_close(obj1);
729 got_object_close(obj2);
735 got_repo_close(repo);
742 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
747 static const struct got_error *
748 cmd_blame(int argc, char *argv[])
750 const struct got_error *error;
751 struct got_repository *repo = NULL;
752 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
753 struct got_object_id *commit_id = NULL;
754 char *commit_id_str = NULL;
758 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
762 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
765 commit_id_str = optarg;
768 repo_path = realpath(optarg, NULL);
769 if (repo_path == NULL)
786 cwd = getcwd(NULL, 0);
788 error = got_error_from_errno();
791 if (repo_path == NULL) {
792 repo_path = strdup(cwd);
793 if (repo_path == NULL) {
794 error = got_error_from_errno();
799 error = got_repo_open(&repo, repo_path);
803 error = got_repo_map_path(&in_repo_path, repo, path);
807 if (commit_id_str == NULL) {
808 struct got_reference *head_ref;
809 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
812 error = got_ref_resolve(&commit_id, repo, head_ref);
813 got_ref_close(head_ref);
817 struct got_object *obj;
818 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
821 commit_id = got_object_get_id(obj);
822 if (commit_id == NULL)
823 error = got_error_from_errno();
824 got_object_close(obj);
827 error = got_blame(in_repo_path, commit_id, repo, stdout);
834 got_repo_close(repo);
839 static const struct got_error *
840 cmd_status(int argc __unused, char *argv[] __unused)
842 git_repository *repo = NULL;
843 git_status_list *status;
844 git_status_options statusopts;
849 if (git_repository_open_ext(&repo, ".", 0, NULL))
850 errx(1, "git_repository_open: %s", giterr_last()->message);
852 if (git_repository_is_bare(repo))
853 errx(1, "bar repository");
855 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
856 errx(1, "git_status_init_options: %s", giterr_last()->message);
858 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
859 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
860 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
861 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
863 if (git_status_list_new(&status, repo, &statusopts))
864 errx(1, "git_status_list_new: %s", giterr_last()->message);
866 for (i = 0; i < git_status_list_entrycount(status); i++) {
867 const git_status_entry *se;
869 se = git_status_byindex(status, i);
870 switch (se->status) {
871 case GIT_STATUS_WT_NEW:
872 printf("? %s\n", se->index_to_workdir->new_file.path);
874 case GIT_STATUS_WT_MODIFIED:
875 printf("M %s\n", se->index_to_workdir->new_file.path);
877 case GIT_STATUS_WT_DELETED:
878 printf("R %s\n", se->index_to_workdir->new_file.path);
880 case GIT_STATUS_WT_RENAMED:
881 printf("m %s -> %s\n",
882 se->index_to_workdir->old_file.path,
883 se->index_to_workdir->new_file.path);
885 case GIT_STATUS_WT_TYPECHANGE:
886 printf("t %s\n", se->index_to_workdir->new_file.path);
888 case GIT_STATUS_INDEX_NEW:
889 printf("A %s\n", se->head_to_index->new_file.path);
891 case GIT_STATUS_INDEX_MODIFIED:
892 printf("M %s\n", se->head_to_index->old_file.path);
894 case GIT_STATUS_INDEX_DELETED:
895 printf("R %s\n", se->head_to_index->old_file.path);
897 case GIT_STATUS_INDEX_RENAMED:
898 printf("m %s -> %s\n",
899 se->head_to_index->old_file.path,
900 se->head_to_index->new_file.path);
902 case GIT_STATUS_INDEX_TYPECHANGE:
903 printf("t %s\n", se->head_to_index->old_file.path);
905 case GIT_STATUS_CURRENT:
911 git_status_list_free(status);
912 git_repository_free(repo);
913 git_libgit2_shutdown();