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 99437157 2018-11-11 stsp #include <signal.h>
31 5c860e29 2018-03-12 stsp #include <stdio.h>
32 5c860e29 2018-03-12 stsp #include <stdlib.h>
33 5c860e29 2018-03-12 stsp #include <string.h>
34 5c860e29 2018-03-12 stsp #include <unistd.h>
35 c09a553d 2018-03-12 stsp #include <libgen.h>
36 c0768b0f 2018-06-10 stsp #include <time.h>
37 33ad4cbe 2019-05-12 jcs #include <paths.h>
38 6841bf13 2019-11-29 kn #include <regex.h>
39 83cd27f8 2020-01-13 stsp #include <getopt.h>
40 d2cdc636 2020-03-18 stsp #include <util.h>
41 5c860e29 2018-03-12 stsp
42 53ccebc2 2019-07-30 stsp #include "got_version.h"
43 f42b1b34 2018-03-12 stsp #include "got_error.h"
44 f42b1b34 2018-03-12 stsp #include "got_object.h"
45 5261c201 2018-04-01 stsp #include "got_reference.h"
46 f42b1b34 2018-03-12 stsp #include "got_repository.h"
47 1dd54920 2019-05-11 stsp #include "got_path.h"
48 e6209546 2019-08-22 stsp #include "got_cancel.h"
49 c09a553d 2018-03-12 stsp #include "got_worktree.h"
50 79109fed 2018-03-27 stsp #include "got_diff.h"
51 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
52 6f23baec 2020-03-18 stsp #include "got_fetch.h"
53 f8a36e22 2021-08-26 stsp #include "got_send.h"
54 404c43c4 2018-06-21 stsp #include "got_blame.h"
55 63219cd2 2019-01-04 stsp #include "got_privsep.h"
56 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
57 50b0790e 2020-09-11 stsp #include "got_gotconfig.h"
58 d65a88a2 2021-09-05 stsp #include "got_dial.h"
59 5c860e29 2018-03-12 stsp
60 5c860e29 2018-03-12 stsp #ifndef nitems
61 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 5c860e29 2018-03-12 stsp #endif
63 99437157 2018-11-11 stsp
64 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
65 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
66 99437157 2018-11-11 stsp
67 99437157 2018-11-11 stsp static void
68 99437157 2018-11-11 stsp catch_sigint(int signo)
69 99437157 2018-11-11 stsp {
70 99437157 2018-11-11 stsp sigint_received = 1;
71 99437157 2018-11-11 stsp }
72 99437157 2018-11-11 stsp
73 99437157 2018-11-11 stsp static void
74 99437157 2018-11-11 stsp catch_sigpipe(int signo)
75 99437157 2018-11-11 stsp {
76 99437157 2018-11-11 stsp sigpipe_received = 1;
77 99437157 2018-11-11 stsp }
78 5c860e29 2018-03-12 stsp
79 99437157 2018-11-11 stsp
80 8cfb4057 2019-07-09 stsp struct got_cmd {
81 820059fa 2020-09-25 stsp const char *cmd_name;
82 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
83 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
84 97b3a7be 2019-07-09 stsp const char *cmd_alias;
85 5c860e29 2018-03-12 stsp };
86 5c860e29 2018-03-12 stsp
87 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
88 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
89 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
90 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
91 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
92 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
93 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
94 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
95 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
96 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
97 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
98 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
99 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
100 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
101 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
102 d00136be 2019-03-26 stsp __dead static void usage_add(void);
103 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
104 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
105 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
106 f8a36e22 2021-08-26 stsp __dead static void usage_send(void);
107 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
108 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
109 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
110 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
111 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
112 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
113 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
114 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
115 b2118c49 2020-07-28 stsp __dead static void usage_info(void);
116 5c860e29 2018-03-12 stsp
117 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
118 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
119 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
120 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
121 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
122 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
123 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
124 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
125 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
126 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
127 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
128 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
129 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
130 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
131 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
132 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
133 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
134 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
135 f8a36e22 2021-08-26 stsp static const struct got_error* cmd_send(int, char *[]);
136 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
137 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
138 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
139 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
140 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
141 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
142 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
143 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
144 b2118c49 2020-07-28 stsp static const struct got_error* cmd_info(int, char *[]);
145 5c860e29 2018-03-12 stsp
146 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
147 b2118c49 2020-07-28 stsp { "init", cmd_init, usage_init, "" },
148 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
149 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
150 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
151 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
152 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
153 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
154 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
155 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
156 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
157 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
158 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
159 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
160 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
161 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
162 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
163 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
164 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
165 f8a36e22 2021-08-26 stsp { "send", cmd_send, usage_send, "se" },
166 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
167 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
168 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
169 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
170 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
171 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
172 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
173 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
174 b2118c49 2020-07-28 stsp { "info", cmd_info, usage_info, "" },
175 5c860e29 2018-03-12 stsp };
176 ce5b7c56 2019-07-09 stsp
177 ce5b7c56 2019-07-09 stsp static void
178 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
179 ce5b7c56 2019-07-09 stsp {
180 6059809a 2020-12-17 stsp size_t i;
181 ce5b7c56 2019-07-09 stsp
182 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
183 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
184 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
185 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->cmd_name);
186 ce5b7c56 2019-07-09 stsp }
187 6879ba42 2020-10-01 naddy fputc('\n', fp);
188 ce5b7c56 2019-07-09 stsp }
189 5c860e29 2018-03-12 stsp
190 ff69268e 2020-12-13 stsp __dead static void
191 ff69268e 2020-12-13 stsp option_conflict(char a, char b)
192 ff69268e 2020-12-13 stsp {
193 ff69268e 2020-12-13 stsp errx(1, "-%c and -%c options are mutually exclusive", a, b);
194 ff69268e 2020-12-13 stsp }
195 ff69268e 2020-12-13 stsp
196 5c860e29 2018-03-12 stsp int
197 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
198 5c860e29 2018-03-12 stsp {
199 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
200 6059809a 2020-12-17 stsp size_t i;
201 5c860e29 2018-03-12 stsp int ch;
202 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
203 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
204 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
205 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0 }
206 83cd27f8 2020-01-13 stsp };
207 5c860e29 2018-03-12 stsp
208 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
209 5c860e29 2018-03-12 stsp
210 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
211 5c860e29 2018-03-12 stsp switch (ch) {
212 1b6b95a8 2018-03-12 stsp case 'h':
213 1b6b95a8 2018-03-12 stsp hflag = 1;
214 53ccebc2 2019-07-30 stsp break;
215 53ccebc2 2019-07-30 stsp case 'V':
216 53ccebc2 2019-07-30 stsp Vflag = 1;
217 1b6b95a8 2018-03-12 stsp break;
218 5c860e29 2018-03-12 stsp default:
219 6879ba42 2020-10-01 naddy usage(hflag, 1);
220 5c860e29 2018-03-12 stsp /* NOTREACHED */
221 5c860e29 2018-03-12 stsp }
222 5c860e29 2018-03-12 stsp }
223 5c860e29 2018-03-12 stsp
224 5c860e29 2018-03-12 stsp argc -= optind;
225 5c860e29 2018-03-12 stsp argv += optind;
226 9814e6a3 2020-09-27 naddy optind = 1;
227 9814e6a3 2020-09-27 naddy optreset = 1;
228 53ccebc2 2019-07-30 stsp
229 53ccebc2 2019-07-30 stsp if (Vflag) {
230 53ccebc2 2019-07-30 stsp got_version_print_str();
231 6879ba42 2020-10-01 naddy return 0;
232 53ccebc2 2019-07-30 stsp }
233 5c860e29 2018-03-12 stsp
234 5c860e29 2018-03-12 stsp if (argc <= 0)
235 6879ba42 2020-10-01 naddy usage(hflag, hflag ? 0 : 1);
236 5c860e29 2018-03-12 stsp
237 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
238 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
239 99437157 2018-11-11 stsp
240 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
241 d7d4f210 2018-03-12 stsp const struct got_error *error;
242 d7d4f210 2018-03-12 stsp
243 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
244 5c860e29 2018-03-12 stsp
245 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
246 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
247 5c860e29 2018-03-12 stsp continue;
248 5c860e29 2018-03-12 stsp
249 1b6b95a8 2018-03-12 stsp if (hflag)
250 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
251 1b6b95a8 2018-03-12 stsp
252 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
253 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
254 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
255 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
256 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
257 70015d7a 2019-11-08 stsp !(sigint_received &&
258 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
259 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
260 d7d4f210 2018-03-12 stsp return 1;
261 d7d4f210 2018-03-12 stsp }
262 d7d4f210 2018-03-12 stsp
263 d7d4f210 2018-03-12 stsp return 0;
264 5c860e29 2018-03-12 stsp }
265 5c860e29 2018-03-12 stsp
266 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
267 6879ba42 2020-10-01 naddy list_commands(stderr);
268 5c860e29 2018-03-12 stsp return 1;
269 5c860e29 2018-03-12 stsp }
270 5c860e29 2018-03-12 stsp
271 4ed7e80c 2018-05-20 stsp __dead static void
272 6879ba42 2020-10-01 naddy usage(int hflag, int status)
273 5c860e29 2018-03-12 stsp {
274 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
275 6879ba42 2020-10-01 naddy
276 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
277 53ccebc2 2019-07-30 stsp getprogname());
278 ce5b7c56 2019-07-09 stsp if (hflag)
279 6879ba42 2020-10-01 naddy list_commands(fp);
280 6879ba42 2020-10-01 naddy exit(status);
281 5c860e29 2018-03-12 stsp }
282 5c860e29 2018-03-12 stsp
283 0266afb7 2019-01-04 stsp static const struct got_error *
284 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
285 e2ba3d07 2019-05-13 stsp {
286 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
287 e2ba3d07 2019-05-13 stsp const char *editor;
288 8920fa04 2019-08-18 stsp
289 8920fa04 2019-08-18 stsp *abspath = NULL;
290 e2ba3d07 2019-05-13 stsp
291 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
292 e2ba3d07 2019-05-13 stsp if (editor == NULL)
293 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
294 e2ba3d07 2019-05-13 stsp
295 0ee7065d 2019-05-13 stsp if (editor) {
296 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
297 0ee7065d 2019-05-13 stsp if (err)
298 0ee7065d 2019-05-13 stsp return err;
299 0ee7065d 2019-05-13 stsp }
300 e2ba3d07 2019-05-13 stsp
301 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
302 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
303 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
304 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
305 0ee7065d 2019-05-13 stsp }
306 0ee7065d 2019-05-13 stsp
307 e2ba3d07 2019-05-13 stsp return NULL;
308 e2ba3d07 2019-05-13 stsp }
309 e2ba3d07 2019-05-13 stsp
310 e2ba3d07 2019-05-13 stsp static const struct got_error *
311 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
312 c530dc23 2019-07-23 stsp const char *worktree_path)
313 0266afb7 2019-01-04 stsp {
314 163ce85a 2019-05-13 stsp const struct got_error *err;
315 0266afb7 2019-01-04 stsp
316 37c06ea4 2019-07-15 stsp #ifdef PROFILE
317 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
318 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
319 37c06ea4 2019-07-15 stsp #endif
320 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
321 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
322 0266afb7 2019-01-04 stsp
323 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
324 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
325 0266afb7 2019-01-04 stsp
326 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
327 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
328 0266afb7 2019-01-04 stsp
329 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
330 163ce85a 2019-05-13 stsp if (err != NULL)
331 163ce85a 2019-05-13 stsp return err;
332 0266afb7 2019-01-04 stsp
333 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
334 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
335 0266afb7 2019-01-04 stsp
336 0266afb7 2019-01-04 stsp return NULL;
337 2c7829a4 2019-06-17 stsp }
338 2c7829a4 2019-06-17 stsp
339 2c7829a4 2019-06-17 stsp __dead static void
340 2c7829a4 2019-06-17 stsp usage_init(void)
341 2c7829a4 2019-06-17 stsp {
342 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
343 2c7829a4 2019-06-17 stsp exit(1);
344 2c7829a4 2019-06-17 stsp }
345 2c7829a4 2019-06-17 stsp
346 2c7829a4 2019-06-17 stsp static const struct got_error *
347 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
348 2c7829a4 2019-06-17 stsp {
349 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
350 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
351 2c7829a4 2019-06-17 stsp int ch;
352 2c7829a4 2019-06-17 stsp
353 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
354 2c7829a4 2019-06-17 stsp switch (ch) {
355 2c7829a4 2019-06-17 stsp default:
356 2c7829a4 2019-06-17 stsp usage_init();
357 2c7829a4 2019-06-17 stsp /* NOTREACHED */
358 2c7829a4 2019-06-17 stsp }
359 2c7829a4 2019-06-17 stsp }
360 2c7829a4 2019-06-17 stsp
361 2c7829a4 2019-06-17 stsp argc -= optind;
362 2c7829a4 2019-06-17 stsp argv += optind;
363 2c7829a4 2019-06-17 stsp
364 2c7829a4 2019-06-17 stsp #ifndef PROFILE
365 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
366 2c7829a4 2019-06-17 stsp err(1, "pledge");
367 2c7829a4 2019-06-17 stsp #endif
368 2c7829a4 2019-06-17 stsp if (argc != 1)
369 bc20e173 2019-06-17 stsp usage_init();
370 2c7829a4 2019-06-17 stsp
371 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
372 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
373 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
374 2c7829a4 2019-06-17 stsp
375 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
376 2c7829a4 2019-06-17 stsp
377 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
378 2c7829a4 2019-06-17 stsp if (error &&
379 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
380 2c7829a4 2019-06-17 stsp goto done;
381 2c7829a4 2019-06-17 stsp
382 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
383 2c7829a4 2019-06-17 stsp if (error)
384 2c7829a4 2019-06-17 stsp goto done;
385 2c7829a4 2019-06-17 stsp
386 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
387 3ce1b845 2019-07-15 stsp done:
388 3ce1b845 2019-07-15 stsp free(repo_path);
389 3ce1b845 2019-07-15 stsp return error;
390 3ce1b845 2019-07-15 stsp }
391 3ce1b845 2019-07-15 stsp
392 3ce1b845 2019-07-15 stsp __dead static void
393 3ce1b845 2019-07-15 stsp usage_import(void)
394 3ce1b845 2019-07-15 stsp {
395 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
396 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
397 3ce1b845 2019-07-15 stsp exit(1);
398 3ce1b845 2019-07-15 stsp }
399 3ce1b845 2019-07-15 stsp
400 3ce1b845 2019-07-15 stsp int
401 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
402 3ce1b845 2019-07-15 stsp {
403 3ce1b845 2019-07-15 stsp pid_t pid;
404 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
405 3ce1b845 2019-07-15 stsp int st = -1;
406 3ce1b845 2019-07-15 stsp
407 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
408 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
409 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
410 3ce1b845 2019-07-15 stsp
411 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
412 3ce1b845 2019-07-15 stsp case -1:
413 3ce1b845 2019-07-15 stsp goto doneediting;
414 3ce1b845 2019-07-15 stsp case 0:
415 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
416 3ce1b845 2019-07-15 stsp _exit(127);
417 3ce1b845 2019-07-15 stsp }
418 3ce1b845 2019-07-15 stsp
419 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
420 3ce1b845 2019-07-15 stsp if (errno != EINTR)
421 3ce1b845 2019-07-15 stsp break;
422 3ce1b845 2019-07-15 stsp
423 3ce1b845 2019-07-15 stsp doneediting:
424 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
425 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
426 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
427 3ce1b845 2019-07-15 stsp
428 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
429 3ce1b845 2019-07-15 stsp errno = EINTR;
430 3ce1b845 2019-07-15 stsp return -1;
431 3ce1b845 2019-07-15 stsp }
432 3ce1b845 2019-07-15 stsp
433 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
434 3ce1b845 2019-07-15 stsp }
435 3ce1b845 2019-07-15 stsp
436 3ce1b845 2019-07-15 stsp static const struct got_error *
437 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
438 28cf319f 2021-01-28 stsp const char *initial_content, size_t initial_content_len,
439 28cf319f 2021-01-28 stsp int require_modification)
440 3ce1b845 2019-07-15 stsp {
441 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
442 bfa12d5e 2020-09-26 stsp char *line = NULL;
443 bfa12d5e 2020-09-26 stsp size_t linesize = 0;
444 bfa12d5e 2020-09-26 stsp ssize_t linelen;
445 3ce1b845 2019-07-15 stsp struct stat st, st2;
446 bfa12d5e 2020-09-26 stsp FILE *fp = NULL;
447 bfa12d5e 2020-09-26 stsp size_t len, logmsg_len;
448 bfa12d5e 2020-09-26 stsp char *initial_content_stripped = NULL, *buf = NULL, *s;
449 3ce1b845 2019-07-15 stsp
450 3ce1b845 2019-07-15 stsp *logmsg = NULL;
451 3ce1b845 2019-07-15 stsp
452 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
453 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
454 3ce1b845 2019-07-15 stsp
455 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
456 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
457 3ce1b845 2019-07-15 stsp
458 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
459 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
460 3ce1b845 2019-07-15 stsp
461 28cf319f 2021-01-28 stsp if (require_modification &&
462 28cf319f 2021-01-28 stsp st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
463 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
464 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
465 3ce1b845 2019-07-15 stsp
466 bfa12d5e 2020-09-26 stsp /*
467 bfa12d5e 2020-09-26 stsp * Set up a stripped version of the initial content without comments
468 bfa12d5e 2020-09-26 stsp * and blank lines. We need this in order to check if the message
469 bfa12d5e 2020-09-26 stsp * has in fact been edited.
470 bfa12d5e 2020-09-26 stsp */
471 bfa12d5e 2020-09-26 stsp initial_content_stripped = malloc(initial_content_len + 1);
472 bfa12d5e 2020-09-26 stsp if (initial_content_stripped == NULL)
473 bfa12d5e 2020-09-26 stsp return got_error_from_errno("malloc");
474 bfa12d5e 2020-09-26 stsp initial_content_stripped[0] = '\0';
475 bfa12d5e 2020-09-26 stsp
476 bfa12d5e 2020-09-26 stsp buf = strdup(initial_content);
477 bfa12d5e 2020-09-26 stsp if (buf == NULL) {
478 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("strdup");
479 bfa12d5e 2020-09-26 stsp goto done;
480 bfa12d5e 2020-09-26 stsp }
481 bfa12d5e 2020-09-26 stsp s = buf;
482 bfa12d5e 2020-09-26 stsp len = 0;
483 bfa12d5e 2020-09-26 stsp while ((line = strsep(&s, "\n")) != NULL) {
484 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
485 bfa12d5e 2020-09-26 stsp continue; /* remove comments and leading empty lines */
486 bfa12d5e 2020-09-26 stsp len = strlcat(initial_content_stripped, line,
487 bfa12d5e 2020-09-26 stsp initial_content_len + 1);
488 bfa12d5e 2020-09-26 stsp if (len >= initial_content_len + 1) {
489 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
490 bfa12d5e 2020-09-26 stsp goto done;
491 bfa12d5e 2020-09-26 stsp }
492 bfa12d5e 2020-09-26 stsp }
493 bfa12d5e 2020-09-26 stsp while (len > 0 && initial_content_stripped[len - 1] == '\n') {
494 bfa12d5e 2020-09-26 stsp initial_content_stripped[len - 1] = '\0';
495 bfa12d5e 2020-09-26 stsp len--;
496 bfa12d5e 2020-09-26 stsp }
497 bfa12d5e 2020-09-26 stsp
498 bfa12d5e 2020-09-26 stsp logmsg_len = st2.st_size;
499 bfa12d5e 2020-09-26 stsp *logmsg = malloc(logmsg_len + 1);
500 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
501 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
502 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
503 3ce1b845 2019-07-15 stsp
504 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
505 3ce1b845 2019-07-15 stsp if (fp == NULL) {
506 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
507 3ce1b845 2019-07-15 stsp goto done;
508 3ce1b845 2019-07-15 stsp }
509 bfa12d5e 2020-09-26 stsp
510 bfa12d5e 2020-09-26 stsp len = 0;
511 bfa12d5e 2020-09-26 stsp while ((linelen = getline(&line, &linesize, fp)) != -1) {
512 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
513 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
514 bfa12d5e 2020-09-26 stsp len = strlcat(*logmsg, line, logmsg_len + 1);
515 bfa12d5e 2020-09-26 stsp if (len >= logmsg_len + 1) {
516 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
517 bfa12d5e 2020-09-26 stsp goto done;
518 bfa12d5e 2020-09-26 stsp }
519 3ce1b845 2019-07-15 stsp }
520 bfa12d5e 2020-09-26 stsp free(line);
521 bfa12d5e 2020-09-26 stsp if (ferror(fp)) {
522 bfa12d5e 2020-09-26 stsp err = got_ferror(fp, GOT_ERR_IO);
523 bfa12d5e 2020-09-26 stsp goto done;
524 bfa12d5e 2020-09-26 stsp }
525 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
526 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
527 3ce1b845 2019-07-15 stsp len--;
528 3ce1b845 2019-07-15 stsp }
529 3ce1b845 2019-07-15 stsp
530 bfa12d5e 2020-09-26 stsp if (len == 0) {
531 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
532 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
533 bfa12d5e 2020-09-26 stsp goto done;
534 bfa12d5e 2020-09-26 stsp }
535 28cf319f 2021-01-28 stsp if (require_modification &&
536 28cf319f 2021-01-28 stsp strcmp(*logmsg, initial_content_stripped) == 0)
537 bfa12d5e 2020-09-26 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
538 bfa12d5e 2020-09-26 stsp "no changes made to commit message, aborting");
539 3ce1b845 2019-07-15 stsp done:
540 bfa12d5e 2020-09-26 stsp free(initial_content_stripped);
541 bfa12d5e 2020-09-26 stsp free(buf);
542 bfa12d5e 2020-09-26 stsp if (fp && fclose(fp) == EOF && err == NULL)
543 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("fclose");
544 3ce1b845 2019-07-15 stsp if (err) {
545 3ce1b845 2019-07-15 stsp free(*logmsg);
546 3ce1b845 2019-07-15 stsp *logmsg = NULL;
547 3ce1b845 2019-07-15 stsp }
548 3ce1b845 2019-07-15 stsp return err;
549 3ce1b845 2019-07-15 stsp }
550 3ce1b845 2019-07-15 stsp
551 3ce1b845 2019-07-15 stsp static const struct got_error *
552 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
553 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
554 3ce1b845 2019-07-15 stsp {
555 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
556 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
557 1601cb9f 2020-09-11 naddy int initial_content_len;
558 97972933 2020-09-11 stsp int fd = -1;
559 3ce1b845 2019-07-15 stsp
560 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
561 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
562 1601cb9f 2020-09-11 naddy branch_name);
563 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
564 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
565 3ce1b845 2019-07-15 stsp
566 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
567 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
568 3ce1b845 2019-07-15 stsp if (err)
569 3ce1b845 2019-07-15 stsp goto done;
570 3ce1b845 2019-07-15 stsp
571 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
572 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
573 97972933 2020-09-11 stsp goto done;
574 97972933 2020-09-11 stsp }
575 3ce1b845 2019-07-15 stsp
576 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
577 0d5bb276 2020-12-15 stsp initial_content_len, 1);
578 3ce1b845 2019-07-15 stsp done:
579 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
580 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
581 3ce1b845 2019-07-15 stsp free(initial_content);
582 59f86c76 2020-09-11 stsp if (err) {
583 59f86c76 2020-09-11 stsp free(*logmsg_path);
584 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
585 59f86c76 2020-09-11 stsp }
586 3ce1b845 2019-07-15 stsp return err;
587 3ce1b845 2019-07-15 stsp }
588 3ce1b845 2019-07-15 stsp
589 3ce1b845 2019-07-15 stsp static const struct got_error *
590 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
591 3ce1b845 2019-07-15 stsp {
592 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
593 3ce1b845 2019-07-15 stsp return NULL;
594 3ce1b845 2019-07-15 stsp }
595 3ce1b845 2019-07-15 stsp
596 3ce1b845 2019-07-15 stsp static const struct got_error *
597 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
598 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
599 84792843 2019-08-09 stsp {
600 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
601 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
602 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
603 84792843 2019-08-09 stsp
604 84792843 2019-08-09 stsp *author = NULL;
605 aba9c984 2019-09-08 stsp
606 50b0790e 2020-09-11 stsp if (worktree)
607 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
608 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
609 50b0790e 2020-09-11 stsp
610 50b0790e 2020-09-11 stsp /*
611 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
612 50b0790e 2020-09-11 stsp * significant to least significant:
613 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
614 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
615 50b0790e 2020-09-11 stsp * 3) repository's git config file
616 50b0790e 2020-09-11 stsp * 4) environment variables
617 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
618 50b0790e 2020-09-11 stsp */
619 50b0790e 2020-09-11 stsp
620 50b0790e 2020-09-11 stsp if (worktree_conf)
621 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
622 50b0790e 2020-09-11 stsp if (got_author == NULL)
623 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
624 84792843 2019-08-09 stsp if (got_author == NULL) {
625 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
626 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
627 c9956ddf 2019-09-08 stsp if (name && email) {
628 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
629 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
630 c9956ddf 2019-09-08 stsp return NULL;
631 c9956ddf 2019-09-08 stsp }
632 257add31 2020-09-09 stsp
633 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
634 257add31 2020-09-09 stsp if (got_author == NULL) {
635 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
636 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
637 257add31 2020-09-09 stsp repo);
638 257add31 2020-09-09 stsp if (name && email) {
639 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
640 257add31 2020-09-09 stsp == -1)
641 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
642 257add31 2020-09-09 stsp return NULL;
643 257add31 2020-09-09 stsp }
644 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
645 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
646 257add31 2020-09-09 stsp }
647 84792843 2019-08-09 stsp }
648 84792843 2019-08-09 stsp
649 aba9c984 2019-09-08 stsp *author = strdup(got_author);
650 aba9c984 2019-09-08 stsp if (*author == NULL)
651 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
652 84792843 2019-08-09 stsp
653 84792843 2019-08-09 stsp /*
654 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
655 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
656 84792843 2019-08-09 stsp */
657 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
658 84792843 2019-08-09 stsp got_author++;
659 aba9c984 2019-09-08 stsp if (*got_author != '<') {
660 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
661 aba9c984 2019-09-08 stsp goto done;
662 aba9c984 2019-09-08 stsp }
663 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
664 84792843 2019-08-09 stsp got_author++;
665 aba9c984 2019-09-08 stsp if (*got_author != '@') {
666 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
667 aba9c984 2019-09-08 stsp goto done;
668 aba9c984 2019-09-08 stsp }
669 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
670 84792843 2019-08-09 stsp got_author++;
671 84792843 2019-08-09 stsp if (*got_author != '>')
672 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
673 aba9c984 2019-09-08 stsp done:
674 aba9c984 2019-09-08 stsp if (err) {
675 aba9c984 2019-09-08 stsp free(*author);
676 aba9c984 2019-09-08 stsp *author = NULL;
677 aba9c984 2019-09-08 stsp }
678 aba9c984 2019-09-08 stsp return err;
679 c9956ddf 2019-09-08 stsp }
680 c9956ddf 2019-09-08 stsp
681 c9956ddf 2019-09-08 stsp static const struct got_error *
682 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
683 c9956ddf 2019-09-08 stsp {
684 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
685 c9956ddf 2019-09-08 stsp
686 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
687 c9956ddf 2019-09-08 stsp if (homedir) {
688 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
689 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
690 c9956ddf 2019-09-08 stsp
691 c9956ddf 2019-09-08 stsp }
692 c9956ddf 2019-09-08 stsp return NULL;
693 84792843 2019-08-09 stsp }
694 84792843 2019-08-09 stsp
695 84792843 2019-08-09 stsp static const struct got_error *
696 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
697 3ce1b845 2019-07-15 stsp {
698 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
699 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
700 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
701 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
702 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
703 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
704 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
705 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
706 3ce1b845 2019-07-15 stsp int ch;
707 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
708 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
709 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
710 3ce1b845 2019-07-15 stsp
711 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
712 3ce1b845 2019-07-15 stsp
713 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
714 3ce1b845 2019-07-15 stsp switch (ch) {
715 3ce1b845 2019-07-15 stsp case 'b':
716 3ce1b845 2019-07-15 stsp branch_name = optarg;
717 3ce1b845 2019-07-15 stsp break;
718 3ce1b845 2019-07-15 stsp case 'm':
719 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
720 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
721 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
722 3ce1b845 2019-07-15 stsp goto done;
723 3ce1b845 2019-07-15 stsp }
724 3ce1b845 2019-07-15 stsp break;
725 3ce1b845 2019-07-15 stsp case 'r':
726 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
727 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
728 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
729 9ba1d308 2019-10-21 stsp optarg);
730 3ce1b845 2019-07-15 stsp goto done;
731 3ce1b845 2019-07-15 stsp }
732 3ce1b845 2019-07-15 stsp break;
733 3ce1b845 2019-07-15 stsp case 'I':
734 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
735 3ce1b845 2019-07-15 stsp break;
736 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
737 3ce1b845 2019-07-15 stsp NULL);
738 3ce1b845 2019-07-15 stsp if (error)
739 3ce1b845 2019-07-15 stsp goto done;
740 3ce1b845 2019-07-15 stsp break;
741 3ce1b845 2019-07-15 stsp default:
742 b2b65d18 2019-08-22 stsp usage_import();
743 3ce1b845 2019-07-15 stsp /* NOTREACHED */
744 3ce1b845 2019-07-15 stsp }
745 3ce1b845 2019-07-15 stsp }
746 3ce1b845 2019-07-15 stsp
747 3ce1b845 2019-07-15 stsp argc -= optind;
748 3ce1b845 2019-07-15 stsp argv += optind;
749 3ce1b845 2019-07-15 stsp
750 3ce1b845 2019-07-15 stsp #ifndef PROFILE
751 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
752 aba9c984 2019-09-08 stsp "unveil",
753 3ce1b845 2019-07-15 stsp NULL) == -1)
754 3ce1b845 2019-07-15 stsp err(1, "pledge");
755 3ce1b845 2019-07-15 stsp #endif
756 3ce1b845 2019-07-15 stsp if (argc != 1)
757 3ce1b845 2019-07-15 stsp usage_import();
758 2c7829a4 2019-06-17 stsp
759 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
760 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
761 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
762 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
763 3ce1b845 2019-07-15 stsp }
764 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
765 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
766 c9956ddf 2019-09-08 stsp if (error)
767 c9956ddf 2019-09-08 stsp goto done;
768 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
769 3ce1b845 2019-07-15 stsp if (error)
770 3ce1b845 2019-07-15 stsp goto done;
771 aba9c984 2019-09-08 stsp
772 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
773 aba9c984 2019-09-08 stsp if (error)
774 aba9c984 2019-09-08 stsp return error;
775 e560b7e0 2019-11-28 stsp
776 e560b7e0 2019-11-28 stsp /*
777 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
778 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
779 e560b7e0 2019-11-28 stsp * an unintended typo.
780 e560b7e0 2019-11-28 stsp */
781 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
782 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
783 3ce1b845 2019-07-15 stsp
784 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
785 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
786 3ce1b845 2019-07-15 stsp goto done;
787 3ce1b845 2019-07-15 stsp }
788 3ce1b845 2019-07-15 stsp
789 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
790 3ce1b845 2019-07-15 stsp if (error) {
791 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
792 3ce1b845 2019-07-15 stsp goto done;
793 3ce1b845 2019-07-15 stsp } else {
794 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
795 3ce1b845 2019-07-15 stsp "import target branch already exists");
796 3ce1b845 2019-07-15 stsp goto done;
797 3ce1b845 2019-07-15 stsp }
798 3ce1b845 2019-07-15 stsp
799 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
800 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
801 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
802 3ce1b845 2019-07-15 stsp goto done;
803 3ce1b845 2019-07-15 stsp }
804 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
805 3ce1b845 2019-07-15 stsp
806 3ce1b845 2019-07-15 stsp /*
807 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
808 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
809 3ce1b845 2019-07-15 stsp */
810 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
811 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
812 3ce1b845 2019-07-15 stsp if (error)
813 3ce1b845 2019-07-15 stsp goto done;
814 8e158b01 2019-09-22 stsp free(logmsg);
815 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
816 ef293bdd 2019-10-21 stsp path_dir, refname);
817 ef293bdd 2019-10-21 stsp if (error) {
818 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
819 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
820 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
821 3ce1b845 2019-07-15 stsp goto done;
822 ef293bdd 2019-10-21 stsp }
823 3ce1b845 2019-07-15 stsp }
824 3ce1b845 2019-07-15 stsp
825 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
826 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
827 ef293bdd 2019-10-21 stsp if (logmsg_path)
828 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
829 3ce1b845 2019-07-15 stsp goto done;
830 ef293bdd 2019-10-21 stsp }
831 3ce1b845 2019-07-15 stsp
832 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
833 ef293bdd 2019-10-21 stsp if (error) {
834 ef293bdd 2019-10-21 stsp if (logmsg_path)
835 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
836 ef293bdd 2019-10-21 stsp goto done;
837 ef293bdd 2019-10-21 stsp }
838 ef293bdd 2019-10-21 stsp
839 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
840 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
841 ef293bdd 2019-10-21 stsp if (error) {
842 ef293bdd 2019-10-21 stsp if (logmsg_path)
843 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
844 3ce1b845 2019-07-15 stsp goto done;
845 ef293bdd 2019-10-21 stsp }
846 3ce1b845 2019-07-15 stsp
847 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
848 ef293bdd 2019-10-21 stsp if (error) {
849 ef293bdd 2019-10-21 stsp if (logmsg_path)
850 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
851 3ce1b845 2019-07-15 stsp goto done;
852 ef293bdd 2019-10-21 stsp }
853 3ce1b845 2019-07-15 stsp
854 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
855 ef293bdd 2019-10-21 stsp if (error) {
856 ef293bdd 2019-10-21 stsp if (logmsg_path)
857 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
858 3ce1b845 2019-07-15 stsp goto done;
859 ef293bdd 2019-10-21 stsp }
860 3ce1b845 2019-07-15 stsp
861 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
862 ef293bdd 2019-10-21 stsp if (error) {
863 ef293bdd 2019-10-21 stsp if (logmsg_path)
864 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
865 3ce1b845 2019-07-15 stsp goto done;
866 ef293bdd 2019-10-21 stsp }
867 3ce1b845 2019-07-15 stsp
868 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
869 3ce1b845 2019-07-15 stsp if (error) {
870 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
871 ef293bdd 2019-10-21 stsp if (logmsg_path)
872 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
873 3ce1b845 2019-07-15 stsp goto done;
874 ef293bdd 2019-10-21 stsp }
875 3ce1b845 2019-07-15 stsp
876 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
877 3ce1b845 2019-07-15 stsp branch_ref);
878 ef293bdd 2019-10-21 stsp if (error) {
879 ef293bdd 2019-10-21 stsp if (logmsg_path)
880 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
881 3ce1b845 2019-07-15 stsp goto done;
882 ef293bdd 2019-10-21 stsp }
883 3ce1b845 2019-07-15 stsp
884 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
885 ef293bdd 2019-10-21 stsp if (error) {
886 ef293bdd 2019-10-21 stsp if (logmsg_path)
887 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
888 3ce1b845 2019-07-15 stsp goto done;
889 ef293bdd 2019-10-21 stsp }
890 3ce1b845 2019-07-15 stsp }
891 3ce1b845 2019-07-15 stsp
892 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
893 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
894 2c7829a4 2019-06-17 stsp done:
895 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
896 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
897 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
898 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
899 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
900 8e158b01 2019-09-22 stsp free(logmsg);
901 ef293bdd 2019-10-21 stsp free(logmsg_path);
902 2c7829a4 2019-06-17 stsp free(repo_path);
903 3ce1b845 2019-07-15 stsp free(editor);
904 3ce1b845 2019-07-15 stsp free(refname);
905 3ce1b845 2019-07-15 stsp free(new_commit_id);
906 3ce1b845 2019-07-15 stsp free(id_str);
907 aba9c984 2019-09-08 stsp free(author);
908 c9956ddf 2019-09-08 stsp free(gitconfig_path);
909 3ce1b845 2019-07-15 stsp if (branch_ref)
910 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
911 3ce1b845 2019-07-15 stsp if (head_ref)
912 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
913 2c7829a4 2019-06-17 stsp return error;
914 93658fb9 2020-03-18 stsp }
915 93658fb9 2020-03-18 stsp
916 93658fb9 2020-03-18 stsp __dead static void
917 93658fb9 2020-03-18 stsp usage_clone(void)
918 93658fb9 2020-03-18 stsp {
919 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
920 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
921 93658fb9 2020-03-18 stsp exit(1);
922 93658fb9 2020-03-18 stsp }
923 892ac3b6 2020-03-18 stsp
924 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
925 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
926 892ac3b6 2020-03-18 stsp int last_p_indexed;
927 892ac3b6 2020-03-18 stsp int last_p_resolved;
928 68999b92 2020-03-18 stsp int verbosity;
929 04d9a9ec 2020-09-24 stsp
930 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
931 04d9a9ec 2020-09-24 stsp
932 04d9a9ec 2020-09-24 stsp int create_configs;
933 04d9a9ec 2020-09-24 stsp int configs_created;
934 04d9a9ec 2020-09-24 stsp struct {
935 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
936 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
937 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs;
938 04d9a9ec 2020-09-24 stsp const char *proto;
939 04d9a9ec 2020-09-24 stsp const char *host;
940 04d9a9ec 2020-09-24 stsp const char *port;
941 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
942 04d9a9ec 2020-09-24 stsp const char *git_url;
943 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
944 04d9a9ec 2020-09-24 stsp int mirror_references;
945 04d9a9ec 2020-09-24 stsp } config_info;
946 892ac3b6 2020-03-18 stsp };
947 93658fb9 2020-03-18 stsp
948 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
949 93658fb9 2020-03-18 stsp static const struct got_error *
950 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
951 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
952 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
953 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
954 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo);
955 04d9a9ec 2020-09-24 stsp
956 04d9a9ec 2020-09-24 stsp static const struct got_error *
957 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
958 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
959 531c3985 2020-03-18 stsp {
960 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
961 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
962 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
963 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
964 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
965 04d9a9ec 2020-09-24 stsp
966 04d9a9ec 2020-09-24 stsp /*
967 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
968 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
969 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
970 04d9a9ec 2020-09-24 stsp * we have all required information.
971 04d9a9ec 2020-09-24 stsp */
972 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
973 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
974 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
975 62d463ca 2020-10-20 naddy a->config_info.host, a->config_info.port,
976 62d463ca 2020-10-20 naddy a->config_info.remote_repo_path,
977 62d463ca 2020-10-20 naddy a->config_info.git_url,
978 62d463ca 2020-10-20 naddy a->config_info.fetch_all_branches,
979 62d463ca 2020-10-20 naddy a->config_info.mirror_references,
980 62d463ca 2020-10-20 naddy a->config_info.symrefs,
981 99495ddb 2021-01-10 stsp a->config_info.wanted_branches,
982 99495ddb 2021-01-10 stsp a->config_info.wanted_refs, a->repo);
983 04d9a9ec 2020-09-24 stsp if (err)
984 04d9a9ec 2020-09-24 stsp return err;
985 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
986 04d9a9ec 2020-09-24 stsp }
987 b2409d58 2020-03-18 stsp
988 68999b92 2020-03-18 stsp if (a->verbosity < 0)
989 68999b92 2020-03-18 stsp return NULL;
990 68999b92 2020-03-18 stsp
991 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
992 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
993 892ac3b6 2020-03-18 stsp fflush(stdout);
994 12d1281e 2020-03-19 stsp return NULL;
995 b2409d58 2020-03-18 stsp }
996 b2409d58 2020-03-18 stsp
997 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
998 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
999 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
1000 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
1001 892ac3b6 2020-03-18 stsp print_size = 1;
1002 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
1003 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1004 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
1005 892ac3b6 2020-03-18 stsp }
1006 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
1007 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
1008 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
1009 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
1010 892ac3b6 2020-03-18 stsp print_indexed = 1;
1011 892ac3b6 2020-03-18 stsp print_size = 1;
1012 892ac3b6 2020-03-18 stsp }
1013 61cc1a7a 2020-03-18 stsp }
1014 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
1015 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
1016 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
1017 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
1018 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
1019 892ac3b6 2020-03-18 stsp print_resolved = 1;
1020 892ac3b6 2020-03-18 stsp print_indexed = 1;
1021 892ac3b6 2020-03-18 stsp print_size = 1;
1022 892ac3b6 2020-03-18 stsp }
1023 61cc1a7a 2020-03-18 stsp }
1024 3168e5da 2020-09-10 stsp
1025 d2cdc636 2020-03-18 stsp }
1026 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1027 892ac3b6 2020-03-18 stsp printf("\r");
1028 892ac3b6 2020-03-18 stsp if (print_size)
1029 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1030 d715f13e 2020-03-19 stsp if (print_indexed)
1031 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
1032 d715f13e 2020-03-19 stsp if (print_resolved)
1033 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
1034 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1035 892ac3b6 2020-03-18 stsp fflush(stdout);
1036 892ac3b6 2020-03-18 stsp
1037 531c3985 2020-03-18 stsp return NULL;
1038 531c3985 2020-03-18 stsp }
1039 531c3985 2020-03-18 stsp
1040 531c3985 2020-03-18 stsp static const struct got_error *
1041 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
1042 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
1043 4ba14133 2020-03-20 stsp {
1044 4ba14133 2020-03-20 stsp const struct got_error *err;
1045 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
1046 4ba14133 2020-03-20 stsp
1047 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1048 4ba14133 2020-03-20 stsp if (err)
1049 4ba14133 2020-03-20 stsp return err;
1050 4ba14133 2020-03-20 stsp
1051 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
1052 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
1053 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
1054 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
1055 6338a6a1 2020-03-21 stsp }
1056 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
1057 4ba14133 2020-03-20 stsp return err;
1058 4ba14133 2020-03-20 stsp }
1059 4ba14133 2020-03-20 stsp
1060 4ba14133 2020-03-20 stsp static const struct got_error *
1061 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
1062 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
1063 41b0de12 2020-03-21 stsp {
1064 41b0de12 2020-03-21 stsp const struct got_error *err;
1065 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
1066 41b0de12 2020-03-21 stsp
1067 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1068 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1069 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1070 41b0de12 2020-03-21 stsp
1071 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1072 41b0de12 2020-03-21 stsp }
1073 41b0de12 2020-03-21 stsp
1074 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1075 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1076 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1077 41b0de12 2020-03-21 stsp char *id_str;
1078 41b0de12 2020-03-21 stsp
1079 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1080 41b0de12 2020-03-21 stsp if (err)
1081 41b0de12 2020-03-21 stsp return err;
1082 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1083 41b0de12 2020-03-21 stsp free(id_str);
1084 41b0de12 2020-03-21 stsp }
1085 41b0de12 2020-03-21 stsp
1086 41b0de12 2020-03-21 stsp return NULL;
1087 6338a6a1 2020-03-21 stsp }
1088 6338a6a1 2020-03-21 stsp
1089 6338a6a1 2020-03-21 stsp static const struct got_error *
1090 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1091 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1092 6338a6a1 2020-03-21 stsp {
1093 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1094 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1095 6338a6a1 2020-03-21 stsp char *id_str;
1096 6338a6a1 2020-03-21 stsp
1097 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1098 6338a6a1 2020-03-21 stsp if (err)
1099 6338a6a1 2020-03-21 stsp return err;
1100 6338a6a1 2020-03-21 stsp
1101 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1102 6338a6a1 2020-03-21 stsp if (err)
1103 6338a6a1 2020-03-21 stsp goto done;
1104 6338a6a1 2020-03-21 stsp
1105 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1106 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1107 6338a6a1 2020-03-21 stsp
1108 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1109 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1110 6338a6a1 2020-03-21 stsp done:
1111 6338a6a1 2020-03-21 stsp free(id_str);
1112 6338a6a1 2020-03-21 stsp return err;
1113 0e4002ca 2020-03-21 stsp }
1114 0e4002ca 2020-03-21 stsp
1115 0e4002ca 2020-03-21 stsp static int
1116 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1117 0e4002ca 2020-03-21 stsp {
1118 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1119 0e4002ca 2020-03-21 stsp return 0;
1120 0e4002ca 2020-03-21 stsp refname += 5;
1121 0e4002ca 2020-03-21 stsp
1122 0e4002ca 2020-03-21 stsp /*
1123 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1124 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1125 0e4002ca 2020-03-21 stsp */
1126 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1127 0e4002ca 2020-03-21 stsp return 0;
1128 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1129 0e4002ca 2020-03-21 stsp return 0;
1130 0e4002ca 2020-03-21 stsp
1131 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1132 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1133 0e4002ca 2020-03-21 stsp
1134 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1135 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1136 0e4002ca 2020-03-21 stsp return 1;
1137 0e4002ca 2020-03-21 stsp
1138 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1139 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1140 41b0de12 2020-03-21 stsp }
1141 41b0de12 2020-03-21 stsp
1142 0e4002ca 2020-03-21 stsp static int
1143 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1144 0e4002ca 2020-03-21 stsp {
1145 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1146 0e4002ca 2020-03-21 stsp
1147 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1148 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1149 0e4002ca 2020-03-21 stsp return 1;
1150 0e4002ca 2020-03-21 stsp }
1151 0e4002ca 2020-03-21 stsp
1152 0e4002ca 2020-03-21 stsp return 0;
1153 0e4002ca 2020-03-21 stsp }
1154 0e4002ca 2020-03-21 stsp
1155 41b0de12 2020-03-21 stsp static const struct got_error *
1156 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1157 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1158 0e4002ca 2020-03-21 stsp {
1159 0e4002ca 2020-03-21 stsp const struct got_error *err;
1160 0e4002ca 2020-03-21 stsp char *remote_refname;
1161 0e4002ca 2020-03-21 stsp
1162 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1163 0e4002ca 2020-03-21 stsp refname += 5;
1164 0e4002ca 2020-03-21 stsp
1165 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1166 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1167 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1168 0e4002ca 2020-03-21 stsp
1169 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1170 0e4002ca 2020-03-21 stsp free(remote_refname);
1171 7c0b7f42 2020-09-24 stsp return err;
1172 7c0b7f42 2020-09-24 stsp }
1173 7c0b7f42 2020-09-24 stsp
1174 7c0b7f42 2020-09-24 stsp static const struct got_error *
1175 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1176 15d3c221 2021-01-05 stsp const char *remote_repo_path, const char *default_branch,
1177 0c8b29c5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1178 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1179 99495ddb 2021-01-10 stsp struct got_repository *repo)
1180 7c0b7f42 2020-09-24 stsp {
1181 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1182 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1183 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1184 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1185 15d3c221 2021-01-05 stsp const char *branchname = NULL;
1186 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1187 7c0b7f42 2020-09-24 stsp ssize_t n;
1188 7c0b7f42 2020-09-24 stsp
1189 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1190 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1191 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1192 132af4a5 2021-01-05 stsp char *s;
1193 132af4a5 2021-01-05 stsp branchname = pe->path;
1194 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1195 132af4a5 2021-01-05 stsp branchname += 11;
1196 132af4a5 2021-01-05 stsp if (asprintf(&s, "%s\"%s\" ",
1197 132af4a5 2021-01-05 stsp branches ? branches : "", branchname) == -1) {
1198 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1199 132af4a5 2021-01-05 stsp goto done;
1200 132af4a5 2021-01-05 stsp }
1201 132af4a5 2021-01-05 stsp free(branches);
1202 132af4a5 2021-01-05 stsp branches = s;
1203 132af4a5 2021-01-05 stsp }
1204 0c8b29c5 2021-01-05 stsp } else if (!fetch_all_branches && default_branch) {
1205 15d3c221 2021-01-05 stsp branchname = default_branch;
1206 15d3c221 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1207 15d3c221 2021-01-05 stsp branchname += 11;
1208 132af4a5 2021-01-05 stsp if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1209 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1210 132af4a5 2021-01-05 stsp goto done;
1211 99495ddb 2021-01-10 stsp }
1212 99495ddb 2021-01-10 stsp }
1213 99495ddb 2021-01-10 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1214 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1215 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1216 99495ddb 2021-01-10 stsp char *s;
1217 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1218 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1219 99495ddb 2021-01-10 stsp branchname += 5;
1220 99495ddb 2021-01-10 stsp if (asprintf(&s, "%s\"%s\" ",
1221 99495ddb 2021-01-10 stsp refs ? refs : "", refname) == -1) {
1222 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1223 99495ddb 2021-01-10 stsp goto done;
1224 99495ddb 2021-01-10 stsp }
1225 99495ddb 2021-01-10 stsp free(refs);
1226 99495ddb 2021-01-10 stsp refs = s;
1227 132af4a5 2021-01-05 stsp }
1228 15d3c221 2021-01-05 stsp }
1229 15d3c221 2021-01-05 stsp
1230 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1231 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1232 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1233 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1234 7c0b7f42 2020-09-24 stsp goto done;
1235 7c0b7f42 2020-09-24 stsp }
1236 7c0b7f42 2020-09-24 stsp gotconfig_file = fopen(gotconfig_path, "a");
1237 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1238 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1239 7c0b7f42 2020-09-24 stsp goto done;
1240 7c0b7f42 2020-09-24 stsp }
1241 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1242 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1243 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1244 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1245 7c0b7f42 2020-09-24 stsp "%s%s%s"
1246 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1247 15d3c221 2021-01-05 stsp "%s%s%s"
1248 99495ddb 2021-01-10 stsp "%s%s%s"
1249 7c0b7f42 2020-09-24 stsp "%s"
1250 0c8b29c5 2021-01-05 stsp "%s"
1251 7c0b7f42 2020-09-24 stsp "}\n",
1252 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1253 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1254 132af4a5 2021-01-05 stsp remote_repo_path, branches ? "\tbranch { " : "",
1255 132af4a5 2021-01-05 stsp branches ? branches : "", branches ? "}\n" : "",
1256 99495ddb 2021-01-10 stsp refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1257 0c8b29c5 2021-01-05 stsp mirror_references ? "\tmirror-references yes\n" : "",
1258 0c8b29c5 2021-01-05 stsp fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1259 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1260 7c0b7f42 2020-09-24 stsp goto done;
1261 7c0b7f42 2020-09-24 stsp }
1262 7c0b7f42 2020-09-24 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1263 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1264 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1265 7c0b7f42 2020-09-24 stsp goto done;
1266 7c0b7f42 2020-09-24 stsp }
1267 7c0b7f42 2020-09-24 stsp
1268 7c0b7f42 2020-09-24 stsp done:
1269 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1270 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1271 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1272 132af4a5 2021-01-05 stsp free(branches);
1273 7c0b7f42 2020-09-24 stsp return err;
1274 7c0b7f42 2020-09-24 stsp }
1275 7c0b7f42 2020-09-24 stsp
1276 7c0b7f42 2020-09-24 stsp static const struct got_error *
1277 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1278 132af4a5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1279 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1280 99495ddb 2021-01-10 stsp struct got_repository *repo)
1281 7c0b7f42 2020-09-24 stsp {
1282 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1283 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1284 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1285 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1286 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1287 56d0a753 2021-01-20 stsp const char *branchname;
1288 7c0b7f42 2020-09-24 stsp ssize_t n;
1289 7c0b7f42 2020-09-24 stsp
1290 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1291 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1292 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1293 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1294 7c0b7f42 2020-09-24 stsp goto done;
1295 7c0b7f42 2020-09-24 stsp }
1296 7c0b7f42 2020-09-24 stsp gitconfig_file = fopen(gitconfig_path, "a");
1297 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1298 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1299 7c0b7f42 2020-09-24 stsp goto done;
1300 7c0b7f42 2020-09-24 stsp }
1301 56d0a753 2021-01-20 stsp if (fetch_all_branches) {
1302 56d0a753 2021-01-20 stsp if (mirror_references) {
1303 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1304 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1305 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1306 56d0a753 2021-01-20 stsp goto done;
1307 56d0a753 2021-01-20 stsp }
1308 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1309 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1310 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1311 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1312 7c0b7f42 2020-09-24 stsp goto done;
1313 132af4a5 2021-01-05 stsp }
1314 132af4a5 2021-01-05 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
1315 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1316 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1317 132af4a5 2021-01-05 stsp char *s;
1318 132af4a5 2021-01-05 stsp branchname = pe->path;
1319 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1320 132af4a5 2021-01-05 stsp branchname += 11;
1321 56d0a753 2021-01-20 stsp if (mirror_references) {
1322 56d0a753 2021-01-20 stsp if (asprintf(&s,
1323 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1324 56d0a753 2021-01-20 stsp branches ? branches : "",
1325 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1326 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1327 56d0a753 2021-01-20 stsp goto done;
1328 56d0a753 2021-01-20 stsp }
1329 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1330 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1331 132af4a5 2021-01-05 stsp branches ? branches : "",
1332 132af4a5 2021-01-05 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1333 132af4a5 2021-01-05 stsp branchname) == -1) {
1334 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1335 132af4a5 2021-01-05 stsp goto done;
1336 132af4a5 2021-01-05 stsp }
1337 132af4a5 2021-01-05 stsp free(branches);
1338 132af4a5 2021-01-05 stsp branches = s;
1339 7c0b7f42 2020-09-24 stsp }
1340 7c0b7f42 2020-09-24 stsp } else {
1341 7c0b7f42 2020-09-24 stsp /*
1342 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1343 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1344 7c0b7f42 2020-09-24 stsp */
1345 04d9a9ec 2020-09-24 stsp if (default_branch) {
1346 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1347 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1348 7c0b7f42 2020-09-24 stsp branchname += 11;
1349 7c0b7f42 2020-09-24 stsp } else
1350 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1351 56d0a753 2021-01-20 stsp if (mirror_references) {
1352 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1353 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/heads/%s\n",
1354 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1355 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1356 56d0a753 2021-01-20 stsp goto done;
1357 56d0a753 2021-01-20 stsp }
1358 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1359 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1360 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1361 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1362 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1363 7c0b7f42 2020-09-24 stsp goto done;
1364 99495ddb 2021-01-10 stsp }
1365 99495ddb 2021-01-10 stsp }
1366 56d0a753 2021-01-20 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1367 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1368 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1369 99495ddb 2021-01-10 stsp char *s;
1370 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1371 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1372 99495ddb 2021-01-10 stsp refname += 5;
1373 56d0a753 2021-01-20 stsp if (mirror_references) {
1374 56d0a753 2021-01-20 stsp if (asprintf(&s,
1375 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/%s\n",
1376 56d0a753 2021-01-20 stsp refs ? refs : "", refname, refname) == -1) {
1377 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1378 56d0a753 2021-01-20 stsp goto done;
1379 56d0a753 2021-01-20 stsp }
1380 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1381 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1382 99495ddb 2021-01-10 stsp refs ? refs : "",
1383 99495ddb 2021-01-10 stsp refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1384 99495ddb 2021-01-10 stsp refname) == -1) {
1385 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1386 99495ddb 2021-01-10 stsp goto done;
1387 99495ddb 2021-01-10 stsp }
1388 99495ddb 2021-01-10 stsp free(refs);
1389 99495ddb 2021-01-10 stsp refs = s;
1390 7c0b7f42 2020-09-24 stsp }
1391 132af4a5 2021-01-05 stsp }
1392 99495ddb 2021-01-10 stsp
1393 132af4a5 2021-01-05 stsp if (asprintf(&gitconfig,
1394 132af4a5 2021-01-05 stsp "[remote \"%s\"]\n"
1395 132af4a5 2021-01-05 stsp "\turl = %s\n"
1396 132af4a5 2021-01-05 stsp "%s"
1397 99495ddb 2021-01-10 stsp "%s"
1398 56d0a753 2021-01-20 stsp "\tfetch = refs/tags/*:refs/tags/*\n",
1399 132af4a5 2021-01-05 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1400 56d0a753 2021-01-20 stsp refs ? refs : "") == -1) {
1401 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1402 132af4a5 2021-01-05 stsp goto done;
1403 7c0b7f42 2020-09-24 stsp }
1404 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1405 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1406 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1407 7c0b7f42 2020-09-24 stsp goto done;
1408 7c0b7f42 2020-09-24 stsp }
1409 7c0b7f42 2020-09-24 stsp done:
1410 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1411 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1412 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1413 132af4a5 2021-01-05 stsp free(branches);
1414 0e4002ca 2020-03-21 stsp return err;
1415 0e4002ca 2020-03-21 stsp }
1416 0e4002ca 2020-03-21 stsp
1417 0e4002ca 2020-03-21 stsp static const struct got_error *
1418 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1419 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1420 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1421 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
1422 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1423 04d9a9ec 2020-09-24 stsp {
1424 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1425 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1426 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1427 04d9a9ec 2020-09-24 stsp
1428 04d9a9ec 2020-09-24 stsp /*
1429 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1430 04d9a9ec 2020-09-24 stsp * one of those.
1431 04d9a9ec 2020-09-24 stsp */
1432 62d463ca 2020-10-20 naddy if (!TAILQ_EMPTY(wanted_branches)) {
1433 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1434 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1435 04d9a9ec 2020-09-24 stsp } else {
1436 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1437 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1438 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1439 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1440 04d9a9ec 2020-09-24 stsp
1441 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1442 04d9a9ec 2020-09-24 stsp continue;
1443 04d9a9ec 2020-09-24 stsp
1444 04d9a9ec 2020-09-24 stsp default_branch = target;
1445 04d9a9ec 2020-09-24 stsp break;
1446 04d9a9ec 2020-09-24 stsp }
1447 04d9a9ec 2020-09-24 stsp }
1448 04d9a9ec 2020-09-24 stsp
1449 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1450 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1451 0c8b29c5 2021-01-05 stsp default_branch, fetch_all_branches, wanted_branches,
1452 99495ddb 2021-01-10 stsp wanted_refs, mirror_references, repo);
1453 04d9a9ec 2020-09-24 stsp if (err)
1454 04d9a9ec 2020-09-24 stsp return err;
1455 04d9a9ec 2020-09-24 stsp
1456 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1457 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1458 99495ddb 2021-01-10 stsp wanted_branches, wanted_refs, mirror_references, repo);
1459 04d9a9ec 2020-09-24 stsp }
1460 04d9a9ec 2020-09-24 stsp
1461 04d9a9ec 2020-09-24 stsp static const struct got_error *
1462 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1463 93658fb9 2020-03-18 stsp {
1464 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1465 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1466 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1467 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1468 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1469 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1470 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1471 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1472 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1473 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1474 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1475 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1476 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1477 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1478 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1479 93658fb9 2020-03-18 stsp
1480 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1481 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1482 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1483 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1484 d9b4d0c0 2020-03-18 stsp
1485 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1486 93658fb9 2020-03-18 stsp switch (ch) {
1487 659e7fbd 2020-03-20 stsp case 'a':
1488 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1489 4ba14133 2020-03-20 stsp break;
1490 4ba14133 2020-03-20 stsp case 'b':
1491 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1492 4ba14133 2020-03-20 stsp optarg, NULL);
1493 4ba14133 2020-03-20 stsp if (error)
1494 4ba14133 2020-03-20 stsp return error;
1495 659e7fbd 2020-03-20 stsp break;
1496 41b0de12 2020-03-21 stsp case 'l':
1497 41b0de12 2020-03-21 stsp list_refs_only = 1;
1498 41b0de12 2020-03-21 stsp break;
1499 469dd726 2020-03-20 stsp case 'm':
1500 469dd726 2020-03-20 stsp mirror_references = 1;
1501 469dd726 2020-03-20 stsp break;
1502 68999b92 2020-03-18 stsp case 'v':
1503 68999b92 2020-03-18 stsp if (verbosity < 0)
1504 68999b92 2020-03-18 stsp verbosity = 0;
1505 68999b92 2020-03-18 stsp else if (verbosity < 3)
1506 68999b92 2020-03-18 stsp verbosity++;
1507 68999b92 2020-03-18 stsp break;
1508 68999b92 2020-03-18 stsp case 'q':
1509 68999b92 2020-03-18 stsp verbosity = -1;
1510 68999b92 2020-03-18 stsp break;
1511 0e4002ca 2020-03-21 stsp case 'R':
1512 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1513 0e4002ca 2020-03-21 stsp optarg, NULL);
1514 0e4002ca 2020-03-21 stsp if (error)
1515 0e4002ca 2020-03-21 stsp return error;
1516 0e4002ca 2020-03-21 stsp break;
1517 93658fb9 2020-03-18 stsp default:
1518 93658fb9 2020-03-18 stsp usage_clone();
1519 93658fb9 2020-03-18 stsp break;
1520 93658fb9 2020-03-18 stsp }
1521 93658fb9 2020-03-18 stsp }
1522 93658fb9 2020-03-18 stsp argc -= optind;
1523 93658fb9 2020-03-18 stsp argv += optind;
1524 39c64a6a 2020-03-18 stsp
1525 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1526 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
1527 41b0de12 2020-03-21 stsp if (list_refs_only) {
1528 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1529 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
1530 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1531 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
1532 41b0de12 2020-03-21 stsp if (mirror_references)
1533 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
1534 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1535 ff69268e 2020-12-13 stsp option_conflict('l', 'R');
1536 41b0de12 2020-03-21 stsp }
1537 4ba14133 2020-03-20 stsp
1538 93658fb9 2020-03-18 stsp uri = argv[0];
1539 9df6f38b 2020-03-18 stsp
1540 9df6f38b 2020-03-18 stsp if (argc == 1)
1541 93658fb9 2020-03-18 stsp dirname = NULL;
1542 9df6f38b 2020-03-18 stsp else if (argc == 2)
1543 93658fb9 2020-03-18 stsp dirname = argv[1];
1544 93658fb9 2020-03-18 stsp else
1545 93658fb9 2020-03-18 stsp usage_clone();
1546 09838ffc 2020-03-18 stsp
1547 5e5da8c4 2021-09-05 stsp error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1548 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1549 39c64a6a 2020-03-18 stsp if (error)
1550 09f63084 2020-03-20 stsp goto done;
1551 09f63084 2020-03-20 stsp
1552 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1553 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1554 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1555 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1556 09838ffc 2020-03-18 stsp goto done;
1557 09f63084 2020-03-20 stsp }
1558 09838ffc 2020-03-18 stsp
1559 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1560 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1561 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1562 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1563 39c64a6a 2020-03-18 stsp err(1, "pledge");
1564 b46f3e71 2020-03-18 stsp #endif
1565 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1566 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1567 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1568 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1569 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1570 39c64a6a 2020-03-18 stsp err(1, "pledge");
1571 b46f3e71 2020-03-18 stsp #endif
1572 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1573 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1574 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1575 39c64a6a 2020-03-18 stsp goto done;
1576 39c64a6a 2020-03-18 stsp } else {
1577 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1578 39c64a6a 2020-03-18 stsp goto done;
1579 39c64a6a 2020-03-18 stsp }
1580 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1581 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1582 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1583 bb64b798 2020-03-18 stsp goto done;
1584 bb64b798 2020-03-18 stsp }
1585 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1586 bb64b798 2020-03-18 stsp } else
1587 bb64b798 2020-03-18 stsp repo_path = dirname;
1588 bb64b798 2020-03-18 stsp
1589 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1590 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1591 2751fe64 2020-09-24 stsp if (error &&
1592 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1593 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1594 41b0de12 2020-03-21 stsp goto done;
1595 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1596 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1597 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1598 41b0de12 2020-03-21 stsp goto done;
1599 2751fe64 2020-09-24 stsp }
1600 41b0de12 2020-03-21 stsp }
1601 bb64b798 2020-03-18 stsp
1602 d65a88a2 2021-09-05 stsp error = got_dial_apply_unveil(proto);
1603 d65a88a2 2021-09-05 stsp if (error)
1604 d65a88a2 2021-09-05 stsp goto done;
1605 d65a88a2 2021-09-05 stsp
1606 f535bcd4 2020-09-30 stsp error = apply_unveil(repo_path, 0, NULL);
1607 ee448f5f 2020-03-18 stsp if (error)
1608 ee448f5f 2020-03-18 stsp goto done;
1609 f79e6490 2020-04-19 stsp
1610 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1611 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1612 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1613 ee448f5f 2020-03-18 stsp
1614 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1615 9c52365f 2020-03-21 stsp server_path, verbosity);
1616 39c64a6a 2020-03-18 stsp if (error)
1617 bb64b798 2020-03-18 stsp goto done;
1618 bb64b798 2020-03-18 stsp
1619 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1620 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1621 2751fe64 2020-09-24 stsp if (error)
1622 2751fe64 2020-09-24 stsp goto done;
1623 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1624 2751fe64 2020-09-24 stsp if (error)
1625 2751fe64 2020-09-24 stsp goto done;
1626 2751fe64 2020-09-24 stsp }
1627 2751fe64 2020-09-24 stsp
1628 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1629 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1630 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1631 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1632 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1633 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1634 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1635 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1636 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1637 99495ddb 2021-01-10 stsp fpa.config_info.wanted_refs = &wanted_refs;
1638 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1639 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1640 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1641 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1642 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1643 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1644 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1645 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1646 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1647 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1648 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1649 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1650 39c64a6a 2020-03-18 stsp if (error)
1651 d9b4d0c0 2020-03-18 stsp goto done;
1652 d9b4d0c0 2020-03-18 stsp
1653 41b0de12 2020-03-21 stsp if (list_refs_only) {
1654 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1655 41b0de12 2020-03-21 stsp goto done;
1656 41b0de12 2020-03-21 stsp }
1657 41b0de12 2020-03-21 stsp
1658 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1659 39c64a6a 2020-03-18 stsp if (error)
1660 d9b4d0c0 2020-03-18 stsp goto done;
1661 68999b92 2020-03-18 stsp if (verbosity >= 0)
1662 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1663 d9b4d0c0 2020-03-18 stsp free(id_str);
1664 d9b4d0c0 2020-03-18 stsp
1665 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1666 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1667 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1668 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1669 7ebc0570 2020-03-18 stsp char *remote_refname;
1670 0e4002ca 2020-03-21 stsp
1671 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1672 0e4002ca 2020-03-21 stsp !mirror_references) {
1673 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1674 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1675 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1676 0e4002ca 2020-03-21 stsp if (error)
1677 0e4002ca 2020-03-21 stsp goto done;
1678 0e4002ca 2020-03-21 stsp continue;
1679 0e4002ca 2020-03-21 stsp }
1680 668a20f6 2020-03-18 stsp
1681 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1682 39c64a6a 2020-03-18 stsp if (error)
1683 d9b4d0c0 2020-03-18 stsp goto done;
1684 d9b4d0c0 2020-03-18 stsp
1685 469dd726 2020-03-20 stsp if (mirror_references)
1686 469dd726 2020-03-20 stsp continue;
1687 469dd726 2020-03-20 stsp
1688 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1689 7ebc0570 2020-03-18 stsp continue;
1690 7ebc0570 2020-03-18 stsp
1691 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1692 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1693 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1694 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1695 7ebc0570 2020-03-18 stsp goto done;
1696 7ebc0570 2020-03-18 stsp }
1697 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1698 f298ae0f 2020-03-25 stsp free(remote_refname);
1699 39c64a6a 2020-03-18 stsp if (error)
1700 d9b4d0c0 2020-03-18 stsp goto done;
1701 d9b4d0c0 2020-03-18 stsp }
1702 d9b4d0c0 2020-03-18 stsp
1703 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1704 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1705 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1706 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1707 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1708 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1709 d9b4d0c0 2020-03-18 stsp
1710 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1711 d9b4d0c0 2020-03-18 stsp continue;
1712 d9b4d0c0 2020-03-18 stsp
1713 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1714 39c64a6a 2020-03-18 stsp if (error) {
1715 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1716 55330abe 2020-03-20 stsp error = NULL;
1717 d9b4d0c0 2020-03-18 stsp continue;
1718 55330abe 2020-03-20 stsp }
1719 d9b4d0c0 2020-03-18 stsp goto done;
1720 d9b4d0c0 2020-03-18 stsp }
1721 d9b4d0c0 2020-03-18 stsp
1722 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1723 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1724 f298ae0f 2020-03-25 stsp if (error)
1725 f298ae0f 2020-03-25 stsp goto done;
1726 f298ae0f 2020-03-25 stsp
1727 f298ae0f 2020-03-25 stsp if (mirror_references)
1728 f298ae0f 2020-03-25 stsp continue;
1729 f298ae0f 2020-03-25 stsp
1730 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1731 f298ae0f 2020-03-25 stsp continue;
1732 f298ae0f 2020-03-25 stsp
1733 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1734 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1735 f298ae0f 2020-03-25 stsp refname) == -1) {
1736 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1737 f298ae0f 2020-03-25 stsp goto done;
1738 f298ae0f 2020-03-25 stsp }
1739 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1740 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1741 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1742 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1743 f298ae0f 2020-03-25 stsp free(remote_refname);
1744 f298ae0f 2020-03-25 stsp goto done;
1745 f298ae0f 2020-03-25 stsp }
1746 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1747 f298ae0f 2020-03-25 stsp if (error) {
1748 f298ae0f 2020-03-25 stsp free(remote_refname);
1749 f298ae0f 2020-03-25 stsp free(remote_target);
1750 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1751 f298ae0f 2020-03-25 stsp error = NULL;
1752 f298ae0f 2020-03-25 stsp continue;
1753 f298ae0f 2020-03-25 stsp }
1754 f298ae0f 2020-03-25 stsp goto done;
1755 f298ae0f 2020-03-25 stsp }
1756 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1757 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1758 f298ae0f 2020-03-25 stsp free(remote_refname);
1759 f298ae0f 2020-03-25 stsp free(remote_target);
1760 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1761 39c64a6a 2020-03-18 stsp if (error)
1762 d9b4d0c0 2020-03-18 stsp goto done;
1763 4ba14133 2020-03-20 stsp }
1764 4ba14133 2020-03-20 stsp if (pe == NULL) {
1765 4ba14133 2020-03-20 stsp /*
1766 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1767 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1768 4ba14133 2020-03-20 stsp * which could be fetched instead.
1769 4ba14133 2020-03-20 stsp */
1770 62d463ca 2020-10-20 naddy TAILQ_FOREACH(pe, &wanted_branches, entry) {
1771 4ba14133 2020-03-20 stsp const char *target = pe->path;
1772 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1773 d9b4d0c0 2020-03-18 stsp
1774 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1775 4ba14133 2020-03-20 stsp if (error) {
1776 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1777 4ba14133 2020-03-20 stsp error = NULL;
1778 4ba14133 2020-03-20 stsp continue;
1779 4ba14133 2020-03-20 stsp }
1780 4ba14133 2020-03-20 stsp goto done;
1781 4ba14133 2020-03-20 stsp }
1782 4ba14133 2020-03-20 stsp
1783 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1784 04d9a9ec 2020-09-24 stsp verbosity, repo);
1785 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1786 4ba14133 2020-03-20 stsp if (error)
1787 4ba14133 2020-03-20 stsp goto done;
1788 4ba14133 2020-03-20 stsp break;
1789 4ba14133 2020-03-20 stsp }
1790 659e7fbd 2020-03-20 stsp }
1791 659e7fbd 2020-03-20 stsp
1792 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1793 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1794 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1795 09838ffc 2020-03-18 stsp done:
1796 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1797 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1798 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1799 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1800 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1801 9c52365f 2020-03-21 stsp }
1802 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1803 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1804 1d0f4054 2021-06-17 stsp if (repo) {
1805 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
1806 1d0f4054 2021-06-17 stsp if (error == NULL)
1807 1d0f4054 2021-06-17 stsp error = close_err;
1808 1d0f4054 2021-06-17 stsp }
1809 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1810 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1811 d9b4d0c0 2020-03-18 stsp free(pe->data);
1812 d9b4d0c0 2020-03-18 stsp }
1813 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1814 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1815 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1816 d9b4d0c0 2020-03-18 stsp free(pe->data);
1817 d9b4d0c0 2020-03-18 stsp }
1818 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1819 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1820 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1821 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1822 09838ffc 2020-03-18 stsp free(proto);
1823 09838ffc 2020-03-18 stsp free(host);
1824 09838ffc 2020-03-18 stsp free(port);
1825 09838ffc 2020-03-18 stsp free(server_path);
1826 09838ffc 2020-03-18 stsp free(repo_name);
1827 bb64b798 2020-03-18 stsp free(default_destdir);
1828 b46f3e71 2020-03-18 stsp free(git_url);
1829 39c64a6a 2020-03-18 stsp return error;
1830 7848a0e1 2020-03-19 stsp }
1831 7848a0e1 2020-03-19 stsp
1832 7848a0e1 2020-03-19 stsp static const struct got_error *
1833 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1834 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1835 7848a0e1 2020-03-19 stsp {
1836 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1837 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1838 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1839 7848a0e1 2020-03-19 stsp
1840 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1841 7848a0e1 2020-03-19 stsp if (err)
1842 7848a0e1 2020-03-19 stsp goto done;
1843 7848a0e1 2020-03-19 stsp
1844 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1845 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1846 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1847 88609724 2020-03-21 stsp if (err)
1848 88609724 2020-03-21 stsp goto done;
1849 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1850 88609724 2020-03-21 stsp goto done;
1851 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1852 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1853 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1854 db6d8ad8 2020-03-21 stsp }
1855 db6d8ad8 2020-03-21 stsp goto done;
1856 db6d8ad8 2020-03-21 stsp }
1857 db6d8ad8 2020-03-21 stsp
1858 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1859 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1860 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1861 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1862 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1863 688f11b3 2020-03-21 stsp }
1864 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1865 7848a0e1 2020-03-19 stsp if (err)
1866 7848a0e1 2020-03-19 stsp goto done;
1867 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1868 e8a967e0 2020-03-21 stsp if (err)
1869 e8a967e0 2020-03-21 stsp goto done;
1870 7848a0e1 2020-03-19 stsp } else {
1871 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1872 7848a0e1 2020-03-19 stsp if (err)
1873 7848a0e1 2020-03-19 stsp goto done;
1874 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1875 6338a6a1 2020-03-21 stsp goto done;
1876 6338a6a1 2020-03-21 stsp
1877 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1878 6338a6a1 2020-03-21 stsp if (err)
1879 6338a6a1 2020-03-21 stsp goto done;
1880 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1881 6338a6a1 2020-03-21 stsp if (err)
1882 6338a6a1 2020-03-21 stsp goto done;
1883 7848a0e1 2020-03-19 stsp }
1884 6338a6a1 2020-03-21 stsp
1885 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1886 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1887 6338a6a1 2020-03-21 stsp new_id_str);
1888 7848a0e1 2020-03-19 stsp done:
1889 7848a0e1 2020-03-19 stsp free(old_id);
1890 7848a0e1 2020-03-19 stsp free(new_id_str);
1891 7848a0e1 2020-03-19 stsp return err;
1892 2ab43947 2020-03-18 stsp }
1893 f1bcca34 2020-03-25 stsp
1894 f1bcca34 2020-03-25 stsp static const struct got_error *
1895 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1896 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1897 f1bcca34 2020-03-25 stsp {
1898 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1899 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1900 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1901 f1bcca34 2020-03-25 stsp
1902 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1903 bcf34b0e 2020-03-26 stsp if (err) {
1904 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1905 bcf34b0e 2020-03-26 stsp return err;
1906 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1907 bcf34b0e 2020-03-26 stsp if (err)
1908 bcf34b0e 2020-03-26 stsp goto done;
1909 2ab43947 2020-03-18 stsp
1910 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1911 bcf34b0e 2020-03-26 stsp if (err)
1912 bcf34b0e 2020-03-26 stsp goto done;
1913 f1bcca34 2020-03-25 stsp
1914 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1915 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1916 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1917 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1918 bcf34b0e 2020-03-26 stsp } else {
1919 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1920 f1bcca34 2020-03-25 stsp
1921 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1922 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1923 bcf34b0e 2020-03-26 stsp goto done;
1924 bcf34b0e 2020-03-26 stsp
1925 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1926 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1927 bcf34b0e 2020-03-26 stsp if (err)
1928 bcf34b0e 2020-03-26 stsp goto done;
1929 bcf34b0e 2020-03-26 stsp
1930 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1931 bcf34b0e 2020-03-26 stsp if (err)
1932 bcf34b0e 2020-03-26 stsp goto done;
1933 bcf34b0e 2020-03-26 stsp
1934 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1935 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1936 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1937 bcf34b0e 2020-03-26 stsp
1938 bcf34b0e 2020-03-26 stsp }
1939 f1bcca34 2020-03-25 stsp done:
1940 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1941 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1942 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1943 bcf34b0e 2020-03-26 stsp err = unlock_err;
1944 bcf34b0e 2020-03-26 stsp }
1945 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1946 f1bcca34 2020-03-25 stsp return err;
1947 f1bcca34 2020-03-25 stsp }
1948 f1bcca34 2020-03-25 stsp
1949 2ab43947 2020-03-18 stsp __dead static void
1950 7848a0e1 2020-03-19 stsp usage_fetch(void)
1951 7848a0e1 2020-03-19 stsp {
1952 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1953 161728eb 2021-07-24 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1954 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1955 13f12b09 2020-03-21 stsp getprogname());
1956 7848a0e1 2020-03-19 stsp exit(1);
1957 7848a0e1 2020-03-19 stsp }
1958 7848a0e1 2020-03-19 stsp
1959 7848a0e1 2020-03-19 stsp static const struct got_error *
1960 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1961 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1962 f21ec2f0 2020-03-21 stsp {
1963 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1964 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1965 3789fd73 2020-03-26 stsp char *id_str = NULL;
1966 3789fd73 2020-03-26 stsp
1967 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1968 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1969 3789fd73 2020-03-26 stsp if (err)
1970 3789fd73 2020-03-26 stsp return err;
1971 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1972 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
1973 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1974 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1975 3789fd73 2020-03-26 stsp }
1976 3789fd73 2020-03-26 stsp } else {
1977 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1978 3789fd73 2020-03-26 stsp if (err)
1979 3789fd73 2020-03-26 stsp return err;
1980 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1981 3789fd73 2020-03-26 stsp if (err)
1982 3789fd73 2020-03-26 stsp goto done;
1983 3168e5da 2020-09-10 stsp
1984 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1985 3789fd73 2020-03-26 stsp if (err)
1986 3789fd73 2020-03-26 stsp goto done;
1987 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1988 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
1989 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1990 3789fd73 2020-03-26 stsp }
1991 3789fd73 2020-03-26 stsp }
1992 3789fd73 2020-03-26 stsp done:
1993 3789fd73 2020-03-26 stsp free(id);
1994 3789fd73 2020-03-26 stsp free(id_str);
1995 3789fd73 2020-03-26 stsp return NULL;
1996 3789fd73 2020-03-26 stsp }
1997 3789fd73 2020-03-26 stsp
1998 3789fd73 2020-03-26 stsp static const struct got_error *
1999 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
2000 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
2001 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
2002 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
2003 3789fd73 2020-03-26 stsp {
2004 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
2005 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
2006 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
2007 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
2008 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
2009 3789fd73 2020-03-26 stsp char *local_refname = NULL;
2010 f21ec2f0 2020-03-21 stsp
2011 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&my_refs);
2012 f21ec2f0 2020-03-21 stsp
2013 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2014 3789fd73 2020-03-26 stsp == -1)
2015 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
2016 3789fd73 2020-03-26 stsp
2017 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2018 f21ec2f0 2020-03-21 stsp if (err)
2019 3789fd73 2020-03-26 stsp goto done;
2020 f21ec2f0 2020-03-21 stsp
2021 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &my_refs, entry) {
2022 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
2023 1b796c3f 2021-09-11 stsp const char *their_refname;
2024 f21ec2f0 2020-03-21 stsp
2025 1b796c3f 2021-09-11 stsp if (remote->mirror_references) {
2026 1b796c3f 2021-09-11 stsp their_refname = refname;
2027 1b796c3f 2021-09-11 stsp } else {
2028 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
2029 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
2030 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
2031 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
2032 3789fd73 2020-03-26 stsp continue;
2033 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
2034 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
2035 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
2036 3789fd73 2020-03-26 stsp goto done;
2037 3789fd73 2020-03-26 stsp }
2038 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
2039 3789fd73 2020-03-26 stsp continue;
2040 f21ec2f0 2020-03-21 stsp
2041 1b796c3f 2021-09-11 stsp their_refname = local_refname;
2042 1b796c3f 2021-09-11 stsp }
2043 1b796c3f 2021-09-11 stsp
2044 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
2045 1b796c3f 2021-09-11 stsp if (strcmp(their_refname, pe->path) == 0)
2046 f21ec2f0 2020-03-21 stsp break;
2047 f21ec2f0 2020-03-21 stsp }
2048 f21ec2f0 2020-03-21 stsp if (pe != NULL)
2049 f21ec2f0 2020-03-21 stsp continue;
2050 f21ec2f0 2020-03-21 stsp
2051 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
2052 1b796c3f 2021-09-11 stsp if (strcmp(their_refname, pe->path) == 0)
2053 3789fd73 2020-03-26 stsp break;
2054 3789fd73 2020-03-26 stsp }
2055 3789fd73 2020-03-26 stsp if (pe != NULL)
2056 3789fd73 2020-03-26 stsp continue;
2057 f21ec2f0 2020-03-21 stsp
2058 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
2059 f21ec2f0 2020-03-21 stsp if (err)
2060 f21ec2f0 2020-03-21 stsp break;
2061 3789fd73 2020-03-26 stsp
2062 3789fd73 2020-03-26 stsp if (local_refname) {
2063 3789fd73 2020-03-26 stsp struct got_reference *ref;
2064 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
2065 3789fd73 2020-03-26 stsp if (err) {
2066 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
2067 3789fd73 2020-03-26 stsp break;
2068 3789fd73 2020-03-26 stsp free(local_refname);
2069 3789fd73 2020-03-26 stsp local_refname = NULL;
2070 3789fd73 2020-03-26 stsp continue;
2071 3789fd73 2020-03-26 stsp }
2072 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
2073 3789fd73 2020-03-26 stsp if (err)
2074 3789fd73 2020-03-26 stsp break;
2075 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
2076 3789fd73 2020-03-26 stsp got_ref_close(ref);
2077 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
2078 3789fd73 2020-03-26 stsp err = unlock_err;
2079 3789fd73 2020-03-26 stsp break;
2080 3789fd73 2020-03-26 stsp }
2081 3789fd73 2020-03-26 stsp
2082 3789fd73 2020-03-26 stsp free(local_refname);
2083 3789fd73 2020-03-26 stsp local_refname = NULL;
2084 6338a6a1 2020-03-21 stsp }
2085 f21ec2f0 2020-03-21 stsp }
2086 3789fd73 2020-03-26 stsp done:
2087 3789fd73 2020-03-26 stsp free(remote_namespace);
2088 3789fd73 2020-03-26 stsp free(local_refname);
2089 0e4002ca 2020-03-21 stsp return err;
2090 0e4002ca 2020-03-21 stsp }
2091 0e4002ca 2020-03-21 stsp
2092 0e4002ca 2020-03-21 stsp static const struct got_error *
2093 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
2094 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
2095 0e4002ca 2020-03-21 stsp {
2096 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
2097 0e4002ca 2020-03-21 stsp char *remote_refname;
2098 0e4002ca 2020-03-21 stsp struct got_reference *ref;
2099 0e4002ca 2020-03-21 stsp
2100 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
2101 0e4002ca 2020-03-21 stsp refname += 5;
2102 0e4002ca 2020-03-21 stsp
2103 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2104 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
2105 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
2106 f21ec2f0 2020-03-21 stsp
2107 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
2108 0e4002ca 2020-03-21 stsp if (err) {
2109 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
2110 0e4002ca 2020-03-21 stsp goto done;
2111 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
2112 0e4002ca 2020-03-21 stsp } else {
2113 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
2114 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2115 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
2116 9f142382 2020-03-21 stsp err = unlock_err;
2117 0e4002ca 2020-03-21 stsp got_ref_close(ref);
2118 0e4002ca 2020-03-21 stsp }
2119 0e4002ca 2020-03-21 stsp done:
2120 0e4002ca 2020-03-21 stsp free(remote_refname);
2121 161728eb 2021-07-24 stsp return err;
2122 161728eb 2021-07-24 stsp }
2123 161728eb 2021-07-24 stsp
2124 161728eb 2021-07-24 stsp static const struct got_error *
2125 161728eb 2021-07-24 stsp delete_ref(struct got_repository *repo, struct got_reference *ref)
2126 161728eb 2021-07-24 stsp {
2127 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2128 161728eb 2021-07-24 stsp struct got_object_id *id = NULL;
2129 161728eb 2021-07-24 stsp char *id_str = NULL;
2130 161728eb 2021-07-24 stsp const char *target;
2131 161728eb 2021-07-24 stsp
2132 161728eb 2021-07-24 stsp if (got_ref_is_symbolic(ref)) {
2133 161728eb 2021-07-24 stsp target = got_ref_get_symref_target(ref);
2134 161728eb 2021-07-24 stsp } else {
2135 161728eb 2021-07-24 stsp err = got_ref_resolve(&id, repo, ref);
2136 161728eb 2021-07-24 stsp if (err)
2137 161728eb 2021-07-24 stsp goto done;
2138 161728eb 2021-07-24 stsp err = got_object_id_str(&id_str, id);
2139 161728eb 2021-07-24 stsp if (err)
2140 161728eb 2021-07-24 stsp goto done;
2141 161728eb 2021-07-24 stsp target = id_str;
2142 161728eb 2021-07-24 stsp }
2143 161728eb 2021-07-24 stsp
2144 161728eb 2021-07-24 stsp err = got_ref_delete(ref, repo);
2145 161728eb 2021-07-24 stsp if (err)
2146 161728eb 2021-07-24 stsp goto done;
2147 161728eb 2021-07-24 stsp
2148 161728eb 2021-07-24 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2149 161728eb 2021-07-24 stsp done:
2150 161728eb 2021-07-24 stsp free(id);
2151 161728eb 2021-07-24 stsp free(id_str);
2152 f21ec2f0 2020-03-21 stsp return err;
2153 f21ec2f0 2020-03-21 stsp }
2154 f21ec2f0 2020-03-21 stsp
2155 f21ec2f0 2020-03-21 stsp static const struct got_error *
2156 161728eb 2021-07-24 stsp delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2157 161728eb 2021-07-24 stsp {
2158 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2159 161728eb 2021-07-24 stsp struct got_reflist_head refs;
2160 161728eb 2021-07-24 stsp struct got_reflist_entry *re;
2161 161728eb 2021-07-24 stsp char *prefix;
2162 161728eb 2021-07-24 stsp
2163 161728eb 2021-07-24 stsp TAILQ_INIT(&refs);
2164 161728eb 2021-07-24 stsp
2165 161728eb 2021-07-24 stsp if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2166 161728eb 2021-07-24 stsp err = got_error_from_errno("asprintf");
2167 161728eb 2021-07-24 stsp goto done;
2168 161728eb 2021-07-24 stsp }
2169 161728eb 2021-07-24 stsp err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2170 161728eb 2021-07-24 stsp if (err)
2171 161728eb 2021-07-24 stsp goto done;
2172 161728eb 2021-07-24 stsp
2173 161728eb 2021-07-24 stsp TAILQ_FOREACH(re, &refs, entry)
2174 161728eb 2021-07-24 stsp delete_ref(repo, re->ref);
2175 161728eb 2021-07-24 stsp done:
2176 161728eb 2021-07-24 stsp got_ref_list_free(&refs);
2177 161728eb 2021-07-24 stsp return err;
2178 161728eb 2021-07-24 stsp }
2179 161728eb 2021-07-24 stsp
2180 161728eb 2021-07-24 stsp static const struct got_error *
2181 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
2182 7848a0e1 2020-03-19 stsp {
2183 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
2184 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
2185 7848a0e1 2020-03-19 stsp const char *remote_name;
2186 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
2187 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
2188 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
2189 7848a0e1 2020-03-19 stsp int nremotes;
2190 7848a0e1 2020-03-19 stsp char *id_str = NULL;
2191 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
2192 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
2193 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2194 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2195 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
2196 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
2197 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
2198 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
2199 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
2200 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2201 161728eb 2021-07-24 stsp int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2202 7848a0e1 2020-03-19 stsp
2203 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
2204 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
2205 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
2206 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
2207 7848a0e1 2020-03-19 stsp
2208 161728eb 2021-07-24 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2209 7848a0e1 2020-03-19 stsp switch (ch) {
2210 659e7fbd 2020-03-20 stsp case 'a':
2211 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
2212 4ba14133 2020-03-20 stsp break;
2213 4ba14133 2020-03-20 stsp case 'b':
2214 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
2215 4ba14133 2020-03-20 stsp optarg, NULL);
2216 4ba14133 2020-03-20 stsp if (error)
2217 4ba14133 2020-03-20 stsp return error;
2218 41b0de12 2020-03-21 stsp break;
2219 f21ec2f0 2020-03-21 stsp case 'd':
2220 f21ec2f0 2020-03-21 stsp delete_refs = 1;
2221 f21ec2f0 2020-03-21 stsp break;
2222 41b0de12 2020-03-21 stsp case 'l':
2223 41b0de12 2020-03-21 stsp list_refs_only = 1;
2224 659e7fbd 2020-03-20 stsp break;
2225 7848a0e1 2020-03-19 stsp case 'r':
2226 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
2227 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2228 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
2229 7848a0e1 2020-03-19 stsp optarg);
2230 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
2231 7848a0e1 2020-03-19 stsp break;
2232 db6d8ad8 2020-03-21 stsp case 't':
2233 db6d8ad8 2020-03-21 stsp replace_tags = 1;
2234 db6d8ad8 2020-03-21 stsp break;
2235 7848a0e1 2020-03-19 stsp case 'v':
2236 7848a0e1 2020-03-19 stsp if (verbosity < 0)
2237 7848a0e1 2020-03-19 stsp verbosity = 0;
2238 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
2239 7848a0e1 2020-03-19 stsp verbosity++;
2240 7848a0e1 2020-03-19 stsp break;
2241 7848a0e1 2020-03-19 stsp case 'q':
2242 7848a0e1 2020-03-19 stsp verbosity = -1;
2243 7848a0e1 2020-03-19 stsp break;
2244 0e4002ca 2020-03-21 stsp case 'R':
2245 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
2246 0e4002ca 2020-03-21 stsp optarg, NULL);
2247 0e4002ca 2020-03-21 stsp if (error)
2248 0e4002ca 2020-03-21 stsp return error;
2249 0e4002ca 2020-03-21 stsp break;
2250 161728eb 2021-07-24 stsp case 'X':
2251 161728eb 2021-07-24 stsp delete_remote = 1;
2252 161728eb 2021-07-24 stsp break;
2253 7848a0e1 2020-03-19 stsp default:
2254 7848a0e1 2020-03-19 stsp usage_fetch();
2255 7848a0e1 2020-03-19 stsp break;
2256 7848a0e1 2020-03-19 stsp }
2257 7848a0e1 2020-03-19 stsp }
2258 7848a0e1 2020-03-19 stsp argc -= optind;
2259 7848a0e1 2020-03-19 stsp argv += optind;
2260 7848a0e1 2020-03-19 stsp
2261 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2262 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
2263 41b0de12 2020-03-21 stsp if (list_refs_only) {
2264 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2265 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
2266 ff69268e 2020-12-13 stsp if (fetch_all_branches)
2267 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
2268 f21ec2f0 2020-03-21 stsp if (delete_refs)
2269 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
2270 161728eb 2021-07-24 stsp if (delete_remote)
2271 161728eb 2021-07-24 stsp option_conflict('l', 'X');
2272 41b0de12 2020-03-21 stsp }
2273 161728eb 2021-07-24 stsp if (delete_remote) {
2274 161728eb 2021-07-24 stsp if (fetch_all_branches)
2275 161728eb 2021-07-24 stsp option_conflict('X', 'a');
2276 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_branches))
2277 161728eb 2021-07-24 stsp option_conflict('X', 'b');
2278 161728eb 2021-07-24 stsp if (delete_refs)
2279 161728eb 2021-07-24 stsp option_conflict('X', 'd');
2280 161728eb 2021-07-24 stsp if (replace_tags)
2281 161728eb 2021-07-24 stsp option_conflict('X', 't');
2282 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_refs))
2283 161728eb 2021-07-24 stsp option_conflict('X', 'R');
2284 161728eb 2021-07-24 stsp }
2285 161728eb 2021-07-24 stsp
2286 161728eb 2021-07-24 stsp if (argc == 0) {
2287 161728eb 2021-07-24 stsp if (delete_remote)
2288 161728eb 2021-07-24 stsp errx(1, "-X option requires a remote name");
2289 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2290 161728eb 2021-07-24 stsp } else if (argc == 1)
2291 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2292 7848a0e1 2020-03-19 stsp else
2293 7848a0e1 2020-03-19 stsp usage_fetch();
2294 7848a0e1 2020-03-19 stsp
2295 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2296 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2297 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2298 7848a0e1 2020-03-19 stsp goto done;
2299 7848a0e1 2020-03-19 stsp }
2300 7848a0e1 2020-03-19 stsp
2301 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2302 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2303 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2304 7848a0e1 2020-03-19 stsp goto done;
2305 7848a0e1 2020-03-19 stsp else
2306 7848a0e1 2020-03-19 stsp error = NULL;
2307 7848a0e1 2020-03-19 stsp if (worktree) {
2308 7848a0e1 2020-03-19 stsp repo_path =
2309 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2310 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2311 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2312 7848a0e1 2020-03-19 stsp if (error)
2313 7848a0e1 2020-03-19 stsp goto done;
2314 7848a0e1 2020-03-19 stsp } else {
2315 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2316 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2317 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2318 7848a0e1 2020-03-19 stsp goto done;
2319 7848a0e1 2020-03-19 stsp }
2320 7848a0e1 2020-03-19 stsp }
2321 7848a0e1 2020-03-19 stsp }
2322 7848a0e1 2020-03-19 stsp
2323 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2324 7848a0e1 2020-03-19 stsp if (error)
2325 7848a0e1 2020-03-19 stsp goto done;
2326 7848a0e1 2020-03-19 stsp
2327 161728eb 2021-07-24 stsp if (delete_remote) {
2328 161728eb 2021-07-24 stsp error = delete_refs_for_remote(repo, remote_name);
2329 161728eb 2021-07-24 stsp goto done; /* nothing else to do */
2330 161728eb 2021-07-24 stsp }
2331 161728eb 2021-07-24 stsp
2332 50b0790e 2020-09-11 stsp if (worktree) {
2333 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2334 50b0790e 2020-09-11 stsp if (worktree_conf) {
2335 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2336 50b0790e 2020-09-11 stsp worktree_conf);
2337 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2338 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2339 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2340 50b0790e 2020-09-11 stsp break;
2341 54eb00d5 2020-10-20 stsp }
2342 50b0790e 2020-09-11 stsp }
2343 50b0790e 2020-09-11 stsp }
2344 7848a0e1 2020-03-19 stsp }
2345 50b0790e 2020-09-11 stsp if (remote == NULL) {
2346 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2347 50b0790e 2020-09-11 stsp if (repo_conf) {
2348 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2349 50b0790e 2020-09-11 stsp repo_conf);
2350 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2351 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2352 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2353 50b0790e 2020-09-11 stsp break;
2354 54eb00d5 2020-10-20 stsp }
2355 50b0790e 2020-09-11 stsp }
2356 50b0790e 2020-09-11 stsp }
2357 50b0790e 2020-09-11 stsp }
2358 50b0790e 2020-09-11 stsp if (remote == NULL) {
2359 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2360 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2361 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2362 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2363 257add31 2020-09-09 stsp break;
2364 54eb00d5 2020-10-20 stsp }
2365 257add31 2020-09-09 stsp }
2366 7848a0e1 2020-03-19 stsp }
2367 50b0790e 2020-09-11 stsp if (remote == NULL) {
2368 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2369 50b0790e 2020-09-11 stsp goto done;
2370 b8adfa55 2020-09-25 stsp }
2371 b8adfa55 2020-09-25 stsp
2372 0c8b29c5 2021-01-05 stsp if (TAILQ_EMPTY(&wanted_branches)) {
2373 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches)
2374 0c8b29c5 2021-01-05 stsp fetch_all_branches = remote->fetch_all_branches;
2375 6480c871 2021-08-30 stsp for (i = 0; i < remote->nfetch_branches; i++) {
2376 b8adfa55 2020-09-25 stsp got_pathlist_append(&wanted_branches,
2377 6480c871 2021-08-30 stsp remote->fetch_branches[i], NULL);
2378 99495ddb 2021-01-10 stsp }
2379 99495ddb 2021-01-10 stsp }
2380 99495ddb 2021-01-10 stsp if (TAILQ_EMPTY(&wanted_refs)) {
2381 6480c871 2021-08-30 stsp for (i = 0; i < remote->nfetch_refs; i++) {
2382 99495ddb 2021-01-10 stsp got_pathlist_append(&wanted_refs,
2383 6480c871 2021-08-30 stsp remote->fetch_refs[i], NULL);
2384 b8adfa55 2020-09-25 stsp }
2385 50b0790e 2020-09-11 stsp }
2386 7848a0e1 2020-03-19 stsp
2387 5e5da8c4 2021-09-05 stsp error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2388 6480c871 2021-08-30 stsp &repo_name, remote->fetch_url);
2389 7848a0e1 2020-03-19 stsp if (error)
2390 7848a0e1 2020-03-19 stsp goto done;
2391 7848a0e1 2020-03-19 stsp
2392 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2393 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2394 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2395 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2396 7848a0e1 2020-03-19 stsp err(1, "pledge");
2397 7848a0e1 2020-03-19 stsp #endif
2398 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2399 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2400 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2401 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2402 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2403 7848a0e1 2020-03-19 stsp err(1, "pledge");
2404 7848a0e1 2020-03-19 stsp #endif
2405 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2406 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2407 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2408 7848a0e1 2020-03-19 stsp goto done;
2409 7848a0e1 2020-03-19 stsp } else {
2410 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2411 7848a0e1 2020-03-19 stsp goto done;
2412 7848a0e1 2020-03-19 stsp }
2413 7848a0e1 2020-03-19 stsp
2414 d65a88a2 2021-09-05 stsp error = got_dial_apply_unveil(proto);
2415 d65a88a2 2021-09-05 stsp if (error)
2416 d65a88a2 2021-09-05 stsp goto done;
2417 d65a88a2 2021-09-05 stsp
2418 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2419 7848a0e1 2020-03-19 stsp if (error)
2420 7848a0e1 2020-03-19 stsp goto done;
2421 f79e6490 2020-04-19 stsp
2422 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2423 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2424 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2425 7848a0e1 2020-03-19 stsp
2426 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2427 9c52365f 2020-03-21 stsp server_path, verbosity);
2428 7848a0e1 2020-03-19 stsp if (error)
2429 7848a0e1 2020-03-19 stsp goto done;
2430 7848a0e1 2020-03-19 stsp
2431 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2432 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2433 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2434 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2435 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2436 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2437 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2438 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2439 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2440 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2441 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2442 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2443 7848a0e1 2020-03-19 stsp if (error)
2444 7848a0e1 2020-03-19 stsp goto done;
2445 7848a0e1 2020-03-19 stsp
2446 41b0de12 2020-03-21 stsp if (list_refs_only) {
2447 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2448 41b0de12 2020-03-21 stsp goto done;
2449 41b0de12 2020-03-21 stsp }
2450 41b0de12 2020-03-21 stsp
2451 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2452 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2453 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2454 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2455 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2456 984065c8 2020-03-19 stsp if (error)
2457 984065c8 2020-03-19 stsp goto done;
2458 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2459 984065c8 2020-03-19 stsp free(id_str);
2460 984065c8 2020-03-19 stsp id_str = NULL;
2461 984065c8 2020-03-19 stsp }
2462 7848a0e1 2020-03-19 stsp
2463 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2464 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2465 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2466 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2467 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2468 7848a0e1 2020-03-19 stsp char *remote_refname;
2469 7848a0e1 2020-03-19 stsp
2470 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2471 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2472 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2473 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2474 0e4002ca 2020-03-21 stsp if (error)
2475 0e4002ca 2020-03-21 stsp goto done;
2476 0e4002ca 2020-03-21 stsp continue;
2477 0e4002ca 2020-03-21 stsp }
2478 0e4002ca 2020-03-21 stsp
2479 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2480 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2481 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2482 7848a0e1 2020-03-19 stsp if (error) {
2483 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2484 7848a0e1 2020-03-19 stsp goto done;
2485 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2486 6338a6a1 2020-03-21 stsp repo);
2487 7848a0e1 2020-03-19 stsp if (error)
2488 7848a0e1 2020-03-19 stsp goto done;
2489 7848a0e1 2020-03-19 stsp } else {
2490 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2491 db6d8ad8 2020-03-21 stsp verbosity, repo);
2492 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2493 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2494 9f142382 2020-03-21 stsp error = unlock_err;
2495 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2496 7848a0e1 2020-03-19 stsp if (error)
2497 7848a0e1 2020-03-19 stsp goto done;
2498 7848a0e1 2020-03-19 stsp }
2499 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2500 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2501 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2502 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2503 7848a0e1 2020-03-19 stsp goto done;
2504 7848a0e1 2020-03-19 stsp }
2505 7848a0e1 2020-03-19 stsp
2506 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2507 7848a0e1 2020-03-19 stsp if (error) {
2508 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2509 7848a0e1 2020-03-19 stsp goto done;
2510 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2511 688f11b3 2020-03-21 stsp verbosity, repo);
2512 7848a0e1 2020-03-19 stsp if (error)
2513 7848a0e1 2020-03-19 stsp goto done;
2514 7848a0e1 2020-03-19 stsp } else {
2515 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2516 db6d8ad8 2020-03-21 stsp verbosity, repo);
2517 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2518 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2519 9f142382 2020-03-21 stsp error = unlock_err;
2520 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2521 7848a0e1 2020-03-19 stsp if (error)
2522 7848a0e1 2020-03-19 stsp goto done;
2523 7848a0e1 2020-03-19 stsp }
2524 2ec30c80 2020-03-20 stsp
2525 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2526 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2527 2ec30c80 2020-03-20 stsp if (error) {
2528 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2529 2ec30c80 2020-03-20 stsp goto done;
2530 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2531 6338a6a1 2020-03-21 stsp repo);
2532 2ec30c80 2020-03-20 stsp if (error)
2533 2ec30c80 2020-03-20 stsp goto done;
2534 9f142382 2020-03-21 stsp } else {
2535 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2536 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2537 9f142382 2020-03-21 stsp error = unlock_err;
2538 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2539 9f142382 2020-03-21 stsp }
2540 7848a0e1 2020-03-19 stsp }
2541 7848a0e1 2020-03-19 stsp }
2542 f1bcca34 2020-03-25 stsp if (delete_refs) {
2543 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2544 3789fd73 2020-03-26 stsp verbosity, repo);
2545 f1bcca34 2020-03-25 stsp if (error)
2546 f1bcca34 2020-03-25 stsp goto done;
2547 f1bcca34 2020-03-25 stsp }
2548 f1bcca34 2020-03-25 stsp
2549 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2550 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2551 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2552 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2553 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2554 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2555 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2556 f1bcca34 2020-03-25 stsp
2557 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2558 f1bcca34 2020-03-25 stsp continue;
2559 f1bcca34 2020-03-25 stsp
2560 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2561 f1bcca34 2020-03-25 stsp continue;
2562 f1bcca34 2020-03-25 stsp
2563 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2564 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2565 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2566 f1bcca34 2020-03-25 stsp goto done;
2567 f1bcca34 2020-03-25 stsp }
2568 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2569 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2570 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2571 f1bcca34 2020-03-25 stsp free(remote_refname);
2572 f1bcca34 2020-03-25 stsp goto done;
2573 f1bcca34 2020-03-25 stsp }
2574 f1bcca34 2020-03-25 stsp
2575 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2576 f1bcca34 2020-03-25 stsp 0);
2577 f1bcca34 2020-03-25 stsp if (error) {
2578 f1bcca34 2020-03-25 stsp free(remote_refname);
2579 f1bcca34 2020-03-25 stsp free(remote_target);
2580 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2581 f1bcca34 2020-03-25 stsp error = NULL;
2582 f1bcca34 2020-03-25 stsp continue;
2583 f1bcca34 2020-03-25 stsp }
2584 f1bcca34 2020-03-25 stsp goto done;
2585 f1bcca34 2020-03-25 stsp }
2586 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2587 f1bcca34 2020-03-25 stsp verbosity, repo);
2588 f1bcca34 2020-03-25 stsp free(remote_refname);
2589 f1bcca34 2020-03-25 stsp free(remote_target);
2590 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2591 f1bcca34 2020-03-25 stsp if (error)
2592 f1bcca34 2020-03-25 stsp goto done;
2593 f1bcca34 2020-03-25 stsp }
2594 f1bcca34 2020-03-25 stsp }
2595 7848a0e1 2020-03-19 stsp done:
2596 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2597 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2598 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2599 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2600 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2601 9c52365f 2020-03-21 stsp }
2602 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2603 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2604 1d0f4054 2021-06-17 stsp if (repo) {
2605 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2606 1d0f4054 2021-06-17 stsp if (error == NULL)
2607 1d0f4054 2021-06-17 stsp error = close_err;
2608 1d0f4054 2021-06-17 stsp }
2609 7848a0e1 2020-03-19 stsp if (worktree)
2610 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2611 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2612 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2613 7848a0e1 2020-03-19 stsp free(pe->data);
2614 7848a0e1 2020-03-19 stsp }
2615 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2616 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2617 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2618 7848a0e1 2020-03-19 stsp free(pe->data);
2619 7848a0e1 2020-03-19 stsp }
2620 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2621 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2622 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2623 7848a0e1 2020-03-19 stsp free(id_str);
2624 7848a0e1 2020-03-19 stsp free(cwd);
2625 7848a0e1 2020-03-19 stsp free(repo_path);
2626 7848a0e1 2020-03-19 stsp free(pack_hash);
2627 7848a0e1 2020-03-19 stsp free(proto);
2628 7848a0e1 2020-03-19 stsp free(host);
2629 7848a0e1 2020-03-19 stsp free(port);
2630 7848a0e1 2020-03-19 stsp free(server_path);
2631 7848a0e1 2020-03-19 stsp free(repo_name);
2632 7848a0e1 2020-03-19 stsp return error;
2633 7848a0e1 2020-03-19 stsp }
2634 7848a0e1 2020-03-19 stsp
2635 7848a0e1 2020-03-19 stsp
2636 7848a0e1 2020-03-19 stsp __dead static void
2637 2ab43947 2020-03-18 stsp usage_checkout(void)
2638 2ab43947 2020-03-18 stsp {
2639 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2640 4ad4a1ec 2021-09-13 tracey "[-p prefix] [-q] repository-path [worktree-path]\n",
2641 4ad4a1ec 2021-09-13 tracey getprogname());
2642 2ab43947 2020-03-18 stsp exit(1);
2643 2ab43947 2020-03-18 stsp }
2644 2ab43947 2020-03-18 stsp
2645 2ab43947 2020-03-18 stsp static void
2646 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2647 2ab43947 2020-03-18 stsp {
2648 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2649 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2650 e6786710 2021-07-03 stsp "garbage-collected by Git or 'gotadmin cleanup'; making the "
2651 e6786710 2021-07-03 stsp "repository writable and running 'got update' will prevent this\n",
2652 2ab43947 2020-03-18 stsp getprogname());
2653 2ab43947 2020-03-18 stsp }
2654 2ab43947 2020-03-18 stsp
2655 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2656 2ab43947 2020-03-18 stsp const char *worktree_path;
2657 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2658 4ad4a1ec 2021-09-13 tracey int verbosity;
2659 2ab43947 2020-03-18 stsp };
2660 2ab43947 2020-03-18 stsp
2661 2ab43947 2020-03-18 stsp static const struct got_error *
2662 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2663 2ab43947 2020-03-18 stsp {
2664 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2665 2ab43947 2020-03-18 stsp
2666 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2667 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2668 2ab43947 2020-03-18 stsp return NULL;
2669 2ab43947 2020-03-18 stsp
2670 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2671 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2672 2ab43947 2020-03-18 stsp return NULL;
2673 2ab43947 2020-03-18 stsp }
2674 2ab43947 2020-03-18 stsp
2675 2ab43947 2020-03-18 stsp while (path[0] == '/')
2676 2ab43947 2020-03-18 stsp path++;
2677 2ab43947 2020-03-18 stsp
2678 4ad4a1ec 2021-09-13 tracey if (a->verbosity >= 0)
2679 4ad4a1ec 2021-09-13 tracey printf("%c %s/%s\n", status, a->worktree_path, path);
2680 4ad4a1ec 2021-09-13 tracey
2681 2ab43947 2020-03-18 stsp return NULL;
2682 2ab43947 2020-03-18 stsp }
2683 2ab43947 2020-03-18 stsp
2684 2ab43947 2020-03-18 stsp static const struct got_error *
2685 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2686 2ab43947 2020-03-18 stsp {
2687 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2688 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2689 2ab43947 2020-03-18 stsp return NULL;
2690 2ab43947 2020-03-18 stsp }
2691 2ab43947 2020-03-18 stsp
2692 2ab43947 2020-03-18 stsp static const struct got_error *
2693 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2694 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2695 2ab43947 2020-03-18 stsp struct got_repository *repo)
2696 2ab43947 2020-03-18 stsp {
2697 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2698 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2699 2ab43947 2020-03-18 stsp
2700 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2701 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2702 2ab43947 2020-03-18 stsp if (err)
2703 2ab43947 2020-03-18 stsp return err;
2704 2ab43947 2020-03-18 stsp
2705 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2706 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2707 2ab43947 2020-03-18 stsp
2708 2ab43947 2020-03-18 stsp /*
2709 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2710 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2711 2ab43947 2020-03-18 stsp *
2712 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2713 2ab43947 2020-03-18 stsp *
2714 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2715 2ab43947 2020-03-18 stsp * \ /
2716 2ab43947 2020-03-18 stsp * C E
2717 2ab43947 2020-03-18 stsp * \ /
2718 2ab43947 2020-03-18 stsp * B (yca)
2719 2ab43947 2020-03-18 stsp * |
2720 2ab43947 2020-03-18 stsp * A
2721 2ab43947 2020-03-18 stsp *
2722 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2723 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2724 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2725 2ab43947 2020-03-18 stsp */
2726 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2727 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2728 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2729 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2730 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2731 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2732 2ab43947 2020-03-18 stsp
2733 2ab43947 2020-03-18 stsp free(yca_id);
2734 2ab43947 2020-03-18 stsp return NULL;
2735 2ab43947 2020-03-18 stsp }
2736 2ab43947 2020-03-18 stsp
2737 2ab43947 2020-03-18 stsp static const struct got_error *
2738 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2739 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2740 2ab43947 2020-03-18 stsp struct got_repository *repo)
2741 2ab43947 2020-03-18 stsp {
2742 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2743 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2744 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2745 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2746 2ab43947 2020-03-18 stsp
2747 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2748 2ab43947 2020-03-18 stsp if (err)
2749 2ab43947 2020-03-18 stsp goto done;
2750 2ab43947 2020-03-18 stsp
2751 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2752 2ab43947 2020-03-18 stsp is_same_branch = 1;
2753 2ab43947 2020-03-18 stsp goto done;
2754 2ab43947 2020-03-18 stsp }
2755 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2756 2ab43947 2020-03-18 stsp is_same_branch = 1;
2757 2ab43947 2020-03-18 stsp goto done;
2758 2ab43947 2020-03-18 stsp }
2759 2ab43947 2020-03-18 stsp
2760 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2761 2ab43947 2020-03-18 stsp if (err)
2762 2ab43947 2020-03-18 stsp goto done;
2763 2ab43947 2020-03-18 stsp
2764 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2765 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2766 2ab43947 2020-03-18 stsp if (err)
2767 2ab43947 2020-03-18 stsp goto done;
2768 2ab43947 2020-03-18 stsp
2769 2ab43947 2020-03-18 stsp for (;;) {
2770 2ab43947 2020-03-18 stsp struct got_object_id *id;
2771 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2772 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2773 2ab43947 2020-03-18 stsp if (err) {
2774 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2775 2ab43947 2020-03-18 stsp err = NULL;
2776 2ab43947 2020-03-18 stsp break;
2777 2ab43947 2020-03-18 stsp }
2778 2ab43947 2020-03-18 stsp
2779 2ab43947 2020-03-18 stsp if (id) {
2780 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2781 2ab43947 2020-03-18 stsp break;
2782 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2783 2ab43947 2020-03-18 stsp is_same_branch = 1;
2784 2ab43947 2020-03-18 stsp break;
2785 2ab43947 2020-03-18 stsp }
2786 2ab43947 2020-03-18 stsp }
2787 2ab43947 2020-03-18 stsp }
2788 2ab43947 2020-03-18 stsp done:
2789 2ab43947 2020-03-18 stsp if (graph)
2790 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2791 2ab43947 2020-03-18 stsp free(head_commit_id);
2792 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2793 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2794 2ab43947 2020-03-18 stsp return err;
2795 a367ff0f 2019-05-14 stsp }
2796 8069f636 2019-01-12 stsp
2797 4ed7e80c 2018-05-20 stsp static const struct got_error *
2798 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2799 4b6c9460 2020-03-05 stsp {
2800 4b6c9460 2020-03-05 stsp static char msg[512];
2801 4b6c9460 2020-03-05 stsp const char *branch_name;
2802 4b6c9460 2020-03-05 stsp
2803 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2804 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2805 4b6c9460 2020-03-05 stsp else
2806 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2807 4b6c9460 2020-03-05 stsp
2808 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2809 4b6c9460 2020-03-05 stsp branch_name += 11;
2810 4b6c9460 2020-03-05 stsp
2811 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2812 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2813 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2814 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2815 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2816 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2817 4b6c9460 2020-03-05 stsp
2818 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2819 4b6c9460 2020-03-05 stsp }
2820 4b6c9460 2020-03-05 stsp
2821 4b6c9460 2020-03-05 stsp static const struct got_error *
2822 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2823 c09a553d 2018-03-12 stsp {
2824 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2825 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2826 08e5873e 2021-09-14 stsp struct got_reference *head_ref = NULL, *ref = NULL;
2827 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2828 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2829 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2830 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2831 08e5873e 2021-09-14 stsp const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2832 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2833 08e5873e 2021-09-14 stsp struct got_object_id *commit_id = NULL;
2834 f0207f6a 2020-10-19 stsp char *cwd = NULL;
2835 4ad4a1ec 2021-09-13 tracey int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2836 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2837 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2838 f2ea84fa 2019-07-27 stsp
2839 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2840 c09a553d 2018-03-12 stsp
2841 4ad4a1ec 2021-09-13 tracey while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2842 0bb8a95e 2018-03-12 stsp switch (ch) {
2843 08573d5b 2019-05-14 stsp case 'b':
2844 08573d5b 2019-05-14 stsp branch_name = optarg;
2845 08573d5b 2019-05-14 stsp break;
2846 8069f636 2019-01-12 stsp case 'c':
2847 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2848 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2849 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2850 bb51a5b4 2020-01-13 stsp break;
2851 bb51a5b4 2020-01-13 stsp case 'E':
2852 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2853 8069f636 2019-01-12 stsp break;
2854 0bb8a95e 2018-03-12 stsp case 'p':
2855 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2856 0bb8a95e 2018-03-12 stsp break;
2857 4ad4a1ec 2021-09-13 tracey case 'q':
2858 4ad4a1ec 2021-09-13 tracey verbosity = -1;
2859 4ad4a1ec 2021-09-13 tracey break;
2860 0bb8a95e 2018-03-12 stsp default:
2861 2deda0b9 2019-03-07 stsp usage_checkout();
2862 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2863 0bb8a95e 2018-03-12 stsp }
2864 0bb8a95e 2018-03-12 stsp }
2865 0bb8a95e 2018-03-12 stsp
2866 0bb8a95e 2018-03-12 stsp argc -= optind;
2867 0bb8a95e 2018-03-12 stsp argv += optind;
2868 0bb8a95e 2018-03-12 stsp
2869 6715a751 2018-03-16 stsp #ifndef PROFILE
2870 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2871 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2872 c09a553d 2018-03-12 stsp err(1, "pledge");
2873 6715a751 2018-03-16 stsp #endif
2874 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2875 c47340da 2020-09-20 stsp char *base, *dotgit;
2876 f0207f6a 2020-10-19 stsp const char *path;
2877 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2878 76089277 2018-04-01 stsp if (repo_path == NULL)
2879 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2880 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2881 76089277 2018-04-01 stsp if (cwd == NULL) {
2882 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2883 76089277 2018-04-01 stsp goto done;
2884 76089277 2018-04-01 stsp }
2885 c47340da 2020-09-20 stsp if (path_prefix[0])
2886 f0207f6a 2020-10-19 stsp path = path_prefix;
2887 c47340da 2020-09-20 stsp else
2888 f0207f6a 2020-10-19 stsp path = repo_path;
2889 f0207f6a 2020-10-19 stsp error = got_path_basename(&base, path);
2890 f0207f6a 2020-10-19 stsp if (error)
2891 c47340da 2020-09-20 stsp goto done;
2892 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2893 c09a553d 2018-03-12 stsp if (dotgit)
2894 c09a553d 2018-03-12 stsp *dotgit = '\0';
2895 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2896 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2897 f0207f6a 2020-10-19 stsp free(base);
2898 76089277 2018-04-01 stsp goto done;
2899 c09a553d 2018-03-12 stsp }
2900 f0207f6a 2020-10-19 stsp free(base);
2901 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2902 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2903 76089277 2018-04-01 stsp if (repo_path == NULL) {
2904 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2905 76089277 2018-04-01 stsp goto done;
2906 76089277 2018-04-01 stsp }
2907 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2908 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2909 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2910 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2911 b4b3a7dd 2019-07-22 stsp argv[1]);
2912 b4b3a7dd 2019-07-22 stsp goto done;
2913 b4b3a7dd 2019-07-22 stsp }
2914 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2915 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2916 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2917 b4b3a7dd 2019-07-22 stsp goto done;
2918 b4b3a7dd 2019-07-22 stsp }
2919 76089277 2018-04-01 stsp }
2920 c09a553d 2018-03-12 stsp } else
2921 c09a553d 2018-03-12 stsp usage_checkout();
2922 c09a553d 2018-03-12 stsp
2923 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2924 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2925 13bfb272 2019-05-10 jcs
2926 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2927 c09a553d 2018-03-12 stsp if (error != NULL)
2928 c02c541e 2019-03-29 stsp goto done;
2929 c02c541e 2019-03-29 stsp
2930 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2931 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2932 c530dc23 2019-07-23 stsp if (error) {
2933 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2934 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2935 c530dc23 2019-07-23 stsp goto done;
2936 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2937 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2938 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2939 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2940 c530dc23 2019-07-23 stsp goto done;
2941 c530dc23 2019-07-23 stsp }
2942 c530dc23 2019-07-23 stsp }
2943 c530dc23 2019-07-23 stsp
2944 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2945 c02c541e 2019-03-29 stsp if (error)
2946 c09a553d 2018-03-12 stsp goto done;
2947 8069f636 2019-01-12 stsp
2948 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2949 c09a553d 2018-03-12 stsp if (error != NULL)
2950 c09a553d 2018-03-12 stsp goto done;
2951 c09a553d 2018-03-12 stsp
2952 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2953 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2954 c09a553d 2018-03-12 stsp goto done;
2955 c09a553d 2018-03-12 stsp
2956 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2957 c09a553d 2018-03-12 stsp if (error != NULL)
2958 c09a553d 2018-03-12 stsp goto done;
2959 c09a553d 2018-03-12 stsp
2960 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2961 e5dc7198 2018-12-29 stsp path_prefix);
2962 e5dc7198 2018-12-29 stsp if (error != NULL)
2963 e5dc7198 2018-12-29 stsp goto done;
2964 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2965 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2966 49520a32 2018-12-29 stsp goto done;
2967 49520a32 2018-12-29 stsp }
2968 49520a32 2018-12-29 stsp
2969 8069f636 2019-01-12 stsp if (commit_id_str) {
2970 84de9106 2020-12-26 stsp struct got_reflist_head refs;
2971 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
2972 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2973 84de9106 2020-12-26 stsp NULL);
2974 84de9106 2020-12-26 stsp if (error)
2975 84de9106 2020-12-26 stsp goto done;
2976 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2977 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2978 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
2979 30837e32 2019-07-25 stsp if (error)
2980 8069f636 2019-01-12 stsp goto done;
2981 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2982 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2983 8069f636 2019-01-12 stsp if (error != NULL) {
2984 8069f636 2019-01-12 stsp free(commit_id);
2985 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2986 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2987 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2988 4b6c9460 2020-03-05 stsp }
2989 8069f636 2019-01-12 stsp goto done;
2990 8069f636 2019-01-12 stsp }
2991 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2992 4b6c9460 2020-03-05 stsp if (error) {
2993 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2994 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2995 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2996 4b6c9460 2020-03-05 stsp }
2997 45d344f6 2019-05-14 stsp goto done;
2998 4b6c9460 2020-03-05 stsp }
2999 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3000 8069f636 2019-01-12 stsp commit_id);
3001 8069f636 2019-01-12 stsp if (error)
3002 8069f636 2019-01-12 stsp goto done;
3003 08e5873e 2021-09-14 stsp /* Expand potentially abbreviated commit ID string. */
3004 08e5873e 2021-09-14 stsp free(commit_id_str);
3005 08e5873e 2021-09-14 stsp error = got_object_id_str(&commit_id_str, commit_id);
3006 08e5873e 2021-09-14 stsp if (error)
3007 08e5873e 2021-09-14 stsp goto done;
3008 08e5873e 2021-09-14 stsp } else {
3009 08e5873e 2021-09-14 stsp commit_id = got_object_id_dup(
3010 08e5873e 2021-09-14 stsp got_worktree_get_base_commit_id(worktree));
3011 08e5873e 2021-09-14 stsp if (commit_id == NULL) {
3012 08e5873e 2021-09-14 stsp error = got_error_from_errno("got_object_id_dup");
3013 08e5873e 2021-09-14 stsp goto done;
3014 08e5873e 2021-09-14 stsp }
3015 08e5873e 2021-09-14 stsp error = got_object_id_str(&commit_id_str, commit_id);
3016 08e5873e 2021-09-14 stsp if (error)
3017 08e5873e 2021-09-14 stsp goto done;
3018 8069f636 2019-01-12 stsp }
3019 8069f636 2019-01-12 stsp
3020 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
3021 f2ea84fa 2019-07-27 stsp if (error)
3022 f2ea84fa 2019-07-27 stsp goto done;
3023 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
3024 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
3025 4ad4a1ec 2021-09-13 tracey cpa.verbosity = verbosity;
3026 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3027 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
3028 c09a553d 2018-03-12 stsp if (error != NULL)
3029 c09a553d 2018-03-12 stsp goto done;
3030 c09a553d 2018-03-12 stsp
3031 08e5873e 2021-09-14 stsp if (got_ref_is_symbolic(head_ref)) {
3032 08e5873e 2021-09-14 stsp error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3033 08e5873e 2021-09-14 stsp if (error)
3034 08e5873e 2021-09-14 stsp goto done;
3035 08e5873e 2021-09-14 stsp refname = got_ref_get_name(ref);
3036 08e5873e 2021-09-14 stsp } else
3037 08e5873e 2021-09-14 stsp refname = got_ref_get_name(head_ref);
3038 08e5873e 2021-09-14 stsp printf("Checked out %s: %s\n", refname, commit_id_str);
3039 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
3040 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
3041 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
3042 c09a553d 2018-03-12 stsp done:
3043 08e5873e 2021-09-14 stsp if (head_ref)
3044 08e5873e 2021-09-14 stsp got_ref_close(head_ref);
3045 08e5873e 2021-09-14 stsp if (ref)
3046 08e5873e 2021-09-14 stsp got_ref_close(ref);
3047 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3048 8069f636 2019-01-12 stsp free(commit_id_str);
3049 08e5873e 2021-09-14 stsp free(commit_id);
3050 76089277 2018-04-01 stsp free(repo_path);
3051 507dc3bb 2018-12-29 stsp free(worktree_path);
3052 c47340da 2020-09-20 stsp free(cwd);
3053 507dc3bb 2018-12-29 stsp return error;
3054 9627c110 2020-04-18 stsp }
3055 9627c110 2020-04-18 stsp
3056 9627c110 2020-04-18 stsp struct got_update_progress_arg {
3057 9627c110 2020-04-18 stsp int did_something;
3058 9627c110 2020-04-18 stsp int conflicts;
3059 9627c110 2020-04-18 stsp int obstructed;
3060 9627c110 2020-04-18 stsp int not_updated;
3061 4ad4a1ec 2021-09-13 tracey int verbosity;
3062 9627c110 2020-04-18 stsp };
3063 9627c110 2020-04-18 stsp
3064 9627c110 2020-04-18 stsp void
3065 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
3066 9627c110 2020-04-18 stsp {
3067 9627c110 2020-04-18 stsp if (!upa->did_something)
3068 9627c110 2020-04-18 stsp return;
3069 9627c110 2020-04-18 stsp
3070 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
3071 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
3072 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
3073 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
3074 9627c110 2020-04-18 stsp upa->obstructed);
3075 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
3076 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
3077 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
3078 507dc3bb 2018-12-29 stsp }
3079 507dc3bb 2018-12-29 stsp
3080 507dc3bb 2018-12-29 stsp __dead static void
3081 507dc3bb 2018-12-29 stsp usage_update(void)
3082 507dc3bb 2018-12-29 stsp {
3083 4ad4a1ec 2021-09-13 tracey fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3084 4ad4a1ec 2021-09-13 tracey "[path ...]\n",
3085 507dc3bb 2018-12-29 stsp getprogname());
3086 507dc3bb 2018-12-29 stsp exit(1);
3087 507dc3bb 2018-12-29 stsp }
3088 507dc3bb 2018-12-29 stsp
3089 1ee397ad 2019-07-12 stsp static const struct got_error *
3090 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
3091 507dc3bb 2018-12-29 stsp {
3092 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
3093 784955db 2019-01-12 stsp
3094 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
3095 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
3096 1ee397ad 2019-07-12 stsp return NULL;
3097 507dc3bb 2018-12-29 stsp
3098 9627c110 2020-04-18 stsp upa->did_something = 1;
3099 a484d721 2019-06-10 stsp
3100 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
3101 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
3102 1ee397ad 2019-07-12 stsp return NULL;
3103 a484d721 2019-06-10 stsp
3104 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
3105 9627c110 2020-04-18 stsp upa->conflicts++;
3106 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
3107 9627c110 2020-04-18 stsp upa->obstructed++;
3108 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
3109 9627c110 2020-04-18 stsp upa->not_updated++;
3110 9627c110 2020-04-18 stsp
3111 507dc3bb 2018-12-29 stsp while (path[0] == '/')
3112 507dc3bb 2018-12-29 stsp path++;
3113 4ad4a1ec 2021-09-13 tracey if (upa->verbosity >= 0)
3114 4ad4a1ec 2021-09-13 tracey printf("%c %s\n", status, path);
3115 4ad4a1ec 2021-09-13 tracey
3116 1ee397ad 2019-07-12 stsp return NULL;
3117 be7061eb 2018-12-30 stsp }
3118 be7061eb 2018-12-30 stsp
3119 be7061eb 2018-12-30 stsp static const struct got_error *
3120 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
3121 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
3122 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
3123 a1fb16d8 2019-05-24 stsp {
3124 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
3125 a1fb16d8 2019-05-24 stsp char *base_id_str;
3126 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
3127 a1fb16d8 2019-05-24 stsp
3128 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
3129 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
3130 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
3131 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
3132 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
3133 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
3134 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
3135 a1fb16d8 2019-05-24 stsp }
3136 a1fb16d8 2019-05-24 stsp
3137 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
3138 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3139 a1fb16d8 2019-05-24 stsp if (err) {
3140 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
3141 a1fb16d8 2019-05-24 stsp return err;
3142 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
3143 a1fb16d8 2019-05-24 stsp }
3144 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
3145 a1fb16d8 2019-05-24 stsp return NULL;
3146 a1fb16d8 2019-05-24 stsp
3147 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
3148 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
3149 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
3150 a1fb16d8 2019-05-24 stsp if (err)
3151 a1fb16d8 2019-05-24 stsp return err;
3152 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
3153 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3154 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
3155 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3156 0ebf8283 2019-07-24 stsp return NULL;
3157 0ebf8283 2019-07-24 stsp }
3158 0ebf8283 2019-07-24 stsp
3159 0ebf8283 2019-07-24 stsp static const struct got_error *
3160 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3161 0ebf8283 2019-07-24 stsp {
3162 0ebf8283 2019-07-24 stsp const struct got_error *err;
3163 0ebf8283 2019-07-24 stsp int in_progress;
3164 0ebf8283 2019-07-24 stsp
3165 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
3166 0ebf8283 2019-07-24 stsp if (err)
3167 0ebf8283 2019-07-24 stsp return err;
3168 0ebf8283 2019-07-24 stsp if (in_progress)
3169 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
3170 0ebf8283 2019-07-24 stsp
3171 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
3172 0ebf8283 2019-07-24 stsp if (err)
3173 0ebf8283 2019-07-24 stsp return err;
3174 0ebf8283 2019-07-24 stsp if (in_progress)
3175 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
3176 0ebf8283 2019-07-24 stsp
3177 a1fb16d8 2019-05-24 stsp return NULL;
3178 a5edda0a 2019-07-27 stsp }
3179 a5edda0a 2019-07-27 stsp
3180 a5edda0a 2019-07-27 stsp static const struct got_error *
3181 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3182 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
3183 a5edda0a 2019-07-27 stsp {
3184 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
3185 a5edda0a 2019-07-27 stsp char *path;
3186 a5edda0a 2019-07-27 stsp int i;
3187 a5edda0a 2019-07-27 stsp
3188 a5edda0a 2019-07-27 stsp if (argc == 0) {
3189 a5edda0a 2019-07-27 stsp path = strdup("");
3190 a5edda0a 2019-07-27 stsp if (path == NULL)
3191 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
3192 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
3193 a5edda0a 2019-07-27 stsp }
3194 a5edda0a 2019-07-27 stsp
3195 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
3196 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
3197 a5edda0a 2019-07-27 stsp if (err)
3198 a5edda0a 2019-07-27 stsp break;
3199 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
3200 a5edda0a 2019-07-27 stsp if (err) {
3201 a5edda0a 2019-07-27 stsp free(path);
3202 a5edda0a 2019-07-27 stsp break;
3203 a5edda0a 2019-07-27 stsp }
3204 a5edda0a 2019-07-27 stsp }
3205 a5edda0a 2019-07-27 stsp
3206 a5edda0a 2019-07-27 stsp return err;
3207 a1fb16d8 2019-05-24 stsp }
3208 a1fb16d8 2019-05-24 stsp
3209 a1fb16d8 2019-05-24 stsp static const struct got_error *
3210 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
3211 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
3212 fa51e947 2020-03-27 stsp {
3213 fa51e947 2020-03-27 stsp const struct got_error *err;
3214 fa51e947 2020-03-27 stsp struct got_repository *repo;
3215 fa51e947 2020-03-27 stsp static char msg[512];
3216 fa51e947 2020-03-27 stsp
3217 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
3218 fa51e947 2020-03-27 stsp if (err)
3219 fa51e947 2020-03-27 stsp return orig_err;
3220 fa51e947 2020-03-27 stsp
3221 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
3222 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
3223 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
3224 fa51e947 2020-03-27 stsp "'got checkout'.\n"
3225 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
3226 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3227 fa51e947 2020-03-27 stsp got_repo_close(repo);
3228 fa51e947 2020-03-27 stsp return err;
3229 fa51e947 2020-03-27 stsp }
3230 fa51e947 2020-03-27 stsp
3231 fa51e947 2020-03-27 stsp static const struct got_error *
3232 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
3233 507dc3bb 2018-12-29 stsp {
3234 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
3235 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
3236 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
3237 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
3238 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
3239 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
3240 024e9686 2019-05-14 stsp const char *branch_name = NULL;
3241 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
3242 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
3243 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
3244 4ad4a1ec 2021-09-13 tracey int ch, verbosity = 0;
3245 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
3246 507dc3bb 2018-12-29 stsp
3247 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
3248 f2ea84fa 2019-07-27 stsp
3249 4ad4a1ec 2021-09-13 tracey while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3250 507dc3bb 2018-12-29 stsp switch (ch) {
3251 024e9686 2019-05-14 stsp case 'b':
3252 024e9686 2019-05-14 stsp branch_name = optarg;
3253 024e9686 2019-05-14 stsp break;
3254 507dc3bb 2018-12-29 stsp case 'c':
3255 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
3256 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
3257 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
3258 4ad4a1ec 2021-09-13 tracey break;
3259 4ad4a1ec 2021-09-13 tracey case 'q':
3260 4ad4a1ec 2021-09-13 tracey verbosity = -1;
3261 507dc3bb 2018-12-29 stsp break;
3262 507dc3bb 2018-12-29 stsp default:
3263 2deda0b9 2019-03-07 stsp usage_update();
3264 507dc3bb 2018-12-29 stsp /* NOTREACHED */
3265 507dc3bb 2018-12-29 stsp }
3266 507dc3bb 2018-12-29 stsp }
3267 507dc3bb 2018-12-29 stsp
3268 507dc3bb 2018-12-29 stsp argc -= optind;
3269 507dc3bb 2018-12-29 stsp argv += optind;
3270 507dc3bb 2018-12-29 stsp
3271 507dc3bb 2018-12-29 stsp #ifndef PROFILE
3272 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3273 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
3274 507dc3bb 2018-12-29 stsp err(1, "pledge");
3275 507dc3bb 2018-12-29 stsp #endif
3276 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
3277 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
3278 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3279 c4cdcb68 2019-04-03 stsp goto done;
3280 c4cdcb68 2019-04-03 stsp }
3281 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
3282 fa51e947 2020-03-27 stsp if (error) {
3283 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3284 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
3285 fa51e947 2020-03-27 stsp worktree_path);
3286 7d5807f4 2019-07-11 stsp goto done;
3287 fa51e947 2020-03-27 stsp }
3288 7d5807f4 2019-07-11 stsp
3289 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
3290 f2ea84fa 2019-07-27 stsp if (error)
3291 f2ea84fa 2019-07-27 stsp goto done;
3292 507dc3bb 2018-12-29 stsp
3293 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3294 c9956ddf 2019-09-08 stsp NULL);
3295 507dc3bb 2018-12-29 stsp if (error != NULL)
3296 507dc3bb 2018-12-29 stsp goto done;
3297 507dc3bb 2018-12-29 stsp
3298 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3299 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3300 087fb88c 2019-08-04 stsp if (error)
3301 087fb88c 2019-08-04 stsp goto done;
3302 087fb88c 2019-08-04 stsp
3303 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3304 0266afb7 2019-01-04 stsp if (error)
3305 0266afb7 2019-01-04 stsp goto done;
3306 0266afb7 2019-01-04 stsp
3307 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3308 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
3309 024e9686 2019-05-14 stsp if (error != NULL)
3310 024e9686 2019-05-14 stsp goto done;
3311 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
3312 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3313 9c4b8182 2019-01-02 stsp if (error != NULL)
3314 9c4b8182 2019-01-02 stsp goto done;
3315 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3316 507dc3bb 2018-12-29 stsp if (error != NULL)
3317 507dc3bb 2018-12-29 stsp goto done;
3318 507dc3bb 2018-12-29 stsp } else {
3319 84de9106 2020-12-26 stsp struct got_reflist_head refs;
3320 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3321 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3322 84de9106 2020-12-26 stsp NULL);
3323 84de9106 2020-12-26 stsp if (error)
3324 84de9106 2020-12-26 stsp goto done;
3325 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3326 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3327 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
3328 04f57cb3 2019-07-25 stsp free(commit_id_str);
3329 bb787f09 2019-07-25 stsp commit_id_str = NULL;
3330 30837e32 2019-07-25 stsp if (error)
3331 a297e751 2019-07-11 stsp goto done;
3332 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
3333 a297e751 2019-07-11 stsp if (error)
3334 507dc3bb 2018-12-29 stsp goto done;
3335 507dc3bb 2018-12-29 stsp }
3336 35c965b2 2018-12-29 stsp
3337 a1fb16d8 2019-05-24 stsp if (branch_name) {
3338 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
3339 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3340 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3341 f2ea84fa 2019-07-27 stsp continue;
3342 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3343 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3344 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3345 024e9686 2019-05-14 stsp goto done;
3346 024e9686 2019-05-14 stsp }
3347 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3348 024e9686 2019-05-14 stsp if (error)
3349 024e9686 2019-05-14 stsp goto done;
3350 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3351 3aef623b 2019-10-15 stsp repo);
3352 a367ff0f 2019-05-14 stsp free(head_commit_id);
3353 024e9686 2019-05-14 stsp if (error != NULL)
3354 024e9686 2019-05-14 stsp goto done;
3355 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3356 a367ff0f 2019-05-14 stsp if (error)
3357 a367ff0f 2019-05-14 stsp goto done;
3358 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3359 024e9686 2019-05-14 stsp if (error)
3360 024e9686 2019-05-14 stsp goto done;
3361 024e9686 2019-05-14 stsp } else {
3362 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3363 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3364 a367ff0f 2019-05-14 stsp if (error != NULL) {
3365 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3366 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3367 024e9686 2019-05-14 stsp goto done;
3368 a367ff0f 2019-05-14 stsp }
3369 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3370 a367ff0f 2019-05-14 stsp if (error)
3371 a367ff0f 2019-05-14 stsp goto done;
3372 024e9686 2019-05-14 stsp }
3373 507dc3bb 2018-12-29 stsp
3374 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3375 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3376 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3377 507dc3bb 2018-12-29 stsp commit_id);
3378 507dc3bb 2018-12-29 stsp if (error)
3379 507dc3bb 2018-12-29 stsp goto done;
3380 507dc3bb 2018-12-29 stsp }
3381 507dc3bb 2018-12-29 stsp
3382 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3383 4ad4a1ec 2021-09-13 tracey upa.verbosity = verbosity;
3384 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3385 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3386 507dc3bb 2018-12-29 stsp if (error != NULL)
3387 507dc3bb 2018-12-29 stsp goto done;
3388 9c4b8182 2019-01-02 stsp
3389 4f3c844b 2021-09-14 stsp if (upa.did_something) {
3390 4f3c844b 2021-09-14 stsp printf("Updated to %s: %s\n",
3391 4f3c844b 2021-09-14 stsp got_worktree_get_head_ref_name(worktree), commit_id_str);
3392 4f3c844b 2021-09-14 stsp } else
3393 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3394 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3395 507dc3bb 2018-12-29 stsp done:
3396 c09a553d 2018-03-12 stsp free(worktree_path);
3397 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3398 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3399 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3400 507dc3bb 2018-12-29 stsp free(commit_id);
3401 9c4b8182 2019-01-02 stsp free(commit_id_str);
3402 c09a553d 2018-03-12 stsp return error;
3403 c09a553d 2018-03-12 stsp }
3404 c09a553d 2018-03-12 stsp
3405 f42b1b34 2018-03-12 stsp static const struct got_error *
3406 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3407 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3408 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3409 5c860e29 2018-03-12 stsp {
3410 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3411 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3412 79109fed 2018-03-27 stsp
3413 44392932 2019-08-25 stsp if (blob_id1) {
3414 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3415 44392932 2019-08-25 stsp if (err)
3416 44392932 2019-08-25 stsp goto done;
3417 44392932 2019-08-25 stsp }
3418 79109fed 2018-03-27 stsp
3419 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3420 44392932 2019-08-25 stsp if (err)
3421 44392932 2019-08-25 stsp goto done;
3422 79109fed 2018-03-27 stsp
3423 44392932 2019-08-25 stsp while (path[0] == '/')
3424 44392932 2019-08-25 stsp path++;
3425 fe621944 2020-11-10 stsp err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3426 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, stdout);
3427 44392932 2019-08-25 stsp done:
3428 44392932 2019-08-25 stsp if (blob1)
3429 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3430 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3431 44392932 2019-08-25 stsp return err;
3432 44392932 2019-08-25 stsp }
3433 44392932 2019-08-25 stsp
3434 44392932 2019-08-25 stsp static const struct got_error *
3435 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3436 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3437 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3438 44392932 2019-08-25 stsp {
3439 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3440 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3441 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3442 44392932 2019-08-25 stsp
3443 44392932 2019-08-25 stsp if (tree_id1) {
3444 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3445 79109fed 2018-03-27 stsp if (err)
3446 44392932 2019-08-25 stsp goto done;
3447 79109fed 2018-03-27 stsp }
3448 79109fed 2018-03-27 stsp
3449 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3450 0f2b3dca 2018-12-22 stsp if (err)
3451 0f2b3dca 2018-12-22 stsp goto done;
3452 0f2b3dca 2018-12-22 stsp
3453 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3454 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3455 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
3456 aaa13589 2019-06-01 stsp arg.outfile = stdout;
3457 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
3458 fe621944 2020-11-10 stsp arg.nlines = 0;
3459 44392932 2019-08-25 stsp while (path[0] == '/')
3460 44392932 2019-08-25 stsp path++;
3461 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
3462 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3463 0f2b3dca 2018-12-22 stsp done:
3464 79109fed 2018-03-27 stsp if (tree1)
3465 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3466 366e0a5f 2019-10-10 stsp if (tree2)
3467 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3468 44392932 2019-08-25 stsp return err;
3469 44392932 2019-08-25 stsp }
3470 44392932 2019-08-25 stsp
3471 44392932 2019-08-25 stsp static const struct got_error *
3472 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3473 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3474 0208f208 2020-05-05 stsp {
3475 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3476 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3477 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3478 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3479 0208f208 2020-05-05 stsp
3480 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3481 0208f208 2020-05-05 stsp if (qid != NULL) {
3482 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3483 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3484 0208f208 2020-05-05 stsp qid->id);
3485 0208f208 2020-05-05 stsp if (err)
3486 0208f208 2020-05-05 stsp return err;
3487 0208f208 2020-05-05 stsp
3488 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3489 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3490 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3491 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3492 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3493 aa8b5dd0 2021-08-01 stsp }
3494 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3495 0208f208 2020-05-05 stsp
3496 0208f208 2020-05-05 stsp }
3497 0208f208 2020-05-05 stsp
3498 0208f208 2020-05-05 stsp if (tree_id1) {
3499 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3500 0208f208 2020-05-05 stsp if (err)
3501 0208f208 2020-05-05 stsp goto done;
3502 0208f208 2020-05-05 stsp }
3503 0208f208 2020-05-05 stsp
3504 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3505 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3506 0208f208 2020-05-05 stsp if (err)
3507 0208f208 2020-05-05 stsp goto done;
3508 0208f208 2020-05-05 stsp
3509 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3510 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3511 0208f208 2020-05-05 stsp done:
3512 0208f208 2020-05-05 stsp if (tree1)
3513 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3514 0208f208 2020-05-05 stsp if (tree2)
3515 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3516 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3517 0208f208 2020-05-05 stsp return err;
3518 0208f208 2020-05-05 stsp }
3519 0208f208 2020-05-05 stsp
3520 0208f208 2020-05-05 stsp static const struct got_error *
3521 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3522 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3523 44392932 2019-08-25 stsp {
3524 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3525 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3526 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3527 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3528 44392932 2019-08-25 stsp struct got_object_qid *qid;
3529 44392932 2019-08-25 stsp
3530 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3531 44392932 2019-08-25 stsp if (qid != NULL) {
3532 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3533 44392932 2019-08-25 stsp qid->id);
3534 44392932 2019-08-25 stsp if (err)
3535 44392932 2019-08-25 stsp return err;
3536 44392932 2019-08-25 stsp }
3537 44392932 2019-08-25 stsp
3538 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3539 44392932 2019-08-25 stsp int obj_type;
3540 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3541 44392932 2019-08-25 stsp if (err)
3542 44392932 2019-08-25 stsp goto done;
3543 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3544 44392932 2019-08-25 stsp if (err) {
3545 44392932 2019-08-25 stsp free(obj_id2);
3546 44392932 2019-08-25 stsp goto done;
3547 44392932 2019-08-25 stsp }
3548 44392932 2019-08-25 stsp if (pcommit) {
3549 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3550 44392932 2019-08-25 stsp qid->id, path);
3551 44392932 2019-08-25 stsp if (err) {
3552 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3553 2e8c69d1 2020-05-04 stsp free(obj_id2);
3554 2e8c69d1 2020-05-04 stsp goto done;
3555 2e8c69d1 2020-05-04 stsp }
3556 2e8c69d1 2020-05-04 stsp } else {
3557 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3558 2e8c69d1 2020-05-04 stsp if (err) {
3559 2e8c69d1 2020-05-04 stsp free(obj_id2);
3560 2e8c69d1 2020-05-04 stsp goto done;
3561 2e8c69d1 2020-05-04 stsp }
3562 44392932 2019-08-25 stsp }
3563 44392932 2019-08-25 stsp }
3564 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3565 44392932 2019-08-25 stsp if (err) {
3566 44392932 2019-08-25 stsp free(obj_id2);
3567 44392932 2019-08-25 stsp goto done;
3568 44392932 2019-08-25 stsp }
3569 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3570 44392932 2019-08-25 stsp switch (obj_type) {
3571 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3572 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3573 64453f7e 2020-11-21 stsp 0, 0, repo);
3574 44392932 2019-08-25 stsp break;
3575 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3576 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3577 64453f7e 2020-11-21 stsp 0, 0, repo);
3578 44392932 2019-08-25 stsp break;
3579 44392932 2019-08-25 stsp default:
3580 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3581 44392932 2019-08-25 stsp break;
3582 44392932 2019-08-25 stsp }
3583 44392932 2019-08-25 stsp free(obj_id1);
3584 44392932 2019-08-25 stsp free(obj_id2);
3585 44392932 2019-08-25 stsp } else {
3586 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3587 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3588 44392932 2019-08-25 stsp if (err)
3589 44392932 2019-08-25 stsp goto done;
3590 579bd556 2020-10-24 stsp if (pcommit) {
3591 579bd556 2020-10-24 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3592 579bd556 2020-10-24 stsp err = got_object_id_str(&id_str1, obj_id1);
3593 579bd556 2020-10-24 stsp if (err)
3594 579bd556 2020-10-24 stsp goto done;
3595 579bd556 2020-10-24 stsp }
3596 64453f7e 2020-11-21 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3597 64453f7e 2020-11-21 stsp id_str2);
3598 64453f7e 2020-11-21 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3599 64453f7e 2020-11-21 stsp repo);
3600 44392932 2019-08-25 stsp }
3601 44392932 2019-08-25 stsp done:
3602 0f2b3dca 2018-12-22 stsp free(id_str1);
3603 0f2b3dca 2018-12-22 stsp free(id_str2);
3604 44392932 2019-08-25 stsp if (pcommit)
3605 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3606 79109fed 2018-03-27 stsp return err;
3607 79109fed 2018-03-27 stsp }
3608 79109fed 2018-03-27 stsp
3609 4bb494d5 2018-06-16 stsp static char *
3610 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3611 6c281f94 2018-06-11 stsp {
3612 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3613 09867e48 2019-08-13 stsp char *p, *s;
3614 09867e48 2019-08-13 stsp
3615 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3616 09867e48 2019-08-13 stsp if (tm == NULL)
3617 09867e48 2019-08-13 stsp return NULL;
3618 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3619 09867e48 2019-08-13 stsp if (s == NULL)
3620 09867e48 2019-08-13 stsp return NULL;
3621 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3622 6c281f94 2018-06-11 stsp if (p)
3623 6c281f94 2018-06-11 stsp *p = '\0';
3624 6c281f94 2018-06-11 stsp return s;
3625 6c281f94 2018-06-11 stsp }
3626 dc424a06 2019-08-07 stsp
3627 6841bf13 2019-11-29 kn static const struct got_error *
3628 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3629 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3630 6841bf13 2019-11-29 kn {
3631 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3632 6841bf13 2019-11-29 kn regmatch_t regmatch;
3633 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3634 6841bf13 2019-11-29 kn
3635 6841bf13 2019-11-29 kn *have_match = 0;
3636 6841bf13 2019-11-29 kn
3637 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3638 6841bf13 2019-11-29 kn if (err)
3639 6841bf13 2019-11-29 kn return err;
3640 6841bf13 2019-11-29 kn
3641 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3642 6841bf13 2019-11-29 kn if (err)
3643 6841bf13 2019-11-29 kn goto done;
3644 6841bf13 2019-11-29 kn
3645 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3646 6841bf13 2019-11-29 kn *have_match = 1;
3647 6841bf13 2019-11-29 kn done:
3648 6841bf13 2019-11-29 kn free(id_str);
3649 6841bf13 2019-11-29 kn free(logmsg);
3650 6841bf13 2019-11-29 kn return err;
3651 6841bf13 2019-11-29 kn }
3652 6841bf13 2019-11-29 kn
3653 0208f208 2020-05-05 stsp static void
3654 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3655 0208f208 2020-05-05 stsp regex_t *regex)
3656 0208f208 2020-05-05 stsp {
3657 0208f208 2020-05-05 stsp regmatch_t regmatch;
3658 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3659 0208f208 2020-05-05 stsp
3660 0208f208 2020-05-05 stsp *have_match = 0;
3661 0208f208 2020-05-05 stsp
3662 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3663 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3664 0208f208 2020-05-05 stsp *have_match = 1;
3665 0208f208 2020-05-05 stsp break;
3666 0208f208 2020-05-05 stsp }
3667 0208f208 2020-05-05 stsp }
3668 0208f208 2020-05-05 stsp }
3669 0208f208 2020-05-05 stsp
3670 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3671 6c281f94 2018-06-11 stsp
3672 888b7d99 2020-12-26 stsp static const struct got_error*
3673 888b7d99 2020-12-26 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
3674 888b7d99 2020-12-26 stsp struct got_object_id *id, struct got_repository *repo)
3675 888b7d99 2020-12-26 stsp {
3676 888b7d99 2020-12-26 stsp static const struct got_error *err = NULL;
3677 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3678 888b7d99 2020-12-26 stsp char *s;
3679 888b7d99 2020-12-26 stsp const char *name;
3680 5c860e29 2018-03-12 stsp
3681 888b7d99 2020-12-26 stsp *refs_str = NULL;
3682 888b7d99 2020-12-26 stsp
3683 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
3684 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3685 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3686 a436ad14 2019-08-13 stsp int cmp;
3687 a436ad14 2019-08-13 stsp
3688 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3689 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3690 d9498b20 2019-02-05 stsp continue;
3691 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3692 199a4027 2019-02-02 stsp name += 5;
3693 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3694 7143d404 2019-03-12 stsp continue;
3695 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3696 e34f9ed6 2019-02-02 stsp name += 6;
3697 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3698 141c2bff 2019-02-04 stsp name += 8;
3699 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3700 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3701 4343a07f 2020-04-24 stsp continue;
3702 4343a07f 2020-04-24 stsp }
3703 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3704 48cae60d 2020-09-22 stsp if (err)
3705 888b7d99 2020-12-26 stsp break;
3706 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3707 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3708 5d844a1e 2019-08-13 stsp if (err) {
3709 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3710 48cae60d 2020-09-22 stsp free(ref_id);
3711 888b7d99 2020-12-26 stsp break;
3712 48cae60d 2020-09-22 stsp }
3713 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3714 5d844a1e 2019-08-13 stsp err = NULL;
3715 5d844a1e 2019-08-13 stsp tag = NULL;
3716 5d844a1e 2019-08-13 stsp }
3717 a436ad14 2019-08-13 stsp }
3718 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3719 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3720 48cae60d 2020-09-22 stsp free(ref_id);
3721 a436ad14 2019-08-13 stsp if (tag)
3722 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3723 a436ad14 2019-08-13 stsp if (cmp != 0)
3724 a436ad14 2019-08-13 stsp continue;
3725 888b7d99 2020-12-26 stsp s = *refs_str;
3726 888b7d99 2020-12-26 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
3727 888b7d99 2020-12-26 stsp s ? ", " : "", name) == -1) {
3728 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3729 199a4027 2019-02-02 stsp free(s);
3730 888b7d99 2020-12-26 stsp *refs_str = NULL;
3731 888b7d99 2020-12-26 stsp break;
3732 199a4027 2019-02-02 stsp }
3733 199a4027 2019-02-02 stsp free(s);
3734 199a4027 2019-02-02 stsp }
3735 888b7d99 2020-12-26 stsp
3736 888b7d99 2020-12-26 stsp return err;
3737 888b7d99 2020-12-26 stsp }
3738 888b7d99 2020-12-26 stsp
3739 888b7d99 2020-12-26 stsp static const struct got_error *
3740 888b7d99 2020-12-26 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3741 888b7d99 2020-12-26 stsp struct got_repository *repo, const char *path,
3742 888b7d99 2020-12-26 stsp struct got_pathlist_head *changed_paths, int show_patch,
3743 e600f124 2021-03-21 stsp int diff_context, struct got_reflist_object_id_map *refs_idmap,
3744 e600f124 2021-03-21 stsp const char *custom_refs_str)
3745 888b7d99 2020-12-26 stsp {
3746 888b7d99 2020-12-26 stsp const struct got_error *err = NULL;
3747 888b7d99 2020-12-26 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3748 888b7d99 2020-12-26 stsp char datebuf[26];
3749 888b7d99 2020-12-26 stsp time_t committer_time;
3750 888b7d99 2020-12-26 stsp const char *author, *committer;
3751 888b7d99 2020-12-26 stsp char *refs_str = NULL;
3752 888b7d99 2020-12-26 stsp
3753 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3754 8bf5b3c9 2018-03-17 stsp if (err)
3755 8bf5b3c9 2018-03-17 stsp return err;
3756 788c352e 2018-06-16 stsp
3757 e600f124 2021-03-21 stsp if (custom_refs_str == NULL) {
3758 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
3759 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3760 e600f124 2021-03-21 stsp if (refs) {
3761 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, id, repo);
3762 e600f124 2021-03-21 stsp if (err)
3763 e600f124 2021-03-21 stsp goto done;
3764 e600f124 2021-03-21 stsp }
3765 888b7d99 2020-12-26 stsp }
3766 888b7d99 2020-12-26 stsp
3767 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3768 e600f124 2021-03-21 stsp if (custom_refs_str)
3769 e600f124 2021-03-21 stsp printf("commit %s (%s)\n", id_str, custom_refs_str);
3770 e600f124 2021-03-21 stsp else
3771 e600f124 2021-03-21 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3772 e600f124 2021-03-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3773 832c249c 2018-06-10 stsp free(id_str);
3774 d3d493d7 2019-02-21 stsp id_str = NULL;
3775 d3d493d7 2019-02-21 stsp free(refs_str);
3776 d3d493d7 2019-02-21 stsp refs_str = NULL;
3777 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3778 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3779 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3780 09867e48 2019-08-13 stsp if (datestr)
3781 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3782 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3783 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3784 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3785 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3786 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3787 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3788 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3789 3fe1abad 2018-06-10 stsp int n = 1;
3790 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3791 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, parent_ids, entry) {
3792 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3793 a0603db2 2018-06-10 stsp if (err)
3794 888b7d99 2020-12-26 stsp goto done;
3795 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3796 a0603db2 2018-06-10 stsp free(id_str);
3797 888b7d99 2020-12-26 stsp id_str = NULL;
3798 a0603db2 2018-06-10 stsp }
3799 a0603db2 2018-06-10 stsp }
3800 832c249c 2018-06-10 stsp
3801 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3802 5943eee2 2019-08-13 stsp if (err)
3803 888b7d99 2020-12-26 stsp goto done;
3804 8bf5b3c9 2018-03-17 stsp
3805 621015ac 2018-07-23 stsp logmsg = logmsg0;
3806 832c249c 2018-06-10 stsp do {
3807 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3808 832c249c 2018-06-10 stsp if (line)
3809 832c249c 2018-06-10 stsp printf(" %s\n", line);
3810 832c249c 2018-06-10 stsp } while (line);
3811 621015ac 2018-07-23 stsp free(logmsg0);
3812 832c249c 2018-06-10 stsp
3813 0208f208 2020-05-05 stsp if (changed_paths) {
3814 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3815 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3816 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3817 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3818 0208f208 2020-05-05 stsp }
3819 0208f208 2020-05-05 stsp printf("\n");
3820 0208f208 2020-05-05 stsp }
3821 971751ac 2018-03-27 stsp if (show_patch) {
3822 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3823 971751ac 2018-03-27 stsp if (err == 0)
3824 971751ac 2018-03-27 stsp printf("\n");
3825 971751ac 2018-03-27 stsp }
3826 07862c20 2018-09-15 stsp
3827 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3828 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3829 888b7d99 2020-12-26 stsp done:
3830 888b7d99 2020-12-26 stsp free(id_str);
3831 888b7d99 2020-12-26 stsp free(refs_str);
3832 07862c20 2018-09-15 stsp return err;
3833 f42b1b34 2018-03-12 stsp }
3834 5c860e29 2018-03-12 stsp
3835 f42b1b34 2018-03-12 stsp static const struct got_error *
3836 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3837 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3838 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3839 888b7d99 2020-12-26 stsp int log_branches, int reverse_display_order,
3840 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap)
3841 f42b1b34 2018-03-12 stsp {
3842 f42b1b34 2018-03-12 stsp const struct got_error *err;
3843 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3844 6841bf13 2019-11-29 kn regex_t regex;
3845 6841bf13 2019-11-29 kn int have_match;
3846 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3847 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3848 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3849 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3850 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3851 dbec59df 2020-04-18 stsp
3852 dbdddfee 2021-06-23 naddy STAILQ_INIT(&reversed_commits);
3853 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3854 372ccdbb 2018-06-10 stsp
3855 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(&regex, search_pattern,
3856 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3857 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3858 6841bf13 2019-11-29 kn
3859 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3860 f42b1b34 2018-03-12 stsp if (err)
3861 f42b1b34 2018-03-12 stsp return err;
3862 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3863 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3864 372ccdbb 2018-06-10 stsp if (err)
3865 fcc85cad 2018-07-22 stsp goto done;
3866 656b1f76 2019-05-11 jcs for (;;) {
3867 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3868 8bf5b3c9 2018-03-17 stsp
3869 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3870 84453469 2018-11-11 stsp break;
3871 84453469 2018-11-11 stsp
3872 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3873 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3874 372ccdbb 2018-06-10 stsp if (err) {
3875 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3876 9ba79e04 2018-06-11 stsp err = NULL;
3877 ee780d5c 2020-01-04 stsp break;
3878 7e665116 2018-04-02 stsp }
3879 b43fbaa0 2018-06-11 stsp if (id == NULL)
3880 7e665116 2018-04-02 stsp break;
3881 b43fbaa0 2018-06-11 stsp
3882 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3883 b43fbaa0 2018-06-11 stsp if (err)
3884 fcc85cad 2018-07-22 stsp break;
3885 6841bf13 2019-11-29 kn
3886 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3887 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3888 0208f208 2020-05-05 stsp if (err)
3889 0208f208 2020-05-05 stsp break;
3890 0208f208 2020-05-05 stsp }
3891 0208f208 2020-05-05 stsp
3892 6841bf13 2019-11-29 kn if (search_pattern) {
3893 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3894 6841bf13 2019-11-29 kn if (err) {
3895 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3896 6841bf13 2019-11-29 kn break;
3897 6841bf13 2019-11-29 kn }
3898 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3899 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3900 0208f208 2020-05-05 stsp &changed_paths, &regex);
3901 6841bf13 2019-11-29 kn if (have_match == 0) {
3902 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3903 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3904 0208f208 2020-05-05 stsp free((char *)pe->path);
3905 0208f208 2020-05-05 stsp free(pe->data);
3906 0208f208 2020-05-05 stsp }
3907 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3908 6841bf13 2019-11-29 kn continue;
3909 6841bf13 2019-11-29 kn }
3910 6841bf13 2019-11-29 kn }
3911 6841bf13 2019-11-29 kn
3912 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3913 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3914 dbec59df 2020-04-18 stsp if (err)
3915 dbec59df 2020-04-18 stsp break;
3916 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3917 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3918 dbec59df 2020-04-18 stsp } else {
3919 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3920 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3921 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
3922 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3923 dbec59df 2020-04-18 stsp if (err)
3924 dbec59df 2020-04-18 stsp break;
3925 dbec59df 2020-04-18 stsp }
3926 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3927 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3928 7e665116 2018-04-02 stsp break;
3929 0208f208 2020-05-05 stsp
3930 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3931 0208f208 2020-05-05 stsp free((char *)pe->path);
3932 0208f208 2020-05-05 stsp free(pe->data);
3933 0208f208 2020-05-05 stsp }
3934 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3935 31cedeaf 2018-09-15 stsp }
3936 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3937 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &reversed_commits, entry) {
3938 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3939 dbec59df 2020-04-18 stsp if (err)
3940 dbec59df 2020-04-18 stsp break;
3941 502b9684 2020-07-31 stsp if (show_changed_paths) {
3942 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3943 502b9684 2020-07-31 stsp commit, repo);
3944 502b9684 2020-07-31 stsp if (err)
3945 502b9684 2020-07-31 stsp break;
3946 502b9684 2020-07-31 stsp }
3947 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3948 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3949 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
3950 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3951 dbec59df 2020-04-18 stsp if (err)
3952 dbec59df 2020-04-18 stsp break;
3953 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3954 502b9684 2020-07-31 stsp free((char *)pe->path);
3955 502b9684 2020-07-31 stsp free(pe->data);
3956 502b9684 2020-07-31 stsp }
3957 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3958 dbec59df 2020-04-18 stsp }
3959 dbec59df 2020-04-18 stsp }
3960 fcc85cad 2018-07-22 stsp done:
3961 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&reversed_commits)) {
3962 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&reversed_commits);
3963 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&reversed_commits, entry);
3964 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3965 dbec59df 2020-04-18 stsp }
3966 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3967 0208f208 2020-05-05 stsp free((char *)pe->path);
3968 0208f208 2020-05-05 stsp free(pe->data);
3969 0208f208 2020-05-05 stsp }
3970 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3971 6841bf13 2019-11-29 kn if (search_pattern)
3972 6841bf13 2019-11-29 kn regfree(&regex);
3973 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3974 f42b1b34 2018-03-12 stsp return err;
3975 f42b1b34 2018-03-12 stsp }
3976 5c860e29 2018-03-12 stsp
3977 4ed7e80c 2018-05-20 stsp __dead static void
3978 6f3d1eb0 2018-03-12 stsp usage_log(void)
3979 6f3d1eb0 2018-03-12 stsp {
3980 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3981 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3982 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3983 6f3d1eb0 2018-03-12 stsp exit(1);
3984 b1ebc001 2019-08-13 stsp }
3985 b1ebc001 2019-08-13 stsp
3986 b1ebc001 2019-08-13 stsp static int
3987 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3988 b1ebc001 2019-08-13 stsp {
3989 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3990 b1ebc001 2019-08-13 stsp long long n;
3991 b1ebc001 2019-08-13 stsp const char *errstr;
3992 b1ebc001 2019-08-13 stsp
3993 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3994 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3995 b1ebc001 2019-08-13 stsp return 0;
3996 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3997 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3998 b1ebc001 2019-08-13 stsp return 0;
3999 b1ebc001 2019-08-13 stsp return n;
4000 6f3d1eb0 2018-03-12 stsp }
4001 6f3d1eb0 2018-03-12 stsp
4002 4ed7e80c 2018-05-20 stsp static const struct got_error *
4003 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
4004 f42b1b34 2018-03-12 stsp {
4005 f42b1b34 2018-03-12 stsp const struct got_error *error;
4006 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
4007 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
4008 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
4009 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4010 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
4011 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
4012 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
4013 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4014 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
4015 64a96a6d 2018-04-01 stsp const char *errstr;
4016 199a4027 2019-02-02 stsp struct got_reflist_head refs;
4017 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
4018 1b3893a2 2019-03-18 stsp
4019 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4020 5c860e29 2018-03-12 stsp
4021 6715a751 2018-03-16 stsp #ifndef PROFILE
4022 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4023 6098196c 2019-01-04 stsp NULL)
4024 ad242220 2018-09-08 stsp == -1)
4025 f42b1b34 2018-03-12 stsp err(1, "pledge");
4026 6715a751 2018-03-16 stsp #endif
4027 79109fed 2018-03-27 stsp
4028 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
4029 b1ebc001 2019-08-13 stsp
4030 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
4031 79109fed 2018-03-27 stsp switch (ch) {
4032 79109fed 2018-03-27 stsp case 'p':
4033 79109fed 2018-03-27 stsp show_patch = 1;
4034 d142fc45 2018-04-01 stsp break;
4035 0208f208 2020-05-05 stsp case 'P':
4036 0208f208 2020-05-05 stsp show_changed_paths = 1;
4037 0208f208 2020-05-05 stsp break;
4038 d142fc45 2018-04-01 stsp case 'c':
4039 d142fc45 2018-04-01 stsp start_commit = optarg;
4040 64a96a6d 2018-04-01 stsp break;
4041 c0cc5c62 2018-10-18 stsp case 'C':
4042 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4043 4a8520aa 2018-10-18 stsp &errstr);
4044 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4045 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4046 c0cc5c62 2018-10-18 stsp break;
4047 64a96a6d 2018-04-01 stsp case 'l':
4048 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
4049 64a96a6d 2018-04-01 stsp if (errstr != NULL)
4050 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
4051 cc54c501 2019-07-15 stsp break;
4052 48c8c60d 2020-01-27 stsp case 'b':
4053 48c8c60d 2020-01-27 stsp log_branches = 1;
4054 a0603db2 2018-06-10 stsp break;
4055 04ca23f4 2018-07-16 stsp case 'r':
4056 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4057 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
4058 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4059 9ba1d308 2019-10-21 stsp optarg);
4060 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4061 04ca23f4 2018-07-16 stsp break;
4062 dbec59df 2020-04-18 stsp case 'R':
4063 dbec59df 2020-04-18 stsp reverse_display_order = 1;
4064 dbec59df 2020-04-18 stsp break;
4065 6841bf13 2019-11-29 kn case 's':
4066 6841bf13 2019-11-29 kn search_pattern = optarg;
4067 6841bf13 2019-11-29 kn break;
4068 d1fe46f9 2020-04-18 stsp case 'x':
4069 d1fe46f9 2020-04-18 stsp end_commit = optarg;
4070 d1fe46f9 2020-04-18 stsp break;
4071 79109fed 2018-03-27 stsp default:
4072 2deda0b9 2019-03-07 stsp usage_log();
4073 79109fed 2018-03-27 stsp /* NOTREACHED */
4074 79109fed 2018-03-27 stsp }
4075 79109fed 2018-03-27 stsp }
4076 79109fed 2018-03-27 stsp
4077 79109fed 2018-03-27 stsp argc -= optind;
4078 79109fed 2018-03-27 stsp argv += optind;
4079 f42b1b34 2018-03-12 stsp
4080 dc1edbfa 2019-11-29 kn if (diff_context == -1)
4081 dc1edbfa 2019-11-29 kn diff_context = 3;
4082 dc1edbfa 2019-11-29 kn else if (!show_patch)
4083 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
4084 dc1edbfa 2019-11-29 kn
4085 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
4086 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
4087 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4088 04ca23f4 2018-07-16 stsp goto done;
4089 04ca23f4 2018-07-16 stsp }
4090 cffc0aa4 2019-02-05 stsp
4091 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
4092 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
4093 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4094 50f2fada 2020-04-24 stsp goto done;
4095 50f2fada 2020-04-24 stsp error = NULL;
4096 50f2fada 2020-04-24 stsp }
4097 cffc0aa4 2019-02-05 stsp
4098 603cdeb0 2020-10-22 stsp if (argc == 1) {
4099 e7301579 2019-03-18 stsp if (worktree) {
4100 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4101 e7301579 2019-03-18 stsp argv[0]);
4102 e7301579 2019-03-18 stsp if (error)
4103 e7301579 2019-03-18 stsp goto done;
4104 e7301579 2019-03-18 stsp } else {
4105 e7301579 2019-03-18 stsp path = strdup(argv[0]);
4106 e7301579 2019-03-18 stsp if (path == NULL) {
4107 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4108 e7301579 2019-03-18 stsp goto done;
4109 e7301579 2019-03-18 stsp }
4110 e7301579 2019-03-18 stsp }
4111 603cdeb0 2020-10-22 stsp } else if (argc != 0)
4112 cbd1af7a 2019-03-18 stsp usage_log();
4113 cbd1af7a 2019-03-18 stsp
4114 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
4115 5486daa2 2019-05-11 stsp repo_path = worktree ?
4116 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4117 5486daa2 2019-05-11 stsp }
4118 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
4119 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4120 cffc0aa4 2019-02-05 stsp goto done;
4121 04ca23f4 2018-07-16 stsp }
4122 6098196c 2019-01-04 stsp
4123 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4124 d7d4f210 2018-03-12 stsp if (error != NULL)
4125 04ca23f4 2018-07-16 stsp goto done;
4126 f42b1b34 2018-03-12 stsp
4127 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4128 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4129 c02c541e 2019-03-29 stsp if (error)
4130 c02c541e 2019-03-29 stsp goto done;
4131 c02c541e 2019-03-29 stsp
4132 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4133 84de9106 2020-12-26 stsp if (error)
4134 84de9106 2020-12-26 stsp goto done;
4135 84de9106 2020-12-26 stsp
4136 888b7d99 2020-12-26 stsp error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4137 888b7d99 2020-12-26 stsp if (error)
4138 888b7d99 2020-12-26 stsp goto done;
4139 888b7d99 2020-12-26 stsp
4140 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
4141 3235492e 2018-04-01 stsp struct got_reference *head_ref;
4142 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
4143 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
4144 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4145 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
4146 3235492e 2018-04-01 stsp if (error != NULL)
4147 d1fe46f9 2020-04-18 stsp goto done;
4148 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
4149 3235492e 2018-04-01 stsp got_ref_close(head_ref);
4150 3235492e 2018-04-01 stsp if (error != NULL)
4151 d1fe46f9 2020-04-18 stsp goto done;
4152 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
4153 d1fe46f9 2020-04-18 stsp start_id);
4154 d1fe46f9 2020-04-18 stsp if (error != NULL)
4155 d1fe46f9 2020-04-18 stsp goto done;
4156 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
4157 3235492e 2018-04-01 stsp } else {
4158 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&start_id, NULL,
4159 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4160 d1fe46f9 2020-04-18 stsp if (error != NULL)
4161 d1fe46f9 2020-04-18 stsp goto done;
4162 3235492e 2018-04-01 stsp }
4163 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
4164 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&end_id, NULL,
4165 84de9106 2020-12-26 stsp end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4166 d1fe46f9 2020-04-18 stsp if (error != NULL)
4167 d1fe46f9 2020-04-18 stsp goto done;
4168 d1fe46f9 2020-04-18 stsp }
4169 04ca23f4 2018-07-16 stsp
4170 deeabeae 2019-08-27 stsp if (worktree) {
4171 603cdeb0 2020-10-22 stsp /*
4172 603cdeb0 2020-10-22 stsp * If a path was specified on the command line it was resolved
4173 603cdeb0 2020-10-22 stsp * to a path in the work tree above. Prepend the work tree's
4174 603cdeb0 2020-10-22 stsp * path prefix to obtain the corresponding in-repository path.
4175 603cdeb0 2020-10-22 stsp */
4176 603cdeb0 2020-10-22 stsp if (path) {
4177 603cdeb0 2020-10-22 stsp const char *prefix;
4178 603cdeb0 2020-10-22 stsp prefix = got_worktree_get_path_prefix(worktree);
4179 603cdeb0 2020-10-22 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4180 603cdeb0 2020-10-22 stsp (path[0] != '\0') ? "/" : "", path) == -1) {
4181 603cdeb0 2020-10-22 stsp error = got_error_from_errno("asprintf");
4182 603cdeb0 2020-10-22 stsp goto done;
4183 603cdeb0 2020-10-22 stsp }
4184 603cdeb0 2020-10-22 stsp }
4185 deeabeae 2019-08-27 stsp } else
4186 603cdeb0 2020-10-22 stsp error = got_repo_map_path(&in_repo_path, repo,
4187 8fa913ec 2020-11-14 stsp path ? path : "");
4188 04ca23f4 2018-07-16 stsp if (error != NULL)
4189 04ca23f4 2018-07-16 stsp goto done;
4190 04ca23f4 2018-07-16 stsp if (in_repo_path) {
4191 04ca23f4 2018-07-16 stsp free(path);
4192 04ca23f4 2018-07-16 stsp path = in_repo_path;
4193 04ca23f4 2018-07-16 stsp }
4194 199a4027 2019-02-02 stsp
4195 603cdeb0 2020-10-22 stsp error = print_commits(start_id, end_id, repo, path ? path : "",
4196 603cdeb0 2020-10-22 stsp show_changed_paths, show_patch, search_pattern, diff_context,
4197 888b7d99 2020-12-26 stsp limit, log_branches, reverse_display_order, refs_idmap);
4198 04ca23f4 2018-07-16 stsp done:
4199 04ca23f4 2018-07-16 stsp free(path);
4200 04ca23f4 2018-07-16 stsp free(repo_path);
4201 04ca23f4 2018-07-16 stsp free(cwd);
4202 cffc0aa4 2019-02-05 stsp if (worktree)
4203 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
4204 ad242220 2018-09-08 stsp if (repo) {
4205 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4206 ad242220 2018-09-08 stsp if (error == NULL)
4207 1d0f4054 2021-06-17 stsp error = close_err;
4208 ad242220 2018-09-08 stsp }
4209 888b7d99 2020-12-26 stsp if (refs_idmap)
4210 888b7d99 2020-12-26 stsp got_reflist_object_id_map_free(refs_idmap);
4211 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4212 8bf5b3c9 2018-03-17 stsp return error;
4213 5c860e29 2018-03-12 stsp }
4214 5c860e29 2018-03-12 stsp
4215 4ed7e80c 2018-05-20 stsp __dead static void
4216 3f8b7d6a 2018-04-01 stsp usage_diff(void)
4217 3f8b7d6a 2018-04-01 stsp {
4218 64453f7e 2020-11-21 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4219 64453f7e 2020-11-21 stsp "[-s] [-w] [object1 object2 | path]\n", getprogname());
4220 3f8b7d6a 2018-04-01 stsp exit(1);
4221 b00d56cd 2018-04-01 stsp }
4222 b00d56cd 2018-04-01 stsp
4223 b72f483a 2019-02-05 stsp struct print_diff_arg {
4224 b72f483a 2019-02-05 stsp struct got_repository *repo;
4225 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
4226 b72f483a 2019-02-05 stsp int diff_context;
4227 3fc0c068 2019-02-10 stsp const char *id_str;
4228 3fc0c068 2019-02-10 stsp int header_shown;
4229 98eaaa12 2019-08-03 stsp int diff_staged;
4230 63035f9f 2019-10-06 stsp int ignore_whitespace;
4231 64453f7e 2020-11-21 stsp int force_text_diff;
4232 b72f483a 2019-02-05 stsp };
4233 39449a05 2020-07-23 stsp
4234 39449a05 2020-07-23 stsp /*
4235 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
4236 39449a05 2020-07-23 stsp * it as content to the diff engine.
4237 39449a05 2020-07-23 stsp */
4238 39449a05 2020-07-23 stsp static const struct got_error *
4239 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4240 39449a05 2020-07-23 stsp const char *abspath)
4241 39449a05 2020-07-23 stsp {
4242 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
4243 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
4244 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
4245 39449a05 2020-07-23 stsp
4246 39449a05 2020-07-23 stsp *fd = -1;
4247 39449a05 2020-07-23 stsp
4248 39449a05 2020-07-23 stsp if (dirfd != -1) {
4249 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4250 39449a05 2020-07-23 stsp if (target_len == -1)
4251 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
4252 39449a05 2020-07-23 stsp } else {
4253 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
4254 39449a05 2020-07-23 stsp if (target_len == -1)
4255 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
4256 39449a05 2020-07-23 stsp }
4257 39449a05 2020-07-23 stsp
4258 39449a05 2020-07-23 stsp *fd = got_opentempfd();
4259 39449a05 2020-07-23 stsp if (*fd == -1)
4260 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
4261 39449a05 2020-07-23 stsp
4262 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
4263 39449a05 2020-07-23 stsp if (outlen == -1) {
4264 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
4265 39449a05 2020-07-23 stsp goto done;
4266 39449a05 2020-07-23 stsp }
4267 39449a05 2020-07-23 stsp
4268 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
4269 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
4270 39449a05 2020-07-23 stsp goto done;
4271 39449a05 2020-07-23 stsp }
4272 39449a05 2020-07-23 stsp done:
4273 39449a05 2020-07-23 stsp if (err) {
4274 39449a05 2020-07-23 stsp close(*fd);
4275 39449a05 2020-07-23 stsp *fd = -1;
4276 39449a05 2020-07-23 stsp }
4277 39449a05 2020-07-23 stsp return err;
4278 39449a05 2020-07-23 stsp }
4279 b72f483a 2019-02-05 stsp
4280 4ed7e80c 2018-05-20 stsp static const struct got_error *
4281 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
4282 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4283 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4284 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4285 b72f483a 2019-02-05 stsp {
4286 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
4287 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
4288 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
4289 12463d8b 2019-12-13 stsp int fd = -1;
4290 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
4291 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
4292 b72f483a 2019-02-05 stsp struct stat sb;
4293 b72f483a 2019-02-05 stsp
4294 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4295 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4296 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4297 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4298 98eaaa12 2019-08-03 stsp return NULL;
4299 98eaaa12 2019-08-03 stsp } else {
4300 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4301 98eaaa12 2019-08-03 stsp return NULL;
4302 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4303 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
4304 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4305 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
4306 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
4307 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
4308 98eaaa12 2019-08-03 stsp return NULL;
4309 98eaaa12 2019-08-03 stsp }
4310 3fc0c068 2019-02-10 stsp
4311 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
4312 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
4313 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
4314 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
4315 3fc0c068 2019-02-10 stsp a->header_shown = 1;
4316 3fc0c068 2019-02-10 stsp }
4317 b72f483a 2019-02-05 stsp
4318 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4319 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
4320 98eaaa12 2019-08-03 stsp switch (staged_status) {
4321 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
4322 98eaaa12 2019-08-03 stsp label1 = path;
4323 98eaaa12 2019-08-03 stsp label2 = path;
4324 98eaaa12 2019-08-03 stsp break;
4325 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
4326 98eaaa12 2019-08-03 stsp label2 = path;
4327 98eaaa12 2019-08-03 stsp break;
4328 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
4329 98eaaa12 2019-08-03 stsp label1 = path;
4330 98eaaa12 2019-08-03 stsp break;
4331 98eaaa12 2019-08-03 stsp default:
4332 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
4333 98eaaa12 2019-08-03 stsp }
4334 fe621944 2020-11-10 stsp return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4335 fe621944 2020-11-10 stsp staged_blob_id, label1, label2, a->diff_context,
4336 64453f7e 2020-11-21 stsp a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4337 98eaaa12 2019-08-03 stsp }
4338 98eaaa12 2019-08-03 stsp
4339 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4340 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
4341 4ce46740 2019-08-08 stsp char *id_str;
4342 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4343 408b4ebc 2019-08-03 stsp 8192);
4344 4ce46740 2019-08-08 stsp if (err)
4345 4ce46740 2019-08-08 stsp goto done;
4346 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4347 4ce46740 2019-08-08 stsp if (err)
4348 4ce46740 2019-08-08 stsp goto done;
4349 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4350 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4351 4ce46740 2019-08-08 stsp free(id_str);
4352 4ce46740 2019-08-08 stsp goto done;
4353 4ce46740 2019-08-08 stsp }
4354 4ce46740 2019-08-08 stsp free(id_str);
4355 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4356 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4357 4ce46740 2019-08-08 stsp if (err)
4358 4ce46740 2019-08-08 stsp goto done;
4359 4ce46740 2019-08-08 stsp }
4360 d00136be 2019-03-26 stsp
4361 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4362 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4363 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4364 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4365 2ec1f75b 2019-03-26 stsp goto done;
4366 2ec1f75b 2019-03-26 stsp }
4367 b72f483a 2019-02-05 stsp
4368 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4369 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4370 12463d8b 2019-12-13 stsp if (fd == -1) {
4371 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4372 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4373 39449a05 2020-07-23 stsp abspath);
4374 39449a05 2020-07-23 stsp goto done;
4375 39449a05 2020-07-23 stsp }
4376 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4377 39449a05 2020-07-23 stsp de_name, abspath);
4378 39449a05 2020-07-23 stsp if (err)
4379 39449a05 2020-07-23 stsp goto done;
4380 12463d8b 2019-12-13 stsp }
4381 12463d8b 2019-12-13 stsp } else {
4382 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4383 12463d8b 2019-12-13 stsp if (fd == -1) {
4384 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4385 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4386 39449a05 2020-07-23 stsp abspath);
4387 39449a05 2020-07-23 stsp goto done;
4388 39449a05 2020-07-23 stsp }
4389 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4390 39449a05 2020-07-23 stsp de_name, abspath);
4391 39449a05 2020-07-23 stsp if (err)
4392 39449a05 2020-07-23 stsp goto done;
4393 12463d8b 2019-12-13 stsp }
4394 12463d8b 2019-12-13 stsp }
4395 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4396 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4397 2ec1f75b 2019-03-26 stsp goto done;
4398 2ec1f75b 2019-03-26 stsp }
4399 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4400 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4401 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4402 2ec1f75b 2019-03-26 stsp goto done;
4403 2ec1f75b 2019-03-26 stsp }
4404 12463d8b 2019-12-13 stsp fd = -1;
4405 2ec1f75b 2019-03-26 stsp } else
4406 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4407 b72f483a 2019-02-05 stsp
4408 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4409 64453f7e 2020-11-21 stsp a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4410 b72f483a 2019-02-05 stsp done:
4411 b72f483a 2019-02-05 stsp if (blob1)
4412 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4413 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4414 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4415 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4416 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4417 b72f483a 2019-02-05 stsp free(abspath);
4418 927df6b7 2019-02-10 stsp return err;
4419 927df6b7 2019-02-10 stsp }
4420 927df6b7 2019-02-10 stsp
4421 927df6b7 2019-02-10 stsp static const struct got_error *
4422 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4423 b00d56cd 2018-04-01 stsp {
4424 b00d56cd 2018-04-01 stsp const struct got_error *error;
4425 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4426 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4427 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4428 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4429 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
4430 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
4431 15a94983 2018-12-23 stsp int type1, type2;
4432 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4433 64453f7e 2020-11-21 stsp int force_text_diff = 0;
4434 c0cc5c62 2018-10-18 stsp const char *errstr;
4435 927df6b7 2019-02-10 stsp char *path = NULL;
4436 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4437 b00d56cd 2018-04-01 stsp
4438 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4439 84de9106 2020-12-26 stsp
4440 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4441 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4442 25eccc22 2019-01-04 stsp NULL) == -1)
4443 b00d56cd 2018-04-01 stsp err(1, "pledge");
4444 b00d56cd 2018-04-01 stsp #endif
4445 b00d56cd 2018-04-01 stsp
4446 64453f7e 2020-11-21 stsp while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4447 b00d56cd 2018-04-01 stsp switch (ch) {
4448 64453f7e 2020-11-21 stsp case 'a':
4449 64453f7e 2020-11-21 stsp force_text_diff = 1;
4450 64453f7e 2020-11-21 stsp break;
4451 c0cc5c62 2018-10-18 stsp case 'C':
4452 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4453 dfcab68b 2019-11-29 kn &errstr);
4454 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4455 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4456 c0cc5c62 2018-10-18 stsp break;
4457 b72f483a 2019-02-05 stsp case 'r':
4458 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4459 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4460 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4461 9ba1d308 2019-10-21 stsp optarg);
4462 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4463 b72f483a 2019-02-05 stsp break;
4464 98eaaa12 2019-08-03 stsp case 's':
4465 98eaaa12 2019-08-03 stsp diff_staged = 1;
4466 63035f9f 2019-10-06 stsp break;
4467 63035f9f 2019-10-06 stsp case 'w':
4468 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4469 98eaaa12 2019-08-03 stsp break;
4470 b00d56cd 2018-04-01 stsp default:
4471 2deda0b9 2019-03-07 stsp usage_diff();
4472 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4473 b00d56cd 2018-04-01 stsp }
4474 b00d56cd 2018-04-01 stsp }
4475 b00d56cd 2018-04-01 stsp
4476 b00d56cd 2018-04-01 stsp argc -= optind;
4477 b00d56cd 2018-04-01 stsp argv += optind;
4478 b00d56cd 2018-04-01 stsp
4479 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4480 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4481 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4482 b72f483a 2019-02-05 stsp goto done;
4483 b72f483a 2019-02-05 stsp }
4484 927df6b7 2019-02-10 stsp if (argc <= 1) {
4485 b72f483a 2019-02-05 stsp if (repo_path)
4486 b72f483a 2019-02-05 stsp errx(1,
4487 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
4488 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4489 fa51e947 2020-03-27 stsp if (error) {
4490 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4491 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
4492 fa51e947 2020-03-27 stsp cwd);
4493 1f03b8da 2020-03-20 stsp goto done;
4494 fa51e947 2020-03-27 stsp }
4495 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4496 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4497 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4498 927df6b7 2019-02-10 stsp goto done;
4499 927df6b7 2019-02-10 stsp }
4500 927df6b7 2019-02-10 stsp if (argc == 1) {
4501 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4502 cbd1af7a 2019-03-18 stsp argv[0]);
4503 927df6b7 2019-02-10 stsp if (error)
4504 927df6b7 2019-02-10 stsp goto done;
4505 927df6b7 2019-02-10 stsp } else {
4506 927df6b7 2019-02-10 stsp path = strdup("");
4507 927df6b7 2019-02-10 stsp if (path == NULL) {
4508 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4509 927df6b7 2019-02-10 stsp goto done;
4510 927df6b7 2019-02-10 stsp }
4511 927df6b7 2019-02-10 stsp }
4512 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4513 98eaaa12 2019-08-03 stsp if (diff_staged)
4514 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4515 98eaaa12 2019-08-03 stsp "objects in repository");
4516 15a94983 2018-12-23 stsp id_str1 = argv[0];
4517 15a94983 2018-12-23 stsp id_str2 = argv[1];
4518 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4519 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4520 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4521 30db809c 2019-06-05 stsp goto done;
4522 1a1242a9 2021-04-01 kn repo_path = strdup(worktree ?
4523 1a1242a9 2021-04-01 kn got_worktree_get_repo_path(worktree) : cwd);
4524 1a1242a9 2021-04-01 kn if (repo_path == NULL) {
4525 1a1242a9 2021-04-01 kn error = got_error_from_errno("strdup");
4526 1a1242a9 2021-04-01 kn goto done;
4527 30db809c 2019-06-05 stsp }
4528 30db809c 2019-06-05 stsp }
4529 b00d56cd 2018-04-01 stsp } else
4530 b00d56cd 2018-04-01 stsp usage_diff();
4531 b00d56cd 2018-04-01 stsp
4532 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4533 76089277 2018-04-01 stsp free(repo_path);
4534 b00d56cd 2018-04-01 stsp if (error != NULL)
4535 b00d56cd 2018-04-01 stsp goto done;
4536 b00d56cd 2018-04-01 stsp
4537 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4538 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4539 c02c541e 2019-03-29 stsp if (error)
4540 c02c541e 2019-03-29 stsp goto done;
4541 c02c541e 2019-03-29 stsp
4542 30db809c 2019-06-05 stsp if (argc <= 1) {
4543 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4544 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4545 b72f483a 2019-02-05 stsp char *id_str;
4546 72ea6654 2019-07-27 stsp
4547 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4548 72ea6654 2019-07-27 stsp
4549 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4550 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4551 b72f483a 2019-02-05 stsp if (error)
4552 b72f483a 2019-02-05 stsp goto done;
4553 b72f483a 2019-02-05 stsp arg.repo = repo;
4554 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4555 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4556 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4557 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4558 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4559 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4560 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
4561 b72f483a 2019-02-05 stsp
4562 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4563 72ea6654 2019-07-27 stsp if (error)
4564 72ea6654 2019-07-27 stsp goto done;
4565 72ea6654 2019-07-27 stsp
4566 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
4567 f6343036 2021-06-22 stsp print_diff, &arg, check_cancelled, NULL);
4568 b72f483a 2019-02-05 stsp free(id_str);
4569 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4570 b72f483a 2019-02-05 stsp goto done;
4571 b72f483a 2019-02-05 stsp }
4572 84de9106 2020-12-26 stsp
4573 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4574 84de9106 2020-12-26 stsp if (error)
4575 84de9106 2020-12-26 stsp return error;
4576 b72f483a 2019-02-05 stsp
4577 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4578 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4579 0adb7196 2019-08-11 stsp if (error)
4580 0adb7196 2019-08-11 stsp goto done;
4581 b00d56cd 2018-04-01 stsp
4582 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4583 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4584 0adb7196 2019-08-11 stsp if (error)
4585 0adb7196 2019-08-11 stsp goto done;
4586 b00d56cd 2018-04-01 stsp
4587 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4588 15a94983 2018-12-23 stsp if (error)
4589 15a94983 2018-12-23 stsp goto done;
4590 15a94983 2018-12-23 stsp
4591 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4592 15a94983 2018-12-23 stsp if (error)
4593 15a94983 2018-12-23 stsp goto done;
4594 15a94983 2018-12-23 stsp
4595 15a94983 2018-12-23 stsp if (type1 != type2) {
4596 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4597 b00d56cd 2018-04-01 stsp goto done;
4598 b00d56cd 2018-04-01 stsp }
4599 b00d56cd 2018-04-01 stsp
4600 15a94983 2018-12-23 stsp switch (type1) {
4601 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4602 fe621944 2020-11-10 stsp error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4603 64453f7e 2020-11-21 stsp NULL, NULL, diff_context, ignore_whitespace,
4604 64453f7e 2020-11-21 stsp force_text_diff, repo, stdout);
4605 b00d56cd 2018-04-01 stsp break;
4606 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4607 fe621944 2020-11-10 stsp error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4608 64453f7e 2020-11-21 stsp "", "", diff_context, ignore_whitespace, force_text_diff,
4609 64453f7e 2020-11-21 stsp repo, stdout);
4610 b00d56cd 2018-04-01 stsp break;
4611 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4612 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4613 fe621944 2020-11-10 stsp error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4614 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, repo,
4615 64453f7e 2020-11-21 stsp stdout);
4616 b00d56cd 2018-04-01 stsp break;
4617 b00d56cd 2018-04-01 stsp default:
4618 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4619 b00d56cd 2018-04-01 stsp }
4620 b00d56cd 2018-04-01 stsp done:
4621 5e70831e 2019-05-28 stsp free(label1);
4622 5e70831e 2019-05-28 stsp free(label2);
4623 15a94983 2018-12-23 stsp free(id1);
4624 15a94983 2018-12-23 stsp free(id2);
4625 927df6b7 2019-02-10 stsp free(path);
4626 b72f483a 2019-02-05 stsp if (worktree)
4627 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4628 ad242220 2018-09-08 stsp if (repo) {
4629 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4630 ad242220 2018-09-08 stsp if (error == NULL)
4631 1d0f4054 2021-06-17 stsp error = close_err;
4632 ad242220 2018-09-08 stsp }
4633 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4634 b00d56cd 2018-04-01 stsp return error;
4635 404c43c4 2018-06-21 stsp }
4636 404c43c4 2018-06-21 stsp
4637 404c43c4 2018-06-21 stsp __dead static void
4638 404c43c4 2018-06-21 stsp usage_blame(void)
4639 404c43c4 2018-06-21 stsp {
4640 9270e621 2019-02-05 stsp fprintf(stderr,
4641 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4642 404c43c4 2018-06-21 stsp getprogname());
4643 404c43c4 2018-06-21 stsp exit(1);
4644 b00d56cd 2018-04-01 stsp }
4645 b00d56cd 2018-04-01 stsp
4646 28315671 2019-08-14 stsp struct blame_line {
4647 28315671 2019-08-14 stsp int annotated;
4648 28315671 2019-08-14 stsp char *id_str;
4649 82f6abb8 2019-08-14 stsp char *committer;
4650 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4651 28315671 2019-08-14 stsp };
4652 28315671 2019-08-14 stsp
4653 28315671 2019-08-14 stsp struct blame_cb_args {
4654 28315671 2019-08-14 stsp struct blame_line *lines;
4655 28315671 2019-08-14 stsp int nlines;
4656 7ef28ff8 2019-08-14 stsp int nlines_prec;
4657 28315671 2019-08-14 stsp int lineno_cur;
4658 28315671 2019-08-14 stsp off_t *line_offsets;
4659 28315671 2019-08-14 stsp FILE *f;
4660 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4661 28315671 2019-08-14 stsp };
4662 28315671 2019-08-14 stsp
4663 404c43c4 2018-06-21 stsp static const struct got_error *
4664 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4665 28315671 2019-08-14 stsp {
4666 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4667 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4668 28315671 2019-08-14 stsp struct blame_line *bline;
4669 82f6abb8 2019-08-14 stsp char *line = NULL;
4670 28315671 2019-08-14 stsp size_t linesize = 0;
4671 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4672 28315671 2019-08-14 stsp off_t offset;
4673 bcb49d15 2019-08-14 stsp struct tm tm;
4674 bcb49d15 2019-08-14 stsp time_t committer_time;
4675 28315671 2019-08-14 stsp
4676 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4677 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4678 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4679 28315671 2019-08-14 stsp
4680 28315671 2019-08-14 stsp if (sigint_received)
4681 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4682 28315671 2019-08-14 stsp
4683 2839f8b9 2019-08-18 stsp if (lineno == -1)
4684 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4685 2839f8b9 2019-08-18 stsp
4686 28315671 2019-08-14 stsp /* Annotate this line. */
4687 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4688 28315671 2019-08-14 stsp if (bline->annotated)
4689 28315671 2019-08-14 stsp return NULL;
4690 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4691 28315671 2019-08-14 stsp if (err)
4692 28315671 2019-08-14 stsp return err;
4693 82f6abb8 2019-08-14 stsp
4694 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4695 82f6abb8 2019-08-14 stsp if (err)
4696 82f6abb8 2019-08-14 stsp goto done;
4697 82f6abb8 2019-08-14 stsp
4698 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4699 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4700 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4701 bcb49d15 2019-08-14 stsp goto done;
4702 bcb49d15 2019-08-14 stsp }
4703 bcb49d15 2019-08-14 stsp
4704 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4705 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
4706 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
4707 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4708 ec6d1a36 2021-03-21 jrick &tm) == 0) {
4709 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4710 82f6abb8 2019-08-14 stsp goto done;
4711 82f6abb8 2019-08-14 stsp }
4712 28315671 2019-08-14 stsp bline->annotated = 1;
4713 28315671 2019-08-14 stsp
4714 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4715 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4716 28315671 2019-08-14 stsp if (!bline->annotated)
4717 82f6abb8 2019-08-14 stsp goto done;
4718 28315671 2019-08-14 stsp
4719 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4720 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4721 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4722 82f6abb8 2019-08-14 stsp goto done;
4723 82f6abb8 2019-08-14 stsp }
4724 28315671 2019-08-14 stsp
4725 28315671 2019-08-14 stsp while (bline->annotated) {
4726 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4727 82f6abb8 2019-08-14 stsp size_t len;
4728 82f6abb8 2019-08-14 stsp
4729 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4730 28315671 2019-08-14 stsp if (ferror(a->f))
4731 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4732 28315671 2019-08-14 stsp break;
4733 28315671 2019-08-14 stsp }
4734 82f6abb8 2019-08-14 stsp
4735 82f6abb8 2019-08-14 stsp committer = bline->committer;
4736 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4737 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4738 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4739 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4740 82f6abb8 2019-08-14 stsp if (at)
4741 82f6abb8 2019-08-14 stsp *at = '\0';
4742 82f6abb8 2019-08-14 stsp len = strlen(committer);
4743 82f6abb8 2019-08-14 stsp if (len >= 9)
4744 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4745 28315671 2019-08-14 stsp
4746 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4747 28315671 2019-08-14 stsp if (nl)
4748 28315671 2019-08-14 stsp *nl = '\0';
4749 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4750 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4751 28315671 2019-08-14 stsp
4752 28315671 2019-08-14 stsp a->lineno_cur++;
4753 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4754 28315671 2019-08-14 stsp }
4755 82f6abb8 2019-08-14 stsp done:
4756 82f6abb8 2019-08-14 stsp if (commit)
4757 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4758 28315671 2019-08-14 stsp free(line);
4759 28315671 2019-08-14 stsp return err;
4760 28315671 2019-08-14 stsp }
4761 28315671 2019-08-14 stsp
4762 28315671 2019-08-14 stsp static const struct got_error *
4763 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4764 404c43c4 2018-06-21 stsp {
4765 404c43c4 2018-06-21 stsp const struct got_error *error;
4766 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4767 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4768 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4769 0587e10c 2020-07-23 stsp char *link_target = NULL;
4770 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4771 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4772 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4773 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4774 28315671 2019-08-14 stsp struct blame_cb_args bca;
4775 28315671 2019-08-14 stsp int ch, obj_type, i;
4776 be659d10 2020-11-18 stsp off_t filesize;
4777 90f3c347 2019-08-19 stsp
4778 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4779 404c43c4 2018-06-21 stsp
4780 404c43c4 2018-06-21 stsp #ifndef PROFILE
4781 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4782 36e2fb66 2019-01-04 stsp NULL) == -1)
4783 404c43c4 2018-06-21 stsp err(1, "pledge");
4784 404c43c4 2018-06-21 stsp #endif
4785 404c43c4 2018-06-21 stsp
4786 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4787 404c43c4 2018-06-21 stsp switch (ch) {
4788 404c43c4 2018-06-21 stsp case 'c':
4789 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4790 404c43c4 2018-06-21 stsp break;
4791 66bea077 2018-08-02 stsp case 'r':
4792 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4793 66bea077 2018-08-02 stsp if (repo_path == NULL)
4794 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4795 9ba1d308 2019-10-21 stsp optarg);
4796 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4797 66bea077 2018-08-02 stsp break;
4798 404c43c4 2018-06-21 stsp default:
4799 2deda0b9 2019-03-07 stsp usage_blame();
4800 404c43c4 2018-06-21 stsp /* NOTREACHED */
4801 404c43c4 2018-06-21 stsp }
4802 404c43c4 2018-06-21 stsp }
4803 404c43c4 2018-06-21 stsp
4804 404c43c4 2018-06-21 stsp argc -= optind;
4805 404c43c4 2018-06-21 stsp argv += optind;
4806 404c43c4 2018-06-21 stsp
4807 a39318fd 2018-08-02 stsp if (argc == 1)
4808 404c43c4 2018-06-21 stsp path = argv[0];
4809 a39318fd 2018-08-02 stsp else
4810 404c43c4 2018-06-21 stsp usage_blame();
4811 404c43c4 2018-06-21 stsp
4812 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4813 66bea077 2018-08-02 stsp if (cwd == NULL) {
4814 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4815 66bea077 2018-08-02 stsp goto done;
4816 66bea077 2018-08-02 stsp }
4817 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4818 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4819 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4820 66bea077 2018-08-02 stsp goto done;
4821 0c06baac 2019-02-05 stsp else
4822 0c06baac 2019-02-05 stsp error = NULL;
4823 0c06baac 2019-02-05 stsp if (worktree) {
4824 0c06baac 2019-02-05 stsp repo_path =
4825 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4826 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4827 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4828 1f03b8da 2020-03-20 stsp if (error)
4829 1f03b8da 2020-03-20 stsp goto done;
4830 1f03b8da 2020-03-20 stsp }
4831 0c06baac 2019-02-05 stsp } else {
4832 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4833 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4834 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4835 0c06baac 2019-02-05 stsp goto done;
4836 0c06baac 2019-02-05 stsp }
4837 66bea077 2018-08-02 stsp }
4838 66bea077 2018-08-02 stsp }
4839 36e2fb66 2019-01-04 stsp
4840 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4841 c02c541e 2019-03-29 stsp if (error != NULL)
4842 404c43c4 2018-06-21 stsp goto done;
4843 404c43c4 2018-06-21 stsp
4844 0c06baac 2019-02-05 stsp if (worktree) {
4845 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4846 01740607 2020-11-04 stsp char *p;
4847 01740607 2020-11-04 stsp
4848 01740607 2020-11-04 stsp error = got_worktree_resolve_path(&p, worktree, path);
4849 01740607 2020-11-04 stsp if (error)
4850 01740607 2020-11-04 stsp goto done;
4851 01740607 2020-11-04 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4852 01740607 2020-11-04 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4853 01740607 2020-11-04 stsp p) == -1) {
4854 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4855 01740607 2020-11-04 stsp free(p);
4856 0c06baac 2019-02-05 stsp goto done;
4857 0c06baac 2019-02-05 stsp }
4858 0c06baac 2019-02-05 stsp free(p);
4859 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4860 0c06baac 2019-02-05 stsp } else {
4861 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4862 01740607 2020-11-04 stsp if (error)
4863 01740607 2020-11-04 stsp goto done;
4864 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
4865 0c06baac 2019-02-05 stsp }
4866 0c06baac 2019-02-05 stsp if (error)
4867 66bea077 2018-08-02 stsp goto done;
4868 66bea077 2018-08-02 stsp
4869 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4870 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4871 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4872 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4873 404c43c4 2018-06-21 stsp if (error != NULL)
4874 66bea077 2018-08-02 stsp goto done;
4875 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4876 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4877 404c43c4 2018-06-21 stsp if (error != NULL)
4878 66bea077 2018-08-02 stsp goto done;
4879 404c43c4 2018-06-21 stsp } else {
4880 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4881 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4882 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4883 84de9106 2020-12-26 stsp NULL);
4884 84de9106 2020-12-26 stsp if (error)
4885 84de9106 2020-12-26 stsp goto done;
4886 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4887 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4888 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4889 30837e32 2019-07-25 stsp if (error)
4890 66bea077 2018-08-02 stsp goto done;
4891 404c43c4 2018-06-21 stsp }
4892 404c43c4 2018-06-21 stsp
4893 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4894 0587e10c 2020-07-23 stsp commit_id, repo);
4895 0587e10c 2020-07-23 stsp if (error)
4896 0587e10c 2020-07-23 stsp goto done;
4897 0587e10c 2020-07-23 stsp
4898 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4899 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4900 28315671 2019-08-14 stsp if (error)
4901 28315671 2019-08-14 stsp goto done;
4902 28315671 2019-08-14 stsp
4903 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4904 28315671 2019-08-14 stsp if (error)
4905 28315671 2019-08-14 stsp goto done;
4906 28315671 2019-08-14 stsp
4907 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4908 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4909 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4910 28315671 2019-08-14 stsp goto done;
4911 28315671 2019-08-14 stsp }
4912 28315671 2019-08-14 stsp
4913 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4914 28315671 2019-08-14 stsp if (error)
4915 28315671 2019-08-14 stsp goto done;
4916 28315671 2019-08-14 stsp bca.f = got_opentemp();
4917 28315671 2019-08-14 stsp if (bca.f == NULL) {
4918 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4919 28315671 2019-08-14 stsp goto done;
4920 28315671 2019-08-14 stsp }
4921 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4922 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4923 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4924 28315671 2019-08-14 stsp goto done;
4925 28315671 2019-08-14 stsp
4926 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4927 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4928 b02560ec 2019-08-19 stsp bca.nlines--;
4929 b02560ec 2019-08-19 stsp
4930 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4931 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4932 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4933 28315671 2019-08-14 stsp goto done;
4934 28315671 2019-08-14 stsp }
4935 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4936 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4937 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4938 7ef28ff8 2019-08-14 stsp while (i > 0) {
4939 7ef28ff8 2019-08-14 stsp i /= 10;
4940 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4941 7ef28ff8 2019-08-14 stsp }
4942 82f6abb8 2019-08-14 stsp bca.repo = repo;
4943 7ef28ff8 2019-08-14 stsp
4944 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4945 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4946 404c43c4 2018-06-21 stsp done:
4947 66bea077 2018-08-02 stsp free(in_repo_path);
4948 0587e10c 2020-07-23 stsp free(link_target);
4949 66bea077 2018-08-02 stsp free(repo_path);
4950 66bea077 2018-08-02 stsp free(cwd);
4951 404c43c4 2018-06-21 stsp free(commit_id);
4952 28315671 2019-08-14 stsp free(obj_id);
4953 28315671 2019-08-14 stsp if (blob)
4954 28315671 2019-08-14 stsp got_object_blob_close(blob);
4955 0c06baac 2019-02-05 stsp if (worktree)
4956 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4957 ad242220 2018-09-08 stsp if (repo) {
4958 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4959 ad242220 2018-09-08 stsp if (error == NULL)
4960 1d0f4054 2021-06-17 stsp error = close_err;
4961 ad242220 2018-09-08 stsp }
4962 3affba96 2019-09-22 stsp if (bca.lines) {
4963 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4964 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4965 3affba96 2019-09-22 stsp free(bline->id_str);
4966 3affba96 2019-09-22 stsp free(bline->committer);
4967 3affba96 2019-09-22 stsp }
4968 3affba96 2019-09-22 stsp free(bca.lines);
4969 28315671 2019-08-14 stsp }
4970 28315671 2019-08-14 stsp free(bca.line_offsets);
4971 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4972 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4973 404c43c4 2018-06-21 stsp return error;
4974 5de5890b 2018-10-18 stsp }
4975 5de5890b 2018-10-18 stsp
4976 5de5890b 2018-10-18 stsp __dead static void
4977 5de5890b 2018-10-18 stsp usage_tree(void)
4978 5de5890b 2018-10-18 stsp {
4979 c1669e2e 2019-01-09 stsp fprintf(stderr,
4980 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4981 5de5890b 2018-10-18 stsp getprogname());
4982 5de5890b 2018-10-18 stsp exit(1);
4983 5de5890b 2018-10-18 stsp }
4984 5de5890b 2018-10-18 stsp
4985 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4986 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4987 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4988 c1669e2e 2019-01-09 stsp {
4989 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4990 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4991 848d6979 2019-08-12 stsp const char *modestr = "";
4992 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4993 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4994 5de5890b 2018-10-18 stsp
4995 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4996 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4997 c1669e2e 2019-01-09 stsp path++;
4998 c1669e2e 2019-01-09 stsp
4999 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5000 63c5ca5d 2019-08-24 stsp modestr = "$";
5001 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5002 0d6c6ee3 2020-05-20 stsp int i;
5003 0d6c6ee3 2020-05-20 stsp
5004 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5005 0d6c6ee3 2020-05-20 stsp if (err)
5006 0d6c6ee3 2020-05-20 stsp return err;
5007 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5008 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5009 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5010 0d6c6ee3 2020-05-20 stsp }
5011 0d6c6ee3 2020-05-20 stsp
5012 848d6979 2019-08-12 stsp modestr = "@";
5013 0d6c6ee3 2020-05-20 stsp }
5014 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5015 848d6979 2019-08-12 stsp modestr = "/";
5016 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5017 848d6979 2019-08-12 stsp modestr = "*";
5018 848d6979 2019-08-12 stsp
5019 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5020 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5021 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
5022 0d6c6ee3 2020-05-20 stsp
5023 0d6c6ee3 2020-05-20 stsp free(link_target);
5024 0d6c6ee3 2020-05-20 stsp return NULL;
5025 c1669e2e 2019-01-09 stsp }
5026 c1669e2e 2019-01-09 stsp
5027 5de5890b 2018-10-18 stsp static const struct got_error *
5028 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
5029 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
5030 c1669e2e 2019-01-09 stsp struct got_repository *repo)
5031 5de5890b 2018-10-18 stsp {
5032 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
5033 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
5034 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
5035 56e0773d 2019-11-28 stsp int nentries, i;
5036 5de5890b 2018-10-18 stsp
5037 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
5038 5de5890b 2018-10-18 stsp if (err)
5039 5de5890b 2018-10-18 stsp goto done;
5040 5de5890b 2018-10-18 stsp
5041 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
5042 5de5890b 2018-10-18 stsp if (err)
5043 5de5890b 2018-10-18 stsp goto done;
5044 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
5045 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
5046 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5047 5de5890b 2018-10-18 stsp char *id = NULL;
5048 84453469 2018-11-11 stsp
5049 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
5050 84453469 2018-11-11 stsp break;
5051 84453469 2018-11-11 stsp
5052 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
5053 5de5890b 2018-10-18 stsp if (show_ids) {
5054 5de5890b 2018-10-18 stsp char *id_str;
5055 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5056 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5057 5de5890b 2018-10-18 stsp if (err)
5058 5de5890b 2018-10-18 stsp goto done;
5059 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
5060 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5061 5de5890b 2018-10-18 stsp free(id_str);
5062 5de5890b 2018-10-18 stsp goto done;
5063 5de5890b 2018-10-18 stsp }
5064 5de5890b 2018-10-18 stsp free(id_str);
5065 5de5890b 2018-10-18 stsp }
5066 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
5067 5de5890b 2018-10-18 stsp free(id);
5068 0d6c6ee3 2020-05-20 stsp if (err)
5069 0d6c6ee3 2020-05-20 stsp goto done;
5070 c1669e2e 2019-01-09 stsp
5071 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5072 c1669e2e 2019-01-09 stsp char *child_path;
5073 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
5074 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
5075 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
5076 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5077 c1669e2e 2019-01-09 stsp goto done;
5078 c1669e2e 2019-01-09 stsp }
5079 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
5080 c1669e2e 2019-01-09 stsp root_path, repo);
5081 c1669e2e 2019-01-09 stsp free(child_path);
5082 c1669e2e 2019-01-09 stsp if (err)
5083 c1669e2e 2019-01-09 stsp goto done;
5084 c1669e2e 2019-01-09 stsp }
5085 5de5890b 2018-10-18 stsp }
5086 5de5890b 2018-10-18 stsp done:
5087 5de5890b 2018-10-18 stsp if (tree)
5088 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
5089 5de5890b 2018-10-18 stsp free(tree_id);
5090 5de5890b 2018-10-18 stsp return err;
5091 404c43c4 2018-06-21 stsp }
5092 404c43c4 2018-06-21 stsp
5093 5de5890b 2018-10-18 stsp static const struct got_error *
5094 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
5095 5de5890b 2018-10-18 stsp {
5096 5de5890b 2018-10-18 stsp const struct got_error *error;
5097 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
5098 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
5099 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
5100 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5101 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
5102 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
5103 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
5104 5de5890b 2018-10-18 stsp int ch;
5105 5de5890b 2018-10-18 stsp
5106 5de5890b 2018-10-18 stsp #ifndef PROFILE
5107 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5108 0f8d269b 2019-01-04 stsp NULL) == -1)
5109 5de5890b 2018-10-18 stsp err(1, "pledge");
5110 5de5890b 2018-10-18 stsp #endif
5111 5de5890b 2018-10-18 stsp
5112 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5113 5de5890b 2018-10-18 stsp switch (ch) {
5114 5de5890b 2018-10-18 stsp case 'c':
5115 5de5890b 2018-10-18 stsp commit_id_str = optarg;
5116 5de5890b 2018-10-18 stsp break;
5117 5de5890b 2018-10-18 stsp case 'r':
5118 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
5119 5de5890b 2018-10-18 stsp if (repo_path == NULL)
5120 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5121 9ba1d308 2019-10-21 stsp optarg);
5122 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5123 5de5890b 2018-10-18 stsp break;
5124 5de5890b 2018-10-18 stsp case 'i':
5125 5de5890b 2018-10-18 stsp show_ids = 1;
5126 5de5890b 2018-10-18 stsp break;
5127 c1669e2e 2019-01-09 stsp case 'R':
5128 c1669e2e 2019-01-09 stsp recurse = 1;
5129 c1669e2e 2019-01-09 stsp break;
5130 5de5890b 2018-10-18 stsp default:
5131 2deda0b9 2019-03-07 stsp usage_tree();
5132 5de5890b 2018-10-18 stsp /* NOTREACHED */
5133 5de5890b 2018-10-18 stsp }
5134 5de5890b 2018-10-18 stsp }
5135 5de5890b 2018-10-18 stsp
5136 5de5890b 2018-10-18 stsp argc -= optind;
5137 5de5890b 2018-10-18 stsp argv += optind;
5138 5de5890b 2018-10-18 stsp
5139 5de5890b 2018-10-18 stsp if (argc == 1)
5140 5de5890b 2018-10-18 stsp path = argv[0];
5141 5de5890b 2018-10-18 stsp else if (argc > 1)
5142 5de5890b 2018-10-18 stsp usage_tree();
5143 5de5890b 2018-10-18 stsp else
5144 7a2c19d6 2019-02-05 stsp path = NULL;
5145 7a2c19d6 2019-02-05 stsp
5146 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
5147 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
5148 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5149 9bf7a39b 2019-02-05 stsp goto done;
5150 9bf7a39b 2019-02-05 stsp }
5151 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
5152 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5153 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5154 7a2c19d6 2019-02-05 stsp goto done;
5155 7a2c19d6 2019-02-05 stsp else
5156 7a2c19d6 2019-02-05 stsp error = NULL;
5157 7a2c19d6 2019-02-05 stsp if (worktree) {
5158 7a2c19d6 2019-02-05 stsp repo_path =
5159 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5160 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
5161 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5162 7a2c19d6 2019-02-05 stsp if (error)
5163 7a2c19d6 2019-02-05 stsp goto done;
5164 7a2c19d6 2019-02-05 stsp } else {
5165 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
5166 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
5167 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5168 7a2c19d6 2019-02-05 stsp goto done;
5169 7a2c19d6 2019-02-05 stsp }
5170 5de5890b 2018-10-18 stsp }
5171 5de5890b 2018-10-18 stsp }
5172 5de5890b 2018-10-18 stsp
5173 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5174 5de5890b 2018-10-18 stsp if (error != NULL)
5175 c02c541e 2019-03-29 stsp goto done;
5176 c02c541e 2019-03-29 stsp
5177 4fedbf4c 2020-11-07 stsp if (worktree) {
5178 4fedbf4c 2020-11-07 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
5179 4fedbf4c 2020-11-07 stsp char *p;
5180 5de5890b 2018-10-18 stsp
5181 4fedbf4c 2020-11-07 stsp if (path == NULL)
5182 4fedbf4c 2020-11-07 stsp path = "";
5183 4fedbf4c 2020-11-07 stsp error = got_worktree_resolve_path(&p, worktree, path);
5184 4fedbf4c 2020-11-07 stsp if (error)
5185 4fedbf4c 2020-11-07 stsp goto done;
5186 4fedbf4c 2020-11-07 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
5187 4fedbf4c 2020-11-07 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5188 4fedbf4c 2020-11-07 stsp p) == -1) {
5189 4fedbf4c 2020-11-07 stsp error = got_error_from_errno("asprintf");
5190 9bf7a39b 2019-02-05 stsp free(p);
5191 4fedbf4c 2020-11-07 stsp goto done;
5192 4fedbf4c 2020-11-07 stsp }
5193 4fedbf4c 2020-11-07 stsp free(p);
5194 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5195 4fedbf4c 2020-11-07 stsp if (error)
5196 4fedbf4c 2020-11-07 stsp goto done;
5197 4fedbf4c 2020-11-07 stsp } else {
5198 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5199 4fedbf4c 2020-11-07 stsp if (error)
5200 4fedbf4c 2020-11-07 stsp goto done;
5201 4fedbf4c 2020-11-07 stsp if (path == NULL)
5202 9bf7a39b 2019-02-05 stsp path = "/";
5203 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
5204 9bf7a39b 2019-02-05 stsp if (error != NULL)
5205 9bf7a39b 2019-02-05 stsp goto done;
5206 9bf7a39b 2019-02-05 stsp }
5207 5de5890b 2018-10-18 stsp
5208 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
5209 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
5210 4e0a20a4 2020-03-23 tracey if (worktree)
5211 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
5212 4e0a20a4 2020-03-23 tracey else
5213 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
5214 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
5215 5de5890b 2018-10-18 stsp if (error != NULL)
5216 5de5890b 2018-10-18 stsp goto done;
5217 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5218 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
5219 5de5890b 2018-10-18 stsp if (error != NULL)
5220 5de5890b 2018-10-18 stsp goto done;
5221 5de5890b 2018-10-18 stsp } else {
5222 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5223 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5224 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5225 84de9106 2020-12-26 stsp NULL);
5226 84de9106 2020-12-26 stsp if (error)
5227 84de9106 2020-12-26 stsp goto done;
5228 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5229 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5230 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5231 30837e32 2019-07-25 stsp if (error)
5232 5de5890b 2018-10-18 stsp goto done;
5233 5de5890b 2018-10-18 stsp }
5234 5de5890b 2018-10-18 stsp
5235 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5236 c1669e2e 2019-01-09 stsp in_repo_path, repo);
5237 5de5890b 2018-10-18 stsp done:
5238 5de5890b 2018-10-18 stsp free(in_repo_path);
5239 5de5890b 2018-10-18 stsp free(repo_path);
5240 5de5890b 2018-10-18 stsp free(cwd);
5241 5de5890b 2018-10-18 stsp free(commit_id);
5242 7a2c19d6 2019-02-05 stsp if (worktree)
5243 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
5244 5de5890b 2018-10-18 stsp if (repo) {
5245 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5246 5de5890b 2018-10-18 stsp if (error == NULL)
5247 1d0f4054 2021-06-17 stsp error = close_err;
5248 5de5890b 2018-10-18 stsp }
5249 5de5890b 2018-10-18 stsp return error;
5250 5de5890b 2018-10-18 stsp }
5251 5de5890b 2018-10-18 stsp
5252 6bad629b 2019-02-04 stsp __dead static void
5253 6bad629b 2019-02-04 stsp usage_status(void)
5254 6bad629b 2019-02-04 stsp {
5255 00357e4d 2021-09-14 tracey fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5256 00357e4d 2021-09-14 tracey "[-S status-codes] [path ...]\n", getprogname());
5257 6bad629b 2019-02-04 stsp exit(1);
5258 6bad629b 2019-02-04 stsp }
5259 00357e4d 2021-09-14 tracey
5260 00357e4d 2021-09-14 tracey struct got_status_arg {
5261 00357e4d 2021-09-14 tracey char *status_codes;
5262 00357e4d 2021-09-14 tracey int suppress;
5263 00357e4d 2021-09-14 tracey };
5264 5c860e29 2018-03-12 stsp
5265 b72f483a 2019-02-05 stsp static const struct got_error *
5266 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
5267 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5268 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5269 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5270 6bad629b 2019-02-04 stsp {
5271 00357e4d 2021-09-14 tracey struct got_status_arg *st = arg;
5272 00357e4d 2021-09-14 tracey
5273 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5274 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
5275 00357e4d 2021-09-14 tracey if (st != NULL && st->status_codes) {
5276 00357e4d 2021-09-14 tracey size_t ncodes = strlen(st->status_codes);
5277 00357e4d 2021-09-14 tracey int i, j = 0;
5278 00357e4d 2021-09-14 tracey
5279 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
5280 00357e4d 2021-09-14 tracey if (st->suppress) {
5281 00357e4d 2021-09-14 tracey if (status == st->status_codes[i] ||
5282 00357e4d 2021-09-14 tracey staged_status == st->status_codes[i]) {
5283 00357e4d 2021-09-14 tracey j++;
5284 00357e4d 2021-09-14 tracey continue;
5285 00357e4d 2021-09-14 tracey }
5286 00357e4d 2021-09-14 tracey } else {
5287 00357e4d 2021-09-14 tracey if (status == st->status_codes[i] ||
5288 00357e4d 2021-09-14 tracey staged_status == st->status_codes[i])
5289 00357e4d 2021-09-14 tracey break;
5290 00357e4d 2021-09-14 tracey }
5291 081470ac 2020-08-13 stsp }
5292 00357e4d 2021-09-14 tracey
5293 00357e4d 2021-09-14 tracey if (st->suppress && j == 0)
5294 00357e4d 2021-09-14 tracey goto print;
5295 00357e4d 2021-09-14 tracey
5296 081470ac 2020-08-13 stsp if (i == ncodes)
5297 081470ac 2020-08-13 stsp return NULL;
5298 081470ac 2020-08-13 stsp }
5299 00357e4d 2021-09-14 tracey print:
5300 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
5301 b72f483a 2019-02-05 stsp return NULL;
5302 6bad629b 2019-02-04 stsp }
5303 5c860e29 2018-03-12 stsp
5304 6bad629b 2019-02-04 stsp static const struct got_error *
5305 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
5306 6bad629b 2019-02-04 stsp {
5307 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
5308 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
5309 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
5310 00357e4d 2021-09-14 tracey struct got_status_arg st;
5311 00357e4d 2021-09-14 tracey char *cwd = NULL;
5312 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
5313 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
5314 f6343036 2021-06-22 stsp int ch, i, no_ignores = 0;
5315 5c860e29 2018-03-12 stsp
5316 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
5317 72ea6654 2019-07-27 stsp
5318 00357e4d 2021-09-14 tracey memset(&st, 0, sizeof(st));
5319 00357e4d 2021-09-14 tracey st.status_codes = NULL;
5320 00357e4d 2021-09-14 tracey st.suppress = 0;
5321 00357e4d 2021-09-14 tracey
5322 00357e4d 2021-09-14 tracey while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
5323 6bad629b 2019-02-04 stsp switch (ch) {
5324 f6343036 2021-06-22 stsp case 'I':
5325 f6343036 2021-06-22 stsp no_ignores = 1;
5326 f6343036 2021-06-22 stsp break;
5327 788d4a19 2021-09-14 stsp case 'S':
5328 788d4a19 2021-09-14 stsp if (st.status_codes != NULL && st.suppress == 0)
5329 788d4a19 2021-09-14 stsp option_conflict('S', 's');
5330 788d4a19 2021-09-14 stsp st.suppress = 1;
5331 788d4a19 2021-09-14 stsp /* fallthrough */
5332 081470ac 2020-08-13 stsp case 's':
5333 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
5334 081470ac 2020-08-13 stsp switch (optarg[i]) {
5335 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
5336 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
5337 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
5338 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
5339 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
5340 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
5341 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
5342 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
5343 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
5344 081470ac 2020-08-13 stsp break;
5345 081470ac 2020-08-13 stsp default:
5346 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
5347 081470ac 2020-08-13 stsp optarg[i]);
5348 081470ac 2020-08-13 stsp }
5349 081470ac 2020-08-13 stsp }
5350 788d4a19 2021-09-14 stsp if (ch == 's' && st.suppress)
5351 b043307b 2021-09-14 stsp option_conflict('s', 'S');
5352 00357e4d 2021-09-14 tracey st.status_codes = optarg;
5353 081470ac 2020-08-13 stsp break;
5354 5c860e29 2018-03-12 stsp default:
5355 2deda0b9 2019-03-07 stsp usage_status();
5356 6bad629b 2019-02-04 stsp /* NOTREACHED */
5357 5c860e29 2018-03-12 stsp }
5358 5c860e29 2018-03-12 stsp }
5359 5c860e29 2018-03-12 stsp
5360 6bad629b 2019-02-04 stsp argc -= optind;
5361 6bad629b 2019-02-04 stsp argv += optind;
5362 5c860e29 2018-03-12 stsp
5363 6bad629b 2019-02-04 stsp #ifndef PROFILE
5364 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5365 6bad629b 2019-02-04 stsp NULL) == -1)
5366 6bad629b 2019-02-04 stsp err(1, "pledge");
5367 f42b1b34 2018-03-12 stsp #endif
5368 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
5369 927df6b7 2019-02-10 stsp if (cwd == NULL) {
5370 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5371 927df6b7 2019-02-10 stsp goto done;
5372 927df6b7 2019-02-10 stsp }
5373 927df6b7 2019-02-10 stsp
5374 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
5375 fa51e947 2020-03-27 stsp if (error) {
5376 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5377 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
5378 a5edda0a 2019-07-27 stsp goto done;
5379 fa51e947 2020-03-27 stsp }
5380 6bad629b 2019-02-04 stsp
5381 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5382 c9956ddf 2019-09-08 stsp NULL);
5383 6bad629b 2019-02-04 stsp if (error != NULL)
5384 6bad629b 2019-02-04 stsp goto done;
5385 6bad629b 2019-02-04 stsp
5386 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5387 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5388 087fb88c 2019-08-04 stsp if (error)
5389 087fb88c 2019-08-04 stsp goto done;
5390 087fb88c 2019-08-04 stsp
5391 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5392 6bad629b 2019-02-04 stsp if (error)
5393 6bad629b 2019-02-04 stsp goto done;
5394 6bad629b 2019-02-04 stsp
5395 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, no_ignores,
5396 00357e4d 2021-09-14 tracey print_status, &st, check_cancelled, NULL);
5397 6bad629b 2019-02-04 stsp done:
5398 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
5399 a5edda0a 2019-07-27 stsp free((char *)pe->path);
5400 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
5401 927df6b7 2019-02-10 stsp free(cwd);
5402 d0eebce4 2019-03-11 stsp return error;
5403 d0eebce4 2019-03-11 stsp }
5404 d0eebce4 2019-03-11 stsp
5405 d0eebce4 2019-03-11 stsp __dead static void
5406 d0eebce4 2019-03-11 stsp usage_ref(void)
5407 d0eebce4 2019-03-11 stsp {
5408 d0eebce4 2019-03-11 stsp fprintf(stderr,
5409 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5410 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
5411 d0eebce4 2019-03-11 stsp getprogname());
5412 d0eebce4 2019-03-11 stsp exit(1);
5413 d0eebce4 2019-03-11 stsp }
5414 d0eebce4 2019-03-11 stsp
5415 d0eebce4 2019-03-11 stsp static const struct got_error *
5416 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
5417 d0eebce4 2019-03-11 stsp {
5418 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
5419 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
5420 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
5421 d0eebce4 2019-03-11 stsp
5422 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5423 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5424 d0eebce4 2019-03-11 stsp if (err)
5425 d0eebce4 2019-03-11 stsp return err;
5426 d0eebce4 2019-03-11 stsp
5427 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
5428 d0eebce4 2019-03-11 stsp char *refstr;
5429 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
5430 d0eebce4 2019-03-11 stsp if (refstr == NULL)
5431 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
5432 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5433 d0eebce4 2019-03-11 stsp free(refstr);
5434 d0eebce4 2019-03-11 stsp }
5435 d0eebce4 2019-03-11 stsp
5436 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5437 d0eebce4 2019-03-11 stsp return NULL;
5438 d0eebce4 2019-03-11 stsp }
5439 d0eebce4 2019-03-11 stsp
5440 d0eebce4 2019-03-11 stsp static const struct got_error *
5441 161728eb 2021-07-24 stsp delete_ref_by_name(struct got_repository *repo, const char *refname)
5442 d0eebce4 2019-03-11 stsp {
5443 161728eb 2021-07-24 stsp const struct got_error *err;
5444 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5445 d0eebce4 2019-03-11 stsp
5446 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5447 d0eebce4 2019-03-11 stsp if (err)
5448 d0eebce4 2019-03-11 stsp return err;
5449 993f033b 2021-07-16 stsp
5450 161728eb 2021-07-24 stsp err = delete_ref(repo, ref);
5451 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5452 d0eebce4 2019-03-11 stsp return err;
5453 d0eebce4 2019-03-11 stsp }
5454 d0eebce4 2019-03-11 stsp
5455 d0eebce4 2019-03-11 stsp static const struct got_error *
5456 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
5457 d0eebce4 2019-03-11 stsp {
5458 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5459 d0eebce4 2019-03-11 stsp struct got_object_id *id;
5460 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
5461 d1644381 2019-07-14 stsp
5462 d1644381 2019-07-14 stsp /*
5463 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5464 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
5465 d1644381 2019-07-14 stsp * an unintended typo.
5466 d1644381 2019-07-14 stsp */
5467 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5468 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5469 d0eebce4 2019-03-11 stsp
5470 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5471 dd88155e 2019-06-29 stsp repo);
5472 d83d9d5c 2019-05-13 stsp if (err) {
5473 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
5474 d0eebce4 2019-03-11 stsp
5475 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5476 d83d9d5c 2019-05-13 stsp return err;
5477 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
5478 d83d9d5c 2019-05-13 stsp if (err)
5479 d83d9d5c 2019-05-13 stsp return err;
5480 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
5481 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
5482 d83d9d5c 2019-05-13 stsp if (err)
5483 d83d9d5c 2019-05-13 stsp return err;
5484 d83d9d5c 2019-05-13 stsp }
5485 d83d9d5c 2019-05-13 stsp
5486 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5487 d0eebce4 2019-03-11 stsp if (err)
5488 d0eebce4 2019-03-11 stsp goto done;
5489 d0eebce4 2019-03-11 stsp
5490 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5491 d0eebce4 2019-03-11 stsp done:
5492 d0eebce4 2019-03-11 stsp if (ref)
5493 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5494 d0eebce4 2019-03-11 stsp free(id);
5495 d1c1ae5f 2019-08-12 stsp return err;
5496 d1c1ae5f 2019-08-12 stsp }
5497 d1c1ae5f 2019-08-12 stsp
5498 d1c1ae5f 2019-08-12 stsp static const struct got_error *
5499 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
5500 d1c1ae5f 2019-08-12 stsp {
5501 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
5502 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
5503 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
5504 d1c1ae5f 2019-08-12 stsp
5505 d1c1ae5f 2019-08-12 stsp /*
5506 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5507 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
5508 d1c1ae5f 2019-08-12 stsp * an unintended typo.
5509 d1c1ae5f 2019-08-12 stsp */
5510 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5511 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5512 d1c1ae5f 2019-08-12 stsp
5513 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5514 d1c1ae5f 2019-08-12 stsp if (err)
5515 d1c1ae5f 2019-08-12 stsp return err;
5516 d1c1ae5f 2019-08-12 stsp
5517 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5518 d1c1ae5f 2019-08-12 stsp if (err)
5519 d1c1ae5f 2019-08-12 stsp goto done;
5520 d1c1ae5f 2019-08-12 stsp
5521 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5522 d1c1ae5f 2019-08-12 stsp done:
5523 d1c1ae5f 2019-08-12 stsp if (target_ref)
5524 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5525 d1c1ae5f 2019-08-12 stsp if (ref)
5526 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5527 d0eebce4 2019-03-11 stsp return err;
5528 d0eebce4 2019-03-11 stsp }
5529 d0eebce4 2019-03-11 stsp
5530 d0eebce4 2019-03-11 stsp static const struct got_error *
5531 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5532 d0eebce4 2019-03-11 stsp {
5533 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
5534 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
5535 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
5536 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
5537 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
5538 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
5539 b2070a3f 2020-03-22 stsp char *refname = NULL;
5540 d0eebce4 2019-03-11 stsp
5541 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5542 d0eebce4 2019-03-11 stsp switch (ch) {
5543 e31abbf2 2020-03-22 stsp case 'c':
5544 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5545 e31abbf2 2020-03-22 stsp break;
5546 d0eebce4 2019-03-11 stsp case 'd':
5547 e31abbf2 2020-03-22 stsp do_delete = 1;
5548 d0eebce4 2019-03-11 stsp break;
5549 d0eebce4 2019-03-11 stsp case 'r':
5550 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5551 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5552 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5553 9ba1d308 2019-10-21 stsp optarg);
5554 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5555 d0eebce4 2019-03-11 stsp break;
5556 d0eebce4 2019-03-11 stsp case 'l':
5557 d0eebce4 2019-03-11 stsp do_list = 1;
5558 d1c1ae5f 2019-08-12 stsp break;
5559 d1c1ae5f 2019-08-12 stsp case 's':
5560 e31abbf2 2020-03-22 stsp symref_target = optarg;
5561 d0eebce4 2019-03-11 stsp break;
5562 d0eebce4 2019-03-11 stsp default:
5563 d0eebce4 2019-03-11 stsp usage_ref();
5564 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5565 d0eebce4 2019-03-11 stsp }
5566 d0eebce4 2019-03-11 stsp }
5567 d0eebce4 2019-03-11 stsp
5568 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5569 ff69268e 2020-12-13 stsp option_conflict('c', 'l');
5570 ff69268e 2020-12-13 stsp if (obj_arg && do_delete)
5571 ff69268e 2020-12-13 stsp option_conflict('c', 'd');
5572 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5573 ff69268e 2020-12-13 stsp option_conflict('c', 's');
5574 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5575 ff69268e 2020-12-13 stsp option_conflict('s', 'd');
5576 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5577 ff69268e 2020-12-13 stsp option_conflict('s', 'l');
5578 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5579 ff69268e 2020-12-13 stsp option_conflict('d', 'l');
5580 d0eebce4 2019-03-11 stsp
5581 d0eebce4 2019-03-11 stsp argc -= optind;
5582 d0eebce4 2019-03-11 stsp argv += optind;
5583 d0eebce4 2019-03-11 stsp
5584 e31abbf2 2020-03-22 stsp if (do_list) {
5585 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5586 d0eebce4 2019-03-11 stsp usage_ref();
5587 b2070a3f 2020-03-22 stsp if (argc == 1) {
5588 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5589 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5590 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5591 b2070a3f 2020-03-22 stsp goto done;
5592 b2070a3f 2020-03-22 stsp }
5593 b2070a3f 2020-03-22 stsp }
5594 e31abbf2 2020-03-22 stsp } else {
5595 e31abbf2 2020-03-22 stsp if (argc != 1)
5596 e31abbf2 2020-03-22 stsp usage_ref();
5597 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5598 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5599 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5600 b2070a3f 2020-03-22 stsp goto done;
5601 b2070a3f 2020-03-22 stsp }
5602 e31abbf2 2020-03-22 stsp }
5603 b2070a3f 2020-03-22 stsp
5604 b2070a3f 2020-03-22 stsp if (refname)
5605 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5606 d0eebce4 2019-03-11 stsp
5607 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5608 e0b57350 2019-03-12 stsp if (do_list) {
5609 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5610 e0b57350 2019-03-12 stsp NULL) == -1)
5611 e0b57350 2019-03-12 stsp err(1, "pledge");
5612 e0b57350 2019-03-12 stsp } else {
5613 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5614 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5615 e0b57350 2019-03-12 stsp err(1, "pledge");
5616 e0b57350 2019-03-12 stsp }
5617 d0eebce4 2019-03-11 stsp #endif
5618 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5619 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5620 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5621 d0eebce4 2019-03-11 stsp goto done;
5622 d0eebce4 2019-03-11 stsp }
5623 d0eebce4 2019-03-11 stsp
5624 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5625 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5626 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5627 d0eebce4 2019-03-11 stsp goto done;
5628 d0eebce4 2019-03-11 stsp else
5629 d0eebce4 2019-03-11 stsp error = NULL;
5630 d0eebce4 2019-03-11 stsp if (worktree) {
5631 d0eebce4 2019-03-11 stsp repo_path =
5632 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5633 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5634 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5635 d0eebce4 2019-03-11 stsp if (error)
5636 d0eebce4 2019-03-11 stsp goto done;
5637 d0eebce4 2019-03-11 stsp } else {
5638 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5639 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5640 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5641 d0eebce4 2019-03-11 stsp goto done;
5642 d0eebce4 2019-03-11 stsp }
5643 d0eebce4 2019-03-11 stsp }
5644 d0eebce4 2019-03-11 stsp }
5645 d0eebce4 2019-03-11 stsp
5646 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5647 d0eebce4 2019-03-11 stsp if (error != NULL)
5648 d0eebce4 2019-03-11 stsp goto done;
5649 d0eebce4 2019-03-11 stsp
5650 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5651 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5652 c02c541e 2019-03-29 stsp if (error)
5653 c02c541e 2019-03-29 stsp goto done;
5654 c02c541e 2019-03-29 stsp
5655 d0eebce4 2019-03-11 stsp if (do_list)
5656 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5657 e31abbf2 2020-03-22 stsp else if (do_delete)
5658 161728eb 2021-07-24 stsp error = delete_ref_by_name(repo, refname);
5659 e31abbf2 2020-03-22 stsp else if (symref_target)
5660 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5661 e31abbf2 2020-03-22 stsp else {
5662 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5663 e31abbf2 2020-03-22 stsp usage_ref();
5664 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5665 e31abbf2 2020-03-22 stsp }
5666 4e759de4 2019-06-26 stsp done:
5667 b2070a3f 2020-03-22 stsp free(refname);
5668 1d0f4054 2021-06-17 stsp if (repo) {
5669 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5670 1d0f4054 2021-06-17 stsp if (error == NULL)
5671 1d0f4054 2021-06-17 stsp error = close_err;
5672 1d0f4054 2021-06-17 stsp }
5673 4e759de4 2019-06-26 stsp if (worktree)
5674 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5675 4e759de4 2019-06-26 stsp free(cwd);
5676 4e759de4 2019-06-26 stsp free(repo_path);
5677 4e759de4 2019-06-26 stsp return error;
5678 4e759de4 2019-06-26 stsp }
5679 4e759de4 2019-06-26 stsp
5680 4e759de4 2019-06-26 stsp __dead static void
5681 4e759de4 2019-06-26 stsp usage_branch(void)
5682 4e759de4 2019-06-26 stsp {
5683 4e759de4 2019-06-26 stsp fprintf(stderr,
5684 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5685 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5686 4e759de4 2019-06-26 stsp exit(1);
5687 4e759de4 2019-06-26 stsp }
5688 4e759de4 2019-06-26 stsp
5689 4e759de4 2019-06-26 stsp static const struct got_error *
5690 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5691 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5692 4e99b47e 2019-10-04 stsp {
5693 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5694 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5695 4e99b47e 2019-10-04 stsp char *refstr;
5696 4e99b47e 2019-10-04 stsp
5697 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5698 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5699 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5700 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5701 4e99b47e 2019-10-04 stsp
5702 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5703 4e99b47e 2019-10-04 stsp if (err)
5704 4e99b47e 2019-10-04 stsp return err;
5705 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5706 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5707 4e99b47e 2019-10-04 stsp marker = "* ";
5708 4e99b47e 2019-10-04 stsp else
5709 4e99b47e 2019-10-04 stsp marker = "~ ";
5710 4e99b47e 2019-10-04 stsp free(id);
5711 4e99b47e 2019-10-04 stsp }
5712 4e99b47e 2019-10-04 stsp
5713 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5714 4e99b47e 2019-10-04 stsp refname += 11;
5715 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5716 4e99b47e 2019-10-04 stsp refname += 18;
5717 34d4e04c 2021-02-08 stsp if (strncmp(refname, "refs/remotes/", 13) == 0)
5718 34d4e04c 2021-02-08 stsp refname += 13;
5719 4e99b47e 2019-10-04 stsp
5720 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5721 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5722 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5723 4e99b47e 2019-10-04 stsp
5724 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5725 4e99b47e 2019-10-04 stsp free(refstr);
5726 4e99b47e 2019-10-04 stsp return NULL;
5727 4e99b47e 2019-10-04 stsp }
5728 4e99b47e 2019-10-04 stsp
5729 4e99b47e 2019-10-04 stsp static const struct got_error *
5730 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5731 ad89fa31 2019-10-04 stsp {
5732 ad89fa31 2019-10-04 stsp const char *refname;
5733 ad89fa31 2019-10-04 stsp
5734 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5735 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5736 ad89fa31 2019-10-04 stsp
5737 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5738 ad89fa31 2019-10-04 stsp
5739 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5740 ad89fa31 2019-10-04 stsp refname += 11;
5741 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5742 ad89fa31 2019-10-04 stsp refname += 18;
5743 ad89fa31 2019-10-04 stsp
5744 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5745 ad89fa31 2019-10-04 stsp
5746 ad89fa31 2019-10-04 stsp return NULL;
5747 ad89fa31 2019-10-04 stsp }
5748 ad89fa31 2019-10-04 stsp
5749 ad89fa31 2019-10-04 stsp static const struct got_error *
5750 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5751 4e759de4 2019-06-26 stsp {
5752 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5753 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5754 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5755 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5756 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5757 4e759de4 2019-06-26 stsp
5758 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5759 ba882ee3 2019-07-11 stsp
5760 4e99b47e 2019-10-04 stsp if (worktree) {
5761 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5762 4e99b47e 2019-10-04 stsp worktree);
5763 4e99b47e 2019-10-04 stsp if (err)
5764 4e99b47e 2019-10-04 stsp return err;
5765 4e99b47e 2019-10-04 stsp
5766 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5767 4e99b47e 2019-10-04 stsp worktree);
5768 4e99b47e 2019-10-04 stsp if (err)
5769 4e99b47e 2019-10-04 stsp return err;
5770 4e99b47e 2019-10-04 stsp
5771 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5772 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5773 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5774 4e99b47e 2019-10-04 stsp if (err)
5775 4e99b47e 2019-10-04 stsp return err;
5776 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5777 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5778 4e99b47e 2019-10-04 stsp }
5779 4e99b47e 2019-10-04 stsp }
5780 4e99b47e 2019-10-04 stsp
5781 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5782 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5783 4e759de4 2019-06-26 stsp if (err)
5784 4e759de4 2019-06-26 stsp return err;
5785 4e759de4 2019-06-26 stsp
5786 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry)
5787 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5788 4e759de4 2019-06-26 stsp
5789 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5790 34d4e04c 2021-02-08 stsp
5791 34d4e04c 2021-02-08 stsp err = got_ref_list(&refs, repo, "refs/remotes",
5792 34d4e04c 2021-02-08 stsp got_ref_cmp_by_name, NULL);
5793 34d4e04c 2021-02-08 stsp if (err)
5794 34d4e04c 2021-02-08 stsp return err;
5795 34d4e04c 2021-02-08 stsp
5796 34d4e04c 2021-02-08 stsp TAILQ_FOREACH(re, &refs, entry)
5797 34d4e04c 2021-02-08 stsp list_branch(repo, worktree, re->ref);
5798 34d4e04c 2021-02-08 stsp
5799 34d4e04c 2021-02-08 stsp got_ref_list_free(&refs);
5800 34d4e04c 2021-02-08 stsp
5801 4e759de4 2019-06-26 stsp return NULL;
5802 4e759de4 2019-06-26 stsp }
5803 4e759de4 2019-06-26 stsp
5804 4e759de4 2019-06-26 stsp static const struct got_error *
5805 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5806 45cd4e47 2019-08-25 stsp const char *branch_name)
5807 4e759de4 2019-06-26 stsp {
5808 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5809 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5810 2f1457c6 2021-08-27 stsp char *refname, *remote_refname = NULL;
5811 4e759de4 2019-06-26 stsp
5812 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "refs/", 5) == 0)
5813 2f1457c6 2021-08-27 stsp branch_name += 5;
5814 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "heads/", 6) == 0)
5815 2f1457c6 2021-08-27 stsp branch_name += 6;
5816 2f1457c6 2021-08-27 stsp else if (strncmp(branch_name, "remotes/", 8) == 0)
5817 2f1457c6 2021-08-27 stsp branch_name += 8;
5818 2f1457c6 2021-08-27 stsp
5819 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5820 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5821 4e759de4 2019-06-26 stsp
5822 2f1457c6 2021-08-27 stsp if (asprintf(&remote_refname, "refs/remotes/%s",
5823 2f1457c6 2021-08-27 stsp branch_name) == -1) {
5824 2f1457c6 2021-08-27 stsp err = got_error_from_errno("asprintf");
5825 4e759de4 2019-06-26 stsp goto done;
5826 2f1457c6 2021-08-27 stsp }
5827 4e759de4 2019-06-26 stsp
5828 2f1457c6 2021-08-27 stsp err = got_ref_open(&ref, repo, refname, 0);
5829 2f1457c6 2021-08-27 stsp if (err) {
5830 2f1457c6 2021-08-27 stsp const struct got_error *err2;
5831 2f1457c6 2021-08-27 stsp if (err->code != GOT_ERR_NOT_REF)
5832 2f1457c6 2021-08-27 stsp goto done;
5833 2f1457c6 2021-08-27 stsp /*
5834 2f1457c6 2021-08-27 stsp * Keep 'err' intact such that if neither branch exists
5835 2f1457c6 2021-08-27 stsp * we report "refs/heads" rather than "refs/remotes" in
5836 2f1457c6 2021-08-27 stsp * our error message.
5837 2f1457c6 2021-08-27 stsp */
5838 2f1457c6 2021-08-27 stsp err2 = got_ref_open(&ref, repo, remote_refname, 0);
5839 2f1457c6 2021-08-27 stsp if (err2)
5840 2f1457c6 2021-08-27 stsp goto done;
5841 2f1457c6 2021-08-27 stsp err = NULL;
5842 2f1457c6 2021-08-27 stsp }
5843 2f1457c6 2021-08-27 stsp
5844 45cd4e47 2019-08-25 stsp if (worktree &&
5845 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5846 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5847 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5848 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5849 45cd4e47 2019-08-25 stsp goto done;
5850 45cd4e47 2019-08-25 stsp }
5851 45cd4e47 2019-08-25 stsp
5852 978a28a1 2021-09-04 naddy err = delete_ref(repo, ref);
5853 4e759de4 2019-06-26 stsp done:
5854 45cd4e47 2019-08-25 stsp if (ref)
5855 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5856 4e759de4 2019-06-26 stsp free(refname);
5857 2f1457c6 2021-08-27 stsp free(remote_refname);
5858 4e759de4 2019-06-26 stsp return err;
5859 4e759de4 2019-06-26 stsp }
5860 4e759de4 2019-06-26 stsp
5861 4e759de4 2019-06-26 stsp static const struct got_error *
5862 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5863 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5864 4e759de4 2019-06-26 stsp {
5865 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5866 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5867 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5868 d3f84d51 2019-07-11 stsp
5869 d3f84d51 2019-07-11 stsp /*
5870 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5871 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5872 d3f84d51 2019-07-11 stsp * an unintended typo.
5873 d3f84d51 2019-07-11 stsp */
5874 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5875 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5876 2f1457c6 2021-08-27 stsp
5877 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
5878 2f1457c6 2021-08-27 stsp branch_name += 11;
5879 4e759de4 2019-06-26 stsp
5880 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5881 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
5882 62d463ca 2020-10-20 naddy goto done;
5883 4e759de4 2019-06-26 stsp }
5884 4e759de4 2019-06-26 stsp
5885 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5886 4e759de4 2019-06-26 stsp if (err == NULL) {
5887 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5888 4e759de4 2019-06-26 stsp goto done;
5889 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5890 4e759de4 2019-06-26 stsp goto done;
5891 4e759de4 2019-06-26 stsp
5892 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5893 4e759de4 2019-06-26 stsp if (err)
5894 4e759de4 2019-06-26 stsp goto done;
5895 4e759de4 2019-06-26 stsp
5896 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5897 d0eebce4 2019-03-11 stsp done:
5898 4e759de4 2019-06-26 stsp if (ref)
5899 4e759de4 2019-06-26 stsp got_ref_close(ref);
5900 4e759de4 2019-06-26 stsp free(base_refname);
5901 4e759de4 2019-06-26 stsp free(refname);
5902 4e759de4 2019-06-26 stsp return err;
5903 4e759de4 2019-06-26 stsp }
5904 4e759de4 2019-06-26 stsp
5905 4e759de4 2019-06-26 stsp static const struct got_error *
5906 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5907 4e759de4 2019-06-26 stsp {
5908 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5909 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5910 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5911 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5912 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5913 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5914 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5915 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5916 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5917 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5918 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5919 4e759de4 2019-06-26 stsp
5920 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5921 da76fce2 2020-02-24 stsp
5922 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5923 4e759de4 2019-06-26 stsp switch (ch) {
5924 a74f7e83 2019-11-10 stsp case 'c':
5925 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5926 a74f7e83 2019-11-10 stsp break;
5927 4e759de4 2019-06-26 stsp case 'd':
5928 4e759de4 2019-06-26 stsp delref = optarg;
5929 4e759de4 2019-06-26 stsp break;
5930 4e759de4 2019-06-26 stsp case 'r':
5931 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5932 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5933 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5934 9ba1d308 2019-10-21 stsp optarg);
5935 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5936 4e759de4 2019-06-26 stsp break;
5937 4e759de4 2019-06-26 stsp case 'l':
5938 4e759de4 2019-06-26 stsp do_list = 1;
5939 da76fce2 2020-02-24 stsp break;
5940 da76fce2 2020-02-24 stsp case 'n':
5941 da76fce2 2020-02-24 stsp do_update = 0;
5942 4e759de4 2019-06-26 stsp break;
5943 4e759de4 2019-06-26 stsp default:
5944 4e759de4 2019-06-26 stsp usage_branch();
5945 4e759de4 2019-06-26 stsp /* NOTREACHED */
5946 4e759de4 2019-06-26 stsp }
5947 4e759de4 2019-06-26 stsp }
5948 4e759de4 2019-06-26 stsp
5949 4e759de4 2019-06-26 stsp if (do_list && delref)
5950 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
5951 4e759de4 2019-06-26 stsp
5952 4e759de4 2019-06-26 stsp argc -= optind;
5953 4e759de4 2019-06-26 stsp argv += optind;
5954 ad89fa31 2019-10-04 stsp
5955 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5956 ad89fa31 2019-10-04 stsp do_show = 1;
5957 4e759de4 2019-06-26 stsp
5958 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5959 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5960 a74f7e83 2019-11-10 stsp
5961 4e759de4 2019-06-26 stsp if (do_list || delref) {
5962 4e759de4 2019-06-26 stsp if (argc > 0)
5963 4e759de4 2019-06-26 stsp usage_branch();
5964 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5965 4e759de4 2019-06-26 stsp usage_branch();
5966 4e759de4 2019-06-26 stsp
5967 4e759de4 2019-06-26 stsp #ifndef PROFILE
5968 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5969 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5970 4e759de4 2019-06-26 stsp NULL) == -1)
5971 4e759de4 2019-06-26 stsp err(1, "pledge");
5972 4e759de4 2019-06-26 stsp } else {
5973 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5974 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5975 4e759de4 2019-06-26 stsp err(1, "pledge");
5976 4e759de4 2019-06-26 stsp }
5977 4e759de4 2019-06-26 stsp #endif
5978 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5979 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5980 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5981 4e759de4 2019-06-26 stsp goto done;
5982 4e759de4 2019-06-26 stsp }
5983 4e759de4 2019-06-26 stsp
5984 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5985 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5986 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5987 4e759de4 2019-06-26 stsp goto done;
5988 4e759de4 2019-06-26 stsp else
5989 4e759de4 2019-06-26 stsp error = NULL;
5990 4e759de4 2019-06-26 stsp if (worktree) {
5991 4e759de4 2019-06-26 stsp repo_path =
5992 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5993 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5994 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5995 4e759de4 2019-06-26 stsp if (error)
5996 4e759de4 2019-06-26 stsp goto done;
5997 4e759de4 2019-06-26 stsp } else {
5998 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5999 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
6000 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
6001 4e759de4 2019-06-26 stsp goto done;
6002 4e759de4 2019-06-26 stsp }
6003 4e759de4 2019-06-26 stsp }
6004 4e759de4 2019-06-26 stsp }
6005 4e759de4 2019-06-26 stsp
6006 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6007 4e759de4 2019-06-26 stsp if (error != NULL)
6008 4e759de4 2019-06-26 stsp goto done;
6009 4e759de4 2019-06-26 stsp
6010 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
6011 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
6012 4e759de4 2019-06-26 stsp if (error)
6013 4e759de4 2019-06-26 stsp goto done;
6014 4e759de4 2019-06-26 stsp
6015 ad89fa31 2019-10-04 stsp if (do_show)
6016 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
6017 ad89fa31 2019-10-04 stsp else if (do_list)
6018 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
6019 4e759de4 2019-06-26 stsp else if (delref)
6020 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
6021 4e759de4 2019-06-26 stsp else {
6022 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6023 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6024 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6025 84de9106 2020-12-26 stsp NULL);
6026 84de9106 2020-12-26 stsp if (error)
6027 84de9106 2020-12-26 stsp goto done;
6028 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
6029 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
6030 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
6031 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
6032 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6033 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6034 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
6035 a74f7e83 2019-11-10 stsp if (error)
6036 a74f7e83 2019-11-10 stsp goto done;
6037 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
6038 da76fce2 2020-02-24 stsp if (error)
6039 da76fce2 2020-02-24 stsp goto done;
6040 da76fce2 2020-02-24 stsp if (worktree && do_update) {
6041 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6042 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
6043 da76fce2 2020-02-24 stsp
6044 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
6045 da76fce2 2020-02-24 stsp if (error)
6046 da76fce2 2020-02-24 stsp goto done;
6047 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
6048 da76fce2 2020-02-24 stsp worktree);
6049 da76fce2 2020-02-24 stsp if (error)
6050 da76fce2 2020-02-24 stsp goto done;
6051 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6052 da76fce2 2020-02-24 stsp == -1) {
6053 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
6054 da76fce2 2020-02-24 stsp goto done;
6055 da76fce2 2020-02-24 stsp }
6056 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
6057 da76fce2 2020-02-24 stsp free(branch_refname);
6058 da76fce2 2020-02-24 stsp if (error)
6059 da76fce2 2020-02-24 stsp goto done;
6060 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
6061 da76fce2 2020-02-24 stsp repo);
6062 da76fce2 2020-02-24 stsp if (error)
6063 da76fce2 2020-02-24 stsp goto done;
6064 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
6065 da76fce2 2020-02-24 stsp commit_id);
6066 da76fce2 2020-02-24 stsp if (error)
6067 da76fce2 2020-02-24 stsp goto done;
6068 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6069 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
6070 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
6071 9627c110 2020-04-18 stsp NULL);
6072 da76fce2 2020-02-24 stsp if (error)
6073 da76fce2 2020-02-24 stsp goto done;
6074 4f3c844b 2021-09-14 stsp if (upa.did_something) {
6075 4f3c844b 2021-09-14 stsp printf("Updated to %s: %s\n",
6076 4f3c844b 2021-09-14 stsp got_worktree_get_head_ref_name(worktree),
6077 4f3c844b 2021-09-14 stsp commit_id_str);
6078 4f3c844b 2021-09-14 stsp }
6079 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6080 da76fce2 2020-02-24 stsp }
6081 4e759de4 2019-06-26 stsp }
6082 4e759de4 2019-06-26 stsp done:
6083 da76fce2 2020-02-24 stsp if (ref)
6084 da76fce2 2020-02-24 stsp got_ref_close(ref);
6085 1d0f4054 2021-06-17 stsp if (repo) {
6086 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6087 1d0f4054 2021-06-17 stsp if (error == NULL)
6088 1d0f4054 2021-06-17 stsp error = close_err;
6089 1d0f4054 2021-06-17 stsp }
6090 d0eebce4 2019-03-11 stsp if (worktree)
6091 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
6092 d0eebce4 2019-03-11 stsp free(cwd);
6093 d0eebce4 2019-03-11 stsp free(repo_path);
6094 da76fce2 2020-02-24 stsp free(commit_id);
6095 da76fce2 2020-02-24 stsp free(commit_id_str);
6096 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
6097 da76fce2 2020-02-24 stsp free((char *)pe->path);
6098 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
6099 d00136be 2019-03-26 stsp return error;
6100 d00136be 2019-03-26 stsp }
6101 d00136be 2019-03-26 stsp
6102 8e7bd50a 2019-08-22 stsp
6103 d00136be 2019-03-26 stsp __dead static void
6104 8e7bd50a 2019-08-22 stsp usage_tag(void)
6105 8e7bd50a 2019-08-22 stsp {
6106 8e7bd50a 2019-08-22 stsp fprintf(stderr,
6107 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
6108 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
6109 8e7bd50a 2019-08-22 stsp exit(1);
6110 b8bad2ba 2019-08-23 stsp }
6111 b8bad2ba 2019-08-23 stsp
6112 b8bad2ba 2019-08-23 stsp #if 0
6113 b8bad2ba 2019-08-23 stsp static const struct got_error *
6114 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6115 b8bad2ba 2019-08-23 stsp {
6116 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
6117 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
6118 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
6119 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
6120 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
6121 b8bad2ba 2019-08-23 stsp
6122 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(re, tags, entry) {
6123 dbdddfee 2021-06-23 naddy se = STAILQ_FIRST(sorted);
6124 b8bad2ba 2019-08-23 stsp if (se == NULL) {
6125 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6126 b8bad2ba 2019-08-23 stsp if (err)
6127 b8bad2ba 2019-08-23 stsp return err;
6128 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(sorted, new, entry);
6129 b8bad2ba 2019-08-23 stsp continue;
6130 b8bad2ba 2019-08-23 stsp } else {
6131 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
6132 b8bad2ba 2019-08-23 stsp if (err)
6133 b8bad2ba 2019-08-23 stsp break;
6134 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
6135 b8bad2ba 2019-08-23 stsp free(re_id);
6136 b8bad2ba 2019-08-23 stsp if (err)
6137 b8bad2ba 2019-08-23 stsp break;
6138 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
6139 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
6140 b8bad2ba 2019-08-23 stsp }
6141 b8bad2ba 2019-08-23 stsp
6142 b8bad2ba 2019-08-23 stsp while (se) {
6143 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
6144 b8bad2ba 2019-08-23 stsp if (err)
6145 b8bad2ba 2019-08-23 stsp break;
6146 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
6147 b8bad2ba 2019-08-23 stsp free(se_id);
6148 b8bad2ba 2019-08-23 stsp if (err)
6149 b8bad2ba 2019-08-23 stsp break;
6150 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
6151 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
6152 b8bad2ba 2019-08-23 stsp
6153 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
6154 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6155 b8bad2ba 2019-08-23 stsp if (err)
6156 b8bad2ba 2019-08-23 stsp return err;
6157 dbdddfee 2021-06-23 naddy STAILQ_INSERT_AFTER(sorted, se, new, entry);
6158 b8bad2ba 2019-08-23 stsp break;
6159 b8bad2ba 2019-08-23 stsp }
6160 dbdddfee 2021-06-23 naddy se = STAILQ_NEXT(se, entry);
6161 b8bad2ba 2019-08-23 stsp continue;
6162 b8bad2ba 2019-08-23 stsp }
6163 b8bad2ba 2019-08-23 stsp }
6164 b8bad2ba 2019-08-23 stsp done:
6165 b8bad2ba 2019-08-23 stsp return err;
6166 8e7bd50a 2019-08-22 stsp }
6167 b8bad2ba 2019-08-23 stsp #endif
6168 b8bad2ba 2019-08-23 stsp
6169 b8bad2ba 2019-08-23 stsp static const struct got_error *
6170 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
6171 8e7bd50a 2019-08-22 stsp {
6172 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
6173 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
6174 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
6175 8e7bd50a 2019-08-22 stsp
6176 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6177 8e7bd50a 2019-08-22 stsp
6178 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6179 8e7bd50a 2019-08-22 stsp if (err)
6180 8e7bd50a 2019-08-22 stsp return err;
6181 8e7bd50a 2019-08-22 stsp
6182 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
6183 8e7bd50a 2019-08-22 stsp const char *refname;
6184 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6185 8e7bd50a 2019-08-22 stsp char datebuf[26];
6186 d4efa91b 2020-01-14 stsp const char *tagger;
6187 8e7bd50a 2019-08-22 stsp time_t tagger_time;
6188 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
6189 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
6190 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
6191 8e7bd50a 2019-08-22 stsp
6192 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
6193 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
6194 8e7bd50a 2019-08-22 stsp continue;
6195 8e7bd50a 2019-08-22 stsp refname += 10;
6196 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
6197 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
6198 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
6199 8e7bd50a 2019-08-22 stsp break;
6200 8e7bd50a 2019-08-22 stsp }
6201 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6202 8e7bd50a 2019-08-22 stsp free(refstr);
6203 8e7bd50a 2019-08-22 stsp
6204 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
6205 8e7bd50a 2019-08-22 stsp if (err)
6206 8e7bd50a 2019-08-22 stsp break;
6207 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
6208 d4efa91b 2020-01-14 stsp if (err) {
6209 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
6210 d4efa91b 2020-01-14 stsp free(id);
6211 d4efa91b 2020-01-14 stsp break;
6212 d4efa91b 2020-01-14 stsp }
6213 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
6214 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
6215 d4efa91b 2020-01-14 stsp if (err) {
6216 d4efa91b 2020-01-14 stsp free(id);
6217 d4efa91b 2020-01-14 stsp break;
6218 d4efa91b 2020-01-14 stsp }
6219 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
6220 d4efa91b 2020-01-14 stsp tagger_time =
6221 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
6222 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
6223 d4efa91b 2020-01-14 stsp free(id);
6224 d4efa91b 2020-01-14 stsp if (err)
6225 d4efa91b 2020-01-14 stsp break;
6226 d4efa91b 2020-01-14 stsp } else {
6227 d4efa91b 2020-01-14 stsp free(id);
6228 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
6229 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
6230 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
6231 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
6232 d4efa91b 2020-01-14 stsp if (err)
6233 d4efa91b 2020-01-14 stsp break;
6234 d4efa91b 2020-01-14 stsp }
6235 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
6236 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
6237 2417344c 2019-08-23 stsp if (datestr)
6238 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
6239 d4efa91b 2020-01-14 stsp if (commit)
6240 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6241 d4efa91b 2020-01-14 stsp else {
6242 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
6243 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
6244 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6245 d4efa91b 2020-01-14 stsp id_str);
6246 d4efa91b 2020-01-14 stsp break;
6247 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
6248 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6249 d4efa91b 2020-01-14 stsp id_str);
6250 d4efa91b 2020-01-14 stsp break;
6251 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
6252 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6253 d4efa91b 2020-01-14 stsp id_str);
6254 d4efa91b 2020-01-14 stsp break;
6255 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
6256 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6257 d4efa91b 2020-01-14 stsp id_str);
6258 d4efa91b 2020-01-14 stsp break;
6259 d4efa91b 2020-01-14 stsp default:
6260 d4efa91b 2020-01-14 stsp break;
6261 d4efa91b 2020-01-14 stsp }
6262 8e7bd50a 2019-08-22 stsp }
6263 8e7bd50a 2019-08-22 stsp free(id_str);
6264 d4efa91b 2020-01-14 stsp if (commit) {
6265 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
6266 d4efa91b 2020-01-14 stsp if (err)
6267 d4efa91b 2020-01-14 stsp break;
6268 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
6269 d4efa91b 2020-01-14 stsp } else {
6270 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
6271 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
6272 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
6273 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
6274 d4efa91b 2020-01-14 stsp break;
6275 d4efa91b 2020-01-14 stsp }
6276 8e7bd50a 2019-08-22 stsp }
6277 8e7bd50a 2019-08-22 stsp
6278 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
6279 8e7bd50a 2019-08-22 stsp do {
6280 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
6281 8e7bd50a 2019-08-22 stsp if (line)
6282 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
6283 8e7bd50a 2019-08-22 stsp } while (line);
6284 8e7bd50a 2019-08-22 stsp free(tagmsg0);
6285 8e7bd50a 2019-08-22 stsp }
6286 8e7bd50a 2019-08-22 stsp
6287 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
6288 8e7bd50a 2019-08-22 stsp return NULL;
6289 8e7bd50a 2019-08-22 stsp }
6290 8e7bd50a 2019-08-22 stsp
6291 8e7bd50a 2019-08-22 stsp static const struct got_error *
6292 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6293 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
6294 8e7bd50a 2019-08-22 stsp {
6295 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6296 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
6297 f372d5cd 2019-10-21 stsp char *editor = NULL;
6298 1601cb9f 2020-09-11 naddy int initial_content_len;
6299 8e7bd50a 2019-08-22 stsp int fd = -1;
6300 8e7bd50a 2019-08-22 stsp
6301 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6302 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6303 8e7bd50a 2019-08-22 stsp goto done;
6304 8e7bd50a 2019-08-22 stsp }
6305 8e7bd50a 2019-08-22 stsp
6306 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6307 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
6308 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
6309 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
6310 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6311 8e7bd50a 2019-08-22 stsp goto done;
6312 8e7bd50a 2019-08-22 stsp }
6313 8e7bd50a 2019-08-22 stsp
6314 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6315 8e7bd50a 2019-08-22 stsp if (err)
6316 8e7bd50a 2019-08-22 stsp goto done;
6317 8e7bd50a 2019-08-22 stsp
6318 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6319 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
6320 97972933 2020-09-11 stsp goto done;
6321 97972933 2020-09-11 stsp }
6322 8e7bd50a 2019-08-22 stsp
6323 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
6324 8e7bd50a 2019-08-22 stsp if (err)
6325 8e7bd50a 2019-08-22 stsp goto done;
6326 bfa12d5e 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6327 0d5bb276 2020-12-15 stsp initial_content_len, 1);
6328 8e7bd50a 2019-08-22 stsp done:
6329 8e7bd50a 2019-08-22 stsp free(initial_content);
6330 8e7bd50a 2019-08-22 stsp free(template);
6331 8e7bd50a 2019-08-22 stsp free(editor);
6332 8e7bd50a 2019-08-22 stsp
6333 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6334 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
6335 97972933 2020-09-11 stsp
6336 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
6337 59f86c76 2020-09-11 stsp if (err == NULL)
6338 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
6339 59f86c76 2020-09-11 stsp if (err) {
6340 59f86c76 2020-09-11 stsp free(*tagmsg);
6341 59f86c76 2020-09-11 stsp *tagmsg = NULL;
6342 8e7bd50a 2019-08-22 stsp }
6343 8e7bd50a 2019-08-22 stsp return err;
6344 8e7bd50a 2019-08-22 stsp }
6345 8e7bd50a 2019-08-22 stsp
6346 8e7bd50a 2019-08-22 stsp static const struct got_error *
6347 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
6348 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6349 8e7bd50a 2019-08-22 stsp {
6350 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6351 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
6352 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
6353 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
6354 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6355 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
6356 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
6357 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6358 8e7bd50a 2019-08-22 stsp
6359 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6360 84de9106 2020-12-26 stsp
6361 8e7bd50a 2019-08-22 stsp /*
6362 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
6363 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
6364 8e7bd50a 2019-08-22 stsp * an unintended typo.
6365 8e7bd50a 2019-08-22 stsp */
6366 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
6367 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6368 8e7bd50a 2019-08-22 stsp
6369 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
6370 8e7bd50a 2019-08-22 stsp if (err)
6371 8e7bd50a 2019-08-22 stsp return err;
6372 8e7bd50a 2019-08-22 stsp
6373 84de9106 2020-12-26 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6374 84de9106 2020-12-26 stsp if (err)
6375 84de9106 2020-12-26 stsp goto done;
6376 84de9106 2020-12-26 stsp
6377 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6378 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &refs, repo);
6379 8e7bd50a 2019-08-22 stsp if (err)
6380 8e7bd50a 2019-08-22 stsp goto done;
6381 8e7bd50a 2019-08-22 stsp
6382 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
6383 8e7bd50a 2019-08-22 stsp if (err)
6384 8e7bd50a 2019-08-22 stsp goto done;
6385 8e7bd50a 2019-08-22 stsp
6386 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
6387 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
6388 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
6389 62d463ca 2020-10-20 naddy err = got_error_from_errno("strdup");
6390 62d463ca 2020-10-20 naddy goto done;
6391 8e7bd50a 2019-08-22 stsp }
6392 8e7bd50a 2019-08-22 stsp tag_name += 10;
6393 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6394 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
6395 62d463ca 2020-10-20 naddy goto done;
6396 8e7bd50a 2019-08-22 stsp }
6397 8e7bd50a 2019-08-22 stsp
6398 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
6399 8e7bd50a 2019-08-22 stsp if (err == NULL) {
6400 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
6401 8e7bd50a 2019-08-22 stsp goto done;
6402 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
6403 8e7bd50a 2019-08-22 stsp goto done;
6404 8e7bd50a 2019-08-22 stsp
6405 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
6406 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6407 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
6408 f372d5cd 2019-10-21 stsp if (err) {
6409 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6410 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
6411 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6412 8e7bd50a 2019-08-22 stsp goto done;
6413 f372d5cd 2019-10-21 stsp }
6414 8e7bd50a 2019-08-22 stsp }
6415 8e7bd50a 2019-08-22 stsp
6416 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
6417 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6418 f372d5cd 2019-10-21 stsp if (err) {
6419 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6420 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6421 8e7bd50a 2019-08-22 stsp goto done;
6422 f372d5cd 2019-10-21 stsp }
6423 8e7bd50a 2019-08-22 stsp
6424 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
6425 f372d5cd 2019-10-21 stsp if (err) {
6426 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6427 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6428 8e7bd50a 2019-08-22 stsp goto done;
6429 f372d5cd 2019-10-21 stsp }
6430 8e7bd50a 2019-08-22 stsp
6431 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
6432 f372d5cd 2019-10-21 stsp if (err) {
6433 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6434 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6435 f372d5cd 2019-10-21 stsp goto done;
6436 f372d5cd 2019-10-21 stsp }
6437 8e7bd50a 2019-08-22 stsp
6438 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
6439 f372d5cd 2019-10-21 stsp if (err) {
6440 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6441 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6442 f372d5cd 2019-10-21 stsp goto done;
6443 8e7bd50a 2019-08-22 stsp }
6444 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
6445 8e7bd50a 2019-08-22 stsp done:
6446 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
6447 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
6448 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
6449 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6450 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
6451 f372d5cd 2019-10-21 stsp free(tag_id_str);
6452 8e7bd50a 2019-08-22 stsp if (ref)
6453 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
6454 8e7bd50a 2019-08-22 stsp free(commit_id);
6455 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6456 8e7bd50a 2019-08-22 stsp free(refname);
6457 8e7bd50a 2019-08-22 stsp free(tagmsg);
6458 f372d5cd 2019-10-21 stsp free(tagmsg_path);
6459 aba9c984 2019-09-08 stsp free(tagger);
6460 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
6461 8e7bd50a 2019-08-22 stsp return err;
6462 8e7bd50a 2019-08-22 stsp }
6463 8e7bd50a 2019-08-22 stsp
6464 8e7bd50a 2019-08-22 stsp static const struct got_error *
6465 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
6466 8e7bd50a 2019-08-22 stsp {
6467 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
6468 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
6469 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
6470 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6471 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
6472 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6473 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
6474 8e7bd50a 2019-08-22 stsp
6475 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6476 8e7bd50a 2019-08-22 stsp switch (ch) {
6477 80106605 2020-02-24 stsp case 'c':
6478 80106605 2020-02-24 stsp commit_id_arg = optarg;
6479 80106605 2020-02-24 stsp break;
6480 8e7bd50a 2019-08-22 stsp case 'm':
6481 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6482 8e7bd50a 2019-08-22 stsp break;
6483 8e7bd50a 2019-08-22 stsp case 'r':
6484 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6485 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6486 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6487 9ba1d308 2019-10-21 stsp optarg);
6488 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6489 8e7bd50a 2019-08-22 stsp break;
6490 8e7bd50a 2019-08-22 stsp case 'l':
6491 8e7bd50a 2019-08-22 stsp do_list = 1;
6492 8e7bd50a 2019-08-22 stsp break;
6493 8e7bd50a 2019-08-22 stsp default:
6494 8e7bd50a 2019-08-22 stsp usage_tag();
6495 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6496 8e7bd50a 2019-08-22 stsp }
6497 8e7bd50a 2019-08-22 stsp }
6498 8e7bd50a 2019-08-22 stsp
6499 8e7bd50a 2019-08-22 stsp argc -= optind;
6500 8e7bd50a 2019-08-22 stsp argv += optind;
6501 8e7bd50a 2019-08-22 stsp
6502 8e7bd50a 2019-08-22 stsp if (do_list) {
6503 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6504 775ce909 2020-03-22 stsp errx(1,
6505 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
6506 8e7bd50a 2019-08-22 stsp if (tagmsg)
6507 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
6508 8e7bd50a 2019-08-22 stsp if (argc > 0)
6509 8e7bd50a 2019-08-22 stsp usage_tag();
6510 80106605 2020-02-24 stsp } else if (argc != 1)
6511 8e7bd50a 2019-08-22 stsp usage_tag();
6512 80106605 2020-02-24 stsp
6513 a2887370 2019-08-23 stsp tag_name = argv[0];
6514 8e7bd50a 2019-08-22 stsp
6515 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
6516 8e7bd50a 2019-08-22 stsp if (do_list) {
6517 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6518 8e7bd50a 2019-08-22 stsp NULL) == -1)
6519 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6520 8e7bd50a 2019-08-22 stsp } else {
6521 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6522 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
6523 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6524 8e7bd50a 2019-08-22 stsp }
6525 8e7bd50a 2019-08-22 stsp #endif
6526 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
6527 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
6528 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
6529 8e7bd50a 2019-08-22 stsp goto done;
6530 8e7bd50a 2019-08-22 stsp }
6531 8e7bd50a 2019-08-22 stsp
6532 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6533 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6534 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6535 8e7bd50a 2019-08-22 stsp goto done;
6536 8e7bd50a 2019-08-22 stsp else
6537 8e7bd50a 2019-08-22 stsp error = NULL;
6538 8e7bd50a 2019-08-22 stsp if (worktree) {
6539 8e7bd50a 2019-08-22 stsp repo_path =
6540 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
6541 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6542 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6543 8e7bd50a 2019-08-22 stsp if (error)
6544 8e7bd50a 2019-08-22 stsp goto done;
6545 8e7bd50a 2019-08-22 stsp } else {
6546 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
6547 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6548 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6549 8e7bd50a 2019-08-22 stsp goto done;
6550 8e7bd50a 2019-08-22 stsp }
6551 8e7bd50a 2019-08-22 stsp }
6552 8e7bd50a 2019-08-22 stsp }
6553 8e7bd50a 2019-08-22 stsp
6554 8e7bd50a 2019-08-22 stsp if (do_list) {
6555 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6556 c9956ddf 2019-09-08 stsp if (error != NULL)
6557 c9956ddf 2019-09-08 stsp goto done;
6558 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6559 8e7bd50a 2019-08-22 stsp if (error)
6560 8e7bd50a 2019-08-22 stsp goto done;
6561 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
6562 8e7bd50a 2019-08-22 stsp } else {
6563 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6564 c9956ddf 2019-09-08 stsp if (error)
6565 c9956ddf 2019-09-08 stsp goto done;
6566 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
6567 c9956ddf 2019-09-08 stsp if (error != NULL)
6568 c9956ddf 2019-09-08 stsp goto done;
6569 c9956ddf 2019-09-08 stsp
6570 8e7bd50a 2019-08-22 stsp if (tagmsg) {
6571 42a285e2 2020-10-01 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6572 8e7bd50a 2019-08-22 stsp if (error)
6573 8e7bd50a 2019-08-22 stsp goto done;
6574 8e7bd50a 2019-08-22 stsp }
6575 8e7bd50a 2019-08-22 stsp
6576 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
6577 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
6578 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
6579 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
6580 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
6581 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
6582 8e7bd50a 2019-08-22 stsp if (error)
6583 8e7bd50a 2019-08-22 stsp goto done;
6584 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6585 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
6586 8e7bd50a 2019-08-22 stsp if (error)
6587 8e7bd50a 2019-08-22 stsp goto done;
6588 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
6589 8e7bd50a 2019-08-22 stsp free(commit_id);
6590 8e7bd50a 2019-08-22 stsp if (error)
6591 8e7bd50a 2019-08-22 stsp goto done;
6592 8e7bd50a 2019-08-22 stsp }
6593 8e7bd50a 2019-08-22 stsp
6594 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
6595 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6596 8e7bd50a 2019-08-22 stsp }
6597 8e7bd50a 2019-08-22 stsp done:
6598 1d0f4054 2021-06-17 stsp if (repo) {
6599 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6600 1d0f4054 2021-06-17 stsp if (error == NULL)
6601 1d0f4054 2021-06-17 stsp error = close_err;
6602 1d0f4054 2021-06-17 stsp }
6603 8e7bd50a 2019-08-22 stsp if (worktree)
6604 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
6605 8e7bd50a 2019-08-22 stsp free(cwd);
6606 8e7bd50a 2019-08-22 stsp free(repo_path);
6607 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6608 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6609 8e7bd50a 2019-08-22 stsp return error;
6610 8e7bd50a 2019-08-22 stsp }
6611 8e7bd50a 2019-08-22 stsp
6612 8e7bd50a 2019-08-22 stsp __dead static void
6613 d00136be 2019-03-26 stsp usage_add(void)
6614 d00136be 2019-03-26 stsp {
6615 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6616 022fae89 2019-12-06 tracey getprogname());
6617 d00136be 2019-03-26 stsp exit(1);
6618 d00136be 2019-03-26 stsp }
6619 d00136be 2019-03-26 stsp
6620 d00136be 2019-03-26 stsp static const struct got_error *
6621 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6622 4e68cba3 2019-11-23 stsp {
6623 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6624 4e68cba3 2019-11-23 stsp path++;
6625 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6626 4e68cba3 2019-11-23 stsp return NULL;
6627 4e68cba3 2019-11-23 stsp }
6628 4e68cba3 2019-11-23 stsp
6629 4e68cba3 2019-11-23 stsp static const struct got_error *
6630 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6631 d00136be 2019-03-26 stsp {
6632 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6633 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6634 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6635 1dd54920 2019-05-11 stsp char *cwd = NULL;
6636 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6637 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6638 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6639 1dd54920 2019-05-11 stsp
6640 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6641 d00136be 2019-03-26 stsp
6642 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6643 d00136be 2019-03-26 stsp switch (ch) {
6644 022fae89 2019-12-06 tracey case 'I':
6645 022fae89 2019-12-06 tracey no_ignores = 1;
6646 022fae89 2019-12-06 tracey break;
6647 4e68cba3 2019-11-23 stsp case 'R':
6648 4e68cba3 2019-11-23 stsp can_recurse = 1;
6649 4e68cba3 2019-11-23 stsp break;
6650 d00136be 2019-03-26 stsp default:
6651 d00136be 2019-03-26 stsp usage_add();
6652 d00136be 2019-03-26 stsp /* NOTREACHED */
6653 d00136be 2019-03-26 stsp }
6654 d00136be 2019-03-26 stsp }
6655 d00136be 2019-03-26 stsp
6656 d00136be 2019-03-26 stsp argc -= optind;
6657 d00136be 2019-03-26 stsp argv += optind;
6658 d00136be 2019-03-26 stsp
6659 43012d58 2019-07-14 stsp #ifndef PROFILE
6660 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6661 43012d58 2019-07-14 stsp NULL) == -1)
6662 43012d58 2019-07-14 stsp err(1, "pledge");
6663 43012d58 2019-07-14 stsp #endif
6664 723c305c 2019-05-11 jcs if (argc < 1)
6665 d00136be 2019-03-26 stsp usage_add();
6666 d00136be 2019-03-26 stsp
6667 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6668 d00136be 2019-03-26 stsp if (cwd == NULL) {
6669 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6670 d00136be 2019-03-26 stsp goto done;
6671 d00136be 2019-03-26 stsp }
6672 723c305c 2019-05-11 jcs
6673 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6674 fa51e947 2020-03-27 stsp if (error) {
6675 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6676 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6677 d00136be 2019-03-26 stsp goto done;
6678 fa51e947 2020-03-27 stsp }
6679 d00136be 2019-03-26 stsp
6680 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6681 c9956ddf 2019-09-08 stsp NULL);
6682 031a5338 2019-03-26 stsp if (error != NULL)
6683 031a5338 2019-03-26 stsp goto done;
6684 031a5338 2019-03-26 stsp
6685 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6686 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6687 d00136be 2019-03-26 stsp if (error)
6688 d00136be 2019-03-26 stsp goto done;
6689 d00136be 2019-03-26 stsp
6690 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6691 6d022e97 2019-08-04 stsp if (error)
6692 022fae89 2019-12-06 tracey goto done;
6693 022fae89 2019-12-06 tracey
6694 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6695 4e68cba3 2019-11-23 stsp char *ondisk_path;
6696 4e68cba3 2019-11-23 stsp struct stat sb;
6697 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6698 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6699 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6700 62d463ca 2020-10-20 naddy pe->path) == -1) {
6701 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6702 4e68cba3 2019-11-23 stsp goto done;
6703 4e68cba3 2019-11-23 stsp }
6704 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6705 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6706 4e68cba3 2019-11-23 stsp free(ondisk_path);
6707 4e68cba3 2019-11-23 stsp continue;
6708 4e68cba3 2019-11-23 stsp }
6709 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6710 4e68cba3 2019-11-23 stsp ondisk_path);
6711 4e68cba3 2019-11-23 stsp free(ondisk_path);
6712 4e68cba3 2019-11-23 stsp goto done;
6713 4e68cba3 2019-11-23 stsp }
6714 4e68cba3 2019-11-23 stsp free(ondisk_path);
6715 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6716 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6717 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6718 4e68cba3 2019-11-23 stsp goto done;
6719 4e68cba3 2019-11-23 stsp }
6720 4e68cba3 2019-11-23 stsp }
6721 4e68cba3 2019-11-23 stsp }
6722 022fae89 2019-12-06 tracey
6723 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6724 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6725 d00136be 2019-03-26 stsp done:
6726 1d0f4054 2021-06-17 stsp if (repo) {
6727 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6728 1d0f4054 2021-06-17 stsp if (error == NULL)
6729 1d0f4054 2021-06-17 stsp error = close_err;
6730 1d0f4054 2021-06-17 stsp }
6731 d00136be 2019-03-26 stsp if (worktree)
6732 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6733 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6734 1dd54920 2019-05-11 stsp free((char *)pe->path);
6735 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6736 2ec1f75b 2019-03-26 stsp free(cwd);
6737 2ec1f75b 2019-03-26 stsp return error;
6738 2ec1f75b 2019-03-26 stsp }
6739 2ec1f75b 2019-03-26 stsp
6740 2ec1f75b 2019-03-26 stsp __dead static void
6741 648e4ef7 2019-07-09 stsp usage_remove(void)
6742 2ec1f75b 2019-03-26 stsp {
6743 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6744 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6745 2ec1f75b 2019-03-26 stsp exit(1);
6746 2a06fe5f 2019-08-24 stsp }
6747 2a06fe5f 2019-08-24 stsp
6748 2a06fe5f 2019-08-24 stsp static const struct got_error *
6749 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6750 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6751 2a06fe5f 2019-08-24 stsp {
6752 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6753 f2a9dc41 2019-12-13 tracey path++;
6754 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6755 2a06fe5f 2019-08-24 stsp return NULL;
6756 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6757 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6758 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6759 2a06fe5f 2019-08-24 stsp return NULL;
6760 2ec1f75b 2019-03-26 stsp }
6761 2ec1f75b 2019-03-26 stsp
6762 2ec1f75b 2019-03-26 stsp static const struct got_error *
6763 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6764 2ec1f75b 2019-03-26 stsp {
6765 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6766 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6767 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6768 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6769 17ed4618 2019-06-02 stsp char *cwd = NULL;
6770 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6771 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6772 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6773 2ec1f75b 2019-03-26 stsp
6774 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6775 17ed4618 2019-06-02 stsp
6776 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6777 2ec1f75b 2019-03-26 stsp switch (ch) {
6778 2ec1f75b 2019-03-26 stsp case 'f':
6779 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6780 2ec1f75b 2019-03-26 stsp break;
6781 70e3e7f5 2019-12-13 tracey case 'k':
6782 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6783 70e3e7f5 2019-12-13 tracey break;
6784 f2a9dc41 2019-12-13 tracey case 'R':
6785 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6786 f2a9dc41 2019-12-13 tracey break;
6787 766841c2 2020-08-13 stsp case 's':
6788 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6789 766841c2 2020-08-13 stsp switch (optarg[i]) {
6790 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6791 766841c2 2020-08-13 stsp delete_local_mods = 1;
6792 766841c2 2020-08-13 stsp break;
6793 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6794 766841c2 2020-08-13 stsp break;
6795 766841c2 2020-08-13 stsp default:
6796 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6797 766841c2 2020-08-13 stsp optarg[i]);
6798 766841c2 2020-08-13 stsp }
6799 766841c2 2020-08-13 stsp }
6800 766841c2 2020-08-13 stsp status_codes = optarg;
6801 766841c2 2020-08-13 stsp break;
6802 2ec1f75b 2019-03-26 stsp default:
6803 f2a9dc41 2019-12-13 tracey usage_remove();
6804 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6805 2ec1f75b 2019-03-26 stsp }
6806 2ec1f75b 2019-03-26 stsp }
6807 2ec1f75b 2019-03-26 stsp
6808 2ec1f75b 2019-03-26 stsp argc -= optind;
6809 2ec1f75b 2019-03-26 stsp argv += optind;
6810 2ec1f75b 2019-03-26 stsp
6811 43012d58 2019-07-14 stsp #ifndef PROFILE
6812 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6813 43012d58 2019-07-14 stsp NULL) == -1)
6814 43012d58 2019-07-14 stsp err(1, "pledge");
6815 43012d58 2019-07-14 stsp #endif
6816 17ed4618 2019-06-02 stsp if (argc < 1)
6817 648e4ef7 2019-07-09 stsp usage_remove();
6818 2ec1f75b 2019-03-26 stsp
6819 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6820 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6821 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6822 2ec1f75b 2019-03-26 stsp goto done;
6823 2ec1f75b 2019-03-26 stsp }
6824 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6825 fa51e947 2020-03-27 stsp if (error) {
6826 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6827 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6828 2ec1f75b 2019-03-26 stsp goto done;
6829 fa51e947 2020-03-27 stsp }
6830 2ec1f75b 2019-03-26 stsp
6831 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6832 c9956ddf 2019-09-08 stsp NULL);
6833 2af4a041 2019-05-11 jcs if (error)
6834 2ec1f75b 2019-03-26 stsp goto done;
6835 2ec1f75b 2019-03-26 stsp
6836 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6837 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6838 2ec1f75b 2019-03-26 stsp if (error)
6839 2ec1f75b 2019-03-26 stsp goto done;
6840 2ec1f75b 2019-03-26 stsp
6841 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6842 6d022e97 2019-08-04 stsp if (error)
6843 6d022e97 2019-08-04 stsp goto done;
6844 17ed4618 2019-06-02 stsp
6845 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6846 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6847 f2a9dc41 2019-12-13 tracey struct stat sb;
6848 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6849 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6850 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6851 62d463ca 2020-10-20 naddy pe->path) == -1) {
6852 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6853 f2a9dc41 2019-12-13 tracey goto done;
6854 f2a9dc41 2019-12-13 tracey }
6855 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6856 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6857 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6858 f2a9dc41 2019-12-13 tracey continue;
6859 f2a9dc41 2019-12-13 tracey }
6860 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6861 f2a9dc41 2019-12-13 tracey ondisk_path);
6862 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6863 f2a9dc41 2019-12-13 tracey goto done;
6864 f2a9dc41 2019-12-13 tracey }
6865 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6866 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6867 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6868 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6869 f2a9dc41 2019-12-13 tracey goto done;
6870 f2a9dc41 2019-12-13 tracey }
6871 f2a9dc41 2019-12-13 tracey }
6872 f2a9dc41 2019-12-13 tracey }
6873 f2a9dc41 2019-12-13 tracey
6874 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6875 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6876 766841c2 2020-08-13 stsp repo, keep_on_disk);
6877 a129376b 2019-03-28 stsp done:
6878 1d0f4054 2021-06-17 stsp if (repo) {
6879 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6880 1d0f4054 2021-06-17 stsp if (error == NULL)
6881 1d0f4054 2021-06-17 stsp error = close_err;
6882 1d0f4054 2021-06-17 stsp }
6883 a129376b 2019-03-28 stsp if (worktree)
6884 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6885 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6886 17ed4618 2019-06-02 stsp free((char *)pe->path);
6887 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6888 a129376b 2019-03-28 stsp free(cwd);
6889 a129376b 2019-03-28 stsp return error;
6890 a129376b 2019-03-28 stsp }
6891 a129376b 2019-03-28 stsp
6892 a129376b 2019-03-28 stsp __dead static void
6893 a129376b 2019-03-28 stsp usage_revert(void)
6894 a129376b 2019-03-28 stsp {
6895 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6896 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6897 a129376b 2019-03-28 stsp exit(1);
6898 a129376b 2019-03-28 stsp }
6899 a129376b 2019-03-28 stsp
6900 1ee397ad 2019-07-12 stsp static const struct got_error *
6901 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6902 a129376b 2019-03-28 stsp {
6903 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6904 fb9704af 2020-01-27 stsp return NULL;
6905 fb9704af 2020-01-27 stsp
6906 a129376b 2019-03-28 stsp while (path[0] == '/')
6907 a129376b 2019-03-28 stsp path++;
6908 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6909 33aa809d 2019-08-08 stsp return NULL;
6910 33aa809d 2019-08-08 stsp }
6911 33aa809d 2019-08-08 stsp
6912 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6913 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6914 33aa809d 2019-08-08 stsp const char *action;
6915 33aa809d 2019-08-08 stsp };
6916 33aa809d 2019-08-08 stsp
6917 33aa809d 2019-08-08 stsp static const struct got_error *
6918 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6919 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6920 33aa809d 2019-08-08 stsp {
6921 33aa809d 2019-08-08 stsp char *line = NULL;
6922 33aa809d 2019-08-08 stsp size_t linesize = 0;
6923 33aa809d 2019-08-08 stsp ssize_t linelen;
6924 33aa809d 2019-08-08 stsp
6925 33aa809d 2019-08-08 stsp switch (status) {
6926 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6927 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6928 33aa809d 2019-08-08 stsp break;
6929 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6930 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6931 33aa809d 2019-08-08 stsp break;
6932 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6933 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6934 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6935 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6936 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6937 33aa809d 2019-08-08 stsp printf("%s", line);
6938 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6939 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6940 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6941 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6942 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6943 33aa809d 2019-08-08 stsp break;
6944 33aa809d 2019-08-08 stsp default:
6945 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6946 33aa809d 2019-08-08 stsp }
6947 33aa809d 2019-08-08 stsp
6948 33aa809d 2019-08-08 stsp return NULL;
6949 33aa809d 2019-08-08 stsp }
6950 33aa809d 2019-08-08 stsp
6951 33aa809d 2019-08-08 stsp static const struct got_error *
6952 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6953 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6954 33aa809d 2019-08-08 stsp {
6955 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6956 33aa809d 2019-08-08 stsp char *line = NULL;
6957 33aa809d 2019-08-08 stsp size_t linesize = 0;
6958 33aa809d 2019-08-08 stsp ssize_t linelen;
6959 33aa809d 2019-08-08 stsp int resp = ' ';
6960 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6961 33aa809d 2019-08-08 stsp
6962 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6963 33aa809d 2019-08-08 stsp
6964 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6965 33aa809d 2019-08-08 stsp char *nl;
6966 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6967 33aa809d 2019-08-08 stsp a->action);
6968 33aa809d 2019-08-08 stsp if (err)
6969 33aa809d 2019-08-08 stsp return err;
6970 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6971 33aa809d 2019-08-08 stsp if (linelen == -1) {
6972 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6973 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6974 33aa809d 2019-08-08 stsp return NULL;
6975 33aa809d 2019-08-08 stsp }
6976 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6977 33aa809d 2019-08-08 stsp if (nl)
6978 33aa809d 2019-08-08 stsp *nl = '\0';
6979 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6980 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6981 33aa809d 2019-08-08 stsp printf("y\n");
6982 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6983 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6984 33aa809d 2019-08-08 stsp printf("n\n");
6985 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6986 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6987 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6988 33aa809d 2019-08-08 stsp printf("q\n");
6989 33aa809d 2019-08-08 stsp } else
6990 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6991 33aa809d 2019-08-08 stsp free(line);
6992 33aa809d 2019-08-08 stsp return NULL;
6993 33aa809d 2019-08-08 stsp }
6994 33aa809d 2019-08-08 stsp
6995 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6996 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6997 33aa809d 2019-08-08 stsp a->action);
6998 33aa809d 2019-08-08 stsp if (err)
6999 33aa809d 2019-08-08 stsp return err;
7000 33aa809d 2019-08-08 stsp resp = getchar();
7001 33aa809d 2019-08-08 stsp if (resp == '\n')
7002 33aa809d 2019-08-08 stsp resp = getchar();
7003 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
7004 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
7005 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
7006 33aa809d 2019-08-08 stsp resp = ' ';
7007 33aa809d 2019-08-08 stsp }
7008 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
7009 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
7010 33aa809d 2019-08-08 stsp resp = ' ';
7011 33aa809d 2019-08-08 stsp }
7012 33aa809d 2019-08-08 stsp }
7013 33aa809d 2019-08-08 stsp
7014 33aa809d 2019-08-08 stsp if (resp == 'y')
7015 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
7016 33aa809d 2019-08-08 stsp else if (resp == 'n')
7017 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
7018 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
7019 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
7020 33aa809d 2019-08-08 stsp
7021 1ee397ad 2019-07-12 stsp return NULL;
7022 a129376b 2019-03-28 stsp }
7023 a129376b 2019-03-28 stsp
7024 33aa809d 2019-08-08 stsp
7025 a129376b 2019-03-28 stsp static const struct got_error *
7026 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
7027 a129376b 2019-03-28 stsp {
7028 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
7029 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
7030 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
7031 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
7032 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
7033 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
7034 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
7035 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
7036 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
7037 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
7038 a129376b 2019-03-28 stsp
7039 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
7040 e20a8b6f 2019-06-04 stsp
7041 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
7042 a129376b 2019-03-28 stsp switch (ch) {
7043 33aa809d 2019-08-08 stsp case 'p':
7044 33aa809d 2019-08-08 stsp pflag = 1;
7045 33aa809d 2019-08-08 stsp break;
7046 33aa809d 2019-08-08 stsp case 'F':
7047 33aa809d 2019-08-08 stsp patch_script_path = optarg;
7048 33aa809d 2019-08-08 stsp break;
7049 0f6d7415 2019-08-08 stsp case 'R':
7050 0f6d7415 2019-08-08 stsp can_recurse = 1;
7051 0f6d7415 2019-08-08 stsp break;
7052 a129376b 2019-03-28 stsp default:
7053 a129376b 2019-03-28 stsp usage_revert();
7054 a129376b 2019-03-28 stsp /* NOTREACHED */
7055 a129376b 2019-03-28 stsp }
7056 a129376b 2019-03-28 stsp }
7057 a129376b 2019-03-28 stsp
7058 a129376b 2019-03-28 stsp argc -= optind;
7059 a129376b 2019-03-28 stsp argv += optind;
7060 a129376b 2019-03-28 stsp
7061 43012d58 2019-07-14 stsp #ifndef PROFILE
7062 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7063 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7064 43012d58 2019-07-14 stsp err(1, "pledge");
7065 43012d58 2019-07-14 stsp #endif
7066 e20a8b6f 2019-06-04 stsp if (argc < 1)
7067 a129376b 2019-03-28 stsp usage_revert();
7068 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
7069 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7070 a129376b 2019-03-28 stsp
7071 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
7072 a129376b 2019-03-28 stsp if (cwd == NULL) {
7073 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7074 a129376b 2019-03-28 stsp goto done;
7075 a129376b 2019-03-28 stsp }
7076 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
7077 fa51e947 2020-03-27 stsp if (error) {
7078 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7079 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
7080 2ec1f75b 2019-03-26 stsp goto done;
7081 fa51e947 2020-03-27 stsp }
7082 a129376b 2019-03-28 stsp
7083 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7084 c9956ddf 2019-09-08 stsp NULL);
7085 a129376b 2019-03-28 stsp if (error != NULL)
7086 a129376b 2019-03-28 stsp goto done;
7087 a129376b 2019-03-28 stsp
7088 33aa809d 2019-08-08 stsp if (patch_script_path) {
7089 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7090 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
7091 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
7092 33aa809d 2019-08-08 stsp patch_script_path);
7093 33aa809d 2019-08-08 stsp goto done;
7094 33aa809d 2019-08-08 stsp }
7095 33aa809d 2019-08-08 stsp }
7096 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
7097 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7098 a129376b 2019-03-28 stsp if (error)
7099 a129376b 2019-03-28 stsp goto done;
7100 a129376b 2019-03-28 stsp
7101 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7102 6d022e97 2019-08-04 stsp if (error)
7103 6d022e97 2019-08-04 stsp goto done;
7104 00db391e 2019-08-03 stsp
7105 0f6d7415 2019-08-08 stsp if (!can_recurse) {
7106 0f6d7415 2019-08-08 stsp char *ondisk_path;
7107 0f6d7415 2019-08-08 stsp struct stat sb;
7108 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
7109 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
7110 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
7111 62d463ca 2020-10-20 naddy pe->path) == -1) {
7112 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
7113 0f6d7415 2019-08-08 stsp goto done;
7114 0f6d7415 2019-08-08 stsp }
7115 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
7116 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
7117 0f6d7415 2019-08-08 stsp free(ondisk_path);
7118 0f6d7415 2019-08-08 stsp continue;
7119 0f6d7415 2019-08-08 stsp }
7120 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
7121 0f6d7415 2019-08-08 stsp ondisk_path);
7122 0f6d7415 2019-08-08 stsp free(ondisk_path);
7123 0f6d7415 2019-08-08 stsp goto done;
7124 0f6d7415 2019-08-08 stsp }
7125 0f6d7415 2019-08-08 stsp free(ondisk_path);
7126 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
7127 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
7128 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
7129 0f6d7415 2019-08-08 stsp goto done;
7130 0f6d7415 2019-08-08 stsp }
7131 0f6d7415 2019-08-08 stsp }
7132 0f6d7415 2019-08-08 stsp }
7133 0f6d7415 2019-08-08 stsp
7134 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7135 33aa809d 2019-08-08 stsp cpa.action = "revert";
7136 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7137 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7138 2ec1f75b 2019-03-26 stsp done:
7139 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7140 33aa809d 2019-08-08 stsp error == NULL)
7141 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7142 1d0f4054 2021-06-17 stsp if (repo) {
7143 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7144 1d0f4054 2021-06-17 stsp if (error == NULL)
7145 1d0f4054 2021-06-17 stsp error = close_err;
7146 1d0f4054 2021-06-17 stsp }
7147 2ec1f75b 2019-03-26 stsp if (worktree)
7148 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
7149 2ec1f75b 2019-03-26 stsp free(path);
7150 d00136be 2019-03-26 stsp free(cwd);
7151 6bad629b 2019-02-04 stsp return error;
7152 c4296144 2019-05-09 stsp }
7153 c4296144 2019-05-09 stsp
7154 c4296144 2019-05-09 stsp __dead static void
7155 c4296144 2019-05-09 stsp usage_commit(void)
7156 c4296144 2019-05-09 stsp {
7157 28cf319f 2021-01-28 stsp fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
7158 28cf319f 2021-01-28 stsp "[path ...]\n", getprogname());
7159 c4296144 2019-05-09 stsp exit(1);
7160 33ad4cbe 2019-05-12 jcs }
7161 33ad4cbe 2019-05-12 jcs
7162 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
7163 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
7164 28cf319f 2021-01-28 stsp const char *prepared_log;
7165 28cf319f 2021-01-28 stsp int non_interactive;
7166 e2ba3d07 2019-05-13 stsp const char *editor;
7167 e0870e44 2019-05-13 stsp const char *worktree_path;
7168 76d98825 2019-06-03 stsp const char *branch_name;
7169 314a6357 2019-05-13 stsp const char *repo_path;
7170 e0870e44 2019-05-13 stsp char *logmsg_path;
7171 e2ba3d07 2019-05-13 stsp
7172 e2ba3d07 2019-05-13 stsp };
7173 28cf319f 2021-01-28 stsp
7174 28cf319f 2021-01-28 stsp static const struct got_error *
7175 28cf319f 2021-01-28 stsp read_prepared_logmsg(char **logmsg, const char *path)
7176 28cf319f 2021-01-28 stsp {
7177 28cf319f 2021-01-28 stsp const struct got_error *err = NULL;
7178 28cf319f 2021-01-28 stsp FILE *f = NULL;
7179 28cf319f 2021-01-28 stsp struct stat sb;
7180 28cf319f 2021-01-28 stsp size_t r;
7181 28cf319f 2021-01-28 stsp
7182 28cf319f 2021-01-28 stsp *logmsg = NULL;
7183 28cf319f 2021-01-28 stsp memset(&sb, 0, sizeof(sb));
7184 28cf319f 2021-01-28 stsp
7185 28cf319f 2021-01-28 stsp f = fopen(path, "r");
7186 28cf319f 2021-01-28 stsp if (f == NULL)
7187 28cf319f 2021-01-28 stsp return got_error_from_errno2("fopen", path);
7188 28cf319f 2021-01-28 stsp
7189 28cf319f 2021-01-28 stsp if (fstat(fileno(f), &sb) == -1) {
7190 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fstat", path);
7191 28cf319f 2021-01-28 stsp goto done;
7192 28cf319f 2021-01-28 stsp }
7193 28cf319f 2021-01-28 stsp if (sb.st_size == 0) {
7194 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7195 28cf319f 2021-01-28 stsp goto done;
7196 28cf319f 2021-01-28 stsp }
7197 28cf319f 2021-01-28 stsp
7198 28cf319f 2021-01-28 stsp *logmsg = malloc(sb.st_size + 1);
7199 28cf319f 2021-01-28 stsp if (*logmsg == NULL) {
7200 28cf319f 2021-01-28 stsp err = got_error_from_errno("malloc");
7201 28cf319f 2021-01-28 stsp goto done;
7202 28cf319f 2021-01-28 stsp }
7203 28cf319f 2021-01-28 stsp
7204 28cf319f 2021-01-28 stsp r = fread(*logmsg, 1, sb.st_size, f);
7205 28cf319f 2021-01-28 stsp if (r != sb.st_size) {
7206 28cf319f 2021-01-28 stsp if (ferror(f))
7207 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fread", path);
7208 28cf319f 2021-01-28 stsp else
7209 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_IO);
7210 28cf319f 2021-01-28 stsp goto done;
7211 28cf319f 2021-01-28 stsp }
7212 28cf319f 2021-01-28 stsp (*logmsg)[sb.st_size] = '\0';
7213 28cf319f 2021-01-28 stsp done:
7214 28cf319f 2021-01-28 stsp if (fclose(f) == EOF && err == NULL)
7215 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fclose", path);
7216 28cf319f 2021-01-28 stsp if (err) {
7217 28cf319f 2021-01-28 stsp free(*logmsg);
7218 28cf319f 2021-01-28 stsp *logmsg = NULL;
7219 28cf319f 2021-01-28 stsp }
7220 28cf319f 2021-01-28 stsp return err;
7221 28cf319f 2021-01-28 stsp
7222 28cf319f 2021-01-28 stsp }
7223 e0870e44 2019-05-13 stsp
7224 33ad4cbe 2019-05-12 jcs static const struct got_error *
7225 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
7226 33ad4cbe 2019-05-12 jcs void *arg)
7227 33ad4cbe 2019-05-12 jcs {
7228 76d98825 2019-06-03 stsp char *initial_content = NULL;
7229 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
7230 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
7231 e0870e44 2019-05-13 stsp char *template = NULL;
7232 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
7233 1601cb9f 2020-09-11 naddy int initial_content_len;
7234 97972933 2020-09-11 stsp int fd = -1;
7235 33ad4cbe 2019-05-12 jcs size_t len;
7236 33ad4cbe 2019-05-12 jcs
7237 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
7238 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
7239 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
7240 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
7241 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
7242 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
7243 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
7244 33ad4cbe 2019-05-12 jcs return NULL;
7245 28cf319f 2021-01-28 stsp } else if (a->prepared_log != NULL && a->non_interactive)
7246 28cf319f 2021-01-28 stsp return read_prepared_logmsg(logmsg, a->prepared_log);
7247 33ad4cbe 2019-05-12 jcs
7248 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7249 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
7250 76d98825 2019-06-03 stsp
7251 28cf319f 2021-01-28 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7252 28cf319f 2021-01-28 stsp if (err)
7253 28cf319f 2021-01-28 stsp goto done;
7254 28cf319f 2021-01-28 stsp
7255 28cf319f 2021-01-28 stsp if (a->prepared_log) {
7256 28cf319f 2021-01-28 stsp char *msg;
7257 28cf319f 2021-01-28 stsp err = read_prepared_logmsg(&msg, a->prepared_log);
7258 28cf319f 2021-01-28 stsp if (err)
7259 28cf319f 2021-01-28 stsp goto done;
7260 28cf319f 2021-01-28 stsp if (write(fd, msg, strlen(msg)) == -1) {
7261 28cf319f 2021-01-28 stsp err = got_error_from_errno2("write", a->logmsg_path);
7262 28cf319f 2021-01-28 stsp free(msg);
7263 28cf319f 2021-01-28 stsp goto done;
7264 28cf319f 2021-01-28 stsp }
7265 28cf319f 2021-01-28 stsp free(msg);
7266 28cf319f 2021-01-28 stsp }
7267 28cf319f 2021-01-28 stsp
7268 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
7269 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
7270 1601cb9f 2020-09-11 naddy a->branch_name);
7271 28cf319f 2021-01-28 stsp if (initial_content_len == -1) {
7272 28cf319f 2021-01-28 stsp err = got_error_from_errno("asprintf");
7273 e0870e44 2019-05-13 stsp goto done;
7274 28cf319f 2021-01-28 stsp }
7275 33ad4cbe 2019-05-12 jcs
7276 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
7277 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
7278 97972933 2020-09-11 stsp goto done;
7279 97972933 2020-09-11 stsp }
7280 33ad4cbe 2019-05-12 jcs
7281 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
7282 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
7283 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
7284 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
7285 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
7286 33ad4cbe 2019-05-12 jcs }
7287 33ad4cbe 2019-05-12 jcs
7288 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7289 28cf319f 2021-01-28 stsp initial_content_len, a->prepared_log ? 0 : 1);
7290 33ad4cbe 2019-05-12 jcs done:
7291 76d98825 2019-06-03 stsp free(initial_content);
7292 e0870e44 2019-05-13 stsp free(template);
7293 314a6357 2019-05-13 stsp
7294 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
7295 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
7296 97972933 2020-09-11 stsp
7297 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
7298 59f86c76 2020-09-11 stsp if (err == NULL)
7299 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
7300 59f86c76 2020-09-11 stsp if (err) {
7301 59f86c76 2020-09-11 stsp free(*logmsg);
7302 59f86c76 2020-09-11 stsp *logmsg = NULL;
7303 3ce1b845 2019-07-15 stsp }
7304 33ad4cbe 2019-05-12 jcs return err;
7305 6bad629b 2019-02-04 stsp }
7306 c4296144 2019-05-09 stsp
7307 c4296144 2019-05-09 stsp static const struct got_error *
7308 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
7309 c4296144 2019-05-09 stsp {
7310 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
7311 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
7312 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
7313 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
7314 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
7315 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
7316 28cf319f 2021-01-28 stsp char *prepared_logmsg = NULL;
7317 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
7318 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7319 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7320 28cf319f 2021-01-28 stsp int allow_bad_symlinks = 0, non_interactive = 0;
7321 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
7322 5c1e53bc 2019-07-28 stsp
7323 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
7324 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
7325 c4296144 2019-05-09 stsp
7326 28cf319f 2021-01-28 stsp while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7327 c4296144 2019-05-09 stsp switch (ch) {
7328 28cf319f 2021-01-28 stsp case 'F':
7329 28cf319f 2021-01-28 stsp if (logmsg != NULL)
7330 28cf319f 2021-01-28 stsp option_conflict('F', 'm');
7331 28cf319f 2021-01-28 stsp prepared_logmsg = realpath(optarg, NULL);
7332 28cf319f 2021-01-28 stsp if (prepared_logmsg == NULL)
7333 28cf319f 2021-01-28 stsp return got_error_from_errno2("realpath",
7334 28cf319f 2021-01-28 stsp optarg);
7335 28cf319f 2021-01-28 stsp break;
7336 c4296144 2019-05-09 stsp case 'm':
7337 28cf319f 2021-01-28 stsp if (prepared_logmsg)
7338 28cf319f 2021-01-28 stsp option_conflict('m', 'F');
7339 c4296144 2019-05-09 stsp logmsg = optarg;
7340 28cf319f 2021-01-28 stsp break;
7341 28cf319f 2021-01-28 stsp case 'N':
7342 28cf319f 2021-01-28 stsp non_interactive = 1;
7343 c4296144 2019-05-09 stsp break;
7344 35213c7c 2020-07-23 stsp case 'S':
7345 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
7346 35213c7c 2020-07-23 stsp break;
7347 c4296144 2019-05-09 stsp default:
7348 c4296144 2019-05-09 stsp usage_commit();
7349 c4296144 2019-05-09 stsp /* NOTREACHED */
7350 c4296144 2019-05-09 stsp }
7351 c4296144 2019-05-09 stsp }
7352 c4296144 2019-05-09 stsp
7353 c4296144 2019-05-09 stsp argc -= optind;
7354 c4296144 2019-05-09 stsp argv += optind;
7355 c4296144 2019-05-09 stsp
7356 43012d58 2019-07-14 stsp #ifndef PROFILE
7357 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7358 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7359 43012d58 2019-07-14 stsp err(1, "pledge");
7360 43012d58 2019-07-14 stsp #endif
7361 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
7362 c4296144 2019-05-09 stsp if (cwd == NULL) {
7363 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7364 c4296144 2019-05-09 stsp goto done;
7365 c4296144 2019-05-09 stsp }
7366 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
7367 fa51e947 2020-03-27 stsp if (error) {
7368 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7369 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
7370 7d5807f4 2019-07-11 stsp goto done;
7371 fa51e947 2020-03-27 stsp }
7372 7d5807f4 2019-07-11 stsp
7373 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7374 c4296144 2019-05-09 stsp if (error)
7375 a698f62e 2019-07-25 stsp goto done;
7376 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
7377 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
7378 c4296144 2019-05-09 stsp goto done;
7379 a698f62e 2019-07-25 stsp }
7380 5c1e53bc 2019-07-28 stsp
7381 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7382 916f288c 2019-07-30 stsp worktree);
7383 5c1e53bc 2019-07-28 stsp if (error)
7384 5c1e53bc 2019-07-28 stsp goto done;
7385 c4296144 2019-05-09 stsp
7386 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
7387 c9956ddf 2019-09-08 stsp if (error)
7388 c9956ddf 2019-09-08 stsp goto done;
7389 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7390 c9956ddf 2019-09-08 stsp gitconfig_path);
7391 c4296144 2019-05-09 stsp if (error != NULL)
7392 c4296144 2019-05-09 stsp goto done;
7393 c4296144 2019-05-09 stsp
7394 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
7395 aba9c984 2019-09-08 stsp if (error)
7396 aba9c984 2019-09-08 stsp return error;
7397 aba9c984 2019-09-08 stsp
7398 314a6357 2019-05-13 stsp /*
7399 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
7400 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
7401 314a6357 2019-05-13 stsp */
7402 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
7403 314a6357 2019-05-13 stsp error = get_editor(&editor);
7404 314a6357 2019-05-13 stsp else
7405 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7406 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7407 c4296144 2019-05-09 stsp if (error)
7408 c4296144 2019-05-09 stsp goto done;
7409 c4296144 2019-05-09 stsp
7410 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7411 087fb88c 2019-08-04 stsp if (error)
7412 087fb88c 2019-08-04 stsp goto done;
7413 087fb88c 2019-08-04 stsp
7414 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
7415 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
7416 28cf319f 2021-01-28 stsp cl_arg.prepared_log = prepared_logmsg;
7417 28cf319f 2021-01-28 stsp cl_arg.non_interactive = non_interactive;
7418 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7419 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7420 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
7421 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7422 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
7423 916f288c 2019-07-30 stsp goto done;
7424 916f288c 2019-07-30 stsp }
7425 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
7426 916f288c 2019-07-30 stsp }
7427 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
7428 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7429 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7430 35213c7c 2020-07-23 stsp print_status, NULL, repo);
7431 e0870e44 2019-05-13 stsp if (error) {
7432 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7433 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
7434 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
7435 c4296144 2019-05-09 stsp goto done;
7436 e0870e44 2019-05-13 stsp }
7437 c4296144 2019-05-09 stsp
7438 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
7439 c4296144 2019-05-09 stsp if (error)
7440 c4296144 2019-05-09 stsp goto done;
7441 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
7442 c4296144 2019-05-09 stsp done:
7443 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
7444 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
7445 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
7446 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7447 7266f21f 2019-10-21 stsp error == NULL)
7448 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7449 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
7450 1d0f4054 2021-06-17 stsp if (repo) {
7451 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7452 1d0f4054 2021-06-17 stsp if (error == NULL)
7453 1d0f4054 2021-06-17 stsp error = close_err;
7454 1d0f4054 2021-06-17 stsp }
7455 c4296144 2019-05-09 stsp if (worktree)
7456 c4296144 2019-05-09 stsp got_worktree_close(worktree);
7457 c4296144 2019-05-09 stsp free(cwd);
7458 c4296144 2019-05-09 stsp free(id_str);
7459 c9956ddf 2019-09-08 stsp free(gitconfig_path);
7460 e2ba3d07 2019-05-13 stsp free(editor);
7461 aba9c984 2019-09-08 stsp free(author);
7462 28cf319f 2021-01-28 stsp free(prepared_logmsg);
7463 f8a36e22 2021-08-26 stsp return error;
7464 f8a36e22 2021-08-26 stsp }
7465 f8a36e22 2021-08-26 stsp
7466 f8a36e22 2021-08-26 stsp __dead static void
7467 f8a36e22 2021-08-26 stsp usage_send(void)
7468 f8a36e22 2021-08-26 stsp {
7469 f8a36e22 2021-08-26 stsp fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
7470 f8a36e22 2021-08-26 stsp "[-r repository-path] [-t tag] [-T] [-q] [-v] "
7471 f8a36e22 2021-08-26 stsp "[remote-repository]\n", getprogname());
7472 f8a36e22 2021-08-26 stsp exit(1);
7473 f8a36e22 2021-08-26 stsp }
7474 f8a36e22 2021-08-26 stsp
7475 f8a36e22 2021-08-26 stsp struct got_send_progress_arg {
7476 f8a36e22 2021-08-26 stsp char last_scaled_packsize[FMT_SCALED_STRSIZE];
7477 f8a36e22 2021-08-26 stsp int verbosity;
7478 f8a36e22 2021-08-26 stsp int last_ncommits;
7479 f8a36e22 2021-08-26 stsp int last_nobj_total;
7480 f8a36e22 2021-08-26 stsp int last_p_deltify;
7481 f8a36e22 2021-08-26 stsp int last_p_written;
7482 f8a36e22 2021-08-26 stsp int last_p_sent;
7483 f8a36e22 2021-08-26 stsp int printed_something;
7484 f8a36e22 2021-08-26 stsp int sent_something;
7485 f8a36e22 2021-08-26 stsp struct got_pathlist_head *delete_branches;
7486 f8a36e22 2021-08-26 stsp };
7487 f8a36e22 2021-08-26 stsp
7488 f8a36e22 2021-08-26 stsp static const struct got_error *
7489 f8a36e22 2021-08-26 stsp send_progress(void *arg, off_t packfile_size, int ncommits, int nobj_total,
7490 f8a36e22 2021-08-26 stsp int nobj_deltify, int nobj_written, off_t bytes_sent, const char *refname,
7491 f8a36e22 2021-08-26 stsp int success)
7492 f8a36e22 2021-08-26 stsp {
7493 f8a36e22 2021-08-26 stsp struct got_send_progress_arg *a = arg;
7494 f8a36e22 2021-08-26 stsp char scaled_packsize[FMT_SCALED_STRSIZE];
7495 f8a36e22 2021-08-26 stsp char scaled_sent[FMT_SCALED_STRSIZE];
7496 f8a36e22 2021-08-26 stsp int p_deltify = 0, p_written = 0, p_sent = 0;
7497 f8a36e22 2021-08-26 stsp int print_searching = 0, print_total = 0;
7498 f8a36e22 2021-08-26 stsp int print_deltify = 0, print_written = 0, print_sent = 0;
7499 f8a36e22 2021-08-26 stsp
7500 f8a36e22 2021-08-26 stsp if (a->verbosity < 0)
7501 f8a36e22 2021-08-26 stsp return NULL;
7502 f8a36e22 2021-08-26 stsp
7503 f8a36e22 2021-08-26 stsp if (refname) {
7504 f8a36e22 2021-08-26 stsp const char *status = success ? "accepted" : "rejected";
7505 f8a36e22 2021-08-26 stsp
7506 f8a36e22 2021-08-26 stsp if (success) {
7507 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
7508 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, a->delete_branches, entry) {
7509 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
7510 f8a36e22 2021-08-26 stsp if (got_path_cmp(branchname, refname,
7511 f8a36e22 2021-08-26 stsp strlen(branchname), strlen(refname)) == 0) {
7512 f8a36e22 2021-08-26 stsp status = "deleted";
7513 27b75514 2021-08-28 stsp a->sent_something = 1;
7514 f8a36e22 2021-08-26 stsp break;
7515 f8a36e22 2021-08-26 stsp }
7516 f8a36e22 2021-08-26 stsp }
7517 f8a36e22 2021-08-26 stsp }
7518 f8a36e22 2021-08-26 stsp
7519 27b75514 2021-08-28 stsp if (a->printed_something)
7520 27b75514 2021-08-28 stsp putchar('\n');
7521 27b75514 2021-08-28 stsp printf("Server has %s %s", status, refname);
7522 f8a36e22 2021-08-26 stsp a->printed_something = 1;
7523 f8a36e22 2021-08-26 stsp return NULL;
7524 f8a36e22 2021-08-26 stsp }
7525 f8a36e22 2021-08-26 stsp
7526 f8a36e22 2021-08-26 stsp if (fmt_scaled(packfile_size, scaled_packsize) == -1)
7527 f8a36e22 2021-08-26 stsp return got_error_from_errno("fmt_scaled");
7528 f8a36e22 2021-08-26 stsp if (fmt_scaled(bytes_sent, scaled_sent) == -1)
7529 f8a36e22 2021-08-26 stsp return got_error_from_errno("fmt_scaled");
7530 f8a36e22 2021-08-26 stsp
7531 f8a36e22 2021-08-26 stsp if (a->last_ncommits != ncommits) {
7532 f8a36e22 2021-08-26 stsp print_searching = 1;
7533 f8a36e22 2021-08-26 stsp a->last_ncommits = ncommits;
7534 f8a36e22 2021-08-26 stsp }
7535 f8a36e22 2021-08-26 stsp
7536 f8a36e22 2021-08-26 stsp if (a->last_nobj_total != nobj_total) {
7537 f8a36e22 2021-08-26 stsp print_searching = 1;
7538 f8a36e22 2021-08-26 stsp print_total = 1;
7539 f8a36e22 2021-08-26 stsp a->last_nobj_total = nobj_total;
7540 f8a36e22 2021-08-26 stsp }
7541 f8a36e22 2021-08-26 stsp
7542 f8a36e22 2021-08-26 stsp if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
7543 f8a36e22 2021-08-26 stsp strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
7544 f8a36e22 2021-08-26 stsp if (strlcpy(a->last_scaled_packsize, scaled_packsize,
7545 f8a36e22 2021-08-26 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
7546 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
7547 f8a36e22 2021-08-26 stsp }
7548 f8a36e22 2021-08-26 stsp
7549 f8a36e22 2021-08-26 stsp if (nobj_deltify > 0 || nobj_written > 0) {
7550 f8a36e22 2021-08-26 stsp if (nobj_deltify > 0) {
7551 f8a36e22 2021-08-26 stsp p_deltify = (nobj_deltify * 100) / nobj_total;
7552 f8a36e22 2021-08-26 stsp if (p_deltify != a->last_p_deltify) {
7553 f8a36e22 2021-08-26 stsp a->last_p_deltify = p_deltify;
7554 f8a36e22 2021-08-26 stsp print_searching = 1;
7555 f8a36e22 2021-08-26 stsp print_total = 1;
7556 f8a36e22 2021-08-26 stsp print_deltify = 1;
7557 f8a36e22 2021-08-26 stsp }
7558 f8a36e22 2021-08-26 stsp }
7559 f8a36e22 2021-08-26 stsp if (nobj_written > 0) {
7560 f8a36e22 2021-08-26 stsp p_written = (nobj_written * 100) / nobj_total;
7561 f8a36e22 2021-08-26 stsp if (p_written != a->last_p_written) {
7562 f8a36e22 2021-08-26 stsp a->last_p_written = p_written;
7563 f8a36e22 2021-08-26 stsp print_searching = 1;
7564 f8a36e22 2021-08-26 stsp print_total = 1;
7565 f8a36e22 2021-08-26 stsp print_deltify = 1;
7566 f8a36e22 2021-08-26 stsp print_written = 1;
7567 f8a36e22 2021-08-26 stsp }
7568 f8a36e22 2021-08-26 stsp }
7569 f8a36e22 2021-08-26 stsp }
7570 f8a36e22 2021-08-26 stsp
7571 f8a36e22 2021-08-26 stsp if (bytes_sent > 0) {
7572 f8a36e22 2021-08-26 stsp p_sent = (bytes_sent * 100) / packfile_size;
7573 f8a36e22 2021-08-26 stsp if (p_sent != a->last_p_sent) {
7574 f8a36e22 2021-08-26 stsp a->last_p_sent = p_sent;
7575 f8a36e22 2021-08-26 stsp print_searching = 1;
7576 f8a36e22 2021-08-26 stsp print_total = 1;
7577 f8a36e22 2021-08-26 stsp print_deltify = 1;
7578 f8a36e22 2021-08-26 stsp print_written = 1;
7579 f8a36e22 2021-08-26 stsp print_sent = 1;
7580 f8a36e22 2021-08-26 stsp }
7581 f8a36e22 2021-08-26 stsp a->sent_something = 1;
7582 f8a36e22 2021-08-26 stsp }
7583 f8a36e22 2021-08-26 stsp
7584 f8a36e22 2021-08-26 stsp if (print_searching || print_total || print_deltify || print_written ||
7585 f8a36e22 2021-08-26 stsp print_sent)
7586 f8a36e22 2021-08-26 stsp printf("\r");
7587 f8a36e22 2021-08-26 stsp if (print_searching)
7588 f8a36e22 2021-08-26 stsp printf("packing %d reference%s", ncommits,
7589 f8a36e22 2021-08-26 stsp ncommits == 1 ? "" : "s");
7590 f8a36e22 2021-08-26 stsp if (print_total)
7591 f8a36e22 2021-08-26 stsp printf("; %d object%s", nobj_total,
7592 f8a36e22 2021-08-26 stsp nobj_total == 1 ? "" : "s");
7593 f8a36e22 2021-08-26 stsp if (print_deltify)
7594 f8a36e22 2021-08-26 stsp printf("; deltify: %d%%", p_deltify);
7595 f8a36e22 2021-08-26 stsp if (print_sent)
7596 f8a36e22 2021-08-26 stsp printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE,
7597 f8a36e22 2021-08-26 stsp scaled_packsize, p_sent);
7598 f8a36e22 2021-08-26 stsp else if (print_written)
7599 f8a36e22 2021-08-26 stsp printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
7600 f8a36e22 2021-08-26 stsp scaled_packsize, p_written);
7601 f8a36e22 2021-08-26 stsp if (print_searching || print_total || print_deltify ||
7602 f8a36e22 2021-08-26 stsp print_written || print_sent) {
7603 f8a36e22 2021-08-26 stsp a->printed_something = 1;
7604 f8a36e22 2021-08-26 stsp fflush(stdout);
7605 f8a36e22 2021-08-26 stsp }
7606 f8a36e22 2021-08-26 stsp return NULL;
7607 f8a36e22 2021-08-26 stsp }
7608 f8a36e22 2021-08-26 stsp
7609 f8a36e22 2021-08-26 stsp static const struct got_error *
7610 f8a36e22 2021-08-26 stsp cmd_send(int argc, char *argv[])
7611 f8a36e22 2021-08-26 stsp {
7612 f8a36e22 2021-08-26 stsp const struct got_error *error = NULL;
7613 f8a36e22 2021-08-26 stsp char *cwd = NULL, *repo_path = NULL;
7614 f8a36e22 2021-08-26 stsp const char *remote_name;
7615 f8a36e22 2021-08-26 stsp char *proto = NULL, *host = NULL, *port = NULL;
7616 f8a36e22 2021-08-26 stsp char *repo_name = NULL, *server_path = NULL;
7617 f8a36e22 2021-08-26 stsp const struct got_remote_repo *remotes, *remote = NULL;
7618 f8a36e22 2021-08-26 stsp int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
7619 f8a36e22 2021-08-26 stsp struct got_repository *repo = NULL;
7620 f8a36e22 2021-08-26 stsp struct got_worktree *worktree = NULL;
7621 f8a36e22 2021-08-26 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
7622 f8a36e22 2021-08-26 stsp struct got_pathlist_head branches;
7623 f8a36e22 2021-08-26 stsp struct got_pathlist_head tags;
7624 f8a36e22 2021-08-26 stsp struct got_reflist_head all_branches;
7625 f8a36e22 2021-08-26 stsp struct got_reflist_head all_tags;
7626 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_args;
7627 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_branches;
7628 f8a36e22 2021-08-26 stsp struct got_reflist_entry *re;
7629 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
7630 f8a36e22 2021-08-26 stsp int i, ch, sendfd = -1, sendstatus;
7631 f8a36e22 2021-08-26 stsp pid_t sendpid = -1;
7632 f8a36e22 2021-08-26 stsp struct got_send_progress_arg spa;
7633 f8a36e22 2021-08-26 stsp int verbosity = 0, overwrite_refs = 0;
7634 f8a36e22 2021-08-26 stsp int send_all_branches = 0, send_all_tags = 0;
7635 f8a36e22 2021-08-26 stsp struct got_reference *ref = NULL;
7636 f8a36e22 2021-08-26 stsp
7637 f8a36e22 2021-08-26 stsp TAILQ_INIT(&branches);
7638 f8a36e22 2021-08-26 stsp TAILQ_INIT(&tags);
7639 f8a36e22 2021-08-26 stsp TAILQ_INIT(&all_branches);
7640 f8a36e22 2021-08-26 stsp TAILQ_INIT(&all_tags);
7641 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_args);
7642 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_branches);
7643 f8a36e22 2021-08-26 stsp
7644 f8a36e22 2021-08-26 stsp while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
7645 f8a36e22 2021-08-26 stsp switch (ch) {
7646 f8a36e22 2021-08-26 stsp case 'a':
7647 f8a36e22 2021-08-26 stsp send_all_branches = 1;
7648 f8a36e22 2021-08-26 stsp break;
7649 f8a36e22 2021-08-26 stsp case 'b':
7650 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches, optarg, NULL);
7651 f8a36e22 2021-08-26 stsp if (error)
7652 f8a36e22 2021-08-26 stsp return error;
7653 f8a36e22 2021-08-26 stsp nbranches++;
7654 f8a36e22 2021-08-26 stsp break;
7655 f8a36e22 2021-08-26 stsp case 'd':
7656 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&delete_args, optarg, NULL);
7657 f8a36e22 2021-08-26 stsp if (error)
7658 f8a36e22 2021-08-26 stsp return error;
7659 f8a36e22 2021-08-26 stsp break;
7660 f8a36e22 2021-08-26 stsp case 'f':
7661 f8a36e22 2021-08-26 stsp overwrite_refs = 1;
7662 f8a36e22 2021-08-26 stsp break;
7663 f8a36e22 2021-08-26 stsp case 'r':
7664 f8a36e22 2021-08-26 stsp repo_path = realpath(optarg, NULL);
7665 f8a36e22 2021-08-26 stsp if (repo_path == NULL)
7666 f8a36e22 2021-08-26 stsp return got_error_from_errno2("realpath",
7667 f8a36e22 2021-08-26 stsp optarg);
7668 f8a36e22 2021-08-26 stsp got_path_strip_trailing_slashes(repo_path);
7669 f8a36e22 2021-08-26 stsp break;
7670 f8a36e22 2021-08-26 stsp case 't':
7671 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&tags, optarg, NULL);
7672 f8a36e22 2021-08-26 stsp if (error)
7673 f8a36e22 2021-08-26 stsp return error;
7674 f8a36e22 2021-08-26 stsp ntags++;
7675 f8a36e22 2021-08-26 stsp break;
7676 f8a36e22 2021-08-26 stsp case 'T':
7677 f8a36e22 2021-08-26 stsp send_all_tags = 1;
7678 f8a36e22 2021-08-26 stsp break;
7679 f8a36e22 2021-08-26 stsp case 'v':
7680 f8a36e22 2021-08-26 stsp if (verbosity < 0)
7681 f8a36e22 2021-08-26 stsp verbosity = 0;
7682 f8a36e22 2021-08-26 stsp else if (verbosity < 3)
7683 f8a36e22 2021-08-26 stsp verbosity++;
7684 f8a36e22 2021-08-26 stsp break;
7685 f8a36e22 2021-08-26 stsp case 'q':
7686 f8a36e22 2021-08-26 stsp verbosity = -1;
7687 f8a36e22 2021-08-26 stsp break;
7688 f8a36e22 2021-08-26 stsp default:
7689 f8a36e22 2021-08-26 stsp usage_send();
7690 f8a36e22 2021-08-26 stsp /* NOTREACHED */
7691 f8a36e22 2021-08-26 stsp }
7692 f8a36e22 2021-08-26 stsp }
7693 f8a36e22 2021-08-26 stsp argc -= optind;
7694 f8a36e22 2021-08-26 stsp argv += optind;
7695 f8a36e22 2021-08-26 stsp
7696 f8a36e22 2021-08-26 stsp if (send_all_branches && !TAILQ_EMPTY(&branches))
7697 f8a36e22 2021-08-26 stsp option_conflict('a', 'b');
7698 f8a36e22 2021-08-26 stsp if (send_all_tags && !TAILQ_EMPTY(&tags))
7699 f8a36e22 2021-08-26 stsp option_conflict('T', 't');
7700 f8a36e22 2021-08-26 stsp
7701 f8a36e22 2021-08-26 stsp
7702 f8a36e22 2021-08-26 stsp if (argc == 0)
7703 f8a36e22 2021-08-26 stsp remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
7704 f8a36e22 2021-08-26 stsp else if (argc == 1)
7705 f8a36e22 2021-08-26 stsp remote_name = argv[0];
7706 f8a36e22 2021-08-26 stsp else
7707 f8a36e22 2021-08-26 stsp usage_send();
7708 f8a36e22 2021-08-26 stsp
7709 f8a36e22 2021-08-26 stsp cwd = getcwd(NULL, 0);
7710 f8a36e22 2021-08-26 stsp if (cwd == NULL) {
7711 f8a36e22 2021-08-26 stsp error = got_error_from_errno("getcwd");
7712 f8a36e22 2021-08-26 stsp goto done;
7713 f8a36e22 2021-08-26 stsp }
7714 f8a36e22 2021-08-26 stsp
7715 f8a36e22 2021-08-26 stsp if (repo_path == NULL) {
7716 f8a36e22 2021-08-26 stsp error = got_worktree_open(&worktree, cwd);
7717 f8a36e22 2021-08-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7718 f8a36e22 2021-08-26 stsp goto done;
7719 f8a36e22 2021-08-26 stsp else
7720 f8a36e22 2021-08-26 stsp error = NULL;
7721 f8a36e22 2021-08-26 stsp if (worktree) {
7722 f8a36e22 2021-08-26 stsp repo_path =
7723 f8a36e22 2021-08-26 stsp strdup(got_worktree_get_repo_path(worktree));
7724 f8a36e22 2021-08-26 stsp if (repo_path == NULL)
7725 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
7726 f8a36e22 2021-08-26 stsp if (error)
7727 f8a36e22 2021-08-26 stsp goto done;
7728 f8a36e22 2021-08-26 stsp } else {
7729 f8a36e22 2021-08-26 stsp repo_path = strdup(cwd);
7730 f8a36e22 2021-08-26 stsp if (repo_path == NULL) {
7731 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
7732 f8a36e22 2021-08-26 stsp goto done;
7733 f8a36e22 2021-08-26 stsp }
7734 f8a36e22 2021-08-26 stsp }
7735 f8a36e22 2021-08-26 stsp }
7736 f8a36e22 2021-08-26 stsp
7737 f8a36e22 2021-08-26 stsp error = got_repo_open(&repo, repo_path, NULL);
7738 f8a36e22 2021-08-26 stsp if (error)
7739 f8a36e22 2021-08-26 stsp goto done;
7740 f8a36e22 2021-08-26 stsp
7741 f8a36e22 2021-08-26 stsp if (worktree) {
7742 f8a36e22 2021-08-26 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
7743 f8a36e22 2021-08-26 stsp if (worktree_conf) {
7744 f8a36e22 2021-08-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
7745 f8a36e22 2021-08-26 stsp worktree_conf);
7746 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
7747 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
7748 f8a36e22 2021-08-26 stsp remote = &remotes[i];
7749 f8a36e22 2021-08-26 stsp break;
7750 f8a36e22 2021-08-26 stsp }
7751 f8a36e22 2021-08-26 stsp }
7752 f8a36e22 2021-08-26 stsp }
7753 f8a36e22 2021-08-26 stsp }
7754 f8a36e22 2021-08-26 stsp if (remote == NULL) {
7755 f8a36e22 2021-08-26 stsp repo_conf = got_repo_get_gotconfig(repo);
7756 f8a36e22 2021-08-26 stsp if (repo_conf) {
7757 f8a36e22 2021-08-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
7758 f8a36e22 2021-08-26 stsp repo_conf);
7759 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
7760 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
7761 f8a36e22 2021-08-26 stsp remote = &remotes[i];
7762 f8a36e22 2021-08-26 stsp break;
7763 f8a36e22 2021-08-26 stsp }
7764 f8a36e22 2021-08-26 stsp }
7765 f8a36e22 2021-08-26 stsp }
7766 f8a36e22 2021-08-26 stsp }
7767 f8a36e22 2021-08-26 stsp if (remote == NULL) {
7768 f8a36e22 2021-08-26 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
7769 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
7770 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
7771 f8a36e22 2021-08-26 stsp remote = &remotes[i];
7772 f8a36e22 2021-08-26 stsp break;
7773 f8a36e22 2021-08-26 stsp }
7774 f8a36e22 2021-08-26 stsp }
7775 f8a36e22 2021-08-26 stsp }
7776 f8a36e22 2021-08-26 stsp if (remote == NULL) {
7777 f8a36e22 2021-08-26 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
7778 f8a36e22 2021-08-26 stsp goto done;
7779 f8a36e22 2021-08-26 stsp }
7780 f8a36e22 2021-08-26 stsp
7781 5e5da8c4 2021-09-05 stsp error = got_dial_parse_uri(&proto, &host, &port, &server_path,
7782 6480c871 2021-08-30 stsp &repo_name, remote->send_url);
7783 f8a36e22 2021-08-26 stsp if (error)
7784 f8a36e22 2021-08-26 stsp goto done;
7785 f8a36e22 2021-08-26 stsp
7786 f8a36e22 2021-08-26 stsp if (strcmp(proto, "git") == 0) {
7787 f8a36e22 2021-08-26 stsp #ifndef PROFILE
7788 f8a36e22 2021-08-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7789 f8a36e22 2021-08-26 stsp "sendfd dns inet unveil", NULL) == -1)
7790 f8a36e22 2021-08-26 stsp err(1, "pledge");
7791 f8a36e22 2021-08-26 stsp #endif
7792 f8a36e22 2021-08-26 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
7793 f8a36e22 2021-08-26 stsp strcmp(proto, "ssh") == 0) {
7794 f8a36e22 2021-08-26 stsp #ifndef PROFILE
7795 f8a36e22 2021-08-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7796 f8a36e22 2021-08-26 stsp "sendfd unveil", NULL) == -1)
7797 f8a36e22 2021-08-26 stsp err(1, "pledge");
7798 f8a36e22 2021-08-26 stsp #endif
7799 f8a36e22 2021-08-26 stsp } else if (strcmp(proto, "http") == 0 ||
7800 f8a36e22 2021-08-26 stsp strcmp(proto, "git+http") == 0) {
7801 f8a36e22 2021-08-26 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
7802 f8a36e22 2021-08-26 stsp goto done;
7803 f8a36e22 2021-08-26 stsp } else {
7804 f8a36e22 2021-08-26 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
7805 f8a36e22 2021-08-26 stsp goto done;
7806 f8a36e22 2021-08-26 stsp }
7807 f8a36e22 2021-08-26 stsp
7808 d65a88a2 2021-09-05 stsp error = got_dial_apply_unveil(proto);
7809 d65a88a2 2021-09-05 stsp if (error)
7810 d65a88a2 2021-09-05 stsp goto done;
7811 d65a88a2 2021-09-05 stsp
7812 f8a36e22 2021-08-26 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7813 f8a36e22 2021-08-26 stsp if (error)
7814 f8a36e22 2021-08-26 stsp goto done;
7815 f8a36e22 2021-08-26 stsp
7816 f8a36e22 2021-08-26 stsp if (send_all_branches) {
7817 f8a36e22 2021-08-26 stsp error = got_ref_list(&all_branches, repo, "refs/heads",
7818 f8a36e22 2021-08-26 stsp got_ref_cmp_by_name, NULL);
7819 f8a36e22 2021-08-26 stsp if (error)
7820 f8a36e22 2021-08-26 stsp goto done;
7821 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &all_branches, entry) {
7822 f8a36e22 2021-08-26 stsp const char *branchname = got_ref_get_name(re->ref);
7823 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches,
7824 f8a36e22 2021-08-26 stsp branchname, NULL);
7825 f8a36e22 2021-08-26 stsp if (error)
7826 f8a36e22 2021-08-26 stsp goto done;
7827 f8a36e22 2021-08-26 stsp nbranches++;
7828 f8a36e22 2021-08-26 stsp }
7829 eac1df47 2021-09-01 stsp } else if (nbranches == 0) {
7830 eac1df47 2021-09-01 stsp for (i = 0; i < remote->nsend_branches; i++) {
7831 eac1df47 2021-09-01 stsp got_pathlist_append(&branches,
7832 eac1df47 2021-09-01 stsp remote->send_branches[i], NULL);
7833 eac1df47 2021-09-01 stsp }
7834 f8a36e22 2021-08-26 stsp }
7835 f8a36e22 2021-08-26 stsp
7836 f8a36e22 2021-08-26 stsp if (send_all_tags) {
7837 f8a36e22 2021-08-26 stsp error = got_ref_list(&all_tags, repo, "refs/tags",
7838 f8a36e22 2021-08-26 stsp got_ref_cmp_by_name, NULL);
7839 f8a36e22 2021-08-26 stsp if (error)
7840 f8a36e22 2021-08-26 stsp goto done;
7841 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &all_tags, entry) {
7842 f8a36e22 2021-08-26 stsp const char *tagname = got_ref_get_name(re->ref);
7843 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&tags,
7844 f8a36e22 2021-08-26 stsp tagname, NULL);
7845 f8a36e22 2021-08-26 stsp if (error)
7846 f8a36e22 2021-08-26 stsp goto done;
7847 f8a36e22 2021-08-26 stsp ntags++;
7848 f8a36e22 2021-08-26 stsp }
7849 f8a36e22 2021-08-26 stsp }
7850 f8a36e22 2021-08-26 stsp
7851 f8a36e22 2021-08-26 stsp /*
7852 f8a36e22 2021-08-26 stsp * To prevent accidents only branches in refs/heads/ can be deleted
7853 f8a36e22 2021-08-26 stsp * with 'got send -d'.
7854 f8a36e22 2021-08-26 stsp * Deleting anything else requires local repository access or Git.
7855 f8a36e22 2021-08-26 stsp */
7856 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_args, entry) {
7857 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
7858 f8a36e22 2021-08-26 stsp char *s;
7859 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *new;
7860 f8a36e22 2021-08-26 stsp if (strncmp(branchname, "refs/heads/", 11) == 0) {
7861 f8a36e22 2021-08-26 stsp s = strdup(branchname);
7862 f8a36e22 2021-08-26 stsp if (s == NULL) {
7863 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
7864 f8a36e22 2021-08-26 stsp goto done;
7865 f8a36e22 2021-08-26 stsp }
7866 f8a36e22 2021-08-26 stsp } else {
7867 f8a36e22 2021-08-26 stsp if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
7868 f8a36e22 2021-08-26 stsp error = got_error_from_errno("asprintf");
7869 f8a36e22 2021-08-26 stsp goto done;
7870 f8a36e22 2021-08-26 stsp }
7871 f8a36e22 2021-08-26 stsp }
7872 f8a36e22 2021-08-26 stsp error = got_pathlist_insert(&new, &delete_branches, s, NULL);
7873 f8a36e22 2021-08-26 stsp if (error || new == NULL /* duplicate */)
7874 f8a36e22 2021-08-26 stsp free(s);
7875 f8a36e22 2021-08-26 stsp if (error)
7876 f8a36e22 2021-08-26 stsp goto done;
7877 f8a36e22 2021-08-26 stsp ndelete_branches++;
7878 f8a36e22 2021-08-26 stsp }
7879 f8a36e22 2021-08-26 stsp
7880 f8a36e22 2021-08-26 stsp if (nbranches == 0 && ndelete_branches == 0) {
7881 f8a36e22 2021-08-26 stsp struct got_reference *head_ref;
7882 f8a36e22 2021-08-26 stsp if (worktree)
7883 f8a36e22 2021-08-26 stsp error = got_ref_open(&head_ref, repo,
7884 f8a36e22 2021-08-26 stsp got_worktree_get_head_ref_name(worktree), 0);
7885 f8a36e22 2021-08-26 stsp else
7886 f8a36e22 2021-08-26 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
7887 f8a36e22 2021-08-26 stsp if (error)
7888 f8a36e22 2021-08-26 stsp goto done;
7889 f8a36e22 2021-08-26 stsp if (got_ref_is_symbolic(head_ref)) {
7890 f8a36e22 2021-08-26 stsp error = got_ref_resolve_symbolic(&ref, repo, head_ref);
7891 f8a36e22 2021-08-26 stsp got_ref_close(head_ref);
7892 f8a36e22 2021-08-26 stsp if (error)
7893 f8a36e22 2021-08-26 stsp goto done;
7894 f8a36e22 2021-08-26 stsp } else
7895 f8a36e22 2021-08-26 stsp ref = head_ref;
7896 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches, got_ref_get_name(ref),
7897 abc59930 2021-09-05 naddy NULL);
7898 f8a36e22 2021-08-26 stsp if (error)
7899 f8a36e22 2021-08-26 stsp goto done;
7900 f8a36e22 2021-08-26 stsp nbranches++;
7901 f8a36e22 2021-08-26 stsp }
7902 f8a36e22 2021-08-26 stsp
7903 f8a36e22 2021-08-26 stsp if (verbosity >= 0)
7904 f8a36e22 2021-08-26 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
7905 f8a36e22 2021-08-26 stsp port ? ":" : "", port ? port : "");
7906 f8a36e22 2021-08-26 stsp
7907 f8a36e22 2021-08-26 stsp error = got_send_connect(&sendpid, &sendfd, proto, host, port,
7908 f8a36e22 2021-08-26 stsp server_path, verbosity);
7909 f8a36e22 2021-08-26 stsp if (error)
7910 f8a36e22 2021-08-26 stsp goto done;
7911 f8a36e22 2021-08-26 stsp
7912 f8a36e22 2021-08-26 stsp memset(&spa, 0, sizeof(spa));
7913 f8a36e22 2021-08-26 stsp spa.last_scaled_packsize[0] = '\0';
7914 f8a36e22 2021-08-26 stsp spa.last_p_deltify = -1;
7915 f8a36e22 2021-08-26 stsp spa.last_p_written = -1;
7916 f8a36e22 2021-08-26 stsp spa.verbosity = verbosity;
7917 f8a36e22 2021-08-26 stsp spa.delete_branches = &delete_branches;
7918 f8a36e22 2021-08-26 stsp error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
7919 f8a36e22 2021-08-26 stsp verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
7920 f8a36e22 2021-08-26 stsp check_cancelled, NULL);
7921 f8a36e22 2021-08-26 stsp if (spa.printed_something)
7922 f8a36e22 2021-08-26 stsp putchar('\n');
7923 f8a36e22 2021-08-26 stsp if (error)
7924 f8a36e22 2021-08-26 stsp goto done;
7925 f8a36e22 2021-08-26 stsp if (!spa.sent_something && verbosity >= 0)
7926 f8a36e22 2021-08-26 stsp printf("Already up-to-date\n");
7927 f8a36e22 2021-08-26 stsp done:
7928 f8a36e22 2021-08-26 stsp if (sendpid > 0) {
7929 f8a36e22 2021-08-26 stsp if (kill(sendpid, SIGTERM) == -1)
7930 f8a36e22 2021-08-26 stsp error = got_error_from_errno("kill");
7931 f8a36e22 2021-08-26 stsp if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
7932 f8a36e22 2021-08-26 stsp error = got_error_from_errno("waitpid");
7933 f8a36e22 2021-08-26 stsp }
7934 f8a36e22 2021-08-26 stsp if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
7935 f8a36e22 2021-08-26 stsp error = got_error_from_errno("close");
7936 f8a36e22 2021-08-26 stsp if (repo) {
7937 f8a36e22 2021-08-26 stsp const struct got_error *close_err = got_repo_close(repo);
7938 f8a36e22 2021-08-26 stsp if (error == NULL)
7939 f8a36e22 2021-08-26 stsp error = close_err;
7940 f8a36e22 2021-08-26 stsp }
7941 f8a36e22 2021-08-26 stsp if (worktree)
7942 f8a36e22 2021-08-26 stsp got_worktree_close(worktree);
7943 f8a36e22 2021-08-26 stsp if (ref)
7944 f8a36e22 2021-08-26 stsp got_ref_close(ref);
7945 f8a36e22 2021-08-26 stsp got_pathlist_free(&branches);
7946 f8a36e22 2021-08-26 stsp got_pathlist_free(&tags);
7947 f8a36e22 2021-08-26 stsp got_ref_list_free(&all_branches);
7948 f8a36e22 2021-08-26 stsp got_ref_list_free(&all_tags);
7949 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_args);
7950 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_branches, entry)
7951 f8a36e22 2021-08-26 stsp free((char *)pe->path);
7952 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_branches);
7953 f8a36e22 2021-08-26 stsp free(cwd);
7954 f8a36e22 2021-08-26 stsp free(repo_path);
7955 f8a36e22 2021-08-26 stsp free(proto);
7956 f8a36e22 2021-08-26 stsp free(host);
7957 f8a36e22 2021-08-26 stsp free(port);
7958 f8a36e22 2021-08-26 stsp free(server_path);
7959 f8a36e22 2021-08-26 stsp free(repo_name);
7960 234035bc 2019-06-01 stsp return error;
7961 234035bc 2019-06-01 stsp }
7962 234035bc 2019-06-01 stsp
7963 234035bc 2019-06-01 stsp __dead static void
7964 234035bc 2019-06-01 stsp usage_cherrypick(void)
7965 234035bc 2019-06-01 stsp {
7966 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
7967 234035bc 2019-06-01 stsp exit(1);
7968 234035bc 2019-06-01 stsp }
7969 234035bc 2019-06-01 stsp
7970 234035bc 2019-06-01 stsp static const struct got_error *
7971 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
7972 234035bc 2019-06-01 stsp {
7973 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
7974 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
7975 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
7976 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
7977 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
7978 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
7979 234035bc 2019-06-01 stsp struct got_object_qid *pid;
7980 9627c110 2020-04-18 stsp int ch;
7981 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7982 234035bc 2019-06-01 stsp
7983 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7984 234035bc 2019-06-01 stsp switch (ch) {
7985 234035bc 2019-06-01 stsp default:
7986 234035bc 2019-06-01 stsp usage_cherrypick();
7987 234035bc 2019-06-01 stsp /* NOTREACHED */
7988 234035bc 2019-06-01 stsp }
7989 234035bc 2019-06-01 stsp }
7990 234035bc 2019-06-01 stsp
7991 234035bc 2019-06-01 stsp argc -= optind;
7992 234035bc 2019-06-01 stsp argv += optind;
7993 234035bc 2019-06-01 stsp
7994 43012d58 2019-07-14 stsp #ifndef PROFILE
7995 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7996 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7997 43012d58 2019-07-14 stsp err(1, "pledge");
7998 43012d58 2019-07-14 stsp #endif
7999 234035bc 2019-06-01 stsp if (argc != 1)
8000 234035bc 2019-06-01 stsp usage_cherrypick();
8001 234035bc 2019-06-01 stsp
8002 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
8003 234035bc 2019-06-01 stsp if (cwd == NULL) {
8004 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
8005 234035bc 2019-06-01 stsp goto done;
8006 234035bc 2019-06-01 stsp }
8007 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
8008 fa51e947 2020-03-27 stsp if (error) {
8009 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8010 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
8011 fa51e947 2020-03-27 stsp cwd);
8012 234035bc 2019-06-01 stsp goto done;
8013 fa51e947 2020-03-27 stsp }
8014 234035bc 2019-06-01 stsp
8015 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8016 c9956ddf 2019-09-08 stsp NULL);
8017 234035bc 2019-06-01 stsp if (error != NULL)
8018 234035bc 2019-06-01 stsp goto done;
8019 234035bc 2019-06-01 stsp
8020 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8021 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
8022 234035bc 2019-06-01 stsp if (error)
8023 234035bc 2019-06-01 stsp goto done;
8024 234035bc 2019-06-01 stsp
8025 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8026 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
8027 234035bc 2019-06-01 stsp if (error != NULL) {
8028 234035bc 2019-06-01 stsp struct got_reference *ref;
8029 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8030 234035bc 2019-06-01 stsp goto done;
8031 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
8032 234035bc 2019-06-01 stsp if (error != NULL)
8033 234035bc 2019-06-01 stsp goto done;
8034 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
8035 234035bc 2019-06-01 stsp got_ref_close(ref);
8036 234035bc 2019-06-01 stsp if (error != NULL)
8037 234035bc 2019-06-01 stsp goto done;
8038 234035bc 2019-06-01 stsp }
8039 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
8040 234035bc 2019-06-01 stsp if (error)
8041 234035bc 2019-06-01 stsp goto done;
8042 234035bc 2019-06-01 stsp
8043 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8044 234035bc 2019-06-01 stsp if (error)
8045 234035bc 2019-06-01 stsp goto done;
8046 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8047 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8048 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
8049 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
8050 03415a1a 2019-06-02 stsp NULL);
8051 234035bc 2019-06-01 stsp if (error != NULL)
8052 234035bc 2019-06-01 stsp goto done;
8053 234035bc 2019-06-01 stsp
8054 9627c110 2020-04-18 stsp if (upa.did_something)
8055 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
8056 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8057 234035bc 2019-06-01 stsp done:
8058 234035bc 2019-06-01 stsp if (commit)
8059 234035bc 2019-06-01 stsp got_object_commit_close(commit);
8060 234035bc 2019-06-01 stsp free(commit_id_str);
8061 234035bc 2019-06-01 stsp if (worktree)
8062 234035bc 2019-06-01 stsp got_worktree_close(worktree);
8063 1d0f4054 2021-06-17 stsp if (repo) {
8064 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8065 1d0f4054 2021-06-17 stsp if (error == NULL)
8066 1d0f4054 2021-06-17 stsp error = close_err;
8067 1d0f4054 2021-06-17 stsp }
8068 c4296144 2019-05-09 stsp return error;
8069 c4296144 2019-05-09 stsp }
8070 5ef14e63 2019-06-02 stsp
8071 5ef14e63 2019-06-02 stsp __dead static void
8072 5ef14e63 2019-06-02 stsp usage_backout(void)
8073 5ef14e63 2019-06-02 stsp {
8074 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
8075 5ef14e63 2019-06-02 stsp exit(1);
8076 5ef14e63 2019-06-02 stsp }
8077 5ef14e63 2019-06-02 stsp
8078 5ef14e63 2019-06-02 stsp static const struct got_error *
8079 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
8080 5ef14e63 2019-06-02 stsp {
8081 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
8082 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
8083 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
8084 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
8085 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
8086 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
8087 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
8088 9627c110 2020-04-18 stsp int ch;
8089 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8090 5ef14e63 2019-06-02 stsp
8091 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8092 5ef14e63 2019-06-02 stsp switch (ch) {
8093 5ef14e63 2019-06-02 stsp default:
8094 5ef14e63 2019-06-02 stsp usage_backout();
8095 5ef14e63 2019-06-02 stsp /* NOTREACHED */
8096 5ef14e63 2019-06-02 stsp }
8097 5ef14e63 2019-06-02 stsp }
8098 5ef14e63 2019-06-02 stsp
8099 5ef14e63 2019-06-02 stsp argc -= optind;
8100 5ef14e63 2019-06-02 stsp argv += optind;
8101 5ef14e63 2019-06-02 stsp
8102 43012d58 2019-07-14 stsp #ifndef PROFILE
8103 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8104 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8105 43012d58 2019-07-14 stsp err(1, "pledge");
8106 43012d58 2019-07-14 stsp #endif
8107 5ef14e63 2019-06-02 stsp if (argc != 1)
8108 5ef14e63 2019-06-02 stsp usage_backout();
8109 5ef14e63 2019-06-02 stsp
8110 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
8111 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
8112 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
8113 5ef14e63 2019-06-02 stsp goto done;
8114 5ef14e63 2019-06-02 stsp }
8115 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
8116 fa51e947 2020-03-27 stsp if (error) {
8117 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8118 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
8119 5ef14e63 2019-06-02 stsp goto done;
8120 fa51e947 2020-03-27 stsp }
8121 5ef14e63 2019-06-02 stsp
8122 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8123 c9956ddf 2019-09-08 stsp NULL);
8124 5ef14e63 2019-06-02 stsp if (error != NULL)
8125 5ef14e63 2019-06-02 stsp goto done;
8126 5ef14e63 2019-06-02 stsp
8127 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8128 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
8129 5ef14e63 2019-06-02 stsp if (error)
8130 5ef14e63 2019-06-02 stsp goto done;
8131 5ef14e63 2019-06-02 stsp
8132 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8133 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
8134 5ef14e63 2019-06-02 stsp if (error != NULL) {
8135 5ef14e63 2019-06-02 stsp struct got_reference *ref;
8136 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8137 5ef14e63 2019-06-02 stsp goto done;
8138 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
8139 5ef14e63 2019-06-02 stsp if (error != NULL)
8140 5ef14e63 2019-06-02 stsp goto done;
8141 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
8142 5ef14e63 2019-06-02 stsp got_ref_close(ref);
8143 5ef14e63 2019-06-02 stsp if (error != NULL)
8144 5ef14e63 2019-06-02 stsp goto done;
8145 5ef14e63 2019-06-02 stsp }
8146 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
8147 5ef14e63 2019-06-02 stsp if (error)
8148 5ef14e63 2019-06-02 stsp goto done;
8149 5ef14e63 2019-06-02 stsp
8150 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8151 5ef14e63 2019-06-02 stsp if (error)
8152 5ef14e63 2019-06-02 stsp goto done;
8153 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8154 5ef14e63 2019-06-02 stsp if (pid == NULL) {
8155 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
8156 5ef14e63 2019-06-02 stsp goto done;
8157 5ef14e63 2019-06-02 stsp }
8158 5ef14e63 2019-06-02 stsp
8159 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8160 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
8161 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8162 5ef14e63 2019-06-02 stsp if (error != NULL)
8163 5ef14e63 2019-06-02 stsp goto done;
8164 5ef14e63 2019-06-02 stsp
8165 9627c110 2020-04-18 stsp if (upa.did_something)
8166 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
8167 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8168 5ef14e63 2019-06-02 stsp done:
8169 5ef14e63 2019-06-02 stsp if (commit)
8170 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
8171 5ef14e63 2019-06-02 stsp free(commit_id_str);
8172 818c7501 2019-07-11 stsp if (worktree)
8173 818c7501 2019-07-11 stsp got_worktree_close(worktree);
8174 1d0f4054 2021-06-17 stsp if (repo) {
8175 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8176 1d0f4054 2021-06-17 stsp if (error == NULL)
8177 1d0f4054 2021-06-17 stsp error = close_err;
8178 1d0f4054 2021-06-17 stsp }
8179 818c7501 2019-07-11 stsp return error;
8180 818c7501 2019-07-11 stsp }
8181 818c7501 2019-07-11 stsp
8182 818c7501 2019-07-11 stsp __dead static void
8183 818c7501 2019-07-11 stsp usage_rebase(void)
8184 818c7501 2019-07-11 stsp {
8185 643b85bc 2021-07-16 stsp fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
8186 af54c8f8 2019-07-11 stsp getprogname());
8187 818c7501 2019-07-11 stsp exit(1);
8188 0ebf8283 2019-07-24 stsp }
8189 0ebf8283 2019-07-24 stsp
8190 0ebf8283 2019-07-24 stsp void
8191 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
8192 0ebf8283 2019-07-24 stsp {
8193 0ebf8283 2019-07-24 stsp char *nl;
8194 0ebf8283 2019-07-24 stsp size_t len;
8195 0ebf8283 2019-07-24 stsp
8196 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
8197 0ebf8283 2019-07-24 stsp if (len > limit)
8198 0ebf8283 2019-07-24 stsp len = limit;
8199 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
8200 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
8201 0ebf8283 2019-07-24 stsp if (nl)
8202 0ebf8283 2019-07-24 stsp *nl = '\0';
8203 0ebf8283 2019-07-24 stsp }
8204 0ebf8283 2019-07-24 stsp
8205 0ebf8283 2019-07-24 stsp static const struct got_error *
8206 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
8207 0ebf8283 2019-07-24 stsp {
8208 5943eee2 2019-08-13 stsp const struct got_error *err;
8209 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
8210 5943eee2 2019-08-13 stsp const char *s;
8211 0ebf8283 2019-07-24 stsp
8212 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
8213 5943eee2 2019-08-13 stsp if (err)
8214 5943eee2 2019-08-13 stsp return err;
8215 0ebf8283 2019-07-24 stsp
8216 5943eee2 2019-08-13 stsp s = logmsg0;
8217 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
8218 5943eee2 2019-08-13 stsp s++;
8219 5943eee2 2019-08-13 stsp
8220 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
8221 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
8222 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
8223 5943eee2 2019-08-13 stsp goto done;
8224 5943eee2 2019-08-13 stsp }
8225 0ebf8283 2019-07-24 stsp
8226 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
8227 5943eee2 2019-08-13 stsp done:
8228 5943eee2 2019-08-13 stsp free(logmsg0);
8229 a0ea4fc0 2020-02-28 stsp return err;
8230 a0ea4fc0 2020-02-28 stsp }
8231 a0ea4fc0 2020-02-28 stsp
8232 a0ea4fc0 2020-02-28 stsp static const struct got_error *
8233 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
8234 a0ea4fc0 2020-02-28 stsp {
8235 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
8236 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
8237 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
8238 a0ea4fc0 2020-02-28 stsp
8239 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
8240 a0ea4fc0 2020-02-28 stsp if (err)
8241 a0ea4fc0 2020-02-28 stsp return err;
8242 a0ea4fc0 2020-02-28 stsp
8243 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
8244 a0ea4fc0 2020-02-28 stsp if (err)
8245 a0ea4fc0 2020-02-28 stsp goto done;
8246 a0ea4fc0 2020-02-28 stsp
8247 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
8248 a0ea4fc0 2020-02-28 stsp
8249 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
8250 a0ea4fc0 2020-02-28 stsp if (err)
8251 a0ea4fc0 2020-02-28 stsp goto done;
8252 a0ea4fc0 2020-02-28 stsp
8253 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
8254 a0ea4fc0 2020-02-28 stsp done:
8255 a0ea4fc0 2020-02-28 stsp free(id_str);
8256 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
8257 a0ea4fc0 2020-02-28 stsp free(logmsg);
8258 5943eee2 2019-08-13 stsp return err;
8259 818c7501 2019-07-11 stsp }
8260 818c7501 2019-07-11 stsp
8261 818c7501 2019-07-11 stsp static const struct got_error *
8262 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
8263 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
8264 818c7501 2019-07-11 stsp {
8265 818c7501 2019-07-11 stsp const struct got_error *err;
8266 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8267 818c7501 2019-07-11 stsp
8268 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
8269 818c7501 2019-07-11 stsp if (err)
8270 818c7501 2019-07-11 stsp goto done;
8271 818c7501 2019-07-11 stsp
8272 ff0d2220 2019-07-11 stsp if (new_id) {
8273 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
8274 ff0d2220 2019-07-11 stsp if (err)
8275 ff0d2220 2019-07-11 stsp goto done;
8276 ff0d2220 2019-07-11 stsp }
8277 818c7501 2019-07-11 stsp
8278 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
8279 ff0d2220 2019-07-11 stsp if (new_id_str)
8280 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
8281 0ebf8283 2019-07-24 stsp
8282 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8283 0ebf8283 2019-07-24 stsp if (err)
8284 0ebf8283 2019-07-24 stsp goto done;
8285 0ebf8283 2019-07-24 stsp
8286 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
8287 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8288 818c7501 2019-07-11 stsp done:
8289 818c7501 2019-07-11 stsp free(old_id_str);
8290 818c7501 2019-07-11 stsp free(new_id_str);
8291 272a1371 2020-02-28 stsp free(logmsg);
8292 818c7501 2019-07-11 stsp return err;
8293 818c7501 2019-07-11 stsp }
8294 818c7501 2019-07-11 stsp
8295 1ee397ad 2019-07-12 stsp static const struct got_error *
8296 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
8297 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
8298 e600f124 2021-03-21 stsp struct got_reference *tmp_branch, struct got_repository *repo,
8299 e600f124 2021-03-21 stsp int create_backup)
8300 818c7501 2019-07-11 stsp {
8301 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
8302 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
8303 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, branch, repo, create_backup);
8304 818c7501 2019-07-11 stsp }
8305 818c7501 2019-07-11 stsp
8306 818c7501 2019-07-11 stsp static const struct got_error *
8307 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
8308 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8309 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
8310 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8311 ff0d2220 2019-07-11 stsp {
8312 ff0d2220 2019-07-11 stsp const struct got_error *error;
8313 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
8314 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
8315 ff0d2220 2019-07-11 stsp
8316 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8317 ff0d2220 2019-07-11 stsp if (error)
8318 ff0d2220 2019-07-11 stsp return error;
8319 ff0d2220 2019-07-11 stsp
8320 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
8321 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
8322 ff0d2220 2019-07-11 stsp if (error) {
8323 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
8324 ff0d2220 2019-07-11 stsp goto done;
8325 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
8326 ff0d2220 2019-07-11 stsp } else {
8327 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
8328 ff0d2220 2019-07-11 stsp free(new_commit_id);
8329 ff0d2220 2019-07-11 stsp }
8330 ff0d2220 2019-07-11 stsp done:
8331 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
8332 ff0d2220 2019-07-11 stsp return error;
8333 64c6d990 2019-07-11 stsp }
8334 64c6d990 2019-07-11 stsp
8335 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
8336 64c6d990 2019-07-11 stsp const char *path_prefix;
8337 64c6d990 2019-07-11 stsp size_t len;
8338 8ca9bd68 2019-07-25 stsp int errcode;
8339 64c6d990 2019-07-11 stsp };
8340 64c6d990 2019-07-11 stsp
8341 64c6d990 2019-07-11 stsp static const struct got_error *
8342 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
8343 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
8344 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
8345 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
8346 64c6d990 2019-07-11 stsp {
8347 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
8348 64c6d990 2019-07-11 stsp
8349 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
8350 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
8351 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
8352 64c6d990 2019-07-11 stsp
8353 64c6d990 2019-07-11 stsp return NULL;
8354 64c6d990 2019-07-11 stsp }
8355 64c6d990 2019-07-11 stsp
8356 64c6d990 2019-07-11 stsp static const struct got_error *
8357 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
8358 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
8359 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
8360 64c6d990 2019-07-11 stsp {
8361 64c6d990 2019-07-11 stsp const struct got_error *err;
8362 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
8363 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
8364 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
8365 64c6d990 2019-07-11 stsp
8366 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
8367 64c6d990 2019-07-11 stsp return NULL;
8368 64c6d990 2019-07-11 stsp
8369 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8370 64c6d990 2019-07-11 stsp if (err)
8371 64c6d990 2019-07-11 stsp goto done;
8372 64c6d990 2019-07-11 stsp
8373 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
8374 64c6d990 2019-07-11 stsp if (err)
8375 64c6d990 2019-07-11 stsp goto done;
8376 64c6d990 2019-07-11 stsp
8377 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
8378 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
8379 64c6d990 2019-07-11 stsp if (err)
8380 64c6d990 2019-07-11 stsp goto done;
8381 64c6d990 2019-07-11 stsp
8382 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
8383 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
8384 64c6d990 2019-07-11 stsp if (err)
8385 64c6d990 2019-07-11 stsp goto done;
8386 64c6d990 2019-07-11 stsp
8387 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
8388 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
8389 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
8390 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
8391 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
8392 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
8393 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
8394 64c6d990 2019-07-11 stsp done:
8395 64c6d990 2019-07-11 stsp if (tree1)
8396 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
8397 64c6d990 2019-07-11 stsp if (tree2)
8398 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
8399 64c6d990 2019-07-11 stsp if (commit)
8400 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
8401 64c6d990 2019-07-11 stsp if (parent_commit)
8402 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
8403 64c6d990 2019-07-11 stsp return err;
8404 ff0d2220 2019-07-11 stsp }
8405 ff0d2220 2019-07-11 stsp
8406 ff0d2220 2019-07-11 stsp static const struct got_error *
8407 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
8408 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
8409 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
8410 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
8411 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
8412 af61c510 2019-07-19 stsp {
8413 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
8414 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
8415 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
8416 af61c510 2019-07-19 stsp struct got_object_qid *qid;
8417 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
8418 af61c510 2019-07-19 stsp
8419 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
8420 af61c510 2019-07-19 stsp if (err)
8421 af61c510 2019-07-19 stsp return err;
8422 af61c510 2019-07-19 stsp
8423 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
8424 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
8425 af61c510 2019-07-19 stsp if (err)
8426 af61c510 2019-07-19 stsp goto done;
8427 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
8428 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
8429 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
8430 af61c510 2019-07-19 stsp if (err) {
8431 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
8432 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
8433 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
8434 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
8435 af61c510 2019-07-19 stsp "been reached?!?");
8436 ee780d5c 2020-01-04 stsp }
8437 ee780d5c 2020-01-04 stsp goto done;
8438 af61c510 2019-07-19 stsp } else {
8439 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
8440 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
8441 af61c510 2019-07-19 stsp if (err)
8442 af61c510 2019-07-19 stsp goto done;
8443 af61c510 2019-07-19 stsp
8444 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
8445 af61c510 2019-07-19 stsp if (err)
8446 af61c510 2019-07-19 stsp goto done;
8447 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(commits, qid, entry);
8448 af61c510 2019-07-19 stsp commit_id = parent_id;
8449 af61c510 2019-07-19 stsp }
8450 af61c510 2019-07-19 stsp }
8451 af61c510 2019-07-19 stsp done:
8452 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
8453 af61c510 2019-07-19 stsp return err;
8454 e600f124 2021-03-21 stsp }
8455 e600f124 2021-03-21 stsp
8456 e600f124 2021-03-21 stsp static const struct got_error *
8457 e600f124 2021-03-21 stsp get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
8458 e600f124 2021-03-21 stsp {
8459 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
8460 e600f124 2021-03-21 stsp time_t committer_time;
8461 e600f124 2021-03-21 stsp struct tm tm;
8462 e600f124 2021-03-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
8463 e600f124 2021-03-21 stsp char *author0 = NULL, *author, *smallerthan;
8464 e600f124 2021-03-21 stsp char *logmsg0 = NULL, *logmsg, *newline;
8465 e600f124 2021-03-21 stsp
8466 e600f124 2021-03-21 stsp committer_time = got_object_commit_get_committer_time(commit);
8467 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
8468 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
8469 e3199de8 2021-03-21 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
8470 e600f124 2021-03-21 stsp return got_error(GOT_ERR_NO_SPACE);
8471 e600f124 2021-03-21 stsp
8472 e600f124 2021-03-21 stsp author0 = strdup(got_object_commit_get_author(commit));
8473 e600f124 2021-03-21 stsp if (author0 == NULL)
8474 e600f124 2021-03-21 stsp return got_error_from_errno("strdup");
8475 e600f124 2021-03-21 stsp author = author0;
8476 e600f124 2021-03-21 stsp smallerthan = strchr(author, '<');
8477 e600f124 2021-03-21 stsp if (smallerthan && smallerthan[1] != '\0')
8478 e600f124 2021-03-21 stsp author = smallerthan + 1;
8479 e600f124 2021-03-21 stsp author[strcspn(author, "@>")] = '\0';
8480 e600f124 2021-03-21 stsp
8481 e600f124 2021-03-21 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
8482 e600f124 2021-03-21 stsp if (err)
8483 e600f124 2021-03-21 stsp goto done;
8484 e600f124 2021-03-21 stsp logmsg = logmsg0;
8485 e600f124 2021-03-21 stsp while (*logmsg == '\n')
8486 e600f124 2021-03-21 stsp logmsg++;
8487 e600f124 2021-03-21 stsp newline = strchr(logmsg, '\n');
8488 e600f124 2021-03-21 stsp if (newline)
8489 e600f124 2021-03-21 stsp *newline = '\0';
8490 e600f124 2021-03-21 stsp
8491 e600f124 2021-03-21 stsp if (asprintf(brief_str, "%s %s %s",
8492 e600f124 2021-03-21 stsp datebuf, author, logmsg) == -1)
8493 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
8494 e600f124 2021-03-21 stsp done:
8495 e600f124 2021-03-21 stsp free(author0);
8496 e600f124 2021-03-21 stsp free(logmsg0);
8497 643b85bc 2021-07-16 stsp return err;
8498 643b85bc 2021-07-16 stsp }
8499 643b85bc 2021-07-16 stsp
8500 643b85bc 2021-07-16 stsp static const struct got_error *
8501 643b85bc 2021-07-16 stsp delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
8502 643b85bc 2021-07-16 stsp struct got_repository *repo)
8503 643b85bc 2021-07-16 stsp {
8504 643b85bc 2021-07-16 stsp const struct got_error *err;
8505 643b85bc 2021-07-16 stsp char *id_str;
8506 643b85bc 2021-07-16 stsp
8507 643b85bc 2021-07-16 stsp err = got_object_id_str(&id_str, id);
8508 643b85bc 2021-07-16 stsp if (err)
8509 643b85bc 2021-07-16 stsp return err;
8510 643b85bc 2021-07-16 stsp
8511 643b85bc 2021-07-16 stsp err = got_ref_delete(ref, repo);
8512 643b85bc 2021-07-16 stsp if (err)
8513 643b85bc 2021-07-16 stsp goto done;
8514 643b85bc 2021-07-16 stsp
8515 643b85bc 2021-07-16 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
8516 643b85bc 2021-07-16 stsp done:
8517 643b85bc 2021-07-16 stsp free(id_str);
8518 e600f124 2021-03-21 stsp return err;
8519 e600f124 2021-03-21 stsp }
8520 e600f124 2021-03-21 stsp
8521 e600f124 2021-03-21 stsp static const struct got_error *
8522 e600f124 2021-03-21 stsp print_backup_ref(const char *branch_name, const char *new_id_str,
8523 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
8524 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap,
8525 e600f124 2021-03-21 stsp struct got_repository *repo)
8526 e600f124 2021-03-21 stsp {
8527 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
8528 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
8529 e600f124 2021-03-21 stsp char *refs_str = NULL;
8530 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id = NULL;
8531 e600f124 2021-03-21 stsp struct got_commit_object *new_commit = NULL;
8532 e600f124 2021-03-21 stsp char *new_commit_brief_str = NULL;
8533 e600f124 2021-03-21 stsp struct got_object_id *yca_id = NULL;
8534 e600f124 2021-03-21 stsp struct got_commit_object *yca_commit = NULL;
8535 e600f124 2021-03-21 stsp char *yca_id_str = NULL, *yca_brief_str = NULL;
8536 e600f124 2021-03-21 stsp char *custom_refs_str;
8537 e600f124 2021-03-21 stsp
8538 e600f124 2021-03-21 stsp if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
8539 e600f124 2021-03-21 stsp return got_error_from_errno("asprintf");
8540 e600f124 2021-03-21 stsp
8541 e600f124 2021-03-21 stsp err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
8542 e600f124 2021-03-21 stsp 0, 0, refs_idmap, custom_refs_str);
8543 e600f124 2021-03-21 stsp if (err)
8544 e600f124 2021-03-21 stsp goto done;
8545 e600f124 2021-03-21 stsp
8546 e600f124 2021-03-21 stsp err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
8547 e600f124 2021-03-21 stsp if (err)
8548 e600f124 2021-03-21 stsp goto done;
8549 e600f124 2021-03-21 stsp
8550 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
8551 e600f124 2021-03-21 stsp if (refs) {
8552 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, new_commit_id, repo);
8553 e600f124 2021-03-21 stsp if (err)
8554 e600f124 2021-03-21 stsp goto done;
8555 e600f124 2021-03-21 stsp }
8556 e600f124 2021-03-21 stsp
8557 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
8558 e600f124 2021-03-21 stsp if (err)
8559 e600f124 2021-03-21 stsp goto done;
8560 e600f124 2021-03-21 stsp
8561 e600f124 2021-03-21 stsp err = get_commit_brief_str(&new_commit_brief_str, new_commit);
8562 e600f124 2021-03-21 stsp if (err)
8563 e600f124 2021-03-21 stsp goto done;
8564 e600f124 2021-03-21 stsp
8565 e600f124 2021-03-21 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8566 e600f124 2021-03-21 stsp old_commit_id, new_commit_id, repo, check_cancelled, NULL);
8567 e600f124 2021-03-21 stsp if (err)
8568 e600f124 2021-03-21 stsp goto done;
8569 e600f124 2021-03-21 stsp
8570 e600f124 2021-03-21 stsp printf("has become commit %s%s%s%s\n %s\n", new_id_str,
8571 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
8572 e600f124 2021-03-21 stsp refs_str ? ")" : "", new_commit_brief_str);
8573 e600f124 2021-03-21 stsp if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
8574 e600f124 2021-03-21 stsp got_object_id_cmp(yca_id, old_commit_id) != 0) {
8575 e600f124 2021-03-21 stsp free(refs_str);
8576 e600f124 2021-03-21 stsp refs_str = NULL;
8577 e600f124 2021-03-21 stsp
8578 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&yca_commit, repo, yca_id);
8579 e600f124 2021-03-21 stsp if (err)
8580 e600f124 2021-03-21 stsp goto done;
8581 e600f124 2021-03-21 stsp
8582 e600f124 2021-03-21 stsp err = get_commit_brief_str(&yca_brief_str, yca_commit);
8583 e600f124 2021-03-21 stsp if (err)
8584 e600f124 2021-03-21 stsp goto done;
8585 e600f124 2021-03-21 stsp
8586 e600f124 2021-03-21 stsp err = got_object_id_str(&yca_id_str, yca_id);
8587 e600f124 2021-03-21 stsp if (err)
8588 e600f124 2021-03-21 stsp goto done;
8589 e600f124 2021-03-21 stsp
8590 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
8591 e600f124 2021-03-21 stsp if (refs) {
8592 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, yca_id, repo);
8593 e600f124 2021-03-21 stsp if (err)
8594 e600f124 2021-03-21 stsp goto done;
8595 e600f124 2021-03-21 stsp }
8596 e600f124 2021-03-21 stsp printf("history forked at %s%s%s%s\n %s\n",
8597 e600f124 2021-03-21 stsp yca_id_str,
8598 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
8599 e600f124 2021-03-21 stsp refs_str ? ")" : "", yca_brief_str);
8600 e600f124 2021-03-21 stsp }
8601 e600f124 2021-03-21 stsp done:
8602 e600f124 2021-03-21 stsp free(custom_refs_str);
8603 e600f124 2021-03-21 stsp free(new_commit_id);
8604 e600f124 2021-03-21 stsp free(refs_str);
8605 e600f124 2021-03-21 stsp free(yca_id);
8606 e600f124 2021-03-21 stsp free(yca_id_str);
8607 e600f124 2021-03-21 stsp free(yca_brief_str);
8608 e600f124 2021-03-21 stsp if (new_commit)
8609 e600f124 2021-03-21 stsp got_object_commit_close(new_commit);
8610 e600f124 2021-03-21 stsp if (yca_commit)
8611 e600f124 2021-03-21 stsp got_object_commit_close(yca_commit);
8612 e600f124 2021-03-21 stsp
8613 e600f124 2021-03-21 stsp return NULL;
8614 af61c510 2019-07-19 stsp }
8615 af61c510 2019-07-19 stsp
8616 af61c510 2019-07-19 stsp static const struct got_error *
8617 643b85bc 2021-07-16 stsp process_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
8618 643b85bc 2021-07-16 stsp int delete, struct got_repository *repo)
8619 e600f124 2021-03-21 stsp {
8620 e600f124 2021-03-21 stsp const struct got_error *err;
8621 e600f124 2021-03-21 stsp struct got_reflist_head refs, backup_refs;
8622 e600f124 2021-03-21 stsp struct got_reflist_entry *re;
8623 e600f124 2021-03-21 stsp const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
8624 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
8625 e600f124 2021-03-21 stsp char *branch_name = NULL;
8626 e600f124 2021-03-21 stsp struct got_commit_object *old_commit = NULL;
8627 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
8628 9e822917 2021-03-23 stsp int wanted_branch_found = 0;
8629 e600f124 2021-03-21 stsp
8630 e600f124 2021-03-21 stsp TAILQ_INIT(&refs);
8631 e600f124 2021-03-21 stsp TAILQ_INIT(&backup_refs);
8632 e600f124 2021-03-21 stsp
8633 e600f124 2021-03-21 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8634 e600f124 2021-03-21 stsp if (err)
8635 e600f124 2021-03-21 stsp return err;
8636 e600f124 2021-03-21 stsp
8637 e600f124 2021-03-21 stsp err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
8638 e600f124 2021-03-21 stsp if (err)
8639 e600f124 2021-03-21 stsp goto done;
8640 e600f124 2021-03-21 stsp
8641 e600f124 2021-03-21 stsp if (wanted_branch_name) {
8642 e600f124 2021-03-21 stsp if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
8643 e600f124 2021-03-21 stsp wanted_branch_name += 11;
8644 e600f124 2021-03-21 stsp }
8645 e600f124 2021-03-21 stsp
8646 e600f124 2021-03-21 stsp err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
8647 e600f124 2021-03-21 stsp got_ref_cmp_by_commit_timestamp_descending, repo);
8648 e600f124 2021-03-21 stsp if (err)
8649 e600f124 2021-03-21 stsp goto done;
8650 e600f124 2021-03-21 stsp
8651 e600f124 2021-03-21 stsp TAILQ_FOREACH(re, &backup_refs, entry) {
8652 e600f124 2021-03-21 stsp const char *refname = got_ref_get_name(re->ref);
8653 e600f124 2021-03-21 stsp char *slash;
8654 e600f124 2021-03-21 stsp
8655 643b85bc 2021-07-16 stsp err = check_cancelled(NULL);
8656 643b85bc 2021-07-16 stsp if (err)
8657 643b85bc 2021-07-16 stsp break;
8658 643b85bc 2021-07-16 stsp
8659 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, re->ref);
8660 e600f124 2021-03-21 stsp if (err)
8661 e600f124 2021-03-21 stsp break;
8662 e600f124 2021-03-21 stsp
8663 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&old_commit, repo,
8664 e600f124 2021-03-21 stsp old_commit_id);
8665 e600f124 2021-03-21 stsp if (err)
8666 e600f124 2021-03-21 stsp break;
8667 e600f124 2021-03-21 stsp
8668 e600f124 2021-03-21 stsp if (strncmp(backup_ref_prefix, refname,
8669 e600f124 2021-03-21 stsp backup_ref_prefix_len) == 0)
8670 e600f124 2021-03-21 stsp refname += backup_ref_prefix_len;
8671 e600f124 2021-03-21 stsp
8672 e600f124 2021-03-21 stsp while (refname[0] == '/')
8673 e600f124 2021-03-21 stsp refname++;
8674 e600f124 2021-03-21 stsp
8675 e600f124 2021-03-21 stsp branch_name = strdup(refname);
8676 e600f124 2021-03-21 stsp if (branch_name == NULL) {
8677 e600f124 2021-03-21 stsp err = got_error_from_errno("strdup");
8678 e600f124 2021-03-21 stsp break;
8679 e600f124 2021-03-21 stsp }
8680 e600f124 2021-03-21 stsp slash = strrchr(branch_name, '/');
8681 e600f124 2021-03-21 stsp if (slash) {
8682 e600f124 2021-03-21 stsp *slash = '\0';
8683 e600f124 2021-03-21 stsp refname += strlen(branch_name) + 1;
8684 e600f124 2021-03-21 stsp }
8685 e600f124 2021-03-21 stsp
8686 e600f124 2021-03-21 stsp if (wanted_branch_name == NULL ||
8687 e600f124 2021-03-21 stsp strcmp(wanted_branch_name, branch_name) == 0) {
8688 9e822917 2021-03-23 stsp wanted_branch_found = 1;
8689 643b85bc 2021-07-16 stsp if (delete) {
8690 643b85bc 2021-07-16 stsp err = delete_backup_ref(re->ref,
8691 643b85bc 2021-07-16 stsp old_commit_id, repo);
8692 643b85bc 2021-07-16 stsp } else {
8693 643b85bc 2021-07-16 stsp err = print_backup_ref(branch_name, refname,
8694 abc59930 2021-09-05 naddy old_commit_id, old_commit, refs_idmap,
8695 abc59930 2021-09-05 naddy repo);
8696 643b85bc 2021-07-16 stsp }
8697 e600f124 2021-03-21 stsp if (err)
8698 e600f124 2021-03-21 stsp break;
8699 e600f124 2021-03-21 stsp }
8700 e600f124 2021-03-21 stsp
8701 e600f124 2021-03-21 stsp free(old_commit_id);
8702 e600f124 2021-03-21 stsp old_commit_id = NULL;
8703 e600f124 2021-03-21 stsp free(branch_name);
8704 e600f124 2021-03-21 stsp branch_name = NULL;
8705 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
8706 e600f124 2021-03-21 stsp old_commit = NULL;
8707 e600f124 2021-03-21 stsp }
8708 9e822917 2021-03-23 stsp
8709 9e822917 2021-03-23 stsp if (wanted_branch_name && !wanted_branch_found) {
8710 9e822917 2021-03-23 stsp err = got_error_fmt(GOT_ERR_NOT_REF,
8711 9e822917 2021-03-23 stsp "%s/%s/", backup_ref_prefix, wanted_branch_name);
8712 9e822917 2021-03-23 stsp }
8713 e600f124 2021-03-21 stsp done:
8714 e600f124 2021-03-21 stsp if (refs_idmap)
8715 e600f124 2021-03-21 stsp got_reflist_object_id_map_free(refs_idmap);
8716 e600f124 2021-03-21 stsp got_ref_list_free(&refs);
8717 e600f124 2021-03-21 stsp got_ref_list_free(&backup_refs);
8718 e600f124 2021-03-21 stsp free(old_commit_id);
8719 e600f124 2021-03-21 stsp free(branch_name);
8720 e600f124 2021-03-21 stsp if (old_commit)
8721 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
8722 e600f124 2021-03-21 stsp return err;
8723 e600f124 2021-03-21 stsp }
8724 e600f124 2021-03-21 stsp
8725 e600f124 2021-03-21 stsp static const struct got_error *
8726 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
8727 818c7501 2019-07-11 stsp {
8728 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
8729 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
8730 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
8731 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8732 818c7501 2019-07-11 stsp char *cwd = NULL;
8733 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
8734 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
8735 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
8736 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
8737 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
8738 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
8739 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
8740 e600f124 2021-03-21 stsp int histedit_in_progress = 0, create_backup = 1, list_backups = 0;
8741 643b85bc 2021-07-16 stsp int delete_backups = 0;
8742 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8743 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
8744 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
8745 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
8746 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
8747 818c7501 2019-07-11 stsp
8748 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
8749 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
8750 818c7501 2019-07-11 stsp
8751 643b85bc 2021-07-16 stsp while ((ch = getopt(argc, argv, "aclX")) != -1) {
8752 818c7501 2019-07-11 stsp switch (ch) {
8753 818c7501 2019-07-11 stsp case 'a':
8754 818c7501 2019-07-11 stsp abort_rebase = 1;
8755 818c7501 2019-07-11 stsp break;
8756 818c7501 2019-07-11 stsp case 'c':
8757 818c7501 2019-07-11 stsp continue_rebase = 1;
8758 818c7501 2019-07-11 stsp break;
8759 e600f124 2021-03-21 stsp case 'l':
8760 e600f124 2021-03-21 stsp list_backups = 1;
8761 e600f124 2021-03-21 stsp break;
8762 643b85bc 2021-07-16 stsp case 'X':
8763 643b85bc 2021-07-16 stsp delete_backups = 1;
8764 643b85bc 2021-07-16 stsp break;
8765 818c7501 2019-07-11 stsp default:
8766 818c7501 2019-07-11 stsp usage_rebase();
8767 818c7501 2019-07-11 stsp /* NOTREACHED */
8768 818c7501 2019-07-11 stsp }
8769 818c7501 2019-07-11 stsp }
8770 818c7501 2019-07-11 stsp
8771 818c7501 2019-07-11 stsp argc -= optind;
8772 818c7501 2019-07-11 stsp argv += optind;
8773 818c7501 2019-07-11 stsp
8774 43012d58 2019-07-14 stsp #ifndef PROFILE
8775 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8776 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8777 43012d58 2019-07-14 stsp err(1, "pledge");
8778 43012d58 2019-07-14 stsp #endif
8779 e600f124 2021-03-21 stsp if (list_backups) {
8780 e600f124 2021-03-21 stsp if (abort_rebase)
8781 e600f124 2021-03-21 stsp option_conflict('l', 'a');
8782 e600f124 2021-03-21 stsp if (continue_rebase)
8783 e600f124 2021-03-21 stsp option_conflict('l', 'c');
8784 643b85bc 2021-07-16 stsp if (delete_backups)
8785 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
8786 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
8787 643b85bc 2021-07-16 stsp usage_rebase();
8788 643b85bc 2021-07-16 stsp } else if (delete_backups) {
8789 643b85bc 2021-07-16 stsp if (abort_rebase)
8790 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
8791 643b85bc 2021-07-16 stsp if (continue_rebase)
8792 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
8793 643b85bc 2021-07-16 stsp if (list_backups)
8794 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
8795 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
8796 818c7501 2019-07-11 stsp usage_rebase();
8797 e600f124 2021-03-21 stsp } else {
8798 e600f124 2021-03-21 stsp if (abort_rebase && continue_rebase)
8799 e600f124 2021-03-21 stsp usage_rebase();
8800 e600f124 2021-03-21 stsp else if (abort_rebase || continue_rebase) {
8801 e600f124 2021-03-21 stsp if (argc != 0)
8802 e600f124 2021-03-21 stsp usage_rebase();
8803 e600f124 2021-03-21 stsp } else if (argc != 1)
8804 e600f124 2021-03-21 stsp usage_rebase();
8805 e600f124 2021-03-21 stsp }
8806 818c7501 2019-07-11 stsp
8807 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
8808 818c7501 2019-07-11 stsp if (cwd == NULL) {
8809 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
8810 818c7501 2019-07-11 stsp goto done;
8811 818c7501 2019-07-11 stsp }
8812 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
8813 fa51e947 2020-03-27 stsp if (error) {
8814 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
8815 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
8816 e600f124 2021-03-21 stsp goto done;
8817 e600f124 2021-03-21 stsp } else {
8818 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8819 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
8820 e600f124 2021-03-21 stsp "rebase", cwd);
8821 e600f124 2021-03-21 stsp goto done;
8822 e600f124 2021-03-21 stsp }
8823 fa51e947 2020-03-27 stsp }
8824 818c7501 2019-07-11 stsp
8825 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
8826 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
8827 818c7501 2019-07-11 stsp if (error != NULL)
8828 818c7501 2019-07-11 stsp goto done;
8829 818c7501 2019-07-11 stsp
8830 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8831 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
8832 7ef62c4e 2020-02-24 stsp if (error)
8833 7ef62c4e 2020-02-24 stsp goto done;
8834 7ef62c4e 2020-02-24 stsp
8835 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
8836 643b85bc 2021-07-16 stsp error = process_backup_refs(
8837 643b85bc 2021-07-16 stsp GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
8838 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
8839 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
8840 e600f124 2021-03-21 stsp }
8841 e600f124 2021-03-21 stsp
8842 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
8843 7ef62c4e 2020-02-24 stsp worktree);
8844 818c7501 2019-07-11 stsp if (error)
8845 7ef62c4e 2020-02-24 stsp goto done;
8846 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
8847 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8848 818c7501 2019-07-11 stsp goto done;
8849 7ef62c4e 2020-02-24 stsp }
8850 818c7501 2019-07-11 stsp
8851 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8852 818c7501 2019-07-11 stsp if (error)
8853 818c7501 2019-07-11 stsp goto done;
8854 818c7501 2019-07-11 stsp
8855 f6794adc 2019-07-23 stsp if (abort_rebase) {
8856 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8857 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
8858 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
8859 f6794adc 2019-07-23 stsp goto done;
8860 f6794adc 2019-07-23 stsp }
8861 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
8862 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
8863 3e3a69f1 2019-07-25 stsp worktree, repo);
8864 818c7501 2019-07-11 stsp if (error)
8865 818c7501 2019-07-11 stsp goto done;
8866 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
8867 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
8868 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8869 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
8870 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
8871 818c7501 2019-07-11 stsp if (error)
8872 818c7501 2019-07-11 stsp goto done;
8873 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
8874 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8875 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
8876 818c7501 2019-07-11 stsp }
8877 818c7501 2019-07-11 stsp
8878 818c7501 2019-07-11 stsp if (continue_rebase) {
8879 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
8880 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
8881 f6794adc 2019-07-23 stsp goto done;
8882 f6794adc 2019-07-23 stsp }
8883 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
8884 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
8885 3e3a69f1 2019-07-25 stsp worktree, repo);
8886 818c7501 2019-07-11 stsp if (error)
8887 818c7501 2019-07-11 stsp goto done;
8888 818c7501 2019-07-11 stsp
8889 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
8890 01757395 2019-07-12 stsp resume_commit_id, repo);
8891 818c7501 2019-07-11 stsp if (error)
8892 818c7501 2019-07-11 stsp goto done;
8893 818c7501 2019-07-11 stsp
8894 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
8895 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
8896 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
8897 818c7501 2019-07-11 stsp goto done;
8898 818c7501 2019-07-11 stsp }
8899 818c7501 2019-07-11 stsp } else {
8900 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
8901 818c7501 2019-07-11 stsp if (error != NULL)
8902 818c7501 2019-07-11 stsp goto done;
8903 ff0d2220 2019-07-11 stsp }
8904 818c7501 2019-07-11 stsp
8905 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
8906 ff0d2220 2019-07-11 stsp if (error)
8907 ff0d2220 2019-07-11 stsp goto done;
8908 ff0d2220 2019-07-11 stsp
8909 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
8910 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
8911 a51a74b3 2019-07-27 stsp
8912 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
8913 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8914 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
8915 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
8916 818c7501 2019-07-11 stsp if (error)
8917 818c7501 2019-07-11 stsp goto done;
8918 818c7501 2019-07-11 stsp if (yca_id == NULL) {
8919 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
8920 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
8921 818c7501 2019-07-11 stsp "with work tree's branch");
8922 818c7501 2019-07-11 stsp goto done;
8923 818c7501 2019-07-11 stsp }
8924 818c7501 2019-07-11 stsp
8925 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
8926 a51a74b3 2019-07-27 stsp if (error) {
8927 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
8928 a51a74b3 2019-07-27 stsp goto done;
8929 a51a74b3 2019-07-27 stsp error = NULL;
8930 a51a74b3 2019-07-27 stsp } else {
8931 df3ed485 2021-01-31 stsp static char msg[128];
8932 df3ed485 2021-01-31 stsp snprintf(msg, sizeof(msg),
8933 df3ed485 2021-01-31 stsp "%s is already based on %s",
8934 df3ed485 2021-01-31 stsp got_ref_get_name(branch),
8935 df3ed485 2021-01-31 stsp got_worktree_get_head_ref_name(worktree));
8936 df3ed485 2021-01-31 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
8937 a51a74b3 2019-07-27 stsp goto done;
8938 a51a74b3 2019-07-27 stsp }
8939 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
8940 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
8941 818c7501 2019-07-11 stsp if (error)
8942 818c7501 2019-07-11 stsp goto done;
8943 818c7501 2019-07-11 stsp }
8944 818c7501 2019-07-11 stsp
8945 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
8946 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8947 818c7501 2019-07-11 stsp if (error)
8948 818c7501 2019-07-11 stsp goto done;
8949 818c7501 2019-07-11 stsp
8950 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8951 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
8952 fc66b545 2019-08-12 stsp if (pid == NULL) {
8953 fc66b545 2019-08-12 stsp if (!continue_rebase) {
8954 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8955 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8956 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
8957 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
8958 fc66b545 2019-08-12 stsp if (error)
8959 fc66b545 2019-08-12 stsp goto done;
8960 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
8961 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
8962 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8963 9627c110 2020-04-18 stsp
8964 fc66b545 2019-08-12 stsp }
8965 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
8966 fc66b545 2019-08-12 stsp goto done;
8967 fc66b545 2019-08-12 stsp }
8968 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
8969 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
8970 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
8971 818c7501 2019-07-11 stsp got_object_commit_close(commit);
8972 818c7501 2019-07-11 stsp commit = NULL;
8973 818c7501 2019-07-11 stsp if (error)
8974 818c7501 2019-07-11 stsp goto done;
8975 64c6d990 2019-07-11 stsp
8976 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
8977 38b0338b 2019-11-29 stsp if (continue_rebase) {
8978 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
8979 e600f124 2021-03-21 stsp branch, new_base_branch, tmp_branch, repo,
8980 e600f124 2021-03-21 stsp create_backup);
8981 38b0338b 2019-11-29 stsp goto done;
8982 38b0338b 2019-11-29 stsp } else {
8983 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
8984 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
8985 38b0338b 2019-11-29 stsp char *id_str;
8986 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
8987 38b0338b 2019-11-29 stsp new_base_branch);
8988 38b0338b 2019-11-29 stsp if (error)
8989 38b0338b 2019-11-29 stsp goto done;
8990 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
8991 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
8992 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
8993 38b0338b 2019-11-29 stsp free(id_str);
8994 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
8995 38b0338b 2019-11-29 stsp new_head_commit_id);
8996 38b0338b 2019-11-29 stsp if (error)
8997 38b0338b 2019-11-29 stsp goto done;
8998 e600f124 2021-03-21 stsp /* No backup needed since objects did not change. */
8999 e600f124 2021-03-21 stsp create_backup = 0;
9000 38b0338b 2019-11-29 stsp }
9001 818c7501 2019-07-11 stsp }
9002 818c7501 2019-07-11 stsp
9003 818c7501 2019-07-11 stsp pid = NULL;
9004 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &commits, entry) {
9005 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9006 9627c110 2020-04-18 stsp
9007 818c7501 2019-07-11 stsp commit_id = qid->id;
9008 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
9009 818c7501 2019-07-11 stsp pid = qid;
9010 818c7501 2019-07-11 stsp
9011 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9012 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
9013 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
9014 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
9015 818c7501 2019-07-11 stsp if (error)
9016 818c7501 2019-07-11 stsp goto done;
9017 9627c110 2020-04-18 stsp
9018 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9019 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
9020 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
9021 818c7501 2019-07-11 stsp
9022 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
9023 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
9024 a0ea4fc0 2020-02-28 stsp if (error)
9025 a0ea4fc0 2020-02-28 stsp goto done;
9026 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9027 818c7501 2019-07-11 stsp break;
9028 01757395 2019-07-12 stsp }
9029 818c7501 2019-07-11 stsp
9030 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
9031 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
9032 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9033 818c7501 2019-07-11 stsp if (error)
9034 818c7501 2019-07-11 stsp goto done;
9035 818c7501 2019-07-11 stsp }
9036 818c7501 2019-07-11 stsp
9037 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
9038 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
9039 818c7501 2019-07-11 stsp if (error)
9040 818c7501 2019-07-11 stsp goto done;
9041 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
9042 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
9043 818c7501 2019-07-11 stsp } else
9044 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
9045 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, repo, create_backup);
9046 818c7501 2019-07-11 stsp done:
9047 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
9048 818c7501 2019-07-11 stsp free(branch_head_commit_id);
9049 818c7501 2019-07-11 stsp free(resume_commit_id);
9050 818c7501 2019-07-11 stsp free(yca_id);
9051 818c7501 2019-07-11 stsp if (commit)
9052 818c7501 2019-07-11 stsp got_object_commit_close(commit);
9053 818c7501 2019-07-11 stsp if (branch)
9054 818c7501 2019-07-11 stsp got_ref_close(branch);
9055 818c7501 2019-07-11 stsp if (new_base_branch)
9056 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
9057 818c7501 2019-07-11 stsp if (tmp_branch)
9058 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
9059 5ef14e63 2019-06-02 stsp if (worktree)
9060 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
9061 1d0f4054 2021-06-17 stsp if (repo) {
9062 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
9063 1d0f4054 2021-06-17 stsp if (error == NULL)
9064 1d0f4054 2021-06-17 stsp error = close_err;
9065 1d0f4054 2021-06-17 stsp }
9066 5ef14e63 2019-06-02 stsp return error;
9067 0ebf8283 2019-07-24 stsp }
9068 0ebf8283 2019-07-24 stsp
9069 0ebf8283 2019-07-24 stsp __dead static void
9070 0ebf8283 2019-07-24 stsp usage_histedit(void)
9071 0ebf8283 2019-07-24 stsp {
9072 e600f124 2021-03-21 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] "
9073 643b85bc 2021-07-16 stsp "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
9074 643b85bc 2021-07-16 stsp getprogname());
9075 0ebf8283 2019-07-24 stsp exit(1);
9076 0ebf8283 2019-07-24 stsp }
9077 0ebf8283 2019-07-24 stsp
9078 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
9079 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
9080 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
9081 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
9082 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
9083 0ebf8283 2019-07-24 stsp
9084 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
9085 0ebf8283 2019-07-24 stsp unsigned char code;
9086 0ebf8283 2019-07-24 stsp const char *name;
9087 0ebf8283 2019-07-24 stsp const char *desc;
9088 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
9089 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
9090 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
9091 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
9092 82997472 2020-01-29 stsp "be used" },
9093 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
9094 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
9095 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
9096 0ebf8283 2019-07-24 stsp };
9097 0ebf8283 2019-07-24 stsp
9098 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
9099 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
9100 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
9101 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
9102 0ebf8283 2019-07-24 stsp char *logmsg;
9103 0ebf8283 2019-07-24 stsp };
9104 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
9105 0ebf8283 2019-07-24 stsp
9106 0ebf8283 2019-07-24 stsp static const struct got_error *
9107 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
9108 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
9109 0ebf8283 2019-07-24 stsp {
9110 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9111 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
9112 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9113 8138f3e1 2019-08-11 stsp int n;
9114 0ebf8283 2019-07-24 stsp
9115 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
9116 0ebf8283 2019-07-24 stsp if (err)
9117 0ebf8283 2019-07-24 stsp goto done;
9118 0ebf8283 2019-07-24 stsp
9119 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
9120 0ebf8283 2019-07-24 stsp if (err)
9121 0ebf8283 2019-07-24 stsp goto done;
9122 0ebf8283 2019-07-24 stsp
9123 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
9124 0ebf8283 2019-07-24 stsp if (err)
9125 0ebf8283 2019-07-24 stsp goto done;
9126 0ebf8283 2019-07-24 stsp
9127 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
9128 0ebf8283 2019-07-24 stsp if (n < 0)
9129 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9130 0ebf8283 2019-07-24 stsp done:
9131 0ebf8283 2019-07-24 stsp if (commit)
9132 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9133 0ebf8283 2019-07-24 stsp free(id_str);
9134 0ebf8283 2019-07-24 stsp free(logmsg);
9135 0ebf8283 2019-07-24 stsp return err;
9136 0ebf8283 2019-07-24 stsp }
9137 0ebf8283 2019-07-24 stsp
9138 0ebf8283 2019-07-24 stsp static const struct got_error *
9139 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
9140 466785b9 2020-12-10 jrick FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
9141 0ebf8283 2019-07-24 stsp {
9142 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9143 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
9144 466785b9 2020-12-10 jrick const char *histedit_cmd = NULL;
9145 0ebf8283 2019-07-24 stsp
9146 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
9147 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
9148 0ebf8283 2019-07-24 stsp
9149 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
9150 466785b9 2020-12-10 jrick histedit_cmd = got_histedit_cmds[0].name;
9151 dbdddfee 2021-06-23 naddy if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
9152 466785b9 2020-12-10 jrick histedit_cmd = "fold";
9153 466785b9 2020-12-10 jrick err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
9154 0ebf8283 2019-07-24 stsp if (err)
9155 0ebf8283 2019-07-24 stsp break;
9156 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
9157 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
9158 083957f4 2020-02-24 stsp if (n < 0) {
9159 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
9160 083957f4 2020-02-24 stsp break;
9161 083957f4 2020-02-24 stsp }
9162 083957f4 2020-02-24 stsp }
9163 0ebf8283 2019-07-24 stsp }
9164 0ebf8283 2019-07-24 stsp
9165 0ebf8283 2019-07-24 stsp return err;
9166 0ebf8283 2019-07-24 stsp }
9167 0ebf8283 2019-07-24 stsp
9168 0ebf8283 2019-07-24 stsp static const struct got_error *
9169 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
9170 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
9171 0ebf8283 2019-07-24 stsp {
9172 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9173 6059809a 2020-12-17 stsp size_t i;
9174 6059809a 2020-12-17 stsp int n;
9175 514f2ffe 2020-01-29 stsp char *id_str;
9176 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
9177 514f2ffe 2020-01-29 stsp
9178 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(commits);
9179 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
9180 514f2ffe 2020-01-29 stsp if (err)
9181 514f2ffe 2020-01-29 stsp return err;
9182 514f2ffe 2020-01-29 stsp
9183 514f2ffe 2020-01-29 stsp n = fprintf(f,
9184 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
9185 514f2ffe 2020-01-29 stsp "# commit %s\n"
9186 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
9187 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
9188 514f2ffe 2020-01-29 stsp if (n < 0) {
9189 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
9190 514f2ffe 2020-01-29 stsp goto done;
9191 514f2ffe 2020-01-29 stsp }
9192 0ebf8283 2019-07-24 stsp
9193 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
9194 514f2ffe 2020-01-29 stsp if (n < 0) {
9195 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
9196 514f2ffe 2020-01-29 stsp goto done;
9197 514f2ffe 2020-01-29 stsp }
9198 0ebf8283 2019-07-24 stsp
9199 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
9200 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
9201 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
9202 0ebf8283 2019-07-24 stsp cmd->desc);
9203 0ebf8283 2019-07-24 stsp if (n < 0) {
9204 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9205 0ebf8283 2019-07-24 stsp break;
9206 0ebf8283 2019-07-24 stsp }
9207 0ebf8283 2019-07-24 stsp }
9208 514f2ffe 2020-01-29 stsp done:
9209 514f2ffe 2020-01-29 stsp free(id_str);
9210 0ebf8283 2019-07-24 stsp return err;
9211 0ebf8283 2019-07-24 stsp }
9212 0ebf8283 2019-07-24 stsp
9213 0ebf8283 2019-07-24 stsp static const struct got_error *
9214 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
9215 0ebf8283 2019-07-24 stsp {
9216 0ebf8283 2019-07-24 stsp static char msg[42];
9217 0ebf8283 2019-07-24 stsp int ret;
9218 0ebf8283 2019-07-24 stsp
9219 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
9220 0ebf8283 2019-07-24 stsp lineno);
9221 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
9222 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
9223 0ebf8283 2019-07-24 stsp
9224 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
9225 0ebf8283 2019-07-24 stsp }
9226 0ebf8283 2019-07-24 stsp
9227 0ebf8283 2019-07-24 stsp static const struct got_error *
9228 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
9229 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
9230 0ebf8283 2019-07-24 stsp {
9231 0ebf8283 2019-07-24 stsp const struct got_error *err;
9232 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
9233 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
9234 0ebf8283 2019-07-24 stsp
9235 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9236 0ebf8283 2019-07-24 stsp if (err)
9237 0ebf8283 2019-07-24 stsp return err;
9238 0ebf8283 2019-07-24 stsp
9239 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
9240 0ebf8283 2019-07-24 stsp if (err)
9241 0ebf8283 2019-07-24 stsp goto done;
9242 0ebf8283 2019-07-24 stsp
9243 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
9244 5943eee2 2019-08-13 stsp if (err)
9245 5943eee2 2019-08-13 stsp goto done;
9246 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
9247 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
9248 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
9249 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
9250 0ebf8283 2019-07-24 stsp }
9251 0ebf8283 2019-07-24 stsp done:
9252 0ebf8283 2019-07-24 stsp if (folded_commit)
9253 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
9254 0ebf8283 2019-07-24 stsp free(id_str);
9255 5943eee2 2019-08-13 stsp free(folded_logmsg);
9256 0ebf8283 2019-07-24 stsp return err;
9257 0ebf8283 2019-07-24 stsp }
9258 0ebf8283 2019-07-24 stsp
9259 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
9260 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
9261 0ebf8283 2019-07-24 stsp {
9262 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
9263 0ebf8283 2019-07-24 stsp
9264 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
9265 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
9266 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
9267 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
9268 3f9de99f 2019-07-24 stsp folded = prev;
9269 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
9270 0ebf8283 2019-07-24 stsp }
9271 0ebf8283 2019-07-24 stsp
9272 0ebf8283 2019-07-24 stsp return folded;
9273 0ebf8283 2019-07-24 stsp }
9274 0ebf8283 2019-07-24 stsp
9275 0ebf8283 2019-07-24 stsp static const struct got_error *
9276 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
9277 0ebf8283 2019-07-24 stsp struct got_repository *repo)
9278 0ebf8283 2019-07-24 stsp {
9279 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
9280 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
9281 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9282 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9283 1601cb9f 2020-09-11 naddy int logmsg_len;
9284 0ebf8283 2019-07-24 stsp int fd;
9285 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
9286 0ebf8283 2019-07-24 stsp
9287 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9288 0ebf8283 2019-07-24 stsp if (err)
9289 0ebf8283 2019-07-24 stsp return err;
9290 0ebf8283 2019-07-24 stsp
9291 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
9292 0ebf8283 2019-07-24 stsp if (folded) {
9293 0ebf8283 2019-07-24 stsp while (folded != hle) {
9294 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
9295 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
9296 3f9de99f 2019-07-24 stsp continue;
9297 3f9de99f 2019-07-24 stsp }
9298 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
9299 0ebf8283 2019-07-24 stsp logmsg, repo);
9300 0ebf8283 2019-07-24 stsp if (err)
9301 0ebf8283 2019-07-24 stsp goto done;
9302 0ebf8283 2019-07-24 stsp free(logmsg);
9303 0ebf8283 2019-07-24 stsp logmsg = new_msg;
9304 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
9305 0ebf8283 2019-07-24 stsp }
9306 0ebf8283 2019-07-24 stsp }
9307 0ebf8283 2019-07-24 stsp
9308 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9309 0ebf8283 2019-07-24 stsp if (err)
9310 0ebf8283 2019-07-24 stsp goto done;
9311 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
9312 5943eee2 2019-08-13 stsp if (err)
9313 5943eee2 2019-08-13 stsp goto done;
9314 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
9315 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
9316 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
9317 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
9318 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
9319 0ebf8283 2019-07-24 stsp goto done;
9320 0ebf8283 2019-07-24 stsp }
9321 0ebf8283 2019-07-24 stsp free(logmsg);
9322 0ebf8283 2019-07-24 stsp logmsg = new_msg;
9323 0ebf8283 2019-07-24 stsp
9324 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9325 0ebf8283 2019-07-24 stsp if (err)
9326 0ebf8283 2019-07-24 stsp goto done;
9327 0ebf8283 2019-07-24 stsp
9328 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
9329 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
9330 0ebf8283 2019-07-24 stsp if (err)
9331 0ebf8283 2019-07-24 stsp goto done;
9332 0ebf8283 2019-07-24 stsp
9333 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
9334 0ebf8283 2019-07-24 stsp close(fd);
9335 0ebf8283 2019-07-24 stsp
9336 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
9337 0ebf8283 2019-07-24 stsp if (err)
9338 0ebf8283 2019-07-24 stsp goto done;
9339 0ebf8283 2019-07-24 stsp
9340 bfa12d5e 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
9341 0d5bb276 2020-12-15 stsp logmsg_len, 0);
9342 0ebf8283 2019-07-24 stsp if (err) {
9343 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
9344 0ebf8283 2019-07-24 stsp goto done;
9345 71392a05 2020-12-13 stsp err = NULL;
9346 71392a05 2020-12-13 stsp hle->logmsg = strdup(new_msg);
9347 71392a05 2020-12-13 stsp if (hle->logmsg == NULL)
9348 71392a05 2020-12-13 stsp err = got_error_from_errno("strdup");
9349 0ebf8283 2019-07-24 stsp }
9350 0ebf8283 2019-07-24 stsp done:
9351 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
9352 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
9353 0ebf8283 2019-07-24 stsp free(logmsg_path);
9354 0ebf8283 2019-07-24 stsp free(logmsg);
9355 5943eee2 2019-08-13 stsp free(orig_logmsg);
9356 0ebf8283 2019-07-24 stsp free(editor);
9357 0ebf8283 2019-07-24 stsp if (commit)
9358 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9359 0ebf8283 2019-07-24 stsp return err;
9360 5ef14e63 2019-06-02 stsp }
9361 0ebf8283 2019-07-24 stsp
9362 0ebf8283 2019-07-24 stsp static const struct got_error *
9363 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
9364 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
9365 0ebf8283 2019-07-24 stsp {
9366 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9367 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
9368 6059809a 2020-12-17 stsp size_t i, size;
9369 0ebf8283 2019-07-24 stsp ssize_t len;
9370 6059809a 2020-12-17 stsp int lineno = 0;
9371 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
9372 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
9373 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
9374 0ebf8283 2019-07-24 stsp
9375 0ebf8283 2019-07-24 stsp for (;;) {
9376 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
9377 0ebf8283 2019-07-24 stsp if (len == -1) {
9378 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
9379 0ebf8283 2019-07-24 stsp if (feof(f))
9380 0ebf8283 2019-07-24 stsp break;
9381 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
9382 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
9383 0ebf8283 2019-07-24 stsp break;
9384 0ebf8283 2019-07-24 stsp }
9385 0ebf8283 2019-07-24 stsp lineno++;
9386 0ebf8283 2019-07-24 stsp p = line;
9387 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
9388 0ebf8283 2019-07-24 stsp p++;
9389 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
9390 0ebf8283 2019-07-24 stsp free(line);
9391 0ebf8283 2019-07-24 stsp line = NULL;
9392 0ebf8283 2019-07-24 stsp continue;
9393 0ebf8283 2019-07-24 stsp }
9394 0ebf8283 2019-07-24 stsp cmd = NULL;
9395 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
9396 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
9397 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
9398 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
9399 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
9400 0ebf8283 2019-07-24 stsp break;
9401 0ebf8283 2019-07-24 stsp }
9402 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
9403 0ebf8283 2019-07-24 stsp p++;
9404 0ebf8283 2019-07-24 stsp break;
9405 0ebf8283 2019-07-24 stsp }
9406 0ebf8283 2019-07-24 stsp }
9407 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
9408 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
9409 0ebf8283 2019-07-24 stsp break;
9410 0ebf8283 2019-07-24 stsp }
9411 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
9412 0ebf8283 2019-07-24 stsp p++;
9413 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
9414 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
9415 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
9416 0ebf8283 2019-07-24 stsp break;
9417 0ebf8283 2019-07-24 stsp }
9418 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
9419 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
9420 0ebf8283 2019-07-24 stsp if (err)
9421 0ebf8283 2019-07-24 stsp break;
9422 0ebf8283 2019-07-24 stsp } else {
9423 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
9424 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
9425 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
9426 0ebf8283 2019-07-24 stsp break;
9427 0ebf8283 2019-07-24 stsp }
9428 0ebf8283 2019-07-24 stsp }
9429 0ebf8283 2019-07-24 stsp free(line);
9430 0ebf8283 2019-07-24 stsp line = NULL;
9431 0ebf8283 2019-07-24 stsp continue;
9432 0ebf8283 2019-07-24 stsp } else {
9433 0ebf8283 2019-07-24 stsp end = p;
9434 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
9435 0ebf8283 2019-07-24 stsp end++;
9436 0ebf8283 2019-07-24 stsp *end = '\0';
9437 0ebf8283 2019-07-24 stsp
9438 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
9439 0ebf8283 2019-07-24 stsp if (err) {
9440 0ebf8283 2019-07-24 stsp /* override error code */
9441 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
9442 0ebf8283 2019-07-24 stsp break;
9443 0ebf8283 2019-07-24 stsp }
9444 0ebf8283 2019-07-24 stsp }
9445 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
9446 0ebf8283 2019-07-24 stsp if (hle == NULL) {
9447 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
9448 0ebf8283 2019-07-24 stsp break;
9449 0ebf8283 2019-07-24 stsp }
9450 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
9451 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
9452 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
9453 0ebf8283 2019-07-24 stsp commit_id = NULL;
9454 0ebf8283 2019-07-24 stsp free(line);
9455 0ebf8283 2019-07-24 stsp line = NULL;
9456 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
9457 0ebf8283 2019-07-24 stsp }
9458 0ebf8283 2019-07-24 stsp
9459 0ebf8283 2019-07-24 stsp free(line);
9460 0ebf8283 2019-07-24 stsp free(commit_id);
9461 0ebf8283 2019-07-24 stsp return err;
9462 0ebf8283 2019-07-24 stsp }
9463 0ebf8283 2019-07-24 stsp
9464 0ebf8283 2019-07-24 stsp static const struct got_error *
9465 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
9466 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
9467 bfce7f83 2019-07-27 stsp {
9468 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
9469 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
9470 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
9471 5b87815e 2020-03-05 stsp static char msg[92];
9472 bfce7f83 2019-07-27 stsp char *id_str;
9473 bfce7f83 2019-07-27 stsp
9474 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
9475 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
9476 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
9477 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
9478 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
9479 5b87815e 2020-03-05 stsp
9480 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9481 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
9482 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
9483 5b87815e 2020-03-05 stsp if (hle == hle2)
9484 5b87815e 2020-03-05 stsp continue;
9485 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
9486 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
9487 5b87815e 2020-03-05 stsp continue;
9488 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
9489 5b87815e 2020-03-05 stsp if (err)
9490 5b87815e 2020-03-05 stsp return err;
9491 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
9492 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
9493 5b87815e 2020-03-05 stsp free(id_str);
9494 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9495 5b87815e 2020-03-05 stsp }
9496 5b87815e 2020-03-05 stsp }
9497 bfce7f83 2019-07-27 stsp
9498 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
9499 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9500 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
9501 bfce7f83 2019-07-27 stsp break;
9502 bfce7f83 2019-07-27 stsp }
9503 bfce7f83 2019-07-27 stsp if (hle == NULL) {
9504 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
9505 bfce7f83 2019-07-27 stsp if (err)
9506 bfce7f83 2019-07-27 stsp return err;
9507 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
9508 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
9509 bfce7f83 2019-07-27 stsp free(id_str);
9510 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9511 bfce7f83 2019-07-27 stsp }
9512 bfce7f83 2019-07-27 stsp }
9513 bfce7f83 2019-07-27 stsp
9514 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
9515 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
9516 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
9517 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
9518 bfce7f83 2019-07-27 stsp
9519 bfce7f83 2019-07-27 stsp return NULL;
9520 bfce7f83 2019-07-27 stsp }
9521 bfce7f83 2019-07-27 stsp
9522 bfce7f83 2019-07-27 stsp static const struct got_error *
9523 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
9524 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
9525 bfce7f83 2019-07-27 stsp struct got_repository *repo)
9526 0ebf8283 2019-07-24 stsp {
9527 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9528 0ebf8283 2019-07-24 stsp char *editor;
9529 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9530 0ebf8283 2019-07-24 stsp
9531 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
9532 0ebf8283 2019-07-24 stsp if (err)
9533 0ebf8283 2019-07-24 stsp return err;
9534 0ebf8283 2019-07-24 stsp
9535 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
9536 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
9537 0ebf8283 2019-07-24 stsp goto done;
9538 0ebf8283 2019-07-24 stsp }
9539 0ebf8283 2019-07-24 stsp
9540 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
9541 0ebf8283 2019-07-24 stsp if (f == NULL) {
9542 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
9543 0ebf8283 2019-07-24 stsp goto done;
9544 0ebf8283 2019-07-24 stsp }
9545 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9546 bfce7f83 2019-07-27 stsp if (err)
9547 bfce7f83 2019-07-27 stsp goto done;
9548 bfce7f83 2019-07-27 stsp
9549 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
9550 0ebf8283 2019-07-24 stsp done:
9551 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9552 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9553 0ebf8283 2019-07-24 stsp free(editor);
9554 0ebf8283 2019-07-24 stsp return err;
9555 0ebf8283 2019-07-24 stsp }
9556 0ebf8283 2019-07-24 stsp
9557 0ebf8283 2019-07-24 stsp static const struct got_error *
9558 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
9559 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
9560 514f2ffe 2020-01-29 stsp struct got_repository *);
9561 0ebf8283 2019-07-24 stsp
9562 0ebf8283 2019-07-24 stsp static const struct got_error *
9563 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
9564 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
9565 466785b9 2020-12-10 jrick int edit_logmsg_only, int fold_only, struct got_repository *repo)
9566 0ebf8283 2019-07-24 stsp {
9567 0ebf8283 2019-07-24 stsp const struct got_error *err;
9568 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9569 0ebf8283 2019-07-24 stsp char *path = NULL;
9570 0ebf8283 2019-07-24 stsp
9571 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
9572 0ebf8283 2019-07-24 stsp if (err)
9573 0ebf8283 2019-07-24 stsp return err;
9574 0ebf8283 2019-07-24 stsp
9575 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
9576 0ebf8283 2019-07-24 stsp if (err)
9577 0ebf8283 2019-07-24 stsp goto done;
9578 0ebf8283 2019-07-24 stsp
9579 466785b9 2020-12-10 jrick err = histedit_write_commit_list(commits, f, edit_logmsg_only,
9580 466785b9 2020-12-10 jrick fold_only, repo);
9581 0ebf8283 2019-07-24 stsp if (err)
9582 0ebf8283 2019-07-24 stsp goto done;
9583 0ebf8283 2019-07-24 stsp
9584 466785b9 2020-12-10 jrick if (edit_logmsg_only || fold_only) {
9585 083957f4 2020-02-24 stsp rewind(f);
9586 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9587 083957f4 2020-02-24 stsp } else {
9588 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF) {
9589 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
9590 0ebf8283 2019-07-24 stsp goto done;
9591 083957f4 2020-02-24 stsp }
9592 083957f4 2020-02-24 stsp f = NULL;
9593 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
9594 083957f4 2020-02-24 stsp if (err) {
9595 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9596 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9597 083957f4 2020-02-24 stsp goto done;
9598 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
9599 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
9600 083957f4 2020-02-24 stsp }
9601 0ebf8283 2019-07-24 stsp }
9602 0ebf8283 2019-07-24 stsp done:
9603 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9604 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9605 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
9606 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
9607 0ebf8283 2019-07-24 stsp free(path);
9608 0ebf8283 2019-07-24 stsp return err;
9609 0ebf8283 2019-07-24 stsp }
9610 0ebf8283 2019-07-24 stsp
9611 0ebf8283 2019-07-24 stsp static const struct got_error *
9612 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
9613 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
9614 0ebf8283 2019-07-24 stsp {
9615 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9616 0ebf8283 2019-07-24 stsp char *path = NULL;
9617 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9618 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
9619 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9620 0ebf8283 2019-07-24 stsp
9621 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
9622 0ebf8283 2019-07-24 stsp if (err)
9623 0ebf8283 2019-07-24 stsp return err;
9624 0ebf8283 2019-07-24 stsp
9625 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
9626 0ebf8283 2019-07-24 stsp if (f == NULL) {
9627 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
9628 0ebf8283 2019-07-24 stsp goto done;
9629 0ebf8283 2019-07-24 stsp }
9630 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9631 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
9632 0ebf8283 2019-07-24 stsp repo);
9633 0ebf8283 2019-07-24 stsp if (err)
9634 0ebf8283 2019-07-24 stsp break;
9635 0ebf8283 2019-07-24 stsp
9636 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
9637 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
9638 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
9639 0ebf8283 2019-07-24 stsp if (n < 0) {
9640 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9641 0ebf8283 2019-07-24 stsp break;
9642 0ebf8283 2019-07-24 stsp }
9643 0ebf8283 2019-07-24 stsp }
9644 0ebf8283 2019-07-24 stsp }
9645 0ebf8283 2019-07-24 stsp done:
9646 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9647 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9648 0ebf8283 2019-07-24 stsp free(path);
9649 0ebf8283 2019-07-24 stsp if (commit)
9650 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9651 0ebf8283 2019-07-24 stsp return err;
9652 0ebf8283 2019-07-24 stsp }
9653 0ebf8283 2019-07-24 stsp
9654 bfce7f83 2019-07-27 stsp void
9655 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
9656 bfce7f83 2019-07-27 stsp {
9657 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
9658 bfce7f83 2019-07-27 stsp
9659 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
9660 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
9661 bfce7f83 2019-07-27 stsp free(hle);
9662 bfce7f83 2019-07-27 stsp }
9663 bfce7f83 2019-07-27 stsp }
9664 bfce7f83 2019-07-27 stsp
9665 0ebf8283 2019-07-24 stsp static const struct got_error *
9666 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
9667 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
9668 0ebf8283 2019-07-24 stsp {
9669 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9670 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9671 0ebf8283 2019-07-24 stsp
9672 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
9673 0ebf8283 2019-07-24 stsp if (f == NULL) {
9674 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
9675 0ebf8283 2019-07-24 stsp goto done;
9676 0ebf8283 2019-07-24 stsp }
9677 0ebf8283 2019-07-24 stsp
9678 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9679 0ebf8283 2019-07-24 stsp done:
9680 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9681 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9682 0ebf8283 2019-07-24 stsp return err;
9683 0ebf8283 2019-07-24 stsp }
9684 0ebf8283 2019-07-24 stsp
9685 0ebf8283 2019-07-24 stsp static const struct got_error *
9686 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
9687 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
9688 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
9689 0ebf8283 2019-07-24 stsp {
9690 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
9691 0ebf8283 2019-07-24 stsp int resp = ' ';
9692 0ebf8283 2019-07-24 stsp
9693 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
9694 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
9695 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
9696 0ebf8283 2019-07-24 stsp resp = getchar();
9697 a61a4414 2019-08-07 stsp if (resp == '\n')
9698 a61a4414 2019-08-07 stsp resp = getchar();
9699 426ebf2e 2019-07-25 stsp if (resp == 'c') {
9700 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
9701 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
9702 bfce7f83 2019-07-27 stsp repo);
9703 426ebf2e 2019-07-25 stsp if (err) {
9704 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9705 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9706 426ebf2e 2019-07-25 stsp break;
9707 bfce7f83 2019-07-27 stsp prev_err = err;
9708 426ebf2e 2019-07-25 stsp resp = ' ';
9709 426ebf2e 2019-07-25 stsp continue;
9710 426ebf2e 2019-07-25 stsp }
9711 bfce7f83 2019-07-27 stsp break;
9712 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
9713 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
9714 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
9715 466785b9 2020-12-10 jrick commits, branch_name, 0, 0, repo);
9716 426ebf2e 2019-07-25 stsp if (err) {
9717 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9718 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9719 426ebf2e 2019-07-25 stsp break;
9720 bfce7f83 2019-07-27 stsp prev_err = err;
9721 426ebf2e 2019-07-25 stsp resp = ' ';
9722 426ebf2e 2019-07-25 stsp continue;
9723 426ebf2e 2019-07-25 stsp }
9724 bfce7f83 2019-07-27 stsp break;
9725 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
9726 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
9727 0ebf8283 2019-07-24 stsp break;
9728 426ebf2e 2019-07-25 stsp } else
9729 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
9730 0ebf8283 2019-07-24 stsp }
9731 0ebf8283 2019-07-24 stsp
9732 0ebf8283 2019-07-24 stsp return err;
9733 0ebf8283 2019-07-24 stsp }
9734 0ebf8283 2019-07-24 stsp
9735 0ebf8283 2019-07-24 stsp static const struct got_error *
9736 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
9737 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
9738 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
9739 0ebf8283 2019-07-24 stsp {
9740 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
9741 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
9742 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
9743 3e3a69f1 2019-07-25 stsp branch, repo);
9744 0ebf8283 2019-07-24 stsp }
9745 0ebf8283 2019-07-24 stsp
9746 0ebf8283 2019-07-24 stsp static const struct got_error *
9747 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
9748 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
9749 0ebf8283 2019-07-24 stsp {
9750 0ebf8283 2019-07-24 stsp const struct got_error *err;
9751 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9752 0ebf8283 2019-07-24 stsp
9753 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
9754 0ebf8283 2019-07-24 stsp if (err)
9755 0ebf8283 2019-07-24 stsp goto done;
9756 0ebf8283 2019-07-24 stsp
9757 0ebf8283 2019-07-24 stsp if (new_id) {
9758 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
9759 0ebf8283 2019-07-24 stsp if (err)
9760 0ebf8283 2019-07-24 stsp goto done;
9761 0ebf8283 2019-07-24 stsp }
9762 0ebf8283 2019-07-24 stsp
9763 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
9764 0ebf8283 2019-07-24 stsp if (new_id_str)
9765 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
9766 0ebf8283 2019-07-24 stsp
9767 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
9768 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
9769 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
9770 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
9771 0ebf8283 2019-07-24 stsp goto done;
9772 0ebf8283 2019-07-24 stsp }
9773 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
9774 0ebf8283 2019-07-24 stsp } else {
9775 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
9776 0ebf8283 2019-07-24 stsp if (err)
9777 0ebf8283 2019-07-24 stsp goto done;
9778 0ebf8283 2019-07-24 stsp }
9779 0ebf8283 2019-07-24 stsp
9780 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
9781 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
9782 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
9783 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
9784 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
9785 0ebf8283 2019-07-24 stsp break;
9786 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
9787 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
9788 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
9789 0ebf8283 2019-07-24 stsp logmsg);
9790 0ebf8283 2019-07-24 stsp break;
9791 0ebf8283 2019-07-24 stsp default:
9792 0ebf8283 2019-07-24 stsp break;
9793 0ebf8283 2019-07-24 stsp }
9794 0ebf8283 2019-07-24 stsp done:
9795 0ebf8283 2019-07-24 stsp free(old_id_str);
9796 0ebf8283 2019-07-24 stsp free(new_id_str);
9797 0ebf8283 2019-07-24 stsp return err;
9798 0ebf8283 2019-07-24 stsp }
9799 0ebf8283 2019-07-24 stsp
9800 0ebf8283 2019-07-24 stsp static const struct got_error *
9801 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
9802 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
9803 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
9804 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
9805 0ebf8283 2019-07-24 stsp {
9806 0ebf8283 2019-07-24 stsp const struct got_error *err;
9807 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
9808 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
9809 0ebf8283 2019-07-24 stsp
9810 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
9811 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
9812 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
9813 0ebf8283 2019-07-24 stsp if (err)
9814 0ebf8283 2019-07-24 stsp return err;
9815 0ebf8283 2019-07-24 stsp }
9816 0ebf8283 2019-07-24 stsp
9817 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9818 0ebf8283 2019-07-24 stsp if (err)
9819 0ebf8283 2019-07-24 stsp return err;
9820 0ebf8283 2019-07-24 stsp
9821 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
9822 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
9823 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
9824 0ebf8283 2019-07-24 stsp if (err) {
9825 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
9826 0ebf8283 2019-07-24 stsp goto done;
9827 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
9828 0ebf8283 2019-07-24 stsp } else {
9829 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
9830 0ebf8283 2019-07-24 stsp free(new_commit_id);
9831 0ebf8283 2019-07-24 stsp }
9832 0ebf8283 2019-07-24 stsp done:
9833 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9834 0ebf8283 2019-07-24 stsp return err;
9835 0ebf8283 2019-07-24 stsp }
9836 0ebf8283 2019-07-24 stsp
9837 0ebf8283 2019-07-24 stsp static const struct got_error *
9838 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
9839 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
9840 0ebf8283 2019-07-24 stsp {
9841 0ebf8283 2019-07-24 stsp const struct got_error *error;
9842 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
9843 0ebf8283 2019-07-24 stsp
9844 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
9845 0ebf8283 2019-07-24 stsp repo);
9846 0ebf8283 2019-07-24 stsp if (error)
9847 0ebf8283 2019-07-24 stsp return error;
9848 0ebf8283 2019-07-24 stsp
9849 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
9850 0ebf8283 2019-07-24 stsp if (error)
9851 0ebf8283 2019-07-24 stsp return error;
9852 0ebf8283 2019-07-24 stsp
9853 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
9854 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9855 0ebf8283 2019-07-24 stsp return error;
9856 ab20a43a 2020-01-29 stsp }
9857 ab20a43a 2020-01-29 stsp
9858 ab20a43a 2020-01-29 stsp static const struct got_error *
9859 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
9860 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
9861 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9862 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9863 ab20a43a 2020-01-29 stsp {
9864 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
9865 ab20a43a 2020-01-29 stsp
9866 ab20a43a 2020-01-29 stsp switch (status) {
9867 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
9868 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
9869 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
9870 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
9871 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
9872 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
9873 ab20a43a 2020-01-29 stsp default:
9874 ab20a43a 2020-01-29 stsp break;
9875 ab20a43a 2020-01-29 stsp }
9876 ab20a43a 2020-01-29 stsp
9877 ab20a43a 2020-01-29 stsp switch (staged_status) {
9878 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
9879 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
9880 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
9881 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
9882 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
9883 ab20a43a 2020-01-29 stsp default:
9884 ab20a43a 2020-01-29 stsp break;
9885 ab20a43a 2020-01-29 stsp }
9886 ab20a43a 2020-01-29 stsp
9887 ab20a43a 2020-01-29 stsp return NULL;
9888 0ebf8283 2019-07-24 stsp }
9889 0ebf8283 2019-07-24 stsp
9890 0ebf8283 2019-07-24 stsp static const struct got_error *
9891 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
9892 0ebf8283 2019-07-24 stsp {
9893 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
9894 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
9895 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
9896 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
9897 0ebf8283 2019-07-24 stsp char *cwd = NULL;
9898 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
9899 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
9900 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
9901 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
9902 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
9903 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9904 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
9905 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9906 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
9907 466785b9 2020-12-10 jrick int edit_logmsg_only = 0, fold_only = 0;
9908 643b85bc 2021-07-16 stsp int list_backups = 0, delete_backups = 0;
9909 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
9910 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
9911 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
9912 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
9913 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
9914 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
9915 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
9916 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
9917 0ebf8283 2019-07-24 stsp
9918 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
9919 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
9920 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
9921 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9922 0ebf8283 2019-07-24 stsp
9923 643b85bc 2021-07-16 stsp while ((ch = getopt(argc, argv, "acfF:mlX")) != -1) {
9924 0ebf8283 2019-07-24 stsp switch (ch) {
9925 0ebf8283 2019-07-24 stsp case 'a':
9926 0ebf8283 2019-07-24 stsp abort_edit = 1;
9927 0ebf8283 2019-07-24 stsp break;
9928 0ebf8283 2019-07-24 stsp case 'c':
9929 0ebf8283 2019-07-24 stsp continue_edit = 1;
9930 0ebf8283 2019-07-24 stsp break;
9931 466785b9 2020-12-10 jrick case 'f':
9932 466785b9 2020-12-10 jrick fold_only = 1;
9933 466785b9 2020-12-10 jrick break;
9934 0ebf8283 2019-07-24 stsp case 'F':
9935 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
9936 0ebf8283 2019-07-24 stsp break;
9937 083957f4 2020-02-24 stsp case 'm':
9938 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
9939 083957f4 2020-02-24 stsp break;
9940 e600f124 2021-03-21 stsp case 'l':
9941 e600f124 2021-03-21 stsp list_backups = 1;
9942 e600f124 2021-03-21 stsp break;
9943 643b85bc 2021-07-16 stsp case 'X':
9944 643b85bc 2021-07-16 stsp delete_backups = 1;
9945 643b85bc 2021-07-16 stsp break;
9946 0ebf8283 2019-07-24 stsp default:
9947 0ebf8283 2019-07-24 stsp usage_histedit();
9948 0ebf8283 2019-07-24 stsp /* NOTREACHED */
9949 0ebf8283 2019-07-24 stsp }
9950 0ebf8283 2019-07-24 stsp }
9951 0ebf8283 2019-07-24 stsp
9952 0ebf8283 2019-07-24 stsp argc -= optind;
9953 0ebf8283 2019-07-24 stsp argv += optind;
9954 0ebf8283 2019-07-24 stsp
9955 0ebf8283 2019-07-24 stsp #ifndef PROFILE
9956 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9957 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
9958 0ebf8283 2019-07-24 stsp err(1, "pledge");
9959 0ebf8283 2019-07-24 stsp #endif
9960 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
9961 ff69268e 2020-12-13 stsp option_conflict('a', 'c');
9962 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
9963 ff69268e 2020-12-13 stsp option_conflict('F', 'm');
9964 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
9965 ff69268e 2020-12-13 stsp option_conflict('a', 'm');
9966 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
9967 ff69268e 2020-12-13 stsp option_conflict('c', 'm');
9968 466785b9 2020-12-10 jrick if (abort_edit && fold_only)
9969 ff69268e 2020-12-13 stsp option_conflict('a', 'f');
9970 ff69268e 2020-12-13 stsp if (continue_edit && fold_only)
9971 ff69268e 2020-12-13 stsp option_conflict('c', 'f');
9972 466785b9 2020-12-10 jrick if (fold_only && edit_logmsg_only)
9973 ff69268e 2020-12-13 stsp option_conflict('f', 'm');
9974 b3805337 2020-12-13 stsp if (edit_script_path && fold_only)
9975 b3805337 2020-12-13 stsp option_conflict('F', 'f');
9976 e600f124 2021-03-21 stsp if (list_backups) {
9977 e600f124 2021-03-21 stsp if (abort_edit)
9978 e600f124 2021-03-21 stsp option_conflict('l', 'a');
9979 e600f124 2021-03-21 stsp if (continue_edit)
9980 e600f124 2021-03-21 stsp option_conflict('l', 'c');
9981 e600f124 2021-03-21 stsp if (edit_script_path)
9982 e600f124 2021-03-21 stsp option_conflict('l', 'F');
9983 e600f124 2021-03-21 stsp if (edit_logmsg_only)
9984 e600f124 2021-03-21 stsp option_conflict('l', 'm');
9985 e600f124 2021-03-21 stsp if (fold_only)
9986 e600f124 2021-03-21 stsp option_conflict('l', 'f');
9987 643b85bc 2021-07-16 stsp if (delete_backups)
9988 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
9989 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
9990 e600f124 2021-03-21 stsp usage_histedit();
9991 643b85bc 2021-07-16 stsp } else if (delete_backups) {
9992 643b85bc 2021-07-16 stsp if (abort_edit)
9993 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
9994 643b85bc 2021-07-16 stsp if (continue_edit)
9995 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
9996 643b85bc 2021-07-16 stsp if (edit_script_path)
9997 643b85bc 2021-07-16 stsp option_conflict('X', 'F');
9998 643b85bc 2021-07-16 stsp if (edit_logmsg_only)
9999 643b85bc 2021-07-16 stsp option_conflict('X', 'm');
10000 643b85bc 2021-07-16 stsp if (fold_only)
10001 643b85bc 2021-07-16 stsp option_conflict('X', 'f');
10002 643b85bc 2021-07-16 stsp if (list_backups)
10003 643b85bc 2021-07-16 stsp option_conflict('X', 'l');
10004 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
10005 643b85bc 2021-07-16 stsp usage_histedit();
10006 e600f124 2021-03-21 stsp } else if (argc != 0)
10007 0ebf8283 2019-07-24 stsp usage_histedit();
10008 0ebf8283 2019-07-24 stsp
10009 0ebf8283 2019-07-24 stsp /*
10010 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
10011 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
10012 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
10013 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
10014 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
10015 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
10016 0ebf8283 2019-07-24 stsp */
10017 0ebf8283 2019-07-24 stsp
10018 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
10019 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
10020 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
10021 0ebf8283 2019-07-24 stsp goto done;
10022 0ebf8283 2019-07-24 stsp }
10023 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
10024 fa51e947 2020-03-27 stsp if (error) {
10025 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
10026 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
10027 e600f124 2021-03-21 stsp goto done;
10028 e600f124 2021-03-21 stsp } else {
10029 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10030 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
10031 e600f124 2021-03-21 stsp "histedit", cwd);
10032 e600f124 2021-03-21 stsp goto done;
10033 e600f124 2021-03-21 stsp }
10034 e600f124 2021-03-21 stsp }
10035 e600f124 2021-03-21 stsp
10036 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
10037 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
10038 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd,
10039 e600f124 2021-03-21 stsp NULL);
10040 e600f124 2021-03-21 stsp if (error != NULL)
10041 e600f124 2021-03-21 stsp goto done;
10042 e600f124 2021-03-21 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10043 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
10044 e600f124 2021-03-21 stsp if (error)
10045 e600f124 2021-03-21 stsp goto done;
10046 643b85bc 2021-07-16 stsp error = process_backup_refs(
10047 e600f124 2021-03-21 stsp GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
10048 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
10049 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
10050 fa51e947 2020-03-27 stsp }
10051 0ebf8283 2019-07-24 stsp
10052 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10053 c9956ddf 2019-09-08 stsp NULL);
10054 0ebf8283 2019-07-24 stsp if (error != NULL)
10055 0ebf8283 2019-07-24 stsp goto done;
10056 0ebf8283 2019-07-24 stsp
10057 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10058 0ebf8283 2019-07-24 stsp if (error)
10059 0ebf8283 2019-07-24 stsp goto done;
10060 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
10061 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
10062 0ebf8283 2019-07-24 stsp goto done;
10063 0ebf8283 2019-07-24 stsp }
10064 0ebf8283 2019-07-24 stsp
10065 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
10066 0ebf8283 2019-07-24 stsp if (error)
10067 0ebf8283 2019-07-24 stsp goto done;
10068 0ebf8283 2019-07-24 stsp
10069 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
10070 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10071 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
10072 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
10073 083957f4 2020-02-24 stsp "before the -m option can be used");
10074 083957f4 2020-02-24 stsp goto done;
10075 083957f4 2020-02-24 stsp }
10076 466785b9 2020-12-10 jrick if (edit_in_progress && fold_only) {
10077 466785b9 2020-12-10 jrick error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10078 466785b9 2020-12-10 jrick "histedit operation is in progress in this "
10079 466785b9 2020-12-10 jrick "work tree and must be continued or aborted "
10080 466785b9 2020-12-10 jrick "before the -f option can be used");
10081 466785b9 2020-12-10 jrick goto done;
10082 466785b9 2020-12-10 jrick }
10083 083957f4 2020-02-24 stsp
10084 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
10085 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
10086 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
10087 3e3a69f1 2019-07-25 stsp worktree, repo);
10088 0ebf8283 2019-07-24 stsp if (error)
10089 0ebf8283 2019-07-24 stsp goto done;
10090 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
10091 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10092 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
10093 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
10094 0ebf8283 2019-07-24 stsp if (error)
10095 0ebf8283 2019-07-24 stsp goto done;
10096 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
10097 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10098 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10099 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
10100 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
10101 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
10102 0ebf8283 2019-07-24 stsp goto done;
10103 0ebf8283 2019-07-24 stsp }
10104 0ebf8283 2019-07-24 stsp
10105 0ebf8283 2019-07-24 stsp if (continue_edit) {
10106 0ebf8283 2019-07-24 stsp char *path;
10107 0ebf8283 2019-07-24 stsp
10108 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
10109 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
10110 0ebf8283 2019-07-24 stsp goto done;
10111 0ebf8283 2019-07-24 stsp }
10112 0ebf8283 2019-07-24 stsp
10113 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
10114 0ebf8283 2019-07-24 stsp if (error)
10115 0ebf8283 2019-07-24 stsp goto done;
10116 0ebf8283 2019-07-24 stsp
10117 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
10118 0ebf8283 2019-07-24 stsp free(path);
10119 0ebf8283 2019-07-24 stsp if (error)
10120 0ebf8283 2019-07-24 stsp goto done;
10121 0ebf8283 2019-07-24 stsp
10122 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
10123 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
10124 3e3a69f1 2019-07-25 stsp worktree, repo);
10125 0ebf8283 2019-07-24 stsp if (error)
10126 0ebf8283 2019-07-24 stsp goto done;
10127 0ebf8283 2019-07-24 stsp
10128 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
10129 0ebf8283 2019-07-24 stsp if (error)
10130 0ebf8283 2019-07-24 stsp goto done;
10131 0ebf8283 2019-07-24 stsp
10132 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10133 0ebf8283 2019-07-24 stsp head_commit_id);
10134 0ebf8283 2019-07-24 stsp if (error)
10135 0ebf8283 2019-07-24 stsp goto done;
10136 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10137 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10138 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
10139 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10140 3aac7cf7 2019-07-25 stsp goto done;
10141 3aac7cf7 2019-07-25 stsp }
10142 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
10143 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
10144 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
10145 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10146 0ebf8283 2019-07-24 stsp commit = NULL;
10147 0ebf8283 2019-07-24 stsp if (error)
10148 0ebf8283 2019-07-24 stsp goto done;
10149 0ebf8283 2019-07-24 stsp } else {
10150 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
10151 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
10152 0ebf8283 2019-07-24 stsp goto done;
10153 0ebf8283 2019-07-24 stsp }
10154 0ebf8283 2019-07-24 stsp
10155 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
10156 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
10157 0ebf8283 2019-07-24 stsp if (error != NULL)
10158 0ebf8283 2019-07-24 stsp goto done;
10159 0ebf8283 2019-07-24 stsp
10160 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10161 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10162 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
10163 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
10164 c7d20a3f 2019-07-30 stsp goto done;
10165 c7d20a3f 2019-07-30 stsp }
10166 c7d20a3f 2019-07-30 stsp
10167 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
10168 bfce7f83 2019-07-27 stsp got_ref_close(branch);
10169 bfce7f83 2019-07-27 stsp branch = NULL;
10170 0ebf8283 2019-07-24 stsp if (error)
10171 0ebf8283 2019-07-24 stsp goto done;
10172 0ebf8283 2019-07-24 stsp
10173 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10174 0ebf8283 2019-07-24 stsp head_commit_id);
10175 0ebf8283 2019-07-24 stsp if (error)
10176 0ebf8283 2019-07-24 stsp goto done;
10177 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10178 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10179 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
10180 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10181 3aac7cf7 2019-07-25 stsp goto done;
10182 3aac7cf7 2019-07-25 stsp }
10183 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
10184 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
10185 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
10186 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
10187 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10188 0ebf8283 2019-07-24 stsp commit = NULL;
10189 0ebf8283 2019-07-24 stsp if (error)
10190 0ebf8283 2019-07-24 stsp goto done;
10191 0ebf8283 2019-07-24 stsp
10192 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
10193 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10194 c5996fff 2020-01-29 stsp goto done;
10195 c5996fff 2020-01-29 stsp }
10196 c5996fff 2020-01-29 stsp
10197 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
10198 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
10199 bfce7f83 2019-07-27 stsp if (error)
10200 bfce7f83 2019-07-27 stsp goto done;
10201 bfce7f83 2019-07-27 stsp
10202 0ebf8283 2019-07-24 stsp if (edit_script_path) {
10203 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
10204 0ebf8283 2019-07-24 stsp edit_script_path, repo);
10205 6ba2bea2 2019-07-27 stsp if (error) {
10206 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10207 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10208 9627c110 2020-04-18 stsp update_progress, &upa);
10209 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10210 0ebf8283 2019-07-24 stsp goto done;
10211 6ba2bea2 2019-07-27 stsp }
10212 0ebf8283 2019-07-24 stsp } else {
10213 514f2ffe 2020-01-29 stsp const char *branch_name;
10214 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
10215 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
10216 514f2ffe 2020-01-29 stsp branch_name += 11;
10217 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
10218 466785b9 2020-12-10 jrick branch_name, edit_logmsg_only, fold_only, repo);
10219 6ba2bea2 2019-07-27 stsp if (error) {
10220 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10221 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10222 9627c110 2020-04-18 stsp update_progress, &upa);
10223 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10224 0ebf8283 2019-07-24 stsp goto done;
10225 6ba2bea2 2019-07-27 stsp }
10226 0ebf8283 2019-07-24 stsp
10227 0ebf8283 2019-07-24 stsp }
10228 0ebf8283 2019-07-24 stsp
10229 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
10230 0ebf8283 2019-07-24 stsp repo);
10231 6ba2bea2 2019-07-27 stsp if (error) {
10232 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10233 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10234 9627c110 2020-04-18 stsp update_progress, &upa);
10235 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10236 0ebf8283 2019-07-24 stsp goto done;
10237 6ba2bea2 2019-07-27 stsp }
10238 0ebf8283 2019-07-24 stsp
10239 0ebf8283 2019-07-24 stsp }
10240 0ebf8283 2019-07-24 stsp
10241 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
10242 4ec14e60 2019-08-28 hiltjo if (error)
10243 0ebf8283 2019-07-24 stsp goto done;
10244 0ebf8283 2019-07-24 stsp
10245 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
10246 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
10247 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
10248 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
10249 0ebf8283 2019-07-24 stsp continue;
10250 0ebf8283 2019-07-24 stsp
10251 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
10252 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
10253 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
10254 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
10255 62d463ca 2020-10-20 naddy repo);
10256 ab20a43a 2020-01-29 stsp if (error)
10257 ab20a43a 2020-01-29 stsp goto done;
10258 0ebf8283 2019-07-24 stsp } else {
10259 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
10260 ab20a43a 2020-01-29 stsp int have_changes = 0;
10261 ab20a43a 2020-01-29 stsp
10262 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
10263 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
10264 ab20a43a 2020-01-29 stsp if (error)
10265 ab20a43a 2020-01-29 stsp goto done;
10266 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
10267 f6343036 2021-06-22 stsp repo, 0, check_local_changes, &have_changes,
10268 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
10269 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
10270 ab20a43a 2020-01-29 stsp if (error) {
10271 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
10272 ab20a43a 2020-01-29 stsp goto done;
10273 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
10274 ab20a43a 2020-01-29 stsp goto done;
10275 ab20a43a 2020-01-29 stsp }
10276 ab20a43a 2020-01-29 stsp if (have_changes) {
10277 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
10278 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
10279 ab20a43a 2020-01-29 stsp if (error)
10280 ab20a43a 2020-01-29 stsp goto done;
10281 ab20a43a 2020-01-29 stsp } else {
10282 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
10283 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
10284 ab20a43a 2020-01-29 stsp if (error)
10285 ab20a43a 2020-01-29 stsp goto done;
10286 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
10287 ab20a43a 2020-01-29 stsp hle, NULL);
10288 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
10289 ab20a43a 2020-01-29 stsp commit = NULL;
10290 ab20a43a 2020-01-29 stsp if (error)
10291 ab20a43a 2020-01-29 stsp goto done;
10292 ab20a43a 2020-01-29 stsp }
10293 0ebf8283 2019-07-24 stsp }
10294 0ebf8283 2019-07-24 stsp continue;
10295 0ebf8283 2019-07-24 stsp }
10296 0ebf8283 2019-07-24 stsp
10297 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
10298 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
10299 0ebf8283 2019-07-24 stsp if (error)
10300 0ebf8283 2019-07-24 stsp goto done;
10301 0ebf8283 2019-07-24 stsp continue;
10302 0ebf8283 2019-07-24 stsp }
10303 0ebf8283 2019-07-24 stsp
10304 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10305 0ebf8283 2019-07-24 stsp hle->commit_id);
10306 0ebf8283 2019-07-24 stsp if (error)
10307 0ebf8283 2019-07-24 stsp goto done;
10308 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10309 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10310 0ebf8283 2019-07-24 stsp
10311 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
10312 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
10313 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
10314 0ebf8283 2019-07-24 stsp if (error)
10315 0ebf8283 2019-07-24 stsp goto done;
10316 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10317 0ebf8283 2019-07-24 stsp commit = NULL;
10318 0ebf8283 2019-07-24 stsp
10319 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10320 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
10321 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
10322 9627c110 2020-04-18 stsp
10323 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
10324 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
10325 a0ea4fc0 2020-02-28 stsp repo);
10326 a0ea4fc0 2020-02-28 stsp if (error)
10327 a0ea4fc0 2020-02-28 stsp goto done;
10328 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10329 0ebf8283 2019-07-24 stsp break;
10330 0ebf8283 2019-07-24 stsp }
10331 0ebf8283 2019-07-24 stsp
10332 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
10333 0ebf8283 2019-07-24 stsp char *id_str;
10334 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
10335 0ebf8283 2019-07-24 stsp if (error)
10336 0ebf8283 2019-07-24 stsp goto done;
10337 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
10338 0ebf8283 2019-07-24 stsp id_str);
10339 0ebf8283 2019-07-24 stsp free(id_str);
10340 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10341 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
10342 3e3a69f1 2019-07-25 stsp fileindex);
10343 0ebf8283 2019-07-24 stsp goto done;
10344 0ebf8283 2019-07-24 stsp }
10345 0ebf8283 2019-07-24 stsp
10346 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
10347 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
10348 0ebf8283 2019-07-24 stsp if (error)
10349 0ebf8283 2019-07-24 stsp goto done;
10350 0ebf8283 2019-07-24 stsp continue;
10351 0ebf8283 2019-07-24 stsp }
10352 0ebf8283 2019-07-24 stsp
10353 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
10354 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
10355 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10356 0ebf8283 2019-07-24 stsp if (error)
10357 0ebf8283 2019-07-24 stsp goto done;
10358 0ebf8283 2019-07-24 stsp }
10359 0ebf8283 2019-07-24 stsp
10360 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
10361 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
10362 0ebf8283 2019-07-24 stsp if (error)
10363 0ebf8283 2019-07-24 stsp goto done;
10364 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
10365 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
10366 0ebf8283 2019-07-24 stsp } else
10367 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
10368 3e3a69f1 2019-07-25 stsp branch, repo);
10369 0ebf8283 2019-07-24 stsp done:
10370 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
10371 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
10372 0ebf8283 2019-07-24 stsp free(head_commit_id);
10373 0ebf8283 2019-07-24 stsp free(base_commit_id);
10374 0ebf8283 2019-07-24 stsp free(resume_commit_id);
10375 0ebf8283 2019-07-24 stsp if (commit)
10376 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10377 0ebf8283 2019-07-24 stsp if (branch)
10378 0ebf8283 2019-07-24 stsp got_ref_close(branch);
10379 0ebf8283 2019-07-24 stsp if (tmp_branch)
10380 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
10381 0ebf8283 2019-07-24 stsp if (worktree)
10382 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
10383 1d0f4054 2021-06-17 stsp if (repo) {
10384 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10385 1d0f4054 2021-06-17 stsp if (error == NULL)
10386 1d0f4054 2021-06-17 stsp error = close_err;
10387 1d0f4054 2021-06-17 stsp }
10388 2822a352 2019-10-15 stsp return error;
10389 2822a352 2019-10-15 stsp }
10390 2822a352 2019-10-15 stsp
10391 2822a352 2019-10-15 stsp __dead static void
10392 2822a352 2019-10-15 stsp usage_integrate(void)
10393 2822a352 2019-10-15 stsp {
10394 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
10395 2822a352 2019-10-15 stsp exit(1);
10396 2822a352 2019-10-15 stsp }
10397 2822a352 2019-10-15 stsp
10398 2822a352 2019-10-15 stsp static const struct got_error *
10399 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
10400 2822a352 2019-10-15 stsp {
10401 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
10402 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
10403 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
10404 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
10405 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
10406 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
10407 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
10408 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
10409 9627c110 2020-04-18 stsp int ch;
10410 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10411 2822a352 2019-10-15 stsp
10412 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
10413 2822a352 2019-10-15 stsp switch (ch) {
10414 2822a352 2019-10-15 stsp default:
10415 2822a352 2019-10-15 stsp usage_integrate();
10416 2822a352 2019-10-15 stsp /* NOTREACHED */
10417 2822a352 2019-10-15 stsp }
10418 2822a352 2019-10-15 stsp }
10419 2822a352 2019-10-15 stsp
10420 2822a352 2019-10-15 stsp argc -= optind;
10421 2822a352 2019-10-15 stsp argv += optind;
10422 2822a352 2019-10-15 stsp
10423 2822a352 2019-10-15 stsp if (argc != 1)
10424 2822a352 2019-10-15 stsp usage_integrate();
10425 2822a352 2019-10-15 stsp branch_arg = argv[0];
10426 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
10427 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10428 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
10429 2822a352 2019-10-15 stsp err(1, "pledge");
10430 b08a0ccd 2020-09-24 stsp #endif
10431 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
10432 2822a352 2019-10-15 stsp if (cwd == NULL) {
10433 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
10434 2822a352 2019-10-15 stsp goto done;
10435 2822a352 2019-10-15 stsp }
10436 2822a352 2019-10-15 stsp
10437 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
10438 fa51e947 2020-03-27 stsp if (error) {
10439 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10440 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
10441 fa51e947 2020-03-27 stsp cwd);
10442 2822a352 2019-10-15 stsp goto done;
10443 fa51e947 2020-03-27 stsp }
10444 2822a352 2019-10-15 stsp
10445 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
10446 2822a352 2019-10-15 stsp if (error)
10447 2822a352 2019-10-15 stsp goto done;
10448 2822a352 2019-10-15 stsp
10449 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10450 2822a352 2019-10-15 stsp NULL);
10451 2822a352 2019-10-15 stsp if (error != NULL)
10452 2822a352 2019-10-15 stsp goto done;
10453 2822a352 2019-10-15 stsp
10454 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10455 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
10456 2822a352 2019-10-15 stsp if (error)
10457 2822a352 2019-10-15 stsp goto done;
10458 2822a352 2019-10-15 stsp
10459 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
10460 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
10461 2822a352 2019-10-15 stsp goto done;
10462 2822a352 2019-10-15 stsp }
10463 2822a352 2019-10-15 stsp
10464 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
10465 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
10466 2822a352 2019-10-15 stsp if (error)
10467 2822a352 2019-10-15 stsp goto done;
10468 2822a352 2019-10-15 stsp
10469 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
10470 2822a352 2019-10-15 stsp if (refname == NULL) {
10471 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
10472 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10473 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10474 2822a352 2019-10-15 stsp goto done;
10475 2822a352 2019-10-15 stsp }
10476 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
10477 2822a352 2019-10-15 stsp if (base_refname == NULL) {
10478 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
10479 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10480 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10481 2822a352 2019-10-15 stsp goto done;
10482 2822a352 2019-10-15 stsp }
10483 2822a352 2019-10-15 stsp
10484 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
10485 2822a352 2019-10-15 stsp if (error)
10486 2822a352 2019-10-15 stsp goto done;
10487 2822a352 2019-10-15 stsp
10488 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
10489 2822a352 2019-10-15 stsp if (error)
10490 2822a352 2019-10-15 stsp goto done;
10491 2822a352 2019-10-15 stsp
10492 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
10493 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
10494 2822a352 2019-10-15 stsp "specified branch has already been integrated");
10495 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10496 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10497 2822a352 2019-10-15 stsp goto done;
10498 2822a352 2019-10-15 stsp }
10499 2822a352 2019-10-15 stsp
10500 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
10501 2822a352 2019-10-15 stsp if (error) {
10502 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
10503 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
10504 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10505 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10506 2822a352 2019-10-15 stsp goto done;
10507 2822a352 2019-10-15 stsp }
10508 2822a352 2019-10-15 stsp
10509 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10510 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
10511 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
10512 2822a352 2019-10-15 stsp check_cancelled, NULL);
10513 2822a352 2019-10-15 stsp if (error)
10514 2822a352 2019-10-15 stsp goto done;
10515 2822a352 2019-10-15 stsp
10516 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
10517 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10518 2822a352 2019-10-15 stsp done:
10519 1d0f4054 2021-06-17 stsp if (repo) {
10520 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10521 1d0f4054 2021-06-17 stsp if (error == NULL)
10522 1d0f4054 2021-06-17 stsp error = close_err;
10523 1d0f4054 2021-06-17 stsp }
10524 2822a352 2019-10-15 stsp if (worktree)
10525 2822a352 2019-10-15 stsp got_worktree_close(worktree);
10526 2822a352 2019-10-15 stsp free(cwd);
10527 2822a352 2019-10-15 stsp free(base_commit_id);
10528 2822a352 2019-10-15 stsp free(commit_id);
10529 2822a352 2019-10-15 stsp free(refname);
10530 2822a352 2019-10-15 stsp free(base_refname);
10531 715dc77e 2019-08-03 stsp return error;
10532 715dc77e 2019-08-03 stsp }
10533 715dc77e 2019-08-03 stsp
10534 715dc77e 2019-08-03 stsp __dead static void
10535 715dc77e 2019-08-03 stsp usage_stage(void)
10536 715dc77e 2019-08-03 stsp {
10537 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
10538 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
10539 715dc77e 2019-08-03 stsp getprogname());
10540 715dc77e 2019-08-03 stsp exit(1);
10541 715dc77e 2019-08-03 stsp }
10542 715dc77e 2019-08-03 stsp
10543 715dc77e 2019-08-03 stsp static const struct got_error *
10544 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
10545 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
10546 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
10547 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
10548 408b4ebc 2019-08-03 stsp {
10549 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
10550 408b4ebc 2019-08-03 stsp char *id_str = NULL;
10551 408b4ebc 2019-08-03 stsp
10552 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
10553 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
10554 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
10555 408b4ebc 2019-08-03 stsp return NULL;
10556 408b4ebc 2019-08-03 stsp
10557 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
10558 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
10559 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
10560 408b4ebc 2019-08-03 stsp else
10561 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
10562 408b4ebc 2019-08-03 stsp if (err)
10563 408b4ebc 2019-08-03 stsp return err;
10564 408b4ebc 2019-08-03 stsp
10565 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
10566 408b4ebc 2019-08-03 stsp free(id_str);
10567 dc424a06 2019-08-07 stsp return NULL;
10568 dc424a06 2019-08-07 stsp }
10569 2e1f37b0 2019-08-08 stsp
10570 dc424a06 2019-08-07 stsp static const struct got_error *
10571 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
10572 715dc77e 2019-08-03 stsp {
10573 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
10574 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
10575 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
10576 715dc77e 2019-08-03 stsp char *cwd = NULL;
10577 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
10578 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
10579 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
10580 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
10581 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
10582 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
10583 715dc77e 2019-08-03 stsp
10584 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
10585 715dc77e 2019-08-03 stsp
10586 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
10587 715dc77e 2019-08-03 stsp switch (ch) {
10588 408b4ebc 2019-08-03 stsp case 'l':
10589 408b4ebc 2019-08-03 stsp list_stage = 1;
10590 408b4ebc 2019-08-03 stsp break;
10591 dc424a06 2019-08-07 stsp case 'p':
10592 dc424a06 2019-08-07 stsp pflag = 1;
10593 dc424a06 2019-08-07 stsp break;
10594 dc424a06 2019-08-07 stsp case 'F':
10595 dc424a06 2019-08-07 stsp patch_script_path = optarg;
10596 dc424a06 2019-08-07 stsp break;
10597 35213c7c 2020-07-23 stsp case 'S':
10598 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
10599 35213c7c 2020-07-23 stsp break;
10600 715dc77e 2019-08-03 stsp default:
10601 715dc77e 2019-08-03 stsp usage_stage();
10602 715dc77e 2019-08-03 stsp /* NOTREACHED */
10603 715dc77e 2019-08-03 stsp }
10604 715dc77e 2019-08-03 stsp }
10605 715dc77e 2019-08-03 stsp
10606 715dc77e 2019-08-03 stsp argc -= optind;
10607 715dc77e 2019-08-03 stsp argv += optind;
10608 715dc77e 2019-08-03 stsp
10609 715dc77e 2019-08-03 stsp #ifndef PROFILE
10610 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10611 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
10612 715dc77e 2019-08-03 stsp err(1, "pledge");
10613 715dc77e 2019-08-03 stsp #endif
10614 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
10615 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
10616 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
10617 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
10618 715dc77e 2019-08-03 stsp
10619 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
10620 715dc77e 2019-08-03 stsp if (cwd == NULL) {
10621 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
10622 715dc77e 2019-08-03 stsp goto done;
10623 715dc77e 2019-08-03 stsp }
10624 715dc77e 2019-08-03 stsp
10625 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
10626 fa51e947 2020-03-27 stsp if (error) {
10627 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10628 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
10629 715dc77e 2019-08-03 stsp goto done;
10630 fa51e947 2020-03-27 stsp }
10631 715dc77e 2019-08-03 stsp
10632 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10633 c9956ddf 2019-09-08 stsp NULL);
10634 715dc77e 2019-08-03 stsp if (error != NULL)
10635 715dc77e 2019-08-03 stsp goto done;
10636 715dc77e 2019-08-03 stsp
10637 dc424a06 2019-08-07 stsp if (patch_script_path) {
10638 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
10639 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
10640 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
10641 dc424a06 2019-08-07 stsp patch_script_path);
10642 dc424a06 2019-08-07 stsp goto done;
10643 dc424a06 2019-08-07 stsp }
10644 dc424a06 2019-08-07 stsp }
10645 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10646 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
10647 715dc77e 2019-08-03 stsp if (error)
10648 715dc77e 2019-08-03 stsp goto done;
10649 715dc77e 2019-08-03 stsp
10650 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10651 6d022e97 2019-08-04 stsp if (error)
10652 6d022e97 2019-08-04 stsp goto done;
10653 715dc77e 2019-08-03 stsp
10654 6d022e97 2019-08-04 stsp if (list_stage)
10655 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
10656 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
10657 2e1f37b0 2019-08-08 stsp else {
10658 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
10659 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
10660 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
10661 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
10662 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
10663 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
10664 2e1f37b0 2019-08-08 stsp }
10665 ad493afc 2019-08-03 stsp done:
10666 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
10667 2e1f37b0 2019-08-08 stsp error == NULL)
10668 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
10669 1d0f4054 2021-06-17 stsp if (repo) {
10670 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10671 1d0f4054 2021-06-17 stsp if (error == NULL)
10672 1d0f4054 2021-06-17 stsp error = close_err;
10673 1d0f4054 2021-06-17 stsp }
10674 ad493afc 2019-08-03 stsp if (worktree)
10675 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
10676 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
10677 ad493afc 2019-08-03 stsp free((char *)pe->path);
10678 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
10679 ad493afc 2019-08-03 stsp free(cwd);
10680 ad493afc 2019-08-03 stsp return error;
10681 ad493afc 2019-08-03 stsp }
10682 ad493afc 2019-08-03 stsp
10683 ad493afc 2019-08-03 stsp __dead static void
10684 ad493afc 2019-08-03 stsp usage_unstage(void)
10685 ad493afc 2019-08-03 stsp {
10686 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
10687 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
10688 ad493afc 2019-08-03 stsp getprogname());
10689 ad493afc 2019-08-03 stsp exit(1);
10690 ad493afc 2019-08-03 stsp }
10691 ad493afc 2019-08-03 stsp
10692 ad493afc 2019-08-03 stsp
10693 ad493afc 2019-08-03 stsp static const struct got_error *
10694 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
10695 ad493afc 2019-08-03 stsp {
10696 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
10697 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
10698 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
10699 ad493afc 2019-08-03 stsp char *cwd = NULL;
10700 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
10701 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
10702 9627c110 2020-04-18 stsp int ch, pflag = 0;
10703 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10704 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
10705 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
10706 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
10707 ad493afc 2019-08-03 stsp
10708 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
10709 ad493afc 2019-08-03 stsp
10710 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
10711 ad493afc 2019-08-03 stsp switch (ch) {
10712 2e1f37b0 2019-08-08 stsp case 'p':
10713 2e1f37b0 2019-08-08 stsp pflag = 1;
10714 2e1f37b0 2019-08-08 stsp break;
10715 2e1f37b0 2019-08-08 stsp case 'F':
10716 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
10717 2e1f37b0 2019-08-08 stsp break;
10718 ad493afc 2019-08-03 stsp default:
10719 ad493afc 2019-08-03 stsp usage_unstage();
10720 ad493afc 2019-08-03 stsp /* NOTREACHED */
10721 ad493afc 2019-08-03 stsp }
10722 ad493afc 2019-08-03 stsp }
10723 ad493afc 2019-08-03 stsp
10724 ad493afc 2019-08-03 stsp argc -= optind;
10725 ad493afc 2019-08-03 stsp argv += optind;
10726 ad493afc 2019-08-03 stsp
10727 ad493afc 2019-08-03 stsp #ifndef PROFILE
10728 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10729 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
10730 ad493afc 2019-08-03 stsp err(1, "pledge");
10731 ad493afc 2019-08-03 stsp #endif
10732 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
10733 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
10734 2e1f37b0 2019-08-08 stsp
10735 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
10736 ad493afc 2019-08-03 stsp if (cwd == NULL) {
10737 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
10738 ad493afc 2019-08-03 stsp goto done;
10739 ad493afc 2019-08-03 stsp }
10740 ad493afc 2019-08-03 stsp
10741 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
10742 fa51e947 2020-03-27 stsp if (error) {
10743 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10744 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
10745 ad493afc 2019-08-03 stsp goto done;
10746 fa51e947 2020-03-27 stsp }
10747 ad493afc 2019-08-03 stsp
10748 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10749 c9956ddf 2019-09-08 stsp NULL);
10750 ad493afc 2019-08-03 stsp if (error != NULL)
10751 ad493afc 2019-08-03 stsp goto done;
10752 ad493afc 2019-08-03 stsp
10753 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
10754 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
10755 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
10756 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
10757 2e1f37b0 2019-08-08 stsp patch_script_path);
10758 2e1f37b0 2019-08-08 stsp goto done;
10759 2e1f37b0 2019-08-08 stsp }
10760 2e1f37b0 2019-08-08 stsp }
10761 2e1f37b0 2019-08-08 stsp
10762 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10763 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
10764 ad493afc 2019-08-03 stsp if (error)
10765 ad493afc 2019-08-03 stsp goto done;
10766 ad493afc 2019-08-03 stsp
10767 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10768 ad493afc 2019-08-03 stsp if (error)
10769 ad493afc 2019-08-03 stsp goto done;
10770 ad493afc 2019-08-03 stsp
10771 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
10772 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
10773 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10774 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
10775 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
10776 9627c110 2020-04-18 stsp if (!error)
10777 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10778 715dc77e 2019-08-03 stsp done:
10779 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
10780 2e1f37b0 2019-08-08 stsp error == NULL)
10781 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
10782 1d0f4054 2021-06-17 stsp if (repo) {
10783 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10784 1d0f4054 2021-06-17 stsp if (error == NULL)
10785 1d0f4054 2021-06-17 stsp error = close_err;
10786 1d0f4054 2021-06-17 stsp }
10787 715dc77e 2019-08-03 stsp if (worktree)
10788 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
10789 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
10790 715dc77e 2019-08-03 stsp free((char *)pe->path);
10791 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
10792 715dc77e 2019-08-03 stsp free(cwd);
10793 01073a5d 2019-08-22 stsp return error;
10794 01073a5d 2019-08-22 stsp }
10795 01073a5d 2019-08-22 stsp
10796 01073a5d 2019-08-22 stsp __dead static void
10797 01073a5d 2019-08-22 stsp usage_cat(void)
10798 01073a5d 2019-08-22 stsp {
10799 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
10800 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
10801 01073a5d 2019-08-22 stsp exit(1);
10802 01073a5d 2019-08-22 stsp }
10803 01073a5d 2019-08-22 stsp
10804 01073a5d 2019-08-22 stsp static const struct got_error *
10805 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10806 01073a5d 2019-08-22 stsp {
10807 01073a5d 2019-08-22 stsp const struct got_error *err;
10808 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
10809 01073a5d 2019-08-22 stsp
10810 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
10811 01073a5d 2019-08-22 stsp if (err)
10812 01073a5d 2019-08-22 stsp return err;
10813 01073a5d 2019-08-22 stsp
10814 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
10815 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
10816 01073a5d 2019-08-22 stsp return err;
10817 01073a5d 2019-08-22 stsp }
10818 01073a5d 2019-08-22 stsp
10819 01073a5d 2019-08-22 stsp static const struct got_error *
10820 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10821 01073a5d 2019-08-22 stsp {
10822 01073a5d 2019-08-22 stsp const struct got_error *err;
10823 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
10824 56e0773d 2019-11-28 stsp int nentries, i;
10825 01073a5d 2019-08-22 stsp
10826 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
10827 01073a5d 2019-08-22 stsp if (err)
10828 01073a5d 2019-08-22 stsp return err;
10829 01073a5d 2019-08-22 stsp
10830 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
10831 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
10832 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
10833 01073a5d 2019-08-22 stsp char *id_str;
10834 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
10835 01073a5d 2019-08-22 stsp break;
10836 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
10837 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
10838 01073a5d 2019-08-22 stsp if (err)
10839 01073a5d 2019-08-22 stsp break;
10840 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
10841 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
10842 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
10843 01073a5d 2019-08-22 stsp free(id_str);
10844 01073a5d 2019-08-22 stsp }
10845 01073a5d 2019-08-22 stsp
10846 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
10847 01073a5d 2019-08-22 stsp return err;
10848 01073a5d 2019-08-22 stsp }
10849 01073a5d 2019-08-22 stsp
10850 01073a5d 2019-08-22 stsp static const struct got_error *
10851 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10852 01073a5d 2019-08-22 stsp {
10853 01073a5d 2019-08-22 stsp const struct got_error *err;
10854 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
10855 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
10856 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
10857 01073a5d 2019-08-22 stsp char *id_str = NULL;
10858 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
10859 01073a5d 2019-08-22 stsp
10860 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
10861 01073a5d 2019-08-22 stsp if (err)
10862 01073a5d 2019-08-22 stsp return err;
10863 01073a5d 2019-08-22 stsp
10864 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
10865 01073a5d 2019-08-22 stsp if (err)
10866 01073a5d 2019-08-22 stsp goto done;
10867 01073a5d 2019-08-22 stsp
10868 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
10869 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10870 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
10871 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
10872 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
10873 01073a5d 2019-08-22 stsp char *pid_str;
10874 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
10875 01073a5d 2019-08-22 stsp if (err)
10876 01073a5d 2019-08-22 stsp goto done;
10877 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
10878 01073a5d 2019-08-22 stsp free(pid_str);
10879 01073a5d 2019-08-22 stsp }
10880 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
10881 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
10882 1367695b 2020-09-26 naddy (long long)got_object_commit_get_author_time(commit));
10883 01073a5d 2019-08-22 stsp
10884 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
10885 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
10886 1367695b 2020-09-26 naddy (long long)got_object_commit_get_committer_time(commit));
10887 01073a5d 2019-08-22 stsp
10888 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
10889 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
10890 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
10891 01073a5d 2019-08-22 stsp done:
10892 01073a5d 2019-08-22 stsp free(id_str);
10893 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
10894 01073a5d 2019-08-22 stsp return err;
10895 01073a5d 2019-08-22 stsp }
10896 01073a5d 2019-08-22 stsp
10897 01073a5d 2019-08-22 stsp static const struct got_error *
10898 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10899 01073a5d 2019-08-22 stsp {
10900 01073a5d 2019-08-22 stsp const struct got_error *err;
10901 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
10902 01073a5d 2019-08-22 stsp char *id_str = NULL;
10903 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
10904 01073a5d 2019-08-22 stsp
10905 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
10906 01073a5d 2019-08-22 stsp if (err)
10907 01073a5d 2019-08-22 stsp return err;
10908 01073a5d 2019-08-22 stsp
10909 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
10910 01073a5d 2019-08-22 stsp if (err)
10911 01073a5d 2019-08-22 stsp goto done;
10912 01073a5d 2019-08-22 stsp
10913 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
10914 e15d5241 2019-08-22 stsp
10915 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
10916 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
10917 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10918 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
10919 01073a5d 2019-08-22 stsp break;
10920 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
10921 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10922 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
10923 01073a5d 2019-08-22 stsp break;
10924 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
10925 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10926 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
10927 01073a5d 2019-08-22 stsp break;
10928 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
10929 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10930 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
10931 01073a5d 2019-08-22 stsp break;
10932 01073a5d 2019-08-22 stsp default:
10933 01073a5d 2019-08-22 stsp break;
10934 01073a5d 2019-08-22 stsp }
10935 01073a5d 2019-08-22 stsp
10936 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
10937 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
10938 e15d5241 2019-08-22 stsp
10939 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
10940 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
10941 1367695b 2020-09-26 naddy (long long)got_object_tag_get_tagger_time(tag));
10942 01073a5d 2019-08-22 stsp
10943 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
10944 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
10945 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
10946 01073a5d 2019-08-22 stsp done:
10947 01073a5d 2019-08-22 stsp free(id_str);
10948 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
10949 01073a5d 2019-08-22 stsp return err;
10950 01073a5d 2019-08-22 stsp }
10951 01073a5d 2019-08-22 stsp
10952 01073a5d 2019-08-22 stsp static const struct got_error *
10953 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
10954 01073a5d 2019-08-22 stsp {
10955 01073a5d 2019-08-22 stsp const struct got_error *error;
10956 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
10957 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
10958 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
10959 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
10960 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
10961 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
10962 84de9106 2020-12-26 stsp struct got_reflist_head refs;
10963 84de9106 2020-12-26 stsp
10964 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
10965 01073a5d 2019-08-22 stsp
10966 01073a5d 2019-08-22 stsp #ifndef PROFILE
10967 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
10968 01073a5d 2019-08-22 stsp NULL) == -1)
10969 01073a5d 2019-08-22 stsp err(1, "pledge");
10970 01073a5d 2019-08-22 stsp #endif
10971 01073a5d 2019-08-22 stsp
10972 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
10973 01073a5d 2019-08-22 stsp switch (ch) {
10974 896e9b6f 2019-08-26 stsp case 'c':
10975 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
10976 896e9b6f 2019-08-26 stsp break;
10977 01073a5d 2019-08-22 stsp case 'r':
10978 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
10979 01073a5d 2019-08-22 stsp if (repo_path == NULL)
10980 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
10981 9ba1d308 2019-10-21 stsp optarg);
10982 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
10983 01073a5d 2019-08-22 stsp break;
10984 896e9b6f 2019-08-26 stsp case 'P':
10985 896e9b6f 2019-08-26 stsp force_path = 1;
10986 896e9b6f 2019-08-26 stsp break;
10987 01073a5d 2019-08-22 stsp default:
10988 01073a5d 2019-08-22 stsp usage_cat();
10989 01073a5d 2019-08-22 stsp /* NOTREACHED */
10990 01073a5d 2019-08-22 stsp }
10991 01073a5d 2019-08-22 stsp }
10992 01073a5d 2019-08-22 stsp
10993 01073a5d 2019-08-22 stsp argc -= optind;
10994 01073a5d 2019-08-22 stsp argv += optind;
10995 01073a5d 2019-08-22 stsp
10996 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
10997 01073a5d 2019-08-22 stsp if (cwd == NULL) {
10998 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
10999 01073a5d 2019-08-22 stsp goto done;
11000 01073a5d 2019-08-22 stsp }
11001 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
11002 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
11003 01073a5d 2019-08-22 stsp goto done;
11004 01073a5d 2019-08-22 stsp if (worktree) {
11005 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
11006 01073a5d 2019-08-22 stsp repo_path = strdup(
11007 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
11008 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
11009 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
11010 01073a5d 2019-08-22 stsp goto done;
11011 01073a5d 2019-08-22 stsp }
11012 01073a5d 2019-08-22 stsp }
11013 01073a5d 2019-08-22 stsp }
11014 01073a5d 2019-08-22 stsp
11015 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
11016 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
11017 01073a5d 2019-08-22 stsp if (repo_path == NULL)
11018 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
11019 01073a5d 2019-08-22 stsp }
11020 01073a5d 2019-08-22 stsp
11021 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
11022 01073a5d 2019-08-22 stsp free(repo_path);
11023 01073a5d 2019-08-22 stsp if (error != NULL)
11024 01073a5d 2019-08-22 stsp goto done;
11025 01073a5d 2019-08-22 stsp
11026 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
11027 896e9b6f 2019-08-26 stsp if (error)
11028 896e9b6f 2019-08-26 stsp goto done;
11029 896e9b6f 2019-08-26 stsp
11030 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
11031 84de9106 2020-12-26 stsp if (error)
11032 84de9106 2020-12-26 stsp goto done;
11033 84de9106 2020-12-26 stsp
11034 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
11035 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
11036 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
11037 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
11038 01073a5d 2019-08-22 stsp if (error)
11039 01073a5d 2019-08-22 stsp goto done;
11040 01073a5d 2019-08-22 stsp
11041 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
11042 896e9b6f 2019-08-26 stsp if (force_path) {
11043 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
11044 896e9b6f 2019-08-26 stsp argv[i]);
11045 896e9b6f 2019-08-26 stsp if (error)
11046 896e9b6f 2019-08-26 stsp break;
11047 896e9b6f 2019-08-26 stsp } else {
11048 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
11049 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
11050 84de9106 2020-12-26 stsp repo);
11051 896e9b6f 2019-08-26 stsp if (error) {
11052 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
11053 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
11054 896e9b6f 2019-08-26 stsp break;
11055 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
11056 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
11057 896e9b6f 2019-08-26 stsp if (error)
11058 896e9b6f 2019-08-26 stsp break;
11059 896e9b6f 2019-08-26 stsp }
11060 896e9b6f 2019-08-26 stsp }
11061 01073a5d 2019-08-22 stsp
11062 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
11063 01073a5d 2019-08-22 stsp if (error)
11064 01073a5d 2019-08-22 stsp break;
11065 01073a5d 2019-08-22 stsp
11066 01073a5d 2019-08-22 stsp switch (obj_type) {
11067 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
11068 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
11069 01073a5d 2019-08-22 stsp break;
11070 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
11071 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
11072 01073a5d 2019-08-22 stsp break;
11073 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
11074 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
11075 01073a5d 2019-08-22 stsp break;
11076 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
11077 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
11078 01073a5d 2019-08-22 stsp break;
11079 01073a5d 2019-08-22 stsp default:
11080 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
11081 01073a5d 2019-08-22 stsp break;
11082 01073a5d 2019-08-22 stsp }
11083 01073a5d 2019-08-22 stsp if (error)
11084 01073a5d 2019-08-22 stsp break;
11085 01073a5d 2019-08-22 stsp free(label);
11086 01073a5d 2019-08-22 stsp label = NULL;
11087 01073a5d 2019-08-22 stsp free(id);
11088 01073a5d 2019-08-22 stsp id = NULL;
11089 01073a5d 2019-08-22 stsp }
11090 01073a5d 2019-08-22 stsp done:
11091 01073a5d 2019-08-22 stsp free(label);
11092 01073a5d 2019-08-22 stsp free(id);
11093 896e9b6f 2019-08-26 stsp free(commit_id);
11094 01073a5d 2019-08-22 stsp if (worktree)
11095 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
11096 01073a5d 2019-08-22 stsp if (repo) {
11097 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
11098 01073a5d 2019-08-22 stsp if (error == NULL)
11099 1d0f4054 2021-06-17 stsp error = close_err;
11100 b2118c49 2020-07-28 stsp }
11101 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
11102 b2118c49 2020-07-28 stsp return error;
11103 b2118c49 2020-07-28 stsp }
11104 b2118c49 2020-07-28 stsp
11105 b2118c49 2020-07-28 stsp __dead static void
11106 b2118c49 2020-07-28 stsp usage_info(void)
11107 b2118c49 2020-07-28 stsp {
11108 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
11109 b2118c49 2020-07-28 stsp getprogname());
11110 b2118c49 2020-07-28 stsp exit(1);
11111 b2118c49 2020-07-28 stsp }
11112 b2118c49 2020-07-28 stsp
11113 b2118c49 2020-07-28 stsp static const struct got_error *
11114 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
11115 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11116 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
11117 b2118c49 2020-07-28 stsp {
11118 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
11119 b2118c49 2020-07-28 stsp char *id_str = NULL;
11120 b2118c49 2020-07-28 stsp char datebuf[128];
11121 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
11122 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
11123 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11124 b2118c49 2020-07-28 stsp
11125 b2118c49 2020-07-28 stsp /*
11126 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
11127 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
11128 b2118c49 2020-07-28 stsp */
11129 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
11130 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
11131 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
11132 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
11133 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
11134 b2118c49 2020-07-28 stsp }
11135 b2118c49 2020-07-28 stsp
11136 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
11137 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
11138 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
11139 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
11140 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
11141 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
11142 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
11143 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
11144 b2118c49 2020-07-28 stsp else
11145 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
11146 b2118c49 2020-07-28 stsp
11147 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
11148 b2118c49 2020-07-28 stsp if (tm == NULL)
11149 b2118c49 2020-07-28 stsp return NULL;
11150 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
11151 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
11152 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
11153 b2118c49 2020-07-28 stsp
11154 b2118c49 2020-07-28 stsp if (blob_id) {
11155 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
11156 b2118c49 2020-07-28 stsp if (err)
11157 b2118c49 2020-07-28 stsp return err;
11158 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
11159 b2118c49 2020-07-28 stsp free(id_str);
11160 b2118c49 2020-07-28 stsp }
11161 b2118c49 2020-07-28 stsp
11162 b2118c49 2020-07-28 stsp if (staged_blob_id) {
11163 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
11164 b2118c49 2020-07-28 stsp if (err)
11165 b2118c49 2020-07-28 stsp return err;
11166 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
11167 b2118c49 2020-07-28 stsp free(id_str);
11168 b2118c49 2020-07-28 stsp }
11169 b2118c49 2020-07-28 stsp
11170 b2118c49 2020-07-28 stsp if (commit_id) {
11171 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
11172 b2118c49 2020-07-28 stsp if (err)
11173 b2118c49 2020-07-28 stsp return err;
11174 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
11175 b2118c49 2020-07-28 stsp free(id_str);
11176 b2118c49 2020-07-28 stsp }
11177 b2118c49 2020-07-28 stsp
11178 b2118c49 2020-07-28 stsp return NULL;
11179 b2118c49 2020-07-28 stsp }
11180 b2118c49 2020-07-28 stsp
11181 b2118c49 2020-07-28 stsp static const struct got_error *
11182 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
11183 b2118c49 2020-07-28 stsp {
11184 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
11185 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
11186 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
11187 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
11188 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11189 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
11190 b2118c49 2020-07-28 stsp int ch, show_files = 0;
11191 b2118c49 2020-07-28 stsp
11192 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
11193 b2118c49 2020-07-28 stsp
11194 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
11195 b2118c49 2020-07-28 stsp switch (ch) {
11196 b2118c49 2020-07-28 stsp default:
11197 b2118c49 2020-07-28 stsp usage_info();
11198 b2118c49 2020-07-28 stsp /* NOTREACHED */
11199 b2118c49 2020-07-28 stsp }
11200 01073a5d 2019-08-22 stsp }
11201 b2118c49 2020-07-28 stsp
11202 b2118c49 2020-07-28 stsp argc -= optind;
11203 b2118c49 2020-07-28 stsp argv += optind;
11204 b2118c49 2020-07-28 stsp
11205 b2118c49 2020-07-28 stsp #ifndef PROFILE
11206 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
11207 b2118c49 2020-07-28 stsp NULL) == -1)
11208 b2118c49 2020-07-28 stsp err(1, "pledge");
11209 b2118c49 2020-07-28 stsp #endif
11210 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
11211 b2118c49 2020-07-28 stsp if (cwd == NULL) {
11212 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
11213 b2118c49 2020-07-28 stsp goto done;
11214 b2118c49 2020-07-28 stsp }
11215 b2118c49 2020-07-28 stsp
11216 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
11217 b2118c49 2020-07-28 stsp if (error) {
11218 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
11219 8ea5c997 2021-02-07 naddy error = wrap_not_worktree_error(error, "info", cwd);
11220 b2118c49 2020-07-28 stsp goto done;
11221 b2118c49 2020-07-28 stsp }
11222 b2118c49 2020-07-28 stsp
11223 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
11224 b2118c49 2020-07-28 stsp if (error)
11225 b2118c49 2020-07-28 stsp goto done;
11226 b2118c49 2020-07-28 stsp
11227 b2118c49 2020-07-28 stsp if (argc >= 1) {
11228 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
11229 b2118c49 2020-07-28 stsp worktree);
11230 b2118c49 2020-07-28 stsp if (error)
11231 b2118c49 2020-07-28 stsp goto done;
11232 b2118c49 2020-07-28 stsp show_files = 1;
11233 b2118c49 2020-07-28 stsp }
11234 b2118c49 2020-07-28 stsp
11235 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
11236 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
11237 b2118c49 2020-07-28 stsp if (error)
11238 b2118c49 2020-07-28 stsp goto done;
11239 b2118c49 2020-07-28 stsp
11240 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
11241 b2118c49 2020-07-28 stsp if (error)
11242 b2118c49 2020-07-28 stsp goto done;
11243 b2118c49 2020-07-28 stsp
11244 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
11245 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
11246 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
11247 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
11248 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
11249 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
11250 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
11251 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
11252 b2118c49 2020-07-28 stsp
11253 b2118c49 2020-07-28 stsp if (show_files) {
11254 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11255 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
11256 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
11257 b2118c49 2020-07-28 stsp continue;
11258 b2118c49 2020-07-28 stsp /*
11259 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
11260 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
11261 b2118c49 2020-07-28 stsp */
11262 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
11263 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
11264 b2118c49 2020-07-28 stsp }
11265 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
11266 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
11267 b2118c49 2020-07-28 stsp if (error)
11268 b2118c49 2020-07-28 stsp goto done;
11269 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
11270 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
11271 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
11272 b2118c49 2020-07-28 stsp break;
11273 b2118c49 2020-07-28 stsp }
11274 b2118c49 2020-07-28 stsp }
11275 b2118c49 2020-07-28 stsp }
11276 b2118c49 2020-07-28 stsp done:
11277 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
11278 b2118c49 2020-07-28 stsp free((char *)pe->path);
11279 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
11280 b2118c49 2020-07-28 stsp free(cwd);
11281 b2118c49 2020-07-28 stsp free(id_str);
11282 b2118c49 2020-07-28 stsp free(uuidstr);
11283 0ebf8283 2019-07-24 stsp return error;
11284 0ebf8283 2019-07-24 stsp }