Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 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 <fcntl.h>
27 #include <limits.h>
28 #include <locale.h>
29 #include <ctype.h>
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <libgen.h>
36 #include <time.h>
37 #include <paths.h>
38 #include <regex.h>
39 #include <getopt.h>
41 #include "got_version.h"
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_path.h"
47 #include "got_cancel.h"
48 #include "got_worktree.h"
49 #include "got_diff.h"
50 #include "got_commit_graph.h"
51 #include "got_blame.h"
52 #include "got_privsep.h"
53 #include "got_opentemp.h"
55 #ifndef nitems
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 #endif
59 static volatile sig_atomic_t sigint_received;
60 static volatile sig_atomic_t sigpipe_received;
62 static void
63 catch_sigint(int signo)
64 {
65 sigint_received = 1;
66 }
68 static void
69 catch_sigpipe(int signo)
70 {
71 sigpipe_received = 1;
72 }
75 struct got_cmd {
76 const char *cmd_name;
77 const struct got_error *(*cmd_main)(int, char *[]);
78 void (*cmd_usage)(void);
79 const char *cmd_alias;
80 };
82 __dead static void usage(int);
83 __dead static void usage_init(void);
84 __dead static void usage_import(void);
85 __dead static void usage_checkout(void);
86 __dead static void usage_update(void);
87 __dead static void usage_log(void);
88 __dead static void usage_diff(void);
89 __dead static void usage_blame(void);
90 __dead static void usage_tree(void);
91 __dead static void usage_status(void);
92 __dead static void usage_ref(void);
93 __dead static void usage_branch(void);
94 __dead static void usage_tag(void);
95 __dead static void usage_add(void);
96 __dead static void usage_remove(void);
97 __dead static void usage_revert(void);
98 __dead static void usage_commit(void);
99 __dead static void usage_cherrypick(void);
100 __dead static void usage_backout(void);
101 __dead static void usage_rebase(void);
102 __dead static void usage_histedit(void);
103 __dead static void usage_integrate(void);
104 __dead static void usage_stage(void);
105 __dead static void usage_unstage(void);
106 __dead static void usage_cat(void);
108 static const struct got_error* cmd_init(int, char *[]);
109 static const struct got_error* cmd_import(int, char *[]);
110 static const struct got_error* cmd_checkout(int, char *[]);
111 static const struct got_error* cmd_update(int, char *[]);
112 static const struct got_error* cmd_log(int, char *[]);
113 static const struct got_error* cmd_diff(int, char *[]);
114 static const struct got_error* cmd_blame(int, char *[]);
115 static const struct got_error* cmd_tree(int, char *[]);
116 static const struct got_error* cmd_status(int, char *[]);
117 static const struct got_error* cmd_ref(int, char *[]);
118 static const struct got_error* cmd_branch(int, char *[]);
119 static const struct got_error* cmd_tag(int, char *[]);
120 static const struct got_error* cmd_add(int, char *[]);
121 static const struct got_error* cmd_remove(int, char *[]);
122 static const struct got_error* cmd_revert(int, char *[]);
123 static const struct got_error* cmd_commit(int, char *[]);
124 static const struct got_error* cmd_cherrypick(int, char *[]);
125 static const struct got_error* cmd_backout(int, char *[]);
126 static const struct got_error* cmd_rebase(int, char *[]);
127 static const struct got_error* cmd_histedit(int, char *[]);
128 static const struct got_error* cmd_integrate(int, char *[]);
129 static const struct got_error* cmd_stage(int, char *[]);
130 static const struct got_error* cmd_unstage(int, char *[]);
131 static const struct got_error* cmd_cat(int, char *[]);
133 static struct got_cmd got_commands[] = {
134 { "init", cmd_init, usage_init, "in" },
135 { "import", cmd_import, usage_import, "im" },
136 { "checkout", cmd_checkout, usage_checkout, "co" },
137 { "update", cmd_update, usage_update, "up" },
138 { "log", cmd_log, usage_log, "" },
139 { "diff", cmd_diff, usage_diff, "di" },
140 { "blame", cmd_blame, usage_blame, "bl" },
141 { "tree", cmd_tree, usage_tree, "tr" },
142 { "status", cmd_status, usage_status, "st" },
143 { "ref", cmd_ref, usage_ref, "" },
144 { "branch", cmd_branch, usage_branch, "br" },
145 { "tag", cmd_tag, usage_tag, "" },
146 { "add", cmd_add, usage_add, "" },
147 { "remove", cmd_remove, usage_remove, "rm" },
148 { "revert", cmd_revert, usage_revert, "rv" },
149 { "commit", cmd_commit, usage_commit, "ci" },
150 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
151 { "backout", cmd_backout, usage_backout, "bo" },
152 { "rebase", cmd_rebase, usage_rebase, "rb" },
153 { "histedit", cmd_histedit, usage_histedit, "he" },
154 { "integrate", cmd_integrate, usage_integrate,"ig" },
155 { "stage", cmd_stage, usage_stage, "sg" },
156 { "unstage", cmd_unstage, usage_unstage, "ug" },
157 { "cat", cmd_cat, usage_cat, "" },
158 };
160 static void
161 list_commands(void)
163 int i;
165 fprintf(stderr, "commands:");
166 for (i = 0; i < nitems(got_commands); i++) {
167 struct got_cmd *cmd = &got_commands[i];
168 fprintf(stderr, " %s", cmd->cmd_name);
170 fputc('\n', stderr);
173 int
174 main(int argc, char *argv[])
176 struct got_cmd *cmd;
177 unsigned int i;
178 int ch;
179 int hflag = 0, Vflag = 0;
180 static struct option longopts[] = {
181 { "version", no_argument, NULL, 'V' },
182 { NULL, 0, NULL, 0}
183 };
185 setlocale(LC_CTYPE, "");
187 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
188 switch (ch) {
189 case 'h':
190 hflag = 1;
191 break;
192 case 'V':
193 Vflag = 1;
194 break;
195 default:
196 usage(hflag);
197 /* NOTREACHED */
201 argc -= optind;
202 argv += optind;
203 optind = 0;
205 if (Vflag) {
206 got_version_print_str();
207 return 1;
210 if (argc <= 0)
211 usage(hflag);
213 signal(SIGINT, catch_sigint);
214 signal(SIGPIPE, catch_sigpipe);
216 for (i = 0; i < nitems(got_commands); i++) {
217 const struct got_error *error;
219 cmd = &got_commands[i];
221 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
222 strcmp(cmd->cmd_alias, argv[0]) != 0)
223 continue;
225 if (hflag)
226 got_commands[i].cmd_usage();
228 error = got_commands[i].cmd_main(argc, argv);
229 if (error && error->code != GOT_ERR_CANCELLED &&
230 error->code != GOT_ERR_PRIVSEP_EXIT &&
231 !(sigpipe_received &&
232 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
233 !(sigint_received &&
234 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
235 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
236 return 1;
239 return 0;
242 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
243 list_commands();
244 return 1;
247 __dead static void
248 usage(int hflag)
250 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
251 getprogname());
252 if (hflag)
253 list_commands();
254 exit(1);
257 static const struct got_error *
258 get_editor(char **abspath)
260 const struct got_error *err = NULL;
261 const char *editor;
263 *abspath = NULL;
265 editor = getenv("VISUAL");
266 if (editor == NULL)
267 editor = getenv("EDITOR");
269 if (editor) {
270 err = got_path_find_prog(abspath, editor);
271 if (err)
272 return err;
275 if (*abspath == NULL) {
276 *abspath = strdup("/bin/ed");
277 if (*abspath == NULL)
278 return got_error_from_errno("strdup");
281 return NULL;
284 static const struct got_error *
285 apply_unveil(const char *repo_path, int repo_read_only,
286 const char *worktree_path)
288 const struct got_error *err;
290 #ifdef PROFILE
291 if (unveil("gmon.out", "rwc") != 0)
292 return got_error_from_errno2("unveil", "gmon.out");
293 #endif
294 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
295 return got_error_from_errno2("unveil", repo_path);
297 if (worktree_path && unveil(worktree_path, "rwc") != 0)
298 return got_error_from_errno2("unveil", worktree_path);
300 if (unveil("/tmp", "rwc") != 0)
301 return got_error_from_errno2("unveil", "/tmp");
303 err = got_privsep_unveil_exec_helpers();
304 if (err != NULL)
305 return err;
307 if (unveil(NULL, NULL) != 0)
308 return got_error_from_errno("unveil");
310 return NULL;
313 __dead static void
314 usage_init(void)
316 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
317 exit(1);
320 static const struct got_error *
321 cmd_init(int argc, char *argv[])
323 const struct got_error *error = NULL;
324 char *repo_path = NULL;
325 int ch;
327 while ((ch = getopt(argc, argv, "")) != -1) {
328 switch (ch) {
329 default:
330 usage_init();
331 /* NOTREACHED */
335 argc -= optind;
336 argv += optind;
338 #ifndef PROFILE
339 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
340 err(1, "pledge");
341 #endif
342 if (argc != 1)
343 usage_init();
345 repo_path = strdup(argv[0]);
346 if (repo_path == NULL)
347 return got_error_from_errno("strdup");
349 got_path_strip_trailing_slashes(repo_path);
351 error = got_path_mkdir(repo_path);
352 if (error &&
353 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
354 goto done;
356 error = apply_unveil(repo_path, 0, NULL);
357 if (error)
358 goto done;
360 error = got_repo_init(repo_path);
361 done:
362 free(repo_path);
363 return error;
366 __dead static void
367 usage_import(void)
369 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
370 "[-r repository-path] [-I pattern] path\n", getprogname());
371 exit(1);
374 int
375 spawn_editor(const char *editor, const char *file)
377 pid_t pid;
378 sig_t sighup, sigint, sigquit;
379 int st = -1;
381 sighup = signal(SIGHUP, SIG_IGN);
382 sigint = signal(SIGINT, SIG_IGN);
383 sigquit = signal(SIGQUIT, SIG_IGN);
385 switch (pid = fork()) {
386 case -1:
387 goto doneediting;
388 case 0:
389 execl(editor, editor, file, (char *)NULL);
390 _exit(127);
393 while (waitpid(pid, &st, 0) == -1)
394 if (errno != EINTR)
395 break;
397 doneediting:
398 (void)signal(SIGHUP, sighup);
399 (void)signal(SIGINT, sigint);
400 (void)signal(SIGQUIT, sigquit);
402 if (!WIFEXITED(st)) {
403 errno = EINTR;
404 return -1;
407 return WEXITSTATUS(st);
410 static const struct got_error *
411 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
412 const char *initial_content)
414 const struct got_error *err = NULL;
415 char buf[1024];
416 struct stat st, st2;
417 FILE *fp;
418 int content_changed = 0;
419 size_t len;
421 *logmsg = NULL;
423 if (stat(logmsg_path, &st) == -1)
424 return got_error_from_errno2("stat", logmsg_path);
426 if (spawn_editor(editor, logmsg_path) == -1)
427 return got_error_from_errno("failed spawning editor");
429 if (stat(logmsg_path, &st2) == -1)
430 return got_error_from_errno("stat");
432 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
433 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
434 "no changes made to commit message, aborting");
436 *logmsg = malloc(st2.st_size + 1);
437 if (*logmsg == NULL)
438 return got_error_from_errno("malloc");
439 (*logmsg)[0] = '\0';
440 len = 0;
442 fp = fopen(logmsg_path, "r");
443 if (fp == NULL) {
444 err = got_error_from_errno("fopen");
445 goto done;
447 while (fgets(buf, sizeof(buf), fp) != NULL) {
448 if (!content_changed && strcmp(buf, initial_content) != 0)
449 content_changed = 1;
450 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
451 continue; /* remove comments and leading empty lines */
452 len = strlcat(*logmsg, buf, st2.st_size);
454 fclose(fp);
456 while (len > 0 && (*logmsg)[len - 1] == '\n') {
457 (*logmsg)[len - 1] = '\0';
458 len--;
461 if (len == 0 || !content_changed)
462 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
463 "commit message cannot be empty, aborting");
464 done:
465 if (err) {
466 free(*logmsg);
467 *logmsg = NULL;
469 return err;
472 static const struct got_error *
473 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
474 const char *path_dir, const char *branch_name)
476 char *initial_content = NULL;
477 const struct got_error *err = NULL;
478 int fd;
480 if (asprintf(&initial_content,
481 "\n# %s to be imported to branch %s\n", path_dir,
482 branch_name) == -1)
483 return got_error_from_errno("asprintf");
485 err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
486 if (err)
487 goto done;
489 dprintf(fd, initial_content);
490 close(fd);
492 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
493 done:
494 free(initial_content);
495 return err;
498 static const struct got_error *
499 import_progress(void *arg, const char *path)
501 printf("A %s\n", path);
502 return NULL;
505 static const struct got_error *
506 get_author(char **author, struct got_repository *repo)
508 const struct got_error *err = NULL;
509 const char *got_author, *name, *email;
511 *author = NULL;
513 name = got_repo_get_gitconfig_author_name(repo);
514 email = got_repo_get_gitconfig_author_email(repo);
515 if (name && email) {
516 if (asprintf(author, "%s <%s>", name, email) == -1)
517 return got_error_from_errno("asprintf");
518 return NULL;
521 got_author = getenv("GOT_AUTHOR");
522 if (got_author == NULL) {
523 name = got_repo_get_global_gitconfig_author_name(repo);
524 email = got_repo_get_global_gitconfig_author_email(repo);
525 if (name && email) {
526 if (asprintf(author, "%s <%s>", name, email) == -1)
527 return got_error_from_errno("asprintf");
528 return NULL;
530 /* TODO: Look up user in password database? */
531 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
534 *author = strdup(got_author);
535 if (*author == NULL)
536 return got_error_from_errno("strdup");
538 /*
539 * Really dumb email address check; we're only doing this to
540 * avoid git's object parser breaking on commits we create.
541 */
542 while (*got_author && *got_author != '<')
543 got_author++;
544 if (*got_author != '<') {
545 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
546 goto done;
548 while (*got_author && *got_author != '@')
549 got_author++;
550 if (*got_author != '@') {
551 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
552 goto done;
554 while (*got_author && *got_author != '>')
555 got_author++;
556 if (*got_author != '>')
557 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
558 done:
559 if (err) {
560 free(*author);
561 *author = NULL;
563 return err;
566 static const struct got_error *
567 get_gitconfig_path(char **gitconfig_path)
569 const char *homedir = getenv("HOME");
571 *gitconfig_path = NULL;
572 if (homedir) {
573 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
574 return got_error_from_errno("asprintf");
577 return NULL;
580 static const struct got_error *
581 cmd_import(int argc, char *argv[])
583 const struct got_error *error = NULL;
584 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
585 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
586 const char *branch_name = "main";
587 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
588 struct got_repository *repo = NULL;
589 struct got_reference *branch_ref = NULL, *head_ref = NULL;
590 struct got_object_id *new_commit_id = NULL;
591 int ch;
592 struct got_pathlist_head ignores;
593 struct got_pathlist_entry *pe;
594 int preserve_logmsg = 0;
596 TAILQ_INIT(&ignores);
598 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
599 switch (ch) {
600 case 'b':
601 branch_name = optarg;
602 break;
603 case 'm':
604 logmsg = strdup(optarg);
605 if (logmsg == NULL) {
606 error = got_error_from_errno("strdup");
607 goto done;
609 break;
610 case 'r':
611 repo_path = realpath(optarg, NULL);
612 if (repo_path == NULL) {
613 error = got_error_from_errno2("realpath",
614 optarg);
615 goto done;
617 break;
618 case 'I':
619 if (optarg[0] == '\0')
620 break;
621 error = got_pathlist_insert(&pe, &ignores, optarg,
622 NULL);
623 if (error)
624 goto done;
625 break;
626 default:
627 usage_import();
628 /* NOTREACHED */
632 argc -= optind;
633 argv += optind;
635 #ifndef PROFILE
636 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
637 "unveil",
638 NULL) == -1)
639 err(1, "pledge");
640 #endif
641 if (argc != 1)
642 usage_import();
644 if (repo_path == NULL) {
645 repo_path = getcwd(NULL, 0);
646 if (repo_path == NULL)
647 return got_error_from_errno("getcwd");
649 got_path_strip_trailing_slashes(repo_path);
650 error = get_gitconfig_path(&gitconfig_path);
651 if (error)
652 goto done;
653 error = got_repo_open(&repo, repo_path, gitconfig_path);
654 if (error)
655 goto done;
657 error = get_author(&author, repo);
658 if (error)
659 return error;
661 /*
662 * Don't let the user create a branch name with a leading '-'.
663 * While technically a valid reference name, this case is usually
664 * an unintended typo.
665 */
666 if (branch_name[0] == '-')
667 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
669 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
670 error = got_error_from_errno("asprintf");
671 goto done;
674 error = got_ref_open(&branch_ref, repo, refname, 0);
675 if (error) {
676 if (error->code != GOT_ERR_NOT_REF)
677 goto done;
678 } else {
679 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
680 "import target branch already exists");
681 goto done;
684 path_dir = realpath(argv[0], NULL);
685 if (path_dir == NULL) {
686 error = got_error_from_errno2("realpath", argv[0]);
687 goto done;
689 got_path_strip_trailing_slashes(path_dir);
691 /*
692 * unveil(2) traverses exec(2); if an editor is used we have
693 * to apply unveil after the log message has been written.
694 */
695 if (logmsg == NULL || strlen(logmsg) == 0) {
696 error = get_editor(&editor);
697 if (error)
698 goto done;
699 free(logmsg);
700 error = collect_import_msg(&logmsg, &logmsg_path, editor,
701 path_dir, refname);
702 if (error) {
703 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
704 logmsg_path != NULL)
705 preserve_logmsg = 1;
706 goto done;
710 if (unveil(path_dir, "r") != 0) {
711 error = got_error_from_errno2("unveil", path_dir);
712 if (logmsg_path)
713 preserve_logmsg = 1;
714 goto done;
717 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
718 if (error) {
719 if (logmsg_path)
720 preserve_logmsg = 1;
721 goto done;
724 error = got_repo_import(&new_commit_id, path_dir, logmsg,
725 author, &ignores, repo, import_progress, NULL);
726 if (error) {
727 if (logmsg_path)
728 preserve_logmsg = 1;
729 goto done;
732 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
733 if (error) {
734 if (logmsg_path)
735 preserve_logmsg = 1;
736 goto done;
739 error = got_ref_write(branch_ref, repo);
740 if (error) {
741 if (logmsg_path)
742 preserve_logmsg = 1;
743 goto done;
746 error = got_object_id_str(&id_str, new_commit_id);
747 if (error) {
748 if (logmsg_path)
749 preserve_logmsg = 1;
750 goto done;
753 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
754 if (error) {
755 if (error->code != GOT_ERR_NOT_REF) {
756 if (logmsg_path)
757 preserve_logmsg = 1;
758 goto done;
761 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
762 branch_ref);
763 if (error) {
764 if (logmsg_path)
765 preserve_logmsg = 1;
766 goto done;
769 error = got_ref_write(head_ref, repo);
770 if (error) {
771 if (logmsg_path)
772 preserve_logmsg = 1;
773 goto done;
777 printf("Created branch %s with commit %s\n",
778 got_ref_get_name(branch_ref), id_str);
779 done:
780 if (preserve_logmsg) {
781 fprintf(stderr, "%s: log message preserved in %s\n",
782 getprogname(), logmsg_path);
783 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
784 error = got_error_from_errno2("unlink", logmsg_path);
785 free(logmsg);
786 free(logmsg_path);
787 free(repo_path);
788 free(editor);
789 free(refname);
790 free(new_commit_id);
791 free(id_str);
792 free(author);
793 free(gitconfig_path);
794 if (branch_ref)
795 got_ref_close(branch_ref);
796 if (head_ref)
797 got_ref_close(head_ref);
798 return error;
801 __dead static void
802 usage_checkout(void)
804 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
805 "[-p prefix] repository-path [worktree-path]\n", getprogname());
806 exit(1);
809 static void
810 show_worktree_base_ref_warning(void)
812 fprintf(stderr, "%s: warning: could not create a reference "
813 "to the work tree's base commit; the commit could be "
814 "garbage-collected by Git; making the repository "
815 "writable and running 'got update' will prevent this\n",
816 getprogname());
819 struct got_checkout_progress_arg {
820 const char *worktree_path;
821 int had_base_commit_ref_error;
822 };
824 static const struct got_error *
825 checkout_progress(void *arg, unsigned char status, const char *path)
827 struct got_checkout_progress_arg *a = arg;
829 /* Base commit bump happens silently. */
830 if (status == GOT_STATUS_BUMP_BASE)
831 return NULL;
833 if (status == GOT_STATUS_BASE_REF_ERR) {
834 a->had_base_commit_ref_error = 1;
835 return NULL;
838 while (path[0] == '/')
839 path++;
841 printf("%c %s/%s\n", status, a->worktree_path, path);
842 return NULL;
845 static const struct got_error *
846 check_cancelled(void *arg)
848 if (sigint_received || sigpipe_received)
849 return got_error(GOT_ERR_CANCELLED);
850 return NULL;
853 static const struct got_error *
854 check_linear_ancestry(struct got_object_id *commit_id,
855 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
856 struct got_repository *repo)
858 const struct got_error *err = NULL;
859 struct got_object_id *yca_id;
861 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
862 commit_id, base_commit_id, repo, check_cancelled, NULL);
863 if (err)
864 return err;
866 if (yca_id == NULL)
867 return got_error(GOT_ERR_ANCESTRY);
869 /*
870 * Require a straight line of history between the target commit
871 * and the work tree's base commit.
873 * Non-linear situations such as this require a rebase:
875 * (commit) D F (base_commit)
876 * \ /
877 * C E
878 * \ /
879 * B (yca)
880 * |
881 * A
883 * 'got update' only handles linear cases:
884 * Update forwards in time: A (base/yca) - B - C - D (commit)
885 * Update backwards in time: D (base) - C - B - A (commit/yca)
886 */
887 if (allow_forwards_in_time_only) {
888 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
889 return got_error(GOT_ERR_ANCESTRY);
890 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
891 got_object_id_cmp(base_commit_id, yca_id) != 0)
892 return got_error(GOT_ERR_ANCESTRY);
894 free(yca_id);
895 return NULL;
898 static const struct got_error *
899 check_same_branch(struct got_object_id *commit_id,
900 struct got_reference *head_ref, struct got_object_id *yca_id,
901 struct got_repository *repo)
903 const struct got_error *err = NULL;
904 struct got_commit_graph *graph = NULL;
905 struct got_object_id *head_commit_id = NULL;
906 int is_same_branch = 0;
908 err = got_ref_resolve(&head_commit_id, repo, head_ref);
909 if (err)
910 goto done;
912 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
913 is_same_branch = 1;
914 goto done;
916 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
917 is_same_branch = 1;
918 goto done;
921 err = got_commit_graph_open(&graph, "/", 1);
922 if (err)
923 goto done;
925 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
926 check_cancelled, NULL);
927 if (err)
928 goto done;
930 for (;;) {
931 struct got_object_id *id;
932 err = got_commit_graph_iter_next(&id, graph, repo,
933 check_cancelled, NULL);
934 if (err) {
935 if (err->code == GOT_ERR_ITER_COMPLETED)
936 err = NULL;
937 break;
940 if (id) {
941 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
942 break;
943 if (got_object_id_cmp(id, commit_id) == 0) {
944 is_same_branch = 1;
945 break;
949 done:
950 if (graph)
951 got_commit_graph_close(graph);
952 free(head_commit_id);
953 if (!err && !is_same_branch)
954 err = got_error(GOT_ERR_ANCESTRY);
955 return err;
958 static const struct got_error *
959 resolve_commit_arg(struct got_object_id **commit_id,
960 const char *commit_id_arg, struct got_repository *repo)
962 const struct got_error *err;
963 struct got_reference *ref;
964 struct got_tag_object *tag;
966 err = got_repo_object_match_tag(&tag, commit_id_arg,
967 GOT_OBJ_TYPE_COMMIT, repo);
968 if (err == NULL) {
969 *commit_id = got_object_id_dup(
970 got_object_tag_get_object_id(tag));
971 if (*commit_id == NULL)
972 err = got_error_from_errno("got_object_id_dup");
973 got_object_tag_close(tag);
974 return err;
975 } else if (err->code != GOT_ERR_NO_OBJ)
976 return err;
978 err = got_ref_open(&ref, repo, commit_id_arg, 0);
979 if (err == NULL) {
980 err = got_ref_resolve(commit_id, repo, ref);
981 got_ref_close(ref);
982 } else {
983 if (err->code != GOT_ERR_NOT_REF)
984 return err;
985 err = got_repo_match_object_id_prefix(commit_id,
986 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
988 return err;
991 static const struct got_error *
992 cmd_checkout(int argc, char *argv[])
994 const struct got_error *error = NULL;
995 struct got_repository *repo = NULL;
996 struct got_reference *head_ref = NULL;
997 struct got_worktree *worktree = NULL;
998 char *repo_path = NULL;
999 char *worktree_path = NULL;
1000 const char *path_prefix = "";
1001 const char *branch_name = GOT_REF_HEAD;
1002 char *commit_id_str = NULL;
1003 int ch, same_path_prefix, allow_nonempty = 0;
1004 struct got_pathlist_head paths;
1005 struct got_checkout_progress_arg cpa;
1007 TAILQ_INIT(&paths);
1009 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1010 switch (ch) {
1011 case 'b':
1012 branch_name = optarg;
1013 break;
1014 case 'c':
1015 commit_id_str = strdup(optarg);
1016 if (commit_id_str == NULL)
1017 return got_error_from_errno("strdup");
1018 break;
1019 case 'E':
1020 allow_nonempty = 1;
1021 break;
1022 case 'p':
1023 path_prefix = optarg;
1024 break;
1025 default:
1026 usage_checkout();
1027 /* NOTREACHED */
1031 argc -= optind;
1032 argv += optind;
1034 #ifndef PROFILE
1035 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1036 "unveil", NULL) == -1)
1037 err(1, "pledge");
1038 #endif
1039 if (argc == 1) {
1040 char *cwd, *base, *dotgit;
1041 repo_path = realpath(argv[0], NULL);
1042 if (repo_path == NULL)
1043 return got_error_from_errno2("realpath", argv[0]);
1044 cwd = getcwd(NULL, 0);
1045 if (cwd == NULL) {
1046 error = got_error_from_errno("getcwd");
1047 goto done;
1049 if (path_prefix[0]) {
1050 base = basename(path_prefix);
1051 if (base == NULL) {
1052 error = got_error_from_errno2("basename",
1053 path_prefix);
1054 goto done;
1056 } else {
1057 base = basename(repo_path);
1058 if (base == NULL) {
1059 error = got_error_from_errno2("basename",
1060 repo_path);
1061 goto done;
1064 dotgit = strstr(base, ".git");
1065 if (dotgit)
1066 *dotgit = '\0';
1067 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1068 error = got_error_from_errno("asprintf");
1069 free(cwd);
1070 goto done;
1072 free(cwd);
1073 } else if (argc == 2) {
1074 repo_path = realpath(argv[0], NULL);
1075 if (repo_path == NULL) {
1076 error = got_error_from_errno2("realpath", argv[0]);
1077 goto done;
1079 worktree_path = realpath(argv[1], NULL);
1080 if (worktree_path == NULL) {
1081 if (errno != ENOENT) {
1082 error = got_error_from_errno2("realpath",
1083 argv[1]);
1084 goto done;
1086 worktree_path = strdup(argv[1]);
1087 if (worktree_path == NULL) {
1088 error = got_error_from_errno("strdup");
1089 goto done;
1092 } else
1093 usage_checkout();
1095 got_path_strip_trailing_slashes(repo_path);
1096 got_path_strip_trailing_slashes(worktree_path);
1098 error = got_repo_open(&repo, repo_path, NULL);
1099 if (error != NULL)
1100 goto done;
1102 /* Pre-create work tree path for unveil(2) */
1103 error = got_path_mkdir(worktree_path);
1104 if (error) {
1105 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1106 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1107 goto done;
1108 if (!allow_nonempty &&
1109 !got_path_dir_is_empty(worktree_path)) {
1110 error = got_error_path(worktree_path,
1111 GOT_ERR_DIR_NOT_EMPTY);
1112 goto done;
1116 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1117 if (error)
1118 goto done;
1120 error = got_ref_open(&head_ref, repo, branch_name, 0);
1121 if (error != NULL)
1122 goto done;
1124 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1125 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1126 goto done;
1128 error = got_worktree_open(&worktree, worktree_path);
1129 if (error != NULL)
1130 goto done;
1132 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1133 path_prefix);
1134 if (error != NULL)
1135 goto done;
1136 if (!same_path_prefix) {
1137 error = got_error(GOT_ERR_PATH_PREFIX);
1138 goto done;
1141 if (commit_id_str) {
1142 struct got_object_id *commit_id;
1143 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1144 if (error)
1145 goto done;
1146 error = check_linear_ancestry(commit_id,
1147 got_worktree_get_base_commit_id(worktree), 0, repo);
1148 if (error != NULL) {
1149 free(commit_id);
1150 goto done;
1152 error = check_same_branch(commit_id, head_ref, NULL, repo);
1153 if (error)
1154 goto done;
1155 error = got_worktree_set_base_commit_id(worktree, repo,
1156 commit_id);
1157 free(commit_id);
1158 if (error)
1159 goto done;
1162 error = got_pathlist_append(&paths, "", NULL);
1163 if (error)
1164 goto done;
1165 cpa.worktree_path = worktree_path;
1166 cpa.had_base_commit_ref_error = 0;
1167 error = got_worktree_checkout_files(worktree, &paths, repo,
1168 checkout_progress, &cpa, check_cancelled, NULL);
1169 if (error != NULL)
1170 goto done;
1172 printf("Now shut up and hack\n");
1173 if (cpa.had_base_commit_ref_error)
1174 show_worktree_base_ref_warning();
1175 done:
1176 got_pathlist_free(&paths);
1177 free(commit_id_str);
1178 free(repo_path);
1179 free(worktree_path);
1180 return error;
1183 __dead static void
1184 usage_update(void)
1186 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1187 getprogname());
1188 exit(1);
1191 static const struct got_error *
1192 update_progress(void *arg, unsigned char status, const char *path)
1194 int *did_something = arg;
1196 if (status == GOT_STATUS_EXISTS ||
1197 status == GOT_STATUS_BASE_REF_ERR)
1198 return NULL;
1200 *did_something = 1;
1202 /* Base commit bump happens silently. */
1203 if (status == GOT_STATUS_BUMP_BASE)
1204 return NULL;
1206 while (path[0] == '/')
1207 path++;
1208 printf("%c %s\n", status, path);
1209 return NULL;
1212 static const struct got_error *
1213 switch_head_ref(struct got_reference *head_ref,
1214 struct got_object_id *commit_id, struct got_worktree *worktree,
1215 struct got_repository *repo)
1217 const struct got_error *err = NULL;
1218 char *base_id_str;
1219 int ref_has_moved = 0;
1221 /* Trivial case: switching between two different references. */
1222 if (strcmp(got_ref_get_name(head_ref),
1223 got_worktree_get_head_ref_name(worktree)) != 0) {
1224 printf("Switching work tree from %s to %s\n",
1225 got_worktree_get_head_ref_name(worktree),
1226 got_ref_get_name(head_ref));
1227 return got_worktree_set_head_ref(worktree, head_ref);
1230 err = check_linear_ancestry(commit_id,
1231 got_worktree_get_base_commit_id(worktree), 0, repo);
1232 if (err) {
1233 if (err->code != GOT_ERR_ANCESTRY)
1234 return err;
1235 ref_has_moved = 1;
1237 if (!ref_has_moved)
1238 return NULL;
1240 /* Switching to a rebased branch with the same reference name. */
1241 err = got_object_id_str(&base_id_str,
1242 got_worktree_get_base_commit_id(worktree));
1243 if (err)
1244 return err;
1245 printf("Reference %s now points at a different branch\n",
1246 got_worktree_get_head_ref_name(worktree));
1247 printf("Switching work tree from %s to %s\n", base_id_str,
1248 got_worktree_get_head_ref_name(worktree));
1249 return NULL;
1252 static const struct got_error *
1253 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1255 const struct got_error *err;
1256 int in_progress;
1258 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1259 if (err)
1260 return err;
1261 if (in_progress)
1262 return got_error(GOT_ERR_REBASING);
1264 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1265 if (err)
1266 return err;
1267 if (in_progress)
1268 return got_error(GOT_ERR_HISTEDIT_BUSY);
1270 return NULL;
1273 static const struct got_error *
1274 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1275 char *argv[], struct got_worktree *worktree)
1277 const struct got_error *err = NULL;
1278 char *path;
1279 int i;
1281 if (argc == 0) {
1282 path = strdup("");
1283 if (path == NULL)
1284 return got_error_from_errno("strdup");
1285 return got_pathlist_append(paths, path, NULL);
1288 for (i = 0; i < argc; i++) {
1289 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1290 if (err)
1291 break;
1292 err = got_pathlist_append(paths, path, NULL);
1293 if (err) {
1294 free(path);
1295 break;
1299 return err;
1302 static const struct got_error *
1303 cmd_update(int argc, char *argv[])
1305 const struct got_error *error = NULL;
1306 struct got_repository *repo = NULL;
1307 struct got_worktree *worktree = NULL;
1308 char *worktree_path = NULL;
1309 struct got_object_id *commit_id = NULL;
1310 char *commit_id_str = NULL;
1311 const char *branch_name = NULL;
1312 struct got_reference *head_ref = NULL;
1313 struct got_pathlist_head paths;
1314 struct got_pathlist_entry *pe;
1315 int ch, did_something = 0;
1317 TAILQ_INIT(&paths);
1319 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1320 switch (ch) {
1321 case 'b':
1322 branch_name = optarg;
1323 break;
1324 case 'c':
1325 commit_id_str = strdup(optarg);
1326 if (commit_id_str == NULL)
1327 return got_error_from_errno("strdup");
1328 break;
1329 default:
1330 usage_update();
1331 /* NOTREACHED */
1335 argc -= optind;
1336 argv += optind;
1338 #ifndef PROFILE
1339 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1340 "unveil", NULL) == -1)
1341 err(1, "pledge");
1342 #endif
1343 worktree_path = getcwd(NULL, 0);
1344 if (worktree_path == NULL) {
1345 error = got_error_from_errno("getcwd");
1346 goto done;
1348 error = got_worktree_open(&worktree, worktree_path);
1349 if (error)
1350 goto done;
1352 error = check_rebase_or_histedit_in_progress(worktree);
1353 if (error)
1354 goto done;
1356 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1357 NULL);
1358 if (error != NULL)
1359 goto done;
1361 error = apply_unveil(got_repo_get_path(repo), 0,
1362 got_worktree_get_root_path(worktree));
1363 if (error)
1364 goto done;
1366 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1367 if (error)
1368 goto done;
1370 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1371 got_worktree_get_head_ref_name(worktree), 0);
1372 if (error != NULL)
1373 goto done;
1374 if (commit_id_str == NULL) {
1375 error = got_ref_resolve(&commit_id, repo, head_ref);
1376 if (error != NULL)
1377 goto done;
1378 error = got_object_id_str(&commit_id_str, commit_id);
1379 if (error != NULL)
1380 goto done;
1381 } else {
1382 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1383 free(commit_id_str);
1384 commit_id_str = NULL;
1385 if (error)
1386 goto done;
1387 error = got_object_id_str(&commit_id_str, commit_id);
1388 if (error)
1389 goto done;
1392 if (branch_name) {
1393 struct got_object_id *head_commit_id;
1394 TAILQ_FOREACH(pe, &paths, entry) {
1395 if (pe->path_len == 0)
1396 continue;
1397 error = got_error_msg(GOT_ERR_BAD_PATH,
1398 "switching between branches requires that "
1399 "the entire work tree gets updated");
1400 goto done;
1402 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1403 if (error)
1404 goto done;
1405 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1406 repo);
1407 free(head_commit_id);
1408 if (error != NULL)
1409 goto done;
1410 error = check_same_branch(commit_id, head_ref, NULL, repo);
1411 if (error)
1412 goto done;
1413 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1414 if (error)
1415 goto done;
1416 } else {
1417 error = check_linear_ancestry(commit_id,
1418 got_worktree_get_base_commit_id(worktree), 0, repo);
1419 if (error != NULL) {
1420 if (error->code == GOT_ERR_ANCESTRY)
1421 error = got_error(GOT_ERR_BRANCH_MOVED);
1422 goto done;
1424 error = check_same_branch(commit_id, head_ref, NULL, repo);
1425 if (error)
1426 goto done;
1429 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1430 commit_id) != 0) {
1431 error = got_worktree_set_base_commit_id(worktree, repo,
1432 commit_id);
1433 if (error)
1434 goto done;
1437 error = got_worktree_checkout_files(worktree, &paths, repo,
1438 update_progress, &did_something, check_cancelled, NULL);
1439 if (error != NULL)
1440 goto done;
1442 if (did_something)
1443 printf("Updated to commit %s\n", commit_id_str);
1444 else
1445 printf("Already up-to-date\n");
1446 done:
1447 free(worktree_path);
1448 TAILQ_FOREACH(pe, &paths, entry)
1449 free((char *)pe->path);
1450 got_pathlist_free(&paths);
1451 free(commit_id);
1452 free(commit_id_str);
1453 return error;
1456 static const struct got_error *
1457 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1458 const char *path, int diff_context, int ignore_whitespace,
1459 struct got_repository *repo)
1461 const struct got_error *err = NULL;
1462 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1464 if (blob_id1) {
1465 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1466 if (err)
1467 goto done;
1470 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1471 if (err)
1472 goto done;
1474 while (path[0] == '/')
1475 path++;
1476 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1477 ignore_whitespace, stdout);
1478 done:
1479 if (blob1)
1480 got_object_blob_close(blob1);
1481 got_object_blob_close(blob2);
1482 return err;
1485 static const struct got_error *
1486 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1487 const char *path, int diff_context, int ignore_whitespace,
1488 struct got_repository *repo)
1490 const struct got_error *err = NULL;
1491 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1492 struct got_diff_blob_output_unidiff_arg arg;
1494 if (tree_id1) {
1495 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1496 if (err)
1497 goto done;
1500 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1501 if (err)
1502 goto done;
1504 arg.diff_context = diff_context;
1505 arg.ignore_whitespace = ignore_whitespace;
1506 arg.outfile = stdout;
1507 while (path[0] == '/')
1508 path++;
1509 err = got_diff_tree(tree1, tree2, path, path, repo,
1510 got_diff_blob_output_unidiff, &arg, 1);
1511 done:
1512 if (tree1)
1513 got_object_tree_close(tree1);
1514 if (tree2)
1515 got_object_tree_close(tree2);
1516 return err;
1519 static const struct got_error *
1520 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1521 const char *path, int diff_context, struct got_repository *repo)
1523 const struct got_error *err = NULL;
1524 struct got_commit_object *pcommit = NULL;
1525 char *id_str1 = NULL, *id_str2 = NULL;
1526 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1527 struct got_object_qid *qid;
1529 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1530 if (qid != NULL) {
1531 err = got_object_open_as_commit(&pcommit, repo,
1532 qid->id);
1533 if (err)
1534 return err;
1537 if (path && path[0] != '\0') {
1538 int obj_type;
1539 err = got_object_id_by_path(&obj_id2, repo, id, path);
1540 if (err)
1541 goto done;
1542 err = got_object_id_str(&id_str2, obj_id2);
1543 if (err) {
1544 free(obj_id2);
1545 goto done;
1547 if (pcommit) {
1548 err = got_object_id_by_path(&obj_id1, repo,
1549 qid->id, path);
1550 if (err) {
1551 free(obj_id2);
1552 goto done;
1554 err = got_object_id_str(&id_str1, obj_id1);
1555 if (err) {
1556 free(obj_id2);
1557 goto done;
1560 err = got_object_get_type(&obj_type, repo, obj_id2);
1561 if (err) {
1562 free(obj_id2);
1563 goto done;
1565 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1566 switch (obj_type) {
1567 case GOT_OBJ_TYPE_BLOB:
1568 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1569 0, repo);
1570 break;
1571 case GOT_OBJ_TYPE_TREE:
1572 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1573 0, repo);
1574 break;
1575 default:
1576 err = got_error(GOT_ERR_OBJ_TYPE);
1577 break;
1579 free(obj_id1);
1580 free(obj_id2);
1581 } else {
1582 obj_id2 = got_object_commit_get_tree_id(commit);
1583 err = got_object_id_str(&id_str2, obj_id2);
1584 if (err)
1585 goto done;
1586 obj_id1 = got_object_commit_get_tree_id(pcommit);
1587 err = got_object_id_str(&id_str1, obj_id1);
1588 if (err)
1589 goto done;
1590 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1591 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1593 done:
1594 free(id_str1);
1595 free(id_str2);
1596 if (pcommit)
1597 got_object_commit_close(pcommit);
1598 return err;
1601 static char *
1602 get_datestr(time_t *time, char *datebuf)
1604 struct tm mytm, *tm;
1605 char *p, *s;
1607 tm = gmtime_r(time, &mytm);
1608 if (tm == NULL)
1609 return NULL;
1610 s = asctime_r(tm, datebuf);
1611 if (s == NULL)
1612 return NULL;
1613 p = strchr(s, '\n');
1614 if (p)
1615 *p = '\0';
1616 return s;
1619 static const struct got_error *
1620 match_logmsg(int *have_match, struct got_object_id *id,
1621 struct got_commit_object *commit, regex_t *regex)
1623 const struct got_error *err = NULL;
1624 regmatch_t regmatch;
1625 char *id_str = NULL, *logmsg = NULL;
1627 *have_match = 0;
1629 err = got_object_id_str(&id_str, id);
1630 if (err)
1631 return err;
1633 err = got_object_commit_get_logmsg(&logmsg, commit);
1634 if (err)
1635 goto done;
1637 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1638 *have_match = 1;
1639 done:
1640 free(id_str);
1641 free(logmsg);
1642 return err;
1645 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1647 static const struct got_error *
1648 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1649 struct got_repository *repo, const char *path, int show_patch,
1650 int diff_context, struct got_reflist_head *refs)
1652 const struct got_error *err = NULL;
1653 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1654 char datebuf[26];
1655 time_t committer_time;
1656 const char *author, *committer;
1657 char *refs_str = NULL;
1658 struct got_reflist_entry *re;
1660 SIMPLEQ_FOREACH(re, refs, entry) {
1661 char *s;
1662 const char *name;
1663 struct got_tag_object *tag = NULL;
1664 int cmp;
1666 name = got_ref_get_name(re->ref);
1667 if (strcmp(name, GOT_REF_HEAD) == 0)
1668 continue;
1669 if (strncmp(name, "refs/", 5) == 0)
1670 name += 5;
1671 if (strncmp(name, "got/", 4) == 0)
1672 continue;
1673 if (strncmp(name, "heads/", 6) == 0)
1674 name += 6;
1675 if (strncmp(name, "remotes/", 8) == 0)
1676 name += 8;
1677 if (strncmp(name, "tags/", 5) == 0) {
1678 err = got_object_open_as_tag(&tag, repo, re->id);
1679 if (err) {
1680 if (err->code != GOT_ERR_OBJ_TYPE)
1681 return err;
1682 /* Ref points at something other than a tag. */
1683 err = NULL;
1684 tag = NULL;
1687 cmp = got_object_id_cmp(tag ?
1688 got_object_tag_get_object_id(tag) : re->id, id);
1689 if (tag)
1690 got_object_tag_close(tag);
1691 if (cmp != 0)
1692 continue;
1693 s = refs_str;
1694 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1695 name) == -1) {
1696 err = got_error_from_errno("asprintf");
1697 free(s);
1698 return err;
1700 free(s);
1702 err = got_object_id_str(&id_str, id);
1703 if (err)
1704 return err;
1706 printf(GOT_COMMIT_SEP_STR);
1707 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1708 refs_str ? refs_str : "", refs_str ? ")" : "");
1709 free(id_str);
1710 id_str = NULL;
1711 free(refs_str);
1712 refs_str = NULL;
1713 printf("from: %s\n", got_object_commit_get_author(commit));
1714 committer_time = got_object_commit_get_committer_time(commit);
1715 datestr = get_datestr(&committer_time, datebuf);
1716 if (datestr)
1717 printf("date: %s UTC\n", datestr);
1718 author = got_object_commit_get_author(commit);
1719 committer = got_object_commit_get_committer(commit);
1720 if (strcmp(author, committer) != 0)
1721 printf("via: %s\n", committer);
1722 if (got_object_commit_get_nparents(commit) > 1) {
1723 const struct got_object_id_queue *parent_ids;
1724 struct got_object_qid *qid;
1725 int n = 1;
1726 parent_ids = got_object_commit_get_parent_ids(commit);
1727 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1728 err = got_object_id_str(&id_str, qid->id);
1729 if (err)
1730 return err;
1731 printf("parent %d: %s\n", n++, id_str);
1732 free(id_str);
1736 err = got_object_commit_get_logmsg(&logmsg0, commit);
1737 if (err)
1738 return err;
1740 logmsg = logmsg0;
1741 do {
1742 line = strsep(&logmsg, "\n");
1743 if (line)
1744 printf(" %s\n", line);
1745 } while (line);
1746 free(logmsg0);
1748 if (show_patch) {
1749 err = print_patch(commit, id, path, diff_context, repo);
1750 if (err == 0)
1751 printf("\n");
1754 if (fflush(stdout) != 0 && err == NULL)
1755 err = got_error_from_errno("fflush");
1756 return err;
1759 static const struct got_error *
1760 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1761 const char *path, int show_patch, const char *search_pattern,
1762 int diff_context, int limit, int first_parent_traversal,
1763 struct got_reflist_head *refs)
1765 const struct got_error *err;
1766 struct got_commit_graph *graph;
1767 regex_t regex;
1768 int have_match;
1770 if (search_pattern &&
1771 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1772 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1774 err = got_commit_graph_open(&graph, path, first_parent_traversal);
1775 if (err)
1776 return err;
1777 err = got_commit_graph_iter_start(graph, root_id, repo,
1778 check_cancelled, NULL);
1779 if (err)
1780 goto done;
1781 for (;;) {
1782 struct got_commit_object *commit;
1783 struct got_object_id *id;
1785 if (sigint_received || sigpipe_received)
1786 break;
1788 err = got_commit_graph_iter_next(&id, graph, repo,
1789 check_cancelled, NULL);
1790 if (err) {
1791 if (err->code == GOT_ERR_ITER_COMPLETED)
1792 err = NULL;
1793 break;
1795 if (id == NULL)
1796 break;
1798 err = got_object_open_as_commit(&commit, repo, id);
1799 if (err)
1800 break;
1802 if (search_pattern) {
1803 err = match_logmsg(&have_match, id, commit, &regex);
1804 if (err) {
1805 got_object_commit_close(commit);
1806 break;
1808 if (have_match == 0) {
1809 got_object_commit_close(commit);
1810 continue;
1814 err = print_commit(commit, id, repo, path, show_patch,
1815 diff_context, refs);
1816 got_object_commit_close(commit);
1817 if (err || (limit && --limit == 0))
1818 break;
1820 done:
1821 if (search_pattern)
1822 regfree(&regex);
1823 got_commit_graph_close(graph);
1824 return err;
1827 __dead static void
1828 usage_log(void)
1830 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1831 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1832 exit(1);
1835 static int
1836 get_default_log_limit(void)
1838 const char *got_default_log_limit;
1839 long long n;
1840 const char *errstr;
1842 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1843 if (got_default_log_limit == NULL)
1844 return 0;
1845 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1846 if (errstr != NULL)
1847 return 0;
1848 return n;
1851 static const struct got_error *
1852 cmd_log(int argc, char *argv[])
1854 const struct got_error *error;
1855 struct got_repository *repo = NULL;
1856 struct got_worktree *worktree = NULL;
1857 struct got_commit_object *commit = NULL;
1858 struct got_object_id *id = NULL;
1859 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1860 const char *start_commit = NULL, *search_pattern = NULL;
1861 int diff_context = -1, ch;
1862 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1863 const char *errstr;
1864 struct got_reflist_head refs;
1866 SIMPLEQ_INIT(&refs);
1868 #ifndef PROFILE
1869 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1870 NULL)
1871 == -1)
1872 err(1, "pledge");
1873 #endif
1875 limit = get_default_log_limit();
1877 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1878 switch (ch) {
1879 case 'p':
1880 show_patch = 1;
1881 break;
1882 case 'c':
1883 start_commit = optarg;
1884 break;
1885 case 'C':
1886 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1887 &errstr);
1888 if (errstr != NULL)
1889 err(1, "-C option %s", errstr);
1890 break;
1891 case 'l':
1892 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1893 if (errstr != NULL)
1894 err(1, "-l option %s", errstr);
1895 break;
1896 case 'f':
1897 first_parent_traversal = 1;
1898 break;
1899 case 'r':
1900 repo_path = realpath(optarg, NULL);
1901 if (repo_path == NULL)
1902 return got_error_from_errno2("realpath",
1903 optarg);
1904 got_path_strip_trailing_slashes(repo_path);
1905 break;
1906 case 's':
1907 search_pattern = optarg;
1908 break;
1909 default:
1910 usage_log();
1911 /* NOTREACHED */
1915 argc -= optind;
1916 argv += optind;
1918 if (diff_context == -1)
1919 diff_context = 3;
1920 else if (!show_patch)
1921 errx(1, "-C reguires -p");
1923 cwd = getcwd(NULL, 0);
1924 if (cwd == NULL) {
1925 error = got_error_from_errno("getcwd");
1926 goto done;
1929 error = got_worktree_open(&worktree, cwd);
1930 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1931 goto done;
1932 error = NULL;
1934 if (argc == 0) {
1935 path = strdup("");
1936 if (path == NULL) {
1937 error = got_error_from_errno("strdup");
1938 goto done;
1940 } else if (argc == 1) {
1941 if (worktree) {
1942 error = got_worktree_resolve_path(&path, worktree,
1943 argv[0]);
1944 if (error)
1945 goto done;
1946 } else {
1947 path = strdup(argv[0]);
1948 if (path == NULL) {
1949 error = got_error_from_errno("strdup");
1950 goto done;
1953 } else
1954 usage_log();
1956 if (repo_path == NULL) {
1957 repo_path = worktree ?
1958 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1960 if (repo_path == NULL) {
1961 error = got_error_from_errno("strdup");
1962 goto done;
1965 error = got_repo_open(&repo, repo_path, NULL);
1966 if (error != NULL)
1967 goto done;
1969 error = apply_unveil(got_repo_get_path(repo), 1,
1970 worktree ? got_worktree_get_root_path(worktree) : NULL);
1971 if (error)
1972 goto done;
1974 if (start_commit == NULL) {
1975 struct got_reference *head_ref;
1976 error = got_ref_open(&head_ref, repo,
1977 worktree ? got_worktree_get_head_ref_name(worktree)
1978 : GOT_REF_HEAD, 0);
1979 if (error != NULL)
1980 return error;
1981 error = got_ref_resolve(&id, repo, head_ref);
1982 got_ref_close(head_ref);
1983 if (error != NULL)
1984 return error;
1985 error = got_object_open_as_commit(&commit, repo, id);
1986 } else {
1987 struct got_reference *ref;
1988 error = got_ref_open(&ref, repo, start_commit, 0);
1989 if (error == NULL) {
1990 int obj_type;
1991 error = got_ref_resolve(&id, repo, ref);
1992 got_ref_close(ref);
1993 if (error != NULL)
1994 goto done;
1995 error = got_object_get_type(&obj_type, repo, id);
1996 if (error != NULL)
1997 goto done;
1998 if (obj_type == GOT_OBJ_TYPE_TAG) {
1999 struct got_tag_object *tag;
2000 error = got_object_open_as_tag(&tag, repo, id);
2001 if (error != NULL)
2002 goto done;
2003 if (got_object_tag_get_object_type(tag) !=
2004 GOT_OBJ_TYPE_COMMIT) {
2005 got_object_tag_close(tag);
2006 error = got_error(GOT_ERR_OBJ_TYPE);
2007 goto done;
2009 free(id);
2010 id = got_object_id_dup(
2011 got_object_tag_get_object_id(tag));
2012 if (id == NULL)
2013 error = got_error_from_errno(
2014 "got_object_id_dup");
2015 got_object_tag_close(tag);
2016 if (error)
2017 goto done;
2018 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2019 error = got_error(GOT_ERR_OBJ_TYPE);
2020 goto done;
2022 error = got_object_open_as_commit(&commit, repo, id);
2023 if (error != NULL)
2024 goto done;
2026 if (commit == NULL) {
2027 error = got_repo_match_object_id_prefix(&id,
2028 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2029 if (error != NULL)
2030 return error;
2033 if (error != NULL)
2034 goto done;
2036 if (worktree) {
2037 const char *prefix = got_worktree_get_path_prefix(worktree);
2038 char *p;
2039 if (asprintf(&p, "%s%s%s", prefix,
2040 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2041 error = got_error_from_errno("asprintf");
2042 goto done;
2044 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2045 free(p);
2046 } else
2047 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2048 if (error != NULL)
2049 goto done;
2050 if (in_repo_path) {
2051 free(path);
2052 path = in_repo_path;
2055 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2056 if (error)
2057 goto done;
2059 error = print_commits(id, repo, path, show_patch, search_pattern,
2060 diff_context, limit, first_parent_traversal, &refs);
2061 done:
2062 free(path);
2063 free(repo_path);
2064 free(cwd);
2065 free(id);
2066 if (worktree)
2067 got_worktree_close(worktree);
2068 if (repo) {
2069 const struct got_error *repo_error;
2070 repo_error = got_repo_close(repo);
2071 if (error == NULL)
2072 error = repo_error;
2074 got_ref_list_free(&refs);
2075 return error;
2078 __dead static void
2079 usage_diff(void)
2081 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2082 "[-w] [object1 object2 | path]\n", getprogname());
2083 exit(1);
2086 struct print_diff_arg {
2087 struct got_repository *repo;
2088 struct got_worktree *worktree;
2089 int diff_context;
2090 const char *id_str;
2091 int header_shown;
2092 int diff_staged;
2093 int ignore_whitespace;
2096 static const struct got_error *
2097 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2098 const char *path, struct got_object_id *blob_id,
2099 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2100 int dirfd, const char *de_name)
2102 struct print_diff_arg *a = arg;
2103 const struct got_error *err = NULL;
2104 struct got_blob_object *blob1 = NULL;
2105 int fd = -1;
2106 FILE *f2 = NULL;
2107 char *abspath = NULL, *label1 = NULL;
2108 struct stat sb;
2110 if (a->diff_staged) {
2111 if (staged_status != GOT_STATUS_MODIFY &&
2112 staged_status != GOT_STATUS_ADD &&
2113 staged_status != GOT_STATUS_DELETE)
2114 return NULL;
2115 } else {
2116 if (staged_status == GOT_STATUS_DELETE)
2117 return NULL;
2118 if (status == GOT_STATUS_NONEXISTENT)
2119 return got_error_set_errno(ENOENT, path);
2120 if (status != GOT_STATUS_MODIFY &&
2121 status != GOT_STATUS_ADD &&
2122 status != GOT_STATUS_DELETE &&
2123 status != GOT_STATUS_CONFLICT)
2124 return NULL;
2127 if (!a->header_shown) {
2128 printf("diff %s %s%s\n", a->id_str,
2129 got_worktree_get_root_path(a->worktree),
2130 a->diff_staged ? " (staged changes)" : "");
2131 a->header_shown = 1;
2134 if (a->diff_staged) {
2135 const char *label1 = NULL, *label2 = NULL;
2136 switch (staged_status) {
2137 case GOT_STATUS_MODIFY:
2138 label1 = path;
2139 label2 = path;
2140 break;
2141 case GOT_STATUS_ADD:
2142 label2 = path;
2143 break;
2144 case GOT_STATUS_DELETE:
2145 label1 = path;
2146 break;
2147 default:
2148 return got_error(GOT_ERR_FILE_STATUS);
2150 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2151 label1, label2, a->diff_context, a->ignore_whitespace,
2152 a->repo, stdout);
2155 if (staged_status == GOT_STATUS_ADD ||
2156 staged_status == GOT_STATUS_MODIFY) {
2157 char *id_str;
2158 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2159 8192);
2160 if (err)
2161 goto done;
2162 err = got_object_id_str(&id_str, staged_blob_id);
2163 if (err)
2164 goto done;
2165 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2166 err = got_error_from_errno("asprintf");
2167 free(id_str);
2168 goto done;
2170 free(id_str);
2171 } else if (status != GOT_STATUS_ADD) {
2172 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2173 if (err)
2174 goto done;
2177 if (status != GOT_STATUS_DELETE) {
2178 if (asprintf(&abspath, "%s/%s",
2179 got_worktree_get_root_path(a->worktree), path) == -1) {
2180 err = got_error_from_errno("asprintf");
2181 goto done;
2184 if (dirfd != -1) {
2185 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2186 if (fd == -1) {
2187 err = got_error_from_errno2("openat", abspath);
2188 goto done;
2190 } else {
2191 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2192 if (fd == -1) {
2193 err = got_error_from_errno2("open", abspath);
2194 goto done;
2197 if (fstat(fd, &sb) == -1) {
2198 err = got_error_from_errno2("fstat", abspath);
2199 goto done;
2201 f2 = fdopen(fd, "r");
2202 if (f2 == NULL) {
2203 err = got_error_from_errno2("fdopen", abspath);
2204 goto done;
2206 fd = -1;
2207 } else
2208 sb.st_size = 0;
2210 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2211 a->diff_context, a->ignore_whitespace, stdout);
2212 done:
2213 if (blob1)
2214 got_object_blob_close(blob1);
2215 if (f2 && fclose(f2) == EOF && err == NULL)
2216 err = got_error_from_errno("fclose");
2217 if (fd != -1 && close(fd) == -1 && err == NULL)
2218 err = got_error_from_errno("close");
2219 free(abspath);
2220 return err;
2223 static const struct got_error *
2224 match_object_id(struct got_object_id **id, char **label,
2225 const char *id_str, int obj_type, int resolve_tags,
2226 struct got_repository *repo)
2228 const struct got_error *err;
2229 struct got_tag_object *tag;
2230 struct got_reference *ref = NULL;
2232 *id = NULL;
2233 *label = NULL;
2235 if (resolve_tags) {
2236 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2237 repo);
2238 if (err == NULL) {
2239 *id = got_object_id_dup(
2240 got_object_tag_get_object_id(tag));
2241 if (*id == NULL)
2242 err = got_error_from_errno("got_object_id_dup");
2243 else if (asprintf(label, "refs/tags/%s",
2244 got_object_tag_get_name(tag)) == -1) {
2245 err = got_error_from_errno("asprintf");
2246 free(*id);
2247 *id = NULL;
2249 got_object_tag_close(tag);
2250 return err;
2251 } else if (err->code != GOT_ERR_OBJ_TYPE &&
2252 err->code != GOT_ERR_NO_OBJ)
2253 return err;
2256 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2257 if (err) {
2258 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2259 return err;
2260 err = got_ref_open(&ref, repo, id_str, 0);
2261 if (err != NULL)
2262 goto done;
2263 *label = strdup(got_ref_get_name(ref));
2264 if (*label == NULL) {
2265 err = got_error_from_errno("strdup");
2266 goto done;
2268 err = got_ref_resolve(id, repo, ref);
2269 } else {
2270 err = got_object_id_str(label, *id);
2271 if (*label == NULL) {
2272 err = got_error_from_errno("strdup");
2273 goto done;
2276 done:
2277 if (ref)
2278 got_ref_close(ref);
2279 return err;
2283 static const struct got_error *
2284 cmd_diff(int argc, char *argv[])
2286 const struct got_error *error;
2287 struct got_repository *repo = NULL;
2288 struct got_worktree *worktree = NULL;
2289 char *cwd = NULL, *repo_path = NULL;
2290 struct got_object_id *id1 = NULL, *id2 = NULL;
2291 const char *id_str1 = NULL, *id_str2 = NULL;
2292 char *label1 = NULL, *label2 = NULL;
2293 int type1, type2;
2294 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2295 const char *errstr;
2296 char *path = NULL;
2298 #ifndef PROFILE
2299 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2300 NULL) == -1)
2301 err(1, "pledge");
2302 #endif
2304 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2305 switch (ch) {
2306 case 'C':
2307 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2308 &errstr);
2309 if (errstr != NULL)
2310 err(1, "-C option %s", errstr);
2311 break;
2312 case 'r':
2313 repo_path = realpath(optarg, NULL);
2314 if (repo_path == NULL)
2315 return got_error_from_errno2("realpath",
2316 optarg);
2317 got_path_strip_trailing_slashes(repo_path);
2318 break;
2319 case 's':
2320 diff_staged = 1;
2321 break;
2322 case 'w':
2323 ignore_whitespace = 1;
2324 break;
2325 default:
2326 usage_diff();
2327 /* NOTREACHED */
2331 argc -= optind;
2332 argv += optind;
2334 cwd = getcwd(NULL, 0);
2335 if (cwd == NULL) {
2336 error = got_error_from_errno("getcwd");
2337 goto done;
2339 error = got_worktree_open(&worktree, cwd);
2340 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2341 goto done;
2342 if (argc <= 1) {
2343 if (worktree == NULL) {
2344 error = got_error(GOT_ERR_NOT_WORKTREE);
2345 goto done;
2347 if (repo_path)
2348 errx(1,
2349 "-r option can't be used when diffing a work tree");
2350 repo_path = strdup(got_worktree_get_repo_path(worktree));
2351 if (repo_path == NULL) {
2352 error = got_error_from_errno("strdup");
2353 goto done;
2355 if (argc == 1) {
2356 error = got_worktree_resolve_path(&path, worktree,
2357 argv[0]);
2358 if (error)
2359 goto done;
2360 } else {
2361 path = strdup("");
2362 if (path == NULL) {
2363 error = got_error_from_errno("strdup");
2364 goto done;
2367 } else if (argc == 2) {
2368 if (diff_staged)
2369 errx(1, "-s option can't be used when diffing "
2370 "objects in repository");
2371 id_str1 = argv[0];
2372 id_str2 = argv[1];
2373 if (worktree && repo_path == NULL) {
2374 repo_path =
2375 strdup(got_worktree_get_repo_path(worktree));
2376 if (repo_path == NULL) {
2377 error = got_error_from_errno("strdup");
2378 goto done;
2381 } else
2382 usage_diff();
2384 if (repo_path == NULL) {
2385 repo_path = getcwd(NULL, 0);
2386 if (repo_path == NULL)
2387 return got_error_from_errno("getcwd");
2390 error = got_repo_open(&repo, repo_path, NULL);
2391 free(repo_path);
2392 if (error != NULL)
2393 goto done;
2395 error = apply_unveil(got_repo_get_path(repo), 1,
2396 worktree ? got_worktree_get_root_path(worktree) : NULL);
2397 if (error)
2398 goto done;
2400 if (argc <= 1) {
2401 struct print_diff_arg arg;
2402 struct got_pathlist_head paths;
2403 char *id_str;
2405 TAILQ_INIT(&paths);
2407 error = got_object_id_str(&id_str,
2408 got_worktree_get_base_commit_id(worktree));
2409 if (error)
2410 goto done;
2411 arg.repo = repo;
2412 arg.worktree = worktree;
2413 arg.diff_context = diff_context;
2414 arg.id_str = id_str;
2415 arg.header_shown = 0;
2416 arg.diff_staged = diff_staged;
2417 arg.ignore_whitespace = ignore_whitespace;
2419 error = got_pathlist_append(&paths, path, NULL);
2420 if (error)
2421 goto done;
2423 error = got_worktree_status(worktree, &paths, repo, print_diff,
2424 &arg, check_cancelled, NULL);
2425 free(id_str);
2426 got_pathlist_free(&paths);
2427 goto done;
2430 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2431 repo);
2432 if (error)
2433 goto done;
2435 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2436 repo);
2437 if (error)
2438 goto done;
2440 error = got_object_get_type(&type1, repo, id1);
2441 if (error)
2442 goto done;
2444 error = got_object_get_type(&type2, repo, id2);
2445 if (error)
2446 goto done;
2448 if (type1 != type2) {
2449 error = got_error(GOT_ERR_OBJ_TYPE);
2450 goto done;
2453 switch (type1) {
2454 case GOT_OBJ_TYPE_BLOB:
2455 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2456 diff_context, ignore_whitespace, repo, stdout);
2457 break;
2458 case GOT_OBJ_TYPE_TREE:
2459 error = got_diff_objects_as_trees(id1, id2, "", "",
2460 diff_context, ignore_whitespace, repo, stdout);
2461 break;
2462 case GOT_OBJ_TYPE_COMMIT:
2463 printf("diff %s %s\n", label1, label2);
2464 error = got_diff_objects_as_commits(id1, id2, diff_context,
2465 ignore_whitespace, repo, stdout);
2466 break;
2467 default:
2468 error = got_error(GOT_ERR_OBJ_TYPE);
2470 done:
2471 free(label1);
2472 free(label2);
2473 free(id1);
2474 free(id2);
2475 free(path);
2476 if (worktree)
2477 got_worktree_close(worktree);
2478 if (repo) {
2479 const struct got_error *repo_error;
2480 repo_error = got_repo_close(repo);
2481 if (error == NULL)
2482 error = repo_error;
2484 return error;
2487 __dead static void
2488 usage_blame(void)
2490 fprintf(stderr,
2491 "usage: %s blame [-c commit] [-r repository-path] path\n",
2492 getprogname());
2493 exit(1);
2496 struct blame_line {
2497 int annotated;
2498 char *id_str;
2499 char *committer;
2500 char datebuf[11]; /* YYYY-MM-DD + NUL */
2503 struct blame_cb_args {
2504 struct blame_line *lines;
2505 int nlines;
2506 int nlines_prec;
2507 int lineno_cur;
2508 off_t *line_offsets;
2509 FILE *f;
2510 struct got_repository *repo;
2513 static const struct got_error *
2514 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2516 const struct got_error *err = NULL;
2517 struct blame_cb_args *a = arg;
2518 struct blame_line *bline;
2519 char *line = NULL;
2520 size_t linesize = 0;
2521 struct got_commit_object *commit = NULL;
2522 off_t offset;
2523 struct tm tm;
2524 time_t committer_time;
2526 if (nlines != a->nlines ||
2527 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2528 return got_error(GOT_ERR_RANGE);
2530 if (sigint_received)
2531 return got_error(GOT_ERR_ITER_COMPLETED);
2533 if (lineno == -1)
2534 return NULL; /* no change in this commit */
2536 /* Annotate this line. */
2537 bline = &a->lines[lineno - 1];
2538 if (bline->annotated)
2539 return NULL;
2540 err = got_object_id_str(&bline->id_str, id);
2541 if (err)
2542 return err;
2544 err = got_object_open_as_commit(&commit, a->repo, id);
2545 if (err)
2546 goto done;
2548 bline->committer = strdup(got_object_commit_get_committer(commit));
2549 if (bline->committer == NULL) {
2550 err = got_error_from_errno("strdup");
2551 goto done;
2554 committer_time = got_object_commit_get_committer_time(commit);
2555 if (localtime_r(&committer_time, &tm) == NULL)
2556 return got_error_from_errno("localtime_r");
2557 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2558 &tm) >= sizeof(bline->datebuf)) {
2559 err = got_error(GOT_ERR_NO_SPACE);
2560 goto done;
2562 bline->annotated = 1;
2564 /* Print lines annotated so far. */
2565 bline = &a->lines[a->lineno_cur - 1];
2566 if (!bline->annotated)
2567 goto done;
2569 offset = a->line_offsets[a->lineno_cur - 1];
2570 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2571 err = got_error_from_errno("fseeko");
2572 goto done;
2575 while (bline->annotated) {
2576 char *smallerthan, *at, *nl, *committer;
2577 size_t len;
2579 if (getline(&line, &linesize, a->f) == -1) {
2580 if (ferror(a->f))
2581 err = got_error_from_errno("getline");
2582 break;
2585 committer = bline->committer;
2586 smallerthan = strchr(committer, '<');
2587 if (smallerthan && smallerthan[1] != '\0')
2588 committer = smallerthan + 1;
2589 at = strchr(committer, '@');
2590 if (at)
2591 *at = '\0';
2592 len = strlen(committer);
2593 if (len >= 9)
2594 committer[8] = '\0';
2596 nl = strchr(line, '\n');
2597 if (nl)
2598 *nl = '\0';
2599 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2600 bline->id_str, bline->datebuf, committer, line);
2602 a->lineno_cur++;
2603 bline = &a->lines[a->lineno_cur - 1];
2605 done:
2606 if (commit)
2607 got_object_commit_close(commit);
2608 free(line);
2609 return err;
2612 static const struct got_error *
2613 cmd_blame(int argc, char *argv[])
2615 const struct got_error *error;
2616 struct got_repository *repo = NULL;
2617 struct got_worktree *worktree = NULL;
2618 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2619 struct got_object_id *obj_id = NULL;
2620 struct got_object_id *commit_id = NULL;
2621 struct got_blob_object *blob = NULL;
2622 char *commit_id_str = NULL;
2623 struct blame_cb_args bca;
2624 int ch, obj_type, i;
2625 size_t filesize;
2627 memset(&bca, 0, sizeof(bca));
2629 #ifndef PROFILE
2630 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2631 NULL) == -1)
2632 err(1, "pledge");
2633 #endif
2635 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2636 switch (ch) {
2637 case 'c':
2638 commit_id_str = optarg;
2639 break;
2640 case 'r':
2641 repo_path = realpath(optarg, NULL);
2642 if (repo_path == NULL)
2643 return got_error_from_errno2("realpath",
2644 optarg);
2645 got_path_strip_trailing_slashes(repo_path);
2646 break;
2647 default:
2648 usage_blame();
2649 /* NOTREACHED */
2653 argc -= optind;
2654 argv += optind;
2656 if (argc == 1)
2657 path = argv[0];
2658 else
2659 usage_blame();
2661 cwd = getcwd(NULL, 0);
2662 if (cwd == NULL) {
2663 error = got_error_from_errno("getcwd");
2664 goto done;
2666 if (repo_path == NULL) {
2667 error = got_worktree_open(&worktree, cwd);
2668 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2669 goto done;
2670 else
2671 error = NULL;
2672 if (worktree) {
2673 repo_path =
2674 strdup(got_worktree_get_repo_path(worktree));
2675 if (repo_path == NULL)
2676 error = got_error_from_errno("strdup");
2677 if (error)
2678 goto done;
2679 } else {
2680 repo_path = strdup(cwd);
2681 if (repo_path == NULL) {
2682 error = got_error_from_errno("strdup");
2683 goto done;
2688 error = got_repo_open(&repo, repo_path, NULL);
2689 if (error != NULL)
2690 goto done;
2692 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2693 if (error)
2694 goto done;
2696 if (worktree) {
2697 const char *prefix = got_worktree_get_path_prefix(worktree);
2698 char *p, *worktree_subdir = cwd +
2699 strlen(got_worktree_get_root_path(worktree));
2700 if (asprintf(&p, "%s%s%s%s%s",
2701 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2702 worktree_subdir, worktree_subdir[0] ? "/" : "",
2703 path) == -1) {
2704 error = got_error_from_errno("asprintf");
2705 goto done;
2707 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2708 free(p);
2709 } else {
2710 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2712 if (error)
2713 goto done;
2715 if (commit_id_str == NULL) {
2716 struct got_reference *head_ref;
2717 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2718 if (error != NULL)
2719 goto done;
2720 error = got_ref_resolve(&commit_id, repo, head_ref);
2721 got_ref_close(head_ref);
2722 if (error != NULL)
2723 goto done;
2724 } else {
2725 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2726 if (error)
2727 goto done;
2730 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2731 if (error)
2732 goto done;
2733 if (obj_id == NULL) {
2734 error = got_error(GOT_ERR_NO_OBJ);
2735 goto done;
2738 error = got_object_get_type(&obj_type, repo, obj_id);
2739 if (error)
2740 goto done;
2742 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2743 error = got_error(GOT_ERR_OBJ_TYPE);
2744 goto done;
2747 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2748 if (error)
2749 goto done;
2750 bca.f = got_opentemp();
2751 if (bca.f == NULL) {
2752 error = got_error_from_errno("got_opentemp");
2753 goto done;
2755 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2756 &bca.line_offsets, bca.f, blob);
2757 if (error || bca.nlines == 0)
2758 goto done;
2760 /* Don't include \n at EOF in the blame line count. */
2761 if (bca.line_offsets[bca.nlines - 1] == filesize)
2762 bca.nlines--;
2764 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2765 if (bca.lines == NULL) {
2766 error = got_error_from_errno("calloc");
2767 goto done;
2769 bca.lineno_cur = 1;
2770 bca.nlines_prec = 0;
2771 i = bca.nlines;
2772 while (i > 0) {
2773 i /= 10;
2774 bca.nlines_prec++;
2776 bca.repo = repo;
2778 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2779 check_cancelled, NULL);
2780 done:
2781 free(in_repo_path);
2782 free(repo_path);
2783 free(cwd);
2784 free(commit_id);
2785 free(obj_id);
2786 if (blob)
2787 got_object_blob_close(blob);
2788 if (worktree)
2789 got_worktree_close(worktree);
2790 if (repo) {
2791 const struct got_error *repo_error;
2792 repo_error = got_repo_close(repo);
2793 if (error == NULL)
2794 error = repo_error;
2796 if (bca.lines) {
2797 for (i = 0; i < bca.nlines; i++) {
2798 struct blame_line *bline = &bca.lines[i];
2799 free(bline->id_str);
2800 free(bline->committer);
2802 free(bca.lines);
2804 free(bca.line_offsets);
2805 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2806 error = got_error_from_errno("fclose");
2807 return error;
2810 __dead static void
2811 usage_tree(void)
2813 fprintf(stderr,
2814 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2815 getprogname());
2816 exit(1);
2819 static void
2820 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2821 const char *root_path)
2823 int is_root_path = (strcmp(path, root_path) == 0);
2824 const char *modestr = "";
2825 mode_t mode = got_tree_entry_get_mode(te);
2827 path += strlen(root_path);
2828 while (path[0] == '/')
2829 path++;
2831 if (got_object_tree_entry_is_submodule(te))
2832 modestr = "$";
2833 else if (S_ISLNK(mode))
2834 modestr = "@";
2835 else if (S_ISDIR(mode))
2836 modestr = "/";
2837 else if (mode & S_IXUSR)
2838 modestr = "*";
2840 printf("%s%s%s%s%s\n", id ? id : "", path,
2841 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2844 static const struct got_error *
2845 print_tree(const char *path, struct got_object_id *commit_id,
2846 int show_ids, int recurse, const char *root_path,
2847 struct got_repository *repo)
2849 const struct got_error *err = NULL;
2850 struct got_object_id *tree_id = NULL;
2851 struct got_tree_object *tree = NULL;
2852 int nentries, i;
2854 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2855 if (err)
2856 goto done;
2858 err = got_object_open_as_tree(&tree, repo, tree_id);
2859 if (err)
2860 goto done;
2861 nentries = got_object_tree_get_nentries(tree);
2862 for (i = 0; i < nentries; i++) {
2863 struct got_tree_entry *te;
2864 char *id = NULL;
2866 if (sigint_received || sigpipe_received)
2867 break;
2869 te = got_object_tree_get_entry(tree, i);
2870 if (show_ids) {
2871 char *id_str;
2872 err = got_object_id_str(&id_str,
2873 got_tree_entry_get_id(te));
2874 if (err)
2875 goto done;
2876 if (asprintf(&id, "%s ", id_str) == -1) {
2877 err = got_error_from_errno("asprintf");
2878 free(id_str);
2879 goto done;
2881 free(id_str);
2883 print_entry(te, id, path, root_path);
2884 free(id);
2886 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2887 char *child_path;
2888 if (asprintf(&child_path, "%s%s%s", path,
2889 path[0] == '/' && path[1] == '\0' ? "" : "/",
2890 got_tree_entry_get_name(te)) == -1) {
2891 err = got_error_from_errno("asprintf");
2892 goto done;
2894 err = print_tree(child_path, commit_id, show_ids, 1,
2895 root_path, repo);
2896 free(child_path);
2897 if (err)
2898 goto done;
2901 done:
2902 if (tree)
2903 got_object_tree_close(tree);
2904 free(tree_id);
2905 return err;
2908 static const struct got_error *
2909 cmd_tree(int argc, char *argv[])
2911 const struct got_error *error;
2912 struct got_repository *repo = NULL;
2913 struct got_worktree *worktree = NULL;
2914 const char *path;
2915 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2916 struct got_object_id *commit_id = NULL;
2917 char *commit_id_str = NULL;
2918 int show_ids = 0, recurse = 0;
2919 int ch;
2921 #ifndef PROFILE
2922 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2923 NULL) == -1)
2924 err(1, "pledge");
2925 #endif
2927 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2928 switch (ch) {
2929 case 'c':
2930 commit_id_str = optarg;
2931 break;
2932 case 'r':
2933 repo_path = realpath(optarg, NULL);
2934 if (repo_path == NULL)
2935 return got_error_from_errno2("realpath",
2936 optarg);
2937 got_path_strip_trailing_slashes(repo_path);
2938 break;
2939 case 'i':
2940 show_ids = 1;
2941 break;
2942 case 'R':
2943 recurse = 1;
2944 break;
2945 default:
2946 usage_tree();
2947 /* NOTREACHED */
2951 argc -= optind;
2952 argv += optind;
2954 if (argc == 1)
2955 path = argv[0];
2956 else if (argc > 1)
2957 usage_tree();
2958 else
2959 path = NULL;
2961 cwd = getcwd(NULL, 0);
2962 if (cwd == NULL) {
2963 error = got_error_from_errno("getcwd");
2964 goto done;
2966 if (repo_path == NULL) {
2967 error = got_worktree_open(&worktree, cwd);
2968 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2969 goto done;
2970 else
2971 error = NULL;
2972 if (worktree) {
2973 repo_path =
2974 strdup(got_worktree_get_repo_path(worktree));
2975 if (repo_path == NULL)
2976 error = got_error_from_errno("strdup");
2977 if (error)
2978 goto done;
2979 } else {
2980 repo_path = strdup(cwd);
2981 if (repo_path == NULL) {
2982 error = got_error_from_errno("strdup");
2983 goto done;
2988 error = got_repo_open(&repo, repo_path, NULL);
2989 if (error != NULL)
2990 goto done;
2992 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2993 if (error)
2994 goto done;
2996 if (path == NULL) {
2997 if (worktree) {
2998 char *p, *worktree_subdir = cwd +
2999 strlen(got_worktree_get_root_path(worktree));
3000 if (asprintf(&p, "%s/%s",
3001 got_worktree_get_path_prefix(worktree),
3002 worktree_subdir) == -1) {
3003 error = got_error_from_errno("asprintf");
3004 goto done;
3006 error = got_repo_map_path(&in_repo_path, repo, p, 1);
3007 free(p);
3008 if (error)
3009 goto done;
3010 } else
3011 path = "/";
3013 if (in_repo_path == NULL) {
3014 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3015 if (error != NULL)
3016 goto done;
3019 if (commit_id_str == NULL) {
3020 struct got_reference *head_ref;
3021 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3022 if (error != NULL)
3023 goto done;
3024 error = got_ref_resolve(&commit_id, repo, head_ref);
3025 got_ref_close(head_ref);
3026 if (error != NULL)
3027 goto done;
3028 } else {
3029 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
3030 if (error)
3031 goto done;
3034 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3035 in_repo_path, repo);
3036 done:
3037 free(in_repo_path);
3038 free(repo_path);
3039 free(cwd);
3040 free(commit_id);
3041 if (worktree)
3042 got_worktree_close(worktree);
3043 if (repo) {
3044 const struct got_error *repo_error;
3045 repo_error = got_repo_close(repo);
3046 if (error == NULL)
3047 error = repo_error;
3049 return error;
3052 __dead static void
3053 usage_status(void)
3055 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3056 exit(1);
3059 static const struct got_error *
3060 print_status(void *arg, unsigned char status, unsigned char staged_status,
3061 const char *path, struct got_object_id *blob_id,
3062 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3063 int dirfd, const char *de_name)
3065 if (status == staged_status && (status == GOT_STATUS_DELETE))
3066 status = GOT_STATUS_NO_CHANGE;
3067 printf("%c%c %s\n", status, staged_status, path);
3068 return NULL;
3071 static const struct got_error *
3072 cmd_status(int argc, char *argv[])
3074 const struct got_error *error = NULL;
3075 struct got_repository *repo = NULL;
3076 struct got_worktree *worktree = NULL;
3077 char *cwd = NULL;
3078 struct got_pathlist_head paths;
3079 struct got_pathlist_entry *pe;
3080 int ch;
3082 TAILQ_INIT(&paths);
3084 while ((ch = getopt(argc, argv, "")) != -1) {
3085 switch (ch) {
3086 default:
3087 usage_status();
3088 /* NOTREACHED */
3092 argc -= optind;
3093 argv += optind;
3095 #ifndef PROFILE
3096 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3097 NULL) == -1)
3098 err(1, "pledge");
3099 #endif
3100 cwd = getcwd(NULL, 0);
3101 if (cwd == NULL) {
3102 error = got_error_from_errno("getcwd");
3103 goto done;
3106 error = got_worktree_open(&worktree, cwd);
3107 if (error != NULL)
3108 goto done;
3110 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3111 NULL);
3112 if (error != NULL)
3113 goto done;
3115 error = apply_unveil(got_repo_get_path(repo), 1,
3116 got_worktree_get_root_path(worktree));
3117 if (error)
3118 goto done;
3120 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3121 if (error)
3122 goto done;
3124 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3125 check_cancelled, NULL);
3126 done:
3127 TAILQ_FOREACH(pe, &paths, entry)
3128 free((char *)pe->path);
3129 got_pathlist_free(&paths);
3130 free(cwd);
3131 return error;
3134 __dead static void
3135 usage_ref(void)
3137 fprintf(stderr,
3138 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3139 getprogname());
3140 exit(1);
3143 static const struct got_error *
3144 list_refs(struct got_repository *repo)
3146 static const struct got_error *err = NULL;
3147 struct got_reflist_head refs;
3148 struct got_reflist_entry *re;
3150 SIMPLEQ_INIT(&refs);
3151 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3152 if (err)
3153 return err;
3155 SIMPLEQ_FOREACH(re, &refs, entry) {
3156 char *refstr;
3157 refstr = got_ref_to_str(re->ref);
3158 if (refstr == NULL)
3159 return got_error_from_errno("got_ref_to_str");
3160 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3161 free(refstr);
3164 got_ref_list_free(&refs);
3165 return NULL;
3168 static const struct got_error *
3169 delete_ref(struct got_repository *repo, const char *refname)
3171 const struct got_error *err = NULL;
3172 struct got_reference *ref;
3174 err = got_ref_open(&ref, repo, refname, 0);
3175 if (err)
3176 return err;
3178 err = got_ref_delete(ref, repo);
3179 got_ref_close(ref);
3180 return err;
3183 static const struct got_error *
3184 add_ref(struct got_repository *repo, const char *refname, const char *target)
3186 const struct got_error *err = NULL;
3187 struct got_object_id *id;
3188 struct got_reference *ref = NULL;
3191 * Don't let the user create a reference name with a leading '-'.
3192 * While technically a valid reference name, this case is usually
3193 * an unintended typo.
3195 if (refname[0] == '-')
3196 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3198 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3199 repo);
3200 if (err) {
3201 struct got_reference *target_ref;
3203 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3204 return err;
3205 err = got_ref_open(&target_ref, repo, target, 0);
3206 if (err)
3207 return err;
3208 err = got_ref_resolve(&id, repo, target_ref);
3209 got_ref_close(target_ref);
3210 if (err)
3211 return err;
3214 err = got_ref_alloc(&ref, refname, id);
3215 if (err)
3216 goto done;
3218 err = got_ref_write(ref, repo);
3219 done:
3220 if (ref)
3221 got_ref_close(ref);
3222 free(id);
3223 return err;
3226 static const struct got_error *
3227 add_symref(struct got_repository *repo, const char *refname, const char *target)
3229 const struct got_error *err = NULL;
3230 struct got_reference *ref = NULL;
3231 struct got_reference *target_ref = NULL;
3234 * Don't let the user create a reference name with a leading '-'.
3235 * While technically a valid reference name, this case is usually
3236 * an unintended typo.
3238 if (refname[0] == '-')
3239 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3241 err = got_ref_open(&target_ref, repo, target, 0);
3242 if (err)
3243 return err;
3245 err = got_ref_alloc_symref(&ref, refname, target_ref);
3246 if (err)
3247 goto done;
3249 err = got_ref_write(ref, repo);
3250 done:
3251 if (target_ref)
3252 got_ref_close(target_ref);
3253 if (ref)
3254 got_ref_close(ref);
3255 return err;
3258 static const struct got_error *
3259 cmd_ref(int argc, char *argv[])
3261 const struct got_error *error = NULL;
3262 struct got_repository *repo = NULL;
3263 struct got_worktree *worktree = NULL;
3264 char *cwd = NULL, *repo_path = NULL;
3265 int ch, do_list = 0, create_symref = 0;
3266 const char *delref = NULL;
3268 /* TODO: Add -s option for adding symbolic references. */
3269 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3270 switch (ch) {
3271 case 'd':
3272 delref = optarg;
3273 break;
3274 case 'r':
3275 repo_path = realpath(optarg, NULL);
3276 if (repo_path == NULL)
3277 return got_error_from_errno2("realpath",
3278 optarg);
3279 got_path_strip_trailing_slashes(repo_path);
3280 break;
3281 case 'l':
3282 do_list = 1;
3283 break;
3284 case 's':
3285 create_symref = 1;
3286 break;
3287 default:
3288 usage_ref();
3289 /* NOTREACHED */
3293 if (do_list && delref)
3294 errx(1, "-l and -d options are mutually exclusive\n");
3296 argc -= optind;
3297 argv += optind;
3299 if (do_list || delref) {
3300 if (create_symref)
3301 errx(1, "-s option cannot be used together with the "
3302 "-l or -d options");
3303 if (argc > 0)
3304 usage_ref();
3305 } else if (argc != 2)
3306 usage_ref();
3308 #ifndef PROFILE
3309 if (do_list) {
3310 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3311 NULL) == -1)
3312 err(1, "pledge");
3313 } else {
3314 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3315 "sendfd unveil", NULL) == -1)
3316 err(1, "pledge");
3318 #endif
3319 cwd = getcwd(NULL, 0);
3320 if (cwd == NULL) {
3321 error = got_error_from_errno("getcwd");
3322 goto done;
3325 if (repo_path == NULL) {
3326 error = got_worktree_open(&worktree, cwd);
3327 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3328 goto done;
3329 else
3330 error = NULL;
3331 if (worktree) {
3332 repo_path =
3333 strdup(got_worktree_get_repo_path(worktree));
3334 if (repo_path == NULL)
3335 error = got_error_from_errno("strdup");
3336 if (error)
3337 goto done;
3338 } else {
3339 repo_path = strdup(cwd);
3340 if (repo_path == NULL) {
3341 error = got_error_from_errno("strdup");
3342 goto done;
3347 error = got_repo_open(&repo, repo_path, NULL);
3348 if (error != NULL)
3349 goto done;
3351 error = apply_unveil(got_repo_get_path(repo), do_list,
3352 worktree ? got_worktree_get_root_path(worktree) : NULL);
3353 if (error)
3354 goto done;
3356 if (do_list)
3357 error = list_refs(repo);
3358 else if (delref)
3359 error = delete_ref(repo, delref);
3360 else if (create_symref)
3361 error = add_symref(repo, argv[0], argv[1]);
3362 else
3363 error = add_ref(repo, argv[0], argv[1]);
3364 done:
3365 if (repo)
3366 got_repo_close(repo);
3367 if (worktree)
3368 got_worktree_close(worktree);
3369 free(cwd);
3370 free(repo_path);
3371 return error;
3374 __dead static void
3375 usage_branch(void)
3377 fprintf(stderr,
3378 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3379 "[name]\n", getprogname());
3380 exit(1);
3383 static const struct got_error *
3384 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3385 struct got_reference *ref)
3387 const struct got_error *err = NULL;
3388 const char *refname, *marker = " ";
3389 char *refstr;
3391 refname = got_ref_get_name(ref);
3392 if (worktree && strcmp(refname,
3393 got_worktree_get_head_ref_name(worktree)) == 0) {
3394 struct got_object_id *id = NULL;
3396 err = got_ref_resolve(&id, repo, ref);
3397 if (err)
3398 return err;
3399 if (got_object_id_cmp(id,
3400 got_worktree_get_base_commit_id(worktree)) == 0)
3401 marker = "* ";
3402 else
3403 marker = "~ ";
3404 free(id);
3407 if (strncmp(refname, "refs/heads/", 11) == 0)
3408 refname += 11;
3409 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3410 refname += 18;
3412 refstr = got_ref_to_str(ref);
3413 if (refstr == NULL)
3414 return got_error_from_errno("got_ref_to_str");
3416 printf("%s%s: %s\n", marker, refname, refstr);
3417 free(refstr);
3418 return NULL;
3421 static const struct got_error *
3422 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3424 const char *refname;
3426 if (worktree == NULL)
3427 return got_error(GOT_ERR_NOT_WORKTREE);
3429 refname = got_worktree_get_head_ref_name(worktree);
3431 if (strncmp(refname, "refs/heads/", 11) == 0)
3432 refname += 11;
3433 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3434 refname += 18;
3436 printf("%s\n", refname);
3438 return NULL;
3441 static const struct got_error *
3442 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3444 static const struct got_error *err = NULL;
3445 struct got_reflist_head refs;
3446 struct got_reflist_entry *re;
3447 struct got_reference *temp_ref = NULL;
3448 int rebase_in_progress, histedit_in_progress;
3450 SIMPLEQ_INIT(&refs);
3452 if (worktree) {
3453 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3454 worktree);
3455 if (err)
3456 return err;
3458 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3459 worktree);
3460 if (err)
3461 return err;
3463 if (rebase_in_progress || histedit_in_progress) {
3464 err = got_ref_open(&temp_ref, repo,
3465 got_worktree_get_head_ref_name(worktree), 0);
3466 if (err)
3467 return err;
3468 list_branch(repo, worktree, temp_ref);
3469 got_ref_close(temp_ref);
3473 err = got_ref_list(&refs, repo, "refs/heads",
3474 got_ref_cmp_by_name, NULL);
3475 if (err)
3476 return err;
3478 SIMPLEQ_FOREACH(re, &refs, entry)
3479 list_branch(repo, worktree, re->ref);
3481 got_ref_list_free(&refs);
3482 return NULL;
3485 static const struct got_error *
3486 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3487 const char *branch_name)
3489 const struct got_error *err = NULL;
3490 struct got_reference *ref = NULL;
3491 char *refname;
3493 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3494 return got_error_from_errno("asprintf");
3496 err = got_ref_open(&ref, repo, refname, 0);
3497 if (err)
3498 goto done;
3500 if (worktree &&
3501 strcmp(got_worktree_get_head_ref_name(worktree),
3502 got_ref_get_name(ref)) == 0) {
3503 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3504 "will not delete this work tree's current branch");
3505 goto done;
3508 err = got_ref_delete(ref, repo);
3509 done:
3510 if (ref)
3511 got_ref_close(ref);
3512 free(refname);
3513 return err;
3516 static const struct got_error *
3517 add_branch(struct got_repository *repo, const char *branch_name,
3518 struct got_object_id *base_commit_id)
3520 const struct got_error *err = NULL;
3521 struct got_reference *ref = NULL;
3522 char *base_refname = NULL, *refname = NULL;
3525 * Don't let the user create a branch name with a leading '-'.
3526 * While technically a valid reference name, this case is usually
3527 * an unintended typo.
3529 if (branch_name[0] == '-')
3530 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3532 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3533 err = got_error_from_errno("asprintf");
3534 goto done;
3537 err = got_ref_open(&ref, repo, refname, 0);
3538 if (err == NULL) {
3539 err = got_error(GOT_ERR_BRANCH_EXISTS);
3540 goto done;
3541 } else if (err->code != GOT_ERR_NOT_REF)
3542 goto done;
3544 err = got_ref_alloc(&ref, refname, base_commit_id);
3545 if (err)
3546 goto done;
3548 err = got_ref_write(ref, repo);
3549 done:
3550 if (ref)
3551 got_ref_close(ref);
3552 free(base_refname);
3553 free(refname);
3554 return err;
3557 static const struct got_error *
3558 cmd_branch(int argc, char *argv[])
3560 const struct got_error *error = NULL;
3561 struct got_repository *repo = NULL;
3562 struct got_worktree *worktree = NULL;
3563 char *cwd = NULL, *repo_path = NULL;
3564 int ch, do_list = 0, do_show = 0;
3565 const char *delref = NULL, *commit_id_arg = NULL;
3567 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3568 switch (ch) {
3569 case 'c':
3570 commit_id_arg = optarg;
3571 break;
3572 case 'd':
3573 delref = optarg;
3574 break;
3575 case 'r':
3576 repo_path = realpath(optarg, NULL);
3577 if (repo_path == NULL)
3578 return got_error_from_errno2("realpath",
3579 optarg);
3580 got_path_strip_trailing_slashes(repo_path);
3581 break;
3582 case 'l':
3583 do_list = 1;
3584 break;
3585 default:
3586 usage_branch();
3587 /* NOTREACHED */
3591 if (do_list && delref)
3592 errx(1, "-l and -d options are mutually exclusive\n");
3594 argc -= optind;
3595 argv += optind;
3597 if (!do_list && !delref && argc == 0)
3598 do_show = 1;
3600 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3601 errx(1, "-c option can only be used when creating a branch");
3603 if (do_list || delref) {
3604 if (argc > 0)
3605 usage_branch();
3606 } else if (!do_show && argc != 1)
3607 usage_branch();
3609 #ifndef PROFILE
3610 if (do_list || do_show) {
3611 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3612 NULL) == -1)
3613 err(1, "pledge");
3614 } else {
3615 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3616 "sendfd unveil", NULL) == -1)
3617 err(1, "pledge");
3619 #endif
3620 cwd = getcwd(NULL, 0);
3621 if (cwd == NULL) {
3622 error = got_error_from_errno("getcwd");
3623 goto done;
3626 if (repo_path == NULL) {
3627 error = got_worktree_open(&worktree, cwd);
3628 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3629 goto done;
3630 else
3631 error = NULL;
3632 if (worktree) {
3633 repo_path =
3634 strdup(got_worktree_get_repo_path(worktree));
3635 if (repo_path == NULL)
3636 error = got_error_from_errno("strdup");
3637 if (error)
3638 goto done;
3639 } else {
3640 repo_path = strdup(cwd);
3641 if (repo_path == NULL) {
3642 error = got_error_from_errno("strdup");
3643 goto done;
3648 error = got_repo_open(&repo, repo_path, NULL);
3649 if (error != NULL)
3650 goto done;
3652 error = apply_unveil(got_repo_get_path(repo), do_list,
3653 worktree ? got_worktree_get_root_path(worktree) : NULL);
3654 if (error)
3655 goto done;
3657 if (do_show)
3658 error = show_current_branch(repo, worktree);
3659 else if (do_list)
3660 error = list_branches(repo, worktree);
3661 else if (delref)
3662 error = delete_branch(repo, worktree, delref);
3663 else {
3664 struct got_object_id *commit_id;
3665 if (commit_id_arg == NULL)
3666 commit_id_arg = worktree ?
3667 got_worktree_get_head_ref_name(worktree) :
3668 GOT_REF_HEAD;
3669 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3670 if (error)
3671 goto done;
3672 error = add_branch(repo, argv[0], commit_id);
3673 free(commit_id);
3675 done:
3676 if (repo)
3677 got_repo_close(repo);
3678 if (worktree)
3679 got_worktree_close(worktree);
3680 free(cwd);
3681 free(repo_path);
3682 return error;
3686 __dead static void
3687 usage_tag(void)
3689 fprintf(stderr,
3690 "usage: %s tag [-r repository] | -l | "
3691 "[-m message] name [commit]\n", getprogname());
3692 exit(1);
3695 #if 0
3696 static const struct got_error *
3697 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3699 const struct got_error *err = NULL;
3700 struct got_reflist_entry *re, *se, *new;
3701 struct got_object_id *re_id, *se_id;
3702 struct got_tag_object *re_tag, *se_tag;
3703 time_t re_time, se_time;
3705 SIMPLEQ_FOREACH(re, tags, entry) {
3706 se = SIMPLEQ_FIRST(sorted);
3707 if (se == NULL) {
3708 err = got_reflist_entry_dup(&new, re);
3709 if (err)
3710 return err;
3711 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3712 continue;
3713 } else {
3714 err = got_ref_resolve(&re_id, repo, re->ref);
3715 if (err)
3716 break;
3717 err = got_object_open_as_tag(&re_tag, repo, re_id);
3718 free(re_id);
3719 if (err)
3720 break;
3721 re_time = got_object_tag_get_tagger_time(re_tag);
3722 got_object_tag_close(re_tag);
3725 while (se) {
3726 err = got_ref_resolve(&se_id, repo, re->ref);
3727 if (err)
3728 break;
3729 err = got_object_open_as_tag(&se_tag, repo, se_id);
3730 free(se_id);
3731 if (err)
3732 break;
3733 se_time = got_object_tag_get_tagger_time(se_tag);
3734 got_object_tag_close(se_tag);
3736 if (se_time > re_time) {
3737 err = got_reflist_entry_dup(&new, re);
3738 if (err)
3739 return err;
3740 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3741 break;
3743 se = SIMPLEQ_NEXT(se, entry);
3744 continue;
3747 done:
3748 return err;
3750 #endif
3752 static const struct got_error *
3753 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3754 struct got_reference *ref2)
3756 const struct got_error *err = NULL;
3757 struct got_repository *repo = arg;
3758 struct got_object_id *id1, *id2 = NULL;
3759 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3760 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3761 time_t time1, time2;
3763 *cmp = 0;
3765 err = got_ref_resolve(&id1, repo, ref1);
3766 if (err)
3767 return err;
3768 err = got_object_open_as_tag(&tag1, repo, id1);
3769 if (err) {
3770 if (err->code != GOT_ERR_OBJ_TYPE)
3771 goto done;
3772 /* "lightweight" tag */
3773 err = got_object_open_as_commit(&commit1, repo, id1);
3774 if (err)
3775 goto done;
3776 time1 = got_object_commit_get_committer_time(commit1);
3777 } else
3778 time1 = got_object_tag_get_tagger_time(tag1);
3780 err = got_ref_resolve(&id2, repo, ref2);
3781 if (err)
3782 goto done;
3783 err = got_object_open_as_tag(&tag2, repo, id2);
3784 if (err) {
3785 if (err->code != GOT_ERR_OBJ_TYPE)
3786 goto done;
3787 /* "lightweight" tag */
3788 err = got_object_open_as_commit(&commit2, repo, id2);
3789 if (err)
3790 goto done;
3791 time2 = got_object_commit_get_committer_time(commit2);
3792 } else
3793 time2 = got_object_tag_get_tagger_time(tag2);
3795 /* Put latest tags first. */
3796 if (time1 < time2)
3797 *cmp = 1;
3798 else if (time1 > time2)
3799 *cmp = -1;
3800 else
3801 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3802 done:
3803 free(id1);
3804 free(id2);
3805 if (tag1)
3806 got_object_tag_close(tag1);
3807 if (tag2)
3808 got_object_tag_close(tag2);
3809 if (commit1)
3810 got_object_commit_close(commit1);
3811 if (commit2)
3812 got_object_commit_close(commit2);
3813 return err;
3816 static const struct got_error *
3817 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3819 static const struct got_error *err = NULL;
3820 struct got_reflist_head refs;
3821 struct got_reflist_entry *re;
3823 SIMPLEQ_INIT(&refs);
3825 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3826 if (err)
3827 return err;
3829 SIMPLEQ_FOREACH(re, &refs, entry) {
3830 const char *refname;
3831 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3832 char datebuf[26];
3833 const char *tagger;
3834 time_t tagger_time;
3835 struct got_object_id *id;
3836 struct got_tag_object *tag;
3837 struct got_commit_object *commit = NULL;
3839 refname = got_ref_get_name(re->ref);
3840 if (strncmp(refname, "refs/tags/", 10) != 0)
3841 continue;
3842 refname += 10;
3843 refstr = got_ref_to_str(re->ref);
3844 if (refstr == NULL) {
3845 err = got_error_from_errno("got_ref_to_str");
3846 break;
3848 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3849 free(refstr);
3851 err = got_ref_resolve(&id, repo, re->ref);
3852 if (err)
3853 break;
3854 err = got_object_open_as_tag(&tag, repo, id);
3855 if (err) {
3856 if (err->code != GOT_ERR_OBJ_TYPE) {
3857 free(id);
3858 break;
3860 /* "lightweight" tag */
3861 err = got_object_open_as_commit(&commit, repo, id);
3862 if (err) {
3863 free(id);
3864 break;
3866 tagger = got_object_commit_get_committer(commit);
3867 tagger_time =
3868 got_object_commit_get_committer_time(commit);
3869 err = got_object_id_str(&id_str, id);
3870 free(id);
3871 if (err)
3872 break;
3873 } else {
3874 free(id);
3875 tagger = got_object_tag_get_tagger(tag);
3876 tagger_time = got_object_tag_get_tagger_time(tag);
3877 err = got_object_id_str(&id_str,
3878 got_object_tag_get_object_id(tag));
3879 if (err)
3880 break;
3882 printf("from: %s\n", tagger);
3883 datestr = get_datestr(&tagger_time, datebuf);
3884 if (datestr)
3885 printf("date: %s UTC\n", datestr);
3886 if (commit)
3887 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3888 else {
3889 switch (got_object_tag_get_object_type(tag)) {
3890 case GOT_OBJ_TYPE_BLOB:
3891 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3892 id_str);
3893 break;
3894 case GOT_OBJ_TYPE_TREE:
3895 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3896 id_str);
3897 break;
3898 case GOT_OBJ_TYPE_COMMIT:
3899 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
3900 id_str);
3901 break;
3902 case GOT_OBJ_TYPE_TAG:
3903 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
3904 id_str);
3905 break;
3906 default:
3907 break;
3910 free(id_str);
3911 if (commit) {
3912 err = got_object_commit_get_logmsg(&tagmsg0, commit);
3913 if (err)
3914 break;
3915 got_object_commit_close(commit);
3916 } else {
3917 tagmsg0 = strdup(got_object_tag_get_message(tag));
3918 got_object_tag_close(tag);
3919 if (tagmsg0 == NULL) {
3920 err = got_error_from_errno("strdup");
3921 break;
3925 tagmsg = tagmsg0;
3926 do {
3927 line = strsep(&tagmsg, "\n");
3928 if (line)
3929 printf(" %s\n", line);
3930 } while (line);
3931 free(tagmsg0);
3934 got_ref_list_free(&refs);
3935 return NULL;
3938 static const struct got_error *
3939 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3940 const char *tag_name, const char *repo_path)
3942 const struct got_error *err = NULL;
3943 char *template = NULL, *initial_content = NULL;
3944 char *editor = NULL;
3945 int fd = -1;
3947 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3948 err = got_error_from_errno("asprintf");
3949 goto done;
3952 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3953 commit_id_str, tag_name) == -1) {
3954 err = got_error_from_errno("asprintf");
3955 goto done;
3958 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3959 if (err)
3960 goto done;
3962 dprintf(fd, initial_content);
3963 close(fd);
3965 err = get_editor(&editor);
3966 if (err)
3967 goto done;
3968 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3969 done:
3970 free(initial_content);
3971 free(template);
3972 free(editor);
3974 /* Editor is done; we can now apply unveil(2) */
3975 if (err == NULL) {
3976 err = apply_unveil(repo_path, 0, NULL);
3977 if (err) {
3978 free(*tagmsg);
3979 *tagmsg = NULL;
3982 return err;
3985 static const struct got_error *
3986 add_tag(struct got_repository *repo, const char *tag_name,
3987 const char *commit_arg, const char *tagmsg_arg)
3989 const struct got_error *err = NULL;
3990 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3991 char *label = NULL, *commit_id_str = NULL;
3992 struct got_reference *ref = NULL;
3993 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3994 char *tagmsg_path = NULL, *tag_id_str = NULL;
3995 int preserve_tagmsg = 0;
3998 * Don't let the user create a tag name with a leading '-'.
3999 * While technically a valid reference name, this case is usually
4000 * an unintended typo.
4002 if (tag_name[0] == '-')
4003 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4005 err = get_author(&tagger, repo);
4006 if (err)
4007 return err;
4009 err = match_object_id(&commit_id, &label, commit_arg,
4010 GOT_OBJ_TYPE_COMMIT, 1, repo);
4011 if (err)
4012 goto done;
4014 err = got_object_id_str(&commit_id_str, commit_id);
4015 if (err)
4016 goto done;
4018 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4019 refname = strdup(tag_name);
4020 if (refname == NULL) {
4021 err = got_error_from_errno("strdup");
4022 goto done;
4024 tag_name += 10;
4025 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4026 err = got_error_from_errno("asprintf");
4027 goto done;
4030 err = got_ref_open(&ref, repo, refname, 0);
4031 if (err == NULL) {
4032 err = got_error(GOT_ERR_TAG_EXISTS);
4033 goto done;
4034 } else if (err->code != GOT_ERR_NOT_REF)
4035 goto done;
4037 if (tagmsg_arg == NULL) {
4038 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4039 tag_name, got_repo_get_path(repo));
4040 if (err) {
4041 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4042 tagmsg_path != NULL)
4043 preserve_tagmsg = 1;
4044 goto done;
4048 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4049 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4050 if (err) {
4051 if (tagmsg_path)
4052 preserve_tagmsg = 1;
4053 goto done;
4056 err = got_ref_alloc(&ref, refname, tag_id);
4057 if (err) {
4058 if (tagmsg_path)
4059 preserve_tagmsg = 1;
4060 goto done;
4063 err = got_ref_write(ref, repo);
4064 if (err) {
4065 if (tagmsg_path)
4066 preserve_tagmsg = 1;
4067 goto done;
4070 err = got_object_id_str(&tag_id_str, tag_id);
4071 if (err) {
4072 if (tagmsg_path)
4073 preserve_tagmsg = 1;
4074 goto done;
4076 printf("Created tag %s\n", tag_id_str);
4077 done:
4078 if (preserve_tagmsg) {
4079 fprintf(stderr, "%s: tag message preserved in %s\n",
4080 getprogname(), tagmsg_path);
4081 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4082 err = got_error_from_errno2("unlink", tagmsg_path);
4083 free(tag_id_str);
4084 if (ref)
4085 got_ref_close(ref);
4086 free(commit_id);
4087 free(commit_id_str);
4088 free(refname);
4089 free(tagmsg);
4090 free(tagmsg_path);
4091 free(tagger);
4092 return err;
4095 static const struct got_error *
4096 cmd_tag(int argc, char *argv[])
4098 const struct got_error *error = NULL;
4099 struct got_repository *repo = NULL;
4100 struct got_worktree *worktree = NULL;
4101 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4102 char *gitconfig_path = NULL;
4103 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4104 int ch, do_list = 0;
4106 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4107 switch (ch) {
4108 case 'm':
4109 tagmsg = optarg;
4110 break;
4111 case 'r':
4112 repo_path = realpath(optarg, NULL);
4113 if (repo_path == NULL)
4114 return got_error_from_errno2("realpath",
4115 optarg);
4116 got_path_strip_trailing_slashes(repo_path);
4117 break;
4118 case 'l':
4119 do_list = 1;
4120 break;
4121 default:
4122 usage_tag();
4123 /* NOTREACHED */
4127 argc -= optind;
4128 argv += optind;
4130 if (do_list) {
4131 if (tagmsg)
4132 errx(1, "-l and -m options are mutually exclusive\n");
4133 if (argc > 0)
4134 usage_tag();
4135 } else if (argc < 1 || argc > 2)
4136 usage_tag();
4137 else if (argc > 1)
4138 commit_id_arg = argv[1];
4139 tag_name = argv[0];
4141 #ifndef PROFILE
4142 if (do_list) {
4143 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4144 NULL) == -1)
4145 err(1, "pledge");
4146 } else {
4147 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4148 "sendfd unveil", NULL) == -1)
4149 err(1, "pledge");
4151 #endif
4152 cwd = getcwd(NULL, 0);
4153 if (cwd == NULL) {
4154 error = got_error_from_errno("getcwd");
4155 goto done;
4158 if (repo_path == NULL) {
4159 error = got_worktree_open(&worktree, cwd);
4160 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4161 goto done;
4162 else
4163 error = NULL;
4164 if (worktree) {
4165 repo_path =
4166 strdup(got_worktree_get_repo_path(worktree));
4167 if (repo_path == NULL)
4168 error = got_error_from_errno("strdup");
4169 if (error)
4170 goto done;
4171 } else {
4172 repo_path = strdup(cwd);
4173 if (repo_path == NULL) {
4174 error = got_error_from_errno("strdup");
4175 goto done;
4180 if (do_list) {
4181 error = got_repo_open(&repo, repo_path, NULL);
4182 if (error != NULL)
4183 goto done;
4184 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4185 if (error)
4186 goto done;
4187 error = list_tags(repo, worktree);
4188 } else {
4189 error = get_gitconfig_path(&gitconfig_path);
4190 if (error)
4191 goto done;
4192 error = got_repo_open(&repo, repo_path, gitconfig_path);
4193 if (error != NULL)
4194 goto done;
4196 if (tagmsg) {
4197 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4198 if (error)
4199 goto done;
4202 if (commit_id_arg == NULL) {
4203 struct got_reference *head_ref;
4204 struct got_object_id *commit_id;
4205 error = got_ref_open(&head_ref, repo,
4206 worktree ? got_worktree_get_head_ref_name(worktree)
4207 : GOT_REF_HEAD, 0);
4208 if (error)
4209 goto done;
4210 error = got_ref_resolve(&commit_id, repo, head_ref);
4211 got_ref_close(head_ref);
4212 if (error)
4213 goto done;
4214 error = got_object_id_str(&commit_id_str, commit_id);
4215 free(commit_id);
4216 if (error)
4217 goto done;
4220 error = add_tag(repo, tag_name,
4221 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4223 done:
4224 if (repo)
4225 got_repo_close(repo);
4226 if (worktree)
4227 got_worktree_close(worktree);
4228 free(cwd);
4229 free(repo_path);
4230 free(gitconfig_path);
4231 free(commit_id_str);
4232 return error;
4235 __dead static void
4236 usage_add(void)
4238 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4239 getprogname());
4240 exit(1);
4243 static const struct got_error *
4244 add_progress(void *arg, unsigned char status, const char *path)
4246 while (path[0] == '/')
4247 path++;
4248 printf("%c %s\n", status, path);
4249 return NULL;
4252 static const struct got_error *
4253 cmd_add(int argc, char *argv[])
4255 const struct got_error *error = NULL;
4256 struct got_repository *repo = NULL;
4257 struct got_worktree *worktree = NULL;
4258 char *cwd = NULL;
4259 struct got_pathlist_head paths;
4260 struct got_pathlist_entry *pe;
4261 int ch, can_recurse = 0, no_ignores = 0;
4263 TAILQ_INIT(&paths);
4265 while ((ch = getopt(argc, argv, "IR")) != -1) {
4266 switch (ch) {
4267 case 'I':
4268 no_ignores = 1;
4269 break;
4270 case 'R':
4271 can_recurse = 1;
4272 break;
4273 default:
4274 usage_add();
4275 /* NOTREACHED */
4279 argc -= optind;
4280 argv += optind;
4282 #ifndef PROFILE
4283 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4284 NULL) == -1)
4285 err(1, "pledge");
4286 #endif
4287 if (argc < 1)
4288 usage_add();
4290 cwd = getcwd(NULL, 0);
4291 if (cwd == NULL) {
4292 error = got_error_from_errno("getcwd");
4293 goto done;
4296 error = got_worktree_open(&worktree, cwd);
4297 if (error)
4298 goto done;
4300 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4301 NULL);
4302 if (error != NULL)
4303 goto done;
4305 error = apply_unveil(got_repo_get_path(repo), 1,
4306 got_worktree_get_root_path(worktree));
4307 if (error)
4308 goto done;
4310 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4311 if (error)
4312 goto done;
4314 if (!can_recurse && no_ignores) {
4315 error = got_error_msg(GOT_ERR_BAD_PATH,
4316 "disregarding ignores requires -R option");
4317 goto done;
4321 if (!can_recurse) {
4322 char *ondisk_path;
4323 struct stat sb;
4324 TAILQ_FOREACH(pe, &paths, entry) {
4325 if (asprintf(&ondisk_path, "%s/%s",
4326 got_worktree_get_root_path(worktree),
4327 pe->path) == -1) {
4328 error = got_error_from_errno("asprintf");
4329 goto done;
4331 if (lstat(ondisk_path, &sb) == -1) {
4332 if (errno == ENOENT) {
4333 free(ondisk_path);
4334 continue;
4336 error = got_error_from_errno2("lstat",
4337 ondisk_path);
4338 free(ondisk_path);
4339 goto done;
4341 free(ondisk_path);
4342 if (S_ISDIR(sb.st_mode)) {
4343 error = got_error_msg(GOT_ERR_BAD_PATH,
4344 "adding directories requires -R option");
4345 goto done;
4350 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4351 NULL, repo, no_ignores);
4352 done:
4353 if (repo)
4354 got_repo_close(repo);
4355 if (worktree)
4356 got_worktree_close(worktree);
4357 TAILQ_FOREACH(pe, &paths, entry)
4358 free((char *)pe->path);
4359 got_pathlist_free(&paths);
4360 free(cwd);
4361 return error;
4364 __dead static void
4365 usage_remove(void)
4367 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4368 getprogname());
4369 exit(1);
4372 static const struct got_error *
4373 print_remove_status(void *arg, unsigned char status,
4374 unsigned char staged_status, const char *path)
4376 while (path[0] == '/')
4377 path++;
4378 if (status == GOT_STATUS_NONEXISTENT)
4379 return NULL;
4380 if (status == staged_status && (status == GOT_STATUS_DELETE))
4381 status = GOT_STATUS_NO_CHANGE;
4382 printf("%c%c %s\n", status, staged_status, path);
4383 return NULL;
4386 static const struct got_error *
4387 cmd_remove(int argc, char *argv[])
4389 const struct got_error *error = NULL;
4390 struct got_worktree *worktree = NULL;
4391 struct got_repository *repo = NULL;
4392 char *cwd = NULL;
4393 struct got_pathlist_head paths;
4394 struct got_pathlist_entry *pe;
4395 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4397 TAILQ_INIT(&paths);
4399 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4400 switch (ch) {
4401 case 'f':
4402 delete_local_mods = 1;
4403 break;
4404 case 'k':
4405 keep_on_disk = 1;
4406 break;
4407 case 'R':
4408 can_recurse = 1;
4409 break;
4410 default:
4411 usage_remove();
4412 /* NOTREACHED */
4416 argc -= optind;
4417 argv += optind;
4419 #ifndef PROFILE
4420 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4421 NULL) == -1)
4422 err(1, "pledge");
4423 #endif
4424 if (argc < 1)
4425 usage_remove();
4427 cwd = getcwd(NULL, 0);
4428 if (cwd == NULL) {
4429 error = got_error_from_errno("getcwd");
4430 goto done;
4432 error = got_worktree_open(&worktree, cwd);
4433 if (error)
4434 goto done;
4436 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4437 NULL);
4438 if (error)
4439 goto done;
4441 error = apply_unveil(got_repo_get_path(repo), 1,
4442 got_worktree_get_root_path(worktree));
4443 if (error)
4444 goto done;
4446 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4447 if (error)
4448 goto done;
4450 if (!can_recurse) {
4451 char *ondisk_path;
4452 struct stat sb;
4453 TAILQ_FOREACH(pe, &paths, entry) {
4454 if (asprintf(&ondisk_path, "%s/%s",
4455 got_worktree_get_root_path(worktree),
4456 pe->path) == -1) {
4457 error = got_error_from_errno("asprintf");
4458 goto done;
4460 if (lstat(ondisk_path, &sb) == -1) {
4461 if (errno == ENOENT) {
4462 free(ondisk_path);
4463 continue;
4465 error = got_error_from_errno2("lstat",
4466 ondisk_path);
4467 free(ondisk_path);
4468 goto done;
4470 free(ondisk_path);
4471 if (S_ISDIR(sb.st_mode)) {
4472 error = got_error_msg(GOT_ERR_BAD_PATH,
4473 "removing directories requires -R option");
4474 goto done;
4479 error = got_worktree_schedule_delete(worktree, &paths,
4480 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4481 done:
4482 if (repo)
4483 got_repo_close(repo);
4484 if (worktree)
4485 got_worktree_close(worktree);
4486 TAILQ_FOREACH(pe, &paths, entry)
4487 free((char *)pe->path);
4488 got_pathlist_free(&paths);
4489 free(cwd);
4490 return error;
4493 __dead static void
4494 usage_revert(void)
4496 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4497 "path ...\n", getprogname());
4498 exit(1);
4501 static const struct got_error *
4502 revert_progress(void *arg, unsigned char status, const char *path)
4504 while (path[0] == '/')
4505 path++;
4506 printf("%c %s\n", status, path);
4507 return NULL;
4510 struct choose_patch_arg {
4511 FILE *patch_script_file;
4512 const char *action;
4515 static const struct got_error *
4516 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4517 int nchanges, const char *action)
4519 char *line = NULL;
4520 size_t linesize = 0;
4521 ssize_t linelen;
4523 switch (status) {
4524 case GOT_STATUS_ADD:
4525 printf("A %s\n%s this addition? [y/n] ", path, action);
4526 break;
4527 case GOT_STATUS_DELETE:
4528 printf("D %s\n%s this deletion? [y/n] ", path, action);
4529 break;
4530 case GOT_STATUS_MODIFY:
4531 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4532 return got_error_from_errno("fseek");
4533 printf(GOT_COMMIT_SEP_STR);
4534 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4535 printf("%s", line);
4536 if (ferror(patch_file))
4537 return got_error_from_errno("getline");
4538 printf(GOT_COMMIT_SEP_STR);
4539 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4540 path, n, nchanges, action);
4541 break;
4542 default:
4543 return got_error_path(path, GOT_ERR_FILE_STATUS);
4546 return NULL;
4549 static const struct got_error *
4550 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4551 FILE *patch_file, int n, int nchanges)
4553 const struct got_error *err = NULL;
4554 char *line = NULL;
4555 size_t linesize = 0;
4556 ssize_t linelen;
4557 int resp = ' ';
4558 struct choose_patch_arg *a = arg;
4560 *choice = GOT_PATCH_CHOICE_NONE;
4562 if (a->patch_script_file) {
4563 char *nl;
4564 err = show_change(status, path, patch_file, n, nchanges,
4565 a->action);
4566 if (err)
4567 return err;
4568 linelen = getline(&line, &linesize, a->patch_script_file);
4569 if (linelen == -1) {
4570 if (ferror(a->patch_script_file))
4571 return got_error_from_errno("getline");
4572 return NULL;
4574 nl = strchr(line, '\n');
4575 if (nl)
4576 *nl = '\0';
4577 if (strcmp(line, "y") == 0) {
4578 *choice = GOT_PATCH_CHOICE_YES;
4579 printf("y\n");
4580 } else if (strcmp(line, "n") == 0) {
4581 *choice = GOT_PATCH_CHOICE_NO;
4582 printf("n\n");
4583 } else if (strcmp(line, "q") == 0 &&
4584 status == GOT_STATUS_MODIFY) {
4585 *choice = GOT_PATCH_CHOICE_QUIT;
4586 printf("q\n");
4587 } else
4588 printf("invalid response '%s'\n", line);
4589 free(line);
4590 return NULL;
4593 while (resp != 'y' && resp != 'n' && resp != 'q') {
4594 err = show_change(status, path, patch_file, n, nchanges,
4595 a->action);
4596 if (err)
4597 return err;
4598 resp = getchar();
4599 if (resp == '\n')
4600 resp = getchar();
4601 if (status == GOT_STATUS_MODIFY) {
4602 if (resp != 'y' && resp != 'n' && resp != 'q') {
4603 printf("invalid response '%c'\n", resp);
4604 resp = ' ';
4606 } else if (resp != 'y' && resp != 'n') {
4607 printf("invalid response '%c'\n", resp);
4608 resp = ' ';
4612 if (resp == 'y')
4613 *choice = GOT_PATCH_CHOICE_YES;
4614 else if (resp == 'n')
4615 *choice = GOT_PATCH_CHOICE_NO;
4616 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4617 *choice = GOT_PATCH_CHOICE_QUIT;
4619 return NULL;
4623 static const struct got_error *
4624 cmd_revert(int argc, char *argv[])
4626 const struct got_error *error = NULL;
4627 struct got_worktree *worktree = NULL;
4628 struct got_repository *repo = NULL;
4629 char *cwd = NULL, *path = NULL;
4630 struct got_pathlist_head paths;
4631 struct got_pathlist_entry *pe;
4632 int ch, can_recurse = 0, pflag = 0;
4633 FILE *patch_script_file = NULL;
4634 const char *patch_script_path = NULL;
4635 struct choose_patch_arg cpa;
4637 TAILQ_INIT(&paths);
4639 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4640 switch (ch) {
4641 case 'p':
4642 pflag = 1;
4643 break;
4644 case 'F':
4645 patch_script_path = optarg;
4646 break;
4647 case 'R':
4648 can_recurse = 1;
4649 break;
4650 default:
4651 usage_revert();
4652 /* NOTREACHED */
4656 argc -= optind;
4657 argv += optind;
4659 #ifndef PROFILE
4660 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4661 "unveil", NULL) == -1)
4662 err(1, "pledge");
4663 #endif
4664 if (argc < 1)
4665 usage_revert();
4666 if (patch_script_path && !pflag)
4667 errx(1, "-F option can only be used together with -p option");
4669 cwd = getcwd(NULL, 0);
4670 if (cwd == NULL) {
4671 error = got_error_from_errno("getcwd");
4672 goto done;
4674 error = got_worktree_open(&worktree, cwd);
4675 if (error)
4676 goto done;
4678 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4679 NULL);
4680 if (error != NULL)
4681 goto done;
4683 if (patch_script_path) {
4684 patch_script_file = fopen(patch_script_path, "r");
4685 if (patch_script_file == NULL) {
4686 error = got_error_from_errno2("fopen",
4687 patch_script_path);
4688 goto done;
4691 error = apply_unveil(got_repo_get_path(repo), 1,
4692 got_worktree_get_root_path(worktree));
4693 if (error)
4694 goto done;
4696 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4697 if (error)
4698 goto done;
4700 if (!can_recurse) {
4701 char *ondisk_path;
4702 struct stat sb;
4703 TAILQ_FOREACH(pe, &paths, entry) {
4704 if (asprintf(&ondisk_path, "%s/%s",
4705 got_worktree_get_root_path(worktree),
4706 pe->path) == -1) {
4707 error = got_error_from_errno("asprintf");
4708 goto done;
4710 if (lstat(ondisk_path, &sb) == -1) {
4711 if (errno == ENOENT) {
4712 free(ondisk_path);
4713 continue;
4715 error = got_error_from_errno2("lstat",
4716 ondisk_path);
4717 free(ondisk_path);
4718 goto done;
4720 free(ondisk_path);
4721 if (S_ISDIR(sb.st_mode)) {
4722 error = got_error_msg(GOT_ERR_BAD_PATH,
4723 "reverting directories requires -R option");
4724 goto done;
4729 cpa.patch_script_file = patch_script_file;
4730 cpa.action = "revert";
4731 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4732 pflag ? choose_patch : NULL, &cpa, repo);
4733 done:
4734 if (patch_script_file && fclose(patch_script_file) == EOF &&
4735 error == NULL)
4736 error = got_error_from_errno2("fclose", patch_script_path);
4737 if (repo)
4738 got_repo_close(repo);
4739 if (worktree)
4740 got_worktree_close(worktree);
4741 free(path);
4742 free(cwd);
4743 return error;
4746 __dead static void
4747 usage_commit(void)
4749 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4750 getprogname());
4751 exit(1);
4754 struct collect_commit_logmsg_arg {
4755 const char *cmdline_log;
4756 const char *editor;
4757 const char *worktree_path;
4758 const char *branch_name;
4759 const char *repo_path;
4760 char *logmsg_path;
4764 static const struct got_error *
4765 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4766 void *arg)
4768 char *initial_content = NULL;
4769 struct got_pathlist_entry *pe;
4770 const struct got_error *err = NULL;
4771 char *template = NULL;
4772 struct collect_commit_logmsg_arg *a = arg;
4773 int fd;
4774 size_t len;
4776 /* if a message was specified on the command line, just use it */
4777 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4778 len = strlen(a->cmdline_log) + 1;
4779 *logmsg = malloc(len + 1);
4780 if (*logmsg == NULL)
4781 return got_error_from_errno("malloc");
4782 strlcpy(*logmsg, a->cmdline_log, len);
4783 return NULL;
4786 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4787 return got_error_from_errno("asprintf");
4789 if (asprintf(&initial_content,
4790 "\n# changes to be committed on branch %s:\n",
4791 a->branch_name) == -1)
4792 return got_error_from_errno("asprintf");
4794 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4795 if (err)
4796 goto done;
4798 dprintf(fd, initial_content);
4800 TAILQ_FOREACH(pe, commitable_paths, entry) {
4801 struct got_commitable *ct = pe->data;
4802 dprintf(fd, "# %c %s\n",
4803 got_commitable_get_status(ct),
4804 got_commitable_get_path(ct));
4806 close(fd);
4808 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4809 done:
4810 free(initial_content);
4811 free(template);
4813 /* Editor is done; we can now apply unveil(2) */
4814 if (err == NULL) {
4815 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4816 if (err) {
4817 free(*logmsg);
4818 *logmsg = NULL;
4821 return err;
4824 static const struct got_error *
4825 cmd_commit(int argc, char *argv[])
4827 const struct got_error *error = NULL;
4828 struct got_worktree *worktree = NULL;
4829 struct got_repository *repo = NULL;
4830 char *cwd = NULL, *id_str = NULL;
4831 struct got_object_id *id = NULL;
4832 const char *logmsg = NULL;
4833 struct collect_commit_logmsg_arg cl_arg;
4834 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4835 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4836 struct got_pathlist_head paths;
4838 TAILQ_INIT(&paths);
4839 cl_arg.logmsg_path = NULL;
4841 while ((ch = getopt(argc, argv, "m:")) != -1) {
4842 switch (ch) {
4843 case 'm':
4844 logmsg = optarg;
4845 break;
4846 default:
4847 usage_commit();
4848 /* NOTREACHED */
4852 argc -= optind;
4853 argv += optind;
4855 #ifndef PROFILE
4856 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4857 "unveil", NULL) == -1)
4858 err(1, "pledge");
4859 #endif
4860 cwd = getcwd(NULL, 0);
4861 if (cwd == NULL) {
4862 error = got_error_from_errno("getcwd");
4863 goto done;
4865 error = got_worktree_open(&worktree, cwd);
4866 if (error)
4867 goto done;
4869 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4870 if (error)
4871 goto done;
4872 if (rebase_in_progress) {
4873 error = got_error(GOT_ERR_REBASING);
4874 goto done;
4877 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4878 worktree);
4879 if (error)
4880 goto done;
4882 error = get_gitconfig_path(&gitconfig_path);
4883 if (error)
4884 goto done;
4885 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4886 gitconfig_path);
4887 if (error != NULL)
4888 goto done;
4890 error = get_author(&author, repo);
4891 if (error)
4892 return error;
4895 * unveil(2) traverses exec(2); if an editor is used we have
4896 * to apply unveil after the log message has been written.
4898 if (logmsg == NULL || strlen(logmsg) == 0)
4899 error = get_editor(&editor);
4900 else
4901 error = apply_unveil(got_repo_get_path(repo), 0,
4902 got_worktree_get_root_path(worktree));
4903 if (error)
4904 goto done;
4906 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4907 if (error)
4908 goto done;
4910 cl_arg.editor = editor;
4911 cl_arg.cmdline_log = logmsg;
4912 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4913 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4914 if (!histedit_in_progress) {
4915 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4916 error = got_error(GOT_ERR_COMMIT_BRANCH);
4917 goto done;
4919 cl_arg.branch_name += 11;
4921 cl_arg.repo_path = got_repo_get_path(repo);
4922 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4923 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4924 if (error) {
4925 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4926 cl_arg.logmsg_path != NULL)
4927 preserve_logmsg = 1;
4928 goto done;
4931 error = got_object_id_str(&id_str, id);
4932 if (error)
4933 goto done;
4934 printf("Created commit %s\n", id_str);
4935 done:
4936 if (preserve_logmsg) {
4937 fprintf(stderr, "%s: log message preserved in %s\n",
4938 getprogname(), cl_arg.logmsg_path);
4939 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4940 error == NULL)
4941 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4942 free(cl_arg.logmsg_path);
4943 if (repo)
4944 got_repo_close(repo);
4945 if (worktree)
4946 got_worktree_close(worktree);
4947 free(cwd);
4948 free(id_str);
4949 free(gitconfig_path);
4950 free(editor);
4951 free(author);
4952 return error;
4955 __dead static void
4956 usage_cherrypick(void)
4958 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4959 exit(1);
4962 static const struct got_error *
4963 cmd_cherrypick(int argc, char *argv[])
4965 const struct got_error *error = NULL;
4966 struct got_worktree *worktree = NULL;
4967 struct got_repository *repo = NULL;
4968 char *cwd = NULL, *commit_id_str = NULL;
4969 struct got_object_id *commit_id = NULL;
4970 struct got_commit_object *commit = NULL;
4971 struct got_object_qid *pid;
4972 struct got_reference *head_ref = NULL;
4973 int ch, did_something = 0;
4975 while ((ch = getopt(argc, argv, "")) != -1) {
4976 switch (ch) {
4977 default:
4978 usage_cherrypick();
4979 /* NOTREACHED */
4983 argc -= optind;
4984 argv += optind;
4986 #ifndef PROFILE
4987 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4988 "unveil", NULL) == -1)
4989 err(1, "pledge");
4990 #endif
4991 if (argc != 1)
4992 usage_cherrypick();
4994 cwd = getcwd(NULL, 0);
4995 if (cwd == NULL) {
4996 error = got_error_from_errno("getcwd");
4997 goto done;
4999 error = got_worktree_open(&worktree, cwd);
5000 if (error)
5001 goto done;
5003 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5004 NULL);
5005 if (error != NULL)
5006 goto done;
5008 error = apply_unveil(got_repo_get_path(repo), 0,
5009 got_worktree_get_root_path(worktree));
5010 if (error)
5011 goto done;
5013 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5014 GOT_OBJ_TYPE_COMMIT, repo);
5015 if (error != NULL) {
5016 struct got_reference *ref;
5017 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5018 goto done;
5019 error = got_ref_open(&ref, repo, argv[0], 0);
5020 if (error != NULL)
5021 goto done;
5022 error = got_ref_resolve(&commit_id, repo, ref);
5023 got_ref_close(ref);
5024 if (error != NULL)
5025 goto done;
5027 error = got_object_id_str(&commit_id_str, commit_id);
5028 if (error)
5029 goto done;
5031 error = got_ref_open(&head_ref, repo,
5032 got_worktree_get_head_ref_name(worktree), 0);
5033 if (error != NULL)
5034 goto done;
5036 error = check_same_branch(commit_id, head_ref, NULL, repo);
5037 if (error) {
5038 if (error->code != GOT_ERR_ANCESTRY)
5039 goto done;
5040 error = NULL;
5041 } else {
5042 error = got_error(GOT_ERR_SAME_BRANCH);
5043 goto done;
5046 error = got_object_open_as_commit(&commit, repo, commit_id);
5047 if (error)
5048 goto done;
5049 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5050 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5051 commit_id, repo, update_progress, &did_something, check_cancelled,
5052 NULL);
5053 if (error != NULL)
5054 goto done;
5056 if (did_something)
5057 printf("Merged commit %s\n", commit_id_str);
5058 done:
5059 if (commit)
5060 got_object_commit_close(commit);
5061 free(commit_id_str);
5062 if (head_ref)
5063 got_ref_close(head_ref);
5064 if (worktree)
5065 got_worktree_close(worktree);
5066 if (repo)
5067 got_repo_close(repo);
5068 return error;
5071 __dead static void
5072 usage_backout(void)
5074 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5075 exit(1);
5078 static const struct got_error *
5079 cmd_backout(int argc, char *argv[])
5081 const struct got_error *error = NULL;
5082 struct got_worktree *worktree = NULL;
5083 struct got_repository *repo = NULL;
5084 char *cwd = NULL, *commit_id_str = NULL;
5085 struct got_object_id *commit_id = NULL;
5086 struct got_commit_object *commit = NULL;
5087 struct got_object_qid *pid;
5088 struct got_reference *head_ref = NULL;
5089 int ch, did_something = 0;
5091 while ((ch = getopt(argc, argv, "")) != -1) {
5092 switch (ch) {
5093 default:
5094 usage_backout();
5095 /* NOTREACHED */
5099 argc -= optind;
5100 argv += optind;
5102 #ifndef PROFILE
5103 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5104 "unveil", NULL) == -1)
5105 err(1, "pledge");
5106 #endif
5107 if (argc != 1)
5108 usage_backout();
5110 cwd = getcwd(NULL, 0);
5111 if (cwd == NULL) {
5112 error = got_error_from_errno("getcwd");
5113 goto done;
5115 error = got_worktree_open(&worktree, cwd);
5116 if (error)
5117 goto done;
5119 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5120 NULL);
5121 if (error != NULL)
5122 goto done;
5124 error = apply_unveil(got_repo_get_path(repo), 0,
5125 got_worktree_get_root_path(worktree));
5126 if (error)
5127 goto done;
5129 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5130 GOT_OBJ_TYPE_COMMIT, repo);
5131 if (error != NULL) {
5132 struct got_reference *ref;
5133 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5134 goto done;
5135 error = got_ref_open(&ref, repo, argv[0], 0);
5136 if (error != NULL)
5137 goto done;
5138 error = got_ref_resolve(&commit_id, repo, ref);
5139 got_ref_close(ref);
5140 if (error != NULL)
5141 goto done;
5143 error = got_object_id_str(&commit_id_str, commit_id);
5144 if (error)
5145 goto done;
5147 error = got_ref_open(&head_ref, repo,
5148 got_worktree_get_head_ref_name(worktree), 0);
5149 if (error != NULL)
5150 goto done;
5152 error = check_same_branch(commit_id, head_ref, NULL, repo);
5153 if (error)
5154 goto done;
5156 error = got_object_open_as_commit(&commit, repo, commit_id);
5157 if (error)
5158 goto done;
5159 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5160 if (pid == NULL) {
5161 error = got_error(GOT_ERR_ROOT_COMMIT);
5162 goto done;
5165 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5166 update_progress, &did_something, check_cancelled, NULL);
5167 if (error != NULL)
5168 goto done;
5170 if (did_something)
5171 printf("Backed out commit %s\n", commit_id_str);
5172 done:
5173 if (commit)
5174 got_object_commit_close(commit);
5175 free(commit_id_str);
5176 if (head_ref)
5177 got_ref_close(head_ref);
5178 if (worktree)
5179 got_worktree_close(worktree);
5180 if (repo)
5181 got_repo_close(repo);
5182 return error;
5185 __dead static void
5186 usage_rebase(void)
5188 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5189 getprogname());
5190 exit(1);
5193 void
5194 trim_logmsg(char *logmsg, int limit)
5196 char *nl;
5197 size_t len;
5199 len = strlen(logmsg);
5200 if (len > limit)
5201 len = limit;
5202 logmsg[len] = '\0';
5203 nl = strchr(logmsg, '\n');
5204 if (nl)
5205 *nl = '\0';
5208 static const struct got_error *
5209 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5211 const struct got_error *err;
5212 char *logmsg0 = NULL;
5213 const char *s;
5215 err = got_object_commit_get_logmsg(&logmsg0, commit);
5216 if (err)
5217 return err;
5219 s = logmsg0;
5220 while (isspace((unsigned char)s[0]))
5221 s++;
5223 *logmsg = strdup(s);
5224 if (*logmsg == NULL) {
5225 err = got_error_from_errno("strdup");
5226 goto done;
5229 trim_logmsg(*logmsg, limit);
5230 done:
5231 free(logmsg0);
5232 return err;
5235 static const struct got_error *
5236 show_rebase_progress(struct got_commit_object *commit,
5237 struct got_object_id *old_id, struct got_object_id *new_id)
5239 const struct got_error *err;
5240 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5242 err = got_object_id_str(&old_id_str, old_id);
5243 if (err)
5244 goto done;
5246 if (new_id) {
5247 err = got_object_id_str(&new_id_str, new_id);
5248 if (err)
5249 goto done;
5252 old_id_str[12] = '\0';
5253 if (new_id_str)
5254 new_id_str[12] = '\0';
5256 err = get_short_logmsg(&logmsg, 42, commit);
5257 if (err)
5258 goto done;
5260 printf("%s -> %s: %s\n", old_id_str,
5261 new_id_str ? new_id_str : "no-op change", logmsg);
5262 done:
5263 free(old_id_str);
5264 free(new_id_str);
5265 return err;
5268 static const struct got_error *
5269 rebase_progress(void *arg, unsigned char status, const char *path)
5271 unsigned char *rebase_status = arg;
5273 while (path[0] == '/')
5274 path++;
5275 printf("%c %s\n", status, path);
5277 if (*rebase_status == GOT_STATUS_CONFLICT)
5278 return NULL;
5279 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5280 *rebase_status = status;
5281 return NULL;
5284 static const struct got_error *
5285 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5286 struct got_reference *branch, struct got_reference *new_base_branch,
5287 struct got_reference *tmp_branch, struct got_repository *repo)
5289 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5290 return got_worktree_rebase_complete(worktree, fileindex,
5291 new_base_branch, tmp_branch, branch, repo);
5294 static const struct got_error *
5295 rebase_commit(struct got_pathlist_head *merged_paths,
5296 struct got_worktree *worktree, struct got_fileindex *fileindex,
5297 struct got_reference *tmp_branch,
5298 struct got_object_id *commit_id, struct got_repository *repo)
5300 const struct got_error *error;
5301 struct got_commit_object *commit;
5302 struct got_object_id *new_commit_id;
5304 error = got_object_open_as_commit(&commit, repo, commit_id);
5305 if (error)
5306 return error;
5308 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5309 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5310 if (error) {
5311 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5312 goto done;
5313 error = show_rebase_progress(commit, commit_id, NULL);
5314 } else {
5315 error = show_rebase_progress(commit, commit_id, new_commit_id);
5316 free(new_commit_id);
5318 done:
5319 got_object_commit_close(commit);
5320 return error;
5323 struct check_path_prefix_arg {
5324 const char *path_prefix;
5325 size_t len;
5326 int errcode;
5329 static const struct got_error *
5330 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5331 struct got_blob_object *blob2, struct got_object_id *id1,
5332 struct got_object_id *id2, const char *path1, const char *path2,
5333 mode_t mode1, mode_t mode2, struct got_repository *repo)
5335 struct check_path_prefix_arg *a = arg;
5337 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5338 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5339 return got_error(a->errcode);
5341 return NULL;
5344 static const struct got_error *
5345 check_path_prefix(struct got_object_id *parent_id,
5346 struct got_object_id *commit_id, const char *path_prefix,
5347 int errcode, struct got_repository *repo)
5349 const struct got_error *err;
5350 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5351 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5352 struct check_path_prefix_arg cpp_arg;
5354 if (got_path_is_root_dir(path_prefix))
5355 return NULL;
5357 err = got_object_open_as_commit(&commit, repo, commit_id);
5358 if (err)
5359 goto done;
5361 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5362 if (err)
5363 goto done;
5365 err = got_object_open_as_tree(&tree1, repo,
5366 got_object_commit_get_tree_id(parent_commit));
5367 if (err)
5368 goto done;
5370 err = got_object_open_as_tree(&tree2, repo,
5371 got_object_commit_get_tree_id(commit));
5372 if (err)
5373 goto done;
5375 cpp_arg.path_prefix = path_prefix;
5376 while (cpp_arg.path_prefix[0] == '/')
5377 cpp_arg.path_prefix++;
5378 cpp_arg.len = strlen(cpp_arg.path_prefix);
5379 cpp_arg.errcode = errcode;
5380 err = got_diff_tree(tree1, tree2, "", "", repo,
5381 check_path_prefix_in_diff, &cpp_arg, 0);
5382 done:
5383 if (tree1)
5384 got_object_tree_close(tree1);
5385 if (tree2)
5386 got_object_tree_close(tree2);
5387 if (commit)
5388 got_object_commit_close(commit);
5389 if (parent_commit)
5390 got_object_commit_close(parent_commit);
5391 return err;
5394 static const struct got_error *
5395 collect_commits(struct got_object_id_queue *commits,
5396 struct got_object_id *initial_commit_id,
5397 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5398 const char *path_prefix, int path_prefix_errcode,
5399 struct got_repository *repo)
5401 const struct got_error *err = NULL;
5402 struct got_commit_graph *graph = NULL;
5403 struct got_object_id *parent_id = NULL;
5404 struct got_object_qid *qid;
5405 struct got_object_id *commit_id = initial_commit_id;
5407 err = got_commit_graph_open(&graph, "/", 1);
5408 if (err)
5409 return err;
5411 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5412 check_cancelled, NULL);
5413 if (err)
5414 goto done;
5415 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5416 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5417 check_cancelled, NULL);
5418 if (err) {
5419 if (err->code == GOT_ERR_ITER_COMPLETED) {
5420 err = got_error_msg(GOT_ERR_ANCESTRY,
5421 "ran out of commits to rebase before "
5422 "youngest common ancestor commit has "
5423 "been reached?!?");
5425 goto done;
5426 } else {
5427 err = check_path_prefix(parent_id, commit_id,
5428 path_prefix, path_prefix_errcode, repo);
5429 if (err)
5430 goto done;
5432 err = got_object_qid_alloc(&qid, commit_id);
5433 if (err)
5434 goto done;
5435 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5436 commit_id = parent_id;
5439 done:
5440 got_commit_graph_close(graph);
5441 return err;
5444 static const struct got_error *
5445 cmd_rebase(int argc, char *argv[])
5447 const struct got_error *error = NULL;
5448 struct got_worktree *worktree = NULL;
5449 struct got_repository *repo = NULL;
5450 struct got_fileindex *fileindex = NULL;
5451 char *cwd = NULL;
5452 struct got_reference *branch = NULL;
5453 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5454 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5455 struct got_object_id *resume_commit_id = NULL;
5456 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5457 struct got_commit_object *commit = NULL;
5458 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5459 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5460 struct got_object_id_queue commits;
5461 struct got_pathlist_head merged_paths;
5462 const struct got_object_id_queue *parent_ids;
5463 struct got_object_qid *qid, *pid;
5465 SIMPLEQ_INIT(&commits);
5466 TAILQ_INIT(&merged_paths);
5468 while ((ch = getopt(argc, argv, "ac")) != -1) {
5469 switch (ch) {
5470 case 'a':
5471 abort_rebase = 1;
5472 break;
5473 case 'c':
5474 continue_rebase = 1;
5475 break;
5476 default:
5477 usage_rebase();
5478 /* NOTREACHED */
5482 argc -= optind;
5483 argv += optind;
5485 #ifndef PROFILE
5486 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5487 "unveil", NULL) == -1)
5488 err(1, "pledge");
5489 #endif
5490 if (abort_rebase && continue_rebase)
5491 usage_rebase();
5492 else if (abort_rebase || continue_rebase) {
5493 if (argc != 0)
5494 usage_rebase();
5495 } else if (argc != 1)
5496 usage_rebase();
5498 cwd = getcwd(NULL, 0);
5499 if (cwd == NULL) {
5500 error = got_error_from_errno("getcwd");
5501 goto done;
5503 error = got_worktree_open(&worktree, cwd);
5504 if (error)
5505 goto done;
5507 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5508 NULL);
5509 if (error != NULL)
5510 goto done;
5512 error = apply_unveil(got_repo_get_path(repo), 0,
5513 got_worktree_get_root_path(worktree));
5514 if (error)
5515 goto done;
5517 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5518 if (error)
5519 goto done;
5521 if (abort_rebase) {
5522 int did_something;
5523 if (!rebase_in_progress) {
5524 error = got_error(GOT_ERR_NOT_REBASING);
5525 goto done;
5527 error = got_worktree_rebase_continue(&resume_commit_id,
5528 &new_base_branch, &tmp_branch, &branch, &fileindex,
5529 worktree, repo);
5530 if (error)
5531 goto done;
5532 printf("Switching work tree to %s\n",
5533 got_ref_get_symref_target(new_base_branch));
5534 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5535 new_base_branch, update_progress, &did_something);
5536 if (error)
5537 goto done;
5538 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5539 goto done; /* nothing else to do */
5542 if (continue_rebase) {
5543 if (!rebase_in_progress) {
5544 error = got_error(GOT_ERR_NOT_REBASING);
5545 goto done;
5547 error = got_worktree_rebase_continue(&resume_commit_id,
5548 &new_base_branch, &tmp_branch, &branch, &fileindex,
5549 worktree, repo);
5550 if (error)
5551 goto done;
5553 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5554 resume_commit_id, repo);
5555 if (error)
5556 goto done;
5558 yca_id = got_object_id_dup(resume_commit_id);
5559 if (yca_id == NULL) {
5560 error = got_error_from_errno("got_object_id_dup");
5561 goto done;
5563 } else {
5564 error = got_ref_open(&branch, repo, argv[0], 0);
5565 if (error != NULL)
5566 goto done;
5569 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5570 if (error)
5571 goto done;
5573 if (!continue_rebase) {
5574 struct got_object_id *base_commit_id;
5576 base_commit_id = got_worktree_get_base_commit_id(worktree);
5577 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5578 base_commit_id, branch_head_commit_id, repo,
5579 check_cancelled, NULL);
5580 if (error)
5581 goto done;
5582 if (yca_id == NULL) {
5583 error = got_error_msg(GOT_ERR_ANCESTRY,
5584 "specified branch shares no common ancestry "
5585 "with work tree's branch");
5586 goto done;
5589 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5590 if (error) {
5591 if (error->code != GOT_ERR_ANCESTRY)
5592 goto done;
5593 error = NULL;
5594 } else {
5595 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5596 "specified branch resolves to a commit which "
5597 "is already contained in work tree's branch");
5598 goto done;
5600 error = got_worktree_rebase_prepare(&new_base_branch,
5601 &tmp_branch, &fileindex, worktree, branch, repo);
5602 if (error)
5603 goto done;
5606 commit_id = branch_head_commit_id;
5607 error = got_object_open_as_commit(&commit, repo, commit_id);
5608 if (error)
5609 goto done;
5611 parent_ids = got_object_commit_get_parent_ids(commit);
5612 pid = SIMPLEQ_FIRST(parent_ids);
5613 if (pid == NULL) {
5614 if (!continue_rebase) {
5615 int did_something;
5616 error = got_worktree_rebase_abort(worktree, fileindex,
5617 repo, new_base_branch, update_progress,
5618 &did_something);
5619 if (error)
5620 goto done;
5621 printf("Rebase of %s aborted\n",
5622 got_ref_get_name(branch));
5624 error = got_error(GOT_ERR_EMPTY_REBASE);
5625 goto done;
5627 error = collect_commits(&commits, commit_id, pid->id,
5628 yca_id, got_worktree_get_path_prefix(worktree),
5629 GOT_ERR_REBASE_PATH, repo);
5630 got_object_commit_close(commit);
5631 commit = NULL;
5632 if (error)
5633 goto done;
5635 if (SIMPLEQ_EMPTY(&commits)) {
5636 if (continue_rebase) {
5637 error = rebase_complete(worktree, fileindex,
5638 branch, new_base_branch, tmp_branch, repo);
5639 goto done;
5640 } else {
5641 /* Fast-forward the reference of the branch. */
5642 struct got_object_id *new_head_commit_id;
5643 char *id_str;
5644 error = got_ref_resolve(&new_head_commit_id, repo,
5645 new_base_branch);
5646 if (error)
5647 goto done;
5648 error = got_object_id_str(&id_str, new_head_commit_id);
5649 printf("Forwarding %s to commit %s\n",
5650 got_ref_get_name(branch), id_str);
5651 free(id_str);
5652 error = got_ref_change_ref(branch,
5653 new_head_commit_id);
5654 if (error)
5655 goto done;
5659 pid = NULL;
5660 SIMPLEQ_FOREACH(qid, &commits, entry) {
5661 commit_id = qid->id;
5662 parent_id = pid ? pid->id : yca_id;
5663 pid = qid;
5665 error = got_worktree_rebase_merge_files(&merged_paths,
5666 worktree, fileindex, parent_id, commit_id, repo,
5667 rebase_progress, &rebase_status, check_cancelled, NULL);
5668 if (error)
5669 goto done;
5671 if (rebase_status == GOT_STATUS_CONFLICT) {
5672 got_worktree_rebase_pathlist_free(&merged_paths);
5673 break;
5676 error = rebase_commit(&merged_paths, worktree, fileindex,
5677 tmp_branch, commit_id, repo);
5678 got_worktree_rebase_pathlist_free(&merged_paths);
5679 if (error)
5680 goto done;
5683 if (rebase_status == GOT_STATUS_CONFLICT) {
5684 error = got_worktree_rebase_postpone(worktree, fileindex);
5685 if (error)
5686 goto done;
5687 error = got_error_msg(GOT_ERR_CONFLICTS,
5688 "conflicts must be resolved before rebasing can continue");
5689 } else
5690 error = rebase_complete(worktree, fileindex, branch,
5691 new_base_branch, tmp_branch, repo);
5692 done:
5693 got_object_id_queue_free(&commits);
5694 free(branch_head_commit_id);
5695 free(resume_commit_id);
5696 free(yca_id);
5697 if (commit)
5698 got_object_commit_close(commit);
5699 if (branch)
5700 got_ref_close(branch);
5701 if (new_base_branch)
5702 got_ref_close(new_base_branch);
5703 if (tmp_branch)
5704 got_ref_close(tmp_branch);
5705 if (worktree)
5706 got_worktree_close(worktree);
5707 if (repo)
5708 got_repo_close(repo);
5709 return error;
5712 __dead static void
5713 usage_histedit(void)
5715 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5716 getprogname());
5717 exit(1);
5720 #define GOT_HISTEDIT_PICK 'p'
5721 #define GOT_HISTEDIT_EDIT 'e'
5722 #define GOT_HISTEDIT_FOLD 'f'
5723 #define GOT_HISTEDIT_DROP 'd'
5724 #define GOT_HISTEDIT_MESG 'm'
5726 static struct got_histedit_cmd {
5727 unsigned char code;
5728 const char *name;
5729 const char *desc;
5730 } got_histedit_cmds[] = {
5731 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5732 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5733 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5734 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5735 { GOT_HISTEDIT_MESG, "mesg",
5736 "single-line log message for commit above (open editor if empty)" },
5739 struct got_histedit_list_entry {
5740 TAILQ_ENTRY(got_histedit_list_entry) entry;
5741 struct got_object_id *commit_id;
5742 const struct got_histedit_cmd *cmd;
5743 char *logmsg;
5745 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5747 static const struct got_error *
5748 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5749 FILE *f, struct got_repository *repo)
5751 const struct got_error *err = NULL;
5752 char *logmsg = NULL, *id_str = NULL;
5753 struct got_commit_object *commit = NULL;
5754 int n;
5756 err = got_object_open_as_commit(&commit, repo, commit_id);
5757 if (err)
5758 goto done;
5760 err = get_short_logmsg(&logmsg, 34, commit);
5761 if (err)
5762 goto done;
5764 err = got_object_id_str(&id_str, commit_id);
5765 if (err)
5766 goto done;
5768 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5769 if (n < 0)
5770 err = got_ferror(f, GOT_ERR_IO);
5771 done:
5772 if (commit)
5773 got_object_commit_close(commit);
5774 free(id_str);
5775 free(logmsg);
5776 return err;
5779 static const struct got_error *
5780 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5781 struct got_repository *repo)
5783 const struct got_error *err = NULL;
5784 struct got_object_qid *qid;
5786 if (SIMPLEQ_EMPTY(commits))
5787 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5789 SIMPLEQ_FOREACH(qid, commits, entry) {
5790 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5791 f, repo);
5792 if (err)
5793 break;
5796 return err;
5799 static const struct got_error *
5800 write_cmd_list(FILE *f)
5802 const struct got_error *err = NULL;
5803 int n, i;
5805 n = fprintf(f, "# Available histedit commands:\n");
5806 if (n < 0)
5807 return got_ferror(f, GOT_ERR_IO);
5809 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5810 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5811 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5812 cmd->desc);
5813 if (n < 0) {
5814 err = got_ferror(f, GOT_ERR_IO);
5815 break;
5818 n = fprintf(f, "# Commits will be processed in order from top to "
5819 "bottom of this file.\n");
5820 if (n < 0)
5821 return got_ferror(f, GOT_ERR_IO);
5822 return err;
5825 static const struct got_error *
5826 histedit_syntax_error(int lineno)
5828 static char msg[42];
5829 int ret;
5831 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5832 lineno);
5833 if (ret == -1 || ret >= sizeof(msg))
5834 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5836 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5839 static const struct got_error *
5840 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5841 char *logmsg, struct got_repository *repo)
5843 const struct got_error *err;
5844 struct got_commit_object *folded_commit = NULL;
5845 char *id_str, *folded_logmsg = NULL;
5847 err = got_object_id_str(&id_str, hle->commit_id);
5848 if (err)
5849 return err;
5851 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5852 if (err)
5853 goto done;
5855 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5856 if (err)
5857 goto done;
5858 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5859 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5860 folded_logmsg) == -1) {
5861 err = got_error_from_errno("asprintf");
5863 done:
5864 if (folded_commit)
5865 got_object_commit_close(folded_commit);
5866 free(id_str);
5867 free(folded_logmsg);
5868 return err;
5871 static struct got_histedit_list_entry *
5872 get_folded_commits(struct got_histedit_list_entry *hle)
5874 struct got_histedit_list_entry *prev, *folded = NULL;
5876 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5877 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5878 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5879 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5880 folded = prev;
5881 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5884 return folded;
5887 static const struct got_error *
5888 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5889 struct got_repository *repo)
5891 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5892 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5893 const struct got_error *err = NULL;
5894 struct got_commit_object *commit = NULL;
5895 int fd;
5896 struct got_histedit_list_entry *folded = NULL;
5898 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5899 if (err)
5900 return err;
5902 folded = get_folded_commits(hle);
5903 if (folded) {
5904 while (folded != hle) {
5905 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5906 folded = TAILQ_NEXT(folded, entry);
5907 continue;
5909 err = append_folded_commit_msg(&new_msg, folded,
5910 logmsg, repo);
5911 if (err)
5912 goto done;
5913 free(logmsg);
5914 logmsg = new_msg;
5915 folded = TAILQ_NEXT(folded, entry);
5919 err = got_object_id_str(&id_str, hle->commit_id);
5920 if (err)
5921 goto done;
5922 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5923 if (err)
5924 goto done;
5925 if (asprintf(&new_msg,
5926 "%s\n# original log message of commit %s: %s",
5927 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5928 err = got_error_from_errno("asprintf");
5929 goto done;
5931 free(logmsg);
5932 logmsg = new_msg;
5934 err = got_object_id_str(&id_str, hle->commit_id);
5935 if (err)
5936 goto done;
5938 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5939 if (err)
5940 goto done;
5942 dprintf(fd, logmsg);
5943 close(fd);
5945 err = get_editor(&editor);
5946 if (err)
5947 goto done;
5949 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5950 if (err) {
5951 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5952 goto done;
5953 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5955 done:
5956 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5957 err = got_error_from_errno2("unlink", logmsg_path);
5958 free(logmsg_path);
5959 free(logmsg);
5960 free(orig_logmsg);
5961 free(editor);
5962 if (commit)
5963 got_object_commit_close(commit);
5964 return err;
5967 static const struct got_error *
5968 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5969 FILE *f, struct got_repository *repo)
5971 const struct got_error *err = NULL;
5972 char *line = NULL, *p, *end;
5973 size_t size;
5974 ssize_t len;
5975 int lineno = 0, i;
5976 const struct got_histedit_cmd *cmd;
5977 struct got_object_id *commit_id = NULL;
5978 struct got_histedit_list_entry *hle = NULL;
5980 for (;;) {
5981 len = getline(&line, &size, f);
5982 if (len == -1) {
5983 const struct got_error *getline_err;
5984 if (feof(f))
5985 break;
5986 getline_err = got_error_from_errno("getline");
5987 err = got_ferror(f, getline_err->code);
5988 break;
5990 lineno++;
5991 p = line;
5992 while (isspace((unsigned char)p[0]))
5993 p++;
5994 if (p[0] == '#' || p[0] == '\0') {
5995 free(line);
5996 line = NULL;
5997 continue;
5999 cmd = NULL;
6000 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6001 cmd = &got_histedit_cmds[i];
6002 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6003 isspace((unsigned char)p[strlen(cmd->name)])) {
6004 p += strlen(cmd->name);
6005 break;
6007 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6008 p++;
6009 break;
6012 if (i == nitems(got_histedit_cmds)) {
6013 err = histedit_syntax_error(lineno);
6014 break;
6016 while (isspace((unsigned char)p[0]))
6017 p++;
6018 if (cmd->code == GOT_HISTEDIT_MESG) {
6019 if (hle == NULL || hle->logmsg != NULL) {
6020 err = got_error(GOT_ERR_HISTEDIT_CMD);
6021 break;
6023 if (p[0] == '\0') {
6024 err = histedit_edit_logmsg(hle, repo);
6025 if (err)
6026 break;
6027 } else {
6028 hle->logmsg = strdup(p);
6029 if (hle->logmsg == NULL) {
6030 err = got_error_from_errno("strdup");
6031 break;
6034 free(line);
6035 line = NULL;
6036 continue;
6037 } else {
6038 end = p;
6039 while (end[0] && !isspace((unsigned char)end[0]))
6040 end++;
6041 *end = '\0';
6043 err = got_object_resolve_id_str(&commit_id, repo, p);
6044 if (err) {
6045 /* override error code */
6046 err = histedit_syntax_error(lineno);
6047 break;
6050 hle = malloc(sizeof(*hle));
6051 if (hle == NULL) {
6052 err = got_error_from_errno("malloc");
6053 break;
6055 hle->cmd = cmd;
6056 hle->commit_id = commit_id;
6057 hle->logmsg = NULL;
6058 commit_id = NULL;
6059 free(line);
6060 line = NULL;
6061 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6064 free(line);
6065 free(commit_id);
6066 return err;
6069 static const struct got_error *
6070 histedit_check_script(struct got_histedit_list *histedit_cmds,
6071 struct got_object_id_queue *commits, struct got_repository *repo)
6073 const struct got_error *err = NULL;
6074 struct got_object_qid *qid;
6075 struct got_histedit_list_entry *hle;
6076 static char msg[80];
6077 char *id_str;
6079 if (TAILQ_EMPTY(histedit_cmds))
6080 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6081 "histedit script contains no commands");
6082 if (SIMPLEQ_EMPTY(commits))
6083 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6085 SIMPLEQ_FOREACH(qid, commits, entry) {
6086 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6087 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6088 break;
6090 if (hle == NULL) {
6091 err = got_object_id_str(&id_str, qid->id);
6092 if (err)
6093 return err;
6094 snprintf(msg, sizeof(msg),
6095 "commit %s missing from histedit script", id_str);
6096 free(id_str);
6097 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6101 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6102 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6103 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6104 "last commit in histedit script cannot be folded");
6106 return NULL;
6109 static const struct got_error *
6110 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6111 const char *path, struct got_object_id_queue *commits,
6112 struct got_repository *repo)
6114 const struct got_error *err = NULL;
6115 char *editor;
6116 FILE *f = NULL;
6118 err = get_editor(&editor);
6119 if (err)
6120 return err;
6122 if (spawn_editor(editor, path) == -1) {
6123 err = got_error_from_errno("failed spawning editor");
6124 goto done;
6127 f = fopen(path, "r");
6128 if (f == NULL) {
6129 err = got_error_from_errno("fopen");
6130 goto done;
6132 err = histedit_parse_list(histedit_cmds, f, repo);
6133 if (err)
6134 goto done;
6136 err = histedit_check_script(histedit_cmds, commits, repo);
6137 done:
6138 if (f && fclose(f) != 0 && err == NULL)
6139 err = got_error_from_errno("fclose");
6140 free(editor);
6141 return err;
6144 static const struct got_error *
6145 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6146 struct got_object_id_queue *, const char *, struct got_repository *);
6148 static const struct got_error *
6149 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6150 struct got_object_id_queue *commits, struct got_repository *repo)
6152 const struct got_error *err;
6153 FILE *f = NULL;
6154 char *path = NULL;
6156 err = got_opentemp_named(&path, &f, "got-histedit");
6157 if (err)
6158 return err;
6160 err = write_cmd_list(f);
6161 if (err)
6162 goto done;
6164 err = histedit_write_commit_list(commits, f, repo);
6165 if (err)
6166 goto done;
6168 if (fclose(f) != 0) {
6169 err = got_error_from_errno("fclose");
6170 goto done;
6172 f = NULL;
6174 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6175 if (err) {
6176 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6177 err->code != GOT_ERR_HISTEDIT_CMD)
6178 goto done;
6179 err = histedit_edit_list_retry(histedit_cmds, err,
6180 commits, path, repo);
6182 done:
6183 if (f && fclose(f) != 0 && err == NULL)
6184 err = got_error_from_errno("fclose");
6185 if (path && unlink(path) != 0 && err == NULL)
6186 err = got_error_from_errno2("unlink", path);
6187 free(path);
6188 return err;
6191 static const struct got_error *
6192 histedit_save_list(struct got_histedit_list *histedit_cmds,
6193 struct got_worktree *worktree, struct got_repository *repo)
6195 const struct got_error *err = NULL;
6196 char *path = NULL;
6197 FILE *f = NULL;
6198 struct got_histedit_list_entry *hle;
6199 struct got_commit_object *commit = NULL;
6201 err = got_worktree_get_histedit_script_path(&path, worktree);
6202 if (err)
6203 return err;
6205 f = fopen(path, "w");
6206 if (f == NULL) {
6207 err = got_error_from_errno2("fopen", path);
6208 goto done;
6210 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6211 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6212 repo);
6213 if (err)
6214 break;
6216 if (hle->logmsg) {
6217 int n = fprintf(f, "%c %s\n",
6218 GOT_HISTEDIT_MESG, hle->logmsg);
6219 if (n < 0) {
6220 err = got_ferror(f, GOT_ERR_IO);
6221 break;
6225 done:
6226 if (f && fclose(f) != 0 && err == NULL)
6227 err = got_error_from_errno("fclose");
6228 free(path);
6229 if (commit)
6230 got_object_commit_close(commit);
6231 return err;
6234 void
6235 histedit_free_list(struct got_histedit_list *histedit_cmds)
6237 struct got_histedit_list_entry *hle;
6239 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6240 TAILQ_REMOVE(histedit_cmds, hle, entry);
6241 free(hle);
6245 static const struct got_error *
6246 histedit_load_list(struct got_histedit_list *histedit_cmds,
6247 const char *path, struct got_repository *repo)
6249 const struct got_error *err = NULL;
6250 FILE *f = NULL;
6252 f = fopen(path, "r");
6253 if (f == NULL) {
6254 err = got_error_from_errno2("fopen", path);
6255 goto done;
6258 err = histedit_parse_list(histedit_cmds, f, repo);
6259 done:
6260 if (f && fclose(f) != 0 && err == NULL)
6261 err = got_error_from_errno("fclose");
6262 return err;
6265 static const struct got_error *
6266 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6267 const struct got_error *edit_err, struct got_object_id_queue *commits,
6268 const char *path, struct got_repository *repo)
6270 const struct got_error *err = NULL, *prev_err = edit_err;
6271 int resp = ' ';
6273 while (resp != 'c' && resp != 'r' && resp != 'a') {
6274 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6275 "or (a)bort: ", getprogname(), prev_err->msg);
6276 resp = getchar();
6277 if (resp == '\n')
6278 resp = getchar();
6279 if (resp == 'c') {
6280 histedit_free_list(histedit_cmds);
6281 err = histedit_run_editor(histedit_cmds, path, commits,
6282 repo);
6283 if (err) {
6284 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6285 err->code != GOT_ERR_HISTEDIT_CMD)
6286 break;
6287 prev_err = err;
6288 resp = ' ';
6289 continue;
6291 break;
6292 } else if (resp == 'r') {
6293 histedit_free_list(histedit_cmds);
6294 err = histedit_edit_script(histedit_cmds,
6295 commits, repo);
6296 if (err) {
6297 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6298 err->code != GOT_ERR_HISTEDIT_CMD)
6299 break;
6300 prev_err = err;
6301 resp = ' ';
6302 continue;
6304 break;
6305 } else if (resp == 'a') {
6306 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6307 break;
6308 } else
6309 printf("invalid response '%c'\n", resp);
6312 return err;
6315 static const struct got_error *
6316 histedit_complete(struct got_worktree *worktree,
6317 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6318 struct got_reference *branch, struct got_repository *repo)
6320 printf("Switching work tree to %s\n",
6321 got_ref_get_symref_target(branch));
6322 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6323 branch, repo);
6326 static const struct got_error *
6327 show_histedit_progress(struct got_commit_object *commit,
6328 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6330 const struct got_error *err;
6331 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6333 err = got_object_id_str(&old_id_str, hle->commit_id);
6334 if (err)
6335 goto done;
6337 if (new_id) {
6338 err = got_object_id_str(&new_id_str, new_id);
6339 if (err)
6340 goto done;
6343 old_id_str[12] = '\0';
6344 if (new_id_str)
6345 new_id_str[12] = '\0';
6347 if (hle->logmsg) {
6348 logmsg = strdup(hle->logmsg);
6349 if (logmsg == NULL) {
6350 err = got_error_from_errno("strdup");
6351 goto done;
6353 trim_logmsg(logmsg, 42);
6354 } else {
6355 err = get_short_logmsg(&logmsg, 42, commit);
6356 if (err)
6357 goto done;
6360 switch (hle->cmd->code) {
6361 case GOT_HISTEDIT_PICK:
6362 case GOT_HISTEDIT_EDIT:
6363 printf("%s -> %s: %s\n", old_id_str,
6364 new_id_str ? new_id_str : "no-op change", logmsg);
6365 break;
6366 case GOT_HISTEDIT_DROP:
6367 case GOT_HISTEDIT_FOLD:
6368 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6369 logmsg);
6370 break;
6371 default:
6372 break;
6374 done:
6375 free(old_id_str);
6376 free(new_id_str);
6377 return err;
6380 static const struct got_error *
6381 histedit_commit(struct got_pathlist_head *merged_paths,
6382 struct got_worktree *worktree, struct got_fileindex *fileindex,
6383 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6384 struct got_repository *repo)
6386 const struct got_error *err;
6387 struct got_commit_object *commit;
6388 struct got_object_id *new_commit_id;
6390 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6391 && hle->logmsg == NULL) {
6392 err = histedit_edit_logmsg(hle, repo);
6393 if (err)
6394 return err;
6397 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6398 if (err)
6399 return err;
6401 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6402 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6403 hle->logmsg, repo);
6404 if (err) {
6405 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6406 goto done;
6407 err = show_histedit_progress(commit, hle, NULL);
6408 } else {
6409 err = show_histedit_progress(commit, hle, new_commit_id);
6410 free(new_commit_id);
6412 done:
6413 got_object_commit_close(commit);
6414 return err;
6417 static const struct got_error *
6418 histedit_skip_commit(struct got_histedit_list_entry *hle,
6419 struct got_worktree *worktree, struct got_repository *repo)
6421 const struct got_error *error;
6422 struct got_commit_object *commit;
6424 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6425 repo);
6426 if (error)
6427 return error;
6429 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6430 if (error)
6431 return error;
6433 error = show_histedit_progress(commit, hle, NULL);
6434 got_object_commit_close(commit);
6435 return error;
6438 static const struct got_error *
6439 cmd_histedit(int argc, char *argv[])
6441 const struct got_error *error = NULL;
6442 struct got_worktree *worktree = NULL;
6443 struct got_fileindex *fileindex = NULL;
6444 struct got_repository *repo = NULL;
6445 char *cwd = NULL;
6446 struct got_reference *branch = NULL;
6447 struct got_reference *tmp_branch = NULL;
6448 struct got_object_id *resume_commit_id = NULL;
6449 struct got_object_id *base_commit_id = NULL;
6450 struct got_object_id *head_commit_id = NULL;
6451 struct got_commit_object *commit = NULL;
6452 int ch, rebase_in_progress = 0, did_something;
6453 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6454 const char *edit_script_path = NULL;
6455 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6456 struct got_object_id_queue commits;
6457 struct got_pathlist_head merged_paths;
6458 const struct got_object_id_queue *parent_ids;
6459 struct got_object_qid *pid;
6460 struct got_histedit_list histedit_cmds;
6461 struct got_histedit_list_entry *hle;
6463 SIMPLEQ_INIT(&commits);
6464 TAILQ_INIT(&histedit_cmds);
6465 TAILQ_INIT(&merged_paths);
6467 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6468 switch (ch) {
6469 case 'a':
6470 abort_edit = 1;
6471 break;
6472 case 'c':
6473 continue_edit = 1;
6474 break;
6475 case 'F':
6476 edit_script_path = optarg;
6477 break;
6478 default:
6479 usage_histedit();
6480 /* NOTREACHED */
6484 argc -= optind;
6485 argv += optind;
6487 #ifndef PROFILE
6488 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6489 "unveil", NULL) == -1)
6490 err(1, "pledge");
6491 #endif
6492 if (abort_edit && continue_edit)
6493 usage_histedit();
6494 if (argc != 0)
6495 usage_histedit();
6498 * This command cannot apply unveil(2) in all cases because the
6499 * user may choose to run an editor to edit the histedit script
6500 * and to edit individual commit log messages.
6501 * unveil(2) traverses exec(2); if an editor is used we have to
6502 * apply unveil after edit script and log messages have been written.
6503 * XXX TODO: Make use of unveil(2) where possible.
6506 cwd = getcwd(NULL, 0);
6507 if (cwd == NULL) {
6508 error = got_error_from_errno("getcwd");
6509 goto done;
6511 error = got_worktree_open(&worktree, cwd);
6512 if (error)
6513 goto done;
6515 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6516 NULL);
6517 if (error != NULL)
6518 goto done;
6520 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6521 if (error)
6522 goto done;
6523 if (rebase_in_progress) {
6524 error = got_error(GOT_ERR_REBASING);
6525 goto done;
6528 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6529 if (error)
6530 goto done;
6532 if (edit_in_progress && abort_edit) {
6533 error = got_worktree_histedit_continue(&resume_commit_id,
6534 &tmp_branch, &branch, &base_commit_id, &fileindex,
6535 worktree, repo);
6536 if (error)
6537 goto done;
6538 printf("Switching work tree to %s\n",
6539 got_ref_get_symref_target(branch));
6540 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6541 branch, base_commit_id, update_progress, &did_something);
6542 if (error)
6543 goto done;
6544 printf("Histedit of %s aborted\n",
6545 got_ref_get_symref_target(branch));
6546 goto done; /* nothing else to do */
6547 } else if (abort_edit) {
6548 error = got_error(GOT_ERR_NOT_HISTEDIT);
6549 goto done;
6552 if (continue_edit) {
6553 char *path;
6555 if (!edit_in_progress) {
6556 error = got_error(GOT_ERR_NOT_HISTEDIT);
6557 goto done;
6560 error = got_worktree_get_histedit_script_path(&path, worktree);
6561 if (error)
6562 goto done;
6564 error = histedit_load_list(&histedit_cmds, path, repo);
6565 free(path);
6566 if (error)
6567 goto done;
6569 error = got_worktree_histedit_continue(&resume_commit_id,
6570 &tmp_branch, &branch, &base_commit_id, &fileindex,
6571 worktree, repo);
6572 if (error)
6573 goto done;
6575 error = got_ref_resolve(&head_commit_id, repo, branch);
6576 if (error)
6577 goto done;
6579 error = got_object_open_as_commit(&commit, repo,
6580 head_commit_id);
6581 if (error)
6582 goto done;
6583 parent_ids = got_object_commit_get_parent_ids(commit);
6584 pid = SIMPLEQ_FIRST(parent_ids);
6585 if (pid == NULL) {
6586 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6587 goto done;
6589 error = collect_commits(&commits, head_commit_id, pid->id,
6590 base_commit_id, got_worktree_get_path_prefix(worktree),
6591 GOT_ERR_HISTEDIT_PATH, repo);
6592 got_object_commit_close(commit);
6593 commit = NULL;
6594 if (error)
6595 goto done;
6596 } else {
6597 if (edit_in_progress) {
6598 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6599 goto done;
6602 error = got_ref_open(&branch, repo,
6603 got_worktree_get_head_ref_name(worktree), 0);
6604 if (error != NULL)
6605 goto done;
6607 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6608 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6609 "will not edit commit history of a branch outside "
6610 "the \"refs/heads/\" reference namespace");
6611 goto done;
6614 error = got_ref_resolve(&head_commit_id, repo, branch);
6615 got_ref_close(branch);
6616 branch = NULL;
6617 if (error)
6618 goto done;
6620 error = got_object_open_as_commit(&commit, repo,
6621 head_commit_id);
6622 if (error)
6623 goto done;
6624 parent_ids = got_object_commit_get_parent_ids(commit);
6625 pid = SIMPLEQ_FIRST(parent_ids);
6626 if (pid == NULL) {
6627 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6628 goto done;
6630 error = collect_commits(&commits, head_commit_id, pid->id,
6631 got_worktree_get_base_commit_id(worktree),
6632 got_worktree_get_path_prefix(worktree),
6633 GOT_ERR_HISTEDIT_PATH, repo);
6634 got_object_commit_close(commit);
6635 commit = NULL;
6636 if (error)
6637 goto done;
6639 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6640 &base_commit_id, &fileindex, worktree, repo);
6641 if (error)
6642 goto done;
6644 if (edit_script_path) {
6645 error = histedit_load_list(&histedit_cmds,
6646 edit_script_path, repo);
6647 if (error) {
6648 got_worktree_histedit_abort(worktree, fileindex,
6649 repo, branch, base_commit_id,
6650 update_progress, &did_something);
6651 goto done;
6653 } else {
6654 error = histedit_edit_script(&histedit_cmds, &commits,
6655 repo);
6656 if (error) {
6657 got_worktree_histedit_abort(worktree, fileindex,
6658 repo, branch, base_commit_id,
6659 update_progress, &did_something);
6660 goto done;
6665 error = histedit_save_list(&histedit_cmds, worktree,
6666 repo);
6667 if (error) {
6668 got_worktree_histedit_abort(worktree, fileindex,
6669 repo, branch, base_commit_id,
6670 update_progress, &did_something);
6671 goto done;
6676 error = histedit_check_script(&histedit_cmds, &commits, repo);
6677 if (error)
6678 goto done;
6680 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6681 if (resume_commit_id) {
6682 if (got_object_id_cmp(hle->commit_id,
6683 resume_commit_id) != 0)
6684 continue;
6686 resume_commit_id = NULL;
6687 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6688 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6689 error = histedit_skip_commit(hle, worktree,
6690 repo);
6691 } else {
6692 error = histedit_commit(NULL, worktree,
6693 fileindex, tmp_branch, hle, repo);
6695 if (error)
6696 goto done;
6697 continue;
6700 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6701 error = histedit_skip_commit(hle, worktree, repo);
6702 if (error)
6703 goto done;
6704 continue;
6707 error = got_object_open_as_commit(&commit, repo,
6708 hle->commit_id);
6709 if (error)
6710 goto done;
6711 parent_ids = got_object_commit_get_parent_ids(commit);
6712 pid = SIMPLEQ_FIRST(parent_ids);
6714 error = got_worktree_histedit_merge_files(&merged_paths,
6715 worktree, fileindex, pid->id, hle->commit_id, repo,
6716 rebase_progress, &rebase_status, check_cancelled, NULL);
6717 if (error)
6718 goto done;
6719 got_object_commit_close(commit);
6720 commit = NULL;
6722 if (rebase_status == GOT_STATUS_CONFLICT) {
6723 got_worktree_rebase_pathlist_free(&merged_paths);
6724 break;
6727 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6728 char *id_str;
6729 error = got_object_id_str(&id_str, hle->commit_id);
6730 if (error)
6731 goto done;
6732 printf("Stopping histedit for amending commit %s\n",
6733 id_str);
6734 free(id_str);
6735 got_worktree_rebase_pathlist_free(&merged_paths);
6736 error = got_worktree_histedit_postpone(worktree,
6737 fileindex);
6738 goto done;
6741 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6742 error = histedit_skip_commit(hle, worktree, repo);
6743 if (error)
6744 goto done;
6745 continue;
6748 error = histedit_commit(&merged_paths, worktree, fileindex,
6749 tmp_branch, hle, repo);
6750 got_worktree_rebase_pathlist_free(&merged_paths);
6751 if (error)
6752 goto done;
6755 if (rebase_status == GOT_STATUS_CONFLICT) {
6756 error = got_worktree_histedit_postpone(worktree, fileindex);
6757 if (error)
6758 goto done;
6759 error = got_error_msg(GOT_ERR_CONFLICTS,
6760 "conflicts must be resolved before rebasing can continue");
6761 } else
6762 error = histedit_complete(worktree, fileindex, tmp_branch,
6763 branch, repo);
6764 done:
6765 got_object_id_queue_free(&commits);
6766 histedit_free_list(&histedit_cmds);
6767 free(head_commit_id);
6768 free(base_commit_id);
6769 free(resume_commit_id);
6770 if (commit)
6771 got_object_commit_close(commit);
6772 if (branch)
6773 got_ref_close(branch);
6774 if (tmp_branch)
6775 got_ref_close(tmp_branch);
6776 if (worktree)
6777 got_worktree_close(worktree);
6778 if (repo)
6779 got_repo_close(repo);
6780 return error;
6783 __dead static void
6784 usage_integrate(void)
6786 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6787 exit(1);
6790 static const struct got_error *
6791 cmd_integrate(int argc, char *argv[])
6793 const struct got_error *error = NULL;
6794 struct got_repository *repo = NULL;
6795 struct got_worktree *worktree = NULL;
6796 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6797 const char *branch_arg = NULL;
6798 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6799 struct got_fileindex *fileindex = NULL;
6800 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6801 int ch, did_something = 0;
6803 while ((ch = getopt(argc, argv, "")) != -1) {
6804 switch (ch) {
6805 default:
6806 usage_integrate();
6807 /* NOTREACHED */
6811 argc -= optind;
6812 argv += optind;
6814 if (argc != 1)
6815 usage_integrate();
6816 branch_arg = argv[0];
6818 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6819 "unveil", NULL) == -1)
6820 err(1, "pledge");
6822 cwd = getcwd(NULL, 0);
6823 if (cwd == NULL) {
6824 error = got_error_from_errno("getcwd");
6825 goto done;
6828 error = got_worktree_open(&worktree, cwd);
6829 if (error)
6830 goto done;
6832 error = check_rebase_or_histedit_in_progress(worktree);
6833 if (error)
6834 goto done;
6836 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6837 NULL);
6838 if (error != NULL)
6839 goto done;
6841 error = apply_unveil(got_repo_get_path(repo), 0,
6842 got_worktree_get_root_path(worktree));
6843 if (error)
6844 goto done;
6846 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6847 error = got_error_from_errno("asprintf");
6848 goto done;
6851 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6852 &base_branch_ref, worktree, refname, repo);
6853 if (error)
6854 goto done;
6856 refname = strdup(got_ref_get_name(branch_ref));
6857 if (refname == NULL) {
6858 error = got_error_from_errno("strdup");
6859 got_worktree_integrate_abort(worktree, fileindex, repo,
6860 branch_ref, base_branch_ref);
6861 goto done;
6863 base_refname = strdup(got_ref_get_name(base_branch_ref));
6864 if (base_refname == NULL) {
6865 error = got_error_from_errno("strdup");
6866 got_worktree_integrate_abort(worktree, fileindex, repo,
6867 branch_ref, base_branch_ref);
6868 goto done;
6871 error = got_ref_resolve(&commit_id, repo, branch_ref);
6872 if (error)
6873 goto done;
6875 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6876 if (error)
6877 goto done;
6879 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6880 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6881 "specified branch has already been integrated");
6882 got_worktree_integrate_abort(worktree, fileindex, repo,
6883 branch_ref, base_branch_ref);
6884 goto done;
6887 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6888 if (error) {
6889 if (error->code == GOT_ERR_ANCESTRY)
6890 error = got_error(GOT_ERR_REBASE_REQUIRED);
6891 got_worktree_integrate_abort(worktree, fileindex, repo,
6892 branch_ref, base_branch_ref);
6893 goto done;
6896 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6897 branch_ref, base_branch_ref, update_progress, &did_something,
6898 check_cancelled, NULL);
6899 if (error)
6900 goto done;
6902 printf("Integrated %s into %s\n", refname, base_refname);
6903 done:
6904 if (repo)
6905 got_repo_close(repo);
6906 if (worktree)
6907 got_worktree_close(worktree);
6908 free(cwd);
6909 free(base_commit_id);
6910 free(commit_id);
6911 free(refname);
6912 free(base_refname);
6913 return error;
6916 __dead static void
6917 usage_stage(void)
6919 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6920 "[file-path ...]\n",
6921 getprogname());
6922 exit(1);
6925 static const struct got_error *
6926 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6927 const char *path, struct got_object_id *blob_id,
6928 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6929 int dirfd, const char *de_name)
6931 const struct got_error *err = NULL;
6932 char *id_str = NULL;
6934 if (staged_status != GOT_STATUS_ADD &&
6935 staged_status != GOT_STATUS_MODIFY &&
6936 staged_status != GOT_STATUS_DELETE)
6937 return NULL;
6939 if (staged_status == GOT_STATUS_ADD ||
6940 staged_status == GOT_STATUS_MODIFY)
6941 err = got_object_id_str(&id_str, staged_blob_id);
6942 else
6943 err = got_object_id_str(&id_str, blob_id);
6944 if (err)
6945 return err;
6947 printf("%s %c %s\n", id_str, staged_status, path);
6948 free(id_str);
6949 return NULL;
6952 static const struct got_error *
6953 cmd_stage(int argc, char *argv[])
6955 const struct got_error *error = NULL;
6956 struct got_repository *repo = NULL;
6957 struct got_worktree *worktree = NULL;
6958 char *cwd = NULL;
6959 struct got_pathlist_head paths;
6960 struct got_pathlist_entry *pe;
6961 int ch, list_stage = 0, pflag = 0;
6962 FILE *patch_script_file = NULL;
6963 const char *patch_script_path = NULL;
6964 struct choose_patch_arg cpa;
6966 TAILQ_INIT(&paths);
6968 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6969 switch (ch) {
6970 case 'l':
6971 list_stage = 1;
6972 break;
6973 case 'p':
6974 pflag = 1;
6975 break;
6976 case 'F':
6977 patch_script_path = optarg;
6978 break;
6979 default:
6980 usage_stage();
6981 /* NOTREACHED */
6985 argc -= optind;
6986 argv += optind;
6988 #ifndef PROFILE
6989 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6990 "unveil", NULL) == -1)
6991 err(1, "pledge");
6992 #endif
6993 if (list_stage && (pflag || patch_script_path))
6994 errx(1, "-l option cannot be used with other options");
6995 if (patch_script_path && !pflag)
6996 errx(1, "-F option can only be used together with -p option");
6998 cwd = getcwd(NULL, 0);
6999 if (cwd == NULL) {
7000 error = got_error_from_errno("getcwd");
7001 goto done;
7004 error = got_worktree_open(&worktree, cwd);
7005 if (error)
7006 goto done;
7008 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7009 NULL);
7010 if (error != NULL)
7011 goto done;
7013 if (patch_script_path) {
7014 patch_script_file = fopen(patch_script_path, "r");
7015 if (patch_script_file == NULL) {
7016 error = got_error_from_errno2("fopen",
7017 patch_script_path);
7018 goto done;
7021 error = apply_unveil(got_repo_get_path(repo), 0,
7022 got_worktree_get_root_path(worktree));
7023 if (error)
7024 goto done;
7026 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7027 if (error)
7028 goto done;
7030 if (list_stage)
7031 error = got_worktree_status(worktree, &paths, repo,
7032 print_stage, NULL, check_cancelled, NULL);
7033 else {
7034 cpa.patch_script_file = patch_script_file;
7035 cpa.action = "stage";
7036 error = got_worktree_stage(worktree, &paths,
7037 pflag ? NULL : print_status, NULL,
7038 pflag ? choose_patch : NULL, &cpa, repo);
7040 done:
7041 if (patch_script_file && fclose(patch_script_file) == EOF &&
7042 error == NULL)
7043 error = got_error_from_errno2("fclose", patch_script_path);
7044 if (repo)
7045 got_repo_close(repo);
7046 if (worktree)
7047 got_worktree_close(worktree);
7048 TAILQ_FOREACH(pe, &paths, entry)
7049 free((char *)pe->path);
7050 got_pathlist_free(&paths);
7051 free(cwd);
7052 return error;
7055 __dead static void
7056 usage_unstage(void)
7058 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7059 "[file-path ...]\n",
7060 getprogname());
7061 exit(1);
7065 static const struct got_error *
7066 cmd_unstage(int argc, char *argv[])
7068 const struct got_error *error = NULL;
7069 struct got_repository *repo = NULL;
7070 struct got_worktree *worktree = NULL;
7071 char *cwd = NULL;
7072 struct got_pathlist_head paths;
7073 struct got_pathlist_entry *pe;
7074 int ch, did_something = 0, pflag = 0;
7075 FILE *patch_script_file = NULL;
7076 const char *patch_script_path = NULL;
7077 struct choose_patch_arg cpa;
7079 TAILQ_INIT(&paths);
7081 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7082 switch (ch) {
7083 case 'p':
7084 pflag = 1;
7085 break;
7086 case 'F':
7087 patch_script_path = optarg;
7088 break;
7089 default:
7090 usage_unstage();
7091 /* NOTREACHED */
7095 argc -= optind;
7096 argv += optind;
7098 #ifndef PROFILE
7099 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7100 "unveil", NULL) == -1)
7101 err(1, "pledge");
7102 #endif
7103 if (patch_script_path && !pflag)
7104 errx(1, "-F option can only be used together with -p option");
7106 cwd = getcwd(NULL, 0);
7107 if (cwd == NULL) {
7108 error = got_error_from_errno("getcwd");
7109 goto done;
7112 error = got_worktree_open(&worktree, cwd);
7113 if (error)
7114 goto done;
7116 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7117 NULL);
7118 if (error != NULL)
7119 goto done;
7121 if (patch_script_path) {
7122 patch_script_file = fopen(patch_script_path, "r");
7123 if (patch_script_file == NULL) {
7124 error = got_error_from_errno2("fopen",
7125 patch_script_path);
7126 goto done;
7130 error = apply_unveil(got_repo_get_path(repo), 0,
7131 got_worktree_get_root_path(worktree));
7132 if (error)
7133 goto done;
7135 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7136 if (error)
7137 goto done;
7139 cpa.patch_script_file = patch_script_file;
7140 cpa.action = "unstage";
7141 error = got_worktree_unstage(worktree, &paths, update_progress,
7142 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7143 done:
7144 if (patch_script_file && fclose(patch_script_file) == EOF &&
7145 error == NULL)
7146 error = got_error_from_errno2("fclose", patch_script_path);
7147 if (repo)
7148 got_repo_close(repo);
7149 if (worktree)
7150 got_worktree_close(worktree);
7151 TAILQ_FOREACH(pe, &paths, entry)
7152 free((char *)pe->path);
7153 got_pathlist_free(&paths);
7154 free(cwd);
7155 return error;
7158 __dead static void
7159 usage_cat(void)
7161 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7162 "arg1 [arg2 ...]\n", getprogname());
7163 exit(1);
7166 static const struct got_error *
7167 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7169 const struct got_error *err;
7170 struct got_blob_object *blob;
7172 err = got_object_open_as_blob(&blob, repo, id, 8192);
7173 if (err)
7174 return err;
7176 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7177 got_object_blob_close(blob);
7178 return err;
7181 static const struct got_error *
7182 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7184 const struct got_error *err;
7185 struct got_tree_object *tree;
7186 int nentries, i;
7188 err = got_object_open_as_tree(&tree, repo, id);
7189 if (err)
7190 return err;
7192 nentries = got_object_tree_get_nentries(tree);
7193 for (i = 0; i < nentries; i++) {
7194 struct got_tree_entry *te;
7195 char *id_str;
7196 if (sigint_received || sigpipe_received)
7197 break;
7198 te = got_object_tree_get_entry(tree, i);
7199 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7200 if (err)
7201 break;
7202 fprintf(outfile, "%s %.7o %s\n", id_str,
7203 got_tree_entry_get_mode(te),
7204 got_tree_entry_get_name(te));
7205 free(id_str);
7208 got_object_tree_close(tree);
7209 return err;
7212 static const struct got_error *
7213 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7215 const struct got_error *err;
7216 struct got_commit_object *commit;
7217 const struct got_object_id_queue *parent_ids;
7218 struct got_object_qid *pid;
7219 char *id_str = NULL;
7220 const char *logmsg = NULL;
7222 err = got_object_open_as_commit(&commit, repo, id);
7223 if (err)
7224 return err;
7226 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7227 if (err)
7228 goto done;
7230 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7231 parent_ids = got_object_commit_get_parent_ids(commit);
7232 fprintf(outfile, "numparents %d\n",
7233 got_object_commit_get_nparents(commit));
7234 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7235 char *pid_str;
7236 err = got_object_id_str(&pid_str, pid->id);
7237 if (err)
7238 goto done;
7239 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7240 free(pid_str);
7242 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7243 got_object_commit_get_author(commit),
7244 got_object_commit_get_author_time(commit));
7246 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7247 got_object_commit_get_author(commit),
7248 got_object_commit_get_committer_time(commit));
7250 logmsg = got_object_commit_get_logmsg_raw(commit);
7251 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7252 fprintf(outfile, "%s", logmsg);
7253 done:
7254 free(id_str);
7255 got_object_commit_close(commit);
7256 return err;
7259 static const struct got_error *
7260 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7262 const struct got_error *err;
7263 struct got_tag_object *tag;
7264 char *id_str = NULL;
7265 const char *tagmsg = NULL;
7267 err = got_object_open_as_tag(&tag, repo, id);
7268 if (err)
7269 return err;
7271 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7272 if (err)
7273 goto done;
7275 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7277 switch (got_object_tag_get_object_type(tag)) {
7278 case GOT_OBJ_TYPE_BLOB:
7279 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7280 GOT_OBJ_LABEL_BLOB);
7281 break;
7282 case GOT_OBJ_TYPE_TREE:
7283 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7284 GOT_OBJ_LABEL_TREE);
7285 break;
7286 case GOT_OBJ_TYPE_COMMIT:
7287 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7288 GOT_OBJ_LABEL_COMMIT);
7289 break;
7290 case GOT_OBJ_TYPE_TAG:
7291 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7292 GOT_OBJ_LABEL_TAG);
7293 break;
7294 default:
7295 break;
7298 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7299 got_object_tag_get_name(tag));
7301 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7302 got_object_tag_get_tagger(tag),
7303 got_object_tag_get_tagger_time(tag));
7305 tagmsg = got_object_tag_get_message(tag);
7306 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7307 fprintf(outfile, "%s", tagmsg);
7308 done:
7309 free(id_str);
7310 got_object_tag_close(tag);
7311 return err;
7314 static const struct got_error *
7315 cmd_cat(int argc, char *argv[])
7317 const struct got_error *error;
7318 struct got_repository *repo = NULL;
7319 struct got_worktree *worktree = NULL;
7320 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7321 const char *commit_id_str = NULL;
7322 struct got_object_id *id = NULL, *commit_id = NULL;
7323 int ch, obj_type, i, force_path = 0;
7325 #ifndef PROFILE
7326 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7327 NULL) == -1)
7328 err(1, "pledge");
7329 #endif
7331 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7332 switch (ch) {
7333 case 'c':
7334 commit_id_str = optarg;
7335 break;
7336 case 'r':
7337 repo_path = realpath(optarg, NULL);
7338 if (repo_path == NULL)
7339 return got_error_from_errno2("realpath",
7340 optarg);
7341 got_path_strip_trailing_slashes(repo_path);
7342 break;
7343 case 'P':
7344 force_path = 1;
7345 break;
7346 default:
7347 usage_cat();
7348 /* NOTREACHED */
7352 argc -= optind;
7353 argv += optind;
7355 cwd = getcwd(NULL, 0);
7356 if (cwd == NULL) {
7357 error = got_error_from_errno("getcwd");
7358 goto done;
7360 error = got_worktree_open(&worktree, cwd);
7361 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7362 goto done;
7363 if (worktree) {
7364 if (repo_path == NULL) {
7365 repo_path = strdup(
7366 got_worktree_get_repo_path(worktree));
7367 if (repo_path == NULL) {
7368 error = got_error_from_errno("strdup");
7369 goto done;
7374 if (repo_path == NULL) {
7375 repo_path = getcwd(NULL, 0);
7376 if (repo_path == NULL)
7377 return got_error_from_errno("getcwd");
7380 error = got_repo_open(&repo, repo_path, NULL);
7381 free(repo_path);
7382 if (error != NULL)
7383 goto done;
7385 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7386 if (error)
7387 goto done;
7389 if (commit_id_str == NULL)
7390 commit_id_str = GOT_REF_HEAD;
7391 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7392 if (error)
7393 goto done;
7395 for (i = 0; i < argc; i++) {
7396 if (force_path) {
7397 error = got_object_id_by_path(&id, repo, commit_id,
7398 argv[i]);
7399 if (error)
7400 break;
7401 } else {
7402 error = match_object_id(&id, &label, argv[i],
7403 GOT_OBJ_TYPE_ANY, 0, repo);
7404 if (error) {
7405 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7406 error->code != GOT_ERR_NOT_REF)
7407 break;
7408 error = got_object_id_by_path(&id, repo,
7409 commit_id, argv[i]);
7410 if (error)
7411 break;
7415 error = got_object_get_type(&obj_type, repo, id);
7416 if (error)
7417 break;
7419 switch (obj_type) {
7420 case GOT_OBJ_TYPE_BLOB:
7421 error = cat_blob(id, repo, stdout);
7422 break;
7423 case GOT_OBJ_TYPE_TREE:
7424 error = cat_tree(id, repo, stdout);
7425 break;
7426 case GOT_OBJ_TYPE_COMMIT:
7427 error = cat_commit(id, repo, stdout);
7428 break;
7429 case GOT_OBJ_TYPE_TAG:
7430 error = cat_tag(id, repo, stdout);
7431 break;
7432 default:
7433 error = got_error(GOT_ERR_OBJ_TYPE);
7434 break;
7436 if (error)
7437 break;
7438 free(label);
7439 label = NULL;
7440 free(id);
7441 id = NULL;
7443 done:
7444 free(label);
7445 free(id);
7446 free(commit_id);
7447 if (worktree)
7448 got_worktree_close(worktree);
7449 if (repo) {
7450 const struct got_error *repo_error;
7451 repo_error = got_repo_close(repo);
7452 if (error == NULL)
7453 error = repo_error;
7455 return error;