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 void usage(void);
49 __dead void usage_checkout(void);
50 __dead void usage_log(void);
51 __dead void usage_diff(void);
53 const struct got_error* cmd_checkout(int, char *[]);
54 const struct got_error* cmd_log(int, char *[]);
55 const struct got_error* cmd_diff(int, char *[]);
56 const struct got_error* cmd_status(int, char *[]);
58 struct cmd got_commands[] = {
59 { "checkout", cmd_checkout, usage_checkout,
60 "check out a new work tree from a repository" },
61 { "log", cmd_log, usage_log,
62 "show repository history" },
63 { "diff", cmd_diff, usage_diff,
64 "compare files and directories" },
65 #ifdef notyet
66 { "status", cmd_status, usage_status,
67 "show modification status of files" },
68 #endif
69 };
71 int
72 main(int argc, char *argv[])
73 {
74 struct cmd *cmd;
75 unsigned int i;
76 int ch;
77 int hflag = 0;
79 setlocale(LC_ALL, "");
81 while ((ch = getopt(argc, argv, "h")) != -1) {
82 switch (ch) {
83 case 'h':
84 hflag = 1;
85 break;
86 default:
87 usage();
88 /* NOTREACHED */
89 }
90 }
92 argc -= optind;
93 argv += optind;
94 optind = 0;
96 if (argc <= 0)
97 usage();
99 for (i = 0; i < nitems(got_commands); i++) {
100 const struct got_error *error;
102 cmd = &got_commands[i];
104 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
105 continue;
107 if (hflag)
108 got_commands[i].cmd_usage();
110 error = got_commands[i].cmd_main(argc, argv);
111 if (error) {
112 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
113 return 1;
116 return 0;
119 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
120 return 1;
123 __dead void
124 usage(void)
126 int i;
128 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
129 "Available commands:\n", getprogname());
130 for (i = 0; i < nitems(got_commands); i++) {
131 struct cmd *cmd = &got_commands[i];
132 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
134 exit(1);
137 __dead void
138 usage_checkout(void)
140 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
141 "[worktree-path]\n", getprogname());
142 exit(1);
145 static void
146 checkout_progress(void *arg, const char *path)
148 char *worktree_path = arg;
150 while (path[0] == '/')
151 path++;
153 printf("A %s/%s\n", worktree_path, path);
156 const struct got_error *
157 cmd_checkout(int argc, char *argv[])
159 const struct got_error *error = NULL;
160 struct got_repository *repo = NULL;
161 struct got_reference *head_ref = NULL;
162 struct got_worktree *worktree = NULL;
163 char *repo_path = NULL;
164 char *worktree_path = NULL;
165 const char *path_prefix = "";
166 int ch;
168 while ((ch = getopt(argc, argv, "p:")) != -1) {
169 switch (ch) {
170 case 'p':
171 path_prefix = optarg;
172 break;
173 default:
174 usage();
175 /* NOTREACHED */
179 argc -= optind;
180 argv += optind;
182 #ifndef PROFILE
183 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
184 err(1, "pledge");
185 #endif
186 if (argc == 1) {
187 char *cwd, *base, *dotgit;
188 repo_path = realpath(argv[0], NULL);
189 if (repo_path == NULL)
190 return got_error_from_errno();
191 cwd = getcwd(NULL, 0);
192 if (cwd == NULL) {
193 error = got_error_from_errno();
194 goto done;
196 if (path_prefix[0])
197 base = basename(path_prefix);
198 else
199 base = basename(repo_path);
200 if (base == NULL) {
201 error = got_error_from_errno();
202 goto done;
204 dotgit = strstr(base, ".git");
205 if (dotgit)
206 *dotgit = '\0';
207 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
208 error = got_error_from_errno();
209 free(cwd);
210 goto done;
212 free(cwd);
213 } else if (argc == 2) {
214 repo_path = realpath(argv[0], NULL);
215 if (repo_path == NULL) {
216 error = got_error_from_errno();
217 goto done;
219 worktree_path = realpath(argv[1], NULL);
220 if (worktree_path == NULL) {
221 error = got_error_from_errno();
222 goto done;
224 } else
225 usage_checkout();
227 error = got_repo_open(&repo, repo_path);
228 if (error != NULL)
229 goto done;
230 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
231 if (error != NULL)
232 goto done;
234 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
235 if (error != NULL)
236 goto done;
238 error = got_worktree_open(&worktree, worktree_path);
239 if (error != NULL)
240 goto done;
242 error = got_worktree_checkout_files(worktree, head_ref, repo,
243 checkout_progress, worktree_path);
244 if (error != NULL)
245 goto done;
247 printf("Checked out %s\n", worktree_path);
248 printf("Now shut up and hack\n");
250 done:
251 free(repo_path);
252 free(worktree_path);
253 return error;
256 static const struct got_error *
257 print_patch(struct got_commit_object *commit, struct got_object_id *id,
258 struct got_repository *repo)
260 const struct got_error *err = NULL;
261 struct got_tree_object *tree1 = NULL, *tree2;
262 struct got_object *obj;
263 struct got_parent_id *pid;
265 err = got_object_open(&obj, repo, commit->tree_id);
266 if (err)
267 return err;
269 err = got_object_tree_open(&tree2, repo, obj);
270 got_object_close(obj);
271 if (err)
272 return err;
274 pid = SIMPLEQ_FIRST(&commit->parent_ids);
275 if (pid != NULL) {
276 struct got_commit_object *pcommit;
278 err = got_object_open(&obj, repo, pid->id);
279 if (err)
280 return err;
282 err = got_object_commit_open(&pcommit, repo, obj);
283 got_object_close(obj);
284 if (err)
285 return err;
287 err = got_object_open(&obj, repo, pcommit->tree_id);
288 got_object_commit_close(pcommit);
289 if (err)
290 return err;
291 err = got_object_tree_open(&tree1, repo, obj);
292 got_object_close(obj);
293 if (err)
294 return err;
297 err = got_diff_tree(tree1, tree2, repo, stdout);
298 if (tree1)
299 got_object_tree_close(tree1);
300 got_object_tree_close(tree2);
301 return err;
304 static const struct got_error *
305 print_commit(struct got_commit_object *commit, struct got_object_id *id,
306 struct got_repository *repo, int show_patch)
308 const struct got_error *err = NULL;
309 char *buf;
311 err = got_object_id_str(&buf, id);
312 if (err)
313 return err;
315 printf("-----------------------------------------------\n");
316 printf("commit %s\n", buf);
317 printf("author: %s\n", commit->author);
318 if (strcmp(commit->author, commit->committer) != 0)
319 printf("committer: %s\n", commit->committer);
320 printf("\n%s\n", commit->logmsg);
322 if (show_patch) {
323 err = print_patch(commit, id, repo);
324 if (err == 0)
325 printf("\n");
328 free(buf);
329 return err;
332 struct commit_queue_entry {
333 TAILQ_ENTRY(commit_queue_entry) entry;
334 struct got_object_id *id;
335 struct got_commit_object *commit;
336 };
338 static const struct got_error *
339 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
340 struct got_repository *repo, int show_patch, int limit)
342 const struct got_error *err;
343 struct got_commit_object *root_commit;
344 TAILQ_HEAD(, commit_queue_entry) commits;
345 struct commit_queue_entry *entry;
347 TAILQ_INIT(&commits);
349 err = got_object_commit_open(&root_commit, repo, root_obj);
350 if (err)
351 return err;
353 entry = calloc(1, sizeof(*entry));
354 if (entry == NULL)
355 return got_error_from_errno();
356 entry->id = got_object_id_dup(root_id);
357 if (entry->id == NULL) {
358 err = got_error_from_errno();
359 free(entry);
360 return err;
362 entry->commit = root_commit;
363 TAILQ_INSERT_HEAD(&commits, entry, entry);
365 while (!TAILQ_EMPTY(&commits)) {
366 struct got_parent_id *pid;
367 struct got_object *obj;
368 struct got_commit_object *pcommit;
369 struct commit_queue_entry *pentry;
371 entry = TAILQ_FIRST(&commits);
373 err = print_commit(entry->commit, entry->id, repo, show_patch);
374 if (err)
375 break;
377 if (limit && --limit == 0)
378 break;
380 if (entry->commit->nparents == 0)
381 break;
383 /* Follow the first parent (TODO: handle merge commits). */
384 pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
385 err = got_object_open(&obj, repo, pid->id);
386 if (err)
387 break;
388 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
389 err = got_error(GOT_ERR_OBJ_TYPE);
390 break;
393 err = got_object_commit_open(&pcommit, repo, obj);
394 got_object_close(obj);
395 if (err)
396 break;
398 pentry = calloc(1, sizeof(*pentry));
399 if (pentry == NULL) {
400 err = got_error_from_errno();
401 got_object_commit_close(pcommit);
402 break;
404 pentry->id = got_object_id_dup(pid->id);
405 if (pentry->id == NULL) {
406 err = got_error_from_errno();
407 got_object_commit_close(pcommit);
408 break;
410 pentry->commit = pcommit;
411 TAILQ_INSERT_TAIL(&commits, pentry, entry);
413 TAILQ_REMOVE(&commits, entry, entry);
414 got_object_commit_close(entry->commit);
415 free(entry->id);
416 free(entry);
419 while (!TAILQ_EMPTY(&commits)) {
420 entry = TAILQ_FIRST(&commits);
421 TAILQ_REMOVE(&commits, entry, entry);
422 got_object_commit_close(entry->commit);
423 free(entry->id);
424 free(entry);
427 return err;
430 __dead void
431 usage_log(void)
433 fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
434 "[repository-path]\n", getprogname());
435 exit(1);
438 const struct got_error *
439 cmd_log(int argc, char *argv[])
441 const struct got_error *error;
442 struct got_repository *repo;
443 struct got_object_id *id = NULL;
444 struct got_object *obj;
445 char *repo_path = NULL;
446 char *start_commit = NULL;
447 int ch;
448 int show_patch = 0, limit = 0;
449 const char *errstr;
451 #ifndef PROFILE
452 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
453 err(1, "pledge");
454 #endif
456 while ((ch = getopt(argc, argv, "pc:l:")) != -1) {
457 switch (ch) {
458 case 'p':
459 show_patch = 1;
460 break;
461 case 'c':
462 start_commit = optarg;
463 break;
464 case 'l':
465 limit = strtonum(optarg, 1, INT_MAX, &errstr);
466 if (errstr != NULL)
467 err(1, "-l option %s", errstr);
468 break;
469 default:
470 usage();
471 /* NOTREACHED */
475 argc -= optind;
476 argv += optind;
478 if (argc == 0) {
479 repo_path = getcwd(NULL, 0);
480 if (repo_path == NULL)
481 return got_error_from_errno();
482 } else if (argc == 1) {
483 repo_path = realpath(argv[0], NULL);
484 if (repo_path == NULL)
485 return got_error_from_errno();
486 } else
487 usage_log();
489 error = got_repo_open(&repo, repo_path);
490 free(repo_path);
491 if (error != NULL)
492 return error;
494 if (start_commit == NULL) {
495 struct got_reference *head_ref;
496 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
497 if (error != NULL)
498 return error;
499 error = got_ref_resolve(&id, repo, head_ref);
500 got_ref_close(head_ref);
501 if (error != NULL)
502 return error;
503 error = got_object_open(&obj, repo, id);
504 } else {
505 error = got_object_open_by_id_str(&obj, repo, start_commit);
506 if (error == NULL) {
507 id = got_object_get_id(obj);
508 if (id == NULL)
509 error = got_error_from_errno();
512 if (error != NULL)
513 return error;
514 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
515 error = print_commits(obj, id, repo, show_patch, limit);
516 else
517 error = got_error(GOT_ERR_OBJ_TYPE);
518 got_object_close(obj);
519 free(id);
520 got_repo_close(repo);
521 return error;
524 __dead void
525 usage_diff(void)
527 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
528 getprogname());
529 exit(1);
532 const struct got_error *
533 cmd_diff(int argc, char *argv[])
535 const struct got_error *error;
536 struct got_repository *repo = NULL;
537 struct got_object_id *id1 = NULL, *id2 = NULL;
538 struct got_object *obj1 = NULL, *obj2 = NULL;
539 char *repo_path = NULL;
540 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
541 int ch;
543 #ifndef PROFILE
544 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
545 err(1, "pledge");
546 #endif
548 while ((ch = getopt(argc, argv, "")) != -1) {
549 switch (ch) {
550 default:
551 usage();
552 /* NOTREACHED */
556 argc -= optind;
557 argv += optind;
559 if (argc == 0) {
560 usage_diff(); /* TODO show local worktree changes */
561 } else if (argc == 2) {
562 repo_path = getcwd(NULL, 0);
563 if (repo_path == NULL)
564 return got_error_from_errno();
565 obj_id_str1 = argv[0];
566 obj_id_str2 = argv[1];
567 } else if (argc == 3) {
568 repo_path = realpath(argv[0], NULL);
569 if (repo_path == NULL)
570 return got_error_from_errno();
571 obj_id_str1 = argv[1];
572 obj_id_str2 = argv[2];
573 } else
574 usage_diff();
576 error = got_repo_open(&repo, repo_path);
577 free(repo_path);
578 if (error != NULL)
579 goto done;
581 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
582 if (error == NULL) {
583 id1 = got_object_get_id(obj1);
584 if (id1 == NULL)
585 error = got_error_from_errno();
587 if (error != NULL)
588 goto done;
590 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
591 if (error == NULL) {
592 id2 = got_object_get_id(obj2);
593 if (id2 == NULL)
594 error = got_error_from_errno();
596 if (error != NULL)
597 goto done;
599 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
600 error = got_error(GOT_ERR_OBJ_TYPE);
601 goto done;
604 switch (got_object_get_type(obj1)) {
605 case GOT_OBJ_TYPE_BLOB:
606 error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
607 break;
608 case GOT_OBJ_TYPE_TREE:
609 error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
610 break;
611 case GOT_OBJ_TYPE_COMMIT:
612 error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
613 break;
614 default:
615 error = got_error(GOT_ERR_OBJ_TYPE);
618 done:
619 if (obj1)
620 got_object_close(obj1);
621 if (obj2)
622 got_object_close(obj2);
623 if (id1)
624 free(id1);
625 if (id2)
626 free(id2);
627 if (repo)
628 got_repo_close(repo);
629 return error;
632 #ifdef notyet
633 const struct got_error *
634 cmd_status(int argc __unused, char *argv[] __unused)
636 git_repository *repo = NULL;
637 git_status_list *status;
638 git_status_options statusopts;
639 size_t i;
641 git_libgit2_init();
643 if (git_repository_open_ext(&repo, ".", 0, NULL))
644 errx(1, "git_repository_open: %s", giterr_last()->message);
646 if (git_repository_is_bare(repo))
647 errx(1, "bar repository");
649 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
650 errx(1, "git_status_init_options: %s", giterr_last()->message);
652 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
653 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
654 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
655 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
657 if (git_status_list_new(&status, repo, &statusopts))
658 errx(1, "git_status_list_new: %s", giterr_last()->message);
660 for (i = 0; i < git_status_list_entrycount(status); i++) {
661 const git_status_entry *se;
663 se = git_status_byindex(status, i);
664 switch (se->status) {
665 case GIT_STATUS_WT_NEW:
666 printf("? %s\n", se->index_to_workdir->new_file.path);
667 break;
668 case GIT_STATUS_WT_MODIFIED:
669 printf("M %s\n", se->index_to_workdir->new_file.path);
670 break;
671 case GIT_STATUS_WT_DELETED:
672 printf("R %s\n", se->index_to_workdir->new_file.path);
673 break;
674 case GIT_STATUS_WT_RENAMED:
675 printf("m %s -> %s\n",
676 se->index_to_workdir->old_file.path,
677 se->index_to_workdir->new_file.path);
678 break;
679 case GIT_STATUS_WT_TYPECHANGE:
680 printf("t %s\n", se->index_to_workdir->new_file.path);
681 break;
682 case GIT_STATUS_INDEX_NEW:
683 printf("A %s\n", se->head_to_index->new_file.path);
684 break;
685 case GIT_STATUS_INDEX_MODIFIED:
686 printf("M %s\n", se->head_to_index->old_file.path);
687 break;
688 case GIT_STATUS_INDEX_DELETED:
689 printf("R %s\n", se->head_to_index->old_file.path);
690 break;
691 case GIT_STATUS_INDEX_RENAMED:
692 printf("m %s -> %s\n",
693 se->head_to_index->old_file.path,
694 se->head_to_index->new_file.path);
695 break;
696 case GIT_STATUS_INDEX_TYPECHANGE:
697 printf("t %s\n", se->head_to_index->old_file.path);
698 break;
699 case GIT_STATUS_CURRENT:
700 default:
701 break;
705 git_status_list_free(status);
706 git_repository_free(repo);
707 git_libgit2_shutdown();
709 return 0;
711 #endif