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/types.h>
20 #include <sys/stat.h>
21 #include <sys/param.h>
22 #include <sys/wait.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <locale.h>
28 #include <ctype.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <libgen.h>
35 #include <time.h>
36 #include <paths.h>
38 #include "got_version.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_diff.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_opentemp.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 static volatile sig_atomic_t sigint_received;
57 static volatile sig_atomic_t sigpipe_received;
59 static void
60 catch_sigint(int signo)
61 {
62 sigint_received = 1;
63 }
65 static void
66 catch_sigpipe(int signo)
67 {
68 sigpipe_received = 1;
69 }
72 struct got_cmd {
73 const char *cmd_name;
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 const char *cmd_alias;
77 };
79 __dead static void usage(int);
80 __dead static void usage_init(void);
81 __dead static void usage_import(void);
82 __dead static void usage_checkout(void);
83 __dead static void usage_update(void);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_status(void);
89 __dead static void usage_ref(void);
90 __dead static void usage_branch(void);
91 __dead static void usage_tag(void);
92 __dead static void usage_add(void);
93 __dead static void usage_remove(void);
94 __dead static void usage_revert(void);
95 __dead static void usage_commit(void);
96 __dead static void usage_cherrypick(void);
97 __dead static void usage_backout(void);
98 __dead static void usage_rebase(void);
99 __dead static void usage_histedit(void);
100 __dead static void usage_integrate(void);
101 __dead static void usage_stage(void);
102 __dead static void usage_unstage(void);
103 __dead static void usage_cat(void);
105 static const struct got_error* cmd_init(int, char *[]);
106 static const struct got_error* cmd_import(int, char *[]);
107 static const struct got_error* cmd_checkout(int, char *[]);
108 static const struct got_error* cmd_update(int, char *[]);
109 static const struct got_error* cmd_log(int, char *[]);
110 static const struct got_error* cmd_diff(int, char *[]);
111 static const struct got_error* cmd_blame(int, char *[]);
112 static const struct got_error* cmd_tree(int, char *[]);
113 static const struct got_error* cmd_status(int, char *[]);
114 static const struct got_error* cmd_ref(int, char *[]);
115 static const struct got_error* cmd_branch(int, char *[]);
116 static const struct got_error* cmd_tag(int, char *[]);
117 static const struct got_error* cmd_add(int, char *[]);
118 static const struct got_error* cmd_remove(int, char *[]);
119 static const struct got_error* cmd_revert(int, char *[]);
120 static const struct got_error* cmd_commit(int, char *[]);
121 static const struct got_error* cmd_cherrypick(int, char *[]);
122 static const struct got_error* cmd_backout(int, char *[]);
123 static const struct got_error* cmd_rebase(int, char *[]);
124 static const struct got_error* cmd_histedit(int, char *[]);
125 static const struct got_error* cmd_integrate(int, char *[]);
126 static const struct got_error* cmd_stage(int, char *[]);
127 static const struct got_error* cmd_unstage(int, char *[]);
128 static const struct got_error* cmd_cat(int, char *[]);
130 static struct got_cmd got_commands[] = {
131 { "init", cmd_init, usage_init, "in" },
132 { "import", cmd_import, usage_import, "im" },
133 { "checkout", cmd_checkout, usage_checkout, "co" },
134 { "update", cmd_update, usage_update, "up" },
135 { "log", cmd_log, usage_log, "" },
136 { "diff", cmd_diff, usage_diff, "di" },
137 { "blame", cmd_blame, usage_blame, "bl" },
138 { "tree", cmd_tree, usage_tree, "tr" },
139 { "status", cmd_status, usage_status, "st" },
140 { "ref", cmd_ref, usage_ref, "" },
141 { "branch", cmd_branch, usage_branch, "br" },
142 { "tag", cmd_tag, usage_tag, "" },
143 { "add", cmd_add, usage_add, "" },
144 { "remove", cmd_remove, usage_remove, "rm" },
145 { "revert", cmd_revert, usage_revert, "rv" },
146 { "commit", cmd_commit, usage_commit, "ci" },
147 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
148 { "backout", cmd_backout, usage_backout, "bo" },
149 { "rebase", cmd_rebase, usage_rebase, "rb" },
150 { "histedit", cmd_histedit, usage_histedit, "he" },
151 { "integrate", cmd_integrate, usage_integrate,"ig" },
152 { "stage", cmd_stage, usage_stage, "sg" },
153 { "unstage", cmd_unstage, usage_unstage, "ug" },
154 { "cat", cmd_cat, usage_cat, "" },
155 };
157 static void
158 list_commands(void)
160 int i;
162 fprintf(stderr, "commands:");
163 for (i = 0; i < nitems(got_commands); i++) {
164 struct got_cmd *cmd = &got_commands[i];
165 fprintf(stderr, " %s", cmd->cmd_name);
167 fputc('\n', stderr);
170 int
171 main(int argc, char *argv[])
173 struct got_cmd *cmd;
174 unsigned int i;
175 int ch;
176 int hflag = 0, Vflag = 0;
178 setlocale(LC_CTYPE, "");
180 while ((ch = getopt(argc, argv, "hV")) != -1) {
181 switch (ch) {
182 case 'h':
183 hflag = 1;
184 break;
185 case 'V':
186 Vflag = 1;
187 break;
188 default:
189 usage(hflag);
190 /* NOTREACHED */
194 argc -= optind;
195 argv += optind;
196 optind = 0;
198 if (Vflag) {
199 got_version_print_str();
200 return 1;
203 if (argc <= 0)
204 usage(hflag);
206 signal(SIGINT, catch_sigint);
207 signal(SIGPIPE, catch_sigpipe);
209 for (i = 0; i < nitems(got_commands); i++) {
210 const struct got_error *error;
212 cmd = &got_commands[i];
214 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
215 strcmp(cmd->cmd_alias, argv[0]) != 0)
216 continue;
218 if (hflag)
219 got_commands[i].cmd_usage();
221 error = got_commands[i].cmd_main(argc, argv);
222 if (error && !(sigint_received || sigpipe_received)) {
223 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
224 return 1;
227 return 0;
230 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
231 list_commands();
232 return 1;
235 __dead static void
236 usage(int hflag)
238 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
239 getprogname());
240 if (hflag)
241 list_commands();
242 exit(1);
245 static const struct got_error *
246 get_editor(char **abspath)
248 const struct got_error *err = NULL;
249 const char *editor;
251 *abspath = NULL;
253 editor = getenv("VISUAL");
254 if (editor == NULL)
255 editor = getenv("EDITOR");
257 if (editor) {
258 err = got_path_find_prog(abspath, editor);
259 if (err)
260 return err;
263 if (*abspath == NULL) {
264 *abspath = strdup("/bin/ed");
265 if (*abspath == NULL)
266 return got_error_from_errno("strdup");
269 return NULL;
272 static const struct got_error *
273 apply_unveil(const char *repo_path, int repo_read_only,
274 const char *worktree_path)
276 const struct got_error *err;
278 #ifdef PROFILE
279 if (unveil("gmon.out", "rwc") != 0)
280 return got_error_from_errno2("unveil", "gmon.out");
281 #endif
282 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
283 return got_error_from_errno2("unveil", repo_path);
285 if (worktree_path && unveil(worktree_path, "rwc") != 0)
286 return got_error_from_errno2("unveil", worktree_path);
288 if (unveil("/tmp", "rwc") != 0)
289 return got_error_from_errno2("unveil", "/tmp");
291 err = got_privsep_unveil_exec_helpers();
292 if (err != NULL)
293 return err;
295 if (unveil(NULL, NULL) != 0)
296 return got_error_from_errno("unveil");
298 return NULL;
301 __dead static void
302 usage_init(void)
304 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
305 exit(1);
308 static const struct got_error *
309 cmd_init(int argc, char *argv[])
311 const struct got_error *error = NULL;
312 char *repo_path = NULL;
313 int ch;
315 while ((ch = getopt(argc, argv, "")) != -1) {
316 switch (ch) {
317 default:
318 usage_init();
319 /* NOTREACHED */
323 argc -= optind;
324 argv += optind;
326 #ifndef PROFILE
327 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
328 err(1, "pledge");
329 #endif
330 if (argc != 1)
331 usage_init();
333 repo_path = strdup(argv[0]);
334 if (repo_path == NULL)
335 return got_error_from_errno("strdup");
337 got_path_strip_trailing_slashes(repo_path);
339 error = got_path_mkdir(repo_path);
340 if (error &&
341 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
342 goto done;
344 error = apply_unveil(repo_path, 0, NULL);
345 if (error)
346 goto done;
348 error = got_repo_init(repo_path);
349 if (error != NULL)
350 goto done;
352 done:
353 free(repo_path);
354 return error;
357 __dead static void
358 usage_import(void)
360 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
361 "[-r repository-path] [-I pattern] path\n", getprogname());
362 exit(1);
365 int
366 spawn_editor(const char *editor, const char *file)
368 pid_t pid;
369 sig_t sighup, sigint, sigquit;
370 int st = -1;
372 sighup = signal(SIGHUP, SIG_IGN);
373 sigint = signal(SIGINT, SIG_IGN);
374 sigquit = signal(SIGQUIT, SIG_IGN);
376 switch (pid = fork()) {
377 case -1:
378 goto doneediting;
379 case 0:
380 execl(editor, editor, file, (char *)NULL);
381 _exit(127);
384 while (waitpid(pid, &st, 0) == -1)
385 if (errno != EINTR)
386 break;
388 doneediting:
389 (void)signal(SIGHUP, sighup);
390 (void)signal(SIGINT, sigint);
391 (void)signal(SIGQUIT, sigquit);
393 if (!WIFEXITED(st)) {
394 errno = EINTR;
395 return -1;
398 return WEXITSTATUS(st);
401 static const struct got_error *
402 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
403 const char *initial_content)
405 const struct got_error *err = NULL;
406 char buf[1024];
407 struct stat st, st2;
408 FILE *fp;
409 int content_changed = 0;
410 size_t len;
412 *logmsg = NULL;
414 if (stat(logmsg_path, &st) == -1)
415 return got_error_from_errno2("stat", logmsg_path);
417 if (spawn_editor(editor, logmsg_path) == -1)
418 return got_error_from_errno("failed spawning editor");
420 if (stat(logmsg_path, &st2) == -1)
421 return got_error_from_errno("stat");
423 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
424 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
425 "no changes made to commit message, aborting");
427 *logmsg = malloc(st2.st_size + 1);
428 if (*logmsg == NULL)
429 return got_error_from_errno("malloc");
430 (*logmsg)[0] = '\0';
431 len = 0;
433 fp = fopen(logmsg_path, "r");
434 if (fp == NULL) {
435 err = got_error_from_errno("fopen");
436 goto done;
438 while (fgets(buf, sizeof(buf), fp) != NULL) {
439 if (!content_changed && strcmp(buf, initial_content) != 0)
440 content_changed = 1;
441 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
442 continue; /* remove comments and leading empty lines */
443 len = strlcat(*logmsg, buf, st2.st_size);
445 fclose(fp);
447 while (len > 0 && (*logmsg)[len - 1] == '\n') {
448 (*logmsg)[len - 1] = '\0';
449 len--;
452 if (len == 0 || !content_changed)
453 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
454 "commit message cannot be empty, aborting");
455 done:
456 if (err) {
457 free(*logmsg);
458 *logmsg = NULL;
460 return err;
463 static const struct got_error *
464 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
465 const char *path_dir, const char *branch_name)
467 char *initial_content = NULL;
468 const struct got_error *err = NULL;
469 int fd;
471 if (asprintf(&initial_content,
472 "\n# %s to be imported to branch %s\n", path_dir,
473 branch_name) == -1)
474 return got_error_from_errno("asprintf");
476 err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
477 if (err)
478 goto done;
480 dprintf(fd, initial_content);
481 close(fd);
483 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
484 done:
485 free(initial_content);
486 return err;
489 static const struct got_error *
490 import_progress(void *arg, const char *path)
492 printf("A %s\n", path);
493 return NULL;
496 static const struct got_error *
497 get_author(char **author, struct got_repository *repo)
499 const struct got_error *err = NULL;
500 const char *got_author, *name, *email;
502 *author = NULL;
504 name = got_repo_get_gitconfig_author_name(repo);
505 email = got_repo_get_gitconfig_author_email(repo);
506 if (name && email) {
507 if (asprintf(author, "%s <%s>", name, email) == -1)
508 return got_error_from_errno("asprintf");
509 return NULL;
512 got_author = getenv("GOT_AUTHOR");
513 if (got_author == NULL) {
514 name = got_repo_get_global_gitconfig_author_name(repo);
515 email = got_repo_get_global_gitconfig_author_email(repo);
516 if (name && email) {
517 if (asprintf(author, "%s <%s>", name, email) == -1)
518 return got_error_from_errno("asprintf");
519 return NULL;
521 /* TODO: Look up user in password database? */
522 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
525 *author = strdup(got_author);
526 if (*author == NULL)
527 return got_error_from_errno("strdup");
529 /*
530 * Really dumb email address check; we're only doing this to
531 * avoid git's object parser breaking on commits we create.
532 */
533 while (*got_author && *got_author != '<')
534 got_author++;
535 if (*got_author != '<') {
536 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
537 goto done;
539 while (*got_author && *got_author != '@')
540 got_author++;
541 if (*got_author != '@') {
542 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
543 goto done;
545 while (*got_author && *got_author != '>')
546 got_author++;
547 if (*got_author != '>')
548 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
549 done:
550 if (err) {
551 free(*author);
552 *author = NULL;
554 return err;
557 static const struct got_error *
558 get_gitconfig_path(char **gitconfig_path)
560 const char *homedir = getenv("HOME");
562 *gitconfig_path = NULL;
563 if (homedir) {
564 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
565 return got_error_from_errno("asprintf");
568 return NULL;
571 static const struct got_error *
572 cmd_import(int argc, char *argv[])
574 const struct got_error *error = NULL;
575 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
576 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
577 const char *branch_name = "master";
578 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
579 struct got_repository *repo = NULL;
580 struct got_reference *branch_ref = NULL, *head_ref = NULL;
581 struct got_object_id *new_commit_id = NULL;
582 int ch;
583 struct got_pathlist_head ignores;
584 struct got_pathlist_entry *pe;
585 int preserve_logmsg = 0;
587 TAILQ_INIT(&ignores);
589 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
590 switch (ch) {
591 case 'b':
592 branch_name = optarg;
593 break;
594 case 'm':
595 logmsg = strdup(optarg);
596 if (logmsg == NULL) {
597 error = got_error_from_errno("strdup");
598 goto done;
600 break;
601 case 'r':
602 repo_path = realpath(optarg, NULL);
603 if (repo_path == NULL) {
604 error = got_error_from_errno2("realpath",
605 optarg);
606 goto done;
608 break;
609 case 'I':
610 if (optarg[0] == '\0')
611 break;
612 error = got_pathlist_insert(&pe, &ignores, optarg,
613 NULL);
614 if (error)
615 goto done;
616 break;
617 default:
618 usage_import();
619 /* NOTREACHED */
623 argc -= optind;
624 argv += optind;
626 #ifndef PROFILE
627 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
628 "unveil",
629 NULL) == -1)
630 err(1, "pledge");
631 #endif
632 if (argc != 1)
633 usage_import();
635 if (repo_path == NULL) {
636 repo_path = getcwd(NULL, 0);
637 if (repo_path == NULL)
638 return got_error_from_errno("getcwd");
640 got_path_strip_trailing_slashes(repo_path);
641 error = get_gitconfig_path(&gitconfig_path);
642 if (error)
643 goto done;
644 error = got_repo_open(&repo, repo_path, gitconfig_path);
645 if (error)
646 goto done;
648 error = get_author(&author, repo);
649 if (error)
650 return error;
652 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
653 error = got_error_from_errno("asprintf");
654 goto done;
657 error = got_ref_open(&branch_ref, repo, refname, 0);
658 if (error) {
659 if (error->code != GOT_ERR_NOT_REF)
660 goto done;
661 } else {
662 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
663 "import target branch already exists");
664 goto done;
667 path_dir = realpath(argv[0], NULL);
668 if (path_dir == NULL) {
669 error = got_error_from_errno2("realpath", argv[0]);
670 goto done;
672 got_path_strip_trailing_slashes(path_dir);
674 /*
675 * unveil(2) traverses exec(2); if an editor is used we have
676 * to apply unveil after the log message has been written.
677 */
678 if (logmsg == NULL || strlen(logmsg) == 0) {
679 error = get_editor(&editor);
680 if (error)
681 goto done;
682 free(logmsg);
683 error = collect_import_msg(&logmsg, &logmsg_path, editor,
684 path_dir, refname);
685 if (error) {
686 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
687 logmsg_path != NULL)
688 preserve_logmsg = 1;
689 goto done;
693 if (unveil(path_dir, "r") != 0) {
694 error = got_error_from_errno2("unveil", path_dir);
695 if (logmsg_path)
696 preserve_logmsg = 1;
697 goto done;
700 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
701 if (error) {
702 if (logmsg_path)
703 preserve_logmsg = 1;
704 goto done;
707 error = got_repo_import(&new_commit_id, path_dir, logmsg,
708 author, &ignores, repo, import_progress, NULL);
709 if (error) {
710 if (logmsg_path)
711 preserve_logmsg = 1;
712 goto done;
715 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
716 if (error) {
717 if (logmsg_path)
718 preserve_logmsg = 1;
719 goto done;
722 error = got_ref_write(branch_ref, repo);
723 if (error) {
724 if (logmsg_path)
725 preserve_logmsg = 1;
726 goto done;
729 error = got_object_id_str(&id_str, new_commit_id);
730 if (error) {
731 if (logmsg_path)
732 preserve_logmsg = 1;
733 goto done;
736 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
737 if (error) {
738 if (error->code != GOT_ERR_NOT_REF) {
739 if (logmsg_path)
740 preserve_logmsg = 1;
741 goto done;
744 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
745 branch_ref);
746 if (error) {
747 if (logmsg_path)
748 preserve_logmsg = 1;
749 goto done;
752 error = got_ref_write(head_ref, repo);
753 if (error) {
754 if (logmsg_path)
755 preserve_logmsg = 1;
756 goto done;
760 printf("Created branch %s with commit %s\n",
761 got_ref_get_name(branch_ref), id_str);
762 done:
763 if (preserve_logmsg) {
764 fprintf(stderr, "%s: log message preserved in %s\n",
765 getprogname(), logmsg_path);
766 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
767 error = got_error_from_errno2("unlink", logmsg_path);
768 free(logmsg);
769 free(logmsg_path);
770 free(repo_path);
771 free(editor);
772 free(refname);
773 free(new_commit_id);
774 free(id_str);
775 free(author);
776 free(gitconfig_path);
777 if (branch_ref)
778 got_ref_close(branch_ref);
779 if (head_ref)
780 got_ref_close(head_ref);
781 return error;
784 __dead static void
785 usage_checkout(void)
787 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
788 "[-p prefix] repository-path [worktree-path]\n", getprogname());
789 exit(1);
792 static const struct got_error *
793 checkout_progress(void *arg, unsigned char status, const char *path)
795 char *worktree_path = arg;
797 /* Base commit bump happens silently. */
798 if (status == GOT_STATUS_BUMP_BASE)
799 return NULL;
801 while (path[0] == '/')
802 path++;
804 printf("%c %s/%s\n", status, worktree_path, path);
805 return NULL;
808 static const struct got_error *
809 check_cancelled(void *arg)
811 if (sigint_received || sigpipe_received)
812 return got_error(GOT_ERR_CANCELLED);
813 return NULL;
816 static const struct got_error *
817 check_linear_ancestry(struct got_object_id *commit_id,
818 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
819 struct got_repository *repo)
821 const struct got_error *err = NULL;
822 struct got_object_id *yca_id;
824 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
825 commit_id, base_commit_id, repo, check_cancelled, NULL);
826 if (err)
827 return err;
829 if (yca_id == NULL)
830 return got_error(GOT_ERR_ANCESTRY);
832 /*
833 * Require a straight line of history between the target commit
834 * and the work tree's base commit.
836 * Non-linear situations such as this require a rebase:
838 * (commit) D F (base_commit)
839 * \ /
840 * C E
841 * \ /
842 * B (yca)
843 * |
844 * A
846 * 'got update' only handles linear cases:
847 * Update forwards in time: A (base/yca) - B - C - D (commit)
848 * Update backwards in time: D (base) - C - B - A (commit/yca)
849 */
850 if (allow_forwards_in_time_only) {
851 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
852 return got_error(GOT_ERR_ANCESTRY);
853 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
854 got_object_id_cmp(base_commit_id, yca_id) != 0)
855 return got_error(GOT_ERR_ANCESTRY);
857 free(yca_id);
858 return NULL;
861 static const struct got_error *
862 check_same_branch(struct got_object_id *commit_id,
863 struct got_reference *head_ref, struct got_object_id *yca_id,
864 struct got_repository *repo)
866 const struct got_error *err = NULL;
867 struct got_commit_graph *graph = NULL;
868 struct got_object_id *head_commit_id = NULL;
869 int is_same_branch = 0;
871 err = got_ref_resolve(&head_commit_id, repo, head_ref);
872 if (err)
873 goto done;
875 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
876 is_same_branch = 1;
877 goto done;
879 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
880 is_same_branch = 1;
881 goto done;
884 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
885 if (err)
886 goto done;
888 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
889 check_cancelled, NULL);
890 if (err)
891 goto done;
893 for (;;) {
894 struct got_object_id *id;
895 err = got_commit_graph_iter_next(&id, graph);
896 if (err) {
897 if (err->code == GOT_ERR_ITER_COMPLETED) {
898 err = NULL;
899 break;
900 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
901 break;
902 err = got_commit_graph_fetch_commits(graph, 1,
903 repo, check_cancelled, NULL);
904 if (err)
905 break;
908 if (id) {
909 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
910 break;
911 if (got_object_id_cmp(id, commit_id) == 0) {
912 is_same_branch = 1;
913 break;
917 done:
918 if (graph)
919 got_commit_graph_close(graph);
920 free(head_commit_id);
921 if (!err && !is_same_branch)
922 err = got_error(GOT_ERR_ANCESTRY);
923 return err;
926 static const struct got_error *
927 resolve_commit_arg(struct got_object_id **commit_id,
928 const char *commit_id_arg, struct got_repository *repo)
930 const struct got_error *err;
931 struct got_reference *ref;
932 struct got_tag_object *tag;
934 err = got_repo_object_match_tag(&tag, commit_id_arg,
935 GOT_OBJ_TYPE_COMMIT, repo);
936 if (err == NULL) {
937 *commit_id = got_object_id_dup(
938 got_object_tag_get_object_id(tag));
939 if (*commit_id == NULL)
940 err = got_error_from_errno("got_object_id_dup");
941 got_object_tag_close(tag);
942 return err;
943 } else if (err->code != GOT_ERR_NO_OBJ)
944 return err;
946 err = got_ref_open(&ref, repo, commit_id_arg, 0);
947 if (err == NULL) {
948 err = got_ref_resolve(commit_id, repo, ref);
949 got_ref_close(ref);
950 } else {
951 if (err->code != GOT_ERR_NOT_REF)
952 return err;
953 err = got_repo_match_object_id_prefix(commit_id,
954 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
956 return err;
959 static const struct got_error *
960 cmd_checkout(int argc, char *argv[])
962 const struct got_error *error = NULL;
963 struct got_repository *repo = NULL;
964 struct got_reference *head_ref = NULL;
965 struct got_worktree *worktree = NULL;
966 char *repo_path = NULL;
967 char *worktree_path = NULL;
968 const char *path_prefix = "";
969 const char *branch_name = GOT_REF_HEAD;
970 char *commit_id_str = NULL;
971 int ch, same_path_prefix;
972 struct got_pathlist_head paths;
974 TAILQ_INIT(&paths);
976 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
977 switch (ch) {
978 case 'b':
979 branch_name = optarg;
980 break;
981 case 'c':
982 commit_id_str = strdup(optarg);
983 if (commit_id_str == NULL)
984 return got_error_from_errno("strdup");
985 break;
986 case 'p':
987 path_prefix = optarg;
988 break;
989 default:
990 usage_checkout();
991 /* NOTREACHED */
995 argc -= optind;
996 argv += optind;
998 #ifndef PROFILE
999 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1000 "unveil", NULL) == -1)
1001 err(1, "pledge");
1002 #endif
1003 if (argc == 1) {
1004 char *cwd, *base, *dotgit;
1005 repo_path = realpath(argv[0], NULL);
1006 if (repo_path == NULL)
1007 return got_error_from_errno2("realpath", argv[0]);
1008 cwd = getcwd(NULL, 0);
1009 if (cwd == NULL) {
1010 error = got_error_from_errno("getcwd");
1011 goto done;
1013 if (path_prefix[0]) {
1014 base = basename(path_prefix);
1015 if (base == NULL) {
1016 error = got_error_from_errno2("basename",
1017 path_prefix);
1018 goto done;
1020 } else {
1021 base = basename(repo_path);
1022 if (base == NULL) {
1023 error = got_error_from_errno2("basename",
1024 repo_path);
1025 goto done;
1028 dotgit = strstr(base, ".git");
1029 if (dotgit)
1030 *dotgit = '\0';
1031 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1032 error = got_error_from_errno("asprintf");
1033 free(cwd);
1034 goto done;
1036 free(cwd);
1037 } else if (argc == 2) {
1038 repo_path = realpath(argv[0], NULL);
1039 if (repo_path == NULL) {
1040 error = got_error_from_errno2("realpath", argv[0]);
1041 goto done;
1043 worktree_path = realpath(argv[1], NULL);
1044 if (worktree_path == NULL) {
1045 if (errno != ENOENT) {
1046 error = got_error_from_errno2("realpath",
1047 argv[1]);
1048 goto done;
1050 worktree_path = strdup(argv[1]);
1051 if (worktree_path == NULL) {
1052 error = got_error_from_errno("strdup");
1053 goto done;
1056 } else
1057 usage_checkout();
1059 got_path_strip_trailing_slashes(repo_path);
1060 got_path_strip_trailing_slashes(worktree_path);
1062 error = got_repo_open(&repo, repo_path, NULL);
1063 if (error != NULL)
1064 goto done;
1066 /* Pre-create work tree path for unveil(2) */
1067 error = got_path_mkdir(worktree_path);
1068 if (error) {
1069 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1070 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1071 goto done;
1072 if (!got_path_dir_is_empty(worktree_path)) {
1073 error = got_error_path(worktree_path,
1074 GOT_ERR_DIR_NOT_EMPTY);
1075 goto done;
1079 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1080 if (error)
1081 goto done;
1083 error = got_ref_open(&head_ref, repo, branch_name, 0);
1084 if (error != NULL)
1085 goto done;
1087 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1088 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1089 goto done;
1091 error = got_worktree_open(&worktree, worktree_path);
1092 if (error != NULL)
1093 goto done;
1095 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1096 path_prefix);
1097 if (error != NULL)
1098 goto done;
1099 if (!same_path_prefix) {
1100 error = got_error(GOT_ERR_PATH_PREFIX);
1101 goto done;
1104 if (commit_id_str) {
1105 struct got_object_id *commit_id;
1106 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1107 if (error)
1108 goto done;
1109 error = check_linear_ancestry(commit_id,
1110 got_worktree_get_base_commit_id(worktree), 0, repo);
1111 if (error != NULL) {
1112 free(commit_id);
1113 goto done;
1115 error = check_same_branch(commit_id, head_ref, NULL, repo);
1116 if (error)
1117 goto done;
1118 error = got_worktree_set_base_commit_id(worktree, repo,
1119 commit_id);
1120 free(commit_id);
1121 if (error)
1122 goto done;
1125 error = got_pathlist_append(&paths, "", NULL);
1126 if (error)
1127 goto done;
1128 error = got_worktree_checkout_files(worktree, &paths, repo,
1129 checkout_progress, worktree_path, check_cancelled, NULL);
1130 if (error != NULL)
1131 goto done;
1133 printf("Now shut up and hack\n");
1135 done:
1136 got_pathlist_free(&paths);
1137 free(commit_id_str);
1138 free(repo_path);
1139 free(worktree_path);
1140 return error;
1143 __dead static void
1144 usage_update(void)
1146 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1147 getprogname());
1148 exit(1);
1151 static const struct got_error *
1152 update_progress(void *arg, unsigned char status, const char *path)
1154 int *did_something = arg;
1156 if (status == GOT_STATUS_EXISTS)
1157 return NULL;
1159 *did_something = 1;
1161 /* Base commit bump happens silently. */
1162 if (status == GOT_STATUS_BUMP_BASE)
1163 return NULL;
1165 while (path[0] == '/')
1166 path++;
1167 printf("%c %s\n", status, path);
1168 return NULL;
1171 static const struct got_error *
1172 switch_head_ref(struct got_reference *head_ref,
1173 struct got_object_id *commit_id, struct got_worktree *worktree,
1174 struct got_repository *repo)
1176 const struct got_error *err = NULL;
1177 char *base_id_str;
1178 int ref_has_moved = 0;
1180 /* Trivial case: switching between two different references. */
1181 if (strcmp(got_ref_get_name(head_ref),
1182 got_worktree_get_head_ref_name(worktree)) != 0) {
1183 printf("Switching work tree from %s to %s\n",
1184 got_worktree_get_head_ref_name(worktree),
1185 got_ref_get_name(head_ref));
1186 return got_worktree_set_head_ref(worktree, head_ref);
1189 err = check_linear_ancestry(commit_id,
1190 got_worktree_get_base_commit_id(worktree), 0, repo);
1191 if (err) {
1192 if (err->code != GOT_ERR_ANCESTRY)
1193 return err;
1194 ref_has_moved = 1;
1196 if (!ref_has_moved)
1197 return NULL;
1199 /* Switching to a rebased branch with the same reference name. */
1200 err = got_object_id_str(&base_id_str,
1201 got_worktree_get_base_commit_id(worktree));
1202 if (err)
1203 return err;
1204 printf("Reference %s now points at a different branch\n",
1205 got_worktree_get_head_ref_name(worktree));
1206 printf("Switching work tree from %s to %s\n", base_id_str,
1207 got_worktree_get_head_ref_name(worktree));
1208 return NULL;
1211 static const struct got_error *
1212 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1214 const struct got_error *err;
1215 int in_progress;
1217 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1218 if (err)
1219 return err;
1220 if (in_progress)
1221 return got_error(GOT_ERR_REBASING);
1223 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1224 if (err)
1225 return err;
1226 if (in_progress)
1227 return got_error(GOT_ERR_HISTEDIT_BUSY);
1229 return NULL;
1232 static const struct got_error *
1233 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1234 char *argv[], struct got_worktree *worktree)
1236 const struct got_error *err = NULL;
1237 char *path;
1238 int i;
1240 if (argc == 0) {
1241 path = strdup("");
1242 if (path == NULL)
1243 return got_error_from_errno("strdup");
1244 return got_pathlist_append(paths, path, NULL);
1247 for (i = 0; i < argc; i++) {
1248 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1249 if (err)
1250 break;
1251 err = got_pathlist_append(paths, path, NULL);
1252 if (err) {
1253 free(path);
1254 break;
1258 return err;
1261 static const struct got_error *
1262 cmd_update(int argc, char *argv[])
1264 const struct got_error *error = NULL;
1265 struct got_repository *repo = NULL;
1266 struct got_worktree *worktree = NULL;
1267 char *worktree_path = NULL;
1268 struct got_object_id *commit_id = NULL;
1269 char *commit_id_str = NULL;
1270 const char *branch_name = NULL;
1271 struct got_reference *head_ref = NULL;
1272 struct got_pathlist_head paths;
1273 struct got_pathlist_entry *pe;
1274 int ch, did_something = 0;
1276 TAILQ_INIT(&paths);
1278 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1279 switch (ch) {
1280 case 'b':
1281 branch_name = optarg;
1282 break;
1283 case 'c':
1284 commit_id_str = strdup(optarg);
1285 if (commit_id_str == NULL)
1286 return got_error_from_errno("strdup");
1287 break;
1288 default:
1289 usage_update();
1290 /* NOTREACHED */
1294 argc -= optind;
1295 argv += optind;
1297 #ifndef PROFILE
1298 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1299 "unveil", NULL) == -1)
1300 err(1, "pledge");
1301 #endif
1302 worktree_path = getcwd(NULL, 0);
1303 if (worktree_path == NULL) {
1304 error = got_error_from_errno("getcwd");
1305 goto done;
1307 error = got_worktree_open(&worktree, worktree_path);
1308 if (error)
1309 goto done;
1311 error = check_rebase_or_histedit_in_progress(worktree);
1312 if (error)
1313 goto done;
1315 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1316 NULL);
1317 if (error != NULL)
1318 goto done;
1320 error = apply_unveil(got_repo_get_path(repo), 0,
1321 got_worktree_get_root_path(worktree));
1322 if (error)
1323 goto done;
1325 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1326 if (error)
1327 goto done;
1329 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1330 got_worktree_get_head_ref_name(worktree), 0);
1331 if (error != NULL)
1332 goto done;
1333 if (commit_id_str == NULL) {
1334 error = got_ref_resolve(&commit_id, repo, head_ref);
1335 if (error != NULL)
1336 goto done;
1337 error = got_object_id_str(&commit_id_str, commit_id);
1338 if (error != NULL)
1339 goto done;
1340 } else {
1341 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1342 free(commit_id_str);
1343 commit_id_str = NULL;
1344 if (error)
1345 goto done;
1346 error = got_object_id_str(&commit_id_str, commit_id);
1347 if (error)
1348 goto done;
1351 if (branch_name) {
1352 struct got_object_id *head_commit_id;
1353 TAILQ_FOREACH(pe, &paths, entry) {
1354 if (pe->path_len == 0)
1355 continue;
1356 error = got_error_msg(GOT_ERR_BAD_PATH,
1357 "switching between branches requires that "
1358 "the entire work tree gets updated");
1359 goto done;
1361 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1362 if (error)
1363 goto done;
1364 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1365 repo);
1366 free(head_commit_id);
1367 if (error != NULL)
1368 goto done;
1369 error = check_same_branch(commit_id, head_ref, NULL, repo);
1370 if (error)
1371 goto done;
1372 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1373 if (error)
1374 goto done;
1375 } else {
1376 error = check_linear_ancestry(commit_id,
1377 got_worktree_get_base_commit_id(worktree), 0, repo);
1378 if (error != NULL) {
1379 if (error->code == GOT_ERR_ANCESTRY)
1380 error = got_error(GOT_ERR_BRANCH_MOVED);
1381 goto done;
1383 error = check_same_branch(commit_id, head_ref, NULL, repo);
1384 if (error)
1385 goto done;
1388 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1389 commit_id) != 0) {
1390 error = got_worktree_set_base_commit_id(worktree, repo,
1391 commit_id);
1392 if (error)
1393 goto done;
1396 error = got_worktree_checkout_files(worktree, &paths, repo,
1397 update_progress, &did_something, check_cancelled, NULL);
1398 if (error != NULL)
1399 goto done;
1401 if (did_something)
1402 printf("Updated to commit %s\n", commit_id_str);
1403 else
1404 printf("Already up-to-date\n");
1405 done:
1406 free(worktree_path);
1407 TAILQ_FOREACH(pe, &paths, entry)
1408 free((char *)pe->path);
1409 got_pathlist_free(&paths);
1410 free(commit_id);
1411 free(commit_id_str);
1412 return error;
1415 static const struct got_error *
1416 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1417 const char *path, int diff_context, int ignore_whitespace,
1418 struct got_repository *repo)
1420 const struct got_error *err = NULL;
1421 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1423 if (blob_id1) {
1424 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1425 if (err)
1426 goto done;
1429 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1430 if (err)
1431 goto done;
1433 while (path[0] == '/')
1434 path++;
1435 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1436 ignore_whitespace, stdout);
1437 done:
1438 if (blob1)
1439 got_object_blob_close(blob1);
1440 got_object_blob_close(blob2);
1441 return err;
1444 static const struct got_error *
1445 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1446 const char *path, int diff_context, int ignore_whitespace,
1447 struct got_repository *repo)
1449 const struct got_error *err = NULL;
1450 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1451 struct got_diff_blob_output_unidiff_arg arg;
1453 if (tree_id1) {
1454 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1455 if (err)
1456 goto done;
1459 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1460 if (err)
1461 goto done;
1463 arg.diff_context = diff_context;
1464 arg.ignore_whitespace = ignore_whitespace;
1465 arg.outfile = stdout;
1466 while (path[0] == '/')
1467 path++;
1468 err = got_diff_tree(tree1, tree2, path, path, repo,
1469 got_diff_blob_output_unidiff, &arg, 1);
1470 done:
1471 if (tree1)
1472 got_object_tree_close(tree1);
1473 if (tree2)
1474 got_object_tree_close(tree2);
1475 return err;
1478 static const struct got_error *
1479 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1480 const char *path, int diff_context, struct got_repository *repo)
1482 const struct got_error *err = NULL;
1483 struct got_commit_object *pcommit = NULL;
1484 char *id_str1 = NULL, *id_str2 = NULL;
1485 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1486 struct got_object_qid *qid;
1488 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1489 if (qid != NULL) {
1490 err = got_object_open_as_commit(&pcommit, repo,
1491 qid->id);
1492 if (err)
1493 return err;
1496 if (path && path[0] != '\0') {
1497 int obj_type;
1498 err = got_object_id_by_path(&obj_id2, repo, id, path);
1499 if (err)
1500 goto done;
1501 err = got_object_id_str(&id_str2, obj_id2);
1502 if (err) {
1503 free(obj_id2);
1504 goto done;
1506 if (pcommit) {
1507 err = got_object_id_by_path(&obj_id1, repo,
1508 qid->id, path);
1509 if (err) {
1510 free(obj_id2);
1511 goto done;
1513 err = got_object_id_str(&id_str1, obj_id1);
1514 if (err) {
1515 free(obj_id2);
1516 goto done;
1519 err = got_object_get_type(&obj_type, repo, obj_id2);
1520 if (err) {
1521 free(obj_id2);
1522 goto done;
1524 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1525 switch (obj_type) {
1526 case GOT_OBJ_TYPE_BLOB:
1527 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1528 0, repo);
1529 break;
1530 case GOT_OBJ_TYPE_TREE:
1531 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1532 0, repo);
1533 break;
1534 default:
1535 err = got_error(GOT_ERR_OBJ_TYPE);
1536 break;
1538 free(obj_id1);
1539 free(obj_id2);
1540 } else {
1541 obj_id2 = got_object_commit_get_tree_id(commit);
1542 err = got_object_id_str(&id_str2, obj_id2);
1543 if (err)
1544 goto done;
1545 obj_id1 = got_object_commit_get_tree_id(pcommit);
1546 err = got_object_id_str(&id_str1, obj_id1);
1547 if (err)
1548 goto done;
1549 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1550 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1553 done:
1554 free(id_str1);
1555 free(id_str2);
1556 if (pcommit)
1557 got_object_commit_close(pcommit);
1558 return err;
1561 static char *
1562 get_datestr(time_t *time, char *datebuf)
1564 struct tm mytm, *tm;
1565 char *p, *s;
1567 tm = gmtime_r(time, &mytm);
1568 if (tm == NULL)
1569 return NULL;
1570 s = asctime_r(tm, datebuf);
1571 if (s == NULL)
1572 return NULL;
1573 p = strchr(s, '\n');
1574 if (p)
1575 *p = '\0';
1576 return s;
1579 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1581 static const struct got_error *
1582 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1583 struct got_repository *repo, const char *path, int show_patch,
1584 int diff_context, struct got_reflist_head *refs)
1586 const struct got_error *err = NULL;
1587 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1588 char datebuf[26];
1589 time_t committer_time;
1590 const char *author, *committer;
1591 char *refs_str = NULL;
1592 struct got_reflist_entry *re;
1594 SIMPLEQ_FOREACH(re, refs, entry) {
1595 char *s;
1596 const char *name;
1597 struct got_tag_object *tag = NULL;
1598 int cmp;
1600 name = got_ref_get_name(re->ref);
1601 if (strcmp(name, GOT_REF_HEAD) == 0)
1602 continue;
1603 if (strncmp(name, "refs/", 5) == 0)
1604 name += 5;
1605 if (strncmp(name, "got/", 4) == 0)
1606 continue;
1607 if (strncmp(name, "heads/", 6) == 0)
1608 name += 6;
1609 if (strncmp(name, "remotes/", 8) == 0)
1610 name += 8;
1611 if (strncmp(name, "tags/", 5) == 0) {
1612 err = got_object_open_as_tag(&tag, repo, re->id);
1613 if (err) {
1614 if (err->code != GOT_ERR_OBJ_TYPE)
1615 return err;
1616 /* Ref points at something other than a tag. */
1617 err = NULL;
1618 tag = NULL;
1621 cmp = got_object_id_cmp(tag ?
1622 got_object_tag_get_object_id(tag) : re->id, id);
1623 if (tag)
1624 got_object_tag_close(tag);
1625 if (cmp != 0)
1626 continue;
1627 s = refs_str;
1628 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1629 name) == -1) {
1630 err = got_error_from_errno("asprintf");
1631 free(s);
1632 return err;
1634 free(s);
1636 err = got_object_id_str(&id_str, id);
1637 if (err)
1638 return err;
1640 printf(GOT_COMMIT_SEP_STR);
1641 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1642 refs_str ? refs_str : "", refs_str ? ")" : "");
1643 free(id_str);
1644 id_str = NULL;
1645 free(refs_str);
1646 refs_str = NULL;
1647 printf("from: %s\n", got_object_commit_get_author(commit));
1648 committer_time = got_object_commit_get_committer_time(commit);
1649 datestr = get_datestr(&committer_time, datebuf);
1650 if (datestr)
1651 printf("date: %s UTC\n", datestr);
1652 author = got_object_commit_get_author(commit);
1653 committer = got_object_commit_get_committer(commit);
1654 if (strcmp(author, committer) != 0)
1655 printf("via: %s\n", committer);
1656 if (got_object_commit_get_nparents(commit) > 1) {
1657 const struct got_object_id_queue *parent_ids;
1658 struct got_object_qid *qid;
1659 int n = 1;
1660 parent_ids = got_object_commit_get_parent_ids(commit);
1661 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1662 err = got_object_id_str(&id_str, qid->id);
1663 if (err)
1664 return err;
1665 printf("parent %d: %s\n", n++, id_str);
1666 free(id_str);
1670 err = got_object_commit_get_logmsg(&logmsg0, commit);
1671 if (err)
1672 return err;
1674 logmsg = logmsg0;
1675 do {
1676 line = strsep(&logmsg, "\n");
1677 if (line)
1678 printf(" %s\n", line);
1679 } while (line);
1680 free(logmsg0);
1682 if (show_patch) {
1683 err = print_patch(commit, id, path, diff_context, repo);
1684 if (err == 0)
1685 printf("\n");
1688 if (fflush(stdout) != 0 && err == NULL)
1689 err = got_error_from_errno("fflush");
1690 return err;
1693 static const struct got_error *
1694 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1695 char *path, int show_patch, int diff_context, int limit,
1696 int first_parent_traversal, struct got_reflist_head *refs)
1698 const struct got_error *err;
1699 struct got_commit_graph *graph;
1701 err = got_commit_graph_open(&graph, root_id, path,
1702 first_parent_traversal, repo);
1703 if (err)
1704 return err;
1705 err = got_commit_graph_iter_start(graph, root_id, repo,
1706 check_cancelled, NULL);
1707 if (err)
1708 goto done;
1709 for (;;) {
1710 struct got_commit_object *commit;
1711 struct got_object_id *id;
1713 if (sigint_received || sigpipe_received)
1714 break;
1716 err = got_commit_graph_iter_next(&id, graph);
1717 if (err) {
1718 if (err->code == GOT_ERR_ITER_COMPLETED) {
1719 err = NULL;
1720 break;
1722 if (err->code != GOT_ERR_ITER_NEED_MORE)
1723 break;
1724 err = got_commit_graph_fetch_commits(graph, 1, repo,
1725 check_cancelled, NULL);
1726 if (err)
1727 break;
1728 else
1729 continue;
1731 if (id == NULL)
1732 break;
1734 err = got_object_open_as_commit(&commit, repo, id);
1735 if (err)
1736 break;
1737 err = print_commit(commit, id, repo, path, show_patch,
1738 diff_context, refs);
1739 got_object_commit_close(commit);
1740 if (err || (limit && --limit == 0))
1741 break;
1743 done:
1744 got_commit_graph_close(graph);
1745 return err;
1748 __dead static void
1749 usage_log(void)
1751 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1752 "[-r repository-path] [path]\n", getprogname());
1753 exit(1);
1756 static int
1757 get_default_log_limit(void)
1759 const char *got_default_log_limit;
1760 long long n;
1761 const char *errstr;
1763 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1764 if (got_default_log_limit == NULL)
1765 return 0;
1766 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1767 if (errstr != NULL)
1768 return 0;
1769 return n;
1772 static const struct got_error *
1773 cmd_log(int argc, char *argv[])
1775 const struct got_error *error;
1776 struct got_repository *repo = NULL;
1777 struct got_worktree *worktree = NULL;
1778 struct got_commit_object *commit = NULL;
1779 struct got_object_id *id = NULL;
1780 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1781 char *start_commit = NULL;
1782 int diff_context = 3, ch;
1783 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1784 const char *errstr;
1785 struct got_reflist_head refs;
1787 SIMPLEQ_INIT(&refs);
1789 #ifndef PROFILE
1790 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1791 NULL)
1792 == -1)
1793 err(1, "pledge");
1794 #endif
1796 limit = get_default_log_limit();
1798 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1799 switch (ch) {
1800 case 'p':
1801 show_patch = 1;
1802 break;
1803 case 'c':
1804 start_commit = optarg;
1805 break;
1806 case 'C':
1807 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1808 &errstr);
1809 if (errstr != NULL)
1810 err(1, "-C option %s", errstr);
1811 break;
1812 case 'l':
1813 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1814 if (errstr != NULL)
1815 err(1, "-l option %s", errstr);
1816 break;
1817 case 'f':
1818 first_parent_traversal = 1;
1819 break;
1820 case 'r':
1821 repo_path = realpath(optarg, NULL);
1822 if (repo_path == NULL)
1823 return got_error_from_errno2("realpath",
1824 optarg);
1825 got_path_strip_trailing_slashes(repo_path);
1826 break;
1827 default:
1828 usage_log();
1829 /* NOTREACHED */
1833 argc -= optind;
1834 argv += optind;
1836 cwd = getcwd(NULL, 0);
1837 if (cwd == NULL) {
1838 error = got_error_from_errno("getcwd");
1839 goto done;
1842 error = got_worktree_open(&worktree, cwd);
1843 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1844 goto done;
1845 error = NULL;
1847 if (argc == 0) {
1848 path = strdup("");
1849 if (path == NULL) {
1850 error = got_error_from_errno("strdup");
1851 goto done;
1853 } else if (argc == 1) {
1854 if (worktree) {
1855 error = got_worktree_resolve_path(&path, worktree,
1856 argv[0]);
1857 if (error)
1858 goto done;
1859 } else {
1860 path = strdup(argv[0]);
1861 if (path == NULL) {
1862 error = got_error_from_errno("strdup");
1863 goto done;
1866 } else
1867 usage_log();
1869 if (repo_path == NULL) {
1870 repo_path = worktree ?
1871 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1873 if (repo_path == NULL) {
1874 error = got_error_from_errno("strdup");
1875 goto done;
1878 error = got_repo_open(&repo, repo_path, NULL);
1879 if (error != NULL)
1880 goto done;
1882 error = apply_unveil(got_repo_get_path(repo), 1,
1883 worktree ? got_worktree_get_root_path(worktree) : NULL);
1884 if (error)
1885 goto done;
1887 if (start_commit == NULL) {
1888 struct got_reference *head_ref;
1889 error = got_ref_open(&head_ref, repo,
1890 worktree ? got_worktree_get_head_ref_name(worktree)
1891 : GOT_REF_HEAD, 0);
1892 if (error != NULL)
1893 return error;
1894 error = got_ref_resolve(&id, repo, head_ref);
1895 got_ref_close(head_ref);
1896 if (error != NULL)
1897 return error;
1898 error = got_object_open_as_commit(&commit, repo, id);
1899 } else {
1900 struct got_reference *ref;
1901 error = got_ref_open(&ref, repo, start_commit, 0);
1902 if (error == NULL) {
1903 int obj_type;
1904 error = got_ref_resolve(&id, repo, ref);
1905 got_ref_close(ref);
1906 if (error != NULL)
1907 goto done;
1908 error = got_object_get_type(&obj_type, repo, id);
1909 if (error != NULL)
1910 goto done;
1911 if (obj_type == GOT_OBJ_TYPE_TAG) {
1912 struct got_tag_object *tag;
1913 error = got_object_open_as_tag(&tag, repo, id);
1914 if (error != NULL)
1915 goto done;
1916 if (got_object_tag_get_object_type(tag) !=
1917 GOT_OBJ_TYPE_COMMIT) {
1918 got_object_tag_close(tag);
1919 error = got_error(GOT_ERR_OBJ_TYPE);
1920 goto done;
1922 free(id);
1923 id = got_object_id_dup(
1924 got_object_tag_get_object_id(tag));
1925 if (id == NULL)
1926 error = got_error_from_errno(
1927 "got_object_id_dup");
1928 got_object_tag_close(tag);
1929 if (error)
1930 goto done;
1931 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1932 error = got_error(GOT_ERR_OBJ_TYPE);
1933 goto done;
1935 error = got_object_open_as_commit(&commit, repo, id);
1936 if (error != NULL)
1937 goto done;
1939 if (commit == NULL) {
1940 error = got_repo_match_object_id_prefix(&id,
1941 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1942 if (error != NULL)
1943 return error;
1946 if (error != NULL)
1947 goto done;
1949 if (worktree) {
1950 const char *prefix = got_worktree_get_path_prefix(worktree);
1951 char *p;
1952 if (asprintf(&p, "%s%s%s", prefix,
1953 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1954 error = got_error_from_errno("asprintf");
1955 goto done;
1957 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1958 free(p);
1959 } else
1960 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1961 if (error != NULL)
1962 goto done;
1963 if (in_repo_path) {
1964 free(path);
1965 path = in_repo_path;
1968 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1969 if (error)
1970 goto done;
1972 error = print_commits(id, repo, path, show_patch,
1973 diff_context, limit, first_parent_traversal, &refs);
1974 done:
1975 free(path);
1976 free(repo_path);
1977 free(cwd);
1978 free(id);
1979 if (worktree)
1980 got_worktree_close(worktree);
1981 if (repo) {
1982 const struct got_error *repo_error;
1983 repo_error = got_repo_close(repo);
1984 if (error == NULL)
1985 error = repo_error;
1987 got_ref_list_free(&refs);
1988 return error;
1991 __dead static void
1992 usage_diff(void)
1994 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1995 "[-w] [object1 object2 | path]\n", getprogname());
1996 exit(1);
1999 struct print_diff_arg {
2000 struct got_repository *repo;
2001 struct got_worktree *worktree;
2002 int diff_context;
2003 const char *id_str;
2004 int header_shown;
2005 int diff_staged;
2006 int ignore_whitespace;
2009 static const struct got_error *
2010 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2011 const char *path, struct got_object_id *blob_id,
2012 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2014 struct print_diff_arg *a = arg;
2015 const struct got_error *err = NULL;
2016 struct got_blob_object *blob1 = NULL;
2017 FILE *f2 = NULL;
2018 char *abspath = NULL, *label1 = NULL;
2019 struct stat sb;
2021 if (a->diff_staged) {
2022 if (staged_status != GOT_STATUS_MODIFY &&
2023 staged_status != GOT_STATUS_ADD &&
2024 staged_status != GOT_STATUS_DELETE)
2025 return NULL;
2026 } else {
2027 if (staged_status == GOT_STATUS_DELETE)
2028 return NULL;
2029 if (status == GOT_STATUS_NONEXISTENT)
2030 return got_error_set_errno(ENOENT, path);
2031 if (status != GOT_STATUS_MODIFY &&
2032 status != GOT_STATUS_ADD &&
2033 status != GOT_STATUS_DELETE &&
2034 status != GOT_STATUS_CONFLICT)
2035 return NULL;
2038 if (!a->header_shown) {
2039 printf("diff %s %s%s\n", a->id_str,
2040 got_worktree_get_root_path(a->worktree),
2041 a->diff_staged ? " (staged changes)" : "");
2042 a->header_shown = 1;
2045 if (a->diff_staged) {
2046 const char *label1 = NULL, *label2 = NULL;
2047 switch (staged_status) {
2048 case GOT_STATUS_MODIFY:
2049 label1 = path;
2050 label2 = path;
2051 break;
2052 case GOT_STATUS_ADD:
2053 label2 = path;
2054 break;
2055 case GOT_STATUS_DELETE:
2056 label1 = path;
2057 break;
2058 default:
2059 return got_error(GOT_ERR_FILE_STATUS);
2061 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2062 label1, label2, a->diff_context, a->ignore_whitespace,
2063 a->repo, stdout);
2066 if (staged_status == GOT_STATUS_ADD ||
2067 staged_status == GOT_STATUS_MODIFY) {
2068 char *id_str;
2069 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2070 8192);
2071 if (err)
2072 goto done;
2073 err = got_object_id_str(&id_str, staged_blob_id);
2074 if (err)
2075 goto done;
2076 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2077 err = got_error_from_errno("asprintf");
2078 free(id_str);
2079 goto done;
2081 free(id_str);
2082 } else if (status != GOT_STATUS_ADD) {
2083 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2084 if (err)
2085 goto done;
2088 if (status != GOT_STATUS_DELETE) {
2089 if (asprintf(&abspath, "%s/%s",
2090 got_worktree_get_root_path(a->worktree), path) == -1) {
2091 err = got_error_from_errno("asprintf");
2092 goto done;
2095 f2 = fopen(abspath, "r");
2096 if (f2 == NULL) {
2097 err = got_error_from_errno2("fopen", abspath);
2098 goto done;
2100 if (lstat(abspath, &sb) == -1) {
2101 err = got_error_from_errno2("lstat", abspath);
2102 goto done;
2104 } else
2105 sb.st_size = 0;
2107 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2108 a->diff_context, a->ignore_whitespace, stdout);
2109 done:
2110 if (blob1)
2111 got_object_blob_close(blob1);
2112 if (f2 && fclose(f2) != 0 && err == NULL)
2113 err = got_error_from_errno("fclose");
2114 free(abspath);
2115 return err;
2118 static const struct got_error *
2119 match_object_id(struct got_object_id **id, char **label,
2120 const char *id_str, int obj_type, int resolve_tags,
2121 struct got_repository *repo)
2123 const struct got_error *err;
2124 struct got_tag_object *tag;
2125 struct got_reference *ref = NULL;
2127 *id = NULL;
2128 *label = NULL;
2130 if (resolve_tags) {
2131 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2132 repo);
2133 if (err == NULL) {
2134 *id = got_object_id_dup(
2135 got_object_tag_get_object_id(tag));
2136 if (*id == NULL)
2137 err = got_error_from_errno("got_object_id_dup");
2138 else if (asprintf(label, "refs/tags/%s",
2139 got_object_tag_get_name(tag)) == -1) {
2140 err = got_error_from_errno("asprintf");
2141 free(*id);
2142 *id = NULL;
2144 got_object_tag_close(tag);
2145 return err;
2146 } else if (err->code != GOT_ERR_NO_OBJ)
2147 return err;
2150 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2151 if (err) {
2152 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2153 return err;
2154 err = got_ref_open(&ref, repo, id_str, 0);
2155 if (err != NULL)
2156 goto done;
2157 *label = strdup(got_ref_get_name(ref));
2158 if (*label == NULL) {
2159 err = got_error_from_errno("strdup");
2160 goto done;
2162 err = got_ref_resolve(id, repo, ref);
2163 } else {
2164 err = got_object_id_str(label, *id);
2165 if (*label == NULL) {
2166 err = got_error_from_errno("strdup");
2167 goto done;
2170 done:
2171 if (ref)
2172 got_ref_close(ref);
2173 return err;
2177 static const struct got_error *
2178 cmd_diff(int argc, char *argv[])
2180 const struct got_error *error;
2181 struct got_repository *repo = NULL;
2182 struct got_worktree *worktree = NULL;
2183 char *cwd = NULL, *repo_path = NULL;
2184 struct got_object_id *id1 = NULL, *id2 = NULL;
2185 const char *id_str1 = NULL, *id_str2 = NULL;
2186 char *label1 = NULL, *label2 = NULL;
2187 int type1, type2;
2188 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2189 const char *errstr;
2190 char *path = NULL;
2192 #ifndef PROFILE
2193 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2194 NULL) == -1)
2195 err(1, "pledge");
2196 #endif
2198 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2199 switch (ch) {
2200 case 'C':
2201 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2202 if (errstr != NULL)
2203 err(1, "-C option %s", errstr);
2204 break;
2205 case 'r':
2206 repo_path = realpath(optarg, NULL);
2207 if (repo_path == NULL)
2208 return got_error_from_errno2("realpath",
2209 optarg);
2210 got_path_strip_trailing_slashes(repo_path);
2211 break;
2212 case 's':
2213 diff_staged = 1;
2214 break;
2215 case 'w':
2216 ignore_whitespace = 1;
2217 break;
2218 default:
2219 usage_diff();
2220 /* NOTREACHED */
2224 argc -= optind;
2225 argv += optind;
2227 cwd = getcwd(NULL, 0);
2228 if (cwd == NULL) {
2229 error = got_error_from_errno("getcwd");
2230 goto done;
2232 error = got_worktree_open(&worktree, cwd);
2233 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2234 goto done;
2235 if (argc <= 1) {
2236 if (worktree == NULL) {
2237 error = got_error(GOT_ERR_NOT_WORKTREE);
2238 goto done;
2240 if (repo_path)
2241 errx(1,
2242 "-r option can't be used when diffing a work tree");
2243 repo_path = strdup(got_worktree_get_repo_path(worktree));
2244 if (repo_path == NULL) {
2245 error = got_error_from_errno("strdup");
2246 goto done;
2248 if (argc == 1) {
2249 error = got_worktree_resolve_path(&path, worktree,
2250 argv[0]);
2251 if (error)
2252 goto done;
2253 } else {
2254 path = strdup("");
2255 if (path == NULL) {
2256 error = got_error_from_errno("strdup");
2257 goto done;
2260 } else if (argc == 2) {
2261 if (diff_staged)
2262 errx(1, "-s option can't be used when diffing "
2263 "objects in repository");
2264 id_str1 = argv[0];
2265 id_str2 = argv[1];
2266 if (worktree && repo_path == NULL) {
2267 repo_path =
2268 strdup(got_worktree_get_repo_path(worktree));
2269 if (repo_path == NULL) {
2270 error = got_error_from_errno("strdup");
2271 goto done;
2274 } else
2275 usage_diff();
2277 if (repo_path == NULL) {
2278 repo_path = getcwd(NULL, 0);
2279 if (repo_path == NULL)
2280 return got_error_from_errno("getcwd");
2283 error = got_repo_open(&repo, repo_path, NULL);
2284 free(repo_path);
2285 if (error != NULL)
2286 goto done;
2288 error = apply_unveil(got_repo_get_path(repo), 1,
2289 worktree ? got_worktree_get_root_path(worktree) : NULL);
2290 if (error)
2291 goto done;
2293 if (argc <= 1) {
2294 struct print_diff_arg arg;
2295 struct got_pathlist_head paths;
2296 char *id_str;
2298 TAILQ_INIT(&paths);
2300 error = got_object_id_str(&id_str,
2301 got_worktree_get_base_commit_id(worktree));
2302 if (error)
2303 goto done;
2304 arg.repo = repo;
2305 arg.worktree = worktree;
2306 arg.diff_context = diff_context;
2307 arg.id_str = id_str;
2308 arg.header_shown = 0;
2309 arg.diff_staged = diff_staged;
2310 arg.ignore_whitespace = ignore_whitespace;
2312 error = got_pathlist_append(&paths, path, NULL);
2313 if (error)
2314 goto done;
2316 error = got_worktree_status(worktree, &paths, repo, print_diff,
2317 &arg, check_cancelled, NULL);
2318 free(id_str);
2319 got_pathlist_free(&paths);
2320 goto done;
2323 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2324 repo);
2325 if (error)
2326 goto done;
2328 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2329 repo);
2330 if (error)
2331 goto done;
2333 error = got_object_get_type(&type1, repo, id1);
2334 if (error)
2335 goto done;
2337 error = got_object_get_type(&type2, repo, id2);
2338 if (error)
2339 goto done;
2341 if (type1 != type2) {
2342 error = got_error(GOT_ERR_OBJ_TYPE);
2343 goto done;
2346 switch (type1) {
2347 case GOT_OBJ_TYPE_BLOB:
2348 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2349 diff_context, ignore_whitespace, repo, stdout);
2350 break;
2351 case GOT_OBJ_TYPE_TREE:
2352 error = got_diff_objects_as_trees(id1, id2, "", "",
2353 diff_context, ignore_whitespace, repo, stdout);
2354 break;
2355 case GOT_OBJ_TYPE_COMMIT:
2356 printf("diff %s %s\n", label1, label2);
2357 error = got_diff_objects_as_commits(id1, id2, diff_context,
2358 ignore_whitespace, repo, stdout);
2359 break;
2360 default:
2361 error = got_error(GOT_ERR_OBJ_TYPE);
2364 done:
2365 free(label1);
2366 free(label2);
2367 free(id1);
2368 free(id2);
2369 free(path);
2370 if (worktree)
2371 got_worktree_close(worktree);
2372 if (repo) {
2373 const struct got_error *repo_error;
2374 repo_error = got_repo_close(repo);
2375 if (error == NULL)
2376 error = repo_error;
2378 return error;
2381 __dead static void
2382 usage_blame(void)
2384 fprintf(stderr,
2385 "usage: %s blame [-c commit] [-r repository-path] path\n",
2386 getprogname());
2387 exit(1);
2390 struct blame_line {
2391 int annotated;
2392 char *id_str;
2393 char *committer;
2394 char datebuf[11]; /* YYYY-MM-DD + NUL */
2397 struct blame_cb_args {
2398 struct blame_line *lines;
2399 int nlines;
2400 int nlines_prec;
2401 int lineno_cur;
2402 off_t *line_offsets;
2403 FILE *f;
2404 struct got_repository *repo;
2407 static const struct got_error *
2408 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2410 const struct got_error *err = NULL;
2411 struct blame_cb_args *a = arg;
2412 struct blame_line *bline;
2413 char *line = NULL;
2414 size_t linesize = 0;
2415 struct got_commit_object *commit = NULL;
2416 off_t offset;
2417 struct tm tm;
2418 time_t committer_time;
2420 if (nlines != a->nlines ||
2421 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2422 return got_error(GOT_ERR_RANGE);
2424 if (sigint_received)
2425 return got_error(GOT_ERR_ITER_COMPLETED);
2427 if (lineno == -1)
2428 return NULL; /* no change in this commit */
2430 /* Annotate this line. */
2431 bline = &a->lines[lineno - 1];
2432 if (bline->annotated)
2433 return NULL;
2434 err = got_object_id_str(&bline->id_str, id);
2435 if (err)
2436 return err;
2438 err = got_object_open_as_commit(&commit, a->repo, id);
2439 if (err)
2440 goto done;
2442 bline->committer = strdup(got_object_commit_get_committer(commit));
2443 if (bline->committer == NULL) {
2444 err = got_error_from_errno("strdup");
2445 goto done;
2448 committer_time = got_object_commit_get_committer_time(commit);
2449 if (localtime_r(&committer_time, &tm) == NULL)
2450 return got_error_from_errno("localtime_r");
2451 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G/%m/%d",
2452 &tm) >= sizeof(bline->datebuf)) {
2453 err = got_error(GOT_ERR_NO_SPACE);
2454 goto done;
2456 bline->annotated = 1;
2458 /* Print lines annotated so far. */
2459 bline = &a->lines[a->lineno_cur - 1];
2460 if (!bline->annotated)
2461 goto done;
2463 offset = a->line_offsets[a->lineno_cur - 1];
2464 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2465 err = got_error_from_errno("fseeko");
2466 goto done;
2469 while (bline->annotated) {
2470 char *smallerthan, *at, *nl, *committer;
2471 size_t len;
2473 if (getline(&line, &linesize, a->f) == -1) {
2474 if (ferror(a->f))
2475 err = got_error_from_errno("getline");
2476 break;
2479 committer = bline->committer;
2480 smallerthan = strchr(committer, '<');
2481 if (smallerthan && smallerthan[1] != '\0')
2482 committer = smallerthan + 1;
2483 at = strchr(committer, '@');
2484 if (at)
2485 *at = '\0';
2486 len = strlen(committer);
2487 if (len >= 9)
2488 committer[8] = '\0';
2490 nl = strchr(line, '\n');
2491 if (nl)
2492 *nl = '\0';
2493 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2494 bline->id_str, bline->datebuf, committer, line);
2496 a->lineno_cur++;
2497 bline = &a->lines[a->lineno_cur - 1];
2499 done:
2500 if (commit)
2501 got_object_commit_close(commit);
2502 free(line);
2503 return err;
2506 static const struct got_error *
2507 cmd_blame(int argc, char *argv[])
2509 const struct got_error *error;
2510 struct got_repository *repo = NULL;
2511 struct got_worktree *worktree = NULL;
2512 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2513 struct got_object_id *obj_id = NULL;
2514 struct got_object_id *commit_id = NULL;
2515 struct got_blob_object *blob = NULL;
2516 char *commit_id_str = NULL;
2517 struct blame_cb_args bca;
2518 int ch, obj_type, i;
2519 size_t filesize;
2521 memset(&bca, 0, sizeof(bca));
2523 #ifndef PROFILE
2524 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2525 NULL) == -1)
2526 err(1, "pledge");
2527 #endif
2529 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2530 switch (ch) {
2531 case 'c':
2532 commit_id_str = optarg;
2533 break;
2534 case 'r':
2535 repo_path = realpath(optarg, NULL);
2536 if (repo_path == NULL)
2537 return got_error_from_errno2("realpath",
2538 optarg);
2539 got_path_strip_trailing_slashes(repo_path);
2540 break;
2541 default:
2542 usage_blame();
2543 /* NOTREACHED */
2547 argc -= optind;
2548 argv += optind;
2550 if (argc == 1)
2551 path = argv[0];
2552 else
2553 usage_blame();
2555 cwd = getcwd(NULL, 0);
2556 if (cwd == NULL) {
2557 error = got_error_from_errno("getcwd");
2558 goto done;
2560 if (repo_path == NULL) {
2561 error = got_worktree_open(&worktree, cwd);
2562 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2563 goto done;
2564 else
2565 error = NULL;
2566 if (worktree) {
2567 repo_path =
2568 strdup(got_worktree_get_repo_path(worktree));
2569 if (repo_path == NULL)
2570 error = got_error_from_errno("strdup");
2571 if (error)
2572 goto done;
2573 } else {
2574 repo_path = strdup(cwd);
2575 if (repo_path == NULL) {
2576 error = got_error_from_errno("strdup");
2577 goto done;
2582 error = got_repo_open(&repo, repo_path, NULL);
2583 if (error != NULL)
2584 goto done;
2586 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2587 if (error)
2588 goto done;
2590 if (worktree) {
2591 const char *prefix = got_worktree_get_path_prefix(worktree);
2592 char *p, *worktree_subdir = cwd +
2593 strlen(got_worktree_get_root_path(worktree));
2594 if (asprintf(&p, "%s%s%s%s%s",
2595 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2596 worktree_subdir, worktree_subdir[0] ? "/" : "",
2597 path) == -1) {
2598 error = got_error_from_errno("asprintf");
2599 goto done;
2601 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2602 free(p);
2603 } else {
2604 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2606 if (error)
2607 goto done;
2609 if (commit_id_str == NULL) {
2610 struct got_reference *head_ref;
2611 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2612 if (error != NULL)
2613 goto done;
2614 error = got_ref_resolve(&commit_id, repo, head_ref);
2615 got_ref_close(head_ref);
2616 if (error != NULL)
2617 goto done;
2618 } else {
2619 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2620 if (error)
2621 goto done;
2624 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2625 if (error)
2626 goto done;
2627 if (obj_id == NULL) {
2628 error = got_error(GOT_ERR_NO_OBJ);
2629 goto done;
2632 error = got_object_get_type(&obj_type, repo, obj_id);
2633 if (error)
2634 goto done;
2636 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2637 error = got_error(GOT_ERR_OBJ_TYPE);
2638 goto done;
2641 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2642 if (error)
2643 goto done;
2644 bca.f = got_opentemp();
2645 if (bca.f == NULL) {
2646 error = got_error_from_errno("got_opentemp");
2647 goto done;
2649 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2650 &bca.line_offsets, bca.f, blob);
2651 if (error || bca.nlines == 0)
2652 goto done;
2654 /* Don't include \n at EOF in the blame line count. */
2655 if (bca.line_offsets[bca.nlines - 1] == filesize)
2656 bca.nlines--;
2658 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2659 if (bca.lines == NULL) {
2660 error = got_error_from_errno("calloc");
2661 goto done;
2663 bca.lineno_cur = 1;
2664 bca.nlines_prec = 0;
2665 i = bca.nlines;
2666 while (i > 0) {
2667 i /= 10;
2668 bca.nlines_prec++;
2670 bca.repo = repo;
2672 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2673 check_cancelled, NULL);
2674 if (error)
2675 goto done;
2676 done:
2677 free(in_repo_path);
2678 free(repo_path);
2679 free(cwd);
2680 free(commit_id);
2681 free(obj_id);
2682 if (blob)
2683 got_object_blob_close(blob);
2684 if (worktree)
2685 got_worktree_close(worktree);
2686 if (repo) {
2687 const struct got_error *repo_error;
2688 repo_error = got_repo_close(repo);
2689 if (error == NULL)
2690 error = repo_error;
2692 if (bca.lines) {
2693 for (i = 0; i < bca.nlines; i++) {
2694 struct blame_line *bline = &bca.lines[i];
2695 free(bline->id_str);
2696 free(bline->committer);
2698 free(bca.lines);
2700 free(bca.line_offsets);
2701 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2702 error = got_error_from_errno("fclose");
2703 return error;
2706 __dead static void
2707 usage_tree(void)
2709 fprintf(stderr,
2710 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2711 getprogname());
2712 exit(1);
2715 static void
2716 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2717 const char *root_path)
2719 int is_root_path = (strcmp(path, root_path) == 0);
2720 const char *modestr = "";
2722 path += strlen(root_path);
2723 while (path[0] == '/')
2724 path++;
2726 if (got_object_tree_entry_is_submodule(te))
2727 modestr = "$";
2728 else if (S_ISLNK(te->mode))
2729 modestr = "@";
2730 else if (S_ISDIR(te->mode))
2731 modestr = "/";
2732 else if (te->mode & S_IXUSR)
2733 modestr = "*";
2735 printf("%s%s%s%s%s\n", id ? id : "", path,
2736 is_root_path ? "" : "/", te->name, modestr);
2739 static const struct got_error *
2740 print_tree(const char *path, struct got_object_id *commit_id,
2741 int show_ids, int recurse, const char *root_path,
2742 struct got_repository *repo)
2744 const struct got_error *err = NULL;
2745 struct got_object_id *tree_id = NULL;
2746 struct got_tree_object *tree = NULL;
2747 const struct got_tree_entries *entries;
2748 struct got_tree_entry *te;
2750 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2751 if (err)
2752 goto done;
2754 err = got_object_open_as_tree(&tree, repo, tree_id);
2755 if (err)
2756 goto done;
2757 entries = got_object_tree_get_entries(tree);
2758 te = SIMPLEQ_FIRST(&entries->head);
2759 while (te) {
2760 char *id = NULL;
2762 if (sigint_received || sigpipe_received)
2763 break;
2765 if (show_ids) {
2766 char *id_str;
2767 err = got_object_id_str(&id_str, te->id);
2768 if (err)
2769 goto done;
2770 if (asprintf(&id, "%s ", id_str) == -1) {
2771 err = got_error_from_errno("asprintf");
2772 free(id_str);
2773 goto done;
2775 free(id_str);
2777 print_entry(te, id, path, root_path);
2778 free(id);
2780 if (recurse && S_ISDIR(te->mode)) {
2781 char *child_path;
2782 if (asprintf(&child_path, "%s%s%s", path,
2783 path[0] == '/' && path[1] == '\0' ? "" : "/",
2784 te->name) == -1) {
2785 err = got_error_from_errno("asprintf");
2786 goto done;
2788 err = print_tree(child_path, commit_id, show_ids, 1,
2789 root_path, repo);
2790 free(child_path);
2791 if (err)
2792 goto done;
2795 te = SIMPLEQ_NEXT(te, entry);
2797 done:
2798 if (tree)
2799 got_object_tree_close(tree);
2800 free(tree_id);
2801 return err;
2804 static const struct got_error *
2805 cmd_tree(int argc, char *argv[])
2807 const struct got_error *error;
2808 struct got_repository *repo = NULL;
2809 struct got_worktree *worktree = NULL;
2810 const char *path;
2811 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2812 struct got_object_id *commit_id = NULL;
2813 char *commit_id_str = NULL;
2814 int show_ids = 0, recurse = 0;
2815 int ch;
2817 #ifndef PROFILE
2818 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2819 NULL) == -1)
2820 err(1, "pledge");
2821 #endif
2823 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2824 switch (ch) {
2825 case 'c':
2826 commit_id_str = optarg;
2827 break;
2828 case 'r':
2829 repo_path = realpath(optarg, NULL);
2830 if (repo_path == NULL)
2831 return got_error_from_errno2("realpath",
2832 optarg);
2833 got_path_strip_trailing_slashes(repo_path);
2834 break;
2835 case 'i':
2836 show_ids = 1;
2837 break;
2838 case 'R':
2839 recurse = 1;
2840 break;
2841 default:
2842 usage_tree();
2843 /* NOTREACHED */
2847 argc -= optind;
2848 argv += optind;
2850 if (argc == 1)
2851 path = argv[0];
2852 else if (argc > 1)
2853 usage_tree();
2854 else
2855 path = NULL;
2857 cwd = getcwd(NULL, 0);
2858 if (cwd == NULL) {
2859 error = got_error_from_errno("getcwd");
2860 goto done;
2862 if (repo_path == NULL) {
2863 error = got_worktree_open(&worktree, cwd);
2864 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2865 goto done;
2866 else
2867 error = NULL;
2868 if (worktree) {
2869 repo_path =
2870 strdup(got_worktree_get_repo_path(worktree));
2871 if (repo_path == NULL)
2872 error = got_error_from_errno("strdup");
2873 if (error)
2874 goto done;
2875 } else {
2876 repo_path = strdup(cwd);
2877 if (repo_path == NULL) {
2878 error = got_error_from_errno("strdup");
2879 goto done;
2884 error = got_repo_open(&repo, repo_path, NULL);
2885 if (error != NULL)
2886 goto done;
2888 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2889 if (error)
2890 goto done;
2892 if (path == NULL) {
2893 if (worktree) {
2894 char *p, *worktree_subdir = cwd +
2895 strlen(got_worktree_get_root_path(worktree));
2896 if (asprintf(&p, "%s/%s",
2897 got_worktree_get_path_prefix(worktree),
2898 worktree_subdir) == -1) {
2899 error = got_error_from_errno("asprintf");
2900 goto done;
2902 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2903 free(p);
2904 if (error)
2905 goto done;
2906 } else
2907 path = "/";
2909 if (in_repo_path == NULL) {
2910 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2911 if (error != NULL)
2912 goto done;
2915 if (commit_id_str == NULL) {
2916 struct got_reference *head_ref;
2917 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2918 if (error != NULL)
2919 goto done;
2920 error = got_ref_resolve(&commit_id, repo, head_ref);
2921 got_ref_close(head_ref);
2922 if (error != NULL)
2923 goto done;
2924 } else {
2925 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2926 if (error)
2927 goto done;
2930 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2931 in_repo_path, repo);
2932 done:
2933 free(in_repo_path);
2934 free(repo_path);
2935 free(cwd);
2936 free(commit_id);
2937 if (worktree)
2938 got_worktree_close(worktree);
2939 if (repo) {
2940 const struct got_error *repo_error;
2941 repo_error = got_repo_close(repo);
2942 if (error == NULL)
2943 error = repo_error;
2945 return error;
2948 __dead static void
2949 usage_status(void)
2951 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2952 exit(1);
2955 static const struct got_error *
2956 print_status(void *arg, unsigned char status, unsigned char staged_status,
2957 const char *path, struct got_object_id *blob_id,
2958 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2960 if (status == staged_status && (status == GOT_STATUS_DELETE))
2961 status = GOT_STATUS_NO_CHANGE;
2962 printf("%c%c %s\n", status, staged_status, path);
2963 return NULL;
2966 static const struct got_error *
2967 cmd_status(int argc, char *argv[])
2969 const struct got_error *error = NULL;
2970 struct got_repository *repo = NULL;
2971 struct got_worktree *worktree = NULL;
2972 char *cwd = NULL;
2973 struct got_pathlist_head paths;
2974 struct got_pathlist_entry *pe;
2975 int ch;
2977 TAILQ_INIT(&paths);
2979 while ((ch = getopt(argc, argv, "")) != -1) {
2980 switch (ch) {
2981 default:
2982 usage_status();
2983 /* NOTREACHED */
2987 argc -= optind;
2988 argv += optind;
2990 #ifndef PROFILE
2991 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2992 NULL) == -1)
2993 err(1, "pledge");
2994 #endif
2995 cwd = getcwd(NULL, 0);
2996 if (cwd == NULL) {
2997 error = got_error_from_errno("getcwd");
2998 goto done;
3001 error = got_worktree_open(&worktree, cwd);
3002 if (error != NULL)
3003 goto done;
3005 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3006 NULL);
3007 if (error != NULL)
3008 goto done;
3010 error = apply_unveil(got_repo_get_path(repo), 1,
3011 got_worktree_get_root_path(worktree));
3012 if (error)
3013 goto done;
3015 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3016 if (error)
3017 goto done;
3019 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3020 check_cancelled, NULL);
3021 done:
3022 TAILQ_FOREACH(pe, &paths, entry)
3023 free((char *)pe->path);
3024 got_pathlist_free(&paths);
3025 free(cwd);
3026 return error;
3029 __dead static void
3030 usage_ref(void)
3032 fprintf(stderr,
3033 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3034 getprogname());
3035 exit(1);
3038 static const struct got_error *
3039 list_refs(struct got_repository *repo)
3041 static const struct got_error *err = NULL;
3042 struct got_reflist_head refs;
3043 struct got_reflist_entry *re;
3045 SIMPLEQ_INIT(&refs);
3046 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3047 if (err)
3048 return err;
3050 SIMPLEQ_FOREACH(re, &refs, entry) {
3051 char *refstr;
3052 refstr = got_ref_to_str(re->ref);
3053 if (refstr == NULL)
3054 return got_error_from_errno("got_ref_to_str");
3055 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3056 free(refstr);
3059 got_ref_list_free(&refs);
3060 return NULL;
3063 static const struct got_error *
3064 delete_ref(struct got_repository *repo, const char *refname)
3066 const struct got_error *err = NULL;
3067 struct got_reference *ref;
3069 err = got_ref_open(&ref, repo, refname, 0);
3070 if (err)
3071 return err;
3073 err = got_ref_delete(ref, repo);
3074 got_ref_close(ref);
3075 return err;
3078 static const struct got_error *
3079 add_ref(struct got_repository *repo, const char *refname, const char *target)
3081 const struct got_error *err = NULL;
3082 struct got_object_id *id;
3083 struct got_reference *ref = NULL;
3086 * Don't let the user create a reference named '-'.
3087 * While technically a valid reference name, this case is usually
3088 * an unintended typo.
3090 if (refname[0] == '-' && refname[1] == '\0')
3091 return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3093 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3094 repo);
3095 if (err) {
3096 struct got_reference *target_ref;
3098 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3099 return err;
3100 err = got_ref_open(&target_ref, repo, target, 0);
3101 if (err)
3102 return err;
3103 err = got_ref_resolve(&id, repo, target_ref);
3104 got_ref_close(target_ref);
3105 if (err)
3106 return err;
3109 err = got_ref_alloc(&ref, refname, id);
3110 if (err)
3111 goto done;
3113 err = got_ref_write(ref, repo);
3114 done:
3115 if (ref)
3116 got_ref_close(ref);
3117 free(id);
3118 return err;
3121 static const struct got_error *
3122 add_symref(struct got_repository *repo, const char *refname, const char *target)
3124 const struct got_error *err = NULL;
3125 struct got_reference *ref = NULL;
3126 struct got_reference *target_ref = NULL;
3129 * Don't let the user create a reference named '-'.
3130 * While technically a valid reference name, this case is usually
3131 * an unintended typo.
3133 if (refname[0] == '-' && refname[1] == '\0')
3134 return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3136 err = got_ref_open(&target_ref, repo, target, 0);
3137 if (err)
3138 return err;
3140 err = got_ref_alloc_symref(&ref, refname, target_ref);
3141 if (err)
3142 goto done;
3144 err = got_ref_write(ref, repo);
3145 done:
3146 if (target_ref)
3147 got_ref_close(target_ref);
3148 if (ref)
3149 got_ref_close(ref);
3150 return err;
3153 static const struct got_error *
3154 cmd_ref(int argc, char *argv[])
3156 const struct got_error *error = NULL;
3157 struct got_repository *repo = NULL;
3158 struct got_worktree *worktree = NULL;
3159 char *cwd = NULL, *repo_path = NULL;
3160 int ch, do_list = 0, create_symref = 0;
3161 const char *delref = NULL;
3163 /* TODO: Add -s option for adding symbolic references. */
3164 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3165 switch (ch) {
3166 case 'd':
3167 delref = optarg;
3168 break;
3169 case 'r':
3170 repo_path = realpath(optarg, NULL);
3171 if (repo_path == NULL)
3172 return got_error_from_errno2("realpath",
3173 optarg);
3174 got_path_strip_trailing_slashes(repo_path);
3175 break;
3176 case 'l':
3177 do_list = 1;
3178 break;
3179 case 's':
3180 create_symref = 1;
3181 break;
3182 default:
3183 usage_ref();
3184 /* NOTREACHED */
3188 if (do_list && delref)
3189 errx(1, "-l and -d options are mutually exclusive\n");
3191 argc -= optind;
3192 argv += optind;
3194 if (do_list || delref) {
3195 if (create_symref)
3196 errx(1, "-s option cannot be used together with the "
3197 "-l or -d options");
3198 if (argc > 0)
3199 usage_ref();
3200 } else if (argc != 2)
3201 usage_ref();
3203 #ifndef PROFILE
3204 if (do_list) {
3205 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3206 NULL) == -1)
3207 err(1, "pledge");
3208 } else {
3209 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3210 "sendfd unveil", NULL) == -1)
3211 err(1, "pledge");
3213 #endif
3214 cwd = getcwd(NULL, 0);
3215 if (cwd == NULL) {
3216 error = got_error_from_errno("getcwd");
3217 goto done;
3220 if (repo_path == NULL) {
3221 error = got_worktree_open(&worktree, cwd);
3222 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3223 goto done;
3224 else
3225 error = NULL;
3226 if (worktree) {
3227 repo_path =
3228 strdup(got_worktree_get_repo_path(worktree));
3229 if (repo_path == NULL)
3230 error = got_error_from_errno("strdup");
3231 if (error)
3232 goto done;
3233 } else {
3234 repo_path = strdup(cwd);
3235 if (repo_path == NULL) {
3236 error = got_error_from_errno("strdup");
3237 goto done;
3242 error = got_repo_open(&repo, repo_path, NULL);
3243 if (error != NULL)
3244 goto done;
3246 error = apply_unveil(got_repo_get_path(repo), do_list,
3247 worktree ? got_worktree_get_root_path(worktree) : NULL);
3248 if (error)
3249 goto done;
3251 if (do_list)
3252 error = list_refs(repo);
3253 else if (delref)
3254 error = delete_ref(repo, delref);
3255 else if (create_symref)
3256 error = add_symref(repo, argv[0], argv[1]);
3257 else
3258 error = add_ref(repo, argv[0], argv[1]);
3259 done:
3260 if (repo)
3261 got_repo_close(repo);
3262 if (worktree)
3263 got_worktree_close(worktree);
3264 free(cwd);
3265 free(repo_path);
3266 return error;
3269 __dead static void
3270 usage_branch(void)
3272 fprintf(stderr,
3273 "usage: %s branch [-r repository] [-l] | -d name | "
3274 "[name [commit]]\n", getprogname());
3275 exit(1);
3278 static const struct got_error *
3279 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3280 struct got_reference *ref)
3282 const struct got_error *err = NULL;
3283 const char *refname, *marker = " ";
3284 char *refstr;
3286 refname = got_ref_get_name(ref);
3287 if (worktree && strcmp(refname,
3288 got_worktree_get_head_ref_name(worktree)) == 0) {
3289 struct got_object_id *id = NULL;
3291 err = got_ref_resolve(&id, repo, ref);
3292 if (err)
3293 return err;
3294 if (got_object_id_cmp(id,
3295 got_worktree_get_base_commit_id(worktree)) == 0)
3296 marker = "* ";
3297 else
3298 marker = "~ ";
3299 free(id);
3302 if (strncmp(refname, "refs/heads/", 11) == 0)
3303 refname += 11;
3304 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3305 refname += 18;
3307 refstr = got_ref_to_str(ref);
3308 if (refstr == NULL)
3309 return got_error_from_errno("got_ref_to_str");
3311 printf("%s%s: %s\n", marker, refname, refstr);
3312 free(refstr);
3313 return NULL;
3316 static const struct got_error *
3317 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3319 const char *refname;
3321 if (worktree == NULL)
3322 return got_error(GOT_ERR_NOT_WORKTREE);
3324 refname = got_worktree_get_head_ref_name(worktree);
3326 if (strncmp(refname, "refs/heads/", 11) == 0)
3327 refname += 11;
3328 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3329 refname += 18;
3331 printf("%s\n", refname);
3333 return NULL;
3336 static const struct got_error *
3337 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3339 static const struct got_error *err = NULL;
3340 struct got_reflist_head refs;
3341 struct got_reflist_entry *re;
3342 struct got_reference *temp_ref = NULL;
3343 int rebase_in_progress, histedit_in_progress;
3345 SIMPLEQ_INIT(&refs);
3347 if (worktree) {
3348 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3349 worktree);
3350 if (err)
3351 return err;
3353 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3354 worktree);
3355 if (err)
3356 return err;
3358 if (rebase_in_progress || histedit_in_progress) {
3359 err = got_ref_open(&temp_ref, repo,
3360 got_worktree_get_head_ref_name(worktree), 0);
3361 if (err)
3362 return err;
3363 list_branch(repo, worktree, temp_ref);
3364 got_ref_close(temp_ref);
3368 err = got_ref_list(&refs, repo, "refs/heads",
3369 got_ref_cmp_by_name, NULL);
3370 if (err)
3371 return err;
3373 SIMPLEQ_FOREACH(re, &refs, entry)
3374 list_branch(repo, worktree, re->ref);
3376 got_ref_list_free(&refs);
3377 return NULL;
3380 static const struct got_error *
3381 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3382 const char *branch_name)
3384 const struct got_error *err = NULL;
3385 struct got_reference *ref = NULL;
3386 char *refname;
3388 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3389 return got_error_from_errno("asprintf");
3391 err = got_ref_open(&ref, repo, refname, 0);
3392 if (err)
3393 goto done;
3395 if (worktree &&
3396 strcmp(got_worktree_get_head_ref_name(worktree),
3397 got_ref_get_name(ref)) == 0) {
3398 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3399 "will not delete this work tree's current branch");
3400 goto done;
3403 err = got_ref_delete(ref, repo);
3404 done:
3405 if (ref)
3406 got_ref_close(ref);
3407 free(refname);
3408 return err;
3411 static const struct got_error *
3412 add_branch(struct got_repository *repo, const char *branch_name,
3413 const char *base_branch)
3415 const struct got_error *err = NULL;
3416 struct got_object_id *id = NULL;
3417 char *label;
3418 struct got_reference *ref = NULL;
3419 char *base_refname = NULL, *refname = NULL;
3422 * Don't let the user create a branch named '-'.
3423 * While technically a valid reference name, this case is usually
3424 * an unintended typo.
3426 if (branch_name[0] == '-' && branch_name[1] == '\0')
3427 return got_error_path(branch_name, GOT_ERR_BAD_REF_NAME);
3429 err = match_object_id(&id, &label, base_branch,
3430 GOT_OBJ_TYPE_COMMIT, 1, repo);
3431 if (err)
3432 return err;
3434 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3435 err = got_error_from_errno("asprintf");
3436 goto done;
3439 err = got_ref_open(&ref, repo, refname, 0);
3440 if (err == NULL) {
3441 err = got_error(GOT_ERR_BRANCH_EXISTS);
3442 goto done;
3443 } else if (err->code != GOT_ERR_NOT_REF)
3444 goto done;
3446 err = got_ref_alloc(&ref, refname, id);
3447 if (err)
3448 goto done;
3450 err = got_ref_write(ref, repo);
3451 done:
3452 if (ref)
3453 got_ref_close(ref);
3454 free(id);
3455 free(base_refname);
3456 free(refname);
3457 return err;
3460 static const struct got_error *
3461 cmd_branch(int argc, char *argv[])
3463 const struct got_error *error = NULL;
3464 struct got_repository *repo = NULL;
3465 struct got_worktree *worktree = NULL;
3466 char *cwd = NULL, *repo_path = NULL;
3467 int ch, do_list = 0, do_show = 0;
3468 const char *delref = NULL;
3470 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3471 switch (ch) {
3472 case 'd':
3473 delref = optarg;
3474 break;
3475 case 'r':
3476 repo_path = realpath(optarg, NULL);
3477 if (repo_path == NULL)
3478 return got_error_from_errno2("realpath",
3479 optarg);
3480 got_path_strip_trailing_slashes(repo_path);
3481 break;
3482 case 'l':
3483 do_list = 1;
3484 break;
3485 default:
3486 usage_branch();
3487 /* NOTREACHED */
3491 if (do_list && delref)
3492 errx(1, "-l and -d options are mutually exclusive\n");
3494 argc -= optind;
3495 argv += optind;
3497 if (!do_list && !delref && argc == 0)
3498 do_show = 1;
3500 if (do_list || delref) {
3501 if (argc > 0)
3502 usage_branch();
3503 } else if (!do_show && (argc < 1 || argc > 2))
3504 usage_branch();
3506 #ifndef PROFILE
3507 if (do_list || do_show) {
3508 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3509 NULL) == -1)
3510 err(1, "pledge");
3511 } else {
3512 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3513 "sendfd unveil", NULL) == -1)
3514 err(1, "pledge");
3516 #endif
3517 cwd = getcwd(NULL, 0);
3518 if (cwd == NULL) {
3519 error = got_error_from_errno("getcwd");
3520 goto done;
3523 if (repo_path == NULL) {
3524 error = got_worktree_open(&worktree, cwd);
3525 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3526 goto done;
3527 else
3528 error = NULL;
3529 if (worktree) {
3530 repo_path =
3531 strdup(got_worktree_get_repo_path(worktree));
3532 if (repo_path == NULL)
3533 error = got_error_from_errno("strdup");
3534 if (error)
3535 goto done;
3536 } else {
3537 repo_path = strdup(cwd);
3538 if (repo_path == NULL) {
3539 error = got_error_from_errno("strdup");
3540 goto done;
3545 error = got_repo_open(&repo, repo_path, NULL);
3546 if (error != NULL)
3547 goto done;
3549 error = apply_unveil(got_repo_get_path(repo), do_list,
3550 worktree ? got_worktree_get_root_path(worktree) : NULL);
3551 if (error)
3552 goto done;
3554 if (do_show)
3555 error = show_current_branch(repo, worktree);
3556 else if (do_list)
3557 error = list_branches(repo, worktree);
3558 else if (delref)
3559 error = delete_branch(repo, worktree, delref);
3560 else {
3561 const char *base_branch;
3562 if (argc == 1) {
3563 base_branch = worktree ?
3564 got_worktree_get_head_ref_name(worktree) :
3565 GOT_REF_HEAD;
3566 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3567 base_branch += 11;
3568 } else
3569 base_branch = argv[1];
3570 error = add_branch(repo, argv[0], base_branch);
3572 done:
3573 if (repo)
3574 got_repo_close(repo);
3575 if (worktree)
3576 got_worktree_close(worktree);
3577 free(cwd);
3578 free(repo_path);
3579 return error;
3583 __dead static void
3584 usage_tag(void)
3586 fprintf(stderr,
3587 "usage: %s tag [-r repository] | -l | "
3588 "[-m message] name [commit]\n", getprogname());
3589 exit(1);
3592 #if 0
3593 static const struct got_error *
3594 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3596 const struct got_error *err = NULL;
3597 struct got_reflist_entry *re, *se, *new;
3598 struct got_object_id *re_id, *se_id;
3599 struct got_tag_object *re_tag, *se_tag;
3600 time_t re_time, se_time;
3602 SIMPLEQ_FOREACH(re, tags, entry) {
3603 se = SIMPLEQ_FIRST(sorted);
3604 if (se == NULL) {
3605 err = got_reflist_entry_dup(&new, re);
3606 if (err)
3607 return err;
3608 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3609 continue;
3610 } else {
3611 err = got_ref_resolve(&re_id, repo, re->ref);
3612 if (err)
3613 break;
3614 err = got_object_open_as_tag(&re_tag, repo, re_id);
3615 free(re_id);
3616 if (err)
3617 break;
3618 re_time = got_object_tag_get_tagger_time(re_tag);
3619 got_object_tag_close(re_tag);
3622 while (se) {
3623 err = got_ref_resolve(&se_id, repo, re->ref);
3624 if (err)
3625 break;
3626 err = got_object_open_as_tag(&se_tag, repo, se_id);
3627 free(se_id);
3628 if (err)
3629 break;
3630 se_time = got_object_tag_get_tagger_time(se_tag);
3631 got_object_tag_close(se_tag);
3633 if (se_time > re_time) {
3634 err = got_reflist_entry_dup(&new, re);
3635 if (err)
3636 return err;
3637 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3638 break;
3640 se = SIMPLEQ_NEXT(se, entry);
3641 continue;
3644 done:
3645 return err;
3647 #endif
3649 static const struct got_error *
3650 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3651 struct got_reference *ref2)
3653 const struct got_error *err = NULL;
3654 struct got_repository *repo = arg;
3655 struct got_object_id *id1, *id2 = NULL;
3656 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3657 time_t time1, time2;
3659 *cmp = 0;
3661 err = got_ref_resolve(&id1, repo, ref1);
3662 if (err)
3663 return err;
3664 err = got_object_open_as_tag(&tag1, repo, id1);
3665 if (err)
3666 goto done;
3668 err = got_ref_resolve(&id2, repo, ref2);
3669 if (err)
3670 goto done;
3671 err = got_object_open_as_tag(&tag2, repo, id2);
3672 if (err)
3673 goto done;
3675 time1 = got_object_tag_get_tagger_time(tag1);
3676 time2 = got_object_tag_get_tagger_time(tag2);
3678 /* Put latest tags first. */
3679 if (time1 < time2)
3680 *cmp = 1;
3681 else if (time1 > time2)
3682 *cmp = -1;
3683 else
3684 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3685 done:
3686 free(id1);
3687 free(id2);
3688 if (tag1)
3689 got_object_tag_close(tag1);
3690 if (tag2)
3691 got_object_tag_close(tag2);
3692 return err;
3695 static const struct got_error *
3696 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3698 static const struct got_error *err = NULL;
3699 struct got_reflist_head refs;
3700 struct got_reflist_entry *re;
3702 SIMPLEQ_INIT(&refs);
3704 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3705 if (err)
3706 return err;
3708 SIMPLEQ_FOREACH(re, &refs, entry) {
3709 const char *refname;
3710 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3711 char datebuf[26];
3712 time_t tagger_time;
3713 struct got_object_id *id;
3714 struct got_tag_object *tag;
3716 refname = got_ref_get_name(re->ref);
3717 if (strncmp(refname, "refs/tags/", 10) != 0)
3718 continue;
3719 refname += 10;
3720 refstr = got_ref_to_str(re->ref);
3721 if (refstr == NULL) {
3722 err = got_error_from_errno("got_ref_to_str");
3723 break;
3725 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3726 free(refstr);
3728 err = got_ref_resolve(&id, repo, re->ref);
3729 if (err)
3730 break;
3731 err = got_object_open_as_tag(&tag, repo, id);
3732 free(id);
3733 if (err)
3734 break;
3735 printf("from: %s\n", got_object_tag_get_tagger(tag));
3736 tagger_time = got_object_tag_get_tagger_time(tag);
3737 datestr = get_datestr(&tagger_time, datebuf);
3738 if (datestr)
3739 printf("date: %s UTC\n", datestr);
3740 err = got_object_id_str(&id_str,
3741 got_object_tag_get_object_id(tag));
3742 if (err)
3743 break;
3744 switch (got_object_tag_get_object_type(tag)) {
3745 case GOT_OBJ_TYPE_BLOB:
3746 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3747 break;
3748 case GOT_OBJ_TYPE_TREE:
3749 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3750 break;
3751 case GOT_OBJ_TYPE_COMMIT:
3752 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3753 break;
3754 case GOT_OBJ_TYPE_TAG:
3755 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3756 break;
3757 default:
3758 break;
3760 free(id_str);
3761 tagmsg0 = strdup(got_object_tag_get_message(tag));
3762 got_object_tag_close(tag);
3763 if (tagmsg0 == NULL) {
3764 err = got_error_from_errno("strdup");
3765 break;
3768 tagmsg = tagmsg0;
3769 do {
3770 line = strsep(&tagmsg, "\n");
3771 if (line)
3772 printf(" %s\n", line);
3773 } while (line);
3774 free(tagmsg0);
3777 got_ref_list_free(&refs);
3778 return NULL;
3781 static const struct got_error *
3782 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3783 const char *tag_name, const char *repo_path)
3785 const struct got_error *err = NULL;
3786 char *template = NULL, *initial_content = NULL;
3787 char *editor = NULL;
3788 int fd = -1;
3790 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3791 err = got_error_from_errno("asprintf");
3792 goto done;
3795 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3796 commit_id_str, tag_name) == -1) {
3797 err = got_error_from_errno("asprintf");
3798 goto done;
3801 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3802 if (err)
3803 goto done;
3805 dprintf(fd, initial_content);
3806 close(fd);
3808 err = get_editor(&editor);
3809 if (err)
3810 goto done;
3811 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3812 done:
3813 free(initial_content);
3814 free(template);
3815 free(editor);
3817 /* Editor is done; we can now apply unveil(2) */
3818 if (err == NULL) {
3819 err = apply_unveil(repo_path, 0, NULL);
3820 if (err) {
3821 free(*tagmsg);
3822 *tagmsg = NULL;
3825 return err;
3828 static const struct got_error *
3829 add_tag(struct got_repository *repo, const char *tag_name,
3830 const char *commit_arg, const char *tagmsg_arg)
3832 const struct got_error *err = NULL;
3833 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3834 char *label = NULL, *commit_id_str = NULL;
3835 struct got_reference *ref = NULL;
3836 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3837 char *tagmsg_path = NULL, *tag_id_str = NULL;
3838 int preserve_tagmsg = 0;
3841 * Don't let the user create a tag named '-'.
3842 * While technically a valid reference name, this case is usually
3843 * an unintended typo.
3845 if (tag_name[0] == '-' && tag_name[1] == '\0')
3846 return got_error_path(tag_name, GOT_ERR_BAD_REF_NAME);
3848 err = get_author(&tagger, repo);
3849 if (err)
3850 return err;
3852 err = match_object_id(&commit_id, &label, commit_arg,
3853 GOT_OBJ_TYPE_COMMIT, 1, repo);
3854 if (err)
3855 goto done;
3857 err = got_object_id_str(&commit_id_str, commit_id);
3858 if (err)
3859 goto done;
3861 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3862 refname = strdup(tag_name);
3863 if (refname == NULL) {
3864 err = got_error_from_errno("strdup");
3865 goto done;
3867 tag_name += 10;
3868 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3869 err = got_error_from_errno("asprintf");
3870 goto done;
3873 err = got_ref_open(&ref, repo, refname, 0);
3874 if (err == NULL) {
3875 err = got_error(GOT_ERR_TAG_EXISTS);
3876 goto done;
3877 } else if (err->code != GOT_ERR_NOT_REF)
3878 goto done;
3880 if (tagmsg_arg == NULL) {
3881 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3882 tag_name, got_repo_get_path(repo));
3883 if (err) {
3884 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3885 tagmsg_path != NULL)
3886 preserve_tagmsg = 1;
3887 goto done;
3891 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3892 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3893 if (err) {
3894 if (tagmsg_path)
3895 preserve_tagmsg = 1;
3896 goto done;
3899 err = got_ref_alloc(&ref, refname, tag_id);
3900 if (err) {
3901 if (tagmsg_path)
3902 preserve_tagmsg = 1;
3903 goto done;
3906 err = got_ref_write(ref, repo);
3907 if (err) {
3908 if (tagmsg_path)
3909 preserve_tagmsg = 1;
3910 goto done;
3913 err = got_object_id_str(&tag_id_str, tag_id);
3914 if (err) {
3915 if (tagmsg_path)
3916 preserve_tagmsg = 1;
3917 goto done;
3919 printf("Created tag %s\n", tag_id_str);
3920 done:
3921 if (preserve_tagmsg) {
3922 fprintf(stderr, "%s: tag message preserved in %s\n",
3923 getprogname(), tagmsg_path);
3924 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3925 err = got_error_from_errno2("unlink", tagmsg_path);
3926 free(tag_id_str);
3927 if (ref)
3928 got_ref_close(ref);
3929 free(commit_id);
3930 free(commit_id_str);
3931 free(refname);
3932 free(tagmsg);
3933 free(tagmsg_path);
3934 free(tagger);
3935 return err;
3938 static const struct got_error *
3939 cmd_tag(int argc, char *argv[])
3941 const struct got_error *error = NULL;
3942 struct got_repository *repo = NULL;
3943 struct got_worktree *worktree = NULL;
3944 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3945 char *gitconfig_path = NULL;
3946 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3947 int ch, do_list = 0;
3949 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3950 switch (ch) {
3951 case 'm':
3952 tagmsg = optarg;
3953 break;
3954 case 'r':
3955 repo_path = realpath(optarg, NULL);
3956 if (repo_path == NULL)
3957 return got_error_from_errno2("realpath",
3958 optarg);
3959 got_path_strip_trailing_slashes(repo_path);
3960 break;
3961 case 'l':
3962 do_list = 1;
3963 break;
3964 default:
3965 usage_tag();
3966 /* NOTREACHED */
3970 argc -= optind;
3971 argv += optind;
3973 if (do_list) {
3974 if (tagmsg)
3975 errx(1, "-l and -m options are mutually exclusive\n");
3976 if (argc > 0)
3977 usage_tag();
3978 } else if (argc < 1 || argc > 2)
3979 usage_tag();
3980 else if (argc > 1)
3981 commit_id_arg = argv[1];
3982 tag_name = argv[0];
3984 #ifndef PROFILE
3985 if (do_list) {
3986 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3987 NULL) == -1)
3988 err(1, "pledge");
3989 } else {
3990 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3991 "sendfd unveil", NULL) == -1)
3992 err(1, "pledge");
3994 #endif
3995 cwd = getcwd(NULL, 0);
3996 if (cwd == NULL) {
3997 error = got_error_from_errno("getcwd");
3998 goto done;
4001 if (repo_path == NULL) {
4002 error = got_worktree_open(&worktree, cwd);
4003 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4004 goto done;
4005 else
4006 error = NULL;
4007 if (worktree) {
4008 repo_path =
4009 strdup(got_worktree_get_repo_path(worktree));
4010 if (repo_path == NULL)
4011 error = got_error_from_errno("strdup");
4012 if (error)
4013 goto done;
4014 } else {
4015 repo_path = strdup(cwd);
4016 if (repo_path == NULL) {
4017 error = got_error_from_errno("strdup");
4018 goto done;
4023 if (do_list) {
4024 error = got_repo_open(&repo, repo_path, NULL);
4025 if (error != NULL)
4026 goto done;
4027 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4028 if (error)
4029 goto done;
4030 error = list_tags(repo, worktree);
4031 } else {
4032 error = get_gitconfig_path(&gitconfig_path);
4033 if (error)
4034 goto done;
4035 error = got_repo_open(&repo, repo_path, gitconfig_path);
4036 if (error != NULL)
4037 goto done;
4039 if (tagmsg) {
4040 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4041 if (error)
4042 goto done;
4045 if (commit_id_arg == NULL) {
4046 struct got_reference *head_ref;
4047 struct got_object_id *commit_id;
4048 error = got_ref_open(&head_ref, repo,
4049 worktree ? got_worktree_get_head_ref_name(worktree)
4050 : GOT_REF_HEAD, 0);
4051 if (error)
4052 goto done;
4053 error = got_ref_resolve(&commit_id, repo, head_ref);
4054 got_ref_close(head_ref);
4055 if (error)
4056 goto done;
4057 error = got_object_id_str(&commit_id_str, commit_id);
4058 free(commit_id);
4059 if (error)
4060 goto done;
4063 error = add_tag(repo, tag_name,
4064 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4066 done:
4067 if (repo)
4068 got_repo_close(repo);
4069 if (worktree)
4070 got_worktree_close(worktree);
4071 free(cwd);
4072 free(repo_path);
4073 free(gitconfig_path);
4074 free(commit_id_str);
4075 return error;
4078 __dead static void
4079 usage_add(void)
4081 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4082 exit(1);
4085 static const struct got_error *
4086 cmd_add(int argc, char *argv[])
4088 const struct got_error *error = NULL;
4089 struct got_repository *repo = NULL;
4090 struct got_worktree *worktree = NULL;
4091 char *cwd = NULL;
4092 struct got_pathlist_head paths;
4093 struct got_pathlist_entry *pe;
4094 int ch;
4096 TAILQ_INIT(&paths);
4098 while ((ch = getopt(argc, argv, "")) != -1) {
4099 switch (ch) {
4100 default:
4101 usage_add();
4102 /* NOTREACHED */
4106 argc -= optind;
4107 argv += optind;
4109 #ifndef PROFILE
4110 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4111 NULL) == -1)
4112 err(1, "pledge");
4113 #endif
4114 if (argc < 1)
4115 usage_add();
4117 cwd = getcwd(NULL, 0);
4118 if (cwd == NULL) {
4119 error = got_error_from_errno("getcwd");
4120 goto done;
4123 error = got_worktree_open(&worktree, cwd);
4124 if (error)
4125 goto done;
4127 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4128 NULL);
4129 if (error != NULL)
4130 goto done;
4132 error = apply_unveil(got_repo_get_path(repo), 1,
4133 got_worktree_get_root_path(worktree));
4134 if (error)
4135 goto done;
4137 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4138 if (error)
4139 goto done;
4141 error = got_worktree_schedule_add(worktree, &paths, print_status,
4142 NULL, repo);
4143 done:
4144 if (repo)
4145 got_repo_close(repo);
4146 if (worktree)
4147 got_worktree_close(worktree);
4148 TAILQ_FOREACH(pe, &paths, entry)
4149 free((char *)pe->path);
4150 got_pathlist_free(&paths);
4151 free(cwd);
4152 return error;
4155 __dead static void
4156 usage_remove(void)
4158 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4159 exit(1);
4162 static const struct got_error *
4163 print_remove_status(void *arg, unsigned char status,
4164 unsigned char staged_status, const char *path,
4165 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4166 struct got_object_id *commit_id)
4168 if (status == GOT_STATUS_NONEXISTENT)
4169 return NULL;
4170 if (status == staged_status && (status == GOT_STATUS_DELETE))
4171 status = GOT_STATUS_NO_CHANGE;
4172 printf("%c%c %s\n", status, staged_status, path);
4173 return NULL;
4176 static const struct got_error *
4177 cmd_remove(int argc, char *argv[])
4179 const struct got_error *error = NULL;
4180 struct got_worktree *worktree = NULL;
4181 struct got_repository *repo = NULL;
4182 char *cwd = NULL;
4183 struct got_pathlist_head paths;
4184 struct got_pathlist_entry *pe;
4185 int ch, delete_local_mods = 0;
4187 TAILQ_INIT(&paths);
4189 while ((ch = getopt(argc, argv, "f")) != -1) {
4190 switch (ch) {
4191 case 'f':
4192 delete_local_mods = 1;
4193 break;
4194 default:
4195 usage_add();
4196 /* NOTREACHED */
4200 argc -= optind;
4201 argv += optind;
4203 #ifndef PROFILE
4204 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4205 NULL) == -1)
4206 err(1, "pledge");
4207 #endif
4208 if (argc < 1)
4209 usage_remove();
4211 cwd = getcwd(NULL, 0);
4212 if (cwd == NULL) {
4213 error = got_error_from_errno("getcwd");
4214 goto done;
4216 error = got_worktree_open(&worktree, cwd);
4217 if (error)
4218 goto done;
4220 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4221 NULL);
4222 if (error)
4223 goto done;
4225 error = apply_unveil(got_repo_get_path(repo), 1,
4226 got_worktree_get_root_path(worktree));
4227 if (error)
4228 goto done;
4230 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4231 if (error)
4232 goto done;
4234 error = got_worktree_schedule_delete(worktree, &paths,
4235 delete_local_mods, print_remove_status, NULL, repo);
4236 if (error)
4237 goto done;
4238 done:
4239 if (repo)
4240 got_repo_close(repo);
4241 if (worktree)
4242 got_worktree_close(worktree);
4243 TAILQ_FOREACH(pe, &paths, entry)
4244 free((char *)pe->path);
4245 got_pathlist_free(&paths);
4246 free(cwd);
4247 return error;
4250 __dead static void
4251 usage_revert(void)
4253 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4254 "path ...\n", getprogname());
4255 exit(1);
4258 static const struct got_error *
4259 revert_progress(void *arg, unsigned char status, const char *path)
4261 while (path[0] == '/')
4262 path++;
4263 printf("%c %s\n", status, path);
4264 return NULL;
4267 struct choose_patch_arg {
4268 FILE *patch_script_file;
4269 const char *action;
4272 static const struct got_error *
4273 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4274 int nchanges, const char *action)
4276 char *line = NULL;
4277 size_t linesize = 0;
4278 ssize_t linelen;
4280 switch (status) {
4281 case GOT_STATUS_ADD:
4282 printf("A %s\n%s this addition? [y/n] ", path, action);
4283 break;
4284 case GOT_STATUS_DELETE:
4285 printf("D %s\n%s this deletion? [y/n] ", path, action);
4286 break;
4287 case GOT_STATUS_MODIFY:
4288 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4289 return got_error_from_errno("fseek");
4290 printf(GOT_COMMIT_SEP_STR);
4291 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4292 printf("%s", line);
4293 if (ferror(patch_file))
4294 return got_error_from_errno("getline");
4295 printf(GOT_COMMIT_SEP_STR);
4296 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4297 path, n, nchanges, action);
4298 break;
4299 default:
4300 return got_error_path(path, GOT_ERR_FILE_STATUS);
4303 return NULL;
4306 static const struct got_error *
4307 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4308 FILE *patch_file, int n, int nchanges)
4310 const struct got_error *err = NULL;
4311 char *line = NULL;
4312 size_t linesize = 0;
4313 ssize_t linelen;
4314 int resp = ' ';
4315 struct choose_patch_arg *a = arg;
4317 *choice = GOT_PATCH_CHOICE_NONE;
4319 if (a->patch_script_file) {
4320 char *nl;
4321 err = show_change(status, path, patch_file, n, nchanges,
4322 a->action);
4323 if (err)
4324 return err;
4325 linelen = getline(&line, &linesize, a->patch_script_file);
4326 if (linelen == -1) {
4327 if (ferror(a->patch_script_file))
4328 return got_error_from_errno("getline");
4329 return NULL;
4331 nl = strchr(line, '\n');
4332 if (nl)
4333 *nl = '\0';
4334 if (strcmp(line, "y") == 0) {
4335 *choice = GOT_PATCH_CHOICE_YES;
4336 printf("y\n");
4337 } else if (strcmp(line, "n") == 0) {
4338 *choice = GOT_PATCH_CHOICE_NO;
4339 printf("n\n");
4340 } else if (strcmp(line, "q") == 0 &&
4341 status == GOT_STATUS_MODIFY) {
4342 *choice = GOT_PATCH_CHOICE_QUIT;
4343 printf("q\n");
4344 } else
4345 printf("invalid response '%s'\n", line);
4346 free(line);
4347 return NULL;
4350 while (resp != 'y' && resp != 'n' && resp != 'q') {
4351 err = show_change(status, path, patch_file, n, nchanges,
4352 a->action);
4353 if (err)
4354 return err;
4355 resp = getchar();
4356 if (resp == '\n')
4357 resp = getchar();
4358 if (status == GOT_STATUS_MODIFY) {
4359 if (resp != 'y' && resp != 'n' && resp != 'q') {
4360 printf("invalid response '%c'\n", resp);
4361 resp = ' ';
4363 } else if (resp != 'y' && resp != 'n') {
4364 printf("invalid response '%c'\n", resp);
4365 resp = ' ';
4369 if (resp == 'y')
4370 *choice = GOT_PATCH_CHOICE_YES;
4371 else if (resp == 'n')
4372 *choice = GOT_PATCH_CHOICE_NO;
4373 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4374 *choice = GOT_PATCH_CHOICE_QUIT;
4376 return NULL;
4380 static const struct got_error *
4381 cmd_revert(int argc, char *argv[])
4383 const struct got_error *error = NULL;
4384 struct got_worktree *worktree = NULL;
4385 struct got_repository *repo = NULL;
4386 char *cwd = NULL, *path = NULL;
4387 struct got_pathlist_head paths;
4388 struct got_pathlist_entry *pe;
4389 int ch, can_recurse = 0, pflag = 0;
4390 FILE *patch_script_file = NULL;
4391 const char *patch_script_path = NULL;
4392 struct choose_patch_arg cpa;
4394 TAILQ_INIT(&paths);
4396 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4397 switch (ch) {
4398 case 'p':
4399 pflag = 1;
4400 break;
4401 case 'F':
4402 patch_script_path = optarg;
4403 break;
4404 case 'R':
4405 can_recurse = 1;
4406 break;
4407 default:
4408 usage_revert();
4409 /* NOTREACHED */
4413 argc -= optind;
4414 argv += optind;
4416 #ifndef PROFILE
4417 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4418 "unveil", NULL) == -1)
4419 err(1, "pledge");
4420 #endif
4421 if (argc < 1)
4422 usage_revert();
4423 if (patch_script_path && !pflag)
4424 errx(1, "-F option can only be used together with -p option");
4426 cwd = getcwd(NULL, 0);
4427 if (cwd == NULL) {
4428 error = got_error_from_errno("getcwd");
4429 goto done;
4431 error = got_worktree_open(&worktree, cwd);
4432 if (error)
4433 goto done;
4435 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4436 NULL);
4437 if (error != NULL)
4438 goto done;
4440 if (patch_script_path) {
4441 patch_script_file = fopen(patch_script_path, "r");
4442 if (patch_script_file == NULL) {
4443 error = got_error_from_errno2("fopen",
4444 patch_script_path);
4445 goto done;
4448 error = apply_unveil(got_repo_get_path(repo), 1,
4449 got_worktree_get_root_path(worktree));
4450 if (error)
4451 goto done;
4453 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4454 if (error)
4455 goto done;
4457 if (!can_recurse) {
4458 char *ondisk_path;
4459 struct stat sb;
4460 TAILQ_FOREACH(pe, &paths, entry) {
4461 if (asprintf(&ondisk_path, "%s/%s",
4462 got_worktree_get_root_path(worktree),
4463 pe->path) == -1) {
4464 error = got_error_from_errno("asprintf");
4465 goto done;
4467 if (lstat(ondisk_path, &sb) == -1) {
4468 if (errno == ENOENT) {
4469 free(ondisk_path);
4470 continue;
4472 error = got_error_from_errno2("lstat",
4473 ondisk_path);
4474 free(ondisk_path);
4475 goto done;
4477 free(ondisk_path);
4478 if (S_ISDIR(sb.st_mode)) {
4479 error = got_error_msg(GOT_ERR_BAD_PATH,
4480 "reverting directories requires -R option");
4481 goto done;
4486 cpa.patch_script_file = patch_script_file;
4487 cpa.action = "revert";
4488 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4489 pflag ? choose_patch : NULL, &cpa, repo);
4490 if (error)
4491 goto done;
4492 done:
4493 if (patch_script_file && fclose(patch_script_file) == EOF &&
4494 error == NULL)
4495 error = got_error_from_errno2("fclose", patch_script_path);
4496 if (repo)
4497 got_repo_close(repo);
4498 if (worktree)
4499 got_worktree_close(worktree);
4500 free(path);
4501 free(cwd);
4502 return error;
4505 __dead static void
4506 usage_commit(void)
4508 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4509 getprogname());
4510 exit(1);
4513 struct collect_commit_logmsg_arg {
4514 const char *cmdline_log;
4515 const char *editor;
4516 const char *worktree_path;
4517 const char *branch_name;
4518 const char *repo_path;
4519 char *logmsg_path;
4523 static const struct got_error *
4524 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4525 void *arg)
4527 char *initial_content = NULL;
4528 struct got_pathlist_entry *pe;
4529 const struct got_error *err = NULL;
4530 char *template = NULL;
4531 struct collect_commit_logmsg_arg *a = arg;
4532 int fd;
4533 size_t len;
4535 /* if a message was specified on the command line, just use it */
4536 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4537 len = strlen(a->cmdline_log) + 1;
4538 *logmsg = malloc(len + 1);
4539 if (*logmsg == NULL)
4540 return got_error_from_errno("malloc");
4541 strlcpy(*logmsg, a->cmdline_log, len);
4542 return NULL;
4545 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4546 return got_error_from_errno("asprintf");
4548 if (asprintf(&initial_content,
4549 "\n# changes to be committed on branch %s:\n",
4550 a->branch_name) == -1)
4551 return got_error_from_errno("asprintf");
4553 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4554 if (err)
4555 goto done;
4557 dprintf(fd, initial_content);
4559 TAILQ_FOREACH(pe, commitable_paths, entry) {
4560 struct got_commitable *ct = pe->data;
4561 dprintf(fd, "# %c %s\n",
4562 got_commitable_get_status(ct),
4563 got_commitable_get_path(ct));
4565 close(fd);
4567 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4568 done:
4569 free(initial_content);
4570 free(template);
4572 /* Editor is done; we can now apply unveil(2) */
4573 if (err == NULL) {
4574 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4575 if (err) {
4576 free(*logmsg);
4577 *logmsg = NULL;
4580 return err;
4583 static const struct got_error *
4584 cmd_commit(int argc, char *argv[])
4586 const struct got_error *error = NULL;
4587 struct got_worktree *worktree = NULL;
4588 struct got_repository *repo = NULL;
4589 char *cwd = NULL, *id_str = NULL;
4590 struct got_object_id *id = NULL;
4591 const char *logmsg = NULL;
4592 struct collect_commit_logmsg_arg cl_arg;
4593 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4594 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4595 struct got_pathlist_head paths;
4597 TAILQ_INIT(&paths);
4598 cl_arg.logmsg_path = NULL;
4600 while ((ch = getopt(argc, argv, "m:")) != -1) {
4601 switch (ch) {
4602 case 'm':
4603 logmsg = optarg;
4604 break;
4605 default:
4606 usage_commit();
4607 /* NOTREACHED */
4611 argc -= optind;
4612 argv += optind;
4614 #ifndef PROFILE
4615 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4616 "unveil", NULL) == -1)
4617 err(1, "pledge");
4618 #endif
4619 cwd = getcwd(NULL, 0);
4620 if (cwd == NULL) {
4621 error = got_error_from_errno("getcwd");
4622 goto done;
4624 error = got_worktree_open(&worktree, cwd);
4625 if (error)
4626 goto done;
4628 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4629 if (error)
4630 goto done;
4631 if (rebase_in_progress) {
4632 error = got_error(GOT_ERR_REBASING);
4633 goto done;
4636 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4637 worktree);
4638 if (error)
4639 goto done;
4641 error = get_gitconfig_path(&gitconfig_path);
4642 if (error)
4643 goto done;
4644 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4645 gitconfig_path);
4646 if (error != NULL)
4647 goto done;
4649 error = get_author(&author, repo);
4650 if (error)
4651 return error;
4654 * unveil(2) traverses exec(2); if an editor is used we have
4655 * to apply unveil after the log message has been written.
4657 if (logmsg == NULL || strlen(logmsg) == 0)
4658 error = get_editor(&editor);
4659 else
4660 error = apply_unveil(got_repo_get_path(repo), 0,
4661 got_worktree_get_root_path(worktree));
4662 if (error)
4663 goto done;
4665 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4666 if (error)
4667 goto done;
4669 cl_arg.editor = editor;
4670 cl_arg.cmdline_log = logmsg;
4671 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4672 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4673 if (!histedit_in_progress) {
4674 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4675 error = got_error(GOT_ERR_COMMIT_BRANCH);
4676 goto done;
4678 cl_arg.branch_name += 11;
4680 cl_arg.repo_path = got_repo_get_path(repo);
4681 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4682 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4683 if (error) {
4684 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4685 cl_arg.logmsg_path != NULL)
4686 preserve_logmsg = 1;
4687 goto done;
4690 error = got_object_id_str(&id_str, id);
4691 if (error)
4692 goto done;
4693 printf("Created commit %s\n", id_str);
4694 done:
4695 if (preserve_logmsg) {
4696 fprintf(stderr, "%s: log message preserved in %s\n",
4697 getprogname(), cl_arg.logmsg_path);
4698 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4699 error == NULL)
4700 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4701 free(cl_arg.logmsg_path);
4702 if (repo)
4703 got_repo_close(repo);
4704 if (worktree)
4705 got_worktree_close(worktree);
4706 free(cwd);
4707 free(id_str);
4708 free(gitconfig_path);
4709 free(editor);
4710 free(author);
4711 return error;
4714 __dead static void
4715 usage_cherrypick(void)
4717 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4718 exit(1);
4721 static const struct got_error *
4722 cmd_cherrypick(int argc, char *argv[])
4724 const struct got_error *error = NULL;
4725 struct got_worktree *worktree = NULL;
4726 struct got_repository *repo = NULL;
4727 char *cwd = NULL, *commit_id_str = NULL;
4728 struct got_object_id *commit_id = NULL;
4729 struct got_commit_object *commit = NULL;
4730 struct got_object_qid *pid;
4731 struct got_reference *head_ref = NULL;
4732 int ch, did_something = 0;
4734 while ((ch = getopt(argc, argv, "")) != -1) {
4735 switch (ch) {
4736 default:
4737 usage_cherrypick();
4738 /* NOTREACHED */
4742 argc -= optind;
4743 argv += optind;
4745 #ifndef PROFILE
4746 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4747 "unveil", NULL) == -1)
4748 err(1, "pledge");
4749 #endif
4750 if (argc != 1)
4751 usage_cherrypick();
4753 cwd = getcwd(NULL, 0);
4754 if (cwd == NULL) {
4755 error = got_error_from_errno("getcwd");
4756 goto done;
4758 error = got_worktree_open(&worktree, cwd);
4759 if (error)
4760 goto done;
4762 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4763 NULL);
4764 if (error != NULL)
4765 goto done;
4767 error = apply_unveil(got_repo_get_path(repo), 0,
4768 got_worktree_get_root_path(worktree));
4769 if (error)
4770 goto done;
4772 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4773 GOT_OBJ_TYPE_COMMIT, repo);
4774 if (error != NULL) {
4775 struct got_reference *ref;
4776 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4777 goto done;
4778 error = got_ref_open(&ref, repo, argv[0], 0);
4779 if (error != NULL)
4780 goto done;
4781 error = got_ref_resolve(&commit_id, repo, ref);
4782 got_ref_close(ref);
4783 if (error != NULL)
4784 goto done;
4786 error = got_object_id_str(&commit_id_str, commit_id);
4787 if (error)
4788 goto done;
4790 error = got_ref_open(&head_ref, repo,
4791 got_worktree_get_head_ref_name(worktree), 0);
4792 if (error != NULL)
4793 goto done;
4795 error = check_same_branch(commit_id, head_ref, NULL, repo);
4796 if (error) {
4797 if (error->code != GOT_ERR_ANCESTRY)
4798 goto done;
4799 error = NULL;
4800 } else {
4801 error = got_error(GOT_ERR_SAME_BRANCH);
4802 goto done;
4805 error = got_object_open_as_commit(&commit, repo, commit_id);
4806 if (error)
4807 goto done;
4808 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4809 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4810 commit_id, repo, update_progress, &did_something, check_cancelled,
4811 NULL);
4812 if (error != NULL)
4813 goto done;
4815 if (did_something)
4816 printf("Merged commit %s\n", commit_id_str);
4817 done:
4818 if (commit)
4819 got_object_commit_close(commit);
4820 free(commit_id_str);
4821 if (head_ref)
4822 got_ref_close(head_ref);
4823 if (worktree)
4824 got_worktree_close(worktree);
4825 if (repo)
4826 got_repo_close(repo);
4827 return error;
4830 __dead static void
4831 usage_backout(void)
4833 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4834 exit(1);
4837 static const struct got_error *
4838 cmd_backout(int argc, char *argv[])
4840 const struct got_error *error = NULL;
4841 struct got_worktree *worktree = NULL;
4842 struct got_repository *repo = NULL;
4843 char *cwd = NULL, *commit_id_str = NULL;
4844 struct got_object_id *commit_id = NULL;
4845 struct got_commit_object *commit = NULL;
4846 struct got_object_qid *pid;
4847 struct got_reference *head_ref = NULL;
4848 int ch, did_something = 0;
4850 while ((ch = getopt(argc, argv, "")) != -1) {
4851 switch (ch) {
4852 default:
4853 usage_backout();
4854 /* NOTREACHED */
4858 argc -= optind;
4859 argv += optind;
4861 #ifndef PROFILE
4862 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4863 "unveil", NULL) == -1)
4864 err(1, "pledge");
4865 #endif
4866 if (argc != 1)
4867 usage_backout();
4869 cwd = getcwd(NULL, 0);
4870 if (cwd == NULL) {
4871 error = got_error_from_errno("getcwd");
4872 goto done;
4874 error = got_worktree_open(&worktree, cwd);
4875 if (error)
4876 goto done;
4878 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4879 NULL);
4880 if (error != NULL)
4881 goto done;
4883 error = apply_unveil(got_repo_get_path(repo), 0,
4884 got_worktree_get_root_path(worktree));
4885 if (error)
4886 goto done;
4888 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4889 GOT_OBJ_TYPE_COMMIT, repo);
4890 if (error != NULL) {
4891 struct got_reference *ref;
4892 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4893 goto done;
4894 error = got_ref_open(&ref, repo, argv[0], 0);
4895 if (error != NULL)
4896 goto done;
4897 error = got_ref_resolve(&commit_id, repo, ref);
4898 got_ref_close(ref);
4899 if (error != NULL)
4900 goto done;
4902 error = got_object_id_str(&commit_id_str, commit_id);
4903 if (error)
4904 goto done;
4906 error = got_ref_open(&head_ref, repo,
4907 got_worktree_get_head_ref_name(worktree), 0);
4908 if (error != NULL)
4909 goto done;
4911 error = check_same_branch(commit_id, head_ref, NULL, repo);
4912 if (error)
4913 goto done;
4915 error = got_object_open_as_commit(&commit, repo, commit_id);
4916 if (error)
4917 goto done;
4918 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4919 if (pid == NULL) {
4920 error = got_error(GOT_ERR_ROOT_COMMIT);
4921 goto done;
4924 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4925 update_progress, &did_something, check_cancelled, NULL);
4926 if (error != NULL)
4927 goto done;
4929 if (did_something)
4930 printf("Backed out commit %s\n", commit_id_str);
4931 done:
4932 if (commit)
4933 got_object_commit_close(commit);
4934 free(commit_id_str);
4935 if (head_ref)
4936 got_ref_close(head_ref);
4937 if (worktree)
4938 got_worktree_close(worktree);
4939 if (repo)
4940 got_repo_close(repo);
4941 return error;
4944 __dead static void
4945 usage_rebase(void)
4947 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4948 getprogname());
4949 exit(1);
4952 void
4953 trim_logmsg(char *logmsg, int limit)
4955 char *nl;
4956 size_t len;
4958 len = strlen(logmsg);
4959 if (len > limit)
4960 len = limit;
4961 logmsg[len] = '\0';
4962 nl = strchr(logmsg, '\n');
4963 if (nl)
4964 *nl = '\0';
4967 static const struct got_error *
4968 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4970 const struct got_error *err;
4971 char *logmsg0 = NULL;
4972 const char *s;
4974 err = got_object_commit_get_logmsg(&logmsg0, commit);
4975 if (err)
4976 return err;
4978 s = logmsg0;
4979 while (isspace((unsigned char)s[0]))
4980 s++;
4982 *logmsg = strdup(s);
4983 if (*logmsg == NULL) {
4984 err = got_error_from_errno("strdup");
4985 goto done;
4988 trim_logmsg(*logmsg, limit);
4989 done:
4990 free(logmsg0);
4991 return err;
4994 static const struct got_error *
4995 show_rebase_progress(struct got_commit_object *commit,
4996 struct got_object_id *old_id, struct got_object_id *new_id)
4998 const struct got_error *err;
4999 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5001 err = got_object_id_str(&old_id_str, old_id);
5002 if (err)
5003 goto done;
5005 if (new_id) {
5006 err = got_object_id_str(&new_id_str, new_id);
5007 if (err)
5008 goto done;
5011 old_id_str[12] = '\0';
5012 if (new_id_str)
5013 new_id_str[12] = '\0';
5015 err = get_short_logmsg(&logmsg, 42, commit);
5016 if (err)
5017 goto done;
5019 printf("%s -> %s: %s\n", old_id_str,
5020 new_id_str ? new_id_str : "no-op change", logmsg);
5021 done:
5022 free(old_id_str);
5023 free(new_id_str);
5024 return err;
5027 static const struct got_error *
5028 rebase_progress(void *arg, unsigned char status, const char *path)
5030 unsigned char *rebase_status = arg;
5032 while (path[0] == '/')
5033 path++;
5034 printf("%c %s\n", status, path);
5036 if (*rebase_status == GOT_STATUS_CONFLICT)
5037 return NULL;
5038 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5039 *rebase_status = status;
5040 return NULL;
5043 static const struct got_error *
5044 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5045 struct got_reference *branch, struct got_reference *new_base_branch,
5046 struct got_reference *tmp_branch, struct got_repository *repo)
5048 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5049 return got_worktree_rebase_complete(worktree, fileindex,
5050 new_base_branch, tmp_branch, branch, repo);
5053 static const struct got_error *
5054 rebase_commit(struct got_pathlist_head *merged_paths,
5055 struct got_worktree *worktree, struct got_fileindex *fileindex,
5056 struct got_reference *tmp_branch,
5057 struct got_object_id *commit_id, struct got_repository *repo)
5059 const struct got_error *error;
5060 struct got_commit_object *commit;
5061 struct got_object_id *new_commit_id;
5063 error = got_object_open_as_commit(&commit, repo, commit_id);
5064 if (error)
5065 return error;
5067 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5068 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5069 if (error) {
5070 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5071 goto done;
5072 error = show_rebase_progress(commit, commit_id, NULL);
5073 } else {
5074 error = show_rebase_progress(commit, commit_id, new_commit_id);
5075 free(new_commit_id);
5077 done:
5078 got_object_commit_close(commit);
5079 return error;
5082 struct check_path_prefix_arg {
5083 const char *path_prefix;
5084 size_t len;
5085 int errcode;
5088 static const struct got_error *
5089 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5090 struct got_blob_object *blob2, struct got_object_id *id1,
5091 struct got_object_id *id2, const char *path1, const char *path2,
5092 mode_t mode1, mode_t mode2, struct got_repository *repo)
5094 struct check_path_prefix_arg *a = arg;
5096 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5097 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5098 return got_error(a->errcode);
5100 return NULL;
5103 static const struct got_error *
5104 check_path_prefix(struct got_object_id *parent_id,
5105 struct got_object_id *commit_id, const char *path_prefix,
5106 int errcode, struct got_repository *repo)
5108 const struct got_error *err;
5109 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5110 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5111 struct check_path_prefix_arg cpp_arg;
5113 if (got_path_is_root_dir(path_prefix))
5114 return NULL;
5116 err = got_object_open_as_commit(&commit, repo, commit_id);
5117 if (err)
5118 goto done;
5120 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5121 if (err)
5122 goto done;
5124 err = got_object_open_as_tree(&tree1, repo,
5125 got_object_commit_get_tree_id(parent_commit));
5126 if (err)
5127 goto done;
5129 err = got_object_open_as_tree(&tree2, repo,
5130 got_object_commit_get_tree_id(commit));
5131 if (err)
5132 goto done;
5134 cpp_arg.path_prefix = path_prefix;
5135 while (cpp_arg.path_prefix[0] == '/')
5136 cpp_arg.path_prefix++;
5137 cpp_arg.len = strlen(cpp_arg.path_prefix);
5138 cpp_arg.errcode = errcode;
5139 err = got_diff_tree(tree1, tree2, "", "", repo,
5140 check_path_prefix_in_diff, &cpp_arg, 0);
5141 done:
5142 if (tree1)
5143 got_object_tree_close(tree1);
5144 if (tree2)
5145 got_object_tree_close(tree2);
5146 if (commit)
5147 got_object_commit_close(commit);
5148 if (parent_commit)
5149 got_object_commit_close(parent_commit);
5150 return err;
5153 static const struct got_error *
5154 collect_commits(struct got_object_id_queue *commits,
5155 struct got_object_id *initial_commit_id,
5156 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5157 const char *path_prefix, int path_prefix_errcode,
5158 struct got_repository *repo)
5160 const struct got_error *err = NULL;
5161 struct got_commit_graph *graph = NULL;
5162 struct got_object_id *parent_id = NULL;
5163 struct got_object_qid *qid;
5164 struct got_object_id *commit_id = initial_commit_id;
5166 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5167 if (err)
5168 return err;
5170 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5171 check_cancelled, NULL);
5172 if (err)
5173 goto done;
5174 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5175 err = got_commit_graph_iter_next(&parent_id, graph);
5176 if (err) {
5177 if (err->code == GOT_ERR_ITER_COMPLETED) {
5178 err = got_error_msg(GOT_ERR_ANCESTRY,
5179 "ran out of commits to rebase before "
5180 "youngest common ancestor commit has "
5181 "been reached?!?");
5182 goto done;
5183 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5184 goto done;
5185 err = got_commit_graph_fetch_commits(graph, 1, repo,
5186 check_cancelled, NULL);
5187 if (err)
5188 goto done;
5189 } else {
5190 err = check_path_prefix(parent_id, commit_id,
5191 path_prefix, path_prefix_errcode, repo);
5192 if (err)
5193 goto done;
5195 err = got_object_qid_alloc(&qid, commit_id);
5196 if (err)
5197 goto done;
5198 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5199 commit_id = parent_id;
5202 done:
5203 got_commit_graph_close(graph);
5204 return err;
5207 static const struct got_error *
5208 cmd_rebase(int argc, char *argv[])
5210 const struct got_error *error = NULL;
5211 struct got_worktree *worktree = NULL;
5212 struct got_repository *repo = NULL;
5213 struct got_fileindex *fileindex = NULL;
5214 char *cwd = NULL;
5215 struct got_reference *branch = NULL;
5216 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5217 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5218 struct got_object_id *resume_commit_id = NULL;
5219 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5220 struct got_commit_object *commit = NULL;
5221 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5222 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5223 struct got_object_id_queue commits;
5224 struct got_pathlist_head merged_paths;
5225 const struct got_object_id_queue *parent_ids;
5226 struct got_object_qid *qid, *pid;
5228 SIMPLEQ_INIT(&commits);
5229 TAILQ_INIT(&merged_paths);
5231 while ((ch = getopt(argc, argv, "ac")) != -1) {
5232 switch (ch) {
5233 case 'a':
5234 abort_rebase = 1;
5235 break;
5236 case 'c':
5237 continue_rebase = 1;
5238 break;
5239 default:
5240 usage_rebase();
5241 /* NOTREACHED */
5245 argc -= optind;
5246 argv += optind;
5248 #ifndef PROFILE
5249 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5250 "unveil", NULL) == -1)
5251 err(1, "pledge");
5252 #endif
5253 if (abort_rebase && continue_rebase)
5254 usage_rebase();
5255 else if (abort_rebase || continue_rebase) {
5256 if (argc != 0)
5257 usage_rebase();
5258 } else if (argc != 1)
5259 usage_rebase();
5261 cwd = getcwd(NULL, 0);
5262 if (cwd == NULL) {
5263 error = got_error_from_errno("getcwd");
5264 goto done;
5266 error = got_worktree_open(&worktree, cwd);
5267 if (error)
5268 goto done;
5270 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5271 NULL);
5272 if (error != NULL)
5273 goto done;
5275 error = apply_unveil(got_repo_get_path(repo), 0,
5276 got_worktree_get_root_path(worktree));
5277 if (error)
5278 goto done;
5280 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5281 if (error)
5282 goto done;
5284 if (abort_rebase) {
5285 int did_something;
5286 if (!rebase_in_progress) {
5287 error = got_error(GOT_ERR_NOT_REBASING);
5288 goto done;
5290 error = got_worktree_rebase_continue(&resume_commit_id,
5291 &new_base_branch, &tmp_branch, &branch, &fileindex,
5292 worktree, repo);
5293 if (error)
5294 goto done;
5295 printf("Switching work tree to %s\n",
5296 got_ref_get_symref_target(new_base_branch));
5297 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5298 new_base_branch, update_progress, &did_something);
5299 if (error)
5300 goto done;
5301 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5302 goto done; /* nothing else to do */
5305 if (continue_rebase) {
5306 if (!rebase_in_progress) {
5307 error = got_error(GOT_ERR_NOT_REBASING);
5308 goto done;
5310 error = got_worktree_rebase_continue(&resume_commit_id,
5311 &new_base_branch, &tmp_branch, &branch, &fileindex,
5312 worktree, repo);
5313 if (error)
5314 goto done;
5316 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5317 resume_commit_id, repo);
5318 if (error)
5319 goto done;
5321 yca_id = got_object_id_dup(resume_commit_id);
5322 if (yca_id == NULL) {
5323 error = got_error_from_errno("got_object_id_dup");
5324 goto done;
5326 } else {
5327 error = got_ref_open(&branch, repo, argv[0], 0);
5328 if (error != NULL)
5329 goto done;
5332 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5333 if (error)
5334 goto done;
5336 if (!continue_rebase) {
5337 struct got_object_id *base_commit_id;
5339 base_commit_id = got_worktree_get_base_commit_id(worktree);
5340 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5341 base_commit_id, branch_head_commit_id, repo,
5342 check_cancelled, NULL);
5343 if (error)
5344 goto done;
5345 if (yca_id == NULL) {
5346 error = got_error_msg(GOT_ERR_ANCESTRY,
5347 "specified branch shares no common ancestry "
5348 "with work tree's branch");
5349 goto done;
5352 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5353 if (error) {
5354 if (error->code != GOT_ERR_ANCESTRY)
5355 goto done;
5356 error = NULL;
5357 } else {
5358 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5359 "specified branch resolves to a commit which "
5360 "is already contained in work tree's branch");
5361 goto done;
5363 error = got_worktree_rebase_prepare(&new_base_branch,
5364 &tmp_branch, &fileindex, worktree, branch, repo);
5365 if (error)
5366 goto done;
5369 commit_id = branch_head_commit_id;
5370 error = got_object_open_as_commit(&commit, repo, commit_id);
5371 if (error)
5372 goto done;
5374 parent_ids = got_object_commit_get_parent_ids(commit);
5375 pid = SIMPLEQ_FIRST(parent_ids);
5376 if (pid == NULL) {
5377 if (!continue_rebase) {
5378 int did_something;
5379 error = got_worktree_rebase_abort(worktree, fileindex,
5380 repo, new_base_branch, update_progress,
5381 &did_something);
5382 if (error)
5383 goto done;
5384 printf("Rebase of %s aborted\n",
5385 got_ref_get_name(branch));
5387 error = got_error(GOT_ERR_EMPTY_REBASE);
5388 goto done;
5390 error = collect_commits(&commits, commit_id, pid->id,
5391 yca_id, got_worktree_get_path_prefix(worktree),
5392 GOT_ERR_REBASE_PATH, repo);
5393 got_object_commit_close(commit);
5394 commit = NULL;
5395 if (error)
5396 goto done;
5398 if (SIMPLEQ_EMPTY(&commits)) {
5399 if (continue_rebase)
5400 error = rebase_complete(worktree, fileindex,
5401 branch, new_base_branch, tmp_branch, repo);
5402 else
5403 error = got_error(GOT_ERR_EMPTY_REBASE);
5404 goto done;
5407 pid = NULL;
5408 SIMPLEQ_FOREACH(qid, &commits, entry) {
5409 commit_id = qid->id;
5410 parent_id = pid ? pid->id : yca_id;
5411 pid = qid;
5413 error = got_worktree_rebase_merge_files(&merged_paths,
5414 worktree, fileindex, parent_id, commit_id, repo,
5415 rebase_progress, &rebase_status, check_cancelled, NULL);
5416 if (error)
5417 goto done;
5419 if (rebase_status == GOT_STATUS_CONFLICT) {
5420 got_worktree_rebase_pathlist_free(&merged_paths);
5421 break;
5424 error = rebase_commit(&merged_paths, worktree, fileindex,
5425 tmp_branch, commit_id, repo);
5426 got_worktree_rebase_pathlist_free(&merged_paths);
5427 if (error)
5428 goto done;
5431 if (rebase_status == GOT_STATUS_CONFLICT) {
5432 error = got_worktree_rebase_postpone(worktree, fileindex);
5433 if (error)
5434 goto done;
5435 error = got_error_msg(GOT_ERR_CONFLICTS,
5436 "conflicts must be resolved before rebasing can continue");
5437 } else
5438 error = rebase_complete(worktree, fileindex, branch,
5439 new_base_branch, tmp_branch, repo);
5440 done:
5441 got_object_id_queue_free(&commits);
5442 free(branch_head_commit_id);
5443 free(resume_commit_id);
5444 free(yca_id);
5445 if (commit)
5446 got_object_commit_close(commit);
5447 if (branch)
5448 got_ref_close(branch);
5449 if (new_base_branch)
5450 got_ref_close(new_base_branch);
5451 if (tmp_branch)
5452 got_ref_close(tmp_branch);
5453 if (worktree)
5454 got_worktree_close(worktree);
5455 if (repo)
5456 got_repo_close(repo);
5457 return error;
5460 __dead static void
5461 usage_histedit(void)
5463 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5464 getprogname());
5465 exit(1);
5468 #define GOT_HISTEDIT_PICK 'p'
5469 #define GOT_HISTEDIT_EDIT 'e'
5470 #define GOT_HISTEDIT_FOLD 'f'
5471 #define GOT_HISTEDIT_DROP 'd'
5472 #define GOT_HISTEDIT_MESG 'm'
5474 static struct got_histedit_cmd {
5475 unsigned char code;
5476 const char *name;
5477 const char *desc;
5478 } got_histedit_cmds[] = {
5479 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5480 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5481 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5482 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5483 { GOT_HISTEDIT_MESG, "mesg",
5484 "single-line log message for commit above (open editor if empty)" },
5487 struct got_histedit_list_entry {
5488 TAILQ_ENTRY(got_histedit_list_entry) entry;
5489 struct got_object_id *commit_id;
5490 const struct got_histedit_cmd *cmd;
5491 char *logmsg;
5493 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5495 static const struct got_error *
5496 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5497 FILE *f, struct got_repository *repo)
5499 const struct got_error *err = NULL;
5500 char *logmsg = NULL, *id_str = NULL;
5501 struct got_commit_object *commit = NULL;
5502 int n;
5504 err = got_object_open_as_commit(&commit, repo, commit_id);
5505 if (err)
5506 goto done;
5508 err = get_short_logmsg(&logmsg, 34, commit);
5509 if (err)
5510 goto done;
5512 err = got_object_id_str(&id_str, commit_id);
5513 if (err)
5514 goto done;
5516 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5517 if (n < 0)
5518 err = got_ferror(f, GOT_ERR_IO);
5519 done:
5520 if (commit)
5521 got_object_commit_close(commit);
5522 free(id_str);
5523 free(logmsg);
5524 return err;
5527 static const struct got_error *
5528 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5529 struct got_repository *repo)
5531 const struct got_error *err = NULL;
5532 struct got_object_qid *qid;
5534 if (SIMPLEQ_EMPTY(commits))
5535 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5537 SIMPLEQ_FOREACH(qid, commits, entry) {
5538 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5539 f, repo);
5540 if (err)
5541 break;
5544 return err;
5547 static const struct got_error *
5548 write_cmd_list(FILE *f)
5550 const struct got_error *err = NULL;
5551 int n, i;
5553 n = fprintf(f, "# Available histedit commands:\n");
5554 if (n < 0)
5555 return got_ferror(f, GOT_ERR_IO);
5557 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5558 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5559 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5560 cmd->desc);
5561 if (n < 0) {
5562 err = got_ferror(f, GOT_ERR_IO);
5563 break;
5566 n = fprintf(f, "# Commits will be processed in order from top to "
5567 "bottom of this file.\n");
5568 if (n < 0)
5569 return got_ferror(f, GOT_ERR_IO);
5570 return err;
5573 static const struct got_error *
5574 histedit_syntax_error(int lineno)
5576 static char msg[42];
5577 int ret;
5579 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5580 lineno);
5581 if (ret == -1 || ret >= sizeof(msg))
5582 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5584 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5587 static const struct got_error *
5588 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5589 char *logmsg, struct got_repository *repo)
5591 const struct got_error *err;
5592 struct got_commit_object *folded_commit = NULL;
5593 char *id_str, *folded_logmsg = NULL;
5595 err = got_object_id_str(&id_str, hle->commit_id);
5596 if (err)
5597 return err;
5599 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5600 if (err)
5601 goto done;
5603 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5604 if (err)
5605 goto done;
5606 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5607 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5608 folded_logmsg) == -1) {
5609 err = got_error_from_errno("asprintf");
5610 goto done;
5612 done:
5613 if (folded_commit)
5614 got_object_commit_close(folded_commit);
5615 free(id_str);
5616 free(folded_logmsg);
5617 return err;
5620 static struct got_histedit_list_entry *
5621 get_folded_commits(struct got_histedit_list_entry *hle)
5623 struct got_histedit_list_entry *prev, *folded = NULL;
5625 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5626 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5627 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5628 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5629 folded = prev;
5630 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5633 return folded;
5636 static const struct got_error *
5637 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5638 struct got_repository *repo)
5640 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5641 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5642 const struct got_error *err = NULL;
5643 struct got_commit_object *commit = NULL;
5644 int fd;
5645 struct got_histedit_list_entry *folded = NULL;
5647 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5648 if (err)
5649 return err;
5651 folded = get_folded_commits(hle);
5652 if (folded) {
5653 while (folded != hle) {
5654 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5655 folded = TAILQ_NEXT(folded, entry);
5656 continue;
5658 err = append_folded_commit_msg(&new_msg, folded,
5659 logmsg, repo);
5660 if (err)
5661 goto done;
5662 free(logmsg);
5663 logmsg = new_msg;
5664 folded = TAILQ_NEXT(folded, entry);
5668 err = got_object_id_str(&id_str, hle->commit_id);
5669 if (err)
5670 goto done;
5671 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5672 if (err)
5673 goto done;
5674 if (asprintf(&new_msg,
5675 "%s\n# original log message of commit %s: %s",
5676 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5677 err = got_error_from_errno("asprintf");
5678 goto done;
5680 free(logmsg);
5681 logmsg = new_msg;
5683 err = got_object_id_str(&id_str, hle->commit_id);
5684 if (err)
5685 goto done;
5687 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5688 if (err)
5689 goto done;
5691 dprintf(fd, logmsg);
5692 close(fd);
5694 err = get_editor(&editor);
5695 if (err)
5696 goto done;
5698 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5699 if (err) {
5700 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5701 goto done;
5702 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5704 done:
5705 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5706 err = got_error_from_errno2("unlink", logmsg_path);
5707 free(logmsg_path);
5708 free(logmsg);
5709 free(orig_logmsg);
5710 free(editor);
5711 if (commit)
5712 got_object_commit_close(commit);
5713 return err;
5716 static const struct got_error *
5717 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5718 FILE *f, struct got_repository *repo)
5720 const struct got_error *err = NULL;
5721 char *line = NULL, *p, *end;
5722 size_t size;
5723 ssize_t len;
5724 int lineno = 0, i;
5725 const struct got_histedit_cmd *cmd;
5726 struct got_object_id *commit_id = NULL;
5727 struct got_histedit_list_entry *hle = NULL;
5729 for (;;) {
5730 len = getline(&line, &size, f);
5731 if (len == -1) {
5732 const struct got_error *getline_err;
5733 if (feof(f))
5734 break;
5735 getline_err = got_error_from_errno("getline");
5736 err = got_ferror(f, getline_err->code);
5737 break;
5739 lineno++;
5740 p = line;
5741 while (isspace((unsigned char)p[0]))
5742 p++;
5743 if (p[0] == '#' || p[0] == '\0') {
5744 free(line);
5745 line = NULL;
5746 continue;
5748 cmd = NULL;
5749 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5750 cmd = &got_histedit_cmds[i];
5751 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5752 isspace((unsigned char)p[strlen(cmd->name)])) {
5753 p += strlen(cmd->name);
5754 break;
5756 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5757 p++;
5758 break;
5761 if (i == nitems(got_histedit_cmds)) {
5762 err = histedit_syntax_error(lineno);
5763 break;
5765 while (isspace((unsigned char)p[0]))
5766 p++;
5767 if (cmd->code == GOT_HISTEDIT_MESG) {
5768 if (hle == NULL || hle->logmsg != NULL) {
5769 err = got_error(GOT_ERR_HISTEDIT_CMD);
5770 break;
5772 if (p[0] == '\0') {
5773 err = histedit_edit_logmsg(hle, repo);
5774 if (err)
5775 break;
5776 } else {
5777 hle->logmsg = strdup(p);
5778 if (hle->logmsg == NULL) {
5779 err = got_error_from_errno("strdup");
5780 break;
5783 free(line);
5784 line = NULL;
5785 continue;
5786 } else {
5787 end = p;
5788 while (end[0] && !isspace((unsigned char)end[0]))
5789 end++;
5790 *end = '\0';
5792 err = got_object_resolve_id_str(&commit_id, repo, p);
5793 if (err) {
5794 /* override error code */
5795 err = histedit_syntax_error(lineno);
5796 break;
5799 hle = malloc(sizeof(*hle));
5800 if (hle == NULL) {
5801 err = got_error_from_errno("malloc");
5802 break;
5804 hle->cmd = cmd;
5805 hle->commit_id = commit_id;
5806 hle->logmsg = NULL;
5807 commit_id = NULL;
5808 free(line);
5809 line = NULL;
5810 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5813 free(line);
5814 free(commit_id);
5815 return err;
5818 static const struct got_error *
5819 histedit_check_script(struct got_histedit_list *histedit_cmds,
5820 struct got_object_id_queue *commits, struct got_repository *repo)
5822 const struct got_error *err = NULL;
5823 struct got_object_qid *qid;
5824 struct got_histedit_list_entry *hle;
5825 static char msg[80];
5826 char *id_str;
5828 if (TAILQ_EMPTY(histedit_cmds))
5829 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5830 "histedit script contains no commands");
5831 if (SIMPLEQ_EMPTY(commits))
5832 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5834 SIMPLEQ_FOREACH(qid, commits, entry) {
5835 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5836 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5837 break;
5839 if (hle == NULL) {
5840 err = got_object_id_str(&id_str, qid->id);
5841 if (err)
5842 return err;
5843 snprintf(msg, sizeof(msg),
5844 "commit %s missing from histedit script", id_str);
5845 free(id_str);
5846 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5850 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5851 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5852 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5853 "last commit in histedit script cannot be folded");
5855 return NULL;
5858 static const struct got_error *
5859 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5860 const char *path, struct got_object_id_queue *commits,
5861 struct got_repository *repo)
5863 const struct got_error *err = NULL;
5864 char *editor;
5865 FILE *f = NULL;
5867 err = get_editor(&editor);
5868 if (err)
5869 return err;
5871 if (spawn_editor(editor, path) == -1) {
5872 err = got_error_from_errno("failed spawning editor");
5873 goto done;
5876 f = fopen(path, "r");
5877 if (f == NULL) {
5878 err = got_error_from_errno("fopen");
5879 goto done;
5881 err = histedit_parse_list(histedit_cmds, f, repo);
5882 if (err)
5883 goto done;
5885 err = histedit_check_script(histedit_cmds, commits, repo);
5886 done:
5887 if (f && fclose(f) != 0 && err == NULL)
5888 err = got_error_from_errno("fclose");
5889 free(editor);
5890 return err;
5893 static const struct got_error *
5894 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5895 struct got_object_id_queue *, const char *, struct got_repository *);
5897 static const struct got_error *
5898 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5899 struct got_object_id_queue *commits, struct got_repository *repo)
5901 const struct got_error *err;
5902 FILE *f = NULL;
5903 char *path = NULL;
5905 err = got_opentemp_named(&path, &f, "got-histedit");
5906 if (err)
5907 return err;
5909 err = write_cmd_list(f);
5910 if (err)
5911 goto done;
5913 err = histedit_write_commit_list(commits, f, repo);
5914 if (err)
5915 goto done;
5917 if (fclose(f) != 0) {
5918 err = got_error_from_errno("fclose");
5919 goto done;
5921 f = NULL;
5923 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5924 if (err) {
5925 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5926 err->code != GOT_ERR_HISTEDIT_CMD)
5927 goto done;
5928 err = histedit_edit_list_retry(histedit_cmds, err,
5929 commits, path, repo);
5931 done:
5932 if (f && fclose(f) != 0 && err == NULL)
5933 err = got_error_from_errno("fclose");
5934 if (path && unlink(path) != 0 && err == NULL)
5935 err = got_error_from_errno2("unlink", path);
5936 free(path);
5937 return err;
5940 static const struct got_error *
5941 histedit_save_list(struct got_histedit_list *histedit_cmds,
5942 struct got_worktree *worktree, struct got_repository *repo)
5944 const struct got_error *err = NULL;
5945 char *path = NULL;
5946 FILE *f = NULL;
5947 struct got_histedit_list_entry *hle;
5948 struct got_commit_object *commit = NULL;
5950 err = got_worktree_get_histedit_script_path(&path, worktree);
5951 if (err)
5952 return err;
5954 f = fopen(path, "w");
5955 if (f == NULL) {
5956 err = got_error_from_errno2("fopen", path);
5957 goto done;
5959 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5960 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5961 repo);
5962 if (err)
5963 break;
5965 if (hle->logmsg) {
5966 int n = fprintf(f, "%c %s\n",
5967 GOT_HISTEDIT_MESG, hle->logmsg);
5968 if (n < 0) {
5969 err = got_ferror(f, GOT_ERR_IO);
5970 break;
5974 done:
5975 if (f && fclose(f) != 0 && err == NULL)
5976 err = got_error_from_errno("fclose");
5977 free(path);
5978 if (commit)
5979 got_object_commit_close(commit);
5980 return err;
5983 void
5984 histedit_free_list(struct got_histedit_list *histedit_cmds)
5986 struct got_histedit_list_entry *hle;
5988 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5989 TAILQ_REMOVE(histedit_cmds, hle, entry);
5990 free(hle);
5994 static const struct got_error *
5995 histedit_load_list(struct got_histedit_list *histedit_cmds,
5996 const char *path, struct got_repository *repo)
5998 const struct got_error *err = NULL;
5999 FILE *f = NULL;
6001 f = fopen(path, "r");
6002 if (f == NULL) {
6003 err = got_error_from_errno2("fopen", path);
6004 goto done;
6007 err = histedit_parse_list(histedit_cmds, f, repo);
6008 done:
6009 if (f && fclose(f) != 0 && err == NULL)
6010 err = got_error_from_errno("fclose");
6011 return err;
6014 static const struct got_error *
6015 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6016 const struct got_error *edit_err, struct got_object_id_queue *commits,
6017 const char *path, struct got_repository *repo)
6019 const struct got_error *err = NULL, *prev_err = edit_err;
6020 int resp = ' ';
6022 while (resp != 'c' && resp != 'r' && resp != 'a') {
6023 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6024 "or (a)bort: ", getprogname(), prev_err->msg);
6025 resp = getchar();
6026 if (resp == '\n')
6027 resp = getchar();
6028 if (resp == 'c') {
6029 histedit_free_list(histedit_cmds);
6030 err = histedit_run_editor(histedit_cmds, path, commits,
6031 repo);
6032 if (err) {
6033 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6034 err->code != GOT_ERR_HISTEDIT_CMD)
6035 break;
6036 prev_err = err;
6037 resp = ' ';
6038 continue;
6040 break;
6041 } else if (resp == 'r') {
6042 histedit_free_list(histedit_cmds);
6043 err = histedit_edit_script(histedit_cmds,
6044 commits, repo);
6045 if (err) {
6046 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6047 err->code != GOT_ERR_HISTEDIT_CMD)
6048 break;
6049 prev_err = err;
6050 resp = ' ';
6051 continue;
6053 break;
6054 } else if (resp == 'a') {
6055 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6056 break;
6057 } else
6058 printf("invalid response '%c'\n", resp);
6061 return err;
6064 static const struct got_error *
6065 histedit_complete(struct got_worktree *worktree,
6066 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6067 struct got_reference *branch, struct got_repository *repo)
6069 printf("Switching work tree to %s\n",
6070 got_ref_get_symref_target(branch));
6071 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6072 branch, repo);
6075 static const struct got_error *
6076 show_histedit_progress(struct got_commit_object *commit,
6077 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6079 const struct got_error *err;
6080 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6082 err = got_object_id_str(&old_id_str, hle->commit_id);
6083 if (err)
6084 goto done;
6086 if (new_id) {
6087 err = got_object_id_str(&new_id_str, new_id);
6088 if (err)
6089 goto done;
6092 old_id_str[12] = '\0';
6093 if (new_id_str)
6094 new_id_str[12] = '\0';
6096 if (hle->logmsg) {
6097 logmsg = strdup(hle->logmsg);
6098 if (logmsg == NULL) {
6099 err = got_error_from_errno("strdup");
6100 goto done;
6102 trim_logmsg(logmsg, 42);
6103 } else {
6104 err = get_short_logmsg(&logmsg, 42, commit);
6105 if (err)
6106 goto done;
6109 switch (hle->cmd->code) {
6110 case GOT_HISTEDIT_PICK:
6111 case GOT_HISTEDIT_EDIT:
6112 printf("%s -> %s: %s\n", old_id_str,
6113 new_id_str ? new_id_str : "no-op change", logmsg);
6114 break;
6115 case GOT_HISTEDIT_DROP:
6116 case GOT_HISTEDIT_FOLD:
6117 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6118 logmsg);
6119 break;
6120 default:
6121 break;
6124 done:
6125 free(old_id_str);
6126 free(new_id_str);
6127 return err;
6130 static const struct got_error *
6131 histedit_commit(struct got_pathlist_head *merged_paths,
6132 struct got_worktree *worktree, struct got_fileindex *fileindex,
6133 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6134 struct got_repository *repo)
6136 const struct got_error *err;
6137 struct got_commit_object *commit;
6138 struct got_object_id *new_commit_id;
6140 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6141 && hle->logmsg == NULL) {
6142 err = histedit_edit_logmsg(hle, repo);
6143 if (err)
6144 return err;
6147 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6148 if (err)
6149 return err;
6151 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6152 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6153 hle->logmsg, repo);
6154 if (err) {
6155 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6156 goto done;
6157 err = show_histedit_progress(commit, hle, NULL);
6158 } else {
6159 err = show_histedit_progress(commit, hle, new_commit_id);
6160 free(new_commit_id);
6162 done:
6163 got_object_commit_close(commit);
6164 return err;
6167 static const struct got_error *
6168 histedit_skip_commit(struct got_histedit_list_entry *hle,
6169 struct got_worktree *worktree, struct got_repository *repo)
6171 const struct got_error *error;
6172 struct got_commit_object *commit;
6174 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6175 repo);
6176 if (error)
6177 return error;
6179 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6180 if (error)
6181 return error;
6183 error = show_histedit_progress(commit, hle, NULL);
6184 got_object_commit_close(commit);
6185 return error;
6188 static const struct got_error *
6189 cmd_histedit(int argc, char *argv[])
6191 const struct got_error *error = NULL;
6192 struct got_worktree *worktree = NULL;
6193 struct got_fileindex *fileindex = NULL;
6194 struct got_repository *repo = NULL;
6195 char *cwd = NULL;
6196 struct got_reference *branch = NULL;
6197 struct got_reference *tmp_branch = NULL;
6198 struct got_object_id *resume_commit_id = NULL;
6199 struct got_object_id *base_commit_id = NULL;
6200 struct got_object_id *head_commit_id = NULL;
6201 struct got_commit_object *commit = NULL;
6202 int ch, rebase_in_progress = 0, did_something;
6203 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6204 const char *edit_script_path = NULL;
6205 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6206 struct got_object_id_queue commits;
6207 struct got_pathlist_head merged_paths;
6208 const struct got_object_id_queue *parent_ids;
6209 struct got_object_qid *pid;
6210 struct got_histedit_list histedit_cmds;
6211 struct got_histedit_list_entry *hle;
6213 SIMPLEQ_INIT(&commits);
6214 TAILQ_INIT(&histedit_cmds);
6215 TAILQ_INIT(&merged_paths);
6217 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6218 switch (ch) {
6219 case 'a':
6220 abort_edit = 1;
6221 break;
6222 case 'c':
6223 continue_edit = 1;
6224 break;
6225 case 'F':
6226 edit_script_path = optarg;
6227 break;
6228 default:
6229 usage_histedit();
6230 /* NOTREACHED */
6234 argc -= optind;
6235 argv += optind;
6237 #ifndef PROFILE
6238 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6239 "unveil", NULL) == -1)
6240 err(1, "pledge");
6241 #endif
6242 if (abort_edit && continue_edit)
6243 usage_histedit();
6244 if (argc != 0)
6245 usage_histedit();
6248 * This command cannot apply unveil(2) in all cases because the
6249 * user may choose to run an editor to edit the histedit script
6250 * and to edit individual commit log messages.
6251 * unveil(2) traverses exec(2); if an editor is used we have to
6252 * apply unveil after edit script and log messages have been written.
6253 * XXX TODO: Make use of unveil(2) where possible.
6256 cwd = getcwd(NULL, 0);
6257 if (cwd == NULL) {
6258 error = got_error_from_errno("getcwd");
6259 goto done;
6261 error = got_worktree_open(&worktree, cwd);
6262 if (error)
6263 goto done;
6265 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6266 NULL);
6267 if (error != NULL)
6268 goto done;
6270 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6271 if (error)
6272 goto done;
6273 if (rebase_in_progress) {
6274 error = got_error(GOT_ERR_REBASING);
6275 goto done;
6278 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6279 if (error)
6280 goto done;
6282 if (edit_in_progress && abort_edit) {
6283 error = got_worktree_histedit_continue(&resume_commit_id,
6284 &tmp_branch, &branch, &base_commit_id, &fileindex,
6285 worktree, repo);
6286 if (error)
6287 goto done;
6288 printf("Switching work tree to %s\n",
6289 got_ref_get_symref_target(branch));
6290 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6291 branch, base_commit_id, update_progress, &did_something);
6292 if (error)
6293 goto done;
6294 printf("Histedit of %s aborted\n",
6295 got_ref_get_symref_target(branch));
6296 goto done; /* nothing else to do */
6297 } else if (abort_edit) {
6298 error = got_error(GOT_ERR_NOT_HISTEDIT);
6299 goto done;
6302 if (continue_edit) {
6303 char *path;
6305 if (!edit_in_progress) {
6306 error = got_error(GOT_ERR_NOT_HISTEDIT);
6307 goto done;
6310 error = got_worktree_get_histedit_script_path(&path, worktree);
6311 if (error)
6312 goto done;
6314 error = histedit_load_list(&histedit_cmds, path, repo);
6315 free(path);
6316 if (error)
6317 goto done;
6319 error = got_worktree_histedit_continue(&resume_commit_id,
6320 &tmp_branch, &branch, &base_commit_id, &fileindex,
6321 worktree, repo);
6322 if (error)
6323 goto done;
6325 error = got_ref_resolve(&head_commit_id, repo, branch);
6326 if (error)
6327 goto done;
6329 error = got_object_open_as_commit(&commit, repo,
6330 head_commit_id);
6331 if (error)
6332 goto done;
6333 parent_ids = got_object_commit_get_parent_ids(commit);
6334 pid = SIMPLEQ_FIRST(parent_ids);
6335 if (pid == NULL) {
6336 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6337 goto done;
6339 error = collect_commits(&commits, head_commit_id, pid->id,
6340 base_commit_id, got_worktree_get_path_prefix(worktree),
6341 GOT_ERR_HISTEDIT_PATH, repo);
6342 got_object_commit_close(commit);
6343 commit = NULL;
6344 if (error)
6345 goto done;
6346 } else {
6347 if (edit_in_progress) {
6348 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6349 goto done;
6352 error = got_ref_open(&branch, repo,
6353 got_worktree_get_head_ref_name(worktree), 0);
6354 if (error != NULL)
6355 goto done;
6357 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6358 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6359 "will not edit commit history of a branch outside "
6360 "the \"refs/heads/\" reference namespace");
6361 goto done;
6364 error = got_ref_resolve(&head_commit_id, repo, branch);
6365 got_ref_close(branch);
6366 branch = NULL;
6367 if (error)
6368 goto done;
6370 error = got_object_open_as_commit(&commit, repo,
6371 head_commit_id);
6372 if (error)
6373 goto done;
6374 parent_ids = got_object_commit_get_parent_ids(commit);
6375 pid = SIMPLEQ_FIRST(parent_ids);
6376 if (pid == NULL) {
6377 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6378 goto done;
6380 error = collect_commits(&commits, head_commit_id, pid->id,
6381 got_worktree_get_base_commit_id(worktree),
6382 got_worktree_get_path_prefix(worktree),
6383 GOT_ERR_HISTEDIT_PATH, repo);
6384 got_object_commit_close(commit);
6385 commit = NULL;
6386 if (error)
6387 goto done;
6389 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6390 &base_commit_id, &fileindex, worktree, repo);
6391 if (error)
6392 goto done;
6394 if (edit_script_path) {
6395 error = histedit_load_list(&histedit_cmds,
6396 edit_script_path, repo);
6397 if (error) {
6398 got_worktree_histedit_abort(worktree, fileindex,
6399 repo, branch, base_commit_id,
6400 update_progress, &did_something);
6401 goto done;
6403 } else {
6404 error = histedit_edit_script(&histedit_cmds, &commits,
6405 repo);
6406 if (error) {
6407 got_worktree_histedit_abort(worktree, fileindex,
6408 repo, branch, base_commit_id,
6409 update_progress, &did_something);
6410 goto done;
6415 error = histedit_save_list(&histedit_cmds, worktree,
6416 repo);
6417 if (error) {
6418 got_worktree_histedit_abort(worktree, fileindex,
6419 repo, branch, base_commit_id,
6420 update_progress, &did_something);
6421 goto done;
6426 error = histedit_check_script(&histedit_cmds, &commits, repo);
6427 if (error)
6428 goto done;
6430 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6431 if (resume_commit_id) {
6432 if (got_object_id_cmp(hle->commit_id,
6433 resume_commit_id) != 0)
6434 continue;
6436 resume_commit_id = NULL;
6437 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6438 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6439 error = histedit_skip_commit(hle, worktree,
6440 repo);
6441 } else {
6442 error = histedit_commit(NULL, worktree,
6443 fileindex, tmp_branch, hle, repo);
6445 if (error)
6446 goto done;
6447 continue;
6450 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6451 error = histedit_skip_commit(hle, worktree, repo);
6452 if (error)
6453 goto done;
6454 continue;
6457 error = got_object_open_as_commit(&commit, repo,
6458 hle->commit_id);
6459 if (error)
6460 goto done;
6461 parent_ids = got_object_commit_get_parent_ids(commit);
6462 pid = SIMPLEQ_FIRST(parent_ids);
6464 error = got_worktree_histedit_merge_files(&merged_paths,
6465 worktree, fileindex, pid->id, hle->commit_id, repo,
6466 rebase_progress, &rebase_status, check_cancelled, NULL);
6467 if (error)
6468 goto done;
6469 got_object_commit_close(commit);
6470 commit = NULL;
6472 if (rebase_status == GOT_STATUS_CONFLICT) {
6473 got_worktree_rebase_pathlist_free(&merged_paths);
6474 break;
6477 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6478 char *id_str;
6479 error = got_object_id_str(&id_str, hle->commit_id);
6480 if (error)
6481 goto done;
6482 printf("Stopping histedit for amending commit %s\n",
6483 id_str);
6484 free(id_str);
6485 got_worktree_rebase_pathlist_free(&merged_paths);
6486 error = got_worktree_histedit_postpone(worktree,
6487 fileindex);
6488 goto done;
6491 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6492 error = histedit_skip_commit(hle, worktree, repo);
6493 if (error)
6494 goto done;
6495 continue;
6498 error = histedit_commit(&merged_paths, worktree, fileindex,
6499 tmp_branch, hle, repo);
6500 got_worktree_rebase_pathlist_free(&merged_paths);
6501 if (error)
6502 goto done;
6505 if (rebase_status == GOT_STATUS_CONFLICT) {
6506 error = got_worktree_histedit_postpone(worktree, fileindex);
6507 if (error)
6508 goto done;
6509 error = got_error_msg(GOT_ERR_CONFLICTS,
6510 "conflicts must be resolved before rebasing can continue");
6511 } else
6512 error = histedit_complete(worktree, fileindex, tmp_branch,
6513 branch, repo);
6514 done:
6515 got_object_id_queue_free(&commits);
6516 histedit_free_list(&histedit_cmds);
6517 free(head_commit_id);
6518 free(base_commit_id);
6519 free(resume_commit_id);
6520 if (commit)
6521 got_object_commit_close(commit);
6522 if (branch)
6523 got_ref_close(branch);
6524 if (tmp_branch)
6525 got_ref_close(tmp_branch);
6526 if (worktree)
6527 got_worktree_close(worktree);
6528 if (repo)
6529 got_repo_close(repo);
6530 return error;
6533 __dead static void
6534 usage_integrate(void)
6536 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6537 exit(1);
6540 static const struct got_error *
6541 cmd_integrate(int argc, char *argv[])
6543 const struct got_error *error = NULL;
6544 struct got_repository *repo = NULL;
6545 struct got_worktree *worktree = NULL;
6546 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6547 const char *branch_arg = NULL;
6548 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6549 struct got_fileindex *fileindex = NULL;
6550 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6551 int ch, did_something = 0;
6553 while ((ch = getopt(argc, argv, "")) != -1) {
6554 switch (ch) {
6555 default:
6556 usage_integrate();
6557 /* NOTREACHED */
6561 argc -= optind;
6562 argv += optind;
6564 if (argc != 1)
6565 usage_integrate();
6566 branch_arg = argv[0];
6568 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6569 "unveil", NULL) == -1)
6570 err(1, "pledge");
6572 cwd = getcwd(NULL, 0);
6573 if (cwd == NULL) {
6574 error = got_error_from_errno("getcwd");
6575 goto done;
6578 error = got_worktree_open(&worktree, cwd);
6579 if (error)
6580 goto done;
6582 error = check_rebase_or_histedit_in_progress(worktree);
6583 if (error)
6584 goto done;
6586 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6587 NULL);
6588 if (error != NULL)
6589 goto done;
6591 error = apply_unveil(got_repo_get_path(repo), 0,
6592 got_worktree_get_root_path(worktree));
6593 if (error)
6594 goto done;
6596 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6597 error = got_error_from_errno("asprintf");
6598 goto done;
6601 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6602 &base_branch_ref, worktree, refname, repo);
6603 if (error)
6604 goto done;
6606 refname = strdup(got_ref_get_name(branch_ref));
6607 if (refname == NULL) {
6608 error = got_error_from_errno("strdup");
6609 got_worktree_integrate_abort(worktree, fileindex, repo,
6610 branch_ref, base_branch_ref);
6611 goto done;
6613 base_refname = strdup(got_ref_get_name(base_branch_ref));
6614 if (base_refname == NULL) {
6615 error = got_error_from_errno("strdup");
6616 got_worktree_integrate_abort(worktree, fileindex, repo,
6617 branch_ref, base_branch_ref);
6618 goto done;
6621 error = got_ref_resolve(&commit_id, repo, branch_ref);
6622 if (error)
6623 goto done;
6625 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6626 if (error)
6627 goto done;
6629 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6630 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6631 "specified branch has already been integrated");
6632 got_worktree_integrate_abort(worktree, fileindex, repo,
6633 branch_ref, base_branch_ref);
6634 goto done;
6637 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6638 if (error) {
6639 if (error->code == GOT_ERR_ANCESTRY)
6640 error = got_error(GOT_ERR_REBASE_REQUIRED);
6641 got_worktree_integrate_abort(worktree, fileindex, repo,
6642 branch_ref, base_branch_ref);
6643 goto done;
6646 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6647 branch_ref, base_branch_ref, update_progress, &did_something,
6648 check_cancelled, NULL);
6649 if (error)
6650 goto done;
6652 printf("Integrated %s into %s\n", refname, base_refname);
6653 done:
6654 if (repo)
6655 got_repo_close(repo);
6656 if (worktree)
6657 got_worktree_close(worktree);
6658 free(cwd);
6659 free(base_commit_id);
6660 free(commit_id);
6661 free(refname);
6662 free(base_refname);
6663 return error;
6666 __dead static void
6667 usage_stage(void)
6669 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6670 "[file-path ...]\n",
6671 getprogname());
6672 exit(1);
6675 static const struct got_error *
6676 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6677 const char *path, struct got_object_id *blob_id,
6678 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6680 const struct got_error *err = NULL;
6681 char *id_str = NULL;
6683 if (staged_status != GOT_STATUS_ADD &&
6684 staged_status != GOT_STATUS_MODIFY &&
6685 staged_status != GOT_STATUS_DELETE)
6686 return NULL;
6688 if (staged_status == GOT_STATUS_ADD ||
6689 staged_status == GOT_STATUS_MODIFY)
6690 err = got_object_id_str(&id_str, staged_blob_id);
6691 else
6692 err = got_object_id_str(&id_str, blob_id);
6693 if (err)
6694 return err;
6696 printf("%s %c %s\n", id_str, staged_status, path);
6697 free(id_str);
6698 return NULL;
6701 static const struct got_error *
6702 cmd_stage(int argc, char *argv[])
6704 const struct got_error *error = NULL;
6705 struct got_repository *repo = NULL;
6706 struct got_worktree *worktree = NULL;
6707 char *cwd = NULL;
6708 struct got_pathlist_head paths;
6709 struct got_pathlist_entry *pe;
6710 int ch, list_stage = 0, pflag = 0;
6711 FILE *patch_script_file = NULL;
6712 const char *patch_script_path = NULL;
6713 struct choose_patch_arg cpa;
6715 TAILQ_INIT(&paths);
6717 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6718 switch (ch) {
6719 case 'l':
6720 list_stage = 1;
6721 break;
6722 case 'p':
6723 pflag = 1;
6724 break;
6725 case 'F':
6726 patch_script_path = optarg;
6727 break;
6728 default:
6729 usage_stage();
6730 /* NOTREACHED */
6734 argc -= optind;
6735 argv += optind;
6737 #ifndef PROFILE
6738 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6739 "unveil", NULL) == -1)
6740 err(1, "pledge");
6741 #endif
6742 if (list_stage && (pflag || patch_script_path))
6743 errx(1, "-l option cannot be used with other options");
6744 if (patch_script_path && !pflag)
6745 errx(1, "-F option can only be used together with -p option");
6747 cwd = getcwd(NULL, 0);
6748 if (cwd == NULL) {
6749 error = got_error_from_errno("getcwd");
6750 goto done;
6753 error = got_worktree_open(&worktree, cwd);
6754 if (error)
6755 goto done;
6757 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6758 NULL);
6759 if (error != NULL)
6760 goto done;
6762 if (patch_script_path) {
6763 patch_script_file = fopen(patch_script_path, "r");
6764 if (patch_script_file == NULL) {
6765 error = got_error_from_errno2("fopen",
6766 patch_script_path);
6767 goto done;
6770 error = apply_unveil(got_repo_get_path(repo), 0,
6771 got_worktree_get_root_path(worktree));
6772 if (error)
6773 goto done;
6775 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6776 if (error)
6777 goto done;
6779 if (list_stage)
6780 error = got_worktree_status(worktree, &paths, repo,
6781 print_stage, NULL, check_cancelled, NULL);
6782 else {
6783 cpa.patch_script_file = patch_script_file;
6784 cpa.action = "stage";
6785 error = got_worktree_stage(worktree, &paths,
6786 pflag ? NULL : print_status, NULL,
6787 pflag ? choose_patch : NULL, &cpa, repo);
6789 done:
6790 if (patch_script_file && fclose(patch_script_file) == EOF &&
6791 error == NULL)
6792 error = got_error_from_errno2("fclose", patch_script_path);
6793 if (repo)
6794 got_repo_close(repo);
6795 if (worktree)
6796 got_worktree_close(worktree);
6797 TAILQ_FOREACH(pe, &paths, entry)
6798 free((char *)pe->path);
6799 got_pathlist_free(&paths);
6800 free(cwd);
6801 return error;
6804 __dead static void
6805 usage_unstage(void)
6807 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6808 "[file-path ...]\n",
6809 getprogname());
6810 exit(1);
6814 static const struct got_error *
6815 cmd_unstage(int argc, char *argv[])
6817 const struct got_error *error = NULL;
6818 struct got_repository *repo = NULL;
6819 struct got_worktree *worktree = NULL;
6820 char *cwd = NULL;
6821 struct got_pathlist_head paths;
6822 struct got_pathlist_entry *pe;
6823 int ch, did_something = 0, pflag = 0;
6824 FILE *patch_script_file = NULL;
6825 const char *patch_script_path = NULL;
6826 struct choose_patch_arg cpa;
6828 TAILQ_INIT(&paths);
6830 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6831 switch (ch) {
6832 case 'p':
6833 pflag = 1;
6834 break;
6835 case 'F':
6836 patch_script_path = optarg;
6837 break;
6838 default:
6839 usage_unstage();
6840 /* NOTREACHED */
6844 argc -= optind;
6845 argv += optind;
6847 #ifndef PROFILE
6848 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6849 "unveil", NULL) == -1)
6850 err(1, "pledge");
6851 #endif
6852 if (patch_script_path && !pflag)
6853 errx(1, "-F option can only be used together with -p option");
6855 cwd = getcwd(NULL, 0);
6856 if (cwd == NULL) {
6857 error = got_error_from_errno("getcwd");
6858 goto done;
6861 error = got_worktree_open(&worktree, cwd);
6862 if (error)
6863 goto done;
6865 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6866 NULL);
6867 if (error != NULL)
6868 goto done;
6870 if (patch_script_path) {
6871 patch_script_file = fopen(patch_script_path, "r");
6872 if (patch_script_file == NULL) {
6873 error = got_error_from_errno2("fopen",
6874 patch_script_path);
6875 goto done;
6879 error = apply_unveil(got_repo_get_path(repo), 0,
6880 got_worktree_get_root_path(worktree));
6881 if (error)
6882 goto done;
6884 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6885 if (error)
6886 goto done;
6888 cpa.patch_script_file = patch_script_file;
6889 cpa.action = "unstage";
6890 error = got_worktree_unstage(worktree, &paths, update_progress,
6891 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6892 done:
6893 if (patch_script_file && fclose(patch_script_file) == EOF &&
6894 error == NULL)
6895 error = got_error_from_errno2("fclose", patch_script_path);
6896 if (repo)
6897 got_repo_close(repo);
6898 if (worktree)
6899 got_worktree_close(worktree);
6900 TAILQ_FOREACH(pe, &paths, entry)
6901 free((char *)pe->path);
6902 got_pathlist_free(&paths);
6903 free(cwd);
6904 return error;
6907 __dead static void
6908 usage_cat(void)
6910 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6911 "arg1 [arg2 ...]\n", getprogname());
6912 exit(1);
6915 static const struct got_error *
6916 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6918 const struct got_error *err;
6919 struct got_blob_object *blob;
6921 err = got_object_open_as_blob(&blob, repo, id, 8192);
6922 if (err)
6923 return err;
6925 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6926 got_object_blob_close(blob);
6927 return err;
6930 static const struct got_error *
6931 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6933 const struct got_error *err;
6934 struct got_tree_object *tree;
6935 const struct got_tree_entries *entries;
6936 struct got_tree_entry *te;
6938 err = got_object_open_as_tree(&tree, repo, id);
6939 if (err)
6940 return err;
6942 entries = got_object_tree_get_entries(tree);
6943 te = SIMPLEQ_FIRST(&entries->head);
6944 while (te) {
6945 char *id_str;
6946 if (sigint_received || sigpipe_received)
6947 break;
6948 err = got_object_id_str(&id_str, te->id);
6949 if (err)
6950 break;
6951 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6952 free(id_str);
6953 te = SIMPLEQ_NEXT(te, entry);
6956 got_object_tree_close(tree);
6957 return err;
6960 static const struct got_error *
6961 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6963 const struct got_error *err;
6964 struct got_commit_object *commit;
6965 const struct got_object_id_queue *parent_ids;
6966 struct got_object_qid *pid;
6967 char *id_str = NULL;
6968 const char *logmsg = NULL;
6970 err = got_object_open_as_commit(&commit, repo, id);
6971 if (err)
6972 return err;
6974 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6975 if (err)
6976 goto done;
6978 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6979 parent_ids = got_object_commit_get_parent_ids(commit);
6980 fprintf(outfile, "numparents %d\n",
6981 got_object_commit_get_nparents(commit));
6982 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6983 char *pid_str;
6984 err = got_object_id_str(&pid_str, pid->id);
6985 if (err)
6986 goto done;
6987 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6988 free(pid_str);
6990 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6991 got_object_commit_get_author(commit),
6992 got_object_commit_get_author_time(commit));
6994 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6995 got_object_commit_get_author(commit),
6996 got_object_commit_get_committer_time(commit));
6998 logmsg = got_object_commit_get_logmsg_raw(commit);
6999 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7000 fprintf(outfile, "%s", logmsg);
7001 done:
7002 free(id_str);
7003 got_object_commit_close(commit);
7004 return err;
7007 static const struct got_error *
7008 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7010 const struct got_error *err;
7011 struct got_tag_object *tag;
7012 char *id_str = NULL;
7013 const char *tagmsg = NULL;
7015 err = got_object_open_as_tag(&tag, repo, id);
7016 if (err)
7017 return err;
7019 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7020 if (err)
7021 goto done;
7023 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7025 switch (got_object_tag_get_object_type(tag)) {
7026 case GOT_OBJ_TYPE_BLOB:
7027 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7028 GOT_OBJ_LABEL_BLOB);
7029 break;
7030 case GOT_OBJ_TYPE_TREE:
7031 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7032 GOT_OBJ_LABEL_TREE);
7033 break;
7034 case GOT_OBJ_TYPE_COMMIT:
7035 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7036 GOT_OBJ_LABEL_COMMIT);
7037 break;
7038 case GOT_OBJ_TYPE_TAG:
7039 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7040 GOT_OBJ_LABEL_TAG);
7041 break;
7042 default:
7043 break;
7046 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7047 got_object_tag_get_name(tag));
7049 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7050 got_object_tag_get_tagger(tag),
7051 got_object_tag_get_tagger_time(tag));
7053 tagmsg = got_object_tag_get_message(tag);
7054 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7055 fprintf(outfile, "%s", tagmsg);
7056 done:
7057 free(id_str);
7058 got_object_tag_close(tag);
7059 return err;
7062 static const struct got_error *
7063 cmd_cat(int argc, char *argv[])
7065 const struct got_error *error;
7066 struct got_repository *repo = NULL;
7067 struct got_worktree *worktree = NULL;
7068 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7069 const char *commit_id_str = NULL;
7070 struct got_object_id *id = NULL, *commit_id = NULL;
7071 int ch, obj_type, i, force_path = 0;
7073 #ifndef PROFILE
7074 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7075 NULL) == -1)
7076 err(1, "pledge");
7077 #endif
7079 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7080 switch (ch) {
7081 case 'c':
7082 commit_id_str = optarg;
7083 break;
7084 case 'r':
7085 repo_path = realpath(optarg, NULL);
7086 if (repo_path == NULL)
7087 return got_error_from_errno2("realpath",
7088 optarg);
7089 got_path_strip_trailing_slashes(repo_path);
7090 break;
7091 case 'P':
7092 force_path = 1;
7093 break;
7094 default:
7095 usage_cat();
7096 /* NOTREACHED */
7100 argc -= optind;
7101 argv += optind;
7103 cwd = getcwd(NULL, 0);
7104 if (cwd == NULL) {
7105 error = got_error_from_errno("getcwd");
7106 goto done;
7108 error = got_worktree_open(&worktree, cwd);
7109 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7110 goto done;
7111 if (worktree) {
7112 if (repo_path == NULL) {
7113 repo_path = strdup(
7114 got_worktree_get_repo_path(worktree));
7115 if (repo_path == NULL) {
7116 error = got_error_from_errno("strdup");
7117 goto done;
7122 if (repo_path == NULL) {
7123 repo_path = getcwd(NULL, 0);
7124 if (repo_path == NULL)
7125 return got_error_from_errno("getcwd");
7128 error = got_repo_open(&repo, repo_path, NULL);
7129 free(repo_path);
7130 if (error != NULL)
7131 goto done;
7133 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7134 if (error)
7135 goto done;
7137 if (commit_id_str == NULL)
7138 commit_id_str = GOT_REF_HEAD;
7139 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7140 if (error)
7141 goto done;
7143 for (i = 0; i < argc; i++) {
7144 if (force_path) {
7145 error = got_object_id_by_path(&id, repo, commit_id,
7146 argv[i]);
7147 if (error)
7148 break;
7149 } else {
7150 error = match_object_id(&id, &label, argv[i],
7151 GOT_OBJ_TYPE_ANY, 0, repo);
7152 if (error) {
7153 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7154 error->code != GOT_ERR_NOT_REF)
7155 break;
7156 error = got_object_id_by_path(&id, repo,
7157 commit_id, argv[i]);
7158 if (error)
7159 break;
7163 error = got_object_get_type(&obj_type, repo, id);
7164 if (error)
7165 break;
7167 switch (obj_type) {
7168 case GOT_OBJ_TYPE_BLOB:
7169 error = cat_blob(id, repo, stdout);
7170 break;
7171 case GOT_OBJ_TYPE_TREE:
7172 error = cat_tree(id, repo, stdout);
7173 break;
7174 case GOT_OBJ_TYPE_COMMIT:
7175 error = cat_commit(id, repo, stdout);
7176 break;
7177 case GOT_OBJ_TYPE_TAG:
7178 error = cat_tag(id, repo, stdout);
7179 break;
7180 default:
7181 error = got_error(GOT_ERR_OBJ_TYPE);
7182 break;
7184 if (error)
7185 break;
7186 free(label);
7187 label = NULL;
7188 free(id);
7189 id = NULL;
7192 done:
7193 free(label);
7194 free(id);
7195 free(commit_id);
7196 if (worktree)
7197 got_worktree_close(worktree);
7198 if (repo) {
7199 const struct got_error *repo_error;
7200 repo_error = got_repo_close(repo);
7201 if (error == NULL)
7202 error = repo_error;
7204 return error;