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 (verbose) {
325 struct got_parent_id *pid;
326 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
327 err = got_object_id_str(&id_str, pid->id);
328 if (err)
329 return err;
330 printf("parent commit: %s\n", id_str);
331 free(id_str);
335 logmsg = strdup(commit->logmsg);
336 if (logmsg == NULL)
337 return got_error_from_errno();
339 do {
340 line = strsep(&logmsg, "\n");
341 if (line)
342 printf(" %s\n", line);
343 } while (line);
344 free(logmsg);
346 if (show_patch) {
347 err = print_patch(commit, id, repo);
348 if (err == 0)
349 printf("\n");
352 return err;
355 static const struct got_error *
356 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
357 struct got_repository *repo, int show_patch, int limit, int verbose)
359 const struct got_error *err;
360 struct got_commit_graph *graph;
361 int ncommits;
363 err = got_commit_graph_open(&graph, root_id, repo);
364 if (err)
365 return err;
366 err = got_commit_graph_iter_start(graph, root_id);
367 if (err)
368 return err;
369 do {
370 struct got_commit_object *commit;
371 struct got_object_id *id;
373 err = got_commit_graph_iter_next(&commit, &id, graph);
374 if (err) {
375 if (err->code != GOT_ERR_ITER_NEED_MORE)
376 break;
377 err = got_commit_graph_fetch_commits(&ncommits,
378 graph, 1, repo);
379 if (err)
380 break;
381 else
382 continue;
384 if (commit == NULL)
385 break;
386 err = print_commit(commit, id, repo, show_patch, verbose);
387 if (err || (limit && --limit == 0))
388 break;
389 } while (ncommits > 0);
391 got_commit_graph_close(graph);
392 return err;
395 __dead static void
396 usage_log(void)
398 fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
399 "[repository-path]\n", getprogname());
400 exit(1);
403 static const struct got_error *
404 cmd_log(int argc, char *argv[])
406 const struct got_error *error;
407 struct got_repository *repo;
408 struct got_object_id *id = NULL;
409 struct got_object *obj;
410 char *repo_path = NULL;
411 char *start_commit = NULL;
412 int ch;
413 int show_patch = 0, limit = 0, verbose = 0;
414 const char *errstr;
416 #ifndef PROFILE
417 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
418 err(1, "pledge");
419 #endif
421 while ((ch = getopt(argc, argv, "pc:l:v")) != -1) {
422 switch (ch) {
423 case 'p':
424 show_patch = 1;
425 break;
426 case 'c':
427 start_commit = optarg;
428 break;
429 case 'l':
430 limit = strtonum(optarg, 1, INT_MAX, &errstr);
431 if (errstr != NULL)
432 err(1, "-l option %s", errstr);
433 break;
434 case 'v':
435 verbose = 1;
436 break;
437 default:
438 usage();
439 /* NOTREACHED */
443 argc -= optind;
444 argv += optind;
446 if (argc == 0) {
447 repo_path = getcwd(NULL, 0);
448 if (repo_path == NULL)
449 return got_error_from_errno();
450 } else if (argc == 1) {
451 repo_path = realpath(argv[0], NULL);
452 if (repo_path == NULL)
453 return got_error_from_errno();
454 } else
455 usage_log();
457 error = got_repo_open(&repo, repo_path);
458 free(repo_path);
459 if (error != NULL)
460 return error;
462 if (start_commit == NULL) {
463 struct got_reference *head_ref;
464 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
465 if (error != NULL)
466 return error;
467 error = got_ref_resolve(&id, repo, head_ref);
468 got_ref_close(head_ref);
469 if (error != NULL)
470 return error;
471 error = got_object_open(&obj, repo, id);
472 } else {
473 error = got_object_open_by_id_str(&obj, repo, start_commit);
474 if (error == NULL) {
475 id = got_object_get_id(obj);
476 if (id == NULL)
477 error = got_error_from_errno();
480 if (error != NULL)
481 return error;
482 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
483 error = print_commits(obj, id, repo, show_patch, limit,
484 verbose);
485 else
486 error = got_error(GOT_ERR_OBJ_TYPE);
487 got_object_close(obj);
488 free(id);
489 got_repo_close(repo);
490 return error;
493 __dead static void
494 usage_diff(void)
496 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
497 getprogname());
498 exit(1);
501 static const struct got_error *
502 cmd_diff(int argc, char *argv[])
504 const struct got_error *error;
505 struct got_repository *repo = NULL;
506 struct got_object_id *id1 = NULL, *id2 = NULL;
507 struct got_object *obj1 = NULL, *obj2 = NULL;
508 char *repo_path = NULL;
509 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
510 int ch;
512 #ifndef PROFILE
513 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
514 err(1, "pledge");
515 #endif
517 while ((ch = getopt(argc, argv, "")) != -1) {
518 switch (ch) {
519 default:
520 usage();
521 /* NOTREACHED */
525 argc -= optind;
526 argv += optind;
528 if (argc == 0) {
529 usage_diff(); /* TODO show local worktree changes */
530 } else if (argc == 2) {
531 repo_path = getcwd(NULL, 0);
532 if (repo_path == NULL)
533 return got_error_from_errno();
534 obj_id_str1 = argv[0];
535 obj_id_str2 = argv[1];
536 } else if (argc == 3) {
537 repo_path = realpath(argv[0], NULL);
538 if (repo_path == NULL)
539 return got_error_from_errno();
540 obj_id_str1 = argv[1];
541 obj_id_str2 = argv[2];
542 } else
543 usage_diff();
545 error = got_repo_open(&repo, repo_path);
546 free(repo_path);
547 if (error != NULL)
548 goto done;
550 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
551 if (error == NULL) {
552 id1 = got_object_get_id(obj1);
553 if (id1 == NULL)
554 error = got_error_from_errno();
556 if (error != NULL)
557 goto done;
559 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
560 if (error == NULL) {
561 id2 = got_object_get_id(obj2);
562 if (id2 == NULL)
563 error = got_error_from_errno();
565 if (error != NULL)
566 goto done;
568 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
569 error = got_error(GOT_ERR_OBJ_TYPE);
570 goto done;
573 switch (got_object_get_type(obj1)) {
574 case GOT_OBJ_TYPE_BLOB:
575 error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
576 break;
577 case GOT_OBJ_TYPE_TREE:
578 error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
579 break;
580 case GOT_OBJ_TYPE_COMMIT:
581 error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
582 break;
583 default:
584 error = got_error(GOT_ERR_OBJ_TYPE);
587 done:
588 if (obj1)
589 got_object_close(obj1);
590 if (obj2)
591 got_object_close(obj2);
592 if (id1)
593 free(id1);
594 if (id2)
595 free(id2);
596 if (repo)
597 got_repo_close(repo);
598 return error;
601 #ifdef notyet
602 static const struct got_error *
603 cmd_status(int argc __unused, char *argv[] __unused)
605 git_repository *repo = NULL;
606 git_status_list *status;
607 git_status_options statusopts;
608 size_t i;
610 git_libgit2_init();
612 if (git_repository_open_ext(&repo, ".", 0, NULL))
613 errx(1, "git_repository_open: %s", giterr_last()->message);
615 if (git_repository_is_bare(repo))
616 errx(1, "bar repository");
618 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
619 errx(1, "git_status_init_options: %s", giterr_last()->message);
621 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
622 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
623 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
624 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
626 if (git_status_list_new(&status, repo, &statusopts))
627 errx(1, "git_status_list_new: %s", giterr_last()->message);
629 for (i = 0; i < git_status_list_entrycount(status); i++) {
630 const git_status_entry *se;
632 se = git_status_byindex(status, i);
633 switch (se->status) {
634 case GIT_STATUS_WT_NEW:
635 printf("? %s\n", se->index_to_workdir->new_file.path);
636 break;
637 case GIT_STATUS_WT_MODIFIED:
638 printf("M %s\n", se->index_to_workdir->new_file.path);
639 break;
640 case GIT_STATUS_WT_DELETED:
641 printf("R %s\n", se->index_to_workdir->new_file.path);
642 break;
643 case GIT_STATUS_WT_RENAMED:
644 printf("m %s -> %s\n",
645 se->index_to_workdir->old_file.path,
646 se->index_to_workdir->new_file.path);
647 break;
648 case GIT_STATUS_WT_TYPECHANGE:
649 printf("t %s\n", se->index_to_workdir->new_file.path);
650 break;
651 case GIT_STATUS_INDEX_NEW:
652 printf("A %s\n", se->head_to_index->new_file.path);
653 break;
654 case GIT_STATUS_INDEX_MODIFIED:
655 printf("M %s\n", se->head_to_index->old_file.path);
656 break;
657 case GIT_STATUS_INDEX_DELETED:
658 printf("R %s\n", se->head_to_index->old_file.path);
659 break;
660 case GIT_STATUS_INDEX_RENAMED:
661 printf("m %s -> %s\n",
662 se->head_to_index->old_file.path,
663 se->head_to_index->new_file.path);
664 break;
665 case GIT_STATUS_INDEX_TYPECHANGE:
666 printf("t %s\n", se->head_to_index->old_file.path);
667 break;
668 case GIT_STATUS_CURRENT:
669 default:
670 break;
674 git_status_list_free(status);
675 git_repository_free(repo);
676 git_libgit2_shutdown();
678 return 0;
680 #endif