Blame


1 5c860e29 2018-03-12 stsp /*
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>
5 5c860e29 2018-03-12 stsp *
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.
9 5c860e29 2018-03-12 stsp *
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.
17 5c860e29 2018-03-12 stsp */
18 5c860e29 2018-03-12 stsp
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>
22 f42b1b34 2018-03-12 stsp
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>
39 dd038bc6 2021-09-21 thomas.ad
40 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
41 5c860e29 2018-03-12 stsp
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"
60 5c860e29 2018-03-12 stsp
61 5c860e29 2018-03-12 stsp #ifndef nitems
62 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 5c860e29 2018-03-12 stsp #endif
64 99437157 2018-11-11 stsp
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;
67 99437157 2018-11-11 stsp
68 99437157 2018-11-11 stsp static void
69 99437157 2018-11-11 stsp catch_sigint(int signo)
70 99437157 2018-11-11 stsp {
71 99437157 2018-11-11 stsp sigint_received = 1;
72 99437157 2018-11-11 stsp }
73 99437157 2018-11-11 stsp
74 99437157 2018-11-11 stsp static void
75 99437157 2018-11-11 stsp catch_sigpipe(int signo)
76 99437157 2018-11-11 stsp {
77 99437157 2018-11-11 stsp sigpipe_received = 1;
78 99437157 2018-11-11 stsp }
79 5c860e29 2018-03-12 stsp
80 99437157 2018-11-11 stsp
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;
86 5c860e29 2018-03-12 stsp };
87 5c860e29 2018-03-12 stsp
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);
119 5c860e29 2018-03-12 stsp
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 *[]);
150 5c860e29 2018-03-12 stsp
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, "" },
182 5c860e29 2018-03-12 stsp };
183 ce5b7c56 2019-07-09 stsp
184 ce5b7c56 2019-07-09 stsp static void
185 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
186 ce5b7c56 2019-07-09 stsp {
187 6059809a 2020-12-17 stsp size_t i;
188 ce5b7c56 2019-07-09 stsp
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);
193 ce5b7c56 2019-07-09 stsp }
194 6879ba42 2020-10-01 naddy fputc('\n', fp);
195 ce5b7c56 2019-07-09 stsp }
196 5c860e29 2018-03-12 stsp
197 ff69268e 2020-12-13 stsp __dead static void
198 ff69268e 2020-12-13 stsp option_conflict(char a, char b)
199 ff69268e 2020-12-13 stsp {
200 ff69268e 2020-12-13 stsp errx(1, "-%c and -%c options are mutually exclusive", a, b);
201 ff69268e 2020-12-13 stsp }
202 ff69268e 2020-12-13 stsp
203 5c860e29 2018-03-12 stsp int
204 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
205 5c860e29 2018-03-12 stsp {
206 641a8ee6 2022-02-16 thomas const struct got_cmd *cmd;
207 6059809a 2020-12-17 stsp size_t i;
208 5c860e29 2018-03-12 stsp int ch;
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 }
213 83cd27f8 2020-01-13 stsp };
214 5c860e29 2018-03-12 stsp
215 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
216 5c860e29 2018-03-12 stsp
217 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
218 5c860e29 2018-03-12 stsp switch (ch) {
219 1b6b95a8 2018-03-12 stsp case 'h':
220 1b6b95a8 2018-03-12 stsp hflag = 1;
221 53ccebc2 2019-07-30 stsp break;
222 53ccebc2 2019-07-30 stsp case 'V':
223 53ccebc2 2019-07-30 stsp Vflag = 1;
224 1b6b95a8 2018-03-12 stsp break;
225 5c860e29 2018-03-12 stsp default:
226 6879ba42 2020-10-01 naddy usage(hflag, 1);
227 5c860e29 2018-03-12 stsp /* NOTREACHED */
228 5c860e29 2018-03-12 stsp }
229 5c860e29 2018-03-12 stsp }
230 5c860e29 2018-03-12 stsp
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;
235 53ccebc2 2019-07-30 stsp
236 53ccebc2 2019-07-30 stsp if (Vflag) {
237 53ccebc2 2019-07-30 stsp got_version_print_str();
238 6879ba42 2020-10-01 naddy return 0;
239 53ccebc2 2019-07-30 stsp }
240 5c860e29 2018-03-12 stsp
241 5c860e29 2018-03-12 stsp if (argc <= 0)
242 6879ba42 2020-10-01 naddy usage(hflag, hflag ? 0 : 1);
243 5c860e29 2018-03-12 stsp
244 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
245 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
246 99437157 2018-11-11 stsp
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;
249 d7d4f210 2018-03-12 stsp
250 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
251 5c860e29 2018-03-12 stsp
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)
254 5c860e29 2018-03-12 stsp continue;
255 5c860e29 2018-03-12 stsp
256 1b6b95a8 2018-03-12 stsp if (hflag)
257 641a8ee6 2022-02-16 thomas cmd->cmd_usage();
258 1b6b95a8 2018-03-12 stsp
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);
267 d7d4f210 2018-03-12 stsp return 1;
268 d7d4f210 2018-03-12 stsp }
269 d7d4f210 2018-03-12 stsp
270 d7d4f210 2018-03-12 stsp return 0;
271 5c860e29 2018-03-12 stsp }
272 5c860e29 2018-03-12 stsp
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);
275 5c860e29 2018-03-12 stsp return 1;
276 5c860e29 2018-03-12 stsp }
277 5c860e29 2018-03-12 stsp
278 4ed7e80c 2018-05-20 stsp __dead static void
279 6879ba42 2020-10-01 naddy usage(int hflag, int status)
280 5c860e29 2018-03-12 stsp {
281 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
282 6879ba42 2020-10-01 naddy
283 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
284 53ccebc2 2019-07-30 stsp getprogname());
285 ce5b7c56 2019-07-09 stsp if (hflag)
286 6879ba42 2020-10-01 naddy list_commands(fp);
287 6879ba42 2020-10-01 naddy exit(status);
288 5c860e29 2018-03-12 stsp }
289 5c860e29 2018-03-12 stsp
290 0266afb7 2019-01-04 stsp static const struct got_error *
291 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
292 e2ba3d07 2019-05-13 stsp {
293 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
294 e2ba3d07 2019-05-13 stsp const char *editor;
295 8920fa04 2019-08-18 stsp
296 8920fa04 2019-08-18 stsp *abspath = NULL;
297 e2ba3d07 2019-05-13 stsp
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");
301 e2ba3d07 2019-05-13 stsp
302 0ee7065d 2019-05-13 stsp if (editor) {
303 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
304 0ee7065d 2019-05-13 stsp if (err)
305 0ee7065d 2019-05-13 stsp return err;
306 0ee7065d 2019-05-13 stsp }
307 e2ba3d07 2019-05-13 stsp
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");
312 0ee7065d 2019-05-13 stsp }
313 0ee7065d 2019-05-13 stsp
314 e2ba3d07 2019-05-13 stsp return NULL;
315 e2ba3d07 2019-05-13 stsp }
316 e2ba3d07 2019-05-13 stsp
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)
320 0266afb7 2019-01-04 stsp {
321 163ce85a 2019-05-13 stsp const struct got_error *err;
322 0266afb7 2019-01-04 stsp
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");
326 37c06ea4 2019-07-15 stsp #endif
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);
329 0266afb7 2019-01-04 stsp
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);
332 0266afb7 2019-01-04 stsp
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);
335 0266afb7 2019-01-04 stsp
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;
339 0266afb7 2019-01-04 stsp
340 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
341 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
342 0266afb7 2019-01-04 stsp
343 0266afb7 2019-01-04 stsp return NULL;
344 2c7829a4 2019-06-17 stsp }
345 2c7829a4 2019-06-17 stsp
346 2c7829a4 2019-06-17 stsp __dead static void
347 2c7829a4 2019-06-17 stsp usage_init(void)
348 2c7829a4 2019-06-17 stsp {
349 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
350 2c7829a4 2019-06-17 stsp exit(1);
351 2c7829a4 2019-06-17 stsp }
352 2c7829a4 2019-06-17 stsp
353 2c7829a4 2019-06-17 stsp static const struct got_error *
354 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
355 2c7829a4 2019-06-17 stsp {
356 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
357 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
358 2c7829a4 2019-06-17 stsp int ch;
359 2c7829a4 2019-06-17 stsp
360 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
361 2c7829a4 2019-06-17 stsp switch (ch) {
362 2c7829a4 2019-06-17 stsp default:
363 2c7829a4 2019-06-17 stsp usage_init();
364 2c7829a4 2019-06-17 stsp /* NOTREACHED */
365 2c7829a4 2019-06-17 stsp }
366 2c7829a4 2019-06-17 stsp }
367 2c7829a4 2019-06-17 stsp
368 2c7829a4 2019-06-17 stsp argc -= optind;
369 2c7829a4 2019-06-17 stsp argv += optind;
370 2c7829a4 2019-06-17 stsp
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");
374 2c7829a4 2019-06-17 stsp #endif
375 2c7829a4 2019-06-17 stsp if (argc != 1)
376 bc20e173 2019-06-17 stsp usage_init();
377 2c7829a4 2019-06-17 stsp
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");
381 2c7829a4 2019-06-17 stsp
382 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
383 2c7829a4 2019-06-17 stsp
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))
387 2c7829a4 2019-06-17 stsp goto done;
388 2c7829a4 2019-06-17 stsp
389 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
390 2c7829a4 2019-06-17 stsp if (error)
391 2c7829a4 2019-06-17 stsp goto done;
392 2c7829a4 2019-06-17 stsp
393 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
394 3ce1b845 2019-07-15 stsp done:
395 3ce1b845 2019-07-15 stsp free(repo_path);
396 3ce1b845 2019-07-15 stsp return error;
397 3ce1b845 2019-07-15 stsp }
398 3ce1b845 2019-07-15 stsp
399 3ce1b845 2019-07-15 stsp __dead static void
400 3ce1b845 2019-07-15 stsp usage_import(void)
401 3ce1b845 2019-07-15 stsp {
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());
404 3ce1b845 2019-07-15 stsp exit(1);
405 3ce1b845 2019-07-15 stsp }
406 3ce1b845 2019-07-15 stsp
407 3ce1b845 2019-07-15 stsp int
408 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
409 3ce1b845 2019-07-15 stsp {
410 3ce1b845 2019-07-15 stsp pid_t pid;
411 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
412 3ce1b845 2019-07-15 stsp int st = -1;
413 3ce1b845 2019-07-15 stsp
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);
417 3ce1b845 2019-07-15 stsp
418 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
419 3ce1b845 2019-07-15 stsp case -1:
420 3ce1b845 2019-07-15 stsp goto doneediting;
421 3ce1b845 2019-07-15 stsp case 0:
422 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
423 3ce1b845 2019-07-15 stsp _exit(127);
424 3ce1b845 2019-07-15 stsp }
425 3ce1b845 2019-07-15 stsp
426 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
427 3ce1b845 2019-07-15 stsp if (errno != EINTR)
428 3ce1b845 2019-07-15 stsp break;
429 3ce1b845 2019-07-15 stsp
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);
434 3ce1b845 2019-07-15 stsp
435 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
436 3ce1b845 2019-07-15 stsp errno = EINTR;
437 3ce1b845 2019-07-15 stsp return -1;
438 3ce1b845 2019-07-15 stsp }
439 3ce1b845 2019-07-15 stsp
440 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
441 3ce1b845 2019-07-15 stsp }
442 3ce1b845 2019-07-15 stsp
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)
447 3ce1b845 2019-07-15 stsp {
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;
456 3ce1b845 2019-07-15 stsp
457 3ce1b845 2019-07-15 stsp *logmsg = NULL;
458 3ce1b845 2019-07-15 stsp
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);
461 3ce1b845 2019-07-15 stsp
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");
464 3ce1b845 2019-07-15 stsp
465 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
466 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
467 3ce1b845 2019-07-15 stsp
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");
472 3ce1b845 2019-07-15 stsp
473 bfa12d5e 2020-09-26 stsp /*
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.
477 bfa12d5e 2020-09-26 stsp */
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';
482 bfa12d5e 2020-09-26 stsp
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");
486 bfa12d5e 2020-09-26 stsp goto done;
487 bfa12d5e 2020-09-26 stsp }
488 bfa12d5e 2020-09-26 stsp s = buf;
489 bfa12d5e 2020-09-26 stsp len = 0;
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);
497 bfa12d5e 2020-09-26 stsp goto done;
498 bfa12d5e 2020-09-26 stsp }
499 bfa12d5e 2020-09-26 stsp }
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';
502 bfa12d5e 2020-09-26 stsp len--;
503 bfa12d5e 2020-09-26 stsp }
504 bfa12d5e 2020-09-26 stsp
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';
510 3ce1b845 2019-07-15 stsp
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");
514 3ce1b845 2019-07-15 stsp goto done;
515 3ce1b845 2019-07-15 stsp }
516 bfa12d5e 2020-09-26 stsp
517 bfa12d5e 2020-09-26 stsp len = 0;
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);
524 bfa12d5e 2020-09-26 stsp goto done;
525 bfa12d5e 2020-09-26 stsp }
526 3ce1b845 2019-07-15 stsp }
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);
530 bfa12d5e 2020-09-26 stsp goto done;
531 bfa12d5e 2020-09-26 stsp }
532 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
533 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
534 3ce1b845 2019-07-15 stsp len--;
535 3ce1b845 2019-07-15 stsp }
536 3ce1b845 2019-07-15 stsp
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");
540 bfa12d5e 2020-09-26 stsp goto done;
541 bfa12d5e 2020-09-26 stsp }
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");
546 3ce1b845 2019-07-15 stsp done:
547 bfa12d5e 2020-09-26 stsp free(initial_content_stripped);
548 bfa12d5e 2020-09-26 stsp free(buf);
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");
551 3ce1b845 2019-07-15 stsp if (err) {
552 3ce1b845 2019-07-15 stsp free(*logmsg);
553 3ce1b845 2019-07-15 stsp *logmsg = NULL;
554 3ce1b845 2019-07-15 stsp }
555 3ce1b845 2019-07-15 stsp return err;
556 3ce1b845 2019-07-15 stsp }
557 3ce1b845 2019-07-15 stsp
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)
561 3ce1b845 2019-07-15 stsp {
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;
566 3ce1b845 2019-07-15 stsp
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");
572 3ce1b845 2019-07-15 stsp
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");
575 3ce1b845 2019-07-15 stsp if (err)
576 3ce1b845 2019-07-15 stsp goto done;
577 3ce1b845 2019-07-15 stsp
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);
580 97972933 2020-09-11 stsp goto done;
581 97972933 2020-09-11 stsp }
582 3ce1b845 2019-07-15 stsp
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);
585 3ce1b845 2019-07-15 stsp done:
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);
589 59f86c76 2020-09-11 stsp if (err) {
590 59f86c76 2020-09-11 stsp free(*logmsg_path);
591 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
592 59f86c76 2020-09-11 stsp }
593 3ce1b845 2019-07-15 stsp return err;
594 3ce1b845 2019-07-15 stsp }
595 3ce1b845 2019-07-15 stsp
596 3ce1b845 2019-07-15 stsp static const struct got_error *
597 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
598 3ce1b845 2019-07-15 stsp {
599 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
600 3ce1b845 2019-07-15 stsp return NULL;
601 17431c13 2022-02-12 thomas }
602 17431c13 2022-02-12 thomas
603 17431c13 2022-02-12 thomas static int
604 17431c13 2022-02-12 thomas valid_author(const char *author)
605 17431c13 2022-02-12 thomas {
606 17431c13 2022-02-12 thomas /*
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.
609 17431c13 2022-02-12 thomas */
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 == '>';
621 3ce1b845 2019-07-15 stsp }
622 3ce1b845 2019-07-15 stsp
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)
626 84792843 2019-08-09 stsp {
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;
630 84792843 2019-08-09 stsp
631 84792843 2019-08-09 stsp *author = NULL;
632 aba9c984 2019-09-08 stsp
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);
636 50b0790e 2020-09-11 stsp
637 50b0790e 2020-09-11 stsp /*
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)
645 50b0790e 2020-09-11 stsp */
646 50b0790e 2020-09-11 stsp
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;
658 c9956ddf 2019-09-08 stsp }
659 257add31 2020-09-09 stsp
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(
664 257add31 2020-09-09 stsp repo);
665 257add31 2020-09-09 stsp if (name && email) {
666 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
667 257add31 2020-09-09 stsp == -1)
668 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
669 257add31 2020-09-09 stsp return NULL;
670 257add31 2020-09-09 stsp }
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);
673 257add31 2020-09-09 stsp }
674 84792843 2019-08-09 stsp }
675 84792843 2019-08-09 stsp
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");
679 84792843 2019-08-09 stsp
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;
684 aba9c984 2019-09-08 stsp }
685 aba9c984 2019-09-08 stsp return err;
686 c9956ddf 2019-09-08 stsp }
687 c9956ddf 2019-09-08 stsp
688 c9956ddf 2019-09-08 stsp static const struct got_error *
689 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
690 c9956ddf 2019-09-08 stsp {
691 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
692 c9956ddf 2019-09-08 stsp
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");
697 c9956ddf 2019-09-08 stsp
698 c9956ddf 2019-09-08 stsp }
699 c9956ddf 2019-09-08 stsp return NULL;
700 84792843 2019-08-09 stsp }
701 84792843 2019-08-09 stsp
702 84792843 2019-08-09 stsp static const struct got_error *
703 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
704 3ce1b845 2019-07-15 stsp {
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;
713 3ce1b845 2019-07-15 stsp int ch;
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;
717 3ce1b845 2019-07-15 stsp
718 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
719 3ce1b845 2019-07-15 stsp
720 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
721 3ce1b845 2019-07-15 stsp switch (ch) {
722 3ce1b845 2019-07-15 stsp case 'b':
723 3ce1b845 2019-07-15 stsp branch_name = optarg;
724 3ce1b845 2019-07-15 stsp break;
725 3ce1b845 2019-07-15 stsp case 'm':
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");
729 3ce1b845 2019-07-15 stsp goto done;
730 3ce1b845 2019-07-15 stsp }
731 3ce1b845 2019-07-15 stsp break;
732 3ce1b845 2019-07-15 stsp case 'r':
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",
736 9ba1d308 2019-10-21 stsp optarg);
737 3ce1b845 2019-07-15 stsp goto done;
738 3ce1b845 2019-07-15 stsp }
739 3ce1b845 2019-07-15 stsp break;
740 3ce1b845 2019-07-15 stsp case 'I':
741 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
742 3ce1b845 2019-07-15 stsp break;
743 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
744 3ce1b845 2019-07-15 stsp NULL);
745 3ce1b845 2019-07-15 stsp if (error)
746 3ce1b845 2019-07-15 stsp goto done;
747 3ce1b845 2019-07-15 stsp break;
748 3ce1b845 2019-07-15 stsp default:
749 b2b65d18 2019-08-22 stsp usage_import();
750 3ce1b845 2019-07-15 stsp /* NOTREACHED */
751 3ce1b845 2019-07-15 stsp }
752 3ce1b845 2019-07-15 stsp }
753 3ce1b845 2019-07-15 stsp
754 3ce1b845 2019-07-15 stsp argc -= optind;
755 3ce1b845 2019-07-15 stsp argv += optind;
756 3ce1b845 2019-07-15 stsp
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 "
759 aba9c984 2019-09-08 stsp "unveil",
760 3ce1b845 2019-07-15 stsp NULL) == -1)
761 3ce1b845 2019-07-15 stsp err(1, "pledge");
762 3ce1b845 2019-07-15 stsp #endif
763 3ce1b845 2019-07-15 stsp if (argc != 1)
764 3ce1b845 2019-07-15 stsp usage_import();
765 2c7829a4 2019-06-17 stsp
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");
770 3ce1b845 2019-07-15 stsp }
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);
773 c9956ddf 2019-09-08 stsp if (error)
774 c9956ddf 2019-09-08 stsp goto done;
775 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
776 3ce1b845 2019-07-15 stsp if (error)
777 3ce1b845 2019-07-15 stsp goto done;
778 aba9c984 2019-09-08 stsp
779 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
780 aba9c984 2019-09-08 stsp if (error)
781 aba9c984 2019-09-08 stsp return error;
782 e560b7e0 2019-11-28 stsp
783 e560b7e0 2019-11-28 stsp /*
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.
787 e560b7e0 2019-11-28 stsp */
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);
790 3ce1b845 2019-07-15 stsp
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");
793 3ce1b845 2019-07-15 stsp goto done;
794 3ce1b845 2019-07-15 stsp }
795 3ce1b845 2019-07-15 stsp
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)
799 3ce1b845 2019-07-15 stsp goto done;
800 3ce1b845 2019-07-15 stsp } else {
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");
803 3ce1b845 2019-07-15 stsp goto done;
804 3ce1b845 2019-07-15 stsp }
805 3ce1b845 2019-07-15 stsp
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]);
809 3ce1b845 2019-07-15 stsp goto done;
810 3ce1b845 2019-07-15 stsp }
811 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
812 3ce1b845 2019-07-15 stsp
813 3ce1b845 2019-07-15 stsp /*
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.
816 3ce1b845 2019-07-15 stsp */
817 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
818 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
819 3ce1b845 2019-07-15 stsp if (error)
820 3ce1b845 2019-07-15 stsp goto done;
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;
828 3ce1b845 2019-07-15 stsp goto done;
829 ef293bdd 2019-10-21 stsp }
830 3ce1b845 2019-07-15 stsp }
831 3ce1b845 2019-07-15 stsp
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;
836 3ce1b845 2019-07-15 stsp goto done;
837 ef293bdd 2019-10-21 stsp }
838 3ce1b845 2019-07-15 stsp
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;
843 ef293bdd 2019-10-21 stsp goto done;
844 ef293bdd 2019-10-21 stsp }
845 ef293bdd 2019-10-21 stsp
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;
851 3ce1b845 2019-07-15 stsp goto done;
852 ef293bdd 2019-10-21 stsp }
853 3ce1b845 2019-07-15 stsp
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;
858 3ce1b845 2019-07-15 stsp goto done;
859 ef293bdd 2019-10-21 stsp }
860 3ce1b845 2019-07-15 stsp
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;
865 3ce1b845 2019-07-15 stsp goto done;
866 ef293bdd 2019-10-21 stsp }
867 3ce1b845 2019-07-15 stsp
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;
872 3ce1b845 2019-07-15 stsp goto done;
873 ef293bdd 2019-10-21 stsp }
874 3ce1b845 2019-07-15 stsp
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;
880 3ce1b845 2019-07-15 stsp goto done;
881 ef293bdd 2019-10-21 stsp }
882 3ce1b845 2019-07-15 stsp
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;
888 3ce1b845 2019-07-15 stsp goto done;
889 ef293bdd 2019-10-21 stsp }
890 3ce1b845 2019-07-15 stsp
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;
895 3ce1b845 2019-07-15 stsp goto done;
896 ef293bdd 2019-10-21 stsp }
897 3ce1b845 2019-07-15 stsp }
898 3ce1b845 2019-07-15 stsp
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);
901 2c7829a4 2019-06-17 stsp done:
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;
921 93658fb9 2020-03-18 stsp }
922 93658fb9 2020-03-18 stsp
923 93658fb9 2020-03-18 stsp __dead static void
924 93658fb9 2020-03-18 stsp usage_clone(void)
925 93658fb9 2020-03-18 stsp {
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());
928 93658fb9 2020-03-18 stsp exit(1);
929 93658fb9 2020-03-18 stsp }
930 892ac3b6 2020-03-18 stsp
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;
936 04d9a9ec 2020-09-24 stsp
937 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
938 04d9a9ec 2020-09-24 stsp
939 04d9a9ec 2020-09-24 stsp int create_configs;
940 04d9a9ec 2020-09-24 stsp int configs_created;
941 04d9a9ec 2020-09-24 stsp struct {
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;
953 892ac3b6 2020-03-18 stsp };
954 93658fb9 2020-03-18 stsp
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);
962 04d9a9ec 2020-09-24 stsp
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)
966 531c3985 2020-03-18 stsp {
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;
972 04d9a9ec 2020-09-24 stsp
973 04d9a9ec 2020-09-24 stsp /*
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.
978 04d9a9ec 2020-09-24 stsp */
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);
990 04d9a9ec 2020-09-24 stsp if (err)
991 04d9a9ec 2020-09-24 stsp return err;
992 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
993 04d9a9ec 2020-09-24 stsp }
994 b2409d58 2020-03-18 stsp
995 68999b92 2020-03-18 stsp if (a->verbosity < 0)
996 68999b92 2020-03-18 stsp return NULL;
997 68999b92 2020-03-18 stsp
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;
1002 b2409d58 2020-03-18 stsp }
1003 b2409d58 2020-03-18 stsp
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);
1012 892ac3b6 2020-03-18 stsp }
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;
1019 892ac3b6 2020-03-18 stsp }
1020 61cc1a7a 2020-03-18 stsp }
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;
1029 892ac3b6 2020-03-18 stsp }
1030 61cc1a7a 2020-03-18 stsp }
1031 3168e5da 2020-09-10 stsp
1032 d2cdc636 2020-03-18 stsp }
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);
1043 892ac3b6 2020-03-18 stsp
1044 531c3985 2020-03-18 stsp return NULL;
1045 531c3985 2020-03-18 stsp }
1046 531c3985 2020-03-18 stsp
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)
1050 4ba14133 2020-03-20 stsp {
1051 4ba14133 2020-03-20 stsp const struct got_error *err;
1052 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
1053 4ba14133 2020-03-20 stsp
1054 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1055 4ba14133 2020-03-20 stsp if (err)
1056 4ba14133 2020-03-20 stsp return err;
1057 4ba14133 2020-03-20 stsp
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));
1062 6338a6a1 2020-03-21 stsp }
1063 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
1064 4ba14133 2020-03-20 stsp return err;
1065 4ba14133 2020-03-20 stsp }
1066 4ba14133 2020-03-20 stsp
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)
1070 41b0de12 2020-03-21 stsp {
1071 41b0de12 2020-03-21 stsp const struct got_error *err;
1072 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
1073 41b0de12 2020-03-21 stsp
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;
1077 41b0de12 2020-03-21 stsp
1078 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1079 41b0de12 2020-03-21 stsp }
1080 41b0de12 2020-03-21 stsp
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;
1085 41b0de12 2020-03-21 stsp
1086 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1087 41b0de12 2020-03-21 stsp if (err)
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);
1091 41b0de12 2020-03-21 stsp }
1092 41b0de12 2020-03-21 stsp
1093 41b0de12 2020-03-21 stsp return NULL;
1094 6338a6a1 2020-03-21 stsp }
1095 6338a6a1 2020-03-21 stsp
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)
1099 6338a6a1 2020-03-21 stsp {
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;
1103 6338a6a1 2020-03-21 stsp
1104 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1105 6338a6a1 2020-03-21 stsp if (err)
1106 6338a6a1 2020-03-21 stsp return err;
1107 6338a6a1 2020-03-21 stsp
1108 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1109 6338a6a1 2020-03-21 stsp if (err)
1110 6338a6a1 2020-03-21 stsp goto done;
1111 6338a6a1 2020-03-21 stsp
1112 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1113 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1114 6338a6a1 2020-03-21 stsp
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);
1117 6338a6a1 2020-03-21 stsp done:
1118 6338a6a1 2020-03-21 stsp free(id_str);
1119 6338a6a1 2020-03-21 stsp return err;
1120 0e4002ca 2020-03-21 stsp }
1121 0e4002ca 2020-03-21 stsp
1122 0e4002ca 2020-03-21 stsp static int
1123 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1124 0e4002ca 2020-03-21 stsp {
1125 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1126 0e4002ca 2020-03-21 stsp return 0;
1127 0e4002ca 2020-03-21 stsp refname += 5;
1128 0e4002ca 2020-03-21 stsp
1129 0e4002ca 2020-03-21 stsp /*
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.
1132 0e4002ca 2020-03-21 stsp */
1133 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1134 0e4002ca 2020-03-21 stsp return 0;
1135 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1136 0e4002ca 2020-03-21 stsp return 0;
1137 0e4002ca 2020-03-21 stsp
1138 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1139 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1140 0e4002ca 2020-03-21 stsp
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)))
1143 0e4002ca 2020-03-21 stsp return 1;
1144 0e4002ca 2020-03-21 stsp
1145 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1146 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1147 41b0de12 2020-03-21 stsp }
1148 41b0de12 2020-03-21 stsp
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)
1151 0e4002ca 2020-03-21 stsp {
1152 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1153 0e4002ca 2020-03-21 stsp
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))
1156 0e4002ca 2020-03-21 stsp return 1;
1157 0e4002ca 2020-03-21 stsp }
1158 0e4002ca 2020-03-21 stsp
1159 0e4002ca 2020-03-21 stsp return 0;
1160 0e4002ca 2020-03-21 stsp }
1161 0e4002ca 2020-03-21 stsp
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)
1165 0e4002ca 2020-03-21 stsp {
1166 0e4002ca 2020-03-21 stsp const struct got_error *err;
1167 0e4002ca 2020-03-21 stsp char *remote_refname;
1168 0e4002ca 2020-03-21 stsp
1169 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1170 0e4002ca 2020-03-21 stsp refname += 5;
1171 0e4002ca 2020-03-21 stsp
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");
1175 0e4002ca 2020-03-21 stsp
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;
1179 7c0b7f42 2020-09-24 stsp }
1180 7c0b7f42 2020-09-24 stsp
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)
1187 7c0b7f42 2020-09-24 stsp {
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;
1195 7c0b7f42 2020-09-24 stsp
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) {
1199 132af4a5 2021-01-05 stsp char *s;
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;
1207 132af4a5 2021-01-05 stsp }
1208 132af4a5 2021-01-05 stsp free(branches);
1209 132af4a5 2021-01-05 stsp branches = s;
1210 132af4a5 2021-01-05 stsp }
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;
1218 99495ddb 2021-01-10 stsp }
1219 99495ddb 2021-01-10 stsp }
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) {
1223 99495ddb 2021-01-10 stsp char *s;
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;
1231 99495ddb 2021-01-10 stsp }
1232 99495ddb 2021-01-10 stsp free(refs);
1233 99495ddb 2021-01-10 stsp refs = s;
1234 132af4a5 2021-01-05 stsp }
1235 15d3c221 2021-01-05 stsp }
1236 15d3c221 2021-01-05 stsp
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;
1242 7c0b7f42 2020-09-24 stsp }
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;
1247 7c0b7f42 2020-09-24 stsp }
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"
1252 7c0b7f42 2020-09-24 stsp "%s%s%s"
1253 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1254 15d3c221 2021-01-05 stsp "%s%s%s"
1255 99495ddb 2021-01-10 stsp "%s%s%s"
1256 7c0b7f42 2020-09-24 stsp "%s"
1257 0c8b29c5 2021-01-05 stsp "%s"
1258 7c0b7f42 2020-09-24 stsp "}\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;
1268 7c0b7f42 2020-09-24 stsp }
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;
1273 7c0b7f42 2020-09-24 stsp }
1274 7c0b7f42 2020-09-24 stsp
1275 7c0b7f42 2020-09-24 stsp 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;
1281 7c0b7f42 2020-09-24 stsp }
1282 7c0b7f42 2020-09-24 stsp
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)
1288 7c0b7f42 2020-09-24 stsp {
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;
1296 7c0b7f42 2020-09-24 stsp
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;
1302 7c0b7f42 2020-09-24 stsp }
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;
1307 7c0b7f42 2020-09-24 stsp }
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;
1314 56d0a753 2021-01-20 stsp }
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;
1320 132af4a5 2021-01-05 stsp }
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) {
1324 132af4a5 2021-01-05 stsp char *s;
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;
1335 56d0a753 2021-01-20 stsp }
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;
1343 132af4a5 2021-01-05 stsp }
1344 132af4a5 2021-01-05 stsp free(branches);
1345 132af4a5 2021-01-05 stsp branches = s;
1346 7c0b7f42 2020-09-24 stsp }
1347 7c0b7f42 2020-09-24 stsp } else {
1348 7c0b7f42 2020-09-24 stsp /*
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.
1351 7c0b7f42 2020-09-24 stsp */
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;
1356 7c0b7f42 2020-09-24 stsp } else
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;
1364 56d0a753 2021-01-20 stsp }
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;
1371 99495ddb 2021-01-10 stsp }
1372 99495ddb 2021-01-10 stsp }
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) {
1376 99495ddb 2021-01-10 stsp char *s;
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;
1386 56d0a753 2021-01-20 stsp }
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;
1394 99495ddb 2021-01-10 stsp }
1395 99495ddb 2021-01-10 stsp free(refs);
1396 99495ddb 2021-01-10 stsp refs = s;
1397 7c0b7f42 2020-09-24 stsp }
1398 132af4a5 2021-01-05 stsp }
1399 99495ddb 2021-01-10 stsp
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"
1403 132af4a5 2021-01-05 stsp "%s"
1404 99495ddb 2021-01-10 stsp "%s"
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;
1410 7c0b7f42 2020-09-24 stsp }
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;
1415 7c0b7f42 2020-09-24 stsp }
1416 7c0b7f42 2020-09-24 stsp 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;
1422 0e4002ca 2020-03-21 stsp }
1423 0e4002ca 2020-03-21 stsp
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)
1430 04d9a9ec 2020-09-24 stsp {
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;
1434 04d9a9ec 2020-09-24 stsp
1435 04d9a9ec 2020-09-24 stsp /*
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.
1438 04d9a9ec 2020-09-24 stsp */
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;
1442 04d9a9ec 2020-09-24 stsp } else {
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;
1447 04d9a9ec 2020-09-24 stsp
1448 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1449 04d9a9ec 2020-09-24 stsp continue;
1450 04d9a9ec 2020-09-24 stsp
1451 04d9a9ec 2020-09-24 stsp default_branch = target;
1452 04d9a9ec 2020-09-24 stsp break;
1453 04d9a9ec 2020-09-24 stsp }
1454 04d9a9ec 2020-09-24 stsp }
1455 04d9a9ec 2020-09-24 stsp
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);
1460 04d9a9ec 2020-09-24 stsp if (err)
1461 04d9a9ec 2020-09-24 stsp return err;
1462 04d9a9ec 2020-09-24 stsp
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);
1466 04d9a9ec 2020-09-24 stsp }
1467 04d9a9ec 2020-09-24 stsp
1468 04d9a9ec 2020-09-24 stsp static const struct got_error *
1469 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1470 93658fb9 2020-03-18 stsp {
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;
1486 93658fb9 2020-03-18 stsp
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);
1491 d9b4d0c0 2020-03-18 stsp
1492 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1493 93658fb9 2020-03-18 stsp switch (ch) {
1494 659e7fbd 2020-03-20 stsp case 'a':
1495 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1496 4ba14133 2020-03-20 stsp break;
1497 4ba14133 2020-03-20 stsp case 'b':
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;
1502 659e7fbd 2020-03-20 stsp break;
1503 41b0de12 2020-03-21 stsp case 'l':
1504 41b0de12 2020-03-21 stsp list_refs_only = 1;
1505 41b0de12 2020-03-21 stsp break;
1506 469dd726 2020-03-20 stsp case 'm':
1507 469dd726 2020-03-20 stsp mirror_references = 1;
1508 469dd726 2020-03-20 stsp break;
1509 68999b92 2020-03-18 stsp case 'v':
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++;
1514 68999b92 2020-03-18 stsp break;
1515 68999b92 2020-03-18 stsp case 'q':
1516 68999b92 2020-03-18 stsp verbosity = -1;
1517 68999b92 2020-03-18 stsp break;
1518 0e4002ca 2020-03-21 stsp case 'R':
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;
1523 0e4002ca 2020-03-21 stsp break;
1524 93658fb9 2020-03-18 stsp default:
1525 93658fb9 2020-03-18 stsp usage_clone();
1526 93658fb9 2020-03-18 stsp break;
1527 93658fb9 2020-03-18 stsp }
1528 93658fb9 2020-03-18 stsp }
1529 93658fb9 2020-03-18 stsp argc -= optind;
1530 93658fb9 2020-03-18 stsp argv += optind;
1531 39c64a6a 2020-03-18 stsp
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');
1543 41b0de12 2020-03-21 stsp }
1544 4ba14133 2020-03-20 stsp
1545 93658fb9 2020-03-18 stsp uri = argv[0];
1546 9df6f38b 2020-03-18 stsp
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];
1551 93658fb9 2020-03-18 stsp else
1552 93658fb9 2020-03-18 stsp usage_clone();
1553 09838ffc 2020-03-18 stsp
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;
1558 09f63084 2020-03-20 stsp
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;
1564 09f63084 2020-03-20 stsp }
1565 09838ffc 2020-03-18 stsp
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");
1571 b46f3e71 2020-03-18 stsp #endif
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");
1578 b46f3e71 2020-03-18 stsp #endif
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;
1583 39c64a6a 2020-03-18 stsp } else {
1584 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1585 39c64a6a 2020-03-18 stsp goto done;
1586 39c64a6a 2020-03-18 stsp }
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;
1591 bb64b798 2020-03-18 stsp }
1592 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1593 bb64b798 2020-03-18 stsp } else
1594 bb64b798 2020-03-18 stsp repo_path = dirname;
1595 bb64b798 2020-03-18 stsp
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;
1606 2751fe64 2020-09-24 stsp }
1607 41b0de12 2020-03-21 stsp }
1608 bb64b798 2020-03-18 stsp
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;
1612 d65a88a2 2021-09-05 stsp
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;
1616 f79e6490 2020-04-19 stsp
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 : "");
1620 ee448f5f 2020-03-18 stsp
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;
1625 bb64b798 2020-03-18 stsp
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;
1633 2751fe64 2020-09-24 stsp }
1634 2751fe64 2020-09-24 stsp
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;
1659 d9b4d0c0 2020-03-18 stsp
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;
1663 41b0de12 2020-03-21 stsp }
1664 41b0de12 2020-03-21 stsp
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;
1669 c3741b51 2021-12-31 thomas }
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);
1676 d9b4d0c0 2020-03-18 stsp
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;
1682 0e4002ca 2020-03-21 stsp
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;
1690 0e4002ca 2020-03-21 stsp continue;
1691 0e4002ca 2020-03-21 stsp }
1692 668a20f6 2020-03-18 stsp
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;
1696 d9b4d0c0 2020-03-18 stsp
1697 469dd726 2020-03-20 stsp if (mirror_references)
1698 469dd726 2020-03-20 stsp continue;
1699 469dd726 2020-03-20 stsp
1700 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1701 7ebc0570 2020-03-18 stsp continue;
1702 7ebc0570 2020-03-18 stsp
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;
1708 7ebc0570 2020-03-18 stsp }
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;
1713 d9b4d0c0 2020-03-18 stsp }
1714 d9b4d0c0 2020-03-18 stsp
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;
1721 d9b4d0c0 2020-03-18 stsp
1722 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1723 d9b4d0c0 2020-03-18 stsp continue;
1724 d9b4d0c0 2020-03-18 stsp
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;
1729 d9b4d0c0 2020-03-18 stsp continue;
1730 55330abe 2020-03-20 stsp }
1731 d9b4d0c0 2020-03-18 stsp goto done;
1732 d9b4d0c0 2020-03-18 stsp }
1733 d9b4d0c0 2020-03-18 stsp
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;
1738 f298ae0f 2020-03-25 stsp
1739 f298ae0f 2020-03-25 stsp if (mirror_references)
1740 f298ae0f 2020-03-25 stsp continue;
1741 f298ae0f 2020-03-25 stsp
1742 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1743 f298ae0f 2020-03-25 stsp continue;
1744 f298ae0f 2020-03-25 stsp
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;
1750 f298ae0f 2020-03-25 stsp }
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;
1757 f298ae0f 2020-03-25 stsp }
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;
1764 f298ae0f 2020-03-25 stsp continue;
1765 f298ae0f 2020-03-25 stsp }
1766 f298ae0f 2020-03-25 stsp goto done;
1767 f298ae0f 2020-03-25 stsp }
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;
1775 4ba14133 2020-03-20 stsp }
1776 4ba14133 2020-03-20 stsp if (pe == NULL) {
1777 4ba14133 2020-03-20 stsp /*
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.
1781 4ba14133 2020-03-20 stsp */
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;
1785 d9b4d0c0 2020-03-18 stsp
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;
1790 4ba14133 2020-03-20 stsp continue;
1791 4ba14133 2020-03-20 stsp }
1792 4ba14133 2020-03-20 stsp goto done;
1793 4ba14133 2020-03-20 stsp }
1794 4ba14133 2020-03-20 stsp
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;
1800 4ba14133 2020-03-20 stsp break;
1801 4ba14133 2020-03-20 stsp }
1802 659e7fbd 2020-03-20 stsp }
1803 659e7fbd 2020-03-20 stsp
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);
1807 09838ffc 2020-03-18 stsp done:
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");
1813 9c52365f 2020-03-21 stsp }
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;
1820 1d0f4054 2021-06-17 stsp }
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);
1824 d9b4d0c0 2020-03-18 stsp }
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);
1829 d9b4d0c0 2020-03-18 stsp }
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;
1842 7848a0e1 2020-03-19 stsp }
1843 7848a0e1 2020-03-19 stsp
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)
1847 7848a0e1 2020-03-19 stsp {
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;
1851 7848a0e1 2020-03-19 stsp
1852 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1853 7848a0e1 2020-03-19 stsp if (err)
1854 7848a0e1 2020-03-19 stsp goto done;
1855 7848a0e1 2020-03-19 stsp
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);
1859 88609724 2020-03-21 stsp if (err)
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);
1866 db6d8ad8 2020-03-21 stsp }
1867 db6d8ad8 2020-03-21 stsp goto done;
1868 db6d8ad8 2020-03-21 stsp }
1869 db6d8ad8 2020-03-21 stsp
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));
1875 688f11b3 2020-03-21 stsp }
1876 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1877 7848a0e1 2020-03-19 stsp if (err)
1878 7848a0e1 2020-03-19 stsp goto done;
1879 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1880 e8a967e0 2020-03-21 stsp if (err)
1881 e8a967e0 2020-03-21 stsp goto done;
1882 7848a0e1 2020-03-19 stsp } else {
1883 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1884 7848a0e1 2020-03-19 stsp if (err)
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;
1888 6338a6a1 2020-03-21 stsp
1889 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1890 6338a6a1 2020-03-21 stsp if (err)
1891 6338a6a1 2020-03-21 stsp goto done;
1892 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1893 6338a6a1 2020-03-21 stsp if (err)
1894 6338a6a1 2020-03-21 stsp goto done;
1895 7848a0e1 2020-03-19 stsp }
1896 6338a6a1 2020-03-21 stsp
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);
1900 7848a0e1 2020-03-19 stsp done:
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;
1904 2ab43947 2020-03-18 stsp }
1905 f1bcca34 2020-03-25 stsp
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)
1909 f1bcca34 2020-03-25 stsp {
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;
1913 f1bcca34 2020-03-25 stsp
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);
1919 bcf34b0e 2020-03-26 stsp if (err)
1920 bcf34b0e 2020-03-26 stsp goto done;
1921 2ab43947 2020-03-18 stsp
1922 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1923 bcf34b0e 2020-03-26 stsp if (err)
1924 bcf34b0e 2020-03-26 stsp goto done;
1925 f1bcca34 2020-03-25 stsp
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));
1930 bcf34b0e 2020-03-26 stsp } else {
1931 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1932 f1bcca34 2020-03-25 stsp
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;
1936 bcf34b0e 2020-03-26 stsp
1937 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1938 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1939 bcf34b0e 2020-03-26 stsp if (err)
1940 bcf34b0e 2020-03-26 stsp goto done;
1941 bcf34b0e 2020-03-26 stsp
1942 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1943 bcf34b0e 2020-03-26 stsp if (err)
1944 bcf34b0e 2020-03-26 stsp goto done;
1945 bcf34b0e 2020-03-26 stsp
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));
1949 bcf34b0e 2020-03-26 stsp
1950 bcf34b0e 2020-03-26 stsp }
1951 f1bcca34 2020-03-25 stsp done:
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;
1956 bcf34b0e 2020-03-26 stsp }
1957 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1958 f1bcca34 2020-03-25 stsp return err;
1959 f1bcca34 2020-03-25 stsp }
1960 f1bcca34 2020-03-25 stsp
1961 2ab43947 2020-03-18 stsp __dead static void
1962 7848a0e1 2020-03-19 stsp usage_fetch(void)
1963 7848a0e1 2020-03-19 stsp {
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());
1968 7848a0e1 2020-03-19 stsp exit(1);
1969 7848a0e1 2020-03-19 stsp }
1970 7848a0e1 2020-03-19 stsp
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)
1974 f21ec2f0 2020-03-21 stsp {
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;
1978 3789fd73 2020-03-26 stsp
1979 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1980 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1981 3789fd73 2020-03-26 stsp if (err)
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));
1987 3789fd73 2020-03-26 stsp }
1988 3789fd73 2020-03-26 stsp } else {
1989 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1990 3789fd73 2020-03-26 stsp if (err)
1991 3789fd73 2020-03-26 stsp return err;
1992 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1993 3789fd73 2020-03-26 stsp if (err)
1994 3789fd73 2020-03-26 stsp goto done;
1995 3168e5da 2020-09-10 stsp
1996 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1997 3789fd73 2020-03-26 stsp if (err)
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);
2002 3789fd73 2020-03-26 stsp }
2003 3789fd73 2020-03-26 stsp }
2004 3789fd73 2020-03-26 stsp done:
2005 3789fd73 2020-03-26 stsp free(id);
2006 3789fd73 2020-03-26 stsp free(id_str);
2007 3789fd73 2020-03-26 stsp return NULL;
2008 3789fd73 2020-03-26 stsp }
2009 3789fd73 2020-03-26 stsp
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)
2015 3789fd73 2020-03-26 stsp {
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;
2022 f21ec2f0 2020-03-21 stsp
2023 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&my_refs);
2024 f21ec2f0 2020-03-21 stsp
2025 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2026 3789fd73 2020-03-26 stsp == -1)
2027 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
2028 3789fd73 2020-03-26 stsp
2029 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2030 f21ec2f0 2020-03-21 stsp if (err)
2031 3789fd73 2020-03-26 stsp goto done;
2032 f21ec2f0 2020-03-21 stsp
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;
2036 f21ec2f0 2020-03-21 stsp
2037 1b796c3f 2021-09-11 stsp if (remote->mirror_references) {
2038 1b796c3f 2021-09-11 stsp their_refname = refname;
2039 1b796c3f 2021-09-11 stsp } else {
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)
2044 3789fd73 2020-03-26 stsp continue;
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;
2049 3789fd73 2020-03-26 stsp }
2050 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
2051 3789fd73 2020-03-26 stsp continue;
2052 f21ec2f0 2020-03-21 stsp
2053 1b796c3f 2021-09-11 stsp their_refname = local_refname;
2054 1b796c3f 2021-09-11 stsp }
2055 1b796c3f 2021-09-11 stsp
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)
2058 f21ec2f0 2020-03-21 stsp break;
2059 f21ec2f0 2020-03-21 stsp }
2060 f21ec2f0 2020-03-21 stsp if (pe != NULL)
2061 f21ec2f0 2020-03-21 stsp continue;
2062 f21ec2f0 2020-03-21 stsp
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)
2065 3789fd73 2020-03-26 stsp break;
2066 3789fd73 2020-03-26 stsp }
2067 3789fd73 2020-03-26 stsp if (pe != NULL)
2068 3789fd73 2020-03-26 stsp continue;
2069 f21ec2f0 2020-03-21 stsp
2070 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
2071 f21ec2f0 2020-03-21 stsp if (err)
2072 f21ec2f0 2020-03-21 stsp break;
2073 3789fd73 2020-03-26 stsp
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)
2079 3789fd73 2020-03-26 stsp break;
2080 3789fd73 2020-03-26 stsp free(local_refname);
2081 3789fd73 2020-03-26 stsp local_refname = NULL;
2082 3789fd73 2020-03-26 stsp continue;
2083 3789fd73 2020-03-26 stsp }
2084 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
2085 3789fd73 2020-03-26 stsp if (err)
2086 3789fd73 2020-03-26 stsp break;
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;
2091 3789fd73 2020-03-26 stsp break;
2092 3789fd73 2020-03-26 stsp }
2093 3789fd73 2020-03-26 stsp
2094 3789fd73 2020-03-26 stsp free(local_refname);
2095 3789fd73 2020-03-26 stsp local_refname = NULL;
2096 6338a6a1 2020-03-21 stsp }
2097 f21ec2f0 2020-03-21 stsp }
2098 3789fd73 2020-03-26 stsp done:
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;
2102 0e4002ca 2020-03-21 stsp }
2103 0e4002ca 2020-03-21 stsp
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)
2107 0e4002ca 2020-03-21 stsp {
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;
2111 0e4002ca 2020-03-21 stsp
2112 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
2113 0e4002ca 2020-03-21 stsp refname += 5;
2114 0e4002ca 2020-03-21 stsp
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");
2118 f21ec2f0 2020-03-21 stsp
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);
2124 0e4002ca 2020-03-21 stsp } else {
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);
2130 0e4002ca 2020-03-21 stsp }
2131 0e4002ca 2020-03-21 stsp done:
2132 0e4002ca 2020-03-21 stsp free(remote_refname);
2133 161728eb 2021-07-24 stsp return err;
2134 161728eb 2021-07-24 stsp }
2135 161728eb 2021-07-24 stsp
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)
2138 161728eb 2021-07-24 stsp {
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;
2143 161728eb 2021-07-24 stsp
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);
2146 161728eb 2021-07-24 stsp } else {
2147 161728eb 2021-07-24 stsp err = got_ref_resolve(&id, repo, ref);
2148 161728eb 2021-07-24 stsp if (err)
2149 161728eb 2021-07-24 stsp goto done;
2150 161728eb 2021-07-24 stsp err = got_object_id_str(&id_str, id);
2151 161728eb 2021-07-24 stsp if (err)
2152 161728eb 2021-07-24 stsp goto done;
2153 161728eb 2021-07-24 stsp target = id_str;
2154 161728eb 2021-07-24 stsp }
2155 161728eb 2021-07-24 stsp
2156 161728eb 2021-07-24 stsp err = got_ref_delete(ref, repo);
2157 161728eb 2021-07-24 stsp if (err)
2158 161728eb 2021-07-24 stsp goto done;
2159 161728eb 2021-07-24 stsp
2160 161728eb 2021-07-24 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2161 161728eb 2021-07-24 stsp done:
2162 161728eb 2021-07-24 stsp free(id);
2163 161728eb 2021-07-24 stsp free(id_str);
2164 f21ec2f0 2020-03-21 stsp return err;
2165 f21ec2f0 2020-03-21 stsp }
2166 f21ec2f0 2020-03-21 stsp
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)
2169 161728eb 2021-07-24 stsp {
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;
2174 161728eb 2021-07-24 stsp
2175 161728eb 2021-07-24 stsp TAILQ_INIT(&refs);
2176 161728eb 2021-07-24 stsp
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;
2180 161728eb 2021-07-24 stsp }
2181 161728eb 2021-07-24 stsp err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2182 161728eb 2021-07-24 stsp if (err)
2183 161728eb 2021-07-24 stsp goto done;
2184 161728eb 2021-07-24 stsp
2185 161728eb 2021-07-24 stsp TAILQ_FOREACH(re, &refs, entry)
2186 161728eb 2021-07-24 stsp delete_ref(repo, re->ref);
2187 161728eb 2021-07-24 stsp done:
2188 161728eb 2021-07-24 stsp got_ref_list_free(&refs);
2189 161728eb 2021-07-24 stsp return err;
2190 161728eb 2021-07-24 stsp }
2191 161728eb 2021-07-24 stsp
2192 161728eb 2021-07-24 stsp static const struct got_error *
2193 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
2194 7848a0e1 2020-03-19 stsp {
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;
2214 7848a0e1 2020-03-19 stsp
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);
2219 7848a0e1 2020-03-19 stsp
2220 161728eb 2021-07-24 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2221 7848a0e1 2020-03-19 stsp switch (ch) {
2222 659e7fbd 2020-03-20 stsp case 'a':
2223 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
2224 4ba14133 2020-03-20 stsp break;
2225 4ba14133 2020-03-20 stsp case 'b':
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;
2230 41b0de12 2020-03-21 stsp break;
2231 f21ec2f0 2020-03-21 stsp case 'd':
2232 f21ec2f0 2020-03-21 stsp delete_refs = 1;
2233 f21ec2f0 2020-03-21 stsp break;
2234 41b0de12 2020-03-21 stsp case 'l':
2235 41b0de12 2020-03-21 stsp list_refs_only = 1;
2236 659e7fbd 2020-03-20 stsp break;
2237 7848a0e1 2020-03-19 stsp case 'r':
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",
2241 7848a0e1 2020-03-19 stsp optarg);
2242 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
2243 7848a0e1 2020-03-19 stsp break;
2244 db6d8ad8 2020-03-21 stsp case 't':
2245 db6d8ad8 2020-03-21 stsp replace_tags = 1;
2246 db6d8ad8 2020-03-21 stsp break;
2247 7848a0e1 2020-03-19 stsp case 'v':
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++;
2252 7848a0e1 2020-03-19 stsp break;
2253 7848a0e1 2020-03-19 stsp case 'q':
2254 7848a0e1 2020-03-19 stsp verbosity = -1;
2255 7848a0e1 2020-03-19 stsp break;
2256 0e4002ca 2020-03-21 stsp case 'R':
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;
2261 0e4002ca 2020-03-21 stsp break;
2262 161728eb 2021-07-24 stsp case 'X':
2263 161728eb 2021-07-24 stsp delete_remote = 1;
2264 161728eb 2021-07-24 stsp break;
2265 7848a0e1 2020-03-19 stsp default:
2266 7848a0e1 2020-03-19 stsp usage_fetch();
2267 7848a0e1 2020-03-19 stsp break;
2268 7848a0e1 2020-03-19 stsp }
2269 7848a0e1 2020-03-19 stsp }
2270 7848a0e1 2020-03-19 stsp argc -= optind;
2271 7848a0e1 2020-03-19 stsp argv += optind;
2272 7848a0e1 2020-03-19 stsp
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');
2284 41b0de12 2020-03-21 stsp }
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');
2296 161728eb 2021-07-24 stsp }
2297 161728eb 2021-07-24 stsp
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];
2304 7848a0e1 2020-03-19 stsp else
2305 7848a0e1 2020-03-19 stsp usage_fetch();
2306 7848a0e1 2020-03-19 stsp
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;
2311 7848a0e1 2020-03-19 stsp }
2312 7848a0e1 2020-03-19 stsp
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;
2317 7848a0e1 2020-03-19 stsp else
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;
2326 7848a0e1 2020-03-19 stsp } else {
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;
2331 7848a0e1 2020-03-19 stsp }
2332 7848a0e1 2020-03-19 stsp }
2333 7848a0e1 2020-03-19 stsp }
2334 7848a0e1 2020-03-19 stsp
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;
2338 7848a0e1 2020-03-19 stsp
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 */
2342 161728eb 2021-07-24 stsp }
2343 161728eb 2021-07-24 stsp
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];
2352 50b0790e 2020-09-11 stsp break;
2353 54eb00d5 2020-10-20 stsp }
2354 50b0790e 2020-09-11 stsp }
2355 50b0790e 2020-09-11 stsp }
2356 7848a0e1 2020-03-19 stsp }
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];
2365 50b0790e 2020-09-11 stsp break;
2366 54eb00d5 2020-10-20 stsp }
2367 50b0790e 2020-09-11 stsp }
2368 50b0790e 2020-09-11 stsp }
2369 50b0790e 2020-09-11 stsp }
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];
2375 257add31 2020-09-09 stsp break;
2376 54eb00d5 2020-10-20 stsp }
2377 257add31 2020-09-09 stsp }
2378 7848a0e1 2020-03-19 stsp }
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;
2382 b8adfa55 2020-09-25 stsp }
2383 b8adfa55 2020-09-25 stsp
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);
2390 99495ddb 2021-01-10 stsp }
2391 99495ddb 2021-01-10 stsp }
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);
2396 b8adfa55 2020-09-25 stsp }
2397 50b0790e 2020-09-11 stsp }
2398 7848a0e1 2020-03-19 stsp
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;
2403 7848a0e1 2020-03-19 stsp
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");
2409 7848a0e1 2020-03-19 stsp #endif
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");
2416 7848a0e1 2020-03-19 stsp #endif
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;
2421 7848a0e1 2020-03-19 stsp } else {
2422 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2423 7848a0e1 2020-03-19 stsp goto done;
2424 7848a0e1 2020-03-19 stsp }
2425 7848a0e1 2020-03-19 stsp
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;
2429 d65a88a2 2021-09-05 stsp
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;
2433 f79e6490 2020-04-19 stsp
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 : "");
2437 7848a0e1 2020-03-19 stsp
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;
2442 7848a0e1 2020-03-19 stsp
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;
2457 7848a0e1 2020-03-19 stsp
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;
2461 41b0de12 2020-03-21 stsp }
2462 41b0de12 2020-03-21 stsp
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;
2473 984065c8 2020-03-19 stsp }
2474 7848a0e1 2020-03-19 stsp
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;
2481 7848a0e1 2020-03-19 stsp
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;
2488 0e4002ca 2020-03-21 stsp continue;
2489 0e4002ca 2020-03-21 stsp }
2490 0e4002ca 2020-03-21 stsp
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,
2498 6338a6a1 2020-03-21 stsp repo);
2499 7848a0e1 2020-03-19 stsp if (error)
2500 7848a0e1 2020-03-19 stsp goto done;
2501 7848a0e1 2020-03-19 stsp } else {
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;
2510 7848a0e1 2020-03-19 stsp }
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;
2516 7848a0e1 2020-03-19 stsp }
2517 7848a0e1 2020-03-19 stsp
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;
2526 7848a0e1 2020-03-19 stsp } else {
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;
2535 7848a0e1 2020-03-19 stsp }
2536 2ec30c80 2020-03-20 stsp
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,
2543 6338a6a1 2020-03-21 stsp repo);
2544 2ec30c80 2020-03-20 stsp if (error)
2545 2ec30c80 2020-03-20 stsp goto done;
2546 9f142382 2020-03-21 stsp } else {
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);
2551 9f142382 2020-03-21 stsp }
2552 7848a0e1 2020-03-19 stsp }
2553 7848a0e1 2020-03-19 stsp }
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;
2559 f1bcca34 2020-03-25 stsp }
2560 f1bcca34 2020-03-25 stsp
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;
2568 f1bcca34 2020-03-25 stsp
2569 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2570 f1bcca34 2020-03-25 stsp continue;
2571 f1bcca34 2020-03-25 stsp
2572 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2573 f1bcca34 2020-03-25 stsp continue;
2574 f1bcca34 2020-03-25 stsp
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;
2579 f1bcca34 2020-03-25 stsp }
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;
2585 f1bcca34 2020-03-25 stsp }
2586 f1bcca34 2020-03-25 stsp
2587 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2588 f1bcca34 2020-03-25 stsp 0);
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;
2594 f1bcca34 2020-03-25 stsp continue;
2595 f1bcca34 2020-03-25 stsp }
2596 f1bcca34 2020-03-25 stsp goto done;
2597 f1bcca34 2020-03-25 stsp }
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;
2605 f1bcca34 2020-03-25 stsp }
2606 f1bcca34 2020-03-25 stsp }
2607 7848a0e1 2020-03-19 stsp 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");
2613 9c52365f 2020-03-21 stsp }
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;
2620 1d0f4054 2021-06-17 stsp }
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);
2626 7848a0e1 2020-03-19 stsp }
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);
2631 7848a0e1 2020-03-19 stsp }
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;
2645 7848a0e1 2020-03-19 stsp }
2646 7848a0e1 2020-03-19 stsp
2647 7848a0e1 2020-03-19 stsp
2648 7848a0e1 2020-03-19 stsp __dead static void
2649 2ab43947 2020-03-18 stsp usage_checkout(void)
2650 2ab43947 2020-03-18 stsp {
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());
2654 2ab43947 2020-03-18 stsp exit(1);
2655 2ab43947 2020-03-18 stsp }
2656 2ab43947 2020-03-18 stsp
2657 2ab43947 2020-03-18 stsp static void
2658 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2659 2ab43947 2020-03-18 stsp {
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());
2665 2ab43947 2020-03-18 stsp }
2666 2ab43947 2020-03-18 stsp
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;
2671 2ab43947 2020-03-18 stsp };
2672 2ab43947 2020-03-18 stsp
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)
2675 2ab43947 2020-03-18 stsp {
2676 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2677 2ab43947 2020-03-18 stsp
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;
2681 2ab43947 2020-03-18 stsp
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;
2685 2ab43947 2020-03-18 stsp }
2686 2ab43947 2020-03-18 stsp
2687 2ab43947 2020-03-18 stsp while (path[0] == '/')
2688 2ab43947 2020-03-18 stsp path++;
2689 2ab43947 2020-03-18 stsp
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);
2692 4ad4a1ec 2021-09-13 tracey
2693 2ab43947 2020-03-18 stsp return NULL;
2694 2ab43947 2020-03-18 stsp }
2695 2ab43947 2020-03-18 stsp
2696 2ab43947 2020-03-18 stsp static const struct got_error *
2697 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2698 2ab43947 2020-03-18 stsp {
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;
2702 2ab43947 2020-03-18 stsp }
2703 2ab43947 2020-03-18 stsp
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)
2708 2ab43947 2020-03-18 stsp {
2709 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2710 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2711 2ab43947 2020-03-18 stsp
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);
2714 2ab43947 2020-03-18 stsp if (err)
2715 2ab43947 2020-03-18 stsp return err;
2716 2ab43947 2020-03-18 stsp
2717 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2718 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2719 2ab43947 2020-03-18 stsp
2720 2ab43947 2020-03-18 stsp /*
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.
2723 2ab43947 2020-03-18 stsp *
2724 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2725 2ab43947 2020-03-18 stsp *
2726 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2727 2ab43947 2020-03-18 stsp * \ /
2728 2ab43947 2020-03-18 stsp * C E
2729 2ab43947 2020-03-18 stsp * \ /
2730 2ab43947 2020-03-18 stsp * B (yca)
2731 2ab43947 2020-03-18 stsp * |
2732 2ab43947 2020-03-18 stsp * A
2733 2ab43947 2020-03-18 stsp *
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)
2737 2ab43947 2020-03-18 stsp */
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);
2744 2ab43947 2020-03-18 stsp
2745 2ab43947 2020-03-18 stsp free(yca_id);
2746 2ab43947 2020-03-18 stsp return NULL;
2747 2ab43947 2020-03-18 stsp }
2748 2ab43947 2020-03-18 stsp
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)
2753 2ab43947 2020-03-18 stsp {
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;
2758 2ab43947 2020-03-18 stsp
2759 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2760 2ab43947 2020-03-18 stsp if (err)
2761 2ab43947 2020-03-18 stsp goto done;
2762 2ab43947 2020-03-18 stsp
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;
2766 2ab43947 2020-03-18 stsp }
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;
2770 2ab43947 2020-03-18 stsp }
2771 2ab43947 2020-03-18 stsp
2772 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2773 2ab43947 2020-03-18 stsp if (err)
2774 2ab43947 2020-03-18 stsp goto done;
2775 2ab43947 2020-03-18 stsp
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);
2778 2ab43947 2020-03-18 stsp if (err)
2779 2ab43947 2020-03-18 stsp goto done;
2780 2ab43947 2020-03-18 stsp
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;
2788 2ab43947 2020-03-18 stsp break;
2789 2ab43947 2020-03-18 stsp }
2790 2ab43947 2020-03-18 stsp
2791 2ab43947 2020-03-18 stsp if (id) {
2792 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2793 2ab43947 2020-03-18 stsp break;
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;
2796 2ab43947 2020-03-18 stsp break;
2797 2ab43947 2020-03-18 stsp }
2798 2ab43947 2020-03-18 stsp }
2799 2ab43947 2020-03-18 stsp }
2800 2ab43947 2020-03-18 stsp done:
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;
2807 a367ff0f 2019-05-14 stsp }
2808 8069f636 2019-01-12 stsp
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)
2811 4b6c9460 2020-03-05 stsp {
2812 4b6c9460 2020-03-05 stsp static char msg[512];
2813 4b6c9460 2020-03-05 stsp const char *branch_name;
2814 4b6c9460 2020-03-05 stsp
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);
2817 4b6c9460 2020-03-05 stsp else
2818 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2819 4b6c9460 2020-03-05 stsp
2820 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2821 4b6c9460 2020-03-05 stsp branch_name += 11;
2822 4b6c9460 2020-03-05 stsp
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);
2829 4b6c9460 2020-03-05 stsp
2830 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2831 4b6c9460 2020-03-05 stsp }
2832 4b6c9460 2020-03-05 stsp
2833 4b6c9460 2020-03-05 stsp static const struct got_error *
2834 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2835 c09a553d 2018-03-12 stsp {
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;
2850 f2ea84fa 2019-07-27 stsp
2851 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2852 c09a553d 2018-03-12 stsp
2853 4ad4a1ec 2021-09-13 tracey while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2854 0bb8a95e 2018-03-12 stsp switch (ch) {
2855 08573d5b 2019-05-14 stsp case 'b':
2856 08573d5b 2019-05-14 stsp branch_name = optarg;
2857 08573d5b 2019-05-14 stsp break;
2858 8069f636 2019-01-12 stsp case 'c':
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");
2862 bb51a5b4 2020-01-13 stsp break;
2863 bb51a5b4 2020-01-13 stsp case 'E':
2864 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2865 8069f636 2019-01-12 stsp break;
2866 0bb8a95e 2018-03-12 stsp case 'p':
2867 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2868 0bb8a95e 2018-03-12 stsp break;
2869 4ad4a1ec 2021-09-13 tracey case 'q':
2870 4ad4a1ec 2021-09-13 tracey verbosity = -1;
2871 4ad4a1ec 2021-09-13 tracey break;
2872 0bb8a95e 2018-03-12 stsp default:
2873 2deda0b9 2019-03-07 stsp usage_checkout();
2874 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2875 0bb8a95e 2018-03-12 stsp }
2876 0bb8a95e 2018-03-12 stsp }
2877 0bb8a95e 2018-03-12 stsp
2878 0bb8a95e 2018-03-12 stsp argc -= optind;
2879 0bb8a95e 2018-03-12 stsp argv += optind;
2880 0bb8a95e 2018-03-12 stsp
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");
2885 6715a751 2018-03-16 stsp #endif
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;
2896 76089277 2018-04-01 stsp }
2897 c47340da 2020-09-20 stsp if (path_prefix[0])
2898 f0207f6a 2020-10-19 stsp path = path_prefix;
2899 c47340da 2020-09-20 stsp else
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;
2911 c09a553d 2018-03-12 stsp }
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;
2918 76089277 2018-04-01 stsp }
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",
2923 b4b3a7dd 2019-07-22 stsp argv[1]);
2924 b4b3a7dd 2019-07-22 stsp goto done;
2925 b4b3a7dd 2019-07-22 stsp }
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;
2930 b4b3a7dd 2019-07-22 stsp }
2931 76089277 2018-04-01 stsp }
2932 c09a553d 2018-03-12 stsp } else
2933 c09a553d 2018-03-12 stsp usage_checkout();
2934 c09a553d 2018-03-12 stsp
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);
2937 13bfb272 2019-05-10 jcs
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;
2941 c02c541e 2019-03-29 stsp
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;
2953 c530dc23 2019-07-23 stsp }
2954 c530dc23 2019-07-23 stsp }
2955 c530dc23 2019-07-23 stsp
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;
2959 8069f636 2019-01-12 stsp
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;
2963 c09a553d 2018-03-12 stsp
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;
2967 c09a553d 2018-03-12 stsp
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;
2971 c09a553d 2018-03-12 stsp
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;
2979 49520a32 2018-12-29 stsp }
2980 49520a32 2018-12-29 stsp
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,
2985 84de9106 2020-12-26 stsp NULL);
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);
3000 4b6c9460 2020-03-05 stsp }
3001 8069f636 2019-01-12 stsp goto done;
3002 8069f636 2019-01-12 stsp }
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);
3008 4b6c9460 2020-03-05 stsp }
3009 45d344f6 2019-05-14 stsp goto done;
3010 4b6c9460 2020-03-05 stsp }
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;
3020 08e5873e 2021-09-14 stsp } else {
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;
3026 08e5873e 2021-09-14 stsp }
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;
3030 8069f636 2019-01-12 stsp }
3031 8069f636 2019-01-12 stsp
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;
3042 c09a553d 2018-03-12 stsp
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);
3048 08e5873e 2021-09-14 stsp } else
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();
3054 c09a553d 2018-03-12 stsp done:
3055 08e5873e 2021-09-14 stsp if (head_ref)
3056 08e5873e 2021-09-14 stsp got_ref_close(head_ref);
3057 08e5873e 2021-09-14 stsp if (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;
3066 9627c110 2020-04-18 stsp }
3067 9627c110 2020-04-18 stsp
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;
3077 9627c110 2020-04-18 stsp };
3078 9627c110 2020-04-18 stsp
3079 9627c110 2020-04-18 stsp void
3080 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
3081 9627c110 2020-04-18 stsp {
3082 9627c110 2020-04-18 stsp if (!upa->did_something)
3083 9627c110 2020-04-18 stsp return;
3084 9627c110 2020-04-18 stsp
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);
3093 507dc3bb 2018-12-29 stsp }
3094 507dc3bb 2018-12-29 stsp
3095 0ef68859 2021-09-28 thomas /*
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.
3101 0ef68859 2021-09-28 thomas */
3102 0ef68859 2021-09-28 thomas void
3103 0ef68859 2021-09-28 thomas print_merge_progress_stats(struct got_update_progress_arg *upa)
3104 0ef68859 2021-09-28 thomas {
3105 0ef68859 2021-09-28 thomas if (!upa->did_something)
3106 0ef68859 2021-09-28 thomas return;
3107 0ef68859 2021-09-28 thomas
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);
3122 0ef68859 2021-09-28 thomas }
3123 0ef68859 2021-09-28 thomas
3124 507dc3bb 2018-12-29 stsp __dead static void
3125 507dc3bb 2018-12-29 stsp usage_update(void)
3126 507dc3bb 2018-12-29 stsp {
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());
3130 507dc3bb 2018-12-29 stsp exit(1);
3131 507dc3bb 2018-12-29 stsp }
3132 507dc3bb 2018-12-29 stsp
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)
3135 507dc3bb 2018-12-29 stsp {
3136 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
3137 784955db 2019-01-12 stsp
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;
3141 507dc3bb 2018-12-29 stsp
3142 9627c110 2020-04-18 stsp upa->did_something = 1;
3143 a484d721 2019-06-10 stsp
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;
3147 a484d721 2019-06-10 stsp
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++;
3160 9627c110 2020-04-18 stsp
3161 507dc3bb 2018-12-29 stsp while (path[0] == '/')
3162 507dc3bb 2018-12-29 stsp path++;
3163 4ad4a1ec 2021-09-13 tracey if (upa->verbosity >= 0)
3164 4ad4a1ec 2021-09-13 tracey printf("%c %s\n", status, path);
3165 4ad4a1ec 2021-09-13 tracey
3166 1ee397ad 2019-07-12 stsp return NULL;
3167 be7061eb 2018-12-30 stsp }
3168 be7061eb 2018-12-30 stsp
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)
3173 a1fb16d8 2019-05-24 stsp {
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;
3177 a1fb16d8 2019-05-24 stsp
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);
3185 a1fb16d8 2019-05-24 stsp }
3186 a1fb16d8 2019-05-24 stsp
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;
3193 a1fb16d8 2019-05-24 stsp }
3194 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
3195 a1fb16d8 2019-05-24 stsp return NULL;
3196 a1fb16d8 2019-05-24 stsp
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));
3200 a1fb16d8 2019-05-24 stsp if (err)
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;
3207 0ebf8283 2019-07-24 stsp }
3208 0ebf8283 2019-07-24 stsp
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)
3211 0ebf8283 2019-07-24 stsp {
3212 0ebf8283 2019-07-24 stsp const struct got_error *err;
3213 0ebf8283 2019-07-24 stsp int in_progress;
3214 0ebf8283 2019-07-24 stsp
3215 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
3216 0ebf8283 2019-07-24 stsp if (err)
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);
3220 0ebf8283 2019-07-24 stsp
3221 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
3222 0ebf8283 2019-07-24 stsp if (err)
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);
3226 10604dce 2021-09-24 thomas
3227 10604dce 2021-09-24 thomas return NULL;
3228 10604dce 2021-09-24 thomas }
3229 10604dce 2021-09-24 thomas
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)
3233 10604dce 2021-09-24 thomas {
3234 10604dce 2021-09-24 thomas const struct got_error *err;
3235 10604dce 2021-09-24 thomas int in_progress;
3236 10604dce 2021-09-24 thomas
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);
3242 0ebf8283 2019-07-24 stsp
3243 a1fb16d8 2019-05-24 stsp return NULL;
3244 a5edda0a 2019-07-27 stsp }
3245 a5edda0a 2019-07-27 stsp
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)
3249 a5edda0a 2019-07-27 stsp {
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;
3253 a5edda0a 2019-07-27 stsp int i;
3254 a5edda0a 2019-07-27 stsp
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);
3260 a5edda0a 2019-07-27 stsp }
3261 a5edda0a 2019-07-27 stsp
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]);
3264 a5edda0a 2019-07-27 stsp if (err)
3265 a5edda0a 2019-07-27 stsp break;
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)
3270 f1417e9f 2021-10-12 thomas break;
3271 a5edda0a 2019-07-27 stsp }
3272 a5edda0a 2019-07-27 stsp }
3273 a5edda0a 2019-07-27 stsp
3274 a5edda0a 2019-07-27 stsp return err;
3275 a1fb16d8 2019-05-24 stsp }
3276 a1fb16d8 2019-05-24 stsp
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)
3280 fa51e947 2020-03-27 stsp {
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];
3284 fa51e947 2020-03-27 stsp
3285 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
3286 fa51e947 2020-03-27 stsp if (err)
3287 fa51e947 2020-03-27 stsp return orig_err;
3288 fa51e947 2020-03-27 stsp
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;
3297 fa51e947 2020-03-27 stsp }
3298 fa51e947 2020-03-27 stsp
3299 fa51e947 2020-03-27 stsp static const struct got_error *
3300 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
3301 507dc3bb 2018-12-29 stsp {
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;
3314 507dc3bb 2018-12-29 stsp
3315 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
3316 f2ea84fa 2019-07-27 stsp
3317 4ad4a1ec 2021-09-13 tracey while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3318 507dc3bb 2018-12-29 stsp switch (ch) {
3319 024e9686 2019-05-14 stsp case 'b':
3320 024e9686 2019-05-14 stsp branch_name = optarg;
3321 024e9686 2019-05-14 stsp break;
3322 507dc3bb 2018-12-29 stsp case 'c':
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");
3326 4ad4a1ec 2021-09-13 tracey break;
3327 4ad4a1ec 2021-09-13 tracey case 'q':
3328 4ad4a1ec 2021-09-13 tracey verbosity = -1;
3329 507dc3bb 2018-12-29 stsp break;
3330 507dc3bb 2018-12-29 stsp default:
3331 2deda0b9 2019-03-07 stsp usage_update();
3332 507dc3bb 2018-12-29 stsp /* NOTREACHED */
3333 507dc3bb 2018-12-29 stsp }
3334 507dc3bb 2018-12-29 stsp }
3335 507dc3bb 2018-12-29 stsp
3336 507dc3bb 2018-12-29 stsp argc -= optind;
3337 507dc3bb 2018-12-29 stsp argv += optind;
3338 507dc3bb 2018-12-29 stsp
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");
3343 507dc3bb 2018-12-29 stsp #endif
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;
3348 c4cdcb68 2019-04-03 stsp }
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;
3355 fa51e947 2020-03-27 stsp }
3356 7d5807f4 2019-07-11 stsp
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;
3360 507dc3bb 2018-12-29 stsp
3361 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3362 c9956ddf 2019-09-08 stsp NULL);
3363 507dc3bb 2018-12-29 stsp if (error != NULL)
3364 507dc3bb 2018-12-29 stsp goto done;
3365 507dc3bb 2018-12-29 stsp
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;
3370 087fb88c 2019-08-04 stsp
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;
3374 10604dce 2021-09-24 thomas
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;
3378 0266afb7 2019-01-04 stsp
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;
3390 507dc3bb 2018-12-29 stsp } else {
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,
3394 84de9106 2020-12-26 stsp NULL);
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;
3407 507dc3bb 2018-12-29 stsp }
3408 35c965b2 2018-12-29 stsp
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)
3413 f2ea84fa 2019-07-27 stsp continue;
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;
3418 024e9686 2019-05-14 stsp }
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,
3423 3aef623b 2019-10-15 stsp repo);
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;
3433 024e9686 2019-05-14 stsp } else {
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;
3440 a367ff0f 2019-05-14 stsp }
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;
3444 024e9686 2019-05-14 stsp }
3445 507dc3bb 2018-12-29 stsp
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;
3452 507dc3bb 2018-12-29 stsp }
3453 507dc3bb 2018-12-29 stsp
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;
3460 9c4b8182 2019-01-02 stsp
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);
3464 4f3c844b 2021-09-14 stsp } else
3465 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3466 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3467 507dc3bb 2018-12-29 stsp done:
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;
3475 c09a553d 2018-03-12 stsp }
3476 c09a553d 2018-03-12 stsp
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)
3481 5c860e29 2018-03-12 stsp {
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;
3484 79109fed 2018-03-27 stsp
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);
3487 44392932 2019-08-25 stsp if (err)
3488 44392932 2019-08-25 stsp goto done;
3489 44392932 2019-08-25 stsp }
3490 79109fed 2018-03-27 stsp
3491 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3492 44392932 2019-08-25 stsp if (err)
3493 44392932 2019-08-25 stsp goto done;
3494 79109fed 2018-03-27 stsp
3495 44392932 2019-08-25 stsp while (path[0] == '/')
3496 44392932 2019-08-25 stsp path++;
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);
3499 44392932 2019-08-25 stsp done:
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;
3504 44392932 2019-08-25 stsp }
3505 44392932 2019-08-25 stsp
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)
3510 44392932 2019-08-25 stsp {
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;
3514 44392932 2019-08-25 stsp
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);
3517 79109fed 2018-03-27 stsp if (err)
3518 44392932 2019-08-25 stsp goto done;
3519 79109fed 2018-03-27 stsp }
3520 79109fed 2018-03-27 stsp
3521 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3522 0f2b3dca 2018-12-22 stsp if (err)
3523 0f2b3dca 2018-12-22 stsp goto done;
3524 0f2b3dca 2018-12-22 stsp
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] == '/')
3532 44392932 2019-08-25 stsp path++;
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);
3535 0f2b3dca 2018-12-22 stsp done:
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;
3541 44392932 2019-08-25 stsp }
3542 44392932 2019-08-25 stsp
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)
3546 0208f208 2020-05-05 stsp {
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;
3551 0208f208 2020-05-05 stsp
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,
3556 0208f208 2020-05-05 stsp qid->id);
3557 0208f208 2020-05-05 stsp if (err)
3558 0208f208 2020-05-05 stsp return err;
3559 0208f208 2020-05-05 stsp
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");
3565 aa8b5dd0 2021-08-01 stsp }
3566 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3567 0208f208 2020-05-05 stsp
3568 0208f208 2020-05-05 stsp }
3569 0208f208 2020-05-05 stsp
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);
3572 0208f208 2020-05-05 stsp if (err)
3573 0208f208 2020-05-05 stsp goto done;
3574 0208f208 2020-05-05 stsp }
3575 0208f208 2020-05-05 stsp
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);
3578 0208f208 2020-05-05 stsp if (err)
3579 0208f208 2020-05-05 stsp goto done;
3580 0208f208 2020-05-05 stsp
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);
3583 0208f208 2020-05-05 stsp done:
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;
3590 0208f208 2020-05-05 stsp }
3591 0208f208 2020-05-05 stsp
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)
3595 44392932 2019-08-25 stsp {
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;
3601 44392932 2019-08-25 stsp
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,
3605 44392932 2019-08-25 stsp qid->id);
3606 44392932 2019-08-25 stsp if (err)
3607 44392932 2019-08-25 stsp return err;
3608 44392932 2019-08-25 stsp }
3609 44392932 2019-08-25 stsp
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);
3613 44392932 2019-08-25 stsp if (err)
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;
3619 44392932 2019-08-25 stsp }
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;
3627 2e8c69d1 2020-05-04 stsp }
3628 2e8c69d1 2020-05-04 stsp } else {
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;
3633 2e8c69d1 2020-05-04 stsp }
3634 44392932 2019-08-25 stsp }
3635 44392932 2019-08-25 stsp }
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;
3640 44392932 2019-08-25 stsp }
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);
3646 44392932 2019-08-25 stsp break;
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);
3650 44392932 2019-08-25 stsp break;
3651 44392932 2019-08-25 stsp default:
3652 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3653 44392932 2019-08-25 stsp break;
3654 44392932 2019-08-25 stsp }
3655 44392932 2019-08-25 stsp free(obj_id1);
3656 44392932 2019-08-25 stsp free(obj_id2);
3657 44392932 2019-08-25 stsp } else {
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);
3660 44392932 2019-08-25 stsp if (err)
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);
3665 579bd556 2020-10-24 stsp if (err)
3666 579bd556 2020-10-24 stsp goto done;
3667 579bd556 2020-10-24 stsp }
3668 64453f7e 2020-11-21 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3669 64453f7e 2020-11-21 stsp id_str2);
3670 64453f7e 2020-11-21 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3671 64453f7e 2020-11-21 stsp repo);
3672 44392932 2019-08-25 stsp }
3673 44392932 2019-08-25 stsp done:
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;
3679 79109fed 2018-03-27 stsp }
3680 79109fed 2018-03-27 stsp
3681 4bb494d5 2018-06-16 stsp static char *
3682 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3683 6c281f94 2018-06-11 stsp {
3684 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3685 09867e48 2019-08-13 stsp char *p, *s;
3686 09867e48 2019-08-13 stsp
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');
3694 6c281f94 2018-06-11 stsp if (p)
3695 6c281f94 2018-06-11 stsp *p = '\0';
3696 6c281f94 2018-06-11 stsp return s;
3697 6c281f94 2018-06-11 stsp }
3698 dc424a06 2019-08-07 stsp
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)
3702 6841bf13 2019-11-29 kn {
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;
3706 6841bf13 2019-11-29 kn
3707 6841bf13 2019-11-29 kn *have_match = 0;
3708 6841bf13 2019-11-29 kn
3709 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3710 6841bf13 2019-11-29 kn if (err)
3711 6841bf13 2019-11-29 kn return err;
3712 6841bf13 2019-11-29 kn
3713 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3714 6841bf13 2019-11-29 kn if (err)
3715 6841bf13 2019-11-29 kn goto done;
3716 6841bf13 2019-11-29 kn
3717 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3718 6841bf13 2019-11-29 kn *have_match = 1;
3719 6841bf13 2019-11-29 kn done:
3720 6841bf13 2019-11-29 kn free(id_str);
3721 6841bf13 2019-11-29 kn free(logmsg);
3722 6841bf13 2019-11-29 kn return err;
3723 6841bf13 2019-11-29 kn }
3724 6841bf13 2019-11-29 kn
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)
3728 0208f208 2020-05-05 stsp {
3729 0208f208 2020-05-05 stsp regmatch_t regmatch;
3730 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3731 0208f208 2020-05-05 stsp
3732 0208f208 2020-05-05 stsp *have_match = 0;
3733 0208f208 2020-05-05 stsp
3734 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3735 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3736 0208f208 2020-05-05 stsp *have_match = 1;
3737 0208f208 2020-05-05 stsp break;
3738 0208f208 2020-05-05 stsp }
3739 0208f208 2020-05-05 stsp }
3740 0208f208 2020-05-05 stsp }
3741 0208f208 2020-05-05 stsp
3742 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3743 6c281f94 2018-06-11 stsp
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)
3747 888b7d99 2020-12-26 stsp {
3748 888b7d99 2020-12-26 stsp static const struct got_error *err = NULL;
3749 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3750 888b7d99 2020-12-26 stsp char *s;
3751 888b7d99 2020-12-26 stsp const char *name;
3752 5c860e29 2018-03-12 stsp
3753 888b7d99 2020-12-26 stsp *refs_str = NULL;
3754 888b7d99 2020-12-26 stsp
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;
3758 a436ad14 2019-08-13 stsp int cmp;
3759 a436ad14 2019-08-13 stsp
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)
3762 d9498b20 2019-02-05 stsp continue;
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)
3766 7143d404 2019-03-12 stsp continue;
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')
3773 4343a07f 2020-04-24 stsp continue;
3774 4343a07f 2020-04-24 stsp }
3775 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3776 48cae60d 2020-09-22 stsp if (err)
3777 888b7d99 2020-12-26 stsp break;
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);
3783 888b7d99 2020-12-26 stsp break;
3784 48cae60d 2020-09-22 stsp }
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;
3788 5d844a1e 2019-08-13 stsp }
3789 a436ad14 2019-08-13 stsp }
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);
3793 a436ad14 2019-08-13 stsp if (tag)
3794 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3795 a436ad14 2019-08-13 stsp if (cmp != 0)
3796 a436ad14 2019-08-13 stsp continue;
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");
3801 199a4027 2019-02-02 stsp free(s);
3802 888b7d99 2020-12-26 stsp *refs_str = NULL;
3803 888b7d99 2020-12-26 stsp break;
3804 199a4027 2019-02-02 stsp }
3805 199a4027 2019-02-02 stsp free(s);
3806 199a4027 2019-02-02 stsp }
3807 888b7d99 2020-12-26 stsp
3808 888b7d99 2020-12-26 stsp return err;
3809 888b7d99 2020-12-26 stsp }
3810 888b7d99 2020-12-26 stsp
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)
3817 888b7d99 2020-12-26 stsp {
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;
3824 888b7d99 2020-12-26 stsp
3825 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3826 8bf5b3c9 2018-03-17 stsp if (err)
3827 8bf5b3c9 2018-03-17 stsp return err;
3828 788c352e 2018-06-16 stsp
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);
3834 e600f124 2021-03-21 stsp if (err)
3835 e600f124 2021-03-21 stsp goto done;
3836 e600f124 2021-03-21 stsp }
3837 888b7d99 2020-12-26 stsp }
3838 888b7d99 2020-12-26 stsp
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);
3842 e600f124 2021-03-21 stsp else
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);
3865 a0603db2 2018-06-10 stsp if (err)
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;
3870 a0603db2 2018-06-10 stsp }
3871 a0603db2 2018-06-10 stsp }
3872 832c249c 2018-06-10 stsp
3873 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3874 5943eee2 2019-08-13 stsp if (err)
3875 888b7d99 2020-12-26 stsp goto done;
3876 8bf5b3c9 2018-03-17 stsp
3877 621015ac 2018-07-23 stsp logmsg = logmsg0;
3878 832c249c 2018-06-10 stsp do {
3879 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3880 832c249c 2018-06-10 stsp if (line)
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);
3884 832c249c 2018-06-10 stsp
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);
3890 0208f208 2020-05-05 stsp }
3891 0208f208 2020-05-05 stsp printf("\n");
3892 0208f208 2020-05-05 stsp }
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");
3897 971751ac 2018-03-27 stsp }
3898 07862c20 2018-09-15 stsp
3899 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3900 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3901 888b7d99 2020-12-26 stsp done:
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;
3905 f42b1b34 2018-03-12 stsp }
3906 5c860e29 2018-03-12 stsp
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)
3913 f42b1b34 2018-03-12 stsp {
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;
3923 dbec59df 2020-04-18 stsp
3924 dbdddfee 2021-06-23 naddy STAILQ_INIT(&reversed_commits);
3925 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3926 372ccdbb 2018-06-10 stsp
3927 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(&regex, 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);
3930 6841bf13 2019-11-29 kn
3931 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3932 f42b1b34 2018-03-12 stsp if (err)
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);
3936 372ccdbb 2018-06-10 stsp if (err)
3937 fcc85cad 2018-07-22 stsp goto done;
3938 656b1f76 2019-05-11 jcs for (;;) {
3939 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3940 8bf5b3c9 2018-03-17 stsp
3941 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3942 84453469 2018-11-11 stsp break;
3943 84453469 2018-11-11 stsp
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;
3949 ee780d5c 2020-01-04 stsp break;
3950 7e665116 2018-04-02 stsp }
3951 b43fbaa0 2018-06-11 stsp if (id == NULL)
3952 7e665116 2018-04-02 stsp break;
3953 b43fbaa0 2018-06-11 stsp
3954 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3955 b43fbaa0 2018-06-11 stsp if (err)
3956 fcc85cad 2018-07-22 stsp break;
3957 6841bf13 2019-11-29 kn
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);
3960 0208f208 2020-05-05 stsp if (err)
3961 0208f208 2020-05-05 stsp break;
3962 0208f208 2020-05-05 stsp }
3963 0208f208 2020-05-05 stsp
3964 6841bf13 2019-11-29 kn if (search_pattern) {
3965 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3966 6841bf13 2019-11-29 kn if (err) {
3967 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3968 6841bf13 2019-11-29 kn break;
3969 6841bf13 2019-11-29 kn }
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, &regex);
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);
3978 0208f208 2020-05-05 stsp }
3979 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3980 6841bf13 2019-11-29 kn continue;
3981 6841bf13 2019-11-29 kn }
3982 6841bf13 2019-11-29 kn }
3983 6841bf13 2019-11-29 kn
3984 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3985 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3986 dbec59df 2020-04-18 stsp if (err)
3987 dbec59df 2020-04-18 stsp break;
3988 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3989 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3990 dbec59df 2020-04-18 stsp } else {
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);
3995 dbec59df 2020-04-18 stsp if (err)
3996 dbec59df 2020-04-18 stsp break;
3997 dbec59df 2020-04-18 stsp }
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))
4000 7e665116 2018-04-02 stsp break;
4001 0208f208 2020-05-05 stsp
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);
4005 0208f208 2020-05-05 stsp }
4006 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4007 31cedeaf 2018-09-15 stsp }
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);
4011 dbec59df 2020-04-18 stsp if (err)
4012 dbec59df 2020-04-18 stsp break;
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);
4016 502b9684 2020-07-31 stsp if (err)
4017 502b9684 2020-07-31 stsp break;
4018 502b9684 2020-07-31 stsp }
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);
4023 dbec59df 2020-04-18 stsp if (err)
4024 dbec59df 2020-04-18 stsp break;
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);
4028 502b9684 2020-07-31 stsp }
4029 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
4030 dbec59df 2020-04-18 stsp }
4031 dbec59df 2020-04-18 stsp }
4032 fcc85cad 2018-07-22 stsp done:
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);
4037 dbec59df 2020-04-18 stsp }
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);
4041 0208f208 2020-05-05 stsp }
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(&regex);
4045 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
4046 f42b1b34 2018-03-12 stsp return err;
4047 f42b1b34 2018-03-12 stsp }
4048 5c860e29 2018-03-12 stsp
4049 4ed7e80c 2018-05-20 stsp __dead static void
4050 6f3d1eb0 2018-03-12 stsp usage_log(void)
4051 6f3d1eb0 2018-03-12 stsp {
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());
4055 6f3d1eb0 2018-03-12 stsp exit(1);
4056 b1ebc001 2019-08-13 stsp }
4057 b1ebc001 2019-08-13 stsp
4058 b1ebc001 2019-08-13 stsp static int
4059 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
4060 b1ebc001 2019-08-13 stsp {
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;
4064 b1ebc001 2019-08-13 stsp
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)
4067 b1ebc001 2019-08-13 stsp return 0;
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)
4070 b1ebc001 2019-08-13 stsp return 0;
4071 b1ebc001 2019-08-13 stsp return n;
4072 6f3d1eb0 2018-03-12 stsp }
4073 6f3d1eb0 2018-03-12 stsp
4074 4ed7e80c 2018-05-20 stsp static const struct got_error *
4075 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
4076 f42b1b34 2018-03-12 stsp {
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;
4090 1b3893a2 2019-03-18 stsp
4091 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4092 5c860e29 2018-03-12 stsp
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",
4095 6098196c 2019-01-04 stsp NULL)
4096 ad242220 2018-09-08 stsp == -1)
4097 f42b1b34 2018-03-12 stsp err(1, "pledge");
4098 6715a751 2018-03-16 stsp #endif
4099 79109fed 2018-03-27 stsp
4100 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
4101 b1ebc001 2019-08-13 stsp
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) {
4104 79109fed 2018-03-27 stsp case 'p':
4105 79109fed 2018-03-27 stsp show_patch = 1;
4106 d142fc45 2018-04-01 stsp break;
4107 0208f208 2020-05-05 stsp case 'P':
4108 0208f208 2020-05-05 stsp show_changed_paths = 1;
4109 0208f208 2020-05-05 stsp break;
4110 d142fc45 2018-04-01 stsp case 'c':
4111 d142fc45 2018-04-01 stsp start_commit = optarg;
4112 64a96a6d 2018-04-01 stsp break;
4113 c0cc5c62 2018-10-18 stsp case 'C':
4114 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4115 4a8520aa 2018-10-18 stsp &errstr);
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);
4119 c0cc5c62 2018-10-18 stsp break;
4120 64a96a6d 2018-04-01 stsp case 'l':
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);
4125 cc54c501 2019-07-15 stsp break;
4126 48c8c60d 2020-01-27 stsp case 'b':
4127 48c8c60d 2020-01-27 stsp log_branches = 1;
4128 a0603db2 2018-06-10 stsp break;
4129 04ca23f4 2018-07-16 stsp case 'r':
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",
4133 9ba1d308 2019-10-21 stsp optarg);
4134 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4135 04ca23f4 2018-07-16 stsp break;
4136 dbec59df 2020-04-18 stsp case 'R':
4137 dbec59df 2020-04-18 stsp reverse_display_order = 1;
4138 dbec59df 2020-04-18 stsp break;
4139 6841bf13 2019-11-29 kn case 's':
4140 6841bf13 2019-11-29 kn search_pattern = optarg;
4141 6841bf13 2019-11-29 kn break;
4142 d1fe46f9 2020-04-18 stsp case 'x':
4143 d1fe46f9 2020-04-18 stsp end_commit = optarg;
4144 d1fe46f9 2020-04-18 stsp break;
4145 79109fed 2018-03-27 stsp default:
4146 2deda0b9 2019-03-07 stsp usage_log();
4147 79109fed 2018-03-27 stsp /* NOTREACHED */
4148 79109fed 2018-03-27 stsp }
4149 79109fed 2018-03-27 stsp }
4150 79109fed 2018-03-27 stsp
4151 79109fed 2018-03-27 stsp argc -= optind;
4152 79109fed 2018-03-27 stsp argv += optind;
4153 f42b1b34 2018-03-12 stsp
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");
4158 dc1edbfa 2019-11-29 kn
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;
4163 04ca23f4 2018-07-16 stsp }
4164 cffc0aa4 2019-02-05 stsp
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;
4170 50f2fada 2020-04-24 stsp }
4171 cffc0aa4 2019-02-05 stsp
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,
4175 e7301579 2019-03-18 stsp argv[0]);
4176 e7301579 2019-03-18 stsp if (error)
4177 e7301579 2019-03-18 stsp goto done;
4178 e7301579 2019-03-18 stsp } else {
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;
4183 e7301579 2019-03-18 stsp }
4184 e7301579 2019-03-18 stsp }
4185 603cdeb0 2020-10-22 stsp } else if (argc != 0)
4186 cbd1af7a 2019-03-18 stsp usage_log();
4187 cbd1af7a 2019-03-18 stsp
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);
4191 5486daa2 2019-05-11 stsp }
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;
4195 04ca23f4 2018-07-16 stsp }
4196 6098196c 2019-01-04 stsp
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;
4200 f42b1b34 2018-03-12 stsp
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;
4205 c02c541e 2019-03-29 stsp
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;
4209 84de9106 2020-12-26 stsp
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;
4213 888b7d99 2020-12-26 stsp
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);
4231 3235492e 2018-04-01 stsp } else {
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;
4236 3235492e 2018-04-01 stsp }
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;
4242 d1fe46f9 2020-04-18 stsp }
4243 04ca23f4 2018-07-16 stsp
4244 deeabeae 2019-08-27 stsp if (worktree) {
4245 603cdeb0 2020-10-22 stsp /*
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.
4249 603cdeb0 2020-10-22 stsp */
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;
4257 603cdeb0 2020-10-22 stsp }
4258 603cdeb0 2020-10-22 stsp }
4259 deeabeae 2019-08-27 stsp } else
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;
4267 04ca23f4 2018-07-16 stsp }
4268 199a4027 2019-02-02 stsp
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;
4273 f89c34e1 2022-03-13 thomas }
4274 f89c34e1 2022-03-13 thomas
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);
4278 04ca23f4 2018-07-16 stsp done:
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;
4288 ad242220 2018-09-08 stsp }
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;
4293 5c860e29 2018-03-12 stsp }
4294 5c860e29 2018-03-12 stsp
4295 4ed7e80c 2018-05-20 stsp __dead static void
4296 3f8b7d6a 2018-04-01 stsp usage_diff(void)
4297 3f8b7d6a 2018-04-01 stsp {
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());
4301 3f8b7d6a 2018-04-01 stsp exit(1);
4302 b00d56cd 2018-04-01 stsp }
4303 b00d56cd 2018-04-01 stsp
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;
4313 b72f483a 2019-02-05 stsp };
4314 39449a05 2020-07-23 stsp
4315 39449a05 2020-07-23 stsp /*
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.
4318 39449a05 2020-07-23 stsp */
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)
4322 39449a05 2020-07-23 stsp {
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;
4326 39449a05 2020-07-23 stsp
4327 39449a05 2020-07-23 stsp *fd = -1;
4328 39449a05 2020-07-23 stsp
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);
4333 39449a05 2020-07-23 stsp } else {
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);
4337 39449a05 2020-07-23 stsp }
4338 39449a05 2020-07-23 stsp
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");
4342 39449a05 2020-07-23 stsp
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;
4347 39449a05 2020-07-23 stsp }
4348 39449a05 2020-07-23 stsp
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;
4352 39449a05 2020-07-23 stsp }
4353 39449a05 2020-07-23 stsp done:
4354 39449a05 2020-07-23 stsp if (err) {
4355 39449a05 2020-07-23 stsp close(*fd);
4356 39449a05 2020-07-23 stsp *fd = -1;
4357 39449a05 2020-07-23 stsp }
4358 39449a05 2020-07-23 stsp return err;
4359 39449a05 2020-07-23 stsp }
4360 b72f483a 2019-02-05 stsp
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)
4366 b72f483a 2019-02-05 stsp {
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;
4374 b72f483a 2019-02-05 stsp
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;
4380 98eaaa12 2019-08-03 stsp } else {
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;
4390 98eaaa12 2019-08-03 stsp }
4391 3fc0c068 2019-02-10 stsp
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;
4397 3fc0c068 2019-02-10 stsp }
4398 b72f483a 2019-02-05 stsp
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;
4405 98eaaa12 2019-08-03 stsp break;
4406 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
4407 98eaaa12 2019-08-03 stsp label2 = path;
4408 98eaaa12 2019-08-03 stsp break;
4409 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
4410 98eaaa12 2019-08-03 stsp label1 = path;
4411 98eaaa12 2019-08-03 stsp break;
4412 98eaaa12 2019-08-03 stsp default:
4413 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
4414 98eaaa12 2019-08-03 stsp }
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);
4418 98eaaa12 2019-08-03 stsp }
4419 98eaaa12 2019-08-03 stsp
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,
4424 408b4ebc 2019-08-03 stsp 8192);
4425 4ce46740 2019-08-08 stsp if (err)
4426 4ce46740 2019-08-08 stsp goto done;
4427 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4428 4ce46740 2019-08-08 stsp if (err)
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;
4434 4ce46740 2019-08-08 stsp }
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);
4438 4ce46740 2019-08-08 stsp if (err)
4439 4ce46740 2019-08-08 stsp goto done;
4440 4ce46740 2019-08-08 stsp }
4441 d00136be 2019-03-26 stsp
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;
4447 2ec1f75b 2019-03-26 stsp }
4448 b72f483a 2019-02-05 stsp
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",
4455 39449a05 2020-07-23 stsp abspath);
4456 39449a05 2020-07-23 stsp goto done;
4457 39449a05 2020-07-23 stsp }
4458 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4459 39449a05 2020-07-23 stsp de_name, abspath);
4460 39449a05 2020-07-23 stsp if (err)
4461 39449a05 2020-07-23 stsp goto done;
4462 12463d8b 2019-12-13 stsp }
4463 12463d8b 2019-12-13 stsp } else {
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",
4468 39449a05 2020-07-23 stsp abspath);
4469 39449a05 2020-07-23 stsp goto done;
4470 39449a05 2020-07-23 stsp }
4471 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4472 39449a05 2020-07-23 stsp de_name, abspath);
4473 39449a05 2020-07-23 stsp if (err)
4474 39449a05 2020-07-23 stsp goto done;
4475 12463d8b 2019-12-13 stsp }
4476 12463d8b 2019-12-13 stsp }
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;
4480 2ec1f75b 2019-03-26 stsp }
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;
4485 2ec1f75b 2019-03-26 stsp }
4486 12463d8b 2019-12-13 stsp fd = -1;
4487 2ec1f75b 2019-03-26 stsp } else
4488 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4489 b72f483a 2019-02-05 stsp
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);
4492 b72f483a 2019-02-05 stsp done:
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;
4501 927df6b7 2019-02-10 stsp }
4502 927df6b7 2019-02-10 stsp
4503 927df6b7 2019-02-10 stsp static const struct got_error *
4504 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4505 b00d56cd 2018-04-01 stsp {
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;
4521 b00d56cd 2018-04-01 stsp
4522 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4523 30f6c0c6 2021-10-08 thomas TAILQ_INIT(&paths);
4524 84de9106 2020-12-26 stsp
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");
4529 b00d56cd 2018-04-01 stsp #endif
4530 b00d56cd 2018-04-01 stsp
4531 cc8021af 2021-10-12 thomas while ((ch = getopt(argc, argv, "ac:C:r:swP")) != -1) {
4532 b00d56cd 2018-04-01 stsp switch (ch) {
4533 64453f7e 2020-11-21 stsp case 'a':
4534 64453f7e 2020-11-21 stsp force_text_diff = 1;
4535 64453f7e 2020-11-21 stsp break;
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;
4540 cc8021af 2021-10-12 thomas break;
4541 c0cc5c62 2018-10-18 stsp case 'C':
4542 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4543 dfcab68b 2019-11-29 kn &errstr);
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);
4547 c0cc5c62 2018-10-18 stsp break;
4548 b72f483a 2019-02-05 stsp case 'r':
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",
4552 9ba1d308 2019-10-21 stsp optarg);
4553 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4554 30f6c0c6 2021-10-08 thomas rflag = 1;
4555 b72f483a 2019-02-05 stsp break;
4556 98eaaa12 2019-08-03 stsp case 's':
4557 98eaaa12 2019-08-03 stsp diff_staged = 1;
4558 63035f9f 2019-10-06 stsp break;
4559 63035f9f 2019-10-06 stsp case 'w':
4560 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4561 98eaaa12 2019-08-03 stsp break;
4562 30f6c0c6 2021-10-08 thomas case 'P':
4563 30f6c0c6 2021-10-08 thomas force_path = 1;
4564 30f6c0c6 2021-10-08 thomas break;
4565 b00d56cd 2018-04-01 stsp default:
4566 2deda0b9 2019-03-07 stsp usage_diff();
4567 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4568 b00d56cd 2018-04-01 stsp }
4569 b00d56cd 2018-04-01 stsp }
4570 b00d56cd 2018-04-01 stsp
4571 b00d56cd 2018-04-01 stsp argc -= optind;
4572 b00d56cd 2018-04-01 stsp argv += optind;
4573 b00d56cd 2018-04-01 stsp
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;
4578 b72f483a 2019-02-05 stsp }
4579 30f6c0c6 2021-10-08 thomas
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;
4584 30f6c0c6 2021-10-08 thomas else
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;
4592 30f6c0c6 2021-10-08 thomas }
4593 927df6b7 2019-02-10 stsp } else {
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");
4597 1a1242a9 2021-04-01 kn goto done;
4598 30db809c 2019-06-05 stsp }
4599 30db809c 2019-06-05 stsp }
4600 30f6c0c6 2021-10-08 thomas }
4601 b00d56cd 2018-04-01 stsp
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;
4606 cc8021af 2021-10-12 thomas
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;
4613 cc8021af 2021-10-12 thomas }
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;
4619 cc8021af 2021-10-12 thomas }
4620 cc8021af 2021-10-12 thomas }
4621 b00d56cd 2018-04-01 stsp
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;
4626 c02c541e 2019-03-29 stsp
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,
4631 30f6c0c6 2021-10-08 thomas NULL);
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];
4638 cc8021af 2021-10-12 thomas else
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;
4649 30f6c0c6 2021-10-08 thomas break;
4650 30f6c0c6 2021-10-08 thomas }
4651 30f6c0c6 2021-10-08 thomas }
4652 30f6c0c6 2021-10-08 thomas }
4653 30f6c0c6 2021-10-08 thomas
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;
4657 72ea6654 2019-07-27 stsp
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;
4672 cc8021af 2021-10-12 thomas }
4673 cc8021af 2021-10-12 thomas }
4674 cc8021af 2021-10-12 thomas
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;
4679 cc8021af 2021-10-12 thomas
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;
4692 b72f483a 2019-02-05 stsp
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;
4697 b72f483a 2019-02-05 stsp }
4698 84de9106 2020-12-26 stsp
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;
4704 b00d56cd 2018-04-01 stsp
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;
4718 cc8021af 2021-10-12 thomas }
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;
4723 cc8021af 2021-10-12 thomas }
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;
4731 cc8021af 2021-10-12 thomas }
4732 cc8021af 2021-10-12 thomas }
4733 cc8021af 2021-10-12 thomas
4734 cc8021af 2021-10-12 thomas got_object_commit_close(commit);
4735 cc8021af 2021-10-12 thomas }
4736 cc8021af 2021-10-12 thomas
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;
4741 cc8021af 2021-10-12 thomas }
4742 b00d56cd 2018-04-01 stsp
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;
4747 cc8021af 2021-10-12 thomas }
4748 cc8021af 2021-10-12 thomas
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;
4755 b00d56cd 2018-04-01 stsp }
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;
4760 cc8021af 2021-10-12 thomas }
4761 b00d56cd 2018-04-01 stsp
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;
4781 cc8021af 2021-10-12 thomas }
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] == '/')
4790 cc8021af 2021-10-12 thomas s++;
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;
4796 cc8021af 2021-10-12 thomas }
4797 cc8021af 2021-10-12 thomas free(mapped_path);
4798 cc8021af 2021-10-12 thomas
4799 cc8021af 2021-10-12 thomas }
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;
4805 f89c34e1 2022-03-13 thomas }
4806 f89c34e1 2022-03-13 thomas
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;
4811 cc8021af 2021-10-12 thomas }
4812 cc8021af 2021-10-12 thomas
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);
4818 b00d56cd 2018-04-01 stsp break;
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);
4823 b00d56cd 2018-04-01 stsp break;
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);
4829 b00d56cd 2018-04-01 stsp break;
4830 b00d56cd 2018-04-01 stsp default:
4831 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4832 b00d56cd 2018-04-01 stsp }
4833 b00d56cd 2018-04-01 stsp done:
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;
4844 ad242220 2018-09-08 stsp }
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;
4850 404c43c4 2018-06-21 stsp }
4851 404c43c4 2018-06-21 stsp
4852 404c43c4 2018-06-21 stsp __dead static void
4853 404c43c4 2018-06-21 stsp usage_blame(void)
4854 404c43c4 2018-06-21 stsp {
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());
4858 404c43c4 2018-06-21 stsp exit(1);
4859 b00d56cd 2018-04-01 stsp }
4860 b00d56cd 2018-04-01 stsp
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 */
4866 28315671 2019-08-14 stsp };
4867 28315671 2019-08-14 stsp
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;
4874 28315671 2019-08-14 stsp FILE *f;
4875 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4876 28315671 2019-08-14 stsp };
4877 28315671 2019-08-14 stsp
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)
4880 28315671 2019-08-14 stsp {
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;
4890 28315671 2019-08-14 stsp
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);
4894 28315671 2019-08-14 stsp
4895 28315671 2019-08-14 stsp if (sigint_received)
4896 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4897 28315671 2019-08-14 stsp
4898 2839f8b9 2019-08-18 stsp if (lineno == -1)
4899 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4900 2839f8b9 2019-08-18 stsp
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);
4906 28315671 2019-08-14 stsp if (err)
4907 28315671 2019-08-14 stsp return err;
4908 82f6abb8 2019-08-14 stsp
4909 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4910 82f6abb8 2019-08-14 stsp if (err)
4911 82f6abb8 2019-08-14 stsp goto done;
4912 82f6abb8 2019-08-14 stsp
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;
4917 bcb49d15 2019-08-14 stsp }
4918 bcb49d15 2019-08-14 stsp
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;
4926 82f6abb8 2019-08-14 stsp }
4927 28315671 2019-08-14 stsp bline->annotated = 1;
4928 28315671 2019-08-14 stsp
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;
4933 28315671 2019-08-14 stsp
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;
4938 82f6abb8 2019-08-14 stsp }
4939 28315671 2019-08-14 stsp
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;
4943 82f6abb8 2019-08-14 stsp
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");
4947 28315671 2019-08-14 stsp break;
4948 28315671 2019-08-14 stsp }
4949 82f6abb8 2019-08-14 stsp
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, '@');
4955 82f6abb8 2019-08-14 stsp if (at)
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';
4960 28315671 2019-08-14 stsp
4961 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4962 28315671 2019-08-14 stsp if (nl)
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);
4966 28315671 2019-08-14 stsp
4967 28315671 2019-08-14 stsp a->lineno_cur++;
4968 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4969 28315671 2019-08-14 stsp }
4970 82f6abb8 2019-08-14 stsp done:
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;
4975 28315671 2019-08-14 stsp }
4976 28315671 2019-08-14 stsp
4977 28315671 2019-08-14 stsp static const struct got_error *
4978 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4979 404c43c4 2018-06-21 stsp {
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;
4992 90f3c347 2019-08-19 stsp
4993 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4994 404c43c4 2018-06-21 stsp
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");
4999 404c43c4 2018-06-21 stsp #endif
5000 404c43c4 2018-06-21 stsp
5001 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5002 404c43c4 2018-06-21 stsp switch (ch) {
5003 404c43c4 2018-06-21 stsp case 'c':
5004 404c43c4 2018-06-21 stsp commit_id_str = optarg;
5005 404c43c4 2018-06-21 stsp break;
5006 66bea077 2018-08-02 stsp case 'r':
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",
5010 9ba1d308 2019-10-21 stsp optarg);
5011 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5012 66bea077 2018-08-02 stsp break;
5013 404c43c4 2018-06-21 stsp default:
5014 2deda0b9 2019-03-07 stsp usage_blame();
5015 404c43c4 2018-06-21 stsp /* NOTREACHED */
5016 404c43c4 2018-06-21 stsp }
5017 404c43c4 2018-06-21 stsp }
5018 404c43c4 2018-06-21 stsp
5019 404c43c4 2018-06-21 stsp argc -= optind;
5020 404c43c4 2018-06-21 stsp argv += optind;
5021 404c43c4 2018-06-21 stsp
5022 a39318fd 2018-08-02 stsp if (argc == 1)
5023 404c43c4 2018-06-21 stsp path = argv[0];
5024 a39318fd 2018-08-02 stsp else
5025 404c43c4 2018-06-21 stsp usage_blame();
5026 404c43c4 2018-06-21 stsp
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;
5031 66bea077 2018-08-02 stsp }
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;
5036 0c06baac 2019-02-05 stsp else
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;
5045 1f03b8da 2020-03-20 stsp }
5046 0c06baac 2019-02-05 stsp } else {
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;
5051 0c06baac 2019-02-05 stsp }
5052 66bea077 2018-08-02 stsp }
5053 66bea077 2018-08-02 stsp }
5054 36e2fb66 2019-01-04 stsp
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;
5058 404c43c4 2018-06-21 stsp
5059 0c06baac 2019-02-05 stsp if (worktree) {
5060 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
5061 01740607 2020-11-04 stsp char *p;
5062 01740607 2020-11-04 stsp
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");
5070 01740607 2020-11-04 stsp free(p);
5071 0c06baac 2019-02-05 stsp goto done;
5072 0c06baac 2019-02-05 stsp }
5073 0c06baac 2019-02-05 stsp free(p);
5074 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5075 0c06baac 2019-02-05 stsp } else {
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);
5080 0c06baac 2019-02-05 stsp }
5081 0c06baac 2019-02-05 stsp if (error)
5082 66bea077 2018-08-02 stsp goto done;
5083 66bea077 2018-08-02 stsp
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;
5094 404c43c4 2018-06-21 stsp } else {
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,
5098 84de9106 2020-12-26 stsp NULL);
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;
5106 f89c34e1 2022-03-13 thomas }
5107 f89c34e1 2022-03-13 thomas
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;
5112 404c43c4 2018-06-21 stsp }
5113 404c43c4 2018-06-21 stsp
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;
5118 0587e10c 2020-07-23 stsp
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;
5123 28315671 2019-08-14 stsp
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;
5127 28315671 2019-08-14 stsp
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;
5132 28315671 2019-08-14 stsp }
5133 28315671 2019-08-14 stsp
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;
5141 28315671 2019-08-14 stsp }
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;
5146 28315671 2019-08-14 stsp
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--;
5150 b02560ec 2019-08-19 stsp
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;
5155 28315671 2019-08-14 stsp }
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) {
5160 7ef28ff8 2019-08-14 stsp i /= 10;
5161 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
5162 7ef28ff8 2019-08-14 stsp }
5163 82f6abb8 2019-08-14 stsp bca.repo = repo;
5164 7ef28ff8 2019-08-14 stsp
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);
5167 404c43c4 2018-06-21 stsp done:
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);
5174 28315671 2019-08-14 stsp if (blob)
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;
5182 ad242220 2018-09-08 stsp }
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);
5188 3affba96 2019-09-22 stsp }
5189 3affba96 2019-09-22 stsp free(bca.lines);
5190 28315671 2019-08-14 stsp }
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;
5195 5de5890b 2018-10-18 stsp }
5196 5de5890b 2018-10-18 stsp
5197 5de5890b 2018-10-18 stsp __dead static void
5198 5de5890b 2018-10-18 stsp usage_tree(void)
5199 5de5890b 2018-10-18 stsp {
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());
5203 5de5890b 2018-10-18 stsp exit(1);
5204 5de5890b 2018-10-18 stsp }
5205 5de5890b 2018-10-18 stsp
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)
5209 c1669e2e 2019-01-09 stsp {
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;
5215 5de5890b 2018-10-18 stsp
5216 c1669e2e 2019-01-09 stsp path += strlen(root_path);
5217 c1669e2e 2019-01-09 stsp while (path[0] == '/')
5218 c1669e2e 2019-01-09 stsp path++;
5219 c1669e2e 2019-01-09 stsp
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)) {
5223 0d6c6ee3 2020-05-20 stsp int i;
5224 0d6c6ee3 2020-05-20 stsp
5225 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5226 0d6c6ee3 2020-05-20 stsp if (err)
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] = '?';
5231 0d6c6ee3 2020-05-20 stsp }
5232 0d6c6ee3 2020-05-20 stsp
5233 848d6979 2019-08-12 stsp modestr = "@";
5234 0d6c6ee3 2020-05-20 stsp }
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 = "*";
5239 848d6979 2019-08-12 stsp
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 : "");
5243 0d6c6ee3 2020-05-20 stsp
5244 0d6c6ee3 2020-05-20 stsp free(link_target);
5245 0d6c6ee3 2020-05-20 stsp return NULL;
5246 c1669e2e 2019-01-09 stsp }
5247 c1669e2e 2019-01-09 stsp
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)
5252 5de5890b 2018-10-18 stsp {
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;
5257 5de5890b 2018-10-18 stsp
5258 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
5259 5de5890b 2018-10-18 stsp if (err)
5260 5de5890b 2018-10-18 stsp goto done;
5261 5de5890b 2018-10-18 stsp
5262 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
5263 5de5890b 2018-10-18 stsp if (err)
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;
5269 84453469 2018-11-11 stsp
5270 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
5271 84453469 2018-11-11 stsp break;
5272 84453469 2018-11-11 stsp
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));
5278 5de5890b 2018-10-18 stsp if (err)
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;
5284 5de5890b 2018-10-18 stsp }
5285 5de5890b 2018-10-18 stsp free(id_str);
5286 5de5890b 2018-10-18 stsp }
5287 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
5288 5de5890b 2018-10-18 stsp free(id);
5289 0d6c6ee3 2020-05-20 stsp if (err)
5290 0d6c6ee3 2020-05-20 stsp goto done;
5291 c1669e2e 2019-01-09 stsp
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;
5299 c1669e2e 2019-01-09 stsp }
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);
5303 c1669e2e 2019-01-09 stsp if (err)
5304 c1669e2e 2019-01-09 stsp goto done;
5305 c1669e2e 2019-01-09 stsp }
5306 5de5890b 2018-10-18 stsp }
5307 5de5890b 2018-10-18 stsp done:
5308 5de5890b 2018-10-18 stsp if (tree)
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;
5312 404c43c4 2018-06-21 stsp }
5313 404c43c4 2018-06-21 stsp
5314 5de5890b 2018-10-18 stsp static const struct got_error *
5315 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
5316 5de5890b 2018-10-18 stsp {
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;
5325 5de5890b 2018-10-18 stsp int ch;
5326 5de5890b 2018-10-18 stsp
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");
5331 5de5890b 2018-10-18 stsp #endif
5332 5de5890b 2018-10-18 stsp
5333 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5334 5de5890b 2018-10-18 stsp switch (ch) {
5335 5de5890b 2018-10-18 stsp case 'c':
5336 5de5890b 2018-10-18 stsp commit_id_str = optarg;
5337 5de5890b 2018-10-18 stsp break;
5338 5de5890b 2018-10-18 stsp case 'r':
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",
5342 9ba1d308 2019-10-21 stsp optarg);
5343 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5344 5de5890b 2018-10-18 stsp break;
5345 5de5890b 2018-10-18 stsp case 'i':
5346 5de5890b 2018-10-18 stsp show_ids = 1;
5347 5de5890b 2018-10-18 stsp break;
5348 c1669e2e 2019-01-09 stsp case 'R':
5349 c1669e2e 2019-01-09 stsp recurse = 1;
5350 c1669e2e 2019-01-09 stsp break;
5351 5de5890b 2018-10-18 stsp default:
5352 2deda0b9 2019-03-07 stsp usage_tree();
5353 5de5890b 2018-10-18 stsp /* NOTREACHED */
5354 5de5890b 2018-10-18 stsp }
5355 5de5890b 2018-10-18 stsp }
5356 5de5890b 2018-10-18 stsp
5357 5de5890b 2018-10-18 stsp argc -= optind;
5358 5de5890b 2018-10-18 stsp argv += optind;
5359 5de5890b 2018-10-18 stsp
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();
5364 5de5890b 2018-10-18 stsp else
5365 7a2c19d6 2019-02-05 stsp path = NULL;
5366 7a2c19d6 2019-02-05 stsp
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;
5371 9bf7a39b 2019-02-05 stsp }
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;
5376 7a2c19d6 2019-02-05 stsp else
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;
5385 7a2c19d6 2019-02-05 stsp } else {
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;
5390 7a2c19d6 2019-02-05 stsp }
5391 5de5890b 2018-10-18 stsp }
5392 5de5890b 2018-10-18 stsp }
5393 5de5890b 2018-10-18 stsp
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;
5397 c02c541e 2019-03-29 stsp
5398 4fedbf4c 2020-11-07 stsp if (worktree) {
5399 4fedbf4c 2020-11-07 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
5400 4fedbf4c 2020-11-07 stsp char *p;
5401 5de5890b 2018-10-18 stsp
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");
5411 9bf7a39b 2019-02-05 stsp free(p);
5412 4fedbf4c 2020-11-07 stsp goto done;
5413 4fedbf4c 2020-11-07 stsp }
5414 4fedbf4c 2020-11-07 stsp free(p);
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;
5418 4fedbf4c 2020-11-07 stsp } else {
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;
5427 9bf7a39b 2019-02-05 stsp }
5428 5de5890b 2018-10-18 stsp
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);
5433 4e0a20a4 2020-03-23 tracey else
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;
5442 5de5890b 2018-10-18 stsp } else {
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,
5446 84de9106 2020-12-26 stsp NULL);
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;
5454 5de5890b 2018-10-18 stsp }
5455 5de5890b 2018-10-18 stsp
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;
5460 f89c34e1 2022-03-13 thomas }
5461 f89c34e1 2022-03-13 thomas
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);
5464 5de5890b 2018-10-18 stsp done:
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;
5475 5de5890b 2018-10-18 stsp }
5476 5de5890b 2018-10-18 stsp return error;
5477 5de5890b 2018-10-18 stsp }
5478 5de5890b 2018-10-18 stsp
5479 6bad629b 2019-02-04 stsp __dead static void
5480 6bad629b 2019-02-04 stsp usage_status(void)
5481 6bad629b 2019-02-04 stsp {
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());
5484 6bad629b 2019-02-04 stsp exit(1);
5485 6bad629b 2019-02-04 stsp }
5486 00357e4d 2021-09-14 tracey
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;
5490 00357e4d 2021-09-14 tracey };
5491 5c860e29 2018-03-12 stsp
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)
5497 6bad629b 2019-02-04 stsp {
5498 00357e4d 2021-09-14 tracey struct got_status_arg *st = arg;
5499 00357e4d 2021-09-14 tracey
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;
5505 00357e4d 2021-09-14 tracey
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]) {
5510 00357e4d 2021-09-14 tracey j++;
5511 00357e4d 2021-09-14 tracey continue;
5512 00357e4d 2021-09-14 tracey }
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])
5516 00357e4d 2021-09-14 tracey break;
5517 00357e4d 2021-09-14 tracey }
5518 081470ac 2020-08-13 stsp }
5519 00357e4d 2021-09-14 tracey
5520 00357e4d 2021-09-14 tracey if (st->suppress && j == 0)
5521 00357e4d 2021-09-14 tracey goto print;
5522 00357e4d 2021-09-14 tracey
5523 081470ac 2020-08-13 stsp if (i == ncodes)
5524 081470ac 2020-08-13 stsp return NULL;
5525 081470ac 2020-08-13 stsp }
5526 00357e4d 2021-09-14 tracey print:
5527 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
5528 b72f483a 2019-02-05 stsp return NULL;
5529 6bad629b 2019-02-04 stsp }
5530 5c860e29 2018-03-12 stsp
5531 6bad629b 2019-02-04 stsp static const struct got_error *
5532 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
5533 6bad629b 2019-02-04 stsp {
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;
5542 5c860e29 2018-03-12 stsp
5543 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
5544 72ea6654 2019-07-27 stsp
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;
5548 00357e4d 2021-09-14 tracey
5549 00357e4d 2021-09-14 tracey while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
5550 6bad629b 2019-02-04 stsp switch (ch) {
5551 f6343036 2021-06-22 stsp case 'I':
5552 f6343036 2021-06-22 stsp no_ignores = 1;
5553 f6343036 2021-06-22 stsp break;
5554 788d4a19 2021-09-14 stsp case 'S':
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 */
5559 081470ac 2020-08-13 stsp case 's':
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:
5571 081470ac 2020-08-13 stsp break;
5572 081470ac 2020-08-13 stsp default:
5573 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
5574 081470ac 2020-08-13 stsp optarg[i]);
5575 081470ac 2020-08-13 stsp }
5576 081470ac 2020-08-13 stsp }
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;
5580 081470ac 2020-08-13 stsp break;
5581 5c860e29 2018-03-12 stsp default:
5582 2deda0b9 2019-03-07 stsp usage_status();
5583 6bad629b 2019-02-04 stsp /* NOTREACHED */
5584 5c860e29 2018-03-12 stsp }
5585 5c860e29 2018-03-12 stsp }
5586 5c860e29 2018-03-12 stsp
5587 6bad629b 2019-02-04 stsp argc -= optind;
5588 6bad629b 2019-02-04 stsp argv += optind;
5589 5c860e29 2018-03-12 stsp
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");
5594 f42b1b34 2018-03-12 stsp #endif
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;
5599 927df6b7 2019-02-10 stsp }
5600 927df6b7 2019-02-10 stsp
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;
5606 fa51e947 2020-03-27 stsp }
5607 6bad629b 2019-02-04 stsp
5608 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5609 c9956ddf 2019-09-08 stsp NULL);
5610 6bad629b 2019-02-04 stsp if (error != NULL)
5611 6bad629b 2019-02-04 stsp goto done;
5612 6bad629b 2019-02-04 stsp
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;
5617 087fb88c 2019-08-04 stsp
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;
5621 6bad629b 2019-02-04 stsp
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);
5624 6bad629b 2019-02-04 stsp done:
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;
5630 d0eebce4 2019-03-11 stsp }
5631 d0eebce4 2019-03-11 stsp
5632 d0eebce4 2019-03-11 stsp __dead static void
5633 d0eebce4 2019-03-11 stsp usage_ref(void)
5634 d0eebce4 2019-03-11 stsp {
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());
5639 d0eebce4 2019-03-11 stsp exit(1);
5640 d0eebce4 2019-03-11 stsp }
5641 d0eebce4 2019-03-11 stsp
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)
5644 d0eebce4 2019-03-11 stsp {
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;
5648 d0eebce4 2019-03-11 stsp
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,
5652 2bf0fa54 2021-11-20 thomas repo);
5653 d0eebce4 2019-03-11 stsp if (err)
5654 d0eebce4 2019-03-11 stsp return err;
5655 d0eebce4 2019-03-11 stsp
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);
5663 d0eebce4 2019-03-11 stsp }
5664 d0eebce4 2019-03-11 stsp
5665 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5666 d0eebce4 2019-03-11 stsp return NULL;
5667 d0eebce4 2019-03-11 stsp }
5668 d0eebce4 2019-03-11 stsp
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)
5671 d0eebce4 2019-03-11 stsp {
5672 161728eb 2021-07-24 stsp const struct got_error *err;
5673 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5674 d0eebce4 2019-03-11 stsp
5675 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5676 d0eebce4 2019-03-11 stsp if (err)
5677 d0eebce4 2019-03-11 stsp return err;
5678 993f033b 2021-07-16 stsp
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;
5682 d0eebce4 2019-03-11 stsp }
5683 d0eebce4 2019-03-11 stsp
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)
5686 d0eebce4 2019-03-11 stsp {
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;
5691 d1644381 2019-07-14 stsp
5692 d1644381 2019-07-14 stsp /*
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.
5696 d1644381 2019-07-14 stsp */
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);
5699 d0eebce4 2019-03-11 stsp
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;
5709 d83d9d5c 2019-05-13 stsp
5710 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5711 d0eebce4 2019-03-11 stsp if (err)
5712 d0eebce4 2019-03-11 stsp goto done;
5713 d0eebce4 2019-03-11 stsp
5714 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5715 d0eebce4 2019-03-11 stsp done:
5716 d0eebce4 2019-03-11 stsp if (ref)
5717 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5718 d0eebce4 2019-03-11 stsp free(id);
5719 d1c1ae5f 2019-08-12 stsp return err;
5720 d1c1ae5f 2019-08-12 stsp }
5721 d1c1ae5f 2019-08-12 stsp
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)
5724 d1c1ae5f 2019-08-12 stsp {
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;
5728 d1c1ae5f 2019-08-12 stsp
5729 d1c1ae5f 2019-08-12 stsp /*
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.
5733 d1c1ae5f 2019-08-12 stsp */
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);
5736 d1c1ae5f 2019-08-12 stsp
5737 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5738 d1c1ae5f 2019-08-12 stsp if (err)
5739 d1c1ae5f 2019-08-12 stsp return err;
5740 d1c1ae5f 2019-08-12 stsp
5741 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5742 d1c1ae5f 2019-08-12 stsp if (err)
5743 d1c1ae5f 2019-08-12 stsp goto done;
5744 d1c1ae5f 2019-08-12 stsp
5745 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5746 d1c1ae5f 2019-08-12 stsp done:
5747 d1c1ae5f 2019-08-12 stsp if (target_ref)
5748 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5749 d1c1ae5f 2019-08-12 stsp if (ref)
5750 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5751 d0eebce4 2019-03-11 stsp return err;
5752 d0eebce4 2019-03-11 stsp }
5753 d0eebce4 2019-03-11 stsp
5754 d0eebce4 2019-03-11 stsp static const struct got_error *
5755 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5756 d0eebce4 2019-03-11 stsp {
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;
5764 d0eebce4 2019-03-11 stsp
5765 2bf0fa54 2021-11-20 thomas while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
5766 d0eebce4 2019-03-11 stsp switch (ch) {
5767 e31abbf2 2020-03-22 stsp case 'c':
5768 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5769 e31abbf2 2020-03-22 stsp break;
5770 d0eebce4 2019-03-11 stsp case 'd':
5771 e31abbf2 2020-03-22 stsp do_delete = 1;
5772 d0eebce4 2019-03-11 stsp break;
5773 d0eebce4 2019-03-11 stsp case 'r':
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",
5777 9ba1d308 2019-10-21 stsp optarg);
5778 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5779 d0eebce4 2019-03-11 stsp break;
5780 d0eebce4 2019-03-11 stsp case 'l':
5781 d0eebce4 2019-03-11 stsp do_list = 1;
5782 d1c1ae5f 2019-08-12 stsp break;
5783 d1c1ae5f 2019-08-12 stsp case 's':
5784 e31abbf2 2020-03-22 stsp symref_target = optarg;
5785 2bf0fa54 2021-11-20 thomas break;
5786 2bf0fa54 2021-11-20 thomas case 't':
5787 2bf0fa54 2021-11-20 thomas sort_by_time = 1;
5788 d0eebce4 2019-03-11 stsp break;
5789 d0eebce4 2019-03-11 stsp default:
5790 d0eebce4 2019-03-11 stsp usage_ref();
5791 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5792 d0eebce4 2019-03-11 stsp }
5793 d0eebce4 2019-03-11 stsp }
5794 d0eebce4 2019-03-11 stsp
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");
5809 d0eebce4 2019-03-11 stsp
5810 d0eebce4 2019-03-11 stsp argc -= optind;
5811 d0eebce4 2019-03-11 stsp argv += optind;
5812 d0eebce4 2019-03-11 stsp
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;
5821 b2070a3f 2020-03-22 stsp }
5822 b2070a3f 2020-03-22 stsp }
5823 e31abbf2 2020-03-22 stsp } else {
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;
5830 b2070a3f 2020-03-22 stsp }
5831 e31abbf2 2020-03-22 stsp }
5832 b2070a3f 2020-03-22 stsp
5833 b2070a3f 2020-03-22 stsp if (refname)
5834 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5835 d0eebce4 2019-03-11 stsp
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");
5841 e0b57350 2019-03-12 stsp } else {
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");
5845 e0b57350 2019-03-12 stsp }
5846 d0eebce4 2019-03-11 stsp #endif
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;
5851 d0eebce4 2019-03-11 stsp }
5852 d0eebce4 2019-03-11 stsp
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;
5857 d0eebce4 2019-03-11 stsp else
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;
5866 d0eebce4 2019-03-11 stsp } else {
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;
5871 d0eebce4 2019-03-11 stsp }
5872 d0eebce4 2019-03-11 stsp }
5873 d0eebce4 2019-03-11 stsp }
5874 d0eebce4 2019-03-11 stsp
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;
5878 d0eebce4 2019-03-11 stsp
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;
5883 c02c541e 2019-03-29 stsp
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);
5890 e31abbf2 2020-03-22 stsp else {
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);
5894 e31abbf2 2020-03-22 stsp }
5895 4e759de4 2019-06-26 stsp done:
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;
5901 1d0f4054 2021-06-17 stsp }
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;
5907 4e759de4 2019-06-26 stsp }
5908 4e759de4 2019-06-26 stsp
5909 4e759de4 2019-06-26 stsp __dead static void
5910 4e759de4 2019-06-26 stsp usage_branch(void)
5911 4e759de4 2019-06-26 stsp {
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());
5915 4e759de4 2019-06-26 stsp exit(1);
5916 4e759de4 2019-06-26 stsp }
5917 4e759de4 2019-06-26 stsp
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)
5921 4e99b47e 2019-10-04 stsp {
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;
5925 4e99b47e 2019-10-04 stsp
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;
5930 4e99b47e 2019-10-04 stsp
5931 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5932 4e99b47e 2019-10-04 stsp if (err)
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 = "* ";
5937 4e99b47e 2019-10-04 stsp else
5938 4e99b47e 2019-10-04 stsp marker = "~ ";
5939 4e99b47e 2019-10-04 stsp free(id);
5940 4e99b47e 2019-10-04 stsp }
5941 4e99b47e 2019-10-04 stsp
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;
5948 4e99b47e 2019-10-04 stsp
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");
5952 4e99b47e 2019-10-04 stsp
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;
5956 4e99b47e 2019-10-04 stsp }
5957 4e99b47e 2019-10-04 stsp
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)
5960 ad89fa31 2019-10-04 stsp {
5961 ad89fa31 2019-10-04 stsp const char *refname;
5962 ad89fa31 2019-10-04 stsp
5963 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5964 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5965 ad89fa31 2019-10-04 stsp
5966 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5967 ad89fa31 2019-10-04 stsp
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;
5972 ad89fa31 2019-10-04 stsp
5973 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5974 ad89fa31 2019-10-04 stsp
5975 ad89fa31 2019-10-04 stsp return NULL;
5976 ad89fa31 2019-10-04 stsp }
5977 ad89fa31 2019-10-04 stsp
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)
5981 4e759de4 2019-06-26 stsp {
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;
5987 4e759de4 2019-06-26 stsp
5988 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5989 ba882ee3 2019-07-11 stsp
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);
5993 4e99b47e 2019-10-04 stsp if (err)
5994 4e99b47e 2019-10-04 stsp return err;
5995 4e99b47e 2019-10-04 stsp
5996 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5997 4e99b47e 2019-10-04 stsp worktree);
5998 4e99b47e 2019-10-04 stsp if (err)
5999 4e99b47e 2019-10-04 stsp return err;
6000 4e99b47e 2019-10-04 stsp
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);
6004 4e99b47e 2019-10-04 stsp if (err)
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);
6008 4e99b47e 2019-10-04 stsp }
6009 4e99b47e 2019-10-04 stsp }
6010 4e99b47e 2019-10-04 stsp
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,
6013 a0b48eaf 2021-11-20 thomas repo);
6014 4e759de4 2019-06-26 stsp if (err)
6015 4e759de4 2019-06-26 stsp return err;
6016 4e759de4 2019-06-26 stsp
6017 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry)
6018 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
6019 4e759de4 2019-06-26 stsp
6020 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
6021 34d4e04c 2021-02-08 stsp
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,
6024 a0b48eaf 2021-11-20 thomas repo);
6025 34d4e04c 2021-02-08 stsp if (err)
6026 34d4e04c 2021-02-08 stsp return err;
6027 34d4e04c 2021-02-08 stsp
6028 34d4e04c 2021-02-08 stsp TAILQ_FOREACH(re, &refs, entry)
6029 34d4e04c 2021-02-08 stsp list_branch(repo, worktree, re->ref);
6030 34d4e04c 2021-02-08 stsp
6031 34d4e04c 2021-02-08 stsp got_ref_list_free(&refs);
6032 34d4e04c 2021-02-08 stsp
6033 4e759de4 2019-06-26 stsp return NULL;
6034 4e759de4 2019-06-26 stsp }
6035 4e759de4 2019-06-26 stsp
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)
6039 4e759de4 2019-06-26 stsp {
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;
6043 4e759de4 2019-06-26 stsp
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;
6050 2f1457c6 2021-08-27 stsp
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");
6053 4e759de4 2019-06-26 stsp
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;
6058 2f1457c6 2021-08-27 stsp }
6059 4e759de4 2019-06-26 stsp
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;
6065 2f1457c6 2021-08-27 stsp /*
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.
6069 2f1457c6 2021-08-27 stsp */
6070 2f1457c6 2021-08-27 stsp err2 = got_ref_open(&ref, repo, remote_refname, 0);
6071 2f1457c6 2021-08-27 stsp if (err2)
6072 2f1457c6 2021-08-27 stsp goto done;
6073 2f1457c6 2021-08-27 stsp err = NULL;
6074 2f1457c6 2021-08-27 stsp }
6075 2f1457c6 2021-08-27 stsp
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;
6082 45cd4e47 2019-08-25 stsp }
6083 45cd4e47 2019-08-25 stsp
6084 978a28a1 2021-09-04 naddy err = delete_ref(repo, ref);
6085 4e759de4 2019-06-26 stsp done:
6086 45cd4e47 2019-08-25 stsp if (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;
6091 4e759de4 2019-06-26 stsp }
6092 4e759de4 2019-06-26 stsp
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)
6096 4e759de4 2019-06-26 stsp {
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;
6100 d3f84d51 2019-07-11 stsp
6101 d3f84d51 2019-07-11 stsp /*
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.
6105 d3f84d51 2019-07-11 stsp */
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);
6108 2f1457c6 2021-08-27 stsp
6109 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
6110 2f1457c6 2021-08-27 stsp branch_name += 11;
6111 4e759de4 2019-06-26 stsp
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;
6115 4e759de4 2019-06-26 stsp }
6116 4e759de4 2019-06-26 stsp
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;
6123 4e759de4 2019-06-26 stsp
6124 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
6125 4e759de4 2019-06-26 stsp if (err)
6126 4e759de4 2019-06-26 stsp goto done;
6127 4e759de4 2019-06-26 stsp
6128 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
6129 d0eebce4 2019-03-11 stsp done:
6130 4e759de4 2019-06-26 stsp if (ref)
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;
6135 4e759de4 2019-06-26 stsp }
6136 4e759de4 2019-06-26 stsp
6137 4e759de4 2019-06-26 stsp static const struct got_error *
6138 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
6139 4e759de4 2019-06-26 stsp {
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;
6151 4e759de4 2019-06-26 stsp
6152 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
6153 da76fce2 2020-02-24 stsp
6154 a0b48eaf 2021-11-20 thomas while ((ch = getopt(argc, argv, "c:d:r:lnt")) != -1) {
6155 4e759de4 2019-06-26 stsp switch (ch) {
6156 a74f7e83 2019-11-10 stsp case 'c':
6157 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
6158 a74f7e83 2019-11-10 stsp break;
6159 4e759de4 2019-06-26 stsp case 'd':
6160 4e759de4 2019-06-26 stsp delref = optarg;
6161 4e759de4 2019-06-26 stsp break;
6162 4e759de4 2019-06-26 stsp case 'r':
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",
6166 9ba1d308 2019-10-21 stsp optarg);
6167 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
6168 4e759de4 2019-06-26 stsp break;
6169 4e759de4 2019-06-26 stsp case 'l':
6170 4e759de4 2019-06-26 stsp do_list = 1;
6171 da76fce2 2020-02-24 stsp break;
6172 da76fce2 2020-02-24 stsp case 'n':
6173 da76fce2 2020-02-24 stsp do_update = 0;
6174 4e759de4 2019-06-26 stsp break;
6175 a0b48eaf 2021-11-20 thomas case 't':
6176 a0b48eaf 2021-11-20 thomas sort_by_time = 1;
6177 a0b48eaf 2021-11-20 thomas break;
6178 4e759de4 2019-06-26 stsp default:
6179 4e759de4 2019-06-26 stsp usage_branch();
6180 4e759de4 2019-06-26 stsp /* NOTREACHED */
6181 4e759de4 2019-06-26 stsp }
6182 4e759de4 2019-06-26 stsp }
6183 4e759de4 2019-06-26 stsp
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");
6188 4e759de4 2019-06-26 stsp
6189 4e759de4 2019-06-26 stsp argc -= optind;
6190 4e759de4 2019-06-26 stsp argv += optind;
6191 ad89fa31 2019-10-04 stsp
6192 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
6193 ad89fa31 2019-10-04 stsp do_show = 1;
6194 4e759de4 2019-06-26 stsp
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");
6197 a74f7e83 2019-11-10 stsp
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();
6203 4e759de4 2019-06-26 stsp
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");
6209 4e759de4 2019-06-26 stsp } else {
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");
6213 4e759de4 2019-06-26 stsp }
6214 4e759de4 2019-06-26 stsp #endif
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;
6219 4e759de4 2019-06-26 stsp }
6220 4e759de4 2019-06-26 stsp
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;
6225 4e759de4 2019-06-26 stsp else
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;
6234 4e759de4 2019-06-26 stsp } else {
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;
6239 4e759de4 2019-06-26 stsp }
6240 4e759de4 2019-06-26 stsp }
6241 4e759de4 2019-06-26 stsp }
6242 4e759de4 2019-06-26 stsp
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;
6246 4e759de4 2019-06-26 stsp
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;
6251 4e759de4 2019-06-26 stsp
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);
6258 4e759de4 2019-06-26 stsp else {
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,
6262 84de9106 2020-12-26 stsp NULL);
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;
6280 da76fce2 2020-02-24 stsp
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])
6289 da76fce2 2020-02-24 stsp == -1) {
6290 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
6291 da76fce2 2020-02-24 stsp goto done;
6292 da76fce2 2020-02-24 stsp }
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,
6298 da76fce2 2020-02-24 stsp repo);
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,
6308 9627c110 2020-04-18 stsp NULL);
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);
6315 4f3c844b 2021-09-14 stsp }
6316 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6317 da76fce2 2020-02-24 stsp }
6318 4e759de4 2019-06-26 stsp }
6319 4e759de4 2019-06-26 stsp done:
6320 da76fce2 2020-02-24 stsp if (ref)
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;
6326 1d0f4054 2021-06-17 stsp }
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;
6337 d00136be 2019-03-26 stsp }
6338 d00136be 2019-03-26 stsp
6339 8e7bd50a 2019-08-22 stsp
6340 d00136be 2019-03-26 stsp __dead static void
6341 8e7bd50a 2019-08-22 stsp usage_tag(void)
6342 8e7bd50a 2019-08-22 stsp {
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());
6346 8e7bd50a 2019-08-22 stsp exit(1);
6347 b8bad2ba 2019-08-23 stsp }
6348 b8bad2ba 2019-08-23 stsp
6349 b8bad2ba 2019-08-23 stsp #if 0
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)
6352 b8bad2ba 2019-08-23 stsp {
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;
6358 b8bad2ba 2019-08-23 stsp
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);
6363 b8bad2ba 2019-08-23 stsp if (err)
6364 b8bad2ba 2019-08-23 stsp return err;
6365 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(sorted, new, entry);
6366 b8bad2ba 2019-08-23 stsp continue;
6367 b8bad2ba 2019-08-23 stsp } else {
6368 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
6369 b8bad2ba 2019-08-23 stsp if (err)
6370 b8bad2ba 2019-08-23 stsp break;
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);
6373 b8bad2ba 2019-08-23 stsp if (err)
6374 b8bad2ba 2019-08-23 stsp break;
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);
6377 b8bad2ba 2019-08-23 stsp }
6378 b8bad2ba 2019-08-23 stsp
6379 b8bad2ba 2019-08-23 stsp while (se) {
6380 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
6381 b8bad2ba 2019-08-23 stsp if (err)
6382 b8bad2ba 2019-08-23 stsp break;
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);
6385 b8bad2ba 2019-08-23 stsp if (err)
6386 b8bad2ba 2019-08-23 stsp break;
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);
6389 b8bad2ba 2019-08-23 stsp
6390 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
6391 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6392 b8bad2ba 2019-08-23 stsp if (err)
6393 b8bad2ba 2019-08-23 stsp return err;
6394 dbdddfee 2021-06-23 naddy STAILQ_INSERT_AFTER(sorted, se, new, entry);
6395 b8bad2ba 2019-08-23 stsp break;
6396 b8bad2ba 2019-08-23 stsp }
6397 dbdddfee 2021-06-23 naddy se = STAILQ_NEXT(se, entry);
6398 b8bad2ba 2019-08-23 stsp continue;
6399 b8bad2ba 2019-08-23 stsp }
6400 b8bad2ba 2019-08-23 stsp }
6401 b8bad2ba 2019-08-23 stsp done:
6402 b8bad2ba 2019-08-23 stsp return err;
6403 8e7bd50a 2019-08-22 stsp }
6404 b8bad2ba 2019-08-23 stsp #endif
6405 b8bad2ba 2019-08-23 stsp
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)
6408 8e7bd50a 2019-08-22 stsp {
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;
6412 8e7bd50a 2019-08-22 stsp
6413 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6414 8e7bd50a 2019-08-22 stsp
6415 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6416 8e7bd50a 2019-08-22 stsp if (err)
6417 8e7bd50a 2019-08-22 stsp return err;
6418 8e7bd50a 2019-08-22 stsp
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;
6428 8e7bd50a 2019-08-22 stsp
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)
6431 8e7bd50a 2019-08-22 stsp continue;
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");
6436 8e7bd50a 2019-08-22 stsp break;
6437 8e7bd50a 2019-08-22 stsp }
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);
6440 8e7bd50a 2019-08-22 stsp
6441 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
6442 8e7bd50a 2019-08-22 stsp if (err)
6443 8e7bd50a 2019-08-22 stsp break;
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) {
6447 d4efa91b 2020-01-14 stsp free(id);
6448 d4efa91b 2020-01-14 stsp break;
6449 d4efa91b 2020-01-14 stsp }
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) {
6453 d4efa91b 2020-01-14 stsp free(id);
6454 d4efa91b 2020-01-14 stsp break;
6455 d4efa91b 2020-01-14 stsp }
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);
6460 d4efa91b 2020-01-14 stsp free(id);
6461 d4efa91b 2020-01-14 stsp if (err)
6462 d4efa91b 2020-01-14 stsp break;
6463 d4efa91b 2020-01-14 stsp } else {
6464 d4efa91b 2020-01-14 stsp free(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));
6469 d4efa91b 2020-01-14 stsp if (err)
6470 d4efa91b 2020-01-14 stsp break;
6471 d4efa91b 2020-01-14 stsp }
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);
6478 d4efa91b 2020-01-14 stsp else {
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,
6482 d4efa91b 2020-01-14 stsp id_str);
6483 d4efa91b 2020-01-14 stsp break;
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,
6486 d4efa91b 2020-01-14 stsp id_str);
6487 d4efa91b 2020-01-14 stsp break;
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,
6490 d4efa91b 2020-01-14 stsp id_str);
6491 d4efa91b 2020-01-14 stsp break;
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,
6494 d4efa91b 2020-01-14 stsp id_str);
6495 d4efa91b 2020-01-14 stsp break;
6496 d4efa91b 2020-01-14 stsp default:
6497 d4efa91b 2020-01-14 stsp break;
6498 d4efa91b 2020-01-14 stsp }
6499 8e7bd50a 2019-08-22 stsp }
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);
6503 d4efa91b 2020-01-14 stsp if (err)
6504 d4efa91b 2020-01-14 stsp break;
6505 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
6506 d4efa91b 2020-01-14 stsp } else {
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");
6511 d4efa91b 2020-01-14 stsp break;
6512 d4efa91b 2020-01-14 stsp }
6513 8e7bd50a 2019-08-22 stsp }
6514 8e7bd50a 2019-08-22 stsp
6515 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
6516 8e7bd50a 2019-08-22 stsp do {
6517 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
6518 8e7bd50a 2019-08-22 stsp if (line)
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);
6522 8e7bd50a 2019-08-22 stsp }
6523 8e7bd50a 2019-08-22 stsp
6524 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
6525 8e7bd50a 2019-08-22 stsp return NULL;
6526 8e7bd50a 2019-08-22 stsp }
6527 8e7bd50a 2019-08-22 stsp
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)
6531 8e7bd50a 2019-08-22 stsp {
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;
6537 8e7bd50a 2019-08-22 stsp
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;
6541 8e7bd50a 2019-08-22 stsp }
6542 8e7bd50a 2019-08-22 stsp
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;
6549 8e7bd50a 2019-08-22 stsp }
6550 8e7bd50a 2019-08-22 stsp
6551 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6552 8e7bd50a 2019-08-22 stsp if (err)
6553 8e7bd50a 2019-08-22 stsp goto done;
6554 8e7bd50a 2019-08-22 stsp
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;
6558 97972933 2020-09-11 stsp }
6559 8e7bd50a 2019-08-22 stsp
6560 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
6561 8e7bd50a 2019-08-22 stsp if (err)
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);
6565 8e7bd50a 2019-08-22 stsp done:
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);
6569 8e7bd50a 2019-08-22 stsp
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);
6572 97972933 2020-09-11 stsp
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;
6579 8e7bd50a 2019-08-22 stsp }
6580 8e7bd50a 2019-08-22 stsp return err;
6581 8e7bd50a 2019-08-22 stsp }
6582 8e7bd50a 2019-08-22 stsp
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)
6586 8e7bd50a 2019-08-22 stsp {
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;
6595 8e7bd50a 2019-08-22 stsp
6596 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6597 84de9106 2020-12-26 stsp
6598 8e7bd50a 2019-08-22 stsp /*
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.
6602 8e7bd50a 2019-08-22 stsp */
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);
6605 8e7bd50a 2019-08-22 stsp
6606 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
6607 8e7bd50a 2019-08-22 stsp if (err)
6608 8e7bd50a 2019-08-22 stsp return err;
6609 8e7bd50a 2019-08-22 stsp
6610 84de9106 2020-12-26 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6611 84de9106 2020-12-26 stsp if (err)
6612 84de9106 2020-12-26 stsp goto done;
6613 84de9106 2020-12-26 stsp
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);
6616 8e7bd50a 2019-08-22 stsp if (err)
6617 8e7bd50a 2019-08-22 stsp goto done;
6618 8e7bd50a 2019-08-22 stsp
6619 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
6620 8e7bd50a 2019-08-22 stsp if (err)
6621 8e7bd50a 2019-08-22 stsp goto done;
6622 8e7bd50a 2019-08-22 stsp
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;
6628 8e7bd50a 2019-08-22 stsp }
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;
6633 8e7bd50a 2019-08-22 stsp }
6634 8e7bd50a 2019-08-22 stsp
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;
6641 8e7bd50a 2019-08-22 stsp
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;
6650 f372d5cd 2019-10-21 stsp }
6651 8e7bd50a 2019-08-22 stsp }
6652 8e7bd50a 2019-08-22 stsp
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;
6659 f372d5cd 2019-10-21 stsp }
6660 8e7bd50a 2019-08-22 stsp
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;
6666 f372d5cd 2019-10-21 stsp }
6667 8e7bd50a 2019-08-22 stsp
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;
6673 f372d5cd 2019-10-21 stsp }
6674 8e7bd50a 2019-08-22 stsp
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;
6680 8e7bd50a 2019-08-22 stsp }
6681 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
6682 8e7bd50a 2019-08-22 stsp done:
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);
6689 8e7bd50a 2019-08-22 stsp if (ref)
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;
6699 8e7bd50a 2019-08-22 stsp }
6700 8e7bd50a 2019-08-22 stsp
6701 8e7bd50a 2019-08-22 stsp static const struct got_error *
6702 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
6703 8e7bd50a 2019-08-22 stsp {
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;
6711 8e7bd50a 2019-08-22 stsp
6712 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6713 8e7bd50a 2019-08-22 stsp switch (ch) {
6714 80106605 2020-02-24 stsp case 'c':
6715 80106605 2020-02-24 stsp commit_id_arg = optarg;
6716 80106605 2020-02-24 stsp break;
6717 8e7bd50a 2019-08-22 stsp case 'm':
6718 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6719 8e7bd50a 2019-08-22 stsp break;
6720 8e7bd50a 2019-08-22 stsp case 'r':
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",
6724 9ba1d308 2019-10-21 stsp optarg);
6725 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6726 8e7bd50a 2019-08-22 stsp break;
6727 8e7bd50a 2019-08-22 stsp case 'l':
6728 8e7bd50a 2019-08-22 stsp do_list = 1;
6729 8e7bd50a 2019-08-22 stsp break;
6730 8e7bd50a 2019-08-22 stsp default:
6731 8e7bd50a 2019-08-22 stsp usage_tag();
6732 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6733 8e7bd50a 2019-08-22 stsp }
6734 8e7bd50a 2019-08-22 stsp }
6735 8e7bd50a 2019-08-22 stsp
6736 8e7bd50a 2019-08-22 stsp argc -= optind;
6737 8e7bd50a 2019-08-22 stsp argv += optind;
6738 8e7bd50a 2019-08-22 stsp
6739 8e7bd50a 2019-08-22 stsp if (do_list) {
6740 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6741 775ce909 2020-03-22 stsp errx(1,
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();
6749 80106605 2020-02-24 stsp
6750 a2887370 2019-08-23 stsp tag_name = argv[0];
6751 8e7bd50a 2019-08-22 stsp
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");
6757 8e7bd50a 2019-08-22 stsp } else {
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");
6761 8e7bd50a 2019-08-22 stsp }
6762 8e7bd50a 2019-08-22 stsp #endif
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;
6767 8e7bd50a 2019-08-22 stsp }
6768 8e7bd50a 2019-08-22 stsp
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;
6773 8e7bd50a 2019-08-22 stsp else
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;
6782 8e7bd50a 2019-08-22 stsp } else {
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;
6787 8e7bd50a 2019-08-22 stsp }
6788 8e7bd50a 2019-08-22 stsp }
6789 8e7bd50a 2019-08-22 stsp }
6790 8e7bd50a 2019-08-22 stsp
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);
6799 8e7bd50a 2019-08-22 stsp } else {
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;
6806 c9956ddf 2019-09-08 stsp
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;
6811 8e7bd50a 2019-08-22 stsp }
6812 8e7bd50a 2019-08-22 stsp
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;
6829 8e7bd50a 2019-08-22 stsp }
6830 8e7bd50a 2019-08-22 stsp
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);
6833 8e7bd50a 2019-08-22 stsp }
6834 8e7bd50a 2019-08-22 stsp done:
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;
6839 1d0f4054 2021-06-17 stsp }
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;
6847 8e7bd50a 2019-08-22 stsp }
6848 8e7bd50a 2019-08-22 stsp
6849 8e7bd50a 2019-08-22 stsp __dead static void
6850 d00136be 2019-03-26 stsp usage_add(void)
6851 d00136be 2019-03-26 stsp {
6852 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6853 022fae89 2019-12-06 tracey getprogname());
6854 d00136be 2019-03-26 stsp exit(1);
6855 d00136be 2019-03-26 stsp }
6856 d00136be 2019-03-26 stsp
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)
6859 4e68cba3 2019-11-23 stsp {
6860 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6861 4e68cba3 2019-11-23 stsp path++;
6862 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6863 4e68cba3 2019-11-23 stsp return NULL;
6864 4e68cba3 2019-11-23 stsp }
6865 4e68cba3 2019-11-23 stsp
6866 4e68cba3 2019-11-23 stsp static const struct got_error *
6867 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6868 d00136be 2019-03-26 stsp {
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;
6876 1dd54920 2019-05-11 stsp
6877 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6878 d00136be 2019-03-26 stsp
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;
6883 022fae89 2019-12-06 tracey break;
6884 4e68cba3 2019-11-23 stsp case 'R':
6885 4e68cba3 2019-11-23 stsp can_recurse = 1;
6886 4e68cba3 2019-11-23 stsp break;
6887 d00136be 2019-03-26 stsp default:
6888 d00136be 2019-03-26 stsp usage_add();
6889 d00136be 2019-03-26 stsp /* NOTREACHED */
6890 d00136be 2019-03-26 stsp }
6891 d00136be 2019-03-26 stsp }
6892 d00136be 2019-03-26 stsp
6893 d00136be 2019-03-26 stsp argc -= optind;
6894 d00136be 2019-03-26 stsp argv += optind;
6895 d00136be 2019-03-26 stsp
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");
6900 43012d58 2019-07-14 stsp #endif
6901 723c305c 2019-05-11 jcs if (argc < 1)
6902 d00136be 2019-03-26 stsp usage_add();
6903 d00136be 2019-03-26 stsp
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;
6908 d00136be 2019-03-26 stsp }
6909 723c305c 2019-05-11 jcs
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;
6915 fa51e947 2020-03-27 stsp }
6916 d00136be 2019-03-26 stsp
6917 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6918 c9956ddf 2019-09-08 stsp NULL);
6919 031a5338 2019-03-26 stsp if (error != NULL)
6920 031a5338 2019-03-26 stsp goto done;
6921 031a5338 2019-03-26 stsp
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;
6926 d00136be 2019-03-26 stsp
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;
6930 022fae89 2019-12-06 tracey
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;
6940 4e68cba3 2019-11-23 stsp }
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);
6944 4e68cba3 2019-11-23 stsp continue;
6945 4e68cba3 2019-11-23 stsp }
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;
6950 4e68cba3 2019-11-23 stsp }
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;
6956 4e68cba3 2019-11-23 stsp }
6957 4e68cba3 2019-11-23 stsp }
6958 4e68cba3 2019-11-23 stsp }
6959 022fae89 2019-12-06 tracey
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);
6962 d00136be 2019-03-26 stsp done:
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;
6967 1d0f4054 2021-06-17 stsp }
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;
6975 2ec1f75b 2019-03-26 stsp }
6976 2ec1f75b 2019-03-26 stsp
6977 2ec1f75b 2019-03-26 stsp __dead static void
6978 648e4ef7 2019-07-09 stsp usage_remove(void)
6979 2ec1f75b 2019-03-26 stsp {
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());
6982 2ec1f75b 2019-03-26 stsp exit(1);
6983 2a06fe5f 2019-08-24 stsp }
6984 2a06fe5f 2019-08-24 stsp
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)
6988 2a06fe5f 2019-08-24 stsp {
6989 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6990 f2a9dc41 2019-12-13 tracey path++;
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;
6997 2ec1f75b 2019-03-26 stsp }
6998 2ec1f75b 2019-03-26 stsp
6999 2ec1f75b 2019-03-26 stsp static const struct got_error *
7000 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
7001 2ec1f75b 2019-03-26 stsp {
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;
7011 2ec1f75b 2019-03-26 stsp
7012 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
7013 17ed4618 2019-06-02 stsp
7014 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7015 2ec1f75b 2019-03-26 stsp switch (ch) {
7016 2ec1f75b 2019-03-26 stsp case 'f':
7017 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
7018 d64cc78a 2022-01-25 thomas ignore_missing_paths = 1;
7019 2ec1f75b 2019-03-26 stsp break;
7020 70e3e7f5 2019-12-13 tracey case 'k':
7021 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
7022 70e3e7f5 2019-12-13 tracey break;
7023 f2a9dc41 2019-12-13 tracey case 'R':
7024 f2a9dc41 2019-12-13 tracey can_recurse = 1;
7025 f2a9dc41 2019-12-13 tracey break;
7026 766841c2 2020-08-13 stsp case 's':
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;
7031 766841c2 2020-08-13 stsp break;
7032 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
7033 d64cc78a 2022-01-25 thomas ignore_missing_paths = 1;
7034 766841c2 2020-08-13 stsp break;
7035 766841c2 2020-08-13 stsp default:
7036 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
7037 766841c2 2020-08-13 stsp optarg[i]);
7038 766841c2 2020-08-13 stsp }
7039 766841c2 2020-08-13 stsp }
7040 766841c2 2020-08-13 stsp status_codes = optarg;
7041 766841c2 2020-08-13 stsp break;
7042 2ec1f75b 2019-03-26 stsp default:
7043 f2a9dc41 2019-12-13 tracey usage_remove();
7044 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
7045 2ec1f75b 2019-03-26 stsp }
7046 2ec1f75b 2019-03-26 stsp }
7047 2ec1f75b 2019-03-26 stsp
7048 2ec1f75b 2019-03-26 stsp argc -= optind;
7049 2ec1f75b 2019-03-26 stsp argv += optind;
7050 2ec1f75b 2019-03-26 stsp
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");
7055 43012d58 2019-07-14 stsp #endif
7056 17ed4618 2019-06-02 stsp if (argc < 1)
7057 648e4ef7 2019-07-09 stsp usage_remove();
7058 2ec1f75b 2019-03-26 stsp
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;
7063 2ec1f75b 2019-03-26 stsp }
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;
7069 fa51e947 2020-03-27 stsp }
7070 2ec1f75b 2019-03-26 stsp
7071 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7072 c9956ddf 2019-09-08 stsp NULL);
7073 2af4a041 2019-05-11 jcs if (error)
7074 2ec1f75b 2019-03-26 stsp goto done;
7075 2ec1f75b 2019-03-26 stsp
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;
7080 2ec1f75b 2019-03-26 stsp
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;
7084 17ed4618 2019-06-02 stsp
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;
7094 f2a9dc41 2019-12-13 tracey }
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;
7099 f2a9dc41 2019-12-13 tracey }
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;
7104 f2a9dc41 2019-12-13 tracey }
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;
7110 f2a9dc41 2019-12-13 tracey }
7111 f2a9dc41 2019-12-13 tracey }
7112 f2a9dc41 2019-12-13 tracey }
7113 f2a9dc41 2019-12-13 tracey
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);
7117 a129376b 2019-03-28 stsp done:
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;
7122 1d0f4054 2021-06-17 stsp }
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;
7130 069bbb86 2022-03-07 thomas }
7131 069bbb86 2022-03-07 thomas
7132 069bbb86 2022-03-07 thomas __dead static void
7133 069bbb86 2022-03-07 thomas usage_patch(void)
7134 069bbb86 2022-03-07 thomas {
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);
7138 069bbb86 2022-03-07 thomas }
7139 069bbb86 2022-03-07 thomas
7140 069bbb86 2022-03-07 thomas static const struct got_error *
7141 069bbb86 2022-03-07 thomas patch_from_stdin(int *patchfd)
7142 069bbb86 2022-03-07 thomas {
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;
7147 069bbb86 2022-03-07 thomas
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);
7154 069bbb86 2022-03-07 thomas
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);
7158 069bbb86 2022-03-07 thomas
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");
7163 069bbb86 2022-03-07 thomas break;
7164 069bbb86 2022-03-07 thomas }
7165 069bbb86 2022-03-07 thomas if (r == 0)
7166 069bbb86 2022-03-07 thomas break;
7167 069bbb86 2022-03-07 thomas if (write(*patchfd, buf, r) == -1) {
7168 069bbb86 2022-03-07 thomas err = got_error_from_errno("write");
7169 069bbb86 2022-03-07 thomas break;
7170 069bbb86 2022-03-07 thomas }
7171 069bbb86 2022-03-07 thomas }
7172 069bbb86 2022-03-07 thomas
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);
7176 069bbb86 2022-03-07 thomas
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");
7179 edb9a180 2022-03-13 thomas
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;
7183 edb9a180 2022-03-13 thomas }
7184 edb9a180 2022-03-13 thomas
7185 edb9a180 2022-03-13 thomas return err;
7186 069bbb86 2022-03-07 thomas }
7187 069bbb86 2022-03-07 thomas
7188 069bbb86 2022-03-07 thomas static const struct got_error *
7189 069bbb86 2022-03-07 thomas cmd_patch(int argc, char *argv[])
7190 069bbb86 2022-03-07 thomas {
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;
7195 069bbb86 2022-03-07 thomas int ch;
7196 069bbb86 2022-03-07 thomas int patchfd;
7197 069bbb86 2022-03-07 thomas
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 */
7203 069bbb86 2022-03-07 thomas }
7204 069bbb86 2022-03-07 thomas }
7205 069bbb86 2022-03-07 thomas
7206 069bbb86 2022-03-07 thomas argc -= optind;
7207 069bbb86 2022-03-07 thomas argv += optind;
7208 069bbb86 2022-03-07 thomas
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;
7218 069bbb86 2022-03-07 thomas }
7219 069bbb86 2022-03-07 thomas } else
7220 069bbb86 2022-03-07 thomas usage_patch();
7221 069bbb86 2022-03-07 thomas
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;
7225 069bbb86 2022-03-07 thomas }
7226 069bbb86 2022-03-07 thomas
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;
7230 069bbb86 2022-03-07 thomas
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;
7235 069bbb86 2022-03-07 thomas
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;
7240 069bbb86 2022-03-07 thomas
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");
7245 069bbb86 2022-03-07 thomas #endif
7246 069bbb86 2022-03-07 thomas
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);
7249 069bbb86 2022-03-07 thomas
7250 069bbb86 2022-03-07 thomas done:
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;
7255 069bbb86 2022-03-07 thomas }
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;
7260 069bbb86 2022-03-07 thomas }
7261 a129376b 2019-03-28 stsp free(cwd);
7262 a129376b 2019-03-28 stsp return error;
7263 a129376b 2019-03-28 stsp }
7264 a129376b 2019-03-28 stsp
7265 a129376b 2019-03-28 stsp __dead static void
7266 a129376b 2019-03-28 stsp usage_revert(void)
7267 a129376b 2019-03-28 stsp {
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());
7270 a129376b 2019-03-28 stsp exit(1);
7271 a129376b 2019-03-28 stsp }
7272 a129376b 2019-03-28 stsp
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)
7275 a129376b 2019-03-28 stsp {
7276 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
7277 fb9704af 2020-01-27 stsp return NULL;
7278 fb9704af 2020-01-27 stsp
7279 a129376b 2019-03-28 stsp while (path[0] == '/')
7280 a129376b 2019-03-28 stsp path++;
7281 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
7282 33aa809d 2019-08-08 stsp return NULL;
7283 33aa809d 2019-08-08 stsp }
7284 33aa809d 2019-08-08 stsp
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;
7288 33aa809d 2019-08-08 stsp };
7289 33aa809d 2019-08-08 stsp
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)
7293 33aa809d 2019-08-08 stsp {
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;
7297 33aa809d 2019-08-08 stsp
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);
7301 33aa809d 2019-08-08 stsp break;
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);
7304 33aa809d 2019-08-08 stsp break;
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);
7316 33aa809d 2019-08-08 stsp break;
7317 33aa809d 2019-08-08 stsp default:
7318 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
7319 33aa809d 2019-08-08 stsp }
7320 33aa809d 2019-08-08 stsp
7321 33aa809d 2019-08-08 stsp return NULL;
7322 33aa809d 2019-08-08 stsp }
7323 33aa809d 2019-08-08 stsp
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)
7327 33aa809d 2019-08-08 stsp {
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;
7334 33aa809d 2019-08-08 stsp
7335 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
7336 33aa809d 2019-08-08 stsp
7337 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
7338 33aa809d 2019-08-08 stsp char *nl;
7339 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
7340 33aa809d 2019-08-08 stsp a->action);
7341 33aa809d 2019-08-08 stsp if (err)
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;
7348 33aa809d 2019-08-08 stsp }
7349 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
7350 33aa809d 2019-08-08 stsp if (nl)
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");
7362 33aa809d 2019-08-08 stsp } else
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;
7366 33aa809d 2019-08-08 stsp }
7367 33aa809d 2019-08-08 stsp
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);
7371 33aa809d 2019-08-08 stsp if (err)
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 = ' ';
7380 33aa809d 2019-08-08 stsp }
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 = ' ';
7384 33aa809d 2019-08-08 stsp }
7385 33aa809d 2019-08-08 stsp }
7386 33aa809d 2019-08-08 stsp
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;
7393 33aa809d 2019-08-08 stsp
7394 1ee397ad 2019-07-12 stsp return NULL;
7395 a129376b 2019-03-28 stsp }
7396 a129376b 2019-03-28 stsp
7397 a129376b 2019-03-28 stsp static const struct got_error *
7398 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
7399 a129376b 2019-03-28 stsp {
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;
7410 a129376b 2019-03-28 stsp
7411 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
7412 e20a8b6f 2019-06-04 stsp
7413 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
7414 a129376b 2019-03-28 stsp switch (ch) {
7415 33aa809d 2019-08-08 stsp case 'p':
7416 33aa809d 2019-08-08 stsp pflag = 1;
7417 33aa809d 2019-08-08 stsp break;
7418 33aa809d 2019-08-08 stsp case 'F':
7419 33aa809d 2019-08-08 stsp patch_script_path = optarg;
7420 33aa809d 2019-08-08 stsp break;
7421 0f6d7415 2019-08-08 stsp case 'R':
7422 0f6d7415 2019-08-08 stsp can_recurse = 1;
7423 0f6d7415 2019-08-08 stsp break;
7424 a129376b 2019-03-28 stsp default:
7425 a129376b 2019-03-28 stsp usage_revert();
7426 a129376b 2019-03-28 stsp /* NOTREACHED */
7427 a129376b 2019-03-28 stsp }
7428 a129376b 2019-03-28 stsp }
7429 a129376b 2019-03-28 stsp
7430 a129376b 2019-03-28 stsp argc -= optind;
7431 a129376b 2019-03-28 stsp argv += optind;
7432 a129376b 2019-03-28 stsp
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");
7437 43012d58 2019-07-14 stsp #endif
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");
7442 a129376b 2019-03-28 stsp
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;
7447 a129376b 2019-03-28 stsp }
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;
7453 fa51e947 2020-03-27 stsp }
7454 a129376b 2019-03-28 stsp
7455 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7456 c9956ddf 2019-09-08 stsp NULL);
7457 a129376b 2019-03-28 stsp if (error != NULL)
7458 a129376b 2019-03-28 stsp goto done;
7459 a129376b 2019-03-28 stsp
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;
7466 33aa809d 2019-08-08 stsp }
7467 33aa809d 2019-08-08 stsp }
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;
7472 a129376b 2019-03-28 stsp
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;
7476 00db391e 2019-08-03 stsp
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;
7486 0f6d7415 2019-08-08 stsp }
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);
7490 0f6d7415 2019-08-08 stsp continue;
7491 0f6d7415 2019-08-08 stsp }
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;
7496 0f6d7415 2019-08-08 stsp }
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;
7502 0f6d7415 2019-08-08 stsp }
7503 0f6d7415 2019-08-08 stsp }
7504 0f6d7415 2019-08-08 stsp }
7505 0f6d7415 2019-08-08 stsp
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);
7510 2ec1f75b 2019-03-26 stsp done:
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;
7518 1d0f4054 2021-06-17 stsp }
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;
7524 c4296144 2019-05-09 stsp }
7525 c4296144 2019-05-09 stsp
7526 c4296144 2019-05-09 stsp __dead static void
7527 c4296144 2019-05-09 stsp usage_commit(void)
7528 c4296144 2019-05-09 stsp {
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());
7531 c4296144 2019-05-09 stsp exit(1);
7532 33ad4cbe 2019-05-12 jcs }
7533 33ad4cbe 2019-05-12 jcs
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;
7543 e2ba3d07 2019-05-13 stsp
7544 e2ba3d07 2019-05-13 stsp };
7545 28cf319f 2021-01-28 stsp
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)
7548 28cf319f 2021-01-28 stsp {
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;
7552 28cf319f 2021-01-28 stsp size_t r;
7553 28cf319f 2021-01-28 stsp
7554 28cf319f 2021-01-28 stsp *logmsg = NULL;
7555 28cf319f 2021-01-28 stsp memset(&sb, 0, sizeof(sb));
7556 28cf319f 2021-01-28 stsp
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);
7560 28cf319f 2021-01-28 stsp
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;
7564 28cf319f 2021-01-28 stsp }
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;
7568 28cf319f 2021-01-28 stsp }
7569 28cf319f 2021-01-28 stsp
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;
7574 28cf319f 2021-01-28 stsp }
7575 28cf319f 2021-01-28 stsp
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);
7580 28cf319f 2021-01-28 stsp else
7581 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_IO);
7582 28cf319f 2021-01-28 stsp goto done;
7583 28cf319f 2021-01-28 stsp }
7584 28cf319f 2021-01-28 stsp (*logmsg)[sb.st_size] = '\0';
7585 28cf319f 2021-01-28 stsp done:
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;
7591 28cf319f 2021-01-28 stsp }
7592 28cf319f 2021-01-28 stsp return err;
7593 28cf319f 2021-01-28 stsp
7594 28cf319f 2021-01-28 stsp }
7595 e0870e44 2019-05-13 stsp
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,
7598 33ad4cbe 2019-05-12 jcs void *arg)
7599 33ad4cbe 2019-05-12 jcs {
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;
7608 33ad4cbe 2019-05-12 jcs
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);
7619 33ad4cbe 2019-05-12 jcs
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");
7622 76d98825 2019-06-03 stsp
7623 28cf319f 2021-01-28 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7624 28cf319f 2021-01-28 stsp if (err)
7625 28cf319f 2021-01-28 stsp goto done;
7626 28cf319f 2021-01-28 stsp
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);
7630 28cf319f 2021-01-28 stsp if (err)
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;
7636 28cf319f 2021-01-28 stsp }
7637 28cf319f 2021-01-28 stsp free(msg);
7638 28cf319f 2021-01-28 stsp }
7639 28cf319f 2021-01-28 stsp
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;
7646 28cf319f 2021-01-28 stsp }
7647 33ad4cbe 2019-05-12 jcs
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;
7651 97972933 2020-09-11 stsp }
7652 33ad4cbe 2019-05-12 jcs
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));
7658 33ad4cbe 2019-05-12 jcs }
7659 33ad4cbe 2019-05-12 jcs
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);
7662 33ad4cbe 2019-05-12 jcs done:
7663 76d98825 2019-06-03 stsp free(initial_content);
7664 e0870e44 2019-05-13 stsp free(template);
7665 314a6357 2019-05-13 stsp
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);
7668 97972933 2020-09-11 stsp
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;
7675 3ce1b845 2019-07-15 stsp }
7676 33ad4cbe 2019-05-12 jcs return err;
7677 6bad629b 2019-02-04 stsp }
7678 c4296144 2019-05-09 stsp
7679 c4296144 2019-05-09 stsp static const struct got_error *
7680 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
7681 c4296144 2019-05-09 stsp {
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;
7694 5c1e53bc 2019-07-28 stsp
7695 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
7696 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
7697 c4296144 2019-05-09 stsp
7698 28cf319f 2021-01-28 stsp while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7699 c4296144 2019-05-09 stsp switch (ch) {
7700 28cf319f 2021-01-28 stsp case 'F':
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",
7706 28cf319f 2021-01-28 stsp optarg);
7707 28cf319f 2021-01-28 stsp break;
7708 c4296144 2019-05-09 stsp case 'm':
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;
7712 28cf319f 2021-01-28 stsp break;
7713 28cf319f 2021-01-28 stsp case 'N':
7714 28cf319f 2021-01-28 stsp non_interactive = 1;
7715 c4296144 2019-05-09 stsp break;
7716 35213c7c 2020-07-23 stsp case 'S':
7717 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
7718 35213c7c 2020-07-23 stsp break;
7719 c4296144 2019-05-09 stsp default:
7720 c4296144 2019-05-09 stsp usage_commit();
7721 c4296144 2019-05-09 stsp /* NOTREACHED */
7722 c4296144 2019-05-09 stsp }
7723 c4296144 2019-05-09 stsp }
7724 c4296144 2019-05-09 stsp
7725 c4296144 2019-05-09 stsp argc -= optind;
7726 c4296144 2019-05-09 stsp argv += optind;
7727 c4296144 2019-05-09 stsp
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");
7732 43012d58 2019-07-14 stsp #endif
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;
7737 c4296144 2019-05-09 stsp }
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;
7743 fa51e947 2020-03-27 stsp }
7744 7d5807f4 2019-07-11 stsp
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;
7751 a698f62e 2019-07-25 stsp }
7752 5c1e53bc 2019-07-28 stsp
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;
7757 c4296144 2019-05-09 stsp
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;
7765 c4296144 2019-05-09 stsp
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;
7772 10604dce 2021-09-24 thomas }
7773 10604dce 2021-09-24 thomas
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;
7777 aba9c984 2019-09-08 stsp
7778 314a6357 2019-05-13 stsp /*
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.
7781 314a6357 2019-05-13 stsp */
7782 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
7783 314a6357 2019-05-13 stsp error = get_editor(&editor);
7784 314a6357 2019-05-13 stsp else
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;
7789 c4296144 2019-05-09 stsp
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;
7793 087fb88c 2019-08-04 stsp
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;
7804 916f288c 2019-07-30 stsp }
7805 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
7806 916f288c 2019-07-30 stsp }
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;
7816 e0870e44 2019-05-13 stsp }
7817 c4296144 2019-05-09 stsp
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);
7822 c4296144 2019-05-09 stsp done:
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;
7834 1d0f4054 2021-06-17 stsp }
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;
7844 f8a36e22 2021-08-26 stsp }
7845 f8a36e22 2021-08-26 stsp
7846 f8a36e22 2021-08-26 stsp __dead static void
7847 f8a36e22 2021-08-26 stsp usage_send(void)
7848 f8a36e22 2021-08-26 stsp {
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());
7852 f8a36e22 2021-08-26 stsp exit(1);
7853 f8a36e22 2021-08-26 stsp }
7854 f8a36e22 2021-08-26 stsp
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;
7866 f8a36e22 2021-08-26 stsp };
7867 f8a36e22 2021-08-26 stsp
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)
7872 f8a36e22 2021-08-26 stsp {
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;
7879 f8a36e22 2021-08-26 stsp
7880 f8a36e22 2021-08-26 stsp if (a->verbosity < 0)
7881 f8a36e22 2021-08-26 stsp return NULL;
7882 f8a36e22 2021-08-26 stsp
7883 f8a36e22 2021-08-26 stsp if (refname) {
7884 f8a36e22 2021-08-26 stsp const char *status = success ? "accepted" : "rejected";
7885 f8a36e22 2021-08-26 stsp
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;
7894 f8a36e22 2021-08-26 stsp break;
7895 f8a36e22 2021-08-26 stsp }
7896 f8a36e22 2021-08-26 stsp }
7897 f8a36e22 2021-08-26 stsp }
7898 f8a36e22 2021-08-26 stsp
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;
7904 f8a36e22 2021-08-26 stsp }
7905 f8a36e22 2021-08-26 stsp
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");
7910 f8a36e22 2021-08-26 stsp
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;
7914 f8a36e22 2021-08-26 stsp }
7915 f8a36e22 2021-08-26 stsp
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;
7920 f8a36e22 2021-08-26 stsp }
7921 f8a36e22 2021-08-26 stsp
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);
7927 f8a36e22 2021-08-26 stsp }
7928 f8a36e22 2021-08-26 stsp
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;
7937 f8a36e22 2021-08-26 stsp }
7938 f8a36e22 2021-08-26 stsp }
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;
7947 f8a36e22 2021-08-26 stsp }
7948 f8a36e22 2021-08-26 stsp }
7949 f8a36e22 2021-08-26 stsp }
7950 f8a36e22 2021-08-26 stsp
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;
7960 f8a36e22 2021-08-26 stsp }
7961 f8a36e22 2021-08-26 stsp a->sent_something = 1;
7962 f8a36e22 2021-08-26 stsp }
7963 f8a36e22 2021-08-26 stsp
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);
7985 f8a36e22 2021-08-26 stsp }
7986 f8a36e22 2021-08-26 stsp return NULL;
7987 f8a36e22 2021-08-26 stsp }
7988 f8a36e22 2021-08-26 stsp
7989 f8a36e22 2021-08-26 stsp static const struct got_error *
7990 f8a36e22 2021-08-26 stsp cmd_send(int argc, char *argv[])
7991 f8a36e22 2021-08-26 stsp {
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;
8016 f8a36e22 2021-08-26 stsp
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);
8023 f8a36e22 2021-08-26 stsp
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) {
8026 f8a36e22 2021-08-26 stsp case 'a':
8027 f8a36e22 2021-08-26 stsp send_all_branches = 1;
8028 f8a36e22 2021-08-26 stsp break;
8029 f8a36e22 2021-08-26 stsp case 'b':
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++;
8034 f8a36e22 2021-08-26 stsp break;
8035 f8a36e22 2021-08-26 stsp case 'd':
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;
8039 f8a36e22 2021-08-26 stsp break;
8040 f8a36e22 2021-08-26 stsp case 'f':
8041 f8a36e22 2021-08-26 stsp overwrite_refs = 1;
8042 f8a36e22 2021-08-26 stsp break;
8043 f8a36e22 2021-08-26 stsp case 'r':
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",
8047 f8a36e22 2021-08-26 stsp optarg);
8048 f8a36e22 2021-08-26 stsp got_path_strip_trailing_slashes(repo_path);
8049 f8a36e22 2021-08-26 stsp break;
8050 f8a36e22 2021-08-26 stsp case 't':
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;
8054 f8a36e22 2021-08-26 stsp ntags++;
8055 f8a36e22 2021-08-26 stsp break;
8056 f8a36e22 2021-08-26 stsp case 'T':
8057 f8a36e22 2021-08-26 stsp send_all_tags = 1;
8058 f8a36e22 2021-08-26 stsp break;
8059 f8a36e22 2021-08-26 stsp case 'v':
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++;
8064 f8a36e22 2021-08-26 stsp break;
8065 f8a36e22 2021-08-26 stsp case 'q':
8066 f8a36e22 2021-08-26 stsp verbosity = -1;
8067 f8a36e22 2021-08-26 stsp break;
8068 f8a36e22 2021-08-26 stsp default:
8069 f8a36e22 2021-08-26 stsp usage_send();
8070 f8a36e22 2021-08-26 stsp /* NOTREACHED */
8071 f8a36e22 2021-08-26 stsp }
8072 f8a36e22 2021-08-26 stsp }
8073 f8a36e22 2021-08-26 stsp argc -= optind;
8074 f8a36e22 2021-08-26 stsp argv += optind;
8075 f8a36e22 2021-08-26 stsp
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');
8080 f8a36e22 2021-08-26 stsp
8081 f8a36e22 2021-08-26 stsp
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];
8086 f8a36e22 2021-08-26 stsp else
8087 f8a36e22 2021-08-26 stsp usage_send();
8088 f8a36e22 2021-08-26 stsp
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;
8093 f8a36e22 2021-08-26 stsp }
8094 f8a36e22 2021-08-26 stsp
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;
8099 f8a36e22 2021-08-26 stsp else
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;
8108 f8a36e22 2021-08-26 stsp } else {
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;
8113 f8a36e22 2021-08-26 stsp }
8114 f8a36e22 2021-08-26 stsp }
8115 f8a36e22 2021-08-26 stsp }
8116 f8a36e22 2021-08-26 stsp
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;
8120 f8a36e22 2021-08-26 stsp
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];
8129 f8a36e22 2021-08-26 stsp break;
8130 f8a36e22 2021-08-26 stsp }
8131 f8a36e22 2021-08-26 stsp }
8132 f8a36e22 2021-08-26 stsp }
8133 f8a36e22 2021-08-26 stsp }
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];
8142 f8a36e22 2021-08-26 stsp break;
8143 f8a36e22 2021-08-26 stsp }
8144 f8a36e22 2021-08-26 stsp }
8145 f8a36e22 2021-08-26 stsp }
8146 f8a36e22 2021-08-26 stsp }
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];
8152 f8a36e22 2021-08-26 stsp break;
8153 f8a36e22 2021-08-26 stsp }
8154 f8a36e22 2021-08-26 stsp }
8155 f8a36e22 2021-08-26 stsp }
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;
8159 f8a36e22 2021-08-26 stsp }
8160 f8a36e22 2021-08-26 stsp
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;
8165 f8a36e22 2021-08-26 stsp
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");
8171 f8a36e22 2021-08-26 stsp #endif
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");
8178 f8a36e22 2021-08-26 stsp #endif
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;
8183 f8a36e22 2021-08-26 stsp } else {
8184 f8a36e22 2021-08-26 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
8185 f8a36e22 2021-08-26 stsp goto done;
8186 f8a36e22 2021-08-26 stsp }
8187 f8a36e22 2021-08-26 stsp
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;
8191 d65a88a2 2021-09-05 stsp
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;
8195 f8a36e22 2021-08-26 stsp
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++;
8208 f8a36e22 2021-08-26 stsp }
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);
8213 eac1df47 2021-09-01 stsp }
8214 f8a36e22 2021-08-26 stsp }
8215 f8a36e22 2021-08-26 stsp
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;
8227 f8a36e22 2021-08-26 stsp ntags++;
8228 f8a36e22 2021-08-26 stsp }
8229 f8a36e22 2021-08-26 stsp }
8230 f8a36e22 2021-08-26 stsp
8231 f8a36e22 2021-08-26 stsp /*
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.
8235 f8a36e22 2021-08-26 stsp */
8236 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_args, entry) {
8237 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
8238 f8a36e22 2021-08-26 stsp char *s;
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;
8245 f8a36e22 2021-08-26 stsp }
8246 f8a36e22 2021-08-26 stsp } else {
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;
8250 f8a36e22 2021-08-26 stsp }
8251 f8a36e22 2021-08-26 stsp }
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 */)
8254 f8a36e22 2021-08-26 stsp free(s);
8255 f8a36e22 2021-08-26 stsp if (error)
8256 f8a36e22 2021-08-26 stsp goto done;
8257 f8a36e22 2021-08-26 stsp ndelete_branches++;
8258 f8a36e22 2021-08-26 stsp }
8259 f8a36e22 2021-08-26 stsp
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);
8265 f8a36e22 2021-08-26 stsp else
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;
8274 f8a36e22 2021-08-26 stsp } else
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),
8277 abc59930 2021-09-05 naddy NULL);
8278 f8a36e22 2021-08-26 stsp if (error)
8279 f8a36e22 2021-08-26 stsp goto done;
8280 f8a36e22 2021-08-26 stsp nbranches++;
8281 f8a36e22 2021-08-26 stsp }
8282 f8a36e22 2021-08-26 stsp
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 : "");
8286 f8a36e22 2021-08-26 stsp
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;
8291 f8a36e22 2021-08-26 stsp
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");
8307 f8a36e22 2021-08-26 stsp done:
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");
8313 f8a36e22 2021-08-26 stsp }
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;
8320 f8a36e22 2021-08-26 stsp }
8321 f8a36e22 2021-08-26 stsp if (worktree)
8322 f8a36e22 2021-08-26 stsp got_worktree_close(worktree);
8323 f8a36e22 2021-08-26 stsp if (ref)
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;
8341 234035bc 2019-06-01 stsp }
8342 234035bc 2019-06-01 stsp
8343 234035bc 2019-06-01 stsp __dead static void
8344 234035bc 2019-06-01 stsp usage_cherrypick(void)
8345 234035bc 2019-06-01 stsp {
8346 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
8347 234035bc 2019-06-01 stsp exit(1);
8348 234035bc 2019-06-01 stsp }
8349 234035bc 2019-06-01 stsp
8350 234035bc 2019-06-01 stsp static const struct got_error *
8351 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
8352 234035bc 2019-06-01 stsp {
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;
8360 9627c110 2020-04-18 stsp int ch;
8361 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8362 234035bc 2019-06-01 stsp
8363 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8364 234035bc 2019-06-01 stsp switch (ch) {
8365 234035bc 2019-06-01 stsp default:
8366 234035bc 2019-06-01 stsp usage_cherrypick();
8367 234035bc 2019-06-01 stsp /* NOTREACHED */
8368 234035bc 2019-06-01 stsp }
8369 234035bc 2019-06-01 stsp }
8370 234035bc 2019-06-01 stsp
8371 234035bc 2019-06-01 stsp argc -= optind;
8372 234035bc 2019-06-01 stsp argv += optind;
8373 234035bc 2019-06-01 stsp
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");
8378 43012d58 2019-07-14 stsp #endif
8379 234035bc 2019-06-01 stsp if (argc != 1)
8380 234035bc 2019-06-01 stsp usage_cherrypick();
8381 234035bc 2019-06-01 stsp
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;
8386 234035bc 2019-06-01 stsp }
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",
8391 fa51e947 2020-03-27 stsp cwd);
8392 234035bc 2019-06-01 stsp goto done;
8393 fa51e947 2020-03-27 stsp }
8394 234035bc 2019-06-01 stsp
8395 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8396 c9956ddf 2019-09-08 stsp NULL);
8397 234035bc 2019-06-01 stsp if (error != NULL)
8398 234035bc 2019-06-01 stsp goto done;
8399 234035bc 2019-06-01 stsp
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;
8404 234035bc 2019-06-01 stsp
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;
8412 234035bc 2019-06-01 stsp
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,
8420 03415a1a 2019-06-02 stsp NULL);
8421 234035bc 2019-06-01 stsp if (error != NULL)
8422 234035bc 2019-06-01 stsp goto done;
8423 234035bc 2019-06-01 stsp
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);
8427 234035bc 2019-06-01 stsp done:
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;
8437 1d0f4054 2021-06-17 stsp }
8438 c4296144 2019-05-09 stsp return error;
8439 c4296144 2019-05-09 stsp }
8440 5ef14e63 2019-06-02 stsp
8441 5ef14e63 2019-06-02 stsp __dead static void
8442 5ef14e63 2019-06-02 stsp usage_backout(void)
8443 5ef14e63 2019-06-02 stsp {
8444 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
8445 5ef14e63 2019-06-02 stsp exit(1);
8446 5ef14e63 2019-06-02 stsp }
8447 5ef14e63 2019-06-02 stsp
8448 5ef14e63 2019-06-02 stsp static const struct got_error *
8449 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
8450 5ef14e63 2019-06-02 stsp {
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;
8458 9627c110 2020-04-18 stsp int ch;
8459 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8460 5ef14e63 2019-06-02 stsp
8461 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8462 5ef14e63 2019-06-02 stsp switch (ch) {
8463 5ef14e63 2019-06-02 stsp default:
8464 5ef14e63 2019-06-02 stsp usage_backout();
8465 5ef14e63 2019-06-02 stsp /* NOTREACHED */
8466 5ef14e63 2019-06-02 stsp }
8467 5ef14e63 2019-06-02 stsp }
8468 5ef14e63 2019-06-02 stsp
8469 5ef14e63 2019-06-02 stsp argc -= optind;
8470 5ef14e63 2019-06-02 stsp argv += optind;
8471 5ef14e63 2019-06-02 stsp
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");
8476 43012d58 2019-07-14 stsp #endif
8477 5ef14e63 2019-06-02 stsp if (argc != 1)
8478 5ef14e63 2019-06-02 stsp usage_backout();
8479 5ef14e63 2019-06-02 stsp
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;
8484 5ef14e63 2019-06-02 stsp }
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;
8490 fa51e947 2020-03-27 stsp }
8491 5ef14e63 2019-06-02 stsp
8492 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8493 c9956ddf 2019-09-08 stsp NULL);
8494 5ef14e63 2019-06-02 stsp if (error != NULL)
8495 5ef14e63 2019-06-02 stsp goto done;
8496 5ef14e63 2019-06-02 stsp
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;
8501 5ef14e63 2019-06-02 stsp
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;
8509 5ef14e63 2019-06-02 stsp
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;
8517 5ef14e63 2019-06-02 stsp }
8518 5ef14e63 2019-06-02 stsp
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;
8524 5ef14e63 2019-06-02 stsp
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);
8528 5ef14e63 2019-06-02 stsp done:
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;
8538 1d0f4054 2021-06-17 stsp }
8539 818c7501 2019-07-11 stsp return error;
8540 818c7501 2019-07-11 stsp }
8541 818c7501 2019-07-11 stsp
8542 818c7501 2019-07-11 stsp __dead static void
8543 818c7501 2019-07-11 stsp usage_rebase(void)
8544 818c7501 2019-07-11 stsp {
8545 643b85bc 2021-07-16 stsp fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
8546 af54c8f8 2019-07-11 stsp getprogname());
8547 818c7501 2019-07-11 stsp exit(1);
8548 0ebf8283 2019-07-24 stsp }
8549 0ebf8283 2019-07-24 stsp
8550 0ebf8283 2019-07-24 stsp void
8551 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
8552 0ebf8283 2019-07-24 stsp {
8553 0ebf8283 2019-07-24 stsp char *nl;
8554 0ebf8283 2019-07-24 stsp size_t len;
8555 0ebf8283 2019-07-24 stsp
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');
8561 0ebf8283 2019-07-24 stsp if (nl)
8562 0ebf8283 2019-07-24 stsp *nl = '\0';
8563 0ebf8283 2019-07-24 stsp }
8564 0ebf8283 2019-07-24 stsp
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)
8567 0ebf8283 2019-07-24 stsp {
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;
8571 0ebf8283 2019-07-24 stsp
8572 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
8573 5943eee2 2019-08-13 stsp if (err)
8574 5943eee2 2019-08-13 stsp return err;
8575 0ebf8283 2019-07-24 stsp
8576 5943eee2 2019-08-13 stsp s = logmsg0;
8577 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
8578 5943eee2 2019-08-13 stsp s++;
8579 5943eee2 2019-08-13 stsp
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;
8584 5943eee2 2019-08-13 stsp }
8585 0ebf8283 2019-07-24 stsp
8586 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
8587 5943eee2 2019-08-13 stsp done:
8588 5943eee2 2019-08-13 stsp free(logmsg0);
8589 a0ea4fc0 2020-02-28 stsp return err;
8590 a0ea4fc0 2020-02-28 stsp }
8591 a0ea4fc0 2020-02-28 stsp
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)
8595 a0ea4fc0 2020-02-28 stsp {
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;
8599 a0ea4fc0 2020-02-28 stsp
8600 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
8601 a0ea4fc0 2020-02-28 stsp if (err)
8602 a0ea4fc0 2020-02-28 stsp return err;
8603 a0ea4fc0 2020-02-28 stsp
8604 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
8605 a0ea4fc0 2020-02-28 stsp if (err)
8606 a0ea4fc0 2020-02-28 stsp goto done;
8607 a0ea4fc0 2020-02-28 stsp
8608 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
8609 a0ea4fc0 2020-02-28 stsp
8610 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
8611 a0ea4fc0 2020-02-28 stsp if (err)
8612 a0ea4fc0 2020-02-28 stsp goto done;
8613 a0ea4fc0 2020-02-28 stsp
8614 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
8615 a0ea4fc0 2020-02-28 stsp done:
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;
8620 818c7501 2019-07-11 stsp }
8621 818c7501 2019-07-11 stsp
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)
8625 818c7501 2019-07-11 stsp {
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;
8628 818c7501 2019-07-11 stsp
8629 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
8630 818c7501 2019-07-11 stsp if (err)
8631 818c7501 2019-07-11 stsp goto done;
8632 818c7501 2019-07-11 stsp
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);
8635 ff0d2220 2019-07-11 stsp if (err)
8636 ff0d2220 2019-07-11 stsp goto done;
8637 ff0d2220 2019-07-11 stsp }
8638 818c7501 2019-07-11 stsp
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';
8642 0ebf8283 2019-07-24 stsp
8643 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8644 0ebf8283 2019-07-24 stsp if (err)
8645 0ebf8283 2019-07-24 stsp goto done;
8646 0ebf8283 2019-07-24 stsp
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);
8649 818c7501 2019-07-11 stsp done:
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;
8654 818c7501 2019-07-11 stsp }
8655 818c7501 2019-07-11 stsp
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)
8661 818c7501 2019-07-11 stsp {
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);
8665 818c7501 2019-07-11 stsp }
8666 818c7501 2019-07-11 stsp
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)
8672 ff0d2220 2019-07-11 stsp {
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;
8676 ff0d2220 2019-07-11 stsp
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;
8680 ff0d2220 2019-07-11 stsp
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);
8687 ff0d2220 2019-07-11 stsp } else {
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);
8690 ff0d2220 2019-07-11 stsp }
8691 ff0d2220 2019-07-11 stsp done:
8692 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
8693 ff0d2220 2019-07-11 stsp return error;
8694 64c6d990 2019-07-11 stsp }
8695 64c6d990 2019-07-11 stsp
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;
8700 64c6d990 2019-07-11 stsp };
8701 64c6d990 2019-07-11 stsp
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)
8707 64c6d990 2019-07-11 stsp {
8708 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
8709 64c6d990 2019-07-11 stsp
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);
8713 64c6d990 2019-07-11 stsp
8714 64c6d990 2019-07-11 stsp return NULL;
8715 64c6d990 2019-07-11 stsp }
8716 64c6d990 2019-07-11 stsp
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)
8721 64c6d990 2019-07-11 stsp {
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;
8726 64c6d990 2019-07-11 stsp
8727 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
8728 64c6d990 2019-07-11 stsp return NULL;
8729 64c6d990 2019-07-11 stsp
8730 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8731 64c6d990 2019-07-11 stsp if (err)
8732 64c6d990 2019-07-11 stsp goto done;
8733 64c6d990 2019-07-11 stsp
8734 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
8735 64c6d990 2019-07-11 stsp if (err)
8736 64c6d990 2019-07-11 stsp goto done;
8737 64c6d990 2019-07-11 stsp
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));
8740 64c6d990 2019-07-11 stsp if (err)
8741 64c6d990 2019-07-11 stsp goto done;
8742 64c6d990 2019-07-11 stsp
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));
8745 64c6d990 2019-07-11 stsp if (err)
8746 64c6d990 2019-07-11 stsp goto done;
8747 64c6d990 2019-07-11 stsp
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);
8755 64c6d990 2019-07-11 stsp done:
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;
8765 ff0d2220 2019-07-11 stsp }
8766 ff0d2220 2019-07-11 stsp
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)
8773 af61c510 2019-07-19 stsp {
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;
8779 af61c510 2019-07-19 stsp
8780 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
8781 af61c510 2019-07-19 stsp if (err)
8782 af61c510 2019-07-19 stsp return err;
8783 af61c510 2019-07-19 stsp
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);
8786 af61c510 2019-07-19 stsp if (err)
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?!?");
8797 ee780d5c 2020-01-04 stsp }
8798 ee780d5c 2020-01-04 stsp goto done;
8799 af61c510 2019-07-19 stsp } else {
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);
8802 af61c510 2019-07-19 stsp if (err)
8803 af61c510 2019-07-19 stsp goto done;
8804 af61c510 2019-07-19 stsp
8805 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
8806 af61c510 2019-07-19 stsp if (err)
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;
8810 af61c510 2019-07-19 stsp }
8811 af61c510 2019-07-19 stsp }
8812 af61c510 2019-07-19 stsp done:
8813 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
8814 af61c510 2019-07-19 stsp return err;
8815 e600f124 2021-03-21 stsp }
8816 e600f124 2021-03-21 stsp
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)
8819 e600f124 2021-03-21 stsp {
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;
8826 e600f124 2021-03-21 stsp
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);
8832 e600f124 2021-03-21 stsp
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';
8841 e600f124 2021-03-21 stsp
8842 e600f124 2021-03-21 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
8843 e600f124 2021-03-21 stsp if (err)
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')
8847 e600f124 2021-03-21 stsp logmsg++;
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';
8851 e600f124 2021-03-21 stsp
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");
8855 e600f124 2021-03-21 stsp done:
8856 e600f124 2021-03-21 stsp free(author0);
8857 e600f124 2021-03-21 stsp free(logmsg0);
8858 643b85bc 2021-07-16 stsp return err;
8859 643b85bc 2021-07-16 stsp }
8860 643b85bc 2021-07-16 stsp
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)
8864 643b85bc 2021-07-16 stsp {
8865 643b85bc 2021-07-16 stsp const struct got_error *err;
8866 643b85bc 2021-07-16 stsp char *id_str;
8867 643b85bc 2021-07-16 stsp
8868 643b85bc 2021-07-16 stsp err = got_object_id_str(&id_str, id);
8869 643b85bc 2021-07-16 stsp if (err)
8870 643b85bc 2021-07-16 stsp return err;
8871 643b85bc 2021-07-16 stsp
8872 643b85bc 2021-07-16 stsp err = got_ref_delete(ref, repo);
8873 643b85bc 2021-07-16 stsp if (err)
8874 643b85bc 2021-07-16 stsp goto done;
8875 643b85bc 2021-07-16 stsp
8876 643b85bc 2021-07-16 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
8877 643b85bc 2021-07-16 stsp done:
8878 643b85bc 2021-07-16 stsp free(id_str);
8879 e600f124 2021-03-21 stsp return err;
8880 e600f124 2021-03-21 stsp }
8881 e600f124 2021-03-21 stsp
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)
8887 e600f124 2021-03-21 stsp {
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;
8898 e600f124 2021-03-21 stsp
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");
8901 e600f124 2021-03-21 stsp
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);
8904 e600f124 2021-03-21 stsp if (err)
8905 e600f124 2021-03-21 stsp goto done;
8906 e600f124 2021-03-21 stsp
8907 e600f124 2021-03-21 stsp err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
8908 e600f124 2021-03-21 stsp if (err)
8909 e600f124 2021-03-21 stsp goto done;
8910 e600f124 2021-03-21 stsp
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);
8914 e600f124 2021-03-21 stsp if (err)
8915 e600f124 2021-03-21 stsp goto done;
8916 e600f124 2021-03-21 stsp }
8917 e600f124 2021-03-21 stsp
8918 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
8919 e600f124 2021-03-21 stsp if (err)
8920 e600f124 2021-03-21 stsp goto done;
8921 e600f124 2021-03-21 stsp
8922 e600f124 2021-03-21 stsp err = get_commit_brief_str(&new_commit_brief_str, new_commit);
8923 e600f124 2021-03-21 stsp if (err)
8924 e600f124 2021-03-21 stsp goto done;
8925 e600f124 2021-03-21 stsp
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);
8928 e600f124 2021-03-21 stsp if (err)
8929 e600f124 2021-03-21 stsp goto done;
8930 e600f124 2021-03-21 stsp
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;
8938 e600f124 2021-03-21 stsp
8939 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&yca_commit, repo, yca_id);
8940 e600f124 2021-03-21 stsp if (err)
8941 e600f124 2021-03-21 stsp goto done;
8942 e600f124 2021-03-21 stsp
8943 e600f124 2021-03-21 stsp err = get_commit_brief_str(&yca_brief_str, yca_commit);
8944 e600f124 2021-03-21 stsp if (err)
8945 e600f124 2021-03-21 stsp goto done;
8946 e600f124 2021-03-21 stsp
8947 e600f124 2021-03-21 stsp err = got_object_id_str(&yca_id_str, yca_id);
8948 e600f124 2021-03-21 stsp if (err)
8949 e600f124 2021-03-21 stsp goto done;
8950 e600f124 2021-03-21 stsp
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);
8954 e600f124 2021-03-21 stsp if (err)
8955 e600f124 2021-03-21 stsp goto done;
8956 e600f124 2021-03-21 stsp }
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);
8961 e600f124 2021-03-21 stsp }
8962 e600f124 2021-03-21 stsp done:
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);
8973 e600f124 2021-03-21 stsp
8974 e600f124 2021-03-21 stsp return NULL;
8975 af61c510 2019-07-19 stsp }
8976 af61c510 2019-07-19 stsp
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)
8981 e600f124 2021-03-21 stsp {
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;
8991 e600f124 2021-03-21 stsp
8992 e600f124 2021-03-21 stsp TAILQ_INIT(&refs);
8993 e600f124 2021-03-21 stsp TAILQ_INIT(&backup_refs);
8994 e600f124 2021-03-21 stsp
8995 e600f124 2021-03-21 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8996 e600f124 2021-03-21 stsp if (err)
8997 e600f124 2021-03-21 stsp return err;
8998 e600f124 2021-03-21 stsp
8999 e600f124 2021-03-21 stsp err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9000 e600f124 2021-03-21 stsp if (err)
9001 e600f124 2021-03-21 stsp goto done;
9002 e600f124 2021-03-21 stsp
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;
9006 e600f124 2021-03-21 stsp }
9007 e600f124 2021-03-21 stsp
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);
9010 e600f124 2021-03-21 stsp if (err)
9011 e600f124 2021-03-21 stsp goto done;
9012 e600f124 2021-03-21 stsp
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;
9016 e600f124 2021-03-21 stsp
9017 643b85bc 2021-07-16 stsp err = check_cancelled(NULL);
9018 643b85bc 2021-07-16 stsp if (err)
9019 643b85bc 2021-07-16 stsp break;
9020 643b85bc 2021-07-16 stsp
9021 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, re->ref);
9022 e600f124 2021-03-21 stsp if (err)
9023 e600f124 2021-03-21 stsp break;
9024 e600f124 2021-03-21 stsp
9025 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&old_commit, repo,
9026 e600f124 2021-03-21 stsp old_commit_id);
9027 e600f124 2021-03-21 stsp if (err)
9028 e600f124 2021-03-21 stsp break;
9029 e600f124 2021-03-21 stsp
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;
9033 e600f124 2021-03-21 stsp
9034 e600f124 2021-03-21 stsp while (refname[0] == '/')
9035 e600f124 2021-03-21 stsp refname++;
9036 e600f124 2021-03-21 stsp
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");
9040 e600f124 2021-03-21 stsp break;
9041 e600f124 2021-03-21 stsp }
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;
9046 e600f124 2021-03-21 stsp }
9047 e600f124 2021-03-21 stsp
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);
9054 643b85bc 2021-07-16 stsp } else {
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,
9057 abc59930 2021-09-05 naddy repo);
9058 643b85bc 2021-07-16 stsp }
9059 e600f124 2021-03-21 stsp if (err)
9060 e600f124 2021-03-21 stsp break;
9061 e600f124 2021-03-21 stsp }
9062 e600f124 2021-03-21 stsp
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;
9069 e600f124 2021-03-21 stsp }
9070 9e822917 2021-03-23 stsp
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);
9074 9e822917 2021-03-23 stsp }
9075 e600f124 2021-03-21 stsp done:
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;
9085 e600f124 2021-03-21 stsp }
9086 e600f124 2021-03-21 stsp
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)
9089 d52bac28 2021-10-08 thomas {
9090 d52bac28 2021-10-08 thomas /*
9091 d52bac28 2021-10-08 thomas * Unversioned files should not clutter progress output when
9092 d52bac28 2021-10-08 thomas * an operation is aborted.
9093 d52bac28 2021-10-08 thomas */
9094 d52bac28 2021-10-08 thomas if (status == GOT_STATUS_UNVERSIONED)
9095 d52bac28 2021-10-08 thomas return NULL;
9096 d52bac28 2021-10-08 thomas
9097 d52bac28 2021-10-08 thomas return update_progress(arg, status, path);
9098 d52bac28 2021-10-08 thomas }
9099 d52bac28 2021-10-08 thomas
9100 d52bac28 2021-10-08 thomas static const struct got_error *
9101 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
9102 818c7501 2019-07-11 stsp {
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;
9122 818c7501 2019-07-11 stsp
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));
9126 818c7501 2019-07-11 stsp
9127 643b85bc 2021-07-16 stsp while ((ch = getopt(argc, argv, "aclX")) != -1) {
9128 818c7501 2019-07-11 stsp switch (ch) {
9129 818c7501 2019-07-11 stsp case 'a':
9130 818c7501 2019-07-11 stsp abort_rebase = 1;
9131 818c7501 2019-07-11 stsp break;
9132 818c7501 2019-07-11 stsp case 'c':
9133 818c7501 2019-07-11 stsp continue_rebase = 1;
9134 818c7501 2019-07-11 stsp break;
9135 e600f124 2021-03-21 stsp case 'l':
9136 e600f124 2021-03-21 stsp list_backups = 1;
9137 e600f124 2021-03-21 stsp break;
9138 643b85bc 2021-07-16 stsp case 'X':
9139 643b85bc 2021-07-16 stsp delete_backups = 1;
9140 643b85bc 2021-07-16 stsp break;
9141 818c7501 2019-07-11 stsp default:
9142 818c7501 2019-07-11 stsp usage_rebase();
9143 818c7501 2019-07-11 stsp /* NOTREACHED */
9144 818c7501 2019-07-11 stsp }
9145 818c7501 2019-07-11 stsp }
9146 818c7501 2019-07-11 stsp
9147 818c7501 2019-07-11 stsp argc -= optind;
9148 818c7501 2019-07-11 stsp argv += optind;
9149 818c7501 2019-07-11 stsp
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");
9154 43012d58 2019-07-14 stsp #endif
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();
9173 e600f124 2021-03-21 stsp } else {
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();
9181 e600f124 2021-03-21 stsp }
9182 818c7501 2019-07-11 stsp
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;
9187 818c7501 2019-07-11 stsp }
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;
9193 e600f124 2021-03-21 stsp } else {
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;
9198 e600f124 2021-03-21 stsp }
9199 fa51e947 2020-03-27 stsp }
9200 818c7501 2019-07-11 stsp
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;
9205 818c7501 2019-07-11 stsp
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;
9210 7ef62c4e 2020-02-24 stsp
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 */
9216 e600f124 2021-03-21 stsp }
9217 e600f124 2021-03-21 stsp
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;
9225 10604dce 2021-09-24 thomas }
9226 10604dce 2021-09-24 thomas
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;
9234 7ef62c4e 2020-02-24 stsp }
9235 818c7501 2019-07-11 stsp
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;
9239 818c7501 2019-07-11 stsp
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;
9244 f6794adc 2019-07-23 stsp }
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 */
9259 818c7501 2019-07-11 stsp }
9260 818c7501 2019-07-11 stsp
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;
9265 f6794adc 2019-07-23 stsp }
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;
9271 818c7501 2019-07-11 stsp
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;
9276 818c7501 2019-07-11 stsp
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;
9281 818c7501 2019-07-11 stsp }
9282 818c7501 2019-07-11 stsp } else {
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;
9286 ff0d2220 2019-07-11 stsp }
9287 818c7501 2019-07-11 stsp
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;
9291 ff0d2220 2019-07-11 stsp
9292 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
9293 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
9294 a51a74b3 2019-07-27 stsp
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;
9306 818c7501 2019-07-11 stsp }
9307 818c7501 2019-07-11 stsp
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;
9313 a51a74b3 2019-07-27 stsp } else {
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);
9346 82d979c5 2021-11-04 thomas } else
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;
9350 a51a74b3 2019-07-27 stsp }
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;
9355 818c7501 2019-07-11 stsp }
9356 818c7501 2019-07-11 stsp
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;
9361 818c7501 2019-07-11 stsp
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);
9373 9627c110 2020-04-18 stsp
9374 fc66b545 2019-08-12 stsp }
9375 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
9376 fc66b545 2019-08-12 stsp goto done;
9377 fc66b545 2019-08-12 stsp }
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;
9385 64c6d990 2019-07-11 stsp
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;
9392 38b0338b 2019-11-29 stsp } else {
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;
9410 38b0338b 2019-11-29 stsp }
9411 818c7501 2019-07-11 stsp }
9412 818c7501 2019-07-11 stsp
9413 818c7501 2019-07-11 stsp pid = NULL;
9414 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &commits, entry) {
9415 9627c110 2020-04-18 stsp
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;
9419 818c7501 2019-07-11 stsp
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;
9426 9627c110 2020-04-18 stsp
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,
9432 bb494413 2021-09-28 thomas repo);
9433 bb494413 2021-09-28 thomas if (error)
9434 bb494413 2021-09-28 thomas goto done;
9435 bb494413 2021-09-28 thomas }
9436 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9437 818c7501 2019-07-11 stsp break;
9438 01757395 2019-07-12 stsp }
9439 818c7501 2019-07-11 stsp
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;
9445 818c7501 2019-07-11 stsp }
9446 818c7501 2019-07-11 stsp
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");
9470 bb494413 2021-09-28 thomas }
9471 818c7501 2019-07-11 stsp } else
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);
9474 818c7501 2019-07-11 stsp done:
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;
9493 1d0f4054 2021-06-17 stsp }
9494 5ef14e63 2019-06-02 stsp return error;
9495 0ebf8283 2019-07-24 stsp }
9496 0ebf8283 2019-07-24 stsp
9497 0ebf8283 2019-07-24 stsp __dead static void
9498 0ebf8283 2019-07-24 stsp usage_histedit(void)
9499 0ebf8283 2019-07-24 stsp {
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());
9503 0ebf8283 2019-07-24 stsp exit(1);
9504 0ebf8283 2019-07-24 stsp }
9505 0ebf8283 2019-07-24 stsp
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'
9511 0ebf8283 2019-07-24 stsp
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)" },
9524 0ebf8283 2019-07-24 stsp };
9525 0ebf8283 2019-07-24 stsp
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;
9531 0ebf8283 2019-07-24 stsp };
9532 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
9533 0ebf8283 2019-07-24 stsp
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)
9537 0ebf8283 2019-07-24 stsp {
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;
9541 8138f3e1 2019-08-11 stsp int n;
9542 0ebf8283 2019-07-24 stsp
9543 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
9544 0ebf8283 2019-07-24 stsp if (err)
9545 0ebf8283 2019-07-24 stsp goto done;
9546 0ebf8283 2019-07-24 stsp
9547 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
9548 0ebf8283 2019-07-24 stsp if (err)
9549 0ebf8283 2019-07-24 stsp goto done;
9550 0ebf8283 2019-07-24 stsp
9551 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
9552 0ebf8283 2019-07-24 stsp if (err)
9553 0ebf8283 2019-07-24 stsp goto done;
9554 0ebf8283 2019-07-24 stsp
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);
9558 0ebf8283 2019-07-24 stsp done:
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;
9564 0ebf8283 2019-07-24 stsp }
9565 0ebf8283 2019-07-24 stsp
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)
9570 0ebf8283 2019-07-24 stsp {
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;
9574 0ebf8283 2019-07-24 stsp
9575 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
9576 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
9577 0ebf8283 2019-07-24 stsp
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);
9585 0ebf8283 2019-07-24 stsp if (err)
9586 0ebf8283 2019-07-24 stsp break;
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);
9591 083957f4 2020-02-24 stsp break;
9592 083957f4 2020-02-24 stsp }
9593 083957f4 2020-02-24 stsp }
9594 0ebf8283 2019-07-24 stsp }
9595 0ebf8283 2019-07-24 stsp
9596 0ebf8283 2019-07-24 stsp return err;
9597 0ebf8283 2019-07-24 stsp }
9598 0ebf8283 2019-07-24 stsp
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)
9602 0ebf8283 2019-07-24 stsp {
9603 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9604 6059809a 2020-12-17 stsp size_t i;
9605 6059809a 2020-12-17 stsp int n;
9606 514f2ffe 2020-01-29 stsp char *id_str;
9607 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
9608 514f2ffe 2020-01-29 stsp
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);
9611 514f2ffe 2020-01-29 stsp if (err)
9612 514f2ffe 2020-01-29 stsp return err;
9613 514f2ffe 2020-01-29 stsp
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;
9622 514f2ffe 2020-01-29 stsp }
9623 0ebf8283 2019-07-24 stsp
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;
9628 514f2ffe 2020-01-29 stsp }
9629 0ebf8283 2019-07-24 stsp
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);
9636 0ebf8283 2019-07-24 stsp break;
9637 0ebf8283 2019-07-24 stsp }
9638 0ebf8283 2019-07-24 stsp }
9639 514f2ffe 2020-01-29 stsp done:
9640 514f2ffe 2020-01-29 stsp free(id_str);
9641 0ebf8283 2019-07-24 stsp return err;
9642 0ebf8283 2019-07-24 stsp }
9643 0ebf8283 2019-07-24 stsp
9644 0ebf8283 2019-07-24 stsp static const struct got_error *
9645 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
9646 0ebf8283 2019-07-24 stsp {
9647 0ebf8283 2019-07-24 stsp static char msg[42];
9648 0ebf8283 2019-07-24 stsp int ret;
9649 0ebf8283 2019-07-24 stsp
9650 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
9651 0ebf8283 2019-07-24 stsp lineno);
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);
9654 0ebf8283 2019-07-24 stsp
9655 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
9656 0ebf8283 2019-07-24 stsp }
9657 0ebf8283 2019-07-24 stsp
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)
9661 0ebf8283 2019-07-24 stsp {
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;
9665 0ebf8283 2019-07-24 stsp
9666 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9667 0ebf8283 2019-07-24 stsp if (err)
9668 0ebf8283 2019-07-24 stsp return err;
9669 0ebf8283 2019-07-24 stsp
9670 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
9671 0ebf8283 2019-07-24 stsp if (err)
9672 0ebf8283 2019-07-24 stsp goto done;
9673 0ebf8283 2019-07-24 stsp
9674 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
9675 5943eee2 2019-08-13 stsp if (err)
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");
9681 0ebf8283 2019-07-24 stsp }
9682 0ebf8283 2019-07-24 stsp done:
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;
9688 0ebf8283 2019-07-24 stsp }
9689 0ebf8283 2019-07-24 stsp
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)
9692 0ebf8283 2019-07-24 stsp {
9693 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
9694 0ebf8283 2019-07-24 stsp
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);
9701 0ebf8283 2019-07-24 stsp }
9702 0ebf8283 2019-07-24 stsp
9703 0ebf8283 2019-07-24 stsp return folded;
9704 0ebf8283 2019-07-24 stsp }
9705 0ebf8283 2019-07-24 stsp
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)
9709 0ebf8283 2019-07-24 stsp {
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;
9715 0ebf8283 2019-07-24 stsp int fd;
9716 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
9717 0ebf8283 2019-07-24 stsp
9718 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9719 0ebf8283 2019-07-24 stsp if (err)
9720 0ebf8283 2019-07-24 stsp return err;
9721 0ebf8283 2019-07-24 stsp
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);
9727 3f9de99f 2019-07-24 stsp continue;
9728 3f9de99f 2019-07-24 stsp }
9729 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
9730 0ebf8283 2019-07-24 stsp logmsg, repo);
9731 0ebf8283 2019-07-24 stsp if (err)
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);
9736 0ebf8283 2019-07-24 stsp }
9737 0ebf8283 2019-07-24 stsp }
9738 0ebf8283 2019-07-24 stsp
9739 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9740 0ebf8283 2019-07-24 stsp if (err)
9741 0ebf8283 2019-07-24 stsp goto done;
9742 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
9743 5943eee2 2019-08-13 stsp if (err)
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;
9751 0ebf8283 2019-07-24 stsp }
9752 0ebf8283 2019-07-24 stsp free(logmsg);
9753 0ebf8283 2019-07-24 stsp logmsg = new_msg;
9754 0ebf8283 2019-07-24 stsp
9755 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9756 0ebf8283 2019-07-24 stsp if (err)
9757 0ebf8283 2019-07-24 stsp goto done;
9758 0ebf8283 2019-07-24 stsp
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");
9761 0ebf8283 2019-07-24 stsp if (err)
9762 0ebf8283 2019-07-24 stsp goto done;
9763 0ebf8283 2019-07-24 stsp
9764 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
9765 0ebf8283 2019-07-24 stsp close(fd);
9766 0ebf8283 2019-07-24 stsp
9767 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
9768 0ebf8283 2019-07-24 stsp if (err)
9769 0ebf8283 2019-07-24 stsp goto done;
9770 0ebf8283 2019-07-24 stsp
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");
9780 0ebf8283 2019-07-24 stsp }
9781 0ebf8283 2019-07-24 stsp done:
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;
9791 5ef14e63 2019-06-02 stsp }
9792 0ebf8283 2019-07-24 stsp
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)
9796 0ebf8283 2019-07-24 stsp {
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;
9805 0ebf8283 2019-07-24 stsp
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))
9811 0ebf8283 2019-07-24 stsp break;
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);
9814 0ebf8283 2019-07-24 stsp break;
9815 0ebf8283 2019-07-24 stsp }
9816 0ebf8283 2019-07-24 stsp lineno++;
9817 0ebf8283 2019-07-24 stsp p = line;
9818 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
9819 0ebf8283 2019-07-24 stsp p++;
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;
9823 0ebf8283 2019-07-24 stsp continue;
9824 0ebf8283 2019-07-24 stsp }
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);
9831 0ebf8283 2019-07-24 stsp break;
9832 0ebf8283 2019-07-24 stsp }
9833 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
9834 0ebf8283 2019-07-24 stsp p++;
9835 0ebf8283 2019-07-24 stsp break;
9836 0ebf8283 2019-07-24 stsp }
9837 0ebf8283 2019-07-24 stsp }
9838 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
9839 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
9840 0ebf8283 2019-07-24 stsp break;
9841 0ebf8283 2019-07-24 stsp }
9842 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
9843 0ebf8283 2019-07-24 stsp p++;
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);
9847 0ebf8283 2019-07-24 stsp break;
9848 0ebf8283 2019-07-24 stsp }
9849 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
9850 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
9851 0ebf8283 2019-07-24 stsp if (err)
9852 0ebf8283 2019-07-24 stsp break;
9853 0ebf8283 2019-07-24 stsp } else {
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");
9857 0ebf8283 2019-07-24 stsp break;
9858 0ebf8283 2019-07-24 stsp }
9859 0ebf8283 2019-07-24 stsp }
9860 0ebf8283 2019-07-24 stsp free(line);
9861 0ebf8283 2019-07-24 stsp line = NULL;
9862 0ebf8283 2019-07-24 stsp continue;
9863 0ebf8283 2019-07-24 stsp } else {
9864 0ebf8283 2019-07-24 stsp end = p;
9865 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
9866 0ebf8283 2019-07-24 stsp end++;
9867 0ebf8283 2019-07-24 stsp *end = '\0';
9868 0ebf8283 2019-07-24 stsp
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);
9873 0ebf8283 2019-07-24 stsp break;
9874 0ebf8283 2019-07-24 stsp }
9875 0ebf8283 2019-07-24 stsp }
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");
9879 0ebf8283 2019-07-24 stsp break;
9880 0ebf8283 2019-07-24 stsp }
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);
9888 0ebf8283 2019-07-24 stsp }
9889 0ebf8283 2019-07-24 stsp
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;
9893 0ebf8283 2019-07-24 stsp }
9894 0ebf8283 2019-07-24 stsp
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)
9898 bfce7f83 2019-07-27 stsp {
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;
9904 bfce7f83 2019-07-27 stsp
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);
9910 5b87815e 2020-03-05 stsp
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)
9915 5b87815e 2020-03-05 stsp continue;
9916 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
9917 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
9918 5b87815e 2020-03-05 stsp continue;
9919 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
9920 5b87815e 2020-03-05 stsp if (err)
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);
9926 5b87815e 2020-03-05 stsp }
9927 5b87815e 2020-03-05 stsp }
9928 bfce7f83 2019-07-27 stsp
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)
9932 bfce7f83 2019-07-27 stsp break;
9933 bfce7f83 2019-07-27 stsp }
9934 bfce7f83 2019-07-27 stsp if (hle == NULL) {
9935 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
9936 bfce7f83 2019-07-27 stsp if (err)
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);
9942 bfce7f83 2019-07-27 stsp }
9943 bfce7f83 2019-07-27 stsp }
9944 bfce7f83 2019-07-27 stsp
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");
9949 bfce7f83 2019-07-27 stsp
9950 bfce7f83 2019-07-27 stsp return NULL;
9951 bfce7f83 2019-07-27 stsp }
9952 bfce7f83 2019-07-27 stsp
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)
9957 0ebf8283 2019-07-24 stsp {
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;
9961 0ebf8283 2019-07-24 stsp
9962 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
9963 0ebf8283 2019-07-24 stsp if (err)
9964 0ebf8283 2019-07-24 stsp return err;
9965 0ebf8283 2019-07-24 stsp
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;
9969 0ebf8283 2019-07-24 stsp }
9970 0ebf8283 2019-07-24 stsp
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;
9975 0ebf8283 2019-07-24 stsp }
9976 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9977 bfce7f83 2019-07-27 stsp if (err)
9978 bfce7f83 2019-07-27 stsp goto done;
9979 bfce7f83 2019-07-27 stsp
9980 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
9981 0ebf8283 2019-07-24 stsp done:
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;
9986 0ebf8283 2019-07-24 stsp }
9987 0ebf8283 2019-07-24 stsp
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 *);
9992 0ebf8283 2019-07-24 stsp
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)
9998 0ebf8283 2019-07-24 stsp {
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;
10002 0ebf8283 2019-07-24 stsp
10003 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
10004 0ebf8283 2019-07-24 stsp if (err)
10005 0ebf8283 2019-07-24 stsp return err;
10006 0ebf8283 2019-07-24 stsp
10007 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
10008 0ebf8283 2019-07-24 stsp if (err)
10009 0ebf8283 2019-07-24 stsp goto done;
10010 0ebf8283 2019-07-24 stsp
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);
10013 0ebf8283 2019-07-24 stsp if (err)
10014 0ebf8283 2019-07-24 stsp goto done;
10015 0ebf8283 2019-07-24 stsp
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);
10019 083957f4 2020-02-24 stsp } else {
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;
10023 083957f4 2020-02-24 stsp }
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);
10032 083957f4 2020-02-24 stsp }
10033 0ebf8283 2019-07-24 stsp }
10034 0ebf8283 2019-07-24 stsp done:
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;
10041 0ebf8283 2019-07-24 stsp }
10042 0ebf8283 2019-07-24 stsp
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)
10046 0ebf8283 2019-07-24 stsp {
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;
10052 0ebf8283 2019-07-24 stsp
10053 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
10054 0ebf8283 2019-07-24 stsp if (err)
10055 0ebf8283 2019-07-24 stsp return err;
10056 0ebf8283 2019-07-24 stsp
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;
10061 0ebf8283 2019-07-24 stsp }
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,
10064 0ebf8283 2019-07-24 stsp repo);
10065 0ebf8283 2019-07-24 stsp if (err)
10066 0ebf8283 2019-07-24 stsp break;
10067 0ebf8283 2019-07-24 stsp
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);
10073 0ebf8283 2019-07-24 stsp break;
10074 0ebf8283 2019-07-24 stsp }
10075 0ebf8283 2019-07-24 stsp }
10076 0ebf8283 2019-07-24 stsp }
10077 0ebf8283 2019-07-24 stsp done:
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;
10084 0ebf8283 2019-07-24 stsp }
10085 0ebf8283 2019-07-24 stsp
10086 bfce7f83 2019-07-27 stsp void
10087 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
10088 bfce7f83 2019-07-27 stsp {
10089 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
10090 bfce7f83 2019-07-27 stsp
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);
10094 bfce7f83 2019-07-27 stsp }
10095 bfce7f83 2019-07-27 stsp }
10096 bfce7f83 2019-07-27 stsp
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)
10100 0ebf8283 2019-07-24 stsp {
10101 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
10102 0ebf8283 2019-07-24 stsp FILE *f = NULL;
10103 0ebf8283 2019-07-24 stsp
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;
10108 0ebf8283 2019-07-24 stsp }
10109 0ebf8283 2019-07-24 stsp
10110 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
10111 0ebf8283 2019-07-24 stsp done:
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;
10115 0ebf8283 2019-07-24 stsp }
10116 0ebf8283 2019-07-24 stsp
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)
10121 0ebf8283 2019-07-24 stsp {
10122 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
10123 0ebf8283 2019-07-24 stsp int resp = ' ';
10124 0ebf8283 2019-07-24 stsp
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,
10134 bfce7f83 2019-07-27 stsp repo);
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)
10138 426ebf2e 2019-07-25 stsp break;
10139 bfce7f83 2019-07-27 stsp prev_err = err;
10140 426ebf2e 2019-07-25 stsp resp = ' ';
10141 426ebf2e 2019-07-25 stsp continue;
10142 426ebf2e 2019-07-25 stsp }
10143 bfce7f83 2019-07-27 stsp break;
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)
10151 426ebf2e 2019-07-25 stsp break;
10152 bfce7f83 2019-07-27 stsp prev_err = err;
10153 426ebf2e 2019-07-25 stsp resp = ' ';
10154 426ebf2e 2019-07-25 stsp continue;
10155 426ebf2e 2019-07-25 stsp }
10156 bfce7f83 2019-07-27 stsp break;
10157 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
10158 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
10159 0ebf8283 2019-07-24 stsp break;
10160 426ebf2e 2019-07-25 stsp } else
10161 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
10162 0ebf8283 2019-07-24 stsp }
10163 0ebf8283 2019-07-24 stsp
10164 0ebf8283 2019-07-24 stsp return err;
10165 0ebf8283 2019-07-24 stsp }
10166 0ebf8283 2019-07-24 stsp
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)
10171 0ebf8283 2019-07-24 stsp {
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);
10176 0ebf8283 2019-07-24 stsp }
10177 0ebf8283 2019-07-24 stsp
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)
10181 0ebf8283 2019-07-24 stsp {
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;
10184 0ebf8283 2019-07-24 stsp
10185 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
10186 0ebf8283 2019-07-24 stsp if (err)
10187 0ebf8283 2019-07-24 stsp goto done;
10188 0ebf8283 2019-07-24 stsp
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);
10191 0ebf8283 2019-07-24 stsp if (err)
10192 0ebf8283 2019-07-24 stsp goto done;
10193 0ebf8283 2019-07-24 stsp }
10194 0ebf8283 2019-07-24 stsp
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';
10198 0ebf8283 2019-07-24 stsp
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;
10204 0ebf8283 2019-07-24 stsp }
10205 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
10206 0ebf8283 2019-07-24 stsp } else {
10207 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
10208 0ebf8283 2019-07-24 stsp if (err)
10209 0ebf8283 2019-07-24 stsp goto done;
10210 0ebf8283 2019-07-24 stsp }
10211 0ebf8283 2019-07-24 stsp
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);
10217 0ebf8283 2019-07-24 stsp break;
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,
10221 0ebf8283 2019-07-24 stsp logmsg);
10222 0ebf8283 2019-07-24 stsp break;
10223 0ebf8283 2019-07-24 stsp default:
10224 0ebf8283 2019-07-24 stsp break;
10225 0ebf8283 2019-07-24 stsp }
10226 0ebf8283 2019-07-24 stsp done:
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;
10230 0ebf8283 2019-07-24 stsp }
10231 0ebf8283 2019-07-24 stsp
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)
10237 0ebf8283 2019-07-24 stsp {
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;
10241 0ebf8283 2019-07-24 stsp
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);
10245 0ebf8283 2019-07-24 stsp if (err)
10246 0ebf8283 2019-07-24 stsp return err;
10247 0ebf8283 2019-07-24 stsp }
10248 0ebf8283 2019-07-24 stsp
10249 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10250 0ebf8283 2019-07-24 stsp if (err)
10251 0ebf8283 2019-07-24 stsp return err;
10252 0ebf8283 2019-07-24 stsp
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);
10260 0ebf8283 2019-07-24 stsp } else {
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);
10263 0ebf8283 2019-07-24 stsp }
10264 0ebf8283 2019-07-24 stsp done:
10265 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10266 0ebf8283 2019-07-24 stsp return err;
10267 0ebf8283 2019-07-24 stsp }
10268 0ebf8283 2019-07-24 stsp
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)
10272 0ebf8283 2019-07-24 stsp {
10273 0ebf8283 2019-07-24 stsp const struct got_error *error;
10274 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
10275 0ebf8283 2019-07-24 stsp
10276 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
10277 0ebf8283 2019-07-24 stsp repo);
10278 0ebf8283 2019-07-24 stsp if (error)
10279 0ebf8283 2019-07-24 stsp return error;
10280 0ebf8283 2019-07-24 stsp
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;
10284 0ebf8283 2019-07-24 stsp
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;
10288 ab20a43a 2020-01-29 stsp }
10289 ab20a43a 2020-01-29 stsp
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)
10295 ab20a43a 2020-01-29 stsp {
10296 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
10297 ab20a43a 2020-01-29 stsp
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);
10305 ab20a43a 2020-01-29 stsp default:
10306 ab20a43a 2020-01-29 stsp break;
10307 ab20a43a 2020-01-29 stsp }
10308 ab20a43a 2020-01-29 stsp
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);
10315 ab20a43a 2020-01-29 stsp default:
10316 ab20a43a 2020-01-29 stsp break;
10317 ab20a43a 2020-01-29 stsp }
10318 ab20a43a 2020-01-29 stsp
10319 ab20a43a 2020-01-29 stsp return NULL;
10320 0ebf8283 2019-07-24 stsp }
10321 0ebf8283 2019-07-24 stsp
10322 0ebf8283 2019-07-24 stsp static const struct got_error *
10323 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
10324 0ebf8283 2019-07-24 stsp {
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;
10348 0ebf8283 2019-07-24 stsp
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));
10353 0ebf8283 2019-07-24 stsp
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;
10358 0ebf8283 2019-07-24 stsp break;
10359 0ebf8283 2019-07-24 stsp case 'c':
10360 0ebf8283 2019-07-24 stsp continue_edit = 1;
10361 0ebf8283 2019-07-24 stsp break;
10362 c50a7455 2021-10-04 thomas case 'e':
10363 c50a7455 2021-10-04 thomas edit_only = 1;
10364 c50a7455 2021-10-04 thomas break;
10365 466785b9 2020-12-10 jrick case 'f':
10366 466785b9 2020-12-10 jrick fold_only = 1;
10367 466785b9 2020-12-10 jrick break;
10368 0ebf8283 2019-07-24 stsp case 'F':
10369 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
10370 0ebf8283 2019-07-24 stsp break;
10371 083957f4 2020-02-24 stsp case 'm':
10372 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
10373 083957f4 2020-02-24 stsp break;
10374 e600f124 2021-03-21 stsp case 'l':
10375 e600f124 2021-03-21 stsp list_backups = 1;
10376 e600f124 2021-03-21 stsp break;
10377 643b85bc 2021-07-16 stsp case 'X':
10378 643b85bc 2021-07-16 stsp delete_backups = 1;
10379 643b85bc 2021-07-16 stsp break;
10380 0ebf8283 2019-07-24 stsp default:
10381 0ebf8283 2019-07-24 stsp usage_histedit();
10382 0ebf8283 2019-07-24 stsp /* NOTREACHED */
10383 0ebf8283 2019-07-24 stsp }
10384 0ebf8283 2019-07-24 stsp }
10385 0ebf8283 2019-07-24 stsp
10386 0ebf8283 2019-07-24 stsp argc -= optind;
10387 0ebf8283 2019-07-24 stsp argv += optind;
10388 0ebf8283 2019-07-24 stsp
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");
10393 0ebf8283 2019-07-24 stsp #endif
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();
10454 0ebf8283 2019-07-24 stsp
10455 0ebf8283 2019-07-24 stsp /*
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.
10462 0ebf8283 2019-07-24 stsp */
10463 0ebf8283 2019-07-24 stsp
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;
10468 0ebf8283 2019-07-24 stsp }
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;
10474 e600f124 2021-03-21 stsp } else {
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;
10479 e600f124 2021-03-21 stsp }
10480 e600f124 2021-03-21 stsp }
10481 e600f124 2021-03-21 stsp
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,
10485 e600f124 2021-03-21 stsp NULL);
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 */
10496 fa51e947 2020-03-27 stsp }
10497 0ebf8283 2019-07-24 stsp
10498 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10499 c9956ddf 2019-09-08 stsp NULL);
10500 0ebf8283 2019-07-24 stsp if (error != NULL)
10501 0ebf8283 2019-07-24 stsp goto done;
10502 0ebf8283 2019-07-24 stsp
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;
10509 10604dce 2021-09-24 thomas }
10510 10604dce 2021-09-24 thomas
10511 10604dce 2021-09-24 thomas error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10512 10604dce 2021-09-24 thomas repo);
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;
10518 0ebf8283 2019-07-24 stsp }
10519 0ebf8283 2019-07-24 stsp
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;
10523 0ebf8283 2019-07-24 stsp
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;
10530 083957f4 2020-02-24 stsp }
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;
10537 466785b9 2020-12-10 jrick }
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;
10544 c50a7455 2021-10-04 thomas }
10545 083957f4 2020-02-24 stsp
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;
10565 0ebf8283 2019-07-24 stsp }
10566 0ebf8283 2019-07-24 stsp
10567 0ebf8283 2019-07-24 stsp if (continue_edit) {
10568 0ebf8283 2019-07-24 stsp char *path;
10569 0ebf8283 2019-07-24 stsp
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;
10573 0ebf8283 2019-07-24 stsp }
10574 0ebf8283 2019-07-24 stsp
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;
10578 0ebf8283 2019-07-24 stsp
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;
10583 0ebf8283 2019-07-24 stsp
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;
10589 0ebf8283 2019-07-24 stsp
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;
10593 0ebf8283 2019-07-24 stsp
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;
10603 3aac7cf7 2019-07-25 stsp }
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;
10611 0ebf8283 2019-07-24 stsp } else {
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;
10615 0ebf8283 2019-07-24 stsp }
10616 0ebf8283 2019-07-24 stsp
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;
10621 0ebf8283 2019-07-24 stsp
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;
10627 c7d20a3f 2019-07-30 stsp }
10628 c7d20a3f 2019-07-30 stsp
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;
10634 0ebf8283 2019-07-24 stsp
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;
10644 3aac7cf7 2019-07-25 stsp }
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;
10653 0ebf8283 2019-07-24 stsp
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;
10657 c5996fff 2020-01-29 stsp }
10658 c5996fff 2020-01-29 stsp
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;
10663 bfce7f83 2019-07-27 stsp
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;
10673 6ba2bea2 2019-07-27 stsp }
10674 0ebf8283 2019-07-24 stsp } else {
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;
10688 6ba2bea2 2019-07-27 stsp }
10689 0ebf8283 2019-07-24 stsp
10690 0ebf8283 2019-07-24 stsp }
10691 0ebf8283 2019-07-24 stsp
10692 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
10693 0ebf8283 2019-07-24 stsp repo);
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;
10700 6ba2bea2 2019-07-27 stsp }
10701 0ebf8283 2019-07-24 stsp
10702 0ebf8283 2019-07-24 stsp }
10703 0ebf8283 2019-07-24 stsp
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;
10707 0ebf8283 2019-07-24 stsp
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;
10713 0ebf8283 2019-07-24 stsp
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,
10718 62d463ca 2020-10-20 naddy repo);
10719 ab20a43a 2020-01-29 stsp if (error)
10720 ab20a43a 2020-01-29 stsp goto done;
10721 0ebf8283 2019-07-24 stsp } else {
10722 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
10723 ab20a43a 2020-01-29 stsp int have_changes = 0;
10724 ab20a43a 2020-01-29 stsp
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;
10738 ab20a43a 2020-01-29 stsp }
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;
10744 ab20a43a 2020-01-29 stsp } else {
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;
10755 ab20a43a 2020-01-29 stsp }
10756 0ebf8283 2019-07-24 stsp }
10757 0ebf8283 2019-07-24 stsp continue;
10758 0ebf8283 2019-07-24 stsp }
10759 0ebf8283 2019-07-24 stsp
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;
10765 0ebf8283 2019-07-24 stsp }
10766 0ebf8283 2019-07-24 stsp
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);
10773 0ebf8283 2019-07-24 stsp
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;
10781 0ebf8283 2019-07-24 stsp
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;
10790 dddbe150 2021-09-28 thomas }
10791 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10792 0ebf8283 2019-07-24 stsp break;
10793 0ebf8283 2019-07-24 stsp }
10794 0ebf8283 2019-07-24 stsp
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",
10801 0ebf8283 2019-07-24 stsp id_str);
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;
10807 0ebf8283 2019-07-24 stsp }
10808 0ebf8283 2019-07-24 stsp
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;
10814 0ebf8283 2019-07-24 stsp }
10815 0ebf8283 2019-07-24 stsp
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;
10821 0ebf8283 2019-07-24 stsp }
10822 0ebf8283 2019-07-24 stsp
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");
10846 dddbe150 2021-09-28 thomas }
10847 0ebf8283 2019-07-24 stsp } else
10848 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
10849 3e3a69f1 2019-07-25 stsp branch, repo);
10850 0ebf8283 2019-07-24 stsp done:
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;
10868 1d0f4054 2021-06-17 stsp }
10869 2822a352 2019-10-15 stsp return error;
10870 2822a352 2019-10-15 stsp }
10871 2822a352 2019-10-15 stsp
10872 2822a352 2019-10-15 stsp __dead static void
10873 2822a352 2019-10-15 stsp usage_integrate(void)
10874 2822a352 2019-10-15 stsp {
10875 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
10876 2822a352 2019-10-15 stsp exit(1);
10877 2822a352 2019-10-15 stsp }
10878 2822a352 2019-10-15 stsp
10879 2822a352 2019-10-15 stsp static const struct got_error *
10880 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
10881 2822a352 2019-10-15 stsp {
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;
10890 9627c110 2020-04-18 stsp int ch;
10891 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10892 2822a352 2019-10-15 stsp
10893 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
10894 2822a352 2019-10-15 stsp switch (ch) {
10895 2822a352 2019-10-15 stsp default:
10896 2822a352 2019-10-15 stsp usage_integrate();
10897 2822a352 2019-10-15 stsp /* NOTREACHED */
10898 2822a352 2019-10-15 stsp }
10899 2822a352 2019-10-15 stsp }
10900 2822a352 2019-10-15 stsp
10901 2822a352 2019-10-15 stsp argc -= optind;
10902 2822a352 2019-10-15 stsp argv += optind;
10903 2822a352 2019-10-15 stsp
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");
10911 b08a0ccd 2020-09-24 stsp #endif
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;
10916 2822a352 2019-10-15 stsp }
10917 2822a352 2019-10-15 stsp
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",
10922 fa51e947 2020-03-27 stsp cwd);
10923 2822a352 2019-10-15 stsp goto done;
10924 fa51e947 2020-03-27 stsp }
10925 2822a352 2019-10-15 stsp
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;
10929 2822a352 2019-10-15 stsp
10930 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10931 2822a352 2019-10-15 stsp NULL);
10932 2822a352 2019-10-15 stsp if (error != NULL)
10933 2822a352 2019-10-15 stsp goto done;
10934 2822a352 2019-10-15 stsp
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;
10939 2822a352 2019-10-15 stsp
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;
10943 10604dce 2021-09-24 thomas
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;
10947 2822a352 2019-10-15 stsp }
10948 2822a352 2019-10-15 stsp
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;
10953 2822a352 2019-10-15 stsp
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;
10960 2822a352 2019-10-15 stsp }
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;
10967 2822a352 2019-10-15 stsp }
10968 2822a352 2019-10-15 stsp
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;
10972 2822a352 2019-10-15 stsp
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;
10976 2822a352 2019-10-15 stsp
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;
10983 2822a352 2019-10-15 stsp }
10984 2822a352 2019-10-15 stsp
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;
10992 2822a352 2019-10-15 stsp }
10993 2822a352 2019-10-15 stsp
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;
11000 2822a352 2019-10-15 stsp
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);
11003 2822a352 2019-10-15 stsp done:
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;
11008 1d0f4054 2021-06-17 stsp }
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;
11017 10604dce 2021-09-24 thomas }
11018 10604dce 2021-09-24 thomas
11019 10604dce 2021-09-24 thomas __dead static void
11020 10604dce 2021-09-24 thomas usage_merge(void)
11021 10604dce 2021-09-24 thomas {
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);
11025 10604dce 2021-09-24 thomas }
11026 10604dce 2021-09-24 thomas
11027 10604dce 2021-09-24 thomas static const struct got_error *
11028 10604dce 2021-09-24 thomas cmd_merge(int argc, char *argv[])
11029 10604dce 2021-09-24 thomas {
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;
11043 10604dce 2021-09-24 thomas
11044 10604dce 2021-09-24 thomas memset(&upa, 0, sizeof(upa));
11045 10604dce 2021-09-24 thomas
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;
11050 10604dce 2021-09-24 thomas break;
11051 10604dce 2021-09-24 thomas case 'c':
11052 10604dce 2021-09-24 thomas continue_merge = 1;
11053 10604dce 2021-09-24 thomas break;
11054 ba34626b 2021-09-27 thomas case 'n':
11055 ba34626b 2021-09-27 thomas interrupt_merge = 1;
11056 ba34626b 2021-09-27 thomas break;
11057 10604dce 2021-09-24 thomas default:
11058 10604dce 2021-09-24 thomas usage_rebase();
11059 10604dce 2021-09-24 thomas /* NOTREACHED */
11060 10604dce 2021-09-24 thomas }
11061 10604dce 2021-09-24 thomas }
11062 10604dce 2021-09-24 thomas
11063 10604dce 2021-09-24 thomas argc -= optind;
11064 10604dce 2021-09-24 thomas argv += optind;
11065 10604dce 2021-09-24 thomas
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");
11070 10604dce 2021-09-24 thomas #endif
11071 10604dce 2021-09-24 thomas
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();
11079 10604dce 2021-09-24 thomas
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;
11084 10604dce 2021-09-24 thomas }
11085 10604dce 2021-09-24 thomas
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;
11092 10604dce 2021-09-24 thomas }
11093 10604dce 2021-09-24 thomas
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;
11098 10604dce 2021-09-24 thomas
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;
11103 10604dce 2021-09-24 thomas
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;
11107 10604dce 2021-09-24 thomas
11108 10604dce 2021-09-24 thomas error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11109 10604dce 2021-09-24 thomas repo);
11110 10604dce 2021-09-24 thomas if (error)
11111 10604dce 2021-09-24 thomas goto done;
11112 10604dce 2021-09-24 thomas
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;
11117 10604dce 2021-09-24 thomas }
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 */
11128 10604dce 2021-09-24 thomas }
11129 10604dce 2021-09-24 thomas
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;
11133 10604dce 2021-09-24 thomas
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;
11138 10604dce 2021-09-24 thomas }
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;
11151 10604dce 2021-09-24 thomas }
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;
11155 10604dce 2021-09-24 thomas }
11156 10604dce 2021-09-24 thomas
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;
11169 10604dce 2021-09-24 thomas
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;
11193 768705e3 2021-09-27 thomas }
11194 10604dce 2021-09-24 thomas }
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;
11199 10604dce 2021-09-24 thomas
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;
11213 768705e3 2021-09-27 thomas }
11214 10604dce 2021-09-24 thomas }
11215 10604dce 2021-09-24 thomas
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");
11244 10604dce 2021-09-24 thomas }
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);
11261 10604dce 2021-09-24 thomas
11262 10604dce 2021-09-24 thomas }
11263 10604dce 2021-09-24 thomas done:
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;
11280 10604dce 2021-09-24 thomas }
11281 715dc77e 2019-08-03 stsp return error;
11282 715dc77e 2019-08-03 stsp }
11283 715dc77e 2019-08-03 stsp
11284 715dc77e 2019-08-03 stsp __dead static void
11285 715dc77e 2019-08-03 stsp usage_stage(void)
11286 715dc77e 2019-08-03 stsp {
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());
11290 715dc77e 2019-08-03 stsp exit(1);
11291 715dc77e 2019-08-03 stsp }
11292 715dc77e 2019-08-03 stsp
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)
11298 408b4ebc 2019-08-03 stsp {
11299 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
11300 408b4ebc 2019-08-03 stsp char *id_str = NULL;
11301 408b4ebc 2019-08-03 stsp
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;
11306 408b4ebc 2019-08-03 stsp
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);
11310 408b4ebc 2019-08-03 stsp else
11311 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
11312 408b4ebc 2019-08-03 stsp if (err)
11313 408b4ebc 2019-08-03 stsp return err;
11314 408b4ebc 2019-08-03 stsp
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;
11318 dc424a06 2019-08-07 stsp }
11319 2e1f37b0 2019-08-08 stsp
11320 dc424a06 2019-08-07 stsp static const struct got_error *
11321 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
11322 715dc77e 2019-08-03 stsp {
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;
11333 715dc77e 2019-08-03 stsp
11334 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
11335 715dc77e 2019-08-03 stsp
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;
11340 408b4ebc 2019-08-03 stsp break;
11341 dc424a06 2019-08-07 stsp case 'p':
11342 dc424a06 2019-08-07 stsp pflag = 1;
11343 dc424a06 2019-08-07 stsp break;
11344 dc424a06 2019-08-07 stsp case 'F':
11345 dc424a06 2019-08-07 stsp patch_script_path = optarg;
11346 dc424a06 2019-08-07 stsp break;
11347 35213c7c 2020-07-23 stsp case 'S':
11348 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
11349 35213c7c 2020-07-23 stsp break;
11350 715dc77e 2019-08-03 stsp default:
11351 715dc77e 2019-08-03 stsp usage_stage();
11352 715dc77e 2019-08-03 stsp /* NOTREACHED */
11353 715dc77e 2019-08-03 stsp }
11354 715dc77e 2019-08-03 stsp }
11355 715dc77e 2019-08-03 stsp
11356 715dc77e 2019-08-03 stsp argc -= optind;
11357 715dc77e 2019-08-03 stsp argv += optind;
11358 715dc77e 2019-08-03 stsp
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");
11363 715dc77e 2019-08-03 stsp #endif
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");
11368 715dc77e 2019-08-03 stsp
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;
11373 715dc77e 2019-08-03 stsp }
11374 715dc77e 2019-08-03 stsp
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;
11380 fa51e947 2020-03-27 stsp }
11381 715dc77e 2019-08-03 stsp
11382 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11383 c9956ddf 2019-09-08 stsp NULL);
11384 715dc77e 2019-08-03 stsp if (error != NULL)
11385 715dc77e 2019-08-03 stsp goto done;
11386 715dc77e 2019-08-03 stsp
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;
11393 dc424a06 2019-08-07 stsp }
11394 dc424a06 2019-08-07 stsp }
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;
11399 715dc77e 2019-08-03 stsp
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;
11403 10604dce 2021-09-24 thomas
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;
11407 715dc77e 2019-08-03 stsp
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);
11411 2e1f37b0 2019-08-08 stsp else {
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);
11418 2e1f37b0 2019-08-08 stsp }
11419 ad493afc 2019-08-03 stsp done:
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;
11427 1d0f4054 2021-06-17 stsp }
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;
11435 ad493afc 2019-08-03 stsp }
11436 ad493afc 2019-08-03 stsp
11437 ad493afc 2019-08-03 stsp __dead static void
11438 ad493afc 2019-08-03 stsp usage_unstage(void)
11439 ad493afc 2019-08-03 stsp {
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());
11443 ad493afc 2019-08-03 stsp exit(1);
11444 ad493afc 2019-08-03 stsp }
11445 ad493afc 2019-08-03 stsp
11446 ad493afc 2019-08-03 stsp
11447 ad493afc 2019-08-03 stsp static const struct got_error *
11448 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
11449 ad493afc 2019-08-03 stsp {
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;
11461 ad493afc 2019-08-03 stsp
11462 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
11463 ad493afc 2019-08-03 stsp
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;
11468 2e1f37b0 2019-08-08 stsp break;
11469 2e1f37b0 2019-08-08 stsp case 'F':
11470 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
11471 2e1f37b0 2019-08-08 stsp break;
11472 ad493afc 2019-08-03 stsp default:
11473 ad493afc 2019-08-03 stsp usage_unstage();
11474 ad493afc 2019-08-03 stsp /* NOTREACHED */
11475 ad493afc 2019-08-03 stsp }
11476 ad493afc 2019-08-03 stsp }
11477 ad493afc 2019-08-03 stsp
11478 ad493afc 2019-08-03 stsp argc -= optind;
11479 ad493afc 2019-08-03 stsp argv += optind;
11480 ad493afc 2019-08-03 stsp
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");
11485 ad493afc 2019-08-03 stsp #endif
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");
11488 2e1f37b0 2019-08-08 stsp
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;
11493 ad493afc 2019-08-03 stsp }
11494 ad493afc 2019-08-03 stsp
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;
11500 fa51e947 2020-03-27 stsp }
11501 ad493afc 2019-08-03 stsp
11502 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11503 c9956ddf 2019-09-08 stsp NULL);
11504 ad493afc 2019-08-03 stsp if (error != NULL)
11505 ad493afc 2019-08-03 stsp goto done;
11506 ad493afc 2019-08-03 stsp
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;
11513 2e1f37b0 2019-08-08 stsp }
11514 2e1f37b0 2019-08-08 stsp }
11515 2e1f37b0 2019-08-08 stsp
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;
11520 ad493afc 2019-08-03 stsp
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;
11524 ad493afc 2019-08-03 stsp
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);
11532 715dc77e 2019-08-03 stsp done:
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;
11540 1d0f4054 2021-06-17 stsp }
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;
11548 01073a5d 2019-08-22 stsp }
11549 01073a5d 2019-08-22 stsp
11550 01073a5d 2019-08-22 stsp __dead static void
11551 01073a5d 2019-08-22 stsp usage_cat(void)
11552 01073a5d 2019-08-22 stsp {
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());
11555 01073a5d 2019-08-22 stsp exit(1);
11556 01073a5d 2019-08-22 stsp }
11557 01073a5d 2019-08-22 stsp
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)
11560 01073a5d 2019-08-22 stsp {
11561 01073a5d 2019-08-22 stsp const struct got_error *err;
11562 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
11563 01073a5d 2019-08-22 stsp
11564 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
11565 01073a5d 2019-08-22 stsp if (err)
11566 01073a5d 2019-08-22 stsp return err;
11567 01073a5d 2019-08-22 stsp
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;
11571 01073a5d 2019-08-22 stsp }
11572 01073a5d 2019-08-22 stsp
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)
11575 01073a5d 2019-08-22 stsp {
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;
11579 01073a5d 2019-08-22 stsp
11580 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
11581 01073a5d 2019-08-22 stsp if (err)
11582 01073a5d 2019-08-22 stsp return err;
11583 01073a5d 2019-08-22 stsp
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)
11589 01073a5d 2019-08-22 stsp break;
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));
11592 01073a5d 2019-08-22 stsp if (err)
11593 01073a5d 2019-08-22 stsp break;
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);
11598 01073a5d 2019-08-22 stsp }
11599 01073a5d 2019-08-22 stsp
11600 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
11601 01073a5d 2019-08-22 stsp return err;
11602 01073a5d 2019-08-22 stsp }
11603 01073a5d 2019-08-22 stsp
11604 d356bf75 2022-02-16 thomas static void
11605 d356bf75 2022-02-16 thomas format_gmtoff(char *buf, size_t sz, time_t gmtoff)
11606 d356bf75 2022-02-16 thomas {
11607 d356bf75 2022-02-16 thomas long long h, m;
11608 d356bf75 2022-02-16 thomas char sign = '+';
11609 b0ca001a 2022-02-16 thomas
11610 d356bf75 2022-02-16 thomas if (gmtoff < 0) {
11611 d356bf75 2022-02-16 thomas sign = '-';
11612 d356bf75 2022-02-16 thomas gmtoff = -gmtoff;
11613 d356bf75 2022-02-16 thomas }
11614 d356bf75 2022-02-16 thomas
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);
11618 d356bf75 2022-02-16 thomas }
11619 d356bf75 2022-02-16 thomas
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)
11622 01073a5d 2019-08-22 stsp {
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];
11630 01073a5d 2019-08-22 stsp
11631 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
11632 01073a5d 2019-08-22 stsp if (err)
11633 01073a5d 2019-08-22 stsp return err;
11634 01073a5d 2019-08-22 stsp
11635 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
11636 01073a5d 2019-08-22 stsp if (err)
11637 01073a5d 2019-08-22 stsp goto done;
11638 01073a5d 2019-08-22 stsp
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);
11646 01073a5d 2019-08-22 stsp if (err)
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);
11650 01073a5d 2019-08-22 stsp }
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);
11657 01073a5d 2019-08-22 stsp
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);
11664 01073a5d 2019-08-22 stsp
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);
11668 01073a5d 2019-08-22 stsp done:
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;
11672 01073a5d 2019-08-22 stsp }
11673 01073a5d 2019-08-22 stsp
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)
11676 01073a5d 2019-08-22 stsp {
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];
11682 01073a5d 2019-08-22 stsp
11683 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
11684 01073a5d 2019-08-22 stsp if (err)
11685 01073a5d 2019-08-22 stsp return err;
11686 01073a5d 2019-08-22 stsp
11687 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
11688 01073a5d 2019-08-22 stsp if (err)
11689 01073a5d 2019-08-22 stsp goto done;
11690 01073a5d 2019-08-22 stsp
11691 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
11692 e15d5241 2019-08-22 stsp
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);
11697 01073a5d 2019-08-22 stsp break;
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);
11701 01073a5d 2019-08-22 stsp break;
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);
11705 01073a5d 2019-08-22 stsp break;
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);
11709 01073a5d 2019-08-22 stsp break;
11710 01073a5d 2019-08-22 stsp default:
11711 01073a5d 2019-08-22 stsp break;
11712 01073a5d 2019-08-22 stsp }
11713 01073a5d 2019-08-22 stsp
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));
11716 e15d5241 2019-08-22 stsp
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);
11723 01073a5d 2019-08-22 stsp
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);
11727 01073a5d 2019-08-22 stsp done:
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;
11731 01073a5d 2019-08-22 stsp }
11732 01073a5d 2019-08-22 stsp
11733 01073a5d 2019-08-22 stsp static const struct got_error *
11734 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
11735 01073a5d 2019-08-22 stsp {
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;
11744 84de9106 2020-12-26 stsp
11745 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
11746 01073a5d 2019-08-22 stsp
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");
11751 01073a5d 2019-08-22 stsp #endif
11752 01073a5d 2019-08-22 stsp
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;
11757 896e9b6f 2019-08-26 stsp break;
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",
11762 9ba1d308 2019-10-21 stsp optarg);
11763 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
11764 01073a5d 2019-08-22 stsp break;
11765 896e9b6f 2019-08-26 stsp case 'P':
11766 896e9b6f 2019-08-26 stsp force_path = 1;
11767 896e9b6f 2019-08-26 stsp break;
11768 01073a5d 2019-08-22 stsp default:
11769 01073a5d 2019-08-22 stsp usage_cat();
11770 01073a5d 2019-08-22 stsp /* NOTREACHED */
11771 01073a5d 2019-08-22 stsp }
11772 01073a5d 2019-08-22 stsp }
11773 01073a5d 2019-08-22 stsp
11774 01073a5d 2019-08-22 stsp argc -= optind;
11775 01073a5d 2019-08-22 stsp argv += optind;
11776 01073a5d 2019-08-22 stsp
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;
11781 01073a5d 2019-08-22 stsp }
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;
11792 01073a5d 2019-08-22 stsp }
11793 01073a5d 2019-08-22 stsp }
11794 f89c34e1 2022-03-13 thomas
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;
11798 01073a5d 2019-08-22 stsp }
11799 01073a5d 2019-08-22 stsp
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");
11804 01073a5d 2019-08-22 stsp }
11805 01073a5d 2019-08-22 stsp
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;
11810 01073a5d 2019-08-22 stsp
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;
11814 896e9b6f 2019-08-26 stsp
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;
11818 84de9106 2020-12-26 stsp
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;
11825 01073a5d 2019-08-22 stsp
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)
11831 896e9b6f 2019-08-26 stsp break;
11832 896e9b6f 2019-08-26 stsp } else {
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 */,
11835 84de9106 2020-12-26 stsp repo);
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)
11839 896e9b6f 2019-08-26 stsp break;
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)
11843 896e9b6f 2019-08-26 stsp break;
11844 896e9b6f 2019-08-26 stsp }
11845 896e9b6f 2019-08-26 stsp }
11846 01073a5d 2019-08-22 stsp
11847 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
11848 01073a5d 2019-08-22 stsp if (error)
11849 01073a5d 2019-08-22 stsp break;
11850 01073a5d 2019-08-22 stsp
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);
11854 01073a5d 2019-08-22 stsp break;
11855 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
11856 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
11857 01073a5d 2019-08-22 stsp break;
11858 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
11859 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
11860 01073a5d 2019-08-22 stsp break;
11861 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
11862 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
11863 01073a5d 2019-08-22 stsp break;
11864 01073a5d 2019-08-22 stsp default:
11865 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
11866 01073a5d 2019-08-22 stsp break;
11867 01073a5d 2019-08-22 stsp }
11868 01073a5d 2019-08-22 stsp if (error)
11869 01073a5d 2019-08-22 stsp break;
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;
11874 01073a5d 2019-08-22 stsp }
11875 01073a5d 2019-08-22 stsp done:
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;
11885 b2118c49 2020-07-28 stsp }
11886 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
11887 b2118c49 2020-07-28 stsp return error;
11888 b2118c49 2020-07-28 stsp }
11889 b2118c49 2020-07-28 stsp
11890 b2118c49 2020-07-28 stsp __dead static void
11891 b2118c49 2020-07-28 stsp usage_info(void)
11892 b2118c49 2020-07-28 stsp {
11893 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
11894 b2118c49 2020-07-28 stsp getprogname());
11895 b2118c49 2020-07-28 stsp exit(1);
11896 b2118c49 2020-07-28 stsp }
11897 b2118c49 2020-07-28 stsp
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)
11902 b2118c49 2020-07-28 stsp {
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;
11909 b2118c49 2020-07-28 stsp
11910 b2118c49 2020-07-28 stsp /*
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.
11913 b2118c49 2020-07-28 stsp */
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 */
11919 b2118c49 2020-07-28 stsp }
11920 b2118c49 2020-07-28 stsp
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);
11929 b2118c49 2020-07-28 stsp else
11930 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
11931 b2118c49 2020-07-28 stsp
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);
11938 b2118c49 2020-07-28 stsp
11939 b2118c49 2020-07-28 stsp if (blob_id) {
11940 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
11941 b2118c49 2020-07-28 stsp if (err)
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);
11945 b2118c49 2020-07-28 stsp }
11946 b2118c49 2020-07-28 stsp
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);
11949 b2118c49 2020-07-28 stsp if (err)
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);
11953 b2118c49 2020-07-28 stsp }
11954 b2118c49 2020-07-28 stsp
11955 b2118c49 2020-07-28 stsp if (commit_id) {
11956 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
11957 b2118c49 2020-07-28 stsp if (err)
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);
11961 b2118c49 2020-07-28 stsp }
11962 b2118c49 2020-07-28 stsp
11963 b2118c49 2020-07-28 stsp return NULL;
11964 b2118c49 2020-07-28 stsp }
11965 b2118c49 2020-07-28 stsp
11966 b2118c49 2020-07-28 stsp static const struct got_error *
11967 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
11968 b2118c49 2020-07-28 stsp {
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;
11976 b2118c49 2020-07-28 stsp
11977 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
11978 b2118c49 2020-07-28 stsp
11979 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
11980 b2118c49 2020-07-28 stsp switch (ch) {
11981 b2118c49 2020-07-28 stsp default:
11982 b2118c49 2020-07-28 stsp usage_info();
11983 b2118c49 2020-07-28 stsp /* NOTREACHED */
11984 b2118c49 2020-07-28 stsp }
11985 01073a5d 2019-08-22 stsp }
11986 b2118c49 2020-07-28 stsp
11987 b2118c49 2020-07-28 stsp argc -= optind;
11988 b2118c49 2020-07-28 stsp argv += optind;
11989 b2118c49 2020-07-28 stsp
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");
11994 b2118c49 2020-07-28 stsp #endif
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;
11999 b2118c49 2020-07-28 stsp }
12000 b2118c49 2020-07-28 stsp
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;
12006 b2118c49 2020-07-28 stsp }
12007 b2118c49 2020-07-28 stsp
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;
12011 b2118c49 2020-07-28 stsp
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;
12018 b2118c49 2020-07-28 stsp }
12019 b2118c49 2020-07-28 stsp
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;
12024 b2118c49 2020-07-28 stsp
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;
12028 b2118c49 2020-07-28 stsp
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));
12037 b2118c49 2020-07-28 stsp
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;
12043 b2118c49 2020-07-28 stsp /*
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.
12046 b2118c49 2020-07-28 stsp */
12047 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
12048 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
12049 b2118c49 2020-07-28 stsp }
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 */
12057 b2118c49 2020-07-28 stsp break;
12058 b2118c49 2020-07-28 stsp }
12059 b2118c49 2020-07-28 stsp }
12060 b2118c49 2020-07-28 stsp }
12061 b2118c49 2020-07-28 stsp done:
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;
12069 0ebf8283 2019-07-24 stsp }