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_parent_id *pid;
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 pid = SIMPLEQ_FIRST(&commit->parent_ids);
280 if (pid != NULL) {
281 struct got_commit_object *pcommit;
283 err = got_object_open(&obj, repo, pid->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 static const struct got_error *
310 print_commit(struct got_commit_object *commit, struct got_object_id *id,
311 struct got_repository *repo, int show_patch, int verbose)
313 const struct got_error *err = NULL;
314 char *id_str, *logmsg, *line;
316 err = got_object_id_str(&id_str, id);
317 if (err)
318 return err;
320 printf("-----------------------------------------------\n");
321 printf("commit %s\n", id_str);
322 free(id_str);
323 printf("author: %s %s", commit->author, ctime(&commit->author_time));
324 if (strcmp(commit->author, commit->committer) != 0)
325 printf("committer: %s %s\n", commit->committer,
326 ctime(&commit->committer_time));
327 if (commit->nparents > 1) {
328 struct got_parent_id *pid;
329 int n = 1;
330 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
331 err = got_object_id_str(&id_str, pid->id);
332 if (err)
333 return err;
334 printf("parent %d: %s\n", n++, id_str);
335 free(id_str);
339 logmsg = strdup(commit->logmsg);
340 if (logmsg == NULL)
341 return got_error_from_errno();
343 do {
344 line = strsep(&logmsg, "\n");
345 if (line)
346 printf(" %s\n", line);
347 } while (line);
348 free(logmsg);
350 if (show_patch) {
351 err = print_patch(commit, id, repo);
352 if (err == 0)
353 printf("\n");
356 return err;
359 static const struct got_error *
360 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
361 struct got_repository *repo, int show_patch, int limit, int verbose)
363 const struct got_error *err;
364 struct got_commit_graph *graph;
365 int ncommits;
367 err = got_commit_graph_open(&graph, root_id, repo);
368 if (err)
369 return err;
370 err = got_commit_graph_iter_start(graph, root_id);
371 if (err)
372 return err;
373 do {
374 struct got_commit_object *commit;
375 struct got_object_id *id;
377 err = got_commit_graph_iter_next(&commit, &id, graph);
378 if (err) {
379 if (err->code != GOT_ERR_ITER_NEED_MORE)
380 break;
381 err = got_commit_graph_fetch_commits(&ncommits,
382 graph, 1, repo);
383 if (err)
384 break;
385 else
386 continue;
388 if (commit == NULL)
389 break;
390 err = print_commit(commit, id, repo, show_patch, verbose);
391 if (err || (limit && --limit == 0))
392 break;
393 } while (ncommits > 0);
395 got_commit_graph_close(graph);
396 return err;
399 __dead static void
400 usage_log(void)
402 fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
403 "[repository-path]\n", getprogname());
404 exit(1);
407 static const struct got_error *
408 cmd_log(int argc, char *argv[])
410 const struct got_error *error;
411 struct got_repository *repo;
412 struct got_object_id *id = NULL;
413 struct got_object *obj;
414 char *repo_path = NULL;
415 char *start_commit = NULL;
416 int ch;
417 int show_patch = 0, limit = 0, verbose = 0;
418 const char *errstr;
420 #ifndef PROFILE
421 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
422 err(1, "pledge");
423 #endif
425 while ((ch = getopt(argc, argv, "pc:l:v")) != -1) {
426 switch (ch) {
427 case 'p':
428 show_patch = 1;
429 break;
430 case 'c':
431 start_commit = optarg;
432 break;
433 case 'l':
434 limit = strtonum(optarg, 1, INT_MAX, &errstr);
435 if (errstr != NULL)
436 err(1, "-l option %s", errstr);
437 break;
438 case 'v':
439 verbose = 1;
440 break;
441 default:
442 usage();
443 /* NOTREACHED */
447 argc -= optind;
448 argv += optind;
450 if (argc == 0) {
451 repo_path = getcwd(NULL, 0);
452 if (repo_path == NULL)
453 return got_error_from_errno();
454 } else if (argc == 1) {
455 repo_path = realpath(argv[0], NULL);
456 if (repo_path == NULL)
457 return got_error_from_errno();
458 } else
459 usage_log();
461 error = got_repo_open(&repo, repo_path);
462 free(repo_path);
463 if (error != NULL)
464 return error;
466 if (start_commit == NULL) {
467 struct got_reference *head_ref;
468 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
469 if (error != NULL)
470 return error;
471 error = got_ref_resolve(&id, repo, head_ref);
472 got_ref_close(head_ref);
473 if (error != NULL)
474 return error;
475 error = got_object_open(&obj, repo, id);
476 } else {
477 error = got_object_open_by_id_str(&obj, repo, start_commit);
478 if (error == NULL) {
479 id = got_object_get_id(obj);
480 if (id == NULL)
481 error = got_error_from_errno();
484 if (error != NULL)
485 return error;
486 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
487 error = print_commits(obj, id, repo, show_patch, limit,
488 verbose);
489 else
490 error = got_error(GOT_ERR_OBJ_TYPE);
491 got_object_close(obj);
492 free(id);
493 got_repo_close(repo);
494 return error;
497 __dead static void
498 usage_diff(void)
500 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
501 getprogname());
502 exit(1);
505 static const struct got_error *
506 cmd_diff(int argc, char *argv[])
508 const struct got_error *error;
509 struct got_repository *repo = NULL;
510 struct got_object_id *id1 = NULL, *id2 = NULL;
511 struct got_object *obj1 = NULL, *obj2 = NULL;
512 char *repo_path = NULL;
513 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
514 int ch;
516 #ifndef PROFILE
517 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
518 err(1, "pledge");
519 #endif
521 while ((ch = getopt(argc, argv, "")) != -1) {
522 switch (ch) {
523 default:
524 usage();
525 /* NOTREACHED */
529 argc -= optind;
530 argv += optind;
532 if (argc == 0) {
533 usage_diff(); /* TODO show local worktree changes */
534 } else if (argc == 2) {
535 repo_path = getcwd(NULL, 0);
536 if (repo_path == NULL)
537 return got_error_from_errno();
538 obj_id_str1 = argv[0];
539 obj_id_str2 = argv[1];
540 } else if (argc == 3) {
541 repo_path = realpath(argv[0], NULL);
542 if (repo_path == NULL)
543 return got_error_from_errno();
544 obj_id_str1 = argv[1];
545 obj_id_str2 = argv[2];
546 } else
547 usage_diff();
549 error = got_repo_open(&repo, repo_path);
550 free(repo_path);
551 if (error != NULL)
552 goto done;
554 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
555 if (error == NULL) {
556 id1 = got_object_get_id(obj1);
557 if (id1 == NULL)
558 error = got_error_from_errno();
560 if (error != NULL)
561 goto done;
563 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
564 if (error == NULL) {
565 id2 = got_object_get_id(obj2);
566 if (id2 == NULL)
567 error = got_error_from_errno();
569 if (error != NULL)
570 goto done;
572 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
573 error = got_error(GOT_ERR_OBJ_TYPE);
574 goto done;
577 switch (got_object_get_type(obj1)) {
578 case GOT_OBJ_TYPE_BLOB:
579 error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
580 break;
581 case GOT_OBJ_TYPE_TREE:
582 error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
583 break;
584 case GOT_OBJ_TYPE_COMMIT:
585 error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
586 break;
587 default:
588 error = got_error(GOT_ERR_OBJ_TYPE);
591 done:
592 if (obj1)
593 got_object_close(obj1);
594 if (obj2)
595 got_object_close(obj2);
596 if (id1)
597 free(id1);
598 if (id2)
599 free(id2);
600 if (repo)
601 got_repo_close(repo);
602 return error;
605 #ifdef notyet
606 static const struct got_error *
607 cmd_status(int argc __unused, char *argv[] __unused)
609 git_repository *repo = NULL;
610 git_status_list *status;
611 git_status_options statusopts;
612 size_t i;
614 git_libgit2_init();
616 if (git_repository_open_ext(&repo, ".", 0, NULL))
617 errx(1, "git_repository_open: %s", giterr_last()->message);
619 if (git_repository_is_bare(repo))
620 errx(1, "bar repository");
622 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
623 errx(1, "git_status_init_options: %s", giterr_last()->message);
625 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
626 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
627 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
628 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
630 if (git_status_list_new(&status, repo, &statusopts))
631 errx(1, "git_status_list_new: %s", giterr_last()->message);
633 for (i = 0; i < git_status_list_entrycount(status); i++) {
634 const git_status_entry *se;
636 se = git_status_byindex(status, i);
637 switch (se->status) {
638 case GIT_STATUS_WT_NEW:
639 printf("? %s\n", se->index_to_workdir->new_file.path);
640 break;
641 case GIT_STATUS_WT_MODIFIED:
642 printf("M %s\n", se->index_to_workdir->new_file.path);
643 break;
644 case GIT_STATUS_WT_DELETED:
645 printf("R %s\n", se->index_to_workdir->new_file.path);
646 break;
647 case GIT_STATUS_WT_RENAMED:
648 printf("m %s -> %s\n",
649 se->index_to_workdir->old_file.path,
650 se->index_to_workdir->new_file.path);
651 break;
652 case GIT_STATUS_WT_TYPECHANGE:
653 printf("t %s\n", se->index_to_workdir->new_file.path);
654 break;
655 case GIT_STATUS_INDEX_NEW:
656 printf("A %s\n", se->head_to_index->new_file.path);
657 break;
658 case GIT_STATUS_INDEX_MODIFIED:
659 printf("M %s\n", se->head_to_index->old_file.path);
660 break;
661 case GIT_STATUS_INDEX_DELETED:
662 printf("R %s\n", se->head_to_index->old_file.path);
663 break;
664 case GIT_STATUS_INDEX_RENAMED:
665 printf("m %s -> %s\n",
666 se->head_to_index->old_file.path,
667 se->head_to_index->new_file.path);
668 break;
669 case GIT_STATUS_INDEX_TYPECHANGE:
670 printf("t %s\n", se->head_to_index->old_file.path);
671 break;
672 case GIT_STATUS_CURRENT:
673 default:
674 break;
678 git_status_list_free(status);
679 git_repository_free(repo);
680 git_libgit2_shutdown();
682 return 0;
684 #endif