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 cmd_update(int argc, char *argv[])
570 const struct got_error *error = NULL;
571 struct got_repository *repo = NULL;
572 struct got_worktree *worktree = NULL;
573 char *worktree_path = NULL, *path = NULL;
574 struct got_object_id *commit_id = NULL;
575 char *commit_id_str = NULL;
576 const char *branch_name = NULL;
577 struct got_reference *head_ref = NULL;
578 int ch, did_something = 0;
580 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
581 switch (ch) {
582 case 'b':
583 branch_name = optarg;
584 break;
585 case 'c':
586 commit_id_str = strdup(optarg);
587 if (commit_id_str == NULL)
588 return got_error_from_errno("strdup");
589 break;
590 default:
591 usage_update();
592 /* NOTREACHED */
596 argc -= optind;
597 argv += optind;
599 #ifndef PROFILE
600 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
601 "unveil", NULL) == -1)
602 err(1, "pledge");
603 #endif
604 worktree_path = getcwd(NULL, 0);
605 if (worktree_path == NULL) {
606 error = got_error_from_errno("getcwd");
607 goto done;
609 error = got_worktree_open(&worktree, worktree_path);
610 if (error)
611 goto done;
613 if (argc == 0) {
614 path = strdup("");
615 if (path == NULL) {
616 error = got_error_from_errno("strdup");
617 goto done;
619 } else if (argc == 1) {
620 error = got_worktree_resolve_path(&path, worktree, argv[0]);
621 if (error)
622 goto done;
623 } else
624 usage_update();
626 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
627 if (error != NULL)
628 goto done;
630 error = apply_unveil(got_repo_get_path(repo), 0,
631 got_worktree_get_root_path(worktree), 0);
632 if (error)
633 goto done;
635 if (branch_name == NULL)
636 branch_name = got_worktree_get_head_ref_name(worktree);
637 error = got_ref_open(&head_ref, repo, branch_name, 0);
638 if (error != NULL)
639 goto done;
640 if (commit_id_str == NULL) {
641 error = got_ref_resolve(&commit_id, repo, head_ref);
642 if (error != NULL)
643 goto done;
644 error = got_object_id_str(&commit_id_str, commit_id);
645 if (error != NULL)
646 goto done;
647 } else {
648 error = got_object_resolve_id_str(&commit_id, repo,
649 commit_id_str);
650 if (error != NULL)
651 goto done;
654 if (strcmp(got_ref_get_name(head_ref),
655 got_worktree_get_head_ref_name(worktree)) != 0) {
656 struct got_object_id *head_commit_id;
657 if (strlen(path) != 0) {
658 fprintf(stderr, "%s: switching to a different "
659 "branch requires that the entire work tree "
660 "gets updated, not just '%s'\n",
661 getprogname(), path);
662 error = got_error(GOT_ERR_BAD_PATH);
663 goto done;
665 error = got_ref_resolve(&head_commit_id, repo, head_ref);
666 if (error)
667 goto done;
668 error = check_linear_ancestry(commit_id, head_commit_id, repo);
669 free(head_commit_id);
670 if (error != NULL)
671 goto done;
672 error = check_same_branch(commit_id, head_ref, repo);
673 if (error)
674 goto done;
675 printf("Switching work tree from %s to %s\n",
676 got_worktree_get_head_ref_name(worktree),
677 got_ref_get_name(head_ref));
678 error = got_worktree_set_head_ref(worktree, head_ref);
679 if (error)
680 goto done;
681 } else {
682 error = check_linear_ancestry(commit_id,
683 got_worktree_get_base_commit_id(worktree), repo);
684 if (error != NULL) {
685 if (error->code == GOT_ERR_ANCESTRY)
686 error = got_error(GOT_ERR_BRANCH_MOVED);
687 goto done;
689 error = check_same_branch(commit_id, head_ref, repo);
690 if (error)
691 goto done;
694 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
695 commit_id) != 0) {
696 error = got_worktree_set_base_commit_id(worktree, repo,
697 commit_id);
698 if (error)
699 goto done;
702 error = got_worktree_checkout_files(worktree, path, repo,
703 update_progress, &did_something, check_cancelled, NULL);
704 if (error != NULL)
705 goto done;
707 if (did_something)
708 printf("Updated to commit %s\n", commit_id_str);
709 else
710 printf("Already up-to-date\n");
711 done:
712 free(worktree_path);
713 free(path);
714 free(commit_id);
715 free(commit_id_str);
716 return error;
719 static const struct got_error *
720 print_patch(struct got_commit_object *commit, struct got_object_id *id,
721 int diff_context, struct got_repository *repo)
723 const struct got_error *err = NULL;
724 struct got_tree_object *tree1 = NULL, *tree2;
725 struct got_object_qid *qid;
726 char *id_str1 = NULL, *id_str2;
728 err = got_object_open_as_tree(&tree2, repo,
729 got_object_commit_get_tree_id(commit));
730 if (err)
731 return err;
733 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
734 if (qid != NULL) {
735 struct got_commit_object *pcommit;
737 err = got_object_open_as_commit(&pcommit, repo, qid->id);
738 if (err)
739 return err;
741 err = got_object_open_as_tree(&tree1, repo,
742 got_object_commit_get_tree_id(pcommit));
743 got_object_commit_close(pcommit);
744 if (err)
745 return err;
747 err = got_object_id_str(&id_str1, qid->id);
748 if (err)
749 return err;
752 err = got_object_id_str(&id_str2, id);
753 if (err)
754 goto done;
756 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
757 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
758 done:
759 if (tree1)
760 got_object_tree_close(tree1);
761 got_object_tree_close(tree2);
762 free(id_str1);
763 free(id_str2);
764 return err;
767 static char *
768 get_datestr(time_t *time, char *datebuf)
770 char *p, *s = ctime_r(time, datebuf);
771 p = strchr(s, '\n');
772 if (p)
773 *p = '\0';
774 return s;
777 static const struct got_error *
778 print_commit(struct got_commit_object *commit, struct got_object_id *id,
779 struct got_repository *repo, int show_patch, int diff_context,
780 struct got_reflist_head *refs)
782 const struct got_error *err = NULL;
783 char *id_str, *datestr, *logmsg0, *logmsg, *line;
784 char datebuf[26];
785 time_t committer_time;
786 const char *author, *committer;
787 char *refs_str = NULL;
788 struct got_reflist_entry *re;
790 SIMPLEQ_FOREACH(re, refs, entry) {
791 char *s;
792 const char *name;
793 if (got_object_id_cmp(re->id, id) != 0)
794 continue;
795 name = got_ref_get_name(re->ref);
796 if (strcmp(name, GOT_REF_HEAD) == 0)
797 continue;
798 if (strncmp(name, "refs/", 5) == 0)
799 name += 5;
800 if (strncmp(name, "got/", 4) == 0)
801 continue;
802 if (strncmp(name, "heads/", 6) == 0)
803 name += 6;
804 if (strncmp(name, "remotes/", 8) == 0)
805 name += 8;
806 s = refs_str;
807 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
808 name) == -1) {
809 err = got_error_from_errno("asprintf");
810 free(s);
811 break;
813 free(s);
815 err = got_object_id_str(&id_str, id);
816 if (err)
817 return err;
819 printf("-----------------------------------------------\n");
820 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
821 refs_str ? refs_str : "", refs_str ? ")" : "");
822 free(id_str);
823 id_str = NULL;
824 free(refs_str);
825 refs_str = NULL;
826 printf("from: %s\n", got_object_commit_get_author(commit));
827 committer_time = got_object_commit_get_committer_time(commit);
828 datestr = get_datestr(&committer_time, datebuf);
829 printf("date: %s UTC\n", datestr);
830 author = got_object_commit_get_author(commit);
831 committer = got_object_commit_get_committer(commit);
832 if (strcmp(author, committer) != 0)
833 printf("via: %s\n", committer);
834 if (got_object_commit_get_nparents(commit) > 1) {
835 const struct got_object_id_queue *parent_ids;
836 struct got_object_qid *qid;
837 int n = 1;
838 parent_ids = got_object_commit_get_parent_ids(commit);
839 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
840 err = got_object_id_str(&id_str, qid->id);
841 if (err)
842 return err;
843 printf("parent %d: %s\n", n++, id_str);
844 free(id_str);
848 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
849 if (logmsg0 == NULL)
850 return got_error_from_errno("strdup");
852 logmsg = logmsg0;
853 do {
854 line = strsep(&logmsg, "\n");
855 if (line)
856 printf(" %s\n", line);
857 } while (line);
858 free(logmsg0);
860 if (show_patch) {
861 err = print_patch(commit, id, diff_context, repo);
862 if (err == 0)
863 printf("\n");
866 if (fflush(stdout) != 0 && err == NULL)
867 err = got_error_from_errno("fflush");
868 return err;
871 static const struct got_error *
872 print_commits(struct got_object_id *root_id, struct got_repository *repo,
873 char *path, int show_patch, int diff_context, int limit,
874 int first_parent_traversal, struct got_reflist_head *refs)
876 const struct got_error *err;
877 struct got_commit_graph *graph;
879 err = got_commit_graph_open(&graph, root_id, path,
880 first_parent_traversal, repo);
881 if (err)
882 return err;
883 err = got_commit_graph_iter_start(graph, root_id, repo);
884 if (err)
885 goto done;
886 for (;;) {
887 struct got_commit_object *commit;
888 struct got_object_id *id;
890 if (sigint_received || sigpipe_received)
891 break;
893 err = got_commit_graph_iter_next(&id, graph);
894 if (err) {
895 if (err->code == GOT_ERR_ITER_COMPLETED) {
896 err = NULL;
897 break;
899 if (err->code != GOT_ERR_ITER_NEED_MORE)
900 break;
901 err = got_commit_graph_fetch_commits(graph, 1, repo);
902 if (err)
903 break;
904 else
905 continue;
907 if (id == NULL)
908 break;
910 err = got_object_open_as_commit(&commit, repo, id);
911 if (err)
912 break;
913 err = print_commit(commit, id, repo, show_patch, diff_context,
914 refs);
915 got_object_commit_close(commit);
916 if (err || (limit && --limit == 0))
917 break;
919 done:
920 got_commit_graph_close(graph);
921 return err;
924 __dead static void
925 usage_log(void)
927 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
928 "[-r repository-path] [path]\n", getprogname());
929 exit(1);
932 static const struct got_error *
933 cmd_log(int argc, char *argv[])
935 const struct got_error *error;
936 struct got_repository *repo = NULL;
937 struct got_worktree *worktree = NULL;
938 struct got_commit_object *commit = NULL;
939 struct got_object_id *id = NULL;
940 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
941 char *start_commit = NULL;
942 int diff_context = 3, ch;
943 int show_patch = 0, limit = 0, first_parent_traversal = 0;
944 const char *errstr;
945 struct got_reflist_head refs;
947 SIMPLEQ_INIT(&refs);
949 #ifndef PROFILE
950 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
951 NULL)
952 == -1)
953 err(1, "pledge");
954 #endif
956 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
957 switch (ch) {
958 case 'b':
959 first_parent_traversal = 1;
960 break;
961 case 'p':
962 show_patch = 1;
963 break;
964 case 'c':
965 start_commit = optarg;
966 break;
967 case 'C':
968 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
969 &errstr);
970 if (errstr != NULL)
971 err(1, "-C option %s", errstr);
972 break;
973 case 'l':
974 limit = strtonum(optarg, 1, INT_MAX, &errstr);
975 if (errstr != NULL)
976 err(1, "-l option %s", errstr);
977 break;
978 case 'r':
979 repo_path = realpath(optarg, NULL);
980 if (repo_path == NULL)
981 err(1, "-r option");
982 got_path_strip_trailing_slashes(repo_path);
983 break;
984 default:
985 usage_log();
986 /* NOTREACHED */
990 argc -= optind;
991 argv += optind;
993 cwd = getcwd(NULL, 0);
994 if (cwd == NULL) {
995 error = got_error_from_errno("getcwd");
996 goto done;
999 error = got_worktree_open(&worktree, cwd);
1000 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1001 goto done;
1002 error = NULL;
1004 if (argc == 0) {
1005 path = strdup("");
1006 if (path == NULL) {
1007 error = got_error_from_errno("strdup");
1008 goto done;
1010 } else if (argc == 1) {
1011 if (worktree) {
1012 error = got_worktree_resolve_path(&path, worktree,
1013 argv[0]);
1014 if (error)
1015 goto done;
1016 } else {
1017 path = strdup(argv[0]);
1018 if (path == NULL) {
1019 error = got_error_from_errno("strdup");
1020 goto done;
1023 } else
1024 usage_log();
1026 if (repo_path == NULL) {
1027 repo_path = worktree ?
1028 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1030 if (repo_path == NULL) {
1031 error = got_error_from_errno("strdup");
1032 goto done;
1035 error = got_repo_open(&repo, repo_path);
1036 if (error != NULL)
1037 goto done;
1039 error = apply_unveil(got_repo_get_path(repo), 1,
1040 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1041 if (error)
1042 goto done;
1044 if (start_commit == NULL) {
1045 struct got_reference *head_ref;
1046 error = got_ref_open(&head_ref, repo,
1047 worktree ? got_worktree_get_head_ref_name(worktree)
1048 : GOT_REF_HEAD, 0);
1049 if (error != NULL)
1050 return error;
1051 error = got_ref_resolve(&id, repo, head_ref);
1052 got_ref_close(head_ref);
1053 if (error != NULL)
1054 return error;
1055 error = got_object_open_as_commit(&commit, repo, id);
1056 } else {
1057 struct got_reference *ref;
1058 error = got_ref_open(&ref, repo, start_commit, 0);
1059 if (error == NULL) {
1060 int obj_type;
1061 error = got_ref_resolve(&id, repo, ref);
1062 got_ref_close(ref);
1063 if (error != NULL)
1064 goto done;
1065 error = got_object_get_type(&obj_type, repo, id);
1066 if (error != NULL)
1067 goto done;
1068 if (obj_type == GOT_OBJ_TYPE_TAG) {
1069 struct got_tag_object *tag;
1070 error = got_object_open_as_tag(&tag, repo, id);
1071 if (error != NULL)
1072 goto done;
1073 if (got_object_tag_get_object_type(tag) !=
1074 GOT_OBJ_TYPE_COMMIT) {
1075 got_object_tag_close(tag);
1076 error = got_error(GOT_ERR_OBJ_TYPE);
1077 goto done;
1079 free(id);
1080 id = got_object_id_dup(
1081 got_object_tag_get_object_id(tag));
1082 if (id == NULL)
1083 error = got_error_from_errno(
1084 "got_object_id_dup");
1085 got_object_tag_close(tag);
1086 if (error)
1087 goto done;
1088 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1089 error = got_error(GOT_ERR_OBJ_TYPE);
1090 goto done;
1092 error = got_object_open_as_commit(&commit, repo, id);
1093 if (error != NULL)
1094 goto done;
1096 if (commit == NULL) {
1097 error = got_object_resolve_id_str(&id, repo,
1098 start_commit);
1099 if (error != NULL)
1100 return error;
1103 if (error != NULL)
1104 goto done;
1106 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1107 if (error != NULL)
1108 goto done;
1109 if (in_repo_path) {
1110 free(path);
1111 path = in_repo_path;
1114 error = got_ref_list(&refs, repo);
1115 if (error)
1116 goto done;
1118 error = print_commits(id, repo, path, show_patch,
1119 diff_context, limit, first_parent_traversal, &refs);
1120 done:
1121 free(path);
1122 free(repo_path);
1123 free(cwd);
1124 free(id);
1125 if (worktree)
1126 got_worktree_close(worktree);
1127 if (repo) {
1128 const struct got_error *repo_error;
1129 repo_error = got_repo_close(repo);
1130 if (error == NULL)
1131 error = repo_error;
1133 got_ref_list_free(&refs);
1134 return error;
1137 __dead static void
1138 usage_diff(void)
1140 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1141 "[object1 object2 | path]\n", getprogname());
1142 exit(1);
1145 struct print_diff_arg {
1146 struct got_repository *repo;
1147 struct got_worktree *worktree;
1148 int diff_context;
1149 const char *id_str;
1150 int header_shown;
1153 static const struct got_error *
1154 print_diff(void *arg, unsigned char status, const char *path,
1155 struct got_object_id *blob_id, struct got_object_id *commit_id)
1157 struct print_diff_arg *a = arg;
1158 const struct got_error *err = NULL;
1159 struct got_blob_object *blob1 = NULL;
1160 FILE *f2 = NULL;
1161 char *abspath = NULL;
1162 struct stat sb;
1164 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1165 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1166 return NULL;
1168 if (!a->header_shown) {
1169 printf("diff %s %s\n", a->id_str,
1170 got_worktree_get_root_path(a->worktree));
1171 a->header_shown = 1;
1174 if (status != GOT_STATUS_ADD) {
1175 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1176 if (err)
1177 goto done;
1181 if (status != GOT_STATUS_DELETE) {
1182 if (asprintf(&abspath, "%s/%s",
1183 got_worktree_get_root_path(a->worktree), path) == -1) {
1184 err = got_error_from_errno("asprintf");
1185 goto done;
1188 f2 = fopen(abspath, "r");
1189 if (f2 == NULL) {
1190 err = got_error_from_errno2("fopen", abspath);
1191 goto done;
1193 if (lstat(abspath, &sb) == -1) {
1194 err = got_error_from_errno2("lstat", abspath);
1195 goto done;
1197 } else
1198 sb.st_size = 0;
1200 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1201 stdout);
1202 done:
1203 if (blob1)
1204 got_object_blob_close(blob1);
1205 if (f2 && fclose(f2) != 0 && err == NULL)
1206 err = got_error_from_errno("fclose");
1207 free(abspath);
1208 return err;
1211 static const struct got_error *
1212 cmd_diff(int argc, char *argv[])
1214 const struct got_error *error;
1215 struct got_repository *repo = NULL;
1216 struct got_worktree *worktree = NULL;
1217 char *cwd = NULL, *repo_path = NULL;
1218 struct got_object_id *id1 = NULL, *id2 = NULL;
1219 char *id_str1 = NULL, *id_str2 = NULL;
1220 int type1, type2;
1221 int diff_context = 3, ch;
1222 const char *errstr;
1223 char *path = NULL;
1225 #ifndef PROFILE
1226 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1227 NULL) == -1)
1228 err(1, "pledge");
1229 #endif
1231 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1232 switch (ch) {
1233 case 'C':
1234 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1235 if (errstr != NULL)
1236 err(1, "-C option %s", errstr);
1237 break;
1238 case 'r':
1239 repo_path = realpath(optarg, NULL);
1240 if (repo_path == NULL)
1241 err(1, "-r option");
1242 got_path_strip_trailing_slashes(repo_path);
1243 break;
1244 default:
1245 usage_diff();
1246 /* NOTREACHED */
1250 argc -= optind;
1251 argv += optind;
1253 cwd = getcwd(NULL, 0);
1254 if (cwd == NULL) {
1255 error = got_error_from_errno("getcwd");
1256 goto done;
1258 error = got_worktree_open(&worktree, cwd);
1259 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1260 goto done;
1261 if (argc <= 1) {
1262 if (worktree == NULL) {
1263 error = got_error(GOT_ERR_NOT_WORKTREE);
1264 goto done;
1266 if (repo_path)
1267 errx(1,
1268 "-r option can't be used when diffing a work tree");
1269 repo_path = strdup(got_worktree_get_repo_path(worktree));
1270 if (repo_path == NULL) {
1271 error = got_error_from_errno("strdup");
1272 goto done;
1274 if (argc == 1) {
1275 error = got_worktree_resolve_path(&path, worktree,
1276 argv[0]);
1277 if (error)
1278 goto done;
1279 } else {
1280 path = strdup("");
1281 if (path == NULL) {
1282 error = got_error_from_errno("strdup");
1283 goto done;
1286 } else if (argc == 2) {
1287 id_str1 = argv[0];
1288 id_str2 = argv[1];
1289 } else
1290 usage_diff();
1292 if (repo_path == NULL) {
1293 repo_path = getcwd(NULL, 0);
1294 if (repo_path == NULL)
1295 return got_error_from_errno("getcwd");
1298 error = got_repo_open(&repo, repo_path);
1299 free(repo_path);
1300 if (error != NULL)
1301 goto done;
1303 error = apply_unveil(got_repo_get_path(repo), 1,
1304 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1305 if (error)
1306 goto done;
1308 if (worktree) {
1309 struct print_diff_arg arg;
1310 char *id_str;
1311 error = got_object_id_str(&id_str,
1312 got_worktree_get_base_commit_id(worktree));
1313 if (error)
1314 goto done;
1315 arg.repo = repo;
1316 arg.worktree = worktree;
1317 arg.diff_context = diff_context;
1318 arg.id_str = id_str;
1319 arg.header_shown = 0;
1321 error = got_worktree_status(worktree, path, repo, print_diff,
1322 &arg, check_cancelled, NULL);
1323 free(id_str);
1324 goto done;
1327 error = got_object_resolve_id_str(&id1, repo, id_str1);
1328 if (error)
1329 goto done;
1331 error = got_object_resolve_id_str(&id2, repo, id_str2);
1332 if (error)
1333 goto done;
1335 error = got_object_get_type(&type1, repo, id1);
1336 if (error)
1337 goto done;
1339 error = got_object_get_type(&type2, repo, id2);
1340 if (error)
1341 goto done;
1343 if (type1 != type2) {
1344 error = got_error(GOT_ERR_OBJ_TYPE);
1345 goto done;
1348 switch (type1) {
1349 case GOT_OBJ_TYPE_BLOB:
1350 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1351 diff_context, repo, stdout);
1352 break;
1353 case GOT_OBJ_TYPE_TREE:
1354 error = got_diff_objects_as_trees(id1, id2, "", "",
1355 diff_context, repo, stdout);
1356 break;
1357 case GOT_OBJ_TYPE_COMMIT:
1358 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1359 id_str2);
1360 error = got_diff_objects_as_commits(id1, id2, diff_context,
1361 repo, stdout);
1362 break;
1363 default:
1364 error = got_error(GOT_ERR_OBJ_TYPE);
1367 done:
1368 free(id1);
1369 free(id2);
1370 free(path);
1371 if (worktree)
1372 got_worktree_close(worktree);
1373 if (repo) {
1374 const struct got_error *repo_error;
1375 repo_error = got_repo_close(repo);
1376 if (error == NULL)
1377 error = repo_error;
1379 return error;
1382 __dead static void
1383 usage_blame(void)
1385 fprintf(stderr,
1386 "usage: %s blame [-c commit] [-r repository-path] path\n",
1387 getprogname());
1388 exit(1);
1391 static const struct got_error *
1392 cmd_blame(int argc, char *argv[])
1394 const struct got_error *error;
1395 struct got_repository *repo = NULL;
1396 struct got_worktree *worktree = NULL;
1397 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1398 struct got_object_id *commit_id = NULL;
1399 char *commit_id_str = NULL;
1400 int ch;
1402 #ifndef PROFILE
1403 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1404 NULL) == -1)
1405 err(1, "pledge");
1406 #endif
1408 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1409 switch (ch) {
1410 case 'c':
1411 commit_id_str = optarg;
1412 break;
1413 case 'r':
1414 repo_path = realpath(optarg, NULL);
1415 if (repo_path == NULL)
1416 err(1, "-r option");
1417 got_path_strip_trailing_slashes(repo_path);
1418 break;
1419 default:
1420 usage_blame();
1421 /* NOTREACHED */
1425 argc -= optind;
1426 argv += optind;
1428 if (argc == 1)
1429 path = argv[0];
1430 else
1431 usage_blame();
1433 cwd = getcwd(NULL, 0);
1434 if (cwd == NULL) {
1435 error = got_error_from_errno("getcwd");
1436 goto done;
1438 if (repo_path == NULL) {
1439 error = got_worktree_open(&worktree, cwd);
1440 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1441 goto done;
1442 else
1443 error = NULL;
1444 if (worktree) {
1445 repo_path =
1446 strdup(got_worktree_get_repo_path(worktree));
1447 if (repo_path == NULL)
1448 error = got_error_from_errno("strdup");
1449 if (error)
1450 goto done;
1451 } else {
1452 repo_path = strdup(cwd);
1453 if (repo_path == NULL) {
1454 error = got_error_from_errno("strdup");
1455 goto done;
1460 error = got_repo_open(&repo, repo_path);
1461 if (error != NULL)
1462 goto done;
1464 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1465 if (error)
1466 goto done;
1468 if (worktree) {
1469 const char *prefix = got_worktree_get_path_prefix(worktree);
1470 char *p, *worktree_subdir = cwd +
1471 strlen(got_worktree_get_root_path(worktree));
1472 if (asprintf(&p, "%s%s%s%s%s",
1473 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1474 worktree_subdir, worktree_subdir[0] ? "/" : "",
1475 path) == -1) {
1476 error = got_error_from_errno("asprintf");
1477 goto done;
1479 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1480 free(p);
1481 } else {
1482 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1484 if (error)
1485 goto done;
1487 if (commit_id_str == NULL) {
1488 struct got_reference *head_ref;
1489 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1490 if (error != NULL)
1491 goto done;
1492 error = got_ref_resolve(&commit_id, repo, head_ref);
1493 got_ref_close(head_ref);
1494 if (error != NULL)
1495 goto done;
1496 } else {
1497 error = got_object_resolve_id_str(&commit_id, repo,
1498 commit_id_str);
1499 if (error != NULL)
1500 goto done;
1503 error = got_blame(in_repo_path, commit_id, repo, stdout);
1504 done:
1505 free(in_repo_path);
1506 free(repo_path);
1507 free(cwd);
1508 free(commit_id);
1509 if (worktree)
1510 got_worktree_close(worktree);
1511 if (repo) {
1512 const struct got_error *repo_error;
1513 repo_error = got_repo_close(repo);
1514 if (error == NULL)
1515 error = repo_error;
1517 return error;
1520 __dead static void
1521 usage_tree(void)
1523 fprintf(stderr,
1524 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1525 getprogname());
1526 exit(1);
1529 static void
1530 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1531 const char *root_path)
1533 int is_root_path = (strcmp(path, root_path) == 0);
1535 path += strlen(root_path);
1536 while (path[0] == '/')
1537 path++;
1539 printf("%s%s%s%s%s\n", id ? id : "", path,
1540 is_root_path ? "" : "/", te->name,
1541 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1544 static const struct got_error *
1545 print_tree(const char *path, struct got_object_id *commit_id,
1546 int show_ids, int recurse, const char *root_path,
1547 struct got_repository *repo)
1549 const struct got_error *err = NULL;
1550 struct got_object_id *tree_id = NULL;
1551 struct got_tree_object *tree = NULL;
1552 const struct got_tree_entries *entries;
1553 struct got_tree_entry *te;
1555 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1556 if (err)
1557 goto done;
1559 err = got_object_open_as_tree(&tree, repo, tree_id);
1560 if (err)
1561 goto done;
1562 entries = got_object_tree_get_entries(tree);
1563 te = SIMPLEQ_FIRST(&entries->head);
1564 while (te) {
1565 char *id = NULL;
1567 if (sigint_received || sigpipe_received)
1568 break;
1570 if (show_ids) {
1571 char *id_str;
1572 err = got_object_id_str(&id_str, te->id);
1573 if (err)
1574 goto done;
1575 if (asprintf(&id, "%s ", id_str) == -1) {
1576 err = got_error_from_errno("asprintf");
1577 free(id_str);
1578 goto done;
1580 free(id_str);
1582 print_entry(te, id, path, root_path);
1583 free(id);
1585 if (recurse && S_ISDIR(te->mode)) {
1586 char *child_path;
1587 if (asprintf(&child_path, "%s%s%s", path,
1588 path[0] == '/' && path[1] == '\0' ? "" : "/",
1589 te->name) == -1) {
1590 err = got_error_from_errno("asprintf");
1591 goto done;
1593 err = print_tree(child_path, commit_id, show_ids, 1,
1594 root_path, repo);
1595 free(child_path);
1596 if (err)
1597 goto done;
1600 te = SIMPLEQ_NEXT(te, entry);
1602 done:
1603 if (tree)
1604 got_object_tree_close(tree);
1605 free(tree_id);
1606 return err;
1609 static const struct got_error *
1610 cmd_tree(int argc, char *argv[])
1612 const struct got_error *error;
1613 struct got_repository *repo = NULL;
1614 struct got_worktree *worktree = NULL;
1615 const char *path;
1616 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1617 struct got_object_id *commit_id = NULL;
1618 char *commit_id_str = NULL;
1619 int show_ids = 0, recurse = 0;
1620 int ch;
1622 #ifndef PROFILE
1623 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1624 NULL) == -1)
1625 err(1, "pledge");
1626 #endif
1628 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1629 switch (ch) {
1630 case 'c':
1631 commit_id_str = optarg;
1632 break;
1633 case 'r':
1634 repo_path = realpath(optarg, NULL);
1635 if (repo_path == NULL)
1636 err(1, "-r option");
1637 got_path_strip_trailing_slashes(repo_path);
1638 break;
1639 case 'i':
1640 show_ids = 1;
1641 break;
1642 case 'R':
1643 recurse = 1;
1644 break;
1645 default:
1646 usage_tree();
1647 /* NOTREACHED */
1651 argc -= optind;
1652 argv += optind;
1654 if (argc == 1)
1655 path = argv[0];
1656 else if (argc > 1)
1657 usage_tree();
1658 else
1659 path = NULL;
1661 cwd = getcwd(NULL, 0);
1662 if (cwd == NULL) {
1663 error = got_error_from_errno("getcwd");
1664 goto done;
1666 if (repo_path == NULL) {
1667 error = got_worktree_open(&worktree, cwd);
1668 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1669 goto done;
1670 else
1671 error = NULL;
1672 if (worktree) {
1673 repo_path =
1674 strdup(got_worktree_get_repo_path(worktree));
1675 if (repo_path == NULL)
1676 error = got_error_from_errno("strdup");
1677 if (error)
1678 goto done;
1679 } else {
1680 repo_path = strdup(cwd);
1681 if (repo_path == NULL) {
1682 error = got_error_from_errno("strdup");
1683 goto done;
1688 error = got_repo_open(&repo, repo_path);
1689 if (error != NULL)
1690 goto done;
1692 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1693 if (error)
1694 goto done;
1696 if (path == NULL) {
1697 if (worktree) {
1698 char *p, *worktree_subdir = cwd +
1699 strlen(got_worktree_get_root_path(worktree));
1700 if (asprintf(&p, "%s/%s",
1701 got_worktree_get_path_prefix(worktree),
1702 worktree_subdir) == -1) {
1703 error = got_error_from_errno("asprintf");
1704 goto done;
1706 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1707 free(p);
1708 if (error)
1709 goto done;
1710 } else
1711 path = "/";
1713 if (in_repo_path == NULL) {
1714 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1715 if (error != NULL)
1716 goto done;
1719 if (commit_id_str == NULL) {
1720 struct got_reference *head_ref;
1721 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1722 if (error != NULL)
1723 goto done;
1724 error = got_ref_resolve(&commit_id, repo, head_ref);
1725 got_ref_close(head_ref);
1726 if (error != NULL)
1727 goto done;
1728 } else {
1729 error = got_object_resolve_id_str(&commit_id, repo,
1730 commit_id_str);
1731 if (error != NULL)
1732 goto done;
1735 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1736 in_repo_path, repo);
1737 done:
1738 free(in_repo_path);
1739 free(repo_path);
1740 free(cwd);
1741 free(commit_id);
1742 if (worktree)
1743 got_worktree_close(worktree);
1744 if (repo) {
1745 const struct got_error *repo_error;
1746 repo_error = got_repo_close(repo);
1747 if (error == NULL)
1748 error = repo_error;
1750 return error;
1753 __dead static void
1754 usage_status(void)
1756 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1757 exit(1);
1760 static const struct got_error *
1761 print_status(void *arg, unsigned char status, const char *path,
1762 struct got_object_id *blob_id, struct got_object_id *commit_id)
1764 printf("%c %s\n", status, path);
1765 return NULL;
1768 static const struct got_error *
1769 cmd_status(int argc, char *argv[])
1771 const struct got_error *error = NULL;
1772 struct got_repository *repo = NULL;
1773 struct got_worktree *worktree = NULL;
1774 char *cwd = NULL, *path = NULL;
1775 int ch;
1777 while ((ch = getopt(argc, argv, "")) != -1) {
1778 switch (ch) {
1779 default:
1780 usage_status();
1781 /* NOTREACHED */
1785 argc -= optind;
1786 argv += optind;
1788 #ifndef PROFILE
1789 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1790 NULL) == -1)
1791 err(1, "pledge");
1792 #endif
1793 cwd = getcwd(NULL, 0);
1794 if (cwd == NULL) {
1795 error = got_error_from_errno("getcwd");
1796 goto done;
1799 error = got_worktree_open(&worktree, cwd);
1800 if (error != NULL)
1801 goto done;
1803 if (argc == 0) {
1804 path = strdup("");
1805 if (path == NULL) {
1806 error = got_error_from_errno("strdup");
1807 goto done;
1809 } else if (argc == 1) {
1810 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1811 if (error)
1812 goto done;
1813 } else
1814 usage_status();
1816 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1817 if (error != NULL)
1818 goto done;
1820 error = apply_unveil(got_repo_get_path(repo), 1,
1821 got_worktree_get_root_path(worktree), 0);
1822 if (error)
1823 goto done;
1825 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1826 check_cancelled, NULL);
1827 done:
1828 free(cwd);
1829 free(path);
1830 return error;
1833 __dead static void
1834 usage_ref(void)
1836 fprintf(stderr,
1837 "usage: %s ref [-r repository] -l | -d name | name target\n",
1838 getprogname());
1839 exit(1);
1842 static const struct got_error *
1843 list_refs(struct got_repository *repo)
1845 static const struct got_error *err = NULL;
1846 struct got_reflist_head refs;
1847 struct got_reflist_entry *re;
1849 SIMPLEQ_INIT(&refs);
1850 err = got_ref_list(&refs, repo);
1851 if (err)
1852 return err;
1854 SIMPLEQ_FOREACH(re, &refs, entry) {
1855 char *refstr;
1856 refstr = got_ref_to_str(re->ref);
1857 if (refstr == NULL)
1858 return got_error_from_errno("got_ref_to_str");
1859 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1860 free(refstr);
1863 got_ref_list_free(&refs);
1864 return NULL;
1867 static const struct got_error *
1868 delete_ref(struct got_repository *repo, const char *refname)
1870 const struct got_error *err = NULL;
1871 struct got_reference *ref;
1873 err = got_ref_open(&ref, repo, refname, 0);
1874 if (err)
1875 return err;
1877 err = got_ref_delete(ref, repo);
1878 got_ref_close(ref);
1879 return err;
1882 static const struct got_error *
1883 add_ref(struct got_repository *repo, const char *refname, const char *target)
1885 const struct got_error *err = NULL;
1886 struct got_object_id *id;
1887 struct got_reference *ref = NULL;
1889 err = got_object_resolve_id_str(&id, repo, target);
1890 if (err) {
1891 struct got_reference *target_ref;
1893 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1894 return err;
1895 err = got_ref_open(&target_ref, repo, target, 0);
1896 if (err)
1897 return err;
1898 err = got_ref_resolve(&id, repo, target_ref);
1899 got_ref_close(target_ref);
1900 if (err)
1901 return err;
1904 err = got_ref_alloc(&ref, refname, id);
1905 if (err)
1906 goto done;
1908 err = got_ref_write(ref, repo);
1909 done:
1910 if (ref)
1911 got_ref_close(ref);
1912 free(id);
1913 return err;
1916 static const struct got_error *
1917 cmd_ref(int argc, char *argv[])
1919 const struct got_error *error = NULL;
1920 struct got_repository *repo = NULL;
1921 struct got_worktree *worktree = NULL;
1922 char *cwd = NULL, *repo_path = NULL;
1923 int ch, do_list = 0;
1924 const char *delref = NULL;
1926 /* TODO: Add -s option for adding symbolic references. */
1927 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1928 switch (ch) {
1929 case 'd':
1930 delref = optarg;
1931 break;
1932 case 'r':
1933 repo_path = realpath(optarg, NULL);
1934 if (repo_path == NULL)
1935 err(1, "-r option");
1936 got_path_strip_trailing_slashes(repo_path);
1937 break;
1938 case 'l':
1939 do_list = 1;
1940 break;
1941 default:
1942 usage_ref();
1943 /* NOTREACHED */
1947 if (do_list && delref)
1948 errx(1, "-l and -d options are mutually exclusive\n");
1950 argc -= optind;
1951 argv += optind;
1953 if (do_list || delref) {
1954 if (argc > 0)
1955 usage_ref();
1956 } else if (argc != 2)
1957 usage_ref();
1959 #ifndef PROFILE
1960 if (do_list) {
1961 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1962 NULL) == -1)
1963 err(1, "pledge");
1964 } else {
1965 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1966 "sendfd unveil", NULL) == -1)
1967 err(1, "pledge");
1969 #endif
1970 cwd = getcwd(NULL, 0);
1971 if (cwd == NULL) {
1972 error = got_error_from_errno("getcwd");
1973 goto done;
1976 if (repo_path == NULL) {
1977 error = got_worktree_open(&worktree, cwd);
1978 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1979 goto done;
1980 else
1981 error = NULL;
1982 if (worktree) {
1983 repo_path =
1984 strdup(got_worktree_get_repo_path(worktree));
1985 if (repo_path == NULL)
1986 error = got_error_from_errno("strdup");
1987 if (error)
1988 goto done;
1989 } else {
1990 repo_path = strdup(cwd);
1991 if (repo_path == NULL) {
1992 error = got_error_from_errno("strdup");
1993 goto done;
1998 error = got_repo_open(&repo, repo_path);
1999 if (error != NULL)
2000 goto done;
2002 error = apply_unveil(got_repo_get_path(repo), do_list,
2003 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2004 if (error)
2005 goto done;
2007 if (do_list)
2008 error = list_refs(repo);
2009 else if (delref)
2010 error = delete_ref(repo, delref);
2011 else
2012 error = add_ref(repo, argv[0], argv[1]);
2013 done:
2014 if (repo)
2015 got_repo_close(repo);
2016 if (worktree)
2017 got_worktree_close(worktree);
2018 free(cwd);
2019 free(repo_path);
2020 return error;
2023 __dead static void
2024 usage_add(void)
2026 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2027 exit(1);
2030 static const struct got_error *
2031 cmd_add(int argc, char *argv[])
2033 const struct got_error *error = NULL;
2034 struct got_repository *repo = NULL;
2035 struct got_worktree *worktree = NULL;
2036 char *cwd = NULL;
2037 struct got_pathlist_head paths;
2038 struct got_pathlist_entry *pe;
2039 int ch, x;
2041 TAILQ_INIT(&paths);
2043 while ((ch = getopt(argc, argv, "")) != -1) {
2044 switch (ch) {
2045 default:
2046 usage_add();
2047 /* NOTREACHED */
2051 argc -= optind;
2052 argv += optind;
2054 if (argc < 1)
2055 usage_add();
2057 /* make sure each file exists before doing anything halfway */
2058 for (x = 0; x < argc; x++) {
2059 char *path = realpath(argv[x], NULL);
2060 if (path == NULL) {
2061 error = got_error_from_errno2("realpath", argv[x]);
2062 goto done;
2064 free(path);
2067 cwd = getcwd(NULL, 0);
2068 if (cwd == NULL) {
2069 error = got_error_from_errno("getcwd");
2070 goto done;
2073 error = got_worktree_open(&worktree, cwd);
2074 if (error)
2075 goto done;
2077 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2078 if (error != NULL)
2079 goto done;
2081 error = apply_unveil(got_repo_get_path(repo), 1,
2082 got_worktree_get_root_path(worktree), 0);
2083 if (error)
2084 goto done;
2086 for (x = 0; x < argc; x++) {
2087 char *path = realpath(argv[x], NULL);
2088 if (path == NULL) {
2089 error = got_error_from_errno2("realpath", argv[x]);
2090 goto done;
2093 got_path_strip_trailing_slashes(path);
2094 error = got_pathlist_insert(&pe, &paths, path, NULL);
2095 if (error) {
2096 free(path);
2097 goto done;
2100 error = got_worktree_schedule_add(worktree, &paths, print_status,
2101 NULL, repo);
2102 done:
2103 if (repo)
2104 got_repo_close(repo);
2105 if (worktree)
2106 got_worktree_close(worktree);
2107 TAILQ_FOREACH(pe, &paths, entry)
2108 free((char *)pe->path);
2109 got_pathlist_free(&paths);
2110 free(cwd);
2111 return error;
2114 __dead static void
2115 usage_rm(void)
2117 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2118 exit(1);
2121 static const struct got_error *
2122 cmd_rm(int argc, char *argv[])
2124 const struct got_error *error = NULL;
2125 struct got_worktree *worktree = NULL;
2126 struct got_repository *repo = NULL;
2127 char *cwd = NULL, *path = NULL;
2128 int ch, delete_local_mods = 0;
2130 while ((ch = getopt(argc, argv, "f")) != -1) {
2131 switch (ch) {
2132 case 'f':
2133 delete_local_mods = 1;
2134 break;
2135 default:
2136 usage_add();
2137 /* NOTREACHED */
2141 argc -= optind;
2142 argv += optind;
2144 if (argc != 1)
2145 usage_rm();
2147 path = realpath(argv[0], NULL);
2148 if (path == NULL) {
2149 error = got_error_from_errno2("realpath", argv[0]);
2150 goto done;
2152 got_path_strip_trailing_slashes(path);
2154 cwd = getcwd(NULL, 0);
2155 if (cwd == NULL) {
2156 error = got_error_from_errno("getcwd");
2157 goto done;
2159 error = got_worktree_open(&worktree, cwd);
2160 if (error)
2161 goto done;
2163 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2164 if (error)
2165 goto done;
2167 error = apply_unveil(got_repo_get_path(repo), 1,
2168 got_worktree_get_root_path(worktree), 0);
2169 if (error)
2170 goto done;
2172 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2173 print_status, NULL, repo);
2174 if (error)
2175 goto done;
2176 done:
2177 if (repo)
2178 got_repo_close(repo);
2179 if (worktree)
2180 got_worktree_close(worktree);
2181 free(path);
2182 free(cwd);
2183 return error;
2186 __dead static void
2187 usage_revert(void)
2189 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2190 exit(1);
2193 static void
2194 revert_progress(void *arg, unsigned char status, const char *path)
2196 while (path[0] == '/')
2197 path++;
2198 printf("%c %s\n", status, path);
2201 static const struct got_error *
2202 cmd_revert(int argc, char *argv[])
2204 const struct got_error *error = NULL;
2205 struct got_worktree *worktree = NULL;
2206 struct got_repository *repo = NULL;
2207 char *cwd = NULL, *path = NULL;
2208 int ch;
2210 while ((ch = getopt(argc, argv, "")) != -1) {
2211 switch (ch) {
2212 default:
2213 usage_revert();
2214 /* NOTREACHED */
2218 argc -= optind;
2219 argv += optind;
2221 if (argc != 1)
2222 usage_revert();
2224 path = realpath(argv[0], NULL);
2225 if (path == NULL) {
2226 error = got_error_from_errno2("realpath", argv[0]);
2227 goto done;
2229 got_path_strip_trailing_slashes(path);
2231 cwd = getcwd(NULL, 0);
2232 if (cwd == NULL) {
2233 error = got_error_from_errno("getcwd");
2234 goto done;
2236 error = got_worktree_open(&worktree, cwd);
2237 if (error)
2238 goto done;
2240 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2241 if (error != NULL)
2242 goto done;
2244 error = apply_unveil(got_repo_get_path(repo), 1,
2245 got_worktree_get_root_path(worktree), 0);
2246 if (error)
2247 goto done;
2249 error = got_worktree_revert(worktree, path,
2250 revert_progress, NULL, repo);
2251 if (error)
2252 goto done;
2253 done:
2254 if (repo)
2255 got_repo_close(repo);
2256 if (worktree)
2257 got_worktree_close(worktree);
2258 free(path);
2259 free(cwd);
2260 return error;
2263 __dead static void
2264 usage_commit(void)
2266 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2267 exit(1);
2270 int
2271 spawn_editor(const char *editor, const char *file)
2273 pid_t pid;
2274 sig_t sighup, sigint, sigquit;
2275 int st = -1;
2277 sighup = signal(SIGHUP, SIG_IGN);
2278 sigint = signal(SIGINT, SIG_IGN);
2279 sigquit = signal(SIGQUIT, SIG_IGN);
2281 switch (pid = fork()) {
2282 case -1:
2283 goto doneediting;
2284 case 0:
2285 execl(editor, editor, file, (char *)NULL);
2286 _exit(127);
2289 while (waitpid(pid, &st, 0) == -1)
2290 if (errno != EINTR)
2291 break;
2293 doneediting:
2294 (void)signal(SIGHUP, sighup);
2295 (void)signal(SIGINT, sigint);
2296 (void)signal(SIGQUIT, sigquit);
2298 if (!WIFEXITED(st)) {
2299 errno = EINTR;
2300 return -1;
2303 return WEXITSTATUS(st);
2306 struct collect_commit_logmsg_arg {
2307 const char *cmdline_log;
2308 const char *editor;
2309 const char *worktree_path;
2310 const char *repo_path;
2311 char *logmsg_path;
2315 static const struct got_error *
2316 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2317 void *arg)
2319 const char *initial_content = "\n# changes to be committed:\n";
2320 struct got_pathlist_entry *pe;
2321 const struct got_error *err = NULL;
2322 char *template = NULL;
2323 struct collect_commit_logmsg_arg *a = arg;
2324 char buf[1024];
2325 struct stat st, st2;
2326 FILE *fp;
2327 size_t len;
2328 int fd, content_changed = 0;
2330 /* if a message was specified on the command line, just use it */
2331 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2332 len = strlen(a->cmdline_log) + 1;
2333 *logmsg = malloc(len + 1);
2334 if (*logmsg == NULL)
2335 return got_error_from_errno("malloc");
2336 strlcpy(*logmsg, a->cmdline_log, len);
2337 return NULL;
2340 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2341 return got_error_from_errno("asprintf");
2343 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2344 if (err)
2345 goto done;
2347 dprintf(fd, initial_content);
2349 TAILQ_FOREACH(pe, commitable_paths, entry) {
2350 struct got_commitable *ct = pe->data;
2351 dprintf(fd, "# %c %s\n",
2352 got_commitable_get_status(ct),
2353 got_commitable_get_path(ct));
2355 close(fd);
2357 if (stat(a->logmsg_path, &st) == -1) {
2358 err = got_error_from_errno2("stat", a->logmsg_path);
2359 goto done;
2362 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2363 err = got_error_from_errno("failed spawning editor");
2364 goto done;
2367 if (stat(a->logmsg_path, &st2) == -1) {
2368 err = got_error_from_errno("stat");
2369 goto done;
2372 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2373 unlink(a->logmsg_path);
2374 free(a->logmsg_path);
2375 a->logmsg_path = NULL;
2376 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2377 "no changes made to commit message, aborting");
2378 goto done;
2381 *logmsg = malloc(st2.st_size + 1);
2382 if (*logmsg == NULL) {
2383 err = got_error_from_errno("malloc");
2384 goto done;
2386 (*logmsg)[0] = '\0';
2387 len = 0;
2389 fp = fopen(a->logmsg_path, "r");
2390 if (fp == NULL) {
2391 err = got_error_from_errno("fopen");
2392 goto done;
2394 while (fgets(buf, sizeof(buf), fp) != NULL) {
2395 if (!content_changed && strcmp(buf, initial_content) != 0)
2396 content_changed = 1;
2397 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2398 continue; /* remove comments and leading empty lines */
2399 len = strlcat(*logmsg, buf, st2.st_size);
2401 fclose(fp);
2403 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2404 (*logmsg)[len - 1] = '\0';
2405 len--;
2408 if (len == 0 || !content_changed) {
2409 unlink(a->logmsg_path);
2410 free(a->logmsg_path);
2411 a->logmsg_path = NULL;
2412 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2413 "commit message cannot be empty, aborting");
2414 goto done;
2416 done:
2417 free(template);
2419 /* Editor is done; we can now apply unveil(2) */
2420 if (err == NULL)
2421 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2422 return err;
2425 static const struct got_error *
2426 cmd_commit(int argc, char *argv[])
2428 const struct got_error *error = NULL;
2429 struct got_worktree *worktree = NULL;
2430 struct got_repository *repo = NULL;
2431 char *cwd = NULL, *path = NULL, *id_str = NULL;
2432 struct got_object_id *id = NULL;
2433 const char *logmsg = NULL;
2434 const char *got_author = getenv("GOT_AUTHOR");
2435 struct collect_commit_logmsg_arg cl_arg;
2436 char *editor = NULL;
2437 int ch;
2439 while ((ch = getopt(argc, argv, "m:")) != -1) {
2440 switch (ch) {
2441 case 'm':
2442 logmsg = optarg;
2443 break;
2444 default:
2445 usage_commit();
2446 /* NOTREACHED */
2450 argc -= optind;
2451 argv += optind;
2453 if (argc == 1) {
2454 path = realpath(argv[0], NULL);
2455 if (path == NULL) {
2456 error = got_error_from_errno2("realpath", argv[0]);
2457 goto done;
2459 got_path_strip_trailing_slashes(path);
2460 } else if (argc != 0)
2461 usage_commit();
2463 if (got_author == NULL) {
2464 /* TODO: Look current user up in password database */
2465 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2466 goto done;
2469 cwd = getcwd(NULL, 0);
2470 if (cwd == NULL) {
2471 error = got_error_from_errno("getcwd");
2472 goto done;
2474 error = got_worktree_open(&worktree, cwd);
2475 if (error)
2476 goto done;
2478 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2479 if (error != NULL)
2480 goto done;
2483 * unveil(2) traverses exec(2); if an editor is used we have
2484 * to apply unveil after the log message has been written.
2486 if (logmsg == NULL || strlen(logmsg) == 0)
2487 error = get_editor(&editor);
2488 else
2489 error = apply_unveil(got_repo_get_path(repo), 0,
2490 got_worktree_get_root_path(worktree), 0);
2491 if (error)
2492 goto done;
2494 cl_arg.editor = editor;
2495 cl_arg.cmdline_log = logmsg;
2496 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2497 cl_arg.repo_path = got_repo_get_path(repo);
2498 cl_arg.logmsg_path = NULL;
2499 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2500 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2501 if (error) {
2502 if (cl_arg.logmsg_path)
2503 fprintf(stderr, "%s: log message preserved in %s\n",
2504 getprogname(), cl_arg.logmsg_path);
2505 goto done;
2508 if (cl_arg.logmsg_path)
2509 unlink(cl_arg.logmsg_path);
2511 error = got_object_id_str(&id_str, id);
2512 if (error)
2513 goto done;
2514 printf("created commit %s\n", id_str);
2515 done:
2516 if (repo)
2517 got_repo_close(repo);
2518 if (worktree)
2519 got_worktree_close(worktree);
2520 free(path);
2521 free(cwd);
2522 free(id_str);
2523 free(editor);
2524 return error;