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)
311 const struct got_error *err = NULL;
314 err = got_object_id_str(&buf, id);
318 printf("-----------------------------------------------\n");
319 printf("commit %s\n", buf);
320 printf("author: %s\n", commit->author);
321 if (strcmp(commit->author, commit->committer) != 0)
322 printf("committer: %s\n", commit->committer);
323 printf("\n%s\n", commit->logmsg);
326 err = print_patch(commit, id, repo);
335 static const struct got_error *
336 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
337 struct got_repository *repo, int show_patch, int limit)
339 const struct got_error *err;
340 struct got_commit_graph *graph;
343 err = got_commit_graph_open(&graph, root_id, repo);
346 err = got_commit_graph_iter_start(graph, root_id);
350 struct got_commit_object *commit;
351 struct got_object_id *id;
353 err = got_commit_graph_iter_next(&commit, &id, graph);
355 if (err->code != GOT_ERR_ITER_NEED_MORE)
357 err = got_commit_graph_fetch_commits(&ncommits,
366 err = print_commit(commit, id, repo, show_patch);
367 if (err || (limit && --limit == 0))
369 } while (ncommits > 0);
371 got_commit_graph_close(graph);
378 fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
379 "[repository-path]\n", getprogname());
383 static const struct got_error *
384 cmd_log(int argc, char *argv[])
386 const struct got_error *error;
387 struct got_repository *repo;
388 struct got_object_id *id = NULL;
389 struct got_object *obj;
390 char *repo_path = NULL;
391 char *start_commit = NULL;
393 int show_patch = 0, limit = 0;
397 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
401 while ((ch = getopt(argc, argv, "pc:l:")) != -1) {
407 start_commit = optarg;
410 limit = strtonum(optarg, 1, INT_MAX, &errstr);
412 err(1, "-l option %s", errstr);
424 repo_path = getcwd(NULL, 0);
425 if (repo_path == NULL)
426 return got_error_from_errno();
427 } else if (argc == 1) {
428 repo_path = realpath(argv[0], NULL);
429 if (repo_path == NULL)
430 return got_error_from_errno();
434 error = got_repo_open(&repo, repo_path);
439 if (start_commit == NULL) {
440 struct got_reference *head_ref;
441 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
444 error = got_ref_resolve(&id, repo, head_ref);
445 got_ref_close(head_ref);
448 error = got_object_open(&obj, repo, id);
450 error = got_object_open_by_id_str(&obj, repo, start_commit);
452 id = got_object_get_id(obj);
454 error = got_error_from_errno();
459 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
460 error = print_commits(obj, id, repo, show_patch, limit);
462 error = got_error(GOT_ERR_OBJ_TYPE);
463 got_object_close(obj);
465 got_repo_close(repo);
472 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
477 static const struct got_error *
478 cmd_diff(int argc, char *argv[])
480 const struct got_error *error;
481 struct got_repository *repo = NULL;
482 struct got_object_id *id1 = NULL, *id2 = NULL;
483 struct got_object *obj1 = NULL, *obj2 = NULL;
484 char *repo_path = NULL;
485 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
489 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
493 while ((ch = getopt(argc, argv, "")) != -1) {
505 usage_diff(); /* TODO show local worktree changes */
506 } else if (argc == 2) {
507 repo_path = getcwd(NULL, 0);
508 if (repo_path == NULL)
509 return got_error_from_errno();
510 obj_id_str1 = argv[0];
511 obj_id_str2 = argv[1];
512 } else if (argc == 3) {
513 repo_path = realpath(argv[0], NULL);
514 if (repo_path == NULL)
515 return got_error_from_errno();
516 obj_id_str1 = argv[1];
517 obj_id_str2 = argv[2];
521 error = got_repo_open(&repo, repo_path);
526 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
528 id1 = got_object_get_id(obj1);
530 error = got_error_from_errno();
535 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
537 id2 = got_object_get_id(obj2);
539 error = got_error_from_errno();
544 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
545 error = got_error(GOT_ERR_OBJ_TYPE);
549 switch (got_object_get_type(obj1)) {
550 case GOT_OBJ_TYPE_BLOB:
551 error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
553 case GOT_OBJ_TYPE_TREE:
554 error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
556 case GOT_OBJ_TYPE_COMMIT:
557 error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
560 error = got_error(GOT_ERR_OBJ_TYPE);
565 got_object_close(obj1);
567 got_object_close(obj2);
573 got_repo_close(repo);
578 static const struct got_error *
579 cmd_status(int argc __unused, char *argv[] __unused)
581 git_repository *repo = NULL;
582 git_status_list *status;
583 git_status_options statusopts;
588 if (git_repository_open_ext(&repo, ".", 0, NULL))
589 errx(1, "git_repository_open: %s", giterr_last()->message);
591 if (git_repository_is_bare(repo))
592 errx(1, "bar repository");
594 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
595 errx(1, "git_status_init_options: %s", giterr_last()->message);
597 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
598 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
599 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
600 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
602 if (git_status_list_new(&status, repo, &statusopts))
603 errx(1, "git_status_list_new: %s", giterr_last()->message);
605 for (i = 0; i < git_status_list_entrycount(status); i++) {
606 const git_status_entry *se;
608 se = git_status_byindex(status, i);
609 switch (se->status) {
610 case GIT_STATUS_WT_NEW:
611 printf("? %s\n", se->index_to_workdir->new_file.path);
613 case GIT_STATUS_WT_MODIFIED:
614 printf("M %s\n", se->index_to_workdir->new_file.path);
616 case GIT_STATUS_WT_DELETED:
617 printf("R %s\n", se->index_to_workdir->new_file.path);
619 case GIT_STATUS_WT_RENAMED:
620 printf("m %s -> %s\n",
621 se->index_to_workdir->old_file.path,
622 se->index_to_workdir->new_file.path);
624 case GIT_STATUS_WT_TYPECHANGE:
625 printf("t %s\n", se->index_to_workdir->new_file.path);
627 case GIT_STATUS_INDEX_NEW:
628 printf("A %s\n", se->head_to_index->new_file.path);
630 case GIT_STATUS_INDEX_MODIFIED:
631 printf("M %s\n", se->head_to_index->old_file.path);
633 case GIT_STATUS_INDEX_DELETED:
634 printf("R %s\n", se->head_to_index->old_file.path);
636 case GIT_STATUS_INDEX_RENAMED:
637 printf("m %s -> %s\n",
638 se->head_to_index->old_file.path,
639 se->head_to_index->new_file.path);
641 case GIT_STATUS_INDEX_TYPECHANGE:
642 printf("t %s\n", se->head_to_index->old_file.path);
644 case GIT_STATUS_CURRENT:
650 git_status_list_free(status);
651 git_repository_free(repo);
652 git_libgit2_shutdown();