2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 93658fb9 2020-03-18 stsp * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
7 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
8 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
10 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 c0768b0f 2018-06-10 stsp #include <sys/types.h>
20 5de5890b 2018-10-18 stsp #include <sys/stat.h>
21 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
23 5c860e29 2018-03-12 stsp #include <err.h>
24 5c860e29 2018-03-12 stsp #include <errno.h>
25 12463d8b 2019-12-13 stsp #include <fcntl.h>
26 12ce7a6c 2019-08-12 stsp #include <limits.h>
27 5c860e29 2018-03-12 stsp #include <locale.h>
28 818c7501 2019-07-11 stsp #include <ctype.h>
29 99437157 2018-11-11 stsp #include <signal.h>
30 5c860e29 2018-03-12 stsp #include <stdio.h>
31 5c860e29 2018-03-12 stsp #include <stdlib.h>
32 5c860e29 2018-03-12 stsp #include <string.h>
33 5c860e29 2018-03-12 stsp #include <unistd.h>
34 c09a553d 2018-03-12 stsp #include <libgen.h>
35 c0768b0f 2018-06-10 stsp #include <time.h>
36 33ad4cbe 2019-05-12 jcs #include <paths.h>
37 6841bf13 2019-11-29 kn #include <regex.h>
38 83cd27f8 2020-01-13 stsp #include <getopt.h>
40 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
42 53ccebc2 2019-07-30 stsp #include "got_version.h"
43 f42b1b34 2018-03-12 stsp #include "got_error.h"
44 f42b1b34 2018-03-12 stsp #include "got_object.h"
45 5261c201 2018-04-01 stsp #include "got_reference.h"
46 f42b1b34 2018-03-12 stsp #include "got_repository.h"
47 1dd54920 2019-05-11 stsp #include "got_path.h"
48 e6209546 2019-08-22 stsp #include "got_cancel.h"
49 c09a553d 2018-03-12 stsp #include "got_worktree.h"
50 79109fed 2018-03-27 stsp #include "got_diff.h"
51 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
52 6f23baec 2020-03-18 stsp #include "got_fetch.h"
53 f8a36e22 2021-08-26 stsp #include "got_send.h"
54 404c43c4 2018-06-21 stsp #include "got_blame.h"
55 63219cd2 2019-01-04 stsp #include "got_privsep.h"
56 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
57 50b0790e 2020-09-11 stsp #include "got_gotconfig.h"
58 d65a88a2 2021-09-05 stsp #include "got_dial.h"
59 069bbb86 2022-03-07 thomas #include "got_patch.h"
61 5c860e29 2018-03-12 stsp #ifndef nitems
62 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
65 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
66 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
69 99437157 2018-11-11 stsp catch_sigint(int signo)
71 99437157 2018-11-11 stsp sigint_received = 1;
75 99437157 2018-11-11 stsp catch_sigpipe(int signo)
77 99437157 2018-11-11 stsp sigpipe_received = 1;
81 8cfb4057 2019-07-09 stsp struct got_cmd {
82 820059fa 2020-09-25 stsp const char *cmd_name;
83 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
84 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
85 97b3a7be 2019-07-09 stsp const char *cmd_alias;
88 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
89 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
90 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
91 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
92 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
93 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
94 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
95 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
96 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
97 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
98 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
99 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
100 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
101 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
102 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
103 d00136be 2019-03-26 stsp __dead static void usage_add(void);
104 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
105 069bbb86 2022-03-07 thomas __dead static void usage_patch(void);
106 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
107 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
108 f8a36e22 2021-08-26 stsp __dead static void usage_send(void);
109 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
110 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
111 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
112 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
113 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
114 10604dce 2021-09-24 thomas __dead static void usage_merge(void);
115 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
116 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
117 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
118 b2118c49 2020-07-28 stsp __dead static void usage_info(void);
120 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
121 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
122 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
123 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
124 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
125 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
126 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
127 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
128 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
129 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
130 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
131 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
132 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
133 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
134 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
135 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
136 069bbb86 2022-03-07 thomas static const struct got_error* cmd_patch(int, char *[]);
137 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
138 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
139 f8a36e22 2021-08-26 stsp static const struct got_error* cmd_send(int, char *[]);
140 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
141 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
142 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
143 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
144 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
145 10604dce 2021-09-24 thomas static const struct got_error* cmd_merge(int, char *[]);
146 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
147 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
148 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
149 b2118c49 2020-07-28 stsp static const struct got_error* cmd_info(int, char *[]);
151 641a8ee6 2022-02-16 thomas static const struct got_cmd got_commands[] = {
152 b2118c49 2020-07-28 stsp { "init", cmd_init, usage_init, "" },
153 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
154 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
155 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
156 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
157 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
158 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
159 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
160 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
161 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
162 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
163 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
164 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
165 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
166 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
167 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
168 069bbb86 2022-03-07 thomas { "patch", cmd_patch, usage_patch, "pa" },
169 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
170 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
171 f8a36e22 2021-08-26 stsp { "send", cmd_send, usage_send, "se" },
172 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
173 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
174 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
175 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
176 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
177 10604dce 2021-09-24 thomas { "merge", cmd_merge, usage_merge, "mg" },
178 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
179 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
180 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
181 b2118c49 2020-07-28 stsp { "info", cmd_info, usage_info, "" },
184 ce5b7c56 2019-07-09 stsp static void
185 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
189 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
190 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
191 641a8ee6 2022-02-16 thomas const struct got_cmd *cmd = &got_commands[i];
192 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->cmd_name);
194 6879ba42 2020-10-01 naddy fputc('\n', fp);
197 ff69268e 2020-12-13 stsp __dead static void
198 ff69268e 2020-12-13 stsp option_conflict(char a, char b)
200 ff69268e 2020-12-13 stsp errx(1, "-%c and -%c options are mutually exclusive", a, b);
204 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
206 641a8ee6 2022-02-16 thomas const struct got_cmd *cmd;
209 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
210 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
211 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
212 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0 }
215 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
217 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
218 5c860e29 2018-03-12 stsp switch (ch) {
226 6879ba42 2020-10-01 naddy usage(hflag, 1);
227 5c860e29 2018-03-12 stsp /* NOTREACHED */
231 5c860e29 2018-03-12 stsp argc -= optind;
232 5c860e29 2018-03-12 stsp argv += optind;
233 9814e6a3 2020-09-27 naddy optind = 1;
234 9814e6a3 2020-09-27 naddy optreset = 1;
236 53ccebc2 2019-07-30 stsp if (Vflag) {
237 53ccebc2 2019-07-30 stsp got_version_print_str();
241 5c860e29 2018-03-12 stsp if (argc <= 0)
242 6879ba42 2020-10-01 naddy usage(hflag, hflag ? 0 : 1);
244 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
245 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
247 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
248 d7d4f210 2018-03-12 stsp const struct got_error *error;
250 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
252 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
253 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
257 641a8ee6 2022-02-16 thomas cmd->cmd_usage();
259 641a8ee6 2022-02-16 thomas error = cmd->cmd_main(argc, argv);
260 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
261 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
262 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
263 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
264 70015d7a 2019-11-08 stsp !(sigint_received &&
265 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
266 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
273 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
274 6879ba42 2020-10-01 naddy list_commands(stderr);
278 4ed7e80c 2018-05-20 stsp __dead static void
279 6879ba42 2020-10-01 naddy usage(int hflag, int status)
281 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
283 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
284 53ccebc2 2019-07-30 stsp getprogname());
286 6879ba42 2020-10-01 naddy list_commands(fp);
287 6879ba42 2020-10-01 naddy exit(status);
290 0266afb7 2019-01-04 stsp static const struct got_error *
291 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
293 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
294 e2ba3d07 2019-05-13 stsp const char *editor;
296 8920fa04 2019-08-18 stsp *abspath = NULL;
298 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
299 e2ba3d07 2019-05-13 stsp if (editor == NULL)
300 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
302 0ee7065d 2019-05-13 stsp if (editor) {
303 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
305 0ee7065d 2019-05-13 stsp return err;
308 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
309 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
310 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
311 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
314 e2ba3d07 2019-05-13 stsp return NULL;
317 e2ba3d07 2019-05-13 stsp static const struct got_error *
318 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
319 c530dc23 2019-07-23 stsp const char *worktree_path)
321 163ce85a 2019-05-13 stsp const struct got_error *err;
323 37c06ea4 2019-07-15 stsp #ifdef PROFILE
324 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
325 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
327 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
328 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
330 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
331 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
333 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
334 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
336 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
337 163ce85a 2019-05-13 stsp if (err != NULL)
338 163ce85a 2019-05-13 stsp return err;
340 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
341 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
343 0266afb7 2019-01-04 stsp return NULL;
346 2c7829a4 2019-06-17 stsp __dead static void
347 2c7829a4 2019-06-17 stsp usage_init(void)
349 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
353 2c7829a4 2019-06-17 stsp static const struct got_error *
354 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
356 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
357 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
360 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
361 2c7829a4 2019-06-17 stsp switch (ch) {
363 2c7829a4 2019-06-17 stsp usage_init();
364 2c7829a4 2019-06-17 stsp /* NOTREACHED */
368 2c7829a4 2019-06-17 stsp argc -= optind;
369 2c7829a4 2019-06-17 stsp argv += optind;
371 2c7829a4 2019-06-17 stsp #ifndef PROFILE
372 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
373 2c7829a4 2019-06-17 stsp err(1, "pledge");
375 2c7829a4 2019-06-17 stsp if (argc != 1)
376 bc20e173 2019-06-17 stsp usage_init();
378 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
379 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
380 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
382 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
384 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
385 2c7829a4 2019-06-17 stsp if (error &&
386 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
389 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
393 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
395 3ce1b845 2019-07-15 stsp free(repo_path);
396 3ce1b845 2019-07-15 stsp return error;
399 3ce1b845 2019-07-15 stsp __dead static void
400 3ce1b845 2019-07-15 stsp usage_import(void)
402 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
403 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
408 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
411 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
412 3ce1b845 2019-07-15 stsp int st = -1;
414 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
415 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
416 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
418 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
420 3ce1b845 2019-07-15 stsp goto doneediting;
422 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
423 3ce1b845 2019-07-15 stsp _exit(127);
426 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
427 3ce1b845 2019-07-15 stsp if (errno != EINTR)
430 3ce1b845 2019-07-15 stsp doneediting:
431 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
432 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
433 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
435 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
436 3ce1b845 2019-07-15 stsp errno = EINTR;
440 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
443 3ce1b845 2019-07-15 stsp static const struct got_error *
444 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
445 28cf319f 2021-01-28 stsp const char *initial_content, size_t initial_content_len,
446 28cf319f 2021-01-28 stsp int require_modification)
448 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
449 bfa12d5e 2020-09-26 stsp char *line = NULL;
450 bfa12d5e 2020-09-26 stsp size_t linesize = 0;
451 bfa12d5e 2020-09-26 stsp ssize_t linelen;
452 3ce1b845 2019-07-15 stsp struct stat st, st2;
453 bfa12d5e 2020-09-26 stsp FILE *fp = NULL;
454 bfa12d5e 2020-09-26 stsp size_t len, logmsg_len;
455 bfa12d5e 2020-09-26 stsp char *initial_content_stripped = NULL, *buf = NULL, *s;
457 3ce1b845 2019-07-15 stsp *logmsg = NULL;
459 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
460 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
462 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
463 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
465 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
466 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
468 28cf319f 2021-01-28 stsp if (require_modification &&
469 28cf319f 2021-01-28 stsp st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
470 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
471 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
474 bfa12d5e 2020-09-26 stsp * Set up a stripped version of the initial content without comments
475 bfa12d5e 2020-09-26 stsp * and blank lines. We need this in order to check if the message
476 bfa12d5e 2020-09-26 stsp * has in fact been edited.
478 bfa12d5e 2020-09-26 stsp initial_content_stripped = malloc(initial_content_len + 1);
479 bfa12d5e 2020-09-26 stsp if (initial_content_stripped == NULL)
480 bfa12d5e 2020-09-26 stsp return got_error_from_errno("malloc");
481 bfa12d5e 2020-09-26 stsp initial_content_stripped[0] = '\0';
483 bfa12d5e 2020-09-26 stsp buf = strdup(initial_content);
484 bfa12d5e 2020-09-26 stsp if (buf == NULL) {
485 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("strdup");
490 bfa12d5e 2020-09-26 stsp while ((line = strsep(&s, "\n")) != NULL) {
491 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
492 bfa12d5e 2020-09-26 stsp continue; /* remove comments and leading empty lines */
493 bfa12d5e 2020-09-26 stsp len = strlcat(initial_content_stripped, line,
494 bfa12d5e 2020-09-26 stsp initial_content_len + 1);
495 bfa12d5e 2020-09-26 stsp if (len >= initial_content_len + 1) {
496 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
500 bfa12d5e 2020-09-26 stsp while (len > 0 && initial_content_stripped[len - 1] == '\n') {
501 bfa12d5e 2020-09-26 stsp initial_content_stripped[len - 1] = '\0';
505 bfa12d5e 2020-09-26 stsp logmsg_len = st2.st_size;
506 bfa12d5e 2020-09-26 stsp *logmsg = malloc(logmsg_len + 1);
507 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
508 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
509 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
511 c56c5d8a 2021-12-31 thomas fp = fopen(logmsg_path, "re");
512 3ce1b845 2019-07-15 stsp if (fp == NULL) {
513 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
518 bfa12d5e 2020-09-26 stsp while ((linelen = getline(&line, &linesize, fp)) != -1) {
519 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
520 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
521 bfa12d5e 2020-09-26 stsp len = strlcat(*logmsg, line, logmsg_len + 1);
522 bfa12d5e 2020-09-26 stsp if (len >= logmsg_len + 1) {
523 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
527 bfa12d5e 2020-09-26 stsp free(line);
528 bfa12d5e 2020-09-26 stsp if (ferror(fp)) {
529 bfa12d5e 2020-09-26 stsp err = got_ferror(fp, GOT_ERR_IO);
532 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
533 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
537 bfa12d5e 2020-09-26 stsp if (len == 0) {
538 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
539 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
542 28cf319f 2021-01-28 stsp if (require_modification &&
543 28cf319f 2021-01-28 stsp strcmp(*logmsg, initial_content_stripped) == 0)
544 bfa12d5e 2020-09-26 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
545 bfa12d5e 2020-09-26 stsp "no changes made to commit message, aborting");
547 bfa12d5e 2020-09-26 stsp free(initial_content_stripped);
549 bfa12d5e 2020-09-26 stsp if (fp && fclose(fp) == EOF && err == NULL)
550 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("fclose");
552 3ce1b845 2019-07-15 stsp free(*logmsg);
553 3ce1b845 2019-07-15 stsp *logmsg = NULL;
555 3ce1b845 2019-07-15 stsp return err;
558 3ce1b845 2019-07-15 stsp static const struct got_error *
559 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
560 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
562 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
563 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
564 1601cb9f 2020-09-11 naddy int initial_content_len;
565 97972933 2020-09-11 stsp int fd = -1;
567 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
568 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
569 1601cb9f 2020-09-11 naddy branch_name);
570 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
571 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
573 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
574 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
578 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
579 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
583 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
584 0d5bb276 2020-12-15 stsp initial_content_len, 1);
586 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
587 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
588 3ce1b845 2019-07-15 stsp free(initial_content);
590 59f86c76 2020-09-11 stsp free(*logmsg_path);
591 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
593 3ce1b845 2019-07-15 stsp return err;
596 3ce1b845 2019-07-15 stsp static const struct got_error *
597 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
599 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
600 3ce1b845 2019-07-15 stsp return NULL;
603 17431c13 2022-02-12 thomas static int
604 17431c13 2022-02-12 thomas valid_author(const char *author)
607 17431c13 2022-02-12 thomas * Really dumb email address check; we're only doing this to
608 17431c13 2022-02-12 thomas * avoid git's object parser breaking on commits we create.
610 17431c13 2022-02-12 thomas while (*author && *author != '<')
611 17431c13 2022-02-12 thomas author++;
612 17431c13 2022-02-12 thomas if (*author != '<')
613 17431c13 2022-02-12 thomas return 0;
614 17431c13 2022-02-12 thomas while (*author && *author != '@')
615 17431c13 2022-02-12 thomas author++;
616 17431c13 2022-02-12 thomas if (*author != '@')
617 17431c13 2022-02-12 thomas return 0;
618 17431c13 2022-02-12 thomas while (*author && *author != '>')
619 17431c13 2022-02-12 thomas author++;
620 17431c13 2022-02-12 thomas return *author == '>';
623 3ce1b845 2019-07-15 stsp static const struct got_error *
624 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
625 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
627 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
628 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
629 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
631 84792843 2019-08-09 stsp *author = NULL;
633 50b0790e 2020-09-11 stsp if (worktree)
634 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
635 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
638 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
639 50b0790e 2020-09-11 stsp * significant to least significant:
640 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
641 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
642 50b0790e 2020-09-11 stsp * 3) repository's git config file
643 50b0790e 2020-09-11 stsp * 4) environment variables
644 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
647 50b0790e 2020-09-11 stsp if (worktree_conf)
648 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
649 50b0790e 2020-09-11 stsp if (got_author == NULL)
650 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
651 84792843 2019-08-09 stsp if (got_author == NULL) {
652 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
653 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
654 c9956ddf 2019-09-08 stsp if (name && email) {
655 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
656 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
657 c9956ddf 2019-09-08 stsp return NULL;
660 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
661 257add31 2020-09-09 stsp if (got_author == NULL) {
662 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
663 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
665 257add31 2020-09-09 stsp if (name && email) {
666 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
668 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
669 257add31 2020-09-09 stsp return NULL;
671 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
672 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
676 aba9c984 2019-09-08 stsp *author = strdup(got_author);
677 aba9c984 2019-09-08 stsp if (*author == NULL)
678 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
680 17431c13 2022-02-12 thomas if (!valid_author(*author)) {
681 17431c13 2022-02-12 thomas err = got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", *author);
682 aba9c984 2019-09-08 stsp free(*author);
683 aba9c984 2019-09-08 stsp *author = NULL;
685 aba9c984 2019-09-08 stsp return err;
688 c9956ddf 2019-09-08 stsp static const struct got_error *
689 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
691 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
693 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
694 c9956ddf 2019-09-08 stsp if (homedir) {
695 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
696 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
699 c9956ddf 2019-09-08 stsp return NULL;
702 84792843 2019-08-09 stsp static const struct got_error *
703 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
705 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
706 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
707 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
708 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
709 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
710 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
711 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
712 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
714 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
715 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
716 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
718 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
720 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
721 3ce1b845 2019-07-15 stsp switch (ch) {
723 3ce1b845 2019-07-15 stsp branch_name = optarg;
726 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
727 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
728 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
733 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
734 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
735 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
741 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
743 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
749 b2b65d18 2019-08-22 stsp usage_import();
750 3ce1b845 2019-07-15 stsp /* NOTREACHED */
754 3ce1b845 2019-07-15 stsp argc -= optind;
755 3ce1b845 2019-07-15 stsp argv += optind;
757 3ce1b845 2019-07-15 stsp #ifndef PROFILE
758 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
760 3ce1b845 2019-07-15 stsp NULL) == -1)
761 3ce1b845 2019-07-15 stsp err(1, "pledge");
763 3ce1b845 2019-07-15 stsp if (argc != 1)
764 3ce1b845 2019-07-15 stsp usage_import();
766 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
767 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
768 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
769 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
771 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
772 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
775 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
779 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
781 aba9c984 2019-09-08 stsp return error;
784 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
785 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
786 e560b7e0 2019-11-28 stsp * an unintended typo.
788 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
789 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
791 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
792 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
796 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
797 3ce1b845 2019-07-15 stsp if (error) {
798 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
801 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
802 3ce1b845 2019-07-15 stsp "import target branch already exists");
806 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
807 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
808 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
811 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
814 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
815 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
817 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
818 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
821 8e158b01 2019-09-22 stsp free(logmsg);
822 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
823 ef293bdd 2019-10-21 stsp path_dir, refname);
824 ef293bdd 2019-10-21 stsp if (error) {
825 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
826 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
827 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
832 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
833 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
834 ef293bdd 2019-10-21 stsp if (logmsg_path)
835 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
839 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
840 ef293bdd 2019-10-21 stsp if (error) {
841 ef293bdd 2019-10-21 stsp if (logmsg_path)
842 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
846 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
847 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
848 ef293bdd 2019-10-21 stsp if (error) {
849 ef293bdd 2019-10-21 stsp if (logmsg_path)
850 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
854 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
855 ef293bdd 2019-10-21 stsp if (error) {
856 ef293bdd 2019-10-21 stsp if (logmsg_path)
857 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
861 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
862 ef293bdd 2019-10-21 stsp if (error) {
863 ef293bdd 2019-10-21 stsp if (logmsg_path)
864 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
868 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
869 ef293bdd 2019-10-21 stsp if (error) {
870 ef293bdd 2019-10-21 stsp if (logmsg_path)
871 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
875 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
876 3ce1b845 2019-07-15 stsp if (error) {
877 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
878 ef293bdd 2019-10-21 stsp if (logmsg_path)
879 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
883 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
884 3ce1b845 2019-07-15 stsp branch_ref);
885 ef293bdd 2019-10-21 stsp if (error) {
886 ef293bdd 2019-10-21 stsp if (logmsg_path)
887 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
891 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
892 ef293bdd 2019-10-21 stsp if (error) {
893 ef293bdd 2019-10-21 stsp if (logmsg_path)
894 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
899 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
900 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
902 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
903 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
904 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
905 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
906 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
907 8e158b01 2019-09-22 stsp free(logmsg);
908 ef293bdd 2019-10-21 stsp free(logmsg_path);
909 2c7829a4 2019-06-17 stsp free(repo_path);
910 3ce1b845 2019-07-15 stsp free(editor);
911 3ce1b845 2019-07-15 stsp free(refname);
912 3ce1b845 2019-07-15 stsp free(new_commit_id);
913 3ce1b845 2019-07-15 stsp free(id_str);
914 aba9c984 2019-09-08 stsp free(author);
915 c9956ddf 2019-09-08 stsp free(gitconfig_path);
916 3ce1b845 2019-07-15 stsp if (branch_ref)
917 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
918 3ce1b845 2019-07-15 stsp if (head_ref)
919 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
920 2c7829a4 2019-06-17 stsp return error;
923 93658fb9 2020-03-18 stsp __dead static void
924 93658fb9 2020-03-18 stsp usage_clone(void)
926 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
927 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
931 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
932 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
933 892ac3b6 2020-03-18 stsp int last_p_indexed;
934 892ac3b6 2020-03-18 stsp int last_p_resolved;
935 68999b92 2020-03-18 stsp int verbosity;
937 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
939 04d9a9ec 2020-09-24 stsp int create_configs;
940 04d9a9ec 2020-09-24 stsp int configs_created;
942 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
943 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
944 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs;
945 04d9a9ec 2020-09-24 stsp const char *proto;
946 04d9a9ec 2020-09-24 stsp const char *host;
947 04d9a9ec 2020-09-24 stsp const char *port;
948 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
949 04d9a9ec 2020-09-24 stsp const char *git_url;
950 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
951 04d9a9ec 2020-09-24 stsp int mirror_references;
952 04d9a9ec 2020-09-24 stsp } config_info;
955 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
956 93658fb9 2020-03-18 stsp static const struct got_error *
957 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
958 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
959 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
960 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
961 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo);
963 04d9a9ec 2020-09-24 stsp static const struct got_error *
964 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
965 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
967 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
968 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
969 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
970 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
971 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
974 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
975 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
976 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
977 04d9a9ec 2020-09-24 stsp * we have all required information.
979 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
980 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
981 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
982 62d463ca 2020-10-20 naddy a->config_info.host, a->config_info.port,
983 62d463ca 2020-10-20 naddy a->config_info.remote_repo_path,
984 62d463ca 2020-10-20 naddy a->config_info.git_url,
985 62d463ca 2020-10-20 naddy a->config_info.fetch_all_branches,
986 62d463ca 2020-10-20 naddy a->config_info.mirror_references,
987 62d463ca 2020-10-20 naddy a->config_info.symrefs,
988 99495ddb 2021-01-10 stsp a->config_info.wanted_branches,
989 99495ddb 2021-01-10 stsp a->config_info.wanted_refs, a->repo);
991 04d9a9ec 2020-09-24 stsp return err;
992 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
995 68999b92 2020-03-18 stsp if (a->verbosity < 0)
996 68999b92 2020-03-18 stsp return NULL;
998 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
999 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
1000 892ac3b6 2020-03-18 stsp fflush(stdout);
1001 12d1281e 2020-03-19 stsp return NULL;
1004 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
1005 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1006 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
1007 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
1008 892ac3b6 2020-03-18 stsp print_size = 1;
1009 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
1010 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1011 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
1013 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
1014 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
1015 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
1016 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
1017 892ac3b6 2020-03-18 stsp print_indexed = 1;
1018 892ac3b6 2020-03-18 stsp print_size = 1;
1021 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
1022 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
1023 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
1024 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
1025 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
1026 892ac3b6 2020-03-18 stsp print_resolved = 1;
1027 892ac3b6 2020-03-18 stsp print_indexed = 1;
1028 892ac3b6 2020-03-18 stsp print_size = 1;
1033 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1034 892ac3b6 2020-03-18 stsp printf("\r");
1035 892ac3b6 2020-03-18 stsp if (print_size)
1036 d2f35ef7 2022-02-13 thomas printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1037 d715f13e 2020-03-19 stsp if (print_indexed)
1038 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
1039 d715f13e 2020-03-19 stsp if (print_resolved)
1040 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
1041 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1042 892ac3b6 2020-03-18 stsp fflush(stdout);
1044 531c3985 2020-03-18 stsp return NULL;
1047 531c3985 2020-03-18 stsp static const struct got_error *
1048 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
1049 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
1051 4ba14133 2020-03-20 stsp const struct got_error *err;
1052 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
1054 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1056 4ba14133 2020-03-20 stsp return err;
1058 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
1059 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
1060 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
1061 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
1063 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
1064 4ba14133 2020-03-20 stsp return err;
1067 4ba14133 2020-03-20 stsp static const struct got_error *
1068 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
1069 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
1071 41b0de12 2020-03-21 stsp const struct got_error *err;
1072 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
1074 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1075 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1076 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1078 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1081 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1082 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1083 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1084 41b0de12 2020-03-21 stsp char *id_str;
1086 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1088 41b0de12 2020-03-21 stsp return err;
1089 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1090 41b0de12 2020-03-21 stsp free(id_str);
1093 41b0de12 2020-03-21 stsp return NULL;
1096 6338a6a1 2020-03-21 stsp static const struct got_error *
1097 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1098 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1100 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1101 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1102 6338a6a1 2020-03-21 stsp char *id_str;
1104 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1106 6338a6a1 2020-03-21 stsp return err;
1108 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1110 6338a6a1 2020-03-21 stsp goto done;
1112 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1113 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1115 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1116 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1118 6338a6a1 2020-03-21 stsp free(id_str);
1119 6338a6a1 2020-03-21 stsp return err;
1122 0e4002ca 2020-03-21 stsp static int
1123 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1125 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1127 0e4002ca 2020-03-21 stsp refname += 5;
1130 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1131 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1133 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1135 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1138 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1139 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1141 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1142 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1145 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1146 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1149 0e4002ca 2020-03-21 stsp static int
1150 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1152 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1154 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1155 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1162 41b0de12 2020-03-21 stsp static const struct got_error *
1163 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1164 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1166 0e4002ca 2020-03-21 stsp const struct got_error *err;
1167 0e4002ca 2020-03-21 stsp char *remote_refname;
1169 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1170 0e4002ca 2020-03-21 stsp refname += 5;
1172 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1173 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1174 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1176 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1177 0e4002ca 2020-03-21 stsp free(remote_refname);
1178 7c0b7f42 2020-09-24 stsp return err;
1181 7c0b7f42 2020-09-24 stsp static const struct got_error *
1182 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1183 15d3c221 2021-01-05 stsp const char *remote_repo_path, const char *default_branch,
1184 0c8b29c5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1185 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1186 99495ddb 2021-01-10 stsp struct got_repository *repo)
1188 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1189 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1190 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1191 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1192 15d3c221 2021-01-05 stsp const char *branchname = NULL;
1193 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1194 7c0b7f42 2020-09-24 stsp ssize_t n;
1196 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1197 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1198 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1200 132af4a5 2021-01-05 stsp branchname = pe->path;
1201 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1202 132af4a5 2021-01-05 stsp branchname += 11;
1203 132af4a5 2021-01-05 stsp if (asprintf(&s, "%s\"%s\" ",
1204 132af4a5 2021-01-05 stsp branches ? branches : "", branchname) == -1) {
1205 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1206 132af4a5 2021-01-05 stsp goto done;
1208 132af4a5 2021-01-05 stsp free(branches);
1209 132af4a5 2021-01-05 stsp branches = s;
1211 0c8b29c5 2021-01-05 stsp } else if (!fetch_all_branches && default_branch) {
1212 15d3c221 2021-01-05 stsp branchname = default_branch;
1213 15d3c221 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1214 15d3c221 2021-01-05 stsp branchname += 11;
1215 132af4a5 2021-01-05 stsp if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1216 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1217 132af4a5 2021-01-05 stsp goto done;
1220 99495ddb 2021-01-10 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1221 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1222 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1224 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1225 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1226 99495ddb 2021-01-10 stsp branchname += 5;
1227 99495ddb 2021-01-10 stsp if (asprintf(&s, "%s\"%s\" ",
1228 99495ddb 2021-01-10 stsp refs ? refs : "", refname) == -1) {
1229 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1230 99495ddb 2021-01-10 stsp goto done;
1232 99495ddb 2021-01-10 stsp free(refs);
1237 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1238 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1239 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1240 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1241 7c0b7f42 2020-09-24 stsp goto done;
1243 c56c5d8a 2021-12-31 thomas gotconfig_file = fopen(gotconfig_path, "ae");
1244 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1245 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1246 7c0b7f42 2020-09-24 stsp goto done;
1248 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1249 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1250 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1251 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1253 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1259 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1260 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1261 132af4a5 2021-01-05 stsp remote_repo_path, branches ? "\tbranch { " : "",
1262 132af4a5 2021-01-05 stsp branches ? branches : "", branches ? "}\n" : "",
1263 99495ddb 2021-01-10 stsp refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1264 0c8b29c5 2021-01-05 stsp mirror_references ? "\tmirror-references yes\n" : "",
1265 0c8b29c5 2021-01-05 stsp fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1266 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1267 7c0b7f42 2020-09-24 stsp goto done;
1269 7c0b7f42 2020-09-24 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1270 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1271 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1272 7c0b7f42 2020-09-24 stsp goto done;
1276 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1277 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1278 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1279 132af4a5 2021-01-05 stsp free(branches);
1280 7c0b7f42 2020-09-24 stsp return err;
1283 7c0b7f42 2020-09-24 stsp static const struct got_error *
1284 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1285 132af4a5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1286 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1287 99495ddb 2021-01-10 stsp struct got_repository *repo)
1289 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1290 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1291 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1292 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1293 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1294 56d0a753 2021-01-20 stsp const char *branchname;
1295 7c0b7f42 2020-09-24 stsp ssize_t n;
1297 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1298 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1299 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1300 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1301 7c0b7f42 2020-09-24 stsp goto done;
1303 c56c5d8a 2021-12-31 thomas gitconfig_file = fopen(gitconfig_path, "ae");
1304 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1305 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1306 7c0b7f42 2020-09-24 stsp goto done;
1308 56d0a753 2021-01-20 stsp if (fetch_all_branches) {
1309 56d0a753 2021-01-20 stsp if (mirror_references) {
1310 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1311 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1312 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1313 56d0a753 2021-01-20 stsp goto done;
1315 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1316 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1317 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1318 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1319 7c0b7f42 2020-09-24 stsp goto done;
1321 132af4a5 2021-01-05 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
1322 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1323 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1325 132af4a5 2021-01-05 stsp branchname = pe->path;
1326 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1327 132af4a5 2021-01-05 stsp branchname += 11;
1328 56d0a753 2021-01-20 stsp if (mirror_references) {
1329 56d0a753 2021-01-20 stsp if (asprintf(&s,
1330 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1331 56d0a753 2021-01-20 stsp branches ? branches : "",
1332 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1333 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1334 56d0a753 2021-01-20 stsp goto done;
1336 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1337 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1338 132af4a5 2021-01-05 stsp branches ? branches : "",
1339 132af4a5 2021-01-05 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1340 132af4a5 2021-01-05 stsp branchname) == -1) {
1341 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1342 132af4a5 2021-01-05 stsp goto done;
1344 132af4a5 2021-01-05 stsp free(branches);
1345 132af4a5 2021-01-05 stsp branches = s;
1349 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1350 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1352 04d9a9ec 2020-09-24 stsp if (default_branch) {
1353 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1354 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1355 7c0b7f42 2020-09-24 stsp branchname += 11;
1357 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1358 56d0a753 2021-01-20 stsp if (mirror_references) {
1359 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1360 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/heads/%s\n",
1361 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1362 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1363 56d0a753 2021-01-20 stsp goto done;
1365 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1366 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1367 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1368 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1369 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1370 7c0b7f42 2020-09-24 stsp goto done;
1373 56d0a753 2021-01-20 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1374 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1375 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1377 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1378 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1379 99495ddb 2021-01-10 stsp refname += 5;
1380 56d0a753 2021-01-20 stsp if (mirror_references) {
1381 56d0a753 2021-01-20 stsp if (asprintf(&s,
1382 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/%s\n",
1383 56d0a753 2021-01-20 stsp refs ? refs : "", refname, refname) == -1) {
1384 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1385 56d0a753 2021-01-20 stsp goto done;
1387 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1388 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1389 99495ddb 2021-01-10 stsp refs ? refs : "",
1390 99495ddb 2021-01-10 stsp refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1391 99495ddb 2021-01-10 stsp refname) == -1) {
1392 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1393 99495ddb 2021-01-10 stsp goto done;
1395 99495ddb 2021-01-10 stsp free(refs);
1400 132af4a5 2021-01-05 stsp if (asprintf(&gitconfig,
1401 132af4a5 2021-01-05 stsp "[remote \"%s\"]\n"
1402 132af4a5 2021-01-05 stsp "\turl = %s\n"
1405 56d0a753 2021-01-20 stsp "\tfetch = refs/tags/*:refs/tags/*\n",
1406 132af4a5 2021-01-05 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1407 56d0a753 2021-01-20 stsp refs ? refs : "") == -1) {
1408 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1409 132af4a5 2021-01-05 stsp goto done;
1411 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1412 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1413 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1414 7c0b7f42 2020-09-24 stsp goto done;
1417 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1418 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1419 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1420 132af4a5 2021-01-05 stsp free(branches);
1421 0e4002ca 2020-03-21 stsp return err;
1424 0e4002ca 2020-03-21 stsp static const struct got_error *
1425 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1426 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1427 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1428 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
1429 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1431 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1432 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1433 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1436 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1437 04d9a9ec 2020-09-24 stsp * one of those.
1439 62d463ca 2020-10-20 naddy if (!TAILQ_EMPTY(wanted_branches)) {
1440 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1441 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1443 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1444 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1445 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1446 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1448 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1451 04d9a9ec 2020-09-24 stsp default_branch = target;
1456 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1457 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1458 0c8b29c5 2021-01-05 stsp default_branch, fetch_all_branches, wanted_branches,
1459 99495ddb 2021-01-10 stsp wanted_refs, mirror_references, repo);
1461 04d9a9ec 2020-09-24 stsp return err;
1463 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1464 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1465 99495ddb 2021-01-10 stsp wanted_branches, wanted_refs, mirror_references, repo);
1468 04d9a9ec 2020-09-24 stsp static const struct got_error *
1469 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1471 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1472 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1473 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1474 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1475 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1476 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1477 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1478 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1479 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1480 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1481 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1482 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1483 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1484 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1485 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1487 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1488 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1489 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1490 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1492 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1493 93658fb9 2020-03-18 stsp switch (ch) {
1495 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1498 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1499 4ba14133 2020-03-20 stsp optarg, NULL);
1500 4ba14133 2020-03-20 stsp if (error)
1501 4ba14133 2020-03-20 stsp return error;
1504 41b0de12 2020-03-21 stsp list_refs_only = 1;
1507 469dd726 2020-03-20 stsp mirror_references = 1;
1510 68999b92 2020-03-18 stsp if (verbosity < 0)
1511 68999b92 2020-03-18 stsp verbosity = 0;
1512 68999b92 2020-03-18 stsp else if (verbosity < 3)
1513 68999b92 2020-03-18 stsp verbosity++;
1516 68999b92 2020-03-18 stsp verbosity = -1;
1519 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1520 0e4002ca 2020-03-21 stsp optarg, NULL);
1521 0e4002ca 2020-03-21 stsp if (error)
1522 0e4002ca 2020-03-21 stsp return error;
1525 93658fb9 2020-03-18 stsp usage_clone();
1529 93658fb9 2020-03-18 stsp argc -= optind;
1530 93658fb9 2020-03-18 stsp argv += optind;
1532 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1533 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
1534 41b0de12 2020-03-21 stsp if (list_refs_only) {
1535 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1536 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
1537 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1538 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
1539 41b0de12 2020-03-21 stsp if (mirror_references)
1540 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
1541 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1542 ff69268e 2020-12-13 stsp option_conflict('l', 'R');
1545 93658fb9 2020-03-18 stsp uri = argv[0];
1547 9df6f38b 2020-03-18 stsp if (argc == 1)
1548 93658fb9 2020-03-18 stsp dirname = NULL;
1549 9df6f38b 2020-03-18 stsp else if (argc == 2)
1550 93658fb9 2020-03-18 stsp dirname = argv[1];
1552 93658fb9 2020-03-18 stsp usage_clone();
1554 5e5da8c4 2021-09-05 stsp error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1555 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1556 39c64a6a 2020-03-18 stsp if (error)
1557 09f63084 2020-03-20 stsp goto done;
1559 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1560 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1561 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1562 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1563 09838ffc 2020-03-18 stsp goto done;
1566 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1567 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1568 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1569 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1570 39c64a6a 2020-03-18 stsp err(1, "pledge");
1572 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1573 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1574 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1575 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1576 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1577 39c64a6a 2020-03-18 stsp err(1, "pledge");
1579 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1580 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1581 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1582 39c64a6a 2020-03-18 stsp goto done;
1584 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1585 39c64a6a 2020-03-18 stsp goto done;
1587 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1588 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1589 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1590 bb64b798 2020-03-18 stsp goto done;
1592 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1594 bb64b798 2020-03-18 stsp repo_path = dirname;
1596 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1597 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1598 2751fe64 2020-09-24 stsp if (error &&
1599 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1600 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1601 41b0de12 2020-03-21 stsp goto done;
1602 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1603 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1604 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1605 41b0de12 2020-03-21 stsp goto done;
1609 d65a88a2 2021-09-05 stsp error = got_dial_apply_unveil(proto);
1610 d65a88a2 2021-09-05 stsp if (error)
1611 d65a88a2 2021-09-05 stsp goto done;
1613 f535bcd4 2020-09-30 stsp error = apply_unveil(repo_path, 0, NULL);
1614 ee448f5f 2020-03-18 stsp if (error)
1615 ee448f5f 2020-03-18 stsp goto done;
1617 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1618 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1619 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1621 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1622 9c52365f 2020-03-21 stsp server_path, verbosity);
1623 39c64a6a 2020-03-18 stsp if (error)
1624 bb64b798 2020-03-18 stsp goto done;
1626 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1627 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1628 2751fe64 2020-09-24 stsp if (error)
1629 2751fe64 2020-09-24 stsp goto done;
1630 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1631 2751fe64 2020-09-24 stsp if (error)
1632 2751fe64 2020-09-24 stsp goto done;
1635 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1636 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1637 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1638 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1639 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1640 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1641 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1642 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1643 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1644 99495ddb 2021-01-10 stsp fpa.config_info.wanted_refs = &wanted_refs;
1645 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1646 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1647 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1648 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1649 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1650 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1651 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1652 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1653 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1654 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1655 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1656 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1657 39c64a6a 2020-03-18 stsp if (error)
1658 d9b4d0c0 2020-03-18 stsp goto done;
1660 41b0de12 2020-03-21 stsp if (list_refs_only) {
1661 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1662 41b0de12 2020-03-21 stsp goto done;
1665 c3741b51 2021-12-31 thomas if (pack_hash == NULL) {
1666 c3741b51 2021-12-31 thomas error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1667 c3741b51 2021-12-31 thomas "server sent an empty pack file");
1668 c3741b51 2021-12-31 thomas goto done;
1670 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1671 39c64a6a 2020-03-18 stsp if (error)
1672 d9b4d0c0 2020-03-18 stsp goto done;
1673 68999b92 2020-03-18 stsp if (verbosity >= 0)
1674 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1675 d9b4d0c0 2020-03-18 stsp free(id_str);
1677 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1678 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1679 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1680 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1681 7ebc0570 2020-03-18 stsp char *remote_refname;
1683 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1684 0e4002ca 2020-03-21 stsp !mirror_references) {
1685 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1686 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1687 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1688 0e4002ca 2020-03-21 stsp if (error)
1689 0e4002ca 2020-03-21 stsp goto done;
1693 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1694 39c64a6a 2020-03-18 stsp if (error)
1695 d9b4d0c0 2020-03-18 stsp goto done;
1697 469dd726 2020-03-20 stsp if (mirror_references)
1700 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1703 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1704 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1705 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1706 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1707 7ebc0570 2020-03-18 stsp goto done;
1709 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1710 f298ae0f 2020-03-25 stsp free(remote_refname);
1711 39c64a6a 2020-03-18 stsp if (error)
1712 d9b4d0c0 2020-03-18 stsp goto done;
1715 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1716 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1717 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1718 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1719 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1720 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1722 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1725 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1726 39c64a6a 2020-03-18 stsp if (error) {
1727 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1728 55330abe 2020-03-20 stsp error = NULL;
1731 d9b4d0c0 2020-03-18 stsp goto done;
1734 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1735 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1736 f298ae0f 2020-03-25 stsp if (error)
1737 f298ae0f 2020-03-25 stsp goto done;
1739 f298ae0f 2020-03-25 stsp if (mirror_references)
1742 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1745 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1746 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1747 f298ae0f 2020-03-25 stsp refname) == -1) {
1748 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1749 f298ae0f 2020-03-25 stsp goto done;
1751 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1752 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1753 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1754 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1755 f298ae0f 2020-03-25 stsp free(remote_refname);
1756 f298ae0f 2020-03-25 stsp goto done;
1758 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1759 f298ae0f 2020-03-25 stsp if (error) {
1760 f298ae0f 2020-03-25 stsp free(remote_refname);
1761 f298ae0f 2020-03-25 stsp free(remote_target);
1762 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1763 f298ae0f 2020-03-25 stsp error = NULL;
1766 f298ae0f 2020-03-25 stsp goto done;
1768 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1769 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1770 f298ae0f 2020-03-25 stsp free(remote_refname);
1771 f298ae0f 2020-03-25 stsp free(remote_target);
1772 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1773 39c64a6a 2020-03-18 stsp if (error)
1774 d9b4d0c0 2020-03-18 stsp goto done;
1776 4ba14133 2020-03-20 stsp if (pe == NULL) {
1778 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1779 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1780 4ba14133 2020-03-20 stsp * which could be fetched instead.
1782 62d463ca 2020-10-20 naddy TAILQ_FOREACH(pe, &wanted_branches, entry) {
1783 4ba14133 2020-03-20 stsp const char *target = pe->path;
1784 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1786 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1787 4ba14133 2020-03-20 stsp if (error) {
1788 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1789 4ba14133 2020-03-20 stsp error = NULL;
1792 4ba14133 2020-03-20 stsp goto done;
1795 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1796 04d9a9ec 2020-09-24 stsp verbosity, repo);
1797 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1798 4ba14133 2020-03-20 stsp if (error)
1799 4ba14133 2020-03-20 stsp goto done;
1804 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1805 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1806 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1808 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1809 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1810 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1811 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1812 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1814 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1815 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1816 1d0f4054 2021-06-17 stsp if (repo) {
1817 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
1818 1d0f4054 2021-06-17 stsp if (error == NULL)
1819 1d0f4054 2021-06-17 stsp error = close_err;
1821 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1822 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1823 d9b4d0c0 2020-03-18 stsp free(pe->data);
1825 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1826 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1827 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1828 d9b4d0c0 2020-03-18 stsp free(pe->data);
1830 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1831 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1832 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1833 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1834 09838ffc 2020-03-18 stsp free(proto);
1835 09838ffc 2020-03-18 stsp free(host);
1836 09838ffc 2020-03-18 stsp free(port);
1837 09838ffc 2020-03-18 stsp free(server_path);
1838 09838ffc 2020-03-18 stsp free(repo_name);
1839 bb64b798 2020-03-18 stsp free(default_destdir);
1840 b46f3e71 2020-03-18 stsp free(git_url);
1841 39c64a6a 2020-03-18 stsp return error;
1844 7848a0e1 2020-03-19 stsp static const struct got_error *
1845 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1846 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1848 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1849 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1850 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1852 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1854 7848a0e1 2020-03-19 stsp goto done;
1856 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1857 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1858 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1860 88609724 2020-03-21 stsp goto done;
1861 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1862 88609724 2020-03-21 stsp goto done;
1863 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1864 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1865 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1867 db6d8ad8 2020-03-21 stsp goto done;
1870 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1871 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1872 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1873 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1874 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1876 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1878 7848a0e1 2020-03-19 stsp goto done;
1879 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1881 e8a967e0 2020-03-21 stsp goto done;
1883 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1885 7848a0e1 2020-03-19 stsp goto done;
1886 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1887 6338a6a1 2020-03-21 stsp goto done;
1889 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1891 6338a6a1 2020-03-21 stsp goto done;
1892 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1894 6338a6a1 2020-03-21 stsp goto done;
1897 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1898 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1899 6338a6a1 2020-03-21 stsp new_id_str);
1901 7848a0e1 2020-03-19 stsp free(old_id);
1902 7848a0e1 2020-03-19 stsp free(new_id_str);
1903 7848a0e1 2020-03-19 stsp return err;
1906 f1bcca34 2020-03-25 stsp static const struct got_error *
1907 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1908 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1910 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1911 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1912 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1914 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1915 bcf34b0e 2020-03-26 stsp if (err) {
1916 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1917 bcf34b0e 2020-03-26 stsp return err;
1918 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1920 bcf34b0e 2020-03-26 stsp goto done;
1922 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1924 bcf34b0e 2020-03-26 stsp goto done;
1926 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1927 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1928 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1929 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1931 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1933 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1934 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1935 bcf34b0e 2020-03-26 stsp goto done;
1937 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1938 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1940 bcf34b0e 2020-03-26 stsp goto done;
1942 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1944 bcf34b0e 2020-03-26 stsp goto done;
1946 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1947 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1948 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1952 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1953 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1954 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1955 bcf34b0e 2020-03-26 stsp err = unlock_err;
1957 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1958 f1bcca34 2020-03-25 stsp return err;
1961 2ab43947 2020-03-18 stsp __dead static void
1962 7848a0e1 2020-03-19 stsp usage_fetch(void)
1964 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1965 161728eb 2021-07-24 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1966 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1967 13f12b09 2020-03-21 stsp getprogname());
1971 7848a0e1 2020-03-19 stsp static const struct got_error *
1972 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1973 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1975 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1976 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1977 3789fd73 2020-03-26 stsp char *id_str = NULL;
1979 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1980 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1982 3789fd73 2020-03-26 stsp return err;
1983 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1984 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
1985 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1986 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1989 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1991 3789fd73 2020-03-26 stsp return err;
1992 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1994 3789fd73 2020-03-26 stsp goto done;
1996 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1998 3789fd73 2020-03-26 stsp goto done;
1999 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
2000 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
2001 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
2006 3789fd73 2020-03-26 stsp free(id_str);
2007 3789fd73 2020-03-26 stsp return NULL;
2010 3789fd73 2020-03-26 stsp static const struct got_error *
2011 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
2012 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
2013 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
2014 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
2016 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
2017 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
2018 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
2019 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
2020 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
2021 3789fd73 2020-03-26 stsp char *local_refname = NULL;
2023 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&my_refs);
2025 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2027 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
2029 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2031 3789fd73 2020-03-26 stsp goto done;
2033 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &my_refs, entry) {
2034 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
2035 1b796c3f 2021-09-11 stsp const char *their_refname;
2037 1b796c3f 2021-09-11 stsp if (remote->mirror_references) {
2038 1b796c3f 2021-09-11 stsp their_refname = refname;
2040 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
2041 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
2042 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
2043 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
2045 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
2046 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
2047 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
2048 3789fd73 2020-03-26 stsp goto done;
2050 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
2053 1b796c3f 2021-09-11 stsp their_refname = local_refname;
2056 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
2057 1b796c3f 2021-09-11 stsp if (strcmp(their_refname, pe->path) == 0)
2060 f21ec2f0 2020-03-21 stsp if (pe != NULL)
2063 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
2064 1b796c3f 2021-09-11 stsp if (strcmp(their_refname, pe->path) == 0)
2067 3789fd73 2020-03-26 stsp if (pe != NULL)
2070 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
2074 3789fd73 2020-03-26 stsp if (local_refname) {
2075 3789fd73 2020-03-26 stsp struct got_reference *ref;
2076 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
2077 3789fd73 2020-03-26 stsp if (err) {
2078 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
2080 3789fd73 2020-03-26 stsp free(local_refname);
2081 3789fd73 2020-03-26 stsp local_refname = NULL;
2084 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
2087 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
2088 3789fd73 2020-03-26 stsp got_ref_close(ref);
2089 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
2090 3789fd73 2020-03-26 stsp err = unlock_err;
2094 3789fd73 2020-03-26 stsp free(local_refname);
2095 3789fd73 2020-03-26 stsp local_refname = NULL;
2099 3789fd73 2020-03-26 stsp free(remote_namespace);
2100 3789fd73 2020-03-26 stsp free(local_refname);
2101 0e4002ca 2020-03-21 stsp return err;
2104 0e4002ca 2020-03-21 stsp static const struct got_error *
2105 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
2106 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
2108 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
2109 0e4002ca 2020-03-21 stsp char *remote_refname;
2110 0e4002ca 2020-03-21 stsp struct got_reference *ref;
2112 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
2113 0e4002ca 2020-03-21 stsp refname += 5;
2115 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2116 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
2117 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
2119 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
2120 0e4002ca 2020-03-21 stsp if (err) {
2121 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
2122 0e4002ca 2020-03-21 stsp goto done;
2123 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
2125 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
2126 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2127 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
2128 9f142382 2020-03-21 stsp err = unlock_err;
2129 0e4002ca 2020-03-21 stsp got_ref_close(ref);
2132 0e4002ca 2020-03-21 stsp free(remote_refname);
2133 161728eb 2021-07-24 stsp return err;
2136 161728eb 2021-07-24 stsp static const struct got_error *
2137 161728eb 2021-07-24 stsp delete_ref(struct got_repository *repo, struct got_reference *ref)
2139 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2140 161728eb 2021-07-24 stsp struct got_object_id *id = NULL;
2141 161728eb 2021-07-24 stsp char *id_str = NULL;
2142 161728eb 2021-07-24 stsp const char *target;
2144 161728eb 2021-07-24 stsp if (got_ref_is_symbolic(ref)) {
2145 161728eb 2021-07-24 stsp target = got_ref_get_symref_target(ref);
2147 161728eb 2021-07-24 stsp err = got_ref_resolve(&id, repo, ref);
2149 161728eb 2021-07-24 stsp goto done;
2150 161728eb 2021-07-24 stsp err = got_object_id_str(&id_str, id);
2152 161728eb 2021-07-24 stsp goto done;
2153 161728eb 2021-07-24 stsp target = id_str;
2156 161728eb 2021-07-24 stsp err = got_ref_delete(ref, repo);
2158 161728eb 2021-07-24 stsp goto done;
2160 161728eb 2021-07-24 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2163 161728eb 2021-07-24 stsp free(id_str);
2164 f21ec2f0 2020-03-21 stsp return err;
2167 f21ec2f0 2020-03-21 stsp static const struct got_error *
2168 161728eb 2021-07-24 stsp delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2170 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2171 161728eb 2021-07-24 stsp struct got_reflist_head refs;
2172 161728eb 2021-07-24 stsp struct got_reflist_entry *re;
2173 161728eb 2021-07-24 stsp char *prefix;
2175 161728eb 2021-07-24 stsp TAILQ_INIT(&refs);
2177 161728eb 2021-07-24 stsp if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2178 161728eb 2021-07-24 stsp err = got_error_from_errno("asprintf");
2179 161728eb 2021-07-24 stsp goto done;
2181 161728eb 2021-07-24 stsp err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2183 161728eb 2021-07-24 stsp goto done;
2185 161728eb 2021-07-24 stsp TAILQ_FOREACH(re, &refs, entry)
2186 161728eb 2021-07-24 stsp delete_ref(repo, re->ref);
2188 161728eb 2021-07-24 stsp got_ref_list_free(&refs);
2189 161728eb 2021-07-24 stsp return err;
2192 161728eb 2021-07-24 stsp static const struct got_error *
2193 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
2195 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
2196 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
2197 7848a0e1 2020-03-19 stsp const char *remote_name;
2198 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
2199 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
2200 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
2201 7848a0e1 2020-03-19 stsp int nremotes;
2202 7848a0e1 2020-03-19 stsp char *id_str = NULL;
2203 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
2204 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
2205 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2206 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2207 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
2208 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
2209 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
2210 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
2211 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
2212 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2213 161728eb 2021-07-24 stsp int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2215 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
2216 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
2217 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
2218 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
2220 161728eb 2021-07-24 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2221 7848a0e1 2020-03-19 stsp switch (ch) {
2223 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
2226 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
2227 4ba14133 2020-03-20 stsp optarg, NULL);
2228 4ba14133 2020-03-20 stsp if (error)
2229 4ba14133 2020-03-20 stsp return error;
2232 f21ec2f0 2020-03-21 stsp delete_refs = 1;
2235 41b0de12 2020-03-21 stsp list_refs_only = 1;
2238 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
2239 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2240 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
2242 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
2245 db6d8ad8 2020-03-21 stsp replace_tags = 1;
2248 7848a0e1 2020-03-19 stsp if (verbosity < 0)
2249 7848a0e1 2020-03-19 stsp verbosity = 0;
2250 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
2251 7848a0e1 2020-03-19 stsp verbosity++;
2254 7848a0e1 2020-03-19 stsp verbosity = -1;
2257 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
2258 0e4002ca 2020-03-21 stsp optarg, NULL);
2259 0e4002ca 2020-03-21 stsp if (error)
2260 0e4002ca 2020-03-21 stsp return error;
2263 161728eb 2021-07-24 stsp delete_remote = 1;
2266 7848a0e1 2020-03-19 stsp usage_fetch();
2270 7848a0e1 2020-03-19 stsp argc -= optind;
2271 7848a0e1 2020-03-19 stsp argv += optind;
2273 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2274 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
2275 41b0de12 2020-03-21 stsp if (list_refs_only) {
2276 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2277 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
2278 ff69268e 2020-12-13 stsp if (fetch_all_branches)
2279 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
2280 f21ec2f0 2020-03-21 stsp if (delete_refs)
2281 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
2282 161728eb 2021-07-24 stsp if (delete_remote)
2283 161728eb 2021-07-24 stsp option_conflict('l', 'X');
2285 161728eb 2021-07-24 stsp if (delete_remote) {
2286 161728eb 2021-07-24 stsp if (fetch_all_branches)
2287 161728eb 2021-07-24 stsp option_conflict('X', 'a');
2288 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_branches))
2289 161728eb 2021-07-24 stsp option_conflict('X', 'b');
2290 161728eb 2021-07-24 stsp if (delete_refs)
2291 161728eb 2021-07-24 stsp option_conflict('X', 'd');
2292 161728eb 2021-07-24 stsp if (replace_tags)
2293 161728eb 2021-07-24 stsp option_conflict('X', 't');
2294 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_refs))
2295 161728eb 2021-07-24 stsp option_conflict('X', 'R');
2298 161728eb 2021-07-24 stsp if (argc == 0) {
2299 161728eb 2021-07-24 stsp if (delete_remote)
2300 161728eb 2021-07-24 stsp errx(1, "-X option requires a remote name");
2301 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2302 161728eb 2021-07-24 stsp } else if (argc == 1)
2303 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2305 7848a0e1 2020-03-19 stsp usage_fetch();
2307 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2308 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2309 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2310 7848a0e1 2020-03-19 stsp goto done;
2313 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2314 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2315 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2316 7848a0e1 2020-03-19 stsp goto done;
2318 7848a0e1 2020-03-19 stsp error = NULL;
2319 7848a0e1 2020-03-19 stsp if (worktree) {
2320 7848a0e1 2020-03-19 stsp repo_path =
2321 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2322 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2323 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2324 7848a0e1 2020-03-19 stsp if (error)
2325 7848a0e1 2020-03-19 stsp goto done;
2327 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2328 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2329 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2330 7848a0e1 2020-03-19 stsp goto done;
2335 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2336 7848a0e1 2020-03-19 stsp if (error)
2337 7848a0e1 2020-03-19 stsp goto done;
2339 161728eb 2021-07-24 stsp if (delete_remote) {
2340 161728eb 2021-07-24 stsp error = delete_refs_for_remote(repo, remote_name);
2341 161728eb 2021-07-24 stsp goto done; /* nothing else to do */
2344 50b0790e 2020-09-11 stsp if (worktree) {
2345 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2346 50b0790e 2020-09-11 stsp if (worktree_conf) {
2347 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2348 50b0790e 2020-09-11 stsp worktree_conf);
2349 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2350 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2351 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2357 50b0790e 2020-09-11 stsp if (remote == NULL) {
2358 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2359 50b0790e 2020-09-11 stsp if (repo_conf) {
2360 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2361 50b0790e 2020-09-11 stsp repo_conf);
2362 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2363 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2364 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2370 50b0790e 2020-09-11 stsp if (remote == NULL) {
2371 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2372 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2373 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2374 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2379 50b0790e 2020-09-11 stsp if (remote == NULL) {
2380 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2381 50b0790e 2020-09-11 stsp goto done;
2384 0c8b29c5 2021-01-05 stsp if (TAILQ_EMPTY(&wanted_branches)) {
2385 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches)
2386 0c8b29c5 2021-01-05 stsp fetch_all_branches = remote->fetch_all_branches;
2387 6480c871 2021-08-30 stsp for (i = 0; i < remote->nfetch_branches; i++) {
2388 b8adfa55 2020-09-25 stsp got_pathlist_append(&wanted_branches,
2389 6480c871 2021-08-30 stsp remote->fetch_branches[i], NULL);
2392 99495ddb 2021-01-10 stsp if (TAILQ_EMPTY(&wanted_refs)) {
2393 6480c871 2021-08-30 stsp for (i = 0; i < remote->nfetch_refs; i++) {
2394 99495ddb 2021-01-10 stsp got_pathlist_append(&wanted_refs,
2395 6480c871 2021-08-30 stsp remote->fetch_refs[i], NULL);
2399 5e5da8c4 2021-09-05 stsp error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2400 6480c871 2021-08-30 stsp &repo_name, remote->fetch_url);
2401 7848a0e1 2020-03-19 stsp if (error)
2402 7848a0e1 2020-03-19 stsp goto done;
2404 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2405 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2406 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2407 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2408 7848a0e1 2020-03-19 stsp err(1, "pledge");
2410 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2411 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2412 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2413 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2414 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2415 7848a0e1 2020-03-19 stsp err(1, "pledge");
2417 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2418 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2419 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2420 7848a0e1 2020-03-19 stsp goto done;
2422 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2423 7848a0e1 2020-03-19 stsp goto done;
2426 d65a88a2 2021-09-05 stsp error = got_dial_apply_unveil(proto);
2427 d65a88a2 2021-09-05 stsp if (error)
2428 d65a88a2 2021-09-05 stsp goto done;
2430 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2431 7848a0e1 2020-03-19 stsp if (error)
2432 7848a0e1 2020-03-19 stsp goto done;
2434 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2435 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2436 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2438 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2439 9c52365f 2020-03-21 stsp server_path, verbosity);
2440 7848a0e1 2020-03-19 stsp if (error)
2441 7848a0e1 2020-03-19 stsp goto done;
2443 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2444 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2445 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2446 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2447 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2448 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2449 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2450 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2451 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2452 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2453 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2454 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2455 7848a0e1 2020-03-19 stsp if (error)
2456 7848a0e1 2020-03-19 stsp goto done;
2458 41b0de12 2020-03-21 stsp if (list_refs_only) {
2459 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2460 41b0de12 2020-03-21 stsp goto done;
2463 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2464 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2465 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2466 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2467 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2468 984065c8 2020-03-19 stsp if (error)
2469 984065c8 2020-03-19 stsp goto done;
2470 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2471 984065c8 2020-03-19 stsp free(id_str);
2472 984065c8 2020-03-19 stsp id_str = NULL;
2475 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2476 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2477 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2478 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2479 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2480 7848a0e1 2020-03-19 stsp char *remote_refname;
2482 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2483 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2484 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2485 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2486 0e4002ca 2020-03-21 stsp if (error)
2487 0e4002ca 2020-03-21 stsp goto done;
2491 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2492 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2493 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2494 7848a0e1 2020-03-19 stsp if (error) {
2495 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2496 7848a0e1 2020-03-19 stsp goto done;
2497 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2499 7848a0e1 2020-03-19 stsp if (error)
2500 7848a0e1 2020-03-19 stsp goto done;
2502 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2503 db6d8ad8 2020-03-21 stsp verbosity, repo);
2504 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2505 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2506 9f142382 2020-03-21 stsp error = unlock_err;
2507 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2508 7848a0e1 2020-03-19 stsp if (error)
2509 7848a0e1 2020-03-19 stsp goto done;
2511 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2512 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2513 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2514 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2515 7848a0e1 2020-03-19 stsp goto done;
2518 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2519 7848a0e1 2020-03-19 stsp if (error) {
2520 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2521 7848a0e1 2020-03-19 stsp goto done;
2522 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2523 688f11b3 2020-03-21 stsp verbosity, repo);
2524 7848a0e1 2020-03-19 stsp if (error)
2525 7848a0e1 2020-03-19 stsp goto done;
2527 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2528 db6d8ad8 2020-03-21 stsp verbosity, repo);
2529 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2530 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2531 9f142382 2020-03-21 stsp error = unlock_err;
2532 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2533 7848a0e1 2020-03-19 stsp if (error)
2534 7848a0e1 2020-03-19 stsp goto done;
2537 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2538 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2539 2ec30c80 2020-03-20 stsp if (error) {
2540 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2541 2ec30c80 2020-03-20 stsp goto done;
2542 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2544 2ec30c80 2020-03-20 stsp if (error)
2545 2ec30c80 2020-03-20 stsp goto done;
2547 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2548 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2549 9f142382 2020-03-21 stsp error = unlock_err;
2550 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2554 f1bcca34 2020-03-25 stsp if (delete_refs) {
2555 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2556 3789fd73 2020-03-26 stsp verbosity, repo);
2557 f1bcca34 2020-03-25 stsp if (error)
2558 f1bcca34 2020-03-25 stsp goto done;
2561 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2562 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2563 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2564 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2565 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2566 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2567 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2569 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2572 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2575 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2576 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2577 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2578 f1bcca34 2020-03-25 stsp goto done;
2580 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2581 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2582 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2583 f1bcca34 2020-03-25 stsp free(remote_refname);
2584 f1bcca34 2020-03-25 stsp goto done;
2587 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2589 f1bcca34 2020-03-25 stsp if (error) {
2590 f1bcca34 2020-03-25 stsp free(remote_refname);
2591 f1bcca34 2020-03-25 stsp free(remote_target);
2592 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2593 f1bcca34 2020-03-25 stsp error = NULL;
2596 f1bcca34 2020-03-25 stsp goto done;
2598 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2599 f1bcca34 2020-03-25 stsp verbosity, repo);
2600 f1bcca34 2020-03-25 stsp free(remote_refname);
2601 f1bcca34 2020-03-25 stsp free(remote_target);
2602 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2603 f1bcca34 2020-03-25 stsp if (error)
2604 f1bcca34 2020-03-25 stsp goto done;
2608 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2609 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2610 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2611 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2612 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2614 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2615 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2616 1d0f4054 2021-06-17 stsp if (repo) {
2617 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2618 1d0f4054 2021-06-17 stsp if (error == NULL)
2619 1d0f4054 2021-06-17 stsp error = close_err;
2621 7848a0e1 2020-03-19 stsp if (worktree)
2622 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2623 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2624 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2625 7848a0e1 2020-03-19 stsp free(pe->data);
2627 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2628 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2629 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2630 7848a0e1 2020-03-19 stsp free(pe->data);
2632 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2633 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2634 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2635 7848a0e1 2020-03-19 stsp free(id_str);
2636 7848a0e1 2020-03-19 stsp free(cwd);
2637 7848a0e1 2020-03-19 stsp free(repo_path);
2638 7848a0e1 2020-03-19 stsp free(pack_hash);
2639 7848a0e1 2020-03-19 stsp free(proto);
2640 7848a0e1 2020-03-19 stsp free(host);
2641 7848a0e1 2020-03-19 stsp free(port);
2642 7848a0e1 2020-03-19 stsp free(server_path);
2643 7848a0e1 2020-03-19 stsp free(repo_name);
2644 7848a0e1 2020-03-19 stsp return error;
2648 7848a0e1 2020-03-19 stsp __dead static void
2649 2ab43947 2020-03-18 stsp usage_checkout(void)
2651 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2652 4ad4a1ec 2021-09-13 tracey "[-p prefix] [-q] repository-path [worktree-path]\n",
2653 4ad4a1ec 2021-09-13 tracey getprogname());
2657 2ab43947 2020-03-18 stsp static void
2658 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2660 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2661 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2662 e6786710 2021-07-03 stsp "garbage-collected by Git or 'gotadmin cleanup'; making the "
2663 e6786710 2021-07-03 stsp "repository writable and running 'got update' will prevent this\n",
2664 2ab43947 2020-03-18 stsp getprogname());
2667 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2668 2ab43947 2020-03-18 stsp const char *worktree_path;
2669 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2670 4ad4a1ec 2021-09-13 tracey int verbosity;
2673 2ab43947 2020-03-18 stsp static const struct got_error *
2674 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2676 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2678 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2679 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2680 2ab43947 2020-03-18 stsp return NULL;
2682 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2683 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2684 2ab43947 2020-03-18 stsp return NULL;
2687 2ab43947 2020-03-18 stsp while (path[0] == '/')
2690 4ad4a1ec 2021-09-13 tracey if (a->verbosity >= 0)
2691 4ad4a1ec 2021-09-13 tracey printf("%c %s/%s\n", status, a->worktree_path, path);
2693 2ab43947 2020-03-18 stsp return NULL;
2696 2ab43947 2020-03-18 stsp static const struct got_error *
2697 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2699 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2700 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2701 2ab43947 2020-03-18 stsp return NULL;
2704 2ab43947 2020-03-18 stsp static const struct got_error *
2705 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2706 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2707 2ab43947 2020-03-18 stsp struct got_repository *repo)
2709 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2710 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2712 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2713 768705e3 2021-09-27 thomas commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2715 2ab43947 2020-03-18 stsp return err;
2717 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2718 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2721 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2722 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2724 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2726 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2734 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2735 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2736 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2738 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2739 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2740 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2741 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2742 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2743 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2745 2ab43947 2020-03-18 stsp free(yca_id);
2746 2ab43947 2020-03-18 stsp return NULL;
2749 2ab43947 2020-03-18 stsp static const struct got_error *
2750 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2751 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2752 2ab43947 2020-03-18 stsp struct got_repository *repo)
2754 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2755 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2756 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2757 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2759 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2761 2ab43947 2020-03-18 stsp goto done;
2763 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2764 2ab43947 2020-03-18 stsp is_same_branch = 1;
2765 2ab43947 2020-03-18 stsp goto done;
2767 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2768 2ab43947 2020-03-18 stsp is_same_branch = 1;
2769 2ab43947 2020-03-18 stsp goto done;
2772 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2774 2ab43947 2020-03-18 stsp goto done;
2776 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2777 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2779 2ab43947 2020-03-18 stsp goto done;
2781 2ab43947 2020-03-18 stsp for (;;) {
2782 2ab43947 2020-03-18 stsp struct got_object_id *id;
2783 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2784 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2785 2ab43947 2020-03-18 stsp if (err) {
2786 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2787 2ab43947 2020-03-18 stsp err = NULL;
2792 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2794 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2795 2ab43947 2020-03-18 stsp is_same_branch = 1;
2801 2ab43947 2020-03-18 stsp if (graph)
2802 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2803 2ab43947 2020-03-18 stsp free(head_commit_id);
2804 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2805 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2806 2ab43947 2020-03-18 stsp return err;
2809 4ed7e80c 2018-05-20 stsp static const struct got_error *
2810 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2812 4b6c9460 2020-03-05 stsp static char msg[512];
2813 4b6c9460 2020-03-05 stsp const char *branch_name;
2815 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2816 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2818 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2820 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2821 4b6c9460 2020-03-05 stsp branch_name += 11;
2823 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2824 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2825 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2826 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2827 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2828 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2830 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2833 4b6c9460 2020-03-05 stsp static const struct got_error *
2834 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2836 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2837 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2838 08e5873e 2021-09-14 stsp struct got_reference *head_ref = NULL, *ref = NULL;
2839 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2840 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2841 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2842 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2843 08e5873e 2021-09-14 stsp const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2844 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2845 08e5873e 2021-09-14 stsp struct got_object_id *commit_id = NULL;
2846 f0207f6a 2020-10-19 stsp char *cwd = NULL;
2847 4ad4a1ec 2021-09-13 tracey int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2848 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2849 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2851 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2853 4ad4a1ec 2021-09-13 tracey while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2854 0bb8a95e 2018-03-12 stsp switch (ch) {
2856 08573d5b 2019-05-14 stsp branch_name = optarg;
2859 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2860 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2861 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2864 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2867 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2869 4ad4a1ec 2021-09-13 tracey case 'q':
2870 4ad4a1ec 2021-09-13 tracey verbosity = -1;
2873 2deda0b9 2019-03-07 stsp usage_checkout();
2874 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2878 0bb8a95e 2018-03-12 stsp argc -= optind;
2879 0bb8a95e 2018-03-12 stsp argv += optind;
2881 6715a751 2018-03-16 stsp #ifndef PROFILE
2882 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2883 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2884 c09a553d 2018-03-12 stsp err(1, "pledge");
2886 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2887 c47340da 2020-09-20 stsp char *base, *dotgit;
2888 f0207f6a 2020-10-19 stsp const char *path;
2889 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2890 76089277 2018-04-01 stsp if (repo_path == NULL)
2891 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2892 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2893 76089277 2018-04-01 stsp if (cwd == NULL) {
2894 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2895 76089277 2018-04-01 stsp goto done;
2897 c47340da 2020-09-20 stsp if (path_prefix[0])
2898 f0207f6a 2020-10-19 stsp path = path_prefix;
2900 f0207f6a 2020-10-19 stsp path = repo_path;
2901 f0207f6a 2020-10-19 stsp error = got_path_basename(&base, path);
2902 f0207f6a 2020-10-19 stsp if (error)
2903 c47340da 2020-09-20 stsp goto done;
2904 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2905 c09a553d 2018-03-12 stsp if (dotgit)
2906 c09a553d 2018-03-12 stsp *dotgit = '\0';
2907 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2908 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2909 f0207f6a 2020-10-19 stsp free(base);
2910 76089277 2018-04-01 stsp goto done;
2912 f0207f6a 2020-10-19 stsp free(base);
2913 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2914 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2915 76089277 2018-04-01 stsp if (repo_path == NULL) {
2916 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2917 76089277 2018-04-01 stsp goto done;
2919 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2920 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2921 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2922 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2924 b4b3a7dd 2019-07-22 stsp goto done;
2926 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2927 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2928 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2929 b4b3a7dd 2019-07-22 stsp goto done;
2933 c09a553d 2018-03-12 stsp usage_checkout();
2935 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2936 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2938 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2939 c09a553d 2018-03-12 stsp if (error != NULL)
2940 c02c541e 2019-03-29 stsp goto done;
2942 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2943 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2944 c530dc23 2019-07-23 stsp if (error) {
2945 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2946 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2947 c530dc23 2019-07-23 stsp goto done;
2948 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2949 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2950 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2951 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2952 c530dc23 2019-07-23 stsp goto done;
2956 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2957 c02c541e 2019-03-29 stsp if (error)
2958 c09a553d 2018-03-12 stsp goto done;
2960 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2961 c09a553d 2018-03-12 stsp if (error != NULL)
2962 c09a553d 2018-03-12 stsp goto done;
2964 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2965 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2966 c09a553d 2018-03-12 stsp goto done;
2968 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2969 c09a553d 2018-03-12 stsp if (error != NULL)
2970 c09a553d 2018-03-12 stsp goto done;
2972 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2973 e5dc7198 2018-12-29 stsp path_prefix);
2974 e5dc7198 2018-12-29 stsp if (error != NULL)
2975 e5dc7198 2018-12-29 stsp goto done;
2976 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2977 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2978 49520a32 2018-12-29 stsp goto done;
2981 8069f636 2019-01-12 stsp if (commit_id_str) {
2982 84de9106 2020-12-26 stsp struct got_reflist_head refs;
2983 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
2984 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2986 84de9106 2020-12-26 stsp if (error)
2987 84de9106 2020-12-26 stsp goto done;
2988 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2989 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2990 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
2991 30837e32 2019-07-25 stsp if (error)
2992 8069f636 2019-01-12 stsp goto done;
2993 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2994 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2995 8069f636 2019-01-12 stsp if (error != NULL) {
2996 8069f636 2019-01-12 stsp free(commit_id);
2997 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2998 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2999 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
3001 8069f636 2019-01-12 stsp goto done;
3003 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3004 4b6c9460 2020-03-05 stsp if (error) {
3005 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
3006 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
3007 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
3009 45d344f6 2019-05-14 stsp goto done;
3011 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3012 8069f636 2019-01-12 stsp commit_id);
3013 8069f636 2019-01-12 stsp if (error)
3014 8069f636 2019-01-12 stsp goto done;
3015 08e5873e 2021-09-14 stsp /* Expand potentially abbreviated commit ID string. */
3016 08e5873e 2021-09-14 stsp free(commit_id_str);
3017 08e5873e 2021-09-14 stsp error = got_object_id_str(&commit_id_str, commit_id);
3018 08e5873e 2021-09-14 stsp if (error)
3019 08e5873e 2021-09-14 stsp goto done;
3021 08e5873e 2021-09-14 stsp commit_id = got_object_id_dup(
3022 08e5873e 2021-09-14 stsp got_worktree_get_base_commit_id(worktree));
3023 08e5873e 2021-09-14 stsp if (commit_id == NULL) {
3024 08e5873e 2021-09-14 stsp error = got_error_from_errno("got_object_id_dup");
3025 08e5873e 2021-09-14 stsp goto done;
3027 08e5873e 2021-09-14 stsp error = got_object_id_str(&commit_id_str, commit_id);
3028 08e5873e 2021-09-14 stsp if (error)
3029 08e5873e 2021-09-14 stsp goto done;
3032 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
3033 f2ea84fa 2019-07-27 stsp if (error)
3034 f2ea84fa 2019-07-27 stsp goto done;
3035 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
3036 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
3037 4ad4a1ec 2021-09-13 tracey cpa.verbosity = verbosity;
3038 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3039 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
3040 c09a553d 2018-03-12 stsp if (error != NULL)
3041 c09a553d 2018-03-12 stsp goto done;
3043 08e5873e 2021-09-14 stsp if (got_ref_is_symbolic(head_ref)) {
3044 08e5873e 2021-09-14 stsp error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3045 08e5873e 2021-09-14 stsp if (error)
3046 08e5873e 2021-09-14 stsp goto done;
3047 08e5873e 2021-09-14 stsp refname = got_ref_get_name(ref);
3049 08e5873e 2021-09-14 stsp refname = got_ref_get_name(head_ref);
3050 08e5873e 2021-09-14 stsp printf("Checked out %s: %s\n", refname, commit_id_str);
3051 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
3052 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
3053 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
3055 08e5873e 2021-09-14 stsp if (head_ref)
3056 08e5873e 2021-09-14 stsp got_ref_close(head_ref);
3058 08e5873e 2021-09-14 stsp got_ref_close(ref);
3059 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3060 8069f636 2019-01-12 stsp free(commit_id_str);
3061 08e5873e 2021-09-14 stsp free(commit_id);
3062 76089277 2018-04-01 stsp free(repo_path);
3063 507dc3bb 2018-12-29 stsp free(worktree_path);
3064 c47340da 2020-09-20 stsp free(cwd);
3065 507dc3bb 2018-12-29 stsp return error;
3068 9627c110 2020-04-18 stsp struct got_update_progress_arg {
3069 9627c110 2020-04-18 stsp int did_something;
3070 9627c110 2020-04-18 stsp int conflicts;
3071 9627c110 2020-04-18 stsp int obstructed;
3072 9627c110 2020-04-18 stsp int not_updated;
3073 10604dce 2021-09-24 thomas int missing;
3074 0ef68859 2021-09-28 thomas int not_deleted;
3075 0ef68859 2021-09-28 thomas int unversioned;
3076 4ad4a1ec 2021-09-13 tracey int verbosity;
3080 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
3082 9627c110 2020-04-18 stsp if (!upa->did_something)
3085 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
3086 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
3087 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
3088 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
3089 9627c110 2020-04-18 stsp upa->obstructed);
3090 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
3091 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
3092 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
3096 0ef68859 2021-09-28 thomas * The meaning of some status codes differs between merge-style operations and
3097 0ef68859 2021-09-28 thomas * update operations. For example, the ! status code means "file was missing"
3098 0ef68859 2021-09-28 thomas * if changes were merged into the work tree, and "missing file was restored"
3099 0ef68859 2021-09-28 thomas * if the work tree was updated. This function should be used by any operation
3100 0ef68859 2021-09-28 thomas * which merges changes into the work tree without updating the work tree.
3103 0ef68859 2021-09-28 thomas print_merge_progress_stats(struct got_update_progress_arg *upa)
3105 0ef68859 2021-09-28 thomas if (!upa->did_something)
3108 0ef68859 2021-09-28 thomas if (upa->conflicts > 0)
3109 0ef68859 2021-09-28 thomas printf("Files with new merge conflicts: %d\n", upa->conflicts);
3110 0ef68859 2021-09-28 thomas if (upa->obstructed > 0)
3111 0ef68859 2021-09-28 thomas printf("File paths obstructed by a non-regular file: %d\n",
3112 0ef68859 2021-09-28 thomas upa->obstructed);
3113 0ef68859 2021-09-28 thomas if (upa->missing > 0)
3114 0ef68859 2021-09-28 thomas printf("Files which had incoming changes but could not be "
3115 0ef68859 2021-09-28 thomas "found in the work tree: %d\n", upa->missing);
3116 0ef68859 2021-09-28 thomas if (upa->not_deleted > 0)
3117 0ef68859 2021-09-28 thomas printf("Files not deleted due to differences in deleted "
3118 0ef68859 2021-09-28 thomas "content: %d\n", upa->not_deleted);
3119 0ef68859 2021-09-28 thomas if (upa->unversioned > 0)
3120 0ef68859 2021-09-28 thomas printf("Files not merged because an unversioned file was "
3121 0ef68859 2021-09-28 thomas "found in the work tree: %d\n", upa->unversioned);
3124 507dc3bb 2018-12-29 stsp __dead static void
3125 507dc3bb 2018-12-29 stsp usage_update(void)
3127 4ad4a1ec 2021-09-13 tracey fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3128 4ad4a1ec 2021-09-13 tracey "[path ...]\n",
3129 507dc3bb 2018-12-29 stsp getprogname());
3133 1ee397ad 2019-07-12 stsp static const struct got_error *
3134 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
3136 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
3138 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
3139 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
3140 1ee397ad 2019-07-12 stsp return NULL;
3142 9627c110 2020-04-18 stsp upa->did_something = 1;
3144 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
3145 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
3146 1ee397ad 2019-07-12 stsp return NULL;
3148 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
3149 9627c110 2020-04-18 stsp upa->conflicts++;
3150 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
3151 9627c110 2020-04-18 stsp upa->obstructed++;
3152 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
3153 9627c110 2020-04-18 stsp upa->not_updated++;
3154 10604dce 2021-09-24 thomas if (status == GOT_STATUS_MISSING)
3155 10604dce 2021-09-24 thomas upa->missing++;
3156 0ef68859 2021-09-28 thomas if (status == GOT_STATUS_CANNOT_DELETE)
3157 0ef68859 2021-09-28 thomas upa->not_deleted++;
3158 0ef68859 2021-09-28 thomas if (status == GOT_STATUS_UNVERSIONED)
3159 0ef68859 2021-09-28 thomas upa->unversioned++;
3161 507dc3bb 2018-12-29 stsp while (path[0] == '/')
3163 4ad4a1ec 2021-09-13 tracey if (upa->verbosity >= 0)
3164 4ad4a1ec 2021-09-13 tracey printf("%c %s\n", status, path);
3166 1ee397ad 2019-07-12 stsp return NULL;
3169 be7061eb 2018-12-30 stsp static const struct got_error *
3170 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
3171 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
3172 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
3174 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
3175 a1fb16d8 2019-05-24 stsp char *base_id_str;
3176 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
3178 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
3179 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
3180 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
3181 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
3182 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
3183 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
3184 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
3187 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
3188 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3189 a1fb16d8 2019-05-24 stsp if (err) {
3190 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
3191 a1fb16d8 2019-05-24 stsp return err;
3192 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
3194 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
3195 a1fb16d8 2019-05-24 stsp return NULL;
3197 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
3198 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
3199 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
3201 a1fb16d8 2019-05-24 stsp return err;
3202 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
3203 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3204 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
3205 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3206 0ebf8283 2019-07-24 stsp return NULL;
3209 0ebf8283 2019-07-24 stsp static const struct got_error *
3210 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3212 0ebf8283 2019-07-24 stsp const struct got_error *err;
3213 0ebf8283 2019-07-24 stsp int in_progress;
3215 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
3217 0ebf8283 2019-07-24 stsp return err;
3218 0ebf8283 2019-07-24 stsp if (in_progress)
3219 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
3221 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
3223 0ebf8283 2019-07-24 stsp return err;
3224 0ebf8283 2019-07-24 stsp if (in_progress)
3225 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
3227 10604dce 2021-09-24 thomas return NULL;
3230 10604dce 2021-09-24 thomas static const struct got_error *
3231 10604dce 2021-09-24 thomas check_merge_in_progress(struct got_worktree *worktree,
3232 10604dce 2021-09-24 thomas struct got_repository *repo)
3234 10604dce 2021-09-24 thomas const struct got_error *err;
3235 10604dce 2021-09-24 thomas int in_progress;
3237 10604dce 2021-09-24 thomas err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3238 10604dce 2021-09-24 thomas if (err)
3239 10604dce 2021-09-24 thomas return err;
3240 10604dce 2021-09-24 thomas if (in_progress)
3241 10604dce 2021-09-24 thomas return got_error(GOT_ERR_MERGE_BUSY);
3243 a1fb16d8 2019-05-24 stsp return NULL;
3246 a5edda0a 2019-07-27 stsp static const struct got_error *
3247 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3248 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
3250 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
3251 a5edda0a 2019-07-27 stsp char *path;
3252 f1417e9f 2021-10-12 thomas struct got_pathlist_entry *new;
3255 a5edda0a 2019-07-27 stsp if (argc == 0) {
3256 a5edda0a 2019-07-27 stsp path = strdup("");
3257 a5edda0a 2019-07-27 stsp if (path == NULL)
3258 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
3259 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
3262 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
3263 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
3266 f1417e9f 2021-10-12 thomas err = got_pathlist_insert(&new, paths, path, NULL);
3267 f1417e9f 2021-10-12 thomas if (err || new == NULL /* duplicate */) {
3268 a5edda0a 2019-07-27 stsp free(path);
3269 f1417e9f 2021-10-12 thomas if (err)
3274 a5edda0a 2019-07-27 stsp return err;
3277 a1fb16d8 2019-05-24 stsp static const struct got_error *
3278 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
3279 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
3281 fa51e947 2020-03-27 stsp const struct got_error *err;
3282 fa51e947 2020-03-27 stsp struct got_repository *repo;
3283 fa51e947 2020-03-27 stsp static char msg[512];
3285 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
3287 fa51e947 2020-03-27 stsp return orig_err;
3289 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
3290 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
3291 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
3292 fa51e947 2020-03-27 stsp "'got checkout'.\n"
3293 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
3294 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3295 fa51e947 2020-03-27 stsp got_repo_close(repo);
3296 fa51e947 2020-03-27 stsp return err;
3299 fa51e947 2020-03-27 stsp static const struct got_error *
3300 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
3302 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
3303 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
3304 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
3305 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
3306 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
3307 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
3308 024e9686 2019-05-14 stsp const char *branch_name = NULL;
3309 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
3310 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
3311 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
3312 4ad4a1ec 2021-09-13 tracey int ch, verbosity = 0;
3313 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
3315 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
3317 4ad4a1ec 2021-09-13 tracey while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3318 507dc3bb 2018-12-29 stsp switch (ch) {
3320 024e9686 2019-05-14 stsp branch_name = optarg;
3323 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
3324 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
3325 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
3327 4ad4a1ec 2021-09-13 tracey case 'q':
3328 4ad4a1ec 2021-09-13 tracey verbosity = -1;
3331 2deda0b9 2019-03-07 stsp usage_update();
3332 507dc3bb 2018-12-29 stsp /* NOTREACHED */
3336 507dc3bb 2018-12-29 stsp argc -= optind;
3337 507dc3bb 2018-12-29 stsp argv += optind;
3339 507dc3bb 2018-12-29 stsp #ifndef PROFILE
3340 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3341 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
3342 507dc3bb 2018-12-29 stsp err(1, "pledge");
3344 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
3345 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
3346 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3347 c4cdcb68 2019-04-03 stsp goto done;
3349 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
3350 fa51e947 2020-03-27 stsp if (error) {
3351 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3352 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
3353 fa51e947 2020-03-27 stsp worktree_path);
3354 7d5807f4 2019-07-11 stsp goto done;
3357 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
3358 f2ea84fa 2019-07-27 stsp if (error)
3359 f2ea84fa 2019-07-27 stsp goto done;
3361 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3363 507dc3bb 2018-12-29 stsp if (error != NULL)
3364 507dc3bb 2018-12-29 stsp goto done;
3366 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3367 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3368 087fb88c 2019-08-04 stsp if (error)
3369 087fb88c 2019-08-04 stsp goto done;
3371 10604dce 2021-09-24 thomas error = check_merge_in_progress(worktree, repo);
3372 10604dce 2021-09-24 thomas if (error)
3373 10604dce 2021-09-24 thomas goto done;
3375 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3376 0266afb7 2019-01-04 stsp if (error)
3377 0266afb7 2019-01-04 stsp goto done;
3379 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3380 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
3381 024e9686 2019-05-14 stsp if (error != NULL)
3382 024e9686 2019-05-14 stsp goto done;
3383 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
3384 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3385 9c4b8182 2019-01-02 stsp if (error != NULL)
3386 9c4b8182 2019-01-02 stsp goto done;
3387 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3388 507dc3bb 2018-12-29 stsp if (error != NULL)
3389 507dc3bb 2018-12-29 stsp goto done;
3391 84de9106 2020-12-26 stsp struct got_reflist_head refs;
3392 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3393 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3395 84de9106 2020-12-26 stsp if (error)
3396 84de9106 2020-12-26 stsp goto done;
3397 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3398 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3399 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
3400 04f57cb3 2019-07-25 stsp free(commit_id_str);
3401 bb787f09 2019-07-25 stsp commit_id_str = NULL;
3402 30837e32 2019-07-25 stsp if (error)
3403 a297e751 2019-07-11 stsp goto done;
3404 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
3405 a297e751 2019-07-11 stsp if (error)
3406 507dc3bb 2018-12-29 stsp goto done;
3409 a1fb16d8 2019-05-24 stsp if (branch_name) {
3410 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
3411 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3412 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3414 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3415 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3416 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3417 024e9686 2019-05-14 stsp goto done;
3419 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3420 024e9686 2019-05-14 stsp if (error)
3421 024e9686 2019-05-14 stsp goto done;
3422 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3424 a367ff0f 2019-05-14 stsp free(head_commit_id);
3425 024e9686 2019-05-14 stsp if (error != NULL)
3426 024e9686 2019-05-14 stsp goto done;
3427 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3428 a367ff0f 2019-05-14 stsp if (error)
3429 a367ff0f 2019-05-14 stsp goto done;
3430 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3431 024e9686 2019-05-14 stsp if (error)
3432 024e9686 2019-05-14 stsp goto done;
3434 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3435 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3436 a367ff0f 2019-05-14 stsp if (error != NULL) {
3437 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3438 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3439 024e9686 2019-05-14 stsp goto done;
3441 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3442 a367ff0f 2019-05-14 stsp if (error)
3443 a367ff0f 2019-05-14 stsp goto done;
3446 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3447 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3448 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3449 507dc3bb 2018-12-29 stsp commit_id);
3450 507dc3bb 2018-12-29 stsp if (error)
3451 507dc3bb 2018-12-29 stsp goto done;
3454 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3455 4ad4a1ec 2021-09-13 tracey upa.verbosity = verbosity;
3456 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3457 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3458 507dc3bb 2018-12-29 stsp if (error != NULL)
3459 507dc3bb 2018-12-29 stsp goto done;
3461 4f3c844b 2021-09-14 stsp if (upa.did_something) {
3462 4f3c844b 2021-09-14 stsp printf("Updated to %s: %s\n",
3463 4f3c844b 2021-09-14 stsp got_worktree_get_head_ref_name(worktree), commit_id_str);
3465 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3466 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3468 c09a553d 2018-03-12 stsp free(worktree_path);
3469 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3470 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3471 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3472 507dc3bb 2018-12-29 stsp free(commit_id);
3473 9c4b8182 2019-01-02 stsp free(commit_id_str);
3474 c09a553d 2018-03-12 stsp return error;
3477 f42b1b34 2018-03-12 stsp static const struct got_error *
3478 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3479 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3480 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3482 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3483 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3485 44392932 2019-08-25 stsp if (blob_id1) {
3486 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3488 44392932 2019-08-25 stsp goto done;
3491 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3493 44392932 2019-08-25 stsp goto done;
3495 44392932 2019-08-25 stsp while (path[0] == '/')
3497 fe621944 2020-11-10 stsp err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3498 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, stdout);
3500 44392932 2019-08-25 stsp if (blob1)
3501 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3502 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3503 44392932 2019-08-25 stsp return err;
3506 44392932 2019-08-25 stsp static const struct got_error *
3507 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3508 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3509 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3511 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3512 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3513 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3515 44392932 2019-08-25 stsp if (tree_id1) {
3516 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3518 44392932 2019-08-25 stsp goto done;
3521 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3523 0f2b3dca 2018-12-22 stsp goto done;
3525 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3526 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3527 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
3528 aaa13589 2019-06-01 stsp arg.outfile = stdout;
3529 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
3530 fe621944 2020-11-10 stsp arg.nlines = 0;
3531 44392932 2019-08-25 stsp while (path[0] == '/')
3533 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
3534 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3536 79109fed 2018-03-27 stsp if (tree1)
3537 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3538 366e0a5f 2019-10-10 stsp if (tree2)
3539 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3540 44392932 2019-08-25 stsp return err;
3543 44392932 2019-08-25 stsp static const struct got_error *
3544 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3545 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3547 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3548 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3549 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3550 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3552 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3553 0208f208 2020-05-05 stsp if (qid != NULL) {
3554 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3555 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3558 0208f208 2020-05-05 stsp return err;
3560 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3561 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3562 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3563 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3564 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3566 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3570 0208f208 2020-05-05 stsp if (tree_id1) {
3571 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3573 0208f208 2020-05-05 stsp goto done;
3576 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3577 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3579 0208f208 2020-05-05 stsp goto done;
3581 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3582 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3584 0208f208 2020-05-05 stsp if (tree1)
3585 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3586 0208f208 2020-05-05 stsp if (tree2)
3587 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3588 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3589 0208f208 2020-05-05 stsp return err;
3592 0208f208 2020-05-05 stsp static const struct got_error *
3593 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3594 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3596 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3597 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3598 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3599 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3600 44392932 2019-08-25 stsp struct got_object_qid *qid;
3602 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3603 44392932 2019-08-25 stsp if (qid != NULL) {
3604 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3607 44392932 2019-08-25 stsp return err;
3610 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3611 44392932 2019-08-25 stsp int obj_type;
3612 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3614 44392932 2019-08-25 stsp goto done;
3615 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3616 44392932 2019-08-25 stsp if (err) {
3617 44392932 2019-08-25 stsp free(obj_id2);
3618 44392932 2019-08-25 stsp goto done;
3620 44392932 2019-08-25 stsp if (pcommit) {
3621 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3622 44392932 2019-08-25 stsp qid->id, path);
3623 44392932 2019-08-25 stsp if (err) {
3624 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3625 2e8c69d1 2020-05-04 stsp free(obj_id2);
3626 2e8c69d1 2020-05-04 stsp goto done;
3629 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3630 2e8c69d1 2020-05-04 stsp if (err) {
3631 2e8c69d1 2020-05-04 stsp free(obj_id2);
3632 2e8c69d1 2020-05-04 stsp goto done;
3636 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3637 44392932 2019-08-25 stsp if (err) {
3638 44392932 2019-08-25 stsp free(obj_id2);
3639 44392932 2019-08-25 stsp goto done;
3641 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3642 44392932 2019-08-25 stsp switch (obj_type) {
3643 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3644 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3645 64453f7e 2020-11-21 stsp 0, 0, repo);
3647 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3648 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3649 64453f7e 2020-11-21 stsp 0, 0, repo);
3652 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3655 44392932 2019-08-25 stsp free(obj_id1);
3656 44392932 2019-08-25 stsp free(obj_id2);
3658 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3659 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3661 44392932 2019-08-25 stsp goto done;
3662 579bd556 2020-10-24 stsp if (pcommit) {
3663 579bd556 2020-10-24 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3664 579bd556 2020-10-24 stsp err = got_object_id_str(&id_str1, obj_id1);
3666 579bd556 2020-10-24 stsp goto done;
3668 64453f7e 2020-11-21 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3670 64453f7e 2020-11-21 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3674 0f2b3dca 2018-12-22 stsp free(id_str1);
3675 0f2b3dca 2018-12-22 stsp free(id_str2);
3676 44392932 2019-08-25 stsp if (pcommit)
3677 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3678 79109fed 2018-03-27 stsp return err;
3681 4bb494d5 2018-06-16 stsp static char *
3682 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3684 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3685 09867e48 2019-08-13 stsp char *p, *s;
3687 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3688 09867e48 2019-08-13 stsp if (tm == NULL)
3689 09867e48 2019-08-13 stsp return NULL;
3690 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3691 09867e48 2019-08-13 stsp if (s == NULL)
3692 09867e48 2019-08-13 stsp return NULL;
3693 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3695 6c281f94 2018-06-11 stsp *p = '\0';
3699 6841bf13 2019-11-29 kn static const struct got_error *
3700 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3701 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3703 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3704 6841bf13 2019-11-29 kn regmatch_t regmatch;
3705 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3707 6841bf13 2019-11-29 kn *have_match = 0;
3709 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3713 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3717 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3718 6841bf13 2019-11-29 kn *have_match = 1;
3720 6841bf13 2019-11-29 kn free(id_str);
3721 6841bf13 2019-11-29 kn free(logmsg);
3725 0208f208 2020-05-05 stsp static void
3726 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3727 0208f208 2020-05-05 stsp regex_t *regex)
3729 0208f208 2020-05-05 stsp regmatch_t regmatch;
3730 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3732 0208f208 2020-05-05 stsp *have_match = 0;
3734 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3735 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3736 0208f208 2020-05-05 stsp *have_match = 1;
3742 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3744 888b7d99 2020-12-26 stsp static const struct got_error*
3745 888b7d99 2020-12-26 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
3746 888b7d99 2020-12-26 stsp struct got_object_id *id, struct got_repository *repo)
3748 888b7d99 2020-12-26 stsp static const struct got_error *err = NULL;
3749 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3751 888b7d99 2020-12-26 stsp const char *name;
3753 888b7d99 2020-12-26 stsp *refs_str = NULL;
3755 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
3756 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3757 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3760 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3761 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3763 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3764 199a4027 2019-02-02 stsp name += 5;
3765 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3767 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3768 e34f9ed6 2019-02-02 stsp name += 6;
3769 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3770 141c2bff 2019-02-04 stsp name += 8;
3771 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3772 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3775 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3778 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3779 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3780 5d844a1e 2019-08-13 stsp if (err) {
3781 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3782 48cae60d 2020-09-22 stsp free(ref_id);
3785 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3786 5d844a1e 2019-08-13 stsp err = NULL;
3787 5d844a1e 2019-08-13 stsp tag = NULL;
3790 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3791 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3792 48cae60d 2020-09-22 stsp free(ref_id);
3794 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3795 a436ad14 2019-08-13 stsp if (cmp != 0)
3797 888b7d99 2020-12-26 stsp s = *refs_str;
3798 888b7d99 2020-12-26 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
3799 888b7d99 2020-12-26 stsp s ? ", " : "", name) == -1) {
3800 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3802 888b7d99 2020-12-26 stsp *refs_str = NULL;
3808 888b7d99 2020-12-26 stsp return err;
3811 888b7d99 2020-12-26 stsp static const struct got_error *
3812 888b7d99 2020-12-26 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3813 888b7d99 2020-12-26 stsp struct got_repository *repo, const char *path,
3814 888b7d99 2020-12-26 stsp struct got_pathlist_head *changed_paths, int show_patch,
3815 e600f124 2021-03-21 stsp int diff_context, struct got_reflist_object_id_map *refs_idmap,
3816 e600f124 2021-03-21 stsp const char *custom_refs_str)
3818 888b7d99 2020-12-26 stsp const struct got_error *err = NULL;
3819 888b7d99 2020-12-26 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3820 888b7d99 2020-12-26 stsp char datebuf[26];
3821 888b7d99 2020-12-26 stsp time_t committer_time;
3822 888b7d99 2020-12-26 stsp const char *author, *committer;
3823 888b7d99 2020-12-26 stsp char *refs_str = NULL;
3825 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3827 8bf5b3c9 2018-03-17 stsp return err;
3829 e600f124 2021-03-21 stsp if (custom_refs_str == NULL) {
3830 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
3831 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3832 e600f124 2021-03-21 stsp if (refs) {
3833 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, id, repo);
3835 e600f124 2021-03-21 stsp goto done;
3839 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3840 e600f124 2021-03-21 stsp if (custom_refs_str)
3841 e600f124 2021-03-21 stsp printf("commit %s (%s)\n", id_str, custom_refs_str);
3843 e600f124 2021-03-21 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3844 e600f124 2021-03-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3845 832c249c 2018-06-10 stsp free(id_str);
3846 d3d493d7 2019-02-21 stsp id_str = NULL;
3847 d3d493d7 2019-02-21 stsp free(refs_str);
3848 d3d493d7 2019-02-21 stsp refs_str = NULL;
3849 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3850 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3851 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3852 09867e48 2019-08-13 stsp if (datestr)
3853 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3854 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3855 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3856 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3857 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3858 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3859 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3860 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3861 3fe1abad 2018-06-10 stsp int n = 1;
3862 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3863 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, parent_ids, entry) {
3864 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3866 888b7d99 2020-12-26 stsp goto done;
3867 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3868 a0603db2 2018-06-10 stsp free(id_str);
3869 888b7d99 2020-12-26 stsp id_str = NULL;
3873 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3875 888b7d99 2020-12-26 stsp goto done;
3877 621015ac 2018-07-23 stsp logmsg = logmsg0;
3879 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3881 832c249c 2018-06-10 stsp printf(" %s\n", line);
3882 832c249c 2018-06-10 stsp } while (line);
3883 621015ac 2018-07-23 stsp free(logmsg0);
3885 0208f208 2020-05-05 stsp if (changed_paths) {
3886 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3887 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3888 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3889 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3891 0208f208 2020-05-05 stsp printf("\n");
3893 971751ac 2018-03-27 stsp if (show_patch) {
3894 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3895 971751ac 2018-03-27 stsp if (err == 0)
3896 971751ac 2018-03-27 stsp printf("\n");
3899 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3900 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3902 888b7d99 2020-12-26 stsp free(id_str);
3903 888b7d99 2020-12-26 stsp free(refs_str);
3904 07862c20 2018-09-15 stsp return err;
3907 f42b1b34 2018-03-12 stsp static const struct got_error *
3908 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3909 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3910 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3911 888b7d99 2020-12-26 stsp int log_branches, int reverse_display_order,
3912 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap)
3914 f42b1b34 2018-03-12 stsp const struct got_error *err;
3915 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3916 6841bf13 2019-11-29 kn regex_t regex;
3917 6841bf13 2019-11-29 kn int have_match;
3918 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3919 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3920 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3921 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3922 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3924 dbdddfee 2021-06-23 naddy STAILQ_INIT(&reversed_commits);
3925 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3927 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(®ex, search_pattern,
3928 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3929 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3931 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3933 f42b1b34 2018-03-12 stsp return err;
3934 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3935 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3937 fcc85cad 2018-07-22 stsp goto done;
3939 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3941 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3944 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3945 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3946 372ccdbb 2018-06-10 stsp if (err) {
3947 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3948 9ba79e04 2018-06-11 stsp err = NULL;
3951 b43fbaa0 2018-06-11 stsp if (id == NULL)
3954 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3958 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3959 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3964 6841bf13 2019-11-29 kn if (search_pattern) {
3965 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, ®ex);
3967 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3970 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3971 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3972 0208f208 2020-05-05 stsp &changed_paths, ®ex);
3973 6841bf13 2019-11-29 kn if (have_match == 0) {
3974 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3975 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3976 0208f208 2020-05-05 stsp free((char *)pe->path);
3977 0208f208 2020-05-05 stsp free(pe->data);
3979 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3984 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3985 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3988 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3989 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3991 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3992 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3993 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
3994 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3998 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3999 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
4002 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4003 0208f208 2020-05-05 stsp free((char *)pe->path);
4004 0208f208 2020-05-05 stsp free(pe->data);
4006 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4008 dbec59df 2020-04-18 stsp if (reverse_display_order) {
4009 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &reversed_commits, entry) {
4010 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
4013 502b9684 2020-07-31 stsp if (show_changed_paths) {
4014 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
4015 502b9684 2020-07-31 stsp commit, repo);
4019 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
4020 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
4021 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
4022 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
4025 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4026 502b9684 2020-07-31 stsp free((char *)pe->path);
4027 502b9684 2020-07-31 stsp free(pe->data);
4029 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
4033 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&reversed_commits)) {
4034 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&reversed_commits);
4035 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4036 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
4038 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4039 0208f208 2020-05-05 stsp free((char *)pe->path);
4040 0208f208 2020-05-05 stsp free(pe->data);
4042 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4043 6841bf13 2019-11-29 kn if (search_pattern)
4044 6841bf13 2019-11-29 kn regfree(®ex);
4045 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
4046 f42b1b34 2018-03-12 stsp return err;
4049 4ed7e80c 2018-05-20 stsp __dead static void
4050 6f3d1eb0 2018-03-12 stsp usage_log(void)
4052 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
4053 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
4054 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
4058 b1ebc001 2019-08-13 stsp static int
4059 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
4061 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
4062 b1ebc001 2019-08-13 stsp long long n;
4063 b1ebc001 2019-08-13 stsp const char *errstr;
4065 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4066 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
4068 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4069 b1ebc001 2019-08-13 stsp if (errstr != NULL)
4074 4ed7e80c 2018-05-20 stsp static const struct got_error *
4075 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
4077 f42b1b34 2018-03-12 stsp const struct got_error *error;
4078 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
4079 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
4080 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
4081 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4082 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
4083 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
4084 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
4085 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4086 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
4087 64a96a6d 2018-04-01 stsp const char *errstr;
4088 199a4027 2019-02-02 stsp struct got_reflist_head refs;
4089 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
4091 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4093 6715a751 2018-03-16 stsp #ifndef PROFILE
4094 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4097 f42b1b34 2018-03-12 stsp err(1, "pledge");
4100 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
4102 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
4103 79109fed 2018-03-27 stsp switch (ch) {
4105 79109fed 2018-03-27 stsp show_patch = 1;
4108 0208f208 2020-05-05 stsp show_changed_paths = 1;
4111 d142fc45 2018-04-01 stsp start_commit = optarg;
4114 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4116 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4117 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
4118 8f666e67 2022-02-12 thomas errstr, optarg);
4121 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
4122 64a96a6d 2018-04-01 stsp if (errstr != NULL)
4123 8f666e67 2022-02-12 thomas errx(1, "number of commits is %s: %s",
4124 8f666e67 2022-02-12 thomas errstr, optarg);
4127 48c8c60d 2020-01-27 stsp log_branches = 1;
4130 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4131 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
4132 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4134 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4137 dbec59df 2020-04-18 stsp reverse_display_order = 1;
4140 6841bf13 2019-11-29 kn search_pattern = optarg;
4143 d1fe46f9 2020-04-18 stsp end_commit = optarg;
4146 2deda0b9 2019-03-07 stsp usage_log();
4147 79109fed 2018-03-27 stsp /* NOTREACHED */
4151 79109fed 2018-03-27 stsp argc -= optind;
4152 79109fed 2018-03-27 stsp argv += optind;
4154 dc1edbfa 2019-11-29 kn if (diff_context == -1)
4155 dc1edbfa 2019-11-29 kn diff_context = 3;
4156 dc1edbfa 2019-11-29 kn else if (!show_patch)
4157 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
4159 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
4160 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
4161 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4162 04ca23f4 2018-07-16 stsp goto done;
4165 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
4166 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
4167 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4168 50f2fada 2020-04-24 stsp goto done;
4169 50f2fada 2020-04-24 stsp error = NULL;
4172 603cdeb0 2020-10-22 stsp if (argc == 1) {
4173 e7301579 2019-03-18 stsp if (worktree) {
4174 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4176 e7301579 2019-03-18 stsp if (error)
4177 e7301579 2019-03-18 stsp goto done;
4179 e7301579 2019-03-18 stsp path = strdup(argv[0]);
4180 e7301579 2019-03-18 stsp if (path == NULL) {
4181 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4182 e7301579 2019-03-18 stsp goto done;
4185 603cdeb0 2020-10-22 stsp } else if (argc != 0)
4186 cbd1af7a 2019-03-18 stsp usage_log();
4188 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
4189 5486daa2 2019-05-11 stsp repo_path = worktree ?
4190 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4192 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
4193 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4194 cffc0aa4 2019-02-05 stsp goto done;
4197 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4198 d7d4f210 2018-03-12 stsp if (error != NULL)
4199 04ca23f4 2018-07-16 stsp goto done;
4201 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4202 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4203 c02c541e 2019-03-29 stsp if (error)
4204 c02c541e 2019-03-29 stsp goto done;
4206 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4207 84de9106 2020-12-26 stsp if (error)
4208 84de9106 2020-12-26 stsp goto done;
4210 888b7d99 2020-12-26 stsp error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4211 888b7d99 2020-12-26 stsp if (error)
4212 888b7d99 2020-12-26 stsp goto done;
4214 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
4215 3235492e 2018-04-01 stsp struct got_reference *head_ref;
4216 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
4217 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
4218 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4219 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
4220 3235492e 2018-04-01 stsp if (error != NULL)
4221 d1fe46f9 2020-04-18 stsp goto done;
4222 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
4223 3235492e 2018-04-01 stsp got_ref_close(head_ref);
4224 3235492e 2018-04-01 stsp if (error != NULL)
4225 d1fe46f9 2020-04-18 stsp goto done;
4226 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
4227 d1fe46f9 2020-04-18 stsp start_id);
4228 d1fe46f9 2020-04-18 stsp if (error != NULL)
4229 d1fe46f9 2020-04-18 stsp goto done;
4230 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
4232 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&start_id, NULL,
4233 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4234 d1fe46f9 2020-04-18 stsp if (error != NULL)
4235 d1fe46f9 2020-04-18 stsp goto done;
4237 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
4238 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&end_id, NULL,
4239 84de9106 2020-12-26 stsp end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4240 d1fe46f9 2020-04-18 stsp if (error != NULL)
4241 d1fe46f9 2020-04-18 stsp goto done;
4244 deeabeae 2019-08-27 stsp if (worktree) {
4246 603cdeb0 2020-10-22 stsp * If a path was specified on the command line it was resolved
4247 603cdeb0 2020-10-22 stsp * to a path in the work tree above. Prepend the work tree's
4248 603cdeb0 2020-10-22 stsp * path prefix to obtain the corresponding in-repository path.
4250 603cdeb0 2020-10-22 stsp if (path) {
4251 603cdeb0 2020-10-22 stsp const char *prefix;
4252 603cdeb0 2020-10-22 stsp prefix = got_worktree_get_path_prefix(worktree);
4253 603cdeb0 2020-10-22 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4254 603cdeb0 2020-10-22 stsp (path[0] != '\0') ? "/" : "", path) == -1) {
4255 603cdeb0 2020-10-22 stsp error = got_error_from_errno("asprintf");
4256 603cdeb0 2020-10-22 stsp goto done;
4260 603cdeb0 2020-10-22 stsp error = got_repo_map_path(&in_repo_path, repo,
4261 8fa913ec 2020-11-14 stsp path ? path : "");
4262 04ca23f4 2018-07-16 stsp if (error != NULL)
4263 04ca23f4 2018-07-16 stsp goto done;
4264 04ca23f4 2018-07-16 stsp if (in_repo_path) {
4265 04ca23f4 2018-07-16 stsp free(path);
4266 04ca23f4 2018-07-16 stsp path = in_repo_path;
4269 f89c34e1 2022-03-13 thomas if (worktree) {
4270 f89c34e1 2022-03-13 thomas /* Release work tree lock. */
4271 f89c34e1 2022-03-13 thomas got_worktree_close(worktree);
4272 f89c34e1 2022-03-13 thomas worktree = NULL;
4275 603cdeb0 2020-10-22 stsp error = print_commits(start_id, end_id, repo, path ? path : "",
4276 603cdeb0 2020-10-22 stsp show_changed_paths, show_patch, search_pattern, diff_context,
4277 888b7d99 2020-12-26 stsp limit, log_branches, reverse_display_order, refs_idmap);
4279 04ca23f4 2018-07-16 stsp free(path);
4280 04ca23f4 2018-07-16 stsp free(repo_path);
4281 04ca23f4 2018-07-16 stsp free(cwd);
4282 cffc0aa4 2019-02-05 stsp if (worktree)
4283 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
4284 ad242220 2018-09-08 stsp if (repo) {
4285 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4286 ad242220 2018-09-08 stsp if (error == NULL)
4287 1d0f4054 2021-06-17 stsp error = close_err;
4289 888b7d99 2020-12-26 stsp if (refs_idmap)
4290 888b7d99 2020-12-26 stsp got_reflist_object_id_map_free(refs_idmap);
4291 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4292 8bf5b3c9 2018-03-17 stsp return error;
4295 4ed7e80c 2018-05-20 stsp __dead static void
4296 3f8b7d6a 2018-04-01 stsp usage_diff(void)
4298 2777a004 2021-10-12 thomas fprintf(stderr, "usage: %s diff [-a] [-c commit] [-C number] "
4299 2777a004 2021-10-12 thomas "[-r repository-path] [-s] [-w] [-P] "
4300 2777a004 2021-10-12 thomas "[object1 object2 | path ...]\n", getprogname());
4304 b72f483a 2019-02-05 stsp struct print_diff_arg {
4305 b72f483a 2019-02-05 stsp struct got_repository *repo;
4306 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
4307 b72f483a 2019-02-05 stsp int diff_context;
4308 3fc0c068 2019-02-10 stsp const char *id_str;
4309 3fc0c068 2019-02-10 stsp int header_shown;
4310 98eaaa12 2019-08-03 stsp int diff_staged;
4311 63035f9f 2019-10-06 stsp int ignore_whitespace;
4312 64453f7e 2020-11-21 stsp int force_text_diff;
4316 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
4317 39449a05 2020-07-23 stsp * it as content to the diff engine.
4319 39449a05 2020-07-23 stsp static const struct got_error *
4320 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4321 39449a05 2020-07-23 stsp const char *abspath)
4323 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
4324 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
4325 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
4329 39449a05 2020-07-23 stsp if (dirfd != -1) {
4330 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4331 39449a05 2020-07-23 stsp if (target_len == -1)
4332 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
4334 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
4335 39449a05 2020-07-23 stsp if (target_len == -1)
4336 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
4339 39449a05 2020-07-23 stsp *fd = got_opentempfd();
4340 39449a05 2020-07-23 stsp if (*fd == -1)
4341 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
4343 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
4344 39449a05 2020-07-23 stsp if (outlen == -1) {
4345 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
4346 39449a05 2020-07-23 stsp goto done;
4349 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
4350 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
4351 39449a05 2020-07-23 stsp goto done;
4354 39449a05 2020-07-23 stsp if (err) {
4355 39449a05 2020-07-23 stsp close(*fd);
4358 39449a05 2020-07-23 stsp return err;
4361 4ed7e80c 2018-05-20 stsp static const struct got_error *
4362 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
4363 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4364 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4365 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4367 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
4368 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
4369 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
4370 12463d8b 2019-12-13 stsp int fd = -1;
4371 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
4372 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
4373 b72f483a 2019-02-05 stsp struct stat sb;
4375 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4376 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4377 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4378 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4379 98eaaa12 2019-08-03 stsp return NULL;
4381 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4382 98eaaa12 2019-08-03 stsp return NULL;
4383 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4384 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
4385 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4386 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
4387 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
4388 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
4389 98eaaa12 2019-08-03 stsp return NULL;
4392 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
4393 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
4394 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
4395 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
4396 3fc0c068 2019-02-10 stsp a->header_shown = 1;
4399 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4400 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
4401 98eaaa12 2019-08-03 stsp switch (staged_status) {
4402 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
4403 98eaaa12 2019-08-03 stsp label1 = path;
4404 98eaaa12 2019-08-03 stsp label2 = path;
4406 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
4407 98eaaa12 2019-08-03 stsp label2 = path;
4409 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
4410 98eaaa12 2019-08-03 stsp label1 = path;
4413 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
4415 fe621944 2020-11-10 stsp return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4416 fe621944 2020-11-10 stsp staged_blob_id, label1, label2, a->diff_context,
4417 64453f7e 2020-11-21 stsp a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4420 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4421 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
4422 4ce46740 2019-08-08 stsp char *id_str;
4423 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4426 4ce46740 2019-08-08 stsp goto done;
4427 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4429 4ce46740 2019-08-08 stsp goto done;
4430 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4431 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4432 4ce46740 2019-08-08 stsp free(id_str);
4433 4ce46740 2019-08-08 stsp goto done;
4435 4ce46740 2019-08-08 stsp free(id_str);
4436 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4437 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4439 4ce46740 2019-08-08 stsp goto done;
4442 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4443 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4444 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4445 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4446 2ec1f75b 2019-03-26 stsp goto done;
4449 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4450 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name,
4451 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4452 12463d8b 2019-12-13 stsp if (fd == -1) {
4453 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4454 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4456 39449a05 2020-07-23 stsp goto done;
4458 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4459 39449a05 2020-07-23 stsp de_name, abspath);
4461 39449a05 2020-07-23 stsp goto done;
4464 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4465 12463d8b 2019-12-13 stsp if (fd == -1) {
4466 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4467 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4469 39449a05 2020-07-23 stsp goto done;
4471 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4472 39449a05 2020-07-23 stsp de_name, abspath);
4474 39449a05 2020-07-23 stsp goto done;
4477 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4478 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4479 2ec1f75b 2019-03-26 stsp goto done;
4481 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4482 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4483 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4484 2ec1f75b 2019-03-26 stsp goto done;
4488 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4490 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4491 64453f7e 2020-11-21 stsp a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4493 b72f483a 2019-02-05 stsp if (blob1)
4494 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4495 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4496 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4497 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4498 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4499 b72f483a 2019-02-05 stsp free(abspath);
4500 927df6b7 2019-02-10 stsp return err;
4503 927df6b7 2019-02-10 stsp static const struct got_error *
4504 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4506 b00d56cd 2018-04-01 stsp const struct got_error *error;
4507 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4508 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4509 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4510 cc8021af 2021-10-12 thomas const char *commit_args[2] = { NULL, NULL };
4511 cc8021af 2021-10-12 thomas int ncommit_args = 0;
4512 30f6c0c6 2021-10-08 thomas struct got_object_id *ids[2] = { NULL, NULL };
4513 30f6c0c6 2021-10-08 thomas char *labels[2] = { NULL, NULL };
4514 cc8021af 2021-10-12 thomas int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4515 30f6c0c6 2021-10-08 thomas int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4516 30f6c0c6 2021-10-08 thomas int force_text_diff = 0, force_path = 0, rflag = 0;
4517 c0cc5c62 2018-10-18 stsp const char *errstr;
4518 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4519 30f6c0c6 2021-10-08 thomas struct got_pathlist_head paths;
4520 30f6c0c6 2021-10-08 thomas struct got_pathlist_entry *pe;
4522 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4523 30f6c0c6 2021-10-08 thomas TAILQ_INIT(&paths);
4525 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4526 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4527 25eccc22 2019-01-04 stsp NULL) == -1)
4528 b00d56cd 2018-04-01 stsp err(1, "pledge");
4531 cc8021af 2021-10-12 thomas while ((ch = getopt(argc, argv, "ac:C:r:swP")) != -1) {
4532 b00d56cd 2018-04-01 stsp switch (ch) {
4534 64453f7e 2020-11-21 stsp force_text_diff = 1;
4536 cc8021af 2021-10-12 thomas case 'c':
4537 cc8021af 2021-10-12 thomas if (ncommit_args >= 2)
4538 cc8021af 2021-10-12 thomas errx(1, "too many -c options used");
4539 cc8021af 2021-10-12 thomas commit_args[ncommit_args++] = optarg;
4542 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4544 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4545 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
4546 8f666e67 2022-02-12 thomas errstr, optarg);
4549 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4550 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4551 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4553 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4554 30f6c0c6 2021-10-08 thomas rflag = 1;
4557 98eaaa12 2019-08-03 stsp diff_staged = 1;
4560 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4562 30f6c0c6 2021-10-08 thomas case 'P':
4563 30f6c0c6 2021-10-08 thomas force_path = 1;
4566 2deda0b9 2019-03-07 stsp usage_diff();
4567 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4571 b00d56cd 2018-04-01 stsp argc -= optind;
4572 b00d56cd 2018-04-01 stsp argv += optind;
4574 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4575 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4576 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4577 b72f483a 2019-02-05 stsp goto done;
4580 30f6c0c6 2021-10-08 thomas if (repo_path == NULL) {
4581 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4582 30f6c0c6 2021-10-08 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
4583 1f03b8da 2020-03-20 stsp goto done;
4585 30f6c0c6 2021-10-08 thomas error = NULL;
4586 30f6c0c6 2021-10-08 thomas if (worktree) {
4587 30f6c0c6 2021-10-08 thomas repo_path =
4588 30f6c0c6 2021-10-08 thomas strdup(got_worktree_get_repo_path(worktree));
4589 30f6c0c6 2021-10-08 thomas if (repo_path == NULL) {
4590 30f6c0c6 2021-10-08 thomas error = got_error_from_errno("strdup");
4591 927df6b7 2019-02-10 stsp goto done;
4594 30f6c0c6 2021-10-08 thomas repo_path = strdup(cwd);
4595 1a1242a9 2021-04-01 kn if (repo_path == NULL) {
4596 1a1242a9 2021-04-01 kn error = got_error_from_errno("strdup");
4602 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4603 76089277 2018-04-01 stsp free(repo_path);
4604 b00d56cd 2018-04-01 stsp if (error != NULL)
4605 b00d56cd 2018-04-01 stsp goto done;
4607 cc8021af 2021-10-12 thomas if (rflag || worktree == NULL || ncommit_args > 0) {
4608 cc8021af 2021-10-12 thomas if (force_path) {
4609 cc8021af 2021-10-12 thomas error = got_error_msg(GOT_ERR_NOT_IMPL,
4610 cc8021af 2021-10-12 thomas "-P option can only be used when diffing "
4611 cc8021af 2021-10-12 thomas "a work tree");
4612 cc8021af 2021-10-12 thomas goto done;
4614 cc8021af 2021-10-12 thomas if (diff_staged) {
4615 cc8021af 2021-10-12 thomas error = got_error_msg(GOT_ERR_NOT_IMPL,
4616 cc8021af 2021-10-12 thomas "-s option can only be used when diffing "
4617 cc8021af 2021-10-12 thomas "a work tree");
4618 cc8021af 2021-10-12 thomas goto done;
4622 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4623 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4624 c02c541e 2019-03-29 stsp if (error)
4625 c02c541e 2019-03-29 stsp goto done;
4627 cc8021af 2021-10-12 thomas if ((!force_path && argc == 2) || ncommit_args > 0) {
4628 cc8021af 2021-10-12 thomas int obj_type = (ncommit_args > 0 ?
4629 cc8021af 2021-10-12 thomas GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
4630 30f6c0c6 2021-10-08 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4632 30f6c0c6 2021-10-08 thomas if (error)
4633 30f6c0c6 2021-10-08 thomas goto done;
4634 cc8021af 2021-10-12 thomas for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
4635 cc8021af 2021-10-12 thomas const char *arg;
4636 cc8021af 2021-10-12 thomas if (ncommit_args > 0)
4637 cc8021af 2021-10-12 thomas arg = commit_args[i];
4639 cc8021af 2021-10-12 thomas arg = argv[i];
4640 30f6c0c6 2021-10-08 thomas error = got_repo_match_object_id(&ids[i], &labels[i],
4641 cc8021af 2021-10-12 thomas arg, obj_type, &refs, repo);
4642 30f6c0c6 2021-10-08 thomas if (error) {
4643 30f6c0c6 2021-10-08 thomas if (error->code != GOT_ERR_NOT_REF &&
4644 30f6c0c6 2021-10-08 thomas error->code != GOT_ERR_NO_OBJ)
4645 30f6c0c6 2021-10-08 thomas goto done;
4646 cc8021af 2021-10-12 thomas if (ncommit_args > 0)
4647 cc8021af 2021-10-12 thomas goto done;
4648 30f6c0c6 2021-10-08 thomas error = NULL;
4654 cc8021af 2021-10-12 thomas if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
4655 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4656 b72f483a 2019-02-05 stsp char *id_str;
4658 cc8021af 2021-10-12 thomas if (worktree == NULL) {
4659 cc8021af 2021-10-12 thomas if (argc == 2 && ids[0] == NULL) {
4660 cc8021af 2021-10-12 thomas error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
4661 cc8021af 2021-10-12 thomas goto done;
4662 cc8021af 2021-10-12 thomas } else if (argc == 2 && ids[1] == NULL) {
4663 cc8021af 2021-10-12 thomas error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
4664 cc8021af 2021-10-12 thomas goto done;
4665 cc8021af 2021-10-12 thomas } else if (argc > 0) {
4666 cc8021af 2021-10-12 thomas error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
4667 cc8021af 2021-10-12 thomas "%s", "specified paths cannot be resolved");
4668 cc8021af 2021-10-12 thomas goto done;
4669 cc8021af 2021-10-12 thomas } else {
4670 cc8021af 2021-10-12 thomas error = got_error(GOT_ERR_NOT_WORKTREE);
4671 cc8021af 2021-10-12 thomas goto done;
4675 cc8021af 2021-10-12 thomas error = get_worktree_paths_from_argv(&paths, argc, argv,
4676 cc8021af 2021-10-12 thomas worktree);
4677 cc8021af 2021-10-12 thomas if (error)
4678 cc8021af 2021-10-12 thomas goto done;
4680 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4681 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4682 b72f483a 2019-02-05 stsp if (error)
4683 b72f483a 2019-02-05 stsp goto done;
4684 b72f483a 2019-02-05 stsp arg.repo = repo;
4685 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4686 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4687 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4688 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4689 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4690 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4691 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
4693 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
4694 f6343036 2021-06-22 stsp print_diff, &arg, check_cancelled, NULL);
4695 b72f483a 2019-02-05 stsp free(id_str);
4696 b72f483a 2019-02-05 stsp goto done;
4699 cc8021af 2021-10-12 thomas if (ncommit_args == 1) {
4700 cc8021af 2021-10-12 thomas struct got_commit_object *commit;
4701 cc8021af 2021-10-12 thomas error = got_object_open_as_commit(&commit, repo, ids[0]);
4702 cc8021af 2021-10-12 thomas if (error)
4703 30f6c0c6 2021-10-08 thomas goto done;
4705 cc8021af 2021-10-12 thomas labels[1] = labels[0];
4706 cc8021af 2021-10-12 thomas ids[1] = ids[0];
4707 cc8021af 2021-10-12 thomas if (got_object_commit_get_nparents(commit) > 0) {
4708 cc8021af 2021-10-12 thomas const struct got_object_id_queue *pids;
4709 cc8021af 2021-10-12 thomas struct got_object_qid *pid;
4710 cc8021af 2021-10-12 thomas pids = got_object_commit_get_parent_ids(commit);
4711 cc8021af 2021-10-12 thomas pid = STAILQ_FIRST(pids);
4712 cc8021af 2021-10-12 thomas ids[0] = got_object_id_dup(pid->id);
4713 cc8021af 2021-10-12 thomas if (ids[0] == NULL) {
4714 cc8021af 2021-10-12 thomas error = got_error_from_errno(
4715 cc8021af 2021-10-12 thomas "got_object_id_dup");
4716 cc8021af 2021-10-12 thomas got_object_commit_close(commit);
4717 cc8021af 2021-10-12 thomas goto done;
4719 cc8021af 2021-10-12 thomas error = got_object_id_str(&labels[0], ids[0]);
4720 cc8021af 2021-10-12 thomas if (error) {
4721 cc8021af 2021-10-12 thomas got_object_commit_close(commit);
4722 cc8021af 2021-10-12 thomas goto done;
4724 cc8021af 2021-10-12 thomas } else {
4725 cc8021af 2021-10-12 thomas ids[0] = NULL;
4726 cc8021af 2021-10-12 thomas labels[0] = strdup("/dev/null");
4727 cc8021af 2021-10-12 thomas if (labels[0] == NULL) {
4728 cc8021af 2021-10-12 thomas error = got_error_from_errno("strdup");
4729 cc8021af 2021-10-12 thomas got_object_commit_close(commit);
4730 cc8021af 2021-10-12 thomas goto done;
4734 cc8021af 2021-10-12 thomas got_object_commit_close(commit);
4737 cc8021af 2021-10-12 thomas if (ncommit_args == 0 && argc > 2) {
4738 cc8021af 2021-10-12 thomas error = got_error_msg(GOT_ERR_BAD_PATH,
4739 cc8021af 2021-10-12 thomas "path arguments cannot be used when diffing two objects");
4740 0adb7196 2019-08-11 stsp goto done;
4743 cc8021af 2021-10-12 thomas if (ids[0]) {
4744 cc8021af 2021-10-12 thomas error = got_object_get_type(&type1, repo, ids[0]);
4745 cc8021af 2021-10-12 thomas if (error)
4746 cc8021af 2021-10-12 thomas goto done;
4749 30f6c0c6 2021-10-08 thomas error = got_object_get_type(&type2, repo, ids[1]);
4750 15a94983 2018-12-23 stsp if (error)
4751 15a94983 2018-12-23 stsp goto done;
4752 cc8021af 2021-10-12 thomas if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
4753 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4754 b00d56cd 2018-04-01 stsp goto done;
4756 cc8021af 2021-10-12 thomas if (type1 == GOT_OBJ_TYPE_BLOB && argc > 0) {
4757 cc8021af 2021-10-12 thomas error = got_error_msg(GOT_ERR_OBJ_TYPE,
4758 cc8021af 2021-10-12 thomas "path arguments cannot be used when diffing blobs");
4759 cc8021af 2021-10-12 thomas goto done;
4762 cc8021af 2021-10-12 thomas for (i = 0; ncommit_args > 0 && i < argc; i++) {
4763 cc8021af 2021-10-12 thomas char *in_repo_path;
4764 cc8021af 2021-10-12 thomas struct got_pathlist_entry *new;
4765 cc8021af 2021-10-12 thomas if (worktree) {
4766 cc8021af 2021-10-12 thomas const char *prefix;
4767 cc8021af 2021-10-12 thomas char *p;
4768 cc8021af 2021-10-12 thomas error = got_worktree_resolve_path(&p, worktree,
4769 cc8021af 2021-10-12 thomas argv[i]);
4770 cc8021af 2021-10-12 thomas if (error)
4771 cc8021af 2021-10-12 thomas goto done;
4772 cc8021af 2021-10-12 thomas prefix = got_worktree_get_path_prefix(worktree);
4773 cc8021af 2021-10-12 thomas while (prefix[0] == '/')
4774 cc8021af 2021-10-12 thomas prefix++;
4775 cc8021af 2021-10-12 thomas if (asprintf(&in_repo_path, "%s%s%s", prefix,
4776 cc8021af 2021-10-12 thomas (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
4777 cc8021af 2021-10-12 thomas p) == -1) {
4778 cc8021af 2021-10-12 thomas error = got_error_from_errno("asprintf");
4779 cc8021af 2021-10-12 thomas free(p);
4780 cc8021af 2021-10-12 thomas goto done;
4782 cc8021af 2021-10-12 thomas free(p);
4783 cc8021af 2021-10-12 thomas } else {
4784 cc8021af 2021-10-12 thomas char *mapped_path, *s;
4785 cc8021af 2021-10-12 thomas error = got_repo_map_path(&mapped_path, repo, argv[i]);
4786 cc8021af 2021-10-12 thomas if (error)
4787 cc8021af 2021-10-12 thomas goto done;
4788 cc8021af 2021-10-12 thomas s = mapped_path;
4789 cc8021af 2021-10-12 thomas while (s[0] == '/')
4791 cc8021af 2021-10-12 thomas in_repo_path = strdup(s);
4792 cc8021af 2021-10-12 thomas if (in_repo_path == NULL) {
4793 cc8021af 2021-10-12 thomas error = got_error_from_errno("asprintf");
4794 cc8021af 2021-10-12 thomas free(mapped_path);
4795 cc8021af 2021-10-12 thomas goto done;
4797 cc8021af 2021-10-12 thomas free(mapped_path);
4800 cc8021af 2021-10-12 thomas error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
4801 cc8021af 2021-10-12 thomas if (error || new == NULL /* duplicate */)
4802 cc8021af 2021-10-12 thomas free(in_repo_path);
4803 cc8021af 2021-10-12 thomas if (error)
4804 cc8021af 2021-10-12 thomas goto done;
4807 f89c34e1 2022-03-13 thomas if (worktree) {
4808 f89c34e1 2022-03-13 thomas /* Release work tree lock. */
4809 f89c34e1 2022-03-13 thomas got_worktree_close(worktree);
4810 f89c34e1 2022-03-13 thomas worktree = NULL;
4813 cc8021af 2021-10-12 thomas switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
4814 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4815 30f6c0c6 2021-10-08 thomas error = got_diff_objects_as_blobs(NULL, NULL, ids[0], ids[1],
4816 64453f7e 2020-11-21 stsp NULL, NULL, diff_context, ignore_whitespace,
4817 64453f7e 2020-11-21 stsp force_text_diff, repo, stdout);
4819 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4820 30f6c0c6 2021-10-08 thomas error = got_diff_objects_as_trees(NULL, NULL, ids[0], ids[1],
4821 cc8021af 2021-10-12 thomas &paths, "", "", diff_context, ignore_whitespace,
4822 cc8021af 2021-10-12 thomas force_text_diff, repo, stdout);
4824 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4825 30f6c0c6 2021-10-08 thomas printf("diff %s %s\n", labels[0], labels[1]);
4826 30f6c0c6 2021-10-08 thomas error = got_diff_objects_as_commits(NULL, NULL, ids[0], ids[1],
4827 cc8021af 2021-10-12 thomas &paths, diff_context, ignore_whitespace, force_text_diff,
4828 cc8021af 2021-10-12 thomas repo, stdout);
4831 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4834 30f6c0c6 2021-10-08 thomas free(labels[0]);
4835 30f6c0c6 2021-10-08 thomas free(labels[1]);
4836 30f6c0c6 2021-10-08 thomas free(ids[0]);
4837 30f6c0c6 2021-10-08 thomas free(ids[1]);
4838 b72f483a 2019-02-05 stsp if (worktree)
4839 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4840 ad242220 2018-09-08 stsp if (repo) {
4841 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4842 ad242220 2018-09-08 stsp if (error == NULL)
4843 1d0f4054 2021-06-17 stsp error = close_err;
4845 30f6c0c6 2021-10-08 thomas TAILQ_FOREACH(pe, &paths, entry)
4846 30f6c0c6 2021-10-08 thomas free((char *)pe->path);
4847 30f6c0c6 2021-10-08 thomas got_pathlist_free(&paths);
4848 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4849 b00d56cd 2018-04-01 stsp return error;
4852 404c43c4 2018-06-21 stsp __dead static void
4853 404c43c4 2018-06-21 stsp usage_blame(void)
4855 9270e621 2019-02-05 stsp fprintf(stderr,
4856 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4857 404c43c4 2018-06-21 stsp getprogname());
4861 28315671 2019-08-14 stsp struct blame_line {
4862 28315671 2019-08-14 stsp int annotated;
4863 28315671 2019-08-14 stsp char *id_str;
4864 82f6abb8 2019-08-14 stsp char *committer;
4865 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4868 28315671 2019-08-14 stsp struct blame_cb_args {
4869 28315671 2019-08-14 stsp struct blame_line *lines;
4870 28315671 2019-08-14 stsp int nlines;
4871 7ef28ff8 2019-08-14 stsp int nlines_prec;
4872 28315671 2019-08-14 stsp int lineno_cur;
4873 28315671 2019-08-14 stsp off_t *line_offsets;
4875 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4878 404c43c4 2018-06-21 stsp static const struct got_error *
4879 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4881 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4882 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4883 28315671 2019-08-14 stsp struct blame_line *bline;
4884 82f6abb8 2019-08-14 stsp char *line = NULL;
4885 28315671 2019-08-14 stsp size_t linesize = 0;
4886 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4887 28315671 2019-08-14 stsp off_t offset;
4888 bcb49d15 2019-08-14 stsp struct tm tm;
4889 bcb49d15 2019-08-14 stsp time_t committer_time;
4891 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4892 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4893 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4895 28315671 2019-08-14 stsp if (sigint_received)
4896 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4898 2839f8b9 2019-08-18 stsp if (lineno == -1)
4899 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4901 28315671 2019-08-14 stsp /* Annotate this line. */
4902 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4903 28315671 2019-08-14 stsp if (bline->annotated)
4904 28315671 2019-08-14 stsp return NULL;
4905 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4907 28315671 2019-08-14 stsp return err;
4909 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4911 82f6abb8 2019-08-14 stsp goto done;
4913 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4914 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4915 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4916 bcb49d15 2019-08-14 stsp goto done;
4919 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4920 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
4921 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
4922 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4923 ec6d1a36 2021-03-21 jrick &tm) == 0) {
4924 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4925 82f6abb8 2019-08-14 stsp goto done;
4927 28315671 2019-08-14 stsp bline->annotated = 1;
4929 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4930 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4931 28315671 2019-08-14 stsp if (!bline->annotated)
4932 82f6abb8 2019-08-14 stsp goto done;
4934 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4935 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4936 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4937 82f6abb8 2019-08-14 stsp goto done;
4940 28315671 2019-08-14 stsp while (bline->annotated) {
4941 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4942 82f6abb8 2019-08-14 stsp size_t len;
4944 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4945 28315671 2019-08-14 stsp if (ferror(a->f))
4946 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4950 82f6abb8 2019-08-14 stsp committer = bline->committer;
4951 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4952 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4953 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4954 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4956 82f6abb8 2019-08-14 stsp *at = '\0';
4957 82f6abb8 2019-08-14 stsp len = strlen(committer);
4958 82f6abb8 2019-08-14 stsp if (len >= 9)
4959 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4961 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4963 28315671 2019-08-14 stsp *nl = '\0';
4964 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4965 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4967 28315671 2019-08-14 stsp a->lineno_cur++;
4968 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4971 82f6abb8 2019-08-14 stsp if (commit)
4972 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4973 28315671 2019-08-14 stsp free(line);
4974 28315671 2019-08-14 stsp return err;
4977 28315671 2019-08-14 stsp static const struct got_error *
4978 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4980 404c43c4 2018-06-21 stsp const struct got_error *error;
4981 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4982 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4983 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4984 0587e10c 2020-07-23 stsp char *link_target = NULL;
4985 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4986 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4987 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4988 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4989 28315671 2019-08-14 stsp struct blame_cb_args bca;
4990 28315671 2019-08-14 stsp int ch, obj_type, i;
4991 be659d10 2020-11-18 stsp off_t filesize;
4993 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4995 404c43c4 2018-06-21 stsp #ifndef PROFILE
4996 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4997 36e2fb66 2019-01-04 stsp NULL) == -1)
4998 404c43c4 2018-06-21 stsp err(1, "pledge");
5001 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5002 404c43c4 2018-06-21 stsp switch (ch) {
5004 404c43c4 2018-06-21 stsp commit_id_str = optarg;
5007 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
5008 66bea077 2018-08-02 stsp if (repo_path == NULL)
5009 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5011 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5014 2deda0b9 2019-03-07 stsp usage_blame();
5015 404c43c4 2018-06-21 stsp /* NOTREACHED */
5019 404c43c4 2018-06-21 stsp argc -= optind;
5020 404c43c4 2018-06-21 stsp argv += optind;
5022 a39318fd 2018-08-02 stsp if (argc == 1)
5023 404c43c4 2018-06-21 stsp path = argv[0];
5025 404c43c4 2018-06-21 stsp usage_blame();
5027 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
5028 66bea077 2018-08-02 stsp if (cwd == NULL) {
5029 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5030 66bea077 2018-08-02 stsp goto done;
5032 66bea077 2018-08-02 stsp if (repo_path == NULL) {
5033 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5034 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5035 66bea077 2018-08-02 stsp goto done;
5037 0c06baac 2019-02-05 stsp error = NULL;
5038 0c06baac 2019-02-05 stsp if (worktree) {
5039 0c06baac 2019-02-05 stsp repo_path =
5040 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5041 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
5042 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5043 1f03b8da 2020-03-20 stsp if (error)
5044 1f03b8da 2020-03-20 stsp goto done;
5047 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
5048 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
5049 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5050 0c06baac 2019-02-05 stsp goto done;
5055 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5056 c02c541e 2019-03-29 stsp if (error != NULL)
5057 404c43c4 2018-06-21 stsp goto done;
5059 0c06baac 2019-02-05 stsp if (worktree) {
5060 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
5063 01740607 2020-11-04 stsp error = got_worktree_resolve_path(&p, worktree, path);
5064 01740607 2020-11-04 stsp if (error)
5065 01740607 2020-11-04 stsp goto done;
5066 01740607 2020-11-04 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
5067 01740607 2020-11-04 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5068 01740607 2020-11-04 stsp p) == -1) {
5069 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
5071 0c06baac 2019-02-05 stsp goto done;
5074 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5076 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5077 01740607 2020-11-04 stsp if (error)
5078 01740607 2020-11-04 stsp goto done;
5079 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
5081 0c06baac 2019-02-05 stsp if (error)
5082 66bea077 2018-08-02 stsp goto done;
5084 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
5085 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
5086 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5087 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5088 404c43c4 2018-06-21 stsp if (error != NULL)
5089 66bea077 2018-08-02 stsp goto done;
5090 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5091 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
5092 404c43c4 2018-06-21 stsp if (error != NULL)
5093 66bea077 2018-08-02 stsp goto done;
5095 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5096 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5097 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5099 84de9106 2020-12-26 stsp if (error)
5100 84de9106 2020-12-26 stsp goto done;
5101 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5102 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5103 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5104 30837e32 2019-07-25 stsp if (error)
5105 66bea077 2018-08-02 stsp goto done;
5108 f89c34e1 2022-03-13 thomas if (worktree) {
5109 f89c34e1 2022-03-13 thomas /* Release work tree lock. */
5110 f89c34e1 2022-03-13 thomas got_worktree_close(worktree);
5111 f89c34e1 2022-03-13 thomas worktree = NULL;
5114 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5115 0587e10c 2020-07-23 stsp commit_id, repo);
5116 0587e10c 2020-07-23 stsp if (error)
5117 0587e10c 2020-07-23 stsp goto done;
5119 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
5120 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
5121 28315671 2019-08-14 stsp if (error)
5122 28315671 2019-08-14 stsp goto done;
5124 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
5125 28315671 2019-08-14 stsp if (error)
5126 28315671 2019-08-14 stsp goto done;
5128 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5129 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
5130 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
5131 28315671 2019-08-14 stsp goto done;
5134 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
5135 28315671 2019-08-14 stsp if (error)
5136 28315671 2019-08-14 stsp goto done;
5137 28315671 2019-08-14 stsp bca.f = got_opentemp();
5138 28315671 2019-08-14 stsp if (bca.f == NULL) {
5139 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
5140 28315671 2019-08-14 stsp goto done;
5142 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5143 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
5144 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
5145 28315671 2019-08-14 stsp goto done;
5147 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5148 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
5149 b02560ec 2019-08-19 stsp bca.nlines--;
5151 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5152 28315671 2019-08-14 stsp if (bca.lines == NULL) {
5153 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
5154 28315671 2019-08-14 stsp goto done;
5156 28315671 2019-08-14 stsp bca.lineno_cur = 1;
5157 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
5158 7ef28ff8 2019-08-14 stsp i = bca.nlines;
5159 7ef28ff8 2019-08-14 stsp while (i > 0) {
5161 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
5163 82f6abb8 2019-08-14 stsp bca.repo = repo;
5165 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5166 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
5168 66bea077 2018-08-02 stsp free(in_repo_path);
5169 0587e10c 2020-07-23 stsp free(link_target);
5170 66bea077 2018-08-02 stsp free(repo_path);
5171 66bea077 2018-08-02 stsp free(cwd);
5172 404c43c4 2018-06-21 stsp free(commit_id);
5173 28315671 2019-08-14 stsp free(obj_id);
5175 28315671 2019-08-14 stsp got_object_blob_close(blob);
5176 0c06baac 2019-02-05 stsp if (worktree)
5177 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
5178 ad242220 2018-09-08 stsp if (repo) {
5179 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5180 ad242220 2018-09-08 stsp if (error == NULL)
5181 1d0f4054 2021-06-17 stsp error = close_err;
5183 3affba96 2019-09-22 stsp if (bca.lines) {
5184 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
5185 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
5186 3affba96 2019-09-22 stsp free(bline->id_str);
5187 3affba96 2019-09-22 stsp free(bline->committer);
5189 3affba96 2019-09-22 stsp free(bca.lines);
5191 28315671 2019-08-14 stsp free(bca.line_offsets);
5192 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
5193 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
5194 404c43c4 2018-06-21 stsp return error;
5197 5de5890b 2018-10-18 stsp __dead static void
5198 5de5890b 2018-10-18 stsp usage_tree(void)
5200 c1669e2e 2019-01-09 stsp fprintf(stderr,
5201 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5202 5de5890b 2018-10-18 stsp getprogname());
5206 0d6c6ee3 2020-05-20 stsp static const struct got_error *
5207 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
5208 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
5210 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
5211 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
5212 848d6979 2019-08-12 stsp const char *modestr = "";
5213 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
5214 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
5216 c1669e2e 2019-01-09 stsp path += strlen(root_path);
5217 c1669e2e 2019-01-09 stsp while (path[0] == '/')
5220 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5221 63c5ca5d 2019-08-24 stsp modestr = "$";
5222 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5225 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5227 0d6c6ee3 2020-05-20 stsp return err;
5228 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5229 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5230 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5233 848d6979 2019-08-12 stsp modestr = "@";
5235 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5236 848d6979 2019-08-12 stsp modestr = "/";
5237 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5238 848d6979 2019-08-12 stsp modestr = "*";
5240 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5241 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5242 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
5244 0d6c6ee3 2020-05-20 stsp free(link_target);
5245 0d6c6ee3 2020-05-20 stsp return NULL;
5248 5de5890b 2018-10-18 stsp static const struct got_error *
5249 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
5250 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
5251 c1669e2e 2019-01-09 stsp struct got_repository *repo)
5253 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
5254 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
5255 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
5256 56e0773d 2019-11-28 stsp int nentries, i;
5258 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
5260 5de5890b 2018-10-18 stsp goto done;
5262 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
5264 5de5890b 2018-10-18 stsp goto done;
5265 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
5266 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
5267 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5268 5de5890b 2018-10-18 stsp char *id = NULL;
5270 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
5273 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
5274 5de5890b 2018-10-18 stsp if (show_ids) {
5275 5de5890b 2018-10-18 stsp char *id_str;
5276 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5277 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5279 5de5890b 2018-10-18 stsp goto done;
5280 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
5281 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5282 5de5890b 2018-10-18 stsp free(id_str);
5283 5de5890b 2018-10-18 stsp goto done;
5285 5de5890b 2018-10-18 stsp free(id_str);
5287 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
5290 0d6c6ee3 2020-05-20 stsp goto done;
5292 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5293 c1669e2e 2019-01-09 stsp char *child_path;
5294 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
5295 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
5296 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
5297 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5298 c1669e2e 2019-01-09 stsp goto done;
5300 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
5301 c1669e2e 2019-01-09 stsp root_path, repo);
5302 c1669e2e 2019-01-09 stsp free(child_path);
5304 c1669e2e 2019-01-09 stsp goto done;
5309 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
5310 5de5890b 2018-10-18 stsp free(tree_id);
5311 5de5890b 2018-10-18 stsp return err;
5314 5de5890b 2018-10-18 stsp static const struct got_error *
5315 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
5317 5de5890b 2018-10-18 stsp const struct got_error *error;
5318 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
5319 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
5320 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
5321 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5322 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
5323 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
5324 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
5327 5de5890b 2018-10-18 stsp #ifndef PROFILE
5328 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5329 0f8d269b 2019-01-04 stsp NULL) == -1)
5330 5de5890b 2018-10-18 stsp err(1, "pledge");
5333 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5334 5de5890b 2018-10-18 stsp switch (ch) {
5336 5de5890b 2018-10-18 stsp commit_id_str = optarg;
5339 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
5340 5de5890b 2018-10-18 stsp if (repo_path == NULL)
5341 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5343 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5346 5de5890b 2018-10-18 stsp show_ids = 1;
5349 c1669e2e 2019-01-09 stsp recurse = 1;
5352 2deda0b9 2019-03-07 stsp usage_tree();
5353 5de5890b 2018-10-18 stsp /* NOTREACHED */
5357 5de5890b 2018-10-18 stsp argc -= optind;
5358 5de5890b 2018-10-18 stsp argv += optind;
5360 5de5890b 2018-10-18 stsp if (argc == 1)
5361 5de5890b 2018-10-18 stsp path = argv[0];
5362 5de5890b 2018-10-18 stsp else if (argc > 1)
5363 5de5890b 2018-10-18 stsp usage_tree();
5365 7a2c19d6 2019-02-05 stsp path = NULL;
5367 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
5368 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
5369 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5370 9bf7a39b 2019-02-05 stsp goto done;
5372 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
5373 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5374 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5375 7a2c19d6 2019-02-05 stsp goto done;
5377 7a2c19d6 2019-02-05 stsp error = NULL;
5378 7a2c19d6 2019-02-05 stsp if (worktree) {
5379 7a2c19d6 2019-02-05 stsp repo_path =
5380 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5381 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
5382 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5383 7a2c19d6 2019-02-05 stsp if (error)
5384 7a2c19d6 2019-02-05 stsp goto done;
5386 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
5387 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
5388 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5389 7a2c19d6 2019-02-05 stsp goto done;
5394 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5395 5de5890b 2018-10-18 stsp if (error != NULL)
5396 c02c541e 2019-03-29 stsp goto done;
5398 4fedbf4c 2020-11-07 stsp if (worktree) {
5399 4fedbf4c 2020-11-07 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
5402 4fedbf4c 2020-11-07 stsp if (path == NULL)
5403 4fedbf4c 2020-11-07 stsp path = "";
5404 4fedbf4c 2020-11-07 stsp error = got_worktree_resolve_path(&p, worktree, path);
5405 4fedbf4c 2020-11-07 stsp if (error)
5406 4fedbf4c 2020-11-07 stsp goto done;
5407 4fedbf4c 2020-11-07 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
5408 4fedbf4c 2020-11-07 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5409 4fedbf4c 2020-11-07 stsp p) == -1) {
5410 4fedbf4c 2020-11-07 stsp error = got_error_from_errno("asprintf");
5412 4fedbf4c 2020-11-07 stsp goto done;
5415 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5416 4fedbf4c 2020-11-07 stsp if (error)
5417 4fedbf4c 2020-11-07 stsp goto done;
5419 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5420 4fedbf4c 2020-11-07 stsp if (error)
5421 4fedbf4c 2020-11-07 stsp goto done;
5422 4fedbf4c 2020-11-07 stsp if (path == NULL)
5423 9bf7a39b 2019-02-05 stsp path = "/";
5424 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
5425 9bf7a39b 2019-02-05 stsp if (error != NULL)
5426 9bf7a39b 2019-02-05 stsp goto done;
5429 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
5430 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
5431 4e0a20a4 2020-03-23 tracey if (worktree)
5432 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
5434 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
5435 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
5436 5de5890b 2018-10-18 stsp if (error != NULL)
5437 5de5890b 2018-10-18 stsp goto done;
5438 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5439 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
5440 5de5890b 2018-10-18 stsp if (error != NULL)
5441 5de5890b 2018-10-18 stsp goto done;
5443 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5444 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5445 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5447 84de9106 2020-12-26 stsp if (error)
5448 84de9106 2020-12-26 stsp goto done;
5449 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5450 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5451 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5452 30837e32 2019-07-25 stsp if (error)
5453 5de5890b 2018-10-18 stsp goto done;
5456 f89c34e1 2022-03-13 thomas if (worktree) {
5457 f89c34e1 2022-03-13 thomas /* Release work tree lock. */
5458 f89c34e1 2022-03-13 thomas got_worktree_close(worktree);
5459 f89c34e1 2022-03-13 thomas worktree = NULL;
5462 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5463 c1669e2e 2019-01-09 stsp in_repo_path, repo);
5465 5de5890b 2018-10-18 stsp free(in_repo_path);
5466 5de5890b 2018-10-18 stsp free(repo_path);
5467 5de5890b 2018-10-18 stsp free(cwd);
5468 5de5890b 2018-10-18 stsp free(commit_id);
5469 7a2c19d6 2019-02-05 stsp if (worktree)
5470 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
5471 5de5890b 2018-10-18 stsp if (repo) {
5472 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5473 5de5890b 2018-10-18 stsp if (error == NULL)
5474 1d0f4054 2021-06-17 stsp error = close_err;
5476 5de5890b 2018-10-18 stsp return error;
5479 6bad629b 2019-02-04 stsp __dead static void
5480 6bad629b 2019-02-04 stsp usage_status(void)
5482 00357e4d 2021-09-14 tracey fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5483 00357e4d 2021-09-14 tracey "[-S status-codes] [path ...]\n", getprogname());
5487 00357e4d 2021-09-14 tracey struct got_status_arg {
5488 00357e4d 2021-09-14 tracey char *status_codes;
5489 00357e4d 2021-09-14 tracey int suppress;
5492 b72f483a 2019-02-05 stsp static const struct got_error *
5493 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
5494 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5495 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5496 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5498 00357e4d 2021-09-14 tracey struct got_status_arg *st = arg;
5500 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5501 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
5502 00357e4d 2021-09-14 tracey if (st != NULL && st->status_codes) {
5503 00357e4d 2021-09-14 tracey size_t ncodes = strlen(st->status_codes);
5504 00357e4d 2021-09-14 tracey int i, j = 0;
5506 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
5507 00357e4d 2021-09-14 tracey if (st->suppress) {
5508 00357e4d 2021-09-14 tracey if (status == st->status_codes[i] ||
5509 00357e4d 2021-09-14 tracey staged_status == st->status_codes[i]) {
5511 00357e4d 2021-09-14 tracey continue;
5513 00357e4d 2021-09-14 tracey } else {
5514 00357e4d 2021-09-14 tracey if (status == st->status_codes[i] ||
5515 00357e4d 2021-09-14 tracey staged_status == st->status_codes[i])
5520 00357e4d 2021-09-14 tracey if (st->suppress && j == 0)
5521 00357e4d 2021-09-14 tracey goto print;
5523 081470ac 2020-08-13 stsp if (i == ncodes)
5524 081470ac 2020-08-13 stsp return NULL;
5527 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
5528 b72f483a 2019-02-05 stsp return NULL;
5531 6bad629b 2019-02-04 stsp static const struct got_error *
5532 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
5534 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
5535 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
5536 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
5537 00357e4d 2021-09-14 tracey struct got_status_arg st;
5538 00357e4d 2021-09-14 tracey char *cwd = NULL;
5539 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
5540 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
5541 f6343036 2021-06-22 stsp int ch, i, no_ignores = 0;
5543 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
5545 00357e4d 2021-09-14 tracey memset(&st, 0, sizeof(st));
5546 00357e4d 2021-09-14 tracey st.status_codes = NULL;
5547 00357e4d 2021-09-14 tracey st.suppress = 0;
5549 00357e4d 2021-09-14 tracey while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
5550 6bad629b 2019-02-04 stsp switch (ch) {
5552 f6343036 2021-06-22 stsp no_ignores = 1;
5555 788d4a19 2021-09-14 stsp if (st.status_codes != NULL && st.suppress == 0)
5556 788d4a19 2021-09-14 stsp option_conflict('S', 's');
5557 788d4a19 2021-09-14 stsp st.suppress = 1;
5558 788d4a19 2021-09-14 stsp /* fallthrough */
5560 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
5561 081470ac 2020-08-13 stsp switch (optarg[i]) {
5562 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
5563 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
5564 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
5565 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
5566 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
5567 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
5568 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
5569 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
5570 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
5573 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
5574 081470ac 2020-08-13 stsp optarg[i]);
5577 788d4a19 2021-09-14 stsp if (ch == 's' && st.suppress)
5578 b043307b 2021-09-14 stsp option_conflict('s', 'S');
5579 00357e4d 2021-09-14 tracey st.status_codes = optarg;
5582 2deda0b9 2019-03-07 stsp usage_status();
5583 6bad629b 2019-02-04 stsp /* NOTREACHED */
5587 6bad629b 2019-02-04 stsp argc -= optind;
5588 6bad629b 2019-02-04 stsp argv += optind;
5590 6bad629b 2019-02-04 stsp #ifndef PROFILE
5591 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5592 6bad629b 2019-02-04 stsp NULL) == -1)
5593 6bad629b 2019-02-04 stsp err(1, "pledge");
5595 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
5596 927df6b7 2019-02-10 stsp if (cwd == NULL) {
5597 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5598 927df6b7 2019-02-10 stsp goto done;
5601 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
5602 fa51e947 2020-03-27 stsp if (error) {
5603 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5604 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
5605 a5edda0a 2019-07-27 stsp goto done;
5608 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5610 6bad629b 2019-02-04 stsp if (error != NULL)
5611 6bad629b 2019-02-04 stsp goto done;
5613 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5614 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5615 087fb88c 2019-08-04 stsp if (error)
5616 087fb88c 2019-08-04 stsp goto done;
5618 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5619 6bad629b 2019-02-04 stsp if (error)
5620 6bad629b 2019-02-04 stsp goto done;
5622 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, no_ignores,
5623 00357e4d 2021-09-14 tracey print_status, &st, check_cancelled, NULL);
5625 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
5626 a5edda0a 2019-07-27 stsp free((char *)pe->path);
5627 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
5628 927df6b7 2019-02-10 stsp free(cwd);
5629 d0eebce4 2019-03-11 stsp return error;
5632 d0eebce4 2019-03-11 stsp __dead static void
5633 d0eebce4 2019-03-11 stsp usage_ref(void)
5635 d0eebce4 2019-03-11 stsp fprintf(stderr,
5636 2bf0fa54 2021-11-20 thomas "usage: %s ref [-r repository] [-l] [-t] [-c object] "
5637 2bf0fa54 2021-11-20 thomas "[-s reference] [-d] [name]\n",
5638 d0eebce4 2019-03-11 stsp getprogname());
5642 d0eebce4 2019-03-11 stsp static const struct got_error *
5643 2bf0fa54 2021-11-20 thomas list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
5645 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
5646 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
5647 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
5649 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5650 2bf0fa54 2021-11-20 thomas err = got_ref_list(&refs, repo, refname, sort_by_time ?
5651 2bf0fa54 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
5654 d0eebce4 2019-03-11 stsp return err;
5656 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
5657 d0eebce4 2019-03-11 stsp char *refstr;
5658 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
5659 d0eebce4 2019-03-11 stsp if (refstr == NULL)
5660 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
5661 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5662 d0eebce4 2019-03-11 stsp free(refstr);
5665 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5666 d0eebce4 2019-03-11 stsp return NULL;
5669 d0eebce4 2019-03-11 stsp static const struct got_error *
5670 161728eb 2021-07-24 stsp delete_ref_by_name(struct got_repository *repo, const char *refname)
5672 161728eb 2021-07-24 stsp const struct got_error *err;
5673 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5675 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5677 d0eebce4 2019-03-11 stsp return err;
5679 161728eb 2021-07-24 stsp err = delete_ref(repo, ref);
5680 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5681 d0eebce4 2019-03-11 stsp return err;
5684 d0eebce4 2019-03-11 stsp static const struct got_error *
5685 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
5687 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5688 f111bd6a 2022-03-11 thomas struct got_object_id *id = NULL;
5689 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
5690 f111bd6a 2022-03-11 thomas struct got_reflist_head refs;
5693 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5694 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
5695 d1644381 2019-07-14 stsp * an unintended typo.
5697 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5698 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5700 f111bd6a 2022-03-11 thomas TAILQ_INIT(&refs);
5701 f111bd6a 2022-03-11 thomas err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5702 f111bd6a 2022-03-11 thomas if (err)
5703 f111bd6a 2022-03-11 thomas goto done;
5704 f111bd6a 2022-03-11 thomas err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
5705 f111bd6a 2022-03-11 thomas &refs, repo);
5706 f111bd6a 2022-03-11 thomas got_ref_list_free(&refs);
5707 f111bd6a 2022-03-11 thomas if (err)
5708 f111bd6a 2022-03-11 thomas goto done;
5710 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5712 d0eebce4 2019-03-11 stsp goto done;
5714 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5717 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5719 d1c1ae5f 2019-08-12 stsp return err;
5722 d1c1ae5f 2019-08-12 stsp static const struct got_error *
5723 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
5725 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
5726 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
5727 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
5730 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5731 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
5732 d1c1ae5f 2019-08-12 stsp * an unintended typo.
5734 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5735 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5737 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5739 d1c1ae5f 2019-08-12 stsp return err;
5741 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5743 d1c1ae5f 2019-08-12 stsp goto done;
5745 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5747 d1c1ae5f 2019-08-12 stsp if (target_ref)
5748 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5750 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5751 d0eebce4 2019-03-11 stsp return err;
5754 d0eebce4 2019-03-11 stsp static const struct got_error *
5755 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5757 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
5758 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
5759 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
5760 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
5761 2bf0fa54 2021-11-20 thomas int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
5762 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
5763 b2070a3f 2020-03-22 stsp char *refname = NULL;
5765 2bf0fa54 2021-11-20 thomas while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
5766 d0eebce4 2019-03-11 stsp switch (ch) {
5768 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5771 e31abbf2 2020-03-22 stsp do_delete = 1;
5774 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5775 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5776 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5778 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5781 d0eebce4 2019-03-11 stsp do_list = 1;
5784 e31abbf2 2020-03-22 stsp symref_target = optarg;
5786 2bf0fa54 2021-11-20 thomas case 't':
5787 2bf0fa54 2021-11-20 thomas sort_by_time = 1;
5790 d0eebce4 2019-03-11 stsp usage_ref();
5791 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5795 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5796 ff69268e 2020-12-13 stsp option_conflict('c', 'l');
5797 ff69268e 2020-12-13 stsp if (obj_arg && do_delete)
5798 ff69268e 2020-12-13 stsp option_conflict('c', 'd');
5799 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5800 ff69268e 2020-12-13 stsp option_conflict('c', 's');
5801 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5802 ff69268e 2020-12-13 stsp option_conflict('s', 'd');
5803 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5804 ff69268e 2020-12-13 stsp option_conflict('s', 'l');
5805 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5806 ff69268e 2020-12-13 stsp option_conflict('d', 'l');
5807 2bf0fa54 2021-11-20 thomas if (sort_by_time && !do_list)
5808 2bf0fa54 2021-11-20 thomas errx(1, "-t option requires -l option");
5810 d0eebce4 2019-03-11 stsp argc -= optind;
5811 d0eebce4 2019-03-11 stsp argv += optind;
5813 e31abbf2 2020-03-22 stsp if (do_list) {
5814 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5815 d0eebce4 2019-03-11 stsp usage_ref();
5816 b2070a3f 2020-03-22 stsp if (argc == 1) {
5817 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5818 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5819 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5820 b2070a3f 2020-03-22 stsp goto done;
5824 e31abbf2 2020-03-22 stsp if (argc != 1)
5825 e31abbf2 2020-03-22 stsp usage_ref();
5826 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5827 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5828 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5829 b2070a3f 2020-03-22 stsp goto done;
5833 b2070a3f 2020-03-22 stsp if (refname)
5834 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5836 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5837 e0b57350 2019-03-12 stsp if (do_list) {
5838 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5839 e0b57350 2019-03-12 stsp NULL) == -1)
5840 e0b57350 2019-03-12 stsp err(1, "pledge");
5842 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5843 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5844 e0b57350 2019-03-12 stsp err(1, "pledge");
5847 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5848 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5849 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5850 d0eebce4 2019-03-11 stsp goto done;
5853 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5854 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5855 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5856 d0eebce4 2019-03-11 stsp goto done;
5858 d0eebce4 2019-03-11 stsp error = NULL;
5859 d0eebce4 2019-03-11 stsp if (worktree) {
5860 d0eebce4 2019-03-11 stsp repo_path =
5861 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5862 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5863 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5864 d0eebce4 2019-03-11 stsp if (error)
5865 d0eebce4 2019-03-11 stsp goto done;
5867 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5868 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5869 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5870 d0eebce4 2019-03-11 stsp goto done;
5875 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5876 d0eebce4 2019-03-11 stsp if (error != NULL)
5877 d0eebce4 2019-03-11 stsp goto done;
5879 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5880 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5881 c02c541e 2019-03-29 stsp if (error)
5882 c02c541e 2019-03-29 stsp goto done;
5884 d0eebce4 2019-03-11 stsp if (do_list)
5885 2bf0fa54 2021-11-20 thomas error = list_refs(repo, refname, sort_by_time);
5886 e31abbf2 2020-03-22 stsp else if (do_delete)
5887 161728eb 2021-07-24 stsp error = delete_ref_by_name(repo, refname);
5888 e31abbf2 2020-03-22 stsp else if (symref_target)
5889 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5891 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5892 e31abbf2 2020-03-22 stsp usage_ref();
5893 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5896 b2070a3f 2020-03-22 stsp free(refname);
5897 1d0f4054 2021-06-17 stsp if (repo) {
5898 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5899 1d0f4054 2021-06-17 stsp if (error == NULL)
5900 1d0f4054 2021-06-17 stsp error = close_err;
5902 4e759de4 2019-06-26 stsp if (worktree)
5903 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5904 4e759de4 2019-06-26 stsp free(cwd);
5905 4e759de4 2019-06-26 stsp free(repo_path);
5906 4e759de4 2019-06-26 stsp return error;
5909 4e759de4 2019-06-26 stsp __dead static void
5910 4e759de4 2019-06-26 stsp usage_branch(void)
5912 4e759de4 2019-06-26 stsp fprintf(stderr,
5913 a0b48eaf 2021-11-20 thomas "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-t] "
5914 a0b48eaf 2021-11-20 thomas "[-n] [name]\n", getprogname());
5918 4e759de4 2019-06-26 stsp static const struct got_error *
5919 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5920 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5922 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5923 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5924 4e99b47e 2019-10-04 stsp char *refstr;
5926 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5927 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5928 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5929 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5931 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5933 4e99b47e 2019-10-04 stsp return err;
5934 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5935 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5936 4e99b47e 2019-10-04 stsp marker = "* ";
5938 4e99b47e 2019-10-04 stsp marker = "~ ";
5942 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5943 4e99b47e 2019-10-04 stsp refname += 11;
5944 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5945 4e99b47e 2019-10-04 stsp refname += 18;
5946 34d4e04c 2021-02-08 stsp if (strncmp(refname, "refs/remotes/", 13) == 0)
5947 34d4e04c 2021-02-08 stsp refname += 13;
5949 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5950 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5951 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5953 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5954 4e99b47e 2019-10-04 stsp free(refstr);
5955 4e99b47e 2019-10-04 stsp return NULL;
5958 4e99b47e 2019-10-04 stsp static const struct got_error *
5959 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5961 ad89fa31 2019-10-04 stsp const char *refname;
5963 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5964 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5966 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5968 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5969 ad89fa31 2019-10-04 stsp refname += 11;
5970 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5971 ad89fa31 2019-10-04 stsp refname += 18;
5973 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5975 ad89fa31 2019-10-04 stsp return NULL;
5978 ad89fa31 2019-10-04 stsp static const struct got_error *
5979 a0b48eaf 2021-11-20 thomas list_branches(struct got_repository *repo, struct got_worktree *worktree,
5980 a0b48eaf 2021-11-20 thomas int sort_by_time)
5982 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5983 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5984 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5985 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5986 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5988 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5990 4e99b47e 2019-10-04 stsp if (worktree) {
5991 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5992 4e99b47e 2019-10-04 stsp worktree);
5994 4e99b47e 2019-10-04 stsp return err;
5996 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5997 4e99b47e 2019-10-04 stsp worktree);
5999 4e99b47e 2019-10-04 stsp return err;
6001 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
6002 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
6003 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
6005 4e99b47e 2019-10-04 stsp return err;
6006 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
6007 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
6011 a0b48eaf 2021-11-20 thomas err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6012 a0b48eaf 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6015 4e759de4 2019-06-26 stsp return err;
6017 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry)
6018 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
6020 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
6022 a0b48eaf 2021-11-20 thomas err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6023 a0b48eaf 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6026 34d4e04c 2021-02-08 stsp return err;
6028 34d4e04c 2021-02-08 stsp TAILQ_FOREACH(re, &refs, entry)
6029 34d4e04c 2021-02-08 stsp list_branch(repo, worktree, re->ref);
6031 34d4e04c 2021-02-08 stsp got_ref_list_free(&refs);
6033 4e759de4 2019-06-26 stsp return NULL;
6036 4e759de4 2019-06-26 stsp static const struct got_error *
6037 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6038 45cd4e47 2019-08-25 stsp const char *branch_name)
6040 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
6041 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
6042 2f1457c6 2021-08-27 stsp char *refname, *remote_refname = NULL;
6044 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "refs/", 5) == 0)
6045 2f1457c6 2021-08-27 stsp branch_name += 5;
6046 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "heads/", 6) == 0)
6047 2f1457c6 2021-08-27 stsp branch_name += 6;
6048 2f1457c6 2021-08-27 stsp else if (strncmp(branch_name, "remotes/", 8) == 0)
6049 2f1457c6 2021-08-27 stsp branch_name += 8;
6051 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6052 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
6054 2f1457c6 2021-08-27 stsp if (asprintf(&remote_refname, "refs/remotes/%s",
6055 2f1457c6 2021-08-27 stsp branch_name) == -1) {
6056 2f1457c6 2021-08-27 stsp err = got_error_from_errno("asprintf");
6057 4e759de4 2019-06-26 stsp goto done;
6060 2f1457c6 2021-08-27 stsp err = got_ref_open(&ref, repo, refname, 0);
6061 2f1457c6 2021-08-27 stsp if (err) {
6062 2f1457c6 2021-08-27 stsp const struct got_error *err2;
6063 2f1457c6 2021-08-27 stsp if (err->code != GOT_ERR_NOT_REF)
6064 2f1457c6 2021-08-27 stsp goto done;
6066 2f1457c6 2021-08-27 stsp * Keep 'err' intact such that if neither branch exists
6067 2f1457c6 2021-08-27 stsp * we report "refs/heads" rather than "refs/remotes" in
6068 2f1457c6 2021-08-27 stsp * our error message.
6070 2f1457c6 2021-08-27 stsp err2 = got_ref_open(&ref, repo, remote_refname, 0);
6072 2f1457c6 2021-08-27 stsp goto done;
6073 2f1457c6 2021-08-27 stsp err = NULL;
6076 45cd4e47 2019-08-25 stsp if (worktree &&
6077 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
6078 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
6079 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6080 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
6081 45cd4e47 2019-08-25 stsp goto done;
6084 978a28a1 2021-09-04 naddy err = delete_ref(repo, ref);
6087 45cd4e47 2019-08-25 stsp got_ref_close(ref);
6088 4e759de4 2019-06-26 stsp free(refname);
6089 2f1457c6 2021-08-27 stsp free(remote_refname);
6090 4e759de4 2019-06-26 stsp return err;
6093 4e759de4 2019-06-26 stsp static const struct got_error *
6094 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
6095 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
6097 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
6098 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
6099 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
6102 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
6103 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
6104 d3f84d51 2019-07-11 stsp * an unintended typo.
6106 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
6107 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6109 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
6110 2f1457c6 2021-08-27 stsp branch_name += 11;
6112 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6113 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
6114 62d463ca 2020-10-20 naddy goto done;
6117 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
6118 4e759de4 2019-06-26 stsp if (err == NULL) {
6119 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
6120 4e759de4 2019-06-26 stsp goto done;
6121 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
6122 4e759de4 2019-06-26 stsp goto done;
6124 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
6126 4e759de4 2019-06-26 stsp goto done;
6128 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
6131 4e759de4 2019-06-26 stsp got_ref_close(ref);
6132 4e759de4 2019-06-26 stsp free(base_refname);
6133 4e759de4 2019-06-26 stsp free(refname);
6134 4e759de4 2019-06-26 stsp return err;
6137 4e759de4 2019-06-26 stsp static const struct got_error *
6138 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
6140 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
6141 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
6142 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
6143 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
6144 a0b48eaf 2021-11-20 thomas int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6145 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
6146 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
6147 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
6148 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
6149 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
6150 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
6152 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
6154 a0b48eaf 2021-11-20 thomas while ((ch = getopt(argc, argv, "c:d:r:lnt")) != -1) {
6155 4e759de4 2019-06-26 stsp switch (ch) {
6157 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
6160 4e759de4 2019-06-26 stsp delref = optarg;
6163 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
6164 4e759de4 2019-06-26 stsp if (repo_path == NULL)
6165 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6167 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
6170 4e759de4 2019-06-26 stsp do_list = 1;
6173 da76fce2 2020-02-24 stsp do_update = 0;
6175 a0b48eaf 2021-11-20 thomas case 't':
6176 a0b48eaf 2021-11-20 thomas sort_by_time = 1;
6179 4e759de4 2019-06-26 stsp usage_branch();
6180 4e759de4 2019-06-26 stsp /* NOTREACHED */
6184 4e759de4 2019-06-26 stsp if (do_list && delref)
6185 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
6186 a0b48eaf 2021-11-20 thomas if (sort_by_time && !do_list)
6187 a0b48eaf 2021-11-20 thomas errx(1, "-t option requires -l option");
6189 4e759de4 2019-06-26 stsp argc -= optind;
6190 4e759de4 2019-06-26 stsp argv += optind;
6192 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
6193 ad89fa31 2019-10-04 stsp do_show = 1;
6195 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
6196 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
6198 4e759de4 2019-06-26 stsp if (do_list || delref) {
6199 4e759de4 2019-06-26 stsp if (argc > 0)
6200 4e759de4 2019-06-26 stsp usage_branch();
6201 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
6202 4e759de4 2019-06-26 stsp usage_branch();
6204 4e759de4 2019-06-26 stsp #ifndef PROFILE
6205 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
6206 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6207 4e759de4 2019-06-26 stsp NULL) == -1)
6208 4e759de4 2019-06-26 stsp err(1, "pledge");
6210 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6211 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
6212 4e759de4 2019-06-26 stsp err(1, "pledge");
6215 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
6216 4e759de4 2019-06-26 stsp if (cwd == NULL) {
6217 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
6218 4e759de4 2019-06-26 stsp goto done;
6221 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
6222 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
6223 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6224 4e759de4 2019-06-26 stsp goto done;
6226 4e759de4 2019-06-26 stsp error = NULL;
6227 4e759de4 2019-06-26 stsp if (worktree) {
6228 4e759de4 2019-06-26 stsp repo_path =
6229 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
6230 4e759de4 2019-06-26 stsp if (repo_path == NULL)
6231 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
6232 4e759de4 2019-06-26 stsp if (error)
6233 4e759de4 2019-06-26 stsp goto done;
6235 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
6236 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
6237 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
6238 4e759de4 2019-06-26 stsp goto done;
6243 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6244 4e759de4 2019-06-26 stsp if (error != NULL)
6245 4e759de4 2019-06-26 stsp goto done;
6247 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
6248 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
6249 4e759de4 2019-06-26 stsp if (error)
6250 4e759de4 2019-06-26 stsp goto done;
6252 ad89fa31 2019-10-04 stsp if (do_show)
6253 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
6254 ad89fa31 2019-10-04 stsp else if (do_list)
6255 a0b48eaf 2021-11-20 thomas error = list_branches(repo, worktree, sort_by_time);
6256 4e759de4 2019-06-26 stsp else if (delref)
6257 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
6259 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6260 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6261 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6263 84de9106 2020-12-26 stsp if (error)
6264 84de9106 2020-12-26 stsp goto done;
6265 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
6266 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
6267 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
6268 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
6269 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6270 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6271 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
6272 a74f7e83 2019-11-10 stsp if (error)
6273 a74f7e83 2019-11-10 stsp goto done;
6274 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
6275 da76fce2 2020-02-24 stsp if (error)
6276 da76fce2 2020-02-24 stsp goto done;
6277 da76fce2 2020-02-24 stsp if (worktree && do_update) {
6278 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6279 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
6281 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
6282 da76fce2 2020-02-24 stsp if (error)
6283 da76fce2 2020-02-24 stsp goto done;
6284 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
6285 da76fce2 2020-02-24 stsp worktree);
6286 da76fce2 2020-02-24 stsp if (error)
6287 da76fce2 2020-02-24 stsp goto done;
6288 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6290 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
6291 da76fce2 2020-02-24 stsp goto done;
6293 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
6294 da76fce2 2020-02-24 stsp free(branch_refname);
6295 da76fce2 2020-02-24 stsp if (error)
6296 da76fce2 2020-02-24 stsp goto done;
6297 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
6299 da76fce2 2020-02-24 stsp if (error)
6300 da76fce2 2020-02-24 stsp goto done;
6301 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
6302 da76fce2 2020-02-24 stsp commit_id);
6303 da76fce2 2020-02-24 stsp if (error)
6304 da76fce2 2020-02-24 stsp goto done;
6305 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6306 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
6307 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
6309 da76fce2 2020-02-24 stsp if (error)
6310 da76fce2 2020-02-24 stsp goto done;
6311 4f3c844b 2021-09-14 stsp if (upa.did_something) {
6312 4f3c844b 2021-09-14 stsp printf("Updated to %s: %s\n",
6313 4f3c844b 2021-09-14 stsp got_worktree_get_head_ref_name(worktree),
6314 4f3c844b 2021-09-14 stsp commit_id_str);
6316 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6321 da76fce2 2020-02-24 stsp got_ref_close(ref);
6322 1d0f4054 2021-06-17 stsp if (repo) {
6323 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6324 1d0f4054 2021-06-17 stsp if (error == NULL)
6325 1d0f4054 2021-06-17 stsp error = close_err;
6327 d0eebce4 2019-03-11 stsp if (worktree)
6328 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
6329 d0eebce4 2019-03-11 stsp free(cwd);
6330 d0eebce4 2019-03-11 stsp free(repo_path);
6331 da76fce2 2020-02-24 stsp free(commit_id);
6332 da76fce2 2020-02-24 stsp free(commit_id_str);
6333 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
6334 da76fce2 2020-02-24 stsp free((char *)pe->path);
6335 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
6336 d00136be 2019-03-26 stsp return error;
6340 d00136be 2019-03-26 stsp __dead static void
6341 8e7bd50a 2019-08-22 stsp usage_tag(void)
6343 8e7bd50a 2019-08-22 stsp fprintf(stderr,
6344 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
6345 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
6350 b8bad2ba 2019-08-23 stsp static const struct got_error *
6351 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6353 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
6354 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
6355 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
6356 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
6357 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
6359 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(re, tags, entry) {
6360 dbdddfee 2021-06-23 naddy se = STAILQ_FIRST(sorted);
6361 b8bad2ba 2019-08-23 stsp if (se == NULL) {
6362 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6364 b8bad2ba 2019-08-23 stsp return err;
6365 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(sorted, new, entry);
6368 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
6371 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
6372 b8bad2ba 2019-08-23 stsp free(re_id);
6375 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
6376 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
6379 b8bad2ba 2019-08-23 stsp while (se) {
6380 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
6383 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
6384 b8bad2ba 2019-08-23 stsp free(se_id);
6387 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
6388 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
6390 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
6391 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6393 b8bad2ba 2019-08-23 stsp return err;
6394 dbdddfee 2021-06-23 naddy STAILQ_INSERT_AFTER(sorted, se, new, entry);
6397 dbdddfee 2021-06-23 naddy se = STAILQ_NEXT(se, entry);
6402 b8bad2ba 2019-08-23 stsp return err;
6406 b8bad2ba 2019-08-23 stsp static const struct got_error *
6407 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
6409 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
6410 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
6411 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
6413 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6415 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6417 8e7bd50a 2019-08-22 stsp return err;
6419 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
6420 8e7bd50a 2019-08-22 stsp const char *refname;
6421 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6422 8e7bd50a 2019-08-22 stsp char datebuf[26];
6423 d4efa91b 2020-01-14 stsp const char *tagger;
6424 8e7bd50a 2019-08-22 stsp time_t tagger_time;
6425 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
6426 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
6427 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
6429 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
6430 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
6432 8e7bd50a 2019-08-22 stsp refname += 10;
6433 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
6434 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
6435 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
6438 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6439 8e7bd50a 2019-08-22 stsp free(refstr);
6441 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
6444 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
6445 d4efa91b 2020-01-14 stsp if (err) {
6446 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
6450 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
6451 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
6452 d4efa91b 2020-01-14 stsp if (err) {
6456 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
6457 d4efa91b 2020-01-14 stsp tagger_time =
6458 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
6459 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
6465 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
6466 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
6467 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
6468 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
6472 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
6473 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
6474 2417344c 2019-08-23 stsp if (datestr)
6475 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
6476 d4efa91b 2020-01-14 stsp if (commit)
6477 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6479 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
6480 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
6481 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6484 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
6485 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6488 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
6489 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6492 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
6493 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6500 8e7bd50a 2019-08-22 stsp free(id_str);
6501 d4efa91b 2020-01-14 stsp if (commit) {
6502 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
6505 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
6507 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
6508 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
6509 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
6510 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
6515 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
6517 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
6519 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
6520 8e7bd50a 2019-08-22 stsp } while (line);
6521 8e7bd50a 2019-08-22 stsp free(tagmsg0);
6524 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
6525 8e7bd50a 2019-08-22 stsp return NULL;
6528 8e7bd50a 2019-08-22 stsp static const struct got_error *
6529 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6530 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
6532 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6533 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
6534 f372d5cd 2019-10-21 stsp char *editor = NULL;
6535 1601cb9f 2020-09-11 naddy int initial_content_len;
6536 8e7bd50a 2019-08-22 stsp int fd = -1;
6538 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6539 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6540 8e7bd50a 2019-08-22 stsp goto done;
6543 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6544 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
6545 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
6546 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
6547 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6548 8e7bd50a 2019-08-22 stsp goto done;
6551 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6553 8e7bd50a 2019-08-22 stsp goto done;
6555 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6556 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
6557 97972933 2020-09-11 stsp goto done;
6560 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
6562 8e7bd50a 2019-08-22 stsp goto done;
6563 bfa12d5e 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6564 0d5bb276 2020-12-15 stsp initial_content_len, 1);
6566 8e7bd50a 2019-08-22 stsp free(initial_content);
6567 8e7bd50a 2019-08-22 stsp free(template);
6568 8e7bd50a 2019-08-22 stsp free(editor);
6570 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6571 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
6573 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
6574 59f86c76 2020-09-11 stsp if (err == NULL)
6575 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
6576 59f86c76 2020-09-11 stsp if (err) {
6577 59f86c76 2020-09-11 stsp free(*tagmsg);
6578 59f86c76 2020-09-11 stsp *tagmsg = NULL;
6580 8e7bd50a 2019-08-22 stsp return err;
6583 8e7bd50a 2019-08-22 stsp static const struct got_error *
6584 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
6585 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6587 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6588 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
6589 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
6590 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
6591 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6592 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
6593 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
6594 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6596 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6599 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
6600 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
6601 8e7bd50a 2019-08-22 stsp * an unintended typo.
6603 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
6604 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6606 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
6608 8e7bd50a 2019-08-22 stsp return err;
6610 84de9106 2020-12-26 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6612 84de9106 2020-12-26 stsp goto done;
6614 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6615 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &refs, repo);
6617 8e7bd50a 2019-08-22 stsp goto done;
6619 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
6621 8e7bd50a 2019-08-22 stsp goto done;
6623 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
6624 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
6625 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
6626 62d463ca 2020-10-20 naddy err = got_error_from_errno("strdup");
6627 62d463ca 2020-10-20 naddy goto done;
6629 8e7bd50a 2019-08-22 stsp tag_name += 10;
6630 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6631 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
6632 62d463ca 2020-10-20 naddy goto done;
6635 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
6636 8e7bd50a 2019-08-22 stsp if (err == NULL) {
6637 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
6638 8e7bd50a 2019-08-22 stsp goto done;
6639 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
6640 8e7bd50a 2019-08-22 stsp goto done;
6642 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
6643 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6644 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
6645 f372d5cd 2019-10-21 stsp if (err) {
6646 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6647 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
6648 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6649 8e7bd50a 2019-08-22 stsp goto done;
6653 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
6654 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6655 f372d5cd 2019-10-21 stsp if (err) {
6656 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6657 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6658 8e7bd50a 2019-08-22 stsp goto done;
6661 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
6662 f372d5cd 2019-10-21 stsp if (err) {
6663 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6664 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6665 8e7bd50a 2019-08-22 stsp goto done;
6668 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
6669 f372d5cd 2019-10-21 stsp if (err) {
6670 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6671 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6672 f372d5cd 2019-10-21 stsp goto done;
6675 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
6676 f372d5cd 2019-10-21 stsp if (err) {
6677 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6678 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6679 f372d5cd 2019-10-21 stsp goto done;
6681 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
6683 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
6684 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
6685 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
6686 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6687 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
6688 f372d5cd 2019-10-21 stsp free(tag_id_str);
6690 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
6691 8e7bd50a 2019-08-22 stsp free(commit_id);
6692 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6693 8e7bd50a 2019-08-22 stsp free(refname);
6694 8e7bd50a 2019-08-22 stsp free(tagmsg);
6695 f372d5cd 2019-10-21 stsp free(tagmsg_path);
6696 aba9c984 2019-09-08 stsp free(tagger);
6697 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
6698 8e7bd50a 2019-08-22 stsp return err;
6701 8e7bd50a 2019-08-22 stsp static const struct got_error *
6702 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
6704 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
6705 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
6706 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
6707 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6708 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
6709 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6710 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
6712 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6713 8e7bd50a 2019-08-22 stsp switch (ch) {
6715 80106605 2020-02-24 stsp commit_id_arg = optarg;
6718 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6721 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6722 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6723 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6725 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6728 8e7bd50a 2019-08-22 stsp do_list = 1;
6731 8e7bd50a 2019-08-22 stsp usage_tag();
6732 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6736 8e7bd50a 2019-08-22 stsp argc -= optind;
6737 8e7bd50a 2019-08-22 stsp argv += optind;
6739 8e7bd50a 2019-08-22 stsp if (do_list) {
6740 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6742 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
6743 8e7bd50a 2019-08-22 stsp if (tagmsg)
6744 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
6745 8e7bd50a 2019-08-22 stsp if (argc > 0)
6746 8e7bd50a 2019-08-22 stsp usage_tag();
6747 80106605 2020-02-24 stsp } else if (argc != 1)
6748 8e7bd50a 2019-08-22 stsp usage_tag();
6750 a2887370 2019-08-23 stsp tag_name = argv[0];
6752 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
6753 8e7bd50a 2019-08-22 stsp if (do_list) {
6754 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6755 8e7bd50a 2019-08-22 stsp NULL) == -1)
6756 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6758 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6759 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
6760 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6763 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
6764 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
6765 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
6766 8e7bd50a 2019-08-22 stsp goto done;
6769 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6770 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6771 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6772 8e7bd50a 2019-08-22 stsp goto done;
6774 8e7bd50a 2019-08-22 stsp error = NULL;
6775 8e7bd50a 2019-08-22 stsp if (worktree) {
6776 8e7bd50a 2019-08-22 stsp repo_path =
6777 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
6778 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6779 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6780 8e7bd50a 2019-08-22 stsp if (error)
6781 8e7bd50a 2019-08-22 stsp goto done;
6783 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
6784 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6785 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6786 8e7bd50a 2019-08-22 stsp goto done;
6791 8e7bd50a 2019-08-22 stsp if (do_list) {
6792 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6793 c9956ddf 2019-09-08 stsp if (error != NULL)
6794 c9956ddf 2019-09-08 stsp goto done;
6795 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6796 8e7bd50a 2019-08-22 stsp if (error)
6797 8e7bd50a 2019-08-22 stsp goto done;
6798 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
6800 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6801 c9956ddf 2019-09-08 stsp if (error)
6802 c9956ddf 2019-09-08 stsp goto done;
6803 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
6804 c9956ddf 2019-09-08 stsp if (error != NULL)
6805 c9956ddf 2019-09-08 stsp goto done;
6807 8e7bd50a 2019-08-22 stsp if (tagmsg) {
6808 42a285e2 2020-10-01 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6809 8e7bd50a 2019-08-22 stsp if (error)
6810 8e7bd50a 2019-08-22 stsp goto done;
6813 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
6814 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
6815 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
6816 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
6817 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
6818 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
6819 8e7bd50a 2019-08-22 stsp if (error)
6820 8e7bd50a 2019-08-22 stsp goto done;
6821 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6822 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
6823 8e7bd50a 2019-08-22 stsp if (error)
6824 8e7bd50a 2019-08-22 stsp goto done;
6825 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
6826 8e7bd50a 2019-08-22 stsp free(commit_id);
6827 8e7bd50a 2019-08-22 stsp if (error)
6828 8e7bd50a 2019-08-22 stsp goto done;
6831 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
6832 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6835 1d0f4054 2021-06-17 stsp if (repo) {
6836 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6837 1d0f4054 2021-06-17 stsp if (error == NULL)
6838 1d0f4054 2021-06-17 stsp error = close_err;
6840 8e7bd50a 2019-08-22 stsp if (worktree)
6841 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
6842 8e7bd50a 2019-08-22 stsp free(cwd);
6843 8e7bd50a 2019-08-22 stsp free(repo_path);
6844 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6845 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6846 8e7bd50a 2019-08-22 stsp return error;
6849 8e7bd50a 2019-08-22 stsp __dead static void
6850 d00136be 2019-03-26 stsp usage_add(void)
6852 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6853 022fae89 2019-12-06 tracey getprogname());
6857 d00136be 2019-03-26 stsp static const struct got_error *
6858 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6860 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6862 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6863 4e68cba3 2019-11-23 stsp return NULL;
6866 4e68cba3 2019-11-23 stsp static const struct got_error *
6867 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6869 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6870 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6871 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6872 1dd54920 2019-05-11 stsp char *cwd = NULL;
6873 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6874 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6875 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6877 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6879 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6880 d00136be 2019-03-26 stsp switch (ch) {
6881 022fae89 2019-12-06 tracey case 'I':
6882 022fae89 2019-12-06 tracey no_ignores = 1;
6885 4e68cba3 2019-11-23 stsp can_recurse = 1;
6888 d00136be 2019-03-26 stsp usage_add();
6889 d00136be 2019-03-26 stsp /* NOTREACHED */
6893 d00136be 2019-03-26 stsp argc -= optind;
6894 d00136be 2019-03-26 stsp argv += optind;
6896 43012d58 2019-07-14 stsp #ifndef PROFILE
6897 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6898 43012d58 2019-07-14 stsp NULL) == -1)
6899 43012d58 2019-07-14 stsp err(1, "pledge");
6901 723c305c 2019-05-11 jcs if (argc < 1)
6902 d00136be 2019-03-26 stsp usage_add();
6904 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6905 d00136be 2019-03-26 stsp if (cwd == NULL) {
6906 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6907 d00136be 2019-03-26 stsp goto done;
6910 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6911 fa51e947 2020-03-27 stsp if (error) {
6912 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6913 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6914 d00136be 2019-03-26 stsp goto done;
6917 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6919 031a5338 2019-03-26 stsp if (error != NULL)
6920 031a5338 2019-03-26 stsp goto done;
6922 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6923 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6924 d00136be 2019-03-26 stsp if (error)
6925 d00136be 2019-03-26 stsp goto done;
6927 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6928 6d022e97 2019-08-04 stsp if (error)
6929 022fae89 2019-12-06 tracey goto done;
6931 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6932 4e68cba3 2019-11-23 stsp char *ondisk_path;
6933 4e68cba3 2019-11-23 stsp struct stat sb;
6934 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6935 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6936 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6937 62d463ca 2020-10-20 naddy pe->path) == -1) {
6938 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6939 4e68cba3 2019-11-23 stsp goto done;
6941 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6942 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6943 4e68cba3 2019-11-23 stsp free(ondisk_path);
6946 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6947 4e68cba3 2019-11-23 stsp ondisk_path);
6948 4e68cba3 2019-11-23 stsp free(ondisk_path);
6949 4e68cba3 2019-11-23 stsp goto done;
6951 4e68cba3 2019-11-23 stsp free(ondisk_path);
6952 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6953 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6954 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6955 4e68cba3 2019-11-23 stsp goto done;
6960 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6961 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6963 1d0f4054 2021-06-17 stsp if (repo) {
6964 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6965 1d0f4054 2021-06-17 stsp if (error == NULL)
6966 1d0f4054 2021-06-17 stsp error = close_err;
6968 d00136be 2019-03-26 stsp if (worktree)
6969 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6970 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6971 1dd54920 2019-05-11 stsp free((char *)pe->path);
6972 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6973 2ec1f75b 2019-03-26 stsp free(cwd);
6974 2ec1f75b 2019-03-26 stsp return error;
6977 2ec1f75b 2019-03-26 stsp __dead static void
6978 648e4ef7 2019-07-09 stsp usage_remove(void)
6980 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6981 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6985 2a06fe5f 2019-08-24 stsp static const struct got_error *
6986 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6987 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6989 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6991 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6992 2a06fe5f 2019-08-24 stsp return NULL;
6993 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6994 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6995 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6996 2a06fe5f 2019-08-24 stsp return NULL;
6999 2ec1f75b 2019-03-26 stsp static const struct got_error *
7000 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
7002 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
7003 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
7004 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
7005 766841c2 2020-08-13 stsp const char *status_codes = NULL;
7006 17ed4618 2019-06-02 stsp char *cwd = NULL;
7007 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
7008 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
7009 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7010 d64cc78a 2022-01-25 thomas int ignore_missing_paths = 0;
7012 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
7014 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7015 2ec1f75b 2019-03-26 stsp switch (ch) {
7017 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
7018 d64cc78a 2022-01-25 thomas ignore_missing_paths = 1;
7020 70e3e7f5 2019-12-13 tracey case 'k':
7021 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
7023 f2a9dc41 2019-12-13 tracey case 'R':
7024 f2a9dc41 2019-12-13 tracey can_recurse = 1;
7027 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
7028 766841c2 2020-08-13 stsp switch (optarg[i]) {
7029 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
7030 766841c2 2020-08-13 stsp delete_local_mods = 1;
7032 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
7033 d64cc78a 2022-01-25 thomas ignore_missing_paths = 1;
7036 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
7037 766841c2 2020-08-13 stsp optarg[i]);
7040 766841c2 2020-08-13 stsp status_codes = optarg;
7043 f2a9dc41 2019-12-13 tracey usage_remove();
7044 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
7048 2ec1f75b 2019-03-26 stsp argc -= optind;
7049 2ec1f75b 2019-03-26 stsp argv += optind;
7051 43012d58 2019-07-14 stsp #ifndef PROFILE
7052 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7053 43012d58 2019-07-14 stsp NULL) == -1)
7054 43012d58 2019-07-14 stsp err(1, "pledge");
7056 17ed4618 2019-06-02 stsp if (argc < 1)
7057 648e4ef7 2019-07-09 stsp usage_remove();
7059 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
7060 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
7061 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7062 2ec1f75b 2019-03-26 stsp goto done;
7064 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
7065 fa51e947 2020-03-27 stsp if (error) {
7066 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7067 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
7068 2ec1f75b 2019-03-26 stsp goto done;
7071 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7074 2ec1f75b 2019-03-26 stsp goto done;
7076 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
7077 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7078 2ec1f75b 2019-03-26 stsp if (error)
7079 2ec1f75b 2019-03-26 stsp goto done;
7081 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7082 6d022e97 2019-08-04 stsp if (error)
7083 6d022e97 2019-08-04 stsp goto done;
7085 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
7086 f2a9dc41 2019-12-13 tracey char *ondisk_path;
7087 f2a9dc41 2019-12-13 tracey struct stat sb;
7088 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
7089 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
7090 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
7091 62d463ca 2020-10-20 naddy pe->path) == -1) {
7092 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
7093 f2a9dc41 2019-12-13 tracey goto done;
7095 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
7096 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
7097 f2a9dc41 2019-12-13 tracey free(ondisk_path);
7098 f2a9dc41 2019-12-13 tracey continue;
7100 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
7101 f2a9dc41 2019-12-13 tracey ondisk_path);
7102 f2a9dc41 2019-12-13 tracey free(ondisk_path);
7103 f2a9dc41 2019-12-13 tracey goto done;
7105 f2a9dc41 2019-12-13 tracey free(ondisk_path);
7106 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
7107 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
7108 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
7109 f2a9dc41 2019-12-13 tracey goto done;
7114 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
7115 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
7116 d64cc78a 2022-01-25 thomas repo, keep_on_disk, ignore_missing_paths);
7118 1d0f4054 2021-06-17 stsp if (repo) {
7119 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7120 1d0f4054 2021-06-17 stsp if (error == NULL)
7121 1d0f4054 2021-06-17 stsp error = close_err;
7123 a129376b 2019-03-28 stsp if (worktree)
7124 a129376b 2019-03-28 stsp got_worktree_close(worktree);
7125 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
7126 17ed4618 2019-06-02 stsp free((char *)pe->path);
7127 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
7128 069bbb86 2022-03-07 thomas free(cwd);
7129 069bbb86 2022-03-07 thomas return error;
7132 069bbb86 2022-03-07 thomas __dead static void
7133 069bbb86 2022-03-07 thomas usage_patch(void)
7135 069bbb86 2022-03-07 thomas fprintf(stderr, "usage: %s patch [patchfile]\n",
7136 069bbb86 2022-03-07 thomas getprogname());
7137 069bbb86 2022-03-07 thomas exit(1);
7140 069bbb86 2022-03-07 thomas static const struct got_error *
7141 069bbb86 2022-03-07 thomas patch_from_stdin(int *patchfd)
7143 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
7144 069bbb86 2022-03-07 thomas ssize_t r;
7145 069bbb86 2022-03-07 thomas char *path, buf[BUFSIZ];
7146 069bbb86 2022-03-07 thomas sig_t sighup, sigint, sigquit;
7148 069bbb86 2022-03-07 thomas err = got_opentemp_named_fd(&path, patchfd,
7149 069bbb86 2022-03-07 thomas GOT_TMPDIR_STR "/got-patch");
7150 069bbb86 2022-03-07 thomas if (err)
7151 069bbb86 2022-03-07 thomas return err;
7152 069bbb86 2022-03-07 thomas unlink(path);
7153 069bbb86 2022-03-07 thomas free(path);
7155 069bbb86 2022-03-07 thomas sighup = signal(SIGHUP, SIG_DFL);
7156 069bbb86 2022-03-07 thomas sigint = signal(SIGINT, SIG_DFL);
7157 069bbb86 2022-03-07 thomas sigquit = signal(SIGQUIT, SIG_DFL);
7159 069bbb86 2022-03-07 thomas for (;;) {
7160 069bbb86 2022-03-07 thomas r = read(0, buf, sizeof(buf));
7161 069bbb86 2022-03-07 thomas if (r == -1) {
7162 069bbb86 2022-03-07 thomas err = got_error_from_errno("read");
7165 069bbb86 2022-03-07 thomas if (r == 0)
7167 069bbb86 2022-03-07 thomas if (write(*patchfd, buf, r) == -1) {
7168 069bbb86 2022-03-07 thomas err = got_error_from_errno("write");
7173 069bbb86 2022-03-07 thomas signal(SIGHUP, sighup);
7174 069bbb86 2022-03-07 thomas signal(SIGINT, sigint);
7175 069bbb86 2022-03-07 thomas signal(SIGQUIT, sigquit);
7177 edb9a180 2022-03-13 thomas if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
7178 edb9a180 2022-03-13 thomas err = got_error_from_errno("lseek");
7180 edb9a180 2022-03-13 thomas if (err != NULL) {
7181 069bbb86 2022-03-07 thomas close(*patchfd);
7182 edb9a180 2022-03-13 thomas *patchfd = -1;
7185 edb9a180 2022-03-13 thomas return err;
7188 069bbb86 2022-03-07 thomas static const struct got_error *
7189 069bbb86 2022-03-07 thomas cmd_patch(int argc, char *argv[])
7191 069bbb86 2022-03-07 thomas const struct got_error *error = NULL, *close_error = NULL;
7192 069bbb86 2022-03-07 thomas struct got_worktree *worktree = NULL;
7193 069bbb86 2022-03-07 thomas struct got_repository *repo = NULL;
7194 069bbb86 2022-03-07 thomas char *cwd = NULL;
7196 069bbb86 2022-03-07 thomas int patchfd;
7198 069bbb86 2022-03-07 thomas while ((ch = getopt(argc, argv, "")) != -1) {
7199 069bbb86 2022-03-07 thomas switch (ch) {
7200 069bbb86 2022-03-07 thomas default:
7201 069bbb86 2022-03-07 thomas usage_patch();
7202 069bbb86 2022-03-07 thomas /* NOTREACHED */
7206 069bbb86 2022-03-07 thomas argc -= optind;
7207 069bbb86 2022-03-07 thomas argv += optind;
7209 069bbb86 2022-03-07 thomas if (argc == 0) {
7210 069bbb86 2022-03-07 thomas error = patch_from_stdin(&patchfd);
7211 069bbb86 2022-03-07 thomas if (error)
7212 069bbb86 2022-03-07 thomas return error;
7213 069bbb86 2022-03-07 thomas } else if (argc == 1) {
7214 069bbb86 2022-03-07 thomas patchfd = open(argv[0], O_RDONLY);
7215 069bbb86 2022-03-07 thomas if (patchfd == -1) {
7216 069bbb86 2022-03-07 thomas error = got_error_from_errno2("open", argv[0]);
7217 069bbb86 2022-03-07 thomas return error;
7220 069bbb86 2022-03-07 thomas usage_patch();
7222 069bbb86 2022-03-07 thomas if ((cwd = getcwd(NULL, 0)) == NULL) {
7223 069bbb86 2022-03-07 thomas error = got_error_from_errno("getcwd");
7224 069bbb86 2022-03-07 thomas goto done;
7227 069bbb86 2022-03-07 thomas error = got_worktree_open(&worktree, cwd);
7228 069bbb86 2022-03-07 thomas if (error != NULL)
7229 069bbb86 2022-03-07 thomas goto done;
7231 069bbb86 2022-03-07 thomas const char *repo_path = got_worktree_get_repo_path(worktree);
7232 069bbb86 2022-03-07 thomas error = got_repo_open(&repo, repo_path, NULL);
7233 069bbb86 2022-03-07 thomas if (error != NULL)
7234 069bbb86 2022-03-07 thomas goto done;
7236 069bbb86 2022-03-07 thomas error = apply_unveil(got_repo_get_path(repo), 0,
7237 069bbb86 2022-03-07 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
7238 069bbb86 2022-03-07 thomas if (error != NULL)
7239 069bbb86 2022-03-07 thomas goto done;
7241 069bbb86 2022-03-07 thomas #ifndef PROFILE
7242 069bbb86 2022-03-07 thomas if (pledge("stdio rpath wpath cpath proc exec sendfd flock",
7243 069bbb86 2022-03-07 thomas NULL) == -1)
7244 069bbb86 2022-03-07 thomas err(1, "pledge");
7247 069bbb86 2022-03-07 thomas error = got_patch(patchfd, worktree, repo, &print_remove_status,
7248 42d9d68e 2022-03-13 thomas NULL, &add_progress, NULL, check_cancelled, NULL);
7251 069bbb86 2022-03-07 thomas if (repo) {
7252 069bbb86 2022-03-07 thomas close_error = got_repo_close(repo);
7253 069bbb86 2022-03-07 thomas if (error == NULL)
7254 069bbb86 2022-03-07 thomas error = close_error;
7256 069bbb86 2022-03-07 thomas if (worktree != NULL) {
7257 069bbb86 2022-03-07 thomas close_error = got_worktree_close(worktree);
7258 069bbb86 2022-03-07 thomas if (error == NULL)
7259 069bbb86 2022-03-07 thomas error = close_error;
7261 a129376b 2019-03-28 stsp free(cwd);
7262 a129376b 2019-03-28 stsp return error;
7265 a129376b 2019-03-28 stsp __dead static void
7266 a129376b 2019-03-28 stsp usage_revert(void)
7268 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
7269 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
7273 1ee397ad 2019-07-12 stsp static const struct got_error *
7274 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
7276 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
7277 fb9704af 2020-01-27 stsp return NULL;
7279 a129376b 2019-03-28 stsp while (path[0] == '/')
7281 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
7282 33aa809d 2019-08-08 stsp return NULL;
7285 33aa809d 2019-08-08 stsp struct choose_patch_arg {
7286 33aa809d 2019-08-08 stsp FILE *patch_script_file;
7287 33aa809d 2019-08-08 stsp const char *action;
7290 33aa809d 2019-08-08 stsp static const struct got_error *
7291 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
7292 33aa809d 2019-08-08 stsp int nchanges, const char *action)
7294 33aa809d 2019-08-08 stsp char *line = NULL;
7295 33aa809d 2019-08-08 stsp size_t linesize = 0;
7296 33aa809d 2019-08-08 stsp ssize_t linelen;
7298 33aa809d 2019-08-08 stsp switch (status) {
7299 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
7300 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
7302 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
7303 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
7305 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
7306 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
7307 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
7308 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
7309 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
7310 33aa809d 2019-08-08 stsp printf("%s", line);
7311 33aa809d 2019-08-08 stsp if (ferror(patch_file))
7312 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
7313 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
7314 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
7315 33aa809d 2019-08-08 stsp path, n, nchanges, action);
7318 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
7321 33aa809d 2019-08-08 stsp return NULL;
7324 33aa809d 2019-08-08 stsp static const struct got_error *
7325 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
7326 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
7328 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
7329 33aa809d 2019-08-08 stsp char *line = NULL;
7330 33aa809d 2019-08-08 stsp size_t linesize = 0;
7331 33aa809d 2019-08-08 stsp ssize_t linelen;
7332 33aa809d 2019-08-08 stsp int resp = ' ';
7333 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
7335 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
7337 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
7339 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
7340 33aa809d 2019-08-08 stsp a->action);
7342 33aa809d 2019-08-08 stsp return err;
7343 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
7344 33aa809d 2019-08-08 stsp if (linelen == -1) {
7345 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
7346 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
7347 33aa809d 2019-08-08 stsp return NULL;
7349 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
7351 33aa809d 2019-08-08 stsp *nl = '\0';
7352 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
7353 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
7354 33aa809d 2019-08-08 stsp printf("y\n");
7355 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
7356 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
7357 33aa809d 2019-08-08 stsp printf("n\n");
7358 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
7359 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
7360 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
7361 33aa809d 2019-08-08 stsp printf("q\n");
7363 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
7364 33aa809d 2019-08-08 stsp free(line);
7365 33aa809d 2019-08-08 stsp return NULL;
7368 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
7369 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
7370 33aa809d 2019-08-08 stsp a->action);
7372 33aa809d 2019-08-08 stsp return err;
7373 33aa809d 2019-08-08 stsp resp = getchar();
7374 33aa809d 2019-08-08 stsp if (resp == '\n')
7375 33aa809d 2019-08-08 stsp resp = getchar();
7376 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
7377 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
7378 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
7379 33aa809d 2019-08-08 stsp resp = ' ';
7381 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
7382 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
7383 33aa809d 2019-08-08 stsp resp = ' ';
7387 33aa809d 2019-08-08 stsp if (resp == 'y')
7388 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
7389 33aa809d 2019-08-08 stsp else if (resp == 'n')
7390 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
7391 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
7392 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
7394 1ee397ad 2019-07-12 stsp return NULL;
7397 a129376b 2019-03-28 stsp static const struct got_error *
7398 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
7400 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
7401 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
7402 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
7403 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
7404 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
7405 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
7406 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
7407 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
7408 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
7409 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
7411 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
7413 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
7414 a129376b 2019-03-28 stsp switch (ch) {
7416 33aa809d 2019-08-08 stsp pflag = 1;
7419 33aa809d 2019-08-08 stsp patch_script_path = optarg;
7422 0f6d7415 2019-08-08 stsp can_recurse = 1;
7425 a129376b 2019-03-28 stsp usage_revert();
7426 a129376b 2019-03-28 stsp /* NOTREACHED */
7430 a129376b 2019-03-28 stsp argc -= optind;
7431 a129376b 2019-03-28 stsp argv += optind;
7433 43012d58 2019-07-14 stsp #ifndef PROFILE
7434 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7435 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7436 43012d58 2019-07-14 stsp err(1, "pledge");
7438 e20a8b6f 2019-06-04 stsp if (argc < 1)
7439 a129376b 2019-03-28 stsp usage_revert();
7440 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
7441 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7443 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
7444 a129376b 2019-03-28 stsp if (cwd == NULL) {
7445 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7446 a129376b 2019-03-28 stsp goto done;
7448 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
7449 fa51e947 2020-03-27 stsp if (error) {
7450 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7451 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
7452 2ec1f75b 2019-03-26 stsp goto done;
7455 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7457 a129376b 2019-03-28 stsp if (error != NULL)
7458 a129376b 2019-03-28 stsp goto done;
7460 33aa809d 2019-08-08 stsp if (patch_script_path) {
7461 c56c5d8a 2021-12-31 thomas patch_script_file = fopen(patch_script_path, "re");
7462 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
7463 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
7464 33aa809d 2019-08-08 stsp patch_script_path);
7465 33aa809d 2019-08-08 stsp goto done;
7468 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
7469 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7470 a129376b 2019-03-28 stsp if (error)
7471 a129376b 2019-03-28 stsp goto done;
7473 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7474 6d022e97 2019-08-04 stsp if (error)
7475 6d022e97 2019-08-04 stsp goto done;
7477 0f6d7415 2019-08-08 stsp if (!can_recurse) {
7478 0f6d7415 2019-08-08 stsp char *ondisk_path;
7479 0f6d7415 2019-08-08 stsp struct stat sb;
7480 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
7481 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
7482 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
7483 62d463ca 2020-10-20 naddy pe->path) == -1) {
7484 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
7485 0f6d7415 2019-08-08 stsp goto done;
7487 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
7488 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
7489 0f6d7415 2019-08-08 stsp free(ondisk_path);
7492 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
7493 0f6d7415 2019-08-08 stsp ondisk_path);
7494 0f6d7415 2019-08-08 stsp free(ondisk_path);
7495 0f6d7415 2019-08-08 stsp goto done;
7497 0f6d7415 2019-08-08 stsp free(ondisk_path);
7498 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
7499 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
7500 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
7501 0f6d7415 2019-08-08 stsp goto done;
7506 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7507 33aa809d 2019-08-08 stsp cpa.action = "revert";
7508 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7509 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7511 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7512 33aa809d 2019-08-08 stsp error == NULL)
7513 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7514 1d0f4054 2021-06-17 stsp if (repo) {
7515 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7516 1d0f4054 2021-06-17 stsp if (error == NULL)
7517 1d0f4054 2021-06-17 stsp error = close_err;
7519 2ec1f75b 2019-03-26 stsp if (worktree)
7520 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
7521 2ec1f75b 2019-03-26 stsp free(path);
7522 d00136be 2019-03-26 stsp free(cwd);
7523 6bad629b 2019-02-04 stsp return error;
7526 c4296144 2019-05-09 stsp __dead static void
7527 c4296144 2019-05-09 stsp usage_commit(void)
7529 28cf319f 2021-01-28 stsp fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
7530 28cf319f 2021-01-28 stsp "[path ...]\n", getprogname());
7534 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
7535 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
7536 28cf319f 2021-01-28 stsp const char *prepared_log;
7537 28cf319f 2021-01-28 stsp int non_interactive;
7538 e2ba3d07 2019-05-13 stsp const char *editor;
7539 e0870e44 2019-05-13 stsp const char *worktree_path;
7540 76d98825 2019-06-03 stsp const char *branch_name;
7541 314a6357 2019-05-13 stsp const char *repo_path;
7542 e0870e44 2019-05-13 stsp char *logmsg_path;
7546 28cf319f 2021-01-28 stsp static const struct got_error *
7547 28cf319f 2021-01-28 stsp read_prepared_logmsg(char **logmsg, const char *path)
7549 28cf319f 2021-01-28 stsp const struct got_error *err = NULL;
7550 28cf319f 2021-01-28 stsp FILE *f = NULL;
7551 28cf319f 2021-01-28 stsp struct stat sb;
7554 28cf319f 2021-01-28 stsp *logmsg = NULL;
7555 28cf319f 2021-01-28 stsp memset(&sb, 0, sizeof(sb));
7557 c56c5d8a 2021-12-31 thomas f = fopen(path, "re");
7558 28cf319f 2021-01-28 stsp if (f == NULL)
7559 28cf319f 2021-01-28 stsp return got_error_from_errno2("fopen", path);
7561 28cf319f 2021-01-28 stsp if (fstat(fileno(f), &sb) == -1) {
7562 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fstat", path);
7563 28cf319f 2021-01-28 stsp goto done;
7565 28cf319f 2021-01-28 stsp if (sb.st_size == 0) {
7566 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7567 28cf319f 2021-01-28 stsp goto done;
7570 28cf319f 2021-01-28 stsp *logmsg = malloc(sb.st_size + 1);
7571 28cf319f 2021-01-28 stsp if (*logmsg == NULL) {
7572 28cf319f 2021-01-28 stsp err = got_error_from_errno("malloc");
7573 28cf319f 2021-01-28 stsp goto done;
7576 28cf319f 2021-01-28 stsp r = fread(*logmsg, 1, sb.st_size, f);
7577 28cf319f 2021-01-28 stsp if (r != sb.st_size) {
7578 28cf319f 2021-01-28 stsp if (ferror(f))
7579 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fread", path);
7581 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_IO);
7582 28cf319f 2021-01-28 stsp goto done;
7584 28cf319f 2021-01-28 stsp (*logmsg)[sb.st_size] = '\0';
7586 28cf319f 2021-01-28 stsp if (fclose(f) == EOF && err == NULL)
7587 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fclose", path);
7588 28cf319f 2021-01-28 stsp if (err) {
7589 28cf319f 2021-01-28 stsp free(*logmsg);
7590 28cf319f 2021-01-28 stsp *logmsg = NULL;
7592 28cf319f 2021-01-28 stsp return err;
7596 33ad4cbe 2019-05-12 jcs static const struct got_error *
7597 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
7600 76d98825 2019-06-03 stsp char *initial_content = NULL;
7601 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
7602 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
7603 e0870e44 2019-05-13 stsp char *template = NULL;
7604 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
7605 1601cb9f 2020-09-11 naddy int initial_content_len;
7606 97972933 2020-09-11 stsp int fd = -1;
7607 33ad4cbe 2019-05-12 jcs size_t len;
7609 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
7610 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
7611 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
7612 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
7613 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
7614 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
7615 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
7616 33ad4cbe 2019-05-12 jcs return NULL;
7617 28cf319f 2021-01-28 stsp } else if (a->prepared_log != NULL && a->non_interactive)
7618 28cf319f 2021-01-28 stsp return read_prepared_logmsg(logmsg, a->prepared_log);
7620 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7621 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
7623 28cf319f 2021-01-28 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7625 28cf319f 2021-01-28 stsp goto done;
7627 28cf319f 2021-01-28 stsp if (a->prepared_log) {
7628 28cf319f 2021-01-28 stsp char *msg;
7629 28cf319f 2021-01-28 stsp err = read_prepared_logmsg(&msg, a->prepared_log);
7631 28cf319f 2021-01-28 stsp goto done;
7632 28cf319f 2021-01-28 stsp if (write(fd, msg, strlen(msg)) == -1) {
7633 28cf319f 2021-01-28 stsp err = got_error_from_errno2("write", a->logmsg_path);
7634 28cf319f 2021-01-28 stsp free(msg);
7635 28cf319f 2021-01-28 stsp goto done;
7637 28cf319f 2021-01-28 stsp free(msg);
7640 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
7641 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
7642 1601cb9f 2020-09-11 naddy a->branch_name);
7643 28cf319f 2021-01-28 stsp if (initial_content_len == -1) {
7644 28cf319f 2021-01-28 stsp err = got_error_from_errno("asprintf");
7645 e0870e44 2019-05-13 stsp goto done;
7648 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
7649 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
7650 97972933 2020-09-11 stsp goto done;
7653 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
7654 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
7655 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
7656 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
7657 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
7660 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7661 28cf319f 2021-01-28 stsp initial_content_len, a->prepared_log ? 0 : 1);
7663 76d98825 2019-06-03 stsp free(initial_content);
7664 e0870e44 2019-05-13 stsp free(template);
7666 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
7667 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
7669 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
7670 59f86c76 2020-09-11 stsp if (err == NULL)
7671 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
7672 59f86c76 2020-09-11 stsp if (err) {
7673 59f86c76 2020-09-11 stsp free(*logmsg);
7674 59f86c76 2020-09-11 stsp *logmsg = NULL;
7676 33ad4cbe 2019-05-12 jcs return err;
7679 c4296144 2019-05-09 stsp static const struct got_error *
7680 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
7682 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
7683 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
7684 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
7685 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
7686 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
7687 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
7688 28cf319f 2021-01-28 stsp char *prepared_logmsg = NULL;
7689 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
7690 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7691 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7692 10604dce 2021-09-24 thomas int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
7693 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
7695 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
7696 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
7698 28cf319f 2021-01-28 stsp while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7699 c4296144 2019-05-09 stsp switch (ch) {
7701 28cf319f 2021-01-28 stsp if (logmsg != NULL)
7702 28cf319f 2021-01-28 stsp option_conflict('F', 'm');
7703 28cf319f 2021-01-28 stsp prepared_logmsg = realpath(optarg, NULL);
7704 28cf319f 2021-01-28 stsp if (prepared_logmsg == NULL)
7705 28cf319f 2021-01-28 stsp return got_error_from_errno2("realpath",
7709 28cf319f 2021-01-28 stsp if (prepared_logmsg)
7710 28cf319f 2021-01-28 stsp option_conflict('m', 'F');
7711 c4296144 2019-05-09 stsp logmsg = optarg;
7714 28cf319f 2021-01-28 stsp non_interactive = 1;
7717 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
7720 c4296144 2019-05-09 stsp usage_commit();
7721 c4296144 2019-05-09 stsp /* NOTREACHED */
7725 c4296144 2019-05-09 stsp argc -= optind;
7726 c4296144 2019-05-09 stsp argv += optind;
7728 43012d58 2019-07-14 stsp #ifndef PROFILE
7729 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7730 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7731 43012d58 2019-07-14 stsp err(1, "pledge");
7733 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
7734 c4296144 2019-05-09 stsp if (cwd == NULL) {
7735 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7736 c4296144 2019-05-09 stsp goto done;
7738 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
7739 fa51e947 2020-03-27 stsp if (error) {
7740 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7741 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
7742 7d5807f4 2019-07-11 stsp goto done;
7745 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7746 c4296144 2019-05-09 stsp if (error)
7747 a698f62e 2019-07-25 stsp goto done;
7748 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
7749 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
7750 c4296144 2019-05-09 stsp goto done;
7753 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7754 916f288c 2019-07-30 stsp worktree);
7755 5c1e53bc 2019-07-28 stsp if (error)
7756 5c1e53bc 2019-07-28 stsp goto done;
7758 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
7759 c9956ddf 2019-09-08 stsp if (error)
7760 c9956ddf 2019-09-08 stsp goto done;
7761 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7762 c9956ddf 2019-09-08 stsp gitconfig_path);
7763 c4296144 2019-05-09 stsp if (error != NULL)
7764 c4296144 2019-05-09 stsp goto done;
7766 10604dce 2021-09-24 thomas error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
7767 10604dce 2021-09-24 thomas if (error)
7768 10604dce 2021-09-24 thomas goto done;
7769 10604dce 2021-09-24 thomas if (merge_in_progress) {
7770 10604dce 2021-09-24 thomas error = got_error(GOT_ERR_MERGE_BUSY);
7771 10604dce 2021-09-24 thomas goto done;
7774 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
7775 aba9c984 2019-09-08 stsp if (error)
7776 aba9c984 2019-09-08 stsp return error;
7779 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
7780 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
7782 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
7783 314a6357 2019-05-13 stsp error = get_editor(&editor);
7785 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7786 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7787 c4296144 2019-05-09 stsp if (error)
7788 c4296144 2019-05-09 stsp goto done;
7790 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7791 087fb88c 2019-08-04 stsp if (error)
7792 087fb88c 2019-08-04 stsp goto done;
7794 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
7795 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
7796 28cf319f 2021-01-28 stsp cl_arg.prepared_log = prepared_logmsg;
7797 28cf319f 2021-01-28 stsp cl_arg.non_interactive = non_interactive;
7798 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7799 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7800 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
7801 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7802 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
7803 916f288c 2019-07-30 stsp goto done;
7805 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
7807 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
7808 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7809 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7810 35213c7c 2020-07-23 stsp print_status, NULL, repo);
7811 e0870e44 2019-05-13 stsp if (error) {
7812 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7813 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
7814 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
7815 c4296144 2019-05-09 stsp goto done;
7818 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
7819 c4296144 2019-05-09 stsp if (error)
7820 c4296144 2019-05-09 stsp goto done;
7821 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
7823 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
7824 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
7825 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
7826 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7827 7266f21f 2019-10-21 stsp error == NULL)
7828 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7829 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
7830 1d0f4054 2021-06-17 stsp if (repo) {
7831 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7832 1d0f4054 2021-06-17 stsp if (error == NULL)
7833 1d0f4054 2021-06-17 stsp error = close_err;
7835 c4296144 2019-05-09 stsp if (worktree)
7836 c4296144 2019-05-09 stsp got_worktree_close(worktree);
7837 c4296144 2019-05-09 stsp free(cwd);
7838 c4296144 2019-05-09 stsp free(id_str);
7839 c9956ddf 2019-09-08 stsp free(gitconfig_path);
7840 e2ba3d07 2019-05-13 stsp free(editor);
7841 aba9c984 2019-09-08 stsp free(author);
7842 28cf319f 2021-01-28 stsp free(prepared_logmsg);
7843 f8a36e22 2021-08-26 stsp return error;
7846 f8a36e22 2021-08-26 stsp __dead static void
7847 f8a36e22 2021-08-26 stsp usage_send(void)
7849 f8a36e22 2021-08-26 stsp fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
7850 f8a36e22 2021-08-26 stsp "[-r repository-path] [-t tag] [-T] [-q] [-v] "
7851 f8a36e22 2021-08-26 stsp "[remote-repository]\n", getprogname());
7855 f8a36e22 2021-08-26 stsp struct got_send_progress_arg {
7856 f8a36e22 2021-08-26 stsp char last_scaled_packsize[FMT_SCALED_STRSIZE];
7857 f8a36e22 2021-08-26 stsp int verbosity;
7858 f8a36e22 2021-08-26 stsp int last_ncommits;
7859 f8a36e22 2021-08-26 stsp int last_nobj_total;
7860 f8a36e22 2021-08-26 stsp int last_p_deltify;
7861 f8a36e22 2021-08-26 stsp int last_p_written;
7862 f8a36e22 2021-08-26 stsp int last_p_sent;
7863 f8a36e22 2021-08-26 stsp int printed_something;
7864 f8a36e22 2021-08-26 stsp int sent_something;
7865 f8a36e22 2021-08-26 stsp struct got_pathlist_head *delete_branches;
7868 f8a36e22 2021-08-26 stsp static const struct got_error *
7869 f8a36e22 2021-08-26 stsp send_progress(void *arg, off_t packfile_size, int ncommits, int nobj_total,
7870 f8a36e22 2021-08-26 stsp int nobj_deltify, int nobj_written, off_t bytes_sent, const char *refname,
7871 f8a36e22 2021-08-26 stsp int success)
7873 f8a36e22 2021-08-26 stsp struct got_send_progress_arg *a = arg;
7874 f8a36e22 2021-08-26 stsp char scaled_packsize[FMT_SCALED_STRSIZE];
7875 f8a36e22 2021-08-26 stsp char scaled_sent[FMT_SCALED_STRSIZE];
7876 f8a36e22 2021-08-26 stsp int p_deltify = 0, p_written = 0, p_sent = 0;
7877 f8a36e22 2021-08-26 stsp int print_searching = 0, print_total = 0;
7878 f8a36e22 2021-08-26 stsp int print_deltify = 0, print_written = 0, print_sent = 0;
7880 f8a36e22 2021-08-26 stsp if (a->verbosity < 0)
7881 f8a36e22 2021-08-26 stsp return NULL;
7883 f8a36e22 2021-08-26 stsp if (refname) {
7884 f8a36e22 2021-08-26 stsp const char *status = success ? "accepted" : "rejected";
7886 f8a36e22 2021-08-26 stsp if (success) {
7887 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
7888 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, a->delete_branches, entry) {
7889 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
7890 f8a36e22 2021-08-26 stsp if (got_path_cmp(branchname, refname,
7891 f8a36e22 2021-08-26 stsp strlen(branchname), strlen(refname)) == 0) {
7892 f8a36e22 2021-08-26 stsp status = "deleted";
7893 27b75514 2021-08-28 stsp a->sent_something = 1;
7899 27b75514 2021-08-28 stsp if (a->printed_something)
7900 27b75514 2021-08-28 stsp putchar('\n');
7901 27b75514 2021-08-28 stsp printf("Server has %s %s", status, refname);
7902 f8a36e22 2021-08-26 stsp a->printed_something = 1;
7903 f8a36e22 2021-08-26 stsp return NULL;
7906 f8a36e22 2021-08-26 stsp if (fmt_scaled(packfile_size, scaled_packsize) == -1)
7907 f8a36e22 2021-08-26 stsp return got_error_from_errno("fmt_scaled");
7908 f8a36e22 2021-08-26 stsp if (fmt_scaled(bytes_sent, scaled_sent) == -1)
7909 f8a36e22 2021-08-26 stsp return got_error_from_errno("fmt_scaled");
7911 f8a36e22 2021-08-26 stsp if (a->last_ncommits != ncommits) {
7912 f8a36e22 2021-08-26 stsp print_searching = 1;
7913 f8a36e22 2021-08-26 stsp a->last_ncommits = ncommits;
7916 f8a36e22 2021-08-26 stsp if (a->last_nobj_total != nobj_total) {
7917 f8a36e22 2021-08-26 stsp print_searching = 1;
7918 f8a36e22 2021-08-26 stsp print_total = 1;
7919 f8a36e22 2021-08-26 stsp a->last_nobj_total = nobj_total;
7922 f8a36e22 2021-08-26 stsp if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
7923 f8a36e22 2021-08-26 stsp strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
7924 f8a36e22 2021-08-26 stsp if (strlcpy(a->last_scaled_packsize, scaled_packsize,
7925 f8a36e22 2021-08-26 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
7926 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
7929 f8a36e22 2021-08-26 stsp if (nobj_deltify > 0 || nobj_written > 0) {
7930 f8a36e22 2021-08-26 stsp if (nobj_deltify > 0) {
7931 f8a36e22 2021-08-26 stsp p_deltify = (nobj_deltify * 100) / nobj_total;
7932 f8a36e22 2021-08-26 stsp if (p_deltify != a->last_p_deltify) {
7933 f8a36e22 2021-08-26 stsp a->last_p_deltify = p_deltify;
7934 f8a36e22 2021-08-26 stsp print_searching = 1;
7935 f8a36e22 2021-08-26 stsp print_total = 1;
7936 f8a36e22 2021-08-26 stsp print_deltify = 1;
7939 f8a36e22 2021-08-26 stsp if (nobj_written > 0) {
7940 f8a36e22 2021-08-26 stsp p_written = (nobj_written * 100) / nobj_total;
7941 f8a36e22 2021-08-26 stsp if (p_written != a->last_p_written) {
7942 f8a36e22 2021-08-26 stsp a->last_p_written = p_written;
7943 f8a36e22 2021-08-26 stsp print_searching = 1;
7944 f8a36e22 2021-08-26 stsp print_total = 1;
7945 f8a36e22 2021-08-26 stsp print_deltify = 1;
7946 f8a36e22 2021-08-26 stsp print_written = 1;
7951 f8a36e22 2021-08-26 stsp if (bytes_sent > 0) {
7952 f8a36e22 2021-08-26 stsp p_sent = (bytes_sent * 100) / packfile_size;
7953 f8a36e22 2021-08-26 stsp if (p_sent != a->last_p_sent) {
7954 f8a36e22 2021-08-26 stsp a->last_p_sent = p_sent;
7955 f8a36e22 2021-08-26 stsp print_searching = 1;
7956 f8a36e22 2021-08-26 stsp print_total = 1;
7957 f8a36e22 2021-08-26 stsp print_deltify = 1;
7958 f8a36e22 2021-08-26 stsp print_written = 1;
7959 f8a36e22 2021-08-26 stsp print_sent = 1;
7961 f8a36e22 2021-08-26 stsp a->sent_something = 1;
7964 f8a36e22 2021-08-26 stsp if (print_searching || print_total || print_deltify || print_written ||
7965 f8a36e22 2021-08-26 stsp print_sent)
7966 f8a36e22 2021-08-26 stsp printf("\r");
7967 f8a36e22 2021-08-26 stsp if (print_searching)
7968 f8a36e22 2021-08-26 stsp printf("packing %d reference%s", ncommits,
7969 f8a36e22 2021-08-26 stsp ncommits == 1 ? "" : "s");
7970 f8a36e22 2021-08-26 stsp if (print_total)
7971 f8a36e22 2021-08-26 stsp printf("; %d object%s", nobj_total,
7972 f8a36e22 2021-08-26 stsp nobj_total == 1 ? "" : "s");
7973 f8a36e22 2021-08-26 stsp if (print_deltify)
7974 f8a36e22 2021-08-26 stsp printf("; deltify: %d%%", p_deltify);
7975 f8a36e22 2021-08-26 stsp if (print_sent)
7976 d2f35ef7 2022-02-13 thomas printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
7977 f8a36e22 2021-08-26 stsp scaled_packsize, p_sent);
7978 f8a36e22 2021-08-26 stsp else if (print_written)
7979 d2f35ef7 2022-02-13 thomas printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
7980 f8a36e22 2021-08-26 stsp scaled_packsize, p_written);
7981 f8a36e22 2021-08-26 stsp if (print_searching || print_total || print_deltify ||
7982 f8a36e22 2021-08-26 stsp print_written || print_sent) {
7983 f8a36e22 2021-08-26 stsp a->printed_something = 1;
7984 f8a36e22 2021-08-26 stsp fflush(stdout);
7986 f8a36e22 2021-08-26 stsp return NULL;
7989 f8a36e22 2021-08-26 stsp static const struct got_error *
7990 f8a36e22 2021-08-26 stsp cmd_send(int argc, char *argv[])
7992 f8a36e22 2021-08-26 stsp const struct got_error *error = NULL;
7993 f8a36e22 2021-08-26 stsp char *cwd = NULL, *repo_path = NULL;
7994 f8a36e22 2021-08-26 stsp const char *remote_name;
7995 f8a36e22 2021-08-26 stsp char *proto = NULL, *host = NULL, *port = NULL;
7996 f8a36e22 2021-08-26 stsp char *repo_name = NULL, *server_path = NULL;
7997 f8a36e22 2021-08-26 stsp const struct got_remote_repo *remotes, *remote = NULL;
7998 f8a36e22 2021-08-26 stsp int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
7999 f8a36e22 2021-08-26 stsp struct got_repository *repo = NULL;
8000 f8a36e22 2021-08-26 stsp struct got_worktree *worktree = NULL;
8001 f8a36e22 2021-08-26 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
8002 f8a36e22 2021-08-26 stsp struct got_pathlist_head branches;
8003 f8a36e22 2021-08-26 stsp struct got_pathlist_head tags;
8004 f8a36e22 2021-08-26 stsp struct got_reflist_head all_branches;
8005 f8a36e22 2021-08-26 stsp struct got_reflist_head all_tags;
8006 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_args;
8007 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_branches;
8008 f8a36e22 2021-08-26 stsp struct got_reflist_entry *re;
8009 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
8010 f8a36e22 2021-08-26 stsp int i, ch, sendfd = -1, sendstatus;
8011 f8a36e22 2021-08-26 stsp pid_t sendpid = -1;
8012 f8a36e22 2021-08-26 stsp struct got_send_progress_arg spa;
8013 f8a36e22 2021-08-26 stsp int verbosity = 0, overwrite_refs = 0;
8014 f8a36e22 2021-08-26 stsp int send_all_branches = 0, send_all_tags = 0;
8015 f8a36e22 2021-08-26 stsp struct got_reference *ref = NULL;
8017 f8a36e22 2021-08-26 stsp TAILQ_INIT(&branches);
8018 f8a36e22 2021-08-26 stsp TAILQ_INIT(&tags);
8019 f8a36e22 2021-08-26 stsp TAILQ_INIT(&all_branches);
8020 f8a36e22 2021-08-26 stsp TAILQ_INIT(&all_tags);
8021 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_args);
8022 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_branches);
8024 f8a36e22 2021-08-26 stsp while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
8025 f8a36e22 2021-08-26 stsp switch (ch) {
8027 f8a36e22 2021-08-26 stsp send_all_branches = 1;
8030 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches, optarg, NULL);
8031 f8a36e22 2021-08-26 stsp if (error)
8032 f8a36e22 2021-08-26 stsp return error;
8033 f8a36e22 2021-08-26 stsp nbranches++;
8036 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&delete_args, optarg, NULL);
8037 f8a36e22 2021-08-26 stsp if (error)
8038 f8a36e22 2021-08-26 stsp return error;
8041 f8a36e22 2021-08-26 stsp overwrite_refs = 1;
8044 f8a36e22 2021-08-26 stsp repo_path = realpath(optarg, NULL);
8045 f8a36e22 2021-08-26 stsp if (repo_path == NULL)
8046 f8a36e22 2021-08-26 stsp return got_error_from_errno2("realpath",
8048 f8a36e22 2021-08-26 stsp got_path_strip_trailing_slashes(repo_path);
8051 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&tags, optarg, NULL);
8052 f8a36e22 2021-08-26 stsp if (error)
8053 f8a36e22 2021-08-26 stsp return error;
8057 f8a36e22 2021-08-26 stsp send_all_tags = 1;
8060 f8a36e22 2021-08-26 stsp if (verbosity < 0)
8061 f8a36e22 2021-08-26 stsp verbosity = 0;
8062 f8a36e22 2021-08-26 stsp else if (verbosity < 3)
8063 f8a36e22 2021-08-26 stsp verbosity++;
8066 f8a36e22 2021-08-26 stsp verbosity = -1;
8069 f8a36e22 2021-08-26 stsp usage_send();
8070 f8a36e22 2021-08-26 stsp /* NOTREACHED */
8073 f8a36e22 2021-08-26 stsp argc -= optind;
8074 f8a36e22 2021-08-26 stsp argv += optind;
8076 f8a36e22 2021-08-26 stsp if (send_all_branches && !TAILQ_EMPTY(&branches))
8077 f8a36e22 2021-08-26 stsp option_conflict('a', 'b');
8078 f8a36e22 2021-08-26 stsp if (send_all_tags && !TAILQ_EMPTY(&tags))
8079 f8a36e22 2021-08-26 stsp option_conflict('T', 't');
8082 f8a36e22 2021-08-26 stsp if (argc == 0)
8083 f8a36e22 2021-08-26 stsp remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
8084 f8a36e22 2021-08-26 stsp else if (argc == 1)
8085 f8a36e22 2021-08-26 stsp remote_name = argv[0];
8087 f8a36e22 2021-08-26 stsp usage_send();
8089 f8a36e22 2021-08-26 stsp cwd = getcwd(NULL, 0);
8090 f8a36e22 2021-08-26 stsp if (cwd == NULL) {
8091 f8a36e22 2021-08-26 stsp error = got_error_from_errno("getcwd");
8092 f8a36e22 2021-08-26 stsp goto done;
8095 f8a36e22 2021-08-26 stsp if (repo_path == NULL) {
8096 f8a36e22 2021-08-26 stsp error = got_worktree_open(&worktree, cwd);
8097 f8a36e22 2021-08-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8098 f8a36e22 2021-08-26 stsp goto done;
8100 f8a36e22 2021-08-26 stsp error = NULL;
8101 f8a36e22 2021-08-26 stsp if (worktree) {
8102 f8a36e22 2021-08-26 stsp repo_path =
8103 f8a36e22 2021-08-26 stsp strdup(got_worktree_get_repo_path(worktree));
8104 f8a36e22 2021-08-26 stsp if (repo_path == NULL)
8105 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
8106 f8a36e22 2021-08-26 stsp if (error)
8107 f8a36e22 2021-08-26 stsp goto done;
8109 f8a36e22 2021-08-26 stsp repo_path = strdup(cwd);
8110 f8a36e22 2021-08-26 stsp if (repo_path == NULL) {
8111 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
8112 f8a36e22 2021-08-26 stsp goto done;
8117 f8a36e22 2021-08-26 stsp error = got_repo_open(&repo, repo_path, NULL);
8118 f8a36e22 2021-08-26 stsp if (error)
8119 f8a36e22 2021-08-26 stsp goto done;
8121 f8a36e22 2021-08-26 stsp if (worktree) {
8122 f8a36e22 2021-08-26 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
8123 f8a36e22 2021-08-26 stsp if (worktree_conf) {
8124 f8a36e22 2021-08-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
8125 f8a36e22 2021-08-26 stsp worktree_conf);
8126 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
8127 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
8128 f8a36e22 2021-08-26 stsp remote = &remotes[i];
8134 f8a36e22 2021-08-26 stsp if (remote == NULL) {
8135 f8a36e22 2021-08-26 stsp repo_conf = got_repo_get_gotconfig(repo);
8136 f8a36e22 2021-08-26 stsp if (repo_conf) {
8137 f8a36e22 2021-08-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
8138 f8a36e22 2021-08-26 stsp repo_conf);
8139 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
8140 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
8141 f8a36e22 2021-08-26 stsp remote = &remotes[i];
8147 f8a36e22 2021-08-26 stsp if (remote == NULL) {
8148 f8a36e22 2021-08-26 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
8149 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
8150 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
8151 f8a36e22 2021-08-26 stsp remote = &remotes[i];
8156 f8a36e22 2021-08-26 stsp if (remote == NULL) {
8157 f8a36e22 2021-08-26 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
8158 f8a36e22 2021-08-26 stsp goto done;
8161 5e5da8c4 2021-09-05 stsp error = got_dial_parse_uri(&proto, &host, &port, &server_path,
8162 6480c871 2021-08-30 stsp &repo_name, remote->send_url);
8163 f8a36e22 2021-08-26 stsp if (error)
8164 f8a36e22 2021-08-26 stsp goto done;
8166 f8a36e22 2021-08-26 stsp if (strcmp(proto, "git") == 0) {
8167 f8a36e22 2021-08-26 stsp #ifndef PROFILE
8168 f8a36e22 2021-08-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8169 f8a36e22 2021-08-26 stsp "sendfd dns inet unveil", NULL) == -1)
8170 f8a36e22 2021-08-26 stsp err(1, "pledge");
8172 f8a36e22 2021-08-26 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
8173 f8a36e22 2021-08-26 stsp strcmp(proto, "ssh") == 0) {
8174 f8a36e22 2021-08-26 stsp #ifndef PROFILE
8175 f8a36e22 2021-08-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8176 f8a36e22 2021-08-26 stsp "sendfd unveil", NULL) == -1)
8177 f8a36e22 2021-08-26 stsp err(1, "pledge");
8179 f8a36e22 2021-08-26 stsp } else if (strcmp(proto, "http") == 0 ||
8180 f8a36e22 2021-08-26 stsp strcmp(proto, "git+http") == 0) {
8181 f8a36e22 2021-08-26 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
8182 f8a36e22 2021-08-26 stsp goto done;
8184 f8a36e22 2021-08-26 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
8185 f8a36e22 2021-08-26 stsp goto done;
8188 d65a88a2 2021-09-05 stsp error = got_dial_apply_unveil(proto);
8189 d65a88a2 2021-09-05 stsp if (error)
8190 d65a88a2 2021-09-05 stsp goto done;
8192 f8a36e22 2021-08-26 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
8193 f8a36e22 2021-08-26 stsp if (error)
8194 f8a36e22 2021-08-26 stsp goto done;
8196 f8a36e22 2021-08-26 stsp if (send_all_branches) {
8197 f8a36e22 2021-08-26 stsp error = got_ref_list(&all_branches, repo, "refs/heads",
8198 f8a36e22 2021-08-26 stsp got_ref_cmp_by_name, NULL);
8199 f8a36e22 2021-08-26 stsp if (error)
8200 f8a36e22 2021-08-26 stsp goto done;
8201 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &all_branches, entry) {
8202 f8a36e22 2021-08-26 stsp const char *branchname = got_ref_get_name(re->ref);
8203 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches,
8204 f8a36e22 2021-08-26 stsp branchname, NULL);
8205 f8a36e22 2021-08-26 stsp if (error)
8206 f8a36e22 2021-08-26 stsp goto done;
8207 f8a36e22 2021-08-26 stsp nbranches++;
8209 eac1df47 2021-09-01 stsp } else if (nbranches == 0) {
8210 eac1df47 2021-09-01 stsp for (i = 0; i < remote->nsend_branches; i++) {
8211 eac1df47 2021-09-01 stsp got_pathlist_append(&branches,
8212 eac1df47 2021-09-01 stsp remote->send_branches[i], NULL);
8216 f8a36e22 2021-08-26 stsp if (send_all_tags) {
8217 f8a36e22 2021-08-26 stsp error = got_ref_list(&all_tags, repo, "refs/tags",
8218 f8a36e22 2021-08-26 stsp got_ref_cmp_by_name, NULL);
8219 f8a36e22 2021-08-26 stsp if (error)
8220 f8a36e22 2021-08-26 stsp goto done;
8221 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &all_tags, entry) {
8222 f8a36e22 2021-08-26 stsp const char *tagname = got_ref_get_name(re->ref);
8223 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&tags,
8224 f8a36e22 2021-08-26 stsp tagname, NULL);
8225 f8a36e22 2021-08-26 stsp if (error)
8226 f8a36e22 2021-08-26 stsp goto done;
8232 f8a36e22 2021-08-26 stsp * To prevent accidents only branches in refs/heads/ can be deleted
8233 f8a36e22 2021-08-26 stsp * with 'got send -d'.
8234 f8a36e22 2021-08-26 stsp * Deleting anything else requires local repository access or Git.
8236 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_args, entry) {
8237 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
8239 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *new;
8240 f8a36e22 2021-08-26 stsp if (strncmp(branchname, "refs/heads/", 11) == 0) {
8241 f8a36e22 2021-08-26 stsp s = strdup(branchname);
8242 f8a36e22 2021-08-26 stsp if (s == NULL) {
8243 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
8244 f8a36e22 2021-08-26 stsp goto done;
8247 f8a36e22 2021-08-26 stsp if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
8248 f8a36e22 2021-08-26 stsp error = got_error_from_errno("asprintf");
8249 f8a36e22 2021-08-26 stsp goto done;
8252 f8a36e22 2021-08-26 stsp error = got_pathlist_insert(&new, &delete_branches, s, NULL);
8253 f8a36e22 2021-08-26 stsp if (error || new == NULL /* duplicate */)
8255 f8a36e22 2021-08-26 stsp if (error)
8256 f8a36e22 2021-08-26 stsp goto done;
8257 f8a36e22 2021-08-26 stsp ndelete_branches++;
8260 f8a36e22 2021-08-26 stsp if (nbranches == 0 && ndelete_branches == 0) {
8261 f8a36e22 2021-08-26 stsp struct got_reference *head_ref;
8262 f8a36e22 2021-08-26 stsp if (worktree)
8263 f8a36e22 2021-08-26 stsp error = got_ref_open(&head_ref, repo,
8264 f8a36e22 2021-08-26 stsp got_worktree_get_head_ref_name(worktree), 0);
8266 f8a36e22 2021-08-26 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
8267 f8a36e22 2021-08-26 stsp if (error)
8268 f8a36e22 2021-08-26 stsp goto done;
8269 f8a36e22 2021-08-26 stsp if (got_ref_is_symbolic(head_ref)) {
8270 f8a36e22 2021-08-26 stsp error = got_ref_resolve_symbolic(&ref, repo, head_ref);
8271 f8a36e22 2021-08-26 stsp got_ref_close(head_ref);
8272 f8a36e22 2021-08-26 stsp if (error)
8273 f8a36e22 2021-08-26 stsp goto done;
8275 f8a36e22 2021-08-26 stsp ref = head_ref;
8276 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches, got_ref_get_name(ref),
8278 f8a36e22 2021-08-26 stsp if (error)
8279 f8a36e22 2021-08-26 stsp goto done;
8280 f8a36e22 2021-08-26 stsp nbranches++;
8283 f8a36e22 2021-08-26 stsp if (verbosity >= 0)
8284 f8a36e22 2021-08-26 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
8285 f8a36e22 2021-08-26 stsp port ? ":" : "", port ? port : "");
8287 f8a36e22 2021-08-26 stsp error = got_send_connect(&sendpid, &sendfd, proto, host, port,
8288 f8a36e22 2021-08-26 stsp server_path, verbosity);
8289 f8a36e22 2021-08-26 stsp if (error)
8290 f8a36e22 2021-08-26 stsp goto done;
8292 f8a36e22 2021-08-26 stsp memset(&spa, 0, sizeof(spa));
8293 f8a36e22 2021-08-26 stsp spa.last_scaled_packsize[0] = '\0';
8294 f8a36e22 2021-08-26 stsp spa.last_p_deltify = -1;
8295 f8a36e22 2021-08-26 stsp spa.last_p_written = -1;
8296 f8a36e22 2021-08-26 stsp spa.verbosity = verbosity;
8297 f8a36e22 2021-08-26 stsp spa.delete_branches = &delete_branches;
8298 f8a36e22 2021-08-26 stsp error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
8299 f8a36e22 2021-08-26 stsp verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
8300 f8a36e22 2021-08-26 stsp check_cancelled, NULL);
8301 f8a36e22 2021-08-26 stsp if (spa.printed_something)
8302 f8a36e22 2021-08-26 stsp putchar('\n');
8303 f8a36e22 2021-08-26 stsp if (error)
8304 f8a36e22 2021-08-26 stsp goto done;
8305 f8a36e22 2021-08-26 stsp if (!spa.sent_something && verbosity >= 0)
8306 f8a36e22 2021-08-26 stsp printf("Already up-to-date\n");
8308 f8a36e22 2021-08-26 stsp if (sendpid > 0) {
8309 f8a36e22 2021-08-26 stsp if (kill(sendpid, SIGTERM) == -1)
8310 f8a36e22 2021-08-26 stsp error = got_error_from_errno("kill");
8311 f8a36e22 2021-08-26 stsp if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
8312 f8a36e22 2021-08-26 stsp error = got_error_from_errno("waitpid");
8314 f8a36e22 2021-08-26 stsp if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
8315 f8a36e22 2021-08-26 stsp error = got_error_from_errno("close");
8316 f8a36e22 2021-08-26 stsp if (repo) {
8317 f8a36e22 2021-08-26 stsp const struct got_error *close_err = got_repo_close(repo);
8318 f8a36e22 2021-08-26 stsp if (error == NULL)
8319 f8a36e22 2021-08-26 stsp error = close_err;
8321 f8a36e22 2021-08-26 stsp if (worktree)
8322 f8a36e22 2021-08-26 stsp got_worktree_close(worktree);
8324 f8a36e22 2021-08-26 stsp got_ref_close(ref);
8325 f8a36e22 2021-08-26 stsp got_pathlist_free(&branches);
8326 f8a36e22 2021-08-26 stsp got_pathlist_free(&tags);
8327 f8a36e22 2021-08-26 stsp got_ref_list_free(&all_branches);
8328 f8a36e22 2021-08-26 stsp got_ref_list_free(&all_tags);
8329 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_args);
8330 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_branches, entry)
8331 f8a36e22 2021-08-26 stsp free((char *)pe->path);
8332 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_branches);
8333 f8a36e22 2021-08-26 stsp free(cwd);
8334 f8a36e22 2021-08-26 stsp free(repo_path);
8335 f8a36e22 2021-08-26 stsp free(proto);
8336 f8a36e22 2021-08-26 stsp free(host);
8337 f8a36e22 2021-08-26 stsp free(port);
8338 f8a36e22 2021-08-26 stsp free(server_path);
8339 f8a36e22 2021-08-26 stsp free(repo_name);
8340 234035bc 2019-06-01 stsp return error;
8343 234035bc 2019-06-01 stsp __dead static void
8344 234035bc 2019-06-01 stsp usage_cherrypick(void)
8346 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
8350 234035bc 2019-06-01 stsp static const struct got_error *
8351 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
8353 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
8354 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
8355 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
8356 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
8357 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
8358 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
8359 234035bc 2019-06-01 stsp struct got_object_qid *pid;
8361 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8363 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8364 234035bc 2019-06-01 stsp switch (ch) {
8366 234035bc 2019-06-01 stsp usage_cherrypick();
8367 234035bc 2019-06-01 stsp /* NOTREACHED */
8371 234035bc 2019-06-01 stsp argc -= optind;
8372 234035bc 2019-06-01 stsp argv += optind;
8374 43012d58 2019-07-14 stsp #ifndef PROFILE
8375 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8376 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8377 43012d58 2019-07-14 stsp err(1, "pledge");
8379 234035bc 2019-06-01 stsp if (argc != 1)
8380 234035bc 2019-06-01 stsp usage_cherrypick();
8382 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
8383 234035bc 2019-06-01 stsp if (cwd == NULL) {
8384 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
8385 234035bc 2019-06-01 stsp goto done;
8387 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
8388 fa51e947 2020-03-27 stsp if (error) {
8389 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8390 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
8392 234035bc 2019-06-01 stsp goto done;
8395 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8397 234035bc 2019-06-01 stsp if (error != NULL)
8398 234035bc 2019-06-01 stsp goto done;
8400 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8401 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
8402 234035bc 2019-06-01 stsp if (error)
8403 234035bc 2019-06-01 stsp goto done;
8405 2d0f364c 2022-03-11 thomas error = got_repo_match_object_id(&commit_id, NULL, argv[0],
8406 2d0f364c 2022-03-11 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
8407 2d0f364c 2022-03-11 thomas if (error)
8408 2d0f364c 2022-03-11 thomas goto done;
8409 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
8410 234035bc 2019-06-01 stsp if (error)
8411 234035bc 2019-06-01 stsp goto done;
8413 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8414 234035bc 2019-06-01 stsp if (error)
8415 234035bc 2019-06-01 stsp goto done;
8416 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8417 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8418 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
8419 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
8421 234035bc 2019-06-01 stsp if (error != NULL)
8422 234035bc 2019-06-01 stsp goto done;
8424 9627c110 2020-04-18 stsp if (upa.did_something)
8425 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
8426 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
8428 234035bc 2019-06-01 stsp if (commit)
8429 234035bc 2019-06-01 stsp got_object_commit_close(commit);
8430 234035bc 2019-06-01 stsp free(commit_id_str);
8431 234035bc 2019-06-01 stsp if (worktree)
8432 234035bc 2019-06-01 stsp got_worktree_close(worktree);
8433 1d0f4054 2021-06-17 stsp if (repo) {
8434 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8435 1d0f4054 2021-06-17 stsp if (error == NULL)
8436 1d0f4054 2021-06-17 stsp error = close_err;
8438 c4296144 2019-05-09 stsp return error;
8441 5ef14e63 2019-06-02 stsp __dead static void
8442 5ef14e63 2019-06-02 stsp usage_backout(void)
8444 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
8448 5ef14e63 2019-06-02 stsp static const struct got_error *
8449 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
8451 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
8452 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
8453 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
8454 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
8455 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
8456 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
8457 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
8459 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8461 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8462 5ef14e63 2019-06-02 stsp switch (ch) {
8464 5ef14e63 2019-06-02 stsp usage_backout();
8465 5ef14e63 2019-06-02 stsp /* NOTREACHED */
8469 5ef14e63 2019-06-02 stsp argc -= optind;
8470 5ef14e63 2019-06-02 stsp argv += optind;
8472 43012d58 2019-07-14 stsp #ifndef PROFILE
8473 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8474 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8475 43012d58 2019-07-14 stsp err(1, "pledge");
8477 5ef14e63 2019-06-02 stsp if (argc != 1)
8478 5ef14e63 2019-06-02 stsp usage_backout();
8480 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
8481 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
8482 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
8483 5ef14e63 2019-06-02 stsp goto done;
8485 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
8486 fa51e947 2020-03-27 stsp if (error) {
8487 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8488 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
8489 5ef14e63 2019-06-02 stsp goto done;
8492 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8494 5ef14e63 2019-06-02 stsp if (error != NULL)
8495 5ef14e63 2019-06-02 stsp goto done;
8497 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8498 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
8499 5ef14e63 2019-06-02 stsp if (error)
8500 5ef14e63 2019-06-02 stsp goto done;
8502 2d0f364c 2022-03-11 thomas error = got_repo_match_object_id(&commit_id, NULL, argv[0],
8503 2d0f364c 2022-03-11 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
8504 2d0f364c 2022-03-11 thomas if (error)
8505 2d0f364c 2022-03-11 thomas goto done;
8506 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
8507 5ef14e63 2019-06-02 stsp if (error)
8508 5ef14e63 2019-06-02 stsp goto done;
8510 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8511 5ef14e63 2019-06-02 stsp if (error)
8512 5ef14e63 2019-06-02 stsp goto done;
8513 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8514 5ef14e63 2019-06-02 stsp if (pid == NULL) {
8515 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
8516 5ef14e63 2019-06-02 stsp goto done;
8519 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8520 10604dce 2021-09-24 thomas error = got_worktree_merge_files(worktree, commit_id, pid->id,
8521 10604dce 2021-09-24 thomas repo, update_progress, &upa, check_cancelled, NULL);
8522 5ef14e63 2019-06-02 stsp if (error != NULL)
8523 5ef14e63 2019-06-02 stsp goto done;
8525 9627c110 2020-04-18 stsp if (upa.did_something)
8526 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
8527 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
8529 5ef14e63 2019-06-02 stsp if (commit)
8530 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
8531 5ef14e63 2019-06-02 stsp free(commit_id_str);
8532 818c7501 2019-07-11 stsp if (worktree)
8533 818c7501 2019-07-11 stsp got_worktree_close(worktree);
8534 1d0f4054 2021-06-17 stsp if (repo) {
8535 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8536 1d0f4054 2021-06-17 stsp if (error == NULL)
8537 1d0f4054 2021-06-17 stsp error = close_err;
8539 818c7501 2019-07-11 stsp return error;
8542 818c7501 2019-07-11 stsp __dead static void
8543 818c7501 2019-07-11 stsp usage_rebase(void)
8545 643b85bc 2021-07-16 stsp fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
8546 af54c8f8 2019-07-11 stsp getprogname());
8551 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
8554 0ebf8283 2019-07-24 stsp size_t len;
8556 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
8557 0ebf8283 2019-07-24 stsp if (len > limit)
8558 0ebf8283 2019-07-24 stsp len = limit;
8559 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
8560 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
8562 0ebf8283 2019-07-24 stsp *nl = '\0';
8565 0ebf8283 2019-07-24 stsp static const struct got_error *
8566 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
8568 5943eee2 2019-08-13 stsp const struct got_error *err;
8569 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
8570 5943eee2 2019-08-13 stsp const char *s;
8572 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
8574 5943eee2 2019-08-13 stsp return err;
8576 5943eee2 2019-08-13 stsp s = logmsg0;
8577 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
8580 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
8581 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
8582 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
8583 5943eee2 2019-08-13 stsp goto done;
8586 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
8588 5943eee2 2019-08-13 stsp free(logmsg0);
8589 a0ea4fc0 2020-02-28 stsp return err;
8592 a0ea4fc0 2020-02-28 stsp static const struct got_error *
8593 48b4f239 2021-12-31 thomas show_rebase_merge_conflict(struct got_object_id *id,
8594 48b4f239 2021-12-31 thomas struct got_repository *repo)
8596 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
8597 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
8598 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
8600 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
8602 a0ea4fc0 2020-02-28 stsp return err;
8604 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
8606 a0ea4fc0 2020-02-28 stsp goto done;
8608 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
8610 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
8612 a0ea4fc0 2020-02-28 stsp goto done;
8614 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
8616 a0ea4fc0 2020-02-28 stsp free(id_str);
8617 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
8618 a0ea4fc0 2020-02-28 stsp free(logmsg);
8619 5943eee2 2019-08-13 stsp return err;
8622 818c7501 2019-07-11 stsp static const struct got_error *
8623 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
8624 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
8626 818c7501 2019-07-11 stsp const struct got_error *err;
8627 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8629 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
8631 818c7501 2019-07-11 stsp goto done;
8633 ff0d2220 2019-07-11 stsp if (new_id) {
8634 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
8636 ff0d2220 2019-07-11 stsp goto done;
8639 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
8640 ff0d2220 2019-07-11 stsp if (new_id_str)
8641 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
8643 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8645 0ebf8283 2019-07-24 stsp goto done;
8647 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
8648 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8650 818c7501 2019-07-11 stsp free(old_id_str);
8651 818c7501 2019-07-11 stsp free(new_id_str);
8652 272a1371 2020-02-28 stsp free(logmsg);
8653 818c7501 2019-07-11 stsp return err;
8656 1ee397ad 2019-07-12 stsp static const struct got_error *
8657 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
8658 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
8659 e600f124 2021-03-21 stsp struct got_reference *tmp_branch, struct got_repository *repo,
8660 e600f124 2021-03-21 stsp int create_backup)
8662 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
8663 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
8664 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, branch, repo, create_backup);
8667 818c7501 2019-07-11 stsp static const struct got_error *
8668 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
8669 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8670 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
8671 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8673 ff0d2220 2019-07-11 stsp const struct got_error *error;
8674 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
8675 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
8677 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8678 ff0d2220 2019-07-11 stsp if (error)
8679 ff0d2220 2019-07-11 stsp return error;
8681 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
8682 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
8683 ff0d2220 2019-07-11 stsp if (error) {
8684 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
8685 ff0d2220 2019-07-11 stsp goto done;
8686 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
8688 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
8689 ff0d2220 2019-07-11 stsp free(new_commit_id);
8692 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
8693 ff0d2220 2019-07-11 stsp return error;
8696 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
8697 64c6d990 2019-07-11 stsp const char *path_prefix;
8698 64c6d990 2019-07-11 stsp size_t len;
8699 8ca9bd68 2019-07-25 stsp int errcode;
8702 64c6d990 2019-07-11 stsp static const struct got_error *
8703 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
8704 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
8705 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
8706 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
8708 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
8710 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
8711 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
8712 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
8714 64c6d990 2019-07-11 stsp return NULL;
8717 64c6d990 2019-07-11 stsp static const struct got_error *
8718 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
8719 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
8720 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
8722 64c6d990 2019-07-11 stsp const struct got_error *err;
8723 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
8724 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
8725 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
8727 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
8728 64c6d990 2019-07-11 stsp return NULL;
8730 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8732 64c6d990 2019-07-11 stsp goto done;
8734 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
8736 64c6d990 2019-07-11 stsp goto done;
8738 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
8739 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
8741 64c6d990 2019-07-11 stsp goto done;
8743 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
8744 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
8746 64c6d990 2019-07-11 stsp goto done;
8748 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
8749 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
8750 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
8751 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
8752 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
8753 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
8754 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
8756 64c6d990 2019-07-11 stsp if (tree1)
8757 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
8758 64c6d990 2019-07-11 stsp if (tree2)
8759 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
8760 64c6d990 2019-07-11 stsp if (commit)
8761 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
8762 64c6d990 2019-07-11 stsp if (parent_commit)
8763 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
8764 64c6d990 2019-07-11 stsp return err;
8767 ff0d2220 2019-07-11 stsp static const struct got_error *
8768 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
8769 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
8770 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
8771 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
8772 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
8774 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
8775 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
8776 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
8777 af61c510 2019-07-19 stsp struct got_object_qid *qid;
8778 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
8780 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
8782 af61c510 2019-07-19 stsp return err;
8784 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
8785 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
8787 af61c510 2019-07-19 stsp goto done;
8788 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
8789 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
8790 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
8791 af61c510 2019-07-19 stsp if (err) {
8792 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
8793 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
8794 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
8795 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
8796 af61c510 2019-07-19 stsp "been reached?!?");
8798 ee780d5c 2020-01-04 stsp goto done;
8800 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
8801 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
8803 af61c510 2019-07-19 stsp goto done;
8805 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
8807 af61c510 2019-07-19 stsp goto done;
8808 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(commits, qid, entry);
8809 af61c510 2019-07-19 stsp commit_id = parent_id;
8813 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
8814 af61c510 2019-07-19 stsp return err;
8817 e600f124 2021-03-21 stsp static const struct got_error *
8818 e600f124 2021-03-21 stsp get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
8820 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
8821 e600f124 2021-03-21 stsp time_t committer_time;
8822 e600f124 2021-03-21 stsp struct tm tm;
8823 e600f124 2021-03-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
8824 e600f124 2021-03-21 stsp char *author0 = NULL, *author, *smallerthan;
8825 e600f124 2021-03-21 stsp char *logmsg0 = NULL, *logmsg, *newline;
8827 e600f124 2021-03-21 stsp committer_time = got_object_commit_get_committer_time(commit);
8828 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
8829 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
8830 e3199de8 2021-03-21 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
8831 e600f124 2021-03-21 stsp return got_error(GOT_ERR_NO_SPACE);
8833 e600f124 2021-03-21 stsp author0 = strdup(got_object_commit_get_author(commit));
8834 e600f124 2021-03-21 stsp if (author0 == NULL)
8835 e600f124 2021-03-21 stsp return got_error_from_errno("strdup");
8836 e600f124 2021-03-21 stsp author = author0;
8837 e600f124 2021-03-21 stsp smallerthan = strchr(author, '<');
8838 e600f124 2021-03-21 stsp if (smallerthan && smallerthan[1] != '\0')
8839 e600f124 2021-03-21 stsp author = smallerthan + 1;
8840 e600f124 2021-03-21 stsp author[strcspn(author, "@>")] = '\0';
8842 e600f124 2021-03-21 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
8844 e600f124 2021-03-21 stsp goto done;
8845 e600f124 2021-03-21 stsp logmsg = logmsg0;
8846 e600f124 2021-03-21 stsp while (*logmsg == '\n')
8848 e600f124 2021-03-21 stsp newline = strchr(logmsg, '\n');
8849 e600f124 2021-03-21 stsp if (newline)
8850 e600f124 2021-03-21 stsp *newline = '\0';
8852 e600f124 2021-03-21 stsp if (asprintf(brief_str, "%s %s %s",
8853 e600f124 2021-03-21 stsp datebuf, author, logmsg) == -1)
8854 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
8856 e600f124 2021-03-21 stsp free(author0);
8857 e600f124 2021-03-21 stsp free(logmsg0);
8858 643b85bc 2021-07-16 stsp return err;
8861 643b85bc 2021-07-16 stsp static const struct got_error *
8862 643b85bc 2021-07-16 stsp delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
8863 643b85bc 2021-07-16 stsp struct got_repository *repo)
8865 643b85bc 2021-07-16 stsp const struct got_error *err;
8866 643b85bc 2021-07-16 stsp char *id_str;
8868 643b85bc 2021-07-16 stsp err = got_object_id_str(&id_str, id);
8870 643b85bc 2021-07-16 stsp return err;
8872 643b85bc 2021-07-16 stsp err = got_ref_delete(ref, repo);
8874 643b85bc 2021-07-16 stsp goto done;
8876 643b85bc 2021-07-16 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
8878 643b85bc 2021-07-16 stsp free(id_str);
8879 e600f124 2021-03-21 stsp return err;
8882 e600f124 2021-03-21 stsp static const struct got_error *
8883 e600f124 2021-03-21 stsp print_backup_ref(const char *branch_name, const char *new_id_str,
8884 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
8885 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap,
8886 e600f124 2021-03-21 stsp struct got_repository *repo)
8888 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
8889 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
8890 e600f124 2021-03-21 stsp char *refs_str = NULL;
8891 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id = NULL;
8892 e600f124 2021-03-21 stsp struct got_commit_object *new_commit = NULL;
8893 e600f124 2021-03-21 stsp char *new_commit_brief_str = NULL;
8894 e600f124 2021-03-21 stsp struct got_object_id *yca_id = NULL;
8895 e600f124 2021-03-21 stsp struct got_commit_object *yca_commit = NULL;
8896 e600f124 2021-03-21 stsp char *yca_id_str = NULL, *yca_brief_str = NULL;
8897 e600f124 2021-03-21 stsp char *custom_refs_str;
8899 e600f124 2021-03-21 stsp if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
8900 e600f124 2021-03-21 stsp return got_error_from_errno("asprintf");
8902 e600f124 2021-03-21 stsp err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
8903 e600f124 2021-03-21 stsp 0, 0, refs_idmap, custom_refs_str);
8905 e600f124 2021-03-21 stsp goto done;
8907 e600f124 2021-03-21 stsp err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
8909 e600f124 2021-03-21 stsp goto done;
8911 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
8912 e600f124 2021-03-21 stsp if (refs) {
8913 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, new_commit_id, repo);
8915 e600f124 2021-03-21 stsp goto done;
8918 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
8920 e600f124 2021-03-21 stsp goto done;
8922 e600f124 2021-03-21 stsp err = get_commit_brief_str(&new_commit_brief_str, new_commit);
8924 e600f124 2021-03-21 stsp goto done;
8926 e600f124 2021-03-21 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8927 768705e3 2021-09-27 thomas old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
8929 e600f124 2021-03-21 stsp goto done;
8931 e600f124 2021-03-21 stsp printf("has become commit %s%s%s%s\n %s\n", new_id_str,
8932 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
8933 e600f124 2021-03-21 stsp refs_str ? ")" : "", new_commit_brief_str);
8934 e600f124 2021-03-21 stsp if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
8935 e600f124 2021-03-21 stsp got_object_id_cmp(yca_id, old_commit_id) != 0) {
8936 e600f124 2021-03-21 stsp free(refs_str);
8937 e600f124 2021-03-21 stsp refs_str = NULL;
8939 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&yca_commit, repo, yca_id);
8941 e600f124 2021-03-21 stsp goto done;
8943 e600f124 2021-03-21 stsp err = get_commit_brief_str(&yca_brief_str, yca_commit);
8945 e600f124 2021-03-21 stsp goto done;
8947 e600f124 2021-03-21 stsp err = got_object_id_str(&yca_id_str, yca_id);
8949 e600f124 2021-03-21 stsp goto done;
8951 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
8952 e600f124 2021-03-21 stsp if (refs) {
8953 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, yca_id, repo);
8955 e600f124 2021-03-21 stsp goto done;
8957 e600f124 2021-03-21 stsp printf("history forked at %s%s%s%s\n %s\n",
8958 e600f124 2021-03-21 stsp yca_id_str,
8959 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
8960 e600f124 2021-03-21 stsp refs_str ? ")" : "", yca_brief_str);
8963 e600f124 2021-03-21 stsp free(custom_refs_str);
8964 e600f124 2021-03-21 stsp free(new_commit_id);
8965 e600f124 2021-03-21 stsp free(refs_str);
8966 e600f124 2021-03-21 stsp free(yca_id);
8967 e600f124 2021-03-21 stsp free(yca_id_str);
8968 e600f124 2021-03-21 stsp free(yca_brief_str);
8969 e600f124 2021-03-21 stsp if (new_commit)
8970 e600f124 2021-03-21 stsp got_object_commit_close(new_commit);
8971 e600f124 2021-03-21 stsp if (yca_commit)
8972 e600f124 2021-03-21 stsp got_object_commit_close(yca_commit);
8974 e600f124 2021-03-21 stsp return NULL;
8977 af61c510 2019-07-19 stsp static const struct got_error *
8978 48b4f239 2021-12-31 thomas process_backup_refs(const char *backup_ref_prefix,
8979 48b4f239 2021-12-31 thomas const char *wanted_branch_name,
8980 643b85bc 2021-07-16 stsp int delete, struct got_repository *repo)
8982 e600f124 2021-03-21 stsp const struct got_error *err;
8983 e600f124 2021-03-21 stsp struct got_reflist_head refs, backup_refs;
8984 e600f124 2021-03-21 stsp struct got_reflist_entry *re;
8985 e600f124 2021-03-21 stsp const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
8986 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
8987 e600f124 2021-03-21 stsp char *branch_name = NULL;
8988 e600f124 2021-03-21 stsp struct got_commit_object *old_commit = NULL;
8989 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
8990 9e822917 2021-03-23 stsp int wanted_branch_found = 0;
8992 e600f124 2021-03-21 stsp TAILQ_INIT(&refs);
8993 e600f124 2021-03-21 stsp TAILQ_INIT(&backup_refs);
8995 e600f124 2021-03-21 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8997 e600f124 2021-03-21 stsp return err;
8999 e600f124 2021-03-21 stsp err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9001 e600f124 2021-03-21 stsp goto done;
9003 e600f124 2021-03-21 stsp if (wanted_branch_name) {
9004 e600f124 2021-03-21 stsp if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
9005 e600f124 2021-03-21 stsp wanted_branch_name += 11;
9008 e600f124 2021-03-21 stsp err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
9009 e600f124 2021-03-21 stsp got_ref_cmp_by_commit_timestamp_descending, repo);
9011 e600f124 2021-03-21 stsp goto done;
9013 e600f124 2021-03-21 stsp TAILQ_FOREACH(re, &backup_refs, entry) {
9014 e600f124 2021-03-21 stsp const char *refname = got_ref_get_name(re->ref);
9015 e600f124 2021-03-21 stsp char *slash;
9017 643b85bc 2021-07-16 stsp err = check_cancelled(NULL);
9021 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, re->ref);
9025 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&old_commit, repo,
9026 e600f124 2021-03-21 stsp old_commit_id);
9030 e600f124 2021-03-21 stsp if (strncmp(backup_ref_prefix, refname,
9031 e600f124 2021-03-21 stsp backup_ref_prefix_len) == 0)
9032 e600f124 2021-03-21 stsp refname += backup_ref_prefix_len;
9034 e600f124 2021-03-21 stsp while (refname[0] == '/')
9035 e600f124 2021-03-21 stsp refname++;
9037 e600f124 2021-03-21 stsp branch_name = strdup(refname);
9038 e600f124 2021-03-21 stsp if (branch_name == NULL) {
9039 e600f124 2021-03-21 stsp err = got_error_from_errno("strdup");
9042 e600f124 2021-03-21 stsp slash = strrchr(branch_name, '/');
9043 e600f124 2021-03-21 stsp if (slash) {
9044 e600f124 2021-03-21 stsp *slash = '\0';
9045 e600f124 2021-03-21 stsp refname += strlen(branch_name) + 1;
9048 e600f124 2021-03-21 stsp if (wanted_branch_name == NULL ||
9049 e600f124 2021-03-21 stsp strcmp(wanted_branch_name, branch_name) == 0) {
9050 9e822917 2021-03-23 stsp wanted_branch_found = 1;
9051 643b85bc 2021-07-16 stsp if (delete) {
9052 643b85bc 2021-07-16 stsp err = delete_backup_ref(re->ref,
9053 643b85bc 2021-07-16 stsp old_commit_id, repo);
9055 643b85bc 2021-07-16 stsp err = print_backup_ref(branch_name, refname,
9056 abc59930 2021-09-05 naddy old_commit_id, old_commit, refs_idmap,
9063 e600f124 2021-03-21 stsp free(old_commit_id);
9064 e600f124 2021-03-21 stsp old_commit_id = NULL;
9065 e600f124 2021-03-21 stsp free(branch_name);
9066 e600f124 2021-03-21 stsp branch_name = NULL;
9067 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
9068 e600f124 2021-03-21 stsp old_commit = NULL;
9071 9e822917 2021-03-23 stsp if (wanted_branch_name && !wanted_branch_found) {
9072 9e822917 2021-03-23 stsp err = got_error_fmt(GOT_ERR_NOT_REF,
9073 9e822917 2021-03-23 stsp "%s/%s/", backup_ref_prefix, wanted_branch_name);
9076 e600f124 2021-03-21 stsp if (refs_idmap)
9077 e600f124 2021-03-21 stsp got_reflist_object_id_map_free(refs_idmap);
9078 e600f124 2021-03-21 stsp got_ref_list_free(&refs);
9079 e600f124 2021-03-21 stsp got_ref_list_free(&backup_refs);
9080 e600f124 2021-03-21 stsp free(old_commit_id);
9081 e600f124 2021-03-21 stsp free(branch_name);
9082 e600f124 2021-03-21 stsp if (old_commit)
9083 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
9084 e600f124 2021-03-21 stsp return err;
9087 e600f124 2021-03-21 stsp static const struct got_error *
9088 d52bac28 2021-10-08 thomas abort_progress(void *arg, unsigned char status, const char *path)
9091 d52bac28 2021-10-08 thomas * Unversioned files should not clutter progress output when
9092 d52bac28 2021-10-08 thomas * an operation is aborted.
9094 d52bac28 2021-10-08 thomas if (status == GOT_STATUS_UNVERSIONED)
9095 d52bac28 2021-10-08 thomas return NULL;
9097 d52bac28 2021-10-08 thomas return update_progress(arg, status, path);
9100 d52bac28 2021-10-08 thomas static const struct got_error *
9101 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
9103 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
9104 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
9105 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
9106 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
9107 818c7501 2019-07-11 stsp char *cwd = NULL;
9108 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
9109 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
9110 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
9111 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
9112 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
9113 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
9114 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
9115 10604dce 2021-09-24 thomas int histedit_in_progress = 0, merge_in_progress = 0;
9116 10604dce 2021-09-24 thomas int create_backup = 1, list_backups = 0, delete_backups = 0;
9117 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
9118 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
9119 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
9120 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
9121 bb494413 2021-09-28 thomas struct got_update_progress_arg upa;
9123 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
9124 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
9125 bb494413 2021-09-28 thomas memset(&upa, 0, sizeof(upa));
9127 643b85bc 2021-07-16 stsp while ((ch = getopt(argc, argv, "aclX")) != -1) {
9128 818c7501 2019-07-11 stsp switch (ch) {
9130 818c7501 2019-07-11 stsp abort_rebase = 1;
9133 818c7501 2019-07-11 stsp continue_rebase = 1;
9136 e600f124 2021-03-21 stsp list_backups = 1;
9139 643b85bc 2021-07-16 stsp delete_backups = 1;
9142 818c7501 2019-07-11 stsp usage_rebase();
9143 818c7501 2019-07-11 stsp /* NOTREACHED */
9147 818c7501 2019-07-11 stsp argc -= optind;
9148 818c7501 2019-07-11 stsp argv += optind;
9150 43012d58 2019-07-14 stsp #ifndef PROFILE
9151 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9152 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
9153 43012d58 2019-07-14 stsp err(1, "pledge");
9155 e600f124 2021-03-21 stsp if (list_backups) {
9156 e600f124 2021-03-21 stsp if (abort_rebase)
9157 e600f124 2021-03-21 stsp option_conflict('l', 'a');
9158 e600f124 2021-03-21 stsp if (continue_rebase)
9159 e600f124 2021-03-21 stsp option_conflict('l', 'c');
9160 643b85bc 2021-07-16 stsp if (delete_backups)
9161 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
9162 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
9163 643b85bc 2021-07-16 stsp usage_rebase();
9164 643b85bc 2021-07-16 stsp } else if (delete_backups) {
9165 643b85bc 2021-07-16 stsp if (abort_rebase)
9166 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
9167 643b85bc 2021-07-16 stsp if (continue_rebase)
9168 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
9169 643b85bc 2021-07-16 stsp if (list_backups)
9170 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
9171 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
9172 818c7501 2019-07-11 stsp usage_rebase();
9174 e600f124 2021-03-21 stsp if (abort_rebase && continue_rebase)
9175 e600f124 2021-03-21 stsp usage_rebase();
9176 e600f124 2021-03-21 stsp else if (abort_rebase || continue_rebase) {
9177 e600f124 2021-03-21 stsp if (argc != 0)
9178 e600f124 2021-03-21 stsp usage_rebase();
9179 e600f124 2021-03-21 stsp } else if (argc != 1)
9180 e600f124 2021-03-21 stsp usage_rebase();
9183 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
9184 818c7501 2019-07-11 stsp if (cwd == NULL) {
9185 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
9186 818c7501 2019-07-11 stsp goto done;
9188 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
9189 fa51e947 2020-03-27 stsp if (error) {
9190 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
9191 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
9192 e600f124 2021-03-21 stsp goto done;
9194 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9195 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
9196 e600f124 2021-03-21 stsp "rebase", cwd);
9197 e600f124 2021-03-21 stsp goto done;
9201 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
9202 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
9203 818c7501 2019-07-11 stsp if (error != NULL)
9204 818c7501 2019-07-11 stsp goto done;
9206 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9207 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
9208 7ef62c4e 2020-02-24 stsp if (error)
9209 7ef62c4e 2020-02-24 stsp goto done;
9211 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
9212 643b85bc 2021-07-16 stsp error = process_backup_refs(
9213 643b85bc 2021-07-16 stsp GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
9214 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
9215 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
9218 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
9219 7ef62c4e 2020-02-24 stsp worktree);
9220 818c7501 2019-07-11 stsp if (error)
9221 7ef62c4e 2020-02-24 stsp goto done;
9222 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
9223 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
9224 10604dce 2021-09-24 thomas goto done;
9227 10604dce 2021-09-24 thomas error = got_worktree_merge_in_progress(&merge_in_progress,
9228 10604dce 2021-09-24 thomas worktree, repo);
9229 10604dce 2021-09-24 thomas if (error)
9230 10604dce 2021-09-24 thomas goto done;
9231 10604dce 2021-09-24 thomas if (merge_in_progress) {
9232 10604dce 2021-09-24 thomas error = got_error(GOT_ERR_MERGE_BUSY);
9233 818c7501 2019-07-11 stsp goto done;
9236 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9237 818c7501 2019-07-11 stsp if (error)
9238 818c7501 2019-07-11 stsp goto done;
9240 f6794adc 2019-07-23 stsp if (abort_rebase) {
9241 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
9242 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
9243 f6794adc 2019-07-23 stsp goto done;
9245 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
9246 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
9247 3e3a69f1 2019-07-25 stsp worktree, repo);
9248 818c7501 2019-07-11 stsp if (error)
9249 818c7501 2019-07-11 stsp goto done;
9250 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
9251 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
9252 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
9253 d52bac28 2021-10-08 thomas new_base_branch, abort_progress, &upa);
9254 818c7501 2019-07-11 stsp if (error)
9255 818c7501 2019-07-11 stsp goto done;
9256 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
9257 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
9258 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
9261 818c7501 2019-07-11 stsp if (continue_rebase) {
9262 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
9263 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
9264 f6794adc 2019-07-23 stsp goto done;
9266 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
9267 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
9268 3e3a69f1 2019-07-25 stsp worktree, repo);
9269 818c7501 2019-07-11 stsp if (error)
9270 818c7501 2019-07-11 stsp goto done;
9272 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
9273 01757395 2019-07-12 stsp resume_commit_id, repo);
9274 818c7501 2019-07-11 stsp if (error)
9275 818c7501 2019-07-11 stsp goto done;
9277 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
9278 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
9279 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
9280 818c7501 2019-07-11 stsp goto done;
9283 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
9284 818c7501 2019-07-11 stsp if (error != NULL)
9285 818c7501 2019-07-11 stsp goto done;
9288 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
9289 ff0d2220 2019-07-11 stsp if (error)
9290 ff0d2220 2019-07-11 stsp goto done;
9292 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
9293 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
9295 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
9296 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9297 768705e3 2021-09-27 thomas base_commit_id, branch_head_commit_id, 1, repo,
9298 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
9299 818c7501 2019-07-11 stsp if (error)
9300 818c7501 2019-07-11 stsp goto done;
9301 818c7501 2019-07-11 stsp if (yca_id == NULL) {
9302 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
9303 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
9304 818c7501 2019-07-11 stsp "with work tree's branch");
9305 818c7501 2019-07-11 stsp goto done;
9308 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
9309 a51a74b3 2019-07-27 stsp if (error) {
9310 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
9311 a51a74b3 2019-07-27 stsp goto done;
9312 a51a74b3 2019-07-27 stsp error = NULL;
9314 82d979c5 2021-11-04 thomas struct got_pathlist_head paths;
9315 82d979c5 2021-11-04 thomas printf("%s is already based on %s\n",
9316 df3ed485 2021-01-31 stsp got_ref_get_name(branch),
9317 df3ed485 2021-01-31 stsp got_worktree_get_head_ref_name(worktree));
9318 82d979c5 2021-11-04 thomas error = switch_head_ref(branch, branch_head_commit_id,
9319 82d979c5 2021-11-04 thomas worktree, repo);
9320 82d979c5 2021-11-04 thomas if (error)
9321 82d979c5 2021-11-04 thomas goto done;
9322 82d979c5 2021-11-04 thomas error = got_worktree_set_base_commit_id(worktree, repo,
9323 82d979c5 2021-11-04 thomas branch_head_commit_id);
9324 82d979c5 2021-11-04 thomas if (error)
9325 82d979c5 2021-11-04 thomas goto done;
9326 82d979c5 2021-11-04 thomas TAILQ_INIT(&paths);
9327 82d979c5 2021-11-04 thomas error = got_pathlist_append(&paths, "", NULL);
9328 82d979c5 2021-11-04 thomas if (error)
9329 82d979c5 2021-11-04 thomas goto done;
9330 82d979c5 2021-11-04 thomas error = got_worktree_checkout_files(worktree,
9331 82d979c5 2021-11-04 thomas &paths, repo, update_progress, &upa,
9332 82d979c5 2021-11-04 thomas check_cancelled, NULL);
9333 82d979c5 2021-11-04 thomas got_pathlist_free(&paths);
9334 82d979c5 2021-11-04 thomas if (error)
9335 82d979c5 2021-11-04 thomas goto done;
9336 82d979c5 2021-11-04 thomas if (upa.did_something) {
9337 82d979c5 2021-11-04 thomas char *id_str;
9338 82d979c5 2021-11-04 thomas error = got_object_id_str(&id_str,
9339 82d979c5 2021-11-04 thomas branch_head_commit_id);
9340 82d979c5 2021-11-04 thomas if (error)
9341 82d979c5 2021-11-04 thomas goto done;
9342 82d979c5 2021-11-04 thomas printf("Updated to %s: %s\n",
9343 82d979c5 2021-11-04 thomas got_worktree_get_head_ref_name(worktree),
9344 82d979c5 2021-11-04 thomas id_str);
9345 82d979c5 2021-11-04 thomas free(id_str);
9347 82d979c5 2021-11-04 thomas printf("Already up-to-date\n");
9348 82d979c5 2021-11-04 thomas print_update_progress_stats(&upa);
9349 a51a74b3 2019-07-27 stsp goto done;
9351 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
9352 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
9353 818c7501 2019-07-11 stsp if (error)
9354 818c7501 2019-07-11 stsp goto done;
9357 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
9358 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
9359 818c7501 2019-07-11 stsp if (error)
9360 818c7501 2019-07-11 stsp goto done;
9362 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9363 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
9364 fc66b545 2019-08-12 stsp if (pid == NULL) {
9365 fc66b545 2019-08-12 stsp if (!continue_rebase) {
9366 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
9367 d52bac28 2021-10-08 thomas repo, new_base_branch, abort_progress, &upa);
9368 fc66b545 2019-08-12 stsp if (error)
9369 fc66b545 2019-08-12 stsp goto done;
9370 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
9371 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
9372 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
9375 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
9376 fc66b545 2019-08-12 stsp goto done;
9378 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
9379 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
9380 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
9381 818c7501 2019-07-11 stsp got_object_commit_close(commit);
9382 818c7501 2019-07-11 stsp commit = NULL;
9383 818c7501 2019-07-11 stsp if (error)
9384 818c7501 2019-07-11 stsp goto done;
9386 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
9387 38b0338b 2019-11-29 stsp if (continue_rebase) {
9388 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
9389 e600f124 2021-03-21 stsp branch, new_base_branch, tmp_branch, repo,
9390 e600f124 2021-03-21 stsp create_backup);
9391 38b0338b 2019-11-29 stsp goto done;
9393 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
9394 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
9395 38b0338b 2019-11-29 stsp char *id_str;
9396 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
9397 38b0338b 2019-11-29 stsp new_base_branch);
9398 38b0338b 2019-11-29 stsp if (error)
9399 38b0338b 2019-11-29 stsp goto done;
9400 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
9401 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
9402 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
9403 38b0338b 2019-11-29 stsp free(id_str);
9404 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
9405 38b0338b 2019-11-29 stsp new_head_commit_id);
9406 38b0338b 2019-11-29 stsp if (error)
9407 38b0338b 2019-11-29 stsp goto done;
9408 e600f124 2021-03-21 stsp /* No backup needed since objects did not change. */
9409 e600f124 2021-03-21 stsp create_backup = 0;
9413 818c7501 2019-07-11 stsp pid = NULL;
9414 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &commits, entry) {
9416 818c7501 2019-07-11 stsp commit_id = qid->id;
9417 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
9418 818c7501 2019-07-11 stsp pid = qid;
9420 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9421 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
9422 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
9423 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
9424 818c7501 2019-07-11 stsp if (error)
9425 818c7501 2019-07-11 stsp goto done;
9427 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
9428 bb494413 2021-09-28 thomas if (upa.conflicts > 0 || upa.missing > 0 ||
9429 bb494413 2021-09-28 thomas upa.not_deleted > 0 || upa.unversioned > 0) {
9430 bb494413 2021-09-28 thomas if (upa.conflicts > 0) {
9431 bb494413 2021-09-28 thomas error = show_rebase_merge_conflict(qid->id,
9433 bb494413 2021-09-28 thomas if (error)
9434 bb494413 2021-09-28 thomas goto done;
9436 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9440 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
9441 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
9442 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9443 818c7501 2019-07-11 stsp if (error)
9444 818c7501 2019-07-11 stsp goto done;
9447 bb494413 2021-09-28 thomas if (upa.conflicts > 0 || upa.missing > 0 ||
9448 bb494413 2021-09-28 thomas upa.not_deleted > 0 || upa.unversioned > 0) {
9449 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
9450 818c7501 2019-07-11 stsp if (error)
9451 818c7501 2019-07-11 stsp goto done;
9452 bb494413 2021-09-28 thomas if (upa.conflicts > 0 && upa.missing == 0 &&
9453 bb494413 2021-09-28 thomas upa.not_deleted == 0 && upa.unversioned == 0) {
9454 bb494413 2021-09-28 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
9455 bb494413 2021-09-28 thomas "conflicts must be resolved before rebasing "
9456 bb494413 2021-09-28 thomas "can continue");
9457 bb494413 2021-09-28 thomas } else if (upa.conflicts > 0) {
9458 bb494413 2021-09-28 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
9459 bb494413 2021-09-28 thomas "conflicts must be resolved before rebasing "
9460 bb494413 2021-09-28 thomas "can continue; changes destined for some "
9461 bb494413 2021-09-28 thomas "files were not yet merged and should be "
9462 bb494413 2021-09-28 thomas "merged manually if required before the "
9463 bb494413 2021-09-28 thomas "rebase operation is continued");
9464 bb494413 2021-09-28 thomas } else {
9465 bb494413 2021-09-28 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
9466 bb494413 2021-09-28 thomas "changes destined for some files were not "
9467 bb494413 2021-09-28 thomas "yet merged and should be merged manually "
9468 bb494413 2021-09-28 thomas "if required before the rebase operation "
9469 bb494413 2021-09-28 thomas "is continued");
9472 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
9473 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, repo, create_backup);
9475 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
9476 818c7501 2019-07-11 stsp free(branch_head_commit_id);
9477 818c7501 2019-07-11 stsp free(resume_commit_id);
9478 818c7501 2019-07-11 stsp free(yca_id);
9479 818c7501 2019-07-11 stsp if (commit)
9480 818c7501 2019-07-11 stsp got_object_commit_close(commit);
9481 818c7501 2019-07-11 stsp if (branch)
9482 818c7501 2019-07-11 stsp got_ref_close(branch);
9483 818c7501 2019-07-11 stsp if (new_base_branch)
9484 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
9485 818c7501 2019-07-11 stsp if (tmp_branch)
9486 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
9487 5ef14e63 2019-06-02 stsp if (worktree)
9488 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
9489 1d0f4054 2021-06-17 stsp if (repo) {
9490 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
9491 1d0f4054 2021-06-17 stsp if (error == NULL)
9492 1d0f4054 2021-06-17 stsp error = close_err;
9494 5ef14e63 2019-06-02 stsp return error;
9497 0ebf8283 2019-07-24 stsp __dead static void
9498 0ebf8283 2019-07-24 stsp usage_histedit(void)
9500 c50a7455 2021-10-04 thomas fprintf(stderr, "usage: %s histedit [-a] [-c] [-e] [-f] "
9501 643b85bc 2021-07-16 stsp "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
9502 643b85bc 2021-07-16 stsp getprogname());
9506 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
9507 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
9508 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
9509 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
9510 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
9512 641a8ee6 2022-02-16 thomas static const struct got_histedit_cmd {
9513 0ebf8283 2019-07-24 stsp unsigned char code;
9514 0ebf8283 2019-07-24 stsp const char *name;
9515 0ebf8283 2019-07-24 stsp const char *desc;
9516 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
9517 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
9518 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
9519 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
9520 82997472 2020-01-29 stsp "be used" },
9521 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
9522 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
9523 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
9526 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
9527 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
9528 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
9529 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
9530 0ebf8283 2019-07-24 stsp char *logmsg;
9532 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
9534 0ebf8283 2019-07-24 stsp static const struct got_error *
9535 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
9536 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
9538 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9539 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
9540 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9543 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
9545 0ebf8283 2019-07-24 stsp goto done;
9547 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
9549 0ebf8283 2019-07-24 stsp goto done;
9551 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
9553 0ebf8283 2019-07-24 stsp goto done;
9555 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
9556 0ebf8283 2019-07-24 stsp if (n < 0)
9557 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9559 0ebf8283 2019-07-24 stsp if (commit)
9560 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9561 0ebf8283 2019-07-24 stsp free(id_str);
9562 0ebf8283 2019-07-24 stsp free(logmsg);
9563 0ebf8283 2019-07-24 stsp return err;
9566 0ebf8283 2019-07-24 stsp static const struct got_error *
9567 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
9568 c50a7455 2021-10-04 thomas FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
9569 c50a7455 2021-10-04 thomas struct got_repository *repo)
9571 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9572 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
9573 466785b9 2020-12-10 jrick const char *histedit_cmd = NULL;
9575 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
9576 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
9578 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
9579 466785b9 2020-12-10 jrick histedit_cmd = got_histedit_cmds[0].name;
9580 c50a7455 2021-10-04 thomas if (edit_only)
9581 c50a7455 2021-10-04 thomas histedit_cmd = "edit";
9582 c50a7455 2021-10-04 thomas else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
9583 466785b9 2020-12-10 jrick histedit_cmd = "fold";
9584 466785b9 2020-12-10 jrick err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
9587 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
9588 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
9589 083957f4 2020-02-24 stsp if (n < 0) {
9590 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
9596 0ebf8283 2019-07-24 stsp return err;
9599 0ebf8283 2019-07-24 stsp static const struct got_error *
9600 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
9601 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
9603 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9606 514f2ffe 2020-01-29 stsp char *id_str;
9607 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
9609 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(commits);
9610 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
9612 514f2ffe 2020-01-29 stsp return err;
9614 514f2ffe 2020-01-29 stsp n = fprintf(f,
9615 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
9616 514f2ffe 2020-01-29 stsp "# commit %s\n"
9617 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
9618 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
9619 514f2ffe 2020-01-29 stsp if (n < 0) {
9620 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
9621 514f2ffe 2020-01-29 stsp goto done;
9624 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
9625 514f2ffe 2020-01-29 stsp if (n < 0) {
9626 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
9627 514f2ffe 2020-01-29 stsp goto done;
9630 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
9631 641a8ee6 2022-02-16 thomas const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
9632 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
9633 0ebf8283 2019-07-24 stsp cmd->desc);
9634 0ebf8283 2019-07-24 stsp if (n < 0) {
9635 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9640 514f2ffe 2020-01-29 stsp free(id_str);
9641 0ebf8283 2019-07-24 stsp return err;
9644 0ebf8283 2019-07-24 stsp static const struct got_error *
9645 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
9647 0ebf8283 2019-07-24 stsp static char msg[42];
9650 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
9652 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
9653 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
9655 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
9658 0ebf8283 2019-07-24 stsp static const struct got_error *
9659 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
9660 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
9662 0ebf8283 2019-07-24 stsp const struct got_error *err;
9663 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
9664 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
9666 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9668 0ebf8283 2019-07-24 stsp return err;
9670 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
9672 0ebf8283 2019-07-24 stsp goto done;
9674 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
9676 5943eee2 2019-08-13 stsp goto done;
9677 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
9678 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
9679 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
9680 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
9683 0ebf8283 2019-07-24 stsp if (folded_commit)
9684 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
9685 0ebf8283 2019-07-24 stsp free(id_str);
9686 5943eee2 2019-08-13 stsp free(folded_logmsg);
9687 0ebf8283 2019-07-24 stsp return err;
9690 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
9691 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
9693 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
9695 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
9696 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
9697 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
9698 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
9699 3f9de99f 2019-07-24 stsp folded = prev;
9700 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
9703 0ebf8283 2019-07-24 stsp return folded;
9706 0ebf8283 2019-07-24 stsp static const struct got_error *
9707 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
9708 0ebf8283 2019-07-24 stsp struct got_repository *repo)
9710 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
9711 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
9712 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9713 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9714 1601cb9f 2020-09-11 naddy int logmsg_len;
9716 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
9718 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9720 0ebf8283 2019-07-24 stsp return err;
9722 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
9723 0ebf8283 2019-07-24 stsp if (folded) {
9724 0ebf8283 2019-07-24 stsp while (folded != hle) {
9725 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
9726 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
9729 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
9730 0ebf8283 2019-07-24 stsp logmsg, repo);
9732 0ebf8283 2019-07-24 stsp goto done;
9733 0ebf8283 2019-07-24 stsp free(logmsg);
9734 0ebf8283 2019-07-24 stsp logmsg = new_msg;
9735 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
9739 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9741 0ebf8283 2019-07-24 stsp goto done;
9742 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
9744 5943eee2 2019-08-13 stsp goto done;
9745 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
9746 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
9747 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
9748 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
9749 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
9750 0ebf8283 2019-07-24 stsp goto done;
9752 0ebf8283 2019-07-24 stsp free(logmsg);
9753 0ebf8283 2019-07-24 stsp logmsg = new_msg;
9755 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9757 0ebf8283 2019-07-24 stsp goto done;
9759 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
9760 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
9762 0ebf8283 2019-07-24 stsp goto done;
9764 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
9765 0ebf8283 2019-07-24 stsp close(fd);
9767 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
9769 0ebf8283 2019-07-24 stsp goto done;
9771 bfa12d5e 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
9772 0d5bb276 2020-12-15 stsp logmsg_len, 0);
9773 0ebf8283 2019-07-24 stsp if (err) {
9774 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
9775 0ebf8283 2019-07-24 stsp goto done;
9776 71392a05 2020-12-13 stsp err = NULL;
9777 71392a05 2020-12-13 stsp hle->logmsg = strdup(new_msg);
9778 71392a05 2020-12-13 stsp if (hle->logmsg == NULL)
9779 71392a05 2020-12-13 stsp err = got_error_from_errno("strdup");
9782 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
9783 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
9784 0ebf8283 2019-07-24 stsp free(logmsg_path);
9785 0ebf8283 2019-07-24 stsp free(logmsg);
9786 5943eee2 2019-08-13 stsp free(orig_logmsg);
9787 0ebf8283 2019-07-24 stsp free(editor);
9788 0ebf8283 2019-07-24 stsp if (commit)
9789 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9790 0ebf8283 2019-07-24 stsp return err;
9793 0ebf8283 2019-07-24 stsp static const struct got_error *
9794 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
9795 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
9797 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9798 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
9799 6059809a 2020-12-17 stsp size_t i, size;
9800 0ebf8283 2019-07-24 stsp ssize_t len;
9801 6059809a 2020-12-17 stsp int lineno = 0;
9802 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
9803 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
9804 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
9806 0ebf8283 2019-07-24 stsp for (;;) {
9807 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
9808 0ebf8283 2019-07-24 stsp if (len == -1) {
9809 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
9810 0ebf8283 2019-07-24 stsp if (feof(f))
9812 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
9813 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
9818 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
9820 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
9821 0ebf8283 2019-07-24 stsp free(line);
9822 0ebf8283 2019-07-24 stsp line = NULL;
9825 0ebf8283 2019-07-24 stsp cmd = NULL;
9826 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
9827 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
9828 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
9829 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
9830 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
9833 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
9838 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
9839 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
9842 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
9844 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
9845 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
9846 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
9849 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
9850 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
9854 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
9855 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
9856 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
9860 0ebf8283 2019-07-24 stsp free(line);
9861 0ebf8283 2019-07-24 stsp line = NULL;
9865 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
9867 0ebf8283 2019-07-24 stsp *end = '\0';
9869 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
9870 0ebf8283 2019-07-24 stsp if (err) {
9871 0ebf8283 2019-07-24 stsp /* override error code */
9872 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
9876 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
9877 0ebf8283 2019-07-24 stsp if (hle == NULL) {
9878 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
9881 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
9882 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
9883 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
9884 0ebf8283 2019-07-24 stsp commit_id = NULL;
9885 0ebf8283 2019-07-24 stsp free(line);
9886 0ebf8283 2019-07-24 stsp line = NULL;
9887 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
9890 0ebf8283 2019-07-24 stsp free(line);
9891 0ebf8283 2019-07-24 stsp free(commit_id);
9892 0ebf8283 2019-07-24 stsp return err;
9895 0ebf8283 2019-07-24 stsp static const struct got_error *
9896 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
9897 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
9899 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
9900 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
9901 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
9902 5b87815e 2020-03-05 stsp static char msg[92];
9903 bfce7f83 2019-07-27 stsp char *id_str;
9905 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
9906 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
9907 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
9908 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
9909 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
9911 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9912 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
9913 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
9914 5b87815e 2020-03-05 stsp if (hle == hle2)
9916 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
9917 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
9919 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
9921 5b87815e 2020-03-05 stsp return err;
9922 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
9923 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
9924 5b87815e 2020-03-05 stsp free(id_str);
9925 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9929 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
9930 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9931 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
9934 bfce7f83 2019-07-27 stsp if (hle == NULL) {
9935 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
9937 bfce7f83 2019-07-27 stsp return err;
9938 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
9939 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
9940 bfce7f83 2019-07-27 stsp free(id_str);
9941 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9945 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
9946 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
9947 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
9948 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
9950 bfce7f83 2019-07-27 stsp return NULL;
9953 bfce7f83 2019-07-27 stsp static const struct got_error *
9954 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
9955 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
9956 bfce7f83 2019-07-27 stsp struct got_repository *repo)
9958 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9959 0ebf8283 2019-07-24 stsp char *editor;
9960 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9962 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
9964 0ebf8283 2019-07-24 stsp return err;
9966 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
9967 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
9968 0ebf8283 2019-07-24 stsp goto done;
9971 c56c5d8a 2021-12-31 thomas f = fopen(path, "re");
9972 0ebf8283 2019-07-24 stsp if (f == NULL) {
9973 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
9974 0ebf8283 2019-07-24 stsp goto done;
9976 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9978 bfce7f83 2019-07-27 stsp goto done;
9980 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
9982 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9983 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9984 0ebf8283 2019-07-24 stsp free(editor);
9985 0ebf8283 2019-07-24 stsp return err;
9988 0ebf8283 2019-07-24 stsp static const struct got_error *
9989 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
9990 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
9991 514f2ffe 2020-01-29 stsp struct got_repository *);
9993 0ebf8283 2019-07-24 stsp static const struct got_error *
9994 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
9995 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
9996 c50a7455 2021-10-04 thomas int edit_logmsg_only, int fold_only, int edit_only,
9997 c50a7455 2021-10-04 thomas struct got_repository *repo)
9999 0ebf8283 2019-07-24 stsp const struct got_error *err;
10000 0ebf8283 2019-07-24 stsp FILE *f = NULL;
10001 0ebf8283 2019-07-24 stsp char *path = NULL;
10003 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
10005 0ebf8283 2019-07-24 stsp return err;
10007 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
10009 0ebf8283 2019-07-24 stsp goto done;
10011 466785b9 2020-12-10 jrick err = histedit_write_commit_list(commits, f, edit_logmsg_only,
10012 c50a7455 2021-10-04 thomas fold_only, edit_only, repo);
10014 0ebf8283 2019-07-24 stsp goto done;
10016 c50a7455 2021-10-04 thomas if (edit_logmsg_only || fold_only || edit_only) {
10017 083957f4 2020-02-24 stsp rewind(f);
10018 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
10020 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF) {
10021 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
10022 0ebf8283 2019-07-24 stsp goto done;
10024 083957f4 2020-02-24 stsp f = NULL;
10025 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
10026 083957f4 2020-02-24 stsp if (err) {
10027 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10028 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
10029 083957f4 2020-02-24 stsp goto done;
10030 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
10031 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
10035 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
10036 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
10037 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
10038 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
10039 0ebf8283 2019-07-24 stsp free(path);
10040 0ebf8283 2019-07-24 stsp return err;
10043 0ebf8283 2019-07-24 stsp static const struct got_error *
10044 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
10045 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
10047 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
10048 0ebf8283 2019-07-24 stsp char *path = NULL;
10049 0ebf8283 2019-07-24 stsp FILE *f = NULL;
10050 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
10051 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
10053 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
10055 0ebf8283 2019-07-24 stsp return err;
10057 c56c5d8a 2021-12-31 thomas f = fopen(path, "we");
10058 0ebf8283 2019-07-24 stsp if (f == NULL) {
10059 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
10060 0ebf8283 2019-07-24 stsp goto done;
10062 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
10063 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
10068 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
10069 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
10070 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
10071 0ebf8283 2019-07-24 stsp if (n < 0) {
10072 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
10078 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
10079 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
10080 0ebf8283 2019-07-24 stsp free(path);
10081 0ebf8283 2019-07-24 stsp if (commit)
10082 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10083 0ebf8283 2019-07-24 stsp return err;
10087 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
10089 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
10091 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
10092 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
10093 bfce7f83 2019-07-27 stsp free(hle);
10097 0ebf8283 2019-07-24 stsp static const struct got_error *
10098 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
10099 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
10101 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
10102 0ebf8283 2019-07-24 stsp FILE *f = NULL;
10104 c56c5d8a 2021-12-31 thomas f = fopen(path, "re");
10105 0ebf8283 2019-07-24 stsp if (f == NULL) {
10106 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
10107 0ebf8283 2019-07-24 stsp goto done;
10110 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
10112 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
10113 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
10114 0ebf8283 2019-07-24 stsp return err;
10117 0ebf8283 2019-07-24 stsp static const struct got_error *
10118 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
10119 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
10120 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
10122 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
10123 0ebf8283 2019-07-24 stsp int resp = ' ';
10125 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
10126 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
10127 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
10128 0ebf8283 2019-07-24 stsp resp = getchar();
10129 a61a4414 2019-08-07 stsp if (resp == '\n')
10130 a61a4414 2019-08-07 stsp resp = getchar();
10131 426ebf2e 2019-07-25 stsp if (resp == 'c') {
10132 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
10133 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
10135 426ebf2e 2019-07-25 stsp if (err) {
10136 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10137 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
10139 bfce7f83 2019-07-27 stsp prev_err = err;
10140 426ebf2e 2019-07-25 stsp resp = ' ';
10141 426ebf2e 2019-07-25 stsp continue;
10144 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
10145 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
10146 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
10147 c50a7455 2021-10-04 thomas commits, branch_name, 0, 0, 0, repo);
10148 426ebf2e 2019-07-25 stsp if (err) {
10149 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10150 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
10152 bfce7f83 2019-07-27 stsp prev_err = err;
10153 426ebf2e 2019-07-25 stsp resp = ' ';
10154 426ebf2e 2019-07-25 stsp continue;
10157 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
10158 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
10161 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
10164 0ebf8283 2019-07-24 stsp return err;
10167 0ebf8283 2019-07-24 stsp static const struct got_error *
10168 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
10169 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
10170 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
10172 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
10173 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10174 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
10175 3e3a69f1 2019-07-25 stsp branch, repo);
10178 0ebf8283 2019-07-24 stsp static const struct got_error *
10179 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
10180 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
10182 0ebf8283 2019-07-24 stsp const struct got_error *err;
10183 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10185 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
10187 0ebf8283 2019-07-24 stsp goto done;
10189 0ebf8283 2019-07-24 stsp if (new_id) {
10190 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
10192 0ebf8283 2019-07-24 stsp goto done;
10195 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
10196 0ebf8283 2019-07-24 stsp if (new_id_str)
10197 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
10199 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
10200 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
10201 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
10202 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
10203 0ebf8283 2019-07-24 stsp goto done;
10205 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
10207 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
10209 0ebf8283 2019-07-24 stsp goto done;
10212 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
10213 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
10214 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
10215 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
10216 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
10218 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
10219 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
10220 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
10227 0ebf8283 2019-07-24 stsp free(old_id_str);
10228 0ebf8283 2019-07-24 stsp free(new_id_str);
10229 0ebf8283 2019-07-24 stsp return err;
10232 0ebf8283 2019-07-24 stsp static const struct got_error *
10233 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
10234 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
10235 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
10236 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
10238 0ebf8283 2019-07-24 stsp const struct got_error *err;
10239 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
10240 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
10242 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
10243 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
10244 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
10246 0ebf8283 2019-07-24 stsp return err;
10249 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10251 0ebf8283 2019-07-24 stsp return err;
10253 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
10254 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
10255 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
10256 0ebf8283 2019-07-24 stsp if (err) {
10257 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
10258 0ebf8283 2019-07-24 stsp goto done;
10259 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
10261 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
10262 0ebf8283 2019-07-24 stsp free(new_commit_id);
10265 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10266 0ebf8283 2019-07-24 stsp return err;
10269 0ebf8283 2019-07-24 stsp static const struct got_error *
10270 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
10271 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
10273 0ebf8283 2019-07-24 stsp const struct got_error *error;
10274 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
10276 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
10278 0ebf8283 2019-07-24 stsp if (error)
10279 0ebf8283 2019-07-24 stsp return error;
10281 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
10282 0ebf8283 2019-07-24 stsp if (error)
10283 0ebf8283 2019-07-24 stsp return error;
10285 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
10286 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10287 0ebf8283 2019-07-24 stsp return error;
10290 ab20a43a 2020-01-29 stsp static const struct got_error *
10291 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
10292 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
10293 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
10294 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
10296 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
10298 ab20a43a 2020-01-29 stsp switch (status) {
10299 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
10300 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
10301 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
10302 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
10303 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
10304 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
10309 ab20a43a 2020-01-29 stsp switch (staged_status) {
10310 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
10311 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
10312 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
10313 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
10314 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
10319 ab20a43a 2020-01-29 stsp return NULL;
10322 0ebf8283 2019-07-24 stsp static const struct got_error *
10323 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
10325 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
10326 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
10327 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
10328 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
10329 0ebf8283 2019-07-24 stsp char *cwd = NULL;
10330 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
10331 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
10332 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
10333 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
10334 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
10335 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
10336 10604dce 2021-09-24 thomas int ch, rebase_in_progress = 0, merge_in_progress = 0;
10337 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10338 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
10339 c50a7455 2021-10-04 thomas int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
10340 643b85bc 2021-07-16 stsp int list_backups = 0, delete_backups = 0;
10341 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
10342 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
10343 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
10344 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
10345 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
10346 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
10347 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
10349 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
10350 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
10351 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
10352 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10354 c50a7455 2021-10-04 thomas while ((ch = getopt(argc, argv, "acefF:mlX")) != -1) {
10355 0ebf8283 2019-07-24 stsp switch (ch) {
10356 0ebf8283 2019-07-24 stsp case 'a':
10357 0ebf8283 2019-07-24 stsp abort_edit = 1;
10359 0ebf8283 2019-07-24 stsp case 'c':
10360 0ebf8283 2019-07-24 stsp continue_edit = 1;
10362 c50a7455 2021-10-04 thomas case 'e':
10363 c50a7455 2021-10-04 thomas edit_only = 1;
10365 466785b9 2020-12-10 jrick case 'f':
10366 466785b9 2020-12-10 jrick fold_only = 1;
10368 0ebf8283 2019-07-24 stsp case 'F':
10369 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
10371 083957f4 2020-02-24 stsp case 'm':
10372 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
10374 e600f124 2021-03-21 stsp case 'l':
10375 e600f124 2021-03-21 stsp list_backups = 1;
10377 643b85bc 2021-07-16 stsp case 'X':
10378 643b85bc 2021-07-16 stsp delete_backups = 1;
10381 0ebf8283 2019-07-24 stsp usage_histedit();
10382 0ebf8283 2019-07-24 stsp /* NOTREACHED */
10386 0ebf8283 2019-07-24 stsp argc -= optind;
10387 0ebf8283 2019-07-24 stsp argv += optind;
10389 0ebf8283 2019-07-24 stsp #ifndef PROFILE
10390 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10391 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
10392 0ebf8283 2019-07-24 stsp err(1, "pledge");
10394 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
10395 ff69268e 2020-12-13 stsp option_conflict('a', 'c');
10396 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
10397 ff69268e 2020-12-13 stsp option_conflict('F', 'm');
10398 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
10399 ff69268e 2020-12-13 stsp option_conflict('a', 'm');
10400 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
10401 ff69268e 2020-12-13 stsp option_conflict('c', 'm');
10402 466785b9 2020-12-10 jrick if (abort_edit && fold_only)
10403 ff69268e 2020-12-13 stsp option_conflict('a', 'f');
10404 ff69268e 2020-12-13 stsp if (continue_edit && fold_only)
10405 ff69268e 2020-12-13 stsp option_conflict('c', 'f');
10406 466785b9 2020-12-10 jrick if (fold_only && edit_logmsg_only)
10407 ff69268e 2020-12-13 stsp option_conflict('f', 'm');
10408 b3805337 2020-12-13 stsp if (edit_script_path && fold_only)
10409 b3805337 2020-12-13 stsp option_conflict('F', 'f');
10410 c50a7455 2021-10-04 thomas if (abort_edit && edit_only)
10411 c50a7455 2021-10-04 thomas option_conflict('a', 'e');
10412 c50a7455 2021-10-04 thomas if (continue_edit && edit_only)
10413 c50a7455 2021-10-04 thomas option_conflict('c', 'e');
10414 c50a7455 2021-10-04 thomas if (edit_only && edit_logmsg_only)
10415 c50a7455 2021-10-04 thomas option_conflict('e', 'm');
10416 c50a7455 2021-10-04 thomas if (edit_script_path && edit_only)
10417 c50a7455 2021-10-04 thomas option_conflict('F', 'e');
10418 e600f124 2021-03-21 stsp if (list_backups) {
10419 e600f124 2021-03-21 stsp if (abort_edit)
10420 e600f124 2021-03-21 stsp option_conflict('l', 'a');
10421 e600f124 2021-03-21 stsp if (continue_edit)
10422 e600f124 2021-03-21 stsp option_conflict('l', 'c');
10423 e600f124 2021-03-21 stsp if (edit_script_path)
10424 e600f124 2021-03-21 stsp option_conflict('l', 'F');
10425 e600f124 2021-03-21 stsp if (edit_logmsg_only)
10426 e600f124 2021-03-21 stsp option_conflict('l', 'm');
10427 e600f124 2021-03-21 stsp if (fold_only)
10428 e600f124 2021-03-21 stsp option_conflict('l', 'f');
10429 c50a7455 2021-10-04 thomas if (edit_only)
10430 c50a7455 2021-10-04 thomas option_conflict('l', 'e');
10431 643b85bc 2021-07-16 stsp if (delete_backups)
10432 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
10433 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
10434 e600f124 2021-03-21 stsp usage_histedit();
10435 643b85bc 2021-07-16 stsp } else if (delete_backups) {
10436 643b85bc 2021-07-16 stsp if (abort_edit)
10437 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
10438 643b85bc 2021-07-16 stsp if (continue_edit)
10439 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
10440 643b85bc 2021-07-16 stsp if (edit_script_path)
10441 643b85bc 2021-07-16 stsp option_conflict('X', 'F');
10442 643b85bc 2021-07-16 stsp if (edit_logmsg_only)
10443 643b85bc 2021-07-16 stsp option_conflict('X', 'm');
10444 643b85bc 2021-07-16 stsp if (fold_only)
10445 643b85bc 2021-07-16 stsp option_conflict('X', 'f');
10446 c50a7455 2021-10-04 thomas if (edit_only)
10447 c50a7455 2021-10-04 thomas option_conflict('X', 'e');
10448 643b85bc 2021-07-16 stsp if (list_backups)
10449 643b85bc 2021-07-16 stsp option_conflict('X', 'l');
10450 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
10451 643b85bc 2021-07-16 stsp usage_histedit();
10452 e600f124 2021-03-21 stsp } else if (argc != 0)
10453 0ebf8283 2019-07-24 stsp usage_histedit();
10456 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
10457 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
10458 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
10459 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
10460 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
10461 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
10464 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
10465 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
10466 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
10467 0ebf8283 2019-07-24 stsp goto done;
10469 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
10470 fa51e947 2020-03-27 stsp if (error) {
10471 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
10472 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
10473 e600f124 2021-03-21 stsp goto done;
10475 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10476 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
10477 e600f124 2021-03-21 stsp "histedit", cwd);
10478 e600f124 2021-03-21 stsp goto done;
10482 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
10483 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
10484 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd,
10486 e600f124 2021-03-21 stsp if (error != NULL)
10487 e600f124 2021-03-21 stsp goto done;
10488 e600f124 2021-03-21 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10489 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
10490 e600f124 2021-03-21 stsp if (error)
10491 e600f124 2021-03-21 stsp goto done;
10492 643b85bc 2021-07-16 stsp error = process_backup_refs(
10493 e600f124 2021-03-21 stsp GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
10494 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
10495 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
10498 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10500 0ebf8283 2019-07-24 stsp if (error != NULL)
10501 0ebf8283 2019-07-24 stsp goto done;
10503 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10504 0ebf8283 2019-07-24 stsp if (error)
10505 0ebf8283 2019-07-24 stsp goto done;
10506 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
10507 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
10508 10604dce 2021-09-24 thomas goto done;
10511 10604dce 2021-09-24 thomas error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10513 10604dce 2021-09-24 thomas if (error)
10514 10604dce 2021-09-24 thomas goto done;
10515 10604dce 2021-09-24 thomas if (merge_in_progress) {
10516 10604dce 2021-09-24 thomas error = got_error(GOT_ERR_MERGE_BUSY);
10517 0ebf8283 2019-07-24 stsp goto done;
10520 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
10521 0ebf8283 2019-07-24 stsp if (error)
10522 0ebf8283 2019-07-24 stsp goto done;
10524 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
10525 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10526 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
10527 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
10528 083957f4 2020-02-24 stsp "before the -m option can be used");
10529 083957f4 2020-02-24 stsp goto done;
10531 466785b9 2020-12-10 jrick if (edit_in_progress && fold_only) {
10532 466785b9 2020-12-10 jrick error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10533 466785b9 2020-12-10 jrick "histedit operation is in progress in this "
10534 466785b9 2020-12-10 jrick "work tree and must be continued or aborted "
10535 466785b9 2020-12-10 jrick "before the -f option can be used");
10536 466785b9 2020-12-10 jrick goto done;
10538 c50a7455 2021-10-04 thomas if (edit_in_progress && edit_only) {
10539 c50a7455 2021-10-04 thomas error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10540 c50a7455 2021-10-04 thomas "histedit operation is in progress in this "
10541 c50a7455 2021-10-04 thomas "work tree and must be continued or aborted "
10542 c50a7455 2021-10-04 thomas "before the -e option can be used");
10543 c50a7455 2021-10-04 thomas goto done;
10546 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
10547 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
10548 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
10549 3e3a69f1 2019-07-25 stsp worktree, repo);
10550 0ebf8283 2019-07-24 stsp if (error)
10551 0ebf8283 2019-07-24 stsp goto done;
10552 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
10553 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10554 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
10555 d52bac28 2021-10-08 thomas branch, base_commit_id, abort_progress, &upa);
10556 0ebf8283 2019-07-24 stsp if (error)
10557 0ebf8283 2019-07-24 stsp goto done;
10558 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
10559 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10560 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
10561 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
10562 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
10563 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
10564 0ebf8283 2019-07-24 stsp goto done;
10567 0ebf8283 2019-07-24 stsp if (continue_edit) {
10568 0ebf8283 2019-07-24 stsp char *path;
10570 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
10571 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
10572 0ebf8283 2019-07-24 stsp goto done;
10575 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
10576 0ebf8283 2019-07-24 stsp if (error)
10577 0ebf8283 2019-07-24 stsp goto done;
10579 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
10580 0ebf8283 2019-07-24 stsp free(path);
10581 0ebf8283 2019-07-24 stsp if (error)
10582 0ebf8283 2019-07-24 stsp goto done;
10584 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
10585 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
10586 3e3a69f1 2019-07-25 stsp worktree, repo);
10587 0ebf8283 2019-07-24 stsp if (error)
10588 0ebf8283 2019-07-24 stsp goto done;
10590 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
10591 0ebf8283 2019-07-24 stsp if (error)
10592 0ebf8283 2019-07-24 stsp goto done;
10594 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10595 0ebf8283 2019-07-24 stsp head_commit_id);
10596 0ebf8283 2019-07-24 stsp if (error)
10597 0ebf8283 2019-07-24 stsp goto done;
10598 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10599 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10600 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
10601 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10602 3aac7cf7 2019-07-25 stsp goto done;
10604 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
10605 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
10606 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
10607 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10608 0ebf8283 2019-07-24 stsp commit = NULL;
10609 0ebf8283 2019-07-24 stsp if (error)
10610 0ebf8283 2019-07-24 stsp goto done;
10612 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
10613 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
10614 0ebf8283 2019-07-24 stsp goto done;
10617 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
10618 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
10619 0ebf8283 2019-07-24 stsp if (error != NULL)
10620 0ebf8283 2019-07-24 stsp goto done;
10622 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10623 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10624 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
10625 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
10626 c7d20a3f 2019-07-30 stsp goto done;
10629 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
10630 bfce7f83 2019-07-27 stsp got_ref_close(branch);
10631 bfce7f83 2019-07-27 stsp branch = NULL;
10632 0ebf8283 2019-07-24 stsp if (error)
10633 0ebf8283 2019-07-24 stsp goto done;
10635 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10636 0ebf8283 2019-07-24 stsp head_commit_id);
10637 0ebf8283 2019-07-24 stsp if (error)
10638 0ebf8283 2019-07-24 stsp goto done;
10639 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10640 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10641 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
10642 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10643 3aac7cf7 2019-07-25 stsp goto done;
10645 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
10646 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
10647 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
10648 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
10649 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10650 0ebf8283 2019-07-24 stsp commit = NULL;
10651 0ebf8283 2019-07-24 stsp if (error)
10652 0ebf8283 2019-07-24 stsp goto done;
10654 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
10655 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10656 c5996fff 2020-01-29 stsp goto done;
10659 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
10660 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
10661 bfce7f83 2019-07-27 stsp if (error)
10662 bfce7f83 2019-07-27 stsp goto done;
10664 0ebf8283 2019-07-24 stsp if (edit_script_path) {
10665 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
10666 0ebf8283 2019-07-24 stsp edit_script_path, repo);
10667 6ba2bea2 2019-07-27 stsp if (error) {
10668 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10669 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10670 d52bac28 2021-10-08 thomas abort_progress, &upa);
10671 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
10672 0ebf8283 2019-07-24 stsp goto done;
10675 514f2ffe 2020-01-29 stsp const char *branch_name;
10676 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
10677 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
10678 514f2ffe 2020-01-29 stsp branch_name += 11;
10679 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
10680 c50a7455 2021-10-04 thomas branch_name, edit_logmsg_only, fold_only,
10681 c50a7455 2021-10-04 thomas edit_only, repo);
10682 6ba2bea2 2019-07-27 stsp if (error) {
10683 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10684 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10685 d52bac28 2021-10-08 thomas abort_progress, &upa);
10686 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
10687 0ebf8283 2019-07-24 stsp goto done;
10692 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
10694 6ba2bea2 2019-07-27 stsp if (error) {
10695 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10696 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10697 d52bac28 2021-10-08 thomas abort_progress, &upa);
10698 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
10699 0ebf8283 2019-07-24 stsp goto done;
10704 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
10705 4ec14e60 2019-08-28 hiltjo if (error)
10706 0ebf8283 2019-07-24 stsp goto done;
10708 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
10709 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
10710 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
10711 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
10712 0ebf8283 2019-07-24 stsp continue;
10714 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
10715 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
10716 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
10717 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
10719 ab20a43a 2020-01-29 stsp if (error)
10720 ab20a43a 2020-01-29 stsp goto done;
10722 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
10723 ab20a43a 2020-01-29 stsp int have_changes = 0;
10725 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
10726 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
10727 ab20a43a 2020-01-29 stsp if (error)
10728 ab20a43a 2020-01-29 stsp goto done;
10729 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
10730 f6343036 2021-06-22 stsp repo, 0, check_local_changes, &have_changes,
10731 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
10732 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
10733 ab20a43a 2020-01-29 stsp if (error) {
10734 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
10735 ab20a43a 2020-01-29 stsp goto done;
10736 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
10737 ab20a43a 2020-01-29 stsp goto done;
10739 ab20a43a 2020-01-29 stsp if (have_changes) {
10740 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
10741 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
10742 ab20a43a 2020-01-29 stsp if (error)
10743 ab20a43a 2020-01-29 stsp goto done;
10745 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
10746 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
10747 ab20a43a 2020-01-29 stsp if (error)
10748 ab20a43a 2020-01-29 stsp goto done;
10749 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
10750 ab20a43a 2020-01-29 stsp hle, NULL);
10751 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
10752 ab20a43a 2020-01-29 stsp commit = NULL;
10753 ab20a43a 2020-01-29 stsp if (error)
10754 ab20a43a 2020-01-29 stsp goto done;
10757 0ebf8283 2019-07-24 stsp continue;
10760 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
10761 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
10762 0ebf8283 2019-07-24 stsp if (error)
10763 0ebf8283 2019-07-24 stsp goto done;
10764 0ebf8283 2019-07-24 stsp continue;
10767 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10768 0ebf8283 2019-07-24 stsp hle->commit_id);
10769 0ebf8283 2019-07-24 stsp if (error)
10770 0ebf8283 2019-07-24 stsp goto done;
10771 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10772 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10774 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
10775 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
10776 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
10777 0ebf8283 2019-07-24 stsp if (error)
10778 0ebf8283 2019-07-24 stsp goto done;
10779 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10780 0ebf8283 2019-07-24 stsp commit = NULL;
10782 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
10783 dddbe150 2021-09-28 thomas if (upa.conflicts > 0 || upa.missing > 0 ||
10784 dddbe150 2021-09-28 thomas upa.not_deleted > 0 || upa.unversioned > 0) {
10785 dddbe150 2021-09-28 thomas if (upa.conflicts > 0) {
10786 dddbe150 2021-09-28 thomas error = show_rebase_merge_conflict(
10787 dddbe150 2021-09-28 thomas hle->commit_id, repo);
10788 dddbe150 2021-09-28 thomas if (error)
10789 dddbe150 2021-09-28 thomas goto done;
10791 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10795 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
10796 0ebf8283 2019-07-24 stsp char *id_str;
10797 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
10798 0ebf8283 2019-07-24 stsp if (error)
10799 0ebf8283 2019-07-24 stsp goto done;
10800 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
10802 0ebf8283 2019-07-24 stsp free(id_str);
10803 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10804 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
10805 3e3a69f1 2019-07-25 stsp fileindex);
10806 0ebf8283 2019-07-24 stsp goto done;
10809 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
10810 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
10811 0ebf8283 2019-07-24 stsp if (error)
10812 0ebf8283 2019-07-24 stsp goto done;
10813 0ebf8283 2019-07-24 stsp continue;
10816 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
10817 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
10818 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10819 0ebf8283 2019-07-24 stsp if (error)
10820 0ebf8283 2019-07-24 stsp goto done;
10823 dddbe150 2021-09-28 thomas if (upa.conflicts > 0 || upa.missing > 0 ||
10824 dddbe150 2021-09-28 thomas upa.not_deleted > 0 || upa.unversioned > 0) {
10825 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
10826 0ebf8283 2019-07-24 stsp if (error)
10827 0ebf8283 2019-07-24 stsp goto done;
10828 dddbe150 2021-09-28 thomas if (upa.conflicts > 0 && upa.missing == 0 &&
10829 dddbe150 2021-09-28 thomas upa.not_deleted == 0 && upa.unversioned == 0) {
10830 dddbe150 2021-09-28 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
10831 dddbe150 2021-09-28 thomas "conflicts must be resolved before histedit "
10832 dddbe150 2021-09-28 thomas "can continue");
10833 dddbe150 2021-09-28 thomas } else if (upa.conflicts > 0) {
10834 dddbe150 2021-09-28 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
10835 dddbe150 2021-09-28 thomas "conflicts must be resolved before histedit "
10836 dddbe150 2021-09-28 thomas "can continue; changes destined for some "
10837 dddbe150 2021-09-28 thomas "files were not yet merged and should be "
10838 dddbe150 2021-09-28 thomas "merged manually if required before the "
10839 dddbe150 2021-09-28 thomas "histedit operation is continued");
10840 dddbe150 2021-09-28 thomas } else {
10841 dddbe150 2021-09-28 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
10842 dddbe150 2021-09-28 thomas "changes destined for some files were not "
10843 dddbe150 2021-09-28 thomas "yet merged and should be merged manually "
10844 dddbe150 2021-09-28 thomas "if required before the histedit operation "
10845 dddbe150 2021-09-28 thomas "is continued");
10848 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
10849 3e3a69f1 2019-07-25 stsp branch, repo);
10851 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
10852 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
10853 0ebf8283 2019-07-24 stsp free(head_commit_id);
10854 0ebf8283 2019-07-24 stsp free(base_commit_id);
10855 0ebf8283 2019-07-24 stsp free(resume_commit_id);
10856 0ebf8283 2019-07-24 stsp if (commit)
10857 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10858 0ebf8283 2019-07-24 stsp if (branch)
10859 0ebf8283 2019-07-24 stsp got_ref_close(branch);
10860 0ebf8283 2019-07-24 stsp if (tmp_branch)
10861 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
10862 0ebf8283 2019-07-24 stsp if (worktree)
10863 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
10864 1d0f4054 2021-06-17 stsp if (repo) {
10865 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10866 1d0f4054 2021-06-17 stsp if (error == NULL)
10867 1d0f4054 2021-06-17 stsp error = close_err;
10869 2822a352 2019-10-15 stsp return error;
10872 2822a352 2019-10-15 stsp __dead static void
10873 2822a352 2019-10-15 stsp usage_integrate(void)
10875 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
10879 2822a352 2019-10-15 stsp static const struct got_error *
10880 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
10882 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
10883 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
10884 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
10885 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
10886 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
10887 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
10888 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
10889 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
10891 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10893 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
10894 2822a352 2019-10-15 stsp switch (ch) {
10896 2822a352 2019-10-15 stsp usage_integrate();
10897 2822a352 2019-10-15 stsp /* NOTREACHED */
10901 2822a352 2019-10-15 stsp argc -= optind;
10902 2822a352 2019-10-15 stsp argv += optind;
10904 2822a352 2019-10-15 stsp if (argc != 1)
10905 2822a352 2019-10-15 stsp usage_integrate();
10906 2822a352 2019-10-15 stsp branch_arg = argv[0];
10907 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
10908 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10909 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
10910 2822a352 2019-10-15 stsp err(1, "pledge");
10912 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
10913 2822a352 2019-10-15 stsp if (cwd == NULL) {
10914 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
10915 2822a352 2019-10-15 stsp goto done;
10918 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
10919 fa51e947 2020-03-27 stsp if (error) {
10920 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10921 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
10923 2822a352 2019-10-15 stsp goto done;
10926 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
10927 2822a352 2019-10-15 stsp if (error)
10928 2822a352 2019-10-15 stsp goto done;
10930 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10932 2822a352 2019-10-15 stsp if (error != NULL)
10933 2822a352 2019-10-15 stsp goto done;
10935 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10936 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
10937 2822a352 2019-10-15 stsp if (error)
10938 2822a352 2019-10-15 stsp goto done;
10940 10604dce 2021-09-24 thomas error = check_merge_in_progress(worktree, repo);
10941 10604dce 2021-09-24 thomas if (error)
10942 10604dce 2021-09-24 thomas goto done;
10944 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
10945 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
10946 2822a352 2019-10-15 stsp goto done;
10949 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
10950 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
10951 2822a352 2019-10-15 stsp if (error)
10952 2822a352 2019-10-15 stsp goto done;
10954 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
10955 2822a352 2019-10-15 stsp if (refname == NULL) {
10956 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
10957 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10958 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10959 2822a352 2019-10-15 stsp goto done;
10961 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
10962 2822a352 2019-10-15 stsp if (base_refname == NULL) {
10963 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
10964 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10965 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10966 2822a352 2019-10-15 stsp goto done;
10969 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
10970 2822a352 2019-10-15 stsp if (error)
10971 2822a352 2019-10-15 stsp goto done;
10973 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
10974 2822a352 2019-10-15 stsp if (error)
10975 2822a352 2019-10-15 stsp goto done;
10977 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
10978 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
10979 2822a352 2019-10-15 stsp "specified branch has already been integrated");
10980 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10981 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10982 2822a352 2019-10-15 stsp goto done;
10985 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
10986 2822a352 2019-10-15 stsp if (error) {
10987 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
10988 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
10989 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10990 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10991 2822a352 2019-10-15 stsp goto done;
10994 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10995 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
10996 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
10997 2822a352 2019-10-15 stsp check_cancelled, NULL);
10998 2822a352 2019-10-15 stsp if (error)
10999 2822a352 2019-10-15 stsp goto done;
11001 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
11002 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
11004 1d0f4054 2021-06-17 stsp if (repo) {
11005 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
11006 1d0f4054 2021-06-17 stsp if (error == NULL)
11007 1d0f4054 2021-06-17 stsp error = close_err;
11009 2822a352 2019-10-15 stsp if (worktree)
11010 2822a352 2019-10-15 stsp got_worktree_close(worktree);
11011 2822a352 2019-10-15 stsp free(cwd);
11012 2822a352 2019-10-15 stsp free(base_commit_id);
11013 2822a352 2019-10-15 stsp free(commit_id);
11014 2822a352 2019-10-15 stsp free(refname);
11015 2822a352 2019-10-15 stsp free(base_refname);
11016 10604dce 2021-09-24 thomas return error;
11019 10604dce 2021-09-24 thomas __dead static void
11020 10604dce 2021-09-24 thomas usage_merge(void)
11022 ba34626b 2021-09-27 thomas fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
11023 10604dce 2021-09-24 thomas getprogname());
11024 10604dce 2021-09-24 thomas exit(1);
11027 10604dce 2021-09-24 thomas static const struct got_error *
11028 10604dce 2021-09-24 thomas cmd_merge(int argc, char *argv[])
11030 10604dce 2021-09-24 thomas const struct got_error *error = NULL;
11031 10604dce 2021-09-24 thomas struct got_worktree *worktree = NULL;
11032 10604dce 2021-09-24 thomas struct got_repository *repo = NULL;
11033 10604dce 2021-09-24 thomas struct got_fileindex *fileindex = NULL;
11034 10604dce 2021-09-24 thomas char *cwd = NULL, *id_str = NULL, *author = NULL;
11035 10604dce 2021-09-24 thomas struct got_reference *branch = NULL, *wt_branch = NULL;
11036 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *yca_id = NULL;
11037 10604dce 2021-09-24 thomas struct got_object_id *wt_branch_tip = NULL;
11038 10604dce 2021-09-24 thomas int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
11039 ba34626b 2021-09-27 thomas int interrupt_merge = 0;
11040 10604dce 2021-09-24 thomas struct got_update_progress_arg upa;
11041 10604dce 2021-09-24 thomas struct got_object_id *merge_commit_id = NULL;
11042 10604dce 2021-09-24 thomas char *branch_name = NULL;
11044 10604dce 2021-09-24 thomas memset(&upa, 0, sizeof(upa));
11046 ba34626b 2021-09-27 thomas while ((ch = getopt(argc, argv, "acn")) != -1) {
11047 10604dce 2021-09-24 thomas switch (ch) {
11048 10604dce 2021-09-24 thomas case 'a':
11049 10604dce 2021-09-24 thomas abort_merge = 1;
11051 10604dce 2021-09-24 thomas case 'c':
11052 10604dce 2021-09-24 thomas continue_merge = 1;
11054 ba34626b 2021-09-27 thomas case 'n':
11055 ba34626b 2021-09-27 thomas interrupt_merge = 1;
11057 10604dce 2021-09-24 thomas default:
11058 10604dce 2021-09-24 thomas usage_rebase();
11059 10604dce 2021-09-24 thomas /* NOTREACHED */
11063 10604dce 2021-09-24 thomas argc -= optind;
11064 10604dce 2021-09-24 thomas argv += optind;
11066 10604dce 2021-09-24 thomas #ifndef PROFILE
11067 10604dce 2021-09-24 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11068 10604dce 2021-09-24 thomas "unveil", NULL) == -1)
11069 10604dce 2021-09-24 thomas err(1, "pledge");
11072 10604dce 2021-09-24 thomas if (abort_merge && continue_merge)
11073 10604dce 2021-09-24 thomas option_conflict('a', 'c');
11074 10604dce 2021-09-24 thomas if (abort_merge || continue_merge) {
11075 10604dce 2021-09-24 thomas if (argc != 0)
11076 10604dce 2021-09-24 thomas usage_merge();
11077 10604dce 2021-09-24 thomas } else if (argc != 1)
11078 10604dce 2021-09-24 thomas usage_merge();
11080 10604dce 2021-09-24 thomas cwd = getcwd(NULL, 0);
11081 10604dce 2021-09-24 thomas if (cwd == NULL) {
11082 10604dce 2021-09-24 thomas error = got_error_from_errno("getcwd");
11083 10604dce 2021-09-24 thomas goto done;
11086 10604dce 2021-09-24 thomas error = got_worktree_open(&worktree, cwd);
11087 10604dce 2021-09-24 thomas if (error) {
11088 10604dce 2021-09-24 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
11089 10604dce 2021-09-24 thomas error = wrap_not_worktree_error(error,
11090 10604dce 2021-09-24 thomas "merge", cwd);
11091 10604dce 2021-09-24 thomas goto done;
11094 10604dce 2021-09-24 thomas error = got_repo_open(&repo,
11095 10604dce 2021-09-24 thomas worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
11096 10604dce 2021-09-24 thomas if (error != NULL)
11097 10604dce 2021-09-24 thomas goto done;
11099 10604dce 2021-09-24 thomas error = apply_unveil(got_repo_get_path(repo), 0,
11100 10604dce 2021-09-24 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
11101 10604dce 2021-09-24 thomas if (error)
11102 10604dce 2021-09-24 thomas goto done;
11104 10604dce 2021-09-24 thomas error = check_rebase_or_histedit_in_progress(worktree);
11105 10604dce 2021-09-24 thomas if (error)
11106 10604dce 2021-09-24 thomas goto done;
11108 10604dce 2021-09-24 thomas error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11110 10604dce 2021-09-24 thomas if (error)
11111 10604dce 2021-09-24 thomas goto done;
11113 10604dce 2021-09-24 thomas if (abort_merge) {
11114 10604dce 2021-09-24 thomas if (!merge_in_progress) {
11115 10604dce 2021-09-24 thomas error = got_error(GOT_ERR_NOT_MERGING);
11116 10604dce 2021-09-24 thomas goto done;
11118 10604dce 2021-09-24 thomas error = got_worktree_merge_continue(&branch_name,
11119 10604dce 2021-09-24 thomas &branch_tip, &fileindex, worktree, repo);
11120 10604dce 2021-09-24 thomas if (error)
11121 10604dce 2021-09-24 thomas goto done;
11122 10604dce 2021-09-24 thomas error = got_worktree_merge_abort(worktree, fileindex, repo,
11123 d52bac28 2021-10-08 thomas abort_progress, &upa);
11124 10604dce 2021-09-24 thomas if (error)
11125 10604dce 2021-09-24 thomas goto done;
11126 10604dce 2021-09-24 thomas printf("Merge of %s aborted\n", branch_name);
11127 10604dce 2021-09-24 thomas goto done; /* nothing else to do */
11130 10604dce 2021-09-24 thomas error = get_author(&author, repo, worktree);
11131 10604dce 2021-09-24 thomas if (error)
11132 10604dce 2021-09-24 thomas goto done;
11134 10604dce 2021-09-24 thomas if (continue_merge) {
11135 10604dce 2021-09-24 thomas if (!merge_in_progress) {
11136 10604dce 2021-09-24 thomas error = got_error(GOT_ERR_NOT_MERGING);
11137 10604dce 2021-09-24 thomas goto done;
11139 10604dce 2021-09-24 thomas error = got_worktree_merge_continue(&branch_name,
11140 10604dce 2021-09-24 thomas &branch_tip, &fileindex, worktree, repo);
11141 10604dce 2021-09-24 thomas if (error)
11142 10604dce 2021-09-24 thomas goto done;
11143 10604dce 2021-09-24 thomas } else {
11144 10604dce 2021-09-24 thomas error = got_ref_open(&branch, repo, argv[0], 0);
11145 10604dce 2021-09-24 thomas if (error != NULL)
11146 10604dce 2021-09-24 thomas goto done;
11147 10604dce 2021-09-24 thomas branch_name = strdup(got_ref_get_name(branch));
11148 10604dce 2021-09-24 thomas if (branch_name == NULL) {
11149 10604dce 2021-09-24 thomas error = got_error_from_errno("strdup");
11150 10604dce 2021-09-24 thomas goto done;
11152 10604dce 2021-09-24 thomas error = got_ref_resolve(&branch_tip, repo, branch);
11153 10604dce 2021-09-24 thomas if (error)
11154 10604dce 2021-09-24 thomas goto done;
11157 10604dce 2021-09-24 thomas error = got_ref_open(&wt_branch, repo,
11158 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(worktree), 0);
11159 10604dce 2021-09-24 thomas if (error)
11160 10604dce 2021-09-24 thomas goto done;
11161 10604dce 2021-09-24 thomas error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
11162 10604dce 2021-09-24 thomas if (error)
11163 10604dce 2021-09-24 thomas goto done;
11164 10604dce 2021-09-24 thomas error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11165 768705e3 2021-09-27 thomas wt_branch_tip, branch_tip, 0, repo,
11166 10604dce 2021-09-24 thomas check_cancelled, NULL);
11167 768705e3 2021-09-27 thomas if (error && error->code != GOT_ERR_ANCESTRY)
11168 10604dce 2021-09-24 thomas goto done;
11170 10604dce 2021-09-24 thomas if (!continue_merge) {
11171 10604dce 2021-09-24 thomas error = check_path_prefix(wt_branch_tip, branch_tip,
11172 10604dce 2021-09-24 thomas got_worktree_get_path_prefix(worktree),
11173 10604dce 2021-09-24 thomas GOT_ERR_MERGE_PATH, repo);
11174 10604dce 2021-09-24 thomas if (error)
11175 10604dce 2021-09-24 thomas goto done;
11176 768705e3 2021-09-27 thomas if (yca_id) {
11177 768705e3 2021-09-27 thomas error = check_same_branch(wt_branch_tip, branch,
11178 768705e3 2021-09-27 thomas yca_id, repo);
11179 768705e3 2021-09-27 thomas if (error) {
11180 768705e3 2021-09-27 thomas if (error->code != GOT_ERR_ANCESTRY)
11181 768705e3 2021-09-27 thomas goto done;
11182 768705e3 2021-09-27 thomas error = NULL;
11183 768705e3 2021-09-27 thomas } else {
11184 768705e3 2021-09-27 thomas static char msg[512];
11185 768705e3 2021-09-27 thomas snprintf(msg, sizeof(msg),
11186 768705e3 2021-09-27 thomas "cannot create a merge commit because "
11187 768705e3 2021-09-27 thomas "%s is based on %s; %s can be integrated "
11188 768705e3 2021-09-27 thomas "with 'got integrate' instead", branch_name,
11189 768705e3 2021-09-27 thomas got_worktree_get_head_ref_name(worktree),
11190 768705e3 2021-09-27 thomas branch_name);
11191 768705e3 2021-09-27 thomas error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
11192 10604dce 2021-09-24 thomas goto done;
11195 10604dce 2021-09-24 thomas error = got_worktree_merge_prepare(&fileindex, worktree,
11196 10604dce 2021-09-24 thomas branch, repo);
11197 10604dce 2021-09-24 thomas if (error)
11198 10604dce 2021-09-24 thomas goto done;
11200 10604dce 2021-09-24 thomas error = got_worktree_merge_branch(worktree, fileindex,
11201 10604dce 2021-09-24 thomas yca_id, branch_tip, repo, update_progress, &upa,
11202 10604dce 2021-09-24 thomas check_cancelled, NULL);
11203 10604dce 2021-09-24 thomas if (error)
11204 10604dce 2021-09-24 thomas goto done;
11205 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
11206 768705e3 2021-09-27 thomas if (!upa.did_something) {
11207 768705e3 2021-09-27 thomas error = got_worktree_merge_abort(worktree, fileindex,
11208 d52bac28 2021-10-08 thomas repo, abort_progress, &upa);
11209 768705e3 2021-09-27 thomas if (error)
11210 768705e3 2021-09-27 thomas goto done;
11211 768705e3 2021-09-27 thomas printf("Already up-to-date\n");
11212 768705e3 2021-09-27 thomas goto done;
11216 ba34626b 2021-09-27 thomas if (interrupt_merge) {
11217 10604dce 2021-09-24 thomas error = got_worktree_merge_postpone(worktree, fileindex);
11218 10604dce 2021-09-24 thomas if (error)
11219 10604dce 2021-09-24 thomas goto done;
11220 ba34626b 2021-09-27 thomas printf("Merge of %s interrupted on request\n", branch_name);
11221 ba162991 2021-09-28 thomas } else if (upa.conflicts > 0 || upa.missing > 0 ||
11222 ba162991 2021-09-28 thomas upa.not_deleted > 0 || upa.unversioned > 0) {
11223 ba34626b 2021-09-27 thomas error = got_worktree_merge_postpone(worktree, fileindex);
11224 ba34626b 2021-09-27 thomas if (error)
11225 ba34626b 2021-09-27 thomas goto done;
11226 ba162991 2021-09-28 thomas if (upa.conflicts > 0 && upa.missing == 0 &&
11227 ba162991 2021-09-28 thomas upa.not_deleted == 0 && upa.unversioned == 0) {
11228 10604dce 2021-09-24 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
11229 10604dce 2021-09-24 thomas "conflicts must be resolved before merging "
11230 10604dce 2021-09-24 thomas "can continue");
11231 10604dce 2021-09-24 thomas } else if (upa.conflicts > 0) {
11232 10604dce 2021-09-24 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
11233 10604dce 2021-09-24 thomas "conflicts must be resolved before merging "
11234 ba162991 2021-09-28 thomas "can continue; changes destined for some "
11235 88d249c2 2021-09-24 thomas "files were not yet merged and "
11236 10604dce 2021-09-24 thomas "should be merged manually if required before the "
11237 10604dce 2021-09-24 thomas "merge operation is continued");
11238 10604dce 2021-09-24 thomas } else {
11239 10604dce 2021-09-24 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
11240 ba162991 2021-09-28 thomas "changes destined for some "
11241 10604dce 2021-09-24 thomas "files were not yet merged and should be "
11242 10604dce 2021-09-24 thomas "merged manually if required before the "
11243 10604dce 2021-09-24 thomas "merge operation is continued");
11245 10604dce 2021-09-24 thomas goto done;
11246 10604dce 2021-09-24 thomas } else {
11247 10604dce 2021-09-24 thomas error = got_worktree_merge_commit(&merge_commit_id, worktree,
11248 ae1e948a 2021-09-28 thomas fileindex, author, NULL, 1, branch_tip, branch_name,
11249 ae1e948a 2021-09-28 thomas repo, continue_merge ? print_status : NULL, NULL);
11250 10604dce 2021-09-24 thomas if (error)
11251 10604dce 2021-09-24 thomas goto done;
11252 10604dce 2021-09-24 thomas error = got_worktree_merge_complete(worktree, fileindex, repo);
11253 10604dce 2021-09-24 thomas if (error)
11254 10604dce 2021-09-24 thomas goto done;
11255 10604dce 2021-09-24 thomas error = got_object_id_str(&id_str, merge_commit_id);
11256 10604dce 2021-09-24 thomas if (error)
11257 10604dce 2021-09-24 thomas goto done;
11258 10604dce 2021-09-24 thomas printf("Merged %s into %s: %s\n", branch_name,
11259 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(worktree),
11260 10604dce 2021-09-24 thomas id_str);
11264 10604dce 2021-09-24 thomas free(id_str);
11265 10604dce 2021-09-24 thomas free(merge_commit_id);
11266 10604dce 2021-09-24 thomas free(author);
11267 10604dce 2021-09-24 thomas free(branch_tip);
11268 10604dce 2021-09-24 thomas free(branch_name);
11269 10604dce 2021-09-24 thomas free(yca_id);
11270 10604dce 2021-09-24 thomas if (branch)
11271 10604dce 2021-09-24 thomas got_ref_close(branch);
11272 10604dce 2021-09-24 thomas if (wt_branch)
11273 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
11274 10604dce 2021-09-24 thomas if (worktree)
11275 10604dce 2021-09-24 thomas got_worktree_close(worktree);
11276 10604dce 2021-09-24 thomas if (repo) {
11277 10604dce 2021-09-24 thomas const struct got_error *close_err = got_repo_close(repo);
11278 10604dce 2021-09-24 thomas if (error == NULL)
11279 10604dce 2021-09-24 thomas error = close_err;
11281 715dc77e 2019-08-03 stsp return error;
11284 715dc77e 2019-08-03 stsp __dead static void
11285 715dc77e 2019-08-03 stsp usage_stage(void)
11287 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
11288 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
11289 715dc77e 2019-08-03 stsp getprogname());
11293 715dc77e 2019-08-03 stsp static const struct got_error *
11294 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
11295 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
11296 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
11297 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
11299 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
11300 408b4ebc 2019-08-03 stsp char *id_str = NULL;
11302 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
11303 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
11304 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
11305 408b4ebc 2019-08-03 stsp return NULL;
11307 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
11308 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
11309 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
11311 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
11313 408b4ebc 2019-08-03 stsp return err;
11315 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
11316 408b4ebc 2019-08-03 stsp free(id_str);
11317 dc424a06 2019-08-07 stsp return NULL;
11320 dc424a06 2019-08-07 stsp static const struct got_error *
11321 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
11323 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
11324 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
11325 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
11326 715dc77e 2019-08-03 stsp char *cwd = NULL;
11327 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
11328 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
11329 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
11330 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
11331 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
11332 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
11334 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
11336 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
11337 715dc77e 2019-08-03 stsp switch (ch) {
11338 408b4ebc 2019-08-03 stsp case 'l':
11339 408b4ebc 2019-08-03 stsp list_stage = 1;
11341 dc424a06 2019-08-07 stsp case 'p':
11342 dc424a06 2019-08-07 stsp pflag = 1;
11344 dc424a06 2019-08-07 stsp case 'F':
11345 dc424a06 2019-08-07 stsp patch_script_path = optarg;
11347 35213c7c 2020-07-23 stsp case 'S':
11348 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
11351 715dc77e 2019-08-03 stsp usage_stage();
11352 715dc77e 2019-08-03 stsp /* NOTREACHED */
11356 715dc77e 2019-08-03 stsp argc -= optind;
11357 715dc77e 2019-08-03 stsp argv += optind;
11359 715dc77e 2019-08-03 stsp #ifndef PROFILE
11360 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11361 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
11362 715dc77e 2019-08-03 stsp err(1, "pledge");
11364 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
11365 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
11366 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
11367 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
11369 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
11370 715dc77e 2019-08-03 stsp if (cwd == NULL) {
11371 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
11372 715dc77e 2019-08-03 stsp goto done;
11375 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
11376 fa51e947 2020-03-27 stsp if (error) {
11377 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
11378 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
11379 715dc77e 2019-08-03 stsp goto done;
11382 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11384 715dc77e 2019-08-03 stsp if (error != NULL)
11385 715dc77e 2019-08-03 stsp goto done;
11387 dc424a06 2019-08-07 stsp if (patch_script_path) {
11388 c56c5d8a 2021-12-31 thomas patch_script_file = fopen(patch_script_path, "re");
11389 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
11390 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
11391 dc424a06 2019-08-07 stsp patch_script_path);
11392 dc424a06 2019-08-07 stsp goto done;
11395 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
11396 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
11397 715dc77e 2019-08-03 stsp if (error)
11398 715dc77e 2019-08-03 stsp goto done;
11400 10604dce 2021-09-24 thomas error = check_merge_in_progress(worktree, repo);
11401 10604dce 2021-09-24 thomas if (error)
11402 10604dce 2021-09-24 thomas goto done;
11404 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
11405 6d022e97 2019-08-04 stsp if (error)
11406 6d022e97 2019-08-04 stsp goto done;
11408 6d022e97 2019-08-04 stsp if (list_stage)
11409 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
11410 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
11412 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
11413 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
11414 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
11415 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
11416 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
11417 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
11420 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
11421 2e1f37b0 2019-08-08 stsp error == NULL)
11422 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
11423 1d0f4054 2021-06-17 stsp if (repo) {
11424 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
11425 1d0f4054 2021-06-17 stsp if (error == NULL)
11426 1d0f4054 2021-06-17 stsp error = close_err;
11428 ad493afc 2019-08-03 stsp if (worktree)
11429 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
11430 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
11431 ad493afc 2019-08-03 stsp free((char *)pe->path);
11432 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
11433 ad493afc 2019-08-03 stsp free(cwd);
11434 ad493afc 2019-08-03 stsp return error;
11437 ad493afc 2019-08-03 stsp __dead static void
11438 ad493afc 2019-08-03 stsp usage_unstage(void)
11440 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
11441 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
11442 ad493afc 2019-08-03 stsp getprogname());
11447 ad493afc 2019-08-03 stsp static const struct got_error *
11448 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
11450 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
11451 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
11452 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
11453 ad493afc 2019-08-03 stsp char *cwd = NULL;
11454 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
11455 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
11456 9627c110 2020-04-18 stsp int ch, pflag = 0;
11457 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
11458 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
11459 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
11460 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
11462 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
11464 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
11465 ad493afc 2019-08-03 stsp switch (ch) {
11466 2e1f37b0 2019-08-08 stsp case 'p':
11467 2e1f37b0 2019-08-08 stsp pflag = 1;
11469 2e1f37b0 2019-08-08 stsp case 'F':
11470 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
11473 ad493afc 2019-08-03 stsp usage_unstage();
11474 ad493afc 2019-08-03 stsp /* NOTREACHED */
11478 ad493afc 2019-08-03 stsp argc -= optind;
11479 ad493afc 2019-08-03 stsp argv += optind;
11481 ad493afc 2019-08-03 stsp #ifndef PROFILE
11482 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11483 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
11484 ad493afc 2019-08-03 stsp err(1, "pledge");
11486 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
11487 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
11489 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
11490 ad493afc 2019-08-03 stsp if (cwd == NULL) {
11491 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
11492 ad493afc 2019-08-03 stsp goto done;
11495 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
11496 fa51e947 2020-03-27 stsp if (error) {
11497 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
11498 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
11499 ad493afc 2019-08-03 stsp goto done;
11502 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11504 ad493afc 2019-08-03 stsp if (error != NULL)
11505 ad493afc 2019-08-03 stsp goto done;
11507 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
11508 c56c5d8a 2021-12-31 thomas patch_script_file = fopen(patch_script_path, "re");
11509 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
11510 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
11511 2e1f37b0 2019-08-08 stsp patch_script_path);
11512 2e1f37b0 2019-08-08 stsp goto done;
11516 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
11517 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
11518 ad493afc 2019-08-03 stsp if (error)
11519 ad493afc 2019-08-03 stsp goto done;
11521 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
11522 ad493afc 2019-08-03 stsp if (error)
11523 ad493afc 2019-08-03 stsp goto done;
11525 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
11526 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
11527 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
11528 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
11529 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
11530 9627c110 2020-04-18 stsp if (!error)
11531 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
11533 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
11534 2e1f37b0 2019-08-08 stsp error == NULL)
11535 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
11536 1d0f4054 2021-06-17 stsp if (repo) {
11537 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
11538 1d0f4054 2021-06-17 stsp if (error == NULL)
11539 1d0f4054 2021-06-17 stsp error = close_err;
11541 715dc77e 2019-08-03 stsp if (worktree)
11542 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
11543 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
11544 715dc77e 2019-08-03 stsp free((char *)pe->path);
11545 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
11546 715dc77e 2019-08-03 stsp free(cwd);
11547 01073a5d 2019-08-22 stsp return error;
11550 01073a5d 2019-08-22 stsp __dead static void
11551 01073a5d 2019-08-22 stsp usage_cat(void)
11553 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
11554 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
11558 01073a5d 2019-08-22 stsp static const struct got_error *
11559 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11561 01073a5d 2019-08-22 stsp const struct got_error *err;
11562 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
11564 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
11566 01073a5d 2019-08-22 stsp return err;
11568 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
11569 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
11570 01073a5d 2019-08-22 stsp return err;
11573 01073a5d 2019-08-22 stsp static const struct got_error *
11574 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11576 01073a5d 2019-08-22 stsp const struct got_error *err;
11577 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
11578 56e0773d 2019-11-28 stsp int nentries, i;
11580 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
11582 01073a5d 2019-08-22 stsp return err;
11584 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
11585 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
11586 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
11587 01073a5d 2019-08-22 stsp char *id_str;
11588 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
11590 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
11591 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
11594 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
11595 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
11596 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
11597 01073a5d 2019-08-22 stsp free(id_str);
11600 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
11601 01073a5d 2019-08-22 stsp return err;
11604 d356bf75 2022-02-16 thomas static void
11605 d356bf75 2022-02-16 thomas format_gmtoff(char *buf, size_t sz, time_t gmtoff)
11607 d356bf75 2022-02-16 thomas long long h, m;
11608 d356bf75 2022-02-16 thomas char sign = '+';
11610 d356bf75 2022-02-16 thomas if (gmtoff < 0) {
11611 d356bf75 2022-02-16 thomas sign = '-';
11612 d356bf75 2022-02-16 thomas gmtoff = -gmtoff;
11615 d356bf75 2022-02-16 thomas h = (long long)gmtoff / 3600;
11616 d356bf75 2022-02-16 thomas m = ((long long)gmtoff - h*3600) / 60;
11617 d356bf75 2022-02-16 thomas snprintf(buf, sz, "%c%02lld%02lld", sign, h, m);
11620 01073a5d 2019-08-22 stsp static const struct got_error *
11621 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11623 01073a5d 2019-08-22 stsp const struct got_error *err;
11624 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
11625 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
11626 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
11627 01073a5d 2019-08-22 stsp char *id_str = NULL;
11628 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
11629 d356bf75 2022-02-16 thomas char gmtoff[6];
11631 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
11633 01073a5d 2019-08-22 stsp return err;
11635 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
11637 01073a5d 2019-08-22 stsp goto done;
11639 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
11640 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
11641 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
11642 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
11643 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
11644 01073a5d 2019-08-22 stsp char *pid_str;
11645 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
11647 01073a5d 2019-08-22 stsp goto done;
11648 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
11649 01073a5d 2019-08-22 stsp free(pid_str);
11651 d356bf75 2022-02-16 thomas format_gmtoff(gmtoff, sizeof(gmtoff),
11652 d356bf75 2022-02-16 thomas got_object_commit_get_author_gmtoff(commit));
11653 d356bf75 2022-02-16 thomas fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
11654 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
11655 d356bf75 2022-02-16 thomas (long long)got_object_commit_get_author_time(commit),
11656 d356bf75 2022-02-16 thomas gmtoff);
11658 d356bf75 2022-02-16 thomas format_gmtoff(gmtoff, sizeof(gmtoff),
11659 d356bf75 2022-02-16 thomas got_object_commit_get_committer_gmtoff(commit));
11660 d356bf75 2022-02-16 thomas fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
11661 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
11662 d356bf75 2022-02-16 thomas (long long)got_object_commit_get_committer_time(commit),
11663 d356bf75 2022-02-16 thomas gmtoff);
11665 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
11666 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
11667 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
11669 01073a5d 2019-08-22 stsp free(id_str);
11670 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
11671 01073a5d 2019-08-22 stsp return err;
11674 01073a5d 2019-08-22 stsp static const struct got_error *
11675 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11677 01073a5d 2019-08-22 stsp const struct got_error *err;
11678 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
11679 01073a5d 2019-08-22 stsp char *id_str = NULL;
11680 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
11681 d356bf75 2022-02-16 thomas char gmtoff[6];
11683 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
11685 01073a5d 2019-08-22 stsp return err;
11687 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
11689 01073a5d 2019-08-22 stsp goto done;
11691 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
11693 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
11694 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
11695 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11696 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
11698 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
11699 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11700 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
11702 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
11703 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11704 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
11706 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
11707 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11708 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
11714 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
11715 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
11717 d356bf75 2022-02-16 thomas format_gmtoff(gmtoff, sizeof(gmtoff),
11718 d356bf75 2022-02-16 thomas got_object_tag_get_tagger_gmtoff(tag));
11719 d356bf75 2022-02-16 thomas fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
11720 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
11721 d356bf75 2022-02-16 thomas (long long)got_object_tag_get_tagger_time(tag),
11722 d356bf75 2022-02-16 thomas gmtoff);
11724 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
11725 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
11726 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
11728 01073a5d 2019-08-22 stsp free(id_str);
11729 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
11730 01073a5d 2019-08-22 stsp return err;
11733 01073a5d 2019-08-22 stsp static const struct got_error *
11734 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
11736 01073a5d 2019-08-22 stsp const struct got_error *error;
11737 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
11738 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
11739 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
11740 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
11741 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
11742 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
11743 84de9106 2020-12-26 stsp struct got_reflist_head refs;
11745 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
11747 01073a5d 2019-08-22 stsp #ifndef PROFILE
11748 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
11749 01073a5d 2019-08-22 stsp NULL) == -1)
11750 01073a5d 2019-08-22 stsp err(1, "pledge");
11753 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
11754 01073a5d 2019-08-22 stsp switch (ch) {
11755 896e9b6f 2019-08-26 stsp case 'c':
11756 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
11758 01073a5d 2019-08-22 stsp case 'r':
11759 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
11760 01073a5d 2019-08-22 stsp if (repo_path == NULL)
11761 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
11763 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
11765 896e9b6f 2019-08-26 stsp case 'P':
11766 896e9b6f 2019-08-26 stsp force_path = 1;
11769 01073a5d 2019-08-22 stsp usage_cat();
11770 01073a5d 2019-08-22 stsp /* NOTREACHED */
11774 01073a5d 2019-08-22 stsp argc -= optind;
11775 01073a5d 2019-08-22 stsp argv += optind;
11777 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
11778 01073a5d 2019-08-22 stsp if (cwd == NULL) {
11779 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
11780 01073a5d 2019-08-22 stsp goto done;
11782 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
11783 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
11784 01073a5d 2019-08-22 stsp goto done;
11785 01073a5d 2019-08-22 stsp if (worktree) {
11786 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
11787 01073a5d 2019-08-22 stsp repo_path = strdup(
11788 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
11789 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
11790 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
11791 01073a5d 2019-08-22 stsp goto done;
11795 f89c34e1 2022-03-13 thomas /* Release work tree lock. */
11796 f89c34e1 2022-03-13 thomas got_worktree_close(worktree);
11797 f89c34e1 2022-03-13 thomas worktree = NULL;
11800 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
11801 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
11802 01073a5d 2019-08-22 stsp if (repo_path == NULL)
11803 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
11806 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
11807 01073a5d 2019-08-22 stsp free(repo_path);
11808 01073a5d 2019-08-22 stsp if (error != NULL)
11809 01073a5d 2019-08-22 stsp goto done;
11811 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
11812 896e9b6f 2019-08-26 stsp if (error)
11813 896e9b6f 2019-08-26 stsp goto done;
11815 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
11816 84de9106 2020-12-26 stsp if (error)
11817 84de9106 2020-12-26 stsp goto done;
11819 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
11820 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
11821 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
11822 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
11823 01073a5d 2019-08-22 stsp if (error)
11824 01073a5d 2019-08-22 stsp goto done;
11826 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
11827 896e9b6f 2019-08-26 stsp if (force_path) {
11828 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
11829 896e9b6f 2019-08-26 stsp argv[i]);
11830 896e9b6f 2019-08-26 stsp if (error)
11833 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
11834 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
11836 896e9b6f 2019-08-26 stsp if (error) {
11837 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
11838 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
11840 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
11841 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
11842 896e9b6f 2019-08-26 stsp if (error)
11847 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
11848 01073a5d 2019-08-22 stsp if (error)
11851 01073a5d 2019-08-22 stsp switch (obj_type) {
11852 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
11853 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
11855 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
11856 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
11858 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
11859 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
11861 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
11862 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
11865 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
11868 01073a5d 2019-08-22 stsp if (error)
11870 01073a5d 2019-08-22 stsp free(label);
11871 01073a5d 2019-08-22 stsp label = NULL;
11872 01073a5d 2019-08-22 stsp free(id);
11873 01073a5d 2019-08-22 stsp id = NULL;
11876 01073a5d 2019-08-22 stsp free(label);
11877 01073a5d 2019-08-22 stsp free(id);
11878 896e9b6f 2019-08-26 stsp free(commit_id);
11879 01073a5d 2019-08-22 stsp if (worktree)
11880 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
11881 01073a5d 2019-08-22 stsp if (repo) {
11882 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
11883 01073a5d 2019-08-22 stsp if (error == NULL)
11884 1d0f4054 2021-06-17 stsp error = close_err;
11886 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
11887 b2118c49 2020-07-28 stsp return error;
11890 b2118c49 2020-07-28 stsp __dead static void
11891 b2118c49 2020-07-28 stsp usage_info(void)
11893 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
11894 b2118c49 2020-07-28 stsp getprogname());
11898 b2118c49 2020-07-28 stsp static const struct got_error *
11899 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
11900 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11901 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
11903 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
11904 b2118c49 2020-07-28 stsp char *id_str = NULL;
11905 b2118c49 2020-07-28 stsp char datebuf[128];
11906 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
11907 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
11908 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11911 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
11912 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
11914 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
11915 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
11916 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
11917 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
11918 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
11921 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
11922 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
11923 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
11924 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
11925 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
11926 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
11927 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
11928 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
11930 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
11932 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
11933 b2118c49 2020-07-28 stsp if (tm == NULL)
11934 b2118c49 2020-07-28 stsp return NULL;
11935 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
11936 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
11937 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
11939 b2118c49 2020-07-28 stsp if (blob_id) {
11940 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
11942 b2118c49 2020-07-28 stsp return err;
11943 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
11944 b2118c49 2020-07-28 stsp free(id_str);
11947 b2118c49 2020-07-28 stsp if (staged_blob_id) {
11948 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
11950 b2118c49 2020-07-28 stsp return err;
11951 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
11952 b2118c49 2020-07-28 stsp free(id_str);
11955 b2118c49 2020-07-28 stsp if (commit_id) {
11956 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
11958 b2118c49 2020-07-28 stsp return err;
11959 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
11960 b2118c49 2020-07-28 stsp free(id_str);
11963 b2118c49 2020-07-28 stsp return NULL;
11966 b2118c49 2020-07-28 stsp static const struct got_error *
11967 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
11969 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
11970 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
11971 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
11972 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
11973 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11974 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
11975 b2118c49 2020-07-28 stsp int ch, show_files = 0;
11977 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
11979 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
11980 b2118c49 2020-07-28 stsp switch (ch) {
11982 b2118c49 2020-07-28 stsp usage_info();
11983 b2118c49 2020-07-28 stsp /* NOTREACHED */
11987 b2118c49 2020-07-28 stsp argc -= optind;
11988 b2118c49 2020-07-28 stsp argv += optind;
11990 b2118c49 2020-07-28 stsp #ifndef PROFILE
11991 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
11992 b2118c49 2020-07-28 stsp NULL) == -1)
11993 b2118c49 2020-07-28 stsp err(1, "pledge");
11995 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
11996 b2118c49 2020-07-28 stsp if (cwd == NULL) {
11997 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
11998 b2118c49 2020-07-28 stsp goto done;
12001 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
12002 b2118c49 2020-07-28 stsp if (error) {
12003 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
12004 8ea5c997 2021-02-07 naddy error = wrap_not_worktree_error(error, "info", cwd);
12005 b2118c49 2020-07-28 stsp goto done;
12008 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
12009 b2118c49 2020-07-28 stsp if (error)
12010 b2118c49 2020-07-28 stsp goto done;
12012 b2118c49 2020-07-28 stsp if (argc >= 1) {
12013 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
12014 b2118c49 2020-07-28 stsp worktree);
12015 b2118c49 2020-07-28 stsp if (error)
12016 b2118c49 2020-07-28 stsp goto done;
12017 b2118c49 2020-07-28 stsp show_files = 1;
12020 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
12021 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
12022 b2118c49 2020-07-28 stsp if (error)
12023 b2118c49 2020-07-28 stsp goto done;
12025 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
12026 b2118c49 2020-07-28 stsp if (error)
12027 b2118c49 2020-07-28 stsp goto done;
12029 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
12030 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
12031 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
12032 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
12033 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
12034 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
12035 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
12036 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
12038 b2118c49 2020-07-28 stsp if (show_files) {
12039 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
12040 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
12041 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
12042 b2118c49 2020-07-28 stsp continue;
12044 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
12045 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
12047 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
12048 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
12050 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
12051 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
12052 b2118c49 2020-07-28 stsp if (error)
12053 b2118c49 2020-07-28 stsp goto done;
12054 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
12055 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
12056 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
12062 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
12063 b2118c49 2020-07-28 stsp free((char *)pe->path);
12064 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
12065 b2118c49 2020-07-28 stsp free(cwd);
12066 b2118c49 2020-07-28 stsp free(id_str);
12067 b2118c49 2020-07-28 stsp free(uuidstr);
12068 0ebf8283 2019-07-24 stsp return error;