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 printf("from: %s\n", commit->author);
335 datestr = get_datestr(&committer_time, datebuf);
336 printf("date: %s UTC\n", datestr);
337 if (strcmp(commit->author, commit->committer) != 0)
338 printf("via: %s\n", commit->committer);
339 if (commit->nparents > 1) {
340 struct got_object_qid *qid;
342 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
343 err = got_object_id_str(&id_str, qid->id);
346 printf("parent %d: %s\n", n++, id_str);
351 logmsg0 = strdup(commit->logmsg);
353 return got_error_from_errno();
357 line = strsep(&logmsg, "\n");
359 printf(" %s\n", line);
364 err = print_patch(commit, id, repo);
372 static const struct got_error *
373 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
374 struct got_repository *repo, char *path, int show_patch, int limit,
375 int first_parent_traversal)
377 const struct got_error *err;
378 struct got_commit_graph *graph;
380 err = got_commit_graph_open(&graph, root_id, path,
381 first_parent_traversal, repo);
384 err = got_commit_graph_iter_start(graph, root_id, repo);
388 struct got_commit_object *commit;
389 struct got_object_id *id;
391 err = got_commit_graph_iter_next(&id, graph);
393 if (err->code == GOT_ERR_ITER_COMPLETED) {
397 if (err->code != GOT_ERR_ITER_NEED_MORE)
399 err = got_commit_graph_fetch_commits(graph, 1, repo);
408 err = got_object_open_as_commit(&commit, repo, id);
411 err = print_commit(commit, id, repo, show_patch);
412 got_object_commit_close(commit);
413 if (err || (limit && --limit == 0))
417 got_commit_graph_close(graph);
424 fprintf(stderr, "usage: %s log [-c commit] [-f] [ -l N ] [-p] "
425 "[-r repository-path] [path]\n", getprogname());
429 static const struct got_error *
430 cmd_log(int argc, char *argv[])
432 const struct got_error *error;
433 struct got_repository *repo = NULL;
434 struct got_object_id *id = NULL;
435 struct got_object *obj = NULL;
436 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
437 char *start_commit = NULL;
439 int show_patch = 0, limit = 0, first_parent_traversal = 0;
443 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
448 while ((ch = getopt(argc, argv, "pc:l:fr:")) != -1) {
454 start_commit = optarg;
457 limit = strtonum(optarg, 1, INT_MAX, &errstr);
459 err(1, "-l option %s", errstr);
462 first_parent_traversal = 1;
465 repo_path = realpath(optarg, NULL);
466 if (repo_path == NULL)
481 path = strdup(argv[0]);
485 return got_error_from_errno();
487 cwd = getcwd(NULL, 0);
489 error = got_error_from_errno();
492 if (repo_path == NULL) {
493 repo_path = strdup(cwd);
494 if (repo_path == NULL) {
495 error = got_error_from_errno();
500 error = got_repo_open(&repo, repo_path);
504 if (start_commit == NULL) {
505 struct got_reference *head_ref;
506 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
509 error = got_ref_resolve(&id, repo, head_ref);
510 got_ref_close(head_ref);
513 error = got_object_open(&obj, repo, id);
515 struct got_reference *ref;
516 error = got_ref_open(&ref, repo, start_commit);
518 error = got_ref_resolve(&id, repo, ref);
522 error = got_object_open(&obj, repo, id);
527 error = got_object_open_by_id_str(&obj, repo,
531 id = got_object_id_dup(got_object_get_id(obj));
533 error = got_error_from_errno();
538 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
539 error = got_error(GOT_ERR_OBJ_TYPE);
543 error = got_repo_map_path(&in_repo_path, repo, path);
551 error = print_commits(obj, id, repo, path, show_patch,
552 limit, first_parent_traversal);
558 got_object_close(obj);
561 const struct got_error *repo_error;
562 repo_error = got_repo_close(repo);
572 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
577 static const struct got_error *
578 cmd_diff(int argc, char *argv[])
580 const struct got_error *error;
581 struct got_repository *repo = NULL;
582 struct got_object *obj1 = NULL, *obj2 = NULL;
583 char *repo_path = NULL;
584 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
588 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
593 while ((ch = getopt(argc, argv, "")) != -1) {
605 usage_diff(); /* TODO show local worktree changes */
606 } else if (argc == 2) {
607 repo_path = getcwd(NULL, 0);
608 if (repo_path == NULL)
609 return got_error_from_errno();
610 obj_id_str1 = argv[0];
611 obj_id_str2 = argv[1];
612 } else if (argc == 3) {
613 repo_path = realpath(argv[0], NULL);
614 if (repo_path == NULL)
615 return got_error_from_errno();
616 obj_id_str1 = argv[1];
617 obj_id_str2 = argv[2];
621 error = got_repo_open(&repo, repo_path);
626 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
630 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
634 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
635 error = got_error(GOT_ERR_OBJ_TYPE);
639 switch (got_object_get_type(obj1)) {
640 case GOT_OBJ_TYPE_BLOB:
641 error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
644 case GOT_OBJ_TYPE_TREE:
645 error = got_diff_objects_as_trees(obj1, obj2, "", "", repo,
648 case GOT_OBJ_TYPE_COMMIT:
649 error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
652 error = got_error(GOT_ERR_OBJ_TYPE);
657 got_object_close(obj1);
659 got_object_close(obj2);
661 const struct got_error *repo_error;
662 repo_error = got_repo_close(repo);
672 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
677 static const struct got_error *
678 cmd_blame(int argc, char *argv[])
680 const struct got_error *error;
681 struct got_repository *repo = NULL;
682 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
683 struct got_object_id *commit_id = NULL;
684 char *commit_id_str = NULL;
688 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
693 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
696 commit_id_str = optarg;
699 repo_path = realpath(optarg, NULL);
700 if (repo_path == NULL)
717 cwd = getcwd(NULL, 0);
719 error = got_error_from_errno();
722 if (repo_path == NULL) {
723 repo_path = strdup(cwd);
724 if (repo_path == NULL) {
725 error = got_error_from_errno();
730 error = got_repo_open(&repo, repo_path);
734 error = got_repo_map_path(&in_repo_path, repo, path);
738 if (commit_id_str == NULL) {
739 struct got_reference *head_ref;
740 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
743 error = got_ref_resolve(&commit_id, repo, head_ref);
744 got_ref_close(head_ref);
748 struct got_object *obj;
749 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
752 commit_id = got_object_id_dup(got_object_get_id(obj));
753 if (commit_id == NULL)
754 error = got_error_from_errno();
755 got_object_close(obj);
758 error = got_blame(in_repo_path, commit_id, repo, stdout);
765 const struct got_error *repo_error;
766 repo_error = got_repo_close(repo);
774 static const struct got_error *
775 cmd_status(int argc __unused, char *argv[] __unused)
777 git_repository *repo = NULL;
778 git_status_list *status;
779 git_status_options statusopts;
784 if (git_repository_open_ext(&repo, ".", 0, NULL))
785 errx(1, "git_repository_open: %s", giterr_last()->message);
787 if (git_repository_is_bare(repo))
788 errx(1, "bar repository");
790 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
791 errx(1, "git_status_init_options: %s", giterr_last()->message);
793 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
794 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
795 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
796 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
798 if (git_status_list_new(&status, repo, &statusopts))
799 errx(1, "git_status_list_new: %s", giterr_last()->message);
801 for (i = 0; i < git_status_list_entrycount(status); i++) {
802 const git_status_entry *se;
804 se = git_status_byindex(status, i);
805 switch (se->status) {
806 case GIT_STATUS_WT_NEW:
807 printf("? %s\n", se->index_to_workdir->new_file.path);
809 case GIT_STATUS_WT_MODIFIED:
810 printf("M %s\n", se->index_to_workdir->new_file.path);
812 case GIT_STATUS_WT_DELETED:
813 printf("R %s\n", se->index_to_workdir->new_file.path);
815 case GIT_STATUS_WT_RENAMED:
816 printf("m %s -> %s\n",
817 se->index_to_workdir->old_file.path,
818 se->index_to_workdir->new_file.path);
820 case GIT_STATUS_WT_TYPECHANGE:
821 printf("t %s\n", se->index_to_workdir->new_file.path);
823 case GIT_STATUS_INDEX_NEW:
824 printf("A %s\n", se->head_to_index->new_file.path);
826 case GIT_STATUS_INDEX_MODIFIED:
827 printf("M %s\n", se->head_to_index->old_file.path);
829 case GIT_STATUS_INDEX_DELETED:
830 printf("R %s\n", se->head_to_index->old_file.path);
832 case GIT_STATUS_INDEX_RENAMED:
833 printf("m %s -> %s\n",
834 se->head_to_index->old_file.path,
835 se->head_to_index->new_file.path);
837 case GIT_STATUS_INDEX_TYPECHANGE:
838 printf("t %s\n", se->head_to_index->old_file.path);
840 case GIT_STATUS_CURRENT:
846 git_status_list_free(status);
847 git_repository_free(repo);
848 git_libgit2_shutdown();