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)
311 const struct got_error *err = NULL;
312 char *buf;
314 err = got_object_id_str(&buf, id);
315 if (err)
316 return err;
318 printf("-----------------------------------------------\n");
319 printf("commit %s\n", buf);
320 printf("author: %s\n", commit->author);
321 if (strcmp(commit->author, commit->committer) != 0)
322 printf("committer: %s\n", commit->committer);
323 printf("\n%s\n", commit->logmsg);
325 if (show_patch) {
326 err = print_patch(commit, id, repo);
327 if (err == 0)
328 printf("\n");
331 free(buf);
332 return err;
335 static const struct got_error *
336 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
337 struct got_repository *repo, int show_patch, int limit)
339 const struct got_error *err;
340 struct got_commit_graph *graph;
341 int ncommits;
343 err = got_commit_graph_open(&graph, root_id, repo);
344 if (err)
345 return err;
346 err = got_commit_graph_iter_start(graph, root_id);
347 if (err)
348 return err;
349 do {
350 struct got_commit_object *commit;
351 struct got_object_id *id;
353 err = got_commit_graph_iter_next(&commit, &id, graph);
354 if (err) {
355 if (err->code != GOT_ERR_ITER_NEED_MORE)
356 break;
357 err = got_commit_graph_fetch_commits(&ncommits,
358 graph, 1, repo);
359 if (err)
360 break;
361 else
362 continue;
364 if (commit == NULL)
365 break;
366 err = print_commit(commit, id, repo, show_patch);
367 if (err || (limit && --limit == 0))
368 break;
369 } while (ncommits > 0);
371 got_commit_graph_close(graph);
372 return err;
375 __dead static void
376 usage_log(void)
378 fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
379 "[repository-path]\n", getprogname());
380 exit(1);
383 static const struct got_error *
384 cmd_log(int argc, char *argv[])
386 const struct got_error *error;
387 struct got_repository *repo;
388 struct got_object_id *id = NULL;
389 struct got_object *obj;
390 char *repo_path = NULL;
391 char *start_commit = NULL;
392 int ch;
393 int show_patch = 0, limit = 0;
394 const char *errstr;
396 #ifndef PROFILE
397 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
398 err(1, "pledge");
399 #endif
401 while ((ch = getopt(argc, argv, "pc:l:")) != -1) {
402 switch (ch) {
403 case 'p':
404 show_patch = 1;
405 break;
406 case 'c':
407 start_commit = optarg;
408 break;
409 case 'l':
410 limit = strtonum(optarg, 1, INT_MAX, &errstr);
411 if (errstr != NULL)
412 err(1, "-l option %s", errstr);
413 break;
414 default:
415 usage();
416 /* NOTREACHED */
420 argc -= optind;
421 argv += optind;
423 if (argc == 0) {
424 repo_path = getcwd(NULL, 0);
425 if (repo_path == NULL)
426 return got_error_from_errno();
427 } else if (argc == 1) {
428 repo_path = realpath(argv[0], NULL);
429 if (repo_path == NULL)
430 return got_error_from_errno();
431 } else
432 usage_log();
434 error = got_repo_open(&repo, repo_path);
435 free(repo_path);
436 if (error != NULL)
437 return error;
439 if (start_commit == NULL) {
440 struct got_reference *head_ref;
441 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
442 if (error != NULL)
443 return error;
444 error = got_ref_resolve(&id, repo, head_ref);
445 got_ref_close(head_ref);
446 if (error != NULL)
447 return error;
448 error = got_object_open(&obj, repo, id);
449 } else {
450 error = got_object_open_by_id_str(&obj, repo, start_commit);
451 if (error == NULL) {
452 id = got_object_get_id(obj);
453 if (id == NULL)
454 error = got_error_from_errno();
457 if (error != NULL)
458 return error;
459 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
460 error = print_commits(obj, id, repo, show_patch, limit);
461 else
462 error = got_error(GOT_ERR_OBJ_TYPE);
463 got_object_close(obj);
464 free(id);
465 got_repo_close(repo);
466 return error;
469 __dead static void
470 usage_diff(void)
472 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
473 getprogname());
474 exit(1);
477 static const struct got_error *
478 cmd_diff(int argc, char *argv[])
480 const struct got_error *error;
481 struct got_repository *repo = NULL;
482 struct got_object_id *id1 = NULL, *id2 = NULL;
483 struct got_object *obj1 = NULL, *obj2 = NULL;
484 char *repo_path = NULL;
485 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
486 int ch;
488 #ifndef PROFILE
489 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
490 err(1, "pledge");
491 #endif
493 while ((ch = getopt(argc, argv, "")) != -1) {
494 switch (ch) {
495 default:
496 usage();
497 /* NOTREACHED */
501 argc -= optind;
502 argv += optind;
504 if (argc == 0) {
505 usage_diff(); /* TODO show local worktree changes */
506 } else if (argc == 2) {
507 repo_path = getcwd(NULL, 0);
508 if (repo_path == NULL)
509 return got_error_from_errno();
510 obj_id_str1 = argv[0];
511 obj_id_str2 = argv[1];
512 } else if (argc == 3) {
513 repo_path = realpath(argv[0], NULL);
514 if (repo_path == NULL)
515 return got_error_from_errno();
516 obj_id_str1 = argv[1];
517 obj_id_str2 = argv[2];
518 } else
519 usage_diff();
521 error = got_repo_open(&repo, repo_path);
522 free(repo_path);
523 if (error != NULL)
524 goto done;
526 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
527 if (error == NULL) {
528 id1 = got_object_get_id(obj1);
529 if (id1 == NULL)
530 error = got_error_from_errno();
532 if (error != NULL)
533 goto done;
535 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
536 if (error == NULL) {
537 id2 = got_object_get_id(obj2);
538 if (id2 == NULL)
539 error = got_error_from_errno();
541 if (error != NULL)
542 goto done;
544 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
545 error = got_error(GOT_ERR_OBJ_TYPE);
546 goto done;
549 switch (got_object_get_type(obj1)) {
550 case GOT_OBJ_TYPE_BLOB:
551 error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
552 break;
553 case GOT_OBJ_TYPE_TREE:
554 error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
555 break;
556 case GOT_OBJ_TYPE_COMMIT:
557 error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
558 break;
559 default:
560 error = got_error(GOT_ERR_OBJ_TYPE);
563 done:
564 if (obj1)
565 got_object_close(obj1);
566 if (obj2)
567 got_object_close(obj2);
568 if (id1)
569 free(id1);
570 if (id2)
571 free(id2);
572 if (repo)
573 got_repo_close(repo);
574 return error;
577 #ifdef notyet
578 static const struct got_error *
579 cmd_status(int argc __unused, char *argv[] __unused)
581 git_repository *repo = NULL;
582 git_status_list *status;
583 git_status_options statusopts;
584 size_t i;
586 git_libgit2_init();
588 if (git_repository_open_ext(&repo, ".", 0, NULL))
589 errx(1, "git_repository_open: %s", giterr_last()->message);
591 if (git_repository_is_bare(repo))
592 errx(1, "bar repository");
594 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
595 errx(1, "git_status_init_options: %s", giterr_last()->message);
597 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
598 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
599 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
600 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
602 if (git_status_list_new(&status, repo, &statusopts))
603 errx(1, "git_status_list_new: %s", giterr_last()->message);
605 for (i = 0; i < git_status_list_entrycount(status); i++) {
606 const git_status_entry *se;
608 se = git_status_byindex(status, i);
609 switch (se->status) {
610 case GIT_STATUS_WT_NEW:
611 printf("? %s\n", se->index_to_workdir->new_file.path);
612 break;
613 case GIT_STATUS_WT_MODIFIED:
614 printf("M %s\n", se->index_to_workdir->new_file.path);
615 break;
616 case GIT_STATUS_WT_DELETED:
617 printf("R %s\n", se->index_to_workdir->new_file.path);
618 break;
619 case GIT_STATUS_WT_RENAMED:
620 printf("m %s -> %s\n",
621 se->index_to_workdir->old_file.path,
622 se->index_to_workdir->new_file.path);
623 break;
624 case GIT_STATUS_WT_TYPECHANGE:
625 printf("t %s\n", se->index_to_workdir->new_file.path);
626 break;
627 case GIT_STATUS_INDEX_NEW:
628 printf("A %s\n", se->head_to_index->new_file.path);
629 break;
630 case GIT_STATUS_INDEX_MODIFIED:
631 printf("M %s\n", se->head_to_index->old_file.path);
632 break;
633 case GIT_STATUS_INDEX_DELETED:
634 printf("R %s\n", se->head_to_index->old_file.path);
635 break;
636 case GIT_STATUS_INDEX_RENAMED:
637 printf("m %s -> %s\n",
638 se->head_to_index->old_file.path,
639 se->head_to_index->new_file.path);
640 break;
641 case GIT_STATUS_INDEX_TYPECHANGE:
642 printf("t %s\n", se->head_to_index->old_file.path);
643 break;
644 case GIT_STATUS_CURRENT:
645 default:
646 break;
650 git_status_list_free(status);
651 git_repository_free(repo);
652 git_libgit2_shutdown();
654 return 0;
656 #endif