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 *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);
325 logmsg = strdup(commit->logmsg);
326 if (logmsg == NULL)
327 return got_error_from_errno();
329 do {
330 line = strsep(&logmsg, "\n");
331 if (line)
332 printf(" %s\n", line);
333 } while (line);
334 free(logmsg);
336 if (show_patch) {
337 err = print_patch(commit, id, repo);
338 if (err == 0)
339 printf("\n");
342 return err;
345 static const struct got_error *
346 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
347 struct got_repository *repo, int show_patch, int limit)
349 const struct got_error *err;
350 struct got_commit_graph *graph;
351 int ncommits;
353 err = got_commit_graph_open(&graph, root_id, repo);
354 if (err)
355 return err;
356 err = got_commit_graph_iter_start(graph, root_id);
357 if (err)
358 return err;
359 do {
360 struct got_commit_object *commit;
361 struct got_object_id *id;
363 err = got_commit_graph_iter_next(&commit, &id, graph);
364 if (err) {
365 if (err->code != GOT_ERR_ITER_NEED_MORE)
366 break;
367 err = got_commit_graph_fetch_commits(&ncommits,
368 graph, 1, repo);
369 if (err)
370 break;
371 else
372 continue;
374 if (commit == NULL)
375 break;
376 err = print_commit(commit, id, repo, show_patch);
377 if (err || (limit && --limit == 0))
378 break;
379 } while (ncommits > 0);
381 got_commit_graph_close(graph);
382 return err;
385 __dead static void
386 usage_log(void)
388 fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
389 "[repository-path]\n", getprogname());
390 exit(1);
393 static const struct got_error *
394 cmd_log(int argc, char *argv[])
396 const struct got_error *error;
397 struct got_repository *repo;
398 struct got_object_id *id = NULL;
399 struct got_object *obj;
400 char *repo_path = NULL;
401 char *start_commit = NULL;
402 int ch;
403 int show_patch = 0, limit = 0;
404 const char *errstr;
406 #ifndef PROFILE
407 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
408 err(1, "pledge");
409 #endif
411 while ((ch = getopt(argc, argv, "pc:l:")) != -1) {
412 switch (ch) {
413 case 'p':
414 show_patch = 1;
415 break;
416 case 'c':
417 start_commit = optarg;
418 break;
419 case 'l':
420 limit = strtonum(optarg, 1, INT_MAX, &errstr);
421 if (errstr != NULL)
422 err(1, "-l option %s", errstr);
423 break;
424 default:
425 usage();
426 /* NOTREACHED */
430 argc -= optind;
431 argv += optind;
433 if (argc == 0) {
434 repo_path = getcwd(NULL, 0);
435 if (repo_path == NULL)
436 return got_error_from_errno();
437 } else if (argc == 1) {
438 repo_path = realpath(argv[0], NULL);
439 if (repo_path == NULL)
440 return got_error_from_errno();
441 } else
442 usage_log();
444 error = got_repo_open(&repo, repo_path);
445 free(repo_path);
446 if (error != NULL)
447 return error;
449 if (start_commit == NULL) {
450 struct got_reference *head_ref;
451 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
452 if (error != NULL)
453 return error;
454 error = got_ref_resolve(&id, repo, head_ref);
455 got_ref_close(head_ref);
456 if (error != NULL)
457 return error;
458 error = got_object_open(&obj, repo, id);
459 } else {
460 error = got_object_open_by_id_str(&obj, repo, start_commit);
461 if (error == NULL) {
462 id = got_object_get_id(obj);
463 if (id == NULL)
464 error = got_error_from_errno();
467 if (error != NULL)
468 return error;
469 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
470 error = print_commits(obj, id, repo, show_patch, limit);
471 else
472 error = got_error(GOT_ERR_OBJ_TYPE);
473 got_object_close(obj);
474 free(id);
475 got_repo_close(repo);
476 return error;
479 __dead static void
480 usage_diff(void)
482 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
483 getprogname());
484 exit(1);
487 static const struct got_error *
488 cmd_diff(int argc, char *argv[])
490 const struct got_error *error;
491 struct got_repository *repo = NULL;
492 struct got_object_id *id1 = NULL, *id2 = NULL;
493 struct got_object *obj1 = NULL, *obj2 = NULL;
494 char *repo_path = NULL;
495 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
496 int ch;
498 #ifndef PROFILE
499 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
500 err(1, "pledge");
501 #endif
503 while ((ch = getopt(argc, argv, "")) != -1) {
504 switch (ch) {
505 default:
506 usage();
507 /* NOTREACHED */
511 argc -= optind;
512 argv += optind;
514 if (argc == 0) {
515 usage_diff(); /* TODO show local worktree changes */
516 } else if (argc == 2) {
517 repo_path = getcwd(NULL, 0);
518 if (repo_path == NULL)
519 return got_error_from_errno();
520 obj_id_str1 = argv[0];
521 obj_id_str2 = argv[1];
522 } else if (argc == 3) {
523 repo_path = realpath(argv[0], NULL);
524 if (repo_path == NULL)
525 return got_error_from_errno();
526 obj_id_str1 = argv[1];
527 obj_id_str2 = argv[2];
528 } else
529 usage_diff();
531 error = got_repo_open(&repo, repo_path);
532 free(repo_path);
533 if (error != NULL)
534 goto done;
536 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
537 if (error == NULL) {
538 id1 = got_object_get_id(obj1);
539 if (id1 == NULL)
540 error = got_error_from_errno();
542 if (error != NULL)
543 goto done;
545 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
546 if (error == NULL) {
547 id2 = got_object_get_id(obj2);
548 if (id2 == NULL)
549 error = got_error_from_errno();
551 if (error != NULL)
552 goto done;
554 if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
555 error = got_error(GOT_ERR_OBJ_TYPE);
556 goto done;
559 switch (got_object_get_type(obj1)) {
560 case GOT_OBJ_TYPE_BLOB:
561 error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
562 break;
563 case GOT_OBJ_TYPE_TREE:
564 error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
565 break;
566 case GOT_OBJ_TYPE_COMMIT:
567 error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
568 break;
569 default:
570 error = got_error(GOT_ERR_OBJ_TYPE);
573 done:
574 if (obj1)
575 got_object_close(obj1);
576 if (obj2)
577 got_object_close(obj2);
578 if (id1)
579 free(id1);
580 if (id2)
581 free(id2);
582 if (repo)
583 got_repo_close(repo);
584 return error;
587 #ifdef notyet
588 static const struct got_error *
589 cmd_status(int argc __unused, char *argv[] __unused)
591 git_repository *repo = NULL;
592 git_status_list *status;
593 git_status_options statusopts;
594 size_t i;
596 git_libgit2_init();
598 if (git_repository_open_ext(&repo, ".", 0, NULL))
599 errx(1, "git_repository_open: %s", giterr_last()->message);
601 if (git_repository_is_bare(repo))
602 errx(1, "bar repository");
604 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
605 errx(1, "git_status_init_options: %s", giterr_last()->message);
607 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
608 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
609 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
610 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
612 if (git_status_list_new(&status, repo, &statusopts))
613 errx(1, "git_status_list_new: %s", giterr_last()->message);
615 for (i = 0; i < git_status_list_entrycount(status); i++) {
616 const git_status_entry *se;
618 se = git_status_byindex(status, i);
619 switch (se->status) {
620 case GIT_STATUS_WT_NEW:
621 printf("? %s\n", se->index_to_workdir->new_file.path);
622 break;
623 case GIT_STATUS_WT_MODIFIED:
624 printf("M %s\n", se->index_to_workdir->new_file.path);
625 break;
626 case GIT_STATUS_WT_DELETED:
627 printf("R %s\n", se->index_to_workdir->new_file.path);
628 break;
629 case GIT_STATUS_WT_RENAMED:
630 printf("m %s -> %s\n",
631 se->index_to_workdir->old_file.path,
632 se->index_to_workdir->new_file.path);
633 break;
634 case GIT_STATUS_WT_TYPECHANGE:
635 printf("t %s\n", se->index_to_workdir->new_file.path);
636 break;
637 case GIT_STATUS_INDEX_NEW:
638 printf("A %s\n", se->head_to_index->new_file.path);
639 break;
640 case GIT_STATUS_INDEX_MODIFIED:
641 printf("M %s\n", se->head_to_index->old_file.path);
642 break;
643 case GIT_STATUS_INDEX_DELETED:
644 printf("R %s\n", se->head_to_index->old_file.path);
645 break;
646 case GIT_STATUS_INDEX_RENAMED:
647 printf("m %s -> %s\n",
648 se->head_to_index->old_file.path,
649 se->head_to_index->new_file.path);
650 break;
651 case GIT_STATUS_INDEX_TYPECHANGE:
652 printf("t %s\n", se->head_to_index->old_file.path);
653 break;
654 case GIT_STATUS_CURRENT:
655 default:
656 break;
660 git_status_list_free(status);
661 git_repository_free(repo);
662 git_libgit2_shutdown();
664 return 0;
666 #endif