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);
90 static const struct got_error* cmd_checkout(int, char *[]);
91 static const struct got_error* cmd_update(int, char *[]);
92 static const struct got_error* cmd_log(int, char *[]);
93 static const struct got_error* cmd_diff(int, char *[]);
94 static const struct got_error* cmd_blame(int, char *[]);
95 static const struct got_error* cmd_tree(int, char *[]);
96 static const struct got_error* cmd_status(int, char *[]);
97 static const struct got_error* cmd_ref(int, char *[]);
98 static const struct got_error* cmd_add(int, char *[]);
99 static const struct got_error* cmd_rm(int, char *[]);
100 static const struct got_error* cmd_revert(int, char *[]);
101 static const struct got_error* cmd_commit(int, char *[]);
103 static struct cmd got_commands[] = {
104 { "checkout", cmd_checkout, usage_checkout,
105 "check out a new work tree from a repository" },
106 { "update", cmd_update, usage_update,
107 "update a work tree to a different commit" },
108 { "log", cmd_log, usage_log,
109 "show repository history" },
110 { "diff", cmd_diff, usage_diff,
111 "compare files and directories" },
112 { "blame", cmd_blame, usage_blame,
113 "show when lines in a file were changed" },
114 { "tree", cmd_tree, usage_tree,
115 "list files and directories in repository" },
116 { "status", cmd_status, usage_status,
117 "show modification status of files" },
118 { "ref", cmd_ref, usage_ref,
119 "manage references in repository" },
120 { "add", cmd_add, usage_add,
121 "add new files to version control" },
122 { "rm", cmd_rm, usage_rm,
123 "remove a versioned file" },
124 { "revert", cmd_revert, usage_revert,
125 "revert uncommitted changes" },
126 { "commit", cmd_commit, usage_commit,
127 "write changes from work tree to repository" },
128 };
130 int
131 main(int argc, char *argv[])
133 struct cmd *cmd;
134 unsigned int i;
135 int ch;
136 int hflag = 0;
138 setlocale(LC_CTYPE, "");
140 while ((ch = getopt(argc, argv, "h")) != -1) {
141 switch (ch) {
142 case 'h':
143 hflag = 1;
144 break;
145 default:
146 usage();
147 /* NOTREACHED */
151 argc -= optind;
152 argv += optind;
153 optind = 0;
155 if (argc <= 0)
156 usage();
158 signal(SIGINT, catch_sigint);
159 signal(SIGPIPE, catch_sigpipe);
161 for (i = 0; i < nitems(got_commands); i++) {
162 const struct got_error *error;
164 cmd = &got_commands[i];
166 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
167 continue;
169 if (hflag)
170 got_commands[i].cmd_usage();
172 error = got_commands[i].cmd_main(argc, argv);
173 if (error && !(sigint_received || sigpipe_received)) {
174 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
175 return 1;
178 return 0;
181 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
182 return 1;
185 __dead static void
186 usage(void)
188 int i;
190 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
191 "Available commands:\n", getprogname());
192 for (i = 0; i < nitems(got_commands); i++) {
193 struct cmd *cmd = &got_commands[i];
194 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
196 exit(1);
199 static const struct got_error *
200 get_editor(char **abspath)
202 const struct got_error *err = NULL;
203 const char *editor;
205 editor = getenv("VISUAL");
206 if (editor == NULL)
207 editor = getenv("EDITOR");
209 if (editor) {
210 err = got_path_find_prog(abspath, editor);
211 if (err)
212 return err;
215 if (*abspath == NULL) {
216 *abspath = strdup("/bin/ed");
217 if (*abspath == NULL)
218 return got_error_from_errno("strdup");
221 return NULL;
224 static const struct got_error *
225 apply_unveil(const char *repo_path, int repo_read_only,
226 const char *worktree_path, int create_worktree)
228 const struct got_error *err;
230 if (create_worktree) {
231 /* Pre-create work tree path to avoid unveiling its parents. */
232 err = got_path_mkdir(worktree_path);
234 if (errno == EEXIST) {
235 if (got_path_dir_is_empty(worktree_path)) {
236 errno = 0;
237 err = NULL;
238 } else {
239 err = got_error_path(worktree_path,
240 GOT_ERR_DIR_NOT_EMPTY);
244 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
245 return err;
248 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
249 return got_error_from_errno2("unveil", repo_path);
251 if (worktree_path && unveil(worktree_path, "rwc") != 0)
252 return got_error_from_errno2("unveil", worktree_path);
254 if (unveil("/tmp", "rwc") != 0)
255 return got_error_from_errno2("unveil", "/tmp");
257 err = got_privsep_unveil_exec_helpers();
258 if (err != NULL)
259 return err;
261 if (unveil(NULL, NULL) != 0)
262 return got_error_from_errno("unveil");
264 return NULL;
267 __dead static void
268 usage_checkout(void)
270 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
271 "[-p prefix] repository-path [worktree-path]\n", getprogname());
272 exit(1);
275 static void
276 checkout_progress(void *arg, unsigned char status, const char *path)
278 char *worktree_path = arg;
280 while (path[0] == '/')
281 path++;
283 printf("%c %s/%s\n", status, worktree_path, path);
286 static const struct got_error *
287 check_cancelled(void *arg)
289 if (sigint_received || sigpipe_received)
290 return got_error(GOT_ERR_CANCELLED);
291 return NULL;
294 static const struct got_error *
295 check_linear_ancestry(struct got_object_id *commit_id,
296 struct got_object_id *base_commit_id, struct got_repository *repo)
298 const struct got_error *err = NULL;
299 struct got_object_id *yca_id;
301 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
302 commit_id, base_commit_id, repo);
303 if (err)
304 return err;
306 if (yca_id == NULL)
307 return got_error(GOT_ERR_ANCESTRY);
309 /*
310 * Require a straight line of history between the target commit
311 * and the work tree's base commit.
313 * Non-linear situations such as this require a rebase:
315 * (commit) D F (base_commit)
316 * \ /
317 * C E
318 * \ /
319 * B (yca)
320 * |
321 * A
323 * 'got update' only handles linear cases:
324 * Update forwards in time: A (base/yca) - B - C - D (commit)
325 * Update backwards in time: D (base) - C - B - A (commit/yca)
326 */
327 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
328 got_object_id_cmp(base_commit_id, yca_id) != 0)
329 return got_error(GOT_ERR_ANCESTRY);
331 free(yca_id);
332 return NULL;
335 static const struct got_error *
336 check_same_branch(struct got_object_id *commit_id,
337 struct got_reference *head_ref, struct got_repository *repo)
339 const struct got_error *err = NULL;
340 struct got_commit_graph *graph = NULL;
341 struct got_object_id *head_commit_id = NULL;
342 int is_same_branch = 0;
344 err = got_ref_resolve(&head_commit_id, repo, head_ref);
345 if (err)
346 goto done;
348 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
349 if (err)
350 goto done;
352 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
353 if (err)
354 goto done;
356 for (;;) {
357 struct got_object_id *id;
358 err = got_commit_graph_iter_next(&id, graph);
359 if (err) {
360 if (err->code == GOT_ERR_ITER_COMPLETED) {
361 err = NULL;
362 break;
364 else if (err->code != GOT_ERR_ITER_NEED_MORE)
365 break;
366 err = got_commit_graph_fetch_commits(graph, 1,
367 repo);
368 if (err)
369 break;
372 if (id) {
373 if (got_object_id_cmp(id, commit_id) == 0) {
374 is_same_branch = 1;
375 break;
379 done:
380 if (graph)
381 got_commit_graph_close(graph);
382 free(head_commit_id);
383 if (!err && !is_same_branch)
384 err = got_error(GOT_ERR_ANCESTRY);
385 return err;
388 static const struct got_error *
389 cmd_checkout(int argc, char *argv[])
391 const struct got_error *error = NULL;
392 struct got_repository *repo = NULL;
393 struct got_reference *head_ref = NULL;
394 struct got_worktree *worktree = NULL;
395 char *repo_path = NULL;
396 char *worktree_path = NULL;
397 const char *path_prefix = "";
398 const char *branch_name = GOT_REF_HEAD;
399 char *commit_id_str = NULL;
400 int ch, same_path_prefix;
402 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
403 switch (ch) {
404 case 'b':
405 branch_name = optarg;
406 break;
407 case 'c':
408 commit_id_str = strdup(optarg);
409 if (commit_id_str == NULL)
410 return got_error_from_errno("strdup");
411 break;
412 case 'p':
413 path_prefix = optarg;
414 break;
415 default:
416 usage_checkout();
417 /* NOTREACHED */
421 argc -= optind;
422 argv += optind;
424 #ifndef PROFILE
425 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
426 "unveil", NULL) == -1)
427 err(1, "pledge");
428 #endif
429 if (argc == 1) {
430 char *cwd, *base, *dotgit;
431 repo_path = realpath(argv[0], NULL);
432 if (repo_path == NULL)
433 return got_error_from_errno2("realpath", argv[0]);
434 cwd = getcwd(NULL, 0);
435 if (cwd == NULL) {
436 error = got_error_from_errno("getcwd");
437 goto done;
439 if (path_prefix[0]) {
440 base = basename(path_prefix);
441 if (base == NULL) {
442 error = got_error_from_errno2("basename",
443 path_prefix);
444 goto done;
446 } else {
447 base = basename(repo_path);
448 if (base == NULL) {
449 error = got_error_from_errno2("basename",
450 repo_path);
451 goto done;
454 dotgit = strstr(base, ".git");
455 if (dotgit)
456 *dotgit = '\0';
457 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
458 error = got_error_from_errno("asprintf");
459 free(cwd);
460 goto done;
462 free(cwd);
463 } else if (argc == 2) {
464 repo_path = realpath(argv[0], NULL);
465 if (repo_path == NULL) {
466 error = got_error_from_errno2("realpath", argv[0]);
467 goto done;
469 worktree_path = realpath(argv[1], NULL);
470 if (worktree_path == NULL) {
471 error = got_error_from_errno2("realpath", argv[1]);
472 goto done;
474 } else
475 usage_checkout();
477 got_path_strip_trailing_slashes(repo_path);
478 got_path_strip_trailing_slashes(worktree_path);
480 error = got_repo_open(&repo, repo_path);
481 if (error != NULL)
482 goto done;
484 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
485 if (error)
486 goto done;
488 error = got_ref_open(&head_ref, repo, branch_name, 0);
489 if (error != NULL)
490 goto done;
492 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
493 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
494 goto done;
496 error = got_worktree_open(&worktree, worktree_path);
497 if (error != NULL)
498 goto done;
500 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
501 path_prefix);
502 if (error != NULL)
503 goto done;
504 if (!same_path_prefix) {
505 error = got_error(GOT_ERR_PATH_PREFIX);
506 goto done;
509 if (commit_id_str) {
510 struct got_object_id *commit_id;
511 error = got_object_resolve_id_str(&commit_id, repo,
512 commit_id_str);
513 if (error != NULL)
514 goto done;
515 error = check_linear_ancestry(commit_id,
516 got_worktree_get_base_commit_id(worktree), repo);
517 if (error != NULL) {
518 free(commit_id);
519 goto done;
521 error = check_same_branch(commit_id, head_ref, repo);
522 if (error)
523 goto done;
524 error = got_worktree_set_base_commit_id(worktree, repo,
525 commit_id);
526 free(commit_id);
527 if (error)
528 goto done;
531 error = got_worktree_checkout_files(worktree, "", repo,
532 checkout_progress, worktree_path, check_cancelled, NULL);
533 if (error != NULL)
534 goto done;
536 printf("Now shut up and hack\n");
538 done:
539 free(commit_id_str);
540 free(repo_path);
541 free(worktree_path);
542 return error;
545 __dead static void
546 usage_update(void)
548 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
549 getprogname());
550 exit(1);
553 static void
554 update_progress(void *arg, unsigned char status, const char *path)
556 int *did_something = arg;
558 if (status == GOT_STATUS_EXISTS)
559 return;
561 *did_something = 1;
562 while (path[0] == '/')
563 path++;
564 printf("%c %s\n", status, path);
567 static const struct got_error *
568 switch_head_ref(struct got_reference *head_ref,
569 struct got_object_id *commit_id, struct got_worktree *worktree,
570 struct got_repository *repo)
572 const struct got_error *err = NULL;
573 char *base_id_str;
574 int ref_has_moved = 0;
576 /* Trivial case: switching between two different references. */
577 if (strcmp(got_ref_get_name(head_ref),
578 got_worktree_get_head_ref_name(worktree)) != 0) {
579 printf("Switching work tree from %s to %s\n",
580 got_worktree_get_head_ref_name(worktree),
581 got_ref_get_name(head_ref));
582 return got_worktree_set_head_ref(worktree, head_ref);
585 err = check_linear_ancestry(commit_id,
586 got_worktree_get_base_commit_id(worktree), repo);
587 if (err) {
588 if (err->code != GOT_ERR_ANCESTRY)
589 return err;
590 ref_has_moved = 1;
592 if (!ref_has_moved)
593 return NULL;
595 /* Switching to a rebased branch with the same reference name. */
596 err = got_object_id_str(&base_id_str,
597 got_worktree_get_base_commit_id(worktree));
598 if (err)
599 return err;
600 printf("Reference %s now points at a different branch\n",
601 got_worktree_get_head_ref_name(worktree));
602 printf("Switching work tree from %s to %s\n", base_id_str,
603 got_worktree_get_head_ref_name(worktree));
604 return NULL;
607 static const struct got_error *
608 cmd_update(int argc, char *argv[])
610 const struct got_error *error = NULL;
611 struct got_repository *repo = NULL;
612 struct got_worktree *worktree = NULL;
613 char *worktree_path = NULL, *path = NULL;
614 struct got_object_id *commit_id = NULL;
615 char *commit_id_str = NULL;
616 const char *branch_name = NULL;
617 struct got_reference *head_ref = NULL;
618 int ch, did_something = 0;
620 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
621 switch (ch) {
622 case 'b':
623 branch_name = optarg;
624 break;
625 case 'c':
626 commit_id_str = strdup(optarg);
627 if (commit_id_str == NULL)
628 return got_error_from_errno("strdup");
629 break;
630 default:
631 usage_update();
632 /* NOTREACHED */
636 argc -= optind;
637 argv += optind;
639 #ifndef PROFILE
640 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
641 "unveil", NULL) == -1)
642 err(1, "pledge");
643 #endif
644 worktree_path = getcwd(NULL, 0);
645 if (worktree_path == NULL) {
646 error = got_error_from_errno("getcwd");
647 goto done;
649 error = got_worktree_open(&worktree, worktree_path);
650 if (error)
651 goto done;
653 if (argc == 0) {
654 path = strdup("");
655 if (path == NULL) {
656 error = got_error_from_errno("strdup");
657 goto done;
659 } else if (argc == 1) {
660 error = got_worktree_resolve_path(&path, worktree, argv[0]);
661 if (error)
662 goto done;
663 } else
664 usage_update();
666 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
667 if (error != NULL)
668 goto done;
670 error = apply_unveil(got_repo_get_path(repo), 0,
671 got_worktree_get_root_path(worktree), 0);
672 if (error)
673 goto done;
675 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
676 got_worktree_get_head_ref_name(worktree), 0);
677 if (error != NULL)
678 goto done;
679 if (commit_id_str == NULL) {
680 error = got_ref_resolve(&commit_id, repo, head_ref);
681 if (error != NULL)
682 goto done;
683 error = got_object_id_str(&commit_id_str, commit_id);
684 if (error != NULL)
685 goto done;
686 } else {
687 error = got_object_resolve_id_str(&commit_id, repo,
688 commit_id_str);
689 if (error != NULL)
690 goto done;
693 if (branch_name) {
694 struct got_object_id *head_commit_id;
695 if (strlen(path) != 0) {
696 fprintf(stderr, "%s: switching between branches "
697 "requires that the entire work tree "
698 "gets updated, not just '%s'\n",
699 getprogname(), path);
700 error = got_error(GOT_ERR_BAD_PATH);
701 goto done;
703 error = got_ref_resolve(&head_commit_id, repo, head_ref);
704 if (error)
705 goto done;
706 error = check_linear_ancestry(commit_id, head_commit_id, repo);
707 free(head_commit_id);
708 if (error != NULL)
709 goto done;
710 error = check_same_branch(commit_id, head_ref, repo);
711 if (error)
712 goto done;
713 error = switch_head_ref(head_ref, commit_id, worktree, repo);
714 if (error)
715 goto done;
716 } else {
717 error = check_linear_ancestry(commit_id,
718 got_worktree_get_base_commit_id(worktree), repo);
719 if (error != NULL) {
720 if (error->code == GOT_ERR_ANCESTRY)
721 error = got_error(GOT_ERR_BRANCH_MOVED);
722 goto done;
724 error = check_same_branch(commit_id, head_ref, repo);
725 if (error)
726 goto done;
729 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
730 commit_id) != 0) {
731 error = got_worktree_set_base_commit_id(worktree, repo,
732 commit_id);
733 if (error)
734 goto done;
737 error = got_worktree_checkout_files(worktree, path, repo,
738 update_progress, &did_something, check_cancelled, NULL);
739 if (error != NULL)
740 goto done;
742 if (did_something)
743 printf("Updated to commit %s\n", commit_id_str);
744 else
745 printf("Already up-to-date\n");
746 done:
747 free(worktree_path);
748 free(path);
749 free(commit_id);
750 free(commit_id_str);
751 return error;
754 static const struct got_error *
755 print_patch(struct got_commit_object *commit, struct got_object_id *id,
756 int diff_context, struct got_repository *repo)
758 const struct got_error *err = NULL;
759 struct got_tree_object *tree1 = NULL, *tree2;
760 struct got_object_qid *qid;
761 char *id_str1 = NULL, *id_str2;
763 err = got_object_open_as_tree(&tree2, repo,
764 got_object_commit_get_tree_id(commit));
765 if (err)
766 return err;
768 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
769 if (qid != NULL) {
770 struct got_commit_object *pcommit;
772 err = got_object_open_as_commit(&pcommit, repo, qid->id);
773 if (err)
774 return err;
776 err = got_object_open_as_tree(&tree1, repo,
777 got_object_commit_get_tree_id(pcommit));
778 got_object_commit_close(pcommit);
779 if (err)
780 return err;
782 err = got_object_id_str(&id_str1, qid->id);
783 if (err)
784 return err;
787 err = got_object_id_str(&id_str2, id);
788 if (err)
789 goto done;
791 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
792 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
793 done:
794 if (tree1)
795 got_object_tree_close(tree1);
796 got_object_tree_close(tree2);
797 free(id_str1);
798 free(id_str2);
799 return err;
802 static char *
803 get_datestr(time_t *time, char *datebuf)
805 char *p, *s = ctime_r(time, datebuf);
806 p = strchr(s, '\n');
807 if (p)
808 *p = '\0';
809 return s;
812 static const struct got_error *
813 print_commit(struct got_commit_object *commit, struct got_object_id *id,
814 struct got_repository *repo, int show_patch, int diff_context,
815 struct got_reflist_head *refs)
817 const struct got_error *err = NULL;
818 char *id_str, *datestr, *logmsg0, *logmsg, *line;
819 char datebuf[26];
820 time_t committer_time;
821 const char *author, *committer;
822 char *refs_str = NULL;
823 struct got_reflist_entry *re;
825 SIMPLEQ_FOREACH(re, refs, entry) {
826 char *s;
827 const char *name;
828 if (got_object_id_cmp(re->id, id) != 0)
829 continue;
830 name = got_ref_get_name(re->ref);
831 if (strcmp(name, GOT_REF_HEAD) == 0)
832 continue;
833 if (strncmp(name, "refs/", 5) == 0)
834 name += 5;
835 if (strncmp(name, "got/", 4) == 0)
836 continue;
837 if (strncmp(name, "heads/", 6) == 0)
838 name += 6;
839 if (strncmp(name, "remotes/", 8) == 0)
840 name += 8;
841 s = refs_str;
842 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
843 name) == -1) {
844 err = got_error_from_errno("asprintf");
845 free(s);
846 break;
848 free(s);
850 err = got_object_id_str(&id_str, id);
851 if (err)
852 return err;
854 printf("-----------------------------------------------\n");
855 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
856 refs_str ? refs_str : "", refs_str ? ")" : "");
857 free(id_str);
858 id_str = NULL;
859 free(refs_str);
860 refs_str = NULL;
861 printf("from: %s\n", got_object_commit_get_author(commit));
862 committer_time = got_object_commit_get_committer_time(commit);
863 datestr = get_datestr(&committer_time, datebuf);
864 printf("date: %s UTC\n", datestr);
865 author = got_object_commit_get_author(commit);
866 committer = got_object_commit_get_committer(commit);
867 if (strcmp(author, committer) != 0)
868 printf("via: %s\n", committer);
869 if (got_object_commit_get_nparents(commit) > 1) {
870 const struct got_object_id_queue *parent_ids;
871 struct got_object_qid *qid;
872 int n = 1;
873 parent_ids = got_object_commit_get_parent_ids(commit);
874 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
875 err = got_object_id_str(&id_str, qid->id);
876 if (err)
877 return err;
878 printf("parent %d: %s\n", n++, id_str);
879 free(id_str);
883 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
884 if (logmsg0 == NULL)
885 return got_error_from_errno("strdup");
887 logmsg = logmsg0;
888 do {
889 line = strsep(&logmsg, "\n");
890 if (line)
891 printf(" %s\n", line);
892 } while (line);
893 free(logmsg0);
895 if (show_patch) {
896 err = print_patch(commit, id, diff_context, repo);
897 if (err == 0)
898 printf("\n");
901 if (fflush(stdout) != 0 && err == NULL)
902 err = got_error_from_errno("fflush");
903 return err;
906 static const struct got_error *
907 print_commits(struct got_object_id *root_id, struct got_repository *repo,
908 char *path, int show_patch, int diff_context, int limit,
909 int first_parent_traversal, struct got_reflist_head *refs)
911 const struct got_error *err;
912 struct got_commit_graph *graph;
914 err = got_commit_graph_open(&graph, root_id, path,
915 first_parent_traversal, repo);
916 if (err)
917 return err;
918 err = got_commit_graph_iter_start(graph, root_id, repo);
919 if (err)
920 goto done;
921 for (;;) {
922 struct got_commit_object *commit;
923 struct got_object_id *id;
925 if (sigint_received || sigpipe_received)
926 break;
928 err = got_commit_graph_iter_next(&id, graph);
929 if (err) {
930 if (err->code == GOT_ERR_ITER_COMPLETED) {
931 err = NULL;
932 break;
934 if (err->code != GOT_ERR_ITER_NEED_MORE)
935 break;
936 err = got_commit_graph_fetch_commits(graph, 1, repo);
937 if (err)
938 break;
939 else
940 continue;
942 if (id == NULL)
943 break;
945 err = got_object_open_as_commit(&commit, repo, id);
946 if (err)
947 break;
948 err = print_commit(commit, id, repo, show_patch, diff_context,
949 refs);
950 got_object_commit_close(commit);
951 if (err || (limit && --limit == 0))
952 break;
954 done:
955 got_commit_graph_close(graph);
956 return err;
959 __dead static void
960 usage_log(void)
962 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
963 "[-r repository-path] [path]\n", getprogname());
964 exit(1);
967 static const struct got_error *
968 cmd_log(int argc, char *argv[])
970 const struct got_error *error;
971 struct got_repository *repo = NULL;
972 struct got_worktree *worktree = NULL;
973 struct got_commit_object *commit = NULL;
974 struct got_object_id *id = NULL;
975 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
976 char *start_commit = NULL;
977 int diff_context = 3, ch;
978 int show_patch = 0, limit = 0, first_parent_traversal = 0;
979 const char *errstr;
980 struct got_reflist_head refs;
982 SIMPLEQ_INIT(&refs);
984 #ifndef PROFILE
985 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
986 NULL)
987 == -1)
988 err(1, "pledge");
989 #endif
991 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
992 switch (ch) {
993 case 'b':
994 first_parent_traversal = 1;
995 break;
996 case 'p':
997 show_patch = 1;
998 break;
999 case 'c':
1000 start_commit = optarg;
1001 break;
1002 case 'C':
1003 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1004 &errstr);
1005 if (errstr != NULL)
1006 err(1, "-C option %s", errstr);
1007 break;
1008 case 'l':
1009 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1010 if (errstr != NULL)
1011 err(1, "-l option %s", errstr);
1012 break;
1013 case 'r':
1014 repo_path = realpath(optarg, NULL);
1015 if (repo_path == NULL)
1016 err(1, "-r option");
1017 got_path_strip_trailing_slashes(repo_path);
1018 break;
1019 default:
1020 usage_log();
1021 /* NOTREACHED */
1025 argc -= optind;
1026 argv += optind;
1028 cwd = getcwd(NULL, 0);
1029 if (cwd == NULL) {
1030 error = got_error_from_errno("getcwd");
1031 goto done;
1034 error = got_worktree_open(&worktree, cwd);
1035 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1036 goto done;
1037 error = NULL;
1039 if (argc == 0) {
1040 path = strdup("");
1041 if (path == NULL) {
1042 error = got_error_from_errno("strdup");
1043 goto done;
1045 } else if (argc == 1) {
1046 if (worktree) {
1047 error = got_worktree_resolve_path(&path, worktree,
1048 argv[0]);
1049 if (error)
1050 goto done;
1051 } else {
1052 path = strdup(argv[0]);
1053 if (path == NULL) {
1054 error = got_error_from_errno("strdup");
1055 goto done;
1058 } else
1059 usage_log();
1061 if (repo_path == NULL) {
1062 repo_path = worktree ?
1063 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1065 if (repo_path == NULL) {
1066 error = got_error_from_errno("strdup");
1067 goto done;
1070 error = got_repo_open(&repo, repo_path);
1071 if (error != NULL)
1072 goto done;
1074 error = apply_unveil(got_repo_get_path(repo), 1,
1075 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1076 if (error)
1077 goto done;
1079 if (start_commit == NULL) {
1080 struct got_reference *head_ref;
1081 error = got_ref_open(&head_ref, repo,
1082 worktree ? got_worktree_get_head_ref_name(worktree)
1083 : GOT_REF_HEAD, 0);
1084 if (error != NULL)
1085 return error;
1086 error = got_ref_resolve(&id, repo, head_ref);
1087 got_ref_close(head_ref);
1088 if (error != NULL)
1089 return error;
1090 error = got_object_open_as_commit(&commit, repo, id);
1091 } else {
1092 struct got_reference *ref;
1093 error = got_ref_open(&ref, repo, start_commit, 0);
1094 if (error == NULL) {
1095 int obj_type;
1096 error = got_ref_resolve(&id, repo, ref);
1097 got_ref_close(ref);
1098 if (error != NULL)
1099 goto done;
1100 error = got_object_get_type(&obj_type, repo, id);
1101 if (error != NULL)
1102 goto done;
1103 if (obj_type == GOT_OBJ_TYPE_TAG) {
1104 struct got_tag_object *tag;
1105 error = got_object_open_as_tag(&tag, repo, id);
1106 if (error != NULL)
1107 goto done;
1108 if (got_object_tag_get_object_type(tag) !=
1109 GOT_OBJ_TYPE_COMMIT) {
1110 got_object_tag_close(tag);
1111 error = got_error(GOT_ERR_OBJ_TYPE);
1112 goto done;
1114 free(id);
1115 id = got_object_id_dup(
1116 got_object_tag_get_object_id(tag));
1117 if (id == NULL)
1118 error = got_error_from_errno(
1119 "got_object_id_dup");
1120 got_object_tag_close(tag);
1121 if (error)
1122 goto done;
1123 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1124 error = got_error(GOT_ERR_OBJ_TYPE);
1125 goto done;
1127 error = got_object_open_as_commit(&commit, repo, id);
1128 if (error != NULL)
1129 goto done;
1131 if (commit == NULL) {
1132 error = got_object_resolve_id_str(&id, repo,
1133 start_commit);
1134 if (error != NULL)
1135 return error;
1138 if (error != NULL)
1139 goto done;
1141 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1142 if (error != NULL)
1143 goto done;
1144 if (in_repo_path) {
1145 free(path);
1146 path = in_repo_path;
1149 error = got_ref_list(&refs, repo);
1150 if (error)
1151 goto done;
1153 error = print_commits(id, repo, path, show_patch,
1154 diff_context, limit, first_parent_traversal, &refs);
1155 done:
1156 free(path);
1157 free(repo_path);
1158 free(cwd);
1159 free(id);
1160 if (worktree)
1161 got_worktree_close(worktree);
1162 if (repo) {
1163 const struct got_error *repo_error;
1164 repo_error = got_repo_close(repo);
1165 if (error == NULL)
1166 error = repo_error;
1168 got_ref_list_free(&refs);
1169 return error;
1172 __dead static void
1173 usage_diff(void)
1175 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1176 "[object1 object2 | path]\n", getprogname());
1177 exit(1);
1180 struct print_diff_arg {
1181 struct got_repository *repo;
1182 struct got_worktree *worktree;
1183 int diff_context;
1184 const char *id_str;
1185 int header_shown;
1188 static const struct got_error *
1189 print_diff(void *arg, unsigned char status, const char *path,
1190 struct got_object_id *blob_id, struct got_object_id *commit_id)
1192 struct print_diff_arg *a = arg;
1193 const struct got_error *err = NULL;
1194 struct got_blob_object *blob1 = NULL;
1195 FILE *f2 = NULL;
1196 char *abspath = NULL;
1197 struct stat sb;
1199 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1200 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1201 return NULL;
1203 if (!a->header_shown) {
1204 printf("diff %s %s\n", a->id_str,
1205 got_worktree_get_root_path(a->worktree));
1206 a->header_shown = 1;
1209 if (status != GOT_STATUS_ADD) {
1210 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1211 if (err)
1212 goto done;
1216 if (status != GOT_STATUS_DELETE) {
1217 if (asprintf(&abspath, "%s/%s",
1218 got_worktree_get_root_path(a->worktree), path) == -1) {
1219 err = got_error_from_errno("asprintf");
1220 goto done;
1223 f2 = fopen(abspath, "r");
1224 if (f2 == NULL) {
1225 err = got_error_from_errno2("fopen", abspath);
1226 goto done;
1228 if (lstat(abspath, &sb) == -1) {
1229 err = got_error_from_errno2("lstat", abspath);
1230 goto done;
1232 } else
1233 sb.st_size = 0;
1235 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1236 stdout);
1237 done:
1238 if (blob1)
1239 got_object_blob_close(blob1);
1240 if (f2 && fclose(f2) != 0 && err == NULL)
1241 err = got_error_from_errno("fclose");
1242 free(abspath);
1243 return err;
1246 static const struct got_error *
1247 cmd_diff(int argc, char *argv[])
1249 const struct got_error *error;
1250 struct got_repository *repo = NULL;
1251 struct got_worktree *worktree = NULL;
1252 char *cwd = NULL, *repo_path = NULL;
1253 struct got_object_id *id1 = NULL, *id2 = NULL;
1254 const char *id_str1 = NULL, *id_str2 = NULL;
1255 char *label1 = NULL, *label2 = NULL;
1256 int type1, type2;
1257 int diff_context = 3, ch;
1258 const char *errstr;
1259 char *path = NULL;
1261 #ifndef PROFILE
1262 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1263 NULL) == -1)
1264 err(1, "pledge");
1265 #endif
1267 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1268 switch (ch) {
1269 case 'C':
1270 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1271 if (errstr != NULL)
1272 err(1, "-C option %s", errstr);
1273 break;
1274 case 'r':
1275 repo_path = realpath(optarg, NULL);
1276 if (repo_path == NULL)
1277 err(1, "-r option");
1278 got_path_strip_trailing_slashes(repo_path);
1279 break;
1280 default:
1281 usage_diff();
1282 /* NOTREACHED */
1286 argc -= optind;
1287 argv += optind;
1289 cwd = getcwd(NULL, 0);
1290 if (cwd == NULL) {
1291 error = got_error_from_errno("getcwd");
1292 goto done;
1294 error = got_worktree_open(&worktree, cwd);
1295 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1296 goto done;
1297 if (argc <= 1) {
1298 if (worktree == NULL) {
1299 error = got_error(GOT_ERR_NOT_WORKTREE);
1300 goto done;
1302 if (repo_path)
1303 errx(1,
1304 "-r option can't be used when diffing a work tree");
1305 repo_path = strdup(got_worktree_get_repo_path(worktree));
1306 if (repo_path == NULL) {
1307 error = got_error_from_errno("strdup");
1308 goto done;
1310 if (argc == 1) {
1311 error = got_worktree_resolve_path(&path, worktree,
1312 argv[0]);
1313 if (error)
1314 goto done;
1315 } else {
1316 path = strdup("");
1317 if (path == NULL) {
1318 error = got_error_from_errno("strdup");
1319 goto done;
1322 } else if (argc == 2) {
1323 id_str1 = argv[0];
1324 id_str2 = argv[1];
1325 } else
1326 usage_diff();
1328 if (repo_path == NULL) {
1329 repo_path = getcwd(NULL, 0);
1330 if (repo_path == NULL)
1331 return got_error_from_errno("getcwd");
1334 error = got_repo_open(&repo, repo_path);
1335 free(repo_path);
1336 if (error != NULL)
1337 goto done;
1339 error = apply_unveil(got_repo_get_path(repo), 1,
1340 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1341 if (error)
1342 goto done;
1344 if (worktree) {
1345 struct print_diff_arg arg;
1346 char *id_str;
1347 error = got_object_id_str(&id_str,
1348 got_worktree_get_base_commit_id(worktree));
1349 if (error)
1350 goto done;
1351 arg.repo = repo;
1352 arg.worktree = worktree;
1353 arg.diff_context = diff_context;
1354 arg.id_str = id_str;
1355 arg.header_shown = 0;
1357 error = got_worktree_status(worktree, path, repo, print_diff,
1358 &arg, check_cancelled, NULL);
1359 free(id_str);
1360 goto done;
1363 error = got_object_resolve_id_str(&id1, repo, id_str1);
1364 if (error) {
1365 struct got_reference *ref;
1366 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1367 goto done;
1368 error = got_ref_open(&ref, repo, id_str1, 0);
1369 if (error != NULL)
1370 goto done;
1371 label1 = strdup(got_ref_get_name(ref));
1372 if (label1 == NULL) {
1373 error = got_error_from_errno("strdup");
1374 goto done;
1376 error = got_ref_resolve(&id1, repo, ref);
1377 got_ref_close(ref);
1378 if (error != NULL)
1379 goto done;
1380 } else {
1381 label1 = strdup(id_str1);
1382 if (label1 == NULL) {
1383 error = got_error_from_errno("strdup");
1384 goto done;
1388 error = got_object_resolve_id_str(&id2, repo, id_str2);
1389 if (error) {
1390 struct got_reference *ref;
1391 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1392 goto done;
1393 error = got_ref_open(&ref, repo, id_str2, 0);
1394 if (error != NULL)
1395 goto done;
1396 label2 = strdup(got_ref_get_name(ref));
1397 if (label2 == NULL) {
1398 error = got_error_from_errno("strdup");
1399 goto done;
1401 error = got_ref_resolve(&id2, repo, ref);
1402 got_ref_close(ref);
1403 if (error != NULL)
1404 goto done;
1405 } else {
1406 label2 = strdup(id_str2);
1407 if (label2 == NULL) {
1408 error = got_error_from_errno("strdup");
1409 goto done;
1413 error = got_object_get_type(&type1, repo, id1);
1414 if (error)
1415 goto done;
1417 error = got_object_get_type(&type2, repo, id2);
1418 if (error)
1419 goto done;
1421 if (type1 != type2) {
1422 error = got_error(GOT_ERR_OBJ_TYPE);
1423 goto done;
1426 switch (type1) {
1427 case GOT_OBJ_TYPE_BLOB:
1428 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1429 diff_context, repo, stdout);
1430 break;
1431 case GOT_OBJ_TYPE_TREE:
1432 error = got_diff_objects_as_trees(id1, id2, "", "",
1433 diff_context, repo, stdout);
1434 break;
1435 case GOT_OBJ_TYPE_COMMIT:
1436 printf("diff %s %s\n", label1, label2);
1437 error = got_diff_objects_as_commits(id1, id2, diff_context,
1438 repo, stdout);
1439 break;
1440 default:
1441 error = got_error(GOT_ERR_OBJ_TYPE);
1444 done:
1445 free(label1);
1446 free(label2);
1447 free(id1);
1448 free(id2);
1449 free(path);
1450 if (worktree)
1451 got_worktree_close(worktree);
1452 if (repo) {
1453 const struct got_error *repo_error;
1454 repo_error = got_repo_close(repo);
1455 if (error == NULL)
1456 error = repo_error;
1458 return error;
1461 __dead static void
1462 usage_blame(void)
1464 fprintf(stderr,
1465 "usage: %s blame [-c commit] [-r repository-path] path\n",
1466 getprogname());
1467 exit(1);
1470 static const struct got_error *
1471 cmd_blame(int argc, char *argv[])
1473 const struct got_error *error;
1474 struct got_repository *repo = NULL;
1475 struct got_worktree *worktree = NULL;
1476 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1477 struct got_object_id *commit_id = NULL;
1478 char *commit_id_str = NULL;
1479 int ch;
1481 #ifndef PROFILE
1482 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1483 NULL) == -1)
1484 err(1, "pledge");
1485 #endif
1487 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1488 switch (ch) {
1489 case 'c':
1490 commit_id_str = optarg;
1491 break;
1492 case 'r':
1493 repo_path = realpath(optarg, NULL);
1494 if (repo_path == NULL)
1495 err(1, "-r option");
1496 got_path_strip_trailing_slashes(repo_path);
1497 break;
1498 default:
1499 usage_blame();
1500 /* NOTREACHED */
1504 argc -= optind;
1505 argv += optind;
1507 if (argc == 1)
1508 path = argv[0];
1509 else
1510 usage_blame();
1512 cwd = getcwd(NULL, 0);
1513 if (cwd == NULL) {
1514 error = got_error_from_errno("getcwd");
1515 goto done;
1517 if (repo_path == NULL) {
1518 error = got_worktree_open(&worktree, cwd);
1519 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1520 goto done;
1521 else
1522 error = NULL;
1523 if (worktree) {
1524 repo_path =
1525 strdup(got_worktree_get_repo_path(worktree));
1526 if (repo_path == NULL)
1527 error = got_error_from_errno("strdup");
1528 if (error)
1529 goto done;
1530 } else {
1531 repo_path = strdup(cwd);
1532 if (repo_path == NULL) {
1533 error = got_error_from_errno("strdup");
1534 goto done;
1539 error = got_repo_open(&repo, repo_path);
1540 if (error != NULL)
1541 goto done;
1543 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1544 if (error)
1545 goto done;
1547 if (worktree) {
1548 const char *prefix = got_worktree_get_path_prefix(worktree);
1549 char *p, *worktree_subdir = cwd +
1550 strlen(got_worktree_get_root_path(worktree));
1551 if (asprintf(&p, "%s%s%s%s%s",
1552 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1553 worktree_subdir, worktree_subdir[0] ? "/" : "",
1554 path) == -1) {
1555 error = got_error_from_errno("asprintf");
1556 goto done;
1558 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1559 free(p);
1560 } else {
1561 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1563 if (error)
1564 goto done;
1566 if (commit_id_str == NULL) {
1567 struct got_reference *head_ref;
1568 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1569 if (error != NULL)
1570 goto done;
1571 error = got_ref_resolve(&commit_id, repo, head_ref);
1572 got_ref_close(head_ref);
1573 if (error != NULL)
1574 goto done;
1575 } else {
1576 error = got_object_resolve_id_str(&commit_id, repo,
1577 commit_id_str);
1578 if (error != NULL)
1579 goto done;
1582 error = got_blame(in_repo_path, commit_id, repo, stdout);
1583 done:
1584 free(in_repo_path);
1585 free(repo_path);
1586 free(cwd);
1587 free(commit_id);
1588 if (worktree)
1589 got_worktree_close(worktree);
1590 if (repo) {
1591 const struct got_error *repo_error;
1592 repo_error = got_repo_close(repo);
1593 if (error == NULL)
1594 error = repo_error;
1596 return error;
1599 __dead static void
1600 usage_tree(void)
1602 fprintf(stderr,
1603 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1604 getprogname());
1605 exit(1);
1608 static void
1609 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1610 const char *root_path)
1612 int is_root_path = (strcmp(path, root_path) == 0);
1614 path += strlen(root_path);
1615 while (path[0] == '/')
1616 path++;
1618 printf("%s%s%s%s%s\n", id ? id : "", path,
1619 is_root_path ? "" : "/", te->name,
1620 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1623 static const struct got_error *
1624 print_tree(const char *path, struct got_object_id *commit_id,
1625 int show_ids, int recurse, const char *root_path,
1626 struct got_repository *repo)
1628 const struct got_error *err = NULL;
1629 struct got_object_id *tree_id = NULL;
1630 struct got_tree_object *tree = NULL;
1631 const struct got_tree_entries *entries;
1632 struct got_tree_entry *te;
1634 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1635 if (err)
1636 goto done;
1638 err = got_object_open_as_tree(&tree, repo, tree_id);
1639 if (err)
1640 goto done;
1641 entries = got_object_tree_get_entries(tree);
1642 te = SIMPLEQ_FIRST(&entries->head);
1643 while (te) {
1644 char *id = NULL;
1646 if (sigint_received || sigpipe_received)
1647 break;
1649 if (show_ids) {
1650 char *id_str;
1651 err = got_object_id_str(&id_str, te->id);
1652 if (err)
1653 goto done;
1654 if (asprintf(&id, "%s ", id_str) == -1) {
1655 err = got_error_from_errno("asprintf");
1656 free(id_str);
1657 goto done;
1659 free(id_str);
1661 print_entry(te, id, path, root_path);
1662 free(id);
1664 if (recurse && S_ISDIR(te->mode)) {
1665 char *child_path;
1666 if (asprintf(&child_path, "%s%s%s", path,
1667 path[0] == '/' && path[1] == '\0' ? "" : "/",
1668 te->name) == -1) {
1669 err = got_error_from_errno("asprintf");
1670 goto done;
1672 err = print_tree(child_path, commit_id, show_ids, 1,
1673 root_path, repo);
1674 free(child_path);
1675 if (err)
1676 goto done;
1679 te = SIMPLEQ_NEXT(te, entry);
1681 done:
1682 if (tree)
1683 got_object_tree_close(tree);
1684 free(tree_id);
1685 return err;
1688 static const struct got_error *
1689 cmd_tree(int argc, char *argv[])
1691 const struct got_error *error;
1692 struct got_repository *repo = NULL;
1693 struct got_worktree *worktree = NULL;
1694 const char *path;
1695 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1696 struct got_object_id *commit_id = NULL;
1697 char *commit_id_str = NULL;
1698 int show_ids = 0, recurse = 0;
1699 int ch;
1701 #ifndef PROFILE
1702 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1703 NULL) == -1)
1704 err(1, "pledge");
1705 #endif
1707 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1708 switch (ch) {
1709 case 'c':
1710 commit_id_str = optarg;
1711 break;
1712 case 'r':
1713 repo_path = realpath(optarg, NULL);
1714 if (repo_path == NULL)
1715 err(1, "-r option");
1716 got_path_strip_trailing_slashes(repo_path);
1717 break;
1718 case 'i':
1719 show_ids = 1;
1720 break;
1721 case 'R':
1722 recurse = 1;
1723 break;
1724 default:
1725 usage_tree();
1726 /* NOTREACHED */
1730 argc -= optind;
1731 argv += optind;
1733 if (argc == 1)
1734 path = argv[0];
1735 else if (argc > 1)
1736 usage_tree();
1737 else
1738 path = NULL;
1740 cwd = getcwd(NULL, 0);
1741 if (cwd == NULL) {
1742 error = got_error_from_errno("getcwd");
1743 goto done;
1745 if (repo_path == NULL) {
1746 error = got_worktree_open(&worktree, cwd);
1747 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1748 goto done;
1749 else
1750 error = NULL;
1751 if (worktree) {
1752 repo_path =
1753 strdup(got_worktree_get_repo_path(worktree));
1754 if (repo_path == NULL)
1755 error = got_error_from_errno("strdup");
1756 if (error)
1757 goto done;
1758 } else {
1759 repo_path = strdup(cwd);
1760 if (repo_path == NULL) {
1761 error = got_error_from_errno("strdup");
1762 goto done;
1767 error = got_repo_open(&repo, repo_path);
1768 if (error != NULL)
1769 goto done;
1771 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1772 if (error)
1773 goto done;
1775 if (path == NULL) {
1776 if (worktree) {
1777 char *p, *worktree_subdir = cwd +
1778 strlen(got_worktree_get_root_path(worktree));
1779 if (asprintf(&p, "%s/%s",
1780 got_worktree_get_path_prefix(worktree),
1781 worktree_subdir) == -1) {
1782 error = got_error_from_errno("asprintf");
1783 goto done;
1785 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1786 free(p);
1787 if (error)
1788 goto done;
1789 } else
1790 path = "/";
1792 if (in_repo_path == NULL) {
1793 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1794 if (error != NULL)
1795 goto done;
1798 if (commit_id_str == NULL) {
1799 struct got_reference *head_ref;
1800 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1801 if (error != NULL)
1802 goto done;
1803 error = got_ref_resolve(&commit_id, repo, head_ref);
1804 got_ref_close(head_ref);
1805 if (error != NULL)
1806 goto done;
1807 } else {
1808 error = got_object_resolve_id_str(&commit_id, repo,
1809 commit_id_str);
1810 if (error != NULL)
1811 goto done;
1814 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1815 in_repo_path, repo);
1816 done:
1817 free(in_repo_path);
1818 free(repo_path);
1819 free(cwd);
1820 free(commit_id);
1821 if (worktree)
1822 got_worktree_close(worktree);
1823 if (repo) {
1824 const struct got_error *repo_error;
1825 repo_error = got_repo_close(repo);
1826 if (error == NULL)
1827 error = repo_error;
1829 return error;
1832 __dead static void
1833 usage_status(void)
1835 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1836 exit(1);
1839 static const struct got_error *
1840 print_status(void *arg, unsigned char status, const char *path,
1841 struct got_object_id *blob_id, struct got_object_id *commit_id)
1843 printf("%c %s\n", status, path);
1844 return NULL;
1847 static const struct got_error *
1848 cmd_status(int argc, char *argv[])
1850 const struct got_error *error = NULL;
1851 struct got_repository *repo = NULL;
1852 struct got_worktree *worktree = NULL;
1853 char *cwd = NULL, *path = NULL;
1854 int ch;
1856 while ((ch = getopt(argc, argv, "")) != -1) {
1857 switch (ch) {
1858 default:
1859 usage_status();
1860 /* NOTREACHED */
1864 argc -= optind;
1865 argv += optind;
1867 #ifndef PROFILE
1868 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1869 NULL) == -1)
1870 err(1, "pledge");
1871 #endif
1872 cwd = getcwd(NULL, 0);
1873 if (cwd == NULL) {
1874 error = got_error_from_errno("getcwd");
1875 goto done;
1878 error = got_worktree_open(&worktree, cwd);
1879 if (error != NULL)
1880 goto done;
1882 if (argc == 0) {
1883 path = strdup("");
1884 if (path == NULL) {
1885 error = got_error_from_errno("strdup");
1886 goto done;
1888 } else if (argc == 1) {
1889 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1890 if (error)
1891 goto done;
1892 } else
1893 usage_status();
1895 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1896 if (error != NULL)
1897 goto done;
1899 error = apply_unveil(got_repo_get_path(repo), 1,
1900 got_worktree_get_root_path(worktree), 0);
1901 if (error)
1902 goto done;
1904 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1905 check_cancelled, NULL);
1906 done:
1907 free(cwd);
1908 free(path);
1909 return error;
1912 __dead static void
1913 usage_ref(void)
1915 fprintf(stderr,
1916 "usage: %s ref [-r repository] -l | -d name | name target\n",
1917 getprogname());
1918 exit(1);
1921 static const struct got_error *
1922 list_refs(struct got_repository *repo)
1924 static const struct got_error *err = NULL;
1925 struct got_reflist_head refs;
1926 struct got_reflist_entry *re;
1928 SIMPLEQ_INIT(&refs);
1929 err = got_ref_list(&refs, repo);
1930 if (err)
1931 return err;
1933 SIMPLEQ_FOREACH(re, &refs, entry) {
1934 char *refstr;
1935 refstr = got_ref_to_str(re->ref);
1936 if (refstr == NULL)
1937 return got_error_from_errno("got_ref_to_str");
1938 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1939 free(refstr);
1942 got_ref_list_free(&refs);
1943 return NULL;
1946 static const struct got_error *
1947 delete_ref(struct got_repository *repo, const char *refname)
1949 const struct got_error *err = NULL;
1950 struct got_reference *ref;
1952 err = got_ref_open(&ref, repo, refname, 0);
1953 if (err)
1954 return err;
1956 err = got_ref_delete(ref, repo);
1957 got_ref_close(ref);
1958 return err;
1961 static const struct got_error *
1962 add_ref(struct got_repository *repo, const char *refname, const char *target)
1964 const struct got_error *err = NULL;
1965 struct got_object_id *id;
1966 struct got_reference *ref = NULL;
1968 err = got_object_resolve_id_str(&id, repo, target);
1969 if (err) {
1970 struct got_reference *target_ref;
1972 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1973 return err;
1974 err = got_ref_open(&target_ref, repo, target, 0);
1975 if (err)
1976 return err;
1977 err = got_ref_resolve(&id, repo, target_ref);
1978 got_ref_close(target_ref);
1979 if (err)
1980 return err;
1983 err = got_ref_alloc(&ref, refname, id);
1984 if (err)
1985 goto done;
1987 err = got_ref_write(ref, repo);
1988 done:
1989 if (ref)
1990 got_ref_close(ref);
1991 free(id);
1992 return err;
1995 static const struct got_error *
1996 cmd_ref(int argc, char *argv[])
1998 const struct got_error *error = NULL;
1999 struct got_repository *repo = NULL;
2000 struct got_worktree *worktree = NULL;
2001 char *cwd = NULL, *repo_path = NULL;
2002 int ch, do_list = 0;
2003 const char *delref = NULL;
2005 /* TODO: Add -s option for adding symbolic references. */
2006 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2007 switch (ch) {
2008 case 'd':
2009 delref = optarg;
2010 break;
2011 case 'r':
2012 repo_path = realpath(optarg, NULL);
2013 if (repo_path == NULL)
2014 err(1, "-r option");
2015 got_path_strip_trailing_slashes(repo_path);
2016 break;
2017 case 'l':
2018 do_list = 1;
2019 break;
2020 default:
2021 usage_ref();
2022 /* NOTREACHED */
2026 if (do_list && delref)
2027 errx(1, "-l and -d options are mutually exclusive\n");
2029 argc -= optind;
2030 argv += optind;
2032 if (do_list || delref) {
2033 if (argc > 0)
2034 usage_ref();
2035 } else if (argc != 2)
2036 usage_ref();
2038 #ifndef PROFILE
2039 if (do_list) {
2040 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2041 NULL) == -1)
2042 err(1, "pledge");
2043 } else {
2044 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2045 "sendfd unveil", NULL) == -1)
2046 err(1, "pledge");
2048 #endif
2049 cwd = getcwd(NULL, 0);
2050 if (cwd == NULL) {
2051 error = got_error_from_errno("getcwd");
2052 goto done;
2055 if (repo_path == NULL) {
2056 error = got_worktree_open(&worktree, cwd);
2057 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2058 goto done;
2059 else
2060 error = NULL;
2061 if (worktree) {
2062 repo_path =
2063 strdup(got_worktree_get_repo_path(worktree));
2064 if (repo_path == NULL)
2065 error = got_error_from_errno("strdup");
2066 if (error)
2067 goto done;
2068 } else {
2069 repo_path = strdup(cwd);
2070 if (repo_path == NULL) {
2071 error = got_error_from_errno("strdup");
2072 goto done;
2077 error = got_repo_open(&repo, repo_path);
2078 if (error != NULL)
2079 goto done;
2081 error = apply_unveil(got_repo_get_path(repo), do_list,
2082 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2083 if (error)
2084 goto done;
2086 if (do_list)
2087 error = list_refs(repo);
2088 else if (delref)
2089 error = delete_ref(repo, delref);
2090 else
2091 error = add_ref(repo, argv[0], argv[1]);
2092 done:
2093 if (repo)
2094 got_repo_close(repo);
2095 if (worktree)
2096 got_worktree_close(worktree);
2097 free(cwd);
2098 free(repo_path);
2099 return error;
2102 __dead static void
2103 usage_add(void)
2105 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2106 exit(1);
2109 static const struct got_error *
2110 cmd_add(int argc, char *argv[])
2112 const struct got_error *error = NULL;
2113 struct got_repository *repo = NULL;
2114 struct got_worktree *worktree = NULL;
2115 char *cwd = NULL;
2116 struct got_pathlist_head paths;
2117 struct got_pathlist_entry *pe;
2118 int ch, x;
2120 TAILQ_INIT(&paths);
2122 while ((ch = getopt(argc, argv, "")) != -1) {
2123 switch (ch) {
2124 default:
2125 usage_add();
2126 /* NOTREACHED */
2130 argc -= optind;
2131 argv += optind;
2133 if (argc < 1)
2134 usage_add();
2136 /* make sure each file exists before doing anything halfway */
2137 for (x = 0; x < argc; x++) {
2138 char *path = realpath(argv[x], NULL);
2139 if (path == NULL) {
2140 error = got_error_from_errno2("realpath", argv[x]);
2141 goto done;
2143 free(path);
2146 cwd = getcwd(NULL, 0);
2147 if (cwd == NULL) {
2148 error = got_error_from_errno("getcwd");
2149 goto done;
2152 error = got_worktree_open(&worktree, cwd);
2153 if (error)
2154 goto done;
2156 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2157 if (error != NULL)
2158 goto done;
2160 error = apply_unveil(got_repo_get_path(repo), 1,
2161 got_worktree_get_root_path(worktree), 0);
2162 if (error)
2163 goto done;
2165 for (x = 0; x < argc; x++) {
2166 char *path = realpath(argv[x], NULL);
2167 if (path == NULL) {
2168 error = got_error_from_errno2("realpath", argv[x]);
2169 goto done;
2172 got_path_strip_trailing_slashes(path);
2173 error = got_pathlist_insert(&pe, &paths, path, NULL);
2174 if (error) {
2175 free(path);
2176 goto done;
2179 error = got_worktree_schedule_add(worktree, &paths, print_status,
2180 NULL, repo);
2181 done:
2182 if (repo)
2183 got_repo_close(repo);
2184 if (worktree)
2185 got_worktree_close(worktree);
2186 TAILQ_FOREACH(pe, &paths, entry)
2187 free((char *)pe->path);
2188 got_pathlist_free(&paths);
2189 free(cwd);
2190 return error;
2193 __dead static void
2194 usage_rm(void)
2196 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2197 exit(1);
2200 static const struct got_error *
2201 cmd_rm(int argc, char *argv[])
2203 const struct got_error *error = NULL;
2204 struct got_worktree *worktree = NULL;
2205 struct got_repository *repo = NULL;
2206 char *cwd = NULL, *path = NULL;
2207 int ch, delete_local_mods = 0;
2209 while ((ch = getopt(argc, argv, "f")) != -1) {
2210 switch (ch) {
2211 case 'f':
2212 delete_local_mods = 1;
2213 break;
2214 default:
2215 usage_add();
2216 /* NOTREACHED */
2220 argc -= optind;
2221 argv += optind;
2223 if (argc != 1)
2224 usage_rm();
2226 path = realpath(argv[0], NULL);
2227 if (path == NULL) {
2228 error = got_error_from_errno2("realpath", argv[0]);
2229 goto done;
2231 got_path_strip_trailing_slashes(path);
2233 cwd = getcwd(NULL, 0);
2234 if (cwd == NULL) {
2235 error = got_error_from_errno("getcwd");
2236 goto done;
2238 error = got_worktree_open(&worktree, cwd);
2239 if (error)
2240 goto done;
2242 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2243 if (error)
2244 goto done;
2246 error = apply_unveil(got_repo_get_path(repo), 1,
2247 got_worktree_get_root_path(worktree), 0);
2248 if (error)
2249 goto done;
2251 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2252 print_status, NULL, repo);
2253 if (error)
2254 goto done;
2255 done:
2256 if (repo)
2257 got_repo_close(repo);
2258 if (worktree)
2259 got_worktree_close(worktree);
2260 free(path);
2261 free(cwd);
2262 return error;
2265 __dead static void
2266 usage_revert(void)
2268 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2269 exit(1);
2272 static void
2273 revert_progress(void *arg, unsigned char status, const char *path)
2275 while (path[0] == '/')
2276 path++;
2277 printf("%c %s\n", status, path);
2280 static const struct got_error *
2281 cmd_revert(int argc, char *argv[])
2283 const struct got_error *error = NULL;
2284 struct got_worktree *worktree = NULL;
2285 struct got_repository *repo = NULL;
2286 char *cwd = NULL, *path = NULL;
2287 int ch;
2289 while ((ch = getopt(argc, argv, "")) != -1) {
2290 switch (ch) {
2291 default:
2292 usage_revert();
2293 /* NOTREACHED */
2297 argc -= optind;
2298 argv += optind;
2300 if (argc != 1)
2301 usage_revert();
2303 path = realpath(argv[0], NULL);
2304 if (path == NULL) {
2305 error = got_error_from_errno2("realpath", argv[0]);
2306 goto done;
2308 got_path_strip_trailing_slashes(path);
2310 cwd = getcwd(NULL, 0);
2311 if (cwd == NULL) {
2312 error = got_error_from_errno("getcwd");
2313 goto done;
2315 error = got_worktree_open(&worktree, cwd);
2316 if (error)
2317 goto done;
2319 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2320 if (error != NULL)
2321 goto done;
2323 error = apply_unveil(got_repo_get_path(repo), 1,
2324 got_worktree_get_root_path(worktree), 0);
2325 if (error)
2326 goto done;
2328 error = got_worktree_revert(worktree, path,
2329 revert_progress, NULL, repo);
2330 if (error)
2331 goto done;
2332 done:
2333 if (repo)
2334 got_repo_close(repo);
2335 if (worktree)
2336 got_worktree_close(worktree);
2337 free(path);
2338 free(cwd);
2339 return error;
2342 __dead static void
2343 usage_commit(void)
2345 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2346 exit(1);
2349 int
2350 spawn_editor(const char *editor, const char *file)
2352 pid_t pid;
2353 sig_t sighup, sigint, sigquit;
2354 int st = -1;
2356 sighup = signal(SIGHUP, SIG_IGN);
2357 sigint = signal(SIGINT, SIG_IGN);
2358 sigquit = signal(SIGQUIT, SIG_IGN);
2360 switch (pid = fork()) {
2361 case -1:
2362 goto doneediting;
2363 case 0:
2364 execl(editor, editor, file, (char *)NULL);
2365 _exit(127);
2368 while (waitpid(pid, &st, 0) == -1)
2369 if (errno != EINTR)
2370 break;
2372 doneediting:
2373 (void)signal(SIGHUP, sighup);
2374 (void)signal(SIGINT, sigint);
2375 (void)signal(SIGQUIT, sigquit);
2377 if (!WIFEXITED(st)) {
2378 errno = EINTR;
2379 return -1;
2382 return WEXITSTATUS(st);
2385 struct collect_commit_logmsg_arg {
2386 const char *cmdline_log;
2387 const char *editor;
2388 const char *worktree_path;
2389 const char *repo_path;
2390 char *logmsg_path;
2394 static const struct got_error *
2395 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2396 void *arg)
2398 const char *initial_content = "\n# changes to be committed:\n";
2399 struct got_pathlist_entry *pe;
2400 const struct got_error *err = NULL;
2401 char *template = NULL;
2402 struct collect_commit_logmsg_arg *a = arg;
2403 char buf[1024];
2404 struct stat st, st2;
2405 FILE *fp;
2406 size_t len;
2407 int fd, content_changed = 0;
2409 /* if a message was specified on the command line, just use it */
2410 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2411 len = strlen(a->cmdline_log) + 1;
2412 *logmsg = malloc(len + 1);
2413 if (*logmsg == NULL)
2414 return got_error_from_errno("malloc");
2415 strlcpy(*logmsg, a->cmdline_log, len);
2416 return NULL;
2419 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2420 return got_error_from_errno("asprintf");
2422 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2423 if (err)
2424 goto done;
2426 dprintf(fd, initial_content);
2428 TAILQ_FOREACH(pe, commitable_paths, entry) {
2429 struct got_commitable *ct = pe->data;
2430 dprintf(fd, "# %c %s\n",
2431 got_commitable_get_status(ct),
2432 got_commitable_get_path(ct));
2434 close(fd);
2436 if (stat(a->logmsg_path, &st) == -1) {
2437 err = got_error_from_errno2("stat", a->logmsg_path);
2438 goto done;
2441 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2442 err = got_error_from_errno("failed spawning editor");
2443 goto done;
2446 if (stat(a->logmsg_path, &st2) == -1) {
2447 err = got_error_from_errno("stat");
2448 goto done;
2451 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2452 unlink(a->logmsg_path);
2453 free(a->logmsg_path);
2454 a->logmsg_path = NULL;
2455 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2456 "no changes made to commit message, aborting");
2457 goto done;
2460 *logmsg = malloc(st2.st_size + 1);
2461 if (*logmsg == NULL) {
2462 err = got_error_from_errno("malloc");
2463 goto done;
2465 (*logmsg)[0] = '\0';
2466 len = 0;
2468 fp = fopen(a->logmsg_path, "r");
2469 if (fp == NULL) {
2470 err = got_error_from_errno("fopen");
2471 goto done;
2473 while (fgets(buf, sizeof(buf), fp) != NULL) {
2474 if (!content_changed && strcmp(buf, initial_content) != 0)
2475 content_changed = 1;
2476 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2477 continue; /* remove comments and leading empty lines */
2478 len = strlcat(*logmsg, buf, st2.st_size);
2480 fclose(fp);
2482 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2483 (*logmsg)[len - 1] = '\0';
2484 len--;
2487 if (len == 0 || !content_changed) {
2488 unlink(a->logmsg_path);
2489 free(a->logmsg_path);
2490 a->logmsg_path = NULL;
2491 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2492 "commit message cannot be empty, aborting");
2493 goto done;
2495 done:
2496 free(template);
2498 /* Editor is done; we can now apply unveil(2) */
2499 if (err == NULL)
2500 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2501 return err;
2504 static const struct got_error *
2505 cmd_commit(int argc, char *argv[])
2507 const struct got_error *error = NULL;
2508 struct got_worktree *worktree = NULL;
2509 struct got_repository *repo = NULL;
2510 char *cwd = NULL, *path = NULL, *id_str = NULL;
2511 struct got_object_id *id = NULL;
2512 const char *logmsg = NULL;
2513 const char *got_author = getenv("GOT_AUTHOR");
2514 struct collect_commit_logmsg_arg cl_arg;
2515 char *editor = NULL;
2516 int ch;
2518 while ((ch = getopt(argc, argv, "m:")) != -1) {
2519 switch (ch) {
2520 case 'm':
2521 logmsg = optarg;
2522 break;
2523 default:
2524 usage_commit();
2525 /* NOTREACHED */
2529 argc -= optind;
2530 argv += optind;
2532 if (argc == 1) {
2533 path = realpath(argv[0], NULL);
2534 if (path == NULL) {
2535 error = got_error_from_errno2("realpath", argv[0]);
2536 goto done;
2538 got_path_strip_trailing_slashes(path);
2539 } else if (argc != 0)
2540 usage_commit();
2542 if (got_author == NULL) {
2543 /* TODO: Look current user up in password database */
2544 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2545 goto done;
2548 cwd = getcwd(NULL, 0);
2549 if (cwd == NULL) {
2550 error = got_error_from_errno("getcwd");
2551 goto done;
2553 error = got_worktree_open(&worktree, cwd);
2554 if (error)
2555 goto done;
2557 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2558 if (error != NULL)
2559 goto done;
2562 * unveil(2) traverses exec(2); if an editor is used we have
2563 * to apply unveil after the log message has been written.
2565 if (logmsg == NULL || strlen(logmsg) == 0)
2566 error = get_editor(&editor);
2567 else
2568 error = apply_unveil(got_repo_get_path(repo), 0,
2569 got_worktree_get_root_path(worktree), 0);
2570 if (error)
2571 goto done;
2573 cl_arg.editor = editor;
2574 cl_arg.cmdline_log = logmsg;
2575 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2576 cl_arg.repo_path = got_repo_get_path(repo);
2577 cl_arg.logmsg_path = NULL;
2578 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2579 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2580 if (error) {
2581 if (cl_arg.logmsg_path)
2582 fprintf(stderr, "%s: log message preserved in %s\n",
2583 getprogname(), cl_arg.logmsg_path);
2584 goto done;
2587 if (cl_arg.logmsg_path)
2588 unlink(cl_arg.logmsg_path);
2590 error = got_object_id_str(&id_str, id);
2591 if (error)
2592 goto done;
2593 printf("created commit %s\n", id_str);
2594 done:
2595 if (repo)
2596 got_repo_close(repo);
2597 if (worktree)
2598 got_worktree_close(worktree);
2599 free(path);
2600 free(cwd);
2601 free(id_str);
2602 free(editor);
2603 return error;