Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 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>
21 #include <sys/stat.h>
22 #include <sys/param.h>
23 #include <sys/wait.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <locale.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <libgen.h>
34 #include <time.h>
35 #include <paths.h>
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_reference.h"
40 #include "got_repository.h"
41 #include "got_path.h"
42 #include "got_worktree.h"
43 #include "got_diff.h"
44 #include "got_commit_graph.h"
45 #include "got_blame.h"
46 #include "got_privsep.h"
47 #include "got_opentemp.h"
49 #ifndef nitems
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 #endif
53 static volatile sig_atomic_t sigint_received;
54 static volatile sig_atomic_t sigpipe_received;
56 static void
57 catch_sigint(int signo)
58 {
59 sigint_received = 1;
60 }
62 static void
63 catch_sigpipe(int signo)
64 {
65 sigpipe_received = 1;
66 }
69 struct cmd {
70 const char *cmd_name;
71 const struct got_error *(*cmd_main)(int, char *[]);
72 void (*cmd_usage)(void);
73 const char *cmd_descr;
74 };
76 __dead static void usage(void);
77 __dead static void usage_checkout(void);
78 __dead static void usage_update(void);
79 __dead static void usage_log(void);
80 __dead static void usage_diff(void);
81 __dead static void usage_blame(void);
82 __dead static void usage_tree(void);
83 __dead static void usage_status(void);
84 __dead static void usage_ref(void);
85 __dead static void usage_add(void);
86 __dead static void usage_rm(void);
87 __dead static void usage_revert(void);
88 __dead static void usage_commit(void);
89 __dead static void usage_cherrypick(void);
91 static const struct got_error* cmd_checkout(int, char *[]);
92 static const struct got_error* cmd_update(int, char *[]);
93 static const struct got_error* cmd_log(int, char *[]);
94 static const struct got_error* cmd_diff(int, char *[]);
95 static const struct got_error* cmd_blame(int, char *[]);
96 static const struct got_error* cmd_tree(int, char *[]);
97 static const struct got_error* cmd_status(int, char *[]);
98 static const struct got_error* cmd_ref(int, char *[]);
99 static const struct got_error* cmd_add(int, char *[]);
100 static const struct got_error* cmd_rm(int, char *[]);
101 static const struct got_error* cmd_revert(int, char *[]);
102 static const struct got_error* cmd_commit(int, char *[]);
103 static const struct got_error* cmd_cherrypick(int, char *[]);
105 static struct cmd got_commands[] = {
106 { "checkout", cmd_checkout, usage_checkout,
107 "check out a new work tree from a repository" },
108 { "update", cmd_update, usage_update,
109 "update a work tree to a different commit" },
110 { "log", cmd_log, usage_log,
111 "show repository history" },
112 { "diff", cmd_diff, usage_diff,
113 "compare files and directories" },
114 { "blame", cmd_blame, usage_blame,
115 "show when lines in a file were changed" },
116 { "tree", cmd_tree, usage_tree,
117 "list files and directories in repository" },
118 { "status", cmd_status, usage_status,
119 "show modification status of files" },
120 { "ref", cmd_ref, usage_ref,
121 "manage references in repository" },
122 { "add", cmd_add, usage_add,
123 "add new files to version control" },
124 { "rm", cmd_rm, usage_rm,
125 "remove a versioned file" },
126 { "revert", cmd_revert, usage_revert,
127 "revert uncommitted changes" },
128 { "commit", cmd_commit, usage_commit,
129 "write changes from work tree to repository" },
130 { "cherrypick", cmd_cherrypick, usage_cherrypick,
131 "merge a single commit from another branch into a work tree" },
132 };
134 int
135 main(int argc, char *argv[])
137 struct cmd *cmd;
138 unsigned int i;
139 int ch;
140 int hflag = 0;
142 setlocale(LC_CTYPE, "");
144 while ((ch = getopt(argc, argv, "h")) != -1) {
145 switch (ch) {
146 case 'h':
147 hflag = 1;
148 break;
149 default:
150 usage();
151 /* NOTREACHED */
155 argc -= optind;
156 argv += optind;
157 optind = 0;
159 if (argc <= 0)
160 usage();
162 signal(SIGINT, catch_sigint);
163 signal(SIGPIPE, catch_sigpipe);
165 for (i = 0; i < nitems(got_commands); i++) {
166 const struct got_error *error;
168 cmd = &got_commands[i];
170 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
171 continue;
173 if (hflag)
174 got_commands[i].cmd_usage();
176 error = got_commands[i].cmd_main(argc, argv);
177 if (error && !(sigint_received || sigpipe_received)) {
178 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
179 return 1;
182 return 0;
185 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
186 return 1;
189 __dead static void
190 usage(void)
192 int i;
194 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
195 "Available commands:\n", getprogname());
196 for (i = 0; i < nitems(got_commands); i++) {
197 struct cmd *cmd = &got_commands[i];
198 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
200 exit(1);
203 static const struct got_error *
204 get_editor(char **abspath)
206 const struct got_error *err = NULL;
207 const char *editor;
209 editor = getenv("VISUAL");
210 if (editor == NULL)
211 editor = getenv("EDITOR");
213 if (editor) {
214 err = got_path_find_prog(abspath, editor);
215 if (err)
216 return err;
219 if (*abspath == NULL) {
220 *abspath = strdup("/bin/ed");
221 if (*abspath == NULL)
222 return got_error_from_errno("strdup");
225 return NULL;
228 static const struct got_error *
229 apply_unveil(const char *repo_path, int repo_read_only,
230 const char *worktree_path, int create_worktree)
232 const struct got_error *err;
234 if (create_worktree) {
235 /* Pre-create work tree path to avoid unveiling its parents. */
236 err = got_path_mkdir(worktree_path);
238 if (errno == EEXIST) {
239 if (got_path_dir_is_empty(worktree_path)) {
240 errno = 0;
241 err = NULL;
242 } else {
243 err = got_error_path(worktree_path,
244 GOT_ERR_DIR_NOT_EMPTY);
248 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
249 return err;
252 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
253 return got_error_from_errno2("unveil", repo_path);
255 if (worktree_path && unveil(worktree_path, "rwc") != 0)
256 return got_error_from_errno2("unveil", worktree_path);
258 if (unveil("/tmp", "rwc") != 0)
259 return got_error_from_errno2("unveil", "/tmp");
261 err = got_privsep_unveil_exec_helpers();
262 if (err != NULL)
263 return err;
265 if (unveil(NULL, NULL) != 0)
266 return got_error_from_errno("unveil");
268 return NULL;
271 __dead static void
272 usage_checkout(void)
274 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
275 "[-p prefix] repository-path [worktree-path]\n", getprogname());
276 exit(1);
279 static void
280 checkout_progress(void *arg, unsigned char status, const char *path)
282 char *worktree_path = arg;
284 while (path[0] == '/')
285 path++;
287 printf("%c %s/%s\n", status, worktree_path, path);
290 static const struct got_error *
291 check_cancelled(void *arg)
293 if (sigint_received || sigpipe_received)
294 return got_error(GOT_ERR_CANCELLED);
295 return NULL;
298 static const struct got_error *
299 check_linear_ancestry(struct got_object_id *commit_id,
300 struct got_object_id *base_commit_id, struct got_repository *repo)
302 const struct got_error *err = NULL;
303 struct got_object_id *yca_id;
305 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
306 commit_id, base_commit_id, repo);
307 if (err)
308 return err;
310 if (yca_id == NULL)
311 return got_error(GOT_ERR_ANCESTRY);
313 /*
314 * Require a straight line of history between the target commit
315 * and the work tree's base commit.
317 * Non-linear situations such as this require a rebase:
319 * (commit) D F (base_commit)
320 * \ /
321 * C E
322 * \ /
323 * B (yca)
324 * |
325 * A
327 * 'got update' only handles linear cases:
328 * Update forwards in time: A (base/yca) - B - C - D (commit)
329 * Update backwards in time: D (base) - C - B - A (commit/yca)
330 */
331 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
332 got_object_id_cmp(base_commit_id, yca_id) != 0)
333 return got_error(GOT_ERR_ANCESTRY);
335 free(yca_id);
336 return NULL;
339 static const struct got_error *
340 check_same_branch(struct got_object_id *commit_id,
341 struct got_reference *head_ref, struct got_repository *repo)
343 const struct got_error *err = NULL;
344 struct got_commit_graph *graph = NULL;
345 struct got_object_id *head_commit_id = NULL;
346 int is_same_branch = 0;
348 err = got_ref_resolve(&head_commit_id, repo, head_ref);
349 if (err)
350 goto done;
352 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
353 if (err)
354 goto done;
356 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
357 if (err)
358 goto done;
360 for (;;) {
361 struct got_object_id *id;
362 err = got_commit_graph_iter_next(&id, graph);
363 if (err) {
364 if (err->code == GOT_ERR_ITER_COMPLETED) {
365 err = NULL;
366 break;
368 else if (err->code != GOT_ERR_ITER_NEED_MORE)
369 break;
370 err = got_commit_graph_fetch_commits(graph, 1,
371 repo);
372 if (err)
373 break;
376 if (id) {
377 if (got_object_id_cmp(id, commit_id) == 0) {
378 is_same_branch = 1;
379 break;
383 done:
384 if (graph)
385 got_commit_graph_close(graph);
386 free(head_commit_id);
387 if (!err && !is_same_branch)
388 err = got_error(GOT_ERR_ANCESTRY);
389 return err;
392 static const struct got_error *
393 cmd_checkout(int argc, char *argv[])
395 const struct got_error *error = NULL;
396 struct got_repository *repo = NULL;
397 struct got_reference *head_ref = NULL;
398 struct got_worktree *worktree = NULL;
399 char *repo_path = NULL;
400 char *worktree_path = NULL;
401 const char *path_prefix = "";
402 const char *branch_name = GOT_REF_HEAD;
403 char *commit_id_str = NULL;
404 int ch, same_path_prefix;
406 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
407 switch (ch) {
408 case 'b':
409 branch_name = optarg;
410 break;
411 case 'c':
412 commit_id_str = strdup(optarg);
413 if (commit_id_str == NULL)
414 return got_error_from_errno("strdup");
415 break;
416 case 'p':
417 path_prefix = optarg;
418 break;
419 default:
420 usage_checkout();
421 /* NOTREACHED */
425 argc -= optind;
426 argv += optind;
428 #ifndef PROFILE
429 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
430 "unveil", NULL) == -1)
431 err(1, "pledge");
432 #endif
433 if (argc == 1) {
434 char *cwd, *base, *dotgit;
435 repo_path = realpath(argv[0], NULL);
436 if (repo_path == NULL)
437 return got_error_from_errno2("realpath", argv[0]);
438 cwd = getcwd(NULL, 0);
439 if (cwd == NULL) {
440 error = got_error_from_errno("getcwd");
441 goto done;
443 if (path_prefix[0]) {
444 base = basename(path_prefix);
445 if (base == NULL) {
446 error = got_error_from_errno2("basename",
447 path_prefix);
448 goto done;
450 } else {
451 base = basename(repo_path);
452 if (base == NULL) {
453 error = got_error_from_errno2("basename",
454 repo_path);
455 goto done;
458 dotgit = strstr(base, ".git");
459 if (dotgit)
460 *dotgit = '\0';
461 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
462 error = got_error_from_errno("asprintf");
463 free(cwd);
464 goto done;
466 free(cwd);
467 } else if (argc == 2) {
468 repo_path = realpath(argv[0], NULL);
469 if (repo_path == NULL) {
470 error = got_error_from_errno2("realpath", argv[0]);
471 goto done;
473 worktree_path = realpath(argv[1], NULL);
474 if (worktree_path == NULL) {
475 error = got_error_from_errno2("realpath", argv[1]);
476 goto done;
478 } else
479 usage_checkout();
481 got_path_strip_trailing_slashes(repo_path);
482 got_path_strip_trailing_slashes(worktree_path);
484 error = got_repo_open(&repo, repo_path);
485 if (error != NULL)
486 goto done;
488 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
489 if (error)
490 goto done;
492 error = got_ref_open(&head_ref, repo, branch_name, 0);
493 if (error != NULL)
494 goto done;
496 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
497 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
498 goto done;
500 error = got_worktree_open(&worktree, worktree_path);
501 if (error != NULL)
502 goto done;
504 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
505 path_prefix);
506 if (error != NULL)
507 goto done;
508 if (!same_path_prefix) {
509 error = got_error(GOT_ERR_PATH_PREFIX);
510 goto done;
513 if (commit_id_str) {
514 struct got_object_id *commit_id;
515 error = got_object_resolve_id_str(&commit_id, repo,
516 commit_id_str);
517 if (error != NULL)
518 goto done;
519 error = check_linear_ancestry(commit_id,
520 got_worktree_get_base_commit_id(worktree), repo);
521 if (error != NULL) {
522 free(commit_id);
523 goto done;
525 error = check_same_branch(commit_id, head_ref, repo);
526 if (error)
527 goto done;
528 error = got_worktree_set_base_commit_id(worktree, repo,
529 commit_id);
530 free(commit_id);
531 if (error)
532 goto done;
535 error = got_worktree_checkout_files(worktree, "", repo,
536 checkout_progress, worktree_path, check_cancelled, NULL);
537 if (error != NULL)
538 goto done;
540 printf("Now shut up and hack\n");
542 done:
543 free(commit_id_str);
544 free(repo_path);
545 free(worktree_path);
546 return error;
549 __dead static void
550 usage_update(void)
552 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
553 getprogname());
554 exit(1);
557 static void
558 update_progress(void *arg, unsigned char status, const char *path)
560 int *did_something = arg;
562 if (status == GOT_STATUS_EXISTS)
563 return;
565 *did_something = 1;
566 while (path[0] == '/')
567 path++;
568 printf("%c %s\n", status, path);
571 static const struct got_error *
572 switch_head_ref(struct got_reference *head_ref,
573 struct got_object_id *commit_id, struct got_worktree *worktree,
574 struct got_repository *repo)
576 const struct got_error *err = NULL;
577 char *base_id_str;
578 int ref_has_moved = 0;
580 /* Trivial case: switching between two different references. */
581 if (strcmp(got_ref_get_name(head_ref),
582 got_worktree_get_head_ref_name(worktree)) != 0) {
583 printf("Switching work tree from %s to %s\n",
584 got_worktree_get_head_ref_name(worktree),
585 got_ref_get_name(head_ref));
586 return got_worktree_set_head_ref(worktree, head_ref);
589 err = check_linear_ancestry(commit_id,
590 got_worktree_get_base_commit_id(worktree), repo);
591 if (err) {
592 if (err->code != GOT_ERR_ANCESTRY)
593 return err;
594 ref_has_moved = 1;
596 if (!ref_has_moved)
597 return NULL;
599 /* Switching to a rebased branch with the same reference name. */
600 err = got_object_id_str(&base_id_str,
601 got_worktree_get_base_commit_id(worktree));
602 if (err)
603 return err;
604 printf("Reference %s now points at a different branch\n",
605 got_worktree_get_head_ref_name(worktree));
606 printf("Switching work tree from %s to %s\n", base_id_str,
607 got_worktree_get_head_ref_name(worktree));
608 return NULL;
611 static const struct got_error *
612 cmd_update(int argc, char *argv[])
614 const struct got_error *error = NULL;
615 struct got_repository *repo = NULL;
616 struct got_worktree *worktree = NULL;
617 char *worktree_path = NULL, *path = NULL;
618 struct got_object_id *commit_id = NULL;
619 char *commit_id_str = NULL;
620 const char *branch_name = NULL;
621 struct got_reference *head_ref = NULL;
622 int ch, did_something = 0;
624 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
625 switch (ch) {
626 case 'b':
627 branch_name = optarg;
628 break;
629 case 'c':
630 commit_id_str = strdup(optarg);
631 if (commit_id_str == NULL)
632 return got_error_from_errno("strdup");
633 break;
634 default:
635 usage_update();
636 /* NOTREACHED */
640 argc -= optind;
641 argv += optind;
643 #ifndef PROFILE
644 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
645 "unveil", NULL) == -1)
646 err(1, "pledge");
647 #endif
648 worktree_path = getcwd(NULL, 0);
649 if (worktree_path == NULL) {
650 error = got_error_from_errno("getcwd");
651 goto done;
653 error = got_worktree_open(&worktree, worktree_path);
654 if (error)
655 goto done;
657 if (argc == 0) {
658 path = strdup("");
659 if (path == NULL) {
660 error = got_error_from_errno("strdup");
661 goto done;
663 } else if (argc == 1) {
664 error = got_worktree_resolve_path(&path, worktree, argv[0]);
665 if (error)
666 goto done;
667 } else
668 usage_update();
670 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
671 if (error != NULL)
672 goto done;
674 error = apply_unveil(got_repo_get_path(repo), 0,
675 got_worktree_get_root_path(worktree), 0);
676 if (error)
677 goto done;
679 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
680 got_worktree_get_head_ref_name(worktree), 0);
681 if (error != NULL)
682 goto done;
683 if (commit_id_str == NULL) {
684 error = got_ref_resolve(&commit_id, repo, head_ref);
685 if (error != NULL)
686 goto done;
687 error = got_object_id_str(&commit_id_str, commit_id);
688 if (error != NULL)
689 goto done;
690 } else {
691 error = got_object_resolve_id_str(&commit_id, repo,
692 commit_id_str);
693 if (error != NULL)
694 goto done;
697 if (branch_name) {
698 struct got_object_id *head_commit_id;
699 if (strlen(path) != 0) {
700 fprintf(stderr, "%s: switching between branches "
701 "requires that the entire work tree "
702 "gets updated, not just '%s'\n",
703 getprogname(), path);
704 error = got_error(GOT_ERR_BAD_PATH);
705 goto done;
707 error = got_ref_resolve(&head_commit_id, repo, head_ref);
708 if (error)
709 goto done;
710 error = check_linear_ancestry(commit_id, head_commit_id, repo);
711 free(head_commit_id);
712 if (error != NULL)
713 goto done;
714 error = check_same_branch(commit_id, head_ref, repo);
715 if (error)
716 goto done;
717 error = switch_head_ref(head_ref, commit_id, worktree, repo);
718 if (error)
719 goto done;
720 } else {
721 error = check_linear_ancestry(commit_id,
722 got_worktree_get_base_commit_id(worktree), repo);
723 if (error != NULL) {
724 if (error->code == GOT_ERR_ANCESTRY)
725 error = got_error(GOT_ERR_BRANCH_MOVED);
726 goto done;
728 error = check_same_branch(commit_id, head_ref, repo);
729 if (error)
730 goto done;
733 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
734 commit_id) != 0) {
735 error = got_worktree_set_base_commit_id(worktree, repo,
736 commit_id);
737 if (error)
738 goto done;
741 error = got_worktree_checkout_files(worktree, path, repo,
742 update_progress, &did_something, check_cancelled, NULL);
743 if (error != NULL)
744 goto done;
746 if (did_something)
747 printf("Updated to commit %s\n", commit_id_str);
748 else
749 printf("Already up-to-date\n");
750 done:
751 free(worktree_path);
752 free(path);
753 free(commit_id);
754 free(commit_id_str);
755 return error;
758 static const struct got_error *
759 print_patch(struct got_commit_object *commit, struct got_object_id *id,
760 int diff_context, struct got_repository *repo)
762 const struct got_error *err = NULL;
763 struct got_tree_object *tree1 = NULL, *tree2;
764 struct got_object_qid *qid;
765 char *id_str1 = NULL, *id_str2;
766 struct got_diff_blob_output_unidiff_arg arg;
768 err = got_object_open_as_tree(&tree2, repo,
769 got_object_commit_get_tree_id(commit));
770 if (err)
771 return err;
773 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
774 if (qid != NULL) {
775 struct got_commit_object *pcommit;
777 err = got_object_open_as_commit(&pcommit, repo, qid->id);
778 if (err)
779 return err;
781 err = got_object_open_as_tree(&tree1, repo,
782 got_object_commit_get_tree_id(pcommit));
783 got_object_commit_close(pcommit);
784 if (err)
785 return err;
787 err = got_object_id_str(&id_str1, qid->id);
788 if (err)
789 return err;
792 err = got_object_id_str(&id_str2, id);
793 if (err)
794 goto done;
796 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
797 arg.diff_context = diff_context;
798 arg.outfile = stdout;
799 err = got_diff_tree(tree1, tree2, "", "", repo,
800 got_diff_blob_output_unidiff, &arg);
801 done:
802 if (tree1)
803 got_object_tree_close(tree1);
804 got_object_tree_close(tree2);
805 free(id_str1);
806 free(id_str2);
807 return err;
810 static char *
811 get_datestr(time_t *time, char *datebuf)
813 char *p, *s = ctime_r(time, datebuf);
814 p = strchr(s, '\n');
815 if (p)
816 *p = '\0';
817 return s;
820 static const struct got_error *
821 print_commit(struct got_commit_object *commit, struct got_object_id *id,
822 struct got_repository *repo, int show_patch, int diff_context,
823 struct got_reflist_head *refs)
825 const struct got_error *err = NULL;
826 char *id_str, *datestr, *logmsg0, *logmsg, *line;
827 char datebuf[26];
828 time_t committer_time;
829 const char *author, *committer;
830 char *refs_str = NULL;
831 struct got_reflist_entry *re;
833 SIMPLEQ_FOREACH(re, refs, entry) {
834 char *s;
835 const char *name;
836 if (got_object_id_cmp(re->id, id) != 0)
837 continue;
838 name = got_ref_get_name(re->ref);
839 if (strcmp(name, GOT_REF_HEAD) == 0)
840 continue;
841 if (strncmp(name, "refs/", 5) == 0)
842 name += 5;
843 if (strncmp(name, "got/", 4) == 0)
844 continue;
845 if (strncmp(name, "heads/", 6) == 0)
846 name += 6;
847 if (strncmp(name, "remotes/", 8) == 0)
848 name += 8;
849 s = refs_str;
850 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
851 name) == -1) {
852 err = got_error_from_errno("asprintf");
853 free(s);
854 break;
856 free(s);
858 err = got_object_id_str(&id_str, id);
859 if (err)
860 return err;
862 printf("-----------------------------------------------\n");
863 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
864 refs_str ? refs_str : "", refs_str ? ")" : "");
865 free(id_str);
866 id_str = NULL;
867 free(refs_str);
868 refs_str = NULL;
869 printf("from: %s\n", got_object_commit_get_author(commit));
870 committer_time = got_object_commit_get_committer_time(commit);
871 datestr = get_datestr(&committer_time, datebuf);
872 printf("date: %s UTC\n", datestr);
873 author = got_object_commit_get_author(commit);
874 committer = got_object_commit_get_committer(commit);
875 if (strcmp(author, committer) != 0)
876 printf("via: %s\n", committer);
877 if (got_object_commit_get_nparents(commit) > 1) {
878 const struct got_object_id_queue *parent_ids;
879 struct got_object_qid *qid;
880 int n = 1;
881 parent_ids = got_object_commit_get_parent_ids(commit);
882 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
883 err = got_object_id_str(&id_str, qid->id);
884 if (err)
885 return err;
886 printf("parent %d: %s\n", n++, id_str);
887 free(id_str);
891 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
892 if (logmsg0 == NULL)
893 return got_error_from_errno("strdup");
895 logmsg = logmsg0;
896 do {
897 line = strsep(&logmsg, "\n");
898 if (line)
899 printf(" %s\n", line);
900 } while (line);
901 free(logmsg0);
903 if (show_patch) {
904 err = print_patch(commit, id, diff_context, repo);
905 if (err == 0)
906 printf("\n");
909 if (fflush(stdout) != 0 && err == NULL)
910 err = got_error_from_errno("fflush");
911 return err;
914 static const struct got_error *
915 print_commits(struct got_object_id *root_id, struct got_repository *repo,
916 char *path, int show_patch, int diff_context, int limit,
917 int first_parent_traversal, struct got_reflist_head *refs)
919 const struct got_error *err;
920 struct got_commit_graph *graph;
922 err = got_commit_graph_open(&graph, root_id, path,
923 first_parent_traversal, repo);
924 if (err)
925 return err;
926 err = got_commit_graph_iter_start(graph, root_id, repo);
927 if (err)
928 goto done;
929 for (;;) {
930 struct got_commit_object *commit;
931 struct got_object_id *id;
933 if (sigint_received || sigpipe_received)
934 break;
936 err = got_commit_graph_iter_next(&id, graph);
937 if (err) {
938 if (err->code == GOT_ERR_ITER_COMPLETED) {
939 err = NULL;
940 break;
942 if (err->code != GOT_ERR_ITER_NEED_MORE)
943 break;
944 err = got_commit_graph_fetch_commits(graph, 1, repo);
945 if (err)
946 break;
947 else
948 continue;
950 if (id == NULL)
951 break;
953 err = got_object_open_as_commit(&commit, repo, id);
954 if (err)
955 break;
956 err = print_commit(commit, id, repo, show_patch, diff_context,
957 refs);
958 got_object_commit_close(commit);
959 if (err || (limit && --limit == 0))
960 break;
962 done:
963 got_commit_graph_close(graph);
964 return err;
967 __dead static void
968 usage_log(void)
970 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
971 "[-r repository-path] [path]\n", getprogname());
972 exit(1);
975 static const struct got_error *
976 cmd_log(int argc, char *argv[])
978 const struct got_error *error;
979 struct got_repository *repo = NULL;
980 struct got_worktree *worktree = NULL;
981 struct got_commit_object *commit = NULL;
982 struct got_object_id *id = NULL;
983 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
984 char *start_commit = NULL;
985 int diff_context = 3, ch;
986 int show_patch = 0, limit = 0, first_parent_traversal = 0;
987 const char *errstr;
988 struct got_reflist_head refs;
990 SIMPLEQ_INIT(&refs);
992 #ifndef PROFILE
993 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
994 NULL)
995 == -1)
996 err(1, "pledge");
997 #endif
999 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1000 switch (ch) {
1001 case 'b':
1002 first_parent_traversal = 1;
1003 break;
1004 case 'p':
1005 show_patch = 1;
1006 break;
1007 case 'c':
1008 start_commit = optarg;
1009 break;
1010 case 'C':
1011 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1012 &errstr);
1013 if (errstr != NULL)
1014 err(1, "-C option %s", errstr);
1015 break;
1016 case 'l':
1017 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1018 if (errstr != NULL)
1019 err(1, "-l option %s", errstr);
1020 break;
1021 case 'r':
1022 repo_path = realpath(optarg, NULL);
1023 if (repo_path == NULL)
1024 err(1, "-r option");
1025 got_path_strip_trailing_slashes(repo_path);
1026 break;
1027 default:
1028 usage_log();
1029 /* NOTREACHED */
1033 argc -= optind;
1034 argv += optind;
1036 cwd = getcwd(NULL, 0);
1037 if (cwd == NULL) {
1038 error = got_error_from_errno("getcwd");
1039 goto done;
1042 error = got_worktree_open(&worktree, cwd);
1043 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1044 goto done;
1045 error = NULL;
1047 if (argc == 0) {
1048 path = strdup("");
1049 if (path == NULL) {
1050 error = got_error_from_errno("strdup");
1051 goto done;
1053 } else if (argc == 1) {
1054 if (worktree) {
1055 error = got_worktree_resolve_path(&path, worktree,
1056 argv[0]);
1057 if (error)
1058 goto done;
1059 } else {
1060 path = strdup(argv[0]);
1061 if (path == NULL) {
1062 error = got_error_from_errno("strdup");
1063 goto done;
1066 } else
1067 usage_log();
1069 if (repo_path == NULL) {
1070 repo_path = worktree ?
1071 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1073 if (repo_path == NULL) {
1074 error = got_error_from_errno("strdup");
1075 goto done;
1078 error = got_repo_open(&repo, repo_path);
1079 if (error != NULL)
1080 goto done;
1082 error = apply_unveil(got_repo_get_path(repo), 1,
1083 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1084 if (error)
1085 goto done;
1087 if (start_commit == NULL) {
1088 struct got_reference *head_ref;
1089 error = got_ref_open(&head_ref, repo,
1090 worktree ? got_worktree_get_head_ref_name(worktree)
1091 : GOT_REF_HEAD, 0);
1092 if (error != NULL)
1093 return error;
1094 error = got_ref_resolve(&id, repo, head_ref);
1095 got_ref_close(head_ref);
1096 if (error != NULL)
1097 return error;
1098 error = got_object_open_as_commit(&commit, repo, id);
1099 } else {
1100 struct got_reference *ref;
1101 error = got_ref_open(&ref, repo, start_commit, 0);
1102 if (error == NULL) {
1103 int obj_type;
1104 error = got_ref_resolve(&id, repo, ref);
1105 got_ref_close(ref);
1106 if (error != NULL)
1107 goto done;
1108 error = got_object_get_type(&obj_type, repo, id);
1109 if (error != NULL)
1110 goto done;
1111 if (obj_type == GOT_OBJ_TYPE_TAG) {
1112 struct got_tag_object *tag;
1113 error = got_object_open_as_tag(&tag, repo, id);
1114 if (error != NULL)
1115 goto done;
1116 if (got_object_tag_get_object_type(tag) !=
1117 GOT_OBJ_TYPE_COMMIT) {
1118 got_object_tag_close(tag);
1119 error = got_error(GOT_ERR_OBJ_TYPE);
1120 goto done;
1122 free(id);
1123 id = got_object_id_dup(
1124 got_object_tag_get_object_id(tag));
1125 if (id == NULL)
1126 error = got_error_from_errno(
1127 "got_object_id_dup");
1128 got_object_tag_close(tag);
1129 if (error)
1130 goto done;
1131 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1132 error = got_error(GOT_ERR_OBJ_TYPE);
1133 goto done;
1135 error = got_object_open_as_commit(&commit, repo, id);
1136 if (error != NULL)
1137 goto done;
1139 if (commit == NULL) {
1140 error = got_object_resolve_id_str(&id, repo,
1141 start_commit);
1142 if (error != NULL)
1143 return error;
1146 if (error != NULL)
1147 goto done;
1149 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1150 if (error != NULL)
1151 goto done;
1152 if (in_repo_path) {
1153 free(path);
1154 path = in_repo_path;
1157 error = got_ref_list(&refs, repo);
1158 if (error)
1159 goto done;
1161 error = print_commits(id, repo, path, show_patch,
1162 diff_context, limit, first_parent_traversal, &refs);
1163 done:
1164 free(path);
1165 free(repo_path);
1166 free(cwd);
1167 free(id);
1168 if (worktree)
1169 got_worktree_close(worktree);
1170 if (repo) {
1171 const struct got_error *repo_error;
1172 repo_error = got_repo_close(repo);
1173 if (error == NULL)
1174 error = repo_error;
1176 got_ref_list_free(&refs);
1177 return error;
1180 __dead static void
1181 usage_diff(void)
1183 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1184 "[object1 object2 | path]\n", getprogname());
1185 exit(1);
1188 struct print_diff_arg {
1189 struct got_repository *repo;
1190 struct got_worktree *worktree;
1191 int diff_context;
1192 const char *id_str;
1193 int header_shown;
1196 static const struct got_error *
1197 print_diff(void *arg, unsigned char status, const char *path,
1198 struct got_object_id *blob_id, struct got_object_id *commit_id)
1200 struct print_diff_arg *a = arg;
1201 const struct got_error *err = NULL;
1202 struct got_blob_object *blob1 = NULL;
1203 FILE *f2 = NULL;
1204 char *abspath = NULL;
1205 struct stat sb;
1207 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1208 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1209 return NULL;
1211 if (!a->header_shown) {
1212 printf("diff %s %s\n", a->id_str,
1213 got_worktree_get_root_path(a->worktree));
1214 a->header_shown = 1;
1217 if (status != GOT_STATUS_ADD) {
1218 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1219 if (err)
1220 goto done;
1224 if (status != GOT_STATUS_DELETE) {
1225 if (asprintf(&abspath, "%s/%s",
1226 got_worktree_get_root_path(a->worktree), path) == -1) {
1227 err = got_error_from_errno("asprintf");
1228 goto done;
1231 f2 = fopen(abspath, "r");
1232 if (f2 == NULL) {
1233 err = got_error_from_errno2("fopen", abspath);
1234 goto done;
1236 if (lstat(abspath, &sb) == -1) {
1237 err = got_error_from_errno2("lstat", abspath);
1238 goto done;
1240 } else
1241 sb.st_size = 0;
1243 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1244 stdout);
1245 done:
1246 if (blob1)
1247 got_object_blob_close(blob1);
1248 if (f2 && fclose(f2) != 0 && err == NULL)
1249 err = got_error_from_errno("fclose");
1250 free(abspath);
1251 return err;
1254 static const struct got_error *
1255 cmd_diff(int argc, char *argv[])
1257 const struct got_error *error;
1258 struct got_repository *repo = NULL;
1259 struct got_worktree *worktree = NULL;
1260 char *cwd = NULL, *repo_path = NULL;
1261 struct got_object_id *id1 = NULL, *id2 = NULL;
1262 const char *id_str1 = NULL, *id_str2 = NULL;
1263 char *label1 = NULL, *label2 = NULL;
1264 int type1, type2;
1265 int diff_context = 3, ch;
1266 const char *errstr;
1267 char *path = NULL;
1269 #ifndef PROFILE
1270 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1271 NULL) == -1)
1272 err(1, "pledge");
1273 #endif
1275 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1276 switch (ch) {
1277 case 'C':
1278 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1279 if (errstr != NULL)
1280 err(1, "-C option %s", errstr);
1281 break;
1282 case 'r':
1283 repo_path = realpath(optarg, NULL);
1284 if (repo_path == NULL)
1285 err(1, "-r option");
1286 got_path_strip_trailing_slashes(repo_path);
1287 break;
1288 default:
1289 usage_diff();
1290 /* NOTREACHED */
1294 argc -= optind;
1295 argv += optind;
1297 cwd = getcwd(NULL, 0);
1298 if (cwd == NULL) {
1299 error = got_error_from_errno("getcwd");
1300 goto done;
1302 error = got_worktree_open(&worktree, cwd);
1303 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1304 goto done;
1305 if (argc <= 1) {
1306 if (worktree == NULL) {
1307 error = got_error(GOT_ERR_NOT_WORKTREE);
1308 goto done;
1310 if (repo_path)
1311 errx(1,
1312 "-r option can't be used when diffing a work tree");
1313 repo_path = strdup(got_worktree_get_repo_path(worktree));
1314 if (repo_path == NULL) {
1315 error = got_error_from_errno("strdup");
1316 goto done;
1318 if (argc == 1) {
1319 error = got_worktree_resolve_path(&path, worktree,
1320 argv[0]);
1321 if (error)
1322 goto done;
1323 } else {
1324 path = strdup("");
1325 if (path == NULL) {
1326 error = got_error_from_errno("strdup");
1327 goto done;
1330 } else if (argc == 2) {
1331 id_str1 = argv[0];
1332 id_str2 = argv[1];
1333 } else
1334 usage_diff();
1336 if (repo_path == NULL) {
1337 repo_path = getcwd(NULL, 0);
1338 if (repo_path == NULL)
1339 return got_error_from_errno("getcwd");
1342 error = got_repo_open(&repo, repo_path);
1343 free(repo_path);
1344 if (error != NULL)
1345 goto done;
1347 error = apply_unveil(got_repo_get_path(repo), 1,
1348 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1349 if (error)
1350 goto done;
1352 if (worktree) {
1353 struct print_diff_arg arg;
1354 char *id_str;
1355 error = got_object_id_str(&id_str,
1356 got_worktree_get_base_commit_id(worktree));
1357 if (error)
1358 goto done;
1359 arg.repo = repo;
1360 arg.worktree = worktree;
1361 arg.diff_context = diff_context;
1362 arg.id_str = id_str;
1363 arg.header_shown = 0;
1365 error = got_worktree_status(worktree, path, repo, print_diff,
1366 &arg, check_cancelled, NULL);
1367 free(id_str);
1368 goto done;
1371 error = got_object_resolve_id_str(&id1, repo, id_str1);
1372 if (error) {
1373 struct got_reference *ref;
1374 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1375 goto done;
1376 error = got_ref_open(&ref, repo, id_str1, 0);
1377 if (error != NULL)
1378 goto done;
1379 label1 = strdup(got_ref_get_name(ref));
1380 if (label1 == NULL) {
1381 error = got_error_from_errno("strdup");
1382 goto done;
1384 error = got_ref_resolve(&id1, repo, ref);
1385 got_ref_close(ref);
1386 if (error != NULL)
1387 goto done;
1388 } else {
1389 label1 = strdup(id_str1);
1390 if (label1 == NULL) {
1391 error = got_error_from_errno("strdup");
1392 goto done;
1396 error = got_object_resolve_id_str(&id2, repo, id_str2);
1397 if (error) {
1398 struct got_reference *ref;
1399 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1400 goto done;
1401 error = got_ref_open(&ref, repo, id_str2, 0);
1402 if (error != NULL)
1403 goto done;
1404 label2 = strdup(got_ref_get_name(ref));
1405 if (label2 == NULL) {
1406 error = got_error_from_errno("strdup");
1407 goto done;
1409 error = got_ref_resolve(&id2, repo, ref);
1410 got_ref_close(ref);
1411 if (error != NULL)
1412 goto done;
1413 } else {
1414 label2 = strdup(id_str2);
1415 if (label2 == NULL) {
1416 error = got_error_from_errno("strdup");
1417 goto done;
1421 error = got_object_get_type(&type1, repo, id1);
1422 if (error)
1423 goto done;
1425 error = got_object_get_type(&type2, repo, id2);
1426 if (error)
1427 goto done;
1429 if (type1 != type2) {
1430 error = got_error(GOT_ERR_OBJ_TYPE);
1431 goto done;
1434 switch (type1) {
1435 case GOT_OBJ_TYPE_BLOB:
1436 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1437 diff_context, repo, stdout);
1438 break;
1439 case GOT_OBJ_TYPE_TREE:
1440 error = got_diff_objects_as_trees(id1, id2, "", "",
1441 diff_context, repo, stdout);
1442 break;
1443 case GOT_OBJ_TYPE_COMMIT:
1444 printf("diff %s %s\n", label1, label2);
1445 error = got_diff_objects_as_commits(id1, id2, diff_context,
1446 repo, stdout);
1447 break;
1448 default:
1449 error = got_error(GOT_ERR_OBJ_TYPE);
1452 done:
1453 free(label1);
1454 free(label2);
1455 free(id1);
1456 free(id2);
1457 free(path);
1458 if (worktree)
1459 got_worktree_close(worktree);
1460 if (repo) {
1461 const struct got_error *repo_error;
1462 repo_error = got_repo_close(repo);
1463 if (error == NULL)
1464 error = repo_error;
1466 return error;
1469 __dead static void
1470 usage_blame(void)
1472 fprintf(stderr,
1473 "usage: %s blame [-c commit] [-r repository-path] path\n",
1474 getprogname());
1475 exit(1);
1478 static const struct got_error *
1479 cmd_blame(int argc, char *argv[])
1481 const struct got_error *error;
1482 struct got_repository *repo = NULL;
1483 struct got_worktree *worktree = NULL;
1484 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1485 struct got_object_id *commit_id = NULL;
1486 char *commit_id_str = NULL;
1487 int ch;
1489 #ifndef PROFILE
1490 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1491 NULL) == -1)
1492 err(1, "pledge");
1493 #endif
1495 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1496 switch (ch) {
1497 case 'c':
1498 commit_id_str = optarg;
1499 break;
1500 case 'r':
1501 repo_path = realpath(optarg, NULL);
1502 if (repo_path == NULL)
1503 err(1, "-r option");
1504 got_path_strip_trailing_slashes(repo_path);
1505 break;
1506 default:
1507 usage_blame();
1508 /* NOTREACHED */
1512 argc -= optind;
1513 argv += optind;
1515 if (argc == 1)
1516 path = argv[0];
1517 else
1518 usage_blame();
1520 cwd = getcwd(NULL, 0);
1521 if (cwd == NULL) {
1522 error = got_error_from_errno("getcwd");
1523 goto done;
1525 if (repo_path == NULL) {
1526 error = got_worktree_open(&worktree, cwd);
1527 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1528 goto done;
1529 else
1530 error = NULL;
1531 if (worktree) {
1532 repo_path =
1533 strdup(got_worktree_get_repo_path(worktree));
1534 if (repo_path == NULL)
1535 error = got_error_from_errno("strdup");
1536 if (error)
1537 goto done;
1538 } else {
1539 repo_path = strdup(cwd);
1540 if (repo_path == NULL) {
1541 error = got_error_from_errno("strdup");
1542 goto done;
1547 error = got_repo_open(&repo, repo_path);
1548 if (error != NULL)
1549 goto done;
1551 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1552 if (error)
1553 goto done;
1555 if (worktree) {
1556 const char *prefix = got_worktree_get_path_prefix(worktree);
1557 char *p, *worktree_subdir = cwd +
1558 strlen(got_worktree_get_root_path(worktree));
1559 if (asprintf(&p, "%s%s%s%s%s",
1560 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1561 worktree_subdir, worktree_subdir[0] ? "/" : "",
1562 path) == -1) {
1563 error = got_error_from_errno("asprintf");
1564 goto done;
1566 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1567 free(p);
1568 } else {
1569 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1571 if (error)
1572 goto done;
1574 if (commit_id_str == NULL) {
1575 struct got_reference *head_ref;
1576 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1577 if (error != NULL)
1578 goto done;
1579 error = got_ref_resolve(&commit_id, repo, head_ref);
1580 got_ref_close(head_ref);
1581 if (error != NULL)
1582 goto done;
1583 } else {
1584 error = got_object_resolve_id_str(&commit_id, repo,
1585 commit_id_str);
1586 if (error != NULL)
1587 goto done;
1590 error = got_blame(in_repo_path, commit_id, repo, stdout);
1591 done:
1592 free(in_repo_path);
1593 free(repo_path);
1594 free(cwd);
1595 free(commit_id);
1596 if (worktree)
1597 got_worktree_close(worktree);
1598 if (repo) {
1599 const struct got_error *repo_error;
1600 repo_error = got_repo_close(repo);
1601 if (error == NULL)
1602 error = repo_error;
1604 return error;
1607 __dead static void
1608 usage_tree(void)
1610 fprintf(stderr,
1611 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1612 getprogname());
1613 exit(1);
1616 static void
1617 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1618 const char *root_path)
1620 int is_root_path = (strcmp(path, root_path) == 0);
1622 path += strlen(root_path);
1623 while (path[0] == '/')
1624 path++;
1626 printf("%s%s%s%s%s\n", id ? id : "", path,
1627 is_root_path ? "" : "/", te->name,
1628 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1631 static const struct got_error *
1632 print_tree(const char *path, struct got_object_id *commit_id,
1633 int show_ids, int recurse, const char *root_path,
1634 struct got_repository *repo)
1636 const struct got_error *err = NULL;
1637 struct got_object_id *tree_id = NULL;
1638 struct got_tree_object *tree = NULL;
1639 const struct got_tree_entries *entries;
1640 struct got_tree_entry *te;
1642 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1643 if (err)
1644 goto done;
1646 err = got_object_open_as_tree(&tree, repo, tree_id);
1647 if (err)
1648 goto done;
1649 entries = got_object_tree_get_entries(tree);
1650 te = SIMPLEQ_FIRST(&entries->head);
1651 while (te) {
1652 char *id = NULL;
1654 if (sigint_received || sigpipe_received)
1655 break;
1657 if (show_ids) {
1658 char *id_str;
1659 err = got_object_id_str(&id_str, te->id);
1660 if (err)
1661 goto done;
1662 if (asprintf(&id, "%s ", id_str) == -1) {
1663 err = got_error_from_errno("asprintf");
1664 free(id_str);
1665 goto done;
1667 free(id_str);
1669 print_entry(te, id, path, root_path);
1670 free(id);
1672 if (recurse && S_ISDIR(te->mode)) {
1673 char *child_path;
1674 if (asprintf(&child_path, "%s%s%s", path,
1675 path[0] == '/' && path[1] == '\0' ? "" : "/",
1676 te->name) == -1) {
1677 err = got_error_from_errno("asprintf");
1678 goto done;
1680 err = print_tree(child_path, commit_id, show_ids, 1,
1681 root_path, repo);
1682 free(child_path);
1683 if (err)
1684 goto done;
1687 te = SIMPLEQ_NEXT(te, entry);
1689 done:
1690 if (tree)
1691 got_object_tree_close(tree);
1692 free(tree_id);
1693 return err;
1696 static const struct got_error *
1697 cmd_tree(int argc, char *argv[])
1699 const struct got_error *error;
1700 struct got_repository *repo = NULL;
1701 struct got_worktree *worktree = NULL;
1702 const char *path;
1703 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1704 struct got_object_id *commit_id = NULL;
1705 char *commit_id_str = NULL;
1706 int show_ids = 0, recurse = 0;
1707 int ch;
1709 #ifndef PROFILE
1710 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1711 NULL) == -1)
1712 err(1, "pledge");
1713 #endif
1715 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1716 switch (ch) {
1717 case 'c':
1718 commit_id_str = optarg;
1719 break;
1720 case 'r':
1721 repo_path = realpath(optarg, NULL);
1722 if (repo_path == NULL)
1723 err(1, "-r option");
1724 got_path_strip_trailing_slashes(repo_path);
1725 break;
1726 case 'i':
1727 show_ids = 1;
1728 break;
1729 case 'R':
1730 recurse = 1;
1731 break;
1732 default:
1733 usage_tree();
1734 /* NOTREACHED */
1738 argc -= optind;
1739 argv += optind;
1741 if (argc == 1)
1742 path = argv[0];
1743 else if (argc > 1)
1744 usage_tree();
1745 else
1746 path = NULL;
1748 cwd = getcwd(NULL, 0);
1749 if (cwd == NULL) {
1750 error = got_error_from_errno("getcwd");
1751 goto done;
1753 if (repo_path == NULL) {
1754 error = got_worktree_open(&worktree, cwd);
1755 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1756 goto done;
1757 else
1758 error = NULL;
1759 if (worktree) {
1760 repo_path =
1761 strdup(got_worktree_get_repo_path(worktree));
1762 if (repo_path == NULL)
1763 error = got_error_from_errno("strdup");
1764 if (error)
1765 goto done;
1766 } else {
1767 repo_path = strdup(cwd);
1768 if (repo_path == NULL) {
1769 error = got_error_from_errno("strdup");
1770 goto done;
1775 error = got_repo_open(&repo, repo_path);
1776 if (error != NULL)
1777 goto done;
1779 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1780 if (error)
1781 goto done;
1783 if (path == NULL) {
1784 if (worktree) {
1785 char *p, *worktree_subdir = cwd +
1786 strlen(got_worktree_get_root_path(worktree));
1787 if (asprintf(&p, "%s/%s",
1788 got_worktree_get_path_prefix(worktree),
1789 worktree_subdir) == -1) {
1790 error = got_error_from_errno("asprintf");
1791 goto done;
1793 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1794 free(p);
1795 if (error)
1796 goto done;
1797 } else
1798 path = "/";
1800 if (in_repo_path == NULL) {
1801 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1802 if (error != NULL)
1803 goto done;
1806 if (commit_id_str == NULL) {
1807 struct got_reference *head_ref;
1808 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1809 if (error != NULL)
1810 goto done;
1811 error = got_ref_resolve(&commit_id, repo, head_ref);
1812 got_ref_close(head_ref);
1813 if (error != NULL)
1814 goto done;
1815 } else {
1816 error = got_object_resolve_id_str(&commit_id, repo,
1817 commit_id_str);
1818 if (error != NULL)
1819 goto done;
1822 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1823 in_repo_path, repo);
1824 done:
1825 free(in_repo_path);
1826 free(repo_path);
1827 free(cwd);
1828 free(commit_id);
1829 if (worktree)
1830 got_worktree_close(worktree);
1831 if (repo) {
1832 const struct got_error *repo_error;
1833 repo_error = got_repo_close(repo);
1834 if (error == NULL)
1835 error = repo_error;
1837 return error;
1840 __dead static void
1841 usage_status(void)
1843 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1844 exit(1);
1847 static const struct got_error *
1848 print_status(void *arg, unsigned char status, const char *path,
1849 struct got_object_id *blob_id, struct got_object_id *commit_id)
1851 printf("%c %s\n", status, path);
1852 return NULL;
1855 static const struct got_error *
1856 cmd_status(int argc, char *argv[])
1858 const struct got_error *error = NULL;
1859 struct got_repository *repo = NULL;
1860 struct got_worktree *worktree = NULL;
1861 char *cwd = NULL, *path = NULL;
1862 int ch;
1864 while ((ch = getopt(argc, argv, "")) != -1) {
1865 switch (ch) {
1866 default:
1867 usage_status();
1868 /* NOTREACHED */
1872 argc -= optind;
1873 argv += optind;
1875 #ifndef PROFILE
1876 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1877 NULL) == -1)
1878 err(1, "pledge");
1879 #endif
1880 cwd = getcwd(NULL, 0);
1881 if (cwd == NULL) {
1882 error = got_error_from_errno("getcwd");
1883 goto done;
1886 error = got_worktree_open(&worktree, cwd);
1887 if (error != NULL)
1888 goto done;
1890 if (argc == 0) {
1891 path = strdup("");
1892 if (path == NULL) {
1893 error = got_error_from_errno("strdup");
1894 goto done;
1896 } else if (argc == 1) {
1897 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1898 if (error)
1899 goto done;
1900 } else
1901 usage_status();
1903 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1904 if (error != NULL)
1905 goto done;
1907 error = apply_unveil(got_repo_get_path(repo), 1,
1908 got_worktree_get_root_path(worktree), 0);
1909 if (error)
1910 goto done;
1912 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1913 check_cancelled, NULL);
1914 done:
1915 free(cwd);
1916 free(path);
1917 return error;
1920 __dead static void
1921 usage_ref(void)
1923 fprintf(stderr,
1924 "usage: %s ref [-r repository] -l | -d name | name target\n",
1925 getprogname());
1926 exit(1);
1929 static const struct got_error *
1930 list_refs(struct got_repository *repo)
1932 static const struct got_error *err = NULL;
1933 struct got_reflist_head refs;
1934 struct got_reflist_entry *re;
1936 SIMPLEQ_INIT(&refs);
1937 err = got_ref_list(&refs, repo);
1938 if (err)
1939 return err;
1941 SIMPLEQ_FOREACH(re, &refs, entry) {
1942 char *refstr;
1943 refstr = got_ref_to_str(re->ref);
1944 if (refstr == NULL)
1945 return got_error_from_errno("got_ref_to_str");
1946 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1947 free(refstr);
1950 got_ref_list_free(&refs);
1951 return NULL;
1954 static const struct got_error *
1955 delete_ref(struct got_repository *repo, const char *refname)
1957 const struct got_error *err = NULL;
1958 struct got_reference *ref;
1960 err = got_ref_open(&ref, repo, refname, 0);
1961 if (err)
1962 return err;
1964 err = got_ref_delete(ref, repo);
1965 got_ref_close(ref);
1966 return err;
1969 static const struct got_error *
1970 add_ref(struct got_repository *repo, const char *refname, const char *target)
1972 const struct got_error *err = NULL;
1973 struct got_object_id *id;
1974 struct got_reference *ref = NULL;
1976 err = got_object_resolve_id_str(&id, repo, target);
1977 if (err) {
1978 struct got_reference *target_ref;
1980 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1981 return err;
1982 err = got_ref_open(&target_ref, repo, target, 0);
1983 if (err)
1984 return err;
1985 err = got_ref_resolve(&id, repo, target_ref);
1986 got_ref_close(target_ref);
1987 if (err)
1988 return err;
1991 err = got_ref_alloc(&ref, refname, id);
1992 if (err)
1993 goto done;
1995 err = got_ref_write(ref, repo);
1996 done:
1997 if (ref)
1998 got_ref_close(ref);
1999 free(id);
2000 return err;
2003 static const struct got_error *
2004 cmd_ref(int argc, char *argv[])
2006 const struct got_error *error = NULL;
2007 struct got_repository *repo = NULL;
2008 struct got_worktree *worktree = NULL;
2009 char *cwd = NULL, *repo_path = NULL;
2010 int ch, do_list = 0;
2011 const char *delref = NULL;
2013 /* TODO: Add -s option for adding symbolic references. */
2014 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2015 switch (ch) {
2016 case 'd':
2017 delref = optarg;
2018 break;
2019 case 'r':
2020 repo_path = realpath(optarg, NULL);
2021 if (repo_path == NULL)
2022 err(1, "-r option");
2023 got_path_strip_trailing_slashes(repo_path);
2024 break;
2025 case 'l':
2026 do_list = 1;
2027 break;
2028 default:
2029 usage_ref();
2030 /* NOTREACHED */
2034 if (do_list && delref)
2035 errx(1, "-l and -d options are mutually exclusive\n");
2037 argc -= optind;
2038 argv += optind;
2040 if (do_list || delref) {
2041 if (argc > 0)
2042 usage_ref();
2043 } else if (argc != 2)
2044 usage_ref();
2046 #ifndef PROFILE
2047 if (do_list) {
2048 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2049 NULL) == -1)
2050 err(1, "pledge");
2051 } else {
2052 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2053 "sendfd unveil", NULL) == -1)
2054 err(1, "pledge");
2056 #endif
2057 cwd = getcwd(NULL, 0);
2058 if (cwd == NULL) {
2059 error = got_error_from_errno("getcwd");
2060 goto done;
2063 if (repo_path == NULL) {
2064 error = got_worktree_open(&worktree, cwd);
2065 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2066 goto done;
2067 else
2068 error = NULL;
2069 if (worktree) {
2070 repo_path =
2071 strdup(got_worktree_get_repo_path(worktree));
2072 if (repo_path == NULL)
2073 error = got_error_from_errno("strdup");
2074 if (error)
2075 goto done;
2076 } else {
2077 repo_path = strdup(cwd);
2078 if (repo_path == NULL) {
2079 error = got_error_from_errno("strdup");
2080 goto done;
2085 error = got_repo_open(&repo, repo_path);
2086 if (error != NULL)
2087 goto done;
2089 error = apply_unveil(got_repo_get_path(repo), do_list,
2090 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2091 if (error)
2092 goto done;
2094 if (do_list)
2095 error = list_refs(repo);
2096 else if (delref)
2097 error = delete_ref(repo, delref);
2098 else
2099 error = add_ref(repo, argv[0], argv[1]);
2100 done:
2101 if (repo)
2102 got_repo_close(repo);
2103 if (worktree)
2104 got_worktree_close(worktree);
2105 free(cwd);
2106 free(repo_path);
2107 return error;
2110 __dead static void
2111 usage_add(void)
2113 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2114 exit(1);
2117 static const struct got_error *
2118 cmd_add(int argc, char *argv[])
2120 const struct got_error *error = NULL;
2121 struct got_repository *repo = NULL;
2122 struct got_worktree *worktree = NULL;
2123 char *cwd = NULL;
2124 struct got_pathlist_head paths;
2125 struct got_pathlist_entry *pe;
2126 int ch, x;
2128 TAILQ_INIT(&paths);
2130 while ((ch = getopt(argc, argv, "")) != -1) {
2131 switch (ch) {
2132 default:
2133 usage_add();
2134 /* NOTREACHED */
2138 argc -= optind;
2139 argv += optind;
2141 if (argc < 1)
2142 usage_add();
2144 /* make sure each file exists before doing anything halfway */
2145 for (x = 0; x < argc; x++) {
2146 char *path = realpath(argv[x], NULL);
2147 if (path == NULL) {
2148 error = got_error_from_errno2("realpath", argv[x]);
2149 goto done;
2151 free(path);
2154 cwd = getcwd(NULL, 0);
2155 if (cwd == NULL) {
2156 error = got_error_from_errno("getcwd");
2157 goto done;
2160 error = got_worktree_open(&worktree, cwd);
2161 if (error)
2162 goto done;
2164 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2165 if (error != NULL)
2166 goto done;
2168 error = apply_unveil(got_repo_get_path(repo), 1,
2169 got_worktree_get_root_path(worktree), 0);
2170 if (error)
2171 goto done;
2173 for (x = 0; x < argc; x++) {
2174 char *path = realpath(argv[x], NULL);
2175 if (path == NULL) {
2176 error = got_error_from_errno2("realpath", argv[x]);
2177 goto done;
2180 got_path_strip_trailing_slashes(path);
2181 error = got_pathlist_insert(&pe, &paths, path, NULL);
2182 if (error) {
2183 free(path);
2184 goto done;
2187 error = got_worktree_schedule_add(worktree, &paths, print_status,
2188 NULL, repo);
2189 done:
2190 if (repo)
2191 got_repo_close(repo);
2192 if (worktree)
2193 got_worktree_close(worktree);
2194 TAILQ_FOREACH(pe, &paths, entry)
2195 free((char *)pe->path);
2196 got_pathlist_free(&paths);
2197 free(cwd);
2198 return error;
2201 __dead static void
2202 usage_rm(void)
2204 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2205 exit(1);
2208 static const struct got_error *
2209 cmd_rm(int argc, char *argv[])
2211 const struct got_error *error = NULL;
2212 struct got_worktree *worktree = NULL;
2213 struct got_repository *repo = NULL;
2214 char *cwd = NULL, *path = NULL;
2215 int ch, delete_local_mods = 0;
2217 while ((ch = getopt(argc, argv, "f")) != -1) {
2218 switch (ch) {
2219 case 'f':
2220 delete_local_mods = 1;
2221 break;
2222 default:
2223 usage_add();
2224 /* NOTREACHED */
2228 argc -= optind;
2229 argv += optind;
2231 if (argc != 1)
2232 usage_rm();
2234 path = realpath(argv[0], NULL);
2235 if (path == NULL) {
2236 error = got_error_from_errno2("realpath", argv[0]);
2237 goto done;
2239 got_path_strip_trailing_slashes(path);
2241 cwd = getcwd(NULL, 0);
2242 if (cwd == NULL) {
2243 error = got_error_from_errno("getcwd");
2244 goto done;
2246 error = got_worktree_open(&worktree, cwd);
2247 if (error)
2248 goto done;
2250 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2251 if (error)
2252 goto done;
2254 error = apply_unveil(got_repo_get_path(repo), 1,
2255 got_worktree_get_root_path(worktree), 0);
2256 if (error)
2257 goto done;
2259 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2260 print_status, NULL, repo);
2261 if (error)
2262 goto done;
2263 done:
2264 if (repo)
2265 got_repo_close(repo);
2266 if (worktree)
2267 got_worktree_close(worktree);
2268 free(path);
2269 free(cwd);
2270 return error;
2273 __dead static void
2274 usage_revert(void)
2276 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2277 exit(1);
2280 static void
2281 revert_progress(void *arg, unsigned char status, const char *path)
2283 while (path[0] == '/')
2284 path++;
2285 printf("%c %s\n", status, path);
2288 static const struct got_error *
2289 cmd_revert(int argc, char *argv[])
2291 const struct got_error *error = NULL;
2292 struct got_worktree *worktree = NULL;
2293 struct got_repository *repo = NULL;
2294 char *cwd = NULL, *path = NULL;
2295 int ch;
2297 while ((ch = getopt(argc, argv, "")) != -1) {
2298 switch (ch) {
2299 default:
2300 usage_revert();
2301 /* NOTREACHED */
2305 argc -= optind;
2306 argv += optind;
2308 if (argc != 1)
2309 usage_revert();
2311 path = realpath(argv[0], NULL);
2312 if (path == NULL) {
2313 error = got_error_from_errno2("realpath", argv[0]);
2314 goto done;
2316 got_path_strip_trailing_slashes(path);
2318 cwd = getcwd(NULL, 0);
2319 if (cwd == NULL) {
2320 error = got_error_from_errno("getcwd");
2321 goto done;
2323 error = got_worktree_open(&worktree, cwd);
2324 if (error)
2325 goto done;
2327 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2328 if (error != NULL)
2329 goto done;
2331 error = apply_unveil(got_repo_get_path(repo), 1,
2332 got_worktree_get_root_path(worktree), 0);
2333 if (error)
2334 goto done;
2336 error = got_worktree_revert(worktree, path,
2337 revert_progress, NULL, repo);
2338 if (error)
2339 goto done;
2340 done:
2341 if (repo)
2342 got_repo_close(repo);
2343 if (worktree)
2344 got_worktree_close(worktree);
2345 free(path);
2346 free(cwd);
2347 return error;
2350 __dead static void
2351 usage_commit(void)
2353 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2354 exit(1);
2357 int
2358 spawn_editor(const char *editor, const char *file)
2360 pid_t pid;
2361 sig_t sighup, sigint, sigquit;
2362 int st = -1;
2364 sighup = signal(SIGHUP, SIG_IGN);
2365 sigint = signal(SIGINT, SIG_IGN);
2366 sigquit = signal(SIGQUIT, SIG_IGN);
2368 switch (pid = fork()) {
2369 case -1:
2370 goto doneediting;
2371 case 0:
2372 execl(editor, editor, file, (char *)NULL);
2373 _exit(127);
2376 while (waitpid(pid, &st, 0) == -1)
2377 if (errno != EINTR)
2378 break;
2380 doneediting:
2381 (void)signal(SIGHUP, sighup);
2382 (void)signal(SIGINT, sigint);
2383 (void)signal(SIGQUIT, sigquit);
2385 if (!WIFEXITED(st)) {
2386 errno = EINTR;
2387 return -1;
2390 return WEXITSTATUS(st);
2393 struct collect_commit_logmsg_arg {
2394 const char *cmdline_log;
2395 const char *editor;
2396 const char *worktree_path;
2397 const char *repo_path;
2398 char *logmsg_path;
2402 static const struct got_error *
2403 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2404 void *arg)
2406 const char *initial_content = "\n# changes to be committed:\n";
2407 struct got_pathlist_entry *pe;
2408 const struct got_error *err = NULL;
2409 char *template = NULL;
2410 struct collect_commit_logmsg_arg *a = arg;
2411 char buf[1024];
2412 struct stat st, st2;
2413 FILE *fp;
2414 size_t len;
2415 int fd, content_changed = 0;
2417 /* if a message was specified on the command line, just use it */
2418 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2419 len = strlen(a->cmdline_log) + 1;
2420 *logmsg = malloc(len + 1);
2421 if (*logmsg == NULL)
2422 return got_error_from_errno("malloc");
2423 strlcpy(*logmsg, a->cmdline_log, len);
2424 return NULL;
2427 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2428 return got_error_from_errno("asprintf");
2430 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2431 if (err)
2432 goto done;
2434 dprintf(fd, initial_content);
2436 TAILQ_FOREACH(pe, commitable_paths, entry) {
2437 struct got_commitable *ct = pe->data;
2438 dprintf(fd, "# %c %s\n",
2439 got_commitable_get_status(ct),
2440 got_commitable_get_path(ct));
2442 close(fd);
2444 if (stat(a->logmsg_path, &st) == -1) {
2445 err = got_error_from_errno2("stat", a->logmsg_path);
2446 goto done;
2449 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2450 err = got_error_from_errno("failed spawning editor");
2451 goto done;
2454 if (stat(a->logmsg_path, &st2) == -1) {
2455 err = got_error_from_errno("stat");
2456 goto done;
2459 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2460 unlink(a->logmsg_path);
2461 free(a->logmsg_path);
2462 a->logmsg_path = NULL;
2463 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2464 "no changes made to commit message, aborting");
2465 goto done;
2468 *logmsg = malloc(st2.st_size + 1);
2469 if (*logmsg == NULL) {
2470 err = got_error_from_errno("malloc");
2471 goto done;
2473 (*logmsg)[0] = '\0';
2474 len = 0;
2476 fp = fopen(a->logmsg_path, "r");
2477 if (fp == NULL) {
2478 err = got_error_from_errno("fopen");
2479 goto done;
2481 while (fgets(buf, sizeof(buf), fp) != NULL) {
2482 if (!content_changed && strcmp(buf, initial_content) != 0)
2483 content_changed = 1;
2484 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2485 continue; /* remove comments and leading empty lines */
2486 len = strlcat(*logmsg, buf, st2.st_size);
2488 fclose(fp);
2490 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2491 (*logmsg)[len - 1] = '\0';
2492 len--;
2495 if (len == 0 || !content_changed) {
2496 unlink(a->logmsg_path);
2497 free(a->logmsg_path);
2498 a->logmsg_path = NULL;
2499 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2500 "commit message cannot be empty, aborting");
2501 goto done;
2503 done:
2504 free(template);
2506 /* Editor is done; we can now apply unveil(2) */
2507 if (err == NULL)
2508 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2509 return err;
2512 static const struct got_error *
2513 cmd_commit(int argc, char *argv[])
2515 const struct got_error *error = NULL;
2516 struct got_worktree *worktree = NULL;
2517 struct got_repository *repo = NULL;
2518 char *cwd = NULL, *path = NULL, *id_str = NULL;
2519 struct got_object_id *id = NULL;
2520 const char *logmsg = NULL;
2521 const char *got_author = getenv("GOT_AUTHOR");
2522 struct collect_commit_logmsg_arg cl_arg;
2523 char *editor = NULL;
2524 int ch;
2526 while ((ch = getopt(argc, argv, "m:")) != -1) {
2527 switch (ch) {
2528 case 'm':
2529 logmsg = optarg;
2530 break;
2531 default:
2532 usage_commit();
2533 /* NOTREACHED */
2537 argc -= optind;
2538 argv += optind;
2540 if (argc == 1) {
2541 path = realpath(argv[0], NULL);
2542 if (path == NULL) {
2543 error = got_error_from_errno2("realpath", argv[0]);
2544 goto done;
2546 got_path_strip_trailing_slashes(path);
2547 } else if (argc != 0)
2548 usage_commit();
2550 if (got_author == NULL) {
2551 /* TODO: Look current user up in password database */
2552 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2553 goto done;
2556 cwd = getcwd(NULL, 0);
2557 if (cwd == NULL) {
2558 error = got_error_from_errno("getcwd");
2559 goto done;
2561 error = got_worktree_open(&worktree, cwd);
2562 if (error)
2563 goto done;
2565 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2566 if (error != NULL)
2567 goto done;
2570 * unveil(2) traverses exec(2); if an editor is used we have
2571 * to apply unveil after the log message has been written.
2573 if (logmsg == NULL || strlen(logmsg) == 0)
2574 error = get_editor(&editor);
2575 else
2576 error = apply_unveil(got_repo_get_path(repo), 0,
2577 got_worktree_get_root_path(worktree), 0);
2578 if (error)
2579 goto done;
2581 cl_arg.editor = editor;
2582 cl_arg.cmdline_log = logmsg;
2583 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2584 cl_arg.repo_path = got_repo_get_path(repo);
2585 cl_arg.logmsg_path = NULL;
2586 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2587 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2588 if (error) {
2589 if (cl_arg.logmsg_path)
2590 fprintf(stderr, "%s: log message preserved in %s\n",
2591 getprogname(), cl_arg.logmsg_path);
2592 goto done;
2595 if (cl_arg.logmsg_path)
2596 unlink(cl_arg.logmsg_path);
2598 error = got_object_id_str(&id_str, id);
2599 if (error)
2600 goto done;
2601 printf("created commit %s\n", id_str);
2602 done:
2603 if (repo)
2604 got_repo_close(repo);
2605 if (worktree)
2606 got_worktree_close(worktree);
2607 free(path);
2608 free(cwd);
2609 free(id_str);
2610 free(editor);
2611 return error;
2614 __dead static void
2615 usage_cherrypick(void)
2617 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2618 exit(1);
2621 static const struct got_error *
2622 cmd_cherrypick(int argc, char *argv[])
2624 const struct got_error *error = NULL;
2625 struct got_worktree *worktree = NULL;
2626 struct got_repository *repo = NULL;
2627 char *cwd = NULL, *commit_id_str = NULL;
2628 struct got_object_id *commit_id = NULL;
2629 struct got_commit_object *commit = NULL;
2630 struct got_object_qid *pid;
2631 struct got_reference *head_ref = NULL;
2632 int ch, did_something = 0;
2634 while ((ch = getopt(argc, argv, "")) != -1) {
2635 switch (ch) {
2636 default:
2637 usage_cherrypick();
2638 /* NOTREACHED */
2642 argc -= optind;
2643 argv += optind;
2645 if (argc != 1)
2646 usage_cherrypick();
2648 cwd = getcwd(NULL, 0);
2649 if (cwd == NULL) {
2650 error = got_error_from_errno("getcwd");
2651 goto done;
2653 error = got_worktree_open(&worktree, cwd);
2654 if (error)
2655 goto done;
2657 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2658 if (error != NULL)
2659 goto done;
2661 error = apply_unveil(got_repo_get_path(repo), 0,
2662 got_worktree_get_root_path(worktree), 0);
2663 if (error)
2664 goto done;
2666 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2667 if (error != NULL) {
2668 struct got_reference *ref;
2669 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2670 goto done;
2671 error = got_ref_open(&ref, repo, argv[0], 0);
2672 if (error != NULL)
2673 goto done;
2674 error = got_ref_resolve(&commit_id, repo, ref);
2675 got_ref_close(ref);
2676 if (error != NULL)
2677 goto done;
2679 error = got_object_id_str(&commit_id_str, commit_id);
2680 if (error)
2681 goto done;
2683 error = got_ref_open(&head_ref, repo,
2684 got_worktree_get_head_ref_name(worktree), 0);
2685 if (error != NULL)
2686 goto done;
2688 error = check_same_branch(commit_id, head_ref, repo);
2689 if (error) {
2690 if (error->code != GOT_ERR_ANCESTRY)
2691 goto done;
2692 error = NULL;
2693 } else {
2694 error = got_error(GOT_ERR_SAME_BRANCH);
2695 goto done;
2698 error = got_object_open_as_commit(&commit, repo, commit_id);
2699 if (error)
2700 goto done;
2701 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2702 if (pid == NULL) {
2703 error = got_error(GOT_ERR_ROOT_COMMIT);
2704 goto done;
2706 error = got_worktree_merge_files(worktree, pid->id, commit_id,
2707 repo, update_progress, &did_something, check_cancelled, NULL);
2708 if (error != NULL)
2709 goto done;
2711 if (did_something)
2712 printf("merged commit %s\n", commit_id_str);
2713 done:
2714 if (commit)
2715 got_object_commit_close(commit);
2716 free(commit_id_str);
2717 if (head_ref)
2718 got_ref_close(head_ref);
2719 if (worktree)
2720 got_worktree_close(worktree);
2721 if (repo)
2722 got_repo_close(repo);
2723 return error;