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>
20 #include <sys/types.h>
22 #include <err.h>
23 #include <errno.h>
24 #include <locale.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <libgen.h>
30 #include <time.h>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_reference.h"
35 #include "got_repository.h"
36 #include "got_worktree.h"
37 #include "got_diff.h"
38 #include "got_commit_graph.h"
40 #ifndef nitems
41 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
42 #endif
44 struct cmd {
45 const char *cmd_name;
46 const struct got_error *(*cmd_main)(int, char *[]);
47 void (*cmd_usage)(void);
48 const char *cmd_descr;
49 };
51 __dead static void usage(void);
52 __dead static void usage_checkout(void);
53 __dead static void usage_log(void);
54 __dead static void usage_diff(void);
56 static const struct got_error* cmd_checkout(int, char *[]);
57 static const struct got_error* cmd_log(int, char *[]);
58 static const struct got_error* cmd_diff(int, char *[]);
59 #ifdef notyet
60 static const struct got_error* cmd_status(int, char *[]);
61 #endif
63 static struct cmd got_commands[] = {
64 { "checkout", cmd_checkout, usage_checkout,
65 "check out a new work tree from a repository" },
66 { "log", cmd_log, usage_log,
67 "show repository history" },
68 { "diff", cmd_diff, usage_diff,
69 "compare files and directories" },
70 #ifdef notyet
71 { "status", cmd_status, usage_status,
72 "show modification status of files" },
73 #endif
74 };
76 int
77 main(int argc, char *argv[])
78 {
79 struct cmd *cmd;
80 unsigned int i;
81 int ch;
82 int hflag = 0;
84 setlocale(LC_ALL, "");
86 while ((ch = getopt(argc, argv, "h")) != -1) {
87 switch (ch) {
88 case 'h':
89 hflag = 1;
90 break;
91 default:
92 usage();
93 /* NOTREACHED */
94 }
95 }
97 argc -= optind;
98 argv += optind;
99 optind = 0;
101 if (argc <= 0)
102 usage();
104 for (i = 0; i < nitems(got_commands); i++) {
105 const struct got_error *error;
107 cmd = &got_commands[i];
109 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
110 continue;
112 if (hflag)
113 got_commands[i].cmd_usage();
115 error = got_commands[i].cmd_main(argc, argv);
116 if (error) {
117 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
118 return 1;
121 return 0;
124 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
125 return 1;
128 __dead static void
129 usage(void)
131 int i;
133 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
134 "Available commands:\n", getprogname());
135 for (i = 0; i < nitems(got_commands); i++) {
136 struct cmd *cmd = &got_commands[i];
137 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
139 exit(1);
142 __dead static void
143 usage_checkout(void)
145 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
146 "[worktree-path]\n", getprogname());
147 exit(1);
150 static void
151 checkout_progress(void *arg, const char *path)
153 char *worktree_path = arg;
155 while (path[0] == '/')
156 path++;
158 printf("A %s/%s\n", worktree_path, path);
161 static const struct got_error *
162 cmd_checkout(int argc, char *argv[])
164 const struct got_error *error = NULL;
165 struct got_repository *repo = NULL;
166 struct got_reference *head_ref = NULL;
167 struct got_worktree *worktree = NULL;
168 char *repo_path = NULL;
169 char *worktree_path = NULL;
170 const char *path_prefix = "";
171 int ch;
173 while ((ch = getopt(argc, argv, "p:")) != -1) {
174 switch (ch) {
175 case 'p':
176 path_prefix = optarg;
177 break;
178 default:
179 usage();
180 /* NOTREACHED */
184 argc -= optind;
185 argv += optind;
187 #ifndef PROFILE
188 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
189 err(1, "pledge");
190 #endif
191 if (argc == 1) {
192 char *cwd, *base, *dotgit;
193 repo_path = realpath(argv[0], NULL);
194 if (repo_path == NULL)
195 return got_error_from_errno();
196 cwd = getcwd(NULL, 0);
197 if (cwd == NULL) {
198 error = got_error_from_errno();
199 goto done;
201 if (path_prefix[0])
202 base = basename(path_prefix);
203 else
204 base = basename(repo_path);
205 if (base == NULL) {
206 error = got_error_from_errno();
207 goto done;
209 dotgit = strstr(base, ".git");
210 if (dotgit)
211 *dotgit = '\0';
212 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
213 error = got_error_from_errno();
214 free(cwd);
215 goto done;
217 free(cwd);
218 } else if (argc == 2) {
219 repo_path = realpath(argv[0], NULL);
220 if (repo_path == NULL) {
221 error = got_error_from_errno();
222 goto done;
224 worktree_path = realpath(argv[1], NULL);
225 if (worktree_path == NULL) {
226 error = got_error_from_errno();
227 goto done;
229 } else
230 usage_checkout();
232 error = got_repo_open(&repo, repo_path);
233 if (error != NULL)
234 goto done;
235 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
236 if (error != NULL)
237 goto done;
239 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
240 if (error != NULL)
241 goto done;
243 error = got_worktree_open(&worktree, worktree_path);
244 if (error != NULL)
245 goto done;
247 error = got_worktree_checkout_files(worktree, head_ref, repo,
248 checkout_progress, worktree_path);
249 if (error != NULL)
250 goto done;
252 printf("Checked out %s\n", worktree_path);
253 printf("Now shut up and hack\n");
255 done:
256 free(repo_path);
257 free(worktree_path);
258 return error;
261 static const struct got_error *
262 print_patch(struct got_commit_object *commit, struct got_object_id *id,
263 struct got_repository *repo)
265 const struct got_error *err = NULL;
266 struct got_tree_object *tree1 = NULL, *tree2;
267 struct got_object *obj;
268 struct got_object_qid *qid;
270 err = got_object_open(&obj, repo, commit->tree_id);
271 if (err)
272 return err;
274 err = got_object_tree_open(&tree2, repo, obj);
275 got_object_close(obj);
276 if (err)
277 return err;
279 qid = SIMPLEQ_FIRST(&commit->parent_ids);
280 if (qid != NULL) {
281 struct got_commit_object *pcommit;
283 err = got_object_open(&obj, repo, qid->id);
284 if (err)
285 return err;
287 err = got_object_commit_open(&pcommit, repo, obj);
288 got_object_close(obj);
289 if (err)
290 return err;
292 err = got_object_open(&obj, repo, pcommit->tree_id);
293 got_object_commit_close(pcommit);
294 if (err)
295 return err;
296 err = got_object_tree_open(&tree1, repo, obj);
297 got_object_close(obj);
298 if (err)
299 return err;
302 err = got_diff_tree(tree1, tree2, repo, stdout);
303 if (tree1)
304 got_object_tree_close(tree1);
305 got_object_tree_close(tree2);
306 return err;
309 char *
310 get_datestr(time_t *time, char *datebuf)
312 char *p, *s = ctime_r(time, datebuf);
313 p = strchr(s, '\n');
314 if (p)
315 *p = '\0';
316 return s;
319 static const struct got_error *
320 print_commit(struct got_commit_object *commit, struct got_object_id *id,
321 struct got_repository *repo, int show_patch, int verbose)
323 const struct got_error *err = NULL;
324 char *id_str, *datestr, *logmsg, *line;
325 char datebuf[26];
327 err = got_object_id_str(&id_str, id);
328 if (err)
329 return err;
331 printf("-----------------------------------------------\n");
332 printf("commit %s\n", id_str);
333 free(id_str);
334 datestr = get_datestr(&commit->author_time, datebuf);
335 printf("author: %s %s %s\n", commit->author, datestr,
336 commit->author_tzoff);
337 if (strcmp(commit->author, commit->committer) != 0 ||
338 commit->author_time != commit->committer_time ||
339 strcmp(commit->author_tzoff, commit->committer_tzoff) != 0) {
340 datestr = get_datestr(&commit->committer_time, datebuf);
341 printf("committer: %s %s %s\n", commit->committer,
342 datestr, commit->committer_tzoff);
344 if (commit->nparents > 1) {
345 struct got_object_qid *qid;
346 int n = 1;
347 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
348 err = got_object_id_str(&id_str, qid->id);
349 if (err)
350 return err;
351 printf("parent %d: %s\n", n++, id_str);
352 free(id_str);
356 logmsg = strdup(commit->logmsg);
357 if (logmsg == NULL)
358 return got_error_from_errno();
360 do {
361 line = strsep(&logmsg, "\n");
362 if (line)
363 printf(" %s\n", line);
364 } while (line);
365 free(logmsg);
367 if (show_patch) {
368 err = print_patch(commit, id, repo);
369 if (err == 0)
370 printf("\n");
373 return err;
376 static const struct got_error *
377 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
378 struct got_repository *repo, int show_patch, int limit, int verbose)
380 const struct got_error *err;
381 struct got_commit_graph *graph;
382 int ncommits;
384 err = got_commit_graph_open(&graph, root_id, repo);
385 if (err)
386 return err;
387 err = got_commit_graph_iter_start(graph, root_id);
388 if (err)
389 return err;
390 do {
391 struct got_commit_object *commit;
392 struct got_object_id *id;
394 err = got_commit_graph_iter_next(&id, graph);
395 if (err) {
396 if (err->code != GOT_ERR_ITER_NEED_MORE)
397 break;
398 err = got_commit_graph_fetch_commits(&ncommits,
399 graph, 1, repo);
400 if (err)
401 break;
402 else
403 continue;
405 if (id == NULL)
406 break;
408 err = got_object_open_as_commit(&commit, repo, id);
409 if (err)
410 return err;
411 err = print_commit(commit, id, repo, show_patch, verbose);
412 got_object_commit_close(commit);
413 if (err || (limit && --limit == 0))
414 break;
415 } while (ncommits > 0);
417 got_commit_graph_close(graph);
418 return err;
421 __dead static void
422 usage_log(void)
424 fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
425 "[repository-path]\n", getprogname());
426 exit(1);
429 static const struct got_error *
430 cmd_log(int argc, char *argv[])
432 const struct got_error *error;
433 struct got_repository *repo;
434 struct got_object_id *id = NULL;
435 struct got_object *obj;
436 char *repo_path = NULL;
437 char *start_commit = NULL;
438 int ch;
439 int show_patch = 0, limit = 0, verbose = 0;
440 const char *errstr;
442 #ifndef PROFILE
443 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
444 err(1, "pledge");
445 #endif
447 while ((ch = getopt(argc, argv, "pc:l:v")) != -1) {
448 switch (ch) {
449 case 'p':
450 show_patch = 1;
451 break;
452 case 'c':
453 start_commit = optarg;
454 break;
455 case 'l':
456 limit = strtonum(optarg, 1, INT_MAX, &errstr);
457 if (errstr != NULL)
458 err(1, "-l option %s", errstr);
459 break;
460 case 'v':
461 verbose = 1;
462 break;
463 default:
464 usage();
465 /* NOTREACHED */
469 argc -= optind;
470 argv += optind;
472 if (argc == 0) {
473 repo_path = getcwd(NULL, 0);
474 if (repo_path == NULL)
475 return got_error_from_errno();
476 } else if (argc == 1) {
477 repo_path = realpath(argv[0], NULL);
478 if (repo_path == NULL)
479 return got_error_from_errno();
480 } else
481 usage_log();
483 error = got_repo_open(&repo, repo_path);
484 free(repo_path);
485 if (error != NULL)
486 return error;
488 if (start_commit == NULL) {
489 struct got_reference *head_ref;
490 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
491 if (error != NULL)
492 return error;
493 error = got_ref_resolve(&id, repo, head_ref);
494 got_ref_close(head_ref);
495 if (error != NULL)
496 return error;
497 error = got_object_open(&obj, repo, id);
498 } else {
499 error = got_object_open_by_id_str(&obj, repo, start_commit);
500 if (error == NULL) {
501 id = got_object_get_id(obj);
502 if (id == NULL)
503 error = got_error_from_errno();
506 if (error != NULL)
507 return error;
508 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
509 error = print_commits(obj, id, repo, show_patch, limit,
510 verbose);
511 else
512 error = got_error(GOT_ERR_OBJ_TYPE);
513 got_object_close(obj);
514 free(id);
515 got_repo_close(repo);
516 return error;
519 __dead static void
520 usage_diff(void)
522 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
523 getprogname());
524 exit(1);
527 static const struct got_error *
528 cmd_diff(int argc, char *argv[])
530 const struct got_error *error;
531 struct got_repository *repo = NULL;
532 struct got_object_id *id1 = NULL, *id2 = NULL;
533 struct got_object *obj1 = NULL, *obj2 = NULL;
534 char *repo_path = NULL;
535 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
536 int ch;
538 #ifndef PROFILE
539 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
540 err(1, "pledge");
541 #endif
543 while ((ch = getopt(argc, argv, "")) != -1) {
544 switch (ch) {
545 default:
546 usage();
547 /* NOTREACHED */
551 argc -= optind;
552 argv += optind;
554 if (argc == 0) {
555 usage_diff(); /* TODO show local worktree changes */
556 } else if (argc == 2) {
557 repo_path = getcwd(NULL, 0);
558 if (repo_path == NULL)
559 return got_error_from_errno();
560 obj_id_str1 = argv[0];
561 obj_id_str2 = argv[1];
562 } else if (argc == 3) {
563 repo_path = realpath(argv[0], NULL);
564 if (repo_path == NULL)
565 return got_error_from_errno();
566 obj_id_str1 = argv[1];
567 obj_id_str2 = argv[2];
568 } else
569 usage_diff();
571 error = got_repo_open(&repo, repo_path);
572 free(repo_path);
573 if (error != NULL)
574 goto done;
576 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
577 if (error == NULL) {
578 id1 = got_object_get_id(obj1);
579 if (id1 == NULL)
580 error = got_error_from_errno();
582 if (error != NULL)
583 goto done;
585 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
586 if (error == NULL) {
587 id2 = got_object_get_id(obj2);
588 if (id2 == NULL)
589 error = got_error_from_errno();
591 if (error != NULL)
592 goto done;
594 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
595 error = got_error(GOT_ERR_OBJ_TYPE);
596 goto done;
599 switch (got_object_get_type(obj1)) {
600 case GOT_OBJ_TYPE_BLOB:
601 error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
602 break;
603 case GOT_OBJ_TYPE_TREE:
604 error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
605 break;
606 case GOT_OBJ_TYPE_COMMIT:
607 error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
608 break;
609 default:
610 error = got_error(GOT_ERR_OBJ_TYPE);
613 done:
614 if (obj1)
615 got_object_close(obj1);
616 if (obj2)
617 got_object_close(obj2);
618 if (id1)
619 free(id1);
620 if (id2)
621 free(id2);
622 if (repo)
623 got_repo_close(repo);
624 return error;
627 #ifdef notyet
628 static const struct got_error *
629 cmd_status(int argc __unused, char *argv[] __unused)
631 git_repository *repo = NULL;
632 git_status_list *status;
633 git_status_options statusopts;
634 size_t i;
636 git_libgit2_init();
638 if (git_repository_open_ext(&repo, ".", 0, NULL))
639 errx(1, "git_repository_open: %s", giterr_last()->message);
641 if (git_repository_is_bare(repo))
642 errx(1, "bar repository");
644 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
645 errx(1, "git_status_init_options: %s", giterr_last()->message);
647 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
648 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
649 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
650 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
652 if (git_status_list_new(&status, repo, &statusopts))
653 errx(1, "git_status_list_new: %s", giterr_last()->message);
655 for (i = 0; i < git_status_list_entrycount(status); i++) {
656 const git_status_entry *se;
658 se = git_status_byindex(status, i);
659 switch (se->status) {
660 case GIT_STATUS_WT_NEW:
661 printf("? %s\n", se->index_to_workdir->new_file.path);
662 break;
663 case GIT_STATUS_WT_MODIFIED:
664 printf("M %s\n", se->index_to_workdir->new_file.path);
665 break;
666 case GIT_STATUS_WT_DELETED:
667 printf("R %s\n", se->index_to_workdir->new_file.path);
668 break;
669 case GIT_STATUS_WT_RENAMED:
670 printf("m %s -> %s\n",
671 se->index_to_workdir->old_file.path,
672 se->index_to_workdir->new_file.path);
673 break;
674 case GIT_STATUS_WT_TYPECHANGE:
675 printf("t %s\n", se->index_to_workdir->new_file.path);
676 break;
677 case GIT_STATUS_INDEX_NEW:
678 printf("A %s\n", se->head_to_index->new_file.path);
679 break;
680 case GIT_STATUS_INDEX_MODIFIED:
681 printf("M %s\n", se->head_to_index->old_file.path);
682 break;
683 case GIT_STATUS_INDEX_DELETED:
684 printf("R %s\n", se->head_to_index->old_file.path);
685 break;
686 case GIT_STATUS_INDEX_RENAMED:
687 printf("m %s -> %s\n",
688 se->head_to_index->old_file.path,
689 se->head_to_index->new_file.path);
690 break;
691 case GIT_STATUS_INDEX_TYPECHANGE:
692 printf("t %s\n", se->head_to_index->old_file.path);
693 break;
694 case GIT_STATUS_CURRENT:
695 default:
696 break;
700 git_status_list_free(status);
701 git_repository_free(repo);
702 git_libgit2_shutdown();
704 return 0;
706 #endif