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 };
75 __dead static void usage(void);
76 __dead static void usage_init(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_init(int, char *[]);
93 static const struct got_error* cmd_checkout(int, char *[]);
94 static const struct got_error* cmd_update(int, char *[]);
95 static const struct got_error* cmd_log(int, char *[]);
96 static const struct got_error* cmd_diff(int, char *[]);
97 static const struct got_error* cmd_blame(int, char *[]);
98 static const struct got_error* cmd_tree(int, char *[]);
99 static const struct got_error* cmd_status(int, char *[]);
100 static const struct got_error* cmd_ref(int, char *[]);
101 static const struct got_error* cmd_add(int, char *[]);
102 static const struct got_error* cmd_rm(int, char *[]);
103 static const struct got_error* cmd_revert(int, char *[]);
104 static const struct got_error* cmd_commit(int, char *[]);
105 static const struct got_error* cmd_cherrypick(int, char *[]);
106 static const struct got_error* cmd_backout(int, char *[]);
108 static struct cmd got_commands[] = {
109 { "init", cmd_init, usage_init },
110 { "checkout", cmd_checkout, usage_checkout },
111 { "update", cmd_update, usage_update },
112 { "log", cmd_log, usage_log },
113 { "diff", cmd_diff, usage_diff },
114 { "blame", cmd_blame, usage_blame },
115 { "tree", cmd_tree, usage_tree },
116 { "status", cmd_status, usage_status },
117 { "ref", cmd_ref, usage_ref },
118 { "add", cmd_add, usage_add },
119 { "rm", cmd_rm, usage_rm },
120 { "revert", cmd_revert, usage_revert },
121 { "commit", cmd_commit, usage_commit },
122 { "cherrypick", cmd_cherrypick, usage_cherrypick },
123 { "backout", cmd_backout, usage_backout },
124 };
126 int
127 main(int argc, char *argv[])
129 struct cmd *cmd;
130 unsigned int i;
131 int ch;
132 int hflag = 0;
134 setlocale(LC_CTYPE, "");
136 while ((ch = getopt(argc, argv, "h")) != -1) {
137 switch (ch) {
138 case 'h':
139 hflag = 1;
140 break;
141 default:
142 usage();
143 /* NOTREACHED */
147 argc -= optind;
148 argv += optind;
149 optind = 0;
151 if (argc <= 0)
152 usage();
154 signal(SIGINT, catch_sigint);
155 signal(SIGPIPE, catch_sigpipe);
157 for (i = 0; i < nitems(got_commands); i++) {
158 const struct got_error *error;
160 cmd = &got_commands[i];
162 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
163 continue;
165 if (hflag)
166 got_commands[i].cmd_usage();
168 error = got_commands[i].cmd_main(argc, argv);
169 if (error && !(sigint_received || sigpipe_received)) {
170 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
171 return 1;
174 return 0;
177 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
178 return 1;
181 __dead static void
182 usage(void)
184 fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
185 exit(1);
188 static const struct got_error *
189 get_editor(char **abspath)
191 const struct got_error *err = NULL;
192 const char *editor;
194 editor = getenv("VISUAL");
195 if (editor == NULL)
196 editor = getenv("EDITOR");
198 if (editor) {
199 err = got_path_find_prog(abspath, editor);
200 if (err)
201 return err;
204 if (*abspath == NULL) {
205 *abspath = strdup("/bin/ed");
206 if (*abspath == NULL)
207 return got_error_from_errno("strdup");
210 return NULL;
213 static const struct got_error *
214 apply_unveil(const char *repo_path, int repo_read_only,
215 const char *worktree_path, int create_worktree)
217 const struct got_error *err;
219 if (create_worktree) {
220 /* Pre-create work tree path to avoid unveiling its parents. */
221 err = got_path_mkdir(worktree_path);
223 if (errno == EEXIST) {
224 if (got_path_dir_is_empty(worktree_path)) {
225 errno = 0;
226 err = NULL;
227 } else {
228 err = got_error_path(worktree_path,
229 GOT_ERR_DIR_NOT_EMPTY);
233 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
234 return err;
237 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
238 return got_error_from_errno2("unveil", repo_path);
240 if (worktree_path && unveil(worktree_path, "rwc") != 0)
241 return got_error_from_errno2("unveil", worktree_path);
243 if (unveil("/tmp", "rwc") != 0)
244 return got_error_from_errno2("unveil", "/tmp");
246 err = got_privsep_unveil_exec_helpers();
247 if (err != NULL)
248 return err;
250 if (unveil(NULL, NULL) != 0)
251 return got_error_from_errno("unveil");
253 return NULL;
256 __dead static void
257 usage_init(void)
259 fprintf(stderr, "usage: %s init path\n", getprogname());
260 exit(1);
263 static const struct got_error *
264 cmd_init(int argc, char *argv[])
266 const struct got_error *error = NULL;
267 char *repo_path = NULL;
268 int ch;
270 while ((ch = getopt(argc, argv, "")) != -1) {
271 switch (ch) {
272 default:
273 usage_init();
274 /* NOTREACHED */
278 argc -= optind;
279 argv += optind;
281 #ifndef PROFILE
282 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
283 err(1, "pledge");
284 #endif
285 if (argc != 1)
286 usage_init();
288 repo_path = strdup(argv[0]);
289 if (repo_path == NULL)
290 return got_error_from_errno("strdup");
292 got_path_strip_trailing_slashes(repo_path);
294 error = got_path_mkdir(repo_path);
295 if (error &&
296 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
297 goto done;
299 error = apply_unveil(repo_path, 0, NULL, 0);
300 if (error)
301 goto done;
303 error = got_repo_init(repo_path);
304 if (error != NULL)
305 goto done;
307 done:
308 free(repo_path);
309 return error;
312 __dead static void
313 usage_checkout(void)
315 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
316 "[-p prefix] repository-path [worktree-path]\n", getprogname());
317 exit(1);
320 static void
321 checkout_progress(void *arg, unsigned char status, const char *path)
323 char *worktree_path = arg;
325 /* Base commit bump happens silently. */
326 if (status == GOT_STATUS_BUMP_BASE)
327 return;
329 while (path[0] == '/')
330 path++;
332 printf("%c %s/%s\n", status, worktree_path, path);
335 static const struct got_error *
336 check_cancelled(void *arg)
338 if (sigint_received || sigpipe_received)
339 return got_error(GOT_ERR_CANCELLED);
340 return NULL;
343 static const struct got_error *
344 check_linear_ancestry(struct got_object_id *commit_id,
345 struct got_object_id *base_commit_id, struct got_repository *repo)
347 const struct got_error *err = NULL;
348 struct got_object_id *yca_id;
350 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
351 commit_id, base_commit_id, repo);
352 if (err)
353 return err;
355 if (yca_id == NULL)
356 return got_error(GOT_ERR_ANCESTRY);
358 /*
359 * Require a straight line of history between the target commit
360 * and the work tree's base commit.
362 * Non-linear situations such as this require a rebase:
364 * (commit) D F (base_commit)
365 * \ /
366 * C E
367 * \ /
368 * B (yca)
369 * |
370 * A
372 * 'got update' only handles linear cases:
373 * Update forwards in time: A (base/yca) - B - C - D (commit)
374 * Update backwards in time: D (base) - C - B - A (commit/yca)
375 */
376 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
377 got_object_id_cmp(base_commit_id, yca_id) != 0)
378 return got_error(GOT_ERR_ANCESTRY);
380 free(yca_id);
381 return NULL;
384 static const struct got_error *
385 check_same_branch(struct got_object_id *commit_id,
386 struct got_reference *head_ref, struct got_repository *repo)
388 const struct got_error *err = NULL;
389 struct got_commit_graph *graph = NULL;
390 struct got_object_id *head_commit_id = NULL;
391 int is_same_branch = 0;
393 err = got_ref_resolve(&head_commit_id, repo, head_ref);
394 if (err)
395 goto done;
397 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
398 if (err)
399 goto done;
401 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
402 if (err)
403 goto done;
405 for (;;) {
406 struct got_object_id *id;
407 err = got_commit_graph_iter_next(&id, graph);
408 if (err) {
409 if (err->code == GOT_ERR_ITER_COMPLETED) {
410 err = NULL;
411 break;
413 else if (err->code != GOT_ERR_ITER_NEED_MORE)
414 break;
415 err = got_commit_graph_fetch_commits(graph, 1,
416 repo);
417 if (err)
418 break;
421 if (id) {
422 if (got_object_id_cmp(id, commit_id) == 0) {
423 is_same_branch = 1;
424 break;
428 done:
429 if (graph)
430 got_commit_graph_close(graph);
431 free(head_commit_id);
432 if (!err && !is_same_branch)
433 err = got_error(GOT_ERR_ANCESTRY);
434 return err;
437 static const struct got_error *
438 cmd_checkout(int argc, char *argv[])
440 const struct got_error *error = NULL;
441 struct got_repository *repo = NULL;
442 struct got_reference *head_ref = NULL;
443 struct got_worktree *worktree = NULL;
444 char *repo_path = NULL;
445 char *worktree_path = NULL;
446 const char *path_prefix = "";
447 const char *branch_name = GOT_REF_HEAD;
448 char *commit_id_str = NULL;
449 int ch, same_path_prefix;
451 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
452 switch (ch) {
453 case 'b':
454 branch_name = optarg;
455 break;
456 case 'c':
457 commit_id_str = strdup(optarg);
458 if (commit_id_str == NULL)
459 return got_error_from_errno("strdup");
460 break;
461 case 'p':
462 path_prefix = optarg;
463 break;
464 default:
465 usage_checkout();
466 /* NOTREACHED */
470 argc -= optind;
471 argv += optind;
473 #ifndef PROFILE
474 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
475 "unveil", NULL) == -1)
476 err(1, "pledge");
477 #endif
478 if (argc == 1) {
479 char *cwd, *base, *dotgit;
480 repo_path = realpath(argv[0], NULL);
481 if (repo_path == NULL)
482 return got_error_from_errno2("realpath", argv[0]);
483 cwd = getcwd(NULL, 0);
484 if (cwd == NULL) {
485 error = got_error_from_errno("getcwd");
486 goto done;
488 if (path_prefix[0]) {
489 base = basename(path_prefix);
490 if (base == NULL) {
491 error = got_error_from_errno2("basename",
492 path_prefix);
493 goto done;
495 } else {
496 base = basename(repo_path);
497 if (base == NULL) {
498 error = got_error_from_errno2("basename",
499 repo_path);
500 goto done;
503 dotgit = strstr(base, ".git");
504 if (dotgit)
505 *dotgit = '\0';
506 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
507 error = got_error_from_errno("asprintf");
508 free(cwd);
509 goto done;
511 free(cwd);
512 } else if (argc == 2) {
513 repo_path = realpath(argv[0], NULL);
514 if (repo_path == NULL) {
515 error = got_error_from_errno2("realpath", argv[0]);
516 goto done;
518 worktree_path = realpath(argv[1], NULL);
519 if (worktree_path == NULL) {
520 error = got_error_from_errno2("realpath", argv[1]);
521 goto done;
523 } else
524 usage_checkout();
526 got_path_strip_trailing_slashes(repo_path);
527 got_path_strip_trailing_slashes(worktree_path);
529 error = got_repo_open(&repo, repo_path);
530 if (error != NULL)
531 goto done;
533 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
534 if (error)
535 goto done;
537 error = got_ref_open(&head_ref, repo, branch_name, 0);
538 if (error != NULL)
539 goto done;
541 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
542 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
543 goto done;
545 error = got_worktree_open(&worktree, worktree_path);
546 if (error != NULL)
547 goto done;
549 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
550 path_prefix);
551 if (error != NULL)
552 goto done;
553 if (!same_path_prefix) {
554 error = got_error(GOT_ERR_PATH_PREFIX);
555 goto done;
558 if (commit_id_str) {
559 struct got_object_id *commit_id;
560 error = got_object_resolve_id_str(&commit_id, repo,
561 commit_id_str);
562 if (error != NULL)
563 goto done;
564 error = check_linear_ancestry(commit_id,
565 got_worktree_get_base_commit_id(worktree), repo);
566 if (error != NULL) {
567 free(commit_id);
568 goto done;
570 error = check_same_branch(commit_id, head_ref, repo);
571 if (error)
572 goto done;
573 error = got_worktree_set_base_commit_id(worktree, repo,
574 commit_id);
575 free(commit_id);
576 if (error)
577 goto done;
580 error = got_worktree_checkout_files(worktree, "", repo,
581 checkout_progress, worktree_path, check_cancelled, NULL);
582 if (error != NULL)
583 goto done;
585 printf("Now shut up and hack\n");
587 done:
588 free(commit_id_str);
589 free(repo_path);
590 free(worktree_path);
591 return error;
594 __dead static void
595 usage_update(void)
597 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
598 getprogname());
599 exit(1);
602 static void
603 update_progress(void *arg, unsigned char status, const char *path)
605 int *did_something = arg;
607 if (status == GOT_STATUS_EXISTS)
608 return;
610 *did_something = 1;
612 /* Base commit bump happens silently. */
613 if (status == GOT_STATUS_BUMP_BASE)
614 return;
616 while (path[0] == '/')
617 path++;
618 printf("%c %s\n", status, path);
621 static const struct got_error *
622 switch_head_ref(struct got_reference *head_ref,
623 struct got_object_id *commit_id, struct got_worktree *worktree,
624 struct got_repository *repo)
626 const struct got_error *err = NULL;
627 char *base_id_str;
628 int ref_has_moved = 0;
630 /* Trivial case: switching between two different references. */
631 if (strcmp(got_ref_get_name(head_ref),
632 got_worktree_get_head_ref_name(worktree)) != 0) {
633 printf("Switching work tree from %s to %s\n",
634 got_worktree_get_head_ref_name(worktree),
635 got_ref_get_name(head_ref));
636 return got_worktree_set_head_ref(worktree, head_ref);
639 err = check_linear_ancestry(commit_id,
640 got_worktree_get_base_commit_id(worktree), repo);
641 if (err) {
642 if (err->code != GOT_ERR_ANCESTRY)
643 return err;
644 ref_has_moved = 1;
646 if (!ref_has_moved)
647 return NULL;
649 /* Switching to a rebased branch with the same reference name. */
650 err = got_object_id_str(&base_id_str,
651 got_worktree_get_base_commit_id(worktree));
652 if (err)
653 return err;
654 printf("Reference %s now points at a different branch\n",
655 got_worktree_get_head_ref_name(worktree));
656 printf("Switching work tree from %s to %s\n", base_id_str,
657 got_worktree_get_head_ref_name(worktree));
658 return NULL;
661 static const struct got_error *
662 cmd_update(int argc, char *argv[])
664 const struct got_error *error = NULL;
665 struct got_repository *repo = NULL;
666 struct got_worktree *worktree = NULL;
667 char *worktree_path = NULL, *path = NULL;
668 struct got_object_id *commit_id = NULL;
669 char *commit_id_str = NULL;
670 const char *branch_name = NULL;
671 struct got_reference *head_ref = NULL;
672 int ch, did_something = 0;
674 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
675 switch (ch) {
676 case 'b':
677 branch_name = optarg;
678 break;
679 case 'c':
680 commit_id_str = strdup(optarg);
681 if (commit_id_str == NULL)
682 return got_error_from_errno("strdup");
683 break;
684 default:
685 usage_update();
686 /* NOTREACHED */
690 argc -= optind;
691 argv += optind;
693 #ifndef PROFILE
694 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
695 "unveil", NULL) == -1)
696 err(1, "pledge");
697 #endif
698 worktree_path = getcwd(NULL, 0);
699 if (worktree_path == NULL) {
700 error = got_error_from_errno("getcwd");
701 goto done;
703 error = got_worktree_open(&worktree, worktree_path);
704 if (error)
705 goto done;
707 if (argc == 0) {
708 path = strdup("");
709 if (path == NULL) {
710 error = got_error_from_errno("strdup");
711 goto done;
713 } else if (argc == 1) {
714 error = got_worktree_resolve_path(&path, worktree, argv[0]);
715 if (error)
716 goto done;
717 } else
718 usage_update();
720 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
721 if (error != NULL)
722 goto done;
724 error = apply_unveil(got_repo_get_path(repo), 0,
725 got_worktree_get_root_path(worktree), 0);
726 if (error)
727 goto done;
729 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
730 got_worktree_get_head_ref_name(worktree), 0);
731 if (error != NULL)
732 goto done;
733 if (commit_id_str == NULL) {
734 error = got_ref_resolve(&commit_id, repo, head_ref);
735 if (error != NULL)
736 goto done;
737 error = got_object_id_str(&commit_id_str, commit_id);
738 if (error != NULL)
739 goto done;
740 } else {
741 error = got_object_resolve_id_str(&commit_id, repo,
742 commit_id_str);
743 if (error != NULL)
744 goto done;
747 if (branch_name) {
748 struct got_object_id *head_commit_id;
749 if (strlen(path) != 0) {
750 fprintf(stderr, "%s: switching between branches "
751 "requires that the entire work tree "
752 "gets updated, not just '%s'\n",
753 getprogname(), path);
754 error = got_error(GOT_ERR_BAD_PATH);
755 goto done;
757 error = got_ref_resolve(&head_commit_id, repo, head_ref);
758 if (error)
759 goto done;
760 error = check_linear_ancestry(commit_id, head_commit_id, repo);
761 free(head_commit_id);
762 if (error != NULL)
763 goto done;
764 error = check_same_branch(commit_id, head_ref, repo);
765 if (error)
766 goto done;
767 error = switch_head_ref(head_ref, commit_id, worktree, repo);
768 if (error)
769 goto done;
770 } else {
771 error = check_linear_ancestry(commit_id,
772 got_worktree_get_base_commit_id(worktree), repo);
773 if (error != NULL) {
774 if (error->code == GOT_ERR_ANCESTRY)
775 error = got_error(GOT_ERR_BRANCH_MOVED);
776 goto done;
778 error = check_same_branch(commit_id, head_ref, repo);
779 if (error)
780 goto done;
783 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
784 commit_id) != 0) {
785 error = got_worktree_set_base_commit_id(worktree, repo,
786 commit_id);
787 if (error)
788 goto done;
791 error = got_worktree_checkout_files(worktree, path, repo,
792 update_progress, &did_something, check_cancelled, NULL);
793 if (error != NULL)
794 goto done;
796 if (did_something)
797 printf("Updated to commit %s\n", commit_id_str);
798 else
799 printf("Already up-to-date\n");
800 done:
801 free(worktree_path);
802 free(path);
803 free(commit_id);
804 free(commit_id_str);
805 return error;
808 static const struct got_error *
809 print_patch(struct got_commit_object *commit, struct got_object_id *id,
810 int diff_context, struct got_repository *repo)
812 const struct got_error *err = NULL;
813 struct got_tree_object *tree1 = NULL, *tree2;
814 struct got_object_qid *qid;
815 char *id_str1 = NULL, *id_str2;
816 struct got_diff_blob_output_unidiff_arg arg;
818 err = got_object_open_as_tree(&tree2, repo,
819 got_object_commit_get_tree_id(commit));
820 if (err)
821 return err;
823 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
824 if (qid != NULL) {
825 struct got_commit_object *pcommit;
827 err = got_object_open_as_commit(&pcommit, repo, qid->id);
828 if (err)
829 return err;
831 err = got_object_open_as_tree(&tree1, repo,
832 got_object_commit_get_tree_id(pcommit));
833 got_object_commit_close(pcommit);
834 if (err)
835 return err;
837 err = got_object_id_str(&id_str1, qid->id);
838 if (err)
839 return err;
842 err = got_object_id_str(&id_str2, id);
843 if (err)
844 goto done;
846 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
847 arg.diff_context = diff_context;
848 arg.outfile = stdout;
849 err = got_diff_tree(tree1, tree2, "", "", repo,
850 got_diff_blob_output_unidiff, &arg);
851 done:
852 if (tree1)
853 got_object_tree_close(tree1);
854 got_object_tree_close(tree2);
855 free(id_str1);
856 free(id_str2);
857 return err;
860 static char *
861 get_datestr(time_t *time, char *datebuf)
863 char *p, *s = ctime_r(time, datebuf);
864 p = strchr(s, '\n');
865 if (p)
866 *p = '\0';
867 return s;
870 static const struct got_error *
871 print_commit(struct got_commit_object *commit, struct got_object_id *id,
872 struct got_repository *repo, int show_patch, int diff_context,
873 struct got_reflist_head *refs)
875 const struct got_error *err = NULL;
876 char *id_str, *datestr, *logmsg0, *logmsg, *line;
877 char datebuf[26];
878 time_t committer_time;
879 const char *author, *committer;
880 char *refs_str = NULL;
881 struct got_reflist_entry *re;
883 SIMPLEQ_FOREACH(re, refs, entry) {
884 char *s;
885 const char *name;
886 if (got_object_id_cmp(re->id, id) != 0)
887 continue;
888 name = got_ref_get_name(re->ref);
889 if (strcmp(name, GOT_REF_HEAD) == 0)
890 continue;
891 if (strncmp(name, "refs/", 5) == 0)
892 name += 5;
893 if (strncmp(name, "got/", 4) == 0)
894 continue;
895 if (strncmp(name, "heads/", 6) == 0)
896 name += 6;
897 if (strncmp(name, "remotes/", 8) == 0)
898 name += 8;
899 s = refs_str;
900 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
901 name) == -1) {
902 err = got_error_from_errno("asprintf");
903 free(s);
904 break;
906 free(s);
908 err = got_object_id_str(&id_str, id);
909 if (err)
910 return err;
912 printf("-----------------------------------------------\n");
913 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
914 refs_str ? refs_str : "", refs_str ? ")" : "");
915 free(id_str);
916 id_str = NULL;
917 free(refs_str);
918 refs_str = NULL;
919 printf("from: %s\n", got_object_commit_get_author(commit));
920 committer_time = got_object_commit_get_committer_time(commit);
921 datestr = get_datestr(&committer_time, datebuf);
922 printf("date: %s UTC\n", datestr);
923 author = got_object_commit_get_author(commit);
924 committer = got_object_commit_get_committer(commit);
925 if (strcmp(author, committer) != 0)
926 printf("via: %s\n", committer);
927 if (got_object_commit_get_nparents(commit) > 1) {
928 const struct got_object_id_queue *parent_ids;
929 struct got_object_qid *qid;
930 int n = 1;
931 parent_ids = got_object_commit_get_parent_ids(commit);
932 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
933 err = got_object_id_str(&id_str, qid->id);
934 if (err)
935 return err;
936 printf("parent %d: %s\n", n++, id_str);
937 free(id_str);
941 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
942 if (logmsg0 == NULL)
943 return got_error_from_errno("strdup");
945 logmsg = logmsg0;
946 do {
947 line = strsep(&logmsg, "\n");
948 if (line)
949 printf(" %s\n", line);
950 } while (line);
951 free(logmsg0);
953 if (show_patch) {
954 err = print_patch(commit, id, diff_context, repo);
955 if (err == 0)
956 printf("\n");
959 if (fflush(stdout) != 0 && err == NULL)
960 err = got_error_from_errno("fflush");
961 return err;
964 static const struct got_error *
965 print_commits(struct got_object_id *root_id, struct got_repository *repo,
966 char *path, int show_patch, int diff_context, int limit,
967 int first_parent_traversal, struct got_reflist_head *refs)
969 const struct got_error *err;
970 struct got_commit_graph *graph;
972 err = got_commit_graph_open(&graph, root_id, path,
973 first_parent_traversal, repo);
974 if (err)
975 return err;
976 err = got_commit_graph_iter_start(graph, root_id, repo);
977 if (err)
978 goto done;
979 for (;;) {
980 struct got_commit_object *commit;
981 struct got_object_id *id;
983 if (sigint_received || sigpipe_received)
984 break;
986 err = got_commit_graph_iter_next(&id, graph);
987 if (err) {
988 if (err->code == GOT_ERR_ITER_COMPLETED) {
989 err = NULL;
990 break;
992 if (err->code != GOT_ERR_ITER_NEED_MORE)
993 break;
994 err = got_commit_graph_fetch_commits(graph, 1, repo);
995 if (err)
996 break;
997 else
998 continue;
1000 if (id == NULL)
1001 break;
1003 err = got_object_open_as_commit(&commit, repo, id);
1004 if (err)
1005 break;
1006 err = print_commit(commit, id, repo, show_patch, diff_context,
1007 refs);
1008 got_object_commit_close(commit);
1009 if (err || (limit && --limit == 0))
1010 break;
1012 done:
1013 got_commit_graph_close(graph);
1014 return err;
1017 __dead static void
1018 usage_log(void)
1020 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1021 "[-r repository-path] [path]\n", getprogname());
1022 exit(1);
1025 static const struct got_error *
1026 cmd_log(int argc, char *argv[])
1028 const struct got_error *error;
1029 struct got_repository *repo = NULL;
1030 struct got_worktree *worktree = NULL;
1031 struct got_commit_object *commit = NULL;
1032 struct got_object_id *id = NULL;
1033 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1034 char *start_commit = NULL;
1035 int diff_context = 3, ch;
1036 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1037 const char *errstr;
1038 struct got_reflist_head refs;
1040 SIMPLEQ_INIT(&refs);
1042 #ifndef PROFILE
1043 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1044 NULL)
1045 == -1)
1046 err(1, "pledge");
1047 #endif
1049 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1050 switch (ch) {
1051 case 'b':
1052 first_parent_traversal = 1;
1053 break;
1054 case 'p':
1055 show_patch = 1;
1056 break;
1057 case 'c':
1058 start_commit = optarg;
1059 break;
1060 case 'C':
1061 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1062 &errstr);
1063 if (errstr != NULL)
1064 err(1, "-C option %s", errstr);
1065 break;
1066 case 'l':
1067 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1068 if (errstr != NULL)
1069 err(1, "-l option %s", errstr);
1070 break;
1071 case 'r':
1072 repo_path = realpath(optarg, NULL);
1073 if (repo_path == NULL)
1074 err(1, "-r option");
1075 got_path_strip_trailing_slashes(repo_path);
1076 break;
1077 default:
1078 usage_log();
1079 /* NOTREACHED */
1083 argc -= optind;
1084 argv += optind;
1086 cwd = getcwd(NULL, 0);
1087 if (cwd == NULL) {
1088 error = got_error_from_errno("getcwd");
1089 goto done;
1092 error = got_worktree_open(&worktree, cwd);
1093 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1094 goto done;
1095 error = NULL;
1097 if (argc == 0) {
1098 path = strdup("");
1099 if (path == NULL) {
1100 error = got_error_from_errno("strdup");
1101 goto done;
1103 } else if (argc == 1) {
1104 if (worktree) {
1105 error = got_worktree_resolve_path(&path, worktree,
1106 argv[0]);
1107 if (error)
1108 goto done;
1109 } else {
1110 path = strdup(argv[0]);
1111 if (path == NULL) {
1112 error = got_error_from_errno("strdup");
1113 goto done;
1116 } else
1117 usage_log();
1119 if (repo_path == NULL) {
1120 repo_path = worktree ?
1121 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1123 if (repo_path == NULL) {
1124 error = got_error_from_errno("strdup");
1125 goto done;
1128 error = got_repo_open(&repo, repo_path);
1129 if (error != NULL)
1130 goto done;
1132 error = apply_unveil(got_repo_get_path(repo), 1,
1133 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1134 if (error)
1135 goto done;
1137 if (start_commit == NULL) {
1138 struct got_reference *head_ref;
1139 error = got_ref_open(&head_ref, repo,
1140 worktree ? got_worktree_get_head_ref_name(worktree)
1141 : GOT_REF_HEAD, 0);
1142 if (error != NULL)
1143 return error;
1144 error = got_ref_resolve(&id, repo, head_ref);
1145 got_ref_close(head_ref);
1146 if (error != NULL)
1147 return error;
1148 error = got_object_open_as_commit(&commit, repo, id);
1149 } else {
1150 struct got_reference *ref;
1151 error = got_ref_open(&ref, repo, start_commit, 0);
1152 if (error == NULL) {
1153 int obj_type;
1154 error = got_ref_resolve(&id, repo, ref);
1155 got_ref_close(ref);
1156 if (error != NULL)
1157 goto done;
1158 error = got_object_get_type(&obj_type, repo, id);
1159 if (error != NULL)
1160 goto done;
1161 if (obj_type == GOT_OBJ_TYPE_TAG) {
1162 struct got_tag_object *tag;
1163 error = got_object_open_as_tag(&tag, repo, id);
1164 if (error != NULL)
1165 goto done;
1166 if (got_object_tag_get_object_type(tag) !=
1167 GOT_OBJ_TYPE_COMMIT) {
1168 got_object_tag_close(tag);
1169 error = got_error(GOT_ERR_OBJ_TYPE);
1170 goto done;
1172 free(id);
1173 id = got_object_id_dup(
1174 got_object_tag_get_object_id(tag));
1175 if (id == NULL)
1176 error = got_error_from_errno(
1177 "got_object_id_dup");
1178 got_object_tag_close(tag);
1179 if (error)
1180 goto done;
1181 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1182 error = got_error(GOT_ERR_OBJ_TYPE);
1183 goto done;
1185 error = got_object_open_as_commit(&commit, repo, id);
1186 if (error != NULL)
1187 goto done;
1189 if (commit == NULL) {
1190 error = got_object_resolve_id_str(&id, repo,
1191 start_commit);
1192 if (error != NULL)
1193 return error;
1196 if (error != NULL)
1197 goto done;
1199 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1200 if (error != NULL)
1201 goto done;
1202 if (in_repo_path) {
1203 free(path);
1204 path = in_repo_path;
1207 error = got_ref_list(&refs, repo);
1208 if (error)
1209 goto done;
1211 error = print_commits(id, repo, path, show_patch,
1212 diff_context, limit, first_parent_traversal, &refs);
1213 done:
1214 free(path);
1215 free(repo_path);
1216 free(cwd);
1217 free(id);
1218 if (worktree)
1219 got_worktree_close(worktree);
1220 if (repo) {
1221 const struct got_error *repo_error;
1222 repo_error = got_repo_close(repo);
1223 if (error == NULL)
1224 error = repo_error;
1226 got_ref_list_free(&refs);
1227 return error;
1230 __dead static void
1231 usage_diff(void)
1233 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1234 "[object1 object2 | path]\n", getprogname());
1235 exit(1);
1238 struct print_diff_arg {
1239 struct got_repository *repo;
1240 struct got_worktree *worktree;
1241 int diff_context;
1242 const char *id_str;
1243 int header_shown;
1246 static const struct got_error *
1247 print_diff(void *arg, unsigned char status, const char *path,
1248 struct got_object_id *blob_id, struct got_object_id *commit_id)
1250 struct print_diff_arg *a = arg;
1251 const struct got_error *err = NULL;
1252 struct got_blob_object *blob1 = NULL;
1253 FILE *f2 = NULL;
1254 char *abspath = NULL;
1255 struct stat sb;
1257 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1258 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1259 return NULL;
1261 if (!a->header_shown) {
1262 printf("diff %s %s\n", a->id_str,
1263 got_worktree_get_root_path(a->worktree));
1264 a->header_shown = 1;
1267 if (status != GOT_STATUS_ADD) {
1268 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1269 if (err)
1270 goto done;
1274 if (status != GOT_STATUS_DELETE) {
1275 if (asprintf(&abspath, "%s/%s",
1276 got_worktree_get_root_path(a->worktree), path) == -1) {
1277 err = got_error_from_errno("asprintf");
1278 goto done;
1281 f2 = fopen(abspath, "r");
1282 if (f2 == NULL) {
1283 err = got_error_from_errno2("fopen", abspath);
1284 goto done;
1286 if (lstat(abspath, &sb) == -1) {
1287 err = got_error_from_errno2("lstat", abspath);
1288 goto done;
1290 } else
1291 sb.st_size = 0;
1293 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1294 stdout);
1295 done:
1296 if (blob1)
1297 got_object_blob_close(blob1);
1298 if (f2 && fclose(f2) != 0 && err == NULL)
1299 err = got_error_from_errno("fclose");
1300 free(abspath);
1301 return err;
1304 static const struct got_error *
1305 cmd_diff(int argc, char *argv[])
1307 const struct got_error *error;
1308 struct got_repository *repo = NULL;
1309 struct got_worktree *worktree = NULL;
1310 char *cwd = NULL, *repo_path = NULL;
1311 struct got_object_id *id1 = NULL, *id2 = NULL;
1312 const char *id_str1 = NULL, *id_str2 = NULL;
1313 char *label1 = NULL, *label2 = NULL;
1314 int type1, type2;
1315 int diff_context = 3, ch;
1316 const char *errstr;
1317 char *path = NULL;
1319 #ifndef PROFILE
1320 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1321 NULL) == -1)
1322 err(1, "pledge");
1323 #endif
1325 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1326 switch (ch) {
1327 case 'C':
1328 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1329 if (errstr != NULL)
1330 err(1, "-C option %s", errstr);
1331 break;
1332 case 'r':
1333 repo_path = realpath(optarg, NULL);
1334 if (repo_path == NULL)
1335 err(1, "-r option");
1336 got_path_strip_trailing_slashes(repo_path);
1337 break;
1338 default:
1339 usage_diff();
1340 /* NOTREACHED */
1344 argc -= optind;
1345 argv += optind;
1347 cwd = getcwd(NULL, 0);
1348 if (cwd == NULL) {
1349 error = got_error_from_errno("getcwd");
1350 goto done;
1352 error = got_worktree_open(&worktree, cwd);
1353 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1354 goto done;
1355 if (argc <= 1) {
1356 if (worktree == NULL) {
1357 error = got_error(GOT_ERR_NOT_WORKTREE);
1358 goto done;
1360 if (repo_path)
1361 errx(1,
1362 "-r option can't be used when diffing a work tree");
1363 repo_path = strdup(got_worktree_get_repo_path(worktree));
1364 if (repo_path == NULL) {
1365 error = got_error_from_errno("strdup");
1366 goto done;
1368 if (argc == 1) {
1369 error = got_worktree_resolve_path(&path, worktree,
1370 argv[0]);
1371 if (error)
1372 goto done;
1373 } else {
1374 path = strdup("");
1375 if (path == NULL) {
1376 error = got_error_from_errno("strdup");
1377 goto done;
1380 } else if (argc == 2) {
1381 id_str1 = argv[0];
1382 id_str2 = argv[1];
1383 if (worktree && repo_path == NULL) {
1384 repo_path =
1385 strdup(got_worktree_get_repo_path(worktree));
1386 if (repo_path == NULL) {
1387 error = got_error_from_errno("strdup");
1388 goto done;
1391 } else
1392 usage_diff();
1394 if (repo_path == NULL) {
1395 repo_path = getcwd(NULL, 0);
1396 if (repo_path == NULL)
1397 return got_error_from_errno("getcwd");
1400 error = got_repo_open(&repo, repo_path);
1401 free(repo_path);
1402 if (error != NULL)
1403 goto done;
1405 error = apply_unveil(got_repo_get_path(repo), 1,
1406 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1407 if (error)
1408 goto done;
1410 if (argc <= 1) {
1411 struct print_diff_arg arg;
1412 char *id_str;
1413 error = got_object_id_str(&id_str,
1414 got_worktree_get_base_commit_id(worktree));
1415 if (error)
1416 goto done;
1417 arg.repo = repo;
1418 arg.worktree = worktree;
1419 arg.diff_context = diff_context;
1420 arg.id_str = id_str;
1421 arg.header_shown = 0;
1423 error = got_worktree_status(worktree, path, repo, print_diff,
1424 &arg, check_cancelled, NULL);
1425 free(id_str);
1426 goto done;
1429 error = got_object_resolve_id_str(&id1, repo, id_str1);
1430 if (error) {
1431 struct got_reference *ref;
1432 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1433 goto done;
1434 error = got_ref_open(&ref, repo, id_str1, 0);
1435 if (error != NULL)
1436 goto done;
1437 label1 = strdup(got_ref_get_name(ref));
1438 if (label1 == NULL) {
1439 error = got_error_from_errno("strdup");
1440 goto done;
1442 error = got_ref_resolve(&id1, repo, ref);
1443 got_ref_close(ref);
1444 if (error != NULL)
1445 goto done;
1446 } else {
1447 label1 = strdup(id_str1);
1448 if (label1 == NULL) {
1449 error = got_error_from_errno("strdup");
1450 goto done;
1454 error = got_object_resolve_id_str(&id2, repo, id_str2);
1455 if (error) {
1456 struct got_reference *ref;
1457 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1458 goto done;
1459 error = got_ref_open(&ref, repo, id_str2, 0);
1460 if (error != NULL)
1461 goto done;
1462 label2 = strdup(got_ref_get_name(ref));
1463 if (label2 == NULL) {
1464 error = got_error_from_errno("strdup");
1465 goto done;
1467 error = got_ref_resolve(&id2, repo, ref);
1468 got_ref_close(ref);
1469 if (error != NULL)
1470 goto done;
1471 } else {
1472 label2 = strdup(id_str2);
1473 if (label2 == NULL) {
1474 error = got_error_from_errno("strdup");
1475 goto done;
1479 error = got_object_get_type(&type1, repo, id1);
1480 if (error)
1481 goto done;
1483 error = got_object_get_type(&type2, repo, id2);
1484 if (error)
1485 goto done;
1487 if (type1 != type2) {
1488 error = got_error(GOT_ERR_OBJ_TYPE);
1489 goto done;
1492 switch (type1) {
1493 case GOT_OBJ_TYPE_BLOB:
1494 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1495 diff_context, repo, stdout);
1496 break;
1497 case GOT_OBJ_TYPE_TREE:
1498 error = got_diff_objects_as_trees(id1, id2, "", "",
1499 diff_context, repo, stdout);
1500 break;
1501 case GOT_OBJ_TYPE_COMMIT:
1502 printf("diff %s %s\n", label1, label2);
1503 error = got_diff_objects_as_commits(id1, id2, diff_context,
1504 repo, stdout);
1505 break;
1506 default:
1507 error = got_error(GOT_ERR_OBJ_TYPE);
1510 done:
1511 free(label1);
1512 free(label2);
1513 free(id1);
1514 free(id2);
1515 free(path);
1516 if (worktree)
1517 got_worktree_close(worktree);
1518 if (repo) {
1519 const struct got_error *repo_error;
1520 repo_error = got_repo_close(repo);
1521 if (error == NULL)
1522 error = repo_error;
1524 return error;
1527 __dead static void
1528 usage_blame(void)
1530 fprintf(stderr,
1531 "usage: %s blame [-c commit] [-r repository-path] path\n",
1532 getprogname());
1533 exit(1);
1536 static const struct got_error *
1537 cmd_blame(int argc, char *argv[])
1539 const struct got_error *error;
1540 struct got_repository *repo = NULL;
1541 struct got_worktree *worktree = NULL;
1542 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1543 struct got_object_id *commit_id = NULL;
1544 char *commit_id_str = NULL;
1545 int ch;
1547 #ifndef PROFILE
1548 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1549 NULL) == -1)
1550 err(1, "pledge");
1551 #endif
1553 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1554 switch (ch) {
1555 case 'c':
1556 commit_id_str = optarg;
1557 break;
1558 case 'r':
1559 repo_path = realpath(optarg, NULL);
1560 if (repo_path == NULL)
1561 err(1, "-r option");
1562 got_path_strip_trailing_slashes(repo_path);
1563 break;
1564 default:
1565 usage_blame();
1566 /* NOTREACHED */
1570 argc -= optind;
1571 argv += optind;
1573 if (argc == 1)
1574 path = argv[0];
1575 else
1576 usage_blame();
1578 cwd = getcwd(NULL, 0);
1579 if (cwd == NULL) {
1580 error = got_error_from_errno("getcwd");
1581 goto done;
1583 if (repo_path == NULL) {
1584 error = got_worktree_open(&worktree, cwd);
1585 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1586 goto done;
1587 else
1588 error = NULL;
1589 if (worktree) {
1590 repo_path =
1591 strdup(got_worktree_get_repo_path(worktree));
1592 if (repo_path == NULL)
1593 error = got_error_from_errno("strdup");
1594 if (error)
1595 goto done;
1596 } else {
1597 repo_path = strdup(cwd);
1598 if (repo_path == NULL) {
1599 error = got_error_from_errno("strdup");
1600 goto done;
1605 error = got_repo_open(&repo, repo_path);
1606 if (error != NULL)
1607 goto done;
1609 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1610 if (error)
1611 goto done;
1613 if (worktree) {
1614 const char *prefix = got_worktree_get_path_prefix(worktree);
1615 char *p, *worktree_subdir = cwd +
1616 strlen(got_worktree_get_root_path(worktree));
1617 if (asprintf(&p, "%s%s%s%s%s",
1618 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1619 worktree_subdir, worktree_subdir[0] ? "/" : "",
1620 path) == -1) {
1621 error = got_error_from_errno("asprintf");
1622 goto done;
1624 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1625 free(p);
1626 } else {
1627 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1629 if (error)
1630 goto done;
1632 if (commit_id_str == NULL) {
1633 struct got_reference *head_ref;
1634 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1635 if (error != NULL)
1636 goto done;
1637 error = got_ref_resolve(&commit_id, repo, head_ref);
1638 got_ref_close(head_ref);
1639 if (error != NULL)
1640 goto done;
1641 } else {
1642 error = got_object_resolve_id_str(&commit_id, repo,
1643 commit_id_str);
1644 if (error != NULL)
1645 goto done;
1648 error = got_blame(in_repo_path, commit_id, repo, stdout);
1649 done:
1650 free(in_repo_path);
1651 free(repo_path);
1652 free(cwd);
1653 free(commit_id);
1654 if (worktree)
1655 got_worktree_close(worktree);
1656 if (repo) {
1657 const struct got_error *repo_error;
1658 repo_error = got_repo_close(repo);
1659 if (error == NULL)
1660 error = repo_error;
1662 return error;
1665 __dead static void
1666 usage_tree(void)
1668 fprintf(stderr,
1669 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1670 getprogname());
1671 exit(1);
1674 static void
1675 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1676 const char *root_path)
1678 int is_root_path = (strcmp(path, root_path) == 0);
1680 path += strlen(root_path);
1681 while (path[0] == '/')
1682 path++;
1684 printf("%s%s%s%s%s\n", id ? id : "", path,
1685 is_root_path ? "" : "/", te->name,
1686 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1689 static const struct got_error *
1690 print_tree(const char *path, struct got_object_id *commit_id,
1691 int show_ids, int recurse, const char *root_path,
1692 struct got_repository *repo)
1694 const struct got_error *err = NULL;
1695 struct got_object_id *tree_id = NULL;
1696 struct got_tree_object *tree = NULL;
1697 const struct got_tree_entries *entries;
1698 struct got_tree_entry *te;
1700 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1701 if (err)
1702 goto done;
1704 err = got_object_open_as_tree(&tree, repo, tree_id);
1705 if (err)
1706 goto done;
1707 entries = got_object_tree_get_entries(tree);
1708 te = SIMPLEQ_FIRST(&entries->head);
1709 while (te) {
1710 char *id = NULL;
1712 if (sigint_received || sigpipe_received)
1713 break;
1715 if (show_ids) {
1716 char *id_str;
1717 err = got_object_id_str(&id_str, te->id);
1718 if (err)
1719 goto done;
1720 if (asprintf(&id, "%s ", id_str) == -1) {
1721 err = got_error_from_errno("asprintf");
1722 free(id_str);
1723 goto done;
1725 free(id_str);
1727 print_entry(te, id, path, root_path);
1728 free(id);
1730 if (recurse && S_ISDIR(te->mode)) {
1731 char *child_path;
1732 if (asprintf(&child_path, "%s%s%s", path,
1733 path[0] == '/' && path[1] == '\0' ? "" : "/",
1734 te->name) == -1) {
1735 err = got_error_from_errno("asprintf");
1736 goto done;
1738 err = print_tree(child_path, commit_id, show_ids, 1,
1739 root_path, repo);
1740 free(child_path);
1741 if (err)
1742 goto done;
1745 te = SIMPLEQ_NEXT(te, entry);
1747 done:
1748 if (tree)
1749 got_object_tree_close(tree);
1750 free(tree_id);
1751 return err;
1754 static const struct got_error *
1755 cmd_tree(int argc, char *argv[])
1757 const struct got_error *error;
1758 struct got_repository *repo = NULL;
1759 struct got_worktree *worktree = NULL;
1760 const char *path;
1761 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1762 struct got_object_id *commit_id = NULL;
1763 char *commit_id_str = NULL;
1764 int show_ids = 0, recurse = 0;
1765 int ch;
1767 #ifndef PROFILE
1768 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1769 NULL) == -1)
1770 err(1, "pledge");
1771 #endif
1773 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1774 switch (ch) {
1775 case 'c':
1776 commit_id_str = optarg;
1777 break;
1778 case 'r':
1779 repo_path = realpath(optarg, NULL);
1780 if (repo_path == NULL)
1781 err(1, "-r option");
1782 got_path_strip_trailing_slashes(repo_path);
1783 break;
1784 case 'i':
1785 show_ids = 1;
1786 break;
1787 case 'R':
1788 recurse = 1;
1789 break;
1790 default:
1791 usage_tree();
1792 /* NOTREACHED */
1796 argc -= optind;
1797 argv += optind;
1799 if (argc == 1)
1800 path = argv[0];
1801 else if (argc > 1)
1802 usage_tree();
1803 else
1804 path = NULL;
1806 cwd = getcwd(NULL, 0);
1807 if (cwd == NULL) {
1808 error = got_error_from_errno("getcwd");
1809 goto done;
1811 if (repo_path == NULL) {
1812 error = got_worktree_open(&worktree, cwd);
1813 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1814 goto done;
1815 else
1816 error = NULL;
1817 if (worktree) {
1818 repo_path =
1819 strdup(got_worktree_get_repo_path(worktree));
1820 if (repo_path == NULL)
1821 error = got_error_from_errno("strdup");
1822 if (error)
1823 goto done;
1824 } else {
1825 repo_path = strdup(cwd);
1826 if (repo_path == NULL) {
1827 error = got_error_from_errno("strdup");
1828 goto done;
1833 error = got_repo_open(&repo, repo_path);
1834 if (error != NULL)
1835 goto done;
1837 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1838 if (error)
1839 goto done;
1841 if (path == NULL) {
1842 if (worktree) {
1843 char *p, *worktree_subdir = cwd +
1844 strlen(got_worktree_get_root_path(worktree));
1845 if (asprintf(&p, "%s/%s",
1846 got_worktree_get_path_prefix(worktree),
1847 worktree_subdir) == -1) {
1848 error = got_error_from_errno("asprintf");
1849 goto done;
1851 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1852 free(p);
1853 if (error)
1854 goto done;
1855 } else
1856 path = "/";
1858 if (in_repo_path == NULL) {
1859 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1860 if (error != NULL)
1861 goto done;
1864 if (commit_id_str == NULL) {
1865 struct got_reference *head_ref;
1866 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1867 if (error != NULL)
1868 goto done;
1869 error = got_ref_resolve(&commit_id, repo, head_ref);
1870 got_ref_close(head_ref);
1871 if (error != NULL)
1872 goto done;
1873 } else {
1874 error = got_object_resolve_id_str(&commit_id, repo,
1875 commit_id_str);
1876 if (error != NULL)
1877 goto done;
1880 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1881 in_repo_path, repo);
1882 done:
1883 free(in_repo_path);
1884 free(repo_path);
1885 free(cwd);
1886 free(commit_id);
1887 if (worktree)
1888 got_worktree_close(worktree);
1889 if (repo) {
1890 const struct got_error *repo_error;
1891 repo_error = got_repo_close(repo);
1892 if (error == NULL)
1893 error = repo_error;
1895 return error;
1898 __dead static void
1899 usage_status(void)
1901 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1902 exit(1);
1905 static const struct got_error *
1906 print_status(void *arg, unsigned char status, const char *path,
1907 struct got_object_id *blob_id, struct got_object_id *commit_id)
1909 printf("%c %s\n", status, path);
1910 return NULL;
1913 static const struct got_error *
1914 cmd_status(int argc, char *argv[])
1916 const struct got_error *error = NULL;
1917 struct got_repository *repo = NULL;
1918 struct got_worktree *worktree = NULL;
1919 char *cwd = NULL, *path = NULL;
1920 int ch;
1922 while ((ch = getopt(argc, argv, "")) != -1) {
1923 switch (ch) {
1924 default:
1925 usage_status();
1926 /* NOTREACHED */
1930 argc -= optind;
1931 argv += optind;
1933 #ifndef PROFILE
1934 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1935 NULL) == -1)
1936 err(1, "pledge");
1937 #endif
1938 cwd = getcwd(NULL, 0);
1939 if (cwd == NULL) {
1940 error = got_error_from_errno("getcwd");
1941 goto done;
1944 error = got_worktree_open(&worktree, cwd);
1945 if (error != NULL)
1946 goto done;
1948 if (argc == 0) {
1949 path = strdup("");
1950 if (path == NULL) {
1951 error = got_error_from_errno("strdup");
1952 goto done;
1954 } else if (argc == 1) {
1955 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1956 if (error)
1957 goto done;
1958 } else
1959 usage_status();
1961 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1962 if (error != NULL)
1963 goto done;
1965 error = apply_unveil(got_repo_get_path(repo), 1,
1966 got_worktree_get_root_path(worktree), 0);
1967 if (error)
1968 goto done;
1970 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1971 check_cancelled, NULL);
1972 done:
1973 free(cwd);
1974 free(path);
1975 return error;
1978 __dead static void
1979 usage_ref(void)
1981 fprintf(stderr,
1982 "usage: %s ref [-r repository] -l | -d name | name target\n",
1983 getprogname());
1984 exit(1);
1987 static const struct got_error *
1988 list_refs(struct got_repository *repo)
1990 static const struct got_error *err = NULL;
1991 struct got_reflist_head refs;
1992 struct got_reflist_entry *re;
1994 SIMPLEQ_INIT(&refs);
1995 err = got_ref_list(&refs, repo);
1996 if (err)
1997 return err;
1999 SIMPLEQ_FOREACH(re, &refs, entry) {
2000 char *refstr;
2001 refstr = got_ref_to_str(re->ref);
2002 if (refstr == NULL)
2003 return got_error_from_errno("got_ref_to_str");
2004 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2005 free(refstr);
2008 got_ref_list_free(&refs);
2009 return NULL;
2012 static const struct got_error *
2013 delete_ref(struct got_repository *repo, const char *refname)
2015 const struct got_error *err = NULL;
2016 struct got_reference *ref;
2018 err = got_ref_open(&ref, repo, refname, 0);
2019 if (err)
2020 return err;
2022 err = got_ref_delete(ref, repo);
2023 got_ref_close(ref);
2024 return err;
2027 static const struct got_error *
2028 add_ref(struct got_repository *repo, const char *refname, const char *target)
2030 const struct got_error *err = NULL;
2031 struct got_object_id *id;
2032 struct got_reference *ref = NULL;
2034 err = got_object_resolve_id_str(&id, repo, target);
2035 if (err) {
2036 struct got_reference *target_ref;
2038 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2039 return err;
2040 err = got_ref_open(&target_ref, repo, target, 0);
2041 if (err)
2042 return err;
2043 err = got_ref_resolve(&id, repo, target_ref);
2044 got_ref_close(target_ref);
2045 if (err)
2046 return err;
2049 err = got_ref_alloc(&ref, refname, id);
2050 if (err)
2051 goto done;
2053 err = got_ref_write(ref, repo);
2054 done:
2055 if (ref)
2056 got_ref_close(ref);
2057 free(id);
2058 return err;
2061 static const struct got_error *
2062 cmd_ref(int argc, char *argv[])
2064 const struct got_error *error = NULL;
2065 struct got_repository *repo = NULL;
2066 struct got_worktree *worktree = NULL;
2067 char *cwd = NULL, *repo_path = NULL;
2068 int ch, do_list = 0;
2069 const char *delref = NULL;
2071 /* TODO: Add -s option for adding symbolic references. */
2072 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2073 switch (ch) {
2074 case 'd':
2075 delref = optarg;
2076 break;
2077 case 'r':
2078 repo_path = realpath(optarg, NULL);
2079 if (repo_path == NULL)
2080 err(1, "-r option");
2081 got_path_strip_trailing_slashes(repo_path);
2082 break;
2083 case 'l':
2084 do_list = 1;
2085 break;
2086 default:
2087 usage_ref();
2088 /* NOTREACHED */
2092 if (do_list && delref)
2093 errx(1, "-l and -d options are mutually exclusive\n");
2095 argc -= optind;
2096 argv += optind;
2098 if (do_list || delref) {
2099 if (argc > 0)
2100 usage_ref();
2101 } else if (argc != 2)
2102 usage_ref();
2104 #ifndef PROFILE
2105 if (do_list) {
2106 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2107 NULL) == -1)
2108 err(1, "pledge");
2109 } else {
2110 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2111 "sendfd unveil", NULL) == -1)
2112 err(1, "pledge");
2114 #endif
2115 cwd = getcwd(NULL, 0);
2116 if (cwd == NULL) {
2117 error = got_error_from_errno("getcwd");
2118 goto done;
2121 if (repo_path == NULL) {
2122 error = got_worktree_open(&worktree, cwd);
2123 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2124 goto done;
2125 else
2126 error = NULL;
2127 if (worktree) {
2128 repo_path =
2129 strdup(got_worktree_get_repo_path(worktree));
2130 if (repo_path == NULL)
2131 error = got_error_from_errno("strdup");
2132 if (error)
2133 goto done;
2134 } else {
2135 repo_path = strdup(cwd);
2136 if (repo_path == NULL) {
2137 error = got_error_from_errno("strdup");
2138 goto done;
2143 error = got_repo_open(&repo, repo_path);
2144 if (error != NULL)
2145 goto done;
2147 error = apply_unveil(got_repo_get_path(repo), do_list,
2148 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2149 if (error)
2150 goto done;
2152 if (do_list)
2153 error = list_refs(repo);
2154 else if (delref)
2155 error = delete_ref(repo, delref);
2156 else
2157 error = add_ref(repo, argv[0], argv[1]);
2158 done:
2159 if (repo)
2160 got_repo_close(repo);
2161 if (worktree)
2162 got_worktree_close(worktree);
2163 free(cwd);
2164 free(repo_path);
2165 return error;
2168 __dead static void
2169 usage_add(void)
2171 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2172 exit(1);
2175 static const struct got_error *
2176 cmd_add(int argc, char *argv[])
2178 const struct got_error *error = NULL;
2179 struct got_repository *repo = NULL;
2180 struct got_worktree *worktree = NULL;
2181 char *cwd = NULL;
2182 struct got_pathlist_head paths;
2183 struct got_pathlist_entry *pe;
2184 int ch, x;
2186 TAILQ_INIT(&paths);
2188 while ((ch = getopt(argc, argv, "")) != -1) {
2189 switch (ch) {
2190 default:
2191 usage_add();
2192 /* NOTREACHED */
2196 argc -= optind;
2197 argv += optind;
2199 if (argc < 1)
2200 usage_add();
2202 /* make sure each file exists before doing anything halfway */
2203 for (x = 0; x < argc; x++) {
2204 char *path = realpath(argv[x], NULL);
2205 if (path == NULL) {
2206 error = got_error_from_errno2("realpath", argv[x]);
2207 goto done;
2209 free(path);
2212 cwd = getcwd(NULL, 0);
2213 if (cwd == NULL) {
2214 error = got_error_from_errno("getcwd");
2215 goto done;
2218 error = got_worktree_open(&worktree, cwd);
2219 if (error)
2220 goto done;
2222 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2223 if (error != NULL)
2224 goto done;
2226 error = apply_unveil(got_repo_get_path(repo), 1,
2227 got_worktree_get_root_path(worktree), 0);
2228 if (error)
2229 goto done;
2231 for (x = 0; x < argc; x++) {
2232 char *path = realpath(argv[x], NULL);
2233 if (path == NULL) {
2234 error = got_error_from_errno2("realpath", argv[x]);
2235 goto done;
2238 got_path_strip_trailing_slashes(path);
2239 error = got_pathlist_insert(&pe, &paths, path, NULL);
2240 if (error) {
2241 free(path);
2242 goto done;
2245 error = got_worktree_schedule_add(worktree, &paths, print_status,
2246 NULL, repo);
2247 done:
2248 if (repo)
2249 got_repo_close(repo);
2250 if (worktree)
2251 got_worktree_close(worktree);
2252 TAILQ_FOREACH(pe, &paths, entry)
2253 free((char *)pe->path);
2254 got_pathlist_free(&paths);
2255 free(cwd);
2256 return error;
2259 __dead static void
2260 usage_rm(void)
2262 fprintf(stderr, "usage: %s rm [-f] file-path ...\n", getprogname());
2263 exit(1);
2266 static const struct got_error *
2267 cmd_rm(int argc, char *argv[])
2269 const struct got_error *error = NULL;
2270 struct got_worktree *worktree = NULL;
2271 struct got_repository *repo = NULL;
2272 char *cwd = NULL;
2273 struct got_pathlist_head paths;
2274 struct got_pathlist_entry *pe;
2275 int ch, i, delete_local_mods = 0;
2277 TAILQ_INIT(&paths);
2279 while ((ch = getopt(argc, argv, "f")) != -1) {
2280 switch (ch) {
2281 case 'f':
2282 delete_local_mods = 1;
2283 break;
2284 default:
2285 usage_add();
2286 /* NOTREACHED */
2290 argc -= optind;
2291 argv += optind;
2293 if (argc < 1)
2294 usage_rm();
2296 /* make sure each file exists before doing anything halfway */
2297 for (i = 0; i < argc; i++) {
2298 char *path = realpath(argv[i], NULL);
2299 if (path == NULL) {
2300 error = got_error_from_errno2("realpath", argv[i]);
2301 goto done;
2303 free(path);
2306 cwd = getcwd(NULL, 0);
2307 if (cwd == NULL) {
2308 error = got_error_from_errno("getcwd");
2309 goto done;
2311 error = got_worktree_open(&worktree, cwd);
2312 if (error)
2313 goto done;
2315 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2316 if (error)
2317 goto done;
2319 error = apply_unveil(got_repo_get_path(repo), 1,
2320 got_worktree_get_root_path(worktree), 0);
2321 if (error)
2322 goto done;
2324 for (i = 0; i < argc; i++) {
2325 char *path = realpath(argv[i], NULL);
2326 if (path == NULL) {
2327 error = got_error_from_errno2("realpath", argv[i]);
2328 goto done;
2331 got_path_strip_trailing_slashes(path);
2332 error = got_pathlist_insert(&pe, &paths, path, NULL);
2333 if (error) {
2334 free(path);
2335 goto done;
2338 error = got_worktree_schedule_delete(worktree, &paths,
2339 delete_local_mods, print_status, NULL, repo);
2340 if (error)
2341 goto done;
2342 done:
2343 if (repo)
2344 got_repo_close(repo);
2345 if (worktree)
2346 got_worktree_close(worktree);
2347 TAILQ_FOREACH(pe, &paths, entry)
2348 free((char *)pe->path);
2349 got_pathlist_free(&paths);
2350 free(cwd);
2351 return error;
2354 __dead static void
2355 usage_revert(void)
2357 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2358 exit(1);
2361 static void
2362 revert_progress(void *arg, unsigned char status, const char *path)
2364 while (path[0] == '/')
2365 path++;
2366 printf("%c %s\n", status, path);
2369 static const struct got_error *
2370 cmd_revert(int argc, char *argv[])
2372 const struct got_error *error = NULL;
2373 struct got_worktree *worktree = NULL;
2374 struct got_repository *repo = NULL;
2375 char *cwd = NULL, *path = NULL;
2376 struct got_pathlist_head paths;
2377 struct got_pathlist_entry *pe;
2378 int ch, i;
2380 TAILQ_INIT(&paths);
2382 while ((ch = getopt(argc, argv, "")) != -1) {
2383 switch (ch) {
2384 default:
2385 usage_revert();
2386 /* NOTREACHED */
2390 argc -= optind;
2391 argv += optind;
2393 if (argc < 1)
2394 usage_revert();
2396 for (i = 0; i < argc; i++) {
2397 char *path = realpath(argv[i], NULL);
2398 if (path == NULL) {
2399 error = got_error_from_errno2("realpath", argv[i]);
2400 goto done;
2403 got_path_strip_trailing_slashes(path);
2404 error = got_pathlist_insert(&pe, &paths, path, NULL);
2405 if (error) {
2406 free(path);
2407 goto done;
2411 cwd = getcwd(NULL, 0);
2412 if (cwd == NULL) {
2413 error = got_error_from_errno("getcwd");
2414 goto done;
2416 error = got_worktree_open(&worktree, cwd);
2417 if (error)
2418 goto done;
2420 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2421 if (error != NULL)
2422 goto done;
2424 error = apply_unveil(got_repo_get_path(repo), 1,
2425 got_worktree_get_root_path(worktree), 0);
2426 if (error)
2427 goto done;
2429 error = got_worktree_revert(worktree, &paths,
2430 revert_progress, NULL, repo);
2431 if (error)
2432 goto done;
2433 done:
2434 if (repo)
2435 got_repo_close(repo);
2436 if (worktree)
2437 got_worktree_close(worktree);
2438 free(path);
2439 free(cwd);
2440 return error;
2443 __dead static void
2444 usage_commit(void)
2446 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2447 exit(1);
2450 int
2451 spawn_editor(const char *editor, const char *file)
2453 pid_t pid;
2454 sig_t sighup, sigint, sigquit;
2455 int st = -1;
2457 sighup = signal(SIGHUP, SIG_IGN);
2458 sigint = signal(SIGINT, SIG_IGN);
2459 sigquit = signal(SIGQUIT, SIG_IGN);
2461 switch (pid = fork()) {
2462 case -1:
2463 goto doneediting;
2464 case 0:
2465 execl(editor, editor, file, (char *)NULL);
2466 _exit(127);
2469 while (waitpid(pid, &st, 0) == -1)
2470 if (errno != EINTR)
2471 break;
2473 doneediting:
2474 (void)signal(SIGHUP, sighup);
2475 (void)signal(SIGINT, sigint);
2476 (void)signal(SIGQUIT, sigquit);
2478 if (!WIFEXITED(st)) {
2479 errno = EINTR;
2480 return -1;
2483 return WEXITSTATUS(st);
2486 struct collect_commit_logmsg_arg {
2487 const char *cmdline_log;
2488 const char *editor;
2489 const char *worktree_path;
2490 const char *branch_name;
2491 const char *repo_path;
2492 char *logmsg_path;
2496 static const struct got_error *
2497 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2498 void *arg)
2500 char *initial_content = NULL;
2501 struct got_pathlist_entry *pe;
2502 const struct got_error *err = NULL;
2503 char *template = NULL;
2504 struct collect_commit_logmsg_arg *a = arg;
2505 char buf[1024];
2506 struct stat st, st2;
2507 FILE *fp;
2508 size_t len;
2509 int fd, content_changed = 0;
2511 /* if a message was specified on the command line, just use it */
2512 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2513 len = strlen(a->cmdline_log) + 1;
2514 *logmsg = malloc(len + 1);
2515 if (*logmsg == NULL)
2516 return got_error_from_errno("malloc");
2517 strlcpy(*logmsg, a->cmdline_log, len);
2518 return NULL;
2521 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2522 return got_error_from_errno("asprintf");
2524 if (asprintf(&initial_content,
2525 "\n# changes to be committed on branch %s:\n",
2526 a->branch_name) == -1)
2527 return got_error_from_errno("asprintf");
2529 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2530 if (err)
2531 goto done;
2533 dprintf(fd, initial_content);
2535 TAILQ_FOREACH(pe, commitable_paths, entry) {
2536 struct got_commitable *ct = pe->data;
2537 dprintf(fd, "# %c %s\n",
2538 got_commitable_get_status(ct),
2539 got_commitable_get_path(ct));
2541 close(fd);
2543 if (stat(a->logmsg_path, &st) == -1) {
2544 err = got_error_from_errno2("stat", a->logmsg_path);
2545 goto done;
2548 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2549 err = got_error_from_errno("failed spawning editor");
2550 goto done;
2553 if (stat(a->logmsg_path, &st2) == -1) {
2554 err = got_error_from_errno("stat");
2555 goto done;
2558 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2559 unlink(a->logmsg_path);
2560 free(a->logmsg_path);
2561 a->logmsg_path = NULL;
2562 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2563 "no changes made to commit message, aborting");
2564 goto done;
2567 *logmsg = malloc(st2.st_size + 1);
2568 if (*logmsg == NULL) {
2569 err = got_error_from_errno("malloc");
2570 goto done;
2572 (*logmsg)[0] = '\0';
2573 len = 0;
2575 fp = fopen(a->logmsg_path, "r");
2576 if (fp == NULL) {
2577 err = got_error_from_errno("fopen");
2578 goto done;
2580 while (fgets(buf, sizeof(buf), fp) != NULL) {
2581 if (!content_changed && strcmp(buf, initial_content) != 0)
2582 content_changed = 1;
2583 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2584 continue; /* remove comments and leading empty lines */
2585 len = strlcat(*logmsg, buf, st2.st_size);
2587 fclose(fp);
2589 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2590 (*logmsg)[len - 1] = '\0';
2591 len--;
2594 if (len == 0 || !content_changed) {
2595 unlink(a->logmsg_path);
2596 free(a->logmsg_path);
2597 a->logmsg_path = NULL;
2598 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2599 "commit message cannot be empty, aborting");
2600 goto done;
2602 done:
2603 free(initial_content);
2604 free(template);
2606 /* Editor is done; we can now apply unveil(2) */
2607 if (err == NULL)
2608 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2609 return err;
2612 static const struct got_error *
2613 cmd_commit(int argc, char *argv[])
2615 const struct got_error *error = NULL;
2616 struct got_worktree *worktree = NULL;
2617 struct got_repository *repo = NULL;
2618 char *cwd = NULL, *path = NULL, *id_str = NULL;
2619 struct got_object_id *id = NULL;
2620 const char *logmsg = NULL;
2621 const char *got_author = getenv("GOT_AUTHOR");
2622 struct collect_commit_logmsg_arg cl_arg;
2623 char *editor = NULL;
2624 int ch;
2626 while ((ch = getopt(argc, argv, "m:")) != -1) {
2627 switch (ch) {
2628 case 'm':
2629 logmsg = optarg;
2630 break;
2631 default:
2632 usage_commit();
2633 /* NOTREACHED */
2637 argc -= optind;
2638 argv += optind;
2640 if (argc == 1) {
2641 path = realpath(argv[0], NULL);
2642 if (path == NULL) {
2643 error = got_error_from_errno2("realpath", argv[0]);
2644 goto done;
2646 got_path_strip_trailing_slashes(path);
2647 } else if (argc != 0)
2648 usage_commit();
2650 if (got_author == NULL) {
2651 /* TODO: Look current user up in password database */
2652 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2653 goto done;
2656 cwd = getcwd(NULL, 0);
2657 if (cwd == NULL) {
2658 error = got_error_from_errno("getcwd");
2659 goto done;
2661 error = got_worktree_open(&worktree, cwd);
2662 if (error)
2663 goto done;
2665 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2666 if (error != NULL)
2667 goto done;
2670 * unveil(2) traverses exec(2); if an editor is used we have
2671 * to apply unveil after the log message has been written.
2673 if (logmsg == NULL || strlen(logmsg) == 0)
2674 error = get_editor(&editor);
2675 else
2676 error = apply_unveil(got_repo_get_path(repo), 0,
2677 got_worktree_get_root_path(worktree), 0);
2678 if (error)
2679 goto done;
2681 cl_arg.editor = editor;
2682 cl_arg.cmdline_log = logmsg;
2683 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2684 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2685 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2686 cl_arg.branch_name += 5;
2687 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2688 cl_arg.branch_name += 6;
2689 cl_arg.repo_path = got_repo_get_path(repo);
2690 cl_arg.logmsg_path = NULL;
2691 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2692 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2693 if (error) {
2694 if (cl_arg.logmsg_path)
2695 fprintf(stderr, "%s: log message preserved in %s\n",
2696 getprogname(), cl_arg.logmsg_path);
2697 goto done;
2700 if (cl_arg.logmsg_path)
2701 unlink(cl_arg.logmsg_path);
2703 error = got_object_id_str(&id_str, id);
2704 if (error)
2705 goto done;
2706 printf("Created commit %s\n", id_str);
2707 done:
2708 if (repo)
2709 got_repo_close(repo);
2710 if (worktree)
2711 got_worktree_close(worktree);
2712 free(path);
2713 free(cwd);
2714 free(id_str);
2715 free(editor);
2716 return error;
2719 __dead static void
2720 usage_cherrypick(void)
2722 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2723 exit(1);
2726 static const struct got_error *
2727 cmd_cherrypick(int argc, char *argv[])
2729 const struct got_error *error = NULL;
2730 struct got_worktree *worktree = NULL;
2731 struct got_repository *repo = NULL;
2732 char *cwd = NULL, *commit_id_str = NULL;
2733 struct got_object_id *commit_id = NULL;
2734 struct got_commit_object *commit = NULL;
2735 struct got_object_qid *pid;
2736 struct got_reference *head_ref = NULL;
2737 int ch, did_something = 0;
2739 while ((ch = getopt(argc, argv, "")) != -1) {
2740 switch (ch) {
2741 default:
2742 usage_cherrypick();
2743 /* NOTREACHED */
2747 argc -= optind;
2748 argv += optind;
2750 if (argc != 1)
2751 usage_cherrypick();
2753 cwd = getcwd(NULL, 0);
2754 if (cwd == NULL) {
2755 error = got_error_from_errno("getcwd");
2756 goto done;
2758 error = got_worktree_open(&worktree, cwd);
2759 if (error)
2760 goto done;
2762 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2763 if (error != NULL)
2764 goto done;
2766 error = apply_unveil(got_repo_get_path(repo), 0,
2767 got_worktree_get_root_path(worktree), 0);
2768 if (error)
2769 goto done;
2771 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2772 if (error != NULL) {
2773 struct got_reference *ref;
2774 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2775 goto done;
2776 error = got_ref_open(&ref, repo, argv[0], 0);
2777 if (error != NULL)
2778 goto done;
2779 error = got_ref_resolve(&commit_id, repo, ref);
2780 got_ref_close(ref);
2781 if (error != NULL)
2782 goto done;
2784 error = got_object_id_str(&commit_id_str, commit_id);
2785 if (error)
2786 goto done;
2788 error = got_ref_open(&head_ref, repo,
2789 got_worktree_get_head_ref_name(worktree), 0);
2790 if (error != NULL)
2791 goto done;
2793 error = check_same_branch(commit_id, head_ref, repo);
2794 if (error) {
2795 if (error->code != GOT_ERR_ANCESTRY)
2796 goto done;
2797 error = NULL;
2798 } else {
2799 error = got_error(GOT_ERR_SAME_BRANCH);
2800 goto done;
2803 error = got_object_open_as_commit(&commit, repo, commit_id);
2804 if (error)
2805 goto done;
2806 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2807 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
2808 commit_id, repo, update_progress, &did_something, check_cancelled,
2809 NULL);
2810 if (error != NULL)
2811 goto done;
2813 if (did_something)
2814 printf("Merged commit %s\n", commit_id_str);
2815 done:
2816 if (commit)
2817 got_object_commit_close(commit);
2818 free(commit_id_str);
2819 if (head_ref)
2820 got_ref_close(head_ref);
2821 if (worktree)
2822 got_worktree_close(worktree);
2823 if (repo)
2824 got_repo_close(repo);
2825 return error;
2828 __dead static void
2829 usage_backout(void)
2831 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
2832 exit(1);
2835 static const struct got_error *
2836 cmd_backout(int argc, char *argv[])
2838 const struct got_error *error = NULL;
2839 struct got_worktree *worktree = NULL;
2840 struct got_repository *repo = NULL;
2841 char *cwd = NULL, *commit_id_str = NULL;
2842 struct got_object_id *commit_id = NULL;
2843 struct got_commit_object *commit = NULL;
2844 struct got_object_qid *pid;
2845 struct got_reference *head_ref = NULL;
2846 int ch, did_something = 0;
2848 while ((ch = getopt(argc, argv, "")) != -1) {
2849 switch (ch) {
2850 default:
2851 usage_backout();
2852 /* NOTREACHED */
2856 argc -= optind;
2857 argv += optind;
2859 if (argc != 1)
2860 usage_backout();
2862 cwd = getcwd(NULL, 0);
2863 if (cwd == NULL) {
2864 error = got_error_from_errno("getcwd");
2865 goto done;
2867 error = got_worktree_open(&worktree, cwd);
2868 if (error)
2869 goto done;
2871 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2872 if (error != NULL)
2873 goto done;
2875 error = apply_unveil(got_repo_get_path(repo), 0,
2876 got_worktree_get_root_path(worktree), 0);
2877 if (error)
2878 goto done;
2880 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
2881 if (error != NULL) {
2882 struct got_reference *ref;
2883 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2884 goto done;
2885 error = got_ref_open(&ref, repo, argv[0], 0);
2886 if (error != NULL)
2887 goto done;
2888 error = got_ref_resolve(&commit_id, repo, ref);
2889 got_ref_close(ref);
2890 if (error != NULL)
2891 goto done;
2893 error = got_object_id_str(&commit_id_str, commit_id);
2894 if (error)
2895 goto done;
2897 error = got_ref_open(&head_ref, repo,
2898 got_worktree_get_head_ref_name(worktree), 0);
2899 if (error != NULL)
2900 goto done;
2902 error = check_same_branch(commit_id, head_ref, repo);
2903 if (error)
2904 goto done;
2906 error = got_object_open_as_commit(&commit, repo, commit_id);
2907 if (error)
2908 goto done;
2909 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2910 if (pid == NULL) {
2911 error = got_error(GOT_ERR_ROOT_COMMIT);
2912 goto done;
2915 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
2916 update_progress, &did_something, check_cancelled, NULL);
2917 if (error != NULL)
2918 goto done;
2920 if (did_something)
2921 printf("Backed out commit %s\n", commit_id_str);
2922 done:
2923 if (commit)
2924 got_object_commit_close(commit);
2925 free(commit_id_str);
2926 if (head_ref)
2927 got_ref_close(head_ref);
2928 if (worktree)
2929 got_worktree_close(worktree);
2930 if (repo)
2931 got_repo_close(repo);
2932 return error;