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"
41 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
46 const struct got_error *(*cmd_main)(int, char *[]);
47 void (*cmd_usage)(void);
48 const char *cmd_descr;
51 __dead static void usage(void);
52 __dead static void usage_checkout(void);
53 __dead static void usage_log(void);
54 __dead static void usage_diff(void);
56 static const struct got_error* cmd_checkout(int, char *[]);
57 static const struct got_error* cmd_log(int, char *[]);
58 static const struct got_error* cmd_diff(int, char *[]);
60 static const struct got_error* cmd_status(int, char *[]);
63 static struct cmd got_commands[] = {
64 { "checkout", cmd_checkout, usage_checkout,
65 "check out a new work tree from a repository" },
66 { "log", cmd_log, usage_log,
67 "show repository history" },
68 { "diff", cmd_diff, usage_diff,
69 "compare files and directories" },
71 { "status", cmd_status, usage_status,
72 "show modification status of files" },
77 main(int argc, char *argv[])
84 setlocale(LC_ALL, "");
86 while ((ch = getopt(argc, argv, "h")) != -1) {
104 for (i = 0; i < nitems(got_commands); i++) {
105 const struct got_error *error;
107 cmd = &got_commands[i];
109 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
113 got_commands[i].cmd_usage();
115 error = got_commands[i].cmd_main(argc, argv);
117 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
124 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
133 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
134 "Available commands:\n", getprogname());
135 for (i = 0; i < nitems(got_commands); i++) {
136 struct cmd *cmd = &got_commands[i];
137 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
145 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
146 "[worktree-path]\n", getprogname());
151 checkout_progress(void *arg, const char *path)
153 char *worktree_path = arg;
155 while (path[0] == '/')
158 printf("A %s/%s\n", worktree_path, path);
161 static const struct got_error *
162 cmd_checkout(int argc, char *argv[])
164 const struct got_error *error = NULL;
165 struct got_repository *repo = NULL;
166 struct got_reference *head_ref = NULL;
167 struct got_worktree *worktree = NULL;
168 char *repo_path = NULL;
169 char *worktree_path = NULL;
170 const char *path_prefix = "";
173 while ((ch = getopt(argc, argv, "p:")) != -1) {
176 path_prefix = optarg;
188 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
192 char *cwd, *base, *dotgit;
193 repo_path = realpath(argv[0], NULL);
194 if (repo_path == NULL)
195 return got_error_from_errno();
196 cwd = getcwd(NULL, 0);
198 error = got_error_from_errno();
202 base = basename(path_prefix);
204 base = basename(repo_path);
206 error = got_error_from_errno();
209 dotgit = strstr(base, ".git");
212 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
213 error = got_error_from_errno();
218 } else if (argc == 2) {
219 repo_path = realpath(argv[0], NULL);
220 if (repo_path == NULL) {
221 error = got_error_from_errno();
224 worktree_path = realpath(argv[1], NULL);
225 if (worktree_path == NULL) {
226 error = got_error_from_errno();
232 error = got_repo_open(&repo, repo_path);
235 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
239 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
243 error = got_worktree_open(&worktree, worktree_path);
247 error = got_worktree_checkout_files(worktree, head_ref, repo,
248 checkout_progress, worktree_path);
252 printf("Checked out %s\n", worktree_path);
253 printf("Now shut up and hack\n");
261 static const struct got_error *
262 print_patch(struct got_commit_object *commit, struct got_object_id *id,
263 struct got_repository *repo)
265 const struct got_error *err = NULL;
266 struct got_tree_object *tree1 = NULL, *tree2;
267 struct got_object *obj;
268 struct got_object_qid *qid;
270 err = got_object_open(&obj, repo, commit->tree_id);
274 err = got_object_tree_open(&tree2, repo, obj);
275 got_object_close(obj);
279 qid = SIMPLEQ_FIRST(&commit->parent_ids);
281 struct got_commit_object *pcommit;
283 err = got_object_open(&obj, repo, qid->id);
287 err = got_object_commit_open(&pcommit, repo, obj);
288 got_object_close(obj);
292 err = got_object_open(&obj, repo, pcommit->tree_id);
293 got_object_commit_close(pcommit);
296 err = got_object_tree_open(&tree1, repo, obj);
297 got_object_close(obj);
302 err = got_diff_tree(tree1, tree2, repo, stdout);
304 got_object_tree_close(tree1);
305 got_object_tree_close(tree2);
310 get_datestr(time_t *time, char *datebuf)
312 char *p, *s = ctime_r(time, datebuf);
319 static const struct got_error *
320 print_commit(struct got_commit_object *commit, struct got_object_id *id,
321 struct got_repository *repo, int show_patch)
323 const struct got_error *err = NULL;
324 char *id_str, *datestr, *logmsg, *line;
326 time_t author_time, committer_time;
328 err = got_object_id_str(&id_str, id);
332 author_time = mktime(&commit->tm_author);
333 committer_time = mktime(&commit->tm_committer);
335 /* This would express the date in committer's timezone. */
336 author_time += commit->tm_author.tm_gmtoff;
337 committer_time += commit->tm_committer.tm_gmtoff;
340 printf("-----------------------------------------------\n");
341 printf("commit %s\n", id_str);
343 datestr = get_datestr(&author_time, datebuf);
344 printf("author: %s %s UTC\n", commit->author, datestr);
345 if (strcmp(commit->author, commit->committer) != 0 ||
346 author_time != committer_time) {
347 datestr = get_datestr(&committer_time, datebuf);
348 printf("committer: %s %s UTC\n", commit->committer, datestr);
350 if (commit->nparents > 1) {
351 struct got_object_qid *qid;
353 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
354 err = got_object_id_str(&id_str, qid->id);
357 printf("parent %d: %s\n", n++, id_str);
362 logmsg = strdup(commit->logmsg);
364 return got_error_from_errno();
367 line = strsep(&logmsg, "\n");
369 printf(" %s\n", line);
374 err = print_patch(commit, id, repo);
382 static const struct got_error *
383 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
384 struct got_repository *repo, int show_patch, int limit,
385 int first_parent_traversal)
387 const struct got_error *err;
388 struct got_commit_graph *graph;
391 err = got_commit_graph_open(&graph, root_id, first_parent_traversal,
395 err = got_commit_graph_iter_start(graph, root_id);
399 struct got_commit_object *commit;
400 struct got_object_id *id;
402 err = got_commit_graph_iter_next(&id, graph);
404 if (err->code == GOT_ERR_ITER_COMPLETED) {
408 if (err->code != GOT_ERR_ITER_NEED_MORE)
410 err = got_commit_graph_fetch_commits(&ncommits,
420 err = got_object_open_as_commit(&commit, repo, id);
423 err = print_commit(commit, id, repo, show_patch);
424 got_object_commit_close(commit);
425 if (err || (limit && --limit == 0))
427 } while (ncommits > 0);
429 got_commit_graph_close(graph);
436 fprintf(stderr, "usage: %s log [-c commit] [-f] [ -l N ] [-p] "
437 "[repository-path]\n", getprogname());
441 static const struct got_error *
442 cmd_log(int argc, char *argv[])
444 const struct got_error *error;
445 struct got_repository *repo;
446 struct got_object_id *id = NULL;
447 struct got_object *obj = NULL;
448 char *repo_path = NULL;
449 char *start_commit = NULL;
451 int show_patch = 0, limit = 0, first_parent_traversal = 0;
455 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
459 while ((ch = getopt(argc, argv, "pc:l:f")) != -1) {
465 start_commit = optarg;
468 limit = strtonum(optarg, 1, INT_MAX, &errstr);
470 err(1, "-l option %s", errstr);
473 first_parent_traversal = 1;
485 repo_path = getcwd(NULL, 0);
486 if (repo_path == NULL)
487 return got_error_from_errno();
488 } else if (argc == 1) {
489 repo_path = realpath(argv[0], NULL);
490 if (repo_path == NULL)
491 return got_error_from_errno();
495 error = got_repo_open(&repo, repo_path);
500 if (start_commit == NULL) {
501 struct got_reference *head_ref;
502 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
505 error = got_ref_resolve(&id, repo, head_ref);
506 got_ref_close(head_ref);
509 error = got_object_open(&obj, repo, id);
511 struct got_reference *ref;
512 error = got_ref_open(&ref, repo, start_commit);
514 error = got_ref_resolve(&id, repo, ref);
518 error = got_object_open(&obj, repo, id);
523 error = got_object_open_by_id_str(&obj, repo,
527 id = got_object_get_id(obj);
529 error = got_error_from_errno();
534 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
535 error = print_commits(obj, id, repo, show_patch, limit,
536 first_parent_traversal);
538 error = got_error(GOT_ERR_OBJ_TYPE);
539 got_object_close(obj);
541 got_repo_close(repo);
548 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
553 static const struct got_error *
554 cmd_diff(int argc, char *argv[])
556 const struct got_error *error;
557 struct got_repository *repo = NULL;
558 struct got_object_id *id1 = NULL, *id2 = NULL;
559 struct got_object *obj1 = NULL, *obj2 = NULL;
560 char *repo_path = NULL;
561 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
565 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
569 while ((ch = getopt(argc, argv, "")) != -1) {
581 usage_diff(); /* TODO show local worktree changes */
582 } else if (argc == 2) {
583 repo_path = getcwd(NULL, 0);
584 if (repo_path == NULL)
585 return got_error_from_errno();
586 obj_id_str1 = argv[0];
587 obj_id_str2 = argv[1];
588 } else if (argc == 3) {
589 repo_path = realpath(argv[0], NULL);
590 if (repo_path == NULL)
591 return got_error_from_errno();
592 obj_id_str1 = argv[1];
593 obj_id_str2 = argv[2];
597 error = got_repo_open(&repo, repo_path);
602 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
604 id1 = got_object_get_id(obj1);
606 error = got_error_from_errno();
611 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
613 id2 = got_object_get_id(obj2);
615 error = got_error_from_errno();
620 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
621 error = got_error(GOT_ERR_OBJ_TYPE);
625 switch (got_object_get_type(obj1)) {
626 case GOT_OBJ_TYPE_BLOB:
627 error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
629 case GOT_OBJ_TYPE_TREE:
630 error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
632 case GOT_OBJ_TYPE_COMMIT:
633 error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
636 error = got_error(GOT_ERR_OBJ_TYPE);
641 got_object_close(obj1);
643 got_object_close(obj2);
649 got_repo_close(repo);
654 static const struct got_error *
655 cmd_status(int argc __unused, char *argv[] __unused)
657 git_repository *repo = NULL;
658 git_status_list *status;
659 git_status_options statusopts;
664 if (git_repository_open_ext(&repo, ".", 0, NULL))
665 errx(1, "git_repository_open: %s", giterr_last()->message);
667 if (git_repository_is_bare(repo))
668 errx(1, "bar repository");
670 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
671 errx(1, "git_status_init_options: %s", giterr_last()->message);
673 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
674 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
675 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
676 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
678 if (git_status_list_new(&status, repo, &statusopts))
679 errx(1, "git_status_list_new: %s", giterr_last()->message);
681 for (i = 0; i < git_status_list_entrycount(status); i++) {
682 const git_status_entry *se;
684 se = git_status_byindex(status, i);
685 switch (se->status) {
686 case GIT_STATUS_WT_NEW:
687 printf("? %s\n", se->index_to_workdir->new_file.path);
689 case GIT_STATUS_WT_MODIFIED:
690 printf("M %s\n", se->index_to_workdir->new_file.path);
692 case GIT_STATUS_WT_DELETED:
693 printf("R %s\n", se->index_to_workdir->new_file.path);
695 case GIT_STATUS_WT_RENAMED:
696 printf("m %s -> %s\n",
697 se->index_to_workdir->old_file.path,
698 se->index_to_workdir->new_file.path);
700 case GIT_STATUS_WT_TYPECHANGE:
701 printf("t %s\n", se->index_to_workdir->new_file.path);
703 case GIT_STATUS_INDEX_NEW:
704 printf("A %s\n", se->head_to_index->new_file.path);
706 case GIT_STATUS_INDEX_MODIFIED:
707 printf("M %s\n", se->head_to_index->old_file.path);
709 case GIT_STATUS_INDEX_DELETED:
710 printf("R %s\n", se->head_to_index->old_file.path);
712 case GIT_STATUS_INDEX_RENAMED:
713 printf("m %s -> %s\n",
714 se->head_to_index->old_file.path,
715 se->head_to_index->new_file.path);
717 case GIT_STATUS_INDEX_TYPECHANGE:
718 printf("t %s\n", se->head_to_index->old_file.path);
720 case GIT_STATUS_CURRENT:
726 git_status_list_free(status);
727 git_repository_free(repo);
728 git_libgit2_shutdown();