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>
30 #include "got_error.h"
31 #include "got_object.h"
32 #include "got_reference.h"
33 #include "got_repository.h"
34 #include "got_worktree.h"
36 #include "got_commit_graph.h"
39 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
44 const struct got_error *(*cmd_main)(int, char *[]);
45 void (*cmd_usage)(void);
46 const char *cmd_descr;
49 __dead static void usage(void);
50 __dead static void usage_checkout(void);
51 __dead static void usage_log(void);
52 __dead static void usage_diff(void);
54 static const struct got_error* cmd_checkout(int, char *[]);
55 static const struct got_error* cmd_log(int, char *[]);
56 static const struct got_error* cmd_diff(int, char *[]);
58 static const struct got_error* cmd_status(int, char *[]);
61 static struct cmd got_commands[] = {
62 { "checkout", cmd_checkout, usage_checkout,
63 "check out a new work tree from a repository" },
64 { "log", cmd_log, usage_log,
65 "show repository history" },
66 { "diff", cmd_diff, usage_diff,
67 "compare files and directories" },
69 { "status", cmd_status, usage_status,
70 "show modification status of files" },
75 main(int argc, char *argv[])
82 setlocale(LC_ALL, "");
84 while ((ch = getopt(argc, argv, "h")) != -1) {
102 for (i = 0; i < nitems(got_commands); i++) {
103 const struct got_error *error;
105 cmd = &got_commands[i];
107 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
111 got_commands[i].cmd_usage();
113 error = got_commands[i].cmd_main(argc, argv);
115 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
122 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
131 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
132 "Available commands:\n", getprogname());
133 for (i = 0; i < nitems(got_commands); i++) {
134 struct cmd *cmd = &got_commands[i];
135 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
143 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
144 "[worktree-path]\n", getprogname());
149 checkout_progress(void *arg, const char *path)
151 char *worktree_path = arg;
153 while (path[0] == '/')
156 printf("A %s/%s\n", worktree_path, path);
159 static const struct got_error *
160 cmd_checkout(int argc, char *argv[])
162 const struct got_error *error = NULL;
163 struct got_repository *repo = NULL;
164 struct got_reference *head_ref = NULL;
165 struct got_worktree *worktree = NULL;
166 char *repo_path = NULL;
167 char *worktree_path = NULL;
168 const char *path_prefix = "";
171 while ((ch = getopt(argc, argv, "p:")) != -1) {
174 path_prefix = optarg;
186 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
190 char *cwd, *base, *dotgit;
191 repo_path = realpath(argv[0], NULL);
192 if (repo_path == NULL)
193 return got_error_from_errno();
194 cwd = getcwd(NULL, 0);
196 error = got_error_from_errno();
200 base = basename(path_prefix);
202 base = basename(repo_path);
204 error = got_error_from_errno();
207 dotgit = strstr(base, ".git");
210 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
211 error = got_error_from_errno();
216 } else if (argc == 2) {
217 repo_path = realpath(argv[0], NULL);
218 if (repo_path == NULL) {
219 error = got_error_from_errno();
222 worktree_path = realpath(argv[1], NULL);
223 if (worktree_path == NULL) {
224 error = got_error_from_errno();
230 error = got_repo_open(&repo, repo_path);
233 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
237 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
241 error = got_worktree_open(&worktree, worktree_path);
245 error = got_worktree_checkout_files(worktree, head_ref, repo,
246 checkout_progress, worktree_path);
250 printf("Checked out %s\n", worktree_path);
251 printf("Now shut up and hack\n");
259 static const struct got_error *
260 print_patch(struct got_commit_object *commit, struct got_object_id *id,
261 struct got_repository *repo)
263 const struct got_error *err = NULL;
264 struct got_tree_object *tree1 = NULL, *tree2;
265 struct got_object *obj;
266 struct got_parent_id *pid;
268 err = got_object_open(&obj, repo, commit->tree_id);
272 err = got_object_tree_open(&tree2, repo, obj);
273 got_object_close(obj);
277 pid = SIMPLEQ_FIRST(&commit->parent_ids);
279 struct got_commit_object *pcommit;
281 err = got_object_open(&obj, repo, pid->id);
285 err = got_object_commit_open(&pcommit, repo, obj);
286 got_object_close(obj);
290 err = got_object_open(&obj, repo, pcommit->tree_id);
291 got_object_commit_close(pcommit);
294 err = got_object_tree_open(&tree1, repo, obj);
295 got_object_close(obj);
300 err = got_diff_tree(tree1, tree2, repo, stdout);
302 got_object_tree_close(tree1);
303 got_object_tree_close(tree2);
307 static const struct got_error *
308 print_commit(struct got_commit_object *commit, struct got_object_id *id,
309 struct got_repository *repo, int show_patch, int verbose)
311 const struct got_error *err = NULL;
312 char *id_str, *logmsg, *line;
314 err = got_object_id_str(&id_str, id);
318 printf("-----------------------------------------------\n");
319 printf("commit %s\n", id_str);
321 printf("author: %s\n", commit->author);
322 if (strcmp(commit->author, commit->committer) != 0)
323 printf("committer: %s\n", commit->committer);
325 struct got_parent_id *pid;
326 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
327 err = got_object_id_str(&id_str, pid->id);
330 printf("parent commit: %s\n", id_str);
335 logmsg = strdup(commit->logmsg);
337 return got_error_from_errno();
340 line = strsep(&logmsg, "\n");
342 printf(" %s\n", line);
347 err = print_patch(commit, id, repo);
355 static const struct got_error *
356 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
357 struct got_repository *repo, int show_patch, int limit, int verbose)
359 const struct got_error *err;
360 struct got_commit_graph *graph;
363 err = got_commit_graph_open(&graph, root_id, repo);
366 err = got_commit_graph_iter_start(graph, root_id);
370 struct got_commit_object *commit;
371 struct got_object_id *id;
373 err = got_commit_graph_iter_next(&commit, &id, graph);
375 if (err->code != GOT_ERR_ITER_NEED_MORE)
377 err = got_commit_graph_fetch_commits(&ncommits,
386 err = print_commit(commit, id, repo, show_patch, verbose);
387 if (err || (limit && --limit == 0))
389 } while (ncommits > 0);
391 got_commit_graph_close(graph);
398 fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
399 "[repository-path]\n", getprogname());
403 static const struct got_error *
404 cmd_log(int argc, char *argv[])
406 const struct got_error *error;
407 struct got_repository *repo;
408 struct got_object_id *id = NULL;
409 struct got_object *obj;
410 char *repo_path = NULL;
411 char *start_commit = NULL;
413 int show_patch = 0, limit = 0, verbose = 0;
417 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
421 while ((ch = getopt(argc, argv, "pc:l:v")) != -1) {
427 start_commit = optarg;
430 limit = strtonum(optarg, 1, INT_MAX, &errstr);
432 err(1, "-l option %s", errstr);
447 repo_path = getcwd(NULL, 0);
448 if (repo_path == NULL)
449 return got_error_from_errno();
450 } else if (argc == 1) {
451 repo_path = realpath(argv[0], NULL);
452 if (repo_path == NULL)
453 return got_error_from_errno();
457 error = got_repo_open(&repo, repo_path);
462 if (start_commit == NULL) {
463 struct got_reference *head_ref;
464 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
467 error = got_ref_resolve(&id, repo, head_ref);
468 got_ref_close(head_ref);
471 error = got_object_open(&obj, repo, id);
473 error = got_object_open_by_id_str(&obj, repo, start_commit);
475 id = got_object_get_id(obj);
477 error = got_error_from_errno();
482 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
483 error = print_commits(obj, id, repo, show_patch, limit,
486 error = got_error(GOT_ERR_OBJ_TYPE);
487 got_object_close(obj);
489 got_repo_close(repo);
496 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
501 static const struct got_error *
502 cmd_diff(int argc, char *argv[])
504 const struct got_error *error;
505 struct got_repository *repo = NULL;
506 struct got_object_id *id1 = NULL, *id2 = NULL;
507 struct got_object *obj1 = NULL, *obj2 = NULL;
508 char *repo_path = NULL;
509 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
513 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
517 while ((ch = getopt(argc, argv, "")) != -1) {
529 usage_diff(); /* TODO show local worktree changes */
530 } else if (argc == 2) {
531 repo_path = getcwd(NULL, 0);
532 if (repo_path == NULL)
533 return got_error_from_errno();
534 obj_id_str1 = argv[0];
535 obj_id_str2 = argv[1];
536 } else if (argc == 3) {
537 repo_path = realpath(argv[0], NULL);
538 if (repo_path == NULL)
539 return got_error_from_errno();
540 obj_id_str1 = argv[1];
541 obj_id_str2 = argv[2];
545 error = got_repo_open(&repo, repo_path);
550 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
552 id1 = got_object_get_id(obj1);
554 error = got_error_from_errno();
559 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
561 id2 = got_object_get_id(obj2);
563 error = got_error_from_errno();
568 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
569 error = got_error(GOT_ERR_OBJ_TYPE);
573 switch (got_object_get_type(obj1)) {
574 case GOT_OBJ_TYPE_BLOB:
575 error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
577 case GOT_OBJ_TYPE_TREE:
578 error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
580 case GOT_OBJ_TYPE_COMMIT:
581 error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
584 error = got_error(GOT_ERR_OBJ_TYPE);
589 got_object_close(obj1);
591 got_object_close(obj2);
597 got_repo_close(repo);
602 static const struct got_error *
603 cmd_status(int argc __unused, char *argv[] __unused)
605 git_repository *repo = NULL;
606 git_status_list *status;
607 git_status_options statusopts;
612 if (git_repository_open_ext(&repo, ".", 0, NULL))
613 errx(1, "git_repository_open: %s", giterr_last()->message);
615 if (git_repository_is_bare(repo))
616 errx(1, "bar repository");
618 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
619 errx(1, "git_status_init_options: %s", giterr_last()->message);
621 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
622 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
623 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
624 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
626 if (git_status_list_new(&status, repo, &statusopts))
627 errx(1, "git_status_list_new: %s", giterr_last()->message);
629 for (i = 0; i < git_status_list_entrycount(status); i++) {
630 const git_status_entry *se;
632 se = git_status_byindex(status, i);
633 switch (se->status) {
634 case GIT_STATUS_WT_NEW:
635 printf("? %s\n", se->index_to_workdir->new_file.path);
637 case GIT_STATUS_WT_MODIFIED:
638 printf("M %s\n", se->index_to_workdir->new_file.path);
640 case GIT_STATUS_WT_DELETED:
641 printf("R %s\n", se->index_to_workdir->new_file.path);
643 case GIT_STATUS_WT_RENAMED:
644 printf("m %s -> %s\n",
645 se->index_to_workdir->old_file.path,
646 se->index_to_workdir->new_file.path);
648 case GIT_STATUS_WT_TYPECHANGE:
649 printf("t %s\n", se->index_to_workdir->new_file.path);
651 case GIT_STATUS_INDEX_NEW:
652 printf("A %s\n", se->head_to_index->new_file.path);
654 case GIT_STATUS_INDEX_MODIFIED:
655 printf("M %s\n", se->head_to_index->old_file.path);
657 case GIT_STATUS_INDEX_DELETED:
658 printf("R %s\n", se->head_to_index->old_file.path);
660 case GIT_STATUS_INDEX_RENAMED:
661 printf("m %s -> %s\n",
662 se->head_to_index->old_file.path,
663 se->head_to_index->new_file.path);
665 case GIT_STATUS_INDEX_TYPECHANGE:
666 printf("t %s\n", se->head_to_index->old_file.path);
668 case GIT_STATUS_CURRENT:
674 git_status_list_free(status);
675 git_repository_free(repo);
676 git_libgit2_shutdown();