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"
38 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
43 const struct got_error *(*cmd_main)(int, char *[]);
44 void (*cmd_usage)(void);
45 const char *cmd_descr;
48 __dead static void usage(void);
49 __dead static void usage_checkout(void);
50 __dead static void usage_log(void);
51 __dead static void usage_diff(void);
53 static const struct got_error* cmd_checkout(int, char *[]);
54 static const struct got_error* cmd_log(int, char *[]);
55 static const struct got_error* cmd_diff(int, char *[]);
57 static const struct got_error* cmd_status(int, char *[]);
60 static struct cmd got_commands[] = {
61 { "checkout", cmd_checkout, usage_checkout,
62 "check out a new work tree from a repository" },
63 { "log", cmd_log, usage_log,
64 "show repository history" },
65 { "diff", cmd_diff, usage_diff,
66 "compare files and directories" },
68 { "status", cmd_status, usage_status,
69 "show modification status of files" },
74 main(int argc, char *argv[])
81 setlocale(LC_ALL, "");
83 while ((ch = getopt(argc, argv, "h")) != -1) {
101 for (i = 0; i < nitems(got_commands); i++) {
102 const struct got_error *error;
104 cmd = &got_commands[i];
106 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
110 got_commands[i].cmd_usage();
112 error = got_commands[i].cmd_main(argc, argv);
114 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
121 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
130 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
131 "Available commands:\n", getprogname());
132 for (i = 0; i < nitems(got_commands); i++) {
133 struct cmd *cmd = &got_commands[i];
134 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
142 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
143 "[worktree-path]\n", getprogname());
148 checkout_progress(void *arg, const char *path)
150 char *worktree_path = arg;
152 while (path[0] == '/')
155 printf("A %s/%s\n", worktree_path, path);
158 static const struct got_error *
159 cmd_checkout(int argc, char *argv[])
161 const struct got_error *error = NULL;
162 struct got_repository *repo = NULL;
163 struct got_reference *head_ref = NULL;
164 struct got_worktree *worktree = NULL;
165 char *repo_path = NULL;
166 char *worktree_path = NULL;
167 const char *path_prefix = "";
170 while ((ch = getopt(argc, argv, "p:")) != -1) {
173 path_prefix = optarg;
185 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
189 char *cwd, *base, *dotgit;
190 repo_path = realpath(argv[0], NULL);
191 if (repo_path == NULL)
192 return got_error_from_errno();
193 cwd = getcwd(NULL, 0);
195 error = got_error_from_errno();
199 base = basename(path_prefix);
201 base = basename(repo_path);
203 error = got_error_from_errno();
206 dotgit = strstr(base, ".git");
209 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
210 error = got_error_from_errno();
215 } else if (argc == 2) {
216 repo_path = realpath(argv[0], NULL);
217 if (repo_path == NULL) {
218 error = got_error_from_errno();
221 worktree_path = realpath(argv[1], NULL);
222 if (worktree_path == NULL) {
223 error = got_error_from_errno();
229 error = got_repo_open(&repo, repo_path);
232 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
236 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
240 error = got_worktree_open(&worktree, worktree_path);
244 error = got_worktree_checkout_files(worktree, head_ref, repo,
245 checkout_progress, worktree_path);
249 printf("Checked out %s\n", worktree_path);
250 printf("Now shut up and hack\n");
258 static const struct got_error *
259 print_patch(struct got_commit_object *commit, struct got_object_id *id,
260 struct got_repository *repo)
262 const struct got_error *err = NULL;
263 struct got_tree_object *tree1 = NULL, *tree2;
264 struct got_object *obj;
265 struct got_parent_id *pid;
267 err = got_object_open(&obj, repo, commit->tree_id);
271 err = got_object_tree_open(&tree2, repo, obj);
272 got_object_close(obj);
276 pid = SIMPLEQ_FIRST(&commit->parent_ids);
278 struct got_commit_object *pcommit;
280 err = got_object_open(&obj, repo, pid->id);
284 err = got_object_commit_open(&pcommit, repo, obj);
285 got_object_close(obj);
289 err = got_object_open(&obj, repo, pcommit->tree_id);
290 got_object_commit_close(pcommit);
293 err = got_object_tree_open(&tree1, repo, obj);
294 got_object_close(obj);
299 err = got_diff_tree(tree1, tree2, repo, stdout);
301 got_object_tree_close(tree1);
302 got_object_tree_close(tree2);
306 static const struct got_error *
307 print_commit(struct got_commit_object *commit, struct got_object_id *id,
308 struct got_repository *repo, int show_patch)
310 const struct got_error *err = NULL;
313 err = got_object_id_str(&buf, id);
317 printf("-----------------------------------------------\n");
318 printf("commit %s\n", buf);
319 printf("author: %s\n", commit->author);
320 if (strcmp(commit->author, commit->committer) != 0)
321 printf("committer: %s\n", commit->committer);
322 printf("\n%s\n", commit->logmsg);
325 err = print_patch(commit, id, repo);
334 struct commit_queue_entry {
335 TAILQ_ENTRY(commit_queue_entry) entry;
336 struct got_object_id *id;
337 struct got_commit_object *commit;
340 static const struct got_error *
341 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
342 struct got_repository *repo, int show_patch, int limit)
344 const struct got_error *err;
345 struct got_commit_object *root_commit;
346 TAILQ_HEAD(, commit_queue_entry) commits;
347 struct commit_queue_entry *entry;
349 TAILQ_INIT(&commits);
351 err = got_object_commit_open(&root_commit, repo, root_obj);
355 entry = calloc(1, sizeof(*entry));
357 return got_error_from_errno();
358 entry->id = got_object_id_dup(root_id);
359 if (entry->id == NULL) {
360 err = got_error_from_errno();
364 entry->commit = root_commit;
365 TAILQ_INSERT_HEAD(&commits, entry, entry);
367 while (!TAILQ_EMPTY(&commits)) {
368 struct got_parent_id *pid;
369 struct got_object *obj;
370 struct got_commit_object *pcommit;
371 struct commit_queue_entry *pentry;
373 entry = TAILQ_FIRST(&commits);
375 err = print_commit(entry->commit, entry->id, repo, show_patch);
379 if (limit && --limit == 0)
382 if (entry->commit->nparents == 0)
385 /* Follow the first parent (TODO: handle merge commits). */
386 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
387 err = got_object_open(&obj, repo, pid->id);
390 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
391 err = got_error(GOT_ERR_OBJ_TYPE);
395 err = got_object_commit_open(&pcommit, repo, obj);
396 got_object_close(obj);
400 pentry = calloc(1, sizeof(*pentry));
401 if (pentry == NULL) {
402 err = got_error_from_errno();
403 got_object_commit_close(pcommit);
406 pentry->id = got_object_id_dup(pid->id);
407 if (pentry->id == NULL) {
408 err = got_error_from_errno();
409 got_object_commit_close(pcommit);
412 pentry->commit = pcommit;
413 TAILQ_INSERT_TAIL(&commits, pentry, entry);
415 TAILQ_REMOVE(&commits, entry, entry);
416 got_object_commit_close(entry->commit);
421 while (!TAILQ_EMPTY(&commits)) {
422 entry = TAILQ_FIRST(&commits);
423 TAILQ_REMOVE(&commits, entry, entry);
424 got_object_commit_close(entry->commit);
435 fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
436 "[repository-path]\n", getprogname());
440 static const struct got_error *
441 cmd_log(int argc, char *argv[])
443 const struct got_error *error;
444 struct got_repository *repo;
445 struct got_object_id *id = NULL;
446 struct got_object *obj;
447 char *repo_path = NULL;
448 char *start_commit = NULL;
450 int show_patch = 0, limit = 0;
454 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
458 while ((ch = getopt(argc, argv, "pc:l:")) != -1) {
464 start_commit = optarg;
467 limit = strtonum(optarg, 1, INT_MAX, &errstr);
469 err(1, "-l option %s", errstr);
481 repo_path = getcwd(NULL, 0);
482 if (repo_path == NULL)
483 return got_error_from_errno();
484 } else if (argc == 1) {
485 repo_path = realpath(argv[0], NULL);
486 if (repo_path == NULL)
487 return got_error_from_errno();
491 error = got_repo_open(&repo, repo_path);
496 if (start_commit == NULL) {
497 struct got_reference *head_ref;
498 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
501 error = got_ref_resolve(&id, repo, head_ref);
502 got_ref_close(head_ref);
505 error = got_object_open(&obj, repo, id);
507 error = got_object_open_by_id_str(&obj, repo, start_commit);
509 id = got_object_get_id(obj);
511 error = got_error_from_errno();
516 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
517 error = print_commits(obj, id, repo, show_patch, limit);
519 error = got_error(GOT_ERR_OBJ_TYPE);
520 got_object_close(obj);
522 got_repo_close(repo);
529 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
534 static const struct got_error *
535 cmd_diff(int argc, char *argv[])
537 const struct got_error *error;
538 struct got_repository *repo = NULL;
539 struct got_object_id *id1 = NULL, *id2 = NULL;
540 struct got_object *obj1 = NULL, *obj2 = NULL;
541 char *repo_path = NULL;
542 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
546 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
550 while ((ch = getopt(argc, argv, "")) != -1) {
562 usage_diff(); /* TODO show local worktree changes */
563 } else if (argc == 2) {
564 repo_path = getcwd(NULL, 0);
565 if (repo_path == NULL)
566 return got_error_from_errno();
567 obj_id_str1 = argv[0];
568 obj_id_str2 = argv[1];
569 } else if (argc == 3) {
570 repo_path = realpath(argv[0], NULL);
571 if (repo_path == NULL)
572 return got_error_from_errno();
573 obj_id_str1 = argv[1];
574 obj_id_str2 = argv[2];
578 error = got_repo_open(&repo, repo_path);
583 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
585 id1 = got_object_get_id(obj1);
587 error = got_error_from_errno();
592 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
594 id2 = got_object_get_id(obj2);
596 error = got_error_from_errno();
601 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
602 error = got_error(GOT_ERR_OBJ_TYPE);
606 switch (got_object_get_type(obj1)) {
607 case GOT_OBJ_TYPE_BLOB:
608 error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
610 case GOT_OBJ_TYPE_TREE:
611 error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
613 case GOT_OBJ_TYPE_COMMIT:
614 error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
617 error = got_error(GOT_ERR_OBJ_TYPE);
622 got_object_close(obj1);
624 got_object_close(obj2);
630 got_repo_close(repo);
635 static const struct got_error *
636 cmd_status(int argc __unused, char *argv[] __unused)
638 git_repository *repo = NULL;
639 git_status_list *status;
640 git_status_options statusopts;
645 if (git_repository_open_ext(&repo, ".", 0, NULL))
646 errx(1, "git_repository_open: %s", giterr_last()->message);
648 if (git_repository_is_bare(repo))
649 errx(1, "bar repository");
651 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
652 errx(1, "git_status_init_options: %s", giterr_last()->message);
654 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
655 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
656 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
657 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
659 if (git_status_list_new(&status, repo, &statusopts))
660 errx(1, "git_status_list_new: %s", giterr_last()->message);
662 for (i = 0; i < git_status_list_entrycount(status); i++) {
663 const git_status_entry *se;
665 se = git_status_byindex(status, i);
666 switch (se->status) {
667 case GIT_STATUS_WT_NEW:
668 printf("? %s\n", se->index_to_workdir->new_file.path);
670 case GIT_STATUS_WT_MODIFIED:
671 printf("M %s\n", se->index_to_workdir->new_file.path);
673 case GIT_STATUS_WT_DELETED:
674 printf("R %s\n", se->index_to_workdir->new_file.path);
676 case GIT_STATUS_WT_RENAMED:
677 printf("m %s -> %s\n",
678 se->index_to_workdir->old_file.path,
679 se->index_to_workdir->new_file.path);
681 case GIT_STATUS_WT_TYPECHANGE:
682 printf("t %s\n", se->index_to_workdir->new_file.path);
684 case GIT_STATUS_INDEX_NEW:
685 printf("A %s\n", se->head_to_index->new_file.path);
687 case GIT_STATUS_INDEX_MODIFIED:
688 printf("M %s\n", se->head_to_index->old_file.path);
690 case GIT_STATUS_INDEX_DELETED:
691 printf("R %s\n", se->head_to_index->old_file.path);
693 case GIT_STATUS_INDEX_RENAMED:
694 printf("m %s -> %s\n",
695 se->head_to_index->old_file.path,
696 se->head_to_index->new_file.path);
698 case GIT_STATUS_INDEX_TYPECHANGE:
699 printf("t %s\n", se->head_to_index->old_file.path);
701 case GIT_STATUS_CURRENT:
707 git_status_list_free(status);
708 git_repository_free(repo);
709 git_libgit2_shutdown();