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"
36 #include "got_commit_graph.h"
38 #ifndef nitems
39 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
40 #endif
42 struct cmd {
43 const char *cmd_name;
44 const struct got_error *(*cmd_main)(int, char *[]);
45 void (*cmd_usage)(void);
46 const char *cmd_descr;
47 };
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 *[]);
57 #ifdef notyet
58 static const struct got_error* cmd_status(int, char *[]);
59 #endif
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" },
68 #ifdef notyet
69 { "status", cmd_status, usage_status,
70 "show modification status of files" },
71 #endif
72 };
74 int
75 main(int argc, char *argv[])
76 {
77 struct cmd *cmd;
78 unsigned int i;
79 int ch;
80 int hflag = 0;
82 setlocale(LC_ALL, "");
84 while ((ch = getopt(argc, argv, "h")) != -1) {
85 switch (ch) {
86 case 'h':
87 hflag = 1;
88 break;
89 default:
90 usage();
91 /* NOTREACHED */
92 }
93 }
95 argc -= optind;
96 argv += optind;
97 optind = 0;
99 if (argc <= 0)
100 usage();
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])))
108 continue;
110 if (hflag)
111 got_commands[i].cmd_usage();
113 error = got_commands[i].cmd_main(argc, argv);
114 if (error) {
115 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
116 return 1;
119 return 0;
122 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
123 return 1;
126 __dead static void
127 usage(void)
129 int i;
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);
137 exit(1);
140 __dead static void
141 usage_checkout(void)
143 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
144 "[worktree-path]\n", getprogname());
145 exit(1);
148 static void
149 checkout_progress(void *arg, const char *path)
151 char *worktree_path = arg;
153 while (path[0] == '/')
154 path++;
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 = "";
169 int ch;
171 while ((ch = getopt(argc, argv, "p:")) != -1) {
172 switch (ch) {
173 case 'p':
174 path_prefix = optarg;
175 break;
176 default:
177 usage();
178 /* NOTREACHED */
182 argc -= optind;
183 argv += optind;
185 #ifndef PROFILE
186 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
187 err(1, "pledge");
188 #endif
189 if (argc == 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);
195 if (cwd == NULL) {
196 error = got_error_from_errno();
197 goto done;
199 if (path_prefix[0])
200 base = basename(path_prefix);
201 else
202 base = basename(repo_path);
203 if (base == NULL) {
204 error = got_error_from_errno();
205 goto done;
207 dotgit = strstr(base, ".git");
208 if (dotgit)
209 *dotgit = '\0';
210 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
211 error = got_error_from_errno();
212 free(cwd);
213 goto done;
215 free(cwd);
216 } else if (argc == 2) {
217 repo_path = realpath(argv[0], NULL);
218 if (repo_path == NULL) {
219 error = got_error_from_errno();
220 goto done;
222 worktree_path = realpath(argv[1], NULL);
223 if (worktree_path == NULL) {
224 error = got_error_from_errno();
225 goto done;
227 } else
228 usage_checkout();
230 error = got_repo_open(&repo, repo_path);
231 if (error != NULL)
232 goto done;
233 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
234 if (error != NULL)
235 goto done;
237 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
238 if (error != NULL)
239 goto done;
241 error = got_worktree_open(&worktree, worktree_path);
242 if (error != NULL)
243 goto done;
245 error = got_worktree_checkout_files(worktree, head_ref, repo,
246 checkout_progress, worktree_path);
247 if (error != NULL)
248 goto done;
250 printf("Checked out %s\n", worktree_path);
251 printf("Now shut up and hack\n");
253 done:
254 free(repo_path);
255 free(worktree_path);
256 return error;
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);
269 if (err)
270 return err;
272 err = got_object_tree_open(&tree2, repo, obj);
273 got_object_close(obj);
274 if (err)
275 return err;
277 pid = SIMPLEQ_FIRST(&commit->parent_ids);
278 if (pid != NULL) {
279 struct got_commit_object *pcommit;
281 err = got_object_open(&obj, repo, pid->id);
282 if (err)
283 return err;
285 err = got_object_commit_open(&pcommit, repo, obj);
286 got_object_close(obj);
287 if (err)
288 return err;
290 err = got_object_open(&obj, repo, pcommit->tree_id);
291 got_object_commit_close(pcommit);
292 if (err)
293 return err;
294 err = got_object_tree_open(&tree1, repo, obj);
295 got_object_close(obj);
296 if (err)
297 return err;
300 err = got_diff_tree(tree1, tree2, repo, stdout);
301 if (tree1)
302 got_object_tree_close(tree1);
303 got_object_tree_close(tree2);
304 return err;
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);
315 if (err)
316 return err;
318 printf("-----------------------------------------------\n");
319 printf("commit %s\n", id_str);
320 free(id_str);
321 printf("author: %s\n", commit->author);
322 if (strcmp(commit->author, commit->committer) != 0)
323 printf("committer: %s\n", commit->committer);
324 if (commit->nparents > 1) {
325 struct got_parent_id *pid;
326 int n = 1;
327 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
328 err = got_object_id_str(&id_str, pid->id);
329 if (err)
330 return err;
331 printf("parent %d: %s\n", n++, id_str);
332 free(id_str);
336 logmsg = strdup(commit->logmsg);
337 if (logmsg == NULL)
338 return got_error_from_errno();
340 do {
341 line = strsep(&logmsg, "\n");
342 if (line)
343 printf(" %s\n", line);
344 } while (line);
345 free(logmsg);
347 if (show_patch) {
348 err = print_patch(commit, id, repo);
349 if (err == 0)
350 printf("\n");
353 return err;
356 static const struct got_error *
357 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
358 struct got_repository *repo, int show_patch, int limit, int verbose)
360 const struct got_error *err;
361 struct got_commit_graph *graph;
362 int ncommits;
364 err = got_commit_graph_open(&graph, root_id, repo);
365 if (err)
366 return err;
367 err = got_commit_graph_iter_start(graph, root_id);
368 if (err)
369 return err;
370 do {
371 struct got_commit_object *commit;
372 struct got_object_id *id;
374 err = got_commit_graph_iter_next(&commit, &id, graph);
375 if (err) {
376 if (err->code != GOT_ERR_ITER_NEED_MORE)
377 break;
378 err = got_commit_graph_fetch_commits(&ncommits,
379 graph, 1, repo);
380 if (err)
381 break;
382 else
383 continue;
385 if (commit == NULL)
386 break;
387 err = print_commit(commit, id, repo, show_patch, verbose);
388 if (err || (limit && --limit == 0))
389 break;
390 } while (ncommits > 0);
392 got_commit_graph_close(graph);
393 return err;
396 __dead static void
397 usage_log(void)
399 fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
400 "[repository-path]\n", getprogname());
401 exit(1);
404 static const struct got_error *
405 cmd_log(int argc, char *argv[])
407 const struct got_error *error;
408 struct got_repository *repo;
409 struct got_object_id *id = NULL;
410 struct got_object *obj;
411 char *repo_path = NULL;
412 char *start_commit = NULL;
413 int ch;
414 int show_patch = 0, limit = 0, verbose = 0;
415 const char *errstr;
417 #ifndef PROFILE
418 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
419 err(1, "pledge");
420 #endif
422 while ((ch = getopt(argc, argv, "pc:l:v")) != -1) {
423 switch (ch) {
424 case 'p':
425 show_patch = 1;
426 break;
427 case 'c':
428 start_commit = optarg;
429 break;
430 case 'l':
431 limit = strtonum(optarg, 1, INT_MAX, &errstr);
432 if (errstr != NULL)
433 err(1, "-l option %s", errstr);
434 break;
435 case 'v':
436 verbose = 1;
437 break;
438 default:
439 usage();
440 /* NOTREACHED */
444 argc -= optind;
445 argv += optind;
447 if (argc == 0) {
448 repo_path = getcwd(NULL, 0);
449 if (repo_path == NULL)
450 return got_error_from_errno();
451 } else if (argc == 1) {
452 repo_path = realpath(argv[0], NULL);
453 if (repo_path == NULL)
454 return got_error_from_errno();
455 } else
456 usage_log();
458 error = got_repo_open(&repo, repo_path);
459 free(repo_path);
460 if (error != NULL)
461 return error;
463 if (start_commit == NULL) {
464 struct got_reference *head_ref;
465 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
466 if (error != NULL)
467 return error;
468 error = got_ref_resolve(&id, repo, head_ref);
469 got_ref_close(head_ref);
470 if (error != NULL)
471 return error;
472 error = got_object_open(&obj, repo, id);
473 } else {
474 error = got_object_open_by_id_str(&obj, repo, start_commit);
475 if (error == NULL) {
476 id = got_object_get_id(obj);
477 if (id == NULL)
478 error = got_error_from_errno();
481 if (error != NULL)
482 return error;
483 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
484 error = print_commits(obj, id, repo, show_patch, limit,
485 verbose);
486 else
487 error = got_error(GOT_ERR_OBJ_TYPE);
488 got_object_close(obj);
489 free(id);
490 got_repo_close(repo);
491 return error;
494 __dead static void
495 usage_diff(void)
497 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
498 getprogname());
499 exit(1);
502 static const struct got_error *
503 cmd_diff(int argc, char *argv[])
505 const struct got_error *error;
506 struct got_repository *repo = NULL;
507 struct got_object_id *id1 = NULL, *id2 = NULL;
508 struct got_object *obj1 = NULL, *obj2 = NULL;
509 char *repo_path = NULL;
510 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
511 int ch;
513 #ifndef PROFILE
514 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
515 err(1, "pledge");
516 #endif
518 while ((ch = getopt(argc, argv, "")) != -1) {
519 switch (ch) {
520 default:
521 usage();
522 /* NOTREACHED */
526 argc -= optind;
527 argv += optind;
529 if (argc == 0) {
530 usage_diff(); /* TODO show local worktree changes */
531 } else if (argc == 2) {
532 repo_path = getcwd(NULL, 0);
533 if (repo_path == NULL)
534 return got_error_from_errno();
535 obj_id_str1 = argv[0];
536 obj_id_str2 = argv[1];
537 } else if (argc == 3) {
538 repo_path = realpath(argv[0], NULL);
539 if (repo_path == NULL)
540 return got_error_from_errno();
541 obj_id_str1 = argv[1];
542 obj_id_str2 = argv[2];
543 } else
544 usage_diff();
546 error = got_repo_open(&repo, repo_path);
547 free(repo_path);
548 if (error != NULL)
549 goto done;
551 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
552 if (error == NULL) {
553 id1 = got_object_get_id(obj1);
554 if (id1 == NULL)
555 error = got_error_from_errno();
557 if (error != NULL)
558 goto done;
560 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
561 if (error == NULL) {
562 id2 = got_object_get_id(obj2);
563 if (id2 == NULL)
564 error = got_error_from_errno();
566 if (error != NULL)
567 goto done;
569 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
570 error = got_error(GOT_ERR_OBJ_TYPE);
571 goto done;
574 switch (got_object_get_type(obj1)) {
575 case GOT_OBJ_TYPE_BLOB:
576 error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
577 break;
578 case GOT_OBJ_TYPE_TREE:
579 error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
580 break;
581 case GOT_OBJ_TYPE_COMMIT:
582 error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
583 break;
584 default:
585 error = got_error(GOT_ERR_OBJ_TYPE);
588 done:
589 if (obj1)
590 got_object_close(obj1);
591 if (obj2)
592 got_object_close(obj2);
593 if (id1)
594 free(id1);
595 if (id2)
596 free(id2);
597 if (repo)
598 got_repo_close(repo);
599 return error;
602 #ifdef notyet
603 static const struct got_error *
604 cmd_status(int argc __unused, char *argv[] __unused)
606 git_repository *repo = NULL;
607 git_status_list *status;
608 git_status_options statusopts;
609 size_t i;
611 git_libgit2_init();
613 if (git_repository_open_ext(&repo, ".", 0, NULL))
614 errx(1, "git_repository_open: %s", giterr_last()->message);
616 if (git_repository_is_bare(repo))
617 errx(1, "bar repository");
619 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
620 errx(1, "git_status_init_options: %s", giterr_last()->message);
622 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
623 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
624 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
625 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
627 if (git_status_list_new(&status, repo, &statusopts))
628 errx(1, "git_status_list_new: %s", giterr_last()->message);
630 for (i = 0; i < git_status_list_entrycount(status); i++) {
631 const git_status_entry *se;
633 se = git_status_byindex(status, i);
634 switch (se->status) {
635 case GIT_STATUS_WT_NEW:
636 printf("? %s\n", se->index_to_workdir->new_file.path);
637 break;
638 case GIT_STATUS_WT_MODIFIED:
639 printf("M %s\n", se->index_to_workdir->new_file.path);
640 break;
641 case GIT_STATUS_WT_DELETED:
642 printf("R %s\n", se->index_to_workdir->new_file.path);
643 break;
644 case GIT_STATUS_WT_RENAMED:
645 printf("m %s -> %s\n",
646 se->index_to_workdir->old_file.path,
647 se->index_to_workdir->new_file.path);
648 break;
649 case GIT_STATUS_WT_TYPECHANGE:
650 printf("t %s\n", se->index_to_workdir->new_file.path);
651 break;
652 case GIT_STATUS_INDEX_NEW:
653 printf("A %s\n", se->head_to_index->new_file.path);
654 break;
655 case GIT_STATUS_INDEX_MODIFIED:
656 printf("M %s\n", se->head_to_index->old_file.path);
657 break;
658 case GIT_STATUS_INDEX_DELETED:
659 printf("R %s\n", se->head_to_index->old_file.path);
660 break;
661 case GIT_STATUS_INDEX_RENAMED:
662 printf("m %s -> %s\n",
663 se->head_to_index->old_file.path,
664 se->head_to_index->new_file.path);
665 break;
666 case GIT_STATUS_INDEX_TYPECHANGE:
667 printf("t %s\n", se->head_to_index->old_file.path);
668 break;
669 case GIT_STATUS_CURRENT:
670 default:
671 break;
675 git_status_list_free(status);
676 git_repository_free(repo);
677 git_libgit2_shutdown();
679 return 0;
681 #endif