2 795b88cb 2023-07-10 thomas * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 795b88cb 2023-07-10 thomas * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 795b88cb 2023-07-10 thomas * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 795b88cb 2023-07-10 thomas * Copyright (C) 2023 Josh Rickmar <jrick@zettaport.com>
7 795b88cb 2023-07-10 thomas * Permission to use, copy, modify, and distribute this software for any
8 795b88cb 2023-07-10 thomas * purpose with or without fee is hereby granted, provided that the above
9 795b88cb 2023-07-10 thomas * copyright notice and this permission notice appear in all copies.
11 795b88cb 2023-07-10 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 795b88cb 2023-07-10 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 795b88cb 2023-07-10 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 795b88cb 2023-07-10 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 795b88cb 2023-07-10 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 795b88cb 2023-07-10 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 795b88cb 2023-07-10 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 b7eff127 2023-07-10 thomas #include "got_compat.h"
22 795b88cb 2023-07-10 thomas #include <sys/time.h>
23 795b88cb 2023-07-10 thomas #include <sys/types.h>
24 795b88cb 2023-07-10 thomas #include <sys/stat.h>
25 795b88cb 2023-07-10 thomas #include <sys/wait.h>
27 795b88cb 2023-07-10 thomas #include <err.h>
28 795b88cb 2023-07-10 thomas #include <errno.h>
29 795b88cb 2023-07-10 thomas #include <fcntl.h>
30 795b88cb 2023-07-10 thomas #include <limits.h>
31 795b88cb 2023-07-10 thomas #include <locale.h>
32 795b88cb 2023-07-10 thomas #include <ctype.h>
33 795b88cb 2023-07-10 thomas #include <signal.h>
34 795b88cb 2023-07-10 thomas #include <stdio.h>
35 795b88cb 2023-07-10 thomas #include <stdlib.h>
36 795b88cb 2023-07-10 thomas #include <string.h>
37 795b88cb 2023-07-10 thomas #include <unistd.h>
38 795b88cb 2023-07-10 thomas #include <libgen.h>
39 795b88cb 2023-07-10 thomas #include <time.h>
40 795b88cb 2023-07-10 thomas #include <paths.h>
41 795b88cb 2023-07-10 thomas #include <regex.h>
42 795b88cb 2023-07-10 thomas #include <getopt.h>
44 795b88cb 2023-07-10 thomas #include "got_version.h"
45 795b88cb 2023-07-10 thomas #include "got_error.h"
46 795b88cb 2023-07-10 thomas #include "got_object.h"
47 795b88cb 2023-07-10 thomas #include "got_reference.h"
48 795b88cb 2023-07-10 thomas #include "got_repository.h"
49 795b88cb 2023-07-10 thomas #include "got_path.h"
50 795b88cb 2023-07-10 thomas #include "got_cancel.h"
51 795b88cb 2023-07-10 thomas #include "got_worktree.h"
52 795b88cb 2023-07-10 thomas #include "got_worktree_cvg.h"
53 795b88cb 2023-07-10 thomas #include "got_diff.h"
54 795b88cb 2023-07-10 thomas #include "got_commit_graph.h"
55 795b88cb 2023-07-10 thomas #include "got_fetch.h"
56 795b88cb 2023-07-10 thomas #include "got_send.h"
57 795b88cb 2023-07-10 thomas #include "got_blame.h"
58 795b88cb 2023-07-10 thomas #include "got_privsep.h"
59 795b88cb 2023-07-10 thomas #include "got_opentemp.h"
60 795b88cb 2023-07-10 thomas #include "got_gotconfig.h"
61 795b88cb 2023-07-10 thomas #include "got_dial.h"
62 795b88cb 2023-07-10 thomas #include "got_patch.h"
63 795b88cb 2023-07-10 thomas #include "got_sigs.h"
64 795b88cb 2023-07-10 thomas #include "got_date.h"
66 795b88cb 2023-07-10 thomas #ifndef nitems
67 795b88cb 2023-07-10 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 a8938c43 2024-03-19 thomas #ifndef GOT_DEFAULT_EDITOR
71 a8938c43 2024-03-19 thomas #define GOT_DEFAULT_EDITOR "/usr/bin/vi"
74 795b88cb 2023-07-10 thomas static volatile sig_atomic_t sigint_received;
75 795b88cb 2023-07-10 thomas static volatile sig_atomic_t sigpipe_received;
77 795b88cb 2023-07-10 thomas static void
78 795b88cb 2023-07-10 thomas catch_sigint(int signo)
80 795b88cb 2023-07-10 thomas sigint_received = 1;
83 795b88cb 2023-07-10 thomas static void
84 795b88cb 2023-07-10 thomas catch_sigpipe(int signo)
86 795b88cb 2023-07-10 thomas sigpipe_received = 1;
90 795b88cb 2023-07-10 thomas struct got_cmd {
91 795b88cb 2023-07-10 thomas const char *cmd_name;
92 795b88cb 2023-07-10 thomas const struct got_error *(*cmd_main)(int, char *[]);
93 795b88cb 2023-07-10 thomas void (*cmd_usage)(void);
94 795b88cb 2023-07-10 thomas const char *cmd_alias;
97 795b88cb 2023-07-10 thomas __dead static void usage(int, int);
98 795b88cb 2023-07-10 thomas __dead static void usage_import(void);
99 795b88cb 2023-07-10 thomas __dead static void usage_clone(void);
100 795b88cb 2023-07-10 thomas __dead static void usage_checkout(void);
101 795b88cb 2023-07-10 thomas __dead static void usage_update(void);
102 795b88cb 2023-07-10 thomas __dead static void usage_log(void);
103 795b88cb 2023-07-10 thomas __dead static void usage_diff(void);
104 795b88cb 2023-07-10 thomas __dead static void usage_blame(void);
105 795b88cb 2023-07-10 thomas __dead static void usage_tree(void);
106 795b88cb 2023-07-10 thomas __dead static void usage_status(void);
107 795b88cb 2023-07-10 thomas __dead static void usage_tag(void);
108 795b88cb 2023-07-10 thomas __dead static void usage_add(void);
109 795b88cb 2023-07-10 thomas __dead static void usage_remove(void);
110 795b88cb 2023-07-10 thomas __dead static void usage_patch(void);
111 795b88cb 2023-07-10 thomas __dead static void usage_revert(void);
112 795b88cb 2023-07-10 thomas __dead static void usage_commit(void);
113 795b88cb 2023-07-10 thomas __dead static void usage_cherrypick(void);
114 795b88cb 2023-07-10 thomas __dead static void usage_backout(void);
115 795b88cb 2023-07-10 thomas __dead static void usage_cat(void);
116 795b88cb 2023-07-10 thomas __dead static void usage_info(void);
118 795b88cb 2023-07-10 thomas static const struct got_error* cmd_import(int, char *[]);
119 795b88cb 2023-07-10 thomas static const struct got_error* cmd_clone(int, char *[]);
120 795b88cb 2023-07-10 thomas static const struct got_error* cmd_checkout(int, char *[]);
121 795b88cb 2023-07-10 thomas static const struct got_error* cmd_update(int, char *[]);
122 795b88cb 2023-07-10 thomas static const struct got_error* cmd_log(int, char *[]);
123 795b88cb 2023-07-10 thomas static const struct got_error* cmd_diff(int, char *[]);
124 795b88cb 2023-07-10 thomas static const struct got_error* cmd_blame(int, char *[]);
125 795b88cb 2023-07-10 thomas static const struct got_error* cmd_tree(int, char *[]);
126 795b88cb 2023-07-10 thomas static const struct got_error* cmd_status(int, char *[]);
127 795b88cb 2023-07-10 thomas static const struct got_error* cmd_tag(int, char *[]);
128 795b88cb 2023-07-10 thomas static const struct got_error* cmd_add(int, char *[]);
129 795b88cb 2023-07-10 thomas static const struct got_error* cmd_remove(int, char *[]);
130 795b88cb 2023-07-10 thomas static const struct got_error* cmd_patch(int, char *[]);
131 795b88cb 2023-07-10 thomas static const struct got_error* cmd_revert(int, char *[]);
132 795b88cb 2023-07-10 thomas static const struct got_error* cmd_commit(int, char *[]);
133 795b88cb 2023-07-10 thomas static const struct got_error* cmd_cherrypick(int, char *[]);
134 795b88cb 2023-07-10 thomas static const struct got_error* cmd_backout(int, char *[]);
135 795b88cb 2023-07-10 thomas static const struct got_error* cmd_cat(int, char *[]);
136 795b88cb 2023-07-10 thomas static const struct got_error* cmd_info(int, char *[]);
138 795b88cb 2023-07-10 thomas static const struct got_cmd got_commands[] = {
139 795b88cb 2023-07-10 thomas { "import", cmd_import, usage_import, "im" },
140 795b88cb 2023-07-10 thomas { "clone", cmd_clone, usage_clone, "cl" },
141 795b88cb 2023-07-10 thomas { "checkout", cmd_checkout, usage_checkout, "co" },
142 795b88cb 2023-07-10 thomas { "update", cmd_update, usage_update, "up" },
143 795b88cb 2023-07-10 thomas { "log", cmd_log, usage_log, "" },
144 795b88cb 2023-07-10 thomas { "diff", cmd_diff, usage_diff, "di" },
145 795b88cb 2023-07-10 thomas { "blame", cmd_blame, usage_blame, "bl" },
146 795b88cb 2023-07-10 thomas { "tree", cmd_tree, usage_tree, "tr" },
147 795b88cb 2023-07-10 thomas { "status", cmd_status, usage_status, "st" },
148 795b88cb 2023-07-10 thomas { "tag", cmd_tag, usage_tag, "" },
149 795b88cb 2023-07-10 thomas { "add", cmd_add, usage_add, "" },
150 795b88cb 2023-07-10 thomas { "remove", cmd_remove, usage_remove, "rm" },
151 795b88cb 2023-07-10 thomas { "patch", cmd_patch, usage_patch, "pa" },
152 795b88cb 2023-07-10 thomas { "revert", cmd_revert, usage_revert, "rv" },
153 795b88cb 2023-07-10 thomas { "commit", cmd_commit, usage_commit, "ci" },
154 795b88cb 2023-07-10 thomas { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
155 795b88cb 2023-07-10 thomas { "backout", cmd_backout, usage_backout, "bo" },
156 795b88cb 2023-07-10 thomas { "cat", cmd_cat, usage_cat, "" },
157 795b88cb 2023-07-10 thomas { "info", cmd_info, usage_info, "" },
160 795b88cb 2023-07-10 thomas static void
161 795b88cb 2023-07-10 thomas list_commands(FILE *fp)
163 795b88cb 2023-07-10 thomas size_t i;
165 795b88cb 2023-07-10 thomas fprintf(fp, "commands:");
166 795b88cb 2023-07-10 thomas for (i = 0; i < nitems(got_commands); i++) {
167 795b88cb 2023-07-10 thomas const struct got_cmd *cmd = &got_commands[i];
168 795b88cb 2023-07-10 thomas fprintf(fp, " %s", cmd->cmd_name);
170 795b88cb 2023-07-10 thomas fputc('\n', fp);
173 795b88cb 2023-07-10 thomas __dead static void
174 795b88cb 2023-07-10 thomas option_conflict(char a, char b)
176 795b88cb 2023-07-10 thomas errx(1, "-%c and -%c options are mutually exclusive", a, b);
180 795b88cb 2023-07-10 thomas main(int argc, char *argv[])
182 795b88cb 2023-07-10 thomas const struct got_cmd *cmd;
183 795b88cb 2023-07-10 thomas size_t i;
185 795b88cb 2023-07-10 thomas int hflag = 0, Vflag = 0;
186 795b88cb 2023-07-10 thomas static const struct option longopts[] = {
187 795b88cb 2023-07-10 thomas { "version", no_argument, NULL, 'V' },
188 795b88cb 2023-07-10 thomas { NULL, 0, NULL, 0 }
191 795b88cb 2023-07-10 thomas setlocale(LC_CTYPE, "");
193 795b88cb 2023-07-10 thomas while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
194 795b88cb 2023-07-10 thomas switch (ch) {
195 795b88cb 2023-07-10 thomas case 'h':
196 795b88cb 2023-07-10 thomas hflag = 1;
198 795b88cb 2023-07-10 thomas case 'V':
199 795b88cb 2023-07-10 thomas Vflag = 1;
202 795b88cb 2023-07-10 thomas usage(hflag, 1);
203 795b88cb 2023-07-10 thomas /* NOTREACHED */
207 795b88cb 2023-07-10 thomas argc -= optind;
208 795b88cb 2023-07-10 thomas argv += optind;
209 795b88cb 2023-07-10 thomas optind = 1;
210 795b88cb 2023-07-10 thomas optreset = 1;
212 795b88cb 2023-07-10 thomas if (Vflag) {
213 795b88cb 2023-07-10 thomas got_version_print_str();
214 795b88cb 2023-07-10 thomas return 0;
217 795b88cb 2023-07-10 thomas if (argc <= 0)
218 795b88cb 2023-07-10 thomas usage(hflag, hflag ? 0 : 1);
220 795b88cb 2023-07-10 thomas signal(SIGINT, catch_sigint);
221 795b88cb 2023-07-10 thomas signal(SIGPIPE, catch_sigpipe);
223 795b88cb 2023-07-10 thomas for (i = 0; i < nitems(got_commands); i++) {
224 795b88cb 2023-07-10 thomas const struct got_error *error;
226 795b88cb 2023-07-10 thomas cmd = &got_commands[i];
228 795b88cb 2023-07-10 thomas if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
229 795b88cb 2023-07-10 thomas strcmp(cmd->cmd_alias, argv[0]) != 0)
230 795b88cb 2023-07-10 thomas continue;
232 795b88cb 2023-07-10 thomas if (hflag)
233 795b88cb 2023-07-10 thomas cmd->cmd_usage();
235 795b88cb 2023-07-10 thomas error = cmd->cmd_main(argc, argv);
236 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_CANCELLED &&
237 795b88cb 2023-07-10 thomas error->code != GOT_ERR_PRIVSEP_EXIT &&
238 795b88cb 2023-07-10 thomas !(sigpipe_received &&
239 795b88cb 2023-07-10 thomas error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
240 795b88cb 2023-07-10 thomas !(sigint_received &&
241 795b88cb 2023-07-10 thomas error->code == GOT_ERR_ERRNO && errno == EINTR)) {
242 795b88cb 2023-07-10 thomas fflush(stdout);
243 795b88cb 2023-07-10 thomas fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
244 795b88cb 2023-07-10 thomas return 1;
247 795b88cb 2023-07-10 thomas return 0;
250 795b88cb 2023-07-10 thomas fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
251 795b88cb 2023-07-10 thomas list_commands(stderr);
252 795b88cb 2023-07-10 thomas return 1;
255 795b88cb 2023-07-10 thomas __dead static void
256 795b88cb 2023-07-10 thomas usage(int hflag, int status)
258 795b88cb 2023-07-10 thomas FILE *fp = (status == 0) ? stdout : stderr;
260 795b88cb 2023-07-10 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
261 795b88cb 2023-07-10 thomas getprogname());
262 795b88cb 2023-07-10 thomas if (hflag)
263 795b88cb 2023-07-10 thomas list_commands(fp);
264 795b88cb 2023-07-10 thomas exit(status);
267 795b88cb 2023-07-10 thomas static const struct got_error *
268 795b88cb 2023-07-10 thomas get_editor(char **abspath)
270 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
271 795b88cb 2023-07-10 thomas const char *editor;
273 795b88cb 2023-07-10 thomas *abspath = NULL;
275 795b88cb 2023-07-10 thomas editor = getenv("VISUAL");
276 795b88cb 2023-07-10 thomas if (editor == NULL)
277 795b88cb 2023-07-10 thomas editor = getenv("EDITOR");
279 795b88cb 2023-07-10 thomas if (editor) {
280 795b88cb 2023-07-10 thomas err = got_path_find_prog(abspath, editor);
282 795b88cb 2023-07-10 thomas return err;
285 795b88cb 2023-07-10 thomas if (*abspath == NULL) {
286 a8938c43 2024-03-19 thomas *abspath = strdup(GOT_DEFAULT_EDITOR);
287 795b88cb 2023-07-10 thomas if (*abspath == NULL)
288 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
291 795b88cb 2023-07-10 thomas return NULL;
294 795b88cb 2023-07-10 thomas static const struct got_error *
295 795b88cb 2023-07-10 thomas apply_unveil(const char *repo_path, int repo_read_only,
296 795b88cb 2023-07-10 thomas const char *worktree_path)
298 795b88cb 2023-07-10 thomas const struct got_error *err;
300 795b88cb 2023-07-10 thomas #ifdef PROFILE
301 795b88cb 2023-07-10 thomas if (unveil("gmon.out", "rwc") != 0)
302 795b88cb 2023-07-10 thomas return got_error_from_errno2("unveil", "gmon.out");
304 795b88cb 2023-07-10 thomas if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
305 795b88cb 2023-07-10 thomas return got_error_from_errno2("unveil", repo_path);
307 795b88cb 2023-07-10 thomas if (worktree_path && unveil(worktree_path, "rwc") != 0)
308 795b88cb 2023-07-10 thomas return got_error_from_errno2("unveil", worktree_path);
310 795b88cb 2023-07-10 thomas if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
311 795b88cb 2023-07-10 thomas return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
313 795b88cb 2023-07-10 thomas err = got_privsep_unveil_exec_helpers();
314 795b88cb 2023-07-10 thomas if (err != NULL)
315 795b88cb 2023-07-10 thomas return err;
317 795b88cb 2023-07-10 thomas if (unveil(NULL, NULL) != 0)
318 795b88cb 2023-07-10 thomas return got_error_from_errno("unveil");
320 795b88cb 2023-07-10 thomas return NULL;
323 795b88cb 2023-07-10 thomas __dead static void
324 795b88cb 2023-07-10 thomas usage_import(void)
326 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s import [-b branch] [-I pattern] [-m message] "
327 795b88cb 2023-07-10 thomas "[-r repository-path] directory\n", getprogname());
331 795b88cb 2023-07-10 thomas static int
332 795b88cb 2023-07-10 thomas spawn_editor(const char *editor, const char *file)
334 795b88cb 2023-07-10 thomas pid_t pid;
335 795b88cb 2023-07-10 thomas sig_t sighup, sigint, sigquit;
336 795b88cb 2023-07-10 thomas int st = -1;
338 795b88cb 2023-07-10 thomas sighup = signal(SIGHUP, SIG_IGN);
339 795b88cb 2023-07-10 thomas sigint = signal(SIGINT, SIG_IGN);
340 795b88cb 2023-07-10 thomas sigquit = signal(SIGQUIT, SIG_IGN);
342 795b88cb 2023-07-10 thomas switch (pid = fork()) {
344 795b88cb 2023-07-10 thomas goto doneediting;
346 795b88cb 2023-07-10 thomas execl(editor, editor, file, (char *)NULL);
347 795b88cb 2023-07-10 thomas _exit(127);
350 795b88cb 2023-07-10 thomas while (waitpid(pid, &st, 0) == -1)
351 795b88cb 2023-07-10 thomas if (errno != EINTR)
354 795b88cb 2023-07-10 thomas doneediting:
355 795b88cb 2023-07-10 thomas (void)signal(SIGHUP, sighup);
356 795b88cb 2023-07-10 thomas (void)signal(SIGINT, sigint);
357 795b88cb 2023-07-10 thomas (void)signal(SIGQUIT, sigquit);
359 795b88cb 2023-07-10 thomas if (!WIFEXITED(st)) {
360 795b88cb 2023-07-10 thomas errno = EINTR;
361 795b88cb 2023-07-10 thomas return -1;
364 795b88cb 2023-07-10 thomas return WEXITSTATUS(st);
367 795b88cb 2023-07-10 thomas static const struct got_error *
368 795b88cb 2023-07-10 thomas read_logmsg(char **logmsg, size_t *len, FILE *fp, size_t filesize)
370 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
371 795b88cb 2023-07-10 thomas char *line = NULL;
372 795b88cb 2023-07-10 thomas size_t linesize = 0;
374 795b88cb 2023-07-10 thomas *logmsg = NULL;
375 795b88cb 2023-07-10 thomas *len = 0;
377 795b88cb 2023-07-10 thomas if (fseeko(fp, 0L, SEEK_SET) == -1)
378 795b88cb 2023-07-10 thomas return got_error_from_errno("fseeko");
380 795b88cb 2023-07-10 thomas *logmsg = malloc(filesize + 1);
381 795b88cb 2023-07-10 thomas if (*logmsg == NULL)
382 795b88cb 2023-07-10 thomas return got_error_from_errno("malloc");
383 795b88cb 2023-07-10 thomas (*logmsg)[0] = '\0';
385 795b88cb 2023-07-10 thomas while (getline(&line, &linesize, fp) != -1) {
386 795b88cb 2023-07-10 thomas if (line[0] == '#' || (*len == 0 && line[0] == '\n'))
387 795b88cb 2023-07-10 thomas continue; /* remove comments and leading empty lines */
388 795b88cb 2023-07-10 thomas *len = strlcat(*logmsg, line, filesize + 1);
389 795b88cb 2023-07-10 thomas if (*len >= filesize + 1) {
390 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_NO_SPACE);
391 795b88cb 2023-07-10 thomas goto done;
394 795b88cb 2023-07-10 thomas if (ferror(fp)) {
395 795b88cb 2023-07-10 thomas err = got_ferror(fp, GOT_ERR_IO);
396 795b88cb 2023-07-10 thomas goto done;
399 795b88cb 2023-07-10 thomas while (*len > 0 && (*logmsg)[*len - 1] == '\n') {
400 795b88cb 2023-07-10 thomas (*logmsg)[*len - 1] = '\0';
401 795b88cb 2023-07-10 thomas (*len)--;
404 795b88cb 2023-07-10 thomas free(line);
405 795b88cb 2023-07-10 thomas if (err) {
406 795b88cb 2023-07-10 thomas free(*logmsg);
407 795b88cb 2023-07-10 thomas *logmsg = NULL;
408 795b88cb 2023-07-10 thomas *len = 0;
410 795b88cb 2023-07-10 thomas return err;
413 795b88cb 2023-07-10 thomas static const struct got_error *
414 795b88cb 2023-07-10 thomas edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
415 795b88cb 2023-07-10 thomas const char *initial_content, size_t initial_content_len,
416 795b88cb 2023-07-10 thomas int require_modification)
418 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
419 795b88cb 2023-07-10 thomas struct stat st, st2;
420 795b88cb 2023-07-10 thomas FILE *fp = NULL;
421 795b88cb 2023-07-10 thomas size_t logmsg_len;
423 795b88cb 2023-07-10 thomas *logmsg = NULL;
425 795b88cb 2023-07-10 thomas if (stat(logmsg_path, &st) == -1)
426 795b88cb 2023-07-10 thomas return got_error_from_errno2("stat", logmsg_path);
428 795b88cb 2023-07-10 thomas if (spawn_editor(editor, logmsg_path) == -1)
429 795b88cb 2023-07-10 thomas return got_error_from_errno("failed spawning editor");
431 795b88cb 2023-07-10 thomas if (require_modification) {
432 795b88cb 2023-07-10 thomas struct timespec timeout;
434 795b88cb 2023-07-10 thomas timeout.tv_sec = 0;
435 795b88cb 2023-07-10 thomas timeout.tv_nsec = 1;
436 795b88cb 2023-07-10 thomas nanosleep(&timeout, NULL);
439 795b88cb 2023-07-10 thomas if (stat(logmsg_path, &st2) == -1)
440 5541355f 2023-07-17 thomas return got_error_from_errno2("stat", logmsg_path);
442 795b88cb 2023-07-10 thomas if (require_modification && st.st_size == st2.st_size &&
443 795b88cb 2023-07-10 thomas timespeccmp(&st.st_mtim, &st2.st_mtim, ==))
444 795b88cb 2023-07-10 thomas return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
445 795b88cb 2023-07-10 thomas "no changes made to commit message, aborting");
447 795b88cb 2023-07-10 thomas fp = fopen(logmsg_path, "re");
448 795b88cb 2023-07-10 thomas if (fp == NULL) {
449 795b88cb 2023-07-10 thomas err = got_error_from_errno("fopen");
450 795b88cb 2023-07-10 thomas goto done;
453 795b88cb 2023-07-10 thomas /* strip comments and leading/trailing newlines */
454 795b88cb 2023-07-10 thomas err = read_logmsg(logmsg, &logmsg_len, fp, st2.st_size);
456 795b88cb 2023-07-10 thomas goto done;
457 795b88cb 2023-07-10 thomas if (logmsg_len == 0) {
458 795b88cb 2023-07-10 thomas err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
459 795b88cb 2023-07-10 thomas "commit message cannot be empty, aborting");
460 795b88cb 2023-07-10 thomas goto done;
463 795b88cb 2023-07-10 thomas if (fp && fclose(fp) == EOF && err == NULL)
464 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
465 795b88cb 2023-07-10 thomas if (err) {
466 795b88cb 2023-07-10 thomas free(*logmsg);
467 795b88cb 2023-07-10 thomas *logmsg = NULL;
469 795b88cb 2023-07-10 thomas return err;
472 795b88cb 2023-07-10 thomas static const struct got_error *
473 795b88cb 2023-07-10 thomas collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
474 795b88cb 2023-07-10 thomas const char *path_dir, const char *branch_name)
476 795b88cb 2023-07-10 thomas char *initial_content = NULL;
477 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
478 795b88cb 2023-07-10 thomas int initial_content_len;
479 795b88cb 2023-07-10 thomas int fd = -1;
481 795b88cb 2023-07-10 thomas initial_content_len = asprintf(&initial_content,
482 795b88cb 2023-07-10 thomas "\n# %s to be imported to branch %s\n", path_dir,
483 795b88cb 2023-07-10 thomas branch_name);
484 795b88cb 2023-07-10 thomas if (initial_content_len == -1)
485 795b88cb 2023-07-10 thomas return got_error_from_errno("asprintf");
487 795b88cb 2023-07-10 thomas err = got_opentemp_named_fd(logmsg_path, &fd,
488 795b88cb 2023-07-10 thomas GOT_TMPDIR_STR "/got-importmsg", "");
490 795b88cb 2023-07-10 thomas goto done;
492 795b88cb 2023-07-10 thomas if (write(fd, initial_content, initial_content_len) == -1) {
493 795b88cb 2023-07-10 thomas err = got_error_from_errno2("write", *logmsg_path);
494 795b88cb 2023-07-10 thomas goto done;
496 795b88cb 2023-07-10 thomas if (close(fd) == -1) {
497 795b88cb 2023-07-10 thomas err = got_error_from_errno2("close", *logmsg_path);
498 795b88cb 2023-07-10 thomas goto done;
502 795b88cb 2023-07-10 thomas err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
503 795b88cb 2023-07-10 thomas initial_content_len, 1);
505 795b88cb 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
506 795b88cb 2023-07-10 thomas err = got_error_from_errno2("close", *logmsg_path);
507 795b88cb 2023-07-10 thomas free(initial_content);
508 795b88cb 2023-07-10 thomas if (err) {
509 795b88cb 2023-07-10 thomas free(*logmsg_path);
510 795b88cb 2023-07-10 thomas *logmsg_path = NULL;
512 795b88cb 2023-07-10 thomas return err;
515 795b88cb 2023-07-10 thomas static const struct got_error *
516 795b88cb 2023-07-10 thomas import_progress(void *arg, const char *path)
518 795b88cb 2023-07-10 thomas printf("A %s\n", path);
519 795b88cb 2023-07-10 thomas return NULL;
522 795b88cb 2023-07-10 thomas static const struct got_error *
523 795b88cb 2023-07-10 thomas valid_author(const char *author)
525 795b88cb 2023-07-10 thomas const char *email = author;
528 795b88cb 2023-07-10 thomas * Git' expects the author (or committer) to be in the form
529 795b88cb 2023-07-10 thomas * "name <email>", which are mostly free form (see the
530 795b88cb 2023-07-10 thomas * "committer" description in git-fast-import(1)). We're only
531 795b88cb 2023-07-10 thomas * doing this to avoid git's object parser breaking on commits
532 795b88cb 2023-07-10 thomas * we create.
535 795b88cb 2023-07-10 thomas while (*author && *author != '\n' && *author != '<' && *author != '>')
536 795b88cb 2023-07-10 thomas author++;
537 795b88cb 2023-07-10 thomas if (author != email && *author == '<' && *(author - 1) != ' ')
538 795b88cb 2023-07-10 thomas return got_error_fmt(GOT_ERR_COMMIT_BAD_AUTHOR, "%s: space "
539 795b88cb 2023-07-10 thomas "between author name and email required", email);
540 795b88cb 2023-07-10 thomas if (*author++ != '<')
541 795b88cb 2023-07-10 thomas return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
542 795b88cb 2023-07-10 thomas while (*author && *author != '\n' && *author != '<' && *author != '>')
543 795b88cb 2023-07-10 thomas author++;
544 795b88cb 2023-07-10 thomas if (strcmp(author, ">") != 0)
545 795b88cb 2023-07-10 thomas return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
546 795b88cb 2023-07-10 thomas return NULL;
549 795b88cb 2023-07-10 thomas static const struct got_error *
550 795b88cb 2023-07-10 thomas get_author(char **author, struct got_repository *repo,
551 795b88cb 2023-07-10 thomas struct got_worktree *worktree)
553 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
554 795b88cb 2023-07-10 thomas const char *got_author = NULL, *name, *email;
555 795b88cb 2023-07-10 thomas const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
557 795b88cb 2023-07-10 thomas *author = NULL;
559 795b88cb 2023-07-10 thomas if (worktree)
560 795b88cb 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
561 795b88cb 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
564 795b88cb 2023-07-10 thomas * Priority of potential author information sources, from most
565 795b88cb 2023-07-10 thomas * significant to least significant:
566 795b88cb 2023-07-10 thomas * 1) work tree's .got/got.conf file
567 795b88cb 2023-07-10 thomas * 2) repository's got.conf file
568 795b88cb 2023-07-10 thomas * 3) repository's git config file
569 795b88cb 2023-07-10 thomas * 4) environment variables
570 795b88cb 2023-07-10 thomas * 5) global git config files (in user's home directory or /etc)
573 795b88cb 2023-07-10 thomas if (worktree_conf)
574 795b88cb 2023-07-10 thomas got_author = got_gotconfig_get_author(worktree_conf);
575 795b88cb 2023-07-10 thomas if (got_author == NULL)
576 795b88cb 2023-07-10 thomas got_author = got_gotconfig_get_author(repo_conf);
577 795b88cb 2023-07-10 thomas if (got_author == NULL) {
578 795b88cb 2023-07-10 thomas name = got_repo_get_gitconfig_author_name(repo);
579 795b88cb 2023-07-10 thomas email = got_repo_get_gitconfig_author_email(repo);
580 795b88cb 2023-07-10 thomas if (name && email) {
581 795b88cb 2023-07-10 thomas if (asprintf(author, "%s <%s>", name, email) == -1)
582 795b88cb 2023-07-10 thomas return got_error_from_errno("asprintf");
583 795b88cb 2023-07-10 thomas return NULL;
586 795b88cb 2023-07-10 thomas got_author = getenv("GOT_AUTHOR");
587 795b88cb 2023-07-10 thomas if (got_author == NULL) {
588 795b88cb 2023-07-10 thomas name = got_repo_get_global_gitconfig_author_name(repo);
589 795b88cb 2023-07-10 thomas email = got_repo_get_global_gitconfig_author_email(
591 795b88cb 2023-07-10 thomas if (name && email) {
592 795b88cb 2023-07-10 thomas if (asprintf(author, "%s <%s>", name, email)
594 795b88cb 2023-07-10 thomas return got_error_from_errno("asprintf");
595 795b88cb 2023-07-10 thomas return NULL;
597 795b88cb 2023-07-10 thomas /* TODO: Look up user in password database? */
598 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
602 795b88cb 2023-07-10 thomas *author = strdup(got_author);
603 795b88cb 2023-07-10 thomas if (*author == NULL)
604 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
606 795b88cb 2023-07-10 thomas err = valid_author(*author);
607 795b88cb 2023-07-10 thomas if (err) {
608 795b88cb 2023-07-10 thomas free(*author);
609 795b88cb 2023-07-10 thomas *author = NULL;
611 795b88cb 2023-07-10 thomas return err;
614 795b88cb 2023-07-10 thomas static const struct got_error *
615 795b88cb 2023-07-10 thomas get_allowed_signers(char **allowed_signers, struct got_repository *repo,
616 795b88cb 2023-07-10 thomas struct got_worktree *worktree)
618 795b88cb 2023-07-10 thomas const char *got_allowed_signers = NULL;
619 795b88cb 2023-07-10 thomas const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
621 795b88cb 2023-07-10 thomas *allowed_signers = NULL;
623 795b88cb 2023-07-10 thomas if (worktree)
624 795b88cb 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
625 795b88cb 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
628 795b88cb 2023-07-10 thomas * Priority of potential author information sources, from most
629 795b88cb 2023-07-10 thomas * significant to least significant:
630 795b88cb 2023-07-10 thomas * 1) work tree's .got/got.conf file
631 795b88cb 2023-07-10 thomas * 2) repository's got.conf file
634 795b88cb 2023-07-10 thomas if (worktree_conf)
635 795b88cb 2023-07-10 thomas got_allowed_signers = got_gotconfig_get_allowed_signers_file(
636 795b88cb 2023-07-10 thomas worktree_conf);
637 795b88cb 2023-07-10 thomas if (got_allowed_signers == NULL)
638 795b88cb 2023-07-10 thomas got_allowed_signers = got_gotconfig_get_allowed_signers_file(
639 795b88cb 2023-07-10 thomas repo_conf);
641 795b88cb 2023-07-10 thomas if (got_allowed_signers) {
642 795b88cb 2023-07-10 thomas *allowed_signers = strdup(got_allowed_signers);
643 795b88cb 2023-07-10 thomas if (*allowed_signers == NULL)
644 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
646 795b88cb 2023-07-10 thomas return NULL;
649 795b88cb 2023-07-10 thomas static const struct got_error *
650 795b88cb 2023-07-10 thomas get_revoked_signers(char **revoked_signers, struct got_repository *repo,
651 795b88cb 2023-07-10 thomas struct got_worktree *worktree)
653 795b88cb 2023-07-10 thomas const char *got_revoked_signers = NULL;
654 795b88cb 2023-07-10 thomas const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
656 795b88cb 2023-07-10 thomas *revoked_signers = NULL;
658 795b88cb 2023-07-10 thomas if (worktree)
659 795b88cb 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
660 795b88cb 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
663 795b88cb 2023-07-10 thomas * Priority of potential author information sources, from most
664 795b88cb 2023-07-10 thomas * significant to least significant:
665 795b88cb 2023-07-10 thomas * 1) work tree's .got/got.conf file
666 795b88cb 2023-07-10 thomas * 2) repository's got.conf file
669 795b88cb 2023-07-10 thomas if (worktree_conf)
670 795b88cb 2023-07-10 thomas got_revoked_signers = got_gotconfig_get_revoked_signers_file(
671 795b88cb 2023-07-10 thomas worktree_conf);
672 795b88cb 2023-07-10 thomas if (got_revoked_signers == NULL)
673 795b88cb 2023-07-10 thomas got_revoked_signers = got_gotconfig_get_revoked_signers_file(
674 795b88cb 2023-07-10 thomas repo_conf);
676 795b88cb 2023-07-10 thomas if (got_revoked_signers) {
677 795b88cb 2023-07-10 thomas *revoked_signers = strdup(got_revoked_signers);
678 795b88cb 2023-07-10 thomas if (*revoked_signers == NULL)
679 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
681 795b88cb 2023-07-10 thomas return NULL;
684 795b88cb 2023-07-10 thomas static const char *
685 795b88cb 2023-07-10 thomas get_signer_id(struct got_repository *repo, struct got_worktree *worktree)
687 795b88cb 2023-07-10 thomas const char *got_signer_id = NULL;
688 795b88cb 2023-07-10 thomas const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
690 795b88cb 2023-07-10 thomas if (worktree)
691 795b88cb 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
692 795b88cb 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
695 795b88cb 2023-07-10 thomas * Priority of potential author information sources, from most
696 795b88cb 2023-07-10 thomas * significant to least significant:
697 795b88cb 2023-07-10 thomas * 1) work tree's .got/got.conf file
698 795b88cb 2023-07-10 thomas * 2) repository's got.conf file
701 795b88cb 2023-07-10 thomas if (worktree_conf)
702 795b88cb 2023-07-10 thomas got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
703 795b88cb 2023-07-10 thomas if (got_signer_id == NULL)
704 795b88cb 2023-07-10 thomas got_signer_id = got_gotconfig_get_signer_id(repo_conf);
706 795b88cb 2023-07-10 thomas return got_signer_id;
709 795b88cb 2023-07-10 thomas static const struct got_error *
710 795b88cb 2023-07-10 thomas get_gitconfig_path(char **gitconfig_path)
712 795b88cb 2023-07-10 thomas const char *homedir = getenv("HOME");
714 795b88cb 2023-07-10 thomas *gitconfig_path = NULL;
715 795b88cb 2023-07-10 thomas if (homedir) {
716 795b88cb 2023-07-10 thomas if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
717 795b88cb 2023-07-10 thomas return got_error_from_errno("asprintf");
720 795b88cb 2023-07-10 thomas return NULL;
723 795b88cb 2023-07-10 thomas static const struct got_error *
724 795b88cb 2023-07-10 thomas cmd_import(int argc, char *argv[])
726 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
727 795b88cb 2023-07-10 thomas char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
728 795b88cb 2023-07-10 thomas char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
729 795b88cb 2023-07-10 thomas const char *branch_name = NULL;
730 795b88cb 2023-07-10 thomas char *id_str = NULL, *logmsg_path = NULL;
731 795b88cb 2023-07-10 thomas char refname[PATH_MAX] = "refs/heads/";
732 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
733 795b88cb 2023-07-10 thomas struct got_reference *branch_ref = NULL, *head_ref = NULL;
734 795b88cb 2023-07-10 thomas struct got_object_id *new_commit_id = NULL;
735 795b88cb 2023-07-10 thomas int ch, n = 0;
736 795b88cb 2023-07-10 thomas struct got_pathlist_head ignores;
737 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
738 795b88cb 2023-07-10 thomas int preserve_logmsg = 0;
739 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
741 795b88cb 2023-07-10 thomas TAILQ_INIT(&ignores);
743 795b88cb 2023-07-10 thomas #ifndef PROFILE
744 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
745 795b88cb 2023-07-10 thomas "unveil",
746 795b88cb 2023-07-10 thomas NULL) == -1)
747 795b88cb 2023-07-10 thomas err(1, "pledge");
750 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "b:I:m:r:")) != -1) {
751 795b88cb 2023-07-10 thomas switch (ch) {
752 795b88cb 2023-07-10 thomas case 'b':
753 795b88cb 2023-07-10 thomas branch_name = optarg;
755 795b88cb 2023-07-10 thomas case 'I':
756 795b88cb 2023-07-10 thomas if (optarg[0] == '\0')
758 795b88cb 2023-07-10 thomas error = got_pathlist_insert(&pe, &ignores, optarg,
760 795b88cb 2023-07-10 thomas if (error)
761 795b88cb 2023-07-10 thomas goto done;
763 795b88cb 2023-07-10 thomas case 'm':
764 795b88cb 2023-07-10 thomas logmsg = strdup(optarg);
765 795b88cb 2023-07-10 thomas if (logmsg == NULL) {
766 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
767 795b88cb 2023-07-10 thomas goto done;
770 795b88cb 2023-07-10 thomas case 'r':
771 795b88cb 2023-07-10 thomas repo_path = realpath(optarg, NULL);
772 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
773 795b88cb 2023-07-10 thomas error = got_error_from_errno2("realpath",
775 795b88cb 2023-07-10 thomas goto done;
779 795b88cb 2023-07-10 thomas usage_import();
780 795b88cb 2023-07-10 thomas /* NOTREACHED */
784 795b88cb 2023-07-10 thomas argc -= optind;
785 795b88cb 2023-07-10 thomas argv += optind;
787 795b88cb 2023-07-10 thomas if (argc != 1)
788 795b88cb 2023-07-10 thomas usage_import();
790 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
791 795b88cb 2023-07-10 thomas repo_path = getcwd(NULL, 0);
792 795b88cb 2023-07-10 thomas if (repo_path == NULL)
793 795b88cb 2023-07-10 thomas return got_error_from_errno("getcwd");
795 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
796 795b88cb 2023-07-10 thomas error = get_gitconfig_path(&gitconfig_path);
797 795b88cb 2023-07-10 thomas if (error)
798 795b88cb 2023-07-10 thomas goto done;
799 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
800 795b88cb 2023-07-10 thomas if (error != NULL)
801 795b88cb 2023-07-10 thomas goto done;
802 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
803 795b88cb 2023-07-10 thomas if (error)
804 795b88cb 2023-07-10 thomas goto done;
806 795b88cb 2023-07-10 thomas error = get_author(&author, repo, NULL);
807 795b88cb 2023-07-10 thomas if (error)
808 795b88cb 2023-07-10 thomas return error;
811 795b88cb 2023-07-10 thomas * Don't let the user create a branch name with a leading '-'.
812 795b88cb 2023-07-10 thomas * While technically a valid reference name, this case is usually
813 795b88cb 2023-07-10 thomas * an unintended typo.
815 795b88cb 2023-07-10 thomas if (branch_name && branch_name[0] == '-')
816 795b88cb 2023-07-10 thomas return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
818 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
819 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_REF)
820 795b88cb 2023-07-10 thomas goto done;
822 795b88cb 2023-07-10 thomas if (branch_name)
823 795b88cb 2023-07-10 thomas n = strlcat(refname, branch_name, sizeof(refname));
824 795b88cb 2023-07-10 thomas else if (head_ref && got_ref_is_symbolic(head_ref))
825 795b88cb 2023-07-10 thomas n = strlcpy(refname, got_ref_get_symref_target(head_ref),
826 795b88cb 2023-07-10 thomas sizeof(refname));
828 795b88cb 2023-07-10 thomas n = strlcat(refname, "main", sizeof(refname));
829 795b88cb 2023-07-10 thomas if (n >= sizeof(refname)) {
830 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_NO_SPACE);
831 795b88cb 2023-07-10 thomas goto done;
834 795b88cb 2023-07-10 thomas error = got_ref_open(&branch_ref, repo, refname, 0);
835 795b88cb 2023-07-10 thomas if (error) {
836 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_NOT_REF)
837 795b88cb 2023-07-10 thomas goto done;
839 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
840 795b88cb 2023-07-10 thomas "import target branch already exists");
841 795b88cb 2023-07-10 thomas goto done;
844 795b88cb 2023-07-10 thomas path_dir = realpath(argv[0], NULL);
845 795b88cb 2023-07-10 thomas if (path_dir == NULL) {
846 795b88cb 2023-07-10 thomas error = got_error_from_errno2("realpath", argv[0]);
847 795b88cb 2023-07-10 thomas goto done;
849 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(path_dir);
852 795b88cb 2023-07-10 thomas * unveil(2) traverses exec(2); if an editor is used we have
853 795b88cb 2023-07-10 thomas * to apply unveil after the log message has been written.
855 795b88cb 2023-07-10 thomas if (logmsg == NULL || *logmsg == '\0') {
856 795b88cb 2023-07-10 thomas error = get_editor(&editor);
857 795b88cb 2023-07-10 thomas if (error)
858 795b88cb 2023-07-10 thomas goto done;
859 795b88cb 2023-07-10 thomas free(logmsg);
860 795b88cb 2023-07-10 thomas error = collect_import_msg(&logmsg, &logmsg_path, editor,
861 795b88cb 2023-07-10 thomas path_dir, refname);
862 795b88cb 2023-07-10 thomas if (error) {
863 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
864 795b88cb 2023-07-10 thomas logmsg_path != NULL)
865 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
866 795b88cb 2023-07-10 thomas goto done;
870 795b88cb 2023-07-10 thomas if (unveil(path_dir, "r") != 0) {
871 795b88cb 2023-07-10 thomas error = got_error_from_errno2("unveil", path_dir);
872 795b88cb 2023-07-10 thomas if (logmsg_path)
873 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
874 795b88cb 2023-07-10 thomas goto done;
877 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0, NULL);
878 795b88cb 2023-07-10 thomas if (error) {
879 795b88cb 2023-07-10 thomas if (logmsg_path)
880 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
881 795b88cb 2023-07-10 thomas goto done;
884 795b88cb 2023-07-10 thomas error = got_repo_import(&new_commit_id, path_dir, logmsg,
885 795b88cb 2023-07-10 thomas author, &ignores, repo, import_progress, NULL);
886 795b88cb 2023-07-10 thomas if (error) {
887 795b88cb 2023-07-10 thomas if (logmsg_path)
888 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
889 795b88cb 2023-07-10 thomas goto done;
892 795b88cb 2023-07-10 thomas error = got_ref_alloc(&branch_ref, refname, new_commit_id);
893 795b88cb 2023-07-10 thomas if (error) {
894 795b88cb 2023-07-10 thomas if (logmsg_path)
895 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
896 795b88cb 2023-07-10 thomas goto done;
899 795b88cb 2023-07-10 thomas error = got_ref_write(branch_ref, repo);
900 795b88cb 2023-07-10 thomas if (error) {
901 795b88cb 2023-07-10 thomas if (logmsg_path)
902 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
903 795b88cb 2023-07-10 thomas goto done;
906 795b88cb 2023-07-10 thomas error = got_object_id_str(&id_str, new_commit_id);
907 795b88cb 2023-07-10 thomas if (error) {
908 795b88cb 2023-07-10 thomas if (logmsg_path)
909 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
910 795b88cb 2023-07-10 thomas goto done;
913 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
914 795b88cb 2023-07-10 thomas if (error) {
915 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_NOT_REF) {
916 795b88cb 2023-07-10 thomas if (logmsg_path)
917 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
918 795b88cb 2023-07-10 thomas goto done;
921 795b88cb 2023-07-10 thomas error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
922 795b88cb 2023-07-10 thomas branch_ref);
923 795b88cb 2023-07-10 thomas if (error) {
924 795b88cb 2023-07-10 thomas if (logmsg_path)
925 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
926 795b88cb 2023-07-10 thomas goto done;
929 795b88cb 2023-07-10 thomas error = got_ref_write(head_ref, repo);
930 795b88cb 2023-07-10 thomas if (error) {
931 795b88cb 2023-07-10 thomas if (logmsg_path)
932 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
933 795b88cb 2023-07-10 thomas goto done;
937 795b88cb 2023-07-10 thomas printf("Created branch %s with commit %s\n",
938 795b88cb 2023-07-10 thomas got_ref_get_name(branch_ref), id_str);
940 795b88cb 2023-07-10 thomas if (pack_fds) {
941 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
942 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
943 795b88cb 2023-07-10 thomas if (error == NULL)
944 795b88cb 2023-07-10 thomas error = pack_err;
946 795b88cb 2023-07-10 thomas if (repo) {
947 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
948 795b88cb 2023-07-10 thomas if (error == NULL)
949 795b88cb 2023-07-10 thomas error = close_err;
951 795b88cb 2023-07-10 thomas if (preserve_logmsg) {
952 795b88cb 2023-07-10 thomas fprintf(stderr, "%s: log message preserved in %s\n",
953 795b88cb 2023-07-10 thomas getprogname(), logmsg_path);
954 795b88cb 2023-07-10 thomas } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
955 795b88cb 2023-07-10 thomas error = got_error_from_errno2("unlink", logmsg_path);
956 795b88cb 2023-07-10 thomas free(logmsg);
957 795b88cb 2023-07-10 thomas free(logmsg_path);
958 795b88cb 2023-07-10 thomas free(repo_path);
959 795b88cb 2023-07-10 thomas free(editor);
960 795b88cb 2023-07-10 thomas free(new_commit_id);
961 795b88cb 2023-07-10 thomas free(id_str);
962 795b88cb 2023-07-10 thomas free(author);
963 795b88cb 2023-07-10 thomas free(gitconfig_path);
964 795b88cb 2023-07-10 thomas if (branch_ref)
965 795b88cb 2023-07-10 thomas got_ref_close(branch_ref);
966 795b88cb 2023-07-10 thomas if (head_ref)
967 795b88cb 2023-07-10 thomas got_ref_close(head_ref);
968 795b88cb 2023-07-10 thomas return error;
971 795b88cb 2023-07-10 thomas __dead static void
972 795b88cb 2023-07-10 thomas usage_clone(void)
974 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s clone [-almqv] [-b branch] [-R reference] "
975 795b88cb 2023-07-10 thomas "repository-URL [directory]\n", getprogname());
979 795b88cb 2023-07-10 thomas struct got_fetch_progress_arg {
980 795b88cb 2023-07-10 thomas char last_scaled_size[FMT_SCALED_STRSIZE];
981 795b88cb 2023-07-10 thomas int last_p_indexed;
982 795b88cb 2023-07-10 thomas int last_p_resolved;
983 795b88cb 2023-07-10 thomas int verbosity;
985 795b88cb 2023-07-10 thomas struct got_repository *repo;
987 795b88cb 2023-07-10 thomas int create_configs;
988 795b88cb 2023-07-10 thomas int configs_created;
990 795b88cb 2023-07-10 thomas struct got_pathlist_head *symrefs;
991 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_branches;
992 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_refs;
993 795b88cb 2023-07-10 thomas const char *proto;
994 795b88cb 2023-07-10 thomas const char *host;
995 795b88cb 2023-07-10 thomas const char *port;
996 795b88cb 2023-07-10 thomas const char *remote_repo_path;
997 795b88cb 2023-07-10 thomas const char *git_url;
998 795b88cb 2023-07-10 thomas int fetch_all_branches;
999 795b88cb 2023-07-10 thomas int mirror_references;
1000 795b88cb 2023-07-10 thomas } config_info;
1003 795b88cb 2023-07-10 thomas /* XXX forward declaration */
1004 795b88cb 2023-07-10 thomas static const struct got_error *
1005 795b88cb 2023-07-10 thomas create_config_files(const char *proto, const char *host, const char *port,
1006 795b88cb 2023-07-10 thomas const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1007 795b88cb 2023-07-10 thomas int mirror_references, struct got_pathlist_head *symrefs,
1008 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_branches,
1009 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1011 795b88cb 2023-07-10 thomas static const struct got_error *
1012 795b88cb 2023-07-10 thomas fetch_progress(void *arg, const char *message, off_t packfile_size,
1013 795b88cb 2023-07-10 thomas int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1015 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
1016 795b88cb 2023-07-10 thomas struct got_fetch_progress_arg *a = arg;
1017 795b88cb 2023-07-10 thomas char scaled_size[FMT_SCALED_STRSIZE];
1018 795b88cb 2023-07-10 thomas int p_indexed, p_resolved;
1019 795b88cb 2023-07-10 thomas int print_size = 0, print_indexed = 0, print_resolved = 0;
1022 795b88cb 2023-07-10 thomas * In order to allow a failed clone to be resumed with 'got fetch'
1023 795b88cb 2023-07-10 thomas * we try to create configuration files as soon as possible.
1024 795b88cb 2023-07-10 thomas * Once the server has sent information about its default branch
1025 795b88cb 2023-07-10 thomas * we have all required information.
1027 795b88cb 2023-07-10 thomas if (a->create_configs && !a->configs_created &&
1028 795b88cb 2023-07-10 thomas !TAILQ_EMPTY(a->config_info.symrefs)) {
1029 795b88cb 2023-07-10 thomas err = create_config_files(a->config_info.proto,
1030 795b88cb 2023-07-10 thomas a->config_info.host, a->config_info.port,
1031 795b88cb 2023-07-10 thomas a->config_info.remote_repo_path,
1032 795b88cb 2023-07-10 thomas a->config_info.git_url,
1033 795b88cb 2023-07-10 thomas a->config_info.fetch_all_branches,
1034 795b88cb 2023-07-10 thomas a->config_info.mirror_references,
1035 795b88cb 2023-07-10 thomas a->config_info.symrefs,
1036 795b88cb 2023-07-10 thomas a->config_info.wanted_branches,
1037 795b88cb 2023-07-10 thomas a->config_info.wanted_refs, a->repo);
1038 795b88cb 2023-07-10 thomas if (err)
1039 795b88cb 2023-07-10 thomas return err;
1040 795b88cb 2023-07-10 thomas a->configs_created = 1;
1043 795b88cb 2023-07-10 thomas if (a->verbosity < 0)
1044 795b88cb 2023-07-10 thomas return NULL;
1046 795b88cb 2023-07-10 thomas if (message && message[0] != '\0') {
1047 795b88cb 2023-07-10 thomas printf("\rserver: %s", message);
1048 795b88cb 2023-07-10 thomas fflush(stdout);
1049 795b88cb 2023-07-10 thomas return NULL;
1052 795b88cb 2023-07-10 thomas if (packfile_size > 0 || nobj_indexed > 0) {
1053 795b88cb 2023-07-10 thomas if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1054 795b88cb 2023-07-10 thomas (a->last_scaled_size[0] == '\0' ||
1055 795b88cb 2023-07-10 thomas strcmp(scaled_size, a->last_scaled_size)) != 0) {
1056 795b88cb 2023-07-10 thomas print_size = 1;
1057 795b88cb 2023-07-10 thomas if (strlcpy(a->last_scaled_size, scaled_size,
1058 795b88cb 2023-07-10 thomas FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1059 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_NO_SPACE);
1061 795b88cb 2023-07-10 thomas if (nobj_indexed > 0) {
1062 795b88cb 2023-07-10 thomas p_indexed = (nobj_indexed * 100) / nobj_total;
1063 795b88cb 2023-07-10 thomas if (p_indexed != a->last_p_indexed) {
1064 795b88cb 2023-07-10 thomas a->last_p_indexed = p_indexed;
1065 795b88cb 2023-07-10 thomas print_indexed = 1;
1066 795b88cb 2023-07-10 thomas print_size = 1;
1069 795b88cb 2023-07-10 thomas if (nobj_resolved > 0) {
1070 795b88cb 2023-07-10 thomas p_resolved = (nobj_resolved * 100) /
1071 795b88cb 2023-07-10 thomas (nobj_total - nobj_loose);
1072 795b88cb 2023-07-10 thomas if (p_resolved != a->last_p_resolved) {
1073 795b88cb 2023-07-10 thomas a->last_p_resolved = p_resolved;
1074 795b88cb 2023-07-10 thomas print_resolved = 1;
1075 795b88cb 2023-07-10 thomas print_indexed = 1;
1076 795b88cb 2023-07-10 thomas print_size = 1;
1081 795b88cb 2023-07-10 thomas if (print_size || print_indexed || print_resolved)
1082 795b88cb 2023-07-10 thomas printf("\r");
1083 795b88cb 2023-07-10 thomas if (print_size)
1084 795b88cb 2023-07-10 thomas printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1085 795b88cb 2023-07-10 thomas if (print_indexed)
1086 795b88cb 2023-07-10 thomas printf("; indexing %d%%", p_indexed);
1087 795b88cb 2023-07-10 thomas if (print_resolved)
1088 795b88cb 2023-07-10 thomas printf("; resolving deltas %d%%", p_resolved);
1089 795b88cb 2023-07-10 thomas if (print_size || print_indexed || print_resolved)
1090 795b88cb 2023-07-10 thomas fflush(stdout);
1092 795b88cb 2023-07-10 thomas return NULL;
1095 795b88cb 2023-07-10 thomas static const struct got_error *
1096 795b88cb 2023-07-10 thomas create_symref(const char *refname, struct got_reference *target_ref,
1097 795b88cb 2023-07-10 thomas int verbosity, struct got_repository *repo)
1099 795b88cb 2023-07-10 thomas const struct got_error *err;
1100 795b88cb 2023-07-10 thomas struct got_reference *head_symref;
1102 795b88cb 2023-07-10 thomas err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1103 795b88cb 2023-07-10 thomas if (err)
1104 795b88cb 2023-07-10 thomas return err;
1106 795b88cb 2023-07-10 thomas err = got_ref_write(head_symref, repo);
1107 795b88cb 2023-07-10 thomas if (err == NULL && verbosity > 0) {
1108 795b88cb 2023-07-10 thomas printf("Created reference %s: %s\n", GOT_REF_HEAD,
1109 795b88cb 2023-07-10 thomas got_ref_get_name(target_ref));
1111 795b88cb 2023-07-10 thomas got_ref_close(head_symref);
1112 795b88cb 2023-07-10 thomas return err;
1115 795b88cb 2023-07-10 thomas static const struct got_error *
1116 795b88cb 2023-07-10 thomas list_remote_refs(struct got_pathlist_head *symrefs,
1117 795b88cb 2023-07-10 thomas struct got_pathlist_head *refs)
1119 795b88cb 2023-07-10 thomas const struct got_error *err;
1120 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1122 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, symrefs, entry) {
1123 795b88cb 2023-07-10 thomas const char *refname = pe->path;
1124 795b88cb 2023-07-10 thomas const char *targetref = pe->data;
1126 795b88cb 2023-07-10 thomas printf("%s: %s\n", refname, targetref);
1129 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, refs, entry) {
1130 795b88cb 2023-07-10 thomas const char *refname = pe->path;
1131 795b88cb 2023-07-10 thomas struct got_object_id *id = pe->data;
1132 795b88cb 2023-07-10 thomas char *id_str;
1134 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, id);
1135 795b88cb 2023-07-10 thomas if (err)
1136 795b88cb 2023-07-10 thomas return err;
1137 795b88cb 2023-07-10 thomas printf("%s: %s\n", refname, id_str);
1138 795b88cb 2023-07-10 thomas free(id_str);
1141 795b88cb 2023-07-10 thomas return NULL;
1144 795b88cb 2023-07-10 thomas static const struct got_error *
1145 795b88cb 2023-07-10 thomas create_ref(const char *refname, struct got_object_id *id,
1146 795b88cb 2023-07-10 thomas int verbosity, struct got_repository *repo)
1148 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
1149 795b88cb 2023-07-10 thomas struct got_reference *ref;
1150 795b88cb 2023-07-10 thomas char *id_str;
1152 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, id);
1153 795b88cb 2023-07-10 thomas if (err)
1154 795b88cb 2023-07-10 thomas return err;
1156 795b88cb 2023-07-10 thomas err = got_ref_alloc(&ref, refname, id);
1157 795b88cb 2023-07-10 thomas if (err)
1158 795b88cb 2023-07-10 thomas goto done;
1160 795b88cb 2023-07-10 thomas err = got_ref_write(ref, repo);
1161 795b88cb 2023-07-10 thomas got_ref_close(ref);
1163 795b88cb 2023-07-10 thomas if (err == NULL && verbosity >= 0)
1164 795b88cb 2023-07-10 thomas printf("Created reference %s: %s\n", refname, id_str);
1166 795b88cb 2023-07-10 thomas free(id_str);
1167 795b88cb 2023-07-10 thomas return err;
1170 795b88cb 2023-07-10 thomas static int
1171 795b88cb 2023-07-10 thomas match_wanted_ref(const char *refname, const char *wanted_ref)
1173 795b88cb 2023-07-10 thomas if (strncmp(refname, "refs/", 5) != 0)
1174 795b88cb 2023-07-10 thomas return 0;
1175 795b88cb 2023-07-10 thomas refname += 5;
1178 795b88cb 2023-07-10 thomas * Prevent fetching of references that won't make any
1179 795b88cb 2023-07-10 thomas * sense outside of the remote repository's context.
1181 795b88cb 2023-07-10 thomas if (strncmp(refname, "got/", 4) == 0)
1182 795b88cb 2023-07-10 thomas return 0;
1183 795b88cb 2023-07-10 thomas if (strncmp(refname, "remotes/", 8) == 0)
1184 795b88cb 2023-07-10 thomas return 0;
1186 795b88cb 2023-07-10 thomas if (strncmp(wanted_ref, "refs/", 5) == 0)
1187 795b88cb 2023-07-10 thomas wanted_ref += 5;
1189 795b88cb 2023-07-10 thomas /* Allow prefix match. */
1190 795b88cb 2023-07-10 thomas if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1191 795b88cb 2023-07-10 thomas return 1;
1193 795b88cb 2023-07-10 thomas /* Allow exact match. */
1194 795b88cb 2023-07-10 thomas return (strcmp(refname, wanted_ref) == 0);
1197 795b88cb 2023-07-10 thomas static int
1198 795b88cb 2023-07-10 thomas is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1200 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1202 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, wanted_refs, entry) {
1203 795b88cb 2023-07-10 thomas if (match_wanted_ref(refname, pe->path))
1204 795b88cb 2023-07-10 thomas return 1;
1207 795b88cb 2023-07-10 thomas return 0;
1210 795b88cb 2023-07-10 thomas static const struct got_error *
1211 795b88cb 2023-07-10 thomas create_wanted_ref(const char *refname, struct got_object_id *id,
1212 795b88cb 2023-07-10 thomas const char *remote_repo_name, int verbosity, struct got_repository *repo)
1214 795b88cb 2023-07-10 thomas const struct got_error *err;
1215 795b88cb 2023-07-10 thomas char *remote_refname;
1217 795b88cb 2023-07-10 thomas if (strncmp("refs/", refname, 5) == 0)
1218 795b88cb 2023-07-10 thomas refname += 5;
1220 795b88cb 2023-07-10 thomas if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1221 795b88cb 2023-07-10 thomas remote_repo_name, refname) == -1)
1222 795b88cb 2023-07-10 thomas return got_error_from_errno("asprintf");
1224 795b88cb 2023-07-10 thomas err = create_ref(remote_refname, id, verbosity, repo);
1225 795b88cb 2023-07-10 thomas free(remote_refname);
1226 795b88cb 2023-07-10 thomas return err;
1229 795b88cb 2023-07-10 thomas static const struct got_error *
1230 795b88cb 2023-07-10 thomas create_gotconfig(const char *proto, const char *host, const char *port,
1231 795b88cb 2023-07-10 thomas const char *remote_repo_path, const char *default_branch,
1232 795b88cb 2023-07-10 thomas int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1233 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_refs, int mirror_references,
1234 795b88cb 2023-07-10 thomas struct got_repository *repo)
1236 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
1237 795b88cb 2023-07-10 thomas char *gotconfig_path = NULL;
1238 795b88cb 2023-07-10 thomas char *gotconfig = NULL;
1239 795b88cb 2023-07-10 thomas FILE *gotconfig_file = NULL;
1240 795b88cb 2023-07-10 thomas const char *branchname = NULL;
1241 795b88cb 2023-07-10 thomas char *branches = NULL, *refs = NULL;
1242 795b88cb 2023-07-10 thomas ssize_t n;
1244 795b88cb 2023-07-10 thomas if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1245 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1246 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, wanted_branches, entry) {
1247 795b88cb 2023-07-10 thomas char *s;
1248 795b88cb 2023-07-10 thomas branchname = pe->path;
1249 795b88cb 2023-07-10 thomas if (strncmp(branchname, "refs/heads/", 11) == 0)
1250 795b88cb 2023-07-10 thomas branchname += 11;
1251 795b88cb 2023-07-10 thomas if (asprintf(&s, "%s\"%s\" ",
1252 795b88cb 2023-07-10 thomas branches ? branches : "", branchname) == -1) {
1253 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1254 795b88cb 2023-07-10 thomas goto done;
1256 795b88cb 2023-07-10 thomas free(branches);
1257 795b88cb 2023-07-10 thomas branches = s;
1259 795b88cb 2023-07-10 thomas } else if (!fetch_all_branches && default_branch) {
1260 795b88cb 2023-07-10 thomas branchname = default_branch;
1261 795b88cb 2023-07-10 thomas if (strncmp(branchname, "refs/heads/", 11) == 0)
1262 795b88cb 2023-07-10 thomas branchname += 11;
1263 795b88cb 2023-07-10 thomas if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1264 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1265 795b88cb 2023-07-10 thomas goto done;
1268 795b88cb 2023-07-10 thomas if (!TAILQ_EMPTY(wanted_refs)) {
1269 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1270 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, wanted_refs, entry) {
1271 795b88cb 2023-07-10 thomas char *s;
1272 795b88cb 2023-07-10 thomas const char *refname = pe->path;
1273 795b88cb 2023-07-10 thomas if (strncmp(refname, "refs/", 5) == 0)
1274 795b88cb 2023-07-10 thomas branchname += 5;
1275 795b88cb 2023-07-10 thomas if (asprintf(&s, "%s\"%s\" ",
1276 795b88cb 2023-07-10 thomas refs ? refs : "", refname) == -1) {
1277 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1278 795b88cb 2023-07-10 thomas goto done;
1280 795b88cb 2023-07-10 thomas free(refs);
1281 795b88cb 2023-07-10 thomas refs = s;
1285 795b88cb 2023-07-10 thomas /* Create got.conf(5). */
1286 795b88cb 2023-07-10 thomas gotconfig_path = got_repo_get_path_gotconfig(repo);
1287 795b88cb 2023-07-10 thomas if (gotconfig_path == NULL) {
1288 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_repo_get_path_gotconfig");
1289 795b88cb 2023-07-10 thomas goto done;
1291 795b88cb 2023-07-10 thomas gotconfig_file = fopen(gotconfig_path, "ae");
1292 795b88cb 2023-07-10 thomas if (gotconfig_file == NULL) {
1293 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fopen", gotconfig_path);
1294 795b88cb 2023-07-10 thomas goto done;
1296 795b88cb 2023-07-10 thomas if (asprintf(&gotconfig,
1297 795b88cb 2023-07-10 thomas "remote \"%s\" {\n"
1298 795b88cb 2023-07-10 thomas "\tserver %s\n"
1299 795b88cb 2023-07-10 thomas "\tprotocol %s\n"
1300 795b88cb 2023-07-10 thomas "%s%s%s"
1301 795b88cb 2023-07-10 thomas "\trepository \"%s\"\n"
1302 795b88cb 2023-07-10 thomas "%s%s%s"
1303 795b88cb 2023-07-10 thomas "%s%s%s"
1307 795b88cb 2023-07-10 thomas GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1308 795b88cb 2023-07-10 thomas port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1309 795b88cb 2023-07-10 thomas remote_repo_path, branches ? "\tbranch { " : "",
1310 795b88cb 2023-07-10 thomas branches ? branches : "", branches ? "}\n" : "",
1311 795b88cb 2023-07-10 thomas refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1312 795b88cb 2023-07-10 thomas mirror_references ? "\tmirror_references yes\n" : "",
1313 795b88cb 2023-07-10 thomas fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1314 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1315 795b88cb 2023-07-10 thomas goto done;
1317 795b88cb 2023-07-10 thomas n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1318 795b88cb 2023-07-10 thomas if (n != strlen(gotconfig)) {
1319 795b88cb 2023-07-10 thomas err = got_ferror(gotconfig_file, GOT_ERR_IO);
1320 795b88cb 2023-07-10 thomas goto done;
1324 795b88cb 2023-07-10 thomas if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1325 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fclose", gotconfig_path);
1326 795b88cb 2023-07-10 thomas free(gotconfig_path);
1327 795b88cb 2023-07-10 thomas free(branches);
1328 795b88cb 2023-07-10 thomas return err;
1331 795b88cb 2023-07-10 thomas static const struct got_error *
1332 795b88cb 2023-07-10 thomas create_gitconfig(const char *git_url, const char *default_branch,
1333 795b88cb 2023-07-10 thomas int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1334 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_refs, int mirror_references,
1335 795b88cb 2023-07-10 thomas struct got_repository *repo)
1337 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
1338 795b88cb 2023-07-10 thomas char *gitconfig_path = NULL;
1339 795b88cb 2023-07-10 thomas char *gitconfig = NULL;
1340 795b88cb 2023-07-10 thomas FILE *gitconfig_file = NULL;
1341 795b88cb 2023-07-10 thomas char *branches = NULL, *refs = NULL;
1342 795b88cb 2023-07-10 thomas const char *branchname;
1343 795b88cb 2023-07-10 thomas ssize_t n;
1345 795b88cb 2023-07-10 thomas /* Create a config file Git can understand. */
1346 795b88cb 2023-07-10 thomas gitconfig_path = got_repo_get_path_gitconfig(repo);
1347 795b88cb 2023-07-10 thomas if (gitconfig_path == NULL) {
1348 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_repo_get_path_gitconfig");
1349 795b88cb 2023-07-10 thomas goto done;
1351 795b88cb 2023-07-10 thomas gitconfig_file = fopen(gitconfig_path, "ae");
1352 795b88cb 2023-07-10 thomas if (gitconfig_file == NULL) {
1353 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fopen", gitconfig_path);
1354 795b88cb 2023-07-10 thomas goto done;
1356 795b88cb 2023-07-10 thomas if (fetch_all_branches) {
1357 795b88cb 2023-07-10 thomas if (mirror_references) {
1358 795b88cb 2023-07-10 thomas if (asprintf(&branches,
1359 795b88cb 2023-07-10 thomas "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1360 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1361 795b88cb 2023-07-10 thomas goto done;
1363 795b88cb 2023-07-10 thomas } else if (asprintf(&branches,
1364 795b88cb 2023-07-10 thomas "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1365 795b88cb 2023-07-10 thomas GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1366 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1367 795b88cb 2023-07-10 thomas goto done;
1369 795b88cb 2023-07-10 thomas } else if (!TAILQ_EMPTY(wanted_branches)) {
1370 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1371 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, wanted_branches, entry) {
1372 795b88cb 2023-07-10 thomas char *s;
1373 795b88cb 2023-07-10 thomas branchname = pe->path;
1374 795b88cb 2023-07-10 thomas if (strncmp(branchname, "refs/heads/", 11) == 0)
1375 795b88cb 2023-07-10 thomas branchname += 11;
1376 795b88cb 2023-07-10 thomas if (mirror_references) {
1377 795b88cb 2023-07-10 thomas if (asprintf(&s,
1378 795b88cb 2023-07-10 thomas "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1379 795b88cb 2023-07-10 thomas branches ? branches : "",
1380 795b88cb 2023-07-10 thomas branchname, branchname) == -1) {
1381 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1382 795b88cb 2023-07-10 thomas goto done;
1384 795b88cb 2023-07-10 thomas } else if (asprintf(&s,
1385 795b88cb 2023-07-10 thomas "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1386 795b88cb 2023-07-10 thomas branches ? branches : "",
1387 795b88cb 2023-07-10 thomas branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1388 795b88cb 2023-07-10 thomas branchname) == -1) {
1389 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1390 795b88cb 2023-07-10 thomas goto done;
1392 795b88cb 2023-07-10 thomas free(branches);
1393 795b88cb 2023-07-10 thomas branches = s;
1395 795b88cb 2023-07-10 thomas } else {
1397 795b88cb 2023-07-10 thomas * If the server specified a default branch, use just that one.
1398 795b88cb 2023-07-10 thomas * Otherwise fall back to fetching all branches on next fetch.
1400 795b88cb 2023-07-10 thomas if (default_branch) {
1401 795b88cb 2023-07-10 thomas branchname = default_branch;
1402 795b88cb 2023-07-10 thomas if (strncmp(branchname, "refs/heads/", 11) == 0)
1403 795b88cb 2023-07-10 thomas branchname += 11;
1405 795b88cb 2023-07-10 thomas branchname = "*"; /* fall back to all branches */
1406 795b88cb 2023-07-10 thomas if (mirror_references) {
1407 795b88cb 2023-07-10 thomas if (asprintf(&branches,
1408 795b88cb 2023-07-10 thomas "\tfetch = refs/heads/%s:refs/heads/%s\n",
1409 795b88cb 2023-07-10 thomas branchname, branchname) == -1) {
1410 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1411 795b88cb 2023-07-10 thomas goto done;
1413 795b88cb 2023-07-10 thomas } else if (asprintf(&branches,
1414 795b88cb 2023-07-10 thomas "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1415 795b88cb 2023-07-10 thomas branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1416 795b88cb 2023-07-10 thomas branchname) == -1) {
1417 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1418 795b88cb 2023-07-10 thomas goto done;
1421 795b88cb 2023-07-10 thomas if (!TAILQ_EMPTY(wanted_refs)) {
1422 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1423 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, wanted_refs, entry) {
1424 795b88cb 2023-07-10 thomas char *s;
1425 795b88cb 2023-07-10 thomas const char *refname = pe->path;
1426 795b88cb 2023-07-10 thomas if (strncmp(refname, "refs/", 5) == 0)
1427 795b88cb 2023-07-10 thomas refname += 5;
1428 795b88cb 2023-07-10 thomas if (mirror_references) {
1429 795b88cb 2023-07-10 thomas if (asprintf(&s,
1430 795b88cb 2023-07-10 thomas "%s\tfetch = refs/%s:refs/%s\n",
1431 795b88cb 2023-07-10 thomas refs ? refs : "", refname, refname) == -1) {
1432 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1433 795b88cb 2023-07-10 thomas goto done;
1435 795b88cb 2023-07-10 thomas } else if (asprintf(&s,
1436 795b88cb 2023-07-10 thomas "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1437 795b88cb 2023-07-10 thomas refs ? refs : "",
1438 795b88cb 2023-07-10 thomas refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1439 795b88cb 2023-07-10 thomas refname) == -1) {
1440 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1441 795b88cb 2023-07-10 thomas goto done;
1443 795b88cb 2023-07-10 thomas free(refs);
1444 795b88cb 2023-07-10 thomas refs = s;
1448 795b88cb 2023-07-10 thomas if (asprintf(&gitconfig,
1449 795b88cb 2023-07-10 thomas "[remote \"%s\"]\n"
1450 795b88cb 2023-07-10 thomas "\turl = %s\n"
1453 795b88cb 2023-07-10 thomas "\tfetch = refs/tags/*:refs/tags/*\n",
1454 795b88cb 2023-07-10 thomas GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1455 795b88cb 2023-07-10 thomas refs ? refs : "") == -1) {
1456 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1457 795b88cb 2023-07-10 thomas goto done;
1459 795b88cb 2023-07-10 thomas n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1460 795b88cb 2023-07-10 thomas if (n != strlen(gitconfig)) {
1461 795b88cb 2023-07-10 thomas err = got_ferror(gitconfig_file, GOT_ERR_IO);
1462 795b88cb 2023-07-10 thomas goto done;
1465 795b88cb 2023-07-10 thomas if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1466 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fclose", gitconfig_path);
1467 795b88cb 2023-07-10 thomas free(gitconfig_path);
1468 795b88cb 2023-07-10 thomas free(branches);
1469 795b88cb 2023-07-10 thomas return err;
1472 795b88cb 2023-07-10 thomas static const struct got_error *
1473 795b88cb 2023-07-10 thomas create_config_files(const char *proto, const char *host, const char *port,
1474 795b88cb 2023-07-10 thomas const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1475 795b88cb 2023-07-10 thomas int mirror_references, struct got_pathlist_head *symrefs,
1476 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_branches,
1477 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1479 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
1480 795b88cb 2023-07-10 thomas const char *default_branch = NULL;
1481 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1484 795b88cb 2023-07-10 thomas * If we asked for a set of wanted branches then use the first
1485 795b88cb 2023-07-10 thomas * one of those.
1487 795b88cb 2023-07-10 thomas if (!TAILQ_EMPTY(wanted_branches)) {
1488 795b88cb 2023-07-10 thomas pe = TAILQ_FIRST(wanted_branches);
1489 795b88cb 2023-07-10 thomas default_branch = pe->path;
1490 795b88cb 2023-07-10 thomas } else {
1491 795b88cb 2023-07-10 thomas /* First HEAD ref listed by server is the default branch. */
1492 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, symrefs, entry) {
1493 795b88cb 2023-07-10 thomas const char *refname = pe->path;
1494 795b88cb 2023-07-10 thomas const char *target = pe->data;
1496 795b88cb 2023-07-10 thomas if (strcmp(refname, GOT_REF_HEAD) != 0)
1497 795b88cb 2023-07-10 thomas continue;
1499 795b88cb 2023-07-10 thomas default_branch = target;
1504 795b88cb 2023-07-10 thomas /* Create got.conf(5). */
1505 795b88cb 2023-07-10 thomas err = create_gotconfig(proto, host, port, remote_repo_path,
1506 795b88cb 2023-07-10 thomas default_branch, fetch_all_branches, wanted_branches,
1507 795b88cb 2023-07-10 thomas wanted_refs, mirror_references, repo);
1508 795b88cb 2023-07-10 thomas if (err)
1509 795b88cb 2023-07-10 thomas return err;
1511 795b88cb 2023-07-10 thomas /* Create a config file Git can understand. */
1512 795b88cb 2023-07-10 thomas return create_gitconfig(git_url, default_branch, fetch_all_branches,
1513 795b88cb 2023-07-10 thomas wanted_branches, wanted_refs, mirror_references, repo);
1516 795b88cb 2023-07-10 thomas static const struct got_error *
1517 795b88cb 2023-07-10 thomas cmd_clone(int argc, char *argv[])
1519 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
1520 795b88cb 2023-07-10 thomas const char *uri, *dirname;
1521 795b88cb 2023-07-10 thomas char *proto, *host, *port, *repo_name, *server_path;
1522 795b88cb 2023-07-10 thomas char *default_destdir = NULL, *id_str = NULL;
1523 795b88cb 2023-07-10 thomas const char *repo_path;
1524 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
1525 795b88cb 2023-07-10 thomas struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1526 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1527 795b88cb 2023-07-10 thomas struct got_object_id *pack_hash = NULL;
1528 795b88cb 2023-07-10 thomas int ch, fetchfd = -1, fetchstatus;
1529 795b88cb 2023-07-10 thomas pid_t fetchpid = -1;
1530 795b88cb 2023-07-10 thomas struct got_fetch_progress_arg fpa;
1531 795b88cb 2023-07-10 thomas char *git_url = NULL;
1532 795b88cb 2023-07-10 thomas int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1533 795b88cb 2023-07-10 thomas int bflag = 0, list_refs_only = 0;
1534 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
1536 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
1537 795b88cb 2023-07-10 thomas TAILQ_INIT(&symrefs);
1538 795b88cb 2023-07-10 thomas TAILQ_INIT(&wanted_branches);
1539 795b88cb 2023-07-10 thomas TAILQ_INIT(&wanted_refs);
1541 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "ab:lmqR:v")) != -1) {
1542 795b88cb 2023-07-10 thomas switch (ch) {
1543 795b88cb 2023-07-10 thomas case 'a':
1544 795b88cb 2023-07-10 thomas fetch_all_branches = 1;
1546 795b88cb 2023-07-10 thomas case 'b':
1547 795b88cb 2023-07-10 thomas error = got_pathlist_append(&wanted_branches,
1548 795b88cb 2023-07-10 thomas optarg, NULL);
1549 795b88cb 2023-07-10 thomas if (error)
1550 795b88cb 2023-07-10 thomas return error;
1551 795b88cb 2023-07-10 thomas bflag = 1;
1553 795b88cb 2023-07-10 thomas case 'l':
1554 795b88cb 2023-07-10 thomas list_refs_only = 1;
1556 795b88cb 2023-07-10 thomas case 'm':
1557 795b88cb 2023-07-10 thomas mirror_references = 1;
1559 795b88cb 2023-07-10 thomas case 'q':
1560 795b88cb 2023-07-10 thomas verbosity = -1;
1562 795b88cb 2023-07-10 thomas case 'R':
1563 795b88cb 2023-07-10 thomas error = got_pathlist_append(&wanted_refs,
1564 795b88cb 2023-07-10 thomas optarg, NULL);
1565 795b88cb 2023-07-10 thomas if (error)
1566 795b88cb 2023-07-10 thomas return error;
1568 795b88cb 2023-07-10 thomas case 'v':
1569 795b88cb 2023-07-10 thomas if (verbosity < 0)
1570 795b88cb 2023-07-10 thomas verbosity = 0;
1571 795b88cb 2023-07-10 thomas else if (verbosity < 3)
1572 795b88cb 2023-07-10 thomas verbosity++;
1574 795b88cb 2023-07-10 thomas default:
1575 795b88cb 2023-07-10 thomas usage_clone();
1579 795b88cb 2023-07-10 thomas argc -= optind;
1580 795b88cb 2023-07-10 thomas argv += optind;
1582 795b88cb 2023-07-10 thomas if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1583 795b88cb 2023-07-10 thomas option_conflict('a', 'b');
1584 795b88cb 2023-07-10 thomas if (list_refs_only) {
1585 795b88cb 2023-07-10 thomas if (!TAILQ_EMPTY(&wanted_branches))
1586 795b88cb 2023-07-10 thomas option_conflict('l', 'b');
1587 795b88cb 2023-07-10 thomas if (fetch_all_branches)
1588 795b88cb 2023-07-10 thomas option_conflict('l', 'a');
1589 795b88cb 2023-07-10 thomas if (mirror_references)
1590 795b88cb 2023-07-10 thomas option_conflict('l', 'm');
1591 795b88cb 2023-07-10 thomas if (!TAILQ_EMPTY(&wanted_refs))
1592 795b88cb 2023-07-10 thomas option_conflict('l', 'R');
1595 795b88cb 2023-07-10 thomas uri = argv[0];
1597 795b88cb 2023-07-10 thomas if (argc == 1)
1598 795b88cb 2023-07-10 thomas dirname = NULL;
1599 795b88cb 2023-07-10 thomas else if (argc == 2)
1600 795b88cb 2023-07-10 thomas dirname = argv[1];
1602 795b88cb 2023-07-10 thomas usage_clone();
1604 795b88cb 2023-07-10 thomas error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1605 795b88cb 2023-07-10 thomas &repo_name, uri);
1606 795b88cb 2023-07-10 thomas if (error)
1607 795b88cb 2023-07-10 thomas goto done;
1609 795b88cb 2023-07-10 thomas if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1610 795b88cb 2023-07-10 thomas host, port ? ":" : "", port ? port : "",
1611 795b88cb 2023-07-10 thomas server_path[0] != '/' ? "/" : "", server_path) == -1) {
1612 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
1613 795b88cb 2023-07-10 thomas goto done;
1616 795b88cb 2023-07-10 thomas if (strcmp(proto, "git") == 0) {
1617 795b88cb 2023-07-10 thomas #ifndef PROFILE
1618 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1619 795b88cb 2023-07-10 thomas "sendfd dns inet unveil", NULL) == -1)
1620 795b88cb 2023-07-10 thomas err(1, "pledge");
1622 795b88cb 2023-07-10 thomas } else if (strcmp(proto, "git+ssh") == 0 ||
1623 795b88cb 2023-07-10 thomas strcmp(proto, "ssh") == 0) {
1624 795b88cb 2023-07-10 thomas #ifndef PROFILE
1625 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1626 795b88cb 2023-07-10 thomas "sendfd unveil", NULL) == -1)
1627 795b88cb 2023-07-10 thomas err(1, "pledge");
1629 795b88cb 2023-07-10 thomas } else if (strcmp(proto, "http") == 0 ||
1630 795b88cb 2023-07-10 thomas strcmp(proto, "git+http") == 0) {
1631 795b88cb 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1632 795b88cb 2023-07-10 thomas goto done;
1633 795b88cb 2023-07-10 thomas } else {
1634 795b88cb 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1635 795b88cb 2023-07-10 thomas goto done;
1637 795b88cb 2023-07-10 thomas if (dirname == NULL) {
1638 795b88cb 2023-07-10 thomas if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1639 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
1640 795b88cb 2023-07-10 thomas goto done;
1642 795b88cb 2023-07-10 thomas repo_path = default_destdir;
1644 795b88cb 2023-07-10 thomas repo_path = dirname;
1646 795b88cb 2023-07-10 thomas if (!list_refs_only) {
1647 795b88cb 2023-07-10 thomas error = got_path_mkdir(repo_path);
1648 795b88cb 2023-07-10 thomas if (error &&
1649 795b88cb 2023-07-10 thomas (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1650 795b88cb 2023-07-10 thomas !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1651 795b88cb 2023-07-10 thomas goto done;
1652 795b88cb 2023-07-10 thomas if (!got_path_dir_is_empty(repo_path)) {
1653 795b88cb 2023-07-10 thomas error = got_error_path(repo_path,
1654 795b88cb 2023-07-10 thomas GOT_ERR_DIR_NOT_EMPTY);
1655 795b88cb 2023-07-10 thomas goto done;
1659 795b88cb 2023-07-10 thomas error = got_dial_apply_unveil(proto);
1660 795b88cb 2023-07-10 thomas if (error)
1661 795b88cb 2023-07-10 thomas goto done;
1663 795b88cb 2023-07-10 thomas error = apply_unveil(repo_path, 0, NULL);
1664 795b88cb 2023-07-10 thomas if (error)
1665 795b88cb 2023-07-10 thomas goto done;
1667 795b88cb 2023-07-10 thomas if (verbosity >= 0)
1668 795b88cb 2023-07-10 thomas printf("Connecting to %s\n", git_url);
1670 795b88cb 2023-07-10 thomas error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1671 795b88cb 2023-07-10 thomas server_path, verbosity);
1672 795b88cb 2023-07-10 thomas if (error)
1673 795b88cb 2023-07-10 thomas goto done;
1675 795b88cb 2023-07-10 thomas if (!list_refs_only) {
1676 795b88cb 2023-07-10 thomas error = got_repo_init(repo_path, NULL);
1677 795b88cb 2023-07-10 thomas if (error)
1678 795b88cb 2023-07-10 thomas goto done;
1679 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
1680 795b88cb 2023-07-10 thomas if (error != NULL)
1681 795b88cb 2023-07-10 thomas goto done;
1682 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1683 795b88cb 2023-07-10 thomas if (error)
1684 795b88cb 2023-07-10 thomas goto done;
1687 795b88cb 2023-07-10 thomas fpa.last_scaled_size[0] = '\0';
1688 795b88cb 2023-07-10 thomas fpa.last_p_indexed = -1;
1689 795b88cb 2023-07-10 thomas fpa.last_p_resolved = -1;
1690 795b88cb 2023-07-10 thomas fpa.verbosity = verbosity;
1691 795b88cb 2023-07-10 thomas fpa.create_configs = 1;
1692 795b88cb 2023-07-10 thomas fpa.configs_created = 0;
1693 795b88cb 2023-07-10 thomas fpa.repo = repo;
1694 795b88cb 2023-07-10 thomas fpa.config_info.symrefs = &symrefs;
1695 795b88cb 2023-07-10 thomas fpa.config_info.wanted_branches = &wanted_branches;
1696 795b88cb 2023-07-10 thomas fpa.config_info.wanted_refs = &wanted_refs;
1697 795b88cb 2023-07-10 thomas fpa.config_info.proto = proto;
1698 795b88cb 2023-07-10 thomas fpa.config_info.host = host;
1699 795b88cb 2023-07-10 thomas fpa.config_info.port = port;
1700 795b88cb 2023-07-10 thomas fpa.config_info.remote_repo_path = server_path;
1701 795b88cb 2023-07-10 thomas fpa.config_info.git_url = git_url;
1702 795b88cb 2023-07-10 thomas fpa.config_info.fetch_all_branches = fetch_all_branches;
1703 795b88cb 2023-07-10 thomas fpa.config_info.mirror_references = mirror_references;
1704 795b88cb 2023-07-10 thomas error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1705 795b88cb 2023-07-10 thomas GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1706 795b88cb 2023-07-10 thomas fetch_all_branches, &wanted_branches, &wanted_refs,
1707 795b88cb 2023-07-10 thomas list_refs_only, verbosity, fetchfd, repo, NULL, NULL, bflag,
1708 795b88cb 2023-07-10 thomas fetch_progress, &fpa);
1709 795b88cb 2023-07-10 thomas if (error)
1710 795b88cb 2023-07-10 thomas goto done;
1712 795b88cb 2023-07-10 thomas if (list_refs_only) {
1713 795b88cb 2023-07-10 thomas error = list_remote_refs(&symrefs, &refs);
1714 795b88cb 2023-07-10 thomas goto done;
1717 795b88cb 2023-07-10 thomas if (pack_hash == NULL) {
1718 795b88cb 2023-07-10 thomas error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1719 795b88cb 2023-07-10 thomas "server sent an empty pack file");
1720 795b88cb 2023-07-10 thomas goto done;
1722 795b88cb 2023-07-10 thomas error = got_object_id_str(&id_str, pack_hash);
1723 795b88cb 2023-07-10 thomas if (error)
1724 795b88cb 2023-07-10 thomas goto done;
1725 795b88cb 2023-07-10 thomas if (verbosity >= 0)
1726 795b88cb 2023-07-10 thomas printf("\nFetched %s.pack\n", id_str);
1727 795b88cb 2023-07-10 thomas free(id_str);
1729 795b88cb 2023-07-10 thomas /* Set up references provided with the pack file. */
1730 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &refs, entry) {
1731 795b88cb 2023-07-10 thomas const char *refname = pe->path;
1732 795b88cb 2023-07-10 thomas struct got_object_id *id = pe->data;
1733 795b88cb 2023-07-10 thomas char *remote_refname;
1735 795b88cb 2023-07-10 thomas if (is_wanted_ref(&wanted_refs, refname) &&
1736 795b88cb 2023-07-10 thomas !mirror_references) {
1737 795b88cb 2023-07-10 thomas error = create_wanted_ref(refname, id,
1738 795b88cb 2023-07-10 thomas GOT_FETCH_DEFAULT_REMOTE_NAME,
1739 795b88cb 2023-07-10 thomas verbosity - 1, repo);
1740 795b88cb 2023-07-10 thomas if (error)
1741 795b88cb 2023-07-10 thomas goto done;
1742 795b88cb 2023-07-10 thomas continue;
1745 795b88cb 2023-07-10 thomas error = create_ref(refname, id, verbosity - 1, repo);
1746 795b88cb 2023-07-10 thomas if (error)
1747 795b88cb 2023-07-10 thomas goto done;
1749 795b88cb 2023-07-10 thomas if (mirror_references)
1750 795b88cb 2023-07-10 thomas continue;
1752 795b88cb 2023-07-10 thomas if (strncmp("refs/heads/", refname, 11) != 0)
1753 795b88cb 2023-07-10 thomas continue;
1755 795b88cb 2023-07-10 thomas if (asprintf(&remote_refname,
1756 795b88cb 2023-07-10 thomas "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1757 795b88cb 2023-07-10 thomas refname + 11) == -1) {
1758 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
1759 795b88cb 2023-07-10 thomas goto done;
1761 795b88cb 2023-07-10 thomas error = create_ref(remote_refname, id, verbosity - 1, repo);
1762 795b88cb 2023-07-10 thomas free(remote_refname);
1763 795b88cb 2023-07-10 thomas if (error)
1764 795b88cb 2023-07-10 thomas goto done;
1767 795b88cb 2023-07-10 thomas /* Set the HEAD reference if the server provided one. */
1768 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &symrefs, entry) {
1769 795b88cb 2023-07-10 thomas struct got_reference *target_ref;
1770 795b88cb 2023-07-10 thomas const char *refname = pe->path;
1771 795b88cb 2023-07-10 thomas const char *target = pe->data;
1772 795b88cb 2023-07-10 thomas char *remote_refname = NULL, *remote_target = NULL;
1774 795b88cb 2023-07-10 thomas if (strcmp(refname, GOT_REF_HEAD) != 0)
1775 795b88cb 2023-07-10 thomas continue;
1777 795b88cb 2023-07-10 thomas error = got_ref_open(&target_ref, repo, target, 0);
1778 795b88cb 2023-07-10 thomas if (error) {
1779 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_REF) {
1780 795b88cb 2023-07-10 thomas error = NULL;
1781 795b88cb 2023-07-10 thomas continue;
1783 795b88cb 2023-07-10 thomas goto done;
1786 795b88cb 2023-07-10 thomas error = create_symref(refname, target_ref, verbosity, repo);
1787 795b88cb 2023-07-10 thomas got_ref_close(target_ref);
1788 795b88cb 2023-07-10 thomas if (error)
1789 795b88cb 2023-07-10 thomas goto done;
1791 795b88cb 2023-07-10 thomas if (mirror_references)
1792 795b88cb 2023-07-10 thomas continue;
1794 795b88cb 2023-07-10 thomas if (strncmp("refs/heads/", target, 11) != 0)
1795 795b88cb 2023-07-10 thomas continue;
1797 795b88cb 2023-07-10 thomas if (asprintf(&remote_refname,
1798 795b88cb 2023-07-10 thomas "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1799 795b88cb 2023-07-10 thomas refname) == -1) {
1800 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
1801 795b88cb 2023-07-10 thomas goto done;
1803 795b88cb 2023-07-10 thomas if (asprintf(&remote_target,
1804 795b88cb 2023-07-10 thomas "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1805 795b88cb 2023-07-10 thomas target + 11) == -1) {
1806 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
1807 795b88cb 2023-07-10 thomas free(remote_refname);
1808 795b88cb 2023-07-10 thomas goto done;
1810 795b88cb 2023-07-10 thomas error = got_ref_open(&target_ref, repo, remote_target, 0);
1811 795b88cb 2023-07-10 thomas if (error) {
1812 795b88cb 2023-07-10 thomas free(remote_refname);
1813 795b88cb 2023-07-10 thomas free(remote_target);
1814 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_REF) {
1815 795b88cb 2023-07-10 thomas error = NULL;
1816 795b88cb 2023-07-10 thomas continue;
1818 795b88cb 2023-07-10 thomas goto done;
1820 795b88cb 2023-07-10 thomas error = create_symref(remote_refname, target_ref,
1821 795b88cb 2023-07-10 thomas verbosity - 1, repo);
1822 795b88cb 2023-07-10 thomas free(remote_refname);
1823 795b88cb 2023-07-10 thomas free(remote_target);
1824 795b88cb 2023-07-10 thomas got_ref_close(target_ref);
1825 795b88cb 2023-07-10 thomas if (error)
1826 795b88cb 2023-07-10 thomas goto done;
1828 795b88cb 2023-07-10 thomas if (pe == NULL) {
1830 795b88cb 2023-07-10 thomas * We failed to set the HEAD reference. If we asked for
1831 795b88cb 2023-07-10 thomas * a set of wanted branches use the first of one of those
1832 795b88cb 2023-07-10 thomas * which could be fetched instead.
1834 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &wanted_branches, entry) {
1835 795b88cb 2023-07-10 thomas const char *target = pe->path;
1836 795b88cb 2023-07-10 thomas struct got_reference *target_ref;
1838 795b88cb 2023-07-10 thomas error = got_ref_open(&target_ref, repo, target, 0);
1839 795b88cb 2023-07-10 thomas if (error) {
1840 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_REF) {
1841 795b88cb 2023-07-10 thomas error = NULL;
1842 795b88cb 2023-07-10 thomas continue;
1844 795b88cb 2023-07-10 thomas goto done;
1847 795b88cb 2023-07-10 thomas error = create_symref(GOT_REF_HEAD, target_ref,
1848 795b88cb 2023-07-10 thomas verbosity, repo);
1849 795b88cb 2023-07-10 thomas got_ref_close(target_ref);
1850 795b88cb 2023-07-10 thomas if (error)
1851 795b88cb 2023-07-10 thomas goto done;
1855 795b88cb 2023-07-10 thomas if (!fpa.configs_created && pe != NULL) {
1856 795b88cb 2023-07-10 thomas error = create_config_files(fpa.config_info.proto,
1857 795b88cb 2023-07-10 thomas fpa.config_info.host, fpa.config_info.port,
1858 795b88cb 2023-07-10 thomas fpa.config_info.remote_repo_path,
1859 795b88cb 2023-07-10 thomas fpa.config_info.git_url,
1860 795b88cb 2023-07-10 thomas fpa.config_info.fetch_all_branches,
1861 795b88cb 2023-07-10 thomas fpa.config_info.mirror_references,
1862 795b88cb 2023-07-10 thomas fpa.config_info.symrefs,
1863 795b88cb 2023-07-10 thomas fpa.config_info.wanted_branches,
1864 795b88cb 2023-07-10 thomas fpa.config_info.wanted_refs, fpa.repo);
1865 795b88cb 2023-07-10 thomas if (error)
1866 795b88cb 2023-07-10 thomas goto done;
1870 795b88cb 2023-07-10 thomas if (verbosity >= 0)
1871 795b88cb 2023-07-10 thomas printf("Created %s repository '%s'\n",
1872 795b88cb 2023-07-10 thomas mirror_references ? "mirrored" : "cloned", repo_path);
1874 795b88cb 2023-07-10 thomas if (pack_fds) {
1875 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
1876 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
1877 795b88cb 2023-07-10 thomas if (error == NULL)
1878 795b88cb 2023-07-10 thomas error = pack_err;
1880 795b88cb 2023-07-10 thomas if (fetchpid > 0) {
1881 795b88cb 2023-07-10 thomas if (kill(fetchpid, SIGTERM) == -1)
1882 795b88cb 2023-07-10 thomas error = got_error_from_errno("kill");
1883 795b88cb 2023-07-10 thomas if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1884 795b88cb 2023-07-10 thomas error = got_error_from_errno("waitpid");
1886 795b88cb 2023-07-10 thomas if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1887 795b88cb 2023-07-10 thomas error = got_error_from_errno("close");
1888 795b88cb 2023-07-10 thomas if (repo) {
1889 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
1890 795b88cb 2023-07-10 thomas if (error == NULL)
1891 795b88cb 2023-07-10 thomas error = close_err;
1893 795b88cb 2023-07-10 thomas got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
1894 795b88cb 2023-07-10 thomas got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
1895 795b88cb 2023-07-10 thomas got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
1896 795b88cb 2023-07-10 thomas got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
1897 795b88cb 2023-07-10 thomas free(pack_hash);
1898 795b88cb 2023-07-10 thomas free(proto);
1899 795b88cb 2023-07-10 thomas free(host);
1900 795b88cb 2023-07-10 thomas free(port);
1901 795b88cb 2023-07-10 thomas free(server_path);
1902 795b88cb 2023-07-10 thomas free(repo_name);
1903 795b88cb 2023-07-10 thomas free(default_destdir);
1904 795b88cb 2023-07-10 thomas free(git_url);
1905 795b88cb 2023-07-10 thomas return error;
1908 795b88cb 2023-07-10 thomas static const struct got_error *
1909 795b88cb 2023-07-10 thomas update_ref(struct got_reference *ref, struct got_object_id *new_id,
1910 795b88cb 2023-07-10 thomas int replace_tags, int verbosity, struct got_repository *repo)
1912 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
1913 795b88cb 2023-07-10 thomas char *new_id_str = NULL;
1914 795b88cb 2023-07-10 thomas struct got_object_id *old_id = NULL;
1916 795b88cb 2023-07-10 thomas err = got_object_id_str(&new_id_str, new_id);
1917 795b88cb 2023-07-10 thomas if (err)
1918 795b88cb 2023-07-10 thomas goto done;
1920 795b88cb 2023-07-10 thomas if (!replace_tags &&
1921 795b88cb 2023-07-10 thomas strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1922 795b88cb 2023-07-10 thomas err = got_ref_resolve(&old_id, repo, ref);
1923 795b88cb 2023-07-10 thomas if (err)
1924 795b88cb 2023-07-10 thomas goto done;
1925 795b88cb 2023-07-10 thomas if (got_object_id_cmp(old_id, new_id) == 0)
1926 795b88cb 2023-07-10 thomas goto done;
1927 795b88cb 2023-07-10 thomas if (verbosity >= 0) {
1928 795b88cb 2023-07-10 thomas printf("Rejecting update of existing tag %s: %s\n",
1929 795b88cb 2023-07-10 thomas got_ref_get_name(ref), new_id_str);
1931 795b88cb 2023-07-10 thomas goto done;
1934 795b88cb 2023-07-10 thomas if (got_ref_is_symbolic(ref)) {
1935 795b88cb 2023-07-10 thomas if (verbosity >= 0) {
1936 795b88cb 2023-07-10 thomas printf("Replacing reference %s: %s\n",
1937 795b88cb 2023-07-10 thomas got_ref_get_name(ref),
1938 795b88cb 2023-07-10 thomas got_ref_get_symref_target(ref));
1940 795b88cb 2023-07-10 thomas err = got_ref_change_symref_to_ref(ref, new_id);
1941 795b88cb 2023-07-10 thomas if (err)
1942 795b88cb 2023-07-10 thomas goto done;
1943 795b88cb 2023-07-10 thomas err = got_ref_write(ref, repo);
1944 795b88cb 2023-07-10 thomas if (err)
1945 795b88cb 2023-07-10 thomas goto done;
1946 795b88cb 2023-07-10 thomas } else {
1947 795b88cb 2023-07-10 thomas err = got_ref_resolve(&old_id, repo, ref);
1948 795b88cb 2023-07-10 thomas if (err)
1949 795b88cb 2023-07-10 thomas goto done;
1950 795b88cb 2023-07-10 thomas if (got_object_id_cmp(old_id, new_id) == 0)
1951 795b88cb 2023-07-10 thomas goto done;
1953 795b88cb 2023-07-10 thomas err = got_ref_change_ref(ref, new_id);
1954 795b88cb 2023-07-10 thomas if (err)
1955 795b88cb 2023-07-10 thomas goto done;
1956 795b88cb 2023-07-10 thomas err = got_ref_write(ref, repo);
1957 795b88cb 2023-07-10 thomas if (err)
1958 795b88cb 2023-07-10 thomas goto done;
1961 795b88cb 2023-07-10 thomas if (verbosity >= 0)
1962 795b88cb 2023-07-10 thomas printf("Updated %s: %s\n", got_ref_get_name(ref),
1963 795b88cb 2023-07-10 thomas new_id_str);
1965 795b88cb 2023-07-10 thomas free(old_id);
1966 795b88cb 2023-07-10 thomas free(new_id_str);
1967 795b88cb 2023-07-10 thomas return err;
1970 795b88cb 2023-07-10 thomas static const struct got_error *
1971 795b88cb 2023-07-10 thomas update_wanted_ref(const char *refname, struct got_object_id *id,
1972 795b88cb 2023-07-10 thomas const char *remote_repo_name, int verbosity, struct got_repository *repo)
1974 795b88cb 2023-07-10 thomas const struct got_error *err, *unlock_err;
1975 795b88cb 2023-07-10 thomas char *remote_refname;
1976 795b88cb 2023-07-10 thomas struct got_reference *ref;
1978 795b88cb 2023-07-10 thomas if (strncmp("refs/", refname, 5) == 0)
1979 795b88cb 2023-07-10 thomas refname += 5;
1981 795b88cb 2023-07-10 thomas if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1982 795b88cb 2023-07-10 thomas remote_repo_name, refname) == -1)
1983 795b88cb 2023-07-10 thomas return got_error_from_errno("asprintf");
1985 795b88cb 2023-07-10 thomas err = got_ref_open(&ref, repo, remote_refname, 1);
1986 795b88cb 2023-07-10 thomas if (err) {
1987 795b88cb 2023-07-10 thomas if (err->code != GOT_ERR_NOT_REF)
1988 795b88cb 2023-07-10 thomas goto done;
1989 795b88cb 2023-07-10 thomas err = create_ref(remote_refname, id, verbosity, repo);
1990 795b88cb 2023-07-10 thomas } else {
1991 795b88cb 2023-07-10 thomas err = update_ref(ref, id, 0, verbosity, repo);
1992 795b88cb 2023-07-10 thomas unlock_err = got_ref_unlock(ref);
1993 795b88cb 2023-07-10 thomas if (unlock_err && err == NULL)
1994 795b88cb 2023-07-10 thomas err = unlock_err;
1995 795b88cb 2023-07-10 thomas got_ref_close(ref);
1998 795b88cb 2023-07-10 thomas free(remote_refname);
1999 795b88cb 2023-07-10 thomas return err;
2002 795b88cb 2023-07-10 thomas __dead static void
2003 795b88cb 2023-07-10 thomas usage_checkout(void)
2005 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2006 795b88cb 2023-07-10 thomas "[-p path-prefix] repository-path [work-tree-path]\n",
2007 795b88cb 2023-07-10 thomas getprogname());
2008 795b88cb 2023-07-10 thomas exit(1);
2011 795b88cb 2023-07-10 thomas static void
2012 795b88cb 2023-07-10 thomas show_worktree_base_ref_warning(void)
2014 795b88cb 2023-07-10 thomas fprintf(stderr, "%s: warning: could not create a reference "
2015 795b88cb 2023-07-10 thomas "to the work tree's base commit; the commit could be "
2016 795b88cb 2023-07-10 thomas "garbage-collected by Git or 'gotadmin cleanup'; making the "
2017 795b88cb 2023-07-10 thomas "repository writable and running 'got update' will prevent this\n",
2018 795b88cb 2023-07-10 thomas getprogname());
2021 795b88cb 2023-07-10 thomas struct got_checkout_progress_arg {
2022 795b88cb 2023-07-10 thomas const char *worktree_path;
2023 795b88cb 2023-07-10 thomas int had_base_commit_ref_error;
2024 795b88cb 2023-07-10 thomas int verbosity;
2027 795b88cb 2023-07-10 thomas static const struct got_error *
2028 795b88cb 2023-07-10 thomas checkout_progress(void *arg, unsigned char status, const char *path)
2030 795b88cb 2023-07-10 thomas struct got_checkout_progress_arg *a = arg;
2032 795b88cb 2023-07-10 thomas /* Base commit bump happens silently. */
2033 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_BUMP_BASE)
2034 795b88cb 2023-07-10 thomas return NULL;
2036 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_BASE_REF_ERR) {
2037 795b88cb 2023-07-10 thomas a->had_base_commit_ref_error = 1;
2038 795b88cb 2023-07-10 thomas return NULL;
2041 795b88cb 2023-07-10 thomas while (path[0] == '/')
2044 795b88cb 2023-07-10 thomas if (a->verbosity >= 0)
2045 795b88cb 2023-07-10 thomas printf("%c %s/%s\n", status, a->worktree_path, path);
2047 795b88cb 2023-07-10 thomas return NULL;
2050 795b88cb 2023-07-10 thomas static const struct got_error *
2051 795b88cb 2023-07-10 thomas check_cancelled(void *arg)
2053 795b88cb 2023-07-10 thomas if (sigint_received || sigpipe_received)
2054 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_CANCELLED);
2055 795b88cb 2023-07-10 thomas return NULL;
2058 795b88cb 2023-07-10 thomas static const struct got_error *
2059 795b88cb 2023-07-10 thomas check_linear_ancestry(struct got_object_id *commit_id,
2060 795b88cb 2023-07-10 thomas struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2061 795b88cb 2023-07-10 thomas struct got_repository *repo)
2063 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
2064 795b88cb 2023-07-10 thomas struct got_object_id *yca_id;
2066 795b88cb 2023-07-10 thomas err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2067 807c60e9 2024-03-30 thomas commit_id, base_commit_id, 1, 0, repo, check_cancelled, NULL);
2068 795b88cb 2023-07-10 thomas if (err)
2069 795b88cb 2023-07-10 thomas return err;
2071 795b88cb 2023-07-10 thomas if (yca_id == NULL)
2072 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_ANCESTRY);
2075 795b88cb 2023-07-10 thomas * Require a straight line of history between the target commit
2076 795b88cb 2023-07-10 thomas * and the work tree's base commit.
2078 795b88cb 2023-07-10 thomas * Non-linear situations such as this require a rebase:
2080 795b88cb 2023-07-10 thomas * (commit) D F (base_commit)
2084 795b88cb 2023-07-10 thomas * B (yca)
2088 795b88cb 2023-07-10 thomas * 'got update' only handles linear cases:
2089 795b88cb 2023-07-10 thomas * Update forwards in time: A (base/yca) - B - C - D (commit)
2090 795b88cb 2023-07-10 thomas * Update backwards in time: D (base) - C - B - A (commit/yca)
2092 795b88cb 2023-07-10 thomas if (allow_forwards_in_time_only) {
2093 795b88cb 2023-07-10 thomas if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2094 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_ANCESTRY);
2095 795b88cb 2023-07-10 thomas } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2096 795b88cb 2023-07-10 thomas got_object_id_cmp(base_commit_id, yca_id) != 0)
2097 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_ANCESTRY);
2099 795b88cb 2023-07-10 thomas free(yca_id);
2100 795b88cb 2023-07-10 thomas return NULL;
2103 795b88cb 2023-07-10 thomas static const struct got_error *
2104 795b88cb 2023-07-10 thomas check_same_branch(struct got_object_id *commit_id,
2105 795b88cb 2023-07-10 thomas struct got_reference *head_ref, struct got_repository *repo)
2107 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
2108 795b88cb 2023-07-10 thomas struct got_commit_graph *graph = NULL;
2109 795b88cb 2023-07-10 thomas struct got_object_id *head_commit_id = NULL;
2111 795b88cb 2023-07-10 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
2112 795b88cb 2023-07-10 thomas if (err)
2113 795b88cb 2023-07-10 thomas goto done;
2115 795b88cb 2023-07-10 thomas if (got_object_id_cmp(head_commit_id, commit_id) == 0)
2116 795b88cb 2023-07-10 thomas goto done;
2118 795b88cb 2023-07-10 thomas err = got_commit_graph_open(&graph, "/", 1);
2119 795b88cb 2023-07-10 thomas if (err)
2120 795b88cb 2023-07-10 thomas goto done;
2122 0279329d 2024-03-30 thomas err = got_commit_graph_bfsort(graph, head_commit_id, repo,
2123 795b88cb 2023-07-10 thomas check_cancelled, NULL);
2124 795b88cb 2023-07-10 thomas if (err)
2125 795b88cb 2023-07-10 thomas goto done;
2127 795b88cb 2023-07-10 thomas for (;;) {
2128 795b88cb 2023-07-10 thomas struct got_object_id id;
2130 795b88cb 2023-07-10 thomas err = got_commit_graph_iter_next(&id, graph, repo,
2131 795b88cb 2023-07-10 thomas check_cancelled, NULL);
2132 795b88cb 2023-07-10 thomas if (err) {
2133 795b88cb 2023-07-10 thomas if (err->code == GOT_ERR_ITER_COMPLETED)
2134 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_ANCESTRY);
2138 795b88cb 2023-07-10 thomas if (got_object_id_cmp(&id, commit_id) == 0)
2142 795b88cb 2023-07-10 thomas if (graph)
2143 795b88cb 2023-07-10 thomas got_commit_graph_close(graph);
2144 795b88cb 2023-07-10 thomas free(head_commit_id);
2145 795b88cb 2023-07-10 thomas return err;
2148 795b88cb 2023-07-10 thomas static const struct got_error *
2149 795b88cb 2023-07-10 thomas checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2151 795b88cb 2023-07-10 thomas static char msg[512];
2152 795b88cb 2023-07-10 thomas const char *branch_name;
2154 795b88cb 2023-07-10 thomas if (got_ref_is_symbolic(ref))
2155 795b88cb 2023-07-10 thomas branch_name = got_ref_get_symref_target(ref);
2157 795b88cb 2023-07-10 thomas branch_name = got_ref_get_name(ref);
2159 795b88cb 2023-07-10 thomas if (strncmp("refs/heads/", branch_name, 11) == 0)
2160 795b88cb 2023-07-10 thomas branch_name += 11;
2162 795b88cb 2023-07-10 thomas snprintf(msg, sizeof(msg),
2163 795b88cb 2023-07-10 thomas "target commit is not contained in branch '%s'; "
2164 795b88cb 2023-07-10 thomas "the branch to use must be specified with -b; "
2165 795b88cb 2023-07-10 thomas "if necessary a new branch can be created for "
2166 795b88cb 2023-07-10 thomas "this commit with 'got branch -c %s BRANCH_NAME'",
2167 795b88cb 2023-07-10 thomas branch_name, commit_id_str);
2169 795b88cb 2023-07-10 thomas return got_error_msg(GOT_ERR_ANCESTRY, msg);
2172 795b88cb 2023-07-10 thomas static const struct got_error *
2173 795b88cb 2023-07-10 thomas cmd_checkout(int argc, char *argv[])
2175 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
2176 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
2177 795b88cb 2023-07-10 thomas struct got_reference *head_ref = NULL, *ref = NULL;
2178 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
2179 795b88cb 2023-07-10 thomas char *repo_path = NULL;
2180 795b88cb 2023-07-10 thomas char *worktree_path = NULL;
2181 795b88cb 2023-07-10 thomas const char *path_prefix = "";
2182 795b88cb 2023-07-10 thomas const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2183 795b88cb 2023-07-10 thomas char *commit_id_str = NULL;
2184 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
2185 795b88cb 2023-07-10 thomas char *cwd = NULL;
2186 795b88cb 2023-07-10 thomas int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2187 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
2188 795b88cb 2023-07-10 thomas struct got_checkout_progress_arg cpa;
2189 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
2191 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
2193 795b88cb 2023-07-10 thomas #ifndef PROFILE
2194 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2195 795b88cb 2023-07-10 thomas "unveil", NULL) == -1)
2196 795b88cb 2023-07-10 thomas err(1, "pledge");
2199 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2200 795b88cb 2023-07-10 thomas switch (ch) {
2201 795b88cb 2023-07-10 thomas case 'b':
2202 795b88cb 2023-07-10 thomas branch_name = optarg;
2204 795b88cb 2023-07-10 thomas case 'c':
2205 795b88cb 2023-07-10 thomas commit_id_str = strdup(optarg);
2206 795b88cb 2023-07-10 thomas if (commit_id_str == NULL)
2207 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
2209 795b88cb 2023-07-10 thomas case 'E':
2210 795b88cb 2023-07-10 thomas allow_nonempty = 1;
2212 795b88cb 2023-07-10 thomas case 'p':
2213 795b88cb 2023-07-10 thomas path_prefix = optarg;
2215 795b88cb 2023-07-10 thomas case 'q':
2216 795b88cb 2023-07-10 thomas verbosity = -1;
2218 795b88cb 2023-07-10 thomas default:
2219 795b88cb 2023-07-10 thomas usage_checkout();
2220 795b88cb 2023-07-10 thomas /* NOTREACHED */
2224 795b88cb 2023-07-10 thomas argc -= optind;
2225 795b88cb 2023-07-10 thomas argv += optind;
2227 795b88cb 2023-07-10 thomas if (argc == 1) {
2228 795b88cb 2023-07-10 thomas char *base, *dotgit;
2229 795b88cb 2023-07-10 thomas const char *path;
2230 795b88cb 2023-07-10 thomas repo_path = realpath(argv[0], NULL);
2231 795b88cb 2023-07-10 thomas if (repo_path == NULL)
2232 795b88cb 2023-07-10 thomas return got_error_from_errno2("realpath", argv[0]);
2233 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
2234 795b88cb 2023-07-10 thomas if (cwd == NULL) {
2235 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
2236 795b88cb 2023-07-10 thomas goto done;
2238 795b88cb 2023-07-10 thomas if (path_prefix[0])
2239 795b88cb 2023-07-10 thomas path = path_prefix;
2241 795b88cb 2023-07-10 thomas path = repo_path;
2242 795b88cb 2023-07-10 thomas error = got_path_basename(&base, path);
2243 795b88cb 2023-07-10 thomas if (error)
2244 795b88cb 2023-07-10 thomas goto done;
2245 795b88cb 2023-07-10 thomas dotgit = strstr(base, ".git");
2246 795b88cb 2023-07-10 thomas if (dotgit)
2247 795b88cb 2023-07-10 thomas *dotgit = '\0';
2248 795b88cb 2023-07-10 thomas if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2249 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
2250 795b88cb 2023-07-10 thomas free(base);
2251 795b88cb 2023-07-10 thomas goto done;
2253 795b88cb 2023-07-10 thomas free(base);
2254 795b88cb 2023-07-10 thomas } else if (argc == 2) {
2255 795b88cb 2023-07-10 thomas repo_path = realpath(argv[0], NULL);
2256 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
2257 795b88cb 2023-07-10 thomas error = got_error_from_errno2("realpath", argv[0]);
2258 795b88cb 2023-07-10 thomas goto done;
2260 795b88cb 2023-07-10 thomas worktree_path = realpath(argv[1], NULL);
2261 795b88cb 2023-07-10 thomas if (worktree_path == NULL) {
2262 795b88cb 2023-07-10 thomas if (errno != ENOENT) {
2263 795b88cb 2023-07-10 thomas error = got_error_from_errno2("realpath",
2264 795b88cb 2023-07-10 thomas argv[1]);
2265 795b88cb 2023-07-10 thomas goto done;
2267 795b88cb 2023-07-10 thomas worktree_path = strdup(argv[1]);
2268 795b88cb 2023-07-10 thomas if (worktree_path == NULL) {
2269 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
2270 795b88cb 2023-07-10 thomas goto done;
2274 795b88cb 2023-07-10 thomas usage_checkout();
2276 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
2277 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(worktree_path);
2279 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
2280 795b88cb 2023-07-10 thomas if (error != NULL)
2281 795b88cb 2023-07-10 thomas goto done;
2283 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2284 795b88cb 2023-07-10 thomas if (error != NULL)
2285 795b88cb 2023-07-10 thomas goto done;
2287 795b88cb 2023-07-10 thomas /* Pre-create work tree path for unveil(2) */
2288 795b88cb 2023-07-10 thomas error = got_path_mkdir(worktree_path);
2289 795b88cb 2023-07-10 thomas if (error) {
2290 795b88cb 2023-07-10 thomas if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2291 795b88cb 2023-07-10 thomas !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2292 795b88cb 2023-07-10 thomas goto done;
2293 795b88cb 2023-07-10 thomas if (!allow_nonempty &&
2294 795b88cb 2023-07-10 thomas !got_path_dir_is_empty(worktree_path)) {
2295 795b88cb 2023-07-10 thomas error = got_error_path(worktree_path,
2296 795b88cb 2023-07-10 thomas GOT_ERR_DIR_NOT_EMPTY);
2297 795b88cb 2023-07-10 thomas goto done;
2301 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2302 795b88cb 2023-07-10 thomas if (error)
2303 795b88cb 2023-07-10 thomas goto done;
2305 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo, branch_name, 0);
2306 795b88cb 2023-07-10 thomas if (error != NULL)
2307 795b88cb 2023-07-10 thomas goto done;
2309 ad10f64e 2023-07-19 thomas error = got_worktree_init(worktree_path, head_ref, path_prefix,
2310 ad10f64e 2023-07-19 thomas GOT_WORKTREE_CVG_DIR, repo);
2311 795b88cb 2023-07-10 thomas if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2312 795b88cb 2023-07-10 thomas goto done;
2314 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, worktree_path, GOT_WORKTREE_CVG_DIR);
2315 795b88cb 2023-07-10 thomas if (error != NULL)
2316 795b88cb 2023-07-10 thomas goto done;
2318 795b88cb 2023-07-10 thomas error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2319 795b88cb 2023-07-10 thomas path_prefix);
2320 795b88cb 2023-07-10 thomas if (error != NULL)
2321 795b88cb 2023-07-10 thomas goto done;
2322 795b88cb 2023-07-10 thomas if (!same_path_prefix) {
2323 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_PATH_PREFIX);
2324 795b88cb 2023-07-10 thomas goto done;
2327 795b88cb 2023-07-10 thomas if (commit_id_str) {
2328 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
2329 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
2330 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2332 795b88cb 2023-07-10 thomas if (error)
2333 795b88cb 2023-07-10 thomas goto done;
2334 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
2335 795b88cb 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2336 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
2337 795b88cb 2023-07-10 thomas if (error)
2338 795b88cb 2023-07-10 thomas goto done;
2339 795b88cb 2023-07-10 thomas error = check_linear_ancestry(commit_id,
2340 795b88cb 2023-07-10 thomas got_worktree_get_base_commit_id(worktree), 0, repo);
2341 795b88cb 2023-07-10 thomas if (error != NULL) {
2342 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_ANCESTRY) {
2343 795b88cb 2023-07-10 thomas error = checkout_ancestry_error(
2344 795b88cb 2023-07-10 thomas head_ref, commit_id_str);
2346 795b88cb 2023-07-10 thomas goto done;
2348 795b88cb 2023-07-10 thomas error = check_same_branch(commit_id, head_ref, repo);
2349 795b88cb 2023-07-10 thomas if (error) {
2350 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_ANCESTRY) {
2351 795b88cb 2023-07-10 thomas error = checkout_ancestry_error(
2352 795b88cb 2023-07-10 thomas head_ref, commit_id_str);
2354 795b88cb 2023-07-10 thomas goto done;
2356 795b88cb 2023-07-10 thomas error = got_worktree_set_base_commit_id(worktree, repo,
2357 795b88cb 2023-07-10 thomas commit_id);
2358 795b88cb 2023-07-10 thomas if (error)
2359 795b88cb 2023-07-10 thomas goto done;
2360 795b88cb 2023-07-10 thomas /* Expand potentially abbreviated commit ID string. */
2361 795b88cb 2023-07-10 thomas free(commit_id_str);
2362 795b88cb 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
2363 795b88cb 2023-07-10 thomas if (error)
2364 795b88cb 2023-07-10 thomas goto done;
2365 795b88cb 2023-07-10 thomas } else {
2366 795b88cb 2023-07-10 thomas commit_id = got_object_id_dup(
2367 795b88cb 2023-07-10 thomas got_worktree_get_base_commit_id(worktree));
2368 795b88cb 2023-07-10 thomas if (commit_id == NULL) {
2369 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_object_id_dup");
2370 795b88cb 2023-07-10 thomas goto done;
2372 795b88cb 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
2373 795b88cb 2023-07-10 thomas if (error)
2374 795b88cb 2023-07-10 thomas goto done;
2377 795b88cb 2023-07-10 thomas error = got_pathlist_append(&paths, "", NULL);
2378 795b88cb 2023-07-10 thomas if (error)
2379 795b88cb 2023-07-10 thomas goto done;
2380 795b88cb 2023-07-10 thomas cpa.worktree_path = worktree_path;
2381 795b88cb 2023-07-10 thomas cpa.had_base_commit_ref_error = 0;
2382 795b88cb 2023-07-10 thomas cpa.verbosity = verbosity;
2383 795b88cb 2023-07-10 thomas error = got_worktree_checkout_files(worktree, &paths, repo,
2384 795b88cb 2023-07-10 thomas checkout_progress, &cpa, check_cancelled, NULL);
2385 795b88cb 2023-07-10 thomas if (error != NULL)
2386 795b88cb 2023-07-10 thomas goto done;
2388 795b88cb 2023-07-10 thomas if (got_ref_is_symbolic(head_ref)) {
2389 795b88cb 2023-07-10 thomas error = got_ref_resolve_symbolic(&ref, repo, head_ref);
2390 795b88cb 2023-07-10 thomas if (error)
2391 795b88cb 2023-07-10 thomas goto done;
2392 795b88cb 2023-07-10 thomas refname = got_ref_get_name(ref);
2394 795b88cb 2023-07-10 thomas refname = got_ref_get_name(head_ref);
2395 795b88cb 2023-07-10 thomas printf("Checked out %s: %s\n", refname, commit_id_str);
2396 795b88cb 2023-07-10 thomas printf("Now shut up and hack\n");
2397 795b88cb 2023-07-10 thomas if (cpa.had_base_commit_ref_error)
2398 795b88cb 2023-07-10 thomas show_worktree_base_ref_warning();
2400 795b88cb 2023-07-10 thomas if (pack_fds) {
2401 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
2402 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
2403 795b88cb 2023-07-10 thomas if (error == NULL)
2404 795b88cb 2023-07-10 thomas error = pack_err;
2406 795b88cb 2023-07-10 thomas if (head_ref)
2407 795b88cb 2023-07-10 thomas got_ref_close(head_ref);
2408 795b88cb 2023-07-10 thomas if (ref)
2409 795b88cb 2023-07-10 thomas got_ref_close(ref);
2410 795b88cb 2023-07-10 thomas if (repo) {
2411 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
2412 795b88cb 2023-07-10 thomas if (error == NULL)
2413 795b88cb 2023-07-10 thomas error = close_err;
2415 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2416 795b88cb 2023-07-10 thomas free(commit_id_str);
2417 795b88cb 2023-07-10 thomas free(commit_id);
2418 795b88cb 2023-07-10 thomas free(repo_path);
2419 795b88cb 2023-07-10 thomas free(worktree_path);
2420 795b88cb 2023-07-10 thomas free(cwd);
2421 795b88cb 2023-07-10 thomas return error;
2424 795b88cb 2023-07-10 thomas struct got_update_progress_arg {
2425 795b88cb 2023-07-10 thomas int did_something;
2426 795b88cb 2023-07-10 thomas int conflicts;
2427 795b88cb 2023-07-10 thomas int obstructed;
2428 795b88cb 2023-07-10 thomas int not_updated;
2429 795b88cb 2023-07-10 thomas int missing;
2430 795b88cb 2023-07-10 thomas int not_deleted;
2431 795b88cb 2023-07-10 thomas int unversioned;
2432 795b88cb 2023-07-10 thomas int verbosity;
2435 795b88cb 2023-07-10 thomas static void
2436 795b88cb 2023-07-10 thomas print_update_progress_stats(struct got_update_progress_arg *upa)
2438 795b88cb 2023-07-10 thomas if (!upa->did_something)
2441 795b88cb 2023-07-10 thomas if (upa->conflicts > 0)
2442 795b88cb 2023-07-10 thomas printf("Files with new merge conflicts: %d\n", upa->conflicts);
2443 795b88cb 2023-07-10 thomas if (upa->obstructed > 0)
2444 795b88cb 2023-07-10 thomas printf("File paths obstructed by a non-regular file: %d\n",
2445 795b88cb 2023-07-10 thomas upa->obstructed);
2446 795b88cb 2023-07-10 thomas if (upa->not_updated > 0)
2447 795b88cb 2023-07-10 thomas printf("Files not updated because of existing merge "
2448 795b88cb 2023-07-10 thomas "conflicts: %d\n", upa->not_updated);
2452 795b88cb 2023-07-10 thomas * The meaning of some status codes differs between merge-style operations and
2453 795b88cb 2023-07-10 thomas * update operations. For example, the ! status code means "file was missing"
2454 795b88cb 2023-07-10 thomas * if changes were merged into the work tree, and "missing file was restored"
2455 795b88cb 2023-07-10 thomas * if the work tree was updated. This function should be used by any operation
2456 795b88cb 2023-07-10 thomas * which merges changes into the work tree without updating the work tree.
2458 795b88cb 2023-07-10 thomas static void
2459 795b88cb 2023-07-10 thomas print_merge_progress_stats(struct got_update_progress_arg *upa)
2461 795b88cb 2023-07-10 thomas if (!upa->did_something)
2464 795b88cb 2023-07-10 thomas if (upa->conflicts > 0)
2465 795b88cb 2023-07-10 thomas printf("Files with new merge conflicts: %d\n", upa->conflicts);
2466 795b88cb 2023-07-10 thomas if (upa->obstructed > 0)
2467 795b88cb 2023-07-10 thomas printf("File paths obstructed by a non-regular file: %d\n",
2468 795b88cb 2023-07-10 thomas upa->obstructed);
2469 795b88cb 2023-07-10 thomas if (upa->missing > 0)
2470 795b88cb 2023-07-10 thomas printf("Files which had incoming changes but could not be "
2471 795b88cb 2023-07-10 thomas "found in the work tree: %d\n", upa->missing);
2472 795b88cb 2023-07-10 thomas if (upa->not_deleted > 0)
2473 795b88cb 2023-07-10 thomas printf("Files not deleted due to differences in deleted "
2474 795b88cb 2023-07-10 thomas "content: %d\n", upa->not_deleted);
2475 795b88cb 2023-07-10 thomas if (upa->unversioned > 0)
2476 795b88cb 2023-07-10 thomas printf("Files not merged because an unversioned file was "
2477 795b88cb 2023-07-10 thomas "found in the work tree: %d\n", upa->unversioned);
2480 795b88cb 2023-07-10 thomas __dead static void
2481 795b88cb 2023-07-10 thomas usage_update(void)
2483 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s update [-qtvX] [-c commit] [-r remote] "
2484 795b88cb 2023-07-10 thomas "[path ...]\n", getprogname());
2485 795b88cb 2023-07-10 thomas exit(1);
2488 795b88cb 2023-07-10 thomas static const struct got_error *
2489 795b88cb 2023-07-10 thomas update_progress(void *arg, unsigned char status, const char *path)
2491 795b88cb 2023-07-10 thomas struct got_update_progress_arg *upa = arg;
2493 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_EXISTS ||
2494 795b88cb 2023-07-10 thomas status == GOT_STATUS_BASE_REF_ERR)
2495 795b88cb 2023-07-10 thomas return NULL;
2497 795b88cb 2023-07-10 thomas upa->did_something = 1;
2499 795b88cb 2023-07-10 thomas /* Base commit bump happens silently. */
2500 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_BUMP_BASE)
2501 795b88cb 2023-07-10 thomas return NULL;
2503 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_CONFLICT)
2504 795b88cb 2023-07-10 thomas upa->conflicts++;
2505 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_OBSTRUCTED)
2506 795b88cb 2023-07-10 thomas upa->obstructed++;
2507 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_CANNOT_UPDATE)
2508 795b88cb 2023-07-10 thomas upa->not_updated++;
2509 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_MISSING)
2510 795b88cb 2023-07-10 thomas upa->missing++;
2511 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_CANNOT_DELETE)
2512 795b88cb 2023-07-10 thomas upa->not_deleted++;
2513 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_UNVERSIONED)
2514 795b88cb 2023-07-10 thomas upa->unversioned++;
2516 795b88cb 2023-07-10 thomas while (path[0] == '/')
2518 795b88cb 2023-07-10 thomas if (upa->verbosity >= 0)
2519 795b88cb 2023-07-10 thomas printf("%c %s\n", status, path);
2521 795b88cb 2023-07-10 thomas return NULL;
2524 795b88cb 2023-07-10 thomas static const struct got_error *
2525 795b88cb 2023-07-10 thomas check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2527 795b88cb 2023-07-10 thomas const struct got_error *err;
2528 795b88cb 2023-07-10 thomas int in_progress;
2530 795b88cb 2023-07-10 thomas err = got_worktree_rebase_in_progress(&in_progress, worktree);
2531 795b88cb 2023-07-10 thomas if (err)
2532 795b88cb 2023-07-10 thomas return err;
2533 795b88cb 2023-07-10 thomas if (in_progress)
2534 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_REBASING);
2536 795b88cb 2023-07-10 thomas err = got_worktree_histedit_in_progress(&in_progress, worktree);
2537 795b88cb 2023-07-10 thomas if (err)
2538 795b88cb 2023-07-10 thomas return err;
2539 795b88cb 2023-07-10 thomas if (in_progress)
2540 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_HISTEDIT_BUSY);
2542 795b88cb 2023-07-10 thomas return NULL;
2545 795b88cb 2023-07-10 thomas static const struct got_error *
2546 795b88cb 2023-07-10 thomas check_merge_in_progress(struct got_worktree *worktree,
2547 795b88cb 2023-07-10 thomas struct got_repository *repo)
2549 795b88cb 2023-07-10 thomas const struct got_error *err;
2550 795b88cb 2023-07-10 thomas int in_progress;
2552 795b88cb 2023-07-10 thomas err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
2553 795b88cb 2023-07-10 thomas if (err)
2554 795b88cb 2023-07-10 thomas return err;
2555 795b88cb 2023-07-10 thomas if (in_progress)
2556 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_MERGE_BUSY);
2558 795b88cb 2023-07-10 thomas return NULL;
2561 795b88cb 2023-07-10 thomas static const struct got_error *
2562 795b88cb 2023-07-10 thomas get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2563 795b88cb 2023-07-10 thomas char *argv[], struct got_worktree *worktree)
2565 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
2566 795b88cb 2023-07-10 thomas char *path;
2567 795b88cb 2023-07-10 thomas struct got_pathlist_entry *new;
2570 795b88cb 2023-07-10 thomas if (argc == 0) {
2571 795b88cb 2023-07-10 thomas path = strdup("");
2572 795b88cb 2023-07-10 thomas if (path == NULL)
2573 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
2574 795b88cb 2023-07-10 thomas return got_pathlist_append(paths, path, NULL);
2577 795b88cb 2023-07-10 thomas for (i = 0; i < argc; i++) {
2578 795b88cb 2023-07-10 thomas err = got_worktree_resolve_path(&path, worktree, argv[i]);
2579 795b88cb 2023-07-10 thomas if (err)
2581 795b88cb 2023-07-10 thomas err = got_pathlist_insert(&new, paths, path, NULL);
2582 795b88cb 2023-07-10 thomas if (err || new == NULL /* duplicate */) {
2583 795b88cb 2023-07-10 thomas free(path);
2584 795b88cb 2023-07-10 thomas if (err)
2589 795b88cb 2023-07-10 thomas return err;
2592 795b88cb 2023-07-10 thomas static const struct got_error *
2593 795b88cb 2023-07-10 thomas wrap_not_worktree_error(const struct got_error *orig_err,
2594 795b88cb 2023-07-10 thomas const char *cmdname, const char *path)
2596 795b88cb 2023-07-10 thomas const struct got_error *err;
2597 795b88cb 2023-07-10 thomas struct got_repository *repo;
2598 795b88cb 2023-07-10 thomas static char msg[512];
2599 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
2601 795b88cb 2023-07-10 thomas err = got_repo_pack_fds_open(&pack_fds);
2602 795b88cb 2023-07-10 thomas if (err)
2603 795b88cb 2023-07-10 thomas return err;
2605 795b88cb 2023-07-10 thomas err = got_repo_open(&repo, path, NULL, pack_fds);
2606 795b88cb 2023-07-10 thomas if (err)
2607 795b88cb 2023-07-10 thomas return orig_err;
2609 795b88cb 2023-07-10 thomas snprintf(msg, sizeof(msg),
2610 795b88cb 2023-07-10 thomas "'got %s' needs a work tree in addition to a git repository\n"
2611 795b88cb 2023-07-10 thomas "Work trees can be checked out from this Git repository with "
2612 795b88cb 2023-07-10 thomas "'got checkout'.\n"
2613 795b88cb 2023-07-10 thomas "The got(1) manual page contains more information.", cmdname);
2614 795b88cb 2023-07-10 thomas err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2615 795b88cb 2023-07-10 thomas if (repo) {
2616 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
2617 795b88cb 2023-07-10 thomas if (err == NULL)
2618 795b88cb 2023-07-10 thomas err = close_err;
2620 795b88cb 2023-07-10 thomas if (pack_fds) {
2621 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
2622 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
2623 795b88cb 2023-07-10 thomas if (err == NULL)
2624 795b88cb 2023-07-10 thomas err = pack_err;
2626 795b88cb 2023-07-10 thomas return err;
2629 795b88cb 2023-07-10 thomas static const struct got_error *
2630 795b88cb 2023-07-10 thomas cmd_update(int argc, char *argv[])
2632 795b88cb 2023-07-10 thomas const struct got_error *error = NULL, *unlock_err;
2633 795b88cb 2023-07-10 thomas char *worktree_path = NULL;
2634 795b88cb 2023-07-10 thomas const char *repo_path = NULL;
2635 795b88cb 2023-07-10 thomas const char *remote_name = NULL;
2636 795b88cb 2023-07-10 thomas char *proto = NULL, *host = NULL, *port = NULL;
2637 795b88cb 2023-07-10 thomas char *repo_name = NULL, *server_path = NULL;
2638 795b88cb 2023-07-10 thomas const struct got_remote_repo *remotes, *remote = NULL;
2639 795b88cb 2023-07-10 thomas int nremotes;
2640 795b88cb 2023-07-10 thomas char *id_str = NULL;
2641 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
2642 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
2643 795b88cb 2023-07-10 thomas const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2644 795b88cb 2023-07-10 thomas struct got_pathlist_head paths, refs, symrefs;
2645 795b88cb 2023-07-10 thomas struct got_pathlist_head wanted_branches, wanted_refs;
2646 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
2647 795b88cb 2023-07-10 thomas struct got_reflist_head remote_refs;
2648 795b88cb 2023-07-10 thomas struct got_reflist_entry *re;
2649 795b88cb 2023-07-10 thomas struct got_object_id *pack_hash = NULL;
2650 795b88cb 2023-07-10 thomas int i, ch, fetchfd = -1, fetchstatus;
2651 795b88cb 2023-07-10 thomas pid_t fetchpid = -1;
2652 795b88cb 2023-07-10 thomas struct got_fetch_progress_arg fpa;
2653 795b88cb 2023-07-10 thomas struct got_update_progress_arg upa;
2654 795b88cb 2023-07-10 thomas int verbosity = 0;
2655 795b88cb 2023-07-10 thomas int delete_remote = 0;
2656 795b88cb 2023-07-10 thomas int replace_tags = 0;
2657 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
2658 795b88cb 2023-07-10 thomas const char *remote_head = NULL, *worktree_branch = NULL;
2659 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
2660 795b88cb 2023-07-10 thomas char *commit_id_str = NULL;
2661 795b88cb 2023-07-10 thomas const char *refname;
2662 795b88cb 2023-07-10 thomas struct got_reference *head_ref = NULL;
2664 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
2665 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
2666 795b88cb 2023-07-10 thomas TAILQ_INIT(&symrefs);
2667 795b88cb 2023-07-10 thomas TAILQ_INIT(&remote_refs);
2668 795b88cb 2023-07-10 thomas TAILQ_INIT(&wanted_branches);
2669 795b88cb 2023-07-10 thomas TAILQ_INIT(&wanted_refs);
2671 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:qr:vX")) != -1) {
2672 795b88cb 2023-07-10 thomas switch (ch) {
2673 795b88cb 2023-07-10 thomas case 'c':
2674 795b88cb 2023-07-10 thomas commit_id_str = strdup(optarg);
2675 795b88cb 2023-07-10 thomas if (commit_id_str == NULL)
2676 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
2678 795b88cb 2023-07-10 thomas case 't':
2679 795b88cb 2023-07-10 thomas replace_tags = 1;
2681 795b88cb 2023-07-10 thomas case 'q':
2682 795b88cb 2023-07-10 thomas verbosity = -1;
2684 795b88cb 2023-07-10 thomas case 'r':
2685 795b88cb 2023-07-10 thomas remote_name = optarg;
2687 795b88cb 2023-07-10 thomas case 'v':
2688 795b88cb 2023-07-10 thomas if (verbosity < 0)
2689 795b88cb 2023-07-10 thomas verbosity = 0;
2690 795b88cb 2023-07-10 thomas else if (verbosity < 3)
2691 795b88cb 2023-07-10 thomas verbosity++;
2693 795b88cb 2023-07-10 thomas case 'X':
2694 795b88cb 2023-07-10 thomas delete_remote = 1;
2696 795b88cb 2023-07-10 thomas default:
2697 795b88cb 2023-07-10 thomas usage_update();
2701 795b88cb 2023-07-10 thomas argc -= optind;
2702 795b88cb 2023-07-10 thomas argv += optind;
2704 795b88cb 2023-07-10 thomas if (delete_remote) {
2705 795b88cb 2023-07-10 thomas if (replace_tags)
2706 795b88cb 2023-07-10 thomas option_conflict('X', 't');
2707 795b88cb 2023-07-10 thomas if (remote_name == NULL)
2708 795b88cb 2023-07-10 thomas errx(1, "-X option requires a remote name");
2710 795b88cb 2023-07-10 thomas if (remote_name == NULL)
2711 795b88cb 2023-07-10 thomas remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2713 795b88cb 2023-07-10 thomas worktree_path = getcwd(NULL, 0);
2714 795b88cb 2023-07-10 thomas if (worktree_path == NULL) {
2715 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
2716 795b88cb 2023-07-10 thomas goto done;
2718 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, worktree_path, GOT_WORKTREE_CVG_DIR);
2719 795b88cb 2023-07-10 thomas if (error) {
2720 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
2721 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error, "update",
2722 795b88cb 2023-07-10 thomas worktree_path);
2723 795b88cb 2023-07-10 thomas goto done;
2725 795b88cb 2023-07-10 thomas repo_path = got_worktree_get_repo_path(worktree);
2727 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
2728 795b88cb 2023-07-10 thomas if (error != NULL)
2729 795b88cb 2023-07-10 thomas goto done;
2731 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2732 795b88cb 2023-07-10 thomas if (error)
2733 795b88cb 2023-07-10 thomas goto done;
2735 795b88cb 2023-07-10 thomas error = check_rebase_or_histedit_in_progress(worktree);
2736 795b88cb 2023-07-10 thomas if (error)
2737 795b88cb 2023-07-10 thomas goto done;
2738 795b88cb 2023-07-10 thomas error = check_merge_in_progress(worktree, repo);
2739 795b88cb 2023-07-10 thomas if (error)
2740 795b88cb 2023-07-10 thomas goto done;
2742 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2743 795b88cb 2023-07-10 thomas if (error)
2744 795b88cb 2023-07-10 thomas goto done;
2746 795b88cb 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
2747 795b88cb 2023-07-10 thomas if (worktree_conf) {
2748 795b88cb 2023-07-10 thomas got_gotconfig_get_remotes(&nremotes, &remotes,
2749 795b88cb 2023-07-10 thomas worktree_conf);
2750 795b88cb 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
2751 795b88cb 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
2752 795b88cb 2023-07-10 thomas remote = &remotes[i];
2758 795b88cb 2023-07-10 thomas if (remote == NULL) {
2759 795b88cb 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
2760 795b88cb 2023-07-10 thomas if (repo_conf) {
2761 795b88cb 2023-07-10 thomas got_gotconfig_get_remotes(&nremotes, &remotes,
2762 795b88cb 2023-07-10 thomas repo_conf);
2763 795b88cb 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
2764 795b88cb 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
2765 795b88cb 2023-07-10 thomas remote = &remotes[i];
2771 795b88cb 2023-07-10 thomas if (remote == NULL) {
2772 795b88cb 2023-07-10 thomas got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2773 795b88cb 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
2774 795b88cb 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
2775 795b88cb 2023-07-10 thomas remote = &remotes[i];
2780 795b88cb 2023-07-10 thomas if (remote == NULL) {
2781 795b88cb 2023-07-10 thomas error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2782 795b88cb 2023-07-10 thomas goto done;
2785 795b88cb 2023-07-10 thomas error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2786 795b88cb 2023-07-10 thomas &repo_name, remote->fetch_url);
2787 795b88cb 2023-07-10 thomas if (error)
2788 795b88cb 2023-07-10 thomas goto done;
2790 795b88cb 2023-07-10 thomas if (strcmp(proto, "git") == 0) {
2791 795b88cb 2023-07-10 thomas #ifndef PROFILE
2792 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2793 795b88cb 2023-07-10 thomas "sendfd dns inet unveil", NULL) == -1)
2794 795b88cb 2023-07-10 thomas err(1, "pledge");
2796 795b88cb 2023-07-10 thomas } else if (strcmp(proto, "git+ssh") == 0 ||
2797 795b88cb 2023-07-10 thomas strcmp(proto, "ssh") == 0) {
2798 795b88cb 2023-07-10 thomas #ifndef PROFILE
2799 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2800 795b88cb 2023-07-10 thomas "sendfd unveil", NULL) == -1)
2801 795b88cb 2023-07-10 thomas err(1, "pledge");
2803 795b88cb 2023-07-10 thomas } else if (strcmp(proto, "http") == 0 ||
2804 795b88cb 2023-07-10 thomas strcmp(proto, "git+http") == 0) {
2805 795b88cb 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2806 795b88cb 2023-07-10 thomas goto done;
2807 795b88cb 2023-07-10 thomas } else {
2808 795b88cb 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2809 795b88cb 2023-07-10 thomas goto done;
2812 795b88cb 2023-07-10 thomas error = got_dial_apply_unveil(proto);
2813 795b88cb 2023-07-10 thomas if (error)
2814 795b88cb 2023-07-10 thomas goto done;
2816 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
2817 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree));
2818 795b88cb 2023-07-10 thomas if (error)
2819 795b88cb 2023-07-10 thomas goto done;
2821 795b88cb 2023-07-10 thomas if (verbosity >= 0) {
2822 795b88cb 2023-07-10 thomas printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2823 795b88cb 2023-07-10 thomas remote->name, proto, host,
2824 795b88cb 2023-07-10 thomas port ? ":" : "", port ? port : "",
2825 795b88cb 2023-07-10 thomas *server_path == '/' ? "" : "/", server_path);
2828 795b88cb 2023-07-10 thomas error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2829 795b88cb 2023-07-10 thomas server_path, verbosity);
2830 795b88cb 2023-07-10 thomas if (error)
2831 795b88cb 2023-07-10 thomas goto done;
2834 795b88cb 2023-07-10 thomas * If set, get this remote's HEAD ref target so
2835 795b88cb 2023-07-10 thomas * if it has changed on the server we can fetch it.
2837 795b88cb 2023-07-10 thomas error = got_ref_list(&remote_refs, repo, "refs/remotes",
2838 795b88cb 2023-07-10 thomas got_ref_cmp_by_name, repo);
2839 795b88cb 2023-07-10 thomas if (error)
2840 795b88cb 2023-07-10 thomas goto done;
2842 795b88cb 2023-07-10 thomas TAILQ_FOREACH(re, &remote_refs, entry) {
2843 795b88cb 2023-07-10 thomas const char *remote_refname, *remote_target;
2844 795b88cb 2023-07-10 thomas size_t remote_name_len;
2846 795b88cb 2023-07-10 thomas if (!got_ref_is_symbolic(re->ref))
2847 795b88cb 2023-07-10 thomas continue;
2849 795b88cb 2023-07-10 thomas remote_name_len = strlen(remote->name);
2850 795b88cb 2023-07-10 thomas remote_refname = got_ref_get_name(re->ref);
2852 795b88cb 2023-07-10 thomas /* we only want refs/remotes/$remote->name/HEAD */
2853 795b88cb 2023-07-10 thomas if (strncmp(remote_refname + 13, remote->name,
2854 795b88cb 2023-07-10 thomas remote_name_len) != 0)
2855 795b88cb 2023-07-10 thomas continue;
2857 795b88cb 2023-07-10 thomas if (strcmp(remote_refname + remote_name_len + 14,
2858 795b88cb 2023-07-10 thomas GOT_REF_HEAD) != 0)
2859 795b88cb 2023-07-10 thomas continue;
2862 795b88cb 2023-07-10 thomas * Take the name itself because we already
2863 795b88cb 2023-07-10 thomas * only match with refs/heads/ in fetch_pack().
2865 795b88cb 2023-07-10 thomas remote_target = got_ref_get_symref_target(re->ref);
2866 795b88cb 2023-07-10 thomas remote_head = remote_target + remote_name_len + 14;
2870 795b88cb 2023-07-10 thomas refname = got_worktree_get_head_ref_name(worktree);
2871 795b88cb 2023-07-10 thomas if (strncmp(refname, "refs/heads/", 11) == 0)
2872 795b88cb 2023-07-10 thomas worktree_branch = refname;
2874 795b88cb 2023-07-10 thomas fpa.last_scaled_size[0] = '\0';
2875 795b88cb 2023-07-10 thomas fpa.last_p_indexed = -1;
2876 795b88cb 2023-07-10 thomas fpa.last_p_resolved = -1;
2877 795b88cb 2023-07-10 thomas fpa.verbosity = verbosity;
2878 795b88cb 2023-07-10 thomas fpa.repo = repo;
2879 795b88cb 2023-07-10 thomas fpa.create_configs = 0;
2880 795b88cb 2023-07-10 thomas fpa.configs_created = 0;
2881 795b88cb 2023-07-10 thomas memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2883 795b88cb 2023-07-10 thomas error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2884 795b88cb 2023-07-10 thomas remote->mirror_references, 0, &wanted_branches, &wanted_refs,
2885 795b88cb 2023-07-10 thomas 0, verbosity, fetchfd, repo, worktree_branch, remote_head,
2886 795b88cb 2023-07-10 thomas 0, fetch_progress, &fpa);
2887 795b88cb 2023-07-10 thomas if (error)
2888 795b88cb 2023-07-10 thomas goto done;
2890 795b88cb 2023-07-10 thomas if (pack_hash != NULL && verbosity >= 0) {
2891 795b88cb 2023-07-10 thomas error = got_object_id_str(&id_str, pack_hash);
2892 795b88cb 2023-07-10 thomas if (error)
2893 795b88cb 2023-07-10 thomas goto done;
2894 795b88cb 2023-07-10 thomas printf("\nFetched %s.pack\n", id_str);
2895 795b88cb 2023-07-10 thomas free(id_str);
2896 795b88cb 2023-07-10 thomas id_str = NULL;
2899 795b88cb 2023-07-10 thomas /* Update references provided with the pack file. */
2900 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &refs, entry) {
2901 795b88cb 2023-07-10 thomas const char *refname = pe->path;
2902 795b88cb 2023-07-10 thomas struct got_object_id *id = pe->data;
2903 795b88cb 2023-07-10 thomas struct got_reference *ref;
2905 795b88cb 2023-07-10 thomas if (is_wanted_ref(&wanted_refs, refname)) {
2906 795b88cb 2023-07-10 thomas error = update_wanted_ref(refname, id,
2907 795b88cb 2023-07-10 thomas remote->name, verbosity, repo);
2908 795b88cb 2023-07-10 thomas if (error)
2909 795b88cb 2023-07-10 thomas goto done;
2910 795b88cb 2023-07-10 thomas continue;
2913 795b88cb 2023-07-10 thomas error = got_ref_open(&ref, repo, refname, 1);
2914 795b88cb 2023-07-10 thomas if (error) {
2915 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_NOT_REF)
2916 795b88cb 2023-07-10 thomas goto done;
2917 795b88cb 2023-07-10 thomas error = create_ref(refname, id, verbosity,
2919 795b88cb 2023-07-10 thomas if (error)
2920 795b88cb 2023-07-10 thomas goto done;
2921 795b88cb 2023-07-10 thomas } else {
2922 795b88cb 2023-07-10 thomas error = update_ref(ref, id, replace_tags,
2923 795b88cb 2023-07-10 thomas verbosity-1, repo);
2924 795b88cb 2023-07-10 thomas unlock_err = got_ref_unlock(ref);
2925 795b88cb 2023-07-10 thomas if (unlock_err && error == NULL)
2926 795b88cb 2023-07-10 thomas error = unlock_err;
2927 795b88cb 2023-07-10 thomas got_ref_close(ref);
2928 795b88cb 2023-07-10 thomas if (error)
2929 795b88cb 2023-07-10 thomas goto done;
2933 795b88cb 2023-07-10 thomas /* Update worktree */
2934 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo,
2935 795b88cb 2023-07-10 thomas got_worktree_get_head_ref_name(worktree), 0);
2936 795b88cb 2023-07-10 thomas if (error != NULL)
2937 795b88cb 2023-07-10 thomas goto done;
2938 795b88cb 2023-07-10 thomas if (commit_id_str == NULL) {
2939 795b88cb 2023-07-10 thomas error = got_ref_resolve(&commit_id, repo, head_ref);
2940 795b88cb 2023-07-10 thomas if (error != NULL)
2941 795b88cb 2023-07-10 thomas goto done;
2942 795b88cb 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
2943 795b88cb 2023-07-10 thomas if (error != NULL)
2944 795b88cb 2023-07-10 thomas goto done;
2945 795b88cb 2023-07-10 thomas } else {
2946 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
2947 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
2948 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2950 795b88cb 2023-07-10 thomas if (error)
2951 795b88cb 2023-07-10 thomas goto done;
2952 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
2953 795b88cb 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2954 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
2955 795b88cb 2023-07-10 thomas free(commit_id_str);
2956 795b88cb 2023-07-10 thomas commit_id_str = NULL;
2957 795b88cb 2023-07-10 thomas if (error)
2958 795b88cb 2023-07-10 thomas goto done;
2959 795b88cb 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
2960 795b88cb 2023-07-10 thomas if (error)
2961 795b88cb 2023-07-10 thomas goto done;
2965 795b88cb 2023-07-10 thomas error = check_linear_ancestry(commit_id,
2966 795b88cb 2023-07-10 thomas got_worktree_get_base_commit_id(worktree), 0, repo);
2967 795b88cb 2023-07-10 thomas if (error != NULL) {
2968 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_ANCESTRY)
2969 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_BRANCH_MOVED);
2970 795b88cb 2023-07-10 thomas goto done;
2972 795b88cb 2023-07-10 thomas error = check_same_branch(commit_id, head_ref, repo);
2973 795b88cb 2023-07-10 thomas if (error)
2974 795b88cb 2023-07-10 thomas goto done;
2976 795b88cb 2023-07-10 thomas if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2977 795b88cb 2023-07-10 thomas commit_id) != 0) {
2978 795b88cb 2023-07-10 thomas error = got_worktree_set_base_commit_id(worktree, repo,
2979 795b88cb 2023-07-10 thomas commit_id);
2980 795b88cb 2023-07-10 thomas if (error)
2981 795b88cb 2023-07-10 thomas goto done;
2984 795b88cb 2023-07-10 thomas memset(&upa, 0, sizeof(upa));
2985 795b88cb 2023-07-10 thomas upa.verbosity = verbosity;
2986 795b88cb 2023-07-10 thomas error = got_worktree_checkout_files(worktree, &paths, repo,
2987 795b88cb 2023-07-10 thomas update_progress, &upa, check_cancelled, NULL);
2988 795b88cb 2023-07-10 thomas if (error != NULL)
2989 795b88cb 2023-07-10 thomas goto done;
2991 795b88cb 2023-07-10 thomas if (upa.did_something) {
2992 795b88cb 2023-07-10 thomas printf("Updated to %s: %s\n",
2993 795b88cb 2023-07-10 thomas got_worktree_get_head_ref_name(worktree), commit_id_str);
2995 795b88cb 2023-07-10 thomas printf("Already up-to-date\n");
2997 795b88cb 2023-07-10 thomas print_update_progress_stats(&upa);
2999 795b88cb 2023-07-10 thomas if (fetchpid > 0) {
3000 795b88cb 2023-07-10 thomas if (kill(fetchpid, SIGTERM) == -1)
3001 795b88cb 2023-07-10 thomas error = got_error_from_errno("kill");
3002 795b88cb 2023-07-10 thomas if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
3003 795b88cb 2023-07-10 thomas error = got_error_from_errno("waitpid");
3005 795b88cb 2023-07-10 thomas if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
3006 795b88cb 2023-07-10 thomas error = got_error_from_errno("close");
3007 795b88cb 2023-07-10 thomas if (repo) {
3008 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
3009 795b88cb 2023-07-10 thomas if (error == NULL)
3010 795b88cb 2023-07-10 thomas error = close_err;
3012 795b88cb 2023-07-10 thomas if (worktree)
3013 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
3014 795b88cb 2023-07-10 thomas if (pack_fds) {
3015 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
3016 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
3017 795b88cb 2023-07-10 thomas if (error == NULL)
3018 795b88cb 2023-07-10 thomas error = pack_err;
3020 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3021 795b88cb 2023-07-10 thomas got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
3022 795b88cb 2023-07-10 thomas got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
3023 795b88cb 2023-07-10 thomas got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
3024 795b88cb 2023-07-10 thomas got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
3025 795b88cb 2023-07-10 thomas got_ref_list_free(&remote_refs);
3026 795b88cb 2023-07-10 thomas free(id_str);
3027 795b88cb 2023-07-10 thomas free(worktree_path);
3028 795b88cb 2023-07-10 thomas free(pack_hash);
3029 795b88cb 2023-07-10 thomas free(proto);
3030 795b88cb 2023-07-10 thomas free(host);
3031 795b88cb 2023-07-10 thomas free(port);
3032 795b88cb 2023-07-10 thomas free(server_path);
3033 795b88cb 2023-07-10 thomas free(repo_name);
3034 795b88cb 2023-07-10 thomas free(commit_id);
3035 795b88cb 2023-07-10 thomas free(commit_id_str);
3036 795b88cb 2023-07-10 thomas return error;
3039 795b88cb 2023-07-10 thomas static const struct got_error *
3040 795b88cb 2023-07-10 thomas diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3041 795b88cb 2023-07-10 thomas const char *path, int diff_context, int ignore_whitespace,
3042 795b88cb 2023-07-10 thomas int force_text_diff, struct got_diffstat_cb_arg *dsa,
3043 795b88cb 2023-07-10 thomas struct got_repository *repo, FILE *outfile)
3045 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3046 795b88cb 2023-07-10 thomas struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3047 795b88cb 2023-07-10 thomas FILE *f1 = NULL, *f2 = NULL;
3048 795b88cb 2023-07-10 thomas int fd1 = -1, fd2 = -1;
3050 795b88cb 2023-07-10 thomas fd1 = got_opentempfd();
3051 795b88cb 2023-07-10 thomas if (fd1 == -1)
3052 795b88cb 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
3053 795b88cb 2023-07-10 thomas fd2 = got_opentempfd();
3054 795b88cb 2023-07-10 thomas if (fd2 == -1) {
3055 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
3056 795b88cb 2023-07-10 thomas goto done;
3059 795b88cb 2023-07-10 thomas if (blob_id1) {
3060 795b88cb 2023-07-10 thomas err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3062 795b88cb 2023-07-10 thomas if (err)
3063 795b88cb 2023-07-10 thomas goto done;
3066 795b88cb 2023-07-10 thomas err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3067 795b88cb 2023-07-10 thomas if (err)
3068 795b88cb 2023-07-10 thomas goto done;
3070 795b88cb 2023-07-10 thomas f1 = got_opentemp();
3071 795b88cb 2023-07-10 thomas if (f1 == NULL) {
3072 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3073 795b88cb 2023-07-10 thomas goto done;
3075 795b88cb 2023-07-10 thomas f2 = got_opentemp();
3076 795b88cb 2023-07-10 thomas if (f2 == NULL) {
3077 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3078 795b88cb 2023-07-10 thomas goto done;
3081 795b88cb 2023-07-10 thomas while (path[0] == '/')
3083 795b88cb 2023-07-10 thomas err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3084 795b88cb 2023-07-10 thomas GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3085 795b88cb 2023-07-10 thomas force_text_diff, dsa, outfile);
3087 795b88cb 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3088 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
3089 795b88cb 2023-07-10 thomas if (blob1)
3090 795b88cb 2023-07-10 thomas got_object_blob_close(blob1);
3091 795b88cb 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3092 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
3093 795b88cb 2023-07-10 thomas if (blob2)
3094 795b88cb 2023-07-10 thomas got_object_blob_close(blob2);
3095 795b88cb 2023-07-10 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3096 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
3097 795b88cb 2023-07-10 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3098 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
3099 795b88cb 2023-07-10 thomas return err;
3102 795b88cb 2023-07-10 thomas static const struct got_error *
3103 795b88cb 2023-07-10 thomas diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3104 795b88cb 2023-07-10 thomas const char *path, int diff_context, int ignore_whitespace,
3105 795b88cb 2023-07-10 thomas int force_text_diff, struct got_diffstat_cb_arg *dsa,
3106 795b88cb 2023-07-10 thomas struct got_repository *repo, FILE *outfile)
3108 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3109 795b88cb 2023-07-10 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3110 795b88cb 2023-07-10 thomas struct got_diff_blob_output_unidiff_arg arg;
3111 795b88cb 2023-07-10 thomas FILE *f1 = NULL, *f2 = NULL;
3112 795b88cb 2023-07-10 thomas int fd1 = -1, fd2 = -1;
3114 795b88cb 2023-07-10 thomas if (tree_id1) {
3115 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&tree1, repo, tree_id1);
3116 795b88cb 2023-07-10 thomas if (err)
3117 795b88cb 2023-07-10 thomas goto done;
3118 795b88cb 2023-07-10 thomas fd1 = got_opentempfd();
3119 795b88cb 2023-07-10 thomas if (fd1 == -1) {
3120 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
3121 795b88cb 2023-07-10 thomas goto done;
3125 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&tree2, repo, tree_id2);
3126 795b88cb 2023-07-10 thomas if (err)
3127 795b88cb 2023-07-10 thomas goto done;
3129 795b88cb 2023-07-10 thomas f1 = got_opentemp();
3130 795b88cb 2023-07-10 thomas if (f1 == NULL) {
3131 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3132 795b88cb 2023-07-10 thomas goto done;
3135 795b88cb 2023-07-10 thomas f2 = got_opentemp();
3136 795b88cb 2023-07-10 thomas if (f2 == NULL) {
3137 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3138 795b88cb 2023-07-10 thomas goto done;
3140 795b88cb 2023-07-10 thomas fd2 = got_opentempfd();
3141 795b88cb 2023-07-10 thomas if (fd2 == -1) {
3142 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
3143 795b88cb 2023-07-10 thomas goto done;
3145 795b88cb 2023-07-10 thomas arg.diff_context = diff_context;
3146 795b88cb 2023-07-10 thomas arg.ignore_whitespace = ignore_whitespace;
3147 795b88cb 2023-07-10 thomas arg.force_text_diff = force_text_diff;
3148 795b88cb 2023-07-10 thomas arg.diffstat = dsa;
3149 795b88cb 2023-07-10 thomas arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3150 795b88cb 2023-07-10 thomas arg.outfile = outfile;
3151 795b88cb 2023-07-10 thomas arg.lines = NULL;
3152 795b88cb 2023-07-10 thomas arg.nlines = 0;
3153 795b88cb 2023-07-10 thomas while (path[0] == '/')
3155 795b88cb 2023-07-10 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3156 795b88cb 2023-07-10 thomas got_diff_blob_output_unidiff, &arg, 1);
3158 795b88cb 2023-07-10 thomas if (tree1)
3159 795b88cb 2023-07-10 thomas got_object_tree_close(tree1);
3160 795b88cb 2023-07-10 thomas if (tree2)
3161 795b88cb 2023-07-10 thomas got_object_tree_close(tree2);
3162 795b88cb 2023-07-10 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3163 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
3164 795b88cb 2023-07-10 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3165 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
3166 795b88cb 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3167 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
3168 795b88cb 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3169 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
3170 795b88cb 2023-07-10 thomas return err;
3173 795b88cb 2023-07-10 thomas static const struct got_error *
3174 795b88cb 2023-07-10 thomas get_changed_paths(struct got_pathlist_head *paths,
3175 795b88cb 2023-07-10 thomas struct got_commit_object *commit, struct got_repository *repo,
3176 795b88cb 2023-07-10 thomas struct got_diffstat_cb_arg *dsa)
3178 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3179 795b88cb 2023-07-10 thomas struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3180 795b88cb 2023-07-10 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3181 795b88cb 2023-07-10 thomas struct got_object_qid *qid;
3182 795b88cb 2023-07-10 thomas got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3183 795b88cb 2023-07-10 thomas FILE *f1 = NULL, *f2 = NULL;
3184 795b88cb 2023-07-10 thomas int fd1 = -1, fd2 = -1;
3186 795b88cb 2023-07-10 thomas if (dsa) {
3187 795b88cb 2023-07-10 thomas cb = got_diff_tree_compute_diffstat;
3189 795b88cb 2023-07-10 thomas f1 = got_opentemp();
3190 795b88cb 2023-07-10 thomas if (f1 == NULL) {
3191 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3192 795b88cb 2023-07-10 thomas goto done;
3194 795b88cb 2023-07-10 thomas f2 = got_opentemp();
3195 795b88cb 2023-07-10 thomas if (f2 == NULL) {
3196 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3197 795b88cb 2023-07-10 thomas goto done;
3199 795b88cb 2023-07-10 thomas fd1 = got_opentempfd();
3200 795b88cb 2023-07-10 thomas if (fd1 == -1) {
3201 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
3202 795b88cb 2023-07-10 thomas goto done;
3204 795b88cb 2023-07-10 thomas fd2 = got_opentempfd();
3205 795b88cb 2023-07-10 thomas if (fd2 == -1) {
3206 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
3207 795b88cb 2023-07-10 thomas goto done;
3211 795b88cb 2023-07-10 thomas qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3212 795b88cb 2023-07-10 thomas if (qid != NULL) {
3213 795b88cb 2023-07-10 thomas struct got_commit_object *pcommit;
3214 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&pcommit, repo,
3215 795b88cb 2023-07-10 thomas &qid->id);
3216 795b88cb 2023-07-10 thomas if (err)
3217 795b88cb 2023-07-10 thomas return err;
3219 795b88cb 2023-07-10 thomas tree_id1 = got_object_id_dup(
3220 795b88cb 2023-07-10 thomas got_object_commit_get_tree_id(pcommit));
3221 795b88cb 2023-07-10 thomas if (tree_id1 == NULL) {
3222 795b88cb 2023-07-10 thomas got_object_commit_close(pcommit);
3223 795b88cb 2023-07-10 thomas return got_error_from_errno("got_object_id_dup");
3225 795b88cb 2023-07-10 thomas got_object_commit_close(pcommit);
3229 795b88cb 2023-07-10 thomas if (tree_id1) {
3230 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&tree1, repo, tree_id1);
3231 795b88cb 2023-07-10 thomas if (err)
3232 795b88cb 2023-07-10 thomas goto done;
3235 795b88cb 2023-07-10 thomas tree_id2 = got_object_commit_get_tree_id(commit);
3236 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&tree2, repo, tree_id2);
3237 795b88cb 2023-07-10 thomas if (err)
3238 795b88cb 2023-07-10 thomas goto done;
3240 795b88cb 2023-07-10 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3241 795b88cb 2023-07-10 thomas cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3243 795b88cb 2023-07-10 thomas if (tree1)
3244 795b88cb 2023-07-10 thomas got_object_tree_close(tree1);
3245 795b88cb 2023-07-10 thomas if (tree2)
3246 795b88cb 2023-07-10 thomas got_object_tree_close(tree2);
3247 795b88cb 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3248 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
3249 795b88cb 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3250 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
3251 795b88cb 2023-07-10 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3252 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
3253 795b88cb 2023-07-10 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3254 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
3255 795b88cb 2023-07-10 thomas free(tree_id1);
3256 795b88cb 2023-07-10 thomas return err;
3259 795b88cb 2023-07-10 thomas static const struct got_error *
3260 795b88cb 2023-07-10 thomas print_patch(struct got_commit_object *commit, struct got_object_id *id,
3261 795b88cb 2023-07-10 thomas const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3262 795b88cb 2023-07-10 thomas struct got_repository *repo, FILE *outfile)
3264 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3265 795b88cb 2023-07-10 thomas struct got_commit_object *pcommit = NULL;
3266 795b88cb 2023-07-10 thomas char *id_str1 = NULL, *id_str2 = NULL;
3267 795b88cb 2023-07-10 thomas struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3268 795b88cb 2023-07-10 thomas struct got_object_qid *qid;
3270 795b88cb 2023-07-10 thomas qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3271 795b88cb 2023-07-10 thomas if (qid != NULL) {
3272 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&pcommit, repo,
3273 795b88cb 2023-07-10 thomas &qid->id);
3274 795b88cb 2023-07-10 thomas if (err)
3275 795b88cb 2023-07-10 thomas return err;
3276 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str1, &qid->id);
3277 795b88cb 2023-07-10 thomas if (err)
3278 795b88cb 2023-07-10 thomas goto done;
3281 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str2, id);
3282 795b88cb 2023-07-10 thomas if (err)
3283 795b88cb 2023-07-10 thomas goto done;
3285 795b88cb 2023-07-10 thomas if (path && path[0] != '\0') {
3286 795b88cb 2023-07-10 thomas int obj_type;
3287 795b88cb 2023-07-10 thomas err = got_object_id_by_path(&obj_id2, repo, commit, path);
3288 795b88cb 2023-07-10 thomas if (err)
3289 795b88cb 2023-07-10 thomas goto done;
3290 795b88cb 2023-07-10 thomas if (pcommit) {
3291 795b88cb 2023-07-10 thomas err = got_object_id_by_path(&obj_id1, repo,
3292 795b88cb 2023-07-10 thomas pcommit, path);
3293 795b88cb 2023-07-10 thomas if (err) {
3294 795b88cb 2023-07-10 thomas if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3295 795b88cb 2023-07-10 thomas free(obj_id2);
3296 795b88cb 2023-07-10 thomas goto done;
3300 795b88cb 2023-07-10 thomas err = got_object_get_type(&obj_type, repo, obj_id2);
3301 795b88cb 2023-07-10 thomas if (err) {
3302 795b88cb 2023-07-10 thomas free(obj_id2);
3303 795b88cb 2023-07-10 thomas goto done;
3305 795b88cb 2023-07-10 thomas fprintf(outfile,
3306 795b88cb 2023-07-10 thomas "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3307 795b88cb 2023-07-10 thomas fprintf(outfile, "commit - %s\n",
3308 795b88cb 2023-07-10 thomas id_str1 ? id_str1 : "/dev/null");
3309 795b88cb 2023-07-10 thomas fprintf(outfile, "commit + %s\n", id_str2);
3310 795b88cb 2023-07-10 thomas switch (obj_type) {
3311 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_BLOB:
3312 795b88cb 2023-07-10 thomas err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3313 795b88cb 2023-07-10 thomas 0, 0, dsa, repo, outfile);
3315 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TREE:
3316 795b88cb 2023-07-10 thomas err = diff_trees(obj_id1, obj_id2, path, diff_context,
3317 795b88cb 2023-07-10 thomas 0, 0, dsa, repo, outfile);
3319 795b88cb 2023-07-10 thomas default:
3320 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_OBJ_TYPE);
3323 795b88cb 2023-07-10 thomas free(obj_id1);
3324 795b88cb 2023-07-10 thomas free(obj_id2);
3325 795b88cb 2023-07-10 thomas } else {
3326 795b88cb 2023-07-10 thomas obj_id2 = got_object_commit_get_tree_id(commit);
3327 795b88cb 2023-07-10 thomas if (pcommit)
3328 795b88cb 2023-07-10 thomas obj_id1 = got_object_commit_get_tree_id(pcommit);
3329 795b88cb 2023-07-10 thomas fprintf(outfile,
3330 795b88cb 2023-07-10 thomas "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3331 795b88cb 2023-07-10 thomas fprintf(outfile, "commit - %s\n",
3332 795b88cb 2023-07-10 thomas id_str1 ? id_str1 : "/dev/null");
3333 795b88cb 2023-07-10 thomas fprintf(outfile, "commit + %s\n", id_str2);
3334 795b88cb 2023-07-10 thomas err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3335 795b88cb 2023-07-10 thomas dsa, repo, outfile);
3338 795b88cb 2023-07-10 thomas free(id_str1);
3339 795b88cb 2023-07-10 thomas free(id_str2);
3340 795b88cb 2023-07-10 thomas if (pcommit)
3341 795b88cb 2023-07-10 thomas got_object_commit_close(pcommit);
3342 795b88cb 2023-07-10 thomas return err;
3345 795b88cb 2023-07-10 thomas static char *
3346 795b88cb 2023-07-10 thomas get_datestr(time_t *time, char *datebuf)
3348 795b88cb 2023-07-10 thomas struct tm mytm, *tm;
3349 795b88cb 2023-07-10 thomas char *p, *s;
3351 795b88cb 2023-07-10 thomas tm = gmtime_r(time, &mytm);
3352 795b88cb 2023-07-10 thomas if (tm == NULL)
3353 795b88cb 2023-07-10 thomas return NULL;
3354 795b88cb 2023-07-10 thomas s = asctime_r(tm, datebuf);
3355 795b88cb 2023-07-10 thomas if (s == NULL)
3356 795b88cb 2023-07-10 thomas return NULL;
3357 795b88cb 2023-07-10 thomas p = strchr(s, '\n');
3359 795b88cb 2023-07-10 thomas *p = '\0';
3360 795b88cb 2023-07-10 thomas return s;
3363 795b88cb 2023-07-10 thomas static const struct got_error *
3364 795b88cb 2023-07-10 thomas match_commit(int *have_match, struct got_object_id *id,
3365 795b88cb 2023-07-10 thomas struct got_commit_object *commit, regex_t *regex)
3367 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3368 795b88cb 2023-07-10 thomas regmatch_t regmatch;
3369 795b88cb 2023-07-10 thomas char *id_str = NULL, *logmsg = NULL;
3371 795b88cb 2023-07-10 thomas *have_match = 0;
3373 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, id);
3374 795b88cb 2023-07-10 thomas if (err)
3375 795b88cb 2023-07-10 thomas return err;
3377 795b88cb 2023-07-10 thomas err = got_object_commit_get_logmsg(&logmsg, commit);
3378 795b88cb 2023-07-10 thomas if (err)
3379 795b88cb 2023-07-10 thomas goto done;
3381 795b88cb 2023-07-10 thomas if (regexec(regex, got_object_commit_get_author(commit), 1,
3382 795b88cb 2023-07-10 thomas ®match, 0) == 0 ||
3383 795b88cb 2023-07-10 thomas regexec(regex, got_object_commit_get_committer(commit), 1,
3384 795b88cb 2023-07-10 thomas ®match, 0) == 0 ||
3385 795b88cb 2023-07-10 thomas regexec(regex, id_str, 1, ®match, 0) == 0 ||
3386 795b88cb 2023-07-10 thomas regexec(regex, logmsg, 1, ®match, 0) == 0)
3387 795b88cb 2023-07-10 thomas *have_match = 1;
3389 795b88cb 2023-07-10 thomas free(id_str);
3390 795b88cb 2023-07-10 thomas free(logmsg);
3391 795b88cb 2023-07-10 thomas return err;
3394 795b88cb 2023-07-10 thomas static void
3395 795b88cb 2023-07-10 thomas match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3396 795b88cb 2023-07-10 thomas regex_t *regex)
3398 795b88cb 2023-07-10 thomas regmatch_t regmatch;
3399 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
3401 795b88cb 2023-07-10 thomas *have_match = 0;
3403 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, changed_paths, entry) {
3404 795b88cb 2023-07-10 thomas if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3405 795b88cb 2023-07-10 thomas *have_match = 1;
3411 795b88cb 2023-07-10 thomas static const struct got_error *
3412 795b88cb 2023-07-10 thomas match_patch(int *have_match, struct got_commit_object *commit,
3413 795b88cb 2023-07-10 thomas struct got_object_id *id, const char *path, int diff_context,
3414 795b88cb 2023-07-10 thomas struct got_repository *repo, regex_t *regex, FILE *f)
3416 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3417 795b88cb 2023-07-10 thomas char *line = NULL;
3418 795b88cb 2023-07-10 thomas size_t linesize = 0;
3419 795b88cb 2023-07-10 thomas regmatch_t regmatch;
3421 795b88cb 2023-07-10 thomas *have_match = 0;
3423 795b88cb 2023-07-10 thomas err = got_opentemp_truncate(f);
3424 795b88cb 2023-07-10 thomas if (err)
3425 795b88cb 2023-07-10 thomas return err;
3427 795b88cb 2023-07-10 thomas err = print_patch(commit, id, path, diff_context, NULL, repo, f);
3428 795b88cb 2023-07-10 thomas if (err)
3429 795b88cb 2023-07-10 thomas goto done;
3431 795b88cb 2023-07-10 thomas if (fseeko(f, 0L, SEEK_SET) == -1) {
3432 795b88cb 2023-07-10 thomas err = got_error_from_errno("fseeko");
3433 795b88cb 2023-07-10 thomas goto done;
3436 795b88cb 2023-07-10 thomas while (getline(&line, &linesize, f) != -1) {
3437 795b88cb 2023-07-10 thomas if (regexec(regex, line, 1, ®match, 0) == 0) {
3438 795b88cb 2023-07-10 thomas *have_match = 1;
3443 795b88cb 2023-07-10 thomas free(line);
3444 795b88cb 2023-07-10 thomas return err;
3447 795b88cb 2023-07-10 thomas #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3449 795b88cb 2023-07-10 thomas static const struct got_error*
3450 795b88cb 2023-07-10 thomas build_refs_str(char **refs_str, struct got_reflist_head *refs,
3451 795b88cb 2023-07-10 thomas struct got_object_id *id, struct got_repository *repo,
3452 795b88cb 2023-07-10 thomas int local_only)
3454 795b88cb 2023-07-10 thomas static const struct got_error *err = NULL;
3455 795b88cb 2023-07-10 thomas struct got_reflist_entry *re;
3456 795b88cb 2023-07-10 thomas char *s;
3457 795b88cb 2023-07-10 thomas const char *name;
3459 795b88cb 2023-07-10 thomas *refs_str = NULL;
3461 795b88cb 2023-07-10 thomas TAILQ_FOREACH(re, refs, entry) {
3462 795b88cb 2023-07-10 thomas struct got_tag_object *tag = NULL;
3463 795b88cb 2023-07-10 thomas struct got_object_id *ref_id;
3464 795b88cb 2023-07-10 thomas int cmp;
3466 795b88cb 2023-07-10 thomas name = got_ref_get_name(re->ref);
3467 795b88cb 2023-07-10 thomas if (strcmp(name, GOT_REF_HEAD) == 0)
3468 795b88cb 2023-07-10 thomas continue;
3469 795b88cb 2023-07-10 thomas if (strncmp(name, "refs/", 5) == 0)
3470 795b88cb 2023-07-10 thomas name += 5;
3471 795b88cb 2023-07-10 thomas if (strncmp(name, "got/", 4) == 0)
3472 795b88cb 2023-07-10 thomas continue;
3473 795b88cb 2023-07-10 thomas if (strncmp(name, "heads/", 6) == 0)
3474 795b88cb 2023-07-10 thomas name += 6;
3475 795b88cb 2023-07-10 thomas if (strncmp(name, "remotes/", 8) == 0) {
3476 795b88cb 2023-07-10 thomas if (local_only)
3477 795b88cb 2023-07-10 thomas continue;
3478 795b88cb 2023-07-10 thomas name += 8;
3479 795b88cb 2023-07-10 thomas s = strstr(name, "/" GOT_REF_HEAD);
3480 795b88cb 2023-07-10 thomas if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
3481 795b88cb 2023-07-10 thomas continue;
3483 795b88cb 2023-07-10 thomas err = got_ref_resolve(&ref_id, repo, re->ref);
3484 795b88cb 2023-07-10 thomas if (err)
3486 795b88cb 2023-07-10 thomas if (strncmp(name, "tags/", 5) == 0) {
3487 795b88cb 2023-07-10 thomas err = got_object_open_as_tag(&tag, repo, ref_id);
3488 795b88cb 2023-07-10 thomas if (err) {
3489 795b88cb 2023-07-10 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
3490 795b88cb 2023-07-10 thomas free(ref_id);
3493 795b88cb 2023-07-10 thomas /* Ref points at something other than a tag. */
3494 795b88cb 2023-07-10 thomas err = NULL;
3495 795b88cb 2023-07-10 thomas tag = NULL;
3498 795b88cb 2023-07-10 thomas cmp = got_object_id_cmp(tag ?
3499 795b88cb 2023-07-10 thomas got_object_tag_get_object_id(tag) : ref_id, id);
3500 795b88cb 2023-07-10 thomas free(ref_id);
3501 795b88cb 2023-07-10 thomas if (tag)
3502 795b88cb 2023-07-10 thomas got_object_tag_close(tag);
3503 795b88cb 2023-07-10 thomas if (cmp != 0)
3504 795b88cb 2023-07-10 thomas continue;
3505 795b88cb 2023-07-10 thomas s = *refs_str;
3506 795b88cb 2023-07-10 thomas if (asprintf(refs_str, "%s%s%s", s ? s : "",
3507 795b88cb 2023-07-10 thomas s ? ", " : "", name) == -1) {
3508 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
3509 795b88cb 2023-07-10 thomas free(s);
3510 795b88cb 2023-07-10 thomas *refs_str = NULL;
3513 795b88cb 2023-07-10 thomas free(s);
3516 795b88cb 2023-07-10 thomas return err;
3519 795b88cb 2023-07-10 thomas static const struct got_error *
3520 795b88cb 2023-07-10 thomas print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
3521 795b88cb 2023-07-10 thomas struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
3523 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3524 795b88cb 2023-07-10 thomas char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
3525 795b88cb 2023-07-10 thomas char *comma, *s, *nl;
3526 795b88cb 2023-07-10 thomas struct got_reflist_head *refs;
3527 795b88cb 2023-07-10 thomas char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
3528 795b88cb 2023-07-10 thomas struct tm tm;
3529 795b88cb 2023-07-10 thomas time_t committer_time;
3531 795b88cb 2023-07-10 thomas refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3532 795b88cb 2023-07-10 thomas if (refs) {
3533 795b88cb 2023-07-10 thomas err = build_refs_str(&ref_str, refs, id, repo, 1);
3534 795b88cb 2023-07-10 thomas if (err)
3535 795b88cb 2023-07-10 thomas return err;
3537 795b88cb 2023-07-10 thomas /* Display the first matching ref only. */
3538 795b88cb 2023-07-10 thomas if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
3539 795b88cb 2023-07-10 thomas *comma = '\0';
3542 795b88cb 2023-07-10 thomas if (ref_str == NULL) {
3543 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, id);
3544 795b88cb 2023-07-10 thomas if (err)
3545 795b88cb 2023-07-10 thomas return err;
3548 795b88cb 2023-07-10 thomas committer_time = got_object_commit_get_committer_time(commit);
3549 795b88cb 2023-07-10 thomas if (gmtime_r(&committer_time, &tm) == NULL) {
3550 795b88cb 2023-07-10 thomas err = got_error_from_errno("gmtime_r");
3551 795b88cb 2023-07-10 thomas goto done;
3553 c24e2d2e 2024-04-25 thomas.ad if (strftime(datebuf, sizeof(datebuf), "%F ", &tm) == 0) {
3554 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_NO_SPACE);
3555 795b88cb 2023-07-10 thomas goto done;
3558 795b88cb 2023-07-10 thomas err = got_object_commit_get_logmsg(&logmsg0, commit);
3559 795b88cb 2023-07-10 thomas if (err)
3560 795b88cb 2023-07-10 thomas goto done;
3562 795b88cb 2023-07-10 thomas s = logmsg0;
3563 795b88cb 2023-07-10 thomas while (isspace((unsigned char)s[0]))
3566 795b88cb 2023-07-10 thomas nl = strchr(s, '\n');
3567 795b88cb 2023-07-10 thomas if (nl) {
3568 795b88cb 2023-07-10 thomas *nl = '\0';
3571 795b88cb 2023-07-10 thomas if (ref_str)
3572 795b88cb 2023-07-10 thomas printf("%s%-7s %s\n", datebuf, ref_str, s);
3574 795b88cb 2023-07-10 thomas printf("%s%.7s %s\n", datebuf, id_str, s);
3576 795b88cb 2023-07-10 thomas if (fflush(stdout) != 0 && err == NULL)
3577 795b88cb 2023-07-10 thomas err = got_error_from_errno("fflush");
3579 795b88cb 2023-07-10 thomas free(id_str);
3580 795b88cb 2023-07-10 thomas free(ref_str);
3581 795b88cb 2023-07-10 thomas free(logmsg0);
3582 795b88cb 2023-07-10 thomas return err;
3585 795b88cb 2023-07-10 thomas static const struct got_error *
3586 795b88cb 2023-07-10 thomas print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
3588 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
3590 795b88cb 2023-07-10 thomas if (header != NULL)
3591 795b88cb 2023-07-10 thomas printf("%s\n", header);
3593 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, dsa->paths, entry) {
3594 795b88cb 2023-07-10 thomas struct got_diff_changed_path *cp = pe->data;
3595 795b88cb 2023-07-10 thomas int pad = dsa->max_path_len - pe->path_len + 1;
3597 795b88cb 2023-07-10 thomas printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
3598 795b88cb 2023-07-10 thomas ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
3600 795b88cb 2023-07-10 thomas printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
3601 795b88cb 2023-07-10 thomas dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
3602 795b88cb 2023-07-10 thomas dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
3604 795b88cb 2023-07-10 thomas if (fflush(stdout) != 0)
3605 795b88cb 2023-07-10 thomas return got_error_from_errno("fflush");
3607 795b88cb 2023-07-10 thomas return NULL;
3610 795b88cb 2023-07-10 thomas static const struct got_error *
3611 795b88cb 2023-07-10 thomas printfile(FILE *f)
3613 795b88cb 2023-07-10 thomas char buf[8192];
3614 795b88cb 2023-07-10 thomas size_t r;
3616 795b88cb 2023-07-10 thomas if (fseeko(f, 0L, SEEK_SET) == -1)
3617 795b88cb 2023-07-10 thomas return got_error_from_errno("fseek");
3619 795b88cb 2023-07-10 thomas for (;;) {
3620 795b88cb 2023-07-10 thomas r = fread(buf, 1, sizeof(buf), f);
3621 795b88cb 2023-07-10 thomas if (r == 0) {
3622 795b88cb 2023-07-10 thomas if (ferror(f))
3623 795b88cb 2023-07-10 thomas return got_error_from_errno("fread");
3624 795b88cb 2023-07-10 thomas if (feof(f))
3627 795b88cb 2023-07-10 thomas if (fwrite(buf, 1, r, stdout) != r)
3628 795b88cb 2023-07-10 thomas return got_ferror(stdout, GOT_ERR_IO);
3631 795b88cb 2023-07-10 thomas return NULL;
3634 795b88cb 2023-07-10 thomas static const struct got_error *
3635 795b88cb 2023-07-10 thomas print_commit(struct got_commit_object *commit, struct got_object_id *id,
3636 795b88cb 2023-07-10 thomas struct got_repository *repo, const char *path,
3637 795b88cb 2023-07-10 thomas struct got_pathlist_head *changed_paths,
3638 795b88cb 2023-07-10 thomas struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
3639 795b88cb 2023-07-10 thomas struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str,
3640 795b88cb 2023-07-10 thomas const char *prefix)
3642 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3643 795b88cb 2023-07-10 thomas FILE *f = NULL;
3644 795b88cb 2023-07-10 thomas char *id_str, *datestr, *logmsg0, *logmsg, *line;
3645 795b88cb 2023-07-10 thomas char datebuf[26];
3646 795b88cb 2023-07-10 thomas time_t committer_time;
3647 795b88cb 2023-07-10 thomas const char *author, *committer;
3648 795b88cb 2023-07-10 thomas char *refs_str = NULL;
3650 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, id);
3651 795b88cb 2023-07-10 thomas if (err)
3652 795b88cb 2023-07-10 thomas return err;
3654 795b88cb 2023-07-10 thomas if (custom_refs_str == NULL) {
3655 795b88cb 2023-07-10 thomas struct got_reflist_head *refs;
3656 795b88cb 2023-07-10 thomas refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3657 795b88cb 2023-07-10 thomas if (refs) {
3658 795b88cb 2023-07-10 thomas err = build_refs_str(&refs_str, refs, id, repo, 0);
3659 795b88cb 2023-07-10 thomas if (err)
3660 795b88cb 2023-07-10 thomas goto done;
3664 795b88cb 2023-07-10 thomas printf(GOT_COMMIT_SEP_STR);
3665 795b88cb 2023-07-10 thomas if (custom_refs_str)
3666 795b88cb 2023-07-10 thomas printf("%s %s (%s)\n", prefix ? prefix : "commit", id_str,
3667 795b88cb 2023-07-10 thomas custom_refs_str);
3669 795b88cb 2023-07-10 thomas printf("%s %s%s%s%s\n", prefix ? prefix : "commit", id_str,
3670 795b88cb 2023-07-10 thomas refs_str ? " (" : "", refs_str ? refs_str : "",
3671 795b88cb 2023-07-10 thomas refs_str ? ")" : "");
3672 795b88cb 2023-07-10 thomas free(id_str);
3673 795b88cb 2023-07-10 thomas id_str = NULL;
3674 795b88cb 2023-07-10 thomas free(refs_str);
3675 795b88cb 2023-07-10 thomas refs_str = NULL;
3676 795b88cb 2023-07-10 thomas printf("from: %s\n", got_object_commit_get_author(commit));
3677 795b88cb 2023-07-10 thomas author = got_object_commit_get_author(commit);
3678 795b88cb 2023-07-10 thomas committer = got_object_commit_get_committer(commit);
3679 795b88cb 2023-07-10 thomas if (strcmp(author, committer) != 0)
3680 795b88cb 2023-07-10 thomas printf("via: %s\n", committer);
3681 795b88cb 2023-07-10 thomas committer_time = got_object_commit_get_committer_time(commit);
3682 795b88cb 2023-07-10 thomas datestr = get_datestr(&committer_time, datebuf);
3683 795b88cb 2023-07-10 thomas if (datestr)
3684 795b88cb 2023-07-10 thomas printf("date: %s UTC\n", datestr);
3685 795b88cb 2023-07-10 thomas if (got_object_commit_get_nparents(commit) > 1) {
3686 795b88cb 2023-07-10 thomas const struct got_object_id_queue *parent_ids;
3687 795b88cb 2023-07-10 thomas struct got_object_qid *qid;
3688 795b88cb 2023-07-10 thomas int n = 1;
3689 795b88cb 2023-07-10 thomas parent_ids = got_object_commit_get_parent_ids(commit);
3690 795b88cb 2023-07-10 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
3691 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, &qid->id);
3692 795b88cb 2023-07-10 thomas if (err)
3693 795b88cb 2023-07-10 thomas goto done;
3694 795b88cb 2023-07-10 thomas printf("parent %d: %s\n", n++, id_str);
3695 795b88cb 2023-07-10 thomas free(id_str);
3696 795b88cb 2023-07-10 thomas id_str = NULL;
3700 795b88cb 2023-07-10 thomas err = got_object_commit_get_logmsg(&logmsg0, commit);
3701 795b88cb 2023-07-10 thomas if (err)
3702 795b88cb 2023-07-10 thomas goto done;
3704 795b88cb 2023-07-10 thomas logmsg = logmsg0;
3706 795b88cb 2023-07-10 thomas line = strsep(&logmsg, "\n");
3707 795b88cb 2023-07-10 thomas if (line)
3708 795b88cb 2023-07-10 thomas printf(" %s\n", line);
3709 795b88cb 2023-07-10 thomas } while (line);
3710 795b88cb 2023-07-10 thomas free(logmsg0);
3712 795b88cb 2023-07-10 thomas if (changed_paths && diffstat == NULL) {
3713 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
3715 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, changed_paths, entry) {
3716 795b88cb 2023-07-10 thomas struct got_diff_changed_path *cp = pe->data;
3718 795b88cb 2023-07-10 thomas printf(" %c %s\n", cp->status, pe->path);
3720 795b88cb 2023-07-10 thomas printf("\n");
3722 795b88cb 2023-07-10 thomas if (show_patch) {
3723 795b88cb 2023-07-10 thomas if (diffstat) {
3724 795b88cb 2023-07-10 thomas f = got_opentemp();
3725 795b88cb 2023-07-10 thomas if (f == NULL) {
3726 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3727 795b88cb 2023-07-10 thomas goto done;
3731 795b88cb 2023-07-10 thomas err = print_patch(commit, id, path, diff_context, diffstat,
3732 795b88cb 2023-07-10 thomas repo, diffstat == NULL ? stdout : f);
3733 795b88cb 2023-07-10 thomas if (err)
3734 795b88cb 2023-07-10 thomas goto done;
3736 795b88cb 2023-07-10 thomas if (diffstat) {
3737 795b88cb 2023-07-10 thomas err = print_diffstat(diffstat, NULL);
3738 795b88cb 2023-07-10 thomas if (err)
3739 795b88cb 2023-07-10 thomas goto done;
3740 795b88cb 2023-07-10 thomas if (show_patch) {
3741 795b88cb 2023-07-10 thomas err = printfile(f);
3742 795b88cb 2023-07-10 thomas if (err)
3743 795b88cb 2023-07-10 thomas goto done;
3746 795b88cb 2023-07-10 thomas if (show_patch)
3747 795b88cb 2023-07-10 thomas printf("\n");
3749 795b88cb 2023-07-10 thomas if (fflush(stdout) != 0 && err == NULL)
3750 795b88cb 2023-07-10 thomas err = got_error_from_errno("fflush");
3752 795b88cb 2023-07-10 thomas if (f && fclose(f) == EOF && err == NULL)
3753 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
3754 795b88cb 2023-07-10 thomas free(id_str);
3755 795b88cb 2023-07-10 thomas free(refs_str);
3756 795b88cb 2023-07-10 thomas return err;
3759 795b88cb 2023-07-10 thomas static const struct got_error *
3760 795b88cb 2023-07-10 thomas print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3761 795b88cb 2023-07-10 thomas struct got_repository *repo, const char *path, int show_changed_paths,
3762 795b88cb 2023-07-10 thomas int show_diffstat, int show_patch, const char *search_pattern,
3763 795b88cb 2023-07-10 thomas int diff_context, int limit, int log_branches, int reverse_display_order,
3764 795b88cb 2023-07-10 thomas struct got_reflist_object_id_map *refs_idmap, int one_line,
3765 795b88cb 2023-07-10 thomas FILE *tmpfile)
3767 795b88cb 2023-07-10 thomas const struct got_error *err;
3768 795b88cb 2023-07-10 thomas struct got_commit_graph *graph;
3769 795b88cb 2023-07-10 thomas regex_t regex;
3770 795b88cb 2023-07-10 thomas int have_match;
3771 795b88cb 2023-07-10 thomas struct got_object_id_queue reversed_commits;
3772 795b88cb 2023-07-10 thomas struct got_object_qid *qid;
3773 795b88cb 2023-07-10 thomas struct got_commit_object *commit;
3774 795b88cb 2023-07-10 thomas struct got_pathlist_head changed_paths;
3776 795b88cb 2023-07-10 thomas STAILQ_INIT(&reversed_commits);
3777 795b88cb 2023-07-10 thomas TAILQ_INIT(&changed_paths);
3779 795b88cb 2023-07-10 thomas if (search_pattern && regcomp(®ex, search_pattern,
3780 795b88cb 2023-07-10 thomas REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3781 795b88cb 2023-07-10 thomas return got_error_msg(GOT_ERR_REGEX, search_pattern);
3783 795b88cb 2023-07-10 thomas err = got_commit_graph_open(&graph, path, !log_branches);
3784 795b88cb 2023-07-10 thomas if (err)
3785 795b88cb 2023-07-10 thomas return err;
3786 0279329d 2024-03-30 thomas err = got_commit_graph_bfsort(graph, root_id, repo,
3787 795b88cb 2023-07-10 thomas check_cancelled, NULL);
3788 795b88cb 2023-07-10 thomas if (err)
3789 795b88cb 2023-07-10 thomas goto done;
3790 795b88cb 2023-07-10 thomas for (;;) {
3791 795b88cb 2023-07-10 thomas struct got_object_id id;
3792 795b88cb 2023-07-10 thomas struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
3793 795b88cb 2023-07-10 thomas &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
3795 795b88cb 2023-07-10 thomas if (sigint_received || sigpipe_received)
3798 795b88cb 2023-07-10 thomas err = got_commit_graph_iter_next(&id, graph, repo,
3799 795b88cb 2023-07-10 thomas check_cancelled, NULL);
3800 795b88cb 2023-07-10 thomas if (err) {
3801 795b88cb 2023-07-10 thomas if (err->code == GOT_ERR_ITER_COMPLETED)
3802 795b88cb 2023-07-10 thomas err = NULL;
3806 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, &id);
3807 795b88cb 2023-07-10 thomas if (err)
3810 795b88cb 2023-07-10 thomas if ((show_changed_paths || (show_diffstat && !show_patch))
3811 795b88cb 2023-07-10 thomas && !reverse_display_order) {
3812 795b88cb 2023-07-10 thomas err = get_changed_paths(&changed_paths, commit, repo,
3813 795b88cb 2023-07-10 thomas show_diffstat ? &dsa : NULL);
3814 795b88cb 2023-07-10 thomas if (err)
3818 795b88cb 2023-07-10 thomas if (search_pattern) {
3819 795b88cb 2023-07-10 thomas err = match_commit(&have_match, &id, commit, ®ex);
3820 795b88cb 2023-07-10 thomas if (err) {
3821 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
3824 795b88cb 2023-07-10 thomas if (have_match == 0 && show_changed_paths)
3825 795b88cb 2023-07-10 thomas match_changed_paths(&have_match,
3826 795b88cb 2023-07-10 thomas &changed_paths, ®ex);
3827 795b88cb 2023-07-10 thomas if (have_match == 0 && show_patch) {
3828 795b88cb 2023-07-10 thomas err = match_patch(&have_match, commit, &id,
3829 795b88cb 2023-07-10 thomas path, diff_context, repo, ®ex, tmpfile);
3830 795b88cb 2023-07-10 thomas if (err)
3833 795b88cb 2023-07-10 thomas if (have_match == 0) {
3834 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
3835 795b88cb 2023-07-10 thomas got_pathlist_free(&changed_paths,
3836 795b88cb 2023-07-10 thomas GOT_PATHLIST_FREE_ALL);
3837 795b88cb 2023-07-10 thomas continue;
3841 795b88cb 2023-07-10 thomas if (reverse_display_order) {
3842 795b88cb 2023-07-10 thomas err = got_object_qid_alloc(&qid, &id);
3843 795b88cb 2023-07-10 thomas if (err)
3845 795b88cb 2023-07-10 thomas STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3846 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
3847 795b88cb 2023-07-10 thomas } else {
3848 795b88cb 2023-07-10 thomas if (one_line)
3849 795b88cb 2023-07-10 thomas err = print_commit_oneline(commit, &id,
3850 795b88cb 2023-07-10 thomas repo, refs_idmap);
3852 795b88cb 2023-07-10 thomas err = print_commit(commit, &id, repo, path,
3853 795b88cb 2023-07-10 thomas (show_changed_paths || show_diffstat) ?
3854 795b88cb 2023-07-10 thomas &changed_paths : NULL,
3855 795b88cb 2023-07-10 thomas show_diffstat ? &dsa : NULL, show_patch,
3856 795b88cb 2023-07-10 thomas diff_context, refs_idmap, NULL, NULL);
3857 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
3858 795b88cb 2023-07-10 thomas if (err)
3861 795b88cb 2023-07-10 thomas if ((limit && --limit == 0) ||
3862 795b88cb 2023-07-10 thomas (end_id && got_object_id_cmp(&id, end_id) == 0))
3865 795b88cb 2023-07-10 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
3867 795b88cb 2023-07-10 thomas if (reverse_display_order) {
3868 795b88cb 2023-07-10 thomas STAILQ_FOREACH(qid, &reversed_commits, entry) {
3869 795b88cb 2023-07-10 thomas struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
3870 795b88cb 2023-07-10 thomas &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
3872 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo,
3873 795b88cb 2023-07-10 thomas &qid->id);
3874 795b88cb 2023-07-10 thomas if (err)
3876 795b88cb 2023-07-10 thomas if (show_changed_paths ||
3877 795b88cb 2023-07-10 thomas (show_diffstat && !show_patch)) {
3878 795b88cb 2023-07-10 thomas err = get_changed_paths(&changed_paths, commit,
3879 795b88cb 2023-07-10 thomas repo, show_diffstat ? &dsa : NULL);
3880 795b88cb 2023-07-10 thomas if (err)
3883 795b88cb 2023-07-10 thomas if (one_line)
3884 795b88cb 2023-07-10 thomas err = print_commit_oneline(commit, &qid->id,
3885 795b88cb 2023-07-10 thomas repo, refs_idmap);
3887 795b88cb 2023-07-10 thomas err = print_commit(commit, &qid->id, repo, path,
3888 795b88cb 2023-07-10 thomas (show_changed_paths || show_diffstat) ?
3889 795b88cb 2023-07-10 thomas &changed_paths : NULL,
3890 795b88cb 2023-07-10 thomas show_diffstat ? &dsa : NULL, show_patch,
3891 795b88cb 2023-07-10 thomas diff_context, refs_idmap, NULL, NULL);
3892 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
3893 795b88cb 2023-07-10 thomas if (err)
3895 795b88cb 2023-07-10 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
3899 795b88cb 2023-07-10 thomas while (!STAILQ_EMPTY(&reversed_commits)) {
3900 795b88cb 2023-07-10 thomas qid = STAILQ_FIRST(&reversed_commits);
3901 795b88cb 2023-07-10 thomas STAILQ_REMOVE_HEAD(&reversed_commits, entry);
3902 795b88cb 2023-07-10 thomas got_object_qid_free(qid);
3904 795b88cb 2023-07-10 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
3905 795b88cb 2023-07-10 thomas if (search_pattern)
3906 795b88cb 2023-07-10 thomas regfree(®ex);
3907 795b88cb 2023-07-10 thomas got_commit_graph_close(graph);
3908 795b88cb 2023-07-10 thomas return err;
3911 795b88cb 2023-07-10 thomas __dead static void
3912 795b88cb 2023-07-10 thomas usage_log(void)
3914 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s log [-bdPpRs] [-C number] [-c commit] "
3915 795b88cb 2023-07-10 thomas "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
3916 795b88cb 2023-07-10 thomas "[path]\n", getprogname());
3917 795b88cb 2023-07-10 thomas exit(1);
3920 795b88cb 2023-07-10 thomas static int
3921 795b88cb 2023-07-10 thomas get_default_log_limit(void)
3923 795b88cb 2023-07-10 thomas const char *got_default_log_limit;
3924 795b88cb 2023-07-10 thomas long long n;
3925 795b88cb 2023-07-10 thomas const char *errstr;
3927 795b88cb 2023-07-10 thomas got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3928 795b88cb 2023-07-10 thomas if (got_default_log_limit == NULL)
3929 795b88cb 2023-07-10 thomas return 0;
3930 795b88cb 2023-07-10 thomas n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3931 795b88cb 2023-07-10 thomas if (errstr != NULL)
3932 795b88cb 2023-07-10 thomas return 0;
3933 795b88cb 2023-07-10 thomas return n;
3936 795b88cb 2023-07-10 thomas static const struct got_error *
3937 795b88cb 2023-07-10 thomas cmd_log(int argc, char *argv[])
3939 795b88cb 2023-07-10 thomas const struct got_error *error;
3940 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
3941 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
3942 795b88cb 2023-07-10 thomas struct got_object_id *start_id = NULL, *end_id = NULL;
3943 795b88cb 2023-07-10 thomas char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3944 795b88cb 2023-07-10 thomas const char *start_commit = NULL, *end_commit = NULL;
3945 795b88cb 2023-07-10 thomas const char *search_pattern = NULL;
3946 795b88cb 2023-07-10 thomas int diff_context = -1, ch;
3947 795b88cb 2023-07-10 thomas int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3948 795b88cb 2023-07-10 thomas int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
3949 795b88cb 2023-07-10 thomas const char *errstr;
3950 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
3951 795b88cb 2023-07-10 thomas struct got_reflist_object_id_map *refs_idmap = NULL;
3952 795b88cb 2023-07-10 thomas FILE *tmpfile = NULL;
3953 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
3955 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
3957 795b88cb 2023-07-10 thomas #ifndef PROFILE
3958 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3961 795b88cb 2023-07-10 thomas err(1, "pledge");
3964 795b88cb 2023-07-10 thomas limit = get_default_log_limit();
3966 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:sx:")) != -1) {
3967 795b88cb 2023-07-10 thomas switch (ch) {
3968 795b88cb 2023-07-10 thomas case 'b':
3969 795b88cb 2023-07-10 thomas log_branches = 1;
3971 795b88cb 2023-07-10 thomas case 'C':
3972 795b88cb 2023-07-10 thomas diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3973 795b88cb 2023-07-10 thomas &errstr);
3974 795b88cb 2023-07-10 thomas if (errstr != NULL)
3975 795b88cb 2023-07-10 thomas errx(1, "number of context lines is %s: %s",
3976 795b88cb 2023-07-10 thomas errstr, optarg);
3978 795b88cb 2023-07-10 thomas case 'c':
3979 795b88cb 2023-07-10 thomas start_commit = optarg;
3981 795b88cb 2023-07-10 thomas case 'd':
3982 795b88cb 2023-07-10 thomas show_diffstat = 1;
3984 795b88cb 2023-07-10 thomas case 'l':
3985 795b88cb 2023-07-10 thomas limit = strtonum(optarg, 0, INT_MAX, &errstr);
3986 795b88cb 2023-07-10 thomas if (errstr != NULL)
3987 795b88cb 2023-07-10 thomas errx(1, "number of commits is %s: %s",
3988 795b88cb 2023-07-10 thomas errstr, optarg);
3990 795b88cb 2023-07-10 thomas case 'P':
3991 795b88cb 2023-07-10 thomas show_changed_paths = 1;
3993 795b88cb 2023-07-10 thomas case 'p':
3994 795b88cb 2023-07-10 thomas show_patch = 1;
3996 795b88cb 2023-07-10 thomas case 'R':
3997 795b88cb 2023-07-10 thomas reverse_display_order = 1;
3999 795b88cb 2023-07-10 thomas case 'r':
4000 795b88cb 2023-07-10 thomas repo_path = realpath(optarg, NULL);
4001 795b88cb 2023-07-10 thomas if (repo_path == NULL)
4002 795b88cb 2023-07-10 thomas return got_error_from_errno2("realpath",
4003 795b88cb 2023-07-10 thomas optarg);
4004 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
4006 795b88cb 2023-07-10 thomas case 'S':
4007 795b88cb 2023-07-10 thomas search_pattern = optarg;
4009 795b88cb 2023-07-10 thomas case 's':
4010 795b88cb 2023-07-10 thomas one_line = 1;
4012 795b88cb 2023-07-10 thomas case 'x':
4013 795b88cb 2023-07-10 thomas end_commit = optarg;
4015 795b88cb 2023-07-10 thomas default:
4016 795b88cb 2023-07-10 thomas usage_log();
4017 795b88cb 2023-07-10 thomas /* NOTREACHED */
4021 795b88cb 2023-07-10 thomas argc -= optind;
4022 795b88cb 2023-07-10 thomas argv += optind;
4024 795b88cb 2023-07-10 thomas if (diff_context == -1)
4025 795b88cb 2023-07-10 thomas diff_context = 3;
4026 795b88cb 2023-07-10 thomas else if (!show_patch)
4027 795b88cb 2023-07-10 thomas errx(1, "-C requires -p");
4029 795b88cb 2023-07-10 thomas if (one_line && (show_patch || show_changed_paths || show_diffstat))
4030 795b88cb 2023-07-10 thomas errx(1, "cannot use -s with -d, -p or -P");
4032 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
4033 795b88cb 2023-07-10 thomas if (cwd == NULL) {
4034 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
4035 795b88cb 2023-07-10 thomas goto done;
4038 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
4039 795b88cb 2023-07-10 thomas if (error != NULL)
4040 795b88cb 2023-07-10 thomas goto done;
4042 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
4043 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
4044 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
4045 795b88cb 2023-07-10 thomas goto done;
4046 795b88cb 2023-07-10 thomas error = NULL;
4049 795b88cb 2023-07-10 thomas if (argc == 1) {
4050 795b88cb 2023-07-10 thomas if (worktree) {
4051 795b88cb 2023-07-10 thomas error = got_worktree_resolve_path(&path, worktree,
4052 795b88cb 2023-07-10 thomas argv[0]);
4053 795b88cb 2023-07-10 thomas if (error)
4054 795b88cb 2023-07-10 thomas goto done;
4055 795b88cb 2023-07-10 thomas } else {
4056 795b88cb 2023-07-10 thomas path = strdup(argv[0]);
4057 795b88cb 2023-07-10 thomas if (path == NULL) {
4058 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
4059 795b88cb 2023-07-10 thomas goto done;
4062 795b88cb 2023-07-10 thomas } else if (argc != 0)
4063 795b88cb 2023-07-10 thomas usage_log();
4065 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
4066 795b88cb 2023-07-10 thomas repo_path = worktree ?
4067 795b88cb 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4069 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
4070 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
4071 795b88cb 2023-07-10 thomas goto done;
4074 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4075 795b88cb 2023-07-10 thomas if (error != NULL)
4076 795b88cb 2023-07-10 thomas goto done;
4078 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1,
4079 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
4080 795b88cb 2023-07-10 thomas if (error)
4081 795b88cb 2023-07-10 thomas goto done;
4083 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4084 795b88cb 2023-07-10 thomas if (error)
4085 795b88cb 2023-07-10 thomas goto done;
4087 795b88cb 2023-07-10 thomas error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4088 795b88cb 2023-07-10 thomas if (error)
4089 795b88cb 2023-07-10 thomas goto done;
4091 795b88cb 2023-07-10 thomas if (start_commit == NULL) {
4092 795b88cb 2023-07-10 thomas struct got_reference *head_ref;
4093 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
4094 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo,
4095 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_head_ref_name(worktree)
4096 795b88cb 2023-07-10 thomas : GOT_REF_HEAD, 0);
4097 795b88cb 2023-07-10 thomas if (error != NULL)
4098 795b88cb 2023-07-10 thomas goto done;
4099 795b88cb 2023-07-10 thomas error = got_ref_resolve(&start_id, repo, head_ref);
4100 795b88cb 2023-07-10 thomas got_ref_close(head_ref);
4101 795b88cb 2023-07-10 thomas if (error != NULL)
4102 795b88cb 2023-07-10 thomas goto done;
4103 795b88cb 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo,
4104 795b88cb 2023-07-10 thomas start_id);
4105 795b88cb 2023-07-10 thomas if (error != NULL)
4106 795b88cb 2023-07-10 thomas goto done;
4107 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
4108 795b88cb 2023-07-10 thomas } else {
4109 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&start_id, NULL,
4110 795b88cb 2023-07-10 thomas start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4111 795b88cb 2023-07-10 thomas if (error != NULL)
4112 795b88cb 2023-07-10 thomas goto done;
4114 795b88cb 2023-07-10 thomas if (end_commit != NULL) {
4115 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&end_id, NULL,
4116 795b88cb 2023-07-10 thomas end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4117 795b88cb 2023-07-10 thomas if (error != NULL)
4118 795b88cb 2023-07-10 thomas goto done;
4121 795b88cb 2023-07-10 thomas if (worktree) {
4123 795b88cb 2023-07-10 thomas * If a path was specified on the command line it was resolved
4124 795b88cb 2023-07-10 thomas * to a path in the work tree above. Prepend the work tree's
4125 795b88cb 2023-07-10 thomas * path prefix to obtain the corresponding in-repository path.
4127 795b88cb 2023-07-10 thomas if (path) {
4128 795b88cb 2023-07-10 thomas const char *prefix;
4129 795b88cb 2023-07-10 thomas prefix = got_worktree_get_path_prefix(worktree);
4130 795b88cb 2023-07-10 thomas if (asprintf(&in_repo_path, "%s%s%s", prefix,
4131 795b88cb 2023-07-10 thomas (path[0] != '\0') ? "/" : "", path) == -1) {
4132 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
4133 795b88cb 2023-07-10 thomas goto done;
4137 795b88cb 2023-07-10 thomas error = got_repo_map_path(&in_repo_path, repo,
4138 795b88cb 2023-07-10 thomas path ? path : "");
4139 795b88cb 2023-07-10 thomas if (error != NULL)
4140 795b88cb 2023-07-10 thomas goto done;
4141 795b88cb 2023-07-10 thomas if (in_repo_path) {
4142 795b88cb 2023-07-10 thomas free(path);
4143 795b88cb 2023-07-10 thomas path = in_repo_path;
4146 795b88cb 2023-07-10 thomas if (worktree) {
4147 795b88cb 2023-07-10 thomas /* Release work tree lock. */
4148 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
4149 795b88cb 2023-07-10 thomas worktree = NULL;
4152 795b88cb 2023-07-10 thomas if (search_pattern && show_patch) {
4153 795b88cb 2023-07-10 thomas tmpfile = got_opentemp();
4154 795b88cb 2023-07-10 thomas if (tmpfile == NULL) {
4155 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
4156 795b88cb 2023-07-10 thomas goto done;
4160 795b88cb 2023-07-10 thomas error = print_commits(start_id, end_id, repo, path ? path : "",
4161 795b88cb 2023-07-10 thomas show_changed_paths, show_diffstat, show_patch, search_pattern,
4162 795b88cb 2023-07-10 thomas diff_context, limit, log_branches, reverse_display_order,
4163 795b88cb 2023-07-10 thomas refs_idmap, one_line, tmpfile);
4165 795b88cb 2023-07-10 thomas free(path);
4166 795b88cb 2023-07-10 thomas free(repo_path);
4167 795b88cb 2023-07-10 thomas free(cwd);
4168 609a7400 2023-07-17 thomas free(start_id);
4169 609a7400 2023-07-17 thomas free(end_id);
4170 795b88cb 2023-07-10 thomas if (worktree)
4171 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
4172 795b88cb 2023-07-10 thomas if (repo) {
4173 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
4174 795b88cb 2023-07-10 thomas if (error == NULL)
4175 795b88cb 2023-07-10 thomas error = close_err;
4177 795b88cb 2023-07-10 thomas if (pack_fds) {
4178 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
4179 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
4180 795b88cb 2023-07-10 thomas if (error == NULL)
4181 795b88cb 2023-07-10 thomas error = pack_err;
4183 795b88cb 2023-07-10 thomas if (refs_idmap)
4184 795b88cb 2023-07-10 thomas got_reflist_object_id_map_free(refs_idmap);
4185 795b88cb 2023-07-10 thomas if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4186 795b88cb 2023-07-10 thomas error = got_error_from_errno("fclose");
4187 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
4188 795b88cb 2023-07-10 thomas return error;
4191 795b88cb 2023-07-10 thomas __dead static void
4192 795b88cb 2023-07-10 thomas usage_diff(void)
4194 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4195 795b88cb 2023-07-10 thomas "[-r repository-path] [object1 object2 | path ...]\n",
4196 795b88cb 2023-07-10 thomas getprogname());
4197 795b88cb 2023-07-10 thomas exit(1);
4200 795b88cb 2023-07-10 thomas struct print_diff_arg {
4201 795b88cb 2023-07-10 thomas struct got_repository *repo;
4202 795b88cb 2023-07-10 thomas struct got_worktree *worktree;
4203 795b88cb 2023-07-10 thomas struct got_diffstat_cb_arg *diffstat;
4204 795b88cb 2023-07-10 thomas int diff_context;
4205 795b88cb 2023-07-10 thomas const char *id_str;
4206 795b88cb 2023-07-10 thomas int header_shown;
4207 795b88cb 2023-07-10 thomas int diff_staged;
4208 795b88cb 2023-07-10 thomas enum got_diff_algorithm diff_algo;
4209 795b88cb 2023-07-10 thomas int ignore_whitespace;
4210 795b88cb 2023-07-10 thomas int force_text_diff;
4211 795b88cb 2023-07-10 thomas FILE *f1;
4212 795b88cb 2023-07-10 thomas FILE *f2;
4213 795b88cb 2023-07-10 thomas FILE *outfile;
4217 795b88cb 2023-07-10 thomas * Create a file which contains the target path of a symlink so we can feed
4218 795b88cb 2023-07-10 thomas * it as content to the diff engine.
4220 795b88cb 2023-07-10 thomas static const struct got_error *
4221 795b88cb 2023-07-10 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4222 795b88cb 2023-07-10 thomas const char *abspath)
4224 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
4225 795b88cb 2023-07-10 thomas char target_path[PATH_MAX];
4226 795b88cb 2023-07-10 thomas ssize_t target_len, outlen;
4228 795b88cb 2023-07-10 thomas *fd = -1;
4230 795b88cb 2023-07-10 thomas if (dirfd != -1) {
4231 795b88cb 2023-07-10 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4232 795b88cb 2023-07-10 thomas if (target_len == -1)
4233 795b88cb 2023-07-10 thomas return got_error_from_errno2("readlinkat", abspath);
4234 795b88cb 2023-07-10 thomas } else {
4235 795b88cb 2023-07-10 thomas target_len = readlink(abspath, target_path, PATH_MAX);
4236 795b88cb 2023-07-10 thomas if (target_len == -1)
4237 795b88cb 2023-07-10 thomas return got_error_from_errno2("readlink", abspath);
4240 795b88cb 2023-07-10 thomas *fd = got_opentempfd();
4241 795b88cb 2023-07-10 thomas if (*fd == -1)
4242 795b88cb 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
4244 795b88cb 2023-07-10 thomas outlen = write(*fd, target_path, target_len);
4245 795b88cb 2023-07-10 thomas if (outlen == -1) {
4246 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
4247 795b88cb 2023-07-10 thomas goto done;
4250 795b88cb 2023-07-10 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
4251 795b88cb 2023-07-10 thomas err = got_error_from_errno2("lseek", abspath);
4252 795b88cb 2023-07-10 thomas goto done;
4255 795b88cb 2023-07-10 thomas if (err) {
4256 795b88cb 2023-07-10 thomas close(*fd);
4257 795b88cb 2023-07-10 thomas *fd = -1;
4259 795b88cb 2023-07-10 thomas return err;
4262 795b88cb 2023-07-10 thomas static const struct got_error *
4263 795b88cb 2023-07-10 thomas print_diff(void *arg, unsigned char status, unsigned char staged_status,
4264 795b88cb 2023-07-10 thomas const char *path, struct got_object_id *blob_id,
4265 795b88cb 2023-07-10 thomas struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4266 795b88cb 2023-07-10 thomas int dirfd, const char *de_name)
4268 795b88cb 2023-07-10 thomas struct print_diff_arg *a = arg;
4269 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
4270 795b88cb 2023-07-10 thomas struct got_blob_object *blob1 = NULL;
4271 795b88cb 2023-07-10 thomas int fd = -1, fd1 = -1, fd2 = -1;
4272 795b88cb 2023-07-10 thomas FILE *f2 = NULL;
4273 795b88cb 2023-07-10 thomas char *abspath = NULL, *label1 = NULL;
4274 795b88cb 2023-07-10 thomas struct stat sb;
4275 795b88cb 2023-07-10 thomas off_t size1 = 0;
4276 795b88cb 2023-07-10 thomas int f2_exists = 0;
4278 795b88cb 2023-07-10 thomas memset(&sb, 0, sizeof(sb));
4280 795b88cb 2023-07-10 thomas if (a->diff_staged) {
4281 795b88cb 2023-07-10 thomas if (staged_status != GOT_STATUS_MODIFY &&
4282 795b88cb 2023-07-10 thomas staged_status != GOT_STATUS_ADD &&
4283 795b88cb 2023-07-10 thomas staged_status != GOT_STATUS_DELETE)
4284 795b88cb 2023-07-10 thomas return NULL;
4285 795b88cb 2023-07-10 thomas } else {
4286 795b88cb 2023-07-10 thomas if (staged_status == GOT_STATUS_DELETE)
4287 795b88cb 2023-07-10 thomas return NULL;
4288 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_NONEXISTENT)
4289 795b88cb 2023-07-10 thomas return got_error_set_errno(ENOENT, path);
4290 795b88cb 2023-07-10 thomas if (status != GOT_STATUS_MODIFY &&
4291 795b88cb 2023-07-10 thomas status != GOT_STATUS_ADD &&
4292 795b88cb 2023-07-10 thomas status != GOT_STATUS_DELETE &&
4293 795b88cb 2023-07-10 thomas status != GOT_STATUS_CONFLICT)
4294 795b88cb 2023-07-10 thomas return NULL;
4297 795b88cb 2023-07-10 thomas err = got_opentemp_truncate(a->f1);
4298 795b88cb 2023-07-10 thomas if (err)
4299 795b88cb 2023-07-10 thomas return got_error_from_errno("got_opentemp_truncate");
4300 795b88cb 2023-07-10 thomas err = got_opentemp_truncate(a->f2);
4301 795b88cb 2023-07-10 thomas if (err)
4302 795b88cb 2023-07-10 thomas return got_error_from_errno("got_opentemp_truncate");
4304 795b88cb 2023-07-10 thomas if (!a->header_shown) {
4305 795b88cb 2023-07-10 thomas if (fprintf(a->outfile, "diff %s%s\n",
4306 795b88cb 2023-07-10 thomas a->diff_staged ? "-s " : "",
4307 795b88cb 2023-07-10 thomas got_worktree_get_root_path(a->worktree)) < 0) {
4308 795b88cb 2023-07-10 thomas err = got_error_from_errno("fprintf");
4309 795b88cb 2023-07-10 thomas goto done;
4311 795b88cb 2023-07-10 thomas if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
4312 795b88cb 2023-07-10 thomas err = got_error_from_errno("fprintf");
4313 795b88cb 2023-07-10 thomas goto done;
4315 795b88cb 2023-07-10 thomas if (fprintf(a->outfile, "path + %s%s\n",
4316 795b88cb 2023-07-10 thomas got_worktree_get_root_path(a->worktree),
4317 795b88cb 2023-07-10 thomas a->diff_staged ? " (staged changes)" : "") < 0) {
4318 795b88cb 2023-07-10 thomas err = got_error_from_errno("fprintf");
4319 795b88cb 2023-07-10 thomas goto done;
4321 795b88cb 2023-07-10 thomas a->header_shown = 1;
4324 795b88cb 2023-07-10 thomas if (a->diff_staged) {
4325 795b88cb 2023-07-10 thomas const char *label1 = NULL, *label2 = NULL;
4326 795b88cb 2023-07-10 thomas switch (staged_status) {
4327 795b88cb 2023-07-10 thomas case GOT_STATUS_MODIFY:
4328 795b88cb 2023-07-10 thomas label1 = path;
4329 795b88cb 2023-07-10 thomas label2 = path;
4331 795b88cb 2023-07-10 thomas case GOT_STATUS_ADD:
4332 795b88cb 2023-07-10 thomas label2 = path;
4334 795b88cb 2023-07-10 thomas case GOT_STATUS_DELETE:
4335 795b88cb 2023-07-10 thomas label1 = path;
4337 795b88cb 2023-07-10 thomas default:
4338 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_FILE_STATUS);
4340 795b88cb 2023-07-10 thomas fd1 = got_opentempfd();
4341 795b88cb 2023-07-10 thomas if (fd1 == -1) {
4342 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
4343 795b88cb 2023-07-10 thomas goto done;
4345 795b88cb 2023-07-10 thomas fd2 = got_opentempfd();
4346 795b88cb 2023-07-10 thomas if (fd2 == -1) {
4347 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
4348 795b88cb 2023-07-10 thomas goto done;
4350 795b88cb 2023-07-10 thomas err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4351 795b88cb 2023-07-10 thomas fd1, fd2, blob_id, staged_blob_id, label1, label2,
4352 795b88cb 2023-07-10 thomas a->diff_algo, a->diff_context, a->ignore_whitespace,
4353 795b88cb 2023-07-10 thomas a->force_text_diff, a->diffstat, a->repo, a->outfile);
4354 795b88cb 2023-07-10 thomas goto done;
4357 795b88cb 2023-07-10 thomas fd1 = got_opentempfd();
4358 795b88cb 2023-07-10 thomas if (fd1 == -1) {
4359 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
4360 795b88cb 2023-07-10 thomas goto done;
4363 795b88cb 2023-07-10 thomas if (staged_status == GOT_STATUS_ADD ||
4364 795b88cb 2023-07-10 thomas staged_status == GOT_STATUS_MODIFY) {
4365 795b88cb 2023-07-10 thomas char *id_str;
4366 795b88cb 2023-07-10 thomas err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4367 795b88cb 2023-07-10 thomas 8192, fd1);
4368 795b88cb 2023-07-10 thomas if (err)
4369 795b88cb 2023-07-10 thomas goto done;
4370 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, staged_blob_id);
4371 795b88cb 2023-07-10 thomas if (err)
4372 795b88cb 2023-07-10 thomas goto done;
4373 795b88cb 2023-07-10 thomas if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4374 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
4375 795b88cb 2023-07-10 thomas free(id_str);
4376 795b88cb 2023-07-10 thomas goto done;
4378 795b88cb 2023-07-10 thomas free(id_str);
4379 795b88cb 2023-07-10 thomas } else if (status != GOT_STATUS_ADD) {
4380 795b88cb 2023-07-10 thomas err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4382 795b88cb 2023-07-10 thomas if (err)
4383 795b88cb 2023-07-10 thomas goto done;
4386 795b88cb 2023-07-10 thomas if (status != GOT_STATUS_DELETE) {
4387 795b88cb 2023-07-10 thomas if (asprintf(&abspath, "%s/%s",
4388 795b88cb 2023-07-10 thomas got_worktree_get_root_path(a->worktree), path) == -1) {
4389 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
4390 795b88cb 2023-07-10 thomas goto done;
4393 795b88cb 2023-07-10 thomas if (dirfd != -1) {
4394 795b88cb 2023-07-10 thomas fd = openat(dirfd, de_name,
4395 795b88cb 2023-07-10 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4396 795b88cb 2023-07-10 thomas if (fd == -1) {
4397 795b88cb 2023-07-10 thomas if (!got_err_open_nofollow_on_symlink()) {
4398 795b88cb 2023-07-10 thomas err = got_error_from_errno2("openat",
4399 795b88cb 2023-07-10 thomas abspath);
4400 795b88cb 2023-07-10 thomas goto done;
4402 795b88cb 2023-07-10 thomas err = get_symlink_target_file(&fd, dirfd,
4403 795b88cb 2023-07-10 thomas de_name, abspath);
4404 795b88cb 2023-07-10 thomas if (err)
4405 795b88cb 2023-07-10 thomas goto done;
4407 795b88cb 2023-07-10 thomas } else {
4408 795b88cb 2023-07-10 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4409 795b88cb 2023-07-10 thomas if (fd == -1) {
4410 795b88cb 2023-07-10 thomas if (!got_err_open_nofollow_on_symlink()) {
4411 795b88cb 2023-07-10 thomas err = got_error_from_errno2("open",
4412 795b88cb 2023-07-10 thomas abspath);
4413 795b88cb 2023-07-10 thomas goto done;
4415 795b88cb 2023-07-10 thomas err = get_symlink_target_file(&fd, dirfd,
4416 795b88cb 2023-07-10 thomas de_name, abspath);
4417 795b88cb 2023-07-10 thomas if (err)
4418 795b88cb 2023-07-10 thomas goto done;
4421 795b88cb 2023-07-10 thomas if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
4422 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fstatat", abspath);
4423 795b88cb 2023-07-10 thomas goto done;
4425 795b88cb 2023-07-10 thomas f2 = fdopen(fd, "r");
4426 795b88cb 2023-07-10 thomas if (f2 == NULL) {
4427 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fdopen", abspath);
4428 795b88cb 2023-07-10 thomas goto done;
4430 795b88cb 2023-07-10 thomas fd = -1;
4431 795b88cb 2023-07-10 thomas f2_exists = 1;
4434 795b88cb 2023-07-10 thomas if (blob1) {
4435 795b88cb 2023-07-10 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
4436 795b88cb 2023-07-10 thomas a->f1, blob1);
4437 795b88cb 2023-07-10 thomas if (err)
4438 795b88cb 2023-07-10 thomas goto done;
4441 795b88cb 2023-07-10 thomas err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
4442 795b88cb 2023-07-10 thomas f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
4443 795b88cb 2023-07-10 thomas a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
4445 795b88cb 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4446 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
4447 795b88cb 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4448 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
4449 795b88cb 2023-07-10 thomas if (blob1)
4450 795b88cb 2023-07-10 thomas got_object_blob_close(blob1);
4451 795b88cb 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4452 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
4453 795b88cb 2023-07-10 thomas if (f2 && fclose(f2) == EOF && err == NULL)
4454 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
4455 795b88cb 2023-07-10 thomas free(abspath);
4456 795b88cb 2023-07-10 thomas return err;
4459 795b88cb 2023-07-10 thomas static const struct got_error *
4460 795b88cb 2023-07-10 thomas cmd_diff(int argc, char *argv[])
4462 795b88cb 2023-07-10 thomas const struct got_error *error;
4463 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
4464 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
4465 795b88cb 2023-07-10 thomas char *cwd = NULL, *repo_path = NULL;
4466 795b88cb 2023-07-10 thomas const char *commit_args[2] = { NULL, NULL };
4467 795b88cb 2023-07-10 thomas int ncommit_args = 0;
4468 795b88cb 2023-07-10 thomas struct got_object_id *ids[2] = { NULL, NULL };
4469 795b88cb 2023-07-10 thomas char *labels[2] = { NULL, NULL };
4470 795b88cb 2023-07-10 thomas int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4471 795b88cb 2023-07-10 thomas int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4472 795b88cb 2023-07-10 thomas int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
4473 795b88cb 2023-07-10 thomas const char *errstr;
4474 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
4475 795b88cb 2023-07-10 thomas struct got_pathlist_head diffstat_paths, paths;
4476 795b88cb 2023-07-10 thomas FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4477 795b88cb 2023-07-10 thomas int fd1 = -1, fd2 = -1;
4478 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
4479 795b88cb 2023-07-10 thomas struct got_diffstat_cb_arg dsa;
4481 795b88cb 2023-07-10 thomas memset(&dsa, 0, sizeof(dsa));
4483 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
4484 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
4485 795b88cb 2023-07-10 thomas TAILQ_INIT(&diffstat_paths);
4487 795b88cb 2023-07-10 thomas #ifndef PROFILE
4488 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4489 795b88cb 2023-07-10 thomas NULL) == -1)
4490 795b88cb 2023-07-10 thomas err(1, "pledge");
4493 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
4494 795b88cb 2023-07-10 thomas switch (ch) {
4495 795b88cb 2023-07-10 thomas case 'a':
4496 795b88cb 2023-07-10 thomas force_text_diff = 1;
4498 795b88cb 2023-07-10 thomas case 'C':
4499 795b88cb 2023-07-10 thomas diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4500 795b88cb 2023-07-10 thomas &errstr);
4501 795b88cb 2023-07-10 thomas if (errstr != NULL)
4502 795b88cb 2023-07-10 thomas errx(1, "number of context lines is %s: %s",
4503 795b88cb 2023-07-10 thomas errstr, optarg);
4505 795b88cb 2023-07-10 thomas case 'c':
4506 795b88cb 2023-07-10 thomas if (ncommit_args >= 2)
4507 795b88cb 2023-07-10 thomas errx(1, "too many -c options used");
4508 795b88cb 2023-07-10 thomas commit_args[ncommit_args++] = optarg;
4510 795b88cb 2023-07-10 thomas case 'd':
4511 795b88cb 2023-07-10 thomas show_diffstat = 1;
4513 795b88cb 2023-07-10 thomas case 'P':
4514 795b88cb 2023-07-10 thomas force_path = 1;
4516 795b88cb 2023-07-10 thomas case 'r':
4517 795b88cb 2023-07-10 thomas repo_path = realpath(optarg, NULL);
4518 795b88cb 2023-07-10 thomas if (repo_path == NULL)
4519 795b88cb 2023-07-10 thomas return got_error_from_errno2("realpath",
4520 795b88cb 2023-07-10 thomas optarg);
4521 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
4522 795b88cb 2023-07-10 thomas rflag = 1;
4524 795b88cb 2023-07-10 thomas case 's':
4525 795b88cb 2023-07-10 thomas diff_staged = 1;
4527 795b88cb 2023-07-10 thomas case 'w':
4528 795b88cb 2023-07-10 thomas ignore_whitespace = 1;
4530 795b88cb 2023-07-10 thomas default:
4531 795b88cb 2023-07-10 thomas usage_diff();
4532 795b88cb 2023-07-10 thomas /* NOTREACHED */
4536 795b88cb 2023-07-10 thomas argc -= optind;
4537 795b88cb 2023-07-10 thomas argv += optind;
4539 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
4540 795b88cb 2023-07-10 thomas if (cwd == NULL) {
4541 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
4542 795b88cb 2023-07-10 thomas goto done;
4545 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
4546 795b88cb 2023-07-10 thomas if (error != NULL)
4547 795b88cb 2023-07-10 thomas goto done;
4549 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
4550 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
4551 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
4552 795b88cb 2023-07-10 thomas goto done;
4554 795b88cb 2023-07-10 thomas error = NULL;
4555 795b88cb 2023-07-10 thomas if (worktree) {
4556 795b88cb 2023-07-10 thomas repo_path =
4557 795b88cb 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree));
4558 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
4559 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
4560 795b88cb 2023-07-10 thomas goto done;
4562 795b88cb 2023-07-10 thomas } else {
4563 795b88cb 2023-07-10 thomas repo_path = strdup(cwd);
4564 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
4565 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
4566 795b88cb 2023-07-10 thomas goto done;
4571 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4572 795b88cb 2023-07-10 thomas free(repo_path);
4573 795b88cb 2023-07-10 thomas if (error != NULL)
4574 795b88cb 2023-07-10 thomas goto done;
4576 795b88cb 2023-07-10 thomas if (show_diffstat) {
4577 795b88cb 2023-07-10 thomas dsa.paths = &diffstat_paths;
4578 795b88cb 2023-07-10 thomas dsa.force_text = force_text_diff;
4579 795b88cb 2023-07-10 thomas dsa.ignore_ws = ignore_whitespace;
4580 795b88cb 2023-07-10 thomas dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
4583 795b88cb 2023-07-10 thomas if (rflag || worktree == NULL || ncommit_args > 0) {
4584 795b88cb 2023-07-10 thomas if (force_path) {
4585 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_NOT_IMPL,
4586 795b88cb 2023-07-10 thomas "-P option can only be used when diffing "
4587 795b88cb 2023-07-10 thomas "a work tree");
4588 795b88cb 2023-07-10 thomas goto done;
4590 795b88cb 2023-07-10 thomas if (diff_staged) {
4591 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_NOT_IMPL,
4592 795b88cb 2023-07-10 thomas "-s option can only be used when diffing "
4593 795b88cb 2023-07-10 thomas "a work tree");
4594 795b88cb 2023-07-10 thomas goto done;
4598 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1,
4599 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
4600 795b88cb 2023-07-10 thomas if (error)
4601 795b88cb 2023-07-10 thomas goto done;
4603 795b88cb 2023-07-10 thomas if ((!force_path && argc == 2) || ncommit_args > 0) {
4604 795b88cb 2023-07-10 thomas int obj_type = (ncommit_args > 0 ?
4605 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
4606 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4608 795b88cb 2023-07-10 thomas if (error)
4609 795b88cb 2023-07-10 thomas goto done;
4610 795b88cb 2023-07-10 thomas for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
4611 795b88cb 2023-07-10 thomas const char *arg;
4612 795b88cb 2023-07-10 thomas if (ncommit_args > 0)
4613 795b88cb 2023-07-10 thomas arg = commit_args[i];
4615 795b88cb 2023-07-10 thomas arg = argv[i];
4616 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&ids[i], &labels[i],
4617 795b88cb 2023-07-10 thomas arg, obj_type, &refs, repo);
4618 795b88cb 2023-07-10 thomas if (error) {
4619 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_NOT_REF &&
4620 795b88cb 2023-07-10 thomas error->code != GOT_ERR_NO_OBJ)
4621 795b88cb 2023-07-10 thomas goto done;
4622 795b88cb 2023-07-10 thomas if (ncommit_args > 0)
4623 795b88cb 2023-07-10 thomas goto done;
4624 795b88cb 2023-07-10 thomas error = NULL;
4630 795b88cb 2023-07-10 thomas f1 = got_opentemp();
4631 795b88cb 2023-07-10 thomas if (f1 == NULL) {
4632 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
4633 795b88cb 2023-07-10 thomas goto done;
4636 795b88cb 2023-07-10 thomas f2 = got_opentemp();
4637 795b88cb 2023-07-10 thomas if (f2 == NULL) {
4638 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
4639 795b88cb 2023-07-10 thomas goto done;
4642 795b88cb 2023-07-10 thomas outfile = got_opentemp();
4643 795b88cb 2023-07-10 thomas if (outfile == NULL) {
4644 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
4645 795b88cb 2023-07-10 thomas goto done;
4648 795b88cb 2023-07-10 thomas if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
4649 795b88cb 2023-07-10 thomas struct print_diff_arg arg;
4650 795b88cb 2023-07-10 thomas char *id_str;
4652 795b88cb 2023-07-10 thomas if (worktree == NULL) {
4653 795b88cb 2023-07-10 thomas if (argc == 2 && ids[0] == NULL) {
4654 795b88cb 2023-07-10 thomas error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
4655 795b88cb 2023-07-10 thomas goto done;
4656 795b88cb 2023-07-10 thomas } else if (argc == 2 && ids[1] == NULL) {
4657 795b88cb 2023-07-10 thomas error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
4658 795b88cb 2023-07-10 thomas goto done;
4659 795b88cb 2023-07-10 thomas } else if (argc > 0) {
4660 795b88cb 2023-07-10 thomas error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
4661 795b88cb 2023-07-10 thomas "%s", "specified paths cannot be resolved");
4662 795b88cb 2023-07-10 thomas goto done;
4663 795b88cb 2023-07-10 thomas } else {
4664 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_NOT_WORKTREE);
4665 795b88cb 2023-07-10 thomas goto done;
4669 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv,
4670 795b88cb 2023-07-10 thomas worktree);
4671 795b88cb 2023-07-10 thomas if (error)
4672 795b88cb 2023-07-10 thomas goto done;
4674 795b88cb 2023-07-10 thomas error = got_object_id_str(&id_str,
4675 795b88cb 2023-07-10 thomas got_worktree_get_base_commit_id(worktree));
4676 795b88cb 2023-07-10 thomas if (error)
4677 795b88cb 2023-07-10 thomas goto done;
4678 795b88cb 2023-07-10 thomas arg.repo = repo;
4679 795b88cb 2023-07-10 thomas arg.worktree = worktree;
4680 795b88cb 2023-07-10 thomas arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
4681 795b88cb 2023-07-10 thomas arg.diff_context = diff_context;
4682 795b88cb 2023-07-10 thomas arg.id_str = id_str;
4683 795b88cb 2023-07-10 thomas arg.header_shown = 0;
4684 795b88cb 2023-07-10 thomas arg.diff_staged = diff_staged;
4685 795b88cb 2023-07-10 thomas arg.ignore_whitespace = ignore_whitespace;
4686 795b88cb 2023-07-10 thomas arg.force_text_diff = force_text_diff;
4687 795b88cb 2023-07-10 thomas arg.diffstat = show_diffstat ? &dsa : NULL;
4688 795b88cb 2023-07-10 thomas arg.f1 = f1;
4689 795b88cb 2023-07-10 thomas arg.f2 = f2;
4690 795b88cb 2023-07-10 thomas arg.outfile = outfile;
4692 795b88cb 2023-07-10 thomas error = got_worktree_status(worktree, &paths, repo, 0,
4693 795b88cb 2023-07-10 thomas print_diff, &arg, check_cancelled, NULL);
4694 795b88cb 2023-07-10 thomas free(id_str);
4695 795b88cb 2023-07-10 thomas if (error)
4696 795b88cb 2023-07-10 thomas goto done;
4698 795b88cb 2023-07-10 thomas if (show_diffstat && dsa.nfiles > 0) {
4699 795b88cb 2023-07-10 thomas char *header;
4701 795b88cb 2023-07-10 thomas if (asprintf(&header, "diffstat %s%s",
4702 795b88cb 2023-07-10 thomas diff_staged ? "-s " : "",
4703 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree)) == -1) {
4704 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
4705 795b88cb 2023-07-10 thomas goto done;
4708 795b88cb 2023-07-10 thomas error = print_diffstat(&dsa, header);
4709 795b88cb 2023-07-10 thomas free(header);
4710 795b88cb 2023-07-10 thomas if (error)
4711 795b88cb 2023-07-10 thomas goto done;
4714 795b88cb 2023-07-10 thomas error = printfile(outfile);
4715 795b88cb 2023-07-10 thomas goto done;
4718 795b88cb 2023-07-10 thomas if (ncommit_args == 1) {
4719 795b88cb 2023-07-10 thomas struct got_commit_object *commit;
4720 795b88cb 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, ids[0]);
4721 795b88cb 2023-07-10 thomas if (error)
4722 795b88cb 2023-07-10 thomas goto done;
4724 795b88cb 2023-07-10 thomas labels[1] = labels[0];
4725 795b88cb 2023-07-10 thomas ids[1] = ids[0];
4726 795b88cb 2023-07-10 thomas if (got_object_commit_get_nparents(commit) > 0) {
4727 795b88cb 2023-07-10 thomas const struct got_object_id_queue *pids;
4728 795b88cb 2023-07-10 thomas struct got_object_qid *pid;
4729 795b88cb 2023-07-10 thomas pids = got_object_commit_get_parent_ids(commit);
4730 795b88cb 2023-07-10 thomas pid = STAILQ_FIRST(pids);
4731 795b88cb 2023-07-10 thomas ids[0] = got_object_id_dup(&pid->id);
4732 795b88cb 2023-07-10 thomas if (ids[0] == NULL) {
4733 795b88cb 2023-07-10 thomas error = got_error_from_errno(
4734 795b88cb 2023-07-10 thomas "got_object_id_dup");
4735 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
4736 795b88cb 2023-07-10 thomas goto done;
4738 795b88cb 2023-07-10 thomas error = got_object_id_str(&labels[0], ids[0]);
4739 795b88cb 2023-07-10 thomas if (error) {
4740 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
4741 795b88cb 2023-07-10 thomas goto done;
4743 795b88cb 2023-07-10 thomas } else {
4744 795b88cb 2023-07-10 thomas ids[0] = NULL;
4745 795b88cb 2023-07-10 thomas labels[0] = strdup("/dev/null");
4746 795b88cb 2023-07-10 thomas if (labels[0] == NULL) {
4747 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
4748 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
4749 795b88cb 2023-07-10 thomas goto done;
4753 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
4756 795b88cb 2023-07-10 thomas if (ncommit_args == 0 && argc > 2) {
4757 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_BAD_PATH,
4758 795b88cb 2023-07-10 thomas "path arguments cannot be used when diffing two objects");
4759 795b88cb 2023-07-10 thomas goto done;
4762 795b88cb 2023-07-10 thomas if (ids[0]) {
4763 795b88cb 2023-07-10 thomas error = got_object_get_type(&type1, repo, ids[0]);
4764 795b88cb 2023-07-10 thomas if (error)
4765 795b88cb 2023-07-10 thomas goto done;
4768 795b88cb 2023-07-10 thomas error = got_object_get_type(&type2, repo, ids[1]);
4769 795b88cb 2023-07-10 thomas if (error)
4770 795b88cb 2023-07-10 thomas goto done;
4771 795b88cb 2023-07-10 thomas if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
4772 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_OBJ_TYPE);
4773 795b88cb 2023-07-10 thomas goto done;
4775 795b88cb 2023-07-10 thomas if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
4776 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_OBJ_TYPE,
4777 795b88cb 2023-07-10 thomas "path arguments cannot be used when diffing blobs");
4778 795b88cb 2023-07-10 thomas goto done;
4781 795b88cb 2023-07-10 thomas for (i = 0; ncommit_args > 0 && i < argc; i++) {
4782 795b88cb 2023-07-10 thomas char *in_repo_path;
4783 795b88cb 2023-07-10 thomas struct got_pathlist_entry *new;
4784 795b88cb 2023-07-10 thomas if (worktree) {
4785 795b88cb 2023-07-10 thomas const char *prefix;
4786 795b88cb 2023-07-10 thomas char *p;
4787 795b88cb 2023-07-10 thomas error = got_worktree_resolve_path(&p, worktree,
4788 795b88cb 2023-07-10 thomas argv[i]);
4789 795b88cb 2023-07-10 thomas if (error)
4790 795b88cb 2023-07-10 thomas goto done;
4791 795b88cb 2023-07-10 thomas prefix = got_worktree_get_path_prefix(worktree);
4792 795b88cb 2023-07-10 thomas while (prefix[0] == '/')
4793 795b88cb 2023-07-10 thomas prefix++;
4794 795b88cb 2023-07-10 thomas if (asprintf(&in_repo_path, "%s%s%s", prefix,
4795 795b88cb 2023-07-10 thomas (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
4796 795b88cb 2023-07-10 thomas p) == -1) {
4797 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
4798 795b88cb 2023-07-10 thomas free(p);
4799 795b88cb 2023-07-10 thomas goto done;
4801 795b88cb 2023-07-10 thomas free(p);
4802 795b88cb 2023-07-10 thomas } else {
4803 795b88cb 2023-07-10 thomas char *mapped_path, *s;
4804 795b88cb 2023-07-10 thomas error = got_repo_map_path(&mapped_path, repo, argv[i]);
4805 795b88cb 2023-07-10 thomas if (error)
4806 795b88cb 2023-07-10 thomas goto done;
4807 795b88cb 2023-07-10 thomas s = mapped_path;
4808 795b88cb 2023-07-10 thomas while (s[0] == '/')
4810 795b88cb 2023-07-10 thomas in_repo_path = strdup(s);
4811 795b88cb 2023-07-10 thomas if (in_repo_path == NULL) {
4812 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
4813 795b88cb 2023-07-10 thomas free(mapped_path);
4814 795b88cb 2023-07-10 thomas goto done;
4816 795b88cb 2023-07-10 thomas free(mapped_path);
4819 795b88cb 2023-07-10 thomas error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
4820 795b88cb 2023-07-10 thomas if (error || new == NULL /* duplicate */)
4821 795b88cb 2023-07-10 thomas free(in_repo_path);
4822 795b88cb 2023-07-10 thomas if (error)
4823 795b88cb 2023-07-10 thomas goto done;
4826 795b88cb 2023-07-10 thomas if (worktree) {
4827 795b88cb 2023-07-10 thomas /* Release work tree lock. */
4828 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
4829 795b88cb 2023-07-10 thomas worktree = NULL;
4832 795b88cb 2023-07-10 thomas fd1 = got_opentempfd();
4833 795b88cb 2023-07-10 thomas if (fd1 == -1) {
4834 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentempfd");
4835 795b88cb 2023-07-10 thomas goto done;
4838 795b88cb 2023-07-10 thomas fd2 = got_opentempfd();
4839 795b88cb 2023-07-10 thomas if (fd2 == -1) {
4840 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentempfd");
4841 795b88cb 2023-07-10 thomas goto done;
4844 795b88cb 2023-07-10 thomas switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
4845 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_BLOB:
4846 795b88cb 2023-07-10 thomas error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
4847 795b88cb 2023-07-10 thomas fd1, fd2, ids[0], ids[1], NULL, NULL,
4848 795b88cb 2023-07-10 thomas GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
4849 795b88cb 2023-07-10 thomas ignore_whitespace, force_text_diff,
4850 795b88cb 2023-07-10 thomas show_diffstat ? &dsa : NULL, repo, outfile);
4852 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TREE:
4853 795b88cb 2023-07-10 thomas error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
4854 795b88cb 2023-07-10 thomas ids[0], ids[1], &paths, "", "",
4855 795b88cb 2023-07-10 thomas GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
4856 795b88cb 2023-07-10 thomas ignore_whitespace, force_text_diff,
4857 795b88cb 2023-07-10 thomas show_diffstat ? &dsa : NULL, repo, outfile);
4859 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_COMMIT:
4860 795b88cb 2023-07-10 thomas fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
4861 795b88cb 2023-07-10 thomas error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
4862 795b88cb 2023-07-10 thomas fd1, fd2, ids[0], ids[1], &paths,
4863 795b88cb 2023-07-10 thomas GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
4864 795b88cb 2023-07-10 thomas ignore_whitespace, force_text_diff,
4865 795b88cb 2023-07-10 thomas show_diffstat ? &dsa : NULL, repo, outfile);
4867 795b88cb 2023-07-10 thomas default:
4868 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_OBJ_TYPE);
4870 795b88cb 2023-07-10 thomas if (error)
4871 795b88cb 2023-07-10 thomas goto done;
4873 795b88cb 2023-07-10 thomas if (show_diffstat && dsa.nfiles > 0) {
4874 795b88cb 2023-07-10 thomas char *header = NULL;
4876 795b88cb 2023-07-10 thomas if (asprintf(&header, "diffstat %s %s",
4877 795b88cb 2023-07-10 thomas labels[0], labels[1]) == -1) {
4878 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
4879 795b88cb 2023-07-10 thomas goto done;
4882 795b88cb 2023-07-10 thomas error = print_diffstat(&dsa, header);
4883 795b88cb 2023-07-10 thomas free(header);
4884 795b88cb 2023-07-10 thomas if (error)
4885 795b88cb 2023-07-10 thomas goto done;
4888 795b88cb 2023-07-10 thomas error = printfile(outfile);
4891 795b88cb 2023-07-10 thomas free(labels[0]);
4892 795b88cb 2023-07-10 thomas free(labels[1]);
4893 795b88cb 2023-07-10 thomas free(ids[0]);
4894 795b88cb 2023-07-10 thomas free(ids[1]);
4895 795b88cb 2023-07-10 thomas if (worktree)
4896 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
4897 795b88cb 2023-07-10 thomas if (repo) {
4898 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
4899 795b88cb 2023-07-10 thomas if (error == NULL)
4900 795b88cb 2023-07-10 thomas error = close_err;
4902 795b88cb 2023-07-10 thomas if (pack_fds) {
4903 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
4904 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
4905 795b88cb 2023-07-10 thomas if (error == NULL)
4906 795b88cb 2023-07-10 thomas error = pack_err;
4908 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
4909 795b88cb 2023-07-10 thomas got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
4910 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
4911 795b88cb 2023-07-10 thomas if (outfile && fclose(outfile) == EOF && error == NULL)
4912 795b88cb 2023-07-10 thomas error = got_error_from_errno("fclose");
4913 795b88cb 2023-07-10 thomas if (f1 && fclose(f1) == EOF && error == NULL)
4914 795b88cb 2023-07-10 thomas error = got_error_from_errno("fclose");
4915 795b88cb 2023-07-10 thomas if (f2 && fclose(f2) == EOF && error == NULL)
4916 795b88cb 2023-07-10 thomas error = got_error_from_errno("fclose");
4917 795b88cb 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && error == NULL)
4918 795b88cb 2023-07-10 thomas error = got_error_from_errno("close");
4919 795b88cb 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && error == NULL)
4920 795b88cb 2023-07-10 thomas error = got_error_from_errno("close");
4921 795b88cb 2023-07-10 thomas return error;
4924 795b88cb 2023-07-10 thomas __dead static void
4925 795b88cb 2023-07-10 thomas usage_blame(void)
4927 795b88cb 2023-07-10 thomas fprintf(stderr,
4928 795b88cb 2023-07-10 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
4929 795b88cb 2023-07-10 thomas getprogname());
4930 795b88cb 2023-07-10 thomas exit(1);
4933 795b88cb 2023-07-10 thomas struct blame_line {
4934 795b88cb 2023-07-10 thomas int annotated;
4935 795b88cb 2023-07-10 thomas char *id_str;
4936 795b88cb 2023-07-10 thomas char *committer;
4937 795b88cb 2023-07-10 thomas char datebuf[11]; /* YYYY-MM-DD + NUL */
4940 795b88cb 2023-07-10 thomas struct blame_cb_args {
4941 795b88cb 2023-07-10 thomas struct blame_line *lines;
4942 795b88cb 2023-07-10 thomas int nlines;
4943 795b88cb 2023-07-10 thomas int nlines_prec;
4944 795b88cb 2023-07-10 thomas int lineno_cur;
4945 795b88cb 2023-07-10 thomas off_t *line_offsets;
4946 795b88cb 2023-07-10 thomas FILE *f;
4947 795b88cb 2023-07-10 thomas struct got_repository *repo;
4950 795b88cb 2023-07-10 thomas static const struct got_error *
4951 795b88cb 2023-07-10 thomas blame_cb(void *arg, int nlines, int lineno,
4952 795b88cb 2023-07-10 thomas struct got_commit_object *commit, struct got_object_id *id)
4954 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
4955 795b88cb 2023-07-10 thomas struct blame_cb_args *a = arg;
4956 795b88cb 2023-07-10 thomas struct blame_line *bline;
4957 795b88cb 2023-07-10 thomas char *line = NULL;
4958 795b88cb 2023-07-10 thomas size_t linesize = 0;
4959 795b88cb 2023-07-10 thomas off_t offset;
4960 795b88cb 2023-07-10 thomas struct tm tm;
4961 795b88cb 2023-07-10 thomas time_t committer_time;
4963 795b88cb 2023-07-10 thomas if (nlines != a->nlines ||
4964 795b88cb 2023-07-10 thomas (lineno != -1 && lineno < 1) || lineno > a->nlines)
4965 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_RANGE);
4967 795b88cb 2023-07-10 thomas if (sigint_received)
4968 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_ITER_COMPLETED);
4970 795b88cb 2023-07-10 thomas if (lineno == -1)
4971 795b88cb 2023-07-10 thomas return NULL; /* no change in this commit */
4973 795b88cb 2023-07-10 thomas /* Annotate this line. */
4974 795b88cb 2023-07-10 thomas bline = &a->lines[lineno - 1];
4975 795b88cb 2023-07-10 thomas if (bline->annotated)
4976 795b88cb 2023-07-10 thomas return NULL;
4977 795b88cb 2023-07-10 thomas err = got_object_id_str(&bline->id_str, id);
4978 795b88cb 2023-07-10 thomas if (err)
4979 795b88cb 2023-07-10 thomas return err;
4981 795b88cb 2023-07-10 thomas bline->committer = strdup(got_object_commit_get_committer(commit));
4982 795b88cb 2023-07-10 thomas if (bline->committer == NULL) {
4983 795b88cb 2023-07-10 thomas err = got_error_from_errno("strdup");
4984 795b88cb 2023-07-10 thomas goto done;
4987 795b88cb 2023-07-10 thomas committer_time = got_object_commit_get_committer_time(commit);
4988 795b88cb 2023-07-10 thomas if (gmtime_r(&committer_time, &tm) == NULL)
4989 795b88cb 2023-07-10 thomas return got_error_from_errno("gmtime_r");
4990 c24e2d2e 2024-04-25 thomas.ad if (strftime(bline->datebuf, sizeof(bline->datebuf), "%F", &tm) == 0) {
4991 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_NO_SPACE);
4992 795b88cb 2023-07-10 thomas goto done;
4994 795b88cb 2023-07-10 thomas bline->annotated = 1;
4996 795b88cb 2023-07-10 thomas /* Print lines annotated so far. */
4997 795b88cb 2023-07-10 thomas bline = &a->lines[a->lineno_cur - 1];
4998 795b88cb 2023-07-10 thomas if (!bline->annotated)
4999 795b88cb 2023-07-10 thomas goto done;
5001 795b88cb 2023-07-10 thomas offset = a->line_offsets[a->lineno_cur - 1];
5002 795b88cb 2023-07-10 thomas if (fseeko(a->f, offset, SEEK_SET) == -1) {
5003 795b88cb 2023-07-10 thomas err = got_error_from_errno("fseeko");
5004 795b88cb 2023-07-10 thomas goto done;
5007 795b88cb 2023-07-10 thomas while (a->lineno_cur <= a->nlines && bline->annotated) {
5008 795b88cb 2023-07-10 thomas char *smallerthan, *at, *nl, *committer;
5009 795b88cb 2023-07-10 thomas size_t len;
5011 795b88cb 2023-07-10 thomas if (getline(&line, &linesize, a->f) == -1) {
5012 795b88cb 2023-07-10 thomas if (ferror(a->f))
5013 795b88cb 2023-07-10 thomas err = got_error_from_errno("getline");
5017 795b88cb 2023-07-10 thomas committer = bline->committer;
5018 795b88cb 2023-07-10 thomas smallerthan = strchr(committer, '<');
5019 795b88cb 2023-07-10 thomas if (smallerthan && smallerthan[1] != '\0')
5020 795b88cb 2023-07-10 thomas committer = smallerthan + 1;
5021 795b88cb 2023-07-10 thomas at = strchr(committer, '@');
5023 795b88cb 2023-07-10 thomas *at = '\0';
5024 795b88cb 2023-07-10 thomas len = strlen(committer);
5025 795b88cb 2023-07-10 thomas if (len >= 9)
5026 795b88cb 2023-07-10 thomas committer[8] = '\0';
5028 795b88cb 2023-07-10 thomas nl = strchr(line, '\n');
5030 795b88cb 2023-07-10 thomas *nl = '\0';
5031 795b88cb 2023-07-10 thomas printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5032 795b88cb 2023-07-10 thomas bline->id_str, bline->datebuf, committer, line);
5034 795b88cb 2023-07-10 thomas a->lineno_cur++;
5035 795b88cb 2023-07-10 thomas bline = &a->lines[a->lineno_cur - 1];
5038 795b88cb 2023-07-10 thomas free(line);
5039 795b88cb 2023-07-10 thomas return err;
5042 795b88cb 2023-07-10 thomas static const struct got_error *
5043 795b88cb 2023-07-10 thomas cmd_blame(int argc, char *argv[])
5045 795b88cb 2023-07-10 thomas const struct got_error *error;
5046 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
5047 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
5048 795b88cb 2023-07-10 thomas char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5049 795b88cb 2023-07-10 thomas char *link_target = NULL;
5050 795b88cb 2023-07-10 thomas struct got_object_id *obj_id = NULL;
5051 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
5052 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
5053 795b88cb 2023-07-10 thomas struct got_blob_object *blob = NULL;
5054 795b88cb 2023-07-10 thomas char *commit_id_str = NULL;
5055 795b88cb 2023-07-10 thomas struct blame_cb_args bca;
5056 795b88cb 2023-07-10 thomas int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5057 795b88cb 2023-07-10 thomas off_t filesize;
5058 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
5059 795b88cb 2023-07-10 thomas FILE *f1 = NULL, *f2 = NULL;
5061 795b88cb 2023-07-10 thomas fd1 = got_opentempfd();
5062 795b88cb 2023-07-10 thomas if (fd1 == -1)
5063 795b88cb 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
5065 795b88cb 2023-07-10 thomas memset(&bca, 0, sizeof(bca));
5067 795b88cb 2023-07-10 thomas #ifndef PROFILE
5068 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5069 795b88cb 2023-07-10 thomas NULL) == -1)
5070 795b88cb 2023-07-10 thomas err(1, "pledge");
5073 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5074 795b88cb 2023-07-10 thomas switch (ch) {
5075 795b88cb 2023-07-10 thomas case 'c':
5076 795b88cb 2023-07-10 thomas commit_id_str = optarg;
5078 795b88cb 2023-07-10 thomas case 'r':
5079 795b88cb 2023-07-10 thomas repo_path = realpath(optarg, NULL);
5080 795b88cb 2023-07-10 thomas if (repo_path == NULL)
5081 795b88cb 2023-07-10 thomas return got_error_from_errno2("realpath",
5082 795b88cb 2023-07-10 thomas optarg);
5083 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
5085 795b88cb 2023-07-10 thomas default:
5086 795b88cb 2023-07-10 thomas usage_blame();
5087 795b88cb 2023-07-10 thomas /* NOTREACHED */
5091 795b88cb 2023-07-10 thomas argc -= optind;
5092 795b88cb 2023-07-10 thomas argv += optind;
5094 795b88cb 2023-07-10 thomas if (argc == 1)
5095 795b88cb 2023-07-10 thomas path = argv[0];
5097 795b88cb 2023-07-10 thomas usage_blame();
5099 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
5100 795b88cb 2023-07-10 thomas if (cwd == NULL) {
5101 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
5102 795b88cb 2023-07-10 thomas goto done;
5105 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
5106 795b88cb 2023-07-10 thomas if (error != NULL)
5107 795b88cb 2023-07-10 thomas goto done;
5109 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
5110 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
5111 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
5112 795b88cb 2023-07-10 thomas goto done;
5114 795b88cb 2023-07-10 thomas error = NULL;
5115 795b88cb 2023-07-10 thomas if (worktree) {
5116 795b88cb 2023-07-10 thomas repo_path =
5117 795b88cb 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree));
5118 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
5119 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
5120 795b88cb 2023-07-10 thomas if (error)
5121 795b88cb 2023-07-10 thomas goto done;
5123 795b88cb 2023-07-10 thomas } else {
5124 795b88cb 2023-07-10 thomas repo_path = strdup(cwd);
5125 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
5126 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
5127 795b88cb 2023-07-10 thomas goto done;
5132 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5133 795b88cb 2023-07-10 thomas if (error != NULL)
5134 795b88cb 2023-07-10 thomas goto done;
5136 795b88cb 2023-07-10 thomas if (worktree) {
5137 795b88cb 2023-07-10 thomas const char *prefix = got_worktree_get_path_prefix(worktree);
5138 795b88cb 2023-07-10 thomas char *p;
5140 795b88cb 2023-07-10 thomas error = got_worktree_resolve_path(&p, worktree, path);
5141 795b88cb 2023-07-10 thomas if (error)
5142 795b88cb 2023-07-10 thomas goto done;
5143 795b88cb 2023-07-10 thomas if (asprintf(&in_repo_path, "%s%s%s", prefix,
5144 795b88cb 2023-07-10 thomas (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5145 795b88cb 2023-07-10 thomas p) == -1) {
5146 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
5147 795b88cb 2023-07-10 thomas free(p);
5148 795b88cb 2023-07-10 thomas goto done;
5150 795b88cb 2023-07-10 thomas free(p);
5151 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5152 795b88cb 2023-07-10 thomas } else {
5153 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5154 795b88cb 2023-07-10 thomas if (error)
5155 795b88cb 2023-07-10 thomas goto done;
5156 795b88cb 2023-07-10 thomas error = got_repo_map_path(&in_repo_path, repo, path);
5158 795b88cb 2023-07-10 thomas if (error)
5159 795b88cb 2023-07-10 thomas goto done;
5161 795b88cb 2023-07-10 thomas if (commit_id_str == NULL) {
5162 795b88cb 2023-07-10 thomas struct got_reference *head_ref;
5163 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo, worktree ?
5164 795b88cb 2023-07-10 thomas got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5165 795b88cb 2023-07-10 thomas if (error != NULL)
5166 795b88cb 2023-07-10 thomas goto done;
5167 795b88cb 2023-07-10 thomas error = got_ref_resolve(&commit_id, repo, head_ref);
5168 795b88cb 2023-07-10 thomas got_ref_close(head_ref);
5169 795b88cb 2023-07-10 thomas if (error != NULL)
5170 795b88cb 2023-07-10 thomas goto done;
5171 795b88cb 2023-07-10 thomas } else {
5172 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
5173 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
5174 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5176 795b88cb 2023-07-10 thomas if (error)
5177 795b88cb 2023-07-10 thomas goto done;
5178 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
5179 795b88cb 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5180 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
5181 795b88cb 2023-07-10 thomas if (error)
5182 795b88cb 2023-07-10 thomas goto done;
5185 795b88cb 2023-07-10 thomas if (worktree) {
5186 795b88cb 2023-07-10 thomas /* Release work tree lock. */
5187 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
5188 795b88cb 2023-07-10 thomas worktree = NULL;
5191 795b88cb 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
5192 795b88cb 2023-07-10 thomas if (error)
5193 795b88cb 2023-07-10 thomas goto done;
5195 795b88cb 2023-07-10 thomas error = got_object_resolve_symlinks(&link_target, in_repo_path,
5196 795b88cb 2023-07-10 thomas commit, repo);
5197 795b88cb 2023-07-10 thomas if (error)
5198 795b88cb 2023-07-10 thomas goto done;
5200 795b88cb 2023-07-10 thomas error = got_object_id_by_path(&obj_id, repo, commit,
5201 795b88cb 2023-07-10 thomas link_target ? link_target : in_repo_path);
5202 795b88cb 2023-07-10 thomas if (error)
5203 795b88cb 2023-07-10 thomas goto done;
5205 795b88cb 2023-07-10 thomas error = got_object_get_type(&obj_type, repo, obj_id);
5206 795b88cb 2023-07-10 thomas if (error)
5207 795b88cb 2023-07-10 thomas goto done;
5209 795b88cb 2023-07-10 thomas if (obj_type != GOT_OBJ_TYPE_BLOB) {
5210 795b88cb 2023-07-10 thomas error = got_error_path(link_target ? link_target : in_repo_path,
5211 795b88cb 2023-07-10 thomas GOT_ERR_OBJ_TYPE);
5212 795b88cb 2023-07-10 thomas goto done;
5215 795b88cb 2023-07-10 thomas error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5216 795b88cb 2023-07-10 thomas if (error)
5217 795b88cb 2023-07-10 thomas goto done;
5218 795b88cb 2023-07-10 thomas bca.f = got_opentemp();
5219 795b88cb 2023-07-10 thomas if (bca.f == NULL) {
5220 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
5221 795b88cb 2023-07-10 thomas goto done;
5223 795b88cb 2023-07-10 thomas error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5224 795b88cb 2023-07-10 thomas &bca.line_offsets, bca.f, blob);
5225 795b88cb 2023-07-10 thomas if (error || bca.nlines == 0)
5226 795b88cb 2023-07-10 thomas goto done;
5228 795b88cb 2023-07-10 thomas /* Don't include \n at EOF in the blame line count. */
5229 795b88cb 2023-07-10 thomas if (bca.line_offsets[bca.nlines - 1] == filesize)
5230 795b88cb 2023-07-10 thomas bca.nlines--;
5232 795b88cb 2023-07-10 thomas bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5233 795b88cb 2023-07-10 thomas if (bca.lines == NULL) {
5234 795b88cb 2023-07-10 thomas error = got_error_from_errno("calloc");
5235 795b88cb 2023-07-10 thomas goto done;
5237 795b88cb 2023-07-10 thomas bca.lineno_cur = 1;
5238 795b88cb 2023-07-10 thomas bca.nlines_prec = 0;
5239 795b88cb 2023-07-10 thomas i = bca.nlines;
5240 795b88cb 2023-07-10 thomas while (i > 0) {
5241 795b88cb 2023-07-10 thomas i /= 10;
5242 795b88cb 2023-07-10 thomas bca.nlines_prec++;
5244 795b88cb 2023-07-10 thomas bca.repo = repo;
5246 795b88cb 2023-07-10 thomas fd2 = got_opentempfd();
5247 795b88cb 2023-07-10 thomas if (fd2 == -1) {
5248 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentempfd");
5249 795b88cb 2023-07-10 thomas goto done;
5251 795b88cb 2023-07-10 thomas fd3 = got_opentempfd();
5252 795b88cb 2023-07-10 thomas if (fd3 == -1) {
5253 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentempfd");
5254 795b88cb 2023-07-10 thomas goto done;
5256 795b88cb 2023-07-10 thomas f1 = got_opentemp();
5257 795b88cb 2023-07-10 thomas if (f1 == NULL) {
5258 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
5259 795b88cb 2023-07-10 thomas goto done;
5261 795b88cb 2023-07-10 thomas f2 = got_opentemp();
5262 795b88cb 2023-07-10 thomas if (f2 == NULL) {
5263 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
5264 795b88cb 2023-07-10 thomas goto done;
5266 795b88cb 2023-07-10 thomas error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5267 795b88cb 2023-07-10 thomas repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5268 795b88cb 2023-07-10 thomas check_cancelled, NULL, fd2, fd3, f1, f2);
5270 795b88cb 2023-07-10 thomas free(in_repo_path);
5271 795b88cb 2023-07-10 thomas free(link_target);
5272 795b88cb 2023-07-10 thomas free(repo_path);
5273 795b88cb 2023-07-10 thomas free(cwd);
5274 795b88cb 2023-07-10 thomas free(commit_id);
5275 795b88cb 2023-07-10 thomas free(obj_id);
5276 795b88cb 2023-07-10 thomas if (commit)
5277 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
5279 795b88cb 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5280 795b88cb 2023-07-10 thomas error = got_error_from_errno("close");
5281 795b88cb 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5282 795b88cb 2023-07-10 thomas error = got_error_from_errno("close");
5283 795b88cb 2023-07-10 thomas if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5284 795b88cb 2023-07-10 thomas error = got_error_from_errno("close");
5285 795b88cb 2023-07-10 thomas if (f1 && fclose(f1) == EOF && error == NULL)
5286 795b88cb 2023-07-10 thomas error = got_error_from_errno("fclose");
5287 795b88cb 2023-07-10 thomas if (f2 && fclose(f2) == EOF && error == NULL)
5288 795b88cb 2023-07-10 thomas error = got_error_from_errno("fclose");
5290 795b88cb 2023-07-10 thomas if (blob)
5291 795b88cb 2023-07-10 thomas got_object_blob_close(blob);
5292 795b88cb 2023-07-10 thomas if (worktree)
5293 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
5294 795b88cb 2023-07-10 thomas if (repo) {
5295 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
5296 795b88cb 2023-07-10 thomas if (error == NULL)
5297 795b88cb 2023-07-10 thomas error = close_err;
5299 795b88cb 2023-07-10 thomas if (pack_fds) {
5300 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
5301 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
5302 795b88cb 2023-07-10 thomas if (error == NULL)
5303 795b88cb 2023-07-10 thomas error = pack_err;
5305 795b88cb 2023-07-10 thomas if (bca.lines) {
5306 795b88cb 2023-07-10 thomas for (i = 0; i < bca.nlines; i++) {
5307 795b88cb 2023-07-10 thomas struct blame_line *bline = &bca.lines[i];
5308 795b88cb 2023-07-10 thomas free(bline->id_str);
5309 795b88cb 2023-07-10 thomas free(bline->committer);
5311 795b88cb 2023-07-10 thomas free(bca.lines);
5313 795b88cb 2023-07-10 thomas free(bca.line_offsets);
5314 795b88cb 2023-07-10 thomas if (bca.f && fclose(bca.f) == EOF && error == NULL)
5315 795b88cb 2023-07-10 thomas error = got_error_from_errno("fclose");
5316 795b88cb 2023-07-10 thomas return error;
5319 795b88cb 2023-07-10 thomas __dead static void
5320 795b88cb 2023-07-10 thomas usage_tree(void)
5322 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
5323 795b88cb 2023-07-10 thomas "[path]\n", getprogname());
5324 795b88cb 2023-07-10 thomas exit(1);
5327 795b88cb 2023-07-10 thomas static const struct got_error *
5328 795b88cb 2023-07-10 thomas print_entry(struct got_tree_entry *te, const char *id, const char *path,
5329 795b88cb 2023-07-10 thomas const char *root_path, struct got_repository *repo)
5331 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
5332 795b88cb 2023-07-10 thomas int is_root_path = (strcmp(path, root_path) == 0);
5333 795b88cb 2023-07-10 thomas const char *modestr = "";
5334 795b88cb 2023-07-10 thomas mode_t mode = got_tree_entry_get_mode(te);
5335 795b88cb 2023-07-10 thomas char *link_target = NULL;
5337 795b88cb 2023-07-10 thomas path += strlen(root_path);
5338 795b88cb 2023-07-10 thomas while (path[0] == '/')
5341 795b88cb 2023-07-10 thomas if (got_object_tree_entry_is_submodule(te))
5342 795b88cb 2023-07-10 thomas modestr = "$";
5343 795b88cb 2023-07-10 thomas else if (S_ISLNK(mode)) {
5346 795b88cb 2023-07-10 thomas err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5347 795b88cb 2023-07-10 thomas if (err)
5348 795b88cb 2023-07-10 thomas return err;
5349 795b88cb 2023-07-10 thomas for (i = 0; link_target[i] != '\0'; i++) {
5350 795b88cb 2023-07-10 thomas if (!isprint((unsigned char)link_target[i]))
5351 795b88cb 2023-07-10 thomas link_target[i] = '?';
5354 795b88cb 2023-07-10 thomas modestr = "@";
5356 795b88cb 2023-07-10 thomas else if (S_ISDIR(mode))
5357 795b88cb 2023-07-10 thomas modestr = "/";
5358 795b88cb 2023-07-10 thomas else if (mode & S_IXUSR)
5359 795b88cb 2023-07-10 thomas modestr = "*";
5361 795b88cb 2023-07-10 thomas printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5362 795b88cb 2023-07-10 thomas is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5363 795b88cb 2023-07-10 thomas link_target ? " -> ": "", link_target ? link_target : "");
5365 795b88cb 2023-07-10 thomas free(link_target);
5366 795b88cb 2023-07-10 thomas return NULL;
5369 795b88cb 2023-07-10 thomas static const struct got_error *
5370 795b88cb 2023-07-10 thomas print_tree(const char *path, struct got_commit_object *commit,
5371 795b88cb 2023-07-10 thomas int show_ids, int recurse, const char *root_path,
5372 795b88cb 2023-07-10 thomas struct got_repository *repo)
5374 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
5375 795b88cb 2023-07-10 thomas struct got_object_id *tree_id = NULL;
5376 795b88cb 2023-07-10 thomas struct got_tree_object *tree = NULL;
5377 795b88cb 2023-07-10 thomas int nentries, i;
5379 795b88cb 2023-07-10 thomas err = got_object_id_by_path(&tree_id, repo, commit, path);
5380 795b88cb 2023-07-10 thomas if (err)
5381 795b88cb 2023-07-10 thomas goto done;
5383 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&tree, repo, tree_id);
5384 795b88cb 2023-07-10 thomas if (err)
5385 795b88cb 2023-07-10 thomas goto done;
5386 795b88cb 2023-07-10 thomas nentries = got_object_tree_get_nentries(tree);
5387 795b88cb 2023-07-10 thomas for (i = 0; i < nentries; i++) {
5388 795b88cb 2023-07-10 thomas struct got_tree_entry *te;
5389 795b88cb 2023-07-10 thomas char *id = NULL;
5391 795b88cb 2023-07-10 thomas if (sigint_received || sigpipe_received)
5394 795b88cb 2023-07-10 thomas te = got_object_tree_get_entry(tree, i);
5395 795b88cb 2023-07-10 thomas if (show_ids) {
5396 795b88cb 2023-07-10 thomas char *id_str;
5397 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str,
5398 795b88cb 2023-07-10 thomas got_tree_entry_get_id(te));
5399 795b88cb 2023-07-10 thomas if (err)
5400 795b88cb 2023-07-10 thomas goto done;
5401 795b88cb 2023-07-10 thomas if (asprintf(&id, "%s ", id_str) == -1) {
5402 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
5403 795b88cb 2023-07-10 thomas free(id_str);
5404 795b88cb 2023-07-10 thomas goto done;
5406 795b88cb 2023-07-10 thomas free(id_str);
5408 795b88cb 2023-07-10 thomas err = print_entry(te, id, path, root_path, repo);
5409 795b88cb 2023-07-10 thomas free(id);
5410 795b88cb 2023-07-10 thomas if (err)
5411 795b88cb 2023-07-10 thomas goto done;
5413 795b88cb 2023-07-10 thomas if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5414 795b88cb 2023-07-10 thomas char *child_path;
5415 795b88cb 2023-07-10 thomas if (asprintf(&child_path, "%s%s%s", path,
5416 795b88cb 2023-07-10 thomas path[0] == '/' && path[1] == '\0' ? "" : "/",
5417 795b88cb 2023-07-10 thomas got_tree_entry_get_name(te)) == -1) {
5418 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
5419 795b88cb 2023-07-10 thomas goto done;
5421 795b88cb 2023-07-10 thomas err = print_tree(child_path, commit, show_ids, 1,
5422 795b88cb 2023-07-10 thomas root_path, repo);
5423 795b88cb 2023-07-10 thomas free(child_path);
5424 795b88cb 2023-07-10 thomas if (err)
5425 795b88cb 2023-07-10 thomas goto done;
5429 795b88cb 2023-07-10 thomas if (tree)
5430 795b88cb 2023-07-10 thomas got_object_tree_close(tree);
5431 795b88cb 2023-07-10 thomas free(tree_id);
5432 795b88cb 2023-07-10 thomas return err;
5435 795b88cb 2023-07-10 thomas static const struct got_error *
5436 795b88cb 2023-07-10 thomas cmd_tree(int argc, char *argv[])
5438 795b88cb 2023-07-10 thomas const struct got_error *error;
5439 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
5440 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
5441 795b88cb 2023-07-10 thomas const char *path, *refname = NULL;
5442 795b88cb 2023-07-10 thomas char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5443 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
5444 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
5445 795b88cb 2023-07-10 thomas char *commit_id_str = NULL;
5446 795b88cb 2023-07-10 thomas int show_ids = 0, recurse = 0;
5448 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
5450 795b88cb 2023-07-10 thomas #ifndef PROFILE
5451 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5452 795b88cb 2023-07-10 thomas NULL) == -1)
5453 795b88cb 2023-07-10 thomas err(1, "pledge");
5456 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
5457 795b88cb 2023-07-10 thomas switch (ch) {
5458 795b88cb 2023-07-10 thomas case 'c':
5459 795b88cb 2023-07-10 thomas commit_id_str = optarg;
5461 795b88cb 2023-07-10 thomas case 'i':
5462 795b88cb 2023-07-10 thomas show_ids = 1;
5464 795b88cb 2023-07-10 thomas case 'R':
5465 795b88cb 2023-07-10 thomas recurse = 1;
5467 795b88cb 2023-07-10 thomas case 'r':
5468 795b88cb 2023-07-10 thomas repo_path = realpath(optarg, NULL);
5469 795b88cb 2023-07-10 thomas if (repo_path == NULL)
5470 795b88cb 2023-07-10 thomas return got_error_from_errno2("realpath",
5471 795b88cb 2023-07-10 thomas optarg);
5472 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
5474 795b88cb 2023-07-10 thomas default:
5475 795b88cb 2023-07-10 thomas usage_tree();
5476 795b88cb 2023-07-10 thomas /* NOTREACHED */
5480 795b88cb 2023-07-10 thomas argc -= optind;
5481 795b88cb 2023-07-10 thomas argv += optind;
5483 795b88cb 2023-07-10 thomas if (argc == 1)
5484 795b88cb 2023-07-10 thomas path = argv[0];
5485 795b88cb 2023-07-10 thomas else if (argc > 1)
5486 795b88cb 2023-07-10 thomas usage_tree();
5488 795b88cb 2023-07-10 thomas path = NULL;
5490 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
5491 795b88cb 2023-07-10 thomas if (cwd == NULL) {
5492 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
5493 795b88cb 2023-07-10 thomas goto done;
5496 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
5497 795b88cb 2023-07-10 thomas if (error != NULL)
5498 795b88cb 2023-07-10 thomas goto done;
5500 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
5501 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
5502 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
5503 795b88cb 2023-07-10 thomas goto done;
5505 795b88cb 2023-07-10 thomas error = NULL;
5506 795b88cb 2023-07-10 thomas if (worktree) {
5507 795b88cb 2023-07-10 thomas repo_path =
5508 795b88cb 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree));
5509 795b88cb 2023-07-10 thomas if (repo_path == NULL)
5510 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
5511 795b88cb 2023-07-10 thomas if (error)
5512 795b88cb 2023-07-10 thomas goto done;
5513 795b88cb 2023-07-10 thomas } else {
5514 795b88cb 2023-07-10 thomas repo_path = strdup(cwd);
5515 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
5516 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
5517 795b88cb 2023-07-10 thomas goto done;
5522 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5523 795b88cb 2023-07-10 thomas if (error != NULL)
5524 795b88cb 2023-07-10 thomas goto done;
5526 795b88cb 2023-07-10 thomas if (worktree) {
5527 795b88cb 2023-07-10 thomas const char *prefix = got_worktree_get_path_prefix(worktree);
5528 795b88cb 2023-07-10 thomas char *p;
5530 795b88cb 2023-07-10 thomas if (path == NULL || got_path_is_root_dir(path))
5531 795b88cb 2023-07-10 thomas path = "";
5532 795b88cb 2023-07-10 thomas error = got_worktree_resolve_path(&p, worktree, path);
5533 795b88cb 2023-07-10 thomas if (error)
5534 795b88cb 2023-07-10 thomas goto done;
5535 795b88cb 2023-07-10 thomas if (asprintf(&in_repo_path, "%s%s%s", prefix,
5536 795b88cb 2023-07-10 thomas (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5537 795b88cb 2023-07-10 thomas p) == -1) {
5538 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
5539 795b88cb 2023-07-10 thomas free(p);
5540 795b88cb 2023-07-10 thomas goto done;
5542 795b88cb 2023-07-10 thomas free(p);
5543 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5544 795b88cb 2023-07-10 thomas if (error)
5545 795b88cb 2023-07-10 thomas goto done;
5546 795b88cb 2023-07-10 thomas } else {
5547 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5548 795b88cb 2023-07-10 thomas if (error)
5549 795b88cb 2023-07-10 thomas goto done;
5550 795b88cb 2023-07-10 thomas if (path == NULL)
5551 795b88cb 2023-07-10 thomas path = "/";
5552 795b88cb 2023-07-10 thomas error = got_repo_map_path(&in_repo_path, repo, path);
5553 795b88cb 2023-07-10 thomas if (error != NULL)
5554 795b88cb 2023-07-10 thomas goto done;
5557 795b88cb 2023-07-10 thomas if (commit_id_str == NULL) {
5558 795b88cb 2023-07-10 thomas struct got_reference *head_ref;
5559 795b88cb 2023-07-10 thomas if (worktree)
5560 795b88cb 2023-07-10 thomas refname = got_worktree_get_head_ref_name(worktree);
5562 795b88cb 2023-07-10 thomas refname = GOT_REF_HEAD;
5563 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo, refname, 0);
5564 795b88cb 2023-07-10 thomas if (error != NULL)
5565 795b88cb 2023-07-10 thomas goto done;
5566 795b88cb 2023-07-10 thomas error = got_ref_resolve(&commit_id, repo, head_ref);
5567 795b88cb 2023-07-10 thomas got_ref_close(head_ref);
5568 795b88cb 2023-07-10 thomas if (error != NULL)
5569 795b88cb 2023-07-10 thomas goto done;
5570 795b88cb 2023-07-10 thomas } else {
5571 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
5572 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
5573 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5575 795b88cb 2023-07-10 thomas if (error)
5576 795b88cb 2023-07-10 thomas goto done;
5577 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
5578 795b88cb 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5579 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
5580 795b88cb 2023-07-10 thomas if (error)
5581 795b88cb 2023-07-10 thomas goto done;
5584 795b88cb 2023-07-10 thomas if (worktree) {
5585 795b88cb 2023-07-10 thomas /* Release work tree lock. */
5586 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
5587 795b88cb 2023-07-10 thomas worktree = NULL;
5590 795b88cb 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
5591 795b88cb 2023-07-10 thomas if (error)
5592 795b88cb 2023-07-10 thomas goto done;
5594 795b88cb 2023-07-10 thomas error = print_tree(in_repo_path, commit, show_ids, recurse,
5595 795b88cb 2023-07-10 thomas in_repo_path, repo);
5597 795b88cb 2023-07-10 thomas free(in_repo_path);
5598 795b88cb 2023-07-10 thomas free(repo_path);
5599 795b88cb 2023-07-10 thomas free(cwd);
5600 795b88cb 2023-07-10 thomas free(commit_id);
5601 795b88cb 2023-07-10 thomas if (commit)
5602 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
5603 795b88cb 2023-07-10 thomas if (worktree)
5604 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
5605 795b88cb 2023-07-10 thomas if (repo) {
5606 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
5607 795b88cb 2023-07-10 thomas if (error == NULL)
5608 795b88cb 2023-07-10 thomas error = close_err;
5610 795b88cb 2023-07-10 thomas if (pack_fds) {
5611 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
5612 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
5613 795b88cb 2023-07-10 thomas if (error == NULL)
5614 795b88cb 2023-07-10 thomas error = pack_err;
5616 795b88cb 2023-07-10 thomas return error;
5619 795b88cb 2023-07-10 thomas __dead static void
5620 795b88cb 2023-07-10 thomas usage_status(void)
5622 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
5623 795b88cb 2023-07-10 thomas "[-s status-codes] [path ...]\n", getprogname());
5624 795b88cb 2023-07-10 thomas exit(1);
5627 795b88cb 2023-07-10 thomas struct got_status_arg {
5628 795b88cb 2023-07-10 thomas char *status_codes;
5629 795b88cb 2023-07-10 thomas int suppress;
5632 795b88cb 2023-07-10 thomas static const struct got_error *
5633 795b88cb 2023-07-10 thomas print_status(void *arg, unsigned char status, unsigned char staged_status,
5634 795b88cb 2023-07-10 thomas const char *path, struct got_object_id *blob_id,
5635 795b88cb 2023-07-10 thomas struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5636 795b88cb 2023-07-10 thomas int dirfd, const char *de_name)
5638 795b88cb 2023-07-10 thomas struct got_status_arg *st = arg;
5640 795b88cb 2023-07-10 thomas if (status == staged_status && (status == GOT_STATUS_DELETE))
5641 795b88cb 2023-07-10 thomas status = GOT_STATUS_NO_CHANGE;
5642 795b88cb 2023-07-10 thomas if (st != NULL && st->status_codes) {
5643 795b88cb 2023-07-10 thomas size_t ncodes = strlen(st->status_codes);
5644 795b88cb 2023-07-10 thomas int i, j = 0;
5646 795b88cb 2023-07-10 thomas for (i = 0; i < ncodes ; i++) {
5647 795b88cb 2023-07-10 thomas if (st->suppress) {
5648 795b88cb 2023-07-10 thomas if (status == st->status_codes[i] ||
5649 795b88cb 2023-07-10 thomas staged_status == st->status_codes[i]) {
5651 795b88cb 2023-07-10 thomas continue;
5653 795b88cb 2023-07-10 thomas } else {
5654 795b88cb 2023-07-10 thomas if (status == st->status_codes[i] ||
5655 795b88cb 2023-07-10 thomas staged_status == st->status_codes[i])
5660 795b88cb 2023-07-10 thomas if (st->suppress && j == 0)
5661 795b88cb 2023-07-10 thomas goto print;
5663 795b88cb 2023-07-10 thomas if (i == ncodes)
5664 795b88cb 2023-07-10 thomas return NULL;
5667 795b88cb 2023-07-10 thomas printf("%c%c %s\n", status, staged_status, path);
5668 795b88cb 2023-07-10 thomas return NULL;
5671 795b88cb 2023-07-10 thomas static const struct got_error *
5672 795b88cb 2023-07-10 thomas cmd_status(int argc, char *argv[])
5674 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
5675 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
5676 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
5677 795b88cb 2023-07-10 thomas struct got_status_arg st;
5678 795b88cb 2023-07-10 thomas char *cwd = NULL;
5679 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
5680 795b88cb 2023-07-10 thomas int ch, i, no_ignores = 0;
5681 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
5683 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
5685 795b88cb 2023-07-10 thomas memset(&st, 0, sizeof(st));
5686 795b88cb 2023-07-10 thomas st.status_codes = NULL;
5687 795b88cb 2023-07-10 thomas st.suppress = 0;
5689 795b88cb 2023-07-10 thomas #ifndef PROFILE
5690 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5691 795b88cb 2023-07-10 thomas NULL) == -1)
5692 795b88cb 2023-07-10 thomas err(1, "pledge");
5695 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
5696 795b88cb 2023-07-10 thomas switch (ch) {
5697 795b88cb 2023-07-10 thomas case 'I':
5698 795b88cb 2023-07-10 thomas no_ignores = 1;
5700 795b88cb 2023-07-10 thomas case 'S':
5701 795b88cb 2023-07-10 thomas if (st.status_codes != NULL && st.suppress == 0)
5702 795b88cb 2023-07-10 thomas option_conflict('S', 's');
5703 795b88cb 2023-07-10 thomas st.suppress = 1;
5704 795b88cb 2023-07-10 thomas /* fallthrough */
5705 795b88cb 2023-07-10 thomas case 's':
5706 795b88cb 2023-07-10 thomas for (i = 0; optarg[i] != '\0'; i++) {
5707 795b88cb 2023-07-10 thomas switch (optarg[i]) {
5708 795b88cb 2023-07-10 thomas case GOT_STATUS_MODIFY:
5709 795b88cb 2023-07-10 thomas case GOT_STATUS_ADD:
5710 795b88cb 2023-07-10 thomas case GOT_STATUS_DELETE:
5711 795b88cb 2023-07-10 thomas case GOT_STATUS_CONFLICT:
5712 795b88cb 2023-07-10 thomas case GOT_STATUS_MISSING:
5713 795b88cb 2023-07-10 thomas case GOT_STATUS_OBSTRUCTED:
5714 795b88cb 2023-07-10 thomas case GOT_STATUS_UNVERSIONED:
5715 795b88cb 2023-07-10 thomas case GOT_STATUS_MODE_CHANGE:
5716 795b88cb 2023-07-10 thomas case GOT_STATUS_NONEXISTENT:
5718 795b88cb 2023-07-10 thomas default:
5719 795b88cb 2023-07-10 thomas errx(1, "invalid status code '%c'",
5720 795b88cb 2023-07-10 thomas optarg[i]);
5723 795b88cb 2023-07-10 thomas if (ch == 's' && st.suppress)
5724 795b88cb 2023-07-10 thomas option_conflict('s', 'S');
5725 795b88cb 2023-07-10 thomas st.status_codes = optarg;
5727 795b88cb 2023-07-10 thomas default:
5728 795b88cb 2023-07-10 thomas usage_status();
5729 795b88cb 2023-07-10 thomas /* NOTREACHED */
5733 795b88cb 2023-07-10 thomas argc -= optind;
5734 795b88cb 2023-07-10 thomas argv += optind;
5736 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
5737 795b88cb 2023-07-10 thomas if (cwd == NULL) {
5738 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
5739 795b88cb 2023-07-10 thomas goto done;
5742 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
5743 795b88cb 2023-07-10 thomas if (error != NULL)
5744 795b88cb 2023-07-10 thomas goto done;
5746 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
5747 795b88cb 2023-07-10 thomas if (error) {
5748 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
5749 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error, "status", cwd);
5750 795b88cb 2023-07-10 thomas goto done;
5753 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5754 795b88cb 2023-07-10 thomas NULL, pack_fds);
5755 795b88cb 2023-07-10 thomas if (error != NULL)
5756 795b88cb 2023-07-10 thomas goto done;
5758 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1,
5759 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree));
5760 795b88cb 2023-07-10 thomas if (error)
5761 795b88cb 2023-07-10 thomas goto done;
5763 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5764 795b88cb 2023-07-10 thomas if (error)
5765 795b88cb 2023-07-10 thomas goto done;
5767 795b88cb 2023-07-10 thomas error = got_worktree_status(worktree, &paths, repo, no_ignores,
5768 795b88cb 2023-07-10 thomas print_status, &st, check_cancelled, NULL);
5770 795b88cb 2023-07-10 thomas if (pack_fds) {
5771 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
5772 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
5773 795b88cb 2023-07-10 thomas if (error == NULL)
5774 795b88cb 2023-07-10 thomas error = pack_err;
5776 795b88cb 2023-07-10 thomas if (repo) {
5777 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
5778 795b88cb 2023-07-10 thomas if (error == NULL)
5779 795b88cb 2023-07-10 thomas error = close_err;
5782 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5783 795b88cb 2023-07-10 thomas free(cwd);
5784 795b88cb 2023-07-10 thomas return error;
5787 795b88cb 2023-07-10 thomas __dead static void
5788 795b88cb 2023-07-10 thomas usage_tag(void)
5790 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
5791 795b88cb 2023-07-10 thomas "[-r repository-path] [-s signer-id] name\n", getprogname());
5792 795b88cb 2023-07-10 thomas exit(1);
5796 795b88cb 2023-07-10 thomas static const struct got_error *
5797 795b88cb 2023-07-10 thomas sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5799 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
5800 795b88cb 2023-07-10 thomas struct got_reflist_entry *re, *se, *new;
5801 795b88cb 2023-07-10 thomas struct got_object_id *re_id, *se_id;
5802 795b88cb 2023-07-10 thomas struct got_tag_object *re_tag, *se_tag;
5803 795b88cb 2023-07-10 thomas time_t re_time, se_time;
5805 795b88cb 2023-07-10 thomas STAILQ_FOREACH(re, tags, entry) {
5806 795b88cb 2023-07-10 thomas se = STAILQ_FIRST(sorted);
5807 795b88cb 2023-07-10 thomas if (se == NULL) {
5808 795b88cb 2023-07-10 thomas err = got_reflist_entry_dup(&new, re);
5809 795b88cb 2023-07-10 thomas if (err)
5810 795b88cb 2023-07-10 thomas return err;
5811 795b88cb 2023-07-10 thomas STAILQ_INSERT_HEAD(sorted, new, entry);
5812 795b88cb 2023-07-10 thomas continue;
5813 795b88cb 2023-07-10 thomas } else {
5814 795b88cb 2023-07-10 thomas err = got_ref_resolve(&re_id, repo, re->ref);
5815 795b88cb 2023-07-10 thomas if (err)
5817 795b88cb 2023-07-10 thomas err = got_object_open_as_tag(&re_tag, repo, re_id);
5818 795b88cb 2023-07-10 thomas free(re_id);
5819 795b88cb 2023-07-10 thomas if (err)
5821 795b88cb 2023-07-10 thomas re_time = got_object_tag_get_tagger_time(re_tag);
5822 795b88cb 2023-07-10 thomas got_object_tag_close(re_tag);
5825 795b88cb 2023-07-10 thomas while (se) {
5826 795b88cb 2023-07-10 thomas err = got_ref_resolve(&se_id, repo, re->ref);
5827 795b88cb 2023-07-10 thomas if (err)
5829 795b88cb 2023-07-10 thomas err = got_object_open_as_tag(&se_tag, repo, se_id);
5830 795b88cb 2023-07-10 thomas free(se_id);
5831 795b88cb 2023-07-10 thomas if (err)
5833 795b88cb 2023-07-10 thomas se_time = got_object_tag_get_tagger_time(se_tag);
5834 795b88cb 2023-07-10 thomas got_object_tag_close(se_tag);
5836 795b88cb 2023-07-10 thomas if (se_time > re_time) {
5837 795b88cb 2023-07-10 thomas err = got_reflist_entry_dup(&new, re);
5838 795b88cb 2023-07-10 thomas if (err)
5839 795b88cb 2023-07-10 thomas return err;
5840 795b88cb 2023-07-10 thomas STAILQ_INSERT_AFTER(sorted, se, new, entry);
5843 795b88cb 2023-07-10 thomas se = STAILQ_NEXT(se, entry);
5844 795b88cb 2023-07-10 thomas continue;
5848 795b88cb 2023-07-10 thomas return err;
5852 795b88cb 2023-07-10 thomas static const struct got_error *
5853 795b88cb 2023-07-10 thomas get_tag_refname(char **refname, const char *tag_name)
5855 795b88cb 2023-07-10 thomas const struct got_error *err;
5857 795b88cb 2023-07-10 thomas if (strncmp("refs/tags/", tag_name, 10) == 0) {
5858 795b88cb 2023-07-10 thomas *refname = strdup(tag_name);
5859 795b88cb 2023-07-10 thomas if (*refname == NULL)
5860 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
5861 795b88cb 2023-07-10 thomas } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
5862 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
5863 795b88cb 2023-07-10 thomas *refname = NULL;
5864 795b88cb 2023-07-10 thomas return err;
5867 795b88cb 2023-07-10 thomas return NULL;
5870 795b88cb 2023-07-10 thomas static const struct got_error *
5871 795b88cb 2023-07-10 thomas list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
5872 795b88cb 2023-07-10 thomas const char *allowed_signers, const char *revoked_signers, int verbosity)
5874 795b88cb 2023-07-10 thomas static const struct got_error *err = NULL;
5875 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
5876 795b88cb 2023-07-10 thomas struct got_reflist_entry *re;
5877 795b88cb 2023-07-10 thomas char *wanted_refname = NULL;
5878 795b88cb 2023-07-10 thomas int bad_sigs = 0;
5880 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
5882 795b88cb 2023-07-10 thomas err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5883 795b88cb 2023-07-10 thomas if (err)
5884 795b88cb 2023-07-10 thomas return err;
5886 795b88cb 2023-07-10 thomas if (tag_name) {
5887 795b88cb 2023-07-10 thomas struct got_reference *ref;
5888 795b88cb 2023-07-10 thomas err = get_tag_refname(&wanted_refname, tag_name);
5889 795b88cb 2023-07-10 thomas if (err)
5890 795b88cb 2023-07-10 thomas goto done;
5891 795b88cb 2023-07-10 thomas /* Wanted tag reference should exist. */
5892 795b88cb 2023-07-10 thomas err = got_ref_open(&ref, repo, wanted_refname, 0);
5893 795b88cb 2023-07-10 thomas if (err)
5894 795b88cb 2023-07-10 thomas goto done;
5895 795b88cb 2023-07-10 thomas got_ref_close(ref);
5898 795b88cb 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
5899 795b88cb 2023-07-10 thomas const char *refname;
5900 795b88cb 2023-07-10 thomas char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5901 795b88cb 2023-07-10 thomas char datebuf[26];
5902 795b88cb 2023-07-10 thomas const char *tagger, *ssh_sig = NULL;
5903 795b88cb 2023-07-10 thomas char *sig_msg = NULL;
5904 795b88cb 2023-07-10 thomas time_t tagger_time;
5905 795b88cb 2023-07-10 thomas struct got_object_id *id;
5906 795b88cb 2023-07-10 thomas struct got_tag_object *tag;
5907 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
5909 795b88cb 2023-07-10 thomas refname = got_ref_get_name(re->ref);
5910 795b88cb 2023-07-10 thomas if (strncmp(refname, "refs/tags/", 10) != 0 ||
5911 795b88cb 2023-07-10 thomas (wanted_refname && strcmp(refname, wanted_refname) != 0))
5912 795b88cb 2023-07-10 thomas continue;
5913 795b88cb 2023-07-10 thomas refname += 10;
5914 795b88cb 2023-07-10 thomas refstr = got_ref_to_str(re->ref);
5915 795b88cb 2023-07-10 thomas if (refstr == NULL) {
5916 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_ref_to_str");
5920 795b88cb 2023-07-10 thomas err = got_ref_resolve(&id, repo, re->ref);
5921 795b88cb 2023-07-10 thomas if (err)
5923 795b88cb 2023-07-10 thomas err = got_object_open_as_tag(&tag, repo, id);
5924 795b88cb 2023-07-10 thomas if (err) {
5925 795b88cb 2023-07-10 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
5926 795b88cb 2023-07-10 thomas free(id);
5929 795b88cb 2023-07-10 thomas /* "lightweight" tag */
5930 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, id);
5931 795b88cb 2023-07-10 thomas if (err) {
5932 795b88cb 2023-07-10 thomas free(id);
5935 795b88cb 2023-07-10 thomas tagger = got_object_commit_get_committer(commit);
5936 795b88cb 2023-07-10 thomas tagger_time =
5937 795b88cb 2023-07-10 thomas got_object_commit_get_committer_time(commit);
5938 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, id);
5939 795b88cb 2023-07-10 thomas free(id);
5940 795b88cb 2023-07-10 thomas if (err)
5942 795b88cb 2023-07-10 thomas } else {
5943 795b88cb 2023-07-10 thomas free(id);
5944 795b88cb 2023-07-10 thomas tagger = got_object_tag_get_tagger(tag);
5945 795b88cb 2023-07-10 thomas tagger_time = got_object_tag_get_tagger_time(tag);
5946 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str,
5947 795b88cb 2023-07-10 thomas got_object_tag_get_object_id(tag));
5948 795b88cb 2023-07-10 thomas if (err)
5952 795b88cb 2023-07-10 thomas if (tag && verify_tags) {
5953 795b88cb 2023-07-10 thomas ssh_sig = got_sigs_get_tagmsg_ssh_signature(
5954 795b88cb 2023-07-10 thomas got_object_tag_get_message(tag));
5955 795b88cb 2023-07-10 thomas if (ssh_sig && allowed_signers == NULL) {
5956 795b88cb 2023-07-10 thomas err = got_error_msg(
5957 795b88cb 2023-07-10 thomas GOT_ERR_VERIFY_TAG_SIGNATURE,
5958 795b88cb 2023-07-10 thomas "SSH signature verification requires "
5959 795b88cb 2023-07-10 thomas "setting allowed_signers in "
5960 795b88cb 2023-07-10 thomas "got.conf(5)");
5965 795b88cb 2023-07-10 thomas printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5966 795b88cb 2023-07-10 thomas free(refstr);
5967 795b88cb 2023-07-10 thomas printf("from: %s\n", tagger);
5968 795b88cb 2023-07-10 thomas datestr = get_datestr(&tagger_time, datebuf);
5969 795b88cb 2023-07-10 thomas if (datestr)
5970 795b88cb 2023-07-10 thomas printf("date: %s UTC\n", datestr);
5971 795b88cb 2023-07-10 thomas if (commit)
5972 795b88cb 2023-07-10 thomas printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5974 795b88cb 2023-07-10 thomas switch (got_object_tag_get_object_type(tag)) {
5975 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_BLOB:
5976 795b88cb 2023-07-10 thomas printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5977 795b88cb 2023-07-10 thomas id_str);
5979 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TREE:
5980 795b88cb 2023-07-10 thomas printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5981 795b88cb 2023-07-10 thomas id_str);
5983 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_COMMIT:
5984 795b88cb 2023-07-10 thomas printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5985 795b88cb 2023-07-10 thomas id_str);
5987 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TAG:
5988 795b88cb 2023-07-10 thomas printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5989 795b88cb 2023-07-10 thomas id_str);
5991 795b88cb 2023-07-10 thomas default:
5995 795b88cb 2023-07-10 thomas free(id_str);
5997 795b88cb 2023-07-10 thomas if (ssh_sig) {
5998 795b88cb 2023-07-10 thomas err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
5999 795b88cb 2023-07-10 thomas allowed_signers, revoked_signers, verbosity);
6000 795b88cb 2023-07-10 thomas if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
6001 795b88cb 2023-07-10 thomas bad_sigs = 1;
6002 795b88cb 2023-07-10 thomas else if (err)
6004 795b88cb 2023-07-10 thomas printf("signature: %s", sig_msg);
6005 795b88cb 2023-07-10 thomas free(sig_msg);
6006 795b88cb 2023-07-10 thomas sig_msg = NULL;
6009 795b88cb 2023-07-10 thomas if (commit) {
6010 795b88cb 2023-07-10 thomas err = got_object_commit_get_logmsg(&tagmsg0, commit);
6011 795b88cb 2023-07-10 thomas if (err)
6013 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
6014 795b88cb 2023-07-10 thomas } else {
6015 795b88cb 2023-07-10 thomas tagmsg0 = strdup(got_object_tag_get_message(tag));
6016 795b88cb 2023-07-10 thomas got_object_tag_close(tag);
6017 795b88cb 2023-07-10 thomas if (tagmsg0 == NULL) {
6018 795b88cb 2023-07-10 thomas err = got_error_from_errno("strdup");
6023 795b88cb 2023-07-10 thomas tagmsg = tagmsg0;
6025 795b88cb 2023-07-10 thomas line = strsep(&tagmsg, "\n");
6026 795b88cb 2023-07-10 thomas if (line)
6027 795b88cb 2023-07-10 thomas printf(" %s\n", line);
6028 795b88cb 2023-07-10 thomas } while (line);
6029 795b88cb 2023-07-10 thomas free(tagmsg0);
6032 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
6033 795b88cb 2023-07-10 thomas free(wanted_refname);
6035 795b88cb 2023-07-10 thomas if (err == NULL && bad_sigs)
6036 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
6037 795b88cb 2023-07-10 thomas return err;
6040 795b88cb 2023-07-10 thomas static const struct got_error *
6041 795b88cb 2023-07-10 thomas get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6042 795b88cb 2023-07-10 thomas const char *tag_name, const char *repo_path)
6044 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
6045 795b88cb 2023-07-10 thomas char *template = NULL, *initial_content = NULL;
6046 795b88cb 2023-07-10 thomas char *editor = NULL;
6047 795b88cb 2023-07-10 thomas int initial_content_len;
6048 795b88cb 2023-07-10 thomas int fd = -1;
6050 795b88cb 2023-07-10 thomas if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6051 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
6052 795b88cb 2023-07-10 thomas goto done;
6055 795b88cb 2023-07-10 thomas initial_content_len = asprintf(&initial_content,
6056 795b88cb 2023-07-10 thomas "\n# tagging commit %s as %s\n",
6057 795b88cb 2023-07-10 thomas commit_id_str, tag_name);
6058 795b88cb 2023-07-10 thomas if (initial_content_len == -1) {
6059 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
6060 795b88cb 2023-07-10 thomas goto done;
6063 795b88cb 2023-07-10 thomas err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
6064 795b88cb 2023-07-10 thomas if (err)
6065 795b88cb 2023-07-10 thomas goto done;
6067 795b88cb 2023-07-10 thomas if (write(fd, initial_content, initial_content_len) == -1) {
6068 795b88cb 2023-07-10 thomas err = got_error_from_errno2("write", *tagmsg_path);
6069 795b88cb 2023-07-10 thomas goto done;
6071 795b88cb 2023-07-10 thomas if (close(fd) == -1) {
6072 795b88cb 2023-07-10 thomas err = got_error_from_errno2("close", *tagmsg_path);
6073 795b88cb 2023-07-10 thomas goto done;
6075 795b88cb 2023-07-10 thomas fd = -1;
6077 795b88cb 2023-07-10 thomas err = get_editor(&editor);
6078 795b88cb 2023-07-10 thomas if (err)
6079 795b88cb 2023-07-10 thomas goto done;
6080 795b88cb 2023-07-10 thomas err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6081 795b88cb 2023-07-10 thomas initial_content_len, 1);
6083 795b88cb 2023-07-10 thomas free(initial_content);
6084 795b88cb 2023-07-10 thomas free(template);
6085 795b88cb 2023-07-10 thomas free(editor);
6087 795b88cb 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
6088 795b88cb 2023-07-10 thomas err = got_error_from_errno2("close", *tagmsg_path);
6090 795b88cb 2023-07-10 thomas if (err) {
6091 795b88cb 2023-07-10 thomas free(*tagmsg);
6092 795b88cb 2023-07-10 thomas *tagmsg = NULL;
6094 795b88cb 2023-07-10 thomas return err;
6097 795b88cb 2023-07-10 thomas static const struct got_error *
6098 795b88cb 2023-07-10 thomas add_tag(struct got_repository *repo, const char *tagger,
6099 795b88cb 2023-07-10 thomas const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
6100 795b88cb 2023-07-10 thomas const char *signer_id, int verbosity)
6102 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
6103 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL, *tag_id = NULL;
6104 795b88cb 2023-07-10 thomas char *label = NULL, *commit_id_str = NULL;
6105 795b88cb 2023-07-10 thomas struct got_reference *ref = NULL;
6106 795b88cb 2023-07-10 thomas char *refname = NULL, *tagmsg = NULL;
6107 795b88cb 2023-07-10 thomas char *tagmsg_path = NULL, *tag_id_str = NULL;
6108 795b88cb 2023-07-10 thomas int preserve_tagmsg = 0;
6109 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
6111 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
6114 795b88cb 2023-07-10 thomas * Don't let the user create a tag name with a leading '-'.
6115 795b88cb 2023-07-10 thomas * While technically a valid reference name, this case is usually
6116 795b88cb 2023-07-10 thomas * an unintended typo.
6118 795b88cb 2023-07-10 thomas if (tag_name[0] == '-')
6119 795b88cb 2023-07-10 thomas return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6121 795b88cb 2023-07-10 thomas err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6122 795b88cb 2023-07-10 thomas if (err)
6123 795b88cb 2023-07-10 thomas goto done;
6125 795b88cb 2023-07-10 thomas err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6126 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, &refs, repo);
6127 795b88cb 2023-07-10 thomas if (err)
6128 795b88cb 2023-07-10 thomas goto done;
6130 795b88cb 2023-07-10 thomas err = got_object_id_str(&commit_id_str, commit_id);
6131 795b88cb 2023-07-10 thomas if (err)
6132 795b88cb 2023-07-10 thomas goto done;
6134 795b88cb 2023-07-10 thomas err = get_tag_refname(&refname, tag_name);
6135 795b88cb 2023-07-10 thomas if (err)
6136 795b88cb 2023-07-10 thomas goto done;
6137 795b88cb 2023-07-10 thomas if (strncmp("refs/tags/", tag_name, 10) == 0)
6138 795b88cb 2023-07-10 thomas tag_name += 10;
6140 795b88cb 2023-07-10 thomas err = got_ref_open(&ref, repo, refname, 0);
6141 795b88cb 2023-07-10 thomas if (err == NULL) {
6142 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_TAG_EXISTS);
6143 795b88cb 2023-07-10 thomas goto done;
6144 795b88cb 2023-07-10 thomas } else if (err->code != GOT_ERR_NOT_REF)
6145 795b88cb 2023-07-10 thomas goto done;
6147 795b88cb 2023-07-10 thomas if (tagmsg_arg == NULL) {
6148 795b88cb 2023-07-10 thomas err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6149 795b88cb 2023-07-10 thomas tag_name, got_repo_get_path(repo));
6150 795b88cb 2023-07-10 thomas if (err) {
6151 795b88cb 2023-07-10 thomas if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6152 795b88cb 2023-07-10 thomas tagmsg_path != NULL)
6153 795b88cb 2023-07-10 thomas preserve_tagmsg = 1;
6154 795b88cb 2023-07-10 thomas goto done;
6156 795b88cb 2023-07-10 thomas /* Editor is done; we can now apply unveil(2) */
6157 795b88cb 2023-07-10 thomas err = got_sigs_apply_unveil();
6158 795b88cb 2023-07-10 thomas if (err)
6159 795b88cb 2023-07-10 thomas goto done;
6160 795b88cb 2023-07-10 thomas err = apply_unveil(got_repo_get_path(repo), 0, NULL);
6161 795b88cb 2023-07-10 thomas if (err)
6162 795b88cb 2023-07-10 thomas goto done;
6165 795b88cb 2023-07-10 thomas err = got_object_tag_create(&tag_id, tag_name, commit_id,
6166 795b88cb 2023-07-10 thomas tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
6167 795b88cb 2023-07-10 thomas verbosity);
6168 795b88cb 2023-07-10 thomas if (err) {
6169 795b88cb 2023-07-10 thomas if (tagmsg_path)
6170 795b88cb 2023-07-10 thomas preserve_tagmsg = 1;
6171 795b88cb 2023-07-10 thomas goto done;
6174 795b88cb 2023-07-10 thomas err = got_ref_alloc(&ref, refname, tag_id);
6175 795b88cb 2023-07-10 thomas if (err) {
6176 795b88cb 2023-07-10 thomas if (tagmsg_path)
6177 795b88cb 2023-07-10 thomas preserve_tagmsg = 1;
6178 795b88cb 2023-07-10 thomas goto done;
6181 795b88cb 2023-07-10 thomas err = got_ref_write(ref, repo);
6182 795b88cb 2023-07-10 thomas if (err) {
6183 795b88cb 2023-07-10 thomas if (tagmsg_path)
6184 795b88cb 2023-07-10 thomas preserve_tagmsg = 1;
6185 795b88cb 2023-07-10 thomas goto done;
6188 795b88cb 2023-07-10 thomas err = got_object_id_str(&tag_id_str, tag_id);
6189 795b88cb 2023-07-10 thomas if (err) {
6190 795b88cb 2023-07-10 thomas if (tagmsg_path)
6191 795b88cb 2023-07-10 thomas preserve_tagmsg = 1;
6192 795b88cb 2023-07-10 thomas goto done;
6194 795b88cb 2023-07-10 thomas printf("Created tag %s\n", tag_id_str);
6196 795b88cb 2023-07-10 thomas if (preserve_tagmsg) {
6197 795b88cb 2023-07-10 thomas fprintf(stderr, "%s: tag message preserved in %s\n",
6198 795b88cb 2023-07-10 thomas getprogname(), tagmsg_path);
6199 795b88cb 2023-07-10 thomas } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6200 795b88cb 2023-07-10 thomas err = got_error_from_errno2("unlink", tagmsg_path);
6201 795b88cb 2023-07-10 thomas free(tag_id_str);
6202 795b88cb 2023-07-10 thomas if (ref)
6203 795b88cb 2023-07-10 thomas got_ref_close(ref);
6204 795b88cb 2023-07-10 thomas free(commit_id);
6205 795b88cb 2023-07-10 thomas free(commit_id_str);
6206 795b88cb 2023-07-10 thomas free(refname);
6207 795b88cb 2023-07-10 thomas free(tagmsg);
6208 795b88cb 2023-07-10 thomas free(tagmsg_path);
6209 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
6210 795b88cb 2023-07-10 thomas return err;
6213 795b88cb 2023-07-10 thomas static const struct got_error *
6214 795b88cb 2023-07-10 thomas cmd_tag(int argc, char *argv[])
6216 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
6217 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
6218 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
6219 795b88cb 2023-07-10 thomas char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6220 795b88cb 2023-07-10 thomas char *gitconfig_path = NULL, *tagger = NULL;
6221 795b88cb 2023-07-10 thomas char *allowed_signers = NULL, *revoked_signers = NULL;
6222 795b88cb 2023-07-10 thomas const char *signer_id = NULL;
6223 795b88cb 2023-07-10 thomas const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
6224 795b88cb 2023-07-10 thomas int ch, do_list = 0, verify_tags = 0, verbosity = 0;
6225 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
6227 795b88cb 2023-07-10 thomas #ifndef PROFILE
6228 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6229 795b88cb 2023-07-10 thomas "sendfd unveil", NULL) == -1)
6230 795b88cb 2023-07-10 thomas err(1, "pledge");
6233 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
6234 795b88cb 2023-07-10 thomas switch (ch) {
6235 795b88cb 2023-07-10 thomas case 'c':
6236 795b88cb 2023-07-10 thomas commit_id_arg = optarg;
6238 795b88cb 2023-07-10 thomas case 'l':
6239 795b88cb 2023-07-10 thomas do_list = 1;
6241 795b88cb 2023-07-10 thomas case 'm':
6242 795b88cb 2023-07-10 thomas tagmsg = optarg;
6244 795b88cb 2023-07-10 thomas case 'r':
6245 795b88cb 2023-07-10 thomas repo_path = realpath(optarg, NULL);
6246 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
6247 795b88cb 2023-07-10 thomas error = got_error_from_errno2("realpath",
6248 795b88cb 2023-07-10 thomas optarg);
6249 795b88cb 2023-07-10 thomas goto done;
6251 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
6253 795b88cb 2023-07-10 thomas case 's':
6254 795b88cb 2023-07-10 thomas signer_id = optarg;
6256 795b88cb 2023-07-10 thomas case 'V':
6257 795b88cb 2023-07-10 thomas verify_tags = 1;
6259 795b88cb 2023-07-10 thomas case 'v':
6260 795b88cb 2023-07-10 thomas if (verbosity < 0)
6261 795b88cb 2023-07-10 thomas verbosity = 0;
6262 795b88cb 2023-07-10 thomas else if (verbosity < 3)
6263 795b88cb 2023-07-10 thomas verbosity++;
6265 795b88cb 2023-07-10 thomas default:
6266 795b88cb 2023-07-10 thomas usage_tag();
6267 795b88cb 2023-07-10 thomas /* NOTREACHED */
6271 795b88cb 2023-07-10 thomas argc -= optind;
6272 795b88cb 2023-07-10 thomas argv += optind;
6274 795b88cb 2023-07-10 thomas if (do_list || verify_tags) {
6275 795b88cb 2023-07-10 thomas if (commit_id_arg != NULL)
6277 795b88cb 2023-07-10 thomas "-c option can only be used when creating a tag");
6278 795b88cb 2023-07-10 thomas if (tagmsg) {
6279 795b88cb 2023-07-10 thomas if (do_list)
6280 795b88cb 2023-07-10 thomas option_conflict('l', 'm');
6282 795b88cb 2023-07-10 thomas option_conflict('V', 'm');
6284 795b88cb 2023-07-10 thomas if (signer_id) {
6285 795b88cb 2023-07-10 thomas if (do_list)
6286 795b88cb 2023-07-10 thomas option_conflict('l', 's');
6288 795b88cb 2023-07-10 thomas option_conflict('V', 's');
6290 795b88cb 2023-07-10 thomas if (argc > 1)
6291 795b88cb 2023-07-10 thomas usage_tag();
6292 795b88cb 2023-07-10 thomas } else if (argc != 1)
6293 795b88cb 2023-07-10 thomas usage_tag();
6295 795b88cb 2023-07-10 thomas if (argc == 1)
6296 795b88cb 2023-07-10 thomas tag_name = argv[0];
6298 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
6299 795b88cb 2023-07-10 thomas if (cwd == NULL) {
6300 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
6301 795b88cb 2023-07-10 thomas goto done;
6304 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
6305 795b88cb 2023-07-10 thomas if (error != NULL)
6306 795b88cb 2023-07-10 thomas goto done;
6308 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
6309 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
6310 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
6311 795b88cb 2023-07-10 thomas goto done;
6313 795b88cb 2023-07-10 thomas error = NULL;
6314 795b88cb 2023-07-10 thomas if (worktree) {
6315 795b88cb 2023-07-10 thomas repo_path =
6316 795b88cb 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree));
6317 795b88cb 2023-07-10 thomas if (repo_path == NULL)
6318 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
6319 795b88cb 2023-07-10 thomas if (error)
6320 795b88cb 2023-07-10 thomas goto done;
6321 795b88cb 2023-07-10 thomas } else {
6322 795b88cb 2023-07-10 thomas repo_path = strdup(cwd);
6323 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
6324 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
6325 795b88cb 2023-07-10 thomas goto done;
6330 795b88cb 2023-07-10 thomas if (do_list || verify_tags) {
6331 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6332 795b88cb 2023-07-10 thomas if (error != NULL)
6333 795b88cb 2023-07-10 thomas goto done;
6334 795b88cb 2023-07-10 thomas error = get_allowed_signers(&allowed_signers, repo, worktree);
6335 795b88cb 2023-07-10 thomas if (error)
6336 795b88cb 2023-07-10 thomas goto done;
6337 795b88cb 2023-07-10 thomas error = get_revoked_signers(&revoked_signers, repo, worktree);
6338 795b88cb 2023-07-10 thomas if (error)
6339 795b88cb 2023-07-10 thomas goto done;
6340 795b88cb 2023-07-10 thomas if (worktree) {
6341 795b88cb 2023-07-10 thomas /* Release work tree lock. */
6342 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
6343 795b88cb 2023-07-10 thomas worktree = NULL;
6347 795b88cb 2023-07-10 thomas * Remove "cpath" promise unless needed for signature tmpfile
6348 795b88cb 2023-07-10 thomas * creation.
6350 795b88cb 2023-07-10 thomas if (verify_tags)
6351 795b88cb 2023-07-10 thomas got_sigs_apply_unveil();
6353 795b88cb 2023-07-10 thomas #ifndef PROFILE
6354 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath flock proc exec sendfd "
6355 795b88cb 2023-07-10 thomas "unveil", NULL) == -1)
6356 795b88cb 2023-07-10 thomas err(1, "pledge");
6359 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6360 795b88cb 2023-07-10 thomas if (error)
6361 795b88cb 2023-07-10 thomas goto done;
6362 795b88cb 2023-07-10 thomas error = list_tags(repo, tag_name, verify_tags, allowed_signers,
6363 795b88cb 2023-07-10 thomas revoked_signers, verbosity);
6364 795b88cb 2023-07-10 thomas } else {
6365 795b88cb 2023-07-10 thomas error = get_gitconfig_path(&gitconfig_path);
6366 795b88cb 2023-07-10 thomas if (error)
6367 795b88cb 2023-07-10 thomas goto done;
6368 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, gitconfig_path,
6369 795b88cb 2023-07-10 thomas pack_fds);
6370 795b88cb 2023-07-10 thomas if (error != NULL)
6371 795b88cb 2023-07-10 thomas goto done;
6373 795b88cb 2023-07-10 thomas error = get_author(&tagger, repo, worktree);
6374 795b88cb 2023-07-10 thomas if (error)
6375 795b88cb 2023-07-10 thomas goto done;
6376 795b88cb 2023-07-10 thomas if (signer_id == NULL)
6377 795b88cb 2023-07-10 thomas signer_id = get_signer_id(repo, worktree);
6379 795b88cb 2023-07-10 thomas if (tagmsg) {
6380 795b88cb 2023-07-10 thomas if (signer_id) {
6381 795b88cb 2023-07-10 thomas error = got_sigs_apply_unveil();
6382 795b88cb 2023-07-10 thomas if (error)
6383 795b88cb 2023-07-10 thomas goto done;
6385 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6386 795b88cb 2023-07-10 thomas if (error)
6387 795b88cb 2023-07-10 thomas goto done;
6390 795b88cb 2023-07-10 thomas if (commit_id_arg == NULL) {
6391 795b88cb 2023-07-10 thomas struct got_reference *head_ref;
6392 795b88cb 2023-07-10 thomas struct got_object_id *commit_id;
6393 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo,
6394 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_head_ref_name(worktree)
6395 795b88cb 2023-07-10 thomas : GOT_REF_HEAD, 0);
6396 795b88cb 2023-07-10 thomas if (error)
6397 795b88cb 2023-07-10 thomas goto done;
6398 795b88cb 2023-07-10 thomas error = got_ref_resolve(&commit_id, repo, head_ref);
6399 795b88cb 2023-07-10 thomas got_ref_close(head_ref);
6400 795b88cb 2023-07-10 thomas if (error)
6401 795b88cb 2023-07-10 thomas goto done;
6402 795b88cb 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
6403 795b88cb 2023-07-10 thomas free(commit_id);
6404 795b88cb 2023-07-10 thomas if (error)
6405 795b88cb 2023-07-10 thomas goto done;
6408 795b88cb 2023-07-10 thomas if (worktree) {
6409 795b88cb 2023-07-10 thomas /* Release work tree lock. */
6410 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
6411 795b88cb 2023-07-10 thomas worktree = NULL;
6414 795b88cb 2023-07-10 thomas error = add_tag(repo, tagger, tag_name,
6415 795b88cb 2023-07-10 thomas commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
6416 795b88cb 2023-07-10 thomas signer_id, verbosity);
6419 795b88cb 2023-07-10 thomas if (repo) {
6420 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
6421 795b88cb 2023-07-10 thomas if (error == NULL)
6422 795b88cb 2023-07-10 thomas error = close_err;
6424 795b88cb 2023-07-10 thomas if (worktree)
6425 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
6426 795b88cb 2023-07-10 thomas if (pack_fds) {
6427 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
6428 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
6429 795b88cb 2023-07-10 thomas if (error == NULL)
6430 795b88cb 2023-07-10 thomas error = pack_err;
6432 795b88cb 2023-07-10 thomas free(cwd);
6433 795b88cb 2023-07-10 thomas free(repo_path);
6434 795b88cb 2023-07-10 thomas free(gitconfig_path);
6435 795b88cb 2023-07-10 thomas free(commit_id_str);
6436 795b88cb 2023-07-10 thomas free(tagger);
6437 795b88cb 2023-07-10 thomas free(allowed_signers);
6438 795b88cb 2023-07-10 thomas free(revoked_signers);
6439 795b88cb 2023-07-10 thomas return error;
6442 795b88cb 2023-07-10 thomas __dead static void
6443 795b88cb 2023-07-10 thomas usage_add(void)
6445 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
6446 795b88cb 2023-07-10 thomas exit(1);
6449 795b88cb 2023-07-10 thomas static const struct got_error *
6450 795b88cb 2023-07-10 thomas add_progress(void *arg, unsigned char status, const char *path)
6452 795b88cb 2023-07-10 thomas while (path[0] == '/')
6454 795b88cb 2023-07-10 thomas printf("%c %s\n", status, path);
6455 795b88cb 2023-07-10 thomas return NULL;
6458 795b88cb 2023-07-10 thomas static const struct got_error *
6459 795b88cb 2023-07-10 thomas cmd_add(int argc, char *argv[])
6461 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
6462 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
6463 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
6464 795b88cb 2023-07-10 thomas char *cwd = NULL;
6465 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
6466 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
6467 795b88cb 2023-07-10 thomas int ch, can_recurse = 0, no_ignores = 0;
6468 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
6470 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
6472 795b88cb 2023-07-10 thomas #ifndef PROFILE
6473 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6474 795b88cb 2023-07-10 thomas NULL) == -1)
6475 795b88cb 2023-07-10 thomas err(1, "pledge");
6478 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "IR")) != -1) {
6479 795b88cb 2023-07-10 thomas switch (ch) {
6480 795b88cb 2023-07-10 thomas case 'I':
6481 795b88cb 2023-07-10 thomas no_ignores = 1;
6483 795b88cb 2023-07-10 thomas case 'R':
6484 795b88cb 2023-07-10 thomas can_recurse = 1;
6486 795b88cb 2023-07-10 thomas default:
6487 795b88cb 2023-07-10 thomas usage_add();
6488 795b88cb 2023-07-10 thomas /* NOTREACHED */
6492 795b88cb 2023-07-10 thomas argc -= optind;
6493 795b88cb 2023-07-10 thomas argv += optind;
6495 795b88cb 2023-07-10 thomas if (argc < 1)
6496 795b88cb 2023-07-10 thomas usage_add();
6498 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
6499 795b88cb 2023-07-10 thomas if (cwd == NULL) {
6500 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
6501 795b88cb 2023-07-10 thomas goto done;
6504 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
6505 795b88cb 2023-07-10 thomas if (error != NULL)
6506 795b88cb 2023-07-10 thomas goto done;
6508 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
6509 795b88cb 2023-07-10 thomas if (error) {
6510 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
6511 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error, "add", cwd);
6512 795b88cb 2023-07-10 thomas goto done;
6515 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6516 795b88cb 2023-07-10 thomas NULL, pack_fds);
6517 795b88cb 2023-07-10 thomas if (error != NULL)
6518 795b88cb 2023-07-10 thomas goto done;
6520 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1,
6521 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree));
6522 795b88cb 2023-07-10 thomas if (error)
6523 795b88cb 2023-07-10 thomas goto done;
6525 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6526 795b88cb 2023-07-10 thomas if (error)
6527 795b88cb 2023-07-10 thomas goto done;
6529 795b88cb 2023-07-10 thomas if (!can_recurse) {
6530 795b88cb 2023-07-10 thomas char *ondisk_path;
6531 795b88cb 2023-07-10 thomas struct stat sb;
6532 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &paths, entry) {
6533 795b88cb 2023-07-10 thomas if (asprintf(&ondisk_path, "%s/%s",
6534 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree),
6535 795b88cb 2023-07-10 thomas pe->path) == -1) {
6536 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
6537 795b88cb 2023-07-10 thomas goto done;
6539 795b88cb 2023-07-10 thomas if (lstat(ondisk_path, &sb) == -1) {
6540 795b88cb 2023-07-10 thomas if (errno == ENOENT) {
6541 795b88cb 2023-07-10 thomas free(ondisk_path);
6542 795b88cb 2023-07-10 thomas continue;
6544 795b88cb 2023-07-10 thomas error = got_error_from_errno2("lstat",
6545 795b88cb 2023-07-10 thomas ondisk_path);
6546 795b88cb 2023-07-10 thomas free(ondisk_path);
6547 795b88cb 2023-07-10 thomas goto done;
6549 795b88cb 2023-07-10 thomas free(ondisk_path);
6550 795b88cb 2023-07-10 thomas if (S_ISDIR(sb.st_mode)) {
6551 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_BAD_PATH,
6552 795b88cb 2023-07-10 thomas "adding directories requires -R option");
6553 795b88cb 2023-07-10 thomas goto done;
6558 795b88cb 2023-07-10 thomas error = got_worktree_schedule_add(worktree, &paths, add_progress,
6559 795b88cb 2023-07-10 thomas NULL, repo, no_ignores);
6561 795b88cb 2023-07-10 thomas if (repo) {
6562 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
6563 795b88cb 2023-07-10 thomas if (error == NULL)
6564 795b88cb 2023-07-10 thomas error = close_err;
6566 795b88cb 2023-07-10 thomas if (worktree)
6567 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
6568 795b88cb 2023-07-10 thomas if (pack_fds) {
6569 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
6570 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
6571 795b88cb 2023-07-10 thomas if (error == NULL)
6572 795b88cb 2023-07-10 thomas error = pack_err;
6574 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6575 795b88cb 2023-07-10 thomas free(cwd);
6576 795b88cb 2023-07-10 thomas return error;
6579 795b88cb 2023-07-10 thomas __dead static void
6580 795b88cb 2023-07-10 thomas usage_remove(void)
6582 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
6583 795b88cb 2023-07-10 thomas getprogname());
6584 795b88cb 2023-07-10 thomas exit(1);
6587 795b88cb 2023-07-10 thomas static const struct got_error *
6588 795b88cb 2023-07-10 thomas print_remove_status(void *arg, unsigned char status,
6589 795b88cb 2023-07-10 thomas unsigned char staged_status, const char *path)
6591 795b88cb 2023-07-10 thomas while (path[0] == '/')
6593 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_NONEXISTENT)
6594 795b88cb 2023-07-10 thomas return NULL;
6595 795b88cb 2023-07-10 thomas if (status == staged_status && (status == GOT_STATUS_DELETE))
6596 795b88cb 2023-07-10 thomas status = GOT_STATUS_NO_CHANGE;
6597 795b88cb 2023-07-10 thomas printf("%c%c %s\n", status, staged_status, path);
6598 795b88cb 2023-07-10 thomas return NULL;
6601 795b88cb 2023-07-10 thomas static const struct got_error *
6602 795b88cb 2023-07-10 thomas cmd_remove(int argc, char *argv[])
6604 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
6605 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
6606 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
6607 795b88cb 2023-07-10 thomas const char *status_codes = NULL;
6608 795b88cb 2023-07-10 thomas char *cwd = NULL;
6609 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
6610 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
6611 795b88cb 2023-07-10 thomas int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6612 795b88cb 2023-07-10 thomas int ignore_missing_paths = 0;
6613 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
6615 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
6617 795b88cb 2023-07-10 thomas #ifndef PROFILE
6618 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6619 795b88cb 2023-07-10 thomas NULL) == -1)
6620 795b88cb 2023-07-10 thomas err(1, "pledge");
6623 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6624 795b88cb 2023-07-10 thomas switch (ch) {
6625 795b88cb 2023-07-10 thomas case 'f':
6626 795b88cb 2023-07-10 thomas delete_local_mods = 1;
6627 795b88cb 2023-07-10 thomas ignore_missing_paths = 1;
6629 795b88cb 2023-07-10 thomas case 'k':
6630 795b88cb 2023-07-10 thomas keep_on_disk = 1;
6632 795b88cb 2023-07-10 thomas case 'R':
6633 795b88cb 2023-07-10 thomas can_recurse = 1;
6635 795b88cb 2023-07-10 thomas case 's':
6636 795b88cb 2023-07-10 thomas for (i = 0; optarg[i] != '\0'; i++) {
6637 795b88cb 2023-07-10 thomas switch (optarg[i]) {
6638 795b88cb 2023-07-10 thomas case GOT_STATUS_MODIFY:
6639 795b88cb 2023-07-10 thomas delete_local_mods = 1;
6641 795b88cb 2023-07-10 thomas case GOT_STATUS_MISSING:
6642 795b88cb 2023-07-10 thomas ignore_missing_paths = 1;
6644 795b88cb 2023-07-10 thomas default:
6645 795b88cb 2023-07-10 thomas errx(1, "invalid status code '%c'",
6646 795b88cb 2023-07-10 thomas optarg[i]);
6649 795b88cb 2023-07-10 thomas status_codes = optarg;
6651 795b88cb 2023-07-10 thomas default:
6652 795b88cb 2023-07-10 thomas usage_remove();
6653 795b88cb 2023-07-10 thomas /* NOTREACHED */
6657 795b88cb 2023-07-10 thomas argc -= optind;
6658 795b88cb 2023-07-10 thomas argv += optind;
6660 795b88cb 2023-07-10 thomas if (argc < 1)
6661 795b88cb 2023-07-10 thomas usage_remove();
6663 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
6664 795b88cb 2023-07-10 thomas if (cwd == NULL) {
6665 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
6666 795b88cb 2023-07-10 thomas goto done;
6669 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
6670 795b88cb 2023-07-10 thomas if (error != NULL)
6671 795b88cb 2023-07-10 thomas goto done;
6673 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
6674 795b88cb 2023-07-10 thomas if (error) {
6675 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
6676 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error, "remove", cwd);
6677 795b88cb 2023-07-10 thomas goto done;
6680 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6681 795b88cb 2023-07-10 thomas NULL, pack_fds);
6682 795b88cb 2023-07-10 thomas if (error)
6683 795b88cb 2023-07-10 thomas goto done;
6685 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1,
6686 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree));
6687 795b88cb 2023-07-10 thomas if (error)
6688 795b88cb 2023-07-10 thomas goto done;
6690 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6691 795b88cb 2023-07-10 thomas if (error)
6692 795b88cb 2023-07-10 thomas goto done;
6694 795b88cb 2023-07-10 thomas if (!can_recurse) {
6695 795b88cb 2023-07-10 thomas char *ondisk_path;
6696 795b88cb 2023-07-10 thomas struct stat sb;
6697 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &paths, entry) {
6698 795b88cb 2023-07-10 thomas if (asprintf(&ondisk_path, "%s/%s",
6699 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree),
6700 795b88cb 2023-07-10 thomas pe->path) == -1) {
6701 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
6702 795b88cb 2023-07-10 thomas goto done;
6704 795b88cb 2023-07-10 thomas if (lstat(ondisk_path, &sb) == -1) {
6705 795b88cb 2023-07-10 thomas if (errno == ENOENT) {
6706 795b88cb 2023-07-10 thomas free(ondisk_path);
6707 795b88cb 2023-07-10 thomas continue;
6709 795b88cb 2023-07-10 thomas error = got_error_from_errno2("lstat",
6710 795b88cb 2023-07-10 thomas ondisk_path);
6711 795b88cb 2023-07-10 thomas free(ondisk_path);
6712 795b88cb 2023-07-10 thomas goto done;
6714 795b88cb 2023-07-10 thomas free(ondisk_path);
6715 795b88cb 2023-07-10 thomas if (S_ISDIR(sb.st_mode)) {
6716 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_BAD_PATH,
6717 795b88cb 2023-07-10 thomas "removing directories requires -R option");
6718 795b88cb 2023-07-10 thomas goto done;
6723 795b88cb 2023-07-10 thomas error = got_worktree_schedule_delete(worktree, &paths,
6724 795b88cb 2023-07-10 thomas delete_local_mods, status_codes, print_remove_status, NULL,
6725 795b88cb 2023-07-10 thomas repo, keep_on_disk, ignore_missing_paths);
6727 795b88cb 2023-07-10 thomas if (repo) {
6728 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
6729 795b88cb 2023-07-10 thomas if (error == NULL)
6730 795b88cb 2023-07-10 thomas error = close_err;
6732 795b88cb 2023-07-10 thomas if (worktree)
6733 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
6734 795b88cb 2023-07-10 thomas if (pack_fds) {
6735 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
6736 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
6737 795b88cb 2023-07-10 thomas if (error == NULL)
6738 795b88cb 2023-07-10 thomas error = pack_err;
6740 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6741 795b88cb 2023-07-10 thomas free(cwd);
6742 795b88cb 2023-07-10 thomas return error;
6745 795b88cb 2023-07-10 thomas __dead static void
6746 795b88cb 2023-07-10 thomas usage_patch(void)
6748 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
6749 795b88cb 2023-07-10 thomas "[patchfile]\n", getprogname());
6750 795b88cb 2023-07-10 thomas exit(1);
6753 795b88cb 2023-07-10 thomas static const struct got_error *
6754 795b88cb 2023-07-10 thomas patch_from_stdin(int *patchfd)
6756 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
6757 795b88cb 2023-07-10 thomas ssize_t r;
6758 795b88cb 2023-07-10 thomas char buf[BUFSIZ];
6759 795b88cb 2023-07-10 thomas sig_t sighup, sigint, sigquit;
6761 795b88cb 2023-07-10 thomas *patchfd = got_opentempfd();
6762 795b88cb 2023-07-10 thomas if (*patchfd == -1)
6763 795b88cb 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
6765 795b88cb 2023-07-10 thomas sighup = signal(SIGHUP, SIG_DFL);
6766 795b88cb 2023-07-10 thomas sigint = signal(SIGINT, SIG_DFL);
6767 795b88cb 2023-07-10 thomas sigquit = signal(SIGQUIT, SIG_DFL);
6769 795b88cb 2023-07-10 thomas for (;;) {
6770 795b88cb 2023-07-10 thomas r = read(0, buf, sizeof(buf));
6771 795b88cb 2023-07-10 thomas if (r == -1) {
6772 795b88cb 2023-07-10 thomas err = got_error_from_errno("read");
6775 795b88cb 2023-07-10 thomas if (r == 0)
6777 795b88cb 2023-07-10 thomas if (write(*patchfd, buf, r) == -1) {
6778 795b88cb 2023-07-10 thomas err = got_error_from_errno("write");
6783 795b88cb 2023-07-10 thomas signal(SIGHUP, sighup);
6784 795b88cb 2023-07-10 thomas signal(SIGINT, sigint);
6785 795b88cb 2023-07-10 thomas signal(SIGQUIT, sigquit);
6787 795b88cb 2023-07-10 thomas if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
6788 795b88cb 2023-07-10 thomas err = got_error_from_errno("lseek");
6790 795b88cb 2023-07-10 thomas if (err != NULL) {
6791 795b88cb 2023-07-10 thomas close(*patchfd);
6792 795b88cb 2023-07-10 thomas *patchfd = -1;
6795 795b88cb 2023-07-10 thomas return err;
6798 795b88cb 2023-07-10 thomas struct got_patch_progress_arg {
6799 795b88cb 2023-07-10 thomas int did_something;
6800 795b88cb 2023-07-10 thomas int conflicts;
6801 795b88cb 2023-07-10 thomas int rejects;
6804 795b88cb 2023-07-10 thomas static const struct got_error *
6805 795b88cb 2023-07-10 thomas patch_progress(void *arg, const char *old, const char *new,
6806 795b88cb 2023-07-10 thomas unsigned char status, const struct got_error *error, int old_from,
6807 795b88cb 2023-07-10 thomas int old_lines, int new_from, int new_lines, int offset,
6808 795b88cb 2023-07-10 thomas int ws_mangled, const struct got_error *hunk_err)
6810 795b88cb 2023-07-10 thomas const char *path = new == NULL ? old : new;
6811 795b88cb 2023-07-10 thomas struct got_patch_progress_arg *a = arg;
6813 795b88cb 2023-07-10 thomas while (*path == '/')
6816 795b88cb 2023-07-10 thomas if (status != GOT_STATUS_NO_CHANGE &&
6817 795b88cb 2023-07-10 thomas status != 0 /* per-hunk progress */) {
6818 795b88cb 2023-07-10 thomas printf("%c %s\n", status, path);
6819 795b88cb 2023-07-10 thomas a->did_something = 1;
6822 795b88cb 2023-07-10 thomas if (hunk_err == NULL) {
6823 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_CANNOT_UPDATE)
6824 795b88cb 2023-07-10 thomas a->rejects++;
6825 795b88cb 2023-07-10 thomas else if (status == GOT_STATUS_CONFLICT)
6826 795b88cb 2023-07-10 thomas a->conflicts++;
6829 795b88cb 2023-07-10 thomas if (error != NULL)
6830 795b88cb 2023-07-10 thomas fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6832 795b88cb 2023-07-10 thomas if (offset != 0 || hunk_err != NULL || ws_mangled) {
6833 795b88cb 2023-07-10 thomas printf("@@ -%d,%d +%d,%d @@ ", old_from,
6834 795b88cb 2023-07-10 thomas old_lines, new_from, new_lines);
6835 795b88cb 2023-07-10 thomas if (hunk_err != NULL)
6836 795b88cb 2023-07-10 thomas printf("%s\n", hunk_err->msg);
6837 795b88cb 2023-07-10 thomas else if (offset != 0)
6838 795b88cb 2023-07-10 thomas printf("applied with offset %d\n", offset);
6840 795b88cb 2023-07-10 thomas printf("hunk contains mangled whitespace\n");
6843 795b88cb 2023-07-10 thomas return NULL;
6846 795b88cb 2023-07-10 thomas static void
6847 795b88cb 2023-07-10 thomas print_patch_progress_stats(struct got_patch_progress_arg *ppa)
6849 795b88cb 2023-07-10 thomas if (!ppa->did_something)
6852 795b88cb 2023-07-10 thomas if (ppa->conflicts > 0)
6853 795b88cb 2023-07-10 thomas printf("Files with merge conflicts: %d\n", ppa->conflicts);
6855 795b88cb 2023-07-10 thomas if (ppa->rejects > 0) {
6856 795b88cb 2023-07-10 thomas printf("Files where patch failed to apply: %d\n",
6857 795b88cb 2023-07-10 thomas ppa->rejects);
6861 795b88cb 2023-07-10 thomas static const struct got_error *
6862 795b88cb 2023-07-10 thomas cmd_patch(int argc, char *argv[])
6864 795b88cb 2023-07-10 thomas const struct got_error *error = NULL, *close_error = NULL;
6865 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
6866 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
6867 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
6868 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
6869 795b88cb 2023-07-10 thomas const char *commit_id_str = NULL;
6870 795b88cb 2023-07-10 thomas struct stat sb;
6871 795b88cb 2023-07-10 thomas const char *errstr;
6872 795b88cb 2023-07-10 thomas char *cwd = NULL;
6873 795b88cb 2023-07-10 thomas int ch, nop = 0, strip = -1, reverse = 0;
6874 795b88cb 2023-07-10 thomas int patchfd;
6875 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
6876 795b88cb 2023-07-10 thomas struct got_patch_progress_arg ppa;
6878 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
6880 795b88cb 2023-07-10 thomas #ifndef PROFILE
6881 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
6882 795b88cb 2023-07-10 thomas "unveil", NULL) == -1)
6883 795b88cb 2023-07-10 thomas err(1, "pledge");
6886 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
6887 795b88cb 2023-07-10 thomas switch (ch) {
6888 795b88cb 2023-07-10 thomas case 'c':
6889 795b88cb 2023-07-10 thomas commit_id_str = optarg;
6891 795b88cb 2023-07-10 thomas case 'n':
6892 795b88cb 2023-07-10 thomas nop = 1;
6894 795b88cb 2023-07-10 thomas case 'p':
6895 795b88cb 2023-07-10 thomas strip = strtonum(optarg, 0, INT_MAX, &errstr);
6896 795b88cb 2023-07-10 thomas if (errstr != NULL)
6897 795b88cb 2023-07-10 thomas errx(1, "pathname strip count is %s: %s",
6898 795b88cb 2023-07-10 thomas errstr, optarg);
6900 795b88cb 2023-07-10 thomas case 'R':
6901 795b88cb 2023-07-10 thomas reverse = 1;
6903 795b88cb 2023-07-10 thomas default:
6904 795b88cb 2023-07-10 thomas usage_patch();
6905 795b88cb 2023-07-10 thomas /* NOTREACHED */
6909 795b88cb 2023-07-10 thomas argc -= optind;
6910 795b88cb 2023-07-10 thomas argv += optind;
6912 795b88cb 2023-07-10 thomas if (argc == 0) {
6913 795b88cb 2023-07-10 thomas error = patch_from_stdin(&patchfd);
6914 795b88cb 2023-07-10 thomas if (error)
6915 795b88cb 2023-07-10 thomas return error;
6916 795b88cb 2023-07-10 thomas } else if (argc == 1) {
6917 795b88cb 2023-07-10 thomas patchfd = open(argv[0], O_RDONLY);
6918 795b88cb 2023-07-10 thomas if (patchfd == -1) {
6919 795b88cb 2023-07-10 thomas error = got_error_from_errno2("open", argv[0]);
6920 795b88cb 2023-07-10 thomas return error;
6922 795b88cb 2023-07-10 thomas if (fstat(patchfd, &sb) == -1) {
6923 795b88cb 2023-07-10 thomas error = got_error_from_errno2("fstat", argv[0]);
6924 795b88cb 2023-07-10 thomas goto done;
6926 795b88cb 2023-07-10 thomas if (!S_ISREG(sb.st_mode)) {
6927 795b88cb 2023-07-10 thomas error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
6928 795b88cb 2023-07-10 thomas goto done;
6931 795b88cb 2023-07-10 thomas usage_patch();
6933 795b88cb 2023-07-10 thomas if ((cwd = getcwd(NULL, 0)) == NULL) {
6934 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
6935 795b88cb 2023-07-10 thomas goto done;
6938 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
6939 795b88cb 2023-07-10 thomas if (error != NULL)
6940 795b88cb 2023-07-10 thomas goto done;
6942 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
6943 795b88cb 2023-07-10 thomas if (error != NULL)
6944 795b88cb 2023-07-10 thomas goto done;
6946 795b88cb 2023-07-10 thomas const char *repo_path = got_worktree_get_repo_path(worktree);
6947 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6948 795b88cb 2023-07-10 thomas if (error != NULL)
6949 795b88cb 2023-07-10 thomas goto done;
6951 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
6952 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree));
6953 795b88cb 2023-07-10 thomas if (error != NULL)
6954 795b88cb 2023-07-10 thomas goto done;
6956 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6957 795b88cb 2023-07-10 thomas if (error)
6958 795b88cb 2023-07-10 thomas goto done;
6960 795b88cb 2023-07-10 thomas if (commit_id_str != NULL) {
6961 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
6962 795b88cb 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6963 795b88cb 2023-07-10 thomas if (error)
6964 795b88cb 2023-07-10 thomas goto done;
6967 795b88cb 2023-07-10 thomas memset(&ppa, 0, sizeof(ppa));
6968 795b88cb 2023-07-10 thomas error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
6969 795b88cb 2023-07-10 thomas commit_id, patch_progress, &ppa, check_cancelled, NULL);
6970 795b88cb 2023-07-10 thomas print_patch_progress_stats(&ppa);
6972 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
6973 795b88cb 2023-07-10 thomas free(commit_id);
6974 795b88cb 2023-07-10 thomas if (repo) {
6975 795b88cb 2023-07-10 thomas close_error = got_repo_close(repo);
6976 795b88cb 2023-07-10 thomas if (error == NULL)
6977 795b88cb 2023-07-10 thomas error = close_error;
6979 795b88cb 2023-07-10 thomas if (worktree != NULL) {
6980 795b88cb 2023-07-10 thomas close_error = got_worktree_close(worktree);
6981 795b88cb 2023-07-10 thomas if (error == NULL)
6982 795b88cb 2023-07-10 thomas error = close_error;
6984 795b88cb 2023-07-10 thomas if (pack_fds) {
6985 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
6986 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
6987 795b88cb 2023-07-10 thomas if (error == NULL)
6988 795b88cb 2023-07-10 thomas error = pack_err;
6990 795b88cb 2023-07-10 thomas free(cwd);
6991 795b88cb 2023-07-10 thomas return error;
6994 795b88cb 2023-07-10 thomas __dead static void
6995 795b88cb 2023-07-10 thomas usage_revert(void)
6997 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
6998 795b88cb 2023-07-10 thomas getprogname());
6999 795b88cb 2023-07-10 thomas exit(1);
7002 795b88cb 2023-07-10 thomas static const struct got_error *
7003 795b88cb 2023-07-10 thomas revert_progress(void *arg, unsigned char status, const char *path)
7005 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_UNVERSIONED)
7006 795b88cb 2023-07-10 thomas return NULL;
7008 795b88cb 2023-07-10 thomas while (path[0] == '/')
7010 795b88cb 2023-07-10 thomas printf("%c %s\n", status, path);
7011 795b88cb 2023-07-10 thomas return NULL;
7014 795b88cb 2023-07-10 thomas struct choose_patch_arg {
7015 795b88cb 2023-07-10 thomas FILE *patch_script_file;
7016 795b88cb 2023-07-10 thomas const char *action;
7019 795b88cb 2023-07-10 thomas static const struct got_error *
7020 795b88cb 2023-07-10 thomas show_change(unsigned char status, const char *path, FILE *patch_file, int n,
7021 795b88cb 2023-07-10 thomas int nchanges, const char *action)
7023 795b88cb 2023-07-10 thomas const struct got_error *err;
7024 795b88cb 2023-07-10 thomas char *line = NULL;
7025 795b88cb 2023-07-10 thomas size_t linesize = 0;
7026 795b88cb 2023-07-10 thomas ssize_t linelen;
7028 795b88cb 2023-07-10 thomas switch (status) {
7029 795b88cb 2023-07-10 thomas case GOT_STATUS_ADD:
7030 795b88cb 2023-07-10 thomas printf("A %s\n%s this addition? [y/n] ", path, action);
7032 795b88cb 2023-07-10 thomas case GOT_STATUS_DELETE:
7033 795b88cb 2023-07-10 thomas printf("D %s\n%s this deletion? [y/n] ", path, action);
7035 795b88cb 2023-07-10 thomas case GOT_STATUS_MODIFY:
7036 795b88cb 2023-07-10 thomas if (fseek(patch_file, 0L, SEEK_SET) == -1)
7037 795b88cb 2023-07-10 thomas return got_error_from_errno("fseek");
7038 795b88cb 2023-07-10 thomas printf(GOT_COMMIT_SEP_STR);
7039 795b88cb 2023-07-10 thomas while ((linelen = getline(&line, &linesize, patch_file)) != -1)
7040 795b88cb 2023-07-10 thomas printf("%s", line);
7041 795b88cb 2023-07-10 thomas if (linelen == -1 && ferror(patch_file)) {
7042 795b88cb 2023-07-10 thomas err = got_error_from_errno("getline");
7043 795b88cb 2023-07-10 thomas free(line);
7044 795b88cb 2023-07-10 thomas return err;
7046 795b88cb 2023-07-10 thomas free(line);
7047 795b88cb 2023-07-10 thomas printf(GOT_COMMIT_SEP_STR);
7048 795b88cb 2023-07-10 thomas printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
7049 795b88cb 2023-07-10 thomas path, n, nchanges, action);
7051 795b88cb 2023-07-10 thomas default:
7052 795b88cb 2023-07-10 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
7055 795b88cb 2023-07-10 thomas fflush(stdout);
7056 795b88cb 2023-07-10 thomas return NULL;
7059 795b88cb 2023-07-10 thomas static const struct got_error *
7060 795b88cb 2023-07-10 thomas choose_patch(int *choice, void *arg, unsigned char status, const char *path,
7061 795b88cb 2023-07-10 thomas FILE *patch_file, int n, int nchanges)
7063 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
7064 795b88cb 2023-07-10 thomas char *line = NULL;
7065 795b88cb 2023-07-10 thomas size_t linesize = 0;
7066 795b88cb 2023-07-10 thomas ssize_t linelen;
7067 795b88cb 2023-07-10 thomas int resp = ' ';
7068 795b88cb 2023-07-10 thomas struct choose_patch_arg *a = arg;
7070 795b88cb 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_NONE;
7072 795b88cb 2023-07-10 thomas if (a->patch_script_file) {
7073 795b88cb 2023-07-10 thomas char *nl;
7074 795b88cb 2023-07-10 thomas err = show_change(status, path, patch_file, n, nchanges,
7075 795b88cb 2023-07-10 thomas a->action);
7076 795b88cb 2023-07-10 thomas if (err)
7077 795b88cb 2023-07-10 thomas return err;
7078 795b88cb 2023-07-10 thomas linelen = getline(&line, &linesize, a->patch_script_file);
7079 795b88cb 2023-07-10 thomas if (linelen == -1) {
7080 795b88cb 2023-07-10 thomas if (ferror(a->patch_script_file))
7081 795b88cb 2023-07-10 thomas return got_error_from_errno("getline");
7082 795b88cb 2023-07-10 thomas return NULL;
7084 795b88cb 2023-07-10 thomas nl = strchr(line, '\n');
7086 795b88cb 2023-07-10 thomas *nl = '\0';
7087 795b88cb 2023-07-10 thomas if (strcmp(line, "y") == 0) {
7088 795b88cb 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_YES;
7089 795b88cb 2023-07-10 thomas printf("y\n");
7090 795b88cb 2023-07-10 thomas } else if (strcmp(line, "n") == 0) {
7091 795b88cb 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_NO;
7092 795b88cb 2023-07-10 thomas printf("n\n");
7093 795b88cb 2023-07-10 thomas } else if (strcmp(line, "q") == 0 &&
7094 795b88cb 2023-07-10 thomas status == GOT_STATUS_MODIFY) {
7095 795b88cb 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_QUIT;
7096 795b88cb 2023-07-10 thomas printf("q\n");
7098 795b88cb 2023-07-10 thomas printf("invalid response '%s'\n", line);
7099 795b88cb 2023-07-10 thomas free(line);
7100 795b88cb 2023-07-10 thomas return NULL;
7103 795b88cb 2023-07-10 thomas while (resp != 'y' && resp != 'n' && resp != 'q') {
7104 795b88cb 2023-07-10 thomas err = show_change(status, path, patch_file, n, nchanges,
7105 795b88cb 2023-07-10 thomas a->action);
7106 795b88cb 2023-07-10 thomas if (err)
7107 795b88cb 2023-07-10 thomas return err;
7108 795b88cb 2023-07-10 thomas resp = getchar();
7109 795b88cb 2023-07-10 thomas if (resp == '\n')
7110 795b88cb 2023-07-10 thomas resp = getchar();
7111 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_MODIFY) {
7112 795b88cb 2023-07-10 thomas if (resp != 'y' && resp != 'n' && resp != 'q') {
7113 795b88cb 2023-07-10 thomas printf("invalid response '%c'\n", resp);
7114 795b88cb 2023-07-10 thomas resp = ' ';
7116 795b88cb 2023-07-10 thomas } else if (resp != 'y' && resp != 'n') {
7117 795b88cb 2023-07-10 thomas printf("invalid response '%c'\n", resp);
7118 795b88cb 2023-07-10 thomas resp = ' ';
7122 795b88cb 2023-07-10 thomas if (resp == 'y')
7123 795b88cb 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_YES;
7124 795b88cb 2023-07-10 thomas else if (resp == 'n')
7125 795b88cb 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_NO;
7126 795b88cb 2023-07-10 thomas else if (resp == 'q' && status == GOT_STATUS_MODIFY)
7127 795b88cb 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_QUIT;
7129 795b88cb 2023-07-10 thomas return NULL;
7132 795b88cb 2023-07-10 thomas struct wt_commitable_path_arg {
7133 795b88cb 2023-07-10 thomas struct got_pathlist_head *commit_paths;
7134 795b88cb 2023-07-10 thomas int *has_changes;
7138 795b88cb 2023-07-10 thomas * Shortcut work tree status callback to determine if the set of paths scanned
7139 795b88cb 2023-07-10 thomas * has at least one versioned path that is being modified and, if not NULL, is
7140 795b88cb 2023-07-10 thomas * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
7141 795b88cb 2023-07-10 thomas * soon as a path is passed with a status that satisfies this criteria.
7143 795b88cb 2023-07-10 thomas static const struct got_error *
7144 795b88cb 2023-07-10 thomas worktree_has_commitable_path(void *arg, unsigned char status,
7145 795b88cb 2023-07-10 thomas unsigned char staged_status, const char *path,
7146 795b88cb 2023-07-10 thomas struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7147 795b88cb 2023-07-10 thomas struct got_object_id *commit_id, int dirfd, const char *de_name)
7149 795b88cb 2023-07-10 thomas struct wt_commitable_path_arg *a = arg;
7151 795b88cb 2023-07-10 thomas if (status == staged_status && (status == GOT_STATUS_DELETE))
7152 795b88cb 2023-07-10 thomas status = GOT_STATUS_NO_CHANGE;
7154 795b88cb 2023-07-10 thomas if (!(status == GOT_STATUS_NO_CHANGE ||
7155 795b88cb 2023-07-10 thomas status == GOT_STATUS_UNVERSIONED) ||
7156 795b88cb 2023-07-10 thomas staged_status != GOT_STATUS_NO_CHANGE) {
7157 795b88cb 2023-07-10 thomas if (a->commit_paths != NULL) {
7158 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
7160 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, a->commit_paths, entry) {
7161 795b88cb 2023-07-10 thomas if (strncmp(path, pe->path,
7162 795b88cb 2023-07-10 thomas pe->path_len) == 0) {
7163 795b88cb 2023-07-10 thomas *a->has_changes = 1;
7168 795b88cb 2023-07-10 thomas *a->has_changes = 1;
7170 795b88cb 2023-07-10 thomas if (*a->has_changes)
7171 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_FILE_MODIFIED);
7174 795b88cb 2023-07-10 thomas return NULL;
7178 795b88cb 2023-07-10 thomas * Check that the changeset of the commit identified by id is
7179 795b88cb 2023-07-10 thomas * comprised of at least one modified path that is being committed.
7181 795b88cb 2023-07-10 thomas static const struct got_error *
7182 795b88cb 2023-07-10 thomas commit_path_changed_in_worktree(struct wt_commitable_path_arg *wcpa,
7183 795b88cb 2023-07-10 thomas struct got_object_id *id, struct got_worktree *worktree,
7184 795b88cb 2023-07-10 thomas struct got_repository *repo)
7186 795b88cb 2023-07-10 thomas const struct got_error *err;
7187 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
7188 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL, *pcommit = NULL;
7189 795b88cb 2023-07-10 thomas struct got_tree_object *tree = NULL, *ptree = NULL;
7190 795b88cb 2023-07-10 thomas struct got_object_qid *pid;
7192 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
7194 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, id);
7195 795b88cb 2023-07-10 thomas if (err)
7196 795b88cb 2023-07-10 thomas goto done;
7198 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&tree, repo,
7199 795b88cb 2023-07-10 thomas got_object_commit_get_tree_id(commit));
7200 795b88cb 2023-07-10 thomas if (err)
7201 795b88cb 2023-07-10 thomas goto done;
7203 795b88cb 2023-07-10 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7204 795b88cb 2023-07-10 thomas if (pid != NULL) {
7205 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&pcommit, repo, &pid->id);
7206 795b88cb 2023-07-10 thomas if (err)
7207 795b88cb 2023-07-10 thomas goto done;
7209 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&ptree, repo,
7210 795b88cb 2023-07-10 thomas got_object_commit_get_tree_id(pcommit));
7211 795b88cb 2023-07-10 thomas if (err)
7212 795b88cb 2023-07-10 thomas goto done;
7215 795b88cb 2023-07-10 thomas err = got_diff_tree(ptree, tree, NULL, NULL, -1, -1, "", "", repo,
7216 795b88cb 2023-07-10 thomas got_diff_tree_collect_changed_paths, &paths, 0);
7217 795b88cb 2023-07-10 thomas if (err)
7218 795b88cb 2023-07-10 thomas goto done;
7220 795b88cb 2023-07-10 thomas err = got_worktree_status(worktree, &paths, repo, 0,
7221 795b88cb 2023-07-10 thomas worktree_has_commitable_path, wcpa, check_cancelled, NULL);
7222 795b88cb 2023-07-10 thomas if (err && err->code == GOT_ERR_FILE_MODIFIED) {
7224 795b88cb 2023-07-10 thomas * At least one changed path in the referenced commit is
7225 795b88cb 2023-07-10 thomas * modified in the work tree, that's all we need to know!
7227 795b88cb 2023-07-10 thomas err = NULL;
7231 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
7232 795b88cb 2023-07-10 thomas if (commit)
7233 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
7234 795b88cb 2023-07-10 thomas if (pcommit)
7235 795b88cb 2023-07-10 thomas got_object_commit_close(pcommit);
7236 795b88cb 2023-07-10 thomas if (tree)
7237 795b88cb 2023-07-10 thomas got_object_tree_close(tree);
7238 795b88cb 2023-07-10 thomas if (ptree)
7239 795b88cb 2023-07-10 thomas got_object_tree_close(ptree);
7240 795b88cb 2023-07-10 thomas return err;
7244 795b88cb 2023-07-10 thomas * Remove any "logmsg" reference comprised entirely of paths that have
7245 795b88cb 2023-07-10 thomas * been reverted in this work tree. If any path in the logmsg ref changeset
7246 795b88cb 2023-07-10 thomas * remains in a changed state in the worktree, do not remove the reference.
7248 795b88cb 2023-07-10 thomas static const struct got_error *
7249 795b88cb 2023-07-10 thomas rm_logmsg_ref(struct got_worktree *worktree, struct got_repository *repo)
7251 795b88cb 2023-07-10 thomas const struct got_error *err;
7252 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
7253 795b88cb 2023-07-10 thomas struct got_reflist_entry *re;
7254 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
7255 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
7256 795b88cb 2023-07-10 thomas struct wt_commitable_path_arg wcpa;
7257 795b88cb 2023-07-10 thomas char *uuidstr = NULL;
7259 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
7261 795b88cb 2023-07-10 thomas err = got_worktree_get_uuid(&uuidstr, worktree);
7262 795b88cb 2023-07-10 thomas if (err)
7263 795b88cb 2023-07-10 thomas goto done;
7265 795b88cb 2023-07-10 thomas err = got_ref_list(&refs, repo, "refs/got/worktree",
7266 795b88cb 2023-07-10 thomas got_ref_cmp_by_name, repo);
7267 795b88cb 2023-07-10 thomas if (err)
7268 795b88cb 2023-07-10 thomas goto done;
7270 795b88cb 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
7271 795b88cb 2023-07-10 thomas const char *refname;
7272 795b88cb 2023-07-10 thomas int has_changes = 0;
7274 795b88cb 2023-07-10 thomas refname = got_ref_get_name(re->ref);
7276 795b88cb 2023-07-10 thomas if (!strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
7277 795b88cb 2023-07-10 thomas GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN))
7278 795b88cb 2023-07-10 thomas refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
7279 795b88cb 2023-07-10 thomas else if (!strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
7280 795b88cb 2023-07-10 thomas GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN))
7281 795b88cb 2023-07-10 thomas refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
7283 795b88cb 2023-07-10 thomas continue;
7285 795b88cb 2023-07-10 thomas if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
7286 795b88cb 2023-07-10 thomas refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
7288 795b88cb 2023-07-10 thomas continue;
7290 795b88cb 2023-07-10 thomas err = got_repo_match_object_id(&commit_id, NULL, refname,
7291 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
7292 795b88cb 2023-07-10 thomas if (err)
7293 795b88cb 2023-07-10 thomas goto done;
7295 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
7296 795b88cb 2023-07-10 thomas if (err)
7297 795b88cb 2023-07-10 thomas goto done;
7299 795b88cb 2023-07-10 thomas wcpa.commit_paths = NULL;
7300 795b88cb 2023-07-10 thomas wcpa.has_changes = &has_changes;
7302 795b88cb 2023-07-10 thomas err = commit_path_changed_in_worktree(&wcpa, commit_id,
7303 795b88cb 2023-07-10 thomas worktree, repo);
7304 795b88cb 2023-07-10 thomas if (err)
7305 795b88cb 2023-07-10 thomas goto done;
7307 795b88cb 2023-07-10 thomas if (!has_changes) {
7308 795b88cb 2023-07-10 thomas err = got_ref_delete(re->ref, repo);
7309 795b88cb 2023-07-10 thomas if (err)
7310 795b88cb 2023-07-10 thomas goto done;
7313 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
7314 795b88cb 2023-07-10 thomas commit = NULL;
7315 795b88cb 2023-07-10 thomas free(commit_id);
7316 795b88cb 2023-07-10 thomas commit_id = NULL;
7320 795b88cb 2023-07-10 thomas free(uuidstr);
7321 795b88cb 2023-07-10 thomas free(commit_id);
7322 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
7323 795b88cb 2023-07-10 thomas if (commit)
7324 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
7325 795b88cb 2023-07-10 thomas return err;
7328 795b88cb 2023-07-10 thomas static const struct got_error *
7329 795b88cb 2023-07-10 thomas cmd_revert(int argc, char *argv[])
7331 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
7332 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
7333 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
7334 795b88cb 2023-07-10 thomas char *cwd = NULL, *path = NULL;
7335 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
7336 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
7337 795b88cb 2023-07-10 thomas int ch, can_recurse = 0, pflag = 0;
7338 795b88cb 2023-07-10 thomas FILE *patch_script_file = NULL;
7339 795b88cb 2023-07-10 thomas const char *patch_script_path = NULL;
7340 795b88cb 2023-07-10 thomas struct choose_patch_arg cpa;
7341 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
7343 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
7345 795b88cb 2023-07-10 thomas #ifndef PROFILE
7346 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7347 795b88cb 2023-07-10 thomas "unveil", NULL) == -1)
7348 795b88cb 2023-07-10 thomas err(1, "pledge");
7351 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "F:pR")) != -1) {
7352 795b88cb 2023-07-10 thomas switch (ch) {
7353 795b88cb 2023-07-10 thomas case 'F':
7354 795b88cb 2023-07-10 thomas patch_script_path = optarg;
7356 795b88cb 2023-07-10 thomas case 'p':
7357 795b88cb 2023-07-10 thomas pflag = 1;
7359 795b88cb 2023-07-10 thomas case 'R':
7360 795b88cb 2023-07-10 thomas can_recurse = 1;
7362 795b88cb 2023-07-10 thomas default:
7363 795b88cb 2023-07-10 thomas usage_revert();
7364 795b88cb 2023-07-10 thomas /* NOTREACHED */
7368 795b88cb 2023-07-10 thomas argc -= optind;
7369 795b88cb 2023-07-10 thomas argv += optind;
7371 795b88cb 2023-07-10 thomas if (argc < 1)
7372 795b88cb 2023-07-10 thomas usage_revert();
7373 795b88cb 2023-07-10 thomas if (patch_script_path && !pflag)
7374 795b88cb 2023-07-10 thomas errx(1, "-F option can only be used together with -p option");
7376 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
7377 795b88cb 2023-07-10 thomas if (cwd == NULL) {
7378 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
7379 795b88cb 2023-07-10 thomas goto done;
7382 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
7383 795b88cb 2023-07-10 thomas if (error != NULL)
7384 795b88cb 2023-07-10 thomas goto done;
7386 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
7387 795b88cb 2023-07-10 thomas if (error) {
7388 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
7389 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error, "revert", cwd);
7390 795b88cb 2023-07-10 thomas goto done;
7393 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7394 795b88cb 2023-07-10 thomas NULL, pack_fds);
7395 795b88cb 2023-07-10 thomas if (error != NULL)
7396 795b88cb 2023-07-10 thomas goto done;
7398 795b88cb 2023-07-10 thomas if (patch_script_path) {
7399 795b88cb 2023-07-10 thomas patch_script_file = fopen(patch_script_path, "re");
7400 795b88cb 2023-07-10 thomas if (patch_script_file == NULL) {
7401 795b88cb 2023-07-10 thomas error = got_error_from_errno2("fopen",
7402 795b88cb 2023-07-10 thomas patch_script_path);
7403 795b88cb 2023-07-10 thomas goto done;
7408 795b88cb 2023-07-10 thomas * XXX "c" perm needed on repo dir to delete merge references.
7410 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
7411 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree));
7412 795b88cb 2023-07-10 thomas if (error)
7413 795b88cb 2023-07-10 thomas goto done;
7415 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7416 795b88cb 2023-07-10 thomas if (error)
7417 795b88cb 2023-07-10 thomas goto done;
7419 795b88cb 2023-07-10 thomas if (!can_recurse) {
7420 795b88cb 2023-07-10 thomas char *ondisk_path;
7421 795b88cb 2023-07-10 thomas struct stat sb;
7422 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &paths, entry) {
7423 795b88cb 2023-07-10 thomas if (asprintf(&ondisk_path, "%s/%s",
7424 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree),
7425 795b88cb 2023-07-10 thomas pe->path) == -1) {
7426 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
7427 795b88cb 2023-07-10 thomas goto done;
7429 795b88cb 2023-07-10 thomas if (lstat(ondisk_path, &sb) == -1) {
7430 795b88cb 2023-07-10 thomas if (errno == ENOENT) {
7431 795b88cb 2023-07-10 thomas free(ondisk_path);
7432 795b88cb 2023-07-10 thomas continue;
7434 795b88cb 2023-07-10 thomas error = got_error_from_errno2("lstat",
7435 795b88cb 2023-07-10 thomas ondisk_path);
7436 795b88cb 2023-07-10 thomas free(ondisk_path);
7437 795b88cb 2023-07-10 thomas goto done;
7439 795b88cb 2023-07-10 thomas free(ondisk_path);
7440 795b88cb 2023-07-10 thomas if (S_ISDIR(sb.st_mode)) {
7441 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_BAD_PATH,
7442 795b88cb 2023-07-10 thomas "reverting directories requires -R option");
7443 795b88cb 2023-07-10 thomas goto done;
7448 795b88cb 2023-07-10 thomas cpa.patch_script_file = patch_script_file;
7449 795b88cb 2023-07-10 thomas cpa.action = "revert";
7450 795b88cb 2023-07-10 thomas error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7451 795b88cb 2023-07-10 thomas pflag ? choose_patch : NULL, &cpa, repo);
7453 795b88cb 2023-07-10 thomas error = rm_logmsg_ref(worktree, repo);
7455 795b88cb 2023-07-10 thomas if (patch_script_file && fclose(patch_script_file) == EOF &&
7456 795b88cb 2023-07-10 thomas error == NULL)
7457 795b88cb 2023-07-10 thomas error = got_error_from_errno2("fclose", patch_script_path);
7458 795b88cb 2023-07-10 thomas if (repo) {
7459 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
7460 795b88cb 2023-07-10 thomas if (error == NULL)
7461 795b88cb 2023-07-10 thomas error = close_err;
7463 795b88cb 2023-07-10 thomas if (worktree)
7464 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
7465 795b88cb 2023-07-10 thomas if (pack_fds) {
7466 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
7467 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
7468 795b88cb 2023-07-10 thomas if (error == NULL)
7469 795b88cb 2023-07-10 thomas error = pack_err;
7471 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7472 795b88cb 2023-07-10 thomas free(path);
7473 795b88cb 2023-07-10 thomas free(cwd);
7474 795b88cb 2023-07-10 thomas return error;
7477 795b88cb 2023-07-10 thomas __dead static void
7478 795b88cb 2023-07-10 thomas usage_commit(void)
7480 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s commit [-CNnS] [-A author] [-F path] "
7481 795b88cb 2023-07-10 thomas "[-m message] [path ...]\n", getprogname());
7482 795b88cb 2023-07-10 thomas exit(1);
7485 795b88cb 2023-07-10 thomas struct collect_commit_logmsg_arg {
7486 795b88cb 2023-07-10 thomas const char *cmdline_log;
7487 795b88cb 2023-07-10 thomas const char *prepared_log;
7488 795b88cb 2023-07-10 thomas const char *merged_log;
7489 795b88cb 2023-07-10 thomas int non_interactive;
7490 795b88cb 2023-07-10 thomas const char *editor;
7491 795b88cb 2023-07-10 thomas const char *worktree_path;
7492 795b88cb 2023-07-10 thomas const char *repo_path;
7493 795b88cb 2023-07-10 thomas const char *dial_proto;
7494 795b88cb 2023-07-10 thomas char *logmsg_path;
7497 795b88cb 2023-07-10 thomas static const struct got_error *
7498 795b88cb 2023-07-10 thomas read_prepared_logmsg(char **logmsg, const char *path)
7500 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
7501 795b88cb 2023-07-10 thomas FILE *f = NULL;
7502 795b88cb 2023-07-10 thomas struct stat sb;
7503 795b88cb 2023-07-10 thomas size_t r;
7505 795b88cb 2023-07-10 thomas *logmsg = NULL;
7506 795b88cb 2023-07-10 thomas memset(&sb, 0, sizeof(sb));
7508 795b88cb 2023-07-10 thomas f = fopen(path, "re");
7509 795b88cb 2023-07-10 thomas if (f == NULL)
7510 795b88cb 2023-07-10 thomas return got_error_from_errno2("fopen", path);
7512 795b88cb 2023-07-10 thomas if (fstat(fileno(f), &sb) == -1) {
7513 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fstat", path);
7514 795b88cb 2023-07-10 thomas goto done;
7516 795b88cb 2023-07-10 thomas if (sb.st_size == 0) {
7517 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7518 795b88cb 2023-07-10 thomas goto done;
7521 795b88cb 2023-07-10 thomas *logmsg = malloc(sb.st_size + 1);
7522 795b88cb 2023-07-10 thomas if (*logmsg == NULL) {
7523 795b88cb 2023-07-10 thomas err = got_error_from_errno("malloc");
7524 795b88cb 2023-07-10 thomas goto done;
7527 795b88cb 2023-07-10 thomas r = fread(*logmsg, 1, sb.st_size, f);
7528 795b88cb 2023-07-10 thomas if (r != sb.st_size) {
7529 795b88cb 2023-07-10 thomas if (ferror(f))
7530 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fread", path);
7532 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_IO);
7533 795b88cb 2023-07-10 thomas goto done;
7535 795b88cb 2023-07-10 thomas (*logmsg)[sb.st_size] = '\0';
7537 795b88cb 2023-07-10 thomas if (fclose(f) == EOF && err == NULL)
7538 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fclose", path);
7539 795b88cb 2023-07-10 thomas if (err) {
7540 795b88cb 2023-07-10 thomas free(*logmsg);
7541 795b88cb 2023-07-10 thomas *logmsg = NULL;
7543 795b88cb 2023-07-10 thomas return err;
7546 795b88cb 2023-07-10 thomas static const struct got_error *
7547 795b88cb 2023-07-10 thomas collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
7548 795b88cb 2023-07-10 thomas const char *diff_path, char **logmsg, void *arg)
7550 795b88cb 2023-07-10 thomas char *initial_content = NULL;
7551 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
7552 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
7553 795b88cb 2023-07-10 thomas char *template = NULL;
7554 795b88cb 2023-07-10 thomas char *prepared_msg = NULL, *merged_msg = NULL;
7555 795b88cb 2023-07-10 thomas struct collect_commit_logmsg_arg *a = arg;
7556 795b88cb 2023-07-10 thomas int initial_content_len;
7557 795b88cb 2023-07-10 thomas int fd = -1;
7558 795b88cb 2023-07-10 thomas size_t len;
7560 795b88cb 2023-07-10 thomas /* if a message was specified on the command line, just use it */
7561 795b88cb 2023-07-10 thomas if (a->cmdline_log != NULL && *a->cmdline_log != '\0') {
7562 795b88cb 2023-07-10 thomas len = strlen(a->cmdline_log) + 1;
7563 795b88cb 2023-07-10 thomas *logmsg = malloc(len + 1);
7564 795b88cb 2023-07-10 thomas if (*logmsg == NULL)
7565 795b88cb 2023-07-10 thomas return got_error_from_errno("malloc");
7566 795b88cb 2023-07-10 thomas strlcpy(*logmsg, a->cmdline_log, len);
7567 795b88cb 2023-07-10 thomas return NULL;
7568 795b88cb 2023-07-10 thomas } else if (a->prepared_log != NULL && a->non_interactive)
7569 795b88cb 2023-07-10 thomas return read_prepared_logmsg(logmsg, a->prepared_log);
7571 795b88cb 2023-07-10 thomas if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7572 795b88cb 2023-07-10 thomas return got_error_from_errno("asprintf");
7574 795b88cb 2023-07-10 thomas err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
7575 795b88cb 2023-07-10 thomas if (err)
7576 795b88cb 2023-07-10 thomas goto done;
7578 795b88cb 2023-07-10 thomas if (a->prepared_log) {
7579 795b88cb 2023-07-10 thomas err = read_prepared_logmsg(&prepared_msg, a->prepared_log);
7580 795b88cb 2023-07-10 thomas if (err)
7581 795b88cb 2023-07-10 thomas goto done;
7582 795b88cb 2023-07-10 thomas } else if (a->merged_log) {
7583 795b88cb 2023-07-10 thomas err = read_prepared_logmsg(&merged_msg, a->merged_log);
7584 795b88cb 2023-07-10 thomas if (err)
7585 795b88cb 2023-07-10 thomas goto done;
7588 795b88cb 2023-07-10 thomas initial_content_len = asprintf(&initial_content,
7589 795b88cb 2023-07-10 thomas "%s%s\n# changes to be committed:\n",
7590 795b88cb 2023-07-10 thomas prepared_msg ? prepared_msg : "",
7591 795b88cb 2023-07-10 thomas merged_msg ? merged_msg : "");
7592 795b88cb 2023-07-10 thomas if (initial_content_len == -1) {
7593 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
7594 795b88cb 2023-07-10 thomas goto done;
7597 795b88cb 2023-07-10 thomas if (write(fd, initial_content, initial_content_len) == -1) {
7598 795b88cb 2023-07-10 thomas err = got_error_from_errno2("write", a->logmsg_path);
7599 795b88cb 2023-07-10 thomas goto done;
7602 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, commitable_paths, entry) {
7603 795b88cb 2023-07-10 thomas struct got_commitable *ct = pe->data;
7604 795b88cb 2023-07-10 thomas dprintf(fd, "# %c %s\n",
7605 795b88cb 2023-07-10 thomas got_commitable_get_status(ct),
7606 795b88cb 2023-07-10 thomas got_commitable_get_path(ct));
7609 795b88cb 2023-07-10 thomas if (diff_path) {
7610 795b88cb 2023-07-10 thomas dprintf(fd, "# detailed changes can be viewed in %s\n",
7611 795b88cb 2023-07-10 thomas diff_path);
7614 795b88cb 2023-07-10 thomas if (close(fd) == -1) {
7615 795b88cb 2023-07-10 thomas err = got_error_from_errno2("close", a->logmsg_path);
7616 795b88cb 2023-07-10 thomas goto done;
7618 795b88cb 2023-07-10 thomas fd = -1;
7620 795b88cb 2023-07-10 thomas err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7621 795b88cb 2023-07-10 thomas initial_content_len, a->prepared_log ? 0 : 1);
7623 795b88cb 2023-07-10 thomas free(initial_content);
7624 795b88cb 2023-07-10 thomas free(template);
7625 795b88cb 2023-07-10 thomas free(prepared_msg);
7626 795b88cb 2023-07-10 thomas free(merged_msg);
7628 795b88cb 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
7629 795b88cb 2023-07-10 thomas err = got_error_from_errno2("close", a->logmsg_path);
7631 795b88cb 2023-07-10 thomas /* Editor is done; we can now apply unveil(2) */
7632 795b88cb 2023-07-10 thomas if (err == NULL)
7633 795b88cb 2023-07-10 thomas err = got_dial_apply_unveil(a->dial_proto);
7634 795b88cb 2023-07-10 thomas if (err == NULL)
7635 795b88cb 2023-07-10 thomas err = apply_unveil(a->repo_path, 0, a->worktree_path);
7636 795b88cb 2023-07-10 thomas if (err) {
7637 795b88cb 2023-07-10 thomas free(*logmsg);
7638 795b88cb 2023-07-10 thomas *logmsg = NULL;
7640 795b88cb 2023-07-10 thomas return err;
7643 795b88cb 2023-07-10 thomas static const struct got_error *
7644 795b88cb 2023-07-10 thomas cat_logmsg(FILE *f, struct got_commit_object *commit, const char *idstr,
7645 795b88cb 2023-07-10 thomas const char *type, int has_content)
7647 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
7648 795b88cb 2023-07-10 thomas char *logmsg = NULL;
7650 795b88cb 2023-07-10 thomas err = got_object_commit_get_logmsg(&logmsg, commit);
7651 795b88cb 2023-07-10 thomas if (err)
7652 795b88cb 2023-07-10 thomas return err;
7654 795b88cb 2023-07-10 thomas if (fprintf(f, "%s# log message of %s commit %s:%s",
7655 795b88cb 2023-07-10 thomas has_content ? "\n" : "", type, idstr, logmsg) < 0)
7656 795b88cb 2023-07-10 thomas err = got_ferror(f, GOT_ERR_IO);
7658 795b88cb 2023-07-10 thomas free(logmsg);
7659 795b88cb 2023-07-10 thomas return err;
7663 795b88cb 2023-07-10 thomas * Lookup "logmsg" references of backed-out and cherrypicked commits
7664 795b88cb 2023-07-10 thomas * belonging to the current work tree. If found, and the worktree has
7665 795b88cb 2023-07-10 thomas * at least one modified file that was changed in the referenced commit,
7666 795b88cb 2023-07-10 thomas * add its log message to a new temporary file at *logmsg_path.
7667 795b88cb 2023-07-10 thomas * Add all refs found to matched_refs to be scheduled for removal on
7668 795b88cb 2023-07-10 thomas * successful commit.
7670 795b88cb 2023-07-10 thomas static const struct got_error *
7671 795b88cb 2023-07-10 thomas lookup_logmsg_ref(char **logmsg_path, struct got_pathlist_head *paths,
7672 795b88cb 2023-07-10 thomas struct got_reflist_head *matched_refs, struct got_worktree *worktree,
7673 795b88cb 2023-07-10 thomas struct got_repository *repo)
7675 795b88cb 2023-07-10 thomas const struct got_error *err;
7676 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
7677 795b88cb 2023-07-10 thomas struct got_object_id *id = NULL;
7678 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
7679 795b88cb 2023-07-10 thomas struct got_reflist_entry *re, *re_match;
7680 795b88cb 2023-07-10 thomas FILE *f = NULL;
7681 795b88cb 2023-07-10 thomas char *uuidstr = NULL;
7682 795b88cb 2023-07-10 thomas int added_logmsg = 0;
7684 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
7686 795b88cb 2023-07-10 thomas *logmsg_path = NULL;
7688 795b88cb 2023-07-10 thomas err = got_worktree_get_uuid(&uuidstr, worktree);
7689 795b88cb 2023-07-10 thomas if (err)
7690 795b88cb 2023-07-10 thomas goto done;
7692 795b88cb 2023-07-10 thomas err = got_ref_list(&refs, repo, "refs/got/worktree",
7693 795b88cb 2023-07-10 thomas got_ref_cmp_by_name, repo);
7694 795b88cb 2023-07-10 thomas if (err)
7695 795b88cb 2023-07-10 thomas goto done;
7697 795b88cb 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
7698 795b88cb 2023-07-10 thomas const char *refname, *type;
7699 795b88cb 2023-07-10 thomas struct wt_commitable_path_arg wcpa;
7700 795b88cb 2023-07-10 thomas int add_logmsg = 0;
7702 795b88cb 2023-07-10 thomas refname = got_ref_get_name(re->ref);
7704 795b88cb 2023-07-10 thomas if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
7705 795b88cb 2023-07-10 thomas GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
7706 795b88cb 2023-07-10 thomas refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
7707 795b88cb 2023-07-10 thomas type = "cherrypicked";
7708 795b88cb 2023-07-10 thomas } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
7709 795b88cb 2023-07-10 thomas GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
7710 795b88cb 2023-07-10 thomas refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
7711 795b88cb 2023-07-10 thomas type = "backed-out";
7713 795b88cb 2023-07-10 thomas continue;
7715 795b88cb 2023-07-10 thomas if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
7716 795b88cb 2023-07-10 thomas refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
7718 795b88cb 2023-07-10 thomas continue;
7720 795b88cb 2023-07-10 thomas err = got_repo_match_object_id(&id, NULL, refname,
7721 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
7722 795b88cb 2023-07-10 thomas if (err)
7723 795b88cb 2023-07-10 thomas goto done;
7725 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, id);
7726 795b88cb 2023-07-10 thomas if (err)
7727 795b88cb 2023-07-10 thomas goto done;
7729 795b88cb 2023-07-10 thomas wcpa.commit_paths = paths;
7730 795b88cb 2023-07-10 thomas wcpa.has_changes = &add_logmsg;
7732 795b88cb 2023-07-10 thomas err = commit_path_changed_in_worktree(&wcpa, id,
7733 795b88cb 2023-07-10 thomas worktree, repo);
7734 795b88cb 2023-07-10 thomas if (err)
7735 795b88cb 2023-07-10 thomas goto done;
7737 795b88cb 2023-07-10 thomas if (add_logmsg) {
7738 795b88cb 2023-07-10 thomas if (f == NULL) {
7739 795b88cb 2023-07-10 thomas err = got_opentemp_named(logmsg_path, &f,
7740 795b88cb 2023-07-10 thomas "got-commit-logmsg", "");
7741 795b88cb 2023-07-10 thomas if (err)
7742 795b88cb 2023-07-10 thomas goto done;
7744 795b88cb 2023-07-10 thomas err = cat_logmsg(f, commit, refname, type,
7745 795b88cb 2023-07-10 thomas added_logmsg);
7746 795b88cb 2023-07-10 thomas if (err)
7747 795b88cb 2023-07-10 thomas goto done;
7748 795b88cb 2023-07-10 thomas if (!added_logmsg)
7749 795b88cb 2023-07-10 thomas ++added_logmsg;
7751 795b88cb 2023-07-10 thomas err = got_reflist_entry_dup(&re_match, re);
7752 795b88cb 2023-07-10 thomas if (err)
7753 795b88cb 2023-07-10 thomas goto done;
7754 795b88cb 2023-07-10 thomas TAILQ_INSERT_HEAD(matched_refs, re_match, entry);
7757 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
7758 795b88cb 2023-07-10 thomas commit = NULL;
7759 795b88cb 2023-07-10 thomas free(id);
7760 795b88cb 2023-07-10 thomas id = NULL;
7764 795b88cb 2023-07-10 thomas free(id);
7765 795b88cb 2023-07-10 thomas free(uuidstr);
7766 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
7767 795b88cb 2023-07-10 thomas if (commit)
7768 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
7769 795b88cb 2023-07-10 thomas if (f && fclose(f) == EOF && err == NULL)
7770 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
7771 795b88cb 2023-07-10 thomas if (!added_logmsg) {
7772 795b88cb 2023-07-10 thomas if (*logmsg_path && unlink(*logmsg_path) != 0 && err == NULL)
7773 795b88cb 2023-07-10 thomas err = got_error_from_errno2("unlink", *logmsg_path);
7774 795b88cb 2023-07-10 thomas *logmsg_path = NULL;
7776 795b88cb 2023-07-10 thomas return err;
7779 795b88cb 2023-07-10 thomas static const struct got_error *
7780 795b88cb 2023-07-10 thomas cmd_commit(int argc, char *argv[])
7782 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
7783 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
7784 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
7785 795b88cb 2023-07-10 thomas char *cwd = NULL, *id_str = NULL;
7786 795b88cb 2023-07-10 thomas struct got_object_id *id = NULL;
7787 795b88cb 2023-07-10 thomas const char *logmsg = NULL;
7788 795b88cb 2023-07-10 thomas char *prepared_logmsg = NULL, *merged_logmsg = NULL;
7789 795b88cb 2023-07-10 thomas struct collect_commit_logmsg_arg cl_arg;
7790 795b88cb 2023-07-10 thomas const char *author = NULL;
7791 795b88cb 2023-07-10 thomas char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
7792 795b88cb 2023-07-10 thomas int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7793 795b88cb 2023-07-10 thomas int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
7794 795b88cb 2023-07-10 thomas int show_diff = 1, commit_conflicts = 0;
7795 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
7796 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
7797 795b88cb 2023-07-10 thomas struct got_reflist_entry *re;
7798 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
7799 795b88cb 2023-07-10 thomas const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
7800 795b88cb 2023-07-10 thomas const struct got_remote_repo *remotes, *remote = NULL;
7801 795b88cb 2023-07-10 thomas int nremotes;
7802 795b88cb 2023-07-10 thomas char *proto = NULL, *host = NULL, *port = NULL;
7803 795b88cb 2023-07-10 thomas char *repo_name = NULL, *server_path = NULL;
7804 795b88cb 2023-07-10 thomas const char *remote_name;
7805 795b88cb 2023-07-10 thomas int verbosity = 0;
7808 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
7809 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
7810 795b88cb 2023-07-10 thomas cl_arg.logmsg_path = NULL;
7812 795b88cb 2023-07-10 thomas #ifndef PROFILE
7813 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7814 795b88cb 2023-07-10 thomas "unveil", NULL) == -1)
7815 795b88cb 2023-07-10 thomas err(1, "pledge");
7818 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "A:CF:m:NnS")) != -1) {
7819 795b88cb 2023-07-10 thomas switch (ch) {
7820 795b88cb 2023-07-10 thomas case 'A':
7821 795b88cb 2023-07-10 thomas author = optarg;
7822 795b88cb 2023-07-10 thomas error = valid_author(author);
7823 795b88cb 2023-07-10 thomas if (error)
7824 795b88cb 2023-07-10 thomas return error;
7826 795b88cb 2023-07-10 thomas case 'C':
7827 795b88cb 2023-07-10 thomas commit_conflicts = 1;
7829 795b88cb 2023-07-10 thomas case 'F':
7830 795b88cb 2023-07-10 thomas if (logmsg != NULL)
7831 795b88cb 2023-07-10 thomas option_conflict('F', 'm');
7832 795b88cb 2023-07-10 thomas prepared_logmsg = realpath(optarg, NULL);
7833 795b88cb 2023-07-10 thomas if (prepared_logmsg == NULL)
7834 795b88cb 2023-07-10 thomas return got_error_from_errno2("realpath",
7835 795b88cb 2023-07-10 thomas optarg);
7837 795b88cb 2023-07-10 thomas case 'm':
7838 795b88cb 2023-07-10 thomas if (prepared_logmsg)
7839 795b88cb 2023-07-10 thomas option_conflict('m', 'F');
7840 795b88cb 2023-07-10 thomas logmsg = optarg;
7842 795b88cb 2023-07-10 thomas case 'N':
7843 795b88cb 2023-07-10 thomas non_interactive = 1;
7845 795b88cb 2023-07-10 thomas case 'n':
7846 795b88cb 2023-07-10 thomas show_diff = 0;
7848 795b88cb 2023-07-10 thomas case 'S':
7849 795b88cb 2023-07-10 thomas allow_bad_symlinks = 1;
7851 795b88cb 2023-07-10 thomas default:
7852 795b88cb 2023-07-10 thomas usage_commit();
7853 795b88cb 2023-07-10 thomas /* NOTREACHED */
7857 795b88cb 2023-07-10 thomas argc -= optind;
7858 795b88cb 2023-07-10 thomas argv += optind;
7860 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
7861 795b88cb 2023-07-10 thomas if (cwd == NULL) {
7862 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
7863 795b88cb 2023-07-10 thomas goto done;
7866 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
7867 795b88cb 2023-07-10 thomas if (error != NULL)
7868 795b88cb 2023-07-10 thomas goto done;
7870 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
7871 795b88cb 2023-07-10 thomas if (error) {
7872 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
7873 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error, "commit", cwd);
7874 795b88cb 2023-07-10 thomas goto done;
7877 795b88cb 2023-07-10 thomas error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7878 795b88cb 2023-07-10 thomas if (error)
7879 795b88cb 2023-07-10 thomas goto done;
7880 795b88cb 2023-07-10 thomas if (rebase_in_progress) {
7881 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_REBASING);
7882 795b88cb 2023-07-10 thomas goto done;
7885 795b88cb 2023-07-10 thomas error = got_worktree_histedit_in_progress(&histedit_in_progress,
7886 795b88cb 2023-07-10 thomas worktree);
7887 795b88cb 2023-07-10 thomas if (error)
7888 795b88cb 2023-07-10 thomas goto done;
7890 795b88cb 2023-07-10 thomas error = get_gitconfig_path(&gitconfig_path);
7891 795b88cb 2023-07-10 thomas if (error)
7892 795b88cb 2023-07-10 thomas goto done;
7893 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7894 795b88cb 2023-07-10 thomas gitconfig_path, pack_fds);
7895 795b88cb 2023-07-10 thomas if (error != NULL)
7896 795b88cb 2023-07-10 thomas goto done;
7898 795b88cb 2023-07-10 thomas error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
7899 795b88cb 2023-07-10 thomas if (error)
7900 795b88cb 2023-07-10 thomas goto done;
7901 795b88cb 2023-07-10 thomas if (merge_in_progress) {
7902 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_MERGE_BUSY);
7903 795b88cb 2023-07-10 thomas goto done;
7906 795b88cb 2023-07-10 thomas error = get_author(&committer, repo, worktree);
7907 795b88cb 2023-07-10 thomas if (error)
7908 795b88cb 2023-07-10 thomas goto done;
7910 795b88cb 2023-07-10 thomas if (author == NULL)
7911 795b88cb 2023-07-10 thomas author = committer;
7913 795b88cb 2023-07-10 thomas remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
7914 795b88cb 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
7915 795b88cb 2023-07-10 thomas if (worktree_conf) {
7916 795b88cb 2023-07-10 thomas got_gotconfig_get_remotes(&nremotes, &remotes,
7917 795b88cb 2023-07-10 thomas worktree_conf);
7918 795b88cb 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
7919 795b88cb 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
7920 795b88cb 2023-07-10 thomas remote = &remotes[i];
7925 795b88cb 2023-07-10 thomas if (remote == NULL) {
7926 795b88cb 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
7927 795b88cb 2023-07-10 thomas if (repo_conf) {
7928 795b88cb 2023-07-10 thomas got_gotconfig_get_remotes(&nremotes, &remotes,
7929 795b88cb 2023-07-10 thomas repo_conf);
7930 795b88cb 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
7931 795b88cb 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
7932 795b88cb 2023-07-10 thomas remote = &remotes[i];
7938 795b88cb 2023-07-10 thomas if (remote == NULL) {
7939 795b88cb 2023-07-10 thomas got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
7940 795b88cb 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
7941 795b88cb 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
7942 795b88cb 2023-07-10 thomas remote = &remotes[i];
7947 795b88cb 2023-07-10 thomas if (remote == NULL) {
7948 795b88cb 2023-07-10 thomas error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
7949 795b88cb 2023-07-10 thomas goto done;
7952 795b88cb 2023-07-10 thomas error = got_dial_parse_uri(&proto, &host, &port, &server_path,
7953 795b88cb 2023-07-10 thomas &repo_name, remote->fetch_url);
7954 795b88cb 2023-07-10 thomas if (error)
7955 795b88cb 2023-07-10 thomas goto done;
7957 795b88cb 2023-07-10 thomas if (strcmp(proto, "git") == 0) {
7958 795b88cb 2023-07-10 thomas #ifndef PROFILE
7959 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7960 795b88cb 2023-07-10 thomas "sendfd dns inet unveil", NULL) == -1)
7961 795b88cb 2023-07-10 thomas err(1, "pledge");
7963 795b88cb 2023-07-10 thomas } else if (strcmp(proto, "git+ssh") == 0 ||
7964 795b88cb 2023-07-10 thomas strcmp(proto, "ssh") == 0) {
7965 795b88cb 2023-07-10 thomas #ifndef PROFILE
7966 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7967 795b88cb 2023-07-10 thomas "sendfd unveil", NULL) == -1)
7968 795b88cb 2023-07-10 thomas err(1, "pledge");
7970 795b88cb 2023-07-10 thomas } else if (strcmp(proto, "http") == 0 ||
7971 795b88cb 2023-07-10 thomas strcmp(proto, "git+http") == 0) {
7972 795b88cb 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_NOT_IMPL);
7973 795b88cb 2023-07-10 thomas goto done;
7974 795b88cb 2023-07-10 thomas } else {
7975 795b88cb 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_BAD_PROTO);
7976 795b88cb 2023-07-10 thomas goto done;
7980 795b88cb 2023-07-10 thomas /*if (verbosity >= 0) {
7981 795b88cb 2023-07-10 thomas printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
7982 795b88cb 2023-07-10 thomas remote->name, proto, host,
7983 795b88cb 2023-07-10 thomas port ? ":" : "", port ? port : "",
7984 795b88cb 2023-07-10 thomas *server_path == '/' ? "" : "/", server_path);
7988 795b88cb 2023-07-10 thomas * unveil(2) traverses exec(2); if an editor is used we have
7989 795b88cb 2023-07-10 thomas * to apply unveil after the log message has been written during
7990 795b88cb 2023-07-10 thomas * the callback.
7992 795b88cb 2023-07-10 thomas if (logmsg == NULL || strlen(logmsg) == 0)
7993 795b88cb 2023-07-10 thomas error = get_editor(&editor);
7995 795b88cb 2023-07-10 thomas error = got_dial_apply_unveil(proto);
7996 795b88cb 2023-07-10 thomas if (error)
7997 795b88cb 2023-07-10 thomas goto done;
7998 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
7999 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree));
8001 795b88cb 2023-07-10 thomas if (error)
8002 795b88cb 2023-07-10 thomas goto done;
8004 795b88cb 2023-07-10 thomas if (prepared_logmsg == NULL) {
8005 795b88cb 2023-07-10 thomas error = lookup_logmsg_ref(&merged_logmsg,
8006 795b88cb 2023-07-10 thomas argc > 0 ? &paths : NULL, &refs, worktree, repo);
8007 795b88cb 2023-07-10 thomas if (error)
8008 795b88cb 2023-07-10 thomas goto done;
8011 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8012 795b88cb 2023-07-10 thomas if (error)
8013 795b88cb 2023-07-10 thomas goto done;
8015 795b88cb 2023-07-10 thomas cl_arg.editor = editor;
8016 795b88cb 2023-07-10 thomas cl_arg.cmdline_log = logmsg;
8017 795b88cb 2023-07-10 thomas cl_arg.prepared_log = prepared_logmsg;
8018 795b88cb 2023-07-10 thomas cl_arg.merged_log = merged_logmsg;
8019 795b88cb 2023-07-10 thomas cl_arg.non_interactive = non_interactive;
8020 795b88cb 2023-07-10 thomas cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8021 795b88cb 2023-07-10 thomas cl_arg.repo_path = got_repo_get_path(repo);
8022 795b88cb 2023-07-10 thomas cl_arg.dial_proto = proto;
8023 795b88cb 2023-07-10 thomas error = got_worktree_cvg_commit(&id, worktree, &paths, author,
8024 795b88cb 2023-07-10 thomas committer, allow_bad_symlinks, show_diff, commit_conflicts,
8025 795b88cb 2023-07-10 thomas collect_commit_logmsg, &cl_arg, print_status, NULL, proto, host,
8026 795b88cb 2023-07-10 thomas port, server_path, verbosity, remote, check_cancelled, repo);
8027 795b88cb 2023-07-10 thomas if (error) {
8028 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8029 795b88cb 2023-07-10 thomas cl_arg.logmsg_path != NULL)
8030 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
8031 795b88cb 2023-07-10 thomas goto done;
8034 795b88cb 2023-07-10 thomas error = got_object_id_str(&id_str, id);
8035 795b88cb 2023-07-10 thomas if (error)
8036 795b88cb 2023-07-10 thomas goto done;
8037 795b88cb 2023-07-10 thomas printf("Created commit %s\n", id_str);
8039 795b88cb 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
8040 795b88cb 2023-07-10 thomas error = got_ref_delete(re->ref, repo);
8041 795b88cb 2023-07-10 thomas if (error)
8042 795b88cb 2023-07-10 thomas goto done;
8046 795b88cb 2023-07-10 thomas if (preserve_logmsg) {
8047 795b88cb 2023-07-10 thomas fprintf(stderr, "%s: log message preserved in %s\n",
8048 795b88cb 2023-07-10 thomas getprogname(), cl_arg.logmsg_path);
8049 795b88cb 2023-07-10 thomas } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8050 795b88cb 2023-07-10 thomas error == NULL)
8051 795b88cb 2023-07-10 thomas error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8052 795b88cb 2023-07-10 thomas free(cl_arg.logmsg_path);
8053 795b88cb 2023-07-10 thomas if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
8054 795b88cb 2023-07-10 thomas error = got_error_from_errno2("unlink", merged_logmsg);
8055 795b88cb 2023-07-10 thomas free(merged_logmsg);
8056 795b88cb 2023-07-10 thomas if (repo) {
8057 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
8058 795b88cb 2023-07-10 thomas if (error == NULL)
8059 795b88cb 2023-07-10 thomas error = close_err;
8061 795b88cb 2023-07-10 thomas if (worktree)
8062 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
8063 795b88cb 2023-07-10 thomas if (pack_fds) {
8064 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
8065 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
8066 795b88cb 2023-07-10 thomas if (error == NULL)
8067 795b88cb 2023-07-10 thomas error = pack_err;
8069 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
8070 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8071 795b88cb 2023-07-10 thomas free(cwd);
8072 795b88cb 2023-07-10 thomas free(id_str);
8073 795b88cb 2023-07-10 thomas free(gitconfig_path);
8074 795b88cb 2023-07-10 thomas free(editor);
8075 795b88cb 2023-07-10 thomas free(committer);
8076 795b88cb 2023-07-10 thomas free(prepared_logmsg);
8077 795b88cb 2023-07-10 thomas return error;
8081 795b88cb 2023-07-10 thomas * Print and if delete is set delete all ref_prefix references.
8082 795b88cb 2023-07-10 thomas * If wanted_ref is not NULL, only print or delete this reference.
8084 795b88cb 2023-07-10 thomas static const struct got_error *
8085 795b88cb 2023-07-10 thomas process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
8086 795b88cb 2023-07-10 thomas const char *wanted_ref, int delete, struct got_worktree *worktree,
8087 795b88cb 2023-07-10 thomas struct got_repository *repo)
8089 795b88cb 2023-07-10 thomas const struct got_error *err;
8090 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
8091 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
8092 795b88cb 2023-07-10 thomas struct got_reflist_entry *re;
8093 795b88cb 2023-07-10 thomas struct got_reflist_object_id_map *refs_idmap = NULL;
8094 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
8095 795b88cb 2023-07-10 thomas struct got_object_id *id = NULL;
8096 795b88cb 2023-07-10 thomas const char *header_prefix;
8097 795b88cb 2023-07-10 thomas char *uuidstr = NULL;
8098 795b88cb 2023-07-10 thomas int found = 0;
8100 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
8101 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
8103 795b88cb 2023-07-10 thomas err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
8104 795b88cb 2023-07-10 thomas if (err)
8105 795b88cb 2023-07-10 thomas goto done;
8107 795b88cb 2023-07-10 thomas err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
8108 795b88cb 2023-07-10 thomas if (err)
8109 795b88cb 2023-07-10 thomas goto done;
8111 795b88cb 2023-07-10 thomas if (worktree != NULL) {
8112 795b88cb 2023-07-10 thomas err = got_worktree_get_uuid(&uuidstr, worktree);
8113 795b88cb 2023-07-10 thomas if (err)
8114 795b88cb 2023-07-10 thomas goto done;
8117 795b88cb 2023-07-10 thomas if (wanted_ref) {
8118 795b88cb 2023-07-10 thomas if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
8119 795b88cb 2023-07-10 thomas wanted_ref += 11;
8122 795b88cb 2023-07-10 thomas if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
8123 795b88cb 2023-07-10 thomas header_prefix = "backout";
8125 795b88cb 2023-07-10 thomas header_prefix = "cherrypick";
8127 795b88cb 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
8128 795b88cb 2023-07-10 thomas const char *refname, *wt;
8130 795b88cb 2023-07-10 thomas refname = got_ref_get_name(re->ref);
8132 795b88cb 2023-07-10 thomas err = check_cancelled(NULL);
8133 795b88cb 2023-07-10 thomas if (err)
8134 795b88cb 2023-07-10 thomas goto done;
8136 795b88cb 2023-07-10 thomas if (strncmp(refname, ref_prefix, prefix_len) == 0)
8137 795b88cb 2023-07-10 thomas refname += prefix_len + 1; /* skip '-' delimiter */
8139 795b88cb 2023-07-10 thomas continue;
8141 795b88cb 2023-07-10 thomas wt = refname;
8143 795b88cb 2023-07-10 thomas if (worktree == NULL || strncmp(refname, uuidstr,
8144 795b88cb 2023-07-10 thomas GOT_WORKTREE_UUID_STRLEN) == 0)
8145 795b88cb 2023-07-10 thomas refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8147 795b88cb 2023-07-10 thomas continue;
8149 795b88cb 2023-07-10 thomas err = got_repo_match_object_id(&id, NULL, refname,
8150 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
8151 795b88cb 2023-07-10 thomas if (err)
8152 795b88cb 2023-07-10 thomas goto done;
8154 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, id);
8155 795b88cb 2023-07-10 thomas if (err)
8156 795b88cb 2023-07-10 thomas goto done;
8158 795b88cb 2023-07-10 thomas if (wanted_ref)
8159 795b88cb 2023-07-10 thomas found = strncmp(wanted_ref, refname,
8160 795b88cb 2023-07-10 thomas strlen(wanted_ref)) == 0;
8161 795b88cb 2023-07-10 thomas if (wanted_ref && !found) {
8162 795b88cb 2023-07-10 thomas struct got_reflist_head *ci_refs;
8164 795b88cb 2023-07-10 thomas ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
8167 795b88cb 2023-07-10 thomas if (ci_refs) {
8168 795b88cb 2023-07-10 thomas char *refs_str = NULL;
8169 795b88cb 2023-07-10 thomas char const *r = NULL;
8171 795b88cb 2023-07-10 thomas err = build_refs_str(&refs_str, ci_refs, id,
8172 795b88cb 2023-07-10 thomas repo, 1);
8173 795b88cb 2023-07-10 thomas if (err)
8174 795b88cb 2023-07-10 thomas goto done;
8176 795b88cb 2023-07-10 thomas r = refs_str;
8177 795b88cb 2023-07-10 thomas while (r) {
8178 795b88cb 2023-07-10 thomas if (strncmp(r, wanted_ref,
8179 795b88cb 2023-07-10 thomas strlen(wanted_ref)) == 0) {
8180 795b88cb 2023-07-10 thomas found = 1;
8183 795b88cb 2023-07-10 thomas r = strchr(r, ' ');
8187 795b88cb 2023-07-10 thomas free(refs_str);
8191 795b88cb 2023-07-10 thomas if (wanted_ref == NULL || found) {
8192 795b88cb 2023-07-10 thomas if (delete) {
8193 795b88cb 2023-07-10 thomas err = got_ref_delete(re->ref, repo);
8194 795b88cb 2023-07-10 thomas if (err)
8195 795b88cb 2023-07-10 thomas goto done;
8196 795b88cb 2023-07-10 thomas printf("Deleted: ");
8197 795b88cb 2023-07-10 thomas err = print_commit_oneline(commit, id, repo,
8198 795b88cb 2023-07-10 thomas refs_idmap);
8199 795b88cb 2023-07-10 thomas } else {
8201 795b88cb 2023-07-10 thomas * Print paths modified by commit to help
8202 795b88cb 2023-07-10 thomas * associate commits with worktree changes.
8204 795b88cb 2023-07-10 thomas err = get_changed_paths(&paths, commit,
8205 795b88cb 2023-07-10 thomas repo, NULL);
8206 795b88cb 2023-07-10 thomas if (err)
8207 795b88cb 2023-07-10 thomas goto done;
8209 795b88cb 2023-07-10 thomas err = print_commit(commit, id, repo, NULL,
8210 795b88cb 2023-07-10 thomas &paths, NULL, 0, 0, refs_idmap, NULL,
8211 795b88cb 2023-07-10 thomas header_prefix);
8212 795b88cb 2023-07-10 thomas got_pathlist_free(&paths,
8213 795b88cb 2023-07-10 thomas GOT_PATHLIST_FREE_ALL);
8215 795b88cb 2023-07-10 thomas if (worktree == NULL)
8216 795b88cb 2023-07-10 thomas printf("work tree: %.*s\n\n",
8217 795b88cb 2023-07-10 thomas GOT_WORKTREE_UUID_STRLEN, wt);
8219 795b88cb 2023-07-10 thomas if (err || found)
8220 795b88cb 2023-07-10 thomas goto done;
8223 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
8224 795b88cb 2023-07-10 thomas commit = NULL;
8225 795b88cb 2023-07-10 thomas free(id);
8226 795b88cb 2023-07-10 thomas id = NULL;
8229 795b88cb 2023-07-10 thomas if (wanted_ref != NULL && !found)
8230 795b88cb 2023-07-10 thomas err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
8233 795b88cb 2023-07-10 thomas free(id);
8234 795b88cb 2023-07-10 thomas free(uuidstr);
8235 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
8236 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
8237 795b88cb 2023-07-10 thomas if (refs_idmap)
8238 795b88cb 2023-07-10 thomas got_reflist_object_id_map_free(refs_idmap);
8239 795b88cb 2023-07-10 thomas if (commit)
8240 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
8241 795b88cb 2023-07-10 thomas return err;
8245 795b88cb 2023-07-10 thomas * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
8246 795b88cb 2023-07-10 thomas * identified by id for log messages to prepopulate the editor on commit.
8248 795b88cb 2023-07-10 thomas static const struct got_error *
8249 795b88cb 2023-07-10 thomas logmsg_ref(struct got_object_id *id, const char *prefix,
8250 795b88cb 2023-07-10 thomas struct got_worktree *worktree, struct got_repository *repo)
8252 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
8253 795b88cb 2023-07-10 thomas char *idstr, *ref = NULL, *refname = NULL;
8254 795b88cb 2023-07-10 thomas int histedit_in_progress;
8255 795b88cb 2023-07-10 thomas int rebase_in_progress, merge_in_progress;
8258 795b88cb 2023-07-10 thomas * Silenty refuse to create merge reference if any histedit, merge,
8259 795b88cb 2023-07-10 thomas * or rebase operation is in progress.
8261 795b88cb 2023-07-10 thomas err = got_worktree_histedit_in_progress(&histedit_in_progress,
8262 795b88cb 2023-07-10 thomas worktree);
8263 795b88cb 2023-07-10 thomas if (err)
8264 795b88cb 2023-07-10 thomas return err;
8265 795b88cb 2023-07-10 thomas if (histedit_in_progress)
8266 795b88cb 2023-07-10 thomas return NULL;
8268 795b88cb 2023-07-10 thomas err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8269 795b88cb 2023-07-10 thomas if (err)
8270 795b88cb 2023-07-10 thomas return err;
8271 795b88cb 2023-07-10 thomas if (rebase_in_progress)
8272 795b88cb 2023-07-10 thomas return NULL;
8274 795b88cb 2023-07-10 thomas err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
8276 795b88cb 2023-07-10 thomas if (err)
8277 795b88cb 2023-07-10 thomas return err;
8278 795b88cb 2023-07-10 thomas if (merge_in_progress)
8279 795b88cb 2023-07-10 thomas return NULL;
8281 795b88cb 2023-07-10 thomas err = got_object_id_str(&idstr, id);
8282 795b88cb 2023-07-10 thomas if (err)
8283 795b88cb 2023-07-10 thomas return err;
8285 795b88cb 2023-07-10 thomas err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
8286 795b88cb 2023-07-10 thomas if (err)
8287 795b88cb 2023-07-10 thomas goto done;
8289 795b88cb 2023-07-10 thomas if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
8290 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
8291 795b88cb 2023-07-10 thomas goto done;
8294 795b88cb 2023-07-10 thomas err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
8295 795b88cb 2023-07-10 thomas -1, repo);
8297 795b88cb 2023-07-10 thomas free(ref);
8298 795b88cb 2023-07-10 thomas free(idstr);
8299 795b88cb 2023-07-10 thomas free(refname);
8300 795b88cb 2023-07-10 thomas return err;
8303 795b88cb 2023-07-10 thomas __dead static void
8304 795b88cb 2023-07-10 thomas usage_cherrypick(void)
8306 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
8307 795b88cb 2023-07-10 thomas getprogname());
8308 795b88cb 2023-07-10 thomas exit(1);
8311 795b88cb 2023-07-10 thomas static const struct got_error *
8312 795b88cb 2023-07-10 thomas cmd_cherrypick(int argc, char *argv[])
8314 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
8315 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
8316 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
8317 795b88cb 2023-07-10 thomas char *cwd = NULL, *commit_id_str = NULL;
8318 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
8319 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
8320 795b88cb 2023-07-10 thomas struct got_object_qid *pid;
8321 795b88cb 2023-07-10 thomas int ch, list_refs = 0, remove_refs = 0;
8322 795b88cb 2023-07-10 thomas struct got_update_progress_arg upa;
8323 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
8325 795b88cb 2023-07-10 thomas #ifndef PROFILE
8326 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8327 795b88cb 2023-07-10 thomas "unveil", NULL) == -1)
8328 795b88cb 2023-07-10 thomas err(1, "pledge");
8331 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "lX")) != -1) {
8332 795b88cb 2023-07-10 thomas switch (ch) {
8333 795b88cb 2023-07-10 thomas case 'l':
8334 795b88cb 2023-07-10 thomas list_refs = 1;
8336 795b88cb 2023-07-10 thomas case 'X':
8337 795b88cb 2023-07-10 thomas remove_refs = 1;
8339 795b88cb 2023-07-10 thomas default:
8340 795b88cb 2023-07-10 thomas usage_cherrypick();
8341 795b88cb 2023-07-10 thomas /* NOTREACHED */
8345 795b88cb 2023-07-10 thomas argc -= optind;
8346 795b88cb 2023-07-10 thomas argv += optind;
8348 795b88cb 2023-07-10 thomas if (list_refs || remove_refs) {
8349 795b88cb 2023-07-10 thomas if (argc != 0 && argc != 1)
8350 795b88cb 2023-07-10 thomas usage_cherrypick();
8351 795b88cb 2023-07-10 thomas } else if (argc != 1)
8352 795b88cb 2023-07-10 thomas usage_cherrypick();
8353 795b88cb 2023-07-10 thomas if (list_refs && remove_refs)
8354 795b88cb 2023-07-10 thomas option_conflict('l', 'X');
8356 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
8357 795b88cb 2023-07-10 thomas if (cwd == NULL) {
8358 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
8359 795b88cb 2023-07-10 thomas goto done;
8362 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
8363 795b88cb 2023-07-10 thomas if (error != NULL)
8364 795b88cb 2023-07-10 thomas goto done;
8366 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
8367 795b88cb 2023-07-10 thomas if (error) {
8368 795b88cb 2023-07-10 thomas if (list_refs || remove_refs) {
8369 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_NOT_WORKTREE)
8370 795b88cb 2023-07-10 thomas goto done;
8371 795b88cb 2023-07-10 thomas } else {
8372 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
8373 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error,
8374 795b88cb 2023-07-10 thomas "cherrypick", cwd);
8375 795b88cb 2023-07-10 thomas goto done;
8379 795b88cb 2023-07-10 thomas error = got_repo_open(&repo,
8380 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_repo_path(worktree) : cwd,
8381 795b88cb 2023-07-10 thomas NULL, pack_fds);
8382 795b88cb 2023-07-10 thomas if (error != NULL)
8383 795b88cb 2023-07-10 thomas goto done;
8385 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
8386 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
8387 795b88cb 2023-07-10 thomas if (error)
8388 795b88cb 2023-07-10 thomas goto done;
8390 795b88cb 2023-07-10 thomas if (list_refs || remove_refs) {
8391 795b88cb 2023-07-10 thomas error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8392 795b88cb 2023-07-10 thomas GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
8393 795b88cb 2023-07-10 thomas argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
8394 795b88cb 2023-07-10 thomas goto done;
8397 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL, argv[0],
8398 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
8399 795b88cb 2023-07-10 thomas if (error)
8400 795b88cb 2023-07-10 thomas goto done;
8401 795b88cb 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
8402 795b88cb 2023-07-10 thomas if (error)
8403 795b88cb 2023-07-10 thomas goto done;
8405 795b88cb 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8406 795b88cb 2023-07-10 thomas if (error)
8407 795b88cb 2023-07-10 thomas goto done;
8408 795b88cb 2023-07-10 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8409 795b88cb 2023-07-10 thomas memset(&upa, 0, sizeof(upa));
8410 795b88cb 2023-07-10 thomas error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
8411 795b88cb 2023-07-10 thomas commit_id, repo, update_progress, &upa, check_cancelled,
8413 795b88cb 2023-07-10 thomas if (error != NULL)
8414 795b88cb 2023-07-10 thomas goto done;
8416 795b88cb 2023-07-10 thomas if (upa.did_something) {
8417 795b88cb 2023-07-10 thomas error = logmsg_ref(commit_id,
8418 795b88cb 2023-07-10 thomas GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
8419 795b88cb 2023-07-10 thomas if (error)
8420 795b88cb 2023-07-10 thomas goto done;
8421 795b88cb 2023-07-10 thomas printf("Merged commit %s\n", commit_id_str);
8423 795b88cb 2023-07-10 thomas print_merge_progress_stats(&upa);
8425 795b88cb 2023-07-10 thomas free(cwd);
8426 795b88cb 2023-07-10 thomas if (commit)
8427 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
8428 795b88cb 2023-07-10 thomas free(commit_id_str);
8429 795b88cb 2023-07-10 thomas if (worktree)
8430 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
8431 795b88cb 2023-07-10 thomas if (repo) {
8432 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
8433 795b88cb 2023-07-10 thomas if (error == NULL)
8434 795b88cb 2023-07-10 thomas error = close_err;
8436 795b88cb 2023-07-10 thomas if (pack_fds) {
8437 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
8438 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
8439 795b88cb 2023-07-10 thomas if (error == NULL)
8440 795b88cb 2023-07-10 thomas error = pack_err;
8443 795b88cb 2023-07-10 thomas return error;
8446 795b88cb 2023-07-10 thomas __dead static void
8447 795b88cb 2023-07-10 thomas usage_backout(void)
8449 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
8450 795b88cb 2023-07-10 thomas exit(1);
8453 795b88cb 2023-07-10 thomas static const struct got_error *
8454 795b88cb 2023-07-10 thomas cmd_backout(int argc, char *argv[])
8456 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
8457 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
8458 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
8459 795b88cb 2023-07-10 thomas char *cwd = NULL, *commit_id_str = NULL;
8460 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
8461 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
8462 795b88cb 2023-07-10 thomas struct got_object_qid *pid;
8463 795b88cb 2023-07-10 thomas int ch, list_refs = 0, remove_refs = 0;
8464 795b88cb 2023-07-10 thomas struct got_update_progress_arg upa;
8465 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
8467 795b88cb 2023-07-10 thomas #ifndef PROFILE
8468 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8469 795b88cb 2023-07-10 thomas "unveil", NULL) == -1)
8470 795b88cb 2023-07-10 thomas err(1, "pledge");
8473 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "lX")) != -1) {
8474 795b88cb 2023-07-10 thomas switch (ch) {
8475 795b88cb 2023-07-10 thomas case 'l':
8476 795b88cb 2023-07-10 thomas list_refs = 1;
8478 795b88cb 2023-07-10 thomas case 'X':
8479 795b88cb 2023-07-10 thomas remove_refs = 1;
8481 795b88cb 2023-07-10 thomas default:
8482 795b88cb 2023-07-10 thomas usage_backout();
8483 795b88cb 2023-07-10 thomas /* NOTREACHED */
8487 795b88cb 2023-07-10 thomas argc -= optind;
8488 795b88cb 2023-07-10 thomas argv += optind;
8490 795b88cb 2023-07-10 thomas if (list_refs || remove_refs) {
8491 795b88cb 2023-07-10 thomas if (argc != 0 && argc != 1)
8492 795b88cb 2023-07-10 thomas usage_backout();
8493 795b88cb 2023-07-10 thomas } else if (argc != 1)
8494 795b88cb 2023-07-10 thomas usage_backout();
8495 795b88cb 2023-07-10 thomas if (list_refs && remove_refs)
8496 795b88cb 2023-07-10 thomas option_conflict('l', 'X');
8498 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
8499 795b88cb 2023-07-10 thomas if (cwd == NULL) {
8500 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
8501 795b88cb 2023-07-10 thomas goto done;
8504 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
8505 795b88cb 2023-07-10 thomas if (error != NULL)
8506 795b88cb 2023-07-10 thomas goto done;
8508 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
8509 795b88cb 2023-07-10 thomas if (error) {
8510 795b88cb 2023-07-10 thomas if (list_refs || remove_refs) {
8511 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_NOT_WORKTREE)
8512 795b88cb 2023-07-10 thomas goto done;
8513 795b88cb 2023-07-10 thomas } else {
8514 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
8515 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error,
8516 795b88cb 2023-07-10 thomas "backout", cwd);
8517 795b88cb 2023-07-10 thomas goto done;
8521 795b88cb 2023-07-10 thomas error = got_repo_open(&repo,
8522 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_repo_path(worktree) : cwd,
8523 795b88cb 2023-07-10 thomas NULL, pack_fds);
8524 795b88cb 2023-07-10 thomas if (error != NULL)
8525 795b88cb 2023-07-10 thomas goto done;
8527 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
8528 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
8529 795b88cb 2023-07-10 thomas if (error)
8530 795b88cb 2023-07-10 thomas goto done;
8532 795b88cb 2023-07-10 thomas if (list_refs || remove_refs) {
8533 795b88cb 2023-07-10 thomas error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
8534 795b88cb 2023-07-10 thomas GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
8535 795b88cb 2023-07-10 thomas argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
8536 795b88cb 2023-07-10 thomas goto done;
8539 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL, argv[0],
8540 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
8541 795b88cb 2023-07-10 thomas if (error)
8542 795b88cb 2023-07-10 thomas goto done;
8543 795b88cb 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
8544 795b88cb 2023-07-10 thomas if (error)
8545 795b88cb 2023-07-10 thomas goto done;
8547 795b88cb 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8548 795b88cb 2023-07-10 thomas if (error)
8549 795b88cb 2023-07-10 thomas goto done;
8550 795b88cb 2023-07-10 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8551 795b88cb 2023-07-10 thomas if (pid == NULL) {
8552 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_ROOT_COMMIT);
8553 795b88cb 2023-07-10 thomas goto done;
8556 795b88cb 2023-07-10 thomas memset(&upa, 0, sizeof(upa));
8557 795b88cb 2023-07-10 thomas error = got_worktree_merge_files(worktree, commit_id, &pid->id,
8558 795b88cb 2023-07-10 thomas repo, update_progress, &upa, check_cancelled, NULL);
8559 795b88cb 2023-07-10 thomas if (error != NULL)
8560 795b88cb 2023-07-10 thomas goto done;
8562 795b88cb 2023-07-10 thomas if (upa.did_something) {
8563 795b88cb 2023-07-10 thomas error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8564 795b88cb 2023-07-10 thomas worktree, repo);
8565 795b88cb 2023-07-10 thomas if (error)
8566 795b88cb 2023-07-10 thomas goto done;
8567 795b88cb 2023-07-10 thomas printf("Backed out commit %s\n", commit_id_str);
8569 795b88cb 2023-07-10 thomas print_merge_progress_stats(&upa);
8571 795b88cb 2023-07-10 thomas free(cwd);
8572 795b88cb 2023-07-10 thomas if (commit)
8573 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
8574 795b88cb 2023-07-10 thomas free(commit_id_str);
8575 795b88cb 2023-07-10 thomas if (worktree)
8576 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
8577 795b88cb 2023-07-10 thomas if (repo) {
8578 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
8579 795b88cb 2023-07-10 thomas if (error == NULL)
8580 795b88cb 2023-07-10 thomas error = close_err;
8582 795b88cb 2023-07-10 thomas if (pack_fds) {
8583 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
8584 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
8585 795b88cb 2023-07-10 thomas if (error == NULL)
8586 795b88cb 2023-07-10 thomas error = pack_err;
8588 795b88cb 2023-07-10 thomas return error;
8591 795b88cb 2023-07-10 thomas __dead static void
8592 795b88cb 2023-07-10 thomas usage_cat(void)
8594 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
8595 795b88cb 2023-07-10 thomas "arg ...\n", getprogname());
8596 795b88cb 2023-07-10 thomas exit(1);
8599 795b88cb 2023-07-10 thomas static const struct got_error *
8600 795b88cb 2023-07-10 thomas cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8602 795b88cb 2023-07-10 thomas const struct got_error *err;
8603 795b88cb 2023-07-10 thomas struct got_blob_object *blob;
8604 795b88cb 2023-07-10 thomas int fd = -1;
8606 795b88cb 2023-07-10 thomas fd = got_opentempfd();
8607 795b88cb 2023-07-10 thomas if (fd == -1)
8608 795b88cb 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
8610 795b88cb 2023-07-10 thomas err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
8611 795b88cb 2023-07-10 thomas if (err)
8612 795b88cb 2023-07-10 thomas goto done;
8614 795b88cb 2023-07-10 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8616 795b88cb 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
8617 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
8618 795b88cb 2023-07-10 thomas if (blob)
8619 795b88cb 2023-07-10 thomas got_object_blob_close(blob);
8620 795b88cb 2023-07-10 thomas return err;
8623 795b88cb 2023-07-10 thomas static const struct got_error *
8624 795b88cb 2023-07-10 thomas cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8626 795b88cb 2023-07-10 thomas const struct got_error *err;
8627 795b88cb 2023-07-10 thomas struct got_tree_object *tree;
8628 795b88cb 2023-07-10 thomas int nentries, i;
8630 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&tree, repo, id);
8631 795b88cb 2023-07-10 thomas if (err)
8632 795b88cb 2023-07-10 thomas return err;
8634 795b88cb 2023-07-10 thomas nentries = got_object_tree_get_nentries(tree);
8635 795b88cb 2023-07-10 thomas for (i = 0; i < nentries; i++) {
8636 795b88cb 2023-07-10 thomas struct got_tree_entry *te;
8637 795b88cb 2023-07-10 thomas char *id_str;
8638 795b88cb 2023-07-10 thomas if (sigint_received || sigpipe_received)
8640 795b88cb 2023-07-10 thomas te = got_object_tree_get_entry(tree, i);
8641 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8642 795b88cb 2023-07-10 thomas if (err)
8644 795b88cb 2023-07-10 thomas fprintf(outfile, "%s %.7o %s\n", id_str,
8645 795b88cb 2023-07-10 thomas got_tree_entry_get_mode(te),
8646 795b88cb 2023-07-10 thomas got_tree_entry_get_name(te));
8647 795b88cb 2023-07-10 thomas free(id_str);
8650 795b88cb 2023-07-10 thomas got_object_tree_close(tree);
8651 795b88cb 2023-07-10 thomas return err;
8654 795b88cb 2023-07-10 thomas static const struct got_error *
8655 795b88cb 2023-07-10 thomas cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8657 795b88cb 2023-07-10 thomas const struct got_error *err;
8658 795b88cb 2023-07-10 thomas struct got_commit_object *commit;
8659 795b88cb 2023-07-10 thomas const struct got_object_id_queue *parent_ids;
8660 795b88cb 2023-07-10 thomas struct got_object_qid *pid;
8661 795b88cb 2023-07-10 thomas char *id_str = NULL;
8662 795b88cb 2023-07-10 thomas const char *logmsg = NULL;
8663 795b88cb 2023-07-10 thomas char gmtoff[6];
8665 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, id);
8666 795b88cb 2023-07-10 thomas if (err)
8667 795b88cb 2023-07-10 thomas return err;
8669 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8670 795b88cb 2023-07-10 thomas if (err)
8671 795b88cb 2023-07-10 thomas goto done;
8673 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8674 795b88cb 2023-07-10 thomas parent_ids = got_object_commit_get_parent_ids(commit);
8675 795b88cb 2023-07-10 thomas fprintf(outfile, "numparents %d\n",
8676 795b88cb 2023-07-10 thomas got_object_commit_get_nparents(commit));
8677 795b88cb 2023-07-10 thomas STAILQ_FOREACH(pid, parent_ids, entry) {
8678 795b88cb 2023-07-10 thomas char *pid_str;
8679 795b88cb 2023-07-10 thomas err = got_object_id_str(&pid_str, &pid->id);
8680 795b88cb 2023-07-10 thomas if (err)
8681 795b88cb 2023-07-10 thomas goto done;
8682 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8683 795b88cb 2023-07-10 thomas free(pid_str);
8685 795b88cb 2023-07-10 thomas got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
8686 795b88cb 2023-07-10 thomas got_object_commit_get_author_gmtoff(commit));
8687 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
8688 795b88cb 2023-07-10 thomas got_object_commit_get_author(commit),
8689 795b88cb 2023-07-10 thomas (long long)got_object_commit_get_author_time(commit),
8690 795b88cb 2023-07-10 thomas gmtoff);
8692 795b88cb 2023-07-10 thomas got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
8693 795b88cb 2023-07-10 thomas got_object_commit_get_committer_gmtoff(commit));
8694 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
8695 795b88cb 2023-07-10 thomas got_object_commit_get_committer(commit),
8696 795b88cb 2023-07-10 thomas (long long)got_object_commit_get_committer_time(commit),
8697 795b88cb 2023-07-10 thomas gmtoff);
8699 795b88cb 2023-07-10 thomas logmsg = got_object_commit_get_logmsg_raw(commit);
8700 795b88cb 2023-07-10 thomas fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8701 795b88cb 2023-07-10 thomas fprintf(outfile, "%s", logmsg);
8703 795b88cb 2023-07-10 thomas free(id_str);
8704 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
8705 795b88cb 2023-07-10 thomas return err;
8708 795b88cb 2023-07-10 thomas static const struct got_error *
8709 795b88cb 2023-07-10 thomas cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8711 795b88cb 2023-07-10 thomas const struct got_error *err;
8712 795b88cb 2023-07-10 thomas struct got_tag_object *tag;
8713 795b88cb 2023-07-10 thomas char *id_str = NULL;
8714 795b88cb 2023-07-10 thomas const char *tagmsg = NULL;
8715 795b88cb 2023-07-10 thomas char gmtoff[6];
8717 795b88cb 2023-07-10 thomas err = got_object_open_as_tag(&tag, repo, id);
8718 795b88cb 2023-07-10 thomas if (err)
8719 795b88cb 2023-07-10 thomas return err;
8721 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8722 795b88cb 2023-07-10 thomas if (err)
8723 795b88cb 2023-07-10 thomas goto done;
8725 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8727 795b88cb 2023-07-10 thomas switch (got_object_tag_get_object_type(tag)) {
8728 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_BLOB:
8729 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8730 795b88cb 2023-07-10 thomas GOT_OBJ_LABEL_BLOB);
8732 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TREE:
8733 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8734 795b88cb 2023-07-10 thomas GOT_OBJ_LABEL_TREE);
8736 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_COMMIT:
8737 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8738 795b88cb 2023-07-10 thomas GOT_OBJ_LABEL_COMMIT);
8740 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TAG:
8741 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8742 795b88cb 2023-07-10 thomas GOT_OBJ_LABEL_TAG);
8744 795b88cb 2023-07-10 thomas default:
8748 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8749 795b88cb 2023-07-10 thomas got_object_tag_get_name(tag));
8751 795b88cb 2023-07-10 thomas got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
8752 795b88cb 2023-07-10 thomas got_object_tag_get_tagger_gmtoff(tag));
8753 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
8754 795b88cb 2023-07-10 thomas got_object_tag_get_tagger(tag),
8755 795b88cb 2023-07-10 thomas (long long)got_object_tag_get_tagger_time(tag),
8756 795b88cb 2023-07-10 thomas gmtoff);
8758 795b88cb 2023-07-10 thomas tagmsg = got_object_tag_get_message(tag);
8759 795b88cb 2023-07-10 thomas fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8760 795b88cb 2023-07-10 thomas fprintf(outfile, "%s", tagmsg);
8762 795b88cb 2023-07-10 thomas free(id_str);
8763 795b88cb 2023-07-10 thomas got_object_tag_close(tag);
8764 795b88cb 2023-07-10 thomas return err;
8767 795b88cb 2023-07-10 thomas static const struct got_error *
8768 795b88cb 2023-07-10 thomas cmd_cat(int argc, char *argv[])
8770 795b88cb 2023-07-10 thomas const struct got_error *error;
8771 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
8772 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
8773 795b88cb 2023-07-10 thomas char *cwd = NULL, *repo_path = NULL, *label = NULL;
8774 795b88cb 2023-07-10 thomas const char *commit_id_str = NULL;
8775 795b88cb 2023-07-10 thomas struct got_object_id *id = NULL, *commit_id = NULL;
8776 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
8777 795b88cb 2023-07-10 thomas int ch, obj_type, i, force_path = 0;
8778 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
8779 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
8781 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
8783 795b88cb 2023-07-10 thomas #ifndef PROFILE
8784 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8785 795b88cb 2023-07-10 thomas NULL) == -1)
8786 795b88cb 2023-07-10 thomas err(1, "pledge");
8789 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
8790 795b88cb 2023-07-10 thomas switch (ch) {
8791 795b88cb 2023-07-10 thomas case 'c':
8792 795b88cb 2023-07-10 thomas commit_id_str = optarg;
8794 795b88cb 2023-07-10 thomas case 'P':
8795 795b88cb 2023-07-10 thomas force_path = 1;
8797 795b88cb 2023-07-10 thomas case 'r':
8798 795b88cb 2023-07-10 thomas repo_path = realpath(optarg, NULL);
8799 795b88cb 2023-07-10 thomas if (repo_path == NULL)
8800 795b88cb 2023-07-10 thomas return got_error_from_errno2("realpath",
8801 795b88cb 2023-07-10 thomas optarg);
8802 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
8804 795b88cb 2023-07-10 thomas default:
8805 795b88cb 2023-07-10 thomas usage_cat();
8806 795b88cb 2023-07-10 thomas /* NOTREACHED */
8810 795b88cb 2023-07-10 thomas argc -= optind;
8811 795b88cb 2023-07-10 thomas argv += optind;
8813 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
8814 795b88cb 2023-07-10 thomas if (cwd == NULL) {
8815 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
8816 795b88cb 2023-07-10 thomas goto done;
8819 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
8820 795b88cb 2023-07-10 thomas if (error != NULL)
8821 795b88cb 2023-07-10 thomas goto done;
8823 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
8824 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
8825 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
8826 795b88cb 2023-07-10 thomas goto done;
8827 795b88cb 2023-07-10 thomas if (worktree) {
8828 795b88cb 2023-07-10 thomas repo_path = strdup(
8829 795b88cb 2023-07-10 thomas got_worktree_get_repo_path(worktree));
8830 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
8831 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
8832 795b88cb 2023-07-10 thomas goto done;
8835 795b88cb 2023-07-10 thomas /* Release work tree lock. */
8836 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
8837 795b88cb 2023-07-10 thomas worktree = NULL;
8841 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
8842 795b88cb 2023-07-10 thomas repo_path = strdup(cwd);
8843 795b88cb 2023-07-10 thomas if (repo_path == NULL)
8844 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
8847 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8848 795b88cb 2023-07-10 thomas free(repo_path);
8849 795b88cb 2023-07-10 thomas if (error != NULL)
8850 795b88cb 2023-07-10 thomas goto done;
8852 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8853 795b88cb 2023-07-10 thomas if (error)
8854 795b88cb 2023-07-10 thomas goto done;
8856 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8857 795b88cb 2023-07-10 thomas if (error)
8858 795b88cb 2023-07-10 thomas goto done;
8860 795b88cb 2023-07-10 thomas if (commit_id_str == NULL)
8861 795b88cb 2023-07-10 thomas commit_id_str = GOT_REF_HEAD;
8862 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
8863 795b88cb 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
8864 795b88cb 2023-07-10 thomas if (error)
8865 795b88cb 2023-07-10 thomas goto done;
8867 795b88cb 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8868 795b88cb 2023-07-10 thomas if (error)
8869 795b88cb 2023-07-10 thomas goto done;
8871 795b88cb 2023-07-10 thomas for (i = 0; i < argc; i++) {
8872 795b88cb 2023-07-10 thomas if (force_path) {
8873 795b88cb 2023-07-10 thomas error = got_object_id_by_path(&id, repo, commit,
8874 795b88cb 2023-07-10 thomas argv[i]);
8875 795b88cb 2023-07-10 thomas if (error)
8877 795b88cb 2023-07-10 thomas } else {
8878 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&id, &label, argv[i],
8879 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
8881 795b88cb 2023-07-10 thomas if (error) {
8882 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8883 795b88cb 2023-07-10 thomas error->code != GOT_ERR_NOT_REF)
8885 795b88cb 2023-07-10 thomas error = got_object_id_by_path(&id, repo,
8886 795b88cb 2023-07-10 thomas commit, argv[i]);
8887 795b88cb 2023-07-10 thomas if (error)
8892 795b88cb 2023-07-10 thomas error = got_object_get_type(&obj_type, repo, id);
8893 795b88cb 2023-07-10 thomas if (error)
8896 795b88cb 2023-07-10 thomas switch (obj_type) {
8897 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_BLOB:
8898 795b88cb 2023-07-10 thomas error = cat_blob(id, repo, stdout);
8900 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TREE:
8901 795b88cb 2023-07-10 thomas error = cat_tree(id, repo, stdout);
8903 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_COMMIT:
8904 795b88cb 2023-07-10 thomas error = cat_commit(id, repo, stdout);
8906 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TAG:
8907 795b88cb 2023-07-10 thomas error = cat_tag(id, repo, stdout);
8909 795b88cb 2023-07-10 thomas default:
8910 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_OBJ_TYPE);
8913 795b88cb 2023-07-10 thomas if (error)
8915 795b88cb 2023-07-10 thomas free(label);
8916 795b88cb 2023-07-10 thomas label = NULL;
8917 795b88cb 2023-07-10 thomas free(id);
8918 795b88cb 2023-07-10 thomas id = NULL;
8921 795b88cb 2023-07-10 thomas free(label);
8922 795b88cb 2023-07-10 thomas free(id);
8923 795b88cb 2023-07-10 thomas free(commit_id);
8924 795b88cb 2023-07-10 thomas if (commit)
8925 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
8926 795b88cb 2023-07-10 thomas if (worktree)
8927 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
8928 795b88cb 2023-07-10 thomas if (repo) {
8929 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
8930 795b88cb 2023-07-10 thomas if (error == NULL)
8931 795b88cb 2023-07-10 thomas error = close_err;
8933 795b88cb 2023-07-10 thomas if (pack_fds) {
8934 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
8935 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
8936 795b88cb 2023-07-10 thomas if (error == NULL)
8937 795b88cb 2023-07-10 thomas error = pack_err;
8940 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
8941 795b88cb 2023-07-10 thomas return error;
8944 795b88cb 2023-07-10 thomas __dead static void
8945 795b88cb 2023-07-10 thomas usage_info(void)
8947 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s info [path ...]\n",
8948 795b88cb 2023-07-10 thomas getprogname());
8949 795b88cb 2023-07-10 thomas exit(1);
8952 795b88cb 2023-07-10 thomas static const struct got_error *
8953 795b88cb 2023-07-10 thomas print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
8954 795b88cb 2023-07-10 thomas struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8955 795b88cb 2023-07-10 thomas struct got_object_id *commit_id)
8957 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
8958 795b88cb 2023-07-10 thomas char *id_str = NULL;
8959 795b88cb 2023-07-10 thomas char datebuf[128];
8960 795b88cb 2023-07-10 thomas struct tm mytm, *tm;
8961 795b88cb 2023-07-10 thomas struct got_pathlist_head *paths = arg;
8962 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
8965 795b88cb 2023-07-10 thomas * Clear error indication from any of the path arguments which
8966 795b88cb 2023-07-10 thomas * would cause this file index entry to be displayed.
8968 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, paths, entry) {
8969 795b88cb 2023-07-10 thomas if (got_path_cmp(path, pe->path, strlen(path),
8970 795b88cb 2023-07-10 thomas pe->path_len) == 0 ||
8971 795b88cb 2023-07-10 thomas got_path_is_child(path, pe->path, pe->path_len))
8972 795b88cb 2023-07-10 thomas pe->data = NULL; /* no error */
8975 795b88cb 2023-07-10 thomas printf(GOT_COMMIT_SEP_STR);
8976 795b88cb 2023-07-10 thomas if (S_ISLNK(mode))
8977 795b88cb 2023-07-10 thomas printf("symlink: %s\n", path);
8978 795b88cb 2023-07-10 thomas else if (S_ISREG(mode)) {
8979 795b88cb 2023-07-10 thomas printf("file: %s\n", path);
8980 795b88cb 2023-07-10 thomas printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
8981 795b88cb 2023-07-10 thomas } else if (S_ISDIR(mode))
8982 795b88cb 2023-07-10 thomas printf("directory: %s\n", path);
8984 795b88cb 2023-07-10 thomas printf("something: %s\n", path);
8986 795b88cb 2023-07-10 thomas tm = localtime_r(&mtime, &mytm);
8987 795b88cb 2023-07-10 thomas if (tm == NULL)
8988 795b88cb 2023-07-10 thomas return NULL;
8989 795b88cb 2023-07-10 thomas if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
8990 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_NO_SPACE);
8991 795b88cb 2023-07-10 thomas printf("timestamp: %s\n", datebuf);
8993 795b88cb 2023-07-10 thomas if (blob_id) {
8994 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, blob_id);
8995 795b88cb 2023-07-10 thomas if (err)
8996 795b88cb 2023-07-10 thomas return err;
8997 795b88cb 2023-07-10 thomas printf("based on blob: %s\n", id_str);
8998 795b88cb 2023-07-10 thomas free(id_str);
9001 795b88cb 2023-07-10 thomas if (staged_blob_id) {
9002 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, staged_blob_id);
9003 795b88cb 2023-07-10 thomas if (err)
9004 795b88cb 2023-07-10 thomas return err;
9005 795b88cb 2023-07-10 thomas printf("based on staged blob: %s\n", id_str);
9006 795b88cb 2023-07-10 thomas free(id_str);
9009 795b88cb 2023-07-10 thomas if (commit_id) {
9010 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, commit_id);
9011 795b88cb 2023-07-10 thomas if (err)
9012 795b88cb 2023-07-10 thomas return err;
9013 795b88cb 2023-07-10 thomas printf("based on commit: %s\n", id_str);
9014 795b88cb 2023-07-10 thomas free(id_str);
9017 795b88cb 2023-07-10 thomas return NULL;
9020 795b88cb 2023-07-10 thomas static const struct got_error *
9021 795b88cb 2023-07-10 thomas cmd_info(int argc, char *argv[])
9023 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
9024 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
9025 795b88cb 2023-07-10 thomas char *cwd = NULL, *id_str = NULL;
9026 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
9027 795b88cb 2023-07-10 thomas char *uuidstr = NULL;
9028 795b88cb 2023-07-10 thomas int ch, show_files = 0;
9030 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
9032 795b88cb 2023-07-10 thomas #ifndef PROFILE
9033 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9034 795b88cb 2023-07-10 thomas NULL) == -1)
9035 795b88cb 2023-07-10 thomas err(1, "pledge");
9038 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "")) != -1) {
9039 795b88cb 2023-07-10 thomas switch (ch) {
9040 795b88cb 2023-07-10 thomas default:
9041 795b88cb 2023-07-10 thomas usage_info();
9042 795b88cb 2023-07-10 thomas /* NOTREACHED */
9046 795b88cb 2023-07-10 thomas argc -= optind;
9047 795b88cb 2023-07-10 thomas argv += optind;
9049 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
9050 795b88cb 2023-07-10 thomas if (cwd == NULL) {
9051 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
9052 795b88cb 2023-07-10 thomas goto done;
9055 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
9056 795b88cb 2023-07-10 thomas if (error) {
9057 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
9058 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error, "info", cwd);
9059 795b88cb 2023-07-10 thomas goto done;
9062 795b88cb 2023-07-10 thomas #ifndef PROFILE
9063 795b88cb 2023-07-10 thomas /* Remove "wpath cpath proc exec sendfd" promises. */
9064 795b88cb 2023-07-10 thomas if (pledge("stdio rpath flock unveil", NULL) == -1)
9065 795b88cb 2023-07-10 thomas err(1, "pledge");
9067 795b88cb 2023-07-10 thomas error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9068 795b88cb 2023-07-10 thomas if (error)
9069 795b88cb 2023-07-10 thomas goto done;
9071 795b88cb 2023-07-10 thomas if (argc >= 1) {
9072 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv,
9073 795b88cb 2023-07-10 thomas worktree);
9074 795b88cb 2023-07-10 thomas if (error)
9075 795b88cb 2023-07-10 thomas goto done;
9076 795b88cb 2023-07-10 thomas show_files = 1;
9079 795b88cb 2023-07-10 thomas error = got_object_id_str(&id_str,
9080 795b88cb 2023-07-10 thomas got_worktree_get_base_commit_id(worktree));
9081 795b88cb 2023-07-10 thomas if (error)
9082 795b88cb 2023-07-10 thomas goto done;
9084 795b88cb 2023-07-10 thomas error = got_worktree_get_uuid(&uuidstr, worktree);
9085 795b88cb 2023-07-10 thomas if (error)
9086 795b88cb 2023-07-10 thomas goto done;
9088 795b88cb 2023-07-10 thomas printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9089 795b88cb 2023-07-10 thomas printf("work tree base commit: %s\n", id_str);
9090 795b88cb 2023-07-10 thomas printf("work tree path prefix: %s\n",
9091 795b88cb 2023-07-10 thomas got_worktree_get_path_prefix(worktree));
9092 795b88cb 2023-07-10 thomas printf("work tree branch reference: %s\n",
9093 795b88cb 2023-07-10 thomas got_worktree_get_head_ref_name(worktree));
9094 795b88cb 2023-07-10 thomas printf("work tree UUID: %s\n", uuidstr);
9095 795b88cb 2023-07-10 thomas printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9097 795b88cb 2023-07-10 thomas if (show_files) {
9098 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
9099 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &paths, entry) {
9100 795b88cb 2023-07-10 thomas if (pe->path_len == 0)
9101 795b88cb 2023-07-10 thomas continue;
9103 795b88cb 2023-07-10 thomas * Assume this path will fail. This will be corrected
9104 795b88cb 2023-07-10 thomas * in print_path_info() in case the path does suceeed.
9106 795b88cb 2023-07-10 thomas pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
9108 795b88cb 2023-07-10 thomas error = got_worktree_path_info(worktree, &paths,
9109 795b88cb 2023-07-10 thomas print_path_info, &paths, check_cancelled, NULL);
9110 795b88cb 2023-07-10 thomas if (error)
9111 795b88cb 2023-07-10 thomas goto done;
9112 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &paths, entry) {
9113 795b88cb 2023-07-10 thomas if (pe->data != NULL) {
9114 795b88cb 2023-07-10 thomas const struct got_error *perr;
9116 795b88cb 2023-07-10 thomas perr = pe->data;
9117 795b88cb 2023-07-10 thomas error = got_error_fmt(perr->code, "%s",
9118 795b88cb 2023-07-10 thomas pe->path);
9124 795b88cb 2023-07-10 thomas if (worktree)
9125 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
9126 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9127 795b88cb 2023-07-10 thomas free(cwd);
9128 795b88cb 2023-07-10 thomas free(id_str);
9129 795b88cb 2023-07-10 thomas free(uuidstr);
9130 795b88cb 2023-07-10 thomas return error;