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);
90 __dead static void usage_backout(void);
92 static const struct got_error* cmd_checkout(int, char *[]);
93 static const struct got_error* cmd_update(int, char *[]);
94 static const struct got_error* cmd_log(int, char *[]);
95 static const struct got_error* cmd_diff(int, char *[]);
96 static const struct got_error* cmd_blame(int, char *[]);
97 static const struct got_error* cmd_tree(int, char *[]);
98 static const struct got_error* cmd_status(int, char *[]);
99 static const struct got_error* cmd_ref(int, char *[]);
100 static const struct got_error* cmd_add(int, char *[]);
101 static const struct got_error* cmd_rm(int, char *[]);
102 static const struct got_error* cmd_revert(int, char *[]);
103 static const struct got_error* cmd_commit(int, char *[]);
104 static const struct got_error* cmd_cherrypick(int, char *[]);
105 static const struct got_error* cmd_backout(int, char *[]);
107 static struct cmd got_commands[] = {
108 { "checkout", cmd_checkout, usage_checkout,
109 "check out a new work tree from a repository" },
110 { "update", cmd_update, usage_update,
111 "update a work tree to a different commit" },
112 { "log", cmd_log, usage_log,
113 "show repository history" },
114 { "diff", cmd_diff, usage_diff,
115 "compare files and directories" },
116 { "blame", cmd_blame, usage_blame,
117 "show when lines in a file were changed" },
118 { "tree", cmd_tree, usage_tree,
119 "list files and directories in repository" },
120 { "status", cmd_status, usage_status,
121 "show modification status of files" },
122 { "ref", cmd_ref, usage_ref,
123 "manage references in repository" },
124 { "add", cmd_add, usage_add,
125 "add new files to version control" },
126 { "rm", cmd_rm, usage_rm,
127 "remove a versioned file" },
128 { "revert", cmd_revert, usage_revert,
129 "revert uncommitted changes" },
130 { "commit", cmd_commit, usage_commit,
131 "write changes from work tree to repository" },
132 { "cherrypick", cmd_cherrypick, usage_cherrypick,
133 "merge a single commit from another branch into a work tree" },
134 { "backout", cmd_backout, usage_backout,
135 "reverse-merge changes from a commit into a work tree" },
136 };
138 int
139 main(int argc, char *argv[])
141 struct cmd *cmd;
142 unsigned int i;
143 int ch;
144 int hflag = 0;
146 setlocale(LC_CTYPE, "");
148 while ((ch = getopt(argc, argv, "h")) != -1) {
149 switch (ch) {
150 case 'h':
151 hflag = 1;
152 break;
153 default:
154 usage();
155 /* NOTREACHED */
159 argc -= optind;
160 argv += optind;
161 optind = 0;
163 if (argc <= 0)
164 usage();
166 signal(SIGINT, catch_sigint);
167 signal(SIGPIPE, catch_sigpipe);
169 for (i = 0; i < nitems(got_commands); i++) {
170 const struct got_error *error;
172 cmd = &got_commands[i];
174 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
175 continue;
177 if (hflag)
178 got_commands[i].cmd_usage();
180 error = got_commands[i].cmd_main(argc, argv);
181 if (error && !(sigint_received || sigpipe_received)) {
182 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
183 return 1;
186 return 0;
189 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
190 return 1;
193 __dead static void
194 usage(void)
196 int i;
198 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
199 "Available commands:\n", getprogname());
200 for (i = 0; i < nitems(got_commands); i++) {
201 struct cmd *cmd = &got_commands[i];
202 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
204 exit(1);
207 static const struct got_error *
208 get_editor(char **abspath)
210 const struct got_error *err = NULL;
211 const char *editor;
213 editor = getenv("VISUAL");
214 if (editor == NULL)
215 editor = getenv("EDITOR");
217 if (editor) {
218 err = got_path_find_prog(abspath, editor);
219 if (err)
220 return err;
223 if (*abspath == NULL) {
224 *abspath = strdup("/bin/ed");
225 if (*abspath == NULL)
226 return got_error_from_errno("strdup");
229 return NULL;
232 static const struct got_error *
233 apply_unveil(const char *repo_path, int repo_read_only,
234 const char *worktree_path, int create_worktree)
236 const struct got_error *err;
238 if (create_worktree) {
239 /* Pre-create work tree path to avoid unveiling its parents. */
240 err = got_path_mkdir(worktree_path);
242 if (errno == EEXIST) {
243 if (got_path_dir_is_empty(worktree_path)) {
244 errno = 0;
245 err = NULL;
246 } else {
247 err = got_error_path(worktree_path,
248 GOT_ERR_DIR_NOT_EMPTY);
252 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
253 return err;
256 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
257 return got_error_from_errno2("unveil", repo_path);
259 if (worktree_path && unveil(worktree_path, "rwc") != 0)
260 return got_error_from_errno2("unveil", worktree_path);
262 if (unveil("/tmp", "rwc") != 0)
263 return got_error_from_errno2("unveil", "/tmp");
265 err = got_privsep_unveil_exec_helpers();
266 if (err != NULL)
267 return err;
269 if (unveil(NULL, NULL) != 0)
270 return got_error_from_errno("unveil");
272 return NULL;
275 __dead static void
276 usage_checkout(void)
278 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
279 "[-p prefix] repository-path [worktree-path]\n", getprogname());
280 exit(1);
283 static void
284 checkout_progress(void *arg, unsigned char status, const char *path)
286 char *worktree_path = arg;
288 while (path[0] == '/')
289 path++;
291 printf("%c %s/%s\n", status, worktree_path, path);
294 static const struct got_error *
295 check_cancelled(void *arg)
297 if (sigint_received || sigpipe_received)
298 return got_error(GOT_ERR_CANCELLED);
299 return NULL;
302 static const struct got_error *
303 check_linear_ancestry(struct got_object_id *commit_id,
304 struct got_object_id *base_commit_id, struct got_repository *repo)
306 const struct got_error *err = NULL;
307 struct got_object_id *yca_id;
309 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
310 commit_id, base_commit_id, repo);
311 if (err)
312 return err;
314 if (yca_id == NULL)
315 return got_error(GOT_ERR_ANCESTRY);
317 /*
318 * Require a straight line of history between the target commit
319 * and the work tree's base commit.
321 * Non-linear situations such as this require a rebase:
323 * (commit) D F (base_commit)
324 * \ /
325 * C E
326 * \ /
327 * B (yca)
328 * |
329 * A
331 * 'got update' only handles linear cases:
332 * Update forwards in time: A (base/yca) - B - C - D (commit)
333 * Update backwards in time: D (base) - C - B - A (commit/yca)
334 */
335 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
336 got_object_id_cmp(base_commit_id, yca_id) != 0)
337 return got_error(GOT_ERR_ANCESTRY);
339 free(yca_id);
340 return NULL;
343 static const struct got_error *
344 check_same_branch(struct got_object_id *commit_id,
345 struct got_reference *head_ref, struct got_repository *repo)
347 const struct got_error *err = NULL;
348 struct got_commit_graph *graph = NULL;
349 struct got_object_id *head_commit_id = NULL;
350 int is_same_branch = 0;
352 err = got_ref_resolve(&head_commit_id, repo, head_ref);
353 if (err)
354 goto done;
356 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
357 if (err)
358 goto done;
360 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
361 if (err)
362 goto done;
364 for (;;) {
365 struct got_object_id *id;
366 err = got_commit_graph_iter_next(&id, graph);
367 if (err) {
368 if (err->code == GOT_ERR_ITER_COMPLETED) {
369 err = NULL;
370 break;
372 else if (err->code != GOT_ERR_ITER_NEED_MORE)
373 break;
374 err = got_commit_graph_fetch_commits(graph, 1,
375 repo);
376 if (err)
377 break;
380 if (id) {
381 if (got_object_id_cmp(id, commit_id) == 0) {
382 is_same_branch = 1;
383 break;
387 done:
388 if (graph)
389 got_commit_graph_close(graph);
390 free(head_commit_id);
391 if (!err && !is_same_branch)
392 err = got_error(GOT_ERR_ANCESTRY);
393 return err;
396 static const struct got_error *
397 cmd_checkout(int argc, char *argv[])
399 const struct got_error *error = NULL;
400 struct got_repository *repo = NULL;
401 struct got_reference *head_ref = NULL;
402 struct got_worktree *worktree = NULL;
403 char *repo_path = NULL;
404 char *worktree_path = NULL;
405 const char *path_prefix = "";
406 const char *branch_name = GOT_REF_HEAD;
407 char *commit_id_str = NULL;
408 int ch, same_path_prefix;
410 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
411 switch (ch) {
412 case 'b':
413 branch_name = optarg;
414 break;
415 case 'c':
416 commit_id_str = strdup(optarg);
417 if (commit_id_str == NULL)
418 return got_error_from_errno("strdup");
419 break;
420 case 'p':
421 path_prefix = optarg;
422 break;
423 default:
424 usage_checkout();
425 /* NOTREACHED */
429 argc -= optind;
430 argv += optind;
432 #ifndef PROFILE
433 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
434 "unveil", NULL) == -1)
435 err(1, "pledge");
436 #endif
437 if (argc == 1) {
438 char *cwd, *base, *dotgit;
439 repo_path = realpath(argv[0], NULL);
440 if (repo_path == NULL)
441 return got_error_from_errno2("realpath", argv[0]);
442 cwd = getcwd(NULL, 0);
443 if (cwd == NULL) {
444 error = got_error_from_errno("getcwd");
445 goto done;
447 if (path_prefix[0]) {
448 base = basename(path_prefix);
449 if (base == NULL) {
450 error = got_error_from_errno2("basename",
451 path_prefix);
452 goto done;
454 } else {
455 base = basename(repo_path);
456 if (base == NULL) {
457 error = got_error_from_errno2("basename",
458 repo_path);
459 goto done;
462 dotgit = strstr(base, ".git");
463 if (dotgit)
464 *dotgit = '\0';
465 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
466 error = got_error_from_errno("asprintf");
467 free(cwd);
468 goto done;
470 free(cwd);
471 } else if (argc == 2) {
472 repo_path = realpath(argv[0], NULL);
473 if (repo_path == NULL) {
474 error = got_error_from_errno2("realpath", argv[0]);
475 goto done;
477 worktree_path = realpath(argv[1], NULL);
478 if (worktree_path == NULL) {
479 error = got_error_from_errno2("realpath", argv[1]);
480 goto done;
482 } else
483 usage_checkout();
485 got_path_strip_trailing_slashes(repo_path);
486 got_path_strip_trailing_slashes(worktree_path);
488 error = got_repo_open(&repo, repo_path);
489 if (error != NULL)
490 goto done;
492 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
493 if (error)
494 goto done;
496 error = got_ref_open(&head_ref, repo, branch_name, 0);
497 if (error != NULL)
498 goto done;
500 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
501 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
502 goto done;
504 error = got_worktree_open(&worktree, worktree_path);
505 if (error != NULL)
506 goto done;
508 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
509 path_prefix);
510 if (error != NULL)
511 goto done;
512 if (!same_path_prefix) {
513 error = got_error(GOT_ERR_PATH_PREFIX);
514 goto done;
517 if (commit_id_str) {
518 struct got_object_id *commit_id;
519 error = got_object_resolve_id_str(&commit_id, repo,
520 commit_id_str);
521 if (error != NULL)
522 goto done;
523 error = check_linear_ancestry(commit_id,
524 got_worktree_get_base_commit_id(worktree), repo);
525 if (error != NULL) {
526 free(commit_id);
527 goto done;
529 error = check_same_branch(commit_id, head_ref, repo);
530 if (error)
531 goto done;
532 error = got_worktree_set_base_commit_id(worktree, repo,
533 commit_id);
534 free(commit_id);
535 if (error)
536 goto done;
539 error = got_worktree_checkout_files(worktree, "", repo,
540 checkout_progress, worktree_path, check_cancelled, NULL);
541 if (error != NULL)
542 goto done;
544 printf("Now shut up and hack\n");
546 done:
547 free(commit_id_str);
548 free(repo_path);
549 free(worktree_path);
550 return error;
553 __dead static void
554 usage_update(void)
556 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
557 getprogname());
558 exit(1);
561 static void
562 update_progress(void *arg, unsigned char status, const char *path)
564 int *did_something = arg;
566 if (status == GOT_STATUS_EXISTS)
567 return;
569 *did_something = 1;
570 while (path[0] == '/')
571 path++;
572 printf("%c %s\n", status, path);
575 static const struct got_error *
576 switch_head_ref(struct got_reference *head_ref,
577 struct got_object_id *commit_id, struct got_worktree *worktree,
578 struct got_repository *repo)
580 const struct got_error *err = NULL;
581 char *base_id_str;
582 int ref_has_moved = 0;
584 /* Trivial case: switching between two different references. */
585 if (strcmp(got_ref_get_name(head_ref),
586 got_worktree_get_head_ref_name(worktree)) != 0) {
587 printf("Switching work tree from %s to %s\n",
588 got_worktree_get_head_ref_name(worktree),
589 got_ref_get_name(head_ref));
590 return got_worktree_set_head_ref(worktree, head_ref);
593 err = check_linear_ancestry(commit_id,
594 got_worktree_get_base_commit_id(worktree), repo);
595 if (err) {
596 if (err->code != GOT_ERR_ANCESTRY)
597 return err;
598 ref_has_moved = 1;
600 if (!ref_has_moved)
601 return NULL;
603 /* Switching to a rebased branch with the same reference name. */
604 err = got_object_id_str(&base_id_str,
605 got_worktree_get_base_commit_id(worktree));
606 if (err)
607 return err;
608 printf("Reference %s now points at a different branch\n",
609 got_worktree_get_head_ref_name(worktree));
610 printf("Switching work tree from %s to %s\n", base_id_str,
611 got_worktree_get_head_ref_name(worktree));
612 return NULL;
615 static const struct got_error *
616 cmd_update(int argc, char *argv[])
618 const struct got_error *error = NULL;
619 struct got_repository *repo = NULL;
620 struct got_worktree *worktree = NULL;
621 char *worktree_path = NULL, *path = NULL;
622 struct got_object_id *commit_id = NULL;
623 char *commit_id_str = NULL;
624 const char *branch_name = NULL;
625 struct got_reference *head_ref = NULL;
626 int ch, did_something = 0;
628 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
629 switch (ch) {
630 case 'b':
631 branch_name = optarg;
632 break;
633 case 'c':
634 commit_id_str = strdup(optarg);
635 if (commit_id_str == NULL)
636 return got_error_from_errno("strdup");
637 break;
638 default:
639 usage_update();
640 /* NOTREACHED */
644 argc -= optind;
645 argv += optind;
647 #ifndef PROFILE
648 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
649 "unveil", NULL) == -1)
650 err(1, "pledge");
651 #endif
652 worktree_path = getcwd(NULL, 0);
653 if (worktree_path == NULL) {
654 error = got_error_from_errno("getcwd");
655 goto done;
657 error = got_worktree_open(&worktree, worktree_path);
658 if (error)
659 goto done;
661 if (argc == 0) {
662 path = strdup("");
663 if (path == NULL) {
664 error = got_error_from_errno("strdup");
665 goto done;
667 } else if (argc == 1) {
668 error = got_worktree_resolve_path(&path, worktree, argv[0]);
669 if (error)
670 goto done;
671 } else
672 usage_update();
674 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
675 if (error != NULL)
676 goto done;
678 error = apply_unveil(got_repo_get_path(repo), 0,
679 got_worktree_get_root_path(worktree), 0);
680 if (error)
681 goto done;
683 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
684 got_worktree_get_head_ref_name(worktree), 0);
685 if (error != NULL)
686 goto done;
687 if (commit_id_str == NULL) {
688 error = got_ref_resolve(&commit_id, repo, head_ref);
689 if (error != NULL)
690 goto done;
691 error = got_object_id_str(&commit_id_str, commit_id);
692 if (error != NULL)
693 goto done;
694 } else {
695 error = got_object_resolve_id_str(&commit_id, repo,
696 commit_id_str);
697 if (error != NULL)
698 goto done;
701 if (branch_name) {
702 struct got_object_id *head_commit_id;
703 if (strlen(path) != 0) {
704 fprintf(stderr, "%s: switching between branches "
705 "requires that the entire work tree "
706 "gets updated, not just '%s'\n",
707 getprogname(), path);
708 error = got_error(GOT_ERR_BAD_PATH);
709 goto done;
711 error = got_ref_resolve(&head_commit_id, repo, head_ref);
712 if (error)
713 goto done;
714 error = check_linear_ancestry(commit_id, head_commit_id, repo);
715 free(head_commit_id);
716 if (error != NULL)
717 goto done;
718 error = check_same_branch(commit_id, head_ref, repo);
719 if (error)
720 goto done;
721 error = switch_head_ref(head_ref, commit_id, worktree, repo);
722 if (error)
723 goto done;
724 } else {
725 error = check_linear_ancestry(commit_id,
726 got_worktree_get_base_commit_id(worktree), repo);
727 if (error != NULL) {
728 if (error->code == GOT_ERR_ANCESTRY)
729 error = got_error(GOT_ERR_BRANCH_MOVED);
730 goto done;
732 error = check_same_branch(commit_id, head_ref, repo);
733 if (error)
734 goto done;
737 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
738 commit_id) != 0) {
739 error = got_worktree_set_base_commit_id(worktree, repo,
740 commit_id);
741 if (error)
742 goto done;
745 error = got_worktree_checkout_files(worktree, path, repo,
746 update_progress, &did_something, check_cancelled, NULL);
747 if (error != NULL)
748 goto done;
750 if (did_something)
751 printf("Updated to commit %s\n", commit_id_str);
752 else
753 printf("Already up-to-date\n");
754 done:
755 free(worktree_path);
756 free(path);
757 free(commit_id);
758 free(commit_id_str);
759 return error;
762 static const struct got_error *
763 print_patch(struct got_commit_object *commit, struct got_object_id *id,
764 int diff_context, struct got_repository *repo)
766 const struct got_error *err = NULL;
767 struct got_tree_object *tree1 = NULL, *tree2;
768 struct got_object_qid *qid;
769 char *id_str1 = NULL, *id_str2;
770 struct got_diff_blob_output_unidiff_arg arg;
772 err = got_object_open_as_tree(&tree2, repo,
773 got_object_commit_get_tree_id(commit));
774 if (err)
775 return err;
777 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
778 if (qid != NULL) {
779 struct got_commit_object *pcommit;
781 err = got_object_open_as_commit(&pcommit, repo, qid->id);
782 if (err)
783 return err;
785 err = got_object_open_as_tree(&tree1, repo,
786 got_object_commit_get_tree_id(pcommit));
787 got_object_commit_close(pcommit);
788 if (err)
789 return err;
791 err = got_object_id_str(&id_str1, qid->id);
792 if (err)
793 return err;
796 err = got_object_id_str(&id_str2, id);
797 if (err)
798 goto done;
800 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
801 arg.diff_context = diff_context;
802 arg.outfile = stdout;
803 err = got_diff_tree(tree1, tree2, "", "", repo,
804 got_diff_blob_output_unidiff, &arg);
805 done:
806 if (tree1)
807 got_object_tree_close(tree1);
808 got_object_tree_close(tree2);
809 free(id_str1);
810 free(id_str2);
811 return err;
814 static char *
815 get_datestr(time_t *time, char *datebuf)
817 char *p, *s = ctime_r(time, datebuf);
818 p = strchr(s, '\n');
819 if (p)
820 *p = '\0';
821 return s;
824 static const struct got_error *
825 print_commit(struct got_commit_object *commit, struct got_object_id *id,
826 struct got_repository *repo, int show_patch, int diff_context,
827 struct got_reflist_head *refs)
829 const struct got_error *err = NULL;
830 char *id_str, *datestr, *logmsg0, *logmsg, *line;
831 char datebuf[26];
832 time_t committer_time;
833 const char *author, *committer;
834 char *refs_str = NULL;
835 struct got_reflist_entry *re;
837 SIMPLEQ_FOREACH(re, refs, entry) {
838 char *s;
839 const char *name;
840 if (got_object_id_cmp(re->id, id) != 0)
841 continue;
842 name = got_ref_get_name(re->ref);
843 if (strcmp(name, GOT_REF_HEAD) == 0)
844 continue;
845 if (strncmp(name, "refs/", 5) == 0)
846 name += 5;
847 if (strncmp(name, "got/", 4) == 0)
848 continue;
849 if (strncmp(name, "heads/", 6) == 0)
850 name += 6;
851 if (strncmp(name, "remotes/", 8) == 0)
852 name += 8;
853 s = refs_str;
854 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
855 name) == -1) {
856 err = got_error_from_errno("asprintf");
857 free(s);
858 break;
860 free(s);
862 err = got_object_id_str(&id_str, id);
863 if (err)
864 return err;
866 printf("-----------------------------------------------\n");
867 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
868 refs_str ? refs_str : "", refs_str ? ")" : "");
869 free(id_str);
870 id_str = NULL;
871 free(refs_str);
872 refs_str = NULL;
873 printf("from: %s\n", got_object_commit_get_author(commit));
874 committer_time = got_object_commit_get_committer_time(commit);
875 datestr = get_datestr(&committer_time, datebuf);
876 printf("date: %s UTC\n", datestr);
877 author = got_object_commit_get_author(commit);
878 committer = got_object_commit_get_committer(commit);
879 if (strcmp(author, committer) != 0)
880 printf("via: %s\n", committer);
881 if (got_object_commit_get_nparents(commit) > 1) {
882 const struct got_object_id_queue *parent_ids;
883 struct got_object_qid *qid;
884 int n = 1;
885 parent_ids = got_object_commit_get_parent_ids(commit);
886 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
887 err = got_object_id_str(&id_str, qid->id);
888 if (err)
889 return err;
890 printf("parent %d: %s\n", n++, id_str);
891 free(id_str);
895 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
896 if (logmsg0 == NULL)
897 return got_error_from_errno("strdup");
899 logmsg = logmsg0;
900 do {
901 line = strsep(&logmsg, "\n");
902 if (line)
903 printf(" %s\n", line);
904 } while (line);
905 free(logmsg0);
907 if (show_patch) {
908 err = print_patch(commit, id, diff_context, repo);
909 if (err == 0)
910 printf("\n");
913 if (fflush(stdout) != 0 && err == NULL)
914 err = got_error_from_errno("fflush");
915 return err;
918 static const struct got_error *
919 print_commits(struct got_object_id *root_id, struct got_repository *repo,
920 char *path, int show_patch, int diff_context, int limit,
921 int first_parent_traversal, struct got_reflist_head *refs)
923 const struct got_error *err;
924 struct got_commit_graph *graph;
926 err = got_commit_graph_open(&graph, root_id, path,
927 first_parent_traversal, repo);
928 if (err)
929 return err;
930 err = got_commit_graph_iter_start(graph, root_id, repo);
931 if (err)
932 goto done;
933 for (;;) {
934 struct got_commit_object *commit;
935 struct got_object_id *id;
937 if (sigint_received || sigpipe_received)
938 break;
940 err = got_commit_graph_iter_next(&id, graph);
941 if (err) {
942 if (err->code == GOT_ERR_ITER_COMPLETED) {
943 err = NULL;
944 break;
946 if (err->code != GOT_ERR_ITER_NEED_MORE)
947 break;
948 err = got_commit_graph_fetch_commits(graph, 1, repo);
949 if (err)
950 break;
951 else
952 continue;
954 if (id == NULL)
955 break;
957 err = got_object_open_as_commit(&commit, repo, id);
958 if (err)
959 break;
960 err = print_commit(commit, id, repo, show_patch, diff_context,
961 refs);
962 got_object_commit_close(commit);
963 if (err || (limit && --limit == 0))
964 break;
966 done:
967 got_commit_graph_close(graph);
968 return err;
971 __dead static void
972 usage_log(void)
974 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
975 "[-r repository-path] [path]\n", getprogname());
976 exit(1);
979 static const struct got_error *
980 cmd_log(int argc, char *argv[])
982 const struct got_error *error;
983 struct got_repository *repo = NULL;
984 struct got_worktree *worktree = NULL;
985 struct got_commit_object *commit = NULL;
986 struct got_object_id *id = NULL;
987 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
988 char *start_commit = NULL;
989 int diff_context = 3, ch;
990 int show_patch = 0, limit = 0, first_parent_traversal = 0;
991 const char *errstr;
992 struct got_reflist_head refs;
994 SIMPLEQ_INIT(&refs);
996 #ifndef PROFILE
997 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
998 NULL)
999 == -1)
1000 err(1, "pledge");
1001 #endif
1003 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1004 switch (ch) {
1005 case 'b':
1006 first_parent_traversal = 1;
1007 break;
1008 case 'p':
1009 show_patch = 1;
1010 break;
1011 case 'c':
1012 start_commit = optarg;
1013 break;
1014 case 'C':
1015 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1016 &errstr);
1017 if (errstr != NULL)
1018 err(1, "-C option %s", errstr);
1019 break;
1020 case 'l':
1021 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1022 if (errstr != NULL)
1023 err(1, "-l option %s", errstr);
1024 break;
1025 case 'r':
1026 repo_path = realpath(optarg, NULL);
1027 if (repo_path == NULL)
1028 err(1, "-r option");
1029 got_path_strip_trailing_slashes(repo_path);
1030 break;
1031 default:
1032 usage_log();
1033 /* NOTREACHED */
1037 argc -= optind;
1038 argv += optind;
1040 cwd = getcwd(NULL, 0);
1041 if (cwd == NULL) {
1042 error = got_error_from_errno("getcwd");
1043 goto done;
1046 error = got_worktree_open(&worktree, cwd);
1047 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1048 goto done;
1049 error = NULL;
1051 if (argc == 0) {
1052 path = strdup("");
1053 if (path == NULL) {
1054 error = got_error_from_errno("strdup");
1055 goto done;
1057 } else if (argc == 1) {
1058 if (worktree) {
1059 error = got_worktree_resolve_path(&path, worktree,
1060 argv[0]);
1061 if (error)
1062 goto done;
1063 } else {
1064 path = strdup(argv[0]);
1065 if (path == NULL) {
1066 error = got_error_from_errno("strdup");
1067 goto done;
1070 } else
1071 usage_log();
1073 if (repo_path == NULL) {
1074 repo_path = worktree ?
1075 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1077 if (repo_path == NULL) {
1078 error = got_error_from_errno("strdup");
1079 goto done;
1082 error = got_repo_open(&repo, repo_path);
1083 if (error != NULL)
1084 goto done;
1086 error = apply_unveil(got_repo_get_path(repo), 1,
1087 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1088 if (error)
1089 goto done;
1091 if (start_commit == NULL) {
1092 struct got_reference *head_ref;
1093 error = got_ref_open(&head_ref, repo,
1094 worktree ? got_worktree_get_head_ref_name(worktree)
1095 : GOT_REF_HEAD, 0);
1096 if (error != NULL)
1097 return error;
1098 error = got_ref_resolve(&id, repo, head_ref);
1099 got_ref_close(head_ref);
1100 if (error != NULL)
1101 return error;
1102 error = got_object_open_as_commit(&commit, repo, id);
1103 } else {
1104 struct got_reference *ref;
1105 error = got_ref_open(&ref, repo, start_commit, 0);
1106 if (error == NULL) {
1107 int obj_type;
1108 error = got_ref_resolve(&id, repo, ref);
1109 got_ref_close(ref);
1110 if (error != NULL)
1111 goto done;
1112 error = got_object_get_type(&obj_type, repo, id);
1113 if (error != NULL)
1114 goto done;
1115 if (obj_type == GOT_OBJ_TYPE_TAG) {
1116 struct got_tag_object *tag;
1117 error = got_object_open_as_tag(&tag, repo, id);
1118 if (error != NULL)
1119 goto done;
1120 if (got_object_tag_get_object_type(tag) !=
1121 GOT_OBJ_TYPE_COMMIT) {
1122 got_object_tag_close(tag);
1123 error = got_error(GOT_ERR_OBJ_TYPE);
1124 goto done;
1126 free(id);
1127 id = got_object_id_dup(
1128 got_object_tag_get_object_id(tag));
1129 if (id == NULL)
1130 error = got_error_from_errno(
1131 "got_object_id_dup");
1132 got_object_tag_close(tag);
1133 if (error)
1134 goto done;
1135 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1136 error = got_error(GOT_ERR_OBJ_TYPE);
1137 goto done;
1139 error = got_object_open_as_commit(&commit, repo, id);
1140 if (error != NULL)
1141 goto done;
1143 if (commit == NULL) {
1144 error = got_object_resolve_id_str(&id, repo,
1145 start_commit);
1146 if (error != NULL)
1147 return error;
1150 if (error != NULL)
1151 goto done;
1153 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1154 if (error != NULL)
1155 goto done;
1156 if (in_repo_path) {
1157 free(path);
1158 path = in_repo_path;
1161 error = got_ref_list(&refs, repo);
1162 if (error)
1163 goto done;
1165 error = print_commits(id, repo, path, show_patch,
1166 diff_context, limit, first_parent_traversal, &refs);
1167 done:
1168 free(path);
1169 free(repo_path);
1170 free(cwd);
1171 free(id);
1172 if (worktree)
1173 got_worktree_close(worktree);
1174 if (repo) {
1175 const struct got_error *repo_error;
1176 repo_error = got_repo_close(repo);
1177 if (error == NULL)
1178 error = repo_error;
1180 got_ref_list_free(&refs);
1181 return error;
1184 __dead static void
1185 usage_diff(void)
1187 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1188 "[object1 object2 | path]\n", getprogname());
1189 exit(1);
1192 struct print_diff_arg {
1193 struct got_repository *repo;
1194 struct got_worktree *worktree;
1195 int diff_context;
1196 const char *id_str;
1197 int header_shown;
1200 static const struct got_error *
1201 print_diff(void *arg, unsigned char status, const char *path,
1202 struct got_object_id *blob_id, struct got_object_id *commit_id)
1204 struct print_diff_arg *a = arg;
1205 const struct got_error *err = NULL;
1206 struct got_blob_object *blob1 = NULL;
1207 FILE *f2 = NULL;
1208 char *abspath = NULL;
1209 struct stat sb;
1211 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1212 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1213 return NULL;
1215 if (!a->header_shown) {
1216 printf("diff %s %s\n", a->id_str,
1217 got_worktree_get_root_path(a->worktree));
1218 a->header_shown = 1;
1221 if (status != GOT_STATUS_ADD) {
1222 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1223 if (err)
1224 goto done;
1228 if (status != GOT_STATUS_DELETE) {
1229 if (asprintf(&abspath, "%s/%s",
1230 got_worktree_get_root_path(a->worktree), path) == -1) {
1231 err = got_error_from_errno("asprintf");
1232 goto done;
1235 f2 = fopen(abspath, "r");
1236 if (f2 == NULL) {
1237 err = got_error_from_errno2("fopen", abspath);
1238 goto done;
1240 if (lstat(abspath, &sb) == -1) {
1241 err = got_error_from_errno2("lstat", abspath);
1242 goto done;
1244 } else
1245 sb.st_size = 0;
1247 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1248 stdout);
1249 done:
1250 if (blob1)
1251 got_object_blob_close(blob1);
1252 if (f2 && fclose(f2) != 0 && err == NULL)
1253 err = got_error_from_errno("fclose");
1254 free(abspath);
1255 return err;
1258 static const struct got_error *
1259 cmd_diff(int argc, char *argv[])
1261 const struct got_error *error;
1262 struct got_repository *repo = NULL;
1263 struct got_worktree *worktree = NULL;
1264 char *cwd = NULL, *repo_path = NULL;
1265 struct got_object_id *id1 = NULL, *id2 = NULL;
1266 const char *id_str1 = NULL, *id_str2 = NULL;
1267 char *label1 = NULL, *label2 = NULL;
1268 int type1, type2;
1269 int diff_context = 3, ch;
1270 const char *errstr;
1271 char *path = NULL;
1273 #ifndef PROFILE
1274 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1275 NULL) == -1)
1276 err(1, "pledge");
1277 #endif
1279 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1280 switch (ch) {
1281 case 'C':
1282 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1283 if (errstr != NULL)
1284 err(1, "-C option %s", errstr);
1285 break;
1286 case 'r':
1287 repo_path = realpath(optarg, NULL);
1288 if (repo_path == NULL)
1289 err(1, "-r option");
1290 got_path_strip_trailing_slashes(repo_path);
1291 break;
1292 default:
1293 usage_diff();
1294 /* NOTREACHED */
1298 argc -= optind;
1299 argv += optind;
1301 cwd = getcwd(NULL, 0);
1302 if (cwd == NULL) {
1303 error = got_error_from_errno("getcwd");
1304 goto done;
1306 error = got_worktree_open(&worktree, cwd);
1307 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1308 goto done;
1309 if (argc <= 1) {
1310 if (worktree == NULL) {
1311 error = got_error(GOT_ERR_NOT_WORKTREE);
1312 goto done;
1314 if (repo_path)
1315 errx(1,
1316 "-r option can't be used when diffing a work tree");
1317 repo_path = strdup(got_worktree_get_repo_path(worktree));
1318 if (repo_path == NULL) {
1319 error = got_error_from_errno("strdup");
1320 goto done;
1322 if (argc == 1) {
1323 error = got_worktree_resolve_path(&path, worktree,
1324 argv[0]);
1325 if (error)
1326 goto done;
1327 } else {
1328 path = strdup("");
1329 if (path == NULL) {
1330 error = got_error_from_errno("strdup");
1331 goto done;
1334 } else if (argc == 2) {
1335 id_str1 = argv[0];
1336 id_str2 = argv[1];
1337 if (worktree && repo_path == NULL) {
1338 repo_path =
1339 strdup(got_worktree_get_repo_path(worktree));
1340 if (repo_path == NULL) {
1341 error = got_error_from_errno("strdup");
1342 goto done;
1345 } else
1346 usage_diff();
1348 if (repo_path == NULL) {
1349 repo_path = getcwd(NULL, 0);
1350 if (repo_path == NULL)
1351 return got_error_from_errno("getcwd");
1354 error = got_repo_open(&repo, repo_path);
1355 free(repo_path);
1356 if (error != NULL)
1357 goto done;
1359 error = apply_unveil(got_repo_get_path(repo), 1,
1360 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1361 if (error)
1362 goto done;
1364 if (argc <= 1) {
1365 struct print_diff_arg arg;
1366 char *id_str;
1367 error = got_object_id_str(&id_str,
1368 got_worktree_get_base_commit_id(worktree));
1369 if (error)
1370 goto done;
1371 arg.repo = repo;
1372 arg.worktree = worktree;
1373 arg.diff_context = diff_context;
1374 arg.id_str = id_str;
1375 arg.header_shown = 0;
1377 error = got_worktree_status(worktree, path, repo, print_diff,
1378 &arg, check_cancelled, NULL);
1379 free(id_str);
1380 goto done;
1383 error = got_object_resolve_id_str(&id1, repo, id_str1);
1384 if (error) {
1385 struct got_reference *ref;
1386 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1387 goto done;
1388 error = got_ref_open(&ref, repo, id_str1, 0);
1389 if (error != NULL)
1390 goto done;
1391 label1 = strdup(got_ref_get_name(ref));
1392 if (label1 == NULL) {
1393 error = got_error_from_errno("strdup");
1394 goto done;
1396 error = got_ref_resolve(&id1, repo, ref);
1397 got_ref_close(ref);
1398 if (error != NULL)
1399 goto done;
1400 } else {
1401 label1 = strdup(id_str1);
1402 if (label1 == NULL) {
1403 error = got_error_from_errno("strdup");
1404 goto done;
1408 error = got_object_resolve_id_str(&id2, repo, id_str2);
1409 if (error) {
1410 struct got_reference *ref;
1411 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1412 goto done;
1413 error = got_ref_open(&ref, repo, id_str2, 0);
1414 if (error != NULL)
1415 goto done;
1416 label2 = strdup(got_ref_get_name(ref));
1417 if (label2 == NULL) {
1418 error = got_error_from_errno("strdup");
1419 goto done;
1421 error = got_ref_resolve(&id2, repo, ref);
1422 got_ref_close(ref);
1423 if (error != NULL)
1424 goto done;
1425 } else {
1426 label2 = strdup(id_str2);
1427 if (label2 == NULL) {
1428 error = got_error_from_errno("strdup");
1429 goto done;
1433 error = got_object_get_type(&type1, repo, id1);
1434 if (error)
1435 goto done;
1437 error = got_object_get_type(&type2, repo, id2);
1438 if (error)
1439 goto done;
1441 if (type1 != type2) {
1442 error = got_error(GOT_ERR_OBJ_TYPE);
1443 goto done;
1446 switch (type1) {
1447 case GOT_OBJ_TYPE_BLOB:
1448 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1449 diff_context, repo, stdout);
1450 break;
1451 case GOT_OBJ_TYPE_TREE:
1452 error = got_diff_objects_as_trees(id1, id2, "", "",
1453 diff_context, repo, stdout);
1454 break;
1455 case GOT_OBJ_TYPE_COMMIT:
1456 printf("diff %s %s\n", label1, label2);
1457 error = got_diff_objects_as_commits(id1, id2, diff_context,
1458 repo, stdout);
1459 break;
1460 default:
1461 error = got_error(GOT_ERR_OBJ_TYPE);
1464 done:
1465 free(label1);
1466 free(label2);
1467 free(id1);
1468 free(id2);
1469 free(path);
1470 if (worktree)
1471 got_worktree_close(worktree);
1472 if (repo) {
1473 const struct got_error *repo_error;
1474 repo_error = got_repo_close(repo);
1475 if (error == NULL)
1476 error = repo_error;
1478 return error;
1481 __dead static void
1482 usage_blame(void)
1484 fprintf(stderr,
1485 "usage: %s blame [-c commit] [-r repository-path] path\n",
1486 getprogname());
1487 exit(1);
1490 static const struct got_error *
1491 cmd_blame(int argc, char *argv[])
1493 const struct got_error *error;
1494 struct got_repository *repo = NULL;
1495 struct got_worktree *worktree = NULL;
1496 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1497 struct got_object_id *commit_id = NULL;
1498 char *commit_id_str = NULL;
1499 int ch;
1501 #ifndef PROFILE
1502 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1503 NULL) == -1)
1504 err(1, "pledge");
1505 #endif
1507 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1508 switch (ch) {
1509 case 'c':
1510 commit_id_str = optarg;
1511 break;
1512 case 'r':
1513 repo_path = realpath(optarg, NULL);
1514 if (repo_path == NULL)
1515 err(1, "-r option");
1516 got_path_strip_trailing_slashes(repo_path);
1517 break;
1518 default:
1519 usage_blame();
1520 /* NOTREACHED */
1524 argc -= optind;
1525 argv += optind;
1527 if (argc == 1)
1528 path = argv[0];
1529 else
1530 usage_blame();
1532 cwd = getcwd(NULL, 0);
1533 if (cwd == NULL) {
1534 error = got_error_from_errno("getcwd");
1535 goto done;
1537 if (repo_path == NULL) {
1538 error = got_worktree_open(&worktree, cwd);
1539 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1540 goto done;
1541 else
1542 error = NULL;
1543 if (worktree) {
1544 repo_path =
1545 strdup(got_worktree_get_repo_path(worktree));
1546 if (repo_path == NULL)
1547 error = got_error_from_errno("strdup");
1548 if (error)
1549 goto done;
1550 } else {
1551 repo_path = strdup(cwd);
1552 if (repo_path == NULL) {
1553 error = got_error_from_errno("strdup");
1554 goto done;
1559 error = got_repo_open(&repo, repo_path);
1560 if (error != NULL)
1561 goto done;
1563 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1564 if (error)
1565 goto done;
1567 if (worktree) {
1568 const char *prefix = got_worktree_get_path_prefix(worktree);
1569 char *p, *worktree_subdir = cwd +
1570 strlen(got_worktree_get_root_path(worktree));
1571 if (asprintf(&p, "%s%s%s%s%s",
1572 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1573 worktree_subdir, worktree_subdir[0] ? "/" : "",
1574 path) == -1) {
1575 error = got_error_from_errno("asprintf");
1576 goto done;
1578 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1579 free(p);
1580 } else {
1581 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1583 if (error)
1584 goto done;
1586 if (commit_id_str == NULL) {
1587 struct got_reference *head_ref;
1588 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1589 if (error != NULL)
1590 goto done;
1591 error = got_ref_resolve(&commit_id, repo, head_ref);
1592 got_ref_close(head_ref);
1593 if (error != NULL)
1594 goto done;
1595 } else {
1596 error = got_object_resolve_id_str(&commit_id, repo,
1597 commit_id_str);
1598 if (error != NULL)
1599 goto done;
1602 error = got_blame(in_repo_path, commit_id, repo, stdout);
1603 done:
1604 free(in_repo_path);
1605 free(repo_path);
1606 free(cwd);
1607 free(commit_id);
1608 if (worktree)
1609 got_worktree_close(worktree);
1610 if (repo) {
1611 const struct got_error *repo_error;
1612 repo_error = got_repo_close(repo);
1613 if (error == NULL)
1614 error = repo_error;
1616 return error;
1619 __dead static void
1620 usage_tree(void)
1622 fprintf(stderr,
1623 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1624 getprogname());
1625 exit(1);
1628 static void
1629 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1630 const char *root_path)
1632 int is_root_path = (strcmp(path, root_path) == 0);
1634 path += strlen(root_path);
1635 while (path[0] == '/')
1636 path++;
1638 printf("%s%s%s%s%s\n", id ? id : "", path,
1639 is_root_path ? "" : "/", te->name,
1640 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1643 static const struct got_error *
1644 print_tree(const char *path, struct got_object_id *commit_id,
1645 int show_ids, int recurse, const char *root_path,
1646 struct got_repository *repo)
1648 const struct got_error *err = NULL;
1649 struct got_object_id *tree_id = NULL;
1650 struct got_tree_object *tree = NULL;
1651 const struct got_tree_entries *entries;
1652 struct got_tree_entry *te;
1654 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1655 if (err)
1656 goto done;
1658 err = got_object_open_as_tree(&tree, repo, tree_id);
1659 if (err)
1660 goto done;
1661 entries = got_object_tree_get_entries(tree);
1662 te = SIMPLEQ_FIRST(&entries->head);
1663 while (te) {
1664 char *id = NULL;
1666 if (sigint_received || sigpipe_received)
1667 break;
1669 if (show_ids) {
1670 char *id_str;
1671 err = got_object_id_str(&id_str, te->id);
1672 if (err)
1673 goto done;
1674 if (asprintf(&id, "%s ", id_str) == -1) {
1675 err = got_error_from_errno("asprintf");
1676 free(id_str);
1677 goto done;
1679 free(id_str);
1681 print_entry(te, id, path, root_path);
1682 free(id);
1684 if (recurse && S_ISDIR(te->mode)) {
1685 char *child_path;
1686 if (asprintf(&child_path, "%s%s%s", path,
1687 path[0] == '/' && path[1] == '\0' ? "" : "/",
1688 te->name) == -1) {
1689 err = got_error_from_errno("asprintf");
1690 goto done;
1692 err = print_tree(child_path, commit_id, show_ids, 1,
1693 root_path, repo);
1694 free(child_path);
1695 if (err)
1696 goto done;
1699 te = SIMPLEQ_NEXT(te, entry);
1701 done:
1702 if (tree)
1703 got_object_tree_close(tree);
1704 free(tree_id);
1705 return err;
1708 static const struct got_error *
1709 cmd_tree(int argc, char *argv[])
1711 const struct got_error *error;
1712 struct got_repository *repo = NULL;
1713 struct got_worktree *worktree = NULL;
1714 const char *path;
1715 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1716 struct got_object_id *commit_id = NULL;
1717 char *commit_id_str = NULL;
1718 int show_ids = 0, recurse = 0;
1719 int ch;
1721 #ifndef PROFILE
1722 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1723 NULL) == -1)
1724 err(1, "pledge");
1725 #endif
1727 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1728 switch (ch) {
1729 case 'c':
1730 commit_id_str = optarg;
1731 break;
1732 case 'r':
1733 repo_path = realpath(optarg, NULL);
1734 if (repo_path == NULL)
1735 err(1, "-r option");
1736 got_path_strip_trailing_slashes(repo_path);
1737 break;
1738 case 'i':
1739 show_ids = 1;
1740 break;
1741 case 'R':
1742 recurse = 1;
1743 break;
1744 default:
1745 usage_tree();
1746 /* NOTREACHED */
1750 argc -= optind;
1751 argv += optind;
1753 if (argc == 1)
1754 path = argv[0];
1755 else if (argc > 1)
1756 usage_tree();
1757 else
1758 path = NULL;
1760 cwd = getcwd(NULL, 0);
1761 if (cwd == NULL) {
1762 error = got_error_from_errno("getcwd");
1763 goto done;
1765 if (repo_path == NULL) {
1766 error = got_worktree_open(&worktree, cwd);
1767 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1768 goto done;
1769 else
1770 error = NULL;
1771 if (worktree) {
1772 repo_path =
1773 strdup(got_worktree_get_repo_path(worktree));
1774 if (repo_path == NULL)
1775 error = got_error_from_errno("strdup");
1776 if (error)
1777 goto done;
1778 } else {
1779 repo_path = strdup(cwd);
1780 if (repo_path == NULL) {
1781 error = got_error_from_errno("strdup");
1782 goto done;
1787 error = got_repo_open(&repo, repo_path);
1788 if (error != NULL)
1789 goto done;
1791 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1792 if (error)
1793 goto done;
1795 if (path == NULL) {
1796 if (worktree) {
1797 char *p, *worktree_subdir = cwd +
1798 strlen(got_worktree_get_root_path(worktree));
1799 if (asprintf(&p, "%s/%s",
1800 got_worktree_get_path_prefix(worktree),
1801 worktree_subdir) == -1) {
1802 error = got_error_from_errno("asprintf");
1803 goto done;
1805 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1806 free(p);
1807 if (error)
1808 goto done;
1809 } else
1810 path = "/";
1812 if (in_repo_path == NULL) {
1813 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1814 if (error != NULL)
1815 goto done;
1818 if (commit_id_str == NULL) {
1819 struct got_reference *head_ref;
1820 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1821 if (error != NULL)
1822 goto done;
1823 error = got_ref_resolve(&commit_id, repo, head_ref);
1824 got_ref_close(head_ref);
1825 if (error != NULL)
1826 goto done;
1827 } else {
1828 error = got_object_resolve_id_str(&commit_id, repo,
1829 commit_id_str);
1830 if (error != NULL)
1831 goto done;
1834 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1835 in_repo_path, repo);
1836 done:
1837 free(in_repo_path);
1838 free(repo_path);
1839 free(cwd);
1840 free(commit_id);
1841 if (worktree)
1842 got_worktree_close(worktree);
1843 if (repo) {
1844 const struct got_error *repo_error;
1845 repo_error = got_repo_close(repo);
1846 if (error == NULL)
1847 error = repo_error;
1849 return error;
1852 __dead static void
1853 usage_status(void)
1855 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1856 exit(1);
1859 static const struct got_error *
1860 print_status(void *arg, unsigned char status, const char *path,
1861 struct got_object_id *blob_id, struct got_object_id *commit_id)
1863 printf("%c %s\n", status, path);
1864 return NULL;
1867 static const struct got_error *
1868 cmd_status(int argc, char *argv[])
1870 const struct got_error *error = NULL;
1871 struct got_repository *repo = NULL;
1872 struct got_worktree *worktree = NULL;
1873 char *cwd = NULL, *path = NULL;
1874 int ch;
1876 while ((ch = getopt(argc, argv, "")) != -1) {
1877 switch (ch) {
1878 default:
1879 usage_status();
1880 /* NOTREACHED */
1884 argc -= optind;
1885 argv += optind;
1887 #ifndef PROFILE
1888 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1889 NULL) == -1)
1890 err(1, "pledge");
1891 #endif
1892 cwd = getcwd(NULL, 0);
1893 if (cwd == NULL) {
1894 error = got_error_from_errno("getcwd");
1895 goto done;
1898 error = got_worktree_open(&worktree, cwd);
1899 if (error != NULL)
1900 goto done;
1902 if (argc == 0) {
1903 path = strdup("");
1904 if (path == NULL) {
1905 error = got_error_from_errno("strdup");
1906 goto done;
1908 } else if (argc == 1) {
1909 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1910 if (error)
1911 goto done;
1912 } else
1913 usage_status();
1915 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1916 if (error != NULL)
1917 goto done;
1919 error = apply_unveil(got_repo_get_path(repo), 1,
1920 got_worktree_get_root_path(worktree), 0);
1921 if (error)
1922 goto done;
1924 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1925 check_cancelled, NULL);
1926 done:
1927 free(cwd);
1928 free(path);
1929 return error;
1932 __dead static void
1933 usage_ref(void)
1935 fprintf(stderr,
1936 "usage: %s ref [-r repository] -l | -d name | name target\n",
1937 getprogname());
1938 exit(1);
1941 static const struct got_error *
1942 list_refs(struct got_repository *repo)
1944 static const struct got_error *err = NULL;
1945 struct got_reflist_head refs;
1946 struct got_reflist_entry *re;
1948 SIMPLEQ_INIT(&refs);
1949 err = got_ref_list(&refs, repo);
1950 if (err)
1951 return err;
1953 SIMPLEQ_FOREACH(re, &refs, entry) {
1954 char *refstr;
1955 refstr = got_ref_to_str(re->ref);
1956 if (refstr == NULL)
1957 return got_error_from_errno("got_ref_to_str");
1958 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1959 free(refstr);
1962 got_ref_list_free(&refs);
1963 return NULL;
1966 static const struct got_error *
1967 delete_ref(struct got_repository *repo, const char *refname)
1969 const struct got_error *err = NULL;
1970 struct got_reference *ref;
1972 err = got_ref_open(&ref, repo, refname, 0);
1973 if (err)
1974 return err;
1976 err = got_ref_delete(ref, repo);
1977 got_ref_close(ref);
1978 return err;
1981 static const struct got_error *
1982 add_ref(struct got_repository *repo, const char *refname, const char *target)
1984 const struct got_error *err = NULL;
1985 struct got_object_id *id;
1986 struct got_reference *ref = NULL;
1988 err = got_object_resolve_id_str(&id, repo, target);
1989 if (err) {
1990 struct got_reference *target_ref;
1992 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1993 return err;
1994 err = got_ref_open(&target_ref, repo, target, 0);
1995 if (err)
1996 return err;
1997 err = got_ref_resolve(&id, repo, target_ref);
1998 got_ref_close(target_ref);
1999 if (err)
2000 return err;
2003 err = got_ref_alloc(&ref, refname, id);
2004 if (err)
2005 goto done;
2007 err = got_ref_write(ref, repo);
2008 done:
2009 if (ref)
2010 got_ref_close(ref);
2011 free(id);
2012 return err;
2015 static const struct got_error *
2016 cmd_ref(int argc, char *argv[])
2018 const struct got_error *error = NULL;
2019 struct got_repository *repo = NULL;
2020 struct got_worktree *worktree = NULL;
2021 char *cwd = NULL, *repo_path = NULL;
2022 int ch, do_list = 0;
2023 const char *delref = NULL;
2025 /* TODO: Add -s option for adding symbolic references. */
2026 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2027 switch (ch) {
2028 case 'd':
2029 delref = optarg;
2030 break;
2031 case 'r':
2032 repo_path = realpath(optarg, NULL);
2033 if (repo_path == NULL)
2034 err(1, "-r option");
2035 got_path_strip_trailing_slashes(repo_path);
2036 break;
2037 case 'l':
2038 do_list = 1;
2039 break;
2040 default:
2041 usage_ref();
2042 /* NOTREACHED */
2046 if (do_list && delref)
2047 errx(1, "-l and -d options are mutually exclusive\n");
2049 argc -= optind;
2050 argv += optind;
2052 if (do_list || delref) {
2053 if (argc > 0)
2054 usage_ref();
2055 } else if (argc != 2)
2056 usage_ref();
2058 #ifndef PROFILE
2059 if (do_list) {
2060 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2061 NULL) == -1)
2062 err(1, "pledge");
2063 } else {
2064 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2065 "sendfd unveil", NULL) == -1)
2066 err(1, "pledge");
2068 #endif
2069 cwd = getcwd(NULL, 0);
2070 if (cwd == NULL) {
2071 error = got_error_from_errno("getcwd");
2072 goto done;
2075 if (repo_path == NULL) {
2076 error = got_worktree_open(&worktree, cwd);
2077 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2078 goto done;
2079 else
2080 error = NULL;
2081 if (worktree) {
2082 repo_path =
2083 strdup(got_worktree_get_repo_path(worktree));
2084 if (repo_path == NULL)
2085 error = got_error_from_errno("strdup");
2086 if (error)
2087 goto done;
2088 } else {
2089 repo_path = strdup(cwd);
2090 if (repo_path == NULL) {
2091 error = got_error_from_errno("strdup");
2092 goto done;
2097 error = got_repo_open(&repo, repo_path);
2098 if (error != NULL)
2099 goto done;
2101 error = apply_unveil(got_repo_get_path(repo), do_list,
2102 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2103 if (error)
2104 goto done;
2106 if (do_list)
2107 error = list_refs(repo);
2108 else if (delref)
2109 error = delete_ref(repo, delref);
2110 else
2111 error = add_ref(repo, argv[0], argv[1]);
2112 done:
2113 if (repo)
2114 got_repo_close(repo);
2115 if (worktree)
2116 got_worktree_close(worktree);
2117 free(cwd);
2118 free(repo_path);
2119 return error;
2122 __dead static void
2123 usage_add(void)
2125 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2126 exit(1);
2129 static const struct got_error *
2130 cmd_add(int argc, char *argv[])
2132 const struct got_error *error = NULL;
2133 struct got_repository *repo = NULL;
2134 struct got_worktree *worktree = NULL;
2135 char *cwd = NULL;
2136 struct got_pathlist_head paths;
2137 struct got_pathlist_entry *pe;
2138 int ch, x;
2140 TAILQ_INIT(&paths);
2142 while ((ch = getopt(argc, argv, "")) != -1) {
2143 switch (ch) {
2144 default:
2145 usage_add();
2146 /* NOTREACHED */
2150 argc -= optind;
2151 argv += optind;
2153 if (argc < 1)
2154 usage_add();
2156 /* make sure each file exists before doing anything halfway */
2157 for (x = 0; x < argc; x++) {
2158 char *path = realpath(argv[x], NULL);
2159 if (path == NULL) {
2160 error = got_error_from_errno2("realpath", argv[x]);
2161 goto done;
2163 free(path);
2166 cwd = getcwd(NULL, 0);
2167 if (cwd == NULL) {
2168 error = got_error_from_errno("getcwd");
2169 goto done;
2172 error = got_worktree_open(&worktree, cwd);
2173 if (error)
2174 goto done;
2176 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2177 if (error != NULL)
2178 goto done;
2180 error = apply_unveil(got_repo_get_path(repo), 1,
2181 got_worktree_get_root_path(worktree), 0);
2182 if (error)
2183 goto done;
2185 for (x = 0; x < argc; x++) {
2186 char *path = realpath(argv[x], NULL);
2187 if (path == NULL) {
2188 error = got_error_from_errno2("realpath", argv[x]);
2189 goto done;
2192 got_path_strip_trailing_slashes(path);
2193 error = got_pathlist_insert(&pe, &paths, path, NULL);
2194 if (error) {
2195 free(path);
2196 goto done;
2199 error = got_worktree_schedule_add(worktree, &paths, print_status,
2200 NULL, repo);
2201 done:
2202 if (repo)
2203 got_repo_close(repo);
2204 if (worktree)
2205 got_worktree_close(worktree);
2206 TAILQ_FOREACH(pe, &paths, entry)
2207 free((char *)pe->path);
2208 got_pathlist_free(&paths);
2209 free(cwd);
2210 return error;
2213 __dead static void
2214 usage_rm(void)
2216 fprintf(stderr, "usage: %s rm [-f] file-path ...\n", getprogname());
2217 exit(1);
2220 static const struct got_error *
2221 cmd_rm(int argc, char *argv[])
2223 const struct got_error *error = NULL;
2224 struct got_worktree *worktree = NULL;
2225 struct got_repository *repo = NULL;
2226 char *cwd = NULL;
2227 struct got_pathlist_head paths;
2228 struct got_pathlist_entry *pe;
2229 int ch, i, delete_local_mods = 0;
2231 TAILQ_INIT(&paths);
2233 while ((ch = getopt(argc, argv, "f")) != -1) {
2234 switch (ch) {
2235 case 'f':
2236 delete_local_mods = 1;
2237 break;
2238 default:
2239 usage_add();
2240 /* NOTREACHED */
2244 argc -= optind;
2245 argv += optind;
2247 if (argc < 1)
2248 usage_rm();
2250 /* make sure each file exists before doing anything halfway */
2251 for (i = 0; i < argc; i++) {
2252 char *path = realpath(argv[i], NULL);
2253 if (path == NULL) {
2254 error = got_error_from_errno2("realpath", argv[i]);
2255 goto done;
2257 free(path);
2260 cwd = getcwd(NULL, 0);
2261 if (cwd == NULL) {
2262 error = got_error_from_errno("getcwd");
2263 goto done;
2265 error = got_worktree_open(&worktree, cwd);
2266 if (error)
2267 goto done;
2269 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2270 if (error)
2271 goto done;
2273 error = apply_unveil(got_repo_get_path(repo), 1,
2274 got_worktree_get_root_path(worktree), 0);
2275 if (error)
2276 goto done;
2278 for (i = 0; i < argc; i++) {
2279 char *path = realpath(argv[i], NULL);
2280 if (path == NULL) {
2281 error = got_error_from_errno2("realpath", argv[i]);
2282 goto done;
2285 got_path_strip_trailing_slashes(path);
2286 error = got_pathlist_insert(&pe, &paths, path, NULL);
2287 if (error) {
2288 free(path);
2289 goto done;
2292 error = got_worktree_schedule_delete(worktree, &paths,
2293 delete_local_mods, print_status, NULL, repo);
2294 if (error)
2295 goto done;
2296 done:
2297 if (repo)
2298 got_repo_close(repo);
2299 if (worktree)
2300 got_worktree_close(worktree);
2301 TAILQ_FOREACH(pe, &paths, entry)
2302 free((char *)pe->path);
2303 got_pathlist_free(&paths);
2304 free(cwd);
2305 return error;
2308 __dead static void
2309 usage_revert(void)
2311 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2312 exit(1);
2315 static void
2316 revert_progress(void *arg, unsigned char status, const char *path)
2318 while (path[0] == '/')
2319 path++;
2320 printf("%c %s\n", status, path);
2323 static const struct got_error *
2324 cmd_revert(int argc, char *argv[])
2326 const struct got_error *error = NULL;
2327 struct got_worktree *worktree = NULL;
2328 struct got_repository *repo = NULL;
2329 char *cwd = NULL, *path = NULL;
2330 struct got_pathlist_head paths;
2331 struct got_pathlist_entry *pe;
2332 int ch, i;
2334 TAILQ_INIT(&paths);
2336 while ((ch = getopt(argc, argv, "")) != -1) {
2337 switch (ch) {
2338 default:
2339 usage_revert();
2340 /* NOTREACHED */
2344 argc -= optind;
2345 argv += optind;
2347 if (argc < 1)
2348 usage_revert();
2350 for (i = 0; i < argc; i++) {
2351 char *path = realpath(argv[i], NULL);
2352 if (path == NULL) {
2353 error = got_error_from_errno2("realpath", argv[i]);
2354 goto done;
2357 got_path_strip_trailing_slashes(path);
2358 error = got_pathlist_insert(&pe, &paths, path, NULL);
2359 if (error) {
2360 free(path);
2361 goto done;
2365 cwd = getcwd(NULL, 0);
2366 if (cwd == NULL) {
2367 error = got_error_from_errno("getcwd");
2368 goto done;
2370 error = got_worktree_open(&worktree, cwd);
2371 if (error)
2372 goto done;
2374 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2375 if (error != NULL)
2376 goto done;
2378 error = apply_unveil(got_repo_get_path(repo), 1,
2379 got_worktree_get_root_path(worktree), 0);
2380 if (error)
2381 goto done;
2383 error = got_worktree_revert(worktree, &paths,
2384 revert_progress, NULL, repo);
2385 if (error)
2386 goto done;
2387 done:
2388 if (repo)
2389 got_repo_close(repo);
2390 if (worktree)
2391 got_worktree_close(worktree);
2392 free(path);
2393 free(cwd);
2394 return error;
2397 __dead static void
2398 usage_commit(void)
2400 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2401 exit(1);
2404 int
2405 spawn_editor(const char *editor, const char *file)
2407 pid_t pid;
2408 sig_t sighup, sigint, sigquit;
2409 int st = -1;
2411 sighup = signal(SIGHUP, SIG_IGN);
2412 sigint = signal(SIGINT, SIG_IGN);
2413 sigquit = signal(SIGQUIT, SIG_IGN);
2415 switch (pid = fork()) {
2416 case -1:
2417 goto doneediting;
2418 case 0:
2419 execl(editor, editor, file, (char *)NULL);
2420 _exit(127);
2423 while (waitpid(pid, &st, 0) == -1)
2424 if (errno != EINTR)
2425 break;
2427 doneediting:
2428 (void)signal(SIGHUP, sighup);
2429 (void)signal(SIGINT, sigint);
2430 (void)signal(SIGQUIT, sigquit);
2432 if (!WIFEXITED(st)) {
2433 errno = EINTR;
2434 return -1;
2437 return WEXITSTATUS(st);
2440 struct collect_commit_logmsg_arg {
2441 const char *cmdline_log;
2442 const char *editor;
2443 const char *worktree_path;
2444 const char *branch_name;
2445 const char *repo_path;
2446 char *logmsg_path;
2450 static const struct got_error *
2451 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2452 void *arg)
2454 char *initial_content = NULL;
2455 struct got_pathlist_entry *pe;
2456 const struct got_error *err = NULL;
2457 char *template = NULL;
2458 struct collect_commit_logmsg_arg *a = arg;
2459 char buf[1024];
2460 struct stat st, st2;
2461 FILE *fp;
2462 size_t len;
2463 int fd, content_changed = 0;
2465 /* if a message was specified on the command line, just use it */
2466 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2467 len = strlen(a->cmdline_log) + 1;
2468 *logmsg = malloc(len + 1);
2469 if (*logmsg == NULL)
2470 return got_error_from_errno("malloc");
2471 strlcpy(*logmsg, a->cmdline_log, len);
2472 return NULL;
2475 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2476 return got_error_from_errno("asprintf");
2478 if (asprintf(&initial_content,
2479 "\n# changes to be committed on branch %s:\n",
2480 a->branch_name) == -1)
2481 return got_error_from_errno("asprintf");
2483 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2484 if (err)
2485 goto done;
2487 dprintf(fd, initial_content);
2489 TAILQ_FOREACH(pe, commitable_paths, entry) {
2490 struct got_commitable *ct = pe->data;
2491 dprintf(fd, "# %c %s\n",
2492 got_commitable_get_status(ct),
2493 got_commitable_get_path(ct));
2495 close(fd);
2497 if (stat(a->logmsg_path, &st) == -1) {
2498 err = got_error_from_errno2("stat", a->logmsg_path);
2499 goto done;
2502 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2503 err = got_error_from_errno("failed spawning editor");
2504 goto done;
2507 if (stat(a->logmsg_path, &st2) == -1) {
2508 err = got_error_from_errno("stat");
2509 goto done;
2512 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2513 unlink(a->logmsg_path);
2514 free(a->logmsg_path);
2515 a->logmsg_path = NULL;
2516 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2517 "no changes made to commit message, aborting");
2518 goto done;
2521 *logmsg = malloc(st2.st_size + 1);
2522 if (*logmsg == NULL) {
2523 err = got_error_from_errno("malloc");
2524 goto done;
2526 (*logmsg)[0] = '\0';
2527 len = 0;
2529 fp = fopen(a->logmsg_path, "r");
2530 if (fp == NULL) {
2531 err = got_error_from_errno("fopen");
2532 goto done;
2534 while (fgets(buf, sizeof(buf), fp) != NULL) {
2535 if (!content_changed && strcmp(buf, initial_content) != 0)
2536 content_changed = 1;
2537 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2538 continue; /* remove comments and leading empty lines */
2539 len = strlcat(*logmsg, buf, st2.st_size);
2541 fclose(fp);
2543 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2544 (*logmsg)[len - 1] = '\0';
2545 len--;
2548 if (len == 0 || !content_changed) {
2549 unlink(a->logmsg_path);
2550 free(a->logmsg_path);
2551 a->logmsg_path = NULL;
2552 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2553 "commit message cannot be empty, aborting");
2554 goto done;
2556 done:
2557 free(initial_content);
2558 free(template);
2560 /* Editor is done; we can now apply unveil(2) */
2561 if (err == NULL)
2562 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2563 return err;
2566 static const struct got_error *
2567 cmd_commit(int argc, char *argv[])
2569 const struct got_error *error = NULL;
2570 struct got_worktree *worktree = NULL;
2571 struct got_repository *repo = NULL;
2572 char *cwd = NULL, *path = NULL, *id_str = NULL;
2573 struct got_object_id *id = NULL;
2574 const char *logmsg = NULL;
2575 const char *got_author = getenv("GOT_AUTHOR");
2576 struct collect_commit_logmsg_arg cl_arg;
2577 char *editor = NULL;
2578 int ch;
2580 while ((ch = getopt(argc, argv, "m:")) != -1) {
2581 switch (ch) {
2582 case 'm':
2583 logmsg = optarg;
2584 break;
2585 default:
2586 usage_commit();
2587 /* NOTREACHED */
2591 argc -= optind;
2592 argv += optind;
2594 if (argc == 1) {
2595 path = realpath(argv[0], NULL);
2596 if (path == NULL) {
2597 error = got_error_from_errno2("realpath", argv[0]);
2598 goto done;
2600 got_path_strip_trailing_slashes(path);
2601 } else if (argc != 0)
2602 usage_commit();
2604 if (got_author == NULL) {
2605 /* TODO: Look current user up in password database */
2606 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2607 goto done;
2610 cwd = getcwd(NULL, 0);
2611 if (cwd == NULL) {
2612 error = got_error_from_errno("getcwd");
2613 goto done;
2615 error = got_worktree_open(&worktree, cwd);
2616 if (error)
2617 goto done;
2619 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2620 if (error != NULL)
2621 goto done;
2624 * unveil(2) traverses exec(2); if an editor is used we have
2625 * to apply unveil after the log message has been written.
2627 if (logmsg == NULL || strlen(logmsg) == 0)
2628 error = get_editor(&editor);
2629 else
2630 error = apply_unveil(got_repo_get_path(repo), 0,
2631 got_worktree_get_root_path(worktree), 0);
2632 if (error)
2633 goto done;
2635 cl_arg.editor = editor;
2636 cl_arg.cmdline_log = logmsg;
2637 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2638 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2639 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2640 cl_arg.branch_name += 5;
2641 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2642 cl_arg.branch_name += 6;
2643 cl_arg.repo_path = got_repo_get_path(repo);
2644 cl_arg.logmsg_path = NULL;
2645 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2646 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2647 if (error) {
2648 if (cl_arg.logmsg_path)
2649 fprintf(stderr, "%s: log message preserved in %s\n",
2650 getprogname(), cl_arg.logmsg_path);
2651 goto done;
2654 if (cl_arg.logmsg_path)
2655 unlink(cl_arg.logmsg_path);
2657 error = got_object_id_str(&id_str, id);
2658 if (error)
2659 goto done;
2660 printf("Created commit %s\n", id_str);
2661 done:
2662 if (repo)
2663 got_repo_close(repo);
2664 if (worktree)
2665 got_worktree_close(worktree);
2666 free(path);
2667 free(cwd);
2668 free(id_str);
2669 free(editor);
2670 return error;
2673 __dead static void
2674 usage_cherrypick(void)
2676 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2677 exit(1);
2680 static const struct got_error *
2681 cmd_cherrypick(int argc, char *argv[])
2683 const struct got_error *error = NULL;
2684 struct got_worktree *worktree = NULL;
2685 struct got_repository *repo = NULL;
2686 char *cwd = NULL, *commit_id_str = NULL;
2687 struct got_object_id *commit_id = NULL;
2688 struct got_commit_object *commit = NULL;
2689 struct got_object_qid *pid;
2690 struct got_reference *head_ref = NULL;
2691 int ch, did_something = 0;
2693 while ((ch = getopt(argc, argv, "")) != -1) {
2694 switch (ch) {
2695 default:
2696 usage_cherrypick();
2697 /* NOTREACHED */
2701 argc -= optind;
2702 argv += optind;
2704 if (argc != 1)
2705 usage_cherrypick();
2707 cwd = getcwd(NULL, 0);
2708 if (cwd == NULL) {
2709 error = got_error_from_errno("getcwd");
2710 goto done;
2712 error = got_worktree_open(&worktree, cwd);
2713 if (error)
2714 goto done;
2716 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2717 if (error != NULL)
2718 goto done;
2720 error = apply_unveil(got_repo_get_path(repo), 0,
2721 got_worktree_get_root_path(worktree), 0);
2722 if (error)
2723 goto done;
2725 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2726 if (error != NULL) {
2727 struct got_reference *ref;
2728 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2729 goto done;
2730 error = got_ref_open(&ref, repo, argv[0], 0);
2731 if (error != NULL)
2732 goto done;
2733 error = got_ref_resolve(&commit_id, repo, ref);
2734 got_ref_close(ref);
2735 if (error != NULL)
2736 goto done;
2738 error = got_object_id_str(&commit_id_str, commit_id);
2739 if (error)
2740 goto done;
2742 error = got_ref_open(&head_ref, repo,
2743 got_worktree_get_head_ref_name(worktree), 0);
2744 if (error != NULL)
2745 goto done;
2747 error = check_same_branch(commit_id, head_ref, repo);
2748 if (error) {
2749 if (error->code != GOT_ERR_ANCESTRY)
2750 goto done;
2751 error = NULL;
2752 } else {
2753 error = got_error(GOT_ERR_SAME_BRANCH);
2754 goto done;
2757 error = got_object_open_as_commit(&commit, repo, commit_id);
2758 if (error)
2759 goto done;
2760 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2761 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
2762 commit_id, repo, update_progress, &did_something, check_cancelled,
2763 NULL);
2764 if (error != NULL)
2765 goto done;
2767 if (did_something)
2768 printf("Merged commit %s\n", commit_id_str);
2769 done:
2770 if (commit)
2771 got_object_commit_close(commit);
2772 free(commit_id_str);
2773 if (head_ref)
2774 got_ref_close(head_ref);
2775 if (worktree)
2776 got_worktree_close(worktree);
2777 if (repo)
2778 got_repo_close(repo);
2779 return error;
2782 __dead static void
2783 usage_backout(void)
2785 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
2786 exit(1);
2789 static const struct got_error *
2790 cmd_backout(int argc, char *argv[])
2792 const struct got_error *error = NULL;
2793 struct got_worktree *worktree = NULL;
2794 struct got_repository *repo = NULL;
2795 char *cwd = NULL, *commit_id_str = NULL;
2796 struct got_object_id *commit_id = NULL;
2797 struct got_commit_object *commit = NULL;
2798 struct got_object_qid *pid;
2799 struct got_reference *head_ref = NULL;
2800 int ch, did_something = 0;
2802 while ((ch = getopt(argc, argv, "")) != -1) {
2803 switch (ch) {
2804 default:
2805 usage_backout();
2806 /* NOTREACHED */
2810 argc -= optind;
2811 argv += optind;
2813 if (argc != 1)
2814 usage_backout();
2816 cwd = getcwd(NULL, 0);
2817 if (cwd == NULL) {
2818 error = got_error_from_errno("getcwd");
2819 goto done;
2821 error = got_worktree_open(&worktree, cwd);
2822 if (error)
2823 goto done;
2825 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2826 if (error != NULL)
2827 goto done;
2829 error = apply_unveil(got_repo_get_path(repo), 0,
2830 got_worktree_get_root_path(worktree), 0);
2831 if (error)
2832 goto done;
2834 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2835 if (error != NULL) {
2836 struct got_reference *ref;
2837 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2838 goto done;
2839 error = got_ref_open(&ref, repo, argv[0], 0);
2840 if (error != NULL)
2841 goto done;
2842 error = got_ref_resolve(&commit_id, repo, ref);
2843 got_ref_close(ref);
2844 if (error != NULL)
2845 goto done;
2847 error = got_object_id_str(&commit_id_str, commit_id);
2848 if (error)
2849 goto done;
2851 error = got_ref_open(&head_ref, repo,
2852 got_worktree_get_head_ref_name(worktree), 0);
2853 if (error != NULL)
2854 goto done;
2856 error = check_same_branch(commit_id, head_ref, repo);
2857 if (error)
2858 goto done;
2860 error = got_object_open_as_commit(&commit, repo, commit_id);
2861 if (error)
2862 goto done;
2863 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2864 if (pid == NULL) {
2865 error = got_error(GOT_ERR_ROOT_COMMIT);
2866 goto done;
2869 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
2870 update_progress, &did_something, check_cancelled, NULL);
2871 if (error != NULL)
2872 goto done;
2874 if (did_something)
2875 printf("Backed out commit %s\n", commit_id_str);
2876 done:
2877 if (commit)
2878 got_object_commit_close(commit);
2879 free(commit_id_str);
2880 if (head_ref)
2881 got_ref_close(head_ref);
2882 if (worktree)
2883 got_worktree_close(worktree);
2884 if (repo)
2885 got_repo_close(repo);
2886 return error;