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, char *path, int show_patch, int limit,
390 int first_parent_traversal)
392 const struct got_error *err;
393 struct got_commit_graph *graph;
394 int ncommits, found_obj = 0;
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);
429 struct got_object *obj;
430 struct got_object_qid *pid;
433 err = got_object_open_by_path(&obj, repo, id, path);
435 if (err->code == GOT_ERR_NO_OBJ && found_obj) {
437 * Object was added in previous commit.
438 * History stops here.
446 pid = SIMPLEQ_FIRST(&commit->parent_ids);
448 struct got_object *pobj;
449 err = got_object_open_by_path(&pobj, repo,
452 if (err->code != GOT_ERR_NO_OBJ) {
453 got_object_close(obj);
459 changed = (got_object_id_cmp(
460 got_object_get_id(obj),
461 got_object_get_id(pobj)) != 0);
462 got_object_close(pobj);
466 got_object_commit_close(commit);
470 err = print_commit(commit, id, repo, show_patch);
471 got_object_commit_close(commit);
472 if (err || (limit && --limit == 0))
474 } while (ncommits > 0);
476 got_commit_graph_close(graph);
483 fprintf(stderr, "usage: %s log [-c commit] [-f] [ -l N ] [-p] "
484 "[-r repository-path] [path]\n", getprogname());
488 static const struct got_error *
489 cmd_log(int argc, char *argv[])
491 const struct got_error *error;
492 struct got_repository *repo = NULL;
493 struct got_object_id *id = NULL;
494 struct got_object *obj = NULL;
495 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
496 char *start_commit = NULL;
498 int show_patch = 0, limit = 0, first_parent_traversal = 0;
502 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
506 while ((ch = getopt(argc, argv, "pc:l:fr:")) != -1) {
512 start_commit = optarg;
515 limit = strtonum(optarg, 1, INT_MAX, &errstr);
517 err(1, "-l option %s", errstr);
520 first_parent_traversal = 1;
523 repo_path = realpath(optarg, NULL);
524 if (repo_path == NULL)
539 path = strdup(argv[0]);
543 return got_error_from_errno();
545 cwd = getcwd(NULL, 0);
547 error = got_error_from_errno();
550 if (repo_path == NULL) {
551 repo_path = strdup(cwd);
552 if (repo_path == NULL) {
553 error = got_error_from_errno();
558 error = got_repo_open(&repo, repo_path);
562 if (start_commit == NULL) {
563 struct got_reference *head_ref;
564 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
567 error = got_ref_resolve(&id, repo, head_ref);
568 got_ref_close(head_ref);
571 error = got_object_open(&obj, repo, id);
573 struct got_reference *ref;
574 error = got_ref_open(&ref, repo, start_commit);
576 error = got_ref_resolve(&id, repo, ref);
580 error = got_object_open(&obj, repo, id);
585 error = got_object_open_by_id_str(&obj, repo,
589 id = got_object_get_id(obj);
591 error = got_error_from_errno();
596 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
597 error = got_error(GOT_ERR_OBJ_TYPE);
601 error = got_repo_map_path(&in_repo_path, repo, path);
609 error = print_commits(obj, id, repo, path, show_patch,
610 limit, first_parent_traversal);
616 got_object_close(obj);
619 got_repo_close(repo);
626 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
631 static const struct got_error *
632 cmd_diff(int argc, char *argv[])
634 const struct got_error *error;
635 struct got_repository *repo = NULL;
636 struct got_object_id *id1 = NULL, *id2 = NULL;
637 struct got_object *obj1 = NULL, *obj2 = NULL;
638 char *repo_path = NULL;
639 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
643 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
647 while ((ch = getopt(argc, argv, "")) != -1) {
659 usage_diff(); /* TODO show local worktree changes */
660 } else if (argc == 2) {
661 repo_path = getcwd(NULL, 0);
662 if (repo_path == NULL)
663 return got_error_from_errno();
664 obj_id_str1 = argv[0];
665 obj_id_str2 = argv[1];
666 } else if (argc == 3) {
667 repo_path = realpath(argv[0], NULL);
668 if (repo_path == NULL)
669 return got_error_from_errno();
670 obj_id_str1 = argv[1];
671 obj_id_str2 = argv[2];
675 error = got_repo_open(&repo, repo_path);
680 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
682 id1 = got_object_get_id(obj1);
684 error = got_error_from_errno();
689 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
691 id2 = got_object_get_id(obj2);
693 error = got_error_from_errno();
698 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
699 error = got_error(GOT_ERR_OBJ_TYPE);
703 switch (got_object_get_type(obj1)) {
704 case GOT_OBJ_TYPE_BLOB:
705 error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
707 case GOT_OBJ_TYPE_TREE:
708 error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
710 case GOT_OBJ_TYPE_COMMIT:
711 error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
714 error = got_error(GOT_ERR_OBJ_TYPE);
719 got_object_close(obj1);
721 got_object_close(obj2);
727 got_repo_close(repo);
734 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
739 static const struct got_error *
740 cmd_blame(int argc, char *argv[])
742 const struct got_error *error;
743 struct got_repository *repo = NULL;
744 char *repo_path = NULL;
746 struct got_object_id *commit_id = NULL;
747 char *commit_id_str = NULL;
751 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
755 while ((ch = getopt(argc, argv, "c:")) != -1) {
758 commit_id_str = optarg;
771 } else if (argc == 1) {
772 repo_path = getcwd(NULL, 0);
773 if (repo_path == NULL)
774 return got_error_from_errno();
776 } else if (argc == 2) {
777 repo_path = realpath(argv[0], NULL);
778 if (repo_path == NULL)
779 return got_error_from_errno();
784 error = got_repo_open(&repo, repo_path);
789 if (commit_id_str == NULL) {
790 struct got_reference *head_ref;
791 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
794 error = got_ref_resolve(&commit_id, repo, head_ref);
795 got_ref_close(head_ref);
799 struct got_object *obj;
800 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
803 commit_id = got_object_get_id(obj);
804 if (commit_id == NULL)
805 error = got_error_from_errno();
806 got_object_close(obj);
809 error = got_blame(path, commit_id, repo, stdout);
813 got_repo_close(repo);
818 static const struct got_error *
819 cmd_status(int argc __unused, char *argv[] __unused)
821 git_repository *repo = NULL;
822 git_status_list *status;
823 git_status_options statusopts;
828 if (git_repository_open_ext(&repo, ".", 0, NULL))
829 errx(1, "git_repository_open: %s", giterr_last()->message);
831 if (git_repository_is_bare(repo))
832 errx(1, "bar repository");
834 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
835 errx(1, "git_status_init_options: %s", giterr_last()->message);
837 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
838 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
839 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
840 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
842 if (git_status_list_new(&status, repo, &statusopts))
843 errx(1, "git_status_list_new: %s", giterr_last()->message);
845 for (i = 0; i < git_status_list_entrycount(status); i++) {
846 const git_status_entry *se;
848 se = git_status_byindex(status, i);
849 switch (se->status) {
850 case GIT_STATUS_WT_NEW:
851 printf("? %s\n", se->index_to_workdir->new_file.path);
853 case GIT_STATUS_WT_MODIFIED:
854 printf("M %s\n", se->index_to_workdir->new_file.path);
856 case GIT_STATUS_WT_DELETED:
857 printf("R %s\n", se->index_to_workdir->new_file.path);
859 case GIT_STATUS_WT_RENAMED:
860 printf("m %s -> %s\n",
861 se->index_to_workdir->old_file.path,
862 se->index_to_workdir->new_file.path);
864 case GIT_STATUS_WT_TYPECHANGE:
865 printf("t %s\n", se->index_to_workdir->new_file.path);
867 case GIT_STATUS_INDEX_NEW:
868 printf("A %s\n", se->head_to_index->new_file.path);
870 case GIT_STATUS_INDEX_MODIFIED:
871 printf("M %s\n", se->head_to_index->old_file.path);
873 case GIT_STATUS_INDEX_DELETED:
874 printf("R %s\n", se->head_to_index->old_file.path);
876 case GIT_STATUS_INDEX_RENAMED:
877 printf("m %s -> %s\n",
878 se->head_to_index->old_file.path,
879 se->head_to_index->new_file.path);
881 case GIT_STATUS_INDEX_TYPECHANGE:
882 printf("t %s\n", se->head_to_index->old_file.path);
884 case GIT_STATUS_CURRENT:
890 git_status_list_free(status);
891 git_repository_free(repo);
892 git_libgit2_shutdown();