2 97a02382 2023-07-10 thomas * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 97a02382 2023-07-10 thomas * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 97a02382 2023-07-10 thomas * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 97a02382 2023-07-10 thomas * Permission to use, copy, modify, and distribute this software for any
7 97a02382 2023-07-10 thomas * purpose with or without fee is hereby granted, provided that the above
8 97a02382 2023-07-10 thomas * copyright notice and this permission notice appear in all copies.
10 97a02382 2023-07-10 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 97a02382 2023-07-10 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 97a02382 2023-07-10 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 97a02382 2023-07-10 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 97a02382 2023-07-10 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 97a02382 2023-07-10 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 97a02382 2023-07-10 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 97a02382 2023-07-10 thomas #include <sys/queue.h>
20 97a02382 2023-07-10 thomas #include <sys/time.h>
21 97a02382 2023-07-10 thomas #include <sys/types.h>
22 97a02382 2023-07-10 thomas #include <sys/stat.h>
23 97a02382 2023-07-10 thomas #include <sys/wait.h>
25 97a02382 2023-07-10 thomas #include <err.h>
26 97a02382 2023-07-10 thomas #include <errno.h>
27 97a02382 2023-07-10 thomas #include <fcntl.h>
28 97a02382 2023-07-10 thomas #include <limits.h>
29 97a02382 2023-07-10 thomas #include <locale.h>
30 97a02382 2023-07-10 thomas #include <ctype.h>
31 97a02382 2023-07-10 thomas #include <sha1.h>
32 97a02382 2023-07-10 thomas #include <sha2.h>
33 97a02382 2023-07-10 thomas #include <signal.h>
34 97a02382 2023-07-10 thomas #include <stdio.h>
35 97a02382 2023-07-10 thomas #include <stdlib.h>
36 97a02382 2023-07-10 thomas #include <string.h>
37 97a02382 2023-07-10 thomas #include <unistd.h>
38 97a02382 2023-07-10 thomas #include <libgen.h>
39 97a02382 2023-07-10 thomas #include <time.h>
40 97a02382 2023-07-10 thomas #include <paths.h>
41 97a02382 2023-07-10 thomas #include <regex.h>
42 97a02382 2023-07-10 thomas #include <getopt.h>
43 97a02382 2023-07-10 thomas #include <util.h>
45 97a02382 2023-07-10 thomas #include "got_version.h"
46 97a02382 2023-07-10 thomas #include "got_error.h"
47 97a02382 2023-07-10 thomas #include "got_object.h"
48 97a02382 2023-07-10 thomas #include "got_reference.h"
49 97a02382 2023-07-10 thomas #include "got_repository.h"
50 97a02382 2023-07-10 thomas #include "got_path.h"
51 97a02382 2023-07-10 thomas #include "got_cancel.h"
52 97a02382 2023-07-10 thomas #include "got_worktree.h"
53 97a02382 2023-07-10 thomas #include "got_diff.h"
54 97a02382 2023-07-10 thomas #include "got_commit_graph.h"
55 97a02382 2023-07-10 thomas #include "got_fetch.h"
56 97a02382 2023-07-10 thomas #include "got_send.h"
57 97a02382 2023-07-10 thomas #include "got_blame.h"
58 97a02382 2023-07-10 thomas #include "got_privsep.h"
59 97a02382 2023-07-10 thomas #include "got_opentemp.h"
60 97a02382 2023-07-10 thomas #include "got_gotconfig.h"
61 97a02382 2023-07-10 thomas #include "got_dial.h"
62 97a02382 2023-07-10 thomas #include "got_patch.h"
63 97a02382 2023-07-10 thomas #include "got_sigs.h"
64 97a02382 2023-07-10 thomas #include "got_date.h"
66 97a02382 2023-07-10 thomas #ifndef nitems
67 97a02382 2023-07-10 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 97a02382 2023-07-10 thomas static volatile sig_atomic_t sigint_received;
71 97a02382 2023-07-10 thomas static volatile sig_atomic_t sigpipe_received;
73 97a02382 2023-07-10 thomas static void
74 97a02382 2023-07-10 thomas catch_sigint(int signo)
76 97a02382 2023-07-10 thomas sigint_received = 1;
79 97a02382 2023-07-10 thomas static void
80 97a02382 2023-07-10 thomas catch_sigpipe(int signo)
82 97a02382 2023-07-10 thomas sigpipe_received = 1;
86 97a02382 2023-07-10 thomas struct got_cmd {
87 97a02382 2023-07-10 thomas const char *cmd_name;
88 97a02382 2023-07-10 thomas const struct got_error *(*cmd_main)(int, char *[]);
89 97a02382 2023-07-10 thomas void (*cmd_usage)(void);
90 97a02382 2023-07-10 thomas const char *cmd_alias;
93 97a02382 2023-07-10 thomas __dead static void usage(int, int);
94 97a02382 2023-07-10 thomas __dead static void usage_import(void);
95 97a02382 2023-07-10 thomas __dead static void usage_clone(void);
96 97a02382 2023-07-10 thomas __dead static void usage_fetch(void);
97 97a02382 2023-07-10 thomas __dead static void usage_checkout(void);
98 97a02382 2023-07-10 thomas __dead static void usage_update(void);
99 97a02382 2023-07-10 thomas __dead static void usage_log(void);
100 97a02382 2023-07-10 thomas __dead static void usage_diff(void);
101 97a02382 2023-07-10 thomas __dead static void usage_blame(void);
102 97a02382 2023-07-10 thomas __dead static void usage_tree(void);
103 97a02382 2023-07-10 thomas __dead static void usage_status(void);
104 97a02382 2023-07-10 thomas __dead static void usage_ref(void);
105 97a02382 2023-07-10 thomas __dead static void usage_branch(void);
106 97a02382 2023-07-10 thomas __dead static void usage_tag(void);
107 97a02382 2023-07-10 thomas __dead static void usage_add(void);
108 97a02382 2023-07-10 thomas __dead static void usage_remove(void);
109 97a02382 2023-07-10 thomas __dead static void usage_patch(void);
110 97a02382 2023-07-10 thomas __dead static void usage_revert(void);
111 97a02382 2023-07-10 thomas __dead static void usage_commit(void);
112 97a02382 2023-07-10 thomas __dead static void usage_send(void);
113 97a02382 2023-07-10 thomas __dead static void usage_cherrypick(void);
114 97a02382 2023-07-10 thomas __dead static void usage_backout(void);
115 97a02382 2023-07-10 thomas __dead static void usage_rebase(void);
116 97a02382 2023-07-10 thomas __dead static void usage_histedit(void);
117 97a02382 2023-07-10 thomas __dead static void usage_integrate(void);
118 97a02382 2023-07-10 thomas __dead static void usage_merge(void);
119 97a02382 2023-07-10 thomas __dead static void usage_stage(void);
120 97a02382 2023-07-10 thomas __dead static void usage_unstage(void);
121 97a02382 2023-07-10 thomas __dead static void usage_cat(void);
122 97a02382 2023-07-10 thomas __dead static void usage_info(void);
124 97a02382 2023-07-10 thomas static const struct got_error* cmd_import(int, char *[]);
125 97a02382 2023-07-10 thomas static const struct got_error* cmd_clone(int, char *[]);
126 97a02382 2023-07-10 thomas static const struct got_error* cmd_fetch(int, char *[]);
127 97a02382 2023-07-10 thomas static const struct got_error* cmd_checkout(int, char *[]);
128 97a02382 2023-07-10 thomas static const struct got_error* cmd_update(int, char *[]);
129 97a02382 2023-07-10 thomas static const struct got_error* cmd_log(int, char *[]);
130 97a02382 2023-07-10 thomas static const struct got_error* cmd_diff(int, char *[]);
131 97a02382 2023-07-10 thomas static const struct got_error* cmd_blame(int, char *[]);
132 97a02382 2023-07-10 thomas static const struct got_error* cmd_tree(int, char *[]);
133 97a02382 2023-07-10 thomas static const struct got_error* cmd_status(int, char *[]);
134 97a02382 2023-07-10 thomas static const struct got_error* cmd_ref(int, char *[]);
135 97a02382 2023-07-10 thomas static const struct got_error* cmd_branch(int, char *[]);
136 97a02382 2023-07-10 thomas static const struct got_error* cmd_tag(int, char *[]);
137 97a02382 2023-07-10 thomas static const struct got_error* cmd_add(int, char *[]);
138 97a02382 2023-07-10 thomas static const struct got_error* cmd_remove(int, char *[]);
139 97a02382 2023-07-10 thomas static const struct got_error* cmd_patch(int, char *[]);
140 97a02382 2023-07-10 thomas static const struct got_error* cmd_revert(int, char *[]);
141 97a02382 2023-07-10 thomas static const struct got_error* cmd_commit(int, char *[]);
142 97a02382 2023-07-10 thomas static const struct got_error* cmd_send(int, char *[]);
143 97a02382 2023-07-10 thomas static const struct got_error* cmd_cherrypick(int, char *[]);
144 97a02382 2023-07-10 thomas static const struct got_error* cmd_backout(int, char *[]);
145 97a02382 2023-07-10 thomas static const struct got_error* cmd_rebase(int, char *[]);
146 97a02382 2023-07-10 thomas static const struct got_error* cmd_histedit(int, char *[]);
147 97a02382 2023-07-10 thomas static const struct got_error* cmd_integrate(int, char *[]);
148 97a02382 2023-07-10 thomas static const struct got_error* cmd_merge(int, char *[]);
149 97a02382 2023-07-10 thomas static const struct got_error* cmd_stage(int, char *[]);
150 97a02382 2023-07-10 thomas static const struct got_error* cmd_unstage(int, char *[]);
151 97a02382 2023-07-10 thomas static const struct got_error* cmd_cat(int, char *[]);
152 97a02382 2023-07-10 thomas static const struct got_error* cmd_info(int, char *[]);
154 97a02382 2023-07-10 thomas static const struct got_cmd got_commands[] = {
155 97a02382 2023-07-10 thomas { "import", cmd_import, usage_import, "im" },
156 97a02382 2023-07-10 thomas { "clone", cmd_clone, usage_clone, "cl" },
157 97a02382 2023-07-10 thomas { "fetch", cmd_fetch, usage_fetch, "fe" },
158 97a02382 2023-07-10 thomas { "checkout", cmd_checkout, usage_checkout, "co" },
159 97a02382 2023-07-10 thomas { "update", cmd_update, usage_update, "up" },
160 97a02382 2023-07-10 thomas { "log", cmd_log, usage_log, "" },
161 97a02382 2023-07-10 thomas { "diff", cmd_diff, usage_diff, "di" },
162 97a02382 2023-07-10 thomas { "blame", cmd_blame, usage_blame, "bl" },
163 97a02382 2023-07-10 thomas { "tree", cmd_tree, usage_tree, "tr" },
164 97a02382 2023-07-10 thomas { "status", cmd_status, usage_status, "st" },
165 97a02382 2023-07-10 thomas { "ref", cmd_ref, usage_ref, "" },
166 97a02382 2023-07-10 thomas { "branch", cmd_branch, usage_branch, "br" },
167 97a02382 2023-07-10 thomas { "tag", cmd_tag, usage_tag, "" },
168 97a02382 2023-07-10 thomas { "add", cmd_add, usage_add, "" },
169 97a02382 2023-07-10 thomas { "remove", cmd_remove, usage_remove, "rm" },
170 97a02382 2023-07-10 thomas { "patch", cmd_patch, usage_patch, "pa" },
171 97a02382 2023-07-10 thomas { "revert", cmd_revert, usage_revert, "rv" },
172 97a02382 2023-07-10 thomas { "commit", cmd_commit, usage_commit, "ci" },
173 97a02382 2023-07-10 thomas { "send", cmd_send, usage_send, "se" },
174 97a02382 2023-07-10 thomas { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
175 97a02382 2023-07-10 thomas { "backout", cmd_backout, usage_backout, "bo" },
176 97a02382 2023-07-10 thomas { "rebase", cmd_rebase, usage_rebase, "rb" },
177 97a02382 2023-07-10 thomas { "histedit", cmd_histedit, usage_histedit, "he" },
178 97a02382 2023-07-10 thomas { "integrate", cmd_integrate, usage_integrate,"ig" },
179 97a02382 2023-07-10 thomas { "merge", cmd_merge, usage_merge, "mg" },
180 97a02382 2023-07-10 thomas { "stage", cmd_stage, usage_stage, "sg" },
181 97a02382 2023-07-10 thomas { "unstage", cmd_unstage, usage_unstage, "ug" },
182 97a02382 2023-07-10 thomas { "cat", cmd_cat, usage_cat, "" },
183 97a02382 2023-07-10 thomas { "info", cmd_info, usage_info, "" },
186 97a02382 2023-07-10 thomas static void
187 97a02382 2023-07-10 thomas list_commands(FILE *fp)
189 97a02382 2023-07-10 thomas size_t i;
191 97a02382 2023-07-10 thomas fprintf(fp, "commands:");
192 97a02382 2023-07-10 thomas for (i = 0; i < nitems(got_commands); i++) {
193 97a02382 2023-07-10 thomas const struct got_cmd *cmd = &got_commands[i];
194 97a02382 2023-07-10 thomas fprintf(fp, " %s", cmd->cmd_name);
196 97a02382 2023-07-10 thomas fputc('\n', fp);
199 97a02382 2023-07-10 thomas __dead static void
200 97a02382 2023-07-10 thomas option_conflict(char a, char b)
202 97a02382 2023-07-10 thomas errx(1, "-%c and -%c options are mutually exclusive", a, b);
206 97a02382 2023-07-10 thomas main(int argc, char *argv[])
208 97a02382 2023-07-10 thomas const struct got_cmd *cmd;
209 97a02382 2023-07-10 thomas size_t i;
211 97a02382 2023-07-10 thomas int hflag = 0, Vflag = 0;
212 97a02382 2023-07-10 thomas static const struct option longopts[] = {
213 97a02382 2023-07-10 thomas { "version", no_argument, NULL, 'V' },
214 97a02382 2023-07-10 thomas { NULL, 0, NULL, 0 }
217 97a02382 2023-07-10 thomas setlocale(LC_CTYPE, "");
219 97a02382 2023-07-10 thomas while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
220 97a02382 2023-07-10 thomas switch (ch) {
221 97a02382 2023-07-10 thomas case 'h':
222 97a02382 2023-07-10 thomas hflag = 1;
224 97a02382 2023-07-10 thomas case 'V':
225 97a02382 2023-07-10 thomas Vflag = 1;
228 97a02382 2023-07-10 thomas usage(hflag, 1);
229 97a02382 2023-07-10 thomas /* NOTREACHED */
233 97a02382 2023-07-10 thomas argc -= optind;
234 97a02382 2023-07-10 thomas argv += optind;
235 97a02382 2023-07-10 thomas optind = 1;
236 97a02382 2023-07-10 thomas optreset = 1;
238 97a02382 2023-07-10 thomas if (Vflag) {
239 97a02382 2023-07-10 thomas got_version_print_str();
240 97a02382 2023-07-10 thomas return 0;
243 97a02382 2023-07-10 thomas if (argc <= 0)
244 97a02382 2023-07-10 thomas usage(hflag, hflag ? 0 : 1);
246 97a02382 2023-07-10 thomas signal(SIGINT, catch_sigint);
247 97a02382 2023-07-10 thomas signal(SIGPIPE, catch_sigpipe);
249 97a02382 2023-07-10 thomas for (i = 0; i < nitems(got_commands); i++) {
250 97a02382 2023-07-10 thomas const struct got_error *error;
252 97a02382 2023-07-10 thomas cmd = &got_commands[i];
254 97a02382 2023-07-10 thomas if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
255 97a02382 2023-07-10 thomas strcmp(cmd->cmd_alias, argv[0]) != 0)
256 97a02382 2023-07-10 thomas continue;
258 97a02382 2023-07-10 thomas if (hflag)
259 97a02382 2023-07-10 thomas cmd->cmd_usage();
261 97a02382 2023-07-10 thomas error = cmd->cmd_main(argc, argv);
262 97a02382 2023-07-10 thomas if (error && error->code != GOT_ERR_CANCELLED &&
263 97a02382 2023-07-10 thomas error->code != GOT_ERR_PRIVSEP_EXIT &&
264 97a02382 2023-07-10 thomas !(sigpipe_received &&
265 97a02382 2023-07-10 thomas error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
266 97a02382 2023-07-10 thomas !(sigint_received &&
267 97a02382 2023-07-10 thomas error->code == GOT_ERR_ERRNO && errno == EINTR)) {
268 97a02382 2023-07-10 thomas fflush(stdout);
269 97a02382 2023-07-10 thomas fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
270 97a02382 2023-07-10 thomas return 1;
273 97a02382 2023-07-10 thomas return 0;
276 97a02382 2023-07-10 thomas fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
277 97a02382 2023-07-10 thomas list_commands(stderr);
278 97a02382 2023-07-10 thomas return 1;
281 97a02382 2023-07-10 thomas __dead static void
282 97a02382 2023-07-10 thomas usage(int hflag, int status)
284 97a02382 2023-07-10 thomas FILE *fp = (status == 0) ? stdout : stderr;
286 97a02382 2023-07-10 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
287 97a02382 2023-07-10 thomas getprogname());
288 97a02382 2023-07-10 thomas if (hflag)
289 97a02382 2023-07-10 thomas list_commands(fp);
290 97a02382 2023-07-10 thomas exit(status);
293 97a02382 2023-07-10 thomas static const struct got_error *
294 97a02382 2023-07-10 thomas get_editor(char **abspath)
296 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
297 97a02382 2023-07-10 thomas const char *editor;
299 97a02382 2023-07-10 thomas *abspath = NULL;
301 97a02382 2023-07-10 thomas editor = getenv("VISUAL");
302 97a02382 2023-07-10 thomas if (editor == NULL)
303 97a02382 2023-07-10 thomas editor = getenv("EDITOR");
305 97a02382 2023-07-10 thomas if (editor) {
306 97a02382 2023-07-10 thomas err = got_path_find_prog(abspath, editor);
308 97a02382 2023-07-10 thomas return err;
311 97a02382 2023-07-10 thomas if (*abspath == NULL) {
312 97a02382 2023-07-10 thomas *abspath = strdup("/usr/bin/vi");
313 97a02382 2023-07-10 thomas if (*abspath == NULL)
314 97a02382 2023-07-10 thomas return got_error_from_errno("strdup");
317 97a02382 2023-07-10 thomas return NULL;
320 97a02382 2023-07-10 thomas static const struct got_error *
321 97a02382 2023-07-10 thomas apply_unveil(const char *repo_path, int repo_read_only,
322 97a02382 2023-07-10 thomas const char *worktree_path)
324 97a02382 2023-07-10 thomas const struct got_error *err;
326 97a02382 2023-07-10 thomas #ifdef PROFILE
327 97a02382 2023-07-10 thomas if (unveil("gmon.out", "rwc") != 0)
328 97a02382 2023-07-10 thomas return got_error_from_errno2("unveil", "gmon.out");
330 97a02382 2023-07-10 thomas if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
331 97a02382 2023-07-10 thomas return got_error_from_errno2("unveil", repo_path);
333 97a02382 2023-07-10 thomas if (worktree_path && unveil(worktree_path, "rwc") != 0)
334 97a02382 2023-07-10 thomas return got_error_from_errno2("unveil", worktree_path);
336 97a02382 2023-07-10 thomas if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
337 97a02382 2023-07-10 thomas return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
339 97a02382 2023-07-10 thomas err = got_privsep_unveil_exec_helpers();
340 97a02382 2023-07-10 thomas if (err != NULL)
341 97a02382 2023-07-10 thomas return err;
343 97a02382 2023-07-10 thomas if (unveil(NULL, NULL) != 0)
344 97a02382 2023-07-10 thomas return got_error_from_errno("unveil");
346 97a02382 2023-07-10 thomas return NULL;
349 97a02382 2023-07-10 thomas __dead static void
350 97a02382 2023-07-10 thomas usage_import(void)
352 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s import [-b branch] [-I pattern] [-m message] "
353 97a02382 2023-07-10 thomas "[-r repository-path] directory\n", getprogname());
357 97a02382 2023-07-10 thomas static int
358 97a02382 2023-07-10 thomas spawn_editor(const char *editor, const char *file)
360 97a02382 2023-07-10 thomas pid_t pid;
361 97a02382 2023-07-10 thomas sig_t sighup, sigint, sigquit;
362 97a02382 2023-07-10 thomas int st = -1;
364 97a02382 2023-07-10 thomas sighup = signal(SIGHUP, SIG_IGN);
365 97a02382 2023-07-10 thomas sigint = signal(SIGINT, SIG_IGN);
366 97a02382 2023-07-10 thomas sigquit = signal(SIGQUIT, SIG_IGN);
368 97a02382 2023-07-10 thomas switch (pid = fork()) {
370 97a02382 2023-07-10 thomas goto doneediting;
372 97a02382 2023-07-10 thomas execl(editor, editor, file, (char *)NULL);
373 97a02382 2023-07-10 thomas _exit(127);
376 97a02382 2023-07-10 thomas while (waitpid(pid, &st, 0) == -1)
377 97a02382 2023-07-10 thomas if (errno != EINTR)
380 97a02382 2023-07-10 thomas doneediting:
381 97a02382 2023-07-10 thomas (void)signal(SIGHUP, sighup);
382 97a02382 2023-07-10 thomas (void)signal(SIGINT, sigint);
383 97a02382 2023-07-10 thomas (void)signal(SIGQUIT, sigquit);
385 97a02382 2023-07-10 thomas if (!WIFEXITED(st)) {
386 97a02382 2023-07-10 thomas errno = EINTR;
387 97a02382 2023-07-10 thomas return -1;
390 97a02382 2023-07-10 thomas return WEXITSTATUS(st);
393 97a02382 2023-07-10 thomas static const struct got_error *
394 97a02382 2023-07-10 thomas read_logmsg(char **logmsg, size_t *len, FILE *fp, size_t filesize)
396 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
397 97a02382 2023-07-10 thomas char *line = NULL;
398 97a02382 2023-07-10 thomas size_t linesize = 0;
400 97a02382 2023-07-10 thomas *logmsg = NULL;
401 97a02382 2023-07-10 thomas *len = 0;
403 97a02382 2023-07-10 thomas if (fseeko(fp, 0L, SEEK_SET) == -1)
404 97a02382 2023-07-10 thomas return got_error_from_errno("fseeko");
406 97a02382 2023-07-10 thomas *logmsg = malloc(filesize + 1);
407 97a02382 2023-07-10 thomas if (*logmsg == NULL)
408 97a02382 2023-07-10 thomas return got_error_from_errno("malloc");
409 97a02382 2023-07-10 thomas (*logmsg)[0] = '\0';
411 97a02382 2023-07-10 thomas while (getline(&line, &linesize, fp) != -1) {
412 97a02382 2023-07-10 thomas if (line[0] == '#' || (*len == 0 && line[0] == '\n'))
413 97a02382 2023-07-10 thomas continue; /* remove comments and leading empty lines */
414 97a02382 2023-07-10 thomas *len = strlcat(*logmsg, line, filesize + 1);
415 97a02382 2023-07-10 thomas if (*len >= filesize + 1) {
416 97a02382 2023-07-10 thomas err = got_error(GOT_ERR_NO_SPACE);
417 97a02382 2023-07-10 thomas goto done;
420 97a02382 2023-07-10 thomas if (ferror(fp)) {
421 97a02382 2023-07-10 thomas err = got_ferror(fp, GOT_ERR_IO);
422 97a02382 2023-07-10 thomas goto done;
425 97a02382 2023-07-10 thomas while (*len > 0 && (*logmsg)[*len - 1] == '\n') {
426 97a02382 2023-07-10 thomas (*logmsg)[*len - 1] = '\0';
427 97a02382 2023-07-10 thomas (*len)--;
430 97a02382 2023-07-10 thomas free(line);
431 97a02382 2023-07-10 thomas if (err) {
432 97a02382 2023-07-10 thomas free(*logmsg);
433 97a02382 2023-07-10 thomas *logmsg = NULL;
434 97a02382 2023-07-10 thomas *len = 0;
436 97a02382 2023-07-10 thomas return err;
439 97a02382 2023-07-10 thomas static const struct got_error *
440 97a02382 2023-07-10 thomas edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
441 97a02382 2023-07-10 thomas const char *initial_content, size_t initial_content_len,
442 97a02382 2023-07-10 thomas int require_modification)
444 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
445 97a02382 2023-07-10 thomas struct stat st, st2;
446 97a02382 2023-07-10 thomas FILE *fp = NULL;
447 97a02382 2023-07-10 thomas size_t logmsg_len;
449 97a02382 2023-07-10 thomas *logmsg = NULL;
451 97a02382 2023-07-10 thomas if (stat(logmsg_path, &st) == -1)
452 97a02382 2023-07-10 thomas return got_error_from_errno2("stat", logmsg_path);
454 97a02382 2023-07-10 thomas if (spawn_editor(editor, logmsg_path) == -1)
455 97a02382 2023-07-10 thomas return got_error_from_errno("failed spawning editor");
457 97a02382 2023-07-10 thomas if (require_modification) {
458 97a02382 2023-07-10 thomas struct timespec timeout;
460 97a02382 2023-07-10 thomas timeout.tv_sec = 0;
461 97a02382 2023-07-10 thomas timeout.tv_nsec = 1;
462 97a02382 2023-07-10 thomas nanosleep(&timeout, NULL);
465 97a02382 2023-07-10 thomas if (stat(logmsg_path, &st2) == -1)
466 97a02382 2023-07-10 thomas return got_error_from_errno("stat");
468 97a02382 2023-07-10 thomas if (require_modification && st.st_size == st2.st_size &&
469 97a02382 2023-07-10 thomas timespeccmp(&st.st_mtim, &st2.st_mtim, ==))
470 97a02382 2023-07-10 thomas return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
471 97a02382 2023-07-10 thomas "no changes made to commit message, aborting");
473 97a02382 2023-07-10 thomas fp = fopen(logmsg_path, "re");
474 97a02382 2023-07-10 thomas if (fp == NULL) {
475 97a02382 2023-07-10 thomas err = got_error_from_errno("fopen");
476 97a02382 2023-07-10 thomas goto done;
479 97a02382 2023-07-10 thomas /* strip comments and leading/trailing newlines */
480 97a02382 2023-07-10 thomas err = read_logmsg(logmsg, &logmsg_len, fp, st2.st_size);
482 97a02382 2023-07-10 thomas goto done;
483 97a02382 2023-07-10 thomas if (logmsg_len == 0) {
484 97a02382 2023-07-10 thomas err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
485 97a02382 2023-07-10 thomas "commit message cannot be empty, aborting");
486 97a02382 2023-07-10 thomas goto done;
489 97a02382 2023-07-10 thomas if (fp && fclose(fp) == EOF && err == NULL)
490 97a02382 2023-07-10 thomas err = got_error_from_errno("fclose");
491 97a02382 2023-07-10 thomas if (err) {
492 97a02382 2023-07-10 thomas free(*logmsg);
493 97a02382 2023-07-10 thomas *logmsg = NULL;
495 97a02382 2023-07-10 thomas return err;
498 97a02382 2023-07-10 thomas static const struct got_error *
499 97a02382 2023-07-10 thomas collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
500 97a02382 2023-07-10 thomas const char *path_dir, const char *branch_name)
502 97a02382 2023-07-10 thomas char *initial_content = NULL;
503 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
504 97a02382 2023-07-10 thomas int initial_content_len;
505 97a02382 2023-07-10 thomas int fd = -1;
507 97a02382 2023-07-10 thomas initial_content_len = asprintf(&initial_content,
508 97a02382 2023-07-10 thomas "\n# %s to be imported to branch %s\n", path_dir,
509 97a02382 2023-07-10 thomas branch_name);
510 97a02382 2023-07-10 thomas if (initial_content_len == -1)
511 97a02382 2023-07-10 thomas return got_error_from_errno("asprintf");
513 97a02382 2023-07-10 thomas err = got_opentemp_named_fd(logmsg_path, &fd,
514 97a02382 2023-07-10 thomas GOT_TMPDIR_STR "/got-importmsg", "");
516 97a02382 2023-07-10 thomas goto done;
518 97a02382 2023-07-10 thomas if (write(fd, initial_content, initial_content_len) == -1) {
519 97a02382 2023-07-10 thomas err = got_error_from_errno2("write", *logmsg_path);
520 97a02382 2023-07-10 thomas goto done;
522 97a02382 2023-07-10 thomas if (close(fd) == -1) {
523 97a02382 2023-07-10 thomas err = got_error_from_errno2("close", *logmsg_path);
524 97a02382 2023-07-10 thomas goto done;
528 97a02382 2023-07-10 thomas err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
529 97a02382 2023-07-10 thomas initial_content_len, 1);
531 97a02382 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
532 97a02382 2023-07-10 thomas err = got_error_from_errno2("close", *logmsg_path);
533 97a02382 2023-07-10 thomas free(initial_content);
534 97a02382 2023-07-10 thomas if (err) {
535 97a02382 2023-07-10 thomas free(*logmsg_path);
536 97a02382 2023-07-10 thomas *logmsg_path = NULL;
538 97a02382 2023-07-10 thomas return err;
541 97a02382 2023-07-10 thomas static const struct got_error *
542 97a02382 2023-07-10 thomas import_progress(void *arg, const char *path)
544 97a02382 2023-07-10 thomas printf("A %s\n", path);
545 97a02382 2023-07-10 thomas return NULL;
548 97a02382 2023-07-10 thomas static const struct got_error *
549 97a02382 2023-07-10 thomas valid_author(const char *author)
551 97a02382 2023-07-10 thomas const char *email = author;
554 97a02382 2023-07-10 thomas * Git' expects the author (or committer) to be in the form
555 97a02382 2023-07-10 thomas * "name <email>", which are mostly free form (see the
556 97a02382 2023-07-10 thomas * "committer" description in git-fast-import(1)). We're only
557 97a02382 2023-07-10 thomas * doing this to avoid git's object parser breaking on commits
558 97a02382 2023-07-10 thomas * we create.
561 97a02382 2023-07-10 thomas while (*author && *author != '\n' && *author != '<' && *author != '>')
562 97a02382 2023-07-10 thomas author++;
563 97a02382 2023-07-10 thomas if (author != email && *author == '<' && *(author - 1) != ' ')
564 97a02382 2023-07-10 thomas return got_error_fmt(GOT_ERR_COMMIT_BAD_AUTHOR, "%s: space "
565 97a02382 2023-07-10 thomas "between author name and email required", email);
566 97a02382 2023-07-10 thomas if (*author++ != '<')
567 97a02382 2023-07-10 thomas return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
568 97a02382 2023-07-10 thomas while (*author && *author != '\n' && *author != '<' && *author != '>')
569 97a02382 2023-07-10 thomas author++;
570 97a02382 2023-07-10 thomas if (strcmp(author, ">") != 0)
571 97a02382 2023-07-10 thomas return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
572 97a02382 2023-07-10 thomas return NULL;
575 97a02382 2023-07-10 thomas static const struct got_error *
576 97a02382 2023-07-10 thomas get_author(char **author, struct got_repository *repo,
577 97a02382 2023-07-10 thomas struct got_worktree *worktree)
579 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
580 97a02382 2023-07-10 thomas const char *got_author = NULL, *name, *email;
581 97a02382 2023-07-10 thomas const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
583 97a02382 2023-07-10 thomas *author = NULL;
585 97a02382 2023-07-10 thomas if (worktree)
586 97a02382 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
587 97a02382 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
590 97a02382 2023-07-10 thomas * Priority of potential author information sources, from most
591 97a02382 2023-07-10 thomas * significant to least significant:
592 97a02382 2023-07-10 thomas * 1) work tree's .got/got.conf file
593 97a02382 2023-07-10 thomas * 2) repository's got.conf file
594 97a02382 2023-07-10 thomas * 3) repository's git config file
595 97a02382 2023-07-10 thomas * 4) environment variables
596 97a02382 2023-07-10 thomas * 5) global git config files (in user's home directory or /etc)
599 97a02382 2023-07-10 thomas if (worktree_conf)
600 97a02382 2023-07-10 thomas got_author = got_gotconfig_get_author(worktree_conf);
601 97a02382 2023-07-10 thomas if (got_author == NULL)
602 97a02382 2023-07-10 thomas got_author = got_gotconfig_get_author(repo_conf);
603 97a02382 2023-07-10 thomas if (got_author == NULL) {
604 97a02382 2023-07-10 thomas name = got_repo_get_gitconfig_author_name(repo);
605 97a02382 2023-07-10 thomas email = got_repo_get_gitconfig_author_email(repo);
606 97a02382 2023-07-10 thomas if (name && email) {
607 97a02382 2023-07-10 thomas if (asprintf(author, "%s <%s>", name, email) == -1)
608 97a02382 2023-07-10 thomas return got_error_from_errno("asprintf");
609 97a02382 2023-07-10 thomas return NULL;
612 97a02382 2023-07-10 thomas got_author = getenv("GOT_AUTHOR");
613 97a02382 2023-07-10 thomas if (got_author == NULL) {
614 97a02382 2023-07-10 thomas name = got_repo_get_global_gitconfig_author_name(repo);
615 97a02382 2023-07-10 thomas email = got_repo_get_global_gitconfig_author_email(
617 97a02382 2023-07-10 thomas if (name && email) {
618 97a02382 2023-07-10 thomas if (asprintf(author, "%s <%s>", name, email)
620 97a02382 2023-07-10 thomas return got_error_from_errno("asprintf");
621 97a02382 2023-07-10 thomas return NULL;
623 97a02382 2023-07-10 thomas /* TODO: Look up user in password database? */
624 97a02382 2023-07-10 thomas return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
628 97a02382 2023-07-10 thomas *author = strdup(got_author);
629 97a02382 2023-07-10 thomas if (*author == NULL)
630 97a02382 2023-07-10 thomas return got_error_from_errno("strdup");
632 97a02382 2023-07-10 thomas err = valid_author(*author);
633 97a02382 2023-07-10 thomas if (err) {
634 97a02382 2023-07-10 thomas free(*author);
635 97a02382 2023-07-10 thomas *author = NULL;
637 97a02382 2023-07-10 thomas return err;
640 97a02382 2023-07-10 thomas static const struct got_error *
641 97a02382 2023-07-10 thomas get_allowed_signers(char **allowed_signers, struct got_repository *repo,
642 97a02382 2023-07-10 thomas struct got_worktree *worktree)
644 97a02382 2023-07-10 thomas const char *got_allowed_signers = NULL;
645 97a02382 2023-07-10 thomas const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
647 97a02382 2023-07-10 thomas *allowed_signers = NULL;
649 97a02382 2023-07-10 thomas if (worktree)
650 97a02382 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
651 97a02382 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
654 97a02382 2023-07-10 thomas * Priority of potential author information sources, from most
655 97a02382 2023-07-10 thomas * significant to least significant:
656 97a02382 2023-07-10 thomas * 1) work tree's .got/got.conf file
657 97a02382 2023-07-10 thomas * 2) repository's got.conf file
660 97a02382 2023-07-10 thomas if (worktree_conf)
661 97a02382 2023-07-10 thomas got_allowed_signers = got_gotconfig_get_allowed_signers_file(
662 97a02382 2023-07-10 thomas worktree_conf);
663 97a02382 2023-07-10 thomas if (got_allowed_signers == NULL)
664 97a02382 2023-07-10 thomas got_allowed_signers = got_gotconfig_get_allowed_signers_file(
665 97a02382 2023-07-10 thomas repo_conf);
667 97a02382 2023-07-10 thomas if (got_allowed_signers) {
668 97a02382 2023-07-10 thomas *allowed_signers = strdup(got_allowed_signers);
669 97a02382 2023-07-10 thomas if (*allowed_signers == NULL)
670 97a02382 2023-07-10 thomas return got_error_from_errno("strdup");
672 97a02382 2023-07-10 thomas return NULL;
675 97a02382 2023-07-10 thomas static const struct got_error *
676 97a02382 2023-07-10 thomas get_revoked_signers(char **revoked_signers, struct got_repository *repo,
677 97a02382 2023-07-10 thomas struct got_worktree *worktree)
679 97a02382 2023-07-10 thomas const char *got_revoked_signers = NULL;
680 97a02382 2023-07-10 thomas const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
682 97a02382 2023-07-10 thomas *revoked_signers = NULL;
684 97a02382 2023-07-10 thomas if (worktree)
685 97a02382 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
686 97a02382 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
689 97a02382 2023-07-10 thomas * Priority of potential author information sources, from most
690 97a02382 2023-07-10 thomas * significant to least significant:
691 97a02382 2023-07-10 thomas * 1) work tree's .got/got.conf file
692 97a02382 2023-07-10 thomas * 2) repository's got.conf file
695 97a02382 2023-07-10 thomas if (worktree_conf)
696 97a02382 2023-07-10 thomas got_revoked_signers = got_gotconfig_get_revoked_signers_file(
697 97a02382 2023-07-10 thomas worktree_conf);
698 97a02382 2023-07-10 thomas if (got_revoked_signers == NULL)
699 97a02382 2023-07-10 thomas got_revoked_signers = got_gotconfig_get_revoked_signers_file(
700 97a02382 2023-07-10 thomas repo_conf);
702 97a02382 2023-07-10 thomas if (got_revoked_signers) {
703 97a02382 2023-07-10 thomas *revoked_signers = strdup(got_revoked_signers);
704 97a02382 2023-07-10 thomas if (*revoked_signers == NULL)
705 97a02382 2023-07-10 thomas return got_error_from_errno("strdup");
707 97a02382 2023-07-10 thomas return NULL;
710 97a02382 2023-07-10 thomas static const char *
711 97a02382 2023-07-10 thomas get_signer_id(struct got_repository *repo, struct got_worktree *worktree)
713 97a02382 2023-07-10 thomas const char *got_signer_id = NULL;
714 97a02382 2023-07-10 thomas const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
716 97a02382 2023-07-10 thomas if (worktree)
717 97a02382 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
718 97a02382 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
721 97a02382 2023-07-10 thomas * Priority of potential author information sources, from most
722 97a02382 2023-07-10 thomas * significant to least significant:
723 97a02382 2023-07-10 thomas * 1) work tree's .got/got.conf file
724 97a02382 2023-07-10 thomas * 2) repository's got.conf file
727 97a02382 2023-07-10 thomas if (worktree_conf)
728 97a02382 2023-07-10 thomas got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
729 97a02382 2023-07-10 thomas if (got_signer_id == NULL)
730 97a02382 2023-07-10 thomas got_signer_id = got_gotconfig_get_signer_id(repo_conf);
732 97a02382 2023-07-10 thomas return got_signer_id;
735 97a02382 2023-07-10 thomas static const struct got_error *
736 97a02382 2023-07-10 thomas get_gitconfig_path(char **gitconfig_path)
738 97a02382 2023-07-10 thomas const char *homedir = getenv("HOME");
740 97a02382 2023-07-10 thomas *gitconfig_path = NULL;
741 97a02382 2023-07-10 thomas if (homedir) {
742 97a02382 2023-07-10 thomas if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
743 97a02382 2023-07-10 thomas return got_error_from_errno("asprintf");
746 97a02382 2023-07-10 thomas return NULL;
749 97a02382 2023-07-10 thomas static const struct got_error *
750 97a02382 2023-07-10 thomas cmd_import(int argc, char *argv[])
752 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
753 97a02382 2023-07-10 thomas char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
754 97a02382 2023-07-10 thomas char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
755 97a02382 2023-07-10 thomas const char *branch_name = NULL;
756 97a02382 2023-07-10 thomas char *id_str = NULL, *logmsg_path = NULL;
757 97a02382 2023-07-10 thomas char refname[PATH_MAX] = "refs/heads/";
758 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
759 97a02382 2023-07-10 thomas struct got_reference *branch_ref = NULL, *head_ref = NULL;
760 97a02382 2023-07-10 thomas struct got_object_id *new_commit_id = NULL;
761 97a02382 2023-07-10 thomas int ch, n = 0;
762 97a02382 2023-07-10 thomas struct got_pathlist_head ignores;
763 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
764 97a02382 2023-07-10 thomas int preserve_logmsg = 0;
765 97a02382 2023-07-10 thomas int *pack_fds = NULL;
767 97a02382 2023-07-10 thomas TAILQ_INIT(&ignores);
769 97a02382 2023-07-10 thomas #ifndef PROFILE
770 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
771 97a02382 2023-07-10 thomas "unveil",
772 97a02382 2023-07-10 thomas NULL) == -1)
773 97a02382 2023-07-10 thomas err(1, "pledge");
776 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "b:I:m:r:")) != -1) {
777 97a02382 2023-07-10 thomas switch (ch) {
778 97a02382 2023-07-10 thomas case 'b':
779 97a02382 2023-07-10 thomas branch_name = optarg;
781 97a02382 2023-07-10 thomas case 'I':
782 97a02382 2023-07-10 thomas if (optarg[0] == '\0')
784 97a02382 2023-07-10 thomas error = got_pathlist_insert(&pe, &ignores, optarg,
786 97a02382 2023-07-10 thomas if (error)
787 97a02382 2023-07-10 thomas goto done;
789 97a02382 2023-07-10 thomas case 'm':
790 97a02382 2023-07-10 thomas logmsg = strdup(optarg);
791 97a02382 2023-07-10 thomas if (logmsg == NULL) {
792 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
793 97a02382 2023-07-10 thomas goto done;
796 97a02382 2023-07-10 thomas case 'r':
797 97a02382 2023-07-10 thomas repo_path = realpath(optarg, NULL);
798 97a02382 2023-07-10 thomas if (repo_path == NULL) {
799 97a02382 2023-07-10 thomas error = got_error_from_errno2("realpath",
801 97a02382 2023-07-10 thomas goto done;
805 97a02382 2023-07-10 thomas usage_import();
806 97a02382 2023-07-10 thomas /* NOTREACHED */
810 97a02382 2023-07-10 thomas argc -= optind;
811 97a02382 2023-07-10 thomas argv += optind;
813 97a02382 2023-07-10 thomas if (argc != 1)
814 97a02382 2023-07-10 thomas usage_import();
816 97a02382 2023-07-10 thomas if (repo_path == NULL) {
817 97a02382 2023-07-10 thomas repo_path = getcwd(NULL, 0);
818 97a02382 2023-07-10 thomas if (repo_path == NULL)
819 97a02382 2023-07-10 thomas return got_error_from_errno("getcwd");
821 97a02382 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
822 97a02382 2023-07-10 thomas error = get_gitconfig_path(&gitconfig_path);
823 97a02382 2023-07-10 thomas if (error)
824 97a02382 2023-07-10 thomas goto done;
825 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
826 97a02382 2023-07-10 thomas if (error != NULL)
827 97a02382 2023-07-10 thomas goto done;
828 97a02382 2023-07-10 thomas error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
829 97a02382 2023-07-10 thomas if (error)
830 97a02382 2023-07-10 thomas goto done;
832 97a02382 2023-07-10 thomas error = get_author(&author, repo, NULL);
833 97a02382 2023-07-10 thomas if (error)
834 97a02382 2023-07-10 thomas return error;
837 97a02382 2023-07-10 thomas * Don't let the user create a branch name with a leading '-'.
838 97a02382 2023-07-10 thomas * While technically a valid reference name, this case is usually
839 97a02382 2023-07-10 thomas * an unintended typo.
841 97a02382 2023-07-10 thomas if (branch_name && branch_name[0] == '-')
842 97a02382 2023-07-10 thomas return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
844 97a02382 2023-07-10 thomas error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
845 97a02382 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_REF)
846 97a02382 2023-07-10 thomas goto done;
848 97a02382 2023-07-10 thomas if (branch_name)
849 97a02382 2023-07-10 thomas n = strlcat(refname, branch_name, sizeof(refname));
850 97a02382 2023-07-10 thomas else if (head_ref && got_ref_is_symbolic(head_ref))
851 97a02382 2023-07-10 thomas n = strlcpy(refname, got_ref_get_symref_target(head_ref),
852 97a02382 2023-07-10 thomas sizeof(refname));
854 97a02382 2023-07-10 thomas n = strlcat(refname, "main", sizeof(refname));
855 97a02382 2023-07-10 thomas if (n >= sizeof(refname)) {
856 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_NO_SPACE);
857 97a02382 2023-07-10 thomas goto done;
860 97a02382 2023-07-10 thomas error = got_ref_open(&branch_ref, repo, refname, 0);
861 97a02382 2023-07-10 thomas if (error) {
862 97a02382 2023-07-10 thomas if (error->code != GOT_ERR_NOT_REF)
863 97a02382 2023-07-10 thomas goto done;
865 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
866 97a02382 2023-07-10 thomas "import target branch already exists");
867 97a02382 2023-07-10 thomas goto done;
870 97a02382 2023-07-10 thomas path_dir = realpath(argv[0], NULL);
871 97a02382 2023-07-10 thomas if (path_dir == NULL) {
872 97a02382 2023-07-10 thomas error = got_error_from_errno2("realpath", argv[0]);
873 97a02382 2023-07-10 thomas goto done;
875 97a02382 2023-07-10 thomas got_path_strip_trailing_slashes(path_dir);
878 97a02382 2023-07-10 thomas * unveil(2) traverses exec(2); if an editor is used we have
879 97a02382 2023-07-10 thomas * to apply unveil after the log message has been written.
881 97a02382 2023-07-10 thomas if (logmsg == NULL || *logmsg == '\0') {
882 97a02382 2023-07-10 thomas error = get_editor(&editor);
883 97a02382 2023-07-10 thomas if (error)
884 97a02382 2023-07-10 thomas goto done;
885 97a02382 2023-07-10 thomas free(logmsg);
886 97a02382 2023-07-10 thomas error = collect_import_msg(&logmsg, &logmsg_path, editor,
887 97a02382 2023-07-10 thomas path_dir, refname);
888 97a02382 2023-07-10 thomas if (error) {
889 97a02382 2023-07-10 thomas if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
890 97a02382 2023-07-10 thomas logmsg_path != NULL)
891 97a02382 2023-07-10 thomas preserve_logmsg = 1;
892 97a02382 2023-07-10 thomas goto done;
896 97a02382 2023-07-10 thomas if (unveil(path_dir, "r") != 0) {
897 97a02382 2023-07-10 thomas error = got_error_from_errno2("unveil", path_dir);
898 97a02382 2023-07-10 thomas if (logmsg_path)
899 97a02382 2023-07-10 thomas preserve_logmsg = 1;
900 97a02382 2023-07-10 thomas goto done;
903 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0, NULL);
904 97a02382 2023-07-10 thomas if (error) {
905 97a02382 2023-07-10 thomas if (logmsg_path)
906 97a02382 2023-07-10 thomas preserve_logmsg = 1;
907 97a02382 2023-07-10 thomas goto done;
910 97a02382 2023-07-10 thomas error = got_repo_import(&new_commit_id, path_dir, logmsg,
911 97a02382 2023-07-10 thomas author, &ignores, repo, import_progress, NULL);
912 97a02382 2023-07-10 thomas if (error) {
913 97a02382 2023-07-10 thomas if (logmsg_path)
914 97a02382 2023-07-10 thomas preserve_logmsg = 1;
915 97a02382 2023-07-10 thomas goto done;
918 97a02382 2023-07-10 thomas error = got_ref_alloc(&branch_ref, refname, new_commit_id);
919 97a02382 2023-07-10 thomas if (error) {
920 97a02382 2023-07-10 thomas if (logmsg_path)
921 97a02382 2023-07-10 thomas preserve_logmsg = 1;
922 97a02382 2023-07-10 thomas goto done;
925 97a02382 2023-07-10 thomas error = got_ref_write(branch_ref, repo);
926 97a02382 2023-07-10 thomas if (error) {
927 97a02382 2023-07-10 thomas if (logmsg_path)
928 97a02382 2023-07-10 thomas preserve_logmsg = 1;
929 97a02382 2023-07-10 thomas goto done;
932 97a02382 2023-07-10 thomas error = got_object_id_str(&id_str, new_commit_id);
933 97a02382 2023-07-10 thomas if (error) {
934 97a02382 2023-07-10 thomas if (logmsg_path)
935 97a02382 2023-07-10 thomas preserve_logmsg = 1;
936 97a02382 2023-07-10 thomas goto done;
939 97a02382 2023-07-10 thomas error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
940 97a02382 2023-07-10 thomas if (error) {
941 97a02382 2023-07-10 thomas if (error->code != GOT_ERR_NOT_REF) {
942 97a02382 2023-07-10 thomas if (logmsg_path)
943 97a02382 2023-07-10 thomas preserve_logmsg = 1;
944 97a02382 2023-07-10 thomas goto done;
947 97a02382 2023-07-10 thomas error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
948 97a02382 2023-07-10 thomas branch_ref);
949 97a02382 2023-07-10 thomas if (error) {
950 97a02382 2023-07-10 thomas if (logmsg_path)
951 97a02382 2023-07-10 thomas preserve_logmsg = 1;
952 97a02382 2023-07-10 thomas goto done;
955 97a02382 2023-07-10 thomas error = got_ref_write(head_ref, repo);
956 97a02382 2023-07-10 thomas if (error) {
957 97a02382 2023-07-10 thomas if (logmsg_path)
958 97a02382 2023-07-10 thomas preserve_logmsg = 1;
959 97a02382 2023-07-10 thomas goto done;
963 97a02382 2023-07-10 thomas printf("Created branch %s with commit %s\n",
964 97a02382 2023-07-10 thomas got_ref_get_name(branch_ref), id_str);
966 97a02382 2023-07-10 thomas if (pack_fds) {
967 97a02382 2023-07-10 thomas const struct got_error *pack_err =
968 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
969 97a02382 2023-07-10 thomas if (error == NULL)
970 97a02382 2023-07-10 thomas error = pack_err;
972 97a02382 2023-07-10 thomas if (repo) {
973 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
974 97a02382 2023-07-10 thomas if (error == NULL)
975 97a02382 2023-07-10 thomas error = close_err;
977 97a02382 2023-07-10 thomas if (preserve_logmsg) {
978 97a02382 2023-07-10 thomas fprintf(stderr, "%s: log message preserved in %s\n",
979 97a02382 2023-07-10 thomas getprogname(), logmsg_path);
980 97a02382 2023-07-10 thomas } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
981 97a02382 2023-07-10 thomas error = got_error_from_errno2("unlink", logmsg_path);
982 97a02382 2023-07-10 thomas free(logmsg);
983 97a02382 2023-07-10 thomas free(logmsg_path);
984 97a02382 2023-07-10 thomas free(repo_path);
985 97a02382 2023-07-10 thomas free(editor);
986 97a02382 2023-07-10 thomas free(new_commit_id);
987 97a02382 2023-07-10 thomas free(id_str);
988 97a02382 2023-07-10 thomas free(author);
989 97a02382 2023-07-10 thomas free(gitconfig_path);
990 97a02382 2023-07-10 thomas if (branch_ref)
991 97a02382 2023-07-10 thomas got_ref_close(branch_ref);
992 97a02382 2023-07-10 thomas if (head_ref)
993 97a02382 2023-07-10 thomas got_ref_close(head_ref);
994 97a02382 2023-07-10 thomas return error;
997 97a02382 2023-07-10 thomas __dead static void
998 97a02382 2023-07-10 thomas usage_clone(void)
1000 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s clone [-almqv] [-b branch] [-R reference] "
1001 97a02382 2023-07-10 thomas "repository-URL [directory]\n", getprogname());
1002 97a02382 2023-07-10 thomas exit(1);
1005 97a02382 2023-07-10 thomas struct got_fetch_progress_arg {
1006 97a02382 2023-07-10 thomas char last_scaled_size[FMT_SCALED_STRSIZE];
1007 97a02382 2023-07-10 thomas int last_p_indexed;
1008 97a02382 2023-07-10 thomas int last_p_resolved;
1009 97a02382 2023-07-10 thomas int verbosity;
1011 97a02382 2023-07-10 thomas struct got_repository *repo;
1013 97a02382 2023-07-10 thomas int create_configs;
1014 97a02382 2023-07-10 thomas int configs_created;
1015 97a02382 2023-07-10 thomas struct {
1016 97a02382 2023-07-10 thomas struct got_pathlist_head *symrefs;
1017 97a02382 2023-07-10 thomas struct got_pathlist_head *wanted_branches;
1018 97a02382 2023-07-10 thomas struct got_pathlist_head *wanted_refs;
1019 97a02382 2023-07-10 thomas const char *proto;
1020 97a02382 2023-07-10 thomas const char *host;
1021 97a02382 2023-07-10 thomas const char *port;
1022 97a02382 2023-07-10 thomas const char *remote_repo_path;
1023 97a02382 2023-07-10 thomas const char *git_url;
1024 97a02382 2023-07-10 thomas int fetch_all_branches;
1025 97a02382 2023-07-10 thomas int mirror_references;
1026 97a02382 2023-07-10 thomas } config_info;
1029 97a02382 2023-07-10 thomas /* XXX forward declaration */
1030 97a02382 2023-07-10 thomas static const struct got_error *
1031 97a02382 2023-07-10 thomas create_config_files(const char *proto, const char *host, const char *port,
1032 97a02382 2023-07-10 thomas const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1033 97a02382 2023-07-10 thomas int mirror_references, struct got_pathlist_head *symrefs,
1034 97a02382 2023-07-10 thomas struct got_pathlist_head *wanted_branches,
1035 97a02382 2023-07-10 thomas struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1037 97a02382 2023-07-10 thomas static const struct got_error *
1038 97a02382 2023-07-10 thomas fetch_progress(void *arg, const char *message, off_t packfile_size,
1039 97a02382 2023-07-10 thomas int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1041 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
1042 97a02382 2023-07-10 thomas struct got_fetch_progress_arg *a = arg;
1043 97a02382 2023-07-10 thomas char scaled_size[FMT_SCALED_STRSIZE];
1044 97a02382 2023-07-10 thomas int p_indexed, p_resolved;
1045 97a02382 2023-07-10 thomas int print_size = 0, print_indexed = 0, print_resolved = 0;
1048 97a02382 2023-07-10 thomas * In order to allow a failed clone to be resumed with 'got fetch'
1049 97a02382 2023-07-10 thomas * we try to create configuration files as soon as possible.
1050 97a02382 2023-07-10 thomas * Once the server has sent information about its default branch
1051 97a02382 2023-07-10 thomas * we have all required information.
1053 97a02382 2023-07-10 thomas if (a->create_configs && !a->configs_created &&
1054 97a02382 2023-07-10 thomas !TAILQ_EMPTY(a->config_info.symrefs)) {
1055 97a02382 2023-07-10 thomas err = create_config_files(a->config_info.proto,
1056 97a02382 2023-07-10 thomas a->config_info.host, a->config_info.port,
1057 97a02382 2023-07-10 thomas a->config_info.remote_repo_path,
1058 97a02382 2023-07-10 thomas a->config_info.git_url,
1059 97a02382 2023-07-10 thomas a->config_info.fetch_all_branches,
1060 97a02382 2023-07-10 thomas a->config_info.mirror_references,
1061 97a02382 2023-07-10 thomas a->config_info.symrefs,
1062 97a02382 2023-07-10 thomas a->config_info.wanted_branches,
1063 97a02382 2023-07-10 thomas a->config_info.wanted_refs, a->repo);
1064 97a02382 2023-07-10 thomas if (err)
1065 97a02382 2023-07-10 thomas return err;
1066 97a02382 2023-07-10 thomas a->configs_created = 1;
1069 97a02382 2023-07-10 thomas if (a->verbosity < 0)
1070 97a02382 2023-07-10 thomas return NULL;
1072 97a02382 2023-07-10 thomas if (message && message[0] != '\0') {
1073 97a02382 2023-07-10 thomas printf("\rserver: %s", message);
1074 97a02382 2023-07-10 thomas fflush(stdout);
1075 97a02382 2023-07-10 thomas return NULL;
1078 97a02382 2023-07-10 thomas if (packfile_size > 0 || nobj_indexed > 0) {
1079 97a02382 2023-07-10 thomas if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1080 97a02382 2023-07-10 thomas (a->last_scaled_size[0] == '\0' ||
1081 97a02382 2023-07-10 thomas strcmp(scaled_size, a->last_scaled_size)) != 0) {
1082 97a02382 2023-07-10 thomas print_size = 1;
1083 97a02382 2023-07-10 thomas if (strlcpy(a->last_scaled_size, scaled_size,
1084 97a02382 2023-07-10 thomas FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1085 97a02382 2023-07-10 thomas return got_error(GOT_ERR_NO_SPACE);
1087 97a02382 2023-07-10 thomas if (nobj_indexed > 0) {
1088 97a02382 2023-07-10 thomas p_indexed = (nobj_indexed * 100) / nobj_total;
1089 97a02382 2023-07-10 thomas if (p_indexed != a->last_p_indexed) {
1090 97a02382 2023-07-10 thomas a->last_p_indexed = p_indexed;
1091 97a02382 2023-07-10 thomas print_indexed = 1;
1092 97a02382 2023-07-10 thomas print_size = 1;
1095 97a02382 2023-07-10 thomas if (nobj_resolved > 0) {
1096 97a02382 2023-07-10 thomas p_resolved = (nobj_resolved * 100) /
1097 97a02382 2023-07-10 thomas (nobj_total - nobj_loose);
1098 97a02382 2023-07-10 thomas if (p_resolved != a->last_p_resolved) {
1099 97a02382 2023-07-10 thomas a->last_p_resolved = p_resolved;
1100 97a02382 2023-07-10 thomas print_resolved = 1;
1101 97a02382 2023-07-10 thomas print_indexed = 1;
1102 97a02382 2023-07-10 thomas print_size = 1;
1107 97a02382 2023-07-10 thomas if (print_size || print_indexed || print_resolved)
1108 97a02382 2023-07-10 thomas printf("\r");
1109 97a02382 2023-07-10 thomas if (print_size)
1110 97a02382 2023-07-10 thomas printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1111 97a02382 2023-07-10 thomas if (print_indexed)
1112 97a02382 2023-07-10 thomas printf("; indexing %d%%", p_indexed);
1113 97a02382 2023-07-10 thomas if (print_resolved)
1114 97a02382 2023-07-10 thomas printf("; resolving deltas %d%%", p_resolved);
1115 97a02382 2023-07-10 thomas if (print_size || print_indexed || print_resolved)
1116 97a02382 2023-07-10 thomas fflush(stdout);
1118 97a02382 2023-07-10 thomas return NULL;
1121 97a02382 2023-07-10 thomas static const struct got_error *
1122 97a02382 2023-07-10 thomas create_symref(const char *refname, struct got_reference *target_ref,
1123 97a02382 2023-07-10 thomas int verbosity, struct got_repository *repo)
1125 97a02382 2023-07-10 thomas const struct got_error *err;
1126 97a02382 2023-07-10 thomas struct got_reference *head_symref;
1128 97a02382 2023-07-10 thomas err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1129 97a02382 2023-07-10 thomas if (err)
1130 97a02382 2023-07-10 thomas return err;
1132 97a02382 2023-07-10 thomas err = got_ref_write(head_symref, repo);
1133 97a02382 2023-07-10 thomas if (err == NULL && verbosity > 0) {
1134 97a02382 2023-07-10 thomas printf("Created reference %s: %s\n", GOT_REF_HEAD,
1135 97a02382 2023-07-10 thomas got_ref_get_name(target_ref));
1137 97a02382 2023-07-10 thomas got_ref_close(head_symref);
1138 97a02382 2023-07-10 thomas return err;
1141 97a02382 2023-07-10 thomas static const struct got_error *
1142 97a02382 2023-07-10 thomas list_remote_refs(struct got_pathlist_head *symrefs,
1143 97a02382 2023-07-10 thomas struct got_pathlist_head *refs)
1145 97a02382 2023-07-10 thomas const struct got_error *err;
1146 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
1148 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, symrefs, entry) {
1149 97a02382 2023-07-10 thomas const char *refname = pe->path;
1150 97a02382 2023-07-10 thomas const char *targetref = pe->data;
1152 97a02382 2023-07-10 thomas printf("%s: %s\n", refname, targetref);
1155 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, refs, entry) {
1156 97a02382 2023-07-10 thomas const char *refname = pe->path;
1157 97a02382 2023-07-10 thomas struct got_object_id *id = pe->data;
1158 97a02382 2023-07-10 thomas char *id_str;
1160 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, id);
1161 97a02382 2023-07-10 thomas if (err)
1162 97a02382 2023-07-10 thomas return err;
1163 97a02382 2023-07-10 thomas printf("%s: %s\n", refname, id_str);
1164 97a02382 2023-07-10 thomas free(id_str);
1167 97a02382 2023-07-10 thomas return NULL;
1170 97a02382 2023-07-10 thomas static const struct got_error *
1171 97a02382 2023-07-10 thomas create_ref(const char *refname, struct got_object_id *id,
1172 97a02382 2023-07-10 thomas int verbosity, struct got_repository *repo)
1174 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
1175 97a02382 2023-07-10 thomas struct got_reference *ref;
1176 97a02382 2023-07-10 thomas char *id_str;
1178 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, id);
1179 97a02382 2023-07-10 thomas if (err)
1180 97a02382 2023-07-10 thomas return err;
1182 97a02382 2023-07-10 thomas err = got_ref_alloc(&ref, refname, id);
1183 97a02382 2023-07-10 thomas if (err)
1184 97a02382 2023-07-10 thomas goto done;
1186 97a02382 2023-07-10 thomas err = got_ref_write(ref, repo);
1187 97a02382 2023-07-10 thomas got_ref_close(ref);
1189 97a02382 2023-07-10 thomas if (err == NULL && verbosity >= 0)
1190 97a02382 2023-07-10 thomas printf("Created reference %s: %s\n", refname, id_str);
1192 97a02382 2023-07-10 thomas free(id_str);
1193 97a02382 2023-07-10 thomas return err;
1196 97a02382 2023-07-10 thomas static int
1197 97a02382 2023-07-10 thomas match_wanted_ref(const char *refname, const char *wanted_ref)
1199 97a02382 2023-07-10 thomas if (strncmp(refname, "refs/", 5) != 0)
1200 97a02382 2023-07-10 thomas return 0;
1201 97a02382 2023-07-10 thomas refname += 5;
1204 97a02382 2023-07-10 thomas * Prevent fetching of references that won't make any
1205 97a02382 2023-07-10 thomas * sense outside of the remote repository's context.
1207 97a02382 2023-07-10 thomas if (strncmp(refname, "got/", 4) == 0)
1208 97a02382 2023-07-10 thomas return 0;
1209 97a02382 2023-07-10 thomas if (strncmp(refname, "remotes/", 8) == 0)
1210 97a02382 2023-07-10 thomas return 0;
1212 97a02382 2023-07-10 thomas if (strncmp(wanted_ref, "refs/", 5) == 0)
1213 97a02382 2023-07-10 thomas wanted_ref += 5;
1215 97a02382 2023-07-10 thomas /* Allow prefix match. */
1216 97a02382 2023-07-10 thomas if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1217 97a02382 2023-07-10 thomas return 1;
1219 97a02382 2023-07-10 thomas /* Allow exact match. */
1220 97a02382 2023-07-10 thomas return (strcmp(refname, wanted_ref) == 0);
1223 97a02382 2023-07-10 thomas static int
1224 97a02382 2023-07-10 thomas is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1226 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
1228 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, wanted_refs, entry) {
1229 97a02382 2023-07-10 thomas if (match_wanted_ref(refname, pe->path))
1230 97a02382 2023-07-10 thomas return 1;
1233 97a02382 2023-07-10 thomas return 0;
1236 97a02382 2023-07-10 thomas static const struct got_error *
1237 97a02382 2023-07-10 thomas create_wanted_ref(const char *refname, struct got_object_id *id,
1238 97a02382 2023-07-10 thomas const char *remote_repo_name, int verbosity, struct got_repository *repo)
1240 97a02382 2023-07-10 thomas const struct got_error *err;
1241 97a02382 2023-07-10 thomas char *remote_refname;
1243 97a02382 2023-07-10 thomas if (strncmp("refs/", refname, 5) == 0)
1244 97a02382 2023-07-10 thomas refname += 5;
1246 97a02382 2023-07-10 thomas if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1247 97a02382 2023-07-10 thomas remote_repo_name, refname) == -1)
1248 97a02382 2023-07-10 thomas return got_error_from_errno("asprintf");
1250 97a02382 2023-07-10 thomas err = create_ref(remote_refname, id, verbosity, repo);
1251 97a02382 2023-07-10 thomas free(remote_refname);
1252 97a02382 2023-07-10 thomas return err;
1255 97a02382 2023-07-10 thomas static const struct got_error *
1256 97a02382 2023-07-10 thomas create_gotconfig(const char *proto, const char *host, const char *port,
1257 97a02382 2023-07-10 thomas const char *remote_repo_path, const char *default_branch,
1258 97a02382 2023-07-10 thomas int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1259 97a02382 2023-07-10 thomas struct got_pathlist_head *wanted_refs, int mirror_references,
1260 97a02382 2023-07-10 thomas struct got_repository *repo)
1262 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
1263 97a02382 2023-07-10 thomas char *gotconfig_path = NULL;
1264 97a02382 2023-07-10 thomas char *gotconfig = NULL;
1265 97a02382 2023-07-10 thomas FILE *gotconfig_file = NULL;
1266 97a02382 2023-07-10 thomas const char *branchname = NULL;
1267 97a02382 2023-07-10 thomas char *branches = NULL, *refs = NULL;
1268 97a02382 2023-07-10 thomas ssize_t n;
1270 97a02382 2023-07-10 thomas if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1271 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
1272 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, wanted_branches, entry) {
1273 97a02382 2023-07-10 thomas char *s;
1274 97a02382 2023-07-10 thomas branchname = pe->path;
1275 97a02382 2023-07-10 thomas if (strncmp(branchname, "refs/heads/", 11) == 0)
1276 97a02382 2023-07-10 thomas branchname += 11;
1277 97a02382 2023-07-10 thomas if (asprintf(&s, "%s\"%s\" ",
1278 97a02382 2023-07-10 thomas branches ? branches : "", branchname) == -1) {
1279 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
1280 97a02382 2023-07-10 thomas goto done;
1282 97a02382 2023-07-10 thomas free(branches);
1283 97a02382 2023-07-10 thomas branches = s;
1285 97a02382 2023-07-10 thomas } else if (!fetch_all_branches && default_branch) {
1286 97a02382 2023-07-10 thomas branchname = default_branch;
1287 97a02382 2023-07-10 thomas if (strncmp(branchname, "refs/heads/", 11) == 0)
1288 97a02382 2023-07-10 thomas branchname += 11;
1289 97a02382 2023-07-10 thomas if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1290 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
1291 97a02382 2023-07-10 thomas goto done;
1294 97a02382 2023-07-10 thomas if (!TAILQ_EMPTY(wanted_refs)) {
1295 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
1296 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, wanted_refs, entry) {
1297 97a02382 2023-07-10 thomas char *s;
1298 97a02382 2023-07-10 thomas const char *refname = pe->path;
1299 97a02382 2023-07-10 thomas if (strncmp(refname, "refs/", 5) == 0)
1300 97a02382 2023-07-10 thomas branchname += 5;
1301 97a02382 2023-07-10 thomas if (asprintf(&s, "%s\"%s\" ",
1302 97a02382 2023-07-10 thomas refs ? refs : "", refname) == -1) {
1303 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
1304 97a02382 2023-07-10 thomas goto done;
1306 97a02382 2023-07-10 thomas free(refs);
1307 97a02382 2023-07-10 thomas refs = s;
1311 97a02382 2023-07-10 thomas /* Create got.conf(5). */
1312 97a02382 2023-07-10 thomas gotconfig_path = got_repo_get_path_gotconfig(repo);
1313 97a02382 2023-07-10 thomas if (gotconfig_path == NULL) {
1314 97a02382 2023-07-10 thomas err = got_error_from_errno("got_repo_get_path_gotconfig");
1315 97a02382 2023-07-10 thomas goto done;
1317 97a02382 2023-07-10 thomas gotconfig_file = fopen(gotconfig_path, "ae");
1318 97a02382 2023-07-10 thomas if (gotconfig_file == NULL) {
1319 97a02382 2023-07-10 thomas err = got_error_from_errno2("fopen", gotconfig_path);
1320 97a02382 2023-07-10 thomas goto done;
1322 97a02382 2023-07-10 thomas if (asprintf(&gotconfig,
1323 97a02382 2023-07-10 thomas "remote \"%s\" {\n"
1324 97a02382 2023-07-10 thomas "\tserver %s\n"
1325 97a02382 2023-07-10 thomas "\tprotocol %s\n"
1326 97a02382 2023-07-10 thomas "%s%s%s"
1327 97a02382 2023-07-10 thomas "\trepository \"%s\"\n"
1328 97a02382 2023-07-10 thomas "%s%s%s"
1329 97a02382 2023-07-10 thomas "%s%s%s"
1333 97a02382 2023-07-10 thomas GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1334 97a02382 2023-07-10 thomas port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1335 97a02382 2023-07-10 thomas remote_repo_path, branches ? "\tbranch { " : "",
1336 97a02382 2023-07-10 thomas branches ? branches : "", branches ? "}\n" : "",
1337 97a02382 2023-07-10 thomas refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1338 97a02382 2023-07-10 thomas mirror_references ? "\tmirror_references yes\n" : "",
1339 97a02382 2023-07-10 thomas fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1340 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
1341 97a02382 2023-07-10 thomas goto done;
1343 97a02382 2023-07-10 thomas n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1344 97a02382 2023-07-10 thomas if (n != strlen(gotconfig)) {
1345 97a02382 2023-07-10 thomas err = got_ferror(gotconfig_file, GOT_ERR_IO);
1346 97a02382 2023-07-10 thomas goto done;
1350 97a02382 2023-07-10 thomas if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1351 97a02382 2023-07-10 thomas err = got_error_from_errno2("fclose", gotconfig_path);
1352 97a02382 2023-07-10 thomas free(gotconfig_path);
1353 97a02382 2023-07-10 thomas free(branches);
1354 97a02382 2023-07-10 thomas return err;
1357 97a02382 2023-07-10 thomas static const struct got_error *
1358 97a02382 2023-07-10 thomas create_gitconfig(const char *git_url, const char *default_branch,
1359 97a02382 2023-07-10 thomas int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1360 97a02382 2023-07-10 thomas struct got_pathlist_head *wanted_refs, int mirror_references,
1361 97a02382 2023-07-10 thomas struct got_repository *repo)
1363 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
1364 97a02382 2023-07-10 thomas char *gitconfig_path = NULL;
1365 97a02382 2023-07-10 thomas char *gitconfig = NULL;
1366 97a02382 2023-07-10 thomas FILE *gitconfig_file = NULL;
1367 97a02382 2023-07-10 thomas char *branches = NULL, *refs = NULL;
1368 97a02382 2023-07-10 thomas const char *branchname;
1369 97a02382 2023-07-10 thomas ssize_t n;
1371 97a02382 2023-07-10 thomas /* Create a config file Git can understand. */
1372 97a02382 2023-07-10 thomas gitconfig_path = got_repo_get_path_gitconfig(repo);
1373 97a02382 2023-07-10 thomas if (gitconfig_path == NULL) {
1374 97a02382 2023-07-10 thomas err = got_error_from_errno("got_repo_get_path_gitconfig");
1375 97a02382 2023-07-10 thomas goto done;
1377 97a02382 2023-07-10 thomas gitconfig_file = fopen(gitconfig_path, "ae");
1378 97a02382 2023-07-10 thomas if (gitconfig_file == NULL) {
1379 97a02382 2023-07-10 thomas err = got_error_from_errno2("fopen", gitconfig_path);
1380 97a02382 2023-07-10 thomas goto done;
1382 97a02382 2023-07-10 thomas if (fetch_all_branches) {
1383 97a02382 2023-07-10 thomas if (mirror_references) {
1384 97a02382 2023-07-10 thomas if (asprintf(&branches,
1385 97a02382 2023-07-10 thomas "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1386 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
1387 97a02382 2023-07-10 thomas goto done;
1389 97a02382 2023-07-10 thomas } else if (asprintf(&branches,
1390 97a02382 2023-07-10 thomas "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1391 97a02382 2023-07-10 thomas GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1392 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
1393 97a02382 2023-07-10 thomas goto done;
1395 97a02382 2023-07-10 thomas } else if (!TAILQ_EMPTY(wanted_branches)) {
1396 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
1397 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, wanted_branches, entry) {
1398 97a02382 2023-07-10 thomas char *s;
1399 97a02382 2023-07-10 thomas branchname = pe->path;
1400 97a02382 2023-07-10 thomas if (strncmp(branchname, "refs/heads/", 11) == 0)
1401 97a02382 2023-07-10 thomas branchname += 11;
1402 97a02382 2023-07-10 thomas if (mirror_references) {
1403 97a02382 2023-07-10 thomas if (asprintf(&s,
1404 97a02382 2023-07-10 thomas "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1405 97a02382 2023-07-10 thomas branches ? branches : "",
1406 97a02382 2023-07-10 thomas branchname, branchname) == -1) {
1407 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
1408 97a02382 2023-07-10 thomas goto done;
1410 97a02382 2023-07-10 thomas } else if (asprintf(&s,
1411 97a02382 2023-07-10 thomas "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1412 97a02382 2023-07-10 thomas branches ? branches : "",
1413 97a02382 2023-07-10 thomas branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1414 97a02382 2023-07-10 thomas branchname) == -1) {
1415 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
1416 97a02382 2023-07-10 thomas goto done;
1418 97a02382 2023-07-10 thomas free(branches);
1419 97a02382 2023-07-10 thomas branches = s;
1421 97a02382 2023-07-10 thomas } else {
1423 97a02382 2023-07-10 thomas * If the server specified a default branch, use just that one.
1424 97a02382 2023-07-10 thomas * Otherwise fall back to fetching all branches on next fetch.
1426 97a02382 2023-07-10 thomas if (default_branch) {
1427 97a02382 2023-07-10 thomas branchname = default_branch;
1428 97a02382 2023-07-10 thomas if (strncmp(branchname, "refs/heads/", 11) == 0)
1429 97a02382 2023-07-10 thomas branchname += 11;
1431 97a02382 2023-07-10 thomas branchname = "*"; /* fall back to all branches */
1432 97a02382 2023-07-10 thomas if (mirror_references) {
1433 97a02382 2023-07-10 thomas if (asprintf(&branches,
1434 97a02382 2023-07-10 thomas "\tfetch = refs/heads/%s:refs/heads/%s\n",
1435 97a02382 2023-07-10 thomas branchname, branchname) == -1) {
1436 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
1437 97a02382 2023-07-10 thomas goto done;
1439 97a02382 2023-07-10 thomas } else if (asprintf(&branches,
1440 97a02382 2023-07-10 thomas "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1441 97a02382 2023-07-10 thomas branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1442 97a02382 2023-07-10 thomas branchname) == -1) {
1443 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
1444 97a02382 2023-07-10 thomas goto done;
1447 97a02382 2023-07-10 thomas if (!TAILQ_EMPTY(wanted_refs)) {
1448 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
1449 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, wanted_refs, entry) {
1450 97a02382 2023-07-10 thomas char *s;
1451 97a02382 2023-07-10 thomas const char *refname = pe->path;
1452 97a02382 2023-07-10 thomas if (strncmp(refname, "refs/", 5) == 0)
1453 97a02382 2023-07-10 thomas refname += 5;
1454 97a02382 2023-07-10 thomas if (mirror_references) {
1455 97a02382 2023-07-10 thomas if (asprintf(&s,
1456 97a02382 2023-07-10 thomas "%s\tfetch = refs/%s:refs/%s\n",
1457 97a02382 2023-07-10 thomas refs ? refs : "", refname, refname) == -1) {
1458 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
1459 97a02382 2023-07-10 thomas goto done;
1461 97a02382 2023-07-10 thomas } else if (asprintf(&s,
1462 97a02382 2023-07-10 thomas "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1463 97a02382 2023-07-10 thomas refs ? refs : "",
1464 97a02382 2023-07-10 thomas refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1465 97a02382 2023-07-10 thomas refname) == -1) {
1466 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
1467 97a02382 2023-07-10 thomas goto done;
1469 97a02382 2023-07-10 thomas free(refs);
1470 97a02382 2023-07-10 thomas refs = s;
1474 97a02382 2023-07-10 thomas if (asprintf(&gitconfig,
1475 97a02382 2023-07-10 thomas "[remote \"%s\"]\n"
1476 97a02382 2023-07-10 thomas "\turl = %s\n"
1479 97a02382 2023-07-10 thomas "\tfetch = refs/tags/*:refs/tags/*\n",
1480 97a02382 2023-07-10 thomas GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1481 97a02382 2023-07-10 thomas refs ? refs : "") == -1) {
1482 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
1483 97a02382 2023-07-10 thomas goto done;
1485 97a02382 2023-07-10 thomas n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1486 97a02382 2023-07-10 thomas if (n != strlen(gitconfig)) {
1487 97a02382 2023-07-10 thomas err = got_ferror(gitconfig_file, GOT_ERR_IO);
1488 97a02382 2023-07-10 thomas goto done;
1491 97a02382 2023-07-10 thomas if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1492 97a02382 2023-07-10 thomas err = got_error_from_errno2("fclose", gitconfig_path);
1493 97a02382 2023-07-10 thomas free(gitconfig_path);
1494 97a02382 2023-07-10 thomas free(branches);
1495 97a02382 2023-07-10 thomas return err;
1498 97a02382 2023-07-10 thomas static const struct got_error *
1499 97a02382 2023-07-10 thomas create_config_files(const char *proto, const char *host, const char *port,
1500 97a02382 2023-07-10 thomas const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1501 97a02382 2023-07-10 thomas int mirror_references, struct got_pathlist_head *symrefs,
1502 97a02382 2023-07-10 thomas struct got_pathlist_head *wanted_branches,
1503 97a02382 2023-07-10 thomas struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1505 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
1506 97a02382 2023-07-10 thomas const char *default_branch = NULL;
1507 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
1510 97a02382 2023-07-10 thomas * If we asked for a set of wanted branches then use the first
1511 97a02382 2023-07-10 thomas * one of those.
1513 97a02382 2023-07-10 thomas if (!TAILQ_EMPTY(wanted_branches)) {
1514 97a02382 2023-07-10 thomas pe = TAILQ_FIRST(wanted_branches);
1515 97a02382 2023-07-10 thomas default_branch = pe->path;
1516 97a02382 2023-07-10 thomas } else {
1517 97a02382 2023-07-10 thomas /* First HEAD ref listed by server is the default branch. */
1518 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, symrefs, entry) {
1519 97a02382 2023-07-10 thomas const char *refname = pe->path;
1520 97a02382 2023-07-10 thomas const char *target = pe->data;
1522 97a02382 2023-07-10 thomas if (strcmp(refname, GOT_REF_HEAD) != 0)
1523 97a02382 2023-07-10 thomas continue;
1525 97a02382 2023-07-10 thomas default_branch = target;
1530 97a02382 2023-07-10 thomas /* Create got.conf(5). */
1531 97a02382 2023-07-10 thomas err = create_gotconfig(proto, host, port, remote_repo_path,
1532 97a02382 2023-07-10 thomas default_branch, fetch_all_branches, wanted_branches,
1533 97a02382 2023-07-10 thomas wanted_refs, mirror_references, repo);
1534 97a02382 2023-07-10 thomas if (err)
1535 97a02382 2023-07-10 thomas return err;
1537 97a02382 2023-07-10 thomas /* Create a config file Git can understand. */
1538 97a02382 2023-07-10 thomas return create_gitconfig(git_url, default_branch, fetch_all_branches,
1539 97a02382 2023-07-10 thomas wanted_branches, wanted_refs, mirror_references, repo);
1542 97a02382 2023-07-10 thomas static const struct got_error *
1543 97a02382 2023-07-10 thomas cmd_clone(int argc, char *argv[])
1545 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
1546 97a02382 2023-07-10 thomas const char *uri, *dirname;
1547 97a02382 2023-07-10 thomas char *proto, *host, *port, *repo_name, *server_path;
1548 97a02382 2023-07-10 thomas char *default_destdir = NULL, *id_str = NULL;
1549 97a02382 2023-07-10 thomas const char *repo_path;
1550 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
1551 97a02382 2023-07-10 thomas struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1552 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
1553 97a02382 2023-07-10 thomas struct got_object_id *pack_hash = NULL;
1554 97a02382 2023-07-10 thomas int ch, fetchfd = -1, fetchstatus;
1555 97a02382 2023-07-10 thomas pid_t fetchpid = -1;
1556 97a02382 2023-07-10 thomas struct got_fetch_progress_arg fpa;
1557 97a02382 2023-07-10 thomas char *git_url = NULL;
1558 97a02382 2023-07-10 thomas int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1559 97a02382 2023-07-10 thomas int bflag = 0, list_refs_only = 0;
1560 97a02382 2023-07-10 thomas int *pack_fds = NULL;
1562 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
1563 97a02382 2023-07-10 thomas TAILQ_INIT(&symrefs);
1564 97a02382 2023-07-10 thomas TAILQ_INIT(&wanted_branches);
1565 97a02382 2023-07-10 thomas TAILQ_INIT(&wanted_refs);
1567 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "ab:lmqR:v")) != -1) {
1568 97a02382 2023-07-10 thomas switch (ch) {
1569 97a02382 2023-07-10 thomas case 'a':
1570 97a02382 2023-07-10 thomas fetch_all_branches = 1;
1572 97a02382 2023-07-10 thomas case 'b':
1573 97a02382 2023-07-10 thomas error = got_pathlist_append(&wanted_branches,
1574 97a02382 2023-07-10 thomas optarg, NULL);
1575 97a02382 2023-07-10 thomas if (error)
1576 97a02382 2023-07-10 thomas return error;
1577 97a02382 2023-07-10 thomas bflag = 1;
1579 97a02382 2023-07-10 thomas case 'l':
1580 97a02382 2023-07-10 thomas list_refs_only = 1;
1582 97a02382 2023-07-10 thomas case 'm':
1583 97a02382 2023-07-10 thomas mirror_references = 1;
1585 97a02382 2023-07-10 thomas case 'q':
1586 97a02382 2023-07-10 thomas verbosity = -1;
1588 97a02382 2023-07-10 thomas case 'R':
1589 97a02382 2023-07-10 thomas error = got_pathlist_append(&wanted_refs,
1590 97a02382 2023-07-10 thomas optarg, NULL);
1591 97a02382 2023-07-10 thomas if (error)
1592 97a02382 2023-07-10 thomas return error;
1594 97a02382 2023-07-10 thomas case 'v':
1595 97a02382 2023-07-10 thomas if (verbosity < 0)
1596 97a02382 2023-07-10 thomas verbosity = 0;
1597 97a02382 2023-07-10 thomas else if (verbosity < 3)
1598 97a02382 2023-07-10 thomas verbosity++;
1600 97a02382 2023-07-10 thomas default:
1601 97a02382 2023-07-10 thomas usage_clone();
1605 97a02382 2023-07-10 thomas argc -= optind;
1606 97a02382 2023-07-10 thomas argv += optind;
1608 97a02382 2023-07-10 thomas if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1609 97a02382 2023-07-10 thomas option_conflict('a', 'b');
1610 97a02382 2023-07-10 thomas if (list_refs_only) {
1611 97a02382 2023-07-10 thomas if (!TAILQ_EMPTY(&wanted_branches))
1612 97a02382 2023-07-10 thomas option_conflict('l', 'b');
1613 97a02382 2023-07-10 thomas if (fetch_all_branches)
1614 97a02382 2023-07-10 thomas option_conflict('l', 'a');
1615 97a02382 2023-07-10 thomas if (mirror_references)
1616 97a02382 2023-07-10 thomas option_conflict('l', 'm');
1617 97a02382 2023-07-10 thomas if (!TAILQ_EMPTY(&wanted_refs))
1618 97a02382 2023-07-10 thomas option_conflict('l', 'R');
1621 97a02382 2023-07-10 thomas uri = argv[0];
1623 97a02382 2023-07-10 thomas if (argc == 1)
1624 97a02382 2023-07-10 thomas dirname = NULL;
1625 97a02382 2023-07-10 thomas else if (argc == 2)
1626 97a02382 2023-07-10 thomas dirname = argv[1];
1628 97a02382 2023-07-10 thomas usage_clone();
1630 97a02382 2023-07-10 thomas error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1631 97a02382 2023-07-10 thomas &repo_name, uri);
1632 97a02382 2023-07-10 thomas if (error)
1633 97a02382 2023-07-10 thomas goto done;
1635 97a02382 2023-07-10 thomas if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1636 97a02382 2023-07-10 thomas host, port ? ":" : "", port ? port : "",
1637 97a02382 2023-07-10 thomas server_path[0] != '/' ? "/" : "", server_path) == -1) {
1638 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
1639 97a02382 2023-07-10 thomas goto done;
1642 97a02382 2023-07-10 thomas if (strcmp(proto, "git") == 0) {
1643 97a02382 2023-07-10 thomas #ifndef PROFILE
1644 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1645 97a02382 2023-07-10 thomas "sendfd dns inet unveil", NULL) == -1)
1646 97a02382 2023-07-10 thomas err(1, "pledge");
1648 97a02382 2023-07-10 thomas } else if (strcmp(proto, "git+ssh") == 0 ||
1649 97a02382 2023-07-10 thomas strcmp(proto, "ssh") == 0) {
1650 97a02382 2023-07-10 thomas #ifndef PROFILE
1651 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1652 97a02382 2023-07-10 thomas "sendfd unveil", NULL) == -1)
1653 97a02382 2023-07-10 thomas err(1, "pledge");
1655 97a02382 2023-07-10 thomas } else if (strcmp(proto, "http") == 0 ||
1656 97a02382 2023-07-10 thomas strcmp(proto, "git+http") == 0) {
1657 97a02382 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1658 97a02382 2023-07-10 thomas goto done;
1659 97a02382 2023-07-10 thomas } else {
1660 97a02382 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1661 97a02382 2023-07-10 thomas goto done;
1663 97a02382 2023-07-10 thomas if (dirname == NULL) {
1664 97a02382 2023-07-10 thomas if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1665 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
1666 97a02382 2023-07-10 thomas goto done;
1668 97a02382 2023-07-10 thomas repo_path = default_destdir;
1670 97a02382 2023-07-10 thomas repo_path = dirname;
1672 97a02382 2023-07-10 thomas if (!list_refs_only) {
1673 97a02382 2023-07-10 thomas error = got_path_mkdir(repo_path);
1674 97a02382 2023-07-10 thomas if (error &&
1675 97a02382 2023-07-10 thomas (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1676 97a02382 2023-07-10 thomas !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1677 97a02382 2023-07-10 thomas goto done;
1678 97a02382 2023-07-10 thomas if (!got_path_dir_is_empty(repo_path)) {
1679 97a02382 2023-07-10 thomas error = got_error_path(repo_path,
1680 97a02382 2023-07-10 thomas GOT_ERR_DIR_NOT_EMPTY);
1681 97a02382 2023-07-10 thomas goto done;
1685 97a02382 2023-07-10 thomas error = got_dial_apply_unveil(proto);
1686 97a02382 2023-07-10 thomas if (error)
1687 97a02382 2023-07-10 thomas goto done;
1689 97a02382 2023-07-10 thomas error = apply_unveil(repo_path, 0, NULL);
1690 97a02382 2023-07-10 thomas if (error)
1691 97a02382 2023-07-10 thomas goto done;
1693 97a02382 2023-07-10 thomas if (verbosity >= 0)
1694 97a02382 2023-07-10 thomas printf("Connecting to %s\n", git_url);
1696 97a02382 2023-07-10 thomas error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1697 97a02382 2023-07-10 thomas server_path, verbosity);
1698 97a02382 2023-07-10 thomas if (error)
1699 97a02382 2023-07-10 thomas goto done;
1701 97a02382 2023-07-10 thomas if (!list_refs_only) {
1702 97a02382 2023-07-10 thomas error = got_repo_init(repo_path, NULL);
1703 97a02382 2023-07-10 thomas if (error)
1704 97a02382 2023-07-10 thomas goto done;
1705 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
1706 97a02382 2023-07-10 thomas if (error != NULL)
1707 97a02382 2023-07-10 thomas goto done;
1708 97a02382 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1709 97a02382 2023-07-10 thomas if (error)
1710 97a02382 2023-07-10 thomas goto done;
1713 97a02382 2023-07-10 thomas fpa.last_scaled_size[0] = '\0';
1714 97a02382 2023-07-10 thomas fpa.last_p_indexed = -1;
1715 97a02382 2023-07-10 thomas fpa.last_p_resolved = -1;
1716 97a02382 2023-07-10 thomas fpa.verbosity = verbosity;
1717 97a02382 2023-07-10 thomas fpa.create_configs = 1;
1718 97a02382 2023-07-10 thomas fpa.configs_created = 0;
1719 97a02382 2023-07-10 thomas fpa.repo = repo;
1720 97a02382 2023-07-10 thomas fpa.config_info.symrefs = &symrefs;
1721 97a02382 2023-07-10 thomas fpa.config_info.wanted_branches = &wanted_branches;
1722 97a02382 2023-07-10 thomas fpa.config_info.wanted_refs = &wanted_refs;
1723 97a02382 2023-07-10 thomas fpa.config_info.proto = proto;
1724 97a02382 2023-07-10 thomas fpa.config_info.host = host;
1725 97a02382 2023-07-10 thomas fpa.config_info.port = port;
1726 97a02382 2023-07-10 thomas fpa.config_info.remote_repo_path = server_path;
1727 97a02382 2023-07-10 thomas fpa.config_info.git_url = git_url;
1728 97a02382 2023-07-10 thomas fpa.config_info.fetch_all_branches = fetch_all_branches;
1729 97a02382 2023-07-10 thomas fpa.config_info.mirror_references = mirror_references;
1730 97a02382 2023-07-10 thomas error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1731 97a02382 2023-07-10 thomas GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1732 97a02382 2023-07-10 thomas fetch_all_branches, &wanted_branches, &wanted_refs,
1733 97a02382 2023-07-10 thomas list_refs_only, verbosity, fetchfd, repo, NULL, NULL, bflag,
1734 97a02382 2023-07-10 thomas fetch_progress, &fpa);
1735 97a02382 2023-07-10 thomas if (error)
1736 97a02382 2023-07-10 thomas goto done;
1738 97a02382 2023-07-10 thomas if (list_refs_only) {
1739 97a02382 2023-07-10 thomas error = list_remote_refs(&symrefs, &refs);
1740 97a02382 2023-07-10 thomas goto done;
1743 97a02382 2023-07-10 thomas if (pack_hash == NULL) {
1744 97a02382 2023-07-10 thomas error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1745 97a02382 2023-07-10 thomas "server sent an empty pack file");
1746 97a02382 2023-07-10 thomas goto done;
1748 97a02382 2023-07-10 thomas error = got_object_id_str(&id_str, pack_hash);
1749 97a02382 2023-07-10 thomas if (error)
1750 97a02382 2023-07-10 thomas goto done;
1751 97a02382 2023-07-10 thomas if (verbosity >= 0)
1752 97a02382 2023-07-10 thomas printf("\nFetched %s.pack\n", id_str);
1753 97a02382 2023-07-10 thomas free(id_str);
1755 97a02382 2023-07-10 thomas /* Set up references provided with the pack file. */
1756 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, &refs, entry) {
1757 97a02382 2023-07-10 thomas const char *refname = pe->path;
1758 97a02382 2023-07-10 thomas struct got_object_id *id = pe->data;
1759 97a02382 2023-07-10 thomas char *remote_refname;
1761 97a02382 2023-07-10 thomas if (is_wanted_ref(&wanted_refs, refname) &&
1762 97a02382 2023-07-10 thomas !mirror_references) {
1763 97a02382 2023-07-10 thomas error = create_wanted_ref(refname, id,
1764 97a02382 2023-07-10 thomas GOT_FETCH_DEFAULT_REMOTE_NAME,
1765 97a02382 2023-07-10 thomas verbosity - 1, repo);
1766 97a02382 2023-07-10 thomas if (error)
1767 97a02382 2023-07-10 thomas goto done;
1768 97a02382 2023-07-10 thomas continue;
1771 97a02382 2023-07-10 thomas error = create_ref(refname, id, verbosity - 1, repo);
1772 97a02382 2023-07-10 thomas if (error)
1773 97a02382 2023-07-10 thomas goto done;
1775 97a02382 2023-07-10 thomas if (mirror_references)
1776 97a02382 2023-07-10 thomas continue;
1778 97a02382 2023-07-10 thomas if (strncmp("refs/heads/", refname, 11) != 0)
1779 97a02382 2023-07-10 thomas continue;
1781 97a02382 2023-07-10 thomas if (asprintf(&remote_refname,
1782 97a02382 2023-07-10 thomas "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1783 97a02382 2023-07-10 thomas refname + 11) == -1) {
1784 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
1785 97a02382 2023-07-10 thomas goto done;
1787 97a02382 2023-07-10 thomas error = create_ref(remote_refname, id, verbosity - 1, repo);
1788 97a02382 2023-07-10 thomas free(remote_refname);
1789 97a02382 2023-07-10 thomas if (error)
1790 97a02382 2023-07-10 thomas goto done;
1793 97a02382 2023-07-10 thomas /* Set the HEAD reference if the server provided one. */
1794 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, &symrefs, entry) {
1795 97a02382 2023-07-10 thomas struct got_reference *target_ref;
1796 97a02382 2023-07-10 thomas const char *refname = pe->path;
1797 97a02382 2023-07-10 thomas const char *target = pe->data;
1798 97a02382 2023-07-10 thomas char *remote_refname = NULL, *remote_target = NULL;
1800 97a02382 2023-07-10 thomas if (strcmp(refname, GOT_REF_HEAD) != 0)
1801 97a02382 2023-07-10 thomas continue;
1803 97a02382 2023-07-10 thomas error = got_ref_open(&target_ref, repo, target, 0);
1804 97a02382 2023-07-10 thomas if (error) {
1805 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_NOT_REF) {
1806 97a02382 2023-07-10 thomas error = NULL;
1807 97a02382 2023-07-10 thomas continue;
1809 97a02382 2023-07-10 thomas goto done;
1812 97a02382 2023-07-10 thomas error = create_symref(refname, target_ref, verbosity, repo);
1813 97a02382 2023-07-10 thomas got_ref_close(target_ref);
1814 97a02382 2023-07-10 thomas if (error)
1815 97a02382 2023-07-10 thomas goto done;
1817 97a02382 2023-07-10 thomas if (mirror_references)
1818 97a02382 2023-07-10 thomas continue;
1820 97a02382 2023-07-10 thomas if (strncmp("refs/heads/", target, 11) != 0)
1821 97a02382 2023-07-10 thomas continue;
1823 97a02382 2023-07-10 thomas if (asprintf(&remote_refname,
1824 97a02382 2023-07-10 thomas "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1825 97a02382 2023-07-10 thomas refname) == -1) {
1826 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
1827 97a02382 2023-07-10 thomas goto done;
1829 97a02382 2023-07-10 thomas if (asprintf(&remote_target,
1830 97a02382 2023-07-10 thomas "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1831 97a02382 2023-07-10 thomas target + 11) == -1) {
1832 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
1833 97a02382 2023-07-10 thomas free(remote_refname);
1834 97a02382 2023-07-10 thomas goto done;
1836 97a02382 2023-07-10 thomas error = got_ref_open(&target_ref, repo, remote_target, 0);
1837 97a02382 2023-07-10 thomas if (error) {
1838 97a02382 2023-07-10 thomas free(remote_refname);
1839 97a02382 2023-07-10 thomas free(remote_target);
1840 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_NOT_REF) {
1841 97a02382 2023-07-10 thomas error = NULL;
1842 97a02382 2023-07-10 thomas continue;
1844 97a02382 2023-07-10 thomas goto done;
1846 97a02382 2023-07-10 thomas error = create_symref(remote_refname, target_ref,
1847 97a02382 2023-07-10 thomas verbosity - 1, repo);
1848 97a02382 2023-07-10 thomas free(remote_refname);
1849 97a02382 2023-07-10 thomas free(remote_target);
1850 97a02382 2023-07-10 thomas got_ref_close(target_ref);
1851 97a02382 2023-07-10 thomas if (error)
1852 97a02382 2023-07-10 thomas goto done;
1854 97a02382 2023-07-10 thomas if (pe == NULL) {
1856 97a02382 2023-07-10 thomas * We failed to set the HEAD reference. If we asked for
1857 97a02382 2023-07-10 thomas * a set of wanted branches use the first of one of those
1858 97a02382 2023-07-10 thomas * which could be fetched instead.
1860 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, &wanted_branches, entry) {
1861 97a02382 2023-07-10 thomas const char *target = pe->path;
1862 97a02382 2023-07-10 thomas struct got_reference *target_ref;
1864 97a02382 2023-07-10 thomas error = got_ref_open(&target_ref, repo, target, 0);
1865 97a02382 2023-07-10 thomas if (error) {
1866 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_NOT_REF) {
1867 97a02382 2023-07-10 thomas error = NULL;
1868 97a02382 2023-07-10 thomas continue;
1870 97a02382 2023-07-10 thomas goto done;
1873 97a02382 2023-07-10 thomas error = create_symref(GOT_REF_HEAD, target_ref,
1874 97a02382 2023-07-10 thomas verbosity, repo);
1875 97a02382 2023-07-10 thomas got_ref_close(target_ref);
1876 97a02382 2023-07-10 thomas if (error)
1877 97a02382 2023-07-10 thomas goto done;
1881 97a02382 2023-07-10 thomas if (!fpa.configs_created && pe != NULL) {
1882 97a02382 2023-07-10 thomas error = create_config_files(fpa.config_info.proto,
1883 97a02382 2023-07-10 thomas fpa.config_info.host, fpa.config_info.port,
1884 97a02382 2023-07-10 thomas fpa.config_info.remote_repo_path,
1885 97a02382 2023-07-10 thomas fpa.config_info.git_url,
1886 97a02382 2023-07-10 thomas fpa.config_info.fetch_all_branches,
1887 97a02382 2023-07-10 thomas fpa.config_info.mirror_references,
1888 97a02382 2023-07-10 thomas fpa.config_info.symrefs,
1889 97a02382 2023-07-10 thomas fpa.config_info.wanted_branches,
1890 97a02382 2023-07-10 thomas fpa.config_info.wanted_refs, fpa.repo);
1891 97a02382 2023-07-10 thomas if (error)
1892 97a02382 2023-07-10 thomas goto done;
1896 97a02382 2023-07-10 thomas if (verbosity >= 0)
1897 97a02382 2023-07-10 thomas printf("Created %s repository '%s'\n",
1898 97a02382 2023-07-10 thomas mirror_references ? "mirrored" : "cloned", repo_path);
1900 97a02382 2023-07-10 thomas if (pack_fds) {
1901 97a02382 2023-07-10 thomas const struct got_error *pack_err =
1902 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
1903 97a02382 2023-07-10 thomas if (error == NULL)
1904 97a02382 2023-07-10 thomas error = pack_err;
1906 97a02382 2023-07-10 thomas if (fetchpid > 0) {
1907 97a02382 2023-07-10 thomas if (kill(fetchpid, SIGTERM) == -1)
1908 97a02382 2023-07-10 thomas error = got_error_from_errno("kill");
1909 97a02382 2023-07-10 thomas if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1910 97a02382 2023-07-10 thomas error = got_error_from_errno("waitpid");
1912 97a02382 2023-07-10 thomas if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1913 97a02382 2023-07-10 thomas error = got_error_from_errno("close");
1914 97a02382 2023-07-10 thomas if (repo) {
1915 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
1916 97a02382 2023-07-10 thomas if (error == NULL)
1917 97a02382 2023-07-10 thomas error = close_err;
1919 97a02382 2023-07-10 thomas got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
1920 97a02382 2023-07-10 thomas got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
1921 97a02382 2023-07-10 thomas got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
1922 97a02382 2023-07-10 thomas got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
1923 97a02382 2023-07-10 thomas free(pack_hash);
1924 97a02382 2023-07-10 thomas free(proto);
1925 97a02382 2023-07-10 thomas free(host);
1926 97a02382 2023-07-10 thomas free(port);
1927 97a02382 2023-07-10 thomas free(server_path);
1928 97a02382 2023-07-10 thomas free(repo_name);
1929 97a02382 2023-07-10 thomas free(default_destdir);
1930 97a02382 2023-07-10 thomas free(git_url);
1931 97a02382 2023-07-10 thomas return error;
1934 97a02382 2023-07-10 thomas static const struct got_error *
1935 97a02382 2023-07-10 thomas update_ref(struct got_reference *ref, struct got_object_id *new_id,
1936 97a02382 2023-07-10 thomas int replace_tags, int verbosity, struct got_repository *repo)
1938 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
1939 97a02382 2023-07-10 thomas char *new_id_str = NULL;
1940 97a02382 2023-07-10 thomas struct got_object_id *old_id = NULL;
1942 97a02382 2023-07-10 thomas err = got_object_id_str(&new_id_str, new_id);
1943 97a02382 2023-07-10 thomas if (err)
1944 97a02382 2023-07-10 thomas goto done;
1946 97a02382 2023-07-10 thomas if (!replace_tags &&
1947 97a02382 2023-07-10 thomas strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1948 97a02382 2023-07-10 thomas err = got_ref_resolve(&old_id, repo, ref);
1949 97a02382 2023-07-10 thomas if (err)
1950 97a02382 2023-07-10 thomas goto done;
1951 97a02382 2023-07-10 thomas if (got_object_id_cmp(old_id, new_id) == 0)
1952 97a02382 2023-07-10 thomas goto done;
1953 97a02382 2023-07-10 thomas if (verbosity >= 0) {
1954 97a02382 2023-07-10 thomas printf("Rejecting update of existing tag %s: %s\n",
1955 97a02382 2023-07-10 thomas got_ref_get_name(ref), new_id_str);
1957 97a02382 2023-07-10 thomas goto done;
1960 97a02382 2023-07-10 thomas if (got_ref_is_symbolic(ref)) {
1961 97a02382 2023-07-10 thomas if (verbosity >= 0) {
1962 97a02382 2023-07-10 thomas printf("Replacing reference %s: %s\n",
1963 97a02382 2023-07-10 thomas got_ref_get_name(ref),
1964 97a02382 2023-07-10 thomas got_ref_get_symref_target(ref));
1966 97a02382 2023-07-10 thomas err = got_ref_change_symref_to_ref(ref, new_id);
1967 97a02382 2023-07-10 thomas if (err)
1968 97a02382 2023-07-10 thomas goto done;
1969 97a02382 2023-07-10 thomas err = got_ref_write(ref, repo);
1970 97a02382 2023-07-10 thomas if (err)
1971 97a02382 2023-07-10 thomas goto done;
1972 97a02382 2023-07-10 thomas } else {
1973 97a02382 2023-07-10 thomas err = got_ref_resolve(&old_id, repo, ref);
1974 97a02382 2023-07-10 thomas if (err)
1975 97a02382 2023-07-10 thomas goto done;
1976 97a02382 2023-07-10 thomas if (got_object_id_cmp(old_id, new_id) == 0)
1977 97a02382 2023-07-10 thomas goto done;
1979 97a02382 2023-07-10 thomas err = got_ref_change_ref(ref, new_id);
1980 97a02382 2023-07-10 thomas if (err)
1981 97a02382 2023-07-10 thomas goto done;
1982 97a02382 2023-07-10 thomas err = got_ref_write(ref, repo);
1983 97a02382 2023-07-10 thomas if (err)
1984 97a02382 2023-07-10 thomas goto done;
1987 97a02382 2023-07-10 thomas if (verbosity >= 0)
1988 97a02382 2023-07-10 thomas printf("Updated %s: %s\n", got_ref_get_name(ref),
1989 97a02382 2023-07-10 thomas new_id_str);
1991 97a02382 2023-07-10 thomas free(old_id);
1992 97a02382 2023-07-10 thomas free(new_id_str);
1993 97a02382 2023-07-10 thomas return err;
1996 97a02382 2023-07-10 thomas static const struct got_error *
1997 97a02382 2023-07-10 thomas update_symref(const char *refname, struct got_reference *target_ref,
1998 97a02382 2023-07-10 thomas int verbosity, struct got_repository *repo)
2000 97a02382 2023-07-10 thomas const struct got_error *err = NULL, *unlock_err;
2001 97a02382 2023-07-10 thomas struct got_reference *symref;
2002 97a02382 2023-07-10 thomas int symref_is_locked = 0;
2004 97a02382 2023-07-10 thomas err = got_ref_open(&symref, repo, refname, 1);
2005 97a02382 2023-07-10 thomas if (err) {
2006 97a02382 2023-07-10 thomas if (err->code != GOT_ERR_NOT_REF)
2007 97a02382 2023-07-10 thomas return err;
2008 97a02382 2023-07-10 thomas err = got_ref_alloc_symref(&symref, refname, target_ref);
2009 97a02382 2023-07-10 thomas if (err)
2010 97a02382 2023-07-10 thomas goto done;
2012 97a02382 2023-07-10 thomas err = got_ref_write(symref, repo);
2013 97a02382 2023-07-10 thomas if (err)
2014 97a02382 2023-07-10 thomas goto done;
2016 97a02382 2023-07-10 thomas if (verbosity >= 0)
2017 97a02382 2023-07-10 thomas printf("Created reference %s: %s\n",
2018 97a02382 2023-07-10 thomas got_ref_get_name(symref),
2019 97a02382 2023-07-10 thomas got_ref_get_symref_target(symref));
2020 97a02382 2023-07-10 thomas } else {
2021 97a02382 2023-07-10 thomas symref_is_locked = 1;
2023 97a02382 2023-07-10 thomas if (strcmp(got_ref_get_symref_target(symref),
2024 97a02382 2023-07-10 thomas got_ref_get_name(target_ref)) == 0)
2025 97a02382 2023-07-10 thomas goto done;
2027 97a02382 2023-07-10 thomas err = got_ref_change_symref(symref,
2028 97a02382 2023-07-10 thomas got_ref_get_name(target_ref));
2029 97a02382 2023-07-10 thomas if (err)
2030 97a02382 2023-07-10 thomas goto done;
2032 97a02382 2023-07-10 thomas err = got_ref_write(symref, repo);
2033 97a02382 2023-07-10 thomas if (err)
2034 97a02382 2023-07-10 thomas goto done;
2036 97a02382 2023-07-10 thomas if (verbosity >= 0)
2037 97a02382 2023-07-10 thomas printf("Updated %s: %s\n", got_ref_get_name(symref),
2038 97a02382 2023-07-10 thomas got_ref_get_symref_target(symref));
2042 97a02382 2023-07-10 thomas if (symref_is_locked) {
2043 97a02382 2023-07-10 thomas unlock_err = got_ref_unlock(symref);
2044 97a02382 2023-07-10 thomas if (unlock_err && err == NULL)
2045 97a02382 2023-07-10 thomas err = unlock_err;
2047 97a02382 2023-07-10 thomas got_ref_close(symref);
2048 97a02382 2023-07-10 thomas return err;
2051 97a02382 2023-07-10 thomas __dead static void
2052 97a02382 2023-07-10 thomas usage_fetch(void)
2054 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s fetch [-adlqtvX] [-b branch] "
2055 97a02382 2023-07-10 thomas "[-R reference] [-r repository-path] [remote-repository]\n",
2056 97a02382 2023-07-10 thomas getprogname());
2057 97a02382 2023-07-10 thomas exit(1);
2060 97a02382 2023-07-10 thomas static const struct got_error *
2061 97a02382 2023-07-10 thomas delete_missing_ref(struct got_reference *ref,
2062 97a02382 2023-07-10 thomas int verbosity, struct got_repository *repo)
2064 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
2065 97a02382 2023-07-10 thomas struct got_object_id *id = NULL;
2066 97a02382 2023-07-10 thomas char *id_str = NULL;
2068 97a02382 2023-07-10 thomas if (got_ref_is_symbolic(ref)) {
2069 97a02382 2023-07-10 thomas err = got_ref_delete(ref, repo);
2070 97a02382 2023-07-10 thomas if (err)
2071 97a02382 2023-07-10 thomas return err;
2072 97a02382 2023-07-10 thomas if (verbosity >= 0) {
2073 97a02382 2023-07-10 thomas printf("Deleted %s: %s\n",
2074 97a02382 2023-07-10 thomas got_ref_get_name(ref),
2075 97a02382 2023-07-10 thomas got_ref_get_symref_target(ref));
2077 97a02382 2023-07-10 thomas } else {
2078 97a02382 2023-07-10 thomas err = got_ref_resolve(&id, repo, ref);
2079 97a02382 2023-07-10 thomas if (err)
2080 97a02382 2023-07-10 thomas return err;
2081 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, id);
2082 97a02382 2023-07-10 thomas if (err)
2083 97a02382 2023-07-10 thomas goto done;
2085 97a02382 2023-07-10 thomas err = got_ref_delete(ref, repo);
2086 97a02382 2023-07-10 thomas if (err)
2087 97a02382 2023-07-10 thomas goto done;
2088 97a02382 2023-07-10 thomas if (verbosity >= 0) {
2089 97a02382 2023-07-10 thomas printf("Deleted %s: %s\n",
2090 97a02382 2023-07-10 thomas got_ref_get_name(ref), id_str);
2094 97a02382 2023-07-10 thomas free(id);
2095 97a02382 2023-07-10 thomas free(id_str);
2096 97a02382 2023-07-10 thomas return err;
2099 97a02382 2023-07-10 thomas static const struct got_error *
2100 97a02382 2023-07-10 thomas delete_missing_refs(struct got_pathlist_head *their_refs,
2101 97a02382 2023-07-10 thomas struct got_pathlist_head *their_symrefs,
2102 97a02382 2023-07-10 thomas const struct got_remote_repo *remote,
2103 97a02382 2023-07-10 thomas int verbosity, struct got_repository *repo)
2105 97a02382 2023-07-10 thomas const struct got_error *err = NULL, *unlock_err;
2106 97a02382 2023-07-10 thomas struct got_reflist_head my_refs;
2107 97a02382 2023-07-10 thomas struct got_reflist_entry *re;
2108 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
2109 97a02382 2023-07-10 thomas char *remote_namespace = NULL;
2110 97a02382 2023-07-10 thomas char *local_refname = NULL;
2112 97a02382 2023-07-10 thomas TAILQ_INIT(&my_refs);
2114 97a02382 2023-07-10 thomas if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2116 97a02382 2023-07-10 thomas return got_error_from_errno("asprintf");
2118 97a02382 2023-07-10 thomas err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2119 97a02382 2023-07-10 thomas if (err)
2120 97a02382 2023-07-10 thomas goto done;
2122 97a02382 2023-07-10 thomas TAILQ_FOREACH(re, &my_refs, entry) {
2123 97a02382 2023-07-10 thomas const char *refname = got_ref_get_name(re->ref);
2124 97a02382 2023-07-10 thomas const char *their_refname;
2126 97a02382 2023-07-10 thomas if (remote->mirror_references) {
2127 97a02382 2023-07-10 thomas their_refname = refname;
2128 97a02382 2023-07-10 thomas } else {
2129 97a02382 2023-07-10 thomas if (strncmp(refname, remote_namespace,
2130 97a02382 2023-07-10 thomas strlen(remote_namespace)) == 0) {
2131 97a02382 2023-07-10 thomas if (strcmp(refname + strlen(remote_namespace),
2132 97a02382 2023-07-10 thomas GOT_REF_HEAD) == 0)
2133 97a02382 2023-07-10 thomas continue;
2134 97a02382 2023-07-10 thomas if (asprintf(&local_refname, "refs/heads/%s",
2135 97a02382 2023-07-10 thomas refname + strlen(remote_namespace)) == -1) {
2136 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
2137 97a02382 2023-07-10 thomas goto done;
2139 97a02382 2023-07-10 thomas } else if (strncmp(refname, "refs/tags/", 10) != 0)
2140 97a02382 2023-07-10 thomas continue;
2142 97a02382 2023-07-10 thomas their_refname = local_refname;
2145 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, their_refs, entry) {
2146 97a02382 2023-07-10 thomas if (strcmp(their_refname, pe->path) == 0)
2149 97a02382 2023-07-10 thomas if (pe != NULL)
2150 97a02382 2023-07-10 thomas continue;
2152 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, their_symrefs, entry) {
2153 97a02382 2023-07-10 thomas if (strcmp(their_refname, pe->path) == 0)
2156 97a02382 2023-07-10 thomas if (pe != NULL)
2157 97a02382 2023-07-10 thomas continue;
2159 97a02382 2023-07-10 thomas err = delete_missing_ref(re->ref, verbosity, repo);
2160 97a02382 2023-07-10 thomas if (err)
2163 97a02382 2023-07-10 thomas if (local_refname) {
2164 97a02382 2023-07-10 thomas struct got_reference *ref;
2165 97a02382 2023-07-10 thomas err = got_ref_open(&ref, repo, local_refname, 1);
2166 97a02382 2023-07-10 thomas if (err) {
2167 97a02382 2023-07-10 thomas if (err->code != GOT_ERR_NOT_REF)
2169 97a02382 2023-07-10 thomas free(local_refname);
2170 97a02382 2023-07-10 thomas local_refname = NULL;
2171 97a02382 2023-07-10 thomas continue;
2173 97a02382 2023-07-10 thomas err = delete_missing_ref(ref, verbosity, repo);
2174 97a02382 2023-07-10 thomas if (err)
2176 97a02382 2023-07-10 thomas unlock_err = got_ref_unlock(ref);
2177 97a02382 2023-07-10 thomas got_ref_close(ref);
2178 97a02382 2023-07-10 thomas if (unlock_err && err == NULL) {
2179 97a02382 2023-07-10 thomas err = unlock_err;
2183 97a02382 2023-07-10 thomas free(local_refname);
2184 97a02382 2023-07-10 thomas local_refname = NULL;
2188 97a02382 2023-07-10 thomas got_ref_list_free(&my_refs);
2189 97a02382 2023-07-10 thomas free(remote_namespace);
2190 97a02382 2023-07-10 thomas free(local_refname);
2191 97a02382 2023-07-10 thomas return err;
2194 97a02382 2023-07-10 thomas static const struct got_error *
2195 97a02382 2023-07-10 thomas update_wanted_ref(const char *refname, struct got_object_id *id,
2196 97a02382 2023-07-10 thomas const char *remote_repo_name, int verbosity, struct got_repository *repo)
2198 97a02382 2023-07-10 thomas const struct got_error *err, *unlock_err;
2199 97a02382 2023-07-10 thomas char *remote_refname;
2200 97a02382 2023-07-10 thomas struct got_reference *ref;
2202 97a02382 2023-07-10 thomas if (strncmp("refs/", refname, 5) == 0)
2203 97a02382 2023-07-10 thomas refname += 5;
2205 97a02382 2023-07-10 thomas if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2206 97a02382 2023-07-10 thomas remote_repo_name, refname) == -1)
2207 97a02382 2023-07-10 thomas return got_error_from_errno("asprintf");
2209 97a02382 2023-07-10 thomas err = got_ref_open(&ref, repo, remote_refname, 1);
2210 97a02382 2023-07-10 thomas if (err) {
2211 97a02382 2023-07-10 thomas if (err->code != GOT_ERR_NOT_REF)
2212 97a02382 2023-07-10 thomas goto done;
2213 97a02382 2023-07-10 thomas err = create_ref(remote_refname, id, verbosity, repo);
2214 97a02382 2023-07-10 thomas } else {
2215 97a02382 2023-07-10 thomas err = update_ref(ref, id, 0, verbosity, repo);
2216 97a02382 2023-07-10 thomas unlock_err = got_ref_unlock(ref);
2217 97a02382 2023-07-10 thomas if (unlock_err && err == NULL)
2218 97a02382 2023-07-10 thomas err = unlock_err;
2219 97a02382 2023-07-10 thomas got_ref_close(ref);
2222 97a02382 2023-07-10 thomas free(remote_refname);
2223 97a02382 2023-07-10 thomas return err;
2226 97a02382 2023-07-10 thomas static const struct got_error *
2227 97a02382 2023-07-10 thomas delete_ref(struct got_repository *repo, struct got_reference *ref)
2229 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
2230 97a02382 2023-07-10 thomas struct got_object_id *id = NULL;
2231 97a02382 2023-07-10 thomas char *id_str = NULL;
2232 97a02382 2023-07-10 thomas const char *target;
2234 97a02382 2023-07-10 thomas if (got_ref_is_symbolic(ref)) {
2235 97a02382 2023-07-10 thomas target = got_ref_get_symref_target(ref);
2236 97a02382 2023-07-10 thomas } else {
2237 97a02382 2023-07-10 thomas err = got_ref_resolve(&id, repo, ref);
2238 97a02382 2023-07-10 thomas if (err)
2239 97a02382 2023-07-10 thomas goto done;
2240 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, id);
2241 97a02382 2023-07-10 thomas if (err)
2242 97a02382 2023-07-10 thomas goto done;
2243 97a02382 2023-07-10 thomas target = id_str;
2246 97a02382 2023-07-10 thomas err = got_ref_delete(ref, repo);
2247 97a02382 2023-07-10 thomas if (err)
2248 97a02382 2023-07-10 thomas goto done;
2250 97a02382 2023-07-10 thomas printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2252 97a02382 2023-07-10 thomas free(id);
2253 97a02382 2023-07-10 thomas free(id_str);
2254 97a02382 2023-07-10 thomas return err;
2257 97a02382 2023-07-10 thomas static const struct got_error *
2258 97a02382 2023-07-10 thomas delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2260 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
2261 97a02382 2023-07-10 thomas struct got_reflist_head refs;
2262 97a02382 2023-07-10 thomas struct got_reflist_entry *re;
2263 97a02382 2023-07-10 thomas char *prefix;
2265 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
2267 97a02382 2023-07-10 thomas if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2268 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
2269 97a02382 2023-07-10 thomas goto done;
2271 97a02382 2023-07-10 thomas err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2272 97a02382 2023-07-10 thomas if (err)
2273 97a02382 2023-07-10 thomas goto done;
2275 97a02382 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry)
2276 97a02382 2023-07-10 thomas delete_ref(repo, re->ref);
2278 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
2279 97a02382 2023-07-10 thomas return err;
2282 97a02382 2023-07-10 thomas static const struct got_error *
2283 97a02382 2023-07-10 thomas cmd_fetch(int argc, char *argv[])
2285 97a02382 2023-07-10 thomas const struct got_error *error = NULL, *unlock_err;
2286 97a02382 2023-07-10 thomas char *cwd = NULL, *repo_path = NULL;
2287 97a02382 2023-07-10 thomas const char *remote_name;
2288 97a02382 2023-07-10 thomas char *proto = NULL, *host = NULL, *port = NULL;
2289 97a02382 2023-07-10 thomas char *repo_name = NULL, *server_path = NULL;
2290 97a02382 2023-07-10 thomas const struct got_remote_repo *remotes, *remote = NULL;
2291 97a02382 2023-07-10 thomas int nremotes;
2292 97a02382 2023-07-10 thomas char *id_str = NULL;
2293 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
2294 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
2295 97a02382 2023-07-10 thomas const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2296 97a02382 2023-07-10 thomas struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2297 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
2298 97a02382 2023-07-10 thomas struct got_reflist_head remote_refs;
2299 97a02382 2023-07-10 thomas struct got_reflist_entry *re;
2300 97a02382 2023-07-10 thomas struct got_object_id *pack_hash = NULL;
2301 97a02382 2023-07-10 thomas int i, ch, fetchfd = -1, fetchstatus;
2302 97a02382 2023-07-10 thomas pid_t fetchpid = -1;
2303 97a02382 2023-07-10 thomas struct got_fetch_progress_arg fpa;
2304 97a02382 2023-07-10 thomas int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2305 97a02382 2023-07-10 thomas int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2306 97a02382 2023-07-10 thomas int *pack_fds = NULL, have_bflag = 0;
2307 97a02382 2023-07-10 thomas const char *remote_head = NULL, *worktree_branch = NULL;
2309 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
2310 97a02382 2023-07-10 thomas TAILQ_INIT(&symrefs);
2311 97a02382 2023-07-10 thomas TAILQ_INIT(&remote_refs);
2312 97a02382 2023-07-10 thomas TAILQ_INIT(&wanted_branches);
2313 97a02382 2023-07-10 thomas TAILQ_INIT(&wanted_refs);
2315 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "ab:dlqR:r:tvX")) != -1) {
2316 97a02382 2023-07-10 thomas switch (ch) {
2317 97a02382 2023-07-10 thomas case 'a':
2318 97a02382 2023-07-10 thomas fetch_all_branches = 1;
2320 97a02382 2023-07-10 thomas case 'b':
2321 97a02382 2023-07-10 thomas error = got_pathlist_append(&wanted_branches,
2322 97a02382 2023-07-10 thomas optarg, NULL);
2323 97a02382 2023-07-10 thomas if (error)
2324 97a02382 2023-07-10 thomas return error;
2325 97a02382 2023-07-10 thomas have_bflag = 1;
2327 97a02382 2023-07-10 thomas case 'd':
2328 97a02382 2023-07-10 thomas delete_refs = 1;
2330 97a02382 2023-07-10 thomas case 'l':
2331 97a02382 2023-07-10 thomas list_refs_only = 1;
2333 97a02382 2023-07-10 thomas case 'q':
2334 97a02382 2023-07-10 thomas verbosity = -1;
2336 97a02382 2023-07-10 thomas case 'R':
2337 97a02382 2023-07-10 thomas error = got_pathlist_append(&wanted_refs,
2338 97a02382 2023-07-10 thomas optarg, NULL);
2339 97a02382 2023-07-10 thomas if (error)
2340 97a02382 2023-07-10 thomas return error;
2342 97a02382 2023-07-10 thomas case 'r':
2343 97a02382 2023-07-10 thomas repo_path = realpath(optarg, NULL);
2344 97a02382 2023-07-10 thomas if (repo_path == NULL)
2345 97a02382 2023-07-10 thomas return got_error_from_errno2("realpath",
2346 97a02382 2023-07-10 thomas optarg);
2347 97a02382 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
2349 97a02382 2023-07-10 thomas case 't':
2350 97a02382 2023-07-10 thomas replace_tags = 1;
2352 97a02382 2023-07-10 thomas case 'v':
2353 97a02382 2023-07-10 thomas if (verbosity < 0)
2354 97a02382 2023-07-10 thomas verbosity = 0;
2355 97a02382 2023-07-10 thomas else if (verbosity < 3)
2356 97a02382 2023-07-10 thomas verbosity++;
2358 97a02382 2023-07-10 thomas case 'X':
2359 97a02382 2023-07-10 thomas delete_remote = 1;
2361 97a02382 2023-07-10 thomas default:
2362 97a02382 2023-07-10 thomas usage_fetch();
2366 97a02382 2023-07-10 thomas argc -= optind;
2367 97a02382 2023-07-10 thomas argv += optind;
2369 97a02382 2023-07-10 thomas if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2370 97a02382 2023-07-10 thomas option_conflict('a', 'b');
2371 97a02382 2023-07-10 thomas if (list_refs_only) {
2372 97a02382 2023-07-10 thomas if (!TAILQ_EMPTY(&wanted_branches))
2373 97a02382 2023-07-10 thomas option_conflict('l', 'b');
2374 97a02382 2023-07-10 thomas if (fetch_all_branches)
2375 97a02382 2023-07-10 thomas option_conflict('l', 'a');
2376 97a02382 2023-07-10 thomas if (delete_refs)
2377 97a02382 2023-07-10 thomas option_conflict('l', 'd');
2378 97a02382 2023-07-10 thomas if (delete_remote)
2379 97a02382 2023-07-10 thomas option_conflict('l', 'X');
2381 97a02382 2023-07-10 thomas if (delete_remote) {
2382 97a02382 2023-07-10 thomas if (fetch_all_branches)
2383 97a02382 2023-07-10 thomas option_conflict('X', 'a');
2384 97a02382 2023-07-10 thomas if (!TAILQ_EMPTY(&wanted_branches))
2385 97a02382 2023-07-10 thomas option_conflict('X', 'b');
2386 97a02382 2023-07-10 thomas if (delete_refs)
2387 97a02382 2023-07-10 thomas option_conflict('X', 'd');
2388 97a02382 2023-07-10 thomas if (replace_tags)
2389 97a02382 2023-07-10 thomas option_conflict('X', 't');
2390 97a02382 2023-07-10 thomas if (!TAILQ_EMPTY(&wanted_refs))
2391 97a02382 2023-07-10 thomas option_conflict('X', 'R');
2394 97a02382 2023-07-10 thomas if (argc == 0) {
2395 97a02382 2023-07-10 thomas if (delete_remote)
2396 97a02382 2023-07-10 thomas errx(1, "-X option requires a remote name");
2397 97a02382 2023-07-10 thomas remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2398 97a02382 2023-07-10 thomas } else if (argc == 1)
2399 97a02382 2023-07-10 thomas remote_name = argv[0];
2401 97a02382 2023-07-10 thomas usage_fetch();
2403 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
2404 97a02382 2023-07-10 thomas if (cwd == NULL) {
2405 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
2406 97a02382 2023-07-10 thomas goto done;
2409 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
2410 97a02382 2023-07-10 thomas if (error != NULL)
2411 97a02382 2023-07-10 thomas goto done;
2413 97a02382 2023-07-10 thomas if (repo_path == NULL) {
2414 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
2415 97a02382 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
2416 97a02382 2023-07-10 thomas goto done;
2418 97a02382 2023-07-10 thomas error = NULL;
2419 97a02382 2023-07-10 thomas if (worktree) {
2420 97a02382 2023-07-10 thomas repo_path =
2421 97a02382 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree));
2422 97a02382 2023-07-10 thomas if (repo_path == NULL)
2423 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
2424 97a02382 2023-07-10 thomas if (error)
2425 97a02382 2023-07-10 thomas goto done;
2426 97a02382 2023-07-10 thomas } else {
2427 97a02382 2023-07-10 thomas repo_path = strdup(cwd);
2428 97a02382 2023-07-10 thomas if (repo_path == NULL) {
2429 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
2430 97a02382 2023-07-10 thomas goto done;
2435 97a02382 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2436 97a02382 2023-07-10 thomas if (error)
2437 97a02382 2023-07-10 thomas goto done;
2439 97a02382 2023-07-10 thomas if (delete_remote) {
2440 97a02382 2023-07-10 thomas error = delete_refs_for_remote(repo, remote_name);
2441 97a02382 2023-07-10 thomas goto done; /* nothing else to do */
2444 97a02382 2023-07-10 thomas if (worktree) {
2445 97a02382 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
2446 97a02382 2023-07-10 thomas if (worktree_conf) {
2447 97a02382 2023-07-10 thomas got_gotconfig_get_remotes(&nremotes, &remotes,
2448 97a02382 2023-07-10 thomas worktree_conf);
2449 97a02382 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
2450 97a02382 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
2451 97a02382 2023-07-10 thomas remote = &remotes[i];
2457 97a02382 2023-07-10 thomas if (remote == NULL) {
2458 97a02382 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
2459 97a02382 2023-07-10 thomas if (repo_conf) {
2460 97a02382 2023-07-10 thomas got_gotconfig_get_remotes(&nremotes, &remotes,
2461 97a02382 2023-07-10 thomas repo_conf);
2462 97a02382 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
2463 97a02382 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
2464 97a02382 2023-07-10 thomas remote = &remotes[i];
2470 97a02382 2023-07-10 thomas if (remote == NULL) {
2471 97a02382 2023-07-10 thomas got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2472 97a02382 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
2473 97a02382 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
2474 97a02382 2023-07-10 thomas remote = &remotes[i];
2479 97a02382 2023-07-10 thomas if (remote == NULL) {
2480 97a02382 2023-07-10 thomas error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2481 97a02382 2023-07-10 thomas goto done;
2484 97a02382 2023-07-10 thomas if (TAILQ_EMPTY(&wanted_branches)) {
2485 97a02382 2023-07-10 thomas if (!fetch_all_branches)
2486 97a02382 2023-07-10 thomas fetch_all_branches = remote->fetch_all_branches;
2487 97a02382 2023-07-10 thomas for (i = 0; i < remote->nfetch_branches; i++) {
2488 97a02382 2023-07-10 thomas error = got_pathlist_append(&wanted_branches,
2489 97a02382 2023-07-10 thomas remote->fetch_branches[i], NULL);
2490 97a02382 2023-07-10 thomas if (error)
2491 97a02382 2023-07-10 thomas goto done;
2494 97a02382 2023-07-10 thomas if (TAILQ_EMPTY(&wanted_refs)) {
2495 97a02382 2023-07-10 thomas for (i = 0; i < remote->nfetch_refs; i++) {
2496 97a02382 2023-07-10 thomas error = got_pathlist_append(&wanted_refs,
2497 97a02382 2023-07-10 thomas remote->fetch_refs[i], NULL);
2498 97a02382 2023-07-10 thomas if (error)
2499 97a02382 2023-07-10 thomas goto done;
2503 97a02382 2023-07-10 thomas error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2504 97a02382 2023-07-10 thomas &repo_name, remote->fetch_url);
2505 97a02382 2023-07-10 thomas if (error)
2506 97a02382 2023-07-10 thomas goto done;
2508 97a02382 2023-07-10 thomas if (strcmp(proto, "git") == 0) {
2509 97a02382 2023-07-10 thomas #ifndef PROFILE
2510 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2511 97a02382 2023-07-10 thomas "sendfd dns inet unveil", NULL) == -1)
2512 97a02382 2023-07-10 thomas err(1, "pledge");
2514 97a02382 2023-07-10 thomas } else if (strcmp(proto, "git+ssh") == 0 ||
2515 97a02382 2023-07-10 thomas strcmp(proto, "ssh") == 0) {
2516 97a02382 2023-07-10 thomas #ifndef PROFILE
2517 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2518 97a02382 2023-07-10 thomas "sendfd unveil", NULL) == -1)
2519 97a02382 2023-07-10 thomas err(1, "pledge");
2521 97a02382 2023-07-10 thomas } else if (strcmp(proto, "http") == 0 ||
2522 97a02382 2023-07-10 thomas strcmp(proto, "git+http") == 0) {
2523 97a02382 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2524 97a02382 2023-07-10 thomas goto done;
2525 97a02382 2023-07-10 thomas } else {
2526 97a02382 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2527 97a02382 2023-07-10 thomas goto done;
2530 97a02382 2023-07-10 thomas error = got_dial_apply_unveil(proto);
2531 97a02382 2023-07-10 thomas if (error)
2532 97a02382 2023-07-10 thomas goto done;
2534 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2535 97a02382 2023-07-10 thomas if (error)
2536 97a02382 2023-07-10 thomas goto done;
2538 97a02382 2023-07-10 thomas if (verbosity >= 0) {
2539 97a02382 2023-07-10 thomas printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2540 97a02382 2023-07-10 thomas remote->name, proto, host,
2541 97a02382 2023-07-10 thomas port ? ":" : "", port ? port : "",
2542 97a02382 2023-07-10 thomas *server_path == '/' ? "" : "/", server_path);
2545 97a02382 2023-07-10 thomas error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2546 97a02382 2023-07-10 thomas server_path, verbosity);
2547 97a02382 2023-07-10 thomas if (error)
2548 97a02382 2023-07-10 thomas goto done;
2550 97a02382 2023-07-10 thomas if (!have_bflag) {
2552 97a02382 2023-07-10 thomas * If set, get this remote's HEAD ref target so
2553 97a02382 2023-07-10 thomas * if it has changed on the server we can fetch it.
2555 97a02382 2023-07-10 thomas error = got_ref_list(&remote_refs, repo, "refs/remotes",
2556 97a02382 2023-07-10 thomas got_ref_cmp_by_name, repo);
2557 97a02382 2023-07-10 thomas if (error)
2558 97a02382 2023-07-10 thomas goto done;
2560 97a02382 2023-07-10 thomas TAILQ_FOREACH(re, &remote_refs, entry) {
2561 97a02382 2023-07-10 thomas const char *remote_refname, *remote_target;
2562 97a02382 2023-07-10 thomas size_t remote_name_len;
2564 97a02382 2023-07-10 thomas if (!got_ref_is_symbolic(re->ref))
2565 97a02382 2023-07-10 thomas continue;
2567 97a02382 2023-07-10 thomas remote_name_len = strlen(remote->name);
2568 97a02382 2023-07-10 thomas remote_refname = got_ref_get_name(re->ref);
2570 97a02382 2023-07-10 thomas /* we only want refs/remotes/$remote->name/HEAD */
2571 97a02382 2023-07-10 thomas if (strncmp(remote_refname + 13, remote->name,
2572 97a02382 2023-07-10 thomas remote_name_len) != 0)
2573 97a02382 2023-07-10 thomas continue;
2575 97a02382 2023-07-10 thomas if (strcmp(remote_refname + remote_name_len + 14,
2576 97a02382 2023-07-10 thomas GOT_REF_HEAD) != 0)
2577 97a02382 2023-07-10 thomas continue;
2580 97a02382 2023-07-10 thomas * Take the name itself because we already
2581 97a02382 2023-07-10 thomas * only match with refs/heads/ in fetch_pack().
2583 97a02382 2023-07-10 thomas remote_target = got_ref_get_symref_target(re->ref);
2584 97a02382 2023-07-10 thomas remote_head = remote_target + remote_name_len + 14;
2588 97a02382 2023-07-10 thomas if (worktree) {
2589 97a02382 2023-07-10 thomas const char *refname;
2591 97a02382 2023-07-10 thomas refname = got_worktree_get_head_ref_name(worktree);
2592 97a02382 2023-07-10 thomas if (strncmp(refname, "refs/heads/", 11) == 0)
2593 97a02382 2023-07-10 thomas worktree_branch = refname;
2597 97a02382 2023-07-10 thomas fpa.last_scaled_size[0] = '\0';
2598 97a02382 2023-07-10 thomas fpa.last_p_indexed = -1;
2599 97a02382 2023-07-10 thomas fpa.last_p_resolved = -1;
2600 97a02382 2023-07-10 thomas fpa.verbosity = verbosity;
2601 97a02382 2023-07-10 thomas fpa.repo = repo;
2602 97a02382 2023-07-10 thomas fpa.create_configs = 0;
2603 97a02382 2023-07-10 thomas fpa.configs_created = 0;
2604 97a02382 2023-07-10 thomas memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2606 97a02382 2023-07-10 thomas error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2607 97a02382 2023-07-10 thomas remote->mirror_references, fetch_all_branches, &wanted_branches,
2608 97a02382 2023-07-10 thomas &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2609 97a02382 2023-07-10 thomas worktree_branch, remote_head, have_bflag, fetch_progress, &fpa);
2610 97a02382 2023-07-10 thomas if (error)
2611 97a02382 2023-07-10 thomas goto done;
2613 97a02382 2023-07-10 thomas if (list_refs_only) {
2614 97a02382 2023-07-10 thomas error = list_remote_refs(&symrefs, &refs);
2615 97a02382 2023-07-10 thomas goto done;
2618 97a02382 2023-07-10 thomas if (pack_hash == NULL) {
2619 97a02382 2023-07-10 thomas if (verbosity >= 0)
2620 97a02382 2023-07-10 thomas printf("Already up-to-date\n");
2621 97a02382 2023-07-10 thomas } else if (verbosity >= 0) {
2622 97a02382 2023-07-10 thomas error = got_object_id_str(&id_str, pack_hash);
2623 97a02382 2023-07-10 thomas if (error)
2624 97a02382 2023-07-10 thomas goto done;
2625 97a02382 2023-07-10 thomas printf("\nFetched %s.pack\n", id_str);
2626 97a02382 2023-07-10 thomas free(id_str);
2627 97a02382 2023-07-10 thomas id_str = NULL;
2630 97a02382 2023-07-10 thomas /* Update references provided with the pack file. */
2631 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, &refs, entry) {
2632 97a02382 2023-07-10 thomas const char *refname = pe->path;
2633 97a02382 2023-07-10 thomas struct got_object_id *id = pe->data;
2634 97a02382 2023-07-10 thomas struct got_reference *ref;
2635 97a02382 2023-07-10 thomas char *remote_refname;
2637 97a02382 2023-07-10 thomas if (is_wanted_ref(&wanted_refs, refname) &&
2638 97a02382 2023-07-10 thomas !remote->mirror_references) {
2639 97a02382 2023-07-10 thomas error = update_wanted_ref(refname, id,
2640 97a02382 2023-07-10 thomas remote->name, verbosity, repo);
2641 97a02382 2023-07-10 thomas if (error)
2642 97a02382 2023-07-10 thomas goto done;
2643 97a02382 2023-07-10 thomas continue;
2646 97a02382 2023-07-10 thomas if (remote->mirror_references ||
2647 97a02382 2023-07-10 thomas strncmp("refs/tags/", refname, 10) == 0) {
2648 97a02382 2023-07-10 thomas error = got_ref_open(&ref, repo, refname, 1);
2649 97a02382 2023-07-10 thomas if (error) {
2650 97a02382 2023-07-10 thomas if (error->code != GOT_ERR_NOT_REF)
2651 97a02382 2023-07-10 thomas goto done;
2652 97a02382 2023-07-10 thomas error = create_ref(refname, id, verbosity,
2654 97a02382 2023-07-10 thomas if (error)
2655 97a02382 2023-07-10 thomas goto done;
2656 97a02382 2023-07-10 thomas } else {
2657 97a02382 2023-07-10 thomas error = update_ref(ref, id, replace_tags,
2658 97a02382 2023-07-10 thomas verbosity, repo);
2659 97a02382 2023-07-10 thomas unlock_err = got_ref_unlock(ref);
2660 97a02382 2023-07-10 thomas if (unlock_err && error == NULL)
2661 97a02382 2023-07-10 thomas error = unlock_err;
2662 97a02382 2023-07-10 thomas got_ref_close(ref);
2663 97a02382 2023-07-10 thomas if (error)
2664 97a02382 2023-07-10 thomas goto done;
2666 97a02382 2023-07-10 thomas } else if (strncmp("refs/heads/", refname, 11) == 0) {
2667 97a02382 2023-07-10 thomas if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2668 97a02382 2023-07-10 thomas remote_name, refname + 11) == -1) {
2669 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
2670 97a02382 2023-07-10 thomas goto done;
2673 97a02382 2023-07-10 thomas error = got_ref_open(&ref, repo, remote_refname, 1);
2674 97a02382 2023-07-10 thomas if (error) {
2675 97a02382 2023-07-10 thomas if (error->code != GOT_ERR_NOT_REF)
2676 97a02382 2023-07-10 thomas goto done;
2677 97a02382 2023-07-10 thomas error = create_ref(remote_refname, id,
2678 97a02382 2023-07-10 thomas verbosity, repo);
2679 97a02382 2023-07-10 thomas if (error)
2680 97a02382 2023-07-10 thomas goto done;
2681 97a02382 2023-07-10 thomas } else {
2682 97a02382 2023-07-10 thomas error = update_ref(ref, id, replace_tags,
2683 97a02382 2023-07-10 thomas verbosity, repo);
2684 97a02382 2023-07-10 thomas unlock_err = got_ref_unlock(ref);
2685 97a02382 2023-07-10 thomas if (unlock_err && error == NULL)
2686 97a02382 2023-07-10 thomas error = unlock_err;
2687 97a02382 2023-07-10 thomas got_ref_close(ref);
2688 97a02382 2023-07-10 thomas if (error)
2689 97a02382 2023-07-10 thomas goto done;
2692 97a02382 2023-07-10 thomas /* Also create a local branch if none exists yet. */
2693 97a02382 2023-07-10 thomas error = got_ref_open(&ref, repo, refname, 1);
2694 97a02382 2023-07-10 thomas if (error) {
2695 97a02382 2023-07-10 thomas if (error->code != GOT_ERR_NOT_REF)
2696 97a02382 2023-07-10 thomas goto done;
2697 97a02382 2023-07-10 thomas error = create_ref(refname, id, verbosity,
2699 97a02382 2023-07-10 thomas if (error)
2700 97a02382 2023-07-10 thomas goto done;
2701 97a02382 2023-07-10 thomas } else {
2702 97a02382 2023-07-10 thomas unlock_err = got_ref_unlock(ref);
2703 97a02382 2023-07-10 thomas if (unlock_err && error == NULL)
2704 97a02382 2023-07-10 thomas error = unlock_err;
2705 97a02382 2023-07-10 thomas got_ref_close(ref);
2709 97a02382 2023-07-10 thomas if (delete_refs) {
2710 97a02382 2023-07-10 thomas error = delete_missing_refs(&refs, &symrefs, remote,
2711 97a02382 2023-07-10 thomas verbosity, repo);
2712 97a02382 2023-07-10 thomas if (error)
2713 97a02382 2023-07-10 thomas goto done;
2716 97a02382 2023-07-10 thomas if (!remote->mirror_references) {
2717 97a02382 2023-07-10 thomas /* Update remote HEAD reference if the server provided one. */
2718 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, &symrefs, entry) {
2719 97a02382 2023-07-10 thomas struct got_reference *target_ref;
2720 97a02382 2023-07-10 thomas const char *refname = pe->path;
2721 97a02382 2023-07-10 thomas const char *target = pe->data;
2722 97a02382 2023-07-10 thomas char *remote_refname = NULL, *remote_target = NULL;
2724 97a02382 2023-07-10 thomas if (strcmp(refname, GOT_REF_HEAD) != 0)
2725 97a02382 2023-07-10 thomas continue;
2727 97a02382 2023-07-10 thomas if (strncmp("refs/heads/", target, 11) != 0)
2728 97a02382 2023-07-10 thomas continue;
2730 97a02382 2023-07-10 thomas if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2731 97a02382 2023-07-10 thomas remote->name, refname) == -1) {
2732 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
2733 97a02382 2023-07-10 thomas goto done;
2735 97a02382 2023-07-10 thomas if (asprintf(&remote_target, "refs/remotes/%s/%s",
2736 97a02382 2023-07-10 thomas remote->name, target + 11) == -1) {
2737 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
2738 97a02382 2023-07-10 thomas free(remote_refname);
2739 97a02382 2023-07-10 thomas goto done;
2742 97a02382 2023-07-10 thomas error = got_ref_open(&target_ref, repo, remote_target,
2744 97a02382 2023-07-10 thomas if (error) {
2745 97a02382 2023-07-10 thomas free(remote_refname);
2746 97a02382 2023-07-10 thomas free(remote_target);
2747 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_NOT_REF) {
2748 97a02382 2023-07-10 thomas error = NULL;
2749 97a02382 2023-07-10 thomas continue;
2751 97a02382 2023-07-10 thomas goto done;
2753 97a02382 2023-07-10 thomas error = update_symref(remote_refname, target_ref,
2754 97a02382 2023-07-10 thomas verbosity, repo);
2755 97a02382 2023-07-10 thomas free(remote_refname);
2756 97a02382 2023-07-10 thomas free(remote_target);
2757 97a02382 2023-07-10 thomas got_ref_close(target_ref);
2758 97a02382 2023-07-10 thomas if (error)
2759 97a02382 2023-07-10 thomas goto done;
2763 97a02382 2023-07-10 thomas if (fetchpid > 0) {
2764 97a02382 2023-07-10 thomas if (kill(fetchpid, SIGTERM) == -1)
2765 97a02382 2023-07-10 thomas error = got_error_from_errno("kill");
2766 97a02382 2023-07-10 thomas if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2767 97a02382 2023-07-10 thomas error = got_error_from_errno("waitpid");
2769 97a02382 2023-07-10 thomas if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2770 97a02382 2023-07-10 thomas error = got_error_from_errno("close");
2771 97a02382 2023-07-10 thomas if (repo) {
2772 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
2773 97a02382 2023-07-10 thomas if (error == NULL)
2774 97a02382 2023-07-10 thomas error = close_err;
2776 97a02382 2023-07-10 thomas if (worktree)
2777 97a02382 2023-07-10 thomas got_worktree_close(worktree);
2778 97a02382 2023-07-10 thomas if (pack_fds) {
2779 97a02382 2023-07-10 thomas const struct got_error *pack_err =
2780 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
2781 97a02382 2023-07-10 thomas if (error == NULL)
2782 97a02382 2023-07-10 thomas error = pack_err;
2784 97a02382 2023-07-10 thomas got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
2785 97a02382 2023-07-10 thomas got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
2786 97a02382 2023-07-10 thomas got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2787 97a02382 2023-07-10 thomas got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2788 97a02382 2023-07-10 thomas got_ref_list_free(&remote_refs);
2789 97a02382 2023-07-10 thomas free(id_str);
2790 97a02382 2023-07-10 thomas free(cwd);
2791 97a02382 2023-07-10 thomas free(repo_path);
2792 97a02382 2023-07-10 thomas free(pack_hash);
2793 97a02382 2023-07-10 thomas free(proto);
2794 97a02382 2023-07-10 thomas free(host);
2795 97a02382 2023-07-10 thomas free(port);
2796 97a02382 2023-07-10 thomas free(server_path);
2797 97a02382 2023-07-10 thomas free(repo_name);
2798 97a02382 2023-07-10 thomas return error;
2802 97a02382 2023-07-10 thomas __dead static void
2803 97a02382 2023-07-10 thomas usage_checkout(void)
2805 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2806 97a02382 2023-07-10 thomas "[-p path-prefix] repository-path [work-tree-path]\n",
2807 97a02382 2023-07-10 thomas getprogname());
2808 97a02382 2023-07-10 thomas exit(1);
2811 97a02382 2023-07-10 thomas static void
2812 97a02382 2023-07-10 thomas show_worktree_base_ref_warning(void)
2814 97a02382 2023-07-10 thomas fprintf(stderr, "%s: warning: could not create a reference "
2815 97a02382 2023-07-10 thomas "to the work tree's base commit; the commit could be "
2816 97a02382 2023-07-10 thomas "garbage-collected by Git or 'gotadmin cleanup'; making the "
2817 97a02382 2023-07-10 thomas "repository writable and running 'got update' will prevent this\n",
2818 97a02382 2023-07-10 thomas getprogname());
2821 97a02382 2023-07-10 thomas struct got_checkout_progress_arg {
2822 97a02382 2023-07-10 thomas const char *worktree_path;
2823 97a02382 2023-07-10 thomas int had_base_commit_ref_error;
2824 97a02382 2023-07-10 thomas int verbosity;
2827 97a02382 2023-07-10 thomas static const struct got_error *
2828 97a02382 2023-07-10 thomas checkout_progress(void *arg, unsigned char status, const char *path)
2830 97a02382 2023-07-10 thomas struct got_checkout_progress_arg *a = arg;
2832 97a02382 2023-07-10 thomas /* Base commit bump happens silently. */
2833 97a02382 2023-07-10 thomas if (status == GOT_STATUS_BUMP_BASE)
2834 97a02382 2023-07-10 thomas return NULL;
2836 97a02382 2023-07-10 thomas if (status == GOT_STATUS_BASE_REF_ERR) {
2837 97a02382 2023-07-10 thomas a->had_base_commit_ref_error = 1;
2838 97a02382 2023-07-10 thomas return NULL;
2841 97a02382 2023-07-10 thomas while (path[0] == '/')
2844 97a02382 2023-07-10 thomas if (a->verbosity >= 0)
2845 97a02382 2023-07-10 thomas printf("%c %s/%s\n", status, a->worktree_path, path);
2847 97a02382 2023-07-10 thomas return NULL;
2850 97a02382 2023-07-10 thomas static const struct got_error *
2851 97a02382 2023-07-10 thomas check_cancelled(void *arg)
2853 97a02382 2023-07-10 thomas if (sigint_received || sigpipe_received)
2854 97a02382 2023-07-10 thomas return got_error(GOT_ERR_CANCELLED);
2855 97a02382 2023-07-10 thomas return NULL;
2858 97a02382 2023-07-10 thomas static const struct got_error *
2859 97a02382 2023-07-10 thomas check_linear_ancestry(struct got_object_id *commit_id,
2860 97a02382 2023-07-10 thomas struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2861 97a02382 2023-07-10 thomas struct got_repository *repo)
2863 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
2864 97a02382 2023-07-10 thomas struct got_object_id *yca_id;
2866 97a02382 2023-07-10 thomas err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2867 97a02382 2023-07-10 thomas commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2868 97a02382 2023-07-10 thomas if (err)
2869 97a02382 2023-07-10 thomas return err;
2871 97a02382 2023-07-10 thomas if (yca_id == NULL)
2872 97a02382 2023-07-10 thomas return got_error(GOT_ERR_ANCESTRY);
2875 97a02382 2023-07-10 thomas * Require a straight line of history between the target commit
2876 97a02382 2023-07-10 thomas * and the work tree's base commit.
2878 97a02382 2023-07-10 thomas * Non-linear situations such as this require a rebase:
2880 97a02382 2023-07-10 thomas * (commit) D F (base_commit)
2884 97a02382 2023-07-10 thomas * B (yca)
2888 97a02382 2023-07-10 thomas * 'got update' only handles linear cases:
2889 97a02382 2023-07-10 thomas * Update forwards in time: A (base/yca) - B - C - D (commit)
2890 97a02382 2023-07-10 thomas * Update backwards in time: D (base) - C - B - A (commit/yca)
2892 97a02382 2023-07-10 thomas if (allow_forwards_in_time_only) {
2893 97a02382 2023-07-10 thomas if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2894 97a02382 2023-07-10 thomas return got_error(GOT_ERR_ANCESTRY);
2895 97a02382 2023-07-10 thomas } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2896 97a02382 2023-07-10 thomas got_object_id_cmp(base_commit_id, yca_id) != 0)
2897 97a02382 2023-07-10 thomas return got_error(GOT_ERR_ANCESTRY);
2899 97a02382 2023-07-10 thomas free(yca_id);
2900 97a02382 2023-07-10 thomas return NULL;
2903 97a02382 2023-07-10 thomas static const struct got_error *
2904 97a02382 2023-07-10 thomas check_same_branch(struct got_object_id *commit_id,
2905 97a02382 2023-07-10 thomas struct got_reference *head_ref, struct got_repository *repo)
2907 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
2908 97a02382 2023-07-10 thomas struct got_commit_graph *graph = NULL;
2909 97a02382 2023-07-10 thomas struct got_object_id *head_commit_id = NULL;
2911 97a02382 2023-07-10 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
2912 97a02382 2023-07-10 thomas if (err)
2913 97a02382 2023-07-10 thomas goto done;
2915 97a02382 2023-07-10 thomas if (got_object_id_cmp(head_commit_id, commit_id) == 0)
2916 97a02382 2023-07-10 thomas goto done;
2918 97a02382 2023-07-10 thomas err = got_commit_graph_open(&graph, "/", 1);
2919 97a02382 2023-07-10 thomas if (err)
2920 97a02382 2023-07-10 thomas goto done;
2922 97a02382 2023-07-10 thomas err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2923 97a02382 2023-07-10 thomas check_cancelled, NULL);
2924 97a02382 2023-07-10 thomas if (err)
2925 97a02382 2023-07-10 thomas goto done;
2927 97a02382 2023-07-10 thomas for (;;) {
2928 97a02382 2023-07-10 thomas struct got_object_id id;
2930 97a02382 2023-07-10 thomas err = got_commit_graph_iter_next(&id, graph, repo,
2931 97a02382 2023-07-10 thomas check_cancelled, NULL);
2932 97a02382 2023-07-10 thomas if (err) {
2933 97a02382 2023-07-10 thomas if (err->code == GOT_ERR_ITER_COMPLETED)
2934 97a02382 2023-07-10 thomas err = got_error(GOT_ERR_ANCESTRY);
2938 97a02382 2023-07-10 thomas if (got_object_id_cmp(&id, commit_id) == 0)
2942 97a02382 2023-07-10 thomas if (graph)
2943 97a02382 2023-07-10 thomas got_commit_graph_close(graph);
2944 97a02382 2023-07-10 thomas free(head_commit_id);
2945 97a02382 2023-07-10 thomas return err;
2948 97a02382 2023-07-10 thomas static const struct got_error *
2949 97a02382 2023-07-10 thomas checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2951 97a02382 2023-07-10 thomas static char msg[512];
2952 97a02382 2023-07-10 thomas const char *branch_name;
2954 97a02382 2023-07-10 thomas if (got_ref_is_symbolic(ref))
2955 97a02382 2023-07-10 thomas branch_name = got_ref_get_symref_target(ref);
2957 97a02382 2023-07-10 thomas branch_name = got_ref_get_name(ref);
2959 97a02382 2023-07-10 thomas if (strncmp("refs/heads/", branch_name, 11) == 0)
2960 97a02382 2023-07-10 thomas branch_name += 11;
2962 97a02382 2023-07-10 thomas snprintf(msg, sizeof(msg),
2963 97a02382 2023-07-10 thomas "target commit is not contained in branch '%s'; "
2964 97a02382 2023-07-10 thomas "the branch to use must be specified with -b; "
2965 97a02382 2023-07-10 thomas "if necessary a new branch can be created for "
2966 97a02382 2023-07-10 thomas "this commit with 'got branch -c %s BRANCH_NAME'",
2967 97a02382 2023-07-10 thomas branch_name, commit_id_str);
2969 97a02382 2023-07-10 thomas return got_error_msg(GOT_ERR_ANCESTRY, msg);
2972 97a02382 2023-07-10 thomas static const struct got_error *
2973 97a02382 2023-07-10 thomas cmd_checkout(int argc, char *argv[])
2975 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
2976 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
2977 97a02382 2023-07-10 thomas struct got_reference *head_ref = NULL, *ref = NULL;
2978 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
2979 97a02382 2023-07-10 thomas char *repo_path = NULL;
2980 97a02382 2023-07-10 thomas char *worktree_path = NULL;
2981 97a02382 2023-07-10 thomas const char *path_prefix = "";
2982 97a02382 2023-07-10 thomas const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2983 97a02382 2023-07-10 thomas char *commit_id_str = NULL;
2984 97a02382 2023-07-10 thomas struct got_object_id *commit_id = NULL;
2985 97a02382 2023-07-10 thomas char *cwd = NULL;
2986 97a02382 2023-07-10 thomas int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2987 97a02382 2023-07-10 thomas struct got_pathlist_head paths;
2988 97a02382 2023-07-10 thomas struct got_checkout_progress_arg cpa;
2989 97a02382 2023-07-10 thomas int *pack_fds = NULL;
2991 97a02382 2023-07-10 thomas TAILQ_INIT(&paths);
2993 97a02382 2023-07-10 thomas #ifndef PROFILE
2994 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2995 97a02382 2023-07-10 thomas "unveil", NULL) == -1)
2996 97a02382 2023-07-10 thomas err(1, "pledge");
2999 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
3000 97a02382 2023-07-10 thomas switch (ch) {
3001 97a02382 2023-07-10 thomas case 'b':
3002 97a02382 2023-07-10 thomas branch_name = optarg;
3004 97a02382 2023-07-10 thomas case 'c':
3005 97a02382 2023-07-10 thomas commit_id_str = strdup(optarg);
3006 97a02382 2023-07-10 thomas if (commit_id_str == NULL)
3007 97a02382 2023-07-10 thomas return got_error_from_errno("strdup");
3009 97a02382 2023-07-10 thomas case 'E':
3010 97a02382 2023-07-10 thomas allow_nonempty = 1;
3012 97a02382 2023-07-10 thomas case 'p':
3013 97a02382 2023-07-10 thomas path_prefix = optarg;
3015 97a02382 2023-07-10 thomas case 'q':
3016 97a02382 2023-07-10 thomas verbosity = -1;
3018 97a02382 2023-07-10 thomas default:
3019 97a02382 2023-07-10 thomas usage_checkout();
3020 97a02382 2023-07-10 thomas /* NOTREACHED */
3024 97a02382 2023-07-10 thomas argc -= optind;
3025 97a02382 2023-07-10 thomas argv += optind;
3027 97a02382 2023-07-10 thomas if (argc == 1) {
3028 97a02382 2023-07-10 thomas char *base, *dotgit;
3029 97a02382 2023-07-10 thomas const char *path;
3030 97a02382 2023-07-10 thomas repo_path = realpath(argv[0], NULL);
3031 97a02382 2023-07-10 thomas if (repo_path == NULL)
3032 97a02382 2023-07-10 thomas return got_error_from_errno2("realpath", argv[0]);
3033 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
3034 97a02382 2023-07-10 thomas if (cwd == NULL) {
3035 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
3036 97a02382 2023-07-10 thomas goto done;
3038 97a02382 2023-07-10 thomas if (path_prefix[0])
3039 97a02382 2023-07-10 thomas path = path_prefix;
3041 97a02382 2023-07-10 thomas path = repo_path;
3042 97a02382 2023-07-10 thomas error = got_path_basename(&base, path);
3043 97a02382 2023-07-10 thomas if (error)
3044 97a02382 2023-07-10 thomas goto done;
3045 97a02382 2023-07-10 thomas dotgit = strstr(base, ".git");
3046 97a02382 2023-07-10 thomas if (dotgit)
3047 97a02382 2023-07-10 thomas *dotgit = '\0';
3048 97a02382 2023-07-10 thomas if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3049 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
3050 97a02382 2023-07-10 thomas free(base);
3051 97a02382 2023-07-10 thomas goto done;
3053 97a02382 2023-07-10 thomas free(base);
3054 97a02382 2023-07-10 thomas } else if (argc == 2) {
3055 97a02382 2023-07-10 thomas repo_path = realpath(argv[0], NULL);
3056 97a02382 2023-07-10 thomas if (repo_path == NULL) {
3057 97a02382 2023-07-10 thomas error = got_error_from_errno2("realpath", argv[0]);
3058 97a02382 2023-07-10 thomas goto done;
3060 97a02382 2023-07-10 thomas worktree_path = realpath(argv[1], NULL);
3061 97a02382 2023-07-10 thomas if (worktree_path == NULL) {
3062 97a02382 2023-07-10 thomas if (errno != ENOENT) {
3063 97a02382 2023-07-10 thomas error = got_error_from_errno2("realpath",
3064 97a02382 2023-07-10 thomas argv[1]);
3065 97a02382 2023-07-10 thomas goto done;
3067 97a02382 2023-07-10 thomas worktree_path = strdup(argv[1]);
3068 97a02382 2023-07-10 thomas if (worktree_path == NULL) {
3069 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
3070 97a02382 2023-07-10 thomas goto done;
3074 97a02382 2023-07-10 thomas usage_checkout();
3076 97a02382 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
3077 97a02382 2023-07-10 thomas got_path_strip_trailing_slashes(worktree_path);
3079 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
3080 97a02382 2023-07-10 thomas if (error != NULL)
3081 97a02382 2023-07-10 thomas goto done;
3083 97a02382 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3084 97a02382 2023-07-10 thomas if (error != NULL)
3085 97a02382 2023-07-10 thomas goto done;
3087 97a02382 2023-07-10 thomas /* Pre-create work tree path for unveil(2) */
3088 97a02382 2023-07-10 thomas error = got_path_mkdir(worktree_path);
3089 97a02382 2023-07-10 thomas if (error) {
3090 97a02382 2023-07-10 thomas if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3091 97a02382 2023-07-10 thomas !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3092 97a02382 2023-07-10 thomas goto done;
3093 97a02382 2023-07-10 thomas if (!allow_nonempty &&
3094 97a02382 2023-07-10 thomas !got_path_dir_is_empty(worktree_path)) {
3095 97a02382 2023-07-10 thomas error = got_error_path(worktree_path,
3096 97a02382 2023-07-10 thomas GOT_ERR_DIR_NOT_EMPTY);
3097 97a02382 2023-07-10 thomas goto done;
3101 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3102 97a02382 2023-07-10 thomas if (error)
3103 97a02382 2023-07-10 thomas goto done;
3105 97a02382 2023-07-10 thomas error = got_ref_open(&head_ref, repo, branch_name, 0);
3106 97a02382 2023-07-10 thomas if (error != NULL)
3107 97a02382 2023-07-10 thomas goto done;
3109 97a02382 2023-07-10 thomas error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3110 97a02382 2023-07-10 thomas if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3111 97a02382 2023-07-10 thomas goto done;
3113 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, worktree_path);
3114 97a02382 2023-07-10 thomas if (error != NULL)
3115 97a02382 2023-07-10 thomas goto done;
3117 97a02382 2023-07-10 thomas error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3118 97a02382 2023-07-10 thomas path_prefix);
3119 97a02382 2023-07-10 thomas if (error != NULL)
3120 97a02382 2023-07-10 thomas goto done;
3121 97a02382 2023-07-10 thomas if (!same_path_prefix) {
3122 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_PATH_PREFIX);
3123 97a02382 2023-07-10 thomas goto done;
3126 97a02382 2023-07-10 thomas if (commit_id_str) {
3127 97a02382 2023-07-10 thomas struct got_reflist_head refs;
3128 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
3129 97a02382 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3131 97a02382 2023-07-10 thomas if (error)
3132 97a02382 2023-07-10 thomas goto done;
3133 97a02382 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
3134 97a02382 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3135 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
3136 97a02382 2023-07-10 thomas if (error)
3137 97a02382 2023-07-10 thomas goto done;
3138 97a02382 2023-07-10 thomas error = check_linear_ancestry(commit_id,
3139 97a02382 2023-07-10 thomas got_worktree_get_base_commit_id(worktree), 0, repo);
3140 97a02382 2023-07-10 thomas if (error != NULL) {
3141 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_ANCESTRY) {
3142 97a02382 2023-07-10 thomas error = checkout_ancestry_error(
3143 97a02382 2023-07-10 thomas head_ref, commit_id_str);
3145 97a02382 2023-07-10 thomas goto done;
3147 97a02382 2023-07-10 thomas error = check_same_branch(commit_id, head_ref, repo);
3148 97a02382 2023-07-10 thomas if (error) {
3149 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_ANCESTRY) {
3150 97a02382 2023-07-10 thomas error = checkout_ancestry_error(
3151 97a02382 2023-07-10 thomas head_ref, commit_id_str);
3153 97a02382 2023-07-10 thomas goto done;
3155 97a02382 2023-07-10 thomas error = got_worktree_set_base_commit_id(worktree, repo,
3156 97a02382 2023-07-10 thomas commit_id);
3157 97a02382 2023-07-10 thomas if (error)
3158 97a02382 2023-07-10 thomas goto done;
3159 97a02382 2023-07-10 thomas /* Expand potentially abbreviated commit ID string. */
3160 97a02382 2023-07-10 thomas free(commit_id_str);
3161 97a02382 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
3162 97a02382 2023-07-10 thomas if (error)
3163 97a02382 2023-07-10 thomas goto done;
3164 97a02382 2023-07-10 thomas } else {
3165 97a02382 2023-07-10 thomas commit_id = got_object_id_dup(
3166 97a02382 2023-07-10 thomas got_worktree_get_base_commit_id(worktree));
3167 97a02382 2023-07-10 thomas if (commit_id == NULL) {
3168 97a02382 2023-07-10 thomas error = got_error_from_errno("got_object_id_dup");
3169 97a02382 2023-07-10 thomas goto done;
3171 97a02382 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
3172 97a02382 2023-07-10 thomas if (error)
3173 97a02382 2023-07-10 thomas goto done;
3176 97a02382 2023-07-10 thomas error = got_pathlist_append(&paths, "", NULL);
3177 97a02382 2023-07-10 thomas if (error)
3178 97a02382 2023-07-10 thomas goto done;
3179 97a02382 2023-07-10 thomas cpa.worktree_path = worktree_path;
3180 97a02382 2023-07-10 thomas cpa.had_base_commit_ref_error = 0;
3181 97a02382 2023-07-10 thomas cpa.verbosity = verbosity;
3182 97a02382 2023-07-10 thomas error = got_worktree_checkout_files(worktree, &paths, repo,
3183 97a02382 2023-07-10 thomas checkout_progress, &cpa, check_cancelled, NULL);
3184 97a02382 2023-07-10 thomas if (error != NULL)
3185 97a02382 2023-07-10 thomas goto done;
3187 97a02382 2023-07-10 thomas if (got_ref_is_symbolic(head_ref)) {
3188 97a02382 2023-07-10 thomas error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3189 97a02382 2023-07-10 thomas if (error)
3190 97a02382 2023-07-10 thomas goto done;
3191 97a02382 2023-07-10 thomas refname = got_ref_get_name(ref);
3193 97a02382 2023-07-10 thomas refname = got_ref_get_name(head_ref);
3194 97a02382 2023-07-10 thomas printf("Checked out %s: %s\n", refname, commit_id_str);
3195 97a02382 2023-07-10 thomas printf("Now shut up and hack\n");
3196 97a02382 2023-07-10 thomas if (cpa.had_base_commit_ref_error)
3197 97a02382 2023-07-10 thomas show_worktree_base_ref_warning();
3199 97a02382 2023-07-10 thomas if (pack_fds) {
3200 97a02382 2023-07-10 thomas const struct got_error *pack_err =
3201 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
3202 97a02382 2023-07-10 thomas if (error == NULL)
3203 97a02382 2023-07-10 thomas error = pack_err;
3205 97a02382 2023-07-10 thomas if (head_ref)
3206 97a02382 2023-07-10 thomas got_ref_close(head_ref);
3207 97a02382 2023-07-10 thomas if (ref)
3208 97a02382 2023-07-10 thomas got_ref_close(ref);
3209 97a02382 2023-07-10 thomas if (repo) {
3210 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
3211 97a02382 2023-07-10 thomas if (error == NULL)
3212 97a02382 2023-07-10 thomas error = close_err;
3214 97a02382 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
3215 97a02382 2023-07-10 thomas free(commit_id_str);
3216 97a02382 2023-07-10 thomas free(commit_id);
3217 97a02382 2023-07-10 thomas free(repo_path);
3218 97a02382 2023-07-10 thomas free(worktree_path);
3219 97a02382 2023-07-10 thomas free(cwd);
3220 97a02382 2023-07-10 thomas return error;
3223 97a02382 2023-07-10 thomas struct got_update_progress_arg {
3224 97a02382 2023-07-10 thomas int did_something;
3225 97a02382 2023-07-10 thomas int conflicts;
3226 97a02382 2023-07-10 thomas int obstructed;
3227 97a02382 2023-07-10 thomas int not_updated;
3228 97a02382 2023-07-10 thomas int missing;
3229 97a02382 2023-07-10 thomas int not_deleted;
3230 97a02382 2023-07-10 thomas int unversioned;
3231 97a02382 2023-07-10 thomas int verbosity;
3234 97a02382 2023-07-10 thomas static void
3235 97a02382 2023-07-10 thomas print_update_progress_stats(struct got_update_progress_arg *upa)
3237 97a02382 2023-07-10 thomas if (!upa->did_something)
3240 97a02382 2023-07-10 thomas if (upa->conflicts > 0)
3241 97a02382 2023-07-10 thomas printf("Files with new merge conflicts: %d\n", upa->conflicts);
3242 97a02382 2023-07-10 thomas if (upa->obstructed > 0)
3243 97a02382 2023-07-10 thomas printf("File paths obstructed by a non-regular file: %d\n",
3244 97a02382 2023-07-10 thomas upa->obstructed);
3245 97a02382 2023-07-10 thomas if (upa->not_updated > 0)
3246 97a02382 2023-07-10 thomas printf("Files not updated because of existing merge "
3247 97a02382 2023-07-10 thomas "conflicts: %d\n", upa->not_updated);
3251 97a02382 2023-07-10 thomas * The meaning of some status codes differs between merge-style operations and
3252 97a02382 2023-07-10 thomas * update operations. For example, the ! status code means "file was missing"
3253 97a02382 2023-07-10 thomas * if changes were merged into the work tree, and "missing file was restored"
3254 97a02382 2023-07-10 thomas * if the work tree was updated. This function should be used by any operation
3255 97a02382 2023-07-10 thomas * which merges changes into the work tree without updating the work tree.
3257 97a02382 2023-07-10 thomas static void
3258 97a02382 2023-07-10 thomas print_merge_progress_stats(struct got_update_progress_arg *upa)
3260 97a02382 2023-07-10 thomas if (!upa->did_something)
3263 97a02382 2023-07-10 thomas if (upa->conflicts > 0)
3264 97a02382 2023-07-10 thomas printf("Files with new merge conflicts: %d\n", upa->conflicts);
3265 97a02382 2023-07-10 thomas if (upa->obstructed > 0)
3266 97a02382 2023-07-10 thomas printf("File paths obstructed by a non-regular file: %d\n",
3267 97a02382 2023-07-10 thomas upa->obstructed);
3268 97a02382 2023-07-10 thomas if (upa->missing > 0)
3269 97a02382 2023-07-10 thomas printf("Files which had incoming changes but could not be "
3270 97a02382 2023-07-10 thomas "found in the work tree: %d\n", upa->missing);
3271 97a02382 2023-07-10 thomas if (upa->not_deleted > 0)
3272 97a02382 2023-07-10 thomas printf("Files not deleted due to differences in deleted "
3273 97a02382 2023-07-10 thomas "content: %d\n", upa->not_deleted);
3274 97a02382 2023-07-10 thomas if (upa->unversioned > 0)
3275 97a02382 2023-07-10 thomas printf("Files not merged because an unversioned file was "
3276 97a02382 2023-07-10 thomas "found in the work tree: %d\n", upa->unversioned);
3279 97a02382 2023-07-10 thomas __dead static void
3280 97a02382 2023-07-10 thomas usage_update(void)
3282 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s update [-q] [-b branch] [-c commit] "
3283 97a02382 2023-07-10 thomas "[path ...]\n", getprogname());
3284 97a02382 2023-07-10 thomas exit(1);
3287 97a02382 2023-07-10 thomas static const struct got_error *
3288 97a02382 2023-07-10 thomas update_progress(void *arg, unsigned char status, const char *path)
3290 97a02382 2023-07-10 thomas struct got_update_progress_arg *upa = arg;
3292 97a02382 2023-07-10 thomas if (status == GOT_STATUS_EXISTS ||
3293 97a02382 2023-07-10 thomas status == GOT_STATUS_BASE_REF_ERR)
3294 97a02382 2023-07-10 thomas return NULL;
3296 97a02382 2023-07-10 thomas upa->did_something = 1;
3298 97a02382 2023-07-10 thomas /* Base commit bump happens silently. */
3299 97a02382 2023-07-10 thomas if (status == GOT_STATUS_BUMP_BASE)
3300 97a02382 2023-07-10 thomas return NULL;
3302 97a02382 2023-07-10 thomas if (status == GOT_STATUS_CONFLICT)
3303 97a02382 2023-07-10 thomas upa->conflicts++;
3304 97a02382 2023-07-10 thomas if (status == GOT_STATUS_OBSTRUCTED)
3305 97a02382 2023-07-10 thomas upa->obstructed++;
3306 97a02382 2023-07-10 thomas if (status == GOT_STATUS_CANNOT_UPDATE)
3307 97a02382 2023-07-10 thomas upa->not_updated++;
3308 97a02382 2023-07-10 thomas if (status == GOT_STATUS_MISSING)
3309 97a02382 2023-07-10 thomas upa->missing++;
3310 97a02382 2023-07-10 thomas if (status == GOT_STATUS_CANNOT_DELETE)
3311 97a02382 2023-07-10 thomas upa->not_deleted++;
3312 97a02382 2023-07-10 thomas if (status == GOT_STATUS_UNVERSIONED)
3313 97a02382 2023-07-10 thomas upa->unversioned++;
3315 97a02382 2023-07-10 thomas while (path[0] == '/')
3317 97a02382 2023-07-10 thomas if (upa->verbosity >= 0)
3318 97a02382 2023-07-10 thomas printf("%c %s\n", status, path);
3320 97a02382 2023-07-10 thomas return NULL;
3323 97a02382 2023-07-10 thomas static const struct got_error *
3324 97a02382 2023-07-10 thomas switch_head_ref(struct got_reference *head_ref,
3325 97a02382 2023-07-10 thomas struct got_object_id *commit_id, struct got_worktree *worktree,
3326 97a02382 2023-07-10 thomas struct got_repository *repo)
3328 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
3329 97a02382 2023-07-10 thomas char *base_id_str;
3330 97a02382 2023-07-10 thomas int ref_has_moved = 0;
3332 97a02382 2023-07-10 thomas /* Trivial case: switching between two different references. */
3333 97a02382 2023-07-10 thomas if (strcmp(got_ref_get_name(head_ref),
3334 97a02382 2023-07-10 thomas got_worktree_get_head_ref_name(worktree)) != 0) {
3335 97a02382 2023-07-10 thomas printf("Switching work tree from %s to %s\n",
3336 97a02382 2023-07-10 thomas got_worktree_get_head_ref_name(worktree),
3337 97a02382 2023-07-10 thomas got_ref_get_name(head_ref));
3338 97a02382 2023-07-10 thomas return got_worktree_set_head_ref(worktree, head_ref);
3341 97a02382 2023-07-10 thomas err = check_linear_ancestry(commit_id,
3342 97a02382 2023-07-10 thomas got_worktree_get_base_commit_id(worktree), 0, repo);
3343 97a02382 2023-07-10 thomas if (err) {
3344 97a02382 2023-07-10 thomas if (err->code != GOT_ERR_ANCESTRY)
3345 97a02382 2023-07-10 thomas return err;
3346 97a02382 2023-07-10 thomas ref_has_moved = 1;
3348 97a02382 2023-07-10 thomas if (!ref_has_moved)
3349 97a02382 2023-07-10 thomas return NULL;
3351 97a02382 2023-07-10 thomas /* Switching to a rebased branch with the same reference name. */
3352 97a02382 2023-07-10 thomas err = got_object_id_str(&base_id_str,
3353 97a02382 2023-07-10 thomas got_worktree_get_base_commit_id(worktree));
3354 97a02382 2023-07-10 thomas if (err)
3355 97a02382 2023-07-10 thomas return err;
3356 97a02382 2023-07-10 thomas printf("Reference %s now points at a different branch\n",
3357 97a02382 2023-07-10 thomas got_worktree_get_head_ref_name(worktree));
3358 97a02382 2023-07-10 thomas printf("Switching work tree from %s to %s\n", base_id_str,
3359 97a02382 2023-07-10 thomas got_worktree_get_head_ref_name(worktree));
3360 97a02382 2023-07-10 thomas return NULL;
3363 97a02382 2023-07-10 thomas static const struct got_error *
3364 97a02382 2023-07-10 thomas check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3366 97a02382 2023-07-10 thomas const struct got_error *err;
3367 97a02382 2023-07-10 thomas int in_progress;
3369 97a02382 2023-07-10 thomas err = got_worktree_rebase_in_progress(&in_progress, worktree);
3370 97a02382 2023-07-10 thomas if (err)
3371 97a02382 2023-07-10 thomas return err;
3372 97a02382 2023-07-10 thomas if (in_progress)
3373 97a02382 2023-07-10 thomas return got_error(GOT_ERR_REBASING);
3375 97a02382 2023-07-10 thomas err = got_worktree_histedit_in_progress(&in_progress, worktree);
3376 97a02382 2023-07-10 thomas if (err)
3377 97a02382 2023-07-10 thomas return err;
3378 97a02382 2023-07-10 thomas if (in_progress)
3379 97a02382 2023-07-10 thomas return got_error(GOT_ERR_HISTEDIT_BUSY);
3381 97a02382 2023-07-10 thomas return NULL;
3384 97a02382 2023-07-10 thomas static const struct got_error *
3385 97a02382 2023-07-10 thomas check_merge_in_progress(struct got_worktree *worktree,
3386 97a02382 2023-07-10 thomas struct got_repository *repo)
3388 97a02382 2023-07-10 thomas const struct got_error *err;
3389 97a02382 2023-07-10 thomas int in_progress;
3391 97a02382 2023-07-10 thomas err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3392 97a02382 2023-07-10 thomas if (err)
3393 97a02382 2023-07-10 thomas return err;
3394 97a02382 2023-07-10 thomas if (in_progress)
3395 97a02382 2023-07-10 thomas return got_error(GOT_ERR_MERGE_BUSY);
3397 97a02382 2023-07-10 thomas return NULL;
3400 97a02382 2023-07-10 thomas static const struct got_error *
3401 97a02382 2023-07-10 thomas get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3402 97a02382 2023-07-10 thomas char *argv[], struct got_worktree *worktree)
3404 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
3405 97a02382 2023-07-10 thomas char *path;
3406 97a02382 2023-07-10 thomas struct got_pathlist_entry *new;
3409 97a02382 2023-07-10 thomas if (argc == 0) {
3410 97a02382 2023-07-10 thomas path = strdup("");
3411 97a02382 2023-07-10 thomas if (path == NULL)
3412 97a02382 2023-07-10 thomas return got_error_from_errno("strdup");
3413 97a02382 2023-07-10 thomas return got_pathlist_append(paths, path, NULL);
3416 97a02382 2023-07-10 thomas for (i = 0; i < argc; i++) {
3417 97a02382 2023-07-10 thomas err = got_worktree_resolve_path(&path, worktree, argv[i]);
3418 97a02382 2023-07-10 thomas if (err)
3420 97a02382 2023-07-10 thomas err = got_pathlist_insert(&new, paths, path, NULL);
3421 97a02382 2023-07-10 thomas if (err || new == NULL /* duplicate */) {
3422 97a02382 2023-07-10 thomas free(path);
3423 97a02382 2023-07-10 thomas if (err)
3428 97a02382 2023-07-10 thomas return err;
3431 97a02382 2023-07-10 thomas static const struct got_error *
3432 97a02382 2023-07-10 thomas wrap_not_worktree_error(const struct got_error *orig_err,
3433 97a02382 2023-07-10 thomas const char *cmdname, const char *path)
3435 97a02382 2023-07-10 thomas const struct got_error *err;
3436 97a02382 2023-07-10 thomas struct got_repository *repo;
3437 97a02382 2023-07-10 thomas static char msg[512];
3438 97a02382 2023-07-10 thomas int *pack_fds = NULL;
3440 97a02382 2023-07-10 thomas err = got_repo_pack_fds_open(&pack_fds);
3441 97a02382 2023-07-10 thomas if (err)
3442 97a02382 2023-07-10 thomas return err;
3444 97a02382 2023-07-10 thomas err = got_repo_open(&repo, path, NULL, pack_fds);
3445 97a02382 2023-07-10 thomas if (err)
3446 97a02382 2023-07-10 thomas return orig_err;
3448 97a02382 2023-07-10 thomas snprintf(msg, sizeof(msg),
3449 97a02382 2023-07-10 thomas "'got %s' needs a work tree in addition to a git repository\n"
3450 97a02382 2023-07-10 thomas "Work trees can be checked out from this Git repository with "
3451 97a02382 2023-07-10 thomas "'got checkout'.\n"
3452 97a02382 2023-07-10 thomas "The got(1) manual page contains more information.", cmdname);
3453 97a02382 2023-07-10 thomas err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3454 97a02382 2023-07-10 thomas if (repo) {
3455 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
3456 97a02382 2023-07-10 thomas if (err == NULL)
3457 97a02382 2023-07-10 thomas err = close_err;
3459 97a02382 2023-07-10 thomas if (pack_fds) {
3460 97a02382 2023-07-10 thomas const struct got_error *pack_err =
3461 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
3462 97a02382 2023-07-10 thomas if (err == NULL)
3463 97a02382 2023-07-10 thomas err = pack_err;
3465 97a02382 2023-07-10 thomas return err;
3468 97a02382 2023-07-10 thomas static const struct got_error *
3469 97a02382 2023-07-10 thomas cmd_update(int argc, char *argv[])
3471 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
3472 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
3473 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
3474 97a02382 2023-07-10 thomas char *worktree_path = NULL;
3475 97a02382 2023-07-10 thomas struct got_object_id *commit_id = NULL;
3476 97a02382 2023-07-10 thomas char *commit_id_str = NULL;
3477 97a02382 2023-07-10 thomas const char *branch_name = NULL;
3478 97a02382 2023-07-10 thomas struct got_reference *head_ref = NULL;
3479 97a02382 2023-07-10 thomas struct got_pathlist_head paths;
3480 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
3481 97a02382 2023-07-10 thomas int ch, verbosity = 0;
3482 97a02382 2023-07-10 thomas struct got_update_progress_arg upa;
3483 97a02382 2023-07-10 thomas int *pack_fds = NULL;
3485 97a02382 2023-07-10 thomas TAILQ_INIT(&paths);
3487 97a02382 2023-07-10 thomas #ifndef PROFILE
3488 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3489 97a02382 2023-07-10 thomas "unveil", NULL) == -1)
3490 97a02382 2023-07-10 thomas err(1, "pledge");
3493 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3494 97a02382 2023-07-10 thomas switch (ch) {
3495 97a02382 2023-07-10 thomas case 'b':
3496 97a02382 2023-07-10 thomas branch_name = optarg;
3498 97a02382 2023-07-10 thomas case 'c':
3499 97a02382 2023-07-10 thomas commit_id_str = strdup(optarg);
3500 97a02382 2023-07-10 thomas if (commit_id_str == NULL)
3501 97a02382 2023-07-10 thomas return got_error_from_errno("strdup");
3503 97a02382 2023-07-10 thomas case 'q':
3504 97a02382 2023-07-10 thomas verbosity = -1;
3506 97a02382 2023-07-10 thomas default:
3507 97a02382 2023-07-10 thomas usage_update();
3508 97a02382 2023-07-10 thomas /* NOTREACHED */
3512 97a02382 2023-07-10 thomas argc -= optind;
3513 97a02382 2023-07-10 thomas argv += optind;
3515 97a02382 2023-07-10 thomas worktree_path = getcwd(NULL, 0);
3516 97a02382 2023-07-10 thomas if (worktree_path == NULL) {
3517 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
3518 97a02382 2023-07-10 thomas goto done;
3521 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
3522 97a02382 2023-07-10 thomas if (error != NULL)
3523 97a02382 2023-07-10 thomas goto done;
3525 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, worktree_path);
3526 97a02382 2023-07-10 thomas if (error) {
3527 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
3528 97a02382 2023-07-10 thomas error = wrap_not_worktree_error(error, "update",
3529 97a02382 2023-07-10 thomas worktree_path);
3530 97a02382 2023-07-10 thomas goto done;
3533 97a02382 2023-07-10 thomas error = check_rebase_or_histedit_in_progress(worktree);
3534 97a02382 2023-07-10 thomas if (error)
3535 97a02382 2023-07-10 thomas goto done;
3537 97a02382 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3538 97a02382 2023-07-10 thomas NULL, pack_fds);
3539 97a02382 2023-07-10 thomas if (error != NULL)
3540 97a02382 2023-07-10 thomas goto done;
3542 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
3543 97a02382 2023-07-10 thomas got_worktree_get_root_path(worktree));
3544 97a02382 2023-07-10 thomas if (error)
3545 97a02382 2023-07-10 thomas goto done;
3547 97a02382 2023-07-10 thomas error = check_merge_in_progress(worktree, repo);
3548 97a02382 2023-07-10 thomas if (error)
3549 97a02382 2023-07-10 thomas goto done;
3551 97a02382 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3552 97a02382 2023-07-10 thomas if (error)
3553 97a02382 2023-07-10 thomas goto done;
3555 97a02382 2023-07-10 thomas error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3556 97a02382 2023-07-10 thomas got_worktree_get_head_ref_name(worktree), 0);
3557 97a02382 2023-07-10 thomas if (error != NULL)
3558 97a02382 2023-07-10 thomas goto done;
3559 97a02382 2023-07-10 thomas if (commit_id_str == NULL) {
3560 97a02382 2023-07-10 thomas error = got_ref_resolve(&commit_id, repo, head_ref);
3561 97a02382 2023-07-10 thomas if (error != NULL)
3562 97a02382 2023-07-10 thomas goto done;
3563 97a02382 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
3564 97a02382 2023-07-10 thomas if (error != NULL)
3565 97a02382 2023-07-10 thomas goto done;
3566 97a02382 2023-07-10 thomas } else {
3567 97a02382 2023-07-10 thomas struct got_reflist_head refs;
3568 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
3569 97a02382 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3571 97a02382 2023-07-10 thomas if (error)
3572 97a02382 2023-07-10 thomas goto done;
3573 97a02382 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
3574 97a02382 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3575 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
3576 97a02382 2023-07-10 thomas free(commit_id_str);
3577 97a02382 2023-07-10 thomas commit_id_str = NULL;
3578 97a02382 2023-07-10 thomas if (error)
3579 97a02382 2023-07-10 thomas goto done;
3580 97a02382 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
3581 97a02382 2023-07-10 thomas if (error)
3582 97a02382 2023-07-10 thomas goto done;
3585 97a02382 2023-07-10 thomas if (branch_name) {
3586 97a02382 2023-07-10 thomas struct got_object_id *head_commit_id;
3587 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, &paths, entry) {
3588 97a02382 2023-07-10 thomas if (pe->path_len == 0)
3589 97a02382 2023-07-10 thomas continue;
3590 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_BAD_PATH,
3591 97a02382 2023-07-10 thomas "switching between branches requires that "
3592 97a02382 2023-07-10 thomas "the entire work tree gets updated");
3593 97a02382 2023-07-10 thomas goto done;
3595 97a02382 2023-07-10 thomas error = got_ref_resolve(&head_commit_id, repo, head_ref);
3596 97a02382 2023-07-10 thomas if (error)
3597 97a02382 2023-07-10 thomas goto done;
3598 97a02382 2023-07-10 thomas error = check_linear_ancestry(commit_id, head_commit_id, 0,
3600 97a02382 2023-07-10 thomas free(head_commit_id);
3601 97a02382 2023-07-10 thomas if (error != NULL)
3602 97a02382 2023-07-10 thomas goto done;
3603 97a02382 2023-07-10 thomas error = check_same_branch(commit_id, head_ref, repo);
3604 97a02382 2023-07-10 thomas if (error)
3605 97a02382 2023-07-10 thomas goto done;
3606 97a02382 2023-07-10 thomas error = switch_head_ref(head_ref, commit_id, worktree, repo);
3607 97a02382 2023-07-10 thomas if (error)
3608 97a02382 2023-07-10 thomas goto done;
3609 97a02382 2023-07-10 thomas } else {
3610 97a02382 2023-07-10 thomas error = check_linear_ancestry(commit_id,
3611 97a02382 2023-07-10 thomas got_worktree_get_base_commit_id(worktree), 0, repo);
3612 97a02382 2023-07-10 thomas if (error != NULL) {
3613 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_ANCESTRY)
3614 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_BRANCH_MOVED);
3615 97a02382 2023-07-10 thomas goto done;
3617 97a02382 2023-07-10 thomas error = check_same_branch(commit_id, head_ref, repo);
3618 97a02382 2023-07-10 thomas if (error)
3619 97a02382 2023-07-10 thomas goto done;
3622 97a02382 2023-07-10 thomas if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3623 97a02382 2023-07-10 thomas commit_id) != 0) {
3624 97a02382 2023-07-10 thomas error = got_worktree_set_base_commit_id(worktree, repo,
3625 97a02382 2023-07-10 thomas commit_id);
3626 97a02382 2023-07-10 thomas if (error)
3627 97a02382 2023-07-10 thomas goto done;
3630 97a02382 2023-07-10 thomas memset(&upa, 0, sizeof(upa));
3631 97a02382 2023-07-10 thomas upa.verbosity = verbosity;
3632 97a02382 2023-07-10 thomas error = got_worktree_checkout_files(worktree, &paths, repo,
3633 97a02382 2023-07-10 thomas update_progress, &upa, check_cancelled, NULL);
3634 97a02382 2023-07-10 thomas if (error != NULL)
3635 97a02382 2023-07-10 thomas goto done;
3637 97a02382 2023-07-10 thomas if (upa.did_something) {
3638 97a02382 2023-07-10 thomas printf("Updated to %s: %s\n",
3639 97a02382 2023-07-10 thomas got_worktree_get_head_ref_name(worktree), commit_id_str);
3641 97a02382 2023-07-10 thomas printf("Already up-to-date\n");
3643 97a02382 2023-07-10 thomas print_update_progress_stats(&upa);
3645 97a02382 2023-07-10 thomas if (pack_fds) {
3646 97a02382 2023-07-10 thomas const struct got_error *pack_err =
3647 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
3648 97a02382 2023-07-10 thomas if (error == NULL)
3649 97a02382 2023-07-10 thomas error = pack_err;
3651 97a02382 2023-07-10 thomas if (repo) {
3652 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
3653 97a02382 2023-07-10 thomas if (error == NULL)
3654 97a02382 2023-07-10 thomas error = close_err;
3656 97a02382 2023-07-10 thomas free(worktree_path);
3657 97a02382 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3658 97a02382 2023-07-10 thomas free(commit_id);
3659 97a02382 2023-07-10 thomas free(commit_id_str);
3660 97a02382 2023-07-10 thomas return error;
3663 97a02382 2023-07-10 thomas static const struct got_error *
3664 97a02382 2023-07-10 thomas diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3665 97a02382 2023-07-10 thomas const char *path, int diff_context, int ignore_whitespace,
3666 97a02382 2023-07-10 thomas int force_text_diff, struct got_diffstat_cb_arg *dsa,
3667 97a02382 2023-07-10 thomas struct got_repository *repo, FILE *outfile)
3669 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
3670 97a02382 2023-07-10 thomas struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3671 97a02382 2023-07-10 thomas FILE *f1 = NULL, *f2 = NULL;
3672 97a02382 2023-07-10 thomas int fd1 = -1, fd2 = -1;
3674 97a02382 2023-07-10 thomas fd1 = got_opentempfd();
3675 97a02382 2023-07-10 thomas if (fd1 == -1)
3676 97a02382 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
3677 97a02382 2023-07-10 thomas fd2 = got_opentempfd();
3678 97a02382 2023-07-10 thomas if (fd2 == -1) {
3679 97a02382 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
3680 97a02382 2023-07-10 thomas goto done;
3683 97a02382 2023-07-10 thomas if (blob_id1) {
3684 97a02382 2023-07-10 thomas err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3686 97a02382 2023-07-10 thomas if (err)
3687 97a02382 2023-07-10 thomas goto done;
3690 97a02382 2023-07-10 thomas err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3691 97a02382 2023-07-10 thomas if (err)
3692 97a02382 2023-07-10 thomas goto done;
3694 97a02382 2023-07-10 thomas f1 = got_opentemp();
3695 97a02382 2023-07-10 thomas if (f1 == NULL) {
3696 97a02382 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3697 97a02382 2023-07-10 thomas goto done;
3699 97a02382 2023-07-10 thomas f2 = got_opentemp();
3700 97a02382 2023-07-10 thomas if (f2 == NULL) {
3701 97a02382 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3702 97a02382 2023-07-10 thomas goto done;
3705 97a02382 2023-07-10 thomas while (path[0] == '/')
3707 97a02382 2023-07-10 thomas err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3708 97a02382 2023-07-10 thomas GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3709 97a02382 2023-07-10 thomas force_text_diff, dsa, outfile);
3711 97a02382 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3712 97a02382 2023-07-10 thomas err = got_error_from_errno("close");
3713 97a02382 2023-07-10 thomas if (blob1)
3714 97a02382 2023-07-10 thomas got_object_blob_close(blob1);
3715 97a02382 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3716 97a02382 2023-07-10 thomas err = got_error_from_errno("close");
3717 97a02382 2023-07-10 thomas if (blob2)
3718 97a02382 2023-07-10 thomas got_object_blob_close(blob2);
3719 97a02382 2023-07-10 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3720 97a02382 2023-07-10 thomas err = got_error_from_errno("fclose");
3721 97a02382 2023-07-10 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3722 97a02382 2023-07-10 thomas err = got_error_from_errno("fclose");
3723 97a02382 2023-07-10 thomas return err;
3726 97a02382 2023-07-10 thomas static const struct got_error *
3727 97a02382 2023-07-10 thomas diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3728 97a02382 2023-07-10 thomas const char *path, int diff_context, int ignore_whitespace,
3729 97a02382 2023-07-10 thomas int force_text_diff, struct got_diffstat_cb_arg *dsa,
3730 97a02382 2023-07-10 thomas struct got_repository *repo, FILE *outfile)
3732 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
3733 97a02382 2023-07-10 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3734 97a02382 2023-07-10 thomas struct got_diff_blob_output_unidiff_arg arg;
3735 97a02382 2023-07-10 thomas FILE *f1 = NULL, *f2 = NULL;
3736 97a02382 2023-07-10 thomas int fd1 = -1, fd2 = -1;
3738 97a02382 2023-07-10 thomas if (tree_id1) {
3739 97a02382 2023-07-10 thomas err = got_object_open_as_tree(&tree1, repo, tree_id1);
3740 97a02382 2023-07-10 thomas if (err)
3741 97a02382 2023-07-10 thomas goto done;
3742 97a02382 2023-07-10 thomas fd1 = got_opentempfd();
3743 97a02382 2023-07-10 thomas if (fd1 == -1) {
3744 97a02382 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
3745 97a02382 2023-07-10 thomas goto done;
3749 97a02382 2023-07-10 thomas err = got_object_open_as_tree(&tree2, repo, tree_id2);
3750 97a02382 2023-07-10 thomas if (err)
3751 97a02382 2023-07-10 thomas goto done;
3753 97a02382 2023-07-10 thomas f1 = got_opentemp();
3754 97a02382 2023-07-10 thomas if (f1 == NULL) {
3755 97a02382 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3756 97a02382 2023-07-10 thomas goto done;
3759 97a02382 2023-07-10 thomas f2 = got_opentemp();
3760 97a02382 2023-07-10 thomas if (f2 == NULL) {
3761 97a02382 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3762 97a02382 2023-07-10 thomas goto done;
3764 97a02382 2023-07-10 thomas fd2 = got_opentempfd();
3765 97a02382 2023-07-10 thomas if (fd2 == -1) {
3766 97a02382 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
3767 97a02382 2023-07-10 thomas goto done;
3769 97a02382 2023-07-10 thomas arg.diff_context = diff_context;
3770 97a02382 2023-07-10 thomas arg.ignore_whitespace = ignore_whitespace;
3771 97a02382 2023-07-10 thomas arg.force_text_diff = force_text_diff;
3772 97a02382 2023-07-10 thomas arg.diffstat = dsa;
3773 97a02382 2023-07-10 thomas arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3774 97a02382 2023-07-10 thomas arg.outfile = outfile;
3775 97a02382 2023-07-10 thomas arg.lines = NULL;
3776 97a02382 2023-07-10 thomas arg.nlines = 0;
3777 97a02382 2023-07-10 thomas while (path[0] == '/')
3779 97a02382 2023-07-10 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3780 97a02382 2023-07-10 thomas got_diff_blob_output_unidiff, &arg, 1);
3782 97a02382 2023-07-10 thomas if (tree1)
3783 97a02382 2023-07-10 thomas got_object_tree_close(tree1);
3784 97a02382 2023-07-10 thomas if (tree2)
3785 97a02382 2023-07-10 thomas got_object_tree_close(tree2);
3786 97a02382 2023-07-10 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3787 97a02382 2023-07-10 thomas err = got_error_from_errno("fclose");
3788 97a02382 2023-07-10 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3789 97a02382 2023-07-10 thomas err = got_error_from_errno("fclose");
3790 97a02382 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3791 97a02382 2023-07-10 thomas err = got_error_from_errno("close");
3792 97a02382 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3793 97a02382 2023-07-10 thomas err = got_error_from_errno("close");
3794 97a02382 2023-07-10 thomas return err;
3797 97a02382 2023-07-10 thomas static const struct got_error *
3798 97a02382 2023-07-10 thomas get_changed_paths(struct got_pathlist_head *paths,
3799 97a02382 2023-07-10 thomas struct got_commit_object *commit, struct got_repository *repo,
3800 97a02382 2023-07-10 thomas struct got_diffstat_cb_arg *dsa)
3802 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
3803 97a02382 2023-07-10 thomas struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3804 97a02382 2023-07-10 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3805 97a02382 2023-07-10 thomas struct got_object_qid *qid;
3806 97a02382 2023-07-10 thomas got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3807 97a02382 2023-07-10 thomas FILE *f1 = NULL, *f2 = NULL;
3808 97a02382 2023-07-10 thomas int fd1 = -1, fd2 = -1;
3810 97a02382 2023-07-10 thomas if (dsa) {
3811 97a02382 2023-07-10 thomas cb = got_diff_tree_compute_diffstat;
3813 97a02382 2023-07-10 thomas f1 = got_opentemp();
3814 97a02382 2023-07-10 thomas if (f1 == NULL) {
3815 97a02382 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3816 97a02382 2023-07-10 thomas goto done;
3818 97a02382 2023-07-10 thomas f2 = got_opentemp();
3819 97a02382 2023-07-10 thomas if (f2 == NULL) {
3820 97a02382 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3821 97a02382 2023-07-10 thomas goto done;
3823 97a02382 2023-07-10 thomas fd1 = got_opentempfd();
3824 97a02382 2023-07-10 thomas if (fd1 == -1) {
3825 97a02382 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
3826 97a02382 2023-07-10 thomas goto done;
3828 97a02382 2023-07-10 thomas fd2 = got_opentempfd();
3829 97a02382 2023-07-10 thomas if (fd2 == -1) {
3830 97a02382 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
3831 97a02382 2023-07-10 thomas goto done;
3835 97a02382 2023-07-10 thomas qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3836 97a02382 2023-07-10 thomas if (qid != NULL) {
3837 97a02382 2023-07-10 thomas struct got_commit_object *pcommit;
3838 97a02382 2023-07-10 thomas err = got_object_open_as_commit(&pcommit, repo,
3839 97a02382 2023-07-10 thomas &qid->id);
3840 97a02382 2023-07-10 thomas if (err)
3841 97a02382 2023-07-10 thomas return err;
3843 97a02382 2023-07-10 thomas tree_id1 = got_object_id_dup(
3844 97a02382 2023-07-10 thomas got_object_commit_get_tree_id(pcommit));
3845 97a02382 2023-07-10 thomas if (tree_id1 == NULL) {
3846 97a02382 2023-07-10 thomas got_object_commit_close(pcommit);
3847 97a02382 2023-07-10 thomas return got_error_from_errno("got_object_id_dup");
3849 97a02382 2023-07-10 thomas got_object_commit_close(pcommit);
3853 97a02382 2023-07-10 thomas if (tree_id1) {
3854 97a02382 2023-07-10 thomas err = got_object_open_as_tree(&tree1, repo, tree_id1);
3855 97a02382 2023-07-10 thomas if (err)
3856 97a02382 2023-07-10 thomas goto done;
3859 97a02382 2023-07-10 thomas tree_id2 = got_object_commit_get_tree_id(commit);
3860 97a02382 2023-07-10 thomas err = got_object_open_as_tree(&tree2, repo, tree_id2);
3861 97a02382 2023-07-10 thomas if (err)
3862 97a02382 2023-07-10 thomas goto done;
3864 97a02382 2023-07-10 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3865 97a02382 2023-07-10 thomas cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3867 97a02382 2023-07-10 thomas if (tree1)
3868 97a02382 2023-07-10 thomas got_object_tree_close(tree1);
3869 97a02382 2023-07-10 thomas if (tree2)
3870 97a02382 2023-07-10 thomas got_object_tree_close(tree2);
3871 97a02382 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3872 97a02382 2023-07-10 thomas err = got_error_from_errno("close");
3873 97a02382 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3874 97a02382 2023-07-10 thomas err = got_error_from_errno("close");
3875 97a02382 2023-07-10 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3876 97a02382 2023-07-10 thomas err = got_error_from_errno("fclose");
3877 97a02382 2023-07-10 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3878 97a02382 2023-07-10 thomas err = got_error_from_errno("fclose");
3879 97a02382 2023-07-10 thomas free(tree_id1);
3880 97a02382 2023-07-10 thomas return err;
3883 97a02382 2023-07-10 thomas static const struct got_error *
3884 97a02382 2023-07-10 thomas print_patch(struct got_commit_object *commit, struct got_object_id *id,
3885 97a02382 2023-07-10 thomas const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3886 97a02382 2023-07-10 thomas struct got_repository *repo, FILE *outfile)
3888 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
3889 97a02382 2023-07-10 thomas struct got_commit_object *pcommit = NULL;
3890 97a02382 2023-07-10 thomas char *id_str1 = NULL, *id_str2 = NULL;
3891 97a02382 2023-07-10 thomas struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3892 97a02382 2023-07-10 thomas struct got_object_qid *qid;
3894 97a02382 2023-07-10 thomas qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3895 97a02382 2023-07-10 thomas if (qid != NULL) {
3896 97a02382 2023-07-10 thomas err = got_object_open_as_commit(&pcommit, repo,
3897 97a02382 2023-07-10 thomas &qid->id);
3898 97a02382 2023-07-10 thomas if (err)
3899 97a02382 2023-07-10 thomas return err;
3900 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str1, &qid->id);
3901 97a02382 2023-07-10 thomas if (err)
3902 97a02382 2023-07-10 thomas goto done;
3905 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str2, id);
3906 97a02382 2023-07-10 thomas if (err)
3907 97a02382 2023-07-10 thomas goto done;
3909 97a02382 2023-07-10 thomas if (path && path[0] != '\0') {
3910 97a02382 2023-07-10 thomas int obj_type;
3911 97a02382 2023-07-10 thomas err = got_object_id_by_path(&obj_id2, repo, commit, path);
3912 97a02382 2023-07-10 thomas if (err)
3913 97a02382 2023-07-10 thomas goto done;
3914 97a02382 2023-07-10 thomas if (pcommit) {
3915 97a02382 2023-07-10 thomas err = got_object_id_by_path(&obj_id1, repo,
3916 97a02382 2023-07-10 thomas pcommit, path);
3917 97a02382 2023-07-10 thomas if (err) {
3918 97a02382 2023-07-10 thomas if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3919 97a02382 2023-07-10 thomas free(obj_id2);
3920 97a02382 2023-07-10 thomas goto done;
3924 97a02382 2023-07-10 thomas err = got_object_get_type(&obj_type, repo, obj_id2);
3925 97a02382 2023-07-10 thomas if (err) {
3926 97a02382 2023-07-10 thomas free(obj_id2);
3927 97a02382 2023-07-10 thomas goto done;
3929 97a02382 2023-07-10 thomas fprintf(outfile,
3930 97a02382 2023-07-10 thomas "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3931 97a02382 2023-07-10 thomas fprintf(outfile, "commit - %s\n",
3932 97a02382 2023-07-10 thomas id_str1 ? id_str1 : "/dev/null");
3933 97a02382 2023-07-10 thomas fprintf(outfile, "commit + %s\n", id_str2);
3934 97a02382 2023-07-10 thomas switch (obj_type) {
3935 97a02382 2023-07-10 thomas case GOT_OBJ_TYPE_BLOB:
3936 97a02382 2023-07-10 thomas err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3937 97a02382 2023-07-10 thomas 0, 0, dsa, repo, outfile);
3939 97a02382 2023-07-10 thomas case GOT_OBJ_TYPE_TREE:
3940 97a02382 2023-07-10 thomas err = diff_trees(obj_id1, obj_id2, path, diff_context,
3941 97a02382 2023-07-10 thomas 0, 0, dsa, repo, outfile);
3943 97a02382 2023-07-10 thomas default:
3944 97a02382 2023-07-10 thomas err = got_error(GOT_ERR_OBJ_TYPE);
3947 97a02382 2023-07-10 thomas free(obj_id1);
3948 97a02382 2023-07-10 thomas free(obj_id2);
3949 97a02382 2023-07-10 thomas } else {
3950 97a02382 2023-07-10 thomas obj_id2 = got_object_commit_get_tree_id(commit);
3951 97a02382 2023-07-10 thomas if (pcommit)
3952 97a02382 2023-07-10 thomas obj_id1 = got_object_commit_get_tree_id(pcommit);
3953 97a02382 2023-07-10 thomas fprintf(outfile,
3954 97a02382 2023-07-10 thomas "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3955 97a02382 2023-07-10 thomas fprintf(outfile, "commit - %s\n",
3956 97a02382 2023-07-10 thomas id_str1 ? id_str1 : "/dev/null");
3957 97a02382 2023-07-10 thomas fprintf(outfile, "commit + %s\n", id_str2);
3958 97a02382 2023-07-10 thomas err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3959 97a02382 2023-07-10 thomas dsa, repo, outfile);
3962 97a02382 2023-07-10 thomas free(id_str1);
3963 97a02382 2023-07-10 thomas free(id_str2);
3964 97a02382 2023-07-10 thomas if (pcommit)
3965 97a02382 2023-07-10 thomas got_object_commit_close(pcommit);
3966 97a02382 2023-07-10 thomas return err;
3969 97a02382 2023-07-10 thomas static char *
3970 97a02382 2023-07-10 thomas get_datestr(time_t *time, char *datebuf)
3972 97a02382 2023-07-10 thomas struct tm mytm, *tm;
3973 97a02382 2023-07-10 thomas char *p, *s;
3975 97a02382 2023-07-10 thomas tm = gmtime_r(time, &mytm);
3976 97a02382 2023-07-10 thomas if (tm == NULL)
3977 97a02382 2023-07-10 thomas return NULL;
3978 97a02382 2023-07-10 thomas s = asctime_r(tm, datebuf);
3979 97a02382 2023-07-10 thomas if (s == NULL)
3980 97a02382 2023-07-10 thomas return NULL;
3981 97a02382 2023-07-10 thomas p = strchr(s, '\n');
3983 97a02382 2023-07-10 thomas *p = '\0';
3984 97a02382 2023-07-10 thomas return s;
3987 97a02382 2023-07-10 thomas static const struct got_error *
3988 97a02382 2023-07-10 thomas match_commit(int *have_match, struct got_object_id *id,
3989 97a02382 2023-07-10 thomas struct got_commit_object *commit, regex_t *regex)
3991 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
3992 97a02382 2023-07-10 thomas regmatch_t regmatch;
3993 97a02382 2023-07-10 thomas char *id_str = NULL, *logmsg = NULL;
3995 97a02382 2023-07-10 thomas *have_match = 0;
3997 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, id);
3998 97a02382 2023-07-10 thomas if (err)
3999 97a02382 2023-07-10 thomas return err;
4001 97a02382 2023-07-10 thomas err = got_object_commit_get_logmsg(&logmsg, commit);
4002 97a02382 2023-07-10 thomas if (err)
4003 97a02382 2023-07-10 thomas goto done;
4005 97a02382 2023-07-10 thomas if (regexec(regex, got_object_commit_get_author(commit), 1,
4006 97a02382 2023-07-10 thomas ®match, 0) == 0 ||
4007 97a02382 2023-07-10 thomas regexec(regex, got_object_commit_get_committer(commit), 1,
4008 97a02382 2023-07-10 thomas ®match, 0) == 0 ||
4009 97a02382 2023-07-10 thomas regexec(regex, id_str, 1, ®match, 0) == 0 ||
4010 97a02382 2023-07-10 thomas regexec(regex, logmsg, 1, ®match, 0) == 0)
4011 97a02382 2023-07-10 thomas *have_match = 1;
4013 97a02382 2023-07-10 thomas free(id_str);
4014 97a02382 2023-07-10 thomas free(logmsg);
4015 97a02382 2023-07-10 thomas return err;
4018 97a02382 2023-07-10 thomas static void
4019 97a02382 2023-07-10 thomas match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
4020 97a02382 2023-07-10 thomas regex_t *regex)
4022 97a02382 2023-07-10 thomas regmatch_t regmatch;
4023 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
4025 97a02382 2023-07-10 thomas *have_match = 0;
4027 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, changed_paths, entry) {
4028 97a02382 2023-07-10 thomas if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
4029 97a02382 2023-07-10 thomas *have_match = 1;
4035 97a02382 2023-07-10 thomas static const struct got_error *
4036 97a02382 2023-07-10 thomas match_patch(int *have_match, struct got_commit_object *commit,
4037 97a02382 2023-07-10 thomas struct got_object_id *id, const char *path, int diff_context,
4038 97a02382 2023-07-10 thomas struct got_repository *repo, regex_t *regex, FILE *f)
4040 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
4041 97a02382 2023-07-10 thomas char *line = NULL;
4042 97a02382 2023-07-10 thomas size_t linesize = 0;
4043 97a02382 2023-07-10 thomas regmatch_t regmatch;
4045 97a02382 2023-07-10 thomas *have_match = 0;
4047 97a02382 2023-07-10 thomas err = got_opentemp_truncate(f);
4048 97a02382 2023-07-10 thomas if (err)
4049 97a02382 2023-07-10 thomas return err;
4051 97a02382 2023-07-10 thomas err = print_patch(commit, id, path, diff_context, NULL, repo, f);
4052 97a02382 2023-07-10 thomas if (err)
4053 97a02382 2023-07-10 thomas goto done;
4055 97a02382 2023-07-10 thomas if (fseeko(f, 0L, SEEK_SET) == -1) {
4056 97a02382 2023-07-10 thomas err = got_error_from_errno("fseeko");
4057 97a02382 2023-07-10 thomas goto done;
4060 97a02382 2023-07-10 thomas while (getline(&line, &linesize, f) != -1) {
4061 97a02382 2023-07-10 thomas if (regexec(regex, line, 1, ®match, 0) == 0) {
4062 97a02382 2023-07-10 thomas *have_match = 1;
4067 97a02382 2023-07-10 thomas free(line);
4068 97a02382 2023-07-10 thomas return err;
4071 97a02382 2023-07-10 thomas #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
4073 97a02382 2023-07-10 thomas static const struct got_error*
4074 97a02382 2023-07-10 thomas build_refs_str(char **refs_str, struct got_reflist_head *refs,
4075 97a02382 2023-07-10 thomas struct got_object_id *id, struct got_repository *repo,
4076 97a02382 2023-07-10 thomas int local_only)
4078 97a02382 2023-07-10 thomas static const struct got_error *err = NULL;
4079 97a02382 2023-07-10 thomas struct got_reflist_entry *re;
4080 97a02382 2023-07-10 thomas char *s;
4081 97a02382 2023-07-10 thomas const char *name;
4083 97a02382 2023-07-10 thomas *refs_str = NULL;
4085 97a02382 2023-07-10 thomas TAILQ_FOREACH(re, refs, entry) {
4086 97a02382 2023-07-10 thomas struct got_tag_object *tag = NULL;
4087 97a02382 2023-07-10 thomas struct got_object_id *ref_id;
4088 97a02382 2023-07-10 thomas int cmp;
4090 97a02382 2023-07-10 thomas name = got_ref_get_name(re->ref);
4091 97a02382 2023-07-10 thomas if (strcmp(name, GOT_REF_HEAD) == 0)
4092 97a02382 2023-07-10 thomas continue;
4093 97a02382 2023-07-10 thomas if (strncmp(name, "refs/", 5) == 0)
4094 97a02382 2023-07-10 thomas name += 5;
4095 97a02382 2023-07-10 thomas if (strncmp(name, "got/", 4) == 0)
4096 97a02382 2023-07-10 thomas continue;
4097 97a02382 2023-07-10 thomas if (strncmp(name, "heads/", 6) == 0)
4098 97a02382 2023-07-10 thomas name += 6;
4099 97a02382 2023-07-10 thomas if (strncmp(name, "remotes/", 8) == 0) {
4100 97a02382 2023-07-10 thomas if (local_only)
4101 97a02382 2023-07-10 thomas continue;
4102 97a02382 2023-07-10 thomas name += 8;
4103 97a02382 2023-07-10 thomas s = strstr(name, "/" GOT_REF_HEAD);
4104 97a02382 2023-07-10 thomas if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
4105 97a02382 2023-07-10 thomas continue;
4107 97a02382 2023-07-10 thomas err = got_ref_resolve(&ref_id, repo, re->ref);
4108 97a02382 2023-07-10 thomas if (err)
4110 97a02382 2023-07-10 thomas if (strncmp(name, "tags/", 5) == 0) {
4111 97a02382 2023-07-10 thomas err = got_object_open_as_tag(&tag, repo, ref_id);
4112 97a02382 2023-07-10 thomas if (err) {
4113 97a02382 2023-07-10 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
4114 97a02382 2023-07-10 thomas free(ref_id);
4117 97a02382 2023-07-10 thomas /* Ref points at something other than a tag. */
4118 97a02382 2023-07-10 thomas err = NULL;
4119 97a02382 2023-07-10 thomas tag = NULL;
4122 97a02382 2023-07-10 thomas cmp = got_object_id_cmp(tag ?
4123 97a02382 2023-07-10 thomas got_object_tag_get_object_id(tag) : ref_id, id);
4124 97a02382 2023-07-10 thomas free(ref_id);
4125 97a02382 2023-07-10 thomas if (tag)
4126 97a02382 2023-07-10 thomas got_object_tag_close(tag);
4127 97a02382 2023-07-10 thomas if (cmp != 0)
4128 97a02382 2023-07-10 thomas continue;
4129 97a02382 2023-07-10 thomas s = *refs_str;
4130 97a02382 2023-07-10 thomas if (asprintf(refs_str, "%s%s%s", s ? s : "",
4131 97a02382 2023-07-10 thomas s ? ", " : "", name) == -1) {
4132 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
4133 97a02382 2023-07-10 thomas free(s);
4134 97a02382 2023-07-10 thomas *refs_str = NULL;
4137 97a02382 2023-07-10 thomas free(s);
4140 97a02382 2023-07-10 thomas return err;
4143 97a02382 2023-07-10 thomas static const struct got_error *
4144 97a02382 2023-07-10 thomas print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4145 97a02382 2023-07-10 thomas struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4147 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
4148 97a02382 2023-07-10 thomas char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4149 97a02382 2023-07-10 thomas char *comma, *s, *nl;
4150 97a02382 2023-07-10 thomas struct got_reflist_head *refs;
4151 97a02382 2023-07-10 thomas char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4152 97a02382 2023-07-10 thomas struct tm tm;
4153 97a02382 2023-07-10 thomas time_t committer_time;
4155 97a02382 2023-07-10 thomas refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4156 97a02382 2023-07-10 thomas if (refs) {
4157 97a02382 2023-07-10 thomas err = build_refs_str(&ref_str, refs, id, repo, 1);
4158 97a02382 2023-07-10 thomas if (err)
4159 97a02382 2023-07-10 thomas return err;
4161 97a02382 2023-07-10 thomas /* Display the first matching ref only. */
4162 97a02382 2023-07-10 thomas if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4163 97a02382 2023-07-10 thomas *comma = '\0';
4166 97a02382 2023-07-10 thomas if (ref_str == NULL) {
4167 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, id);
4168 97a02382 2023-07-10 thomas if (err)
4169 97a02382 2023-07-10 thomas return err;
4172 97a02382 2023-07-10 thomas committer_time = got_object_commit_get_committer_time(commit);
4173 97a02382 2023-07-10 thomas if (gmtime_r(&committer_time, &tm) == NULL) {
4174 97a02382 2023-07-10 thomas err = got_error_from_errno("gmtime_r");
4175 97a02382 2023-07-10 thomas goto done;
4177 97a02382 2023-07-10 thomas if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4178 97a02382 2023-07-10 thomas err = got_error(GOT_ERR_NO_SPACE);
4179 97a02382 2023-07-10 thomas goto done;
4182 97a02382 2023-07-10 thomas err = got_object_commit_get_logmsg(&logmsg0, commit);
4183 97a02382 2023-07-10 thomas if (err)
4184 97a02382 2023-07-10 thomas goto done;
4186 97a02382 2023-07-10 thomas s = logmsg0;
4187 97a02382 2023-07-10 thomas while (isspace((unsigned char)s[0]))
4190 97a02382 2023-07-10 thomas nl = strchr(s, '\n');
4191 97a02382 2023-07-10 thomas if (nl) {
4192 97a02382 2023-07-10 thomas *nl = '\0';
4195 97a02382 2023-07-10 thomas if (ref_str)
4196 97a02382 2023-07-10 thomas printf("%s%-7s %s\n", datebuf, ref_str, s);
4198 97a02382 2023-07-10 thomas printf("%s%.7s %s\n", datebuf, id_str, s);
4200 97a02382 2023-07-10 thomas if (fflush(stdout) != 0 && err == NULL)
4201 97a02382 2023-07-10 thomas err = got_error_from_errno("fflush");
4203 97a02382 2023-07-10 thomas free(id_str);
4204 97a02382 2023-07-10 thomas free(ref_str);
4205 97a02382 2023-07-10 thomas free(logmsg0);
4206 97a02382 2023-07-10 thomas return err;
4209 97a02382 2023-07-10 thomas static const struct got_error *
4210 97a02382 2023-07-10 thomas print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
4212 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
4214 97a02382 2023-07-10 thomas if (header != NULL)
4215 97a02382 2023-07-10 thomas printf("%s\n", header);
4217 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, dsa->paths, entry) {
4218 97a02382 2023-07-10 thomas struct got_diff_changed_path *cp = pe->data;
4219 97a02382 2023-07-10 thomas int pad = dsa->max_path_len - pe->path_len + 1;
4221 97a02382 2023-07-10 thomas printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
4222 97a02382 2023-07-10 thomas ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
4224 97a02382 2023-07-10 thomas printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
4225 97a02382 2023-07-10 thomas dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4226 97a02382 2023-07-10 thomas dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4228 97a02382 2023-07-10 thomas if (fflush(stdout) != 0)
4229 97a02382 2023-07-10 thomas return got_error_from_errno("fflush");
4231 97a02382 2023-07-10 thomas return NULL;
4234 97a02382 2023-07-10 thomas static const struct got_error *
4235 97a02382 2023-07-10 thomas printfile(FILE *f)
4237 97a02382 2023-07-10 thomas char buf[8192];
4238 97a02382 2023-07-10 thomas size_t r;
4240 97a02382 2023-07-10 thomas if (fseeko(f, 0L, SEEK_SET) == -1)
4241 97a02382 2023-07-10 thomas return got_error_from_errno("fseek");
4243 97a02382 2023-07-10 thomas for (;;) {
4244 97a02382 2023-07-10 thomas r = fread(buf, 1, sizeof(buf), f);
4245 97a02382 2023-07-10 thomas if (r == 0) {
4246 97a02382 2023-07-10 thomas if (ferror(f))
4247 97a02382 2023-07-10 thomas return got_error_from_errno("fread");
4248 97a02382 2023-07-10 thomas if (feof(f))
4251 97a02382 2023-07-10 thomas if (fwrite(buf, 1, r, stdout) != r)
4252 97a02382 2023-07-10 thomas return got_ferror(stdout, GOT_ERR_IO);
4255 97a02382 2023-07-10 thomas return NULL;
4258 97a02382 2023-07-10 thomas static const struct got_error *
4259 97a02382 2023-07-10 thomas print_commit(struct got_commit_object *commit, struct got_object_id *id,
4260 97a02382 2023-07-10 thomas struct got_repository *repo, const char *path,
4261 97a02382 2023-07-10 thomas struct got_pathlist_head *changed_paths,
4262 97a02382 2023-07-10 thomas struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
4263 97a02382 2023-07-10 thomas struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str,
4264 97a02382 2023-07-10 thomas const char *prefix)
4266 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
4267 97a02382 2023-07-10 thomas FILE *f = NULL;
4268 97a02382 2023-07-10 thomas char *id_str, *datestr, *logmsg0, *logmsg, *line;
4269 97a02382 2023-07-10 thomas char datebuf[26];
4270 97a02382 2023-07-10 thomas time_t committer_time;
4271 97a02382 2023-07-10 thomas const char *author, *committer;
4272 97a02382 2023-07-10 thomas char *refs_str = NULL;
4274 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, id);
4275 97a02382 2023-07-10 thomas if (err)
4276 97a02382 2023-07-10 thomas return err;
4278 97a02382 2023-07-10 thomas if (custom_refs_str == NULL) {
4279 97a02382 2023-07-10 thomas struct got_reflist_head *refs;
4280 97a02382 2023-07-10 thomas refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4281 97a02382 2023-07-10 thomas if (refs) {
4282 97a02382 2023-07-10 thomas err = build_refs_str(&refs_str, refs, id, repo, 0);
4283 97a02382 2023-07-10 thomas if (err)
4284 97a02382 2023-07-10 thomas goto done;
4288 97a02382 2023-07-10 thomas printf(GOT_COMMIT_SEP_STR);
4289 97a02382 2023-07-10 thomas if (custom_refs_str)
4290 97a02382 2023-07-10 thomas printf("%s %s (%s)\n", prefix ? prefix : "commit", id_str,
4291 97a02382 2023-07-10 thomas custom_refs_str);
4293 97a02382 2023-07-10 thomas printf("%s %s%s%s%s\n", prefix ? prefix : "commit", id_str,
4294 97a02382 2023-07-10 thomas refs_str ? " (" : "", refs_str ? refs_str : "",
4295 97a02382 2023-07-10 thomas refs_str ? ")" : "");
4296 97a02382 2023-07-10 thomas free(id_str);
4297 97a02382 2023-07-10 thomas id_str = NULL;
4298 97a02382 2023-07-10 thomas free(refs_str);
4299 97a02382 2023-07-10 thomas refs_str = NULL;
4300 97a02382 2023-07-10 thomas printf("from: %s\n", got_object_commit_get_author(commit));
4301 97a02382 2023-07-10 thomas author = got_object_commit_get_author(commit);
4302 97a02382 2023-07-10 thomas committer = got_object_commit_get_committer(commit);
4303 97a02382 2023-07-10 thomas if (strcmp(author, committer) != 0)
4304 97a02382 2023-07-10 thomas printf("via: %s\n", committer);
4305 97a02382 2023-07-10 thomas committer_time = got_object_commit_get_committer_time(commit);
4306 97a02382 2023-07-10 thomas datestr = get_datestr(&committer_time, datebuf);
4307 97a02382 2023-07-10 thomas if (datestr)
4308 97a02382 2023-07-10 thomas printf("date: %s UTC\n", datestr);
4309 97a02382 2023-07-10 thomas if (got_object_commit_get_nparents(commit) > 1) {
4310 97a02382 2023-07-10 thomas const struct got_object_id_queue *parent_ids;
4311 97a02382 2023-07-10 thomas struct got_object_qid *qid;
4312 97a02382 2023-07-10 thomas int n = 1;
4313 97a02382 2023-07-10 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4314 97a02382 2023-07-10 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4315 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, &qid->id);
4316 97a02382 2023-07-10 thomas if (err)
4317 97a02382 2023-07-10 thomas goto done;
4318 97a02382 2023-07-10 thomas printf("parent %d: %s\n", n++, id_str);
4319 97a02382 2023-07-10 thomas free(id_str);
4320 97a02382 2023-07-10 thomas id_str = NULL;
4324 97a02382 2023-07-10 thomas err = got_object_commit_get_logmsg(&logmsg0, commit);
4325 97a02382 2023-07-10 thomas if (err)
4326 97a02382 2023-07-10 thomas goto done;
4328 97a02382 2023-07-10 thomas logmsg = logmsg0;
4330 97a02382 2023-07-10 thomas line = strsep(&logmsg, "\n");
4331 97a02382 2023-07-10 thomas if (line)
4332 97a02382 2023-07-10 thomas printf(" %s\n", line);
4333 97a02382 2023-07-10 thomas } while (line);
4334 97a02382 2023-07-10 thomas free(logmsg0);
4336 97a02382 2023-07-10 thomas if (changed_paths && diffstat == NULL) {
4337 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
4339 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, changed_paths, entry) {
4340 97a02382 2023-07-10 thomas struct got_diff_changed_path *cp = pe->data;
4342 97a02382 2023-07-10 thomas printf(" %c %s\n", cp->status, pe->path);
4344 97a02382 2023-07-10 thomas printf("\n");
4346 97a02382 2023-07-10 thomas if (show_patch) {
4347 97a02382 2023-07-10 thomas if (diffstat) {
4348 97a02382 2023-07-10 thomas f = got_opentemp();
4349 97a02382 2023-07-10 thomas if (f == NULL) {
4350 97a02382 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
4351 97a02382 2023-07-10 thomas goto done;
4355 97a02382 2023-07-10 thomas err = print_patch(commit, id, path, diff_context, diffstat,
4356 97a02382 2023-07-10 thomas repo, diffstat == NULL ? stdout : f);
4357 97a02382 2023-07-10 thomas if (err)
4358 97a02382 2023-07-10 thomas goto done;
4360 97a02382 2023-07-10 thomas if (diffstat) {
4361 97a02382 2023-07-10 thomas err = print_diffstat(diffstat, NULL);
4362 97a02382 2023-07-10 thomas if (err)
4363 97a02382 2023-07-10 thomas goto done;
4364 97a02382 2023-07-10 thomas if (show_patch) {
4365 97a02382 2023-07-10 thomas err = printfile(f);
4366 97a02382 2023-07-10 thomas if (err)
4367 97a02382 2023-07-10 thomas goto done;
4370 97a02382 2023-07-10 thomas if (show_patch)
4371 97a02382 2023-07-10 thomas printf("\n");
4373 97a02382 2023-07-10 thomas if (fflush(stdout) != 0 && err == NULL)
4374 97a02382 2023-07-10 thomas err = got_error_from_errno("fflush");
4376 97a02382 2023-07-10 thomas if (f && fclose(f) == EOF && err == NULL)
4377 97a02382 2023-07-10 thomas err = got_error_from_errno("fclose");
4378 97a02382 2023-07-10 thomas free(id_str);
4379 97a02382 2023-07-10 thomas free(refs_str);
4380 97a02382 2023-07-10 thomas return err;
4383 97a02382 2023-07-10 thomas static const struct got_error *
4384 97a02382 2023-07-10 thomas print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4385 97a02382 2023-07-10 thomas struct got_repository *repo, const char *path, int show_changed_paths,
4386 97a02382 2023-07-10 thomas int show_diffstat, int show_patch, const char *search_pattern,
4387 97a02382 2023-07-10 thomas int diff_context, int limit, int log_branches, int reverse_display_order,
4388 97a02382 2023-07-10 thomas struct got_reflist_object_id_map *refs_idmap, int one_line,
4389 97a02382 2023-07-10 thomas FILE *tmpfile)
4391 97a02382 2023-07-10 thomas const struct got_error *err;
4392 97a02382 2023-07-10 thomas struct got_commit_graph *graph;
4393 97a02382 2023-07-10 thomas regex_t regex;
4394 97a02382 2023-07-10 thomas int have_match;
4395 97a02382 2023-07-10 thomas struct got_object_id_queue reversed_commits;
4396 97a02382 2023-07-10 thomas struct got_object_qid *qid;
4397 97a02382 2023-07-10 thomas struct got_commit_object *commit;
4398 97a02382 2023-07-10 thomas struct got_pathlist_head changed_paths;
4400 97a02382 2023-07-10 thomas STAILQ_INIT(&reversed_commits);
4401 97a02382 2023-07-10 thomas TAILQ_INIT(&changed_paths);
4403 97a02382 2023-07-10 thomas if (search_pattern && regcomp(®ex, search_pattern,
4404 97a02382 2023-07-10 thomas REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4405 97a02382 2023-07-10 thomas return got_error_msg(GOT_ERR_REGEX, search_pattern);
4407 97a02382 2023-07-10 thomas err = got_commit_graph_open(&graph, path, !log_branches);
4408 97a02382 2023-07-10 thomas if (err)
4409 97a02382 2023-07-10 thomas return err;
4410 97a02382 2023-07-10 thomas err = got_commit_graph_iter_start(graph, root_id, repo,
4411 97a02382 2023-07-10 thomas check_cancelled, NULL);
4412 97a02382 2023-07-10 thomas if (err)
4413 97a02382 2023-07-10 thomas goto done;
4414 97a02382 2023-07-10 thomas for (;;) {
4415 97a02382 2023-07-10 thomas struct got_object_id id;
4416 97a02382 2023-07-10 thomas struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4417 97a02382 2023-07-10 thomas &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4419 97a02382 2023-07-10 thomas if (sigint_received || sigpipe_received)
4422 97a02382 2023-07-10 thomas err = got_commit_graph_iter_next(&id, graph, repo,
4423 97a02382 2023-07-10 thomas check_cancelled, NULL);
4424 97a02382 2023-07-10 thomas if (err) {
4425 97a02382 2023-07-10 thomas if (err->code == GOT_ERR_ITER_COMPLETED)
4426 97a02382 2023-07-10 thomas err = NULL;
4430 97a02382 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, &id);
4431 97a02382 2023-07-10 thomas if (err)
4434 97a02382 2023-07-10 thomas if ((show_changed_paths || (show_diffstat && !show_patch))
4435 97a02382 2023-07-10 thomas && !reverse_display_order) {
4436 97a02382 2023-07-10 thomas err = get_changed_paths(&changed_paths, commit, repo,
4437 97a02382 2023-07-10 thomas show_diffstat ? &dsa : NULL);
4438 97a02382 2023-07-10 thomas if (err)
4442 97a02382 2023-07-10 thomas if (search_pattern) {
4443 97a02382 2023-07-10 thomas err = match_commit(&have_match, &id, commit, ®ex);
4444 97a02382 2023-07-10 thomas if (err) {
4445 97a02382 2023-07-10 thomas got_object_commit_close(commit);
4448 97a02382 2023-07-10 thomas if (have_match == 0 && show_changed_paths)
4449 97a02382 2023-07-10 thomas match_changed_paths(&have_match,
4450 97a02382 2023-07-10 thomas &changed_paths, ®ex);
4451 97a02382 2023-07-10 thomas if (have_match == 0 && show_patch) {
4452 97a02382 2023-07-10 thomas err = match_patch(&have_match, commit, &id,
4453 97a02382 2023-07-10 thomas path, diff_context, repo, ®ex, tmpfile);
4454 97a02382 2023-07-10 thomas if (err)
4457 97a02382 2023-07-10 thomas if (have_match == 0) {
4458 97a02382 2023-07-10 thomas got_object_commit_close(commit);
4459 97a02382 2023-07-10 thomas got_pathlist_free(&changed_paths,
4460 97a02382 2023-07-10 thomas GOT_PATHLIST_FREE_ALL);
4461 97a02382 2023-07-10 thomas continue;
4465 97a02382 2023-07-10 thomas if (reverse_display_order) {
4466 97a02382 2023-07-10 thomas err = got_object_qid_alloc(&qid, &id);
4467 97a02382 2023-07-10 thomas if (err)
4469 97a02382 2023-07-10 thomas STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4470 97a02382 2023-07-10 thomas got_object_commit_close(commit);
4471 97a02382 2023-07-10 thomas } else {
4472 97a02382 2023-07-10 thomas if (one_line)
4473 97a02382 2023-07-10 thomas err = print_commit_oneline(commit, &id,
4474 97a02382 2023-07-10 thomas repo, refs_idmap);
4476 97a02382 2023-07-10 thomas err = print_commit(commit, &id, repo, path,
4477 97a02382 2023-07-10 thomas (show_changed_paths || show_diffstat) ?
4478 97a02382 2023-07-10 thomas &changed_paths : NULL,
4479 97a02382 2023-07-10 thomas show_diffstat ? &dsa : NULL, show_patch,
4480 97a02382 2023-07-10 thomas diff_context, refs_idmap, NULL, NULL);
4481 97a02382 2023-07-10 thomas got_object_commit_close(commit);
4482 97a02382 2023-07-10 thomas if (err)
4485 97a02382 2023-07-10 thomas if ((limit && --limit == 0) ||
4486 97a02382 2023-07-10 thomas (end_id && got_object_id_cmp(&id, end_id) == 0))
4489 97a02382 2023-07-10 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4491 97a02382 2023-07-10 thomas if (reverse_display_order) {
4492 97a02382 2023-07-10 thomas STAILQ_FOREACH(qid, &reversed_commits, entry) {
4493 97a02382 2023-07-10 thomas struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4494 97a02382 2023-07-10 thomas &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4496 97a02382 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo,
4497 97a02382 2023-07-10 thomas &qid->id);
4498 97a02382 2023-07-10 thomas if (err)
4500 97a02382 2023-07-10 thomas if (show_changed_paths ||
4501 97a02382 2023-07-10 thomas (show_diffstat && !show_patch)) {
4502 97a02382 2023-07-10 thomas err = get_changed_paths(&changed_paths, commit,
4503 97a02382 2023-07-10 thomas repo, show_diffstat ? &dsa : NULL);
4504 97a02382 2023-07-10 thomas if (err)
4507 97a02382 2023-07-10 thomas if (one_line)
4508 97a02382 2023-07-10 thomas err = print_commit_oneline(commit, &qid->id,
4509 97a02382 2023-07-10 thomas repo, refs_idmap);
4511 97a02382 2023-07-10 thomas err = print_commit(commit, &qid->id, repo, path,
4512 97a02382 2023-07-10 thomas (show_changed_paths || show_diffstat) ?
4513 97a02382 2023-07-10 thomas &changed_paths : NULL,
4514 97a02382 2023-07-10 thomas show_diffstat ? &dsa : NULL, show_patch,
4515 97a02382 2023-07-10 thomas diff_context, refs_idmap, NULL, NULL);
4516 97a02382 2023-07-10 thomas got_object_commit_close(commit);
4517 97a02382 2023-07-10 thomas if (err)
4519 97a02382 2023-07-10 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4523 97a02382 2023-07-10 thomas while (!STAILQ_EMPTY(&reversed_commits)) {
4524 97a02382 2023-07-10 thomas qid = STAILQ_FIRST(&reversed_commits);
4525 97a02382 2023-07-10 thomas STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4526 97a02382 2023-07-10 thomas got_object_qid_free(qid);
4528 97a02382 2023-07-10 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4529 97a02382 2023-07-10 thomas if (search_pattern)
4530 97a02382 2023-07-10 thomas regfree(®ex);
4531 97a02382 2023-07-10 thomas got_commit_graph_close(graph);
4532 97a02382 2023-07-10 thomas return err;
4535 97a02382 2023-07-10 thomas __dead static void
4536 97a02382 2023-07-10 thomas usage_log(void)
4538 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s log [-bdPpRs] [-C number] [-c commit] "
4539 97a02382 2023-07-10 thomas "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
4540 97a02382 2023-07-10 thomas "[path]\n", getprogname());
4541 97a02382 2023-07-10 thomas exit(1);
4544 97a02382 2023-07-10 thomas static int
4545 97a02382 2023-07-10 thomas get_default_log_limit(void)
4547 97a02382 2023-07-10 thomas const char *got_default_log_limit;
4548 97a02382 2023-07-10 thomas long long n;
4549 97a02382 2023-07-10 thomas const char *errstr;
4551 97a02382 2023-07-10 thomas got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4552 97a02382 2023-07-10 thomas if (got_default_log_limit == NULL)
4553 97a02382 2023-07-10 thomas return 0;
4554 97a02382 2023-07-10 thomas n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4555 97a02382 2023-07-10 thomas if (errstr != NULL)
4556 97a02382 2023-07-10 thomas return 0;
4557 97a02382 2023-07-10 thomas return n;
4560 97a02382 2023-07-10 thomas static const struct got_error *
4561 97a02382 2023-07-10 thomas cmd_log(int argc, char *argv[])
4563 97a02382 2023-07-10 thomas const struct got_error *error;
4564 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
4565 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
4566 97a02382 2023-07-10 thomas struct got_object_id *start_id = NULL, *end_id = NULL;
4567 97a02382 2023-07-10 thomas char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4568 97a02382 2023-07-10 thomas const char *start_commit = NULL, *end_commit = NULL;
4569 97a02382 2023-07-10 thomas const char *search_pattern = NULL;
4570 97a02382 2023-07-10 thomas int diff_context = -1, ch;
4571 97a02382 2023-07-10 thomas int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4572 97a02382 2023-07-10 thomas int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
4573 97a02382 2023-07-10 thomas const char *errstr;
4574 97a02382 2023-07-10 thomas struct got_reflist_head refs;
4575 97a02382 2023-07-10 thomas struct got_reflist_object_id_map *refs_idmap = NULL;
4576 97a02382 2023-07-10 thomas FILE *tmpfile = NULL;
4577 97a02382 2023-07-10 thomas int *pack_fds = NULL;
4579 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
4581 97a02382 2023-07-10 thomas #ifndef PROFILE
4582 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4585 97a02382 2023-07-10 thomas err(1, "pledge");
4588 97a02382 2023-07-10 thomas limit = get_default_log_limit();
4590 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:sx:")) != -1) {
4591 97a02382 2023-07-10 thomas switch (ch) {
4592 97a02382 2023-07-10 thomas case 'b':
4593 97a02382 2023-07-10 thomas log_branches = 1;
4595 97a02382 2023-07-10 thomas case 'C':
4596 97a02382 2023-07-10 thomas diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4597 97a02382 2023-07-10 thomas &errstr);
4598 97a02382 2023-07-10 thomas if (errstr != NULL)
4599 97a02382 2023-07-10 thomas errx(1, "number of context lines is %s: %s",
4600 97a02382 2023-07-10 thomas errstr, optarg);
4602 97a02382 2023-07-10 thomas case 'c':
4603 97a02382 2023-07-10 thomas start_commit = optarg;
4605 97a02382 2023-07-10 thomas case 'd':
4606 97a02382 2023-07-10 thomas show_diffstat = 1;
4608 97a02382 2023-07-10 thomas case 'l':
4609 97a02382 2023-07-10 thomas limit = strtonum(optarg, 0, INT_MAX, &errstr);
4610 97a02382 2023-07-10 thomas if (errstr != NULL)
4611 97a02382 2023-07-10 thomas errx(1, "number of commits is %s: %s",
4612 97a02382 2023-07-10 thomas errstr, optarg);
4614 97a02382 2023-07-10 thomas case 'P':
4615 97a02382 2023-07-10 thomas show_changed_paths = 1;
4617 97a02382 2023-07-10 thomas case 'p':
4618 97a02382 2023-07-10 thomas show_patch = 1;
4620 97a02382 2023-07-10 thomas case 'R':
4621 97a02382 2023-07-10 thomas reverse_display_order = 1;
4623 97a02382 2023-07-10 thomas case 'r':
4624 97a02382 2023-07-10 thomas repo_path = realpath(optarg, NULL);
4625 97a02382 2023-07-10 thomas if (repo_path == NULL)
4626 97a02382 2023-07-10 thomas return got_error_from_errno2("realpath",
4627 97a02382 2023-07-10 thomas optarg);
4628 97a02382 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
4630 97a02382 2023-07-10 thomas case 'S':
4631 97a02382 2023-07-10 thomas search_pattern = optarg;
4633 97a02382 2023-07-10 thomas case 's':
4634 97a02382 2023-07-10 thomas one_line = 1;
4636 97a02382 2023-07-10 thomas case 'x':
4637 97a02382 2023-07-10 thomas end_commit = optarg;
4639 97a02382 2023-07-10 thomas default:
4640 97a02382 2023-07-10 thomas usage_log();
4641 97a02382 2023-07-10 thomas /* NOTREACHED */
4645 97a02382 2023-07-10 thomas argc -= optind;
4646 97a02382 2023-07-10 thomas argv += optind;
4648 97a02382 2023-07-10 thomas if (diff_context == -1)
4649 97a02382 2023-07-10 thomas diff_context = 3;
4650 97a02382 2023-07-10 thomas else if (!show_patch)
4651 97a02382 2023-07-10 thomas errx(1, "-C requires -p");
4653 97a02382 2023-07-10 thomas if (one_line && (show_patch || show_changed_paths || show_diffstat))
4654 97a02382 2023-07-10 thomas errx(1, "cannot use -s with -d, -p or -P");
4656 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
4657 97a02382 2023-07-10 thomas if (cwd == NULL) {
4658 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
4659 97a02382 2023-07-10 thomas goto done;
4662 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
4663 97a02382 2023-07-10 thomas if (error != NULL)
4664 97a02382 2023-07-10 thomas goto done;
4666 97a02382 2023-07-10 thomas if (repo_path == NULL) {
4667 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
4668 97a02382 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
4669 97a02382 2023-07-10 thomas goto done;
4670 97a02382 2023-07-10 thomas error = NULL;
4673 97a02382 2023-07-10 thomas if (argc == 1) {
4674 97a02382 2023-07-10 thomas if (worktree) {
4675 97a02382 2023-07-10 thomas error = got_worktree_resolve_path(&path, worktree,
4676 97a02382 2023-07-10 thomas argv[0]);
4677 97a02382 2023-07-10 thomas if (error)
4678 97a02382 2023-07-10 thomas goto done;
4679 97a02382 2023-07-10 thomas } else {
4680 97a02382 2023-07-10 thomas path = strdup(argv[0]);
4681 97a02382 2023-07-10 thomas if (path == NULL) {
4682 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
4683 97a02382 2023-07-10 thomas goto done;
4686 97a02382 2023-07-10 thomas } else if (argc != 0)
4687 97a02382 2023-07-10 thomas usage_log();
4689 97a02382 2023-07-10 thomas if (repo_path == NULL) {
4690 97a02382 2023-07-10 thomas repo_path = worktree ?
4691 97a02382 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4693 97a02382 2023-07-10 thomas if (repo_path == NULL) {
4694 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
4695 97a02382 2023-07-10 thomas goto done;
4698 97a02382 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4699 97a02382 2023-07-10 thomas if (error != NULL)
4700 97a02382 2023-07-10 thomas goto done;
4702 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1,
4703 97a02382 2023-07-10 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
4704 97a02382 2023-07-10 thomas if (error)
4705 97a02382 2023-07-10 thomas goto done;
4707 97a02382 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4708 97a02382 2023-07-10 thomas if (error)
4709 97a02382 2023-07-10 thomas goto done;
4711 97a02382 2023-07-10 thomas error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4712 97a02382 2023-07-10 thomas if (error)
4713 97a02382 2023-07-10 thomas goto done;
4715 97a02382 2023-07-10 thomas if (start_commit == NULL) {
4716 97a02382 2023-07-10 thomas struct got_reference *head_ref;
4717 97a02382 2023-07-10 thomas struct got_commit_object *commit = NULL;
4718 97a02382 2023-07-10 thomas error = got_ref_open(&head_ref, repo,
4719 97a02382 2023-07-10 thomas worktree ? got_worktree_get_head_ref_name(worktree)
4720 97a02382 2023-07-10 thomas : GOT_REF_HEAD, 0);
4721 97a02382 2023-07-10 thomas if (error != NULL)
4722 97a02382 2023-07-10 thomas goto done;
4723 97a02382 2023-07-10 thomas error = got_ref_resolve(&start_id, repo, head_ref);
4724 97a02382 2023-07-10 thomas got_ref_close(head_ref);
4725 97a02382 2023-07-10 thomas if (error != NULL)
4726 97a02382 2023-07-10 thomas goto done;
4727 97a02382 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo,
4728 97a02382 2023-07-10 thomas start_id);
4729 97a02382 2023-07-10 thomas if (error != NULL)
4730 97a02382 2023-07-10 thomas goto done;
4731 97a02382 2023-07-10 thomas got_object_commit_close(commit);
4732 97a02382 2023-07-10 thomas } else {
4733 97a02382 2023-07-10 thomas error = got_repo_match_object_id(&start_id, NULL,
4734 97a02382 2023-07-10 thomas start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4735 97a02382 2023-07-10 thomas if (error != NULL)
4736 97a02382 2023-07-10 thomas goto done;
4738 97a02382 2023-07-10 thomas if (end_commit != NULL) {
4739 97a02382 2023-07-10 thomas error = got_repo_match_object_id(&end_id, NULL,
4740 97a02382 2023-07-10 thomas end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4741 97a02382 2023-07-10 thomas if (error != NULL)
4742 97a02382 2023-07-10 thomas goto done;
4745 97a02382 2023-07-10 thomas if (worktree) {
4747 97a02382 2023-07-10 thomas * If a path was specified on the command line it was resolved
4748 97a02382 2023-07-10 thomas * to a path in the work tree above. Prepend the work tree's
4749 97a02382 2023-07-10 thomas * path prefix to obtain the corresponding in-repository path.
4751 97a02382 2023-07-10 thomas if (path) {
4752 97a02382 2023-07-10 thomas const char *prefix;
4753 97a02382 2023-07-10 thomas prefix = got_worktree_get_path_prefix(worktree);
4754 97a02382 2023-07-10 thomas if (asprintf(&in_repo_path, "%s%s%s", prefix,
4755 97a02382 2023-07-10 thomas (path[0] != '\0') ? "/" : "", path) == -1) {
4756 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
4757 97a02382 2023-07-10 thomas goto done;
4761 97a02382 2023-07-10 thomas error = got_repo_map_path(&in_repo_path, repo,
4762 97a02382 2023-07-10 thomas path ? path : "");
4763 97a02382 2023-07-10 thomas if (error != NULL)
4764 97a02382 2023-07-10 thomas goto done;
4765 97a02382 2023-07-10 thomas if (in_repo_path) {
4766 97a02382 2023-07-10 thomas free(path);
4767 97a02382 2023-07-10 thomas path = in_repo_path;
4770 97a02382 2023-07-10 thomas if (worktree) {
4771 97a02382 2023-07-10 thomas /* Release work tree lock. */
4772 97a02382 2023-07-10 thomas got_worktree_close(worktree);
4773 97a02382 2023-07-10 thomas worktree = NULL;
4776 97a02382 2023-07-10 thomas if (search_pattern && show_patch) {
4777 97a02382 2023-07-10 thomas tmpfile = got_opentemp();
4778 97a02382 2023-07-10 thomas if (tmpfile == NULL) {
4779 97a02382 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
4780 97a02382 2023-07-10 thomas goto done;
4784 97a02382 2023-07-10 thomas error = print_commits(start_id, end_id, repo, path ? path : "",
4785 97a02382 2023-07-10 thomas show_changed_paths, show_diffstat, show_patch, search_pattern,
4786 97a02382 2023-07-10 thomas diff_context, limit, log_branches, reverse_display_order,
4787 97a02382 2023-07-10 thomas refs_idmap, one_line, tmpfile);
4789 97a02382 2023-07-10 thomas free(path);
4790 97a02382 2023-07-10 thomas free(repo_path);
4791 97a02382 2023-07-10 thomas free(cwd);
4792 97a02382 2023-07-10 thomas if (worktree)
4793 97a02382 2023-07-10 thomas got_worktree_close(worktree);
4794 97a02382 2023-07-10 thomas if (repo) {
4795 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
4796 97a02382 2023-07-10 thomas if (error == NULL)
4797 97a02382 2023-07-10 thomas error = close_err;
4799 97a02382 2023-07-10 thomas if (pack_fds) {
4800 97a02382 2023-07-10 thomas const struct got_error *pack_err =
4801 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
4802 97a02382 2023-07-10 thomas if (error == NULL)
4803 97a02382 2023-07-10 thomas error = pack_err;
4805 97a02382 2023-07-10 thomas if (refs_idmap)
4806 97a02382 2023-07-10 thomas got_reflist_object_id_map_free(refs_idmap);
4807 97a02382 2023-07-10 thomas if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4808 97a02382 2023-07-10 thomas error = got_error_from_errno("fclose");
4809 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
4810 97a02382 2023-07-10 thomas return error;
4813 97a02382 2023-07-10 thomas __dead static void
4814 97a02382 2023-07-10 thomas usage_diff(void)
4816 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4817 97a02382 2023-07-10 thomas "[-r repository-path] [object1 object2 | path ...]\n",
4818 97a02382 2023-07-10 thomas getprogname());
4819 97a02382 2023-07-10 thomas exit(1);
4822 97a02382 2023-07-10 thomas struct print_diff_arg {
4823 97a02382 2023-07-10 thomas struct got_repository *repo;
4824 97a02382 2023-07-10 thomas struct got_worktree *worktree;
4825 97a02382 2023-07-10 thomas struct got_diffstat_cb_arg *diffstat;
4826 97a02382 2023-07-10 thomas int diff_context;
4827 97a02382 2023-07-10 thomas const char *id_str;
4828 97a02382 2023-07-10 thomas int header_shown;
4829 97a02382 2023-07-10 thomas int diff_staged;
4830 97a02382 2023-07-10 thomas enum got_diff_algorithm diff_algo;
4831 97a02382 2023-07-10 thomas int ignore_whitespace;
4832 97a02382 2023-07-10 thomas int force_text_diff;
4833 97a02382 2023-07-10 thomas FILE *f1;
4834 97a02382 2023-07-10 thomas FILE *f2;
4835 97a02382 2023-07-10 thomas FILE *outfile;
4839 97a02382 2023-07-10 thomas * Create a file which contains the target path of a symlink so we can feed
4840 97a02382 2023-07-10 thomas * it as content to the diff engine.
4842 97a02382 2023-07-10 thomas static const struct got_error *
4843 97a02382 2023-07-10 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4844 97a02382 2023-07-10 thomas const char *abspath)
4846 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
4847 97a02382 2023-07-10 thomas char target_path[PATH_MAX];
4848 97a02382 2023-07-10 thomas ssize_t target_len, outlen;
4850 97a02382 2023-07-10 thomas *fd = -1;
4852 97a02382 2023-07-10 thomas if (dirfd != -1) {
4853 97a02382 2023-07-10 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4854 97a02382 2023-07-10 thomas if (target_len == -1)
4855 97a02382 2023-07-10 thomas return got_error_from_errno2("readlinkat", abspath);
4856 97a02382 2023-07-10 thomas } else {
4857 97a02382 2023-07-10 thomas target_len = readlink(abspath, target_path, PATH_MAX);
4858 97a02382 2023-07-10 thomas if (target_len == -1)
4859 97a02382 2023-07-10 thomas return got_error_from_errno2("readlink", abspath);
4862 97a02382 2023-07-10 thomas *fd = got_opentempfd();
4863 97a02382 2023-07-10 thomas if (*fd == -1)
4864 97a02382 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
4866 97a02382 2023-07-10 thomas outlen = write(*fd, target_path, target_len);
4867 97a02382 2023-07-10 thomas if (outlen == -1) {
4868 97a02382 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
4869 97a02382 2023-07-10 thomas goto done;
4872 97a02382 2023-07-10 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
4873 97a02382 2023-07-10 thomas err = got_error_from_errno2("lseek", abspath);
4874 97a02382 2023-07-10 thomas goto done;
4877 97a02382 2023-07-10 thomas if (err) {
4878 97a02382 2023-07-10 thomas close(*fd);
4879 97a02382 2023-07-10 thomas *fd = -1;
4881 97a02382 2023-07-10 thomas return err;
4884 97a02382 2023-07-10 thomas static const struct got_error *
4885 97a02382 2023-07-10 thomas print_diff(void *arg, unsigned char status, unsigned char staged_status,
4886 97a02382 2023-07-10 thomas const char *path, struct got_object_id *blob_id,
4887 97a02382 2023-07-10 thomas struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4888 97a02382 2023-07-10 thomas int dirfd, const char *de_name)
4890 97a02382 2023-07-10 thomas struct print_diff_arg *a = arg;
4891 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
4892 97a02382 2023-07-10 thomas struct got_blob_object *blob1 = NULL;
4893 97a02382 2023-07-10 thomas int fd = -1, fd1 = -1, fd2 = -1;
4894 97a02382 2023-07-10 thomas FILE *f2 = NULL;
4895 97a02382 2023-07-10 thomas char *abspath = NULL, *label1 = NULL;
4896 97a02382 2023-07-10 thomas struct stat sb;
4897 97a02382 2023-07-10 thomas off_t size1 = 0;
4898 97a02382 2023-07-10 thomas int f2_exists = 0;
4900 97a02382 2023-07-10 thomas memset(&sb, 0, sizeof(sb));
4902 97a02382 2023-07-10 thomas if (a->diff_staged) {
4903 97a02382 2023-07-10 thomas if (staged_status != GOT_STATUS_MODIFY &&
4904 97a02382 2023-07-10 thomas staged_status != GOT_STATUS_ADD &&
4905 97a02382 2023-07-10 thomas staged_status != GOT_STATUS_DELETE)
4906 97a02382 2023-07-10 thomas return NULL;
4907 97a02382 2023-07-10 thomas } else {
4908 97a02382 2023-07-10 thomas if (staged_status == GOT_STATUS_DELETE)
4909 97a02382 2023-07-10 thomas return NULL;
4910 97a02382 2023-07-10 thomas if (status == GOT_STATUS_NONEXISTENT)
4911 97a02382 2023-07-10 thomas return got_error_set_errno(ENOENT, path);
4912 97a02382 2023-07-10 thomas if (status != GOT_STATUS_MODIFY &&
4913 97a02382 2023-07-10 thomas status != GOT_STATUS_ADD &&
4914 97a02382 2023-07-10 thomas status != GOT_STATUS_DELETE &&
4915 97a02382 2023-07-10 thomas status != GOT_STATUS_CONFLICT)
4916 97a02382 2023-07-10 thomas return NULL;
4919 97a02382 2023-07-10 thomas err = got_opentemp_truncate(a->f1);
4920 97a02382 2023-07-10 thomas if (err)
4921 97a02382 2023-07-10 thomas return got_error_from_errno("got_opentemp_truncate");
4922 97a02382 2023-07-10 thomas err = got_opentemp_truncate(a->f2);
4923 97a02382 2023-07-10 thomas if (err)
4924 97a02382 2023-07-10 thomas return got_error_from_errno("got_opentemp_truncate");
4926 97a02382 2023-07-10 thomas if (!a->header_shown) {
4927 97a02382 2023-07-10 thomas if (fprintf(a->outfile, "diff %s%s\n",
4928 97a02382 2023-07-10 thomas a->diff_staged ? "-s " : "",
4929 97a02382 2023-07-10 thomas got_worktree_get_root_path(a->worktree)) < 0) {
4930 97a02382 2023-07-10 thomas err = got_error_from_errno("fprintf");
4931 97a02382 2023-07-10 thomas goto done;
4933 97a02382 2023-07-10 thomas if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
4934 97a02382 2023-07-10 thomas err = got_error_from_errno("fprintf");
4935 97a02382 2023-07-10 thomas goto done;
4937 97a02382 2023-07-10 thomas if (fprintf(a->outfile, "path + %s%s\n",
4938 97a02382 2023-07-10 thomas got_worktree_get_root_path(a->worktree),
4939 97a02382 2023-07-10 thomas a->diff_staged ? " (staged changes)" : "") < 0) {
4940 97a02382 2023-07-10 thomas err = got_error_from_errno("fprintf");
4941 97a02382 2023-07-10 thomas goto done;
4943 97a02382 2023-07-10 thomas a->header_shown = 1;
4946 97a02382 2023-07-10 thomas if (a->diff_staged) {
4947 97a02382 2023-07-10 thomas const char *label1 = NULL, *label2 = NULL;
4948 97a02382 2023-07-10 thomas switch (staged_status) {
4949 97a02382 2023-07-10 thomas case GOT_STATUS_MODIFY:
4950 97a02382 2023-07-10 thomas label1 = path;
4951 97a02382 2023-07-10 thomas label2 = path;
4953 97a02382 2023-07-10 thomas case GOT_STATUS_ADD:
4954 97a02382 2023-07-10 thomas label2 = path;
4956 97a02382 2023-07-10 thomas case GOT_STATUS_DELETE:
4957 97a02382 2023-07-10 thomas label1 = path;
4959 97a02382 2023-07-10 thomas default:
4960 97a02382 2023-07-10 thomas return got_error(GOT_ERR_FILE_STATUS);
4962 97a02382 2023-07-10 thomas fd1 = got_opentempfd();
4963 97a02382 2023-07-10 thomas if (fd1 == -1) {
4964 97a02382 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
4965 97a02382 2023-07-10 thomas goto done;
4967 97a02382 2023-07-10 thomas fd2 = got_opentempfd();
4968 97a02382 2023-07-10 thomas if (fd2 == -1) {
4969 97a02382 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
4970 97a02382 2023-07-10 thomas goto done;
4972 97a02382 2023-07-10 thomas err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4973 97a02382 2023-07-10 thomas fd1, fd2, blob_id, staged_blob_id, label1, label2,
4974 97a02382 2023-07-10 thomas a->diff_algo, a->diff_context, a->ignore_whitespace,
4975 97a02382 2023-07-10 thomas a->force_text_diff, a->diffstat, a->repo, a->outfile);
4976 97a02382 2023-07-10 thomas goto done;
4979 97a02382 2023-07-10 thomas fd1 = got_opentempfd();
4980 97a02382 2023-07-10 thomas if (fd1 == -1) {
4981 97a02382 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
4982 97a02382 2023-07-10 thomas goto done;
4985 97a02382 2023-07-10 thomas if (staged_status == GOT_STATUS_ADD ||
4986 97a02382 2023-07-10 thomas staged_status == GOT_STATUS_MODIFY) {
4987 97a02382 2023-07-10 thomas char *id_str;
4988 97a02382 2023-07-10 thomas err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4989 97a02382 2023-07-10 thomas 8192, fd1);
4990 97a02382 2023-07-10 thomas if (err)
4991 97a02382 2023-07-10 thomas goto done;
4992 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, staged_blob_id);
4993 97a02382 2023-07-10 thomas if (err)
4994 97a02382 2023-07-10 thomas goto done;
4995 97a02382 2023-07-10 thomas if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4996 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
4997 97a02382 2023-07-10 thomas free(id_str);
4998 97a02382 2023-07-10 thomas goto done;
5000 97a02382 2023-07-10 thomas free(id_str);
5001 97a02382 2023-07-10 thomas } else if (status != GOT_STATUS_ADD) {
5002 97a02382 2023-07-10 thomas err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
5004 97a02382 2023-07-10 thomas if (err)
5005 97a02382 2023-07-10 thomas goto done;
5008 97a02382 2023-07-10 thomas if (status != GOT_STATUS_DELETE) {
5009 97a02382 2023-07-10 thomas if (asprintf(&abspath, "%s/%s",
5010 97a02382 2023-07-10 thomas got_worktree_get_root_path(a->worktree), path) == -1) {
5011 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
5012 97a02382 2023-07-10 thomas goto done;
5015 97a02382 2023-07-10 thomas if (dirfd != -1) {
5016 97a02382 2023-07-10 thomas fd = openat(dirfd, de_name,
5017 97a02382 2023-07-10 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5018 97a02382 2023-07-10 thomas if (fd == -1) {
5019 97a02382 2023-07-10 thomas if (!got_err_open_nofollow_on_symlink()) {
5020 97a02382 2023-07-10 thomas err = got_error_from_errno2("openat",
5021 97a02382 2023-07-10 thomas abspath);
5022 97a02382 2023-07-10 thomas goto done;
5024 97a02382 2023-07-10 thomas err = get_symlink_target_file(&fd, dirfd,
5025 97a02382 2023-07-10 thomas de_name, abspath);
5026 97a02382 2023-07-10 thomas if (err)
5027 97a02382 2023-07-10 thomas goto done;
5029 97a02382 2023-07-10 thomas } else {
5030 97a02382 2023-07-10 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5031 97a02382 2023-07-10 thomas if (fd == -1) {
5032 97a02382 2023-07-10 thomas if (!got_err_open_nofollow_on_symlink()) {
5033 97a02382 2023-07-10 thomas err = got_error_from_errno2("open",
5034 97a02382 2023-07-10 thomas abspath);
5035 97a02382 2023-07-10 thomas goto done;
5037 97a02382 2023-07-10 thomas err = get_symlink_target_file(&fd, dirfd,
5038 97a02382 2023-07-10 thomas de_name, abspath);
5039 97a02382 2023-07-10 thomas if (err)
5040 97a02382 2023-07-10 thomas goto done;
5043 97a02382 2023-07-10 thomas if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
5044 97a02382 2023-07-10 thomas err = got_error_from_errno2("fstatat", abspath);
5045 97a02382 2023-07-10 thomas goto done;
5047 97a02382 2023-07-10 thomas f2 = fdopen(fd, "r");
5048 97a02382 2023-07-10 thomas if (f2 == NULL) {
5049 97a02382 2023-07-10 thomas err = got_error_from_errno2("fdopen", abspath);
5050 97a02382 2023-07-10 thomas goto done;
5052 97a02382 2023-07-10 thomas fd = -1;
5053 97a02382 2023-07-10 thomas f2_exists = 1;
5056 97a02382 2023-07-10 thomas if (blob1) {
5057 97a02382 2023-07-10 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5058 97a02382 2023-07-10 thomas a->f1, blob1);
5059 97a02382 2023-07-10 thomas if (err)
5060 97a02382 2023-07-10 thomas goto done;
5063 97a02382 2023-07-10 thomas err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
5064 97a02382 2023-07-10 thomas f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
5065 97a02382 2023-07-10 thomas a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
5067 97a02382 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5068 97a02382 2023-07-10 thomas err = got_error_from_errno("close");
5069 97a02382 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5070 97a02382 2023-07-10 thomas err = got_error_from_errno("close");
5071 97a02382 2023-07-10 thomas if (blob1)
5072 97a02382 2023-07-10 thomas got_object_blob_close(blob1);
5073 97a02382 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5074 97a02382 2023-07-10 thomas err = got_error_from_errno("close");
5075 97a02382 2023-07-10 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5076 97a02382 2023-07-10 thomas err = got_error_from_errno("fclose");
5077 97a02382 2023-07-10 thomas free(abspath);
5078 97a02382 2023-07-10 thomas return err;
5081 97a02382 2023-07-10 thomas static const struct got_error *
5082 97a02382 2023-07-10 thomas cmd_diff(int argc, char *argv[])
5084 97a02382 2023-07-10 thomas const struct got_error *error;
5085 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
5086 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
5087 97a02382 2023-07-10 thomas char *cwd = NULL, *repo_path = NULL;
5088 97a02382 2023-07-10 thomas const char *commit_args[2] = { NULL, NULL };
5089 97a02382 2023-07-10 thomas int ncommit_args = 0;
5090 97a02382 2023-07-10 thomas struct got_object_id *ids[2] = { NULL, NULL };
5091 97a02382 2023-07-10 thomas char *labels[2] = { NULL, NULL };
5092 97a02382 2023-07-10 thomas int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
5093 97a02382 2023-07-10 thomas int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
5094 97a02382 2023-07-10 thomas int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
5095 97a02382 2023-07-10 thomas const char *errstr;
5096 97a02382 2023-07-10 thomas struct got_reflist_head refs;
5097 97a02382 2023-07-10 thomas struct got_pathlist_head diffstat_paths, paths;
5098 97a02382 2023-07-10 thomas FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5099 97a02382 2023-07-10 thomas int fd1 = -1, fd2 = -1;
5100 97a02382 2023-07-10 thomas int *pack_fds = NULL;
5101 97a02382 2023-07-10 thomas struct got_diffstat_cb_arg dsa;
5103 97a02382 2023-07-10 thomas memset(&dsa, 0, sizeof(dsa));
5105 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
5106 97a02382 2023-07-10 thomas TAILQ_INIT(&paths);
5107 97a02382 2023-07-10 thomas TAILQ_INIT(&diffstat_paths);
5109 97a02382 2023-07-10 thomas #ifndef PROFILE
5110 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5111 97a02382 2023-07-10 thomas NULL) == -1)
5112 97a02382 2023-07-10 thomas err(1, "pledge");
5115 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
5116 97a02382 2023-07-10 thomas switch (ch) {
5117 97a02382 2023-07-10 thomas case 'a':
5118 97a02382 2023-07-10 thomas force_text_diff = 1;
5120 97a02382 2023-07-10 thomas case 'C':
5121 97a02382 2023-07-10 thomas diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5122 97a02382 2023-07-10 thomas &errstr);
5123 97a02382 2023-07-10 thomas if (errstr != NULL)
5124 97a02382 2023-07-10 thomas errx(1, "number of context lines is %s: %s",
5125 97a02382 2023-07-10 thomas errstr, optarg);
5127 97a02382 2023-07-10 thomas case 'c':
5128 97a02382 2023-07-10 thomas if (ncommit_args >= 2)
5129 97a02382 2023-07-10 thomas errx(1, "too many -c options used");
5130 97a02382 2023-07-10 thomas commit_args[ncommit_args++] = optarg;
5132 97a02382 2023-07-10 thomas case 'd':
5133 97a02382 2023-07-10 thomas show_diffstat = 1;
5135 97a02382 2023-07-10 thomas case 'P':
5136 97a02382 2023-07-10 thomas force_path = 1;
5138 97a02382 2023-07-10 thomas case 'r':
5139 97a02382 2023-07-10 thomas repo_path = realpath(optarg, NULL);
5140 97a02382 2023-07-10 thomas if (repo_path == NULL)
5141 97a02382 2023-07-10 thomas return got_error_from_errno2("realpath",
5142 97a02382 2023-07-10 thomas optarg);
5143 97a02382 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
5144 97a02382 2023-07-10 thomas rflag = 1;
5146 97a02382 2023-07-10 thomas case 's':
5147 97a02382 2023-07-10 thomas diff_staged = 1;
5149 97a02382 2023-07-10 thomas case 'w':
5150 97a02382 2023-07-10 thomas ignore_whitespace = 1;
5152 97a02382 2023-07-10 thomas default:
5153 97a02382 2023-07-10 thomas usage_diff();
5154 97a02382 2023-07-10 thomas /* NOTREACHED */
5158 97a02382 2023-07-10 thomas argc -= optind;
5159 97a02382 2023-07-10 thomas argv += optind;
5161 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
5162 97a02382 2023-07-10 thomas if (cwd == NULL) {
5163 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
5164 97a02382 2023-07-10 thomas goto done;
5167 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
5168 97a02382 2023-07-10 thomas if (error != NULL)
5169 97a02382 2023-07-10 thomas goto done;
5171 97a02382 2023-07-10 thomas if (repo_path == NULL) {
5172 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
5173 97a02382 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
5174 97a02382 2023-07-10 thomas goto done;
5176 97a02382 2023-07-10 thomas error = NULL;
5177 97a02382 2023-07-10 thomas if (worktree) {
5178 97a02382 2023-07-10 thomas repo_path =
5179 97a02382 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree));
5180 97a02382 2023-07-10 thomas if (repo_path == NULL) {
5181 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
5182 97a02382 2023-07-10 thomas goto done;
5184 97a02382 2023-07-10 thomas } else {
5185 97a02382 2023-07-10 thomas repo_path = strdup(cwd);
5186 97a02382 2023-07-10 thomas if (repo_path == NULL) {
5187 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
5188 97a02382 2023-07-10 thomas goto done;
5193 97a02382 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5194 97a02382 2023-07-10 thomas free(repo_path);
5195 97a02382 2023-07-10 thomas if (error != NULL)
5196 97a02382 2023-07-10 thomas goto done;
5198 97a02382 2023-07-10 thomas if (show_diffstat) {
5199 97a02382 2023-07-10 thomas dsa.paths = &diffstat_paths;
5200 97a02382 2023-07-10 thomas dsa.force_text = force_text_diff;
5201 97a02382 2023-07-10 thomas dsa.ignore_ws = ignore_whitespace;
5202 97a02382 2023-07-10 thomas dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5205 97a02382 2023-07-10 thomas if (rflag || worktree == NULL || ncommit_args > 0) {
5206 97a02382 2023-07-10 thomas if (force_path) {
5207 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_NOT_IMPL,
5208 97a02382 2023-07-10 thomas "-P option can only be used when diffing "
5209 97a02382 2023-07-10 thomas "a work tree");
5210 97a02382 2023-07-10 thomas goto done;
5212 97a02382 2023-07-10 thomas if (diff_staged) {
5213 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_NOT_IMPL,
5214 97a02382 2023-07-10 thomas "-s option can only be used when diffing "
5215 97a02382 2023-07-10 thomas "a work tree");
5216 97a02382 2023-07-10 thomas goto done;
5220 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1,
5221 97a02382 2023-07-10 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
5222 97a02382 2023-07-10 thomas if (error)
5223 97a02382 2023-07-10 thomas goto done;
5225 97a02382 2023-07-10 thomas if ((!force_path && argc == 2) || ncommit_args > 0) {
5226 97a02382 2023-07-10 thomas int obj_type = (ncommit_args > 0 ?
5227 97a02382 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5228 97a02382 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5230 97a02382 2023-07-10 thomas if (error)
5231 97a02382 2023-07-10 thomas goto done;
5232 97a02382 2023-07-10 thomas for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5233 97a02382 2023-07-10 thomas const char *arg;
5234 97a02382 2023-07-10 thomas if (ncommit_args > 0)
5235 97a02382 2023-07-10 thomas arg = commit_args[i];
5237 97a02382 2023-07-10 thomas arg = argv[i];
5238 97a02382 2023-07-10 thomas error = got_repo_match_object_id(&ids[i], &labels[i],
5239 97a02382 2023-07-10 thomas arg, obj_type, &refs, repo);
5240 97a02382 2023-07-10 thomas if (error) {
5241 97a02382 2023-07-10 thomas if (error->code != GOT_ERR_NOT_REF &&
5242 97a02382 2023-07-10 thomas error->code != GOT_ERR_NO_OBJ)
5243 97a02382 2023-07-10 thomas goto done;
5244 97a02382 2023-07-10 thomas if (ncommit_args > 0)
5245 97a02382 2023-07-10 thomas goto done;
5246 97a02382 2023-07-10 thomas error = NULL;
5252 97a02382 2023-07-10 thomas f1 = got_opentemp();
5253 97a02382 2023-07-10 thomas if (f1 == NULL) {
5254 97a02382 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
5255 97a02382 2023-07-10 thomas goto done;
5258 97a02382 2023-07-10 thomas f2 = got_opentemp();
5259 97a02382 2023-07-10 thomas if (f2 == NULL) {
5260 97a02382 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
5261 97a02382 2023-07-10 thomas goto done;
5264 97a02382 2023-07-10 thomas outfile = got_opentemp();
5265 97a02382 2023-07-10 thomas if (outfile == NULL) {
5266 97a02382 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
5267 97a02382 2023-07-10 thomas goto done;
5270 97a02382 2023-07-10 thomas if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5271 97a02382 2023-07-10 thomas struct print_diff_arg arg;
5272 97a02382 2023-07-10 thomas char *id_str;
5274 97a02382 2023-07-10 thomas if (worktree == NULL) {
5275 97a02382 2023-07-10 thomas if (argc == 2 && ids[0] == NULL) {
5276 97a02382 2023-07-10 thomas error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5277 97a02382 2023-07-10 thomas goto done;
5278 97a02382 2023-07-10 thomas } else if (argc == 2 && ids[1] == NULL) {
5279 97a02382 2023-07-10 thomas error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5280 97a02382 2023-07-10 thomas goto done;
5281 97a02382 2023-07-10 thomas } else if (argc > 0) {
5282 97a02382 2023-07-10 thomas error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5283 97a02382 2023-07-10 thomas "%s", "specified paths cannot be resolved");
5284 97a02382 2023-07-10 thomas goto done;
5285 97a02382 2023-07-10 thomas } else {
5286 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_NOT_WORKTREE);
5287 97a02382 2023-07-10 thomas goto done;
5291 97a02382 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv,
5292 97a02382 2023-07-10 thomas worktree);
5293 97a02382 2023-07-10 thomas if (error)
5294 97a02382 2023-07-10 thomas goto done;
5296 97a02382 2023-07-10 thomas error = got_object_id_str(&id_str,
5297 97a02382 2023-07-10 thomas got_worktree_get_base_commit_id(worktree));
5298 97a02382 2023-07-10 thomas if (error)
5299 97a02382 2023-07-10 thomas goto done;
5300 97a02382 2023-07-10 thomas arg.repo = repo;
5301 97a02382 2023-07-10 thomas arg.worktree = worktree;
5302 97a02382 2023-07-10 thomas arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5303 97a02382 2023-07-10 thomas arg.diff_context = diff_context;
5304 97a02382 2023-07-10 thomas arg.id_str = id_str;
5305 97a02382 2023-07-10 thomas arg.header_shown = 0;
5306 97a02382 2023-07-10 thomas arg.diff_staged = diff_staged;
5307 97a02382 2023-07-10 thomas arg.ignore_whitespace = ignore_whitespace;
5308 97a02382 2023-07-10 thomas arg.force_text_diff = force_text_diff;
5309 97a02382 2023-07-10 thomas arg.diffstat = show_diffstat ? &dsa : NULL;
5310 97a02382 2023-07-10 thomas arg.f1 = f1;
5311 97a02382 2023-07-10 thomas arg.f2 = f2;
5312 97a02382 2023-07-10 thomas arg.outfile = outfile;
5314 97a02382 2023-07-10 thomas error = got_worktree_status(worktree, &paths, repo, 0,
5315 97a02382 2023-07-10 thomas print_diff, &arg, check_cancelled, NULL);
5316 97a02382 2023-07-10 thomas free(id_str);
5317 97a02382 2023-07-10 thomas if (error)
5318 97a02382 2023-07-10 thomas goto done;
5320 97a02382 2023-07-10 thomas if (show_diffstat && dsa.nfiles > 0) {
5321 97a02382 2023-07-10 thomas char *header;
5323 97a02382 2023-07-10 thomas if (asprintf(&header, "diffstat %s%s",
5324 97a02382 2023-07-10 thomas diff_staged ? "-s " : "",
5325 97a02382 2023-07-10 thomas got_worktree_get_root_path(worktree)) == -1) {
5326 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
5327 97a02382 2023-07-10 thomas goto done;
5330 97a02382 2023-07-10 thomas error = print_diffstat(&dsa, header);
5331 97a02382 2023-07-10 thomas free(header);
5332 97a02382 2023-07-10 thomas if (error)
5333 97a02382 2023-07-10 thomas goto done;
5336 97a02382 2023-07-10 thomas error = printfile(outfile);
5337 97a02382 2023-07-10 thomas goto done;
5340 97a02382 2023-07-10 thomas if (ncommit_args == 1) {
5341 97a02382 2023-07-10 thomas struct got_commit_object *commit;
5342 97a02382 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, ids[0]);
5343 97a02382 2023-07-10 thomas if (error)
5344 97a02382 2023-07-10 thomas goto done;
5346 97a02382 2023-07-10 thomas labels[1] = labels[0];
5347 97a02382 2023-07-10 thomas ids[1] = ids[0];
5348 97a02382 2023-07-10 thomas if (got_object_commit_get_nparents(commit) > 0) {
5349 97a02382 2023-07-10 thomas const struct got_object_id_queue *pids;
5350 97a02382 2023-07-10 thomas struct got_object_qid *pid;
5351 97a02382 2023-07-10 thomas pids = got_object_commit_get_parent_ids(commit);
5352 97a02382 2023-07-10 thomas pid = STAILQ_FIRST(pids);
5353 97a02382 2023-07-10 thomas ids[0] = got_object_id_dup(&pid->id);
5354 97a02382 2023-07-10 thomas if (ids[0] == NULL) {
5355 97a02382 2023-07-10 thomas error = got_error_from_errno(
5356 97a02382 2023-07-10 thomas "got_object_id_dup");
5357 97a02382 2023-07-10 thomas got_object_commit_close(commit);
5358 97a02382 2023-07-10 thomas goto done;
5360 97a02382 2023-07-10 thomas error = got_object_id_str(&labels[0], ids[0]);
5361 97a02382 2023-07-10 thomas if (error) {
5362 97a02382 2023-07-10 thomas got_object_commit_close(commit);
5363 97a02382 2023-07-10 thomas goto done;
5365 97a02382 2023-07-10 thomas } else {
5366 97a02382 2023-07-10 thomas ids[0] = NULL;
5367 97a02382 2023-07-10 thomas labels[0] = strdup("/dev/null");
5368 97a02382 2023-07-10 thomas if (labels[0] == NULL) {
5369 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
5370 97a02382 2023-07-10 thomas got_object_commit_close(commit);
5371 97a02382 2023-07-10 thomas goto done;
5375 97a02382 2023-07-10 thomas got_object_commit_close(commit);
5378 97a02382 2023-07-10 thomas if (ncommit_args == 0 && argc > 2) {
5379 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_BAD_PATH,
5380 97a02382 2023-07-10 thomas "path arguments cannot be used when diffing two objects");
5381 97a02382 2023-07-10 thomas goto done;
5384 97a02382 2023-07-10 thomas if (ids[0]) {
5385 97a02382 2023-07-10 thomas error = got_object_get_type(&type1, repo, ids[0]);
5386 97a02382 2023-07-10 thomas if (error)
5387 97a02382 2023-07-10 thomas goto done;
5390 97a02382 2023-07-10 thomas error = got_object_get_type(&type2, repo, ids[1]);
5391 97a02382 2023-07-10 thomas if (error)
5392 97a02382 2023-07-10 thomas goto done;
5393 97a02382 2023-07-10 thomas if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5394 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_OBJ_TYPE);
5395 97a02382 2023-07-10 thomas goto done;
5397 97a02382 2023-07-10 thomas if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5398 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_OBJ_TYPE,
5399 97a02382 2023-07-10 thomas "path arguments cannot be used when diffing blobs");
5400 97a02382 2023-07-10 thomas goto done;
5403 97a02382 2023-07-10 thomas for (i = 0; ncommit_args > 0 && i < argc; i++) {
5404 97a02382 2023-07-10 thomas char *in_repo_path;
5405 97a02382 2023-07-10 thomas struct got_pathlist_entry *new;
5406 97a02382 2023-07-10 thomas if (worktree) {
5407 97a02382 2023-07-10 thomas const char *prefix;
5408 97a02382 2023-07-10 thomas char *p;
5409 97a02382 2023-07-10 thomas error = got_worktree_resolve_path(&p, worktree,
5410 97a02382 2023-07-10 thomas argv[i]);
5411 97a02382 2023-07-10 thomas if (error)
5412 97a02382 2023-07-10 thomas goto done;
5413 97a02382 2023-07-10 thomas prefix = got_worktree_get_path_prefix(worktree);
5414 97a02382 2023-07-10 thomas while (prefix[0] == '/')
5415 97a02382 2023-07-10 thomas prefix++;
5416 97a02382 2023-07-10 thomas if (asprintf(&in_repo_path, "%s%s%s", prefix,
5417 97a02382 2023-07-10 thomas (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5418 97a02382 2023-07-10 thomas p) == -1) {
5419 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
5420 97a02382 2023-07-10 thomas free(p);
5421 97a02382 2023-07-10 thomas goto done;
5423 97a02382 2023-07-10 thomas free(p);
5424 97a02382 2023-07-10 thomas } else {
5425 97a02382 2023-07-10 thomas char *mapped_path, *s;
5426 97a02382 2023-07-10 thomas error = got_repo_map_path(&mapped_path, repo, argv[i]);
5427 97a02382 2023-07-10 thomas if (error)
5428 97a02382 2023-07-10 thomas goto done;
5429 97a02382 2023-07-10 thomas s = mapped_path;
5430 97a02382 2023-07-10 thomas while (s[0] == '/')
5432 97a02382 2023-07-10 thomas in_repo_path = strdup(s);
5433 97a02382 2023-07-10 thomas if (in_repo_path == NULL) {
5434 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
5435 97a02382 2023-07-10 thomas free(mapped_path);
5436 97a02382 2023-07-10 thomas goto done;
5438 97a02382 2023-07-10 thomas free(mapped_path);
5441 97a02382 2023-07-10 thomas error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5442 97a02382 2023-07-10 thomas if (error || new == NULL /* duplicate */)
5443 97a02382 2023-07-10 thomas free(in_repo_path);
5444 97a02382 2023-07-10 thomas if (error)
5445 97a02382 2023-07-10 thomas goto done;
5448 97a02382 2023-07-10 thomas if (worktree) {
5449 97a02382 2023-07-10 thomas /* Release work tree lock. */
5450 97a02382 2023-07-10 thomas got_worktree_close(worktree);
5451 97a02382 2023-07-10 thomas worktree = NULL;
5454 97a02382 2023-07-10 thomas fd1 = got_opentempfd();
5455 97a02382 2023-07-10 thomas if (fd1 == -1) {
5456 97a02382 2023-07-10 thomas error = got_error_from_errno("got_opentempfd");
5457 97a02382 2023-07-10 thomas goto done;
5460 97a02382 2023-07-10 thomas fd2 = got_opentempfd();
5461 97a02382 2023-07-10 thomas if (fd2 == -1) {
5462 97a02382 2023-07-10 thomas error = got_error_from_errno("got_opentempfd");
5463 97a02382 2023-07-10 thomas goto done;
5466 97a02382 2023-07-10 thomas switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5467 97a02382 2023-07-10 thomas case GOT_OBJ_TYPE_BLOB:
5468 97a02382 2023-07-10 thomas error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5469 97a02382 2023-07-10 thomas fd1, fd2, ids[0], ids[1], NULL, NULL,
5470 97a02382 2023-07-10 thomas GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5471 97a02382 2023-07-10 thomas ignore_whitespace, force_text_diff,
5472 97a02382 2023-07-10 thomas show_diffstat ? &dsa : NULL, repo, outfile);
5474 97a02382 2023-07-10 thomas case GOT_OBJ_TYPE_TREE:
5475 97a02382 2023-07-10 thomas error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5476 97a02382 2023-07-10 thomas ids[0], ids[1], &paths, "", "",
5477 97a02382 2023-07-10 thomas GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5478 97a02382 2023-07-10 thomas ignore_whitespace, force_text_diff,
5479 97a02382 2023-07-10 thomas show_diffstat ? &dsa : NULL, repo, outfile);
5481 97a02382 2023-07-10 thomas case GOT_OBJ_TYPE_COMMIT:
5482 97a02382 2023-07-10 thomas fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
5483 97a02382 2023-07-10 thomas error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5484 97a02382 2023-07-10 thomas fd1, fd2, ids[0], ids[1], &paths,
5485 97a02382 2023-07-10 thomas GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5486 97a02382 2023-07-10 thomas ignore_whitespace, force_text_diff,
5487 97a02382 2023-07-10 thomas show_diffstat ? &dsa : NULL, repo, outfile);
5489 97a02382 2023-07-10 thomas default:
5490 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_OBJ_TYPE);
5492 97a02382 2023-07-10 thomas if (error)
5493 97a02382 2023-07-10 thomas goto done;
5495 97a02382 2023-07-10 thomas if (show_diffstat && dsa.nfiles > 0) {
5496 97a02382 2023-07-10 thomas char *header = NULL;
5498 97a02382 2023-07-10 thomas if (asprintf(&header, "diffstat %s %s",
5499 97a02382 2023-07-10 thomas labels[0], labels[1]) == -1) {
5500 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
5501 97a02382 2023-07-10 thomas goto done;
5504 97a02382 2023-07-10 thomas error = print_diffstat(&dsa, header);
5505 97a02382 2023-07-10 thomas free(header);
5506 97a02382 2023-07-10 thomas if (error)
5507 97a02382 2023-07-10 thomas goto done;
5510 97a02382 2023-07-10 thomas error = printfile(outfile);
5513 97a02382 2023-07-10 thomas free(labels[0]);
5514 97a02382 2023-07-10 thomas free(labels[1]);
5515 97a02382 2023-07-10 thomas free(ids[0]);
5516 97a02382 2023-07-10 thomas free(ids[1]);
5517 97a02382 2023-07-10 thomas if (worktree)
5518 97a02382 2023-07-10 thomas got_worktree_close(worktree);
5519 97a02382 2023-07-10 thomas if (repo) {
5520 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
5521 97a02382 2023-07-10 thomas if (error == NULL)
5522 97a02382 2023-07-10 thomas error = close_err;
5524 97a02382 2023-07-10 thomas if (pack_fds) {
5525 97a02382 2023-07-10 thomas const struct got_error *pack_err =
5526 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
5527 97a02382 2023-07-10 thomas if (error == NULL)
5528 97a02382 2023-07-10 thomas error = pack_err;
5530 97a02382 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5531 97a02382 2023-07-10 thomas got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
5532 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
5533 97a02382 2023-07-10 thomas if (outfile && fclose(outfile) == EOF && error == NULL)
5534 97a02382 2023-07-10 thomas error = got_error_from_errno("fclose");
5535 97a02382 2023-07-10 thomas if (f1 && fclose(f1) == EOF && error == NULL)
5536 97a02382 2023-07-10 thomas error = got_error_from_errno("fclose");
5537 97a02382 2023-07-10 thomas if (f2 && fclose(f2) == EOF && error == NULL)
5538 97a02382 2023-07-10 thomas error = got_error_from_errno("fclose");
5539 97a02382 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5540 97a02382 2023-07-10 thomas error = got_error_from_errno("close");
5541 97a02382 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5542 97a02382 2023-07-10 thomas error = got_error_from_errno("close");
5543 97a02382 2023-07-10 thomas return error;
5546 97a02382 2023-07-10 thomas __dead static void
5547 97a02382 2023-07-10 thomas usage_blame(void)
5549 97a02382 2023-07-10 thomas fprintf(stderr,
5550 97a02382 2023-07-10 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5551 97a02382 2023-07-10 thomas getprogname());
5552 97a02382 2023-07-10 thomas exit(1);
5555 97a02382 2023-07-10 thomas struct blame_line {
5556 97a02382 2023-07-10 thomas int annotated;
5557 97a02382 2023-07-10 thomas char *id_str;
5558 97a02382 2023-07-10 thomas char *committer;
5559 97a02382 2023-07-10 thomas char datebuf[11]; /* YYYY-MM-DD + NUL */
5562 97a02382 2023-07-10 thomas struct blame_cb_args {
5563 97a02382 2023-07-10 thomas struct blame_line *lines;
5564 97a02382 2023-07-10 thomas int nlines;
5565 97a02382 2023-07-10 thomas int nlines_prec;
5566 97a02382 2023-07-10 thomas int lineno_cur;
5567 97a02382 2023-07-10 thomas off_t *line_offsets;
5568 97a02382 2023-07-10 thomas FILE *f;
5569 97a02382 2023-07-10 thomas struct got_repository *repo;
5572 97a02382 2023-07-10 thomas static const struct got_error *
5573 97a02382 2023-07-10 thomas blame_cb(void *arg, int nlines, int lineno,
5574 97a02382 2023-07-10 thomas struct got_commit_object *commit, struct got_object_id *id)
5576 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
5577 97a02382 2023-07-10 thomas struct blame_cb_args *a = arg;
5578 97a02382 2023-07-10 thomas struct blame_line *bline;
5579 97a02382 2023-07-10 thomas char *line = NULL;
5580 97a02382 2023-07-10 thomas size_t linesize = 0;
5581 97a02382 2023-07-10 thomas off_t offset;
5582 97a02382 2023-07-10 thomas struct tm tm;
5583 97a02382 2023-07-10 thomas time_t committer_time;
5585 97a02382 2023-07-10 thomas if (nlines != a->nlines ||
5586 97a02382 2023-07-10 thomas (lineno != -1 && lineno < 1) || lineno > a->nlines)
5587 97a02382 2023-07-10 thomas return got_error(GOT_ERR_RANGE);
5589 97a02382 2023-07-10 thomas if (sigint_received)
5590 97a02382 2023-07-10 thomas return got_error(GOT_ERR_ITER_COMPLETED);
5592 97a02382 2023-07-10 thomas if (lineno == -1)
5593 97a02382 2023-07-10 thomas return NULL; /* no change in this commit */
5595 97a02382 2023-07-10 thomas /* Annotate this line. */
5596 97a02382 2023-07-10 thomas bline = &a->lines[lineno - 1];
5597 97a02382 2023-07-10 thomas if (bline->annotated)
5598 97a02382 2023-07-10 thomas return NULL;
5599 97a02382 2023-07-10 thomas err = got_object_id_str(&bline->id_str, id);
5600 97a02382 2023-07-10 thomas if (err)
5601 97a02382 2023-07-10 thomas return err;
5603 97a02382 2023-07-10 thomas bline->committer = strdup(got_object_commit_get_committer(commit));
5604 97a02382 2023-07-10 thomas if (bline->committer == NULL) {
5605 97a02382 2023-07-10 thomas err = got_error_from_errno("strdup");
5606 97a02382 2023-07-10 thomas goto done;
5609 97a02382 2023-07-10 thomas committer_time = got_object_commit_get_committer_time(commit);
5610 97a02382 2023-07-10 thomas if (gmtime_r(&committer_time, &tm) == NULL)
5611 97a02382 2023-07-10 thomas return got_error_from_errno("gmtime_r");
5612 97a02382 2023-07-10 thomas if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5613 97a02382 2023-07-10 thomas &tm) == 0) {
5614 97a02382 2023-07-10 thomas err = got_error(GOT_ERR_NO_SPACE);
5615 97a02382 2023-07-10 thomas goto done;
5617 97a02382 2023-07-10 thomas bline->annotated = 1;
5619 97a02382 2023-07-10 thomas /* Print lines annotated so far. */
5620 97a02382 2023-07-10 thomas bline = &a->lines[a->lineno_cur - 1];
5621 97a02382 2023-07-10 thomas if (!bline->annotated)
5622 97a02382 2023-07-10 thomas goto done;
5624 97a02382 2023-07-10 thomas offset = a->line_offsets[a->lineno_cur - 1];
5625 97a02382 2023-07-10 thomas if (fseeko(a->f, offset, SEEK_SET) == -1) {
5626 97a02382 2023-07-10 thomas err = got_error_from_errno("fseeko");
5627 97a02382 2023-07-10 thomas goto done;
5630 97a02382 2023-07-10 thomas while (a->lineno_cur <= a->nlines && bline->annotated) {
5631 97a02382 2023-07-10 thomas char *smallerthan, *at, *nl, *committer;
5632 97a02382 2023-07-10 thomas size_t len;
5634 97a02382 2023-07-10 thomas if (getline(&line, &linesize, a->f) == -1) {
5635 97a02382 2023-07-10 thomas if (ferror(a->f))
5636 97a02382 2023-07-10 thomas err = got_error_from_errno("getline");
5640 97a02382 2023-07-10 thomas committer = bline->committer;
5641 97a02382 2023-07-10 thomas smallerthan = strchr(committer, '<');
5642 97a02382 2023-07-10 thomas if (smallerthan && smallerthan[1] != '\0')
5643 97a02382 2023-07-10 thomas committer = smallerthan + 1;
5644 97a02382 2023-07-10 thomas at = strchr(committer, '@');
5646 97a02382 2023-07-10 thomas *at = '\0';
5647 97a02382 2023-07-10 thomas len = strlen(committer);
5648 97a02382 2023-07-10 thomas if (len >= 9)
5649 97a02382 2023-07-10 thomas committer[8] = '\0';
5651 97a02382 2023-07-10 thomas nl = strchr(line, '\n');
5653 97a02382 2023-07-10 thomas *nl = '\0';
5654 97a02382 2023-07-10 thomas printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5655 97a02382 2023-07-10 thomas bline->id_str, bline->datebuf, committer, line);
5657 97a02382 2023-07-10 thomas a->lineno_cur++;
5658 97a02382 2023-07-10 thomas bline = &a->lines[a->lineno_cur - 1];
5661 97a02382 2023-07-10 thomas free(line);
5662 97a02382 2023-07-10 thomas return err;
5665 97a02382 2023-07-10 thomas static const struct got_error *
5666 97a02382 2023-07-10 thomas cmd_blame(int argc, char *argv[])
5668 97a02382 2023-07-10 thomas const struct got_error *error;
5669 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
5670 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
5671 97a02382 2023-07-10 thomas char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5672 97a02382 2023-07-10 thomas char *link_target = NULL;
5673 97a02382 2023-07-10 thomas struct got_object_id *obj_id = NULL;
5674 97a02382 2023-07-10 thomas struct got_object_id *commit_id = NULL;
5675 97a02382 2023-07-10 thomas struct got_commit_object *commit = NULL;
5676 97a02382 2023-07-10 thomas struct got_blob_object *blob = NULL;
5677 97a02382 2023-07-10 thomas char *commit_id_str = NULL;
5678 97a02382 2023-07-10 thomas struct blame_cb_args bca;
5679 97a02382 2023-07-10 thomas int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5680 97a02382 2023-07-10 thomas off_t filesize;
5681 97a02382 2023-07-10 thomas int *pack_fds = NULL;
5682 97a02382 2023-07-10 thomas FILE *f1 = NULL, *f2 = NULL;
5684 97a02382 2023-07-10 thomas fd1 = got_opentempfd();
5685 97a02382 2023-07-10 thomas if (fd1 == -1)
5686 97a02382 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
5688 97a02382 2023-07-10 thomas memset(&bca, 0, sizeof(bca));
5690 97a02382 2023-07-10 thomas #ifndef PROFILE
5691 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5692 97a02382 2023-07-10 thomas NULL) == -1)
5693 97a02382 2023-07-10 thomas err(1, "pledge");
5696 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5697 97a02382 2023-07-10 thomas switch (ch) {
5698 97a02382 2023-07-10 thomas case 'c':
5699 97a02382 2023-07-10 thomas commit_id_str = optarg;
5701 97a02382 2023-07-10 thomas case 'r':
5702 97a02382 2023-07-10 thomas repo_path = realpath(optarg, NULL);
5703 97a02382 2023-07-10 thomas if (repo_path == NULL)
5704 97a02382 2023-07-10 thomas return got_error_from_errno2("realpath",
5705 97a02382 2023-07-10 thomas optarg);
5706 97a02382 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
5708 97a02382 2023-07-10 thomas default:
5709 97a02382 2023-07-10 thomas usage_blame();
5710 97a02382 2023-07-10 thomas /* NOTREACHED */
5714 97a02382 2023-07-10 thomas argc -= optind;
5715 97a02382 2023-07-10 thomas argv += optind;
5717 97a02382 2023-07-10 thomas if (argc == 1)
5718 97a02382 2023-07-10 thomas path = argv[0];
5720 97a02382 2023-07-10 thomas usage_blame();
5722 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
5723 97a02382 2023-07-10 thomas if (cwd == NULL) {
5724 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
5725 97a02382 2023-07-10 thomas goto done;
5728 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
5729 97a02382 2023-07-10 thomas if (error != NULL)
5730 97a02382 2023-07-10 thomas goto done;
5732 97a02382 2023-07-10 thomas if (repo_path == NULL) {
5733 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
5734 97a02382 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
5735 97a02382 2023-07-10 thomas goto done;
5737 97a02382 2023-07-10 thomas error = NULL;
5738 97a02382 2023-07-10 thomas if (worktree) {
5739 97a02382 2023-07-10 thomas repo_path =
5740 97a02382 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree));
5741 97a02382 2023-07-10 thomas if (repo_path == NULL) {
5742 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
5743 97a02382 2023-07-10 thomas if (error)
5744 97a02382 2023-07-10 thomas goto done;
5746 97a02382 2023-07-10 thomas } else {
5747 97a02382 2023-07-10 thomas repo_path = strdup(cwd);
5748 97a02382 2023-07-10 thomas if (repo_path == NULL) {
5749 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
5750 97a02382 2023-07-10 thomas goto done;
5755 97a02382 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5756 97a02382 2023-07-10 thomas if (error != NULL)
5757 97a02382 2023-07-10 thomas goto done;
5759 97a02382 2023-07-10 thomas if (worktree) {
5760 97a02382 2023-07-10 thomas const char *prefix = got_worktree_get_path_prefix(worktree);
5761 97a02382 2023-07-10 thomas char *p;
5763 97a02382 2023-07-10 thomas error = got_worktree_resolve_path(&p, worktree, path);
5764 97a02382 2023-07-10 thomas if (error)
5765 97a02382 2023-07-10 thomas goto done;
5766 97a02382 2023-07-10 thomas if (asprintf(&in_repo_path, "%s%s%s", prefix,
5767 97a02382 2023-07-10 thomas (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5768 97a02382 2023-07-10 thomas p) == -1) {
5769 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
5770 97a02382 2023-07-10 thomas free(p);
5771 97a02382 2023-07-10 thomas goto done;
5773 97a02382 2023-07-10 thomas free(p);
5774 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5775 97a02382 2023-07-10 thomas } else {
5776 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5777 97a02382 2023-07-10 thomas if (error)
5778 97a02382 2023-07-10 thomas goto done;
5779 97a02382 2023-07-10 thomas error = got_repo_map_path(&in_repo_path, repo, path);
5781 97a02382 2023-07-10 thomas if (error)
5782 97a02382 2023-07-10 thomas goto done;
5784 97a02382 2023-07-10 thomas if (commit_id_str == NULL) {
5785 97a02382 2023-07-10 thomas struct got_reference *head_ref;
5786 97a02382 2023-07-10 thomas error = got_ref_open(&head_ref, repo, worktree ?
5787 97a02382 2023-07-10 thomas got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5788 97a02382 2023-07-10 thomas if (error != NULL)
5789 97a02382 2023-07-10 thomas goto done;
5790 97a02382 2023-07-10 thomas error = got_ref_resolve(&commit_id, repo, head_ref);
5791 97a02382 2023-07-10 thomas got_ref_close(head_ref);
5792 97a02382 2023-07-10 thomas if (error != NULL)
5793 97a02382 2023-07-10 thomas goto done;
5794 97a02382 2023-07-10 thomas } else {
5795 97a02382 2023-07-10 thomas struct got_reflist_head refs;
5796 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
5797 97a02382 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5799 97a02382 2023-07-10 thomas if (error)
5800 97a02382 2023-07-10 thomas goto done;
5801 97a02382 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
5802 97a02382 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5803 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
5804 97a02382 2023-07-10 thomas if (error)
5805 97a02382 2023-07-10 thomas goto done;
5808 97a02382 2023-07-10 thomas if (worktree) {
5809 97a02382 2023-07-10 thomas /* Release work tree lock. */
5810 97a02382 2023-07-10 thomas got_worktree_close(worktree);
5811 97a02382 2023-07-10 thomas worktree = NULL;
5814 97a02382 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
5815 97a02382 2023-07-10 thomas if (error)
5816 97a02382 2023-07-10 thomas goto done;
5818 97a02382 2023-07-10 thomas error = got_object_resolve_symlinks(&link_target, in_repo_path,
5819 97a02382 2023-07-10 thomas commit, repo);
5820 97a02382 2023-07-10 thomas if (error)
5821 97a02382 2023-07-10 thomas goto done;
5823 97a02382 2023-07-10 thomas error = got_object_id_by_path(&obj_id, repo, commit,
5824 97a02382 2023-07-10 thomas link_target ? link_target : in_repo_path);
5825 97a02382 2023-07-10 thomas if (error)
5826 97a02382 2023-07-10 thomas goto done;
5828 97a02382 2023-07-10 thomas error = got_object_get_type(&obj_type, repo, obj_id);
5829 97a02382 2023-07-10 thomas if (error)
5830 97a02382 2023-07-10 thomas goto done;
5832 97a02382 2023-07-10 thomas if (obj_type != GOT_OBJ_TYPE_BLOB) {
5833 97a02382 2023-07-10 thomas error = got_error_path(link_target ? link_target : in_repo_path,
5834 97a02382 2023-07-10 thomas GOT_ERR_OBJ_TYPE);
5835 97a02382 2023-07-10 thomas goto done;
5838 97a02382 2023-07-10 thomas error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5839 97a02382 2023-07-10 thomas if (error)
5840 97a02382 2023-07-10 thomas goto done;
5841 97a02382 2023-07-10 thomas bca.f = got_opentemp();
5842 97a02382 2023-07-10 thomas if (bca.f == NULL) {
5843 97a02382 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
5844 97a02382 2023-07-10 thomas goto done;
5846 97a02382 2023-07-10 thomas error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5847 97a02382 2023-07-10 thomas &bca.line_offsets, bca.f, blob);
5848 97a02382 2023-07-10 thomas if (error || bca.nlines == 0)
5849 97a02382 2023-07-10 thomas goto done;
5851 97a02382 2023-07-10 thomas /* Don't include \n at EOF in the blame line count. */
5852 97a02382 2023-07-10 thomas if (bca.line_offsets[bca.nlines - 1] == filesize)
5853 97a02382 2023-07-10 thomas bca.nlines--;
5855 97a02382 2023-07-10 thomas bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5856 97a02382 2023-07-10 thomas if (bca.lines == NULL) {
5857 97a02382 2023-07-10 thomas error = got_error_from_errno("calloc");
5858 97a02382 2023-07-10 thomas goto done;
5860 97a02382 2023-07-10 thomas bca.lineno_cur = 1;
5861 97a02382 2023-07-10 thomas bca.nlines_prec = 0;
5862 97a02382 2023-07-10 thomas i = bca.nlines;
5863 97a02382 2023-07-10 thomas while (i > 0) {
5864 97a02382 2023-07-10 thomas i /= 10;
5865 97a02382 2023-07-10 thomas bca.nlines_prec++;
5867 97a02382 2023-07-10 thomas bca.repo = repo;
5869 97a02382 2023-07-10 thomas fd2 = got_opentempfd();
5870 97a02382 2023-07-10 thomas if (fd2 == -1) {
5871 97a02382 2023-07-10 thomas error = got_error_from_errno("got_opentempfd");
5872 97a02382 2023-07-10 thomas goto done;
5874 97a02382 2023-07-10 thomas fd3 = got_opentempfd();
5875 97a02382 2023-07-10 thomas if (fd3 == -1) {
5876 97a02382 2023-07-10 thomas error = got_error_from_errno("got_opentempfd");
5877 97a02382 2023-07-10 thomas goto done;
5879 97a02382 2023-07-10 thomas f1 = got_opentemp();
5880 97a02382 2023-07-10 thomas if (f1 == NULL) {
5881 97a02382 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
5882 97a02382 2023-07-10 thomas goto done;
5884 97a02382 2023-07-10 thomas f2 = got_opentemp();
5885 97a02382 2023-07-10 thomas if (f2 == NULL) {
5886 97a02382 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
5887 97a02382 2023-07-10 thomas goto done;
5889 97a02382 2023-07-10 thomas error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5890 97a02382 2023-07-10 thomas repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5891 97a02382 2023-07-10 thomas check_cancelled, NULL, fd2, fd3, f1, f2);
5893 97a02382 2023-07-10 thomas free(in_repo_path);
5894 97a02382 2023-07-10 thomas free(link_target);
5895 97a02382 2023-07-10 thomas free(repo_path);
5896 97a02382 2023-07-10 thomas free(cwd);
5897 97a02382 2023-07-10 thomas free(commit_id);
5898 97a02382 2023-07-10 thomas free(obj_id);
5899 97a02382 2023-07-10 thomas if (commit)
5900 97a02382 2023-07-10 thomas got_object_commit_close(commit);
5902 97a02382 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5903 97a02382 2023-07-10 thomas error = got_error_from_errno("close");
5904 97a02382 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5905 97a02382 2023-07-10 thomas error = got_error_from_errno("close");
5906 97a02382 2023-07-10 thomas if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5907 97a02382 2023-07-10 thomas error = got_error_from_errno("close");
5908 97a02382 2023-07-10 thomas if (f1 && fclose(f1) == EOF && error == NULL)
5909 97a02382 2023-07-10 thomas error = got_error_from_errno("fclose");
5910 97a02382 2023-07-10 thomas if (f2 && fclose(f2) == EOF && error == NULL)
5911 97a02382 2023-07-10 thomas error = got_error_from_errno("fclose");
5913 97a02382 2023-07-10 thomas if (blob)
5914 97a02382 2023-07-10 thomas got_object_blob_close(blob);
5915 97a02382 2023-07-10 thomas if (worktree)
5916 97a02382 2023-07-10 thomas got_worktree_close(worktree);
5917 97a02382 2023-07-10 thomas if (repo) {
5918 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
5919 97a02382 2023-07-10 thomas if (error == NULL)
5920 97a02382 2023-07-10 thomas error = close_err;
5922 97a02382 2023-07-10 thomas if (pack_fds) {
5923 97a02382 2023-07-10 thomas const struct got_error *pack_err =
5924 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
5925 97a02382 2023-07-10 thomas if (error == NULL)
5926 97a02382 2023-07-10 thomas error = pack_err;
5928 97a02382 2023-07-10 thomas if (bca.lines) {
5929 97a02382 2023-07-10 thomas for (i = 0; i < bca.nlines; i++) {
5930 97a02382 2023-07-10 thomas struct blame_line *bline = &bca.lines[i];
5931 97a02382 2023-07-10 thomas free(bline->id_str);
5932 97a02382 2023-07-10 thomas free(bline->committer);
5934 97a02382 2023-07-10 thomas free(bca.lines);
5936 97a02382 2023-07-10 thomas free(bca.line_offsets);
5937 97a02382 2023-07-10 thomas if (bca.f && fclose(bca.f) == EOF && error == NULL)
5938 97a02382 2023-07-10 thomas error = got_error_from_errno("fclose");
5939 97a02382 2023-07-10 thomas return error;
5942 97a02382 2023-07-10 thomas __dead static void
5943 97a02382 2023-07-10 thomas usage_tree(void)
5945 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
5946 97a02382 2023-07-10 thomas "[path]\n", getprogname());
5947 97a02382 2023-07-10 thomas exit(1);
5950 97a02382 2023-07-10 thomas static const struct got_error *
5951 97a02382 2023-07-10 thomas print_entry(struct got_tree_entry *te, const char *id, const char *path,
5952 97a02382 2023-07-10 thomas const char *root_path, struct got_repository *repo)
5954 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
5955 97a02382 2023-07-10 thomas int is_root_path = (strcmp(path, root_path) == 0);
5956 97a02382 2023-07-10 thomas const char *modestr = "";
5957 97a02382 2023-07-10 thomas mode_t mode = got_tree_entry_get_mode(te);
5958 97a02382 2023-07-10 thomas char *link_target = NULL;
5960 97a02382 2023-07-10 thomas path += strlen(root_path);
5961 97a02382 2023-07-10 thomas while (path[0] == '/')
5964 97a02382 2023-07-10 thomas if (got_object_tree_entry_is_submodule(te))
5965 97a02382 2023-07-10 thomas modestr = "$";
5966 97a02382 2023-07-10 thomas else if (S_ISLNK(mode)) {
5969 97a02382 2023-07-10 thomas err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5970 97a02382 2023-07-10 thomas if (err)
5971 97a02382 2023-07-10 thomas return err;
5972 97a02382 2023-07-10 thomas for (i = 0; link_target[i] != '\0'; i++) {
5973 97a02382 2023-07-10 thomas if (!isprint((unsigned char)link_target[i]))
5974 97a02382 2023-07-10 thomas link_target[i] = '?';
5977 97a02382 2023-07-10 thomas modestr = "@";
5979 97a02382 2023-07-10 thomas else if (S_ISDIR(mode))
5980 97a02382 2023-07-10 thomas modestr = "/";
5981 97a02382 2023-07-10 thomas else if (mode & S_IXUSR)
5982 97a02382 2023-07-10 thomas modestr = "*";
5984 97a02382 2023-07-10 thomas printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5985 97a02382 2023-07-10 thomas is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5986 97a02382 2023-07-10 thomas link_target ? " -> ": "", link_target ? link_target : "");
5988 97a02382 2023-07-10 thomas free(link_target);
5989 97a02382 2023-07-10 thomas return NULL;
5992 97a02382 2023-07-10 thomas static const struct got_error *
5993 97a02382 2023-07-10 thomas print_tree(const char *path, struct got_commit_object *commit,
5994 97a02382 2023-07-10 thomas int show_ids, int recurse, const char *root_path,
5995 97a02382 2023-07-10 thomas struct got_repository *repo)
5997 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
5998 97a02382 2023-07-10 thomas struct got_object_id *tree_id = NULL;
5999 97a02382 2023-07-10 thomas struct got_tree_object *tree = NULL;
6000 97a02382 2023-07-10 thomas int nentries, i;
6002 97a02382 2023-07-10 thomas err = got_object_id_by_path(&tree_id, repo, commit, path);
6003 97a02382 2023-07-10 thomas if (err)
6004 97a02382 2023-07-10 thomas goto done;
6006 97a02382 2023-07-10 thomas err = got_object_open_as_tree(&tree, repo, tree_id);
6007 97a02382 2023-07-10 thomas if (err)
6008 97a02382 2023-07-10 thomas goto done;
6009 97a02382 2023-07-10 thomas nentries = got_object_tree_get_nentries(tree);
6010 97a02382 2023-07-10 thomas for (i = 0; i < nentries; i++) {
6011 97a02382 2023-07-10 thomas struct got_tree_entry *te;
6012 97a02382 2023-07-10 thomas char *id = NULL;
6014 97a02382 2023-07-10 thomas if (sigint_received || sigpipe_received)
6017 97a02382 2023-07-10 thomas te = got_object_tree_get_entry(tree, i);
6018 97a02382 2023-07-10 thomas if (show_ids) {
6019 97a02382 2023-07-10 thomas char *id_str;
6020 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str,
6021 97a02382 2023-07-10 thomas got_tree_entry_get_id(te));
6022 97a02382 2023-07-10 thomas if (err)
6023 97a02382 2023-07-10 thomas goto done;
6024 97a02382 2023-07-10 thomas if (asprintf(&id, "%s ", id_str) == -1) {
6025 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
6026 97a02382 2023-07-10 thomas free(id_str);
6027 97a02382 2023-07-10 thomas goto done;
6029 97a02382 2023-07-10 thomas free(id_str);
6031 97a02382 2023-07-10 thomas err = print_entry(te, id, path, root_path, repo);
6032 97a02382 2023-07-10 thomas free(id);
6033 97a02382 2023-07-10 thomas if (err)
6034 97a02382 2023-07-10 thomas goto done;
6036 97a02382 2023-07-10 thomas if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
6037 97a02382 2023-07-10 thomas char *child_path;
6038 97a02382 2023-07-10 thomas if (asprintf(&child_path, "%s%s%s", path,
6039 97a02382 2023-07-10 thomas path[0] == '/' && path[1] == '\0' ? "" : "/",
6040 97a02382 2023-07-10 thomas got_tree_entry_get_name(te)) == -1) {
6041 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
6042 97a02382 2023-07-10 thomas goto done;
6044 97a02382 2023-07-10 thomas err = print_tree(child_path, commit, show_ids, 1,
6045 97a02382 2023-07-10 thomas root_path, repo);
6046 97a02382 2023-07-10 thomas free(child_path);
6047 97a02382 2023-07-10 thomas if (err)
6048 97a02382 2023-07-10 thomas goto done;
6052 97a02382 2023-07-10 thomas if (tree)
6053 97a02382 2023-07-10 thomas got_object_tree_close(tree);
6054 97a02382 2023-07-10 thomas free(tree_id);
6055 97a02382 2023-07-10 thomas return err;
6058 97a02382 2023-07-10 thomas static const struct got_error *
6059 97a02382 2023-07-10 thomas cmd_tree(int argc, char *argv[])
6061 97a02382 2023-07-10 thomas const struct got_error *error;
6062 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
6063 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
6064 97a02382 2023-07-10 thomas const char *path, *refname = NULL;
6065 97a02382 2023-07-10 thomas char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6066 97a02382 2023-07-10 thomas struct got_object_id *commit_id = NULL;
6067 97a02382 2023-07-10 thomas struct got_commit_object *commit = NULL;
6068 97a02382 2023-07-10 thomas char *commit_id_str = NULL;
6069 97a02382 2023-07-10 thomas int show_ids = 0, recurse = 0;
6071 97a02382 2023-07-10 thomas int *pack_fds = NULL;
6073 97a02382 2023-07-10 thomas #ifndef PROFILE
6074 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6075 97a02382 2023-07-10 thomas NULL) == -1)
6076 97a02382 2023-07-10 thomas err(1, "pledge");
6079 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
6080 97a02382 2023-07-10 thomas switch (ch) {
6081 97a02382 2023-07-10 thomas case 'c':
6082 97a02382 2023-07-10 thomas commit_id_str = optarg;
6084 97a02382 2023-07-10 thomas case 'i':
6085 97a02382 2023-07-10 thomas show_ids = 1;
6087 97a02382 2023-07-10 thomas case 'R':
6088 97a02382 2023-07-10 thomas recurse = 1;
6090 97a02382 2023-07-10 thomas case 'r':
6091 97a02382 2023-07-10 thomas repo_path = realpath(optarg, NULL);
6092 97a02382 2023-07-10 thomas if (repo_path == NULL)
6093 97a02382 2023-07-10 thomas return got_error_from_errno2("realpath",
6094 97a02382 2023-07-10 thomas optarg);
6095 97a02382 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
6097 97a02382 2023-07-10 thomas default:
6098 97a02382 2023-07-10 thomas usage_tree();
6099 97a02382 2023-07-10 thomas /* NOTREACHED */
6103 97a02382 2023-07-10 thomas argc -= optind;
6104 97a02382 2023-07-10 thomas argv += optind;
6106 97a02382 2023-07-10 thomas if (argc == 1)
6107 97a02382 2023-07-10 thomas path = argv[0];
6108 97a02382 2023-07-10 thomas else if (argc > 1)
6109 97a02382 2023-07-10 thomas usage_tree();
6111 97a02382 2023-07-10 thomas path = NULL;
6113 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
6114 97a02382 2023-07-10 thomas if (cwd == NULL) {
6115 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
6116 97a02382 2023-07-10 thomas goto done;
6119 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
6120 97a02382 2023-07-10 thomas if (error != NULL)
6121 97a02382 2023-07-10 thomas goto done;
6123 97a02382 2023-07-10 thomas if (repo_path == NULL) {
6124 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
6125 97a02382 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
6126 97a02382 2023-07-10 thomas goto done;
6128 97a02382 2023-07-10 thomas error = NULL;
6129 97a02382 2023-07-10 thomas if (worktree) {
6130 97a02382 2023-07-10 thomas repo_path =
6131 97a02382 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree));
6132 97a02382 2023-07-10 thomas if (repo_path == NULL)
6133 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
6134 97a02382 2023-07-10 thomas if (error)
6135 97a02382 2023-07-10 thomas goto done;
6136 97a02382 2023-07-10 thomas } else {
6137 97a02382 2023-07-10 thomas repo_path = strdup(cwd);
6138 97a02382 2023-07-10 thomas if (repo_path == NULL) {
6139 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
6140 97a02382 2023-07-10 thomas goto done;
6145 97a02382 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6146 97a02382 2023-07-10 thomas if (error != NULL)
6147 97a02382 2023-07-10 thomas goto done;
6149 97a02382 2023-07-10 thomas if (worktree) {
6150 97a02382 2023-07-10 thomas const char *prefix = got_worktree_get_path_prefix(worktree);
6151 97a02382 2023-07-10 thomas char *p;
6153 97a02382 2023-07-10 thomas if (path == NULL || got_path_is_root_dir(path))
6154 97a02382 2023-07-10 thomas path = "";
6155 97a02382 2023-07-10 thomas error = got_worktree_resolve_path(&p, worktree, path);
6156 97a02382 2023-07-10 thomas if (error)
6157 97a02382 2023-07-10 thomas goto done;
6158 97a02382 2023-07-10 thomas if (asprintf(&in_repo_path, "%s%s%s", prefix,
6159 97a02382 2023-07-10 thomas (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
6160 97a02382 2023-07-10 thomas p) == -1) {
6161 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
6162 97a02382 2023-07-10 thomas free(p);
6163 97a02382 2023-07-10 thomas goto done;
6165 97a02382 2023-07-10 thomas free(p);
6166 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6167 97a02382 2023-07-10 thomas if (error)
6168 97a02382 2023-07-10 thomas goto done;
6169 97a02382 2023-07-10 thomas } else {
6170 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6171 97a02382 2023-07-10 thomas if (error)
6172 97a02382 2023-07-10 thomas goto done;
6173 97a02382 2023-07-10 thomas if (path == NULL)
6174 97a02382 2023-07-10 thomas path = "/";
6175 97a02382 2023-07-10 thomas error = got_repo_map_path(&in_repo_path, repo, path);
6176 97a02382 2023-07-10 thomas if (error != NULL)
6177 97a02382 2023-07-10 thomas goto done;
6180 97a02382 2023-07-10 thomas if (commit_id_str == NULL) {
6181 97a02382 2023-07-10 thomas struct got_reference *head_ref;
6182 97a02382 2023-07-10 thomas if (worktree)
6183 97a02382 2023-07-10 thomas refname = got_worktree_get_head_ref_name(worktree);
6185 97a02382 2023-07-10 thomas refname = GOT_REF_HEAD;
6186 97a02382 2023-07-10 thomas error = got_ref_open(&head_ref, repo, refname, 0);
6187 97a02382 2023-07-10 thomas if (error != NULL)
6188 97a02382 2023-07-10 thomas goto done;
6189 97a02382 2023-07-10 thomas error = got_ref_resolve(&commit_id, repo, head_ref);
6190 97a02382 2023-07-10 thomas got_ref_close(head_ref);
6191 97a02382 2023-07-10 thomas if (error != NULL)
6192 97a02382 2023-07-10 thomas goto done;
6193 97a02382 2023-07-10 thomas } else {
6194 97a02382 2023-07-10 thomas struct got_reflist_head refs;
6195 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
6196 97a02382 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6198 97a02382 2023-07-10 thomas if (error)
6199 97a02382 2023-07-10 thomas goto done;
6200 97a02382 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
6201 97a02382 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6202 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
6203 97a02382 2023-07-10 thomas if (error)
6204 97a02382 2023-07-10 thomas goto done;
6207 97a02382 2023-07-10 thomas if (worktree) {
6208 97a02382 2023-07-10 thomas /* Release work tree lock. */
6209 97a02382 2023-07-10 thomas got_worktree_close(worktree);
6210 97a02382 2023-07-10 thomas worktree = NULL;
6213 97a02382 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6214 97a02382 2023-07-10 thomas if (error)
6215 97a02382 2023-07-10 thomas goto done;
6217 97a02382 2023-07-10 thomas error = print_tree(in_repo_path, commit, show_ids, recurse,
6218 97a02382 2023-07-10 thomas in_repo_path, repo);
6220 97a02382 2023-07-10 thomas free(in_repo_path);
6221 97a02382 2023-07-10 thomas free(repo_path);
6222 97a02382 2023-07-10 thomas free(cwd);
6223 97a02382 2023-07-10 thomas free(commit_id);
6224 97a02382 2023-07-10 thomas if (commit)
6225 97a02382 2023-07-10 thomas got_object_commit_close(commit);
6226 97a02382 2023-07-10 thomas if (worktree)
6227 97a02382 2023-07-10 thomas got_worktree_close(worktree);
6228 97a02382 2023-07-10 thomas if (repo) {
6229 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
6230 97a02382 2023-07-10 thomas if (error == NULL)
6231 97a02382 2023-07-10 thomas error = close_err;
6233 97a02382 2023-07-10 thomas if (pack_fds) {
6234 97a02382 2023-07-10 thomas const struct got_error *pack_err =
6235 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
6236 97a02382 2023-07-10 thomas if (error == NULL)
6237 97a02382 2023-07-10 thomas error = pack_err;
6239 97a02382 2023-07-10 thomas return error;
6242 97a02382 2023-07-10 thomas __dead static void
6243 97a02382 2023-07-10 thomas usage_status(void)
6245 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
6246 97a02382 2023-07-10 thomas "[-s status-codes] [path ...]\n", getprogname());
6247 97a02382 2023-07-10 thomas exit(1);
6250 97a02382 2023-07-10 thomas struct got_status_arg {
6251 97a02382 2023-07-10 thomas char *status_codes;
6252 97a02382 2023-07-10 thomas int suppress;
6255 97a02382 2023-07-10 thomas static const struct got_error *
6256 97a02382 2023-07-10 thomas print_status(void *arg, unsigned char status, unsigned char staged_status,
6257 97a02382 2023-07-10 thomas const char *path, struct got_object_id *blob_id,
6258 97a02382 2023-07-10 thomas struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6259 97a02382 2023-07-10 thomas int dirfd, const char *de_name)
6261 97a02382 2023-07-10 thomas struct got_status_arg *st = arg;
6263 97a02382 2023-07-10 thomas if (status == staged_status && (status == GOT_STATUS_DELETE))
6264 97a02382 2023-07-10 thomas status = GOT_STATUS_NO_CHANGE;
6265 97a02382 2023-07-10 thomas if (st != NULL && st->status_codes) {
6266 97a02382 2023-07-10 thomas size_t ncodes = strlen(st->status_codes);
6267 97a02382 2023-07-10 thomas int i, j = 0;
6269 97a02382 2023-07-10 thomas for (i = 0; i < ncodes ; i++) {
6270 97a02382 2023-07-10 thomas if (st->suppress) {
6271 97a02382 2023-07-10 thomas if (status == st->status_codes[i] ||
6272 97a02382 2023-07-10 thomas staged_status == st->status_codes[i]) {
6274 97a02382 2023-07-10 thomas continue;
6276 97a02382 2023-07-10 thomas } else {
6277 97a02382 2023-07-10 thomas if (status == st->status_codes[i] ||
6278 97a02382 2023-07-10 thomas staged_status == st->status_codes[i])
6283 97a02382 2023-07-10 thomas if (st->suppress && j == 0)
6284 97a02382 2023-07-10 thomas goto print;
6286 97a02382 2023-07-10 thomas if (i == ncodes)
6287 97a02382 2023-07-10 thomas return NULL;
6290 97a02382 2023-07-10 thomas printf("%c%c %s\n", status, staged_status, path);
6291 97a02382 2023-07-10 thomas return NULL;
6294 97a02382 2023-07-10 thomas static const struct got_error *
6295 97a02382 2023-07-10 thomas cmd_status(int argc, char *argv[])
6297 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
6298 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
6299 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
6300 97a02382 2023-07-10 thomas struct got_status_arg st;
6301 97a02382 2023-07-10 thomas char *cwd = NULL;
6302 97a02382 2023-07-10 thomas struct got_pathlist_head paths;
6303 97a02382 2023-07-10 thomas int ch, i, no_ignores = 0;
6304 97a02382 2023-07-10 thomas int *pack_fds = NULL;
6306 97a02382 2023-07-10 thomas TAILQ_INIT(&paths);
6308 97a02382 2023-07-10 thomas memset(&st, 0, sizeof(st));
6309 97a02382 2023-07-10 thomas st.status_codes = NULL;
6310 97a02382 2023-07-10 thomas st.suppress = 0;
6312 97a02382 2023-07-10 thomas #ifndef PROFILE
6313 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6314 97a02382 2023-07-10 thomas NULL) == -1)
6315 97a02382 2023-07-10 thomas err(1, "pledge");
6318 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6319 97a02382 2023-07-10 thomas switch (ch) {
6320 97a02382 2023-07-10 thomas case 'I':
6321 97a02382 2023-07-10 thomas no_ignores = 1;
6323 97a02382 2023-07-10 thomas case 'S':
6324 97a02382 2023-07-10 thomas if (st.status_codes != NULL && st.suppress == 0)
6325 97a02382 2023-07-10 thomas option_conflict('S', 's');
6326 97a02382 2023-07-10 thomas st.suppress = 1;
6327 97a02382 2023-07-10 thomas /* fallthrough */
6328 97a02382 2023-07-10 thomas case 's':
6329 97a02382 2023-07-10 thomas for (i = 0; optarg[i] != '\0'; i++) {
6330 97a02382 2023-07-10 thomas switch (optarg[i]) {
6331 97a02382 2023-07-10 thomas case GOT_STATUS_MODIFY:
6332 97a02382 2023-07-10 thomas case GOT_STATUS_ADD:
6333 97a02382 2023-07-10 thomas case GOT_STATUS_DELETE:
6334 97a02382 2023-07-10 thomas case GOT_STATUS_CONFLICT:
6335 97a02382 2023-07-10 thomas case GOT_STATUS_MISSING:
6336 97a02382 2023-07-10 thomas case GOT_STATUS_OBSTRUCTED:
6337 97a02382 2023-07-10 thomas case GOT_STATUS_UNVERSIONED:
6338 97a02382 2023-07-10 thomas case GOT_STATUS_MODE_CHANGE:
6339 97a02382 2023-07-10 thomas case GOT_STATUS_NONEXISTENT:
6341 97a02382 2023-07-10 thomas default:
6342 97a02382 2023-07-10 thomas errx(1, "invalid status code '%c'",
6343 97a02382 2023-07-10 thomas optarg[i]);
6346 97a02382 2023-07-10 thomas if (ch == 's' && st.suppress)
6347 97a02382 2023-07-10 thomas option_conflict('s', 'S');
6348 97a02382 2023-07-10 thomas st.status_codes = optarg;
6350 97a02382 2023-07-10 thomas default:
6351 97a02382 2023-07-10 thomas usage_status();
6352 97a02382 2023-07-10 thomas /* NOTREACHED */
6356 97a02382 2023-07-10 thomas argc -= optind;
6357 97a02382 2023-07-10 thomas argv += optind;
6359 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
6360 97a02382 2023-07-10 thomas if (cwd == NULL) {
6361 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
6362 97a02382 2023-07-10 thomas goto done;
6365 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
6366 97a02382 2023-07-10 thomas if (error != NULL)
6367 97a02382 2023-07-10 thomas goto done;
6369 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
6370 97a02382 2023-07-10 thomas if (error) {
6371 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
6372 97a02382 2023-07-10 thomas error = wrap_not_worktree_error(error, "status", cwd);
6373 97a02382 2023-07-10 thomas goto done;
6376 97a02382 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6377 97a02382 2023-07-10 thomas NULL, pack_fds);
6378 97a02382 2023-07-10 thomas if (error != NULL)
6379 97a02382 2023-07-10 thomas goto done;
6381 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1,
6382 97a02382 2023-07-10 thomas got_worktree_get_root_path(worktree));
6383 97a02382 2023-07-10 thomas if (error)
6384 97a02382 2023-07-10 thomas goto done;
6386 97a02382 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6387 97a02382 2023-07-10 thomas if (error)
6388 97a02382 2023-07-10 thomas goto done;
6390 97a02382 2023-07-10 thomas error = got_worktree_status(worktree, &paths, repo, no_ignores,
6391 97a02382 2023-07-10 thomas print_status, &st, check_cancelled, NULL);
6393 97a02382 2023-07-10 thomas if (pack_fds) {
6394 97a02382 2023-07-10 thomas const struct got_error *pack_err =
6395 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
6396 97a02382 2023-07-10 thomas if (error == NULL)
6397 97a02382 2023-07-10 thomas error = pack_err;
6399 97a02382 2023-07-10 thomas if (repo) {
6400 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
6401 97a02382 2023-07-10 thomas if (error == NULL)
6402 97a02382 2023-07-10 thomas error = close_err;
6405 97a02382 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6406 97a02382 2023-07-10 thomas free(cwd);
6407 97a02382 2023-07-10 thomas return error;
6410 97a02382 2023-07-10 thomas __dead static void
6411 97a02382 2023-07-10 thomas usage_ref(void)
6413 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6414 97a02382 2023-07-10 thomas "[-s reference] [name]\n", getprogname());
6415 97a02382 2023-07-10 thomas exit(1);
6418 97a02382 2023-07-10 thomas static const struct got_error *
6419 97a02382 2023-07-10 thomas list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6421 97a02382 2023-07-10 thomas static const struct got_error *err = NULL;
6422 97a02382 2023-07-10 thomas struct got_reflist_head refs;
6423 97a02382 2023-07-10 thomas struct got_reflist_entry *re;
6425 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
6426 97a02382 2023-07-10 thomas err = got_ref_list(&refs, repo, refname, sort_by_time ?
6427 97a02382 2023-07-10 thomas got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6429 97a02382 2023-07-10 thomas if (err)
6430 97a02382 2023-07-10 thomas return err;
6432 97a02382 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
6433 97a02382 2023-07-10 thomas char *refstr;
6434 97a02382 2023-07-10 thomas refstr = got_ref_to_str(re->ref);
6435 97a02382 2023-07-10 thomas if (refstr == NULL) {
6436 97a02382 2023-07-10 thomas err = got_error_from_errno("got_ref_to_str");
6439 97a02382 2023-07-10 thomas printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6440 97a02382 2023-07-10 thomas free(refstr);
6443 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
6444 97a02382 2023-07-10 thomas return err;
6447 97a02382 2023-07-10 thomas static const struct got_error *
6448 97a02382 2023-07-10 thomas delete_ref_by_name(struct got_repository *repo, const char *refname)
6450 97a02382 2023-07-10 thomas const struct got_error *err;
6451 97a02382 2023-07-10 thomas struct got_reference *ref;
6453 97a02382 2023-07-10 thomas err = got_ref_open(&ref, repo, refname, 0);
6454 97a02382 2023-07-10 thomas if (err)
6455 97a02382 2023-07-10 thomas return err;
6457 97a02382 2023-07-10 thomas err = delete_ref(repo, ref);
6458 97a02382 2023-07-10 thomas got_ref_close(ref);
6459 97a02382 2023-07-10 thomas return err;
6462 97a02382 2023-07-10 thomas static const struct got_error *
6463 97a02382 2023-07-10 thomas add_ref(struct got_repository *repo, const char *refname, const char *target)
6465 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
6466 97a02382 2023-07-10 thomas struct got_object_id *id = NULL;
6467 97a02382 2023-07-10 thomas struct got_reference *ref = NULL;
6468 97a02382 2023-07-10 thomas struct got_reflist_head refs;
6471 97a02382 2023-07-10 thomas * Don't let the user create a reference name with a leading '-'.
6472 97a02382 2023-07-10 thomas * While technically a valid reference name, this case is usually
6473 97a02382 2023-07-10 thomas * an unintended typo.
6475 97a02382 2023-07-10 thomas if (refname[0] == '-')
6476 97a02382 2023-07-10 thomas return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6478 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
6479 97a02382 2023-07-10 thomas err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6480 97a02382 2023-07-10 thomas if (err)
6481 97a02382 2023-07-10 thomas goto done;
6482 97a02382 2023-07-10 thomas err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6483 97a02382 2023-07-10 thomas &refs, repo);
6484 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
6485 97a02382 2023-07-10 thomas if (err)
6486 97a02382 2023-07-10 thomas goto done;
6488 97a02382 2023-07-10 thomas err = got_ref_alloc(&ref, refname, id);
6489 97a02382 2023-07-10 thomas if (err)
6490 97a02382 2023-07-10 thomas goto done;
6492 97a02382 2023-07-10 thomas err = got_ref_write(ref, repo);
6494 97a02382 2023-07-10 thomas if (ref)
6495 97a02382 2023-07-10 thomas got_ref_close(ref);
6496 97a02382 2023-07-10 thomas free(id);
6497 97a02382 2023-07-10 thomas return err;
6500 97a02382 2023-07-10 thomas static const struct got_error *
6501 97a02382 2023-07-10 thomas add_symref(struct got_repository *repo, const char *refname, const char *target)
6503 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
6504 97a02382 2023-07-10 thomas struct got_reference *ref = NULL;
6505 97a02382 2023-07-10 thomas struct got_reference *target_ref = NULL;
6508 97a02382 2023-07-10 thomas * Don't let the user create a reference name with a leading '-'.
6509 97a02382 2023-07-10 thomas * While technically a valid reference name, this case is usually
6510 97a02382 2023-07-10 thomas * an unintended typo.
6512 97a02382 2023-07-10 thomas if (refname[0] == '-')
6513 97a02382 2023-07-10 thomas return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6515 97a02382 2023-07-10 thomas err = got_ref_open(&target_ref, repo, target, 0);
6516 97a02382 2023-07-10 thomas if (err)
6517 97a02382 2023-07-10 thomas return err;
6519 97a02382 2023-07-10 thomas err = got_ref_alloc_symref(&ref, refname, target_ref);
6520 97a02382 2023-07-10 thomas if (err)
6521 97a02382 2023-07-10 thomas goto done;
6523 97a02382 2023-07-10 thomas err = got_ref_write(ref, repo);
6525 97a02382 2023-07-10 thomas if (target_ref)
6526 97a02382 2023-07-10 thomas got_ref_close(target_ref);
6527 97a02382 2023-07-10 thomas if (ref)
6528 97a02382 2023-07-10 thomas got_ref_close(ref);
6529 97a02382 2023-07-10 thomas return err;
6532 97a02382 2023-07-10 thomas static const struct got_error *
6533 97a02382 2023-07-10 thomas cmd_ref(int argc, char *argv[])
6535 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
6536 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
6537 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
6538 97a02382 2023-07-10 thomas char *cwd = NULL, *repo_path = NULL;
6539 97a02382 2023-07-10 thomas int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6540 97a02382 2023-07-10 thomas const char *obj_arg = NULL, *symref_target= NULL;
6541 97a02382 2023-07-10 thomas char *refname = NULL;
6542 97a02382 2023-07-10 thomas int *pack_fds = NULL;
6544 97a02382 2023-07-10 thomas #ifndef PROFILE
6545 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6546 97a02382 2023-07-10 thomas "sendfd unveil", NULL) == -1)
6547 97a02382 2023-07-10 thomas err(1, "pledge");
6550 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6551 97a02382 2023-07-10 thomas switch (ch) {
6552 97a02382 2023-07-10 thomas case 'c':
6553 97a02382 2023-07-10 thomas obj_arg = optarg;
6555 97a02382 2023-07-10 thomas case 'd':
6556 97a02382 2023-07-10 thomas do_delete = 1;
6558 97a02382 2023-07-10 thomas case 'l':
6559 97a02382 2023-07-10 thomas do_list = 1;
6561 97a02382 2023-07-10 thomas case 'r':
6562 97a02382 2023-07-10 thomas repo_path = realpath(optarg, NULL);
6563 97a02382 2023-07-10 thomas if (repo_path == NULL)
6564 97a02382 2023-07-10 thomas return got_error_from_errno2("realpath",
6565 97a02382 2023-07-10 thomas optarg);
6566 97a02382 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
6568 97a02382 2023-07-10 thomas case 's':
6569 97a02382 2023-07-10 thomas symref_target = optarg;
6571 97a02382 2023-07-10 thomas case 't':
6572 97a02382 2023-07-10 thomas sort_by_time = 1;
6574 97a02382 2023-07-10 thomas default:
6575 97a02382 2023-07-10 thomas usage_ref();
6576 97a02382 2023-07-10 thomas /* NOTREACHED */
6580 97a02382 2023-07-10 thomas if (obj_arg && do_list)
6581 97a02382 2023-07-10 thomas option_conflict('c', 'l');
6582 97a02382 2023-07-10 thomas if (obj_arg && do_delete)
6583 97a02382 2023-07-10 thomas option_conflict('c', 'd');
6584 97a02382 2023-07-10 thomas if (obj_arg && symref_target)
6585 97a02382 2023-07-10 thomas option_conflict('c', 's');
6586 97a02382 2023-07-10 thomas if (symref_target && do_delete)
6587 97a02382 2023-07-10 thomas option_conflict('s', 'd');
6588 97a02382 2023-07-10 thomas if (symref_target && do_list)
6589 97a02382 2023-07-10 thomas option_conflict('s', 'l');
6590 97a02382 2023-07-10 thomas if (do_delete && do_list)
6591 97a02382 2023-07-10 thomas option_conflict('d', 'l');
6592 97a02382 2023-07-10 thomas if (sort_by_time && !do_list)
6593 97a02382 2023-07-10 thomas errx(1, "-t option requires -l option");
6595 97a02382 2023-07-10 thomas argc -= optind;
6596 97a02382 2023-07-10 thomas argv += optind;
6598 97a02382 2023-07-10 thomas if (do_list) {
6599 97a02382 2023-07-10 thomas if (argc != 0 && argc != 1)
6600 97a02382 2023-07-10 thomas usage_ref();
6601 97a02382 2023-07-10 thomas if (argc == 1) {
6602 97a02382 2023-07-10 thomas refname = strdup(argv[0]);
6603 97a02382 2023-07-10 thomas if (refname == NULL) {
6604 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
6605 97a02382 2023-07-10 thomas goto done;
6608 97a02382 2023-07-10 thomas } else {
6609 97a02382 2023-07-10 thomas if (argc != 1)
6610 97a02382 2023-07-10 thomas usage_ref();
6611 97a02382 2023-07-10 thomas refname = strdup(argv[0]);
6612 97a02382 2023-07-10 thomas if (refname == NULL) {
6613 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
6614 97a02382 2023-07-10 thomas goto done;
6618 97a02382 2023-07-10 thomas if (refname)
6619 97a02382 2023-07-10 thomas got_path_strip_trailing_slashes(refname);
6621 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
6622 97a02382 2023-07-10 thomas if (cwd == NULL) {
6623 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
6624 97a02382 2023-07-10 thomas goto done;
6627 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
6628 97a02382 2023-07-10 thomas if (error != NULL)
6629 97a02382 2023-07-10 thomas goto done;
6631 97a02382 2023-07-10 thomas if (repo_path == NULL) {
6632 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
6633 97a02382 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
6634 97a02382 2023-07-10 thomas goto done;
6636 97a02382 2023-07-10 thomas error = NULL;
6637 97a02382 2023-07-10 thomas if (worktree) {
6638 97a02382 2023-07-10 thomas repo_path =
6639 97a02382 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree));
6640 97a02382 2023-07-10 thomas if (repo_path == NULL)
6641 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
6642 97a02382 2023-07-10 thomas if (error)
6643 97a02382 2023-07-10 thomas goto done;
6644 97a02382 2023-07-10 thomas } else {
6645 97a02382 2023-07-10 thomas repo_path = strdup(cwd);
6646 97a02382 2023-07-10 thomas if (repo_path == NULL) {
6647 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
6648 97a02382 2023-07-10 thomas goto done;
6653 97a02382 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6654 97a02382 2023-07-10 thomas if (error != NULL)
6655 97a02382 2023-07-10 thomas goto done;
6657 97a02382 2023-07-10 thomas #ifndef PROFILE
6658 97a02382 2023-07-10 thomas if (do_list) {
6659 97a02382 2023-07-10 thomas /* Remove "cpath" promise. */
6660 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6661 97a02382 2023-07-10 thomas NULL) == -1)
6662 97a02382 2023-07-10 thomas err(1, "pledge");
6666 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), do_list,
6667 97a02382 2023-07-10 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
6668 97a02382 2023-07-10 thomas if (error)
6669 97a02382 2023-07-10 thomas goto done;
6671 97a02382 2023-07-10 thomas if (do_list)
6672 97a02382 2023-07-10 thomas error = list_refs(repo, refname, sort_by_time);
6673 97a02382 2023-07-10 thomas else if (do_delete)
6674 97a02382 2023-07-10 thomas error = delete_ref_by_name(repo, refname);
6675 97a02382 2023-07-10 thomas else if (symref_target)
6676 97a02382 2023-07-10 thomas error = add_symref(repo, refname, symref_target);
6678 97a02382 2023-07-10 thomas if (obj_arg == NULL)
6679 97a02382 2023-07-10 thomas usage_ref();
6680 97a02382 2023-07-10 thomas error = add_ref(repo, refname, obj_arg);
6683 97a02382 2023-07-10 thomas free(refname);
6684 97a02382 2023-07-10 thomas if (repo) {
6685 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
6686 97a02382 2023-07-10 thomas if (error == NULL)
6687 97a02382 2023-07-10 thomas error = close_err;
6689 97a02382 2023-07-10 thomas if (worktree)
6690 97a02382 2023-07-10 thomas got_worktree_close(worktree);
6691 97a02382 2023-07-10 thomas if (pack_fds) {
6692 97a02382 2023-07-10 thomas const struct got_error *pack_err =
6693 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
6694 97a02382 2023-07-10 thomas if (error == NULL)
6695 97a02382 2023-07-10 thomas error = pack_err;
6697 97a02382 2023-07-10 thomas free(cwd);
6698 97a02382 2023-07-10 thomas free(repo_path);
6699 97a02382 2023-07-10 thomas return error;
6702 97a02382 2023-07-10 thomas __dead static void
6703 97a02382 2023-07-10 thomas usage_branch(void)
6705 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6706 97a02382 2023-07-10 thomas "[-r repository-path] [name]\n", getprogname());
6707 97a02382 2023-07-10 thomas exit(1);
6710 97a02382 2023-07-10 thomas static const struct got_error *
6711 97a02382 2023-07-10 thomas list_branch(struct got_repository *repo, struct got_worktree *worktree,
6712 97a02382 2023-07-10 thomas struct got_reference *ref)
6714 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
6715 97a02382 2023-07-10 thomas const char *refname, *marker = " ";
6716 97a02382 2023-07-10 thomas char *refstr;
6718 97a02382 2023-07-10 thomas refname = got_ref_get_name(ref);
6719 97a02382 2023-07-10 thomas if (worktree && strcmp(refname,
6720 97a02382 2023-07-10 thomas got_worktree_get_head_ref_name(worktree)) == 0) {
6721 97a02382 2023-07-10 thomas struct got_object_id *id = NULL;
6723 97a02382 2023-07-10 thomas err = got_ref_resolve(&id, repo, ref);
6724 97a02382 2023-07-10 thomas if (err)
6725 97a02382 2023-07-10 thomas return err;
6726 97a02382 2023-07-10 thomas if (got_object_id_cmp(id,
6727 97a02382 2023-07-10 thomas got_worktree_get_base_commit_id(worktree)) == 0)
6728 97a02382 2023-07-10 thomas marker = "* ";
6730 97a02382 2023-07-10 thomas marker = "~ ";
6731 97a02382 2023-07-10 thomas free(id);
6734 97a02382 2023-07-10 thomas if (strncmp(refname, "refs/heads/", 11) == 0)
6735 97a02382 2023-07-10 thomas refname += 11;
6736 97a02382 2023-07-10 thomas if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6737 97a02382 2023-07-10 thomas refname += 18;
6738 97a02382 2023-07-10 thomas if (strncmp(refname, "refs/remotes/", 13) == 0)
6739 97a02382 2023-07-10 thomas refname += 13;
6741 97a02382 2023-07-10 thomas refstr = got_ref_to_str(ref);
6742 97a02382 2023-07-10 thomas if (refstr == NULL)
6743 97a02382 2023-07-10 thomas return got_error_from_errno("got_ref_to_str");
6745 97a02382 2023-07-10 thomas printf("%s%s: %s\n", marker, refname, refstr);
6746 97a02382 2023-07-10 thomas free(refstr);
6747 97a02382 2023-07-10 thomas return NULL;
6750 97a02382 2023-07-10 thomas static const struct got_error *
6751 97a02382 2023-07-10 thomas show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6753 97a02382 2023-07-10 thomas const char *refname;
6755 97a02382 2023-07-10 thomas if (worktree == NULL)
6756 97a02382 2023-07-10 thomas return got_error(GOT_ERR_NOT_WORKTREE);
6758 97a02382 2023-07-10 thomas refname = got_worktree_get_head_ref_name(worktree);
6760 97a02382 2023-07-10 thomas if (strncmp(refname, "refs/heads/", 11) == 0)
6761 97a02382 2023-07-10 thomas refname += 11;
6762 97a02382 2023-07-10 thomas if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6763 97a02382 2023-07-10 thomas refname += 18;
6765 97a02382 2023-07-10 thomas printf("%s\n", refname);
6767 97a02382 2023-07-10 thomas return NULL;
6770 97a02382 2023-07-10 thomas static const struct got_error *
6771 97a02382 2023-07-10 thomas list_branches(struct got_repository *repo, struct got_worktree *worktree,
6772 97a02382 2023-07-10 thomas int sort_by_time)
6774 97a02382 2023-07-10 thomas static const struct got_error *err = NULL;
6775 97a02382 2023-07-10 thomas struct got_reflist_head refs;
6776 97a02382 2023-07-10 thomas struct got_reflist_entry *re;
6777 97a02382 2023-07-10 thomas struct got_reference *temp_ref = NULL;
6778 97a02382 2023-07-10 thomas int rebase_in_progress, histedit_in_progress;
6780 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
6782 97a02382 2023-07-10 thomas if (worktree) {
6783 97a02382 2023-07-10 thomas err = got_worktree_rebase_in_progress(&rebase_in_progress,
6784 97a02382 2023-07-10 thomas worktree);
6785 97a02382 2023-07-10 thomas if (err)
6786 97a02382 2023-07-10 thomas return err;
6788 97a02382 2023-07-10 thomas err = got_worktree_histedit_in_progress(&histedit_in_progress,
6789 97a02382 2023-07-10 thomas worktree);
6790 97a02382 2023-07-10 thomas if (err)
6791 97a02382 2023-07-10 thomas return err;
6793 97a02382 2023-07-10 thomas if (rebase_in_progress || histedit_in_progress) {
6794 97a02382 2023-07-10 thomas err = got_ref_open(&temp_ref, repo,
6795 97a02382 2023-07-10 thomas got_worktree_get_head_ref_name(worktree), 0);
6796 97a02382 2023-07-10 thomas if (err)
6797 97a02382 2023-07-10 thomas return err;
6798 97a02382 2023-07-10 thomas list_branch(repo, worktree, temp_ref);
6799 97a02382 2023-07-10 thomas got_ref_close(temp_ref);
6803 97a02382 2023-07-10 thomas err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6804 97a02382 2023-07-10 thomas got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6806 97a02382 2023-07-10 thomas if (err)
6807 97a02382 2023-07-10 thomas return err;
6809 97a02382 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry)
6810 97a02382 2023-07-10 thomas list_branch(repo, worktree, re->ref);
6812 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
6814 97a02382 2023-07-10 thomas err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6815 97a02382 2023-07-10 thomas got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6817 97a02382 2023-07-10 thomas if (err)
6818 97a02382 2023-07-10 thomas return err;
6820 97a02382 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry)
6821 97a02382 2023-07-10 thomas list_branch(repo, worktree, re->ref);
6823 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
6825 97a02382 2023-07-10 thomas return NULL;
6828 97a02382 2023-07-10 thomas static const struct got_error *
6829 97a02382 2023-07-10 thomas delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6830 97a02382 2023-07-10 thomas const char *branch_name)
6832 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
6833 97a02382 2023-07-10 thomas struct got_reference *ref = NULL;
6834 97a02382 2023-07-10 thomas char *refname, *remote_refname = NULL;
6836 97a02382 2023-07-10 thomas if (strncmp(branch_name, "refs/", 5) == 0)
6837 97a02382 2023-07-10 thomas branch_name += 5;
6838 97a02382 2023-07-10 thomas if (strncmp(branch_name, "heads/", 6) == 0)
6839 97a02382 2023-07-10 thomas branch_name += 6;
6840 97a02382 2023-07-10 thomas else if (strncmp(branch_name, "remotes/", 8) == 0)
6841 97a02382 2023-07-10 thomas branch_name += 8;
6843 97a02382 2023-07-10 thomas if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6844 97a02382 2023-07-10 thomas return got_error_from_errno("asprintf");
6846 97a02382 2023-07-10 thomas if (asprintf(&remote_refname, "refs/remotes/%s",
6847 97a02382 2023-07-10 thomas branch_name) == -1) {
6848 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
6849 97a02382 2023-07-10 thomas goto done;
6852 97a02382 2023-07-10 thomas err = got_ref_open(&ref, repo, refname, 0);
6853 97a02382 2023-07-10 thomas if (err) {
6854 97a02382 2023-07-10 thomas const struct got_error *err2;
6855 97a02382 2023-07-10 thomas if (err->code != GOT_ERR_NOT_REF)
6856 97a02382 2023-07-10 thomas goto done;
6858 97a02382 2023-07-10 thomas * Keep 'err' intact such that if neither branch exists
6859 97a02382 2023-07-10 thomas * we report "refs/heads" rather than "refs/remotes" in
6860 97a02382 2023-07-10 thomas * our error message.
6862 97a02382 2023-07-10 thomas err2 = got_ref_open(&ref, repo, remote_refname, 0);
6863 97a02382 2023-07-10 thomas if (err2)
6864 97a02382 2023-07-10 thomas goto done;
6865 97a02382 2023-07-10 thomas err = NULL;
6868 97a02382 2023-07-10 thomas if (worktree &&
6869 97a02382 2023-07-10 thomas strcmp(got_worktree_get_head_ref_name(worktree),
6870 97a02382 2023-07-10 thomas got_ref_get_name(ref)) == 0) {
6871 97a02382 2023-07-10 thomas err = got_error_msg(GOT_ERR_SAME_BRANCH,
6872 97a02382 2023-07-10 thomas "will not delete this work tree's current branch");
6873 97a02382 2023-07-10 thomas goto done;
6876 97a02382 2023-07-10 thomas err = delete_ref(repo, ref);
6878 97a02382 2023-07-10 thomas if (ref)
6879 97a02382 2023-07-10 thomas got_ref_close(ref);
6880 97a02382 2023-07-10 thomas free(refname);
6881 97a02382 2023-07-10 thomas free(remote_refname);
6882 97a02382 2023-07-10 thomas return err;
6885 97a02382 2023-07-10 thomas static const struct got_error *
6886 97a02382 2023-07-10 thomas add_branch(struct got_repository *repo, const char *branch_name,
6887 97a02382 2023-07-10 thomas struct got_object_id *base_commit_id)
6889 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
6890 97a02382 2023-07-10 thomas struct got_reference *ref = NULL;
6891 97a02382 2023-07-10 thomas char *refname = NULL;
6894 97a02382 2023-07-10 thomas * Don't let the user create a branch name with a leading '-'.
6895 97a02382 2023-07-10 thomas * While technically a valid reference name, this case is usually
6896 97a02382 2023-07-10 thomas * an unintended typo.
6898 97a02382 2023-07-10 thomas if (branch_name[0] == '-')
6899 97a02382 2023-07-10 thomas return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6901 97a02382 2023-07-10 thomas if (strncmp(branch_name, "refs/heads/", 11) == 0)
6902 97a02382 2023-07-10 thomas branch_name += 11;
6904 97a02382 2023-07-10 thomas if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6905 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
6906 97a02382 2023-07-10 thomas goto done;
6909 97a02382 2023-07-10 thomas err = got_ref_open(&ref, repo, refname, 0);
6910 97a02382 2023-07-10 thomas if (err == NULL) {
6911 97a02382 2023-07-10 thomas err = got_error(GOT_ERR_BRANCH_EXISTS);
6912 97a02382 2023-07-10 thomas goto done;
6913 97a02382 2023-07-10 thomas } else if (err->code != GOT_ERR_NOT_REF)
6914 97a02382 2023-07-10 thomas goto done;
6916 97a02382 2023-07-10 thomas err = got_ref_alloc(&ref, refname, base_commit_id);
6917 97a02382 2023-07-10 thomas if (err)
6918 97a02382 2023-07-10 thomas goto done;
6920 97a02382 2023-07-10 thomas err = got_ref_write(ref, repo);
6922 97a02382 2023-07-10 thomas if (ref)
6923 97a02382 2023-07-10 thomas got_ref_close(ref);
6924 97a02382 2023-07-10 thomas free(refname);
6925 97a02382 2023-07-10 thomas return err;
6928 97a02382 2023-07-10 thomas static const struct got_error *
6929 97a02382 2023-07-10 thomas cmd_branch(int argc, char *argv[])
6931 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
6932 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
6933 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
6934 97a02382 2023-07-10 thomas char *cwd = NULL, *repo_path = NULL;
6935 97a02382 2023-07-10 thomas int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6936 97a02382 2023-07-10 thomas const char *delref = NULL, *commit_id_arg = NULL;
6937 97a02382 2023-07-10 thomas struct got_reference *ref = NULL;
6938 97a02382 2023-07-10 thomas struct got_pathlist_head paths;
6939 97a02382 2023-07-10 thomas struct got_object_id *commit_id = NULL;
6940 97a02382 2023-07-10 thomas char *commit_id_str = NULL;
6941 97a02382 2023-07-10 thomas int *pack_fds = NULL;
6943 97a02382 2023-07-10 thomas TAILQ_INIT(&paths);
6945 97a02382 2023-07-10 thomas #ifndef PROFILE
6946 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6947 97a02382 2023-07-10 thomas "sendfd unveil", NULL) == -1)
6948 97a02382 2023-07-10 thomas err(1, "pledge");
6951 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
6952 97a02382 2023-07-10 thomas switch (ch) {
6953 97a02382 2023-07-10 thomas case 'c':
6954 97a02382 2023-07-10 thomas commit_id_arg = optarg;
6956 97a02382 2023-07-10 thomas case 'd':
6957 97a02382 2023-07-10 thomas delref = optarg;
6959 97a02382 2023-07-10 thomas case 'l':
6960 97a02382 2023-07-10 thomas do_list = 1;
6962 97a02382 2023-07-10 thomas case 'n':
6963 97a02382 2023-07-10 thomas do_update = 0;
6965 97a02382 2023-07-10 thomas case 'r':
6966 97a02382 2023-07-10 thomas repo_path = realpath(optarg, NULL);
6967 97a02382 2023-07-10 thomas if (repo_path == NULL)
6968 97a02382 2023-07-10 thomas return got_error_from_errno2("realpath",
6969 97a02382 2023-07-10 thomas optarg);
6970 97a02382 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
6972 97a02382 2023-07-10 thomas case 't':
6973 97a02382 2023-07-10 thomas sort_by_time = 1;
6975 97a02382 2023-07-10 thomas default:
6976 97a02382 2023-07-10 thomas usage_branch();
6977 97a02382 2023-07-10 thomas /* NOTREACHED */
6981 97a02382 2023-07-10 thomas if (do_list && delref)
6982 97a02382 2023-07-10 thomas option_conflict('l', 'd');
6983 97a02382 2023-07-10 thomas if (sort_by_time && !do_list)
6984 97a02382 2023-07-10 thomas errx(1, "-t option requires -l option");
6986 97a02382 2023-07-10 thomas argc -= optind;
6987 97a02382 2023-07-10 thomas argv += optind;
6989 97a02382 2023-07-10 thomas if (!do_list && !delref && argc == 0)
6990 97a02382 2023-07-10 thomas do_show = 1;
6992 97a02382 2023-07-10 thomas if ((do_list || delref || do_show) && commit_id_arg != NULL)
6993 97a02382 2023-07-10 thomas errx(1, "-c option can only be used when creating a branch");
6995 97a02382 2023-07-10 thomas if (do_list || delref) {
6996 97a02382 2023-07-10 thomas if (argc > 0)
6997 97a02382 2023-07-10 thomas usage_branch();
6998 97a02382 2023-07-10 thomas } else if (!do_show && argc != 1)
6999 97a02382 2023-07-10 thomas usage_branch();
7001 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
7002 97a02382 2023-07-10 thomas if (cwd == NULL) {
7003 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
7004 97a02382 2023-07-10 thomas goto done;
7007 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
7008 97a02382 2023-07-10 thomas if (error != NULL)
7009 97a02382 2023-07-10 thomas goto done;
7011 97a02382 2023-07-10 thomas if (repo_path == NULL) {
7012 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
7013 97a02382 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
7014 97a02382 2023-07-10 thomas goto done;
7016 97a02382 2023-07-10 thomas error = NULL;
7017 97a02382 2023-07-10 thomas if (worktree) {
7018 97a02382 2023-07-10 thomas repo_path =
7019 97a02382 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree));
7020 97a02382 2023-07-10 thomas if (repo_path == NULL)
7021 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
7022 97a02382 2023-07-10 thomas if (error)
7023 97a02382 2023-07-10 thomas goto done;
7024 97a02382 2023-07-10 thomas } else {
7025 97a02382 2023-07-10 thomas repo_path = strdup(cwd);
7026 97a02382 2023-07-10 thomas if (repo_path == NULL) {
7027 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
7028 97a02382 2023-07-10 thomas goto done;
7033 97a02382 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7034 97a02382 2023-07-10 thomas if (error != NULL)
7035 97a02382 2023-07-10 thomas goto done;
7037 97a02382 2023-07-10 thomas #ifndef PROFILE
7038 97a02382 2023-07-10 thomas if (do_list || do_show) {
7039 97a02382 2023-07-10 thomas /* Remove "cpath" promise. */
7040 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
7041 97a02382 2023-07-10 thomas NULL) == -1)
7042 97a02382 2023-07-10 thomas err(1, "pledge");
7046 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), do_list,
7047 97a02382 2023-07-10 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
7048 97a02382 2023-07-10 thomas if (error)
7049 97a02382 2023-07-10 thomas goto done;
7051 97a02382 2023-07-10 thomas if (do_show)
7052 97a02382 2023-07-10 thomas error = show_current_branch(repo, worktree);
7053 97a02382 2023-07-10 thomas else if (do_list)
7054 97a02382 2023-07-10 thomas error = list_branches(repo, worktree, sort_by_time);
7055 97a02382 2023-07-10 thomas else if (delref)
7056 97a02382 2023-07-10 thomas error = delete_branch(repo, worktree, delref);
7058 97a02382 2023-07-10 thomas struct got_reflist_head refs;
7059 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
7060 97a02382 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
7062 97a02382 2023-07-10 thomas if (error)
7063 97a02382 2023-07-10 thomas goto done;
7064 97a02382 2023-07-10 thomas if (commit_id_arg == NULL)
7065 97a02382 2023-07-10 thomas commit_id_arg = worktree ?
7066 97a02382 2023-07-10 thomas got_worktree_get_head_ref_name(worktree) :
7067 97a02382 2023-07-10 thomas GOT_REF_HEAD;
7068 97a02382 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
7069 97a02382 2023-07-10 thomas commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
7070 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
7071 97a02382 2023-07-10 thomas if (error)
7072 97a02382 2023-07-10 thomas goto done;
7073 97a02382 2023-07-10 thomas error = add_branch(repo, argv[0], commit_id);
7074 97a02382 2023-07-10 thomas if (error)
7075 97a02382 2023-07-10 thomas goto done;
7076 97a02382 2023-07-10 thomas if (worktree && do_update) {
7077 97a02382 2023-07-10 thomas struct got_update_progress_arg upa;
7078 97a02382 2023-07-10 thomas char *branch_refname = NULL;
7080 97a02382 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
7081 97a02382 2023-07-10 thomas if (error)
7082 97a02382 2023-07-10 thomas goto done;
7083 97a02382 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, 0, NULL,
7084 97a02382 2023-07-10 thomas worktree);
7085 97a02382 2023-07-10 thomas if (error)
7086 97a02382 2023-07-10 thomas goto done;
7087 97a02382 2023-07-10 thomas if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
7088 97a02382 2023-07-10 thomas == -1) {
7089 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
7090 97a02382 2023-07-10 thomas goto done;
7092 97a02382 2023-07-10 thomas error = got_ref_open(&ref, repo, branch_refname, 0);
7093 97a02382 2023-07-10 thomas free(branch_refname);
7094 97a02382 2023-07-10 thomas if (error)
7095 97a02382 2023-07-10 thomas goto done;
7096 97a02382 2023-07-10 thomas error = switch_head_ref(ref, commit_id, worktree,
7098 97a02382 2023-07-10 thomas if (error)
7099 97a02382 2023-07-10 thomas goto done;
7100 97a02382 2023-07-10 thomas error = got_worktree_set_base_commit_id(worktree, repo,
7101 97a02382 2023-07-10 thomas commit_id);
7102 97a02382 2023-07-10 thomas if (error)
7103 97a02382 2023-07-10 thomas goto done;
7104 97a02382 2023-07-10 thomas memset(&upa, 0, sizeof(upa));
7105 97a02382 2023-07-10 thomas error = got_worktree_checkout_files(worktree, &paths,
7106 97a02382 2023-07-10 thomas repo, update_progress, &upa, check_cancelled,
7108 97a02382 2023-07-10 thomas if (error)
7109 97a02382 2023-07-10 thomas goto done;
7110 97a02382 2023-07-10 thomas if (upa.did_something) {
7111 97a02382 2023-07-10 thomas printf("Updated to %s: %s\n",
7112 97a02382 2023-07-10 thomas got_worktree_get_head_ref_name(worktree),
7113 97a02382 2023-07-10 thomas commit_id_str);
7115 97a02382 2023-07-10 thomas print_update_progress_stats(&upa);
7119 97a02382 2023-07-10 thomas if (ref)
7120 97a02382 2023-07-10 thomas got_ref_close(ref);
7121 97a02382 2023-07-10 thomas if (repo) {
7122 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
7123 97a02382 2023-07-10 thomas if (error == NULL)
7124 97a02382 2023-07-10 thomas error = close_err;
7126 97a02382 2023-07-10 thomas if (worktree)
7127 97a02382 2023-07-10 thomas got_worktree_close(worktree);
7128 97a02382 2023-07-10 thomas if (pack_fds) {
7129 97a02382 2023-07-10 thomas const struct got_error *pack_err =
7130 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
7131 97a02382 2023-07-10 thomas if (error == NULL)
7132 97a02382 2023-07-10 thomas error = pack_err;
7134 97a02382 2023-07-10 thomas free(cwd);
7135 97a02382 2023-07-10 thomas free(repo_path);
7136 97a02382 2023-07-10 thomas free(commit_id);
7137 97a02382 2023-07-10 thomas free(commit_id_str);
7138 97a02382 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7139 97a02382 2023-07-10 thomas return error;
7143 97a02382 2023-07-10 thomas __dead static void
7144 97a02382 2023-07-10 thomas usage_tag(void)
7146 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
7147 97a02382 2023-07-10 thomas "[-r repository-path] [-s signer-id] name\n", getprogname());
7148 97a02382 2023-07-10 thomas exit(1);
7152 97a02382 2023-07-10 thomas static const struct got_error *
7153 97a02382 2023-07-10 thomas sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
7155 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
7156 97a02382 2023-07-10 thomas struct got_reflist_entry *re, *se, *new;
7157 97a02382 2023-07-10 thomas struct got_object_id *re_id, *se_id;
7158 97a02382 2023-07-10 thomas struct got_tag_object *re_tag, *se_tag;
7159 97a02382 2023-07-10 thomas time_t re_time, se_time;
7161 97a02382 2023-07-10 thomas STAILQ_FOREACH(re, tags, entry) {
7162 97a02382 2023-07-10 thomas se = STAILQ_FIRST(sorted);
7163 97a02382 2023-07-10 thomas if (se == NULL) {
7164 97a02382 2023-07-10 thomas err = got_reflist_entry_dup(&new, re);
7165 97a02382 2023-07-10 thomas if (err)
7166 97a02382 2023-07-10 thomas return err;
7167 97a02382 2023-07-10 thomas STAILQ_INSERT_HEAD(sorted, new, entry);
7168 97a02382 2023-07-10 thomas continue;
7169 97a02382 2023-07-10 thomas } else {
7170 97a02382 2023-07-10 thomas err = got_ref_resolve(&re_id, repo, re->ref);
7171 97a02382 2023-07-10 thomas if (err)
7173 97a02382 2023-07-10 thomas err = got_object_open_as_tag(&re_tag, repo, re_id);
7174 97a02382 2023-07-10 thomas free(re_id);
7175 97a02382 2023-07-10 thomas if (err)
7177 97a02382 2023-07-10 thomas re_time = got_object_tag_get_tagger_time(re_tag);
7178 97a02382 2023-07-10 thomas got_object_tag_close(re_tag);
7181 97a02382 2023-07-10 thomas while (se) {
7182 97a02382 2023-07-10 thomas err = got_ref_resolve(&se_id, repo, re->ref);
7183 97a02382 2023-07-10 thomas if (err)
7185 97a02382 2023-07-10 thomas err = got_object_open_as_tag(&se_tag, repo, se_id);
7186 97a02382 2023-07-10 thomas free(se_id);
7187 97a02382 2023-07-10 thomas if (err)
7189 97a02382 2023-07-10 thomas se_time = got_object_tag_get_tagger_time(se_tag);
7190 97a02382 2023-07-10 thomas got_object_tag_close(se_tag);
7192 97a02382 2023-07-10 thomas if (se_time > re_time) {
7193 97a02382 2023-07-10 thomas err = got_reflist_entry_dup(&new, re);
7194 97a02382 2023-07-10 thomas if (err)
7195 97a02382 2023-07-10 thomas return err;
7196 97a02382 2023-07-10 thomas STAILQ_INSERT_AFTER(sorted, se, new, entry);
7199 97a02382 2023-07-10 thomas se = STAILQ_NEXT(se, entry);
7200 97a02382 2023-07-10 thomas continue;
7204 97a02382 2023-07-10 thomas return err;
7208 97a02382 2023-07-10 thomas static const struct got_error *
7209 97a02382 2023-07-10 thomas get_tag_refname(char **refname, const char *tag_name)
7211 97a02382 2023-07-10 thomas const struct got_error *err;
7213 97a02382 2023-07-10 thomas if (strncmp("refs/tags/", tag_name, 10) == 0) {
7214 97a02382 2023-07-10 thomas *refname = strdup(tag_name);
7215 97a02382 2023-07-10 thomas if (*refname == NULL)
7216 97a02382 2023-07-10 thomas return got_error_from_errno("strdup");
7217 97a02382 2023-07-10 thomas } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
7218 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
7219 97a02382 2023-07-10 thomas *refname = NULL;
7220 97a02382 2023-07-10 thomas return err;
7223 97a02382 2023-07-10 thomas return NULL;
7226 97a02382 2023-07-10 thomas static const struct got_error *
7227 97a02382 2023-07-10 thomas list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
7228 97a02382 2023-07-10 thomas const char *allowed_signers, const char *revoked_signers, int verbosity)
7230 97a02382 2023-07-10 thomas static const struct got_error *err = NULL;
7231 97a02382 2023-07-10 thomas struct got_reflist_head refs;
7232 97a02382 2023-07-10 thomas struct got_reflist_entry *re;
7233 97a02382 2023-07-10 thomas char *wanted_refname = NULL;
7234 97a02382 2023-07-10 thomas int bad_sigs = 0;
7236 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
7238 97a02382 2023-07-10 thomas err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7239 97a02382 2023-07-10 thomas if (err)
7240 97a02382 2023-07-10 thomas return err;
7242 97a02382 2023-07-10 thomas if (tag_name) {
7243 97a02382 2023-07-10 thomas struct got_reference *ref;
7244 97a02382 2023-07-10 thomas err = get_tag_refname(&wanted_refname, tag_name);
7245 97a02382 2023-07-10 thomas if (err)
7246 97a02382 2023-07-10 thomas goto done;
7247 97a02382 2023-07-10 thomas /* Wanted tag reference should exist. */
7248 97a02382 2023-07-10 thomas err = got_ref_open(&ref, repo, wanted_refname, 0);
7249 97a02382 2023-07-10 thomas if (err)
7250 97a02382 2023-07-10 thomas goto done;
7251 97a02382 2023-07-10 thomas got_ref_close(ref);
7254 97a02382 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
7255 97a02382 2023-07-10 thomas const char *refname;
7256 97a02382 2023-07-10 thomas char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7257 97a02382 2023-07-10 thomas char datebuf[26];
7258 97a02382 2023-07-10 thomas const char *tagger, *ssh_sig = NULL;
7259 97a02382 2023-07-10 thomas char *sig_msg = NULL;
7260 97a02382 2023-07-10 thomas time_t tagger_time;
7261 97a02382 2023-07-10 thomas struct got_object_id *id;
7262 97a02382 2023-07-10 thomas struct got_tag_object *tag;
7263 97a02382 2023-07-10 thomas struct got_commit_object *commit = NULL;
7265 97a02382 2023-07-10 thomas refname = got_ref_get_name(re->ref);
7266 97a02382 2023-07-10 thomas if (strncmp(refname, "refs/tags/", 10) != 0 ||
7267 97a02382 2023-07-10 thomas (wanted_refname && strcmp(refname, wanted_refname) != 0))
7268 97a02382 2023-07-10 thomas continue;
7269 97a02382 2023-07-10 thomas refname += 10;
7270 97a02382 2023-07-10 thomas refstr = got_ref_to_str(re->ref);
7271 97a02382 2023-07-10 thomas if (refstr == NULL) {
7272 97a02382 2023-07-10 thomas err = got_error_from_errno("got_ref_to_str");
7276 97a02382 2023-07-10 thomas err = got_ref_resolve(&id, repo, re->ref);
7277 97a02382 2023-07-10 thomas if (err)
7279 97a02382 2023-07-10 thomas err = got_object_open_as_tag(&tag, repo, id);
7280 97a02382 2023-07-10 thomas if (err) {
7281 97a02382 2023-07-10 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7282 97a02382 2023-07-10 thomas free(id);
7285 97a02382 2023-07-10 thomas /* "lightweight" tag */
7286 97a02382 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, id);
7287 97a02382 2023-07-10 thomas if (err) {
7288 97a02382 2023-07-10 thomas free(id);
7291 97a02382 2023-07-10 thomas tagger = got_object_commit_get_committer(commit);
7292 97a02382 2023-07-10 thomas tagger_time =
7293 97a02382 2023-07-10 thomas got_object_commit_get_committer_time(commit);
7294 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, id);
7295 97a02382 2023-07-10 thomas free(id);
7296 97a02382 2023-07-10 thomas if (err)
7298 97a02382 2023-07-10 thomas } else {
7299 97a02382 2023-07-10 thomas free(id);
7300 97a02382 2023-07-10 thomas tagger = got_object_tag_get_tagger(tag);
7301 97a02382 2023-07-10 thomas tagger_time = got_object_tag_get_tagger_time(tag);
7302 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str,
7303 97a02382 2023-07-10 thomas got_object_tag_get_object_id(tag));
7304 97a02382 2023-07-10 thomas if (err)
7308 97a02382 2023-07-10 thomas if (tag && verify_tags) {
7309 97a02382 2023-07-10 thomas ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7310 97a02382 2023-07-10 thomas got_object_tag_get_message(tag));
7311 97a02382 2023-07-10 thomas if (ssh_sig && allowed_signers == NULL) {
7312 97a02382 2023-07-10 thomas err = got_error_msg(
7313 97a02382 2023-07-10 thomas GOT_ERR_VERIFY_TAG_SIGNATURE,
7314 97a02382 2023-07-10 thomas "SSH signature verification requires "
7315 97a02382 2023-07-10 thomas "setting allowed_signers in "
7316 97a02382 2023-07-10 thomas "got.conf(5)");
7321 97a02382 2023-07-10 thomas printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7322 97a02382 2023-07-10 thomas free(refstr);
7323 97a02382 2023-07-10 thomas printf("from: %s\n", tagger);
7324 97a02382 2023-07-10 thomas datestr = get_datestr(&tagger_time, datebuf);
7325 97a02382 2023-07-10 thomas if (datestr)
7326 97a02382 2023-07-10 thomas printf("date: %s UTC\n", datestr);
7327 97a02382 2023-07-10 thomas if (commit)
7328 97a02382 2023-07-10 thomas printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7330 97a02382 2023-07-10 thomas switch (got_object_tag_get_object_type(tag)) {
7331 97a02382 2023-07-10 thomas case GOT_OBJ_TYPE_BLOB:
7332 97a02382 2023-07-10 thomas printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7333 97a02382 2023-07-10 thomas id_str);
7335 97a02382 2023-07-10 thomas case GOT_OBJ_TYPE_TREE:
7336 97a02382 2023-07-10 thomas printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7337 97a02382 2023-07-10 thomas id_str);
7339 97a02382 2023-07-10 thomas case GOT_OBJ_TYPE_COMMIT:
7340 97a02382 2023-07-10 thomas printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7341 97a02382 2023-07-10 thomas id_str);
7343 97a02382 2023-07-10 thomas case GOT_OBJ_TYPE_TAG:
7344 97a02382 2023-07-10 thomas printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7345 97a02382 2023-07-10 thomas id_str);
7347 97a02382 2023-07-10 thomas default:
7351 97a02382 2023-07-10 thomas free(id_str);
7353 97a02382 2023-07-10 thomas if (ssh_sig) {
7354 97a02382 2023-07-10 thomas err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7355 97a02382 2023-07-10 thomas allowed_signers, revoked_signers, verbosity);
7356 97a02382 2023-07-10 thomas if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7357 97a02382 2023-07-10 thomas bad_sigs = 1;
7358 97a02382 2023-07-10 thomas else if (err)
7360 97a02382 2023-07-10 thomas printf("signature: %s", sig_msg);
7361 97a02382 2023-07-10 thomas free(sig_msg);
7362 97a02382 2023-07-10 thomas sig_msg = NULL;
7365 97a02382 2023-07-10 thomas if (commit) {
7366 97a02382 2023-07-10 thomas err = got_object_commit_get_logmsg(&tagmsg0, commit);
7367 97a02382 2023-07-10 thomas if (err)
7369 97a02382 2023-07-10 thomas got_object_commit_close(commit);
7370 97a02382 2023-07-10 thomas } else {
7371 97a02382 2023-07-10 thomas tagmsg0 = strdup(got_object_tag_get_message(tag));
7372 97a02382 2023-07-10 thomas got_object_tag_close(tag);
7373 97a02382 2023-07-10 thomas if (tagmsg0 == NULL) {
7374 97a02382 2023-07-10 thomas err = got_error_from_errno("strdup");
7379 97a02382 2023-07-10 thomas tagmsg = tagmsg0;
7381 97a02382 2023-07-10 thomas line = strsep(&tagmsg, "\n");
7382 97a02382 2023-07-10 thomas if (line)
7383 97a02382 2023-07-10 thomas printf(" %s\n", line);
7384 97a02382 2023-07-10 thomas } while (line);
7385 97a02382 2023-07-10 thomas free(tagmsg0);
7388 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
7389 97a02382 2023-07-10 thomas free(wanted_refname);
7391 97a02382 2023-07-10 thomas if (err == NULL && bad_sigs)
7392 97a02382 2023-07-10 thomas err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7393 97a02382 2023-07-10 thomas return err;
7396 97a02382 2023-07-10 thomas static const struct got_error *
7397 97a02382 2023-07-10 thomas get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7398 97a02382 2023-07-10 thomas const char *tag_name, const char *repo_path)
7400 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
7401 97a02382 2023-07-10 thomas char *template = NULL, *initial_content = NULL;
7402 97a02382 2023-07-10 thomas char *editor = NULL;
7403 97a02382 2023-07-10 thomas int initial_content_len;
7404 97a02382 2023-07-10 thomas int fd = -1;
7406 97a02382 2023-07-10 thomas if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7407 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
7408 97a02382 2023-07-10 thomas goto done;
7411 97a02382 2023-07-10 thomas initial_content_len = asprintf(&initial_content,
7412 97a02382 2023-07-10 thomas "\n# tagging commit %s as %s\n",
7413 97a02382 2023-07-10 thomas commit_id_str, tag_name);
7414 97a02382 2023-07-10 thomas if (initial_content_len == -1) {
7415 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
7416 97a02382 2023-07-10 thomas goto done;
7419 97a02382 2023-07-10 thomas err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
7420 97a02382 2023-07-10 thomas if (err)
7421 97a02382 2023-07-10 thomas goto done;
7423 97a02382 2023-07-10 thomas if (write(fd, initial_content, initial_content_len) == -1) {
7424 97a02382 2023-07-10 thomas err = got_error_from_errno2("write", *tagmsg_path);
7425 97a02382 2023-07-10 thomas goto done;
7427 97a02382 2023-07-10 thomas if (close(fd) == -1) {
7428 97a02382 2023-07-10 thomas err = got_error_from_errno2("close", *tagmsg_path);
7429 97a02382 2023-07-10 thomas goto done;
7431 97a02382 2023-07-10 thomas fd = -1;
7433 97a02382 2023-07-10 thomas err = get_editor(&editor);
7434 97a02382 2023-07-10 thomas if (err)
7435 97a02382 2023-07-10 thomas goto done;
7436 97a02382 2023-07-10 thomas err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7437 97a02382 2023-07-10 thomas initial_content_len, 1);
7439 97a02382 2023-07-10 thomas free(initial_content);
7440 97a02382 2023-07-10 thomas free(template);
7441 97a02382 2023-07-10 thomas free(editor);
7443 97a02382 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
7444 97a02382 2023-07-10 thomas err = got_error_from_errno2("close", *tagmsg_path);
7446 97a02382 2023-07-10 thomas if (err) {
7447 97a02382 2023-07-10 thomas free(*tagmsg);
7448 97a02382 2023-07-10 thomas *tagmsg = NULL;
7450 97a02382 2023-07-10 thomas return err;
7453 97a02382 2023-07-10 thomas static const struct got_error *
7454 97a02382 2023-07-10 thomas add_tag(struct got_repository *repo, const char *tagger,
7455 97a02382 2023-07-10 thomas const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7456 97a02382 2023-07-10 thomas const char *signer_id, int verbosity)
7458 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
7459 97a02382 2023-07-10 thomas struct got_object_id *commit_id = NULL, *tag_id = NULL;
7460 97a02382 2023-07-10 thomas char *label = NULL, *commit_id_str = NULL;
7461 97a02382 2023-07-10 thomas struct got_reference *ref = NULL;
7462 97a02382 2023-07-10 thomas char *refname = NULL, *tagmsg = NULL;
7463 97a02382 2023-07-10 thomas char *tagmsg_path = NULL, *tag_id_str = NULL;
7464 97a02382 2023-07-10 thomas int preserve_tagmsg = 0;
7465 97a02382 2023-07-10 thomas struct got_reflist_head refs;
7467 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
7470 97a02382 2023-07-10 thomas * Don't let the user create a tag name with a leading '-'.
7471 97a02382 2023-07-10 thomas * While technically a valid reference name, this case is usually
7472 97a02382 2023-07-10 thomas * an unintended typo.
7474 97a02382 2023-07-10 thomas if (tag_name[0] == '-')
7475 97a02382 2023-07-10 thomas return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7477 97a02382 2023-07-10 thomas err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7478 97a02382 2023-07-10 thomas if (err)
7479 97a02382 2023-07-10 thomas goto done;
7481 97a02382 2023-07-10 thomas err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7482 97a02382 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, &refs, repo);
7483 97a02382 2023-07-10 thomas if (err)
7484 97a02382 2023-07-10 thomas goto done;
7486 97a02382 2023-07-10 thomas err = got_object_id_str(&commit_id_str, commit_id);
7487 97a02382 2023-07-10 thomas if (err)
7488 97a02382 2023-07-10 thomas goto done;
7490 97a02382 2023-07-10 thomas err = get_tag_refname(&refname, tag_name);
7491 97a02382 2023-07-10 thomas if (err)
7492 97a02382 2023-07-10 thomas goto done;
7493 97a02382 2023-07-10 thomas if (strncmp("refs/tags/", tag_name, 10) == 0)
7494 97a02382 2023-07-10 thomas tag_name += 10;
7496 97a02382 2023-07-10 thomas err = got_ref_open(&ref, repo, refname, 0);
7497 97a02382 2023-07-10 thomas if (err == NULL) {
7498 97a02382 2023-07-10 thomas err = got_error(GOT_ERR_TAG_EXISTS);
7499 97a02382 2023-07-10 thomas goto done;
7500 97a02382 2023-07-10 thomas } else if (err->code != GOT_ERR_NOT_REF)
7501 97a02382 2023-07-10 thomas goto done;
7503 97a02382 2023-07-10 thomas if (tagmsg_arg == NULL) {
7504 97a02382 2023-07-10 thomas err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7505 97a02382 2023-07-10 thomas tag_name, got_repo_get_path(repo));
7506 97a02382 2023-07-10 thomas if (err) {
7507 97a02382 2023-07-10 thomas if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7508 97a02382 2023-07-10 thomas tagmsg_path != NULL)
7509 97a02382 2023-07-10 thomas preserve_tagmsg = 1;
7510 97a02382 2023-07-10 thomas goto done;
7512 97a02382 2023-07-10 thomas /* Editor is done; we can now apply unveil(2) */
7513 97a02382 2023-07-10 thomas err = got_sigs_apply_unveil();
7514 97a02382 2023-07-10 thomas if (err)
7515 97a02382 2023-07-10 thomas goto done;
7516 97a02382 2023-07-10 thomas err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7517 97a02382 2023-07-10 thomas if (err)
7518 97a02382 2023-07-10 thomas goto done;
7521 97a02382 2023-07-10 thomas err = got_object_tag_create(&tag_id, tag_name, commit_id,
7522 97a02382 2023-07-10 thomas tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7523 97a02382 2023-07-10 thomas verbosity);
7524 97a02382 2023-07-10 thomas if (err) {
7525 97a02382 2023-07-10 thomas if (tagmsg_path)
7526 97a02382 2023-07-10 thomas preserve_tagmsg = 1;
7527 97a02382 2023-07-10 thomas goto done;
7530 97a02382 2023-07-10 thomas err = got_ref_alloc(&ref, refname, tag_id);
7531 97a02382 2023-07-10 thomas if (err) {
7532 97a02382 2023-07-10 thomas if (tagmsg_path)
7533 97a02382 2023-07-10 thomas preserve_tagmsg = 1;
7534 97a02382 2023-07-10 thomas goto done;
7537 97a02382 2023-07-10 thomas err = got_ref_write(ref, repo);
7538 97a02382 2023-07-10 thomas if (err) {
7539 97a02382 2023-07-10 thomas if (tagmsg_path)
7540 97a02382 2023-07-10 thomas preserve_tagmsg = 1;
7541 97a02382 2023-07-10 thomas goto done;
7544 97a02382 2023-07-10 thomas err = got_object_id_str(&tag_id_str, tag_id);
7545 97a02382 2023-07-10 thomas if (err) {
7546 97a02382 2023-07-10 thomas if (tagmsg_path)
7547 97a02382 2023-07-10 thomas preserve_tagmsg = 1;
7548 97a02382 2023-07-10 thomas goto done;
7550 97a02382 2023-07-10 thomas printf("Created tag %s\n", tag_id_str);
7552 97a02382 2023-07-10 thomas if (preserve_tagmsg) {
7553 97a02382 2023-07-10 thomas fprintf(stderr, "%s: tag message preserved in %s\n",
7554 97a02382 2023-07-10 thomas getprogname(), tagmsg_path);
7555 97a02382 2023-07-10 thomas } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7556 97a02382 2023-07-10 thomas err = got_error_from_errno2("unlink", tagmsg_path);
7557 97a02382 2023-07-10 thomas free(tag_id_str);
7558 97a02382 2023-07-10 thomas if (ref)
7559 97a02382 2023-07-10 thomas got_ref_close(ref);
7560 97a02382 2023-07-10 thomas free(commit_id);
7561 97a02382 2023-07-10 thomas free(commit_id_str);
7562 97a02382 2023-07-10 thomas free(refname);
7563 97a02382 2023-07-10 thomas free(tagmsg);
7564 97a02382 2023-07-10 thomas free(tagmsg_path);
7565 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
7566 97a02382 2023-07-10 thomas return err;
7569 97a02382 2023-07-10 thomas static const struct got_error *
7570 97a02382 2023-07-10 thomas cmd_tag(int argc, char *argv[])
7572 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
7573 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
7574 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
7575 97a02382 2023-07-10 thomas char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7576 97a02382 2023-07-10 thomas char *gitconfig_path = NULL, *tagger = NULL;
7577 97a02382 2023-07-10 thomas char *allowed_signers = NULL, *revoked_signers = NULL;
7578 97a02382 2023-07-10 thomas const char *signer_id = NULL;
7579 97a02382 2023-07-10 thomas const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7580 97a02382 2023-07-10 thomas int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7581 97a02382 2023-07-10 thomas int *pack_fds = NULL;
7583 97a02382 2023-07-10 thomas #ifndef PROFILE
7584 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7585 97a02382 2023-07-10 thomas "sendfd unveil", NULL) == -1)
7586 97a02382 2023-07-10 thomas err(1, "pledge");
7589 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7590 97a02382 2023-07-10 thomas switch (ch) {
7591 97a02382 2023-07-10 thomas case 'c':
7592 97a02382 2023-07-10 thomas commit_id_arg = optarg;
7594 97a02382 2023-07-10 thomas case 'l':
7595 97a02382 2023-07-10 thomas do_list = 1;
7597 97a02382 2023-07-10 thomas case 'm':
7598 97a02382 2023-07-10 thomas tagmsg = optarg;
7600 97a02382 2023-07-10 thomas case 'r':
7601 97a02382 2023-07-10 thomas repo_path = realpath(optarg, NULL);
7602 97a02382 2023-07-10 thomas if (repo_path == NULL) {
7603 97a02382 2023-07-10 thomas error = got_error_from_errno2("realpath",
7604 97a02382 2023-07-10 thomas optarg);
7605 97a02382 2023-07-10 thomas goto done;
7607 97a02382 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
7609 97a02382 2023-07-10 thomas case 's':
7610 97a02382 2023-07-10 thomas signer_id = optarg;
7612 97a02382 2023-07-10 thomas case 'V':
7613 97a02382 2023-07-10 thomas verify_tags = 1;
7615 97a02382 2023-07-10 thomas case 'v':
7616 97a02382 2023-07-10 thomas if (verbosity < 0)
7617 97a02382 2023-07-10 thomas verbosity = 0;
7618 97a02382 2023-07-10 thomas else if (verbosity < 3)
7619 97a02382 2023-07-10 thomas verbosity++;
7621 97a02382 2023-07-10 thomas default:
7622 97a02382 2023-07-10 thomas usage_tag();
7623 97a02382 2023-07-10 thomas /* NOTREACHED */
7627 97a02382 2023-07-10 thomas argc -= optind;
7628 97a02382 2023-07-10 thomas argv += optind;
7630 97a02382 2023-07-10 thomas if (do_list || verify_tags) {
7631 97a02382 2023-07-10 thomas if (commit_id_arg != NULL)
7633 97a02382 2023-07-10 thomas "-c option can only be used when creating a tag");
7634 97a02382 2023-07-10 thomas if (tagmsg) {
7635 97a02382 2023-07-10 thomas if (do_list)
7636 97a02382 2023-07-10 thomas option_conflict('l', 'm');
7638 97a02382 2023-07-10 thomas option_conflict('V', 'm');
7640 97a02382 2023-07-10 thomas if (signer_id) {
7641 97a02382 2023-07-10 thomas if (do_list)
7642 97a02382 2023-07-10 thomas option_conflict('l', 's');
7644 97a02382 2023-07-10 thomas option_conflict('V', 's');
7646 97a02382 2023-07-10 thomas if (argc > 1)
7647 97a02382 2023-07-10 thomas usage_tag();
7648 97a02382 2023-07-10 thomas } else if (argc != 1)
7649 97a02382 2023-07-10 thomas usage_tag();
7651 97a02382 2023-07-10 thomas if (argc == 1)
7652 97a02382 2023-07-10 thomas tag_name = argv[0];
7654 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
7655 97a02382 2023-07-10 thomas if (cwd == NULL) {
7656 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
7657 97a02382 2023-07-10 thomas goto done;
7660 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
7661 97a02382 2023-07-10 thomas if (error != NULL)
7662 97a02382 2023-07-10 thomas goto done;
7664 97a02382 2023-07-10 thomas if (repo_path == NULL) {
7665 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
7666 97a02382 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
7667 97a02382 2023-07-10 thomas goto done;
7669 97a02382 2023-07-10 thomas error = NULL;
7670 97a02382 2023-07-10 thomas if (worktree) {
7671 97a02382 2023-07-10 thomas repo_path =
7672 97a02382 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree));
7673 97a02382 2023-07-10 thomas if (repo_path == NULL)
7674 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
7675 97a02382 2023-07-10 thomas if (error)
7676 97a02382 2023-07-10 thomas goto done;
7677 97a02382 2023-07-10 thomas } else {
7678 97a02382 2023-07-10 thomas repo_path = strdup(cwd);
7679 97a02382 2023-07-10 thomas if (repo_path == NULL) {
7680 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
7681 97a02382 2023-07-10 thomas goto done;
7686 97a02382 2023-07-10 thomas if (do_list || verify_tags) {
7687 97a02382 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7688 97a02382 2023-07-10 thomas if (error != NULL)
7689 97a02382 2023-07-10 thomas goto done;
7690 97a02382 2023-07-10 thomas error = get_allowed_signers(&allowed_signers, repo, worktree);
7691 97a02382 2023-07-10 thomas if (error)
7692 97a02382 2023-07-10 thomas goto done;
7693 97a02382 2023-07-10 thomas error = get_revoked_signers(&revoked_signers, repo, worktree);
7694 97a02382 2023-07-10 thomas if (error)
7695 97a02382 2023-07-10 thomas goto done;
7696 97a02382 2023-07-10 thomas if (worktree) {
7697 97a02382 2023-07-10 thomas /* Release work tree lock. */
7698 97a02382 2023-07-10 thomas got_worktree_close(worktree);
7699 97a02382 2023-07-10 thomas worktree = NULL;
7703 97a02382 2023-07-10 thomas * Remove "cpath" promise unless needed for signature tmpfile
7704 97a02382 2023-07-10 thomas * creation.
7706 97a02382 2023-07-10 thomas if (verify_tags)
7707 97a02382 2023-07-10 thomas got_sigs_apply_unveil();
7709 97a02382 2023-07-10 thomas #ifndef PROFILE
7710 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath flock proc exec sendfd "
7711 97a02382 2023-07-10 thomas "unveil", NULL) == -1)
7712 97a02382 2023-07-10 thomas err(1, "pledge");
7715 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7716 97a02382 2023-07-10 thomas if (error)
7717 97a02382 2023-07-10 thomas goto done;
7718 97a02382 2023-07-10 thomas error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7719 97a02382 2023-07-10 thomas revoked_signers, verbosity);
7720 97a02382 2023-07-10 thomas } else {
7721 97a02382 2023-07-10 thomas error = get_gitconfig_path(&gitconfig_path);
7722 97a02382 2023-07-10 thomas if (error)
7723 97a02382 2023-07-10 thomas goto done;
7724 97a02382 2023-07-10 thomas error = got_repo_open(&repo, repo_path, gitconfig_path,
7725 97a02382 2023-07-10 thomas pack_fds);
7726 97a02382 2023-07-10 thomas if (error != NULL)
7727 97a02382 2023-07-10 thomas goto done;
7729 97a02382 2023-07-10 thomas error = get_author(&tagger, repo, worktree);
7730 97a02382 2023-07-10 thomas if (error)
7731 97a02382 2023-07-10 thomas goto done;
7732 97a02382 2023-07-10 thomas if (signer_id == NULL)
7733 97a02382 2023-07-10 thomas signer_id = get_signer_id(repo, worktree);
7735 97a02382 2023-07-10 thomas if (tagmsg) {
7736 97a02382 2023-07-10 thomas if (signer_id) {
7737 97a02382 2023-07-10 thomas error = got_sigs_apply_unveil();
7738 97a02382 2023-07-10 thomas if (error)
7739 97a02382 2023-07-10 thomas goto done;
7741 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7742 97a02382 2023-07-10 thomas if (error)
7743 97a02382 2023-07-10 thomas goto done;
7746 97a02382 2023-07-10 thomas if (commit_id_arg == NULL) {
7747 97a02382 2023-07-10 thomas struct got_reference *head_ref;
7748 97a02382 2023-07-10 thomas struct got_object_id *commit_id;
7749 97a02382 2023-07-10 thomas error = got_ref_open(&head_ref, repo,
7750 97a02382 2023-07-10 thomas worktree ? got_worktree_get_head_ref_name(worktree)
7751 97a02382 2023-07-10 thomas : GOT_REF_HEAD, 0);
7752 97a02382 2023-07-10 thomas if (error)
7753 97a02382 2023-07-10 thomas goto done;
7754 97a02382 2023-07-10 thomas error = got_ref_resolve(&commit_id, repo, head_ref);
7755 97a02382 2023-07-10 thomas got_ref_close(head_ref);
7756 97a02382 2023-07-10 thomas if (error)
7757 97a02382 2023-07-10 thomas goto done;
7758 97a02382 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
7759 97a02382 2023-07-10 thomas free(commit_id);
7760 97a02382 2023-07-10 thomas if (error)
7761 97a02382 2023-07-10 thomas goto done;
7764 97a02382 2023-07-10 thomas if (worktree) {
7765 97a02382 2023-07-10 thomas /* Release work tree lock. */
7766 97a02382 2023-07-10 thomas got_worktree_close(worktree);
7767 97a02382 2023-07-10 thomas worktree = NULL;
7770 97a02382 2023-07-10 thomas error = add_tag(repo, tagger, tag_name,
7771 97a02382 2023-07-10 thomas commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7772 97a02382 2023-07-10 thomas signer_id, verbosity);
7775 97a02382 2023-07-10 thomas if (repo) {
7776 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
7777 97a02382 2023-07-10 thomas if (error == NULL)
7778 97a02382 2023-07-10 thomas error = close_err;
7780 97a02382 2023-07-10 thomas if (worktree)
7781 97a02382 2023-07-10 thomas got_worktree_close(worktree);
7782 97a02382 2023-07-10 thomas if (pack_fds) {
7783 97a02382 2023-07-10 thomas const struct got_error *pack_err =
7784 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
7785 97a02382 2023-07-10 thomas if (error == NULL)
7786 97a02382 2023-07-10 thomas error = pack_err;
7788 97a02382 2023-07-10 thomas free(cwd);
7789 97a02382 2023-07-10 thomas free(repo_path);
7790 97a02382 2023-07-10 thomas free(gitconfig_path);
7791 97a02382 2023-07-10 thomas free(commit_id_str);
7792 97a02382 2023-07-10 thomas free(tagger);
7793 97a02382 2023-07-10 thomas free(allowed_signers);
7794 97a02382 2023-07-10 thomas free(revoked_signers);
7795 97a02382 2023-07-10 thomas return error;
7798 97a02382 2023-07-10 thomas __dead static void
7799 97a02382 2023-07-10 thomas usage_add(void)
7801 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
7802 97a02382 2023-07-10 thomas exit(1);
7805 97a02382 2023-07-10 thomas static const struct got_error *
7806 97a02382 2023-07-10 thomas add_progress(void *arg, unsigned char status, const char *path)
7808 97a02382 2023-07-10 thomas while (path[0] == '/')
7810 97a02382 2023-07-10 thomas printf("%c %s\n", status, path);
7811 97a02382 2023-07-10 thomas return NULL;
7814 97a02382 2023-07-10 thomas static const struct got_error *
7815 97a02382 2023-07-10 thomas cmd_add(int argc, char *argv[])
7817 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
7818 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
7819 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
7820 97a02382 2023-07-10 thomas char *cwd = NULL;
7821 97a02382 2023-07-10 thomas struct got_pathlist_head paths;
7822 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
7823 97a02382 2023-07-10 thomas int ch, can_recurse = 0, no_ignores = 0;
7824 97a02382 2023-07-10 thomas int *pack_fds = NULL;
7826 97a02382 2023-07-10 thomas TAILQ_INIT(&paths);
7828 97a02382 2023-07-10 thomas #ifndef PROFILE
7829 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7830 97a02382 2023-07-10 thomas NULL) == -1)
7831 97a02382 2023-07-10 thomas err(1, "pledge");
7834 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "IR")) != -1) {
7835 97a02382 2023-07-10 thomas switch (ch) {
7836 97a02382 2023-07-10 thomas case 'I':
7837 97a02382 2023-07-10 thomas no_ignores = 1;
7839 97a02382 2023-07-10 thomas case 'R':
7840 97a02382 2023-07-10 thomas can_recurse = 1;
7842 97a02382 2023-07-10 thomas default:
7843 97a02382 2023-07-10 thomas usage_add();
7844 97a02382 2023-07-10 thomas /* NOTREACHED */
7848 97a02382 2023-07-10 thomas argc -= optind;
7849 97a02382 2023-07-10 thomas argv += optind;
7851 97a02382 2023-07-10 thomas if (argc < 1)
7852 97a02382 2023-07-10 thomas usage_add();
7854 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
7855 97a02382 2023-07-10 thomas if (cwd == NULL) {
7856 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
7857 97a02382 2023-07-10 thomas goto done;
7860 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
7861 97a02382 2023-07-10 thomas if (error != NULL)
7862 97a02382 2023-07-10 thomas goto done;
7864 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
7865 97a02382 2023-07-10 thomas if (error) {
7866 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
7867 97a02382 2023-07-10 thomas error = wrap_not_worktree_error(error, "add", cwd);
7868 97a02382 2023-07-10 thomas goto done;
7871 97a02382 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7872 97a02382 2023-07-10 thomas NULL, pack_fds);
7873 97a02382 2023-07-10 thomas if (error != NULL)
7874 97a02382 2023-07-10 thomas goto done;
7876 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1,
7877 97a02382 2023-07-10 thomas got_worktree_get_root_path(worktree));
7878 97a02382 2023-07-10 thomas if (error)
7879 97a02382 2023-07-10 thomas goto done;
7881 97a02382 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7882 97a02382 2023-07-10 thomas if (error)
7883 97a02382 2023-07-10 thomas goto done;
7885 97a02382 2023-07-10 thomas if (!can_recurse) {
7886 97a02382 2023-07-10 thomas char *ondisk_path;
7887 97a02382 2023-07-10 thomas struct stat sb;
7888 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, &paths, entry) {
7889 97a02382 2023-07-10 thomas if (asprintf(&ondisk_path, "%s/%s",
7890 97a02382 2023-07-10 thomas got_worktree_get_root_path(worktree),
7891 97a02382 2023-07-10 thomas pe->path) == -1) {
7892 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
7893 97a02382 2023-07-10 thomas goto done;
7895 97a02382 2023-07-10 thomas if (lstat(ondisk_path, &sb) == -1) {
7896 97a02382 2023-07-10 thomas if (errno == ENOENT) {
7897 97a02382 2023-07-10 thomas free(ondisk_path);
7898 97a02382 2023-07-10 thomas continue;
7900 97a02382 2023-07-10 thomas error = got_error_from_errno2("lstat",
7901 97a02382 2023-07-10 thomas ondisk_path);
7902 97a02382 2023-07-10 thomas free(ondisk_path);
7903 97a02382 2023-07-10 thomas goto done;
7905 97a02382 2023-07-10 thomas free(ondisk_path);
7906 97a02382 2023-07-10 thomas if (S_ISDIR(sb.st_mode)) {
7907 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_BAD_PATH,
7908 97a02382 2023-07-10 thomas "adding directories requires -R option");
7909 97a02382 2023-07-10 thomas goto done;
7914 97a02382 2023-07-10 thomas error = got_worktree_schedule_add(worktree, &paths, add_progress,
7915 97a02382 2023-07-10 thomas NULL, repo, no_ignores);
7917 97a02382 2023-07-10 thomas if (repo) {
7918 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
7919 97a02382 2023-07-10 thomas if (error == NULL)
7920 97a02382 2023-07-10 thomas error = close_err;
7922 97a02382 2023-07-10 thomas if (worktree)
7923 97a02382 2023-07-10 thomas got_worktree_close(worktree);
7924 97a02382 2023-07-10 thomas if (pack_fds) {
7925 97a02382 2023-07-10 thomas const struct got_error *pack_err =
7926 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
7927 97a02382 2023-07-10 thomas if (error == NULL)
7928 97a02382 2023-07-10 thomas error = pack_err;
7930 97a02382 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7931 97a02382 2023-07-10 thomas free(cwd);
7932 97a02382 2023-07-10 thomas return error;
7935 97a02382 2023-07-10 thomas __dead static void
7936 97a02382 2023-07-10 thomas usage_remove(void)
7938 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
7939 97a02382 2023-07-10 thomas getprogname());
7940 97a02382 2023-07-10 thomas exit(1);
7943 97a02382 2023-07-10 thomas static const struct got_error *
7944 97a02382 2023-07-10 thomas print_remove_status(void *arg, unsigned char status,
7945 97a02382 2023-07-10 thomas unsigned char staged_status, const char *path)
7947 97a02382 2023-07-10 thomas while (path[0] == '/')
7949 97a02382 2023-07-10 thomas if (status == GOT_STATUS_NONEXISTENT)
7950 97a02382 2023-07-10 thomas return NULL;
7951 97a02382 2023-07-10 thomas if (status == staged_status && (status == GOT_STATUS_DELETE))
7952 97a02382 2023-07-10 thomas status = GOT_STATUS_NO_CHANGE;
7953 97a02382 2023-07-10 thomas printf("%c%c %s\n", status, staged_status, path);
7954 97a02382 2023-07-10 thomas return NULL;
7957 97a02382 2023-07-10 thomas static const struct got_error *
7958 97a02382 2023-07-10 thomas cmd_remove(int argc, char *argv[])
7960 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
7961 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
7962 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
7963 97a02382 2023-07-10 thomas const char *status_codes = NULL;
7964 97a02382 2023-07-10 thomas char *cwd = NULL;
7965 97a02382 2023-07-10 thomas struct got_pathlist_head paths;
7966 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
7967 97a02382 2023-07-10 thomas int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7968 97a02382 2023-07-10 thomas int ignore_missing_paths = 0;
7969 97a02382 2023-07-10 thomas int *pack_fds = NULL;
7971 97a02382 2023-07-10 thomas TAILQ_INIT(&paths);
7973 97a02382 2023-07-10 thomas #ifndef PROFILE
7974 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7975 97a02382 2023-07-10 thomas NULL) == -1)
7976 97a02382 2023-07-10 thomas err(1, "pledge");
7979 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7980 97a02382 2023-07-10 thomas switch (ch) {
7981 97a02382 2023-07-10 thomas case 'f':
7982 97a02382 2023-07-10 thomas delete_local_mods = 1;
7983 97a02382 2023-07-10 thomas ignore_missing_paths = 1;
7985 97a02382 2023-07-10 thomas case 'k':
7986 97a02382 2023-07-10 thomas keep_on_disk = 1;
7988 97a02382 2023-07-10 thomas case 'R':
7989 97a02382 2023-07-10 thomas can_recurse = 1;
7991 97a02382 2023-07-10 thomas case 's':
7992 97a02382 2023-07-10 thomas for (i = 0; optarg[i] != '\0'; i++) {
7993 97a02382 2023-07-10 thomas switch (optarg[i]) {
7994 97a02382 2023-07-10 thomas case GOT_STATUS_MODIFY:
7995 97a02382 2023-07-10 thomas delete_local_mods = 1;
7997 97a02382 2023-07-10 thomas case GOT_STATUS_MISSING:
7998 97a02382 2023-07-10 thomas ignore_missing_paths = 1;
8000 97a02382 2023-07-10 thomas default:
8001 97a02382 2023-07-10 thomas errx(1, "invalid status code '%c'",
8002 97a02382 2023-07-10 thomas optarg[i]);
8005 97a02382 2023-07-10 thomas status_codes = optarg;
8007 97a02382 2023-07-10 thomas default:
8008 97a02382 2023-07-10 thomas usage_remove();
8009 97a02382 2023-07-10 thomas /* NOTREACHED */
8013 97a02382 2023-07-10 thomas argc -= optind;
8014 97a02382 2023-07-10 thomas argv += optind;
8016 97a02382 2023-07-10 thomas if (argc < 1)
8017 97a02382 2023-07-10 thomas usage_remove();
8019 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
8020 97a02382 2023-07-10 thomas if (cwd == NULL) {
8021 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
8022 97a02382 2023-07-10 thomas goto done;
8025 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
8026 97a02382 2023-07-10 thomas if (error != NULL)
8027 97a02382 2023-07-10 thomas goto done;
8029 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
8030 97a02382 2023-07-10 thomas if (error) {
8031 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
8032 97a02382 2023-07-10 thomas error = wrap_not_worktree_error(error, "remove", cwd);
8033 97a02382 2023-07-10 thomas goto done;
8036 97a02382 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8037 97a02382 2023-07-10 thomas NULL, pack_fds);
8038 97a02382 2023-07-10 thomas if (error)
8039 97a02382 2023-07-10 thomas goto done;
8041 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1,
8042 97a02382 2023-07-10 thomas got_worktree_get_root_path(worktree));
8043 97a02382 2023-07-10 thomas if (error)
8044 97a02382 2023-07-10 thomas goto done;
8046 97a02382 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8047 97a02382 2023-07-10 thomas if (error)
8048 97a02382 2023-07-10 thomas goto done;
8050 97a02382 2023-07-10 thomas if (!can_recurse) {
8051 97a02382 2023-07-10 thomas char *ondisk_path;
8052 97a02382 2023-07-10 thomas struct stat sb;
8053 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, &paths, entry) {
8054 97a02382 2023-07-10 thomas if (asprintf(&ondisk_path, "%s/%s",
8055 97a02382 2023-07-10 thomas got_worktree_get_root_path(worktree),
8056 97a02382 2023-07-10 thomas pe->path) == -1) {
8057 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
8058 97a02382 2023-07-10 thomas goto done;
8060 97a02382 2023-07-10 thomas if (lstat(ondisk_path, &sb) == -1) {
8061 97a02382 2023-07-10 thomas if (errno == ENOENT) {
8062 97a02382 2023-07-10 thomas free(ondisk_path);
8063 97a02382 2023-07-10 thomas continue;
8065 97a02382 2023-07-10 thomas error = got_error_from_errno2("lstat",
8066 97a02382 2023-07-10 thomas ondisk_path);
8067 97a02382 2023-07-10 thomas free(ondisk_path);
8068 97a02382 2023-07-10 thomas goto done;
8070 97a02382 2023-07-10 thomas free(ondisk_path);
8071 97a02382 2023-07-10 thomas if (S_ISDIR(sb.st_mode)) {
8072 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_BAD_PATH,
8073 97a02382 2023-07-10 thomas "removing directories requires -R option");
8074 97a02382 2023-07-10 thomas goto done;
8079 97a02382 2023-07-10 thomas error = got_worktree_schedule_delete(worktree, &paths,
8080 97a02382 2023-07-10 thomas delete_local_mods, status_codes, print_remove_status, NULL,
8081 97a02382 2023-07-10 thomas repo, keep_on_disk, ignore_missing_paths);
8083 97a02382 2023-07-10 thomas if (repo) {
8084 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
8085 97a02382 2023-07-10 thomas if (error == NULL)
8086 97a02382 2023-07-10 thomas error = close_err;
8088 97a02382 2023-07-10 thomas if (worktree)
8089 97a02382 2023-07-10 thomas got_worktree_close(worktree);
8090 97a02382 2023-07-10 thomas if (pack_fds) {
8091 97a02382 2023-07-10 thomas const struct got_error *pack_err =
8092 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
8093 97a02382 2023-07-10 thomas if (error == NULL)
8094 97a02382 2023-07-10 thomas error = pack_err;
8096 97a02382 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8097 97a02382 2023-07-10 thomas free(cwd);
8098 97a02382 2023-07-10 thomas return error;
8101 97a02382 2023-07-10 thomas __dead static void
8102 97a02382 2023-07-10 thomas usage_patch(void)
8104 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
8105 97a02382 2023-07-10 thomas "[patchfile]\n", getprogname());
8106 97a02382 2023-07-10 thomas exit(1);
8109 97a02382 2023-07-10 thomas static const struct got_error *
8110 97a02382 2023-07-10 thomas patch_from_stdin(int *patchfd)
8112 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
8113 97a02382 2023-07-10 thomas ssize_t r;
8114 97a02382 2023-07-10 thomas char buf[BUFSIZ];
8115 97a02382 2023-07-10 thomas sig_t sighup, sigint, sigquit;
8117 97a02382 2023-07-10 thomas *patchfd = got_opentempfd();
8118 97a02382 2023-07-10 thomas if (*patchfd == -1)
8119 97a02382 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
8121 97a02382 2023-07-10 thomas sighup = signal(SIGHUP, SIG_DFL);
8122 97a02382 2023-07-10 thomas sigint = signal(SIGINT, SIG_DFL);
8123 97a02382 2023-07-10 thomas sigquit = signal(SIGQUIT, SIG_DFL);
8125 97a02382 2023-07-10 thomas for (;;) {
8126 97a02382 2023-07-10 thomas r = read(0, buf, sizeof(buf));
8127 97a02382 2023-07-10 thomas if (r == -1) {
8128 97a02382 2023-07-10 thomas err = got_error_from_errno("read");
8131 97a02382 2023-07-10 thomas if (r == 0)
8133 97a02382 2023-07-10 thomas if (write(*patchfd, buf, r) == -1) {
8134 97a02382 2023-07-10 thomas err = got_error_from_errno("write");
8139 97a02382 2023-07-10 thomas signal(SIGHUP, sighup);
8140 97a02382 2023-07-10 thomas signal(SIGINT, sigint);
8141 97a02382 2023-07-10 thomas signal(SIGQUIT, sigquit);
8143 97a02382 2023-07-10 thomas if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
8144 97a02382 2023-07-10 thomas err = got_error_from_errno("lseek");
8146 97a02382 2023-07-10 thomas if (err != NULL) {
8147 97a02382 2023-07-10 thomas close(*patchfd);
8148 97a02382 2023-07-10 thomas *patchfd = -1;
8151 97a02382 2023-07-10 thomas return err;
8154 97a02382 2023-07-10 thomas struct got_patch_progress_arg {
8155 97a02382 2023-07-10 thomas int did_something;
8156 97a02382 2023-07-10 thomas int conflicts;
8157 97a02382 2023-07-10 thomas int rejects;
8160 97a02382 2023-07-10 thomas static const struct got_error *
8161 97a02382 2023-07-10 thomas patch_progress(void *arg, const char *old, const char *new,
8162 97a02382 2023-07-10 thomas unsigned char status, const struct got_error *error, int old_from,
8163 97a02382 2023-07-10 thomas int old_lines, int new_from, int new_lines, int offset,
8164 97a02382 2023-07-10 thomas int ws_mangled, const struct got_error *hunk_err)
8166 97a02382 2023-07-10 thomas const char *path = new == NULL ? old : new;
8167 97a02382 2023-07-10 thomas struct got_patch_progress_arg *a = arg;
8169 97a02382 2023-07-10 thomas while (*path == '/')
8172 97a02382 2023-07-10 thomas if (status != GOT_STATUS_NO_CHANGE &&
8173 97a02382 2023-07-10 thomas status != 0 /* per-hunk progress */) {
8174 97a02382 2023-07-10 thomas printf("%c %s\n", status, path);
8175 97a02382 2023-07-10 thomas a->did_something = 1;
8178 97a02382 2023-07-10 thomas if (hunk_err == NULL) {
8179 97a02382 2023-07-10 thomas if (status == GOT_STATUS_CANNOT_UPDATE)
8180 97a02382 2023-07-10 thomas a->rejects++;
8181 97a02382 2023-07-10 thomas else if (status == GOT_STATUS_CONFLICT)
8182 97a02382 2023-07-10 thomas a->conflicts++;
8185 97a02382 2023-07-10 thomas if (error != NULL)
8186 97a02382 2023-07-10 thomas fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8188 97a02382 2023-07-10 thomas if (offset != 0 || hunk_err != NULL || ws_mangled) {
8189 97a02382 2023-07-10 thomas printf("@@ -%d,%d +%d,%d @@ ", old_from,
8190 97a02382 2023-07-10 thomas old_lines, new_from, new_lines);
8191 97a02382 2023-07-10 thomas if (hunk_err != NULL)
8192 97a02382 2023-07-10 thomas printf("%s\n", hunk_err->msg);
8193 97a02382 2023-07-10 thomas else if (offset != 0)
8194 97a02382 2023-07-10 thomas printf("applied with offset %d\n", offset);
8196 97a02382 2023-07-10 thomas printf("hunk contains mangled whitespace\n");
8199 97a02382 2023-07-10 thomas return NULL;
8202 97a02382 2023-07-10 thomas static void
8203 97a02382 2023-07-10 thomas print_patch_progress_stats(struct got_patch_progress_arg *ppa)
8205 97a02382 2023-07-10 thomas if (!ppa->did_something)
8208 97a02382 2023-07-10 thomas if (ppa->conflicts > 0)
8209 97a02382 2023-07-10 thomas printf("Files with merge conflicts: %d\n", ppa->conflicts);
8211 97a02382 2023-07-10 thomas if (ppa->rejects > 0) {
8212 97a02382 2023-07-10 thomas printf("Files where patch failed to apply: %d\n",
8213 97a02382 2023-07-10 thomas ppa->rejects);
8217 97a02382 2023-07-10 thomas static const struct got_error *
8218 97a02382 2023-07-10 thomas cmd_patch(int argc, char *argv[])
8220 97a02382 2023-07-10 thomas const struct got_error *error = NULL, *close_error = NULL;
8221 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
8222 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
8223 97a02382 2023-07-10 thomas struct got_reflist_head refs;
8224 97a02382 2023-07-10 thomas struct got_object_id *commit_id = NULL;
8225 97a02382 2023-07-10 thomas const char *commit_id_str = NULL;
8226 97a02382 2023-07-10 thomas struct stat sb;
8227 97a02382 2023-07-10 thomas const char *errstr;
8228 97a02382 2023-07-10 thomas char *cwd = NULL;
8229 97a02382 2023-07-10 thomas int ch, nop = 0, strip = -1, reverse = 0;
8230 97a02382 2023-07-10 thomas int patchfd;
8231 97a02382 2023-07-10 thomas int *pack_fds = NULL;
8232 97a02382 2023-07-10 thomas struct got_patch_progress_arg ppa;
8234 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
8236 97a02382 2023-07-10 thomas #ifndef PROFILE
8237 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
8238 97a02382 2023-07-10 thomas "unveil", NULL) == -1)
8239 97a02382 2023-07-10 thomas err(1, "pledge");
8242 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
8243 97a02382 2023-07-10 thomas switch (ch) {
8244 97a02382 2023-07-10 thomas case 'c':
8245 97a02382 2023-07-10 thomas commit_id_str = optarg;
8247 97a02382 2023-07-10 thomas case 'n':
8248 97a02382 2023-07-10 thomas nop = 1;
8250 97a02382 2023-07-10 thomas case 'p':
8251 97a02382 2023-07-10 thomas strip = strtonum(optarg, 0, INT_MAX, &errstr);
8252 97a02382 2023-07-10 thomas if (errstr != NULL)
8253 97a02382 2023-07-10 thomas errx(1, "pathname strip count is %s: %s",
8254 97a02382 2023-07-10 thomas errstr, optarg);
8256 97a02382 2023-07-10 thomas case 'R':
8257 97a02382 2023-07-10 thomas reverse = 1;
8259 97a02382 2023-07-10 thomas default:
8260 97a02382 2023-07-10 thomas usage_patch();
8261 97a02382 2023-07-10 thomas /* NOTREACHED */
8265 97a02382 2023-07-10 thomas argc -= optind;
8266 97a02382 2023-07-10 thomas argv += optind;
8268 97a02382 2023-07-10 thomas if (argc == 0) {
8269 97a02382 2023-07-10 thomas error = patch_from_stdin(&patchfd);
8270 97a02382 2023-07-10 thomas if (error)
8271 97a02382 2023-07-10 thomas return error;
8272 97a02382 2023-07-10 thomas } else if (argc == 1) {
8273 97a02382 2023-07-10 thomas patchfd = open(argv[0], O_RDONLY);
8274 97a02382 2023-07-10 thomas if (patchfd == -1) {
8275 97a02382 2023-07-10 thomas error = got_error_from_errno2("open", argv[0]);
8276 97a02382 2023-07-10 thomas return error;
8278 97a02382 2023-07-10 thomas if (fstat(patchfd, &sb) == -1) {
8279 97a02382 2023-07-10 thomas error = got_error_from_errno2("fstat", argv[0]);
8280 97a02382 2023-07-10 thomas goto done;
8282 97a02382 2023-07-10 thomas if (!S_ISREG(sb.st_mode)) {
8283 97a02382 2023-07-10 thomas error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8284 97a02382 2023-07-10 thomas goto done;
8287 97a02382 2023-07-10 thomas usage_patch();
8289 97a02382 2023-07-10 thomas if ((cwd = getcwd(NULL, 0)) == NULL) {
8290 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
8291 97a02382 2023-07-10 thomas goto done;
8294 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
8295 97a02382 2023-07-10 thomas if (error != NULL)
8296 97a02382 2023-07-10 thomas goto done;
8298 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
8299 97a02382 2023-07-10 thomas if (error != NULL)
8300 97a02382 2023-07-10 thomas goto done;
8302 97a02382 2023-07-10 thomas const char *repo_path = got_worktree_get_repo_path(worktree);
8303 97a02382 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8304 97a02382 2023-07-10 thomas if (error != NULL)
8305 97a02382 2023-07-10 thomas goto done;
8307 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
8308 97a02382 2023-07-10 thomas got_worktree_get_root_path(worktree));
8309 97a02382 2023-07-10 thomas if (error != NULL)
8310 97a02382 2023-07-10 thomas goto done;
8312 97a02382 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8313 97a02382 2023-07-10 thomas if (error)
8314 97a02382 2023-07-10 thomas goto done;
8316 97a02382 2023-07-10 thomas if (commit_id_str != NULL) {
8317 97a02382 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
8318 97a02382 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
8319 97a02382 2023-07-10 thomas if (error)
8320 97a02382 2023-07-10 thomas goto done;
8323 97a02382 2023-07-10 thomas memset(&ppa, 0, sizeof(ppa));
8324 97a02382 2023-07-10 thomas error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8325 97a02382 2023-07-10 thomas commit_id, patch_progress, &ppa, check_cancelled, NULL);
8326 97a02382 2023-07-10 thomas print_patch_progress_stats(&ppa);
8328 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
8329 97a02382 2023-07-10 thomas free(commit_id);
8330 97a02382 2023-07-10 thomas if (repo) {
8331 97a02382 2023-07-10 thomas close_error = got_repo_close(repo);
8332 97a02382 2023-07-10 thomas if (error == NULL)
8333 97a02382 2023-07-10 thomas error = close_error;
8335 97a02382 2023-07-10 thomas if (worktree != NULL) {
8336 97a02382 2023-07-10 thomas close_error = got_worktree_close(worktree);
8337 97a02382 2023-07-10 thomas if (error == NULL)
8338 97a02382 2023-07-10 thomas error = close_error;
8340 97a02382 2023-07-10 thomas if (pack_fds) {
8341 97a02382 2023-07-10 thomas const struct got_error *pack_err =
8342 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
8343 97a02382 2023-07-10 thomas if (error == NULL)
8344 97a02382 2023-07-10 thomas error = pack_err;
8346 97a02382 2023-07-10 thomas free(cwd);
8347 97a02382 2023-07-10 thomas return error;
8350 97a02382 2023-07-10 thomas __dead static void
8351 97a02382 2023-07-10 thomas usage_revert(void)
8353 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8354 97a02382 2023-07-10 thomas getprogname());
8355 97a02382 2023-07-10 thomas exit(1);
8358 97a02382 2023-07-10 thomas static const struct got_error *
8359 97a02382 2023-07-10 thomas revert_progress(void *arg, unsigned char status, const char *path)
8361 97a02382 2023-07-10 thomas if (status == GOT_STATUS_UNVERSIONED)
8362 97a02382 2023-07-10 thomas return NULL;
8364 97a02382 2023-07-10 thomas while (path[0] == '/')
8366 97a02382 2023-07-10 thomas printf("%c %s\n", status, path);
8367 97a02382 2023-07-10 thomas return NULL;
8370 97a02382 2023-07-10 thomas struct choose_patch_arg {
8371 97a02382 2023-07-10 thomas FILE *patch_script_file;
8372 97a02382 2023-07-10 thomas const char *action;
8375 97a02382 2023-07-10 thomas static const struct got_error *
8376 97a02382 2023-07-10 thomas show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8377 97a02382 2023-07-10 thomas int nchanges, const char *action)
8379 97a02382 2023-07-10 thomas const struct got_error *err;
8380 97a02382 2023-07-10 thomas char *line = NULL;
8381 97a02382 2023-07-10 thomas size_t linesize = 0;
8382 97a02382 2023-07-10 thomas ssize_t linelen;
8384 97a02382 2023-07-10 thomas switch (status) {
8385 97a02382 2023-07-10 thomas case GOT_STATUS_ADD:
8386 97a02382 2023-07-10 thomas printf("A %s\n%s this addition? [y/n] ", path, action);
8388 97a02382 2023-07-10 thomas case GOT_STATUS_DELETE:
8389 97a02382 2023-07-10 thomas printf("D %s\n%s this deletion? [y/n] ", path, action);
8391 97a02382 2023-07-10 thomas case GOT_STATUS_MODIFY:
8392 97a02382 2023-07-10 thomas if (fseek(patch_file, 0L, SEEK_SET) == -1)
8393 97a02382 2023-07-10 thomas return got_error_from_errno("fseek");
8394 97a02382 2023-07-10 thomas printf(GOT_COMMIT_SEP_STR);
8395 97a02382 2023-07-10 thomas while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8396 97a02382 2023-07-10 thomas printf("%s", line);
8397 97a02382 2023-07-10 thomas if (linelen == -1 && ferror(patch_file)) {
8398 97a02382 2023-07-10 thomas err = got_error_from_errno("getline");
8399 97a02382 2023-07-10 thomas free(line);
8400 97a02382 2023-07-10 thomas return err;
8402 97a02382 2023-07-10 thomas free(line);
8403 97a02382 2023-07-10 thomas printf(GOT_COMMIT_SEP_STR);
8404 97a02382 2023-07-10 thomas printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8405 97a02382 2023-07-10 thomas path, n, nchanges, action);
8407 97a02382 2023-07-10 thomas default:
8408 97a02382 2023-07-10 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8411 97a02382 2023-07-10 thomas fflush(stdout);
8412 97a02382 2023-07-10 thomas return NULL;
8415 97a02382 2023-07-10 thomas static const struct got_error *
8416 97a02382 2023-07-10 thomas choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8417 97a02382 2023-07-10 thomas FILE *patch_file, int n, int nchanges)
8419 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
8420 97a02382 2023-07-10 thomas char *line = NULL;
8421 97a02382 2023-07-10 thomas size_t linesize = 0;
8422 97a02382 2023-07-10 thomas ssize_t linelen;
8423 97a02382 2023-07-10 thomas int resp = ' ';
8424 97a02382 2023-07-10 thomas struct choose_patch_arg *a = arg;
8426 97a02382 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_NONE;
8428 97a02382 2023-07-10 thomas if (a->patch_script_file) {
8429 97a02382 2023-07-10 thomas char *nl;
8430 97a02382 2023-07-10 thomas err = show_change(status, path, patch_file, n, nchanges,
8431 97a02382 2023-07-10 thomas a->action);
8432 97a02382 2023-07-10 thomas if (err)
8433 97a02382 2023-07-10 thomas return err;
8434 97a02382 2023-07-10 thomas linelen = getline(&line, &linesize, a->patch_script_file);
8435 97a02382 2023-07-10 thomas if (linelen == -1) {
8436 97a02382 2023-07-10 thomas if (ferror(a->patch_script_file))
8437 97a02382 2023-07-10 thomas return got_error_from_errno("getline");
8438 97a02382 2023-07-10 thomas return NULL;
8440 97a02382 2023-07-10 thomas nl = strchr(line, '\n');
8442 97a02382 2023-07-10 thomas *nl = '\0';
8443 97a02382 2023-07-10 thomas if (strcmp(line, "y") == 0) {
8444 97a02382 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_YES;
8445 97a02382 2023-07-10 thomas printf("y\n");
8446 97a02382 2023-07-10 thomas } else if (strcmp(line, "n") == 0) {
8447 97a02382 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_NO;
8448 97a02382 2023-07-10 thomas printf("n\n");
8449 97a02382 2023-07-10 thomas } else if (strcmp(line, "q") == 0 &&
8450 97a02382 2023-07-10 thomas status == GOT_STATUS_MODIFY) {
8451 97a02382 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_QUIT;
8452 97a02382 2023-07-10 thomas printf("q\n");
8454 97a02382 2023-07-10 thomas printf("invalid response '%s'\n", line);
8455 97a02382 2023-07-10 thomas free(line);
8456 97a02382 2023-07-10 thomas return NULL;
8459 97a02382 2023-07-10 thomas while (resp != 'y' && resp != 'n' && resp != 'q') {
8460 97a02382 2023-07-10 thomas err = show_change(status, path, patch_file, n, nchanges,
8461 97a02382 2023-07-10 thomas a->action);
8462 97a02382 2023-07-10 thomas if (err)
8463 97a02382 2023-07-10 thomas return err;
8464 97a02382 2023-07-10 thomas resp = getchar();
8465 97a02382 2023-07-10 thomas if (resp == '\n')
8466 97a02382 2023-07-10 thomas resp = getchar();
8467 97a02382 2023-07-10 thomas if (status == GOT_STATUS_MODIFY) {
8468 97a02382 2023-07-10 thomas if (resp != 'y' && resp != 'n' && resp != 'q') {
8469 97a02382 2023-07-10 thomas printf("invalid response '%c'\n", resp);
8470 97a02382 2023-07-10 thomas resp = ' ';
8472 97a02382 2023-07-10 thomas } else if (resp != 'y' && resp != 'n') {
8473 97a02382 2023-07-10 thomas printf("invalid response '%c'\n", resp);
8474 97a02382 2023-07-10 thomas resp = ' ';
8478 97a02382 2023-07-10 thomas if (resp == 'y')
8479 97a02382 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_YES;
8480 97a02382 2023-07-10 thomas else if (resp == 'n')
8481 97a02382 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_NO;
8482 97a02382 2023-07-10 thomas else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8483 97a02382 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_QUIT;
8485 97a02382 2023-07-10 thomas return NULL;
8488 97a02382 2023-07-10 thomas struct wt_commitable_path_arg {
8489 97a02382 2023-07-10 thomas struct got_pathlist_head *commit_paths;
8490 97a02382 2023-07-10 thomas int *has_changes;
8494 97a02382 2023-07-10 thomas * Shortcut work tree status callback to determine if the set of paths scanned
8495 97a02382 2023-07-10 thomas * has at least one versioned path that is being modified and, if not NULL, is
8496 97a02382 2023-07-10 thomas * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
8497 97a02382 2023-07-10 thomas * soon as a path is passed with a status that satisfies this criteria.
8499 97a02382 2023-07-10 thomas static const struct got_error *
8500 97a02382 2023-07-10 thomas worktree_has_commitable_path(void *arg, unsigned char status,
8501 97a02382 2023-07-10 thomas unsigned char staged_status, const char *path,
8502 97a02382 2023-07-10 thomas struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8503 97a02382 2023-07-10 thomas struct got_object_id *commit_id, int dirfd, const char *de_name)
8505 97a02382 2023-07-10 thomas struct wt_commitable_path_arg *a = arg;
8507 97a02382 2023-07-10 thomas if (status == staged_status && (status == GOT_STATUS_DELETE))
8508 97a02382 2023-07-10 thomas status = GOT_STATUS_NO_CHANGE;
8510 97a02382 2023-07-10 thomas if (!(status == GOT_STATUS_NO_CHANGE ||
8511 97a02382 2023-07-10 thomas status == GOT_STATUS_UNVERSIONED) ||
8512 97a02382 2023-07-10 thomas staged_status != GOT_STATUS_NO_CHANGE) {
8513 97a02382 2023-07-10 thomas if (a->commit_paths != NULL) {
8514 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
8516 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, a->commit_paths, entry) {
8517 97a02382 2023-07-10 thomas if (strncmp(path, pe->path,
8518 97a02382 2023-07-10 thomas pe->path_len) == 0) {
8519 97a02382 2023-07-10 thomas *a->has_changes = 1;
8524 97a02382 2023-07-10 thomas *a->has_changes = 1;
8526 97a02382 2023-07-10 thomas if (*a->has_changes)
8527 97a02382 2023-07-10 thomas return got_error(GOT_ERR_FILE_MODIFIED);
8530 97a02382 2023-07-10 thomas return NULL;
8534 97a02382 2023-07-10 thomas * Check that the changeset of the commit identified by id is
8535 97a02382 2023-07-10 thomas * comprised of at least one modified path that is being committed.
8537 97a02382 2023-07-10 thomas static const struct got_error *
8538 97a02382 2023-07-10 thomas commit_path_changed_in_worktree(struct wt_commitable_path_arg *wcpa,
8539 97a02382 2023-07-10 thomas struct got_object_id *id, struct got_worktree *worktree,
8540 97a02382 2023-07-10 thomas struct got_repository *repo)
8542 97a02382 2023-07-10 thomas const struct got_error *err;
8543 97a02382 2023-07-10 thomas struct got_pathlist_head paths;
8544 97a02382 2023-07-10 thomas struct got_commit_object *commit = NULL, *pcommit = NULL;
8545 97a02382 2023-07-10 thomas struct got_tree_object *tree = NULL, *ptree = NULL;
8546 97a02382 2023-07-10 thomas struct got_object_qid *pid;
8548 97a02382 2023-07-10 thomas TAILQ_INIT(&paths);
8550 97a02382 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, id);
8551 97a02382 2023-07-10 thomas if (err)
8552 97a02382 2023-07-10 thomas goto done;
8554 97a02382 2023-07-10 thomas err = got_object_open_as_tree(&tree, repo,
8555 97a02382 2023-07-10 thomas got_object_commit_get_tree_id(commit));
8556 97a02382 2023-07-10 thomas if (err)
8557 97a02382 2023-07-10 thomas goto done;
8559 97a02382 2023-07-10 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8560 97a02382 2023-07-10 thomas if (pid != NULL) {
8561 97a02382 2023-07-10 thomas err = got_object_open_as_commit(&pcommit, repo, &pid->id);
8562 97a02382 2023-07-10 thomas if (err)
8563 97a02382 2023-07-10 thomas goto done;
8565 97a02382 2023-07-10 thomas err = got_object_open_as_tree(&ptree, repo,
8566 97a02382 2023-07-10 thomas got_object_commit_get_tree_id(pcommit));
8567 97a02382 2023-07-10 thomas if (err)
8568 97a02382 2023-07-10 thomas goto done;
8571 97a02382 2023-07-10 thomas err = got_diff_tree(ptree, tree, NULL, NULL, -1, -1, "", "", repo,
8572 97a02382 2023-07-10 thomas got_diff_tree_collect_changed_paths, &paths, 0);
8573 97a02382 2023-07-10 thomas if (err)
8574 97a02382 2023-07-10 thomas goto done;
8576 97a02382 2023-07-10 thomas err = got_worktree_status(worktree, &paths, repo, 0,
8577 97a02382 2023-07-10 thomas worktree_has_commitable_path, wcpa, check_cancelled, NULL);
8578 97a02382 2023-07-10 thomas if (err && err->code == GOT_ERR_FILE_MODIFIED) {
8580 97a02382 2023-07-10 thomas * At least one changed path in the referenced commit is
8581 97a02382 2023-07-10 thomas * modified in the work tree, that's all we need to know!
8583 97a02382 2023-07-10 thomas err = NULL;
8587 97a02382 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
8588 97a02382 2023-07-10 thomas if (commit)
8589 97a02382 2023-07-10 thomas got_object_commit_close(commit);
8590 97a02382 2023-07-10 thomas if (pcommit)
8591 97a02382 2023-07-10 thomas got_object_commit_close(pcommit);
8592 97a02382 2023-07-10 thomas if (tree)
8593 97a02382 2023-07-10 thomas got_object_tree_close(tree);
8594 97a02382 2023-07-10 thomas if (ptree)
8595 97a02382 2023-07-10 thomas got_object_tree_close(ptree);
8596 97a02382 2023-07-10 thomas return err;
8600 97a02382 2023-07-10 thomas * Remove any "logmsg" reference comprised entirely of paths that have
8601 97a02382 2023-07-10 thomas * been reverted in this work tree. If any path in the logmsg ref changeset
8602 97a02382 2023-07-10 thomas * remains in a changed state in the worktree, do not remove the reference.
8604 97a02382 2023-07-10 thomas static const struct got_error *
8605 97a02382 2023-07-10 thomas rm_logmsg_ref(struct got_worktree *worktree, struct got_repository *repo)
8607 97a02382 2023-07-10 thomas const struct got_error *err;
8608 97a02382 2023-07-10 thomas struct got_reflist_head refs;
8609 97a02382 2023-07-10 thomas struct got_reflist_entry *re;
8610 97a02382 2023-07-10 thomas struct got_commit_object *commit = NULL;
8611 97a02382 2023-07-10 thomas struct got_object_id *commit_id = NULL;
8612 97a02382 2023-07-10 thomas struct wt_commitable_path_arg wcpa;
8613 97a02382 2023-07-10 thomas char *uuidstr = NULL;
8615 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
8617 97a02382 2023-07-10 thomas err = got_worktree_get_uuid(&uuidstr, worktree);
8618 97a02382 2023-07-10 thomas if (err)
8619 97a02382 2023-07-10 thomas goto done;
8621 97a02382 2023-07-10 thomas err = got_ref_list(&refs, repo, "refs/got/worktree",
8622 97a02382 2023-07-10 thomas got_ref_cmp_by_name, repo);
8623 97a02382 2023-07-10 thomas if (err)
8624 97a02382 2023-07-10 thomas goto done;
8626 97a02382 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
8627 97a02382 2023-07-10 thomas const char *refname;
8628 97a02382 2023-07-10 thomas int has_changes = 0;
8630 97a02382 2023-07-10 thomas refname = got_ref_get_name(re->ref);
8632 97a02382 2023-07-10 thomas if (!strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8633 97a02382 2023-07-10 thomas GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN))
8634 97a02382 2023-07-10 thomas refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8635 97a02382 2023-07-10 thomas else if (!strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8636 97a02382 2023-07-10 thomas GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN))
8637 97a02382 2023-07-10 thomas refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8639 97a02382 2023-07-10 thomas continue;
8641 97a02382 2023-07-10 thomas if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8642 97a02382 2023-07-10 thomas refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8644 97a02382 2023-07-10 thomas continue;
8646 97a02382 2023-07-10 thomas err = got_repo_match_object_id(&commit_id, NULL, refname,
8647 97a02382 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
8648 97a02382 2023-07-10 thomas if (err)
8649 97a02382 2023-07-10 thomas goto done;
8651 97a02382 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
8652 97a02382 2023-07-10 thomas if (err)
8653 97a02382 2023-07-10 thomas goto done;
8655 97a02382 2023-07-10 thomas wcpa.commit_paths = NULL;
8656 97a02382 2023-07-10 thomas wcpa.has_changes = &has_changes;
8658 97a02382 2023-07-10 thomas err = commit_path_changed_in_worktree(&wcpa, commit_id,
8659 97a02382 2023-07-10 thomas worktree, repo);
8660 97a02382 2023-07-10 thomas if (err)
8661 97a02382 2023-07-10 thomas goto done;
8663 97a02382 2023-07-10 thomas if (!has_changes) {
8664 97a02382 2023-07-10 thomas err = got_ref_delete(re->ref, repo);
8665 97a02382 2023-07-10 thomas if (err)
8666 97a02382 2023-07-10 thomas goto done;
8669 97a02382 2023-07-10 thomas got_object_commit_close(commit);
8670 97a02382 2023-07-10 thomas commit = NULL;
8671 97a02382 2023-07-10 thomas free(commit_id);
8672 97a02382 2023-07-10 thomas commit_id = NULL;
8676 97a02382 2023-07-10 thomas free(uuidstr);
8677 97a02382 2023-07-10 thomas free(commit_id);
8678 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
8679 97a02382 2023-07-10 thomas if (commit)
8680 97a02382 2023-07-10 thomas got_object_commit_close(commit);
8681 97a02382 2023-07-10 thomas return err;
8684 97a02382 2023-07-10 thomas static const struct got_error *
8685 97a02382 2023-07-10 thomas cmd_revert(int argc, char *argv[])
8687 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
8688 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
8689 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
8690 97a02382 2023-07-10 thomas char *cwd = NULL, *path = NULL;
8691 97a02382 2023-07-10 thomas struct got_pathlist_head paths;
8692 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
8693 97a02382 2023-07-10 thomas int ch, can_recurse = 0, pflag = 0;
8694 97a02382 2023-07-10 thomas FILE *patch_script_file = NULL;
8695 97a02382 2023-07-10 thomas const char *patch_script_path = NULL;
8696 97a02382 2023-07-10 thomas struct choose_patch_arg cpa;
8697 97a02382 2023-07-10 thomas int *pack_fds = NULL;
8699 97a02382 2023-07-10 thomas TAILQ_INIT(&paths);
8701 97a02382 2023-07-10 thomas #ifndef PROFILE
8702 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8703 97a02382 2023-07-10 thomas "unveil", NULL) == -1)
8704 97a02382 2023-07-10 thomas err(1, "pledge");
8707 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "F:pR")) != -1) {
8708 97a02382 2023-07-10 thomas switch (ch) {
8709 97a02382 2023-07-10 thomas case 'F':
8710 97a02382 2023-07-10 thomas patch_script_path = optarg;
8712 97a02382 2023-07-10 thomas case 'p':
8713 97a02382 2023-07-10 thomas pflag = 1;
8715 97a02382 2023-07-10 thomas case 'R':
8716 97a02382 2023-07-10 thomas can_recurse = 1;
8718 97a02382 2023-07-10 thomas default:
8719 97a02382 2023-07-10 thomas usage_revert();
8720 97a02382 2023-07-10 thomas /* NOTREACHED */
8724 97a02382 2023-07-10 thomas argc -= optind;
8725 97a02382 2023-07-10 thomas argv += optind;
8727 97a02382 2023-07-10 thomas if (argc < 1)
8728 97a02382 2023-07-10 thomas usage_revert();
8729 97a02382 2023-07-10 thomas if (patch_script_path && !pflag)
8730 97a02382 2023-07-10 thomas errx(1, "-F option can only be used together with -p option");
8732 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
8733 97a02382 2023-07-10 thomas if (cwd == NULL) {
8734 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
8735 97a02382 2023-07-10 thomas goto done;
8738 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
8739 97a02382 2023-07-10 thomas if (error != NULL)
8740 97a02382 2023-07-10 thomas goto done;
8742 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
8743 97a02382 2023-07-10 thomas if (error) {
8744 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
8745 97a02382 2023-07-10 thomas error = wrap_not_worktree_error(error, "revert", cwd);
8746 97a02382 2023-07-10 thomas goto done;
8749 97a02382 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8750 97a02382 2023-07-10 thomas NULL, pack_fds);
8751 97a02382 2023-07-10 thomas if (error != NULL)
8752 97a02382 2023-07-10 thomas goto done;
8754 97a02382 2023-07-10 thomas if (patch_script_path) {
8755 97a02382 2023-07-10 thomas patch_script_file = fopen(patch_script_path, "re");
8756 97a02382 2023-07-10 thomas if (patch_script_file == NULL) {
8757 97a02382 2023-07-10 thomas error = got_error_from_errno2("fopen",
8758 97a02382 2023-07-10 thomas patch_script_path);
8759 97a02382 2023-07-10 thomas goto done;
8764 97a02382 2023-07-10 thomas * XXX "c" perm needed on repo dir to delete merge references.
8766 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
8767 97a02382 2023-07-10 thomas got_worktree_get_root_path(worktree));
8768 97a02382 2023-07-10 thomas if (error)
8769 97a02382 2023-07-10 thomas goto done;
8771 97a02382 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8772 97a02382 2023-07-10 thomas if (error)
8773 97a02382 2023-07-10 thomas goto done;
8775 97a02382 2023-07-10 thomas if (!can_recurse) {
8776 97a02382 2023-07-10 thomas char *ondisk_path;
8777 97a02382 2023-07-10 thomas struct stat sb;
8778 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, &paths, entry) {
8779 97a02382 2023-07-10 thomas if (asprintf(&ondisk_path, "%s/%s",
8780 97a02382 2023-07-10 thomas got_worktree_get_root_path(worktree),
8781 97a02382 2023-07-10 thomas pe->path) == -1) {
8782 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
8783 97a02382 2023-07-10 thomas goto done;
8785 97a02382 2023-07-10 thomas if (lstat(ondisk_path, &sb) == -1) {
8786 97a02382 2023-07-10 thomas if (errno == ENOENT) {
8787 97a02382 2023-07-10 thomas free(ondisk_path);
8788 97a02382 2023-07-10 thomas continue;
8790 97a02382 2023-07-10 thomas error = got_error_from_errno2("lstat",
8791 97a02382 2023-07-10 thomas ondisk_path);
8792 97a02382 2023-07-10 thomas free(ondisk_path);
8793 97a02382 2023-07-10 thomas goto done;
8795 97a02382 2023-07-10 thomas free(ondisk_path);
8796 97a02382 2023-07-10 thomas if (S_ISDIR(sb.st_mode)) {
8797 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_BAD_PATH,
8798 97a02382 2023-07-10 thomas "reverting directories requires -R option");
8799 97a02382 2023-07-10 thomas goto done;
8804 97a02382 2023-07-10 thomas cpa.patch_script_file = patch_script_file;
8805 97a02382 2023-07-10 thomas cpa.action = "revert";
8806 97a02382 2023-07-10 thomas error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8807 97a02382 2023-07-10 thomas pflag ? choose_patch : NULL, &cpa, repo);
8809 97a02382 2023-07-10 thomas error = rm_logmsg_ref(worktree, repo);
8811 97a02382 2023-07-10 thomas if (patch_script_file && fclose(patch_script_file) == EOF &&
8812 97a02382 2023-07-10 thomas error == NULL)
8813 97a02382 2023-07-10 thomas error = got_error_from_errno2("fclose", patch_script_path);
8814 97a02382 2023-07-10 thomas if (repo) {
8815 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
8816 97a02382 2023-07-10 thomas if (error == NULL)
8817 97a02382 2023-07-10 thomas error = close_err;
8819 97a02382 2023-07-10 thomas if (worktree)
8820 97a02382 2023-07-10 thomas got_worktree_close(worktree);
8821 97a02382 2023-07-10 thomas if (pack_fds) {
8822 97a02382 2023-07-10 thomas const struct got_error *pack_err =
8823 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
8824 97a02382 2023-07-10 thomas if (error == NULL)
8825 97a02382 2023-07-10 thomas error = pack_err;
8827 97a02382 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8828 97a02382 2023-07-10 thomas free(path);
8829 97a02382 2023-07-10 thomas free(cwd);
8830 97a02382 2023-07-10 thomas return error;
8833 97a02382 2023-07-10 thomas __dead static void
8834 97a02382 2023-07-10 thomas usage_commit(void)
8836 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s commit [-CNnS] [-A author] [-F path] "
8837 97a02382 2023-07-10 thomas "[-m message] [path ...]\n", getprogname());
8838 97a02382 2023-07-10 thomas exit(1);
8841 97a02382 2023-07-10 thomas struct collect_commit_logmsg_arg {
8842 97a02382 2023-07-10 thomas const char *cmdline_log;
8843 97a02382 2023-07-10 thomas const char *prepared_log;
8844 97a02382 2023-07-10 thomas const char *merged_log;
8845 97a02382 2023-07-10 thomas int non_interactive;
8846 97a02382 2023-07-10 thomas const char *editor;
8847 97a02382 2023-07-10 thomas const char *worktree_path;
8848 97a02382 2023-07-10 thomas const char *branch_name;
8849 97a02382 2023-07-10 thomas const char *repo_path;
8850 97a02382 2023-07-10 thomas char *logmsg_path;
8854 97a02382 2023-07-10 thomas static const struct got_error *
8855 97a02382 2023-07-10 thomas read_prepared_logmsg(char **logmsg, const char *path)
8857 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
8858 97a02382 2023-07-10 thomas FILE *f = NULL;
8859 97a02382 2023-07-10 thomas struct stat sb;
8860 97a02382 2023-07-10 thomas size_t r;
8862 97a02382 2023-07-10 thomas *logmsg = NULL;
8863 97a02382 2023-07-10 thomas memset(&sb, 0, sizeof(sb));
8865 97a02382 2023-07-10 thomas f = fopen(path, "re");
8866 97a02382 2023-07-10 thomas if (f == NULL)
8867 97a02382 2023-07-10 thomas return got_error_from_errno2("fopen", path);
8869 97a02382 2023-07-10 thomas if (fstat(fileno(f), &sb) == -1) {
8870 97a02382 2023-07-10 thomas err = got_error_from_errno2("fstat", path);
8871 97a02382 2023-07-10 thomas goto done;
8873 97a02382 2023-07-10 thomas if (sb.st_size == 0) {
8874 97a02382 2023-07-10 thomas err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8875 97a02382 2023-07-10 thomas goto done;
8878 97a02382 2023-07-10 thomas *logmsg = malloc(sb.st_size + 1);
8879 97a02382 2023-07-10 thomas if (*logmsg == NULL) {
8880 97a02382 2023-07-10 thomas err = got_error_from_errno("malloc");
8881 97a02382 2023-07-10 thomas goto done;
8884 97a02382 2023-07-10 thomas r = fread(*logmsg, 1, sb.st_size, f);
8885 97a02382 2023-07-10 thomas if (r != sb.st_size) {
8886 97a02382 2023-07-10 thomas if (ferror(f))
8887 97a02382 2023-07-10 thomas err = got_error_from_errno2("fread", path);
8889 97a02382 2023-07-10 thomas err = got_error(GOT_ERR_IO);
8890 97a02382 2023-07-10 thomas goto done;
8892 97a02382 2023-07-10 thomas (*logmsg)[sb.st_size] = '\0';
8894 97a02382 2023-07-10 thomas if (fclose(f) == EOF && err == NULL)
8895 97a02382 2023-07-10 thomas err = got_error_from_errno2("fclose", path);
8896 97a02382 2023-07-10 thomas if (err) {
8897 97a02382 2023-07-10 thomas free(*logmsg);
8898 97a02382 2023-07-10 thomas *logmsg = NULL;
8900 97a02382 2023-07-10 thomas return err;
8903 97a02382 2023-07-10 thomas static const struct got_error *
8904 97a02382 2023-07-10 thomas collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
8905 97a02382 2023-07-10 thomas const char *diff_path, char **logmsg, void *arg)
8907 97a02382 2023-07-10 thomas char *initial_content = NULL;
8908 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
8909 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
8910 97a02382 2023-07-10 thomas char *template = NULL;
8911 97a02382 2023-07-10 thomas char *prepared_msg = NULL, *merged_msg = NULL;
8912 97a02382 2023-07-10 thomas struct collect_commit_logmsg_arg *a = arg;
8913 97a02382 2023-07-10 thomas int initial_content_len;
8914 97a02382 2023-07-10 thomas int fd = -1;
8915 97a02382 2023-07-10 thomas size_t len;
8917 97a02382 2023-07-10 thomas /* if a message was specified on the command line, just use it */
8918 97a02382 2023-07-10 thomas if (a->cmdline_log != NULL && *a->cmdline_log != '\0') {
8919 97a02382 2023-07-10 thomas len = strlen(a->cmdline_log) + 1;
8920 97a02382 2023-07-10 thomas *logmsg = malloc(len + 1);
8921 97a02382 2023-07-10 thomas if (*logmsg == NULL)
8922 97a02382 2023-07-10 thomas return got_error_from_errno("malloc");
8923 97a02382 2023-07-10 thomas strlcpy(*logmsg, a->cmdline_log, len);
8924 97a02382 2023-07-10 thomas return NULL;
8925 97a02382 2023-07-10 thomas } else if (a->prepared_log != NULL && a->non_interactive)
8926 97a02382 2023-07-10 thomas return read_prepared_logmsg(logmsg, a->prepared_log);
8928 97a02382 2023-07-10 thomas if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8929 97a02382 2023-07-10 thomas return got_error_from_errno("asprintf");
8931 97a02382 2023-07-10 thomas err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
8932 97a02382 2023-07-10 thomas if (err)
8933 97a02382 2023-07-10 thomas goto done;
8935 97a02382 2023-07-10 thomas if (a->prepared_log) {
8936 97a02382 2023-07-10 thomas err = read_prepared_logmsg(&prepared_msg, a->prepared_log);
8937 97a02382 2023-07-10 thomas if (err)
8938 97a02382 2023-07-10 thomas goto done;
8939 97a02382 2023-07-10 thomas } else if (a->merged_log) {
8940 97a02382 2023-07-10 thomas err = read_prepared_logmsg(&merged_msg, a->merged_log);
8941 97a02382 2023-07-10 thomas if (err)
8942 97a02382 2023-07-10 thomas goto done;
8945 97a02382 2023-07-10 thomas initial_content_len = asprintf(&initial_content,
8946 97a02382 2023-07-10 thomas "%s%s\n# changes to be committed on branch %s:\n",
8947 97a02382 2023-07-10 thomas prepared_msg ? prepared_msg : "",
8948 97a02382 2023-07-10 thomas merged_msg ? merged_msg : "", a->branch_name);
8949 97a02382 2023-07-10 thomas if (initial_content_len == -1) {
8950 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
8951 97a02382 2023-07-10 thomas goto done;
8954 97a02382 2023-07-10 thomas if (write(fd, initial_content, initial_content_len) == -1) {
8955 97a02382 2023-07-10 thomas err = got_error_from_errno2("write", a->logmsg_path);
8956 97a02382 2023-07-10 thomas goto done;
8959 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, commitable_paths, entry) {
8960 97a02382 2023-07-10 thomas struct got_commitable *ct = pe->data;
8961 97a02382 2023-07-10 thomas dprintf(fd, "# %c %s\n",
8962 97a02382 2023-07-10 thomas got_commitable_get_status(ct),
8963 97a02382 2023-07-10 thomas got_commitable_get_path(ct));
8966 97a02382 2023-07-10 thomas if (diff_path) {
8967 97a02382 2023-07-10 thomas dprintf(fd, "# detailed changes can be viewed in %s\n",
8968 97a02382 2023-07-10 thomas diff_path);
8971 97a02382 2023-07-10 thomas if (close(fd) == -1) {
8972 97a02382 2023-07-10 thomas err = got_error_from_errno2("close", a->logmsg_path);
8973 97a02382 2023-07-10 thomas goto done;
8975 97a02382 2023-07-10 thomas fd = -1;
8977 97a02382 2023-07-10 thomas err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8978 97a02382 2023-07-10 thomas initial_content_len, a->prepared_log ? 0 : 1);
8980 97a02382 2023-07-10 thomas free(initial_content);
8981 97a02382 2023-07-10 thomas free(template);
8982 97a02382 2023-07-10 thomas free(prepared_msg);
8983 97a02382 2023-07-10 thomas free(merged_msg);
8985 97a02382 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
8986 97a02382 2023-07-10 thomas err = got_error_from_errno2("close", a->logmsg_path);
8988 97a02382 2023-07-10 thomas /* Editor is done; we can now apply unveil(2) */
8989 97a02382 2023-07-10 thomas if (err == NULL)
8990 97a02382 2023-07-10 thomas err = apply_unveil(a->repo_path, 0, a->worktree_path);
8991 97a02382 2023-07-10 thomas if (err) {
8992 97a02382 2023-07-10 thomas free(*logmsg);
8993 97a02382 2023-07-10 thomas *logmsg = NULL;
8995 97a02382 2023-07-10 thomas return err;
8998 97a02382 2023-07-10 thomas static const struct got_error *
8999 97a02382 2023-07-10 thomas cat_logmsg(FILE *f, struct got_commit_object *commit, const char *idstr,
9000 97a02382 2023-07-10 thomas const char *type, int has_content)
9002 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
9003 97a02382 2023-07-10 thomas char *logmsg = NULL;
9005 97a02382 2023-07-10 thomas err = got_object_commit_get_logmsg(&logmsg, commit);
9006 97a02382 2023-07-10 thomas if (err)
9007 97a02382 2023-07-10 thomas return err;
9009 97a02382 2023-07-10 thomas if (fprintf(f, "%s# log message of %s commit %s:%s",
9010 97a02382 2023-07-10 thomas has_content ? "\n" : "", type, idstr, logmsg) < 0)
9011 97a02382 2023-07-10 thomas err = got_ferror(f, GOT_ERR_IO);
9013 97a02382 2023-07-10 thomas free(logmsg);
9014 97a02382 2023-07-10 thomas return err;
9018 97a02382 2023-07-10 thomas * Lookup "logmsg" references of backed-out and cherrypicked commits
9019 97a02382 2023-07-10 thomas * belonging to the current work tree. If found, and the worktree has
9020 97a02382 2023-07-10 thomas * at least one modified file that was changed in the referenced commit,
9021 97a02382 2023-07-10 thomas * add its log message to a new temporary file at *logmsg_path.
9022 97a02382 2023-07-10 thomas * Add all refs found to matched_refs to be scheduled for removal on
9023 97a02382 2023-07-10 thomas * successful commit.
9025 97a02382 2023-07-10 thomas static const struct got_error *
9026 97a02382 2023-07-10 thomas lookup_logmsg_ref(char **logmsg_path, struct got_pathlist_head *paths,
9027 97a02382 2023-07-10 thomas struct got_reflist_head *matched_refs, struct got_worktree *worktree,
9028 97a02382 2023-07-10 thomas struct got_repository *repo)
9030 97a02382 2023-07-10 thomas const struct got_error *err;
9031 97a02382 2023-07-10 thomas struct got_commit_object *commit = NULL;
9032 97a02382 2023-07-10 thomas struct got_object_id *id = NULL;
9033 97a02382 2023-07-10 thomas struct got_reflist_head refs;
9034 97a02382 2023-07-10 thomas struct got_reflist_entry *re, *re_match;
9035 97a02382 2023-07-10 thomas FILE *f = NULL;
9036 97a02382 2023-07-10 thomas char *uuidstr = NULL;
9037 97a02382 2023-07-10 thomas int added_logmsg = 0;
9039 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
9041 97a02382 2023-07-10 thomas *logmsg_path = NULL;
9043 97a02382 2023-07-10 thomas err = got_worktree_get_uuid(&uuidstr, worktree);
9044 97a02382 2023-07-10 thomas if (err)
9045 97a02382 2023-07-10 thomas goto done;
9047 97a02382 2023-07-10 thomas err = got_ref_list(&refs, repo, "refs/got/worktree",
9048 97a02382 2023-07-10 thomas got_ref_cmp_by_name, repo);
9049 97a02382 2023-07-10 thomas if (err)
9050 97a02382 2023-07-10 thomas goto done;
9052 97a02382 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
9053 97a02382 2023-07-10 thomas const char *refname, *type;
9054 97a02382 2023-07-10 thomas struct wt_commitable_path_arg wcpa;
9055 97a02382 2023-07-10 thomas int add_logmsg = 0;
9057 97a02382 2023-07-10 thomas refname = got_ref_get_name(re->ref);
9059 97a02382 2023-07-10 thomas if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
9060 97a02382 2023-07-10 thomas GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
9061 97a02382 2023-07-10 thomas refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
9062 97a02382 2023-07-10 thomas type = "cherrypicked";
9063 97a02382 2023-07-10 thomas } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
9064 97a02382 2023-07-10 thomas GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
9065 97a02382 2023-07-10 thomas refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
9066 97a02382 2023-07-10 thomas type = "backed-out";
9068 97a02382 2023-07-10 thomas continue;
9070 97a02382 2023-07-10 thomas if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
9071 97a02382 2023-07-10 thomas refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9073 97a02382 2023-07-10 thomas continue;
9075 97a02382 2023-07-10 thomas err = got_repo_match_object_id(&id, NULL, refname,
9076 97a02382 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
9077 97a02382 2023-07-10 thomas if (err)
9078 97a02382 2023-07-10 thomas goto done;
9080 97a02382 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, id);
9081 97a02382 2023-07-10 thomas if (err)
9082 97a02382 2023-07-10 thomas goto done;
9084 97a02382 2023-07-10 thomas wcpa.commit_paths = paths;
9085 97a02382 2023-07-10 thomas wcpa.has_changes = &add_logmsg;
9087 97a02382 2023-07-10 thomas err = commit_path_changed_in_worktree(&wcpa, id,
9088 97a02382 2023-07-10 thomas worktree, repo);
9089 97a02382 2023-07-10 thomas if (err)
9090 97a02382 2023-07-10 thomas goto done;
9092 97a02382 2023-07-10 thomas if (add_logmsg) {
9093 97a02382 2023-07-10 thomas if (f == NULL) {
9094 97a02382 2023-07-10 thomas err = got_opentemp_named(logmsg_path, &f,
9095 97a02382 2023-07-10 thomas "got-commit-logmsg", "");
9096 97a02382 2023-07-10 thomas if (err)
9097 97a02382 2023-07-10 thomas goto done;
9099 97a02382 2023-07-10 thomas err = cat_logmsg(f, commit, refname, type,
9100 97a02382 2023-07-10 thomas added_logmsg);
9101 97a02382 2023-07-10 thomas if (err)
9102 97a02382 2023-07-10 thomas goto done;
9103 97a02382 2023-07-10 thomas if (!added_logmsg)
9104 97a02382 2023-07-10 thomas ++added_logmsg;
9106 97a02382 2023-07-10 thomas err = got_reflist_entry_dup(&re_match, re);
9107 97a02382 2023-07-10 thomas if (err)
9108 97a02382 2023-07-10 thomas goto done;
9109 97a02382 2023-07-10 thomas TAILQ_INSERT_HEAD(matched_refs, re_match, entry);
9112 97a02382 2023-07-10 thomas got_object_commit_close(commit);
9113 97a02382 2023-07-10 thomas commit = NULL;
9114 97a02382 2023-07-10 thomas free(id);
9115 97a02382 2023-07-10 thomas id = NULL;
9119 97a02382 2023-07-10 thomas free(id);
9120 97a02382 2023-07-10 thomas free(uuidstr);
9121 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
9122 97a02382 2023-07-10 thomas if (commit)
9123 97a02382 2023-07-10 thomas got_object_commit_close(commit);
9124 97a02382 2023-07-10 thomas if (f && fclose(f) == EOF && err == NULL)
9125 97a02382 2023-07-10 thomas err = got_error_from_errno("fclose");
9126 97a02382 2023-07-10 thomas if (!added_logmsg) {
9127 97a02382 2023-07-10 thomas if (*logmsg_path && unlink(*logmsg_path) != 0 && err == NULL)
9128 97a02382 2023-07-10 thomas err = got_error_from_errno2("unlink", *logmsg_path);
9129 97a02382 2023-07-10 thomas *logmsg_path = NULL;
9131 97a02382 2023-07-10 thomas return err;
9134 97a02382 2023-07-10 thomas static const struct got_error *
9135 97a02382 2023-07-10 thomas cmd_commit(int argc, char *argv[])
9137 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
9138 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
9139 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
9140 97a02382 2023-07-10 thomas char *cwd = NULL, *id_str = NULL;
9141 97a02382 2023-07-10 thomas struct got_object_id *id = NULL;
9142 97a02382 2023-07-10 thomas const char *logmsg = NULL;
9143 97a02382 2023-07-10 thomas char *prepared_logmsg = NULL, *merged_logmsg = NULL;
9144 97a02382 2023-07-10 thomas struct collect_commit_logmsg_arg cl_arg;
9145 97a02382 2023-07-10 thomas const char *author = NULL;
9146 97a02382 2023-07-10 thomas char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
9147 97a02382 2023-07-10 thomas int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
9148 97a02382 2023-07-10 thomas int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
9149 97a02382 2023-07-10 thomas int show_diff = 1, commit_conflicts = 0;
9150 97a02382 2023-07-10 thomas struct got_pathlist_head paths;
9151 97a02382 2023-07-10 thomas struct got_reflist_head refs;
9152 97a02382 2023-07-10 thomas struct got_reflist_entry *re;
9153 97a02382 2023-07-10 thomas int *pack_fds = NULL;
9155 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
9156 97a02382 2023-07-10 thomas TAILQ_INIT(&paths);
9157 97a02382 2023-07-10 thomas cl_arg.logmsg_path = NULL;
9159 97a02382 2023-07-10 thomas #ifndef PROFILE
9160 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9161 97a02382 2023-07-10 thomas "unveil", NULL) == -1)
9162 97a02382 2023-07-10 thomas err(1, "pledge");
9165 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "A:CF:m:NnS")) != -1) {
9166 97a02382 2023-07-10 thomas switch (ch) {
9167 97a02382 2023-07-10 thomas case 'A':
9168 97a02382 2023-07-10 thomas author = optarg;
9169 97a02382 2023-07-10 thomas error = valid_author(author);
9170 97a02382 2023-07-10 thomas if (error)
9171 97a02382 2023-07-10 thomas return error;
9173 97a02382 2023-07-10 thomas case 'C':
9174 97a02382 2023-07-10 thomas commit_conflicts = 1;
9176 97a02382 2023-07-10 thomas case 'F':
9177 97a02382 2023-07-10 thomas if (logmsg != NULL)
9178 97a02382 2023-07-10 thomas option_conflict('F', 'm');
9179 97a02382 2023-07-10 thomas prepared_logmsg = realpath(optarg, NULL);
9180 97a02382 2023-07-10 thomas if (prepared_logmsg == NULL)
9181 97a02382 2023-07-10 thomas return got_error_from_errno2("realpath",
9182 97a02382 2023-07-10 thomas optarg);
9184 97a02382 2023-07-10 thomas case 'm':
9185 97a02382 2023-07-10 thomas if (prepared_logmsg)
9186 97a02382 2023-07-10 thomas option_conflict('m', 'F');
9187 97a02382 2023-07-10 thomas logmsg = optarg;
9189 97a02382 2023-07-10 thomas case 'N':
9190 97a02382 2023-07-10 thomas non_interactive = 1;
9192 97a02382 2023-07-10 thomas case 'n':
9193 97a02382 2023-07-10 thomas show_diff = 0;
9195 97a02382 2023-07-10 thomas case 'S':
9196 97a02382 2023-07-10 thomas allow_bad_symlinks = 1;
9198 97a02382 2023-07-10 thomas default:
9199 97a02382 2023-07-10 thomas usage_commit();
9200 97a02382 2023-07-10 thomas /* NOTREACHED */
9204 97a02382 2023-07-10 thomas argc -= optind;
9205 97a02382 2023-07-10 thomas argv += optind;
9207 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
9208 97a02382 2023-07-10 thomas if (cwd == NULL) {
9209 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
9210 97a02382 2023-07-10 thomas goto done;
9213 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
9214 97a02382 2023-07-10 thomas if (error != NULL)
9215 97a02382 2023-07-10 thomas goto done;
9217 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
9218 97a02382 2023-07-10 thomas if (error) {
9219 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
9220 97a02382 2023-07-10 thomas error = wrap_not_worktree_error(error, "commit", cwd);
9221 97a02382 2023-07-10 thomas goto done;
9224 97a02382 2023-07-10 thomas error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9225 97a02382 2023-07-10 thomas if (error)
9226 97a02382 2023-07-10 thomas goto done;
9227 97a02382 2023-07-10 thomas if (rebase_in_progress) {
9228 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_REBASING);
9229 97a02382 2023-07-10 thomas goto done;
9232 97a02382 2023-07-10 thomas error = got_worktree_histedit_in_progress(&histedit_in_progress,
9233 97a02382 2023-07-10 thomas worktree);
9234 97a02382 2023-07-10 thomas if (error)
9235 97a02382 2023-07-10 thomas goto done;
9237 97a02382 2023-07-10 thomas error = get_gitconfig_path(&gitconfig_path);
9238 97a02382 2023-07-10 thomas if (error)
9239 97a02382 2023-07-10 thomas goto done;
9240 97a02382 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9241 97a02382 2023-07-10 thomas gitconfig_path, pack_fds);
9242 97a02382 2023-07-10 thomas if (error != NULL)
9243 97a02382 2023-07-10 thomas goto done;
9245 97a02382 2023-07-10 thomas error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
9246 97a02382 2023-07-10 thomas if (error)
9247 97a02382 2023-07-10 thomas goto done;
9248 97a02382 2023-07-10 thomas if (merge_in_progress) {
9249 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_MERGE_BUSY);
9250 97a02382 2023-07-10 thomas goto done;
9253 97a02382 2023-07-10 thomas error = get_author(&committer, repo, worktree);
9254 97a02382 2023-07-10 thomas if (error)
9255 97a02382 2023-07-10 thomas goto done;
9257 97a02382 2023-07-10 thomas if (author == NULL)
9258 97a02382 2023-07-10 thomas author = committer;
9261 97a02382 2023-07-10 thomas * unveil(2) traverses exec(2); if an editor is used we have
9262 97a02382 2023-07-10 thomas * to apply unveil after the log message has been written.
9264 97a02382 2023-07-10 thomas if (logmsg == NULL || strlen(logmsg) == 0)
9265 97a02382 2023-07-10 thomas error = get_editor(&editor);
9267 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
9268 97a02382 2023-07-10 thomas got_worktree_get_root_path(worktree));
9269 97a02382 2023-07-10 thomas if (error)
9270 97a02382 2023-07-10 thomas goto done;
9272 97a02382 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9273 97a02382 2023-07-10 thomas if (error)
9274 97a02382 2023-07-10 thomas goto done;
9276 97a02382 2023-07-10 thomas if (prepared_logmsg == NULL) {
9277 97a02382 2023-07-10 thomas error = lookup_logmsg_ref(&merged_logmsg,
9278 97a02382 2023-07-10 thomas argc > 0 ? &paths : NULL, &refs, worktree, repo);
9279 97a02382 2023-07-10 thomas if (error)
9280 97a02382 2023-07-10 thomas goto done;
9283 97a02382 2023-07-10 thomas cl_arg.editor = editor;
9284 97a02382 2023-07-10 thomas cl_arg.cmdline_log = logmsg;
9285 97a02382 2023-07-10 thomas cl_arg.prepared_log = prepared_logmsg;
9286 97a02382 2023-07-10 thomas cl_arg.merged_log = merged_logmsg;
9287 97a02382 2023-07-10 thomas cl_arg.non_interactive = non_interactive;
9288 97a02382 2023-07-10 thomas cl_arg.worktree_path = got_worktree_get_root_path(worktree);
9289 97a02382 2023-07-10 thomas cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
9290 97a02382 2023-07-10 thomas if (!histedit_in_progress) {
9291 97a02382 2023-07-10 thomas if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
9292 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_COMMIT_BRANCH);
9293 97a02382 2023-07-10 thomas goto done;
9295 97a02382 2023-07-10 thomas cl_arg.branch_name += 11;
9297 97a02382 2023-07-10 thomas cl_arg.repo_path = got_repo_get_path(repo);
9298 97a02382 2023-07-10 thomas error = got_worktree_commit(&id, worktree, &paths, author, committer,
9299 97a02382 2023-07-10 thomas allow_bad_symlinks, show_diff, commit_conflicts,
9300 97a02382 2023-07-10 thomas collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
9301 97a02382 2023-07-10 thomas if (error) {
9302 97a02382 2023-07-10 thomas if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
9303 97a02382 2023-07-10 thomas cl_arg.logmsg_path != NULL)
9304 97a02382 2023-07-10 thomas preserve_logmsg = 1;
9305 97a02382 2023-07-10 thomas goto done;
9308 97a02382 2023-07-10 thomas error = got_object_id_str(&id_str, id);
9309 97a02382 2023-07-10 thomas if (error)
9310 97a02382 2023-07-10 thomas goto done;
9311 97a02382 2023-07-10 thomas printf("Created commit %s\n", id_str);
9313 97a02382 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
9314 97a02382 2023-07-10 thomas error = got_ref_delete(re->ref, repo);
9315 97a02382 2023-07-10 thomas if (error)
9316 97a02382 2023-07-10 thomas goto done;
9320 97a02382 2023-07-10 thomas if (preserve_logmsg) {
9321 97a02382 2023-07-10 thomas fprintf(stderr, "%s: log message preserved in %s\n",
9322 97a02382 2023-07-10 thomas getprogname(), cl_arg.logmsg_path);
9323 97a02382 2023-07-10 thomas } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
9324 97a02382 2023-07-10 thomas error == NULL)
9325 97a02382 2023-07-10 thomas error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
9326 97a02382 2023-07-10 thomas free(cl_arg.logmsg_path);
9327 97a02382 2023-07-10 thomas if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
9328 97a02382 2023-07-10 thomas error = got_error_from_errno2("unlink", merged_logmsg);
9329 97a02382 2023-07-10 thomas free(merged_logmsg);
9330 97a02382 2023-07-10 thomas if (repo) {
9331 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
9332 97a02382 2023-07-10 thomas if (error == NULL)
9333 97a02382 2023-07-10 thomas error = close_err;
9335 97a02382 2023-07-10 thomas if (worktree)
9336 97a02382 2023-07-10 thomas got_worktree_close(worktree);
9337 97a02382 2023-07-10 thomas if (pack_fds) {
9338 97a02382 2023-07-10 thomas const struct got_error *pack_err =
9339 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
9340 97a02382 2023-07-10 thomas if (error == NULL)
9341 97a02382 2023-07-10 thomas error = pack_err;
9343 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
9344 97a02382 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9345 97a02382 2023-07-10 thomas free(cwd);
9346 97a02382 2023-07-10 thomas free(id_str);
9347 97a02382 2023-07-10 thomas free(gitconfig_path);
9348 97a02382 2023-07-10 thomas free(editor);
9349 97a02382 2023-07-10 thomas free(committer);
9350 97a02382 2023-07-10 thomas free(prepared_logmsg);
9351 97a02382 2023-07-10 thomas return error;
9354 97a02382 2023-07-10 thomas __dead static void
9355 97a02382 2023-07-10 thomas usage_send(void)
9357 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
9358 97a02382 2023-07-10 thomas "[-r repository-path] [-t tag] [remote-repository]\n",
9359 97a02382 2023-07-10 thomas getprogname());
9360 97a02382 2023-07-10 thomas exit(1);
9363 97a02382 2023-07-10 thomas static void
9364 97a02382 2023-07-10 thomas print_load_info(int print_colored, int print_found, int print_trees,
9365 97a02382 2023-07-10 thomas int ncolored, int nfound, int ntrees)
9367 97a02382 2023-07-10 thomas if (print_colored) {
9368 97a02382 2023-07-10 thomas printf("%d commit%s colored", ncolored,
9369 97a02382 2023-07-10 thomas ncolored == 1 ? "" : "s");
9371 97a02382 2023-07-10 thomas if (print_found) {
9372 97a02382 2023-07-10 thomas printf("%s%d object%s found",
9373 97a02382 2023-07-10 thomas ncolored > 0 ? "; " : "",
9374 97a02382 2023-07-10 thomas nfound, nfound == 1 ? "" : "s");
9376 97a02382 2023-07-10 thomas if (print_trees) {
9377 97a02382 2023-07-10 thomas printf("; %d tree%s scanned", ntrees,
9378 97a02382 2023-07-10 thomas ntrees == 1 ? "" : "s");
9382 97a02382 2023-07-10 thomas struct got_send_progress_arg {
9383 97a02382 2023-07-10 thomas char last_scaled_packsize[FMT_SCALED_STRSIZE];
9384 97a02382 2023-07-10 thomas int verbosity;
9385 97a02382 2023-07-10 thomas int last_ncolored;
9386 97a02382 2023-07-10 thomas int last_nfound;
9387 97a02382 2023-07-10 thomas int last_ntrees;
9388 97a02382 2023-07-10 thomas int loading_done;
9389 97a02382 2023-07-10 thomas int last_ncommits;
9390 97a02382 2023-07-10 thomas int last_nobj_total;
9391 97a02382 2023-07-10 thomas int last_p_deltify;
9392 97a02382 2023-07-10 thomas int last_p_written;
9393 97a02382 2023-07-10 thomas int last_p_sent;
9394 97a02382 2023-07-10 thomas int printed_something;
9395 97a02382 2023-07-10 thomas int sent_something;
9396 97a02382 2023-07-10 thomas struct got_pathlist_head *delete_branches;
9399 97a02382 2023-07-10 thomas static const struct got_error *
9400 97a02382 2023-07-10 thomas send_progress(void *arg, int ncolored, int nfound, int ntrees,
9401 97a02382 2023-07-10 thomas off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
9402 97a02382 2023-07-10 thomas int nobj_written, off_t bytes_sent, const char *refname,
9403 97a02382 2023-07-10 thomas const char *errmsg, int success)
9405 97a02382 2023-07-10 thomas struct got_send_progress_arg *a = arg;
9406 97a02382 2023-07-10 thomas char scaled_packsize[FMT_SCALED_STRSIZE];
9407 97a02382 2023-07-10 thomas char scaled_sent[FMT_SCALED_STRSIZE];
9408 97a02382 2023-07-10 thomas int p_deltify = 0, p_written = 0, p_sent = 0;
9409 97a02382 2023-07-10 thomas int print_colored = 0, print_found = 0, print_trees = 0;
9410 97a02382 2023-07-10 thomas int print_searching = 0, print_total = 0;
9411 97a02382 2023-07-10 thomas int print_deltify = 0, print_written = 0, print_sent = 0;
9413 97a02382 2023-07-10 thomas if (a->verbosity < 0)
9414 97a02382 2023-07-10 thomas return NULL;
9416 97a02382 2023-07-10 thomas if (refname) {
9417 97a02382 2023-07-10 thomas const char *status = success ? "accepted" : "rejected";
9419 97a02382 2023-07-10 thomas if (success) {
9420 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
9421 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, a->delete_branches, entry) {
9422 97a02382 2023-07-10 thomas const char *branchname = pe->path;
9423 97a02382 2023-07-10 thomas if (got_path_cmp(branchname, refname,
9424 97a02382 2023-07-10 thomas strlen(branchname), strlen(refname)) == 0) {
9425 97a02382 2023-07-10 thomas status = "deleted";
9426 97a02382 2023-07-10 thomas a->sent_something = 1;
9432 97a02382 2023-07-10 thomas if (a->printed_something)
9433 97a02382 2023-07-10 thomas putchar('\n');
9434 97a02382 2023-07-10 thomas printf("Server has %s %s", status, refname);
9435 97a02382 2023-07-10 thomas if (errmsg)
9436 97a02382 2023-07-10 thomas printf(": %s", errmsg);
9437 97a02382 2023-07-10 thomas a->printed_something = 1;
9438 97a02382 2023-07-10 thomas return NULL;
9441 97a02382 2023-07-10 thomas if (a->last_ncolored != ncolored) {
9442 97a02382 2023-07-10 thomas print_colored = 1;
9443 97a02382 2023-07-10 thomas a->last_ncolored = ncolored;
9446 97a02382 2023-07-10 thomas if (a->last_nfound != nfound) {
9447 97a02382 2023-07-10 thomas print_colored = 1;
9448 97a02382 2023-07-10 thomas print_found = 1;
9449 97a02382 2023-07-10 thomas a->last_nfound = nfound;
9452 97a02382 2023-07-10 thomas if (a->last_ntrees != ntrees) {
9453 97a02382 2023-07-10 thomas print_colored = 1;
9454 97a02382 2023-07-10 thomas print_found = 1;
9455 97a02382 2023-07-10 thomas print_trees = 1;
9456 97a02382 2023-07-10 thomas a->last_ntrees = ntrees;
9459 97a02382 2023-07-10 thomas if ((print_colored || print_found || print_trees) &&
9460 97a02382 2023-07-10 thomas !a->loading_done) {
9461 97a02382 2023-07-10 thomas printf("\r");
9462 97a02382 2023-07-10 thomas print_load_info(print_colored, print_found, print_trees,
9463 97a02382 2023-07-10 thomas ncolored, nfound, ntrees);
9464 97a02382 2023-07-10 thomas a->printed_something = 1;
9465 97a02382 2023-07-10 thomas fflush(stdout);
9466 97a02382 2023-07-10 thomas return NULL;
9467 97a02382 2023-07-10 thomas } else if (!a->loading_done) {
9468 97a02382 2023-07-10 thomas printf("\r");
9469 97a02382 2023-07-10 thomas print_load_info(1, 1, 1, ncolored, nfound, ntrees);
9470 97a02382 2023-07-10 thomas printf("\n");
9471 97a02382 2023-07-10 thomas a->loading_done = 1;
9474 97a02382 2023-07-10 thomas if (fmt_scaled(packfile_size, scaled_packsize) == -1)
9475 97a02382 2023-07-10 thomas return got_error_from_errno("fmt_scaled");
9476 97a02382 2023-07-10 thomas if (fmt_scaled(bytes_sent, scaled_sent) == -1)
9477 97a02382 2023-07-10 thomas return got_error_from_errno("fmt_scaled");
9479 97a02382 2023-07-10 thomas if (a->last_ncommits != ncommits) {
9480 97a02382 2023-07-10 thomas print_searching = 1;
9481 97a02382 2023-07-10 thomas a->last_ncommits = ncommits;
9484 97a02382 2023-07-10 thomas if (a->last_nobj_total != nobj_total) {
9485 97a02382 2023-07-10 thomas print_searching = 1;
9486 97a02382 2023-07-10 thomas print_total = 1;
9487 97a02382 2023-07-10 thomas a->last_nobj_total = nobj_total;
9490 97a02382 2023-07-10 thomas if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
9491 97a02382 2023-07-10 thomas strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
9492 97a02382 2023-07-10 thomas if (strlcpy(a->last_scaled_packsize, scaled_packsize,
9493 97a02382 2023-07-10 thomas FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
9494 97a02382 2023-07-10 thomas return got_error(GOT_ERR_NO_SPACE);
9497 97a02382 2023-07-10 thomas if (nobj_deltify > 0 || nobj_written > 0) {
9498 97a02382 2023-07-10 thomas if (nobj_deltify > 0) {
9499 97a02382 2023-07-10 thomas p_deltify = (nobj_deltify * 100) / nobj_total;
9500 97a02382 2023-07-10 thomas if (p_deltify != a->last_p_deltify) {
9501 97a02382 2023-07-10 thomas a->last_p_deltify = p_deltify;
9502 97a02382 2023-07-10 thomas print_searching = 1;
9503 97a02382 2023-07-10 thomas print_total = 1;
9504 97a02382 2023-07-10 thomas print_deltify = 1;
9507 97a02382 2023-07-10 thomas if (nobj_written > 0) {
9508 97a02382 2023-07-10 thomas p_written = (nobj_written * 100) / nobj_total;
9509 97a02382 2023-07-10 thomas if (p_written != a->last_p_written) {
9510 97a02382 2023-07-10 thomas a->last_p_written = p_written;
9511 97a02382 2023-07-10 thomas print_searching = 1;
9512 97a02382 2023-07-10 thomas print_total = 1;
9513 97a02382 2023-07-10 thomas print_deltify = 1;
9514 97a02382 2023-07-10 thomas print_written = 1;
9519 97a02382 2023-07-10 thomas if (bytes_sent > 0) {
9520 97a02382 2023-07-10 thomas p_sent = (bytes_sent * 100) / packfile_size;
9521 97a02382 2023-07-10 thomas if (p_sent != a->last_p_sent) {
9522 97a02382 2023-07-10 thomas a->last_p_sent = p_sent;
9523 97a02382 2023-07-10 thomas print_searching = 1;
9524 97a02382 2023-07-10 thomas print_total = 1;
9525 97a02382 2023-07-10 thomas print_deltify = 1;
9526 97a02382 2023-07-10 thomas print_written = 1;
9527 97a02382 2023-07-10 thomas print_sent = 1;
9529 97a02382 2023-07-10 thomas a->sent_something = 1;
9532 97a02382 2023-07-10 thomas if (print_searching || print_total || print_deltify || print_written ||
9533 97a02382 2023-07-10 thomas print_sent)
9534 97a02382 2023-07-10 thomas printf("\r");
9535 97a02382 2023-07-10 thomas if (print_searching)
9536 97a02382 2023-07-10 thomas printf("packing %d reference%s", ncommits,
9537 97a02382 2023-07-10 thomas ncommits == 1 ? "" : "s");
9538 97a02382 2023-07-10 thomas if (print_total)
9539 97a02382 2023-07-10 thomas printf("; %d object%s", nobj_total,
9540 97a02382 2023-07-10 thomas nobj_total == 1 ? "" : "s");
9541 97a02382 2023-07-10 thomas if (print_deltify)
9542 97a02382 2023-07-10 thomas printf("; deltify: %d%%", p_deltify);
9543 97a02382 2023-07-10 thomas if (print_sent)
9544 97a02382 2023-07-10 thomas printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9545 97a02382 2023-07-10 thomas scaled_packsize, p_sent);
9546 97a02382 2023-07-10 thomas else if (print_written)
9547 97a02382 2023-07-10 thomas printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9548 97a02382 2023-07-10 thomas scaled_packsize, p_written);
9549 97a02382 2023-07-10 thomas if (print_searching || print_total || print_deltify ||
9550 97a02382 2023-07-10 thomas print_written || print_sent) {
9551 97a02382 2023-07-10 thomas a->printed_something = 1;
9552 97a02382 2023-07-10 thomas fflush(stdout);
9554 97a02382 2023-07-10 thomas return NULL;
9557 97a02382 2023-07-10 thomas static const struct got_error *
9558 97a02382 2023-07-10 thomas cmd_send(int argc, char *argv[])
9560 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
9561 97a02382 2023-07-10 thomas char *cwd = NULL, *repo_path = NULL;
9562 97a02382 2023-07-10 thomas const char *remote_name;
9563 97a02382 2023-07-10 thomas char *proto = NULL, *host = NULL, *port = NULL;
9564 97a02382 2023-07-10 thomas char *repo_name = NULL, *server_path = NULL;
9565 97a02382 2023-07-10 thomas const struct got_remote_repo *remotes, *remote = NULL;
9566 97a02382 2023-07-10 thomas int nremotes, nbranches = 0, ndelete_branches = 0;
9567 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
9568 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
9569 97a02382 2023-07-10 thomas const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
9570 97a02382 2023-07-10 thomas struct got_pathlist_head branches;
9571 97a02382 2023-07-10 thomas struct got_pathlist_head tags;
9572 97a02382 2023-07-10 thomas struct got_reflist_head all_branches;
9573 97a02382 2023-07-10 thomas struct got_reflist_head all_tags;
9574 97a02382 2023-07-10 thomas struct got_pathlist_head delete_args;
9575 97a02382 2023-07-10 thomas struct got_pathlist_head delete_branches;
9576 97a02382 2023-07-10 thomas struct got_reflist_entry *re;
9577 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
9578 97a02382 2023-07-10 thomas int i, ch, sendfd = -1, sendstatus;
9579 97a02382 2023-07-10 thomas pid_t sendpid = -1;
9580 97a02382 2023-07-10 thomas struct got_send_progress_arg spa;
9581 97a02382 2023-07-10 thomas int verbosity = 0, overwrite_refs = 0;
9582 97a02382 2023-07-10 thomas int send_all_branches = 0, send_all_tags = 0;
9583 97a02382 2023-07-10 thomas struct got_reference *ref = NULL;
9584 97a02382 2023-07-10 thomas int *pack_fds = NULL;
9586 97a02382 2023-07-10 thomas TAILQ_INIT(&branches);
9587 97a02382 2023-07-10 thomas TAILQ_INIT(&tags);
9588 97a02382 2023-07-10 thomas TAILQ_INIT(&all_branches);
9589 97a02382 2023-07-10 thomas TAILQ_INIT(&all_tags);
9590 97a02382 2023-07-10 thomas TAILQ_INIT(&delete_args);
9591 97a02382 2023-07-10 thomas TAILQ_INIT(&delete_branches);
9593 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
9594 97a02382 2023-07-10 thomas switch (ch) {
9595 97a02382 2023-07-10 thomas case 'a':
9596 97a02382 2023-07-10 thomas send_all_branches = 1;
9598 97a02382 2023-07-10 thomas case 'b':
9599 97a02382 2023-07-10 thomas error = got_pathlist_append(&branches, optarg, NULL);
9600 97a02382 2023-07-10 thomas if (error)
9601 97a02382 2023-07-10 thomas return error;
9602 97a02382 2023-07-10 thomas nbranches++;
9604 97a02382 2023-07-10 thomas case 'd':
9605 97a02382 2023-07-10 thomas error = got_pathlist_append(&delete_args, optarg, NULL);
9606 97a02382 2023-07-10 thomas if (error)
9607 97a02382 2023-07-10 thomas return error;
9609 97a02382 2023-07-10 thomas case 'f':
9610 97a02382 2023-07-10 thomas overwrite_refs = 1;
9612 97a02382 2023-07-10 thomas case 'q':
9613 97a02382 2023-07-10 thomas verbosity = -1;
9615 97a02382 2023-07-10 thomas case 'r':
9616 97a02382 2023-07-10 thomas repo_path = realpath(optarg, NULL);
9617 97a02382 2023-07-10 thomas if (repo_path == NULL)
9618 97a02382 2023-07-10 thomas return got_error_from_errno2("realpath",
9619 97a02382 2023-07-10 thomas optarg);
9620 97a02382 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
9622 97a02382 2023-07-10 thomas case 'T':
9623 97a02382 2023-07-10 thomas send_all_tags = 1;
9625 97a02382 2023-07-10 thomas case 't':
9626 97a02382 2023-07-10 thomas error = got_pathlist_append(&tags, optarg, NULL);
9627 97a02382 2023-07-10 thomas if (error)
9628 97a02382 2023-07-10 thomas return error;
9630 97a02382 2023-07-10 thomas case 'v':
9631 97a02382 2023-07-10 thomas if (verbosity < 0)
9632 97a02382 2023-07-10 thomas verbosity = 0;
9633 97a02382 2023-07-10 thomas else if (verbosity < 3)
9634 97a02382 2023-07-10 thomas verbosity++;
9636 97a02382 2023-07-10 thomas default:
9637 97a02382 2023-07-10 thomas usage_send();
9638 97a02382 2023-07-10 thomas /* NOTREACHED */
9641 97a02382 2023-07-10 thomas argc -= optind;
9642 97a02382 2023-07-10 thomas argv += optind;
9644 97a02382 2023-07-10 thomas if (send_all_branches && !TAILQ_EMPTY(&branches))
9645 97a02382 2023-07-10 thomas option_conflict('a', 'b');
9646 97a02382 2023-07-10 thomas if (send_all_tags && !TAILQ_EMPTY(&tags))
9647 97a02382 2023-07-10 thomas option_conflict('T', 't');
9650 97a02382 2023-07-10 thomas if (argc == 0)
9651 97a02382 2023-07-10 thomas remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
9652 97a02382 2023-07-10 thomas else if (argc == 1)
9653 97a02382 2023-07-10 thomas remote_name = argv[0];
9655 97a02382 2023-07-10 thomas usage_send();
9657 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
9658 97a02382 2023-07-10 thomas if (cwd == NULL) {
9659 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
9660 97a02382 2023-07-10 thomas goto done;
9663 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
9664 97a02382 2023-07-10 thomas if (error != NULL)
9665 97a02382 2023-07-10 thomas goto done;
9667 97a02382 2023-07-10 thomas if (repo_path == NULL) {
9668 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
9669 97a02382 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
9670 97a02382 2023-07-10 thomas goto done;
9672 97a02382 2023-07-10 thomas error = NULL;
9673 97a02382 2023-07-10 thomas if (worktree) {
9674 97a02382 2023-07-10 thomas repo_path =
9675 97a02382 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree));
9676 97a02382 2023-07-10 thomas if (repo_path == NULL)
9677 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
9678 97a02382 2023-07-10 thomas if (error)
9679 97a02382 2023-07-10 thomas goto done;
9680 97a02382 2023-07-10 thomas } else {
9681 97a02382 2023-07-10 thomas repo_path = strdup(cwd);
9682 97a02382 2023-07-10 thomas if (repo_path == NULL) {
9683 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
9684 97a02382 2023-07-10 thomas goto done;
9689 97a02382 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9690 97a02382 2023-07-10 thomas if (error)
9691 97a02382 2023-07-10 thomas goto done;
9693 97a02382 2023-07-10 thomas if (worktree) {
9694 97a02382 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
9695 97a02382 2023-07-10 thomas if (worktree_conf) {
9696 97a02382 2023-07-10 thomas got_gotconfig_get_remotes(&nremotes, &remotes,
9697 97a02382 2023-07-10 thomas worktree_conf);
9698 97a02382 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
9699 97a02382 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
9700 97a02382 2023-07-10 thomas remote = &remotes[i];
9706 97a02382 2023-07-10 thomas if (remote == NULL) {
9707 97a02382 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
9708 97a02382 2023-07-10 thomas if (repo_conf) {
9709 97a02382 2023-07-10 thomas got_gotconfig_get_remotes(&nremotes, &remotes,
9710 97a02382 2023-07-10 thomas repo_conf);
9711 97a02382 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
9712 97a02382 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
9713 97a02382 2023-07-10 thomas remote = &remotes[i];
9719 97a02382 2023-07-10 thomas if (remote == NULL) {
9720 97a02382 2023-07-10 thomas got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9721 97a02382 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
9722 97a02382 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
9723 97a02382 2023-07-10 thomas remote = &remotes[i];
9728 97a02382 2023-07-10 thomas if (remote == NULL) {
9729 97a02382 2023-07-10 thomas error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9730 97a02382 2023-07-10 thomas goto done;
9733 97a02382 2023-07-10 thomas error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9734 97a02382 2023-07-10 thomas &repo_name, remote->send_url);
9735 97a02382 2023-07-10 thomas if (error)
9736 97a02382 2023-07-10 thomas goto done;
9738 97a02382 2023-07-10 thomas if (strcmp(proto, "git") == 0) {
9739 97a02382 2023-07-10 thomas #ifndef PROFILE
9740 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9741 97a02382 2023-07-10 thomas "sendfd dns inet unveil", NULL) == -1)
9742 97a02382 2023-07-10 thomas err(1, "pledge");
9744 97a02382 2023-07-10 thomas } else if (strcmp(proto, "git+ssh") == 0 ||
9745 97a02382 2023-07-10 thomas strcmp(proto, "ssh") == 0) {
9746 97a02382 2023-07-10 thomas #ifndef PROFILE
9747 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9748 97a02382 2023-07-10 thomas "sendfd unveil", NULL) == -1)
9749 97a02382 2023-07-10 thomas err(1, "pledge");
9751 97a02382 2023-07-10 thomas } else if (strcmp(proto, "http") == 0 ||
9752 97a02382 2023-07-10 thomas strcmp(proto, "git+http") == 0) {
9753 97a02382 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9754 97a02382 2023-07-10 thomas goto done;
9755 97a02382 2023-07-10 thomas } else {
9756 97a02382 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9757 97a02382 2023-07-10 thomas goto done;
9760 97a02382 2023-07-10 thomas error = got_dial_apply_unveil(proto);
9761 97a02382 2023-07-10 thomas if (error)
9762 97a02382 2023-07-10 thomas goto done;
9764 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9765 97a02382 2023-07-10 thomas if (error)
9766 97a02382 2023-07-10 thomas goto done;
9768 97a02382 2023-07-10 thomas if (send_all_branches) {
9769 97a02382 2023-07-10 thomas error = got_ref_list(&all_branches, repo, "refs/heads",
9770 97a02382 2023-07-10 thomas got_ref_cmp_by_name, NULL);
9771 97a02382 2023-07-10 thomas if (error)
9772 97a02382 2023-07-10 thomas goto done;
9773 97a02382 2023-07-10 thomas TAILQ_FOREACH(re, &all_branches, entry) {
9774 97a02382 2023-07-10 thomas const char *branchname = got_ref_get_name(re->ref);
9775 97a02382 2023-07-10 thomas error = got_pathlist_append(&branches,
9776 97a02382 2023-07-10 thomas branchname, NULL);
9777 97a02382 2023-07-10 thomas if (error)
9778 97a02382 2023-07-10 thomas goto done;
9779 97a02382 2023-07-10 thomas nbranches++;
9781 97a02382 2023-07-10 thomas } else if (nbranches == 0) {
9782 97a02382 2023-07-10 thomas for (i = 0; i < remote->nsend_branches; i++) {
9783 97a02382 2023-07-10 thomas error = got_pathlist_append(&branches,
9784 97a02382 2023-07-10 thomas remote->send_branches[i], NULL);
9785 97a02382 2023-07-10 thomas if (error)
9786 97a02382 2023-07-10 thomas goto done;
9790 97a02382 2023-07-10 thomas if (send_all_tags) {
9791 97a02382 2023-07-10 thomas error = got_ref_list(&all_tags, repo, "refs/tags",
9792 97a02382 2023-07-10 thomas got_ref_cmp_by_name, NULL);
9793 97a02382 2023-07-10 thomas if (error)
9794 97a02382 2023-07-10 thomas goto done;
9795 97a02382 2023-07-10 thomas TAILQ_FOREACH(re, &all_tags, entry) {
9796 97a02382 2023-07-10 thomas const char *tagname = got_ref_get_name(re->ref);
9797 97a02382 2023-07-10 thomas error = got_pathlist_append(&tags,
9798 97a02382 2023-07-10 thomas tagname, NULL);
9799 97a02382 2023-07-10 thomas if (error)
9800 97a02382 2023-07-10 thomas goto done;
9805 97a02382 2023-07-10 thomas * To prevent accidents only branches in refs/heads/ can be deleted
9806 97a02382 2023-07-10 thomas * with 'got send -d'.
9807 97a02382 2023-07-10 thomas * Deleting anything else requires local repository access or Git.
9809 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, &delete_args, entry) {
9810 97a02382 2023-07-10 thomas const char *branchname = pe->path;
9811 97a02382 2023-07-10 thomas char *s;
9812 97a02382 2023-07-10 thomas struct got_pathlist_entry *new;
9813 97a02382 2023-07-10 thomas if (strncmp(branchname, "refs/heads/", 11) == 0) {
9814 97a02382 2023-07-10 thomas s = strdup(branchname);
9815 97a02382 2023-07-10 thomas if (s == NULL) {
9816 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
9817 97a02382 2023-07-10 thomas goto done;
9819 97a02382 2023-07-10 thomas } else {
9820 97a02382 2023-07-10 thomas if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9821 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
9822 97a02382 2023-07-10 thomas goto done;
9825 97a02382 2023-07-10 thomas error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9826 97a02382 2023-07-10 thomas if (error || new == NULL /* duplicate */)
9827 97a02382 2023-07-10 thomas free(s);
9828 97a02382 2023-07-10 thomas if (error)
9829 97a02382 2023-07-10 thomas goto done;
9830 97a02382 2023-07-10 thomas ndelete_branches++;
9833 97a02382 2023-07-10 thomas if (nbranches == 0 && ndelete_branches == 0) {
9834 97a02382 2023-07-10 thomas struct got_reference *head_ref;
9835 97a02382 2023-07-10 thomas if (worktree)
9836 97a02382 2023-07-10 thomas error = got_ref_open(&head_ref, repo,
9837 97a02382 2023-07-10 thomas got_worktree_get_head_ref_name(worktree), 0);
9839 97a02382 2023-07-10 thomas error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9840 97a02382 2023-07-10 thomas if (error)
9841 97a02382 2023-07-10 thomas goto done;
9842 97a02382 2023-07-10 thomas if (got_ref_is_symbolic(head_ref)) {
9843 97a02382 2023-07-10 thomas error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9844 97a02382 2023-07-10 thomas got_ref_close(head_ref);
9845 97a02382 2023-07-10 thomas if (error)
9846 97a02382 2023-07-10 thomas goto done;
9848 97a02382 2023-07-10 thomas ref = head_ref;
9849 97a02382 2023-07-10 thomas error = got_pathlist_append(&branches, got_ref_get_name(ref),
9851 97a02382 2023-07-10 thomas if (error)
9852 97a02382 2023-07-10 thomas goto done;
9853 97a02382 2023-07-10 thomas nbranches++;
9856 97a02382 2023-07-10 thomas if (verbosity >= 0) {
9857 97a02382 2023-07-10 thomas printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
9858 97a02382 2023-07-10 thomas remote->name, proto, host,
9859 97a02382 2023-07-10 thomas port ? ":" : "", port ? port : "",
9860 97a02382 2023-07-10 thomas *server_path == '/' ? "" : "/", server_path);
9863 97a02382 2023-07-10 thomas error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9864 97a02382 2023-07-10 thomas server_path, verbosity);
9865 97a02382 2023-07-10 thomas if (error)
9866 97a02382 2023-07-10 thomas goto done;
9868 97a02382 2023-07-10 thomas memset(&spa, 0, sizeof(spa));
9869 97a02382 2023-07-10 thomas spa.last_scaled_packsize[0] = '\0';
9870 97a02382 2023-07-10 thomas spa.last_p_deltify = -1;
9871 97a02382 2023-07-10 thomas spa.last_p_written = -1;
9872 97a02382 2023-07-10 thomas spa.verbosity = verbosity;
9873 97a02382 2023-07-10 thomas spa.delete_branches = &delete_branches;
9874 97a02382 2023-07-10 thomas error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9875 97a02382 2023-07-10 thomas verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9876 97a02382 2023-07-10 thomas check_cancelled, NULL);
9877 97a02382 2023-07-10 thomas if (spa.printed_something)
9878 97a02382 2023-07-10 thomas putchar('\n');
9879 97a02382 2023-07-10 thomas if (error)
9880 97a02382 2023-07-10 thomas goto done;
9881 97a02382 2023-07-10 thomas if (!spa.sent_something && verbosity >= 0)
9882 97a02382 2023-07-10 thomas printf("Already up-to-date\n");
9884 97a02382 2023-07-10 thomas if (sendpid > 0) {
9885 97a02382 2023-07-10 thomas if (kill(sendpid, SIGTERM) == -1)
9886 97a02382 2023-07-10 thomas error = got_error_from_errno("kill");
9887 97a02382 2023-07-10 thomas if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9888 97a02382 2023-07-10 thomas error = got_error_from_errno("waitpid");
9890 97a02382 2023-07-10 thomas if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9891 97a02382 2023-07-10 thomas error = got_error_from_errno("close");
9892 97a02382 2023-07-10 thomas if (repo) {
9893 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
9894 97a02382 2023-07-10 thomas if (error == NULL)
9895 97a02382 2023-07-10 thomas error = close_err;
9897 97a02382 2023-07-10 thomas if (worktree)
9898 97a02382 2023-07-10 thomas got_worktree_close(worktree);
9899 97a02382 2023-07-10 thomas if (pack_fds) {
9900 97a02382 2023-07-10 thomas const struct got_error *pack_err =
9901 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
9902 97a02382 2023-07-10 thomas if (error == NULL)
9903 97a02382 2023-07-10 thomas error = pack_err;
9905 97a02382 2023-07-10 thomas if (ref)
9906 97a02382 2023-07-10 thomas got_ref_close(ref);
9907 97a02382 2023-07-10 thomas got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
9908 97a02382 2023-07-10 thomas got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
9909 97a02382 2023-07-10 thomas got_ref_list_free(&all_branches);
9910 97a02382 2023-07-10 thomas got_ref_list_free(&all_tags);
9911 97a02382 2023-07-10 thomas got_pathlist_free(&delete_args, GOT_PATHLIST_FREE_NONE);
9912 97a02382 2023-07-10 thomas got_pathlist_free(&delete_branches, GOT_PATHLIST_FREE_PATH);
9913 97a02382 2023-07-10 thomas free(cwd);
9914 97a02382 2023-07-10 thomas free(repo_path);
9915 97a02382 2023-07-10 thomas free(proto);
9916 97a02382 2023-07-10 thomas free(host);
9917 97a02382 2023-07-10 thomas free(port);
9918 97a02382 2023-07-10 thomas free(server_path);
9919 97a02382 2023-07-10 thomas free(repo_name);
9920 97a02382 2023-07-10 thomas return error;
9924 97a02382 2023-07-10 thomas * Print and if delete is set delete all ref_prefix references.
9925 97a02382 2023-07-10 thomas * If wanted_ref is not NULL, only print or delete this reference.
9927 97a02382 2023-07-10 thomas static const struct got_error *
9928 97a02382 2023-07-10 thomas process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
9929 97a02382 2023-07-10 thomas const char *wanted_ref, int delete, struct got_worktree *worktree,
9930 97a02382 2023-07-10 thomas struct got_repository *repo)
9932 97a02382 2023-07-10 thomas const struct got_error *err;
9933 97a02382 2023-07-10 thomas struct got_pathlist_head paths;
9934 97a02382 2023-07-10 thomas struct got_reflist_head refs;
9935 97a02382 2023-07-10 thomas struct got_reflist_entry *re;
9936 97a02382 2023-07-10 thomas struct got_reflist_object_id_map *refs_idmap = NULL;
9937 97a02382 2023-07-10 thomas struct got_commit_object *commit = NULL;
9938 97a02382 2023-07-10 thomas struct got_object_id *id = NULL;
9939 97a02382 2023-07-10 thomas const char *header_prefix;
9940 97a02382 2023-07-10 thomas char *uuidstr = NULL;
9941 97a02382 2023-07-10 thomas int found = 0;
9943 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
9944 97a02382 2023-07-10 thomas TAILQ_INIT(&paths);
9946 97a02382 2023-07-10 thomas err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
9947 97a02382 2023-07-10 thomas if (err)
9948 97a02382 2023-07-10 thomas goto done;
9950 97a02382 2023-07-10 thomas err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9951 97a02382 2023-07-10 thomas if (err)
9952 97a02382 2023-07-10 thomas goto done;
9954 97a02382 2023-07-10 thomas if (worktree != NULL) {
9955 97a02382 2023-07-10 thomas err = got_worktree_get_uuid(&uuidstr, worktree);
9956 97a02382 2023-07-10 thomas if (err)
9957 97a02382 2023-07-10 thomas goto done;
9960 97a02382 2023-07-10 thomas if (wanted_ref) {
9961 97a02382 2023-07-10 thomas if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
9962 97a02382 2023-07-10 thomas wanted_ref += 11;
9965 97a02382 2023-07-10 thomas if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
9966 97a02382 2023-07-10 thomas header_prefix = "backout";
9968 97a02382 2023-07-10 thomas header_prefix = "cherrypick";
9970 97a02382 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
9971 97a02382 2023-07-10 thomas const char *refname, *wt;
9973 97a02382 2023-07-10 thomas refname = got_ref_get_name(re->ref);
9975 97a02382 2023-07-10 thomas err = check_cancelled(NULL);
9976 97a02382 2023-07-10 thomas if (err)
9977 97a02382 2023-07-10 thomas goto done;
9979 97a02382 2023-07-10 thomas if (strncmp(refname, ref_prefix, prefix_len) == 0)
9980 97a02382 2023-07-10 thomas refname += prefix_len + 1; /* skip '-' delimiter */
9982 97a02382 2023-07-10 thomas continue;
9984 97a02382 2023-07-10 thomas wt = refname;
9986 97a02382 2023-07-10 thomas if (worktree == NULL || strncmp(refname, uuidstr,
9987 97a02382 2023-07-10 thomas GOT_WORKTREE_UUID_STRLEN) == 0)
9988 97a02382 2023-07-10 thomas refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9990 97a02382 2023-07-10 thomas continue;
9992 97a02382 2023-07-10 thomas err = got_repo_match_object_id(&id, NULL, refname,
9993 97a02382 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
9994 97a02382 2023-07-10 thomas if (err)
9995 97a02382 2023-07-10 thomas goto done;
9997 97a02382 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, id);
9998 97a02382 2023-07-10 thomas if (err)
9999 97a02382 2023-07-10 thomas goto done;
10001 97a02382 2023-07-10 thomas if (wanted_ref)
10002 97a02382 2023-07-10 thomas found = strncmp(wanted_ref, refname,
10003 97a02382 2023-07-10 thomas strlen(wanted_ref)) == 0;
10004 97a02382 2023-07-10 thomas if (wanted_ref && !found) {
10005 97a02382 2023-07-10 thomas struct got_reflist_head *ci_refs;
10007 97a02382 2023-07-10 thomas ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
10010 97a02382 2023-07-10 thomas if (ci_refs) {
10011 97a02382 2023-07-10 thomas char *refs_str = NULL;
10012 97a02382 2023-07-10 thomas char const *r = NULL;
10014 97a02382 2023-07-10 thomas err = build_refs_str(&refs_str, ci_refs, id,
10015 97a02382 2023-07-10 thomas repo, 1);
10016 97a02382 2023-07-10 thomas if (err)
10017 97a02382 2023-07-10 thomas goto done;
10019 97a02382 2023-07-10 thomas r = refs_str;
10020 97a02382 2023-07-10 thomas while (r) {
10021 97a02382 2023-07-10 thomas if (strncmp(r, wanted_ref,
10022 97a02382 2023-07-10 thomas strlen(wanted_ref)) == 0) {
10023 97a02382 2023-07-10 thomas found = 1;
10026 97a02382 2023-07-10 thomas r = strchr(r, ' ');
10030 97a02382 2023-07-10 thomas free(refs_str);
10034 97a02382 2023-07-10 thomas if (wanted_ref == NULL || found) {
10035 97a02382 2023-07-10 thomas if (delete) {
10036 97a02382 2023-07-10 thomas err = got_ref_delete(re->ref, repo);
10037 97a02382 2023-07-10 thomas if (err)
10038 97a02382 2023-07-10 thomas goto done;
10039 97a02382 2023-07-10 thomas printf("Deleted: ");
10040 97a02382 2023-07-10 thomas err = print_commit_oneline(commit, id, repo,
10041 97a02382 2023-07-10 thomas refs_idmap);
10042 97a02382 2023-07-10 thomas } else {
10044 97a02382 2023-07-10 thomas * Print paths modified by commit to help
10045 97a02382 2023-07-10 thomas * associate commits with worktree changes.
10047 97a02382 2023-07-10 thomas err = get_changed_paths(&paths, commit,
10048 97a02382 2023-07-10 thomas repo, NULL);
10049 97a02382 2023-07-10 thomas if (err)
10050 97a02382 2023-07-10 thomas goto done;
10052 97a02382 2023-07-10 thomas err = print_commit(commit, id, repo, NULL,
10053 97a02382 2023-07-10 thomas &paths, NULL, 0, 0, refs_idmap, NULL,
10054 97a02382 2023-07-10 thomas header_prefix);
10055 97a02382 2023-07-10 thomas got_pathlist_free(&paths,
10056 97a02382 2023-07-10 thomas GOT_PATHLIST_FREE_ALL);
10058 97a02382 2023-07-10 thomas if (worktree == NULL)
10059 97a02382 2023-07-10 thomas printf("work tree: %.*s\n\n",
10060 97a02382 2023-07-10 thomas GOT_WORKTREE_UUID_STRLEN, wt);
10062 97a02382 2023-07-10 thomas if (err || found)
10063 97a02382 2023-07-10 thomas goto done;
10066 97a02382 2023-07-10 thomas got_object_commit_close(commit);
10067 97a02382 2023-07-10 thomas commit = NULL;
10068 97a02382 2023-07-10 thomas free(id);
10069 97a02382 2023-07-10 thomas id = NULL;
10072 97a02382 2023-07-10 thomas if (wanted_ref != NULL && !found)
10073 97a02382 2023-07-10 thomas err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
10076 97a02382 2023-07-10 thomas free(id);
10077 97a02382 2023-07-10 thomas free(uuidstr);
10078 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
10079 97a02382 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
10080 97a02382 2023-07-10 thomas if (refs_idmap)
10081 97a02382 2023-07-10 thomas got_reflist_object_id_map_free(refs_idmap);
10082 97a02382 2023-07-10 thomas if (commit)
10083 97a02382 2023-07-10 thomas got_object_commit_close(commit);
10084 97a02382 2023-07-10 thomas return err;
10088 97a02382 2023-07-10 thomas * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
10089 97a02382 2023-07-10 thomas * identified by id for log messages to prepopulate the editor on commit.
10091 97a02382 2023-07-10 thomas static const struct got_error *
10092 97a02382 2023-07-10 thomas logmsg_ref(struct got_object_id *id, const char *prefix,
10093 97a02382 2023-07-10 thomas struct got_worktree *worktree, struct got_repository *repo)
10095 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
10096 97a02382 2023-07-10 thomas char *idstr, *ref = NULL, *refname = NULL;
10097 97a02382 2023-07-10 thomas int histedit_in_progress;
10098 97a02382 2023-07-10 thomas int rebase_in_progress, merge_in_progress;
10101 97a02382 2023-07-10 thomas * Silenty refuse to create merge reference if any histedit, merge,
10102 97a02382 2023-07-10 thomas * or rebase operation is in progress.
10104 97a02382 2023-07-10 thomas err = got_worktree_histedit_in_progress(&histedit_in_progress,
10105 97a02382 2023-07-10 thomas worktree);
10106 97a02382 2023-07-10 thomas if (err)
10107 97a02382 2023-07-10 thomas return err;
10108 97a02382 2023-07-10 thomas if (histedit_in_progress)
10109 97a02382 2023-07-10 thomas return NULL;
10111 97a02382 2023-07-10 thomas err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10112 97a02382 2023-07-10 thomas if (err)
10113 97a02382 2023-07-10 thomas return err;
10114 97a02382 2023-07-10 thomas if (rebase_in_progress)
10115 97a02382 2023-07-10 thomas return NULL;
10117 97a02382 2023-07-10 thomas err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10119 97a02382 2023-07-10 thomas if (err)
10120 97a02382 2023-07-10 thomas return err;
10121 97a02382 2023-07-10 thomas if (merge_in_progress)
10122 97a02382 2023-07-10 thomas return NULL;
10124 97a02382 2023-07-10 thomas err = got_object_id_str(&idstr, id);
10125 97a02382 2023-07-10 thomas if (err)
10126 97a02382 2023-07-10 thomas return err;
10128 97a02382 2023-07-10 thomas err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
10129 97a02382 2023-07-10 thomas if (err)
10130 97a02382 2023-07-10 thomas goto done;
10132 97a02382 2023-07-10 thomas if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
10133 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
10134 97a02382 2023-07-10 thomas goto done;
10137 97a02382 2023-07-10 thomas err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
10138 97a02382 2023-07-10 thomas -1, repo);
10140 97a02382 2023-07-10 thomas free(ref);
10141 97a02382 2023-07-10 thomas free(idstr);
10142 97a02382 2023-07-10 thomas free(refname);
10143 97a02382 2023-07-10 thomas return err;
10146 97a02382 2023-07-10 thomas __dead static void
10147 97a02382 2023-07-10 thomas usage_cherrypick(void)
10149 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
10150 97a02382 2023-07-10 thomas getprogname());
10151 97a02382 2023-07-10 thomas exit(1);
10154 97a02382 2023-07-10 thomas static const struct got_error *
10155 97a02382 2023-07-10 thomas cmd_cherrypick(int argc, char *argv[])
10157 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
10158 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
10159 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
10160 97a02382 2023-07-10 thomas char *cwd = NULL, *commit_id_str = NULL;
10161 97a02382 2023-07-10 thomas struct got_object_id *commit_id = NULL;
10162 97a02382 2023-07-10 thomas struct got_commit_object *commit = NULL;
10163 97a02382 2023-07-10 thomas struct got_object_qid *pid;
10164 97a02382 2023-07-10 thomas int ch, list_refs = 0, remove_refs = 0;
10165 97a02382 2023-07-10 thomas struct got_update_progress_arg upa;
10166 97a02382 2023-07-10 thomas int *pack_fds = NULL;
10168 97a02382 2023-07-10 thomas #ifndef PROFILE
10169 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10170 97a02382 2023-07-10 thomas "unveil", NULL) == -1)
10171 97a02382 2023-07-10 thomas err(1, "pledge");
10174 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "lX")) != -1) {
10175 97a02382 2023-07-10 thomas switch (ch) {
10176 97a02382 2023-07-10 thomas case 'l':
10177 97a02382 2023-07-10 thomas list_refs = 1;
10179 97a02382 2023-07-10 thomas case 'X':
10180 97a02382 2023-07-10 thomas remove_refs = 1;
10182 97a02382 2023-07-10 thomas default:
10183 97a02382 2023-07-10 thomas usage_cherrypick();
10184 97a02382 2023-07-10 thomas /* NOTREACHED */
10188 97a02382 2023-07-10 thomas argc -= optind;
10189 97a02382 2023-07-10 thomas argv += optind;
10191 97a02382 2023-07-10 thomas if (list_refs || remove_refs) {
10192 97a02382 2023-07-10 thomas if (argc != 0 && argc != 1)
10193 97a02382 2023-07-10 thomas usage_cherrypick();
10194 97a02382 2023-07-10 thomas } else if (argc != 1)
10195 97a02382 2023-07-10 thomas usage_cherrypick();
10196 97a02382 2023-07-10 thomas if (list_refs && remove_refs)
10197 97a02382 2023-07-10 thomas option_conflict('l', 'X');
10199 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
10200 97a02382 2023-07-10 thomas if (cwd == NULL) {
10201 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
10202 97a02382 2023-07-10 thomas goto done;
10205 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
10206 97a02382 2023-07-10 thomas if (error != NULL)
10207 97a02382 2023-07-10 thomas goto done;
10209 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
10210 97a02382 2023-07-10 thomas if (error) {
10211 97a02382 2023-07-10 thomas if (list_refs || remove_refs) {
10212 97a02382 2023-07-10 thomas if (error->code != GOT_ERR_NOT_WORKTREE)
10213 97a02382 2023-07-10 thomas goto done;
10214 97a02382 2023-07-10 thomas } else {
10215 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
10216 97a02382 2023-07-10 thomas error = wrap_not_worktree_error(error,
10217 97a02382 2023-07-10 thomas "cherrypick", cwd);
10218 97a02382 2023-07-10 thomas goto done;
10222 97a02382 2023-07-10 thomas error = got_repo_open(&repo,
10223 97a02382 2023-07-10 thomas worktree ? got_worktree_get_repo_path(worktree) : cwd,
10224 97a02382 2023-07-10 thomas NULL, pack_fds);
10225 97a02382 2023-07-10 thomas if (error != NULL)
10226 97a02382 2023-07-10 thomas goto done;
10228 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
10229 97a02382 2023-07-10 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
10230 97a02382 2023-07-10 thomas if (error)
10231 97a02382 2023-07-10 thomas goto done;
10233 97a02382 2023-07-10 thomas if (list_refs || remove_refs) {
10234 97a02382 2023-07-10 thomas error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10235 97a02382 2023-07-10 thomas GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
10236 97a02382 2023-07-10 thomas argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10237 97a02382 2023-07-10 thomas goto done;
10240 97a02382 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL, argv[0],
10241 97a02382 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
10242 97a02382 2023-07-10 thomas if (error)
10243 97a02382 2023-07-10 thomas goto done;
10244 97a02382 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
10245 97a02382 2023-07-10 thomas if (error)
10246 97a02382 2023-07-10 thomas goto done;
10248 97a02382 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
10249 97a02382 2023-07-10 thomas if (error)
10250 97a02382 2023-07-10 thomas goto done;
10251 97a02382 2023-07-10 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10252 97a02382 2023-07-10 thomas memset(&upa, 0, sizeof(upa));
10253 97a02382 2023-07-10 thomas error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
10254 97a02382 2023-07-10 thomas commit_id, repo, update_progress, &upa, check_cancelled,
10256 97a02382 2023-07-10 thomas if (error != NULL)
10257 97a02382 2023-07-10 thomas goto done;
10259 97a02382 2023-07-10 thomas if (upa.did_something) {
10260 97a02382 2023-07-10 thomas error = logmsg_ref(commit_id,
10261 97a02382 2023-07-10 thomas GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
10262 97a02382 2023-07-10 thomas if (error)
10263 97a02382 2023-07-10 thomas goto done;
10264 97a02382 2023-07-10 thomas printf("Merged commit %s\n", commit_id_str);
10266 97a02382 2023-07-10 thomas print_merge_progress_stats(&upa);
10268 97a02382 2023-07-10 thomas free(cwd);
10269 97a02382 2023-07-10 thomas if (commit)
10270 97a02382 2023-07-10 thomas got_object_commit_close(commit);
10271 97a02382 2023-07-10 thomas free(commit_id_str);
10272 97a02382 2023-07-10 thomas if (worktree)
10273 97a02382 2023-07-10 thomas got_worktree_close(worktree);
10274 97a02382 2023-07-10 thomas if (repo) {
10275 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
10276 97a02382 2023-07-10 thomas if (error == NULL)
10277 97a02382 2023-07-10 thomas error = close_err;
10279 97a02382 2023-07-10 thomas if (pack_fds) {
10280 97a02382 2023-07-10 thomas const struct got_error *pack_err =
10281 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
10282 97a02382 2023-07-10 thomas if (error == NULL)
10283 97a02382 2023-07-10 thomas error = pack_err;
10286 97a02382 2023-07-10 thomas return error;
10289 97a02382 2023-07-10 thomas __dead static void
10290 97a02382 2023-07-10 thomas usage_backout(void)
10292 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
10293 97a02382 2023-07-10 thomas exit(1);
10296 97a02382 2023-07-10 thomas static const struct got_error *
10297 97a02382 2023-07-10 thomas cmd_backout(int argc, char *argv[])
10299 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
10300 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
10301 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
10302 97a02382 2023-07-10 thomas char *cwd = NULL, *commit_id_str = NULL;
10303 97a02382 2023-07-10 thomas struct got_object_id *commit_id = NULL;
10304 97a02382 2023-07-10 thomas struct got_commit_object *commit = NULL;
10305 97a02382 2023-07-10 thomas struct got_object_qid *pid;
10306 97a02382 2023-07-10 thomas int ch, list_refs = 0, remove_refs = 0;
10307 97a02382 2023-07-10 thomas struct got_update_progress_arg upa;
10308 97a02382 2023-07-10 thomas int *pack_fds = NULL;
10310 97a02382 2023-07-10 thomas #ifndef PROFILE
10311 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10312 97a02382 2023-07-10 thomas "unveil", NULL) == -1)
10313 97a02382 2023-07-10 thomas err(1, "pledge");
10316 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "lX")) != -1) {
10317 97a02382 2023-07-10 thomas switch (ch) {
10318 97a02382 2023-07-10 thomas case 'l':
10319 97a02382 2023-07-10 thomas list_refs = 1;
10321 97a02382 2023-07-10 thomas case 'X':
10322 97a02382 2023-07-10 thomas remove_refs = 1;
10324 97a02382 2023-07-10 thomas default:
10325 97a02382 2023-07-10 thomas usage_backout();
10326 97a02382 2023-07-10 thomas /* NOTREACHED */
10330 97a02382 2023-07-10 thomas argc -= optind;
10331 97a02382 2023-07-10 thomas argv += optind;
10333 97a02382 2023-07-10 thomas if (list_refs || remove_refs) {
10334 97a02382 2023-07-10 thomas if (argc != 0 && argc != 1)
10335 97a02382 2023-07-10 thomas usage_backout();
10336 97a02382 2023-07-10 thomas } else if (argc != 1)
10337 97a02382 2023-07-10 thomas usage_backout();
10338 97a02382 2023-07-10 thomas if (list_refs && remove_refs)
10339 97a02382 2023-07-10 thomas option_conflict('l', 'X');
10341 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
10342 97a02382 2023-07-10 thomas if (cwd == NULL) {
10343 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
10344 97a02382 2023-07-10 thomas goto done;
10347 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
10348 97a02382 2023-07-10 thomas if (error != NULL)
10349 97a02382 2023-07-10 thomas goto done;
10351 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
10352 97a02382 2023-07-10 thomas if (error) {
10353 97a02382 2023-07-10 thomas if (list_refs || remove_refs) {
10354 97a02382 2023-07-10 thomas if (error->code != GOT_ERR_NOT_WORKTREE)
10355 97a02382 2023-07-10 thomas goto done;
10356 97a02382 2023-07-10 thomas } else {
10357 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
10358 97a02382 2023-07-10 thomas error = wrap_not_worktree_error(error,
10359 97a02382 2023-07-10 thomas "backout", cwd);
10360 97a02382 2023-07-10 thomas goto done;
10364 97a02382 2023-07-10 thomas error = got_repo_open(&repo,
10365 97a02382 2023-07-10 thomas worktree ? got_worktree_get_repo_path(worktree) : cwd,
10366 97a02382 2023-07-10 thomas NULL, pack_fds);
10367 97a02382 2023-07-10 thomas if (error != NULL)
10368 97a02382 2023-07-10 thomas goto done;
10370 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
10371 97a02382 2023-07-10 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
10372 97a02382 2023-07-10 thomas if (error)
10373 97a02382 2023-07-10 thomas goto done;
10375 97a02382 2023-07-10 thomas if (list_refs || remove_refs) {
10376 97a02382 2023-07-10 thomas error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
10377 97a02382 2023-07-10 thomas GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
10378 97a02382 2023-07-10 thomas argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10379 97a02382 2023-07-10 thomas goto done;
10382 97a02382 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL, argv[0],
10383 97a02382 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
10384 97a02382 2023-07-10 thomas if (error)
10385 97a02382 2023-07-10 thomas goto done;
10386 97a02382 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
10387 97a02382 2023-07-10 thomas if (error)
10388 97a02382 2023-07-10 thomas goto done;
10390 97a02382 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
10391 97a02382 2023-07-10 thomas if (error)
10392 97a02382 2023-07-10 thomas goto done;
10393 97a02382 2023-07-10 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10394 97a02382 2023-07-10 thomas if (pid == NULL) {
10395 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_ROOT_COMMIT);
10396 97a02382 2023-07-10 thomas goto done;
10399 97a02382 2023-07-10 thomas memset(&upa, 0, sizeof(upa));
10400 97a02382 2023-07-10 thomas error = got_worktree_merge_files(worktree, commit_id, &pid->id,
10401 97a02382 2023-07-10 thomas repo, update_progress, &upa, check_cancelled, NULL);
10402 97a02382 2023-07-10 thomas if (error != NULL)
10403 97a02382 2023-07-10 thomas goto done;
10405 97a02382 2023-07-10 thomas if (upa.did_something) {
10406 97a02382 2023-07-10 thomas error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10407 97a02382 2023-07-10 thomas worktree, repo);
10408 97a02382 2023-07-10 thomas if (error)
10409 97a02382 2023-07-10 thomas goto done;
10410 97a02382 2023-07-10 thomas printf("Backed out commit %s\n", commit_id_str);
10412 97a02382 2023-07-10 thomas print_merge_progress_stats(&upa);
10414 97a02382 2023-07-10 thomas free(cwd);
10415 97a02382 2023-07-10 thomas if (commit)
10416 97a02382 2023-07-10 thomas got_object_commit_close(commit);
10417 97a02382 2023-07-10 thomas free(commit_id_str);
10418 97a02382 2023-07-10 thomas if (worktree)
10419 97a02382 2023-07-10 thomas got_worktree_close(worktree);
10420 97a02382 2023-07-10 thomas if (repo) {
10421 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
10422 97a02382 2023-07-10 thomas if (error == NULL)
10423 97a02382 2023-07-10 thomas error = close_err;
10425 97a02382 2023-07-10 thomas if (pack_fds) {
10426 97a02382 2023-07-10 thomas const struct got_error *pack_err =
10427 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
10428 97a02382 2023-07-10 thomas if (error == NULL)
10429 97a02382 2023-07-10 thomas error = pack_err;
10431 97a02382 2023-07-10 thomas return error;
10434 97a02382 2023-07-10 thomas __dead static void
10435 97a02382 2023-07-10 thomas usage_rebase(void)
10437 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s rebase [-aCclX] [branch]\n", getprogname());
10438 97a02382 2023-07-10 thomas exit(1);
10441 97a02382 2023-07-10 thomas static void
10442 97a02382 2023-07-10 thomas trim_logmsg(char *logmsg, int limit)
10444 97a02382 2023-07-10 thomas char *nl;
10445 97a02382 2023-07-10 thomas size_t len;
10447 97a02382 2023-07-10 thomas len = strlen(logmsg);
10448 97a02382 2023-07-10 thomas if (len > limit)
10449 97a02382 2023-07-10 thomas len = limit;
10450 97a02382 2023-07-10 thomas logmsg[len] = '\0';
10451 97a02382 2023-07-10 thomas nl = strchr(logmsg, '\n');
10452 97a02382 2023-07-10 thomas if (nl)
10453 97a02382 2023-07-10 thomas *nl = '\0';
10456 97a02382 2023-07-10 thomas static const struct got_error *
10457 97a02382 2023-07-10 thomas get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
10459 97a02382 2023-07-10 thomas const struct got_error *err;
10460 97a02382 2023-07-10 thomas char *logmsg0 = NULL;
10461 97a02382 2023-07-10 thomas const char *s;
10463 97a02382 2023-07-10 thomas err = got_object_commit_get_logmsg(&logmsg0, commit);
10464 97a02382 2023-07-10 thomas if (err)
10465 97a02382 2023-07-10 thomas return err;
10467 97a02382 2023-07-10 thomas s = logmsg0;
10468 97a02382 2023-07-10 thomas while (isspace((unsigned char)s[0]))
10471 97a02382 2023-07-10 thomas *logmsg = strdup(s);
10472 97a02382 2023-07-10 thomas if (*logmsg == NULL) {
10473 97a02382 2023-07-10 thomas err = got_error_from_errno("strdup");
10474 97a02382 2023-07-10 thomas goto done;
10477 97a02382 2023-07-10 thomas trim_logmsg(*logmsg, limit);
10479 97a02382 2023-07-10 thomas free(logmsg0);
10480 97a02382 2023-07-10 thomas return err;
10483 97a02382 2023-07-10 thomas static const struct got_error *
10484 97a02382 2023-07-10 thomas show_rebase_merge_conflict(struct got_object_id *id,
10485 97a02382 2023-07-10 thomas struct got_repository *repo)
10487 97a02382 2023-07-10 thomas const struct got_error *err;
10488 97a02382 2023-07-10 thomas struct got_commit_object *commit = NULL;
10489 97a02382 2023-07-10 thomas char *id_str = NULL, *logmsg = NULL;
10491 97a02382 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, id);
10492 97a02382 2023-07-10 thomas if (err)
10493 97a02382 2023-07-10 thomas return err;
10495 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, id);
10496 97a02382 2023-07-10 thomas if (err)
10497 97a02382 2023-07-10 thomas goto done;
10499 97a02382 2023-07-10 thomas id_str[12] = '\0';
10501 97a02382 2023-07-10 thomas err = get_short_logmsg(&logmsg, 42, commit);
10502 97a02382 2023-07-10 thomas if (err)
10503 97a02382 2023-07-10 thomas goto done;
10505 97a02382 2023-07-10 thomas printf("%s -> merge conflict: %s\n", id_str, logmsg);
10507 97a02382 2023-07-10 thomas free(id_str);
10508 97a02382 2023-07-10 thomas got_object_commit_close(commit);
10509 97a02382 2023-07-10 thomas free(logmsg);
10510 97a02382 2023-07-10 thomas return err;
10513 97a02382 2023-07-10 thomas static const struct got_error *
10514 97a02382 2023-07-10 thomas show_rebase_progress(struct got_commit_object *commit,
10515 97a02382 2023-07-10 thomas struct got_object_id *old_id, struct got_object_id *new_id)
10517 97a02382 2023-07-10 thomas const struct got_error *err;
10518 97a02382 2023-07-10 thomas char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10520 97a02382 2023-07-10 thomas err = got_object_id_str(&old_id_str, old_id);
10521 97a02382 2023-07-10 thomas if (err)
10522 97a02382 2023-07-10 thomas goto done;
10524 97a02382 2023-07-10 thomas if (new_id) {
10525 97a02382 2023-07-10 thomas err = got_object_id_str(&new_id_str, new_id);
10526 97a02382 2023-07-10 thomas if (err)
10527 97a02382 2023-07-10 thomas goto done;
10530 97a02382 2023-07-10 thomas old_id_str[12] = '\0';
10531 97a02382 2023-07-10 thomas if (new_id_str)
10532 97a02382 2023-07-10 thomas new_id_str[12] = '\0';
10534 97a02382 2023-07-10 thomas err = get_short_logmsg(&logmsg, 42, commit);
10535 97a02382 2023-07-10 thomas if (err)
10536 97a02382 2023-07-10 thomas goto done;
10538 97a02382 2023-07-10 thomas printf("%s -> %s: %s\n", old_id_str,
10539 97a02382 2023-07-10 thomas new_id_str ? new_id_str : "no-op change", logmsg);
10541 97a02382 2023-07-10 thomas free(old_id_str);
10542 97a02382 2023-07-10 thomas free(new_id_str);
10543 97a02382 2023-07-10 thomas free(logmsg);
10544 97a02382 2023-07-10 thomas return err;
10547 97a02382 2023-07-10 thomas static const struct got_error *
10548 97a02382 2023-07-10 thomas rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
10549 97a02382 2023-07-10 thomas struct got_reference *branch, struct got_reference *tmp_branch,
10550 97a02382 2023-07-10 thomas struct got_repository *repo, int create_backup)
10552 97a02382 2023-07-10 thomas printf("Switching work tree to %s\n", got_ref_get_name(branch));
10553 97a02382 2023-07-10 thomas return got_worktree_rebase_complete(worktree, fileindex,
10554 97a02382 2023-07-10 thomas tmp_branch, branch, repo, create_backup);
10557 97a02382 2023-07-10 thomas static const struct got_error *
10558 97a02382 2023-07-10 thomas rebase_commit(struct got_pathlist_head *merged_paths,
10559 97a02382 2023-07-10 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
10560 97a02382 2023-07-10 thomas struct got_reference *tmp_branch, const char *committer,
10561 97a02382 2023-07-10 thomas struct got_object_id *commit_id, int allow_conflict,
10562 97a02382 2023-07-10 thomas struct got_repository *repo)
10564 97a02382 2023-07-10 thomas const struct got_error *error;
10565 97a02382 2023-07-10 thomas struct got_commit_object *commit;
10566 97a02382 2023-07-10 thomas struct got_object_id *new_commit_id;
10568 97a02382 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
10569 97a02382 2023-07-10 thomas if (error)
10570 97a02382 2023-07-10 thomas return error;
10572 97a02382 2023-07-10 thomas error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
10573 97a02382 2023-07-10 thomas worktree, fileindex, tmp_branch, committer, commit, commit_id,
10574 97a02382 2023-07-10 thomas allow_conflict, repo);
10575 97a02382 2023-07-10 thomas if (error) {
10576 97a02382 2023-07-10 thomas if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
10577 97a02382 2023-07-10 thomas goto done;
10578 97a02382 2023-07-10 thomas error = show_rebase_progress(commit, commit_id, NULL);
10579 97a02382 2023-07-10 thomas } else {
10580 97a02382 2023-07-10 thomas error = show_rebase_progress(commit, commit_id, new_commit_id);
10581 97a02382 2023-07-10 thomas free(new_commit_id);
10584 97a02382 2023-07-10 thomas got_object_commit_close(commit);
10585 97a02382 2023-07-10 thomas return error;
10588 97a02382 2023-07-10 thomas struct check_path_prefix_arg {
10589 97a02382 2023-07-10 thomas const char *path_prefix;
10590 97a02382 2023-07-10 thomas size_t len;
10591 97a02382 2023-07-10 thomas int errcode;
10594 97a02382 2023-07-10 thomas static const struct got_error *
10595 97a02382 2023-07-10 thomas check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
10596 97a02382 2023-07-10 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
10597 97a02382 2023-07-10 thomas struct got_object_id *id1, struct got_object_id *id2,
10598 97a02382 2023-07-10 thomas const char *path1, const char *path2,
10599 97a02382 2023-07-10 thomas mode_t mode1, mode_t mode2, struct got_repository *repo)
10601 97a02382 2023-07-10 thomas struct check_path_prefix_arg *a = arg;
10603 97a02382 2023-07-10 thomas if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
10604 97a02382 2023-07-10 thomas (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
10605 97a02382 2023-07-10 thomas return got_error(a->errcode);
10607 97a02382 2023-07-10 thomas return NULL;
10610 97a02382 2023-07-10 thomas static const struct got_error *
10611 97a02382 2023-07-10 thomas check_path_prefix(struct got_object_id *parent_id,
10612 97a02382 2023-07-10 thomas struct got_object_id *commit_id, const char *path_prefix,
10613 97a02382 2023-07-10 thomas int errcode, struct got_repository *repo)
10615 97a02382 2023-07-10 thomas const struct got_error *err;
10616 97a02382 2023-07-10 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
10617 97a02382 2023-07-10 thomas struct got_commit_object *commit = NULL, *parent_commit = NULL;
10618 97a02382 2023-07-10 thomas struct check_path_prefix_arg cpp_arg;
10620 97a02382 2023-07-10 thomas if (got_path_is_root_dir(path_prefix))
10621 97a02382 2023-07-10 thomas return NULL;
10623 97a02382 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
10624 97a02382 2023-07-10 thomas if (err)
10625 97a02382 2023-07-10 thomas goto done;
10627 97a02382 2023-07-10 thomas err = got_object_open_as_commit(&parent_commit, repo, parent_id);
10628 97a02382 2023-07-10 thomas if (err)
10629 97a02382 2023-07-10 thomas goto done;
10631 97a02382 2023-07-10 thomas err = got_object_open_as_tree(&tree1, repo,
10632 97a02382 2023-07-10 thomas got_object_commit_get_tree_id(parent_commit));
10633 97a02382 2023-07-10 thomas if (err)
10634 97a02382 2023-07-10 thomas goto done;
10636 97a02382 2023-07-10 thomas err = got_object_open_as_tree(&tree2, repo,
10637 97a02382 2023-07-10 thomas got_object_commit_get_tree_id(commit));
10638 97a02382 2023-07-10 thomas if (err)
10639 97a02382 2023-07-10 thomas goto done;
10641 97a02382 2023-07-10 thomas cpp_arg.path_prefix = path_prefix;
10642 97a02382 2023-07-10 thomas while (cpp_arg.path_prefix[0] == '/')
10643 97a02382 2023-07-10 thomas cpp_arg.path_prefix++;
10644 97a02382 2023-07-10 thomas cpp_arg.len = strlen(cpp_arg.path_prefix);
10645 97a02382 2023-07-10 thomas cpp_arg.errcode = errcode;
10646 97a02382 2023-07-10 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
10647 97a02382 2023-07-10 thomas check_path_prefix_in_diff, &cpp_arg, 0);
10649 97a02382 2023-07-10 thomas if (tree1)
10650 97a02382 2023-07-10 thomas got_object_tree_close(tree1);
10651 97a02382 2023-07-10 thomas if (tree2)
10652 97a02382 2023-07-10 thomas got_object_tree_close(tree2);
10653 97a02382 2023-07-10 thomas if (commit)
10654 97a02382 2023-07-10 thomas got_object_commit_close(commit);
10655 97a02382 2023-07-10 thomas if (parent_commit)
10656 97a02382 2023-07-10 thomas got_object_commit_close(parent_commit);
10657 97a02382 2023-07-10 thomas return err;
10660 97a02382 2023-07-10 thomas static const struct got_error *
10661 97a02382 2023-07-10 thomas collect_commits(struct got_object_id_queue *commits,
10662 97a02382 2023-07-10 thomas struct got_object_id *initial_commit_id,
10663 97a02382 2023-07-10 thomas struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
10664 97a02382 2023-07-10 thomas const char *path_prefix, int path_prefix_errcode,
10665 97a02382 2023-07-10 thomas struct got_repository *repo)
10667 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
10668 97a02382 2023-07-10 thomas struct got_commit_graph *graph = NULL;
10669 97a02382 2023-07-10 thomas struct got_object_id parent_id, commit_id;
10670 97a02382 2023-07-10 thomas struct got_object_qid *qid;
10672 97a02382 2023-07-10 thomas err = got_commit_graph_open(&graph, "/", 1);
10673 97a02382 2023-07-10 thomas if (err)
10674 97a02382 2023-07-10 thomas return err;
10676 97a02382 2023-07-10 thomas err = got_commit_graph_iter_start(graph, iter_start_id, repo,
10677 97a02382 2023-07-10 thomas check_cancelled, NULL);
10678 97a02382 2023-07-10 thomas if (err)
10679 97a02382 2023-07-10 thomas goto done;
10681 97a02382 2023-07-10 thomas memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
10682 97a02382 2023-07-10 thomas while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
10683 97a02382 2023-07-10 thomas err = got_commit_graph_iter_next(&parent_id, graph, repo,
10684 97a02382 2023-07-10 thomas check_cancelled, NULL);
10685 97a02382 2023-07-10 thomas if (err) {
10686 97a02382 2023-07-10 thomas if (err->code == GOT_ERR_ITER_COMPLETED) {
10687 97a02382 2023-07-10 thomas err = got_error_msg(GOT_ERR_ANCESTRY,
10688 97a02382 2023-07-10 thomas "ran out of commits to rebase before "
10689 97a02382 2023-07-10 thomas "youngest common ancestor commit has "
10690 97a02382 2023-07-10 thomas "been reached?!?");
10692 97a02382 2023-07-10 thomas goto done;
10693 97a02382 2023-07-10 thomas } else {
10694 97a02382 2023-07-10 thomas err = check_path_prefix(&parent_id, &commit_id,
10695 97a02382 2023-07-10 thomas path_prefix, path_prefix_errcode, repo);
10696 97a02382 2023-07-10 thomas if (err)
10697 97a02382 2023-07-10 thomas goto done;
10699 97a02382 2023-07-10 thomas err = got_object_qid_alloc(&qid, &commit_id);
10700 97a02382 2023-07-10 thomas if (err)
10701 97a02382 2023-07-10 thomas goto done;
10702 97a02382 2023-07-10 thomas STAILQ_INSERT_HEAD(commits, qid, entry);
10704 97a02382 2023-07-10 thomas memcpy(&commit_id, &parent_id, sizeof(commit_id));
10708 97a02382 2023-07-10 thomas got_commit_graph_close(graph);
10709 97a02382 2023-07-10 thomas return err;
10712 97a02382 2023-07-10 thomas static const struct got_error *
10713 97a02382 2023-07-10 thomas get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
10715 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
10716 97a02382 2023-07-10 thomas time_t committer_time;
10717 97a02382 2023-07-10 thomas struct tm tm;
10718 97a02382 2023-07-10 thomas char datebuf[11]; /* YYYY-MM-DD + NUL */
10719 97a02382 2023-07-10 thomas char *author0 = NULL, *author, *smallerthan;
10720 97a02382 2023-07-10 thomas char *logmsg0 = NULL, *logmsg, *newline;
10722 97a02382 2023-07-10 thomas committer_time = got_object_commit_get_committer_time(commit);
10723 97a02382 2023-07-10 thomas if (gmtime_r(&committer_time, &tm) == NULL)
10724 97a02382 2023-07-10 thomas return got_error_from_errno("gmtime_r");
10725 97a02382 2023-07-10 thomas if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
10726 97a02382 2023-07-10 thomas return got_error(GOT_ERR_NO_SPACE);
10728 97a02382 2023-07-10 thomas author0 = strdup(got_object_commit_get_author(commit));
10729 97a02382 2023-07-10 thomas if (author0 == NULL)
10730 97a02382 2023-07-10 thomas return got_error_from_errno("strdup");
10731 97a02382 2023-07-10 thomas author = author0;
10732 97a02382 2023-07-10 thomas smallerthan = strchr(author, '<');
10733 97a02382 2023-07-10 thomas if (smallerthan && smallerthan[1] != '\0')
10734 97a02382 2023-07-10 thomas author = smallerthan + 1;
10735 97a02382 2023-07-10 thomas author[strcspn(author, "@>")] = '\0';
10737 97a02382 2023-07-10 thomas err = got_object_commit_get_logmsg(&logmsg0, commit);
10738 97a02382 2023-07-10 thomas if (err)
10739 97a02382 2023-07-10 thomas goto done;
10740 97a02382 2023-07-10 thomas logmsg = logmsg0;
10741 97a02382 2023-07-10 thomas while (*logmsg == '\n')
10742 97a02382 2023-07-10 thomas logmsg++;
10743 97a02382 2023-07-10 thomas newline = strchr(logmsg, '\n');
10744 97a02382 2023-07-10 thomas if (newline)
10745 97a02382 2023-07-10 thomas *newline = '\0';
10747 97a02382 2023-07-10 thomas if (asprintf(brief_str, "%s %s %s",
10748 97a02382 2023-07-10 thomas datebuf, author, logmsg) == -1)
10749 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
10751 97a02382 2023-07-10 thomas free(author0);
10752 97a02382 2023-07-10 thomas free(logmsg0);
10753 97a02382 2023-07-10 thomas return err;
10756 97a02382 2023-07-10 thomas static const struct got_error *
10757 97a02382 2023-07-10 thomas delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
10758 97a02382 2023-07-10 thomas struct got_repository *repo)
10760 97a02382 2023-07-10 thomas const struct got_error *err;
10761 97a02382 2023-07-10 thomas char *id_str;
10763 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, id);
10764 97a02382 2023-07-10 thomas if (err)
10765 97a02382 2023-07-10 thomas return err;
10767 97a02382 2023-07-10 thomas err = got_ref_delete(ref, repo);
10768 97a02382 2023-07-10 thomas if (err)
10769 97a02382 2023-07-10 thomas goto done;
10771 97a02382 2023-07-10 thomas printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
10773 97a02382 2023-07-10 thomas free(id_str);
10774 97a02382 2023-07-10 thomas return err;
10777 97a02382 2023-07-10 thomas static const struct got_error *
10778 97a02382 2023-07-10 thomas print_backup_ref(const char *branch_name, const char *new_id_str,
10779 97a02382 2023-07-10 thomas struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
10780 97a02382 2023-07-10 thomas struct got_reflist_object_id_map *refs_idmap,
10781 97a02382 2023-07-10 thomas struct got_repository *repo)
10783 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
10784 97a02382 2023-07-10 thomas struct got_reflist_head *refs;
10785 97a02382 2023-07-10 thomas char *refs_str = NULL;
10786 97a02382 2023-07-10 thomas struct got_object_id *new_commit_id = NULL;
10787 97a02382 2023-07-10 thomas struct got_commit_object *new_commit = NULL;
10788 97a02382 2023-07-10 thomas char *new_commit_brief_str = NULL;
10789 97a02382 2023-07-10 thomas struct got_object_id *yca_id = NULL;
10790 97a02382 2023-07-10 thomas struct got_commit_object *yca_commit = NULL;
10791 97a02382 2023-07-10 thomas char *yca_id_str = NULL, *yca_brief_str = NULL;
10792 97a02382 2023-07-10 thomas char *custom_refs_str;
10794 97a02382 2023-07-10 thomas if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
10795 97a02382 2023-07-10 thomas return got_error_from_errno("asprintf");
10797 97a02382 2023-07-10 thomas err = print_commit(old_commit, old_commit_id, repo, NULL, NULL, NULL,
10798 97a02382 2023-07-10 thomas 0, 0, refs_idmap, custom_refs_str, NULL);
10799 97a02382 2023-07-10 thomas if (err)
10800 97a02382 2023-07-10 thomas goto done;
10802 97a02382 2023-07-10 thomas err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
10803 97a02382 2023-07-10 thomas if (err)
10804 97a02382 2023-07-10 thomas goto done;
10806 97a02382 2023-07-10 thomas refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
10807 97a02382 2023-07-10 thomas if (refs) {
10808 97a02382 2023-07-10 thomas err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
10809 97a02382 2023-07-10 thomas if (err)
10810 97a02382 2023-07-10 thomas goto done;
10813 97a02382 2023-07-10 thomas err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
10814 97a02382 2023-07-10 thomas if (err)
10815 97a02382 2023-07-10 thomas goto done;
10817 97a02382 2023-07-10 thomas err = get_commit_brief_str(&new_commit_brief_str, new_commit);
10818 97a02382 2023-07-10 thomas if (err)
10819 97a02382 2023-07-10 thomas goto done;
10821 97a02382 2023-07-10 thomas err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10822 97a02382 2023-07-10 thomas old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
10823 97a02382 2023-07-10 thomas if (err)
10824 97a02382 2023-07-10 thomas goto done;
10826 97a02382 2023-07-10 thomas printf("has become commit %s%s%s%s\n %s\n", new_id_str,
10827 97a02382 2023-07-10 thomas refs_str ? " (" : "", refs_str ? refs_str : "",
10828 97a02382 2023-07-10 thomas refs_str ? ")" : "", new_commit_brief_str);
10829 97a02382 2023-07-10 thomas if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
10830 97a02382 2023-07-10 thomas got_object_id_cmp(yca_id, old_commit_id) != 0) {
10831 97a02382 2023-07-10 thomas free(refs_str);
10832 97a02382 2023-07-10 thomas refs_str = NULL;
10834 97a02382 2023-07-10 thomas err = got_object_open_as_commit(&yca_commit, repo, yca_id);
10835 97a02382 2023-07-10 thomas if (err)
10836 97a02382 2023-07-10 thomas goto done;
10838 97a02382 2023-07-10 thomas err = get_commit_brief_str(&yca_brief_str, yca_commit);
10839 97a02382 2023-07-10 thomas if (err)
10840 97a02382 2023-07-10 thomas goto done;
10842 97a02382 2023-07-10 thomas err = got_object_id_str(&yca_id_str, yca_id);
10843 97a02382 2023-07-10 thomas if (err)
10844 97a02382 2023-07-10 thomas goto done;
10846 97a02382 2023-07-10 thomas refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
10847 97a02382 2023-07-10 thomas if (refs) {
10848 97a02382 2023-07-10 thomas err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
10849 97a02382 2023-07-10 thomas if (err)
10850 97a02382 2023-07-10 thomas goto done;
10852 97a02382 2023-07-10 thomas printf("history forked at %s%s%s%s\n %s\n",
10853 97a02382 2023-07-10 thomas yca_id_str,
10854 97a02382 2023-07-10 thomas refs_str ? " (" : "", refs_str ? refs_str : "",
10855 97a02382 2023-07-10 thomas refs_str ? ")" : "", yca_brief_str);
10858 97a02382 2023-07-10 thomas free(custom_refs_str);
10859 97a02382 2023-07-10 thomas free(new_commit_id);
10860 97a02382 2023-07-10 thomas free(refs_str);
10861 97a02382 2023-07-10 thomas free(yca_id);
10862 97a02382 2023-07-10 thomas free(yca_id_str);
10863 97a02382 2023-07-10 thomas free(yca_brief_str);
10864 97a02382 2023-07-10 thomas if (new_commit)
10865 97a02382 2023-07-10 thomas got_object_commit_close(new_commit);
10866 97a02382 2023-07-10 thomas if (yca_commit)
10867 97a02382 2023-07-10 thomas got_object_commit_close(yca_commit);
10869 97a02382 2023-07-10 thomas return err;
10872 97a02382 2023-07-10 thomas static const struct got_error *
10873 97a02382 2023-07-10 thomas worktree_has_logmsg_ref(const char *caller, struct got_worktree *worktree,
10874 97a02382 2023-07-10 thomas struct got_repository *repo)
10876 97a02382 2023-07-10 thomas const struct got_error *err;
10877 97a02382 2023-07-10 thomas struct got_reflist_head refs;
10878 97a02382 2023-07-10 thomas struct got_reflist_entry *re;
10879 97a02382 2023-07-10 thomas char *uuidstr = NULL;
10880 97a02382 2023-07-10 thomas static char msg[160];
10882 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
10884 97a02382 2023-07-10 thomas err = got_worktree_get_uuid(&uuidstr, worktree);
10885 97a02382 2023-07-10 thomas if (err)
10886 97a02382 2023-07-10 thomas goto done;
10888 97a02382 2023-07-10 thomas err = got_ref_list(&refs, repo, "refs/got/worktree",
10889 97a02382 2023-07-10 thomas got_ref_cmp_by_name, repo);
10890 97a02382 2023-07-10 thomas if (err)
10891 97a02382 2023-07-10 thomas goto done;
10893 97a02382 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
10894 97a02382 2023-07-10 thomas const char *cmd, *refname, *type;
10896 97a02382 2023-07-10 thomas refname = got_ref_get_name(re->ref);
10898 97a02382 2023-07-10 thomas if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10899 97a02382 2023-07-10 thomas GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
10900 97a02382 2023-07-10 thomas refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
10901 97a02382 2023-07-10 thomas cmd = "cherrypick";
10902 97a02382 2023-07-10 thomas type = "cherrypicked";
10903 97a02382 2023-07-10 thomas } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10904 97a02382 2023-07-10 thomas GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
10905 97a02382 2023-07-10 thomas refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
10906 97a02382 2023-07-10 thomas cmd = "backout";
10907 97a02382 2023-07-10 thomas type = "backed-out";
10909 97a02382 2023-07-10 thomas continue;
10911 97a02382 2023-07-10 thomas if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) != 0)
10912 97a02382 2023-07-10 thomas continue;
10914 97a02382 2023-07-10 thomas snprintf(msg, sizeof(msg),
10915 97a02382 2023-07-10 thomas "work tree has references created by %s commits which "
10916 97a02382 2023-07-10 thomas "must be removed with 'got %s -X' before running the %s "
10917 97a02382 2023-07-10 thomas "command", type, cmd, caller);
10918 97a02382 2023-07-10 thomas err = got_error_msg(GOT_ERR_WORKTREE_META, msg);
10919 97a02382 2023-07-10 thomas goto done;
10923 97a02382 2023-07-10 thomas free(uuidstr);
10924 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
10925 97a02382 2023-07-10 thomas return err;
10928 97a02382 2023-07-10 thomas static const struct got_error *
10929 97a02382 2023-07-10 thomas process_backup_refs(const char *backup_ref_prefix,
10930 97a02382 2023-07-10 thomas const char *wanted_branch_name,
10931 97a02382 2023-07-10 thomas int delete, struct got_repository *repo)
10933 97a02382 2023-07-10 thomas const struct got_error *err;
10934 97a02382 2023-07-10 thomas struct got_reflist_head refs, backup_refs;
10935 97a02382 2023-07-10 thomas struct got_reflist_entry *re;
10936 97a02382 2023-07-10 thomas const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
10937 97a02382 2023-07-10 thomas struct got_object_id *old_commit_id = NULL;
10938 97a02382 2023-07-10 thomas char *branch_name = NULL;
10939 97a02382 2023-07-10 thomas struct got_commit_object *old_commit = NULL;
10940 97a02382 2023-07-10 thomas struct got_reflist_object_id_map *refs_idmap = NULL;
10941 97a02382 2023-07-10 thomas int wanted_branch_found = 0;
10943 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
10944 97a02382 2023-07-10 thomas TAILQ_INIT(&backup_refs);
10946 97a02382 2023-07-10 thomas err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10947 97a02382 2023-07-10 thomas if (err)
10948 97a02382 2023-07-10 thomas return err;
10950 97a02382 2023-07-10 thomas err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
10951 97a02382 2023-07-10 thomas if (err)
10952 97a02382 2023-07-10 thomas goto done;
10954 97a02382 2023-07-10 thomas if (wanted_branch_name) {
10955 97a02382 2023-07-10 thomas if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
10956 97a02382 2023-07-10 thomas wanted_branch_name += 11;
10959 97a02382 2023-07-10 thomas err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
10960 97a02382 2023-07-10 thomas got_ref_cmp_by_commit_timestamp_descending, repo);
10961 97a02382 2023-07-10 thomas if (err)
10962 97a02382 2023-07-10 thomas goto done;
10964 97a02382 2023-07-10 thomas TAILQ_FOREACH(re, &backup_refs, entry) {
10965 97a02382 2023-07-10 thomas const char *refname = got_ref_get_name(re->ref);
10966 97a02382 2023-07-10 thomas char *slash;
10968 97a02382 2023-07-10 thomas err = check_cancelled(NULL);
10969 97a02382 2023-07-10 thomas if (err)
10972 97a02382 2023-07-10 thomas err = got_ref_resolve(&old_commit_id, repo, re->ref);
10973 97a02382 2023-07-10 thomas if (err)
10976 97a02382 2023-07-10 thomas err = got_object_open_as_commit(&old_commit, repo,
10977 97a02382 2023-07-10 thomas old_commit_id);
10978 97a02382 2023-07-10 thomas if (err)
10981 97a02382 2023-07-10 thomas if (strncmp(backup_ref_prefix, refname,
10982 97a02382 2023-07-10 thomas backup_ref_prefix_len) == 0)
10983 97a02382 2023-07-10 thomas refname += backup_ref_prefix_len;
10985 97a02382 2023-07-10 thomas while (refname[0] == '/')
10986 97a02382 2023-07-10 thomas refname++;
10988 97a02382 2023-07-10 thomas branch_name = strdup(refname);
10989 97a02382 2023-07-10 thomas if (branch_name == NULL) {
10990 97a02382 2023-07-10 thomas err = got_error_from_errno("strdup");
10993 97a02382 2023-07-10 thomas slash = strrchr(branch_name, '/');
10994 97a02382 2023-07-10 thomas if (slash) {
10995 97a02382 2023-07-10 thomas *slash = '\0';
10996 97a02382 2023-07-10 thomas refname += strlen(branch_name) + 1;
10999 97a02382 2023-07-10 thomas if (wanted_branch_name == NULL ||
11000 97a02382 2023-07-10 thomas strcmp(wanted_branch_name, branch_name) == 0) {
11001 97a02382 2023-07-10 thomas wanted_branch_found = 1;
11002 97a02382 2023-07-10 thomas if (delete) {
11003 97a02382 2023-07-10 thomas err = delete_backup_ref(re->ref,
11004 97a02382 2023-07-10 thomas old_commit_id, repo);
11005 97a02382 2023-07-10 thomas } else {
11006 97a02382 2023-07-10 thomas err = print_backup_ref(branch_name, refname,
11007 97a02382 2023-07-10 thomas old_commit_id, old_commit, refs_idmap,
11010 97a02382 2023-07-10 thomas if (err)
11014 97a02382 2023-07-10 thomas free(old_commit_id);
11015 97a02382 2023-07-10 thomas old_commit_id = NULL;
11016 97a02382 2023-07-10 thomas free(branch_name);
11017 97a02382 2023-07-10 thomas branch_name = NULL;
11018 97a02382 2023-07-10 thomas got_object_commit_close(old_commit);
11019 97a02382 2023-07-10 thomas old_commit = NULL;
11022 97a02382 2023-07-10 thomas if (wanted_branch_name && !wanted_branch_found) {
11023 97a02382 2023-07-10 thomas err = got_error_fmt(GOT_ERR_NOT_REF,
11024 97a02382 2023-07-10 thomas "%s/%s/", backup_ref_prefix, wanted_branch_name);
11027 97a02382 2023-07-10 thomas if (refs_idmap)
11028 97a02382 2023-07-10 thomas got_reflist_object_id_map_free(refs_idmap);
11029 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
11030 97a02382 2023-07-10 thomas got_ref_list_free(&backup_refs);
11031 97a02382 2023-07-10 thomas free(old_commit_id);
11032 97a02382 2023-07-10 thomas free(branch_name);
11033 97a02382 2023-07-10 thomas if (old_commit)
11034 97a02382 2023-07-10 thomas got_object_commit_close(old_commit);
11035 97a02382 2023-07-10 thomas return err;
11038 97a02382 2023-07-10 thomas static const struct got_error *
11039 97a02382 2023-07-10 thomas abort_progress(void *arg, unsigned char status, const char *path)
11042 97a02382 2023-07-10 thomas * Unversioned files should not clutter progress output when
11043 97a02382 2023-07-10 thomas * an operation is aborted.
11045 97a02382 2023-07-10 thomas if (status == GOT_STATUS_UNVERSIONED)
11046 97a02382 2023-07-10 thomas return NULL;
11048 97a02382 2023-07-10 thomas return update_progress(arg, status, path);
11051 97a02382 2023-07-10 thomas static const struct got_error *
11052 97a02382 2023-07-10 thomas cmd_rebase(int argc, char *argv[])
11054 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
11055 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
11056 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
11057 97a02382 2023-07-10 thomas struct got_fileindex *fileindex = NULL;
11058 97a02382 2023-07-10 thomas char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
11059 97a02382 2023-07-10 thomas struct got_reference *branch = NULL;
11060 97a02382 2023-07-10 thomas struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
11061 97a02382 2023-07-10 thomas struct got_object_id *commit_id = NULL, *parent_id = NULL;
11062 97a02382 2023-07-10 thomas struct got_object_id *resume_commit_id = NULL;
11063 97a02382 2023-07-10 thomas struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
11064 97a02382 2023-07-10 thomas struct got_object_id *head_commit_id = NULL;
11065 97a02382 2023-07-10 thomas struct got_reference *head_ref = NULL;
11066 97a02382 2023-07-10 thomas struct got_commit_object *commit = NULL;
11067 97a02382 2023-07-10 thomas int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
11068 97a02382 2023-07-10 thomas int histedit_in_progress = 0, merge_in_progress = 0;
11069 97a02382 2023-07-10 thomas int create_backup = 1, list_backups = 0, delete_backups = 0;
11070 97a02382 2023-07-10 thomas int allow_conflict = 0;
11071 97a02382 2023-07-10 thomas struct got_object_id_queue commits;
11072 97a02382 2023-07-10 thomas struct got_pathlist_head merged_paths;
11073 97a02382 2023-07-10 thomas const struct got_object_id_queue *parent_ids;
11074 97a02382 2023-07-10 thomas struct got_object_qid *qid, *pid;
11075 97a02382 2023-07-10 thomas struct got_update_progress_arg upa;
11076 97a02382 2023-07-10 thomas int *pack_fds = NULL;
11078 97a02382 2023-07-10 thomas STAILQ_INIT(&commits);
11079 97a02382 2023-07-10 thomas TAILQ_INIT(&merged_paths);
11080 97a02382 2023-07-10 thomas memset(&upa, 0, sizeof(upa));
11082 97a02382 2023-07-10 thomas #ifndef PROFILE
11083 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11084 97a02382 2023-07-10 thomas "unveil", NULL) == -1)
11085 97a02382 2023-07-10 thomas err(1, "pledge");
11088 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "aCclX")) != -1) {
11089 97a02382 2023-07-10 thomas switch (ch) {
11090 97a02382 2023-07-10 thomas case 'a':
11091 97a02382 2023-07-10 thomas abort_rebase = 1;
11093 97a02382 2023-07-10 thomas case 'C':
11094 97a02382 2023-07-10 thomas allow_conflict = 1;
11096 97a02382 2023-07-10 thomas case 'c':
11097 97a02382 2023-07-10 thomas continue_rebase = 1;
11099 97a02382 2023-07-10 thomas case 'l':
11100 97a02382 2023-07-10 thomas list_backups = 1;
11102 97a02382 2023-07-10 thomas case 'X':
11103 97a02382 2023-07-10 thomas delete_backups = 1;
11105 97a02382 2023-07-10 thomas default:
11106 97a02382 2023-07-10 thomas usage_rebase();
11107 97a02382 2023-07-10 thomas /* NOTREACHED */
11111 97a02382 2023-07-10 thomas argc -= optind;
11112 97a02382 2023-07-10 thomas argv += optind;
11114 97a02382 2023-07-10 thomas if (list_backups) {
11115 97a02382 2023-07-10 thomas if (abort_rebase)
11116 97a02382 2023-07-10 thomas option_conflict('l', 'a');
11117 97a02382 2023-07-10 thomas if (allow_conflict)
11118 97a02382 2023-07-10 thomas option_conflict('l', 'C');
11119 97a02382 2023-07-10 thomas if (continue_rebase)
11120 97a02382 2023-07-10 thomas option_conflict('l', 'c');
11121 97a02382 2023-07-10 thomas if (delete_backups)
11122 97a02382 2023-07-10 thomas option_conflict('l', 'X');
11123 97a02382 2023-07-10 thomas if (argc != 0 && argc != 1)
11124 97a02382 2023-07-10 thomas usage_rebase();
11125 97a02382 2023-07-10 thomas } else if (delete_backups) {
11126 97a02382 2023-07-10 thomas if (abort_rebase)
11127 97a02382 2023-07-10 thomas option_conflict('X', 'a');
11128 97a02382 2023-07-10 thomas if (allow_conflict)
11129 97a02382 2023-07-10 thomas option_conflict('X', 'C');
11130 97a02382 2023-07-10 thomas if (continue_rebase)
11131 97a02382 2023-07-10 thomas option_conflict('X', 'c');
11132 97a02382 2023-07-10 thomas if (list_backups)
11133 97a02382 2023-07-10 thomas option_conflict('l', 'X');
11134 97a02382 2023-07-10 thomas if (argc != 0 && argc != 1)
11135 97a02382 2023-07-10 thomas usage_rebase();
11136 97a02382 2023-07-10 thomas } else if (allow_conflict) {
11137 97a02382 2023-07-10 thomas if (abort_rebase)
11138 97a02382 2023-07-10 thomas option_conflict('C', 'a');
11139 97a02382 2023-07-10 thomas if (!continue_rebase)
11140 97a02382 2023-07-10 thomas errx(1, "-C option requires -c");
11141 97a02382 2023-07-10 thomas } else {
11142 97a02382 2023-07-10 thomas if (abort_rebase && continue_rebase)
11143 97a02382 2023-07-10 thomas usage_rebase();
11144 97a02382 2023-07-10 thomas else if (abort_rebase || continue_rebase) {
11145 97a02382 2023-07-10 thomas if (argc != 0)
11146 97a02382 2023-07-10 thomas usage_rebase();
11147 97a02382 2023-07-10 thomas } else if (argc != 1)
11148 97a02382 2023-07-10 thomas usage_rebase();
11151 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
11152 97a02382 2023-07-10 thomas if (cwd == NULL) {
11153 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
11154 97a02382 2023-07-10 thomas goto done;
11157 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
11158 97a02382 2023-07-10 thomas if (error != NULL)
11159 97a02382 2023-07-10 thomas goto done;
11161 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
11162 97a02382 2023-07-10 thomas if (error) {
11163 97a02382 2023-07-10 thomas if (list_backups || delete_backups) {
11164 97a02382 2023-07-10 thomas if (error->code != GOT_ERR_NOT_WORKTREE)
11165 97a02382 2023-07-10 thomas goto done;
11166 97a02382 2023-07-10 thomas } else {
11167 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
11168 97a02382 2023-07-10 thomas error = wrap_not_worktree_error(error,
11169 97a02382 2023-07-10 thomas "rebase", cwd);
11170 97a02382 2023-07-10 thomas goto done;
11174 97a02382 2023-07-10 thomas error = get_gitconfig_path(&gitconfig_path);
11175 97a02382 2023-07-10 thomas if (error)
11176 97a02382 2023-07-10 thomas goto done;
11177 97a02382 2023-07-10 thomas error = got_repo_open(&repo,
11178 97a02382 2023-07-10 thomas worktree ? got_worktree_get_repo_path(worktree) : cwd,
11179 97a02382 2023-07-10 thomas gitconfig_path, pack_fds);
11180 97a02382 2023-07-10 thomas if (error != NULL)
11181 97a02382 2023-07-10 thomas goto done;
11183 97a02382 2023-07-10 thomas if (worktree != NULL && !list_backups && !delete_backups) {
11184 97a02382 2023-07-10 thomas error = worktree_has_logmsg_ref("rebase", worktree, repo);
11185 97a02382 2023-07-10 thomas if (error)
11186 97a02382 2023-07-10 thomas goto done;
11189 97a02382 2023-07-10 thomas error = get_author(&committer, repo, worktree);
11190 97a02382 2023-07-10 thomas if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
11191 97a02382 2023-07-10 thomas goto done;
11193 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
11194 97a02382 2023-07-10 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
11195 97a02382 2023-07-10 thomas if (error)
11196 97a02382 2023-07-10 thomas goto done;
11198 97a02382 2023-07-10 thomas if (list_backups || delete_backups) {
11199 97a02382 2023-07-10 thomas error = process_backup_refs(
11200 97a02382 2023-07-10 thomas GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
11201 97a02382 2023-07-10 thomas argc == 1 ? argv[0] : NULL, delete_backups, repo);
11202 97a02382 2023-07-10 thomas goto done; /* nothing else to do */
11205 97a02382 2023-07-10 thomas error = got_worktree_histedit_in_progress(&histedit_in_progress,
11206 97a02382 2023-07-10 thomas worktree);
11207 97a02382 2023-07-10 thomas if (error)
11208 97a02382 2023-07-10 thomas goto done;
11209 97a02382 2023-07-10 thomas if (histedit_in_progress) {
11210 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_HISTEDIT_BUSY);
11211 97a02382 2023-07-10 thomas goto done;
11214 97a02382 2023-07-10 thomas error = got_worktree_merge_in_progress(&merge_in_progress,
11215 97a02382 2023-07-10 thomas worktree, repo);
11216 97a02382 2023-07-10 thomas if (error)
11217 97a02382 2023-07-10 thomas goto done;
11218 97a02382 2023-07-10 thomas if (merge_in_progress) {
11219 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_MERGE_BUSY);
11220 97a02382 2023-07-10 thomas goto done;
11223 97a02382 2023-07-10 thomas error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11224 97a02382 2023-07-10 thomas if (error)
11225 97a02382 2023-07-10 thomas goto done;
11227 97a02382 2023-07-10 thomas if (abort_rebase) {
11228 97a02382 2023-07-10 thomas if (!rebase_in_progress) {
11229 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_NOT_REBASING);
11230 97a02382 2023-07-10 thomas goto done;
11232 97a02382 2023-07-10 thomas error = got_worktree_rebase_continue(&resume_commit_id,
11233 97a02382 2023-07-10 thomas &new_base_branch, &tmp_branch, &branch, &fileindex,
11234 97a02382 2023-07-10 thomas worktree, repo);
11235 97a02382 2023-07-10 thomas if (error)
11236 97a02382 2023-07-10 thomas goto done;
11237 97a02382 2023-07-10 thomas printf("Switching work tree to %s\n",
11238 97a02382 2023-07-10 thomas got_ref_get_symref_target(new_base_branch));
11239 97a02382 2023-07-10 thomas error = got_worktree_rebase_abort(worktree, fileindex, repo,
11240 97a02382 2023-07-10 thomas new_base_branch, abort_progress, &upa);
11241 97a02382 2023-07-10 thomas if (error)
11242 97a02382 2023-07-10 thomas goto done;
11243 97a02382 2023-07-10 thomas printf("Rebase of %s aborted\n", got_ref_get_name(branch));
11244 97a02382 2023-07-10 thomas print_merge_progress_stats(&upa);
11245 97a02382 2023-07-10 thomas goto done; /* nothing else to do */
11248 97a02382 2023-07-10 thomas if (continue_rebase) {
11249 97a02382 2023-07-10 thomas if (!rebase_in_progress) {
11250 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_NOT_REBASING);
11251 97a02382 2023-07-10 thomas goto done;
11253 97a02382 2023-07-10 thomas error = got_worktree_rebase_continue(&resume_commit_id,
11254 97a02382 2023-07-10 thomas &new_base_branch, &tmp_branch, &branch, &fileindex,
11255 97a02382 2023-07-10 thomas worktree, repo);
11256 97a02382 2023-07-10 thomas if (error)
11257 97a02382 2023-07-10 thomas goto done;
11259 97a02382 2023-07-10 thomas error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
11260 97a02382 2023-07-10 thomas committer, resume_commit_id, allow_conflict, repo);
11261 97a02382 2023-07-10 thomas if (error)
11262 97a02382 2023-07-10 thomas goto done;
11264 97a02382 2023-07-10 thomas yca_id = got_object_id_dup(resume_commit_id);
11265 97a02382 2023-07-10 thomas if (yca_id == NULL) {
11266 97a02382 2023-07-10 thomas error = got_error_from_errno("got_object_id_dup");
11267 97a02382 2023-07-10 thomas goto done;
11269 97a02382 2023-07-10 thomas } else {
11270 97a02382 2023-07-10 thomas error = got_ref_open(&branch, repo, argv[0], 0);
11271 97a02382 2023-07-10 thomas if (error != NULL)
11272 97a02382 2023-07-10 thomas goto done;
11273 97a02382 2023-07-10 thomas if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11274 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11275 97a02382 2023-07-10 thomas "will not rebase a branch which lives outside "
11276 97a02382 2023-07-10 thomas "the \"refs/heads/\" reference namespace");
11277 97a02382 2023-07-10 thomas goto done;
11281 97a02382 2023-07-10 thomas error = got_ref_resolve(&branch_head_commit_id, repo, branch);
11282 97a02382 2023-07-10 thomas if (error)
11283 97a02382 2023-07-10 thomas goto done;
11285 97a02382 2023-07-10 thomas if (!continue_rebase) {
11286 97a02382 2023-07-10 thomas struct got_object_id *base_commit_id;
11288 97a02382 2023-07-10 thomas error = got_ref_open(&head_ref, repo,
11289 97a02382 2023-07-10 thomas got_worktree_get_head_ref_name(worktree), 0);
11290 97a02382 2023-07-10 thomas if (error)
11291 97a02382 2023-07-10 thomas goto done;
11292 97a02382 2023-07-10 thomas error = got_ref_resolve(&head_commit_id, repo, head_ref);
11293 97a02382 2023-07-10 thomas if (error)
11294 97a02382 2023-07-10 thomas goto done;
11295 97a02382 2023-07-10 thomas base_commit_id = got_worktree_get_base_commit_id(worktree);
11296 97a02382 2023-07-10 thomas if (got_object_id_cmp(base_commit_id, head_commit_id) != 0) {
11297 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
11298 97a02382 2023-07-10 thomas goto done;
11301 97a02382 2023-07-10 thomas error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11302 97a02382 2023-07-10 thomas base_commit_id, branch_head_commit_id, 1, repo,
11303 97a02382 2023-07-10 thomas check_cancelled, NULL);
11304 97a02382 2023-07-10 thomas if (error) {
11305 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_ANCESTRY) {
11306 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_ANCESTRY,
11307 97a02382 2023-07-10 thomas "specified branch shares no common "
11308 97a02382 2023-07-10 thomas "ancestry with work tree's branch");
11310 97a02382 2023-07-10 thomas goto done;
11313 97a02382 2023-07-10 thomas if (got_object_id_cmp(base_commit_id, yca_id) == 0) {
11314 97a02382 2023-07-10 thomas struct got_pathlist_head paths;
11315 97a02382 2023-07-10 thomas printf("%s is already based on %s\n",
11316 97a02382 2023-07-10 thomas got_ref_get_name(branch),
11317 97a02382 2023-07-10 thomas got_worktree_get_head_ref_name(worktree));
11318 97a02382 2023-07-10 thomas error = switch_head_ref(branch, branch_head_commit_id,
11319 97a02382 2023-07-10 thomas worktree, repo);
11320 97a02382 2023-07-10 thomas if (error)
11321 97a02382 2023-07-10 thomas goto done;
11322 97a02382 2023-07-10 thomas error = got_worktree_set_base_commit_id(worktree, repo,
11323 97a02382 2023-07-10 thomas branch_head_commit_id);
11324 97a02382 2023-07-10 thomas if (error)
11325 97a02382 2023-07-10 thomas goto done;
11326 97a02382 2023-07-10 thomas TAILQ_INIT(&paths);
11327 97a02382 2023-07-10 thomas error = got_pathlist_append(&paths, "", NULL);
11328 97a02382 2023-07-10 thomas if (error)
11329 97a02382 2023-07-10 thomas goto done;
11330 97a02382 2023-07-10 thomas error = got_worktree_checkout_files(worktree,
11331 97a02382 2023-07-10 thomas &paths, repo, update_progress, &upa,
11332 97a02382 2023-07-10 thomas check_cancelled, NULL);
11333 97a02382 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
11334 97a02382 2023-07-10 thomas if (error)
11335 97a02382 2023-07-10 thomas goto done;
11336 97a02382 2023-07-10 thomas if (upa.did_something) {
11337 97a02382 2023-07-10 thomas char *id_str;
11338 97a02382 2023-07-10 thomas error = got_object_id_str(&id_str,
11339 97a02382 2023-07-10 thomas branch_head_commit_id);
11340 97a02382 2023-07-10 thomas if (error)
11341 97a02382 2023-07-10 thomas goto done;
11342 97a02382 2023-07-10 thomas printf("Updated to %s: %s\n",
11343 97a02382 2023-07-10 thomas got_worktree_get_head_ref_name(worktree),
11344 97a02382 2023-07-10 thomas id_str);
11345 97a02382 2023-07-10 thomas free(id_str);
11347 97a02382 2023-07-10 thomas printf("Already up-to-date\n");
11348 97a02382 2023-07-10 thomas print_update_progress_stats(&upa);
11349 97a02382 2023-07-10 thomas goto done;
11353 97a02382 2023-07-10 thomas commit_id = branch_head_commit_id;
11354 97a02382 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
11355 97a02382 2023-07-10 thomas if (error)
11356 97a02382 2023-07-10 thomas goto done;
11358 97a02382 2023-07-10 thomas parent_ids = got_object_commit_get_parent_ids(commit);
11359 97a02382 2023-07-10 thomas pid = STAILQ_FIRST(parent_ids);
11360 97a02382 2023-07-10 thomas if (pid) {
11361 97a02382 2023-07-10 thomas error = collect_commits(&commits, commit_id, &pid->id,
11362 97a02382 2023-07-10 thomas yca_id, got_worktree_get_path_prefix(worktree),
11363 97a02382 2023-07-10 thomas GOT_ERR_REBASE_PATH, repo);
11364 97a02382 2023-07-10 thomas if (error)
11365 97a02382 2023-07-10 thomas goto done;
11368 97a02382 2023-07-10 thomas got_object_commit_close(commit);
11369 97a02382 2023-07-10 thomas commit = NULL;
11371 97a02382 2023-07-10 thomas if (!continue_rebase) {
11372 97a02382 2023-07-10 thomas error = got_worktree_rebase_prepare(&new_base_branch,
11373 97a02382 2023-07-10 thomas &tmp_branch, &fileindex, worktree, branch, repo);
11374 97a02382 2023-07-10 thomas if (error)
11375 97a02382 2023-07-10 thomas goto done;
11378 97a02382 2023-07-10 thomas if (STAILQ_EMPTY(&commits)) {
11379 97a02382 2023-07-10 thomas if (continue_rebase) {
11380 97a02382 2023-07-10 thomas error = rebase_complete(worktree, fileindex,
11381 97a02382 2023-07-10 thomas branch, tmp_branch, repo, create_backup);
11382 97a02382 2023-07-10 thomas goto done;
11383 97a02382 2023-07-10 thomas } else {
11384 97a02382 2023-07-10 thomas /* Fast-forward the reference of the branch. */
11385 97a02382 2023-07-10 thomas struct got_object_id *new_head_commit_id;
11386 97a02382 2023-07-10 thomas char *id_str;
11387 97a02382 2023-07-10 thomas error = got_ref_resolve(&new_head_commit_id, repo,
11388 97a02382 2023-07-10 thomas new_base_branch);
11389 97a02382 2023-07-10 thomas if (error)
11390 97a02382 2023-07-10 thomas goto done;
11391 97a02382 2023-07-10 thomas error = got_object_id_str(&id_str, new_head_commit_id);
11392 97a02382 2023-07-10 thomas if (error)
11393 97a02382 2023-07-10 thomas goto done;
11394 97a02382 2023-07-10 thomas printf("Forwarding %s to commit %s\n",
11395 97a02382 2023-07-10 thomas got_ref_get_name(branch), id_str);
11396 97a02382 2023-07-10 thomas free(id_str);
11397 97a02382 2023-07-10 thomas error = got_ref_change_ref(branch,
11398 97a02382 2023-07-10 thomas new_head_commit_id);
11399 97a02382 2023-07-10 thomas if (error)
11400 97a02382 2023-07-10 thomas goto done;
11401 97a02382 2023-07-10 thomas /* No backup needed since objects did not change. */
11402 97a02382 2023-07-10 thomas create_backup = 0;
11406 97a02382 2023-07-10 thomas pid = NULL;
11407 97a02382 2023-07-10 thomas STAILQ_FOREACH(qid, &commits, entry) {
11409 97a02382 2023-07-10 thomas commit_id = &qid->id;
11410 97a02382 2023-07-10 thomas parent_id = pid ? &pid->id : yca_id;
11411 97a02382 2023-07-10 thomas pid = qid;
11413 97a02382 2023-07-10 thomas memset(&upa, 0, sizeof(upa));
11414 97a02382 2023-07-10 thomas error = got_worktree_rebase_merge_files(&merged_paths,
11415 97a02382 2023-07-10 thomas worktree, fileindex, parent_id, commit_id, repo,
11416 97a02382 2023-07-10 thomas update_progress, &upa, check_cancelled, NULL);
11417 97a02382 2023-07-10 thomas if (error)
11418 97a02382 2023-07-10 thomas goto done;
11420 97a02382 2023-07-10 thomas print_merge_progress_stats(&upa);
11421 97a02382 2023-07-10 thomas if (upa.conflicts > 0 || upa.missing > 0 ||
11422 97a02382 2023-07-10 thomas upa.not_deleted > 0 || upa.unversioned > 0) {
11423 97a02382 2023-07-10 thomas if (upa.conflicts > 0) {
11424 97a02382 2023-07-10 thomas error = show_rebase_merge_conflict(&qid->id,
11426 97a02382 2023-07-10 thomas if (error)
11427 97a02382 2023-07-10 thomas goto done;
11429 97a02382 2023-07-10 thomas got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11433 97a02382 2023-07-10 thomas error = rebase_commit(&merged_paths, worktree, fileindex,
11434 97a02382 2023-07-10 thomas tmp_branch, committer, commit_id, 0, repo);
11435 97a02382 2023-07-10 thomas got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11436 97a02382 2023-07-10 thomas if (error)
11437 97a02382 2023-07-10 thomas goto done;
11440 97a02382 2023-07-10 thomas if (upa.conflicts > 0 || upa.missing > 0 ||
11441 97a02382 2023-07-10 thomas upa.not_deleted > 0 || upa.unversioned > 0) {
11442 97a02382 2023-07-10 thomas error = got_worktree_rebase_postpone(worktree, fileindex);
11443 97a02382 2023-07-10 thomas if (error)
11444 97a02382 2023-07-10 thomas goto done;
11445 97a02382 2023-07-10 thomas if (upa.conflicts > 0 && upa.missing == 0 &&
11446 97a02382 2023-07-10 thomas upa.not_deleted == 0 && upa.unversioned == 0) {
11447 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
11448 97a02382 2023-07-10 thomas "conflicts must be resolved before rebasing "
11449 97a02382 2023-07-10 thomas "can continue");
11450 97a02382 2023-07-10 thomas } else if (upa.conflicts > 0) {
11451 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
11452 97a02382 2023-07-10 thomas "conflicts must be resolved before rebasing "
11453 97a02382 2023-07-10 thomas "can continue; changes destined for some "
11454 97a02382 2023-07-10 thomas "files were not yet merged and should be "
11455 97a02382 2023-07-10 thomas "merged manually if required before the "
11456 97a02382 2023-07-10 thomas "rebase operation is continued");
11457 97a02382 2023-07-10 thomas } else {
11458 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
11459 97a02382 2023-07-10 thomas "changes destined for some files were not "
11460 97a02382 2023-07-10 thomas "yet merged and should be merged manually "
11461 97a02382 2023-07-10 thomas "if required before the rebase operation "
11462 97a02382 2023-07-10 thomas "is continued");
11465 97a02382 2023-07-10 thomas error = rebase_complete(worktree, fileindex, branch,
11466 97a02382 2023-07-10 thomas tmp_branch, repo, create_backup);
11468 97a02382 2023-07-10 thomas free(cwd);
11469 97a02382 2023-07-10 thomas free(committer);
11470 97a02382 2023-07-10 thomas free(gitconfig_path);
11471 97a02382 2023-07-10 thomas got_object_id_queue_free(&commits);
11472 97a02382 2023-07-10 thomas free(branch_head_commit_id);
11473 97a02382 2023-07-10 thomas free(resume_commit_id);
11474 97a02382 2023-07-10 thomas free(head_commit_id);
11475 97a02382 2023-07-10 thomas free(yca_id);
11476 97a02382 2023-07-10 thomas if (commit)
11477 97a02382 2023-07-10 thomas got_object_commit_close(commit);
11478 97a02382 2023-07-10 thomas if (branch)
11479 97a02382 2023-07-10 thomas got_ref_close(branch);
11480 97a02382 2023-07-10 thomas if (new_base_branch)
11481 97a02382 2023-07-10 thomas got_ref_close(new_base_branch);
11482 97a02382 2023-07-10 thomas if (tmp_branch)
11483 97a02382 2023-07-10 thomas got_ref_close(tmp_branch);
11484 97a02382 2023-07-10 thomas if (head_ref)
11485 97a02382 2023-07-10 thomas got_ref_close(head_ref);
11486 97a02382 2023-07-10 thomas if (worktree)
11487 97a02382 2023-07-10 thomas got_worktree_close(worktree);
11488 97a02382 2023-07-10 thomas if (repo) {
11489 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
11490 97a02382 2023-07-10 thomas if (error == NULL)
11491 97a02382 2023-07-10 thomas error = close_err;
11493 97a02382 2023-07-10 thomas if (pack_fds) {
11494 97a02382 2023-07-10 thomas const struct got_error *pack_err =
11495 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
11496 97a02382 2023-07-10 thomas if (error == NULL)
11497 97a02382 2023-07-10 thomas error = pack_err;
11499 97a02382 2023-07-10 thomas return error;
11502 97a02382 2023-07-10 thomas __dead static void
11503 97a02382 2023-07-10 thomas usage_histedit(void)
11505 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s histedit [-aCcdeflmX] [-F histedit-script] "
11506 97a02382 2023-07-10 thomas "[branch]\n", getprogname());
11507 97a02382 2023-07-10 thomas exit(1);
11510 97a02382 2023-07-10 thomas #define GOT_HISTEDIT_PICK 'p'
11511 97a02382 2023-07-10 thomas #define GOT_HISTEDIT_EDIT 'e'
11512 97a02382 2023-07-10 thomas #define GOT_HISTEDIT_FOLD 'f'
11513 97a02382 2023-07-10 thomas #define GOT_HISTEDIT_DROP 'd'
11514 97a02382 2023-07-10 thomas #define GOT_HISTEDIT_MESG 'm'
11516 97a02382 2023-07-10 thomas static const struct got_histedit_cmd {
11517 97a02382 2023-07-10 thomas unsigned char code;
11518 97a02382 2023-07-10 thomas const char *name;
11519 97a02382 2023-07-10 thomas const char *desc;
11520 97a02382 2023-07-10 thomas } got_histedit_cmds[] = {
11521 97a02382 2023-07-10 thomas { GOT_HISTEDIT_PICK, "pick", "use commit" },
11522 97a02382 2023-07-10 thomas { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
11523 97a02382 2023-07-10 thomas { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
11524 97a02382 2023-07-10 thomas "be used" },
11525 97a02382 2023-07-10 thomas { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
11526 97a02382 2023-07-10 thomas { GOT_HISTEDIT_MESG, "mesg",
11527 97a02382 2023-07-10 thomas "single-line log message for commit above (open editor if empty)" },
11530 97a02382 2023-07-10 thomas struct got_histedit_list_entry {
11531 97a02382 2023-07-10 thomas TAILQ_ENTRY(got_histedit_list_entry) entry;
11532 97a02382 2023-07-10 thomas struct got_object_id *commit_id;
11533 97a02382 2023-07-10 thomas const struct got_histedit_cmd *cmd;
11534 97a02382 2023-07-10 thomas char *logmsg;
11536 97a02382 2023-07-10 thomas TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
11538 97a02382 2023-07-10 thomas static const struct got_error *
11539 97a02382 2023-07-10 thomas histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
11540 97a02382 2023-07-10 thomas FILE *f, struct got_repository *repo)
11542 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
11543 97a02382 2023-07-10 thomas char *logmsg = NULL, *id_str = NULL;
11544 97a02382 2023-07-10 thomas struct got_commit_object *commit = NULL;
11547 97a02382 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
11548 97a02382 2023-07-10 thomas if (err)
11549 97a02382 2023-07-10 thomas goto done;
11551 97a02382 2023-07-10 thomas err = get_short_logmsg(&logmsg, 34, commit);
11552 97a02382 2023-07-10 thomas if (err)
11553 97a02382 2023-07-10 thomas goto done;
11555 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, commit_id);
11556 97a02382 2023-07-10 thomas if (err)
11557 97a02382 2023-07-10 thomas goto done;
11559 97a02382 2023-07-10 thomas n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
11560 97a02382 2023-07-10 thomas if (n < 0)
11561 97a02382 2023-07-10 thomas err = got_ferror(f, GOT_ERR_IO);
11563 97a02382 2023-07-10 thomas if (commit)
11564 97a02382 2023-07-10 thomas got_object_commit_close(commit);
11565 97a02382 2023-07-10 thomas free(id_str);
11566 97a02382 2023-07-10 thomas free(logmsg);
11567 97a02382 2023-07-10 thomas return err;
11570 97a02382 2023-07-10 thomas static const struct got_error *
11571 97a02382 2023-07-10 thomas histedit_write_commit_list(struct got_object_id_queue *commits,
11572 97a02382 2023-07-10 thomas FILE *f, int edit_logmsg_only, int fold_only, int drop_only,
11573 97a02382 2023-07-10 thomas int edit_only, struct got_repository *repo)
11575 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
11576 97a02382 2023-07-10 thomas struct got_object_qid *qid;
11577 97a02382 2023-07-10 thomas const char *histedit_cmd = NULL;
11579 97a02382 2023-07-10 thomas if (STAILQ_EMPTY(commits))
11580 97a02382 2023-07-10 thomas return got_error(GOT_ERR_EMPTY_HISTEDIT);
11582 97a02382 2023-07-10 thomas STAILQ_FOREACH(qid, commits, entry) {
11583 97a02382 2023-07-10 thomas histedit_cmd = got_histedit_cmds[0].name;
11584 97a02382 2023-07-10 thomas if (drop_only)
11585 97a02382 2023-07-10 thomas histedit_cmd = "drop";
11586 97a02382 2023-07-10 thomas else if (edit_only)
11587 97a02382 2023-07-10 thomas histedit_cmd = "edit";
11588 97a02382 2023-07-10 thomas else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
11589 97a02382 2023-07-10 thomas histedit_cmd = "fold";
11590 97a02382 2023-07-10 thomas err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
11591 97a02382 2023-07-10 thomas if (err)
11593 97a02382 2023-07-10 thomas if (edit_logmsg_only) {
11594 97a02382 2023-07-10 thomas int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
11595 97a02382 2023-07-10 thomas if (n < 0) {
11596 97a02382 2023-07-10 thomas err = got_ferror(f, GOT_ERR_IO);
11602 97a02382 2023-07-10 thomas return err;
11605 97a02382 2023-07-10 thomas static const struct got_error *
11606 97a02382 2023-07-10 thomas write_cmd_list(FILE *f, const char *branch_name,
11607 97a02382 2023-07-10 thomas struct got_object_id_queue *commits)
11609 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
11610 97a02382 2023-07-10 thomas size_t i;
11612 97a02382 2023-07-10 thomas char *id_str;
11613 97a02382 2023-07-10 thomas struct got_object_qid *qid;
11615 97a02382 2023-07-10 thomas qid = STAILQ_FIRST(commits);
11616 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, &qid->id);
11617 97a02382 2023-07-10 thomas if (err)
11618 97a02382 2023-07-10 thomas return err;
11620 97a02382 2023-07-10 thomas n = fprintf(f,
11621 97a02382 2023-07-10 thomas "# Editing the history of branch '%s' starting at\n"
11622 97a02382 2023-07-10 thomas "# commit %s\n"
11623 97a02382 2023-07-10 thomas "# Commits will be processed in order from top to "
11624 97a02382 2023-07-10 thomas "bottom of this file.\n", branch_name, id_str);
11625 97a02382 2023-07-10 thomas if (n < 0) {
11626 97a02382 2023-07-10 thomas err = got_ferror(f, GOT_ERR_IO);
11627 97a02382 2023-07-10 thomas goto done;
11630 97a02382 2023-07-10 thomas n = fprintf(f, "# Available histedit commands:\n");
11631 97a02382 2023-07-10 thomas if (n < 0) {
11632 97a02382 2023-07-10 thomas err = got_ferror(f, GOT_ERR_IO);
11633 97a02382 2023-07-10 thomas goto done;
11636 97a02382 2023-07-10 thomas for (i = 0; i < nitems(got_histedit_cmds); i++) {
11637 97a02382 2023-07-10 thomas const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
11638 97a02382 2023-07-10 thomas n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
11639 97a02382 2023-07-10 thomas cmd->desc);
11640 97a02382 2023-07-10 thomas if (n < 0) {
11641 97a02382 2023-07-10 thomas err = got_ferror(f, GOT_ERR_IO);
11646 97a02382 2023-07-10 thomas free(id_str);
11647 97a02382 2023-07-10 thomas return err;
11650 97a02382 2023-07-10 thomas static const struct got_error *
11651 97a02382 2023-07-10 thomas histedit_syntax_error(int lineno)
11653 97a02382 2023-07-10 thomas static char msg[42];
11654 97a02382 2023-07-10 thomas int ret;
11656 97a02382 2023-07-10 thomas ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
11657 97a02382 2023-07-10 thomas lineno);
11658 97a02382 2023-07-10 thomas if (ret < 0 || (size_t)ret >= sizeof(msg))
11659 97a02382 2023-07-10 thomas return got_error(GOT_ERR_HISTEDIT_SYNTAX);
11661 97a02382 2023-07-10 thomas return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
11664 97a02382 2023-07-10 thomas static const struct got_error *
11665 97a02382 2023-07-10 thomas append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
11666 97a02382 2023-07-10 thomas char *logmsg, struct got_repository *repo)
11668 97a02382 2023-07-10 thomas const struct got_error *err;
11669 97a02382 2023-07-10 thomas struct got_commit_object *folded_commit = NULL;
11670 97a02382 2023-07-10 thomas char *id_str, *folded_logmsg = NULL;
11672 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, hle->commit_id);
11673 97a02382 2023-07-10 thomas if (err)
11674 97a02382 2023-07-10 thomas return err;
11676 97a02382 2023-07-10 thomas err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
11677 97a02382 2023-07-10 thomas if (err)
11678 97a02382 2023-07-10 thomas goto done;
11680 97a02382 2023-07-10 thomas err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
11681 97a02382 2023-07-10 thomas if (err)
11682 97a02382 2023-07-10 thomas goto done;
11683 97a02382 2023-07-10 thomas if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
11684 97a02382 2023-07-10 thomas logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
11685 97a02382 2023-07-10 thomas folded_logmsg) == -1) {
11686 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
11689 97a02382 2023-07-10 thomas if (folded_commit)
11690 97a02382 2023-07-10 thomas got_object_commit_close(folded_commit);
11691 97a02382 2023-07-10 thomas free(id_str);
11692 97a02382 2023-07-10 thomas free(folded_logmsg);
11693 97a02382 2023-07-10 thomas return err;
11696 97a02382 2023-07-10 thomas static struct got_histedit_list_entry *
11697 97a02382 2023-07-10 thomas get_folded_commits(struct got_histedit_list_entry *hle)
11699 97a02382 2023-07-10 thomas struct got_histedit_list_entry *prev, *folded = NULL;
11701 97a02382 2023-07-10 thomas prev = TAILQ_PREV(hle, got_histedit_list, entry);
11702 97a02382 2023-07-10 thomas while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
11703 97a02382 2023-07-10 thomas prev->cmd->code == GOT_HISTEDIT_DROP)) {
11704 97a02382 2023-07-10 thomas if (prev->cmd->code == GOT_HISTEDIT_FOLD)
11705 97a02382 2023-07-10 thomas folded = prev;
11706 97a02382 2023-07-10 thomas prev = TAILQ_PREV(prev, got_histedit_list, entry);
11709 97a02382 2023-07-10 thomas return folded;
11712 97a02382 2023-07-10 thomas static const struct got_error *
11713 97a02382 2023-07-10 thomas histedit_edit_logmsg(struct got_histedit_list_entry *hle,
11714 97a02382 2023-07-10 thomas struct got_repository *repo)
11716 97a02382 2023-07-10 thomas char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
11717 97a02382 2023-07-10 thomas char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
11718 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
11719 97a02382 2023-07-10 thomas struct got_commit_object *commit = NULL;
11720 97a02382 2023-07-10 thomas int logmsg_len;
11721 97a02382 2023-07-10 thomas int fd = -1;
11722 97a02382 2023-07-10 thomas struct got_histedit_list_entry *folded = NULL;
11724 97a02382 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11725 97a02382 2023-07-10 thomas if (err)
11726 97a02382 2023-07-10 thomas return err;
11728 97a02382 2023-07-10 thomas folded = get_folded_commits(hle);
11729 97a02382 2023-07-10 thomas if (folded) {
11730 97a02382 2023-07-10 thomas while (folded != hle) {
11731 97a02382 2023-07-10 thomas if (folded->cmd->code == GOT_HISTEDIT_DROP) {
11732 97a02382 2023-07-10 thomas folded = TAILQ_NEXT(folded, entry);
11733 97a02382 2023-07-10 thomas continue;
11735 97a02382 2023-07-10 thomas err = append_folded_commit_msg(&new_msg, folded,
11736 97a02382 2023-07-10 thomas logmsg, repo);
11737 97a02382 2023-07-10 thomas if (err)
11738 97a02382 2023-07-10 thomas goto done;
11739 97a02382 2023-07-10 thomas free(logmsg);
11740 97a02382 2023-07-10 thomas logmsg = new_msg;
11741 97a02382 2023-07-10 thomas folded = TAILQ_NEXT(folded, entry);
11745 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, hle->commit_id);
11746 97a02382 2023-07-10 thomas if (err)
11747 97a02382 2023-07-10 thomas goto done;
11748 97a02382 2023-07-10 thomas err = got_object_commit_get_logmsg(&orig_logmsg, commit);
11749 97a02382 2023-07-10 thomas if (err)
11750 97a02382 2023-07-10 thomas goto done;
11751 97a02382 2023-07-10 thomas logmsg_len = asprintf(&new_msg,
11752 97a02382 2023-07-10 thomas "%s\n# original log message of commit %s: %s",
11753 97a02382 2023-07-10 thomas logmsg ? logmsg : "", id_str, orig_logmsg);
11754 97a02382 2023-07-10 thomas if (logmsg_len == -1) {
11755 97a02382 2023-07-10 thomas err = got_error_from_errno("asprintf");
11756 97a02382 2023-07-10 thomas goto done;
11758 97a02382 2023-07-10 thomas free(logmsg);
11759 97a02382 2023-07-10 thomas logmsg = new_msg;
11761 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, hle->commit_id);
11762 97a02382 2023-07-10 thomas if (err)
11763 97a02382 2023-07-10 thomas goto done;
11765 97a02382 2023-07-10 thomas err = got_opentemp_named_fd(&logmsg_path, &fd,
11766 97a02382 2023-07-10 thomas GOT_TMPDIR_STR "/got-logmsg", "");
11767 97a02382 2023-07-10 thomas if (err)
11768 97a02382 2023-07-10 thomas goto done;
11770 97a02382 2023-07-10 thomas if (write(fd, logmsg, logmsg_len) == -1) {
11771 97a02382 2023-07-10 thomas err = got_error_from_errno2("write", logmsg_path);
11772 97a02382 2023-07-10 thomas goto done;
11774 97a02382 2023-07-10 thomas if (close(fd) == -1) {
11775 97a02382 2023-07-10 thomas err = got_error_from_errno2("close", logmsg_path);
11776 97a02382 2023-07-10 thomas goto done;
11778 97a02382 2023-07-10 thomas fd = -1;
11780 97a02382 2023-07-10 thomas err = get_editor(&editor);
11781 97a02382 2023-07-10 thomas if (err)
11782 97a02382 2023-07-10 thomas goto done;
11784 97a02382 2023-07-10 thomas err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
11785 97a02382 2023-07-10 thomas logmsg_len, 0);
11786 97a02382 2023-07-10 thomas if (err) {
11787 97a02382 2023-07-10 thomas if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
11788 97a02382 2023-07-10 thomas goto done;
11789 97a02382 2023-07-10 thomas err = NULL;
11790 97a02382 2023-07-10 thomas hle->logmsg = strdup(new_msg);
11791 97a02382 2023-07-10 thomas if (hle->logmsg == NULL)
11792 97a02382 2023-07-10 thomas err = got_error_from_errno("strdup");
11795 97a02382 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
11796 97a02382 2023-07-10 thomas err = got_error_from_errno2("close", logmsg_path);
11797 97a02382 2023-07-10 thomas if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
11798 97a02382 2023-07-10 thomas err = got_error_from_errno2("unlink", logmsg_path);
11799 97a02382 2023-07-10 thomas free(logmsg_path);
11800 97a02382 2023-07-10 thomas free(logmsg);
11801 97a02382 2023-07-10 thomas free(orig_logmsg);
11802 97a02382 2023-07-10 thomas free(editor);
11803 97a02382 2023-07-10 thomas if (commit)
11804 97a02382 2023-07-10 thomas got_object_commit_close(commit);
11805 97a02382 2023-07-10 thomas return err;
11808 97a02382 2023-07-10 thomas static const struct got_error *
11809 97a02382 2023-07-10 thomas histedit_parse_list(struct got_histedit_list *histedit_cmds,
11810 97a02382 2023-07-10 thomas FILE *f, struct got_repository *repo)
11812 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
11813 97a02382 2023-07-10 thomas char *line = NULL, *p, *end;
11814 97a02382 2023-07-10 thomas size_t i, linesize = 0;
11815 97a02382 2023-07-10 thomas ssize_t linelen;
11816 97a02382 2023-07-10 thomas int lineno = 0, lastcmd = -1;
11817 97a02382 2023-07-10 thomas const struct got_histedit_cmd *cmd;
11818 97a02382 2023-07-10 thomas struct got_object_id *commit_id = NULL;
11819 97a02382 2023-07-10 thomas struct got_histedit_list_entry *hle = NULL;
11821 97a02382 2023-07-10 thomas for (;;) {
11822 97a02382 2023-07-10 thomas linelen = getline(&line, &linesize, f);
11823 97a02382 2023-07-10 thomas if (linelen == -1) {
11824 97a02382 2023-07-10 thomas const struct got_error *getline_err;
11825 97a02382 2023-07-10 thomas if (feof(f))
11827 97a02382 2023-07-10 thomas getline_err = got_error_from_errno("getline");
11828 97a02382 2023-07-10 thomas err = got_ferror(f, getline_err->code);
11831 97a02382 2023-07-10 thomas lineno++;
11832 97a02382 2023-07-10 thomas p = line;
11833 97a02382 2023-07-10 thomas while (isspace((unsigned char)p[0]))
11835 97a02382 2023-07-10 thomas if (p[0] == '#' || p[0] == '\0')
11836 97a02382 2023-07-10 thomas continue;
11837 97a02382 2023-07-10 thomas cmd = NULL;
11838 97a02382 2023-07-10 thomas for (i = 0; i < nitems(got_histedit_cmds); i++) {
11839 97a02382 2023-07-10 thomas cmd = &got_histedit_cmds[i];
11840 97a02382 2023-07-10 thomas if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
11841 97a02382 2023-07-10 thomas isspace((unsigned char)p[strlen(cmd->name)])) {
11842 97a02382 2023-07-10 thomas p += strlen(cmd->name);
11845 97a02382 2023-07-10 thomas if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
11850 97a02382 2023-07-10 thomas if (i == nitems(got_histedit_cmds)) {
11851 97a02382 2023-07-10 thomas err = histedit_syntax_error(lineno);
11854 97a02382 2023-07-10 thomas while (isspace((unsigned char)p[0]))
11856 97a02382 2023-07-10 thomas if (cmd->code == GOT_HISTEDIT_MESG) {
11857 97a02382 2023-07-10 thomas if (lastcmd != GOT_HISTEDIT_PICK &&
11858 97a02382 2023-07-10 thomas lastcmd != GOT_HISTEDIT_EDIT) {
11859 97a02382 2023-07-10 thomas err = got_error(GOT_ERR_HISTEDIT_CMD);
11862 97a02382 2023-07-10 thomas if (p[0] == '\0') {
11863 97a02382 2023-07-10 thomas err = histedit_edit_logmsg(hle, repo);
11864 97a02382 2023-07-10 thomas if (err)
11866 97a02382 2023-07-10 thomas } else {
11867 97a02382 2023-07-10 thomas hle->logmsg = strdup(p);
11868 97a02382 2023-07-10 thomas if (hle->logmsg == NULL) {
11869 97a02382 2023-07-10 thomas err = got_error_from_errno("strdup");
11873 97a02382 2023-07-10 thomas lastcmd = cmd->code;
11874 97a02382 2023-07-10 thomas continue;
11875 97a02382 2023-07-10 thomas } else {
11876 97a02382 2023-07-10 thomas end = p;
11877 97a02382 2023-07-10 thomas while (end[0] && !isspace((unsigned char)end[0]))
11879 97a02382 2023-07-10 thomas *end = '\0';
11881 97a02382 2023-07-10 thomas err = got_object_resolve_id_str(&commit_id, repo, p);
11882 97a02382 2023-07-10 thomas if (err) {
11883 97a02382 2023-07-10 thomas /* override error code */
11884 97a02382 2023-07-10 thomas err = histedit_syntax_error(lineno);
11888 97a02382 2023-07-10 thomas hle = malloc(sizeof(*hle));
11889 97a02382 2023-07-10 thomas if (hle == NULL) {
11890 97a02382 2023-07-10 thomas err = got_error_from_errno("malloc");
11893 97a02382 2023-07-10 thomas hle->cmd = cmd;
11894 97a02382 2023-07-10 thomas hle->commit_id = commit_id;
11895 97a02382 2023-07-10 thomas hle->logmsg = NULL;
11896 97a02382 2023-07-10 thomas commit_id = NULL;
11897 97a02382 2023-07-10 thomas TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
11898 97a02382 2023-07-10 thomas lastcmd = cmd->code;
11901 97a02382 2023-07-10 thomas free(line);
11902 97a02382 2023-07-10 thomas free(commit_id);
11903 97a02382 2023-07-10 thomas return err;
11906 97a02382 2023-07-10 thomas static const struct got_error *
11907 97a02382 2023-07-10 thomas histedit_check_script(struct got_histedit_list *histedit_cmds,
11908 97a02382 2023-07-10 thomas struct got_object_id_queue *commits, struct got_repository *repo)
11910 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
11911 97a02382 2023-07-10 thomas struct got_object_qid *qid;
11912 97a02382 2023-07-10 thomas struct got_histedit_list_entry *hle;
11913 97a02382 2023-07-10 thomas static char msg[92];
11914 97a02382 2023-07-10 thomas char *id_str;
11916 97a02382 2023-07-10 thomas if (TAILQ_EMPTY(histedit_cmds))
11917 97a02382 2023-07-10 thomas return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
11918 97a02382 2023-07-10 thomas "histedit script contains no commands");
11919 97a02382 2023-07-10 thomas if (STAILQ_EMPTY(commits))
11920 97a02382 2023-07-10 thomas return got_error(GOT_ERR_EMPTY_HISTEDIT);
11922 97a02382 2023-07-10 thomas TAILQ_FOREACH(hle, histedit_cmds, entry) {
11923 97a02382 2023-07-10 thomas struct got_histedit_list_entry *hle2;
11924 97a02382 2023-07-10 thomas TAILQ_FOREACH(hle2, histedit_cmds, entry) {
11925 97a02382 2023-07-10 thomas if (hle == hle2)
11926 97a02382 2023-07-10 thomas continue;
11927 97a02382 2023-07-10 thomas if (got_object_id_cmp(hle->commit_id,
11928 97a02382 2023-07-10 thomas hle2->commit_id) != 0)
11929 97a02382 2023-07-10 thomas continue;
11930 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, hle->commit_id);
11931 97a02382 2023-07-10 thomas if (err)
11932 97a02382 2023-07-10 thomas return err;
11933 97a02382 2023-07-10 thomas snprintf(msg, sizeof(msg), "commit %s is listed "
11934 97a02382 2023-07-10 thomas "more than once in histedit script", id_str);
11935 97a02382 2023-07-10 thomas free(id_str);
11936 97a02382 2023-07-10 thomas return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11940 97a02382 2023-07-10 thomas STAILQ_FOREACH(qid, commits, entry) {
11941 97a02382 2023-07-10 thomas TAILQ_FOREACH(hle, histedit_cmds, entry) {
11942 97a02382 2023-07-10 thomas if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
11945 97a02382 2023-07-10 thomas if (hle == NULL) {
11946 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, &qid->id);
11947 97a02382 2023-07-10 thomas if (err)
11948 97a02382 2023-07-10 thomas return err;
11949 97a02382 2023-07-10 thomas snprintf(msg, sizeof(msg),
11950 97a02382 2023-07-10 thomas "commit %s missing from histedit script", id_str);
11951 97a02382 2023-07-10 thomas free(id_str);
11952 97a02382 2023-07-10 thomas return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11956 97a02382 2023-07-10 thomas hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
11957 97a02382 2023-07-10 thomas if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
11958 97a02382 2023-07-10 thomas return got_error_msg(GOT_ERR_HISTEDIT_CMD,
11959 97a02382 2023-07-10 thomas "last commit in histedit script cannot be folded");
11961 97a02382 2023-07-10 thomas return NULL;
11964 97a02382 2023-07-10 thomas static const struct got_error *
11965 97a02382 2023-07-10 thomas histedit_run_editor(struct got_histedit_list *histedit_cmds,
11966 97a02382 2023-07-10 thomas const char *path, struct got_object_id_queue *commits,
11967 97a02382 2023-07-10 thomas struct got_repository *repo)
11969 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
11970 97a02382 2023-07-10 thomas char *editor;
11971 97a02382 2023-07-10 thomas FILE *f = NULL;
11973 97a02382 2023-07-10 thomas err = get_editor(&editor);
11974 97a02382 2023-07-10 thomas if (err)
11975 97a02382 2023-07-10 thomas return err;
11977 97a02382 2023-07-10 thomas if (spawn_editor(editor, path) == -1) {
11978 97a02382 2023-07-10 thomas err = got_error_from_errno("failed spawning editor");
11979 97a02382 2023-07-10 thomas goto done;
11982 97a02382 2023-07-10 thomas f = fopen(path, "re");
11983 97a02382 2023-07-10 thomas if (f == NULL) {
11984 97a02382 2023-07-10 thomas err = got_error_from_errno("fopen");
11985 97a02382 2023-07-10 thomas goto done;
11987 97a02382 2023-07-10 thomas err = histedit_parse_list(histedit_cmds, f, repo);
11988 97a02382 2023-07-10 thomas if (err)
11989 97a02382 2023-07-10 thomas goto done;
11991 97a02382 2023-07-10 thomas err = histedit_check_script(histedit_cmds, commits, repo);
11993 97a02382 2023-07-10 thomas if (f && fclose(f) == EOF && err == NULL)
11994 97a02382 2023-07-10 thomas err = got_error_from_errno("fclose");
11995 97a02382 2023-07-10 thomas free(editor);
11996 97a02382 2023-07-10 thomas return err;
11999 97a02382 2023-07-10 thomas static const struct got_error *
12000 97a02382 2023-07-10 thomas histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
12001 97a02382 2023-07-10 thomas struct got_object_id_queue *, const char *, const char *,
12002 97a02382 2023-07-10 thomas struct got_repository *);
12004 97a02382 2023-07-10 thomas static const struct got_error *
12005 97a02382 2023-07-10 thomas histedit_edit_script(struct got_histedit_list *histedit_cmds,
12006 97a02382 2023-07-10 thomas struct got_object_id_queue *commits, const char *branch_name,
12007 97a02382 2023-07-10 thomas int edit_logmsg_only, int fold_only, int drop_only, int edit_only,
12008 97a02382 2023-07-10 thomas struct got_repository *repo)
12010 97a02382 2023-07-10 thomas const struct got_error *err;
12011 97a02382 2023-07-10 thomas FILE *f = NULL;
12012 97a02382 2023-07-10 thomas char *path = NULL;
12014 97a02382 2023-07-10 thomas err = got_opentemp_named(&path, &f, "got-histedit", "");
12015 97a02382 2023-07-10 thomas if (err)
12016 97a02382 2023-07-10 thomas return err;
12018 97a02382 2023-07-10 thomas err = write_cmd_list(f, branch_name, commits);
12019 97a02382 2023-07-10 thomas if (err)
12020 97a02382 2023-07-10 thomas goto done;
12022 97a02382 2023-07-10 thomas err = histedit_write_commit_list(commits, f, edit_logmsg_only,
12023 97a02382 2023-07-10 thomas fold_only, drop_only, edit_only, repo);
12024 97a02382 2023-07-10 thomas if (err)
12025 97a02382 2023-07-10 thomas goto done;
12027 97a02382 2023-07-10 thomas if (drop_only || edit_logmsg_only || fold_only || edit_only) {
12028 97a02382 2023-07-10 thomas rewind(f);
12029 97a02382 2023-07-10 thomas err = histedit_parse_list(histedit_cmds, f, repo);
12030 97a02382 2023-07-10 thomas } else {
12031 97a02382 2023-07-10 thomas if (fclose(f) == EOF) {
12032 97a02382 2023-07-10 thomas err = got_error_from_errno("fclose");
12033 97a02382 2023-07-10 thomas goto done;
12035 97a02382 2023-07-10 thomas f = NULL;
12036 97a02382 2023-07-10 thomas err = histedit_run_editor(histedit_cmds, path, commits, repo);
12037 97a02382 2023-07-10 thomas if (err) {
12038 97a02382 2023-07-10 thomas if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12039 97a02382 2023-07-10 thomas err->code != GOT_ERR_HISTEDIT_CMD)
12040 97a02382 2023-07-10 thomas goto done;
12041 97a02382 2023-07-10 thomas err = histedit_edit_list_retry(histedit_cmds, err,
12042 97a02382 2023-07-10 thomas commits, path, branch_name, repo);
12046 97a02382 2023-07-10 thomas if (f && fclose(f) == EOF && err == NULL)
12047 97a02382 2023-07-10 thomas err = got_error_from_errno("fclose");
12048 97a02382 2023-07-10 thomas if (path && unlink(path) != 0 && err == NULL)
12049 97a02382 2023-07-10 thomas err = got_error_from_errno2("unlink", path);
12050 97a02382 2023-07-10 thomas free(path);
12051 97a02382 2023-07-10 thomas return err;
12054 97a02382 2023-07-10 thomas static const struct got_error *
12055 97a02382 2023-07-10 thomas histedit_save_list(struct got_histedit_list *histedit_cmds,
12056 97a02382 2023-07-10 thomas struct got_worktree *worktree, struct got_repository *repo)
12058 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
12059 97a02382 2023-07-10 thomas char *path = NULL;
12060 97a02382 2023-07-10 thomas FILE *f = NULL;
12061 97a02382 2023-07-10 thomas struct got_histedit_list_entry *hle;
12062 97a02382 2023-07-10 thomas struct got_commit_object *commit = NULL;
12064 97a02382 2023-07-10 thomas err = got_worktree_get_histedit_script_path(&path, worktree);
12065 97a02382 2023-07-10 thomas if (err)
12066 97a02382 2023-07-10 thomas return err;
12068 97a02382 2023-07-10 thomas f = fopen(path, "we");
12069 97a02382 2023-07-10 thomas if (f == NULL) {
12070 97a02382 2023-07-10 thomas err = got_error_from_errno2("fopen", path);
12071 97a02382 2023-07-10 thomas goto done;
12073 97a02382 2023-07-10 thomas TAILQ_FOREACH(hle, histedit_cmds, entry) {
12074 97a02382 2023-07-10 thomas err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
12076 97a02382 2023-07-10 thomas if (err)
12079 97a02382 2023-07-10 thomas if (hle->logmsg) {
12080 97a02382 2023-07-10 thomas int n = fprintf(f, "%c %s\n",
12081 97a02382 2023-07-10 thomas GOT_HISTEDIT_MESG, hle->logmsg);
12082 97a02382 2023-07-10 thomas if (n < 0) {
12083 97a02382 2023-07-10 thomas err = got_ferror(f, GOT_ERR_IO);
12089 97a02382 2023-07-10 thomas if (f && fclose(f) == EOF && err == NULL)
12090 97a02382 2023-07-10 thomas err = got_error_from_errno("fclose");
12091 97a02382 2023-07-10 thomas free(path);
12092 97a02382 2023-07-10 thomas if (commit)
12093 97a02382 2023-07-10 thomas got_object_commit_close(commit);
12094 97a02382 2023-07-10 thomas return err;
12097 97a02382 2023-07-10 thomas static void
12098 97a02382 2023-07-10 thomas histedit_free_list(struct got_histedit_list *histedit_cmds)
12100 97a02382 2023-07-10 thomas struct got_histedit_list_entry *hle;
12102 97a02382 2023-07-10 thomas while ((hle = TAILQ_FIRST(histedit_cmds))) {
12103 97a02382 2023-07-10 thomas TAILQ_REMOVE(histedit_cmds, hle, entry);
12104 97a02382 2023-07-10 thomas free(hle);
12108 97a02382 2023-07-10 thomas static const struct got_error *
12109 97a02382 2023-07-10 thomas histedit_load_list(struct got_histedit_list *histedit_cmds,
12110 97a02382 2023-07-10 thomas const char *path, struct got_repository *repo)
12112 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
12113 97a02382 2023-07-10 thomas FILE *f = NULL;
12115 97a02382 2023-07-10 thomas f = fopen(path, "re");
12116 97a02382 2023-07-10 thomas if (f == NULL) {
12117 97a02382 2023-07-10 thomas err = got_error_from_errno2("fopen", path);
12118 97a02382 2023-07-10 thomas goto done;
12121 97a02382 2023-07-10 thomas err = histedit_parse_list(histedit_cmds, f, repo);
12123 97a02382 2023-07-10 thomas if (f && fclose(f) == EOF && err == NULL)
12124 97a02382 2023-07-10 thomas err = got_error_from_errno("fclose");
12125 97a02382 2023-07-10 thomas return err;
12128 97a02382 2023-07-10 thomas static const struct got_error *
12129 97a02382 2023-07-10 thomas histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
12130 97a02382 2023-07-10 thomas const struct got_error *edit_err, struct got_object_id_queue *commits,
12131 97a02382 2023-07-10 thomas const char *path, const char *branch_name, struct got_repository *repo)
12133 97a02382 2023-07-10 thomas const struct got_error *err = NULL, *prev_err = edit_err;
12134 97a02382 2023-07-10 thomas int resp = ' ';
12136 97a02382 2023-07-10 thomas while (resp != 'c' && resp != 'r' && resp != 'a') {
12137 97a02382 2023-07-10 thomas printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
12138 97a02382 2023-07-10 thomas "or (a)bort: ", getprogname(), prev_err->msg);
12139 97a02382 2023-07-10 thomas resp = getchar();
12140 97a02382 2023-07-10 thomas if (resp == '\n')
12141 97a02382 2023-07-10 thomas resp = getchar();
12142 97a02382 2023-07-10 thomas if (resp == 'c') {
12143 97a02382 2023-07-10 thomas histedit_free_list(histedit_cmds);
12144 97a02382 2023-07-10 thomas err = histedit_run_editor(histedit_cmds, path, commits,
12146 97a02382 2023-07-10 thomas if (err) {
12147 97a02382 2023-07-10 thomas if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12148 97a02382 2023-07-10 thomas err->code != GOT_ERR_HISTEDIT_CMD)
12150 97a02382 2023-07-10 thomas prev_err = err;
12151 97a02382 2023-07-10 thomas resp = ' ';
12152 97a02382 2023-07-10 thomas continue;
12155 97a02382 2023-07-10 thomas } else if (resp == 'r') {
12156 97a02382 2023-07-10 thomas histedit_free_list(histedit_cmds);
12157 97a02382 2023-07-10 thomas err = histedit_edit_script(histedit_cmds,
12158 97a02382 2023-07-10 thomas commits, branch_name, 0, 0, 0, 0, repo);
12159 97a02382 2023-07-10 thomas if (err) {
12160 97a02382 2023-07-10 thomas if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12161 97a02382 2023-07-10 thomas err->code != GOT_ERR_HISTEDIT_CMD)
12163 97a02382 2023-07-10 thomas prev_err = err;
12164 97a02382 2023-07-10 thomas resp = ' ';
12165 97a02382 2023-07-10 thomas continue;
12168 97a02382 2023-07-10 thomas } else if (resp == 'a') {
12169 97a02382 2023-07-10 thomas err = got_error(GOT_ERR_HISTEDIT_CANCEL);
12172 97a02382 2023-07-10 thomas printf("invalid response '%c'\n", resp);
12175 97a02382 2023-07-10 thomas return err;
12178 97a02382 2023-07-10 thomas static const struct got_error *
12179 97a02382 2023-07-10 thomas histedit_complete(struct got_worktree *worktree,
12180 97a02382 2023-07-10 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
12181 97a02382 2023-07-10 thomas struct got_reference *branch, struct got_repository *repo)
12183 97a02382 2023-07-10 thomas printf("Switching work tree to %s\n",
12184 97a02382 2023-07-10 thomas got_ref_get_symref_target(branch));
12185 97a02382 2023-07-10 thomas return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
12186 97a02382 2023-07-10 thomas branch, repo);
12189 97a02382 2023-07-10 thomas static const struct got_error *
12190 97a02382 2023-07-10 thomas show_histedit_progress(struct got_commit_object *commit,
12191 97a02382 2023-07-10 thomas struct got_histedit_list_entry *hle, struct got_object_id *new_id)
12193 97a02382 2023-07-10 thomas const struct got_error *err;
12194 97a02382 2023-07-10 thomas char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
12196 97a02382 2023-07-10 thomas err = got_object_id_str(&old_id_str, hle->commit_id);
12197 97a02382 2023-07-10 thomas if (err)
12198 97a02382 2023-07-10 thomas goto done;
12200 97a02382 2023-07-10 thomas if (new_id) {
12201 97a02382 2023-07-10 thomas err = got_object_id_str(&new_id_str, new_id);
12202 97a02382 2023-07-10 thomas if (err)
12203 97a02382 2023-07-10 thomas goto done;
12206 97a02382 2023-07-10 thomas old_id_str[12] = '\0';
12207 97a02382 2023-07-10 thomas if (new_id_str)
12208 97a02382 2023-07-10 thomas new_id_str[12] = '\0';
12210 97a02382 2023-07-10 thomas if (hle->logmsg) {
12211 97a02382 2023-07-10 thomas logmsg = strdup(hle->logmsg);
12212 97a02382 2023-07-10 thomas if (logmsg == NULL) {
12213 97a02382 2023-07-10 thomas err = got_error_from_errno("strdup");
12214 97a02382 2023-07-10 thomas goto done;
12216 97a02382 2023-07-10 thomas trim_logmsg(logmsg, 42);
12217 97a02382 2023-07-10 thomas } else {
12218 97a02382 2023-07-10 thomas err = get_short_logmsg(&logmsg, 42, commit);
12219 97a02382 2023-07-10 thomas if (err)
12220 97a02382 2023-07-10 thomas goto done;
12223 97a02382 2023-07-10 thomas switch (hle->cmd->code) {
12224 97a02382 2023-07-10 thomas case GOT_HISTEDIT_PICK:
12225 97a02382 2023-07-10 thomas case GOT_HISTEDIT_EDIT:
12226 97a02382 2023-07-10 thomas printf("%s -> %s: %s\n", old_id_str,
12227 97a02382 2023-07-10 thomas new_id_str ? new_id_str : "no-op change", logmsg);
12229 97a02382 2023-07-10 thomas case GOT_HISTEDIT_DROP:
12230 97a02382 2023-07-10 thomas case GOT_HISTEDIT_FOLD:
12231 97a02382 2023-07-10 thomas printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
12232 97a02382 2023-07-10 thomas logmsg);
12234 97a02382 2023-07-10 thomas default:
12238 97a02382 2023-07-10 thomas free(old_id_str);
12239 97a02382 2023-07-10 thomas free(new_id_str);
12240 97a02382 2023-07-10 thomas return err;
12243 97a02382 2023-07-10 thomas static const struct got_error *
12244 97a02382 2023-07-10 thomas histedit_commit(struct got_pathlist_head *merged_paths,
12245 97a02382 2023-07-10 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
12246 97a02382 2023-07-10 thomas struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
12247 97a02382 2023-07-10 thomas const char *committer, int allow_conflict, struct got_repository *repo)
12249 97a02382 2023-07-10 thomas const struct got_error *err;
12250 97a02382 2023-07-10 thomas struct got_commit_object *commit;
12251 97a02382 2023-07-10 thomas struct got_object_id *new_commit_id;
12253 97a02382 2023-07-10 thomas if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
12254 97a02382 2023-07-10 thomas && hle->logmsg == NULL) {
12255 97a02382 2023-07-10 thomas err = histedit_edit_logmsg(hle, repo);
12256 97a02382 2023-07-10 thomas if (err)
12257 97a02382 2023-07-10 thomas return err;
12260 97a02382 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, hle->commit_id);
12261 97a02382 2023-07-10 thomas if (err)
12262 97a02382 2023-07-10 thomas return err;
12264 97a02382 2023-07-10 thomas err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
12265 97a02382 2023-07-10 thomas worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
12266 97a02382 2023-07-10 thomas hle->logmsg, allow_conflict, repo);
12267 97a02382 2023-07-10 thomas if (err) {
12268 97a02382 2023-07-10 thomas if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
12269 97a02382 2023-07-10 thomas goto done;
12270 97a02382 2023-07-10 thomas err = show_histedit_progress(commit, hle, NULL);
12271 97a02382 2023-07-10 thomas } else {
12272 97a02382 2023-07-10 thomas err = show_histedit_progress(commit, hle, new_commit_id);
12273 97a02382 2023-07-10 thomas free(new_commit_id);
12276 97a02382 2023-07-10 thomas got_object_commit_close(commit);
12277 97a02382 2023-07-10 thomas return err;
12280 97a02382 2023-07-10 thomas static const struct got_error *
12281 97a02382 2023-07-10 thomas histedit_skip_commit(struct got_histedit_list_entry *hle,
12282 97a02382 2023-07-10 thomas struct got_worktree *worktree, struct got_repository *repo)
12284 97a02382 2023-07-10 thomas const struct got_error *error;
12285 97a02382 2023-07-10 thomas struct got_commit_object *commit;
12287 97a02382 2023-07-10 thomas error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
12289 97a02382 2023-07-10 thomas if (error)
12290 97a02382 2023-07-10 thomas return error;
12292 97a02382 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, hle->commit_id);
12293 97a02382 2023-07-10 thomas if (error)
12294 97a02382 2023-07-10 thomas return error;
12296 97a02382 2023-07-10 thomas error = show_histedit_progress(commit, hle, NULL);
12297 97a02382 2023-07-10 thomas got_object_commit_close(commit);
12298 97a02382 2023-07-10 thomas return error;
12301 97a02382 2023-07-10 thomas static const struct got_error *
12302 97a02382 2023-07-10 thomas check_local_changes(void *arg, unsigned char status,
12303 97a02382 2023-07-10 thomas unsigned char staged_status, const char *path,
12304 97a02382 2023-07-10 thomas struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12305 97a02382 2023-07-10 thomas struct got_object_id *commit_id, int dirfd, const char *de_name)
12307 97a02382 2023-07-10 thomas int *have_local_changes = arg;
12309 97a02382 2023-07-10 thomas switch (status) {
12310 97a02382 2023-07-10 thomas case GOT_STATUS_ADD:
12311 97a02382 2023-07-10 thomas case GOT_STATUS_DELETE:
12312 97a02382 2023-07-10 thomas case GOT_STATUS_MODIFY:
12313 97a02382 2023-07-10 thomas case GOT_STATUS_CONFLICT:
12314 97a02382 2023-07-10 thomas *have_local_changes = 1;
12315 97a02382 2023-07-10 thomas return got_error(GOT_ERR_CANCELLED);
12316 97a02382 2023-07-10 thomas default:
12320 97a02382 2023-07-10 thomas switch (staged_status) {
12321 97a02382 2023-07-10 thomas case GOT_STATUS_ADD:
12322 97a02382 2023-07-10 thomas case GOT_STATUS_DELETE:
12323 97a02382 2023-07-10 thomas case GOT_STATUS_MODIFY:
12324 97a02382 2023-07-10 thomas *have_local_changes = 1;
12325 97a02382 2023-07-10 thomas return got_error(GOT_ERR_CANCELLED);
12326 97a02382 2023-07-10 thomas default:
12330 97a02382 2023-07-10 thomas return NULL;
12333 97a02382 2023-07-10 thomas static const struct got_error *
12334 97a02382 2023-07-10 thomas cmd_histedit(int argc, char *argv[])
12336 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
12337 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
12338 97a02382 2023-07-10 thomas struct got_fileindex *fileindex = NULL;
12339 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
12340 97a02382 2023-07-10 thomas char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
12341 97a02382 2023-07-10 thomas struct got_reference *branch = NULL;
12342 97a02382 2023-07-10 thomas struct got_reference *tmp_branch = NULL;
12343 97a02382 2023-07-10 thomas struct got_object_id *resume_commit_id = NULL;
12344 97a02382 2023-07-10 thomas struct got_object_id *base_commit_id = NULL;
12345 97a02382 2023-07-10 thomas struct got_object_id *head_commit_id = NULL;
12346 97a02382 2023-07-10 thomas struct got_commit_object *commit = NULL;
12347 97a02382 2023-07-10 thomas int ch, rebase_in_progress = 0, merge_in_progress = 0;
12348 97a02382 2023-07-10 thomas struct got_update_progress_arg upa;
12349 97a02382 2023-07-10 thomas int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
12350 97a02382 2023-07-10 thomas int drop_only = 0, edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
12351 97a02382 2023-07-10 thomas int allow_conflict = 0, list_backups = 0, delete_backups = 0;
12352 97a02382 2023-07-10 thomas const char *edit_script_path = NULL;
12353 97a02382 2023-07-10 thomas struct got_object_id_queue commits;
12354 97a02382 2023-07-10 thomas struct got_pathlist_head merged_paths;
12355 97a02382 2023-07-10 thomas const struct got_object_id_queue *parent_ids;
12356 97a02382 2023-07-10 thomas struct got_object_qid *pid;
12357 97a02382 2023-07-10 thomas struct got_histedit_list histedit_cmds;
12358 97a02382 2023-07-10 thomas struct got_histedit_list_entry *hle;
12359 97a02382 2023-07-10 thomas int *pack_fds = NULL;
12361 97a02382 2023-07-10 thomas STAILQ_INIT(&commits);
12362 97a02382 2023-07-10 thomas TAILQ_INIT(&histedit_cmds);
12363 97a02382 2023-07-10 thomas TAILQ_INIT(&merged_paths);
12364 97a02382 2023-07-10 thomas memset(&upa, 0, sizeof(upa));
12366 97a02382 2023-07-10 thomas #ifndef PROFILE
12367 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12368 97a02382 2023-07-10 thomas "unveil", NULL) == -1)
12369 97a02382 2023-07-10 thomas err(1, "pledge");
12372 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "aCcdeF:flmX")) != -1) {
12373 97a02382 2023-07-10 thomas switch (ch) {
12374 97a02382 2023-07-10 thomas case 'a':
12375 97a02382 2023-07-10 thomas abort_edit = 1;
12377 97a02382 2023-07-10 thomas case 'C':
12378 97a02382 2023-07-10 thomas allow_conflict = 1;
12380 97a02382 2023-07-10 thomas case 'c':
12381 97a02382 2023-07-10 thomas continue_edit = 1;
12383 97a02382 2023-07-10 thomas case 'd':
12384 97a02382 2023-07-10 thomas drop_only = 1;
12386 97a02382 2023-07-10 thomas case 'e':
12387 97a02382 2023-07-10 thomas edit_only = 1;
12389 97a02382 2023-07-10 thomas case 'F':
12390 97a02382 2023-07-10 thomas edit_script_path = optarg;
12392 97a02382 2023-07-10 thomas case 'f':
12393 97a02382 2023-07-10 thomas fold_only = 1;
12395 97a02382 2023-07-10 thomas case 'l':
12396 97a02382 2023-07-10 thomas list_backups = 1;
12398 97a02382 2023-07-10 thomas case 'm':
12399 97a02382 2023-07-10 thomas edit_logmsg_only = 1;
12401 97a02382 2023-07-10 thomas case 'X':
12402 97a02382 2023-07-10 thomas delete_backups = 1;
12404 97a02382 2023-07-10 thomas default:
12405 97a02382 2023-07-10 thomas usage_histedit();
12406 97a02382 2023-07-10 thomas /* NOTREACHED */
12410 97a02382 2023-07-10 thomas argc -= optind;
12411 97a02382 2023-07-10 thomas argv += optind;
12413 97a02382 2023-07-10 thomas if (abort_edit && allow_conflict)
12414 97a02382 2023-07-10 thomas option_conflict('a', 'C');
12415 97a02382 2023-07-10 thomas if (abort_edit && continue_edit)
12416 97a02382 2023-07-10 thomas option_conflict('a', 'c');
12417 97a02382 2023-07-10 thomas if (edit_script_path && allow_conflict)
12418 97a02382 2023-07-10 thomas option_conflict('F', 'C');
12419 97a02382 2023-07-10 thomas if (edit_script_path && edit_logmsg_only)
12420 97a02382 2023-07-10 thomas option_conflict('F', 'm');
12421 97a02382 2023-07-10 thomas if (abort_edit && edit_logmsg_only)
12422 97a02382 2023-07-10 thomas option_conflict('a', 'm');
12423 97a02382 2023-07-10 thomas if (edit_logmsg_only && allow_conflict)
12424 97a02382 2023-07-10 thomas option_conflict('m', 'C');
12425 97a02382 2023-07-10 thomas if (continue_edit && edit_logmsg_only)
12426 97a02382 2023-07-10 thomas option_conflict('c', 'm');
12427 97a02382 2023-07-10 thomas if (abort_edit && fold_only)
12428 97a02382 2023-07-10 thomas option_conflict('a', 'f');
12429 97a02382 2023-07-10 thomas if (fold_only && allow_conflict)
12430 97a02382 2023-07-10 thomas option_conflict('f', 'C');
12431 97a02382 2023-07-10 thomas if (continue_edit && fold_only)
12432 97a02382 2023-07-10 thomas option_conflict('c', 'f');
12433 97a02382 2023-07-10 thomas if (fold_only && edit_logmsg_only)
12434 97a02382 2023-07-10 thomas option_conflict('f', 'm');
12435 97a02382 2023-07-10 thomas if (edit_script_path && fold_only)
12436 97a02382 2023-07-10 thomas option_conflict('F', 'f');
12437 97a02382 2023-07-10 thomas if (abort_edit && edit_only)
12438 97a02382 2023-07-10 thomas option_conflict('a', 'e');
12439 97a02382 2023-07-10 thomas if (continue_edit && edit_only)
12440 97a02382 2023-07-10 thomas option_conflict('c', 'e');
12441 97a02382 2023-07-10 thomas if (edit_only && edit_logmsg_only)
12442 97a02382 2023-07-10 thomas option_conflict('e', 'm');
12443 97a02382 2023-07-10 thomas if (edit_script_path && edit_only)
12444 97a02382 2023-07-10 thomas option_conflict('F', 'e');
12445 97a02382 2023-07-10 thomas if (fold_only && edit_only)
12446 97a02382 2023-07-10 thomas option_conflict('f', 'e');
12447 97a02382 2023-07-10 thomas if (drop_only && abort_edit)
12448 97a02382 2023-07-10 thomas option_conflict('d', 'a');
12449 97a02382 2023-07-10 thomas if (drop_only && allow_conflict)
12450 97a02382 2023-07-10 thomas option_conflict('d', 'C');
12451 97a02382 2023-07-10 thomas if (drop_only && continue_edit)
12452 97a02382 2023-07-10 thomas option_conflict('d', 'c');
12453 97a02382 2023-07-10 thomas if (drop_only && edit_logmsg_only)
12454 97a02382 2023-07-10 thomas option_conflict('d', 'm');
12455 97a02382 2023-07-10 thomas if (drop_only && edit_only)
12456 97a02382 2023-07-10 thomas option_conflict('d', 'e');
12457 97a02382 2023-07-10 thomas if (drop_only && edit_script_path)
12458 97a02382 2023-07-10 thomas option_conflict('d', 'F');
12459 97a02382 2023-07-10 thomas if (drop_only && fold_only)
12460 97a02382 2023-07-10 thomas option_conflict('d', 'f');
12461 97a02382 2023-07-10 thomas if (list_backups) {
12462 97a02382 2023-07-10 thomas if (abort_edit)
12463 97a02382 2023-07-10 thomas option_conflict('l', 'a');
12464 97a02382 2023-07-10 thomas if (allow_conflict)
12465 97a02382 2023-07-10 thomas option_conflict('l', 'C');
12466 97a02382 2023-07-10 thomas if (continue_edit)
12467 97a02382 2023-07-10 thomas option_conflict('l', 'c');
12468 97a02382 2023-07-10 thomas if (edit_script_path)
12469 97a02382 2023-07-10 thomas option_conflict('l', 'F');
12470 97a02382 2023-07-10 thomas if (edit_logmsg_only)
12471 97a02382 2023-07-10 thomas option_conflict('l', 'm');
12472 97a02382 2023-07-10 thomas if (drop_only)
12473 97a02382 2023-07-10 thomas option_conflict('l', 'd');
12474 97a02382 2023-07-10 thomas if (fold_only)
12475 97a02382 2023-07-10 thomas option_conflict('l', 'f');
12476 97a02382 2023-07-10 thomas if (edit_only)
12477 97a02382 2023-07-10 thomas option_conflict('l', 'e');
12478 97a02382 2023-07-10 thomas if (delete_backups)
12479 97a02382 2023-07-10 thomas option_conflict('l', 'X');
12480 97a02382 2023-07-10 thomas if (argc != 0 && argc != 1)
12481 97a02382 2023-07-10 thomas usage_histedit();
12482 97a02382 2023-07-10 thomas } else if (delete_backups) {
12483 97a02382 2023-07-10 thomas if (abort_edit)
12484 97a02382 2023-07-10 thomas option_conflict('X', 'a');
12485 97a02382 2023-07-10 thomas if (allow_conflict)
12486 97a02382 2023-07-10 thomas option_conflict('X', 'C');
12487 97a02382 2023-07-10 thomas if (continue_edit)
12488 97a02382 2023-07-10 thomas option_conflict('X', 'c');
12489 97a02382 2023-07-10 thomas if (drop_only)
12490 97a02382 2023-07-10 thomas option_conflict('X', 'd');
12491 97a02382 2023-07-10 thomas if (edit_script_path)
12492 97a02382 2023-07-10 thomas option_conflict('X', 'F');
12493 97a02382 2023-07-10 thomas if (edit_logmsg_only)
12494 97a02382 2023-07-10 thomas option_conflict('X', 'm');
12495 97a02382 2023-07-10 thomas if (fold_only)
12496 97a02382 2023-07-10 thomas option_conflict('X', 'f');
12497 97a02382 2023-07-10 thomas if (edit_only)
12498 97a02382 2023-07-10 thomas option_conflict('X', 'e');
12499 97a02382 2023-07-10 thomas if (list_backups)
12500 97a02382 2023-07-10 thomas option_conflict('X', 'l');
12501 97a02382 2023-07-10 thomas if (argc != 0 && argc != 1)
12502 97a02382 2023-07-10 thomas usage_histedit();
12503 97a02382 2023-07-10 thomas } else if (allow_conflict && !continue_edit)
12504 97a02382 2023-07-10 thomas errx(1, "-C option requires -c");
12505 97a02382 2023-07-10 thomas else if (argc != 0)
12506 97a02382 2023-07-10 thomas usage_histedit();
12509 97a02382 2023-07-10 thomas * This command cannot apply unveil(2) in all cases because the
12510 97a02382 2023-07-10 thomas * user may choose to run an editor to edit the histedit script
12511 97a02382 2023-07-10 thomas * and to edit individual commit log messages.
12512 97a02382 2023-07-10 thomas * unveil(2) traverses exec(2); if an editor is used we have to
12513 97a02382 2023-07-10 thomas * apply unveil after edit script and log messages have been written.
12514 97a02382 2023-07-10 thomas * XXX TODO: Make use of unveil(2) where possible.
12517 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
12518 97a02382 2023-07-10 thomas if (cwd == NULL) {
12519 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
12520 97a02382 2023-07-10 thomas goto done;
12523 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
12524 97a02382 2023-07-10 thomas if (error != NULL)
12525 97a02382 2023-07-10 thomas goto done;
12527 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
12528 97a02382 2023-07-10 thomas if (error) {
12529 97a02382 2023-07-10 thomas if (list_backups || delete_backups) {
12530 97a02382 2023-07-10 thomas if (error->code != GOT_ERR_NOT_WORKTREE)
12531 97a02382 2023-07-10 thomas goto done;
12532 97a02382 2023-07-10 thomas } else {
12533 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
12534 97a02382 2023-07-10 thomas error = wrap_not_worktree_error(error,
12535 97a02382 2023-07-10 thomas "histedit", cwd);
12536 97a02382 2023-07-10 thomas goto done;
12540 97a02382 2023-07-10 thomas if (list_backups || delete_backups) {
12541 97a02382 2023-07-10 thomas error = got_repo_open(&repo,
12542 97a02382 2023-07-10 thomas worktree ? got_worktree_get_repo_path(worktree) : cwd,
12543 97a02382 2023-07-10 thomas NULL, pack_fds);
12544 97a02382 2023-07-10 thomas if (error != NULL)
12545 97a02382 2023-07-10 thomas goto done;
12546 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
12547 97a02382 2023-07-10 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
12548 97a02382 2023-07-10 thomas if (error)
12549 97a02382 2023-07-10 thomas goto done;
12550 97a02382 2023-07-10 thomas error = process_backup_refs(
12551 97a02382 2023-07-10 thomas GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
12552 97a02382 2023-07-10 thomas argc == 1 ? argv[0] : NULL, delete_backups, repo);
12553 97a02382 2023-07-10 thomas goto done; /* nothing else to do */
12556 97a02382 2023-07-10 thomas error = get_gitconfig_path(&gitconfig_path);
12557 97a02382 2023-07-10 thomas if (error)
12558 97a02382 2023-07-10 thomas goto done;
12559 97a02382 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12560 97a02382 2023-07-10 thomas gitconfig_path, pack_fds);
12561 97a02382 2023-07-10 thomas if (error != NULL)
12562 97a02382 2023-07-10 thomas goto done;
12564 97a02382 2023-07-10 thomas if (worktree != NULL && !list_backups && !delete_backups) {
12565 97a02382 2023-07-10 thomas error = worktree_has_logmsg_ref("histedit", worktree, repo);
12566 97a02382 2023-07-10 thomas if (error)
12567 97a02382 2023-07-10 thomas goto done;
12570 97a02382 2023-07-10 thomas error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
12571 97a02382 2023-07-10 thomas if (error)
12572 97a02382 2023-07-10 thomas goto done;
12573 97a02382 2023-07-10 thomas if (rebase_in_progress) {
12574 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_REBASING);
12575 97a02382 2023-07-10 thomas goto done;
12578 97a02382 2023-07-10 thomas error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12580 97a02382 2023-07-10 thomas if (error)
12581 97a02382 2023-07-10 thomas goto done;
12582 97a02382 2023-07-10 thomas if (merge_in_progress) {
12583 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_MERGE_BUSY);
12584 97a02382 2023-07-10 thomas goto done;
12587 97a02382 2023-07-10 thomas error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
12588 97a02382 2023-07-10 thomas if (error)
12589 97a02382 2023-07-10 thomas goto done;
12591 97a02382 2023-07-10 thomas if (edit_in_progress && edit_logmsg_only) {
12592 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12593 97a02382 2023-07-10 thomas "histedit operation is in progress in this "
12594 97a02382 2023-07-10 thomas "work tree and must be continued or aborted "
12595 97a02382 2023-07-10 thomas "before the -m option can be used");
12596 97a02382 2023-07-10 thomas goto done;
12598 97a02382 2023-07-10 thomas if (edit_in_progress && drop_only) {
12599 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12600 97a02382 2023-07-10 thomas "histedit operation is in progress in this "
12601 97a02382 2023-07-10 thomas "work tree and must be continued or aborted "
12602 97a02382 2023-07-10 thomas "before the -d option can be used");
12603 97a02382 2023-07-10 thomas goto done;
12605 97a02382 2023-07-10 thomas if (edit_in_progress && fold_only) {
12606 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12607 97a02382 2023-07-10 thomas "histedit operation is in progress in this "
12608 97a02382 2023-07-10 thomas "work tree and must be continued or aborted "
12609 97a02382 2023-07-10 thomas "before the -f option can be used");
12610 97a02382 2023-07-10 thomas goto done;
12612 97a02382 2023-07-10 thomas if (edit_in_progress && edit_only) {
12613 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12614 97a02382 2023-07-10 thomas "histedit operation is in progress in this "
12615 97a02382 2023-07-10 thomas "work tree and must be continued or aborted "
12616 97a02382 2023-07-10 thomas "before the -e option can be used");
12617 97a02382 2023-07-10 thomas goto done;
12620 97a02382 2023-07-10 thomas if (edit_in_progress && abort_edit) {
12621 97a02382 2023-07-10 thomas error = got_worktree_histedit_continue(&resume_commit_id,
12622 97a02382 2023-07-10 thomas &tmp_branch, &branch, &base_commit_id, &fileindex,
12623 97a02382 2023-07-10 thomas worktree, repo);
12624 97a02382 2023-07-10 thomas if (error)
12625 97a02382 2023-07-10 thomas goto done;
12626 97a02382 2023-07-10 thomas printf("Switching work tree to %s\n",
12627 97a02382 2023-07-10 thomas got_ref_get_symref_target(branch));
12628 97a02382 2023-07-10 thomas error = got_worktree_histedit_abort(worktree, fileindex, repo,
12629 97a02382 2023-07-10 thomas branch, base_commit_id, abort_progress, &upa);
12630 97a02382 2023-07-10 thomas if (error)
12631 97a02382 2023-07-10 thomas goto done;
12632 97a02382 2023-07-10 thomas printf("Histedit of %s aborted\n",
12633 97a02382 2023-07-10 thomas got_ref_get_symref_target(branch));
12634 97a02382 2023-07-10 thomas print_merge_progress_stats(&upa);
12635 97a02382 2023-07-10 thomas goto done; /* nothing else to do */
12636 97a02382 2023-07-10 thomas } else if (abort_edit) {
12637 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_NOT_HISTEDIT);
12638 97a02382 2023-07-10 thomas goto done;
12641 97a02382 2023-07-10 thomas error = get_author(&committer, repo, worktree);
12642 97a02382 2023-07-10 thomas if (error)
12643 97a02382 2023-07-10 thomas goto done;
12645 97a02382 2023-07-10 thomas if (continue_edit) {
12646 97a02382 2023-07-10 thomas char *path;
12648 97a02382 2023-07-10 thomas if (!edit_in_progress) {
12649 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_NOT_HISTEDIT);
12650 97a02382 2023-07-10 thomas goto done;
12653 97a02382 2023-07-10 thomas error = got_worktree_get_histedit_script_path(&path, worktree);
12654 97a02382 2023-07-10 thomas if (error)
12655 97a02382 2023-07-10 thomas goto done;
12657 97a02382 2023-07-10 thomas error = histedit_load_list(&histedit_cmds, path, repo);
12658 97a02382 2023-07-10 thomas free(path);
12659 97a02382 2023-07-10 thomas if (error)
12660 97a02382 2023-07-10 thomas goto done;
12662 97a02382 2023-07-10 thomas error = got_worktree_histedit_continue(&resume_commit_id,
12663 97a02382 2023-07-10 thomas &tmp_branch, &branch, &base_commit_id, &fileindex,
12664 97a02382 2023-07-10 thomas worktree, repo);
12665 97a02382 2023-07-10 thomas if (error)
12666 97a02382 2023-07-10 thomas goto done;
12668 97a02382 2023-07-10 thomas error = got_ref_resolve(&head_commit_id, repo, branch);
12669 97a02382 2023-07-10 thomas if (error)
12670 97a02382 2023-07-10 thomas goto done;
12672 97a02382 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo,
12673 97a02382 2023-07-10 thomas head_commit_id);
12674 97a02382 2023-07-10 thomas if (error)
12675 97a02382 2023-07-10 thomas goto done;
12676 97a02382 2023-07-10 thomas parent_ids = got_object_commit_get_parent_ids(commit);
12677 97a02382 2023-07-10 thomas pid = STAILQ_FIRST(parent_ids);
12678 97a02382 2023-07-10 thomas if (pid == NULL) {
12679 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12680 97a02382 2023-07-10 thomas goto done;
12682 97a02382 2023-07-10 thomas error = collect_commits(&commits, head_commit_id, &pid->id,
12683 97a02382 2023-07-10 thomas base_commit_id, got_worktree_get_path_prefix(worktree),
12684 97a02382 2023-07-10 thomas GOT_ERR_HISTEDIT_PATH, repo);
12685 97a02382 2023-07-10 thomas got_object_commit_close(commit);
12686 97a02382 2023-07-10 thomas commit = NULL;
12687 97a02382 2023-07-10 thomas if (error)
12688 97a02382 2023-07-10 thomas goto done;
12689 97a02382 2023-07-10 thomas } else {
12690 97a02382 2023-07-10 thomas if (edit_in_progress) {
12691 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_HISTEDIT_BUSY);
12692 97a02382 2023-07-10 thomas goto done;
12695 97a02382 2023-07-10 thomas error = got_ref_open(&branch, repo,
12696 97a02382 2023-07-10 thomas got_worktree_get_head_ref_name(worktree), 0);
12697 97a02382 2023-07-10 thomas if (error != NULL)
12698 97a02382 2023-07-10 thomas goto done;
12700 97a02382 2023-07-10 thomas if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
12701 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
12702 97a02382 2023-07-10 thomas "will not edit commit history of a branch outside "
12703 97a02382 2023-07-10 thomas "the \"refs/heads/\" reference namespace");
12704 97a02382 2023-07-10 thomas goto done;
12707 97a02382 2023-07-10 thomas error = got_ref_resolve(&head_commit_id, repo, branch);
12708 97a02382 2023-07-10 thomas got_ref_close(branch);
12709 97a02382 2023-07-10 thomas branch = NULL;
12710 97a02382 2023-07-10 thomas if (error)
12711 97a02382 2023-07-10 thomas goto done;
12713 97a02382 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo,
12714 97a02382 2023-07-10 thomas head_commit_id);
12715 97a02382 2023-07-10 thomas if (error)
12716 97a02382 2023-07-10 thomas goto done;
12717 97a02382 2023-07-10 thomas parent_ids = got_object_commit_get_parent_ids(commit);
12718 97a02382 2023-07-10 thomas pid = STAILQ_FIRST(parent_ids);
12719 97a02382 2023-07-10 thomas if (pid == NULL) {
12720 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12721 97a02382 2023-07-10 thomas goto done;
12723 97a02382 2023-07-10 thomas error = collect_commits(&commits, head_commit_id, &pid->id,
12724 97a02382 2023-07-10 thomas got_worktree_get_base_commit_id(worktree),
12725 97a02382 2023-07-10 thomas got_worktree_get_path_prefix(worktree),
12726 97a02382 2023-07-10 thomas GOT_ERR_HISTEDIT_PATH, repo);
12727 97a02382 2023-07-10 thomas got_object_commit_close(commit);
12728 97a02382 2023-07-10 thomas commit = NULL;
12729 97a02382 2023-07-10 thomas if (error)
12730 97a02382 2023-07-10 thomas goto done;
12732 97a02382 2023-07-10 thomas if (STAILQ_EMPTY(&commits)) {
12733 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12734 97a02382 2023-07-10 thomas goto done;
12737 97a02382 2023-07-10 thomas error = got_worktree_histedit_prepare(&tmp_branch, &branch,
12738 97a02382 2023-07-10 thomas &base_commit_id, &fileindex, worktree, repo);
12739 97a02382 2023-07-10 thomas if (error)
12740 97a02382 2023-07-10 thomas goto done;
12742 97a02382 2023-07-10 thomas if (edit_script_path) {
12743 97a02382 2023-07-10 thomas error = histedit_load_list(&histedit_cmds,
12744 97a02382 2023-07-10 thomas edit_script_path, repo);
12745 97a02382 2023-07-10 thomas if (error) {
12746 97a02382 2023-07-10 thomas got_worktree_histedit_abort(worktree, fileindex,
12747 97a02382 2023-07-10 thomas repo, branch, base_commit_id,
12748 97a02382 2023-07-10 thomas abort_progress, &upa);
12749 97a02382 2023-07-10 thomas print_merge_progress_stats(&upa);
12750 97a02382 2023-07-10 thomas goto done;
12752 97a02382 2023-07-10 thomas } else {
12753 97a02382 2023-07-10 thomas const char *branch_name;
12754 97a02382 2023-07-10 thomas branch_name = got_ref_get_symref_target(branch);
12755 97a02382 2023-07-10 thomas if (strncmp(branch_name, "refs/heads/", 11) == 0)
12756 97a02382 2023-07-10 thomas branch_name += 11;
12757 97a02382 2023-07-10 thomas error = histedit_edit_script(&histedit_cmds, &commits,
12758 97a02382 2023-07-10 thomas branch_name, edit_logmsg_only, fold_only,
12759 97a02382 2023-07-10 thomas drop_only, edit_only, repo);
12760 97a02382 2023-07-10 thomas if (error) {
12761 97a02382 2023-07-10 thomas got_worktree_histedit_abort(worktree, fileindex,
12762 97a02382 2023-07-10 thomas repo, branch, base_commit_id,
12763 97a02382 2023-07-10 thomas abort_progress, &upa);
12764 97a02382 2023-07-10 thomas print_merge_progress_stats(&upa);
12765 97a02382 2023-07-10 thomas goto done;
12770 97a02382 2023-07-10 thomas error = histedit_save_list(&histedit_cmds, worktree,
12772 97a02382 2023-07-10 thomas if (error) {
12773 97a02382 2023-07-10 thomas got_worktree_histedit_abort(worktree, fileindex,
12774 97a02382 2023-07-10 thomas repo, branch, base_commit_id,
12775 97a02382 2023-07-10 thomas abort_progress, &upa);
12776 97a02382 2023-07-10 thomas print_merge_progress_stats(&upa);
12777 97a02382 2023-07-10 thomas goto done;
12782 97a02382 2023-07-10 thomas error = histedit_check_script(&histedit_cmds, &commits, repo);
12783 97a02382 2023-07-10 thomas if (error)
12784 97a02382 2023-07-10 thomas goto done;
12786 97a02382 2023-07-10 thomas TAILQ_FOREACH(hle, &histedit_cmds, entry) {
12787 97a02382 2023-07-10 thomas if (resume_commit_id) {
12788 97a02382 2023-07-10 thomas if (got_object_id_cmp(hle->commit_id,
12789 97a02382 2023-07-10 thomas resume_commit_id) != 0)
12790 97a02382 2023-07-10 thomas continue;
12792 97a02382 2023-07-10 thomas resume_commit_id = NULL;
12793 97a02382 2023-07-10 thomas if (hle->cmd->code == GOT_HISTEDIT_DROP ||
12794 97a02382 2023-07-10 thomas hle->cmd->code == GOT_HISTEDIT_FOLD) {
12795 97a02382 2023-07-10 thomas error = histedit_skip_commit(hle, worktree,
12797 97a02382 2023-07-10 thomas if (error)
12798 97a02382 2023-07-10 thomas goto done;
12799 97a02382 2023-07-10 thomas } else {
12800 97a02382 2023-07-10 thomas struct got_pathlist_head paths;
12801 97a02382 2023-07-10 thomas int have_changes = 0;
12803 97a02382 2023-07-10 thomas TAILQ_INIT(&paths);
12804 97a02382 2023-07-10 thomas error = got_pathlist_append(&paths, "", NULL);
12805 97a02382 2023-07-10 thomas if (error)
12806 97a02382 2023-07-10 thomas goto done;
12807 97a02382 2023-07-10 thomas error = got_worktree_status(worktree, &paths,
12808 97a02382 2023-07-10 thomas repo, 0, check_local_changes, &have_changes,
12809 97a02382 2023-07-10 thomas check_cancelled, NULL);
12810 97a02382 2023-07-10 thomas got_pathlist_free(&paths,
12811 97a02382 2023-07-10 thomas GOT_PATHLIST_FREE_NONE);
12812 97a02382 2023-07-10 thomas if (error) {
12813 97a02382 2023-07-10 thomas if (error->code != GOT_ERR_CANCELLED)
12814 97a02382 2023-07-10 thomas goto done;
12815 97a02382 2023-07-10 thomas if (sigint_received || sigpipe_received)
12816 97a02382 2023-07-10 thomas goto done;
12818 97a02382 2023-07-10 thomas if (have_changes) {
12819 97a02382 2023-07-10 thomas error = histedit_commit(NULL, worktree,
12820 97a02382 2023-07-10 thomas fileindex, tmp_branch, hle,
12821 97a02382 2023-07-10 thomas committer, allow_conflict, repo);
12822 97a02382 2023-07-10 thomas if (error)
12823 97a02382 2023-07-10 thomas goto done;
12824 97a02382 2023-07-10 thomas } else {
12825 97a02382 2023-07-10 thomas error = got_object_open_as_commit(
12826 97a02382 2023-07-10 thomas &commit, repo, hle->commit_id);
12827 97a02382 2023-07-10 thomas if (error)
12828 97a02382 2023-07-10 thomas goto done;
12829 97a02382 2023-07-10 thomas error = show_histedit_progress(commit,
12830 97a02382 2023-07-10 thomas hle, NULL);
12831 97a02382 2023-07-10 thomas got_object_commit_close(commit);
12832 97a02382 2023-07-10 thomas commit = NULL;
12833 97a02382 2023-07-10 thomas if (error)
12834 97a02382 2023-07-10 thomas goto done;
12837 97a02382 2023-07-10 thomas continue;
12840 97a02382 2023-07-10 thomas if (hle->cmd->code == GOT_HISTEDIT_DROP) {
12841 97a02382 2023-07-10 thomas error = histedit_skip_commit(hle, worktree, repo);
12842 97a02382 2023-07-10 thomas if (error)
12843 97a02382 2023-07-10 thomas goto done;
12844 97a02382 2023-07-10 thomas continue;
12847 97a02382 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo,
12848 97a02382 2023-07-10 thomas hle->commit_id);
12849 97a02382 2023-07-10 thomas if (error)
12850 97a02382 2023-07-10 thomas goto done;
12851 97a02382 2023-07-10 thomas parent_ids = got_object_commit_get_parent_ids(commit);
12852 97a02382 2023-07-10 thomas pid = STAILQ_FIRST(parent_ids);
12854 97a02382 2023-07-10 thomas error = got_worktree_histedit_merge_files(&merged_paths,
12855 97a02382 2023-07-10 thomas worktree, fileindex, &pid->id, hle->commit_id, repo,
12856 97a02382 2023-07-10 thomas update_progress, &upa, check_cancelled, NULL);
12857 97a02382 2023-07-10 thomas if (error)
12858 97a02382 2023-07-10 thomas goto done;
12859 97a02382 2023-07-10 thomas got_object_commit_close(commit);
12860 97a02382 2023-07-10 thomas commit = NULL;
12862 97a02382 2023-07-10 thomas print_merge_progress_stats(&upa);
12863 97a02382 2023-07-10 thomas if (upa.conflicts > 0 || upa.missing > 0 ||
12864 97a02382 2023-07-10 thomas upa.not_deleted > 0 || upa.unversioned > 0) {
12865 97a02382 2023-07-10 thomas if (upa.conflicts > 0) {
12866 97a02382 2023-07-10 thomas error = show_rebase_merge_conflict(
12867 97a02382 2023-07-10 thomas hle->commit_id, repo);
12868 97a02382 2023-07-10 thomas if (error)
12869 97a02382 2023-07-10 thomas goto done;
12871 97a02382 2023-07-10 thomas got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12875 97a02382 2023-07-10 thomas if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
12876 97a02382 2023-07-10 thomas char *id_str;
12877 97a02382 2023-07-10 thomas error = got_object_id_str(&id_str, hle->commit_id);
12878 97a02382 2023-07-10 thomas if (error)
12879 97a02382 2023-07-10 thomas goto done;
12880 97a02382 2023-07-10 thomas printf("Stopping histedit for amending commit %s\n",
12881 97a02382 2023-07-10 thomas id_str);
12882 97a02382 2023-07-10 thomas free(id_str);
12883 97a02382 2023-07-10 thomas got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12884 97a02382 2023-07-10 thomas error = got_worktree_histedit_postpone(worktree,
12885 97a02382 2023-07-10 thomas fileindex);
12886 97a02382 2023-07-10 thomas goto done;
12889 97a02382 2023-07-10 thomas if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
12890 97a02382 2023-07-10 thomas error = histedit_skip_commit(hle, worktree, repo);
12891 97a02382 2023-07-10 thomas if (error)
12892 97a02382 2023-07-10 thomas goto done;
12893 97a02382 2023-07-10 thomas continue;
12896 97a02382 2023-07-10 thomas error = histedit_commit(&merged_paths, worktree, fileindex,
12897 97a02382 2023-07-10 thomas tmp_branch, hle, committer, allow_conflict, repo);
12898 97a02382 2023-07-10 thomas got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12899 97a02382 2023-07-10 thomas if (error)
12900 97a02382 2023-07-10 thomas goto done;
12903 97a02382 2023-07-10 thomas if (upa.conflicts > 0 || upa.missing > 0 ||
12904 97a02382 2023-07-10 thomas upa.not_deleted > 0 || upa.unversioned > 0) {
12905 97a02382 2023-07-10 thomas error = got_worktree_histedit_postpone(worktree, fileindex);
12906 97a02382 2023-07-10 thomas if (error)
12907 97a02382 2023-07-10 thomas goto done;
12908 97a02382 2023-07-10 thomas if (upa.conflicts > 0 && upa.missing == 0 &&
12909 97a02382 2023-07-10 thomas upa.not_deleted == 0 && upa.unversioned == 0) {
12910 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
12911 97a02382 2023-07-10 thomas "conflicts must be resolved before histedit "
12912 97a02382 2023-07-10 thomas "can continue");
12913 97a02382 2023-07-10 thomas } else if (upa.conflicts > 0) {
12914 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
12915 97a02382 2023-07-10 thomas "conflicts must be resolved before histedit "
12916 97a02382 2023-07-10 thomas "can continue; changes destined for some "
12917 97a02382 2023-07-10 thomas "files were not yet merged and should be "
12918 97a02382 2023-07-10 thomas "merged manually if required before the "
12919 97a02382 2023-07-10 thomas "histedit operation is continued");
12920 97a02382 2023-07-10 thomas } else {
12921 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
12922 97a02382 2023-07-10 thomas "changes destined for some files were not "
12923 97a02382 2023-07-10 thomas "yet merged and should be merged manually "
12924 97a02382 2023-07-10 thomas "if required before the histedit operation "
12925 97a02382 2023-07-10 thomas "is continued");
12928 97a02382 2023-07-10 thomas error = histedit_complete(worktree, fileindex, tmp_branch,
12929 97a02382 2023-07-10 thomas branch, repo);
12931 97a02382 2023-07-10 thomas free(cwd);
12932 97a02382 2023-07-10 thomas free(committer);
12933 97a02382 2023-07-10 thomas free(gitconfig_path);
12934 97a02382 2023-07-10 thomas got_object_id_queue_free(&commits);
12935 97a02382 2023-07-10 thomas histedit_free_list(&histedit_cmds);
12936 97a02382 2023-07-10 thomas free(head_commit_id);
12937 97a02382 2023-07-10 thomas free(base_commit_id);
12938 97a02382 2023-07-10 thomas free(resume_commit_id);
12939 97a02382 2023-07-10 thomas if (commit)
12940 97a02382 2023-07-10 thomas got_object_commit_close(commit);
12941 97a02382 2023-07-10 thomas if (branch)
12942 97a02382 2023-07-10 thomas got_ref_close(branch);
12943 97a02382 2023-07-10 thomas if (tmp_branch)
12944 97a02382 2023-07-10 thomas got_ref_close(tmp_branch);
12945 97a02382 2023-07-10 thomas if (worktree)
12946 97a02382 2023-07-10 thomas got_worktree_close(worktree);
12947 97a02382 2023-07-10 thomas if (repo) {
12948 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
12949 97a02382 2023-07-10 thomas if (error == NULL)
12950 97a02382 2023-07-10 thomas error = close_err;
12952 97a02382 2023-07-10 thomas if (pack_fds) {
12953 97a02382 2023-07-10 thomas const struct got_error *pack_err =
12954 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
12955 97a02382 2023-07-10 thomas if (error == NULL)
12956 97a02382 2023-07-10 thomas error = pack_err;
12958 97a02382 2023-07-10 thomas return error;
12961 97a02382 2023-07-10 thomas __dead static void
12962 97a02382 2023-07-10 thomas usage_integrate(void)
12964 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s integrate branch\n", getprogname());
12965 97a02382 2023-07-10 thomas exit(1);
12968 97a02382 2023-07-10 thomas static const struct got_error *
12969 97a02382 2023-07-10 thomas cmd_integrate(int argc, char *argv[])
12971 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
12972 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
12973 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
12974 97a02382 2023-07-10 thomas char *cwd = NULL, *refname = NULL, *base_refname = NULL;
12975 97a02382 2023-07-10 thomas const char *branch_arg = NULL;
12976 97a02382 2023-07-10 thomas struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
12977 97a02382 2023-07-10 thomas struct got_fileindex *fileindex = NULL;
12978 97a02382 2023-07-10 thomas struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
12979 97a02382 2023-07-10 thomas int ch;
12980 97a02382 2023-07-10 thomas struct got_update_progress_arg upa;
12981 97a02382 2023-07-10 thomas int *pack_fds = NULL;
12983 97a02382 2023-07-10 thomas #ifndef PROFILE
12984 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12985 97a02382 2023-07-10 thomas "unveil", NULL) == -1)
12986 97a02382 2023-07-10 thomas err(1, "pledge");
12989 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "")) != -1) {
12990 97a02382 2023-07-10 thomas switch (ch) {
12991 97a02382 2023-07-10 thomas default:
12992 97a02382 2023-07-10 thomas usage_integrate();
12993 97a02382 2023-07-10 thomas /* NOTREACHED */
12997 97a02382 2023-07-10 thomas argc -= optind;
12998 97a02382 2023-07-10 thomas argv += optind;
13000 97a02382 2023-07-10 thomas if (argc != 1)
13001 97a02382 2023-07-10 thomas usage_integrate();
13002 97a02382 2023-07-10 thomas branch_arg = argv[0];
13004 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
13005 97a02382 2023-07-10 thomas if (cwd == NULL) {
13006 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
13007 97a02382 2023-07-10 thomas goto done;
13010 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
13011 97a02382 2023-07-10 thomas if (error != NULL)
13012 97a02382 2023-07-10 thomas goto done;
13014 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
13015 97a02382 2023-07-10 thomas if (error) {
13016 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
13017 97a02382 2023-07-10 thomas error = wrap_not_worktree_error(error, "integrate",
13019 97a02382 2023-07-10 thomas goto done;
13022 97a02382 2023-07-10 thomas error = check_rebase_or_histedit_in_progress(worktree);
13023 97a02382 2023-07-10 thomas if (error)
13024 97a02382 2023-07-10 thomas goto done;
13026 97a02382 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13027 97a02382 2023-07-10 thomas NULL, pack_fds);
13028 97a02382 2023-07-10 thomas if (error != NULL)
13029 97a02382 2023-07-10 thomas goto done;
13031 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
13032 97a02382 2023-07-10 thomas got_worktree_get_root_path(worktree));
13033 97a02382 2023-07-10 thomas if (error)
13034 97a02382 2023-07-10 thomas goto done;
13036 97a02382 2023-07-10 thomas error = check_merge_in_progress(worktree, repo);
13037 97a02382 2023-07-10 thomas if (error)
13038 97a02382 2023-07-10 thomas goto done;
13040 97a02382 2023-07-10 thomas if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
13041 97a02382 2023-07-10 thomas error = got_error_from_errno("asprintf");
13042 97a02382 2023-07-10 thomas goto done;
13045 97a02382 2023-07-10 thomas error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
13046 97a02382 2023-07-10 thomas &base_branch_ref, worktree, refname, repo);
13047 97a02382 2023-07-10 thomas if (error)
13048 97a02382 2023-07-10 thomas goto done;
13050 97a02382 2023-07-10 thomas refname = strdup(got_ref_get_name(branch_ref));
13051 97a02382 2023-07-10 thomas if (refname == NULL) {
13052 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
13053 97a02382 2023-07-10 thomas got_worktree_integrate_abort(worktree, fileindex, repo,
13054 97a02382 2023-07-10 thomas branch_ref, base_branch_ref);
13055 97a02382 2023-07-10 thomas goto done;
13057 97a02382 2023-07-10 thomas base_refname = strdup(got_ref_get_name(base_branch_ref));
13058 97a02382 2023-07-10 thomas if (base_refname == NULL) {
13059 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
13060 97a02382 2023-07-10 thomas got_worktree_integrate_abort(worktree, fileindex, repo,
13061 97a02382 2023-07-10 thomas branch_ref, base_branch_ref);
13062 97a02382 2023-07-10 thomas goto done;
13064 97a02382 2023-07-10 thomas if (strncmp(base_refname, "refs/heads/", 11) != 0) {
13065 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_INTEGRATE_BRANCH);
13066 97a02382 2023-07-10 thomas got_worktree_integrate_abort(worktree, fileindex, repo,
13067 97a02382 2023-07-10 thomas branch_ref, base_branch_ref);
13068 97a02382 2023-07-10 thomas goto done;
13071 97a02382 2023-07-10 thomas error = got_ref_resolve(&commit_id, repo, branch_ref);
13072 97a02382 2023-07-10 thomas if (error)
13073 97a02382 2023-07-10 thomas goto done;
13075 97a02382 2023-07-10 thomas error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
13076 97a02382 2023-07-10 thomas if (error)
13077 97a02382 2023-07-10 thomas goto done;
13079 97a02382 2023-07-10 thomas if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
13080 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_SAME_BRANCH,
13081 97a02382 2023-07-10 thomas "specified branch has already been integrated");
13082 97a02382 2023-07-10 thomas got_worktree_integrate_abort(worktree, fileindex, repo,
13083 97a02382 2023-07-10 thomas branch_ref, base_branch_ref);
13084 97a02382 2023-07-10 thomas goto done;
13087 97a02382 2023-07-10 thomas error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
13088 97a02382 2023-07-10 thomas if (error) {
13089 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_ANCESTRY)
13090 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_REBASE_REQUIRED);
13091 97a02382 2023-07-10 thomas got_worktree_integrate_abort(worktree, fileindex, repo,
13092 97a02382 2023-07-10 thomas branch_ref, base_branch_ref);
13093 97a02382 2023-07-10 thomas goto done;
13096 97a02382 2023-07-10 thomas memset(&upa, 0, sizeof(upa));
13097 97a02382 2023-07-10 thomas error = got_worktree_integrate_continue(worktree, fileindex, repo,
13098 97a02382 2023-07-10 thomas branch_ref, base_branch_ref, update_progress, &upa,
13099 97a02382 2023-07-10 thomas check_cancelled, NULL);
13100 97a02382 2023-07-10 thomas if (error)
13101 97a02382 2023-07-10 thomas goto done;
13103 97a02382 2023-07-10 thomas printf("Integrated %s into %s\n", refname, base_refname);
13104 97a02382 2023-07-10 thomas print_update_progress_stats(&upa);
13106 97a02382 2023-07-10 thomas if (repo) {
13107 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
13108 97a02382 2023-07-10 thomas if (error == NULL)
13109 97a02382 2023-07-10 thomas error = close_err;
13111 97a02382 2023-07-10 thomas if (worktree)
13112 97a02382 2023-07-10 thomas got_worktree_close(worktree);
13113 97a02382 2023-07-10 thomas if (pack_fds) {
13114 97a02382 2023-07-10 thomas const struct got_error *pack_err =
13115 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
13116 97a02382 2023-07-10 thomas if (error == NULL)
13117 97a02382 2023-07-10 thomas error = pack_err;
13119 97a02382 2023-07-10 thomas free(cwd);
13120 97a02382 2023-07-10 thomas free(base_commit_id);
13121 97a02382 2023-07-10 thomas free(commit_id);
13122 97a02382 2023-07-10 thomas free(refname);
13123 97a02382 2023-07-10 thomas free(base_refname);
13124 97a02382 2023-07-10 thomas return error;
13127 97a02382 2023-07-10 thomas __dead static void
13128 97a02382 2023-07-10 thomas usage_merge(void)
13130 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s merge [-aCcn] [branch]\n", getprogname());
13131 97a02382 2023-07-10 thomas exit(1);
13134 97a02382 2023-07-10 thomas static const struct got_error *
13135 97a02382 2023-07-10 thomas cmd_merge(int argc, char *argv[])
13137 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
13138 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
13139 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
13140 97a02382 2023-07-10 thomas struct got_fileindex *fileindex = NULL;
13141 97a02382 2023-07-10 thomas char *cwd = NULL, *id_str = NULL, *author = NULL;
13142 97a02382 2023-07-10 thomas char *gitconfig_path = NULL;
13143 97a02382 2023-07-10 thomas struct got_reference *branch = NULL, *wt_branch = NULL;
13144 97a02382 2023-07-10 thomas struct got_object_id *branch_tip = NULL, *yca_id = NULL;
13145 97a02382 2023-07-10 thomas struct got_object_id *wt_branch_tip = NULL;
13146 97a02382 2023-07-10 thomas int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
13147 97a02382 2023-07-10 thomas int allow_conflict = 0, prefer_fast_forward = 1, interrupt_merge = 0;
13148 97a02382 2023-07-10 thomas struct got_update_progress_arg upa;
13149 97a02382 2023-07-10 thomas struct got_object_id *merge_commit_id = NULL;
13150 97a02382 2023-07-10 thomas char *branch_name = NULL;
13151 97a02382 2023-07-10 thomas int *pack_fds = NULL;
13153 97a02382 2023-07-10 thomas memset(&upa, 0, sizeof(upa));
13155 97a02382 2023-07-10 thomas #ifndef PROFILE
13156 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13157 97a02382 2023-07-10 thomas "unveil", NULL) == -1)
13158 97a02382 2023-07-10 thomas err(1, "pledge");
13161 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "aCcMn")) != -1) {
13162 97a02382 2023-07-10 thomas switch (ch) {
13163 97a02382 2023-07-10 thomas case 'a':
13164 97a02382 2023-07-10 thomas abort_merge = 1;
13166 97a02382 2023-07-10 thomas case 'C':
13167 97a02382 2023-07-10 thomas allow_conflict = 1;
13169 97a02382 2023-07-10 thomas case 'c':
13170 97a02382 2023-07-10 thomas continue_merge = 1;
13172 97a02382 2023-07-10 thomas case 'M':
13173 97a02382 2023-07-10 thomas prefer_fast_forward = 0;
13175 97a02382 2023-07-10 thomas case 'n':
13176 97a02382 2023-07-10 thomas interrupt_merge = 1;
13178 97a02382 2023-07-10 thomas default:
13179 97a02382 2023-07-10 thomas usage_merge();
13180 97a02382 2023-07-10 thomas /* NOTREACHED */
13184 97a02382 2023-07-10 thomas argc -= optind;
13185 97a02382 2023-07-10 thomas argv += optind;
13187 97a02382 2023-07-10 thomas if (abort_merge) {
13188 97a02382 2023-07-10 thomas if (continue_merge)
13189 97a02382 2023-07-10 thomas option_conflict('a', 'c');
13190 97a02382 2023-07-10 thomas if (!prefer_fast_forward)
13191 97a02382 2023-07-10 thomas option_conflict('a', 'M');
13192 97a02382 2023-07-10 thomas if (interrupt_merge)
13193 97a02382 2023-07-10 thomas option_conflict('a', 'n');
13194 97a02382 2023-07-10 thomas } else if (continue_merge) {
13195 97a02382 2023-07-10 thomas if (!prefer_fast_forward)
13196 97a02382 2023-07-10 thomas option_conflict('c', 'M');
13197 97a02382 2023-07-10 thomas if (interrupt_merge)
13198 97a02382 2023-07-10 thomas option_conflict('c', 'n');
13200 97a02382 2023-07-10 thomas if (allow_conflict) {
13201 97a02382 2023-07-10 thomas if (!continue_merge)
13202 97a02382 2023-07-10 thomas errx(1, "-C option requires -c");
13204 97a02382 2023-07-10 thomas if (abort_merge || continue_merge) {
13205 97a02382 2023-07-10 thomas if (argc != 0)
13206 97a02382 2023-07-10 thomas usage_merge();
13207 97a02382 2023-07-10 thomas } else if (argc != 1)
13208 97a02382 2023-07-10 thomas usage_merge();
13210 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
13211 97a02382 2023-07-10 thomas if (cwd == NULL) {
13212 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
13213 97a02382 2023-07-10 thomas goto done;
13216 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
13217 97a02382 2023-07-10 thomas if (error != NULL)
13218 97a02382 2023-07-10 thomas goto done;
13220 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
13221 97a02382 2023-07-10 thomas if (error) {
13222 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
13223 97a02382 2023-07-10 thomas error = wrap_not_worktree_error(error,
13224 97a02382 2023-07-10 thomas "merge", cwd);
13225 97a02382 2023-07-10 thomas goto done;
13228 97a02382 2023-07-10 thomas error = get_gitconfig_path(&gitconfig_path);
13229 97a02382 2023-07-10 thomas if (error)
13230 97a02382 2023-07-10 thomas goto done;
13231 97a02382 2023-07-10 thomas error = got_repo_open(&repo,
13232 97a02382 2023-07-10 thomas worktree ? got_worktree_get_repo_path(worktree) : cwd,
13233 97a02382 2023-07-10 thomas gitconfig_path, pack_fds);
13234 97a02382 2023-07-10 thomas if (error != NULL)
13235 97a02382 2023-07-10 thomas goto done;
13237 97a02382 2023-07-10 thomas if (worktree != NULL) {
13238 97a02382 2023-07-10 thomas error = worktree_has_logmsg_ref("merge", worktree, repo);
13239 97a02382 2023-07-10 thomas if (error)
13240 97a02382 2023-07-10 thomas goto done;
13243 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
13244 97a02382 2023-07-10 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
13245 97a02382 2023-07-10 thomas if (error)
13246 97a02382 2023-07-10 thomas goto done;
13248 97a02382 2023-07-10 thomas error = check_rebase_or_histedit_in_progress(worktree);
13249 97a02382 2023-07-10 thomas if (error)
13250 97a02382 2023-07-10 thomas goto done;
13252 97a02382 2023-07-10 thomas error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
13254 97a02382 2023-07-10 thomas if (error)
13255 97a02382 2023-07-10 thomas goto done;
13257 97a02382 2023-07-10 thomas if (merge_in_progress && !(abort_merge || continue_merge)) {
13258 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_MERGE_BUSY);
13259 97a02382 2023-07-10 thomas goto done;
13262 97a02382 2023-07-10 thomas if (!merge_in_progress && (abort_merge || continue_merge)) {
13263 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_NOT_MERGING);
13264 97a02382 2023-07-10 thomas goto done;
13267 97a02382 2023-07-10 thomas if (abort_merge) {
13268 97a02382 2023-07-10 thomas error = got_worktree_merge_continue(&branch_name,
13269 97a02382 2023-07-10 thomas &branch_tip, &fileindex, worktree, repo);
13270 97a02382 2023-07-10 thomas if (error)
13271 97a02382 2023-07-10 thomas goto done;
13272 97a02382 2023-07-10 thomas error = got_worktree_merge_abort(worktree, fileindex, repo,
13273 97a02382 2023-07-10 thomas abort_progress, &upa);
13274 97a02382 2023-07-10 thomas if (error)
13275 97a02382 2023-07-10 thomas goto done;
13276 97a02382 2023-07-10 thomas printf("Merge of %s aborted\n", branch_name);
13277 97a02382 2023-07-10 thomas goto done; /* nothing else to do */
13280 97a02382 2023-07-10 thomas if (strncmp(got_worktree_get_head_ref_name(worktree),
13281 97a02382 2023-07-10 thomas "refs/heads/", 11) != 0) {
13282 97a02382 2023-07-10 thomas error = got_error_fmt(GOT_ERR_COMMIT_BRANCH,
13283 97a02382 2023-07-10 thomas "work tree's current branch %s is outside the "
13284 97a02382 2023-07-10 thomas "\"refs/heads/\" reference namespace; "
13285 97a02382 2023-07-10 thomas "update -b required",
13286 97a02382 2023-07-10 thomas got_worktree_get_head_ref_name(worktree));
13287 97a02382 2023-07-10 thomas goto done;
13290 97a02382 2023-07-10 thomas error = get_author(&author, repo, worktree);
13291 97a02382 2023-07-10 thomas if (error)
13292 97a02382 2023-07-10 thomas goto done;
13294 97a02382 2023-07-10 thomas error = got_ref_open(&wt_branch, repo,
13295 97a02382 2023-07-10 thomas got_worktree_get_head_ref_name(worktree), 0);
13296 97a02382 2023-07-10 thomas if (error)
13297 97a02382 2023-07-10 thomas goto done;
13298 97a02382 2023-07-10 thomas error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
13299 97a02382 2023-07-10 thomas if (error)
13300 97a02382 2023-07-10 thomas goto done;
13302 97a02382 2023-07-10 thomas if (continue_merge) {
13303 97a02382 2023-07-10 thomas struct got_object_id *base_commit_id;
13304 97a02382 2023-07-10 thomas base_commit_id = got_worktree_get_base_commit_id(worktree);
13305 97a02382 2023-07-10 thomas if (got_object_id_cmp(wt_branch_tip, base_commit_id) != 0) {
13306 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
13307 97a02382 2023-07-10 thomas goto done;
13309 97a02382 2023-07-10 thomas error = got_worktree_merge_continue(&branch_name,
13310 97a02382 2023-07-10 thomas &branch_tip, &fileindex, worktree, repo);
13311 97a02382 2023-07-10 thomas if (error)
13312 97a02382 2023-07-10 thomas goto done;
13313 97a02382 2023-07-10 thomas } else {
13314 97a02382 2023-07-10 thomas error = got_ref_open(&branch, repo, argv[0], 0);
13315 97a02382 2023-07-10 thomas if (error != NULL)
13316 97a02382 2023-07-10 thomas goto done;
13317 97a02382 2023-07-10 thomas branch_name = strdup(got_ref_get_name(branch));
13318 97a02382 2023-07-10 thomas if (branch_name == NULL) {
13319 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
13320 97a02382 2023-07-10 thomas goto done;
13322 97a02382 2023-07-10 thomas error = got_ref_resolve(&branch_tip, repo, branch);
13323 97a02382 2023-07-10 thomas if (error)
13324 97a02382 2023-07-10 thomas goto done;
13327 97a02382 2023-07-10 thomas error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
13328 97a02382 2023-07-10 thomas wt_branch_tip, branch_tip, 0, repo,
13329 97a02382 2023-07-10 thomas check_cancelled, NULL);
13330 97a02382 2023-07-10 thomas if (error && error->code != GOT_ERR_ANCESTRY)
13331 97a02382 2023-07-10 thomas goto done;
13333 97a02382 2023-07-10 thomas if (!continue_merge) {
13334 97a02382 2023-07-10 thomas error = check_path_prefix(wt_branch_tip, branch_tip,
13335 97a02382 2023-07-10 thomas got_worktree_get_path_prefix(worktree),
13336 97a02382 2023-07-10 thomas GOT_ERR_MERGE_PATH, repo);
13337 97a02382 2023-07-10 thomas if (error)
13338 97a02382 2023-07-10 thomas goto done;
13339 97a02382 2023-07-10 thomas error = got_worktree_merge_prepare(&fileindex, worktree, repo);
13340 97a02382 2023-07-10 thomas if (error)
13341 97a02382 2023-07-10 thomas goto done;
13342 97a02382 2023-07-10 thomas if (prefer_fast_forward && yca_id &&
13343 97a02382 2023-07-10 thomas got_object_id_cmp(wt_branch_tip, yca_id) == 0) {
13344 97a02382 2023-07-10 thomas struct got_pathlist_head paths;
13345 97a02382 2023-07-10 thomas if (interrupt_merge) {
13346 97a02382 2023-07-10 thomas error = got_error_fmt(GOT_ERR_BAD_OPTION,
13347 97a02382 2023-07-10 thomas "there are no changes to merge since %s "
13348 97a02382 2023-07-10 thomas "is already based on %s; merge cannot be "
13349 97a02382 2023-07-10 thomas "interrupted for amending; -n",
13350 97a02382 2023-07-10 thomas branch_name, got_ref_get_name(wt_branch));
13351 97a02382 2023-07-10 thomas goto done;
13353 97a02382 2023-07-10 thomas printf("Forwarding %s to %s\n",
13354 97a02382 2023-07-10 thomas got_ref_get_name(wt_branch), branch_name);
13355 97a02382 2023-07-10 thomas error = got_ref_change_ref(wt_branch, branch_tip);
13356 97a02382 2023-07-10 thomas if (error)
13357 97a02382 2023-07-10 thomas goto done;
13358 97a02382 2023-07-10 thomas error = got_ref_write(wt_branch, repo);
13359 97a02382 2023-07-10 thomas if (error)
13360 97a02382 2023-07-10 thomas goto done;
13361 97a02382 2023-07-10 thomas error = got_worktree_set_base_commit_id(worktree, repo,
13362 97a02382 2023-07-10 thomas branch_tip);
13363 97a02382 2023-07-10 thomas if (error)
13364 97a02382 2023-07-10 thomas goto done;
13365 97a02382 2023-07-10 thomas TAILQ_INIT(&paths);
13366 97a02382 2023-07-10 thomas error = got_pathlist_append(&paths, "", NULL);
13367 97a02382 2023-07-10 thomas if (error)
13368 97a02382 2023-07-10 thomas goto done;
13369 97a02382 2023-07-10 thomas error = got_worktree_checkout_files(worktree,
13370 97a02382 2023-07-10 thomas &paths, repo, update_progress, &upa,
13371 97a02382 2023-07-10 thomas check_cancelled, NULL);
13372 97a02382 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
13373 97a02382 2023-07-10 thomas if (error)
13374 97a02382 2023-07-10 thomas goto done;
13375 97a02382 2023-07-10 thomas if (upa.did_something) {
13376 97a02382 2023-07-10 thomas char *id_str;
13377 97a02382 2023-07-10 thomas error = got_object_id_str(&id_str, branch_tip);
13378 97a02382 2023-07-10 thomas if (error)
13379 97a02382 2023-07-10 thomas goto done;
13380 97a02382 2023-07-10 thomas printf("Updated to commit %s\n", id_str);
13381 97a02382 2023-07-10 thomas free(id_str);
13383 97a02382 2023-07-10 thomas printf("Already up-to-date\n");
13384 97a02382 2023-07-10 thomas print_update_progress_stats(&upa);
13385 97a02382 2023-07-10 thomas goto done;
13387 97a02382 2023-07-10 thomas error = got_worktree_merge_write_refs(worktree, branch, repo);
13388 97a02382 2023-07-10 thomas if (error)
13389 97a02382 2023-07-10 thomas goto done;
13391 97a02382 2023-07-10 thomas error = got_worktree_merge_branch(worktree, fileindex,
13392 97a02382 2023-07-10 thomas yca_id, branch_tip, repo, update_progress, &upa,
13393 97a02382 2023-07-10 thomas check_cancelled, NULL);
13394 97a02382 2023-07-10 thomas if (error)
13395 97a02382 2023-07-10 thomas goto done;
13396 97a02382 2023-07-10 thomas print_merge_progress_stats(&upa);
13397 97a02382 2023-07-10 thomas if (!upa.did_something) {
13398 97a02382 2023-07-10 thomas error = got_worktree_merge_abort(worktree, fileindex,
13399 97a02382 2023-07-10 thomas repo, abort_progress, &upa);
13400 97a02382 2023-07-10 thomas if (error)
13401 97a02382 2023-07-10 thomas goto done;
13402 97a02382 2023-07-10 thomas printf("Already up-to-date\n");
13403 97a02382 2023-07-10 thomas goto done;
13407 97a02382 2023-07-10 thomas if (interrupt_merge) {
13408 97a02382 2023-07-10 thomas error = got_worktree_merge_postpone(worktree, fileindex);
13409 97a02382 2023-07-10 thomas if (error)
13410 97a02382 2023-07-10 thomas goto done;
13411 97a02382 2023-07-10 thomas printf("Merge of %s interrupted on request\n", branch_name);
13412 97a02382 2023-07-10 thomas } else if (upa.conflicts > 0 || upa.missing > 0 ||
13413 97a02382 2023-07-10 thomas upa.not_deleted > 0 || upa.unversioned > 0) {
13414 97a02382 2023-07-10 thomas error = got_worktree_merge_postpone(worktree, fileindex);
13415 97a02382 2023-07-10 thomas if (error)
13416 97a02382 2023-07-10 thomas goto done;
13417 97a02382 2023-07-10 thomas if (upa.conflicts > 0 && upa.missing == 0 &&
13418 97a02382 2023-07-10 thomas upa.not_deleted == 0 && upa.unversioned == 0) {
13419 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
13420 97a02382 2023-07-10 thomas "conflicts must be resolved before merging "
13421 97a02382 2023-07-10 thomas "can continue");
13422 97a02382 2023-07-10 thomas } else if (upa.conflicts > 0) {
13423 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
13424 97a02382 2023-07-10 thomas "conflicts must be resolved before merging "
13425 97a02382 2023-07-10 thomas "can continue; changes destined for some "
13426 97a02382 2023-07-10 thomas "files were not yet merged and "
13427 97a02382 2023-07-10 thomas "should be merged manually if required before the "
13428 97a02382 2023-07-10 thomas "merge operation is continued");
13429 97a02382 2023-07-10 thomas } else {
13430 97a02382 2023-07-10 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
13431 97a02382 2023-07-10 thomas "changes destined for some "
13432 97a02382 2023-07-10 thomas "files were not yet merged and should be "
13433 97a02382 2023-07-10 thomas "merged manually if required before the "
13434 97a02382 2023-07-10 thomas "merge operation is continued");
13436 97a02382 2023-07-10 thomas goto done;
13437 97a02382 2023-07-10 thomas } else {
13438 97a02382 2023-07-10 thomas error = got_worktree_merge_commit(&merge_commit_id, worktree,
13439 97a02382 2023-07-10 thomas fileindex, author, NULL, 1, branch_tip, branch_name,
13440 97a02382 2023-07-10 thomas allow_conflict, repo, continue_merge ? print_status : NULL,
13442 97a02382 2023-07-10 thomas if (error)
13443 97a02382 2023-07-10 thomas goto done;
13444 97a02382 2023-07-10 thomas error = got_worktree_merge_complete(worktree, fileindex, repo);
13445 97a02382 2023-07-10 thomas if (error)
13446 97a02382 2023-07-10 thomas goto done;
13447 97a02382 2023-07-10 thomas error = got_object_id_str(&id_str, merge_commit_id);
13448 97a02382 2023-07-10 thomas if (error)
13449 97a02382 2023-07-10 thomas goto done;
13450 97a02382 2023-07-10 thomas printf("Merged %s into %s: %s\n", branch_name,
13451 97a02382 2023-07-10 thomas got_worktree_get_head_ref_name(worktree),
13452 97a02382 2023-07-10 thomas id_str);
13456 97a02382 2023-07-10 thomas free(gitconfig_path);
13457 97a02382 2023-07-10 thomas free(id_str);
13458 97a02382 2023-07-10 thomas free(merge_commit_id);
13459 97a02382 2023-07-10 thomas free(author);
13460 97a02382 2023-07-10 thomas free(branch_tip);
13461 97a02382 2023-07-10 thomas free(branch_name);
13462 97a02382 2023-07-10 thomas free(yca_id);
13463 97a02382 2023-07-10 thomas if (branch)
13464 97a02382 2023-07-10 thomas got_ref_close(branch);
13465 97a02382 2023-07-10 thomas if (wt_branch)
13466 97a02382 2023-07-10 thomas got_ref_close(wt_branch);
13467 97a02382 2023-07-10 thomas if (worktree)
13468 97a02382 2023-07-10 thomas got_worktree_close(worktree);
13469 97a02382 2023-07-10 thomas if (repo) {
13470 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
13471 97a02382 2023-07-10 thomas if (error == NULL)
13472 97a02382 2023-07-10 thomas error = close_err;
13474 97a02382 2023-07-10 thomas if (pack_fds) {
13475 97a02382 2023-07-10 thomas const struct got_error *pack_err =
13476 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
13477 97a02382 2023-07-10 thomas if (error == NULL)
13478 97a02382 2023-07-10 thomas error = pack_err;
13480 97a02382 2023-07-10 thomas return error;
13483 97a02382 2023-07-10 thomas __dead static void
13484 97a02382 2023-07-10 thomas usage_stage(void)
13486 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
13487 97a02382 2023-07-10 thomas "[path ...]\n", getprogname());
13488 97a02382 2023-07-10 thomas exit(1);
13491 97a02382 2023-07-10 thomas static const struct got_error *
13492 97a02382 2023-07-10 thomas print_stage(void *arg, unsigned char status, unsigned char staged_status,
13493 97a02382 2023-07-10 thomas const char *path, struct got_object_id *blob_id,
13494 97a02382 2023-07-10 thomas struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
13495 97a02382 2023-07-10 thomas int dirfd, const char *de_name)
13497 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
13498 97a02382 2023-07-10 thomas char *id_str = NULL;
13500 97a02382 2023-07-10 thomas if (staged_status != GOT_STATUS_ADD &&
13501 97a02382 2023-07-10 thomas staged_status != GOT_STATUS_MODIFY &&
13502 97a02382 2023-07-10 thomas staged_status != GOT_STATUS_DELETE)
13503 97a02382 2023-07-10 thomas return NULL;
13505 97a02382 2023-07-10 thomas if (staged_status == GOT_STATUS_ADD ||
13506 97a02382 2023-07-10 thomas staged_status == GOT_STATUS_MODIFY)
13507 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, staged_blob_id);
13509 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, blob_id);
13510 97a02382 2023-07-10 thomas if (err)
13511 97a02382 2023-07-10 thomas return err;
13513 97a02382 2023-07-10 thomas printf("%s %c %s\n", id_str, staged_status, path);
13514 97a02382 2023-07-10 thomas free(id_str);
13515 97a02382 2023-07-10 thomas return NULL;
13518 97a02382 2023-07-10 thomas static const struct got_error *
13519 97a02382 2023-07-10 thomas cmd_stage(int argc, char *argv[])
13521 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
13522 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
13523 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
13524 97a02382 2023-07-10 thomas char *cwd = NULL;
13525 97a02382 2023-07-10 thomas struct got_pathlist_head paths;
13526 97a02382 2023-07-10 thomas int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
13527 97a02382 2023-07-10 thomas FILE *patch_script_file = NULL;
13528 97a02382 2023-07-10 thomas const char *patch_script_path = NULL;
13529 97a02382 2023-07-10 thomas struct choose_patch_arg cpa;
13530 97a02382 2023-07-10 thomas int *pack_fds = NULL;
13532 97a02382 2023-07-10 thomas TAILQ_INIT(&paths);
13534 97a02382 2023-07-10 thomas #ifndef PROFILE
13535 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13536 97a02382 2023-07-10 thomas "unveil", NULL) == -1)
13537 97a02382 2023-07-10 thomas err(1, "pledge");
13540 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
13541 97a02382 2023-07-10 thomas switch (ch) {
13542 97a02382 2023-07-10 thomas case 'F':
13543 97a02382 2023-07-10 thomas patch_script_path = optarg;
13545 97a02382 2023-07-10 thomas case 'l':
13546 97a02382 2023-07-10 thomas list_stage = 1;
13548 97a02382 2023-07-10 thomas case 'p':
13549 97a02382 2023-07-10 thomas pflag = 1;
13551 97a02382 2023-07-10 thomas case 'S':
13552 97a02382 2023-07-10 thomas allow_bad_symlinks = 1;
13554 97a02382 2023-07-10 thomas default:
13555 97a02382 2023-07-10 thomas usage_stage();
13556 97a02382 2023-07-10 thomas /* NOTREACHED */
13560 97a02382 2023-07-10 thomas argc -= optind;
13561 97a02382 2023-07-10 thomas argv += optind;
13563 97a02382 2023-07-10 thomas if (list_stage && (pflag || patch_script_path))
13564 97a02382 2023-07-10 thomas errx(1, "-l option cannot be used with other options");
13565 97a02382 2023-07-10 thomas if (patch_script_path && !pflag)
13566 97a02382 2023-07-10 thomas errx(1, "-F option can only be used together with -p option");
13568 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
13569 97a02382 2023-07-10 thomas if (cwd == NULL) {
13570 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
13571 97a02382 2023-07-10 thomas goto done;
13574 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
13575 97a02382 2023-07-10 thomas if (error != NULL)
13576 97a02382 2023-07-10 thomas goto done;
13578 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
13579 97a02382 2023-07-10 thomas if (error) {
13580 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
13581 97a02382 2023-07-10 thomas error = wrap_not_worktree_error(error, "stage", cwd);
13582 97a02382 2023-07-10 thomas goto done;
13585 97a02382 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13586 97a02382 2023-07-10 thomas NULL, pack_fds);
13587 97a02382 2023-07-10 thomas if (error != NULL)
13588 97a02382 2023-07-10 thomas goto done;
13590 97a02382 2023-07-10 thomas if (patch_script_path) {
13591 97a02382 2023-07-10 thomas patch_script_file = fopen(patch_script_path, "re");
13592 97a02382 2023-07-10 thomas if (patch_script_file == NULL) {
13593 97a02382 2023-07-10 thomas error = got_error_from_errno2("fopen",
13594 97a02382 2023-07-10 thomas patch_script_path);
13595 97a02382 2023-07-10 thomas goto done;
13598 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
13599 97a02382 2023-07-10 thomas got_worktree_get_root_path(worktree));
13600 97a02382 2023-07-10 thomas if (error)
13601 97a02382 2023-07-10 thomas goto done;
13603 97a02382 2023-07-10 thomas error = check_merge_in_progress(worktree, repo);
13604 97a02382 2023-07-10 thomas if (error)
13605 97a02382 2023-07-10 thomas goto done;
13607 97a02382 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13608 97a02382 2023-07-10 thomas if (error)
13609 97a02382 2023-07-10 thomas goto done;
13611 97a02382 2023-07-10 thomas if (list_stage)
13612 97a02382 2023-07-10 thomas error = got_worktree_status(worktree, &paths, repo, 0,
13613 97a02382 2023-07-10 thomas print_stage, NULL, check_cancelled, NULL);
13615 97a02382 2023-07-10 thomas cpa.patch_script_file = patch_script_file;
13616 97a02382 2023-07-10 thomas cpa.action = "stage";
13617 97a02382 2023-07-10 thomas error = got_worktree_stage(worktree, &paths,
13618 97a02382 2023-07-10 thomas pflag ? NULL : print_status, NULL,
13619 97a02382 2023-07-10 thomas pflag ? choose_patch : NULL, &cpa,
13620 97a02382 2023-07-10 thomas allow_bad_symlinks, repo);
13623 97a02382 2023-07-10 thomas if (patch_script_file && fclose(patch_script_file) == EOF &&
13624 97a02382 2023-07-10 thomas error == NULL)
13625 97a02382 2023-07-10 thomas error = got_error_from_errno2("fclose", patch_script_path);
13626 97a02382 2023-07-10 thomas if (repo) {
13627 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
13628 97a02382 2023-07-10 thomas if (error == NULL)
13629 97a02382 2023-07-10 thomas error = close_err;
13631 97a02382 2023-07-10 thomas if (worktree)
13632 97a02382 2023-07-10 thomas got_worktree_close(worktree);
13633 97a02382 2023-07-10 thomas if (pack_fds) {
13634 97a02382 2023-07-10 thomas const struct got_error *pack_err =
13635 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
13636 97a02382 2023-07-10 thomas if (error == NULL)
13637 97a02382 2023-07-10 thomas error = pack_err;
13639 97a02382 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13640 97a02382 2023-07-10 thomas free(cwd);
13641 97a02382 2023-07-10 thomas return error;
13644 97a02382 2023-07-10 thomas __dead static void
13645 97a02382 2023-07-10 thomas usage_unstage(void)
13647 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
13648 97a02382 2023-07-10 thomas "[path ...]\n", getprogname());
13649 97a02382 2023-07-10 thomas exit(1);
13653 97a02382 2023-07-10 thomas static const struct got_error *
13654 97a02382 2023-07-10 thomas cmd_unstage(int argc, char *argv[])
13656 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
13657 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
13658 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
13659 97a02382 2023-07-10 thomas char *cwd = NULL;
13660 97a02382 2023-07-10 thomas struct got_pathlist_head paths;
13661 97a02382 2023-07-10 thomas int ch, pflag = 0;
13662 97a02382 2023-07-10 thomas struct got_update_progress_arg upa;
13663 97a02382 2023-07-10 thomas FILE *patch_script_file = NULL;
13664 97a02382 2023-07-10 thomas const char *patch_script_path = NULL;
13665 97a02382 2023-07-10 thomas struct choose_patch_arg cpa;
13666 97a02382 2023-07-10 thomas int *pack_fds = NULL;
13668 97a02382 2023-07-10 thomas TAILQ_INIT(&paths);
13670 97a02382 2023-07-10 thomas #ifndef PROFILE
13671 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13672 97a02382 2023-07-10 thomas "unveil", NULL) == -1)
13673 97a02382 2023-07-10 thomas err(1, "pledge");
13676 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "F:p")) != -1) {
13677 97a02382 2023-07-10 thomas switch (ch) {
13678 97a02382 2023-07-10 thomas case 'F':
13679 97a02382 2023-07-10 thomas patch_script_path = optarg;
13681 97a02382 2023-07-10 thomas case 'p':
13682 97a02382 2023-07-10 thomas pflag = 1;
13684 97a02382 2023-07-10 thomas default:
13685 97a02382 2023-07-10 thomas usage_unstage();
13686 97a02382 2023-07-10 thomas /* NOTREACHED */
13690 97a02382 2023-07-10 thomas argc -= optind;
13691 97a02382 2023-07-10 thomas argv += optind;
13693 97a02382 2023-07-10 thomas if (patch_script_path && !pflag)
13694 97a02382 2023-07-10 thomas errx(1, "-F option can only be used together with -p option");
13696 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
13697 97a02382 2023-07-10 thomas if (cwd == NULL) {
13698 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
13699 97a02382 2023-07-10 thomas goto done;
13702 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
13703 97a02382 2023-07-10 thomas if (error != NULL)
13704 97a02382 2023-07-10 thomas goto done;
13706 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
13707 97a02382 2023-07-10 thomas if (error) {
13708 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
13709 97a02382 2023-07-10 thomas error = wrap_not_worktree_error(error, "unstage", cwd);
13710 97a02382 2023-07-10 thomas goto done;
13713 97a02382 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13714 97a02382 2023-07-10 thomas NULL, pack_fds);
13715 97a02382 2023-07-10 thomas if (error != NULL)
13716 97a02382 2023-07-10 thomas goto done;
13718 97a02382 2023-07-10 thomas if (patch_script_path) {
13719 97a02382 2023-07-10 thomas patch_script_file = fopen(patch_script_path, "re");
13720 97a02382 2023-07-10 thomas if (patch_script_file == NULL) {
13721 97a02382 2023-07-10 thomas error = got_error_from_errno2("fopen",
13722 97a02382 2023-07-10 thomas patch_script_path);
13723 97a02382 2023-07-10 thomas goto done;
13727 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
13728 97a02382 2023-07-10 thomas got_worktree_get_root_path(worktree));
13729 97a02382 2023-07-10 thomas if (error)
13730 97a02382 2023-07-10 thomas goto done;
13732 97a02382 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13733 97a02382 2023-07-10 thomas if (error)
13734 97a02382 2023-07-10 thomas goto done;
13736 97a02382 2023-07-10 thomas cpa.patch_script_file = patch_script_file;
13737 97a02382 2023-07-10 thomas cpa.action = "unstage";
13738 97a02382 2023-07-10 thomas memset(&upa, 0, sizeof(upa));
13739 97a02382 2023-07-10 thomas error = got_worktree_unstage(worktree, &paths, update_progress,
13740 97a02382 2023-07-10 thomas &upa, pflag ? choose_patch : NULL, &cpa, repo);
13741 97a02382 2023-07-10 thomas if (!error)
13742 97a02382 2023-07-10 thomas print_merge_progress_stats(&upa);
13744 97a02382 2023-07-10 thomas if (patch_script_file && fclose(patch_script_file) == EOF &&
13745 97a02382 2023-07-10 thomas error == NULL)
13746 97a02382 2023-07-10 thomas error = got_error_from_errno2("fclose", patch_script_path);
13747 97a02382 2023-07-10 thomas if (repo) {
13748 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
13749 97a02382 2023-07-10 thomas if (error == NULL)
13750 97a02382 2023-07-10 thomas error = close_err;
13752 97a02382 2023-07-10 thomas if (worktree)
13753 97a02382 2023-07-10 thomas got_worktree_close(worktree);
13754 97a02382 2023-07-10 thomas if (pack_fds) {
13755 97a02382 2023-07-10 thomas const struct got_error *pack_err =
13756 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
13757 97a02382 2023-07-10 thomas if (error == NULL)
13758 97a02382 2023-07-10 thomas error = pack_err;
13760 97a02382 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13761 97a02382 2023-07-10 thomas free(cwd);
13762 97a02382 2023-07-10 thomas return error;
13765 97a02382 2023-07-10 thomas __dead static void
13766 97a02382 2023-07-10 thomas usage_cat(void)
13768 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
13769 97a02382 2023-07-10 thomas "arg ...\n", getprogname());
13770 97a02382 2023-07-10 thomas exit(1);
13773 97a02382 2023-07-10 thomas static const struct got_error *
13774 97a02382 2023-07-10 thomas cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13776 97a02382 2023-07-10 thomas const struct got_error *err;
13777 97a02382 2023-07-10 thomas struct got_blob_object *blob;
13778 97a02382 2023-07-10 thomas int fd = -1;
13780 97a02382 2023-07-10 thomas fd = got_opentempfd();
13781 97a02382 2023-07-10 thomas if (fd == -1)
13782 97a02382 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
13784 97a02382 2023-07-10 thomas err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
13785 97a02382 2023-07-10 thomas if (err)
13786 97a02382 2023-07-10 thomas goto done;
13788 97a02382 2023-07-10 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
13790 97a02382 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
13791 97a02382 2023-07-10 thomas err = got_error_from_errno("close");
13792 97a02382 2023-07-10 thomas if (blob)
13793 97a02382 2023-07-10 thomas got_object_blob_close(blob);
13794 97a02382 2023-07-10 thomas return err;
13797 97a02382 2023-07-10 thomas static const struct got_error *
13798 97a02382 2023-07-10 thomas cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13800 97a02382 2023-07-10 thomas const struct got_error *err;
13801 97a02382 2023-07-10 thomas struct got_tree_object *tree;
13802 97a02382 2023-07-10 thomas int nentries, i;
13804 97a02382 2023-07-10 thomas err = got_object_open_as_tree(&tree, repo, id);
13805 97a02382 2023-07-10 thomas if (err)
13806 97a02382 2023-07-10 thomas return err;
13808 97a02382 2023-07-10 thomas nentries = got_object_tree_get_nentries(tree);
13809 97a02382 2023-07-10 thomas for (i = 0; i < nentries; i++) {
13810 97a02382 2023-07-10 thomas struct got_tree_entry *te;
13811 97a02382 2023-07-10 thomas char *id_str;
13812 97a02382 2023-07-10 thomas if (sigint_received || sigpipe_received)
13814 97a02382 2023-07-10 thomas te = got_object_tree_get_entry(tree, i);
13815 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
13816 97a02382 2023-07-10 thomas if (err)
13818 97a02382 2023-07-10 thomas fprintf(outfile, "%s %.7o %s\n", id_str,
13819 97a02382 2023-07-10 thomas got_tree_entry_get_mode(te),
13820 97a02382 2023-07-10 thomas got_tree_entry_get_name(te));
13821 97a02382 2023-07-10 thomas free(id_str);
13824 97a02382 2023-07-10 thomas got_object_tree_close(tree);
13825 97a02382 2023-07-10 thomas return err;
13828 97a02382 2023-07-10 thomas static const struct got_error *
13829 97a02382 2023-07-10 thomas cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13831 97a02382 2023-07-10 thomas const struct got_error *err;
13832 97a02382 2023-07-10 thomas struct got_commit_object *commit;
13833 97a02382 2023-07-10 thomas const struct got_object_id_queue *parent_ids;
13834 97a02382 2023-07-10 thomas struct got_object_qid *pid;
13835 97a02382 2023-07-10 thomas char *id_str = NULL;
13836 97a02382 2023-07-10 thomas const char *logmsg = NULL;
13837 97a02382 2023-07-10 thomas char gmtoff[6];
13839 97a02382 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, id);
13840 97a02382 2023-07-10 thomas if (err)
13841 97a02382 2023-07-10 thomas return err;
13843 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
13844 97a02382 2023-07-10 thomas if (err)
13845 97a02382 2023-07-10 thomas goto done;
13847 97a02382 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
13848 97a02382 2023-07-10 thomas parent_ids = got_object_commit_get_parent_ids(commit);
13849 97a02382 2023-07-10 thomas fprintf(outfile, "numparents %d\n",
13850 97a02382 2023-07-10 thomas got_object_commit_get_nparents(commit));
13851 97a02382 2023-07-10 thomas STAILQ_FOREACH(pid, parent_ids, entry) {
13852 97a02382 2023-07-10 thomas char *pid_str;
13853 97a02382 2023-07-10 thomas err = got_object_id_str(&pid_str, &pid->id);
13854 97a02382 2023-07-10 thomas if (err)
13855 97a02382 2023-07-10 thomas goto done;
13856 97a02382 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
13857 97a02382 2023-07-10 thomas free(pid_str);
13859 97a02382 2023-07-10 thomas got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13860 97a02382 2023-07-10 thomas got_object_commit_get_author_gmtoff(commit));
13861 97a02382 2023-07-10 thomas fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
13862 97a02382 2023-07-10 thomas got_object_commit_get_author(commit),
13863 97a02382 2023-07-10 thomas (long long)got_object_commit_get_author_time(commit),
13864 97a02382 2023-07-10 thomas gmtoff);
13866 97a02382 2023-07-10 thomas got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13867 97a02382 2023-07-10 thomas got_object_commit_get_committer_gmtoff(commit));
13868 97a02382 2023-07-10 thomas fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
13869 97a02382 2023-07-10 thomas got_object_commit_get_committer(commit),
13870 97a02382 2023-07-10 thomas (long long)got_object_commit_get_committer_time(commit),
13871 97a02382 2023-07-10 thomas gmtoff);
13873 97a02382 2023-07-10 thomas logmsg = got_object_commit_get_logmsg_raw(commit);
13874 97a02382 2023-07-10 thomas fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
13875 97a02382 2023-07-10 thomas fprintf(outfile, "%s", logmsg);
13877 97a02382 2023-07-10 thomas free(id_str);
13878 97a02382 2023-07-10 thomas got_object_commit_close(commit);
13879 97a02382 2023-07-10 thomas return err;
13882 97a02382 2023-07-10 thomas static const struct got_error *
13883 97a02382 2023-07-10 thomas cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13885 97a02382 2023-07-10 thomas const struct got_error *err;
13886 97a02382 2023-07-10 thomas struct got_tag_object *tag;
13887 97a02382 2023-07-10 thomas char *id_str = NULL;
13888 97a02382 2023-07-10 thomas const char *tagmsg = NULL;
13889 97a02382 2023-07-10 thomas char gmtoff[6];
13891 97a02382 2023-07-10 thomas err = got_object_open_as_tag(&tag, repo, id);
13892 97a02382 2023-07-10 thomas if (err)
13893 97a02382 2023-07-10 thomas return err;
13895 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
13896 97a02382 2023-07-10 thomas if (err)
13897 97a02382 2023-07-10 thomas goto done;
13899 97a02382 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
13901 97a02382 2023-07-10 thomas switch (got_object_tag_get_object_type(tag)) {
13902 97a02382 2023-07-10 thomas case GOT_OBJ_TYPE_BLOB:
13903 97a02382 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13904 97a02382 2023-07-10 thomas GOT_OBJ_LABEL_BLOB);
13906 97a02382 2023-07-10 thomas case GOT_OBJ_TYPE_TREE:
13907 97a02382 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13908 97a02382 2023-07-10 thomas GOT_OBJ_LABEL_TREE);
13910 97a02382 2023-07-10 thomas case GOT_OBJ_TYPE_COMMIT:
13911 97a02382 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13912 97a02382 2023-07-10 thomas GOT_OBJ_LABEL_COMMIT);
13914 97a02382 2023-07-10 thomas case GOT_OBJ_TYPE_TAG:
13915 97a02382 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13916 97a02382 2023-07-10 thomas GOT_OBJ_LABEL_TAG);
13918 97a02382 2023-07-10 thomas default:
13922 97a02382 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
13923 97a02382 2023-07-10 thomas got_object_tag_get_name(tag));
13925 97a02382 2023-07-10 thomas got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13926 97a02382 2023-07-10 thomas got_object_tag_get_tagger_gmtoff(tag));
13927 97a02382 2023-07-10 thomas fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
13928 97a02382 2023-07-10 thomas got_object_tag_get_tagger(tag),
13929 97a02382 2023-07-10 thomas (long long)got_object_tag_get_tagger_time(tag),
13930 97a02382 2023-07-10 thomas gmtoff);
13932 97a02382 2023-07-10 thomas tagmsg = got_object_tag_get_message(tag);
13933 97a02382 2023-07-10 thomas fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
13934 97a02382 2023-07-10 thomas fprintf(outfile, "%s", tagmsg);
13936 97a02382 2023-07-10 thomas free(id_str);
13937 97a02382 2023-07-10 thomas got_object_tag_close(tag);
13938 97a02382 2023-07-10 thomas return err;
13941 97a02382 2023-07-10 thomas static const struct got_error *
13942 97a02382 2023-07-10 thomas cmd_cat(int argc, char *argv[])
13944 97a02382 2023-07-10 thomas const struct got_error *error;
13945 97a02382 2023-07-10 thomas struct got_repository *repo = NULL;
13946 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
13947 97a02382 2023-07-10 thomas char *cwd = NULL, *repo_path = NULL, *label = NULL;
13948 97a02382 2023-07-10 thomas const char *commit_id_str = NULL;
13949 97a02382 2023-07-10 thomas struct got_object_id *id = NULL, *commit_id = NULL;
13950 97a02382 2023-07-10 thomas struct got_commit_object *commit = NULL;
13951 97a02382 2023-07-10 thomas int ch, obj_type, i, force_path = 0;
13952 97a02382 2023-07-10 thomas struct got_reflist_head refs;
13953 97a02382 2023-07-10 thomas int *pack_fds = NULL;
13955 97a02382 2023-07-10 thomas TAILQ_INIT(&refs);
13957 97a02382 2023-07-10 thomas #ifndef PROFILE
13958 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
13959 97a02382 2023-07-10 thomas NULL) == -1)
13960 97a02382 2023-07-10 thomas err(1, "pledge");
13963 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
13964 97a02382 2023-07-10 thomas switch (ch) {
13965 97a02382 2023-07-10 thomas case 'c':
13966 97a02382 2023-07-10 thomas commit_id_str = optarg;
13968 97a02382 2023-07-10 thomas case 'P':
13969 97a02382 2023-07-10 thomas force_path = 1;
13971 97a02382 2023-07-10 thomas case 'r':
13972 97a02382 2023-07-10 thomas repo_path = realpath(optarg, NULL);
13973 97a02382 2023-07-10 thomas if (repo_path == NULL)
13974 97a02382 2023-07-10 thomas return got_error_from_errno2("realpath",
13975 97a02382 2023-07-10 thomas optarg);
13976 97a02382 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
13978 97a02382 2023-07-10 thomas default:
13979 97a02382 2023-07-10 thomas usage_cat();
13980 97a02382 2023-07-10 thomas /* NOTREACHED */
13984 97a02382 2023-07-10 thomas argc -= optind;
13985 97a02382 2023-07-10 thomas argv += optind;
13987 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
13988 97a02382 2023-07-10 thomas if (cwd == NULL) {
13989 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
13990 97a02382 2023-07-10 thomas goto done;
13993 97a02382 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
13994 97a02382 2023-07-10 thomas if (error != NULL)
13995 97a02382 2023-07-10 thomas goto done;
13997 97a02382 2023-07-10 thomas if (repo_path == NULL) {
13998 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
13999 97a02382 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
14000 97a02382 2023-07-10 thomas goto done;
14001 97a02382 2023-07-10 thomas if (worktree) {
14002 97a02382 2023-07-10 thomas repo_path = strdup(
14003 97a02382 2023-07-10 thomas got_worktree_get_repo_path(worktree));
14004 97a02382 2023-07-10 thomas if (repo_path == NULL) {
14005 97a02382 2023-07-10 thomas error = got_error_from_errno("strdup");
14006 97a02382 2023-07-10 thomas goto done;
14009 97a02382 2023-07-10 thomas /* Release work tree lock. */
14010 97a02382 2023-07-10 thomas got_worktree_close(worktree);
14011 97a02382 2023-07-10 thomas worktree = NULL;
14015 97a02382 2023-07-10 thomas if (repo_path == NULL) {
14016 97a02382 2023-07-10 thomas repo_path = strdup(cwd);
14017 97a02382 2023-07-10 thomas if (repo_path == NULL)
14018 97a02382 2023-07-10 thomas return got_error_from_errno("strdup");
14021 97a02382 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
14022 97a02382 2023-07-10 thomas free(repo_path);
14023 97a02382 2023-07-10 thomas if (error != NULL)
14024 97a02382 2023-07-10 thomas goto done;
14026 97a02382 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
14027 97a02382 2023-07-10 thomas if (error)
14028 97a02382 2023-07-10 thomas goto done;
14030 97a02382 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
14031 97a02382 2023-07-10 thomas if (error)
14032 97a02382 2023-07-10 thomas goto done;
14034 97a02382 2023-07-10 thomas if (commit_id_str == NULL)
14035 97a02382 2023-07-10 thomas commit_id_str = GOT_REF_HEAD;
14036 97a02382 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
14037 97a02382 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
14038 97a02382 2023-07-10 thomas if (error)
14039 97a02382 2023-07-10 thomas goto done;
14041 97a02382 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
14042 97a02382 2023-07-10 thomas if (error)
14043 97a02382 2023-07-10 thomas goto done;
14045 97a02382 2023-07-10 thomas for (i = 0; i < argc; i++) {
14046 97a02382 2023-07-10 thomas if (force_path) {
14047 97a02382 2023-07-10 thomas error = got_object_id_by_path(&id, repo, commit,
14048 97a02382 2023-07-10 thomas argv[i]);
14049 97a02382 2023-07-10 thomas if (error)
14051 97a02382 2023-07-10 thomas } else {
14052 97a02382 2023-07-10 thomas error = got_repo_match_object_id(&id, &label, argv[i],
14053 97a02382 2023-07-10 thomas GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
14055 97a02382 2023-07-10 thomas if (error) {
14056 97a02382 2023-07-10 thomas if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
14057 97a02382 2023-07-10 thomas error->code != GOT_ERR_NOT_REF)
14059 97a02382 2023-07-10 thomas error = got_object_id_by_path(&id, repo,
14060 97a02382 2023-07-10 thomas commit, argv[i]);
14061 97a02382 2023-07-10 thomas if (error)
14066 97a02382 2023-07-10 thomas error = got_object_get_type(&obj_type, repo, id);
14067 97a02382 2023-07-10 thomas if (error)
14070 97a02382 2023-07-10 thomas switch (obj_type) {
14071 97a02382 2023-07-10 thomas case GOT_OBJ_TYPE_BLOB:
14072 97a02382 2023-07-10 thomas error = cat_blob(id, repo, stdout);
14074 97a02382 2023-07-10 thomas case GOT_OBJ_TYPE_TREE:
14075 97a02382 2023-07-10 thomas error = cat_tree(id, repo, stdout);
14077 97a02382 2023-07-10 thomas case GOT_OBJ_TYPE_COMMIT:
14078 97a02382 2023-07-10 thomas error = cat_commit(id, repo, stdout);
14080 97a02382 2023-07-10 thomas case GOT_OBJ_TYPE_TAG:
14081 97a02382 2023-07-10 thomas error = cat_tag(id, repo, stdout);
14083 97a02382 2023-07-10 thomas default:
14084 97a02382 2023-07-10 thomas error = got_error(GOT_ERR_OBJ_TYPE);
14087 97a02382 2023-07-10 thomas if (error)
14089 97a02382 2023-07-10 thomas free(label);
14090 97a02382 2023-07-10 thomas label = NULL;
14091 97a02382 2023-07-10 thomas free(id);
14092 97a02382 2023-07-10 thomas id = NULL;
14095 97a02382 2023-07-10 thomas free(label);
14096 97a02382 2023-07-10 thomas free(id);
14097 97a02382 2023-07-10 thomas free(commit_id);
14098 97a02382 2023-07-10 thomas if (commit)
14099 97a02382 2023-07-10 thomas got_object_commit_close(commit);
14100 97a02382 2023-07-10 thomas if (worktree)
14101 97a02382 2023-07-10 thomas got_worktree_close(worktree);
14102 97a02382 2023-07-10 thomas if (repo) {
14103 97a02382 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
14104 97a02382 2023-07-10 thomas if (error == NULL)
14105 97a02382 2023-07-10 thomas error = close_err;
14107 97a02382 2023-07-10 thomas if (pack_fds) {
14108 97a02382 2023-07-10 thomas const struct got_error *pack_err =
14109 97a02382 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
14110 97a02382 2023-07-10 thomas if (error == NULL)
14111 97a02382 2023-07-10 thomas error = pack_err;
14114 97a02382 2023-07-10 thomas got_ref_list_free(&refs);
14115 97a02382 2023-07-10 thomas return error;
14118 97a02382 2023-07-10 thomas __dead static void
14119 97a02382 2023-07-10 thomas usage_info(void)
14121 97a02382 2023-07-10 thomas fprintf(stderr, "usage: %s info [path ...]\n",
14122 97a02382 2023-07-10 thomas getprogname());
14123 97a02382 2023-07-10 thomas exit(1);
14126 97a02382 2023-07-10 thomas static const struct got_error *
14127 97a02382 2023-07-10 thomas print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
14128 97a02382 2023-07-10 thomas struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
14129 97a02382 2023-07-10 thomas struct got_object_id *commit_id)
14131 97a02382 2023-07-10 thomas const struct got_error *err = NULL;
14132 97a02382 2023-07-10 thomas char *id_str = NULL;
14133 97a02382 2023-07-10 thomas char datebuf[128];
14134 97a02382 2023-07-10 thomas struct tm mytm, *tm;
14135 97a02382 2023-07-10 thomas struct got_pathlist_head *paths = arg;
14136 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
14139 97a02382 2023-07-10 thomas * Clear error indication from any of the path arguments which
14140 97a02382 2023-07-10 thomas * would cause this file index entry to be displayed.
14142 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, paths, entry) {
14143 97a02382 2023-07-10 thomas if (got_path_cmp(path, pe->path, strlen(path),
14144 97a02382 2023-07-10 thomas pe->path_len) == 0 ||
14145 97a02382 2023-07-10 thomas got_path_is_child(path, pe->path, pe->path_len))
14146 97a02382 2023-07-10 thomas pe->data = NULL; /* no error */
14149 97a02382 2023-07-10 thomas printf(GOT_COMMIT_SEP_STR);
14150 97a02382 2023-07-10 thomas if (S_ISLNK(mode))
14151 97a02382 2023-07-10 thomas printf("symlink: %s\n", path);
14152 97a02382 2023-07-10 thomas else if (S_ISREG(mode)) {
14153 97a02382 2023-07-10 thomas printf("file: %s\n", path);
14154 97a02382 2023-07-10 thomas printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
14155 97a02382 2023-07-10 thomas } else if (S_ISDIR(mode))
14156 97a02382 2023-07-10 thomas printf("directory: %s\n", path);
14158 97a02382 2023-07-10 thomas printf("something: %s\n", path);
14160 97a02382 2023-07-10 thomas tm = localtime_r(&mtime, &mytm);
14161 97a02382 2023-07-10 thomas if (tm == NULL)
14162 97a02382 2023-07-10 thomas return NULL;
14163 97a02382 2023-07-10 thomas if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
14164 97a02382 2023-07-10 thomas return got_error(GOT_ERR_NO_SPACE);
14165 97a02382 2023-07-10 thomas printf("timestamp: %s\n", datebuf);
14167 97a02382 2023-07-10 thomas if (blob_id) {
14168 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, blob_id);
14169 97a02382 2023-07-10 thomas if (err)
14170 97a02382 2023-07-10 thomas return err;
14171 97a02382 2023-07-10 thomas printf("based on blob: %s\n", id_str);
14172 97a02382 2023-07-10 thomas free(id_str);
14175 97a02382 2023-07-10 thomas if (staged_blob_id) {
14176 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, staged_blob_id);
14177 97a02382 2023-07-10 thomas if (err)
14178 97a02382 2023-07-10 thomas return err;
14179 97a02382 2023-07-10 thomas printf("based on staged blob: %s\n", id_str);
14180 97a02382 2023-07-10 thomas free(id_str);
14183 97a02382 2023-07-10 thomas if (commit_id) {
14184 97a02382 2023-07-10 thomas err = got_object_id_str(&id_str, commit_id);
14185 97a02382 2023-07-10 thomas if (err)
14186 97a02382 2023-07-10 thomas return err;
14187 97a02382 2023-07-10 thomas printf("based on commit: %s\n", id_str);
14188 97a02382 2023-07-10 thomas free(id_str);
14191 97a02382 2023-07-10 thomas return NULL;
14194 97a02382 2023-07-10 thomas static const struct got_error *
14195 97a02382 2023-07-10 thomas cmd_info(int argc, char *argv[])
14197 97a02382 2023-07-10 thomas const struct got_error *error = NULL;
14198 97a02382 2023-07-10 thomas struct got_worktree *worktree = NULL;
14199 97a02382 2023-07-10 thomas char *cwd = NULL, *id_str = NULL;
14200 97a02382 2023-07-10 thomas struct got_pathlist_head paths;
14201 97a02382 2023-07-10 thomas char *uuidstr = NULL;
14202 97a02382 2023-07-10 thomas int ch, show_files = 0;
14204 97a02382 2023-07-10 thomas TAILQ_INIT(&paths);
14206 97a02382 2023-07-10 thomas #ifndef PROFILE
14207 97a02382 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14208 97a02382 2023-07-10 thomas NULL) == -1)
14209 97a02382 2023-07-10 thomas err(1, "pledge");
14212 97a02382 2023-07-10 thomas while ((ch = getopt(argc, argv, "")) != -1) {
14213 97a02382 2023-07-10 thomas switch (ch) {
14214 97a02382 2023-07-10 thomas default:
14215 97a02382 2023-07-10 thomas usage_info();
14216 97a02382 2023-07-10 thomas /* NOTREACHED */
14220 97a02382 2023-07-10 thomas argc -= optind;
14221 97a02382 2023-07-10 thomas argv += optind;
14223 97a02382 2023-07-10 thomas cwd = getcwd(NULL, 0);
14224 97a02382 2023-07-10 thomas if (cwd == NULL) {
14225 97a02382 2023-07-10 thomas error = got_error_from_errno("getcwd");
14226 97a02382 2023-07-10 thomas goto done;
14229 97a02382 2023-07-10 thomas error = got_worktree_open(&worktree, cwd);
14230 97a02382 2023-07-10 thomas if (error) {
14231 97a02382 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
14232 97a02382 2023-07-10 thomas error = wrap_not_worktree_error(error, "info", cwd);
14233 97a02382 2023-07-10 thomas goto done;
14236 97a02382 2023-07-10 thomas #ifndef PROFILE
14237 97a02382 2023-07-10 thomas /* Remove "wpath cpath proc exec sendfd" promises. */
14238 97a02382 2023-07-10 thomas if (pledge("stdio rpath flock unveil", NULL) == -1)
14239 97a02382 2023-07-10 thomas err(1, "pledge");
14241 97a02382 2023-07-10 thomas error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
14242 97a02382 2023-07-10 thomas if (error)
14243 97a02382 2023-07-10 thomas goto done;
14245 97a02382 2023-07-10 thomas if (argc >= 1) {
14246 97a02382 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv,
14247 97a02382 2023-07-10 thomas worktree);
14248 97a02382 2023-07-10 thomas if (error)
14249 97a02382 2023-07-10 thomas goto done;
14250 97a02382 2023-07-10 thomas show_files = 1;
14253 97a02382 2023-07-10 thomas error = got_object_id_str(&id_str,
14254 97a02382 2023-07-10 thomas got_worktree_get_base_commit_id(worktree));
14255 97a02382 2023-07-10 thomas if (error)
14256 97a02382 2023-07-10 thomas goto done;
14258 97a02382 2023-07-10 thomas error = got_worktree_get_uuid(&uuidstr, worktree);
14259 97a02382 2023-07-10 thomas if (error)
14260 97a02382 2023-07-10 thomas goto done;
14262 97a02382 2023-07-10 thomas printf("work tree: %s\n", got_worktree_get_root_path(worktree));
14263 97a02382 2023-07-10 thomas printf("work tree base commit: %s\n", id_str);
14264 97a02382 2023-07-10 thomas printf("work tree path prefix: %s\n",
14265 97a02382 2023-07-10 thomas got_worktree_get_path_prefix(worktree));
14266 97a02382 2023-07-10 thomas printf("work tree branch reference: %s\n",
14267 97a02382 2023-07-10 thomas got_worktree_get_head_ref_name(worktree));
14268 97a02382 2023-07-10 thomas printf("work tree UUID: %s\n", uuidstr);
14269 97a02382 2023-07-10 thomas printf("repository: %s\n", got_worktree_get_repo_path(worktree));
14271 97a02382 2023-07-10 thomas if (show_files) {
14272 97a02382 2023-07-10 thomas struct got_pathlist_entry *pe;
14273 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, &paths, entry) {
14274 97a02382 2023-07-10 thomas if (pe->path_len == 0)
14275 97a02382 2023-07-10 thomas continue;
14277 97a02382 2023-07-10 thomas * Assume this path will fail. This will be corrected
14278 97a02382 2023-07-10 thomas * in print_path_info() in case the path does suceeed.
14280 97a02382 2023-07-10 thomas pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
14282 97a02382 2023-07-10 thomas error = got_worktree_path_info(worktree, &paths,
14283 97a02382 2023-07-10 thomas print_path_info, &paths, check_cancelled, NULL);
14284 97a02382 2023-07-10 thomas if (error)
14285 97a02382 2023-07-10 thomas goto done;
14286 97a02382 2023-07-10 thomas TAILQ_FOREACH(pe, &paths, entry) {
14287 97a02382 2023-07-10 thomas if (pe->data != NULL) {
14288 97a02382 2023-07-10 thomas const struct got_error *perr;
14290 97a02382 2023-07-10 thomas perr = pe->data;
14291 97a02382 2023-07-10 thomas error = got_error_fmt(perr->code, "%s",
14292 97a02382 2023-07-10 thomas pe->path);
14298 97a02382 2023-07-10 thomas if (worktree)
14299 97a02382 2023-07-10 thomas got_worktree_close(worktree);
14300 97a02382 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
14301 97a02382 2023-07-10 thomas free(cwd);
14302 97a02382 2023-07-10 thomas free(id_str);
14303 97a02382 2023-07-10 thomas free(uuidstr);
14304 97a02382 2023-07-10 thomas return error;