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 /* Base commit bump happens silently. */
289 if (status == GOT_STATUS_BUMP_BASE)
290 return;
292 while (path[0] == '/')
293 path++;
295 printf("%c %s/%s\n", status, worktree_path, path);
298 static const struct got_error *
299 check_cancelled(void *arg)
301 if (sigint_received || sigpipe_received)
302 return got_error(GOT_ERR_CANCELLED);
303 return NULL;
306 static const struct got_error *
307 check_linear_ancestry(struct got_object_id *commit_id,
308 struct got_object_id *base_commit_id, struct got_repository *repo)
310 const struct got_error *err = NULL;
311 struct got_object_id *yca_id;
313 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
314 commit_id, base_commit_id, repo);
315 if (err)
316 return err;
318 if (yca_id == NULL)
319 return got_error(GOT_ERR_ANCESTRY);
321 /*
322 * Require a straight line of history between the target commit
323 * and the work tree's base commit.
325 * Non-linear situations such as this require a rebase:
327 * (commit) D F (base_commit)
328 * \ /
329 * C E
330 * \ /
331 * B (yca)
332 * |
333 * A
335 * 'got update' only handles linear cases:
336 * Update forwards in time: A (base/yca) - B - C - D (commit)
337 * Update backwards in time: D (base) - C - B - A (commit/yca)
338 */
339 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
340 got_object_id_cmp(base_commit_id, yca_id) != 0)
341 return got_error(GOT_ERR_ANCESTRY);
343 free(yca_id);
344 return NULL;
347 static const struct got_error *
348 check_same_branch(struct got_object_id *commit_id,
349 struct got_reference *head_ref, struct got_repository *repo)
351 const struct got_error *err = NULL;
352 struct got_commit_graph *graph = NULL;
353 struct got_object_id *head_commit_id = NULL;
354 int is_same_branch = 0;
356 err = got_ref_resolve(&head_commit_id, repo, head_ref);
357 if (err)
358 goto done;
360 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
361 if (err)
362 goto done;
364 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
365 if (err)
366 goto done;
368 for (;;) {
369 struct got_object_id *id;
370 err = got_commit_graph_iter_next(&id, graph);
371 if (err) {
372 if (err->code == GOT_ERR_ITER_COMPLETED) {
373 err = NULL;
374 break;
376 else if (err->code != GOT_ERR_ITER_NEED_MORE)
377 break;
378 err = got_commit_graph_fetch_commits(graph, 1,
379 repo);
380 if (err)
381 break;
384 if (id) {
385 if (got_object_id_cmp(id, commit_id) == 0) {
386 is_same_branch = 1;
387 break;
391 done:
392 if (graph)
393 got_commit_graph_close(graph);
394 free(head_commit_id);
395 if (!err && !is_same_branch)
396 err = got_error(GOT_ERR_ANCESTRY);
397 return err;
400 static const struct got_error *
401 cmd_checkout(int argc, char *argv[])
403 const struct got_error *error = NULL;
404 struct got_repository *repo = NULL;
405 struct got_reference *head_ref = NULL;
406 struct got_worktree *worktree = NULL;
407 char *repo_path = NULL;
408 char *worktree_path = NULL;
409 const char *path_prefix = "";
410 const char *branch_name = GOT_REF_HEAD;
411 char *commit_id_str = NULL;
412 int ch, same_path_prefix;
414 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
415 switch (ch) {
416 case 'b':
417 branch_name = optarg;
418 break;
419 case 'c':
420 commit_id_str = strdup(optarg);
421 if (commit_id_str == NULL)
422 return got_error_from_errno("strdup");
423 break;
424 case 'p':
425 path_prefix = optarg;
426 break;
427 default:
428 usage_checkout();
429 /* NOTREACHED */
433 argc -= optind;
434 argv += optind;
436 #ifndef PROFILE
437 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
438 "unveil", NULL) == -1)
439 err(1, "pledge");
440 #endif
441 if (argc == 1) {
442 char *cwd, *base, *dotgit;
443 repo_path = realpath(argv[0], NULL);
444 if (repo_path == NULL)
445 return got_error_from_errno2("realpath", argv[0]);
446 cwd = getcwd(NULL, 0);
447 if (cwd == NULL) {
448 error = got_error_from_errno("getcwd");
449 goto done;
451 if (path_prefix[0]) {
452 base = basename(path_prefix);
453 if (base == NULL) {
454 error = got_error_from_errno2("basename",
455 path_prefix);
456 goto done;
458 } else {
459 base = basename(repo_path);
460 if (base == NULL) {
461 error = got_error_from_errno2("basename",
462 repo_path);
463 goto done;
466 dotgit = strstr(base, ".git");
467 if (dotgit)
468 *dotgit = '\0';
469 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
470 error = got_error_from_errno("asprintf");
471 free(cwd);
472 goto done;
474 free(cwd);
475 } else if (argc == 2) {
476 repo_path = realpath(argv[0], NULL);
477 if (repo_path == NULL) {
478 error = got_error_from_errno2("realpath", argv[0]);
479 goto done;
481 worktree_path = realpath(argv[1], NULL);
482 if (worktree_path == NULL) {
483 error = got_error_from_errno2("realpath", argv[1]);
484 goto done;
486 } else
487 usage_checkout();
489 got_path_strip_trailing_slashes(repo_path);
490 got_path_strip_trailing_slashes(worktree_path);
492 error = got_repo_open(&repo, repo_path);
493 if (error != NULL)
494 goto done;
496 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
497 if (error)
498 goto done;
500 error = got_ref_open(&head_ref, repo, branch_name, 0);
501 if (error != NULL)
502 goto done;
504 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
505 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
506 goto done;
508 error = got_worktree_open(&worktree, worktree_path);
509 if (error != NULL)
510 goto done;
512 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
513 path_prefix);
514 if (error != NULL)
515 goto done;
516 if (!same_path_prefix) {
517 error = got_error(GOT_ERR_PATH_PREFIX);
518 goto done;
521 if (commit_id_str) {
522 struct got_object_id *commit_id;
523 error = got_object_resolve_id_str(&commit_id, repo,
524 commit_id_str);
525 if (error != NULL)
526 goto done;
527 error = check_linear_ancestry(commit_id,
528 got_worktree_get_base_commit_id(worktree), repo);
529 if (error != NULL) {
530 free(commit_id);
531 goto done;
533 error = check_same_branch(commit_id, head_ref, repo);
534 if (error)
535 goto done;
536 error = got_worktree_set_base_commit_id(worktree, repo,
537 commit_id);
538 free(commit_id);
539 if (error)
540 goto done;
543 error = got_worktree_checkout_files(worktree, "", repo,
544 checkout_progress, worktree_path, check_cancelled, NULL);
545 if (error != NULL)
546 goto done;
548 printf("Now shut up and hack\n");
550 done:
551 free(commit_id_str);
552 free(repo_path);
553 free(worktree_path);
554 return error;
557 __dead static void
558 usage_update(void)
560 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
561 getprogname());
562 exit(1);
565 static void
566 update_progress(void *arg, unsigned char status, const char *path)
568 int *did_something = arg;
570 if (status == GOT_STATUS_EXISTS)
571 return;
573 *did_something = 1;
575 /* Base commit bump happens silently. */
576 if (status == GOT_STATUS_BUMP_BASE)
577 return;
579 while (path[0] == '/')
580 path++;
581 printf("%c %s\n", status, path);
584 static const struct got_error *
585 switch_head_ref(struct got_reference *head_ref,
586 struct got_object_id *commit_id, struct got_worktree *worktree,
587 struct got_repository *repo)
589 const struct got_error *err = NULL;
590 char *base_id_str;
591 int ref_has_moved = 0;
593 /* Trivial case: switching between two different references. */
594 if (strcmp(got_ref_get_name(head_ref),
595 got_worktree_get_head_ref_name(worktree)) != 0) {
596 printf("Switching work tree from %s to %s\n",
597 got_worktree_get_head_ref_name(worktree),
598 got_ref_get_name(head_ref));
599 return got_worktree_set_head_ref(worktree, head_ref);
602 err = check_linear_ancestry(commit_id,
603 got_worktree_get_base_commit_id(worktree), repo);
604 if (err) {
605 if (err->code != GOT_ERR_ANCESTRY)
606 return err;
607 ref_has_moved = 1;
609 if (!ref_has_moved)
610 return NULL;
612 /* Switching to a rebased branch with the same reference name. */
613 err = got_object_id_str(&base_id_str,
614 got_worktree_get_base_commit_id(worktree));
615 if (err)
616 return err;
617 printf("Reference %s now points at a different branch\n",
618 got_worktree_get_head_ref_name(worktree));
619 printf("Switching work tree from %s to %s\n", base_id_str,
620 got_worktree_get_head_ref_name(worktree));
621 return NULL;
624 static const struct got_error *
625 cmd_update(int argc, char *argv[])
627 const struct got_error *error = NULL;
628 struct got_repository *repo = NULL;
629 struct got_worktree *worktree = NULL;
630 char *worktree_path = NULL, *path = NULL;
631 struct got_object_id *commit_id = NULL;
632 char *commit_id_str = NULL;
633 const char *branch_name = NULL;
634 struct got_reference *head_ref = NULL;
635 int ch, did_something = 0;
637 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
638 switch (ch) {
639 case 'b':
640 branch_name = optarg;
641 break;
642 case 'c':
643 commit_id_str = strdup(optarg);
644 if (commit_id_str == NULL)
645 return got_error_from_errno("strdup");
646 break;
647 default:
648 usage_update();
649 /* NOTREACHED */
653 argc -= optind;
654 argv += optind;
656 #ifndef PROFILE
657 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
658 "unveil", NULL) == -1)
659 err(1, "pledge");
660 #endif
661 worktree_path = getcwd(NULL, 0);
662 if (worktree_path == NULL) {
663 error = got_error_from_errno("getcwd");
664 goto done;
666 error = got_worktree_open(&worktree, worktree_path);
667 if (error)
668 goto done;
670 if (argc == 0) {
671 path = strdup("");
672 if (path == NULL) {
673 error = got_error_from_errno("strdup");
674 goto done;
676 } else if (argc == 1) {
677 error = got_worktree_resolve_path(&path, worktree, argv[0]);
678 if (error)
679 goto done;
680 } else
681 usage_update();
683 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
684 if (error != NULL)
685 goto done;
687 error = apply_unveil(got_repo_get_path(repo), 0,
688 got_worktree_get_root_path(worktree), 0);
689 if (error)
690 goto done;
692 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
693 got_worktree_get_head_ref_name(worktree), 0);
694 if (error != NULL)
695 goto done;
696 if (commit_id_str == NULL) {
697 error = got_ref_resolve(&commit_id, repo, head_ref);
698 if (error != NULL)
699 goto done;
700 error = got_object_id_str(&commit_id_str, commit_id);
701 if (error != NULL)
702 goto done;
703 } else {
704 error = got_object_resolve_id_str(&commit_id, repo,
705 commit_id_str);
706 if (error != NULL)
707 goto done;
710 if (branch_name) {
711 struct got_object_id *head_commit_id;
712 if (strlen(path) != 0) {
713 fprintf(stderr, "%s: switching between branches "
714 "requires that the entire work tree "
715 "gets updated, not just '%s'\n",
716 getprogname(), path);
717 error = got_error(GOT_ERR_BAD_PATH);
718 goto done;
720 error = got_ref_resolve(&head_commit_id, repo, head_ref);
721 if (error)
722 goto done;
723 error = check_linear_ancestry(commit_id, head_commit_id, repo);
724 free(head_commit_id);
725 if (error != NULL)
726 goto done;
727 error = check_same_branch(commit_id, head_ref, repo);
728 if (error)
729 goto done;
730 error = switch_head_ref(head_ref, commit_id, worktree, repo);
731 if (error)
732 goto done;
733 } else {
734 error = check_linear_ancestry(commit_id,
735 got_worktree_get_base_commit_id(worktree), repo);
736 if (error != NULL) {
737 if (error->code == GOT_ERR_ANCESTRY)
738 error = got_error(GOT_ERR_BRANCH_MOVED);
739 goto done;
741 error = check_same_branch(commit_id, head_ref, repo);
742 if (error)
743 goto done;
746 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
747 commit_id) != 0) {
748 error = got_worktree_set_base_commit_id(worktree, repo,
749 commit_id);
750 if (error)
751 goto done;
754 error = got_worktree_checkout_files(worktree, path, repo,
755 update_progress, &did_something, check_cancelled, NULL);
756 if (error != NULL)
757 goto done;
759 if (did_something)
760 printf("Updated to commit %s\n", commit_id_str);
761 else
762 printf("Already up-to-date\n");
763 done:
764 free(worktree_path);
765 free(path);
766 free(commit_id);
767 free(commit_id_str);
768 return error;
771 static const struct got_error *
772 print_patch(struct got_commit_object *commit, struct got_object_id *id,
773 int diff_context, struct got_repository *repo)
775 const struct got_error *err = NULL;
776 struct got_tree_object *tree1 = NULL, *tree2;
777 struct got_object_qid *qid;
778 char *id_str1 = NULL, *id_str2;
779 struct got_diff_blob_output_unidiff_arg arg;
781 err = got_object_open_as_tree(&tree2, repo,
782 got_object_commit_get_tree_id(commit));
783 if (err)
784 return err;
786 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
787 if (qid != NULL) {
788 struct got_commit_object *pcommit;
790 err = got_object_open_as_commit(&pcommit, repo, qid->id);
791 if (err)
792 return err;
794 err = got_object_open_as_tree(&tree1, repo,
795 got_object_commit_get_tree_id(pcommit));
796 got_object_commit_close(pcommit);
797 if (err)
798 return err;
800 err = got_object_id_str(&id_str1, qid->id);
801 if (err)
802 return err;
805 err = got_object_id_str(&id_str2, id);
806 if (err)
807 goto done;
809 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
810 arg.diff_context = diff_context;
811 arg.outfile = stdout;
812 err = got_diff_tree(tree1, tree2, "", "", repo,
813 got_diff_blob_output_unidiff, &arg);
814 done:
815 if (tree1)
816 got_object_tree_close(tree1);
817 got_object_tree_close(tree2);
818 free(id_str1);
819 free(id_str2);
820 return err;
823 static char *
824 get_datestr(time_t *time, char *datebuf)
826 char *p, *s = ctime_r(time, datebuf);
827 p = strchr(s, '\n');
828 if (p)
829 *p = '\0';
830 return s;
833 static const struct got_error *
834 print_commit(struct got_commit_object *commit, struct got_object_id *id,
835 struct got_repository *repo, int show_patch, int diff_context,
836 struct got_reflist_head *refs)
838 const struct got_error *err = NULL;
839 char *id_str, *datestr, *logmsg0, *logmsg, *line;
840 char datebuf[26];
841 time_t committer_time;
842 const char *author, *committer;
843 char *refs_str = NULL;
844 struct got_reflist_entry *re;
846 SIMPLEQ_FOREACH(re, refs, entry) {
847 char *s;
848 const char *name;
849 if (got_object_id_cmp(re->id, id) != 0)
850 continue;
851 name = got_ref_get_name(re->ref);
852 if (strcmp(name, GOT_REF_HEAD) == 0)
853 continue;
854 if (strncmp(name, "refs/", 5) == 0)
855 name += 5;
856 if (strncmp(name, "got/", 4) == 0)
857 continue;
858 if (strncmp(name, "heads/", 6) == 0)
859 name += 6;
860 if (strncmp(name, "remotes/", 8) == 0)
861 name += 8;
862 s = refs_str;
863 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
864 name) == -1) {
865 err = got_error_from_errno("asprintf");
866 free(s);
867 break;
869 free(s);
871 err = got_object_id_str(&id_str, id);
872 if (err)
873 return err;
875 printf("-----------------------------------------------\n");
876 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
877 refs_str ? refs_str : "", refs_str ? ")" : "");
878 free(id_str);
879 id_str = NULL;
880 free(refs_str);
881 refs_str = NULL;
882 printf("from: %s\n", got_object_commit_get_author(commit));
883 committer_time = got_object_commit_get_committer_time(commit);
884 datestr = get_datestr(&committer_time, datebuf);
885 printf("date: %s UTC\n", datestr);
886 author = got_object_commit_get_author(commit);
887 committer = got_object_commit_get_committer(commit);
888 if (strcmp(author, committer) != 0)
889 printf("via: %s\n", committer);
890 if (got_object_commit_get_nparents(commit) > 1) {
891 const struct got_object_id_queue *parent_ids;
892 struct got_object_qid *qid;
893 int n = 1;
894 parent_ids = got_object_commit_get_parent_ids(commit);
895 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
896 err = got_object_id_str(&id_str, qid->id);
897 if (err)
898 return err;
899 printf("parent %d: %s\n", n++, id_str);
900 free(id_str);
904 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
905 if (logmsg0 == NULL)
906 return got_error_from_errno("strdup");
908 logmsg = logmsg0;
909 do {
910 line = strsep(&logmsg, "\n");
911 if (line)
912 printf(" %s\n", line);
913 } while (line);
914 free(logmsg0);
916 if (show_patch) {
917 err = print_patch(commit, id, diff_context, repo);
918 if (err == 0)
919 printf("\n");
922 if (fflush(stdout) != 0 && err == NULL)
923 err = got_error_from_errno("fflush");
924 return err;
927 static const struct got_error *
928 print_commits(struct got_object_id *root_id, struct got_repository *repo,
929 char *path, int show_patch, int diff_context, int limit,
930 int first_parent_traversal, struct got_reflist_head *refs)
932 const struct got_error *err;
933 struct got_commit_graph *graph;
935 err = got_commit_graph_open(&graph, root_id, path,
936 first_parent_traversal, repo);
937 if (err)
938 return err;
939 err = got_commit_graph_iter_start(graph, root_id, repo);
940 if (err)
941 goto done;
942 for (;;) {
943 struct got_commit_object *commit;
944 struct got_object_id *id;
946 if (sigint_received || sigpipe_received)
947 break;
949 err = got_commit_graph_iter_next(&id, graph);
950 if (err) {
951 if (err->code == GOT_ERR_ITER_COMPLETED) {
952 err = NULL;
953 break;
955 if (err->code != GOT_ERR_ITER_NEED_MORE)
956 break;
957 err = got_commit_graph_fetch_commits(graph, 1, repo);
958 if (err)
959 break;
960 else
961 continue;
963 if (id == NULL)
964 break;
966 err = got_object_open_as_commit(&commit, repo, id);
967 if (err)
968 break;
969 err = print_commit(commit, id, repo, show_patch, diff_context,
970 refs);
971 got_object_commit_close(commit);
972 if (err || (limit && --limit == 0))
973 break;
975 done:
976 got_commit_graph_close(graph);
977 return err;
980 __dead static void
981 usage_log(void)
983 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
984 "[-r repository-path] [path]\n", getprogname());
985 exit(1);
988 static const struct got_error *
989 cmd_log(int argc, char *argv[])
991 const struct got_error *error;
992 struct got_repository *repo = NULL;
993 struct got_worktree *worktree = NULL;
994 struct got_commit_object *commit = NULL;
995 struct got_object_id *id = NULL;
996 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
997 char *start_commit = NULL;
998 int diff_context = 3, ch;
999 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1000 const char *errstr;
1001 struct got_reflist_head refs;
1003 SIMPLEQ_INIT(&refs);
1005 #ifndef PROFILE
1006 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1007 NULL)
1008 == -1)
1009 err(1, "pledge");
1010 #endif
1012 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1013 switch (ch) {
1014 case 'b':
1015 first_parent_traversal = 1;
1016 break;
1017 case 'p':
1018 show_patch = 1;
1019 break;
1020 case 'c':
1021 start_commit = optarg;
1022 break;
1023 case 'C':
1024 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1025 &errstr);
1026 if (errstr != NULL)
1027 err(1, "-C option %s", errstr);
1028 break;
1029 case 'l':
1030 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1031 if (errstr != NULL)
1032 err(1, "-l option %s", errstr);
1033 break;
1034 case 'r':
1035 repo_path = realpath(optarg, NULL);
1036 if (repo_path == NULL)
1037 err(1, "-r option");
1038 got_path_strip_trailing_slashes(repo_path);
1039 break;
1040 default:
1041 usage_log();
1042 /* NOTREACHED */
1046 argc -= optind;
1047 argv += optind;
1049 cwd = getcwd(NULL, 0);
1050 if (cwd == NULL) {
1051 error = got_error_from_errno("getcwd");
1052 goto done;
1055 error = got_worktree_open(&worktree, cwd);
1056 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1057 goto done;
1058 error = NULL;
1060 if (argc == 0) {
1061 path = strdup("");
1062 if (path == NULL) {
1063 error = got_error_from_errno("strdup");
1064 goto done;
1066 } else if (argc == 1) {
1067 if (worktree) {
1068 error = got_worktree_resolve_path(&path, worktree,
1069 argv[0]);
1070 if (error)
1071 goto done;
1072 } else {
1073 path = strdup(argv[0]);
1074 if (path == NULL) {
1075 error = got_error_from_errno("strdup");
1076 goto done;
1079 } else
1080 usage_log();
1082 if (repo_path == NULL) {
1083 repo_path = worktree ?
1084 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1086 if (repo_path == NULL) {
1087 error = got_error_from_errno("strdup");
1088 goto done;
1091 error = got_repo_open(&repo, repo_path);
1092 if (error != NULL)
1093 goto done;
1095 error = apply_unveil(got_repo_get_path(repo), 1,
1096 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1097 if (error)
1098 goto done;
1100 if (start_commit == NULL) {
1101 struct got_reference *head_ref;
1102 error = got_ref_open(&head_ref, repo,
1103 worktree ? got_worktree_get_head_ref_name(worktree)
1104 : GOT_REF_HEAD, 0);
1105 if (error != NULL)
1106 return error;
1107 error = got_ref_resolve(&id, repo, head_ref);
1108 got_ref_close(head_ref);
1109 if (error != NULL)
1110 return error;
1111 error = got_object_open_as_commit(&commit, repo, id);
1112 } else {
1113 struct got_reference *ref;
1114 error = got_ref_open(&ref, repo, start_commit, 0);
1115 if (error == NULL) {
1116 int obj_type;
1117 error = got_ref_resolve(&id, repo, ref);
1118 got_ref_close(ref);
1119 if (error != NULL)
1120 goto done;
1121 error = got_object_get_type(&obj_type, repo, id);
1122 if (error != NULL)
1123 goto done;
1124 if (obj_type == GOT_OBJ_TYPE_TAG) {
1125 struct got_tag_object *tag;
1126 error = got_object_open_as_tag(&tag, repo, id);
1127 if (error != NULL)
1128 goto done;
1129 if (got_object_tag_get_object_type(tag) !=
1130 GOT_OBJ_TYPE_COMMIT) {
1131 got_object_tag_close(tag);
1132 error = got_error(GOT_ERR_OBJ_TYPE);
1133 goto done;
1135 free(id);
1136 id = got_object_id_dup(
1137 got_object_tag_get_object_id(tag));
1138 if (id == NULL)
1139 error = got_error_from_errno(
1140 "got_object_id_dup");
1141 got_object_tag_close(tag);
1142 if (error)
1143 goto done;
1144 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1145 error = got_error(GOT_ERR_OBJ_TYPE);
1146 goto done;
1148 error = got_object_open_as_commit(&commit, repo, id);
1149 if (error != NULL)
1150 goto done;
1152 if (commit == NULL) {
1153 error = got_object_resolve_id_str(&id, repo,
1154 start_commit);
1155 if (error != NULL)
1156 return error;
1159 if (error != NULL)
1160 goto done;
1162 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1163 if (error != NULL)
1164 goto done;
1165 if (in_repo_path) {
1166 free(path);
1167 path = in_repo_path;
1170 error = got_ref_list(&refs, repo);
1171 if (error)
1172 goto done;
1174 error = print_commits(id, repo, path, show_patch,
1175 diff_context, limit, first_parent_traversal, &refs);
1176 done:
1177 free(path);
1178 free(repo_path);
1179 free(cwd);
1180 free(id);
1181 if (worktree)
1182 got_worktree_close(worktree);
1183 if (repo) {
1184 const struct got_error *repo_error;
1185 repo_error = got_repo_close(repo);
1186 if (error == NULL)
1187 error = repo_error;
1189 got_ref_list_free(&refs);
1190 return error;
1193 __dead static void
1194 usage_diff(void)
1196 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1197 "[object1 object2 | path]\n", getprogname());
1198 exit(1);
1201 struct print_diff_arg {
1202 struct got_repository *repo;
1203 struct got_worktree *worktree;
1204 int diff_context;
1205 const char *id_str;
1206 int header_shown;
1209 static const struct got_error *
1210 print_diff(void *arg, unsigned char status, const char *path,
1211 struct got_object_id *blob_id, struct got_object_id *commit_id)
1213 struct print_diff_arg *a = arg;
1214 const struct got_error *err = NULL;
1215 struct got_blob_object *blob1 = NULL;
1216 FILE *f2 = NULL;
1217 char *abspath = NULL;
1218 struct stat sb;
1220 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1221 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1222 return NULL;
1224 if (!a->header_shown) {
1225 printf("diff %s %s\n", a->id_str,
1226 got_worktree_get_root_path(a->worktree));
1227 a->header_shown = 1;
1230 if (status != GOT_STATUS_ADD) {
1231 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1232 if (err)
1233 goto done;
1237 if (status != GOT_STATUS_DELETE) {
1238 if (asprintf(&abspath, "%s/%s",
1239 got_worktree_get_root_path(a->worktree), path) == -1) {
1240 err = got_error_from_errno("asprintf");
1241 goto done;
1244 f2 = fopen(abspath, "r");
1245 if (f2 == NULL) {
1246 err = got_error_from_errno2("fopen", abspath);
1247 goto done;
1249 if (lstat(abspath, &sb) == -1) {
1250 err = got_error_from_errno2("lstat", abspath);
1251 goto done;
1253 } else
1254 sb.st_size = 0;
1256 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1257 stdout);
1258 done:
1259 if (blob1)
1260 got_object_blob_close(blob1);
1261 if (f2 && fclose(f2) != 0 && err == NULL)
1262 err = got_error_from_errno("fclose");
1263 free(abspath);
1264 return err;
1267 static const struct got_error *
1268 cmd_diff(int argc, char *argv[])
1270 const struct got_error *error;
1271 struct got_repository *repo = NULL;
1272 struct got_worktree *worktree = NULL;
1273 char *cwd = NULL, *repo_path = NULL;
1274 struct got_object_id *id1 = NULL, *id2 = NULL;
1275 const char *id_str1 = NULL, *id_str2 = NULL;
1276 char *label1 = NULL, *label2 = NULL;
1277 int type1, type2;
1278 int diff_context = 3, ch;
1279 const char *errstr;
1280 char *path = NULL;
1282 #ifndef PROFILE
1283 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1284 NULL) == -1)
1285 err(1, "pledge");
1286 #endif
1288 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1289 switch (ch) {
1290 case 'C':
1291 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1292 if (errstr != NULL)
1293 err(1, "-C option %s", errstr);
1294 break;
1295 case 'r':
1296 repo_path = realpath(optarg, NULL);
1297 if (repo_path == NULL)
1298 err(1, "-r option");
1299 got_path_strip_trailing_slashes(repo_path);
1300 break;
1301 default:
1302 usage_diff();
1303 /* NOTREACHED */
1307 argc -= optind;
1308 argv += optind;
1310 cwd = getcwd(NULL, 0);
1311 if (cwd == NULL) {
1312 error = got_error_from_errno("getcwd");
1313 goto done;
1315 error = got_worktree_open(&worktree, cwd);
1316 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1317 goto done;
1318 if (argc <= 1) {
1319 if (worktree == NULL) {
1320 error = got_error(GOT_ERR_NOT_WORKTREE);
1321 goto done;
1323 if (repo_path)
1324 errx(1,
1325 "-r option can't be used when diffing a work tree");
1326 repo_path = strdup(got_worktree_get_repo_path(worktree));
1327 if (repo_path == NULL) {
1328 error = got_error_from_errno("strdup");
1329 goto done;
1331 if (argc == 1) {
1332 error = got_worktree_resolve_path(&path, worktree,
1333 argv[0]);
1334 if (error)
1335 goto done;
1336 } else {
1337 path = strdup("");
1338 if (path == NULL) {
1339 error = got_error_from_errno("strdup");
1340 goto done;
1343 } else if (argc == 2) {
1344 id_str1 = argv[0];
1345 id_str2 = argv[1];
1346 if (worktree && repo_path == NULL) {
1347 repo_path =
1348 strdup(got_worktree_get_repo_path(worktree));
1349 if (repo_path == NULL) {
1350 error = got_error_from_errno("strdup");
1351 goto done;
1354 } else
1355 usage_diff();
1357 if (repo_path == NULL) {
1358 repo_path = getcwd(NULL, 0);
1359 if (repo_path == NULL)
1360 return got_error_from_errno("getcwd");
1363 error = got_repo_open(&repo, repo_path);
1364 free(repo_path);
1365 if (error != NULL)
1366 goto done;
1368 error = apply_unveil(got_repo_get_path(repo), 1,
1369 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1370 if (error)
1371 goto done;
1373 if (argc <= 1) {
1374 struct print_diff_arg arg;
1375 char *id_str;
1376 error = got_object_id_str(&id_str,
1377 got_worktree_get_base_commit_id(worktree));
1378 if (error)
1379 goto done;
1380 arg.repo = repo;
1381 arg.worktree = worktree;
1382 arg.diff_context = diff_context;
1383 arg.id_str = id_str;
1384 arg.header_shown = 0;
1386 error = got_worktree_status(worktree, path, repo, print_diff,
1387 &arg, check_cancelled, NULL);
1388 free(id_str);
1389 goto done;
1392 error = got_object_resolve_id_str(&id1, repo, id_str1);
1393 if (error) {
1394 struct got_reference *ref;
1395 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1396 goto done;
1397 error = got_ref_open(&ref, repo, id_str1, 0);
1398 if (error != NULL)
1399 goto done;
1400 label1 = strdup(got_ref_get_name(ref));
1401 if (label1 == NULL) {
1402 error = got_error_from_errno("strdup");
1403 goto done;
1405 error = got_ref_resolve(&id1, repo, ref);
1406 got_ref_close(ref);
1407 if (error != NULL)
1408 goto done;
1409 } else {
1410 label1 = strdup(id_str1);
1411 if (label1 == NULL) {
1412 error = got_error_from_errno("strdup");
1413 goto done;
1417 error = got_object_resolve_id_str(&id2, repo, id_str2);
1418 if (error) {
1419 struct got_reference *ref;
1420 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1421 goto done;
1422 error = got_ref_open(&ref, repo, id_str2, 0);
1423 if (error != NULL)
1424 goto done;
1425 label2 = strdup(got_ref_get_name(ref));
1426 if (label2 == NULL) {
1427 error = got_error_from_errno("strdup");
1428 goto done;
1430 error = got_ref_resolve(&id2, repo, ref);
1431 got_ref_close(ref);
1432 if (error != NULL)
1433 goto done;
1434 } else {
1435 label2 = strdup(id_str2);
1436 if (label2 == NULL) {
1437 error = got_error_from_errno("strdup");
1438 goto done;
1442 error = got_object_get_type(&type1, repo, id1);
1443 if (error)
1444 goto done;
1446 error = got_object_get_type(&type2, repo, id2);
1447 if (error)
1448 goto done;
1450 if (type1 != type2) {
1451 error = got_error(GOT_ERR_OBJ_TYPE);
1452 goto done;
1455 switch (type1) {
1456 case GOT_OBJ_TYPE_BLOB:
1457 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1458 diff_context, repo, stdout);
1459 break;
1460 case GOT_OBJ_TYPE_TREE:
1461 error = got_diff_objects_as_trees(id1, id2, "", "",
1462 diff_context, repo, stdout);
1463 break;
1464 case GOT_OBJ_TYPE_COMMIT:
1465 printf("diff %s %s\n", label1, label2);
1466 error = got_diff_objects_as_commits(id1, id2, diff_context,
1467 repo, stdout);
1468 break;
1469 default:
1470 error = got_error(GOT_ERR_OBJ_TYPE);
1473 done:
1474 free(label1);
1475 free(label2);
1476 free(id1);
1477 free(id2);
1478 free(path);
1479 if (worktree)
1480 got_worktree_close(worktree);
1481 if (repo) {
1482 const struct got_error *repo_error;
1483 repo_error = got_repo_close(repo);
1484 if (error == NULL)
1485 error = repo_error;
1487 return error;
1490 __dead static void
1491 usage_blame(void)
1493 fprintf(stderr,
1494 "usage: %s blame [-c commit] [-r repository-path] path\n",
1495 getprogname());
1496 exit(1);
1499 static const struct got_error *
1500 cmd_blame(int argc, char *argv[])
1502 const struct got_error *error;
1503 struct got_repository *repo = NULL;
1504 struct got_worktree *worktree = NULL;
1505 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1506 struct got_object_id *commit_id = NULL;
1507 char *commit_id_str = NULL;
1508 int ch;
1510 #ifndef PROFILE
1511 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1512 NULL) == -1)
1513 err(1, "pledge");
1514 #endif
1516 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1517 switch (ch) {
1518 case 'c':
1519 commit_id_str = optarg;
1520 break;
1521 case 'r':
1522 repo_path = realpath(optarg, NULL);
1523 if (repo_path == NULL)
1524 err(1, "-r option");
1525 got_path_strip_trailing_slashes(repo_path);
1526 break;
1527 default:
1528 usage_blame();
1529 /* NOTREACHED */
1533 argc -= optind;
1534 argv += optind;
1536 if (argc == 1)
1537 path = argv[0];
1538 else
1539 usage_blame();
1541 cwd = getcwd(NULL, 0);
1542 if (cwd == NULL) {
1543 error = got_error_from_errno("getcwd");
1544 goto done;
1546 if (repo_path == NULL) {
1547 error = got_worktree_open(&worktree, cwd);
1548 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1549 goto done;
1550 else
1551 error = NULL;
1552 if (worktree) {
1553 repo_path =
1554 strdup(got_worktree_get_repo_path(worktree));
1555 if (repo_path == NULL)
1556 error = got_error_from_errno("strdup");
1557 if (error)
1558 goto done;
1559 } else {
1560 repo_path = strdup(cwd);
1561 if (repo_path == NULL) {
1562 error = got_error_from_errno("strdup");
1563 goto done;
1568 error = got_repo_open(&repo, repo_path);
1569 if (error != NULL)
1570 goto done;
1572 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1573 if (error)
1574 goto done;
1576 if (worktree) {
1577 const char *prefix = got_worktree_get_path_prefix(worktree);
1578 char *p, *worktree_subdir = cwd +
1579 strlen(got_worktree_get_root_path(worktree));
1580 if (asprintf(&p, "%s%s%s%s%s",
1581 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1582 worktree_subdir, worktree_subdir[0] ? "/" : "",
1583 path) == -1) {
1584 error = got_error_from_errno("asprintf");
1585 goto done;
1587 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1588 free(p);
1589 } else {
1590 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1592 if (error)
1593 goto done;
1595 if (commit_id_str == NULL) {
1596 struct got_reference *head_ref;
1597 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1598 if (error != NULL)
1599 goto done;
1600 error = got_ref_resolve(&commit_id, repo, head_ref);
1601 got_ref_close(head_ref);
1602 if (error != NULL)
1603 goto done;
1604 } else {
1605 error = got_object_resolve_id_str(&commit_id, repo,
1606 commit_id_str);
1607 if (error != NULL)
1608 goto done;
1611 error = got_blame(in_repo_path, commit_id, repo, stdout);
1612 done:
1613 free(in_repo_path);
1614 free(repo_path);
1615 free(cwd);
1616 free(commit_id);
1617 if (worktree)
1618 got_worktree_close(worktree);
1619 if (repo) {
1620 const struct got_error *repo_error;
1621 repo_error = got_repo_close(repo);
1622 if (error == NULL)
1623 error = repo_error;
1625 return error;
1628 __dead static void
1629 usage_tree(void)
1631 fprintf(stderr,
1632 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1633 getprogname());
1634 exit(1);
1637 static void
1638 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1639 const char *root_path)
1641 int is_root_path = (strcmp(path, root_path) == 0);
1643 path += strlen(root_path);
1644 while (path[0] == '/')
1645 path++;
1647 printf("%s%s%s%s%s\n", id ? id : "", path,
1648 is_root_path ? "" : "/", te->name,
1649 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1652 static const struct got_error *
1653 print_tree(const char *path, struct got_object_id *commit_id,
1654 int show_ids, int recurse, const char *root_path,
1655 struct got_repository *repo)
1657 const struct got_error *err = NULL;
1658 struct got_object_id *tree_id = NULL;
1659 struct got_tree_object *tree = NULL;
1660 const struct got_tree_entries *entries;
1661 struct got_tree_entry *te;
1663 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1664 if (err)
1665 goto done;
1667 err = got_object_open_as_tree(&tree, repo, tree_id);
1668 if (err)
1669 goto done;
1670 entries = got_object_tree_get_entries(tree);
1671 te = SIMPLEQ_FIRST(&entries->head);
1672 while (te) {
1673 char *id = NULL;
1675 if (sigint_received || sigpipe_received)
1676 break;
1678 if (show_ids) {
1679 char *id_str;
1680 err = got_object_id_str(&id_str, te->id);
1681 if (err)
1682 goto done;
1683 if (asprintf(&id, "%s ", id_str) == -1) {
1684 err = got_error_from_errno("asprintf");
1685 free(id_str);
1686 goto done;
1688 free(id_str);
1690 print_entry(te, id, path, root_path);
1691 free(id);
1693 if (recurse && S_ISDIR(te->mode)) {
1694 char *child_path;
1695 if (asprintf(&child_path, "%s%s%s", path,
1696 path[0] == '/' && path[1] == '\0' ? "" : "/",
1697 te->name) == -1) {
1698 err = got_error_from_errno("asprintf");
1699 goto done;
1701 err = print_tree(child_path, commit_id, show_ids, 1,
1702 root_path, repo);
1703 free(child_path);
1704 if (err)
1705 goto done;
1708 te = SIMPLEQ_NEXT(te, entry);
1710 done:
1711 if (tree)
1712 got_object_tree_close(tree);
1713 free(tree_id);
1714 return err;
1717 static const struct got_error *
1718 cmd_tree(int argc, char *argv[])
1720 const struct got_error *error;
1721 struct got_repository *repo = NULL;
1722 struct got_worktree *worktree = NULL;
1723 const char *path;
1724 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1725 struct got_object_id *commit_id = NULL;
1726 char *commit_id_str = NULL;
1727 int show_ids = 0, recurse = 0;
1728 int ch;
1730 #ifndef PROFILE
1731 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1732 NULL) == -1)
1733 err(1, "pledge");
1734 #endif
1736 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1737 switch (ch) {
1738 case 'c':
1739 commit_id_str = optarg;
1740 break;
1741 case 'r':
1742 repo_path = realpath(optarg, NULL);
1743 if (repo_path == NULL)
1744 err(1, "-r option");
1745 got_path_strip_trailing_slashes(repo_path);
1746 break;
1747 case 'i':
1748 show_ids = 1;
1749 break;
1750 case 'R':
1751 recurse = 1;
1752 break;
1753 default:
1754 usage_tree();
1755 /* NOTREACHED */
1759 argc -= optind;
1760 argv += optind;
1762 if (argc == 1)
1763 path = argv[0];
1764 else if (argc > 1)
1765 usage_tree();
1766 else
1767 path = NULL;
1769 cwd = getcwd(NULL, 0);
1770 if (cwd == NULL) {
1771 error = got_error_from_errno("getcwd");
1772 goto done;
1774 if (repo_path == NULL) {
1775 error = got_worktree_open(&worktree, cwd);
1776 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1777 goto done;
1778 else
1779 error = NULL;
1780 if (worktree) {
1781 repo_path =
1782 strdup(got_worktree_get_repo_path(worktree));
1783 if (repo_path == NULL)
1784 error = got_error_from_errno("strdup");
1785 if (error)
1786 goto done;
1787 } else {
1788 repo_path = strdup(cwd);
1789 if (repo_path == NULL) {
1790 error = got_error_from_errno("strdup");
1791 goto done;
1796 error = got_repo_open(&repo, repo_path);
1797 if (error != NULL)
1798 goto done;
1800 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1801 if (error)
1802 goto done;
1804 if (path == NULL) {
1805 if (worktree) {
1806 char *p, *worktree_subdir = cwd +
1807 strlen(got_worktree_get_root_path(worktree));
1808 if (asprintf(&p, "%s/%s",
1809 got_worktree_get_path_prefix(worktree),
1810 worktree_subdir) == -1) {
1811 error = got_error_from_errno("asprintf");
1812 goto done;
1814 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1815 free(p);
1816 if (error)
1817 goto done;
1818 } else
1819 path = "/";
1821 if (in_repo_path == NULL) {
1822 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1823 if (error != NULL)
1824 goto done;
1827 if (commit_id_str == NULL) {
1828 struct got_reference *head_ref;
1829 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1830 if (error != NULL)
1831 goto done;
1832 error = got_ref_resolve(&commit_id, repo, head_ref);
1833 got_ref_close(head_ref);
1834 if (error != NULL)
1835 goto done;
1836 } else {
1837 error = got_object_resolve_id_str(&commit_id, repo,
1838 commit_id_str);
1839 if (error != NULL)
1840 goto done;
1843 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1844 in_repo_path, repo);
1845 done:
1846 free(in_repo_path);
1847 free(repo_path);
1848 free(cwd);
1849 free(commit_id);
1850 if (worktree)
1851 got_worktree_close(worktree);
1852 if (repo) {
1853 const struct got_error *repo_error;
1854 repo_error = got_repo_close(repo);
1855 if (error == NULL)
1856 error = repo_error;
1858 return error;
1861 __dead static void
1862 usage_status(void)
1864 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1865 exit(1);
1868 static const struct got_error *
1869 print_status(void *arg, unsigned char status, const char *path,
1870 struct got_object_id *blob_id, struct got_object_id *commit_id)
1872 printf("%c %s\n", status, path);
1873 return NULL;
1876 static const struct got_error *
1877 cmd_status(int argc, char *argv[])
1879 const struct got_error *error = NULL;
1880 struct got_repository *repo = NULL;
1881 struct got_worktree *worktree = NULL;
1882 char *cwd = NULL, *path = NULL;
1883 int ch;
1885 while ((ch = getopt(argc, argv, "")) != -1) {
1886 switch (ch) {
1887 default:
1888 usage_status();
1889 /* NOTREACHED */
1893 argc -= optind;
1894 argv += optind;
1896 #ifndef PROFILE
1897 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1898 NULL) == -1)
1899 err(1, "pledge");
1900 #endif
1901 cwd = getcwd(NULL, 0);
1902 if (cwd == NULL) {
1903 error = got_error_from_errno("getcwd");
1904 goto done;
1907 error = got_worktree_open(&worktree, cwd);
1908 if (error != NULL)
1909 goto done;
1911 if (argc == 0) {
1912 path = strdup("");
1913 if (path == NULL) {
1914 error = got_error_from_errno("strdup");
1915 goto done;
1917 } else if (argc == 1) {
1918 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1919 if (error)
1920 goto done;
1921 } else
1922 usage_status();
1924 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1925 if (error != NULL)
1926 goto done;
1928 error = apply_unveil(got_repo_get_path(repo), 1,
1929 got_worktree_get_root_path(worktree), 0);
1930 if (error)
1931 goto done;
1933 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1934 check_cancelled, NULL);
1935 done:
1936 free(cwd);
1937 free(path);
1938 return error;
1941 __dead static void
1942 usage_ref(void)
1944 fprintf(stderr,
1945 "usage: %s ref [-r repository] -l | -d name | name target\n",
1946 getprogname());
1947 exit(1);
1950 static const struct got_error *
1951 list_refs(struct got_repository *repo)
1953 static const struct got_error *err = NULL;
1954 struct got_reflist_head refs;
1955 struct got_reflist_entry *re;
1957 SIMPLEQ_INIT(&refs);
1958 err = got_ref_list(&refs, repo);
1959 if (err)
1960 return err;
1962 SIMPLEQ_FOREACH(re, &refs, entry) {
1963 char *refstr;
1964 refstr = got_ref_to_str(re->ref);
1965 if (refstr == NULL)
1966 return got_error_from_errno("got_ref_to_str");
1967 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1968 free(refstr);
1971 got_ref_list_free(&refs);
1972 return NULL;
1975 static const struct got_error *
1976 delete_ref(struct got_repository *repo, const char *refname)
1978 const struct got_error *err = NULL;
1979 struct got_reference *ref;
1981 err = got_ref_open(&ref, repo, refname, 0);
1982 if (err)
1983 return err;
1985 err = got_ref_delete(ref, repo);
1986 got_ref_close(ref);
1987 return err;
1990 static const struct got_error *
1991 add_ref(struct got_repository *repo, const char *refname, const char *target)
1993 const struct got_error *err = NULL;
1994 struct got_object_id *id;
1995 struct got_reference *ref = NULL;
1997 err = got_object_resolve_id_str(&id, repo, target);
1998 if (err) {
1999 struct got_reference *target_ref;
2001 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2002 return err;
2003 err = got_ref_open(&target_ref, repo, target, 0);
2004 if (err)
2005 return err;
2006 err = got_ref_resolve(&id, repo, target_ref);
2007 got_ref_close(target_ref);
2008 if (err)
2009 return err;
2012 err = got_ref_alloc(&ref, refname, id);
2013 if (err)
2014 goto done;
2016 err = got_ref_write(ref, repo);
2017 done:
2018 if (ref)
2019 got_ref_close(ref);
2020 free(id);
2021 return err;
2024 static const struct got_error *
2025 cmd_ref(int argc, char *argv[])
2027 const struct got_error *error = NULL;
2028 struct got_repository *repo = NULL;
2029 struct got_worktree *worktree = NULL;
2030 char *cwd = NULL, *repo_path = NULL;
2031 int ch, do_list = 0;
2032 const char *delref = NULL;
2034 /* TODO: Add -s option for adding symbolic references. */
2035 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2036 switch (ch) {
2037 case 'd':
2038 delref = optarg;
2039 break;
2040 case 'r':
2041 repo_path = realpath(optarg, NULL);
2042 if (repo_path == NULL)
2043 err(1, "-r option");
2044 got_path_strip_trailing_slashes(repo_path);
2045 break;
2046 case 'l':
2047 do_list = 1;
2048 break;
2049 default:
2050 usage_ref();
2051 /* NOTREACHED */
2055 if (do_list && delref)
2056 errx(1, "-l and -d options are mutually exclusive\n");
2058 argc -= optind;
2059 argv += optind;
2061 if (do_list || delref) {
2062 if (argc > 0)
2063 usage_ref();
2064 } else if (argc != 2)
2065 usage_ref();
2067 #ifndef PROFILE
2068 if (do_list) {
2069 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2070 NULL) == -1)
2071 err(1, "pledge");
2072 } else {
2073 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2074 "sendfd unveil", NULL) == -1)
2075 err(1, "pledge");
2077 #endif
2078 cwd = getcwd(NULL, 0);
2079 if (cwd == NULL) {
2080 error = got_error_from_errno("getcwd");
2081 goto done;
2084 if (repo_path == NULL) {
2085 error = got_worktree_open(&worktree, cwd);
2086 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2087 goto done;
2088 else
2089 error = NULL;
2090 if (worktree) {
2091 repo_path =
2092 strdup(got_worktree_get_repo_path(worktree));
2093 if (repo_path == NULL)
2094 error = got_error_from_errno("strdup");
2095 if (error)
2096 goto done;
2097 } else {
2098 repo_path = strdup(cwd);
2099 if (repo_path == NULL) {
2100 error = got_error_from_errno("strdup");
2101 goto done;
2106 error = got_repo_open(&repo, repo_path);
2107 if (error != NULL)
2108 goto done;
2110 error = apply_unveil(got_repo_get_path(repo), do_list,
2111 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2112 if (error)
2113 goto done;
2115 if (do_list)
2116 error = list_refs(repo);
2117 else if (delref)
2118 error = delete_ref(repo, delref);
2119 else
2120 error = add_ref(repo, argv[0], argv[1]);
2121 done:
2122 if (repo)
2123 got_repo_close(repo);
2124 if (worktree)
2125 got_worktree_close(worktree);
2126 free(cwd);
2127 free(repo_path);
2128 return error;
2131 __dead static void
2132 usage_add(void)
2134 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2135 exit(1);
2138 static const struct got_error *
2139 cmd_add(int argc, char *argv[])
2141 const struct got_error *error = NULL;
2142 struct got_repository *repo = NULL;
2143 struct got_worktree *worktree = NULL;
2144 char *cwd = NULL;
2145 struct got_pathlist_head paths;
2146 struct got_pathlist_entry *pe;
2147 int ch, x;
2149 TAILQ_INIT(&paths);
2151 while ((ch = getopt(argc, argv, "")) != -1) {
2152 switch (ch) {
2153 default:
2154 usage_add();
2155 /* NOTREACHED */
2159 argc -= optind;
2160 argv += optind;
2162 if (argc < 1)
2163 usage_add();
2165 /* make sure each file exists before doing anything halfway */
2166 for (x = 0; x < argc; x++) {
2167 char *path = realpath(argv[x], NULL);
2168 if (path == NULL) {
2169 error = got_error_from_errno2("realpath", argv[x]);
2170 goto done;
2172 free(path);
2175 cwd = getcwd(NULL, 0);
2176 if (cwd == NULL) {
2177 error = got_error_from_errno("getcwd");
2178 goto done;
2181 error = got_worktree_open(&worktree, cwd);
2182 if (error)
2183 goto done;
2185 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2186 if (error != NULL)
2187 goto done;
2189 error = apply_unveil(got_repo_get_path(repo), 1,
2190 got_worktree_get_root_path(worktree), 0);
2191 if (error)
2192 goto done;
2194 for (x = 0; x < argc; x++) {
2195 char *path = realpath(argv[x], NULL);
2196 if (path == NULL) {
2197 error = got_error_from_errno2("realpath", argv[x]);
2198 goto done;
2201 got_path_strip_trailing_slashes(path);
2202 error = got_pathlist_insert(&pe, &paths, path, NULL);
2203 if (error) {
2204 free(path);
2205 goto done;
2208 error = got_worktree_schedule_add(worktree, &paths, print_status,
2209 NULL, repo);
2210 done:
2211 if (repo)
2212 got_repo_close(repo);
2213 if (worktree)
2214 got_worktree_close(worktree);
2215 TAILQ_FOREACH(pe, &paths, entry)
2216 free((char *)pe->path);
2217 got_pathlist_free(&paths);
2218 free(cwd);
2219 return error;
2222 __dead static void
2223 usage_rm(void)
2225 fprintf(stderr, "usage: %s rm [-f] file-path ...\n", getprogname());
2226 exit(1);
2229 static const struct got_error *
2230 cmd_rm(int argc, char *argv[])
2232 const struct got_error *error = NULL;
2233 struct got_worktree *worktree = NULL;
2234 struct got_repository *repo = NULL;
2235 char *cwd = NULL;
2236 struct got_pathlist_head paths;
2237 struct got_pathlist_entry *pe;
2238 int ch, i, delete_local_mods = 0;
2240 TAILQ_INIT(&paths);
2242 while ((ch = getopt(argc, argv, "f")) != -1) {
2243 switch (ch) {
2244 case 'f':
2245 delete_local_mods = 1;
2246 break;
2247 default:
2248 usage_add();
2249 /* NOTREACHED */
2253 argc -= optind;
2254 argv += optind;
2256 if (argc < 1)
2257 usage_rm();
2259 /* make sure each file exists before doing anything halfway */
2260 for (i = 0; i < argc; i++) {
2261 char *path = realpath(argv[i], NULL);
2262 if (path == NULL) {
2263 error = got_error_from_errno2("realpath", argv[i]);
2264 goto done;
2266 free(path);
2269 cwd = getcwd(NULL, 0);
2270 if (cwd == NULL) {
2271 error = got_error_from_errno("getcwd");
2272 goto done;
2274 error = got_worktree_open(&worktree, cwd);
2275 if (error)
2276 goto done;
2278 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2279 if (error)
2280 goto done;
2282 error = apply_unveil(got_repo_get_path(repo), 1,
2283 got_worktree_get_root_path(worktree), 0);
2284 if (error)
2285 goto done;
2287 for (i = 0; i < argc; i++) {
2288 char *path = realpath(argv[i], NULL);
2289 if (path == NULL) {
2290 error = got_error_from_errno2("realpath", argv[i]);
2291 goto done;
2294 got_path_strip_trailing_slashes(path);
2295 error = got_pathlist_insert(&pe, &paths, path, NULL);
2296 if (error) {
2297 free(path);
2298 goto done;
2301 error = got_worktree_schedule_delete(worktree, &paths,
2302 delete_local_mods, print_status, NULL, repo);
2303 if (error)
2304 goto done;
2305 done:
2306 if (repo)
2307 got_repo_close(repo);
2308 if (worktree)
2309 got_worktree_close(worktree);
2310 TAILQ_FOREACH(pe, &paths, entry)
2311 free((char *)pe->path);
2312 got_pathlist_free(&paths);
2313 free(cwd);
2314 return error;
2317 __dead static void
2318 usage_revert(void)
2320 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2321 exit(1);
2324 static void
2325 revert_progress(void *arg, unsigned char status, const char *path)
2327 while (path[0] == '/')
2328 path++;
2329 printf("%c %s\n", status, path);
2332 static const struct got_error *
2333 cmd_revert(int argc, char *argv[])
2335 const struct got_error *error = NULL;
2336 struct got_worktree *worktree = NULL;
2337 struct got_repository *repo = NULL;
2338 char *cwd = NULL, *path = NULL;
2339 struct got_pathlist_head paths;
2340 struct got_pathlist_entry *pe;
2341 int ch, i;
2343 TAILQ_INIT(&paths);
2345 while ((ch = getopt(argc, argv, "")) != -1) {
2346 switch (ch) {
2347 default:
2348 usage_revert();
2349 /* NOTREACHED */
2353 argc -= optind;
2354 argv += optind;
2356 if (argc < 1)
2357 usage_revert();
2359 for (i = 0; i < argc; i++) {
2360 char *path = realpath(argv[i], NULL);
2361 if (path == NULL) {
2362 error = got_error_from_errno2("realpath", argv[i]);
2363 goto done;
2366 got_path_strip_trailing_slashes(path);
2367 error = got_pathlist_insert(&pe, &paths, path, NULL);
2368 if (error) {
2369 free(path);
2370 goto done;
2374 cwd = getcwd(NULL, 0);
2375 if (cwd == NULL) {
2376 error = got_error_from_errno("getcwd");
2377 goto done;
2379 error = got_worktree_open(&worktree, cwd);
2380 if (error)
2381 goto done;
2383 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2384 if (error != NULL)
2385 goto done;
2387 error = apply_unveil(got_repo_get_path(repo), 1,
2388 got_worktree_get_root_path(worktree), 0);
2389 if (error)
2390 goto done;
2392 error = got_worktree_revert(worktree, &paths,
2393 revert_progress, NULL, repo);
2394 if (error)
2395 goto done;
2396 done:
2397 if (repo)
2398 got_repo_close(repo);
2399 if (worktree)
2400 got_worktree_close(worktree);
2401 free(path);
2402 free(cwd);
2403 return error;
2406 __dead static void
2407 usage_commit(void)
2409 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2410 exit(1);
2413 int
2414 spawn_editor(const char *editor, const char *file)
2416 pid_t pid;
2417 sig_t sighup, sigint, sigquit;
2418 int st = -1;
2420 sighup = signal(SIGHUP, SIG_IGN);
2421 sigint = signal(SIGINT, SIG_IGN);
2422 sigquit = signal(SIGQUIT, SIG_IGN);
2424 switch (pid = fork()) {
2425 case -1:
2426 goto doneediting;
2427 case 0:
2428 execl(editor, editor, file, (char *)NULL);
2429 _exit(127);
2432 while (waitpid(pid, &st, 0) == -1)
2433 if (errno != EINTR)
2434 break;
2436 doneediting:
2437 (void)signal(SIGHUP, sighup);
2438 (void)signal(SIGINT, sigint);
2439 (void)signal(SIGQUIT, sigquit);
2441 if (!WIFEXITED(st)) {
2442 errno = EINTR;
2443 return -1;
2446 return WEXITSTATUS(st);
2449 struct collect_commit_logmsg_arg {
2450 const char *cmdline_log;
2451 const char *editor;
2452 const char *worktree_path;
2453 const char *branch_name;
2454 const char *repo_path;
2455 char *logmsg_path;
2459 static const struct got_error *
2460 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2461 void *arg)
2463 char *initial_content = NULL;
2464 struct got_pathlist_entry *pe;
2465 const struct got_error *err = NULL;
2466 char *template = NULL;
2467 struct collect_commit_logmsg_arg *a = arg;
2468 char buf[1024];
2469 struct stat st, st2;
2470 FILE *fp;
2471 size_t len;
2472 int fd, content_changed = 0;
2474 /* if a message was specified on the command line, just use it */
2475 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2476 len = strlen(a->cmdline_log) + 1;
2477 *logmsg = malloc(len + 1);
2478 if (*logmsg == NULL)
2479 return got_error_from_errno("malloc");
2480 strlcpy(*logmsg, a->cmdline_log, len);
2481 return NULL;
2484 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2485 return got_error_from_errno("asprintf");
2487 if (asprintf(&initial_content,
2488 "\n# changes to be committed on branch %s:\n",
2489 a->branch_name) == -1)
2490 return got_error_from_errno("asprintf");
2492 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2493 if (err)
2494 goto done;
2496 dprintf(fd, initial_content);
2498 TAILQ_FOREACH(pe, commitable_paths, entry) {
2499 struct got_commitable *ct = pe->data;
2500 dprintf(fd, "# %c %s\n",
2501 got_commitable_get_status(ct),
2502 got_commitable_get_path(ct));
2504 close(fd);
2506 if (stat(a->logmsg_path, &st) == -1) {
2507 err = got_error_from_errno2("stat", a->logmsg_path);
2508 goto done;
2511 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2512 err = got_error_from_errno("failed spawning editor");
2513 goto done;
2516 if (stat(a->logmsg_path, &st2) == -1) {
2517 err = got_error_from_errno("stat");
2518 goto done;
2521 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2522 unlink(a->logmsg_path);
2523 free(a->logmsg_path);
2524 a->logmsg_path = NULL;
2525 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2526 "no changes made to commit message, aborting");
2527 goto done;
2530 *logmsg = malloc(st2.st_size + 1);
2531 if (*logmsg == NULL) {
2532 err = got_error_from_errno("malloc");
2533 goto done;
2535 (*logmsg)[0] = '\0';
2536 len = 0;
2538 fp = fopen(a->logmsg_path, "r");
2539 if (fp == NULL) {
2540 err = got_error_from_errno("fopen");
2541 goto done;
2543 while (fgets(buf, sizeof(buf), fp) != NULL) {
2544 if (!content_changed && strcmp(buf, initial_content) != 0)
2545 content_changed = 1;
2546 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2547 continue; /* remove comments and leading empty lines */
2548 len = strlcat(*logmsg, buf, st2.st_size);
2550 fclose(fp);
2552 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2553 (*logmsg)[len - 1] = '\0';
2554 len--;
2557 if (len == 0 || !content_changed) {
2558 unlink(a->logmsg_path);
2559 free(a->logmsg_path);
2560 a->logmsg_path = NULL;
2561 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2562 "commit message cannot be empty, aborting");
2563 goto done;
2565 done:
2566 free(initial_content);
2567 free(template);
2569 /* Editor is done; we can now apply unveil(2) */
2570 if (err == NULL)
2571 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2572 return err;
2575 static const struct got_error *
2576 cmd_commit(int argc, char *argv[])
2578 const struct got_error *error = NULL;
2579 struct got_worktree *worktree = NULL;
2580 struct got_repository *repo = NULL;
2581 char *cwd = NULL, *path = NULL, *id_str = NULL;
2582 struct got_object_id *id = NULL;
2583 const char *logmsg = NULL;
2584 const char *got_author = getenv("GOT_AUTHOR");
2585 struct collect_commit_logmsg_arg cl_arg;
2586 char *editor = NULL;
2587 int ch;
2589 while ((ch = getopt(argc, argv, "m:")) != -1) {
2590 switch (ch) {
2591 case 'm':
2592 logmsg = optarg;
2593 break;
2594 default:
2595 usage_commit();
2596 /* NOTREACHED */
2600 argc -= optind;
2601 argv += optind;
2603 if (argc == 1) {
2604 path = realpath(argv[0], NULL);
2605 if (path == NULL) {
2606 error = got_error_from_errno2("realpath", argv[0]);
2607 goto done;
2609 got_path_strip_trailing_slashes(path);
2610 } else if (argc != 0)
2611 usage_commit();
2613 if (got_author == NULL) {
2614 /* TODO: Look current user up in password database */
2615 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2616 goto done;
2619 cwd = getcwd(NULL, 0);
2620 if (cwd == NULL) {
2621 error = got_error_from_errno("getcwd");
2622 goto done;
2624 error = got_worktree_open(&worktree, cwd);
2625 if (error)
2626 goto done;
2628 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2629 if (error != NULL)
2630 goto done;
2633 * unveil(2) traverses exec(2); if an editor is used we have
2634 * to apply unveil after the log message has been written.
2636 if (logmsg == NULL || strlen(logmsg) == 0)
2637 error = get_editor(&editor);
2638 else
2639 error = apply_unveil(got_repo_get_path(repo), 0,
2640 got_worktree_get_root_path(worktree), 0);
2641 if (error)
2642 goto done;
2644 cl_arg.editor = editor;
2645 cl_arg.cmdline_log = logmsg;
2646 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2647 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2648 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2649 cl_arg.branch_name += 5;
2650 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2651 cl_arg.branch_name += 6;
2652 cl_arg.repo_path = got_repo_get_path(repo);
2653 cl_arg.logmsg_path = NULL;
2654 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2655 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2656 if (error) {
2657 if (cl_arg.logmsg_path)
2658 fprintf(stderr, "%s: log message preserved in %s\n",
2659 getprogname(), cl_arg.logmsg_path);
2660 goto done;
2663 if (cl_arg.logmsg_path)
2664 unlink(cl_arg.logmsg_path);
2666 error = got_object_id_str(&id_str, id);
2667 if (error)
2668 goto done;
2669 printf("Created commit %s\n", id_str);
2670 done:
2671 if (repo)
2672 got_repo_close(repo);
2673 if (worktree)
2674 got_worktree_close(worktree);
2675 free(path);
2676 free(cwd);
2677 free(id_str);
2678 free(editor);
2679 return error;
2682 __dead static void
2683 usage_cherrypick(void)
2685 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2686 exit(1);
2689 static const struct got_error *
2690 cmd_cherrypick(int argc, char *argv[])
2692 const struct got_error *error = NULL;
2693 struct got_worktree *worktree = NULL;
2694 struct got_repository *repo = NULL;
2695 char *cwd = NULL, *commit_id_str = NULL;
2696 struct got_object_id *commit_id = NULL;
2697 struct got_commit_object *commit = NULL;
2698 struct got_object_qid *pid;
2699 struct got_reference *head_ref = NULL;
2700 int ch, did_something = 0;
2702 while ((ch = getopt(argc, argv, "")) != -1) {
2703 switch (ch) {
2704 default:
2705 usage_cherrypick();
2706 /* NOTREACHED */
2710 argc -= optind;
2711 argv += optind;
2713 if (argc != 1)
2714 usage_cherrypick();
2716 cwd = getcwd(NULL, 0);
2717 if (cwd == NULL) {
2718 error = got_error_from_errno("getcwd");
2719 goto done;
2721 error = got_worktree_open(&worktree, cwd);
2722 if (error)
2723 goto done;
2725 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2726 if (error != NULL)
2727 goto done;
2729 error = apply_unveil(got_repo_get_path(repo), 0,
2730 got_worktree_get_root_path(worktree), 0);
2731 if (error)
2732 goto done;
2734 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2735 if (error != NULL) {
2736 struct got_reference *ref;
2737 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2738 goto done;
2739 error = got_ref_open(&ref, repo, argv[0], 0);
2740 if (error != NULL)
2741 goto done;
2742 error = got_ref_resolve(&commit_id, repo, ref);
2743 got_ref_close(ref);
2744 if (error != NULL)
2745 goto done;
2747 error = got_object_id_str(&commit_id_str, commit_id);
2748 if (error)
2749 goto done;
2751 error = got_ref_open(&head_ref, repo,
2752 got_worktree_get_head_ref_name(worktree), 0);
2753 if (error != NULL)
2754 goto done;
2756 error = check_same_branch(commit_id, head_ref, repo);
2757 if (error) {
2758 if (error->code != GOT_ERR_ANCESTRY)
2759 goto done;
2760 error = NULL;
2761 } else {
2762 error = got_error(GOT_ERR_SAME_BRANCH);
2763 goto done;
2766 error = got_object_open_as_commit(&commit, repo, commit_id);
2767 if (error)
2768 goto done;
2769 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2770 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
2771 commit_id, repo, update_progress, &did_something, check_cancelled,
2772 NULL);
2773 if (error != NULL)
2774 goto done;
2776 if (did_something)
2777 printf("Merged commit %s\n", commit_id_str);
2778 done:
2779 if (commit)
2780 got_object_commit_close(commit);
2781 free(commit_id_str);
2782 if (head_ref)
2783 got_ref_close(head_ref);
2784 if (worktree)
2785 got_worktree_close(worktree);
2786 if (repo)
2787 got_repo_close(repo);
2788 return error;
2791 __dead static void
2792 usage_backout(void)
2794 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
2795 exit(1);
2798 static const struct got_error *
2799 cmd_backout(int argc, char *argv[])
2801 const struct got_error *error = NULL;
2802 struct got_worktree *worktree = NULL;
2803 struct got_repository *repo = NULL;
2804 char *cwd = NULL, *commit_id_str = NULL;
2805 struct got_object_id *commit_id = NULL;
2806 struct got_commit_object *commit = NULL;
2807 struct got_object_qid *pid;
2808 struct got_reference *head_ref = NULL;
2809 int ch, did_something = 0;
2811 while ((ch = getopt(argc, argv, "")) != -1) {
2812 switch (ch) {
2813 default:
2814 usage_backout();
2815 /* NOTREACHED */
2819 argc -= optind;
2820 argv += optind;
2822 if (argc != 1)
2823 usage_backout();
2825 cwd = getcwd(NULL, 0);
2826 if (cwd == NULL) {
2827 error = got_error_from_errno("getcwd");
2828 goto done;
2830 error = got_worktree_open(&worktree, cwd);
2831 if (error)
2832 goto done;
2834 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2835 if (error != NULL)
2836 goto done;
2838 error = apply_unveil(got_repo_get_path(repo), 0,
2839 got_worktree_get_root_path(worktree), 0);
2840 if (error)
2841 goto done;
2843 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2844 if (error != NULL) {
2845 struct got_reference *ref;
2846 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2847 goto done;
2848 error = got_ref_open(&ref, repo, argv[0], 0);
2849 if (error != NULL)
2850 goto done;
2851 error = got_ref_resolve(&commit_id, repo, ref);
2852 got_ref_close(ref);
2853 if (error != NULL)
2854 goto done;
2856 error = got_object_id_str(&commit_id_str, commit_id);
2857 if (error)
2858 goto done;
2860 error = got_ref_open(&head_ref, repo,
2861 got_worktree_get_head_ref_name(worktree), 0);
2862 if (error != NULL)
2863 goto done;
2865 error = check_same_branch(commit_id, head_ref, repo);
2866 if (error)
2867 goto done;
2869 error = got_object_open_as_commit(&commit, repo, commit_id);
2870 if (error)
2871 goto done;
2872 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2873 if (pid == NULL) {
2874 error = got_error(GOT_ERR_ROOT_COMMIT);
2875 goto done;
2878 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
2879 update_progress, &did_something, check_cancelled, NULL);
2880 if (error != NULL)
2881 goto done;
2883 if (did_something)
2884 printf("Backed out commit %s\n", commit_id_str);
2885 done:
2886 if (commit)
2887 got_object_commit_close(commit);
2888 free(commit_id_str);
2889 if (head_ref)
2890 got_ref_close(head_ref);
2891 if (worktree)
2892 got_worktree_close(worktree);
2893 if (repo)
2894 got_repo_close(repo);
2895 return error;