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 } else
1338 usage_diff();
1340 if (repo_path == NULL) {
1341 repo_path = getcwd(NULL, 0);
1342 if (repo_path == NULL)
1343 return got_error_from_errno("getcwd");
1346 error = got_repo_open(&repo, repo_path);
1347 free(repo_path);
1348 if (error != NULL)
1349 goto done;
1351 error = apply_unveil(got_repo_get_path(repo), 1,
1352 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1353 if (error)
1354 goto done;
1356 if (worktree) {
1357 struct print_diff_arg arg;
1358 char *id_str;
1359 error = got_object_id_str(&id_str,
1360 got_worktree_get_base_commit_id(worktree));
1361 if (error)
1362 goto done;
1363 arg.repo = repo;
1364 arg.worktree = worktree;
1365 arg.diff_context = diff_context;
1366 arg.id_str = id_str;
1367 arg.header_shown = 0;
1369 error = got_worktree_status(worktree, path, repo, print_diff,
1370 &arg, check_cancelled, NULL);
1371 free(id_str);
1372 goto done;
1375 error = got_object_resolve_id_str(&id1, repo, id_str1);
1376 if (error) {
1377 struct got_reference *ref;
1378 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1379 goto done;
1380 error = got_ref_open(&ref, repo, id_str1, 0);
1381 if (error != NULL)
1382 goto done;
1383 label1 = strdup(got_ref_get_name(ref));
1384 if (label1 == NULL) {
1385 error = got_error_from_errno("strdup");
1386 goto done;
1388 error = got_ref_resolve(&id1, repo, ref);
1389 got_ref_close(ref);
1390 if (error != NULL)
1391 goto done;
1392 } else {
1393 label1 = strdup(id_str1);
1394 if (label1 == NULL) {
1395 error = got_error_from_errno("strdup");
1396 goto done;
1400 error = got_object_resolve_id_str(&id2, repo, id_str2);
1401 if (error) {
1402 struct got_reference *ref;
1403 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1404 goto done;
1405 error = got_ref_open(&ref, repo, id_str2, 0);
1406 if (error != NULL)
1407 goto done;
1408 label2 = strdup(got_ref_get_name(ref));
1409 if (label2 == NULL) {
1410 error = got_error_from_errno("strdup");
1411 goto done;
1413 error = got_ref_resolve(&id2, repo, ref);
1414 got_ref_close(ref);
1415 if (error != NULL)
1416 goto done;
1417 } else {
1418 label2 = strdup(id_str2);
1419 if (label2 == NULL) {
1420 error = got_error_from_errno("strdup");
1421 goto done;
1425 error = got_object_get_type(&type1, repo, id1);
1426 if (error)
1427 goto done;
1429 error = got_object_get_type(&type2, repo, id2);
1430 if (error)
1431 goto done;
1433 if (type1 != type2) {
1434 error = got_error(GOT_ERR_OBJ_TYPE);
1435 goto done;
1438 switch (type1) {
1439 case GOT_OBJ_TYPE_BLOB:
1440 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1441 diff_context, repo, stdout);
1442 break;
1443 case GOT_OBJ_TYPE_TREE:
1444 error = got_diff_objects_as_trees(id1, id2, "", "",
1445 diff_context, repo, stdout);
1446 break;
1447 case GOT_OBJ_TYPE_COMMIT:
1448 printf("diff %s %s\n", label1, label2);
1449 error = got_diff_objects_as_commits(id1, id2, diff_context,
1450 repo, stdout);
1451 break;
1452 default:
1453 error = got_error(GOT_ERR_OBJ_TYPE);
1456 done:
1457 free(label1);
1458 free(label2);
1459 free(id1);
1460 free(id2);
1461 free(path);
1462 if (worktree)
1463 got_worktree_close(worktree);
1464 if (repo) {
1465 const struct got_error *repo_error;
1466 repo_error = got_repo_close(repo);
1467 if (error == NULL)
1468 error = repo_error;
1470 return error;
1473 __dead static void
1474 usage_blame(void)
1476 fprintf(stderr,
1477 "usage: %s blame [-c commit] [-r repository-path] path\n",
1478 getprogname());
1479 exit(1);
1482 static const struct got_error *
1483 cmd_blame(int argc, char *argv[])
1485 const struct got_error *error;
1486 struct got_repository *repo = NULL;
1487 struct got_worktree *worktree = NULL;
1488 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1489 struct got_object_id *commit_id = NULL;
1490 char *commit_id_str = NULL;
1491 int ch;
1493 #ifndef PROFILE
1494 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1495 NULL) == -1)
1496 err(1, "pledge");
1497 #endif
1499 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1500 switch (ch) {
1501 case 'c':
1502 commit_id_str = optarg;
1503 break;
1504 case 'r':
1505 repo_path = realpath(optarg, NULL);
1506 if (repo_path == NULL)
1507 err(1, "-r option");
1508 got_path_strip_trailing_slashes(repo_path);
1509 break;
1510 default:
1511 usage_blame();
1512 /* NOTREACHED */
1516 argc -= optind;
1517 argv += optind;
1519 if (argc == 1)
1520 path = argv[0];
1521 else
1522 usage_blame();
1524 cwd = getcwd(NULL, 0);
1525 if (cwd == NULL) {
1526 error = got_error_from_errno("getcwd");
1527 goto done;
1529 if (repo_path == NULL) {
1530 error = got_worktree_open(&worktree, cwd);
1531 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1532 goto done;
1533 else
1534 error = NULL;
1535 if (worktree) {
1536 repo_path =
1537 strdup(got_worktree_get_repo_path(worktree));
1538 if (repo_path == NULL)
1539 error = got_error_from_errno("strdup");
1540 if (error)
1541 goto done;
1542 } else {
1543 repo_path = strdup(cwd);
1544 if (repo_path == NULL) {
1545 error = got_error_from_errno("strdup");
1546 goto done;
1551 error = got_repo_open(&repo, repo_path);
1552 if (error != NULL)
1553 goto done;
1555 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1556 if (error)
1557 goto done;
1559 if (worktree) {
1560 const char *prefix = got_worktree_get_path_prefix(worktree);
1561 char *p, *worktree_subdir = cwd +
1562 strlen(got_worktree_get_root_path(worktree));
1563 if (asprintf(&p, "%s%s%s%s%s",
1564 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1565 worktree_subdir, worktree_subdir[0] ? "/" : "",
1566 path) == -1) {
1567 error = got_error_from_errno("asprintf");
1568 goto done;
1570 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1571 free(p);
1572 } else {
1573 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1575 if (error)
1576 goto done;
1578 if (commit_id_str == NULL) {
1579 struct got_reference *head_ref;
1580 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1581 if (error != NULL)
1582 goto done;
1583 error = got_ref_resolve(&commit_id, repo, head_ref);
1584 got_ref_close(head_ref);
1585 if (error != NULL)
1586 goto done;
1587 } else {
1588 error = got_object_resolve_id_str(&commit_id, repo,
1589 commit_id_str);
1590 if (error != NULL)
1591 goto done;
1594 error = got_blame(in_repo_path, commit_id, repo, stdout);
1595 done:
1596 free(in_repo_path);
1597 free(repo_path);
1598 free(cwd);
1599 free(commit_id);
1600 if (worktree)
1601 got_worktree_close(worktree);
1602 if (repo) {
1603 const struct got_error *repo_error;
1604 repo_error = got_repo_close(repo);
1605 if (error == NULL)
1606 error = repo_error;
1608 return error;
1611 __dead static void
1612 usage_tree(void)
1614 fprintf(stderr,
1615 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1616 getprogname());
1617 exit(1);
1620 static void
1621 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1622 const char *root_path)
1624 int is_root_path = (strcmp(path, root_path) == 0);
1626 path += strlen(root_path);
1627 while (path[0] == '/')
1628 path++;
1630 printf("%s%s%s%s%s\n", id ? id : "", path,
1631 is_root_path ? "" : "/", te->name,
1632 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1635 static const struct got_error *
1636 print_tree(const char *path, struct got_object_id *commit_id,
1637 int show_ids, int recurse, const char *root_path,
1638 struct got_repository *repo)
1640 const struct got_error *err = NULL;
1641 struct got_object_id *tree_id = NULL;
1642 struct got_tree_object *tree = NULL;
1643 const struct got_tree_entries *entries;
1644 struct got_tree_entry *te;
1646 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1647 if (err)
1648 goto done;
1650 err = got_object_open_as_tree(&tree, repo, tree_id);
1651 if (err)
1652 goto done;
1653 entries = got_object_tree_get_entries(tree);
1654 te = SIMPLEQ_FIRST(&entries->head);
1655 while (te) {
1656 char *id = NULL;
1658 if (sigint_received || sigpipe_received)
1659 break;
1661 if (show_ids) {
1662 char *id_str;
1663 err = got_object_id_str(&id_str, te->id);
1664 if (err)
1665 goto done;
1666 if (asprintf(&id, "%s ", id_str) == -1) {
1667 err = got_error_from_errno("asprintf");
1668 free(id_str);
1669 goto done;
1671 free(id_str);
1673 print_entry(te, id, path, root_path);
1674 free(id);
1676 if (recurse && S_ISDIR(te->mode)) {
1677 char *child_path;
1678 if (asprintf(&child_path, "%s%s%s", path,
1679 path[0] == '/' && path[1] == '\0' ? "" : "/",
1680 te->name) == -1) {
1681 err = got_error_from_errno("asprintf");
1682 goto done;
1684 err = print_tree(child_path, commit_id, show_ids, 1,
1685 root_path, repo);
1686 free(child_path);
1687 if (err)
1688 goto done;
1691 te = SIMPLEQ_NEXT(te, entry);
1693 done:
1694 if (tree)
1695 got_object_tree_close(tree);
1696 free(tree_id);
1697 return err;
1700 static const struct got_error *
1701 cmd_tree(int argc, char *argv[])
1703 const struct got_error *error;
1704 struct got_repository *repo = NULL;
1705 struct got_worktree *worktree = NULL;
1706 const char *path;
1707 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1708 struct got_object_id *commit_id = NULL;
1709 char *commit_id_str = NULL;
1710 int show_ids = 0, recurse = 0;
1711 int ch;
1713 #ifndef PROFILE
1714 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1715 NULL) == -1)
1716 err(1, "pledge");
1717 #endif
1719 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1720 switch (ch) {
1721 case 'c':
1722 commit_id_str = optarg;
1723 break;
1724 case 'r':
1725 repo_path = realpath(optarg, NULL);
1726 if (repo_path == NULL)
1727 err(1, "-r option");
1728 got_path_strip_trailing_slashes(repo_path);
1729 break;
1730 case 'i':
1731 show_ids = 1;
1732 break;
1733 case 'R':
1734 recurse = 1;
1735 break;
1736 default:
1737 usage_tree();
1738 /* NOTREACHED */
1742 argc -= optind;
1743 argv += optind;
1745 if (argc == 1)
1746 path = argv[0];
1747 else if (argc > 1)
1748 usage_tree();
1749 else
1750 path = NULL;
1752 cwd = getcwd(NULL, 0);
1753 if (cwd == NULL) {
1754 error = got_error_from_errno("getcwd");
1755 goto done;
1757 if (repo_path == NULL) {
1758 error = got_worktree_open(&worktree, cwd);
1759 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1760 goto done;
1761 else
1762 error = NULL;
1763 if (worktree) {
1764 repo_path =
1765 strdup(got_worktree_get_repo_path(worktree));
1766 if (repo_path == NULL)
1767 error = got_error_from_errno("strdup");
1768 if (error)
1769 goto done;
1770 } else {
1771 repo_path = strdup(cwd);
1772 if (repo_path == NULL) {
1773 error = got_error_from_errno("strdup");
1774 goto done;
1779 error = got_repo_open(&repo, repo_path);
1780 if (error != NULL)
1781 goto done;
1783 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1784 if (error)
1785 goto done;
1787 if (path == NULL) {
1788 if (worktree) {
1789 char *p, *worktree_subdir = cwd +
1790 strlen(got_worktree_get_root_path(worktree));
1791 if (asprintf(&p, "%s/%s",
1792 got_worktree_get_path_prefix(worktree),
1793 worktree_subdir) == -1) {
1794 error = got_error_from_errno("asprintf");
1795 goto done;
1797 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1798 free(p);
1799 if (error)
1800 goto done;
1801 } else
1802 path = "/";
1804 if (in_repo_path == NULL) {
1805 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1806 if (error != NULL)
1807 goto done;
1810 if (commit_id_str == NULL) {
1811 struct got_reference *head_ref;
1812 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1813 if (error != NULL)
1814 goto done;
1815 error = got_ref_resolve(&commit_id, repo, head_ref);
1816 got_ref_close(head_ref);
1817 if (error != NULL)
1818 goto done;
1819 } else {
1820 error = got_object_resolve_id_str(&commit_id, repo,
1821 commit_id_str);
1822 if (error != NULL)
1823 goto done;
1826 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1827 in_repo_path, repo);
1828 done:
1829 free(in_repo_path);
1830 free(repo_path);
1831 free(cwd);
1832 free(commit_id);
1833 if (worktree)
1834 got_worktree_close(worktree);
1835 if (repo) {
1836 const struct got_error *repo_error;
1837 repo_error = got_repo_close(repo);
1838 if (error == NULL)
1839 error = repo_error;
1841 return error;
1844 __dead static void
1845 usage_status(void)
1847 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1848 exit(1);
1851 static const struct got_error *
1852 print_status(void *arg, unsigned char status, const char *path,
1853 struct got_object_id *blob_id, struct got_object_id *commit_id)
1855 printf("%c %s\n", status, path);
1856 return NULL;
1859 static const struct got_error *
1860 cmd_status(int argc, char *argv[])
1862 const struct got_error *error = NULL;
1863 struct got_repository *repo = NULL;
1864 struct got_worktree *worktree = NULL;
1865 char *cwd = NULL, *path = NULL;
1866 int ch;
1868 while ((ch = getopt(argc, argv, "")) != -1) {
1869 switch (ch) {
1870 default:
1871 usage_status();
1872 /* NOTREACHED */
1876 argc -= optind;
1877 argv += optind;
1879 #ifndef PROFILE
1880 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1881 NULL) == -1)
1882 err(1, "pledge");
1883 #endif
1884 cwd = getcwd(NULL, 0);
1885 if (cwd == NULL) {
1886 error = got_error_from_errno("getcwd");
1887 goto done;
1890 error = got_worktree_open(&worktree, cwd);
1891 if (error != NULL)
1892 goto done;
1894 if (argc == 0) {
1895 path = strdup("");
1896 if (path == NULL) {
1897 error = got_error_from_errno("strdup");
1898 goto done;
1900 } else if (argc == 1) {
1901 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1902 if (error)
1903 goto done;
1904 } else
1905 usage_status();
1907 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1908 if (error != NULL)
1909 goto done;
1911 error = apply_unveil(got_repo_get_path(repo), 1,
1912 got_worktree_get_root_path(worktree), 0);
1913 if (error)
1914 goto done;
1916 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1917 check_cancelled, NULL);
1918 done:
1919 free(cwd);
1920 free(path);
1921 return error;
1924 __dead static void
1925 usage_ref(void)
1927 fprintf(stderr,
1928 "usage: %s ref [-r repository] -l | -d name | name target\n",
1929 getprogname());
1930 exit(1);
1933 static const struct got_error *
1934 list_refs(struct got_repository *repo)
1936 static const struct got_error *err = NULL;
1937 struct got_reflist_head refs;
1938 struct got_reflist_entry *re;
1940 SIMPLEQ_INIT(&refs);
1941 err = got_ref_list(&refs, repo);
1942 if (err)
1943 return err;
1945 SIMPLEQ_FOREACH(re, &refs, entry) {
1946 char *refstr;
1947 refstr = got_ref_to_str(re->ref);
1948 if (refstr == NULL)
1949 return got_error_from_errno("got_ref_to_str");
1950 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1951 free(refstr);
1954 got_ref_list_free(&refs);
1955 return NULL;
1958 static const struct got_error *
1959 delete_ref(struct got_repository *repo, const char *refname)
1961 const struct got_error *err = NULL;
1962 struct got_reference *ref;
1964 err = got_ref_open(&ref, repo, refname, 0);
1965 if (err)
1966 return err;
1968 err = got_ref_delete(ref, repo);
1969 got_ref_close(ref);
1970 return err;
1973 static const struct got_error *
1974 add_ref(struct got_repository *repo, const char *refname, const char *target)
1976 const struct got_error *err = NULL;
1977 struct got_object_id *id;
1978 struct got_reference *ref = NULL;
1980 err = got_object_resolve_id_str(&id, repo, target);
1981 if (err) {
1982 struct got_reference *target_ref;
1984 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1985 return err;
1986 err = got_ref_open(&target_ref, repo, target, 0);
1987 if (err)
1988 return err;
1989 err = got_ref_resolve(&id, repo, target_ref);
1990 got_ref_close(target_ref);
1991 if (err)
1992 return err;
1995 err = got_ref_alloc(&ref, refname, id);
1996 if (err)
1997 goto done;
1999 err = got_ref_write(ref, repo);
2000 done:
2001 if (ref)
2002 got_ref_close(ref);
2003 free(id);
2004 return err;
2007 static const struct got_error *
2008 cmd_ref(int argc, char *argv[])
2010 const struct got_error *error = NULL;
2011 struct got_repository *repo = NULL;
2012 struct got_worktree *worktree = NULL;
2013 char *cwd = NULL, *repo_path = NULL;
2014 int ch, do_list = 0;
2015 const char *delref = NULL;
2017 /* TODO: Add -s option for adding symbolic references. */
2018 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2019 switch (ch) {
2020 case 'd':
2021 delref = optarg;
2022 break;
2023 case 'r':
2024 repo_path = realpath(optarg, NULL);
2025 if (repo_path == NULL)
2026 err(1, "-r option");
2027 got_path_strip_trailing_slashes(repo_path);
2028 break;
2029 case 'l':
2030 do_list = 1;
2031 break;
2032 default:
2033 usage_ref();
2034 /* NOTREACHED */
2038 if (do_list && delref)
2039 errx(1, "-l and -d options are mutually exclusive\n");
2041 argc -= optind;
2042 argv += optind;
2044 if (do_list || delref) {
2045 if (argc > 0)
2046 usage_ref();
2047 } else if (argc != 2)
2048 usage_ref();
2050 #ifndef PROFILE
2051 if (do_list) {
2052 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2053 NULL) == -1)
2054 err(1, "pledge");
2055 } else {
2056 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2057 "sendfd unveil", NULL) == -1)
2058 err(1, "pledge");
2060 #endif
2061 cwd = getcwd(NULL, 0);
2062 if (cwd == NULL) {
2063 error = got_error_from_errno("getcwd");
2064 goto done;
2067 if (repo_path == NULL) {
2068 error = got_worktree_open(&worktree, cwd);
2069 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2070 goto done;
2071 else
2072 error = NULL;
2073 if (worktree) {
2074 repo_path =
2075 strdup(got_worktree_get_repo_path(worktree));
2076 if (repo_path == NULL)
2077 error = got_error_from_errno("strdup");
2078 if (error)
2079 goto done;
2080 } else {
2081 repo_path = strdup(cwd);
2082 if (repo_path == NULL) {
2083 error = got_error_from_errno("strdup");
2084 goto done;
2089 error = got_repo_open(&repo, repo_path);
2090 if (error != NULL)
2091 goto done;
2093 error = apply_unveil(got_repo_get_path(repo), do_list,
2094 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2095 if (error)
2096 goto done;
2098 if (do_list)
2099 error = list_refs(repo);
2100 else if (delref)
2101 error = delete_ref(repo, delref);
2102 else
2103 error = add_ref(repo, argv[0], argv[1]);
2104 done:
2105 if (repo)
2106 got_repo_close(repo);
2107 if (worktree)
2108 got_worktree_close(worktree);
2109 free(cwd);
2110 free(repo_path);
2111 return error;
2114 __dead static void
2115 usage_add(void)
2117 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2118 exit(1);
2121 static const struct got_error *
2122 cmd_add(int argc, char *argv[])
2124 const struct got_error *error = NULL;
2125 struct got_repository *repo = NULL;
2126 struct got_worktree *worktree = NULL;
2127 char *cwd = NULL;
2128 struct got_pathlist_head paths;
2129 struct got_pathlist_entry *pe;
2130 int ch, x;
2132 TAILQ_INIT(&paths);
2134 while ((ch = getopt(argc, argv, "")) != -1) {
2135 switch (ch) {
2136 default:
2137 usage_add();
2138 /* NOTREACHED */
2142 argc -= optind;
2143 argv += optind;
2145 if (argc < 1)
2146 usage_add();
2148 /* make sure each file exists before doing anything halfway */
2149 for (x = 0; x < argc; x++) {
2150 char *path = realpath(argv[x], NULL);
2151 if (path == NULL) {
2152 error = got_error_from_errno2("realpath", argv[x]);
2153 goto done;
2155 free(path);
2158 cwd = getcwd(NULL, 0);
2159 if (cwd == NULL) {
2160 error = got_error_from_errno("getcwd");
2161 goto done;
2164 error = got_worktree_open(&worktree, cwd);
2165 if (error)
2166 goto done;
2168 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2169 if (error != NULL)
2170 goto done;
2172 error = apply_unveil(got_repo_get_path(repo), 1,
2173 got_worktree_get_root_path(worktree), 0);
2174 if (error)
2175 goto done;
2177 for (x = 0; x < argc; x++) {
2178 char *path = realpath(argv[x], NULL);
2179 if (path == NULL) {
2180 error = got_error_from_errno2("realpath", argv[x]);
2181 goto done;
2184 got_path_strip_trailing_slashes(path);
2185 error = got_pathlist_insert(&pe, &paths, path, NULL);
2186 if (error) {
2187 free(path);
2188 goto done;
2191 error = got_worktree_schedule_add(worktree, &paths, print_status,
2192 NULL, repo);
2193 done:
2194 if (repo)
2195 got_repo_close(repo);
2196 if (worktree)
2197 got_worktree_close(worktree);
2198 TAILQ_FOREACH(pe, &paths, entry)
2199 free((char *)pe->path);
2200 got_pathlist_free(&paths);
2201 free(cwd);
2202 return error;
2205 __dead static void
2206 usage_rm(void)
2208 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2209 exit(1);
2212 static const struct got_error *
2213 cmd_rm(int argc, char *argv[])
2215 const struct got_error *error = NULL;
2216 struct got_worktree *worktree = NULL;
2217 struct got_repository *repo = NULL;
2218 char *cwd = NULL;
2219 struct got_pathlist_head paths;
2220 struct got_pathlist_entry *pe;
2221 int ch, i, delete_local_mods = 0;
2223 TAILQ_INIT(&paths);
2225 while ((ch = getopt(argc, argv, "f")) != -1) {
2226 switch (ch) {
2227 case 'f':
2228 delete_local_mods = 1;
2229 break;
2230 default:
2231 usage_add();
2232 /* NOTREACHED */
2236 argc -= optind;
2237 argv += optind;
2239 if (argc < 1)
2240 usage_rm();
2242 /* make sure each file exists before doing anything halfway */
2243 for (i = 0; i < argc; i++) {
2244 char *path = realpath(argv[i], NULL);
2245 if (path == NULL) {
2246 error = got_error_from_errno2("realpath", argv[i]);
2247 goto done;
2249 free(path);
2252 cwd = getcwd(NULL, 0);
2253 if (cwd == NULL) {
2254 error = got_error_from_errno("getcwd");
2255 goto done;
2257 error = got_worktree_open(&worktree, cwd);
2258 if (error)
2259 goto done;
2261 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2262 if (error)
2263 goto done;
2265 error = apply_unveil(got_repo_get_path(repo), 1,
2266 got_worktree_get_root_path(worktree), 0);
2267 if (error)
2268 goto done;
2270 for (i = 0; i < argc; i++) {
2271 char *path = realpath(argv[i], NULL);
2272 if (path == NULL) {
2273 error = got_error_from_errno2("realpath", argv[i]);
2274 goto done;
2277 got_path_strip_trailing_slashes(path);
2278 error = got_pathlist_insert(&pe, &paths, path, NULL);
2279 if (error) {
2280 free(path);
2281 goto done;
2284 error = got_worktree_schedule_delete(worktree, &paths,
2285 delete_local_mods, print_status, NULL, repo);
2286 if (error)
2287 goto done;
2288 done:
2289 if (repo)
2290 got_repo_close(repo);
2291 if (worktree)
2292 got_worktree_close(worktree);
2293 TAILQ_FOREACH(pe, &paths, entry)
2294 free((char *)pe->path);
2295 got_pathlist_free(&paths);
2296 free(cwd);
2297 return error;
2300 __dead static void
2301 usage_revert(void)
2303 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2304 exit(1);
2307 static void
2308 revert_progress(void *arg, unsigned char status, const char *path)
2310 while (path[0] == '/')
2311 path++;
2312 printf("%c %s\n", status, path);
2315 static const struct got_error *
2316 cmd_revert(int argc, char *argv[])
2318 const struct got_error *error = NULL;
2319 struct got_worktree *worktree = NULL;
2320 struct got_repository *repo = NULL;
2321 char *cwd = NULL, *path = NULL;
2322 struct got_pathlist_head paths;
2323 struct got_pathlist_entry *pe;
2324 int ch, i;
2326 TAILQ_INIT(&paths);
2328 while ((ch = getopt(argc, argv, "")) != -1) {
2329 switch (ch) {
2330 default:
2331 usage_revert();
2332 /* NOTREACHED */
2336 argc -= optind;
2337 argv += optind;
2339 if (argc < 1)
2340 usage_revert();
2342 for (i = 0; i < argc; i++) {
2343 char *path = realpath(argv[i], NULL);
2344 if (path == NULL) {
2345 error = got_error_from_errno2("realpath", argv[i]);
2346 goto done;
2349 got_path_strip_trailing_slashes(path);
2350 error = got_pathlist_insert(&pe, &paths, path, NULL);
2351 if (error) {
2352 free(path);
2353 goto done;
2357 cwd = getcwd(NULL, 0);
2358 if (cwd == NULL) {
2359 error = got_error_from_errno("getcwd");
2360 goto done;
2362 error = got_worktree_open(&worktree, cwd);
2363 if (error)
2364 goto done;
2366 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2367 if (error != NULL)
2368 goto done;
2370 error = apply_unveil(got_repo_get_path(repo), 1,
2371 got_worktree_get_root_path(worktree), 0);
2372 if (error)
2373 goto done;
2375 error = got_worktree_revert(worktree, &paths,
2376 revert_progress, NULL, repo);
2377 if (error)
2378 goto done;
2379 done:
2380 if (repo)
2381 got_repo_close(repo);
2382 if (worktree)
2383 got_worktree_close(worktree);
2384 free(path);
2385 free(cwd);
2386 return error;
2389 __dead static void
2390 usage_commit(void)
2392 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2393 exit(1);
2396 int
2397 spawn_editor(const char *editor, const char *file)
2399 pid_t pid;
2400 sig_t sighup, sigint, sigquit;
2401 int st = -1;
2403 sighup = signal(SIGHUP, SIG_IGN);
2404 sigint = signal(SIGINT, SIG_IGN);
2405 sigquit = signal(SIGQUIT, SIG_IGN);
2407 switch (pid = fork()) {
2408 case -1:
2409 goto doneediting;
2410 case 0:
2411 execl(editor, editor, file, (char *)NULL);
2412 _exit(127);
2415 while (waitpid(pid, &st, 0) == -1)
2416 if (errno != EINTR)
2417 break;
2419 doneediting:
2420 (void)signal(SIGHUP, sighup);
2421 (void)signal(SIGINT, sigint);
2422 (void)signal(SIGQUIT, sigquit);
2424 if (!WIFEXITED(st)) {
2425 errno = EINTR;
2426 return -1;
2429 return WEXITSTATUS(st);
2432 struct collect_commit_logmsg_arg {
2433 const char *cmdline_log;
2434 const char *editor;
2435 const char *worktree_path;
2436 const char *branch_name;
2437 const char *repo_path;
2438 char *logmsg_path;
2442 static const struct got_error *
2443 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2444 void *arg)
2446 char *initial_content = NULL;
2447 struct got_pathlist_entry *pe;
2448 const struct got_error *err = NULL;
2449 char *template = NULL;
2450 struct collect_commit_logmsg_arg *a = arg;
2451 char buf[1024];
2452 struct stat st, st2;
2453 FILE *fp;
2454 size_t len;
2455 int fd, content_changed = 0;
2457 /* if a message was specified on the command line, just use it */
2458 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2459 len = strlen(a->cmdline_log) + 1;
2460 *logmsg = malloc(len + 1);
2461 if (*logmsg == NULL)
2462 return got_error_from_errno("malloc");
2463 strlcpy(*logmsg, a->cmdline_log, len);
2464 return NULL;
2467 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2468 return got_error_from_errno("asprintf");
2470 if (asprintf(&initial_content,
2471 "\n# changes to be committed on branch %s:\n",
2472 a->branch_name) == -1)
2473 return got_error_from_errno("asprintf");
2475 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2476 if (err)
2477 goto done;
2479 dprintf(fd, initial_content);
2481 TAILQ_FOREACH(pe, commitable_paths, entry) {
2482 struct got_commitable *ct = pe->data;
2483 dprintf(fd, "# %c %s\n",
2484 got_commitable_get_status(ct),
2485 got_commitable_get_path(ct));
2487 close(fd);
2489 if (stat(a->logmsg_path, &st) == -1) {
2490 err = got_error_from_errno2("stat", a->logmsg_path);
2491 goto done;
2494 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2495 err = got_error_from_errno("failed spawning editor");
2496 goto done;
2499 if (stat(a->logmsg_path, &st2) == -1) {
2500 err = got_error_from_errno("stat");
2501 goto done;
2504 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2505 unlink(a->logmsg_path);
2506 free(a->logmsg_path);
2507 a->logmsg_path = NULL;
2508 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2509 "no changes made to commit message, aborting");
2510 goto done;
2513 *logmsg = malloc(st2.st_size + 1);
2514 if (*logmsg == NULL) {
2515 err = got_error_from_errno("malloc");
2516 goto done;
2518 (*logmsg)[0] = '\0';
2519 len = 0;
2521 fp = fopen(a->logmsg_path, "r");
2522 if (fp == NULL) {
2523 err = got_error_from_errno("fopen");
2524 goto done;
2526 while (fgets(buf, sizeof(buf), fp) != NULL) {
2527 if (!content_changed && strcmp(buf, initial_content) != 0)
2528 content_changed = 1;
2529 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2530 continue; /* remove comments and leading empty lines */
2531 len = strlcat(*logmsg, buf, st2.st_size);
2533 fclose(fp);
2535 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2536 (*logmsg)[len - 1] = '\0';
2537 len--;
2540 if (len == 0 || !content_changed) {
2541 unlink(a->logmsg_path);
2542 free(a->logmsg_path);
2543 a->logmsg_path = NULL;
2544 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2545 "commit message cannot be empty, aborting");
2546 goto done;
2548 done:
2549 free(initial_content);
2550 free(template);
2552 /* Editor is done; we can now apply unveil(2) */
2553 if (err == NULL)
2554 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2555 return err;
2558 static const struct got_error *
2559 cmd_commit(int argc, char *argv[])
2561 const struct got_error *error = NULL;
2562 struct got_worktree *worktree = NULL;
2563 struct got_repository *repo = NULL;
2564 char *cwd = NULL, *path = NULL, *id_str = NULL;
2565 struct got_object_id *id = NULL;
2566 const char *logmsg = NULL;
2567 const char *got_author = getenv("GOT_AUTHOR");
2568 struct collect_commit_logmsg_arg cl_arg;
2569 char *editor = NULL;
2570 int ch;
2572 while ((ch = getopt(argc, argv, "m:")) != -1) {
2573 switch (ch) {
2574 case 'm':
2575 logmsg = optarg;
2576 break;
2577 default:
2578 usage_commit();
2579 /* NOTREACHED */
2583 argc -= optind;
2584 argv += optind;
2586 if (argc == 1) {
2587 path = realpath(argv[0], NULL);
2588 if (path == NULL) {
2589 error = got_error_from_errno2("realpath", argv[0]);
2590 goto done;
2592 got_path_strip_trailing_slashes(path);
2593 } else if (argc != 0)
2594 usage_commit();
2596 if (got_author == NULL) {
2597 /* TODO: Look current user up in password database */
2598 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2599 goto done;
2602 cwd = getcwd(NULL, 0);
2603 if (cwd == NULL) {
2604 error = got_error_from_errno("getcwd");
2605 goto done;
2607 error = got_worktree_open(&worktree, cwd);
2608 if (error)
2609 goto done;
2611 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2612 if (error != NULL)
2613 goto done;
2616 * unveil(2) traverses exec(2); if an editor is used we have
2617 * to apply unveil after the log message has been written.
2619 if (logmsg == NULL || strlen(logmsg) == 0)
2620 error = get_editor(&editor);
2621 else
2622 error = apply_unveil(got_repo_get_path(repo), 0,
2623 got_worktree_get_root_path(worktree), 0);
2624 if (error)
2625 goto done;
2627 cl_arg.editor = editor;
2628 cl_arg.cmdline_log = logmsg;
2629 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2630 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2631 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2632 cl_arg.branch_name += 5;
2633 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2634 cl_arg.branch_name += 6;
2635 cl_arg.repo_path = got_repo_get_path(repo);
2636 cl_arg.logmsg_path = NULL;
2637 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2638 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2639 if (error) {
2640 if (cl_arg.logmsg_path)
2641 fprintf(stderr, "%s: log message preserved in %s\n",
2642 getprogname(), cl_arg.logmsg_path);
2643 goto done;
2646 if (cl_arg.logmsg_path)
2647 unlink(cl_arg.logmsg_path);
2649 error = got_object_id_str(&id_str, id);
2650 if (error)
2651 goto done;
2652 printf("Created commit %s\n", id_str);
2653 done:
2654 if (repo)
2655 got_repo_close(repo);
2656 if (worktree)
2657 got_worktree_close(worktree);
2658 free(path);
2659 free(cwd);
2660 free(id_str);
2661 free(editor);
2662 return error;
2665 __dead static void
2666 usage_cherrypick(void)
2668 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2669 exit(1);
2672 static const struct got_error *
2673 cmd_cherrypick(int argc, char *argv[])
2675 const struct got_error *error = NULL;
2676 struct got_worktree *worktree = NULL;
2677 struct got_repository *repo = NULL;
2678 char *cwd = NULL, *commit_id_str = NULL;
2679 struct got_object_id *commit_id = NULL;
2680 struct got_commit_object *commit = NULL;
2681 struct got_object_qid *pid;
2682 struct got_reference *head_ref = NULL;
2683 int ch, did_something = 0;
2685 while ((ch = getopt(argc, argv, "")) != -1) {
2686 switch (ch) {
2687 default:
2688 usage_cherrypick();
2689 /* NOTREACHED */
2693 argc -= optind;
2694 argv += optind;
2696 if (argc != 1)
2697 usage_cherrypick();
2699 cwd = getcwd(NULL, 0);
2700 if (cwd == NULL) {
2701 error = got_error_from_errno("getcwd");
2702 goto done;
2704 error = got_worktree_open(&worktree, cwd);
2705 if (error)
2706 goto done;
2708 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2709 if (error != NULL)
2710 goto done;
2712 error = apply_unveil(got_repo_get_path(repo), 0,
2713 got_worktree_get_root_path(worktree), 0);
2714 if (error)
2715 goto done;
2717 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2718 if (error != NULL) {
2719 struct got_reference *ref;
2720 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2721 goto done;
2722 error = got_ref_open(&ref, repo, argv[0], 0);
2723 if (error != NULL)
2724 goto done;
2725 error = got_ref_resolve(&commit_id, repo, ref);
2726 got_ref_close(ref);
2727 if (error != NULL)
2728 goto done;
2730 error = got_object_id_str(&commit_id_str, commit_id);
2731 if (error)
2732 goto done;
2734 error = got_ref_open(&head_ref, repo,
2735 got_worktree_get_head_ref_name(worktree), 0);
2736 if (error != NULL)
2737 goto done;
2739 error = check_same_branch(commit_id, head_ref, repo);
2740 if (error) {
2741 if (error->code != GOT_ERR_ANCESTRY)
2742 goto done;
2743 error = NULL;
2744 } else {
2745 error = got_error(GOT_ERR_SAME_BRANCH);
2746 goto done;
2749 error = got_object_open_as_commit(&commit, repo, commit_id);
2750 if (error)
2751 goto done;
2752 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2753 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
2754 commit_id, repo, update_progress, &did_something, check_cancelled,
2755 NULL);
2756 if (error != NULL)
2757 goto done;
2759 if (did_something)
2760 printf("Merged commit %s\n", commit_id_str);
2761 done:
2762 if (commit)
2763 got_object_commit_close(commit);
2764 free(commit_id_str);
2765 if (head_ref)
2766 got_ref_close(head_ref);
2767 if (worktree)
2768 got_worktree_close(worktree);
2769 if (repo)
2770 got_repo_close(repo);
2771 return error;
2774 __dead static void
2775 usage_backout(void)
2777 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
2778 exit(1);
2781 static const struct got_error *
2782 cmd_backout(int argc, char *argv[])
2784 const struct got_error *error = NULL;
2785 struct got_worktree *worktree = NULL;
2786 struct got_repository *repo = NULL;
2787 char *cwd = NULL, *commit_id_str = NULL;
2788 struct got_object_id *commit_id = NULL;
2789 struct got_commit_object *commit = NULL;
2790 struct got_object_qid *pid;
2791 struct got_reference *head_ref = NULL;
2792 int ch, did_something = 0;
2794 while ((ch = getopt(argc, argv, "")) != -1) {
2795 switch (ch) {
2796 default:
2797 usage_backout();
2798 /* NOTREACHED */
2802 argc -= optind;
2803 argv += optind;
2805 if (argc != 1)
2806 usage_backout();
2808 cwd = getcwd(NULL, 0);
2809 if (cwd == NULL) {
2810 error = got_error_from_errno("getcwd");
2811 goto done;
2813 error = got_worktree_open(&worktree, cwd);
2814 if (error)
2815 goto done;
2817 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2818 if (error != NULL)
2819 goto done;
2821 error = apply_unveil(got_repo_get_path(repo), 0,
2822 got_worktree_get_root_path(worktree), 0);
2823 if (error)
2824 goto done;
2826 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2827 if (error != NULL) {
2828 struct got_reference *ref;
2829 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2830 goto done;
2831 error = got_ref_open(&ref, repo, argv[0], 0);
2832 if (error != NULL)
2833 goto done;
2834 error = got_ref_resolve(&commit_id, repo, ref);
2835 got_ref_close(ref);
2836 if (error != NULL)
2837 goto done;
2839 error = got_object_id_str(&commit_id_str, commit_id);
2840 if (error)
2841 goto done;
2843 error = got_ref_open(&head_ref, repo,
2844 got_worktree_get_head_ref_name(worktree), 0);
2845 if (error != NULL)
2846 goto done;
2848 error = check_same_branch(commit_id, head_ref, repo);
2849 if (error)
2850 goto done;
2852 error = got_object_open_as_commit(&commit, repo, commit_id);
2853 if (error)
2854 goto done;
2855 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2856 if (pid == NULL) {
2857 error = got_error(GOT_ERR_ROOT_COMMIT);
2858 goto done;
2861 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
2862 update_progress, &did_something, check_cancelled, NULL);
2863 if (error != NULL)
2864 goto done;
2866 if (did_something)
2867 printf("Backed out commit %s\n", commit_id_str);
2868 done:
2869 if (commit)
2870 got_object_commit_close(commit);
2871 free(commit_id_str);
2872 if (head_ref)
2873 got_ref_close(head_ref);
2874 if (worktree)
2875 got_worktree_close(worktree);
2876 if (repo)
2877 got_repo_close(repo);
2878 return error;