Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 *
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.
8 *
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.
16 */
18 #include <sys/queue.h>
19 #include <sys/limits.h>
21 #include <err.h>
22 #include <errno.h>
23 #include <locale.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <libgen.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"
35 #include "got_diff.h"
37 #ifndef nitems
38 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
39 #endif
41 struct cmd {
42 const char *cmd_name;
43 const struct got_error *(*cmd_main)(int, char *[]);
44 void (*cmd_usage)(void);
45 const char *cmd_descr;
46 };
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 *[]);
56 #ifdef notyet
57 static const struct got_error* cmd_status(int, char *[]);
58 #endif
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" },
67 #ifdef notyet
68 { "status", cmd_status, usage_status,
69 "show modification status of files" },
70 #endif
71 };
73 int
74 main(int argc, char *argv[])
75 {
76 struct cmd *cmd;
77 unsigned int i;
78 int ch;
79 int hflag = 0;
81 setlocale(LC_ALL, "");
83 while ((ch = getopt(argc, argv, "h")) != -1) {
84 switch (ch) {
85 case 'h':
86 hflag = 1;
87 break;
88 default:
89 usage();
90 /* NOTREACHED */
91 }
92 }
94 argc -= optind;
95 argv += optind;
96 optind = 0;
98 if (argc <= 0)
99 usage();
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])))
107 continue;
109 if (hflag)
110 got_commands[i].cmd_usage();
112 error = got_commands[i].cmd_main(argc, argv);
113 if (error) {
114 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
115 return 1;
118 return 0;
121 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
122 return 1;
125 __dead static void
126 usage(void)
128 int i;
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);
136 exit(1);
139 __dead static void
140 usage_checkout(void)
142 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
143 "[worktree-path]\n", getprogname());
144 exit(1);
147 static void
148 checkout_progress(void *arg, const char *path)
150 char *worktree_path = arg;
152 while (path[0] == '/')
153 path++;
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 = "";
168 int ch;
170 while ((ch = getopt(argc, argv, "p:")) != -1) {
171 switch (ch) {
172 case 'p':
173 path_prefix = optarg;
174 break;
175 default:
176 usage();
177 /* NOTREACHED */
181 argc -= optind;
182 argv += optind;
184 #ifndef PROFILE
185 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
186 err(1, "pledge");
187 #endif
188 if (argc == 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);
194 if (cwd == NULL) {
195 error = got_error_from_errno();
196 goto done;
198 if (path_prefix[0])
199 base = basename(path_prefix);
200 else
201 base = basename(repo_path);
202 if (base == NULL) {
203 error = got_error_from_errno();
204 goto done;
206 dotgit = strstr(base, ".git");
207 if (dotgit)
208 *dotgit = '\0';
209 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
210 error = got_error_from_errno();
211 free(cwd);
212 goto done;
214 free(cwd);
215 } else if (argc == 2) {
216 repo_path = realpath(argv[0], NULL);
217 if (repo_path == NULL) {
218 error = got_error_from_errno();
219 goto done;
221 worktree_path = realpath(argv[1], NULL);
222 if (worktree_path == NULL) {
223 error = got_error_from_errno();
224 goto done;
226 } else
227 usage_checkout();
229 error = got_repo_open(&repo, repo_path);
230 if (error != NULL)
231 goto done;
232 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
233 if (error != NULL)
234 goto done;
236 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
237 if (error != NULL)
238 goto done;
240 error = got_worktree_open(&worktree, worktree_path);
241 if (error != NULL)
242 goto done;
244 error = got_worktree_checkout_files(worktree, head_ref, repo,
245 checkout_progress, worktree_path);
246 if (error != NULL)
247 goto done;
249 printf("Checked out %s\n", worktree_path);
250 printf("Now shut up and hack\n");
252 done:
253 free(repo_path);
254 free(worktree_path);
255 return error;
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);
268 if (err)
269 return err;
271 err = got_object_tree_open(&tree2, repo, obj);
272 got_object_close(obj);
273 if (err)
274 return err;
276 pid = SIMPLEQ_FIRST(&commit->parent_ids);
277 if (pid != NULL) {
278 struct got_commit_object *pcommit;
280 err = got_object_open(&obj, repo, pid->id);
281 if (err)
282 return err;
284 err = got_object_commit_open(&pcommit, repo, obj);
285 got_object_close(obj);
286 if (err)
287 return err;
289 err = got_object_open(&obj, repo, pcommit->tree_id);
290 got_object_commit_close(pcommit);
291 if (err)
292 return err;
293 err = got_object_tree_open(&tree1, repo, obj);
294 got_object_close(obj);
295 if (err)
296 return err;
299 err = got_diff_tree(tree1, tree2, repo, stdout);
300 if (tree1)
301 got_object_tree_close(tree1);
302 got_object_tree_close(tree2);
303 return err;
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;
311 char *buf;
313 err = got_object_id_str(&buf, id);
314 if (err)
315 return err;
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);
324 if (show_patch) {
325 err = print_patch(commit, id, repo);
326 if (err == 0)
327 printf("\n");
330 free(buf);
331 return err;
334 struct commit_queue_entry {
335 TAILQ_ENTRY(commit_queue_entry) entry;
336 struct got_object_id *id;
337 struct got_commit_object *commit;
338 };
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);
352 if (err)
353 return err;
355 entry = calloc(1, sizeof(*entry));
356 if (entry == NULL)
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();
361 free(entry);
362 return err;
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);
376 if (err)
377 break;
379 if (limit && --limit == 0)
380 break;
382 if (entry->commit->nparents == 0)
383 break;
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);
388 if (err)
389 break;
390 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
391 err = got_error(GOT_ERR_OBJ_TYPE);
392 break;
395 err = got_object_commit_open(&pcommit, repo, obj);
396 got_object_close(obj);
397 if (err)
398 break;
400 pentry = calloc(1, sizeof(*pentry));
401 if (pentry == NULL) {
402 err = got_error_from_errno();
403 got_object_commit_close(pcommit);
404 break;
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);
410 break;
412 pentry->commit = pcommit;
413 TAILQ_INSERT_TAIL(&commits, pentry, entry);
415 TAILQ_REMOVE(&commits, entry, entry);
416 got_object_commit_close(entry->commit);
417 free(entry->id);
418 free(entry);
421 while (!TAILQ_EMPTY(&commits)) {
422 entry = TAILQ_FIRST(&commits);
423 TAILQ_REMOVE(&commits, entry, entry);
424 got_object_commit_close(entry->commit);
425 free(entry->id);
426 free(entry);
429 return err;
432 __dead static void
433 usage_log(void)
435 fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
436 "[repository-path]\n", getprogname());
437 exit(1);
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;
449 int ch;
450 int show_patch = 0, limit = 0;
451 const char *errstr;
453 #ifndef PROFILE
454 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
455 err(1, "pledge");
456 #endif
458 while ((ch = getopt(argc, argv, "pc:l:")) != -1) {
459 switch (ch) {
460 case 'p':
461 show_patch = 1;
462 break;
463 case 'c':
464 start_commit = optarg;
465 break;
466 case 'l':
467 limit = strtonum(optarg, 1, INT_MAX, &errstr);
468 if (errstr != NULL)
469 err(1, "-l option %s", errstr);
470 break;
471 default:
472 usage();
473 /* NOTREACHED */
477 argc -= optind;
478 argv += optind;
480 if (argc == 0) {
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();
488 } else
489 usage_log();
491 error = got_repo_open(&repo, repo_path);
492 free(repo_path);
493 if (error != NULL)
494 return error;
496 if (start_commit == NULL) {
497 struct got_reference *head_ref;
498 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
499 if (error != NULL)
500 return error;
501 error = got_ref_resolve(&id, repo, head_ref);
502 got_ref_close(head_ref);
503 if (error != NULL)
504 return error;
505 error = got_object_open(&obj, repo, id);
506 } else {
507 error = got_object_open_by_id_str(&obj, repo, start_commit);
508 if (error == NULL) {
509 id = got_object_get_id(obj);
510 if (id == NULL)
511 error = got_error_from_errno();
514 if (error != NULL)
515 return error;
516 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
517 error = print_commits(obj, id, repo, show_patch, limit);
518 else
519 error = got_error(GOT_ERR_OBJ_TYPE);
520 got_object_close(obj);
521 free(id);
522 got_repo_close(repo);
523 return error;
526 __dead static void
527 usage_diff(void)
529 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
530 getprogname());
531 exit(1);
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;
543 int ch;
545 #ifndef PROFILE
546 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
547 err(1, "pledge");
548 #endif
550 while ((ch = getopt(argc, argv, "")) != -1) {
551 switch (ch) {
552 default:
553 usage();
554 /* NOTREACHED */
558 argc -= optind;
559 argv += optind;
561 if (argc == 0) {
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];
575 } else
576 usage_diff();
578 error = got_repo_open(&repo, repo_path);
579 free(repo_path);
580 if (error != NULL)
581 goto done;
583 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
584 if (error == NULL) {
585 id1 = got_object_get_id(obj1);
586 if (id1 == NULL)
587 error = got_error_from_errno();
589 if (error != NULL)
590 goto done;
592 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
593 if (error == NULL) {
594 id2 = got_object_get_id(obj2);
595 if (id2 == NULL)
596 error = got_error_from_errno();
598 if (error != NULL)
599 goto done;
601 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
602 error = got_error(GOT_ERR_OBJ_TYPE);
603 goto done;
606 switch (got_object_get_type(obj1)) {
607 case GOT_OBJ_TYPE_BLOB:
608 error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
609 break;
610 case GOT_OBJ_TYPE_TREE:
611 error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
612 break;
613 case GOT_OBJ_TYPE_COMMIT:
614 error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
615 break;
616 default:
617 error = got_error(GOT_ERR_OBJ_TYPE);
620 done:
621 if (obj1)
622 got_object_close(obj1);
623 if (obj2)
624 got_object_close(obj2);
625 if (id1)
626 free(id1);
627 if (id2)
628 free(id2);
629 if (repo)
630 got_repo_close(repo);
631 return error;
634 #ifdef notyet
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;
641 size_t i;
643 git_libgit2_init();
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);
669 break;
670 case GIT_STATUS_WT_MODIFIED:
671 printf("M %s\n", se->index_to_workdir->new_file.path);
672 break;
673 case GIT_STATUS_WT_DELETED:
674 printf("R %s\n", se->index_to_workdir->new_file.path);
675 break;
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);
680 break;
681 case GIT_STATUS_WT_TYPECHANGE:
682 printf("t %s\n", se->index_to_workdir->new_file.path);
683 break;
684 case GIT_STATUS_INDEX_NEW:
685 printf("A %s\n", se->head_to_index->new_file.path);
686 break;
687 case GIT_STATUS_INDEX_MODIFIED:
688 printf("M %s\n", se->head_to_index->old_file.path);
689 break;
690 case GIT_STATUS_INDEX_DELETED:
691 printf("R %s\n", se->head_to_index->old_file.path);
692 break;
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);
697 break;
698 case GIT_STATUS_INDEX_TYPECHANGE:
699 printf("t %s\n", se->head_to_index->old_file.path);
700 break;
701 case GIT_STATUS_CURRENT:
702 default:
703 break;
707 git_status_list_free(status);
708 git_repository_free(repo);
709 git_libgit2_shutdown();
711 return 0;
713 #endif