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 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
22 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
23 f42b1b34 2018-03-12 stsp
24 5c860e29 2018-03-12 stsp #include <err.h>
25 5c860e29 2018-03-12 stsp #include <errno.h>
26 12463d8b 2019-12-13 stsp #include <fcntl.h>
27 12ce7a6c 2019-08-12 stsp #include <limits.h>
28 5c860e29 2018-03-12 stsp #include <locale.h>
29 818c7501 2019-07-11 stsp #include <ctype.h>
30 d7b5a0e8 2022-04-20 stsp #include <sha1.h>
31 99437157 2018-11-11 stsp #include <signal.h>
32 5c860e29 2018-03-12 stsp #include <stdio.h>
33 5c860e29 2018-03-12 stsp #include <stdlib.h>
34 5c860e29 2018-03-12 stsp #include <string.h>
35 5c860e29 2018-03-12 stsp #include <unistd.h>
36 c09a553d 2018-03-12 stsp #include <libgen.h>
37 c0768b0f 2018-06-10 stsp #include <time.h>
38 33ad4cbe 2019-05-12 jcs #include <paths.h>
39 6841bf13 2019-11-29 kn #include <regex.h>
40 83cd27f8 2020-01-13 stsp #include <getopt.h>
41 d2cdc636 2020-03-18 stsp #include <util.h>
42 5c860e29 2018-03-12 stsp
43 53ccebc2 2019-07-30 stsp #include "got_version.h"
44 f42b1b34 2018-03-12 stsp #include "got_error.h"
45 f42b1b34 2018-03-12 stsp #include "got_object.h"
46 5261c201 2018-04-01 stsp #include "got_reference.h"
47 f42b1b34 2018-03-12 stsp #include "got_repository.h"
48 1dd54920 2019-05-11 stsp #include "got_path.h"
49 e6209546 2019-08-22 stsp #include "got_cancel.h"
50 c09a553d 2018-03-12 stsp #include "got_worktree.h"
51 79109fed 2018-03-27 stsp #include "got_diff.h"
52 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
53 6f23baec 2020-03-18 stsp #include "got_fetch.h"
54 f8a36e22 2021-08-26 stsp #include "got_send.h"
55 404c43c4 2018-06-21 stsp #include "got_blame.h"
56 63219cd2 2019-01-04 stsp #include "got_privsep.h"
57 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
58 50b0790e 2020-09-11 stsp #include "got_gotconfig.h"
59 d65a88a2 2021-09-05 stsp #include "got_dial.h"
60 e9ce266e 2022-03-07 op #include "got_patch.h"
61 5c860e29 2018-03-12 stsp
62 5c860e29 2018-03-12 stsp #ifndef nitems
63 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 5c860e29 2018-03-12 stsp #endif
65 99437157 2018-11-11 stsp
66 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
67 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
68 99437157 2018-11-11 stsp
69 99437157 2018-11-11 stsp static void
70 99437157 2018-11-11 stsp catch_sigint(int signo)
71 99437157 2018-11-11 stsp {
72 99437157 2018-11-11 stsp sigint_received = 1;
73 99437157 2018-11-11 stsp }
74 99437157 2018-11-11 stsp
75 99437157 2018-11-11 stsp static void
76 99437157 2018-11-11 stsp catch_sigpipe(int signo)
77 99437157 2018-11-11 stsp {
78 99437157 2018-11-11 stsp sigpipe_received = 1;
79 99437157 2018-11-11 stsp }
80 5c860e29 2018-03-12 stsp
81 99437157 2018-11-11 stsp
82 8cfb4057 2019-07-09 stsp struct got_cmd {
83 820059fa 2020-09-25 stsp const char *cmd_name;
84 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
85 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
86 97b3a7be 2019-07-09 stsp const char *cmd_alias;
87 5c860e29 2018-03-12 stsp };
88 5c860e29 2018-03-12 stsp
89 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
90 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
91 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
92 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
93 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
94 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
95 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
96 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
97 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
98 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
99 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
100 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
101 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
102 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
103 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
104 d00136be 2019-03-26 stsp __dead static void usage_add(void);
105 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
106 e9ce266e 2022-03-07 op __dead static void usage_patch(void);
107 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
108 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
109 f8a36e22 2021-08-26 stsp __dead static void usage_send(void);
110 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
111 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
112 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
113 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
114 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
115 f259c4c1 2021-09-24 stsp __dead static void usage_merge(void);
116 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
117 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
118 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
119 b2118c49 2020-07-28 stsp __dead static void usage_info(void);
120 5c860e29 2018-03-12 stsp
121 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
122 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
123 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
124 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
125 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
126 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
127 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
128 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
129 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
130 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
131 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
132 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
133 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
134 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
135 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
136 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
137 e9ce266e 2022-03-07 op static const struct got_error* cmd_patch(int, char *[]);
138 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
139 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
140 f8a36e22 2021-08-26 stsp static const struct got_error* cmd_send(int, char *[]);
141 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
142 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
143 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
144 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
145 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
146 f259c4c1 2021-09-24 stsp static const struct got_error* cmd_merge(int, char *[]);
147 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
148 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
149 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
150 b2118c49 2020-07-28 stsp static const struct got_error* cmd_info(int, char *[]);
151 5c860e29 2018-03-12 stsp
152 3e166534 2022-02-16 naddy static const struct got_cmd got_commands[] = {
153 b2118c49 2020-07-28 stsp { "init", cmd_init, usage_init, "" },
154 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
155 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
156 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
157 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
158 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
159 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
160 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
161 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
162 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
163 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
164 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
165 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
166 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
167 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
168 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
169 e9ce266e 2022-03-07 op { "patch", cmd_patch, usage_patch, "pa" },
170 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
171 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
172 f8a36e22 2021-08-26 stsp { "send", cmd_send, usage_send, "se" },
173 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
174 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
175 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
176 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
177 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
178 f259c4c1 2021-09-24 stsp { "merge", cmd_merge, usage_merge, "mg" },
179 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
180 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
181 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
182 b2118c49 2020-07-28 stsp { "info", cmd_info, usage_info, "" },
183 5c860e29 2018-03-12 stsp };
184 ce5b7c56 2019-07-09 stsp
185 ce5b7c56 2019-07-09 stsp static void
186 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
187 ce5b7c56 2019-07-09 stsp {
188 6059809a 2020-12-17 stsp size_t i;
189 ce5b7c56 2019-07-09 stsp
190 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
191 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
192 3e166534 2022-02-16 naddy const struct got_cmd *cmd = &got_commands[i];
193 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->cmd_name);
194 ce5b7c56 2019-07-09 stsp }
195 6879ba42 2020-10-01 naddy fputc('\n', fp);
196 ce5b7c56 2019-07-09 stsp }
197 5c860e29 2018-03-12 stsp
198 ff69268e 2020-12-13 stsp __dead static void
199 ff69268e 2020-12-13 stsp option_conflict(char a, char b)
200 ff69268e 2020-12-13 stsp {
201 ff69268e 2020-12-13 stsp errx(1, "-%c and -%c options are mutually exclusive", a, b);
202 ff69268e 2020-12-13 stsp }
203 ff69268e 2020-12-13 stsp
204 5c860e29 2018-03-12 stsp int
205 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
206 5c860e29 2018-03-12 stsp {
207 3e166534 2022-02-16 naddy const struct got_cmd *cmd;
208 6059809a 2020-12-17 stsp size_t i;
209 5c860e29 2018-03-12 stsp int ch;
210 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
211 3e166534 2022-02-16 naddy static const struct option longopts[] = {
212 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
213 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0 }
214 83cd27f8 2020-01-13 stsp };
215 5c860e29 2018-03-12 stsp
216 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
217 5c860e29 2018-03-12 stsp
218 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
219 5c860e29 2018-03-12 stsp switch (ch) {
220 1b6b95a8 2018-03-12 stsp case 'h':
221 1b6b95a8 2018-03-12 stsp hflag = 1;
222 53ccebc2 2019-07-30 stsp break;
223 53ccebc2 2019-07-30 stsp case 'V':
224 53ccebc2 2019-07-30 stsp Vflag = 1;
225 1b6b95a8 2018-03-12 stsp break;
226 5c860e29 2018-03-12 stsp default:
227 6879ba42 2020-10-01 naddy usage(hflag, 1);
228 5c860e29 2018-03-12 stsp /* NOTREACHED */
229 5c860e29 2018-03-12 stsp }
230 5c860e29 2018-03-12 stsp }
231 5c860e29 2018-03-12 stsp
232 5c860e29 2018-03-12 stsp argc -= optind;
233 5c860e29 2018-03-12 stsp argv += optind;
234 9814e6a3 2020-09-27 naddy optind = 1;
235 9814e6a3 2020-09-27 naddy optreset = 1;
236 53ccebc2 2019-07-30 stsp
237 53ccebc2 2019-07-30 stsp if (Vflag) {
238 53ccebc2 2019-07-30 stsp got_version_print_str();
239 6879ba42 2020-10-01 naddy return 0;
240 53ccebc2 2019-07-30 stsp }
241 5c860e29 2018-03-12 stsp
242 5c860e29 2018-03-12 stsp if (argc <= 0)
243 6879ba42 2020-10-01 naddy usage(hflag, hflag ? 0 : 1);
244 5c860e29 2018-03-12 stsp
245 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
246 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
247 99437157 2018-11-11 stsp
248 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
249 d7d4f210 2018-03-12 stsp const struct got_error *error;
250 d7d4f210 2018-03-12 stsp
251 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
252 5c860e29 2018-03-12 stsp
253 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
254 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
255 5c860e29 2018-03-12 stsp continue;
256 5c860e29 2018-03-12 stsp
257 1b6b95a8 2018-03-12 stsp if (hflag)
258 3e166534 2022-02-16 naddy cmd->cmd_usage();
259 1b6b95a8 2018-03-12 stsp
260 3e166534 2022-02-16 naddy error = cmd->cmd_main(argc, argv);
261 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
262 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
263 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
264 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
265 70015d7a 2019-11-08 stsp !(sigint_received &&
266 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
267 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
268 d7d4f210 2018-03-12 stsp return 1;
269 d7d4f210 2018-03-12 stsp }
270 d7d4f210 2018-03-12 stsp
271 d7d4f210 2018-03-12 stsp return 0;
272 5c860e29 2018-03-12 stsp }
273 5c860e29 2018-03-12 stsp
274 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
275 6879ba42 2020-10-01 naddy list_commands(stderr);
276 5c860e29 2018-03-12 stsp return 1;
277 5c860e29 2018-03-12 stsp }
278 5c860e29 2018-03-12 stsp
279 4ed7e80c 2018-05-20 stsp __dead static void
280 6879ba42 2020-10-01 naddy usage(int hflag, int status)
281 5c860e29 2018-03-12 stsp {
282 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
283 6879ba42 2020-10-01 naddy
284 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
285 53ccebc2 2019-07-30 stsp getprogname());
286 ce5b7c56 2019-07-09 stsp if (hflag)
287 6879ba42 2020-10-01 naddy list_commands(fp);
288 6879ba42 2020-10-01 naddy exit(status);
289 5c860e29 2018-03-12 stsp }
290 5c860e29 2018-03-12 stsp
291 0266afb7 2019-01-04 stsp static const struct got_error *
292 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
293 e2ba3d07 2019-05-13 stsp {
294 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
295 e2ba3d07 2019-05-13 stsp const char *editor;
296 8920fa04 2019-08-18 stsp
297 8920fa04 2019-08-18 stsp *abspath = NULL;
298 e2ba3d07 2019-05-13 stsp
299 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
300 e2ba3d07 2019-05-13 stsp if (editor == NULL)
301 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
302 e2ba3d07 2019-05-13 stsp
303 0ee7065d 2019-05-13 stsp if (editor) {
304 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
305 0ee7065d 2019-05-13 stsp if (err)
306 0ee7065d 2019-05-13 stsp return err;
307 0ee7065d 2019-05-13 stsp }
308 e2ba3d07 2019-05-13 stsp
309 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
310 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
311 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
312 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
313 0ee7065d 2019-05-13 stsp }
314 0ee7065d 2019-05-13 stsp
315 e2ba3d07 2019-05-13 stsp return NULL;
316 e2ba3d07 2019-05-13 stsp }
317 e2ba3d07 2019-05-13 stsp
318 e2ba3d07 2019-05-13 stsp static const struct got_error *
319 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
320 c530dc23 2019-07-23 stsp const char *worktree_path)
321 0266afb7 2019-01-04 stsp {
322 163ce85a 2019-05-13 stsp const struct got_error *err;
323 0266afb7 2019-01-04 stsp
324 37c06ea4 2019-07-15 stsp #ifdef PROFILE
325 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
326 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
327 37c06ea4 2019-07-15 stsp #endif
328 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
329 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
330 0266afb7 2019-01-04 stsp
331 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
332 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
333 0266afb7 2019-01-04 stsp
334 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
335 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
336 0266afb7 2019-01-04 stsp
337 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
338 163ce85a 2019-05-13 stsp if (err != NULL)
339 163ce85a 2019-05-13 stsp return err;
340 0266afb7 2019-01-04 stsp
341 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
342 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
343 0266afb7 2019-01-04 stsp
344 0266afb7 2019-01-04 stsp return NULL;
345 2c7829a4 2019-06-17 stsp }
346 2c7829a4 2019-06-17 stsp
347 2c7829a4 2019-06-17 stsp __dead static void
348 2c7829a4 2019-06-17 stsp usage_init(void)
349 2c7829a4 2019-06-17 stsp {
350 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
351 2c7829a4 2019-06-17 stsp exit(1);
352 2c7829a4 2019-06-17 stsp }
353 2c7829a4 2019-06-17 stsp
354 2c7829a4 2019-06-17 stsp static const struct got_error *
355 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
356 2c7829a4 2019-06-17 stsp {
357 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
358 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
359 2c7829a4 2019-06-17 stsp int ch;
360 2c7829a4 2019-06-17 stsp
361 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
362 2c7829a4 2019-06-17 stsp switch (ch) {
363 2c7829a4 2019-06-17 stsp default:
364 2c7829a4 2019-06-17 stsp usage_init();
365 2c7829a4 2019-06-17 stsp /* NOTREACHED */
366 2c7829a4 2019-06-17 stsp }
367 2c7829a4 2019-06-17 stsp }
368 2c7829a4 2019-06-17 stsp
369 2c7829a4 2019-06-17 stsp argc -= optind;
370 2c7829a4 2019-06-17 stsp argv += optind;
371 2c7829a4 2019-06-17 stsp
372 2c7829a4 2019-06-17 stsp #ifndef PROFILE
373 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
374 2c7829a4 2019-06-17 stsp err(1, "pledge");
375 2c7829a4 2019-06-17 stsp #endif
376 2c7829a4 2019-06-17 stsp if (argc != 1)
377 bc20e173 2019-06-17 stsp usage_init();
378 2c7829a4 2019-06-17 stsp
379 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
380 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
381 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
382 2c7829a4 2019-06-17 stsp
383 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
384 2c7829a4 2019-06-17 stsp
385 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
386 2c7829a4 2019-06-17 stsp if (error &&
387 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
388 2c7829a4 2019-06-17 stsp goto done;
389 2c7829a4 2019-06-17 stsp
390 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
391 2c7829a4 2019-06-17 stsp if (error)
392 2c7829a4 2019-06-17 stsp goto done;
393 2c7829a4 2019-06-17 stsp
394 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
395 3ce1b845 2019-07-15 stsp done:
396 3ce1b845 2019-07-15 stsp free(repo_path);
397 3ce1b845 2019-07-15 stsp return error;
398 3ce1b845 2019-07-15 stsp }
399 3ce1b845 2019-07-15 stsp
400 3ce1b845 2019-07-15 stsp __dead static void
401 3ce1b845 2019-07-15 stsp usage_import(void)
402 3ce1b845 2019-07-15 stsp {
403 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
404 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
405 3ce1b845 2019-07-15 stsp exit(1);
406 3ce1b845 2019-07-15 stsp }
407 3ce1b845 2019-07-15 stsp
408 3ce1b845 2019-07-15 stsp int
409 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
410 3ce1b845 2019-07-15 stsp {
411 3ce1b845 2019-07-15 stsp pid_t pid;
412 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
413 3ce1b845 2019-07-15 stsp int st = -1;
414 3ce1b845 2019-07-15 stsp
415 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
416 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
417 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
418 3ce1b845 2019-07-15 stsp
419 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
420 3ce1b845 2019-07-15 stsp case -1:
421 3ce1b845 2019-07-15 stsp goto doneediting;
422 3ce1b845 2019-07-15 stsp case 0:
423 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
424 3ce1b845 2019-07-15 stsp _exit(127);
425 3ce1b845 2019-07-15 stsp }
426 3ce1b845 2019-07-15 stsp
427 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
428 3ce1b845 2019-07-15 stsp if (errno != EINTR)
429 3ce1b845 2019-07-15 stsp break;
430 3ce1b845 2019-07-15 stsp
431 3ce1b845 2019-07-15 stsp doneediting:
432 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
433 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
434 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
435 3ce1b845 2019-07-15 stsp
436 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
437 3ce1b845 2019-07-15 stsp errno = EINTR;
438 3ce1b845 2019-07-15 stsp return -1;
439 3ce1b845 2019-07-15 stsp }
440 3ce1b845 2019-07-15 stsp
441 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
442 3ce1b845 2019-07-15 stsp }
443 3ce1b845 2019-07-15 stsp
444 3ce1b845 2019-07-15 stsp static const struct got_error *
445 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
446 28cf319f 2021-01-28 stsp const char *initial_content, size_t initial_content_len,
447 28cf319f 2021-01-28 stsp int require_modification)
448 3ce1b845 2019-07-15 stsp {
449 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
450 bfa12d5e 2020-09-26 stsp char *line = NULL;
451 bfa12d5e 2020-09-26 stsp size_t linesize = 0;
452 bfa12d5e 2020-09-26 stsp ssize_t linelen;
453 3ce1b845 2019-07-15 stsp struct stat st, st2;
454 bfa12d5e 2020-09-26 stsp FILE *fp = NULL;
455 bfa12d5e 2020-09-26 stsp size_t len, logmsg_len;
456 bfa12d5e 2020-09-26 stsp char *initial_content_stripped = NULL, *buf = NULL, *s;
457 3ce1b845 2019-07-15 stsp
458 3ce1b845 2019-07-15 stsp *logmsg = NULL;
459 3ce1b845 2019-07-15 stsp
460 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
461 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
462 3ce1b845 2019-07-15 stsp
463 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
464 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
465 3ce1b845 2019-07-15 stsp
466 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
467 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
468 3ce1b845 2019-07-15 stsp
469 28cf319f 2021-01-28 stsp if (require_modification &&
470 28cf319f 2021-01-28 stsp st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
471 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
472 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
473 3ce1b845 2019-07-15 stsp
474 bfa12d5e 2020-09-26 stsp /*
475 bfa12d5e 2020-09-26 stsp * Set up a stripped version of the initial content without comments
476 bfa12d5e 2020-09-26 stsp * and blank lines. We need this in order to check if the message
477 bfa12d5e 2020-09-26 stsp * has in fact been edited.
478 bfa12d5e 2020-09-26 stsp */
479 bfa12d5e 2020-09-26 stsp initial_content_stripped = malloc(initial_content_len + 1);
480 bfa12d5e 2020-09-26 stsp if (initial_content_stripped == NULL)
481 bfa12d5e 2020-09-26 stsp return got_error_from_errno("malloc");
482 bfa12d5e 2020-09-26 stsp initial_content_stripped[0] = '\0';
483 bfa12d5e 2020-09-26 stsp
484 bfa12d5e 2020-09-26 stsp buf = strdup(initial_content);
485 bfa12d5e 2020-09-26 stsp if (buf == NULL) {
486 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("strdup");
487 bfa12d5e 2020-09-26 stsp goto done;
488 bfa12d5e 2020-09-26 stsp }
489 bfa12d5e 2020-09-26 stsp s = buf;
490 bfa12d5e 2020-09-26 stsp len = 0;
491 bfa12d5e 2020-09-26 stsp while ((line = strsep(&s, "\n")) != NULL) {
492 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
493 bfa12d5e 2020-09-26 stsp continue; /* remove comments and leading empty lines */
494 bfa12d5e 2020-09-26 stsp len = strlcat(initial_content_stripped, line,
495 bfa12d5e 2020-09-26 stsp initial_content_len + 1);
496 bfa12d5e 2020-09-26 stsp if (len >= initial_content_len + 1) {
497 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
498 bfa12d5e 2020-09-26 stsp goto done;
499 bfa12d5e 2020-09-26 stsp }
500 bfa12d5e 2020-09-26 stsp }
501 bfa12d5e 2020-09-26 stsp while (len > 0 && initial_content_stripped[len - 1] == '\n') {
502 bfa12d5e 2020-09-26 stsp initial_content_stripped[len - 1] = '\0';
503 bfa12d5e 2020-09-26 stsp len--;
504 bfa12d5e 2020-09-26 stsp }
505 bfa12d5e 2020-09-26 stsp
506 bfa12d5e 2020-09-26 stsp logmsg_len = st2.st_size;
507 bfa12d5e 2020-09-26 stsp *logmsg = malloc(logmsg_len + 1);
508 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
509 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
510 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
511 3ce1b845 2019-07-15 stsp
512 00fe21f2 2021-12-31 stsp fp = fopen(logmsg_path, "re");
513 3ce1b845 2019-07-15 stsp if (fp == NULL) {
514 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
515 3ce1b845 2019-07-15 stsp goto done;
516 3ce1b845 2019-07-15 stsp }
517 bfa12d5e 2020-09-26 stsp
518 bfa12d5e 2020-09-26 stsp len = 0;
519 bfa12d5e 2020-09-26 stsp while ((linelen = getline(&line, &linesize, fp)) != -1) {
520 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
521 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
522 bfa12d5e 2020-09-26 stsp len = strlcat(*logmsg, line, logmsg_len + 1);
523 bfa12d5e 2020-09-26 stsp if (len >= logmsg_len + 1) {
524 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
525 bfa12d5e 2020-09-26 stsp goto done;
526 bfa12d5e 2020-09-26 stsp }
527 3ce1b845 2019-07-15 stsp }
528 bfa12d5e 2020-09-26 stsp free(line);
529 bfa12d5e 2020-09-26 stsp if (ferror(fp)) {
530 bfa12d5e 2020-09-26 stsp err = got_ferror(fp, GOT_ERR_IO);
531 bfa12d5e 2020-09-26 stsp goto done;
532 bfa12d5e 2020-09-26 stsp }
533 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
534 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
535 3ce1b845 2019-07-15 stsp len--;
536 3ce1b845 2019-07-15 stsp }
537 3ce1b845 2019-07-15 stsp
538 bfa12d5e 2020-09-26 stsp if (len == 0) {
539 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
540 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
541 bfa12d5e 2020-09-26 stsp goto done;
542 bfa12d5e 2020-09-26 stsp }
543 28cf319f 2021-01-28 stsp if (require_modification &&
544 28cf319f 2021-01-28 stsp strcmp(*logmsg, initial_content_stripped) == 0)
545 bfa12d5e 2020-09-26 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
546 bfa12d5e 2020-09-26 stsp "no changes made to commit message, aborting");
547 3ce1b845 2019-07-15 stsp done:
548 bfa12d5e 2020-09-26 stsp free(initial_content_stripped);
549 bfa12d5e 2020-09-26 stsp free(buf);
550 bfa12d5e 2020-09-26 stsp if (fp && fclose(fp) == EOF && err == NULL)
551 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("fclose");
552 3ce1b845 2019-07-15 stsp if (err) {
553 3ce1b845 2019-07-15 stsp free(*logmsg);
554 3ce1b845 2019-07-15 stsp *logmsg = NULL;
555 3ce1b845 2019-07-15 stsp }
556 3ce1b845 2019-07-15 stsp return err;
557 3ce1b845 2019-07-15 stsp }
558 3ce1b845 2019-07-15 stsp
559 3ce1b845 2019-07-15 stsp static const struct got_error *
560 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
561 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
562 3ce1b845 2019-07-15 stsp {
563 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
564 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
565 1601cb9f 2020-09-11 naddy int initial_content_len;
566 97972933 2020-09-11 stsp int fd = -1;
567 3ce1b845 2019-07-15 stsp
568 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
569 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
570 1601cb9f 2020-09-11 naddy branch_name);
571 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
572 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
573 3ce1b845 2019-07-15 stsp
574 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
575 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
576 3ce1b845 2019-07-15 stsp if (err)
577 3ce1b845 2019-07-15 stsp goto done;
578 3ce1b845 2019-07-15 stsp
579 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
580 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
581 97972933 2020-09-11 stsp goto done;
582 97972933 2020-09-11 stsp }
583 3ce1b845 2019-07-15 stsp
584 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
585 0d5bb276 2020-12-15 stsp initial_content_len, 1);
586 3ce1b845 2019-07-15 stsp done:
587 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
588 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
589 3ce1b845 2019-07-15 stsp free(initial_content);
590 59f86c76 2020-09-11 stsp if (err) {
591 59f86c76 2020-09-11 stsp free(*logmsg_path);
592 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
593 59f86c76 2020-09-11 stsp }
594 3ce1b845 2019-07-15 stsp return err;
595 3ce1b845 2019-07-15 stsp }
596 3ce1b845 2019-07-15 stsp
597 3ce1b845 2019-07-15 stsp static const struct got_error *
598 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
599 3ce1b845 2019-07-15 stsp {
600 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
601 3ce1b845 2019-07-15 stsp return NULL;
602 1d918cf9 2022-02-06 op }
603 1d918cf9 2022-02-06 op
604 1d918cf9 2022-02-06 op static int
605 1d918cf9 2022-02-06 op valid_author(const char *author)
606 1d918cf9 2022-02-06 op {
607 1d918cf9 2022-02-06 op /*
608 1d918cf9 2022-02-06 op * Really dumb email address check; we're only doing this to
609 1d918cf9 2022-02-06 op * avoid git's object parser breaking on commits we create.
610 1d918cf9 2022-02-06 op */
611 1d918cf9 2022-02-06 op while (*author && *author != '<')
612 1d918cf9 2022-02-06 op author++;
613 1d918cf9 2022-02-06 op if (*author != '<')
614 1d918cf9 2022-02-06 op return 0;
615 1d918cf9 2022-02-06 op while (*author && *author != '@')
616 1d918cf9 2022-02-06 op author++;
617 1d918cf9 2022-02-06 op if (*author != '@')
618 1d918cf9 2022-02-06 op return 0;
619 1d918cf9 2022-02-06 op while (*author && *author != '>')
620 1d918cf9 2022-02-06 op author++;
621 1d918cf9 2022-02-06 op return *author == '>';
622 3ce1b845 2019-07-15 stsp }
623 3ce1b845 2019-07-15 stsp
624 3ce1b845 2019-07-15 stsp static const struct got_error *
625 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
626 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
627 84792843 2019-08-09 stsp {
628 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
629 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
630 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
631 84792843 2019-08-09 stsp
632 84792843 2019-08-09 stsp *author = NULL;
633 aba9c984 2019-09-08 stsp
634 50b0790e 2020-09-11 stsp if (worktree)
635 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
636 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
637 50b0790e 2020-09-11 stsp
638 50b0790e 2020-09-11 stsp /*
639 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
640 50b0790e 2020-09-11 stsp * significant to least significant:
641 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
642 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
643 50b0790e 2020-09-11 stsp * 3) repository's git config file
644 50b0790e 2020-09-11 stsp * 4) environment variables
645 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
646 50b0790e 2020-09-11 stsp */
647 50b0790e 2020-09-11 stsp
648 50b0790e 2020-09-11 stsp if (worktree_conf)
649 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
650 50b0790e 2020-09-11 stsp if (got_author == NULL)
651 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
652 84792843 2019-08-09 stsp if (got_author == NULL) {
653 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
654 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
655 c9956ddf 2019-09-08 stsp if (name && email) {
656 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
657 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
658 c9956ddf 2019-09-08 stsp return NULL;
659 c9956ddf 2019-09-08 stsp }
660 257add31 2020-09-09 stsp
661 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
662 257add31 2020-09-09 stsp if (got_author == NULL) {
663 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
664 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
665 257add31 2020-09-09 stsp repo);
666 257add31 2020-09-09 stsp if (name && email) {
667 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
668 257add31 2020-09-09 stsp == -1)
669 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
670 257add31 2020-09-09 stsp return NULL;
671 257add31 2020-09-09 stsp }
672 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
673 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
674 257add31 2020-09-09 stsp }
675 84792843 2019-08-09 stsp }
676 84792843 2019-08-09 stsp
677 aba9c984 2019-09-08 stsp *author = strdup(got_author);
678 aba9c984 2019-09-08 stsp if (*author == NULL)
679 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
680 84792843 2019-08-09 stsp
681 1d918cf9 2022-02-06 op if (!valid_author(*author)) {
682 1d918cf9 2022-02-06 op err = got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", *author);
683 aba9c984 2019-09-08 stsp free(*author);
684 aba9c984 2019-09-08 stsp *author = NULL;
685 aba9c984 2019-09-08 stsp }
686 aba9c984 2019-09-08 stsp return err;
687 c9956ddf 2019-09-08 stsp }
688 c9956ddf 2019-09-08 stsp
689 c9956ddf 2019-09-08 stsp static const struct got_error *
690 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
691 c9956ddf 2019-09-08 stsp {
692 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
693 c9956ddf 2019-09-08 stsp
694 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
695 c9956ddf 2019-09-08 stsp if (homedir) {
696 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
697 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
698 c9956ddf 2019-09-08 stsp
699 c9956ddf 2019-09-08 stsp }
700 c9956ddf 2019-09-08 stsp return NULL;
701 84792843 2019-08-09 stsp }
702 84792843 2019-08-09 stsp
703 84792843 2019-08-09 stsp static const struct got_error *
704 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
705 3ce1b845 2019-07-15 stsp {
706 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
707 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
708 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
709 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
710 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
711 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
712 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
713 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
714 3ce1b845 2019-07-15 stsp int ch;
715 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
716 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
717 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
718 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
719 3ce1b845 2019-07-15 stsp
720 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
721 3ce1b845 2019-07-15 stsp
722 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
723 3ce1b845 2019-07-15 stsp switch (ch) {
724 3ce1b845 2019-07-15 stsp case 'b':
725 3ce1b845 2019-07-15 stsp branch_name = optarg;
726 3ce1b845 2019-07-15 stsp break;
727 3ce1b845 2019-07-15 stsp case 'm':
728 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
729 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
730 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
731 3ce1b845 2019-07-15 stsp goto done;
732 3ce1b845 2019-07-15 stsp }
733 3ce1b845 2019-07-15 stsp break;
734 3ce1b845 2019-07-15 stsp case 'r':
735 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
736 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
737 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
738 9ba1d308 2019-10-21 stsp optarg);
739 3ce1b845 2019-07-15 stsp goto done;
740 3ce1b845 2019-07-15 stsp }
741 3ce1b845 2019-07-15 stsp break;
742 3ce1b845 2019-07-15 stsp case 'I':
743 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
744 3ce1b845 2019-07-15 stsp break;
745 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
746 3ce1b845 2019-07-15 stsp NULL);
747 3ce1b845 2019-07-15 stsp if (error)
748 3ce1b845 2019-07-15 stsp goto done;
749 3ce1b845 2019-07-15 stsp break;
750 3ce1b845 2019-07-15 stsp default:
751 b2b65d18 2019-08-22 stsp usage_import();
752 3ce1b845 2019-07-15 stsp /* NOTREACHED */
753 3ce1b845 2019-07-15 stsp }
754 3ce1b845 2019-07-15 stsp }
755 3ce1b845 2019-07-15 stsp
756 3ce1b845 2019-07-15 stsp argc -= optind;
757 3ce1b845 2019-07-15 stsp argv += optind;
758 3ce1b845 2019-07-15 stsp
759 3ce1b845 2019-07-15 stsp #ifndef PROFILE
760 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
761 aba9c984 2019-09-08 stsp "unveil",
762 3ce1b845 2019-07-15 stsp NULL) == -1)
763 3ce1b845 2019-07-15 stsp err(1, "pledge");
764 3ce1b845 2019-07-15 stsp #endif
765 3ce1b845 2019-07-15 stsp if (argc != 1)
766 3ce1b845 2019-07-15 stsp usage_import();
767 2c7829a4 2019-06-17 stsp
768 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
769 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
770 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
771 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
772 3ce1b845 2019-07-15 stsp }
773 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
774 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
775 c9956ddf 2019-09-08 stsp if (error)
776 c9956ddf 2019-09-08 stsp goto done;
777 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
778 0ae84acc 2022-06-15 tracey if (error != NULL)
779 0ae84acc 2022-06-15 tracey goto done;
780 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
781 3ce1b845 2019-07-15 stsp if (error)
782 3ce1b845 2019-07-15 stsp goto done;
783 aba9c984 2019-09-08 stsp
784 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
785 aba9c984 2019-09-08 stsp if (error)
786 aba9c984 2019-09-08 stsp return error;
787 e560b7e0 2019-11-28 stsp
788 e560b7e0 2019-11-28 stsp /*
789 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
790 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
791 e560b7e0 2019-11-28 stsp * an unintended typo.
792 e560b7e0 2019-11-28 stsp */
793 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
794 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
795 3ce1b845 2019-07-15 stsp
796 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
797 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
798 3ce1b845 2019-07-15 stsp goto done;
799 3ce1b845 2019-07-15 stsp }
800 3ce1b845 2019-07-15 stsp
801 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
802 3ce1b845 2019-07-15 stsp if (error) {
803 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
804 3ce1b845 2019-07-15 stsp goto done;
805 3ce1b845 2019-07-15 stsp } else {
806 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
807 3ce1b845 2019-07-15 stsp "import target branch already exists");
808 3ce1b845 2019-07-15 stsp goto done;
809 3ce1b845 2019-07-15 stsp }
810 3ce1b845 2019-07-15 stsp
811 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
812 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
813 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
814 3ce1b845 2019-07-15 stsp goto done;
815 3ce1b845 2019-07-15 stsp }
816 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
817 3ce1b845 2019-07-15 stsp
818 3ce1b845 2019-07-15 stsp /*
819 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
820 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
821 3ce1b845 2019-07-15 stsp */
822 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
823 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
824 3ce1b845 2019-07-15 stsp if (error)
825 3ce1b845 2019-07-15 stsp goto done;
826 8e158b01 2019-09-22 stsp free(logmsg);
827 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
828 ef293bdd 2019-10-21 stsp path_dir, refname);
829 ef293bdd 2019-10-21 stsp if (error) {
830 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
831 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
832 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
833 3ce1b845 2019-07-15 stsp goto done;
834 ef293bdd 2019-10-21 stsp }
835 3ce1b845 2019-07-15 stsp }
836 3ce1b845 2019-07-15 stsp
837 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
838 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
839 ef293bdd 2019-10-21 stsp if (logmsg_path)
840 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
841 3ce1b845 2019-07-15 stsp goto done;
842 ef293bdd 2019-10-21 stsp }
843 3ce1b845 2019-07-15 stsp
844 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
845 ef293bdd 2019-10-21 stsp if (error) {
846 ef293bdd 2019-10-21 stsp if (logmsg_path)
847 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
848 ef293bdd 2019-10-21 stsp goto done;
849 ef293bdd 2019-10-21 stsp }
850 ef293bdd 2019-10-21 stsp
851 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
852 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
853 ef293bdd 2019-10-21 stsp if (error) {
854 ef293bdd 2019-10-21 stsp if (logmsg_path)
855 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
856 3ce1b845 2019-07-15 stsp goto done;
857 ef293bdd 2019-10-21 stsp }
858 3ce1b845 2019-07-15 stsp
859 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
860 ef293bdd 2019-10-21 stsp if (error) {
861 ef293bdd 2019-10-21 stsp if (logmsg_path)
862 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
863 3ce1b845 2019-07-15 stsp goto done;
864 ef293bdd 2019-10-21 stsp }
865 3ce1b845 2019-07-15 stsp
866 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
867 ef293bdd 2019-10-21 stsp if (error) {
868 ef293bdd 2019-10-21 stsp if (logmsg_path)
869 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
870 3ce1b845 2019-07-15 stsp goto done;
871 ef293bdd 2019-10-21 stsp }
872 3ce1b845 2019-07-15 stsp
873 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
874 ef293bdd 2019-10-21 stsp if (error) {
875 ef293bdd 2019-10-21 stsp if (logmsg_path)
876 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
877 3ce1b845 2019-07-15 stsp goto done;
878 ef293bdd 2019-10-21 stsp }
879 3ce1b845 2019-07-15 stsp
880 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
881 3ce1b845 2019-07-15 stsp if (error) {
882 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
883 ef293bdd 2019-10-21 stsp if (logmsg_path)
884 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
885 3ce1b845 2019-07-15 stsp goto done;
886 ef293bdd 2019-10-21 stsp }
887 3ce1b845 2019-07-15 stsp
888 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
889 3ce1b845 2019-07-15 stsp branch_ref);
890 ef293bdd 2019-10-21 stsp if (error) {
891 ef293bdd 2019-10-21 stsp if (logmsg_path)
892 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
893 3ce1b845 2019-07-15 stsp goto done;
894 ef293bdd 2019-10-21 stsp }
895 3ce1b845 2019-07-15 stsp
896 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
897 ef293bdd 2019-10-21 stsp if (error) {
898 ef293bdd 2019-10-21 stsp if (logmsg_path)
899 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
900 3ce1b845 2019-07-15 stsp goto done;
901 ef293bdd 2019-10-21 stsp }
902 3ce1b845 2019-07-15 stsp }
903 3ce1b845 2019-07-15 stsp
904 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
905 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
906 2c7829a4 2019-06-17 stsp done:
907 0ae84acc 2022-06-15 tracey if (pack_fds) {
908 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
909 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
910 0ae84acc 2022-06-15 tracey if (error == NULL)
911 0ae84acc 2022-06-15 tracey error = pack_err;
912 0ae84acc 2022-06-15 tracey }
913 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
914 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
915 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
916 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
917 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
918 8e158b01 2019-09-22 stsp free(logmsg);
919 ef293bdd 2019-10-21 stsp free(logmsg_path);
920 2c7829a4 2019-06-17 stsp free(repo_path);
921 3ce1b845 2019-07-15 stsp free(editor);
922 3ce1b845 2019-07-15 stsp free(refname);
923 3ce1b845 2019-07-15 stsp free(new_commit_id);
924 3ce1b845 2019-07-15 stsp free(id_str);
925 aba9c984 2019-09-08 stsp free(author);
926 c9956ddf 2019-09-08 stsp free(gitconfig_path);
927 3ce1b845 2019-07-15 stsp if (branch_ref)
928 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
929 3ce1b845 2019-07-15 stsp if (head_ref)
930 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
931 2c7829a4 2019-06-17 stsp return error;
932 93658fb9 2020-03-18 stsp }
933 93658fb9 2020-03-18 stsp
934 93658fb9 2020-03-18 stsp __dead static void
935 93658fb9 2020-03-18 stsp usage_clone(void)
936 93658fb9 2020-03-18 stsp {
937 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
938 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
939 93658fb9 2020-03-18 stsp exit(1);
940 93658fb9 2020-03-18 stsp }
941 892ac3b6 2020-03-18 stsp
942 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
943 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
944 892ac3b6 2020-03-18 stsp int last_p_indexed;
945 892ac3b6 2020-03-18 stsp int last_p_resolved;
946 68999b92 2020-03-18 stsp int verbosity;
947 04d9a9ec 2020-09-24 stsp
948 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
949 04d9a9ec 2020-09-24 stsp
950 04d9a9ec 2020-09-24 stsp int create_configs;
951 04d9a9ec 2020-09-24 stsp int configs_created;
952 04d9a9ec 2020-09-24 stsp struct {
953 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
954 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
955 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs;
956 04d9a9ec 2020-09-24 stsp const char *proto;
957 04d9a9ec 2020-09-24 stsp const char *host;
958 04d9a9ec 2020-09-24 stsp const char *port;
959 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
960 04d9a9ec 2020-09-24 stsp const char *git_url;
961 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
962 04d9a9ec 2020-09-24 stsp int mirror_references;
963 04d9a9ec 2020-09-24 stsp } config_info;
964 892ac3b6 2020-03-18 stsp };
965 93658fb9 2020-03-18 stsp
966 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
967 93658fb9 2020-03-18 stsp static const struct got_error *
968 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
969 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
970 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
971 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
972 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo);
973 04d9a9ec 2020-09-24 stsp
974 04d9a9ec 2020-09-24 stsp static const struct got_error *
975 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
976 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
977 531c3985 2020-03-18 stsp {
978 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
979 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
980 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
981 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
982 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
983 04d9a9ec 2020-09-24 stsp
984 04d9a9ec 2020-09-24 stsp /*
985 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
986 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
987 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
988 04d9a9ec 2020-09-24 stsp * we have all required information.
989 04d9a9ec 2020-09-24 stsp */
990 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
991 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
992 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
993 62d463ca 2020-10-20 naddy a->config_info.host, a->config_info.port,
994 62d463ca 2020-10-20 naddy a->config_info.remote_repo_path,
995 62d463ca 2020-10-20 naddy a->config_info.git_url,
996 62d463ca 2020-10-20 naddy a->config_info.fetch_all_branches,
997 62d463ca 2020-10-20 naddy a->config_info.mirror_references,
998 62d463ca 2020-10-20 naddy a->config_info.symrefs,
999 99495ddb 2021-01-10 stsp a->config_info.wanted_branches,
1000 99495ddb 2021-01-10 stsp a->config_info.wanted_refs, a->repo);
1001 04d9a9ec 2020-09-24 stsp if (err)
1002 04d9a9ec 2020-09-24 stsp return err;
1003 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
1004 04d9a9ec 2020-09-24 stsp }
1005 b2409d58 2020-03-18 stsp
1006 68999b92 2020-03-18 stsp if (a->verbosity < 0)
1007 68999b92 2020-03-18 stsp return NULL;
1008 68999b92 2020-03-18 stsp
1009 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
1010 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
1011 892ac3b6 2020-03-18 stsp fflush(stdout);
1012 12d1281e 2020-03-19 stsp return NULL;
1013 b2409d58 2020-03-18 stsp }
1014 b2409d58 2020-03-18 stsp
1015 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
1016 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1017 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
1018 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
1019 892ac3b6 2020-03-18 stsp print_size = 1;
1020 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
1021 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1022 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
1023 892ac3b6 2020-03-18 stsp }
1024 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
1025 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
1026 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
1027 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
1028 892ac3b6 2020-03-18 stsp print_indexed = 1;
1029 892ac3b6 2020-03-18 stsp print_size = 1;
1030 892ac3b6 2020-03-18 stsp }
1031 61cc1a7a 2020-03-18 stsp }
1032 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
1033 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
1034 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
1035 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
1036 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
1037 892ac3b6 2020-03-18 stsp print_resolved = 1;
1038 892ac3b6 2020-03-18 stsp print_indexed = 1;
1039 892ac3b6 2020-03-18 stsp print_size = 1;
1040 892ac3b6 2020-03-18 stsp }
1041 61cc1a7a 2020-03-18 stsp }
1042 3168e5da 2020-09-10 stsp
1043 d2cdc636 2020-03-18 stsp }
1044 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1045 892ac3b6 2020-03-18 stsp printf("\r");
1046 892ac3b6 2020-03-18 stsp if (print_size)
1047 b5934965 2022-02-12 naddy printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1048 d715f13e 2020-03-19 stsp if (print_indexed)
1049 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
1050 d715f13e 2020-03-19 stsp if (print_resolved)
1051 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
1052 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1053 892ac3b6 2020-03-18 stsp fflush(stdout);
1054 892ac3b6 2020-03-18 stsp
1055 531c3985 2020-03-18 stsp return NULL;
1056 531c3985 2020-03-18 stsp }
1057 531c3985 2020-03-18 stsp
1058 531c3985 2020-03-18 stsp static const struct got_error *
1059 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
1060 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
1061 4ba14133 2020-03-20 stsp {
1062 4ba14133 2020-03-20 stsp const struct got_error *err;
1063 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
1064 4ba14133 2020-03-20 stsp
1065 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1066 4ba14133 2020-03-20 stsp if (err)
1067 4ba14133 2020-03-20 stsp return err;
1068 4ba14133 2020-03-20 stsp
1069 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
1070 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
1071 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
1072 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
1073 6338a6a1 2020-03-21 stsp }
1074 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
1075 4ba14133 2020-03-20 stsp return err;
1076 4ba14133 2020-03-20 stsp }
1077 4ba14133 2020-03-20 stsp
1078 4ba14133 2020-03-20 stsp static const struct got_error *
1079 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
1080 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
1081 41b0de12 2020-03-21 stsp {
1082 41b0de12 2020-03-21 stsp const struct got_error *err;
1083 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
1084 41b0de12 2020-03-21 stsp
1085 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1086 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1087 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1088 41b0de12 2020-03-21 stsp
1089 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1090 41b0de12 2020-03-21 stsp }
1091 41b0de12 2020-03-21 stsp
1092 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1093 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1094 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1095 41b0de12 2020-03-21 stsp char *id_str;
1096 41b0de12 2020-03-21 stsp
1097 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1098 41b0de12 2020-03-21 stsp if (err)
1099 41b0de12 2020-03-21 stsp return err;
1100 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1101 41b0de12 2020-03-21 stsp free(id_str);
1102 41b0de12 2020-03-21 stsp }
1103 41b0de12 2020-03-21 stsp
1104 41b0de12 2020-03-21 stsp return NULL;
1105 6338a6a1 2020-03-21 stsp }
1106 6338a6a1 2020-03-21 stsp
1107 6338a6a1 2020-03-21 stsp static const struct got_error *
1108 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1109 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1110 6338a6a1 2020-03-21 stsp {
1111 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1112 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1113 6338a6a1 2020-03-21 stsp char *id_str;
1114 6338a6a1 2020-03-21 stsp
1115 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1116 6338a6a1 2020-03-21 stsp if (err)
1117 6338a6a1 2020-03-21 stsp return err;
1118 6338a6a1 2020-03-21 stsp
1119 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1120 6338a6a1 2020-03-21 stsp if (err)
1121 6338a6a1 2020-03-21 stsp goto done;
1122 6338a6a1 2020-03-21 stsp
1123 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1124 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1125 6338a6a1 2020-03-21 stsp
1126 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1127 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1128 6338a6a1 2020-03-21 stsp done:
1129 6338a6a1 2020-03-21 stsp free(id_str);
1130 6338a6a1 2020-03-21 stsp return err;
1131 0e4002ca 2020-03-21 stsp }
1132 0e4002ca 2020-03-21 stsp
1133 0e4002ca 2020-03-21 stsp static int
1134 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1135 0e4002ca 2020-03-21 stsp {
1136 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1137 0e4002ca 2020-03-21 stsp return 0;
1138 0e4002ca 2020-03-21 stsp refname += 5;
1139 0e4002ca 2020-03-21 stsp
1140 0e4002ca 2020-03-21 stsp /*
1141 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1142 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1143 0e4002ca 2020-03-21 stsp */
1144 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1145 0e4002ca 2020-03-21 stsp return 0;
1146 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1147 0e4002ca 2020-03-21 stsp return 0;
1148 0e4002ca 2020-03-21 stsp
1149 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1150 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1151 0e4002ca 2020-03-21 stsp
1152 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1153 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1154 0e4002ca 2020-03-21 stsp return 1;
1155 0e4002ca 2020-03-21 stsp
1156 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1157 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1158 41b0de12 2020-03-21 stsp }
1159 41b0de12 2020-03-21 stsp
1160 0e4002ca 2020-03-21 stsp static int
1161 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1162 0e4002ca 2020-03-21 stsp {
1163 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1164 0e4002ca 2020-03-21 stsp
1165 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1166 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1167 0e4002ca 2020-03-21 stsp return 1;
1168 0e4002ca 2020-03-21 stsp }
1169 0e4002ca 2020-03-21 stsp
1170 0e4002ca 2020-03-21 stsp return 0;
1171 0e4002ca 2020-03-21 stsp }
1172 0e4002ca 2020-03-21 stsp
1173 41b0de12 2020-03-21 stsp static const struct got_error *
1174 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1175 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1176 0e4002ca 2020-03-21 stsp {
1177 0e4002ca 2020-03-21 stsp const struct got_error *err;
1178 0e4002ca 2020-03-21 stsp char *remote_refname;
1179 0e4002ca 2020-03-21 stsp
1180 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1181 0e4002ca 2020-03-21 stsp refname += 5;
1182 0e4002ca 2020-03-21 stsp
1183 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1184 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1185 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1186 0e4002ca 2020-03-21 stsp
1187 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1188 0e4002ca 2020-03-21 stsp free(remote_refname);
1189 7c0b7f42 2020-09-24 stsp return err;
1190 7c0b7f42 2020-09-24 stsp }
1191 7c0b7f42 2020-09-24 stsp
1192 7c0b7f42 2020-09-24 stsp static const struct got_error *
1193 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1194 15d3c221 2021-01-05 stsp const char *remote_repo_path, const char *default_branch,
1195 0c8b29c5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1196 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1197 99495ddb 2021-01-10 stsp struct got_repository *repo)
1198 7c0b7f42 2020-09-24 stsp {
1199 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1200 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1201 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1202 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1203 15d3c221 2021-01-05 stsp const char *branchname = NULL;
1204 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1205 7c0b7f42 2020-09-24 stsp ssize_t n;
1206 7c0b7f42 2020-09-24 stsp
1207 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1208 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1209 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1210 132af4a5 2021-01-05 stsp char *s;
1211 132af4a5 2021-01-05 stsp branchname = pe->path;
1212 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1213 132af4a5 2021-01-05 stsp branchname += 11;
1214 132af4a5 2021-01-05 stsp if (asprintf(&s, "%s\"%s\" ",
1215 132af4a5 2021-01-05 stsp branches ? branches : "", branchname) == -1) {
1216 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1217 132af4a5 2021-01-05 stsp goto done;
1218 132af4a5 2021-01-05 stsp }
1219 132af4a5 2021-01-05 stsp free(branches);
1220 132af4a5 2021-01-05 stsp branches = s;
1221 132af4a5 2021-01-05 stsp }
1222 0c8b29c5 2021-01-05 stsp } else if (!fetch_all_branches && default_branch) {
1223 15d3c221 2021-01-05 stsp branchname = default_branch;
1224 15d3c221 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1225 15d3c221 2021-01-05 stsp branchname += 11;
1226 132af4a5 2021-01-05 stsp if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1227 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1228 132af4a5 2021-01-05 stsp goto done;
1229 99495ddb 2021-01-10 stsp }
1230 99495ddb 2021-01-10 stsp }
1231 99495ddb 2021-01-10 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1232 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1233 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1234 99495ddb 2021-01-10 stsp char *s;
1235 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1236 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1237 99495ddb 2021-01-10 stsp branchname += 5;
1238 99495ddb 2021-01-10 stsp if (asprintf(&s, "%s\"%s\" ",
1239 99495ddb 2021-01-10 stsp refs ? refs : "", refname) == -1) {
1240 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1241 99495ddb 2021-01-10 stsp goto done;
1242 99495ddb 2021-01-10 stsp }
1243 99495ddb 2021-01-10 stsp free(refs);
1244 99495ddb 2021-01-10 stsp refs = s;
1245 132af4a5 2021-01-05 stsp }
1246 15d3c221 2021-01-05 stsp }
1247 15d3c221 2021-01-05 stsp
1248 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1249 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1250 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1251 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1252 7c0b7f42 2020-09-24 stsp goto done;
1253 7c0b7f42 2020-09-24 stsp }
1254 00fe21f2 2021-12-31 stsp gotconfig_file = fopen(gotconfig_path, "ae");
1255 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1256 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1257 7c0b7f42 2020-09-24 stsp goto done;
1258 7c0b7f42 2020-09-24 stsp }
1259 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1260 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1261 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1262 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1263 7c0b7f42 2020-09-24 stsp "%s%s%s"
1264 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1265 15d3c221 2021-01-05 stsp "%s%s%s"
1266 99495ddb 2021-01-10 stsp "%s%s%s"
1267 7c0b7f42 2020-09-24 stsp "%s"
1268 0c8b29c5 2021-01-05 stsp "%s"
1269 7c0b7f42 2020-09-24 stsp "}\n",
1270 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1271 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1272 132af4a5 2021-01-05 stsp remote_repo_path, branches ? "\tbranch { " : "",
1273 132af4a5 2021-01-05 stsp branches ? branches : "", branches ? "}\n" : "",
1274 99495ddb 2021-01-10 stsp refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1275 0c8b29c5 2021-01-05 stsp mirror_references ? "\tmirror-references yes\n" : "",
1276 0c8b29c5 2021-01-05 stsp fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1277 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1278 7c0b7f42 2020-09-24 stsp goto done;
1279 7c0b7f42 2020-09-24 stsp }
1280 7c0b7f42 2020-09-24 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1281 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1282 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1283 7c0b7f42 2020-09-24 stsp goto done;
1284 7c0b7f42 2020-09-24 stsp }
1285 7c0b7f42 2020-09-24 stsp
1286 7c0b7f42 2020-09-24 stsp done:
1287 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1288 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1289 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1290 132af4a5 2021-01-05 stsp free(branches);
1291 7c0b7f42 2020-09-24 stsp return err;
1292 7c0b7f42 2020-09-24 stsp }
1293 7c0b7f42 2020-09-24 stsp
1294 7c0b7f42 2020-09-24 stsp static const struct got_error *
1295 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1296 132af4a5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1297 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1298 99495ddb 2021-01-10 stsp struct got_repository *repo)
1299 7c0b7f42 2020-09-24 stsp {
1300 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1301 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1302 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1303 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1304 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1305 56d0a753 2021-01-20 stsp const char *branchname;
1306 7c0b7f42 2020-09-24 stsp ssize_t n;
1307 7c0b7f42 2020-09-24 stsp
1308 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1309 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1310 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1311 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1312 7c0b7f42 2020-09-24 stsp goto done;
1313 7c0b7f42 2020-09-24 stsp }
1314 00fe21f2 2021-12-31 stsp gitconfig_file = fopen(gitconfig_path, "ae");
1315 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1316 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1317 7c0b7f42 2020-09-24 stsp goto done;
1318 7c0b7f42 2020-09-24 stsp }
1319 56d0a753 2021-01-20 stsp if (fetch_all_branches) {
1320 56d0a753 2021-01-20 stsp if (mirror_references) {
1321 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1322 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1323 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1324 56d0a753 2021-01-20 stsp goto done;
1325 56d0a753 2021-01-20 stsp }
1326 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1327 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1328 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1329 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1330 7c0b7f42 2020-09-24 stsp goto done;
1331 132af4a5 2021-01-05 stsp }
1332 132af4a5 2021-01-05 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
1333 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1334 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1335 132af4a5 2021-01-05 stsp char *s;
1336 132af4a5 2021-01-05 stsp branchname = pe->path;
1337 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1338 132af4a5 2021-01-05 stsp branchname += 11;
1339 56d0a753 2021-01-20 stsp if (mirror_references) {
1340 56d0a753 2021-01-20 stsp if (asprintf(&s,
1341 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1342 56d0a753 2021-01-20 stsp branches ? branches : "",
1343 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1344 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1345 56d0a753 2021-01-20 stsp goto done;
1346 56d0a753 2021-01-20 stsp }
1347 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1348 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1349 132af4a5 2021-01-05 stsp branches ? branches : "",
1350 132af4a5 2021-01-05 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1351 132af4a5 2021-01-05 stsp branchname) == -1) {
1352 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1353 132af4a5 2021-01-05 stsp goto done;
1354 132af4a5 2021-01-05 stsp }
1355 132af4a5 2021-01-05 stsp free(branches);
1356 132af4a5 2021-01-05 stsp branches = s;
1357 7c0b7f42 2020-09-24 stsp }
1358 7c0b7f42 2020-09-24 stsp } else {
1359 7c0b7f42 2020-09-24 stsp /*
1360 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1361 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1362 7c0b7f42 2020-09-24 stsp */
1363 04d9a9ec 2020-09-24 stsp if (default_branch) {
1364 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1365 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1366 7c0b7f42 2020-09-24 stsp branchname += 11;
1367 7c0b7f42 2020-09-24 stsp } else
1368 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1369 56d0a753 2021-01-20 stsp if (mirror_references) {
1370 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1371 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/heads/%s\n",
1372 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1373 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1374 56d0a753 2021-01-20 stsp goto done;
1375 56d0a753 2021-01-20 stsp }
1376 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1377 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1378 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1379 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1380 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1381 7c0b7f42 2020-09-24 stsp goto done;
1382 99495ddb 2021-01-10 stsp }
1383 99495ddb 2021-01-10 stsp }
1384 56d0a753 2021-01-20 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1385 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1386 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1387 99495ddb 2021-01-10 stsp char *s;
1388 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1389 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1390 99495ddb 2021-01-10 stsp refname += 5;
1391 56d0a753 2021-01-20 stsp if (mirror_references) {
1392 56d0a753 2021-01-20 stsp if (asprintf(&s,
1393 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/%s\n",
1394 56d0a753 2021-01-20 stsp refs ? refs : "", refname, refname) == -1) {
1395 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1396 56d0a753 2021-01-20 stsp goto done;
1397 56d0a753 2021-01-20 stsp }
1398 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1399 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1400 99495ddb 2021-01-10 stsp refs ? refs : "",
1401 99495ddb 2021-01-10 stsp refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1402 99495ddb 2021-01-10 stsp refname) == -1) {
1403 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1404 99495ddb 2021-01-10 stsp goto done;
1405 99495ddb 2021-01-10 stsp }
1406 99495ddb 2021-01-10 stsp free(refs);
1407 99495ddb 2021-01-10 stsp refs = s;
1408 7c0b7f42 2020-09-24 stsp }
1409 132af4a5 2021-01-05 stsp }
1410 99495ddb 2021-01-10 stsp
1411 132af4a5 2021-01-05 stsp if (asprintf(&gitconfig,
1412 132af4a5 2021-01-05 stsp "[remote \"%s\"]\n"
1413 132af4a5 2021-01-05 stsp "\turl = %s\n"
1414 132af4a5 2021-01-05 stsp "%s"
1415 99495ddb 2021-01-10 stsp "%s"
1416 56d0a753 2021-01-20 stsp "\tfetch = refs/tags/*:refs/tags/*\n",
1417 132af4a5 2021-01-05 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1418 56d0a753 2021-01-20 stsp refs ? refs : "") == -1) {
1419 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1420 132af4a5 2021-01-05 stsp goto done;
1421 7c0b7f42 2020-09-24 stsp }
1422 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1423 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1424 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1425 7c0b7f42 2020-09-24 stsp goto done;
1426 7c0b7f42 2020-09-24 stsp }
1427 7c0b7f42 2020-09-24 stsp done:
1428 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1429 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1430 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1431 132af4a5 2021-01-05 stsp free(branches);
1432 0e4002ca 2020-03-21 stsp return err;
1433 0e4002ca 2020-03-21 stsp }
1434 0e4002ca 2020-03-21 stsp
1435 0e4002ca 2020-03-21 stsp static const struct got_error *
1436 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1437 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1438 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1439 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
1440 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1441 04d9a9ec 2020-09-24 stsp {
1442 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1443 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1444 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1445 04d9a9ec 2020-09-24 stsp
1446 04d9a9ec 2020-09-24 stsp /*
1447 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1448 04d9a9ec 2020-09-24 stsp * one of those.
1449 04d9a9ec 2020-09-24 stsp */
1450 62d463ca 2020-10-20 naddy if (!TAILQ_EMPTY(wanted_branches)) {
1451 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1452 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1453 04d9a9ec 2020-09-24 stsp } else {
1454 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1455 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1456 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1457 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1458 04d9a9ec 2020-09-24 stsp
1459 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1460 04d9a9ec 2020-09-24 stsp continue;
1461 04d9a9ec 2020-09-24 stsp
1462 04d9a9ec 2020-09-24 stsp default_branch = target;
1463 04d9a9ec 2020-09-24 stsp break;
1464 04d9a9ec 2020-09-24 stsp }
1465 04d9a9ec 2020-09-24 stsp }
1466 04d9a9ec 2020-09-24 stsp
1467 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1468 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1469 0c8b29c5 2021-01-05 stsp default_branch, fetch_all_branches, wanted_branches,
1470 99495ddb 2021-01-10 stsp wanted_refs, mirror_references, repo);
1471 04d9a9ec 2020-09-24 stsp if (err)
1472 04d9a9ec 2020-09-24 stsp return err;
1473 04d9a9ec 2020-09-24 stsp
1474 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1475 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1476 99495ddb 2021-01-10 stsp wanted_branches, wanted_refs, mirror_references, repo);
1477 04d9a9ec 2020-09-24 stsp }
1478 04d9a9ec 2020-09-24 stsp
1479 04d9a9ec 2020-09-24 stsp static const struct got_error *
1480 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1481 93658fb9 2020-03-18 stsp {
1482 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1483 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1484 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1485 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1486 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1487 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1488 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1489 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1490 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1491 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1492 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1493 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1494 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1495 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1496 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1497 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
1498 93658fb9 2020-03-18 stsp
1499 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1500 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1501 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1502 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1503 d9b4d0c0 2020-03-18 stsp
1504 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1505 93658fb9 2020-03-18 stsp switch (ch) {
1506 659e7fbd 2020-03-20 stsp case 'a':
1507 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1508 4ba14133 2020-03-20 stsp break;
1509 4ba14133 2020-03-20 stsp case 'b':
1510 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1511 4ba14133 2020-03-20 stsp optarg, NULL);
1512 4ba14133 2020-03-20 stsp if (error)
1513 4ba14133 2020-03-20 stsp return error;
1514 659e7fbd 2020-03-20 stsp break;
1515 41b0de12 2020-03-21 stsp case 'l':
1516 41b0de12 2020-03-21 stsp list_refs_only = 1;
1517 41b0de12 2020-03-21 stsp break;
1518 469dd726 2020-03-20 stsp case 'm':
1519 469dd726 2020-03-20 stsp mirror_references = 1;
1520 469dd726 2020-03-20 stsp break;
1521 68999b92 2020-03-18 stsp case 'v':
1522 68999b92 2020-03-18 stsp if (verbosity < 0)
1523 68999b92 2020-03-18 stsp verbosity = 0;
1524 68999b92 2020-03-18 stsp else if (verbosity < 3)
1525 68999b92 2020-03-18 stsp verbosity++;
1526 68999b92 2020-03-18 stsp break;
1527 68999b92 2020-03-18 stsp case 'q':
1528 68999b92 2020-03-18 stsp verbosity = -1;
1529 68999b92 2020-03-18 stsp break;
1530 0e4002ca 2020-03-21 stsp case 'R':
1531 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1532 0e4002ca 2020-03-21 stsp optarg, NULL);
1533 0e4002ca 2020-03-21 stsp if (error)
1534 0e4002ca 2020-03-21 stsp return error;
1535 0e4002ca 2020-03-21 stsp break;
1536 93658fb9 2020-03-18 stsp default:
1537 93658fb9 2020-03-18 stsp usage_clone();
1538 93658fb9 2020-03-18 stsp break;
1539 93658fb9 2020-03-18 stsp }
1540 93658fb9 2020-03-18 stsp }
1541 93658fb9 2020-03-18 stsp argc -= optind;
1542 93658fb9 2020-03-18 stsp argv += optind;
1543 39c64a6a 2020-03-18 stsp
1544 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1545 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
1546 41b0de12 2020-03-21 stsp if (list_refs_only) {
1547 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1548 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
1549 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1550 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
1551 41b0de12 2020-03-21 stsp if (mirror_references)
1552 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
1553 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1554 ff69268e 2020-12-13 stsp option_conflict('l', 'R');
1555 41b0de12 2020-03-21 stsp }
1556 4ba14133 2020-03-20 stsp
1557 93658fb9 2020-03-18 stsp uri = argv[0];
1558 9df6f38b 2020-03-18 stsp
1559 9df6f38b 2020-03-18 stsp if (argc == 1)
1560 93658fb9 2020-03-18 stsp dirname = NULL;
1561 9df6f38b 2020-03-18 stsp else if (argc == 2)
1562 93658fb9 2020-03-18 stsp dirname = argv[1];
1563 93658fb9 2020-03-18 stsp else
1564 93658fb9 2020-03-18 stsp usage_clone();
1565 09838ffc 2020-03-18 stsp
1566 5e5da8c4 2021-09-05 stsp error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1567 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1568 39c64a6a 2020-03-18 stsp if (error)
1569 09f63084 2020-03-20 stsp goto done;
1570 09f63084 2020-03-20 stsp
1571 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1572 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1573 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1574 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1575 09838ffc 2020-03-18 stsp goto done;
1576 09f63084 2020-03-20 stsp }
1577 09838ffc 2020-03-18 stsp
1578 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1579 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1580 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1581 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1582 39c64a6a 2020-03-18 stsp err(1, "pledge");
1583 b46f3e71 2020-03-18 stsp #endif
1584 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1585 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1586 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1587 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1588 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1589 39c64a6a 2020-03-18 stsp err(1, "pledge");
1590 b46f3e71 2020-03-18 stsp #endif
1591 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1592 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1593 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1594 39c64a6a 2020-03-18 stsp goto done;
1595 39c64a6a 2020-03-18 stsp } else {
1596 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1597 39c64a6a 2020-03-18 stsp goto done;
1598 39c64a6a 2020-03-18 stsp }
1599 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1600 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1601 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1602 bb64b798 2020-03-18 stsp goto done;
1603 bb64b798 2020-03-18 stsp }
1604 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1605 bb64b798 2020-03-18 stsp } else
1606 bb64b798 2020-03-18 stsp repo_path = dirname;
1607 bb64b798 2020-03-18 stsp
1608 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1609 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1610 2751fe64 2020-09-24 stsp if (error &&
1611 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1612 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1613 41b0de12 2020-03-21 stsp goto done;
1614 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1615 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1616 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1617 41b0de12 2020-03-21 stsp goto done;
1618 2751fe64 2020-09-24 stsp }
1619 41b0de12 2020-03-21 stsp }
1620 bb64b798 2020-03-18 stsp
1621 d65a88a2 2021-09-05 stsp error = got_dial_apply_unveil(proto);
1622 d65a88a2 2021-09-05 stsp if (error)
1623 d65a88a2 2021-09-05 stsp goto done;
1624 d65a88a2 2021-09-05 stsp
1625 f535bcd4 2020-09-30 stsp error = apply_unveil(repo_path, 0, NULL);
1626 ee448f5f 2020-03-18 stsp if (error)
1627 ee448f5f 2020-03-18 stsp goto done;
1628 f79e6490 2020-04-19 stsp
1629 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1630 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1631 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1632 ee448f5f 2020-03-18 stsp
1633 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1634 9c52365f 2020-03-21 stsp server_path, verbosity);
1635 39c64a6a 2020-03-18 stsp if (error)
1636 bb64b798 2020-03-18 stsp goto done;
1637 bb64b798 2020-03-18 stsp
1638 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1639 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1640 2751fe64 2020-09-24 stsp if (error)
1641 2751fe64 2020-09-24 stsp goto done;
1642 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
1643 0ae84acc 2022-06-15 tracey if (error != NULL)
1644 0ae84acc 2022-06-15 tracey goto done;
1645 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1646 2751fe64 2020-09-24 stsp if (error)
1647 2751fe64 2020-09-24 stsp goto done;
1648 2751fe64 2020-09-24 stsp }
1649 2751fe64 2020-09-24 stsp
1650 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1651 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1652 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1653 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1654 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1655 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1656 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1657 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1658 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1659 99495ddb 2021-01-10 stsp fpa.config_info.wanted_refs = &wanted_refs;
1660 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1661 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1662 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1663 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1664 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1665 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1666 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1667 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1668 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1669 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1670 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1671 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1672 39c64a6a 2020-03-18 stsp if (error)
1673 d9b4d0c0 2020-03-18 stsp goto done;
1674 d9b4d0c0 2020-03-18 stsp
1675 41b0de12 2020-03-21 stsp if (list_refs_only) {
1676 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1677 41b0de12 2020-03-21 stsp goto done;
1678 41b0de12 2020-03-21 stsp }
1679 41b0de12 2020-03-21 stsp
1680 8a4f8535 2021-12-27 stsp if (pack_hash == NULL) {
1681 8a4f8535 2021-12-27 stsp error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1682 8a4f8535 2021-12-27 stsp "server sent an empty pack file");
1683 8a4f8535 2021-12-27 stsp goto done;
1684 8a4f8535 2021-12-27 stsp }
1685 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1686 39c64a6a 2020-03-18 stsp if (error)
1687 d9b4d0c0 2020-03-18 stsp goto done;
1688 68999b92 2020-03-18 stsp if (verbosity >= 0)
1689 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1690 d9b4d0c0 2020-03-18 stsp free(id_str);
1691 d9b4d0c0 2020-03-18 stsp
1692 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1693 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1694 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1695 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1696 7ebc0570 2020-03-18 stsp char *remote_refname;
1697 0e4002ca 2020-03-21 stsp
1698 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1699 0e4002ca 2020-03-21 stsp !mirror_references) {
1700 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1701 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1702 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1703 0e4002ca 2020-03-21 stsp if (error)
1704 0e4002ca 2020-03-21 stsp goto done;
1705 0e4002ca 2020-03-21 stsp continue;
1706 0e4002ca 2020-03-21 stsp }
1707 668a20f6 2020-03-18 stsp
1708 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1709 39c64a6a 2020-03-18 stsp if (error)
1710 d9b4d0c0 2020-03-18 stsp goto done;
1711 d9b4d0c0 2020-03-18 stsp
1712 469dd726 2020-03-20 stsp if (mirror_references)
1713 469dd726 2020-03-20 stsp continue;
1714 469dd726 2020-03-20 stsp
1715 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1716 7ebc0570 2020-03-18 stsp continue;
1717 7ebc0570 2020-03-18 stsp
1718 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1719 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1720 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1721 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1722 7ebc0570 2020-03-18 stsp goto done;
1723 7ebc0570 2020-03-18 stsp }
1724 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1725 f298ae0f 2020-03-25 stsp free(remote_refname);
1726 39c64a6a 2020-03-18 stsp if (error)
1727 d9b4d0c0 2020-03-18 stsp goto done;
1728 d9b4d0c0 2020-03-18 stsp }
1729 d9b4d0c0 2020-03-18 stsp
1730 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1731 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1732 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1733 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1734 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1735 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1736 d9b4d0c0 2020-03-18 stsp
1737 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1738 d9b4d0c0 2020-03-18 stsp continue;
1739 d9b4d0c0 2020-03-18 stsp
1740 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1741 39c64a6a 2020-03-18 stsp if (error) {
1742 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1743 55330abe 2020-03-20 stsp error = NULL;
1744 d9b4d0c0 2020-03-18 stsp continue;
1745 55330abe 2020-03-20 stsp }
1746 d9b4d0c0 2020-03-18 stsp goto done;
1747 d9b4d0c0 2020-03-18 stsp }
1748 d9b4d0c0 2020-03-18 stsp
1749 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1750 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1751 f298ae0f 2020-03-25 stsp if (error)
1752 f298ae0f 2020-03-25 stsp goto done;
1753 f298ae0f 2020-03-25 stsp
1754 f298ae0f 2020-03-25 stsp if (mirror_references)
1755 f298ae0f 2020-03-25 stsp continue;
1756 f298ae0f 2020-03-25 stsp
1757 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1758 f298ae0f 2020-03-25 stsp continue;
1759 f298ae0f 2020-03-25 stsp
1760 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1761 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1762 f298ae0f 2020-03-25 stsp refname) == -1) {
1763 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1764 f298ae0f 2020-03-25 stsp goto done;
1765 f298ae0f 2020-03-25 stsp }
1766 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1767 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1768 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1769 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1770 f298ae0f 2020-03-25 stsp free(remote_refname);
1771 f298ae0f 2020-03-25 stsp goto done;
1772 f298ae0f 2020-03-25 stsp }
1773 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1774 f298ae0f 2020-03-25 stsp if (error) {
1775 f298ae0f 2020-03-25 stsp free(remote_refname);
1776 f298ae0f 2020-03-25 stsp free(remote_target);
1777 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1778 f298ae0f 2020-03-25 stsp error = NULL;
1779 f298ae0f 2020-03-25 stsp continue;
1780 f298ae0f 2020-03-25 stsp }
1781 f298ae0f 2020-03-25 stsp goto done;
1782 f298ae0f 2020-03-25 stsp }
1783 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1784 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1785 f298ae0f 2020-03-25 stsp free(remote_refname);
1786 f298ae0f 2020-03-25 stsp free(remote_target);
1787 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1788 39c64a6a 2020-03-18 stsp if (error)
1789 d9b4d0c0 2020-03-18 stsp goto done;
1790 4ba14133 2020-03-20 stsp }
1791 4ba14133 2020-03-20 stsp if (pe == NULL) {
1792 4ba14133 2020-03-20 stsp /*
1793 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1794 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1795 4ba14133 2020-03-20 stsp * which could be fetched instead.
1796 4ba14133 2020-03-20 stsp */
1797 62d463ca 2020-10-20 naddy TAILQ_FOREACH(pe, &wanted_branches, entry) {
1798 4ba14133 2020-03-20 stsp const char *target = pe->path;
1799 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1800 d9b4d0c0 2020-03-18 stsp
1801 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1802 4ba14133 2020-03-20 stsp if (error) {
1803 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1804 4ba14133 2020-03-20 stsp error = NULL;
1805 4ba14133 2020-03-20 stsp continue;
1806 4ba14133 2020-03-20 stsp }
1807 4ba14133 2020-03-20 stsp goto done;
1808 4ba14133 2020-03-20 stsp }
1809 4ba14133 2020-03-20 stsp
1810 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1811 04d9a9ec 2020-09-24 stsp verbosity, repo);
1812 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1813 4ba14133 2020-03-20 stsp if (error)
1814 4ba14133 2020-03-20 stsp goto done;
1815 4ba14133 2020-03-20 stsp break;
1816 4ba14133 2020-03-20 stsp }
1817 659e7fbd 2020-03-20 stsp }
1818 659e7fbd 2020-03-20 stsp
1819 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1820 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1821 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1822 09838ffc 2020-03-18 stsp done:
1823 0ae84acc 2022-06-15 tracey if (pack_fds) {
1824 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
1825 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
1826 0ae84acc 2022-06-15 tracey if (error == NULL)
1827 0ae84acc 2022-06-15 tracey error = pack_err;
1828 0ae84acc 2022-06-15 tracey }
1829 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1830 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1831 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1832 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1833 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1834 9c52365f 2020-03-21 stsp }
1835 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1836 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1837 1d0f4054 2021-06-17 stsp if (repo) {
1838 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
1839 1d0f4054 2021-06-17 stsp if (error == NULL)
1840 1d0f4054 2021-06-17 stsp error = close_err;
1841 1d0f4054 2021-06-17 stsp }
1842 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1843 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1844 d9b4d0c0 2020-03-18 stsp free(pe->data);
1845 d9b4d0c0 2020-03-18 stsp }
1846 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1847 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1848 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1849 d9b4d0c0 2020-03-18 stsp free(pe->data);
1850 d9b4d0c0 2020-03-18 stsp }
1851 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1852 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1853 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1854 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1855 09838ffc 2020-03-18 stsp free(proto);
1856 09838ffc 2020-03-18 stsp free(host);
1857 09838ffc 2020-03-18 stsp free(port);
1858 09838ffc 2020-03-18 stsp free(server_path);
1859 09838ffc 2020-03-18 stsp free(repo_name);
1860 bb64b798 2020-03-18 stsp free(default_destdir);
1861 b46f3e71 2020-03-18 stsp free(git_url);
1862 39c64a6a 2020-03-18 stsp return error;
1863 7848a0e1 2020-03-19 stsp }
1864 7848a0e1 2020-03-19 stsp
1865 7848a0e1 2020-03-19 stsp static const struct got_error *
1866 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1867 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1868 7848a0e1 2020-03-19 stsp {
1869 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1870 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1871 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1872 7848a0e1 2020-03-19 stsp
1873 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1874 7848a0e1 2020-03-19 stsp if (err)
1875 7848a0e1 2020-03-19 stsp goto done;
1876 7848a0e1 2020-03-19 stsp
1877 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1878 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1879 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1880 88609724 2020-03-21 stsp if (err)
1881 88609724 2020-03-21 stsp goto done;
1882 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1883 88609724 2020-03-21 stsp goto done;
1884 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1885 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1886 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1887 db6d8ad8 2020-03-21 stsp }
1888 db6d8ad8 2020-03-21 stsp goto done;
1889 db6d8ad8 2020-03-21 stsp }
1890 db6d8ad8 2020-03-21 stsp
1891 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1892 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1893 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1894 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1895 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1896 688f11b3 2020-03-21 stsp }
1897 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1898 7848a0e1 2020-03-19 stsp if (err)
1899 7848a0e1 2020-03-19 stsp goto done;
1900 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1901 e8a967e0 2020-03-21 stsp if (err)
1902 e8a967e0 2020-03-21 stsp goto done;
1903 7848a0e1 2020-03-19 stsp } else {
1904 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1905 7848a0e1 2020-03-19 stsp if (err)
1906 7848a0e1 2020-03-19 stsp goto done;
1907 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1908 6338a6a1 2020-03-21 stsp goto done;
1909 6338a6a1 2020-03-21 stsp
1910 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1911 6338a6a1 2020-03-21 stsp if (err)
1912 6338a6a1 2020-03-21 stsp goto done;
1913 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1914 6338a6a1 2020-03-21 stsp if (err)
1915 6338a6a1 2020-03-21 stsp goto done;
1916 7848a0e1 2020-03-19 stsp }
1917 6338a6a1 2020-03-21 stsp
1918 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1919 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1920 6338a6a1 2020-03-21 stsp new_id_str);
1921 7848a0e1 2020-03-19 stsp done:
1922 7848a0e1 2020-03-19 stsp free(old_id);
1923 7848a0e1 2020-03-19 stsp free(new_id_str);
1924 7848a0e1 2020-03-19 stsp return err;
1925 2ab43947 2020-03-18 stsp }
1926 f1bcca34 2020-03-25 stsp
1927 f1bcca34 2020-03-25 stsp static const struct got_error *
1928 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1929 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1930 f1bcca34 2020-03-25 stsp {
1931 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1932 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1933 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1934 f1bcca34 2020-03-25 stsp
1935 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1936 bcf34b0e 2020-03-26 stsp if (err) {
1937 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1938 bcf34b0e 2020-03-26 stsp return err;
1939 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1940 bcf34b0e 2020-03-26 stsp if (err)
1941 bcf34b0e 2020-03-26 stsp goto done;
1942 2ab43947 2020-03-18 stsp
1943 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1944 bcf34b0e 2020-03-26 stsp if (err)
1945 bcf34b0e 2020-03-26 stsp goto done;
1946 f1bcca34 2020-03-25 stsp
1947 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1948 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1949 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1950 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1951 bcf34b0e 2020-03-26 stsp } else {
1952 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1953 f1bcca34 2020-03-25 stsp
1954 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1955 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1956 bcf34b0e 2020-03-26 stsp goto done;
1957 bcf34b0e 2020-03-26 stsp
1958 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1959 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1960 bcf34b0e 2020-03-26 stsp if (err)
1961 bcf34b0e 2020-03-26 stsp goto done;
1962 bcf34b0e 2020-03-26 stsp
1963 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1964 bcf34b0e 2020-03-26 stsp if (err)
1965 bcf34b0e 2020-03-26 stsp goto done;
1966 bcf34b0e 2020-03-26 stsp
1967 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1968 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1969 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1970 bcf34b0e 2020-03-26 stsp
1971 bcf34b0e 2020-03-26 stsp }
1972 f1bcca34 2020-03-25 stsp done:
1973 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1974 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1975 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1976 bcf34b0e 2020-03-26 stsp err = unlock_err;
1977 bcf34b0e 2020-03-26 stsp }
1978 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1979 f1bcca34 2020-03-25 stsp return err;
1980 f1bcca34 2020-03-25 stsp }
1981 f1bcca34 2020-03-25 stsp
1982 2ab43947 2020-03-18 stsp __dead static void
1983 7848a0e1 2020-03-19 stsp usage_fetch(void)
1984 7848a0e1 2020-03-19 stsp {
1985 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1986 161728eb 2021-07-24 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1987 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1988 13f12b09 2020-03-21 stsp getprogname());
1989 7848a0e1 2020-03-19 stsp exit(1);
1990 7848a0e1 2020-03-19 stsp }
1991 7848a0e1 2020-03-19 stsp
1992 7848a0e1 2020-03-19 stsp static const struct got_error *
1993 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1994 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1995 f21ec2f0 2020-03-21 stsp {
1996 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1997 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1998 3789fd73 2020-03-26 stsp char *id_str = NULL;
1999 3789fd73 2020-03-26 stsp
2000 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
2001 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
2002 3789fd73 2020-03-26 stsp if (err)
2003 3789fd73 2020-03-26 stsp return err;
2004 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
2005 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
2006 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
2007 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
2008 3789fd73 2020-03-26 stsp }
2009 3789fd73 2020-03-26 stsp } else {
2010 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
2011 3789fd73 2020-03-26 stsp if (err)
2012 3789fd73 2020-03-26 stsp return err;
2013 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
2014 3789fd73 2020-03-26 stsp if (err)
2015 3789fd73 2020-03-26 stsp goto done;
2016 3168e5da 2020-09-10 stsp
2017 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
2018 3789fd73 2020-03-26 stsp if (err)
2019 3789fd73 2020-03-26 stsp goto done;
2020 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
2021 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
2022 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
2023 3789fd73 2020-03-26 stsp }
2024 3789fd73 2020-03-26 stsp }
2025 3789fd73 2020-03-26 stsp done:
2026 3789fd73 2020-03-26 stsp free(id);
2027 3789fd73 2020-03-26 stsp free(id_str);
2028 3789fd73 2020-03-26 stsp return NULL;
2029 3789fd73 2020-03-26 stsp }
2030 3789fd73 2020-03-26 stsp
2031 3789fd73 2020-03-26 stsp static const struct got_error *
2032 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
2033 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
2034 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
2035 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
2036 3789fd73 2020-03-26 stsp {
2037 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
2038 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
2039 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
2040 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
2041 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
2042 3789fd73 2020-03-26 stsp char *local_refname = NULL;
2043 f21ec2f0 2020-03-21 stsp
2044 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&my_refs);
2045 f21ec2f0 2020-03-21 stsp
2046 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2047 3789fd73 2020-03-26 stsp == -1)
2048 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
2049 3789fd73 2020-03-26 stsp
2050 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2051 f21ec2f0 2020-03-21 stsp if (err)
2052 3789fd73 2020-03-26 stsp goto done;
2053 f21ec2f0 2020-03-21 stsp
2054 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &my_refs, entry) {
2055 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
2056 1b796c3f 2021-09-11 stsp const char *their_refname;
2057 f21ec2f0 2020-03-21 stsp
2058 1b796c3f 2021-09-11 stsp if (remote->mirror_references) {
2059 1b796c3f 2021-09-11 stsp their_refname = refname;
2060 1b796c3f 2021-09-11 stsp } else {
2061 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
2062 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
2063 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
2064 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
2065 3789fd73 2020-03-26 stsp continue;
2066 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
2067 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
2068 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
2069 3789fd73 2020-03-26 stsp goto done;
2070 3789fd73 2020-03-26 stsp }
2071 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
2072 3789fd73 2020-03-26 stsp continue;
2073 f21ec2f0 2020-03-21 stsp
2074 1b796c3f 2021-09-11 stsp their_refname = local_refname;
2075 1b796c3f 2021-09-11 stsp }
2076 1b796c3f 2021-09-11 stsp
2077 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
2078 1b796c3f 2021-09-11 stsp if (strcmp(their_refname, pe->path) == 0)
2079 f21ec2f0 2020-03-21 stsp break;
2080 f21ec2f0 2020-03-21 stsp }
2081 f21ec2f0 2020-03-21 stsp if (pe != NULL)
2082 f21ec2f0 2020-03-21 stsp continue;
2083 f21ec2f0 2020-03-21 stsp
2084 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
2085 1b796c3f 2021-09-11 stsp if (strcmp(their_refname, pe->path) == 0)
2086 3789fd73 2020-03-26 stsp break;
2087 3789fd73 2020-03-26 stsp }
2088 3789fd73 2020-03-26 stsp if (pe != NULL)
2089 3789fd73 2020-03-26 stsp continue;
2090 f21ec2f0 2020-03-21 stsp
2091 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
2092 f21ec2f0 2020-03-21 stsp if (err)
2093 f21ec2f0 2020-03-21 stsp break;
2094 3789fd73 2020-03-26 stsp
2095 3789fd73 2020-03-26 stsp if (local_refname) {
2096 3789fd73 2020-03-26 stsp struct got_reference *ref;
2097 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
2098 3789fd73 2020-03-26 stsp if (err) {
2099 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
2100 3789fd73 2020-03-26 stsp break;
2101 3789fd73 2020-03-26 stsp free(local_refname);
2102 3789fd73 2020-03-26 stsp local_refname = NULL;
2103 3789fd73 2020-03-26 stsp continue;
2104 3789fd73 2020-03-26 stsp }
2105 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
2106 3789fd73 2020-03-26 stsp if (err)
2107 3789fd73 2020-03-26 stsp break;
2108 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
2109 3789fd73 2020-03-26 stsp got_ref_close(ref);
2110 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
2111 3789fd73 2020-03-26 stsp err = unlock_err;
2112 3789fd73 2020-03-26 stsp break;
2113 3789fd73 2020-03-26 stsp }
2114 3789fd73 2020-03-26 stsp
2115 3789fd73 2020-03-26 stsp free(local_refname);
2116 3789fd73 2020-03-26 stsp local_refname = NULL;
2117 6338a6a1 2020-03-21 stsp }
2118 f21ec2f0 2020-03-21 stsp }
2119 3789fd73 2020-03-26 stsp done:
2120 3789fd73 2020-03-26 stsp free(remote_namespace);
2121 3789fd73 2020-03-26 stsp free(local_refname);
2122 0e4002ca 2020-03-21 stsp return err;
2123 0e4002ca 2020-03-21 stsp }
2124 0e4002ca 2020-03-21 stsp
2125 0e4002ca 2020-03-21 stsp static const struct got_error *
2126 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
2127 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
2128 0e4002ca 2020-03-21 stsp {
2129 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
2130 0e4002ca 2020-03-21 stsp char *remote_refname;
2131 0e4002ca 2020-03-21 stsp struct got_reference *ref;
2132 0e4002ca 2020-03-21 stsp
2133 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
2134 0e4002ca 2020-03-21 stsp refname += 5;
2135 0e4002ca 2020-03-21 stsp
2136 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2137 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
2138 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
2139 f21ec2f0 2020-03-21 stsp
2140 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
2141 0e4002ca 2020-03-21 stsp if (err) {
2142 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
2143 0e4002ca 2020-03-21 stsp goto done;
2144 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
2145 0e4002ca 2020-03-21 stsp } else {
2146 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
2147 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2148 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
2149 9f142382 2020-03-21 stsp err = unlock_err;
2150 0e4002ca 2020-03-21 stsp got_ref_close(ref);
2151 0e4002ca 2020-03-21 stsp }
2152 0e4002ca 2020-03-21 stsp done:
2153 0e4002ca 2020-03-21 stsp free(remote_refname);
2154 161728eb 2021-07-24 stsp return err;
2155 161728eb 2021-07-24 stsp }
2156 161728eb 2021-07-24 stsp
2157 161728eb 2021-07-24 stsp static const struct got_error *
2158 161728eb 2021-07-24 stsp delete_ref(struct got_repository *repo, struct got_reference *ref)
2159 161728eb 2021-07-24 stsp {
2160 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2161 161728eb 2021-07-24 stsp struct got_object_id *id = NULL;
2162 161728eb 2021-07-24 stsp char *id_str = NULL;
2163 161728eb 2021-07-24 stsp const char *target;
2164 161728eb 2021-07-24 stsp
2165 161728eb 2021-07-24 stsp if (got_ref_is_symbolic(ref)) {
2166 161728eb 2021-07-24 stsp target = got_ref_get_symref_target(ref);
2167 161728eb 2021-07-24 stsp } else {
2168 161728eb 2021-07-24 stsp err = got_ref_resolve(&id, repo, ref);
2169 161728eb 2021-07-24 stsp if (err)
2170 161728eb 2021-07-24 stsp goto done;
2171 161728eb 2021-07-24 stsp err = got_object_id_str(&id_str, id);
2172 161728eb 2021-07-24 stsp if (err)
2173 161728eb 2021-07-24 stsp goto done;
2174 161728eb 2021-07-24 stsp target = id_str;
2175 161728eb 2021-07-24 stsp }
2176 161728eb 2021-07-24 stsp
2177 161728eb 2021-07-24 stsp err = got_ref_delete(ref, repo);
2178 161728eb 2021-07-24 stsp if (err)
2179 161728eb 2021-07-24 stsp goto done;
2180 161728eb 2021-07-24 stsp
2181 161728eb 2021-07-24 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2182 161728eb 2021-07-24 stsp done:
2183 161728eb 2021-07-24 stsp free(id);
2184 161728eb 2021-07-24 stsp free(id_str);
2185 f21ec2f0 2020-03-21 stsp return err;
2186 f21ec2f0 2020-03-21 stsp }
2187 f21ec2f0 2020-03-21 stsp
2188 f21ec2f0 2020-03-21 stsp static const struct got_error *
2189 161728eb 2021-07-24 stsp delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2190 161728eb 2021-07-24 stsp {
2191 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2192 161728eb 2021-07-24 stsp struct got_reflist_head refs;
2193 161728eb 2021-07-24 stsp struct got_reflist_entry *re;
2194 161728eb 2021-07-24 stsp char *prefix;
2195 161728eb 2021-07-24 stsp
2196 161728eb 2021-07-24 stsp TAILQ_INIT(&refs);
2197 161728eb 2021-07-24 stsp
2198 161728eb 2021-07-24 stsp if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2199 161728eb 2021-07-24 stsp err = got_error_from_errno("asprintf");
2200 161728eb 2021-07-24 stsp goto done;
2201 161728eb 2021-07-24 stsp }
2202 161728eb 2021-07-24 stsp err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2203 161728eb 2021-07-24 stsp if (err)
2204 161728eb 2021-07-24 stsp goto done;
2205 161728eb 2021-07-24 stsp
2206 161728eb 2021-07-24 stsp TAILQ_FOREACH(re, &refs, entry)
2207 161728eb 2021-07-24 stsp delete_ref(repo, re->ref);
2208 161728eb 2021-07-24 stsp done:
2209 161728eb 2021-07-24 stsp got_ref_list_free(&refs);
2210 161728eb 2021-07-24 stsp return err;
2211 161728eb 2021-07-24 stsp }
2212 161728eb 2021-07-24 stsp
2213 161728eb 2021-07-24 stsp static const struct got_error *
2214 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
2215 7848a0e1 2020-03-19 stsp {
2216 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
2217 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
2218 7848a0e1 2020-03-19 stsp const char *remote_name;
2219 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
2220 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
2221 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
2222 7848a0e1 2020-03-19 stsp int nremotes;
2223 7848a0e1 2020-03-19 stsp char *id_str = NULL;
2224 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
2225 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
2226 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2227 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2228 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
2229 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
2230 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
2231 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
2232 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
2233 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2234 161728eb 2021-07-24 stsp int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2235 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
2236 7848a0e1 2020-03-19 stsp
2237 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
2238 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
2239 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
2240 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
2241 7848a0e1 2020-03-19 stsp
2242 161728eb 2021-07-24 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2243 7848a0e1 2020-03-19 stsp switch (ch) {
2244 659e7fbd 2020-03-20 stsp case 'a':
2245 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
2246 4ba14133 2020-03-20 stsp break;
2247 4ba14133 2020-03-20 stsp case 'b':
2248 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
2249 4ba14133 2020-03-20 stsp optarg, NULL);
2250 4ba14133 2020-03-20 stsp if (error)
2251 4ba14133 2020-03-20 stsp return error;
2252 41b0de12 2020-03-21 stsp break;
2253 f21ec2f0 2020-03-21 stsp case 'd':
2254 f21ec2f0 2020-03-21 stsp delete_refs = 1;
2255 f21ec2f0 2020-03-21 stsp break;
2256 41b0de12 2020-03-21 stsp case 'l':
2257 41b0de12 2020-03-21 stsp list_refs_only = 1;
2258 659e7fbd 2020-03-20 stsp break;
2259 7848a0e1 2020-03-19 stsp case 'r':
2260 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
2261 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2262 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
2263 7848a0e1 2020-03-19 stsp optarg);
2264 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
2265 7848a0e1 2020-03-19 stsp break;
2266 db6d8ad8 2020-03-21 stsp case 't':
2267 db6d8ad8 2020-03-21 stsp replace_tags = 1;
2268 db6d8ad8 2020-03-21 stsp break;
2269 7848a0e1 2020-03-19 stsp case 'v':
2270 7848a0e1 2020-03-19 stsp if (verbosity < 0)
2271 7848a0e1 2020-03-19 stsp verbosity = 0;
2272 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
2273 7848a0e1 2020-03-19 stsp verbosity++;
2274 7848a0e1 2020-03-19 stsp break;
2275 7848a0e1 2020-03-19 stsp case 'q':
2276 7848a0e1 2020-03-19 stsp verbosity = -1;
2277 7848a0e1 2020-03-19 stsp break;
2278 0e4002ca 2020-03-21 stsp case 'R':
2279 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
2280 0e4002ca 2020-03-21 stsp optarg, NULL);
2281 0e4002ca 2020-03-21 stsp if (error)
2282 0e4002ca 2020-03-21 stsp return error;
2283 0e4002ca 2020-03-21 stsp break;
2284 161728eb 2021-07-24 stsp case 'X':
2285 161728eb 2021-07-24 stsp delete_remote = 1;
2286 161728eb 2021-07-24 stsp break;
2287 7848a0e1 2020-03-19 stsp default:
2288 7848a0e1 2020-03-19 stsp usage_fetch();
2289 7848a0e1 2020-03-19 stsp break;
2290 7848a0e1 2020-03-19 stsp }
2291 7848a0e1 2020-03-19 stsp }
2292 7848a0e1 2020-03-19 stsp argc -= optind;
2293 7848a0e1 2020-03-19 stsp argv += optind;
2294 7848a0e1 2020-03-19 stsp
2295 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2296 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
2297 41b0de12 2020-03-21 stsp if (list_refs_only) {
2298 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2299 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
2300 ff69268e 2020-12-13 stsp if (fetch_all_branches)
2301 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
2302 f21ec2f0 2020-03-21 stsp if (delete_refs)
2303 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
2304 161728eb 2021-07-24 stsp if (delete_remote)
2305 161728eb 2021-07-24 stsp option_conflict('l', 'X');
2306 41b0de12 2020-03-21 stsp }
2307 161728eb 2021-07-24 stsp if (delete_remote) {
2308 161728eb 2021-07-24 stsp if (fetch_all_branches)
2309 161728eb 2021-07-24 stsp option_conflict('X', 'a');
2310 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_branches))
2311 161728eb 2021-07-24 stsp option_conflict('X', 'b');
2312 161728eb 2021-07-24 stsp if (delete_refs)
2313 161728eb 2021-07-24 stsp option_conflict('X', 'd');
2314 161728eb 2021-07-24 stsp if (replace_tags)
2315 161728eb 2021-07-24 stsp option_conflict('X', 't');
2316 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_refs))
2317 161728eb 2021-07-24 stsp option_conflict('X', 'R');
2318 161728eb 2021-07-24 stsp }
2319 161728eb 2021-07-24 stsp
2320 161728eb 2021-07-24 stsp if (argc == 0) {
2321 161728eb 2021-07-24 stsp if (delete_remote)
2322 161728eb 2021-07-24 stsp errx(1, "-X option requires a remote name");
2323 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2324 161728eb 2021-07-24 stsp } else if (argc == 1)
2325 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2326 7848a0e1 2020-03-19 stsp else
2327 7848a0e1 2020-03-19 stsp usage_fetch();
2328 7848a0e1 2020-03-19 stsp
2329 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2330 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2331 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2332 7848a0e1 2020-03-19 stsp goto done;
2333 7848a0e1 2020-03-19 stsp }
2334 7848a0e1 2020-03-19 stsp
2335 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
2336 0ae84acc 2022-06-15 tracey if (error != NULL)
2337 0ae84acc 2022-06-15 tracey goto done;
2338 0ae84acc 2022-06-15 tracey
2339 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2340 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2341 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2342 7848a0e1 2020-03-19 stsp goto done;
2343 7848a0e1 2020-03-19 stsp else
2344 7848a0e1 2020-03-19 stsp error = NULL;
2345 7848a0e1 2020-03-19 stsp if (worktree) {
2346 7848a0e1 2020-03-19 stsp repo_path =
2347 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2348 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2349 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2350 7848a0e1 2020-03-19 stsp if (error)
2351 7848a0e1 2020-03-19 stsp goto done;
2352 7848a0e1 2020-03-19 stsp } else {
2353 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2354 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2355 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2356 7848a0e1 2020-03-19 stsp goto done;
2357 7848a0e1 2020-03-19 stsp }
2358 7848a0e1 2020-03-19 stsp }
2359 7848a0e1 2020-03-19 stsp }
2360 7848a0e1 2020-03-19 stsp
2361 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2362 7848a0e1 2020-03-19 stsp if (error)
2363 7848a0e1 2020-03-19 stsp goto done;
2364 7848a0e1 2020-03-19 stsp
2365 161728eb 2021-07-24 stsp if (delete_remote) {
2366 161728eb 2021-07-24 stsp error = delete_refs_for_remote(repo, remote_name);
2367 161728eb 2021-07-24 stsp goto done; /* nothing else to do */
2368 161728eb 2021-07-24 stsp }
2369 161728eb 2021-07-24 stsp
2370 50b0790e 2020-09-11 stsp if (worktree) {
2371 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2372 50b0790e 2020-09-11 stsp if (worktree_conf) {
2373 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2374 50b0790e 2020-09-11 stsp worktree_conf);
2375 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2376 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2377 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2378 50b0790e 2020-09-11 stsp break;
2379 54eb00d5 2020-10-20 stsp }
2380 50b0790e 2020-09-11 stsp }
2381 50b0790e 2020-09-11 stsp }
2382 7848a0e1 2020-03-19 stsp }
2383 50b0790e 2020-09-11 stsp if (remote == NULL) {
2384 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2385 50b0790e 2020-09-11 stsp if (repo_conf) {
2386 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2387 50b0790e 2020-09-11 stsp repo_conf);
2388 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2389 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2390 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2391 50b0790e 2020-09-11 stsp break;
2392 54eb00d5 2020-10-20 stsp }
2393 50b0790e 2020-09-11 stsp }
2394 50b0790e 2020-09-11 stsp }
2395 50b0790e 2020-09-11 stsp }
2396 50b0790e 2020-09-11 stsp if (remote == NULL) {
2397 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2398 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2399 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2400 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2401 257add31 2020-09-09 stsp break;
2402 54eb00d5 2020-10-20 stsp }
2403 257add31 2020-09-09 stsp }
2404 7848a0e1 2020-03-19 stsp }
2405 50b0790e 2020-09-11 stsp if (remote == NULL) {
2406 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2407 50b0790e 2020-09-11 stsp goto done;
2408 b8adfa55 2020-09-25 stsp }
2409 b8adfa55 2020-09-25 stsp
2410 0c8b29c5 2021-01-05 stsp if (TAILQ_EMPTY(&wanted_branches)) {
2411 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches)
2412 0c8b29c5 2021-01-05 stsp fetch_all_branches = remote->fetch_all_branches;
2413 6480c871 2021-08-30 stsp for (i = 0; i < remote->nfetch_branches; i++) {
2414 b8adfa55 2020-09-25 stsp got_pathlist_append(&wanted_branches,
2415 6480c871 2021-08-30 stsp remote->fetch_branches[i], NULL);
2416 99495ddb 2021-01-10 stsp }
2417 99495ddb 2021-01-10 stsp }
2418 99495ddb 2021-01-10 stsp if (TAILQ_EMPTY(&wanted_refs)) {
2419 6480c871 2021-08-30 stsp for (i = 0; i < remote->nfetch_refs; i++) {
2420 99495ddb 2021-01-10 stsp got_pathlist_append(&wanted_refs,
2421 6480c871 2021-08-30 stsp remote->fetch_refs[i], NULL);
2422 b8adfa55 2020-09-25 stsp }
2423 50b0790e 2020-09-11 stsp }
2424 7848a0e1 2020-03-19 stsp
2425 5e5da8c4 2021-09-05 stsp error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2426 6480c871 2021-08-30 stsp &repo_name, remote->fetch_url);
2427 7848a0e1 2020-03-19 stsp if (error)
2428 7848a0e1 2020-03-19 stsp goto done;
2429 7848a0e1 2020-03-19 stsp
2430 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2431 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2432 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2433 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2434 7848a0e1 2020-03-19 stsp err(1, "pledge");
2435 7848a0e1 2020-03-19 stsp #endif
2436 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2437 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2438 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2439 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2440 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2441 7848a0e1 2020-03-19 stsp err(1, "pledge");
2442 7848a0e1 2020-03-19 stsp #endif
2443 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2444 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2445 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2446 7848a0e1 2020-03-19 stsp goto done;
2447 7848a0e1 2020-03-19 stsp } else {
2448 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2449 7848a0e1 2020-03-19 stsp goto done;
2450 7848a0e1 2020-03-19 stsp }
2451 7848a0e1 2020-03-19 stsp
2452 d65a88a2 2021-09-05 stsp error = got_dial_apply_unveil(proto);
2453 d65a88a2 2021-09-05 stsp if (error)
2454 d65a88a2 2021-09-05 stsp goto done;
2455 d65a88a2 2021-09-05 stsp
2456 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2457 7848a0e1 2020-03-19 stsp if (error)
2458 7848a0e1 2020-03-19 stsp goto done;
2459 f79e6490 2020-04-19 stsp
2460 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2461 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2462 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2463 7848a0e1 2020-03-19 stsp
2464 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2465 9c52365f 2020-03-21 stsp server_path, verbosity);
2466 7848a0e1 2020-03-19 stsp if (error)
2467 7848a0e1 2020-03-19 stsp goto done;
2468 7848a0e1 2020-03-19 stsp
2469 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2470 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2471 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2472 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2473 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2474 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2475 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2476 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2477 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2478 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2479 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2480 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2481 7848a0e1 2020-03-19 stsp if (error)
2482 7848a0e1 2020-03-19 stsp goto done;
2483 7848a0e1 2020-03-19 stsp
2484 41b0de12 2020-03-21 stsp if (list_refs_only) {
2485 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2486 41b0de12 2020-03-21 stsp goto done;
2487 41b0de12 2020-03-21 stsp }
2488 41b0de12 2020-03-21 stsp
2489 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2490 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2491 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2492 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2493 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2494 984065c8 2020-03-19 stsp if (error)
2495 984065c8 2020-03-19 stsp goto done;
2496 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2497 984065c8 2020-03-19 stsp free(id_str);
2498 984065c8 2020-03-19 stsp id_str = NULL;
2499 984065c8 2020-03-19 stsp }
2500 7848a0e1 2020-03-19 stsp
2501 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2502 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2503 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2504 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2505 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2506 7848a0e1 2020-03-19 stsp char *remote_refname;
2507 7848a0e1 2020-03-19 stsp
2508 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2509 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2510 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2511 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2512 0e4002ca 2020-03-21 stsp if (error)
2513 0e4002ca 2020-03-21 stsp goto done;
2514 0e4002ca 2020-03-21 stsp continue;
2515 0e4002ca 2020-03-21 stsp }
2516 0e4002ca 2020-03-21 stsp
2517 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2518 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2519 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2520 7848a0e1 2020-03-19 stsp if (error) {
2521 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2522 7848a0e1 2020-03-19 stsp goto done;
2523 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2524 6338a6a1 2020-03-21 stsp repo);
2525 7848a0e1 2020-03-19 stsp if (error)
2526 7848a0e1 2020-03-19 stsp goto done;
2527 7848a0e1 2020-03-19 stsp } else {
2528 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2529 db6d8ad8 2020-03-21 stsp verbosity, repo);
2530 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2531 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2532 9f142382 2020-03-21 stsp error = unlock_err;
2533 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2534 7848a0e1 2020-03-19 stsp if (error)
2535 7848a0e1 2020-03-19 stsp goto done;
2536 7848a0e1 2020-03-19 stsp }
2537 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2538 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2539 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2540 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2541 7848a0e1 2020-03-19 stsp goto done;
2542 7848a0e1 2020-03-19 stsp }
2543 7848a0e1 2020-03-19 stsp
2544 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2545 7848a0e1 2020-03-19 stsp if (error) {
2546 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2547 7848a0e1 2020-03-19 stsp goto done;
2548 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2549 688f11b3 2020-03-21 stsp verbosity, repo);
2550 7848a0e1 2020-03-19 stsp if (error)
2551 7848a0e1 2020-03-19 stsp goto done;
2552 7848a0e1 2020-03-19 stsp } else {
2553 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2554 db6d8ad8 2020-03-21 stsp verbosity, repo);
2555 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2556 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2557 9f142382 2020-03-21 stsp error = unlock_err;
2558 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2559 7848a0e1 2020-03-19 stsp if (error)
2560 7848a0e1 2020-03-19 stsp goto done;
2561 7848a0e1 2020-03-19 stsp }
2562 2ec30c80 2020-03-20 stsp
2563 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2564 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2565 2ec30c80 2020-03-20 stsp if (error) {
2566 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2567 2ec30c80 2020-03-20 stsp goto done;
2568 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2569 6338a6a1 2020-03-21 stsp repo);
2570 2ec30c80 2020-03-20 stsp if (error)
2571 2ec30c80 2020-03-20 stsp goto done;
2572 9f142382 2020-03-21 stsp } else {
2573 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2574 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2575 9f142382 2020-03-21 stsp error = unlock_err;
2576 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2577 9f142382 2020-03-21 stsp }
2578 7848a0e1 2020-03-19 stsp }
2579 7848a0e1 2020-03-19 stsp }
2580 f1bcca34 2020-03-25 stsp if (delete_refs) {
2581 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2582 3789fd73 2020-03-26 stsp verbosity, repo);
2583 f1bcca34 2020-03-25 stsp if (error)
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 if (!remote->mirror_references) {
2588 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2589 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2590 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2591 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2592 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2593 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2594 f1bcca34 2020-03-25 stsp
2595 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2596 f1bcca34 2020-03-25 stsp continue;
2597 f1bcca34 2020-03-25 stsp
2598 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2599 f1bcca34 2020-03-25 stsp continue;
2600 f1bcca34 2020-03-25 stsp
2601 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2602 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2603 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2604 f1bcca34 2020-03-25 stsp goto done;
2605 f1bcca34 2020-03-25 stsp }
2606 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2607 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2608 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2609 f1bcca34 2020-03-25 stsp free(remote_refname);
2610 f1bcca34 2020-03-25 stsp goto done;
2611 f1bcca34 2020-03-25 stsp }
2612 f1bcca34 2020-03-25 stsp
2613 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2614 f1bcca34 2020-03-25 stsp 0);
2615 f1bcca34 2020-03-25 stsp if (error) {
2616 f1bcca34 2020-03-25 stsp free(remote_refname);
2617 f1bcca34 2020-03-25 stsp free(remote_target);
2618 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2619 f1bcca34 2020-03-25 stsp error = NULL;
2620 f1bcca34 2020-03-25 stsp continue;
2621 f1bcca34 2020-03-25 stsp }
2622 f1bcca34 2020-03-25 stsp goto done;
2623 f1bcca34 2020-03-25 stsp }
2624 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2625 f1bcca34 2020-03-25 stsp verbosity, repo);
2626 f1bcca34 2020-03-25 stsp free(remote_refname);
2627 f1bcca34 2020-03-25 stsp free(remote_target);
2628 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2629 f1bcca34 2020-03-25 stsp if (error)
2630 f1bcca34 2020-03-25 stsp goto done;
2631 f1bcca34 2020-03-25 stsp }
2632 f1bcca34 2020-03-25 stsp }
2633 7848a0e1 2020-03-19 stsp done:
2634 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2635 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2636 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2637 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2638 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2639 9c52365f 2020-03-21 stsp }
2640 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2641 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2642 1d0f4054 2021-06-17 stsp if (repo) {
2643 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2644 1d0f4054 2021-06-17 stsp if (error == NULL)
2645 1d0f4054 2021-06-17 stsp error = close_err;
2646 1d0f4054 2021-06-17 stsp }
2647 7848a0e1 2020-03-19 stsp if (worktree)
2648 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2649 0ae84acc 2022-06-15 tracey if (pack_fds) {
2650 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
2651 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
2652 0ae84acc 2022-06-15 tracey if (error == NULL)
2653 0ae84acc 2022-06-15 tracey error = pack_err;
2654 0ae84acc 2022-06-15 tracey }
2655 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2656 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2657 7848a0e1 2020-03-19 stsp free(pe->data);
2658 7848a0e1 2020-03-19 stsp }
2659 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2660 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2661 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2662 7848a0e1 2020-03-19 stsp free(pe->data);
2663 7848a0e1 2020-03-19 stsp }
2664 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2665 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2666 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2667 7848a0e1 2020-03-19 stsp free(id_str);
2668 7848a0e1 2020-03-19 stsp free(cwd);
2669 7848a0e1 2020-03-19 stsp free(repo_path);
2670 7848a0e1 2020-03-19 stsp free(pack_hash);
2671 7848a0e1 2020-03-19 stsp free(proto);
2672 7848a0e1 2020-03-19 stsp free(host);
2673 7848a0e1 2020-03-19 stsp free(port);
2674 7848a0e1 2020-03-19 stsp free(server_path);
2675 7848a0e1 2020-03-19 stsp free(repo_name);
2676 7848a0e1 2020-03-19 stsp return error;
2677 7848a0e1 2020-03-19 stsp }
2678 7848a0e1 2020-03-19 stsp
2679 7848a0e1 2020-03-19 stsp
2680 7848a0e1 2020-03-19 stsp __dead static void
2681 2ab43947 2020-03-18 stsp usage_checkout(void)
2682 2ab43947 2020-03-18 stsp {
2683 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2684 4ad4a1ec 2021-09-13 tracey "[-p prefix] [-q] repository-path [worktree-path]\n",
2685 4ad4a1ec 2021-09-13 tracey getprogname());
2686 2ab43947 2020-03-18 stsp exit(1);
2687 2ab43947 2020-03-18 stsp }
2688 2ab43947 2020-03-18 stsp
2689 2ab43947 2020-03-18 stsp static void
2690 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2691 2ab43947 2020-03-18 stsp {
2692 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2693 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2694 e6786710 2021-07-03 stsp "garbage-collected by Git or 'gotadmin cleanup'; making the "
2695 e6786710 2021-07-03 stsp "repository writable and running 'got update' will prevent this\n",
2696 2ab43947 2020-03-18 stsp getprogname());
2697 2ab43947 2020-03-18 stsp }
2698 2ab43947 2020-03-18 stsp
2699 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2700 2ab43947 2020-03-18 stsp const char *worktree_path;
2701 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2702 4ad4a1ec 2021-09-13 tracey int verbosity;
2703 2ab43947 2020-03-18 stsp };
2704 2ab43947 2020-03-18 stsp
2705 2ab43947 2020-03-18 stsp static const struct got_error *
2706 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2707 2ab43947 2020-03-18 stsp {
2708 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2709 2ab43947 2020-03-18 stsp
2710 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2711 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2712 2ab43947 2020-03-18 stsp return NULL;
2713 2ab43947 2020-03-18 stsp
2714 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2715 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2716 2ab43947 2020-03-18 stsp return NULL;
2717 2ab43947 2020-03-18 stsp }
2718 2ab43947 2020-03-18 stsp
2719 2ab43947 2020-03-18 stsp while (path[0] == '/')
2720 2ab43947 2020-03-18 stsp path++;
2721 2ab43947 2020-03-18 stsp
2722 4ad4a1ec 2021-09-13 tracey if (a->verbosity >= 0)
2723 4ad4a1ec 2021-09-13 tracey printf("%c %s/%s\n", status, a->worktree_path, path);
2724 4ad4a1ec 2021-09-13 tracey
2725 2ab43947 2020-03-18 stsp return NULL;
2726 2ab43947 2020-03-18 stsp }
2727 2ab43947 2020-03-18 stsp
2728 2ab43947 2020-03-18 stsp static const struct got_error *
2729 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2730 2ab43947 2020-03-18 stsp {
2731 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2732 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2733 2ab43947 2020-03-18 stsp return NULL;
2734 2ab43947 2020-03-18 stsp }
2735 2ab43947 2020-03-18 stsp
2736 2ab43947 2020-03-18 stsp static const struct got_error *
2737 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2738 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2739 2ab43947 2020-03-18 stsp struct got_repository *repo)
2740 2ab43947 2020-03-18 stsp {
2741 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2742 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2743 2ab43947 2020-03-18 stsp
2744 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2745 4e91ef15 2021-09-26 stsp commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2746 2ab43947 2020-03-18 stsp if (err)
2747 2ab43947 2020-03-18 stsp return err;
2748 2ab43947 2020-03-18 stsp
2749 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2750 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2751 2ab43947 2020-03-18 stsp
2752 2ab43947 2020-03-18 stsp /*
2753 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2754 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2755 2ab43947 2020-03-18 stsp *
2756 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2757 2ab43947 2020-03-18 stsp *
2758 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2759 2ab43947 2020-03-18 stsp * \ /
2760 2ab43947 2020-03-18 stsp * C E
2761 2ab43947 2020-03-18 stsp * \ /
2762 2ab43947 2020-03-18 stsp * B (yca)
2763 2ab43947 2020-03-18 stsp * |
2764 2ab43947 2020-03-18 stsp * A
2765 2ab43947 2020-03-18 stsp *
2766 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2767 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2768 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2769 2ab43947 2020-03-18 stsp */
2770 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2771 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2772 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2773 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2774 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2775 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2776 2ab43947 2020-03-18 stsp
2777 2ab43947 2020-03-18 stsp free(yca_id);
2778 2ab43947 2020-03-18 stsp return NULL;
2779 2ab43947 2020-03-18 stsp }
2780 2ab43947 2020-03-18 stsp
2781 2ab43947 2020-03-18 stsp static const struct got_error *
2782 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2783 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2784 2ab43947 2020-03-18 stsp struct got_repository *repo)
2785 2ab43947 2020-03-18 stsp {
2786 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2787 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2788 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2789 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2790 2ab43947 2020-03-18 stsp
2791 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2792 2ab43947 2020-03-18 stsp if (err)
2793 2ab43947 2020-03-18 stsp goto done;
2794 2ab43947 2020-03-18 stsp
2795 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2796 2ab43947 2020-03-18 stsp is_same_branch = 1;
2797 2ab43947 2020-03-18 stsp goto done;
2798 2ab43947 2020-03-18 stsp }
2799 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2800 2ab43947 2020-03-18 stsp is_same_branch = 1;
2801 2ab43947 2020-03-18 stsp goto done;
2802 2ab43947 2020-03-18 stsp }
2803 2ab43947 2020-03-18 stsp
2804 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2805 2ab43947 2020-03-18 stsp if (err)
2806 2ab43947 2020-03-18 stsp goto done;
2807 2ab43947 2020-03-18 stsp
2808 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2809 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2810 2ab43947 2020-03-18 stsp if (err)
2811 2ab43947 2020-03-18 stsp goto done;
2812 2ab43947 2020-03-18 stsp
2813 2ab43947 2020-03-18 stsp for (;;) {
2814 2ab43947 2020-03-18 stsp struct got_object_id *id;
2815 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2816 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2817 2ab43947 2020-03-18 stsp if (err) {
2818 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2819 2ab43947 2020-03-18 stsp err = NULL;
2820 2ab43947 2020-03-18 stsp break;
2821 2ab43947 2020-03-18 stsp }
2822 2ab43947 2020-03-18 stsp
2823 2ab43947 2020-03-18 stsp if (id) {
2824 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2825 2ab43947 2020-03-18 stsp break;
2826 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2827 2ab43947 2020-03-18 stsp is_same_branch = 1;
2828 2ab43947 2020-03-18 stsp break;
2829 2ab43947 2020-03-18 stsp }
2830 2ab43947 2020-03-18 stsp }
2831 2ab43947 2020-03-18 stsp }
2832 2ab43947 2020-03-18 stsp done:
2833 2ab43947 2020-03-18 stsp if (graph)
2834 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2835 2ab43947 2020-03-18 stsp free(head_commit_id);
2836 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2837 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2838 2ab43947 2020-03-18 stsp return err;
2839 a367ff0f 2019-05-14 stsp }
2840 8069f636 2019-01-12 stsp
2841 4ed7e80c 2018-05-20 stsp static const struct got_error *
2842 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2843 4b6c9460 2020-03-05 stsp {
2844 4b6c9460 2020-03-05 stsp static char msg[512];
2845 4b6c9460 2020-03-05 stsp const char *branch_name;
2846 4b6c9460 2020-03-05 stsp
2847 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2848 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2849 4b6c9460 2020-03-05 stsp else
2850 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2851 4b6c9460 2020-03-05 stsp
2852 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2853 4b6c9460 2020-03-05 stsp branch_name += 11;
2854 4b6c9460 2020-03-05 stsp
2855 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2856 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2857 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2858 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2859 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2860 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2861 4b6c9460 2020-03-05 stsp
2862 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2863 4b6c9460 2020-03-05 stsp }
2864 4b6c9460 2020-03-05 stsp
2865 4b6c9460 2020-03-05 stsp static const struct got_error *
2866 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2867 c09a553d 2018-03-12 stsp {
2868 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2869 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2870 08e5873e 2021-09-14 stsp struct got_reference *head_ref = NULL, *ref = NULL;
2871 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2872 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2873 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2874 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2875 08e5873e 2021-09-14 stsp const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2876 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2877 08e5873e 2021-09-14 stsp struct got_object_id *commit_id = NULL;
2878 f0207f6a 2020-10-19 stsp char *cwd = NULL;
2879 4ad4a1ec 2021-09-13 tracey int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2880 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2881 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2882 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
2883 f2ea84fa 2019-07-27 stsp
2884 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2885 c09a553d 2018-03-12 stsp
2886 4ad4a1ec 2021-09-13 tracey while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2887 0bb8a95e 2018-03-12 stsp switch (ch) {
2888 08573d5b 2019-05-14 stsp case 'b':
2889 08573d5b 2019-05-14 stsp branch_name = optarg;
2890 08573d5b 2019-05-14 stsp break;
2891 8069f636 2019-01-12 stsp case 'c':
2892 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2893 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2894 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2895 bb51a5b4 2020-01-13 stsp break;
2896 bb51a5b4 2020-01-13 stsp case 'E':
2897 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2898 8069f636 2019-01-12 stsp break;
2899 0bb8a95e 2018-03-12 stsp case 'p':
2900 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2901 0bb8a95e 2018-03-12 stsp break;
2902 4ad4a1ec 2021-09-13 tracey case 'q':
2903 4ad4a1ec 2021-09-13 tracey verbosity = -1;
2904 4ad4a1ec 2021-09-13 tracey break;
2905 0bb8a95e 2018-03-12 stsp default:
2906 2deda0b9 2019-03-07 stsp usage_checkout();
2907 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2908 0bb8a95e 2018-03-12 stsp }
2909 0bb8a95e 2018-03-12 stsp }
2910 0bb8a95e 2018-03-12 stsp
2911 0bb8a95e 2018-03-12 stsp argc -= optind;
2912 0bb8a95e 2018-03-12 stsp argv += optind;
2913 0bb8a95e 2018-03-12 stsp
2914 6715a751 2018-03-16 stsp #ifndef PROFILE
2915 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2916 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2917 c09a553d 2018-03-12 stsp err(1, "pledge");
2918 6715a751 2018-03-16 stsp #endif
2919 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2920 c47340da 2020-09-20 stsp char *base, *dotgit;
2921 f0207f6a 2020-10-19 stsp const char *path;
2922 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2923 76089277 2018-04-01 stsp if (repo_path == NULL)
2924 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2925 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2926 76089277 2018-04-01 stsp if (cwd == NULL) {
2927 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2928 76089277 2018-04-01 stsp goto done;
2929 76089277 2018-04-01 stsp }
2930 c47340da 2020-09-20 stsp if (path_prefix[0])
2931 f0207f6a 2020-10-19 stsp path = path_prefix;
2932 c47340da 2020-09-20 stsp else
2933 f0207f6a 2020-10-19 stsp path = repo_path;
2934 f0207f6a 2020-10-19 stsp error = got_path_basename(&base, path);
2935 f0207f6a 2020-10-19 stsp if (error)
2936 c47340da 2020-09-20 stsp goto done;
2937 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2938 c09a553d 2018-03-12 stsp if (dotgit)
2939 c09a553d 2018-03-12 stsp *dotgit = '\0';
2940 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2941 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2942 f0207f6a 2020-10-19 stsp free(base);
2943 76089277 2018-04-01 stsp goto done;
2944 c09a553d 2018-03-12 stsp }
2945 f0207f6a 2020-10-19 stsp free(base);
2946 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2947 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2948 76089277 2018-04-01 stsp if (repo_path == NULL) {
2949 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2950 76089277 2018-04-01 stsp goto done;
2951 76089277 2018-04-01 stsp }
2952 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2953 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2954 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2955 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2956 b4b3a7dd 2019-07-22 stsp argv[1]);
2957 b4b3a7dd 2019-07-22 stsp goto done;
2958 b4b3a7dd 2019-07-22 stsp }
2959 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2960 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2961 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2962 b4b3a7dd 2019-07-22 stsp goto done;
2963 b4b3a7dd 2019-07-22 stsp }
2964 76089277 2018-04-01 stsp }
2965 c09a553d 2018-03-12 stsp } else
2966 c09a553d 2018-03-12 stsp usage_checkout();
2967 c09a553d 2018-03-12 stsp
2968 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2969 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2970 13bfb272 2019-05-10 jcs
2971 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
2972 c09a553d 2018-03-12 stsp if (error != NULL)
2973 c02c541e 2019-03-29 stsp goto done;
2974 c02c541e 2019-03-29 stsp
2975 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2976 0ae84acc 2022-06-15 tracey if (error != NULL)
2977 0ae84acc 2022-06-15 tracey goto done;
2978 0ae84acc 2022-06-15 tracey
2979 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2980 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2981 c530dc23 2019-07-23 stsp if (error) {
2982 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2983 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2984 c530dc23 2019-07-23 stsp goto done;
2985 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2986 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2987 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2988 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2989 c530dc23 2019-07-23 stsp goto done;
2990 c530dc23 2019-07-23 stsp }
2991 c530dc23 2019-07-23 stsp }
2992 c530dc23 2019-07-23 stsp
2993 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2994 c02c541e 2019-03-29 stsp if (error)
2995 c09a553d 2018-03-12 stsp goto done;
2996 8069f636 2019-01-12 stsp
2997 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2998 c09a553d 2018-03-12 stsp if (error != NULL)
2999 c09a553d 2018-03-12 stsp goto done;
3000 c09a553d 2018-03-12 stsp
3001 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3002 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3003 c09a553d 2018-03-12 stsp goto done;
3004 c09a553d 2018-03-12 stsp
3005 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
3006 c09a553d 2018-03-12 stsp if (error != NULL)
3007 c09a553d 2018-03-12 stsp goto done;
3008 c09a553d 2018-03-12 stsp
3009 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3010 e5dc7198 2018-12-29 stsp path_prefix);
3011 e5dc7198 2018-12-29 stsp if (error != NULL)
3012 e5dc7198 2018-12-29 stsp goto done;
3013 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
3014 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
3015 49520a32 2018-12-29 stsp goto done;
3016 49520a32 2018-12-29 stsp }
3017 49520a32 2018-12-29 stsp
3018 8069f636 2019-01-12 stsp if (commit_id_str) {
3019 84de9106 2020-12-26 stsp struct got_reflist_head refs;
3020 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3021 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3022 84de9106 2020-12-26 stsp NULL);
3023 84de9106 2020-12-26 stsp if (error)
3024 84de9106 2020-12-26 stsp goto done;
3025 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3026 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3027 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
3028 30837e32 2019-07-25 stsp if (error)
3029 8069f636 2019-01-12 stsp goto done;
3030 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3031 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3032 8069f636 2019-01-12 stsp if (error != NULL) {
3033 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
3034 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
3035 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
3036 4b6c9460 2020-03-05 stsp }
3037 8069f636 2019-01-12 stsp goto done;
3038 8069f636 2019-01-12 stsp }
3039 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3040 4b6c9460 2020-03-05 stsp if (error) {
3041 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
3042 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
3043 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
3044 4b6c9460 2020-03-05 stsp }
3045 45d344f6 2019-05-14 stsp goto done;
3046 4b6c9460 2020-03-05 stsp }
3047 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3048 8069f636 2019-01-12 stsp commit_id);
3049 8069f636 2019-01-12 stsp if (error)
3050 8069f636 2019-01-12 stsp goto done;
3051 08e5873e 2021-09-14 stsp /* Expand potentially abbreviated commit ID string. */
3052 08e5873e 2021-09-14 stsp free(commit_id_str);
3053 08e5873e 2021-09-14 stsp error = got_object_id_str(&commit_id_str, commit_id);
3054 08e5873e 2021-09-14 stsp if (error)
3055 08e5873e 2021-09-14 stsp goto done;
3056 08e5873e 2021-09-14 stsp } else {
3057 08e5873e 2021-09-14 stsp commit_id = got_object_id_dup(
3058 08e5873e 2021-09-14 stsp got_worktree_get_base_commit_id(worktree));
3059 08e5873e 2021-09-14 stsp if (commit_id == NULL) {
3060 08e5873e 2021-09-14 stsp error = got_error_from_errno("got_object_id_dup");
3061 08e5873e 2021-09-14 stsp goto done;
3062 08e5873e 2021-09-14 stsp }
3063 08e5873e 2021-09-14 stsp error = got_object_id_str(&commit_id_str, commit_id);
3064 08e5873e 2021-09-14 stsp if (error)
3065 08e5873e 2021-09-14 stsp goto done;
3066 8069f636 2019-01-12 stsp }
3067 8069f636 2019-01-12 stsp
3068 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
3069 f2ea84fa 2019-07-27 stsp if (error)
3070 f2ea84fa 2019-07-27 stsp goto done;
3071 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
3072 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
3073 4ad4a1ec 2021-09-13 tracey cpa.verbosity = verbosity;
3074 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3075 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
3076 c09a553d 2018-03-12 stsp if (error != NULL)
3077 c09a553d 2018-03-12 stsp goto done;
3078 c09a553d 2018-03-12 stsp
3079 08e5873e 2021-09-14 stsp if (got_ref_is_symbolic(head_ref)) {
3080 08e5873e 2021-09-14 stsp error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3081 08e5873e 2021-09-14 stsp if (error)
3082 08e5873e 2021-09-14 stsp goto done;
3083 08e5873e 2021-09-14 stsp refname = got_ref_get_name(ref);
3084 08e5873e 2021-09-14 stsp } else
3085 08e5873e 2021-09-14 stsp refname = got_ref_get_name(head_ref);
3086 08e5873e 2021-09-14 stsp printf("Checked out %s: %s\n", refname, commit_id_str);
3087 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
3088 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
3089 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
3090 c09a553d 2018-03-12 stsp done:
3091 0ae84acc 2022-06-15 tracey if (pack_fds) {
3092 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3093 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3094 0ae84acc 2022-06-15 tracey if (error == NULL)
3095 0ae84acc 2022-06-15 tracey error = pack_err;
3096 0ae84acc 2022-06-15 tracey }
3097 08e5873e 2021-09-14 stsp if (head_ref)
3098 08e5873e 2021-09-14 stsp got_ref_close(head_ref);
3099 08e5873e 2021-09-14 stsp if (ref)
3100 08e5873e 2021-09-14 stsp got_ref_close(ref);
3101 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3102 8069f636 2019-01-12 stsp free(commit_id_str);
3103 08e5873e 2021-09-14 stsp free(commit_id);
3104 76089277 2018-04-01 stsp free(repo_path);
3105 507dc3bb 2018-12-29 stsp free(worktree_path);
3106 c47340da 2020-09-20 stsp free(cwd);
3107 507dc3bb 2018-12-29 stsp return error;
3108 9627c110 2020-04-18 stsp }
3109 9627c110 2020-04-18 stsp
3110 9627c110 2020-04-18 stsp struct got_update_progress_arg {
3111 9627c110 2020-04-18 stsp int did_something;
3112 9627c110 2020-04-18 stsp int conflicts;
3113 9627c110 2020-04-18 stsp int obstructed;
3114 9627c110 2020-04-18 stsp int not_updated;
3115 f259c4c1 2021-09-24 stsp int missing;
3116 35ca1db7 2021-09-28 stsp int not_deleted;
3117 35ca1db7 2021-09-28 stsp int unversioned;
3118 4ad4a1ec 2021-09-13 tracey int verbosity;
3119 9627c110 2020-04-18 stsp };
3120 9627c110 2020-04-18 stsp
3121 9627c110 2020-04-18 stsp void
3122 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
3123 9627c110 2020-04-18 stsp {
3124 9627c110 2020-04-18 stsp if (!upa->did_something)
3125 9627c110 2020-04-18 stsp return;
3126 9627c110 2020-04-18 stsp
3127 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
3128 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
3129 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
3130 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
3131 9627c110 2020-04-18 stsp upa->obstructed);
3132 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
3133 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
3134 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
3135 507dc3bb 2018-12-29 stsp }
3136 507dc3bb 2018-12-29 stsp
3137 35ca1db7 2021-09-28 stsp /*
3138 35ca1db7 2021-09-28 stsp * The meaning of some status codes differs between merge-style operations and
3139 35ca1db7 2021-09-28 stsp * update operations. For example, the ! status code means "file was missing"
3140 35ca1db7 2021-09-28 stsp * if changes were merged into the work tree, and "missing file was restored"
3141 35ca1db7 2021-09-28 stsp * if the work tree was updated. This function should be used by any operation
3142 35ca1db7 2021-09-28 stsp * which merges changes into the work tree without updating the work tree.
3143 35ca1db7 2021-09-28 stsp */
3144 35ca1db7 2021-09-28 stsp void
3145 35ca1db7 2021-09-28 stsp print_merge_progress_stats(struct got_update_progress_arg *upa)
3146 35ca1db7 2021-09-28 stsp {
3147 35ca1db7 2021-09-28 stsp if (!upa->did_something)
3148 35ca1db7 2021-09-28 stsp return;
3149 35ca1db7 2021-09-28 stsp
3150 35ca1db7 2021-09-28 stsp if (upa->conflicts > 0)
3151 35ca1db7 2021-09-28 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
3152 35ca1db7 2021-09-28 stsp if (upa->obstructed > 0)
3153 35ca1db7 2021-09-28 stsp printf("File paths obstructed by a non-regular file: %d\n",
3154 35ca1db7 2021-09-28 stsp upa->obstructed);
3155 35ca1db7 2021-09-28 stsp if (upa->missing > 0)
3156 35ca1db7 2021-09-28 stsp printf("Files which had incoming changes but could not be "
3157 35ca1db7 2021-09-28 stsp "found in the work tree: %d\n", upa->missing);
3158 35ca1db7 2021-09-28 stsp if (upa->not_deleted > 0)
3159 35ca1db7 2021-09-28 stsp printf("Files not deleted due to differences in deleted "
3160 35ca1db7 2021-09-28 stsp "content: %d\n", upa->not_deleted);
3161 35ca1db7 2021-09-28 stsp if (upa->unversioned > 0)
3162 35ca1db7 2021-09-28 stsp printf("Files not merged because an unversioned file was "
3163 35ca1db7 2021-09-28 stsp "found in the work tree: %d\n", upa->unversioned);
3164 35ca1db7 2021-09-28 stsp }
3165 35ca1db7 2021-09-28 stsp
3166 507dc3bb 2018-12-29 stsp __dead static void
3167 507dc3bb 2018-12-29 stsp usage_update(void)
3168 507dc3bb 2018-12-29 stsp {
3169 4ad4a1ec 2021-09-13 tracey fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3170 4ad4a1ec 2021-09-13 tracey "[path ...]\n",
3171 507dc3bb 2018-12-29 stsp getprogname());
3172 507dc3bb 2018-12-29 stsp exit(1);
3173 507dc3bb 2018-12-29 stsp }
3174 507dc3bb 2018-12-29 stsp
3175 1ee397ad 2019-07-12 stsp static const struct got_error *
3176 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
3177 507dc3bb 2018-12-29 stsp {
3178 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
3179 784955db 2019-01-12 stsp
3180 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
3181 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
3182 1ee397ad 2019-07-12 stsp return NULL;
3183 507dc3bb 2018-12-29 stsp
3184 9627c110 2020-04-18 stsp upa->did_something = 1;
3185 a484d721 2019-06-10 stsp
3186 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
3187 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
3188 1ee397ad 2019-07-12 stsp return NULL;
3189 a484d721 2019-06-10 stsp
3190 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
3191 9627c110 2020-04-18 stsp upa->conflicts++;
3192 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
3193 9627c110 2020-04-18 stsp upa->obstructed++;
3194 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
3195 9627c110 2020-04-18 stsp upa->not_updated++;
3196 f259c4c1 2021-09-24 stsp if (status == GOT_STATUS_MISSING)
3197 f259c4c1 2021-09-24 stsp upa->missing++;
3198 35ca1db7 2021-09-28 stsp if (status == GOT_STATUS_CANNOT_DELETE)
3199 35ca1db7 2021-09-28 stsp upa->not_deleted++;
3200 35ca1db7 2021-09-28 stsp if (status == GOT_STATUS_UNVERSIONED)
3201 35ca1db7 2021-09-28 stsp upa->unversioned++;
3202 9627c110 2020-04-18 stsp
3203 507dc3bb 2018-12-29 stsp while (path[0] == '/')
3204 507dc3bb 2018-12-29 stsp path++;
3205 4ad4a1ec 2021-09-13 tracey if (upa->verbosity >= 0)
3206 4ad4a1ec 2021-09-13 tracey printf("%c %s\n", status, path);
3207 4ad4a1ec 2021-09-13 tracey
3208 1ee397ad 2019-07-12 stsp return NULL;
3209 be7061eb 2018-12-30 stsp }
3210 be7061eb 2018-12-30 stsp
3211 be7061eb 2018-12-30 stsp static const struct got_error *
3212 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
3213 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
3214 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
3215 a1fb16d8 2019-05-24 stsp {
3216 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
3217 a1fb16d8 2019-05-24 stsp char *base_id_str;
3218 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
3219 a1fb16d8 2019-05-24 stsp
3220 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
3221 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
3222 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
3223 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
3224 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
3225 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
3226 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
3227 a1fb16d8 2019-05-24 stsp }
3228 a1fb16d8 2019-05-24 stsp
3229 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
3230 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3231 a1fb16d8 2019-05-24 stsp if (err) {
3232 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
3233 a1fb16d8 2019-05-24 stsp return err;
3234 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
3235 a1fb16d8 2019-05-24 stsp }
3236 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
3237 a1fb16d8 2019-05-24 stsp return NULL;
3238 a1fb16d8 2019-05-24 stsp
3239 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
3240 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
3241 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
3242 a1fb16d8 2019-05-24 stsp if (err)
3243 a1fb16d8 2019-05-24 stsp return err;
3244 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
3245 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3246 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
3247 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3248 0ebf8283 2019-07-24 stsp return NULL;
3249 0ebf8283 2019-07-24 stsp }
3250 0ebf8283 2019-07-24 stsp
3251 0ebf8283 2019-07-24 stsp static const struct got_error *
3252 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3253 0ebf8283 2019-07-24 stsp {
3254 0ebf8283 2019-07-24 stsp const struct got_error *err;
3255 0ebf8283 2019-07-24 stsp int in_progress;
3256 0ebf8283 2019-07-24 stsp
3257 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
3258 0ebf8283 2019-07-24 stsp if (err)
3259 0ebf8283 2019-07-24 stsp return err;
3260 0ebf8283 2019-07-24 stsp if (in_progress)
3261 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
3262 0ebf8283 2019-07-24 stsp
3263 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
3264 0ebf8283 2019-07-24 stsp if (err)
3265 0ebf8283 2019-07-24 stsp return err;
3266 0ebf8283 2019-07-24 stsp if (in_progress)
3267 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
3268 f259c4c1 2021-09-24 stsp
3269 f259c4c1 2021-09-24 stsp return NULL;
3270 f259c4c1 2021-09-24 stsp }
3271 f259c4c1 2021-09-24 stsp
3272 f259c4c1 2021-09-24 stsp static const struct got_error *
3273 f259c4c1 2021-09-24 stsp check_merge_in_progress(struct got_worktree *worktree,
3274 f259c4c1 2021-09-24 stsp struct got_repository *repo)
3275 f259c4c1 2021-09-24 stsp {
3276 f259c4c1 2021-09-24 stsp const struct got_error *err;
3277 f259c4c1 2021-09-24 stsp int in_progress;
3278 f259c4c1 2021-09-24 stsp
3279 f259c4c1 2021-09-24 stsp err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3280 f259c4c1 2021-09-24 stsp if (err)
3281 f259c4c1 2021-09-24 stsp return err;
3282 f259c4c1 2021-09-24 stsp if (in_progress)
3283 f259c4c1 2021-09-24 stsp return got_error(GOT_ERR_MERGE_BUSY);
3284 0ebf8283 2019-07-24 stsp
3285 a1fb16d8 2019-05-24 stsp return NULL;
3286 a5edda0a 2019-07-27 stsp }
3287 a5edda0a 2019-07-27 stsp
3288 a5edda0a 2019-07-27 stsp static const struct got_error *
3289 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3290 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
3291 a5edda0a 2019-07-27 stsp {
3292 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
3293 a5edda0a 2019-07-27 stsp char *path;
3294 10a623df 2021-10-11 stsp struct got_pathlist_entry *new;
3295 a5edda0a 2019-07-27 stsp int i;
3296 a5edda0a 2019-07-27 stsp
3297 a5edda0a 2019-07-27 stsp if (argc == 0) {
3298 a5edda0a 2019-07-27 stsp path = strdup("");
3299 a5edda0a 2019-07-27 stsp if (path == NULL)
3300 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
3301 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
3302 a5edda0a 2019-07-27 stsp }
3303 a5edda0a 2019-07-27 stsp
3304 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
3305 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
3306 a5edda0a 2019-07-27 stsp if (err)
3307 a5edda0a 2019-07-27 stsp break;
3308 10a623df 2021-10-11 stsp err = got_pathlist_insert(&new, paths, path, NULL);
3309 10a623df 2021-10-11 stsp if (err || new == NULL /* duplicate */) {
3310 a5edda0a 2019-07-27 stsp free(path);
3311 10a623df 2021-10-11 stsp if (err)
3312 10a623df 2021-10-11 stsp break;
3313 a5edda0a 2019-07-27 stsp }
3314 a5edda0a 2019-07-27 stsp }
3315 a5edda0a 2019-07-27 stsp
3316 a5edda0a 2019-07-27 stsp return err;
3317 a1fb16d8 2019-05-24 stsp }
3318 a1fb16d8 2019-05-24 stsp
3319 a1fb16d8 2019-05-24 stsp static const struct got_error *
3320 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
3321 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
3322 fa51e947 2020-03-27 stsp {
3323 fa51e947 2020-03-27 stsp const struct got_error *err;
3324 fa51e947 2020-03-27 stsp struct got_repository *repo;
3325 fa51e947 2020-03-27 stsp static char msg[512];
3326 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3327 0ae84acc 2022-06-15 tracey
3328 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
3329 0ae84acc 2022-06-15 tracey if (err)
3330 0ae84acc 2022-06-15 tracey return err;
3331 fa51e947 2020-03-27 stsp
3332 0ae84acc 2022-06-15 tracey err = got_repo_open(&repo, path, NULL, pack_fds);
3333 fa51e947 2020-03-27 stsp if (err)
3334 fa51e947 2020-03-27 stsp return orig_err;
3335 fa51e947 2020-03-27 stsp
3336 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
3337 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
3338 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
3339 fa51e947 2020-03-27 stsp "'got checkout'.\n"
3340 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
3341 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3342 fa51e947 2020-03-27 stsp got_repo_close(repo);
3343 0ae84acc 2022-06-15 tracey if (pack_fds) {
3344 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3345 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3346 0ae84acc 2022-06-15 tracey if (err == NULL)
3347 0ae84acc 2022-06-15 tracey err = pack_err;
3348 0ae84acc 2022-06-15 tracey }
3349 fa51e947 2020-03-27 stsp return err;
3350 fa51e947 2020-03-27 stsp }
3351 fa51e947 2020-03-27 stsp
3352 fa51e947 2020-03-27 stsp static const struct got_error *
3353 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
3354 507dc3bb 2018-12-29 stsp {
3355 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
3356 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
3357 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
3358 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
3359 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
3360 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
3361 024e9686 2019-05-14 stsp const char *branch_name = NULL;
3362 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
3363 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
3364 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
3365 4ad4a1ec 2021-09-13 tracey int ch, verbosity = 0;
3366 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
3367 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3368 507dc3bb 2018-12-29 stsp
3369 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
3370 f2ea84fa 2019-07-27 stsp
3371 4ad4a1ec 2021-09-13 tracey while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3372 507dc3bb 2018-12-29 stsp switch (ch) {
3373 024e9686 2019-05-14 stsp case 'b':
3374 024e9686 2019-05-14 stsp branch_name = optarg;
3375 024e9686 2019-05-14 stsp break;
3376 507dc3bb 2018-12-29 stsp case 'c':
3377 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
3378 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
3379 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
3380 4ad4a1ec 2021-09-13 tracey break;
3381 4ad4a1ec 2021-09-13 tracey case 'q':
3382 4ad4a1ec 2021-09-13 tracey verbosity = -1;
3383 507dc3bb 2018-12-29 stsp break;
3384 507dc3bb 2018-12-29 stsp default:
3385 2deda0b9 2019-03-07 stsp usage_update();
3386 507dc3bb 2018-12-29 stsp /* NOTREACHED */
3387 507dc3bb 2018-12-29 stsp }
3388 507dc3bb 2018-12-29 stsp }
3389 507dc3bb 2018-12-29 stsp
3390 507dc3bb 2018-12-29 stsp argc -= optind;
3391 507dc3bb 2018-12-29 stsp argv += optind;
3392 507dc3bb 2018-12-29 stsp
3393 507dc3bb 2018-12-29 stsp #ifndef PROFILE
3394 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3395 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
3396 507dc3bb 2018-12-29 stsp err(1, "pledge");
3397 507dc3bb 2018-12-29 stsp #endif
3398 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
3399 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
3400 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3401 c4cdcb68 2019-04-03 stsp goto done;
3402 c4cdcb68 2019-04-03 stsp }
3403 0ae84acc 2022-06-15 tracey
3404 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3405 0ae84acc 2022-06-15 tracey if (error != NULL)
3406 0ae84acc 2022-06-15 tracey goto done;
3407 0ae84acc 2022-06-15 tracey
3408 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
3409 fa51e947 2020-03-27 stsp if (error) {
3410 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3411 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
3412 fa51e947 2020-03-27 stsp worktree_path);
3413 7d5807f4 2019-07-11 stsp goto done;
3414 fa51e947 2020-03-27 stsp }
3415 7d5807f4 2019-07-11 stsp
3416 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
3417 f2ea84fa 2019-07-27 stsp if (error)
3418 f2ea84fa 2019-07-27 stsp goto done;
3419 507dc3bb 2018-12-29 stsp
3420 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3421 0ae84acc 2022-06-15 tracey NULL, pack_fds);
3422 507dc3bb 2018-12-29 stsp if (error != NULL)
3423 507dc3bb 2018-12-29 stsp goto done;
3424 507dc3bb 2018-12-29 stsp
3425 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3426 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3427 087fb88c 2019-08-04 stsp if (error)
3428 087fb88c 2019-08-04 stsp goto done;
3429 087fb88c 2019-08-04 stsp
3430 f259c4c1 2021-09-24 stsp error = check_merge_in_progress(worktree, repo);
3431 f259c4c1 2021-09-24 stsp if (error)
3432 f259c4c1 2021-09-24 stsp goto done;
3433 f259c4c1 2021-09-24 stsp
3434 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3435 0266afb7 2019-01-04 stsp if (error)
3436 0266afb7 2019-01-04 stsp goto done;
3437 0266afb7 2019-01-04 stsp
3438 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3439 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
3440 024e9686 2019-05-14 stsp if (error != NULL)
3441 024e9686 2019-05-14 stsp goto done;
3442 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
3443 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3444 9c4b8182 2019-01-02 stsp if (error != NULL)
3445 9c4b8182 2019-01-02 stsp goto done;
3446 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3447 507dc3bb 2018-12-29 stsp if (error != NULL)
3448 507dc3bb 2018-12-29 stsp goto done;
3449 507dc3bb 2018-12-29 stsp } else {
3450 84de9106 2020-12-26 stsp struct got_reflist_head refs;
3451 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3452 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3453 84de9106 2020-12-26 stsp NULL);
3454 84de9106 2020-12-26 stsp if (error)
3455 84de9106 2020-12-26 stsp goto done;
3456 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3457 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3458 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
3459 04f57cb3 2019-07-25 stsp free(commit_id_str);
3460 bb787f09 2019-07-25 stsp commit_id_str = NULL;
3461 30837e32 2019-07-25 stsp if (error)
3462 a297e751 2019-07-11 stsp goto done;
3463 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
3464 a297e751 2019-07-11 stsp if (error)
3465 507dc3bb 2018-12-29 stsp goto done;
3466 507dc3bb 2018-12-29 stsp }
3467 35c965b2 2018-12-29 stsp
3468 a1fb16d8 2019-05-24 stsp if (branch_name) {
3469 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
3470 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3471 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3472 f2ea84fa 2019-07-27 stsp continue;
3473 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3474 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3475 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3476 024e9686 2019-05-14 stsp goto done;
3477 024e9686 2019-05-14 stsp }
3478 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3479 024e9686 2019-05-14 stsp if (error)
3480 024e9686 2019-05-14 stsp goto done;
3481 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3482 3aef623b 2019-10-15 stsp repo);
3483 a367ff0f 2019-05-14 stsp free(head_commit_id);
3484 024e9686 2019-05-14 stsp if (error != NULL)
3485 024e9686 2019-05-14 stsp goto done;
3486 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3487 a367ff0f 2019-05-14 stsp if (error)
3488 a367ff0f 2019-05-14 stsp goto done;
3489 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3490 024e9686 2019-05-14 stsp if (error)
3491 024e9686 2019-05-14 stsp goto done;
3492 024e9686 2019-05-14 stsp } else {
3493 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3494 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3495 a367ff0f 2019-05-14 stsp if (error != NULL) {
3496 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3497 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3498 024e9686 2019-05-14 stsp goto done;
3499 a367ff0f 2019-05-14 stsp }
3500 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3501 a367ff0f 2019-05-14 stsp if (error)
3502 a367ff0f 2019-05-14 stsp goto done;
3503 024e9686 2019-05-14 stsp }
3504 507dc3bb 2018-12-29 stsp
3505 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3506 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3507 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3508 507dc3bb 2018-12-29 stsp commit_id);
3509 507dc3bb 2018-12-29 stsp if (error)
3510 507dc3bb 2018-12-29 stsp goto done;
3511 507dc3bb 2018-12-29 stsp }
3512 507dc3bb 2018-12-29 stsp
3513 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3514 4ad4a1ec 2021-09-13 tracey upa.verbosity = verbosity;
3515 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3516 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3517 507dc3bb 2018-12-29 stsp if (error != NULL)
3518 507dc3bb 2018-12-29 stsp goto done;
3519 9c4b8182 2019-01-02 stsp
3520 4f3c844b 2021-09-14 stsp if (upa.did_something) {
3521 4f3c844b 2021-09-14 stsp printf("Updated to %s: %s\n",
3522 4f3c844b 2021-09-14 stsp got_worktree_get_head_ref_name(worktree), commit_id_str);
3523 4f3c844b 2021-09-14 stsp } else
3524 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3525 0ae84acc 2022-06-15 tracey
3526 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3527 507dc3bb 2018-12-29 stsp done:
3528 0ae84acc 2022-06-15 tracey if (pack_fds) {
3529 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3530 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3531 0ae84acc 2022-06-15 tracey if (error == NULL)
3532 0ae84acc 2022-06-15 tracey error = pack_err;
3533 0ae84acc 2022-06-15 tracey }
3534 c09a553d 2018-03-12 stsp free(worktree_path);
3535 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3536 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3537 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3538 507dc3bb 2018-12-29 stsp free(commit_id);
3539 9c4b8182 2019-01-02 stsp free(commit_id_str);
3540 c09a553d 2018-03-12 stsp return error;
3541 c09a553d 2018-03-12 stsp }
3542 c09a553d 2018-03-12 stsp
3543 f42b1b34 2018-03-12 stsp static const struct got_error *
3544 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3545 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3546 ee0fb1ff 2022-06-08 stsp int force_text_diff, struct got_repository *repo, FILE *outfile)
3547 5c860e29 2018-03-12 stsp {
3548 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3549 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3550 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
3551 79109fed 2018-03-27 stsp
3552 44392932 2019-08-25 stsp if (blob_id1) {
3553 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3554 44392932 2019-08-25 stsp if (err)
3555 44392932 2019-08-25 stsp goto done;
3556 b72706c3 2022-06-01 stsp f1 = got_opentemp();
3557 b72706c3 2022-06-01 stsp if (f1 == NULL) {
3558 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3559 b72706c3 2022-06-01 stsp goto done;
3560 b72706c3 2022-06-01 stsp }
3561 44392932 2019-08-25 stsp }
3562 79109fed 2018-03-27 stsp
3563 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3564 44392932 2019-08-25 stsp if (err)
3565 44392932 2019-08-25 stsp goto done;
3566 79109fed 2018-03-27 stsp
3567 b72706c3 2022-06-01 stsp f2 = got_opentemp();
3568 b72706c3 2022-06-01 stsp if (f2 == NULL) {
3569 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3570 b72706c3 2022-06-01 stsp goto done;
3571 b72706c3 2022-06-01 stsp }
3572 b72706c3 2022-06-01 stsp
3573 44392932 2019-08-25 stsp while (path[0] == '/')
3574 44392932 2019-08-25 stsp path++;
3575 b72706c3 2022-06-01 stsp err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3576 ee0fb1ff 2022-06-08 stsp diff_context, ignore_whitespace, force_text_diff, outfile);
3577 44392932 2019-08-25 stsp done:
3578 44392932 2019-08-25 stsp if (blob1)
3579 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3580 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3581 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3582 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3583 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3584 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3585 44392932 2019-08-25 stsp return err;
3586 44392932 2019-08-25 stsp }
3587 44392932 2019-08-25 stsp
3588 44392932 2019-08-25 stsp static const struct got_error *
3589 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3590 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3591 ee0fb1ff 2022-06-08 stsp int force_text_diff, struct got_repository *repo, FILE *outfile)
3592 44392932 2019-08-25 stsp {
3593 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3594 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3595 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3596 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
3597 44392932 2019-08-25 stsp
3598 44392932 2019-08-25 stsp if (tree_id1) {
3599 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3600 79109fed 2018-03-27 stsp if (err)
3601 44392932 2019-08-25 stsp goto done;
3602 b72706c3 2022-06-01 stsp f1 = got_opentemp();
3603 b72706c3 2022-06-01 stsp if (f1 == NULL) {
3604 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3605 b72706c3 2022-06-01 stsp goto done;
3606 b72706c3 2022-06-01 stsp }
3607 79109fed 2018-03-27 stsp }
3608 79109fed 2018-03-27 stsp
3609 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3610 0f2b3dca 2018-12-22 stsp if (err)
3611 0f2b3dca 2018-12-22 stsp goto done;
3612 b72706c3 2022-06-01 stsp
3613 b72706c3 2022-06-01 stsp f2 = got_opentemp();
3614 b72706c3 2022-06-01 stsp if (f2 == NULL) {
3615 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3616 b72706c3 2022-06-01 stsp goto done;
3617 b72706c3 2022-06-01 stsp }
3618 0f2b3dca 2018-12-22 stsp
3619 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3620 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3621 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
3622 ee0fb1ff 2022-06-08 stsp arg.outfile = outfile;
3623 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
3624 fe621944 2020-11-10 stsp arg.nlines = 0;
3625 44392932 2019-08-25 stsp while (path[0] == '/')
3626 44392932 2019-08-25 stsp path++;
3627 b72706c3 2022-06-01 stsp err = got_diff_tree(tree1, tree2, f1, f2, path, path, repo,
3628 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3629 0f2b3dca 2018-12-22 stsp done:
3630 79109fed 2018-03-27 stsp if (tree1)
3631 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3632 366e0a5f 2019-10-10 stsp if (tree2)
3633 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3634 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3635 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3636 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3637 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3638 44392932 2019-08-25 stsp return err;
3639 44392932 2019-08-25 stsp }
3640 44392932 2019-08-25 stsp
3641 44392932 2019-08-25 stsp static const struct got_error *
3642 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3643 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3644 0208f208 2020-05-05 stsp {
3645 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3646 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3647 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3648 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3649 0208f208 2020-05-05 stsp
3650 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3651 0208f208 2020-05-05 stsp if (qid != NULL) {
3652 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3653 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3654 d7b5a0e8 2022-04-20 stsp &qid->id);
3655 0208f208 2020-05-05 stsp if (err)
3656 0208f208 2020-05-05 stsp return err;
3657 0208f208 2020-05-05 stsp
3658 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3659 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3660 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3661 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3662 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3663 aa8b5dd0 2021-08-01 stsp }
3664 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3665 0208f208 2020-05-05 stsp
3666 0208f208 2020-05-05 stsp }
3667 0208f208 2020-05-05 stsp
3668 0208f208 2020-05-05 stsp if (tree_id1) {
3669 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3670 0208f208 2020-05-05 stsp if (err)
3671 0208f208 2020-05-05 stsp goto done;
3672 0208f208 2020-05-05 stsp }
3673 0208f208 2020-05-05 stsp
3674 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3675 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3676 0208f208 2020-05-05 stsp if (err)
3677 0208f208 2020-05-05 stsp goto done;
3678 0208f208 2020-05-05 stsp
3679 b72706c3 2022-06-01 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3680 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3681 0208f208 2020-05-05 stsp done:
3682 0208f208 2020-05-05 stsp if (tree1)
3683 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3684 0208f208 2020-05-05 stsp if (tree2)
3685 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3686 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3687 0208f208 2020-05-05 stsp return err;
3688 0208f208 2020-05-05 stsp }
3689 0208f208 2020-05-05 stsp
3690 0208f208 2020-05-05 stsp static const struct got_error *
3691 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3692 ee0fb1ff 2022-06-08 stsp const char *path, int diff_context, struct got_repository *repo,
3693 ee0fb1ff 2022-06-08 stsp FILE *outfile)
3694 44392932 2019-08-25 stsp {
3695 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3696 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3697 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3698 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3699 44392932 2019-08-25 stsp struct got_object_qid *qid;
3700 44392932 2019-08-25 stsp
3701 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3702 44392932 2019-08-25 stsp if (qid != NULL) {
3703 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3704 d7b5a0e8 2022-04-20 stsp &qid->id);
3705 44392932 2019-08-25 stsp if (err)
3706 44392932 2019-08-25 stsp return err;
3707 44392932 2019-08-25 stsp }
3708 44392932 2019-08-25 stsp
3709 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3710 44392932 2019-08-25 stsp int obj_type;
3711 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id2, repo, commit, path);
3712 44392932 2019-08-25 stsp if (err)
3713 44392932 2019-08-25 stsp goto done;
3714 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3715 44392932 2019-08-25 stsp if (err) {
3716 44392932 2019-08-25 stsp free(obj_id2);
3717 44392932 2019-08-25 stsp goto done;
3718 44392932 2019-08-25 stsp }
3719 44392932 2019-08-25 stsp if (pcommit) {
3720 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3721 a44927cc 2022-04-07 stsp pcommit, path);
3722 44392932 2019-08-25 stsp if (err) {
3723 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3724 2e8c69d1 2020-05-04 stsp free(obj_id2);
3725 2e8c69d1 2020-05-04 stsp goto done;
3726 2e8c69d1 2020-05-04 stsp }
3727 2e8c69d1 2020-05-04 stsp } else {
3728 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3729 2e8c69d1 2020-05-04 stsp if (err) {
3730 2e8c69d1 2020-05-04 stsp free(obj_id2);
3731 2e8c69d1 2020-05-04 stsp goto done;
3732 2e8c69d1 2020-05-04 stsp }
3733 44392932 2019-08-25 stsp }
3734 44392932 2019-08-25 stsp }
3735 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3736 44392932 2019-08-25 stsp if (err) {
3737 44392932 2019-08-25 stsp free(obj_id2);
3738 44392932 2019-08-25 stsp goto done;
3739 44392932 2019-08-25 stsp }
3740 ee0fb1ff 2022-06-08 stsp fprintf(outfile,
3741 ee0fb1ff 2022-06-08 stsp "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3742 44392932 2019-08-25 stsp switch (obj_type) {
3743 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3744 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3745 ee0fb1ff 2022-06-08 stsp 0, 0, repo, outfile);
3746 44392932 2019-08-25 stsp break;
3747 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3748 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3749 ee0fb1ff 2022-06-08 stsp 0, 0, repo, outfile);
3750 44392932 2019-08-25 stsp break;
3751 44392932 2019-08-25 stsp default:
3752 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3753 44392932 2019-08-25 stsp break;
3754 44392932 2019-08-25 stsp }
3755 44392932 2019-08-25 stsp free(obj_id1);
3756 44392932 2019-08-25 stsp free(obj_id2);
3757 44392932 2019-08-25 stsp } else {
3758 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3759 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3760 44392932 2019-08-25 stsp if (err)
3761 44392932 2019-08-25 stsp goto done;
3762 579bd556 2020-10-24 stsp if (pcommit) {
3763 579bd556 2020-10-24 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3764 579bd556 2020-10-24 stsp err = got_object_id_str(&id_str1, obj_id1);
3765 579bd556 2020-10-24 stsp if (err)
3766 579bd556 2020-10-24 stsp goto done;
3767 579bd556 2020-10-24 stsp }
3768 ee0fb1ff 2022-06-08 stsp fprintf(outfile,
3769 ee0fb1ff 2022-06-08 stsp "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3770 64453f7e 2020-11-21 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3771 ee0fb1ff 2022-06-08 stsp repo, outfile);
3772 44392932 2019-08-25 stsp }
3773 44392932 2019-08-25 stsp done:
3774 0f2b3dca 2018-12-22 stsp free(id_str1);
3775 0f2b3dca 2018-12-22 stsp free(id_str2);
3776 44392932 2019-08-25 stsp if (pcommit)
3777 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3778 79109fed 2018-03-27 stsp return err;
3779 79109fed 2018-03-27 stsp }
3780 79109fed 2018-03-27 stsp
3781 4bb494d5 2018-06-16 stsp static char *
3782 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3783 6c281f94 2018-06-11 stsp {
3784 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3785 09867e48 2019-08-13 stsp char *p, *s;
3786 09867e48 2019-08-13 stsp
3787 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3788 09867e48 2019-08-13 stsp if (tm == NULL)
3789 09867e48 2019-08-13 stsp return NULL;
3790 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3791 09867e48 2019-08-13 stsp if (s == NULL)
3792 09867e48 2019-08-13 stsp return NULL;
3793 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3794 6c281f94 2018-06-11 stsp if (p)
3795 6c281f94 2018-06-11 stsp *p = '\0';
3796 6c281f94 2018-06-11 stsp return s;
3797 6c281f94 2018-06-11 stsp }
3798 dc424a06 2019-08-07 stsp
3799 6841bf13 2019-11-29 kn static const struct got_error *
3800 793b9394 2022-06-12 op match_commit(int *have_match, struct got_object_id *id,
3801 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3802 6841bf13 2019-11-29 kn {
3803 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3804 6841bf13 2019-11-29 kn regmatch_t regmatch;
3805 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3806 6841bf13 2019-11-29 kn
3807 6841bf13 2019-11-29 kn *have_match = 0;
3808 6841bf13 2019-11-29 kn
3809 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3810 6841bf13 2019-11-29 kn if (err)
3811 6841bf13 2019-11-29 kn return err;
3812 6841bf13 2019-11-29 kn
3813 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3814 6841bf13 2019-11-29 kn if (err)
3815 6841bf13 2019-11-29 kn goto done;
3816 6841bf13 2019-11-29 kn
3817 793b9394 2022-06-12 op if (regexec(regex, got_object_commit_get_author(commit), 1,
3818 793b9394 2022-06-12 op &regmatch, 0) == 0 ||
3819 793b9394 2022-06-12 op regexec(regex, got_object_commit_get_committer(commit), 1,
3820 793b9394 2022-06-12 op &regmatch, 0) == 0 ||
3821 793b9394 2022-06-12 op regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
3822 793b9394 2022-06-12 op regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3823 6841bf13 2019-11-29 kn *have_match = 1;
3824 6841bf13 2019-11-29 kn done:
3825 6841bf13 2019-11-29 kn free(id_str);
3826 6841bf13 2019-11-29 kn free(logmsg);
3827 6841bf13 2019-11-29 kn return err;
3828 6841bf13 2019-11-29 kn }
3829 6841bf13 2019-11-29 kn
3830 0208f208 2020-05-05 stsp static void
3831 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3832 0208f208 2020-05-05 stsp regex_t *regex)
3833 0208f208 2020-05-05 stsp {
3834 0208f208 2020-05-05 stsp regmatch_t regmatch;
3835 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3836 0208f208 2020-05-05 stsp
3837 0208f208 2020-05-05 stsp *have_match = 0;
3838 0208f208 2020-05-05 stsp
3839 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3840 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3841 ee0fb1ff 2022-06-08 stsp *have_match = 1;
3842 ee0fb1ff 2022-06-08 stsp break;
3843 ee0fb1ff 2022-06-08 stsp }
3844 ee0fb1ff 2022-06-08 stsp }
3845 ee0fb1ff 2022-06-08 stsp }
3846 ee0fb1ff 2022-06-08 stsp
3847 ee0fb1ff 2022-06-08 stsp static const struct got_error *
3848 ee0fb1ff 2022-06-08 stsp match_patch(int *have_match, struct got_commit_object *commit,
3849 ee0fb1ff 2022-06-08 stsp struct got_object_id *id, const char *path, int diff_context,
3850 a558dd1b 2022-06-08 stsp struct got_repository *repo, regex_t *regex, FILE *f)
3851 ee0fb1ff 2022-06-08 stsp {
3852 ee0fb1ff 2022-06-08 stsp const struct got_error *err = NULL;
3853 ee0fb1ff 2022-06-08 stsp char *line = NULL;
3854 ee0fb1ff 2022-06-08 stsp size_t linesize = 0;
3855 ee0fb1ff 2022-06-08 stsp ssize_t linelen;
3856 ee0fb1ff 2022-06-08 stsp regmatch_t regmatch;
3857 ee0fb1ff 2022-06-08 stsp
3858 ee0fb1ff 2022-06-08 stsp *have_match = 0;
3859 ee0fb1ff 2022-06-08 stsp
3860 a558dd1b 2022-06-08 stsp err = got_opentemp_truncate(f);
3861 a558dd1b 2022-06-08 stsp if (err)
3862 a558dd1b 2022-06-08 stsp return err;
3863 ee0fb1ff 2022-06-08 stsp
3864 ee0fb1ff 2022-06-08 stsp err = print_patch(commit, id, path, diff_context, repo, f);
3865 ee0fb1ff 2022-06-08 stsp if (err)
3866 ee0fb1ff 2022-06-08 stsp goto done;
3867 ee0fb1ff 2022-06-08 stsp
3868 ee0fb1ff 2022-06-08 stsp if (fseeko(f, 0L, SEEK_SET) == -1) {
3869 ee0fb1ff 2022-06-08 stsp err = got_error_from_errno("fseeko");
3870 ee0fb1ff 2022-06-08 stsp goto done;
3871 ee0fb1ff 2022-06-08 stsp }
3872 ee0fb1ff 2022-06-08 stsp
3873 ee0fb1ff 2022-06-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3874 ee0fb1ff 2022-06-08 stsp if (regexec(regex, line, 1, &regmatch, 0) == 0) {
3875 0208f208 2020-05-05 stsp *have_match = 1;
3876 0208f208 2020-05-05 stsp break;
3877 0208f208 2020-05-05 stsp }
3878 0208f208 2020-05-05 stsp }
3879 ee0fb1ff 2022-06-08 stsp done:
3880 ee0fb1ff 2022-06-08 stsp free(line);
3881 ee0fb1ff 2022-06-08 stsp return err;
3882 0208f208 2020-05-05 stsp }
3883 0208f208 2020-05-05 stsp
3884 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3885 6c281f94 2018-06-11 stsp
3886 888b7d99 2020-12-26 stsp static const struct got_error*
3887 888b7d99 2020-12-26 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
3888 4cac2b4e 2022-06-13 stsp struct got_object_id *id, struct got_repository *repo,
3889 4cac2b4e 2022-06-13 stsp int local_only)
3890 888b7d99 2020-12-26 stsp {
3891 888b7d99 2020-12-26 stsp static const struct got_error *err = NULL;
3892 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3893 888b7d99 2020-12-26 stsp char *s;
3894 888b7d99 2020-12-26 stsp const char *name;
3895 5c860e29 2018-03-12 stsp
3896 888b7d99 2020-12-26 stsp *refs_str = NULL;
3897 888b7d99 2020-12-26 stsp
3898 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
3899 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3900 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3901 a436ad14 2019-08-13 stsp int cmp;
3902 a436ad14 2019-08-13 stsp
3903 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3904 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3905 d9498b20 2019-02-05 stsp continue;
3906 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3907 199a4027 2019-02-02 stsp name += 5;
3908 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3909 7143d404 2019-03-12 stsp continue;
3910 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3911 e34f9ed6 2019-02-02 stsp name += 6;
3912 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3913 4cac2b4e 2022-06-13 stsp if (local_only)
3914 4cac2b4e 2022-06-13 stsp continue;
3915 141c2bff 2019-02-04 stsp name += 8;
3916 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3917 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3918 4343a07f 2020-04-24 stsp continue;
3919 4343a07f 2020-04-24 stsp }
3920 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3921 48cae60d 2020-09-22 stsp if (err)
3922 888b7d99 2020-12-26 stsp break;
3923 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3924 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3925 5d844a1e 2019-08-13 stsp if (err) {
3926 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3927 48cae60d 2020-09-22 stsp free(ref_id);
3928 888b7d99 2020-12-26 stsp break;
3929 48cae60d 2020-09-22 stsp }
3930 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3931 5d844a1e 2019-08-13 stsp err = NULL;
3932 5d844a1e 2019-08-13 stsp tag = NULL;
3933 5d844a1e 2019-08-13 stsp }
3934 a436ad14 2019-08-13 stsp }
3935 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3936 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3937 48cae60d 2020-09-22 stsp free(ref_id);
3938 a436ad14 2019-08-13 stsp if (tag)
3939 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3940 a436ad14 2019-08-13 stsp if (cmp != 0)
3941 a436ad14 2019-08-13 stsp continue;
3942 888b7d99 2020-12-26 stsp s = *refs_str;
3943 888b7d99 2020-12-26 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
3944 888b7d99 2020-12-26 stsp s ? ", " : "", name) == -1) {
3945 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3946 199a4027 2019-02-02 stsp free(s);
3947 888b7d99 2020-12-26 stsp *refs_str = NULL;
3948 888b7d99 2020-12-26 stsp break;
3949 199a4027 2019-02-02 stsp }
3950 199a4027 2019-02-02 stsp free(s);
3951 199a4027 2019-02-02 stsp }
3952 888b7d99 2020-12-26 stsp
3953 888b7d99 2020-12-26 stsp return err;
3954 888b7d99 2020-12-26 stsp }
3955 888b7d99 2020-12-26 stsp
3956 888b7d99 2020-12-26 stsp static const struct got_error *
3957 4cac2b4e 2022-06-13 stsp print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
3958 4cac2b4e 2022-06-13 stsp struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
3959 c1c775eb 2022-06-08 stsp {
3960 c1c775eb 2022-06-08 stsp const struct got_error *err = NULL;
3961 4cac2b4e 2022-06-13 stsp char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
3962 4cac2b4e 2022-06-13 stsp char *comma, *s, *nl;
3963 4cac2b4e 2022-06-13 stsp struct got_reflist_head *refs;
3964 d91e0535 2022-06-13 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
3965 d91e0535 2022-06-13 stsp struct tm tm;
3966 d91e0535 2022-06-13 stsp time_t committer_time;
3967 c1c775eb 2022-06-08 stsp
3968 4cac2b4e 2022-06-13 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3969 4cac2b4e 2022-06-13 stsp if (refs) {
3970 4cac2b4e 2022-06-13 stsp err = build_refs_str(&ref_str, refs, id, repo, 1);
3971 4cac2b4e 2022-06-13 stsp if (err)
3972 85c34e74 2022-06-13 stsp return err;
3973 4cac2b4e 2022-06-13 stsp
3974 4cac2b4e 2022-06-13 stsp /* Display the first matching ref only. */
3975 4cac2b4e 2022-06-13 stsp if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
3976 4cac2b4e 2022-06-13 stsp *comma = '\0';
3977 4cac2b4e 2022-06-13 stsp }
3978 4cac2b4e 2022-06-13 stsp
3979 4cac2b4e 2022-06-13 stsp if (ref_str == NULL) {
3980 4cac2b4e 2022-06-13 stsp err = got_object_id_str(&id_str, id);
3981 4cac2b4e 2022-06-13 stsp if (err)
3982 4cac2b4e 2022-06-13 stsp return err;
3983 4cac2b4e 2022-06-13 stsp }
3984 4cac2b4e 2022-06-13 stsp
3985 d91e0535 2022-06-13 stsp committer_time = got_object_commit_get_committer_time(commit);
3986 85c34e74 2022-06-13 stsp if (gmtime_r(&committer_time, &tm) == NULL) {
3987 85c34e74 2022-06-13 stsp err = got_error_from_errno("gmtime_r");
3988 85c34e74 2022-06-13 stsp goto done;
3989 85c34e74 2022-06-13 stsp }
3990 85c34e74 2022-06-13 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
3991 85c34e74 2022-06-13 stsp err = got_error(GOT_ERR_NO_SPACE);
3992 85c34e74 2022-06-13 stsp goto done;
3993 85c34e74 2022-06-13 stsp }
3994 d91e0535 2022-06-13 stsp
3995 c1c775eb 2022-06-08 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3996 c1c775eb 2022-06-08 stsp if (err)
3997 c1c775eb 2022-06-08 stsp goto done;
3998 c1c775eb 2022-06-08 stsp
3999 c1c775eb 2022-06-08 stsp s = logmsg0;
4000 c1c775eb 2022-06-08 stsp while (isspace((unsigned char)s[0]))
4001 c1c775eb 2022-06-08 stsp s++;
4002 c1c775eb 2022-06-08 stsp
4003 c1c775eb 2022-06-08 stsp nl = strchr(s, '\n');
4004 c1c775eb 2022-06-08 stsp if (nl) {
4005 c1c775eb 2022-06-08 stsp *nl = '\0';
4006 c1c775eb 2022-06-08 stsp }
4007 c1c775eb 2022-06-08 stsp
4008 4cac2b4e 2022-06-13 stsp if (ref_str)
4009 d91e0535 2022-06-13 stsp printf("%s%-7s %s\n", datebuf, ref_str, s);
4010 4cac2b4e 2022-06-13 stsp else
4011 d91e0535 2022-06-13 stsp printf("%s%.7s %s\n", datebuf, id_str, s);
4012 c1c775eb 2022-06-08 stsp
4013 c1c775eb 2022-06-08 stsp if (fflush(stdout) != 0 && err == NULL)
4014 c1c775eb 2022-06-08 stsp err = got_error_from_errno("fflush");
4015 c1c775eb 2022-06-08 stsp done:
4016 c1c775eb 2022-06-08 stsp free(id_str);
4017 4cac2b4e 2022-06-13 stsp free(ref_str);
4018 c1c775eb 2022-06-08 stsp free(logmsg0);
4019 c1c775eb 2022-06-08 stsp return err;
4020 c1c775eb 2022-06-08 stsp }
4021 c1c775eb 2022-06-08 stsp
4022 c1c775eb 2022-06-08 stsp static const struct got_error *
4023 888b7d99 2020-12-26 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
4024 888b7d99 2020-12-26 stsp struct got_repository *repo, const char *path,
4025 888b7d99 2020-12-26 stsp struct got_pathlist_head *changed_paths, int show_patch,
4026 e600f124 2021-03-21 stsp int diff_context, struct got_reflist_object_id_map *refs_idmap,
4027 e600f124 2021-03-21 stsp const char *custom_refs_str)
4028 888b7d99 2020-12-26 stsp {
4029 888b7d99 2020-12-26 stsp const struct got_error *err = NULL;
4030 888b7d99 2020-12-26 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
4031 888b7d99 2020-12-26 stsp char datebuf[26];
4032 888b7d99 2020-12-26 stsp time_t committer_time;
4033 888b7d99 2020-12-26 stsp const char *author, *committer;
4034 888b7d99 2020-12-26 stsp char *refs_str = NULL;
4035 888b7d99 2020-12-26 stsp
4036 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
4037 8bf5b3c9 2018-03-17 stsp if (err)
4038 8bf5b3c9 2018-03-17 stsp return err;
4039 788c352e 2018-06-16 stsp
4040 e600f124 2021-03-21 stsp if (custom_refs_str == NULL) {
4041 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
4042 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4043 e600f124 2021-03-21 stsp if (refs) {
4044 4cac2b4e 2022-06-13 stsp err = build_refs_str(&refs_str, refs, id, repo, 0);
4045 e600f124 2021-03-21 stsp if (err)
4046 e600f124 2021-03-21 stsp goto done;
4047 e600f124 2021-03-21 stsp }
4048 888b7d99 2020-12-26 stsp }
4049 888b7d99 2020-12-26 stsp
4050 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
4051 e600f124 2021-03-21 stsp if (custom_refs_str)
4052 e600f124 2021-03-21 stsp printf("commit %s (%s)\n", id_str, custom_refs_str);
4053 e600f124 2021-03-21 stsp else
4054 e600f124 2021-03-21 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4055 e600f124 2021-03-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4056 832c249c 2018-06-10 stsp free(id_str);
4057 d3d493d7 2019-02-21 stsp id_str = NULL;
4058 d3d493d7 2019-02-21 stsp free(refs_str);
4059 d3d493d7 2019-02-21 stsp refs_str = NULL;
4060 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
4061 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4062 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
4063 09867e48 2019-08-13 stsp if (datestr)
4064 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
4065 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4066 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4067 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
4068 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
4069 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
4070 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4071 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
4072 3fe1abad 2018-06-10 stsp int n = 1;
4073 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4074 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, parent_ids, entry) {
4075 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4076 a0603db2 2018-06-10 stsp if (err)
4077 888b7d99 2020-12-26 stsp goto done;
4078 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
4079 a0603db2 2018-06-10 stsp free(id_str);
4080 888b7d99 2020-12-26 stsp id_str = NULL;
4081 a0603db2 2018-06-10 stsp }
4082 a0603db2 2018-06-10 stsp }
4083 832c249c 2018-06-10 stsp
4084 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
4085 5943eee2 2019-08-13 stsp if (err)
4086 888b7d99 2020-12-26 stsp goto done;
4087 8bf5b3c9 2018-03-17 stsp
4088 621015ac 2018-07-23 stsp logmsg = logmsg0;
4089 832c249c 2018-06-10 stsp do {
4090 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
4091 832c249c 2018-06-10 stsp if (line)
4092 832c249c 2018-06-10 stsp printf(" %s\n", line);
4093 832c249c 2018-06-10 stsp } while (line);
4094 621015ac 2018-07-23 stsp free(logmsg0);
4095 832c249c 2018-06-10 stsp
4096 0208f208 2020-05-05 stsp if (changed_paths) {
4097 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4098 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
4099 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4100 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
4101 0208f208 2020-05-05 stsp }
4102 0208f208 2020-05-05 stsp printf("\n");
4103 0208f208 2020-05-05 stsp }
4104 971751ac 2018-03-27 stsp if (show_patch) {
4105 ee0fb1ff 2022-06-08 stsp err = print_patch(commit, id, path, diff_context, repo, stdout);
4106 971751ac 2018-03-27 stsp if (err == 0)
4107 971751ac 2018-03-27 stsp printf("\n");
4108 971751ac 2018-03-27 stsp }
4109 07862c20 2018-09-15 stsp
4110 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
4111 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4112 888b7d99 2020-12-26 stsp done:
4113 888b7d99 2020-12-26 stsp free(id_str);
4114 888b7d99 2020-12-26 stsp free(refs_str);
4115 07862c20 2018-09-15 stsp return err;
4116 f42b1b34 2018-03-12 stsp }
4117 5c860e29 2018-03-12 stsp
4118 f42b1b34 2018-03-12 stsp static const struct got_error *
4119 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4120 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
4121 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
4122 888b7d99 2020-12-26 stsp int log_branches, int reverse_display_order,
4123 a558dd1b 2022-06-08 stsp struct got_reflist_object_id_map *refs_idmap, int one_line,
4124 a558dd1b 2022-06-08 stsp FILE *tmpfile)
4125 f42b1b34 2018-03-12 stsp {
4126 f42b1b34 2018-03-12 stsp const struct got_error *err;
4127 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
4128 6841bf13 2019-11-29 kn regex_t regex;
4129 6841bf13 2019-11-29 kn int have_match;
4130 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
4131 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
4132 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
4133 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4134 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4135 dbec59df 2020-04-18 stsp
4136 dbdddfee 2021-06-23 naddy STAILQ_INIT(&reversed_commits);
4137 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4138 372ccdbb 2018-06-10 stsp
4139 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(&regex, search_pattern,
4140 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4141 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
4142 6841bf13 2019-11-29 kn
4143 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
4144 f42b1b34 2018-03-12 stsp if (err)
4145 f42b1b34 2018-03-12 stsp return err;
4146 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
4147 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
4148 372ccdbb 2018-06-10 stsp if (err)
4149 fcc85cad 2018-07-22 stsp goto done;
4150 656b1f76 2019-05-11 jcs for (;;) {
4151 372ccdbb 2018-06-10 stsp struct got_object_id *id;
4152 8bf5b3c9 2018-03-17 stsp
4153 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4154 84453469 2018-11-11 stsp break;
4155 84453469 2018-11-11 stsp
4156 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
4157 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
4158 372ccdbb 2018-06-10 stsp if (err) {
4159 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
4160 9ba79e04 2018-06-11 stsp err = NULL;
4161 ee780d5c 2020-01-04 stsp break;
4162 7e665116 2018-04-02 stsp }
4163 b43fbaa0 2018-06-11 stsp if (id == NULL)
4164 7e665116 2018-04-02 stsp break;
4165 b43fbaa0 2018-06-11 stsp
4166 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
4167 b43fbaa0 2018-06-11 stsp if (err)
4168 fcc85cad 2018-07-22 stsp break;
4169 6841bf13 2019-11-29 kn
4170 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
4171 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4172 0208f208 2020-05-05 stsp if (err)
4173 0208f208 2020-05-05 stsp break;
4174 0208f208 2020-05-05 stsp }
4175 0208f208 2020-05-05 stsp
4176 6841bf13 2019-11-29 kn if (search_pattern) {
4177 793b9394 2022-06-12 op err = match_commit(&have_match, id, commit, &regex);
4178 6841bf13 2019-11-29 kn if (err) {
4179 6841bf13 2019-11-29 kn got_object_commit_close(commit);
4180 6841bf13 2019-11-29 kn break;
4181 6841bf13 2019-11-29 kn }
4182 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
4183 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
4184 0208f208 2020-05-05 stsp &changed_paths, &regex);
4185 ee0fb1ff 2022-06-08 stsp if (have_match == 0 && show_patch) {
4186 ee0fb1ff 2022-06-08 stsp err = match_patch(&have_match, commit, id,
4187 a558dd1b 2022-06-08 stsp path, diff_context, repo, &regex,
4188 a558dd1b 2022-06-08 stsp tmpfile);
4189 ee0fb1ff 2022-06-08 stsp if (err)
4190 ee0fb1ff 2022-06-08 stsp break;
4191 ee0fb1ff 2022-06-08 stsp }
4192 6841bf13 2019-11-29 kn if (have_match == 0) {
4193 6841bf13 2019-11-29 kn got_object_commit_close(commit);
4194 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4195 0208f208 2020-05-05 stsp free((char *)pe->path);
4196 0208f208 2020-05-05 stsp free(pe->data);
4197 0208f208 2020-05-05 stsp }
4198 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4199 6841bf13 2019-11-29 kn continue;
4200 6841bf13 2019-11-29 kn }
4201 6841bf13 2019-11-29 kn }
4202 6841bf13 2019-11-29 kn
4203 dbec59df 2020-04-18 stsp if (reverse_display_order) {
4204 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
4205 dbec59df 2020-04-18 stsp if (err)
4206 dbec59df 2020-04-18 stsp break;
4207 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4208 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
4209 dbec59df 2020-04-18 stsp } else {
4210 c1c775eb 2022-06-08 stsp if (one_line)
4211 4cac2b4e 2022-06-13 stsp err = print_commit_oneline(commit, id,
4212 4cac2b4e 2022-06-13 stsp repo, refs_idmap);
4213 c1c775eb 2022-06-08 stsp else
4214 c1c775eb 2022-06-08 stsp err = print_commit(commit, id, repo, path,
4215 c1c775eb 2022-06-08 stsp show_changed_paths ? &changed_paths : NULL,
4216 c1c775eb 2022-06-08 stsp show_patch, diff_context, refs_idmap, NULL);
4217 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
4218 dbec59df 2020-04-18 stsp if (err)
4219 dbec59df 2020-04-18 stsp break;
4220 dbec59df 2020-04-18 stsp }
4221 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
4222 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
4223 7e665116 2018-04-02 stsp break;
4224 0208f208 2020-05-05 stsp
4225 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4226 0208f208 2020-05-05 stsp free((char *)pe->path);
4227 0208f208 2020-05-05 stsp free(pe->data);
4228 0208f208 2020-05-05 stsp }
4229 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4230 31cedeaf 2018-09-15 stsp }
4231 dbec59df 2020-04-18 stsp if (reverse_display_order) {
4232 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &reversed_commits, entry) {
4233 d7b5a0e8 2022-04-20 stsp err = got_object_open_as_commit(&commit, repo,
4234 d7b5a0e8 2022-04-20 stsp &qid->id);
4235 dbec59df 2020-04-18 stsp if (err)
4236 dbec59df 2020-04-18 stsp break;
4237 502b9684 2020-07-31 stsp if (show_changed_paths) {
4238 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
4239 502b9684 2020-07-31 stsp commit, repo);
4240 502b9684 2020-07-31 stsp if (err)
4241 502b9684 2020-07-31 stsp break;
4242 502b9684 2020-07-31 stsp }
4243 c1c775eb 2022-06-08 stsp if (one_line)
4244 4cac2b4e 2022-06-13 stsp err = print_commit_oneline(commit, &qid->id,
4245 4cac2b4e 2022-06-13 stsp repo, refs_idmap);
4246 c1c775eb 2022-06-08 stsp else
4247 c1c775eb 2022-06-08 stsp err = print_commit(commit, &qid->id, repo, path,
4248 c1c775eb 2022-06-08 stsp show_changed_paths ? &changed_paths : NULL,
4249 c1c775eb 2022-06-08 stsp show_patch, diff_context, refs_idmap, NULL);
4250 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
4251 dbec59df 2020-04-18 stsp if (err)
4252 dbec59df 2020-04-18 stsp break;
4253 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4254 502b9684 2020-07-31 stsp free((char *)pe->path);
4255 502b9684 2020-07-31 stsp free(pe->data);
4256 502b9684 2020-07-31 stsp }
4257 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
4258 dbec59df 2020-04-18 stsp }
4259 dbec59df 2020-04-18 stsp }
4260 fcc85cad 2018-07-22 stsp done:
4261 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&reversed_commits)) {
4262 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&reversed_commits);
4263 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4264 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
4265 dbec59df 2020-04-18 stsp }
4266 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4267 0208f208 2020-05-05 stsp free((char *)pe->path);
4268 0208f208 2020-05-05 stsp free(pe->data);
4269 0208f208 2020-05-05 stsp }
4270 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4271 6841bf13 2019-11-29 kn if (search_pattern)
4272 6841bf13 2019-11-29 kn regfree(&regex);
4273 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
4274 f42b1b34 2018-03-12 stsp return err;
4275 f42b1b34 2018-03-12 stsp }
4276 5c860e29 2018-03-12 stsp
4277 4ed7e80c 2018-05-20 stsp __dead static void
4278 6f3d1eb0 2018-03-12 stsp usage_log(void)
4279 6f3d1eb0 2018-03-12 stsp {
4280 c1c775eb 2022-06-08 stsp fprintf(stderr, "usage: %s log [-b] [-p] [-P] [-s] [-c commit] "
4281 c1c775eb 2022-06-08 stsp "[-C number] [ -l N ] [-x commit] [-S search-pattern] "
4282 c1c775eb 2022-06-08 stsp "[-r repository-path] [-R] [path]\n", getprogname());
4283 6f3d1eb0 2018-03-12 stsp exit(1);
4284 b1ebc001 2019-08-13 stsp }
4285 b1ebc001 2019-08-13 stsp
4286 b1ebc001 2019-08-13 stsp static int
4287 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
4288 b1ebc001 2019-08-13 stsp {
4289 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
4290 b1ebc001 2019-08-13 stsp long long n;
4291 b1ebc001 2019-08-13 stsp const char *errstr;
4292 b1ebc001 2019-08-13 stsp
4293 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4294 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
4295 b1ebc001 2019-08-13 stsp return 0;
4296 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4297 b1ebc001 2019-08-13 stsp if (errstr != NULL)
4298 b1ebc001 2019-08-13 stsp return 0;
4299 b1ebc001 2019-08-13 stsp return n;
4300 6f3d1eb0 2018-03-12 stsp }
4301 6f3d1eb0 2018-03-12 stsp
4302 4ed7e80c 2018-05-20 stsp static const struct got_error *
4303 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
4304 f42b1b34 2018-03-12 stsp {
4305 f42b1b34 2018-03-12 stsp const struct got_error *error;
4306 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
4307 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
4308 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
4309 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4310 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
4311 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
4312 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
4313 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4314 c1c775eb 2022-06-08 stsp int reverse_display_order = 0, one_line = 0;
4315 64a96a6d 2018-04-01 stsp const char *errstr;
4316 199a4027 2019-02-02 stsp struct got_reflist_head refs;
4317 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
4318 a558dd1b 2022-06-08 stsp FILE *tmpfile = NULL;
4319 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4320 1b3893a2 2019-03-18 stsp
4321 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4322 5c860e29 2018-03-12 stsp
4323 6715a751 2018-03-16 stsp #ifndef PROFILE
4324 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4325 6098196c 2019-01-04 stsp NULL)
4326 ad242220 2018-09-08 stsp == -1)
4327 f42b1b34 2018-03-12 stsp err(1, "pledge");
4328 6715a751 2018-03-16 stsp #endif
4329 79109fed 2018-03-27 stsp
4330 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
4331 b1ebc001 2019-08-13 stsp
4332 c1c775eb 2022-06-08 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:RsS:x:")) != -1) {
4333 79109fed 2018-03-27 stsp switch (ch) {
4334 79109fed 2018-03-27 stsp case 'p':
4335 79109fed 2018-03-27 stsp show_patch = 1;
4336 d142fc45 2018-04-01 stsp break;
4337 0208f208 2020-05-05 stsp case 'P':
4338 0208f208 2020-05-05 stsp show_changed_paths = 1;
4339 0208f208 2020-05-05 stsp break;
4340 d142fc45 2018-04-01 stsp case 'c':
4341 d142fc45 2018-04-01 stsp start_commit = optarg;
4342 64a96a6d 2018-04-01 stsp break;
4343 c0cc5c62 2018-10-18 stsp case 'C':
4344 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4345 4a8520aa 2018-10-18 stsp &errstr);
4346 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4347 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4348 5a20d08d 2022-02-09 op errstr, optarg);
4349 c0cc5c62 2018-10-18 stsp break;
4350 64a96a6d 2018-04-01 stsp case 'l':
4351 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
4352 64a96a6d 2018-04-01 stsp if (errstr != NULL)
4353 5a20d08d 2022-02-09 op errx(1, "number of commits is %s: %s",
4354 5a20d08d 2022-02-09 op errstr, optarg);
4355 cc54c501 2019-07-15 stsp break;
4356 48c8c60d 2020-01-27 stsp case 'b':
4357 48c8c60d 2020-01-27 stsp log_branches = 1;
4358 a0603db2 2018-06-10 stsp break;
4359 04ca23f4 2018-07-16 stsp case 'r':
4360 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4361 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
4362 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4363 9ba1d308 2019-10-21 stsp optarg);
4364 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4365 04ca23f4 2018-07-16 stsp break;
4366 dbec59df 2020-04-18 stsp case 'R':
4367 dbec59df 2020-04-18 stsp reverse_display_order = 1;
4368 dbec59df 2020-04-18 stsp break;
4369 6841bf13 2019-11-29 kn case 's':
4370 c1c775eb 2022-06-08 stsp one_line = 1;
4371 c1c775eb 2022-06-08 stsp break;
4372 c1c775eb 2022-06-08 stsp case 'S':
4373 6841bf13 2019-11-29 kn search_pattern = optarg;
4374 6841bf13 2019-11-29 kn break;
4375 d1fe46f9 2020-04-18 stsp case 'x':
4376 d1fe46f9 2020-04-18 stsp end_commit = optarg;
4377 d1fe46f9 2020-04-18 stsp break;
4378 79109fed 2018-03-27 stsp default:
4379 2deda0b9 2019-03-07 stsp usage_log();
4380 79109fed 2018-03-27 stsp /* NOTREACHED */
4381 79109fed 2018-03-27 stsp }
4382 79109fed 2018-03-27 stsp }
4383 79109fed 2018-03-27 stsp
4384 79109fed 2018-03-27 stsp argc -= optind;
4385 79109fed 2018-03-27 stsp argv += optind;
4386 f42b1b34 2018-03-12 stsp
4387 dc1edbfa 2019-11-29 kn if (diff_context == -1)
4388 dc1edbfa 2019-11-29 kn diff_context = 3;
4389 dc1edbfa 2019-11-29 kn else if (!show_patch)
4390 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
4391 c1c775eb 2022-06-08 stsp
4392 c1c775eb 2022-06-08 stsp if (one_line && (show_patch || show_changed_paths))
4393 c1c775eb 2022-06-08 stsp errx(1, "cannot use -s with -p or -P");
4394 dc1edbfa 2019-11-29 kn
4395 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
4396 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
4397 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4398 04ca23f4 2018-07-16 stsp goto done;
4399 04ca23f4 2018-07-16 stsp }
4400 0ae84acc 2022-06-15 tracey
4401 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4402 0ae84acc 2022-06-15 tracey if (error != NULL)
4403 0ae84acc 2022-06-15 tracey goto done;
4404 cffc0aa4 2019-02-05 stsp
4405 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
4406 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
4407 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4408 50f2fada 2020-04-24 stsp goto done;
4409 50f2fada 2020-04-24 stsp error = NULL;
4410 50f2fada 2020-04-24 stsp }
4411 cffc0aa4 2019-02-05 stsp
4412 603cdeb0 2020-10-22 stsp if (argc == 1) {
4413 e7301579 2019-03-18 stsp if (worktree) {
4414 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4415 e7301579 2019-03-18 stsp argv[0]);
4416 e7301579 2019-03-18 stsp if (error)
4417 e7301579 2019-03-18 stsp goto done;
4418 e7301579 2019-03-18 stsp } else {
4419 e7301579 2019-03-18 stsp path = strdup(argv[0]);
4420 e7301579 2019-03-18 stsp if (path == NULL) {
4421 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4422 e7301579 2019-03-18 stsp goto done;
4423 e7301579 2019-03-18 stsp }
4424 e7301579 2019-03-18 stsp }
4425 603cdeb0 2020-10-22 stsp } else if (argc != 0)
4426 cbd1af7a 2019-03-18 stsp usage_log();
4427 cbd1af7a 2019-03-18 stsp
4428 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
4429 5486daa2 2019-05-11 stsp repo_path = worktree ?
4430 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4431 5486daa2 2019-05-11 stsp }
4432 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
4433 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4434 cffc0aa4 2019-02-05 stsp goto done;
4435 04ca23f4 2018-07-16 stsp }
4436 6098196c 2019-01-04 stsp
4437 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4438 d7d4f210 2018-03-12 stsp if (error != NULL)
4439 04ca23f4 2018-07-16 stsp goto done;
4440 f42b1b34 2018-03-12 stsp
4441 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4442 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4443 c02c541e 2019-03-29 stsp if (error)
4444 c02c541e 2019-03-29 stsp goto done;
4445 c02c541e 2019-03-29 stsp
4446 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4447 84de9106 2020-12-26 stsp if (error)
4448 84de9106 2020-12-26 stsp goto done;
4449 84de9106 2020-12-26 stsp
4450 888b7d99 2020-12-26 stsp error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4451 888b7d99 2020-12-26 stsp if (error)
4452 888b7d99 2020-12-26 stsp goto done;
4453 888b7d99 2020-12-26 stsp
4454 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
4455 3235492e 2018-04-01 stsp struct got_reference *head_ref;
4456 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
4457 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
4458 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4459 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
4460 3235492e 2018-04-01 stsp if (error != NULL)
4461 d1fe46f9 2020-04-18 stsp goto done;
4462 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
4463 3235492e 2018-04-01 stsp got_ref_close(head_ref);
4464 3235492e 2018-04-01 stsp if (error != NULL)
4465 d1fe46f9 2020-04-18 stsp goto done;
4466 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
4467 d1fe46f9 2020-04-18 stsp start_id);
4468 d1fe46f9 2020-04-18 stsp if (error != NULL)
4469 d1fe46f9 2020-04-18 stsp goto done;
4470 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
4471 3235492e 2018-04-01 stsp } else {
4472 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&start_id, NULL,
4473 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4474 d1fe46f9 2020-04-18 stsp if (error != NULL)
4475 d1fe46f9 2020-04-18 stsp goto done;
4476 3235492e 2018-04-01 stsp }
4477 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
4478 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&end_id, NULL,
4479 84de9106 2020-12-26 stsp end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4480 d1fe46f9 2020-04-18 stsp if (error != NULL)
4481 d1fe46f9 2020-04-18 stsp goto done;
4482 d1fe46f9 2020-04-18 stsp }
4483 04ca23f4 2018-07-16 stsp
4484 deeabeae 2019-08-27 stsp if (worktree) {
4485 603cdeb0 2020-10-22 stsp /*
4486 603cdeb0 2020-10-22 stsp * If a path was specified on the command line it was resolved
4487 603cdeb0 2020-10-22 stsp * to a path in the work tree above. Prepend the work tree's
4488 603cdeb0 2020-10-22 stsp * path prefix to obtain the corresponding in-repository path.
4489 603cdeb0 2020-10-22 stsp */
4490 603cdeb0 2020-10-22 stsp if (path) {
4491 603cdeb0 2020-10-22 stsp const char *prefix;
4492 603cdeb0 2020-10-22 stsp prefix = got_worktree_get_path_prefix(worktree);
4493 603cdeb0 2020-10-22 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4494 603cdeb0 2020-10-22 stsp (path[0] != '\0') ? "/" : "", path) == -1) {
4495 603cdeb0 2020-10-22 stsp error = got_error_from_errno("asprintf");
4496 603cdeb0 2020-10-22 stsp goto done;
4497 603cdeb0 2020-10-22 stsp }
4498 603cdeb0 2020-10-22 stsp }
4499 deeabeae 2019-08-27 stsp } else
4500 603cdeb0 2020-10-22 stsp error = got_repo_map_path(&in_repo_path, repo,
4501 8fa913ec 2020-11-14 stsp path ? path : "");
4502 04ca23f4 2018-07-16 stsp if (error != NULL)
4503 04ca23f4 2018-07-16 stsp goto done;
4504 04ca23f4 2018-07-16 stsp if (in_repo_path) {
4505 04ca23f4 2018-07-16 stsp free(path);
4506 04ca23f4 2018-07-16 stsp path = in_repo_path;
4507 04ca23f4 2018-07-16 stsp }
4508 199a4027 2019-02-02 stsp
4509 f7fce2e2 2022-03-12 stsp if (worktree) {
4510 f7fce2e2 2022-03-12 stsp /* Release work tree lock. */
4511 f7fce2e2 2022-03-12 stsp got_worktree_close(worktree);
4512 f7fce2e2 2022-03-12 stsp worktree = NULL;
4513 f7fce2e2 2022-03-12 stsp }
4514 f7fce2e2 2022-03-12 stsp
4515 a558dd1b 2022-06-08 stsp if (search_pattern && show_patch) {
4516 a558dd1b 2022-06-08 stsp tmpfile = got_opentemp();
4517 a558dd1b 2022-06-08 stsp if (tmpfile == NULL) {
4518 a558dd1b 2022-06-08 stsp error = got_error_from_errno("got_opentemp");
4519 a558dd1b 2022-06-08 stsp goto done;
4520 a558dd1b 2022-06-08 stsp }
4521 a558dd1b 2022-06-08 stsp }
4522 a558dd1b 2022-06-08 stsp
4523 603cdeb0 2020-10-22 stsp error = print_commits(start_id, end_id, repo, path ? path : "",
4524 603cdeb0 2020-10-22 stsp show_changed_paths, show_patch, search_pattern, diff_context,
4525 a558dd1b 2022-06-08 stsp limit, log_branches, reverse_display_order, refs_idmap, one_line,
4526 a558dd1b 2022-06-08 stsp tmpfile);
4527 04ca23f4 2018-07-16 stsp done:
4528 04ca23f4 2018-07-16 stsp free(path);
4529 04ca23f4 2018-07-16 stsp free(repo_path);
4530 04ca23f4 2018-07-16 stsp free(cwd);
4531 cffc0aa4 2019-02-05 stsp if (worktree)
4532 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
4533 ad242220 2018-09-08 stsp if (repo) {
4534 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4535 ad242220 2018-09-08 stsp if (error == NULL)
4536 1d0f4054 2021-06-17 stsp error = close_err;
4537 0ae84acc 2022-06-15 tracey }
4538 0ae84acc 2022-06-15 tracey if (pack_fds) {
4539 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4540 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4541 0ae84acc 2022-06-15 tracey if (error == NULL)
4542 0ae84acc 2022-06-15 tracey error = pack_err;
4543 ad242220 2018-09-08 stsp }
4544 888b7d99 2020-12-26 stsp if (refs_idmap)
4545 888b7d99 2020-12-26 stsp got_reflist_object_id_map_free(refs_idmap);
4546 a558dd1b 2022-06-08 stsp if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4547 a558dd1b 2022-06-08 stsp error = got_error_from_errno("fclose");
4548 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4549 8bf5b3c9 2018-03-17 stsp return error;
4550 5c860e29 2018-03-12 stsp }
4551 5c860e29 2018-03-12 stsp
4552 4ed7e80c 2018-05-20 stsp __dead static void
4553 3f8b7d6a 2018-04-01 stsp usage_diff(void)
4554 3f8b7d6a 2018-04-01 stsp {
4555 51ed7429 2021-10-10 stsp fprintf(stderr, "usage: %s diff [-a] [-c commit] [-C number] "
4556 51ed7429 2021-10-10 stsp "[-r repository-path] [-s] [-w] [-P] "
4557 51ed7429 2021-10-10 stsp "[object1 object2 | path ...]\n", getprogname());
4558 3f8b7d6a 2018-04-01 stsp exit(1);
4559 b00d56cd 2018-04-01 stsp }
4560 b00d56cd 2018-04-01 stsp
4561 b72f483a 2019-02-05 stsp struct print_diff_arg {
4562 b72f483a 2019-02-05 stsp struct got_repository *repo;
4563 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
4564 b72f483a 2019-02-05 stsp int diff_context;
4565 3fc0c068 2019-02-10 stsp const char *id_str;
4566 3fc0c068 2019-02-10 stsp int header_shown;
4567 98eaaa12 2019-08-03 stsp int diff_staged;
4568 63035f9f 2019-10-06 stsp int ignore_whitespace;
4569 64453f7e 2020-11-21 stsp int force_text_diff;
4570 b72f483a 2019-02-05 stsp };
4571 39449a05 2020-07-23 stsp
4572 39449a05 2020-07-23 stsp /*
4573 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
4574 39449a05 2020-07-23 stsp * it as content to the diff engine.
4575 39449a05 2020-07-23 stsp */
4576 39449a05 2020-07-23 stsp static const struct got_error *
4577 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4578 39449a05 2020-07-23 stsp const char *abspath)
4579 39449a05 2020-07-23 stsp {
4580 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
4581 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
4582 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
4583 39449a05 2020-07-23 stsp
4584 39449a05 2020-07-23 stsp *fd = -1;
4585 39449a05 2020-07-23 stsp
4586 39449a05 2020-07-23 stsp if (dirfd != -1) {
4587 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4588 39449a05 2020-07-23 stsp if (target_len == -1)
4589 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
4590 39449a05 2020-07-23 stsp } else {
4591 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
4592 39449a05 2020-07-23 stsp if (target_len == -1)
4593 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
4594 39449a05 2020-07-23 stsp }
4595 39449a05 2020-07-23 stsp
4596 39449a05 2020-07-23 stsp *fd = got_opentempfd();
4597 39449a05 2020-07-23 stsp if (*fd == -1)
4598 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
4599 39449a05 2020-07-23 stsp
4600 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
4601 39449a05 2020-07-23 stsp if (outlen == -1) {
4602 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
4603 39449a05 2020-07-23 stsp goto done;
4604 39449a05 2020-07-23 stsp }
4605 39449a05 2020-07-23 stsp
4606 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
4607 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
4608 39449a05 2020-07-23 stsp goto done;
4609 39449a05 2020-07-23 stsp }
4610 39449a05 2020-07-23 stsp done:
4611 39449a05 2020-07-23 stsp if (err) {
4612 39449a05 2020-07-23 stsp close(*fd);
4613 39449a05 2020-07-23 stsp *fd = -1;
4614 39449a05 2020-07-23 stsp }
4615 39449a05 2020-07-23 stsp return err;
4616 39449a05 2020-07-23 stsp }
4617 b72f483a 2019-02-05 stsp
4618 4ed7e80c 2018-05-20 stsp static const struct got_error *
4619 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
4620 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4621 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4622 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4623 b72f483a 2019-02-05 stsp {
4624 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
4625 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
4626 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
4627 12463d8b 2019-12-13 stsp int fd = -1;
4628 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
4629 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
4630 b72f483a 2019-02-05 stsp struct stat sb;
4631 b72706c3 2022-06-01 stsp off_t size1 = 0;
4632 b72f483a 2019-02-05 stsp
4633 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4634 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4635 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4636 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4637 98eaaa12 2019-08-03 stsp return NULL;
4638 98eaaa12 2019-08-03 stsp } else {
4639 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4640 98eaaa12 2019-08-03 stsp return NULL;
4641 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4642 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
4643 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4644 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
4645 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
4646 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
4647 98eaaa12 2019-08-03 stsp return NULL;
4648 98eaaa12 2019-08-03 stsp }
4649 3fc0c068 2019-02-10 stsp
4650 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
4651 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
4652 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
4653 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
4654 3fc0c068 2019-02-10 stsp a->header_shown = 1;
4655 3fc0c068 2019-02-10 stsp }
4656 b72f483a 2019-02-05 stsp
4657 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4658 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
4659 98eaaa12 2019-08-03 stsp switch (staged_status) {
4660 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
4661 98eaaa12 2019-08-03 stsp label1 = path;
4662 98eaaa12 2019-08-03 stsp label2 = path;
4663 98eaaa12 2019-08-03 stsp break;
4664 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
4665 98eaaa12 2019-08-03 stsp label2 = path;
4666 98eaaa12 2019-08-03 stsp break;
4667 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
4668 98eaaa12 2019-08-03 stsp label1 = path;
4669 98eaaa12 2019-08-03 stsp break;
4670 98eaaa12 2019-08-03 stsp default:
4671 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
4672 98eaaa12 2019-08-03 stsp }
4673 b72706c3 2022-06-01 stsp f1 = got_opentemp();
4674 b72706c3 2022-06-01 stsp if (f1 == NULL) {
4675 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4676 b72706c3 2022-06-01 stsp goto done;
4677 b72706c3 2022-06-01 stsp }
4678 b72706c3 2022-06-01 stsp f2 = got_opentemp();
4679 b72706c3 2022-06-01 stsp if (f2 == NULL) {
4680 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4681 b72706c3 2022-06-01 stsp goto done;
4682 b72706c3 2022-06-01 stsp }
4683 b72706c3 2022-06-01 stsp err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
4684 b72706c3 2022-06-01 stsp blob_id, staged_blob_id, label1, label2, a->diff_context,
4685 64453f7e 2020-11-21 stsp a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4686 b72706c3 2022-06-01 stsp goto done;
4687 98eaaa12 2019-08-03 stsp }
4688 98eaaa12 2019-08-03 stsp
4689 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4690 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
4691 4ce46740 2019-08-08 stsp char *id_str;
4692 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4693 408b4ebc 2019-08-03 stsp 8192);
4694 4ce46740 2019-08-08 stsp if (err)
4695 4ce46740 2019-08-08 stsp goto done;
4696 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4697 4ce46740 2019-08-08 stsp if (err)
4698 4ce46740 2019-08-08 stsp goto done;
4699 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4700 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4701 4ce46740 2019-08-08 stsp free(id_str);
4702 4ce46740 2019-08-08 stsp goto done;
4703 4ce46740 2019-08-08 stsp }
4704 4ce46740 2019-08-08 stsp free(id_str);
4705 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4706 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4707 4ce46740 2019-08-08 stsp if (err)
4708 4ce46740 2019-08-08 stsp goto done;
4709 4ce46740 2019-08-08 stsp }
4710 d00136be 2019-03-26 stsp
4711 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4712 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4713 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4714 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4715 2ec1f75b 2019-03-26 stsp goto done;
4716 2ec1f75b 2019-03-26 stsp }
4717 b72f483a 2019-02-05 stsp
4718 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4719 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, de_name,
4720 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4721 12463d8b 2019-12-13 stsp if (fd == -1) {
4722 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4723 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4724 39449a05 2020-07-23 stsp abspath);
4725 39449a05 2020-07-23 stsp goto done;
4726 39449a05 2020-07-23 stsp }
4727 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4728 39449a05 2020-07-23 stsp de_name, abspath);
4729 39449a05 2020-07-23 stsp if (err)
4730 39449a05 2020-07-23 stsp goto done;
4731 12463d8b 2019-12-13 stsp }
4732 12463d8b 2019-12-13 stsp } else {
4733 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4734 12463d8b 2019-12-13 stsp if (fd == -1) {
4735 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4736 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4737 39449a05 2020-07-23 stsp abspath);
4738 39449a05 2020-07-23 stsp goto done;
4739 39449a05 2020-07-23 stsp }
4740 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4741 39449a05 2020-07-23 stsp de_name, abspath);
4742 39449a05 2020-07-23 stsp if (err)
4743 39449a05 2020-07-23 stsp goto done;
4744 12463d8b 2019-12-13 stsp }
4745 12463d8b 2019-12-13 stsp }
4746 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4747 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4748 2ec1f75b 2019-03-26 stsp goto done;
4749 2ec1f75b 2019-03-26 stsp }
4750 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4751 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4752 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4753 2ec1f75b 2019-03-26 stsp goto done;
4754 2ec1f75b 2019-03-26 stsp }
4755 12463d8b 2019-12-13 stsp fd = -1;
4756 2ec1f75b 2019-03-26 stsp } else
4757 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4758 b72f483a 2019-02-05 stsp
4759 b72706c3 2022-06-01 stsp if (blob1) {
4760 b72706c3 2022-06-01 stsp f1 = got_opentemp();
4761 b72706c3 2022-06-01 stsp if (f1 == NULL) {
4762 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4763 b72706c3 2022-06-01 stsp goto done;
4764 b72706c3 2022-06-01 stsp }
4765 b72706c3 2022-06-01 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
4766 b72706c3 2022-06-01 stsp blob1);
4767 b72706c3 2022-06-01 stsp if (err)
4768 b72706c3 2022-06-01 stsp goto done;
4769 b72706c3 2022-06-01 stsp }
4770 b72706c3 2022-06-01 stsp
4771 b72706c3 2022-06-01 stsp err = got_diff_blob_file(blob1, f1, size1, label1, f2, sb.st_size,
4772 b72706c3 2022-06-01 stsp path, a->diff_context, a->ignore_whitespace, a->force_text_diff,
4773 b72706c3 2022-06-01 stsp stdout);
4774 b72f483a 2019-02-05 stsp done:
4775 b72f483a 2019-02-05 stsp if (blob1)
4776 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4777 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4778 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4779 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4780 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4781 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4782 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4783 b72f483a 2019-02-05 stsp free(abspath);
4784 927df6b7 2019-02-10 stsp return err;
4785 927df6b7 2019-02-10 stsp }
4786 927df6b7 2019-02-10 stsp
4787 927df6b7 2019-02-10 stsp static const struct got_error *
4788 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4789 b00d56cd 2018-04-01 stsp {
4790 b00d56cd 2018-04-01 stsp const struct got_error *error;
4791 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4792 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4793 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4794 67b631c9 2021-10-10 stsp const char *commit_args[2] = { NULL, NULL };
4795 67b631c9 2021-10-10 stsp int ncommit_args = 0;
4796 e7ffb0b0 2021-10-07 stsp struct got_object_id *ids[2] = { NULL, NULL };
4797 e7ffb0b0 2021-10-07 stsp char *labels[2] = { NULL, NULL };
4798 67b631c9 2021-10-10 stsp int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4799 e7ffb0b0 2021-10-07 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4800 e7ffb0b0 2021-10-07 stsp int force_text_diff = 0, force_path = 0, rflag = 0;
4801 c0cc5c62 2018-10-18 stsp const char *errstr;
4802 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4803 e7ffb0b0 2021-10-07 stsp struct got_pathlist_head paths;
4804 e7ffb0b0 2021-10-07 stsp struct got_pathlist_entry *pe;
4805 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
4806 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4807 b00d56cd 2018-04-01 stsp
4808 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4809 e7ffb0b0 2021-10-07 stsp TAILQ_INIT(&paths);
4810 84de9106 2020-12-26 stsp
4811 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4812 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4813 25eccc22 2019-01-04 stsp NULL) == -1)
4814 b00d56cd 2018-04-01 stsp err(1, "pledge");
4815 b00d56cd 2018-04-01 stsp #endif
4816 b00d56cd 2018-04-01 stsp
4817 67b631c9 2021-10-10 stsp while ((ch = getopt(argc, argv, "ac:C:r:swP")) != -1) {
4818 b00d56cd 2018-04-01 stsp switch (ch) {
4819 64453f7e 2020-11-21 stsp case 'a':
4820 64453f7e 2020-11-21 stsp force_text_diff = 1;
4821 64453f7e 2020-11-21 stsp break;
4822 67b631c9 2021-10-10 stsp case 'c':
4823 67b631c9 2021-10-10 stsp if (ncommit_args >= 2)
4824 67b631c9 2021-10-10 stsp errx(1, "too many -c options used");
4825 67b631c9 2021-10-10 stsp commit_args[ncommit_args++] = optarg;
4826 67b631c9 2021-10-10 stsp break;
4827 c0cc5c62 2018-10-18 stsp case 'C':
4828 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4829 dfcab68b 2019-11-29 kn &errstr);
4830 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4831 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4832 5a20d08d 2022-02-09 op errstr, optarg);
4833 c0cc5c62 2018-10-18 stsp break;
4834 b72f483a 2019-02-05 stsp case 'r':
4835 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4836 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4837 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4838 9ba1d308 2019-10-21 stsp optarg);
4839 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4840 e7ffb0b0 2021-10-07 stsp rflag = 1;
4841 b72f483a 2019-02-05 stsp break;
4842 98eaaa12 2019-08-03 stsp case 's':
4843 98eaaa12 2019-08-03 stsp diff_staged = 1;
4844 63035f9f 2019-10-06 stsp break;
4845 63035f9f 2019-10-06 stsp case 'w':
4846 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4847 98eaaa12 2019-08-03 stsp break;
4848 e7ffb0b0 2021-10-07 stsp case 'P':
4849 e7ffb0b0 2021-10-07 stsp force_path = 1;
4850 e7ffb0b0 2021-10-07 stsp break;
4851 b00d56cd 2018-04-01 stsp default:
4852 2deda0b9 2019-03-07 stsp usage_diff();
4853 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4854 b00d56cd 2018-04-01 stsp }
4855 b00d56cd 2018-04-01 stsp }
4856 b00d56cd 2018-04-01 stsp
4857 b00d56cd 2018-04-01 stsp argc -= optind;
4858 b00d56cd 2018-04-01 stsp argv += optind;
4859 b00d56cd 2018-04-01 stsp
4860 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4861 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4862 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4863 b72f483a 2019-02-05 stsp goto done;
4864 b72f483a 2019-02-05 stsp }
4865 e7ffb0b0 2021-10-07 stsp
4866 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4867 0ae84acc 2022-06-15 tracey if (error != NULL)
4868 0ae84acc 2022-06-15 tracey goto done;
4869 0ae84acc 2022-06-15 tracey
4870 e7ffb0b0 2021-10-07 stsp if (repo_path == NULL) {
4871 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4872 e7ffb0b0 2021-10-07 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4873 1f03b8da 2020-03-20 stsp goto done;
4874 e7ffb0b0 2021-10-07 stsp else
4875 e7ffb0b0 2021-10-07 stsp error = NULL;
4876 e7ffb0b0 2021-10-07 stsp if (worktree) {
4877 e7ffb0b0 2021-10-07 stsp repo_path =
4878 e7ffb0b0 2021-10-07 stsp strdup(got_worktree_get_repo_path(worktree));
4879 e7ffb0b0 2021-10-07 stsp if (repo_path == NULL) {
4880 e7ffb0b0 2021-10-07 stsp error = got_error_from_errno("strdup");
4881 927df6b7 2019-02-10 stsp goto done;
4882 e7ffb0b0 2021-10-07 stsp }
4883 927df6b7 2019-02-10 stsp } else {
4884 e7ffb0b0 2021-10-07 stsp repo_path = strdup(cwd);
4885 1a1242a9 2021-04-01 kn if (repo_path == NULL) {
4886 1a1242a9 2021-04-01 kn error = got_error_from_errno("strdup");
4887 1a1242a9 2021-04-01 kn goto done;
4888 30db809c 2019-06-05 stsp }
4889 30db809c 2019-06-05 stsp }
4890 e7ffb0b0 2021-10-07 stsp }
4891 b00d56cd 2018-04-01 stsp
4892 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4893 76089277 2018-04-01 stsp free(repo_path);
4894 b00d56cd 2018-04-01 stsp if (error != NULL)
4895 b00d56cd 2018-04-01 stsp goto done;
4896 67b631c9 2021-10-10 stsp
4897 67b631c9 2021-10-10 stsp if (rflag || worktree == NULL || ncommit_args > 0) {
4898 67b631c9 2021-10-10 stsp if (force_path) {
4899 67b631c9 2021-10-10 stsp error = got_error_msg(GOT_ERR_NOT_IMPL,
4900 67b631c9 2021-10-10 stsp "-P option can only be used when diffing "
4901 67b631c9 2021-10-10 stsp "a work tree");
4902 67b631c9 2021-10-10 stsp goto done;
4903 67b631c9 2021-10-10 stsp }
4904 67b631c9 2021-10-10 stsp if (diff_staged) {
4905 67b631c9 2021-10-10 stsp error = got_error_msg(GOT_ERR_NOT_IMPL,
4906 67b631c9 2021-10-10 stsp "-s option can only be used when diffing "
4907 67b631c9 2021-10-10 stsp "a work tree");
4908 67b631c9 2021-10-10 stsp goto done;
4909 67b631c9 2021-10-10 stsp }
4910 67b631c9 2021-10-10 stsp }
4911 b00d56cd 2018-04-01 stsp
4912 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4913 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4914 c02c541e 2019-03-29 stsp if (error)
4915 c02c541e 2019-03-29 stsp goto done;
4916 c02c541e 2019-03-29 stsp
4917 67b631c9 2021-10-10 stsp if ((!force_path && argc == 2) || ncommit_args > 0) {
4918 67b631c9 2021-10-10 stsp int obj_type = (ncommit_args > 0 ?
4919 67b631c9 2021-10-10 stsp GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
4920 e7ffb0b0 2021-10-07 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4921 e7ffb0b0 2021-10-07 stsp NULL);
4922 e7ffb0b0 2021-10-07 stsp if (error)
4923 e7ffb0b0 2021-10-07 stsp goto done;
4924 67b631c9 2021-10-10 stsp for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
4925 67b631c9 2021-10-10 stsp const char *arg;
4926 67b631c9 2021-10-10 stsp if (ncommit_args > 0)
4927 67b631c9 2021-10-10 stsp arg = commit_args[i];
4928 67b631c9 2021-10-10 stsp else
4929 67b631c9 2021-10-10 stsp arg = argv[i];
4930 e7ffb0b0 2021-10-07 stsp error = got_repo_match_object_id(&ids[i], &labels[i],
4931 67b631c9 2021-10-10 stsp arg, obj_type, &refs, repo);
4932 e7ffb0b0 2021-10-07 stsp if (error) {
4933 e7ffb0b0 2021-10-07 stsp if (error->code != GOT_ERR_NOT_REF &&
4934 e7ffb0b0 2021-10-07 stsp error->code != GOT_ERR_NO_OBJ)
4935 e7ffb0b0 2021-10-07 stsp goto done;
4936 67b631c9 2021-10-10 stsp if (ncommit_args > 0)
4937 67b631c9 2021-10-10 stsp goto done;
4938 e7ffb0b0 2021-10-07 stsp error = NULL;
4939 e7ffb0b0 2021-10-07 stsp break;
4940 e7ffb0b0 2021-10-07 stsp }
4941 e7ffb0b0 2021-10-07 stsp }
4942 e7ffb0b0 2021-10-07 stsp }
4943 e7ffb0b0 2021-10-07 stsp
4944 67b631c9 2021-10-10 stsp if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
4945 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4946 b72f483a 2019-02-05 stsp char *id_str;
4947 72ea6654 2019-07-27 stsp
4948 67b631c9 2021-10-10 stsp if (worktree == NULL) {
4949 67b631c9 2021-10-10 stsp if (argc == 2 && ids[0] == NULL) {
4950 67b631c9 2021-10-10 stsp error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
4951 67b631c9 2021-10-10 stsp goto done;
4952 67b631c9 2021-10-10 stsp } else if (argc == 2 && ids[1] == NULL) {
4953 67b631c9 2021-10-10 stsp error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
4954 67b631c9 2021-10-10 stsp goto done;
4955 67b631c9 2021-10-10 stsp } else if (argc > 0) {
4956 67b631c9 2021-10-10 stsp error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
4957 67b631c9 2021-10-10 stsp "%s", "specified paths cannot be resolved");
4958 67b631c9 2021-10-10 stsp goto done;
4959 67b631c9 2021-10-10 stsp } else {
4960 67b631c9 2021-10-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
4961 67b631c9 2021-10-10 stsp goto done;
4962 67b631c9 2021-10-10 stsp }
4963 67b631c9 2021-10-10 stsp }
4964 67b631c9 2021-10-10 stsp
4965 67b631c9 2021-10-10 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
4966 67b631c9 2021-10-10 stsp worktree);
4967 67b631c9 2021-10-10 stsp if (error)
4968 67b631c9 2021-10-10 stsp goto done;
4969 67b631c9 2021-10-10 stsp
4970 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4971 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4972 b72f483a 2019-02-05 stsp if (error)
4973 b72f483a 2019-02-05 stsp goto done;
4974 b72f483a 2019-02-05 stsp arg.repo = repo;
4975 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4976 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4977 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4978 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4979 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4980 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4981 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
4982 b72f483a 2019-02-05 stsp
4983 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
4984 f6343036 2021-06-22 stsp print_diff, &arg, check_cancelled, NULL);
4985 b72f483a 2019-02-05 stsp free(id_str);
4986 b72f483a 2019-02-05 stsp goto done;
4987 b72f483a 2019-02-05 stsp }
4988 84de9106 2020-12-26 stsp
4989 67b631c9 2021-10-10 stsp if (ncommit_args == 1) {
4990 67b631c9 2021-10-10 stsp struct got_commit_object *commit;
4991 67b631c9 2021-10-10 stsp error = got_object_open_as_commit(&commit, repo, ids[0]);
4992 67b631c9 2021-10-10 stsp if (error)
4993 e7ffb0b0 2021-10-07 stsp goto done;
4994 b00d56cd 2018-04-01 stsp
4995 67b631c9 2021-10-10 stsp labels[1] = labels[0];
4996 67b631c9 2021-10-10 stsp ids[1] = ids[0];
4997 67b631c9 2021-10-10 stsp if (got_object_commit_get_nparents(commit) > 0) {
4998 67b631c9 2021-10-10 stsp const struct got_object_id_queue *pids;
4999 67b631c9 2021-10-10 stsp struct got_object_qid *pid;
5000 67b631c9 2021-10-10 stsp pids = got_object_commit_get_parent_ids(commit);
5001 67b631c9 2021-10-10 stsp pid = STAILQ_FIRST(pids);
5002 d7b5a0e8 2022-04-20 stsp ids[0] = got_object_id_dup(&pid->id);
5003 67b631c9 2021-10-10 stsp if (ids[0] == NULL) {
5004 67b631c9 2021-10-10 stsp error = got_error_from_errno(
5005 67b631c9 2021-10-10 stsp "got_object_id_dup");
5006 67b631c9 2021-10-10 stsp got_object_commit_close(commit);
5007 67b631c9 2021-10-10 stsp goto done;
5008 67b631c9 2021-10-10 stsp }
5009 67b631c9 2021-10-10 stsp error = got_object_id_str(&labels[0], ids[0]);
5010 67b631c9 2021-10-10 stsp if (error) {
5011 67b631c9 2021-10-10 stsp got_object_commit_close(commit);
5012 67b631c9 2021-10-10 stsp goto done;
5013 67b631c9 2021-10-10 stsp }
5014 67b631c9 2021-10-10 stsp } else {
5015 67b631c9 2021-10-10 stsp ids[0] = NULL;
5016 67b631c9 2021-10-10 stsp labels[0] = strdup("/dev/null");
5017 67b631c9 2021-10-10 stsp if (labels[0] == NULL) {
5018 67b631c9 2021-10-10 stsp error = got_error_from_errno("strdup");
5019 67b631c9 2021-10-10 stsp got_object_commit_close(commit);
5020 67b631c9 2021-10-10 stsp goto done;
5021 67b631c9 2021-10-10 stsp }
5022 67b631c9 2021-10-10 stsp }
5023 67b631c9 2021-10-10 stsp
5024 67b631c9 2021-10-10 stsp got_object_commit_close(commit);
5025 67b631c9 2021-10-10 stsp }
5026 67b631c9 2021-10-10 stsp
5027 67b631c9 2021-10-10 stsp if (ncommit_args == 0 && argc > 2) {
5028 67b631c9 2021-10-10 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5029 67b631c9 2021-10-10 stsp "path arguments cannot be used when diffing two objects");
5030 0adb7196 2019-08-11 stsp goto done;
5031 67b631c9 2021-10-10 stsp }
5032 b00d56cd 2018-04-01 stsp
5033 67b631c9 2021-10-10 stsp if (ids[0]) {
5034 67b631c9 2021-10-10 stsp error = got_object_get_type(&type1, repo, ids[0]);
5035 67b631c9 2021-10-10 stsp if (error)
5036 67b631c9 2021-10-10 stsp goto done;
5037 67b631c9 2021-10-10 stsp }
5038 67b631c9 2021-10-10 stsp
5039 e7ffb0b0 2021-10-07 stsp error = got_object_get_type(&type2, repo, ids[1]);
5040 15a94983 2018-12-23 stsp if (error)
5041 15a94983 2018-12-23 stsp goto done;
5042 67b631c9 2021-10-10 stsp if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5043 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
5044 b00d56cd 2018-04-01 stsp goto done;
5045 b00d56cd 2018-04-01 stsp }
5046 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_BLOB && argc > 0) {
5047 67b631c9 2021-10-10 stsp error = got_error_msg(GOT_ERR_OBJ_TYPE,
5048 67b631c9 2021-10-10 stsp "path arguments cannot be used when diffing blobs");
5049 67b631c9 2021-10-10 stsp goto done;
5050 67b631c9 2021-10-10 stsp }
5051 b00d56cd 2018-04-01 stsp
5052 67b631c9 2021-10-10 stsp for (i = 0; ncommit_args > 0 && i < argc; i++) {
5053 67b631c9 2021-10-10 stsp char *in_repo_path;
5054 67b631c9 2021-10-10 stsp struct got_pathlist_entry *new;
5055 67b631c9 2021-10-10 stsp if (worktree) {
5056 67b631c9 2021-10-10 stsp const char *prefix;
5057 67b631c9 2021-10-10 stsp char *p;
5058 67b631c9 2021-10-10 stsp error = got_worktree_resolve_path(&p, worktree,
5059 67b631c9 2021-10-10 stsp argv[i]);
5060 67b631c9 2021-10-10 stsp if (error)
5061 67b631c9 2021-10-10 stsp goto done;
5062 67b631c9 2021-10-10 stsp prefix = got_worktree_get_path_prefix(worktree);
5063 67b631c9 2021-10-10 stsp while (prefix[0] == '/')
5064 67b631c9 2021-10-10 stsp prefix++;
5065 67b631c9 2021-10-10 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
5066 67b631c9 2021-10-10 stsp (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5067 67b631c9 2021-10-10 stsp p) == -1) {
5068 67b631c9 2021-10-10 stsp error = got_error_from_errno("asprintf");
5069 67b631c9 2021-10-10 stsp free(p);
5070 67b631c9 2021-10-10 stsp goto done;
5071 67b631c9 2021-10-10 stsp }
5072 67b631c9 2021-10-10 stsp free(p);
5073 67b631c9 2021-10-10 stsp } else {
5074 67b631c9 2021-10-10 stsp char *mapped_path, *s;
5075 67b631c9 2021-10-10 stsp error = got_repo_map_path(&mapped_path, repo, argv[i]);
5076 67b631c9 2021-10-10 stsp if (error)
5077 67b631c9 2021-10-10 stsp goto done;
5078 67b631c9 2021-10-10 stsp s = mapped_path;
5079 67b631c9 2021-10-10 stsp while (s[0] == '/')
5080 67b631c9 2021-10-10 stsp s++;
5081 67b631c9 2021-10-10 stsp in_repo_path = strdup(s);
5082 67b631c9 2021-10-10 stsp if (in_repo_path == NULL) {
5083 67b631c9 2021-10-10 stsp error = got_error_from_errno("asprintf");
5084 67b631c9 2021-10-10 stsp free(mapped_path);
5085 67b631c9 2021-10-10 stsp goto done;
5086 67b631c9 2021-10-10 stsp }
5087 67b631c9 2021-10-10 stsp free(mapped_path);
5088 67b631c9 2021-10-10 stsp
5089 67b631c9 2021-10-10 stsp }
5090 67b631c9 2021-10-10 stsp error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5091 67b631c9 2021-10-10 stsp if (error || new == NULL /* duplicate */)
5092 67b631c9 2021-10-10 stsp free(in_repo_path);
5093 67b631c9 2021-10-10 stsp if (error)
5094 67b631c9 2021-10-10 stsp goto done;
5095 f7fce2e2 2022-03-12 stsp }
5096 f7fce2e2 2022-03-12 stsp
5097 f7fce2e2 2022-03-12 stsp if (worktree) {
5098 f7fce2e2 2022-03-12 stsp /* Release work tree lock. */
5099 f7fce2e2 2022-03-12 stsp got_worktree_close(worktree);
5100 f7fce2e2 2022-03-12 stsp worktree = NULL;
5101 b72706c3 2022-06-01 stsp }
5102 b72706c3 2022-06-01 stsp
5103 b72706c3 2022-06-01 stsp f1 = got_opentemp();
5104 b72706c3 2022-06-01 stsp if (f1 == NULL) {
5105 b72706c3 2022-06-01 stsp error = got_error_from_errno("got_opentemp");
5106 b72706c3 2022-06-01 stsp goto done;
5107 b72706c3 2022-06-01 stsp }
5108 b72706c3 2022-06-01 stsp
5109 b72706c3 2022-06-01 stsp f2 = got_opentemp();
5110 b72706c3 2022-06-01 stsp if (f2 == NULL) {
5111 b72706c3 2022-06-01 stsp error = got_error_from_errno("got_opentemp");
5112 b72706c3 2022-06-01 stsp goto done;
5113 67b631c9 2021-10-10 stsp }
5114 67b631c9 2021-10-10 stsp
5115 67b631c9 2021-10-10 stsp switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5116 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
5117 b72706c3 2022-06-01 stsp error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5118 b72706c3 2022-06-01 stsp ids[0], ids[1], NULL, NULL, diff_context,
5119 b72706c3 2022-06-01 stsp ignore_whitespace, force_text_diff, repo, stdout);
5120 b00d56cd 2018-04-01 stsp break;
5121 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
5122 b72706c3 2022-06-01 stsp error = got_diff_objects_as_trees(NULL, NULL, f1, f2,
5123 b72706c3 2022-06-01 stsp ids[0], ids[1], &paths, "", "", diff_context,
5124 b72706c3 2022-06-01 stsp ignore_whitespace, force_text_diff, repo, stdout);
5125 b00d56cd 2018-04-01 stsp break;
5126 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
5127 e7ffb0b0 2021-10-07 stsp printf("diff %s %s\n", labels[0], labels[1]);
5128 b72706c3 2022-06-01 stsp error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5129 b72706c3 2022-06-01 stsp ids[0], ids[1], &paths, diff_context, ignore_whitespace,
5130 b72706c3 2022-06-01 stsp force_text_diff, repo, stdout);
5131 b00d56cd 2018-04-01 stsp break;
5132 b00d56cd 2018-04-01 stsp default:
5133 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
5134 b00d56cd 2018-04-01 stsp }
5135 b00d56cd 2018-04-01 stsp done:
5136 e7ffb0b0 2021-10-07 stsp free(labels[0]);
5137 e7ffb0b0 2021-10-07 stsp free(labels[1]);
5138 e7ffb0b0 2021-10-07 stsp free(ids[0]);
5139 e7ffb0b0 2021-10-07 stsp free(ids[1]);
5140 b72f483a 2019-02-05 stsp if (worktree)
5141 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
5142 ad242220 2018-09-08 stsp if (repo) {
5143 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5144 ad242220 2018-09-08 stsp if (error == NULL)
5145 1d0f4054 2021-06-17 stsp error = close_err;
5146 0ae84acc 2022-06-15 tracey }
5147 0ae84acc 2022-06-15 tracey if (pack_fds) {
5148 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5149 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5150 0ae84acc 2022-06-15 tracey if (error == NULL)
5151 0ae84acc 2022-06-15 tracey error = pack_err;
5152 ad242220 2018-09-08 stsp }
5153 e7ffb0b0 2021-10-07 stsp TAILQ_FOREACH(pe, &paths, entry)
5154 e7ffb0b0 2021-10-07 stsp free((char *)pe->path);
5155 e7ffb0b0 2021-10-07 stsp got_pathlist_free(&paths);
5156 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5157 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && error == NULL)
5158 b72706c3 2022-06-01 stsp error = got_error_from_errno("fclose");
5159 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && error == NULL)
5160 b72706c3 2022-06-01 stsp error = got_error_from_errno("fclose");
5161 b00d56cd 2018-04-01 stsp return error;
5162 404c43c4 2018-06-21 stsp }
5163 404c43c4 2018-06-21 stsp
5164 404c43c4 2018-06-21 stsp __dead static void
5165 404c43c4 2018-06-21 stsp usage_blame(void)
5166 404c43c4 2018-06-21 stsp {
5167 9270e621 2019-02-05 stsp fprintf(stderr,
5168 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5169 404c43c4 2018-06-21 stsp getprogname());
5170 404c43c4 2018-06-21 stsp exit(1);
5171 b00d56cd 2018-04-01 stsp }
5172 b00d56cd 2018-04-01 stsp
5173 28315671 2019-08-14 stsp struct blame_line {
5174 28315671 2019-08-14 stsp int annotated;
5175 28315671 2019-08-14 stsp char *id_str;
5176 82f6abb8 2019-08-14 stsp char *committer;
5177 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
5178 28315671 2019-08-14 stsp };
5179 28315671 2019-08-14 stsp
5180 28315671 2019-08-14 stsp struct blame_cb_args {
5181 28315671 2019-08-14 stsp struct blame_line *lines;
5182 28315671 2019-08-14 stsp int nlines;
5183 7ef28ff8 2019-08-14 stsp int nlines_prec;
5184 28315671 2019-08-14 stsp int lineno_cur;
5185 28315671 2019-08-14 stsp off_t *line_offsets;
5186 28315671 2019-08-14 stsp FILE *f;
5187 82f6abb8 2019-08-14 stsp struct got_repository *repo;
5188 28315671 2019-08-14 stsp };
5189 28315671 2019-08-14 stsp
5190 404c43c4 2018-06-21 stsp static const struct got_error *
5191 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5192 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5193 28315671 2019-08-14 stsp {
5194 28315671 2019-08-14 stsp const struct got_error *err = NULL;
5195 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
5196 28315671 2019-08-14 stsp struct blame_line *bline;
5197 82f6abb8 2019-08-14 stsp char *line = NULL;
5198 28315671 2019-08-14 stsp size_t linesize = 0;
5199 28315671 2019-08-14 stsp off_t offset;
5200 bcb49d15 2019-08-14 stsp struct tm tm;
5201 bcb49d15 2019-08-14 stsp time_t committer_time;
5202 28315671 2019-08-14 stsp
5203 28315671 2019-08-14 stsp if (nlines != a->nlines ||
5204 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5205 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
5206 28315671 2019-08-14 stsp
5207 28315671 2019-08-14 stsp if (sigint_received)
5208 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
5209 28315671 2019-08-14 stsp
5210 2839f8b9 2019-08-18 stsp if (lineno == -1)
5211 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
5212 2839f8b9 2019-08-18 stsp
5213 28315671 2019-08-14 stsp /* Annotate this line. */
5214 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
5215 28315671 2019-08-14 stsp if (bline->annotated)
5216 28315671 2019-08-14 stsp return NULL;
5217 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
5218 28315671 2019-08-14 stsp if (err)
5219 28315671 2019-08-14 stsp return err;
5220 82f6abb8 2019-08-14 stsp
5221 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
5222 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
5223 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
5224 bcb49d15 2019-08-14 stsp goto done;
5225 bcb49d15 2019-08-14 stsp }
5226 bcb49d15 2019-08-14 stsp
5227 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
5228 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
5229 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
5230 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5231 ec6d1a36 2021-03-21 jrick &tm) == 0) {
5232 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
5233 82f6abb8 2019-08-14 stsp goto done;
5234 82f6abb8 2019-08-14 stsp }
5235 28315671 2019-08-14 stsp bline->annotated = 1;
5236 28315671 2019-08-14 stsp
5237 28315671 2019-08-14 stsp /* Print lines annotated so far. */
5238 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
5239 28315671 2019-08-14 stsp if (!bline->annotated)
5240 82f6abb8 2019-08-14 stsp goto done;
5241 28315671 2019-08-14 stsp
5242 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
5243 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
5244 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
5245 82f6abb8 2019-08-14 stsp goto done;
5246 82f6abb8 2019-08-14 stsp }
5247 28315671 2019-08-14 stsp
5248 28315671 2019-08-14 stsp while (bline->annotated) {
5249 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
5250 82f6abb8 2019-08-14 stsp size_t len;
5251 82f6abb8 2019-08-14 stsp
5252 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
5253 28315671 2019-08-14 stsp if (ferror(a->f))
5254 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
5255 28315671 2019-08-14 stsp break;
5256 28315671 2019-08-14 stsp }
5257 82f6abb8 2019-08-14 stsp
5258 82f6abb8 2019-08-14 stsp committer = bline->committer;
5259 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
5260 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
5261 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
5262 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
5263 82f6abb8 2019-08-14 stsp if (at)
5264 82f6abb8 2019-08-14 stsp *at = '\0';
5265 82f6abb8 2019-08-14 stsp len = strlen(committer);
5266 82f6abb8 2019-08-14 stsp if (len >= 9)
5267 82f6abb8 2019-08-14 stsp committer[8] = '\0';
5268 28315671 2019-08-14 stsp
5269 28315671 2019-08-14 stsp nl = strchr(line, '\n');
5270 28315671 2019-08-14 stsp if (nl)
5271 28315671 2019-08-14 stsp *nl = '\0';
5272 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5273 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
5274 28315671 2019-08-14 stsp
5275 28315671 2019-08-14 stsp a->lineno_cur++;
5276 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
5277 28315671 2019-08-14 stsp }
5278 82f6abb8 2019-08-14 stsp done:
5279 28315671 2019-08-14 stsp free(line);
5280 28315671 2019-08-14 stsp return err;
5281 28315671 2019-08-14 stsp }
5282 28315671 2019-08-14 stsp
5283 28315671 2019-08-14 stsp static const struct got_error *
5284 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
5285 404c43c4 2018-06-21 stsp {
5286 404c43c4 2018-06-21 stsp const struct got_error *error;
5287 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
5288 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
5289 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5290 0587e10c 2020-07-23 stsp char *link_target = NULL;
5291 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
5292 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
5293 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5294 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
5295 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
5296 28315671 2019-08-14 stsp struct blame_cb_args bca;
5297 28315671 2019-08-14 stsp int ch, obj_type, i;
5298 be659d10 2020-11-18 stsp off_t filesize;
5299 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5300 90f3c347 2019-08-19 stsp
5301 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
5302 404c43c4 2018-06-21 stsp
5303 404c43c4 2018-06-21 stsp #ifndef PROFILE
5304 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5305 36e2fb66 2019-01-04 stsp NULL) == -1)
5306 404c43c4 2018-06-21 stsp err(1, "pledge");
5307 404c43c4 2018-06-21 stsp #endif
5308 404c43c4 2018-06-21 stsp
5309 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5310 404c43c4 2018-06-21 stsp switch (ch) {
5311 404c43c4 2018-06-21 stsp case 'c':
5312 404c43c4 2018-06-21 stsp commit_id_str = optarg;
5313 404c43c4 2018-06-21 stsp break;
5314 66bea077 2018-08-02 stsp case 'r':
5315 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
5316 66bea077 2018-08-02 stsp if (repo_path == NULL)
5317 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5318 9ba1d308 2019-10-21 stsp optarg);
5319 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5320 66bea077 2018-08-02 stsp break;
5321 404c43c4 2018-06-21 stsp default:
5322 2deda0b9 2019-03-07 stsp usage_blame();
5323 404c43c4 2018-06-21 stsp /* NOTREACHED */
5324 404c43c4 2018-06-21 stsp }
5325 404c43c4 2018-06-21 stsp }
5326 404c43c4 2018-06-21 stsp
5327 404c43c4 2018-06-21 stsp argc -= optind;
5328 404c43c4 2018-06-21 stsp argv += optind;
5329 404c43c4 2018-06-21 stsp
5330 a39318fd 2018-08-02 stsp if (argc == 1)
5331 404c43c4 2018-06-21 stsp path = argv[0];
5332 a39318fd 2018-08-02 stsp else
5333 404c43c4 2018-06-21 stsp usage_blame();
5334 404c43c4 2018-06-21 stsp
5335 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
5336 66bea077 2018-08-02 stsp if (cwd == NULL) {
5337 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5338 66bea077 2018-08-02 stsp goto done;
5339 66bea077 2018-08-02 stsp }
5340 0ae84acc 2022-06-15 tracey
5341 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5342 0ae84acc 2022-06-15 tracey if (error != NULL)
5343 0ae84acc 2022-06-15 tracey goto done;
5344 0ae84acc 2022-06-15 tracey
5345 66bea077 2018-08-02 stsp if (repo_path == NULL) {
5346 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5347 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5348 66bea077 2018-08-02 stsp goto done;
5349 0c06baac 2019-02-05 stsp else
5350 0c06baac 2019-02-05 stsp error = NULL;
5351 0c06baac 2019-02-05 stsp if (worktree) {
5352 0c06baac 2019-02-05 stsp repo_path =
5353 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5354 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
5355 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5356 1f03b8da 2020-03-20 stsp if (error)
5357 1f03b8da 2020-03-20 stsp goto done;
5358 1f03b8da 2020-03-20 stsp }
5359 0c06baac 2019-02-05 stsp } else {
5360 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
5361 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
5362 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5363 0c06baac 2019-02-05 stsp goto done;
5364 0c06baac 2019-02-05 stsp }
5365 66bea077 2018-08-02 stsp }
5366 66bea077 2018-08-02 stsp }
5367 36e2fb66 2019-01-04 stsp
5368 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5369 c02c541e 2019-03-29 stsp if (error != NULL)
5370 404c43c4 2018-06-21 stsp goto done;
5371 404c43c4 2018-06-21 stsp
5372 0c06baac 2019-02-05 stsp if (worktree) {
5373 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
5374 01740607 2020-11-04 stsp char *p;
5375 01740607 2020-11-04 stsp
5376 01740607 2020-11-04 stsp error = got_worktree_resolve_path(&p, worktree, path);
5377 01740607 2020-11-04 stsp if (error)
5378 01740607 2020-11-04 stsp goto done;
5379 01740607 2020-11-04 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
5380 01740607 2020-11-04 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5381 01740607 2020-11-04 stsp p) == -1) {
5382 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
5383 01740607 2020-11-04 stsp free(p);
5384 0c06baac 2019-02-05 stsp goto done;
5385 0c06baac 2019-02-05 stsp }
5386 0c06baac 2019-02-05 stsp free(p);
5387 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5388 0c06baac 2019-02-05 stsp } else {
5389 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5390 01740607 2020-11-04 stsp if (error)
5391 01740607 2020-11-04 stsp goto done;
5392 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
5393 0c06baac 2019-02-05 stsp }
5394 0c06baac 2019-02-05 stsp if (error)
5395 66bea077 2018-08-02 stsp goto done;
5396 66bea077 2018-08-02 stsp
5397 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
5398 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
5399 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5400 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5401 404c43c4 2018-06-21 stsp if (error != NULL)
5402 66bea077 2018-08-02 stsp goto done;
5403 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5404 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
5405 404c43c4 2018-06-21 stsp if (error != NULL)
5406 66bea077 2018-08-02 stsp goto done;
5407 404c43c4 2018-06-21 stsp } else {
5408 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5409 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5410 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5411 84de9106 2020-12-26 stsp NULL);
5412 84de9106 2020-12-26 stsp if (error)
5413 84de9106 2020-12-26 stsp goto done;
5414 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5415 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5416 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5417 30837e32 2019-07-25 stsp if (error)
5418 66bea077 2018-08-02 stsp goto done;
5419 f7fce2e2 2022-03-12 stsp }
5420 f7fce2e2 2022-03-12 stsp
5421 f7fce2e2 2022-03-12 stsp if (worktree) {
5422 f7fce2e2 2022-03-12 stsp /* Release work tree lock. */
5423 f7fce2e2 2022-03-12 stsp got_worktree_close(worktree);
5424 f7fce2e2 2022-03-12 stsp worktree = NULL;
5425 404c43c4 2018-06-21 stsp }
5426 404c43c4 2018-06-21 stsp
5427 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5428 a44927cc 2022-04-07 stsp if (error)
5429 a44927cc 2022-04-07 stsp goto done;
5430 a44927cc 2022-04-07 stsp
5431 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5432 a44927cc 2022-04-07 stsp commit, repo);
5433 0587e10c 2020-07-23 stsp if (error)
5434 0587e10c 2020-07-23 stsp goto done;
5435 0587e10c 2020-07-23 stsp
5436 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&obj_id, repo, commit,
5437 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
5438 28315671 2019-08-14 stsp if (error)
5439 28315671 2019-08-14 stsp goto done;
5440 28315671 2019-08-14 stsp
5441 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
5442 28315671 2019-08-14 stsp if (error)
5443 28315671 2019-08-14 stsp goto done;
5444 28315671 2019-08-14 stsp
5445 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5446 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
5447 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
5448 28315671 2019-08-14 stsp goto done;
5449 28315671 2019-08-14 stsp }
5450 28315671 2019-08-14 stsp
5451 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
5452 28315671 2019-08-14 stsp if (error)
5453 28315671 2019-08-14 stsp goto done;
5454 28315671 2019-08-14 stsp bca.f = got_opentemp();
5455 28315671 2019-08-14 stsp if (bca.f == NULL) {
5456 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
5457 28315671 2019-08-14 stsp goto done;
5458 28315671 2019-08-14 stsp }
5459 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5460 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
5461 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
5462 28315671 2019-08-14 stsp goto done;
5463 28315671 2019-08-14 stsp
5464 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5465 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
5466 b02560ec 2019-08-19 stsp bca.nlines--;
5467 b02560ec 2019-08-19 stsp
5468 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5469 28315671 2019-08-14 stsp if (bca.lines == NULL) {
5470 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
5471 28315671 2019-08-14 stsp goto done;
5472 28315671 2019-08-14 stsp }
5473 28315671 2019-08-14 stsp bca.lineno_cur = 1;
5474 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
5475 7ef28ff8 2019-08-14 stsp i = bca.nlines;
5476 7ef28ff8 2019-08-14 stsp while (i > 0) {
5477 7ef28ff8 2019-08-14 stsp i /= 10;
5478 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
5479 7ef28ff8 2019-08-14 stsp }
5480 82f6abb8 2019-08-14 stsp bca.repo = repo;
5481 7ef28ff8 2019-08-14 stsp
5482 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5483 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
5484 404c43c4 2018-06-21 stsp done:
5485 66bea077 2018-08-02 stsp free(in_repo_path);
5486 0587e10c 2020-07-23 stsp free(link_target);
5487 66bea077 2018-08-02 stsp free(repo_path);
5488 66bea077 2018-08-02 stsp free(cwd);
5489 404c43c4 2018-06-21 stsp free(commit_id);
5490 28315671 2019-08-14 stsp free(obj_id);
5491 a44927cc 2022-04-07 stsp if (commit)
5492 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5493 28315671 2019-08-14 stsp if (blob)
5494 28315671 2019-08-14 stsp got_object_blob_close(blob);
5495 0c06baac 2019-02-05 stsp if (worktree)
5496 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
5497 ad242220 2018-09-08 stsp if (repo) {
5498 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5499 ad242220 2018-09-08 stsp if (error == NULL)
5500 1d0f4054 2021-06-17 stsp error = close_err;
5501 ad242220 2018-09-08 stsp }
5502 0ae84acc 2022-06-15 tracey if (pack_fds) {
5503 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5504 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5505 0ae84acc 2022-06-15 tracey if (error == NULL)
5506 0ae84acc 2022-06-15 tracey error = pack_err;
5507 0ae84acc 2022-06-15 tracey }
5508 3affba96 2019-09-22 stsp if (bca.lines) {
5509 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
5510 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
5511 3affba96 2019-09-22 stsp free(bline->id_str);
5512 3affba96 2019-09-22 stsp free(bline->committer);
5513 3affba96 2019-09-22 stsp }
5514 3affba96 2019-09-22 stsp free(bca.lines);
5515 28315671 2019-08-14 stsp }
5516 28315671 2019-08-14 stsp free(bca.line_offsets);
5517 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
5518 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
5519 404c43c4 2018-06-21 stsp return error;
5520 5de5890b 2018-10-18 stsp }
5521 5de5890b 2018-10-18 stsp
5522 5de5890b 2018-10-18 stsp __dead static void
5523 5de5890b 2018-10-18 stsp usage_tree(void)
5524 5de5890b 2018-10-18 stsp {
5525 c1669e2e 2019-01-09 stsp fprintf(stderr,
5526 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5527 5de5890b 2018-10-18 stsp getprogname());
5528 5de5890b 2018-10-18 stsp exit(1);
5529 5de5890b 2018-10-18 stsp }
5530 5de5890b 2018-10-18 stsp
5531 0d6c6ee3 2020-05-20 stsp static const struct got_error *
5532 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
5533 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
5534 c1669e2e 2019-01-09 stsp {
5535 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
5536 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
5537 848d6979 2019-08-12 stsp const char *modestr = "";
5538 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
5539 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
5540 5de5890b 2018-10-18 stsp
5541 c1669e2e 2019-01-09 stsp path += strlen(root_path);
5542 c1669e2e 2019-01-09 stsp while (path[0] == '/')
5543 c1669e2e 2019-01-09 stsp path++;
5544 c1669e2e 2019-01-09 stsp
5545 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5546 63c5ca5d 2019-08-24 stsp modestr = "$";
5547 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5548 0d6c6ee3 2020-05-20 stsp int i;
5549 0d6c6ee3 2020-05-20 stsp
5550 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5551 0d6c6ee3 2020-05-20 stsp if (err)
5552 0d6c6ee3 2020-05-20 stsp return err;
5553 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5554 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5555 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5556 0d6c6ee3 2020-05-20 stsp }
5557 0d6c6ee3 2020-05-20 stsp
5558 848d6979 2019-08-12 stsp modestr = "@";
5559 0d6c6ee3 2020-05-20 stsp }
5560 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5561 848d6979 2019-08-12 stsp modestr = "/";
5562 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5563 848d6979 2019-08-12 stsp modestr = "*";
5564 848d6979 2019-08-12 stsp
5565 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5566 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5567 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
5568 0d6c6ee3 2020-05-20 stsp
5569 0d6c6ee3 2020-05-20 stsp free(link_target);
5570 0d6c6ee3 2020-05-20 stsp return NULL;
5571 c1669e2e 2019-01-09 stsp }
5572 c1669e2e 2019-01-09 stsp
5573 5de5890b 2018-10-18 stsp static const struct got_error *
5574 a44927cc 2022-04-07 stsp print_tree(const char *path, struct got_commit_object *commit,
5575 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
5576 c1669e2e 2019-01-09 stsp struct got_repository *repo)
5577 5de5890b 2018-10-18 stsp {
5578 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
5579 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
5580 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
5581 56e0773d 2019-11-28 stsp int nentries, i;
5582 5de5890b 2018-10-18 stsp
5583 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit, path);
5584 5de5890b 2018-10-18 stsp if (err)
5585 5de5890b 2018-10-18 stsp goto done;
5586 5de5890b 2018-10-18 stsp
5587 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
5588 5de5890b 2018-10-18 stsp if (err)
5589 5de5890b 2018-10-18 stsp goto done;
5590 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
5591 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
5592 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5593 5de5890b 2018-10-18 stsp char *id = NULL;
5594 84453469 2018-11-11 stsp
5595 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
5596 84453469 2018-11-11 stsp break;
5597 84453469 2018-11-11 stsp
5598 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
5599 5de5890b 2018-10-18 stsp if (show_ids) {
5600 5de5890b 2018-10-18 stsp char *id_str;
5601 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5602 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5603 5de5890b 2018-10-18 stsp if (err)
5604 5de5890b 2018-10-18 stsp goto done;
5605 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
5606 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5607 5de5890b 2018-10-18 stsp free(id_str);
5608 5de5890b 2018-10-18 stsp goto done;
5609 5de5890b 2018-10-18 stsp }
5610 5de5890b 2018-10-18 stsp free(id_str);
5611 5de5890b 2018-10-18 stsp }
5612 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
5613 5de5890b 2018-10-18 stsp free(id);
5614 0d6c6ee3 2020-05-20 stsp if (err)
5615 0d6c6ee3 2020-05-20 stsp goto done;
5616 c1669e2e 2019-01-09 stsp
5617 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5618 c1669e2e 2019-01-09 stsp char *child_path;
5619 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
5620 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
5621 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
5622 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5623 c1669e2e 2019-01-09 stsp goto done;
5624 c1669e2e 2019-01-09 stsp }
5625 a44927cc 2022-04-07 stsp err = print_tree(child_path, commit, show_ids, 1,
5626 c1669e2e 2019-01-09 stsp root_path, repo);
5627 c1669e2e 2019-01-09 stsp free(child_path);
5628 c1669e2e 2019-01-09 stsp if (err)
5629 c1669e2e 2019-01-09 stsp goto done;
5630 c1669e2e 2019-01-09 stsp }
5631 5de5890b 2018-10-18 stsp }
5632 5de5890b 2018-10-18 stsp done:
5633 5de5890b 2018-10-18 stsp if (tree)
5634 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
5635 5de5890b 2018-10-18 stsp free(tree_id);
5636 5de5890b 2018-10-18 stsp return err;
5637 404c43c4 2018-06-21 stsp }
5638 404c43c4 2018-06-21 stsp
5639 5de5890b 2018-10-18 stsp static const struct got_error *
5640 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
5641 5de5890b 2018-10-18 stsp {
5642 5de5890b 2018-10-18 stsp const struct got_error *error;
5643 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
5644 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
5645 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
5646 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5647 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
5648 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5649 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
5650 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
5651 5de5890b 2018-10-18 stsp int ch;
5652 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5653 5de5890b 2018-10-18 stsp
5654 5de5890b 2018-10-18 stsp #ifndef PROFILE
5655 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5656 0f8d269b 2019-01-04 stsp NULL) == -1)
5657 5de5890b 2018-10-18 stsp err(1, "pledge");
5658 5de5890b 2018-10-18 stsp #endif
5659 5de5890b 2018-10-18 stsp
5660 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5661 5de5890b 2018-10-18 stsp switch (ch) {
5662 5de5890b 2018-10-18 stsp case 'c':
5663 5de5890b 2018-10-18 stsp commit_id_str = optarg;
5664 5de5890b 2018-10-18 stsp break;
5665 5de5890b 2018-10-18 stsp case 'r':
5666 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
5667 5de5890b 2018-10-18 stsp if (repo_path == NULL)
5668 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5669 9ba1d308 2019-10-21 stsp optarg);
5670 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5671 5de5890b 2018-10-18 stsp break;
5672 5de5890b 2018-10-18 stsp case 'i':
5673 5de5890b 2018-10-18 stsp show_ids = 1;
5674 5de5890b 2018-10-18 stsp break;
5675 c1669e2e 2019-01-09 stsp case 'R':
5676 c1669e2e 2019-01-09 stsp recurse = 1;
5677 c1669e2e 2019-01-09 stsp break;
5678 5de5890b 2018-10-18 stsp default:
5679 2deda0b9 2019-03-07 stsp usage_tree();
5680 5de5890b 2018-10-18 stsp /* NOTREACHED */
5681 5de5890b 2018-10-18 stsp }
5682 5de5890b 2018-10-18 stsp }
5683 5de5890b 2018-10-18 stsp
5684 5de5890b 2018-10-18 stsp argc -= optind;
5685 5de5890b 2018-10-18 stsp argv += optind;
5686 5de5890b 2018-10-18 stsp
5687 5de5890b 2018-10-18 stsp if (argc == 1)
5688 5de5890b 2018-10-18 stsp path = argv[0];
5689 5de5890b 2018-10-18 stsp else if (argc > 1)
5690 5de5890b 2018-10-18 stsp usage_tree();
5691 5de5890b 2018-10-18 stsp else
5692 7a2c19d6 2019-02-05 stsp path = NULL;
5693 7a2c19d6 2019-02-05 stsp
5694 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
5695 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
5696 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5697 9bf7a39b 2019-02-05 stsp goto done;
5698 9bf7a39b 2019-02-05 stsp }
5699 0ae84acc 2022-06-15 tracey
5700 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5701 0ae84acc 2022-06-15 tracey if (error != NULL)
5702 0ae84acc 2022-06-15 tracey goto done;
5703 0ae84acc 2022-06-15 tracey
5704 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
5705 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5706 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5707 7a2c19d6 2019-02-05 stsp goto done;
5708 7a2c19d6 2019-02-05 stsp else
5709 7a2c19d6 2019-02-05 stsp error = NULL;
5710 7a2c19d6 2019-02-05 stsp if (worktree) {
5711 7a2c19d6 2019-02-05 stsp repo_path =
5712 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5713 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
5714 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5715 7a2c19d6 2019-02-05 stsp if (error)
5716 7a2c19d6 2019-02-05 stsp goto done;
5717 7a2c19d6 2019-02-05 stsp } else {
5718 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
5719 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
5720 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5721 7a2c19d6 2019-02-05 stsp goto done;
5722 7a2c19d6 2019-02-05 stsp }
5723 5de5890b 2018-10-18 stsp }
5724 5de5890b 2018-10-18 stsp }
5725 5de5890b 2018-10-18 stsp
5726 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5727 5de5890b 2018-10-18 stsp if (error != NULL)
5728 c02c541e 2019-03-29 stsp goto done;
5729 c02c541e 2019-03-29 stsp
5730 4fedbf4c 2020-11-07 stsp if (worktree) {
5731 4fedbf4c 2020-11-07 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
5732 4fedbf4c 2020-11-07 stsp char *p;
5733 5de5890b 2018-10-18 stsp
5734 4fedbf4c 2020-11-07 stsp if (path == NULL)
5735 4fedbf4c 2020-11-07 stsp path = "";
5736 4fedbf4c 2020-11-07 stsp error = got_worktree_resolve_path(&p, worktree, path);
5737 4fedbf4c 2020-11-07 stsp if (error)
5738 4fedbf4c 2020-11-07 stsp goto done;
5739 4fedbf4c 2020-11-07 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
5740 4fedbf4c 2020-11-07 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5741 4fedbf4c 2020-11-07 stsp p) == -1) {
5742 4fedbf4c 2020-11-07 stsp error = got_error_from_errno("asprintf");
5743 9bf7a39b 2019-02-05 stsp free(p);
5744 4fedbf4c 2020-11-07 stsp goto done;
5745 4fedbf4c 2020-11-07 stsp }
5746 4fedbf4c 2020-11-07 stsp free(p);
5747 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5748 4fedbf4c 2020-11-07 stsp if (error)
5749 4fedbf4c 2020-11-07 stsp goto done;
5750 4fedbf4c 2020-11-07 stsp } else {
5751 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5752 4fedbf4c 2020-11-07 stsp if (error)
5753 4fedbf4c 2020-11-07 stsp goto done;
5754 4fedbf4c 2020-11-07 stsp if (path == NULL)
5755 9bf7a39b 2019-02-05 stsp path = "/";
5756 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
5757 9bf7a39b 2019-02-05 stsp if (error != NULL)
5758 9bf7a39b 2019-02-05 stsp goto done;
5759 9bf7a39b 2019-02-05 stsp }
5760 5de5890b 2018-10-18 stsp
5761 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
5762 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
5763 4e0a20a4 2020-03-23 tracey if (worktree)
5764 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
5765 4e0a20a4 2020-03-23 tracey else
5766 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
5767 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
5768 5de5890b 2018-10-18 stsp if (error != NULL)
5769 5de5890b 2018-10-18 stsp goto done;
5770 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5771 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
5772 5de5890b 2018-10-18 stsp if (error != NULL)
5773 5de5890b 2018-10-18 stsp goto done;
5774 5de5890b 2018-10-18 stsp } else {
5775 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5776 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5777 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5778 84de9106 2020-12-26 stsp NULL);
5779 84de9106 2020-12-26 stsp if (error)
5780 84de9106 2020-12-26 stsp goto done;
5781 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5782 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5783 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5784 30837e32 2019-07-25 stsp if (error)
5785 5de5890b 2018-10-18 stsp goto done;
5786 5de5890b 2018-10-18 stsp }
5787 5de5890b 2018-10-18 stsp
5788 f7fce2e2 2022-03-12 stsp if (worktree) {
5789 f7fce2e2 2022-03-12 stsp /* Release work tree lock. */
5790 f7fce2e2 2022-03-12 stsp got_worktree_close(worktree);
5791 f7fce2e2 2022-03-12 stsp worktree = NULL;
5792 f7fce2e2 2022-03-12 stsp }
5793 f7fce2e2 2022-03-12 stsp
5794 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5795 a44927cc 2022-04-07 stsp if (error)
5796 a44927cc 2022-04-07 stsp goto done;
5797 a44927cc 2022-04-07 stsp
5798 a44927cc 2022-04-07 stsp error = print_tree(in_repo_path, commit, show_ids, recurse,
5799 c1669e2e 2019-01-09 stsp in_repo_path, repo);
5800 5de5890b 2018-10-18 stsp done:
5801 5de5890b 2018-10-18 stsp free(in_repo_path);
5802 5de5890b 2018-10-18 stsp free(repo_path);
5803 5de5890b 2018-10-18 stsp free(cwd);
5804 5de5890b 2018-10-18 stsp free(commit_id);
5805 a44927cc 2022-04-07 stsp if (commit)
5806 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5807 7a2c19d6 2019-02-05 stsp if (worktree)
5808 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
5809 5de5890b 2018-10-18 stsp if (repo) {
5810 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5811 5de5890b 2018-10-18 stsp if (error == NULL)
5812 1d0f4054 2021-06-17 stsp error = close_err;
5813 0ae84acc 2022-06-15 tracey }
5814 0ae84acc 2022-06-15 tracey if (pack_fds) {
5815 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5816 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5817 0ae84acc 2022-06-15 tracey if (error == NULL)
5818 0ae84acc 2022-06-15 tracey error = pack_err;
5819 5de5890b 2018-10-18 stsp }
5820 5de5890b 2018-10-18 stsp return error;
5821 5de5890b 2018-10-18 stsp }
5822 5de5890b 2018-10-18 stsp
5823 6bad629b 2019-02-04 stsp __dead static void
5824 6bad629b 2019-02-04 stsp usage_status(void)
5825 6bad629b 2019-02-04 stsp {
5826 00357e4d 2021-09-14 tracey fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5827 00357e4d 2021-09-14 tracey "[-S status-codes] [path ...]\n", getprogname());
5828 6bad629b 2019-02-04 stsp exit(1);
5829 6bad629b 2019-02-04 stsp }
5830 00357e4d 2021-09-14 tracey
5831 00357e4d 2021-09-14 tracey struct got_status_arg {
5832 00357e4d 2021-09-14 tracey char *status_codes;
5833 00357e4d 2021-09-14 tracey int suppress;
5834 00357e4d 2021-09-14 tracey };
5835 5c860e29 2018-03-12 stsp
5836 b72f483a 2019-02-05 stsp static const struct got_error *
5837 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
5838 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5839 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5840 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5841 6bad629b 2019-02-04 stsp {
5842 00357e4d 2021-09-14 tracey struct got_status_arg *st = arg;
5843 00357e4d 2021-09-14 tracey
5844 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5845 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
5846 00357e4d 2021-09-14 tracey if (st != NULL && st->status_codes) {
5847 00357e4d 2021-09-14 tracey size_t ncodes = strlen(st->status_codes);
5848 00357e4d 2021-09-14 tracey int i, j = 0;
5849 00357e4d 2021-09-14 tracey
5850 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
5851 00357e4d 2021-09-14 tracey if (st->suppress) {
5852 00357e4d 2021-09-14 tracey if (status == st->status_codes[i] ||
5853 00357e4d 2021-09-14 tracey staged_status == st->status_codes[i]) {
5854 00357e4d 2021-09-14 tracey j++;
5855 00357e4d 2021-09-14 tracey continue;
5856 00357e4d 2021-09-14 tracey }
5857 00357e4d 2021-09-14 tracey } else {
5858 00357e4d 2021-09-14 tracey if (status == st->status_codes[i] ||
5859 00357e4d 2021-09-14 tracey staged_status == st->status_codes[i])
5860 00357e4d 2021-09-14 tracey break;
5861 00357e4d 2021-09-14 tracey }
5862 081470ac 2020-08-13 stsp }
5863 00357e4d 2021-09-14 tracey
5864 00357e4d 2021-09-14 tracey if (st->suppress && j == 0)
5865 00357e4d 2021-09-14 tracey goto print;
5866 00357e4d 2021-09-14 tracey
5867 081470ac 2020-08-13 stsp if (i == ncodes)
5868 081470ac 2020-08-13 stsp return NULL;
5869 081470ac 2020-08-13 stsp }
5870 00357e4d 2021-09-14 tracey print:
5871 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
5872 b72f483a 2019-02-05 stsp return NULL;
5873 6bad629b 2019-02-04 stsp }
5874 5c860e29 2018-03-12 stsp
5875 6bad629b 2019-02-04 stsp static const struct got_error *
5876 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
5877 6bad629b 2019-02-04 stsp {
5878 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
5879 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
5880 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
5881 00357e4d 2021-09-14 tracey struct got_status_arg st;
5882 00357e4d 2021-09-14 tracey char *cwd = NULL;
5883 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
5884 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
5885 f6343036 2021-06-22 stsp int ch, i, no_ignores = 0;
5886 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5887 5c860e29 2018-03-12 stsp
5888 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
5889 72ea6654 2019-07-27 stsp
5890 00357e4d 2021-09-14 tracey memset(&st, 0, sizeof(st));
5891 00357e4d 2021-09-14 tracey st.status_codes = NULL;
5892 00357e4d 2021-09-14 tracey st.suppress = 0;
5893 00357e4d 2021-09-14 tracey
5894 00357e4d 2021-09-14 tracey while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
5895 6bad629b 2019-02-04 stsp switch (ch) {
5896 f6343036 2021-06-22 stsp case 'I':
5897 f6343036 2021-06-22 stsp no_ignores = 1;
5898 f6343036 2021-06-22 stsp break;
5899 788d4a19 2021-09-14 stsp case 'S':
5900 788d4a19 2021-09-14 stsp if (st.status_codes != NULL && st.suppress == 0)
5901 788d4a19 2021-09-14 stsp option_conflict('S', 's');
5902 788d4a19 2021-09-14 stsp st.suppress = 1;
5903 788d4a19 2021-09-14 stsp /* fallthrough */
5904 081470ac 2020-08-13 stsp case 's':
5905 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
5906 081470ac 2020-08-13 stsp switch (optarg[i]) {
5907 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
5908 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
5909 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
5910 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
5911 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
5912 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
5913 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
5914 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
5915 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
5916 081470ac 2020-08-13 stsp break;
5917 081470ac 2020-08-13 stsp default:
5918 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
5919 081470ac 2020-08-13 stsp optarg[i]);
5920 081470ac 2020-08-13 stsp }
5921 081470ac 2020-08-13 stsp }
5922 788d4a19 2021-09-14 stsp if (ch == 's' && st.suppress)
5923 b043307b 2021-09-14 stsp option_conflict('s', 'S');
5924 00357e4d 2021-09-14 tracey st.status_codes = optarg;
5925 081470ac 2020-08-13 stsp break;
5926 5c860e29 2018-03-12 stsp default:
5927 2deda0b9 2019-03-07 stsp usage_status();
5928 6bad629b 2019-02-04 stsp /* NOTREACHED */
5929 5c860e29 2018-03-12 stsp }
5930 5c860e29 2018-03-12 stsp }
5931 5c860e29 2018-03-12 stsp
5932 6bad629b 2019-02-04 stsp argc -= optind;
5933 6bad629b 2019-02-04 stsp argv += optind;
5934 5c860e29 2018-03-12 stsp
5935 6bad629b 2019-02-04 stsp #ifndef PROFILE
5936 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5937 6bad629b 2019-02-04 stsp NULL) == -1)
5938 6bad629b 2019-02-04 stsp err(1, "pledge");
5939 f42b1b34 2018-03-12 stsp #endif
5940 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
5941 927df6b7 2019-02-10 stsp if (cwd == NULL) {
5942 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5943 927df6b7 2019-02-10 stsp goto done;
5944 927df6b7 2019-02-10 stsp }
5945 927df6b7 2019-02-10 stsp
5946 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5947 0ae84acc 2022-06-15 tracey if (error != NULL)
5948 0ae84acc 2022-06-15 tracey goto done;
5949 0ae84acc 2022-06-15 tracey
5950 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
5951 fa51e947 2020-03-27 stsp if (error) {
5952 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5953 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
5954 a5edda0a 2019-07-27 stsp goto done;
5955 fa51e947 2020-03-27 stsp }
5956 6bad629b 2019-02-04 stsp
5957 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5958 0ae84acc 2022-06-15 tracey NULL, pack_fds);
5959 6bad629b 2019-02-04 stsp if (error != NULL)
5960 6bad629b 2019-02-04 stsp goto done;
5961 6bad629b 2019-02-04 stsp
5962 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5963 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5964 087fb88c 2019-08-04 stsp if (error)
5965 087fb88c 2019-08-04 stsp goto done;
5966 087fb88c 2019-08-04 stsp
5967 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5968 6bad629b 2019-02-04 stsp if (error)
5969 6bad629b 2019-02-04 stsp goto done;
5970 6bad629b 2019-02-04 stsp
5971 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, no_ignores,
5972 00357e4d 2021-09-14 tracey print_status, &st, check_cancelled, NULL);
5973 6bad629b 2019-02-04 stsp done:
5974 0ae84acc 2022-06-15 tracey if (pack_fds) {
5975 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5976 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5977 0ae84acc 2022-06-15 tracey if (error == NULL)
5978 0ae84acc 2022-06-15 tracey error = pack_err;
5979 0ae84acc 2022-06-15 tracey }
5980 0ae84acc 2022-06-15 tracey
5981 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
5982 a5edda0a 2019-07-27 stsp free((char *)pe->path);
5983 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
5984 927df6b7 2019-02-10 stsp free(cwd);
5985 d0eebce4 2019-03-11 stsp return error;
5986 d0eebce4 2019-03-11 stsp }
5987 d0eebce4 2019-03-11 stsp
5988 d0eebce4 2019-03-11 stsp __dead static void
5989 d0eebce4 2019-03-11 stsp usage_ref(void)
5990 d0eebce4 2019-03-11 stsp {
5991 d0eebce4 2019-03-11 stsp fprintf(stderr,
5992 0f104432 2021-11-20 stsp "usage: %s ref [-r repository] [-l] [-t] [-c object] "
5993 0f104432 2021-11-20 stsp "[-s reference] [-d] [name]\n",
5994 d0eebce4 2019-03-11 stsp getprogname());
5995 d0eebce4 2019-03-11 stsp exit(1);
5996 d0eebce4 2019-03-11 stsp }
5997 d0eebce4 2019-03-11 stsp
5998 d0eebce4 2019-03-11 stsp static const struct got_error *
5999 0f104432 2021-11-20 stsp list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6000 d0eebce4 2019-03-11 stsp {
6001 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
6002 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
6003 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
6004 d0eebce4 2019-03-11 stsp
6005 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6006 0f104432 2021-11-20 stsp err = got_ref_list(&refs, repo, refname, sort_by_time ?
6007 0f104432 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6008 0f104432 2021-11-20 stsp repo);
6009 d0eebce4 2019-03-11 stsp if (err)
6010 d0eebce4 2019-03-11 stsp return err;
6011 d0eebce4 2019-03-11 stsp
6012 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
6013 d0eebce4 2019-03-11 stsp char *refstr;
6014 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
6015 3e99fbc5 2022-03-23 op if (refstr == NULL) {
6016 3e99fbc5 2022-03-23 op err = got_error_from_errno("got_ref_to_str");
6017 3e99fbc5 2022-03-23 op break;
6018 3e99fbc5 2022-03-23 op }
6019 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6020 d0eebce4 2019-03-11 stsp free(refstr);
6021 d0eebce4 2019-03-11 stsp }
6022 d0eebce4 2019-03-11 stsp
6023 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
6024 3e99fbc5 2022-03-23 op return err;
6025 d0eebce4 2019-03-11 stsp }
6026 d0eebce4 2019-03-11 stsp
6027 d0eebce4 2019-03-11 stsp static const struct got_error *
6028 161728eb 2021-07-24 stsp delete_ref_by_name(struct got_repository *repo, const char *refname)
6029 d0eebce4 2019-03-11 stsp {
6030 161728eb 2021-07-24 stsp const struct got_error *err;
6031 d0eebce4 2019-03-11 stsp struct got_reference *ref;
6032 d0eebce4 2019-03-11 stsp
6033 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
6034 d0eebce4 2019-03-11 stsp if (err)
6035 d0eebce4 2019-03-11 stsp return err;
6036 993f033b 2021-07-16 stsp
6037 161728eb 2021-07-24 stsp err = delete_ref(repo, ref);
6038 d0eebce4 2019-03-11 stsp got_ref_close(ref);
6039 d0eebce4 2019-03-11 stsp return err;
6040 d0eebce4 2019-03-11 stsp }
6041 d0eebce4 2019-03-11 stsp
6042 d0eebce4 2019-03-11 stsp static const struct got_error *
6043 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
6044 d0eebce4 2019-03-11 stsp {
6045 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
6046 d8247bfd 2022-03-10 naddy struct got_object_id *id = NULL;
6047 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
6048 d8247bfd 2022-03-10 naddy struct got_reflist_head refs;
6049 d1644381 2019-07-14 stsp
6050 d1644381 2019-07-14 stsp /*
6051 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
6052 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
6053 d1644381 2019-07-14 stsp * an unintended typo.
6054 d1644381 2019-07-14 stsp */
6055 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
6056 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6057 d0eebce4 2019-03-11 stsp
6058 d8247bfd 2022-03-10 naddy TAILQ_INIT(&refs);
6059 d8247bfd 2022-03-10 naddy err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6060 d8247bfd 2022-03-10 naddy if (err)
6061 d8247bfd 2022-03-10 naddy goto done;
6062 d8247bfd 2022-03-10 naddy err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6063 d8247bfd 2022-03-10 naddy &refs, repo);
6064 d8247bfd 2022-03-10 naddy got_ref_list_free(&refs);
6065 d8247bfd 2022-03-10 naddy if (err)
6066 d8247bfd 2022-03-10 naddy goto done;
6067 d83d9d5c 2019-05-13 stsp
6068 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
6069 d0eebce4 2019-03-11 stsp if (err)
6070 d0eebce4 2019-03-11 stsp goto done;
6071 d0eebce4 2019-03-11 stsp
6072 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
6073 d0eebce4 2019-03-11 stsp done:
6074 d0eebce4 2019-03-11 stsp if (ref)
6075 d0eebce4 2019-03-11 stsp got_ref_close(ref);
6076 d0eebce4 2019-03-11 stsp free(id);
6077 d1c1ae5f 2019-08-12 stsp return err;
6078 d1c1ae5f 2019-08-12 stsp }
6079 d1c1ae5f 2019-08-12 stsp
6080 d1c1ae5f 2019-08-12 stsp static const struct got_error *
6081 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
6082 d1c1ae5f 2019-08-12 stsp {
6083 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
6084 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
6085 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
6086 d1c1ae5f 2019-08-12 stsp
6087 d1c1ae5f 2019-08-12 stsp /*
6088 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
6089 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
6090 d1c1ae5f 2019-08-12 stsp * an unintended typo.
6091 d1c1ae5f 2019-08-12 stsp */
6092 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
6093 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6094 d1c1ae5f 2019-08-12 stsp
6095 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
6096 d1c1ae5f 2019-08-12 stsp if (err)
6097 d1c1ae5f 2019-08-12 stsp return err;
6098 d1c1ae5f 2019-08-12 stsp
6099 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
6100 d1c1ae5f 2019-08-12 stsp if (err)
6101 d1c1ae5f 2019-08-12 stsp goto done;
6102 d1c1ae5f 2019-08-12 stsp
6103 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
6104 d1c1ae5f 2019-08-12 stsp done:
6105 d1c1ae5f 2019-08-12 stsp if (target_ref)
6106 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
6107 d1c1ae5f 2019-08-12 stsp if (ref)
6108 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
6109 d0eebce4 2019-03-11 stsp return err;
6110 d0eebce4 2019-03-11 stsp }
6111 d0eebce4 2019-03-11 stsp
6112 d0eebce4 2019-03-11 stsp static const struct got_error *
6113 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
6114 d0eebce4 2019-03-11 stsp {
6115 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
6116 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
6117 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
6118 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
6119 0f104432 2021-11-20 stsp int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6120 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
6121 b2070a3f 2020-03-22 stsp char *refname = NULL;
6122 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6123 d0eebce4 2019-03-11 stsp
6124 0f104432 2021-11-20 stsp while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
6125 d0eebce4 2019-03-11 stsp switch (ch) {
6126 e31abbf2 2020-03-22 stsp case 'c':
6127 e31abbf2 2020-03-22 stsp obj_arg = optarg;
6128 e31abbf2 2020-03-22 stsp break;
6129 d0eebce4 2019-03-11 stsp case 'd':
6130 e31abbf2 2020-03-22 stsp do_delete = 1;
6131 d0eebce4 2019-03-11 stsp break;
6132 d0eebce4 2019-03-11 stsp case 'r':
6133 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
6134 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
6135 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6136 9ba1d308 2019-10-21 stsp optarg);
6137 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
6138 d0eebce4 2019-03-11 stsp break;
6139 d0eebce4 2019-03-11 stsp case 'l':
6140 d0eebce4 2019-03-11 stsp do_list = 1;
6141 d1c1ae5f 2019-08-12 stsp break;
6142 d1c1ae5f 2019-08-12 stsp case 's':
6143 e31abbf2 2020-03-22 stsp symref_target = optarg;
6144 0f104432 2021-11-20 stsp break;
6145 0f104432 2021-11-20 stsp case 't':
6146 0f104432 2021-11-20 stsp sort_by_time = 1;
6147 d0eebce4 2019-03-11 stsp break;
6148 d0eebce4 2019-03-11 stsp default:
6149 d0eebce4 2019-03-11 stsp usage_ref();
6150 d0eebce4 2019-03-11 stsp /* NOTREACHED */
6151 d0eebce4 2019-03-11 stsp }
6152 d0eebce4 2019-03-11 stsp }
6153 d0eebce4 2019-03-11 stsp
6154 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
6155 ff69268e 2020-12-13 stsp option_conflict('c', 'l');
6156 ff69268e 2020-12-13 stsp if (obj_arg && do_delete)
6157 ff69268e 2020-12-13 stsp option_conflict('c', 'd');
6158 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
6159 ff69268e 2020-12-13 stsp option_conflict('c', 's');
6160 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
6161 ff69268e 2020-12-13 stsp option_conflict('s', 'd');
6162 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
6163 ff69268e 2020-12-13 stsp option_conflict('s', 'l');
6164 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
6165 ff69268e 2020-12-13 stsp option_conflict('d', 'l');
6166 0f104432 2021-11-20 stsp if (sort_by_time && !do_list)
6167 0f104432 2021-11-20 stsp errx(1, "-t option requires -l option");
6168 d0eebce4 2019-03-11 stsp
6169 d0eebce4 2019-03-11 stsp argc -= optind;
6170 d0eebce4 2019-03-11 stsp argv += optind;
6171 d0eebce4 2019-03-11 stsp
6172 e31abbf2 2020-03-22 stsp if (do_list) {
6173 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
6174 d0eebce4 2019-03-11 stsp usage_ref();
6175 b2070a3f 2020-03-22 stsp if (argc == 1) {
6176 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
6177 b2070a3f 2020-03-22 stsp if (refname == NULL) {
6178 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
6179 b2070a3f 2020-03-22 stsp goto done;
6180 b2070a3f 2020-03-22 stsp }
6181 b2070a3f 2020-03-22 stsp }
6182 e31abbf2 2020-03-22 stsp } else {
6183 e31abbf2 2020-03-22 stsp if (argc != 1)
6184 e31abbf2 2020-03-22 stsp usage_ref();
6185 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
6186 b2070a3f 2020-03-22 stsp if (refname == NULL) {
6187 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
6188 b2070a3f 2020-03-22 stsp goto done;
6189 b2070a3f 2020-03-22 stsp }
6190 e31abbf2 2020-03-22 stsp }
6191 b2070a3f 2020-03-22 stsp
6192 b2070a3f 2020-03-22 stsp if (refname)
6193 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
6194 d0eebce4 2019-03-11 stsp
6195 d0eebce4 2019-03-11 stsp #ifndef PROFILE
6196 57160834 2022-05-31 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6197 57160834 2022-05-31 stsp "sendfd unveil", NULL) == -1)
6198 57160834 2022-05-31 stsp err(1, "pledge");
6199 d0eebce4 2019-03-11 stsp #endif
6200 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
6201 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
6202 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6203 d0eebce4 2019-03-11 stsp goto done;
6204 d0eebce4 2019-03-11 stsp }
6205 d0eebce4 2019-03-11 stsp
6206 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6207 0ae84acc 2022-06-15 tracey if (error != NULL)
6208 0ae84acc 2022-06-15 tracey goto done;
6209 0ae84acc 2022-06-15 tracey
6210 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
6211 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
6212 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6213 d0eebce4 2019-03-11 stsp goto done;
6214 d0eebce4 2019-03-11 stsp else
6215 d0eebce4 2019-03-11 stsp error = NULL;
6216 d0eebce4 2019-03-11 stsp if (worktree) {
6217 d0eebce4 2019-03-11 stsp repo_path =
6218 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
6219 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
6220 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
6221 d0eebce4 2019-03-11 stsp if (error)
6222 d0eebce4 2019-03-11 stsp goto done;
6223 d0eebce4 2019-03-11 stsp } else {
6224 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
6225 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
6226 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
6227 d0eebce4 2019-03-11 stsp goto done;
6228 d0eebce4 2019-03-11 stsp }
6229 d0eebce4 2019-03-11 stsp }
6230 d0eebce4 2019-03-11 stsp }
6231 d0eebce4 2019-03-11 stsp
6232 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6233 d0eebce4 2019-03-11 stsp if (error != NULL)
6234 d0eebce4 2019-03-11 stsp goto done;
6235 57160834 2022-05-31 stsp
6236 57160834 2022-05-31 stsp #ifndef PROFILE
6237 57160834 2022-05-31 stsp if (do_list) {
6238 57160834 2022-05-31 stsp /* Remove "cpath" promise. */
6239 57160834 2022-05-31 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6240 57160834 2022-05-31 stsp NULL) == -1)
6241 57160834 2022-05-31 stsp err(1, "pledge");
6242 57160834 2022-05-31 stsp }
6243 57160834 2022-05-31 stsp #endif
6244 d0eebce4 2019-03-11 stsp
6245 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
6246 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
6247 c02c541e 2019-03-29 stsp if (error)
6248 c02c541e 2019-03-29 stsp goto done;
6249 c02c541e 2019-03-29 stsp
6250 d0eebce4 2019-03-11 stsp if (do_list)
6251 0f104432 2021-11-20 stsp error = list_refs(repo, refname, sort_by_time);
6252 e31abbf2 2020-03-22 stsp else if (do_delete)
6253 161728eb 2021-07-24 stsp error = delete_ref_by_name(repo, refname);
6254 e31abbf2 2020-03-22 stsp else if (symref_target)
6255 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
6256 e31abbf2 2020-03-22 stsp else {
6257 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
6258 e31abbf2 2020-03-22 stsp usage_ref();
6259 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
6260 e31abbf2 2020-03-22 stsp }
6261 4e759de4 2019-06-26 stsp done:
6262 b2070a3f 2020-03-22 stsp free(refname);
6263 1d0f4054 2021-06-17 stsp if (repo) {
6264 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6265 1d0f4054 2021-06-17 stsp if (error == NULL)
6266 1d0f4054 2021-06-17 stsp error = close_err;
6267 1d0f4054 2021-06-17 stsp }
6268 4e759de4 2019-06-26 stsp if (worktree)
6269 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
6270 0ae84acc 2022-06-15 tracey if (pack_fds) {
6271 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6272 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6273 0ae84acc 2022-06-15 tracey if (error == NULL)
6274 0ae84acc 2022-06-15 tracey error = pack_err;
6275 0ae84acc 2022-06-15 tracey }
6276 4e759de4 2019-06-26 stsp free(cwd);
6277 4e759de4 2019-06-26 stsp free(repo_path);
6278 4e759de4 2019-06-26 stsp return error;
6279 4e759de4 2019-06-26 stsp }
6280 4e759de4 2019-06-26 stsp
6281 4e759de4 2019-06-26 stsp __dead static void
6282 4e759de4 2019-06-26 stsp usage_branch(void)
6283 4e759de4 2019-06-26 stsp {
6284 4e759de4 2019-06-26 stsp fprintf(stderr,
6285 f76670f0 2021-11-20 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-t] "
6286 f76670f0 2021-11-20 stsp "[-n] [name]\n", getprogname());
6287 4e759de4 2019-06-26 stsp exit(1);
6288 4e759de4 2019-06-26 stsp }
6289 4e759de4 2019-06-26 stsp
6290 4e759de4 2019-06-26 stsp static const struct got_error *
6291 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
6292 4e99b47e 2019-10-04 stsp struct got_reference *ref)
6293 4e99b47e 2019-10-04 stsp {
6294 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
6295 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
6296 4e99b47e 2019-10-04 stsp char *refstr;
6297 4e99b47e 2019-10-04 stsp
6298 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
6299 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
6300 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
6301 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
6302 4e99b47e 2019-10-04 stsp
6303 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
6304 4e99b47e 2019-10-04 stsp if (err)
6305 4e99b47e 2019-10-04 stsp return err;
6306 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
6307 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
6308 4e99b47e 2019-10-04 stsp marker = "* ";
6309 4e99b47e 2019-10-04 stsp else
6310 4e99b47e 2019-10-04 stsp marker = "~ ";
6311 4e99b47e 2019-10-04 stsp free(id);
6312 4e99b47e 2019-10-04 stsp }
6313 4e99b47e 2019-10-04 stsp
6314 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
6315 4e99b47e 2019-10-04 stsp refname += 11;
6316 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6317 4e99b47e 2019-10-04 stsp refname += 18;
6318 34d4e04c 2021-02-08 stsp if (strncmp(refname, "refs/remotes/", 13) == 0)
6319 34d4e04c 2021-02-08 stsp refname += 13;
6320 4e99b47e 2019-10-04 stsp
6321 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
6322 4e99b47e 2019-10-04 stsp if (refstr == NULL)
6323 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
6324 4e99b47e 2019-10-04 stsp
6325 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
6326 4e99b47e 2019-10-04 stsp free(refstr);
6327 4e99b47e 2019-10-04 stsp return NULL;
6328 4e99b47e 2019-10-04 stsp }
6329 4e99b47e 2019-10-04 stsp
6330 4e99b47e 2019-10-04 stsp static const struct got_error *
6331 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6332 ad89fa31 2019-10-04 stsp {
6333 ad89fa31 2019-10-04 stsp const char *refname;
6334 ad89fa31 2019-10-04 stsp
6335 ad89fa31 2019-10-04 stsp if (worktree == NULL)
6336 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
6337 ad89fa31 2019-10-04 stsp
6338 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
6339 ad89fa31 2019-10-04 stsp
6340 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
6341 ad89fa31 2019-10-04 stsp refname += 11;
6342 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6343 ad89fa31 2019-10-04 stsp refname += 18;
6344 ad89fa31 2019-10-04 stsp
6345 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
6346 ad89fa31 2019-10-04 stsp
6347 ad89fa31 2019-10-04 stsp return NULL;
6348 ad89fa31 2019-10-04 stsp }
6349 ad89fa31 2019-10-04 stsp
6350 ad89fa31 2019-10-04 stsp static const struct got_error *
6351 f76670f0 2021-11-20 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree,
6352 f76670f0 2021-11-20 stsp int sort_by_time)
6353 4e759de4 2019-06-26 stsp {
6354 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
6355 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
6356 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
6357 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
6358 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
6359 4e759de4 2019-06-26 stsp
6360 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6361 ba882ee3 2019-07-11 stsp
6362 4e99b47e 2019-10-04 stsp if (worktree) {
6363 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
6364 4e99b47e 2019-10-04 stsp worktree);
6365 4e99b47e 2019-10-04 stsp if (err)
6366 4e99b47e 2019-10-04 stsp return err;
6367 4e99b47e 2019-10-04 stsp
6368 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
6369 4e99b47e 2019-10-04 stsp worktree);
6370 4e99b47e 2019-10-04 stsp if (err)
6371 4e99b47e 2019-10-04 stsp return err;
6372 4e99b47e 2019-10-04 stsp
6373 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
6374 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
6375 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
6376 4e99b47e 2019-10-04 stsp if (err)
6377 4e99b47e 2019-10-04 stsp return err;
6378 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
6379 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
6380 4e99b47e 2019-10-04 stsp }
6381 4e99b47e 2019-10-04 stsp }
6382 4e99b47e 2019-10-04 stsp
6383 f76670f0 2021-11-20 stsp err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6384 f76670f0 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6385 f76670f0 2021-11-20 stsp repo);
6386 4e759de4 2019-06-26 stsp if (err)
6387 4e759de4 2019-06-26 stsp return err;
6388 4e759de4 2019-06-26 stsp
6389 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry)
6390 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
6391 4e759de4 2019-06-26 stsp
6392 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
6393 34d4e04c 2021-02-08 stsp
6394 f76670f0 2021-11-20 stsp err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6395 f76670f0 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6396 f76670f0 2021-11-20 stsp repo);
6397 34d4e04c 2021-02-08 stsp if (err)
6398 34d4e04c 2021-02-08 stsp return err;
6399 34d4e04c 2021-02-08 stsp
6400 34d4e04c 2021-02-08 stsp TAILQ_FOREACH(re, &refs, entry)
6401 34d4e04c 2021-02-08 stsp list_branch(repo, worktree, re->ref);
6402 34d4e04c 2021-02-08 stsp
6403 34d4e04c 2021-02-08 stsp got_ref_list_free(&refs);
6404 34d4e04c 2021-02-08 stsp
6405 4e759de4 2019-06-26 stsp return NULL;
6406 4e759de4 2019-06-26 stsp }
6407 4e759de4 2019-06-26 stsp
6408 4e759de4 2019-06-26 stsp static const struct got_error *
6409 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6410 45cd4e47 2019-08-25 stsp const char *branch_name)
6411 4e759de4 2019-06-26 stsp {
6412 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
6413 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
6414 2f1457c6 2021-08-27 stsp char *refname, *remote_refname = NULL;
6415 4e759de4 2019-06-26 stsp
6416 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "refs/", 5) == 0)
6417 2f1457c6 2021-08-27 stsp branch_name += 5;
6418 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "heads/", 6) == 0)
6419 2f1457c6 2021-08-27 stsp branch_name += 6;
6420 2f1457c6 2021-08-27 stsp else if (strncmp(branch_name, "remotes/", 8) == 0)
6421 2f1457c6 2021-08-27 stsp branch_name += 8;
6422 2f1457c6 2021-08-27 stsp
6423 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6424 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
6425 4e759de4 2019-06-26 stsp
6426 2f1457c6 2021-08-27 stsp if (asprintf(&remote_refname, "refs/remotes/%s",
6427 2f1457c6 2021-08-27 stsp branch_name) == -1) {
6428 2f1457c6 2021-08-27 stsp err = got_error_from_errno("asprintf");
6429 4e759de4 2019-06-26 stsp goto done;
6430 2f1457c6 2021-08-27 stsp }
6431 4e759de4 2019-06-26 stsp
6432 2f1457c6 2021-08-27 stsp err = got_ref_open(&ref, repo, refname, 0);
6433 2f1457c6 2021-08-27 stsp if (err) {
6434 2f1457c6 2021-08-27 stsp const struct got_error *err2;
6435 2f1457c6 2021-08-27 stsp if (err->code != GOT_ERR_NOT_REF)
6436 2f1457c6 2021-08-27 stsp goto done;
6437 2f1457c6 2021-08-27 stsp /*
6438 2f1457c6 2021-08-27 stsp * Keep 'err' intact such that if neither branch exists
6439 2f1457c6 2021-08-27 stsp * we report "refs/heads" rather than "refs/remotes" in
6440 2f1457c6 2021-08-27 stsp * our error message.
6441 2f1457c6 2021-08-27 stsp */
6442 2f1457c6 2021-08-27 stsp err2 = got_ref_open(&ref, repo, remote_refname, 0);
6443 2f1457c6 2021-08-27 stsp if (err2)
6444 2f1457c6 2021-08-27 stsp goto done;
6445 2f1457c6 2021-08-27 stsp err = NULL;
6446 2f1457c6 2021-08-27 stsp }
6447 2f1457c6 2021-08-27 stsp
6448 45cd4e47 2019-08-25 stsp if (worktree &&
6449 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
6450 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
6451 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6452 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
6453 45cd4e47 2019-08-25 stsp goto done;
6454 45cd4e47 2019-08-25 stsp }
6455 45cd4e47 2019-08-25 stsp
6456 978a28a1 2021-09-04 naddy err = delete_ref(repo, ref);
6457 4e759de4 2019-06-26 stsp done:
6458 45cd4e47 2019-08-25 stsp if (ref)
6459 45cd4e47 2019-08-25 stsp got_ref_close(ref);
6460 4e759de4 2019-06-26 stsp free(refname);
6461 2f1457c6 2021-08-27 stsp free(remote_refname);
6462 4e759de4 2019-06-26 stsp return err;
6463 4e759de4 2019-06-26 stsp }
6464 4e759de4 2019-06-26 stsp
6465 4e759de4 2019-06-26 stsp static const struct got_error *
6466 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
6467 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
6468 4e759de4 2019-06-26 stsp {
6469 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
6470 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
6471 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
6472 d3f84d51 2019-07-11 stsp
6473 d3f84d51 2019-07-11 stsp /*
6474 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
6475 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
6476 d3f84d51 2019-07-11 stsp * an unintended typo.
6477 d3f84d51 2019-07-11 stsp */
6478 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
6479 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6480 2f1457c6 2021-08-27 stsp
6481 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
6482 2f1457c6 2021-08-27 stsp branch_name += 11;
6483 4e759de4 2019-06-26 stsp
6484 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6485 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
6486 62d463ca 2020-10-20 naddy goto done;
6487 4e759de4 2019-06-26 stsp }
6488 4e759de4 2019-06-26 stsp
6489 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
6490 4e759de4 2019-06-26 stsp if (err == NULL) {
6491 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
6492 4e759de4 2019-06-26 stsp goto done;
6493 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
6494 4e759de4 2019-06-26 stsp goto done;
6495 4e759de4 2019-06-26 stsp
6496 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
6497 4e759de4 2019-06-26 stsp if (err)
6498 4e759de4 2019-06-26 stsp goto done;
6499 4e759de4 2019-06-26 stsp
6500 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
6501 d0eebce4 2019-03-11 stsp done:
6502 4e759de4 2019-06-26 stsp if (ref)
6503 4e759de4 2019-06-26 stsp got_ref_close(ref);
6504 4e759de4 2019-06-26 stsp free(base_refname);
6505 4e759de4 2019-06-26 stsp free(refname);
6506 4e759de4 2019-06-26 stsp return err;
6507 4e759de4 2019-06-26 stsp }
6508 4e759de4 2019-06-26 stsp
6509 4e759de4 2019-06-26 stsp static const struct got_error *
6510 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
6511 4e759de4 2019-06-26 stsp {
6512 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
6513 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
6514 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
6515 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
6516 f76670f0 2021-11-20 stsp int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6517 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
6518 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
6519 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
6520 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
6521 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
6522 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
6523 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6524 4e759de4 2019-06-26 stsp
6525 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
6526 da76fce2 2020-02-24 stsp
6527 f76670f0 2021-11-20 stsp while ((ch = getopt(argc, argv, "c:d:r:lnt")) != -1) {
6528 4e759de4 2019-06-26 stsp switch (ch) {
6529 a74f7e83 2019-11-10 stsp case 'c':
6530 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
6531 a74f7e83 2019-11-10 stsp break;
6532 4e759de4 2019-06-26 stsp case 'd':
6533 4e759de4 2019-06-26 stsp delref = optarg;
6534 4e759de4 2019-06-26 stsp break;
6535 4e759de4 2019-06-26 stsp case 'r':
6536 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
6537 4e759de4 2019-06-26 stsp if (repo_path == NULL)
6538 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6539 9ba1d308 2019-10-21 stsp optarg);
6540 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
6541 4e759de4 2019-06-26 stsp break;
6542 4e759de4 2019-06-26 stsp case 'l':
6543 4e759de4 2019-06-26 stsp do_list = 1;
6544 da76fce2 2020-02-24 stsp break;
6545 da76fce2 2020-02-24 stsp case 'n':
6546 da76fce2 2020-02-24 stsp do_update = 0;
6547 4e759de4 2019-06-26 stsp break;
6548 f76670f0 2021-11-20 stsp case 't':
6549 f76670f0 2021-11-20 stsp sort_by_time = 1;
6550 f76670f0 2021-11-20 stsp break;
6551 4e759de4 2019-06-26 stsp default:
6552 4e759de4 2019-06-26 stsp usage_branch();
6553 4e759de4 2019-06-26 stsp /* NOTREACHED */
6554 4e759de4 2019-06-26 stsp }
6555 4e759de4 2019-06-26 stsp }
6556 4e759de4 2019-06-26 stsp
6557 4e759de4 2019-06-26 stsp if (do_list && delref)
6558 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
6559 f76670f0 2021-11-20 stsp if (sort_by_time && !do_list)
6560 f76670f0 2021-11-20 stsp errx(1, "-t option requires -l option");
6561 4e759de4 2019-06-26 stsp
6562 4e759de4 2019-06-26 stsp argc -= optind;
6563 4e759de4 2019-06-26 stsp argv += optind;
6564 ad89fa31 2019-10-04 stsp
6565 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
6566 ad89fa31 2019-10-04 stsp do_show = 1;
6567 4e759de4 2019-06-26 stsp
6568 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
6569 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
6570 a74f7e83 2019-11-10 stsp
6571 4e759de4 2019-06-26 stsp if (do_list || delref) {
6572 4e759de4 2019-06-26 stsp if (argc > 0)
6573 4e759de4 2019-06-26 stsp usage_branch();
6574 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
6575 4e759de4 2019-06-26 stsp usage_branch();
6576 4e759de4 2019-06-26 stsp
6577 4e759de4 2019-06-26 stsp #ifndef PROFILE
6578 57160834 2022-05-31 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6579 57160834 2022-05-31 stsp "sendfd unveil", NULL) == -1)
6580 57160834 2022-05-31 stsp err(1, "pledge");
6581 4e759de4 2019-06-26 stsp #endif
6582 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
6583 4e759de4 2019-06-26 stsp if (cwd == NULL) {
6584 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
6585 4e759de4 2019-06-26 stsp goto done;
6586 4e759de4 2019-06-26 stsp }
6587 4e759de4 2019-06-26 stsp
6588 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6589 0ae84acc 2022-06-15 tracey if (error != NULL)
6590 0ae84acc 2022-06-15 tracey goto done;
6591 0ae84acc 2022-06-15 tracey
6592 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
6593 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
6594 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6595 4e759de4 2019-06-26 stsp goto done;
6596 4e759de4 2019-06-26 stsp else
6597 4e759de4 2019-06-26 stsp error = NULL;
6598 4e759de4 2019-06-26 stsp if (worktree) {
6599 4e759de4 2019-06-26 stsp repo_path =
6600 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
6601 4e759de4 2019-06-26 stsp if (repo_path == NULL)
6602 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
6603 4e759de4 2019-06-26 stsp if (error)
6604 4e759de4 2019-06-26 stsp goto done;
6605 4e759de4 2019-06-26 stsp } else {
6606 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
6607 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
6608 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
6609 4e759de4 2019-06-26 stsp goto done;
6610 4e759de4 2019-06-26 stsp }
6611 4e759de4 2019-06-26 stsp }
6612 4e759de4 2019-06-26 stsp }
6613 4e759de4 2019-06-26 stsp
6614 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6615 4e759de4 2019-06-26 stsp if (error != NULL)
6616 4e759de4 2019-06-26 stsp goto done;
6617 57160834 2022-05-31 stsp
6618 57160834 2022-05-31 stsp #ifndef PROFILE
6619 57160834 2022-05-31 stsp if (do_list || do_show) {
6620 57160834 2022-05-31 stsp /* Remove "cpath" promise. */
6621 57160834 2022-05-31 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6622 57160834 2022-05-31 stsp NULL) == -1)
6623 57160834 2022-05-31 stsp err(1, "pledge");
6624 57160834 2022-05-31 stsp }
6625 57160834 2022-05-31 stsp #endif
6626 4e759de4 2019-06-26 stsp
6627 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
6628 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
6629 4e759de4 2019-06-26 stsp if (error)
6630 4e759de4 2019-06-26 stsp goto done;
6631 4e759de4 2019-06-26 stsp
6632 ad89fa31 2019-10-04 stsp if (do_show)
6633 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
6634 ad89fa31 2019-10-04 stsp else if (do_list)
6635 f76670f0 2021-11-20 stsp error = list_branches(repo, worktree, sort_by_time);
6636 4e759de4 2019-06-26 stsp else if (delref)
6637 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
6638 4e759de4 2019-06-26 stsp else {
6639 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6640 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6641 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6642 84de9106 2020-12-26 stsp NULL);
6643 84de9106 2020-12-26 stsp if (error)
6644 84de9106 2020-12-26 stsp goto done;
6645 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
6646 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
6647 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
6648 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
6649 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6650 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6651 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
6652 a74f7e83 2019-11-10 stsp if (error)
6653 a74f7e83 2019-11-10 stsp goto done;
6654 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
6655 da76fce2 2020-02-24 stsp if (error)
6656 da76fce2 2020-02-24 stsp goto done;
6657 da76fce2 2020-02-24 stsp if (worktree && do_update) {
6658 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6659 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
6660 da76fce2 2020-02-24 stsp
6661 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
6662 da76fce2 2020-02-24 stsp if (error)
6663 da76fce2 2020-02-24 stsp goto done;
6664 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
6665 da76fce2 2020-02-24 stsp worktree);
6666 da76fce2 2020-02-24 stsp if (error)
6667 da76fce2 2020-02-24 stsp goto done;
6668 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6669 da76fce2 2020-02-24 stsp == -1) {
6670 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
6671 da76fce2 2020-02-24 stsp goto done;
6672 da76fce2 2020-02-24 stsp }
6673 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
6674 da76fce2 2020-02-24 stsp free(branch_refname);
6675 da76fce2 2020-02-24 stsp if (error)
6676 da76fce2 2020-02-24 stsp goto done;
6677 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
6678 da76fce2 2020-02-24 stsp repo);
6679 da76fce2 2020-02-24 stsp if (error)
6680 da76fce2 2020-02-24 stsp goto done;
6681 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
6682 da76fce2 2020-02-24 stsp commit_id);
6683 da76fce2 2020-02-24 stsp if (error)
6684 da76fce2 2020-02-24 stsp goto done;
6685 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6686 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
6687 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
6688 9627c110 2020-04-18 stsp NULL);
6689 da76fce2 2020-02-24 stsp if (error)
6690 da76fce2 2020-02-24 stsp goto done;
6691 4f3c844b 2021-09-14 stsp if (upa.did_something) {
6692 4f3c844b 2021-09-14 stsp printf("Updated to %s: %s\n",
6693 4f3c844b 2021-09-14 stsp got_worktree_get_head_ref_name(worktree),
6694 4f3c844b 2021-09-14 stsp commit_id_str);
6695 4f3c844b 2021-09-14 stsp }
6696 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6697 da76fce2 2020-02-24 stsp }
6698 4e759de4 2019-06-26 stsp }
6699 4e759de4 2019-06-26 stsp done:
6700 da76fce2 2020-02-24 stsp if (ref)
6701 da76fce2 2020-02-24 stsp got_ref_close(ref);
6702 1d0f4054 2021-06-17 stsp if (repo) {
6703 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6704 1d0f4054 2021-06-17 stsp if (error == NULL)
6705 1d0f4054 2021-06-17 stsp error = close_err;
6706 1d0f4054 2021-06-17 stsp }
6707 d0eebce4 2019-03-11 stsp if (worktree)
6708 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
6709 0ae84acc 2022-06-15 tracey if (pack_fds) {
6710 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6711 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6712 0ae84acc 2022-06-15 tracey if (error == NULL)
6713 0ae84acc 2022-06-15 tracey error = pack_err;
6714 0ae84acc 2022-06-15 tracey }
6715 d0eebce4 2019-03-11 stsp free(cwd);
6716 d0eebce4 2019-03-11 stsp free(repo_path);
6717 da76fce2 2020-02-24 stsp free(commit_id);
6718 da76fce2 2020-02-24 stsp free(commit_id_str);
6719 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
6720 da76fce2 2020-02-24 stsp free((char *)pe->path);
6721 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
6722 d00136be 2019-03-26 stsp return error;
6723 d00136be 2019-03-26 stsp }
6724 d00136be 2019-03-26 stsp
6725 8e7bd50a 2019-08-22 stsp
6726 d00136be 2019-03-26 stsp __dead static void
6727 8e7bd50a 2019-08-22 stsp usage_tag(void)
6728 8e7bd50a 2019-08-22 stsp {
6729 8e7bd50a 2019-08-22 stsp fprintf(stderr,
6730 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
6731 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
6732 8e7bd50a 2019-08-22 stsp exit(1);
6733 b8bad2ba 2019-08-23 stsp }
6734 b8bad2ba 2019-08-23 stsp
6735 b8bad2ba 2019-08-23 stsp #if 0
6736 b8bad2ba 2019-08-23 stsp static const struct got_error *
6737 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6738 b8bad2ba 2019-08-23 stsp {
6739 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
6740 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
6741 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
6742 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
6743 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
6744 b8bad2ba 2019-08-23 stsp
6745 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(re, tags, entry) {
6746 dbdddfee 2021-06-23 naddy se = STAILQ_FIRST(sorted);
6747 b8bad2ba 2019-08-23 stsp if (se == NULL) {
6748 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6749 b8bad2ba 2019-08-23 stsp if (err)
6750 b8bad2ba 2019-08-23 stsp return err;
6751 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(sorted, new, entry);
6752 b8bad2ba 2019-08-23 stsp continue;
6753 b8bad2ba 2019-08-23 stsp } else {
6754 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
6755 b8bad2ba 2019-08-23 stsp if (err)
6756 b8bad2ba 2019-08-23 stsp break;
6757 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
6758 b8bad2ba 2019-08-23 stsp free(re_id);
6759 b8bad2ba 2019-08-23 stsp if (err)
6760 b8bad2ba 2019-08-23 stsp break;
6761 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
6762 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
6763 b8bad2ba 2019-08-23 stsp }
6764 b8bad2ba 2019-08-23 stsp
6765 b8bad2ba 2019-08-23 stsp while (se) {
6766 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
6767 b8bad2ba 2019-08-23 stsp if (err)
6768 b8bad2ba 2019-08-23 stsp break;
6769 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
6770 b8bad2ba 2019-08-23 stsp free(se_id);
6771 b8bad2ba 2019-08-23 stsp if (err)
6772 b8bad2ba 2019-08-23 stsp break;
6773 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
6774 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
6775 b8bad2ba 2019-08-23 stsp
6776 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
6777 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6778 b8bad2ba 2019-08-23 stsp if (err)
6779 b8bad2ba 2019-08-23 stsp return err;
6780 dbdddfee 2021-06-23 naddy STAILQ_INSERT_AFTER(sorted, se, new, entry);
6781 b8bad2ba 2019-08-23 stsp break;
6782 b8bad2ba 2019-08-23 stsp }
6783 dbdddfee 2021-06-23 naddy se = STAILQ_NEXT(se, entry);
6784 b8bad2ba 2019-08-23 stsp continue;
6785 b8bad2ba 2019-08-23 stsp }
6786 b8bad2ba 2019-08-23 stsp }
6787 b8bad2ba 2019-08-23 stsp done:
6788 b8bad2ba 2019-08-23 stsp return err;
6789 8e7bd50a 2019-08-22 stsp }
6790 b8bad2ba 2019-08-23 stsp #endif
6791 b8bad2ba 2019-08-23 stsp
6792 b8bad2ba 2019-08-23 stsp static const struct got_error *
6793 2a261ce7 2022-04-09 stsp list_tags(struct got_repository *repo)
6794 8e7bd50a 2019-08-22 stsp {
6795 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
6796 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
6797 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
6798 8e7bd50a 2019-08-22 stsp
6799 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6800 8e7bd50a 2019-08-22 stsp
6801 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6802 8e7bd50a 2019-08-22 stsp if (err)
6803 8e7bd50a 2019-08-22 stsp return err;
6804 8e7bd50a 2019-08-22 stsp
6805 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
6806 8e7bd50a 2019-08-22 stsp const char *refname;
6807 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6808 8e7bd50a 2019-08-22 stsp char datebuf[26];
6809 d4efa91b 2020-01-14 stsp const char *tagger;
6810 8e7bd50a 2019-08-22 stsp time_t tagger_time;
6811 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
6812 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
6813 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
6814 8e7bd50a 2019-08-22 stsp
6815 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
6816 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
6817 8e7bd50a 2019-08-22 stsp continue;
6818 8e7bd50a 2019-08-22 stsp refname += 10;
6819 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
6820 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
6821 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
6822 8e7bd50a 2019-08-22 stsp break;
6823 8e7bd50a 2019-08-22 stsp }
6824 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6825 8e7bd50a 2019-08-22 stsp free(refstr);
6826 8e7bd50a 2019-08-22 stsp
6827 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
6828 8e7bd50a 2019-08-22 stsp if (err)
6829 8e7bd50a 2019-08-22 stsp break;
6830 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
6831 d4efa91b 2020-01-14 stsp if (err) {
6832 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
6833 d4efa91b 2020-01-14 stsp free(id);
6834 d4efa91b 2020-01-14 stsp break;
6835 d4efa91b 2020-01-14 stsp }
6836 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
6837 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
6838 d4efa91b 2020-01-14 stsp if (err) {
6839 d4efa91b 2020-01-14 stsp free(id);
6840 d4efa91b 2020-01-14 stsp break;
6841 d4efa91b 2020-01-14 stsp }
6842 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
6843 d4efa91b 2020-01-14 stsp tagger_time =
6844 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
6845 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
6846 d4efa91b 2020-01-14 stsp free(id);
6847 d4efa91b 2020-01-14 stsp if (err)
6848 d4efa91b 2020-01-14 stsp break;
6849 d4efa91b 2020-01-14 stsp } else {
6850 d4efa91b 2020-01-14 stsp free(id);
6851 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
6852 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
6853 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
6854 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
6855 d4efa91b 2020-01-14 stsp if (err)
6856 d4efa91b 2020-01-14 stsp break;
6857 d4efa91b 2020-01-14 stsp }
6858 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
6859 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
6860 2417344c 2019-08-23 stsp if (datestr)
6861 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
6862 d4efa91b 2020-01-14 stsp if (commit)
6863 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6864 d4efa91b 2020-01-14 stsp else {
6865 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
6866 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
6867 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6868 d4efa91b 2020-01-14 stsp id_str);
6869 d4efa91b 2020-01-14 stsp break;
6870 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
6871 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6872 d4efa91b 2020-01-14 stsp id_str);
6873 d4efa91b 2020-01-14 stsp break;
6874 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
6875 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6876 d4efa91b 2020-01-14 stsp id_str);
6877 d4efa91b 2020-01-14 stsp break;
6878 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
6879 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6880 d4efa91b 2020-01-14 stsp id_str);
6881 d4efa91b 2020-01-14 stsp break;
6882 d4efa91b 2020-01-14 stsp default:
6883 d4efa91b 2020-01-14 stsp break;
6884 d4efa91b 2020-01-14 stsp }
6885 8e7bd50a 2019-08-22 stsp }
6886 8e7bd50a 2019-08-22 stsp free(id_str);
6887 d4efa91b 2020-01-14 stsp if (commit) {
6888 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
6889 d4efa91b 2020-01-14 stsp if (err)
6890 d4efa91b 2020-01-14 stsp break;
6891 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
6892 d4efa91b 2020-01-14 stsp } else {
6893 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
6894 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
6895 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
6896 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
6897 d4efa91b 2020-01-14 stsp break;
6898 d4efa91b 2020-01-14 stsp }
6899 8e7bd50a 2019-08-22 stsp }
6900 8e7bd50a 2019-08-22 stsp
6901 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
6902 8e7bd50a 2019-08-22 stsp do {
6903 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
6904 8e7bd50a 2019-08-22 stsp if (line)
6905 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
6906 8e7bd50a 2019-08-22 stsp } while (line);
6907 8e7bd50a 2019-08-22 stsp free(tagmsg0);
6908 8e7bd50a 2019-08-22 stsp }
6909 8e7bd50a 2019-08-22 stsp
6910 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
6911 8e7bd50a 2019-08-22 stsp return NULL;
6912 8e7bd50a 2019-08-22 stsp }
6913 8e7bd50a 2019-08-22 stsp
6914 8e7bd50a 2019-08-22 stsp static const struct got_error *
6915 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6916 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
6917 8e7bd50a 2019-08-22 stsp {
6918 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6919 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
6920 f372d5cd 2019-10-21 stsp char *editor = NULL;
6921 1601cb9f 2020-09-11 naddy int initial_content_len;
6922 8e7bd50a 2019-08-22 stsp int fd = -1;
6923 8e7bd50a 2019-08-22 stsp
6924 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6925 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6926 8e7bd50a 2019-08-22 stsp goto done;
6927 8e7bd50a 2019-08-22 stsp }
6928 8e7bd50a 2019-08-22 stsp
6929 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6930 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
6931 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
6932 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
6933 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6934 8e7bd50a 2019-08-22 stsp goto done;
6935 8e7bd50a 2019-08-22 stsp }
6936 8e7bd50a 2019-08-22 stsp
6937 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6938 8e7bd50a 2019-08-22 stsp if (err)
6939 8e7bd50a 2019-08-22 stsp goto done;
6940 8e7bd50a 2019-08-22 stsp
6941 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6942 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
6943 97972933 2020-09-11 stsp goto done;
6944 97972933 2020-09-11 stsp }
6945 8e7bd50a 2019-08-22 stsp
6946 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
6947 8e7bd50a 2019-08-22 stsp if (err)
6948 8e7bd50a 2019-08-22 stsp goto done;
6949 bfa12d5e 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6950 0d5bb276 2020-12-15 stsp initial_content_len, 1);
6951 8e7bd50a 2019-08-22 stsp done:
6952 8e7bd50a 2019-08-22 stsp free(initial_content);
6953 8e7bd50a 2019-08-22 stsp free(template);
6954 8e7bd50a 2019-08-22 stsp free(editor);
6955 8e7bd50a 2019-08-22 stsp
6956 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6957 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
6958 97972933 2020-09-11 stsp
6959 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
6960 59f86c76 2020-09-11 stsp if (err == NULL)
6961 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
6962 59f86c76 2020-09-11 stsp if (err) {
6963 59f86c76 2020-09-11 stsp free(*tagmsg);
6964 59f86c76 2020-09-11 stsp *tagmsg = NULL;
6965 8e7bd50a 2019-08-22 stsp }
6966 8e7bd50a 2019-08-22 stsp return err;
6967 8e7bd50a 2019-08-22 stsp }
6968 8e7bd50a 2019-08-22 stsp
6969 8e7bd50a 2019-08-22 stsp static const struct got_error *
6970 ffd9dda7 2022-04-10 stsp add_tag(struct got_repository *repo, const char *tagger,
6971 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6972 8e7bd50a 2019-08-22 stsp {
6973 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6974 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
6975 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
6976 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
6977 ffd9dda7 2022-04-10 stsp char *refname = NULL, *tagmsg = NULL;
6978 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
6979 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
6980 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6981 8e7bd50a 2019-08-22 stsp
6982 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6983 84de9106 2020-12-26 stsp
6984 8e7bd50a 2019-08-22 stsp /*
6985 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
6986 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
6987 8e7bd50a 2019-08-22 stsp * an unintended typo.
6988 8e7bd50a 2019-08-22 stsp */
6989 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
6990 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6991 8e7bd50a 2019-08-22 stsp
6992 84de9106 2020-12-26 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6993 84de9106 2020-12-26 stsp if (err)
6994 84de9106 2020-12-26 stsp goto done;
6995 84de9106 2020-12-26 stsp
6996 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6997 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &refs, repo);
6998 8e7bd50a 2019-08-22 stsp if (err)
6999 8e7bd50a 2019-08-22 stsp goto done;
7000 8e7bd50a 2019-08-22 stsp
7001 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
7002 8e7bd50a 2019-08-22 stsp if (err)
7003 8e7bd50a 2019-08-22 stsp goto done;
7004 8e7bd50a 2019-08-22 stsp
7005 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
7006 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
7007 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
7008 62d463ca 2020-10-20 naddy err = got_error_from_errno("strdup");
7009 62d463ca 2020-10-20 naddy goto done;
7010 8e7bd50a 2019-08-22 stsp }
7011 8e7bd50a 2019-08-22 stsp tag_name += 10;
7012 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
7013 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
7014 62d463ca 2020-10-20 naddy goto done;
7015 8e7bd50a 2019-08-22 stsp }
7016 8e7bd50a 2019-08-22 stsp
7017 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
7018 8e7bd50a 2019-08-22 stsp if (err == NULL) {
7019 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
7020 8e7bd50a 2019-08-22 stsp goto done;
7021 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
7022 8e7bd50a 2019-08-22 stsp goto done;
7023 8e7bd50a 2019-08-22 stsp
7024 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
7025 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7026 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
7027 f372d5cd 2019-10-21 stsp if (err) {
7028 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7029 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
7030 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
7031 8e7bd50a 2019-08-22 stsp goto done;
7032 f372d5cd 2019-10-21 stsp }
7033 8e7bd50a 2019-08-22 stsp }
7034 8e7bd50a 2019-08-22 stsp
7035 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
7036 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
7037 f372d5cd 2019-10-21 stsp if (err) {
7038 f372d5cd 2019-10-21 stsp if (tagmsg_path)
7039 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
7040 8e7bd50a 2019-08-22 stsp goto done;
7041 f372d5cd 2019-10-21 stsp }
7042 8e7bd50a 2019-08-22 stsp
7043 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
7044 f372d5cd 2019-10-21 stsp if (err) {
7045 f372d5cd 2019-10-21 stsp if (tagmsg_path)
7046 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
7047 8e7bd50a 2019-08-22 stsp goto done;
7048 f372d5cd 2019-10-21 stsp }
7049 8e7bd50a 2019-08-22 stsp
7050 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
7051 f372d5cd 2019-10-21 stsp if (err) {
7052 f372d5cd 2019-10-21 stsp if (tagmsg_path)
7053 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
7054 f372d5cd 2019-10-21 stsp goto done;
7055 f372d5cd 2019-10-21 stsp }
7056 8e7bd50a 2019-08-22 stsp
7057 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
7058 f372d5cd 2019-10-21 stsp if (err) {
7059 f372d5cd 2019-10-21 stsp if (tagmsg_path)
7060 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
7061 f372d5cd 2019-10-21 stsp goto done;
7062 8e7bd50a 2019-08-22 stsp }
7063 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
7064 8e7bd50a 2019-08-22 stsp done:
7065 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
7066 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
7067 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
7068 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7069 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
7070 f372d5cd 2019-10-21 stsp free(tag_id_str);
7071 8e7bd50a 2019-08-22 stsp if (ref)
7072 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
7073 8e7bd50a 2019-08-22 stsp free(commit_id);
7074 8e7bd50a 2019-08-22 stsp free(commit_id_str);
7075 8e7bd50a 2019-08-22 stsp free(refname);
7076 8e7bd50a 2019-08-22 stsp free(tagmsg);
7077 f372d5cd 2019-10-21 stsp free(tagmsg_path);
7078 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
7079 8e7bd50a 2019-08-22 stsp return err;
7080 8e7bd50a 2019-08-22 stsp }
7081 8e7bd50a 2019-08-22 stsp
7082 8e7bd50a 2019-08-22 stsp static const struct got_error *
7083 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
7084 8e7bd50a 2019-08-22 stsp {
7085 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
7086 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
7087 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
7088 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7089 ffd9dda7 2022-04-10 stsp char *gitconfig_path = NULL, *tagger = NULL;
7090 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
7091 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
7092 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7093 8e7bd50a 2019-08-22 stsp
7094 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
7095 8e7bd50a 2019-08-22 stsp switch (ch) {
7096 80106605 2020-02-24 stsp case 'c':
7097 80106605 2020-02-24 stsp commit_id_arg = optarg;
7098 80106605 2020-02-24 stsp break;
7099 8e7bd50a 2019-08-22 stsp case 'm':
7100 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
7101 8e7bd50a 2019-08-22 stsp break;
7102 8e7bd50a 2019-08-22 stsp case 'r':
7103 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
7104 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
7105 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7106 9ba1d308 2019-10-21 stsp optarg);
7107 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
7108 8e7bd50a 2019-08-22 stsp break;
7109 8e7bd50a 2019-08-22 stsp case 'l':
7110 8e7bd50a 2019-08-22 stsp do_list = 1;
7111 8e7bd50a 2019-08-22 stsp break;
7112 8e7bd50a 2019-08-22 stsp default:
7113 8e7bd50a 2019-08-22 stsp usage_tag();
7114 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
7115 8e7bd50a 2019-08-22 stsp }
7116 8e7bd50a 2019-08-22 stsp }
7117 8e7bd50a 2019-08-22 stsp
7118 8e7bd50a 2019-08-22 stsp argc -= optind;
7119 8e7bd50a 2019-08-22 stsp argv += optind;
7120 8e7bd50a 2019-08-22 stsp
7121 8e7bd50a 2019-08-22 stsp if (do_list) {
7122 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
7123 775ce909 2020-03-22 stsp errx(1,
7124 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
7125 8e7bd50a 2019-08-22 stsp if (tagmsg)
7126 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
7127 8e7bd50a 2019-08-22 stsp if (argc > 0)
7128 8e7bd50a 2019-08-22 stsp usage_tag();
7129 80106605 2020-02-24 stsp } else if (argc != 1)
7130 8e7bd50a 2019-08-22 stsp usage_tag();
7131 80106605 2020-02-24 stsp
7132 a2887370 2019-08-23 stsp tag_name = argv[0];
7133 8e7bd50a 2019-08-22 stsp
7134 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
7135 57160834 2022-05-31 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7136 57160834 2022-05-31 stsp "sendfd unveil", NULL) == -1)
7137 57160834 2022-05-31 stsp err(1, "pledge");
7138 8e7bd50a 2019-08-22 stsp #endif
7139 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
7140 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
7141 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
7142 8e7bd50a 2019-08-22 stsp goto done;
7143 8e7bd50a 2019-08-22 stsp }
7144 8e7bd50a 2019-08-22 stsp
7145 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7146 0ae84acc 2022-06-15 tracey if (error != NULL)
7147 0ae84acc 2022-06-15 tracey goto done;
7148 0ae84acc 2022-06-15 tracey
7149 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
7150 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
7151 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7152 8e7bd50a 2019-08-22 stsp goto done;
7153 8e7bd50a 2019-08-22 stsp else
7154 8e7bd50a 2019-08-22 stsp error = NULL;
7155 8e7bd50a 2019-08-22 stsp if (worktree) {
7156 8e7bd50a 2019-08-22 stsp repo_path =
7157 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
7158 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
7159 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
7160 8e7bd50a 2019-08-22 stsp if (error)
7161 8e7bd50a 2019-08-22 stsp goto done;
7162 8e7bd50a 2019-08-22 stsp } else {
7163 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
7164 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
7165 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
7166 8e7bd50a 2019-08-22 stsp goto done;
7167 8e7bd50a 2019-08-22 stsp }
7168 8e7bd50a 2019-08-22 stsp }
7169 8e7bd50a 2019-08-22 stsp }
7170 8e7bd50a 2019-08-22 stsp
7171 8e7bd50a 2019-08-22 stsp if (do_list) {
7172 2a261ce7 2022-04-09 stsp if (worktree) {
7173 2a261ce7 2022-04-09 stsp /* Release work tree lock. */
7174 2a261ce7 2022-04-09 stsp got_worktree_close(worktree);
7175 2a261ce7 2022-04-09 stsp worktree = NULL;
7176 2a261ce7 2022-04-09 stsp }
7177 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7178 c9956ddf 2019-09-08 stsp if (error != NULL)
7179 c9956ddf 2019-09-08 stsp goto done;
7180 0ae84acc 2022-06-15 tracey
7181 57160834 2022-05-31 stsp #ifndef PROFILE
7182 57160834 2022-05-31 stsp /* Remove "cpath" promise. */
7183 57160834 2022-05-31 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
7184 57160834 2022-05-31 stsp NULL) == -1)
7185 57160834 2022-05-31 stsp err(1, "pledge");
7186 57160834 2022-05-31 stsp #endif
7187 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7188 8e7bd50a 2019-08-22 stsp if (error)
7189 8e7bd50a 2019-08-22 stsp goto done;
7190 2a261ce7 2022-04-09 stsp error = list_tags(repo);
7191 8e7bd50a 2019-08-22 stsp } else {
7192 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
7193 c9956ddf 2019-09-08 stsp if (error)
7194 c9956ddf 2019-09-08 stsp goto done;
7195 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, gitconfig_path,
7196 0ae84acc 2022-06-15 tracey pack_fds);
7197 c9956ddf 2019-09-08 stsp if (error != NULL)
7198 c9956ddf 2019-09-08 stsp goto done;
7199 c9956ddf 2019-09-08 stsp
7200 ffd9dda7 2022-04-10 stsp error = get_author(&tagger, repo, worktree);
7201 ffd9dda7 2022-04-10 stsp if (error)
7202 ffd9dda7 2022-04-10 stsp goto done;
7203 ffd9dda7 2022-04-10 stsp if (worktree) {
7204 ffd9dda7 2022-04-10 stsp /* Release work tree lock. */
7205 ffd9dda7 2022-04-10 stsp got_worktree_close(worktree);
7206 ffd9dda7 2022-04-10 stsp worktree = NULL;
7207 ffd9dda7 2022-04-10 stsp }
7208 ffd9dda7 2022-04-10 stsp
7209 8e7bd50a 2019-08-22 stsp if (tagmsg) {
7210 42a285e2 2020-10-01 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7211 8e7bd50a 2019-08-22 stsp if (error)
7212 8e7bd50a 2019-08-22 stsp goto done;
7213 8e7bd50a 2019-08-22 stsp }
7214 8e7bd50a 2019-08-22 stsp
7215 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
7216 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
7217 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
7218 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
7219 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
7220 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
7221 8e7bd50a 2019-08-22 stsp if (error)
7222 8e7bd50a 2019-08-22 stsp goto done;
7223 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
7224 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
7225 8e7bd50a 2019-08-22 stsp if (error)
7226 8e7bd50a 2019-08-22 stsp goto done;
7227 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
7228 8e7bd50a 2019-08-22 stsp free(commit_id);
7229 8e7bd50a 2019-08-22 stsp if (error)
7230 8e7bd50a 2019-08-22 stsp goto done;
7231 8e7bd50a 2019-08-22 stsp }
7232 8e7bd50a 2019-08-22 stsp
7233 ffd9dda7 2022-04-10 stsp error = add_tag(repo, tagger, tag_name,
7234 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
7235 8e7bd50a 2019-08-22 stsp }
7236 8e7bd50a 2019-08-22 stsp done:
7237 1d0f4054 2021-06-17 stsp if (repo) {
7238 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7239 1d0f4054 2021-06-17 stsp if (error == NULL)
7240 1d0f4054 2021-06-17 stsp error = close_err;
7241 1d0f4054 2021-06-17 stsp }
7242 8e7bd50a 2019-08-22 stsp if (worktree)
7243 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
7244 0ae84acc 2022-06-15 tracey if (pack_fds) {
7245 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7246 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7247 0ae84acc 2022-06-15 tracey if (error == NULL)
7248 0ae84acc 2022-06-15 tracey error = pack_err;
7249 0ae84acc 2022-06-15 tracey }
7250 8e7bd50a 2019-08-22 stsp free(cwd);
7251 8e7bd50a 2019-08-22 stsp free(repo_path);
7252 c9956ddf 2019-09-08 stsp free(gitconfig_path);
7253 8e7bd50a 2019-08-22 stsp free(commit_id_str);
7254 ffd9dda7 2022-04-10 stsp free(tagger);
7255 8e7bd50a 2019-08-22 stsp return error;
7256 8e7bd50a 2019-08-22 stsp }
7257 8e7bd50a 2019-08-22 stsp
7258 8e7bd50a 2019-08-22 stsp __dead static void
7259 d00136be 2019-03-26 stsp usage_add(void)
7260 d00136be 2019-03-26 stsp {
7261 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
7262 022fae89 2019-12-06 tracey getprogname());
7263 d00136be 2019-03-26 stsp exit(1);
7264 d00136be 2019-03-26 stsp }
7265 d00136be 2019-03-26 stsp
7266 d00136be 2019-03-26 stsp static const struct got_error *
7267 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
7268 4e68cba3 2019-11-23 stsp {
7269 4e68cba3 2019-11-23 stsp while (path[0] == '/')
7270 4e68cba3 2019-11-23 stsp path++;
7271 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
7272 4e68cba3 2019-11-23 stsp return NULL;
7273 4e68cba3 2019-11-23 stsp }
7274 4e68cba3 2019-11-23 stsp
7275 4e68cba3 2019-11-23 stsp static const struct got_error *
7276 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
7277 d00136be 2019-03-26 stsp {
7278 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
7279 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
7280 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
7281 1dd54920 2019-05-11 stsp char *cwd = NULL;
7282 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
7283 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
7284 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
7285 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7286 1dd54920 2019-05-11 stsp
7287 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
7288 d00136be 2019-03-26 stsp
7289 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
7290 d00136be 2019-03-26 stsp switch (ch) {
7291 022fae89 2019-12-06 tracey case 'I':
7292 022fae89 2019-12-06 tracey no_ignores = 1;
7293 022fae89 2019-12-06 tracey break;
7294 4e68cba3 2019-11-23 stsp case 'R':
7295 4e68cba3 2019-11-23 stsp can_recurse = 1;
7296 4e68cba3 2019-11-23 stsp break;
7297 d00136be 2019-03-26 stsp default:
7298 d00136be 2019-03-26 stsp usage_add();
7299 d00136be 2019-03-26 stsp /* NOTREACHED */
7300 d00136be 2019-03-26 stsp }
7301 d00136be 2019-03-26 stsp }
7302 d00136be 2019-03-26 stsp
7303 d00136be 2019-03-26 stsp argc -= optind;
7304 d00136be 2019-03-26 stsp argv += optind;
7305 d00136be 2019-03-26 stsp
7306 43012d58 2019-07-14 stsp #ifndef PROFILE
7307 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7308 43012d58 2019-07-14 stsp NULL) == -1)
7309 43012d58 2019-07-14 stsp err(1, "pledge");
7310 43012d58 2019-07-14 stsp #endif
7311 723c305c 2019-05-11 jcs if (argc < 1)
7312 d00136be 2019-03-26 stsp usage_add();
7313 d00136be 2019-03-26 stsp
7314 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
7315 d00136be 2019-03-26 stsp if (cwd == NULL) {
7316 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7317 d00136be 2019-03-26 stsp goto done;
7318 d00136be 2019-03-26 stsp }
7319 723c305c 2019-05-11 jcs
7320 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7321 0ae84acc 2022-06-15 tracey if (error != NULL)
7322 0ae84acc 2022-06-15 tracey goto done;
7323 0ae84acc 2022-06-15 tracey
7324 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
7325 fa51e947 2020-03-27 stsp if (error) {
7326 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7327 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
7328 d00136be 2019-03-26 stsp goto done;
7329 fa51e947 2020-03-27 stsp }
7330 d00136be 2019-03-26 stsp
7331 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7332 0ae84acc 2022-06-15 tracey NULL, pack_fds);
7333 031a5338 2019-03-26 stsp if (error != NULL)
7334 031a5338 2019-03-26 stsp goto done;
7335 031a5338 2019-03-26 stsp
7336 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
7337 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7338 d00136be 2019-03-26 stsp if (error)
7339 d00136be 2019-03-26 stsp goto done;
7340 d00136be 2019-03-26 stsp
7341 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7342 6d022e97 2019-08-04 stsp if (error)
7343 022fae89 2019-12-06 tracey goto done;
7344 022fae89 2019-12-06 tracey
7345 4e68cba3 2019-11-23 stsp if (!can_recurse) {
7346 4e68cba3 2019-11-23 stsp char *ondisk_path;
7347 4e68cba3 2019-11-23 stsp struct stat sb;
7348 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
7349 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
7350 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
7351 62d463ca 2020-10-20 naddy pe->path) == -1) {
7352 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
7353 4e68cba3 2019-11-23 stsp goto done;
7354 4e68cba3 2019-11-23 stsp }
7355 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7356 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
7357 4e68cba3 2019-11-23 stsp free(ondisk_path);
7358 4e68cba3 2019-11-23 stsp continue;
7359 4e68cba3 2019-11-23 stsp }
7360 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
7361 4e68cba3 2019-11-23 stsp ondisk_path);
7362 4e68cba3 2019-11-23 stsp free(ondisk_path);
7363 4e68cba3 2019-11-23 stsp goto done;
7364 4e68cba3 2019-11-23 stsp }
7365 4e68cba3 2019-11-23 stsp free(ondisk_path);
7366 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
7367 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
7368 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
7369 4e68cba3 2019-11-23 stsp goto done;
7370 4e68cba3 2019-11-23 stsp }
7371 4e68cba3 2019-11-23 stsp }
7372 4e68cba3 2019-11-23 stsp }
7373 022fae89 2019-12-06 tracey
7374 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
7375 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
7376 d00136be 2019-03-26 stsp done:
7377 1d0f4054 2021-06-17 stsp if (repo) {
7378 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7379 1d0f4054 2021-06-17 stsp if (error == NULL)
7380 1d0f4054 2021-06-17 stsp error = close_err;
7381 1d0f4054 2021-06-17 stsp }
7382 d00136be 2019-03-26 stsp if (worktree)
7383 d00136be 2019-03-26 stsp got_worktree_close(worktree);
7384 0ae84acc 2022-06-15 tracey if (pack_fds) {
7385 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7386 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7387 0ae84acc 2022-06-15 tracey if (error == NULL)
7388 0ae84acc 2022-06-15 tracey error = pack_err;
7389 0ae84acc 2022-06-15 tracey }
7390 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
7391 1dd54920 2019-05-11 stsp free((char *)pe->path);
7392 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
7393 2ec1f75b 2019-03-26 stsp free(cwd);
7394 2ec1f75b 2019-03-26 stsp return error;
7395 2ec1f75b 2019-03-26 stsp }
7396 2ec1f75b 2019-03-26 stsp
7397 2ec1f75b 2019-03-26 stsp __dead static void
7398 648e4ef7 2019-07-09 stsp usage_remove(void)
7399 2ec1f75b 2019-03-26 stsp {
7400 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
7401 766841c2 2020-08-13 stsp "path ...\n", getprogname());
7402 2ec1f75b 2019-03-26 stsp exit(1);
7403 2a06fe5f 2019-08-24 stsp }
7404 2a06fe5f 2019-08-24 stsp
7405 2a06fe5f 2019-08-24 stsp static const struct got_error *
7406 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
7407 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
7408 2a06fe5f 2019-08-24 stsp {
7409 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
7410 f2a9dc41 2019-12-13 tracey path++;
7411 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7412 2a06fe5f 2019-08-24 stsp return NULL;
7413 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
7414 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
7415 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
7416 2a06fe5f 2019-08-24 stsp return NULL;
7417 2ec1f75b 2019-03-26 stsp }
7418 2ec1f75b 2019-03-26 stsp
7419 2ec1f75b 2019-03-26 stsp static const struct got_error *
7420 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
7421 2ec1f75b 2019-03-26 stsp {
7422 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
7423 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
7424 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
7425 766841c2 2020-08-13 stsp const char *status_codes = NULL;
7426 17ed4618 2019-06-02 stsp char *cwd = NULL;
7427 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
7428 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
7429 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7430 4e12cd97 2022-01-25 stsp int ignore_missing_paths = 0;
7431 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7432 2ec1f75b 2019-03-26 stsp
7433 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
7434 17ed4618 2019-06-02 stsp
7435 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7436 2ec1f75b 2019-03-26 stsp switch (ch) {
7437 2ec1f75b 2019-03-26 stsp case 'f':
7438 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
7439 4e12cd97 2022-01-25 stsp ignore_missing_paths = 1;
7440 2ec1f75b 2019-03-26 stsp break;
7441 70e3e7f5 2019-12-13 tracey case 'k':
7442 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
7443 70e3e7f5 2019-12-13 tracey break;
7444 f2a9dc41 2019-12-13 tracey case 'R':
7445 f2a9dc41 2019-12-13 tracey can_recurse = 1;
7446 f2a9dc41 2019-12-13 tracey break;
7447 766841c2 2020-08-13 stsp case 's':
7448 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
7449 766841c2 2020-08-13 stsp switch (optarg[i]) {
7450 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
7451 766841c2 2020-08-13 stsp delete_local_mods = 1;
7452 766841c2 2020-08-13 stsp break;
7453 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
7454 4e12cd97 2022-01-25 stsp ignore_missing_paths = 1;
7455 766841c2 2020-08-13 stsp break;
7456 766841c2 2020-08-13 stsp default:
7457 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
7458 766841c2 2020-08-13 stsp optarg[i]);
7459 766841c2 2020-08-13 stsp }
7460 766841c2 2020-08-13 stsp }
7461 766841c2 2020-08-13 stsp status_codes = optarg;
7462 766841c2 2020-08-13 stsp break;
7463 2ec1f75b 2019-03-26 stsp default:
7464 f2a9dc41 2019-12-13 tracey usage_remove();
7465 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
7466 2ec1f75b 2019-03-26 stsp }
7467 2ec1f75b 2019-03-26 stsp }
7468 2ec1f75b 2019-03-26 stsp
7469 2ec1f75b 2019-03-26 stsp argc -= optind;
7470 2ec1f75b 2019-03-26 stsp argv += optind;
7471 2ec1f75b 2019-03-26 stsp
7472 43012d58 2019-07-14 stsp #ifndef PROFILE
7473 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7474 43012d58 2019-07-14 stsp NULL) == -1)
7475 43012d58 2019-07-14 stsp err(1, "pledge");
7476 43012d58 2019-07-14 stsp #endif
7477 17ed4618 2019-06-02 stsp if (argc < 1)
7478 648e4ef7 2019-07-09 stsp usage_remove();
7479 2ec1f75b 2019-03-26 stsp
7480 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
7481 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
7482 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7483 2ec1f75b 2019-03-26 stsp goto done;
7484 2ec1f75b 2019-03-26 stsp }
7485 0ae84acc 2022-06-15 tracey
7486 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7487 0ae84acc 2022-06-15 tracey if (error != NULL)
7488 0ae84acc 2022-06-15 tracey goto done;
7489 0ae84acc 2022-06-15 tracey
7490 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
7491 fa51e947 2020-03-27 stsp if (error) {
7492 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7493 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
7494 2ec1f75b 2019-03-26 stsp goto done;
7495 fa51e947 2020-03-27 stsp }
7496 2ec1f75b 2019-03-26 stsp
7497 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7498 0ae84acc 2022-06-15 tracey NULL, pack_fds);
7499 2af4a041 2019-05-11 jcs if (error)
7500 2ec1f75b 2019-03-26 stsp goto done;
7501 2ec1f75b 2019-03-26 stsp
7502 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
7503 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7504 2ec1f75b 2019-03-26 stsp if (error)
7505 2ec1f75b 2019-03-26 stsp goto done;
7506 2ec1f75b 2019-03-26 stsp
7507 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7508 6d022e97 2019-08-04 stsp if (error)
7509 6d022e97 2019-08-04 stsp goto done;
7510 17ed4618 2019-06-02 stsp
7511 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
7512 f2a9dc41 2019-12-13 tracey char *ondisk_path;
7513 f2a9dc41 2019-12-13 tracey struct stat sb;
7514 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
7515 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
7516 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
7517 62d463ca 2020-10-20 naddy pe->path) == -1) {
7518 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
7519 f2a9dc41 2019-12-13 tracey goto done;
7520 f2a9dc41 2019-12-13 tracey }
7521 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
7522 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
7523 f2a9dc41 2019-12-13 tracey free(ondisk_path);
7524 f2a9dc41 2019-12-13 tracey continue;
7525 f2a9dc41 2019-12-13 tracey }
7526 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
7527 f2a9dc41 2019-12-13 tracey ondisk_path);
7528 f2a9dc41 2019-12-13 tracey free(ondisk_path);
7529 f2a9dc41 2019-12-13 tracey goto done;
7530 f2a9dc41 2019-12-13 tracey }
7531 f2a9dc41 2019-12-13 tracey free(ondisk_path);
7532 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
7533 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
7534 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
7535 f2a9dc41 2019-12-13 tracey goto done;
7536 f2a9dc41 2019-12-13 tracey }
7537 f2a9dc41 2019-12-13 tracey }
7538 f2a9dc41 2019-12-13 tracey }
7539 f2a9dc41 2019-12-13 tracey
7540 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
7541 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
7542 4e12cd97 2022-01-25 stsp repo, keep_on_disk, ignore_missing_paths);
7543 a129376b 2019-03-28 stsp done:
7544 1d0f4054 2021-06-17 stsp if (repo) {
7545 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7546 1d0f4054 2021-06-17 stsp if (error == NULL)
7547 1d0f4054 2021-06-17 stsp error = close_err;
7548 1d0f4054 2021-06-17 stsp }
7549 a129376b 2019-03-28 stsp if (worktree)
7550 a129376b 2019-03-28 stsp got_worktree_close(worktree);
7551 0ae84acc 2022-06-15 tracey if (pack_fds) {
7552 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7553 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7554 0ae84acc 2022-06-15 tracey if (error == NULL)
7555 0ae84acc 2022-06-15 tracey error = pack_err;
7556 0ae84acc 2022-06-15 tracey }
7557 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
7558 17ed4618 2019-06-02 stsp free((char *)pe->path);
7559 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
7560 e9ce266e 2022-03-07 op free(cwd);
7561 e9ce266e 2022-03-07 op return error;
7562 e9ce266e 2022-03-07 op }
7563 e9ce266e 2022-03-07 op
7564 e9ce266e 2022-03-07 op __dead static void
7565 e9ce266e 2022-03-07 op usage_patch(void)
7566 e9ce266e 2022-03-07 op {
7567 bad961bf 2022-04-23 op fprintf(stderr, "usage: %s patch [-n] [-p strip-count] "
7568 bad961bf 2022-04-23 op "[-R] [patchfile]\n", getprogname());
7569 e9ce266e 2022-03-07 op exit(1);
7570 e9ce266e 2022-03-07 op }
7571 e9ce266e 2022-03-07 op
7572 e9ce266e 2022-03-07 op static const struct got_error *
7573 e9ce266e 2022-03-07 op patch_from_stdin(int *patchfd)
7574 e9ce266e 2022-03-07 op {
7575 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
7576 e9ce266e 2022-03-07 op ssize_t r;
7577 e9ce266e 2022-03-07 op char *path, buf[BUFSIZ];
7578 e9ce266e 2022-03-07 op sig_t sighup, sigint, sigquit;
7579 e9ce266e 2022-03-07 op
7580 e9ce266e 2022-03-07 op err = got_opentemp_named_fd(&path, patchfd,
7581 e9ce266e 2022-03-07 op GOT_TMPDIR_STR "/got-patch");
7582 e9ce266e 2022-03-07 op if (err)
7583 e9ce266e 2022-03-07 op return err;
7584 e9ce266e 2022-03-07 op unlink(path);
7585 e9ce266e 2022-03-07 op free(path);
7586 e9ce266e 2022-03-07 op
7587 e9ce266e 2022-03-07 op sighup = signal(SIGHUP, SIG_DFL);
7588 e9ce266e 2022-03-07 op sigint = signal(SIGINT, SIG_DFL);
7589 e9ce266e 2022-03-07 op sigquit = signal(SIGQUIT, SIG_DFL);
7590 e9ce266e 2022-03-07 op
7591 e9ce266e 2022-03-07 op for (;;) {
7592 e9ce266e 2022-03-07 op r = read(0, buf, sizeof(buf));
7593 e9ce266e 2022-03-07 op if (r == -1) {
7594 e9ce266e 2022-03-07 op err = got_error_from_errno("read");
7595 e9ce266e 2022-03-07 op break;
7596 e9ce266e 2022-03-07 op }
7597 e9ce266e 2022-03-07 op if (r == 0)
7598 e9ce266e 2022-03-07 op break;
7599 e9ce266e 2022-03-07 op if (write(*patchfd, buf, r) == -1) {
7600 e9ce266e 2022-03-07 op err = got_error_from_errno("write");
7601 e9ce266e 2022-03-07 op break;
7602 e9ce266e 2022-03-07 op }
7603 e9ce266e 2022-03-07 op }
7604 e9ce266e 2022-03-07 op
7605 e9ce266e 2022-03-07 op signal(SIGHUP, sighup);
7606 e9ce266e 2022-03-07 op signal(SIGINT, sigint);
7607 e9ce266e 2022-03-07 op signal(SIGQUIT, sigquit);
7608 e9ce266e 2022-03-07 op
7609 ca6444c5 2022-03-11 op if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
7610 ca6444c5 2022-03-11 op err = got_error_from_errno("lseek");
7611 ca6444c5 2022-03-11 op
7612 ca6444c5 2022-03-11 op if (err != NULL) {
7613 e9ce266e 2022-03-07 op close(*patchfd);
7614 ca6444c5 2022-03-11 op *patchfd = -1;
7615 ca6444c5 2022-03-11 op }
7616 ca6444c5 2022-03-11 op
7617 ca6444c5 2022-03-11 op return err;
7618 e9ce266e 2022-03-07 op }
7619 e9ce266e 2022-03-07 op
7620 e9ce266e 2022-03-07 op static const struct got_error *
7621 60aa1fa0 2022-03-17 op patch_progress(void *arg, const char *old, const char *new,
7622 60aa1fa0 2022-03-17 op unsigned char status, const struct got_error *error, long old_from,
7623 60aa1fa0 2022-03-17 op long old_lines, long new_from, long new_lines, long offset,
7624 60aa1fa0 2022-03-17 op const struct got_error *hunk_err)
7625 b22138f5 2022-03-16 op {
7626 b22138f5 2022-03-16 op const char *path = new == NULL ? old : new;
7627 b22138f5 2022-03-16 op
7628 b22138f5 2022-03-16 op while (*path == '/')
7629 b22138f5 2022-03-16 op path++;
7630 60aa1fa0 2022-03-17 op
7631 60aa1fa0 2022-03-17 op if (status != 0)
7632 60aa1fa0 2022-03-17 op printf("%c %s\n", status, path);
7633 60aa1fa0 2022-03-17 op
7634 60aa1fa0 2022-03-17 op if (error != NULL)
7635 60aa1fa0 2022-03-17 op fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7636 60aa1fa0 2022-03-17 op
7637 60aa1fa0 2022-03-17 op if (offset != 0 || hunk_err != NULL) {
7638 60aa1fa0 2022-03-17 op printf("@@ -%ld,%ld +%ld,%ld @@ ", old_from,
7639 60aa1fa0 2022-03-17 op old_lines, new_from, new_lines);
7640 60aa1fa0 2022-03-17 op if (hunk_err != NULL)
7641 60aa1fa0 2022-03-17 op printf("%s\n", hunk_err->msg);
7642 60aa1fa0 2022-03-17 op else
7643 60aa1fa0 2022-03-17 op printf("applied with offset %ld\n", offset);
7644 60aa1fa0 2022-03-17 op }
7645 60aa1fa0 2022-03-17 op
7646 b22138f5 2022-03-16 op return NULL;
7647 b22138f5 2022-03-16 op }
7648 b22138f5 2022-03-16 op
7649 b22138f5 2022-03-16 op static const struct got_error *
7650 e9ce266e 2022-03-07 op cmd_patch(int argc, char *argv[])
7651 e9ce266e 2022-03-07 op {
7652 e9ce266e 2022-03-07 op const struct got_error *error = NULL, *close_error = NULL;
7653 e9ce266e 2022-03-07 op struct got_worktree *worktree = NULL;
7654 e9ce266e 2022-03-07 op struct got_repository *repo = NULL;
7655 9d6cabd5 2022-04-07 op const char *errstr;
7656 e9ce266e 2022-03-07 op char *cwd = NULL;
7657 bad961bf 2022-04-23 op int ch, nop = 0, strip = -1, reverse = 0;
7658 e9ce266e 2022-03-07 op int patchfd;
7659 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7660 e9ce266e 2022-03-07 op
7661 bad961bf 2022-04-23 op while ((ch = getopt(argc, argv, "np:R")) != -1) {
7662 e9ce266e 2022-03-07 op switch (ch) {
7663 899fcfdf 2022-03-13 op case 'n':
7664 899fcfdf 2022-03-13 op nop = 1;
7665 899fcfdf 2022-03-13 op break;
7666 9d6cabd5 2022-04-07 op case 'p':
7667 9d6cabd5 2022-04-07 op strip = strtonum(optarg, 0, INT_MAX, &errstr);
7668 9d6cabd5 2022-04-07 op if (errstr != NULL)
7669 9d6cabd5 2022-04-07 op errx(1, "pathname strip count is %s: %s",
7670 9d6cabd5 2022-04-07 op errstr, optarg);
7671 bad961bf 2022-04-23 op break;
7672 bad961bf 2022-04-23 op case 'R':
7673 bad961bf 2022-04-23 op reverse = 1;
7674 9d6cabd5 2022-04-07 op break;
7675 e9ce266e 2022-03-07 op default:
7676 e9ce266e 2022-03-07 op usage_patch();
7677 e9ce266e 2022-03-07 op /* NOTREACHED */
7678 e9ce266e 2022-03-07 op }
7679 e9ce266e 2022-03-07 op }
7680 e9ce266e 2022-03-07 op
7681 e9ce266e 2022-03-07 op argc -= optind;
7682 e9ce266e 2022-03-07 op argv += optind;
7683 e9ce266e 2022-03-07 op
7684 e9ce266e 2022-03-07 op if (argc == 0) {
7685 e9ce266e 2022-03-07 op error = patch_from_stdin(&patchfd);
7686 e9ce266e 2022-03-07 op if (error)
7687 e9ce266e 2022-03-07 op return error;
7688 e9ce266e 2022-03-07 op } else if (argc == 1) {
7689 e9ce266e 2022-03-07 op patchfd = open(argv[0], O_RDONLY);
7690 e9ce266e 2022-03-07 op if (patchfd == -1) {
7691 e9ce266e 2022-03-07 op error = got_error_from_errno2("open", argv[0]);
7692 e9ce266e 2022-03-07 op return error;
7693 e9ce266e 2022-03-07 op }
7694 e9ce266e 2022-03-07 op } else
7695 e9ce266e 2022-03-07 op usage_patch();
7696 e9ce266e 2022-03-07 op
7697 e9ce266e 2022-03-07 op if ((cwd = getcwd(NULL, 0)) == NULL) {
7698 e9ce266e 2022-03-07 op error = got_error_from_errno("getcwd");
7699 e9ce266e 2022-03-07 op goto done;
7700 e9ce266e 2022-03-07 op }
7701 e9ce266e 2022-03-07 op
7702 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7703 0ae84acc 2022-06-15 tracey if (error != NULL)
7704 0ae84acc 2022-06-15 tracey goto done;
7705 0ae84acc 2022-06-15 tracey
7706 e9ce266e 2022-03-07 op error = got_worktree_open(&worktree, cwd);
7707 e9ce266e 2022-03-07 op if (error != NULL)
7708 e9ce266e 2022-03-07 op goto done;
7709 e9ce266e 2022-03-07 op
7710 e9ce266e 2022-03-07 op const char *repo_path = got_worktree_get_repo_path(worktree);
7711 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7712 e9ce266e 2022-03-07 op if (error != NULL)
7713 e9ce266e 2022-03-07 op goto done;
7714 e9ce266e 2022-03-07 op
7715 e9ce266e 2022-03-07 op error = apply_unveil(got_repo_get_path(repo), 0,
7716 728ed142 2022-04-19 op got_worktree_get_root_path(worktree));
7717 e9ce266e 2022-03-07 op if (error != NULL)
7718 e9ce266e 2022-03-07 op goto done;
7719 e9ce266e 2022-03-07 op
7720 e9ce266e 2022-03-07 op #ifndef PROFILE
7721 2be5e1a2 2022-03-16 op if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock",
7722 e9ce266e 2022-03-07 op NULL) == -1)
7723 e9ce266e 2022-03-07 op err(1, "pledge");
7724 e9ce266e 2022-03-07 op #endif
7725 e9ce266e 2022-03-07 op
7726 bad961bf 2022-04-23 op error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
7727 9d6cabd5 2022-04-07 op &patch_progress, NULL, check_cancelled, NULL);
7728 e9ce266e 2022-03-07 op
7729 e9ce266e 2022-03-07 op done:
7730 e9ce266e 2022-03-07 op if (repo) {
7731 e9ce266e 2022-03-07 op close_error = got_repo_close(repo);
7732 e9ce266e 2022-03-07 op if (error == NULL)
7733 e9ce266e 2022-03-07 op error = close_error;
7734 e9ce266e 2022-03-07 op }
7735 e9ce266e 2022-03-07 op if (worktree != NULL) {
7736 e9ce266e 2022-03-07 op close_error = got_worktree_close(worktree);
7737 e9ce266e 2022-03-07 op if (error == NULL)
7738 e9ce266e 2022-03-07 op error = close_error;
7739 e9ce266e 2022-03-07 op }
7740 0ae84acc 2022-06-15 tracey if (pack_fds) {
7741 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7742 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7743 0ae84acc 2022-06-15 tracey if (error == NULL)
7744 0ae84acc 2022-06-15 tracey error = pack_err;
7745 0ae84acc 2022-06-15 tracey }
7746 a129376b 2019-03-28 stsp free(cwd);
7747 a129376b 2019-03-28 stsp return error;
7748 a129376b 2019-03-28 stsp }
7749 a129376b 2019-03-28 stsp
7750 a129376b 2019-03-28 stsp __dead static void
7751 a129376b 2019-03-28 stsp usage_revert(void)
7752 a129376b 2019-03-28 stsp {
7753 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
7754 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
7755 a129376b 2019-03-28 stsp exit(1);
7756 a129376b 2019-03-28 stsp }
7757 a129376b 2019-03-28 stsp
7758 1ee397ad 2019-07-12 stsp static const struct got_error *
7759 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
7760 a129376b 2019-03-28 stsp {
7761 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
7762 fb9704af 2020-01-27 stsp return NULL;
7763 fb9704af 2020-01-27 stsp
7764 a129376b 2019-03-28 stsp while (path[0] == '/')
7765 a129376b 2019-03-28 stsp path++;
7766 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
7767 33aa809d 2019-08-08 stsp return NULL;
7768 33aa809d 2019-08-08 stsp }
7769 33aa809d 2019-08-08 stsp
7770 33aa809d 2019-08-08 stsp struct choose_patch_arg {
7771 33aa809d 2019-08-08 stsp FILE *patch_script_file;
7772 33aa809d 2019-08-08 stsp const char *action;
7773 33aa809d 2019-08-08 stsp };
7774 33aa809d 2019-08-08 stsp
7775 33aa809d 2019-08-08 stsp static const struct got_error *
7776 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
7777 33aa809d 2019-08-08 stsp int nchanges, const char *action)
7778 33aa809d 2019-08-08 stsp {
7779 90e25b1f 2022-06-08 stsp const struct got_error *err;
7780 33aa809d 2019-08-08 stsp char *line = NULL;
7781 33aa809d 2019-08-08 stsp size_t linesize = 0;
7782 33aa809d 2019-08-08 stsp ssize_t linelen;
7783 33aa809d 2019-08-08 stsp
7784 33aa809d 2019-08-08 stsp switch (status) {
7785 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
7786 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
7787 33aa809d 2019-08-08 stsp break;
7788 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
7789 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
7790 33aa809d 2019-08-08 stsp break;
7791 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
7792 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
7793 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
7794 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
7795 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
7796 33aa809d 2019-08-08 stsp printf("%s", line);
7797 90e25b1f 2022-06-08 stsp if (linelen == -1 && ferror(patch_file)) {
7798 90e25b1f 2022-06-08 stsp err = got_error_from_errno("getline");
7799 90e25b1f 2022-06-08 stsp free(line);
7800 90e25b1f 2022-06-08 stsp return err;
7801 90e25b1f 2022-06-08 stsp }
7802 90e25b1f 2022-06-08 stsp free(line);
7803 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
7804 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
7805 33aa809d 2019-08-08 stsp path, n, nchanges, action);
7806 33aa809d 2019-08-08 stsp break;
7807 33aa809d 2019-08-08 stsp default:
7808 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
7809 33aa809d 2019-08-08 stsp }
7810 33aa809d 2019-08-08 stsp
7811 33aa809d 2019-08-08 stsp return NULL;
7812 33aa809d 2019-08-08 stsp }
7813 33aa809d 2019-08-08 stsp
7814 33aa809d 2019-08-08 stsp static const struct got_error *
7815 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
7816 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
7817 33aa809d 2019-08-08 stsp {
7818 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
7819 33aa809d 2019-08-08 stsp char *line = NULL;
7820 33aa809d 2019-08-08 stsp size_t linesize = 0;
7821 33aa809d 2019-08-08 stsp ssize_t linelen;
7822 33aa809d 2019-08-08 stsp int resp = ' ';
7823 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
7824 33aa809d 2019-08-08 stsp
7825 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
7826 33aa809d 2019-08-08 stsp
7827 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
7828 33aa809d 2019-08-08 stsp char *nl;
7829 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
7830 33aa809d 2019-08-08 stsp a->action);
7831 33aa809d 2019-08-08 stsp if (err)
7832 33aa809d 2019-08-08 stsp return err;
7833 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
7834 33aa809d 2019-08-08 stsp if (linelen == -1) {
7835 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
7836 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
7837 33aa809d 2019-08-08 stsp return NULL;
7838 33aa809d 2019-08-08 stsp }
7839 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
7840 33aa809d 2019-08-08 stsp if (nl)
7841 33aa809d 2019-08-08 stsp *nl = '\0';
7842 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
7843 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
7844 33aa809d 2019-08-08 stsp printf("y\n");
7845 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
7846 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
7847 33aa809d 2019-08-08 stsp printf("n\n");
7848 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
7849 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
7850 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
7851 33aa809d 2019-08-08 stsp printf("q\n");
7852 33aa809d 2019-08-08 stsp } else
7853 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
7854 33aa809d 2019-08-08 stsp free(line);
7855 33aa809d 2019-08-08 stsp return NULL;
7856 33aa809d 2019-08-08 stsp }
7857 33aa809d 2019-08-08 stsp
7858 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
7859 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
7860 33aa809d 2019-08-08 stsp a->action);
7861 33aa809d 2019-08-08 stsp if (err)
7862 33aa809d 2019-08-08 stsp return err;
7863 33aa809d 2019-08-08 stsp resp = getchar();
7864 33aa809d 2019-08-08 stsp if (resp == '\n')
7865 33aa809d 2019-08-08 stsp resp = getchar();
7866 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
7867 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
7868 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
7869 33aa809d 2019-08-08 stsp resp = ' ';
7870 33aa809d 2019-08-08 stsp }
7871 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
7872 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
7873 33aa809d 2019-08-08 stsp resp = ' ';
7874 33aa809d 2019-08-08 stsp }
7875 33aa809d 2019-08-08 stsp }
7876 33aa809d 2019-08-08 stsp
7877 33aa809d 2019-08-08 stsp if (resp == 'y')
7878 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
7879 33aa809d 2019-08-08 stsp else if (resp == 'n')
7880 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
7881 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
7882 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
7883 33aa809d 2019-08-08 stsp
7884 1ee397ad 2019-07-12 stsp return NULL;
7885 a129376b 2019-03-28 stsp }
7886 a129376b 2019-03-28 stsp
7887 a129376b 2019-03-28 stsp static const struct got_error *
7888 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
7889 a129376b 2019-03-28 stsp {
7890 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
7891 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
7892 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
7893 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
7894 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
7895 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
7896 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
7897 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
7898 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
7899 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
7900 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7901 a129376b 2019-03-28 stsp
7902 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
7903 e20a8b6f 2019-06-04 stsp
7904 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
7905 a129376b 2019-03-28 stsp switch (ch) {
7906 33aa809d 2019-08-08 stsp case 'p':
7907 33aa809d 2019-08-08 stsp pflag = 1;
7908 33aa809d 2019-08-08 stsp break;
7909 33aa809d 2019-08-08 stsp case 'F':
7910 33aa809d 2019-08-08 stsp patch_script_path = optarg;
7911 33aa809d 2019-08-08 stsp break;
7912 0f6d7415 2019-08-08 stsp case 'R':
7913 0f6d7415 2019-08-08 stsp can_recurse = 1;
7914 0f6d7415 2019-08-08 stsp break;
7915 a129376b 2019-03-28 stsp default:
7916 a129376b 2019-03-28 stsp usage_revert();
7917 a129376b 2019-03-28 stsp /* NOTREACHED */
7918 a129376b 2019-03-28 stsp }
7919 a129376b 2019-03-28 stsp }
7920 a129376b 2019-03-28 stsp
7921 a129376b 2019-03-28 stsp argc -= optind;
7922 a129376b 2019-03-28 stsp argv += optind;
7923 a129376b 2019-03-28 stsp
7924 43012d58 2019-07-14 stsp #ifndef PROFILE
7925 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7926 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7927 43012d58 2019-07-14 stsp err(1, "pledge");
7928 43012d58 2019-07-14 stsp #endif
7929 e20a8b6f 2019-06-04 stsp if (argc < 1)
7930 a129376b 2019-03-28 stsp usage_revert();
7931 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
7932 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7933 a129376b 2019-03-28 stsp
7934 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
7935 a129376b 2019-03-28 stsp if (cwd == NULL) {
7936 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7937 a129376b 2019-03-28 stsp goto done;
7938 a129376b 2019-03-28 stsp }
7939 0ae84acc 2022-06-15 tracey
7940 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7941 0ae84acc 2022-06-15 tracey if (error != NULL)
7942 0ae84acc 2022-06-15 tracey goto done;
7943 0ae84acc 2022-06-15 tracey
7944 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
7945 fa51e947 2020-03-27 stsp if (error) {
7946 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7947 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
7948 2ec1f75b 2019-03-26 stsp goto done;
7949 fa51e947 2020-03-27 stsp }
7950 a129376b 2019-03-28 stsp
7951 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7952 0ae84acc 2022-06-15 tracey NULL, pack_fds);
7953 a129376b 2019-03-28 stsp if (error != NULL)
7954 a129376b 2019-03-28 stsp goto done;
7955 a129376b 2019-03-28 stsp
7956 33aa809d 2019-08-08 stsp if (patch_script_path) {
7957 00fe21f2 2021-12-31 stsp patch_script_file = fopen(patch_script_path, "re");
7958 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
7959 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
7960 33aa809d 2019-08-08 stsp patch_script_path);
7961 33aa809d 2019-08-08 stsp goto done;
7962 33aa809d 2019-08-08 stsp }
7963 33aa809d 2019-08-08 stsp }
7964 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
7965 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7966 a129376b 2019-03-28 stsp if (error)
7967 a129376b 2019-03-28 stsp goto done;
7968 a129376b 2019-03-28 stsp
7969 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7970 6d022e97 2019-08-04 stsp if (error)
7971 6d022e97 2019-08-04 stsp goto done;
7972 00db391e 2019-08-03 stsp
7973 0f6d7415 2019-08-08 stsp if (!can_recurse) {
7974 0f6d7415 2019-08-08 stsp char *ondisk_path;
7975 0f6d7415 2019-08-08 stsp struct stat sb;
7976 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
7977 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
7978 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
7979 62d463ca 2020-10-20 naddy pe->path) == -1) {
7980 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
7981 0f6d7415 2019-08-08 stsp goto done;
7982 0f6d7415 2019-08-08 stsp }
7983 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
7984 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
7985 0f6d7415 2019-08-08 stsp free(ondisk_path);
7986 0f6d7415 2019-08-08 stsp continue;
7987 0f6d7415 2019-08-08 stsp }
7988 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
7989 0f6d7415 2019-08-08 stsp ondisk_path);
7990 0f6d7415 2019-08-08 stsp free(ondisk_path);
7991 0f6d7415 2019-08-08 stsp goto done;
7992 0f6d7415 2019-08-08 stsp }
7993 0f6d7415 2019-08-08 stsp free(ondisk_path);
7994 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
7995 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
7996 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
7997 0f6d7415 2019-08-08 stsp goto done;
7998 0f6d7415 2019-08-08 stsp }
7999 0f6d7415 2019-08-08 stsp }
8000 0f6d7415 2019-08-08 stsp }
8001 0f6d7415 2019-08-08 stsp
8002 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8003 33aa809d 2019-08-08 stsp cpa.action = "revert";
8004 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8005 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
8006 2ec1f75b 2019-03-26 stsp done:
8007 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8008 33aa809d 2019-08-08 stsp error == NULL)
8009 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8010 1d0f4054 2021-06-17 stsp if (repo) {
8011 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8012 1d0f4054 2021-06-17 stsp if (error == NULL)
8013 1d0f4054 2021-06-17 stsp error = close_err;
8014 1d0f4054 2021-06-17 stsp }
8015 2ec1f75b 2019-03-26 stsp if (worktree)
8016 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
8017 0ae84acc 2022-06-15 tracey if (pack_fds) {
8018 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8019 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8020 0ae84acc 2022-06-15 tracey if (error == NULL)
8021 0ae84acc 2022-06-15 tracey error = pack_err;
8022 0ae84acc 2022-06-15 tracey }
8023 2ec1f75b 2019-03-26 stsp free(path);
8024 d00136be 2019-03-26 stsp free(cwd);
8025 6bad629b 2019-02-04 stsp return error;
8026 c4296144 2019-05-09 stsp }
8027 c4296144 2019-05-09 stsp
8028 c4296144 2019-05-09 stsp __dead static void
8029 c4296144 2019-05-09 stsp usage_commit(void)
8030 c4296144 2019-05-09 stsp {
8031 28cf319f 2021-01-28 stsp fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
8032 28cf319f 2021-01-28 stsp "[path ...]\n", getprogname());
8033 c4296144 2019-05-09 stsp exit(1);
8034 33ad4cbe 2019-05-12 jcs }
8035 33ad4cbe 2019-05-12 jcs
8036 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
8037 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
8038 28cf319f 2021-01-28 stsp const char *prepared_log;
8039 28cf319f 2021-01-28 stsp int non_interactive;
8040 e2ba3d07 2019-05-13 stsp const char *editor;
8041 e0870e44 2019-05-13 stsp const char *worktree_path;
8042 76d98825 2019-06-03 stsp const char *branch_name;
8043 314a6357 2019-05-13 stsp const char *repo_path;
8044 e0870e44 2019-05-13 stsp char *logmsg_path;
8045 e2ba3d07 2019-05-13 stsp
8046 e2ba3d07 2019-05-13 stsp };
8047 28cf319f 2021-01-28 stsp
8048 28cf319f 2021-01-28 stsp static const struct got_error *
8049 28cf319f 2021-01-28 stsp read_prepared_logmsg(char **logmsg, const char *path)
8050 28cf319f 2021-01-28 stsp {
8051 28cf319f 2021-01-28 stsp const struct got_error *err = NULL;
8052 28cf319f 2021-01-28 stsp FILE *f = NULL;
8053 28cf319f 2021-01-28 stsp struct stat sb;
8054 28cf319f 2021-01-28 stsp size_t r;
8055 28cf319f 2021-01-28 stsp
8056 28cf319f 2021-01-28 stsp *logmsg = NULL;
8057 28cf319f 2021-01-28 stsp memset(&sb, 0, sizeof(sb));
8058 28cf319f 2021-01-28 stsp
8059 00fe21f2 2021-12-31 stsp f = fopen(path, "re");
8060 28cf319f 2021-01-28 stsp if (f == NULL)
8061 28cf319f 2021-01-28 stsp return got_error_from_errno2("fopen", path);
8062 28cf319f 2021-01-28 stsp
8063 28cf319f 2021-01-28 stsp if (fstat(fileno(f), &sb) == -1) {
8064 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fstat", path);
8065 28cf319f 2021-01-28 stsp goto done;
8066 28cf319f 2021-01-28 stsp }
8067 28cf319f 2021-01-28 stsp if (sb.st_size == 0) {
8068 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8069 28cf319f 2021-01-28 stsp goto done;
8070 28cf319f 2021-01-28 stsp }
8071 28cf319f 2021-01-28 stsp
8072 28cf319f 2021-01-28 stsp *logmsg = malloc(sb.st_size + 1);
8073 28cf319f 2021-01-28 stsp if (*logmsg == NULL) {
8074 28cf319f 2021-01-28 stsp err = got_error_from_errno("malloc");
8075 28cf319f 2021-01-28 stsp goto done;
8076 28cf319f 2021-01-28 stsp }
8077 28cf319f 2021-01-28 stsp
8078 28cf319f 2021-01-28 stsp r = fread(*logmsg, 1, sb.st_size, f);
8079 28cf319f 2021-01-28 stsp if (r != sb.st_size) {
8080 28cf319f 2021-01-28 stsp if (ferror(f))
8081 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fread", path);
8082 28cf319f 2021-01-28 stsp else
8083 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_IO);
8084 28cf319f 2021-01-28 stsp goto done;
8085 28cf319f 2021-01-28 stsp }
8086 28cf319f 2021-01-28 stsp (*logmsg)[sb.st_size] = '\0';
8087 28cf319f 2021-01-28 stsp done:
8088 28cf319f 2021-01-28 stsp if (fclose(f) == EOF && err == NULL)
8089 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fclose", path);
8090 28cf319f 2021-01-28 stsp if (err) {
8091 28cf319f 2021-01-28 stsp free(*logmsg);
8092 28cf319f 2021-01-28 stsp *logmsg = NULL;
8093 28cf319f 2021-01-28 stsp }
8094 28cf319f 2021-01-28 stsp return err;
8095 28cf319f 2021-01-28 stsp
8096 28cf319f 2021-01-28 stsp }
8097 e0870e44 2019-05-13 stsp
8098 33ad4cbe 2019-05-12 jcs static const struct got_error *
8099 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
8100 33ad4cbe 2019-05-12 jcs void *arg)
8101 33ad4cbe 2019-05-12 jcs {
8102 76d98825 2019-06-03 stsp char *initial_content = NULL;
8103 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
8104 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
8105 e0870e44 2019-05-13 stsp char *template = NULL;
8106 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
8107 1601cb9f 2020-09-11 naddy int initial_content_len;
8108 97972933 2020-09-11 stsp int fd = -1;
8109 33ad4cbe 2019-05-12 jcs size_t len;
8110 33ad4cbe 2019-05-12 jcs
8111 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
8112 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8113 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
8114 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
8115 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
8116 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
8117 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
8118 33ad4cbe 2019-05-12 jcs return NULL;
8119 28cf319f 2021-01-28 stsp } else if (a->prepared_log != NULL && a->non_interactive)
8120 28cf319f 2021-01-28 stsp return read_prepared_logmsg(logmsg, a->prepared_log);
8121 33ad4cbe 2019-05-12 jcs
8122 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8123 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
8124 76d98825 2019-06-03 stsp
8125 28cf319f 2021-01-28 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
8126 28cf319f 2021-01-28 stsp if (err)
8127 28cf319f 2021-01-28 stsp goto done;
8128 28cf319f 2021-01-28 stsp
8129 28cf319f 2021-01-28 stsp if (a->prepared_log) {
8130 28cf319f 2021-01-28 stsp char *msg;
8131 28cf319f 2021-01-28 stsp err = read_prepared_logmsg(&msg, a->prepared_log);
8132 28cf319f 2021-01-28 stsp if (err)
8133 28cf319f 2021-01-28 stsp goto done;
8134 28cf319f 2021-01-28 stsp if (write(fd, msg, strlen(msg)) == -1) {
8135 28cf319f 2021-01-28 stsp err = got_error_from_errno2("write", a->logmsg_path);
8136 28cf319f 2021-01-28 stsp free(msg);
8137 28cf319f 2021-01-28 stsp goto done;
8138 28cf319f 2021-01-28 stsp }
8139 28cf319f 2021-01-28 stsp free(msg);
8140 28cf319f 2021-01-28 stsp }
8141 28cf319f 2021-01-28 stsp
8142 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
8143 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
8144 1601cb9f 2020-09-11 naddy a->branch_name);
8145 28cf319f 2021-01-28 stsp if (initial_content_len == -1) {
8146 28cf319f 2021-01-28 stsp err = got_error_from_errno("asprintf");
8147 e0870e44 2019-05-13 stsp goto done;
8148 28cf319f 2021-01-28 stsp }
8149 33ad4cbe 2019-05-12 jcs
8150 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
8151 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
8152 97972933 2020-09-11 stsp goto done;
8153 97972933 2020-09-11 stsp }
8154 33ad4cbe 2019-05-12 jcs
8155 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
8156 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
8157 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
8158 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
8159 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
8160 33ad4cbe 2019-05-12 jcs }
8161 33ad4cbe 2019-05-12 jcs
8162 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8163 28cf319f 2021-01-28 stsp initial_content_len, a->prepared_log ? 0 : 1);
8164 33ad4cbe 2019-05-12 jcs done:
8165 76d98825 2019-06-03 stsp free(initial_content);
8166 e0870e44 2019-05-13 stsp free(template);
8167 314a6357 2019-05-13 stsp
8168 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
8169 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
8170 97972933 2020-09-11 stsp
8171 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
8172 59f86c76 2020-09-11 stsp if (err == NULL)
8173 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
8174 59f86c76 2020-09-11 stsp if (err) {
8175 59f86c76 2020-09-11 stsp free(*logmsg);
8176 59f86c76 2020-09-11 stsp *logmsg = NULL;
8177 3ce1b845 2019-07-15 stsp }
8178 33ad4cbe 2019-05-12 jcs return err;
8179 6bad629b 2019-02-04 stsp }
8180 c4296144 2019-05-09 stsp
8181 c4296144 2019-05-09 stsp static const struct got_error *
8182 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
8183 c4296144 2019-05-09 stsp {
8184 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
8185 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
8186 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
8187 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
8188 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
8189 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
8190 28cf319f 2021-01-28 stsp char *prepared_logmsg = NULL;
8191 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
8192 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
8193 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
8194 f259c4c1 2021-09-24 stsp int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
8195 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
8196 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8197 5c1e53bc 2019-07-28 stsp
8198 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
8199 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
8200 c4296144 2019-05-09 stsp
8201 28cf319f 2021-01-28 stsp while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
8202 c4296144 2019-05-09 stsp switch (ch) {
8203 28cf319f 2021-01-28 stsp case 'F':
8204 28cf319f 2021-01-28 stsp if (logmsg != NULL)
8205 28cf319f 2021-01-28 stsp option_conflict('F', 'm');
8206 28cf319f 2021-01-28 stsp prepared_logmsg = realpath(optarg, NULL);
8207 28cf319f 2021-01-28 stsp if (prepared_logmsg == NULL)
8208 28cf319f 2021-01-28 stsp return got_error_from_errno2("realpath",
8209 28cf319f 2021-01-28 stsp optarg);
8210 28cf319f 2021-01-28 stsp break;
8211 c4296144 2019-05-09 stsp case 'm':
8212 28cf319f 2021-01-28 stsp if (prepared_logmsg)
8213 28cf319f 2021-01-28 stsp option_conflict('m', 'F');
8214 c4296144 2019-05-09 stsp logmsg = optarg;
8215 28cf319f 2021-01-28 stsp break;
8216 28cf319f 2021-01-28 stsp case 'N':
8217 28cf319f 2021-01-28 stsp non_interactive = 1;
8218 c4296144 2019-05-09 stsp break;
8219 35213c7c 2020-07-23 stsp case 'S':
8220 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
8221 35213c7c 2020-07-23 stsp break;
8222 c4296144 2019-05-09 stsp default:
8223 c4296144 2019-05-09 stsp usage_commit();
8224 c4296144 2019-05-09 stsp /* NOTREACHED */
8225 c4296144 2019-05-09 stsp }
8226 c4296144 2019-05-09 stsp }
8227 c4296144 2019-05-09 stsp
8228 c4296144 2019-05-09 stsp argc -= optind;
8229 c4296144 2019-05-09 stsp argv += optind;
8230 c4296144 2019-05-09 stsp
8231 43012d58 2019-07-14 stsp #ifndef PROFILE
8232 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8233 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8234 43012d58 2019-07-14 stsp err(1, "pledge");
8235 43012d58 2019-07-14 stsp #endif
8236 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
8237 c4296144 2019-05-09 stsp if (cwd == NULL) {
8238 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
8239 c4296144 2019-05-09 stsp goto done;
8240 c4296144 2019-05-09 stsp }
8241 0ae84acc 2022-06-15 tracey
8242 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8243 0ae84acc 2022-06-15 tracey if (error != NULL)
8244 0ae84acc 2022-06-15 tracey goto done;
8245 0ae84acc 2022-06-15 tracey
8246 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
8247 fa51e947 2020-03-27 stsp if (error) {
8248 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8249 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
8250 7d5807f4 2019-07-11 stsp goto done;
8251 fa51e947 2020-03-27 stsp }
8252 7d5807f4 2019-07-11 stsp
8253 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8254 c4296144 2019-05-09 stsp if (error)
8255 a698f62e 2019-07-25 stsp goto done;
8256 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
8257 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
8258 c4296144 2019-05-09 stsp goto done;
8259 a698f62e 2019-07-25 stsp }
8260 5c1e53bc 2019-07-28 stsp
8261 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
8262 916f288c 2019-07-30 stsp worktree);
8263 5c1e53bc 2019-07-28 stsp if (error)
8264 5c1e53bc 2019-07-28 stsp goto done;
8265 c4296144 2019-05-09 stsp
8266 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
8267 c9956ddf 2019-09-08 stsp if (error)
8268 c9956ddf 2019-09-08 stsp goto done;
8269 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8270 0ae84acc 2022-06-15 tracey gitconfig_path, pack_fds);
8271 c4296144 2019-05-09 stsp if (error != NULL)
8272 c4296144 2019-05-09 stsp goto done;
8273 c4296144 2019-05-09 stsp
8274 f259c4c1 2021-09-24 stsp error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
8275 f259c4c1 2021-09-24 stsp if (error)
8276 f259c4c1 2021-09-24 stsp goto done;
8277 f259c4c1 2021-09-24 stsp if (merge_in_progress) {
8278 f259c4c1 2021-09-24 stsp error = got_error(GOT_ERR_MERGE_BUSY);
8279 f259c4c1 2021-09-24 stsp goto done;
8280 f259c4c1 2021-09-24 stsp }
8281 f259c4c1 2021-09-24 stsp
8282 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
8283 aba9c984 2019-09-08 stsp if (error)
8284 aba9c984 2019-09-08 stsp return error;
8285 aba9c984 2019-09-08 stsp
8286 314a6357 2019-05-13 stsp /*
8287 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
8288 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
8289 314a6357 2019-05-13 stsp */
8290 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
8291 314a6357 2019-05-13 stsp error = get_editor(&editor);
8292 314a6357 2019-05-13 stsp else
8293 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8294 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
8295 c4296144 2019-05-09 stsp if (error)
8296 c4296144 2019-05-09 stsp goto done;
8297 c4296144 2019-05-09 stsp
8298 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8299 087fb88c 2019-08-04 stsp if (error)
8300 087fb88c 2019-08-04 stsp goto done;
8301 087fb88c 2019-08-04 stsp
8302 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
8303 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
8304 28cf319f 2021-01-28 stsp cl_arg.prepared_log = prepared_logmsg;
8305 28cf319f 2021-01-28 stsp cl_arg.non_interactive = non_interactive;
8306 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8307 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
8308 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
8309 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
8310 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
8311 916f288c 2019-07-30 stsp goto done;
8312 916f288c 2019-07-30 stsp }
8313 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
8314 916f288c 2019-07-30 stsp }
8315 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
8316 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
8317 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
8318 35213c7c 2020-07-23 stsp print_status, NULL, repo);
8319 e0870e44 2019-05-13 stsp if (error) {
8320 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8321 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
8322 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
8323 c4296144 2019-05-09 stsp goto done;
8324 e0870e44 2019-05-13 stsp }
8325 c4296144 2019-05-09 stsp
8326 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
8327 c4296144 2019-05-09 stsp if (error)
8328 c4296144 2019-05-09 stsp goto done;
8329 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
8330 c4296144 2019-05-09 stsp done:
8331 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
8332 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
8333 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
8334 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8335 7266f21f 2019-10-21 stsp error == NULL)
8336 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8337 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
8338 1d0f4054 2021-06-17 stsp if (repo) {
8339 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8340 1d0f4054 2021-06-17 stsp if (error == NULL)
8341 1d0f4054 2021-06-17 stsp error = close_err;
8342 1d0f4054 2021-06-17 stsp }
8343 c4296144 2019-05-09 stsp if (worktree)
8344 c4296144 2019-05-09 stsp got_worktree_close(worktree);
8345 0ae84acc 2022-06-15 tracey if (pack_fds) {
8346 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8347 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8348 0ae84acc 2022-06-15 tracey if (error == NULL)
8349 0ae84acc 2022-06-15 tracey error = pack_err;
8350 0ae84acc 2022-06-15 tracey }
8351 c4296144 2019-05-09 stsp free(cwd);
8352 c4296144 2019-05-09 stsp free(id_str);
8353 c9956ddf 2019-09-08 stsp free(gitconfig_path);
8354 e2ba3d07 2019-05-13 stsp free(editor);
8355 aba9c984 2019-09-08 stsp free(author);
8356 28cf319f 2021-01-28 stsp free(prepared_logmsg);
8357 f8a36e22 2021-08-26 stsp return error;
8358 f8a36e22 2021-08-26 stsp }
8359 f8a36e22 2021-08-26 stsp
8360 f8a36e22 2021-08-26 stsp __dead static void
8361 f8a36e22 2021-08-26 stsp usage_send(void)
8362 f8a36e22 2021-08-26 stsp {
8363 f8a36e22 2021-08-26 stsp fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
8364 f8a36e22 2021-08-26 stsp "[-r repository-path] [-t tag] [-T] [-q] [-v] "
8365 f8a36e22 2021-08-26 stsp "[remote-repository]\n", getprogname());
8366 f8a36e22 2021-08-26 stsp exit(1);
8367 f8a36e22 2021-08-26 stsp }
8368 f8a36e22 2021-08-26 stsp
8369 b8af7c06 2022-03-15 stsp static void
8370 b8af7c06 2022-03-15 stsp print_load_info(int print_colored, int print_found, int print_trees,
8371 b8af7c06 2022-03-15 stsp int ncolored, int nfound, int ntrees)
8372 b8af7c06 2022-03-15 stsp {
8373 b8af7c06 2022-03-15 stsp if (print_colored) {
8374 b8af7c06 2022-03-15 stsp printf("%d commit%s colored", ncolored,
8375 b8af7c06 2022-03-15 stsp ncolored == 1 ? "" : "s");
8376 b8af7c06 2022-03-15 stsp }
8377 b8af7c06 2022-03-15 stsp if (print_found) {
8378 b8af7c06 2022-03-15 stsp printf("%s%d object%s found",
8379 b8af7c06 2022-03-15 stsp ncolored > 0 ? "; " : "",
8380 b8af7c06 2022-03-15 stsp nfound, nfound == 1 ? "" : "s");
8381 b8af7c06 2022-03-15 stsp }
8382 b8af7c06 2022-03-15 stsp if (print_trees) {
8383 b8af7c06 2022-03-15 stsp printf("; %d tree%s scanned", ntrees,
8384 b8af7c06 2022-03-15 stsp ntrees == 1 ? "" : "s");
8385 b8af7c06 2022-03-15 stsp }
8386 b8af7c06 2022-03-15 stsp }
8387 b8af7c06 2022-03-15 stsp
8388 f8a36e22 2021-08-26 stsp struct got_send_progress_arg {
8389 f8a36e22 2021-08-26 stsp char last_scaled_packsize[FMT_SCALED_STRSIZE];
8390 f8a36e22 2021-08-26 stsp int verbosity;
8391 b8af7c06 2022-03-15 stsp int last_ncolored;
8392 b8af7c06 2022-03-15 stsp int last_nfound;
8393 b8af7c06 2022-03-15 stsp int last_ntrees;
8394 b8af7c06 2022-03-15 stsp int loading_done;
8395 f8a36e22 2021-08-26 stsp int last_ncommits;
8396 f8a36e22 2021-08-26 stsp int last_nobj_total;
8397 f8a36e22 2021-08-26 stsp int last_p_deltify;
8398 f8a36e22 2021-08-26 stsp int last_p_written;
8399 f8a36e22 2021-08-26 stsp int last_p_sent;
8400 f8a36e22 2021-08-26 stsp int printed_something;
8401 f8a36e22 2021-08-26 stsp int sent_something;
8402 f8a36e22 2021-08-26 stsp struct got_pathlist_head *delete_branches;
8403 f8a36e22 2021-08-26 stsp };
8404 f8a36e22 2021-08-26 stsp
8405 f8a36e22 2021-08-26 stsp static const struct got_error *
8406 b8af7c06 2022-03-15 stsp send_progress(void *arg, int ncolored, int nfound, int ntrees,
8407 b8af7c06 2022-03-15 stsp off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
8408 b8af7c06 2022-03-15 stsp int nobj_written, off_t bytes_sent, const char *refname, int success)
8409 f8a36e22 2021-08-26 stsp {
8410 f8a36e22 2021-08-26 stsp struct got_send_progress_arg *a = arg;
8411 f8a36e22 2021-08-26 stsp char scaled_packsize[FMT_SCALED_STRSIZE];
8412 f8a36e22 2021-08-26 stsp char scaled_sent[FMT_SCALED_STRSIZE];
8413 f8a36e22 2021-08-26 stsp int p_deltify = 0, p_written = 0, p_sent = 0;
8414 b8af7c06 2022-03-15 stsp int print_colored = 0, print_found = 0, print_trees = 0;
8415 f8a36e22 2021-08-26 stsp int print_searching = 0, print_total = 0;
8416 f8a36e22 2021-08-26 stsp int print_deltify = 0, print_written = 0, print_sent = 0;
8417 f8a36e22 2021-08-26 stsp
8418 f8a36e22 2021-08-26 stsp if (a->verbosity < 0)
8419 f8a36e22 2021-08-26 stsp return NULL;
8420 f8a36e22 2021-08-26 stsp
8421 f8a36e22 2021-08-26 stsp if (refname) {
8422 f8a36e22 2021-08-26 stsp const char *status = success ? "accepted" : "rejected";
8423 f8a36e22 2021-08-26 stsp
8424 f8a36e22 2021-08-26 stsp if (success) {
8425 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
8426 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, a->delete_branches, entry) {
8427 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
8428 f8a36e22 2021-08-26 stsp if (got_path_cmp(branchname, refname,
8429 f8a36e22 2021-08-26 stsp strlen(branchname), strlen(refname)) == 0) {
8430 f8a36e22 2021-08-26 stsp status = "deleted";
8431 27b75514 2021-08-28 stsp a->sent_something = 1;
8432 f8a36e22 2021-08-26 stsp break;
8433 f8a36e22 2021-08-26 stsp }
8434 f8a36e22 2021-08-26 stsp }
8435 f8a36e22 2021-08-26 stsp }
8436 f8a36e22 2021-08-26 stsp
8437 27b75514 2021-08-28 stsp if (a->printed_something)
8438 27b75514 2021-08-28 stsp putchar('\n');
8439 27b75514 2021-08-28 stsp printf("Server has %s %s", status, refname);
8440 f8a36e22 2021-08-26 stsp a->printed_something = 1;
8441 f8a36e22 2021-08-26 stsp return NULL;
8442 f8a36e22 2021-08-26 stsp }
8443 f8a36e22 2021-08-26 stsp
8444 b8af7c06 2022-03-15 stsp if (a->last_ncolored != ncolored) {
8445 b8af7c06 2022-03-15 stsp print_colored = 1;
8446 b8af7c06 2022-03-15 stsp a->last_ncolored = ncolored;
8447 b8af7c06 2022-03-15 stsp }
8448 b8af7c06 2022-03-15 stsp
8449 b8af7c06 2022-03-15 stsp if (a->last_nfound != nfound) {
8450 b8af7c06 2022-03-15 stsp print_colored = 1;
8451 b8af7c06 2022-03-15 stsp print_found = 1;
8452 b8af7c06 2022-03-15 stsp a->last_nfound = nfound;
8453 b8af7c06 2022-03-15 stsp }
8454 b8af7c06 2022-03-15 stsp
8455 b8af7c06 2022-03-15 stsp if (a->last_ntrees != ntrees) {
8456 b8af7c06 2022-03-15 stsp print_colored = 1;
8457 b8af7c06 2022-03-15 stsp print_found = 1;
8458 b8af7c06 2022-03-15 stsp print_trees = 1;
8459 b8af7c06 2022-03-15 stsp a->last_ntrees = ntrees;
8460 b8af7c06 2022-03-15 stsp }
8461 b8af7c06 2022-03-15 stsp
8462 b8af7c06 2022-03-15 stsp if ((print_colored || print_found || print_trees) &&
8463 b8af7c06 2022-03-15 stsp !a->loading_done) {
8464 b8af7c06 2022-03-15 stsp printf("\r");
8465 b8af7c06 2022-03-15 stsp print_load_info(print_colored, print_found, print_trees,
8466 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees);
8467 b8af7c06 2022-03-15 stsp a->printed_something = 1;
8468 b8af7c06 2022-03-15 stsp fflush(stdout);
8469 b8af7c06 2022-03-15 stsp return NULL;
8470 b8af7c06 2022-03-15 stsp } else if (!a->loading_done) {
8471 b8af7c06 2022-03-15 stsp printf("\r");
8472 b8af7c06 2022-03-15 stsp print_load_info(1, 1, 1, ncolored, nfound, ntrees);
8473 b8af7c06 2022-03-15 stsp printf("\n");
8474 b8af7c06 2022-03-15 stsp a->loading_done = 1;
8475 b8af7c06 2022-03-15 stsp }
8476 b8af7c06 2022-03-15 stsp
8477 f8a36e22 2021-08-26 stsp if (fmt_scaled(packfile_size, scaled_packsize) == -1)
8478 f8a36e22 2021-08-26 stsp return got_error_from_errno("fmt_scaled");
8479 f8a36e22 2021-08-26 stsp if (fmt_scaled(bytes_sent, scaled_sent) == -1)
8480 f8a36e22 2021-08-26 stsp return got_error_from_errno("fmt_scaled");
8481 f8a36e22 2021-08-26 stsp
8482 f8a36e22 2021-08-26 stsp if (a->last_ncommits != ncommits) {
8483 f8a36e22 2021-08-26 stsp print_searching = 1;
8484 f8a36e22 2021-08-26 stsp a->last_ncommits = ncommits;
8485 f8a36e22 2021-08-26 stsp }
8486 f8a36e22 2021-08-26 stsp
8487 f8a36e22 2021-08-26 stsp if (a->last_nobj_total != nobj_total) {
8488 f8a36e22 2021-08-26 stsp print_searching = 1;
8489 f8a36e22 2021-08-26 stsp print_total = 1;
8490 f8a36e22 2021-08-26 stsp a->last_nobj_total = nobj_total;
8491 f8a36e22 2021-08-26 stsp }
8492 f8a36e22 2021-08-26 stsp
8493 f8a36e22 2021-08-26 stsp if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
8494 f8a36e22 2021-08-26 stsp strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
8495 f8a36e22 2021-08-26 stsp if (strlcpy(a->last_scaled_packsize, scaled_packsize,
8496 f8a36e22 2021-08-26 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
8497 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
8498 f8a36e22 2021-08-26 stsp }
8499 f8a36e22 2021-08-26 stsp
8500 f8a36e22 2021-08-26 stsp if (nobj_deltify > 0 || nobj_written > 0) {
8501 f8a36e22 2021-08-26 stsp if (nobj_deltify > 0) {
8502 f8a36e22 2021-08-26 stsp p_deltify = (nobj_deltify * 100) / nobj_total;
8503 f8a36e22 2021-08-26 stsp if (p_deltify != a->last_p_deltify) {
8504 f8a36e22 2021-08-26 stsp a->last_p_deltify = p_deltify;
8505 f8a36e22 2021-08-26 stsp print_searching = 1;
8506 f8a36e22 2021-08-26 stsp print_total = 1;
8507 f8a36e22 2021-08-26 stsp print_deltify = 1;
8508 f8a36e22 2021-08-26 stsp }
8509 f8a36e22 2021-08-26 stsp }
8510 f8a36e22 2021-08-26 stsp if (nobj_written > 0) {
8511 f8a36e22 2021-08-26 stsp p_written = (nobj_written * 100) / nobj_total;
8512 f8a36e22 2021-08-26 stsp if (p_written != a->last_p_written) {
8513 f8a36e22 2021-08-26 stsp a->last_p_written = p_written;
8514 f8a36e22 2021-08-26 stsp print_searching = 1;
8515 f8a36e22 2021-08-26 stsp print_total = 1;
8516 f8a36e22 2021-08-26 stsp print_deltify = 1;
8517 f8a36e22 2021-08-26 stsp print_written = 1;
8518 f8a36e22 2021-08-26 stsp }
8519 f8a36e22 2021-08-26 stsp }
8520 f8a36e22 2021-08-26 stsp }
8521 f8a36e22 2021-08-26 stsp
8522 f8a36e22 2021-08-26 stsp if (bytes_sent > 0) {
8523 f8a36e22 2021-08-26 stsp p_sent = (bytes_sent * 100) / packfile_size;
8524 f8a36e22 2021-08-26 stsp if (p_sent != a->last_p_sent) {
8525 f8a36e22 2021-08-26 stsp a->last_p_sent = p_sent;
8526 f8a36e22 2021-08-26 stsp print_searching = 1;
8527 f8a36e22 2021-08-26 stsp print_total = 1;
8528 f8a36e22 2021-08-26 stsp print_deltify = 1;
8529 f8a36e22 2021-08-26 stsp print_written = 1;
8530 f8a36e22 2021-08-26 stsp print_sent = 1;
8531 f8a36e22 2021-08-26 stsp }
8532 f8a36e22 2021-08-26 stsp a->sent_something = 1;
8533 f8a36e22 2021-08-26 stsp }
8534 f8a36e22 2021-08-26 stsp
8535 f8a36e22 2021-08-26 stsp if (print_searching || print_total || print_deltify || print_written ||
8536 f8a36e22 2021-08-26 stsp print_sent)
8537 f8a36e22 2021-08-26 stsp printf("\r");
8538 f8a36e22 2021-08-26 stsp if (print_searching)
8539 f8a36e22 2021-08-26 stsp printf("packing %d reference%s", ncommits,
8540 f8a36e22 2021-08-26 stsp ncommits == 1 ? "" : "s");
8541 f8a36e22 2021-08-26 stsp if (print_total)
8542 f8a36e22 2021-08-26 stsp printf("; %d object%s", nobj_total,
8543 f8a36e22 2021-08-26 stsp nobj_total == 1 ? "" : "s");
8544 f8a36e22 2021-08-26 stsp if (print_deltify)
8545 f8a36e22 2021-08-26 stsp printf("; deltify: %d%%", p_deltify);
8546 f8a36e22 2021-08-26 stsp if (print_sent)
8547 b5934965 2022-02-12 naddy printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8548 f8a36e22 2021-08-26 stsp scaled_packsize, p_sent);
8549 f8a36e22 2021-08-26 stsp else if (print_written)
8550 b5934965 2022-02-12 naddy printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8551 f8a36e22 2021-08-26 stsp scaled_packsize, p_written);
8552 f8a36e22 2021-08-26 stsp if (print_searching || print_total || print_deltify ||
8553 f8a36e22 2021-08-26 stsp print_written || print_sent) {
8554 f8a36e22 2021-08-26 stsp a->printed_something = 1;
8555 f8a36e22 2021-08-26 stsp fflush(stdout);
8556 f8a36e22 2021-08-26 stsp }
8557 f8a36e22 2021-08-26 stsp return NULL;
8558 f8a36e22 2021-08-26 stsp }
8559 f8a36e22 2021-08-26 stsp
8560 f8a36e22 2021-08-26 stsp static const struct got_error *
8561 f8a36e22 2021-08-26 stsp cmd_send(int argc, char *argv[])
8562 f8a36e22 2021-08-26 stsp {
8563 f8a36e22 2021-08-26 stsp const struct got_error *error = NULL;
8564 f8a36e22 2021-08-26 stsp char *cwd = NULL, *repo_path = NULL;
8565 f8a36e22 2021-08-26 stsp const char *remote_name;
8566 f8a36e22 2021-08-26 stsp char *proto = NULL, *host = NULL, *port = NULL;
8567 f8a36e22 2021-08-26 stsp char *repo_name = NULL, *server_path = NULL;
8568 f8a36e22 2021-08-26 stsp const struct got_remote_repo *remotes, *remote = NULL;
8569 f8a36e22 2021-08-26 stsp int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
8570 f8a36e22 2021-08-26 stsp struct got_repository *repo = NULL;
8571 f8a36e22 2021-08-26 stsp struct got_worktree *worktree = NULL;
8572 f8a36e22 2021-08-26 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
8573 f8a36e22 2021-08-26 stsp struct got_pathlist_head branches;
8574 f8a36e22 2021-08-26 stsp struct got_pathlist_head tags;
8575 f8a36e22 2021-08-26 stsp struct got_reflist_head all_branches;
8576 f8a36e22 2021-08-26 stsp struct got_reflist_head all_tags;
8577 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_args;
8578 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_branches;
8579 f8a36e22 2021-08-26 stsp struct got_reflist_entry *re;
8580 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
8581 f8a36e22 2021-08-26 stsp int i, ch, sendfd = -1, sendstatus;
8582 f8a36e22 2021-08-26 stsp pid_t sendpid = -1;
8583 f8a36e22 2021-08-26 stsp struct got_send_progress_arg spa;
8584 f8a36e22 2021-08-26 stsp int verbosity = 0, overwrite_refs = 0;
8585 f8a36e22 2021-08-26 stsp int send_all_branches = 0, send_all_tags = 0;
8586 f8a36e22 2021-08-26 stsp struct got_reference *ref = NULL;
8587 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8588 f8a36e22 2021-08-26 stsp
8589 f8a36e22 2021-08-26 stsp TAILQ_INIT(&branches);
8590 f8a36e22 2021-08-26 stsp TAILQ_INIT(&tags);
8591 f8a36e22 2021-08-26 stsp TAILQ_INIT(&all_branches);
8592 f8a36e22 2021-08-26 stsp TAILQ_INIT(&all_tags);
8593 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_args);
8594 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_branches);
8595 f8a36e22 2021-08-26 stsp
8596 f8a36e22 2021-08-26 stsp while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
8597 f8a36e22 2021-08-26 stsp switch (ch) {
8598 f8a36e22 2021-08-26 stsp case 'a':
8599 f8a36e22 2021-08-26 stsp send_all_branches = 1;
8600 f8a36e22 2021-08-26 stsp break;
8601 f8a36e22 2021-08-26 stsp case 'b':
8602 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches, optarg, NULL);
8603 f8a36e22 2021-08-26 stsp if (error)
8604 f8a36e22 2021-08-26 stsp return error;
8605 f8a36e22 2021-08-26 stsp nbranches++;
8606 f8a36e22 2021-08-26 stsp break;
8607 f8a36e22 2021-08-26 stsp case 'd':
8608 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&delete_args, optarg, NULL);
8609 f8a36e22 2021-08-26 stsp if (error)
8610 f8a36e22 2021-08-26 stsp return error;
8611 f8a36e22 2021-08-26 stsp break;
8612 f8a36e22 2021-08-26 stsp case 'f':
8613 f8a36e22 2021-08-26 stsp overwrite_refs = 1;
8614 f8a36e22 2021-08-26 stsp break;
8615 f8a36e22 2021-08-26 stsp case 'r':
8616 f8a36e22 2021-08-26 stsp repo_path = realpath(optarg, NULL);
8617 f8a36e22 2021-08-26 stsp if (repo_path == NULL)
8618 f8a36e22 2021-08-26 stsp return got_error_from_errno2("realpath",
8619 f8a36e22 2021-08-26 stsp optarg);
8620 f8a36e22 2021-08-26 stsp got_path_strip_trailing_slashes(repo_path);
8621 f8a36e22 2021-08-26 stsp break;
8622 f8a36e22 2021-08-26 stsp case 't':
8623 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&tags, optarg, NULL);
8624 f8a36e22 2021-08-26 stsp if (error)
8625 f8a36e22 2021-08-26 stsp return error;
8626 f8a36e22 2021-08-26 stsp ntags++;
8627 f8a36e22 2021-08-26 stsp break;
8628 f8a36e22 2021-08-26 stsp case 'T':
8629 f8a36e22 2021-08-26 stsp send_all_tags = 1;
8630 f8a36e22 2021-08-26 stsp break;
8631 f8a36e22 2021-08-26 stsp case 'v':
8632 f8a36e22 2021-08-26 stsp if (verbosity < 0)
8633 f8a36e22 2021-08-26 stsp verbosity = 0;
8634 f8a36e22 2021-08-26 stsp else if (verbosity < 3)
8635 f8a36e22 2021-08-26 stsp verbosity++;
8636 f8a36e22 2021-08-26 stsp break;
8637 f8a36e22 2021-08-26 stsp case 'q':
8638 f8a36e22 2021-08-26 stsp verbosity = -1;
8639 f8a36e22 2021-08-26 stsp break;
8640 f8a36e22 2021-08-26 stsp default:
8641 f8a36e22 2021-08-26 stsp usage_send();
8642 f8a36e22 2021-08-26 stsp /* NOTREACHED */
8643 f8a36e22 2021-08-26 stsp }
8644 f8a36e22 2021-08-26 stsp }
8645 f8a36e22 2021-08-26 stsp argc -= optind;
8646 f8a36e22 2021-08-26 stsp argv += optind;
8647 f8a36e22 2021-08-26 stsp
8648 f8a36e22 2021-08-26 stsp if (send_all_branches && !TAILQ_EMPTY(&branches))
8649 f8a36e22 2021-08-26 stsp option_conflict('a', 'b');
8650 f8a36e22 2021-08-26 stsp if (send_all_tags && !TAILQ_EMPTY(&tags))
8651 f8a36e22 2021-08-26 stsp option_conflict('T', 't');
8652 f8a36e22 2021-08-26 stsp
8653 f8a36e22 2021-08-26 stsp
8654 f8a36e22 2021-08-26 stsp if (argc == 0)
8655 f8a36e22 2021-08-26 stsp remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
8656 f8a36e22 2021-08-26 stsp else if (argc == 1)
8657 f8a36e22 2021-08-26 stsp remote_name = argv[0];
8658 f8a36e22 2021-08-26 stsp else
8659 f8a36e22 2021-08-26 stsp usage_send();
8660 f8a36e22 2021-08-26 stsp
8661 f8a36e22 2021-08-26 stsp cwd = getcwd(NULL, 0);
8662 f8a36e22 2021-08-26 stsp if (cwd == NULL) {
8663 f8a36e22 2021-08-26 stsp error = got_error_from_errno("getcwd");
8664 f8a36e22 2021-08-26 stsp goto done;
8665 f8a36e22 2021-08-26 stsp }
8666 0ae84acc 2022-06-15 tracey
8667 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8668 0ae84acc 2022-06-15 tracey if (error != NULL)
8669 0ae84acc 2022-06-15 tracey goto done;
8670 f8a36e22 2021-08-26 stsp
8671 f8a36e22 2021-08-26 stsp if (repo_path == NULL) {
8672 f8a36e22 2021-08-26 stsp error = got_worktree_open(&worktree, cwd);
8673 f8a36e22 2021-08-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8674 f8a36e22 2021-08-26 stsp goto done;
8675 f8a36e22 2021-08-26 stsp else
8676 f8a36e22 2021-08-26 stsp error = NULL;
8677 f8a36e22 2021-08-26 stsp if (worktree) {
8678 f8a36e22 2021-08-26 stsp repo_path =
8679 f8a36e22 2021-08-26 stsp strdup(got_worktree_get_repo_path(worktree));
8680 f8a36e22 2021-08-26 stsp if (repo_path == NULL)
8681 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
8682 f8a36e22 2021-08-26 stsp if (error)
8683 f8a36e22 2021-08-26 stsp goto done;
8684 f8a36e22 2021-08-26 stsp } else {
8685 f8a36e22 2021-08-26 stsp repo_path = strdup(cwd);
8686 f8a36e22 2021-08-26 stsp if (repo_path == NULL) {
8687 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
8688 f8a36e22 2021-08-26 stsp goto done;
8689 f8a36e22 2021-08-26 stsp }
8690 f8a36e22 2021-08-26 stsp }
8691 f8a36e22 2021-08-26 stsp }
8692 f8a36e22 2021-08-26 stsp
8693 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8694 f8a36e22 2021-08-26 stsp if (error)
8695 f8a36e22 2021-08-26 stsp goto done;
8696 f8a36e22 2021-08-26 stsp
8697 f8a36e22 2021-08-26 stsp if (worktree) {
8698 f8a36e22 2021-08-26 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
8699 f8a36e22 2021-08-26 stsp if (worktree_conf) {
8700 f8a36e22 2021-08-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
8701 f8a36e22 2021-08-26 stsp worktree_conf);
8702 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
8703 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
8704 f8a36e22 2021-08-26 stsp remote = &remotes[i];
8705 f8a36e22 2021-08-26 stsp break;
8706 f8a36e22 2021-08-26 stsp }
8707 f8a36e22 2021-08-26 stsp }
8708 f8a36e22 2021-08-26 stsp }
8709 f8a36e22 2021-08-26 stsp }
8710 f8a36e22 2021-08-26 stsp if (remote == NULL) {
8711 f8a36e22 2021-08-26 stsp repo_conf = got_repo_get_gotconfig(repo);
8712 f8a36e22 2021-08-26 stsp if (repo_conf) {
8713 f8a36e22 2021-08-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
8714 f8a36e22 2021-08-26 stsp repo_conf);
8715 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
8716 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
8717 f8a36e22 2021-08-26 stsp remote = &remotes[i];
8718 f8a36e22 2021-08-26 stsp break;
8719 f8a36e22 2021-08-26 stsp }
8720 f8a36e22 2021-08-26 stsp }
8721 f8a36e22 2021-08-26 stsp }
8722 f8a36e22 2021-08-26 stsp }
8723 f8a36e22 2021-08-26 stsp if (remote == NULL) {
8724 f8a36e22 2021-08-26 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
8725 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
8726 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
8727 f8a36e22 2021-08-26 stsp remote = &remotes[i];
8728 f8a36e22 2021-08-26 stsp break;
8729 f8a36e22 2021-08-26 stsp }
8730 f8a36e22 2021-08-26 stsp }
8731 f8a36e22 2021-08-26 stsp }
8732 f8a36e22 2021-08-26 stsp if (remote == NULL) {
8733 f8a36e22 2021-08-26 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
8734 f8a36e22 2021-08-26 stsp goto done;
8735 f8a36e22 2021-08-26 stsp }
8736 f8a36e22 2021-08-26 stsp
8737 5e5da8c4 2021-09-05 stsp error = got_dial_parse_uri(&proto, &host, &port, &server_path,
8738 6480c871 2021-08-30 stsp &repo_name, remote->send_url);
8739 f8a36e22 2021-08-26 stsp if (error)
8740 f8a36e22 2021-08-26 stsp goto done;
8741 f8a36e22 2021-08-26 stsp
8742 f8a36e22 2021-08-26 stsp if (strcmp(proto, "git") == 0) {
8743 f8a36e22 2021-08-26 stsp #ifndef PROFILE
8744 f8a36e22 2021-08-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8745 f8a36e22 2021-08-26 stsp "sendfd dns inet unveil", NULL) == -1)
8746 f8a36e22 2021-08-26 stsp err(1, "pledge");
8747 f8a36e22 2021-08-26 stsp #endif
8748 f8a36e22 2021-08-26 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
8749 f8a36e22 2021-08-26 stsp strcmp(proto, "ssh") == 0) {
8750 f8a36e22 2021-08-26 stsp #ifndef PROFILE
8751 f8a36e22 2021-08-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8752 f8a36e22 2021-08-26 stsp "sendfd unveil", NULL) == -1)
8753 f8a36e22 2021-08-26 stsp err(1, "pledge");
8754 f8a36e22 2021-08-26 stsp #endif
8755 f8a36e22 2021-08-26 stsp } else if (strcmp(proto, "http") == 0 ||
8756 f8a36e22 2021-08-26 stsp strcmp(proto, "git+http") == 0) {
8757 f8a36e22 2021-08-26 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
8758 f8a36e22 2021-08-26 stsp goto done;
8759 f8a36e22 2021-08-26 stsp } else {
8760 f8a36e22 2021-08-26 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
8761 f8a36e22 2021-08-26 stsp goto done;
8762 f8a36e22 2021-08-26 stsp }
8763 f8a36e22 2021-08-26 stsp
8764 d65a88a2 2021-09-05 stsp error = got_dial_apply_unveil(proto);
8765 d65a88a2 2021-09-05 stsp if (error)
8766 d65a88a2 2021-09-05 stsp goto done;
8767 d65a88a2 2021-09-05 stsp
8768 f8a36e22 2021-08-26 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
8769 f8a36e22 2021-08-26 stsp if (error)
8770 f8a36e22 2021-08-26 stsp goto done;
8771 f8a36e22 2021-08-26 stsp
8772 f8a36e22 2021-08-26 stsp if (send_all_branches) {
8773 f8a36e22 2021-08-26 stsp error = got_ref_list(&all_branches, repo, "refs/heads",
8774 f8a36e22 2021-08-26 stsp got_ref_cmp_by_name, NULL);
8775 f8a36e22 2021-08-26 stsp if (error)
8776 f8a36e22 2021-08-26 stsp goto done;
8777 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &all_branches, entry) {
8778 f8a36e22 2021-08-26 stsp const char *branchname = got_ref_get_name(re->ref);
8779 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches,
8780 f8a36e22 2021-08-26 stsp branchname, NULL);
8781 f8a36e22 2021-08-26 stsp if (error)
8782 f8a36e22 2021-08-26 stsp goto done;
8783 f8a36e22 2021-08-26 stsp nbranches++;
8784 f8a36e22 2021-08-26 stsp }
8785 eac1df47 2021-09-01 stsp } else if (nbranches == 0) {
8786 eac1df47 2021-09-01 stsp for (i = 0; i < remote->nsend_branches; i++) {
8787 eac1df47 2021-09-01 stsp got_pathlist_append(&branches,
8788 eac1df47 2021-09-01 stsp remote->send_branches[i], NULL);
8789 eac1df47 2021-09-01 stsp }
8790 f8a36e22 2021-08-26 stsp }
8791 f8a36e22 2021-08-26 stsp
8792 f8a36e22 2021-08-26 stsp if (send_all_tags) {
8793 f8a36e22 2021-08-26 stsp error = got_ref_list(&all_tags, repo, "refs/tags",
8794 f8a36e22 2021-08-26 stsp got_ref_cmp_by_name, NULL);
8795 f8a36e22 2021-08-26 stsp if (error)
8796 f8a36e22 2021-08-26 stsp goto done;
8797 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &all_tags, entry) {
8798 f8a36e22 2021-08-26 stsp const char *tagname = got_ref_get_name(re->ref);
8799 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&tags,
8800 f8a36e22 2021-08-26 stsp tagname, NULL);
8801 f8a36e22 2021-08-26 stsp if (error)
8802 f8a36e22 2021-08-26 stsp goto done;
8803 f8a36e22 2021-08-26 stsp ntags++;
8804 f8a36e22 2021-08-26 stsp }
8805 f8a36e22 2021-08-26 stsp }
8806 f8a36e22 2021-08-26 stsp
8807 f8a36e22 2021-08-26 stsp /*
8808 f8a36e22 2021-08-26 stsp * To prevent accidents only branches in refs/heads/ can be deleted
8809 f8a36e22 2021-08-26 stsp * with 'got send -d'.
8810 f8a36e22 2021-08-26 stsp * Deleting anything else requires local repository access or Git.
8811 f8a36e22 2021-08-26 stsp */
8812 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_args, entry) {
8813 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
8814 f8a36e22 2021-08-26 stsp char *s;
8815 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *new;
8816 f8a36e22 2021-08-26 stsp if (strncmp(branchname, "refs/heads/", 11) == 0) {
8817 f8a36e22 2021-08-26 stsp s = strdup(branchname);
8818 f8a36e22 2021-08-26 stsp if (s == NULL) {
8819 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
8820 f8a36e22 2021-08-26 stsp goto done;
8821 f8a36e22 2021-08-26 stsp }
8822 f8a36e22 2021-08-26 stsp } else {
8823 f8a36e22 2021-08-26 stsp if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
8824 f8a36e22 2021-08-26 stsp error = got_error_from_errno("asprintf");
8825 f8a36e22 2021-08-26 stsp goto done;
8826 f8a36e22 2021-08-26 stsp }
8827 f8a36e22 2021-08-26 stsp }
8828 f8a36e22 2021-08-26 stsp error = got_pathlist_insert(&new, &delete_branches, s, NULL);
8829 f8a36e22 2021-08-26 stsp if (error || new == NULL /* duplicate */)
8830 f8a36e22 2021-08-26 stsp free(s);
8831 f8a36e22 2021-08-26 stsp if (error)
8832 f8a36e22 2021-08-26 stsp goto done;
8833 f8a36e22 2021-08-26 stsp ndelete_branches++;
8834 f8a36e22 2021-08-26 stsp }
8835 f8a36e22 2021-08-26 stsp
8836 f8a36e22 2021-08-26 stsp if (nbranches == 0 && ndelete_branches == 0) {
8837 f8a36e22 2021-08-26 stsp struct got_reference *head_ref;
8838 f8a36e22 2021-08-26 stsp if (worktree)
8839 f8a36e22 2021-08-26 stsp error = got_ref_open(&head_ref, repo,
8840 f8a36e22 2021-08-26 stsp got_worktree_get_head_ref_name(worktree), 0);
8841 f8a36e22 2021-08-26 stsp else
8842 f8a36e22 2021-08-26 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
8843 f8a36e22 2021-08-26 stsp if (error)
8844 f8a36e22 2021-08-26 stsp goto done;
8845 f8a36e22 2021-08-26 stsp if (got_ref_is_symbolic(head_ref)) {
8846 f8a36e22 2021-08-26 stsp error = got_ref_resolve_symbolic(&ref, repo, head_ref);
8847 f8a36e22 2021-08-26 stsp got_ref_close(head_ref);
8848 f8a36e22 2021-08-26 stsp if (error)
8849 f8a36e22 2021-08-26 stsp goto done;
8850 f8a36e22 2021-08-26 stsp } else
8851 f8a36e22 2021-08-26 stsp ref = head_ref;
8852 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches, got_ref_get_name(ref),
8853 abc59930 2021-09-05 naddy NULL);
8854 f8a36e22 2021-08-26 stsp if (error)
8855 f8a36e22 2021-08-26 stsp goto done;
8856 f8a36e22 2021-08-26 stsp nbranches++;
8857 f8a36e22 2021-08-26 stsp }
8858 f8a36e22 2021-08-26 stsp
8859 f8a36e22 2021-08-26 stsp if (verbosity >= 0)
8860 f8a36e22 2021-08-26 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
8861 f8a36e22 2021-08-26 stsp port ? ":" : "", port ? port : "");
8862 f8a36e22 2021-08-26 stsp
8863 f8a36e22 2021-08-26 stsp error = got_send_connect(&sendpid, &sendfd, proto, host, port,
8864 f8a36e22 2021-08-26 stsp server_path, verbosity);
8865 f8a36e22 2021-08-26 stsp if (error)
8866 f8a36e22 2021-08-26 stsp goto done;
8867 f8a36e22 2021-08-26 stsp
8868 f8a36e22 2021-08-26 stsp memset(&spa, 0, sizeof(spa));
8869 f8a36e22 2021-08-26 stsp spa.last_scaled_packsize[0] = '\0';
8870 f8a36e22 2021-08-26 stsp spa.last_p_deltify = -1;
8871 f8a36e22 2021-08-26 stsp spa.last_p_written = -1;
8872 f8a36e22 2021-08-26 stsp spa.verbosity = verbosity;
8873 f8a36e22 2021-08-26 stsp spa.delete_branches = &delete_branches;
8874 f8a36e22 2021-08-26 stsp error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
8875 f8a36e22 2021-08-26 stsp verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
8876 f8a36e22 2021-08-26 stsp check_cancelled, NULL);
8877 f8a36e22 2021-08-26 stsp if (spa.printed_something)
8878 f8a36e22 2021-08-26 stsp putchar('\n');
8879 f8a36e22 2021-08-26 stsp if (error)
8880 f8a36e22 2021-08-26 stsp goto done;
8881 f8a36e22 2021-08-26 stsp if (!spa.sent_something && verbosity >= 0)
8882 f8a36e22 2021-08-26 stsp printf("Already up-to-date\n");
8883 f8a36e22 2021-08-26 stsp done:
8884 f8a36e22 2021-08-26 stsp if (sendpid > 0) {
8885 f8a36e22 2021-08-26 stsp if (kill(sendpid, SIGTERM) == -1)
8886 f8a36e22 2021-08-26 stsp error = got_error_from_errno("kill");
8887 f8a36e22 2021-08-26 stsp if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
8888 f8a36e22 2021-08-26 stsp error = got_error_from_errno("waitpid");
8889 f8a36e22 2021-08-26 stsp }
8890 f8a36e22 2021-08-26 stsp if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
8891 f8a36e22 2021-08-26 stsp error = got_error_from_errno("close");
8892 f8a36e22 2021-08-26 stsp if (repo) {
8893 f8a36e22 2021-08-26 stsp const struct got_error *close_err = got_repo_close(repo);
8894 f8a36e22 2021-08-26 stsp if (error == NULL)
8895 f8a36e22 2021-08-26 stsp error = close_err;
8896 f8a36e22 2021-08-26 stsp }
8897 f8a36e22 2021-08-26 stsp if (worktree)
8898 f8a36e22 2021-08-26 stsp got_worktree_close(worktree);
8899 0ae84acc 2022-06-15 tracey if (pack_fds) {
8900 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8901 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8902 0ae84acc 2022-06-15 tracey if (error == NULL)
8903 0ae84acc 2022-06-15 tracey error = pack_err;
8904 0ae84acc 2022-06-15 tracey }
8905 f8a36e22 2021-08-26 stsp if (ref)
8906 f8a36e22 2021-08-26 stsp got_ref_close(ref);
8907 f8a36e22 2021-08-26 stsp got_pathlist_free(&branches);
8908 f8a36e22 2021-08-26 stsp got_pathlist_free(&tags);
8909 f8a36e22 2021-08-26 stsp got_ref_list_free(&all_branches);
8910 f8a36e22 2021-08-26 stsp got_ref_list_free(&all_tags);
8911 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_args);
8912 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_branches, entry)
8913 f8a36e22 2021-08-26 stsp free((char *)pe->path);
8914 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_branches);
8915 f8a36e22 2021-08-26 stsp free(cwd);
8916 f8a36e22 2021-08-26 stsp free(repo_path);
8917 f8a36e22 2021-08-26 stsp free(proto);
8918 f8a36e22 2021-08-26 stsp free(host);
8919 f8a36e22 2021-08-26 stsp free(port);
8920 f8a36e22 2021-08-26 stsp free(server_path);
8921 f8a36e22 2021-08-26 stsp free(repo_name);
8922 234035bc 2019-06-01 stsp return error;
8923 234035bc 2019-06-01 stsp }
8924 234035bc 2019-06-01 stsp
8925 234035bc 2019-06-01 stsp __dead static void
8926 234035bc 2019-06-01 stsp usage_cherrypick(void)
8927 234035bc 2019-06-01 stsp {
8928 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
8929 234035bc 2019-06-01 stsp exit(1);
8930 234035bc 2019-06-01 stsp }
8931 234035bc 2019-06-01 stsp
8932 234035bc 2019-06-01 stsp static const struct got_error *
8933 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
8934 234035bc 2019-06-01 stsp {
8935 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
8936 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
8937 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
8938 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
8939 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
8940 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
8941 234035bc 2019-06-01 stsp struct got_object_qid *pid;
8942 9627c110 2020-04-18 stsp int ch;
8943 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8944 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8945 234035bc 2019-06-01 stsp
8946 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8947 234035bc 2019-06-01 stsp switch (ch) {
8948 234035bc 2019-06-01 stsp default:
8949 234035bc 2019-06-01 stsp usage_cherrypick();
8950 234035bc 2019-06-01 stsp /* NOTREACHED */
8951 234035bc 2019-06-01 stsp }
8952 234035bc 2019-06-01 stsp }
8953 234035bc 2019-06-01 stsp
8954 234035bc 2019-06-01 stsp argc -= optind;
8955 234035bc 2019-06-01 stsp argv += optind;
8956 234035bc 2019-06-01 stsp
8957 43012d58 2019-07-14 stsp #ifndef PROFILE
8958 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8959 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8960 43012d58 2019-07-14 stsp err(1, "pledge");
8961 43012d58 2019-07-14 stsp #endif
8962 234035bc 2019-06-01 stsp if (argc != 1)
8963 234035bc 2019-06-01 stsp usage_cherrypick();
8964 234035bc 2019-06-01 stsp
8965 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
8966 234035bc 2019-06-01 stsp if (cwd == NULL) {
8967 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
8968 234035bc 2019-06-01 stsp goto done;
8969 234035bc 2019-06-01 stsp }
8970 0ae84acc 2022-06-15 tracey
8971 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8972 0ae84acc 2022-06-15 tracey if (error != NULL)
8973 0ae84acc 2022-06-15 tracey goto done;
8974 0ae84acc 2022-06-15 tracey
8975 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
8976 fa51e947 2020-03-27 stsp if (error) {
8977 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8978 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
8979 fa51e947 2020-03-27 stsp cwd);
8980 234035bc 2019-06-01 stsp goto done;
8981 fa51e947 2020-03-27 stsp }
8982 234035bc 2019-06-01 stsp
8983 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8984 0ae84acc 2022-06-15 tracey NULL, pack_fds);
8985 234035bc 2019-06-01 stsp if (error != NULL)
8986 234035bc 2019-06-01 stsp goto done;
8987 234035bc 2019-06-01 stsp
8988 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8989 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
8990 234035bc 2019-06-01 stsp if (error)
8991 234035bc 2019-06-01 stsp goto done;
8992 234035bc 2019-06-01 stsp
8993 39479dae 2022-03-10 naddy error = got_repo_match_object_id(&commit_id, NULL, argv[0],
8994 39479dae 2022-03-10 naddy GOT_OBJ_TYPE_COMMIT, NULL, repo);
8995 39479dae 2022-03-10 naddy if (error)
8996 39479dae 2022-03-10 naddy goto done;
8997 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
8998 234035bc 2019-06-01 stsp if (error)
8999 234035bc 2019-06-01 stsp goto done;
9000 234035bc 2019-06-01 stsp
9001 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
9002 234035bc 2019-06-01 stsp if (error)
9003 234035bc 2019-06-01 stsp goto done;
9004 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9005 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9006 d7b5a0e8 2022-04-20 stsp error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
9007 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
9008 03415a1a 2019-06-02 stsp NULL);
9009 234035bc 2019-06-01 stsp if (error != NULL)
9010 234035bc 2019-06-01 stsp goto done;
9011 234035bc 2019-06-01 stsp
9012 9627c110 2020-04-18 stsp if (upa.did_something)
9013 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
9014 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
9015 234035bc 2019-06-01 stsp done:
9016 234035bc 2019-06-01 stsp if (commit)
9017 234035bc 2019-06-01 stsp got_object_commit_close(commit);
9018 234035bc 2019-06-01 stsp free(commit_id_str);
9019 234035bc 2019-06-01 stsp if (worktree)
9020 234035bc 2019-06-01 stsp got_worktree_close(worktree);
9021 1d0f4054 2021-06-17 stsp if (repo) {
9022 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
9023 1d0f4054 2021-06-17 stsp if (error == NULL)
9024 1d0f4054 2021-06-17 stsp error = close_err;
9025 0ae84acc 2022-06-15 tracey }
9026 0ae84acc 2022-06-15 tracey if (pack_fds) {
9027 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
9028 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
9029 0ae84acc 2022-06-15 tracey if (error == NULL)
9030 0ae84acc 2022-06-15 tracey error = pack_err;
9031 1d0f4054 2021-06-17 stsp }
9032 0ae84acc 2022-06-15 tracey
9033 c4296144 2019-05-09 stsp return error;
9034 c4296144 2019-05-09 stsp }
9035 5ef14e63 2019-06-02 stsp
9036 5ef14e63 2019-06-02 stsp __dead static void
9037 5ef14e63 2019-06-02 stsp usage_backout(void)
9038 5ef14e63 2019-06-02 stsp {
9039 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
9040 5ef14e63 2019-06-02 stsp exit(1);
9041 5ef14e63 2019-06-02 stsp }
9042 5ef14e63 2019-06-02 stsp
9043 5ef14e63 2019-06-02 stsp static const struct got_error *
9044 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
9045 5ef14e63 2019-06-02 stsp {
9046 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
9047 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
9048 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
9049 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
9050 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
9051 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
9052 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
9053 9627c110 2020-04-18 stsp int ch;
9054 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9055 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
9056 5ef14e63 2019-06-02 stsp
9057 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9058 5ef14e63 2019-06-02 stsp switch (ch) {
9059 5ef14e63 2019-06-02 stsp default:
9060 5ef14e63 2019-06-02 stsp usage_backout();
9061 5ef14e63 2019-06-02 stsp /* NOTREACHED */
9062 5ef14e63 2019-06-02 stsp }
9063 5ef14e63 2019-06-02 stsp }
9064 5ef14e63 2019-06-02 stsp
9065 5ef14e63 2019-06-02 stsp argc -= optind;
9066 5ef14e63 2019-06-02 stsp argv += optind;
9067 5ef14e63 2019-06-02 stsp
9068 43012d58 2019-07-14 stsp #ifndef PROFILE
9069 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9070 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
9071 43012d58 2019-07-14 stsp err(1, "pledge");
9072 43012d58 2019-07-14 stsp #endif
9073 5ef14e63 2019-06-02 stsp if (argc != 1)
9074 5ef14e63 2019-06-02 stsp usage_backout();
9075 5ef14e63 2019-06-02 stsp
9076 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
9077 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
9078 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
9079 5ef14e63 2019-06-02 stsp goto done;
9080 5ef14e63 2019-06-02 stsp }
9081 0ae84acc 2022-06-15 tracey
9082 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
9083 0ae84acc 2022-06-15 tracey if (error != NULL)
9084 0ae84acc 2022-06-15 tracey goto done;
9085 0ae84acc 2022-06-15 tracey
9086 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
9087 fa51e947 2020-03-27 stsp if (error) {
9088 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9089 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
9090 5ef14e63 2019-06-02 stsp goto done;
9091 fa51e947 2020-03-27 stsp }
9092 5ef14e63 2019-06-02 stsp
9093 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9094 0ae84acc 2022-06-15 tracey NULL, pack_fds);
9095 5ef14e63 2019-06-02 stsp if (error != NULL)
9096 5ef14e63 2019-06-02 stsp goto done;
9097 5ef14e63 2019-06-02 stsp
9098 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9099 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
9100 5ef14e63 2019-06-02 stsp if (error)
9101 5ef14e63 2019-06-02 stsp goto done;
9102 5ef14e63 2019-06-02 stsp
9103 39479dae 2022-03-10 naddy error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9104 39479dae 2022-03-10 naddy GOT_OBJ_TYPE_COMMIT, NULL, repo);
9105 39479dae 2022-03-10 naddy if (error)
9106 39479dae 2022-03-10 naddy goto done;
9107 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
9108 5ef14e63 2019-06-02 stsp if (error)
9109 5ef14e63 2019-06-02 stsp goto done;
9110 5ef14e63 2019-06-02 stsp
9111 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
9112 5ef14e63 2019-06-02 stsp if (error)
9113 5ef14e63 2019-06-02 stsp goto done;
9114 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9115 5ef14e63 2019-06-02 stsp if (pid == NULL) {
9116 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
9117 5ef14e63 2019-06-02 stsp goto done;
9118 5ef14e63 2019-06-02 stsp }
9119 5ef14e63 2019-06-02 stsp
9120 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9121 d7b5a0e8 2022-04-20 stsp error = got_worktree_merge_files(worktree, commit_id, &pid->id,
9122 f259c4c1 2021-09-24 stsp repo, update_progress, &upa, check_cancelled, NULL);
9123 5ef14e63 2019-06-02 stsp if (error != NULL)
9124 5ef14e63 2019-06-02 stsp goto done;
9125 5ef14e63 2019-06-02 stsp
9126 9627c110 2020-04-18 stsp if (upa.did_something)
9127 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
9128 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
9129 5ef14e63 2019-06-02 stsp done:
9130 5ef14e63 2019-06-02 stsp if (commit)
9131 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
9132 5ef14e63 2019-06-02 stsp free(commit_id_str);
9133 818c7501 2019-07-11 stsp if (worktree)
9134 818c7501 2019-07-11 stsp got_worktree_close(worktree);
9135 1d0f4054 2021-06-17 stsp if (repo) {
9136 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
9137 1d0f4054 2021-06-17 stsp if (error == NULL)
9138 1d0f4054 2021-06-17 stsp error = close_err;
9139 0ae84acc 2022-06-15 tracey }
9140 0ae84acc 2022-06-15 tracey if (pack_fds) {
9141 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
9142 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
9143 0ae84acc 2022-06-15 tracey if (error == NULL)
9144 0ae84acc 2022-06-15 tracey error = pack_err;
9145 1d0f4054 2021-06-17 stsp }
9146 818c7501 2019-07-11 stsp return error;
9147 818c7501 2019-07-11 stsp }
9148 818c7501 2019-07-11 stsp
9149 818c7501 2019-07-11 stsp __dead static void
9150 818c7501 2019-07-11 stsp usage_rebase(void)
9151 818c7501 2019-07-11 stsp {
9152 643b85bc 2021-07-16 stsp fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
9153 af54c8f8 2019-07-11 stsp getprogname());
9154 818c7501 2019-07-11 stsp exit(1);
9155 0ebf8283 2019-07-24 stsp }
9156 0ebf8283 2019-07-24 stsp
9157 0ebf8283 2019-07-24 stsp void
9158 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
9159 0ebf8283 2019-07-24 stsp {
9160 0ebf8283 2019-07-24 stsp char *nl;
9161 0ebf8283 2019-07-24 stsp size_t len;
9162 0ebf8283 2019-07-24 stsp
9163 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
9164 0ebf8283 2019-07-24 stsp if (len > limit)
9165 0ebf8283 2019-07-24 stsp len = limit;
9166 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
9167 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
9168 0ebf8283 2019-07-24 stsp if (nl)
9169 0ebf8283 2019-07-24 stsp *nl = '\0';
9170 0ebf8283 2019-07-24 stsp }
9171 0ebf8283 2019-07-24 stsp
9172 0ebf8283 2019-07-24 stsp static const struct got_error *
9173 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
9174 0ebf8283 2019-07-24 stsp {
9175 5943eee2 2019-08-13 stsp const struct got_error *err;
9176 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
9177 5943eee2 2019-08-13 stsp const char *s;
9178 0ebf8283 2019-07-24 stsp
9179 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
9180 5943eee2 2019-08-13 stsp if (err)
9181 5943eee2 2019-08-13 stsp return err;
9182 0ebf8283 2019-07-24 stsp
9183 5943eee2 2019-08-13 stsp s = logmsg0;
9184 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
9185 5943eee2 2019-08-13 stsp s++;
9186 5943eee2 2019-08-13 stsp
9187 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
9188 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
9189 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
9190 5943eee2 2019-08-13 stsp goto done;
9191 5943eee2 2019-08-13 stsp }
9192 0ebf8283 2019-07-24 stsp
9193 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
9194 5943eee2 2019-08-13 stsp done:
9195 5943eee2 2019-08-13 stsp free(logmsg0);
9196 a0ea4fc0 2020-02-28 stsp return err;
9197 a0ea4fc0 2020-02-28 stsp }
9198 a0ea4fc0 2020-02-28 stsp
9199 a0ea4fc0 2020-02-28 stsp static const struct got_error *
9200 c0df5966 2021-12-31 stsp show_rebase_merge_conflict(struct got_object_id *id,
9201 c0df5966 2021-12-31 stsp struct got_repository *repo)
9202 a0ea4fc0 2020-02-28 stsp {
9203 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
9204 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
9205 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
9206 a0ea4fc0 2020-02-28 stsp
9207 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
9208 a0ea4fc0 2020-02-28 stsp if (err)
9209 a0ea4fc0 2020-02-28 stsp return err;
9210 a0ea4fc0 2020-02-28 stsp
9211 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
9212 a0ea4fc0 2020-02-28 stsp if (err)
9213 a0ea4fc0 2020-02-28 stsp goto done;
9214 a0ea4fc0 2020-02-28 stsp
9215 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
9216 a0ea4fc0 2020-02-28 stsp
9217 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
9218 a0ea4fc0 2020-02-28 stsp if (err)
9219 a0ea4fc0 2020-02-28 stsp goto done;
9220 a0ea4fc0 2020-02-28 stsp
9221 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
9222 a0ea4fc0 2020-02-28 stsp done:
9223 a0ea4fc0 2020-02-28 stsp free(id_str);
9224 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
9225 a0ea4fc0 2020-02-28 stsp free(logmsg);
9226 5943eee2 2019-08-13 stsp return err;
9227 818c7501 2019-07-11 stsp }
9228 818c7501 2019-07-11 stsp
9229 818c7501 2019-07-11 stsp static const struct got_error *
9230 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
9231 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
9232 818c7501 2019-07-11 stsp {
9233 818c7501 2019-07-11 stsp const struct got_error *err;
9234 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9235 818c7501 2019-07-11 stsp
9236 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
9237 818c7501 2019-07-11 stsp if (err)
9238 818c7501 2019-07-11 stsp goto done;
9239 818c7501 2019-07-11 stsp
9240 ff0d2220 2019-07-11 stsp if (new_id) {
9241 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
9242 ff0d2220 2019-07-11 stsp if (err)
9243 ff0d2220 2019-07-11 stsp goto done;
9244 ff0d2220 2019-07-11 stsp }
9245 818c7501 2019-07-11 stsp
9246 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
9247 ff0d2220 2019-07-11 stsp if (new_id_str)
9248 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
9249 0ebf8283 2019-07-24 stsp
9250 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
9251 0ebf8283 2019-07-24 stsp if (err)
9252 0ebf8283 2019-07-24 stsp goto done;
9253 0ebf8283 2019-07-24 stsp
9254 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
9255 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
9256 818c7501 2019-07-11 stsp done:
9257 818c7501 2019-07-11 stsp free(old_id_str);
9258 818c7501 2019-07-11 stsp free(new_id_str);
9259 272a1371 2020-02-28 stsp free(logmsg);
9260 818c7501 2019-07-11 stsp return err;
9261 818c7501 2019-07-11 stsp }
9262 818c7501 2019-07-11 stsp
9263 1ee397ad 2019-07-12 stsp static const struct got_error *
9264 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
9265 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
9266 e600f124 2021-03-21 stsp struct got_reference *tmp_branch, struct got_repository *repo,
9267 e600f124 2021-03-21 stsp int create_backup)
9268 818c7501 2019-07-11 stsp {
9269 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
9270 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
9271 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, branch, repo, create_backup);
9272 818c7501 2019-07-11 stsp }
9273 818c7501 2019-07-11 stsp
9274 818c7501 2019-07-11 stsp static const struct got_error *
9275 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
9276 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
9277 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
9278 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
9279 ff0d2220 2019-07-11 stsp {
9280 ff0d2220 2019-07-11 stsp const struct got_error *error;
9281 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
9282 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
9283 ff0d2220 2019-07-11 stsp
9284 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
9285 ff0d2220 2019-07-11 stsp if (error)
9286 ff0d2220 2019-07-11 stsp return error;
9287 ff0d2220 2019-07-11 stsp
9288 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
9289 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
9290 ff0d2220 2019-07-11 stsp if (error) {
9291 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
9292 ff0d2220 2019-07-11 stsp goto done;
9293 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
9294 ff0d2220 2019-07-11 stsp } else {
9295 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
9296 ff0d2220 2019-07-11 stsp free(new_commit_id);
9297 ff0d2220 2019-07-11 stsp }
9298 ff0d2220 2019-07-11 stsp done:
9299 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
9300 ff0d2220 2019-07-11 stsp return error;
9301 64c6d990 2019-07-11 stsp }
9302 64c6d990 2019-07-11 stsp
9303 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
9304 64c6d990 2019-07-11 stsp const char *path_prefix;
9305 64c6d990 2019-07-11 stsp size_t len;
9306 8ca9bd68 2019-07-25 stsp int errcode;
9307 64c6d990 2019-07-11 stsp };
9308 64c6d990 2019-07-11 stsp
9309 64c6d990 2019-07-11 stsp static const struct got_error *
9310 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
9311 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
9312 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
9313 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
9314 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
9315 64c6d990 2019-07-11 stsp {
9316 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
9317 64c6d990 2019-07-11 stsp
9318 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
9319 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
9320 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
9321 64c6d990 2019-07-11 stsp
9322 64c6d990 2019-07-11 stsp return NULL;
9323 64c6d990 2019-07-11 stsp }
9324 64c6d990 2019-07-11 stsp
9325 64c6d990 2019-07-11 stsp static const struct got_error *
9326 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
9327 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
9328 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
9329 64c6d990 2019-07-11 stsp {
9330 64c6d990 2019-07-11 stsp const struct got_error *err;
9331 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
9332 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
9333 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
9334 64c6d990 2019-07-11 stsp
9335 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
9336 64c6d990 2019-07-11 stsp return NULL;
9337 64c6d990 2019-07-11 stsp
9338 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
9339 64c6d990 2019-07-11 stsp if (err)
9340 64c6d990 2019-07-11 stsp goto done;
9341 64c6d990 2019-07-11 stsp
9342 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
9343 64c6d990 2019-07-11 stsp if (err)
9344 64c6d990 2019-07-11 stsp goto done;
9345 64c6d990 2019-07-11 stsp
9346 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
9347 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
9348 64c6d990 2019-07-11 stsp if (err)
9349 64c6d990 2019-07-11 stsp goto done;
9350 64c6d990 2019-07-11 stsp
9351 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
9352 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
9353 64c6d990 2019-07-11 stsp if (err)
9354 64c6d990 2019-07-11 stsp goto done;
9355 64c6d990 2019-07-11 stsp
9356 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
9357 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
9358 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
9359 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
9360 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
9361 b72706c3 2022-06-01 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
9362 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
9363 64c6d990 2019-07-11 stsp done:
9364 64c6d990 2019-07-11 stsp if (tree1)
9365 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
9366 64c6d990 2019-07-11 stsp if (tree2)
9367 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
9368 64c6d990 2019-07-11 stsp if (commit)
9369 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
9370 64c6d990 2019-07-11 stsp if (parent_commit)
9371 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
9372 64c6d990 2019-07-11 stsp return err;
9373 ff0d2220 2019-07-11 stsp }
9374 ff0d2220 2019-07-11 stsp
9375 ff0d2220 2019-07-11 stsp static const struct got_error *
9376 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
9377 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
9378 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
9379 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
9380 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
9381 af61c510 2019-07-19 stsp {
9382 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
9383 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
9384 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
9385 af61c510 2019-07-19 stsp struct got_object_qid *qid;
9386 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
9387 af61c510 2019-07-19 stsp
9388 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
9389 af61c510 2019-07-19 stsp if (err)
9390 af61c510 2019-07-19 stsp return err;
9391 af61c510 2019-07-19 stsp
9392 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
9393 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
9394 af61c510 2019-07-19 stsp if (err)
9395 af61c510 2019-07-19 stsp goto done;
9396 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
9397 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
9398 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
9399 af61c510 2019-07-19 stsp if (err) {
9400 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
9401 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
9402 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
9403 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
9404 af61c510 2019-07-19 stsp "been reached?!?");
9405 ee780d5c 2020-01-04 stsp }
9406 ee780d5c 2020-01-04 stsp goto done;
9407 af61c510 2019-07-19 stsp } else {
9408 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
9409 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
9410 af61c510 2019-07-19 stsp if (err)
9411 af61c510 2019-07-19 stsp goto done;
9412 af61c510 2019-07-19 stsp
9413 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
9414 af61c510 2019-07-19 stsp if (err)
9415 af61c510 2019-07-19 stsp goto done;
9416 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(commits, qid, entry);
9417 af61c510 2019-07-19 stsp commit_id = parent_id;
9418 af61c510 2019-07-19 stsp }
9419 af61c510 2019-07-19 stsp }
9420 af61c510 2019-07-19 stsp done:
9421 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
9422 af61c510 2019-07-19 stsp return err;
9423 e600f124 2021-03-21 stsp }
9424 e600f124 2021-03-21 stsp
9425 e600f124 2021-03-21 stsp static const struct got_error *
9426 e600f124 2021-03-21 stsp get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
9427 e600f124 2021-03-21 stsp {
9428 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
9429 e600f124 2021-03-21 stsp time_t committer_time;
9430 e600f124 2021-03-21 stsp struct tm tm;
9431 e600f124 2021-03-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
9432 e600f124 2021-03-21 stsp char *author0 = NULL, *author, *smallerthan;
9433 e600f124 2021-03-21 stsp char *logmsg0 = NULL, *logmsg, *newline;
9434 e600f124 2021-03-21 stsp
9435 e600f124 2021-03-21 stsp committer_time = got_object_commit_get_committer_time(commit);
9436 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
9437 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
9438 e3199de8 2021-03-21 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
9439 e600f124 2021-03-21 stsp return got_error(GOT_ERR_NO_SPACE);
9440 e600f124 2021-03-21 stsp
9441 e600f124 2021-03-21 stsp author0 = strdup(got_object_commit_get_author(commit));
9442 e600f124 2021-03-21 stsp if (author0 == NULL)
9443 e600f124 2021-03-21 stsp return got_error_from_errno("strdup");
9444 e600f124 2021-03-21 stsp author = author0;
9445 e600f124 2021-03-21 stsp smallerthan = strchr(author, '<');
9446 e600f124 2021-03-21 stsp if (smallerthan && smallerthan[1] != '\0')
9447 e600f124 2021-03-21 stsp author = smallerthan + 1;
9448 e600f124 2021-03-21 stsp author[strcspn(author, "@>")] = '\0';
9449 e600f124 2021-03-21 stsp
9450 e600f124 2021-03-21 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
9451 e600f124 2021-03-21 stsp if (err)
9452 e600f124 2021-03-21 stsp goto done;
9453 e600f124 2021-03-21 stsp logmsg = logmsg0;
9454 e600f124 2021-03-21 stsp while (*logmsg == '\n')
9455 e600f124 2021-03-21 stsp logmsg++;
9456 e600f124 2021-03-21 stsp newline = strchr(logmsg, '\n');
9457 e600f124 2021-03-21 stsp if (newline)
9458 e600f124 2021-03-21 stsp *newline = '\0';
9459 e600f124 2021-03-21 stsp
9460 e600f124 2021-03-21 stsp if (asprintf(brief_str, "%s %s %s",
9461 e600f124 2021-03-21 stsp datebuf, author, logmsg) == -1)
9462 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
9463 e600f124 2021-03-21 stsp done:
9464 e600f124 2021-03-21 stsp free(author0);
9465 e600f124 2021-03-21 stsp free(logmsg0);
9466 643b85bc 2021-07-16 stsp return err;
9467 643b85bc 2021-07-16 stsp }
9468 643b85bc 2021-07-16 stsp
9469 643b85bc 2021-07-16 stsp static const struct got_error *
9470 643b85bc 2021-07-16 stsp delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
9471 643b85bc 2021-07-16 stsp struct got_repository *repo)
9472 643b85bc 2021-07-16 stsp {
9473 643b85bc 2021-07-16 stsp const struct got_error *err;
9474 643b85bc 2021-07-16 stsp char *id_str;
9475 643b85bc 2021-07-16 stsp
9476 643b85bc 2021-07-16 stsp err = got_object_id_str(&id_str, id);
9477 643b85bc 2021-07-16 stsp if (err)
9478 643b85bc 2021-07-16 stsp return err;
9479 643b85bc 2021-07-16 stsp
9480 643b85bc 2021-07-16 stsp err = got_ref_delete(ref, repo);
9481 643b85bc 2021-07-16 stsp if (err)
9482 643b85bc 2021-07-16 stsp goto done;
9483 643b85bc 2021-07-16 stsp
9484 643b85bc 2021-07-16 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
9485 643b85bc 2021-07-16 stsp done:
9486 643b85bc 2021-07-16 stsp free(id_str);
9487 e600f124 2021-03-21 stsp return err;
9488 e600f124 2021-03-21 stsp }
9489 e600f124 2021-03-21 stsp
9490 e600f124 2021-03-21 stsp static const struct got_error *
9491 e600f124 2021-03-21 stsp print_backup_ref(const char *branch_name, const char *new_id_str,
9492 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
9493 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap,
9494 e600f124 2021-03-21 stsp struct got_repository *repo)
9495 e600f124 2021-03-21 stsp {
9496 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
9497 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
9498 e600f124 2021-03-21 stsp char *refs_str = NULL;
9499 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id = NULL;
9500 e600f124 2021-03-21 stsp struct got_commit_object *new_commit = NULL;
9501 e600f124 2021-03-21 stsp char *new_commit_brief_str = NULL;
9502 e600f124 2021-03-21 stsp struct got_object_id *yca_id = NULL;
9503 e600f124 2021-03-21 stsp struct got_commit_object *yca_commit = NULL;
9504 e600f124 2021-03-21 stsp char *yca_id_str = NULL, *yca_brief_str = NULL;
9505 e600f124 2021-03-21 stsp char *custom_refs_str;
9506 e600f124 2021-03-21 stsp
9507 e600f124 2021-03-21 stsp if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
9508 e600f124 2021-03-21 stsp return got_error_from_errno("asprintf");
9509 e600f124 2021-03-21 stsp
9510 e600f124 2021-03-21 stsp err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
9511 e600f124 2021-03-21 stsp 0, 0, refs_idmap, custom_refs_str);
9512 e600f124 2021-03-21 stsp if (err)
9513 e600f124 2021-03-21 stsp goto done;
9514 e600f124 2021-03-21 stsp
9515 e600f124 2021-03-21 stsp err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
9516 e600f124 2021-03-21 stsp if (err)
9517 e600f124 2021-03-21 stsp goto done;
9518 e600f124 2021-03-21 stsp
9519 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
9520 e600f124 2021-03-21 stsp if (refs) {
9521 4cac2b4e 2022-06-13 stsp err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
9522 e600f124 2021-03-21 stsp if (err)
9523 e600f124 2021-03-21 stsp goto done;
9524 e600f124 2021-03-21 stsp }
9525 e600f124 2021-03-21 stsp
9526 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
9527 e600f124 2021-03-21 stsp if (err)
9528 e600f124 2021-03-21 stsp goto done;
9529 e600f124 2021-03-21 stsp
9530 e600f124 2021-03-21 stsp err = get_commit_brief_str(&new_commit_brief_str, new_commit);
9531 e600f124 2021-03-21 stsp if (err)
9532 e600f124 2021-03-21 stsp goto done;
9533 e600f124 2021-03-21 stsp
9534 e600f124 2021-03-21 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9535 4e91ef15 2021-09-26 stsp old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
9536 e600f124 2021-03-21 stsp if (err)
9537 e600f124 2021-03-21 stsp goto done;
9538 e600f124 2021-03-21 stsp
9539 e600f124 2021-03-21 stsp printf("has become commit %s%s%s%s\n %s\n", new_id_str,
9540 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
9541 e600f124 2021-03-21 stsp refs_str ? ")" : "", new_commit_brief_str);
9542 e600f124 2021-03-21 stsp if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
9543 e600f124 2021-03-21 stsp got_object_id_cmp(yca_id, old_commit_id) != 0) {
9544 e600f124 2021-03-21 stsp free(refs_str);
9545 e600f124 2021-03-21 stsp refs_str = NULL;
9546 e600f124 2021-03-21 stsp
9547 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&yca_commit, repo, yca_id);
9548 e600f124 2021-03-21 stsp if (err)
9549 e600f124 2021-03-21 stsp goto done;
9550 e600f124 2021-03-21 stsp
9551 e600f124 2021-03-21 stsp err = get_commit_brief_str(&yca_brief_str, yca_commit);
9552 e600f124 2021-03-21 stsp if (err)
9553 e600f124 2021-03-21 stsp goto done;
9554 e600f124 2021-03-21 stsp
9555 e600f124 2021-03-21 stsp err = got_object_id_str(&yca_id_str, yca_id);
9556 e600f124 2021-03-21 stsp if (err)
9557 e600f124 2021-03-21 stsp goto done;
9558 e600f124 2021-03-21 stsp
9559 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
9560 e600f124 2021-03-21 stsp if (refs) {
9561 4cac2b4e 2022-06-13 stsp err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
9562 e600f124 2021-03-21 stsp if (err)
9563 e600f124 2021-03-21 stsp goto done;
9564 e600f124 2021-03-21 stsp }
9565 e600f124 2021-03-21 stsp printf("history forked at %s%s%s%s\n %s\n",
9566 e600f124 2021-03-21 stsp yca_id_str,
9567 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
9568 e600f124 2021-03-21 stsp refs_str ? ")" : "", yca_brief_str);
9569 e600f124 2021-03-21 stsp }
9570 e600f124 2021-03-21 stsp done:
9571 e600f124 2021-03-21 stsp free(custom_refs_str);
9572 e600f124 2021-03-21 stsp free(new_commit_id);
9573 e600f124 2021-03-21 stsp free(refs_str);
9574 e600f124 2021-03-21 stsp free(yca_id);
9575 e600f124 2021-03-21 stsp free(yca_id_str);
9576 e600f124 2021-03-21 stsp free(yca_brief_str);
9577 e600f124 2021-03-21 stsp if (new_commit)
9578 e600f124 2021-03-21 stsp got_object_commit_close(new_commit);
9579 e600f124 2021-03-21 stsp if (yca_commit)
9580 e600f124 2021-03-21 stsp got_object_commit_close(yca_commit);
9581 e600f124 2021-03-21 stsp
9582 e600f124 2021-03-21 stsp return NULL;
9583 af61c510 2019-07-19 stsp }
9584 af61c510 2019-07-19 stsp
9585 af61c510 2019-07-19 stsp static const struct got_error *
9586 c0df5966 2021-12-31 stsp process_backup_refs(const char *backup_ref_prefix,
9587 c0df5966 2021-12-31 stsp const char *wanted_branch_name,
9588 643b85bc 2021-07-16 stsp int delete, struct got_repository *repo)
9589 e600f124 2021-03-21 stsp {
9590 e600f124 2021-03-21 stsp const struct got_error *err;
9591 e600f124 2021-03-21 stsp struct got_reflist_head refs, backup_refs;
9592 e600f124 2021-03-21 stsp struct got_reflist_entry *re;
9593 e600f124 2021-03-21 stsp const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
9594 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
9595 e600f124 2021-03-21 stsp char *branch_name = NULL;
9596 e600f124 2021-03-21 stsp struct got_commit_object *old_commit = NULL;
9597 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
9598 9e822917 2021-03-23 stsp int wanted_branch_found = 0;
9599 e600f124 2021-03-21 stsp
9600 e600f124 2021-03-21 stsp TAILQ_INIT(&refs);
9601 e600f124 2021-03-21 stsp TAILQ_INIT(&backup_refs);
9602 e600f124 2021-03-21 stsp
9603 e600f124 2021-03-21 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9604 e600f124 2021-03-21 stsp if (err)
9605 e600f124 2021-03-21 stsp return err;
9606 e600f124 2021-03-21 stsp
9607 e600f124 2021-03-21 stsp err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9608 e600f124 2021-03-21 stsp if (err)
9609 e600f124 2021-03-21 stsp goto done;
9610 e600f124 2021-03-21 stsp
9611 e600f124 2021-03-21 stsp if (wanted_branch_name) {
9612 e600f124 2021-03-21 stsp if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
9613 e600f124 2021-03-21 stsp wanted_branch_name += 11;
9614 e600f124 2021-03-21 stsp }
9615 e600f124 2021-03-21 stsp
9616 e600f124 2021-03-21 stsp err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
9617 e600f124 2021-03-21 stsp got_ref_cmp_by_commit_timestamp_descending, repo);
9618 e600f124 2021-03-21 stsp if (err)
9619 e600f124 2021-03-21 stsp goto done;
9620 e600f124 2021-03-21 stsp
9621 e600f124 2021-03-21 stsp TAILQ_FOREACH(re, &backup_refs, entry) {
9622 e600f124 2021-03-21 stsp const char *refname = got_ref_get_name(re->ref);
9623 e600f124 2021-03-21 stsp char *slash;
9624 e600f124 2021-03-21 stsp
9625 643b85bc 2021-07-16 stsp err = check_cancelled(NULL);
9626 643b85bc 2021-07-16 stsp if (err)
9627 643b85bc 2021-07-16 stsp break;
9628 643b85bc 2021-07-16 stsp
9629 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, re->ref);
9630 e600f124 2021-03-21 stsp if (err)
9631 e600f124 2021-03-21 stsp break;
9632 e600f124 2021-03-21 stsp
9633 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&old_commit, repo,
9634 e600f124 2021-03-21 stsp old_commit_id);
9635 e600f124 2021-03-21 stsp if (err)
9636 e600f124 2021-03-21 stsp break;
9637 e600f124 2021-03-21 stsp
9638 e600f124 2021-03-21 stsp if (strncmp(backup_ref_prefix, refname,
9639 e600f124 2021-03-21 stsp backup_ref_prefix_len) == 0)
9640 e600f124 2021-03-21 stsp refname += backup_ref_prefix_len;
9641 e600f124 2021-03-21 stsp
9642 e600f124 2021-03-21 stsp while (refname[0] == '/')
9643 e600f124 2021-03-21 stsp refname++;
9644 e600f124 2021-03-21 stsp
9645 e600f124 2021-03-21 stsp branch_name = strdup(refname);
9646 e600f124 2021-03-21 stsp if (branch_name == NULL) {
9647 e600f124 2021-03-21 stsp err = got_error_from_errno("strdup");
9648 e600f124 2021-03-21 stsp break;
9649 e600f124 2021-03-21 stsp }
9650 e600f124 2021-03-21 stsp slash = strrchr(branch_name, '/');
9651 e600f124 2021-03-21 stsp if (slash) {
9652 e600f124 2021-03-21 stsp *slash = '\0';
9653 e600f124 2021-03-21 stsp refname += strlen(branch_name) + 1;
9654 e600f124 2021-03-21 stsp }
9655 e600f124 2021-03-21 stsp
9656 e600f124 2021-03-21 stsp if (wanted_branch_name == NULL ||
9657 e600f124 2021-03-21 stsp strcmp(wanted_branch_name, branch_name) == 0) {
9658 9e822917 2021-03-23 stsp wanted_branch_found = 1;
9659 643b85bc 2021-07-16 stsp if (delete) {
9660 643b85bc 2021-07-16 stsp err = delete_backup_ref(re->ref,
9661 643b85bc 2021-07-16 stsp old_commit_id, repo);
9662 643b85bc 2021-07-16 stsp } else {
9663 643b85bc 2021-07-16 stsp err = print_backup_ref(branch_name, refname,
9664 abc59930 2021-09-05 naddy old_commit_id, old_commit, refs_idmap,
9665 abc59930 2021-09-05 naddy repo);
9666 643b85bc 2021-07-16 stsp }
9667 e600f124 2021-03-21 stsp if (err)
9668 e600f124 2021-03-21 stsp break;
9669 e600f124 2021-03-21 stsp }
9670 e600f124 2021-03-21 stsp
9671 e600f124 2021-03-21 stsp free(old_commit_id);
9672 e600f124 2021-03-21 stsp old_commit_id = NULL;
9673 e600f124 2021-03-21 stsp free(branch_name);
9674 e600f124 2021-03-21 stsp branch_name = NULL;
9675 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
9676 e600f124 2021-03-21 stsp old_commit = NULL;
9677 e600f124 2021-03-21 stsp }
9678 9e822917 2021-03-23 stsp
9679 9e822917 2021-03-23 stsp if (wanted_branch_name && !wanted_branch_found) {
9680 9e822917 2021-03-23 stsp err = got_error_fmt(GOT_ERR_NOT_REF,
9681 9e822917 2021-03-23 stsp "%s/%s/", backup_ref_prefix, wanted_branch_name);
9682 9e822917 2021-03-23 stsp }
9683 e600f124 2021-03-21 stsp done:
9684 e600f124 2021-03-21 stsp if (refs_idmap)
9685 e600f124 2021-03-21 stsp got_reflist_object_id_map_free(refs_idmap);
9686 e600f124 2021-03-21 stsp got_ref_list_free(&refs);
9687 e600f124 2021-03-21 stsp got_ref_list_free(&backup_refs);
9688 e600f124 2021-03-21 stsp free(old_commit_id);
9689 e600f124 2021-03-21 stsp free(branch_name);
9690 e600f124 2021-03-21 stsp if (old_commit)
9691 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
9692 e600f124 2021-03-21 stsp return err;
9693 e600f124 2021-03-21 stsp }
9694 e600f124 2021-03-21 stsp
9695 e600f124 2021-03-21 stsp static const struct got_error *
9696 41f061b2 2021-10-05 stsp abort_progress(void *arg, unsigned char status, const char *path)
9697 41f061b2 2021-10-05 stsp {
9698 41f061b2 2021-10-05 stsp /*
9699 41f061b2 2021-10-05 stsp * Unversioned files should not clutter progress output when
9700 41f061b2 2021-10-05 stsp * an operation is aborted.
9701 41f061b2 2021-10-05 stsp */
9702 41f061b2 2021-10-05 stsp if (status == GOT_STATUS_UNVERSIONED)
9703 41f061b2 2021-10-05 stsp return NULL;
9704 41f061b2 2021-10-05 stsp
9705 41f061b2 2021-10-05 stsp return update_progress(arg, status, path);
9706 41f061b2 2021-10-05 stsp }
9707 41f061b2 2021-10-05 stsp
9708 41f061b2 2021-10-05 stsp static const struct got_error *
9709 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
9710 818c7501 2019-07-11 stsp {
9711 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
9712 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
9713 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
9714 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
9715 818c7501 2019-07-11 stsp char *cwd = NULL;
9716 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
9717 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
9718 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
9719 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
9720 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
9721 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
9722 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
9723 f259c4c1 2021-09-24 stsp int histedit_in_progress = 0, merge_in_progress = 0;
9724 f259c4c1 2021-09-24 stsp int create_backup = 1, list_backups = 0, delete_backups = 0;
9725 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
9726 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
9727 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
9728 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
9729 1fa49072 2021-09-28 stsp struct got_update_progress_arg upa;
9730 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
9731 818c7501 2019-07-11 stsp
9732 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
9733 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
9734 1fa49072 2021-09-28 stsp memset(&upa, 0, sizeof(upa));
9735 818c7501 2019-07-11 stsp
9736 643b85bc 2021-07-16 stsp while ((ch = getopt(argc, argv, "aclX")) != -1) {
9737 818c7501 2019-07-11 stsp switch (ch) {
9738 818c7501 2019-07-11 stsp case 'a':
9739 818c7501 2019-07-11 stsp abort_rebase = 1;
9740 818c7501 2019-07-11 stsp break;
9741 818c7501 2019-07-11 stsp case 'c':
9742 818c7501 2019-07-11 stsp continue_rebase = 1;
9743 818c7501 2019-07-11 stsp break;
9744 e600f124 2021-03-21 stsp case 'l':
9745 e600f124 2021-03-21 stsp list_backups = 1;
9746 e600f124 2021-03-21 stsp break;
9747 643b85bc 2021-07-16 stsp case 'X':
9748 643b85bc 2021-07-16 stsp delete_backups = 1;
9749 643b85bc 2021-07-16 stsp break;
9750 818c7501 2019-07-11 stsp default:
9751 818c7501 2019-07-11 stsp usage_rebase();
9752 818c7501 2019-07-11 stsp /* NOTREACHED */
9753 818c7501 2019-07-11 stsp }
9754 818c7501 2019-07-11 stsp }
9755 818c7501 2019-07-11 stsp
9756 818c7501 2019-07-11 stsp argc -= optind;
9757 818c7501 2019-07-11 stsp argv += optind;
9758 818c7501 2019-07-11 stsp
9759 43012d58 2019-07-14 stsp #ifndef PROFILE
9760 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9761 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
9762 43012d58 2019-07-14 stsp err(1, "pledge");
9763 43012d58 2019-07-14 stsp #endif
9764 e600f124 2021-03-21 stsp if (list_backups) {
9765 e600f124 2021-03-21 stsp if (abort_rebase)
9766 e600f124 2021-03-21 stsp option_conflict('l', 'a');
9767 e600f124 2021-03-21 stsp if (continue_rebase)
9768 e600f124 2021-03-21 stsp option_conflict('l', 'c');
9769 643b85bc 2021-07-16 stsp if (delete_backups)
9770 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
9771 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
9772 643b85bc 2021-07-16 stsp usage_rebase();
9773 643b85bc 2021-07-16 stsp } else if (delete_backups) {
9774 643b85bc 2021-07-16 stsp if (abort_rebase)
9775 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
9776 643b85bc 2021-07-16 stsp if (continue_rebase)
9777 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
9778 643b85bc 2021-07-16 stsp if (list_backups)
9779 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
9780 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
9781 818c7501 2019-07-11 stsp usage_rebase();
9782 e600f124 2021-03-21 stsp } else {
9783 e600f124 2021-03-21 stsp if (abort_rebase && continue_rebase)
9784 e600f124 2021-03-21 stsp usage_rebase();
9785 e600f124 2021-03-21 stsp else if (abort_rebase || continue_rebase) {
9786 e600f124 2021-03-21 stsp if (argc != 0)
9787 e600f124 2021-03-21 stsp usage_rebase();
9788 e600f124 2021-03-21 stsp } else if (argc != 1)
9789 e600f124 2021-03-21 stsp usage_rebase();
9790 e600f124 2021-03-21 stsp }
9791 818c7501 2019-07-11 stsp
9792 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
9793 818c7501 2019-07-11 stsp if (cwd == NULL) {
9794 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
9795 818c7501 2019-07-11 stsp goto done;
9796 818c7501 2019-07-11 stsp }
9797 0ae84acc 2022-06-15 tracey
9798 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
9799 0ae84acc 2022-06-15 tracey if (error != NULL)
9800 0ae84acc 2022-06-15 tracey goto done;
9801 0ae84acc 2022-06-15 tracey
9802 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
9803 fa51e947 2020-03-27 stsp if (error) {
9804 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
9805 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
9806 e600f124 2021-03-21 stsp goto done;
9807 e600f124 2021-03-21 stsp } else {
9808 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9809 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
9810 e600f124 2021-03-21 stsp "rebase", cwd);
9811 e600f124 2021-03-21 stsp goto done;
9812 e600f124 2021-03-21 stsp }
9813 fa51e947 2020-03-27 stsp }
9814 818c7501 2019-07-11 stsp
9815 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
9816 0ae84acc 2022-06-15 tracey worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
9817 0ae84acc 2022-06-15 tracey pack_fds);
9818 818c7501 2019-07-11 stsp if (error != NULL)
9819 818c7501 2019-07-11 stsp goto done;
9820 818c7501 2019-07-11 stsp
9821 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9822 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
9823 7ef62c4e 2020-02-24 stsp if (error)
9824 7ef62c4e 2020-02-24 stsp goto done;
9825 7ef62c4e 2020-02-24 stsp
9826 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
9827 643b85bc 2021-07-16 stsp error = process_backup_refs(
9828 643b85bc 2021-07-16 stsp GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
9829 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
9830 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
9831 e600f124 2021-03-21 stsp }
9832 e600f124 2021-03-21 stsp
9833 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
9834 7ef62c4e 2020-02-24 stsp worktree);
9835 818c7501 2019-07-11 stsp if (error)
9836 7ef62c4e 2020-02-24 stsp goto done;
9837 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
9838 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
9839 f259c4c1 2021-09-24 stsp goto done;
9840 f259c4c1 2021-09-24 stsp }
9841 f259c4c1 2021-09-24 stsp
9842 f259c4c1 2021-09-24 stsp error = got_worktree_merge_in_progress(&merge_in_progress,
9843 f259c4c1 2021-09-24 stsp worktree, repo);
9844 f259c4c1 2021-09-24 stsp if (error)
9845 f259c4c1 2021-09-24 stsp goto done;
9846 f259c4c1 2021-09-24 stsp if (merge_in_progress) {
9847 f259c4c1 2021-09-24 stsp error = got_error(GOT_ERR_MERGE_BUSY);
9848 818c7501 2019-07-11 stsp goto done;
9849 7ef62c4e 2020-02-24 stsp }
9850 818c7501 2019-07-11 stsp
9851 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9852 818c7501 2019-07-11 stsp if (error)
9853 818c7501 2019-07-11 stsp goto done;
9854 818c7501 2019-07-11 stsp
9855 f6794adc 2019-07-23 stsp if (abort_rebase) {
9856 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
9857 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
9858 f6794adc 2019-07-23 stsp goto done;
9859 f6794adc 2019-07-23 stsp }
9860 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
9861 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
9862 3e3a69f1 2019-07-25 stsp worktree, repo);
9863 818c7501 2019-07-11 stsp if (error)
9864 818c7501 2019-07-11 stsp goto done;
9865 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
9866 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
9867 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
9868 41f061b2 2021-10-05 stsp new_base_branch, abort_progress, &upa);
9869 818c7501 2019-07-11 stsp if (error)
9870 818c7501 2019-07-11 stsp goto done;
9871 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
9872 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
9873 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
9874 818c7501 2019-07-11 stsp }
9875 818c7501 2019-07-11 stsp
9876 818c7501 2019-07-11 stsp if (continue_rebase) {
9877 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
9878 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
9879 f6794adc 2019-07-23 stsp goto done;
9880 f6794adc 2019-07-23 stsp }
9881 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
9882 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
9883 3e3a69f1 2019-07-25 stsp worktree, repo);
9884 818c7501 2019-07-11 stsp if (error)
9885 818c7501 2019-07-11 stsp goto done;
9886 818c7501 2019-07-11 stsp
9887 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
9888 01757395 2019-07-12 stsp resume_commit_id, repo);
9889 818c7501 2019-07-11 stsp if (error)
9890 818c7501 2019-07-11 stsp goto done;
9891 818c7501 2019-07-11 stsp
9892 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
9893 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
9894 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
9895 818c7501 2019-07-11 stsp goto done;
9896 818c7501 2019-07-11 stsp }
9897 818c7501 2019-07-11 stsp } else {
9898 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
9899 818c7501 2019-07-11 stsp if (error != NULL)
9900 818c7501 2019-07-11 stsp goto done;
9901 ff0d2220 2019-07-11 stsp }
9902 818c7501 2019-07-11 stsp
9903 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
9904 ff0d2220 2019-07-11 stsp if (error)
9905 ff0d2220 2019-07-11 stsp goto done;
9906 ff0d2220 2019-07-11 stsp
9907 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
9908 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
9909 a51a74b3 2019-07-27 stsp
9910 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
9911 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9912 4e91ef15 2021-09-26 stsp base_commit_id, branch_head_commit_id, 1, repo,
9913 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
9914 818c7501 2019-07-11 stsp if (error)
9915 818c7501 2019-07-11 stsp goto done;
9916 818c7501 2019-07-11 stsp if (yca_id == NULL) {
9917 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
9918 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
9919 818c7501 2019-07-11 stsp "with work tree's branch");
9920 818c7501 2019-07-11 stsp goto done;
9921 818c7501 2019-07-11 stsp }
9922 818c7501 2019-07-11 stsp
9923 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
9924 a51a74b3 2019-07-27 stsp if (error) {
9925 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
9926 a51a74b3 2019-07-27 stsp goto done;
9927 a51a74b3 2019-07-27 stsp error = NULL;
9928 a51a74b3 2019-07-27 stsp } else {
9929 3d42b266 2021-11-03 jrick struct got_pathlist_head paths;
9930 3d42b266 2021-11-03 jrick printf("%s is already based on %s\n",
9931 df3ed485 2021-01-31 stsp got_ref_get_name(branch),
9932 df3ed485 2021-01-31 stsp got_worktree_get_head_ref_name(worktree));
9933 3d42b266 2021-11-03 jrick error = switch_head_ref(branch, branch_head_commit_id,
9934 3d42b266 2021-11-03 jrick worktree, repo);
9935 3d42b266 2021-11-03 jrick if (error)
9936 3d42b266 2021-11-03 jrick goto done;
9937 3d42b266 2021-11-03 jrick error = got_worktree_set_base_commit_id(worktree, repo,
9938 3d42b266 2021-11-03 jrick branch_head_commit_id);
9939 3d42b266 2021-11-03 jrick if (error)
9940 3d42b266 2021-11-03 jrick goto done;
9941 3d42b266 2021-11-03 jrick TAILQ_INIT(&paths);
9942 3d42b266 2021-11-03 jrick error = got_pathlist_append(&paths, "", NULL);
9943 3d42b266 2021-11-03 jrick if (error)
9944 3d42b266 2021-11-03 jrick goto done;
9945 3d42b266 2021-11-03 jrick error = got_worktree_checkout_files(worktree,
9946 3d42b266 2021-11-03 jrick &paths, repo, update_progress, &upa,
9947 3d42b266 2021-11-03 jrick check_cancelled, NULL);
9948 3d42b266 2021-11-03 jrick got_pathlist_free(&paths);
9949 3d42b266 2021-11-03 jrick if (error)
9950 3d42b266 2021-11-03 jrick goto done;
9951 3d42b266 2021-11-03 jrick if (upa.did_something) {
9952 3d42b266 2021-11-03 jrick char *id_str;
9953 3d42b266 2021-11-03 jrick error = got_object_id_str(&id_str,
9954 3d42b266 2021-11-03 jrick branch_head_commit_id);
9955 3d42b266 2021-11-03 jrick if (error)
9956 3d42b266 2021-11-03 jrick goto done;
9957 3d42b266 2021-11-03 jrick printf("Updated to %s: %s\n",
9958 3d42b266 2021-11-03 jrick got_worktree_get_head_ref_name(worktree),
9959 3d42b266 2021-11-03 jrick id_str);
9960 3d42b266 2021-11-03 jrick free(id_str);
9961 3d42b266 2021-11-03 jrick } else
9962 3d42b266 2021-11-03 jrick printf("Already up-to-date\n");
9963 3d42b266 2021-11-03 jrick print_update_progress_stats(&upa);
9964 a51a74b3 2019-07-27 stsp goto done;
9965 a51a74b3 2019-07-27 stsp }
9966 818c7501 2019-07-11 stsp }
9967 818c7501 2019-07-11 stsp
9968 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
9969 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
9970 818c7501 2019-07-11 stsp if (error)
9971 818c7501 2019-07-11 stsp goto done;
9972 818c7501 2019-07-11 stsp
9973 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9974 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
9975 fc66b545 2019-08-12 stsp if (pid == NULL) {
9976 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
9977 fc66b545 2019-08-12 stsp goto done;
9978 fc66b545 2019-08-12 stsp }
9979 d7b5a0e8 2022-04-20 stsp error = collect_commits(&commits, commit_id, &pid->id,
9980 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
9981 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
9982 818c7501 2019-07-11 stsp got_object_commit_close(commit);
9983 818c7501 2019-07-11 stsp commit = NULL;
9984 818c7501 2019-07-11 stsp if (error)
9985 818c7501 2019-07-11 stsp goto done;
9986 67ba6161 2022-04-08 stsp
9987 67ba6161 2022-04-08 stsp if (!continue_rebase) {
9988 67ba6161 2022-04-08 stsp error = got_worktree_rebase_prepare(&new_base_branch,
9989 67ba6161 2022-04-08 stsp &tmp_branch, &fileindex, worktree, branch, repo);
9990 67ba6161 2022-04-08 stsp if (error)
9991 67ba6161 2022-04-08 stsp goto done;
9992 67ba6161 2022-04-08 stsp }
9993 64c6d990 2019-07-11 stsp
9994 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
9995 38b0338b 2019-11-29 stsp if (continue_rebase) {
9996 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
9997 e600f124 2021-03-21 stsp branch, new_base_branch, tmp_branch, repo,
9998 e600f124 2021-03-21 stsp create_backup);
9999 38b0338b 2019-11-29 stsp goto done;
10000 38b0338b 2019-11-29 stsp } else {
10001 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
10002 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
10003 38b0338b 2019-11-29 stsp char *id_str;
10004 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
10005 38b0338b 2019-11-29 stsp new_base_branch);
10006 38b0338b 2019-11-29 stsp if (error)
10007 38b0338b 2019-11-29 stsp goto done;
10008 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
10009 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
10010 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
10011 38b0338b 2019-11-29 stsp free(id_str);
10012 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
10013 38b0338b 2019-11-29 stsp new_head_commit_id);
10014 38b0338b 2019-11-29 stsp if (error)
10015 38b0338b 2019-11-29 stsp goto done;
10016 e600f124 2021-03-21 stsp /* No backup needed since objects did not change. */
10017 e600f124 2021-03-21 stsp create_backup = 0;
10018 38b0338b 2019-11-29 stsp }
10019 818c7501 2019-07-11 stsp }
10020 818c7501 2019-07-11 stsp
10021 818c7501 2019-07-11 stsp pid = NULL;
10022 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &commits, entry) {
10023 9627c110 2020-04-18 stsp
10024 d7b5a0e8 2022-04-20 stsp commit_id = &qid->id;
10025 d7b5a0e8 2022-04-20 stsp parent_id = pid ? &pid->id : yca_id;
10026 818c7501 2019-07-11 stsp pid = qid;
10027 818c7501 2019-07-11 stsp
10028 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10029 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
10030 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
10031 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
10032 818c7501 2019-07-11 stsp if (error)
10033 818c7501 2019-07-11 stsp goto done;
10034 9627c110 2020-04-18 stsp
10035 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
10036 1fa49072 2021-09-28 stsp if (upa.conflicts > 0 || upa.missing > 0 ||
10037 1fa49072 2021-09-28 stsp upa.not_deleted > 0 || upa.unversioned > 0) {
10038 1fa49072 2021-09-28 stsp if (upa.conflicts > 0) {
10039 d7b5a0e8 2022-04-20 stsp error = show_rebase_merge_conflict(&qid->id,
10040 1fa49072 2021-09-28 stsp repo);
10041 1fa49072 2021-09-28 stsp if (error)
10042 1fa49072 2021-09-28 stsp goto done;
10043 1fa49072 2021-09-28 stsp }
10044 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10045 818c7501 2019-07-11 stsp break;
10046 01757395 2019-07-12 stsp }
10047 818c7501 2019-07-11 stsp
10048 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
10049 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
10050 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10051 818c7501 2019-07-11 stsp if (error)
10052 818c7501 2019-07-11 stsp goto done;
10053 818c7501 2019-07-11 stsp }
10054 818c7501 2019-07-11 stsp
10055 1fa49072 2021-09-28 stsp if (upa.conflicts > 0 || upa.missing > 0 ||
10056 1fa49072 2021-09-28 stsp upa.not_deleted > 0 || upa.unversioned > 0) {
10057 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
10058 818c7501 2019-07-11 stsp if (error)
10059 818c7501 2019-07-11 stsp goto done;
10060 1fa49072 2021-09-28 stsp if (upa.conflicts > 0 && upa.missing == 0 &&
10061 1fa49072 2021-09-28 stsp upa.not_deleted == 0 && upa.unversioned == 0) {
10062 1fa49072 2021-09-28 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
10063 1fa49072 2021-09-28 stsp "conflicts must be resolved before rebasing "
10064 1fa49072 2021-09-28 stsp "can continue");
10065 1fa49072 2021-09-28 stsp } else if (upa.conflicts > 0) {
10066 1fa49072 2021-09-28 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
10067 1fa49072 2021-09-28 stsp "conflicts must be resolved before rebasing "
10068 1fa49072 2021-09-28 stsp "can continue; changes destined for some "
10069 1fa49072 2021-09-28 stsp "files were not yet merged and should be "
10070 1fa49072 2021-09-28 stsp "merged manually if required before the "
10071 1fa49072 2021-09-28 stsp "rebase operation is continued");
10072 1fa49072 2021-09-28 stsp } else {
10073 1fa49072 2021-09-28 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
10074 1fa49072 2021-09-28 stsp "changes destined for some files were not "
10075 1fa49072 2021-09-28 stsp "yet merged and should be merged manually "
10076 1fa49072 2021-09-28 stsp "if required before the rebase operation "
10077 1fa49072 2021-09-28 stsp "is continued");
10078 1fa49072 2021-09-28 stsp }
10079 818c7501 2019-07-11 stsp } else
10080 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
10081 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, repo, create_backup);
10082 818c7501 2019-07-11 stsp done:
10083 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
10084 818c7501 2019-07-11 stsp free(branch_head_commit_id);
10085 818c7501 2019-07-11 stsp free(resume_commit_id);
10086 818c7501 2019-07-11 stsp free(yca_id);
10087 818c7501 2019-07-11 stsp if (commit)
10088 818c7501 2019-07-11 stsp got_object_commit_close(commit);
10089 818c7501 2019-07-11 stsp if (branch)
10090 818c7501 2019-07-11 stsp got_ref_close(branch);
10091 818c7501 2019-07-11 stsp if (new_base_branch)
10092 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
10093 818c7501 2019-07-11 stsp if (tmp_branch)
10094 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
10095 5ef14e63 2019-06-02 stsp if (worktree)
10096 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
10097 1d0f4054 2021-06-17 stsp if (repo) {
10098 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10099 1d0f4054 2021-06-17 stsp if (error == NULL)
10100 1d0f4054 2021-06-17 stsp error = close_err;
10101 1d0f4054 2021-06-17 stsp }
10102 0ae84acc 2022-06-15 tracey if (pack_fds) {
10103 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
10104 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
10105 0ae84acc 2022-06-15 tracey if (error == NULL)
10106 0ae84acc 2022-06-15 tracey error = pack_err;
10107 0ae84acc 2022-06-15 tracey }
10108 5ef14e63 2019-06-02 stsp return error;
10109 0ebf8283 2019-07-24 stsp }
10110 0ebf8283 2019-07-24 stsp
10111 0ebf8283 2019-07-24 stsp __dead static void
10112 0ebf8283 2019-07-24 stsp usage_histedit(void)
10113 0ebf8283 2019-07-24 stsp {
10114 b93c7142 2021-10-01 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-e] [-f] "
10115 643b85bc 2021-07-16 stsp "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
10116 643b85bc 2021-07-16 stsp getprogname());
10117 0ebf8283 2019-07-24 stsp exit(1);
10118 0ebf8283 2019-07-24 stsp }
10119 0ebf8283 2019-07-24 stsp
10120 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
10121 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
10122 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
10123 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
10124 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
10125 0ebf8283 2019-07-24 stsp
10126 3e166534 2022-02-16 naddy static const struct got_histedit_cmd {
10127 0ebf8283 2019-07-24 stsp unsigned char code;
10128 0ebf8283 2019-07-24 stsp const char *name;
10129 0ebf8283 2019-07-24 stsp const char *desc;
10130 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
10131 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
10132 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
10133 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
10134 82997472 2020-01-29 stsp "be used" },
10135 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
10136 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
10137 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
10138 0ebf8283 2019-07-24 stsp };
10139 0ebf8283 2019-07-24 stsp
10140 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
10141 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
10142 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
10143 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
10144 0ebf8283 2019-07-24 stsp char *logmsg;
10145 0ebf8283 2019-07-24 stsp };
10146 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
10147 0ebf8283 2019-07-24 stsp
10148 0ebf8283 2019-07-24 stsp static const struct got_error *
10149 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
10150 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
10151 0ebf8283 2019-07-24 stsp {
10152 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
10153 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
10154 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
10155 8138f3e1 2019-08-11 stsp int n;
10156 0ebf8283 2019-07-24 stsp
10157 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
10158 0ebf8283 2019-07-24 stsp if (err)
10159 0ebf8283 2019-07-24 stsp goto done;
10160 0ebf8283 2019-07-24 stsp
10161 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
10162 0ebf8283 2019-07-24 stsp if (err)
10163 0ebf8283 2019-07-24 stsp goto done;
10164 0ebf8283 2019-07-24 stsp
10165 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
10166 0ebf8283 2019-07-24 stsp if (err)
10167 0ebf8283 2019-07-24 stsp goto done;
10168 0ebf8283 2019-07-24 stsp
10169 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
10170 0ebf8283 2019-07-24 stsp if (n < 0)
10171 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
10172 0ebf8283 2019-07-24 stsp done:
10173 0ebf8283 2019-07-24 stsp if (commit)
10174 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10175 0ebf8283 2019-07-24 stsp free(id_str);
10176 0ebf8283 2019-07-24 stsp free(logmsg);
10177 0ebf8283 2019-07-24 stsp return err;
10178 0ebf8283 2019-07-24 stsp }
10179 0ebf8283 2019-07-24 stsp
10180 0ebf8283 2019-07-24 stsp static const struct got_error *
10181 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
10182 b93c7142 2021-10-01 stsp FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
10183 b93c7142 2021-10-01 stsp struct got_repository *repo)
10184 0ebf8283 2019-07-24 stsp {
10185 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
10186 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
10187 466785b9 2020-12-10 jrick const char *histedit_cmd = NULL;
10188 0ebf8283 2019-07-24 stsp
10189 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
10190 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
10191 0ebf8283 2019-07-24 stsp
10192 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
10193 466785b9 2020-12-10 jrick histedit_cmd = got_histedit_cmds[0].name;
10194 b93c7142 2021-10-01 stsp if (edit_only)
10195 b93c7142 2021-10-01 stsp histedit_cmd = "edit";
10196 b93c7142 2021-10-01 stsp else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
10197 466785b9 2020-12-10 jrick histedit_cmd = "fold";
10198 d7b5a0e8 2022-04-20 stsp err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
10199 0ebf8283 2019-07-24 stsp if (err)
10200 0ebf8283 2019-07-24 stsp break;
10201 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
10202 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
10203 083957f4 2020-02-24 stsp if (n < 0) {
10204 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
10205 083957f4 2020-02-24 stsp break;
10206 083957f4 2020-02-24 stsp }
10207 083957f4 2020-02-24 stsp }
10208 0ebf8283 2019-07-24 stsp }
10209 0ebf8283 2019-07-24 stsp
10210 0ebf8283 2019-07-24 stsp return err;
10211 0ebf8283 2019-07-24 stsp }
10212 0ebf8283 2019-07-24 stsp
10213 0ebf8283 2019-07-24 stsp static const struct got_error *
10214 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
10215 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
10216 0ebf8283 2019-07-24 stsp {
10217 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
10218 6059809a 2020-12-17 stsp size_t i;
10219 6059809a 2020-12-17 stsp int n;
10220 514f2ffe 2020-01-29 stsp char *id_str;
10221 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
10222 514f2ffe 2020-01-29 stsp
10223 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(commits);
10224 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
10225 514f2ffe 2020-01-29 stsp if (err)
10226 514f2ffe 2020-01-29 stsp return err;
10227 514f2ffe 2020-01-29 stsp
10228 514f2ffe 2020-01-29 stsp n = fprintf(f,
10229 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
10230 514f2ffe 2020-01-29 stsp "# commit %s\n"
10231 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
10232 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
10233 514f2ffe 2020-01-29 stsp if (n < 0) {
10234 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
10235 514f2ffe 2020-01-29 stsp goto done;
10236 514f2ffe 2020-01-29 stsp }
10237 0ebf8283 2019-07-24 stsp
10238 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
10239 514f2ffe 2020-01-29 stsp if (n < 0) {
10240 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
10241 514f2ffe 2020-01-29 stsp goto done;
10242 514f2ffe 2020-01-29 stsp }
10243 0ebf8283 2019-07-24 stsp
10244 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
10245 3e166534 2022-02-16 naddy const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
10246 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
10247 0ebf8283 2019-07-24 stsp cmd->desc);
10248 0ebf8283 2019-07-24 stsp if (n < 0) {
10249 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
10250 0ebf8283 2019-07-24 stsp break;
10251 0ebf8283 2019-07-24 stsp }
10252 0ebf8283 2019-07-24 stsp }
10253 514f2ffe 2020-01-29 stsp done:
10254 514f2ffe 2020-01-29 stsp free(id_str);
10255 0ebf8283 2019-07-24 stsp return err;
10256 0ebf8283 2019-07-24 stsp }
10257 0ebf8283 2019-07-24 stsp
10258 0ebf8283 2019-07-24 stsp static const struct got_error *
10259 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
10260 0ebf8283 2019-07-24 stsp {
10261 0ebf8283 2019-07-24 stsp static char msg[42];
10262 0ebf8283 2019-07-24 stsp int ret;
10263 0ebf8283 2019-07-24 stsp
10264 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
10265 0ebf8283 2019-07-24 stsp lineno);
10266 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
10267 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
10268 0ebf8283 2019-07-24 stsp
10269 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
10270 0ebf8283 2019-07-24 stsp }
10271 0ebf8283 2019-07-24 stsp
10272 0ebf8283 2019-07-24 stsp static const struct got_error *
10273 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
10274 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
10275 0ebf8283 2019-07-24 stsp {
10276 0ebf8283 2019-07-24 stsp const struct got_error *err;
10277 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
10278 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
10279 0ebf8283 2019-07-24 stsp
10280 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
10281 0ebf8283 2019-07-24 stsp if (err)
10282 0ebf8283 2019-07-24 stsp return err;
10283 0ebf8283 2019-07-24 stsp
10284 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
10285 0ebf8283 2019-07-24 stsp if (err)
10286 0ebf8283 2019-07-24 stsp goto done;
10287 0ebf8283 2019-07-24 stsp
10288 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
10289 5943eee2 2019-08-13 stsp if (err)
10290 5943eee2 2019-08-13 stsp goto done;
10291 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
10292 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
10293 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
10294 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
10295 0ebf8283 2019-07-24 stsp }
10296 0ebf8283 2019-07-24 stsp done:
10297 0ebf8283 2019-07-24 stsp if (folded_commit)
10298 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
10299 0ebf8283 2019-07-24 stsp free(id_str);
10300 5943eee2 2019-08-13 stsp free(folded_logmsg);
10301 0ebf8283 2019-07-24 stsp return err;
10302 0ebf8283 2019-07-24 stsp }
10303 0ebf8283 2019-07-24 stsp
10304 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
10305 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
10306 0ebf8283 2019-07-24 stsp {
10307 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
10308 0ebf8283 2019-07-24 stsp
10309 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
10310 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
10311 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
10312 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
10313 3f9de99f 2019-07-24 stsp folded = prev;
10314 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
10315 0ebf8283 2019-07-24 stsp }
10316 0ebf8283 2019-07-24 stsp
10317 0ebf8283 2019-07-24 stsp return folded;
10318 0ebf8283 2019-07-24 stsp }
10319 0ebf8283 2019-07-24 stsp
10320 0ebf8283 2019-07-24 stsp static const struct got_error *
10321 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
10322 0ebf8283 2019-07-24 stsp struct got_repository *repo)
10323 0ebf8283 2019-07-24 stsp {
10324 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
10325 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
10326 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
10327 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
10328 1601cb9f 2020-09-11 naddy int logmsg_len;
10329 0ebf8283 2019-07-24 stsp int fd;
10330 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
10331 0ebf8283 2019-07-24 stsp
10332 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10333 0ebf8283 2019-07-24 stsp if (err)
10334 0ebf8283 2019-07-24 stsp return err;
10335 0ebf8283 2019-07-24 stsp
10336 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
10337 0ebf8283 2019-07-24 stsp if (folded) {
10338 0ebf8283 2019-07-24 stsp while (folded != hle) {
10339 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
10340 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
10341 3f9de99f 2019-07-24 stsp continue;
10342 3f9de99f 2019-07-24 stsp }
10343 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
10344 0ebf8283 2019-07-24 stsp logmsg, repo);
10345 0ebf8283 2019-07-24 stsp if (err)
10346 0ebf8283 2019-07-24 stsp goto done;
10347 0ebf8283 2019-07-24 stsp free(logmsg);
10348 0ebf8283 2019-07-24 stsp logmsg = new_msg;
10349 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
10350 0ebf8283 2019-07-24 stsp }
10351 0ebf8283 2019-07-24 stsp }
10352 0ebf8283 2019-07-24 stsp
10353 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
10354 0ebf8283 2019-07-24 stsp if (err)
10355 0ebf8283 2019-07-24 stsp goto done;
10356 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
10357 5943eee2 2019-08-13 stsp if (err)
10358 5943eee2 2019-08-13 stsp goto done;
10359 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
10360 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
10361 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
10362 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
10363 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
10364 0ebf8283 2019-07-24 stsp goto done;
10365 0ebf8283 2019-07-24 stsp }
10366 0ebf8283 2019-07-24 stsp free(logmsg);
10367 0ebf8283 2019-07-24 stsp logmsg = new_msg;
10368 0ebf8283 2019-07-24 stsp
10369 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
10370 0ebf8283 2019-07-24 stsp if (err)
10371 0ebf8283 2019-07-24 stsp goto done;
10372 0ebf8283 2019-07-24 stsp
10373 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
10374 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
10375 0ebf8283 2019-07-24 stsp if (err)
10376 0ebf8283 2019-07-24 stsp goto done;
10377 0ebf8283 2019-07-24 stsp
10378 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
10379 0ebf8283 2019-07-24 stsp close(fd);
10380 0ebf8283 2019-07-24 stsp
10381 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
10382 0ebf8283 2019-07-24 stsp if (err)
10383 0ebf8283 2019-07-24 stsp goto done;
10384 0ebf8283 2019-07-24 stsp
10385 bfa12d5e 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
10386 0d5bb276 2020-12-15 stsp logmsg_len, 0);
10387 0ebf8283 2019-07-24 stsp if (err) {
10388 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
10389 0ebf8283 2019-07-24 stsp goto done;
10390 71392a05 2020-12-13 stsp err = NULL;
10391 71392a05 2020-12-13 stsp hle->logmsg = strdup(new_msg);
10392 71392a05 2020-12-13 stsp if (hle->logmsg == NULL)
10393 71392a05 2020-12-13 stsp err = got_error_from_errno("strdup");
10394 0ebf8283 2019-07-24 stsp }
10395 0ebf8283 2019-07-24 stsp done:
10396 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
10397 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
10398 0ebf8283 2019-07-24 stsp free(logmsg_path);
10399 0ebf8283 2019-07-24 stsp free(logmsg);
10400 5943eee2 2019-08-13 stsp free(orig_logmsg);
10401 0ebf8283 2019-07-24 stsp free(editor);
10402 0ebf8283 2019-07-24 stsp if (commit)
10403 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10404 0ebf8283 2019-07-24 stsp return err;
10405 5ef14e63 2019-06-02 stsp }
10406 0ebf8283 2019-07-24 stsp
10407 0ebf8283 2019-07-24 stsp static const struct got_error *
10408 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
10409 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
10410 0ebf8283 2019-07-24 stsp {
10411 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
10412 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
10413 6059809a 2020-12-17 stsp size_t i, size;
10414 0ebf8283 2019-07-24 stsp ssize_t len;
10415 6059809a 2020-12-17 stsp int lineno = 0;
10416 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
10417 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
10418 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
10419 0ebf8283 2019-07-24 stsp
10420 0ebf8283 2019-07-24 stsp for (;;) {
10421 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
10422 0ebf8283 2019-07-24 stsp if (len == -1) {
10423 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
10424 0ebf8283 2019-07-24 stsp if (feof(f))
10425 0ebf8283 2019-07-24 stsp break;
10426 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
10427 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
10428 0ebf8283 2019-07-24 stsp break;
10429 0ebf8283 2019-07-24 stsp }
10430 0ebf8283 2019-07-24 stsp lineno++;
10431 0ebf8283 2019-07-24 stsp p = line;
10432 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
10433 0ebf8283 2019-07-24 stsp p++;
10434 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
10435 0ebf8283 2019-07-24 stsp free(line);
10436 0ebf8283 2019-07-24 stsp line = NULL;
10437 0ebf8283 2019-07-24 stsp continue;
10438 0ebf8283 2019-07-24 stsp }
10439 0ebf8283 2019-07-24 stsp cmd = NULL;
10440 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
10441 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
10442 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
10443 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
10444 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
10445 0ebf8283 2019-07-24 stsp break;
10446 0ebf8283 2019-07-24 stsp }
10447 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
10448 0ebf8283 2019-07-24 stsp p++;
10449 0ebf8283 2019-07-24 stsp break;
10450 0ebf8283 2019-07-24 stsp }
10451 0ebf8283 2019-07-24 stsp }
10452 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
10453 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
10454 0ebf8283 2019-07-24 stsp break;
10455 0ebf8283 2019-07-24 stsp }
10456 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
10457 0ebf8283 2019-07-24 stsp p++;
10458 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
10459 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
10460 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
10461 0ebf8283 2019-07-24 stsp break;
10462 0ebf8283 2019-07-24 stsp }
10463 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
10464 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
10465 0ebf8283 2019-07-24 stsp if (err)
10466 0ebf8283 2019-07-24 stsp break;
10467 0ebf8283 2019-07-24 stsp } else {
10468 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
10469 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
10470 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
10471 0ebf8283 2019-07-24 stsp break;
10472 0ebf8283 2019-07-24 stsp }
10473 0ebf8283 2019-07-24 stsp }
10474 0ebf8283 2019-07-24 stsp free(line);
10475 0ebf8283 2019-07-24 stsp line = NULL;
10476 0ebf8283 2019-07-24 stsp continue;
10477 0ebf8283 2019-07-24 stsp } else {
10478 0ebf8283 2019-07-24 stsp end = p;
10479 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
10480 0ebf8283 2019-07-24 stsp end++;
10481 0ebf8283 2019-07-24 stsp *end = '\0';
10482 0ebf8283 2019-07-24 stsp
10483 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
10484 0ebf8283 2019-07-24 stsp if (err) {
10485 0ebf8283 2019-07-24 stsp /* override error code */
10486 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
10487 0ebf8283 2019-07-24 stsp break;
10488 0ebf8283 2019-07-24 stsp }
10489 0ebf8283 2019-07-24 stsp }
10490 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
10491 0ebf8283 2019-07-24 stsp if (hle == NULL) {
10492 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
10493 0ebf8283 2019-07-24 stsp break;
10494 0ebf8283 2019-07-24 stsp }
10495 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
10496 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
10497 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
10498 0ebf8283 2019-07-24 stsp commit_id = NULL;
10499 0ebf8283 2019-07-24 stsp free(line);
10500 0ebf8283 2019-07-24 stsp line = NULL;
10501 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
10502 0ebf8283 2019-07-24 stsp }
10503 0ebf8283 2019-07-24 stsp
10504 0ebf8283 2019-07-24 stsp free(line);
10505 0ebf8283 2019-07-24 stsp free(commit_id);
10506 0ebf8283 2019-07-24 stsp return err;
10507 0ebf8283 2019-07-24 stsp }
10508 0ebf8283 2019-07-24 stsp
10509 0ebf8283 2019-07-24 stsp static const struct got_error *
10510 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
10511 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
10512 bfce7f83 2019-07-27 stsp {
10513 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
10514 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
10515 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
10516 5b87815e 2020-03-05 stsp static char msg[92];
10517 bfce7f83 2019-07-27 stsp char *id_str;
10518 bfce7f83 2019-07-27 stsp
10519 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
10520 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
10521 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
10522 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
10523 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
10524 5b87815e 2020-03-05 stsp
10525 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
10526 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
10527 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
10528 5b87815e 2020-03-05 stsp if (hle == hle2)
10529 5b87815e 2020-03-05 stsp continue;
10530 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
10531 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
10532 5b87815e 2020-03-05 stsp continue;
10533 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
10534 5b87815e 2020-03-05 stsp if (err)
10535 5b87815e 2020-03-05 stsp return err;
10536 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
10537 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
10538 5b87815e 2020-03-05 stsp free(id_str);
10539 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10540 5b87815e 2020-03-05 stsp }
10541 5b87815e 2020-03-05 stsp }
10542 bfce7f83 2019-07-27 stsp
10543 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
10544 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
10545 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
10546 bfce7f83 2019-07-27 stsp break;
10547 bfce7f83 2019-07-27 stsp }
10548 bfce7f83 2019-07-27 stsp if (hle == NULL) {
10549 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
10550 bfce7f83 2019-07-27 stsp if (err)
10551 bfce7f83 2019-07-27 stsp return err;
10552 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
10553 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
10554 bfce7f83 2019-07-27 stsp free(id_str);
10555 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10556 bfce7f83 2019-07-27 stsp }
10557 bfce7f83 2019-07-27 stsp }
10558 bfce7f83 2019-07-27 stsp
10559 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
10560 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
10561 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
10562 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
10563 bfce7f83 2019-07-27 stsp
10564 bfce7f83 2019-07-27 stsp return NULL;
10565 bfce7f83 2019-07-27 stsp }
10566 bfce7f83 2019-07-27 stsp
10567 bfce7f83 2019-07-27 stsp static const struct got_error *
10568 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
10569 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
10570 bfce7f83 2019-07-27 stsp struct got_repository *repo)
10571 0ebf8283 2019-07-24 stsp {
10572 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
10573 0ebf8283 2019-07-24 stsp char *editor;
10574 0ebf8283 2019-07-24 stsp FILE *f = NULL;
10575 0ebf8283 2019-07-24 stsp
10576 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
10577 0ebf8283 2019-07-24 stsp if (err)
10578 0ebf8283 2019-07-24 stsp return err;
10579 0ebf8283 2019-07-24 stsp
10580 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
10581 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
10582 0ebf8283 2019-07-24 stsp goto done;
10583 0ebf8283 2019-07-24 stsp }
10584 0ebf8283 2019-07-24 stsp
10585 00fe21f2 2021-12-31 stsp f = fopen(path, "re");
10586 0ebf8283 2019-07-24 stsp if (f == NULL) {
10587 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
10588 0ebf8283 2019-07-24 stsp goto done;
10589 0ebf8283 2019-07-24 stsp }
10590 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
10591 bfce7f83 2019-07-27 stsp if (err)
10592 bfce7f83 2019-07-27 stsp goto done;
10593 bfce7f83 2019-07-27 stsp
10594 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
10595 0ebf8283 2019-07-24 stsp done:
10596 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
10597 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
10598 0ebf8283 2019-07-24 stsp free(editor);
10599 0ebf8283 2019-07-24 stsp return err;
10600 0ebf8283 2019-07-24 stsp }
10601 0ebf8283 2019-07-24 stsp
10602 0ebf8283 2019-07-24 stsp static const struct got_error *
10603 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
10604 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
10605 514f2ffe 2020-01-29 stsp struct got_repository *);
10606 0ebf8283 2019-07-24 stsp
10607 0ebf8283 2019-07-24 stsp static const struct got_error *
10608 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
10609 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
10610 b93c7142 2021-10-01 stsp int edit_logmsg_only, int fold_only, int edit_only,
10611 b93c7142 2021-10-01 stsp struct got_repository *repo)
10612 0ebf8283 2019-07-24 stsp {
10613 0ebf8283 2019-07-24 stsp const struct got_error *err;
10614 0ebf8283 2019-07-24 stsp FILE *f = NULL;
10615 0ebf8283 2019-07-24 stsp char *path = NULL;
10616 0ebf8283 2019-07-24 stsp
10617 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
10618 0ebf8283 2019-07-24 stsp if (err)
10619 0ebf8283 2019-07-24 stsp return err;
10620 0ebf8283 2019-07-24 stsp
10621 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
10622 0ebf8283 2019-07-24 stsp if (err)
10623 0ebf8283 2019-07-24 stsp goto done;
10624 0ebf8283 2019-07-24 stsp
10625 466785b9 2020-12-10 jrick err = histedit_write_commit_list(commits, f, edit_logmsg_only,
10626 b93c7142 2021-10-01 stsp fold_only, edit_only, repo);
10627 0ebf8283 2019-07-24 stsp if (err)
10628 0ebf8283 2019-07-24 stsp goto done;
10629 0ebf8283 2019-07-24 stsp
10630 b93c7142 2021-10-01 stsp if (edit_logmsg_only || fold_only || edit_only) {
10631 083957f4 2020-02-24 stsp rewind(f);
10632 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
10633 083957f4 2020-02-24 stsp } else {
10634 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF) {
10635 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
10636 0ebf8283 2019-07-24 stsp goto done;
10637 083957f4 2020-02-24 stsp }
10638 083957f4 2020-02-24 stsp f = NULL;
10639 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
10640 083957f4 2020-02-24 stsp if (err) {
10641 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10642 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
10643 083957f4 2020-02-24 stsp goto done;
10644 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
10645 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
10646 083957f4 2020-02-24 stsp }
10647 0ebf8283 2019-07-24 stsp }
10648 0ebf8283 2019-07-24 stsp done:
10649 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
10650 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
10651 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
10652 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
10653 0ebf8283 2019-07-24 stsp free(path);
10654 0ebf8283 2019-07-24 stsp return err;
10655 0ebf8283 2019-07-24 stsp }
10656 0ebf8283 2019-07-24 stsp
10657 0ebf8283 2019-07-24 stsp static const struct got_error *
10658 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
10659 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
10660 0ebf8283 2019-07-24 stsp {
10661 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
10662 0ebf8283 2019-07-24 stsp char *path = NULL;
10663 0ebf8283 2019-07-24 stsp FILE *f = NULL;
10664 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
10665 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
10666 0ebf8283 2019-07-24 stsp
10667 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
10668 0ebf8283 2019-07-24 stsp if (err)
10669 0ebf8283 2019-07-24 stsp return err;
10670 0ebf8283 2019-07-24 stsp
10671 00fe21f2 2021-12-31 stsp f = fopen(path, "we");
10672 0ebf8283 2019-07-24 stsp if (f == NULL) {
10673 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
10674 0ebf8283 2019-07-24 stsp goto done;
10675 0ebf8283 2019-07-24 stsp }
10676 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
10677 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
10678 0ebf8283 2019-07-24 stsp repo);
10679 0ebf8283 2019-07-24 stsp if (err)
10680 0ebf8283 2019-07-24 stsp break;
10681 0ebf8283 2019-07-24 stsp
10682 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
10683 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
10684 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
10685 0ebf8283 2019-07-24 stsp if (n < 0) {
10686 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
10687 0ebf8283 2019-07-24 stsp break;
10688 0ebf8283 2019-07-24 stsp }
10689 0ebf8283 2019-07-24 stsp }
10690 0ebf8283 2019-07-24 stsp }
10691 0ebf8283 2019-07-24 stsp done:
10692 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
10693 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
10694 0ebf8283 2019-07-24 stsp free(path);
10695 0ebf8283 2019-07-24 stsp if (commit)
10696 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10697 0ebf8283 2019-07-24 stsp return err;
10698 0ebf8283 2019-07-24 stsp }
10699 0ebf8283 2019-07-24 stsp
10700 bfce7f83 2019-07-27 stsp void
10701 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
10702 bfce7f83 2019-07-27 stsp {
10703 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
10704 bfce7f83 2019-07-27 stsp
10705 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
10706 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
10707 bfce7f83 2019-07-27 stsp free(hle);
10708 bfce7f83 2019-07-27 stsp }
10709 bfce7f83 2019-07-27 stsp }
10710 bfce7f83 2019-07-27 stsp
10711 0ebf8283 2019-07-24 stsp static const struct got_error *
10712 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
10713 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
10714 0ebf8283 2019-07-24 stsp {
10715 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
10716 0ebf8283 2019-07-24 stsp FILE *f = NULL;
10717 0ebf8283 2019-07-24 stsp
10718 00fe21f2 2021-12-31 stsp f = fopen(path, "re");
10719 0ebf8283 2019-07-24 stsp if (f == NULL) {
10720 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
10721 0ebf8283 2019-07-24 stsp goto done;
10722 0ebf8283 2019-07-24 stsp }
10723 0ebf8283 2019-07-24 stsp
10724 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
10725 0ebf8283 2019-07-24 stsp done:
10726 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
10727 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
10728 0ebf8283 2019-07-24 stsp return err;
10729 0ebf8283 2019-07-24 stsp }
10730 0ebf8283 2019-07-24 stsp
10731 0ebf8283 2019-07-24 stsp static const struct got_error *
10732 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
10733 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
10734 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
10735 0ebf8283 2019-07-24 stsp {
10736 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
10737 0ebf8283 2019-07-24 stsp int resp = ' ';
10738 0ebf8283 2019-07-24 stsp
10739 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
10740 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
10741 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
10742 0ebf8283 2019-07-24 stsp resp = getchar();
10743 a61a4414 2019-08-07 stsp if (resp == '\n')
10744 a61a4414 2019-08-07 stsp resp = getchar();
10745 426ebf2e 2019-07-25 stsp if (resp == 'c') {
10746 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
10747 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
10748 bfce7f83 2019-07-27 stsp repo);
10749 426ebf2e 2019-07-25 stsp if (err) {
10750 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10751 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
10752 426ebf2e 2019-07-25 stsp break;
10753 bfce7f83 2019-07-27 stsp prev_err = err;
10754 426ebf2e 2019-07-25 stsp resp = ' ';
10755 426ebf2e 2019-07-25 stsp continue;
10756 426ebf2e 2019-07-25 stsp }
10757 bfce7f83 2019-07-27 stsp break;
10758 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
10759 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
10760 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
10761 b93c7142 2021-10-01 stsp commits, branch_name, 0, 0, 0, repo);
10762 426ebf2e 2019-07-25 stsp if (err) {
10763 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10764 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
10765 426ebf2e 2019-07-25 stsp break;
10766 bfce7f83 2019-07-27 stsp prev_err = err;
10767 426ebf2e 2019-07-25 stsp resp = ' ';
10768 426ebf2e 2019-07-25 stsp continue;
10769 426ebf2e 2019-07-25 stsp }
10770 bfce7f83 2019-07-27 stsp break;
10771 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
10772 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
10773 0ebf8283 2019-07-24 stsp break;
10774 426ebf2e 2019-07-25 stsp } else
10775 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
10776 0ebf8283 2019-07-24 stsp }
10777 0ebf8283 2019-07-24 stsp
10778 0ebf8283 2019-07-24 stsp return err;
10779 0ebf8283 2019-07-24 stsp }
10780 0ebf8283 2019-07-24 stsp
10781 0ebf8283 2019-07-24 stsp static const struct got_error *
10782 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
10783 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
10784 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
10785 0ebf8283 2019-07-24 stsp {
10786 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
10787 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10788 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
10789 3e3a69f1 2019-07-25 stsp branch, repo);
10790 0ebf8283 2019-07-24 stsp }
10791 0ebf8283 2019-07-24 stsp
10792 0ebf8283 2019-07-24 stsp static const struct got_error *
10793 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
10794 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
10795 0ebf8283 2019-07-24 stsp {
10796 0ebf8283 2019-07-24 stsp const struct got_error *err;
10797 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10798 0ebf8283 2019-07-24 stsp
10799 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
10800 0ebf8283 2019-07-24 stsp if (err)
10801 0ebf8283 2019-07-24 stsp goto done;
10802 0ebf8283 2019-07-24 stsp
10803 0ebf8283 2019-07-24 stsp if (new_id) {
10804 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
10805 0ebf8283 2019-07-24 stsp if (err)
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 old_id_str[12] = '\0';
10810 0ebf8283 2019-07-24 stsp if (new_id_str)
10811 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
10812 0ebf8283 2019-07-24 stsp
10813 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
10814 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
10815 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
10816 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
10817 0ebf8283 2019-07-24 stsp goto done;
10818 0ebf8283 2019-07-24 stsp }
10819 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
10820 0ebf8283 2019-07-24 stsp } else {
10821 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
10822 0ebf8283 2019-07-24 stsp if (err)
10823 0ebf8283 2019-07-24 stsp goto done;
10824 0ebf8283 2019-07-24 stsp }
10825 0ebf8283 2019-07-24 stsp
10826 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
10827 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
10828 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
10829 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
10830 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
10831 0ebf8283 2019-07-24 stsp break;
10832 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
10833 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
10834 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
10835 0ebf8283 2019-07-24 stsp logmsg);
10836 0ebf8283 2019-07-24 stsp break;
10837 0ebf8283 2019-07-24 stsp default:
10838 0ebf8283 2019-07-24 stsp break;
10839 0ebf8283 2019-07-24 stsp }
10840 0ebf8283 2019-07-24 stsp done:
10841 0ebf8283 2019-07-24 stsp free(old_id_str);
10842 0ebf8283 2019-07-24 stsp free(new_id_str);
10843 0ebf8283 2019-07-24 stsp return err;
10844 0ebf8283 2019-07-24 stsp }
10845 0ebf8283 2019-07-24 stsp
10846 0ebf8283 2019-07-24 stsp static const struct got_error *
10847 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
10848 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
10849 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
10850 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
10851 0ebf8283 2019-07-24 stsp {
10852 0ebf8283 2019-07-24 stsp const struct got_error *err;
10853 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
10854 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
10855 0ebf8283 2019-07-24 stsp
10856 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
10857 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
10858 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
10859 0ebf8283 2019-07-24 stsp if (err)
10860 0ebf8283 2019-07-24 stsp return err;
10861 0ebf8283 2019-07-24 stsp }
10862 0ebf8283 2019-07-24 stsp
10863 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10864 0ebf8283 2019-07-24 stsp if (err)
10865 0ebf8283 2019-07-24 stsp return err;
10866 0ebf8283 2019-07-24 stsp
10867 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
10868 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
10869 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
10870 0ebf8283 2019-07-24 stsp if (err) {
10871 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
10872 0ebf8283 2019-07-24 stsp goto done;
10873 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
10874 0ebf8283 2019-07-24 stsp } else {
10875 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
10876 0ebf8283 2019-07-24 stsp free(new_commit_id);
10877 0ebf8283 2019-07-24 stsp }
10878 0ebf8283 2019-07-24 stsp done:
10879 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10880 0ebf8283 2019-07-24 stsp return err;
10881 0ebf8283 2019-07-24 stsp }
10882 0ebf8283 2019-07-24 stsp
10883 0ebf8283 2019-07-24 stsp static const struct got_error *
10884 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
10885 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
10886 0ebf8283 2019-07-24 stsp {
10887 0ebf8283 2019-07-24 stsp const struct got_error *error;
10888 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
10889 0ebf8283 2019-07-24 stsp
10890 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
10891 0ebf8283 2019-07-24 stsp repo);
10892 0ebf8283 2019-07-24 stsp if (error)
10893 0ebf8283 2019-07-24 stsp return error;
10894 0ebf8283 2019-07-24 stsp
10895 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
10896 0ebf8283 2019-07-24 stsp if (error)
10897 0ebf8283 2019-07-24 stsp return error;
10898 0ebf8283 2019-07-24 stsp
10899 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
10900 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10901 0ebf8283 2019-07-24 stsp return error;
10902 ab20a43a 2020-01-29 stsp }
10903 ab20a43a 2020-01-29 stsp
10904 ab20a43a 2020-01-29 stsp static const struct got_error *
10905 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
10906 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
10907 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
10908 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
10909 ab20a43a 2020-01-29 stsp {
10910 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
10911 ab20a43a 2020-01-29 stsp
10912 ab20a43a 2020-01-29 stsp switch (status) {
10913 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
10914 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
10915 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
10916 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
10917 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
10918 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
10919 ab20a43a 2020-01-29 stsp default:
10920 ab20a43a 2020-01-29 stsp break;
10921 ab20a43a 2020-01-29 stsp }
10922 ab20a43a 2020-01-29 stsp
10923 ab20a43a 2020-01-29 stsp switch (staged_status) {
10924 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
10925 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
10926 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
10927 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
10928 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
10929 ab20a43a 2020-01-29 stsp default:
10930 ab20a43a 2020-01-29 stsp break;
10931 ab20a43a 2020-01-29 stsp }
10932 ab20a43a 2020-01-29 stsp
10933 ab20a43a 2020-01-29 stsp return NULL;
10934 0ebf8283 2019-07-24 stsp }
10935 0ebf8283 2019-07-24 stsp
10936 0ebf8283 2019-07-24 stsp static const struct got_error *
10937 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
10938 0ebf8283 2019-07-24 stsp {
10939 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
10940 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
10941 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
10942 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
10943 0ebf8283 2019-07-24 stsp char *cwd = NULL;
10944 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
10945 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
10946 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
10947 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
10948 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
10949 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
10950 f259c4c1 2021-09-24 stsp int ch, rebase_in_progress = 0, merge_in_progress = 0;
10951 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10952 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
10953 b93c7142 2021-10-01 stsp int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
10954 643b85bc 2021-07-16 stsp int list_backups = 0, delete_backups = 0;
10955 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
10956 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
10957 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
10958 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
10959 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
10960 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
10961 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
10962 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
10963 0ebf8283 2019-07-24 stsp
10964 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
10965 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
10966 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
10967 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10968 0ebf8283 2019-07-24 stsp
10969 b93c7142 2021-10-01 stsp while ((ch = getopt(argc, argv, "acefF:mlX")) != -1) {
10970 0ebf8283 2019-07-24 stsp switch (ch) {
10971 0ebf8283 2019-07-24 stsp case 'a':
10972 0ebf8283 2019-07-24 stsp abort_edit = 1;
10973 0ebf8283 2019-07-24 stsp break;
10974 0ebf8283 2019-07-24 stsp case 'c':
10975 0ebf8283 2019-07-24 stsp continue_edit = 1;
10976 0ebf8283 2019-07-24 stsp break;
10977 b93c7142 2021-10-01 stsp case 'e':
10978 b93c7142 2021-10-01 stsp edit_only = 1;
10979 b93c7142 2021-10-01 stsp break;
10980 466785b9 2020-12-10 jrick case 'f':
10981 466785b9 2020-12-10 jrick fold_only = 1;
10982 466785b9 2020-12-10 jrick break;
10983 0ebf8283 2019-07-24 stsp case 'F':
10984 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
10985 0ebf8283 2019-07-24 stsp break;
10986 083957f4 2020-02-24 stsp case 'm':
10987 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
10988 083957f4 2020-02-24 stsp break;
10989 e600f124 2021-03-21 stsp case 'l':
10990 e600f124 2021-03-21 stsp list_backups = 1;
10991 e600f124 2021-03-21 stsp break;
10992 643b85bc 2021-07-16 stsp case 'X':
10993 643b85bc 2021-07-16 stsp delete_backups = 1;
10994 643b85bc 2021-07-16 stsp break;
10995 0ebf8283 2019-07-24 stsp default:
10996 0ebf8283 2019-07-24 stsp usage_histedit();
10997 0ebf8283 2019-07-24 stsp /* NOTREACHED */
10998 0ebf8283 2019-07-24 stsp }
10999 0ebf8283 2019-07-24 stsp }
11000 0ebf8283 2019-07-24 stsp
11001 0ebf8283 2019-07-24 stsp argc -= optind;
11002 0ebf8283 2019-07-24 stsp argv += optind;
11003 0ebf8283 2019-07-24 stsp
11004 0ebf8283 2019-07-24 stsp #ifndef PROFILE
11005 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11006 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
11007 0ebf8283 2019-07-24 stsp err(1, "pledge");
11008 0ebf8283 2019-07-24 stsp #endif
11009 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
11010 ff69268e 2020-12-13 stsp option_conflict('a', 'c');
11011 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
11012 ff69268e 2020-12-13 stsp option_conflict('F', 'm');
11013 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
11014 ff69268e 2020-12-13 stsp option_conflict('a', 'm');
11015 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
11016 ff69268e 2020-12-13 stsp option_conflict('c', 'm');
11017 466785b9 2020-12-10 jrick if (abort_edit && fold_only)
11018 ff69268e 2020-12-13 stsp option_conflict('a', 'f');
11019 ff69268e 2020-12-13 stsp if (continue_edit && fold_only)
11020 ff69268e 2020-12-13 stsp option_conflict('c', 'f');
11021 466785b9 2020-12-10 jrick if (fold_only && edit_logmsg_only)
11022 ff69268e 2020-12-13 stsp option_conflict('f', 'm');
11023 b3805337 2020-12-13 stsp if (edit_script_path && fold_only)
11024 b3805337 2020-12-13 stsp option_conflict('F', 'f');
11025 b93c7142 2021-10-01 stsp if (abort_edit && edit_only)
11026 b93c7142 2021-10-01 stsp option_conflict('a', 'e');
11027 b93c7142 2021-10-01 stsp if (continue_edit && edit_only)
11028 b93c7142 2021-10-01 stsp option_conflict('c', 'e');
11029 b93c7142 2021-10-01 stsp if (edit_only && edit_logmsg_only)
11030 b93c7142 2021-10-01 stsp option_conflict('e', 'm');
11031 b93c7142 2021-10-01 stsp if (edit_script_path && edit_only)
11032 b93c7142 2021-10-01 stsp option_conflict('F', 'e');
11033 e600f124 2021-03-21 stsp if (list_backups) {
11034 e600f124 2021-03-21 stsp if (abort_edit)
11035 e600f124 2021-03-21 stsp option_conflict('l', 'a');
11036 e600f124 2021-03-21 stsp if (continue_edit)
11037 e600f124 2021-03-21 stsp option_conflict('l', 'c');
11038 e600f124 2021-03-21 stsp if (edit_script_path)
11039 e600f124 2021-03-21 stsp option_conflict('l', 'F');
11040 e600f124 2021-03-21 stsp if (edit_logmsg_only)
11041 e600f124 2021-03-21 stsp option_conflict('l', 'm');
11042 e600f124 2021-03-21 stsp if (fold_only)
11043 e600f124 2021-03-21 stsp option_conflict('l', 'f');
11044 b93c7142 2021-10-01 stsp if (edit_only)
11045 b93c7142 2021-10-01 stsp option_conflict('l', 'e');
11046 643b85bc 2021-07-16 stsp if (delete_backups)
11047 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
11048 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
11049 e600f124 2021-03-21 stsp usage_histedit();
11050 643b85bc 2021-07-16 stsp } else if (delete_backups) {
11051 643b85bc 2021-07-16 stsp if (abort_edit)
11052 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
11053 643b85bc 2021-07-16 stsp if (continue_edit)
11054 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
11055 643b85bc 2021-07-16 stsp if (edit_script_path)
11056 643b85bc 2021-07-16 stsp option_conflict('X', 'F');
11057 643b85bc 2021-07-16 stsp if (edit_logmsg_only)
11058 643b85bc 2021-07-16 stsp option_conflict('X', 'm');
11059 643b85bc 2021-07-16 stsp if (fold_only)
11060 643b85bc 2021-07-16 stsp option_conflict('X', 'f');
11061 b93c7142 2021-10-01 stsp if (edit_only)
11062 b93c7142 2021-10-01 stsp option_conflict('X', 'e');
11063 643b85bc 2021-07-16 stsp if (list_backups)
11064 643b85bc 2021-07-16 stsp option_conflict('X', 'l');
11065 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
11066 643b85bc 2021-07-16 stsp usage_histedit();
11067 e600f124 2021-03-21 stsp } else if (argc != 0)
11068 0ebf8283 2019-07-24 stsp usage_histedit();
11069 0ebf8283 2019-07-24 stsp
11070 0ebf8283 2019-07-24 stsp /*
11071 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
11072 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
11073 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
11074 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
11075 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
11076 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
11077 0ebf8283 2019-07-24 stsp */
11078 0ebf8283 2019-07-24 stsp
11079 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
11080 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
11081 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
11082 0ebf8283 2019-07-24 stsp goto done;
11083 0ebf8283 2019-07-24 stsp }
11084 0ae84acc 2022-06-15 tracey
11085 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
11086 0ae84acc 2022-06-15 tracey if (error != NULL)
11087 0ae84acc 2022-06-15 tracey goto done;
11088 0ae84acc 2022-06-15 tracey
11089 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
11090 fa51e947 2020-03-27 stsp if (error) {
11091 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
11092 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
11093 e600f124 2021-03-21 stsp goto done;
11094 e600f124 2021-03-21 stsp } else {
11095 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
11096 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
11097 e600f124 2021-03-21 stsp "histedit", cwd);
11098 e600f124 2021-03-21 stsp goto done;
11099 e600f124 2021-03-21 stsp }
11100 e600f124 2021-03-21 stsp }
11101 e600f124 2021-03-21 stsp
11102 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
11103 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
11104 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd,
11105 0ae84acc 2022-06-15 tracey NULL, pack_fds);
11106 e600f124 2021-03-21 stsp if (error != NULL)
11107 e600f124 2021-03-21 stsp goto done;
11108 e600f124 2021-03-21 stsp error = apply_unveil(got_repo_get_path(repo), 0,
11109 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
11110 e600f124 2021-03-21 stsp if (error)
11111 e600f124 2021-03-21 stsp goto done;
11112 643b85bc 2021-07-16 stsp error = process_backup_refs(
11113 e600f124 2021-03-21 stsp GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
11114 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
11115 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
11116 fa51e947 2020-03-27 stsp }
11117 0ebf8283 2019-07-24 stsp
11118 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11119 0ae84acc 2022-06-15 tracey NULL, pack_fds);
11120 0ebf8283 2019-07-24 stsp if (error != NULL)
11121 0ebf8283 2019-07-24 stsp goto done;
11122 0ebf8283 2019-07-24 stsp
11123 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11124 0ebf8283 2019-07-24 stsp if (error)
11125 0ebf8283 2019-07-24 stsp goto done;
11126 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
11127 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
11128 f259c4c1 2021-09-24 stsp goto done;
11129 f259c4c1 2021-09-24 stsp }
11130 f259c4c1 2021-09-24 stsp
11131 f259c4c1 2021-09-24 stsp error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11132 f259c4c1 2021-09-24 stsp repo);
11133 f259c4c1 2021-09-24 stsp if (error)
11134 f259c4c1 2021-09-24 stsp goto done;
11135 f259c4c1 2021-09-24 stsp if (merge_in_progress) {
11136 f259c4c1 2021-09-24 stsp error = got_error(GOT_ERR_MERGE_BUSY);
11137 0ebf8283 2019-07-24 stsp goto done;
11138 0ebf8283 2019-07-24 stsp }
11139 0ebf8283 2019-07-24 stsp
11140 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
11141 0ebf8283 2019-07-24 stsp if (error)
11142 0ebf8283 2019-07-24 stsp goto done;
11143 0ebf8283 2019-07-24 stsp
11144 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
11145 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11146 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
11147 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
11148 083957f4 2020-02-24 stsp "before the -m option can be used");
11149 083957f4 2020-02-24 stsp goto done;
11150 083957f4 2020-02-24 stsp }
11151 466785b9 2020-12-10 jrick if (edit_in_progress && fold_only) {
11152 466785b9 2020-12-10 jrick error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11153 466785b9 2020-12-10 jrick "histedit operation is in progress in this "
11154 466785b9 2020-12-10 jrick "work tree and must be continued or aborted "
11155 466785b9 2020-12-10 jrick "before the -f option can be used");
11156 466785b9 2020-12-10 jrick goto done;
11157 466785b9 2020-12-10 jrick }
11158 b93c7142 2021-10-01 stsp if (edit_in_progress && edit_only) {
11159 b93c7142 2021-10-01 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11160 b93c7142 2021-10-01 stsp "histedit operation is in progress in this "
11161 b93c7142 2021-10-01 stsp "work tree and must be continued or aborted "
11162 b93c7142 2021-10-01 stsp "before the -e option can be used");
11163 b93c7142 2021-10-01 stsp goto done;
11164 b93c7142 2021-10-01 stsp }
11165 083957f4 2020-02-24 stsp
11166 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
11167 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
11168 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
11169 3e3a69f1 2019-07-25 stsp worktree, repo);
11170 0ebf8283 2019-07-24 stsp if (error)
11171 0ebf8283 2019-07-24 stsp goto done;
11172 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
11173 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
11174 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
11175 41f061b2 2021-10-05 stsp branch, base_commit_id, abort_progress, &upa);
11176 0ebf8283 2019-07-24 stsp if (error)
11177 0ebf8283 2019-07-24 stsp goto done;
11178 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
11179 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
11180 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
11181 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
11182 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
11183 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
11184 0ebf8283 2019-07-24 stsp goto done;
11185 0ebf8283 2019-07-24 stsp }
11186 0ebf8283 2019-07-24 stsp
11187 0ebf8283 2019-07-24 stsp if (continue_edit) {
11188 0ebf8283 2019-07-24 stsp char *path;
11189 0ebf8283 2019-07-24 stsp
11190 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
11191 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
11192 0ebf8283 2019-07-24 stsp goto done;
11193 0ebf8283 2019-07-24 stsp }
11194 0ebf8283 2019-07-24 stsp
11195 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
11196 0ebf8283 2019-07-24 stsp if (error)
11197 0ebf8283 2019-07-24 stsp goto done;
11198 0ebf8283 2019-07-24 stsp
11199 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
11200 0ebf8283 2019-07-24 stsp free(path);
11201 0ebf8283 2019-07-24 stsp if (error)
11202 0ebf8283 2019-07-24 stsp goto done;
11203 0ebf8283 2019-07-24 stsp
11204 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
11205 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
11206 3e3a69f1 2019-07-25 stsp worktree, repo);
11207 0ebf8283 2019-07-24 stsp if (error)
11208 0ebf8283 2019-07-24 stsp goto done;
11209 0ebf8283 2019-07-24 stsp
11210 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
11211 0ebf8283 2019-07-24 stsp if (error)
11212 0ebf8283 2019-07-24 stsp goto done;
11213 0ebf8283 2019-07-24 stsp
11214 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
11215 0ebf8283 2019-07-24 stsp head_commit_id);
11216 0ebf8283 2019-07-24 stsp if (error)
11217 0ebf8283 2019-07-24 stsp goto done;
11218 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
11219 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
11220 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
11221 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11222 3aac7cf7 2019-07-25 stsp goto done;
11223 3aac7cf7 2019-07-25 stsp }
11224 d7b5a0e8 2022-04-20 stsp error = collect_commits(&commits, head_commit_id, &pid->id,
11225 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
11226 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
11227 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
11228 0ebf8283 2019-07-24 stsp commit = NULL;
11229 0ebf8283 2019-07-24 stsp if (error)
11230 0ebf8283 2019-07-24 stsp goto done;
11231 0ebf8283 2019-07-24 stsp } else {
11232 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
11233 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
11234 0ebf8283 2019-07-24 stsp goto done;
11235 0ebf8283 2019-07-24 stsp }
11236 0ebf8283 2019-07-24 stsp
11237 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
11238 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
11239 0ebf8283 2019-07-24 stsp if (error != NULL)
11240 0ebf8283 2019-07-24 stsp goto done;
11241 0ebf8283 2019-07-24 stsp
11242 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11243 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11244 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
11245 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
11246 c7d20a3f 2019-07-30 stsp goto done;
11247 c7d20a3f 2019-07-30 stsp }
11248 c7d20a3f 2019-07-30 stsp
11249 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
11250 bfce7f83 2019-07-27 stsp got_ref_close(branch);
11251 bfce7f83 2019-07-27 stsp branch = NULL;
11252 0ebf8283 2019-07-24 stsp if (error)
11253 0ebf8283 2019-07-24 stsp goto done;
11254 0ebf8283 2019-07-24 stsp
11255 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
11256 0ebf8283 2019-07-24 stsp head_commit_id);
11257 0ebf8283 2019-07-24 stsp if (error)
11258 0ebf8283 2019-07-24 stsp goto done;
11259 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
11260 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
11261 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
11262 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11263 3aac7cf7 2019-07-25 stsp goto done;
11264 3aac7cf7 2019-07-25 stsp }
11265 d7b5a0e8 2022-04-20 stsp error = collect_commits(&commits, head_commit_id, &pid->id,
11266 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
11267 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
11268 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
11269 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
11270 0ebf8283 2019-07-24 stsp commit = NULL;
11271 0ebf8283 2019-07-24 stsp if (error)
11272 0ebf8283 2019-07-24 stsp goto done;
11273 0ebf8283 2019-07-24 stsp
11274 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
11275 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11276 c5996fff 2020-01-29 stsp goto done;
11277 c5996fff 2020-01-29 stsp }
11278 c5996fff 2020-01-29 stsp
11279 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
11280 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
11281 bfce7f83 2019-07-27 stsp if (error)
11282 bfce7f83 2019-07-27 stsp goto done;
11283 bfce7f83 2019-07-27 stsp
11284 0ebf8283 2019-07-24 stsp if (edit_script_path) {
11285 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
11286 0ebf8283 2019-07-24 stsp edit_script_path, repo);
11287 6ba2bea2 2019-07-27 stsp if (error) {
11288 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
11289 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
11290 41f061b2 2021-10-05 stsp abort_progress, &upa);
11291 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
11292 0ebf8283 2019-07-24 stsp goto done;
11293 6ba2bea2 2019-07-27 stsp }
11294 0ebf8283 2019-07-24 stsp } else {
11295 514f2ffe 2020-01-29 stsp const char *branch_name;
11296 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
11297 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
11298 514f2ffe 2020-01-29 stsp branch_name += 11;
11299 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
11300 b93c7142 2021-10-01 stsp branch_name, edit_logmsg_only, fold_only,
11301 b93c7142 2021-10-01 stsp edit_only, repo);
11302 6ba2bea2 2019-07-27 stsp if (error) {
11303 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
11304 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
11305 41f061b2 2021-10-05 stsp abort_progress, &upa);
11306 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
11307 0ebf8283 2019-07-24 stsp goto done;
11308 6ba2bea2 2019-07-27 stsp }
11309 0ebf8283 2019-07-24 stsp
11310 0ebf8283 2019-07-24 stsp }
11311 0ebf8283 2019-07-24 stsp
11312 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
11313 0ebf8283 2019-07-24 stsp repo);
11314 6ba2bea2 2019-07-27 stsp if (error) {
11315 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
11316 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
11317 41f061b2 2021-10-05 stsp abort_progress, &upa);
11318 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
11319 0ebf8283 2019-07-24 stsp goto done;
11320 6ba2bea2 2019-07-27 stsp }
11321 0ebf8283 2019-07-24 stsp
11322 0ebf8283 2019-07-24 stsp }
11323 0ebf8283 2019-07-24 stsp
11324 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
11325 4ec14e60 2019-08-28 hiltjo if (error)
11326 0ebf8283 2019-07-24 stsp goto done;
11327 0ebf8283 2019-07-24 stsp
11328 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
11329 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
11330 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
11331 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
11332 0ebf8283 2019-07-24 stsp continue;
11333 0ebf8283 2019-07-24 stsp
11334 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
11335 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
11336 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
11337 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
11338 62d463ca 2020-10-20 naddy repo);
11339 ab20a43a 2020-01-29 stsp if (error)
11340 ab20a43a 2020-01-29 stsp goto done;
11341 0ebf8283 2019-07-24 stsp } else {
11342 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
11343 ab20a43a 2020-01-29 stsp int have_changes = 0;
11344 ab20a43a 2020-01-29 stsp
11345 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
11346 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
11347 ab20a43a 2020-01-29 stsp if (error)
11348 ab20a43a 2020-01-29 stsp goto done;
11349 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
11350 f6343036 2021-06-22 stsp repo, 0, check_local_changes, &have_changes,
11351 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
11352 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
11353 ab20a43a 2020-01-29 stsp if (error) {
11354 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
11355 ab20a43a 2020-01-29 stsp goto done;
11356 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
11357 ab20a43a 2020-01-29 stsp goto done;
11358 ab20a43a 2020-01-29 stsp }
11359 ab20a43a 2020-01-29 stsp if (have_changes) {
11360 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
11361 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
11362 ab20a43a 2020-01-29 stsp if (error)
11363 ab20a43a 2020-01-29 stsp goto done;
11364 ab20a43a 2020-01-29 stsp } else {
11365 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
11366 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
11367 ab20a43a 2020-01-29 stsp if (error)
11368 ab20a43a 2020-01-29 stsp goto done;
11369 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
11370 ab20a43a 2020-01-29 stsp hle, NULL);
11371 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
11372 ab20a43a 2020-01-29 stsp commit = NULL;
11373 ab20a43a 2020-01-29 stsp if (error)
11374 ab20a43a 2020-01-29 stsp goto done;
11375 ab20a43a 2020-01-29 stsp }
11376 0ebf8283 2019-07-24 stsp }
11377 0ebf8283 2019-07-24 stsp continue;
11378 0ebf8283 2019-07-24 stsp }
11379 0ebf8283 2019-07-24 stsp
11380 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
11381 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
11382 0ebf8283 2019-07-24 stsp if (error)
11383 0ebf8283 2019-07-24 stsp goto done;
11384 0ebf8283 2019-07-24 stsp continue;
11385 0ebf8283 2019-07-24 stsp }
11386 0ebf8283 2019-07-24 stsp
11387 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
11388 0ebf8283 2019-07-24 stsp hle->commit_id);
11389 0ebf8283 2019-07-24 stsp if (error)
11390 0ebf8283 2019-07-24 stsp goto done;
11391 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
11392 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
11393 0ebf8283 2019-07-24 stsp
11394 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
11395 d7b5a0e8 2022-04-20 stsp worktree, fileindex, &pid->id, hle->commit_id, repo,
11396 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
11397 0ebf8283 2019-07-24 stsp if (error)
11398 0ebf8283 2019-07-24 stsp goto done;
11399 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
11400 0ebf8283 2019-07-24 stsp commit = NULL;
11401 0ebf8283 2019-07-24 stsp
11402 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
11403 cd33da48 2021-09-28 stsp if (upa.conflicts > 0 || upa.missing > 0 ||
11404 cd33da48 2021-09-28 stsp upa.not_deleted > 0 || upa.unversioned > 0) {
11405 cd33da48 2021-09-28 stsp if (upa.conflicts > 0) {
11406 cd33da48 2021-09-28 stsp error = show_rebase_merge_conflict(
11407 cd33da48 2021-09-28 stsp hle->commit_id, repo);
11408 cd33da48 2021-09-28 stsp if (error)
11409 cd33da48 2021-09-28 stsp goto done;
11410 cd33da48 2021-09-28 stsp }
11411 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
11412 0ebf8283 2019-07-24 stsp break;
11413 0ebf8283 2019-07-24 stsp }
11414 0ebf8283 2019-07-24 stsp
11415 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
11416 0ebf8283 2019-07-24 stsp char *id_str;
11417 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
11418 0ebf8283 2019-07-24 stsp if (error)
11419 0ebf8283 2019-07-24 stsp goto done;
11420 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
11421 0ebf8283 2019-07-24 stsp id_str);
11422 0ebf8283 2019-07-24 stsp free(id_str);
11423 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
11424 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
11425 3e3a69f1 2019-07-25 stsp fileindex);
11426 0ebf8283 2019-07-24 stsp goto done;
11427 0ebf8283 2019-07-24 stsp }
11428 0ebf8283 2019-07-24 stsp
11429 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
11430 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
11431 0ebf8283 2019-07-24 stsp if (error)
11432 0ebf8283 2019-07-24 stsp goto done;
11433 0ebf8283 2019-07-24 stsp continue;
11434 0ebf8283 2019-07-24 stsp }
11435 0ebf8283 2019-07-24 stsp
11436 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
11437 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
11438 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
11439 0ebf8283 2019-07-24 stsp if (error)
11440 0ebf8283 2019-07-24 stsp goto done;
11441 0ebf8283 2019-07-24 stsp }
11442 0ebf8283 2019-07-24 stsp
11443 cd33da48 2021-09-28 stsp if (upa.conflicts > 0 || upa.missing > 0 ||
11444 cd33da48 2021-09-28 stsp upa.not_deleted > 0 || upa.unversioned > 0) {
11445 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
11446 0ebf8283 2019-07-24 stsp if (error)
11447 0ebf8283 2019-07-24 stsp goto done;
11448 cd33da48 2021-09-28 stsp if (upa.conflicts > 0 && upa.missing == 0 &&
11449 cd33da48 2021-09-28 stsp upa.not_deleted == 0 && upa.unversioned == 0) {
11450 cd33da48 2021-09-28 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
11451 cd33da48 2021-09-28 stsp "conflicts must be resolved before histedit "
11452 cd33da48 2021-09-28 stsp "can continue");
11453 cd33da48 2021-09-28 stsp } else if (upa.conflicts > 0) {
11454 cd33da48 2021-09-28 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
11455 cd33da48 2021-09-28 stsp "conflicts must be resolved before histedit "
11456 cd33da48 2021-09-28 stsp "can continue; changes destined for some "
11457 cd33da48 2021-09-28 stsp "files were not yet merged and should be "
11458 cd33da48 2021-09-28 stsp "merged manually if required before the "
11459 cd33da48 2021-09-28 stsp "histedit operation is continued");
11460 cd33da48 2021-09-28 stsp } else {
11461 cd33da48 2021-09-28 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
11462 cd33da48 2021-09-28 stsp "changes destined for some files were not "
11463 cd33da48 2021-09-28 stsp "yet merged and should be merged manually "
11464 cd33da48 2021-09-28 stsp "if required before the histedit operation "
11465 cd33da48 2021-09-28 stsp "is continued");
11466 cd33da48 2021-09-28 stsp }
11467 0ebf8283 2019-07-24 stsp } else
11468 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
11469 3e3a69f1 2019-07-25 stsp branch, repo);
11470 0ebf8283 2019-07-24 stsp done:
11471 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
11472 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
11473 0ebf8283 2019-07-24 stsp free(head_commit_id);
11474 0ebf8283 2019-07-24 stsp free(base_commit_id);
11475 0ebf8283 2019-07-24 stsp free(resume_commit_id);
11476 0ebf8283 2019-07-24 stsp if (commit)
11477 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
11478 0ebf8283 2019-07-24 stsp if (branch)
11479 0ebf8283 2019-07-24 stsp got_ref_close(branch);
11480 0ebf8283 2019-07-24 stsp if (tmp_branch)
11481 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
11482 0ebf8283 2019-07-24 stsp if (worktree)
11483 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
11484 1d0f4054 2021-06-17 stsp if (repo) {
11485 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
11486 1d0f4054 2021-06-17 stsp if (error == NULL)
11487 1d0f4054 2021-06-17 stsp error = close_err;
11488 0ae84acc 2022-06-15 tracey }
11489 0ae84acc 2022-06-15 tracey if (pack_fds) {
11490 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
11491 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
11492 0ae84acc 2022-06-15 tracey if (error == NULL)
11493 0ae84acc 2022-06-15 tracey error = pack_err;
11494 1d0f4054 2021-06-17 stsp }
11495 2822a352 2019-10-15 stsp return error;
11496 2822a352 2019-10-15 stsp }
11497 2822a352 2019-10-15 stsp
11498 2822a352 2019-10-15 stsp __dead static void
11499 2822a352 2019-10-15 stsp usage_integrate(void)
11500 2822a352 2019-10-15 stsp {
11501 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
11502 2822a352 2019-10-15 stsp exit(1);
11503 2822a352 2019-10-15 stsp }
11504 2822a352 2019-10-15 stsp
11505 2822a352 2019-10-15 stsp static const struct got_error *
11506 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
11507 2822a352 2019-10-15 stsp {
11508 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
11509 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
11510 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
11511 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
11512 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
11513 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
11514 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
11515 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
11516 9627c110 2020-04-18 stsp int ch;
11517 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
11518 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
11519 2822a352 2019-10-15 stsp
11520 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
11521 2822a352 2019-10-15 stsp switch (ch) {
11522 2822a352 2019-10-15 stsp default:
11523 2822a352 2019-10-15 stsp usage_integrate();
11524 2822a352 2019-10-15 stsp /* NOTREACHED */
11525 2822a352 2019-10-15 stsp }
11526 2822a352 2019-10-15 stsp }
11527 2822a352 2019-10-15 stsp
11528 2822a352 2019-10-15 stsp argc -= optind;
11529 2822a352 2019-10-15 stsp argv += optind;
11530 2822a352 2019-10-15 stsp
11531 2822a352 2019-10-15 stsp if (argc != 1)
11532 2822a352 2019-10-15 stsp usage_integrate();
11533 2822a352 2019-10-15 stsp branch_arg = argv[0];
11534 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
11535 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11536 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
11537 2822a352 2019-10-15 stsp err(1, "pledge");
11538 b08a0ccd 2020-09-24 stsp #endif
11539 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
11540 2822a352 2019-10-15 stsp if (cwd == NULL) {
11541 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
11542 2822a352 2019-10-15 stsp goto done;
11543 2822a352 2019-10-15 stsp }
11544 2822a352 2019-10-15 stsp
11545 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
11546 0ae84acc 2022-06-15 tracey if (error != NULL)
11547 0ae84acc 2022-06-15 tracey goto done;
11548 0ae84acc 2022-06-15 tracey
11549 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
11550 fa51e947 2020-03-27 stsp if (error) {
11551 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
11552 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
11553 fa51e947 2020-03-27 stsp cwd);
11554 2822a352 2019-10-15 stsp goto done;
11555 fa51e947 2020-03-27 stsp }
11556 2822a352 2019-10-15 stsp
11557 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
11558 2822a352 2019-10-15 stsp if (error)
11559 2822a352 2019-10-15 stsp goto done;
11560 2822a352 2019-10-15 stsp
11561 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11562 0ae84acc 2022-06-15 tracey NULL, pack_fds);
11563 2822a352 2019-10-15 stsp if (error != NULL)
11564 2822a352 2019-10-15 stsp goto done;
11565 2822a352 2019-10-15 stsp
11566 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
11567 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
11568 2822a352 2019-10-15 stsp if (error)
11569 2822a352 2019-10-15 stsp goto done;
11570 2822a352 2019-10-15 stsp
11571 f259c4c1 2021-09-24 stsp error = check_merge_in_progress(worktree, repo);
11572 f259c4c1 2021-09-24 stsp if (error)
11573 f259c4c1 2021-09-24 stsp goto done;
11574 f259c4c1 2021-09-24 stsp
11575 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
11576 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
11577 2822a352 2019-10-15 stsp goto done;
11578 2822a352 2019-10-15 stsp }
11579 2822a352 2019-10-15 stsp
11580 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
11581 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
11582 2822a352 2019-10-15 stsp if (error)
11583 2822a352 2019-10-15 stsp goto done;
11584 2822a352 2019-10-15 stsp
11585 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
11586 2822a352 2019-10-15 stsp if (refname == NULL) {
11587 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
11588 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
11589 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
11590 2822a352 2019-10-15 stsp goto done;
11591 2822a352 2019-10-15 stsp }
11592 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
11593 2822a352 2019-10-15 stsp if (base_refname == NULL) {
11594 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
11595 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
11596 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
11597 2822a352 2019-10-15 stsp goto done;
11598 2822a352 2019-10-15 stsp }
11599 2822a352 2019-10-15 stsp
11600 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
11601 2822a352 2019-10-15 stsp if (error)
11602 2822a352 2019-10-15 stsp goto done;
11603 2822a352 2019-10-15 stsp
11604 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
11605 2822a352 2019-10-15 stsp if (error)
11606 2822a352 2019-10-15 stsp goto done;
11607 2822a352 2019-10-15 stsp
11608 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
11609 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
11610 2822a352 2019-10-15 stsp "specified branch has already been integrated");
11611 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
11612 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
11613 2822a352 2019-10-15 stsp goto done;
11614 2822a352 2019-10-15 stsp }
11615 2822a352 2019-10-15 stsp
11616 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
11617 2822a352 2019-10-15 stsp if (error) {
11618 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
11619 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
11620 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
11621 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
11622 2822a352 2019-10-15 stsp goto done;
11623 2822a352 2019-10-15 stsp }
11624 2822a352 2019-10-15 stsp
11625 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
11626 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
11627 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
11628 2822a352 2019-10-15 stsp check_cancelled, NULL);
11629 2822a352 2019-10-15 stsp if (error)
11630 2822a352 2019-10-15 stsp goto done;
11631 2822a352 2019-10-15 stsp
11632 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
11633 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
11634 2822a352 2019-10-15 stsp done:
11635 1d0f4054 2021-06-17 stsp if (repo) {
11636 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
11637 1d0f4054 2021-06-17 stsp if (error == NULL)
11638 1d0f4054 2021-06-17 stsp error = close_err;
11639 1d0f4054 2021-06-17 stsp }
11640 2822a352 2019-10-15 stsp if (worktree)
11641 2822a352 2019-10-15 stsp got_worktree_close(worktree);
11642 0ae84acc 2022-06-15 tracey if (pack_fds) {
11643 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
11644 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
11645 0ae84acc 2022-06-15 tracey if (error == NULL)
11646 0ae84acc 2022-06-15 tracey error = pack_err;
11647 0ae84acc 2022-06-15 tracey }
11648 2822a352 2019-10-15 stsp free(cwd);
11649 2822a352 2019-10-15 stsp free(base_commit_id);
11650 2822a352 2019-10-15 stsp free(commit_id);
11651 2822a352 2019-10-15 stsp free(refname);
11652 2822a352 2019-10-15 stsp free(base_refname);
11653 f259c4c1 2021-09-24 stsp return error;
11654 f259c4c1 2021-09-24 stsp }
11655 f259c4c1 2021-09-24 stsp
11656 f259c4c1 2021-09-24 stsp __dead static void
11657 f259c4c1 2021-09-24 stsp usage_merge(void)
11658 f259c4c1 2021-09-24 stsp {
11659 088449d3 2021-09-26 stsp fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
11660 f259c4c1 2021-09-24 stsp getprogname());
11661 f259c4c1 2021-09-24 stsp exit(1);
11662 f259c4c1 2021-09-24 stsp }
11663 f259c4c1 2021-09-24 stsp
11664 f259c4c1 2021-09-24 stsp static const struct got_error *
11665 f259c4c1 2021-09-24 stsp cmd_merge(int argc, char *argv[])
11666 f259c4c1 2021-09-24 stsp {
11667 f259c4c1 2021-09-24 stsp const struct got_error *error = NULL;
11668 f259c4c1 2021-09-24 stsp struct got_worktree *worktree = NULL;
11669 f259c4c1 2021-09-24 stsp struct got_repository *repo = NULL;
11670 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex = NULL;
11671 f259c4c1 2021-09-24 stsp char *cwd = NULL, *id_str = NULL, *author = NULL;
11672 f259c4c1 2021-09-24 stsp struct got_reference *branch = NULL, *wt_branch = NULL;
11673 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *yca_id = NULL;
11674 f259c4c1 2021-09-24 stsp struct got_object_id *wt_branch_tip = NULL;
11675 f259c4c1 2021-09-24 stsp int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
11676 088449d3 2021-09-26 stsp int interrupt_merge = 0;
11677 f259c4c1 2021-09-24 stsp struct got_update_progress_arg upa;
11678 f259c4c1 2021-09-24 stsp struct got_object_id *merge_commit_id = NULL;
11679 f259c4c1 2021-09-24 stsp char *branch_name = NULL;
11680 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
11681 f259c4c1 2021-09-24 stsp
11682 f259c4c1 2021-09-24 stsp memset(&upa, 0, sizeof(upa));
11683 f259c4c1 2021-09-24 stsp
11684 088449d3 2021-09-26 stsp while ((ch = getopt(argc, argv, "acn")) != -1) {
11685 f259c4c1 2021-09-24 stsp switch (ch) {
11686 f259c4c1 2021-09-24 stsp case 'a':
11687 f259c4c1 2021-09-24 stsp abort_merge = 1;
11688 f259c4c1 2021-09-24 stsp break;
11689 f259c4c1 2021-09-24 stsp case 'c':
11690 f259c4c1 2021-09-24 stsp continue_merge = 1;
11691 f259c4c1 2021-09-24 stsp break;
11692 088449d3 2021-09-26 stsp case 'n':
11693 088449d3 2021-09-26 stsp interrupt_merge = 1;
11694 088449d3 2021-09-26 stsp break;
11695 f259c4c1 2021-09-24 stsp default:
11696 f259c4c1 2021-09-24 stsp usage_rebase();
11697 f259c4c1 2021-09-24 stsp /* NOTREACHED */
11698 f259c4c1 2021-09-24 stsp }
11699 f259c4c1 2021-09-24 stsp }
11700 f259c4c1 2021-09-24 stsp
11701 f259c4c1 2021-09-24 stsp argc -= optind;
11702 f259c4c1 2021-09-24 stsp argv += optind;
11703 f259c4c1 2021-09-24 stsp
11704 f259c4c1 2021-09-24 stsp #ifndef PROFILE
11705 f259c4c1 2021-09-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11706 f259c4c1 2021-09-24 stsp "unveil", NULL) == -1)
11707 f259c4c1 2021-09-24 stsp err(1, "pledge");
11708 f259c4c1 2021-09-24 stsp #endif
11709 f259c4c1 2021-09-24 stsp
11710 f259c4c1 2021-09-24 stsp if (abort_merge && continue_merge)
11711 f259c4c1 2021-09-24 stsp option_conflict('a', 'c');
11712 f259c4c1 2021-09-24 stsp if (abort_merge || continue_merge) {
11713 f259c4c1 2021-09-24 stsp if (argc != 0)
11714 f259c4c1 2021-09-24 stsp usage_merge();
11715 f259c4c1 2021-09-24 stsp } else if (argc != 1)
11716 f259c4c1 2021-09-24 stsp usage_merge();
11717 f259c4c1 2021-09-24 stsp
11718 f259c4c1 2021-09-24 stsp cwd = getcwd(NULL, 0);
11719 f259c4c1 2021-09-24 stsp if (cwd == NULL) {
11720 f259c4c1 2021-09-24 stsp error = got_error_from_errno("getcwd");
11721 f259c4c1 2021-09-24 stsp goto done;
11722 f259c4c1 2021-09-24 stsp }
11723 f259c4c1 2021-09-24 stsp
11724 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
11725 0ae84acc 2022-06-15 tracey if (error != NULL)
11726 0ae84acc 2022-06-15 tracey goto done;
11727 0ae84acc 2022-06-15 tracey
11728 f259c4c1 2021-09-24 stsp error = got_worktree_open(&worktree, cwd);
11729 f259c4c1 2021-09-24 stsp if (error) {
11730 f259c4c1 2021-09-24 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
11731 f259c4c1 2021-09-24 stsp error = wrap_not_worktree_error(error,
11732 f259c4c1 2021-09-24 stsp "merge", cwd);
11733 f259c4c1 2021-09-24 stsp goto done;
11734 f259c4c1 2021-09-24 stsp }
11735 f259c4c1 2021-09-24 stsp
11736 f259c4c1 2021-09-24 stsp error = got_repo_open(&repo,
11737 0ae84acc 2022-06-15 tracey worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
11738 0ae84acc 2022-06-15 tracey pack_fds);
11739 f259c4c1 2021-09-24 stsp if (error != NULL)
11740 f259c4c1 2021-09-24 stsp goto done;
11741 f259c4c1 2021-09-24 stsp
11742 f259c4c1 2021-09-24 stsp error = apply_unveil(got_repo_get_path(repo), 0,
11743 f259c4c1 2021-09-24 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
11744 f259c4c1 2021-09-24 stsp if (error)
11745 f259c4c1 2021-09-24 stsp goto done;
11746 f259c4c1 2021-09-24 stsp
11747 f259c4c1 2021-09-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
11748 f259c4c1 2021-09-24 stsp if (error)
11749 f259c4c1 2021-09-24 stsp goto done;
11750 f259c4c1 2021-09-24 stsp
11751 f259c4c1 2021-09-24 stsp error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11752 f259c4c1 2021-09-24 stsp repo);
11753 f259c4c1 2021-09-24 stsp if (error)
11754 f259c4c1 2021-09-24 stsp goto done;
11755 f259c4c1 2021-09-24 stsp
11756 f259c4c1 2021-09-24 stsp if (abort_merge) {
11757 f259c4c1 2021-09-24 stsp if (!merge_in_progress) {
11758 f259c4c1 2021-09-24 stsp error = got_error(GOT_ERR_NOT_MERGING);
11759 f259c4c1 2021-09-24 stsp goto done;
11760 f259c4c1 2021-09-24 stsp }
11761 f259c4c1 2021-09-24 stsp error = got_worktree_merge_continue(&branch_name,
11762 f259c4c1 2021-09-24 stsp &branch_tip, &fileindex, worktree, repo);
11763 f259c4c1 2021-09-24 stsp if (error)
11764 f259c4c1 2021-09-24 stsp goto done;
11765 f259c4c1 2021-09-24 stsp error = got_worktree_merge_abort(worktree, fileindex, repo,
11766 41f061b2 2021-10-05 stsp abort_progress, &upa);
11767 f259c4c1 2021-09-24 stsp if (error)
11768 f259c4c1 2021-09-24 stsp goto done;
11769 f259c4c1 2021-09-24 stsp printf("Merge of %s aborted\n", branch_name);
11770 f259c4c1 2021-09-24 stsp goto done; /* nothing else to do */
11771 f259c4c1 2021-09-24 stsp }
11772 f259c4c1 2021-09-24 stsp
11773 f259c4c1 2021-09-24 stsp error = get_author(&author, repo, worktree);
11774 f259c4c1 2021-09-24 stsp if (error)
11775 f259c4c1 2021-09-24 stsp goto done;
11776 f259c4c1 2021-09-24 stsp
11777 f259c4c1 2021-09-24 stsp if (continue_merge) {
11778 f259c4c1 2021-09-24 stsp if (!merge_in_progress) {
11779 f259c4c1 2021-09-24 stsp error = got_error(GOT_ERR_NOT_MERGING);
11780 f259c4c1 2021-09-24 stsp goto done;
11781 f259c4c1 2021-09-24 stsp }
11782 f259c4c1 2021-09-24 stsp error = got_worktree_merge_continue(&branch_name,
11783 f259c4c1 2021-09-24 stsp &branch_tip, &fileindex, worktree, repo);
11784 f259c4c1 2021-09-24 stsp if (error)
11785 f259c4c1 2021-09-24 stsp goto done;
11786 f259c4c1 2021-09-24 stsp } else {
11787 f259c4c1 2021-09-24 stsp error = got_ref_open(&branch, repo, argv[0], 0);
11788 f259c4c1 2021-09-24 stsp if (error != NULL)
11789 f259c4c1 2021-09-24 stsp goto done;
11790 f259c4c1 2021-09-24 stsp branch_name = strdup(got_ref_get_name(branch));
11791 f259c4c1 2021-09-24 stsp if (branch_name == NULL) {
11792 f259c4c1 2021-09-24 stsp error = got_error_from_errno("strdup");
11793 f259c4c1 2021-09-24 stsp goto done;
11794 f259c4c1 2021-09-24 stsp }
11795 f259c4c1 2021-09-24 stsp error = got_ref_resolve(&branch_tip, repo, branch);
11796 f259c4c1 2021-09-24 stsp if (error)
11797 f259c4c1 2021-09-24 stsp goto done;
11798 f259c4c1 2021-09-24 stsp }
11799 f259c4c1 2021-09-24 stsp
11800 f259c4c1 2021-09-24 stsp error = got_ref_open(&wt_branch, repo,
11801 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(worktree), 0);
11802 f259c4c1 2021-09-24 stsp if (error)
11803 f259c4c1 2021-09-24 stsp goto done;
11804 f259c4c1 2021-09-24 stsp error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
11805 f259c4c1 2021-09-24 stsp if (error)
11806 f259c4c1 2021-09-24 stsp goto done;
11807 f259c4c1 2021-09-24 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11808 4e91ef15 2021-09-26 stsp wt_branch_tip, branch_tip, 0, repo,
11809 f259c4c1 2021-09-24 stsp check_cancelled, NULL);
11810 4e91ef15 2021-09-26 stsp if (error && error->code != GOT_ERR_ANCESTRY)
11811 f259c4c1 2021-09-24 stsp goto done;
11812 f259c4c1 2021-09-24 stsp
11813 f259c4c1 2021-09-24 stsp if (!continue_merge) {
11814 f259c4c1 2021-09-24 stsp error = check_path_prefix(wt_branch_tip, branch_tip,
11815 f259c4c1 2021-09-24 stsp got_worktree_get_path_prefix(worktree),
11816 f259c4c1 2021-09-24 stsp GOT_ERR_MERGE_PATH, repo);
11817 f259c4c1 2021-09-24 stsp if (error)
11818 f259c4c1 2021-09-24 stsp goto done;
11819 4e91ef15 2021-09-26 stsp if (yca_id) {
11820 4e91ef15 2021-09-26 stsp error = check_same_branch(wt_branch_tip, branch,
11821 4e91ef15 2021-09-26 stsp yca_id, repo);
11822 4e91ef15 2021-09-26 stsp if (error) {
11823 4e91ef15 2021-09-26 stsp if (error->code != GOT_ERR_ANCESTRY)
11824 4e91ef15 2021-09-26 stsp goto done;
11825 4e91ef15 2021-09-26 stsp error = NULL;
11826 4e91ef15 2021-09-26 stsp } else {
11827 4e91ef15 2021-09-26 stsp static char msg[512];
11828 4e91ef15 2021-09-26 stsp snprintf(msg, sizeof(msg),
11829 4e91ef15 2021-09-26 stsp "cannot create a merge commit because "
11830 4e91ef15 2021-09-26 stsp "%s is based on %s; %s can be integrated "
11831 4e91ef15 2021-09-26 stsp "with 'got integrate' instead", branch_name,
11832 4e91ef15 2021-09-26 stsp got_worktree_get_head_ref_name(worktree),
11833 4e91ef15 2021-09-26 stsp branch_name);
11834 4e91ef15 2021-09-26 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
11835 f259c4c1 2021-09-24 stsp goto done;
11836 4e91ef15 2021-09-26 stsp }
11837 f259c4c1 2021-09-24 stsp }
11838 f259c4c1 2021-09-24 stsp error = got_worktree_merge_prepare(&fileindex, worktree,
11839 f259c4c1 2021-09-24 stsp branch, repo);
11840 f259c4c1 2021-09-24 stsp if (error)
11841 f259c4c1 2021-09-24 stsp goto done;
11842 f259c4c1 2021-09-24 stsp
11843 f259c4c1 2021-09-24 stsp error = got_worktree_merge_branch(worktree, fileindex,
11844 f259c4c1 2021-09-24 stsp yca_id, branch_tip, repo, update_progress, &upa,
11845 f259c4c1 2021-09-24 stsp check_cancelled, NULL);
11846 f259c4c1 2021-09-24 stsp if (error)
11847 f259c4c1 2021-09-24 stsp goto done;
11848 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
11849 4e91ef15 2021-09-26 stsp if (!upa.did_something) {
11850 4e91ef15 2021-09-26 stsp error = got_worktree_merge_abort(worktree, fileindex,
11851 41f061b2 2021-10-05 stsp repo, abort_progress, &upa);
11852 4e91ef15 2021-09-26 stsp if (error)
11853 4e91ef15 2021-09-26 stsp goto done;
11854 4e91ef15 2021-09-26 stsp printf("Already up-to-date\n");
11855 4e91ef15 2021-09-26 stsp goto done;
11856 4e91ef15 2021-09-26 stsp }
11857 f259c4c1 2021-09-24 stsp }
11858 f259c4c1 2021-09-24 stsp
11859 088449d3 2021-09-26 stsp if (interrupt_merge) {
11860 f259c4c1 2021-09-24 stsp error = got_worktree_merge_postpone(worktree, fileindex);
11861 f259c4c1 2021-09-24 stsp if (error)
11862 f259c4c1 2021-09-24 stsp goto done;
11863 088449d3 2021-09-26 stsp printf("Merge of %s interrupted on request\n", branch_name);
11864 c1b05723 2021-09-28 stsp } else if (upa.conflicts > 0 || upa.missing > 0 ||
11865 c1b05723 2021-09-28 stsp upa.not_deleted > 0 || upa.unversioned > 0) {
11866 088449d3 2021-09-26 stsp error = got_worktree_merge_postpone(worktree, fileindex);
11867 088449d3 2021-09-26 stsp if (error)
11868 088449d3 2021-09-26 stsp goto done;
11869 c1b05723 2021-09-28 stsp if (upa.conflicts > 0 && upa.missing == 0 &&
11870 c1b05723 2021-09-28 stsp upa.not_deleted == 0 && upa.unversioned == 0) {
11871 f259c4c1 2021-09-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
11872 f259c4c1 2021-09-24 stsp "conflicts must be resolved before merging "
11873 f259c4c1 2021-09-24 stsp "can continue");
11874 f259c4c1 2021-09-24 stsp } else if (upa.conflicts > 0) {
11875 f259c4c1 2021-09-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
11876 f259c4c1 2021-09-24 stsp "conflicts must be resolved before merging "
11877 c1b05723 2021-09-28 stsp "can continue; changes destined for some "
11878 1acd48bc 2021-09-24 stsp "files were not yet merged and "
11879 f259c4c1 2021-09-24 stsp "should be merged manually if required before the "
11880 f259c4c1 2021-09-24 stsp "merge operation is continued");
11881 f259c4c1 2021-09-24 stsp } else {
11882 f259c4c1 2021-09-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
11883 c1b05723 2021-09-28 stsp "changes destined for some "
11884 f259c4c1 2021-09-24 stsp "files were not yet merged and should be "
11885 f259c4c1 2021-09-24 stsp "merged manually if required before the "
11886 f259c4c1 2021-09-24 stsp "merge operation is continued");
11887 f259c4c1 2021-09-24 stsp }
11888 f259c4c1 2021-09-24 stsp goto done;
11889 f259c4c1 2021-09-24 stsp } else {
11890 f259c4c1 2021-09-24 stsp error = got_worktree_merge_commit(&merge_commit_id, worktree,
11891 0ff8d236 2021-09-28 stsp fileindex, author, NULL, 1, branch_tip, branch_name,
11892 0ff8d236 2021-09-28 stsp repo, continue_merge ? print_status : NULL, NULL);
11893 f259c4c1 2021-09-24 stsp if (error)
11894 f259c4c1 2021-09-24 stsp goto done;
11895 f259c4c1 2021-09-24 stsp error = got_worktree_merge_complete(worktree, fileindex, repo);
11896 f259c4c1 2021-09-24 stsp if (error)
11897 f259c4c1 2021-09-24 stsp goto done;
11898 f259c4c1 2021-09-24 stsp error = got_object_id_str(&id_str, merge_commit_id);
11899 f259c4c1 2021-09-24 stsp if (error)
11900 f259c4c1 2021-09-24 stsp goto done;
11901 f259c4c1 2021-09-24 stsp printf("Merged %s into %s: %s\n", branch_name,
11902 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(worktree),
11903 f259c4c1 2021-09-24 stsp id_str);
11904 f259c4c1 2021-09-24 stsp
11905 f259c4c1 2021-09-24 stsp }
11906 f259c4c1 2021-09-24 stsp done:
11907 f259c4c1 2021-09-24 stsp free(id_str);
11908 f259c4c1 2021-09-24 stsp free(merge_commit_id);
11909 f259c4c1 2021-09-24 stsp free(author);
11910 f259c4c1 2021-09-24 stsp free(branch_tip);
11911 f259c4c1 2021-09-24 stsp free(branch_name);
11912 f259c4c1 2021-09-24 stsp free(yca_id);
11913 f259c4c1 2021-09-24 stsp if (branch)
11914 f259c4c1 2021-09-24 stsp got_ref_close(branch);
11915 f259c4c1 2021-09-24 stsp if (wt_branch)
11916 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
11917 f259c4c1 2021-09-24 stsp if (worktree)
11918 f259c4c1 2021-09-24 stsp got_worktree_close(worktree);
11919 f259c4c1 2021-09-24 stsp if (repo) {
11920 f259c4c1 2021-09-24 stsp const struct got_error *close_err = got_repo_close(repo);
11921 f259c4c1 2021-09-24 stsp if (error == NULL)
11922 f259c4c1 2021-09-24 stsp error = close_err;
11923 f259c4c1 2021-09-24 stsp }
11924 0ae84acc 2022-06-15 tracey if (pack_fds) {
11925 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
11926 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
11927 0ae84acc 2022-06-15 tracey if (error == NULL)
11928 0ae84acc 2022-06-15 tracey error = pack_err;
11929 0ae84acc 2022-06-15 tracey }
11930 715dc77e 2019-08-03 stsp return error;
11931 715dc77e 2019-08-03 stsp }
11932 715dc77e 2019-08-03 stsp
11933 715dc77e 2019-08-03 stsp __dead static void
11934 715dc77e 2019-08-03 stsp usage_stage(void)
11935 715dc77e 2019-08-03 stsp {
11936 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
11937 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
11938 715dc77e 2019-08-03 stsp getprogname());
11939 715dc77e 2019-08-03 stsp exit(1);
11940 715dc77e 2019-08-03 stsp }
11941 715dc77e 2019-08-03 stsp
11942 715dc77e 2019-08-03 stsp static const struct got_error *
11943 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
11944 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
11945 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
11946 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
11947 408b4ebc 2019-08-03 stsp {
11948 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
11949 408b4ebc 2019-08-03 stsp char *id_str = NULL;
11950 408b4ebc 2019-08-03 stsp
11951 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
11952 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
11953 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
11954 408b4ebc 2019-08-03 stsp return NULL;
11955 408b4ebc 2019-08-03 stsp
11956 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
11957 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
11958 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
11959 408b4ebc 2019-08-03 stsp else
11960 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
11961 408b4ebc 2019-08-03 stsp if (err)
11962 408b4ebc 2019-08-03 stsp return err;
11963 408b4ebc 2019-08-03 stsp
11964 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
11965 408b4ebc 2019-08-03 stsp free(id_str);
11966 dc424a06 2019-08-07 stsp return NULL;
11967 dc424a06 2019-08-07 stsp }
11968 2e1f37b0 2019-08-08 stsp
11969 dc424a06 2019-08-07 stsp static const struct got_error *
11970 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
11971 715dc77e 2019-08-03 stsp {
11972 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
11973 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
11974 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
11975 715dc77e 2019-08-03 stsp char *cwd = NULL;
11976 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
11977 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
11978 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
11979 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
11980 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
11981 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
11982 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
11983 715dc77e 2019-08-03 stsp
11984 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
11985 715dc77e 2019-08-03 stsp
11986 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
11987 715dc77e 2019-08-03 stsp switch (ch) {
11988 408b4ebc 2019-08-03 stsp case 'l':
11989 408b4ebc 2019-08-03 stsp list_stage = 1;
11990 408b4ebc 2019-08-03 stsp break;
11991 dc424a06 2019-08-07 stsp case 'p':
11992 dc424a06 2019-08-07 stsp pflag = 1;
11993 dc424a06 2019-08-07 stsp break;
11994 dc424a06 2019-08-07 stsp case 'F':
11995 dc424a06 2019-08-07 stsp patch_script_path = optarg;
11996 dc424a06 2019-08-07 stsp break;
11997 35213c7c 2020-07-23 stsp case 'S':
11998 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
11999 35213c7c 2020-07-23 stsp break;
12000 715dc77e 2019-08-03 stsp default:
12001 715dc77e 2019-08-03 stsp usage_stage();
12002 715dc77e 2019-08-03 stsp /* NOTREACHED */
12003 715dc77e 2019-08-03 stsp }
12004 715dc77e 2019-08-03 stsp }
12005 715dc77e 2019-08-03 stsp
12006 715dc77e 2019-08-03 stsp argc -= optind;
12007 715dc77e 2019-08-03 stsp argv += optind;
12008 715dc77e 2019-08-03 stsp
12009 715dc77e 2019-08-03 stsp #ifndef PROFILE
12010 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12011 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
12012 715dc77e 2019-08-03 stsp err(1, "pledge");
12013 715dc77e 2019-08-03 stsp #endif
12014 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
12015 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
12016 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
12017 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
12018 715dc77e 2019-08-03 stsp
12019 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
12020 715dc77e 2019-08-03 stsp if (cwd == NULL) {
12021 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
12022 715dc77e 2019-08-03 stsp goto done;
12023 715dc77e 2019-08-03 stsp }
12024 715dc77e 2019-08-03 stsp
12025 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
12026 0ae84acc 2022-06-15 tracey if (error != NULL)
12027 0ae84acc 2022-06-15 tracey goto done;
12028 0ae84acc 2022-06-15 tracey
12029 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
12030 fa51e947 2020-03-27 stsp if (error) {
12031 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
12032 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
12033 715dc77e 2019-08-03 stsp goto done;
12034 fa51e947 2020-03-27 stsp }
12035 715dc77e 2019-08-03 stsp
12036 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12037 0ae84acc 2022-06-15 tracey NULL, pack_fds);
12038 715dc77e 2019-08-03 stsp if (error != NULL)
12039 715dc77e 2019-08-03 stsp goto done;
12040 715dc77e 2019-08-03 stsp
12041 dc424a06 2019-08-07 stsp if (patch_script_path) {
12042 00fe21f2 2021-12-31 stsp patch_script_file = fopen(patch_script_path, "re");
12043 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
12044 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
12045 dc424a06 2019-08-07 stsp patch_script_path);
12046 dc424a06 2019-08-07 stsp goto done;
12047 dc424a06 2019-08-07 stsp }
12048 dc424a06 2019-08-07 stsp }
12049 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
12050 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
12051 715dc77e 2019-08-03 stsp if (error)
12052 715dc77e 2019-08-03 stsp goto done;
12053 715dc77e 2019-08-03 stsp
12054 f259c4c1 2021-09-24 stsp error = check_merge_in_progress(worktree, repo);
12055 f259c4c1 2021-09-24 stsp if (error)
12056 f259c4c1 2021-09-24 stsp goto done;
12057 f259c4c1 2021-09-24 stsp
12058 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12059 6d022e97 2019-08-04 stsp if (error)
12060 6d022e97 2019-08-04 stsp goto done;
12061 715dc77e 2019-08-03 stsp
12062 6d022e97 2019-08-04 stsp if (list_stage)
12063 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
12064 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
12065 2e1f37b0 2019-08-08 stsp else {
12066 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
12067 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
12068 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
12069 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
12070 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
12071 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
12072 2e1f37b0 2019-08-08 stsp }
12073 ad493afc 2019-08-03 stsp done:
12074 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
12075 2e1f37b0 2019-08-08 stsp error == NULL)
12076 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
12077 1d0f4054 2021-06-17 stsp if (repo) {
12078 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
12079 1d0f4054 2021-06-17 stsp if (error == NULL)
12080 1d0f4054 2021-06-17 stsp error = close_err;
12081 1d0f4054 2021-06-17 stsp }
12082 ad493afc 2019-08-03 stsp if (worktree)
12083 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
12084 0ae84acc 2022-06-15 tracey if (pack_fds) {
12085 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
12086 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
12087 0ae84acc 2022-06-15 tracey if (error == NULL)
12088 0ae84acc 2022-06-15 tracey error = pack_err;
12089 0ae84acc 2022-06-15 tracey }
12090 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
12091 ad493afc 2019-08-03 stsp free((char *)pe->path);
12092 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
12093 ad493afc 2019-08-03 stsp free(cwd);
12094 ad493afc 2019-08-03 stsp return error;
12095 ad493afc 2019-08-03 stsp }
12096 ad493afc 2019-08-03 stsp
12097 ad493afc 2019-08-03 stsp __dead static void
12098 ad493afc 2019-08-03 stsp usage_unstage(void)
12099 ad493afc 2019-08-03 stsp {
12100 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
12101 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
12102 ad493afc 2019-08-03 stsp getprogname());
12103 ad493afc 2019-08-03 stsp exit(1);
12104 ad493afc 2019-08-03 stsp }
12105 ad493afc 2019-08-03 stsp
12106 ad493afc 2019-08-03 stsp
12107 ad493afc 2019-08-03 stsp static const struct got_error *
12108 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
12109 ad493afc 2019-08-03 stsp {
12110 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
12111 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
12112 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
12113 ad493afc 2019-08-03 stsp char *cwd = NULL;
12114 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
12115 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
12116 9627c110 2020-04-18 stsp int ch, pflag = 0;
12117 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
12118 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
12119 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
12120 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
12121 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
12122 ad493afc 2019-08-03 stsp
12123 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
12124 ad493afc 2019-08-03 stsp
12125 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
12126 ad493afc 2019-08-03 stsp switch (ch) {
12127 2e1f37b0 2019-08-08 stsp case 'p':
12128 2e1f37b0 2019-08-08 stsp pflag = 1;
12129 2e1f37b0 2019-08-08 stsp break;
12130 2e1f37b0 2019-08-08 stsp case 'F':
12131 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
12132 2e1f37b0 2019-08-08 stsp break;
12133 ad493afc 2019-08-03 stsp default:
12134 ad493afc 2019-08-03 stsp usage_unstage();
12135 ad493afc 2019-08-03 stsp /* NOTREACHED */
12136 ad493afc 2019-08-03 stsp }
12137 ad493afc 2019-08-03 stsp }
12138 ad493afc 2019-08-03 stsp
12139 ad493afc 2019-08-03 stsp argc -= optind;
12140 ad493afc 2019-08-03 stsp argv += optind;
12141 ad493afc 2019-08-03 stsp
12142 ad493afc 2019-08-03 stsp #ifndef PROFILE
12143 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12144 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
12145 ad493afc 2019-08-03 stsp err(1, "pledge");
12146 ad493afc 2019-08-03 stsp #endif
12147 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
12148 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
12149 2e1f37b0 2019-08-08 stsp
12150 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
12151 ad493afc 2019-08-03 stsp if (cwd == NULL) {
12152 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
12153 ad493afc 2019-08-03 stsp goto done;
12154 ad493afc 2019-08-03 stsp }
12155 ad493afc 2019-08-03 stsp
12156 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
12157 0ae84acc 2022-06-15 tracey if (error != NULL)
12158 0ae84acc 2022-06-15 tracey goto done;
12159 0ae84acc 2022-06-15 tracey
12160 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
12161 fa51e947 2020-03-27 stsp if (error) {
12162 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
12163 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
12164 ad493afc 2019-08-03 stsp goto done;
12165 fa51e947 2020-03-27 stsp }
12166 ad493afc 2019-08-03 stsp
12167 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12168 0ae84acc 2022-06-15 tracey NULL, pack_fds);
12169 ad493afc 2019-08-03 stsp if (error != NULL)
12170 ad493afc 2019-08-03 stsp goto done;
12171 ad493afc 2019-08-03 stsp
12172 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
12173 00fe21f2 2021-12-31 stsp patch_script_file = fopen(patch_script_path, "re");
12174 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
12175 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
12176 2e1f37b0 2019-08-08 stsp patch_script_path);
12177 2e1f37b0 2019-08-08 stsp goto done;
12178 2e1f37b0 2019-08-08 stsp }
12179 2e1f37b0 2019-08-08 stsp }
12180 2e1f37b0 2019-08-08 stsp
12181 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
12182 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
12183 ad493afc 2019-08-03 stsp if (error)
12184 ad493afc 2019-08-03 stsp goto done;
12185 ad493afc 2019-08-03 stsp
12186 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12187 ad493afc 2019-08-03 stsp if (error)
12188 ad493afc 2019-08-03 stsp goto done;
12189 ad493afc 2019-08-03 stsp
12190 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
12191 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
12192 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
12193 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
12194 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
12195 9627c110 2020-04-18 stsp if (!error)
12196 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
12197 715dc77e 2019-08-03 stsp done:
12198 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
12199 2e1f37b0 2019-08-08 stsp error == NULL)
12200 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
12201 1d0f4054 2021-06-17 stsp if (repo) {
12202 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
12203 1d0f4054 2021-06-17 stsp if (error == NULL)
12204 1d0f4054 2021-06-17 stsp error = close_err;
12205 1d0f4054 2021-06-17 stsp }
12206 715dc77e 2019-08-03 stsp if (worktree)
12207 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
12208 0ae84acc 2022-06-15 tracey if (pack_fds) {
12209 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
12210 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
12211 0ae84acc 2022-06-15 tracey if (error == NULL)
12212 0ae84acc 2022-06-15 tracey error = pack_err;
12213 0ae84acc 2022-06-15 tracey }
12214 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
12215 715dc77e 2019-08-03 stsp free((char *)pe->path);
12216 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
12217 715dc77e 2019-08-03 stsp free(cwd);
12218 01073a5d 2019-08-22 stsp return error;
12219 01073a5d 2019-08-22 stsp }
12220 01073a5d 2019-08-22 stsp
12221 01073a5d 2019-08-22 stsp __dead static void
12222 01073a5d 2019-08-22 stsp usage_cat(void)
12223 01073a5d 2019-08-22 stsp {
12224 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
12225 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
12226 01073a5d 2019-08-22 stsp exit(1);
12227 01073a5d 2019-08-22 stsp }
12228 01073a5d 2019-08-22 stsp
12229 01073a5d 2019-08-22 stsp static const struct got_error *
12230 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12231 01073a5d 2019-08-22 stsp {
12232 01073a5d 2019-08-22 stsp const struct got_error *err;
12233 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
12234 01073a5d 2019-08-22 stsp
12235 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
12236 01073a5d 2019-08-22 stsp if (err)
12237 01073a5d 2019-08-22 stsp return err;
12238 01073a5d 2019-08-22 stsp
12239 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
12240 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
12241 01073a5d 2019-08-22 stsp return err;
12242 01073a5d 2019-08-22 stsp }
12243 01073a5d 2019-08-22 stsp
12244 01073a5d 2019-08-22 stsp static const struct got_error *
12245 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12246 01073a5d 2019-08-22 stsp {
12247 01073a5d 2019-08-22 stsp const struct got_error *err;
12248 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
12249 56e0773d 2019-11-28 stsp int nentries, i;
12250 01073a5d 2019-08-22 stsp
12251 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
12252 01073a5d 2019-08-22 stsp if (err)
12253 01073a5d 2019-08-22 stsp return err;
12254 01073a5d 2019-08-22 stsp
12255 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
12256 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
12257 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
12258 01073a5d 2019-08-22 stsp char *id_str;
12259 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
12260 01073a5d 2019-08-22 stsp break;
12261 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
12262 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
12263 01073a5d 2019-08-22 stsp if (err)
12264 01073a5d 2019-08-22 stsp break;
12265 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
12266 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
12267 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
12268 01073a5d 2019-08-22 stsp free(id_str);
12269 01073a5d 2019-08-22 stsp }
12270 01073a5d 2019-08-22 stsp
12271 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
12272 01073a5d 2019-08-22 stsp return err;
12273 01073a5d 2019-08-22 stsp }
12274 01073a5d 2019-08-22 stsp
12275 bdc78ba6 2022-02-16 jrick static void
12276 bdc78ba6 2022-02-16 jrick format_gmtoff(char *buf, size_t sz, time_t gmtoff)
12277 bdc78ba6 2022-02-16 jrick {
12278 bdc78ba6 2022-02-16 jrick long long h, m;
12279 bdc78ba6 2022-02-16 jrick char sign = '+';
12280 e546352c 2022-02-16 jrick
12281 bdc78ba6 2022-02-16 jrick if (gmtoff < 0) {
12282 bdc78ba6 2022-02-16 jrick sign = '-';
12283 bdc78ba6 2022-02-16 jrick gmtoff = -gmtoff;
12284 bdc78ba6 2022-02-16 jrick }
12285 bdc78ba6 2022-02-16 jrick
12286 bdc78ba6 2022-02-16 jrick h = (long long)gmtoff / 3600;
12287 bdc78ba6 2022-02-16 jrick m = ((long long)gmtoff - h*3600) / 60;
12288 bdc78ba6 2022-02-16 jrick snprintf(buf, sz, "%c%02lld%02lld", sign, h, m);
12289 bdc78ba6 2022-02-16 jrick }
12290 bdc78ba6 2022-02-16 jrick
12291 01073a5d 2019-08-22 stsp static const struct got_error *
12292 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12293 01073a5d 2019-08-22 stsp {
12294 01073a5d 2019-08-22 stsp const struct got_error *err;
12295 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
12296 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
12297 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
12298 01073a5d 2019-08-22 stsp char *id_str = NULL;
12299 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
12300 bdc78ba6 2022-02-16 jrick char gmtoff[6];
12301 01073a5d 2019-08-22 stsp
12302 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
12303 01073a5d 2019-08-22 stsp if (err)
12304 01073a5d 2019-08-22 stsp return err;
12305 01073a5d 2019-08-22 stsp
12306 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
12307 01073a5d 2019-08-22 stsp if (err)
12308 01073a5d 2019-08-22 stsp goto done;
12309 01073a5d 2019-08-22 stsp
12310 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
12311 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
12312 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
12313 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
12314 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
12315 01073a5d 2019-08-22 stsp char *pid_str;
12316 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&pid_str, &pid->id);
12317 01073a5d 2019-08-22 stsp if (err)
12318 01073a5d 2019-08-22 stsp goto done;
12319 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
12320 01073a5d 2019-08-22 stsp free(pid_str);
12321 01073a5d 2019-08-22 stsp }
12322 bdc78ba6 2022-02-16 jrick format_gmtoff(gmtoff, sizeof(gmtoff),
12323 bdc78ba6 2022-02-16 jrick got_object_commit_get_author_gmtoff(commit));
12324 bdc78ba6 2022-02-16 jrick fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
12325 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
12326 bdc78ba6 2022-02-16 jrick (long long)got_object_commit_get_author_time(commit),
12327 bdc78ba6 2022-02-16 jrick gmtoff);
12328 01073a5d 2019-08-22 stsp
12329 bdc78ba6 2022-02-16 jrick format_gmtoff(gmtoff, sizeof(gmtoff),
12330 bdc78ba6 2022-02-16 jrick got_object_commit_get_committer_gmtoff(commit));
12331 bdc78ba6 2022-02-16 jrick fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
12332 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
12333 bdc78ba6 2022-02-16 jrick (long long)got_object_commit_get_committer_time(commit),
12334 bdc78ba6 2022-02-16 jrick gmtoff);
12335 01073a5d 2019-08-22 stsp
12336 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
12337 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
12338 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
12339 01073a5d 2019-08-22 stsp done:
12340 01073a5d 2019-08-22 stsp free(id_str);
12341 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
12342 01073a5d 2019-08-22 stsp return err;
12343 01073a5d 2019-08-22 stsp }
12344 01073a5d 2019-08-22 stsp
12345 01073a5d 2019-08-22 stsp static const struct got_error *
12346 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12347 01073a5d 2019-08-22 stsp {
12348 01073a5d 2019-08-22 stsp const struct got_error *err;
12349 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
12350 01073a5d 2019-08-22 stsp char *id_str = NULL;
12351 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
12352 bdc78ba6 2022-02-16 jrick char gmtoff[6];
12353 01073a5d 2019-08-22 stsp
12354 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
12355 01073a5d 2019-08-22 stsp if (err)
12356 01073a5d 2019-08-22 stsp return err;
12357 01073a5d 2019-08-22 stsp
12358 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
12359 01073a5d 2019-08-22 stsp if (err)
12360 01073a5d 2019-08-22 stsp goto done;
12361 01073a5d 2019-08-22 stsp
12362 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
12363 e15d5241 2019-08-22 stsp
12364 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
12365 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
12366 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12367 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
12368 01073a5d 2019-08-22 stsp break;
12369 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
12370 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12371 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
12372 01073a5d 2019-08-22 stsp break;
12373 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
12374 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12375 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
12376 01073a5d 2019-08-22 stsp break;
12377 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
12378 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12379 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
12380 01073a5d 2019-08-22 stsp break;
12381 01073a5d 2019-08-22 stsp default:
12382 01073a5d 2019-08-22 stsp break;
12383 01073a5d 2019-08-22 stsp }
12384 01073a5d 2019-08-22 stsp
12385 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
12386 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
12387 e15d5241 2019-08-22 stsp
12388 bdc78ba6 2022-02-16 jrick format_gmtoff(gmtoff, sizeof(gmtoff),
12389 bdc78ba6 2022-02-16 jrick got_object_tag_get_tagger_gmtoff(tag));
12390 bdc78ba6 2022-02-16 jrick fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
12391 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
12392 bdc78ba6 2022-02-16 jrick (long long)got_object_tag_get_tagger_time(tag),
12393 bdc78ba6 2022-02-16 jrick gmtoff);
12394 01073a5d 2019-08-22 stsp
12395 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
12396 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
12397 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
12398 01073a5d 2019-08-22 stsp done:
12399 01073a5d 2019-08-22 stsp free(id_str);
12400 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
12401 01073a5d 2019-08-22 stsp return err;
12402 01073a5d 2019-08-22 stsp }
12403 01073a5d 2019-08-22 stsp
12404 01073a5d 2019-08-22 stsp static const struct got_error *
12405 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
12406 01073a5d 2019-08-22 stsp {
12407 01073a5d 2019-08-22 stsp const struct got_error *error;
12408 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
12409 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
12410 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
12411 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
12412 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
12413 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
12414 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
12415 84de9106 2020-12-26 stsp struct got_reflist_head refs;
12416 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
12417 84de9106 2020-12-26 stsp
12418 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
12419 01073a5d 2019-08-22 stsp
12420 01073a5d 2019-08-22 stsp #ifndef PROFILE
12421 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12422 01073a5d 2019-08-22 stsp NULL) == -1)
12423 01073a5d 2019-08-22 stsp err(1, "pledge");
12424 01073a5d 2019-08-22 stsp #endif
12425 01073a5d 2019-08-22 stsp
12426 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
12427 01073a5d 2019-08-22 stsp switch (ch) {
12428 896e9b6f 2019-08-26 stsp case 'c':
12429 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
12430 896e9b6f 2019-08-26 stsp break;
12431 01073a5d 2019-08-22 stsp case 'r':
12432 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
12433 01073a5d 2019-08-22 stsp if (repo_path == NULL)
12434 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
12435 9ba1d308 2019-10-21 stsp optarg);
12436 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
12437 01073a5d 2019-08-22 stsp break;
12438 896e9b6f 2019-08-26 stsp case 'P':
12439 896e9b6f 2019-08-26 stsp force_path = 1;
12440 896e9b6f 2019-08-26 stsp break;
12441 01073a5d 2019-08-22 stsp default:
12442 01073a5d 2019-08-22 stsp usage_cat();
12443 01073a5d 2019-08-22 stsp /* NOTREACHED */
12444 01073a5d 2019-08-22 stsp }
12445 01073a5d 2019-08-22 stsp }
12446 01073a5d 2019-08-22 stsp
12447 01073a5d 2019-08-22 stsp argc -= optind;
12448 01073a5d 2019-08-22 stsp argv += optind;
12449 01073a5d 2019-08-22 stsp
12450 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
12451 01073a5d 2019-08-22 stsp if (cwd == NULL) {
12452 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
12453 01073a5d 2019-08-22 stsp goto done;
12454 01073a5d 2019-08-22 stsp }
12455 a091f71a 2022-04-09 stsp
12456 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
12457 0ae84acc 2022-06-15 tracey if (error != NULL)
12458 0ae84acc 2022-06-15 tracey goto done;
12459 0ae84acc 2022-06-15 tracey
12460 a091f71a 2022-04-09 stsp if (repo_path == NULL) {
12461 a091f71a 2022-04-09 stsp error = got_worktree_open(&worktree, cwd);
12462 a091f71a 2022-04-09 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
12463 a091f71a 2022-04-09 stsp goto done;
12464 a091f71a 2022-04-09 stsp if (worktree) {
12465 01073a5d 2019-08-22 stsp repo_path = strdup(
12466 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
12467 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
12468 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
12469 01073a5d 2019-08-22 stsp goto done;
12470 01073a5d 2019-08-22 stsp }
12471 f7fce2e2 2022-03-12 stsp
12472 a091f71a 2022-04-09 stsp /* Release work tree lock. */
12473 a091f71a 2022-04-09 stsp got_worktree_close(worktree);
12474 a091f71a 2022-04-09 stsp worktree = NULL;
12475 a091f71a 2022-04-09 stsp }
12476 01073a5d 2019-08-22 stsp }
12477 01073a5d 2019-08-22 stsp
12478 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
12479 a091f71a 2022-04-09 stsp repo_path = strdup(cwd);
12480 01073a5d 2019-08-22 stsp if (repo_path == NULL)
12481 a091f71a 2022-04-09 stsp return got_error_from_errno("strdup");
12482 01073a5d 2019-08-22 stsp }
12483 01073a5d 2019-08-22 stsp
12484 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
12485 01073a5d 2019-08-22 stsp free(repo_path);
12486 01073a5d 2019-08-22 stsp if (error != NULL)
12487 01073a5d 2019-08-22 stsp goto done;
12488 01073a5d 2019-08-22 stsp
12489 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
12490 896e9b6f 2019-08-26 stsp if (error)
12491 896e9b6f 2019-08-26 stsp goto done;
12492 896e9b6f 2019-08-26 stsp
12493 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
12494 84de9106 2020-12-26 stsp if (error)
12495 84de9106 2020-12-26 stsp goto done;
12496 84de9106 2020-12-26 stsp
12497 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
12498 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
12499 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
12500 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
12501 01073a5d 2019-08-22 stsp if (error)
12502 01073a5d 2019-08-22 stsp goto done;
12503 01073a5d 2019-08-22 stsp
12504 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
12505 a44927cc 2022-04-07 stsp if (error)
12506 a44927cc 2022-04-07 stsp goto done;
12507 a44927cc 2022-04-07 stsp
12508 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
12509 896e9b6f 2019-08-26 stsp if (force_path) {
12510 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit,
12511 896e9b6f 2019-08-26 stsp argv[i]);
12512 896e9b6f 2019-08-26 stsp if (error)
12513 896e9b6f 2019-08-26 stsp break;
12514 896e9b6f 2019-08-26 stsp } else {
12515 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
12516 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
12517 84de9106 2020-12-26 stsp repo);
12518 896e9b6f 2019-08-26 stsp if (error) {
12519 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
12520 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
12521 896e9b6f 2019-08-26 stsp break;
12522 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
12523 a44927cc 2022-04-07 stsp commit, argv[i]);
12524 896e9b6f 2019-08-26 stsp if (error)
12525 896e9b6f 2019-08-26 stsp break;
12526 896e9b6f 2019-08-26 stsp }
12527 896e9b6f 2019-08-26 stsp }
12528 01073a5d 2019-08-22 stsp
12529 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
12530 01073a5d 2019-08-22 stsp if (error)
12531 01073a5d 2019-08-22 stsp break;
12532 01073a5d 2019-08-22 stsp
12533 01073a5d 2019-08-22 stsp switch (obj_type) {
12534 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
12535 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
12536 01073a5d 2019-08-22 stsp break;
12537 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
12538 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
12539 01073a5d 2019-08-22 stsp break;
12540 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
12541 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
12542 01073a5d 2019-08-22 stsp break;
12543 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
12544 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
12545 01073a5d 2019-08-22 stsp break;
12546 01073a5d 2019-08-22 stsp default:
12547 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
12548 01073a5d 2019-08-22 stsp break;
12549 01073a5d 2019-08-22 stsp }
12550 01073a5d 2019-08-22 stsp if (error)
12551 01073a5d 2019-08-22 stsp break;
12552 01073a5d 2019-08-22 stsp free(label);
12553 01073a5d 2019-08-22 stsp label = NULL;
12554 01073a5d 2019-08-22 stsp free(id);
12555 01073a5d 2019-08-22 stsp id = NULL;
12556 01073a5d 2019-08-22 stsp }
12557 01073a5d 2019-08-22 stsp done:
12558 01073a5d 2019-08-22 stsp free(label);
12559 01073a5d 2019-08-22 stsp free(id);
12560 896e9b6f 2019-08-26 stsp free(commit_id);
12561 a44927cc 2022-04-07 stsp if (commit)
12562 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
12563 01073a5d 2019-08-22 stsp if (worktree)
12564 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
12565 01073a5d 2019-08-22 stsp if (repo) {
12566 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
12567 01073a5d 2019-08-22 stsp if (error == NULL)
12568 1d0f4054 2021-06-17 stsp error = close_err;
12569 b2118c49 2020-07-28 stsp }
12570 0ae84acc 2022-06-15 tracey if (pack_fds) {
12571 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
12572 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
12573 0ae84acc 2022-06-15 tracey if (error == NULL)
12574 0ae84acc 2022-06-15 tracey error = pack_err;
12575 0ae84acc 2022-06-15 tracey }
12576 0ae84acc 2022-06-15 tracey
12577 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
12578 b2118c49 2020-07-28 stsp return error;
12579 b2118c49 2020-07-28 stsp }
12580 b2118c49 2020-07-28 stsp
12581 b2118c49 2020-07-28 stsp __dead static void
12582 b2118c49 2020-07-28 stsp usage_info(void)
12583 b2118c49 2020-07-28 stsp {
12584 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
12585 b2118c49 2020-07-28 stsp getprogname());
12586 b2118c49 2020-07-28 stsp exit(1);
12587 b2118c49 2020-07-28 stsp }
12588 b2118c49 2020-07-28 stsp
12589 b2118c49 2020-07-28 stsp static const struct got_error *
12590 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
12591 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12592 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
12593 b2118c49 2020-07-28 stsp {
12594 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
12595 b2118c49 2020-07-28 stsp char *id_str = NULL;
12596 b2118c49 2020-07-28 stsp char datebuf[128];
12597 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
12598 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
12599 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
12600 b2118c49 2020-07-28 stsp
12601 b2118c49 2020-07-28 stsp /*
12602 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
12603 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
12604 b2118c49 2020-07-28 stsp */
12605 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
12606 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
12607 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
12608 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
12609 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
12610 b2118c49 2020-07-28 stsp }
12611 b2118c49 2020-07-28 stsp
12612 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
12613 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
12614 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
12615 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
12616 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
12617 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
12618 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
12619 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
12620 b2118c49 2020-07-28 stsp else
12621 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
12622 b2118c49 2020-07-28 stsp
12623 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
12624 b2118c49 2020-07-28 stsp if (tm == NULL)
12625 b2118c49 2020-07-28 stsp return NULL;
12626 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
12627 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
12628 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
12629 b2118c49 2020-07-28 stsp
12630 b2118c49 2020-07-28 stsp if (blob_id) {
12631 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
12632 b2118c49 2020-07-28 stsp if (err)
12633 b2118c49 2020-07-28 stsp return err;
12634 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
12635 b2118c49 2020-07-28 stsp free(id_str);
12636 b2118c49 2020-07-28 stsp }
12637 b2118c49 2020-07-28 stsp
12638 b2118c49 2020-07-28 stsp if (staged_blob_id) {
12639 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
12640 b2118c49 2020-07-28 stsp if (err)
12641 b2118c49 2020-07-28 stsp return err;
12642 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
12643 b2118c49 2020-07-28 stsp free(id_str);
12644 b2118c49 2020-07-28 stsp }
12645 b2118c49 2020-07-28 stsp
12646 b2118c49 2020-07-28 stsp if (commit_id) {
12647 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
12648 b2118c49 2020-07-28 stsp if (err)
12649 b2118c49 2020-07-28 stsp return err;
12650 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
12651 b2118c49 2020-07-28 stsp free(id_str);
12652 b2118c49 2020-07-28 stsp }
12653 b2118c49 2020-07-28 stsp
12654 b2118c49 2020-07-28 stsp return NULL;
12655 b2118c49 2020-07-28 stsp }
12656 b2118c49 2020-07-28 stsp
12657 b2118c49 2020-07-28 stsp static const struct got_error *
12658 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
12659 b2118c49 2020-07-28 stsp {
12660 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
12661 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
12662 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
12663 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
12664 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
12665 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
12666 b2118c49 2020-07-28 stsp int ch, show_files = 0;
12667 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
12668 b2118c49 2020-07-28 stsp
12669 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
12670 b2118c49 2020-07-28 stsp
12671 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
12672 b2118c49 2020-07-28 stsp switch (ch) {
12673 b2118c49 2020-07-28 stsp default:
12674 b2118c49 2020-07-28 stsp usage_info();
12675 b2118c49 2020-07-28 stsp /* NOTREACHED */
12676 b2118c49 2020-07-28 stsp }
12677 01073a5d 2019-08-22 stsp }
12678 b2118c49 2020-07-28 stsp
12679 b2118c49 2020-07-28 stsp argc -= optind;
12680 b2118c49 2020-07-28 stsp argv += optind;
12681 b2118c49 2020-07-28 stsp
12682 b2118c49 2020-07-28 stsp #ifndef PROFILE
12683 57160834 2022-05-31 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12684 b2118c49 2020-07-28 stsp NULL) == -1)
12685 b2118c49 2020-07-28 stsp err(1, "pledge");
12686 b2118c49 2020-07-28 stsp #endif
12687 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
12688 b2118c49 2020-07-28 stsp if (cwd == NULL) {
12689 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
12690 b2118c49 2020-07-28 stsp goto done;
12691 b2118c49 2020-07-28 stsp }
12692 b2118c49 2020-07-28 stsp
12693 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
12694 0ae84acc 2022-06-15 tracey if (error != NULL)
12695 0ae84acc 2022-06-15 tracey goto done;
12696 0ae84acc 2022-06-15 tracey
12697 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
12698 b2118c49 2020-07-28 stsp if (error) {
12699 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
12700 8ea5c997 2021-02-07 naddy error = wrap_not_worktree_error(error, "info", cwd);
12701 b2118c49 2020-07-28 stsp goto done;
12702 b2118c49 2020-07-28 stsp }
12703 b2118c49 2020-07-28 stsp
12704 57160834 2022-05-31 stsp #ifndef PROFILE
12705 57160834 2022-05-31 stsp /* Remove "cpath" promise. */
12706 57160834 2022-05-31 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
12707 57160834 2022-05-31 stsp NULL) == -1)
12708 57160834 2022-05-31 stsp err(1, "pledge");
12709 57160834 2022-05-31 stsp #endif
12710 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
12711 b2118c49 2020-07-28 stsp if (error)
12712 b2118c49 2020-07-28 stsp goto done;
12713 b2118c49 2020-07-28 stsp
12714 b2118c49 2020-07-28 stsp if (argc >= 1) {
12715 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
12716 b2118c49 2020-07-28 stsp worktree);
12717 b2118c49 2020-07-28 stsp if (error)
12718 b2118c49 2020-07-28 stsp goto done;
12719 b2118c49 2020-07-28 stsp show_files = 1;
12720 b2118c49 2020-07-28 stsp }
12721 b2118c49 2020-07-28 stsp
12722 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
12723 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
12724 b2118c49 2020-07-28 stsp if (error)
12725 b2118c49 2020-07-28 stsp goto done;
12726 b2118c49 2020-07-28 stsp
12727 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
12728 b2118c49 2020-07-28 stsp if (error)
12729 b2118c49 2020-07-28 stsp goto done;
12730 b2118c49 2020-07-28 stsp
12731 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
12732 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
12733 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
12734 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
12735 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
12736 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
12737 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
12738 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
12739 b2118c49 2020-07-28 stsp
12740 b2118c49 2020-07-28 stsp if (show_files) {
12741 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
12742 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
12743 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
12744 b2118c49 2020-07-28 stsp continue;
12745 b2118c49 2020-07-28 stsp /*
12746 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
12747 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
12748 b2118c49 2020-07-28 stsp */
12749 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
12750 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
12751 b2118c49 2020-07-28 stsp }
12752 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
12753 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
12754 b2118c49 2020-07-28 stsp if (error)
12755 b2118c49 2020-07-28 stsp goto done;
12756 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
12757 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
12758 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
12759 b2118c49 2020-07-28 stsp break;
12760 b2118c49 2020-07-28 stsp }
12761 b2118c49 2020-07-28 stsp }
12762 b2118c49 2020-07-28 stsp }
12763 b2118c49 2020-07-28 stsp done:
12764 0ae84acc 2022-06-15 tracey if (pack_fds) {
12765 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
12766 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
12767 0ae84acc 2022-06-15 tracey if (error == NULL)
12768 0ae84acc 2022-06-15 tracey error = pack_err;
12769 0ae84acc 2022-06-15 tracey }
12770 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
12771 b2118c49 2020-07-28 stsp free((char *)pe->path);
12772 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
12773 b2118c49 2020-07-28 stsp free(cwd);
12774 b2118c49 2020-07-28 stsp free(id_str);
12775 b2118c49 2020-07-28 stsp free(uuidstr);
12776 0ebf8283 2019-07-24 stsp return error;
12777 0ebf8283 2019-07-24 stsp }