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 *obj;
273 struct got_object_qid *qid;
275 err = got_object_open(&obj, repo, commit->tree_id);
279 err = got_object_tree_open(&tree2, repo, obj);
280 got_object_close(obj);
284 qid = SIMPLEQ_FIRST(&commit->parent_ids);
286 struct got_commit_object *pcommit;
288 err = got_object_open(&obj, repo, qid->id);
292 err = got_object_commit_open(&pcommit, repo, obj);
293 got_object_close(obj);
297 err = got_object_open(&obj, repo, pcommit->tree_id);
298 got_object_commit_close(pcommit);
301 err = got_object_tree_open(&tree1, repo, obj);
302 got_object_close(obj);
307 err = got_diff_tree(tree1, tree2, repo, stdout);
309 got_object_tree_close(tree1);
310 got_object_tree_close(tree2);
315 get_datestr(time_t *time, char *datebuf)
317 char *p, *s = ctime_r(time, datebuf);
324 static const struct got_error *
325 print_commit(struct got_commit_object *commit, struct got_object_id *id,
326 struct got_repository *repo, int show_patch)
328 const struct got_error *err = NULL;
329 char *id_str, *datestr, *logmsg, *line;
331 time_t author_time, committer_time;
333 err = got_object_id_str(&id_str, id);
337 author_time = mktime(&commit->tm_author);
338 committer_time = mktime(&commit->tm_committer);
340 /* This would express the date in committer's timezone. */
341 author_time += commit->tm_author.tm_gmtoff;
342 committer_time += commit->tm_committer.tm_gmtoff;
345 printf("-----------------------------------------------\n");
346 printf("commit %s\n", id_str);
348 datestr = get_datestr(&author_time, datebuf);
349 printf("author: %s %s UTC\n", commit->author, datestr);
350 if (strcmp(commit->author, commit->committer) != 0 ||
351 author_time != committer_time) {
352 datestr = get_datestr(&committer_time, datebuf);
353 printf("committer: %s %s UTC\n", commit->committer, datestr);
355 if (commit->nparents > 1) {
356 struct got_object_qid *qid;
358 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
359 err = got_object_id_str(&id_str, qid->id);
362 printf("parent %d: %s\n", n++, id_str);
367 logmsg = strdup(commit->logmsg);
369 return got_error_from_errno();
372 line = strsep(&logmsg, "\n");
374 printf(" %s\n", line);
379 err = print_patch(commit, id, repo);
387 static const struct got_error *
388 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
389 struct got_repository *repo, int show_patch, int limit,
390 int first_parent_traversal)
392 const struct got_error *err;
393 struct got_commit_graph *graph;
396 err = got_commit_graph_open(&graph, root_id, first_parent_traversal,
400 err = got_commit_graph_iter_start(graph, root_id);
404 struct got_commit_object *commit;
405 struct got_object_id *id;
407 err = got_commit_graph_iter_next(&id, graph);
409 if (err->code == GOT_ERR_ITER_COMPLETED) {
413 if (err->code != GOT_ERR_ITER_NEED_MORE)
415 err = got_commit_graph_fetch_commits(&ncommits,
425 err = got_object_open_as_commit(&commit, repo, id);
428 err = print_commit(commit, id, repo, show_patch);
429 got_object_commit_close(commit);
430 if (err || (limit && --limit == 0))
432 } while (ncommits > 0);
434 got_commit_graph_close(graph);
441 fprintf(stderr, "usage: %s log [-c commit] [-f] [ -l N ] [-p] "
442 "[repository-path]\n", getprogname());
446 static const struct got_error *
447 cmd_log(int argc, char *argv[])
449 const struct got_error *error;
450 struct got_repository *repo;
451 struct got_object_id *id = NULL;
452 struct got_object *obj = NULL;
453 char *repo_path = NULL;
454 char *start_commit = NULL;
456 int show_patch = 0, limit = 0, first_parent_traversal = 0;
460 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
464 while ((ch = getopt(argc, argv, "pc:l:f")) != -1) {
470 start_commit = optarg;
473 limit = strtonum(optarg, 1, INT_MAX, &errstr);
475 err(1, "-l option %s", errstr);
478 first_parent_traversal = 1;
490 repo_path = getcwd(NULL, 0);
491 if (repo_path == NULL)
492 return got_error_from_errno();
493 } else if (argc == 1) {
494 repo_path = realpath(argv[0], NULL);
495 if (repo_path == NULL)
496 return got_error_from_errno();
500 error = got_repo_open(&repo, repo_path);
505 if (start_commit == NULL) {
506 struct got_reference *head_ref;
507 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
510 error = got_ref_resolve(&id, repo, head_ref);
511 got_ref_close(head_ref);
514 error = got_object_open(&obj, repo, id);
516 struct got_reference *ref;
517 error = got_ref_open(&ref, repo, start_commit);
519 error = got_ref_resolve(&id, repo, ref);
523 error = got_object_open(&obj, repo, id);
528 error = got_object_open_by_id_str(&obj, repo,
532 id = got_object_get_id(obj);
534 error = got_error_from_errno();
539 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
540 error = print_commits(obj, id, repo, show_patch, limit,
541 first_parent_traversal);
543 error = got_error(GOT_ERR_OBJ_TYPE);
544 got_object_close(obj);
546 got_repo_close(repo);
553 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
558 static const struct got_error *
559 cmd_diff(int argc, char *argv[])
561 const struct got_error *error;
562 struct got_repository *repo = NULL;
563 struct got_object_id *id1 = NULL, *id2 = NULL;
564 struct got_object *obj1 = NULL, *obj2 = NULL;
565 char *repo_path = NULL;
566 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
570 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
574 while ((ch = getopt(argc, argv, "")) != -1) {
586 usage_diff(); /* TODO show local worktree changes */
587 } else if (argc == 2) {
588 repo_path = getcwd(NULL, 0);
589 if (repo_path == NULL)
590 return got_error_from_errno();
591 obj_id_str1 = argv[0];
592 obj_id_str2 = argv[1];
593 } else if (argc == 3) {
594 repo_path = realpath(argv[0], NULL);
595 if (repo_path == NULL)
596 return got_error_from_errno();
597 obj_id_str1 = argv[1];
598 obj_id_str2 = argv[2];
602 error = got_repo_open(&repo, repo_path);
607 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
609 id1 = got_object_get_id(obj1);
611 error = got_error_from_errno();
616 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
618 id2 = got_object_get_id(obj2);
620 error = got_error_from_errno();
625 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
626 error = got_error(GOT_ERR_OBJ_TYPE);
630 switch (got_object_get_type(obj1)) {
631 case GOT_OBJ_TYPE_BLOB:
632 error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
634 case GOT_OBJ_TYPE_TREE:
635 error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
637 case GOT_OBJ_TYPE_COMMIT:
638 error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
641 error = got_error(GOT_ERR_OBJ_TYPE);
646 got_object_close(obj1);
648 got_object_close(obj2);
654 got_repo_close(repo);
661 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
666 static const struct got_error *
667 cmd_blame(int argc, char *argv[])
669 const struct got_error *error;
670 struct got_repository *repo = NULL;
671 char *repo_path = NULL;
673 struct got_object_id *commit_id = NULL;
674 char *commit_id_str = NULL;
678 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
682 while ((ch = getopt(argc, argv, "c:")) != -1) {
685 commit_id_str = optarg;
698 } else if (argc == 1) {
699 repo_path = getcwd(NULL, 0);
700 if (repo_path == NULL)
701 return got_error_from_errno();
703 } else if (argc == 2) {
704 repo_path = realpath(argv[0], NULL);
705 if (repo_path == NULL)
706 return got_error_from_errno();
711 error = got_repo_open(&repo, repo_path);
716 if (commit_id_str == NULL) {
717 struct got_reference *head_ref;
718 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
721 error = got_ref_resolve(&commit_id, repo, head_ref);
722 got_ref_close(head_ref);
726 struct got_object *obj;
727 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
730 commit_id = got_object_get_id(obj);
731 if (commit_id == NULL)
732 error = got_error_from_errno();
733 got_object_close(obj);
736 error = got_blame(path, commit_id, repo, stdout);
740 got_repo_close(repo);
745 static const struct got_error *
746 cmd_status(int argc __unused, char *argv[] __unused)
748 git_repository *repo = NULL;
749 git_status_list *status;
750 git_status_options statusopts;
755 if (git_repository_open_ext(&repo, ".", 0, NULL))
756 errx(1, "git_repository_open: %s", giterr_last()->message);
758 if (git_repository_is_bare(repo))
759 errx(1, "bar repository");
761 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
762 errx(1, "git_status_init_options: %s", giterr_last()->message);
764 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
765 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
766 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
767 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
769 if (git_status_list_new(&status, repo, &statusopts))
770 errx(1, "git_status_list_new: %s", giterr_last()->message);
772 for (i = 0; i < git_status_list_entrycount(status); i++) {
773 const git_status_entry *se;
775 se = git_status_byindex(status, i);
776 switch (se->status) {
777 case GIT_STATUS_WT_NEW:
778 printf("? %s\n", se->index_to_workdir->new_file.path);
780 case GIT_STATUS_WT_MODIFIED:
781 printf("M %s\n", se->index_to_workdir->new_file.path);
783 case GIT_STATUS_WT_DELETED:
784 printf("R %s\n", se->index_to_workdir->new_file.path);
786 case GIT_STATUS_WT_RENAMED:
787 printf("m %s -> %s\n",
788 se->index_to_workdir->old_file.path,
789 se->index_to_workdir->new_file.path);
791 case GIT_STATUS_WT_TYPECHANGE:
792 printf("t %s\n", se->index_to_workdir->new_file.path);
794 case GIT_STATUS_INDEX_NEW:
795 printf("A %s\n", se->head_to_index->new_file.path);
797 case GIT_STATUS_INDEX_MODIFIED:
798 printf("M %s\n", se->head_to_index->old_file.path);
800 case GIT_STATUS_INDEX_DELETED:
801 printf("R %s\n", se->head_to_index->old_file.path);
803 case GIT_STATUS_INDEX_RENAMED:
804 printf("m %s -> %s\n",
805 se->head_to_index->old_file.path,
806 se->head_to_index->new_file.path);
808 case GIT_STATUS_INDEX_TYPECHANGE:
809 printf("t %s\n", se->head_to_index->old_file.path);
811 case GIT_STATUS_CURRENT:
817 git_status_list_free(status);
818 git_repository_free(repo);
819 git_libgit2_shutdown();