Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 93658fb9 2020-03-18 stsp * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 5c860e29 2018-03-12 stsp *
6 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
7 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
8 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
9 5c860e29 2018-03-12 stsp *
10 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 5c860e29 2018-03-12 stsp */
18 5c860e29 2018-03-12 stsp
19 c0768b0f 2018-06-10 stsp #include <sys/types.h>
20 5de5890b 2018-10-18 stsp #include <sys/stat.h>
21 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
22 f42b1b34 2018-03-12 stsp
23 5c860e29 2018-03-12 stsp #include <err.h>
24 5c860e29 2018-03-12 stsp #include <errno.h>
25 12463d8b 2019-12-13 stsp #include <fcntl.h>
26 12ce7a6c 2019-08-12 stsp #include <limits.h>
27 5c860e29 2018-03-12 stsp #include <locale.h>
28 818c7501 2019-07-11 stsp #include <ctype.h>
29 99437157 2018-11-11 stsp #include <signal.h>
30 5c860e29 2018-03-12 stsp #include <stdio.h>
31 5c860e29 2018-03-12 stsp #include <stdlib.h>
32 5c860e29 2018-03-12 stsp #include <string.h>
33 5c860e29 2018-03-12 stsp #include <unistd.h>
34 c09a553d 2018-03-12 stsp #include <libgen.h>
35 c0768b0f 2018-06-10 stsp #include <time.h>
36 33ad4cbe 2019-05-12 jcs #include <paths.h>
37 6841bf13 2019-11-29 kn #include <regex.h>
38 83cd27f8 2020-01-13 stsp #include <getopt.h>
39 dd038bc6 2021-09-21 thomas.ad
40 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
41 5c860e29 2018-03-12 stsp
42 53ccebc2 2019-07-30 stsp #include "got_version.h"
43 f42b1b34 2018-03-12 stsp #include "got_error.h"
44 f42b1b34 2018-03-12 stsp #include "got_object.h"
45 5261c201 2018-04-01 stsp #include "got_reference.h"
46 f42b1b34 2018-03-12 stsp #include "got_repository.h"
47 1dd54920 2019-05-11 stsp #include "got_path.h"
48 e6209546 2019-08-22 stsp #include "got_cancel.h"
49 c09a553d 2018-03-12 stsp #include "got_worktree.h"
50 79109fed 2018-03-27 stsp #include "got_diff.h"
51 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
52 6f23baec 2020-03-18 stsp #include "got_fetch.h"
53 f8a36e22 2021-08-26 stsp #include "got_send.h"
54 404c43c4 2018-06-21 stsp #include "got_blame.h"
55 63219cd2 2019-01-04 stsp #include "got_privsep.h"
56 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
57 50b0790e 2020-09-11 stsp #include "got_gotconfig.h"
58 d65a88a2 2021-09-05 stsp #include "got_dial.h"
59 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 10604dce 2021-09-24 thomas __dead static void usage_merge(void);
113 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
114 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
115 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
116 b2118c49 2020-07-28 stsp __dead static void usage_info(void);
117 5c860e29 2018-03-12 stsp
118 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
119 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
120 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
121 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
122 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
123 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
124 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
125 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
126 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
127 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
128 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
129 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
130 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
131 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
132 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
133 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
134 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
135 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
136 f8a36e22 2021-08-26 stsp static const struct got_error* cmd_send(int, char *[]);
137 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
138 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
139 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
140 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
141 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
142 10604dce 2021-09-24 thomas static const struct got_error* cmd_merge(int, char *[]);
143 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
144 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
145 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
146 b2118c49 2020-07-28 stsp static const struct got_error* cmd_info(int, char *[]);
147 5c860e29 2018-03-12 stsp
148 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
149 b2118c49 2020-07-28 stsp { "init", cmd_init, usage_init, "" },
150 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
151 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
152 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
153 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
154 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
155 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
156 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
157 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
158 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
159 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
160 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
161 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
162 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
163 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
164 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
165 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
166 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
167 f8a36e22 2021-08-26 stsp { "send", cmd_send, usage_send, "se" },
168 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
169 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
170 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
171 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
172 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
173 10604dce 2021-09-24 thomas { "merge", cmd_merge, usage_merge, "mg" },
174 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
175 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
176 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
177 b2118c49 2020-07-28 stsp { "info", cmd_info, usage_info, "" },
178 5c860e29 2018-03-12 stsp };
179 ce5b7c56 2019-07-09 stsp
180 ce5b7c56 2019-07-09 stsp static void
181 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
182 ce5b7c56 2019-07-09 stsp {
183 6059809a 2020-12-17 stsp size_t i;
184 ce5b7c56 2019-07-09 stsp
185 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
186 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
187 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
188 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->cmd_name);
189 ce5b7c56 2019-07-09 stsp }
190 6879ba42 2020-10-01 naddy fputc('\n', fp);
191 ce5b7c56 2019-07-09 stsp }
192 5c860e29 2018-03-12 stsp
193 ff69268e 2020-12-13 stsp __dead static void
194 ff69268e 2020-12-13 stsp option_conflict(char a, char b)
195 ff69268e 2020-12-13 stsp {
196 ff69268e 2020-12-13 stsp errx(1, "-%c and -%c options are mutually exclusive", a, b);
197 ff69268e 2020-12-13 stsp }
198 ff69268e 2020-12-13 stsp
199 5c860e29 2018-03-12 stsp int
200 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
201 5c860e29 2018-03-12 stsp {
202 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
203 6059809a 2020-12-17 stsp size_t i;
204 5c860e29 2018-03-12 stsp int ch;
205 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
206 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
207 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
208 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0 }
209 83cd27f8 2020-01-13 stsp };
210 5c860e29 2018-03-12 stsp
211 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
212 5c860e29 2018-03-12 stsp
213 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
214 5c860e29 2018-03-12 stsp switch (ch) {
215 1b6b95a8 2018-03-12 stsp case 'h':
216 1b6b95a8 2018-03-12 stsp hflag = 1;
217 53ccebc2 2019-07-30 stsp break;
218 53ccebc2 2019-07-30 stsp case 'V':
219 53ccebc2 2019-07-30 stsp Vflag = 1;
220 1b6b95a8 2018-03-12 stsp break;
221 5c860e29 2018-03-12 stsp default:
222 6879ba42 2020-10-01 naddy usage(hflag, 1);
223 5c860e29 2018-03-12 stsp /* NOTREACHED */
224 5c860e29 2018-03-12 stsp }
225 5c860e29 2018-03-12 stsp }
226 5c860e29 2018-03-12 stsp
227 5c860e29 2018-03-12 stsp argc -= optind;
228 5c860e29 2018-03-12 stsp argv += optind;
229 9814e6a3 2020-09-27 naddy optind = 1;
230 9814e6a3 2020-09-27 naddy optreset = 1;
231 53ccebc2 2019-07-30 stsp
232 53ccebc2 2019-07-30 stsp if (Vflag) {
233 53ccebc2 2019-07-30 stsp got_version_print_str();
234 6879ba42 2020-10-01 naddy return 0;
235 53ccebc2 2019-07-30 stsp }
236 5c860e29 2018-03-12 stsp
237 5c860e29 2018-03-12 stsp if (argc <= 0)
238 6879ba42 2020-10-01 naddy usage(hflag, hflag ? 0 : 1);
239 5c860e29 2018-03-12 stsp
240 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
241 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
242 99437157 2018-11-11 stsp
243 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
244 d7d4f210 2018-03-12 stsp const struct got_error *error;
245 d7d4f210 2018-03-12 stsp
246 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
247 5c860e29 2018-03-12 stsp
248 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
249 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
250 5c860e29 2018-03-12 stsp continue;
251 5c860e29 2018-03-12 stsp
252 1b6b95a8 2018-03-12 stsp if (hflag)
253 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
254 1b6b95a8 2018-03-12 stsp
255 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
256 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
257 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
258 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
259 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
260 70015d7a 2019-11-08 stsp !(sigint_received &&
261 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
262 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
263 d7d4f210 2018-03-12 stsp return 1;
264 d7d4f210 2018-03-12 stsp }
265 d7d4f210 2018-03-12 stsp
266 d7d4f210 2018-03-12 stsp return 0;
267 5c860e29 2018-03-12 stsp }
268 5c860e29 2018-03-12 stsp
269 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
270 6879ba42 2020-10-01 naddy list_commands(stderr);
271 5c860e29 2018-03-12 stsp return 1;
272 5c860e29 2018-03-12 stsp }
273 5c860e29 2018-03-12 stsp
274 4ed7e80c 2018-05-20 stsp __dead static void
275 6879ba42 2020-10-01 naddy usage(int hflag, int status)
276 5c860e29 2018-03-12 stsp {
277 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
278 6879ba42 2020-10-01 naddy
279 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
280 53ccebc2 2019-07-30 stsp getprogname());
281 ce5b7c56 2019-07-09 stsp if (hflag)
282 6879ba42 2020-10-01 naddy list_commands(fp);
283 6879ba42 2020-10-01 naddy exit(status);
284 5c860e29 2018-03-12 stsp }
285 5c860e29 2018-03-12 stsp
286 0266afb7 2019-01-04 stsp static const struct got_error *
287 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
288 e2ba3d07 2019-05-13 stsp {
289 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
290 e2ba3d07 2019-05-13 stsp const char *editor;
291 8920fa04 2019-08-18 stsp
292 8920fa04 2019-08-18 stsp *abspath = NULL;
293 e2ba3d07 2019-05-13 stsp
294 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
295 e2ba3d07 2019-05-13 stsp if (editor == NULL)
296 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
297 e2ba3d07 2019-05-13 stsp
298 0ee7065d 2019-05-13 stsp if (editor) {
299 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
300 0ee7065d 2019-05-13 stsp if (err)
301 0ee7065d 2019-05-13 stsp return err;
302 0ee7065d 2019-05-13 stsp }
303 e2ba3d07 2019-05-13 stsp
304 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
305 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
306 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
307 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
308 0ee7065d 2019-05-13 stsp }
309 0ee7065d 2019-05-13 stsp
310 e2ba3d07 2019-05-13 stsp return NULL;
311 e2ba3d07 2019-05-13 stsp }
312 e2ba3d07 2019-05-13 stsp
313 e2ba3d07 2019-05-13 stsp static const struct got_error *
314 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
315 c530dc23 2019-07-23 stsp const char *worktree_path)
316 0266afb7 2019-01-04 stsp {
317 163ce85a 2019-05-13 stsp const struct got_error *err;
318 0266afb7 2019-01-04 stsp
319 37c06ea4 2019-07-15 stsp #ifdef PROFILE
320 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
321 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
322 37c06ea4 2019-07-15 stsp #endif
323 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
324 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
325 0266afb7 2019-01-04 stsp
326 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
327 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
328 0266afb7 2019-01-04 stsp
329 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
330 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
331 0266afb7 2019-01-04 stsp
332 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
333 163ce85a 2019-05-13 stsp if (err != NULL)
334 163ce85a 2019-05-13 stsp return err;
335 0266afb7 2019-01-04 stsp
336 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
337 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
338 0266afb7 2019-01-04 stsp
339 0266afb7 2019-01-04 stsp return NULL;
340 2c7829a4 2019-06-17 stsp }
341 2c7829a4 2019-06-17 stsp
342 2c7829a4 2019-06-17 stsp __dead static void
343 2c7829a4 2019-06-17 stsp usage_init(void)
344 2c7829a4 2019-06-17 stsp {
345 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
346 2c7829a4 2019-06-17 stsp exit(1);
347 2c7829a4 2019-06-17 stsp }
348 2c7829a4 2019-06-17 stsp
349 2c7829a4 2019-06-17 stsp static const struct got_error *
350 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
351 2c7829a4 2019-06-17 stsp {
352 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
353 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
354 2c7829a4 2019-06-17 stsp int ch;
355 2c7829a4 2019-06-17 stsp
356 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
357 2c7829a4 2019-06-17 stsp switch (ch) {
358 2c7829a4 2019-06-17 stsp default:
359 2c7829a4 2019-06-17 stsp usage_init();
360 2c7829a4 2019-06-17 stsp /* NOTREACHED */
361 2c7829a4 2019-06-17 stsp }
362 2c7829a4 2019-06-17 stsp }
363 2c7829a4 2019-06-17 stsp
364 2c7829a4 2019-06-17 stsp argc -= optind;
365 2c7829a4 2019-06-17 stsp argv += optind;
366 2c7829a4 2019-06-17 stsp
367 2c7829a4 2019-06-17 stsp #ifndef PROFILE
368 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
369 2c7829a4 2019-06-17 stsp err(1, "pledge");
370 2c7829a4 2019-06-17 stsp #endif
371 2c7829a4 2019-06-17 stsp if (argc != 1)
372 bc20e173 2019-06-17 stsp usage_init();
373 2c7829a4 2019-06-17 stsp
374 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
375 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
376 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
377 2c7829a4 2019-06-17 stsp
378 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
379 2c7829a4 2019-06-17 stsp
380 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
381 2c7829a4 2019-06-17 stsp if (error &&
382 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
383 2c7829a4 2019-06-17 stsp goto done;
384 2c7829a4 2019-06-17 stsp
385 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
386 2c7829a4 2019-06-17 stsp if (error)
387 2c7829a4 2019-06-17 stsp goto done;
388 2c7829a4 2019-06-17 stsp
389 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
390 3ce1b845 2019-07-15 stsp done:
391 3ce1b845 2019-07-15 stsp free(repo_path);
392 3ce1b845 2019-07-15 stsp return error;
393 3ce1b845 2019-07-15 stsp }
394 3ce1b845 2019-07-15 stsp
395 3ce1b845 2019-07-15 stsp __dead static void
396 3ce1b845 2019-07-15 stsp usage_import(void)
397 3ce1b845 2019-07-15 stsp {
398 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
399 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
400 3ce1b845 2019-07-15 stsp exit(1);
401 3ce1b845 2019-07-15 stsp }
402 3ce1b845 2019-07-15 stsp
403 3ce1b845 2019-07-15 stsp int
404 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
405 3ce1b845 2019-07-15 stsp {
406 3ce1b845 2019-07-15 stsp pid_t pid;
407 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
408 3ce1b845 2019-07-15 stsp int st = -1;
409 3ce1b845 2019-07-15 stsp
410 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
411 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
412 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
413 3ce1b845 2019-07-15 stsp
414 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
415 3ce1b845 2019-07-15 stsp case -1:
416 3ce1b845 2019-07-15 stsp goto doneediting;
417 3ce1b845 2019-07-15 stsp case 0:
418 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
419 3ce1b845 2019-07-15 stsp _exit(127);
420 3ce1b845 2019-07-15 stsp }
421 3ce1b845 2019-07-15 stsp
422 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
423 3ce1b845 2019-07-15 stsp if (errno != EINTR)
424 3ce1b845 2019-07-15 stsp break;
425 3ce1b845 2019-07-15 stsp
426 3ce1b845 2019-07-15 stsp doneediting:
427 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
428 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
429 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
430 3ce1b845 2019-07-15 stsp
431 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
432 3ce1b845 2019-07-15 stsp errno = EINTR;
433 3ce1b845 2019-07-15 stsp return -1;
434 3ce1b845 2019-07-15 stsp }
435 3ce1b845 2019-07-15 stsp
436 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
437 3ce1b845 2019-07-15 stsp }
438 3ce1b845 2019-07-15 stsp
439 3ce1b845 2019-07-15 stsp static const struct got_error *
440 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
441 28cf319f 2021-01-28 stsp const char *initial_content, size_t initial_content_len,
442 28cf319f 2021-01-28 stsp int require_modification)
443 3ce1b845 2019-07-15 stsp {
444 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
445 bfa12d5e 2020-09-26 stsp char *line = NULL;
446 bfa12d5e 2020-09-26 stsp size_t linesize = 0;
447 bfa12d5e 2020-09-26 stsp ssize_t linelen;
448 3ce1b845 2019-07-15 stsp struct stat st, st2;
449 bfa12d5e 2020-09-26 stsp FILE *fp = NULL;
450 bfa12d5e 2020-09-26 stsp size_t len, logmsg_len;
451 bfa12d5e 2020-09-26 stsp char *initial_content_stripped = NULL, *buf = NULL, *s;
452 3ce1b845 2019-07-15 stsp
453 3ce1b845 2019-07-15 stsp *logmsg = NULL;
454 3ce1b845 2019-07-15 stsp
455 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
456 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
457 3ce1b845 2019-07-15 stsp
458 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
459 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
460 3ce1b845 2019-07-15 stsp
461 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
462 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
463 3ce1b845 2019-07-15 stsp
464 28cf319f 2021-01-28 stsp if (require_modification &&
465 28cf319f 2021-01-28 stsp st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
466 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
467 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
468 3ce1b845 2019-07-15 stsp
469 bfa12d5e 2020-09-26 stsp /*
470 bfa12d5e 2020-09-26 stsp * Set up a stripped version of the initial content without comments
471 bfa12d5e 2020-09-26 stsp * and blank lines. We need this in order to check if the message
472 bfa12d5e 2020-09-26 stsp * has in fact been edited.
473 bfa12d5e 2020-09-26 stsp */
474 bfa12d5e 2020-09-26 stsp initial_content_stripped = malloc(initial_content_len + 1);
475 bfa12d5e 2020-09-26 stsp if (initial_content_stripped == NULL)
476 bfa12d5e 2020-09-26 stsp return got_error_from_errno("malloc");
477 bfa12d5e 2020-09-26 stsp initial_content_stripped[0] = '\0';
478 bfa12d5e 2020-09-26 stsp
479 bfa12d5e 2020-09-26 stsp buf = strdup(initial_content);
480 bfa12d5e 2020-09-26 stsp if (buf == NULL) {
481 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("strdup");
482 bfa12d5e 2020-09-26 stsp goto done;
483 bfa12d5e 2020-09-26 stsp }
484 bfa12d5e 2020-09-26 stsp s = buf;
485 bfa12d5e 2020-09-26 stsp len = 0;
486 bfa12d5e 2020-09-26 stsp while ((line = strsep(&s, "\n")) != NULL) {
487 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
488 bfa12d5e 2020-09-26 stsp continue; /* remove comments and leading empty lines */
489 bfa12d5e 2020-09-26 stsp len = strlcat(initial_content_stripped, line,
490 bfa12d5e 2020-09-26 stsp initial_content_len + 1);
491 bfa12d5e 2020-09-26 stsp if (len >= initial_content_len + 1) {
492 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
493 bfa12d5e 2020-09-26 stsp goto done;
494 bfa12d5e 2020-09-26 stsp }
495 bfa12d5e 2020-09-26 stsp }
496 bfa12d5e 2020-09-26 stsp while (len > 0 && initial_content_stripped[len - 1] == '\n') {
497 bfa12d5e 2020-09-26 stsp initial_content_stripped[len - 1] = '\0';
498 bfa12d5e 2020-09-26 stsp len--;
499 bfa12d5e 2020-09-26 stsp }
500 bfa12d5e 2020-09-26 stsp
501 bfa12d5e 2020-09-26 stsp logmsg_len = st2.st_size;
502 bfa12d5e 2020-09-26 stsp *logmsg = malloc(logmsg_len + 1);
503 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
504 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
505 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
506 3ce1b845 2019-07-15 stsp
507 c56c5d8a 2021-12-31 thomas fp = fopen(logmsg_path, "re");
508 3ce1b845 2019-07-15 stsp if (fp == NULL) {
509 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
510 3ce1b845 2019-07-15 stsp goto done;
511 3ce1b845 2019-07-15 stsp }
512 bfa12d5e 2020-09-26 stsp
513 bfa12d5e 2020-09-26 stsp len = 0;
514 bfa12d5e 2020-09-26 stsp while ((linelen = getline(&line, &linesize, fp)) != -1) {
515 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
516 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
517 bfa12d5e 2020-09-26 stsp len = strlcat(*logmsg, line, logmsg_len + 1);
518 bfa12d5e 2020-09-26 stsp if (len >= logmsg_len + 1) {
519 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
520 bfa12d5e 2020-09-26 stsp goto done;
521 bfa12d5e 2020-09-26 stsp }
522 3ce1b845 2019-07-15 stsp }
523 bfa12d5e 2020-09-26 stsp free(line);
524 bfa12d5e 2020-09-26 stsp if (ferror(fp)) {
525 bfa12d5e 2020-09-26 stsp err = got_ferror(fp, GOT_ERR_IO);
526 bfa12d5e 2020-09-26 stsp goto done;
527 bfa12d5e 2020-09-26 stsp }
528 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
529 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
530 3ce1b845 2019-07-15 stsp len--;
531 3ce1b845 2019-07-15 stsp }
532 3ce1b845 2019-07-15 stsp
533 bfa12d5e 2020-09-26 stsp if (len == 0) {
534 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
535 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
536 bfa12d5e 2020-09-26 stsp goto done;
537 bfa12d5e 2020-09-26 stsp }
538 28cf319f 2021-01-28 stsp if (require_modification &&
539 28cf319f 2021-01-28 stsp strcmp(*logmsg, initial_content_stripped) == 0)
540 bfa12d5e 2020-09-26 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
541 bfa12d5e 2020-09-26 stsp "no changes made to commit message, aborting");
542 3ce1b845 2019-07-15 stsp done:
543 bfa12d5e 2020-09-26 stsp free(initial_content_stripped);
544 bfa12d5e 2020-09-26 stsp free(buf);
545 bfa12d5e 2020-09-26 stsp if (fp && fclose(fp) == EOF && err == NULL)
546 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("fclose");
547 3ce1b845 2019-07-15 stsp if (err) {
548 3ce1b845 2019-07-15 stsp free(*logmsg);
549 3ce1b845 2019-07-15 stsp *logmsg = NULL;
550 3ce1b845 2019-07-15 stsp }
551 3ce1b845 2019-07-15 stsp return err;
552 3ce1b845 2019-07-15 stsp }
553 3ce1b845 2019-07-15 stsp
554 3ce1b845 2019-07-15 stsp static const struct got_error *
555 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
556 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
557 3ce1b845 2019-07-15 stsp {
558 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
559 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
560 1601cb9f 2020-09-11 naddy int initial_content_len;
561 97972933 2020-09-11 stsp int fd = -1;
562 3ce1b845 2019-07-15 stsp
563 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
564 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
565 1601cb9f 2020-09-11 naddy branch_name);
566 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
567 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
568 3ce1b845 2019-07-15 stsp
569 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
570 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
571 3ce1b845 2019-07-15 stsp if (err)
572 3ce1b845 2019-07-15 stsp goto done;
573 3ce1b845 2019-07-15 stsp
574 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
575 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
576 97972933 2020-09-11 stsp goto done;
577 97972933 2020-09-11 stsp }
578 3ce1b845 2019-07-15 stsp
579 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
580 0d5bb276 2020-12-15 stsp initial_content_len, 1);
581 3ce1b845 2019-07-15 stsp done:
582 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
583 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
584 3ce1b845 2019-07-15 stsp free(initial_content);
585 59f86c76 2020-09-11 stsp if (err) {
586 59f86c76 2020-09-11 stsp free(*logmsg_path);
587 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
588 59f86c76 2020-09-11 stsp }
589 3ce1b845 2019-07-15 stsp return err;
590 3ce1b845 2019-07-15 stsp }
591 3ce1b845 2019-07-15 stsp
592 3ce1b845 2019-07-15 stsp static const struct got_error *
593 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
594 3ce1b845 2019-07-15 stsp {
595 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
596 3ce1b845 2019-07-15 stsp return NULL;
597 3ce1b845 2019-07-15 stsp }
598 3ce1b845 2019-07-15 stsp
599 3ce1b845 2019-07-15 stsp static const struct got_error *
600 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
601 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
602 84792843 2019-08-09 stsp {
603 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
604 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
605 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
606 84792843 2019-08-09 stsp
607 84792843 2019-08-09 stsp *author = NULL;
608 aba9c984 2019-09-08 stsp
609 50b0790e 2020-09-11 stsp if (worktree)
610 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
611 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
612 50b0790e 2020-09-11 stsp
613 50b0790e 2020-09-11 stsp /*
614 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
615 50b0790e 2020-09-11 stsp * significant to least significant:
616 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
617 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
618 50b0790e 2020-09-11 stsp * 3) repository's git config file
619 50b0790e 2020-09-11 stsp * 4) environment variables
620 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
621 50b0790e 2020-09-11 stsp */
622 50b0790e 2020-09-11 stsp
623 50b0790e 2020-09-11 stsp if (worktree_conf)
624 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
625 50b0790e 2020-09-11 stsp if (got_author == NULL)
626 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
627 84792843 2019-08-09 stsp if (got_author == NULL) {
628 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
629 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
630 c9956ddf 2019-09-08 stsp if (name && email) {
631 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
632 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
633 c9956ddf 2019-09-08 stsp return NULL;
634 c9956ddf 2019-09-08 stsp }
635 257add31 2020-09-09 stsp
636 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
637 257add31 2020-09-09 stsp if (got_author == NULL) {
638 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
639 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
640 257add31 2020-09-09 stsp repo);
641 257add31 2020-09-09 stsp if (name && email) {
642 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
643 257add31 2020-09-09 stsp == -1)
644 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
645 257add31 2020-09-09 stsp return NULL;
646 257add31 2020-09-09 stsp }
647 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
648 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
649 257add31 2020-09-09 stsp }
650 84792843 2019-08-09 stsp }
651 84792843 2019-08-09 stsp
652 aba9c984 2019-09-08 stsp *author = strdup(got_author);
653 aba9c984 2019-09-08 stsp if (*author == NULL)
654 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
655 84792843 2019-08-09 stsp
656 84792843 2019-08-09 stsp /*
657 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
658 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
659 84792843 2019-08-09 stsp */
660 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
661 84792843 2019-08-09 stsp got_author++;
662 aba9c984 2019-09-08 stsp if (*got_author != '<') {
663 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
664 aba9c984 2019-09-08 stsp goto done;
665 aba9c984 2019-09-08 stsp }
666 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
667 84792843 2019-08-09 stsp got_author++;
668 aba9c984 2019-09-08 stsp if (*got_author != '@') {
669 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
670 aba9c984 2019-09-08 stsp goto done;
671 aba9c984 2019-09-08 stsp }
672 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
673 84792843 2019-08-09 stsp got_author++;
674 84792843 2019-08-09 stsp if (*got_author != '>')
675 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
676 aba9c984 2019-09-08 stsp done:
677 aba9c984 2019-09-08 stsp if (err) {
678 aba9c984 2019-09-08 stsp free(*author);
679 aba9c984 2019-09-08 stsp *author = NULL;
680 aba9c984 2019-09-08 stsp }
681 aba9c984 2019-09-08 stsp return err;
682 c9956ddf 2019-09-08 stsp }
683 c9956ddf 2019-09-08 stsp
684 c9956ddf 2019-09-08 stsp static const struct got_error *
685 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
686 c9956ddf 2019-09-08 stsp {
687 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
688 c9956ddf 2019-09-08 stsp
689 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
690 c9956ddf 2019-09-08 stsp if (homedir) {
691 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
692 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
693 c9956ddf 2019-09-08 stsp
694 c9956ddf 2019-09-08 stsp }
695 c9956ddf 2019-09-08 stsp return NULL;
696 84792843 2019-08-09 stsp }
697 84792843 2019-08-09 stsp
698 84792843 2019-08-09 stsp static const struct got_error *
699 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
700 3ce1b845 2019-07-15 stsp {
701 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
702 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
703 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
704 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
705 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
706 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
707 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
708 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
709 3ce1b845 2019-07-15 stsp int ch;
710 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
711 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
712 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
713 3ce1b845 2019-07-15 stsp
714 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
715 3ce1b845 2019-07-15 stsp
716 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
717 3ce1b845 2019-07-15 stsp switch (ch) {
718 3ce1b845 2019-07-15 stsp case 'b':
719 3ce1b845 2019-07-15 stsp branch_name = optarg;
720 3ce1b845 2019-07-15 stsp break;
721 3ce1b845 2019-07-15 stsp case 'm':
722 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
723 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
724 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
725 3ce1b845 2019-07-15 stsp goto done;
726 3ce1b845 2019-07-15 stsp }
727 3ce1b845 2019-07-15 stsp break;
728 3ce1b845 2019-07-15 stsp case 'r':
729 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
730 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
731 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
732 9ba1d308 2019-10-21 stsp optarg);
733 3ce1b845 2019-07-15 stsp goto done;
734 3ce1b845 2019-07-15 stsp }
735 3ce1b845 2019-07-15 stsp break;
736 3ce1b845 2019-07-15 stsp case 'I':
737 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
738 3ce1b845 2019-07-15 stsp break;
739 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
740 3ce1b845 2019-07-15 stsp NULL);
741 3ce1b845 2019-07-15 stsp if (error)
742 3ce1b845 2019-07-15 stsp goto done;
743 3ce1b845 2019-07-15 stsp break;
744 3ce1b845 2019-07-15 stsp default:
745 b2b65d18 2019-08-22 stsp usage_import();
746 3ce1b845 2019-07-15 stsp /* NOTREACHED */
747 3ce1b845 2019-07-15 stsp }
748 3ce1b845 2019-07-15 stsp }
749 3ce1b845 2019-07-15 stsp
750 3ce1b845 2019-07-15 stsp argc -= optind;
751 3ce1b845 2019-07-15 stsp argv += optind;
752 3ce1b845 2019-07-15 stsp
753 3ce1b845 2019-07-15 stsp #ifndef PROFILE
754 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
755 aba9c984 2019-09-08 stsp "unveil",
756 3ce1b845 2019-07-15 stsp NULL) == -1)
757 3ce1b845 2019-07-15 stsp err(1, "pledge");
758 3ce1b845 2019-07-15 stsp #endif
759 3ce1b845 2019-07-15 stsp if (argc != 1)
760 3ce1b845 2019-07-15 stsp usage_import();
761 2c7829a4 2019-06-17 stsp
762 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
763 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
764 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
765 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
766 3ce1b845 2019-07-15 stsp }
767 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
768 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
769 c9956ddf 2019-09-08 stsp if (error)
770 c9956ddf 2019-09-08 stsp goto done;
771 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
772 3ce1b845 2019-07-15 stsp if (error)
773 3ce1b845 2019-07-15 stsp goto done;
774 aba9c984 2019-09-08 stsp
775 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
776 aba9c984 2019-09-08 stsp if (error)
777 aba9c984 2019-09-08 stsp return error;
778 e560b7e0 2019-11-28 stsp
779 e560b7e0 2019-11-28 stsp /*
780 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
781 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
782 e560b7e0 2019-11-28 stsp * an unintended typo.
783 e560b7e0 2019-11-28 stsp */
784 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
785 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
786 3ce1b845 2019-07-15 stsp
787 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
788 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
789 3ce1b845 2019-07-15 stsp goto done;
790 3ce1b845 2019-07-15 stsp }
791 3ce1b845 2019-07-15 stsp
792 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
793 3ce1b845 2019-07-15 stsp if (error) {
794 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
795 3ce1b845 2019-07-15 stsp goto done;
796 3ce1b845 2019-07-15 stsp } else {
797 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
798 3ce1b845 2019-07-15 stsp "import target branch already exists");
799 3ce1b845 2019-07-15 stsp goto done;
800 3ce1b845 2019-07-15 stsp }
801 3ce1b845 2019-07-15 stsp
802 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
803 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
804 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
805 3ce1b845 2019-07-15 stsp goto done;
806 3ce1b845 2019-07-15 stsp }
807 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
808 3ce1b845 2019-07-15 stsp
809 3ce1b845 2019-07-15 stsp /*
810 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
811 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
812 3ce1b845 2019-07-15 stsp */
813 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
814 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
815 3ce1b845 2019-07-15 stsp if (error)
816 3ce1b845 2019-07-15 stsp goto done;
817 8e158b01 2019-09-22 stsp free(logmsg);
818 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
819 ef293bdd 2019-10-21 stsp path_dir, refname);
820 ef293bdd 2019-10-21 stsp if (error) {
821 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
822 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
823 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
824 3ce1b845 2019-07-15 stsp goto done;
825 ef293bdd 2019-10-21 stsp }
826 3ce1b845 2019-07-15 stsp }
827 3ce1b845 2019-07-15 stsp
828 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
829 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
830 ef293bdd 2019-10-21 stsp if (logmsg_path)
831 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
832 3ce1b845 2019-07-15 stsp goto done;
833 ef293bdd 2019-10-21 stsp }
834 3ce1b845 2019-07-15 stsp
835 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
836 ef293bdd 2019-10-21 stsp if (error) {
837 ef293bdd 2019-10-21 stsp if (logmsg_path)
838 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
839 ef293bdd 2019-10-21 stsp goto done;
840 ef293bdd 2019-10-21 stsp }
841 ef293bdd 2019-10-21 stsp
842 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
843 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
844 ef293bdd 2019-10-21 stsp if (error) {
845 ef293bdd 2019-10-21 stsp if (logmsg_path)
846 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
847 3ce1b845 2019-07-15 stsp goto done;
848 ef293bdd 2019-10-21 stsp }
849 3ce1b845 2019-07-15 stsp
850 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
851 ef293bdd 2019-10-21 stsp if (error) {
852 ef293bdd 2019-10-21 stsp if (logmsg_path)
853 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
854 3ce1b845 2019-07-15 stsp goto done;
855 ef293bdd 2019-10-21 stsp }
856 3ce1b845 2019-07-15 stsp
857 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
858 ef293bdd 2019-10-21 stsp if (error) {
859 ef293bdd 2019-10-21 stsp if (logmsg_path)
860 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
861 3ce1b845 2019-07-15 stsp goto done;
862 ef293bdd 2019-10-21 stsp }
863 3ce1b845 2019-07-15 stsp
864 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
865 ef293bdd 2019-10-21 stsp if (error) {
866 ef293bdd 2019-10-21 stsp if (logmsg_path)
867 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
868 3ce1b845 2019-07-15 stsp goto done;
869 ef293bdd 2019-10-21 stsp }
870 3ce1b845 2019-07-15 stsp
871 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
872 3ce1b845 2019-07-15 stsp if (error) {
873 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
874 ef293bdd 2019-10-21 stsp if (logmsg_path)
875 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
876 3ce1b845 2019-07-15 stsp goto done;
877 ef293bdd 2019-10-21 stsp }
878 3ce1b845 2019-07-15 stsp
879 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
880 3ce1b845 2019-07-15 stsp branch_ref);
881 ef293bdd 2019-10-21 stsp if (error) {
882 ef293bdd 2019-10-21 stsp if (logmsg_path)
883 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
884 3ce1b845 2019-07-15 stsp goto done;
885 ef293bdd 2019-10-21 stsp }
886 3ce1b845 2019-07-15 stsp
887 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
888 ef293bdd 2019-10-21 stsp if (error) {
889 ef293bdd 2019-10-21 stsp if (logmsg_path)
890 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
891 3ce1b845 2019-07-15 stsp goto done;
892 ef293bdd 2019-10-21 stsp }
893 3ce1b845 2019-07-15 stsp }
894 3ce1b845 2019-07-15 stsp
895 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
896 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
897 2c7829a4 2019-06-17 stsp done:
898 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
899 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
900 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
901 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
902 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
903 8e158b01 2019-09-22 stsp free(logmsg);
904 ef293bdd 2019-10-21 stsp free(logmsg_path);
905 2c7829a4 2019-06-17 stsp free(repo_path);
906 3ce1b845 2019-07-15 stsp free(editor);
907 3ce1b845 2019-07-15 stsp free(refname);
908 3ce1b845 2019-07-15 stsp free(new_commit_id);
909 3ce1b845 2019-07-15 stsp free(id_str);
910 aba9c984 2019-09-08 stsp free(author);
911 c9956ddf 2019-09-08 stsp free(gitconfig_path);
912 3ce1b845 2019-07-15 stsp if (branch_ref)
913 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
914 3ce1b845 2019-07-15 stsp if (head_ref)
915 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
916 2c7829a4 2019-06-17 stsp return error;
917 93658fb9 2020-03-18 stsp }
918 93658fb9 2020-03-18 stsp
919 93658fb9 2020-03-18 stsp __dead static void
920 93658fb9 2020-03-18 stsp usage_clone(void)
921 93658fb9 2020-03-18 stsp {
922 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
923 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
924 93658fb9 2020-03-18 stsp exit(1);
925 93658fb9 2020-03-18 stsp }
926 892ac3b6 2020-03-18 stsp
927 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
928 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
929 892ac3b6 2020-03-18 stsp int last_p_indexed;
930 892ac3b6 2020-03-18 stsp int last_p_resolved;
931 68999b92 2020-03-18 stsp int verbosity;
932 04d9a9ec 2020-09-24 stsp
933 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
934 04d9a9ec 2020-09-24 stsp
935 04d9a9ec 2020-09-24 stsp int create_configs;
936 04d9a9ec 2020-09-24 stsp int configs_created;
937 04d9a9ec 2020-09-24 stsp struct {
938 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
939 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
940 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs;
941 04d9a9ec 2020-09-24 stsp const char *proto;
942 04d9a9ec 2020-09-24 stsp const char *host;
943 04d9a9ec 2020-09-24 stsp const char *port;
944 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
945 04d9a9ec 2020-09-24 stsp const char *git_url;
946 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
947 04d9a9ec 2020-09-24 stsp int mirror_references;
948 04d9a9ec 2020-09-24 stsp } config_info;
949 892ac3b6 2020-03-18 stsp };
950 93658fb9 2020-03-18 stsp
951 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
952 93658fb9 2020-03-18 stsp static const struct got_error *
953 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
954 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
955 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
956 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
957 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo);
958 04d9a9ec 2020-09-24 stsp
959 04d9a9ec 2020-09-24 stsp static const struct got_error *
960 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
961 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
962 531c3985 2020-03-18 stsp {
963 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
964 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
965 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
966 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
967 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
968 04d9a9ec 2020-09-24 stsp
969 04d9a9ec 2020-09-24 stsp /*
970 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
971 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
972 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
973 04d9a9ec 2020-09-24 stsp * we have all required information.
974 04d9a9ec 2020-09-24 stsp */
975 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
976 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
977 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
978 62d463ca 2020-10-20 naddy a->config_info.host, a->config_info.port,
979 62d463ca 2020-10-20 naddy a->config_info.remote_repo_path,
980 62d463ca 2020-10-20 naddy a->config_info.git_url,
981 62d463ca 2020-10-20 naddy a->config_info.fetch_all_branches,
982 62d463ca 2020-10-20 naddy a->config_info.mirror_references,
983 62d463ca 2020-10-20 naddy a->config_info.symrefs,
984 99495ddb 2021-01-10 stsp a->config_info.wanted_branches,
985 99495ddb 2021-01-10 stsp a->config_info.wanted_refs, a->repo);
986 04d9a9ec 2020-09-24 stsp if (err)
987 04d9a9ec 2020-09-24 stsp return err;
988 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
989 04d9a9ec 2020-09-24 stsp }
990 b2409d58 2020-03-18 stsp
991 68999b92 2020-03-18 stsp if (a->verbosity < 0)
992 68999b92 2020-03-18 stsp return NULL;
993 68999b92 2020-03-18 stsp
994 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
995 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
996 892ac3b6 2020-03-18 stsp fflush(stdout);
997 12d1281e 2020-03-19 stsp return NULL;
998 b2409d58 2020-03-18 stsp }
999 b2409d58 2020-03-18 stsp
1000 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
1001 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1002 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
1003 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
1004 892ac3b6 2020-03-18 stsp print_size = 1;
1005 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
1006 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1007 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
1008 892ac3b6 2020-03-18 stsp }
1009 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
1010 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
1011 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
1012 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
1013 892ac3b6 2020-03-18 stsp print_indexed = 1;
1014 892ac3b6 2020-03-18 stsp print_size = 1;
1015 892ac3b6 2020-03-18 stsp }
1016 61cc1a7a 2020-03-18 stsp }
1017 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
1018 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
1019 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
1020 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
1021 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
1022 892ac3b6 2020-03-18 stsp print_resolved = 1;
1023 892ac3b6 2020-03-18 stsp print_indexed = 1;
1024 892ac3b6 2020-03-18 stsp print_size = 1;
1025 892ac3b6 2020-03-18 stsp }
1026 61cc1a7a 2020-03-18 stsp }
1027 3168e5da 2020-09-10 stsp
1028 d2cdc636 2020-03-18 stsp }
1029 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1030 892ac3b6 2020-03-18 stsp printf("\r");
1031 892ac3b6 2020-03-18 stsp if (print_size)
1032 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1033 d715f13e 2020-03-19 stsp if (print_indexed)
1034 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
1035 d715f13e 2020-03-19 stsp if (print_resolved)
1036 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
1037 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1038 892ac3b6 2020-03-18 stsp fflush(stdout);
1039 892ac3b6 2020-03-18 stsp
1040 531c3985 2020-03-18 stsp return NULL;
1041 531c3985 2020-03-18 stsp }
1042 531c3985 2020-03-18 stsp
1043 531c3985 2020-03-18 stsp static const struct got_error *
1044 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
1045 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
1046 4ba14133 2020-03-20 stsp {
1047 4ba14133 2020-03-20 stsp const struct got_error *err;
1048 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
1049 4ba14133 2020-03-20 stsp
1050 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1051 4ba14133 2020-03-20 stsp if (err)
1052 4ba14133 2020-03-20 stsp return err;
1053 4ba14133 2020-03-20 stsp
1054 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
1055 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
1056 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
1057 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
1058 6338a6a1 2020-03-21 stsp }
1059 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
1060 4ba14133 2020-03-20 stsp return err;
1061 4ba14133 2020-03-20 stsp }
1062 4ba14133 2020-03-20 stsp
1063 4ba14133 2020-03-20 stsp static const struct got_error *
1064 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
1065 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
1066 41b0de12 2020-03-21 stsp {
1067 41b0de12 2020-03-21 stsp const struct got_error *err;
1068 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
1069 41b0de12 2020-03-21 stsp
1070 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1071 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1072 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1073 41b0de12 2020-03-21 stsp
1074 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1075 41b0de12 2020-03-21 stsp }
1076 41b0de12 2020-03-21 stsp
1077 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1078 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1079 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1080 41b0de12 2020-03-21 stsp char *id_str;
1081 41b0de12 2020-03-21 stsp
1082 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1083 41b0de12 2020-03-21 stsp if (err)
1084 41b0de12 2020-03-21 stsp return err;
1085 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1086 41b0de12 2020-03-21 stsp free(id_str);
1087 41b0de12 2020-03-21 stsp }
1088 41b0de12 2020-03-21 stsp
1089 41b0de12 2020-03-21 stsp return NULL;
1090 6338a6a1 2020-03-21 stsp }
1091 6338a6a1 2020-03-21 stsp
1092 6338a6a1 2020-03-21 stsp static const struct got_error *
1093 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1094 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1095 6338a6a1 2020-03-21 stsp {
1096 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1097 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1098 6338a6a1 2020-03-21 stsp char *id_str;
1099 6338a6a1 2020-03-21 stsp
1100 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1101 6338a6a1 2020-03-21 stsp if (err)
1102 6338a6a1 2020-03-21 stsp return err;
1103 6338a6a1 2020-03-21 stsp
1104 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1105 6338a6a1 2020-03-21 stsp if (err)
1106 6338a6a1 2020-03-21 stsp goto done;
1107 6338a6a1 2020-03-21 stsp
1108 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1109 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1110 6338a6a1 2020-03-21 stsp
1111 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1112 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1113 6338a6a1 2020-03-21 stsp done:
1114 6338a6a1 2020-03-21 stsp free(id_str);
1115 6338a6a1 2020-03-21 stsp return err;
1116 0e4002ca 2020-03-21 stsp }
1117 0e4002ca 2020-03-21 stsp
1118 0e4002ca 2020-03-21 stsp static int
1119 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1120 0e4002ca 2020-03-21 stsp {
1121 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1122 0e4002ca 2020-03-21 stsp return 0;
1123 0e4002ca 2020-03-21 stsp refname += 5;
1124 0e4002ca 2020-03-21 stsp
1125 0e4002ca 2020-03-21 stsp /*
1126 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1127 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1128 0e4002ca 2020-03-21 stsp */
1129 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1130 0e4002ca 2020-03-21 stsp return 0;
1131 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1132 0e4002ca 2020-03-21 stsp return 0;
1133 0e4002ca 2020-03-21 stsp
1134 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1135 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1136 0e4002ca 2020-03-21 stsp
1137 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1138 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1139 0e4002ca 2020-03-21 stsp return 1;
1140 0e4002ca 2020-03-21 stsp
1141 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1142 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1143 41b0de12 2020-03-21 stsp }
1144 41b0de12 2020-03-21 stsp
1145 0e4002ca 2020-03-21 stsp static int
1146 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1147 0e4002ca 2020-03-21 stsp {
1148 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1149 0e4002ca 2020-03-21 stsp
1150 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1151 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1152 0e4002ca 2020-03-21 stsp return 1;
1153 0e4002ca 2020-03-21 stsp }
1154 0e4002ca 2020-03-21 stsp
1155 0e4002ca 2020-03-21 stsp return 0;
1156 0e4002ca 2020-03-21 stsp }
1157 0e4002ca 2020-03-21 stsp
1158 41b0de12 2020-03-21 stsp static const struct got_error *
1159 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1160 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1161 0e4002ca 2020-03-21 stsp {
1162 0e4002ca 2020-03-21 stsp const struct got_error *err;
1163 0e4002ca 2020-03-21 stsp char *remote_refname;
1164 0e4002ca 2020-03-21 stsp
1165 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1166 0e4002ca 2020-03-21 stsp refname += 5;
1167 0e4002ca 2020-03-21 stsp
1168 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1169 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1170 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1171 0e4002ca 2020-03-21 stsp
1172 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1173 0e4002ca 2020-03-21 stsp free(remote_refname);
1174 7c0b7f42 2020-09-24 stsp return err;
1175 7c0b7f42 2020-09-24 stsp }
1176 7c0b7f42 2020-09-24 stsp
1177 7c0b7f42 2020-09-24 stsp static const struct got_error *
1178 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1179 15d3c221 2021-01-05 stsp const char *remote_repo_path, const char *default_branch,
1180 0c8b29c5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1181 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1182 99495ddb 2021-01-10 stsp struct got_repository *repo)
1183 7c0b7f42 2020-09-24 stsp {
1184 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1185 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1186 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1187 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1188 15d3c221 2021-01-05 stsp const char *branchname = NULL;
1189 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1190 7c0b7f42 2020-09-24 stsp ssize_t n;
1191 7c0b7f42 2020-09-24 stsp
1192 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1193 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1194 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1195 132af4a5 2021-01-05 stsp char *s;
1196 132af4a5 2021-01-05 stsp branchname = pe->path;
1197 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1198 132af4a5 2021-01-05 stsp branchname += 11;
1199 132af4a5 2021-01-05 stsp if (asprintf(&s, "%s\"%s\" ",
1200 132af4a5 2021-01-05 stsp branches ? branches : "", branchname) == -1) {
1201 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1202 132af4a5 2021-01-05 stsp goto done;
1203 132af4a5 2021-01-05 stsp }
1204 132af4a5 2021-01-05 stsp free(branches);
1205 132af4a5 2021-01-05 stsp branches = s;
1206 132af4a5 2021-01-05 stsp }
1207 0c8b29c5 2021-01-05 stsp } else if (!fetch_all_branches && default_branch) {
1208 15d3c221 2021-01-05 stsp branchname = default_branch;
1209 15d3c221 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1210 15d3c221 2021-01-05 stsp branchname += 11;
1211 132af4a5 2021-01-05 stsp if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1212 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1213 132af4a5 2021-01-05 stsp goto done;
1214 99495ddb 2021-01-10 stsp }
1215 99495ddb 2021-01-10 stsp }
1216 99495ddb 2021-01-10 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1217 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1218 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1219 99495ddb 2021-01-10 stsp char *s;
1220 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1221 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1222 99495ddb 2021-01-10 stsp branchname += 5;
1223 99495ddb 2021-01-10 stsp if (asprintf(&s, "%s\"%s\" ",
1224 99495ddb 2021-01-10 stsp refs ? refs : "", refname) == -1) {
1225 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1226 99495ddb 2021-01-10 stsp goto done;
1227 99495ddb 2021-01-10 stsp }
1228 99495ddb 2021-01-10 stsp free(refs);
1229 99495ddb 2021-01-10 stsp refs = s;
1230 132af4a5 2021-01-05 stsp }
1231 15d3c221 2021-01-05 stsp }
1232 15d3c221 2021-01-05 stsp
1233 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1234 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1235 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1236 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1237 7c0b7f42 2020-09-24 stsp goto done;
1238 7c0b7f42 2020-09-24 stsp }
1239 c56c5d8a 2021-12-31 thomas gotconfig_file = fopen(gotconfig_path, "ae");
1240 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1241 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1242 7c0b7f42 2020-09-24 stsp goto done;
1243 7c0b7f42 2020-09-24 stsp }
1244 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1245 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1246 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1247 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1248 7c0b7f42 2020-09-24 stsp "%s%s%s"
1249 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1250 15d3c221 2021-01-05 stsp "%s%s%s"
1251 99495ddb 2021-01-10 stsp "%s%s%s"
1252 7c0b7f42 2020-09-24 stsp "%s"
1253 0c8b29c5 2021-01-05 stsp "%s"
1254 7c0b7f42 2020-09-24 stsp "}\n",
1255 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1256 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1257 132af4a5 2021-01-05 stsp remote_repo_path, branches ? "\tbranch { " : "",
1258 132af4a5 2021-01-05 stsp branches ? branches : "", branches ? "}\n" : "",
1259 99495ddb 2021-01-10 stsp refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1260 0c8b29c5 2021-01-05 stsp mirror_references ? "\tmirror-references yes\n" : "",
1261 0c8b29c5 2021-01-05 stsp fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1262 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1263 7c0b7f42 2020-09-24 stsp goto done;
1264 7c0b7f42 2020-09-24 stsp }
1265 7c0b7f42 2020-09-24 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1266 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1267 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1268 7c0b7f42 2020-09-24 stsp goto done;
1269 7c0b7f42 2020-09-24 stsp }
1270 7c0b7f42 2020-09-24 stsp
1271 7c0b7f42 2020-09-24 stsp done:
1272 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1273 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1274 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1275 132af4a5 2021-01-05 stsp free(branches);
1276 7c0b7f42 2020-09-24 stsp return err;
1277 7c0b7f42 2020-09-24 stsp }
1278 7c0b7f42 2020-09-24 stsp
1279 7c0b7f42 2020-09-24 stsp static const struct got_error *
1280 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1281 132af4a5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1282 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1283 99495ddb 2021-01-10 stsp struct got_repository *repo)
1284 7c0b7f42 2020-09-24 stsp {
1285 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1286 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1287 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1288 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1289 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1290 56d0a753 2021-01-20 stsp const char *branchname;
1291 7c0b7f42 2020-09-24 stsp ssize_t n;
1292 7c0b7f42 2020-09-24 stsp
1293 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1294 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1295 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1296 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1297 7c0b7f42 2020-09-24 stsp goto done;
1298 7c0b7f42 2020-09-24 stsp }
1299 c56c5d8a 2021-12-31 thomas gitconfig_file = fopen(gitconfig_path, "ae");
1300 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1301 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1302 7c0b7f42 2020-09-24 stsp goto done;
1303 7c0b7f42 2020-09-24 stsp }
1304 56d0a753 2021-01-20 stsp if (fetch_all_branches) {
1305 56d0a753 2021-01-20 stsp if (mirror_references) {
1306 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1307 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1308 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1309 56d0a753 2021-01-20 stsp goto done;
1310 56d0a753 2021-01-20 stsp }
1311 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1312 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1313 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1314 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1315 7c0b7f42 2020-09-24 stsp goto done;
1316 132af4a5 2021-01-05 stsp }
1317 132af4a5 2021-01-05 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
1318 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1319 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1320 132af4a5 2021-01-05 stsp char *s;
1321 132af4a5 2021-01-05 stsp branchname = pe->path;
1322 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1323 132af4a5 2021-01-05 stsp branchname += 11;
1324 56d0a753 2021-01-20 stsp if (mirror_references) {
1325 56d0a753 2021-01-20 stsp if (asprintf(&s,
1326 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1327 56d0a753 2021-01-20 stsp branches ? branches : "",
1328 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1329 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1330 56d0a753 2021-01-20 stsp goto done;
1331 56d0a753 2021-01-20 stsp }
1332 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1333 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1334 132af4a5 2021-01-05 stsp branches ? branches : "",
1335 132af4a5 2021-01-05 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1336 132af4a5 2021-01-05 stsp branchname) == -1) {
1337 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1338 132af4a5 2021-01-05 stsp goto done;
1339 132af4a5 2021-01-05 stsp }
1340 132af4a5 2021-01-05 stsp free(branches);
1341 132af4a5 2021-01-05 stsp branches = s;
1342 7c0b7f42 2020-09-24 stsp }
1343 7c0b7f42 2020-09-24 stsp } else {
1344 7c0b7f42 2020-09-24 stsp /*
1345 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1346 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1347 7c0b7f42 2020-09-24 stsp */
1348 04d9a9ec 2020-09-24 stsp if (default_branch) {
1349 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1350 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1351 7c0b7f42 2020-09-24 stsp branchname += 11;
1352 7c0b7f42 2020-09-24 stsp } else
1353 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1354 56d0a753 2021-01-20 stsp if (mirror_references) {
1355 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1356 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/heads/%s\n",
1357 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1358 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1359 56d0a753 2021-01-20 stsp goto done;
1360 56d0a753 2021-01-20 stsp }
1361 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1362 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1363 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1364 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1365 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1366 7c0b7f42 2020-09-24 stsp goto done;
1367 99495ddb 2021-01-10 stsp }
1368 99495ddb 2021-01-10 stsp }
1369 56d0a753 2021-01-20 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1370 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1371 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1372 99495ddb 2021-01-10 stsp char *s;
1373 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1374 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1375 99495ddb 2021-01-10 stsp refname += 5;
1376 56d0a753 2021-01-20 stsp if (mirror_references) {
1377 56d0a753 2021-01-20 stsp if (asprintf(&s,
1378 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/%s\n",
1379 56d0a753 2021-01-20 stsp refs ? refs : "", refname, refname) == -1) {
1380 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1381 56d0a753 2021-01-20 stsp goto done;
1382 56d0a753 2021-01-20 stsp }
1383 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1384 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1385 99495ddb 2021-01-10 stsp refs ? refs : "",
1386 99495ddb 2021-01-10 stsp refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1387 99495ddb 2021-01-10 stsp refname) == -1) {
1388 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1389 99495ddb 2021-01-10 stsp goto done;
1390 99495ddb 2021-01-10 stsp }
1391 99495ddb 2021-01-10 stsp free(refs);
1392 99495ddb 2021-01-10 stsp refs = s;
1393 7c0b7f42 2020-09-24 stsp }
1394 132af4a5 2021-01-05 stsp }
1395 99495ddb 2021-01-10 stsp
1396 132af4a5 2021-01-05 stsp if (asprintf(&gitconfig,
1397 132af4a5 2021-01-05 stsp "[remote \"%s\"]\n"
1398 132af4a5 2021-01-05 stsp "\turl = %s\n"
1399 132af4a5 2021-01-05 stsp "%s"
1400 99495ddb 2021-01-10 stsp "%s"
1401 56d0a753 2021-01-20 stsp "\tfetch = refs/tags/*:refs/tags/*\n",
1402 132af4a5 2021-01-05 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1403 56d0a753 2021-01-20 stsp refs ? refs : "") == -1) {
1404 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1405 132af4a5 2021-01-05 stsp goto done;
1406 7c0b7f42 2020-09-24 stsp }
1407 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1408 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1409 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1410 7c0b7f42 2020-09-24 stsp goto done;
1411 7c0b7f42 2020-09-24 stsp }
1412 7c0b7f42 2020-09-24 stsp done:
1413 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1414 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1415 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1416 132af4a5 2021-01-05 stsp free(branches);
1417 0e4002ca 2020-03-21 stsp return err;
1418 0e4002ca 2020-03-21 stsp }
1419 0e4002ca 2020-03-21 stsp
1420 0e4002ca 2020-03-21 stsp static const struct got_error *
1421 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1422 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1423 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1424 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
1425 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1426 04d9a9ec 2020-09-24 stsp {
1427 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1428 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1429 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1430 04d9a9ec 2020-09-24 stsp
1431 04d9a9ec 2020-09-24 stsp /*
1432 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1433 04d9a9ec 2020-09-24 stsp * one of those.
1434 04d9a9ec 2020-09-24 stsp */
1435 62d463ca 2020-10-20 naddy if (!TAILQ_EMPTY(wanted_branches)) {
1436 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1437 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1438 04d9a9ec 2020-09-24 stsp } else {
1439 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1440 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1441 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1442 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1443 04d9a9ec 2020-09-24 stsp
1444 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1445 04d9a9ec 2020-09-24 stsp continue;
1446 04d9a9ec 2020-09-24 stsp
1447 04d9a9ec 2020-09-24 stsp default_branch = target;
1448 04d9a9ec 2020-09-24 stsp break;
1449 04d9a9ec 2020-09-24 stsp }
1450 04d9a9ec 2020-09-24 stsp }
1451 04d9a9ec 2020-09-24 stsp
1452 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1453 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1454 0c8b29c5 2021-01-05 stsp default_branch, fetch_all_branches, wanted_branches,
1455 99495ddb 2021-01-10 stsp wanted_refs, mirror_references, repo);
1456 04d9a9ec 2020-09-24 stsp if (err)
1457 04d9a9ec 2020-09-24 stsp return err;
1458 04d9a9ec 2020-09-24 stsp
1459 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1460 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1461 99495ddb 2021-01-10 stsp wanted_branches, wanted_refs, mirror_references, repo);
1462 04d9a9ec 2020-09-24 stsp }
1463 04d9a9ec 2020-09-24 stsp
1464 04d9a9ec 2020-09-24 stsp static const struct got_error *
1465 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1466 93658fb9 2020-03-18 stsp {
1467 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1468 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1469 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1470 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1471 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1472 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1473 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1474 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1475 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1476 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1477 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1478 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1479 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1480 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1481 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1482 93658fb9 2020-03-18 stsp
1483 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1484 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1485 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1486 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1487 d9b4d0c0 2020-03-18 stsp
1488 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1489 93658fb9 2020-03-18 stsp switch (ch) {
1490 659e7fbd 2020-03-20 stsp case 'a':
1491 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1492 4ba14133 2020-03-20 stsp break;
1493 4ba14133 2020-03-20 stsp case 'b':
1494 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1495 4ba14133 2020-03-20 stsp optarg, NULL);
1496 4ba14133 2020-03-20 stsp if (error)
1497 4ba14133 2020-03-20 stsp return error;
1498 659e7fbd 2020-03-20 stsp break;
1499 41b0de12 2020-03-21 stsp case 'l':
1500 41b0de12 2020-03-21 stsp list_refs_only = 1;
1501 41b0de12 2020-03-21 stsp break;
1502 469dd726 2020-03-20 stsp case 'm':
1503 469dd726 2020-03-20 stsp mirror_references = 1;
1504 469dd726 2020-03-20 stsp break;
1505 68999b92 2020-03-18 stsp case 'v':
1506 68999b92 2020-03-18 stsp if (verbosity < 0)
1507 68999b92 2020-03-18 stsp verbosity = 0;
1508 68999b92 2020-03-18 stsp else if (verbosity < 3)
1509 68999b92 2020-03-18 stsp verbosity++;
1510 68999b92 2020-03-18 stsp break;
1511 68999b92 2020-03-18 stsp case 'q':
1512 68999b92 2020-03-18 stsp verbosity = -1;
1513 68999b92 2020-03-18 stsp break;
1514 0e4002ca 2020-03-21 stsp case 'R':
1515 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1516 0e4002ca 2020-03-21 stsp optarg, NULL);
1517 0e4002ca 2020-03-21 stsp if (error)
1518 0e4002ca 2020-03-21 stsp return error;
1519 0e4002ca 2020-03-21 stsp break;
1520 93658fb9 2020-03-18 stsp default:
1521 93658fb9 2020-03-18 stsp usage_clone();
1522 93658fb9 2020-03-18 stsp break;
1523 93658fb9 2020-03-18 stsp }
1524 93658fb9 2020-03-18 stsp }
1525 93658fb9 2020-03-18 stsp argc -= optind;
1526 93658fb9 2020-03-18 stsp argv += optind;
1527 39c64a6a 2020-03-18 stsp
1528 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1529 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
1530 41b0de12 2020-03-21 stsp if (list_refs_only) {
1531 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1532 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
1533 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1534 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
1535 41b0de12 2020-03-21 stsp if (mirror_references)
1536 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
1537 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1538 ff69268e 2020-12-13 stsp option_conflict('l', 'R');
1539 41b0de12 2020-03-21 stsp }
1540 4ba14133 2020-03-20 stsp
1541 93658fb9 2020-03-18 stsp uri = argv[0];
1542 9df6f38b 2020-03-18 stsp
1543 9df6f38b 2020-03-18 stsp if (argc == 1)
1544 93658fb9 2020-03-18 stsp dirname = NULL;
1545 9df6f38b 2020-03-18 stsp else if (argc == 2)
1546 93658fb9 2020-03-18 stsp dirname = argv[1];
1547 93658fb9 2020-03-18 stsp else
1548 93658fb9 2020-03-18 stsp usage_clone();
1549 09838ffc 2020-03-18 stsp
1550 5e5da8c4 2021-09-05 stsp error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1551 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1552 39c64a6a 2020-03-18 stsp if (error)
1553 09f63084 2020-03-20 stsp goto done;
1554 09f63084 2020-03-20 stsp
1555 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1556 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1557 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1558 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1559 09838ffc 2020-03-18 stsp goto done;
1560 09f63084 2020-03-20 stsp }
1561 09838ffc 2020-03-18 stsp
1562 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1563 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1564 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1565 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1566 39c64a6a 2020-03-18 stsp err(1, "pledge");
1567 b46f3e71 2020-03-18 stsp #endif
1568 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1569 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1570 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1571 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1572 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1573 39c64a6a 2020-03-18 stsp err(1, "pledge");
1574 b46f3e71 2020-03-18 stsp #endif
1575 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1576 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1577 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1578 39c64a6a 2020-03-18 stsp goto done;
1579 39c64a6a 2020-03-18 stsp } else {
1580 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1581 39c64a6a 2020-03-18 stsp goto done;
1582 39c64a6a 2020-03-18 stsp }
1583 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1584 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1585 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1586 bb64b798 2020-03-18 stsp goto done;
1587 bb64b798 2020-03-18 stsp }
1588 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1589 bb64b798 2020-03-18 stsp } else
1590 bb64b798 2020-03-18 stsp repo_path = dirname;
1591 bb64b798 2020-03-18 stsp
1592 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1593 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1594 2751fe64 2020-09-24 stsp if (error &&
1595 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1596 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1597 41b0de12 2020-03-21 stsp goto done;
1598 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1599 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1600 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1601 41b0de12 2020-03-21 stsp goto done;
1602 2751fe64 2020-09-24 stsp }
1603 41b0de12 2020-03-21 stsp }
1604 bb64b798 2020-03-18 stsp
1605 d65a88a2 2021-09-05 stsp error = got_dial_apply_unveil(proto);
1606 d65a88a2 2021-09-05 stsp if (error)
1607 d65a88a2 2021-09-05 stsp goto done;
1608 d65a88a2 2021-09-05 stsp
1609 f535bcd4 2020-09-30 stsp error = apply_unveil(repo_path, 0, NULL);
1610 ee448f5f 2020-03-18 stsp if (error)
1611 ee448f5f 2020-03-18 stsp goto done;
1612 f79e6490 2020-04-19 stsp
1613 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1614 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1615 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1616 ee448f5f 2020-03-18 stsp
1617 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1618 9c52365f 2020-03-21 stsp server_path, verbosity);
1619 39c64a6a 2020-03-18 stsp if (error)
1620 bb64b798 2020-03-18 stsp goto done;
1621 bb64b798 2020-03-18 stsp
1622 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1623 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1624 2751fe64 2020-09-24 stsp if (error)
1625 2751fe64 2020-09-24 stsp goto done;
1626 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1627 2751fe64 2020-09-24 stsp if (error)
1628 2751fe64 2020-09-24 stsp goto done;
1629 2751fe64 2020-09-24 stsp }
1630 2751fe64 2020-09-24 stsp
1631 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1632 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1633 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1634 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1635 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1636 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1637 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1638 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1639 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1640 99495ddb 2021-01-10 stsp fpa.config_info.wanted_refs = &wanted_refs;
1641 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1642 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1643 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1644 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1645 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1646 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1647 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1648 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1649 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1650 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1651 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1652 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1653 39c64a6a 2020-03-18 stsp if (error)
1654 d9b4d0c0 2020-03-18 stsp goto done;
1655 d9b4d0c0 2020-03-18 stsp
1656 41b0de12 2020-03-21 stsp if (list_refs_only) {
1657 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1658 41b0de12 2020-03-21 stsp goto done;
1659 41b0de12 2020-03-21 stsp }
1660 41b0de12 2020-03-21 stsp
1661 c3741b51 2021-12-31 thomas if (pack_hash == NULL) {
1662 c3741b51 2021-12-31 thomas error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1663 c3741b51 2021-12-31 thomas "server sent an empty pack file");
1664 c3741b51 2021-12-31 thomas goto done;
1665 c3741b51 2021-12-31 thomas }
1666 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1667 39c64a6a 2020-03-18 stsp if (error)
1668 d9b4d0c0 2020-03-18 stsp goto done;
1669 68999b92 2020-03-18 stsp if (verbosity >= 0)
1670 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1671 d9b4d0c0 2020-03-18 stsp free(id_str);
1672 d9b4d0c0 2020-03-18 stsp
1673 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1674 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1675 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1676 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1677 7ebc0570 2020-03-18 stsp char *remote_refname;
1678 0e4002ca 2020-03-21 stsp
1679 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1680 0e4002ca 2020-03-21 stsp !mirror_references) {
1681 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1682 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1683 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1684 0e4002ca 2020-03-21 stsp if (error)
1685 0e4002ca 2020-03-21 stsp goto done;
1686 0e4002ca 2020-03-21 stsp continue;
1687 0e4002ca 2020-03-21 stsp }
1688 668a20f6 2020-03-18 stsp
1689 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1690 39c64a6a 2020-03-18 stsp if (error)
1691 d9b4d0c0 2020-03-18 stsp goto done;
1692 d9b4d0c0 2020-03-18 stsp
1693 469dd726 2020-03-20 stsp if (mirror_references)
1694 469dd726 2020-03-20 stsp continue;
1695 469dd726 2020-03-20 stsp
1696 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1697 7ebc0570 2020-03-18 stsp continue;
1698 7ebc0570 2020-03-18 stsp
1699 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1700 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1701 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1702 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1703 7ebc0570 2020-03-18 stsp goto done;
1704 7ebc0570 2020-03-18 stsp }
1705 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1706 f298ae0f 2020-03-25 stsp free(remote_refname);
1707 39c64a6a 2020-03-18 stsp if (error)
1708 d9b4d0c0 2020-03-18 stsp goto done;
1709 d9b4d0c0 2020-03-18 stsp }
1710 d9b4d0c0 2020-03-18 stsp
1711 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1712 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1713 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1714 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1715 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1716 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1717 d9b4d0c0 2020-03-18 stsp
1718 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1719 d9b4d0c0 2020-03-18 stsp continue;
1720 d9b4d0c0 2020-03-18 stsp
1721 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1722 39c64a6a 2020-03-18 stsp if (error) {
1723 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1724 55330abe 2020-03-20 stsp error = NULL;
1725 d9b4d0c0 2020-03-18 stsp continue;
1726 55330abe 2020-03-20 stsp }
1727 d9b4d0c0 2020-03-18 stsp goto done;
1728 d9b4d0c0 2020-03-18 stsp }
1729 d9b4d0c0 2020-03-18 stsp
1730 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1731 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1732 f298ae0f 2020-03-25 stsp if (error)
1733 f298ae0f 2020-03-25 stsp goto done;
1734 f298ae0f 2020-03-25 stsp
1735 f298ae0f 2020-03-25 stsp if (mirror_references)
1736 f298ae0f 2020-03-25 stsp continue;
1737 f298ae0f 2020-03-25 stsp
1738 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1739 f298ae0f 2020-03-25 stsp continue;
1740 f298ae0f 2020-03-25 stsp
1741 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1742 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1743 f298ae0f 2020-03-25 stsp refname) == -1) {
1744 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1745 f298ae0f 2020-03-25 stsp goto done;
1746 f298ae0f 2020-03-25 stsp }
1747 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1748 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1749 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1750 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1751 f298ae0f 2020-03-25 stsp free(remote_refname);
1752 f298ae0f 2020-03-25 stsp goto done;
1753 f298ae0f 2020-03-25 stsp }
1754 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1755 f298ae0f 2020-03-25 stsp if (error) {
1756 f298ae0f 2020-03-25 stsp free(remote_refname);
1757 f298ae0f 2020-03-25 stsp free(remote_target);
1758 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1759 f298ae0f 2020-03-25 stsp error = NULL;
1760 f298ae0f 2020-03-25 stsp continue;
1761 f298ae0f 2020-03-25 stsp }
1762 f298ae0f 2020-03-25 stsp goto done;
1763 f298ae0f 2020-03-25 stsp }
1764 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1765 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1766 f298ae0f 2020-03-25 stsp free(remote_refname);
1767 f298ae0f 2020-03-25 stsp free(remote_target);
1768 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1769 39c64a6a 2020-03-18 stsp if (error)
1770 d9b4d0c0 2020-03-18 stsp goto done;
1771 4ba14133 2020-03-20 stsp }
1772 4ba14133 2020-03-20 stsp if (pe == NULL) {
1773 4ba14133 2020-03-20 stsp /*
1774 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1775 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1776 4ba14133 2020-03-20 stsp * which could be fetched instead.
1777 4ba14133 2020-03-20 stsp */
1778 62d463ca 2020-10-20 naddy TAILQ_FOREACH(pe, &wanted_branches, entry) {
1779 4ba14133 2020-03-20 stsp const char *target = pe->path;
1780 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1781 d9b4d0c0 2020-03-18 stsp
1782 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1783 4ba14133 2020-03-20 stsp if (error) {
1784 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1785 4ba14133 2020-03-20 stsp error = NULL;
1786 4ba14133 2020-03-20 stsp continue;
1787 4ba14133 2020-03-20 stsp }
1788 4ba14133 2020-03-20 stsp goto done;
1789 4ba14133 2020-03-20 stsp }
1790 4ba14133 2020-03-20 stsp
1791 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1792 04d9a9ec 2020-09-24 stsp verbosity, repo);
1793 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1794 4ba14133 2020-03-20 stsp if (error)
1795 4ba14133 2020-03-20 stsp goto done;
1796 4ba14133 2020-03-20 stsp break;
1797 4ba14133 2020-03-20 stsp }
1798 659e7fbd 2020-03-20 stsp }
1799 659e7fbd 2020-03-20 stsp
1800 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1801 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1802 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1803 09838ffc 2020-03-18 stsp done:
1804 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1805 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1806 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1807 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1808 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1809 9c52365f 2020-03-21 stsp }
1810 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1811 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1812 1d0f4054 2021-06-17 stsp if (repo) {
1813 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
1814 1d0f4054 2021-06-17 stsp if (error == NULL)
1815 1d0f4054 2021-06-17 stsp error = close_err;
1816 1d0f4054 2021-06-17 stsp }
1817 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1818 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1819 d9b4d0c0 2020-03-18 stsp free(pe->data);
1820 d9b4d0c0 2020-03-18 stsp }
1821 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1822 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1823 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1824 d9b4d0c0 2020-03-18 stsp free(pe->data);
1825 d9b4d0c0 2020-03-18 stsp }
1826 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1827 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1828 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1829 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1830 09838ffc 2020-03-18 stsp free(proto);
1831 09838ffc 2020-03-18 stsp free(host);
1832 09838ffc 2020-03-18 stsp free(port);
1833 09838ffc 2020-03-18 stsp free(server_path);
1834 09838ffc 2020-03-18 stsp free(repo_name);
1835 bb64b798 2020-03-18 stsp free(default_destdir);
1836 b46f3e71 2020-03-18 stsp free(git_url);
1837 39c64a6a 2020-03-18 stsp return error;
1838 7848a0e1 2020-03-19 stsp }
1839 7848a0e1 2020-03-19 stsp
1840 7848a0e1 2020-03-19 stsp static const struct got_error *
1841 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1842 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1843 7848a0e1 2020-03-19 stsp {
1844 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1845 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1846 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1847 7848a0e1 2020-03-19 stsp
1848 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1849 7848a0e1 2020-03-19 stsp if (err)
1850 7848a0e1 2020-03-19 stsp goto done;
1851 7848a0e1 2020-03-19 stsp
1852 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1853 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1854 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1855 88609724 2020-03-21 stsp if (err)
1856 88609724 2020-03-21 stsp goto done;
1857 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1858 88609724 2020-03-21 stsp goto done;
1859 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1860 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1861 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1862 db6d8ad8 2020-03-21 stsp }
1863 db6d8ad8 2020-03-21 stsp goto done;
1864 db6d8ad8 2020-03-21 stsp }
1865 db6d8ad8 2020-03-21 stsp
1866 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1867 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1868 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1869 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1870 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1871 688f11b3 2020-03-21 stsp }
1872 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1873 7848a0e1 2020-03-19 stsp if (err)
1874 7848a0e1 2020-03-19 stsp goto done;
1875 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1876 e8a967e0 2020-03-21 stsp if (err)
1877 e8a967e0 2020-03-21 stsp goto done;
1878 7848a0e1 2020-03-19 stsp } else {
1879 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1880 7848a0e1 2020-03-19 stsp if (err)
1881 7848a0e1 2020-03-19 stsp goto done;
1882 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1883 6338a6a1 2020-03-21 stsp goto done;
1884 6338a6a1 2020-03-21 stsp
1885 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1886 6338a6a1 2020-03-21 stsp if (err)
1887 6338a6a1 2020-03-21 stsp goto done;
1888 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1889 6338a6a1 2020-03-21 stsp if (err)
1890 6338a6a1 2020-03-21 stsp goto done;
1891 7848a0e1 2020-03-19 stsp }
1892 6338a6a1 2020-03-21 stsp
1893 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1894 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1895 6338a6a1 2020-03-21 stsp new_id_str);
1896 7848a0e1 2020-03-19 stsp done:
1897 7848a0e1 2020-03-19 stsp free(old_id);
1898 7848a0e1 2020-03-19 stsp free(new_id_str);
1899 7848a0e1 2020-03-19 stsp return err;
1900 2ab43947 2020-03-18 stsp }
1901 f1bcca34 2020-03-25 stsp
1902 f1bcca34 2020-03-25 stsp static const struct got_error *
1903 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1904 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1905 f1bcca34 2020-03-25 stsp {
1906 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1907 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1908 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1909 f1bcca34 2020-03-25 stsp
1910 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1911 bcf34b0e 2020-03-26 stsp if (err) {
1912 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1913 bcf34b0e 2020-03-26 stsp return err;
1914 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1915 bcf34b0e 2020-03-26 stsp if (err)
1916 bcf34b0e 2020-03-26 stsp goto done;
1917 2ab43947 2020-03-18 stsp
1918 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1919 bcf34b0e 2020-03-26 stsp if (err)
1920 bcf34b0e 2020-03-26 stsp goto done;
1921 f1bcca34 2020-03-25 stsp
1922 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1923 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1924 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1925 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1926 bcf34b0e 2020-03-26 stsp } else {
1927 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1928 f1bcca34 2020-03-25 stsp
1929 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1930 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1931 bcf34b0e 2020-03-26 stsp goto done;
1932 bcf34b0e 2020-03-26 stsp
1933 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1934 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1935 bcf34b0e 2020-03-26 stsp if (err)
1936 bcf34b0e 2020-03-26 stsp goto done;
1937 bcf34b0e 2020-03-26 stsp
1938 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1939 bcf34b0e 2020-03-26 stsp if (err)
1940 bcf34b0e 2020-03-26 stsp goto done;
1941 bcf34b0e 2020-03-26 stsp
1942 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1943 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1944 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1945 bcf34b0e 2020-03-26 stsp
1946 bcf34b0e 2020-03-26 stsp }
1947 f1bcca34 2020-03-25 stsp done:
1948 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1949 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1950 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1951 bcf34b0e 2020-03-26 stsp err = unlock_err;
1952 bcf34b0e 2020-03-26 stsp }
1953 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1954 f1bcca34 2020-03-25 stsp return err;
1955 f1bcca34 2020-03-25 stsp }
1956 f1bcca34 2020-03-25 stsp
1957 2ab43947 2020-03-18 stsp __dead static void
1958 7848a0e1 2020-03-19 stsp usage_fetch(void)
1959 7848a0e1 2020-03-19 stsp {
1960 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1961 161728eb 2021-07-24 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1962 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1963 13f12b09 2020-03-21 stsp getprogname());
1964 7848a0e1 2020-03-19 stsp exit(1);
1965 7848a0e1 2020-03-19 stsp }
1966 7848a0e1 2020-03-19 stsp
1967 7848a0e1 2020-03-19 stsp static const struct got_error *
1968 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1969 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1970 f21ec2f0 2020-03-21 stsp {
1971 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1972 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1973 3789fd73 2020-03-26 stsp char *id_str = NULL;
1974 3789fd73 2020-03-26 stsp
1975 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1976 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1977 3789fd73 2020-03-26 stsp if (err)
1978 3789fd73 2020-03-26 stsp return err;
1979 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1980 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
1981 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1982 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1983 3789fd73 2020-03-26 stsp }
1984 3789fd73 2020-03-26 stsp } else {
1985 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1986 3789fd73 2020-03-26 stsp if (err)
1987 3789fd73 2020-03-26 stsp return err;
1988 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1989 3789fd73 2020-03-26 stsp if (err)
1990 3789fd73 2020-03-26 stsp goto done;
1991 3168e5da 2020-09-10 stsp
1992 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1993 3789fd73 2020-03-26 stsp if (err)
1994 3789fd73 2020-03-26 stsp goto done;
1995 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1996 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
1997 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1998 3789fd73 2020-03-26 stsp }
1999 3789fd73 2020-03-26 stsp }
2000 3789fd73 2020-03-26 stsp done:
2001 3789fd73 2020-03-26 stsp free(id);
2002 3789fd73 2020-03-26 stsp free(id_str);
2003 3789fd73 2020-03-26 stsp return NULL;
2004 3789fd73 2020-03-26 stsp }
2005 3789fd73 2020-03-26 stsp
2006 3789fd73 2020-03-26 stsp static const struct got_error *
2007 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
2008 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
2009 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
2010 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
2011 3789fd73 2020-03-26 stsp {
2012 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
2013 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
2014 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
2015 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
2016 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
2017 3789fd73 2020-03-26 stsp char *local_refname = NULL;
2018 f21ec2f0 2020-03-21 stsp
2019 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&my_refs);
2020 f21ec2f0 2020-03-21 stsp
2021 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2022 3789fd73 2020-03-26 stsp == -1)
2023 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
2024 3789fd73 2020-03-26 stsp
2025 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2026 f21ec2f0 2020-03-21 stsp if (err)
2027 3789fd73 2020-03-26 stsp goto done;
2028 f21ec2f0 2020-03-21 stsp
2029 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &my_refs, entry) {
2030 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
2031 1b796c3f 2021-09-11 stsp const char *their_refname;
2032 f21ec2f0 2020-03-21 stsp
2033 1b796c3f 2021-09-11 stsp if (remote->mirror_references) {
2034 1b796c3f 2021-09-11 stsp their_refname = refname;
2035 1b796c3f 2021-09-11 stsp } else {
2036 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
2037 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
2038 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
2039 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
2040 3789fd73 2020-03-26 stsp continue;
2041 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
2042 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
2043 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
2044 3789fd73 2020-03-26 stsp goto done;
2045 3789fd73 2020-03-26 stsp }
2046 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
2047 3789fd73 2020-03-26 stsp continue;
2048 f21ec2f0 2020-03-21 stsp
2049 1b796c3f 2021-09-11 stsp their_refname = local_refname;
2050 1b796c3f 2021-09-11 stsp }
2051 1b796c3f 2021-09-11 stsp
2052 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
2053 1b796c3f 2021-09-11 stsp if (strcmp(their_refname, pe->path) == 0)
2054 f21ec2f0 2020-03-21 stsp break;
2055 f21ec2f0 2020-03-21 stsp }
2056 f21ec2f0 2020-03-21 stsp if (pe != NULL)
2057 f21ec2f0 2020-03-21 stsp continue;
2058 f21ec2f0 2020-03-21 stsp
2059 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
2060 1b796c3f 2021-09-11 stsp if (strcmp(their_refname, pe->path) == 0)
2061 3789fd73 2020-03-26 stsp break;
2062 3789fd73 2020-03-26 stsp }
2063 3789fd73 2020-03-26 stsp if (pe != NULL)
2064 3789fd73 2020-03-26 stsp continue;
2065 f21ec2f0 2020-03-21 stsp
2066 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
2067 f21ec2f0 2020-03-21 stsp if (err)
2068 f21ec2f0 2020-03-21 stsp break;
2069 3789fd73 2020-03-26 stsp
2070 3789fd73 2020-03-26 stsp if (local_refname) {
2071 3789fd73 2020-03-26 stsp struct got_reference *ref;
2072 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
2073 3789fd73 2020-03-26 stsp if (err) {
2074 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
2075 3789fd73 2020-03-26 stsp break;
2076 3789fd73 2020-03-26 stsp free(local_refname);
2077 3789fd73 2020-03-26 stsp local_refname = NULL;
2078 3789fd73 2020-03-26 stsp continue;
2079 3789fd73 2020-03-26 stsp }
2080 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
2081 3789fd73 2020-03-26 stsp if (err)
2082 3789fd73 2020-03-26 stsp break;
2083 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
2084 3789fd73 2020-03-26 stsp got_ref_close(ref);
2085 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
2086 3789fd73 2020-03-26 stsp err = unlock_err;
2087 3789fd73 2020-03-26 stsp break;
2088 3789fd73 2020-03-26 stsp }
2089 3789fd73 2020-03-26 stsp
2090 3789fd73 2020-03-26 stsp free(local_refname);
2091 3789fd73 2020-03-26 stsp local_refname = NULL;
2092 6338a6a1 2020-03-21 stsp }
2093 f21ec2f0 2020-03-21 stsp }
2094 3789fd73 2020-03-26 stsp done:
2095 3789fd73 2020-03-26 stsp free(remote_namespace);
2096 3789fd73 2020-03-26 stsp free(local_refname);
2097 0e4002ca 2020-03-21 stsp return err;
2098 0e4002ca 2020-03-21 stsp }
2099 0e4002ca 2020-03-21 stsp
2100 0e4002ca 2020-03-21 stsp static const struct got_error *
2101 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
2102 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
2103 0e4002ca 2020-03-21 stsp {
2104 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
2105 0e4002ca 2020-03-21 stsp char *remote_refname;
2106 0e4002ca 2020-03-21 stsp struct got_reference *ref;
2107 0e4002ca 2020-03-21 stsp
2108 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
2109 0e4002ca 2020-03-21 stsp refname += 5;
2110 0e4002ca 2020-03-21 stsp
2111 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2112 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
2113 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
2114 f21ec2f0 2020-03-21 stsp
2115 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
2116 0e4002ca 2020-03-21 stsp if (err) {
2117 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
2118 0e4002ca 2020-03-21 stsp goto done;
2119 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
2120 0e4002ca 2020-03-21 stsp } else {
2121 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
2122 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2123 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
2124 9f142382 2020-03-21 stsp err = unlock_err;
2125 0e4002ca 2020-03-21 stsp got_ref_close(ref);
2126 0e4002ca 2020-03-21 stsp }
2127 0e4002ca 2020-03-21 stsp done:
2128 0e4002ca 2020-03-21 stsp free(remote_refname);
2129 161728eb 2021-07-24 stsp return err;
2130 161728eb 2021-07-24 stsp }
2131 161728eb 2021-07-24 stsp
2132 161728eb 2021-07-24 stsp static const struct got_error *
2133 161728eb 2021-07-24 stsp delete_ref(struct got_repository *repo, struct got_reference *ref)
2134 161728eb 2021-07-24 stsp {
2135 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2136 161728eb 2021-07-24 stsp struct got_object_id *id = NULL;
2137 161728eb 2021-07-24 stsp char *id_str = NULL;
2138 161728eb 2021-07-24 stsp const char *target;
2139 161728eb 2021-07-24 stsp
2140 161728eb 2021-07-24 stsp if (got_ref_is_symbolic(ref)) {
2141 161728eb 2021-07-24 stsp target = got_ref_get_symref_target(ref);
2142 161728eb 2021-07-24 stsp } else {
2143 161728eb 2021-07-24 stsp err = got_ref_resolve(&id, repo, ref);
2144 161728eb 2021-07-24 stsp if (err)
2145 161728eb 2021-07-24 stsp goto done;
2146 161728eb 2021-07-24 stsp err = got_object_id_str(&id_str, id);
2147 161728eb 2021-07-24 stsp if (err)
2148 161728eb 2021-07-24 stsp goto done;
2149 161728eb 2021-07-24 stsp target = id_str;
2150 161728eb 2021-07-24 stsp }
2151 161728eb 2021-07-24 stsp
2152 161728eb 2021-07-24 stsp err = got_ref_delete(ref, repo);
2153 161728eb 2021-07-24 stsp if (err)
2154 161728eb 2021-07-24 stsp goto done;
2155 161728eb 2021-07-24 stsp
2156 161728eb 2021-07-24 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2157 161728eb 2021-07-24 stsp done:
2158 161728eb 2021-07-24 stsp free(id);
2159 161728eb 2021-07-24 stsp free(id_str);
2160 f21ec2f0 2020-03-21 stsp return err;
2161 f21ec2f0 2020-03-21 stsp }
2162 f21ec2f0 2020-03-21 stsp
2163 f21ec2f0 2020-03-21 stsp static const struct got_error *
2164 161728eb 2021-07-24 stsp delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2165 161728eb 2021-07-24 stsp {
2166 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2167 161728eb 2021-07-24 stsp struct got_reflist_head refs;
2168 161728eb 2021-07-24 stsp struct got_reflist_entry *re;
2169 161728eb 2021-07-24 stsp char *prefix;
2170 161728eb 2021-07-24 stsp
2171 161728eb 2021-07-24 stsp TAILQ_INIT(&refs);
2172 161728eb 2021-07-24 stsp
2173 161728eb 2021-07-24 stsp if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2174 161728eb 2021-07-24 stsp err = got_error_from_errno("asprintf");
2175 161728eb 2021-07-24 stsp goto done;
2176 161728eb 2021-07-24 stsp }
2177 161728eb 2021-07-24 stsp err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2178 161728eb 2021-07-24 stsp if (err)
2179 161728eb 2021-07-24 stsp goto done;
2180 161728eb 2021-07-24 stsp
2181 161728eb 2021-07-24 stsp TAILQ_FOREACH(re, &refs, entry)
2182 161728eb 2021-07-24 stsp delete_ref(repo, re->ref);
2183 161728eb 2021-07-24 stsp done:
2184 161728eb 2021-07-24 stsp got_ref_list_free(&refs);
2185 161728eb 2021-07-24 stsp return err;
2186 161728eb 2021-07-24 stsp }
2187 161728eb 2021-07-24 stsp
2188 161728eb 2021-07-24 stsp static const struct got_error *
2189 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
2190 7848a0e1 2020-03-19 stsp {
2191 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
2192 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
2193 7848a0e1 2020-03-19 stsp const char *remote_name;
2194 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
2195 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
2196 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
2197 7848a0e1 2020-03-19 stsp int nremotes;
2198 7848a0e1 2020-03-19 stsp char *id_str = NULL;
2199 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
2200 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
2201 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2202 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2203 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
2204 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
2205 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
2206 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
2207 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
2208 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2209 161728eb 2021-07-24 stsp int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2210 7848a0e1 2020-03-19 stsp
2211 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
2212 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
2213 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
2214 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
2215 7848a0e1 2020-03-19 stsp
2216 161728eb 2021-07-24 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2217 7848a0e1 2020-03-19 stsp switch (ch) {
2218 659e7fbd 2020-03-20 stsp case 'a':
2219 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
2220 4ba14133 2020-03-20 stsp break;
2221 4ba14133 2020-03-20 stsp case 'b':
2222 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
2223 4ba14133 2020-03-20 stsp optarg, NULL);
2224 4ba14133 2020-03-20 stsp if (error)
2225 4ba14133 2020-03-20 stsp return error;
2226 41b0de12 2020-03-21 stsp break;
2227 f21ec2f0 2020-03-21 stsp case 'd':
2228 f21ec2f0 2020-03-21 stsp delete_refs = 1;
2229 f21ec2f0 2020-03-21 stsp break;
2230 41b0de12 2020-03-21 stsp case 'l':
2231 41b0de12 2020-03-21 stsp list_refs_only = 1;
2232 659e7fbd 2020-03-20 stsp break;
2233 7848a0e1 2020-03-19 stsp case 'r':
2234 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
2235 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2236 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
2237 7848a0e1 2020-03-19 stsp optarg);
2238 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
2239 7848a0e1 2020-03-19 stsp break;
2240 db6d8ad8 2020-03-21 stsp case 't':
2241 db6d8ad8 2020-03-21 stsp replace_tags = 1;
2242 db6d8ad8 2020-03-21 stsp break;
2243 7848a0e1 2020-03-19 stsp case 'v':
2244 7848a0e1 2020-03-19 stsp if (verbosity < 0)
2245 7848a0e1 2020-03-19 stsp verbosity = 0;
2246 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
2247 7848a0e1 2020-03-19 stsp verbosity++;
2248 7848a0e1 2020-03-19 stsp break;
2249 7848a0e1 2020-03-19 stsp case 'q':
2250 7848a0e1 2020-03-19 stsp verbosity = -1;
2251 7848a0e1 2020-03-19 stsp break;
2252 0e4002ca 2020-03-21 stsp case 'R':
2253 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
2254 0e4002ca 2020-03-21 stsp optarg, NULL);
2255 0e4002ca 2020-03-21 stsp if (error)
2256 0e4002ca 2020-03-21 stsp return error;
2257 0e4002ca 2020-03-21 stsp break;
2258 161728eb 2021-07-24 stsp case 'X':
2259 161728eb 2021-07-24 stsp delete_remote = 1;
2260 161728eb 2021-07-24 stsp break;
2261 7848a0e1 2020-03-19 stsp default:
2262 7848a0e1 2020-03-19 stsp usage_fetch();
2263 7848a0e1 2020-03-19 stsp break;
2264 7848a0e1 2020-03-19 stsp }
2265 7848a0e1 2020-03-19 stsp }
2266 7848a0e1 2020-03-19 stsp argc -= optind;
2267 7848a0e1 2020-03-19 stsp argv += optind;
2268 7848a0e1 2020-03-19 stsp
2269 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2270 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
2271 41b0de12 2020-03-21 stsp if (list_refs_only) {
2272 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2273 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
2274 ff69268e 2020-12-13 stsp if (fetch_all_branches)
2275 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
2276 f21ec2f0 2020-03-21 stsp if (delete_refs)
2277 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
2278 161728eb 2021-07-24 stsp if (delete_remote)
2279 161728eb 2021-07-24 stsp option_conflict('l', 'X');
2280 41b0de12 2020-03-21 stsp }
2281 161728eb 2021-07-24 stsp if (delete_remote) {
2282 161728eb 2021-07-24 stsp if (fetch_all_branches)
2283 161728eb 2021-07-24 stsp option_conflict('X', 'a');
2284 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_branches))
2285 161728eb 2021-07-24 stsp option_conflict('X', 'b');
2286 161728eb 2021-07-24 stsp if (delete_refs)
2287 161728eb 2021-07-24 stsp option_conflict('X', 'd');
2288 161728eb 2021-07-24 stsp if (replace_tags)
2289 161728eb 2021-07-24 stsp option_conflict('X', 't');
2290 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_refs))
2291 161728eb 2021-07-24 stsp option_conflict('X', 'R');
2292 161728eb 2021-07-24 stsp }
2293 161728eb 2021-07-24 stsp
2294 161728eb 2021-07-24 stsp if (argc == 0) {
2295 161728eb 2021-07-24 stsp if (delete_remote)
2296 161728eb 2021-07-24 stsp errx(1, "-X option requires a remote name");
2297 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2298 161728eb 2021-07-24 stsp } else if (argc == 1)
2299 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2300 7848a0e1 2020-03-19 stsp else
2301 7848a0e1 2020-03-19 stsp usage_fetch();
2302 7848a0e1 2020-03-19 stsp
2303 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2304 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2305 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2306 7848a0e1 2020-03-19 stsp goto done;
2307 7848a0e1 2020-03-19 stsp }
2308 7848a0e1 2020-03-19 stsp
2309 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2310 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2311 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2312 7848a0e1 2020-03-19 stsp goto done;
2313 7848a0e1 2020-03-19 stsp else
2314 7848a0e1 2020-03-19 stsp error = NULL;
2315 7848a0e1 2020-03-19 stsp if (worktree) {
2316 7848a0e1 2020-03-19 stsp repo_path =
2317 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2318 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2319 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2320 7848a0e1 2020-03-19 stsp if (error)
2321 7848a0e1 2020-03-19 stsp goto done;
2322 7848a0e1 2020-03-19 stsp } else {
2323 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2324 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2325 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2326 7848a0e1 2020-03-19 stsp goto done;
2327 7848a0e1 2020-03-19 stsp }
2328 7848a0e1 2020-03-19 stsp }
2329 7848a0e1 2020-03-19 stsp }
2330 7848a0e1 2020-03-19 stsp
2331 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2332 7848a0e1 2020-03-19 stsp if (error)
2333 7848a0e1 2020-03-19 stsp goto done;
2334 7848a0e1 2020-03-19 stsp
2335 161728eb 2021-07-24 stsp if (delete_remote) {
2336 161728eb 2021-07-24 stsp error = delete_refs_for_remote(repo, remote_name);
2337 161728eb 2021-07-24 stsp goto done; /* nothing else to do */
2338 161728eb 2021-07-24 stsp }
2339 161728eb 2021-07-24 stsp
2340 50b0790e 2020-09-11 stsp if (worktree) {
2341 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2342 50b0790e 2020-09-11 stsp if (worktree_conf) {
2343 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2344 50b0790e 2020-09-11 stsp worktree_conf);
2345 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2346 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2347 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2348 50b0790e 2020-09-11 stsp break;
2349 54eb00d5 2020-10-20 stsp }
2350 50b0790e 2020-09-11 stsp }
2351 50b0790e 2020-09-11 stsp }
2352 7848a0e1 2020-03-19 stsp }
2353 50b0790e 2020-09-11 stsp if (remote == NULL) {
2354 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2355 50b0790e 2020-09-11 stsp if (repo_conf) {
2356 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2357 50b0790e 2020-09-11 stsp repo_conf);
2358 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2359 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2360 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2361 50b0790e 2020-09-11 stsp break;
2362 54eb00d5 2020-10-20 stsp }
2363 50b0790e 2020-09-11 stsp }
2364 50b0790e 2020-09-11 stsp }
2365 50b0790e 2020-09-11 stsp }
2366 50b0790e 2020-09-11 stsp if (remote == NULL) {
2367 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2368 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2369 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2370 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2371 257add31 2020-09-09 stsp break;
2372 54eb00d5 2020-10-20 stsp }
2373 257add31 2020-09-09 stsp }
2374 7848a0e1 2020-03-19 stsp }
2375 50b0790e 2020-09-11 stsp if (remote == NULL) {
2376 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2377 50b0790e 2020-09-11 stsp goto done;
2378 b8adfa55 2020-09-25 stsp }
2379 b8adfa55 2020-09-25 stsp
2380 0c8b29c5 2021-01-05 stsp if (TAILQ_EMPTY(&wanted_branches)) {
2381 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches)
2382 0c8b29c5 2021-01-05 stsp fetch_all_branches = remote->fetch_all_branches;
2383 6480c871 2021-08-30 stsp for (i = 0; i < remote->nfetch_branches; i++) {
2384 b8adfa55 2020-09-25 stsp got_pathlist_append(&wanted_branches,
2385 6480c871 2021-08-30 stsp remote->fetch_branches[i], NULL);
2386 99495ddb 2021-01-10 stsp }
2387 99495ddb 2021-01-10 stsp }
2388 99495ddb 2021-01-10 stsp if (TAILQ_EMPTY(&wanted_refs)) {
2389 6480c871 2021-08-30 stsp for (i = 0; i < remote->nfetch_refs; i++) {
2390 99495ddb 2021-01-10 stsp got_pathlist_append(&wanted_refs,
2391 6480c871 2021-08-30 stsp remote->fetch_refs[i], NULL);
2392 b8adfa55 2020-09-25 stsp }
2393 50b0790e 2020-09-11 stsp }
2394 7848a0e1 2020-03-19 stsp
2395 5e5da8c4 2021-09-05 stsp error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2396 6480c871 2021-08-30 stsp &repo_name, remote->fetch_url);
2397 7848a0e1 2020-03-19 stsp if (error)
2398 7848a0e1 2020-03-19 stsp goto done;
2399 7848a0e1 2020-03-19 stsp
2400 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2401 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2402 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2403 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2404 7848a0e1 2020-03-19 stsp err(1, "pledge");
2405 7848a0e1 2020-03-19 stsp #endif
2406 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2407 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2408 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2409 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2410 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2411 7848a0e1 2020-03-19 stsp err(1, "pledge");
2412 7848a0e1 2020-03-19 stsp #endif
2413 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2414 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2415 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2416 7848a0e1 2020-03-19 stsp goto done;
2417 7848a0e1 2020-03-19 stsp } else {
2418 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2419 7848a0e1 2020-03-19 stsp goto done;
2420 7848a0e1 2020-03-19 stsp }
2421 7848a0e1 2020-03-19 stsp
2422 d65a88a2 2021-09-05 stsp error = got_dial_apply_unveil(proto);
2423 d65a88a2 2021-09-05 stsp if (error)
2424 d65a88a2 2021-09-05 stsp goto done;
2425 d65a88a2 2021-09-05 stsp
2426 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2427 7848a0e1 2020-03-19 stsp if (error)
2428 7848a0e1 2020-03-19 stsp goto done;
2429 f79e6490 2020-04-19 stsp
2430 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2431 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2432 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2433 7848a0e1 2020-03-19 stsp
2434 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2435 9c52365f 2020-03-21 stsp server_path, verbosity);
2436 7848a0e1 2020-03-19 stsp if (error)
2437 7848a0e1 2020-03-19 stsp goto done;
2438 7848a0e1 2020-03-19 stsp
2439 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2440 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2441 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2442 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2443 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2444 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2445 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2446 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2447 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2448 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2449 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2450 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2451 7848a0e1 2020-03-19 stsp if (error)
2452 7848a0e1 2020-03-19 stsp goto done;
2453 7848a0e1 2020-03-19 stsp
2454 41b0de12 2020-03-21 stsp if (list_refs_only) {
2455 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2456 41b0de12 2020-03-21 stsp goto done;
2457 41b0de12 2020-03-21 stsp }
2458 41b0de12 2020-03-21 stsp
2459 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2460 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2461 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2462 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2463 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2464 984065c8 2020-03-19 stsp if (error)
2465 984065c8 2020-03-19 stsp goto done;
2466 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2467 984065c8 2020-03-19 stsp free(id_str);
2468 984065c8 2020-03-19 stsp id_str = NULL;
2469 984065c8 2020-03-19 stsp }
2470 7848a0e1 2020-03-19 stsp
2471 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2472 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2473 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2474 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2475 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2476 7848a0e1 2020-03-19 stsp char *remote_refname;
2477 7848a0e1 2020-03-19 stsp
2478 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2479 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2480 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2481 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2482 0e4002ca 2020-03-21 stsp if (error)
2483 0e4002ca 2020-03-21 stsp goto done;
2484 0e4002ca 2020-03-21 stsp continue;
2485 0e4002ca 2020-03-21 stsp }
2486 0e4002ca 2020-03-21 stsp
2487 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2488 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2489 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2490 7848a0e1 2020-03-19 stsp if (error) {
2491 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2492 7848a0e1 2020-03-19 stsp goto done;
2493 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2494 6338a6a1 2020-03-21 stsp repo);
2495 7848a0e1 2020-03-19 stsp if (error)
2496 7848a0e1 2020-03-19 stsp goto done;
2497 7848a0e1 2020-03-19 stsp } else {
2498 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2499 db6d8ad8 2020-03-21 stsp verbosity, repo);
2500 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2501 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2502 9f142382 2020-03-21 stsp error = unlock_err;
2503 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2504 7848a0e1 2020-03-19 stsp if (error)
2505 7848a0e1 2020-03-19 stsp goto done;
2506 7848a0e1 2020-03-19 stsp }
2507 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2508 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2509 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2510 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2511 7848a0e1 2020-03-19 stsp goto done;
2512 7848a0e1 2020-03-19 stsp }
2513 7848a0e1 2020-03-19 stsp
2514 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2515 7848a0e1 2020-03-19 stsp if (error) {
2516 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2517 7848a0e1 2020-03-19 stsp goto done;
2518 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2519 688f11b3 2020-03-21 stsp verbosity, repo);
2520 7848a0e1 2020-03-19 stsp if (error)
2521 7848a0e1 2020-03-19 stsp goto done;
2522 7848a0e1 2020-03-19 stsp } else {
2523 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2524 db6d8ad8 2020-03-21 stsp verbosity, repo);
2525 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2526 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2527 9f142382 2020-03-21 stsp error = unlock_err;
2528 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2529 7848a0e1 2020-03-19 stsp if (error)
2530 7848a0e1 2020-03-19 stsp goto done;
2531 7848a0e1 2020-03-19 stsp }
2532 2ec30c80 2020-03-20 stsp
2533 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2534 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2535 2ec30c80 2020-03-20 stsp if (error) {
2536 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2537 2ec30c80 2020-03-20 stsp goto done;
2538 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2539 6338a6a1 2020-03-21 stsp repo);
2540 2ec30c80 2020-03-20 stsp if (error)
2541 2ec30c80 2020-03-20 stsp goto done;
2542 9f142382 2020-03-21 stsp } else {
2543 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2544 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2545 9f142382 2020-03-21 stsp error = unlock_err;
2546 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2547 9f142382 2020-03-21 stsp }
2548 7848a0e1 2020-03-19 stsp }
2549 7848a0e1 2020-03-19 stsp }
2550 f1bcca34 2020-03-25 stsp if (delete_refs) {
2551 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2552 3789fd73 2020-03-26 stsp verbosity, repo);
2553 f1bcca34 2020-03-25 stsp if (error)
2554 f1bcca34 2020-03-25 stsp goto done;
2555 f1bcca34 2020-03-25 stsp }
2556 f1bcca34 2020-03-25 stsp
2557 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2558 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2559 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2560 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2561 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2562 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2563 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2564 f1bcca34 2020-03-25 stsp
2565 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2566 f1bcca34 2020-03-25 stsp continue;
2567 f1bcca34 2020-03-25 stsp
2568 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2569 f1bcca34 2020-03-25 stsp continue;
2570 f1bcca34 2020-03-25 stsp
2571 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2572 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2573 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2574 f1bcca34 2020-03-25 stsp goto done;
2575 f1bcca34 2020-03-25 stsp }
2576 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2577 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2578 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2579 f1bcca34 2020-03-25 stsp free(remote_refname);
2580 f1bcca34 2020-03-25 stsp goto done;
2581 f1bcca34 2020-03-25 stsp }
2582 f1bcca34 2020-03-25 stsp
2583 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2584 f1bcca34 2020-03-25 stsp 0);
2585 f1bcca34 2020-03-25 stsp if (error) {
2586 f1bcca34 2020-03-25 stsp free(remote_refname);
2587 f1bcca34 2020-03-25 stsp free(remote_target);
2588 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2589 f1bcca34 2020-03-25 stsp error = NULL;
2590 f1bcca34 2020-03-25 stsp continue;
2591 f1bcca34 2020-03-25 stsp }
2592 f1bcca34 2020-03-25 stsp goto done;
2593 f1bcca34 2020-03-25 stsp }
2594 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2595 f1bcca34 2020-03-25 stsp verbosity, repo);
2596 f1bcca34 2020-03-25 stsp free(remote_refname);
2597 f1bcca34 2020-03-25 stsp free(remote_target);
2598 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2599 f1bcca34 2020-03-25 stsp if (error)
2600 f1bcca34 2020-03-25 stsp goto done;
2601 f1bcca34 2020-03-25 stsp }
2602 f1bcca34 2020-03-25 stsp }
2603 7848a0e1 2020-03-19 stsp done:
2604 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2605 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2606 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2607 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2608 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2609 9c52365f 2020-03-21 stsp }
2610 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2611 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2612 1d0f4054 2021-06-17 stsp if (repo) {
2613 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2614 1d0f4054 2021-06-17 stsp if (error == NULL)
2615 1d0f4054 2021-06-17 stsp error = close_err;
2616 1d0f4054 2021-06-17 stsp }
2617 7848a0e1 2020-03-19 stsp if (worktree)
2618 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2619 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2620 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2621 7848a0e1 2020-03-19 stsp free(pe->data);
2622 7848a0e1 2020-03-19 stsp }
2623 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2624 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2625 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2626 7848a0e1 2020-03-19 stsp free(pe->data);
2627 7848a0e1 2020-03-19 stsp }
2628 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2629 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2630 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2631 7848a0e1 2020-03-19 stsp free(id_str);
2632 7848a0e1 2020-03-19 stsp free(cwd);
2633 7848a0e1 2020-03-19 stsp free(repo_path);
2634 7848a0e1 2020-03-19 stsp free(pack_hash);
2635 7848a0e1 2020-03-19 stsp free(proto);
2636 7848a0e1 2020-03-19 stsp free(host);
2637 7848a0e1 2020-03-19 stsp free(port);
2638 7848a0e1 2020-03-19 stsp free(server_path);
2639 7848a0e1 2020-03-19 stsp free(repo_name);
2640 7848a0e1 2020-03-19 stsp return error;
2641 7848a0e1 2020-03-19 stsp }
2642 7848a0e1 2020-03-19 stsp
2643 7848a0e1 2020-03-19 stsp
2644 7848a0e1 2020-03-19 stsp __dead static void
2645 2ab43947 2020-03-18 stsp usage_checkout(void)
2646 2ab43947 2020-03-18 stsp {
2647 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2648 4ad4a1ec 2021-09-13 tracey "[-p prefix] [-q] repository-path [worktree-path]\n",
2649 4ad4a1ec 2021-09-13 tracey getprogname());
2650 2ab43947 2020-03-18 stsp exit(1);
2651 2ab43947 2020-03-18 stsp }
2652 2ab43947 2020-03-18 stsp
2653 2ab43947 2020-03-18 stsp static void
2654 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2655 2ab43947 2020-03-18 stsp {
2656 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2657 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2658 e6786710 2021-07-03 stsp "garbage-collected by Git or 'gotadmin cleanup'; making the "
2659 e6786710 2021-07-03 stsp "repository writable and running 'got update' will prevent this\n",
2660 2ab43947 2020-03-18 stsp getprogname());
2661 2ab43947 2020-03-18 stsp }
2662 2ab43947 2020-03-18 stsp
2663 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2664 2ab43947 2020-03-18 stsp const char *worktree_path;
2665 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2666 4ad4a1ec 2021-09-13 tracey int verbosity;
2667 2ab43947 2020-03-18 stsp };
2668 2ab43947 2020-03-18 stsp
2669 2ab43947 2020-03-18 stsp static const struct got_error *
2670 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2671 2ab43947 2020-03-18 stsp {
2672 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2673 2ab43947 2020-03-18 stsp
2674 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2675 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2676 2ab43947 2020-03-18 stsp return NULL;
2677 2ab43947 2020-03-18 stsp
2678 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2679 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2680 2ab43947 2020-03-18 stsp return NULL;
2681 2ab43947 2020-03-18 stsp }
2682 2ab43947 2020-03-18 stsp
2683 2ab43947 2020-03-18 stsp while (path[0] == '/')
2684 2ab43947 2020-03-18 stsp path++;
2685 2ab43947 2020-03-18 stsp
2686 4ad4a1ec 2021-09-13 tracey if (a->verbosity >= 0)
2687 4ad4a1ec 2021-09-13 tracey printf("%c %s/%s\n", status, a->worktree_path, path);
2688 4ad4a1ec 2021-09-13 tracey
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_cancelled(void *arg)
2694 2ab43947 2020-03-18 stsp {
2695 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2696 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2697 2ab43947 2020-03-18 stsp return NULL;
2698 2ab43947 2020-03-18 stsp }
2699 2ab43947 2020-03-18 stsp
2700 2ab43947 2020-03-18 stsp static const struct got_error *
2701 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2702 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2703 2ab43947 2020-03-18 stsp struct got_repository *repo)
2704 2ab43947 2020-03-18 stsp {
2705 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2706 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2707 2ab43947 2020-03-18 stsp
2708 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2709 768705e3 2021-09-27 thomas commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2710 2ab43947 2020-03-18 stsp if (err)
2711 2ab43947 2020-03-18 stsp return err;
2712 2ab43947 2020-03-18 stsp
2713 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2714 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2715 2ab43947 2020-03-18 stsp
2716 2ab43947 2020-03-18 stsp /*
2717 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2718 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2719 2ab43947 2020-03-18 stsp *
2720 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2721 2ab43947 2020-03-18 stsp *
2722 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2723 2ab43947 2020-03-18 stsp * \ /
2724 2ab43947 2020-03-18 stsp * C E
2725 2ab43947 2020-03-18 stsp * \ /
2726 2ab43947 2020-03-18 stsp * B (yca)
2727 2ab43947 2020-03-18 stsp * |
2728 2ab43947 2020-03-18 stsp * A
2729 2ab43947 2020-03-18 stsp *
2730 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2731 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2732 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2733 2ab43947 2020-03-18 stsp */
2734 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2735 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2736 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2737 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2738 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2739 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2740 2ab43947 2020-03-18 stsp
2741 2ab43947 2020-03-18 stsp free(yca_id);
2742 2ab43947 2020-03-18 stsp return NULL;
2743 2ab43947 2020-03-18 stsp }
2744 2ab43947 2020-03-18 stsp
2745 2ab43947 2020-03-18 stsp static const struct got_error *
2746 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2747 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2748 2ab43947 2020-03-18 stsp struct got_repository *repo)
2749 2ab43947 2020-03-18 stsp {
2750 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2751 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2752 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2753 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2754 2ab43947 2020-03-18 stsp
2755 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2756 2ab43947 2020-03-18 stsp if (err)
2757 2ab43947 2020-03-18 stsp goto done;
2758 2ab43947 2020-03-18 stsp
2759 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2760 2ab43947 2020-03-18 stsp is_same_branch = 1;
2761 2ab43947 2020-03-18 stsp goto done;
2762 2ab43947 2020-03-18 stsp }
2763 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2764 2ab43947 2020-03-18 stsp is_same_branch = 1;
2765 2ab43947 2020-03-18 stsp goto done;
2766 2ab43947 2020-03-18 stsp }
2767 2ab43947 2020-03-18 stsp
2768 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2769 2ab43947 2020-03-18 stsp if (err)
2770 2ab43947 2020-03-18 stsp goto done;
2771 2ab43947 2020-03-18 stsp
2772 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2773 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2774 2ab43947 2020-03-18 stsp if (err)
2775 2ab43947 2020-03-18 stsp goto done;
2776 2ab43947 2020-03-18 stsp
2777 2ab43947 2020-03-18 stsp for (;;) {
2778 2ab43947 2020-03-18 stsp struct got_object_id *id;
2779 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2780 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2781 2ab43947 2020-03-18 stsp if (err) {
2782 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2783 2ab43947 2020-03-18 stsp err = NULL;
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 if (id) {
2788 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2789 2ab43947 2020-03-18 stsp break;
2790 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2791 2ab43947 2020-03-18 stsp is_same_branch = 1;
2792 2ab43947 2020-03-18 stsp break;
2793 2ab43947 2020-03-18 stsp }
2794 2ab43947 2020-03-18 stsp }
2795 2ab43947 2020-03-18 stsp }
2796 2ab43947 2020-03-18 stsp done:
2797 2ab43947 2020-03-18 stsp if (graph)
2798 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2799 2ab43947 2020-03-18 stsp free(head_commit_id);
2800 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2801 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2802 2ab43947 2020-03-18 stsp return err;
2803 a367ff0f 2019-05-14 stsp }
2804 8069f636 2019-01-12 stsp
2805 4ed7e80c 2018-05-20 stsp static const struct got_error *
2806 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2807 4b6c9460 2020-03-05 stsp {
2808 4b6c9460 2020-03-05 stsp static char msg[512];
2809 4b6c9460 2020-03-05 stsp const char *branch_name;
2810 4b6c9460 2020-03-05 stsp
2811 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2812 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2813 4b6c9460 2020-03-05 stsp else
2814 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2815 4b6c9460 2020-03-05 stsp
2816 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2817 4b6c9460 2020-03-05 stsp branch_name += 11;
2818 4b6c9460 2020-03-05 stsp
2819 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2820 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2821 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2822 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2823 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2824 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2825 4b6c9460 2020-03-05 stsp
2826 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2827 4b6c9460 2020-03-05 stsp }
2828 4b6c9460 2020-03-05 stsp
2829 4b6c9460 2020-03-05 stsp static const struct got_error *
2830 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2831 c09a553d 2018-03-12 stsp {
2832 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2833 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2834 08e5873e 2021-09-14 stsp struct got_reference *head_ref = NULL, *ref = NULL;
2835 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2836 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2837 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2838 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2839 08e5873e 2021-09-14 stsp const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2840 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2841 08e5873e 2021-09-14 stsp struct got_object_id *commit_id = NULL;
2842 f0207f6a 2020-10-19 stsp char *cwd = NULL;
2843 4ad4a1ec 2021-09-13 tracey int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2844 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2845 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2846 f2ea84fa 2019-07-27 stsp
2847 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2848 c09a553d 2018-03-12 stsp
2849 4ad4a1ec 2021-09-13 tracey while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2850 0bb8a95e 2018-03-12 stsp switch (ch) {
2851 08573d5b 2019-05-14 stsp case 'b':
2852 08573d5b 2019-05-14 stsp branch_name = optarg;
2853 08573d5b 2019-05-14 stsp break;
2854 8069f636 2019-01-12 stsp case 'c':
2855 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2856 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2857 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2858 bb51a5b4 2020-01-13 stsp break;
2859 bb51a5b4 2020-01-13 stsp case 'E':
2860 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2861 8069f636 2019-01-12 stsp break;
2862 0bb8a95e 2018-03-12 stsp case 'p':
2863 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2864 0bb8a95e 2018-03-12 stsp break;
2865 4ad4a1ec 2021-09-13 tracey case 'q':
2866 4ad4a1ec 2021-09-13 tracey verbosity = -1;
2867 4ad4a1ec 2021-09-13 tracey break;
2868 0bb8a95e 2018-03-12 stsp default:
2869 2deda0b9 2019-03-07 stsp usage_checkout();
2870 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2871 0bb8a95e 2018-03-12 stsp }
2872 0bb8a95e 2018-03-12 stsp }
2873 0bb8a95e 2018-03-12 stsp
2874 0bb8a95e 2018-03-12 stsp argc -= optind;
2875 0bb8a95e 2018-03-12 stsp argv += optind;
2876 0bb8a95e 2018-03-12 stsp
2877 6715a751 2018-03-16 stsp #ifndef PROFILE
2878 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2879 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2880 c09a553d 2018-03-12 stsp err(1, "pledge");
2881 6715a751 2018-03-16 stsp #endif
2882 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2883 c47340da 2020-09-20 stsp char *base, *dotgit;
2884 f0207f6a 2020-10-19 stsp const char *path;
2885 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2886 76089277 2018-04-01 stsp if (repo_path == NULL)
2887 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2888 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2889 76089277 2018-04-01 stsp if (cwd == NULL) {
2890 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2891 76089277 2018-04-01 stsp goto done;
2892 76089277 2018-04-01 stsp }
2893 c47340da 2020-09-20 stsp if (path_prefix[0])
2894 f0207f6a 2020-10-19 stsp path = path_prefix;
2895 c47340da 2020-09-20 stsp else
2896 f0207f6a 2020-10-19 stsp path = repo_path;
2897 f0207f6a 2020-10-19 stsp error = got_path_basename(&base, path);
2898 f0207f6a 2020-10-19 stsp if (error)
2899 c47340da 2020-09-20 stsp goto done;
2900 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2901 c09a553d 2018-03-12 stsp if (dotgit)
2902 c09a553d 2018-03-12 stsp *dotgit = '\0';
2903 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2904 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2905 f0207f6a 2020-10-19 stsp free(base);
2906 76089277 2018-04-01 stsp goto done;
2907 c09a553d 2018-03-12 stsp }
2908 f0207f6a 2020-10-19 stsp free(base);
2909 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2910 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2911 76089277 2018-04-01 stsp if (repo_path == NULL) {
2912 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2913 76089277 2018-04-01 stsp goto done;
2914 76089277 2018-04-01 stsp }
2915 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2916 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2917 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2918 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2919 b4b3a7dd 2019-07-22 stsp argv[1]);
2920 b4b3a7dd 2019-07-22 stsp goto done;
2921 b4b3a7dd 2019-07-22 stsp }
2922 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2923 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2924 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2925 b4b3a7dd 2019-07-22 stsp goto done;
2926 b4b3a7dd 2019-07-22 stsp }
2927 76089277 2018-04-01 stsp }
2928 c09a553d 2018-03-12 stsp } else
2929 c09a553d 2018-03-12 stsp usage_checkout();
2930 c09a553d 2018-03-12 stsp
2931 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2932 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2933 13bfb272 2019-05-10 jcs
2934 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2935 c09a553d 2018-03-12 stsp if (error != NULL)
2936 c02c541e 2019-03-29 stsp goto done;
2937 c02c541e 2019-03-29 stsp
2938 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2939 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2940 c530dc23 2019-07-23 stsp if (error) {
2941 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2942 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2943 c530dc23 2019-07-23 stsp goto done;
2944 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2945 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2946 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2947 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2948 c530dc23 2019-07-23 stsp goto done;
2949 c530dc23 2019-07-23 stsp }
2950 c530dc23 2019-07-23 stsp }
2951 c530dc23 2019-07-23 stsp
2952 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2953 c02c541e 2019-03-29 stsp if (error)
2954 c09a553d 2018-03-12 stsp goto done;
2955 8069f636 2019-01-12 stsp
2956 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2957 c09a553d 2018-03-12 stsp if (error != NULL)
2958 c09a553d 2018-03-12 stsp goto done;
2959 c09a553d 2018-03-12 stsp
2960 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2961 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2962 c09a553d 2018-03-12 stsp goto done;
2963 c09a553d 2018-03-12 stsp
2964 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2965 c09a553d 2018-03-12 stsp if (error != NULL)
2966 c09a553d 2018-03-12 stsp goto done;
2967 c09a553d 2018-03-12 stsp
2968 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2969 e5dc7198 2018-12-29 stsp path_prefix);
2970 e5dc7198 2018-12-29 stsp if (error != NULL)
2971 e5dc7198 2018-12-29 stsp goto done;
2972 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2973 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2974 49520a32 2018-12-29 stsp goto done;
2975 49520a32 2018-12-29 stsp }
2976 49520a32 2018-12-29 stsp
2977 8069f636 2019-01-12 stsp if (commit_id_str) {
2978 84de9106 2020-12-26 stsp struct got_reflist_head refs;
2979 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
2980 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2981 84de9106 2020-12-26 stsp NULL);
2982 84de9106 2020-12-26 stsp if (error)
2983 84de9106 2020-12-26 stsp goto done;
2984 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2985 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2986 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
2987 30837e32 2019-07-25 stsp if (error)
2988 8069f636 2019-01-12 stsp goto done;
2989 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2990 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2991 8069f636 2019-01-12 stsp if (error != NULL) {
2992 8069f636 2019-01-12 stsp free(commit_id);
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 8069f636 2019-01-12 stsp goto done;
2998 8069f636 2019-01-12 stsp }
2999 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3000 4b6c9460 2020-03-05 stsp if (error) {
3001 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
3002 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
3003 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
3004 4b6c9460 2020-03-05 stsp }
3005 45d344f6 2019-05-14 stsp goto done;
3006 4b6c9460 2020-03-05 stsp }
3007 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3008 8069f636 2019-01-12 stsp commit_id);
3009 8069f636 2019-01-12 stsp if (error)
3010 8069f636 2019-01-12 stsp goto done;
3011 08e5873e 2021-09-14 stsp /* Expand potentially abbreviated commit ID string. */
3012 08e5873e 2021-09-14 stsp free(commit_id_str);
3013 08e5873e 2021-09-14 stsp error = got_object_id_str(&commit_id_str, commit_id);
3014 08e5873e 2021-09-14 stsp if (error)
3015 08e5873e 2021-09-14 stsp goto done;
3016 08e5873e 2021-09-14 stsp } else {
3017 08e5873e 2021-09-14 stsp commit_id = got_object_id_dup(
3018 08e5873e 2021-09-14 stsp got_worktree_get_base_commit_id(worktree));
3019 08e5873e 2021-09-14 stsp if (commit_id == NULL) {
3020 08e5873e 2021-09-14 stsp error = got_error_from_errno("got_object_id_dup");
3021 08e5873e 2021-09-14 stsp goto done;
3022 08e5873e 2021-09-14 stsp }
3023 08e5873e 2021-09-14 stsp error = got_object_id_str(&commit_id_str, commit_id);
3024 08e5873e 2021-09-14 stsp if (error)
3025 08e5873e 2021-09-14 stsp goto done;
3026 8069f636 2019-01-12 stsp }
3027 8069f636 2019-01-12 stsp
3028 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
3029 f2ea84fa 2019-07-27 stsp if (error)
3030 f2ea84fa 2019-07-27 stsp goto done;
3031 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
3032 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
3033 4ad4a1ec 2021-09-13 tracey cpa.verbosity = verbosity;
3034 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3035 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
3036 c09a553d 2018-03-12 stsp if (error != NULL)
3037 c09a553d 2018-03-12 stsp goto done;
3038 c09a553d 2018-03-12 stsp
3039 08e5873e 2021-09-14 stsp if (got_ref_is_symbolic(head_ref)) {
3040 08e5873e 2021-09-14 stsp error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3041 08e5873e 2021-09-14 stsp if (error)
3042 08e5873e 2021-09-14 stsp goto done;
3043 08e5873e 2021-09-14 stsp refname = got_ref_get_name(ref);
3044 08e5873e 2021-09-14 stsp } else
3045 08e5873e 2021-09-14 stsp refname = got_ref_get_name(head_ref);
3046 08e5873e 2021-09-14 stsp printf("Checked out %s: %s\n", refname, commit_id_str);
3047 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
3048 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
3049 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
3050 c09a553d 2018-03-12 stsp done:
3051 08e5873e 2021-09-14 stsp if (head_ref)
3052 08e5873e 2021-09-14 stsp got_ref_close(head_ref);
3053 08e5873e 2021-09-14 stsp if (ref)
3054 08e5873e 2021-09-14 stsp got_ref_close(ref);
3055 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3056 8069f636 2019-01-12 stsp free(commit_id_str);
3057 08e5873e 2021-09-14 stsp free(commit_id);
3058 76089277 2018-04-01 stsp free(repo_path);
3059 507dc3bb 2018-12-29 stsp free(worktree_path);
3060 c47340da 2020-09-20 stsp free(cwd);
3061 507dc3bb 2018-12-29 stsp return error;
3062 9627c110 2020-04-18 stsp }
3063 9627c110 2020-04-18 stsp
3064 9627c110 2020-04-18 stsp struct got_update_progress_arg {
3065 9627c110 2020-04-18 stsp int did_something;
3066 9627c110 2020-04-18 stsp int conflicts;
3067 9627c110 2020-04-18 stsp int obstructed;
3068 9627c110 2020-04-18 stsp int not_updated;
3069 10604dce 2021-09-24 thomas int missing;
3070 0ef68859 2021-09-28 thomas int not_deleted;
3071 0ef68859 2021-09-28 thomas int unversioned;
3072 4ad4a1ec 2021-09-13 tracey int verbosity;
3073 9627c110 2020-04-18 stsp };
3074 9627c110 2020-04-18 stsp
3075 9627c110 2020-04-18 stsp void
3076 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
3077 9627c110 2020-04-18 stsp {
3078 9627c110 2020-04-18 stsp if (!upa->did_something)
3079 9627c110 2020-04-18 stsp return;
3080 9627c110 2020-04-18 stsp
3081 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
3082 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
3083 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
3084 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
3085 9627c110 2020-04-18 stsp upa->obstructed);
3086 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
3087 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
3088 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
3089 507dc3bb 2018-12-29 stsp }
3090 507dc3bb 2018-12-29 stsp
3091 0ef68859 2021-09-28 thomas /*
3092 0ef68859 2021-09-28 thomas * The meaning of some status codes differs between merge-style operations and
3093 0ef68859 2021-09-28 thomas * update operations. For example, the ! status code means "file was missing"
3094 0ef68859 2021-09-28 thomas * if changes were merged into the work tree, and "missing file was restored"
3095 0ef68859 2021-09-28 thomas * if the work tree was updated. This function should be used by any operation
3096 0ef68859 2021-09-28 thomas * which merges changes into the work tree without updating the work tree.
3097 0ef68859 2021-09-28 thomas */
3098 0ef68859 2021-09-28 thomas void
3099 0ef68859 2021-09-28 thomas print_merge_progress_stats(struct got_update_progress_arg *upa)
3100 0ef68859 2021-09-28 thomas {
3101 0ef68859 2021-09-28 thomas if (!upa->did_something)
3102 0ef68859 2021-09-28 thomas return;
3103 0ef68859 2021-09-28 thomas
3104 0ef68859 2021-09-28 thomas if (upa->conflicts > 0)
3105 0ef68859 2021-09-28 thomas printf("Files with new merge conflicts: %d\n", upa->conflicts);
3106 0ef68859 2021-09-28 thomas if (upa->obstructed > 0)
3107 0ef68859 2021-09-28 thomas printf("File paths obstructed by a non-regular file: %d\n",
3108 0ef68859 2021-09-28 thomas upa->obstructed);
3109 0ef68859 2021-09-28 thomas if (upa->missing > 0)
3110 0ef68859 2021-09-28 thomas printf("Files which had incoming changes but could not be "
3111 0ef68859 2021-09-28 thomas "found in the work tree: %d\n", upa->missing);
3112 0ef68859 2021-09-28 thomas if (upa->not_deleted > 0)
3113 0ef68859 2021-09-28 thomas printf("Files not deleted due to differences in deleted "
3114 0ef68859 2021-09-28 thomas "content: %d\n", upa->not_deleted);
3115 0ef68859 2021-09-28 thomas if (upa->unversioned > 0)
3116 0ef68859 2021-09-28 thomas printf("Files not merged because an unversioned file was "
3117 0ef68859 2021-09-28 thomas "found in the work tree: %d\n", upa->unversioned);
3118 0ef68859 2021-09-28 thomas }
3119 0ef68859 2021-09-28 thomas
3120 507dc3bb 2018-12-29 stsp __dead static void
3121 507dc3bb 2018-12-29 stsp usage_update(void)
3122 507dc3bb 2018-12-29 stsp {
3123 4ad4a1ec 2021-09-13 tracey fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3124 4ad4a1ec 2021-09-13 tracey "[path ...]\n",
3125 507dc3bb 2018-12-29 stsp getprogname());
3126 507dc3bb 2018-12-29 stsp exit(1);
3127 507dc3bb 2018-12-29 stsp }
3128 507dc3bb 2018-12-29 stsp
3129 1ee397ad 2019-07-12 stsp static const struct got_error *
3130 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
3131 507dc3bb 2018-12-29 stsp {
3132 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
3133 784955db 2019-01-12 stsp
3134 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
3135 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
3136 1ee397ad 2019-07-12 stsp return NULL;
3137 507dc3bb 2018-12-29 stsp
3138 9627c110 2020-04-18 stsp upa->did_something = 1;
3139 a484d721 2019-06-10 stsp
3140 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
3141 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
3142 1ee397ad 2019-07-12 stsp return NULL;
3143 a484d721 2019-06-10 stsp
3144 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
3145 9627c110 2020-04-18 stsp upa->conflicts++;
3146 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
3147 9627c110 2020-04-18 stsp upa->obstructed++;
3148 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
3149 9627c110 2020-04-18 stsp upa->not_updated++;
3150 10604dce 2021-09-24 thomas if (status == GOT_STATUS_MISSING)
3151 10604dce 2021-09-24 thomas upa->missing++;
3152 0ef68859 2021-09-28 thomas if (status == GOT_STATUS_CANNOT_DELETE)
3153 0ef68859 2021-09-28 thomas upa->not_deleted++;
3154 0ef68859 2021-09-28 thomas if (status == GOT_STATUS_UNVERSIONED)
3155 0ef68859 2021-09-28 thomas upa->unversioned++;
3156 9627c110 2020-04-18 stsp
3157 507dc3bb 2018-12-29 stsp while (path[0] == '/')
3158 507dc3bb 2018-12-29 stsp path++;
3159 4ad4a1ec 2021-09-13 tracey if (upa->verbosity >= 0)
3160 4ad4a1ec 2021-09-13 tracey printf("%c %s\n", status, path);
3161 4ad4a1ec 2021-09-13 tracey
3162 1ee397ad 2019-07-12 stsp return NULL;
3163 be7061eb 2018-12-30 stsp }
3164 be7061eb 2018-12-30 stsp
3165 be7061eb 2018-12-30 stsp static const struct got_error *
3166 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
3167 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
3168 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
3169 a1fb16d8 2019-05-24 stsp {
3170 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
3171 a1fb16d8 2019-05-24 stsp char *base_id_str;
3172 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
3173 a1fb16d8 2019-05-24 stsp
3174 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
3175 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
3176 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
3177 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
3178 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
3179 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
3180 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
3181 a1fb16d8 2019-05-24 stsp }
3182 a1fb16d8 2019-05-24 stsp
3183 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
3184 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3185 a1fb16d8 2019-05-24 stsp if (err) {
3186 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
3187 a1fb16d8 2019-05-24 stsp return err;
3188 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
3189 a1fb16d8 2019-05-24 stsp }
3190 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
3191 a1fb16d8 2019-05-24 stsp return NULL;
3192 a1fb16d8 2019-05-24 stsp
3193 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
3194 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
3195 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
3196 a1fb16d8 2019-05-24 stsp if (err)
3197 a1fb16d8 2019-05-24 stsp return err;
3198 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
3199 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3200 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
3201 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3202 0ebf8283 2019-07-24 stsp return NULL;
3203 0ebf8283 2019-07-24 stsp }
3204 0ebf8283 2019-07-24 stsp
3205 0ebf8283 2019-07-24 stsp static const struct got_error *
3206 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3207 0ebf8283 2019-07-24 stsp {
3208 0ebf8283 2019-07-24 stsp const struct got_error *err;
3209 0ebf8283 2019-07-24 stsp int in_progress;
3210 0ebf8283 2019-07-24 stsp
3211 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
3212 0ebf8283 2019-07-24 stsp if (err)
3213 0ebf8283 2019-07-24 stsp return err;
3214 0ebf8283 2019-07-24 stsp if (in_progress)
3215 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
3216 0ebf8283 2019-07-24 stsp
3217 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
3218 0ebf8283 2019-07-24 stsp if (err)
3219 0ebf8283 2019-07-24 stsp return err;
3220 0ebf8283 2019-07-24 stsp if (in_progress)
3221 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
3222 10604dce 2021-09-24 thomas
3223 10604dce 2021-09-24 thomas return NULL;
3224 10604dce 2021-09-24 thomas }
3225 10604dce 2021-09-24 thomas
3226 10604dce 2021-09-24 thomas static const struct got_error *
3227 10604dce 2021-09-24 thomas check_merge_in_progress(struct got_worktree *worktree,
3228 10604dce 2021-09-24 thomas struct got_repository *repo)
3229 10604dce 2021-09-24 thomas {
3230 10604dce 2021-09-24 thomas const struct got_error *err;
3231 10604dce 2021-09-24 thomas int in_progress;
3232 10604dce 2021-09-24 thomas
3233 10604dce 2021-09-24 thomas err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3234 10604dce 2021-09-24 thomas if (err)
3235 10604dce 2021-09-24 thomas return err;
3236 10604dce 2021-09-24 thomas if (in_progress)
3237 10604dce 2021-09-24 thomas return got_error(GOT_ERR_MERGE_BUSY);
3238 0ebf8283 2019-07-24 stsp
3239 a1fb16d8 2019-05-24 stsp return NULL;
3240 a5edda0a 2019-07-27 stsp }
3241 a5edda0a 2019-07-27 stsp
3242 a5edda0a 2019-07-27 stsp static const struct got_error *
3243 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3244 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
3245 a5edda0a 2019-07-27 stsp {
3246 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
3247 a5edda0a 2019-07-27 stsp char *path;
3248 f1417e9f 2021-10-12 thomas struct got_pathlist_entry *new;
3249 a5edda0a 2019-07-27 stsp int i;
3250 a5edda0a 2019-07-27 stsp
3251 a5edda0a 2019-07-27 stsp if (argc == 0) {
3252 a5edda0a 2019-07-27 stsp path = strdup("");
3253 a5edda0a 2019-07-27 stsp if (path == NULL)
3254 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
3255 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
3256 a5edda0a 2019-07-27 stsp }
3257 a5edda0a 2019-07-27 stsp
3258 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
3259 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
3260 a5edda0a 2019-07-27 stsp if (err)
3261 a5edda0a 2019-07-27 stsp break;
3262 f1417e9f 2021-10-12 thomas err = got_pathlist_insert(&new, paths, path, NULL);
3263 f1417e9f 2021-10-12 thomas if (err || new == NULL /* duplicate */) {
3264 a5edda0a 2019-07-27 stsp free(path);
3265 f1417e9f 2021-10-12 thomas if (err)
3266 f1417e9f 2021-10-12 thomas break;
3267 a5edda0a 2019-07-27 stsp }
3268 a5edda0a 2019-07-27 stsp }
3269 a5edda0a 2019-07-27 stsp
3270 a5edda0a 2019-07-27 stsp return err;
3271 a1fb16d8 2019-05-24 stsp }
3272 a1fb16d8 2019-05-24 stsp
3273 a1fb16d8 2019-05-24 stsp static const struct got_error *
3274 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
3275 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
3276 fa51e947 2020-03-27 stsp {
3277 fa51e947 2020-03-27 stsp const struct got_error *err;
3278 fa51e947 2020-03-27 stsp struct got_repository *repo;
3279 fa51e947 2020-03-27 stsp static char msg[512];
3280 fa51e947 2020-03-27 stsp
3281 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
3282 fa51e947 2020-03-27 stsp if (err)
3283 fa51e947 2020-03-27 stsp return orig_err;
3284 fa51e947 2020-03-27 stsp
3285 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
3286 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
3287 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
3288 fa51e947 2020-03-27 stsp "'got checkout'.\n"
3289 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
3290 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3291 fa51e947 2020-03-27 stsp got_repo_close(repo);
3292 fa51e947 2020-03-27 stsp return err;
3293 fa51e947 2020-03-27 stsp }
3294 fa51e947 2020-03-27 stsp
3295 fa51e947 2020-03-27 stsp static const struct got_error *
3296 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
3297 507dc3bb 2018-12-29 stsp {
3298 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
3299 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
3300 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
3301 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
3302 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
3303 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
3304 024e9686 2019-05-14 stsp const char *branch_name = NULL;
3305 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
3306 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
3307 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
3308 4ad4a1ec 2021-09-13 tracey int ch, verbosity = 0;
3309 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
3310 507dc3bb 2018-12-29 stsp
3311 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
3312 f2ea84fa 2019-07-27 stsp
3313 4ad4a1ec 2021-09-13 tracey while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3314 507dc3bb 2018-12-29 stsp switch (ch) {
3315 024e9686 2019-05-14 stsp case 'b':
3316 024e9686 2019-05-14 stsp branch_name = optarg;
3317 024e9686 2019-05-14 stsp break;
3318 507dc3bb 2018-12-29 stsp case 'c':
3319 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
3320 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
3321 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
3322 4ad4a1ec 2021-09-13 tracey break;
3323 4ad4a1ec 2021-09-13 tracey case 'q':
3324 4ad4a1ec 2021-09-13 tracey verbosity = -1;
3325 507dc3bb 2018-12-29 stsp break;
3326 507dc3bb 2018-12-29 stsp default:
3327 2deda0b9 2019-03-07 stsp usage_update();
3328 507dc3bb 2018-12-29 stsp /* NOTREACHED */
3329 507dc3bb 2018-12-29 stsp }
3330 507dc3bb 2018-12-29 stsp }
3331 507dc3bb 2018-12-29 stsp
3332 507dc3bb 2018-12-29 stsp argc -= optind;
3333 507dc3bb 2018-12-29 stsp argv += optind;
3334 507dc3bb 2018-12-29 stsp
3335 507dc3bb 2018-12-29 stsp #ifndef PROFILE
3336 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3337 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
3338 507dc3bb 2018-12-29 stsp err(1, "pledge");
3339 507dc3bb 2018-12-29 stsp #endif
3340 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
3341 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
3342 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3343 c4cdcb68 2019-04-03 stsp goto done;
3344 c4cdcb68 2019-04-03 stsp }
3345 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
3346 fa51e947 2020-03-27 stsp if (error) {
3347 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3348 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
3349 fa51e947 2020-03-27 stsp worktree_path);
3350 7d5807f4 2019-07-11 stsp goto done;
3351 fa51e947 2020-03-27 stsp }
3352 7d5807f4 2019-07-11 stsp
3353 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
3354 f2ea84fa 2019-07-27 stsp if (error)
3355 f2ea84fa 2019-07-27 stsp goto done;
3356 507dc3bb 2018-12-29 stsp
3357 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3358 c9956ddf 2019-09-08 stsp NULL);
3359 507dc3bb 2018-12-29 stsp if (error != NULL)
3360 507dc3bb 2018-12-29 stsp goto done;
3361 507dc3bb 2018-12-29 stsp
3362 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3363 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3364 087fb88c 2019-08-04 stsp if (error)
3365 087fb88c 2019-08-04 stsp goto done;
3366 087fb88c 2019-08-04 stsp
3367 10604dce 2021-09-24 thomas error = check_merge_in_progress(worktree, repo);
3368 10604dce 2021-09-24 thomas if (error)
3369 10604dce 2021-09-24 thomas goto done;
3370 10604dce 2021-09-24 thomas
3371 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3372 0266afb7 2019-01-04 stsp if (error)
3373 0266afb7 2019-01-04 stsp goto done;
3374 0266afb7 2019-01-04 stsp
3375 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3376 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
3377 024e9686 2019-05-14 stsp if (error != NULL)
3378 024e9686 2019-05-14 stsp goto done;
3379 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
3380 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3381 9c4b8182 2019-01-02 stsp if (error != NULL)
3382 9c4b8182 2019-01-02 stsp goto done;
3383 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3384 507dc3bb 2018-12-29 stsp if (error != NULL)
3385 507dc3bb 2018-12-29 stsp goto done;
3386 507dc3bb 2018-12-29 stsp } else {
3387 84de9106 2020-12-26 stsp struct got_reflist_head refs;
3388 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3389 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3390 84de9106 2020-12-26 stsp NULL);
3391 84de9106 2020-12-26 stsp if (error)
3392 84de9106 2020-12-26 stsp goto done;
3393 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3394 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3395 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
3396 04f57cb3 2019-07-25 stsp free(commit_id_str);
3397 bb787f09 2019-07-25 stsp commit_id_str = NULL;
3398 30837e32 2019-07-25 stsp if (error)
3399 a297e751 2019-07-11 stsp goto done;
3400 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
3401 a297e751 2019-07-11 stsp if (error)
3402 507dc3bb 2018-12-29 stsp goto done;
3403 507dc3bb 2018-12-29 stsp }
3404 35c965b2 2018-12-29 stsp
3405 a1fb16d8 2019-05-24 stsp if (branch_name) {
3406 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
3407 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3408 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3409 f2ea84fa 2019-07-27 stsp continue;
3410 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3411 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3412 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3413 024e9686 2019-05-14 stsp goto done;
3414 024e9686 2019-05-14 stsp }
3415 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3416 024e9686 2019-05-14 stsp if (error)
3417 024e9686 2019-05-14 stsp goto done;
3418 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3419 3aef623b 2019-10-15 stsp repo);
3420 a367ff0f 2019-05-14 stsp free(head_commit_id);
3421 024e9686 2019-05-14 stsp if (error != NULL)
3422 024e9686 2019-05-14 stsp goto done;
3423 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3424 a367ff0f 2019-05-14 stsp if (error)
3425 a367ff0f 2019-05-14 stsp goto done;
3426 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3427 024e9686 2019-05-14 stsp if (error)
3428 024e9686 2019-05-14 stsp goto done;
3429 024e9686 2019-05-14 stsp } else {
3430 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3431 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3432 a367ff0f 2019-05-14 stsp if (error != NULL) {
3433 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3434 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3435 024e9686 2019-05-14 stsp goto done;
3436 a367ff0f 2019-05-14 stsp }
3437 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3438 a367ff0f 2019-05-14 stsp if (error)
3439 a367ff0f 2019-05-14 stsp goto done;
3440 024e9686 2019-05-14 stsp }
3441 507dc3bb 2018-12-29 stsp
3442 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3443 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3444 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3445 507dc3bb 2018-12-29 stsp commit_id);
3446 507dc3bb 2018-12-29 stsp if (error)
3447 507dc3bb 2018-12-29 stsp goto done;
3448 507dc3bb 2018-12-29 stsp }
3449 507dc3bb 2018-12-29 stsp
3450 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3451 4ad4a1ec 2021-09-13 tracey upa.verbosity = verbosity;
3452 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3453 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3454 507dc3bb 2018-12-29 stsp if (error != NULL)
3455 507dc3bb 2018-12-29 stsp goto done;
3456 9c4b8182 2019-01-02 stsp
3457 4f3c844b 2021-09-14 stsp if (upa.did_something) {
3458 4f3c844b 2021-09-14 stsp printf("Updated to %s: %s\n",
3459 4f3c844b 2021-09-14 stsp got_worktree_get_head_ref_name(worktree), commit_id_str);
3460 4f3c844b 2021-09-14 stsp } else
3461 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3462 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3463 507dc3bb 2018-12-29 stsp done:
3464 c09a553d 2018-03-12 stsp free(worktree_path);
3465 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3466 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3467 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3468 507dc3bb 2018-12-29 stsp free(commit_id);
3469 9c4b8182 2019-01-02 stsp free(commit_id_str);
3470 c09a553d 2018-03-12 stsp return error;
3471 c09a553d 2018-03-12 stsp }
3472 c09a553d 2018-03-12 stsp
3473 f42b1b34 2018-03-12 stsp static const struct got_error *
3474 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3475 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3476 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3477 5c860e29 2018-03-12 stsp {
3478 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3479 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3480 79109fed 2018-03-27 stsp
3481 44392932 2019-08-25 stsp if (blob_id1) {
3482 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3483 44392932 2019-08-25 stsp if (err)
3484 44392932 2019-08-25 stsp goto done;
3485 44392932 2019-08-25 stsp }
3486 79109fed 2018-03-27 stsp
3487 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3488 44392932 2019-08-25 stsp if (err)
3489 44392932 2019-08-25 stsp goto done;
3490 79109fed 2018-03-27 stsp
3491 44392932 2019-08-25 stsp while (path[0] == '/')
3492 44392932 2019-08-25 stsp path++;
3493 fe621944 2020-11-10 stsp err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3494 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, stdout);
3495 44392932 2019-08-25 stsp done:
3496 44392932 2019-08-25 stsp if (blob1)
3497 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3498 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3499 44392932 2019-08-25 stsp return err;
3500 44392932 2019-08-25 stsp }
3501 44392932 2019-08-25 stsp
3502 44392932 2019-08-25 stsp static const struct got_error *
3503 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3504 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3505 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3506 44392932 2019-08-25 stsp {
3507 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3508 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3509 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3510 44392932 2019-08-25 stsp
3511 44392932 2019-08-25 stsp if (tree_id1) {
3512 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3513 79109fed 2018-03-27 stsp if (err)
3514 44392932 2019-08-25 stsp goto done;
3515 79109fed 2018-03-27 stsp }
3516 79109fed 2018-03-27 stsp
3517 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3518 0f2b3dca 2018-12-22 stsp if (err)
3519 0f2b3dca 2018-12-22 stsp goto done;
3520 0f2b3dca 2018-12-22 stsp
3521 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3522 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3523 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
3524 aaa13589 2019-06-01 stsp arg.outfile = stdout;
3525 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
3526 fe621944 2020-11-10 stsp arg.nlines = 0;
3527 44392932 2019-08-25 stsp while (path[0] == '/')
3528 44392932 2019-08-25 stsp path++;
3529 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
3530 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3531 0f2b3dca 2018-12-22 stsp done:
3532 79109fed 2018-03-27 stsp if (tree1)
3533 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3534 366e0a5f 2019-10-10 stsp if (tree2)
3535 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3536 44392932 2019-08-25 stsp return err;
3537 44392932 2019-08-25 stsp }
3538 44392932 2019-08-25 stsp
3539 44392932 2019-08-25 stsp static const struct got_error *
3540 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3541 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3542 0208f208 2020-05-05 stsp {
3543 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3544 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3545 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3546 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3547 0208f208 2020-05-05 stsp
3548 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3549 0208f208 2020-05-05 stsp if (qid != NULL) {
3550 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3551 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3552 0208f208 2020-05-05 stsp qid->id);
3553 0208f208 2020-05-05 stsp if (err)
3554 0208f208 2020-05-05 stsp return err;
3555 0208f208 2020-05-05 stsp
3556 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3557 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3558 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3559 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3560 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3561 aa8b5dd0 2021-08-01 stsp }
3562 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3563 0208f208 2020-05-05 stsp
3564 0208f208 2020-05-05 stsp }
3565 0208f208 2020-05-05 stsp
3566 0208f208 2020-05-05 stsp if (tree_id1) {
3567 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3568 0208f208 2020-05-05 stsp if (err)
3569 0208f208 2020-05-05 stsp goto done;
3570 0208f208 2020-05-05 stsp }
3571 0208f208 2020-05-05 stsp
3572 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3573 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3574 0208f208 2020-05-05 stsp if (err)
3575 0208f208 2020-05-05 stsp goto done;
3576 0208f208 2020-05-05 stsp
3577 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3578 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3579 0208f208 2020-05-05 stsp done:
3580 0208f208 2020-05-05 stsp if (tree1)
3581 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3582 0208f208 2020-05-05 stsp if (tree2)
3583 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3584 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3585 0208f208 2020-05-05 stsp return err;
3586 0208f208 2020-05-05 stsp }
3587 0208f208 2020-05-05 stsp
3588 0208f208 2020-05-05 stsp static const struct got_error *
3589 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3590 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3591 44392932 2019-08-25 stsp {
3592 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3593 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3594 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3595 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3596 44392932 2019-08-25 stsp struct got_object_qid *qid;
3597 44392932 2019-08-25 stsp
3598 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3599 44392932 2019-08-25 stsp if (qid != NULL) {
3600 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3601 44392932 2019-08-25 stsp qid->id);
3602 44392932 2019-08-25 stsp if (err)
3603 44392932 2019-08-25 stsp return err;
3604 44392932 2019-08-25 stsp }
3605 44392932 2019-08-25 stsp
3606 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3607 44392932 2019-08-25 stsp int obj_type;
3608 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3609 44392932 2019-08-25 stsp if (err)
3610 44392932 2019-08-25 stsp goto done;
3611 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3612 44392932 2019-08-25 stsp if (err) {
3613 44392932 2019-08-25 stsp free(obj_id2);
3614 44392932 2019-08-25 stsp goto done;
3615 44392932 2019-08-25 stsp }
3616 44392932 2019-08-25 stsp if (pcommit) {
3617 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3618 44392932 2019-08-25 stsp qid->id, path);
3619 44392932 2019-08-25 stsp if (err) {
3620 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3621 2e8c69d1 2020-05-04 stsp free(obj_id2);
3622 2e8c69d1 2020-05-04 stsp goto done;
3623 2e8c69d1 2020-05-04 stsp }
3624 2e8c69d1 2020-05-04 stsp } else {
3625 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3626 2e8c69d1 2020-05-04 stsp if (err) {
3627 2e8c69d1 2020-05-04 stsp free(obj_id2);
3628 2e8c69d1 2020-05-04 stsp goto done;
3629 2e8c69d1 2020-05-04 stsp }
3630 44392932 2019-08-25 stsp }
3631 44392932 2019-08-25 stsp }
3632 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3633 44392932 2019-08-25 stsp if (err) {
3634 44392932 2019-08-25 stsp free(obj_id2);
3635 44392932 2019-08-25 stsp goto done;
3636 44392932 2019-08-25 stsp }
3637 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3638 44392932 2019-08-25 stsp switch (obj_type) {
3639 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3640 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3641 64453f7e 2020-11-21 stsp 0, 0, repo);
3642 44392932 2019-08-25 stsp break;
3643 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3644 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3645 64453f7e 2020-11-21 stsp 0, 0, repo);
3646 44392932 2019-08-25 stsp break;
3647 44392932 2019-08-25 stsp default:
3648 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3649 44392932 2019-08-25 stsp break;
3650 44392932 2019-08-25 stsp }
3651 44392932 2019-08-25 stsp free(obj_id1);
3652 44392932 2019-08-25 stsp free(obj_id2);
3653 44392932 2019-08-25 stsp } else {
3654 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3655 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3656 44392932 2019-08-25 stsp if (err)
3657 44392932 2019-08-25 stsp goto done;
3658 579bd556 2020-10-24 stsp if (pcommit) {
3659 579bd556 2020-10-24 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3660 579bd556 2020-10-24 stsp err = got_object_id_str(&id_str1, obj_id1);
3661 579bd556 2020-10-24 stsp if (err)
3662 579bd556 2020-10-24 stsp goto done;
3663 579bd556 2020-10-24 stsp }
3664 64453f7e 2020-11-21 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3665 64453f7e 2020-11-21 stsp id_str2);
3666 64453f7e 2020-11-21 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3667 64453f7e 2020-11-21 stsp repo);
3668 44392932 2019-08-25 stsp }
3669 44392932 2019-08-25 stsp done:
3670 0f2b3dca 2018-12-22 stsp free(id_str1);
3671 0f2b3dca 2018-12-22 stsp free(id_str2);
3672 44392932 2019-08-25 stsp if (pcommit)
3673 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3674 79109fed 2018-03-27 stsp return err;
3675 79109fed 2018-03-27 stsp }
3676 79109fed 2018-03-27 stsp
3677 4bb494d5 2018-06-16 stsp static char *
3678 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3679 6c281f94 2018-06-11 stsp {
3680 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3681 09867e48 2019-08-13 stsp char *p, *s;
3682 09867e48 2019-08-13 stsp
3683 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3684 09867e48 2019-08-13 stsp if (tm == NULL)
3685 09867e48 2019-08-13 stsp return NULL;
3686 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3687 09867e48 2019-08-13 stsp if (s == NULL)
3688 09867e48 2019-08-13 stsp return NULL;
3689 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3690 6c281f94 2018-06-11 stsp if (p)
3691 6c281f94 2018-06-11 stsp *p = '\0';
3692 6c281f94 2018-06-11 stsp return s;
3693 6c281f94 2018-06-11 stsp }
3694 dc424a06 2019-08-07 stsp
3695 6841bf13 2019-11-29 kn static const struct got_error *
3696 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3697 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3698 6841bf13 2019-11-29 kn {
3699 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3700 6841bf13 2019-11-29 kn regmatch_t regmatch;
3701 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3702 6841bf13 2019-11-29 kn
3703 6841bf13 2019-11-29 kn *have_match = 0;
3704 6841bf13 2019-11-29 kn
3705 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3706 6841bf13 2019-11-29 kn if (err)
3707 6841bf13 2019-11-29 kn return err;
3708 6841bf13 2019-11-29 kn
3709 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3710 6841bf13 2019-11-29 kn if (err)
3711 6841bf13 2019-11-29 kn goto done;
3712 6841bf13 2019-11-29 kn
3713 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3714 6841bf13 2019-11-29 kn *have_match = 1;
3715 6841bf13 2019-11-29 kn done:
3716 6841bf13 2019-11-29 kn free(id_str);
3717 6841bf13 2019-11-29 kn free(logmsg);
3718 6841bf13 2019-11-29 kn return err;
3719 6841bf13 2019-11-29 kn }
3720 6841bf13 2019-11-29 kn
3721 0208f208 2020-05-05 stsp static void
3722 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3723 0208f208 2020-05-05 stsp regex_t *regex)
3724 0208f208 2020-05-05 stsp {
3725 0208f208 2020-05-05 stsp regmatch_t regmatch;
3726 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3727 0208f208 2020-05-05 stsp
3728 0208f208 2020-05-05 stsp *have_match = 0;
3729 0208f208 2020-05-05 stsp
3730 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3731 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3732 0208f208 2020-05-05 stsp *have_match = 1;
3733 0208f208 2020-05-05 stsp break;
3734 0208f208 2020-05-05 stsp }
3735 0208f208 2020-05-05 stsp }
3736 0208f208 2020-05-05 stsp }
3737 0208f208 2020-05-05 stsp
3738 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3739 6c281f94 2018-06-11 stsp
3740 888b7d99 2020-12-26 stsp static const struct got_error*
3741 888b7d99 2020-12-26 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
3742 888b7d99 2020-12-26 stsp struct got_object_id *id, struct got_repository *repo)
3743 888b7d99 2020-12-26 stsp {
3744 888b7d99 2020-12-26 stsp static const struct got_error *err = NULL;
3745 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3746 888b7d99 2020-12-26 stsp char *s;
3747 888b7d99 2020-12-26 stsp const char *name;
3748 5c860e29 2018-03-12 stsp
3749 888b7d99 2020-12-26 stsp *refs_str = NULL;
3750 888b7d99 2020-12-26 stsp
3751 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
3752 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3753 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3754 a436ad14 2019-08-13 stsp int cmp;
3755 a436ad14 2019-08-13 stsp
3756 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3757 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3758 d9498b20 2019-02-05 stsp continue;
3759 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3760 199a4027 2019-02-02 stsp name += 5;
3761 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3762 7143d404 2019-03-12 stsp continue;
3763 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3764 e34f9ed6 2019-02-02 stsp name += 6;
3765 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3766 141c2bff 2019-02-04 stsp name += 8;
3767 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3768 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3769 4343a07f 2020-04-24 stsp continue;
3770 4343a07f 2020-04-24 stsp }
3771 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3772 48cae60d 2020-09-22 stsp if (err)
3773 888b7d99 2020-12-26 stsp break;
3774 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3775 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3776 5d844a1e 2019-08-13 stsp if (err) {
3777 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3778 48cae60d 2020-09-22 stsp free(ref_id);
3779 888b7d99 2020-12-26 stsp break;
3780 48cae60d 2020-09-22 stsp }
3781 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3782 5d844a1e 2019-08-13 stsp err = NULL;
3783 5d844a1e 2019-08-13 stsp tag = NULL;
3784 5d844a1e 2019-08-13 stsp }
3785 a436ad14 2019-08-13 stsp }
3786 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3787 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3788 48cae60d 2020-09-22 stsp free(ref_id);
3789 a436ad14 2019-08-13 stsp if (tag)
3790 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3791 a436ad14 2019-08-13 stsp if (cmp != 0)
3792 a436ad14 2019-08-13 stsp continue;
3793 888b7d99 2020-12-26 stsp s = *refs_str;
3794 888b7d99 2020-12-26 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
3795 888b7d99 2020-12-26 stsp s ? ", " : "", name) == -1) {
3796 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3797 199a4027 2019-02-02 stsp free(s);
3798 888b7d99 2020-12-26 stsp *refs_str = NULL;
3799 888b7d99 2020-12-26 stsp break;
3800 199a4027 2019-02-02 stsp }
3801 199a4027 2019-02-02 stsp free(s);
3802 199a4027 2019-02-02 stsp }
3803 888b7d99 2020-12-26 stsp
3804 888b7d99 2020-12-26 stsp return err;
3805 888b7d99 2020-12-26 stsp }
3806 888b7d99 2020-12-26 stsp
3807 888b7d99 2020-12-26 stsp static const struct got_error *
3808 888b7d99 2020-12-26 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3809 888b7d99 2020-12-26 stsp struct got_repository *repo, const char *path,
3810 888b7d99 2020-12-26 stsp struct got_pathlist_head *changed_paths, int show_patch,
3811 e600f124 2021-03-21 stsp int diff_context, struct got_reflist_object_id_map *refs_idmap,
3812 e600f124 2021-03-21 stsp const char *custom_refs_str)
3813 888b7d99 2020-12-26 stsp {
3814 888b7d99 2020-12-26 stsp const struct got_error *err = NULL;
3815 888b7d99 2020-12-26 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3816 888b7d99 2020-12-26 stsp char datebuf[26];
3817 888b7d99 2020-12-26 stsp time_t committer_time;
3818 888b7d99 2020-12-26 stsp const char *author, *committer;
3819 888b7d99 2020-12-26 stsp char *refs_str = NULL;
3820 888b7d99 2020-12-26 stsp
3821 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3822 8bf5b3c9 2018-03-17 stsp if (err)
3823 8bf5b3c9 2018-03-17 stsp return err;
3824 788c352e 2018-06-16 stsp
3825 e600f124 2021-03-21 stsp if (custom_refs_str == NULL) {
3826 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
3827 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3828 e600f124 2021-03-21 stsp if (refs) {
3829 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, id, repo);
3830 e600f124 2021-03-21 stsp if (err)
3831 e600f124 2021-03-21 stsp goto done;
3832 e600f124 2021-03-21 stsp }
3833 888b7d99 2020-12-26 stsp }
3834 888b7d99 2020-12-26 stsp
3835 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3836 e600f124 2021-03-21 stsp if (custom_refs_str)
3837 e600f124 2021-03-21 stsp printf("commit %s (%s)\n", id_str, custom_refs_str);
3838 e600f124 2021-03-21 stsp else
3839 e600f124 2021-03-21 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3840 e600f124 2021-03-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3841 832c249c 2018-06-10 stsp free(id_str);
3842 d3d493d7 2019-02-21 stsp id_str = NULL;
3843 d3d493d7 2019-02-21 stsp free(refs_str);
3844 d3d493d7 2019-02-21 stsp refs_str = NULL;
3845 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3846 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3847 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3848 09867e48 2019-08-13 stsp if (datestr)
3849 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3850 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3851 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3852 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3853 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3854 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3855 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3856 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3857 3fe1abad 2018-06-10 stsp int n = 1;
3858 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3859 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, parent_ids, entry) {
3860 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3861 a0603db2 2018-06-10 stsp if (err)
3862 888b7d99 2020-12-26 stsp goto done;
3863 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3864 a0603db2 2018-06-10 stsp free(id_str);
3865 888b7d99 2020-12-26 stsp id_str = NULL;
3866 a0603db2 2018-06-10 stsp }
3867 a0603db2 2018-06-10 stsp }
3868 832c249c 2018-06-10 stsp
3869 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3870 5943eee2 2019-08-13 stsp if (err)
3871 888b7d99 2020-12-26 stsp goto done;
3872 8bf5b3c9 2018-03-17 stsp
3873 621015ac 2018-07-23 stsp logmsg = logmsg0;
3874 832c249c 2018-06-10 stsp do {
3875 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3876 832c249c 2018-06-10 stsp if (line)
3877 832c249c 2018-06-10 stsp printf(" %s\n", line);
3878 832c249c 2018-06-10 stsp } while (line);
3879 621015ac 2018-07-23 stsp free(logmsg0);
3880 832c249c 2018-06-10 stsp
3881 0208f208 2020-05-05 stsp if (changed_paths) {
3882 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3883 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3884 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3885 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3886 0208f208 2020-05-05 stsp }
3887 0208f208 2020-05-05 stsp printf("\n");
3888 0208f208 2020-05-05 stsp }
3889 971751ac 2018-03-27 stsp if (show_patch) {
3890 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3891 971751ac 2018-03-27 stsp if (err == 0)
3892 971751ac 2018-03-27 stsp printf("\n");
3893 971751ac 2018-03-27 stsp }
3894 07862c20 2018-09-15 stsp
3895 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3896 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3897 888b7d99 2020-12-26 stsp done:
3898 888b7d99 2020-12-26 stsp free(id_str);
3899 888b7d99 2020-12-26 stsp free(refs_str);
3900 07862c20 2018-09-15 stsp return err;
3901 f42b1b34 2018-03-12 stsp }
3902 5c860e29 2018-03-12 stsp
3903 f42b1b34 2018-03-12 stsp static const struct got_error *
3904 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3905 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3906 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3907 888b7d99 2020-12-26 stsp int log_branches, int reverse_display_order,
3908 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap)
3909 f42b1b34 2018-03-12 stsp {
3910 f42b1b34 2018-03-12 stsp const struct got_error *err;
3911 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3912 6841bf13 2019-11-29 kn regex_t regex;
3913 6841bf13 2019-11-29 kn int have_match;
3914 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3915 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3916 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3917 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3918 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3919 dbec59df 2020-04-18 stsp
3920 dbdddfee 2021-06-23 naddy STAILQ_INIT(&reversed_commits);
3921 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3922 372ccdbb 2018-06-10 stsp
3923 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(&regex, search_pattern,
3924 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3925 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3926 6841bf13 2019-11-29 kn
3927 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3928 f42b1b34 2018-03-12 stsp if (err)
3929 f42b1b34 2018-03-12 stsp return err;
3930 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3931 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3932 372ccdbb 2018-06-10 stsp if (err)
3933 fcc85cad 2018-07-22 stsp goto done;
3934 656b1f76 2019-05-11 jcs for (;;) {
3935 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3936 8bf5b3c9 2018-03-17 stsp
3937 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3938 84453469 2018-11-11 stsp break;
3939 84453469 2018-11-11 stsp
3940 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3941 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3942 372ccdbb 2018-06-10 stsp if (err) {
3943 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3944 9ba79e04 2018-06-11 stsp err = NULL;
3945 ee780d5c 2020-01-04 stsp break;
3946 7e665116 2018-04-02 stsp }
3947 b43fbaa0 2018-06-11 stsp if (id == NULL)
3948 7e665116 2018-04-02 stsp break;
3949 b43fbaa0 2018-06-11 stsp
3950 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3951 b43fbaa0 2018-06-11 stsp if (err)
3952 fcc85cad 2018-07-22 stsp break;
3953 6841bf13 2019-11-29 kn
3954 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3955 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3956 0208f208 2020-05-05 stsp if (err)
3957 0208f208 2020-05-05 stsp break;
3958 0208f208 2020-05-05 stsp }
3959 0208f208 2020-05-05 stsp
3960 6841bf13 2019-11-29 kn if (search_pattern) {
3961 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3962 6841bf13 2019-11-29 kn if (err) {
3963 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3964 6841bf13 2019-11-29 kn break;
3965 6841bf13 2019-11-29 kn }
3966 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3967 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3968 0208f208 2020-05-05 stsp &changed_paths, &regex);
3969 6841bf13 2019-11-29 kn if (have_match == 0) {
3970 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3971 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3972 0208f208 2020-05-05 stsp free((char *)pe->path);
3973 0208f208 2020-05-05 stsp free(pe->data);
3974 0208f208 2020-05-05 stsp }
3975 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3976 6841bf13 2019-11-29 kn continue;
3977 6841bf13 2019-11-29 kn }
3978 6841bf13 2019-11-29 kn }
3979 6841bf13 2019-11-29 kn
3980 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3981 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3982 dbec59df 2020-04-18 stsp if (err)
3983 dbec59df 2020-04-18 stsp break;
3984 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3985 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3986 dbec59df 2020-04-18 stsp } else {
3987 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3988 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3989 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
3990 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3991 dbec59df 2020-04-18 stsp if (err)
3992 dbec59df 2020-04-18 stsp break;
3993 dbec59df 2020-04-18 stsp }
3994 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3995 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3996 7e665116 2018-04-02 stsp break;
3997 0208f208 2020-05-05 stsp
3998 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3999 0208f208 2020-05-05 stsp free((char *)pe->path);
4000 0208f208 2020-05-05 stsp free(pe->data);
4001 0208f208 2020-05-05 stsp }
4002 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4003 31cedeaf 2018-09-15 stsp }
4004 dbec59df 2020-04-18 stsp if (reverse_display_order) {
4005 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &reversed_commits, entry) {
4006 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
4007 dbec59df 2020-04-18 stsp if (err)
4008 dbec59df 2020-04-18 stsp break;
4009 502b9684 2020-07-31 stsp if (show_changed_paths) {
4010 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
4011 502b9684 2020-07-31 stsp commit, repo);
4012 502b9684 2020-07-31 stsp if (err)
4013 502b9684 2020-07-31 stsp break;
4014 502b9684 2020-07-31 stsp }
4015 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
4016 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
4017 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
4018 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
4019 dbec59df 2020-04-18 stsp if (err)
4020 dbec59df 2020-04-18 stsp break;
4021 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4022 502b9684 2020-07-31 stsp free((char *)pe->path);
4023 502b9684 2020-07-31 stsp free(pe->data);
4024 502b9684 2020-07-31 stsp }
4025 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
4026 dbec59df 2020-04-18 stsp }
4027 dbec59df 2020-04-18 stsp }
4028 fcc85cad 2018-07-22 stsp done:
4029 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&reversed_commits)) {
4030 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&reversed_commits);
4031 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4032 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
4033 dbec59df 2020-04-18 stsp }
4034 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4035 0208f208 2020-05-05 stsp free((char *)pe->path);
4036 0208f208 2020-05-05 stsp free(pe->data);
4037 0208f208 2020-05-05 stsp }
4038 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4039 6841bf13 2019-11-29 kn if (search_pattern)
4040 6841bf13 2019-11-29 kn regfree(&regex);
4041 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
4042 f42b1b34 2018-03-12 stsp return err;
4043 f42b1b34 2018-03-12 stsp }
4044 5c860e29 2018-03-12 stsp
4045 4ed7e80c 2018-05-20 stsp __dead static void
4046 6f3d1eb0 2018-03-12 stsp usage_log(void)
4047 6f3d1eb0 2018-03-12 stsp {
4048 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
4049 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
4050 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
4051 6f3d1eb0 2018-03-12 stsp exit(1);
4052 b1ebc001 2019-08-13 stsp }
4053 b1ebc001 2019-08-13 stsp
4054 b1ebc001 2019-08-13 stsp static int
4055 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
4056 b1ebc001 2019-08-13 stsp {
4057 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
4058 b1ebc001 2019-08-13 stsp long long n;
4059 b1ebc001 2019-08-13 stsp const char *errstr;
4060 b1ebc001 2019-08-13 stsp
4061 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4062 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
4063 b1ebc001 2019-08-13 stsp return 0;
4064 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4065 b1ebc001 2019-08-13 stsp if (errstr != NULL)
4066 b1ebc001 2019-08-13 stsp return 0;
4067 b1ebc001 2019-08-13 stsp return n;
4068 6f3d1eb0 2018-03-12 stsp }
4069 6f3d1eb0 2018-03-12 stsp
4070 4ed7e80c 2018-05-20 stsp static const struct got_error *
4071 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
4072 f42b1b34 2018-03-12 stsp {
4073 f42b1b34 2018-03-12 stsp const struct got_error *error;
4074 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
4075 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
4076 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
4077 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4078 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
4079 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
4080 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
4081 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4082 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
4083 64a96a6d 2018-04-01 stsp const char *errstr;
4084 199a4027 2019-02-02 stsp struct got_reflist_head refs;
4085 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
4086 1b3893a2 2019-03-18 stsp
4087 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4088 5c860e29 2018-03-12 stsp
4089 6715a751 2018-03-16 stsp #ifndef PROFILE
4090 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4091 6098196c 2019-01-04 stsp NULL)
4092 ad242220 2018-09-08 stsp == -1)
4093 f42b1b34 2018-03-12 stsp err(1, "pledge");
4094 6715a751 2018-03-16 stsp #endif
4095 79109fed 2018-03-27 stsp
4096 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
4097 b1ebc001 2019-08-13 stsp
4098 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
4099 79109fed 2018-03-27 stsp switch (ch) {
4100 79109fed 2018-03-27 stsp case 'p':
4101 79109fed 2018-03-27 stsp show_patch = 1;
4102 d142fc45 2018-04-01 stsp break;
4103 0208f208 2020-05-05 stsp case 'P':
4104 0208f208 2020-05-05 stsp show_changed_paths = 1;
4105 0208f208 2020-05-05 stsp break;
4106 d142fc45 2018-04-01 stsp case 'c':
4107 d142fc45 2018-04-01 stsp start_commit = optarg;
4108 64a96a6d 2018-04-01 stsp break;
4109 c0cc5c62 2018-10-18 stsp case 'C':
4110 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4111 4a8520aa 2018-10-18 stsp &errstr);
4112 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4113 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4114 c0cc5c62 2018-10-18 stsp break;
4115 64a96a6d 2018-04-01 stsp case 'l':
4116 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
4117 64a96a6d 2018-04-01 stsp if (errstr != NULL)
4118 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
4119 cc54c501 2019-07-15 stsp break;
4120 48c8c60d 2020-01-27 stsp case 'b':
4121 48c8c60d 2020-01-27 stsp log_branches = 1;
4122 a0603db2 2018-06-10 stsp break;
4123 04ca23f4 2018-07-16 stsp case 'r':
4124 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4125 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
4126 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4127 9ba1d308 2019-10-21 stsp optarg);
4128 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4129 04ca23f4 2018-07-16 stsp break;
4130 dbec59df 2020-04-18 stsp case 'R':
4131 dbec59df 2020-04-18 stsp reverse_display_order = 1;
4132 dbec59df 2020-04-18 stsp break;
4133 6841bf13 2019-11-29 kn case 's':
4134 6841bf13 2019-11-29 kn search_pattern = optarg;
4135 6841bf13 2019-11-29 kn break;
4136 d1fe46f9 2020-04-18 stsp case 'x':
4137 d1fe46f9 2020-04-18 stsp end_commit = optarg;
4138 d1fe46f9 2020-04-18 stsp break;
4139 79109fed 2018-03-27 stsp default:
4140 2deda0b9 2019-03-07 stsp usage_log();
4141 79109fed 2018-03-27 stsp /* NOTREACHED */
4142 79109fed 2018-03-27 stsp }
4143 79109fed 2018-03-27 stsp }
4144 79109fed 2018-03-27 stsp
4145 79109fed 2018-03-27 stsp argc -= optind;
4146 79109fed 2018-03-27 stsp argv += optind;
4147 f42b1b34 2018-03-12 stsp
4148 dc1edbfa 2019-11-29 kn if (diff_context == -1)
4149 dc1edbfa 2019-11-29 kn diff_context = 3;
4150 dc1edbfa 2019-11-29 kn else if (!show_patch)
4151 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
4152 dc1edbfa 2019-11-29 kn
4153 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
4154 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
4155 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4156 04ca23f4 2018-07-16 stsp goto done;
4157 04ca23f4 2018-07-16 stsp }
4158 cffc0aa4 2019-02-05 stsp
4159 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
4160 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
4161 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4162 50f2fada 2020-04-24 stsp goto done;
4163 50f2fada 2020-04-24 stsp error = NULL;
4164 50f2fada 2020-04-24 stsp }
4165 cffc0aa4 2019-02-05 stsp
4166 603cdeb0 2020-10-22 stsp if (argc == 1) {
4167 e7301579 2019-03-18 stsp if (worktree) {
4168 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4169 e7301579 2019-03-18 stsp argv[0]);
4170 e7301579 2019-03-18 stsp if (error)
4171 e7301579 2019-03-18 stsp goto done;
4172 e7301579 2019-03-18 stsp } else {
4173 e7301579 2019-03-18 stsp path = strdup(argv[0]);
4174 e7301579 2019-03-18 stsp if (path == NULL) {
4175 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4176 e7301579 2019-03-18 stsp goto done;
4177 e7301579 2019-03-18 stsp }
4178 e7301579 2019-03-18 stsp }
4179 603cdeb0 2020-10-22 stsp } else if (argc != 0)
4180 cbd1af7a 2019-03-18 stsp usage_log();
4181 cbd1af7a 2019-03-18 stsp
4182 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
4183 5486daa2 2019-05-11 stsp repo_path = worktree ?
4184 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4185 5486daa2 2019-05-11 stsp }
4186 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
4187 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4188 cffc0aa4 2019-02-05 stsp goto done;
4189 04ca23f4 2018-07-16 stsp }
4190 6098196c 2019-01-04 stsp
4191 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4192 d7d4f210 2018-03-12 stsp if (error != NULL)
4193 04ca23f4 2018-07-16 stsp goto done;
4194 f42b1b34 2018-03-12 stsp
4195 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4196 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4197 c02c541e 2019-03-29 stsp if (error)
4198 c02c541e 2019-03-29 stsp goto done;
4199 c02c541e 2019-03-29 stsp
4200 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4201 84de9106 2020-12-26 stsp if (error)
4202 84de9106 2020-12-26 stsp goto done;
4203 84de9106 2020-12-26 stsp
4204 888b7d99 2020-12-26 stsp error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4205 888b7d99 2020-12-26 stsp if (error)
4206 888b7d99 2020-12-26 stsp goto done;
4207 888b7d99 2020-12-26 stsp
4208 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
4209 3235492e 2018-04-01 stsp struct got_reference *head_ref;
4210 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
4211 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
4212 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4213 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
4214 3235492e 2018-04-01 stsp if (error != NULL)
4215 d1fe46f9 2020-04-18 stsp goto done;
4216 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
4217 3235492e 2018-04-01 stsp got_ref_close(head_ref);
4218 3235492e 2018-04-01 stsp if (error != NULL)
4219 d1fe46f9 2020-04-18 stsp goto done;
4220 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
4221 d1fe46f9 2020-04-18 stsp start_id);
4222 d1fe46f9 2020-04-18 stsp if (error != NULL)
4223 d1fe46f9 2020-04-18 stsp goto done;
4224 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
4225 3235492e 2018-04-01 stsp } else {
4226 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&start_id, NULL,
4227 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4228 d1fe46f9 2020-04-18 stsp if (error != NULL)
4229 d1fe46f9 2020-04-18 stsp goto done;
4230 3235492e 2018-04-01 stsp }
4231 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
4232 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&end_id, NULL,
4233 84de9106 2020-12-26 stsp end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4234 d1fe46f9 2020-04-18 stsp if (error != NULL)
4235 d1fe46f9 2020-04-18 stsp goto done;
4236 d1fe46f9 2020-04-18 stsp }
4237 04ca23f4 2018-07-16 stsp
4238 deeabeae 2019-08-27 stsp if (worktree) {
4239 603cdeb0 2020-10-22 stsp /*
4240 603cdeb0 2020-10-22 stsp * If a path was specified on the command line it was resolved
4241 603cdeb0 2020-10-22 stsp * to a path in the work tree above. Prepend the work tree's
4242 603cdeb0 2020-10-22 stsp * path prefix to obtain the corresponding in-repository path.
4243 603cdeb0 2020-10-22 stsp */
4244 603cdeb0 2020-10-22 stsp if (path) {
4245 603cdeb0 2020-10-22 stsp const char *prefix;
4246 603cdeb0 2020-10-22 stsp prefix = got_worktree_get_path_prefix(worktree);
4247 603cdeb0 2020-10-22 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4248 603cdeb0 2020-10-22 stsp (path[0] != '\0') ? "/" : "", path) == -1) {
4249 603cdeb0 2020-10-22 stsp error = got_error_from_errno("asprintf");
4250 603cdeb0 2020-10-22 stsp goto done;
4251 603cdeb0 2020-10-22 stsp }
4252 603cdeb0 2020-10-22 stsp }
4253 deeabeae 2019-08-27 stsp } else
4254 603cdeb0 2020-10-22 stsp error = got_repo_map_path(&in_repo_path, repo,
4255 8fa913ec 2020-11-14 stsp path ? path : "");
4256 04ca23f4 2018-07-16 stsp if (error != NULL)
4257 04ca23f4 2018-07-16 stsp goto done;
4258 04ca23f4 2018-07-16 stsp if (in_repo_path) {
4259 04ca23f4 2018-07-16 stsp free(path);
4260 04ca23f4 2018-07-16 stsp path = in_repo_path;
4261 04ca23f4 2018-07-16 stsp }
4262 199a4027 2019-02-02 stsp
4263 603cdeb0 2020-10-22 stsp error = print_commits(start_id, end_id, repo, path ? path : "",
4264 603cdeb0 2020-10-22 stsp show_changed_paths, show_patch, search_pattern, diff_context,
4265 888b7d99 2020-12-26 stsp limit, log_branches, reverse_display_order, refs_idmap);
4266 04ca23f4 2018-07-16 stsp done:
4267 04ca23f4 2018-07-16 stsp free(path);
4268 04ca23f4 2018-07-16 stsp free(repo_path);
4269 04ca23f4 2018-07-16 stsp free(cwd);
4270 cffc0aa4 2019-02-05 stsp if (worktree)
4271 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
4272 ad242220 2018-09-08 stsp if (repo) {
4273 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4274 ad242220 2018-09-08 stsp if (error == NULL)
4275 1d0f4054 2021-06-17 stsp error = close_err;
4276 ad242220 2018-09-08 stsp }
4277 888b7d99 2020-12-26 stsp if (refs_idmap)
4278 888b7d99 2020-12-26 stsp got_reflist_object_id_map_free(refs_idmap);
4279 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4280 8bf5b3c9 2018-03-17 stsp return error;
4281 5c860e29 2018-03-12 stsp }
4282 5c860e29 2018-03-12 stsp
4283 4ed7e80c 2018-05-20 stsp __dead static void
4284 3f8b7d6a 2018-04-01 stsp usage_diff(void)
4285 3f8b7d6a 2018-04-01 stsp {
4286 2777a004 2021-10-12 thomas fprintf(stderr, "usage: %s diff [-a] [-c commit] [-C number] "
4287 2777a004 2021-10-12 thomas "[-r repository-path] [-s] [-w] [-P] "
4288 2777a004 2021-10-12 thomas "[object1 object2 | path ...]\n", getprogname());
4289 3f8b7d6a 2018-04-01 stsp exit(1);
4290 b00d56cd 2018-04-01 stsp }
4291 b00d56cd 2018-04-01 stsp
4292 b72f483a 2019-02-05 stsp struct print_diff_arg {
4293 b72f483a 2019-02-05 stsp struct got_repository *repo;
4294 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
4295 b72f483a 2019-02-05 stsp int diff_context;
4296 3fc0c068 2019-02-10 stsp const char *id_str;
4297 3fc0c068 2019-02-10 stsp int header_shown;
4298 98eaaa12 2019-08-03 stsp int diff_staged;
4299 63035f9f 2019-10-06 stsp int ignore_whitespace;
4300 64453f7e 2020-11-21 stsp int force_text_diff;
4301 b72f483a 2019-02-05 stsp };
4302 39449a05 2020-07-23 stsp
4303 39449a05 2020-07-23 stsp /*
4304 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
4305 39449a05 2020-07-23 stsp * it as content to the diff engine.
4306 39449a05 2020-07-23 stsp */
4307 39449a05 2020-07-23 stsp static const struct got_error *
4308 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4309 39449a05 2020-07-23 stsp const char *abspath)
4310 39449a05 2020-07-23 stsp {
4311 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
4312 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
4313 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
4314 39449a05 2020-07-23 stsp
4315 39449a05 2020-07-23 stsp *fd = -1;
4316 39449a05 2020-07-23 stsp
4317 39449a05 2020-07-23 stsp if (dirfd != -1) {
4318 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4319 39449a05 2020-07-23 stsp if (target_len == -1)
4320 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
4321 39449a05 2020-07-23 stsp } else {
4322 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
4323 39449a05 2020-07-23 stsp if (target_len == -1)
4324 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
4325 39449a05 2020-07-23 stsp }
4326 39449a05 2020-07-23 stsp
4327 39449a05 2020-07-23 stsp *fd = got_opentempfd();
4328 39449a05 2020-07-23 stsp if (*fd == -1)
4329 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
4330 39449a05 2020-07-23 stsp
4331 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
4332 39449a05 2020-07-23 stsp if (outlen == -1) {
4333 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
4334 39449a05 2020-07-23 stsp goto done;
4335 39449a05 2020-07-23 stsp }
4336 39449a05 2020-07-23 stsp
4337 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
4338 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
4339 39449a05 2020-07-23 stsp goto done;
4340 39449a05 2020-07-23 stsp }
4341 39449a05 2020-07-23 stsp done:
4342 39449a05 2020-07-23 stsp if (err) {
4343 39449a05 2020-07-23 stsp close(*fd);
4344 39449a05 2020-07-23 stsp *fd = -1;
4345 39449a05 2020-07-23 stsp }
4346 39449a05 2020-07-23 stsp return err;
4347 39449a05 2020-07-23 stsp }
4348 b72f483a 2019-02-05 stsp
4349 4ed7e80c 2018-05-20 stsp static const struct got_error *
4350 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
4351 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4352 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4353 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4354 b72f483a 2019-02-05 stsp {
4355 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
4356 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
4357 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
4358 12463d8b 2019-12-13 stsp int fd = -1;
4359 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
4360 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
4361 b72f483a 2019-02-05 stsp struct stat sb;
4362 b72f483a 2019-02-05 stsp
4363 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4364 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4365 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4366 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4367 98eaaa12 2019-08-03 stsp return NULL;
4368 98eaaa12 2019-08-03 stsp } else {
4369 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4370 98eaaa12 2019-08-03 stsp return NULL;
4371 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4372 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
4373 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4374 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
4375 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
4376 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
4377 98eaaa12 2019-08-03 stsp return NULL;
4378 98eaaa12 2019-08-03 stsp }
4379 3fc0c068 2019-02-10 stsp
4380 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
4381 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
4382 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
4383 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
4384 3fc0c068 2019-02-10 stsp a->header_shown = 1;
4385 3fc0c068 2019-02-10 stsp }
4386 b72f483a 2019-02-05 stsp
4387 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4388 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
4389 98eaaa12 2019-08-03 stsp switch (staged_status) {
4390 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
4391 98eaaa12 2019-08-03 stsp label1 = path;
4392 98eaaa12 2019-08-03 stsp label2 = path;
4393 98eaaa12 2019-08-03 stsp break;
4394 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
4395 98eaaa12 2019-08-03 stsp label2 = path;
4396 98eaaa12 2019-08-03 stsp break;
4397 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
4398 98eaaa12 2019-08-03 stsp label1 = path;
4399 98eaaa12 2019-08-03 stsp break;
4400 98eaaa12 2019-08-03 stsp default:
4401 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
4402 98eaaa12 2019-08-03 stsp }
4403 fe621944 2020-11-10 stsp return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4404 fe621944 2020-11-10 stsp staged_blob_id, label1, label2, a->diff_context,
4405 64453f7e 2020-11-21 stsp a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4406 98eaaa12 2019-08-03 stsp }
4407 98eaaa12 2019-08-03 stsp
4408 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4409 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
4410 4ce46740 2019-08-08 stsp char *id_str;
4411 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4412 408b4ebc 2019-08-03 stsp 8192);
4413 4ce46740 2019-08-08 stsp if (err)
4414 4ce46740 2019-08-08 stsp goto done;
4415 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4416 4ce46740 2019-08-08 stsp if (err)
4417 4ce46740 2019-08-08 stsp goto done;
4418 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4419 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4420 4ce46740 2019-08-08 stsp free(id_str);
4421 4ce46740 2019-08-08 stsp goto done;
4422 4ce46740 2019-08-08 stsp }
4423 4ce46740 2019-08-08 stsp free(id_str);
4424 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4425 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4426 4ce46740 2019-08-08 stsp if (err)
4427 4ce46740 2019-08-08 stsp goto done;
4428 4ce46740 2019-08-08 stsp }
4429 d00136be 2019-03-26 stsp
4430 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4431 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4432 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4433 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4434 2ec1f75b 2019-03-26 stsp goto done;
4435 2ec1f75b 2019-03-26 stsp }
4436 b72f483a 2019-02-05 stsp
4437 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4438 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4439 12463d8b 2019-12-13 stsp if (fd == -1) {
4440 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4441 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4442 39449a05 2020-07-23 stsp abspath);
4443 39449a05 2020-07-23 stsp goto done;
4444 39449a05 2020-07-23 stsp }
4445 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4446 39449a05 2020-07-23 stsp de_name, abspath);
4447 39449a05 2020-07-23 stsp if (err)
4448 39449a05 2020-07-23 stsp goto done;
4449 12463d8b 2019-12-13 stsp }
4450 12463d8b 2019-12-13 stsp } else {
4451 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4452 12463d8b 2019-12-13 stsp if (fd == -1) {
4453 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4454 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4455 39449a05 2020-07-23 stsp abspath);
4456 39449a05 2020-07-23 stsp goto done;
4457 39449a05 2020-07-23 stsp }
4458 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4459 39449a05 2020-07-23 stsp de_name, abspath);
4460 39449a05 2020-07-23 stsp if (err)
4461 39449a05 2020-07-23 stsp goto done;
4462 12463d8b 2019-12-13 stsp }
4463 12463d8b 2019-12-13 stsp }
4464 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4465 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4466 2ec1f75b 2019-03-26 stsp goto done;
4467 2ec1f75b 2019-03-26 stsp }
4468 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4469 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4470 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4471 2ec1f75b 2019-03-26 stsp goto done;
4472 2ec1f75b 2019-03-26 stsp }
4473 12463d8b 2019-12-13 stsp fd = -1;
4474 2ec1f75b 2019-03-26 stsp } else
4475 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4476 b72f483a 2019-02-05 stsp
4477 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4478 64453f7e 2020-11-21 stsp a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4479 b72f483a 2019-02-05 stsp done:
4480 b72f483a 2019-02-05 stsp if (blob1)
4481 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4482 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4483 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4484 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4485 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4486 b72f483a 2019-02-05 stsp free(abspath);
4487 927df6b7 2019-02-10 stsp return err;
4488 927df6b7 2019-02-10 stsp }
4489 927df6b7 2019-02-10 stsp
4490 927df6b7 2019-02-10 stsp static const struct got_error *
4491 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4492 b00d56cd 2018-04-01 stsp {
4493 b00d56cd 2018-04-01 stsp const struct got_error *error;
4494 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4495 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4496 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4497 cc8021af 2021-10-12 thomas const char *commit_args[2] = { NULL, NULL };
4498 cc8021af 2021-10-12 thomas int ncommit_args = 0;
4499 30f6c0c6 2021-10-08 thomas struct got_object_id *ids[2] = { NULL, NULL };
4500 30f6c0c6 2021-10-08 thomas char *labels[2] = { NULL, NULL };
4501 cc8021af 2021-10-12 thomas int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4502 30f6c0c6 2021-10-08 thomas int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4503 30f6c0c6 2021-10-08 thomas int force_text_diff = 0, force_path = 0, rflag = 0;
4504 c0cc5c62 2018-10-18 stsp const char *errstr;
4505 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4506 30f6c0c6 2021-10-08 thomas struct got_pathlist_head paths;
4507 30f6c0c6 2021-10-08 thomas struct got_pathlist_entry *pe;
4508 b00d56cd 2018-04-01 stsp
4509 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4510 30f6c0c6 2021-10-08 thomas TAILQ_INIT(&paths);
4511 84de9106 2020-12-26 stsp
4512 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4513 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4514 25eccc22 2019-01-04 stsp NULL) == -1)
4515 b00d56cd 2018-04-01 stsp err(1, "pledge");
4516 b00d56cd 2018-04-01 stsp #endif
4517 b00d56cd 2018-04-01 stsp
4518 cc8021af 2021-10-12 thomas while ((ch = getopt(argc, argv, "ac:C:r:swP")) != -1) {
4519 b00d56cd 2018-04-01 stsp switch (ch) {
4520 64453f7e 2020-11-21 stsp case 'a':
4521 64453f7e 2020-11-21 stsp force_text_diff = 1;
4522 64453f7e 2020-11-21 stsp break;
4523 cc8021af 2021-10-12 thomas case 'c':
4524 cc8021af 2021-10-12 thomas if (ncommit_args >= 2)
4525 cc8021af 2021-10-12 thomas errx(1, "too many -c options used");
4526 cc8021af 2021-10-12 thomas commit_args[ncommit_args++] = optarg;
4527 cc8021af 2021-10-12 thomas break;
4528 c0cc5c62 2018-10-18 stsp case 'C':
4529 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4530 dfcab68b 2019-11-29 kn &errstr);
4531 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4532 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4533 c0cc5c62 2018-10-18 stsp break;
4534 b72f483a 2019-02-05 stsp case 'r':
4535 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4536 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4537 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4538 9ba1d308 2019-10-21 stsp optarg);
4539 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4540 30f6c0c6 2021-10-08 thomas rflag = 1;
4541 b72f483a 2019-02-05 stsp break;
4542 98eaaa12 2019-08-03 stsp case 's':
4543 98eaaa12 2019-08-03 stsp diff_staged = 1;
4544 63035f9f 2019-10-06 stsp break;
4545 63035f9f 2019-10-06 stsp case 'w':
4546 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4547 98eaaa12 2019-08-03 stsp break;
4548 30f6c0c6 2021-10-08 thomas case 'P':
4549 30f6c0c6 2021-10-08 thomas force_path = 1;
4550 30f6c0c6 2021-10-08 thomas break;
4551 b00d56cd 2018-04-01 stsp default:
4552 2deda0b9 2019-03-07 stsp usage_diff();
4553 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4554 b00d56cd 2018-04-01 stsp }
4555 b00d56cd 2018-04-01 stsp }
4556 b00d56cd 2018-04-01 stsp
4557 b00d56cd 2018-04-01 stsp argc -= optind;
4558 b00d56cd 2018-04-01 stsp argv += optind;
4559 b00d56cd 2018-04-01 stsp
4560 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4561 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4562 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4563 b72f483a 2019-02-05 stsp goto done;
4564 b72f483a 2019-02-05 stsp }
4565 30f6c0c6 2021-10-08 thomas
4566 30f6c0c6 2021-10-08 thomas if (repo_path == NULL) {
4567 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4568 30f6c0c6 2021-10-08 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
4569 1f03b8da 2020-03-20 stsp goto done;
4570 30f6c0c6 2021-10-08 thomas else
4571 30f6c0c6 2021-10-08 thomas error = NULL;
4572 30f6c0c6 2021-10-08 thomas if (worktree) {
4573 30f6c0c6 2021-10-08 thomas repo_path =
4574 30f6c0c6 2021-10-08 thomas strdup(got_worktree_get_repo_path(worktree));
4575 30f6c0c6 2021-10-08 thomas if (repo_path == NULL) {
4576 30f6c0c6 2021-10-08 thomas error = got_error_from_errno("strdup");
4577 927df6b7 2019-02-10 stsp goto done;
4578 30f6c0c6 2021-10-08 thomas }
4579 927df6b7 2019-02-10 stsp } else {
4580 30f6c0c6 2021-10-08 thomas repo_path = strdup(cwd);
4581 1a1242a9 2021-04-01 kn if (repo_path == NULL) {
4582 1a1242a9 2021-04-01 kn error = got_error_from_errno("strdup");
4583 1a1242a9 2021-04-01 kn goto done;
4584 30db809c 2019-06-05 stsp }
4585 30db809c 2019-06-05 stsp }
4586 30f6c0c6 2021-10-08 thomas }
4587 b00d56cd 2018-04-01 stsp
4588 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4589 76089277 2018-04-01 stsp free(repo_path);
4590 b00d56cd 2018-04-01 stsp if (error != NULL)
4591 b00d56cd 2018-04-01 stsp goto done;
4592 cc8021af 2021-10-12 thomas
4593 cc8021af 2021-10-12 thomas if (rflag || worktree == NULL || ncommit_args > 0) {
4594 cc8021af 2021-10-12 thomas if (force_path) {
4595 cc8021af 2021-10-12 thomas error = got_error_msg(GOT_ERR_NOT_IMPL,
4596 cc8021af 2021-10-12 thomas "-P option can only be used when diffing "
4597 cc8021af 2021-10-12 thomas "a work tree");
4598 cc8021af 2021-10-12 thomas goto done;
4599 cc8021af 2021-10-12 thomas }
4600 cc8021af 2021-10-12 thomas if (diff_staged) {
4601 cc8021af 2021-10-12 thomas error = got_error_msg(GOT_ERR_NOT_IMPL,
4602 cc8021af 2021-10-12 thomas "-s option can only be used when diffing "
4603 cc8021af 2021-10-12 thomas "a work tree");
4604 cc8021af 2021-10-12 thomas goto done;
4605 cc8021af 2021-10-12 thomas }
4606 cc8021af 2021-10-12 thomas }
4607 b00d56cd 2018-04-01 stsp
4608 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4609 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4610 c02c541e 2019-03-29 stsp if (error)
4611 c02c541e 2019-03-29 stsp goto done;
4612 c02c541e 2019-03-29 stsp
4613 cc8021af 2021-10-12 thomas if ((!force_path && argc == 2) || ncommit_args > 0) {
4614 cc8021af 2021-10-12 thomas int obj_type = (ncommit_args > 0 ?
4615 cc8021af 2021-10-12 thomas GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
4616 30f6c0c6 2021-10-08 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4617 30f6c0c6 2021-10-08 thomas NULL);
4618 30f6c0c6 2021-10-08 thomas if (error)
4619 30f6c0c6 2021-10-08 thomas goto done;
4620 cc8021af 2021-10-12 thomas for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
4621 cc8021af 2021-10-12 thomas const char *arg;
4622 cc8021af 2021-10-12 thomas if (ncommit_args > 0)
4623 cc8021af 2021-10-12 thomas arg = commit_args[i];
4624 cc8021af 2021-10-12 thomas else
4625 cc8021af 2021-10-12 thomas arg = argv[i];
4626 30f6c0c6 2021-10-08 thomas error = got_repo_match_object_id(&ids[i], &labels[i],
4627 cc8021af 2021-10-12 thomas arg, obj_type, &refs, repo);
4628 30f6c0c6 2021-10-08 thomas if (error) {
4629 30f6c0c6 2021-10-08 thomas if (error->code != GOT_ERR_NOT_REF &&
4630 30f6c0c6 2021-10-08 thomas error->code != GOT_ERR_NO_OBJ)
4631 30f6c0c6 2021-10-08 thomas goto done;
4632 cc8021af 2021-10-12 thomas if (ncommit_args > 0)
4633 cc8021af 2021-10-12 thomas goto done;
4634 30f6c0c6 2021-10-08 thomas error = NULL;
4635 30f6c0c6 2021-10-08 thomas break;
4636 30f6c0c6 2021-10-08 thomas }
4637 30f6c0c6 2021-10-08 thomas }
4638 30f6c0c6 2021-10-08 thomas }
4639 30f6c0c6 2021-10-08 thomas
4640 cc8021af 2021-10-12 thomas if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
4641 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4642 b72f483a 2019-02-05 stsp char *id_str;
4643 72ea6654 2019-07-27 stsp
4644 cc8021af 2021-10-12 thomas if (worktree == NULL) {
4645 cc8021af 2021-10-12 thomas if (argc == 2 && ids[0] == NULL) {
4646 cc8021af 2021-10-12 thomas error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
4647 cc8021af 2021-10-12 thomas goto done;
4648 cc8021af 2021-10-12 thomas } else if (argc == 2 && ids[1] == NULL) {
4649 cc8021af 2021-10-12 thomas error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
4650 cc8021af 2021-10-12 thomas goto done;
4651 cc8021af 2021-10-12 thomas } else if (argc > 0) {
4652 cc8021af 2021-10-12 thomas error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
4653 cc8021af 2021-10-12 thomas "%s", "specified paths cannot be resolved");
4654 cc8021af 2021-10-12 thomas goto done;
4655 cc8021af 2021-10-12 thomas } else {
4656 cc8021af 2021-10-12 thomas error = got_error(GOT_ERR_NOT_WORKTREE);
4657 cc8021af 2021-10-12 thomas goto done;
4658 cc8021af 2021-10-12 thomas }
4659 cc8021af 2021-10-12 thomas }
4660 cc8021af 2021-10-12 thomas
4661 cc8021af 2021-10-12 thomas error = get_worktree_paths_from_argv(&paths, argc, argv,
4662 cc8021af 2021-10-12 thomas worktree);
4663 cc8021af 2021-10-12 thomas if (error)
4664 cc8021af 2021-10-12 thomas goto done;
4665 cc8021af 2021-10-12 thomas
4666 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4667 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4668 b72f483a 2019-02-05 stsp if (error)
4669 b72f483a 2019-02-05 stsp goto done;
4670 b72f483a 2019-02-05 stsp arg.repo = repo;
4671 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4672 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4673 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4674 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4675 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4676 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4677 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
4678 b72f483a 2019-02-05 stsp
4679 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
4680 f6343036 2021-06-22 stsp print_diff, &arg, check_cancelled, NULL);
4681 b72f483a 2019-02-05 stsp free(id_str);
4682 b72f483a 2019-02-05 stsp goto done;
4683 b72f483a 2019-02-05 stsp }
4684 84de9106 2020-12-26 stsp
4685 cc8021af 2021-10-12 thomas if (ncommit_args == 1) {
4686 cc8021af 2021-10-12 thomas struct got_commit_object *commit;
4687 cc8021af 2021-10-12 thomas error = got_object_open_as_commit(&commit, repo, ids[0]);
4688 cc8021af 2021-10-12 thomas if (error)
4689 30f6c0c6 2021-10-08 thomas goto done;
4690 b00d56cd 2018-04-01 stsp
4691 cc8021af 2021-10-12 thomas labels[1] = labels[0];
4692 cc8021af 2021-10-12 thomas ids[1] = ids[0];
4693 cc8021af 2021-10-12 thomas if (got_object_commit_get_nparents(commit) > 0) {
4694 cc8021af 2021-10-12 thomas const struct got_object_id_queue *pids;
4695 cc8021af 2021-10-12 thomas struct got_object_qid *pid;
4696 cc8021af 2021-10-12 thomas pids = got_object_commit_get_parent_ids(commit);
4697 cc8021af 2021-10-12 thomas pid = STAILQ_FIRST(pids);
4698 cc8021af 2021-10-12 thomas ids[0] = got_object_id_dup(pid->id);
4699 cc8021af 2021-10-12 thomas if (ids[0] == NULL) {
4700 cc8021af 2021-10-12 thomas error = got_error_from_errno(
4701 cc8021af 2021-10-12 thomas "got_object_id_dup");
4702 cc8021af 2021-10-12 thomas got_object_commit_close(commit);
4703 cc8021af 2021-10-12 thomas goto done;
4704 cc8021af 2021-10-12 thomas }
4705 cc8021af 2021-10-12 thomas error = got_object_id_str(&labels[0], ids[0]);
4706 cc8021af 2021-10-12 thomas if (error) {
4707 cc8021af 2021-10-12 thomas got_object_commit_close(commit);
4708 cc8021af 2021-10-12 thomas goto done;
4709 cc8021af 2021-10-12 thomas }
4710 cc8021af 2021-10-12 thomas } else {
4711 cc8021af 2021-10-12 thomas ids[0] = NULL;
4712 cc8021af 2021-10-12 thomas labels[0] = strdup("/dev/null");
4713 cc8021af 2021-10-12 thomas if (labels[0] == NULL) {
4714 cc8021af 2021-10-12 thomas error = got_error_from_errno("strdup");
4715 cc8021af 2021-10-12 thomas got_object_commit_close(commit);
4716 cc8021af 2021-10-12 thomas goto done;
4717 cc8021af 2021-10-12 thomas }
4718 cc8021af 2021-10-12 thomas }
4719 cc8021af 2021-10-12 thomas
4720 cc8021af 2021-10-12 thomas got_object_commit_close(commit);
4721 cc8021af 2021-10-12 thomas }
4722 cc8021af 2021-10-12 thomas
4723 cc8021af 2021-10-12 thomas if (ncommit_args == 0 && argc > 2) {
4724 cc8021af 2021-10-12 thomas error = got_error_msg(GOT_ERR_BAD_PATH,
4725 cc8021af 2021-10-12 thomas "path arguments cannot be used when diffing two objects");
4726 0adb7196 2019-08-11 stsp goto done;
4727 cc8021af 2021-10-12 thomas }
4728 b00d56cd 2018-04-01 stsp
4729 cc8021af 2021-10-12 thomas if (ids[0]) {
4730 cc8021af 2021-10-12 thomas error = got_object_get_type(&type1, repo, ids[0]);
4731 cc8021af 2021-10-12 thomas if (error)
4732 cc8021af 2021-10-12 thomas goto done;
4733 cc8021af 2021-10-12 thomas }
4734 cc8021af 2021-10-12 thomas
4735 30f6c0c6 2021-10-08 thomas error = got_object_get_type(&type2, repo, ids[1]);
4736 15a94983 2018-12-23 stsp if (error)
4737 15a94983 2018-12-23 stsp goto done;
4738 cc8021af 2021-10-12 thomas if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
4739 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4740 b00d56cd 2018-04-01 stsp goto done;
4741 b00d56cd 2018-04-01 stsp }
4742 cc8021af 2021-10-12 thomas if (type1 == GOT_OBJ_TYPE_BLOB && argc > 0) {
4743 cc8021af 2021-10-12 thomas error = got_error_msg(GOT_ERR_OBJ_TYPE,
4744 cc8021af 2021-10-12 thomas "path arguments cannot be used when diffing blobs");
4745 cc8021af 2021-10-12 thomas goto done;
4746 cc8021af 2021-10-12 thomas }
4747 b00d56cd 2018-04-01 stsp
4748 cc8021af 2021-10-12 thomas for (i = 0; ncommit_args > 0 && i < argc; i++) {
4749 cc8021af 2021-10-12 thomas char *in_repo_path;
4750 cc8021af 2021-10-12 thomas struct got_pathlist_entry *new;
4751 cc8021af 2021-10-12 thomas if (worktree) {
4752 cc8021af 2021-10-12 thomas const char *prefix;
4753 cc8021af 2021-10-12 thomas char *p;
4754 cc8021af 2021-10-12 thomas error = got_worktree_resolve_path(&p, worktree,
4755 cc8021af 2021-10-12 thomas argv[i]);
4756 cc8021af 2021-10-12 thomas if (error)
4757 cc8021af 2021-10-12 thomas goto done;
4758 cc8021af 2021-10-12 thomas prefix = got_worktree_get_path_prefix(worktree);
4759 cc8021af 2021-10-12 thomas while (prefix[0] == '/')
4760 cc8021af 2021-10-12 thomas prefix++;
4761 cc8021af 2021-10-12 thomas if (asprintf(&in_repo_path, "%s%s%s", prefix,
4762 cc8021af 2021-10-12 thomas (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
4763 cc8021af 2021-10-12 thomas p) == -1) {
4764 cc8021af 2021-10-12 thomas error = got_error_from_errno("asprintf");
4765 cc8021af 2021-10-12 thomas free(p);
4766 cc8021af 2021-10-12 thomas goto done;
4767 cc8021af 2021-10-12 thomas }
4768 cc8021af 2021-10-12 thomas free(p);
4769 cc8021af 2021-10-12 thomas } else {
4770 cc8021af 2021-10-12 thomas char *mapped_path, *s;
4771 cc8021af 2021-10-12 thomas error = got_repo_map_path(&mapped_path, repo, argv[i]);
4772 cc8021af 2021-10-12 thomas if (error)
4773 cc8021af 2021-10-12 thomas goto done;
4774 cc8021af 2021-10-12 thomas s = mapped_path;
4775 cc8021af 2021-10-12 thomas while (s[0] == '/')
4776 cc8021af 2021-10-12 thomas s++;
4777 cc8021af 2021-10-12 thomas in_repo_path = strdup(s);
4778 cc8021af 2021-10-12 thomas if (in_repo_path == NULL) {
4779 cc8021af 2021-10-12 thomas error = got_error_from_errno("asprintf");
4780 cc8021af 2021-10-12 thomas free(mapped_path);
4781 cc8021af 2021-10-12 thomas goto done;
4782 cc8021af 2021-10-12 thomas }
4783 cc8021af 2021-10-12 thomas free(mapped_path);
4784 cc8021af 2021-10-12 thomas
4785 cc8021af 2021-10-12 thomas }
4786 cc8021af 2021-10-12 thomas error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
4787 cc8021af 2021-10-12 thomas if (error || new == NULL /* duplicate */)
4788 cc8021af 2021-10-12 thomas free(in_repo_path);
4789 cc8021af 2021-10-12 thomas if (error)
4790 cc8021af 2021-10-12 thomas goto done;
4791 cc8021af 2021-10-12 thomas }
4792 cc8021af 2021-10-12 thomas
4793 cc8021af 2021-10-12 thomas switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
4794 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4795 30f6c0c6 2021-10-08 thomas error = got_diff_objects_as_blobs(NULL, NULL, ids[0], ids[1],
4796 64453f7e 2020-11-21 stsp NULL, NULL, diff_context, ignore_whitespace,
4797 64453f7e 2020-11-21 stsp force_text_diff, repo, stdout);
4798 b00d56cd 2018-04-01 stsp break;
4799 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4800 30f6c0c6 2021-10-08 thomas error = got_diff_objects_as_trees(NULL, NULL, ids[0], ids[1],
4801 cc8021af 2021-10-12 thomas &paths, "", "", diff_context, ignore_whitespace,
4802 cc8021af 2021-10-12 thomas force_text_diff, repo, stdout);
4803 b00d56cd 2018-04-01 stsp break;
4804 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4805 30f6c0c6 2021-10-08 thomas printf("diff %s %s\n", labels[0], labels[1]);
4806 30f6c0c6 2021-10-08 thomas error = got_diff_objects_as_commits(NULL, NULL, ids[0], ids[1],
4807 cc8021af 2021-10-12 thomas &paths, diff_context, ignore_whitespace, force_text_diff,
4808 cc8021af 2021-10-12 thomas repo, stdout);
4809 b00d56cd 2018-04-01 stsp break;
4810 b00d56cd 2018-04-01 stsp default:
4811 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4812 b00d56cd 2018-04-01 stsp }
4813 b00d56cd 2018-04-01 stsp done:
4814 30f6c0c6 2021-10-08 thomas free(labels[0]);
4815 30f6c0c6 2021-10-08 thomas free(labels[1]);
4816 30f6c0c6 2021-10-08 thomas free(ids[0]);
4817 30f6c0c6 2021-10-08 thomas free(ids[1]);
4818 b72f483a 2019-02-05 stsp if (worktree)
4819 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4820 ad242220 2018-09-08 stsp if (repo) {
4821 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4822 ad242220 2018-09-08 stsp if (error == NULL)
4823 1d0f4054 2021-06-17 stsp error = close_err;
4824 ad242220 2018-09-08 stsp }
4825 30f6c0c6 2021-10-08 thomas TAILQ_FOREACH(pe, &paths, entry)
4826 30f6c0c6 2021-10-08 thomas free((char *)pe->path);
4827 30f6c0c6 2021-10-08 thomas got_pathlist_free(&paths);
4828 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4829 b00d56cd 2018-04-01 stsp return error;
4830 404c43c4 2018-06-21 stsp }
4831 404c43c4 2018-06-21 stsp
4832 404c43c4 2018-06-21 stsp __dead static void
4833 404c43c4 2018-06-21 stsp usage_blame(void)
4834 404c43c4 2018-06-21 stsp {
4835 9270e621 2019-02-05 stsp fprintf(stderr,
4836 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4837 404c43c4 2018-06-21 stsp getprogname());
4838 404c43c4 2018-06-21 stsp exit(1);
4839 b00d56cd 2018-04-01 stsp }
4840 b00d56cd 2018-04-01 stsp
4841 28315671 2019-08-14 stsp struct blame_line {
4842 28315671 2019-08-14 stsp int annotated;
4843 28315671 2019-08-14 stsp char *id_str;
4844 82f6abb8 2019-08-14 stsp char *committer;
4845 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4846 28315671 2019-08-14 stsp };
4847 28315671 2019-08-14 stsp
4848 28315671 2019-08-14 stsp struct blame_cb_args {
4849 28315671 2019-08-14 stsp struct blame_line *lines;
4850 28315671 2019-08-14 stsp int nlines;
4851 7ef28ff8 2019-08-14 stsp int nlines_prec;
4852 28315671 2019-08-14 stsp int lineno_cur;
4853 28315671 2019-08-14 stsp off_t *line_offsets;
4854 28315671 2019-08-14 stsp FILE *f;
4855 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4856 28315671 2019-08-14 stsp };
4857 28315671 2019-08-14 stsp
4858 404c43c4 2018-06-21 stsp static const struct got_error *
4859 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4860 28315671 2019-08-14 stsp {
4861 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4862 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4863 28315671 2019-08-14 stsp struct blame_line *bline;
4864 82f6abb8 2019-08-14 stsp char *line = NULL;
4865 28315671 2019-08-14 stsp size_t linesize = 0;
4866 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4867 28315671 2019-08-14 stsp off_t offset;
4868 bcb49d15 2019-08-14 stsp struct tm tm;
4869 bcb49d15 2019-08-14 stsp time_t committer_time;
4870 28315671 2019-08-14 stsp
4871 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4872 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4873 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4874 28315671 2019-08-14 stsp
4875 28315671 2019-08-14 stsp if (sigint_received)
4876 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4877 28315671 2019-08-14 stsp
4878 2839f8b9 2019-08-18 stsp if (lineno == -1)
4879 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4880 2839f8b9 2019-08-18 stsp
4881 28315671 2019-08-14 stsp /* Annotate this line. */
4882 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4883 28315671 2019-08-14 stsp if (bline->annotated)
4884 28315671 2019-08-14 stsp return NULL;
4885 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4886 28315671 2019-08-14 stsp if (err)
4887 28315671 2019-08-14 stsp return err;
4888 82f6abb8 2019-08-14 stsp
4889 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4890 82f6abb8 2019-08-14 stsp if (err)
4891 82f6abb8 2019-08-14 stsp goto done;
4892 82f6abb8 2019-08-14 stsp
4893 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4894 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4895 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4896 bcb49d15 2019-08-14 stsp goto done;
4897 bcb49d15 2019-08-14 stsp }
4898 bcb49d15 2019-08-14 stsp
4899 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4900 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
4901 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
4902 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4903 ec6d1a36 2021-03-21 jrick &tm) == 0) {
4904 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4905 82f6abb8 2019-08-14 stsp goto done;
4906 82f6abb8 2019-08-14 stsp }
4907 28315671 2019-08-14 stsp bline->annotated = 1;
4908 28315671 2019-08-14 stsp
4909 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4910 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4911 28315671 2019-08-14 stsp if (!bline->annotated)
4912 82f6abb8 2019-08-14 stsp goto done;
4913 28315671 2019-08-14 stsp
4914 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4915 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4916 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4917 82f6abb8 2019-08-14 stsp goto done;
4918 82f6abb8 2019-08-14 stsp }
4919 28315671 2019-08-14 stsp
4920 28315671 2019-08-14 stsp while (bline->annotated) {
4921 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4922 82f6abb8 2019-08-14 stsp size_t len;
4923 82f6abb8 2019-08-14 stsp
4924 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4925 28315671 2019-08-14 stsp if (ferror(a->f))
4926 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4927 28315671 2019-08-14 stsp break;
4928 28315671 2019-08-14 stsp }
4929 82f6abb8 2019-08-14 stsp
4930 82f6abb8 2019-08-14 stsp committer = bline->committer;
4931 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4932 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4933 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4934 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4935 82f6abb8 2019-08-14 stsp if (at)
4936 82f6abb8 2019-08-14 stsp *at = '\0';
4937 82f6abb8 2019-08-14 stsp len = strlen(committer);
4938 82f6abb8 2019-08-14 stsp if (len >= 9)
4939 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4940 28315671 2019-08-14 stsp
4941 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4942 28315671 2019-08-14 stsp if (nl)
4943 28315671 2019-08-14 stsp *nl = '\0';
4944 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4945 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4946 28315671 2019-08-14 stsp
4947 28315671 2019-08-14 stsp a->lineno_cur++;
4948 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4949 28315671 2019-08-14 stsp }
4950 82f6abb8 2019-08-14 stsp done:
4951 82f6abb8 2019-08-14 stsp if (commit)
4952 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4953 28315671 2019-08-14 stsp free(line);
4954 28315671 2019-08-14 stsp return err;
4955 28315671 2019-08-14 stsp }
4956 28315671 2019-08-14 stsp
4957 28315671 2019-08-14 stsp static const struct got_error *
4958 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4959 404c43c4 2018-06-21 stsp {
4960 404c43c4 2018-06-21 stsp const struct got_error *error;
4961 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4962 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4963 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4964 0587e10c 2020-07-23 stsp char *link_target = NULL;
4965 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4966 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4967 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4968 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4969 28315671 2019-08-14 stsp struct blame_cb_args bca;
4970 28315671 2019-08-14 stsp int ch, obj_type, i;
4971 be659d10 2020-11-18 stsp off_t filesize;
4972 90f3c347 2019-08-19 stsp
4973 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4974 404c43c4 2018-06-21 stsp
4975 404c43c4 2018-06-21 stsp #ifndef PROFILE
4976 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4977 36e2fb66 2019-01-04 stsp NULL) == -1)
4978 404c43c4 2018-06-21 stsp err(1, "pledge");
4979 404c43c4 2018-06-21 stsp #endif
4980 404c43c4 2018-06-21 stsp
4981 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4982 404c43c4 2018-06-21 stsp switch (ch) {
4983 404c43c4 2018-06-21 stsp case 'c':
4984 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4985 404c43c4 2018-06-21 stsp break;
4986 66bea077 2018-08-02 stsp case 'r':
4987 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4988 66bea077 2018-08-02 stsp if (repo_path == NULL)
4989 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4990 9ba1d308 2019-10-21 stsp optarg);
4991 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4992 66bea077 2018-08-02 stsp break;
4993 404c43c4 2018-06-21 stsp default:
4994 2deda0b9 2019-03-07 stsp usage_blame();
4995 404c43c4 2018-06-21 stsp /* NOTREACHED */
4996 404c43c4 2018-06-21 stsp }
4997 404c43c4 2018-06-21 stsp }
4998 404c43c4 2018-06-21 stsp
4999 404c43c4 2018-06-21 stsp argc -= optind;
5000 404c43c4 2018-06-21 stsp argv += optind;
5001 404c43c4 2018-06-21 stsp
5002 a39318fd 2018-08-02 stsp if (argc == 1)
5003 404c43c4 2018-06-21 stsp path = argv[0];
5004 a39318fd 2018-08-02 stsp else
5005 404c43c4 2018-06-21 stsp usage_blame();
5006 404c43c4 2018-06-21 stsp
5007 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
5008 66bea077 2018-08-02 stsp if (cwd == NULL) {
5009 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5010 66bea077 2018-08-02 stsp goto done;
5011 66bea077 2018-08-02 stsp }
5012 66bea077 2018-08-02 stsp if (repo_path == NULL) {
5013 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5014 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5015 66bea077 2018-08-02 stsp goto done;
5016 0c06baac 2019-02-05 stsp else
5017 0c06baac 2019-02-05 stsp error = NULL;
5018 0c06baac 2019-02-05 stsp if (worktree) {
5019 0c06baac 2019-02-05 stsp repo_path =
5020 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5021 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
5022 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5023 1f03b8da 2020-03-20 stsp if (error)
5024 1f03b8da 2020-03-20 stsp goto done;
5025 1f03b8da 2020-03-20 stsp }
5026 0c06baac 2019-02-05 stsp } else {
5027 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
5028 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
5029 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5030 0c06baac 2019-02-05 stsp goto done;
5031 0c06baac 2019-02-05 stsp }
5032 66bea077 2018-08-02 stsp }
5033 66bea077 2018-08-02 stsp }
5034 36e2fb66 2019-01-04 stsp
5035 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5036 c02c541e 2019-03-29 stsp if (error != NULL)
5037 404c43c4 2018-06-21 stsp goto done;
5038 404c43c4 2018-06-21 stsp
5039 0c06baac 2019-02-05 stsp if (worktree) {
5040 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
5041 01740607 2020-11-04 stsp char *p;
5042 01740607 2020-11-04 stsp
5043 01740607 2020-11-04 stsp error = got_worktree_resolve_path(&p, worktree, path);
5044 01740607 2020-11-04 stsp if (error)
5045 01740607 2020-11-04 stsp goto done;
5046 01740607 2020-11-04 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
5047 01740607 2020-11-04 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5048 01740607 2020-11-04 stsp p) == -1) {
5049 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
5050 01740607 2020-11-04 stsp free(p);
5051 0c06baac 2019-02-05 stsp goto done;
5052 0c06baac 2019-02-05 stsp }
5053 0c06baac 2019-02-05 stsp free(p);
5054 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5055 0c06baac 2019-02-05 stsp } else {
5056 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5057 01740607 2020-11-04 stsp if (error)
5058 01740607 2020-11-04 stsp goto done;
5059 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
5060 0c06baac 2019-02-05 stsp }
5061 0c06baac 2019-02-05 stsp if (error)
5062 66bea077 2018-08-02 stsp goto done;
5063 66bea077 2018-08-02 stsp
5064 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
5065 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
5066 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5067 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5068 404c43c4 2018-06-21 stsp if (error != NULL)
5069 66bea077 2018-08-02 stsp goto done;
5070 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5071 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
5072 404c43c4 2018-06-21 stsp if (error != NULL)
5073 66bea077 2018-08-02 stsp goto done;
5074 404c43c4 2018-06-21 stsp } else {
5075 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5076 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5077 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5078 84de9106 2020-12-26 stsp NULL);
5079 84de9106 2020-12-26 stsp if (error)
5080 84de9106 2020-12-26 stsp goto done;
5081 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5082 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5083 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5084 30837e32 2019-07-25 stsp if (error)
5085 66bea077 2018-08-02 stsp goto done;
5086 404c43c4 2018-06-21 stsp }
5087 404c43c4 2018-06-21 stsp
5088 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5089 0587e10c 2020-07-23 stsp commit_id, repo);
5090 0587e10c 2020-07-23 stsp if (error)
5091 0587e10c 2020-07-23 stsp goto done;
5092 0587e10c 2020-07-23 stsp
5093 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
5094 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
5095 28315671 2019-08-14 stsp if (error)
5096 28315671 2019-08-14 stsp goto done;
5097 28315671 2019-08-14 stsp
5098 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
5099 28315671 2019-08-14 stsp if (error)
5100 28315671 2019-08-14 stsp goto done;
5101 28315671 2019-08-14 stsp
5102 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5103 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
5104 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
5105 28315671 2019-08-14 stsp goto done;
5106 28315671 2019-08-14 stsp }
5107 28315671 2019-08-14 stsp
5108 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
5109 28315671 2019-08-14 stsp if (error)
5110 28315671 2019-08-14 stsp goto done;
5111 28315671 2019-08-14 stsp bca.f = got_opentemp();
5112 28315671 2019-08-14 stsp if (bca.f == NULL) {
5113 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
5114 28315671 2019-08-14 stsp goto done;
5115 28315671 2019-08-14 stsp }
5116 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5117 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
5118 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
5119 28315671 2019-08-14 stsp goto done;
5120 28315671 2019-08-14 stsp
5121 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5122 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
5123 b02560ec 2019-08-19 stsp bca.nlines--;
5124 b02560ec 2019-08-19 stsp
5125 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5126 28315671 2019-08-14 stsp if (bca.lines == NULL) {
5127 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
5128 28315671 2019-08-14 stsp goto done;
5129 28315671 2019-08-14 stsp }
5130 28315671 2019-08-14 stsp bca.lineno_cur = 1;
5131 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
5132 7ef28ff8 2019-08-14 stsp i = bca.nlines;
5133 7ef28ff8 2019-08-14 stsp while (i > 0) {
5134 7ef28ff8 2019-08-14 stsp i /= 10;
5135 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
5136 7ef28ff8 2019-08-14 stsp }
5137 82f6abb8 2019-08-14 stsp bca.repo = repo;
5138 7ef28ff8 2019-08-14 stsp
5139 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5140 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
5141 404c43c4 2018-06-21 stsp done:
5142 66bea077 2018-08-02 stsp free(in_repo_path);
5143 0587e10c 2020-07-23 stsp free(link_target);
5144 66bea077 2018-08-02 stsp free(repo_path);
5145 66bea077 2018-08-02 stsp free(cwd);
5146 404c43c4 2018-06-21 stsp free(commit_id);
5147 28315671 2019-08-14 stsp free(obj_id);
5148 28315671 2019-08-14 stsp if (blob)
5149 28315671 2019-08-14 stsp got_object_blob_close(blob);
5150 0c06baac 2019-02-05 stsp if (worktree)
5151 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
5152 ad242220 2018-09-08 stsp if (repo) {
5153 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5154 ad242220 2018-09-08 stsp if (error == NULL)
5155 1d0f4054 2021-06-17 stsp error = close_err;
5156 ad242220 2018-09-08 stsp }
5157 3affba96 2019-09-22 stsp if (bca.lines) {
5158 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
5159 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
5160 3affba96 2019-09-22 stsp free(bline->id_str);
5161 3affba96 2019-09-22 stsp free(bline->committer);
5162 3affba96 2019-09-22 stsp }
5163 3affba96 2019-09-22 stsp free(bca.lines);
5164 28315671 2019-08-14 stsp }
5165 28315671 2019-08-14 stsp free(bca.line_offsets);
5166 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
5167 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
5168 404c43c4 2018-06-21 stsp return error;
5169 5de5890b 2018-10-18 stsp }
5170 5de5890b 2018-10-18 stsp
5171 5de5890b 2018-10-18 stsp __dead static void
5172 5de5890b 2018-10-18 stsp usage_tree(void)
5173 5de5890b 2018-10-18 stsp {
5174 c1669e2e 2019-01-09 stsp fprintf(stderr,
5175 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5176 5de5890b 2018-10-18 stsp getprogname());
5177 5de5890b 2018-10-18 stsp exit(1);
5178 5de5890b 2018-10-18 stsp }
5179 5de5890b 2018-10-18 stsp
5180 0d6c6ee3 2020-05-20 stsp static const struct got_error *
5181 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
5182 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
5183 c1669e2e 2019-01-09 stsp {
5184 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
5185 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
5186 848d6979 2019-08-12 stsp const char *modestr = "";
5187 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
5188 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
5189 5de5890b 2018-10-18 stsp
5190 c1669e2e 2019-01-09 stsp path += strlen(root_path);
5191 c1669e2e 2019-01-09 stsp while (path[0] == '/')
5192 c1669e2e 2019-01-09 stsp path++;
5193 c1669e2e 2019-01-09 stsp
5194 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5195 63c5ca5d 2019-08-24 stsp modestr = "$";
5196 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5197 0d6c6ee3 2020-05-20 stsp int i;
5198 0d6c6ee3 2020-05-20 stsp
5199 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5200 0d6c6ee3 2020-05-20 stsp if (err)
5201 0d6c6ee3 2020-05-20 stsp return err;
5202 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5203 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5204 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5205 0d6c6ee3 2020-05-20 stsp }
5206 0d6c6ee3 2020-05-20 stsp
5207 848d6979 2019-08-12 stsp modestr = "@";
5208 0d6c6ee3 2020-05-20 stsp }
5209 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5210 848d6979 2019-08-12 stsp modestr = "/";
5211 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5212 848d6979 2019-08-12 stsp modestr = "*";
5213 848d6979 2019-08-12 stsp
5214 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5215 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5216 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
5217 0d6c6ee3 2020-05-20 stsp
5218 0d6c6ee3 2020-05-20 stsp free(link_target);
5219 0d6c6ee3 2020-05-20 stsp return NULL;
5220 c1669e2e 2019-01-09 stsp }
5221 c1669e2e 2019-01-09 stsp
5222 5de5890b 2018-10-18 stsp static const struct got_error *
5223 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
5224 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
5225 c1669e2e 2019-01-09 stsp struct got_repository *repo)
5226 5de5890b 2018-10-18 stsp {
5227 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
5228 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
5229 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
5230 56e0773d 2019-11-28 stsp int nentries, i;
5231 5de5890b 2018-10-18 stsp
5232 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
5233 5de5890b 2018-10-18 stsp if (err)
5234 5de5890b 2018-10-18 stsp goto done;
5235 5de5890b 2018-10-18 stsp
5236 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
5237 5de5890b 2018-10-18 stsp if (err)
5238 5de5890b 2018-10-18 stsp goto done;
5239 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
5240 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
5241 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5242 5de5890b 2018-10-18 stsp char *id = NULL;
5243 84453469 2018-11-11 stsp
5244 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
5245 84453469 2018-11-11 stsp break;
5246 84453469 2018-11-11 stsp
5247 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
5248 5de5890b 2018-10-18 stsp if (show_ids) {
5249 5de5890b 2018-10-18 stsp char *id_str;
5250 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5251 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5252 5de5890b 2018-10-18 stsp if (err)
5253 5de5890b 2018-10-18 stsp goto done;
5254 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
5255 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5256 5de5890b 2018-10-18 stsp free(id_str);
5257 5de5890b 2018-10-18 stsp goto done;
5258 5de5890b 2018-10-18 stsp }
5259 5de5890b 2018-10-18 stsp free(id_str);
5260 5de5890b 2018-10-18 stsp }
5261 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
5262 5de5890b 2018-10-18 stsp free(id);
5263 0d6c6ee3 2020-05-20 stsp if (err)
5264 0d6c6ee3 2020-05-20 stsp goto done;
5265 c1669e2e 2019-01-09 stsp
5266 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5267 c1669e2e 2019-01-09 stsp char *child_path;
5268 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
5269 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
5270 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
5271 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5272 c1669e2e 2019-01-09 stsp goto done;
5273 c1669e2e 2019-01-09 stsp }
5274 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
5275 c1669e2e 2019-01-09 stsp root_path, repo);
5276 c1669e2e 2019-01-09 stsp free(child_path);
5277 c1669e2e 2019-01-09 stsp if (err)
5278 c1669e2e 2019-01-09 stsp goto done;
5279 c1669e2e 2019-01-09 stsp }
5280 5de5890b 2018-10-18 stsp }
5281 5de5890b 2018-10-18 stsp done:
5282 5de5890b 2018-10-18 stsp if (tree)
5283 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
5284 5de5890b 2018-10-18 stsp free(tree_id);
5285 5de5890b 2018-10-18 stsp return err;
5286 404c43c4 2018-06-21 stsp }
5287 404c43c4 2018-06-21 stsp
5288 5de5890b 2018-10-18 stsp static const struct got_error *
5289 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
5290 5de5890b 2018-10-18 stsp {
5291 5de5890b 2018-10-18 stsp const struct got_error *error;
5292 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
5293 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
5294 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
5295 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5296 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
5297 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
5298 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
5299 5de5890b 2018-10-18 stsp int ch;
5300 5de5890b 2018-10-18 stsp
5301 5de5890b 2018-10-18 stsp #ifndef PROFILE
5302 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5303 0f8d269b 2019-01-04 stsp NULL) == -1)
5304 5de5890b 2018-10-18 stsp err(1, "pledge");
5305 5de5890b 2018-10-18 stsp #endif
5306 5de5890b 2018-10-18 stsp
5307 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5308 5de5890b 2018-10-18 stsp switch (ch) {
5309 5de5890b 2018-10-18 stsp case 'c':
5310 5de5890b 2018-10-18 stsp commit_id_str = optarg;
5311 5de5890b 2018-10-18 stsp break;
5312 5de5890b 2018-10-18 stsp case 'r':
5313 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
5314 5de5890b 2018-10-18 stsp if (repo_path == NULL)
5315 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5316 9ba1d308 2019-10-21 stsp optarg);
5317 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5318 5de5890b 2018-10-18 stsp break;
5319 5de5890b 2018-10-18 stsp case 'i':
5320 5de5890b 2018-10-18 stsp show_ids = 1;
5321 5de5890b 2018-10-18 stsp break;
5322 c1669e2e 2019-01-09 stsp case 'R':
5323 c1669e2e 2019-01-09 stsp recurse = 1;
5324 c1669e2e 2019-01-09 stsp break;
5325 5de5890b 2018-10-18 stsp default:
5326 2deda0b9 2019-03-07 stsp usage_tree();
5327 5de5890b 2018-10-18 stsp /* NOTREACHED */
5328 5de5890b 2018-10-18 stsp }
5329 5de5890b 2018-10-18 stsp }
5330 5de5890b 2018-10-18 stsp
5331 5de5890b 2018-10-18 stsp argc -= optind;
5332 5de5890b 2018-10-18 stsp argv += optind;
5333 5de5890b 2018-10-18 stsp
5334 5de5890b 2018-10-18 stsp if (argc == 1)
5335 5de5890b 2018-10-18 stsp path = argv[0];
5336 5de5890b 2018-10-18 stsp else if (argc > 1)
5337 5de5890b 2018-10-18 stsp usage_tree();
5338 5de5890b 2018-10-18 stsp else
5339 7a2c19d6 2019-02-05 stsp path = NULL;
5340 7a2c19d6 2019-02-05 stsp
5341 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
5342 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
5343 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5344 9bf7a39b 2019-02-05 stsp goto done;
5345 9bf7a39b 2019-02-05 stsp }
5346 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
5347 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5348 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5349 7a2c19d6 2019-02-05 stsp goto done;
5350 7a2c19d6 2019-02-05 stsp else
5351 7a2c19d6 2019-02-05 stsp error = NULL;
5352 7a2c19d6 2019-02-05 stsp if (worktree) {
5353 7a2c19d6 2019-02-05 stsp repo_path =
5354 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5355 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
5356 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5357 7a2c19d6 2019-02-05 stsp if (error)
5358 7a2c19d6 2019-02-05 stsp goto done;
5359 7a2c19d6 2019-02-05 stsp } else {
5360 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
5361 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
5362 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5363 7a2c19d6 2019-02-05 stsp goto done;
5364 7a2c19d6 2019-02-05 stsp }
5365 5de5890b 2018-10-18 stsp }
5366 5de5890b 2018-10-18 stsp }
5367 5de5890b 2018-10-18 stsp
5368 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5369 5de5890b 2018-10-18 stsp if (error != NULL)
5370 c02c541e 2019-03-29 stsp goto done;
5371 c02c541e 2019-03-29 stsp
5372 4fedbf4c 2020-11-07 stsp if (worktree) {
5373 4fedbf4c 2020-11-07 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
5374 4fedbf4c 2020-11-07 stsp char *p;
5375 5de5890b 2018-10-18 stsp
5376 4fedbf4c 2020-11-07 stsp if (path == NULL)
5377 4fedbf4c 2020-11-07 stsp path = "";
5378 4fedbf4c 2020-11-07 stsp error = got_worktree_resolve_path(&p, worktree, path);
5379 4fedbf4c 2020-11-07 stsp if (error)
5380 4fedbf4c 2020-11-07 stsp goto done;
5381 4fedbf4c 2020-11-07 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
5382 4fedbf4c 2020-11-07 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5383 4fedbf4c 2020-11-07 stsp p) == -1) {
5384 4fedbf4c 2020-11-07 stsp error = got_error_from_errno("asprintf");
5385 9bf7a39b 2019-02-05 stsp free(p);
5386 4fedbf4c 2020-11-07 stsp goto done;
5387 4fedbf4c 2020-11-07 stsp }
5388 4fedbf4c 2020-11-07 stsp free(p);
5389 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5390 4fedbf4c 2020-11-07 stsp if (error)
5391 4fedbf4c 2020-11-07 stsp goto done;
5392 4fedbf4c 2020-11-07 stsp } else {
5393 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5394 4fedbf4c 2020-11-07 stsp if (error)
5395 4fedbf4c 2020-11-07 stsp goto done;
5396 4fedbf4c 2020-11-07 stsp if (path == NULL)
5397 9bf7a39b 2019-02-05 stsp path = "/";
5398 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
5399 9bf7a39b 2019-02-05 stsp if (error != NULL)
5400 9bf7a39b 2019-02-05 stsp goto done;
5401 9bf7a39b 2019-02-05 stsp }
5402 5de5890b 2018-10-18 stsp
5403 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
5404 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
5405 4e0a20a4 2020-03-23 tracey if (worktree)
5406 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
5407 4e0a20a4 2020-03-23 tracey else
5408 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
5409 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
5410 5de5890b 2018-10-18 stsp if (error != NULL)
5411 5de5890b 2018-10-18 stsp goto done;
5412 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5413 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
5414 5de5890b 2018-10-18 stsp if (error != NULL)
5415 5de5890b 2018-10-18 stsp goto done;
5416 5de5890b 2018-10-18 stsp } else {
5417 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5418 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5419 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5420 84de9106 2020-12-26 stsp NULL);
5421 84de9106 2020-12-26 stsp if (error)
5422 84de9106 2020-12-26 stsp goto done;
5423 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5424 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5425 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5426 30837e32 2019-07-25 stsp if (error)
5427 5de5890b 2018-10-18 stsp goto done;
5428 5de5890b 2018-10-18 stsp }
5429 5de5890b 2018-10-18 stsp
5430 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5431 c1669e2e 2019-01-09 stsp in_repo_path, repo);
5432 5de5890b 2018-10-18 stsp done:
5433 5de5890b 2018-10-18 stsp free(in_repo_path);
5434 5de5890b 2018-10-18 stsp free(repo_path);
5435 5de5890b 2018-10-18 stsp free(cwd);
5436 5de5890b 2018-10-18 stsp free(commit_id);
5437 7a2c19d6 2019-02-05 stsp if (worktree)
5438 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
5439 5de5890b 2018-10-18 stsp if (repo) {
5440 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5441 5de5890b 2018-10-18 stsp if (error == NULL)
5442 1d0f4054 2021-06-17 stsp error = close_err;
5443 5de5890b 2018-10-18 stsp }
5444 5de5890b 2018-10-18 stsp return error;
5445 5de5890b 2018-10-18 stsp }
5446 5de5890b 2018-10-18 stsp
5447 6bad629b 2019-02-04 stsp __dead static void
5448 6bad629b 2019-02-04 stsp usage_status(void)
5449 6bad629b 2019-02-04 stsp {
5450 00357e4d 2021-09-14 tracey fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5451 00357e4d 2021-09-14 tracey "[-S status-codes] [path ...]\n", getprogname());
5452 6bad629b 2019-02-04 stsp exit(1);
5453 6bad629b 2019-02-04 stsp }
5454 00357e4d 2021-09-14 tracey
5455 00357e4d 2021-09-14 tracey struct got_status_arg {
5456 00357e4d 2021-09-14 tracey char *status_codes;
5457 00357e4d 2021-09-14 tracey int suppress;
5458 00357e4d 2021-09-14 tracey };
5459 5c860e29 2018-03-12 stsp
5460 b72f483a 2019-02-05 stsp static const struct got_error *
5461 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
5462 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5463 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5464 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5465 6bad629b 2019-02-04 stsp {
5466 00357e4d 2021-09-14 tracey struct got_status_arg *st = arg;
5467 00357e4d 2021-09-14 tracey
5468 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5469 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
5470 00357e4d 2021-09-14 tracey if (st != NULL && st->status_codes) {
5471 00357e4d 2021-09-14 tracey size_t ncodes = strlen(st->status_codes);
5472 00357e4d 2021-09-14 tracey int i, j = 0;
5473 00357e4d 2021-09-14 tracey
5474 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
5475 00357e4d 2021-09-14 tracey if (st->suppress) {
5476 00357e4d 2021-09-14 tracey if (status == st->status_codes[i] ||
5477 00357e4d 2021-09-14 tracey staged_status == st->status_codes[i]) {
5478 00357e4d 2021-09-14 tracey j++;
5479 00357e4d 2021-09-14 tracey continue;
5480 00357e4d 2021-09-14 tracey }
5481 00357e4d 2021-09-14 tracey } else {
5482 00357e4d 2021-09-14 tracey if (status == st->status_codes[i] ||
5483 00357e4d 2021-09-14 tracey staged_status == st->status_codes[i])
5484 00357e4d 2021-09-14 tracey break;
5485 00357e4d 2021-09-14 tracey }
5486 081470ac 2020-08-13 stsp }
5487 00357e4d 2021-09-14 tracey
5488 00357e4d 2021-09-14 tracey if (st->suppress && j == 0)
5489 00357e4d 2021-09-14 tracey goto print;
5490 00357e4d 2021-09-14 tracey
5491 081470ac 2020-08-13 stsp if (i == ncodes)
5492 081470ac 2020-08-13 stsp return NULL;
5493 081470ac 2020-08-13 stsp }
5494 00357e4d 2021-09-14 tracey print:
5495 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
5496 b72f483a 2019-02-05 stsp return NULL;
5497 6bad629b 2019-02-04 stsp }
5498 5c860e29 2018-03-12 stsp
5499 6bad629b 2019-02-04 stsp static const struct got_error *
5500 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
5501 6bad629b 2019-02-04 stsp {
5502 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
5503 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
5504 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
5505 00357e4d 2021-09-14 tracey struct got_status_arg st;
5506 00357e4d 2021-09-14 tracey char *cwd = NULL;
5507 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
5508 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
5509 f6343036 2021-06-22 stsp int ch, i, no_ignores = 0;
5510 5c860e29 2018-03-12 stsp
5511 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
5512 72ea6654 2019-07-27 stsp
5513 00357e4d 2021-09-14 tracey memset(&st, 0, sizeof(st));
5514 00357e4d 2021-09-14 tracey st.status_codes = NULL;
5515 00357e4d 2021-09-14 tracey st.suppress = 0;
5516 00357e4d 2021-09-14 tracey
5517 00357e4d 2021-09-14 tracey while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
5518 6bad629b 2019-02-04 stsp switch (ch) {
5519 f6343036 2021-06-22 stsp case 'I':
5520 f6343036 2021-06-22 stsp no_ignores = 1;
5521 f6343036 2021-06-22 stsp break;
5522 788d4a19 2021-09-14 stsp case 'S':
5523 788d4a19 2021-09-14 stsp if (st.status_codes != NULL && st.suppress == 0)
5524 788d4a19 2021-09-14 stsp option_conflict('S', 's');
5525 788d4a19 2021-09-14 stsp st.suppress = 1;
5526 788d4a19 2021-09-14 stsp /* fallthrough */
5527 081470ac 2020-08-13 stsp case 's':
5528 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
5529 081470ac 2020-08-13 stsp switch (optarg[i]) {
5530 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
5531 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
5532 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
5533 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
5534 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
5535 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
5536 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
5537 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
5538 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
5539 081470ac 2020-08-13 stsp break;
5540 081470ac 2020-08-13 stsp default:
5541 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
5542 081470ac 2020-08-13 stsp optarg[i]);
5543 081470ac 2020-08-13 stsp }
5544 081470ac 2020-08-13 stsp }
5545 788d4a19 2021-09-14 stsp if (ch == 's' && st.suppress)
5546 b043307b 2021-09-14 stsp option_conflict('s', 'S');
5547 00357e4d 2021-09-14 tracey st.status_codes = optarg;
5548 081470ac 2020-08-13 stsp break;
5549 5c860e29 2018-03-12 stsp default:
5550 2deda0b9 2019-03-07 stsp usage_status();
5551 6bad629b 2019-02-04 stsp /* NOTREACHED */
5552 5c860e29 2018-03-12 stsp }
5553 5c860e29 2018-03-12 stsp }
5554 5c860e29 2018-03-12 stsp
5555 6bad629b 2019-02-04 stsp argc -= optind;
5556 6bad629b 2019-02-04 stsp argv += optind;
5557 5c860e29 2018-03-12 stsp
5558 6bad629b 2019-02-04 stsp #ifndef PROFILE
5559 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5560 6bad629b 2019-02-04 stsp NULL) == -1)
5561 6bad629b 2019-02-04 stsp err(1, "pledge");
5562 f42b1b34 2018-03-12 stsp #endif
5563 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
5564 927df6b7 2019-02-10 stsp if (cwd == NULL) {
5565 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5566 927df6b7 2019-02-10 stsp goto done;
5567 927df6b7 2019-02-10 stsp }
5568 927df6b7 2019-02-10 stsp
5569 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
5570 fa51e947 2020-03-27 stsp if (error) {
5571 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5572 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
5573 a5edda0a 2019-07-27 stsp goto done;
5574 fa51e947 2020-03-27 stsp }
5575 6bad629b 2019-02-04 stsp
5576 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5577 c9956ddf 2019-09-08 stsp NULL);
5578 6bad629b 2019-02-04 stsp if (error != NULL)
5579 6bad629b 2019-02-04 stsp goto done;
5580 6bad629b 2019-02-04 stsp
5581 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5582 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5583 087fb88c 2019-08-04 stsp if (error)
5584 087fb88c 2019-08-04 stsp goto done;
5585 087fb88c 2019-08-04 stsp
5586 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5587 6bad629b 2019-02-04 stsp if (error)
5588 6bad629b 2019-02-04 stsp goto done;
5589 6bad629b 2019-02-04 stsp
5590 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, no_ignores,
5591 00357e4d 2021-09-14 tracey print_status, &st, check_cancelled, NULL);
5592 6bad629b 2019-02-04 stsp done:
5593 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
5594 a5edda0a 2019-07-27 stsp free((char *)pe->path);
5595 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
5596 927df6b7 2019-02-10 stsp free(cwd);
5597 d0eebce4 2019-03-11 stsp return error;
5598 d0eebce4 2019-03-11 stsp }
5599 d0eebce4 2019-03-11 stsp
5600 d0eebce4 2019-03-11 stsp __dead static void
5601 d0eebce4 2019-03-11 stsp usage_ref(void)
5602 d0eebce4 2019-03-11 stsp {
5603 d0eebce4 2019-03-11 stsp fprintf(stderr,
5604 2bf0fa54 2021-11-20 thomas "usage: %s ref [-r repository] [-l] [-t] [-c object] "
5605 2bf0fa54 2021-11-20 thomas "[-s reference] [-d] [name]\n",
5606 d0eebce4 2019-03-11 stsp getprogname());
5607 d0eebce4 2019-03-11 stsp exit(1);
5608 d0eebce4 2019-03-11 stsp }
5609 d0eebce4 2019-03-11 stsp
5610 d0eebce4 2019-03-11 stsp static const struct got_error *
5611 2bf0fa54 2021-11-20 thomas list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
5612 d0eebce4 2019-03-11 stsp {
5613 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
5614 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
5615 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
5616 d0eebce4 2019-03-11 stsp
5617 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5618 2bf0fa54 2021-11-20 thomas err = got_ref_list(&refs, repo, refname, sort_by_time ?
5619 2bf0fa54 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
5620 2bf0fa54 2021-11-20 thomas repo);
5621 d0eebce4 2019-03-11 stsp if (err)
5622 d0eebce4 2019-03-11 stsp return err;
5623 d0eebce4 2019-03-11 stsp
5624 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
5625 d0eebce4 2019-03-11 stsp char *refstr;
5626 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
5627 d0eebce4 2019-03-11 stsp if (refstr == NULL)
5628 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
5629 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5630 d0eebce4 2019-03-11 stsp free(refstr);
5631 d0eebce4 2019-03-11 stsp }
5632 d0eebce4 2019-03-11 stsp
5633 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5634 d0eebce4 2019-03-11 stsp return NULL;
5635 d0eebce4 2019-03-11 stsp }
5636 d0eebce4 2019-03-11 stsp
5637 d0eebce4 2019-03-11 stsp static const struct got_error *
5638 161728eb 2021-07-24 stsp delete_ref_by_name(struct got_repository *repo, const char *refname)
5639 d0eebce4 2019-03-11 stsp {
5640 161728eb 2021-07-24 stsp const struct got_error *err;
5641 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5642 d0eebce4 2019-03-11 stsp
5643 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5644 d0eebce4 2019-03-11 stsp if (err)
5645 d0eebce4 2019-03-11 stsp return err;
5646 993f033b 2021-07-16 stsp
5647 161728eb 2021-07-24 stsp err = delete_ref(repo, ref);
5648 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5649 d0eebce4 2019-03-11 stsp return err;
5650 d0eebce4 2019-03-11 stsp }
5651 d0eebce4 2019-03-11 stsp
5652 d0eebce4 2019-03-11 stsp static const struct got_error *
5653 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
5654 d0eebce4 2019-03-11 stsp {
5655 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5656 d0eebce4 2019-03-11 stsp struct got_object_id *id;
5657 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
5658 d1644381 2019-07-14 stsp
5659 d1644381 2019-07-14 stsp /*
5660 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5661 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
5662 d1644381 2019-07-14 stsp * an unintended typo.
5663 d1644381 2019-07-14 stsp */
5664 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5665 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5666 d0eebce4 2019-03-11 stsp
5667 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5668 dd88155e 2019-06-29 stsp repo);
5669 d83d9d5c 2019-05-13 stsp if (err) {
5670 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
5671 d0eebce4 2019-03-11 stsp
5672 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5673 d83d9d5c 2019-05-13 stsp return err;
5674 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
5675 d83d9d5c 2019-05-13 stsp if (err)
5676 d83d9d5c 2019-05-13 stsp return err;
5677 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
5678 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
5679 d83d9d5c 2019-05-13 stsp if (err)
5680 d83d9d5c 2019-05-13 stsp return err;
5681 d83d9d5c 2019-05-13 stsp }
5682 d83d9d5c 2019-05-13 stsp
5683 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5684 d0eebce4 2019-03-11 stsp if (err)
5685 d0eebce4 2019-03-11 stsp goto done;
5686 d0eebce4 2019-03-11 stsp
5687 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5688 d0eebce4 2019-03-11 stsp done:
5689 d0eebce4 2019-03-11 stsp if (ref)
5690 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5691 d0eebce4 2019-03-11 stsp free(id);
5692 d1c1ae5f 2019-08-12 stsp return err;
5693 d1c1ae5f 2019-08-12 stsp }
5694 d1c1ae5f 2019-08-12 stsp
5695 d1c1ae5f 2019-08-12 stsp static const struct got_error *
5696 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
5697 d1c1ae5f 2019-08-12 stsp {
5698 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
5699 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
5700 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
5701 d1c1ae5f 2019-08-12 stsp
5702 d1c1ae5f 2019-08-12 stsp /*
5703 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5704 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
5705 d1c1ae5f 2019-08-12 stsp * an unintended typo.
5706 d1c1ae5f 2019-08-12 stsp */
5707 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5708 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5709 d1c1ae5f 2019-08-12 stsp
5710 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5711 d1c1ae5f 2019-08-12 stsp if (err)
5712 d1c1ae5f 2019-08-12 stsp return err;
5713 d1c1ae5f 2019-08-12 stsp
5714 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5715 d1c1ae5f 2019-08-12 stsp if (err)
5716 d1c1ae5f 2019-08-12 stsp goto done;
5717 d1c1ae5f 2019-08-12 stsp
5718 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5719 d1c1ae5f 2019-08-12 stsp done:
5720 d1c1ae5f 2019-08-12 stsp if (target_ref)
5721 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5722 d1c1ae5f 2019-08-12 stsp if (ref)
5723 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5724 d0eebce4 2019-03-11 stsp return err;
5725 d0eebce4 2019-03-11 stsp }
5726 d0eebce4 2019-03-11 stsp
5727 d0eebce4 2019-03-11 stsp static const struct got_error *
5728 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5729 d0eebce4 2019-03-11 stsp {
5730 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
5731 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
5732 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
5733 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
5734 2bf0fa54 2021-11-20 thomas int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
5735 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
5736 b2070a3f 2020-03-22 stsp char *refname = NULL;
5737 d0eebce4 2019-03-11 stsp
5738 2bf0fa54 2021-11-20 thomas while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
5739 d0eebce4 2019-03-11 stsp switch (ch) {
5740 e31abbf2 2020-03-22 stsp case 'c':
5741 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5742 e31abbf2 2020-03-22 stsp break;
5743 d0eebce4 2019-03-11 stsp case 'd':
5744 e31abbf2 2020-03-22 stsp do_delete = 1;
5745 d0eebce4 2019-03-11 stsp break;
5746 d0eebce4 2019-03-11 stsp case 'r':
5747 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5748 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5749 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5750 9ba1d308 2019-10-21 stsp optarg);
5751 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5752 d0eebce4 2019-03-11 stsp break;
5753 d0eebce4 2019-03-11 stsp case 'l':
5754 d0eebce4 2019-03-11 stsp do_list = 1;
5755 d1c1ae5f 2019-08-12 stsp break;
5756 d1c1ae5f 2019-08-12 stsp case 's':
5757 e31abbf2 2020-03-22 stsp symref_target = optarg;
5758 2bf0fa54 2021-11-20 thomas break;
5759 2bf0fa54 2021-11-20 thomas case 't':
5760 2bf0fa54 2021-11-20 thomas sort_by_time = 1;
5761 d0eebce4 2019-03-11 stsp break;
5762 d0eebce4 2019-03-11 stsp default:
5763 d0eebce4 2019-03-11 stsp usage_ref();
5764 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5765 d0eebce4 2019-03-11 stsp }
5766 d0eebce4 2019-03-11 stsp }
5767 d0eebce4 2019-03-11 stsp
5768 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5769 ff69268e 2020-12-13 stsp option_conflict('c', 'l');
5770 ff69268e 2020-12-13 stsp if (obj_arg && do_delete)
5771 ff69268e 2020-12-13 stsp option_conflict('c', 'd');
5772 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5773 ff69268e 2020-12-13 stsp option_conflict('c', 's');
5774 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5775 ff69268e 2020-12-13 stsp option_conflict('s', 'd');
5776 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5777 ff69268e 2020-12-13 stsp option_conflict('s', 'l');
5778 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5779 ff69268e 2020-12-13 stsp option_conflict('d', 'l');
5780 2bf0fa54 2021-11-20 thomas if (sort_by_time && !do_list)
5781 2bf0fa54 2021-11-20 thomas errx(1, "-t option requires -l option");
5782 d0eebce4 2019-03-11 stsp
5783 d0eebce4 2019-03-11 stsp argc -= optind;
5784 d0eebce4 2019-03-11 stsp argv += optind;
5785 d0eebce4 2019-03-11 stsp
5786 e31abbf2 2020-03-22 stsp if (do_list) {
5787 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5788 d0eebce4 2019-03-11 stsp usage_ref();
5789 b2070a3f 2020-03-22 stsp if (argc == 1) {
5790 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5791 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5792 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5793 b2070a3f 2020-03-22 stsp goto done;
5794 b2070a3f 2020-03-22 stsp }
5795 b2070a3f 2020-03-22 stsp }
5796 e31abbf2 2020-03-22 stsp } else {
5797 e31abbf2 2020-03-22 stsp if (argc != 1)
5798 e31abbf2 2020-03-22 stsp usage_ref();
5799 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5800 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5801 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5802 b2070a3f 2020-03-22 stsp goto done;
5803 b2070a3f 2020-03-22 stsp }
5804 e31abbf2 2020-03-22 stsp }
5805 b2070a3f 2020-03-22 stsp
5806 b2070a3f 2020-03-22 stsp if (refname)
5807 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5808 d0eebce4 2019-03-11 stsp
5809 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5810 e0b57350 2019-03-12 stsp if (do_list) {
5811 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5812 e0b57350 2019-03-12 stsp NULL) == -1)
5813 e0b57350 2019-03-12 stsp err(1, "pledge");
5814 e0b57350 2019-03-12 stsp } else {
5815 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5816 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5817 e0b57350 2019-03-12 stsp err(1, "pledge");
5818 e0b57350 2019-03-12 stsp }
5819 d0eebce4 2019-03-11 stsp #endif
5820 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5821 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5822 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5823 d0eebce4 2019-03-11 stsp goto done;
5824 d0eebce4 2019-03-11 stsp }
5825 d0eebce4 2019-03-11 stsp
5826 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5827 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5828 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5829 d0eebce4 2019-03-11 stsp goto done;
5830 d0eebce4 2019-03-11 stsp else
5831 d0eebce4 2019-03-11 stsp error = NULL;
5832 d0eebce4 2019-03-11 stsp if (worktree) {
5833 d0eebce4 2019-03-11 stsp repo_path =
5834 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5835 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5836 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5837 d0eebce4 2019-03-11 stsp if (error)
5838 d0eebce4 2019-03-11 stsp goto done;
5839 d0eebce4 2019-03-11 stsp } else {
5840 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5841 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5842 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5843 d0eebce4 2019-03-11 stsp goto done;
5844 d0eebce4 2019-03-11 stsp }
5845 d0eebce4 2019-03-11 stsp }
5846 d0eebce4 2019-03-11 stsp }
5847 d0eebce4 2019-03-11 stsp
5848 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5849 d0eebce4 2019-03-11 stsp if (error != NULL)
5850 d0eebce4 2019-03-11 stsp goto done;
5851 d0eebce4 2019-03-11 stsp
5852 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5853 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5854 c02c541e 2019-03-29 stsp if (error)
5855 c02c541e 2019-03-29 stsp goto done;
5856 c02c541e 2019-03-29 stsp
5857 d0eebce4 2019-03-11 stsp if (do_list)
5858 2bf0fa54 2021-11-20 thomas error = list_refs(repo, refname, sort_by_time);
5859 e31abbf2 2020-03-22 stsp else if (do_delete)
5860 161728eb 2021-07-24 stsp error = delete_ref_by_name(repo, refname);
5861 e31abbf2 2020-03-22 stsp else if (symref_target)
5862 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5863 e31abbf2 2020-03-22 stsp else {
5864 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5865 e31abbf2 2020-03-22 stsp usage_ref();
5866 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5867 e31abbf2 2020-03-22 stsp }
5868 4e759de4 2019-06-26 stsp done:
5869 b2070a3f 2020-03-22 stsp free(refname);
5870 1d0f4054 2021-06-17 stsp if (repo) {
5871 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5872 1d0f4054 2021-06-17 stsp if (error == NULL)
5873 1d0f4054 2021-06-17 stsp error = close_err;
5874 1d0f4054 2021-06-17 stsp }
5875 4e759de4 2019-06-26 stsp if (worktree)
5876 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5877 4e759de4 2019-06-26 stsp free(cwd);
5878 4e759de4 2019-06-26 stsp free(repo_path);
5879 4e759de4 2019-06-26 stsp return error;
5880 4e759de4 2019-06-26 stsp }
5881 4e759de4 2019-06-26 stsp
5882 4e759de4 2019-06-26 stsp __dead static void
5883 4e759de4 2019-06-26 stsp usage_branch(void)
5884 4e759de4 2019-06-26 stsp {
5885 4e759de4 2019-06-26 stsp fprintf(stderr,
5886 a0b48eaf 2021-11-20 thomas "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-t] "
5887 a0b48eaf 2021-11-20 thomas "[-n] [name]\n", getprogname());
5888 4e759de4 2019-06-26 stsp exit(1);
5889 4e759de4 2019-06-26 stsp }
5890 4e759de4 2019-06-26 stsp
5891 4e759de4 2019-06-26 stsp static const struct got_error *
5892 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5893 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5894 4e99b47e 2019-10-04 stsp {
5895 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5896 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5897 4e99b47e 2019-10-04 stsp char *refstr;
5898 4e99b47e 2019-10-04 stsp
5899 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5900 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5901 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5902 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5903 4e99b47e 2019-10-04 stsp
5904 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5905 4e99b47e 2019-10-04 stsp if (err)
5906 4e99b47e 2019-10-04 stsp return err;
5907 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5908 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5909 4e99b47e 2019-10-04 stsp marker = "* ";
5910 4e99b47e 2019-10-04 stsp else
5911 4e99b47e 2019-10-04 stsp marker = "~ ";
5912 4e99b47e 2019-10-04 stsp free(id);
5913 4e99b47e 2019-10-04 stsp }
5914 4e99b47e 2019-10-04 stsp
5915 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5916 4e99b47e 2019-10-04 stsp refname += 11;
5917 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5918 4e99b47e 2019-10-04 stsp refname += 18;
5919 34d4e04c 2021-02-08 stsp if (strncmp(refname, "refs/remotes/", 13) == 0)
5920 34d4e04c 2021-02-08 stsp refname += 13;
5921 4e99b47e 2019-10-04 stsp
5922 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5923 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5924 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5925 4e99b47e 2019-10-04 stsp
5926 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5927 4e99b47e 2019-10-04 stsp free(refstr);
5928 4e99b47e 2019-10-04 stsp return NULL;
5929 4e99b47e 2019-10-04 stsp }
5930 4e99b47e 2019-10-04 stsp
5931 4e99b47e 2019-10-04 stsp static const struct got_error *
5932 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5933 ad89fa31 2019-10-04 stsp {
5934 ad89fa31 2019-10-04 stsp const char *refname;
5935 ad89fa31 2019-10-04 stsp
5936 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5937 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5938 ad89fa31 2019-10-04 stsp
5939 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5940 ad89fa31 2019-10-04 stsp
5941 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5942 ad89fa31 2019-10-04 stsp refname += 11;
5943 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5944 ad89fa31 2019-10-04 stsp refname += 18;
5945 ad89fa31 2019-10-04 stsp
5946 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5947 ad89fa31 2019-10-04 stsp
5948 ad89fa31 2019-10-04 stsp return NULL;
5949 ad89fa31 2019-10-04 stsp }
5950 ad89fa31 2019-10-04 stsp
5951 ad89fa31 2019-10-04 stsp static const struct got_error *
5952 a0b48eaf 2021-11-20 thomas list_branches(struct got_repository *repo, struct got_worktree *worktree,
5953 a0b48eaf 2021-11-20 thomas int sort_by_time)
5954 4e759de4 2019-06-26 stsp {
5955 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5956 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5957 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5958 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5959 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5960 4e759de4 2019-06-26 stsp
5961 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5962 ba882ee3 2019-07-11 stsp
5963 4e99b47e 2019-10-04 stsp if (worktree) {
5964 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5965 4e99b47e 2019-10-04 stsp worktree);
5966 4e99b47e 2019-10-04 stsp if (err)
5967 4e99b47e 2019-10-04 stsp return err;
5968 4e99b47e 2019-10-04 stsp
5969 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5970 4e99b47e 2019-10-04 stsp worktree);
5971 4e99b47e 2019-10-04 stsp if (err)
5972 4e99b47e 2019-10-04 stsp return err;
5973 4e99b47e 2019-10-04 stsp
5974 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5975 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5976 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5977 4e99b47e 2019-10-04 stsp if (err)
5978 4e99b47e 2019-10-04 stsp return err;
5979 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5980 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5981 4e99b47e 2019-10-04 stsp }
5982 4e99b47e 2019-10-04 stsp }
5983 4e99b47e 2019-10-04 stsp
5984 a0b48eaf 2021-11-20 thomas err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
5985 a0b48eaf 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
5986 a0b48eaf 2021-11-20 thomas repo);
5987 4e759de4 2019-06-26 stsp if (err)
5988 4e759de4 2019-06-26 stsp return err;
5989 4e759de4 2019-06-26 stsp
5990 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry)
5991 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5992 4e759de4 2019-06-26 stsp
5993 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5994 34d4e04c 2021-02-08 stsp
5995 a0b48eaf 2021-11-20 thomas err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
5996 a0b48eaf 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
5997 a0b48eaf 2021-11-20 thomas repo);
5998 34d4e04c 2021-02-08 stsp if (err)
5999 34d4e04c 2021-02-08 stsp return err;
6000 34d4e04c 2021-02-08 stsp
6001 34d4e04c 2021-02-08 stsp TAILQ_FOREACH(re, &refs, entry)
6002 34d4e04c 2021-02-08 stsp list_branch(repo, worktree, re->ref);
6003 34d4e04c 2021-02-08 stsp
6004 34d4e04c 2021-02-08 stsp got_ref_list_free(&refs);
6005 34d4e04c 2021-02-08 stsp
6006 4e759de4 2019-06-26 stsp return NULL;
6007 4e759de4 2019-06-26 stsp }
6008 4e759de4 2019-06-26 stsp
6009 4e759de4 2019-06-26 stsp static const struct got_error *
6010 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6011 45cd4e47 2019-08-25 stsp const char *branch_name)
6012 4e759de4 2019-06-26 stsp {
6013 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
6014 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
6015 2f1457c6 2021-08-27 stsp char *refname, *remote_refname = NULL;
6016 4e759de4 2019-06-26 stsp
6017 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "refs/", 5) == 0)
6018 2f1457c6 2021-08-27 stsp branch_name += 5;
6019 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "heads/", 6) == 0)
6020 2f1457c6 2021-08-27 stsp branch_name += 6;
6021 2f1457c6 2021-08-27 stsp else if (strncmp(branch_name, "remotes/", 8) == 0)
6022 2f1457c6 2021-08-27 stsp branch_name += 8;
6023 2f1457c6 2021-08-27 stsp
6024 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6025 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
6026 4e759de4 2019-06-26 stsp
6027 2f1457c6 2021-08-27 stsp if (asprintf(&remote_refname, "refs/remotes/%s",
6028 2f1457c6 2021-08-27 stsp branch_name) == -1) {
6029 2f1457c6 2021-08-27 stsp err = got_error_from_errno("asprintf");
6030 4e759de4 2019-06-26 stsp goto done;
6031 2f1457c6 2021-08-27 stsp }
6032 4e759de4 2019-06-26 stsp
6033 2f1457c6 2021-08-27 stsp err = got_ref_open(&ref, repo, refname, 0);
6034 2f1457c6 2021-08-27 stsp if (err) {
6035 2f1457c6 2021-08-27 stsp const struct got_error *err2;
6036 2f1457c6 2021-08-27 stsp if (err->code != GOT_ERR_NOT_REF)
6037 2f1457c6 2021-08-27 stsp goto done;
6038 2f1457c6 2021-08-27 stsp /*
6039 2f1457c6 2021-08-27 stsp * Keep 'err' intact such that if neither branch exists
6040 2f1457c6 2021-08-27 stsp * we report "refs/heads" rather than "refs/remotes" in
6041 2f1457c6 2021-08-27 stsp * our error message.
6042 2f1457c6 2021-08-27 stsp */
6043 2f1457c6 2021-08-27 stsp err2 = got_ref_open(&ref, repo, remote_refname, 0);
6044 2f1457c6 2021-08-27 stsp if (err2)
6045 2f1457c6 2021-08-27 stsp goto done;
6046 2f1457c6 2021-08-27 stsp err = NULL;
6047 2f1457c6 2021-08-27 stsp }
6048 2f1457c6 2021-08-27 stsp
6049 45cd4e47 2019-08-25 stsp if (worktree &&
6050 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
6051 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
6052 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6053 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
6054 45cd4e47 2019-08-25 stsp goto done;
6055 45cd4e47 2019-08-25 stsp }
6056 45cd4e47 2019-08-25 stsp
6057 978a28a1 2021-09-04 naddy err = delete_ref(repo, ref);
6058 4e759de4 2019-06-26 stsp done:
6059 45cd4e47 2019-08-25 stsp if (ref)
6060 45cd4e47 2019-08-25 stsp got_ref_close(ref);
6061 4e759de4 2019-06-26 stsp free(refname);
6062 2f1457c6 2021-08-27 stsp free(remote_refname);
6063 4e759de4 2019-06-26 stsp return err;
6064 4e759de4 2019-06-26 stsp }
6065 4e759de4 2019-06-26 stsp
6066 4e759de4 2019-06-26 stsp static const struct got_error *
6067 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
6068 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
6069 4e759de4 2019-06-26 stsp {
6070 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
6071 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
6072 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
6073 d3f84d51 2019-07-11 stsp
6074 d3f84d51 2019-07-11 stsp /*
6075 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
6076 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
6077 d3f84d51 2019-07-11 stsp * an unintended typo.
6078 d3f84d51 2019-07-11 stsp */
6079 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
6080 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6081 2f1457c6 2021-08-27 stsp
6082 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
6083 2f1457c6 2021-08-27 stsp branch_name += 11;
6084 4e759de4 2019-06-26 stsp
6085 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6086 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
6087 62d463ca 2020-10-20 naddy goto done;
6088 4e759de4 2019-06-26 stsp }
6089 4e759de4 2019-06-26 stsp
6090 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
6091 4e759de4 2019-06-26 stsp if (err == NULL) {
6092 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
6093 4e759de4 2019-06-26 stsp goto done;
6094 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
6095 4e759de4 2019-06-26 stsp goto done;
6096 4e759de4 2019-06-26 stsp
6097 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
6098 4e759de4 2019-06-26 stsp if (err)
6099 4e759de4 2019-06-26 stsp goto done;
6100 4e759de4 2019-06-26 stsp
6101 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
6102 d0eebce4 2019-03-11 stsp done:
6103 4e759de4 2019-06-26 stsp if (ref)
6104 4e759de4 2019-06-26 stsp got_ref_close(ref);
6105 4e759de4 2019-06-26 stsp free(base_refname);
6106 4e759de4 2019-06-26 stsp free(refname);
6107 4e759de4 2019-06-26 stsp return err;
6108 4e759de4 2019-06-26 stsp }
6109 4e759de4 2019-06-26 stsp
6110 4e759de4 2019-06-26 stsp static const struct got_error *
6111 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
6112 4e759de4 2019-06-26 stsp {
6113 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
6114 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
6115 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
6116 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
6117 a0b48eaf 2021-11-20 thomas int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6118 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
6119 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
6120 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
6121 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
6122 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
6123 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
6124 4e759de4 2019-06-26 stsp
6125 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
6126 da76fce2 2020-02-24 stsp
6127 a0b48eaf 2021-11-20 thomas while ((ch = getopt(argc, argv, "c:d:r:lnt")) != -1) {
6128 4e759de4 2019-06-26 stsp switch (ch) {
6129 a74f7e83 2019-11-10 stsp case 'c':
6130 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
6131 a74f7e83 2019-11-10 stsp break;
6132 4e759de4 2019-06-26 stsp case 'd':
6133 4e759de4 2019-06-26 stsp delref = optarg;
6134 4e759de4 2019-06-26 stsp break;
6135 4e759de4 2019-06-26 stsp case 'r':
6136 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
6137 4e759de4 2019-06-26 stsp if (repo_path == NULL)
6138 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6139 9ba1d308 2019-10-21 stsp optarg);
6140 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
6141 4e759de4 2019-06-26 stsp break;
6142 4e759de4 2019-06-26 stsp case 'l':
6143 4e759de4 2019-06-26 stsp do_list = 1;
6144 da76fce2 2020-02-24 stsp break;
6145 da76fce2 2020-02-24 stsp case 'n':
6146 da76fce2 2020-02-24 stsp do_update = 0;
6147 4e759de4 2019-06-26 stsp break;
6148 a0b48eaf 2021-11-20 thomas case 't':
6149 a0b48eaf 2021-11-20 thomas sort_by_time = 1;
6150 a0b48eaf 2021-11-20 thomas break;
6151 4e759de4 2019-06-26 stsp default:
6152 4e759de4 2019-06-26 stsp usage_branch();
6153 4e759de4 2019-06-26 stsp /* NOTREACHED */
6154 4e759de4 2019-06-26 stsp }
6155 4e759de4 2019-06-26 stsp }
6156 4e759de4 2019-06-26 stsp
6157 4e759de4 2019-06-26 stsp if (do_list && delref)
6158 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
6159 a0b48eaf 2021-11-20 thomas if (sort_by_time && !do_list)
6160 a0b48eaf 2021-11-20 thomas errx(1, "-t option requires -l option");
6161 4e759de4 2019-06-26 stsp
6162 4e759de4 2019-06-26 stsp argc -= optind;
6163 4e759de4 2019-06-26 stsp argv += optind;
6164 ad89fa31 2019-10-04 stsp
6165 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
6166 ad89fa31 2019-10-04 stsp do_show = 1;
6167 4e759de4 2019-06-26 stsp
6168 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
6169 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
6170 a74f7e83 2019-11-10 stsp
6171 4e759de4 2019-06-26 stsp if (do_list || delref) {
6172 4e759de4 2019-06-26 stsp if (argc > 0)
6173 4e759de4 2019-06-26 stsp usage_branch();
6174 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
6175 4e759de4 2019-06-26 stsp usage_branch();
6176 4e759de4 2019-06-26 stsp
6177 4e759de4 2019-06-26 stsp #ifndef PROFILE
6178 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
6179 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6180 4e759de4 2019-06-26 stsp NULL) == -1)
6181 4e759de4 2019-06-26 stsp err(1, "pledge");
6182 4e759de4 2019-06-26 stsp } else {
6183 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6184 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
6185 4e759de4 2019-06-26 stsp err(1, "pledge");
6186 4e759de4 2019-06-26 stsp }
6187 4e759de4 2019-06-26 stsp #endif
6188 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
6189 4e759de4 2019-06-26 stsp if (cwd == NULL) {
6190 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
6191 4e759de4 2019-06-26 stsp goto done;
6192 4e759de4 2019-06-26 stsp }
6193 4e759de4 2019-06-26 stsp
6194 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
6195 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
6196 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6197 4e759de4 2019-06-26 stsp goto done;
6198 4e759de4 2019-06-26 stsp else
6199 4e759de4 2019-06-26 stsp error = NULL;
6200 4e759de4 2019-06-26 stsp if (worktree) {
6201 4e759de4 2019-06-26 stsp repo_path =
6202 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
6203 4e759de4 2019-06-26 stsp if (repo_path == NULL)
6204 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
6205 4e759de4 2019-06-26 stsp if (error)
6206 4e759de4 2019-06-26 stsp goto done;
6207 4e759de4 2019-06-26 stsp } else {
6208 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
6209 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
6210 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
6211 4e759de4 2019-06-26 stsp goto done;
6212 4e759de4 2019-06-26 stsp }
6213 4e759de4 2019-06-26 stsp }
6214 4e759de4 2019-06-26 stsp }
6215 4e759de4 2019-06-26 stsp
6216 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6217 4e759de4 2019-06-26 stsp if (error != NULL)
6218 4e759de4 2019-06-26 stsp goto done;
6219 4e759de4 2019-06-26 stsp
6220 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
6221 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
6222 4e759de4 2019-06-26 stsp if (error)
6223 4e759de4 2019-06-26 stsp goto done;
6224 4e759de4 2019-06-26 stsp
6225 ad89fa31 2019-10-04 stsp if (do_show)
6226 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
6227 ad89fa31 2019-10-04 stsp else if (do_list)
6228 a0b48eaf 2021-11-20 thomas error = list_branches(repo, worktree, sort_by_time);
6229 4e759de4 2019-06-26 stsp else if (delref)
6230 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
6231 4e759de4 2019-06-26 stsp else {
6232 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6233 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6234 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6235 84de9106 2020-12-26 stsp NULL);
6236 84de9106 2020-12-26 stsp if (error)
6237 84de9106 2020-12-26 stsp goto done;
6238 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
6239 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
6240 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
6241 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
6242 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6243 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6244 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
6245 a74f7e83 2019-11-10 stsp if (error)
6246 a74f7e83 2019-11-10 stsp goto done;
6247 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
6248 da76fce2 2020-02-24 stsp if (error)
6249 da76fce2 2020-02-24 stsp goto done;
6250 da76fce2 2020-02-24 stsp if (worktree && do_update) {
6251 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6252 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
6253 da76fce2 2020-02-24 stsp
6254 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
6255 da76fce2 2020-02-24 stsp if (error)
6256 da76fce2 2020-02-24 stsp goto done;
6257 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
6258 da76fce2 2020-02-24 stsp worktree);
6259 da76fce2 2020-02-24 stsp if (error)
6260 da76fce2 2020-02-24 stsp goto done;
6261 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6262 da76fce2 2020-02-24 stsp == -1) {
6263 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
6264 da76fce2 2020-02-24 stsp goto done;
6265 da76fce2 2020-02-24 stsp }
6266 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
6267 da76fce2 2020-02-24 stsp free(branch_refname);
6268 da76fce2 2020-02-24 stsp if (error)
6269 da76fce2 2020-02-24 stsp goto done;
6270 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
6271 da76fce2 2020-02-24 stsp repo);
6272 da76fce2 2020-02-24 stsp if (error)
6273 da76fce2 2020-02-24 stsp goto done;
6274 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
6275 da76fce2 2020-02-24 stsp commit_id);
6276 da76fce2 2020-02-24 stsp if (error)
6277 da76fce2 2020-02-24 stsp goto done;
6278 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6279 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
6280 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
6281 9627c110 2020-04-18 stsp NULL);
6282 da76fce2 2020-02-24 stsp if (error)
6283 da76fce2 2020-02-24 stsp goto done;
6284 4f3c844b 2021-09-14 stsp if (upa.did_something) {
6285 4f3c844b 2021-09-14 stsp printf("Updated to %s: %s\n",
6286 4f3c844b 2021-09-14 stsp got_worktree_get_head_ref_name(worktree),
6287 4f3c844b 2021-09-14 stsp commit_id_str);
6288 4f3c844b 2021-09-14 stsp }
6289 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6290 da76fce2 2020-02-24 stsp }
6291 4e759de4 2019-06-26 stsp }
6292 4e759de4 2019-06-26 stsp done:
6293 da76fce2 2020-02-24 stsp if (ref)
6294 da76fce2 2020-02-24 stsp got_ref_close(ref);
6295 1d0f4054 2021-06-17 stsp if (repo) {
6296 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6297 1d0f4054 2021-06-17 stsp if (error == NULL)
6298 1d0f4054 2021-06-17 stsp error = close_err;
6299 1d0f4054 2021-06-17 stsp }
6300 d0eebce4 2019-03-11 stsp if (worktree)
6301 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
6302 d0eebce4 2019-03-11 stsp free(cwd);
6303 d0eebce4 2019-03-11 stsp free(repo_path);
6304 da76fce2 2020-02-24 stsp free(commit_id);
6305 da76fce2 2020-02-24 stsp free(commit_id_str);
6306 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
6307 da76fce2 2020-02-24 stsp free((char *)pe->path);
6308 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
6309 d00136be 2019-03-26 stsp return error;
6310 d00136be 2019-03-26 stsp }
6311 d00136be 2019-03-26 stsp
6312 8e7bd50a 2019-08-22 stsp
6313 d00136be 2019-03-26 stsp __dead static void
6314 8e7bd50a 2019-08-22 stsp usage_tag(void)
6315 8e7bd50a 2019-08-22 stsp {
6316 8e7bd50a 2019-08-22 stsp fprintf(stderr,
6317 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
6318 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
6319 8e7bd50a 2019-08-22 stsp exit(1);
6320 b8bad2ba 2019-08-23 stsp }
6321 b8bad2ba 2019-08-23 stsp
6322 b8bad2ba 2019-08-23 stsp #if 0
6323 b8bad2ba 2019-08-23 stsp static const struct got_error *
6324 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6325 b8bad2ba 2019-08-23 stsp {
6326 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
6327 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
6328 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
6329 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
6330 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
6331 b8bad2ba 2019-08-23 stsp
6332 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(re, tags, entry) {
6333 dbdddfee 2021-06-23 naddy se = STAILQ_FIRST(sorted);
6334 b8bad2ba 2019-08-23 stsp if (se == NULL) {
6335 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6336 b8bad2ba 2019-08-23 stsp if (err)
6337 b8bad2ba 2019-08-23 stsp return err;
6338 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(sorted, new, entry);
6339 b8bad2ba 2019-08-23 stsp continue;
6340 b8bad2ba 2019-08-23 stsp } else {
6341 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
6342 b8bad2ba 2019-08-23 stsp if (err)
6343 b8bad2ba 2019-08-23 stsp break;
6344 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
6345 b8bad2ba 2019-08-23 stsp free(re_id);
6346 b8bad2ba 2019-08-23 stsp if (err)
6347 b8bad2ba 2019-08-23 stsp break;
6348 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
6349 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
6350 b8bad2ba 2019-08-23 stsp }
6351 b8bad2ba 2019-08-23 stsp
6352 b8bad2ba 2019-08-23 stsp while (se) {
6353 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
6354 b8bad2ba 2019-08-23 stsp if (err)
6355 b8bad2ba 2019-08-23 stsp break;
6356 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
6357 b8bad2ba 2019-08-23 stsp free(se_id);
6358 b8bad2ba 2019-08-23 stsp if (err)
6359 b8bad2ba 2019-08-23 stsp break;
6360 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
6361 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
6362 b8bad2ba 2019-08-23 stsp
6363 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
6364 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6365 b8bad2ba 2019-08-23 stsp if (err)
6366 b8bad2ba 2019-08-23 stsp return err;
6367 dbdddfee 2021-06-23 naddy STAILQ_INSERT_AFTER(sorted, se, new, entry);
6368 b8bad2ba 2019-08-23 stsp break;
6369 b8bad2ba 2019-08-23 stsp }
6370 dbdddfee 2021-06-23 naddy se = STAILQ_NEXT(se, entry);
6371 b8bad2ba 2019-08-23 stsp continue;
6372 b8bad2ba 2019-08-23 stsp }
6373 b8bad2ba 2019-08-23 stsp }
6374 b8bad2ba 2019-08-23 stsp done:
6375 b8bad2ba 2019-08-23 stsp return err;
6376 8e7bd50a 2019-08-22 stsp }
6377 b8bad2ba 2019-08-23 stsp #endif
6378 b8bad2ba 2019-08-23 stsp
6379 b8bad2ba 2019-08-23 stsp static const struct got_error *
6380 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
6381 8e7bd50a 2019-08-22 stsp {
6382 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
6383 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
6384 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
6385 8e7bd50a 2019-08-22 stsp
6386 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6387 8e7bd50a 2019-08-22 stsp
6388 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6389 8e7bd50a 2019-08-22 stsp if (err)
6390 8e7bd50a 2019-08-22 stsp return err;
6391 8e7bd50a 2019-08-22 stsp
6392 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
6393 8e7bd50a 2019-08-22 stsp const char *refname;
6394 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6395 8e7bd50a 2019-08-22 stsp char datebuf[26];
6396 d4efa91b 2020-01-14 stsp const char *tagger;
6397 8e7bd50a 2019-08-22 stsp time_t tagger_time;
6398 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
6399 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
6400 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
6401 8e7bd50a 2019-08-22 stsp
6402 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
6403 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
6404 8e7bd50a 2019-08-22 stsp continue;
6405 8e7bd50a 2019-08-22 stsp refname += 10;
6406 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
6407 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
6408 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
6409 8e7bd50a 2019-08-22 stsp break;
6410 8e7bd50a 2019-08-22 stsp }
6411 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6412 8e7bd50a 2019-08-22 stsp free(refstr);
6413 8e7bd50a 2019-08-22 stsp
6414 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
6415 8e7bd50a 2019-08-22 stsp if (err)
6416 8e7bd50a 2019-08-22 stsp break;
6417 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
6418 d4efa91b 2020-01-14 stsp if (err) {
6419 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
6420 d4efa91b 2020-01-14 stsp free(id);
6421 d4efa91b 2020-01-14 stsp break;
6422 d4efa91b 2020-01-14 stsp }
6423 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
6424 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
6425 d4efa91b 2020-01-14 stsp if (err) {
6426 d4efa91b 2020-01-14 stsp free(id);
6427 d4efa91b 2020-01-14 stsp break;
6428 d4efa91b 2020-01-14 stsp }
6429 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
6430 d4efa91b 2020-01-14 stsp tagger_time =
6431 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
6432 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
6433 d4efa91b 2020-01-14 stsp free(id);
6434 d4efa91b 2020-01-14 stsp if (err)
6435 d4efa91b 2020-01-14 stsp break;
6436 d4efa91b 2020-01-14 stsp } else {
6437 d4efa91b 2020-01-14 stsp free(id);
6438 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
6439 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
6440 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
6441 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
6442 d4efa91b 2020-01-14 stsp if (err)
6443 d4efa91b 2020-01-14 stsp break;
6444 d4efa91b 2020-01-14 stsp }
6445 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
6446 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
6447 2417344c 2019-08-23 stsp if (datestr)
6448 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
6449 d4efa91b 2020-01-14 stsp if (commit)
6450 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6451 d4efa91b 2020-01-14 stsp else {
6452 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
6453 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
6454 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6455 d4efa91b 2020-01-14 stsp id_str);
6456 d4efa91b 2020-01-14 stsp break;
6457 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
6458 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6459 d4efa91b 2020-01-14 stsp id_str);
6460 d4efa91b 2020-01-14 stsp break;
6461 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
6462 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6463 d4efa91b 2020-01-14 stsp id_str);
6464 d4efa91b 2020-01-14 stsp break;
6465 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
6466 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6467 d4efa91b 2020-01-14 stsp id_str);
6468 d4efa91b 2020-01-14 stsp break;
6469 d4efa91b 2020-01-14 stsp default:
6470 d4efa91b 2020-01-14 stsp break;
6471 d4efa91b 2020-01-14 stsp }
6472 8e7bd50a 2019-08-22 stsp }
6473 8e7bd50a 2019-08-22 stsp free(id_str);
6474 d4efa91b 2020-01-14 stsp if (commit) {
6475 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
6476 d4efa91b 2020-01-14 stsp if (err)
6477 d4efa91b 2020-01-14 stsp break;
6478 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
6479 d4efa91b 2020-01-14 stsp } else {
6480 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
6481 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
6482 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
6483 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
6484 d4efa91b 2020-01-14 stsp break;
6485 d4efa91b 2020-01-14 stsp }
6486 8e7bd50a 2019-08-22 stsp }
6487 8e7bd50a 2019-08-22 stsp
6488 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
6489 8e7bd50a 2019-08-22 stsp do {
6490 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
6491 8e7bd50a 2019-08-22 stsp if (line)
6492 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
6493 8e7bd50a 2019-08-22 stsp } while (line);
6494 8e7bd50a 2019-08-22 stsp free(tagmsg0);
6495 8e7bd50a 2019-08-22 stsp }
6496 8e7bd50a 2019-08-22 stsp
6497 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
6498 8e7bd50a 2019-08-22 stsp return NULL;
6499 8e7bd50a 2019-08-22 stsp }
6500 8e7bd50a 2019-08-22 stsp
6501 8e7bd50a 2019-08-22 stsp static const struct got_error *
6502 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6503 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
6504 8e7bd50a 2019-08-22 stsp {
6505 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6506 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
6507 f372d5cd 2019-10-21 stsp char *editor = NULL;
6508 1601cb9f 2020-09-11 naddy int initial_content_len;
6509 8e7bd50a 2019-08-22 stsp int fd = -1;
6510 8e7bd50a 2019-08-22 stsp
6511 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6512 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6513 8e7bd50a 2019-08-22 stsp goto done;
6514 8e7bd50a 2019-08-22 stsp }
6515 8e7bd50a 2019-08-22 stsp
6516 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6517 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
6518 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
6519 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
6520 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6521 8e7bd50a 2019-08-22 stsp goto done;
6522 8e7bd50a 2019-08-22 stsp }
6523 8e7bd50a 2019-08-22 stsp
6524 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6525 8e7bd50a 2019-08-22 stsp if (err)
6526 8e7bd50a 2019-08-22 stsp goto done;
6527 8e7bd50a 2019-08-22 stsp
6528 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6529 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
6530 97972933 2020-09-11 stsp goto done;
6531 97972933 2020-09-11 stsp }
6532 8e7bd50a 2019-08-22 stsp
6533 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
6534 8e7bd50a 2019-08-22 stsp if (err)
6535 8e7bd50a 2019-08-22 stsp goto done;
6536 bfa12d5e 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6537 0d5bb276 2020-12-15 stsp initial_content_len, 1);
6538 8e7bd50a 2019-08-22 stsp done:
6539 8e7bd50a 2019-08-22 stsp free(initial_content);
6540 8e7bd50a 2019-08-22 stsp free(template);
6541 8e7bd50a 2019-08-22 stsp free(editor);
6542 8e7bd50a 2019-08-22 stsp
6543 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6544 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
6545 97972933 2020-09-11 stsp
6546 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
6547 59f86c76 2020-09-11 stsp if (err == NULL)
6548 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
6549 59f86c76 2020-09-11 stsp if (err) {
6550 59f86c76 2020-09-11 stsp free(*tagmsg);
6551 59f86c76 2020-09-11 stsp *tagmsg = NULL;
6552 8e7bd50a 2019-08-22 stsp }
6553 8e7bd50a 2019-08-22 stsp return err;
6554 8e7bd50a 2019-08-22 stsp }
6555 8e7bd50a 2019-08-22 stsp
6556 8e7bd50a 2019-08-22 stsp static const struct got_error *
6557 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
6558 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6559 8e7bd50a 2019-08-22 stsp {
6560 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6561 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
6562 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
6563 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
6564 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6565 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
6566 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
6567 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6568 8e7bd50a 2019-08-22 stsp
6569 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6570 84de9106 2020-12-26 stsp
6571 8e7bd50a 2019-08-22 stsp /*
6572 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
6573 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
6574 8e7bd50a 2019-08-22 stsp * an unintended typo.
6575 8e7bd50a 2019-08-22 stsp */
6576 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
6577 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6578 8e7bd50a 2019-08-22 stsp
6579 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
6580 8e7bd50a 2019-08-22 stsp if (err)
6581 8e7bd50a 2019-08-22 stsp return err;
6582 8e7bd50a 2019-08-22 stsp
6583 84de9106 2020-12-26 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6584 84de9106 2020-12-26 stsp if (err)
6585 84de9106 2020-12-26 stsp goto done;
6586 84de9106 2020-12-26 stsp
6587 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6588 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &refs, repo);
6589 8e7bd50a 2019-08-22 stsp if (err)
6590 8e7bd50a 2019-08-22 stsp goto done;
6591 8e7bd50a 2019-08-22 stsp
6592 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
6593 8e7bd50a 2019-08-22 stsp if (err)
6594 8e7bd50a 2019-08-22 stsp goto done;
6595 8e7bd50a 2019-08-22 stsp
6596 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
6597 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
6598 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
6599 62d463ca 2020-10-20 naddy err = got_error_from_errno("strdup");
6600 62d463ca 2020-10-20 naddy goto done;
6601 8e7bd50a 2019-08-22 stsp }
6602 8e7bd50a 2019-08-22 stsp tag_name += 10;
6603 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6604 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
6605 62d463ca 2020-10-20 naddy goto done;
6606 8e7bd50a 2019-08-22 stsp }
6607 8e7bd50a 2019-08-22 stsp
6608 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
6609 8e7bd50a 2019-08-22 stsp if (err == NULL) {
6610 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
6611 8e7bd50a 2019-08-22 stsp goto done;
6612 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
6613 8e7bd50a 2019-08-22 stsp goto done;
6614 8e7bd50a 2019-08-22 stsp
6615 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
6616 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6617 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
6618 f372d5cd 2019-10-21 stsp if (err) {
6619 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6620 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
6621 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6622 8e7bd50a 2019-08-22 stsp goto done;
6623 f372d5cd 2019-10-21 stsp }
6624 8e7bd50a 2019-08-22 stsp }
6625 8e7bd50a 2019-08-22 stsp
6626 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
6627 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6628 f372d5cd 2019-10-21 stsp if (err) {
6629 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6630 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6631 8e7bd50a 2019-08-22 stsp goto done;
6632 f372d5cd 2019-10-21 stsp }
6633 8e7bd50a 2019-08-22 stsp
6634 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
6635 f372d5cd 2019-10-21 stsp if (err) {
6636 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6637 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6638 8e7bd50a 2019-08-22 stsp goto done;
6639 f372d5cd 2019-10-21 stsp }
6640 8e7bd50a 2019-08-22 stsp
6641 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
6642 f372d5cd 2019-10-21 stsp if (err) {
6643 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6644 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6645 f372d5cd 2019-10-21 stsp goto done;
6646 f372d5cd 2019-10-21 stsp }
6647 8e7bd50a 2019-08-22 stsp
6648 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
6649 f372d5cd 2019-10-21 stsp if (err) {
6650 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6651 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6652 f372d5cd 2019-10-21 stsp goto done;
6653 8e7bd50a 2019-08-22 stsp }
6654 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
6655 8e7bd50a 2019-08-22 stsp done:
6656 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
6657 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
6658 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
6659 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6660 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
6661 f372d5cd 2019-10-21 stsp free(tag_id_str);
6662 8e7bd50a 2019-08-22 stsp if (ref)
6663 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
6664 8e7bd50a 2019-08-22 stsp free(commit_id);
6665 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6666 8e7bd50a 2019-08-22 stsp free(refname);
6667 8e7bd50a 2019-08-22 stsp free(tagmsg);
6668 f372d5cd 2019-10-21 stsp free(tagmsg_path);
6669 aba9c984 2019-09-08 stsp free(tagger);
6670 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
6671 8e7bd50a 2019-08-22 stsp return err;
6672 8e7bd50a 2019-08-22 stsp }
6673 8e7bd50a 2019-08-22 stsp
6674 8e7bd50a 2019-08-22 stsp static const struct got_error *
6675 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
6676 8e7bd50a 2019-08-22 stsp {
6677 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
6678 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
6679 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
6680 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6681 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
6682 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6683 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
6684 8e7bd50a 2019-08-22 stsp
6685 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6686 8e7bd50a 2019-08-22 stsp switch (ch) {
6687 80106605 2020-02-24 stsp case 'c':
6688 80106605 2020-02-24 stsp commit_id_arg = optarg;
6689 80106605 2020-02-24 stsp break;
6690 8e7bd50a 2019-08-22 stsp case 'm':
6691 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6692 8e7bd50a 2019-08-22 stsp break;
6693 8e7bd50a 2019-08-22 stsp case 'r':
6694 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6695 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6696 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6697 9ba1d308 2019-10-21 stsp optarg);
6698 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6699 8e7bd50a 2019-08-22 stsp break;
6700 8e7bd50a 2019-08-22 stsp case 'l':
6701 8e7bd50a 2019-08-22 stsp do_list = 1;
6702 8e7bd50a 2019-08-22 stsp break;
6703 8e7bd50a 2019-08-22 stsp default:
6704 8e7bd50a 2019-08-22 stsp usage_tag();
6705 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6706 8e7bd50a 2019-08-22 stsp }
6707 8e7bd50a 2019-08-22 stsp }
6708 8e7bd50a 2019-08-22 stsp
6709 8e7bd50a 2019-08-22 stsp argc -= optind;
6710 8e7bd50a 2019-08-22 stsp argv += optind;
6711 8e7bd50a 2019-08-22 stsp
6712 8e7bd50a 2019-08-22 stsp if (do_list) {
6713 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6714 775ce909 2020-03-22 stsp errx(1,
6715 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
6716 8e7bd50a 2019-08-22 stsp if (tagmsg)
6717 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
6718 8e7bd50a 2019-08-22 stsp if (argc > 0)
6719 8e7bd50a 2019-08-22 stsp usage_tag();
6720 80106605 2020-02-24 stsp } else if (argc != 1)
6721 8e7bd50a 2019-08-22 stsp usage_tag();
6722 80106605 2020-02-24 stsp
6723 a2887370 2019-08-23 stsp tag_name = argv[0];
6724 8e7bd50a 2019-08-22 stsp
6725 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
6726 8e7bd50a 2019-08-22 stsp if (do_list) {
6727 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6728 8e7bd50a 2019-08-22 stsp NULL) == -1)
6729 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6730 8e7bd50a 2019-08-22 stsp } else {
6731 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6732 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
6733 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6734 8e7bd50a 2019-08-22 stsp }
6735 8e7bd50a 2019-08-22 stsp #endif
6736 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
6737 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
6738 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
6739 8e7bd50a 2019-08-22 stsp goto done;
6740 8e7bd50a 2019-08-22 stsp }
6741 8e7bd50a 2019-08-22 stsp
6742 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6743 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6744 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6745 8e7bd50a 2019-08-22 stsp goto done;
6746 8e7bd50a 2019-08-22 stsp else
6747 8e7bd50a 2019-08-22 stsp error = NULL;
6748 8e7bd50a 2019-08-22 stsp if (worktree) {
6749 8e7bd50a 2019-08-22 stsp repo_path =
6750 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
6751 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6752 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6753 8e7bd50a 2019-08-22 stsp if (error)
6754 8e7bd50a 2019-08-22 stsp goto done;
6755 8e7bd50a 2019-08-22 stsp } else {
6756 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
6757 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6758 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6759 8e7bd50a 2019-08-22 stsp goto done;
6760 8e7bd50a 2019-08-22 stsp }
6761 8e7bd50a 2019-08-22 stsp }
6762 8e7bd50a 2019-08-22 stsp }
6763 8e7bd50a 2019-08-22 stsp
6764 8e7bd50a 2019-08-22 stsp if (do_list) {
6765 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6766 c9956ddf 2019-09-08 stsp if (error != NULL)
6767 c9956ddf 2019-09-08 stsp goto done;
6768 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6769 8e7bd50a 2019-08-22 stsp if (error)
6770 8e7bd50a 2019-08-22 stsp goto done;
6771 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
6772 8e7bd50a 2019-08-22 stsp } else {
6773 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6774 c9956ddf 2019-09-08 stsp if (error)
6775 c9956ddf 2019-09-08 stsp goto done;
6776 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
6777 c9956ddf 2019-09-08 stsp if (error != NULL)
6778 c9956ddf 2019-09-08 stsp goto done;
6779 c9956ddf 2019-09-08 stsp
6780 8e7bd50a 2019-08-22 stsp if (tagmsg) {
6781 42a285e2 2020-10-01 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6782 8e7bd50a 2019-08-22 stsp if (error)
6783 8e7bd50a 2019-08-22 stsp goto done;
6784 8e7bd50a 2019-08-22 stsp }
6785 8e7bd50a 2019-08-22 stsp
6786 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
6787 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
6788 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
6789 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
6790 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
6791 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
6792 8e7bd50a 2019-08-22 stsp if (error)
6793 8e7bd50a 2019-08-22 stsp goto done;
6794 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6795 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
6796 8e7bd50a 2019-08-22 stsp if (error)
6797 8e7bd50a 2019-08-22 stsp goto done;
6798 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
6799 8e7bd50a 2019-08-22 stsp free(commit_id);
6800 8e7bd50a 2019-08-22 stsp if (error)
6801 8e7bd50a 2019-08-22 stsp goto done;
6802 8e7bd50a 2019-08-22 stsp }
6803 8e7bd50a 2019-08-22 stsp
6804 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
6805 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6806 8e7bd50a 2019-08-22 stsp }
6807 8e7bd50a 2019-08-22 stsp done:
6808 1d0f4054 2021-06-17 stsp if (repo) {
6809 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6810 1d0f4054 2021-06-17 stsp if (error == NULL)
6811 1d0f4054 2021-06-17 stsp error = close_err;
6812 1d0f4054 2021-06-17 stsp }
6813 8e7bd50a 2019-08-22 stsp if (worktree)
6814 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
6815 8e7bd50a 2019-08-22 stsp free(cwd);
6816 8e7bd50a 2019-08-22 stsp free(repo_path);
6817 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6818 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6819 8e7bd50a 2019-08-22 stsp return error;
6820 8e7bd50a 2019-08-22 stsp }
6821 8e7bd50a 2019-08-22 stsp
6822 8e7bd50a 2019-08-22 stsp __dead static void
6823 d00136be 2019-03-26 stsp usage_add(void)
6824 d00136be 2019-03-26 stsp {
6825 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6826 022fae89 2019-12-06 tracey getprogname());
6827 d00136be 2019-03-26 stsp exit(1);
6828 d00136be 2019-03-26 stsp }
6829 d00136be 2019-03-26 stsp
6830 d00136be 2019-03-26 stsp static const struct got_error *
6831 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6832 4e68cba3 2019-11-23 stsp {
6833 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6834 4e68cba3 2019-11-23 stsp path++;
6835 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6836 4e68cba3 2019-11-23 stsp return NULL;
6837 4e68cba3 2019-11-23 stsp }
6838 4e68cba3 2019-11-23 stsp
6839 4e68cba3 2019-11-23 stsp static const struct got_error *
6840 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6841 d00136be 2019-03-26 stsp {
6842 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6843 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6844 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6845 1dd54920 2019-05-11 stsp char *cwd = NULL;
6846 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6847 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6848 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6849 1dd54920 2019-05-11 stsp
6850 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6851 d00136be 2019-03-26 stsp
6852 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6853 d00136be 2019-03-26 stsp switch (ch) {
6854 022fae89 2019-12-06 tracey case 'I':
6855 022fae89 2019-12-06 tracey no_ignores = 1;
6856 022fae89 2019-12-06 tracey break;
6857 4e68cba3 2019-11-23 stsp case 'R':
6858 4e68cba3 2019-11-23 stsp can_recurse = 1;
6859 4e68cba3 2019-11-23 stsp break;
6860 d00136be 2019-03-26 stsp default:
6861 d00136be 2019-03-26 stsp usage_add();
6862 d00136be 2019-03-26 stsp /* NOTREACHED */
6863 d00136be 2019-03-26 stsp }
6864 d00136be 2019-03-26 stsp }
6865 d00136be 2019-03-26 stsp
6866 d00136be 2019-03-26 stsp argc -= optind;
6867 d00136be 2019-03-26 stsp argv += optind;
6868 d00136be 2019-03-26 stsp
6869 43012d58 2019-07-14 stsp #ifndef PROFILE
6870 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6871 43012d58 2019-07-14 stsp NULL) == -1)
6872 43012d58 2019-07-14 stsp err(1, "pledge");
6873 43012d58 2019-07-14 stsp #endif
6874 723c305c 2019-05-11 jcs if (argc < 1)
6875 d00136be 2019-03-26 stsp usage_add();
6876 d00136be 2019-03-26 stsp
6877 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6878 d00136be 2019-03-26 stsp if (cwd == NULL) {
6879 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6880 d00136be 2019-03-26 stsp goto done;
6881 d00136be 2019-03-26 stsp }
6882 723c305c 2019-05-11 jcs
6883 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6884 fa51e947 2020-03-27 stsp if (error) {
6885 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6886 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6887 d00136be 2019-03-26 stsp goto done;
6888 fa51e947 2020-03-27 stsp }
6889 d00136be 2019-03-26 stsp
6890 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6891 c9956ddf 2019-09-08 stsp NULL);
6892 031a5338 2019-03-26 stsp if (error != NULL)
6893 031a5338 2019-03-26 stsp goto done;
6894 031a5338 2019-03-26 stsp
6895 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6896 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6897 d00136be 2019-03-26 stsp if (error)
6898 d00136be 2019-03-26 stsp goto done;
6899 d00136be 2019-03-26 stsp
6900 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6901 6d022e97 2019-08-04 stsp if (error)
6902 022fae89 2019-12-06 tracey goto done;
6903 022fae89 2019-12-06 tracey
6904 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6905 4e68cba3 2019-11-23 stsp char *ondisk_path;
6906 4e68cba3 2019-11-23 stsp struct stat sb;
6907 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6908 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6909 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6910 62d463ca 2020-10-20 naddy pe->path) == -1) {
6911 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6912 4e68cba3 2019-11-23 stsp goto done;
6913 4e68cba3 2019-11-23 stsp }
6914 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6915 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6916 4e68cba3 2019-11-23 stsp free(ondisk_path);
6917 4e68cba3 2019-11-23 stsp continue;
6918 4e68cba3 2019-11-23 stsp }
6919 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6920 4e68cba3 2019-11-23 stsp ondisk_path);
6921 4e68cba3 2019-11-23 stsp free(ondisk_path);
6922 4e68cba3 2019-11-23 stsp goto done;
6923 4e68cba3 2019-11-23 stsp }
6924 4e68cba3 2019-11-23 stsp free(ondisk_path);
6925 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6926 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6927 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6928 4e68cba3 2019-11-23 stsp goto done;
6929 4e68cba3 2019-11-23 stsp }
6930 4e68cba3 2019-11-23 stsp }
6931 4e68cba3 2019-11-23 stsp }
6932 022fae89 2019-12-06 tracey
6933 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6934 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6935 d00136be 2019-03-26 stsp done:
6936 1d0f4054 2021-06-17 stsp if (repo) {
6937 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6938 1d0f4054 2021-06-17 stsp if (error == NULL)
6939 1d0f4054 2021-06-17 stsp error = close_err;
6940 1d0f4054 2021-06-17 stsp }
6941 d00136be 2019-03-26 stsp if (worktree)
6942 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6943 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6944 1dd54920 2019-05-11 stsp free((char *)pe->path);
6945 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6946 2ec1f75b 2019-03-26 stsp free(cwd);
6947 2ec1f75b 2019-03-26 stsp return error;
6948 2ec1f75b 2019-03-26 stsp }
6949 2ec1f75b 2019-03-26 stsp
6950 2ec1f75b 2019-03-26 stsp __dead static void
6951 648e4ef7 2019-07-09 stsp usage_remove(void)
6952 2ec1f75b 2019-03-26 stsp {
6953 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6954 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6955 2ec1f75b 2019-03-26 stsp exit(1);
6956 2a06fe5f 2019-08-24 stsp }
6957 2a06fe5f 2019-08-24 stsp
6958 2a06fe5f 2019-08-24 stsp static const struct got_error *
6959 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6960 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6961 2a06fe5f 2019-08-24 stsp {
6962 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6963 f2a9dc41 2019-12-13 tracey path++;
6964 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6965 2a06fe5f 2019-08-24 stsp return NULL;
6966 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6967 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6968 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6969 2a06fe5f 2019-08-24 stsp return NULL;
6970 2ec1f75b 2019-03-26 stsp }
6971 2ec1f75b 2019-03-26 stsp
6972 2ec1f75b 2019-03-26 stsp static const struct got_error *
6973 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6974 2ec1f75b 2019-03-26 stsp {
6975 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6976 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6977 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6978 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6979 17ed4618 2019-06-02 stsp char *cwd = NULL;
6980 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6981 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6982 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6983 2ec1f75b 2019-03-26 stsp
6984 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6985 17ed4618 2019-06-02 stsp
6986 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6987 2ec1f75b 2019-03-26 stsp switch (ch) {
6988 2ec1f75b 2019-03-26 stsp case 'f':
6989 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6990 2ec1f75b 2019-03-26 stsp break;
6991 70e3e7f5 2019-12-13 tracey case 'k':
6992 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6993 70e3e7f5 2019-12-13 tracey break;
6994 f2a9dc41 2019-12-13 tracey case 'R':
6995 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6996 f2a9dc41 2019-12-13 tracey break;
6997 766841c2 2020-08-13 stsp case 's':
6998 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6999 766841c2 2020-08-13 stsp switch (optarg[i]) {
7000 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
7001 766841c2 2020-08-13 stsp delete_local_mods = 1;
7002 766841c2 2020-08-13 stsp break;
7003 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
7004 766841c2 2020-08-13 stsp break;
7005 766841c2 2020-08-13 stsp default:
7006 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
7007 766841c2 2020-08-13 stsp optarg[i]);
7008 766841c2 2020-08-13 stsp }
7009 766841c2 2020-08-13 stsp }
7010 766841c2 2020-08-13 stsp status_codes = optarg;
7011 766841c2 2020-08-13 stsp break;
7012 2ec1f75b 2019-03-26 stsp default:
7013 f2a9dc41 2019-12-13 tracey usage_remove();
7014 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
7015 2ec1f75b 2019-03-26 stsp }
7016 2ec1f75b 2019-03-26 stsp }
7017 2ec1f75b 2019-03-26 stsp
7018 2ec1f75b 2019-03-26 stsp argc -= optind;
7019 2ec1f75b 2019-03-26 stsp argv += optind;
7020 2ec1f75b 2019-03-26 stsp
7021 43012d58 2019-07-14 stsp #ifndef PROFILE
7022 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7023 43012d58 2019-07-14 stsp NULL) == -1)
7024 43012d58 2019-07-14 stsp err(1, "pledge");
7025 43012d58 2019-07-14 stsp #endif
7026 17ed4618 2019-06-02 stsp if (argc < 1)
7027 648e4ef7 2019-07-09 stsp usage_remove();
7028 2ec1f75b 2019-03-26 stsp
7029 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
7030 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
7031 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7032 2ec1f75b 2019-03-26 stsp goto done;
7033 2ec1f75b 2019-03-26 stsp }
7034 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
7035 fa51e947 2020-03-27 stsp if (error) {
7036 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7037 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
7038 2ec1f75b 2019-03-26 stsp goto done;
7039 fa51e947 2020-03-27 stsp }
7040 2ec1f75b 2019-03-26 stsp
7041 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7042 c9956ddf 2019-09-08 stsp NULL);
7043 2af4a041 2019-05-11 jcs if (error)
7044 2ec1f75b 2019-03-26 stsp goto done;
7045 2ec1f75b 2019-03-26 stsp
7046 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
7047 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7048 2ec1f75b 2019-03-26 stsp if (error)
7049 2ec1f75b 2019-03-26 stsp goto done;
7050 2ec1f75b 2019-03-26 stsp
7051 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7052 6d022e97 2019-08-04 stsp if (error)
7053 6d022e97 2019-08-04 stsp goto done;
7054 17ed4618 2019-06-02 stsp
7055 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
7056 f2a9dc41 2019-12-13 tracey char *ondisk_path;
7057 f2a9dc41 2019-12-13 tracey struct stat sb;
7058 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
7059 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
7060 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
7061 62d463ca 2020-10-20 naddy pe->path) == -1) {
7062 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
7063 f2a9dc41 2019-12-13 tracey goto done;
7064 f2a9dc41 2019-12-13 tracey }
7065 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
7066 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
7067 f2a9dc41 2019-12-13 tracey free(ondisk_path);
7068 f2a9dc41 2019-12-13 tracey continue;
7069 f2a9dc41 2019-12-13 tracey }
7070 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
7071 f2a9dc41 2019-12-13 tracey ondisk_path);
7072 f2a9dc41 2019-12-13 tracey free(ondisk_path);
7073 f2a9dc41 2019-12-13 tracey goto done;
7074 f2a9dc41 2019-12-13 tracey }
7075 f2a9dc41 2019-12-13 tracey free(ondisk_path);
7076 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
7077 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
7078 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
7079 f2a9dc41 2019-12-13 tracey goto done;
7080 f2a9dc41 2019-12-13 tracey }
7081 f2a9dc41 2019-12-13 tracey }
7082 f2a9dc41 2019-12-13 tracey }
7083 f2a9dc41 2019-12-13 tracey
7084 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
7085 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
7086 766841c2 2020-08-13 stsp repo, keep_on_disk);
7087 a129376b 2019-03-28 stsp done:
7088 1d0f4054 2021-06-17 stsp if (repo) {
7089 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7090 1d0f4054 2021-06-17 stsp if (error == NULL)
7091 1d0f4054 2021-06-17 stsp error = close_err;
7092 1d0f4054 2021-06-17 stsp }
7093 a129376b 2019-03-28 stsp if (worktree)
7094 a129376b 2019-03-28 stsp got_worktree_close(worktree);
7095 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
7096 17ed4618 2019-06-02 stsp free((char *)pe->path);
7097 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
7098 a129376b 2019-03-28 stsp free(cwd);
7099 a129376b 2019-03-28 stsp return error;
7100 a129376b 2019-03-28 stsp }
7101 a129376b 2019-03-28 stsp
7102 a129376b 2019-03-28 stsp __dead static void
7103 a129376b 2019-03-28 stsp usage_revert(void)
7104 a129376b 2019-03-28 stsp {
7105 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
7106 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
7107 a129376b 2019-03-28 stsp exit(1);
7108 a129376b 2019-03-28 stsp }
7109 a129376b 2019-03-28 stsp
7110 1ee397ad 2019-07-12 stsp static const struct got_error *
7111 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
7112 a129376b 2019-03-28 stsp {
7113 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
7114 fb9704af 2020-01-27 stsp return NULL;
7115 fb9704af 2020-01-27 stsp
7116 a129376b 2019-03-28 stsp while (path[0] == '/')
7117 a129376b 2019-03-28 stsp path++;
7118 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
7119 33aa809d 2019-08-08 stsp return NULL;
7120 33aa809d 2019-08-08 stsp }
7121 33aa809d 2019-08-08 stsp
7122 33aa809d 2019-08-08 stsp struct choose_patch_arg {
7123 33aa809d 2019-08-08 stsp FILE *patch_script_file;
7124 33aa809d 2019-08-08 stsp const char *action;
7125 33aa809d 2019-08-08 stsp };
7126 33aa809d 2019-08-08 stsp
7127 33aa809d 2019-08-08 stsp static const struct got_error *
7128 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
7129 33aa809d 2019-08-08 stsp int nchanges, const char *action)
7130 33aa809d 2019-08-08 stsp {
7131 33aa809d 2019-08-08 stsp char *line = NULL;
7132 33aa809d 2019-08-08 stsp size_t linesize = 0;
7133 33aa809d 2019-08-08 stsp ssize_t linelen;
7134 33aa809d 2019-08-08 stsp
7135 33aa809d 2019-08-08 stsp switch (status) {
7136 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
7137 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
7138 33aa809d 2019-08-08 stsp break;
7139 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
7140 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
7141 33aa809d 2019-08-08 stsp break;
7142 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
7143 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
7144 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
7145 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
7146 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
7147 33aa809d 2019-08-08 stsp printf("%s", line);
7148 33aa809d 2019-08-08 stsp if (ferror(patch_file))
7149 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
7150 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
7151 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
7152 33aa809d 2019-08-08 stsp path, n, nchanges, action);
7153 33aa809d 2019-08-08 stsp break;
7154 33aa809d 2019-08-08 stsp default:
7155 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
7156 33aa809d 2019-08-08 stsp }
7157 33aa809d 2019-08-08 stsp
7158 33aa809d 2019-08-08 stsp return NULL;
7159 33aa809d 2019-08-08 stsp }
7160 33aa809d 2019-08-08 stsp
7161 33aa809d 2019-08-08 stsp static const struct got_error *
7162 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
7163 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
7164 33aa809d 2019-08-08 stsp {
7165 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
7166 33aa809d 2019-08-08 stsp char *line = NULL;
7167 33aa809d 2019-08-08 stsp size_t linesize = 0;
7168 33aa809d 2019-08-08 stsp ssize_t linelen;
7169 33aa809d 2019-08-08 stsp int resp = ' ';
7170 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
7171 33aa809d 2019-08-08 stsp
7172 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
7173 33aa809d 2019-08-08 stsp
7174 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
7175 33aa809d 2019-08-08 stsp char *nl;
7176 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
7177 33aa809d 2019-08-08 stsp a->action);
7178 33aa809d 2019-08-08 stsp if (err)
7179 33aa809d 2019-08-08 stsp return err;
7180 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
7181 33aa809d 2019-08-08 stsp if (linelen == -1) {
7182 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
7183 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
7184 33aa809d 2019-08-08 stsp return NULL;
7185 33aa809d 2019-08-08 stsp }
7186 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
7187 33aa809d 2019-08-08 stsp if (nl)
7188 33aa809d 2019-08-08 stsp *nl = '\0';
7189 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
7190 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
7191 33aa809d 2019-08-08 stsp printf("y\n");
7192 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
7193 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
7194 33aa809d 2019-08-08 stsp printf("n\n");
7195 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
7196 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
7197 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
7198 33aa809d 2019-08-08 stsp printf("q\n");
7199 33aa809d 2019-08-08 stsp } else
7200 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
7201 33aa809d 2019-08-08 stsp free(line);
7202 33aa809d 2019-08-08 stsp return NULL;
7203 33aa809d 2019-08-08 stsp }
7204 33aa809d 2019-08-08 stsp
7205 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
7206 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
7207 33aa809d 2019-08-08 stsp a->action);
7208 33aa809d 2019-08-08 stsp if (err)
7209 33aa809d 2019-08-08 stsp return err;
7210 33aa809d 2019-08-08 stsp resp = getchar();
7211 33aa809d 2019-08-08 stsp if (resp == '\n')
7212 33aa809d 2019-08-08 stsp resp = getchar();
7213 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
7214 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
7215 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
7216 33aa809d 2019-08-08 stsp resp = ' ';
7217 33aa809d 2019-08-08 stsp }
7218 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
7219 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
7220 33aa809d 2019-08-08 stsp resp = ' ';
7221 33aa809d 2019-08-08 stsp }
7222 33aa809d 2019-08-08 stsp }
7223 33aa809d 2019-08-08 stsp
7224 33aa809d 2019-08-08 stsp if (resp == 'y')
7225 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
7226 33aa809d 2019-08-08 stsp else if (resp == 'n')
7227 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
7228 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
7229 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
7230 33aa809d 2019-08-08 stsp
7231 1ee397ad 2019-07-12 stsp return NULL;
7232 a129376b 2019-03-28 stsp }
7233 a129376b 2019-03-28 stsp
7234 33aa809d 2019-08-08 stsp
7235 a129376b 2019-03-28 stsp static const struct got_error *
7236 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
7237 a129376b 2019-03-28 stsp {
7238 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
7239 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
7240 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
7241 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
7242 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
7243 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
7244 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
7245 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
7246 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
7247 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
7248 a129376b 2019-03-28 stsp
7249 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
7250 e20a8b6f 2019-06-04 stsp
7251 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
7252 a129376b 2019-03-28 stsp switch (ch) {
7253 33aa809d 2019-08-08 stsp case 'p':
7254 33aa809d 2019-08-08 stsp pflag = 1;
7255 33aa809d 2019-08-08 stsp break;
7256 33aa809d 2019-08-08 stsp case 'F':
7257 33aa809d 2019-08-08 stsp patch_script_path = optarg;
7258 33aa809d 2019-08-08 stsp break;
7259 0f6d7415 2019-08-08 stsp case 'R':
7260 0f6d7415 2019-08-08 stsp can_recurse = 1;
7261 0f6d7415 2019-08-08 stsp break;
7262 a129376b 2019-03-28 stsp default:
7263 a129376b 2019-03-28 stsp usage_revert();
7264 a129376b 2019-03-28 stsp /* NOTREACHED */
7265 a129376b 2019-03-28 stsp }
7266 a129376b 2019-03-28 stsp }
7267 a129376b 2019-03-28 stsp
7268 a129376b 2019-03-28 stsp argc -= optind;
7269 a129376b 2019-03-28 stsp argv += optind;
7270 a129376b 2019-03-28 stsp
7271 43012d58 2019-07-14 stsp #ifndef PROFILE
7272 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7273 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7274 43012d58 2019-07-14 stsp err(1, "pledge");
7275 43012d58 2019-07-14 stsp #endif
7276 e20a8b6f 2019-06-04 stsp if (argc < 1)
7277 a129376b 2019-03-28 stsp usage_revert();
7278 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
7279 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7280 a129376b 2019-03-28 stsp
7281 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
7282 a129376b 2019-03-28 stsp if (cwd == NULL) {
7283 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7284 a129376b 2019-03-28 stsp goto done;
7285 a129376b 2019-03-28 stsp }
7286 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
7287 fa51e947 2020-03-27 stsp if (error) {
7288 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7289 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
7290 2ec1f75b 2019-03-26 stsp goto done;
7291 fa51e947 2020-03-27 stsp }
7292 a129376b 2019-03-28 stsp
7293 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7294 c9956ddf 2019-09-08 stsp NULL);
7295 a129376b 2019-03-28 stsp if (error != NULL)
7296 a129376b 2019-03-28 stsp goto done;
7297 a129376b 2019-03-28 stsp
7298 33aa809d 2019-08-08 stsp if (patch_script_path) {
7299 c56c5d8a 2021-12-31 thomas patch_script_file = fopen(patch_script_path, "re");
7300 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
7301 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
7302 33aa809d 2019-08-08 stsp patch_script_path);
7303 33aa809d 2019-08-08 stsp goto done;
7304 33aa809d 2019-08-08 stsp }
7305 33aa809d 2019-08-08 stsp }
7306 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
7307 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7308 a129376b 2019-03-28 stsp if (error)
7309 a129376b 2019-03-28 stsp goto done;
7310 a129376b 2019-03-28 stsp
7311 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7312 6d022e97 2019-08-04 stsp if (error)
7313 6d022e97 2019-08-04 stsp goto done;
7314 00db391e 2019-08-03 stsp
7315 0f6d7415 2019-08-08 stsp if (!can_recurse) {
7316 0f6d7415 2019-08-08 stsp char *ondisk_path;
7317 0f6d7415 2019-08-08 stsp struct stat sb;
7318 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
7319 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
7320 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
7321 62d463ca 2020-10-20 naddy pe->path) == -1) {
7322 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
7323 0f6d7415 2019-08-08 stsp goto done;
7324 0f6d7415 2019-08-08 stsp }
7325 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
7326 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
7327 0f6d7415 2019-08-08 stsp free(ondisk_path);
7328 0f6d7415 2019-08-08 stsp continue;
7329 0f6d7415 2019-08-08 stsp }
7330 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
7331 0f6d7415 2019-08-08 stsp ondisk_path);
7332 0f6d7415 2019-08-08 stsp free(ondisk_path);
7333 0f6d7415 2019-08-08 stsp goto done;
7334 0f6d7415 2019-08-08 stsp }
7335 0f6d7415 2019-08-08 stsp free(ondisk_path);
7336 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
7337 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
7338 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
7339 0f6d7415 2019-08-08 stsp goto done;
7340 0f6d7415 2019-08-08 stsp }
7341 0f6d7415 2019-08-08 stsp }
7342 0f6d7415 2019-08-08 stsp }
7343 0f6d7415 2019-08-08 stsp
7344 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7345 33aa809d 2019-08-08 stsp cpa.action = "revert";
7346 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7347 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7348 2ec1f75b 2019-03-26 stsp done:
7349 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7350 33aa809d 2019-08-08 stsp error == NULL)
7351 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7352 1d0f4054 2021-06-17 stsp if (repo) {
7353 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7354 1d0f4054 2021-06-17 stsp if (error == NULL)
7355 1d0f4054 2021-06-17 stsp error = close_err;
7356 1d0f4054 2021-06-17 stsp }
7357 2ec1f75b 2019-03-26 stsp if (worktree)
7358 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
7359 2ec1f75b 2019-03-26 stsp free(path);
7360 d00136be 2019-03-26 stsp free(cwd);
7361 6bad629b 2019-02-04 stsp return error;
7362 c4296144 2019-05-09 stsp }
7363 c4296144 2019-05-09 stsp
7364 c4296144 2019-05-09 stsp __dead static void
7365 c4296144 2019-05-09 stsp usage_commit(void)
7366 c4296144 2019-05-09 stsp {
7367 28cf319f 2021-01-28 stsp fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
7368 28cf319f 2021-01-28 stsp "[path ...]\n", getprogname());
7369 c4296144 2019-05-09 stsp exit(1);
7370 33ad4cbe 2019-05-12 jcs }
7371 33ad4cbe 2019-05-12 jcs
7372 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
7373 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
7374 28cf319f 2021-01-28 stsp const char *prepared_log;
7375 28cf319f 2021-01-28 stsp int non_interactive;
7376 e2ba3d07 2019-05-13 stsp const char *editor;
7377 e0870e44 2019-05-13 stsp const char *worktree_path;
7378 76d98825 2019-06-03 stsp const char *branch_name;
7379 314a6357 2019-05-13 stsp const char *repo_path;
7380 e0870e44 2019-05-13 stsp char *logmsg_path;
7381 e2ba3d07 2019-05-13 stsp
7382 e2ba3d07 2019-05-13 stsp };
7383 28cf319f 2021-01-28 stsp
7384 28cf319f 2021-01-28 stsp static const struct got_error *
7385 28cf319f 2021-01-28 stsp read_prepared_logmsg(char **logmsg, const char *path)
7386 28cf319f 2021-01-28 stsp {
7387 28cf319f 2021-01-28 stsp const struct got_error *err = NULL;
7388 28cf319f 2021-01-28 stsp FILE *f = NULL;
7389 28cf319f 2021-01-28 stsp struct stat sb;
7390 28cf319f 2021-01-28 stsp size_t r;
7391 28cf319f 2021-01-28 stsp
7392 28cf319f 2021-01-28 stsp *logmsg = NULL;
7393 28cf319f 2021-01-28 stsp memset(&sb, 0, sizeof(sb));
7394 28cf319f 2021-01-28 stsp
7395 c56c5d8a 2021-12-31 thomas f = fopen(path, "re");
7396 28cf319f 2021-01-28 stsp if (f == NULL)
7397 28cf319f 2021-01-28 stsp return got_error_from_errno2("fopen", path);
7398 28cf319f 2021-01-28 stsp
7399 28cf319f 2021-01-28 stsp if (fstat(fileno(f), &sb) == -1) {
7400 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fstat", path);
7401 28cf319f 2021-01-28 stsp goto done;
7402 28cf319f 2021-01-28 stsp }
7403 28cf319f 2021-01-28 stsp if (sb.st_size == 0) {
7404 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7405 28cf319f 2021-01-28 stsp goto done;
7406 28cf319f 2021-01-28 stsp }
7407 28cf319f 2021-01-28 stsp
7408 28cf319f 2021-01-28 stsp *logmsg = malloc(sb.st_size + 1);
7409 28cf319f 2021-01-28 stsp if (*logmsg == NULL) {
7410 28cf319f 2021-01-28 stsp err = got_error_from_errno("malloc");
7411 28cf319f 2021-01-28 stsp goto done;
7412 28cf319f 2021-01-28 stsp }
7413 28cf319f 2021-01-28 stsp
7414 28cf319f 2021-01-28 stsp r = fread(*logmsg, 1, sb.st_size, f);
7415 28cf319f 2021-01-28 stsp if (r != sb.st_size) {
7416 28cf319f 2021-01-28 stsp if (ferror(f))
7417 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fread", path);
7418 28cf319f 2021-01-28 stsp else
7419 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_IO);
7420 28cf319f 2021-01-28 stsp goto done;
7421 28cf319f 2021-01-28 stsp }
7422 28cf319f 2021-01-28 stsp (*logmsg)[sb.st_size] = '\0';
7423 28cf319f 2021-01-28 stsp done:
7424 28cf319f 2021-01-28 stsp if (fclose(f) == EOF && err == NULL)
7425 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fclose", path);
7426 28cf319f 2021-01-28 stsp if (err) {
7427 28cf319f 2021-01-28 stsp free(*logmsg);
7428 28cf319f 2021-01-28 stsp *logmsg = NULL;
7429 28cf319f 2021-01-28 stsp }
7430 28cf319f 2021-01-28 stsp return err;
7431 28cf319f 2021-01-28 stsp
7432 28cf319f 2021-01-28 stsp }
7433 e0870e44 2019-05-13 stsp
7434 33ad4cbe 2019-05-12 jcs static const struct got_error *
7435 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
7436 33ad4cbe 2019-05-12 jcs void *arg)
7437 33ad4cbe 2019-05-12 jcs {
7438 76d98825 2019-06-03 stsp char *initial_content = NULL;
7439 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
7440 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
7441 e0870e44 2019-05-13 stsp char *template = NULL;
7442 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
7443 1601cb9f 2020-09-11 naddy int initial_content_len;
7444 97972933 2020-09-11 stsp int fd = -1;
7445 33ad4cbe 2019-05-12 jcs size_t len;
7446 33ad4cbe 2019-05-12 jcs
7447 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
7448 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
7449 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
7450 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
7451 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
7452 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
7453 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
7454 33ad4cbe 2019-05-12 jcs return NULL;
7455 28cf319f 2021-01-28 stsp } else if (a->prepared_log != NULL && a->non_interactive)
7456 28cf319f 2021-01-28 stsp return read_prepared_logmsg(logmsg, a->prepared_log);
7457 33ad4cbe 2019-05-12 jcs
7458 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7459 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
7460 76d98825 2019-06-03 stsp
7461 28cf319f 2021-01-28 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7462 28cf319f 2021-01-28 stsp if (err)
7463 28cf319f 2021-01-28 stsp goto done;
7464 28cf319f 2021-01-28 stsp
7465 28cf319f 2021-01-28 stsp if (a->prepared_log) {
7466 28cf319f 2021-01-28 stsp char *msg;
7467 28cf319f 2021-01-28 stsp err = read_prepared_logmsg(&msg, a->prepared_log);
7468 28cf319f 2021-01-28 stsp if (err)
7469 28cf319f 2021-01-28 stsp goto done;
7470 28cf319f 2021-01-28 stsp if (write(fd, msg, strlen(msg)) == -1) {
7471 28cf319f 2021-01-28 stsp err = got_error_from_errno2("write", a->logmsg_path);
7472 28cf319f 2021-01-28 stsp free(msg);
7473 28cf319f 2021-01-28 stsp goto done;
7474 28cf319f 2021-01-28 stsp }
7475 28cf319f 2021-01-28 stsp free(msg);
7476 28cf319f 2021-01-28 stsp }
7477 28cf319f 2021-01-28 stsp
7478 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
7479 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
7480 1601cb9f 2020-09-11 naddy a->branch_name);
7481 28cf319f 2021-01-28 stsp if (initial_content_len == -1) {
7482 28cf319f 2021-01-28 stsp err = got_error_from_errno("asprintf");
7483 e0870e44 2019-05-13 stsp goto done;
7484 28cf319f 2021-01-28 stsp }
7485 33ad4cbe 2019-05-12 jcs
7486 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
7487 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
7488 97972933 2020-09-11 stsp goto done;
7489 97972933 2020-09-11 stsp }
7490 33ad4cbe 2019-05-12 jcs
7491 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
7492 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
7493 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
7494 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
7495 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
7496 33ad4cbe 2019-05-12 jcs }
7497 33ad4cbe 2019-05-12 jcs
7498 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7499 28cf319f 2021-01-28 stsp initial_content_len, a->prepared_log ? 0 : 1);
7500 33ad4cbe 2019-05-12 jcs done:
7501 76d98825 2019-06-03 stsp free(initial_content);
7502 e0870e44 2019-05-13 stsp free(template);
7503 314a6357 2019-05-13 stsp
7504 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
7505 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
7506 97972933 2020-09-11 stsp
7507 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
7508 59f86c76 2020-09-11 stsp if (err == NULL)
7509 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
7510 59f86c76 2020-09-11 stsp if (err) {
7511 59f86c76 2020-09-11 stsp free(*logmsg);
7512 59f86c76 2020-09-11 stsp *logmsg = NULL;
7513 3ce1b845 2019-07-15 stsp }
7514 33ad4cbe 2019-05-12 jcs return err;
7515 6bad629b 2019-02-04 stsp }
7516 c4296144 2019-05-09 stsp
7517 c4296144 2019-05-09 stsp static const struct got_error *
7518 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
7519 c4296144 2019-05-09 stsp {
7520 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
7521 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
7522 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
7523 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
7524 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
7525 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
7526 28cf319f 2021-01-28 stsp char *prepared_logmsg = NULL;
7527 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
7528 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7529 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7530 10604dce 2021-09-24 thomas int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
7531 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
7532 5c1e53bc 2019-07-28 stsp
7533 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
7534 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
7535 c4296144 2019-05-09 stsp
7536 28cf319f 2021-01-28 stsp while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7537 c4296144 2019-05-09 stsp switch (ch) {
7538 28cf319f 2021-01-28 stsp case 'F':
7539 28cf319f 2021-01-28 stsp if (logmsg != NULL)
7540 28cf319f 2021-01-28 stsp option_conflict('F', 'm');
7541 28cf319f 2021-01-28 stsp prepared_logmsg = realpath(optarg, NULL);
7542 28cf319f 2021-01-28 stsp if (prepared_logmsg == NULL)
7543 28cf319f 2021-01-28 stsp return got_error_from_errno2("realpath",
7544 28cf319f 2021-01-28 stsp optarg);
7545 28cf319f 2021-01-28 stsp break;
7546 c4296144 2019-05-09 stsp case 'm':
7547 28cf319f 2021-01-28 stsp if (prepared_logmsg)
7548 28cf319f 2021-01-28 stsp option_conflict('m', 'F');
7549 c4296144 2019-05-09 stsp logmsg = optarg;
7550 28cf319f 2021-01-28 stsp break;
7551 28cf319f 2021-01-28 stsp case 'N':
7552 28cf319f 2021-01-28 stsp non_interactive = 1;
7553 c4296144 2019-05-09 stsp break;
7554 35213c7c 2020-07-23 stsp case 'S':
7555 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
7556 35213c7c 2020-07-23 stsp break;
7557 c4296144 2019-05-09 stsp default:
7558 c4296144 2019-05-09 stsp usage_commit();
7559 c4296144 2019-05-09 stsp /* NOTREACHED */
7560 c4296144 2019-05-09 stsp }
7561 c4296144 2019-05-09 stsp }
7562 c4296144 2019-05-09 stsp
7563 c4296144 2019-05-09 stsp argc -= optind;
7564 c4296144 2019-05-09 stsp argv += optind;
7565 c4296144 2019-05-09 stsp
7566 43012d58 2019-07-14 stsp #ifndef PROFILE
7567 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7568 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7569 43012d58 2019-07-14 stsp err(1, "pledge");
7570 43012d58 2019-07-14 stsp #endif
7571 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
7572 c4296144 2019-05-09 stsp if (cwd == NULL) {
7573 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7574 c4296144 2019-05-09 stsp goto done;
7575 c4296144 2019-05-09 stsp }
7576 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
7577 fa51e947 2020-03-27 stsp if (error) {
7578 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7579 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
7580 7d5807f4 2019-07-11 stsp goto done;
7581 fa51e947 2020-03-27 stsp }
7582 7d5807f4 2019-07-11 stsp
7583 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7584 c4296144 2019-05-09 stsp if (error)
7585 a698f62e 2019-07-25 stsp goto done;
7586 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
7587 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
7588 c4296144 2019-05-09 stsp goto done;
7589 a698f62e 2019-07-25 stsp }
7590 5c1e53bc 2019-07-28 stsp
7591 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7592 916f288c 2019-07-30 stsp worktree);
7593 5c1e53bc 2019-07-28 stsp if (error)
7594 5c1e53bc 2019-07-28 stsp goto done;
7595 c4296144 2019-05-09 stsp
7596 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
7597 c9956ddf 2019-09-08 stsp if (error)
7598 c9956ddf 2019-09-08 stsp goto done;
7599 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7600 c9956ddf 2019-09-08 stsp gitconfig_path);
7601 c4296144 2019-05-09 stsp if (error != NULL)
7602 c4296144 2019-05-09 stsp goto done;
7603 c4296144 2019-05-09 stsp
7604 10604dce 2021-09-24 thomas error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
7605 10604dce 2021-09-24 thomas if (error)
7606 10604dce 2021-09-24 thomas goto done;
7607 10604dce 2021-09-24 thomas if (merge_in_progress) {
7608 10604dce 2021-09-24 thomas error = got_error(GOT_ERR_MERGE_BUSY);
7609 10604dce 2021-09-24 thomas goto done;
7610 10604dce 2021-09-24 thomas }
7611 10604dce 2021-09-24 thomas
7612 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
7613 aba9c984 2019-09-08 stsp if (error)
7614 aba9c984 2019-09-08 stsp return error;
7615 aba9c984 2019-09-08 stsp
7616 314a6357 2019-05-13 stsp /*
7617 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
7618 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
7619 314a6357 2019-05-13 stsp */
7620 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
7621 314a6357 2019-05-13 stsp error = get_editor(&editor);
7622 314a6357 2019-05-13 stsp else
7623 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7624 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7625 c4296144 2019-05-09 stsp if (error)
7626 c4296144 2019-05-09 stsp goto done;
7627 c4296144 2019-05-09 stsp
7628 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7629 087fb88c 2019-08-04 stsp if (error)
7630 087fb88c 2019-08-04 stsp goto done;
7631 087fb88c 2019-08-04 stsp
7632 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
7633 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
7634 28cf319f 2021-01-28 stsp cl_arg.prepared_log = prepared_logmsg;
7635 28cf319f 2021-01-28 stsp cl_arg.non_interactive = non_interactive;
7636 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7637 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7638 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
7639 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7640 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
7641 916f288c 2019-07-30 stsp goto done;
7642 916f288c 2019-07-30 stsp }
7643 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
7644 916f288c 2019-07-30 stsp }
7645 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
7646 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7647 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7648 35213c7c 2020-07-23 stsp print_status, NULL, repo);
7649 e0870e44 2019-05-13 stsp if (error) {
7650 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7651 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
7652 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
7653 c4296144 2019-05-09 stsp goto done;
7654 e0870e44 2019-05-13 stsp }
7655 c4296144 2019-05-09 stsp
7656 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
7657 c4296144 2019-05-09 stsp if (error)
7658 c4296144 2019-05-09 stsp goto done;
7659 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
7660 c4296144 2019-05-09 stsp done:
7661 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
7662 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
7663 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
7664 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7665 7266f21f 2019-10-21 stsp error == NULL)
7666 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7667 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
7668 1d0f4054 2021-06-17 stsp if (repo) {
7669 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7670 1d0f4054 2021-06-17 stsp if (error == NULL)
7671 1d0f4054 2021-06-17 stsp error = close_err;
7672 1d0f4054 2021-06-17 stsp }
7673 c4296144 2019-05-09 stsp if (worktree)
7674 c4296144 2019-05-09 stsp got_worktree_close(worktree);
7675 c4296144 2019-05-09 stsp free(cwd);
7676 c4296144 2019-05-09 stsp free(id_str);
7677 c9956ddf 2019-09-08 stsp free(gitconfig_path);
7678 e2ba3d07 2019-05-13 stsp free(editor);
7679 aba9c984 2019-09-08 stsp free(author);
7680 28cf319f 2021-01-28 stsp free(prepared_logmsg);
7681 f8a36e22 2021-08-26 stsp return error;
7682 f8a36e22 2021-08-26 stsp }
7683 f8a36e22 2021-08-26 stsp
7684 f8a36e22 2021-08-26 stsp __dead static void
7685 f8a36e22 2021-08-26 stsp usage_send(void)
7686 f8a36e22 2021-08-26 stsp {
7687 f8a36e22 2021-08-26 stsp fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
7688 f8a36e22 2021-08-26 stsp "[-r repository-path] [-t tag] [-T] [-q] [-v] "
7689 f8a36e22 2021-08-26 stsp "[remote-repository]\n", getprogname());
7690 f8a36e22 2021-08-26 stsp exit(1);
7691 f8a36e22 2021-08-26 stsp }
7692 f8a36e22 2021-08-26 stsp
7693 f8a36e22 2021-08-26 stsp struct got_send_progress_arg {
7694 f8a36e22 2021-08-26 stsp char last_scaled_packsize[FMT_SCALED_STRSIZE];
7695 f8a36e22 2021-08-26 stsp int verbosity;
7696 f8a36e22 2021-08-26 stsp int last_ncommits;
7697 f8a36e22 2021-08-26 stsp int last_nobj_total;
7698 f8a36e22 2021-08-26 stsp int last_p_deltify;
7699 f8a36e22 2021-08-26 stsp int last_p_written;
7700 f8a36e22 2021-08-26 stsp int last_p_sent;
7701 f8a36e22 2021-08-26 stsp int printed_something;
7702 f8a36e22 2021-08-26 stsp int sent_something;
7703 f8a36e22 2021-08-26 stsp struct got_pathlist_head *delete_branches;
7704 f8a36e22 2021-08-26 stsp };
7705 f8a36e22 2021-08-26 stsp
7706 f8a36e22 2021-08-26 stsp static const struct got_error *
7707 f8a36e22 2021-08-26 stsp send_progress(void *arg, off_t packfile_size, int ncommits, int nobj_total,
7708 f8a36e22 2021-08-26 stsp int nobj_deltify, int nobj_written, off_t bytes_sent, const char *refname,
7709 f8a36e22 2021-08-26 stsp int success)
7710 f8a36e22 2021-08-26 stsp {
7711 f8a36e22 2021-08-26 stsp struct got_send_progress_arg *a = arg;
7712 f8a36e22 2021-08-26 stsp char scaled_packsize[FMT_SCALED_STRSIZE];
7713 f8a36e22 2021-08-26 stsp char scaled_sent[FMT_SCALED_STRSIZE];
7714 f8a36e22 2021-08-26 stsp int p_deltify = 0, p_written = 0, p_sent = 0;
7715 f8a36e22 2021-08-26 stsp int print_searching = 0, print_total = 0;
7716 f8a36e22 2021-08-26 stsp int print_deltify = 0, print_written = 0, print_sent = 0;
7717 f8a36e22 2021-08-26 stsp
7718 f8a36e22 2021-08-26 stsp if (a->verbosity < 0)
7719 f8a36e22 2021-08-26 stsp return NULL;
7720 f8a36e22 2021-08-26 stsp
7721 f8a36e22 2021-08-26 stsp if (refname) {
7722 f8a36e22 2021-08-26 stsp const char *status = success ? "accepted" : "rejected";
7723 f8a36e22 2021-08-26 stsp
7724 f8a36e22 2021-08-26 stsp if (success) {
7725 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
7726 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, a->delete_branches, entry) {
7727 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
7728 f8a36e22 2021-08-26 stsp if (got_path_cmp(branchname, refname,
7729 f8a36e22 2021-08-26 stsp strlen(branchname), strlen(refname)) == 0) {
7730 f8a36e22 2021-08-26 stsp status = "deleted";
7731 27b75514 2021-08-28 stsp a->sent_something = 1;
7732 f8a36e22 2021-08-26 stsp break;
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 27b75514 2021-08-28 stsp if (a->printed_something)
7738 27b75514 2021-08-28 stsp putchar('\n');
7739 27b75514 2021-08-28 stsp printf("Server has %s %s", status, refname);
7740 f8a36e22 2021-08-26 stsp a->printed_something = 1;
7741 f8a36e22 2021-08-26 stsp return NULL;
7742 f8a36e22 2021-08-26 stsp }
7743 f8a36e22 2021-08-26 stsp
7744 f8a36e22 2021-08-26 stsp if (fmt_scaled(packfile_size, scaled_packsize) == -1)
7745 f8a36e22 2021-08-26 stsp return got_error_from_errno("fmt_scaled");
7746 f8a36e22 2021-08-26 stsp if (fmt_scaled(bytes_sent, scaled_sent) == -1)
7747 f8a36e22 2021-08-26 stsp return got_error_from_errno("fmt_scaled");
7748 f8a36e22 2021-08-26 stsp
7749 f8a36e22 2021-08-26 stsp if (a->last_ncommits != ncommits) {
7750 f8a36e22 2021-08-26 stsp print_searching = 1;
7751 f8a36e22 2021-08-26 stsp a->last_ncommits = ncommits;
7752 f8a36e22 2021-08-26 stsp }
7753 f8a36e22 2021-08-26 stsp
7754 f8a36e22 2021-08-26 stsp if (a->last_nobj_total != nobj_total) {
7755 f8a36e22 2021-08-26 stsp print_searching = 1;
7756 f8a36e22 2021-08-26 stsp print_total = 1;
7757 f8a36e22 2021-08-26 stsp a->last_nobj_total = nobj_total;
7758 f8a36e22 2021-08-26 stsp }
7759 f8a36e22 2021-08-26 stsp
7760 f8a36e22 2021-08-26 stsp if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
7761 f8a36e22 2021-08-26 stsp strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
7762 f8a36e22 2021-08-26 stsp if (strlcpy(a->last_scaled_packsize, scaled_packsize,
7763 f8a36e22 2021-08-26 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
7764 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
7765 f8a36e22 2021-08-26 stsp }
7766 f8a36e22 2021-08-26 stsp
7767 f8a36e22 2021-08-26 stsp if (nobj_deltify > 0 || nobj_written > 0) {
7768 f8a36e22 2021-08-26 stsp if (nobj_deltify > 0) {
7769 f8a36e22 2021-08-26 stsp p_deltify = (nobj_deltify * 100) / nobj_total;
7770 f8a36e22 2021-08-26 stsp if (p_deltify != a->last_p_deltify) {
7771 f8a36e22 2021-08-26 stsp a->last_p_deltify = p_deltify;
7772 f8a36e22 2021-08-26 stsp print_searching = 1;
7773 f8a36e22 2021-08-26 stsp print_total = 1;
7774 f8a36e22 2021-08-26 stsp print_deltify = 1;
7775 f8a36e22 2021-08-26 stsp }
7776 f8a36e22 2021-08-26 stsp }
7777 f8a36e22 2021-08-26 stsp if (nobj_written > 0) {
7778 f8a36e22 2021-08-26 stsp p_written = (nobj_written * 100) / nobj_total;
7779 f8a36e22 2021-08-26 stsp if (p_written != a->last_p_written) {
7780 f8a36e22 2021-08-26 stsp a->last_p_written = p_written;
7781 f8a36e22 2021-08-26 stsp print_searching = 1;
7782 f8a36e22 2021-08-26 stsp print_total = 1;
7783 f8a36e22 2021-08-26 stsp print_deltify = 1;
7784 f8a36e22 2021-08-26 stsp print_written = 1;
7785 f8a36e22 2021-08-26 stsp }
7786 f8a36e22 2021-08-26 stsp }
7787 f8a36e22 2021-08-26 stsp }
7788 f8a36e22 2021-08-26 stsp
7789 f8a36e22 2021-08-26 stsp if (bytes_sent > 0) {
7790 f8a36e22 2021-08-26 stsp p_sent = (bytes_sent * 100) / packfile_size;
7791 f8a36e22 2021-08-26 stsp if (p_sent != a->last_p_sent) {
7792 f8a36e22 2021-08-26 stsp a->last_p_sent = p_sent;
7793 f8a36e22 2021-08-26 stsp print_searching = 1;
7794 f8a36e22 2021-08-26 stsp print_total = 1;
7795 f8a36e22 2021-08-26 stsp print_deltify = 1;
7796 f8a36e22 2021-08-26 stsp print_written = 1;
7797 f8a36e22 2021-08-26 stsp print_sent = 1;
7798 f8a36e22 2021-08-26 stsp }
7799 f8a36e22 2021-08-26 stsp a->sent_something = 1;
7800 f8a36e22 2021-08-26 stsp }
7801 f8a36e22 2021-08-26 stsp
7802 f8a36e22 2021-08-26 stsp if (print_searching || print_total || print_deltify || print_written ||
7803 f8a36e22 2021-08-26 stsp print_sent)
7804 f8a36e22 2021-08-26 stsp printf("\r");
7805 f8a36e22 2021-08-26 stsp if (print_searching)
7806 f8a36e22 2021-08-26 stsp printf("packing %d reference%s", ncommits,
7807 f8a36e22 2021-08-26 stsp ncommits == 1 ? "" : "s");
7808 f8a36e22 2021-08-26 stsp if (print_total)
7809 f8a36e22 2021-08-26 stsp printf("; %d object%s", nobj_total,
7810 f8a36e22 2021-08-26 stsp nobj_total == 1 ? "" : "s");
7811 f8a36e22 2021-08-26 stsp if (print_deltify)
7812 f8a36e22 2021-08-26 stsp printf("; deltify: %d%%", p_deltify);
7813 f8a36e22 2021-08-26 stsp if (print_sent)
7814 f8a36e22 2021-08-26 stsp printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE,
7815 f8a36e22 2021-08-26 stsp scaled_packsize, p_sent);
7816 f8a36e22 2021-08-26 stsp else if (print_written)
7817 f8a36e22 2021-08-26 stsp printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
7818 f8a36e22 2021-08-26 stsp scaled_packsize, p_written);
7819 f8a36e22 2021-08-26 stsp if (print_searching || print_total || print_deltify ||
7820 f8a36e22 2021-08-26 stsp print_written || print_sent) {
7821 f8a36e22 2021-08-26 stsp a->printed_something = 1;
7822 f8a36e22 2021-08-26 stsp fflush(stdout);
7823 f8a36e22 2021-08-26 stsp }
7824 f8a36e22 2021-08-26 stsp return NULL;
7825 f8a36e22 2021-08-26 stsp }
7826 f8a36e22 2021-08-26 stsp
7827 f8a36e22 2021-08-26 stsp static const struct got_error *
7828 f8a36e22 2021-08-26 stsp cmd_send(int argc, char *argv[])
7829 f8a36e22 2021-08-26 stsp {
7830 f8a36e22 2021-08-26 stsp const struct got_error *error = NULL;
7831 f8a36e22 2021-08-26 stsp char *cwd = NULL, *repo_path = NULL;
7832 f8a36e22 2021-08-26 stsp const char *remote_name;
7833 f8a36e22 2021-08-26 stsp char *proto = NULL, *host = NULL, *port = NULL;
7834 f8a36e22 2021-08-26 stsp char *repo_name = NULL, *server_path = NULL;
7835 f8a36e22 2021-08-26 stsp const struct got_remote_repo *remotes, *remote = NULL;
7836 f8a36e22 2021-08-26 stsp int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
7837 f8a36e22 2021-08-26 stsp struct got_repository *repo = NULL;
7838 f8a36e22 2021-08-26 stsp struct got_worktree *worktree = NULL;
7839 f8a36e22 2021-08-26 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
7840 f8a36e22 2021-08-26 stsp struct got_pathlist_head branches;
7841 f8a36e22 2021-08-26 stsp struct got_pathlist_head tags;
7842 f8a36e22 2021-08-26 stsp struct got_reflist_head all_branches;
7843 f8a36e22 2021-08-26 stsp struct got_reflist_head all_tags;
7844 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_args;
7845 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_branches;
7846 f8a36e22 2021-08-26 stsp struct got_reflist_entry *re;
7847 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
7848 f8a36e22 2021-08-26 stsp int i, ch, sendfd = -1, sendstatus;
7849 f8a36e22 2021-08-26 stsp pid_t sendpid = -1;
7850 f8a36e22 2021-08-26 stsp struct got_send_progress_arg spa;
7851 f8a36e22 2021-08-26 stsp int verbosity = 0, overwrite_refs = 0;
7852 f8a36e22 2021-08-26 stsp int send_all_branches = 0, send_all_tags = 0;
7853 f8a36e22 2021-08-26 stsp struct got_reference *ref = NULL;
7854 f8a36e22 2021-08-26 stsp
7855 f8a36e22 2021-08-26 stsp TAILQ_INIT(&branches);
7856 f8a36e22 2021-08-26 stsp TAILQ_INIT(&tags);
7857 f8a36e22 2021-08-26 stsp TAILQ_INIT(&all_branches);
7858 f8a36e22 2021-08-26 stsp TAILQ_INIT(&all_tags);
7859 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_args);
7860 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_branches);
7861 f8a36e22 2021-08-26 stsp
7862 f8a36e22 2021-08-26 stsp while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
7863 f8a36e22 2021-08-26 stsp switch (ch) {
7864 f8a36e22 2021-08-26 stsp case 'a':
7865 f8a36e22 2021-08-26 stsp send_all_branches = 1;
7866 f8a36e22 2021-08-26 stsp break;
7867 f8a36e22 2021-08-26 stsp case 'b':
7868 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches, optarg, NULL);
7869 f8a36e22 2021-08-26 stsp if (error)
7870 f8a36e22 2021-08-26 stsp return error;
7871 f8a36e22 2021-08-26 stsp nbranches++;
7872 f8a36e22 2021-08-26 stsp break;
7873 f8a36e22 2021-08-26 stsp case 'd':
7874 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&delete_args, optarg, NULL);
7875 f8a36e22 2021-08-26 stsp if (error)
7876 f8a36e22 2021-08-26 stsp return error;
7877 f8a36e22 2021-08-26 stsp break;
7878 f8a36e22 2021-08-26 stsp case 'f':
7879 f8a36e22 2021-08-26 stsp overwrite_refs = 1;
7880 f8a36e22 2021-08-26 stsp break;
7881 f8a36e22 2021-08-26 stsp case 'r':
7882 f8a36e22 2021-08-26 stsp repo_path = realpath(optarg, NULL);
7883 f8a36e22 2021-08-26 stsp if (repo_path == NULL)
7884 f8a36e22 2021-08-26 stsp return got_error_from_errno2("realpath",
7885 f8a36e22 2021-08-26 stsp optarg);
7886 f8a36e22 2021-08-26 stsp got_path_strip_trailing_slashes(repo_path);
7887 f8a36e22 2021-08-26 stsp break;
7888 f8a36e22 2021-08-26 stsp case 't':
7889 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&tags, optarg, NULL);
7890 f8a36e22 2021-08-26 stsp if (error)
7891 f8a36e22 2021-08-26 stsp return error;
7892 f8a36e22 2021-08-26 stsp ntags++;
7893 f8a36e22 2021-08-26 stsp break;
7894 f8a36e22 2021-08-26 stsp case 'T':
7895 f8a36e22 2021-08-26 stsp send_all_tags = 1;
7896 f8a36e22 2021-08-26 stsp break;
7897 f8a36e22 2021-08-26 stsp case 'v':
7898 f8a36e22 2021-08-26 stsp if (verbosity < 0)
7899 f8a36e22 2021-08-26 stsp verbosity = 0;
7900 f8a36e22 2021-08-26 stsp else if (verbosity < 3)
7901 f8a36e22 2021-08-26 stsp verbosity++;
7902 f8a36e22 2021-08-26 stsp break;
7903 f8a36e22 2021-08-26 stsp case 'q':
7904 f8a36e22 2021-08-26 stsp verbosity = -1;
7905 f8a36e22 2021-08-26 stsp break;
7906 f8a36e22 2021-08-26 stsp default:
7907 f8a36e22 2021-08-26 stsp usage_send();
7908 f8a36e22 2021-08-26 stsp /* NOTREACHED */
7909 f8a36e22 2021-08-26 stsp }
7910 f8a36e22 2021-08-26 stsp }
7911 f8a36e22 2021-08-26 stsp argc -= optind;
7912 f8a36e22 2021-08-26 stsp argv += optind;
7913 f8a36e22 2021-08-26 stsp
7914 f8a36e22 2021-08-26 stsp if (send_all_branches && !TAILQ_EMPTY(&branches))
7915 f8a36e22 2021-08-26 stsp option_conflict('a', 'b');
7916 f8a36e22 2021-08-26 stsp if (send_all_tags && !TAILQ_EMPTY(&tags))
7917 f8a36e22 2021-08-26 stsp option_conflict('T', 't');
7918 f8a36e22 2021-08-26 stsp
7919 f8a36e22 2021-08-26 stsp
7920 f8a36e22 2021-08-26 stsp if (argc == 0)
7921 f8a36e22 2021-08-26 stsp remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
7922 f8a36e22 2021-08-26 stsp else if (argc == 1)
7923 f8a36e22 2021-08-26 stsp remote_name = argv[0];
7924 f8a36e22 2021-08-26 stsp else
7925 f8a36e22 2021-08-26 stsp usage_send();
7926 f8a36e22 2021-08-26 stsp
7927 f8a36e22 2021-08-26 stsp cwd = getcwd(NULL, 0);
7928 f8a36e22 2021-08-26 stsp if (cwd == NULL) {
7929 f8a36e22 2021-08-26 stsp error = got_error_from_errno("getcwd");
7930 f8a36e22 2021-08-26 stsp goto done;
7931 f8a36e22 2021-08-26 stsp }
7932 f8a36e22 2021-08-26 stsp
7933 f8a36e22 2021-08-26 stsp if (repo_path == NULL) {
7934 f8a36e22 2021-08-26 stsp error = got_worktree_open(&worktree, cwd);
7935 f8a36e22 2021-08-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7936 f8a36e22 2021-08-26 stsp goto done;
7937 f8a36e22 2021-08-26 stsp else
7938 f8a36e22 2021-08-26 stsp error = NULL;
7939 f8a36e22 2021-08-26 stsp if (worktree) {
7940 f8a36e22 2021-08-26 stsp repo_path =
7941 f8a36e22 2021-08-26 stsp strdup(got_worktree_get_repo_path(worktree));
7942 f8a36e22 2021-08-26 stsp if (repo_path == NULL)
7943 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
7944 f8a36e22 2021-08-26 stsp if (error)
7945 f8a36e22 2021-08-26 stsp goto done;
7946 f8a36e22 2021-08-26 stsp } else {
7947 f8a36e22 2021-08-26 stsp repo_path = strdup(cwd);
7948 f8a36e22 2021-08-26 stsp if (repo_path == NULL) {
7949 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
7950 f8a36e22 2021-08-26 stsp goto done;
7951 f8a36e22 2021-08-26 stsp }
7952 f8a36e22 2021-08-26 stsp }
7953 f8a36e22 2021-08-26 stsp }
7954 f8a36e22 2021-08-26 stsp
7955 f8a36e22 2021-08-26 stsp error = got_repo_open(&repo, repo_path, NULL);
7956 f8a36e22 2021-08-26 stsp if (error)
7957 f8a36e22 2021-08-26 stsp goto done;
7958 f8a36e22 2021-08-26 stsp
7959 f8a36e22 2021-08-26 stsp if (worktree) {
7960 f8a36e22 2021-08-26 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
7961 f8a36e22 2021-08-26 stsp if (worktree_conf) {
7962 f8a36e22 2021-08-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
7963 f8a36e22 2021-08-26 stsp worktree_conf);
7964 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
7965 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
7966 f8a36e22 2021-08-26 stsp remote = &remotes[i];
7967 f8a36e22 2021-08-26 stsp break;
7968 f8a36e22 2021-08-26 stsp }
7969 f8a36e22 2021-08-26 stsp }
7970 f8a36e22 2021-08-26 stsp }
7971 f8a36e22 2021-08-26 stsp }
7972 f8a36e22 2021-08-26 stsp if (remote == NULL) {
7973 f8a36e22 2021-08-26 stsp repo_conf = got_repo_get_gotconfig(repo);
7974 f8a36e22 2021-08-26 stsp if (repo_conf) {
7975 f8a36e22 2021-08-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
7976 f8a36e22 2021-08-26 stsp repo_conf);
7977 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
7978 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
7979 f8a36e22 2021-08-26 stsp remote = &remotes[i];
7980 f8a36e22 2021-08-26 stsp break;
7981 f8a36e22 2021-08-26 stsp }
7982 f8a36e22 2021-08-26 stsp }
7983 f8a36e22 2021-08-26 stsp }
7984 f8a36e22 2021-08-26 stsp }
7985 f8a36e22 2021-08-26 stsp if (remote == NULL) {
7986 f8a36e22 2021-08-26 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
7987 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
7988 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
7989 f8a36e22 2021-08-26 stsp remote = &remotes[i];
7990 f8a36e22 2021-08-26 stsp break;
7991 f8a36e22 2021-08-26 stsp }
7992 f8a36e22 2021-08-26 stsp }
7993 f8a36e22 2021-08-26 stsp }
7994 f8a36e22 2021-08-26 stsp if (remote == NULL) {
7995 f8a36e22 2021-08-26 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
7996 f8a36e22 2021-08-26 stsp goto done;
7997 f8a36e22 2021-08-26 stsp }
7998 f8a36e22 2021-08-26 stsp
7999 5e5da8c4 2021-09-05 stsp error = got_dial_parse_uri(&proto, &host, &port, &server_path,
8000 6480c871 2021-08-30 stsp &repo_name, remote->send_url);
8001 f8a36e22 2021-08-26 stsp if (error)
8002 f8a36e22 2021-08-26 stsp goto done;
8003 f8a36e22 2021-08-26 stsp
8004 f8a36e22 2021-08-26 stsp if (strcmp(proto, "git") == 0) {
8005 f8a36e22 2021-08-26 stsp #ifndef PROFILE
8006 f8a36e22 2021-08-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8007 f8a36e22 2021-08-26 stsp "sendfd dns inet unveil", NULL) == -1)
8008 f8a36e22 2021-08-26 stsp err(1, "pledge");
8009 f8a36e22 2021-08-26 stsp #endif
8010 f8a36e22 2021-08-26 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
8011 f8a36e22 2021-08-26 stsp strcmp(proto, "ssh") == 0) {
8012 f8a36e22 2021-08-26 stsp #ifndef PROFILE
8013 f8a36e22 2021-08-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8014 f8a36e22 2021-08-26 stsp "sendfd unveil", NULL) == -1)
8015 f8a36e22 2021-08-26 stsp err(1, "pledge");
8016 f8a36e22 2021-08-26 stsp #endif
8017 f8a36e22 2021-08-26 stsp } else if (strcmp(proto, "http") == 0 ||
8018 f8a36e22 2021-08-26 stsp strcmp(proto, "git+http") == 0) {
8019 f8a36e22 2021-08-26 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
8020 f8a36e22 2021-08-26 stsp goto done;
8021 f8a36e22 2021-08-26 stsp } else {
8022 f8a36e22 2021-08-26 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
8023 f8a36e22 2021-08-26 stsp goto done;
8024 f8a36e22 2021-08-26 stsp }
8025 f8a36e22 2021-08-26 stsp
8026 d65a88a2 2021-09-05 stsp error = got_dial_apply_unveil(proto);
8027 d65a88a2 2021-09-05 stsp if (error)
8028 d65a88a2 2021-09-05 stsp goto done;
8029 d65a88a2 2021-09-05 stsp
8030 f8a36e22 2021-08-26 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
8031 f8a36e22 2021-08-26 stsp if (error)
8032 f8a36e22 2021-08-26 stsp goto done;
8033 f8a36e22 2021-08-26 stsp
8034 f8a36e22 2021-08-26 stsp if (send_all_branches) {
8035 f8a36e22 2021-08-26 stsp error = got_ref_list(&all_branches, repo, "refs/heads",
8036 f8a36e22 2021-08-26 stsp got_ref_cmp_by_name, NULL);
8037 f8a36e22 2021-08-26 stsp if (error)
8038 f8a36e22 2021-08-26 stsp goto done;
8039 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &all_branches, entry) {
8040 f8a36e22 2021-08-26 stsp const char *branchname = got_ref_get_name(re->ref);
8041 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches,
8042 f8a36e22 2021-08-26 stsp branchname, NULL);
8043 f8a36e22 2021-08-26 stsp if (error)
8044 f8a36e22 2021-08-26 stsp goto done;
8045 f8a36e22 2021-08-26 stsp nbranches++;
8046 f8a36e22 2021-08-26 stsp }
8047 eac1df47 2021-09-01 stsp } else if (nbranches == 0) {
8048 eac1df47 2021-09-01 stsp for (i = 0; i < remote->nsend_branches; i++) {
8049 eac1df47 2021-09-01 stsp got_pathlist_append(&branches,
8050 eac1df47 2021-09-01 stsp remote->send_branches[i], NULL);
8051 eac1df47 2021-09-01 stsp }
8052 f8a36e22 2021-08-26 stsp }
8053 f8a36e22 2021-08-26 stsp
8054 f8a36e22 2021-08-26 stsp if (send_all_tags) {
8055 f8a36e22 2021-08-26 stsp error = got_ref_list(&all_tags, repo, "refs/tags",
8056 f8a36e22 2021-08-26 stsp got_ref_cmp_by_name, NULL);
8057 f8a36e22 2021-08-26 stsp if (error)
8058 f8a36e22 2021-08-26 stsp goto done;
8059 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &all_tags, entry) {
8060 f8a36e22 2021-08-26 stsp const char *tagname = got_ref_get_name(re->ref);
8061 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&tags,
8062 f8a36e22 2021-08-26 stsp tagname, NULL);
8063 f8a36e22 2021-08-26 stsp if (error)
8064 f8a36e22 2021-08-26 stsp goto done;
8065 f8a36e22 2021-08-26 stsp ntags++;
8066 f8a36e22 2021-08-26 stsp }
8067 f8a36e22 2021-08-26 stsp }
8068 f8a36e22 2021-08-26 stsp
8069 f8a36e22 2021-08-26 stsp /*
8070 f8a36e22 2021-08-26 stsp * To prevent accidents only branches in refs/heads/ can be deleted
8071 f8a36e22 2021-08-26 stsp * with 'got send -d'.
8072 f8a36e22 2021-08-26 stsp * Deleting anything else requires local repository access or Git.
8073 f8a36e22 2021-08-26 stsp */
8074 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_args, entry) {
8075 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
8076 f8a36e22 2021-08-26 stsp char *s;
8077 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *new;
8078 f8a36e22 2021-08-26 stsp if (strncmp(branchname, "refs/heads/", 11) == 0) {
8079 f8a36e22 2021-08-26 stsp s = strdup(branchname);
8080 f8a36e22 2021-08-26 stsp if (s == NULL) {
8081 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
8082 f8a36e22 2021-08-26 stsp goto done;
8083 f8a36e22 2021-08-26 stsp }
8084 f8a36e22 2021-08-26 stsp } else {
8085 f8a36e22 2021-08-26 stsp if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
8086 f8a36e22 2021-08-26 stsp error = got_error_from_errno("asprintf");
8087 f8a36e22 2021-08-26 stsp goto done;
8088 f8a36e22 2021-08-26 stsp }
8089 f8a36e22 2021-08-26 stsp }
8090 f8a36e22 2021-08-26 stsp error = got_pathlist_insert(&new, &delete_branches, s, NULL);
8091 f8a36e22 2021-08-26 stsp if (error || new == NULL /* duplicate */)
8092 f8a36e22 2021-08-26 stsp free(s);
8093 f8a36e22 2021-08-26 stsp if (error)
8094 f8a36e22 2021-08-26 stsp goto done;
8095 f8a36e22 2021-08-26 stsp ndelete_branches++;
8096 f8a36e22 2021-08-26 stsp }
8097 f8a36e22 2021-08-26 stsp
8098 f8a36e22 2021-08-26 stsp if (nbranches == 0 && ndelete_branches == 0) {
8099 f8a36e22 2021-08-26 stsp struct got_reference *head_ref;
8100 f8a36e22 2021-08-26 stsp if (worktree)
8101 f8a36e22 2021-08-26 stsp error = got_ref_open(&head_ref, repo,
8102 f8a36e22 2021-08-26 stsp got_worktree_get_head_ref_name(worktree), 0);
8103 f8a36e22 2021-08-26 stsp else
8104 f8a36e22 2021-08-26 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
8105 f8a36e22 2021-08-26 stsp if (error)
8106 f8a36e22 2021-08-26 stsp goto done;
8107 f8a36e22 2021-08-26 stsp if (got_ref_is_symbolic(head_ref)) {
8108 f8a36e22 2021-08-26 stsp error = got_ref_resolve_symbolic(&ref, repo, head_ref);
8109 f8a36e22 2021-08-26 stsp got_ref_close(head_ref);
8110 f8a36e22 2021-08-26 stsp if (error)
8111 f8a36e22 2021-08-26 stsp goto done;
8112 f8a36e22 2021-08-26 stsp } else
8113 f8a36e22 2021-08-26 stsp ref = head_ref;
8114 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches, got_ref_get_name(ref),
8115 abc59930 2021-09-05 naddy NULL);
8116 f8a36e22 2021-08-26 stsp if (error)
8117 f8a36e22 2021-08-26 stsp goto done;
8118 f8a36e22 2021-08-26 stsp nbranches++;
8119 f8a36e22 2021-08-26 stsp }
8120 f8a36e22 2021-08-26 stsp
8121 f8a36e22 2021-08-26 stsp if (verbosity >= 0)
8122 f8a36e22 2021-08-26 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
8123 f8a36e22 2021-08-26 stsp port ? ":" : "", port ? port : "");
8124 f8a36e22 2021-08-26 stsp
8125 f8a36e22 2021-08-26 stsp error = got_send_connect(&sendpid, &sendfd, proto, host, port,
8126 f8a36e22 2021-08-26 stsp server_path, verbosity);
8127 f8a36e22 2021-08-26 stsp if (error)
8128 f8a36e22 2021-08-26 stsp goto done;
8129 f8a36e22 2021-08-26 stsp
8130 f8a36e22 2021-08-26 stsp memset(&spa, 0, sizeof(spa));
8131 f8a36e22 2021-08-26 stsp spa.last_scaled_packsize[0] = '\0';
8132 f8a36e22 2021-08-26 stsp spa.last_p_deltify = -1;
8133 f8a36e22 2021-08-26 stsp spa.last_p_written = -1;
8134 f8a36e22 2021-08-26 stsp spa.verbosity = verbosity;
8135 f8a36e22 2021-08-26 stsp spa.delete_branches = &delete_branches;
8136 f8a36e22 2021-08-26 stsp error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
8137 f8a36e22 2021-08-26 stsp verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
8138 f8a36e22 2021-08-26 stsp check_cancelled, NULL);
8139 f8a36e22 2021-08-26 stsp if (spa.printed_something)
8140 f8a36e22 2021-08-26 stsp putchar('\n');
8141 f8a36e22 2021-08-26 stsp if (error)
8142 f8a36e22 2021-08-26 stsp goto done;
8143 f8a36e22 2021-08-26 stsp if (!spa.sent_something && verbosity >= 0)
8144 f8a36e22 2021-08-26 stsp printf("Already up-to-date\n");
8145 f8a36e22 2021-08-26 stsp done:
8146 f8a36e22 2021-08-26 stsp if (sendpid > 0) {
8147 f8a36e22 2021-08-26 stsp if (kill(sendpid, SIGTERM) == -1)
8148 f8a36e22 2021-08-26 stsp error = got_error_from_errno("kill");
8149 f8a36e22 2021-08-26 stsp if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
8150 f8a36e22 2021-08-26 stsp error = got_error_from_errno("waitpid");
8151 f8a36e22 2021-08-26 stsp }
8152 f8a36e22 2021-08-26 stsp if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
8153 f8a36e22 2021-08-26 stsp error = got_error_from_errno("close");
8154 f8a36e22 2021-08-26 stsp if (repo) {
8155 f8a36e22 2021-08-26 stsp const struct got_error *close_err = got_repo_close(repo);
8156 f8a36e22 2021-08-26 stsp if (error == NULL)
8157 f8a36e22 2021-08-26 stsp error = close_err;
8158 f8a36e22 2021-08-26 stsp }
8159 f8a36e22 2021-08-26 stsp if (worktree)
8160 f8a36e22 2021-08-26 stsp got_worktree_close(worktree);
8161 f8a36e22 2021-08-26 stsp if (ref)
8162 f8a36e22 2021-08-26 stsp got_ref_close(ref);
8163 f8a36e22 2021-08-26 stsp got_pathlist_free(&branches);
8164 f8a36e22 2021-08-26 stsp got_pathlist_free(&tags);
8165 f8a36e22 2021-08-26 stsp got_ref_list_free(&all_branches);
8166 f8a36e22 2021-08-26 stsp got_ref_list_free(&all_tags);
8167 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_args);
8168 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_branches, entry)
8169 f8a36e22 2021-08-26 stsp free((char *)pe->path);
8170 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_branches);
8171 f8a36e22 2021-08-26 stsp free(cwd);
8172 f8a36e22 2021-08-26 stsp free(repo_path);
8173 f8a36e22 2021-08-26 stsp free(proto);
8174 f8a36e22 2021-08-26 stsp free(host);
8175 f8a36e22 2021-08-26 stsp free(port);
8176 f8a36e22 2021-08-26 stsp free(server_path);
8177 f8a36e22 2021-08-26 stsp free(repo_name);
8178 234035bc 2019-06-01 stsp return error;
8179 234035bc 2019-06-01 stsp }
8180 234035bc 2019-06-01 stsp
8181 234035bc 2019-06-01 stsp __dead static void
8182 234035bc 2019-06-01 stsp usage_cherrypick(void)
8183 234035bc 2019-06-01 stsp {
8184 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
8185 234035bc 2019-06-01 stsp exit(1);
8186 234035bc 2019-06-01 stsp }
8187 234035bc 2019-06-01 stsp
8188 234035bc 2019-06-01 stsp static const struct got_error *
8189 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
8190 234035bc 2019-06-01 stsp {
8191 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
8192 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
8193 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
8194 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
8195 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
8196 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
8197 234035bc 2019-06-01 stsp struct got_object_qid *pid;
8198 9627c110 2020-04-18 stsp int ch;
8199 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8200 234035bc 2019-06-01 stsp
8201 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8202 234035bc 2019-06-01 stsp switch (ch) {
8203 234035bc 2019-06-01 stsp default:
8204 234035bc 2019-06-01 stsp usage_cherrypick();
8205 234035bc 2019-06-01 stsp /* NOTREACHED */
8206 234035bc 2019-06-01 stsp }
8207 234035bc 2019-06-01 stsp }
8208 234035bc 2019-06-01 stsp
8209 234035bc 2019-06-01 stsp argc -= optind;
8210 234035bc 2019-06-01 stsp argv += optind;
8211 234035bc 2019-06-01 stsp
8212 43012d58 2019-07-14 stsp #ifndef PROFILE
8213 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8214 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8215 43012d58 2019-07-14 stsp err(1, "pledge");
8216 43012d58 2019-07-14 stsp #endif
8217 234035bc 2019-06-01 stsp if (argc != 1)
8218 234035bc 2019-06-01 stsp usage_cherrypick();
8219 234035bc 2019-06-01 stsp
8220 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
8221 234035bc 2019-06-01 stsp if (cwd == NULL) {
8222 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
8223 234035bc 2019-06-01 stsp goto done;
8224 234035bc 2019-06-01 stsp }
8225 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
8226 fa51e947 2020-03-27 stsp if (error) {
8227 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8228 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
8229 fa51e947 2020-03-27 stsp cwd);
8230 234035bc 2019-06-01 stsp goto done;
8231 fa51e947 2020-03-27 stsp }
8232 234035bc 2019-06-01 stsp
8233 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8234 c9956ddf 2019-09-08 stsp NULL);
8235 234035bc 2019-06-01 stsp if (error != NULL)
8236 234035bc 2019-06-01 stsp goto done;
8237 234035bc 2019-06-01 stsp
8238 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8239 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
8240 234035bc 2019-06-01 stsp if (error)
8241 234035bc 2019-06-01 stsp goto done;
8242 234035bc 2019-06-01 stsp
8243 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8244 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
8245 234035bc 2019-06-01 stsp if (error != NULL) {
8246 234035bc 2019-06-01 stsp struct got_reference *ref;
8247 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8248 234035bc 2019-06-01 stsp goto done;
8249 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
8250 234035bc 2019-06-01 stsp if (error != NULL)
8251 234035bc 2019-06-01 stsp goto done;
8252 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
8253 234035bc 2019-06-01 stsp got_ref_close(ref);
8254 234035bc 2019-06-01 stsp if (error != NULL)
8255 234035bc 2019-06-01 stsp goto done;
8256 234035bc 2019-06-01 stsp }
8257 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
8258 234035bc 2019-06-01 stsp if (error)
8259 234035bc 2019-06-01 stsp goto done;
8260 234035bc 2019-06-01 stsp
8261 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8262 234035bc 2019-06-01 stsp if (error)
8263 234035bc 2019-06-01 stsp goto done;
8264 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8265 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8266 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
8267 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
8268 03415a1a 2019-06-02 stsp NULL);
8269 234035bc 2019-06-01 stsp if (error != NULL)
8270 234035bc 2019-06-01 stsp goto done;
8271 234035bc 2019-06-01 stsp
8272 9627c110 2020-04-18 stsp if (upa.did_something)
8273 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
8274 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
8275 234035bc 2019-06-01 stsp done:
8276 234035bc 2019-06-01 stsp if (commit)
8277 234035bc 2019-06-01 stsp got_object_commit_close(commit);
8278 234035bc 2019-06-01 stsp free(commit_id_str);
8279 234035bc 2019-06-01 stsp if (worktree)
8280 234035bc 2019-06-01 stsp got_worktree_close(worktree);
8281 1d0f4054 2021-06-17 stsp if (repo) {
8282 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8283 1d0f4054 2021-06-17 stsp if (error == NULL)
8284 1d0f4054 2021-06-17 stsp error = close_err;
8285 1d0f4054 2021-06-17 stsp }
8286 c4296144 2019-05-09 stsp return error;
8287 c4296144 2019-05-09 stsp }
8288 5ef14e63 2019-06-02 stsp
8289 5ef14e63 2019-06-02 stsp __dead static void
8290 5ef14e63 2019-06-02 stsp usage_backout(void)
8291 5ef14e63 2019-06-02 stsp {
8292 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
8293 5ef14e63 2019-06-02 stsp exit(1);
8294 5ef14e63 2019-06-02 stsp }
8295 5ef14e63 2019-06-02 stsp
8296 5ef14e63 2019-06-02 stsp static const struct got_error *
8297 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
8298 5ef14e63 2019-06-02 stsp {
8299 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
8300 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
8301 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
8302 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
8303 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
8304 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
8305 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
8306 9627c110 2020-04-18 stsp int ch;
8307 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8308 5ef14e63 2019-06-02 stsp
8309 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8310 5ef14e63 2019-06-02 stsp switch (ch) {
8311 5ef14e63 2019-06-02 stsp default:
8312 5ef14e63 2019-06-02 stsp usage_backout();
8313 5ef14e63 2019-06-02 stsp /* NOTREACHED */
8314 5ef14e63 2019-06-02 stsp }
8315 5ef14e63 2019-06-02 stsp }
8316 5ef14e63 2019-06-02 stsp
8317 5ef14e63 2019-06-02 stsp argc -= optind;
8318 5ef14e63 2019-06-02 stsp argv += optind;
8319 5ef14e63 2019-06-02 stsp
8320 43012d58 2019-07-14 stsp #ifndef PROFILE
8321 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8322 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8323 43012d58 2019-07-14 stsp err(1, "pledge");
8324 43012d58 2019-07-14 stsp #endif
8325 5ef14e63 2019-06-02 stsp if (argc != 1)
8326 5ef14e63 2019-06-02 stsp usage_backout();
8327 5ef14e63 2019-06-02 stsp
8328 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
8329 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
8330 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
8331 5ef14e63 2019-06-02 stsp goto done;
8332 5ef14e63 2019-06-02 stsp }
8333 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
8334 fa51e947 2020-03-27 stsp if (error) {
8335 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8336 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
8337 5ef14e63 2019-06-02 stsp goto done;
8338 fa51e947 2020-03-27 stsp }
8339 5ef14e63 2019-06-02 stsp
8340 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8341 c9956ddf 2019-09-08 stsp NULL);
8342 5ef14e63 2019-06-02 stsp if (error != NULL)
8343 5ef14e63 2019-06-02 stsp goto done;
8344 5ef14e63 2019-06-02 stsp
8345 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8346 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
8347 5ef14e63 2019-06-02 stsp if (error)
8348 5ef14e63 2019-06-02 stsp goto done;
8349 5ef14e63 2019-06-02 stsp
8350 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8351 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
8352 5ef14e63 2019-06-02 stsp if (error != NULL) {
8353 5ef14e63 2019-06-02 stsp struct got_reference *ref;
8354 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8355 5ef14e63 2019-06-02 stsp goto done;
8356 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
8357 5ef14e63 2019-06-02 stsp if (error != NULL)
8358 5ef14e63 2019-06-02 stsp goto done;
8359 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
8360 5ef14e63 2019-06-02 stsp got_ref_close(ref);
8361 5ef14e63 2019-06-02 stsp if (error != NULL)
8362 5ef14e63 2019-06-02 stsp goto done;
8363 5ef14e63 2019-06-02 stsp }
8364 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
8365 5ef14e63 2019-06-02 stsp if (error)
8366 5ef14e63 2019-06-02 stsp goto done;
8367 5ef14e63 2019-06-02 stsp
8368 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8369 5ef14e63 2019-06-02 stsp if (error)
8370 5ef14e63 2019-06-02 stsp goto done;
8371 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8372 5ef14e63 2019-06-02 stsp if (pid == NULL) {
8373 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
8374 5ef14e63 2019-06-02 stsp goto done;
8375 5ef14e63 2019-06-02 stsp }
8376 5ef14e63 2019-06-02 stsp
8377 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8378 10604dce 2021-09-24 thomas error = got_worktree_merge_files(worktree, commit_id, pid->id,
8379 10604dce 2021-09-24 thomas repo, update_progress, &upa, check_cancelled, NULL);
8380 5ef14e63 2019-06-02 stsp if (error != NULL)
8381 5ef14e63 2019-06-02 stsp goto done;
8382 5ef14e63 2019-06-02 stsp
8383 9627c110 2020-04-18 stsp if (upa.did_something)
8384 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
8385 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
8386 5ef14e63 2019-06-02 stsp done:
8387 5ef14e63 2019-06-02 stsp if (commit)
8388 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
8389 5ef14e63 2019-06-02 stsp free(commit_id_str);
8390 818c7501 2019-07-11 stsp if (worktree)
8391 818c7501 2019-07-11 stsp got_worktree_close(worktree);
8392 1d0f4054 2021-06-17 stsp if (repo) {
8393 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8394 1d0f4054 2021-06-17 stsp if (error == NULL)
8395 1d0f4054 2021-06-17 stsp error = close_err;
8396 1d0f4054 2021-06-17 stsp }
8397 818c7501 2019-07-11 stsp return error;
8398 818c7501 2019-07-11 stsp }
8399 818c7501 2019-07-11 stsp
8400 818c7501 2019-07-11 stsp __dead static void
8401 818c7501 2019-07-11 stsp usage_rebase(void)
8402 818c7501 2019-07-11 stsp {
8403 643b85bc 2021-07-16 stsp fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
8404 af54c8f8 2019-07-11 stsp getprogname());
8405 818c7501 2019-07-11 stsp exit(1);
8406 0ebf8283 2019-07-24 stsp }
8407 0ebf8283 2019-07-24 stsp
8408 0ebf8283 2019-07-24 stsp void
8409 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
8410 0ebf8283 2019-07-24 stsp {
8411 0ebf8283 2019-07-24 stsp char *nl;
8412 0ebf8283 2019-07-24 stsp size_t len;
8413 0ebf8283 2019-07-24 stsp
8414 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
8415 0ebf8283 2019-07-24 stsp if (len > limit)
8416 0ebf8283 2019-07-24 stsp len = limit;
8417 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
8418 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
8419 0ebf8283 2019-07-24 stsp if (nl)
8420 0ebf8283 2019-07-24 stsp *nl = '\0';
8421 0ebf8283 2019-07-24 stsp }
8422 0ebf8283 2019-07-24 stsp
8423 0ebf8283 2019-07-24 stsp static const struct got_error *
8424 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
8425 0ebf8283 2019-07-24 stsp {
8426 5943eee2 2019-08-13 stsp const struct got_error *err;
8427 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
8428 5943eee2 2019-08-13 stsp const char *s;
8429 0ebf8283 2019-07-24 stsp
8430 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
8431 5943eee2 2019-08-13 stsp if (err)
8432 5943eee2 2019-08-13 stsp return err;
8433 0ebf8283 2019-07-24 stsp
8434 5943eee2 2019-08-13 stsp s = logmsg0;
8435 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
8436 5943eee2 2019-08-13 stsp s++;
8437 5943eee2 2019-08-13 stsp
8438 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
8439 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
8440 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
8441 5943eee2 2019-08-13 stsp goto done;
8442 5943eee2 2019-08-13 stsp }
8443 0ebf8283 2019-07-24 stsp
8444 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
8445 5943eee2 2019-08-13 stsp done:
8446 5943eee2 2019-08-13 stsp free(logmsg0);
8447 a0ea4fc0 2020-02-28 stsp return err;
8448 a0ea4fc0 2020-02-28 stsp }
8449 a0ea4fc0 2020-02-28 stsp
8450 a0ea4fc0 2020-02-28 stsp static const struct got_error *
8451 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
8452 a0ea4fc0 2020-02-28 stsp {
8453 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
8454 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
8455 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
8456 a0ea4fc0 2020-02-28 stsp
8457 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
8458 a0ea4fc0 2020-02-28 stsp if (err)
8459 a0ea4fc0 2020-02-28 stsp return err;
8460 a0ea4fc0 2020-02-28 stsp
8461 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
8462 a0ea4fc0 2020-02-28 stsp if (err)
8463 a0ea4fc0 2020-02-28 stsp goto done;
8464 a0ea4fc0 2020-02-28 stsp
8465 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
8466 a0ea4fc0 2020-02-28 stsp
8467 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
8468 a0ea4fc0 2020-02-28 stsp if (err)
8469 a0ea4fc0 2020-02-28 stsp goto done;
8470 a0ea4fc0 2020-02-28 stsp
8471 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
8472 a0ea4fc0 2020-02-28 stsp done:
8473 a0ea4fc0 2020-02-28 stsp free(id_str);
8474 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
8475 a0ea4fc0 2020-02-28 stsp free(logmsg);
8476 5943eee2 2019-08-13 stsp return err;
8477 818c7501 2019-07-11 stsp }
8478 818c7501 2019-07-11 stsp
8479 818c7501 2019-07-11 stsp static const struct got_error *
8480 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
8481 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
8482 818c7501 2019-07-11 stsp {
8483 818c7501 2019-07-11 stsp const struct got_error *err;
8484 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8485 818c7501 2019-07-11 stsp
8486 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
8487 818c7501 2019-07-11 stsp if (err)
8488 818c7501 2019-07-11 stsp goto done;
8489 818c7501 2019-07-11 stsp
8490 ff0d2220 2019-07-11 stsp if (new_id) {
8491 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
8492 ff0d2220 2019-07-11 stsp if (err)
8493 ff0d2220 2019-07-11 stsp goto done;
8494 ff0d2220 2019-07-11 stsp }
8495 818c7501 2019-07-11 stsp
8496 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
8497 ff0d2220 2019-07-11 stsp if (new_id_str)
8498 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
8499 0ebf8283 2019-07-24 stsp
8500 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8501 0ebf8283 2019-07-24 stsp if (err)
8502 0ebf8283 2019-07-24 stsp goto done;
8503 0ebf8283 2019-07-24 stsp
8504 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
8505 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8506 818c7501 2019-07-11 stsp done:
8507 818c7501 2019-07-11 stsp free(old_id_str);
8508 818c7501 2019-07-11 stsp free(new_id_str);
8509 272a1371 2020-02-28 stsp free(logmsg);
8510 818c7501 2019-07-11 stsp return err;
8511 818c7501 2019-07-11 stsp }
8512 818c7501 2019-07-11 stsp
8513 1ee397ad 2019-07-12 stsp static const struct got_error *
8514 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
8515 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
8516 e600f124 2021-03-21 stsp struct got_reference *tmp_branch, struct got_repository *repo,
8517 e600f124 2021-03-21 stsp int create_backup)
8518 818c7501 2019-07-11 stsp {
8519 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
8520 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
8521 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, branch, repo, create_backup);
8522 818c7501 2019-07-11 stsp }
8523 818c7501 2019-07-11 stsp
8524 818c7501 2019-07-11 stsp static const struct got_error *
8525 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
8526 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8527 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
8528 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8529 ff0d2220 2019-07-11 stsp {
8530 ff0d2220 2019-07-11 stsp const struct got_error *error;
8531 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
8532 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
8533 ff0d2220 2019-07-11 stsp
8534 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8535 ff0d2220 2019-07-11 stsp if (error)
8536 ff0d2220 2019-07-11 stsp return error;
8537 ff0d2220 2019-07-11 stsp
8538 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
8539 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
8540 ff0d2220 2019-07-11 stsp if (error) {
8541 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
8542 ff0d2220 2019-07-11 stsp goto done;
8543 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
8544 ff0d2220 2019-07-11 stsp } else {
8545 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
8546 ff0d2220 2019-07-11 stsp free(new_commit_id);
8547 ff0d2220 2019-07-11 stsp }
8548 ff0d2220 2019-07-11 stsp done:
8549 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
8550 ff0d2220 2019-07-11 stsp return error;
8551 64c6d990 2019-07-11 stsp }
8552 64c6d990 2019-07-11 stsp
8553 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
8554 64c6d990 2019-07-11 stsp const char *path_prefix;
8555 64c6d990 2019-07-11 stsp size_t len;
8556 8ca9bd68 2019-07-25 stsp int errcode;
8557 64c6d990 2019-07-11 stsp };
8558 64c6d990 2019-07-11 stsp
8559 64c6d990 2019-07-11 stsp static const struct got_error *
8560 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
8561 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
8562 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
8563 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
8564 64c6d990 2019-07-11 stsp {
8565 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
8566 64c6d990 2019-07-11 stsp
8567 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
8568 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
8569 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
8570 64c6d990 2019-07-11 stsp
8571 64c6d990 2019-07-11 stsp return NULL;
8572 64c6d990 2019-07-11 stsp }
8573 64c6d990 2019-07-11 stsp
8574 64c6d990 2019-07-11 stsp static const struct got_error *
8575 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
8576 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
8577 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
8578 64c6d990 2019-07-11 stsp {
8579 64c6d990 2019-07-11 stsp const struct got_error *err;
8580 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
8581 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
8582 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
8583 64c6d990 2019-07-11 stsp
8584 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
8585 64c6d990 2019-07-11 stsp return NULL;
8586 64c6d990 2019-07-11 stsp
8587 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8588 64c6d990 2019-07-11 stsp if (err)
8589 64c6d990 2019-07-11 stsp goto done;
8590 64c6d990 2019-07-11 stsp
8591 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
8592 64c6d990 2019-07-11 stsp if (err)
8593 64c6d990 2019-07-11 stsp goto done;
8594 64c6d990 2019-07-11 stsp
8595 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
8596 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
8597 64c6d990 2019-07-11 stsp if (err)
8598 64c6d990 2019-07-11 stsp goto done;
8599 64c6d990 2019-07-11 stsp
8600 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
8601 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
8602 64c6d990 2019-07-11 stsp if (err)
8603 64c6d990 2019-07-11 stsp goto done;
8604 64c6d990 2019-07-11 stsp
8605 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
8606 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
8607 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
8608 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
8609 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
8610 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
8611 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
8612 64c6d990 2019-07-11 stsp done:
8613 64c6d990 2019-07-11 stsp if (tree1)
8614 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
8615 64c6d990 2019-07-11 stsp if (tree2)
8616 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
8617 64c6d990 2019-07-11 stsp if (commit)
8618 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
8619 64c6d990 2019-07-11 stsp if (parent_commit)
8620 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
8621 64c6d990 2019-07-11 stsp return err;
8622 ff0d2220 2019-07-11 stsp }
8623 ff0d2220 2019-07-11 stsp
8624 ff0d2220 2019-07-11 stsp static const struct got_error *
8625 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
8626 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
8627 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
8628 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
8629 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
8630 af61c510 2019-07-19 stsp {
8631 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
8632 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
8633 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
8634 af61c510 2019-07-19 stsp struct got_object_qid *qid;
8635 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
8636 af61c510 2019-07-19 stsp
8637 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
8638 af61c510 2019-07-19 stsp if (err)
8639 af61c510 2019-07-19 stsp return err;
8640 af61c510 2019-07-19 stsp
8641 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
8642 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
8643 af61c510 2019-07-19 stsp if (err)
8644 af61c510 2019-07-19 stsp goto done;
8645 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
8646 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
8647 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
8648 af61c510 2019-07-19 stsp if (err) {
8649 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
8650 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
8651 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
8652 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
8653 af61c510 2019-07-19 stsp "been reached?!?");
8654 ee780d5c 2020-01-04 stsp }
8655 ee780d5c 2020-01-04 stsp goto done;
8656 af61c510 2019-07-19 stsp } else {
8657 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
8658 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
8659 af61c510 2019-07-19 stsp if (err)
8660 af61c510 2019-07-19 stsp goto done;
8661 af61c510 2019-07-19 stsp
8662 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
8663 af61c510 2019-07-19 stsp if (err)
8664 af61c510 2019-07-19 stsp goto done;
8665 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(commits, qid, entry);
8666 af61c510 2019-07-19 stsp commit_id = parent_id;
8667 af61c510 2019-07-19 stsp }
8668 af61c510 2019-07-19 stsp }
8669 af61c510 2019-07-19 stsp done:
8670 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
8671 af61c510 2019-07-19 stsp return err;
8672 e600f124 2021-03-21 stsp }
8673 e600f124 2021-03-21 stsp
8674 e600f124 2021-03-21 stsp static const struct got_error *
8675 e600f124 2021-03-21 stsp get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
8676 e600f124 2021-03-21 stsp {
8677 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
8678 e600f124 2021-03-21 stsp time_t committer_time;
8679 e600f124 2021-03-21 stsp struct tm tm;
8680 e600f124 2021-03-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
8681 e600f124 2021-03-21 stsp char *author0 = NULL, *author, *smallerthan;
8682 e600f124 2021-03-21 stsp char *logmsg0 = NULL, *logmsg, *newline;
8683 e600f124 2021-03-21 stsp
8684 e600f124 2021-03-21 stsp committer_time = got_object_commit_get_committer_time(commit);
8685 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
8686 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
8687 e3199de8 2021-03-21 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
8688 e600f124 2021-03-21 stsp return got_error(GOT_ERR_NO_SPACE);
8689 e600f124 2021-03-21 stsp
8690 e600f124 2021-03-21 stsp author0 = strdup(got_object_commit_get_author(commit));
8691 e600f124 2021-03-21 stsp if (author0 == NULL)
8692 e600f124 2021-03-21 stsp return got_error_from_errno("strdup");
8693 e600f124 2021-03-21 stsp author = author0;
8694 e600f124 2021-03-21 stsp smallerthan = strchr(author, '<');
8695 e600f124 2021-03-21 stsp if (smallerthan && smallerthan[1] != '\0')
8696 e600f124 2021-03-21 stsp author = smallerthan + 1;
8697 e600f124 2021-03-21 stsp author[strcspn(author, "@>")] = '\0';
8698 e600f124 2021-03-21 stsp
8699 e600f124 2021-03-21 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
8700 e600f124 2021-03-21 stsp if (err)
8701 e600f124 2021-03-21 stsp goto done;
8702 e600f124 2021-03-21 stsp logmsg = logmsg0;
8703 e600f124 2021-03-21 stsp while (*logmsg == '\n')
8704 e600f124 2021-03-21 stsp logmsg++;
8705 e600f124 2021-03-21 stsp newline = strchr(logmsg, '\n');
8706 e600f124 2021-03-21 stsp if (newline)
8707 e600f124 2021-03-21 stsp *newline = '\0';
8708 e600f124 2021-03-21 stsp
8709 e600f124 2021-03-21 stsp if (asprintf(brief_str, "%s %s %s",
8710 e600f124 2021-03-21 stsp datebuf, author, logmsg) == -1)
8711 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
8712 e600f124 2021-03-21 stsp done:
8713 e600f124 2021-03-21 stsp free(author0);
8714 e600f124 2021-03-21 stsp free(logmsg0);
8715 643b85bc 2021-07-16 stsp return err;
8716 643b85bc 2021-07-16 stsp }
8717 643b85bc 2021-07-16 stsp
8718 643b85bc 2021-07-16 stsp static const struct got_error *
8719 643b85bc 2021-07-16 stsp delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
8720 643b85bc 2021-07-16 stsp struct got_repository *repo)
8721 643b85bc 2021-07-16 stsp {
8722 643b85bc 2021-07-16 stsp const struct got_error *err;
8723 643b85bc 2021-07-16 stsp char *id_str;
8724 643b85bc 2021-07-16 stsp
8725 643b85bc 2021-07-16 stsp err = got_object_id_str(&id_str, id);
8726 643b85bc 2021-07-16 stsp if (err)
8727 643b85bc 2021-07-16 stsp return err;
8728 643b85bc 2021-07-16 stsp
8729 643b85bc 2021-07-16 stsp err = got_ref_delete(ref, repo);
8730 643b85bc 2021-07-16 stsp if (err)
8731 643b85bc 2021-07-16 stsp goto done;
8732 643b85bc 2021-07-16 stsp
8733 643b85bc 2021-07-16 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
8734 643b85bc 2021-07-16 stsp done:
8735 643b85bc 2021-07-16 stsp free(id_str);
8736 e600f124 2021-03-21 stsp return err;
8737 e600f124 2021-03-21 stsp }
8738 e600f124 2021-03-21 stsp
8739 e600f124 2021-03-21 stsp static const struct got_error *
8740 e600f124 2021-03-21 stsp print_backup_ref(const char *branch_name, const char *new_id_str,
8741 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
8742 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap,
8743 e600f124 2021-03-21 stsp struct got_repository *repo)
8744 e600f124 2021-03-21 stsp {
8745 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
8746 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
8747 e600f124 2021-03-21 stsp char *refs_str = NULL;
8748 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id = NULL;
8749 e600f124 2021-03-21 stsp struct got_commit_object *new_commit = NULL;
8750 e600f124 2021-03-21 stsp char *new_commit_brief_str = NULL;
8751 e600f124 2021-03-21 stsp struct got_object_id *yca_id = NULL;
8752 e600f124 2021-03-21 stsp struct got_commit_object *yca_commit = NULL;
8753 e600f124 2021-03-21 stsp char *yca_id_str = NULL, *yca_brief_str = NULL;
8754 e600f124 2021-03-21 stsp char *custom_refs_str;
8755 e600f124 2021-03-21 stsp
8756 e600f124 2021-03-21 stsp if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
8757 e600f124 2021-03-21 stsp return got_error_from_errno("asprintf");
8758 e600f124 2021-03-21 stsp
8759 e600f124 2021-03-21 stsp err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
8760 e600f124 2021-03-21 stsp 0, 0, refs_idmap, custom_refs_str);
8761 e600f124 2021-03-21 stsp if (err)
8762 e600f124 2021-03-21 stsp goto done;
8763 e600f124 2021-03-21 stsp
8764 e600f124 2021-03-21 stsp err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
8765 e600f124 2021-03-21 stsp if (err)
8766 e600f124 2021-03-21 stsp goto done;
8767 e600f124 2021-03-21 stsp
8768 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
8769 e600f124 2021-03-21 stsp if (refs) {
8770 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, new_commit_id, repo);
8771 e600f124 2021-03-21 stsp if (err)
8772 e600f124 2021-03-21 stsp goto done;
8773 e600f124 2021-03-21 stsp }
8774 e600f124 2021-03-21 stsp
8775 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
8776 e600f124 2021-03-21 stsp if (err)
8777 e600f124 2021-03-21 stsp goto done;
8778 e600f124 2021-03-21 stsp
8779 e600f124 2021-03-21 stsp err = get_commit_brief_str(&new_commit_brief_str, new_commit);
8780 e600f124 2021-03-21 stsp if (err)
8781 e600f124 2021-03-21 stsp goto done;
8782 e600f124 2021-03-21 stsp
8783 e600f124 2021-03-21 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8784 768705e3 2021-09-27 thomas old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
8785 e600f124 2021-03-21 stsp if (err)
8786 e600f124 2021-03-21 stsp goto done;
8787 e600f124 2021-03-21 stsp
8788 e600f124 2021-03-21 stsp printf("has become commit %s%s%s%s\n %s\n", new_id_str,
8789 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
8790 e600f124 2021-03-21 stsp refs_str ? ")" : "", new_commit_brief_str);
8791 e600f124 2021-03-21 stsp if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
8792 e600f124 2021-03-21 stsp got_object_id_cmp(yca_id, old_commit_id) != 0) {
8793 e600f124 2021-03-21 stsp free(refs_str);
8794 e600f124 2021-03-21 stsp refs_str = NULL;
8795 e600f124 2021-03-21 stsp
8796 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&yca_commit, repo, yca_id);
8797 e600f124 2021-03-21 stsp if (err)
8798 e600f124 2021-03-21 stsp goto done;
8799 e600f124 2021-03-21 stsp
8800 e600f124 2021-03-21 stsp err = get_commit_brief_str(&yca_brief_str, yca_commit);
8801 e600f124 2021-03-21 stsp if (err)
8802 e600f124 2021-03-21 stsp goto done;
8803 e600f124 2021-03-21 stsp
8804 e600f124 2021-03-21 stsp err = got_object_id_str(&yca_id_str, yca_id);
8805 e600f124 2021-03-21 stsp if (err)
8806 e600f124 2021-03-21 stsp goto done;
8807 e600f124 2021-03-21 stsp
8808 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
8809 e600f124 2021-03-21 stsp if (refs) {
8810 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, yca_id, repo);
8811 e600f124 2021-03-21 stsp if (err)
8812 e600f124 2021-03-21 stsp goto done;
8813 e600f124 2021-03-21 stsp }
8814 e600f124 2021-03-21 stsp printf("history forked at %s%s%s%s\n %s\n",
8815 e600f124 2021-03-21 stsp yca_id_str,
8816 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
8817 e600f124 2021-03-21 stsp refs_str ? ")" : "", yca_brief_str);
8818 e600f124 2021-03-21 stsp }
8819 e600f124 2021-03-21 stsp done:
8820 e600f124 2021-03-21 stsp free(custom_refs_str);
8821 e600f124 2021-03-21 stsp free(new_commit_id);
8822 e600f124 2021-03-21 stsp free(refs_str);
8823 e600f124 2021-03-21 stsp free(yca_id);
8824 e600f124 2021-03-21 stsp free(yca_id_str);
8825 e600f124 2021-03-21 stsp free(yca_brief_str);
8826 e600f124 2021-03-21 stsp if (new_commit)
8827 e600f124 2021-03-21 stsp got_object_commit_close(new_commit);
8828 e600f124 2021-03-21 stsp if (yca_commit)
8829 e600f124 2021-03-21 stsp got_object_commit_close(yca_commit);
8830 e600f124 2021-03-21 stsp
8831 e600f124 2021-03-21 stsp return NULL;
8832 af61c510 2019-07-19 stsp }
8833 af61c510 2019-07-19 stsp
8834 af61c510 2019-07-19 stsp static const struct got_error *
8835 643b85bc 2021-07-16 stsp process_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
8836 643b85bc 2021-07-16 stsp int delete, struct got_repository *repo)
8837 e600f124 2021-03-21 stsp {
8838 e600f124 2021-03-21 stsp const struct got_error *err;
8839 e600f124 2021-03-21 stsp struct got_reflist_head refs, backup_refs;
8840 e600f124 2021-03-21 stsp struct got_reflist_entry *re;
8841 e600f124 2021-03-21 stsp const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
8842 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
8843 e600f124 2021-03-21 stsp char *branch_name = NULL;
8844 e600f124 2021-03-21 stsp struct got_commit_object *old_commit = NULL;
8845 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
8846 9e822917 2021-03-23 stsp int wanted_branch_found = 0;
8847 e600f124 2021-03-21 stsp
8848 e600f124 2021-03-21 stsp TAILQ_INIT(&refs);
8849 e600f124 2021-03-21 stsp TAILQ_INIT(&backup_refs);
8850 e600f124 2021-03-21 stsp
8851 e600f124 2021-03-21 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8852 e600f124 2021-03-21 stsp if (err)
8853 e600f124 2021-03-21 stsp return err;
8854 e600f124 2021-03-21 stsp
8855 e600f124 2021-03-21 stsp err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
8856 e600f124 2021-03-21 stsp if (err)
8857 e600f124 2021-03-21 stsp goto done;
8858 e600f124 2021-03-21 stsp
8859 e600f124 2021-03-21 stsp if (wanted_branch_name) {
8860 e600f124 2021-03-21 stsp if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
8861 e600f124 2021-03-21 stsp wanted_branch_name += 11;
8862 e600f124 2021-03-21 stsp }
8863 e600f124 2021-03-21 stsp
8864 e600f124 2021-03-21 stsp err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
8865 e600f124 2021-03-21 stsp got_ref_cmp_by_commit_timestamp_descending, repo);
8866 e600f124 2021-03-21 stsp if (err)
8867 e600f124 2021-03-21 stsp goto done;
8868 e600f124 2021-03-21 stsp
8869 e600f124 2021-03-21 stsp TAILQ_FOREACH(re, &backup_refs, entry) {
8870 e600f124 2021-03-21 stsp const char *refname = got_ref_get_name(re->ref);
8871 e600f124 2021-03-21 stsp char *slash;
8872 e600f124 2021-03-21 stsp
8873 643b85bc 2021-07-16 stsp err = check_cancelled(NULL);
8874 643b85bc 2021-07-16 stsp if (err)
8875 643b85bc 2021-07-16 stsp break;
8876 643b85bc 2021-07-16 stsp
8877 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, re->ref);
8878 e600f124 2021-03-21 stsp if (err)
8879 e600f124 2021-03-21 stsp break;
8880 e600f124 2021-03-21 stsp
8881 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&old_commit, repo,
8882 e600f124 2021-03-21 stsp old_commit_id);
8883 e600f124 2021-03-21 stsp if (err)
8884 e600f124 2021-03-21 stsp break;
8885 e600f124 2021-03-21 stsp
8886 e600f124 2021-03-21 stsp if (strncmp(backup_ref_prefix, refname,
8887 e600f124 2021-03-21 stsp backup_ref_prefix_len) == 0)
8888 e600f124 2021-03-21 stsp refname += backup_ref_prefix_len;
8889 e600f124 2021-03-21 stsp
8890 e600f124 2021-03-21 stsp while (refname[0] == '/')
8891 e600f124 2021-03-21 stsp refname++;
8892 e600f124 2021-03-21 stsp
8893 e600f124 2021-03-21 stsp branch_name = strdup(refname);
8894 e600f124 2021-03-21 stsp if (branch_name == NULL) {
8895 e600f124 2021-03-21 stsp err = got_error_from_errno("strdup");
8896 e600f124 2021-03-21 stsp break;
8897 e600f124 2021-03-21 stsp }
8898 e600f124 2021-03-21 stsp slash = strrchr(branch_name, '/');
8899 e600f124 2021-03-21 stsp if (slash) {
8900 e600f124 2021-03-21 stsp *slash = '\0';
8901 e600f124 2021-03-21 stsp refname += strlen(branch_name) + 1;
8902 e600f124 2021-03-21 stsp }
8903 e600f124 2021-03-21 stsp
8904 e600f124 2021-03-21 stsp if (wanted_branch_name == NULL ||
8905 e600f124 2021-03-21 stsp strcmp(wanted_branch_name, branch_name) == 0) {
8906 9e822917 2021-03-23 stsp wanted_branch_found = 1;
8907 643b85bc 2021-07-16 stsp if (delete) {
8908 643b85bc 2021-07-16 stsp err = delete_backup_ref(re->ref,
8909 643b85bc 2021-07-16 stsp old_commit_id, repo);
8910 643b85bc 2021-07-16 stsp } else {
8911 643b85bc 2021-07-16 stsp err = print_backup_ref(branch_name, refname,
8912 abc59930 2021-09-05 naddy old_commit_id, old_commit, refs_idmap,
8913 abc59930 2021-09-05 naddy repo);
8914 643b85bc 2021-07-16 stsp }
8915 e600f124 2021-03-21 stsp if (err)
8916 e600f124 2021-03-21 stsp break;
8917 e600f124 2021-03-21 stsp }
8918 e600f124 2021-03-21 stsp
8919 e600f124 2021-03-21 stsp free(old_commit_id);
8920 e600f124 2021-03-21 stsp old_commit_id = NULL;
8921 e600f124 2021-03-21 stsp free(branch_name);
8922 e600f124 2021-03-21 stsp branch_name = NULL;
8923 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
8924 e600f124 2021-03-21 stsp old_commit = NULL;
8925 e600f124 2021-03-21 stsp }
8926 9e822917 2021-03-23 stsp
8927 9e822917 2021-03-23 stsp if (wanted_branch_name && !wanted_branch_found) {
8928 9e822917 2021-03-23 stsp err = got_error_fmt(GOT_ERR_NOT_REF,
8929 9e822917 2021-03-23 stsp "%s/%s/", backup_ref_prefix, wanted_branch_name);
8930 9e822917 2021-03-23 stsp }
8931 e600f124 2021-03-21 stsp done:
8932 e600f124 2021-03-21 stsp if (refs_idmap)
8933 e600f124 2021-03-21 stsp got_reflist_object_id_map_free(refs_idmap);
8934 e600f124 2021-03-21 stsp got_ref_list_free(&refs);
8935 e600f124 2021-03-21 stsp got_ref_list_free(&backup_refs);
8936 e600f124 2021-03-21 stsp free(old_commit_id);
8937 e600f124 2021-03-21 stsp free(branch_name);
8938 e600f124 2021-03-21 stsp if (old_commit)
8939 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
8940 e600f124 2021-03-21 stsp return err;
8941 e600f124 2021-03-21 stsp }
8942 e600f124 2021-03-21 stsp
8943 e600f124 2021-03-21 stsp static const struct got_error *
8944 d52bac28 2021-10-08 thomas abort_progress(void *arg, unsigned char status, const char *path)
8945 d52bac28 2021-10-08 thomas {
8946 d52bac28 2021-10-08 thomas /*
8947 d52bac28 2021-10-08 thomas * Unversioned files should not clutter progress output when
8948 d52bac28 2021-10-08 thomas * an operation is aborted.
8949 d52bac28 2021-10-08 thomas */
8950 d52bac28 2021-10-08 thomas if (status == GOT_STATUS_UNVERSIONED)
8951 d52bac28 2021-10-08 thomas return NULL;
8952 d52bac28 2021-10-08 thomas
8953 d52bac28 2021-10-08 thomas return update_progress(arg, status, path);
8954 d52bac28 2021-10-08 thomas }
8955 d52bac28 2021-10-08 thomas
8956 d52bac28 2021-10-08 thomas static const struct got_error *
8957 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
8958 818c7501 2019-07-11 stsp {
8959 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
8960 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
8961 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
8962 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8963 818c7501 2019-07-11 stsp char *cwd = NULL;
8964 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
8965 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
8966 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
8967 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
8968 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
8969 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
8970 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
8971 10604dce 2021-09-24 thomas int histedit_in_progress = 0, merge_in_progress = 0;
8972 10604dce 2021-09-24 thomas int create_backup = 1, list_backups = 0, delete_backups = 0;
8973 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
8974 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
8975 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
8976 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
8977 bb494413 2021-09-28 thomas struct got_update_progress_arg upa;
8978 818c7501 2019-07-11 stsp
8979 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
8980 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
8981 bb494413 2021-09-28 thomas memset(&upa, 0, sizeof(upa));
8982 818c7501 2019-07-11 stsp
8983 643b85bc 2021-07-16 stsp while ((ch = getopt(argc, argv, "aclX")) != -1) {
8984 818c7501 2019-07-11 stsp switch (ch) {
8985 818c7501 2019-07-11 stsp case 'a':
8986 818c7501 2019-07-11 stsp abort_rebase = 1;
8987 818c7501 2019-07-11 stsp break;
8988 818c7501 2019-07-11 stsp case 'c':
8989 818c7501 2019-07-11 stsp continue_rebase = 1;
8990 818c7501 2019-07-11 stsp break;
8991 e600f124 2021-03-21 stsp case 'l':
8992 e600f124 2021-03-21 stsp list_backups = 1;
8993 e600f124 2021-03-21 stsp break;
8994 643b85bc 2021-07-16 stsp case 'X':
8995 643b85bc 2021-07-16 stsp delete_backups = 1;
8996 643b85bc 2021-07-16 stsp break;
8997 818c7501 2019-07-11 stsp default:
8998 818c7501 2019-07-11 stsp usage_rebase();
8999 818c7501 2019-07-11 stsp /* NOTREACHED */
9000 818c7501 2019-07-11 stsp }
9001 818c7501 2019-07-11 stsp }
9002 818c7501 2019-07-11 stsp
9003 818c7501 2019-07-11 stsp argc -= optind;
9004 818c7501 2019-07-11 stsp argv += optind;
9005 818c7501 2019-07-11 stsp
9006 43012d58 2019-07-14 stsp #ifndef PROFILE
9007 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9008 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
9009 43012d58 2019-07-14 stsp err(1, "pledge");
9010 43012d58 2019-07-14 stsp #endif
9011 e600f124 2021-03-21 stsp if (list_backups) {
9012 e600f124 2021-03-21 stsp if (abort_rebase)
9013 e600f124 2021-03-21 stsp option_conflict('l', 'a');
9014 e600f124 2021-03-21 stsp if (continue_rebase)
9015 e600f124 2021-03-21 stsp option_conflict('l', 'c');
9016 643b85bc 2021-07-16 stsp if (delete_backups)
9017 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
9018 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
9019 643b85bc 2021-07-16 stsp usage_rebase();
9020 643b85bc 2021-07-16 stsp } else if (delete_backups) {
9021 643b85bc 2021-07-16 stsp if (abort_rebase)
9022 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
9023 643b85bc 2021-07-16 stsp if (continue_rebase)
9024 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
9025 643b85bc 2021-07-16 stsp if (list_backups)
9026 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
9027 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
9028 818c7501 2019-07-11 stsp usage_rebase();
9029 e600f124 2021-03-21 stsp } else {
9030 e600f124 2021-03-21 stsp if (abort_rebase && continue_rebase)
9031 e600f124 2021-03-21 stsp usage_rebase();
9032 e600f124 2021-03-21 stsp else if (abort_rebase || continue_rebase) {
9033 e600f124 2021-03-21 stsp if (argc != 0)
9034 e600f124 2021-03-21 stsp usage_rebase();
9035 e600f124 2021-03-21 stsp } else if (argc != 1)
9036 e600f124 2021-03-21 stsp usage_rebase();
9037 e600f124 2021-03-21 stsp }
9038 818c7501 2019-07-11 stsp
9039 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
9040 818c7501 2019-07-11 stsp if (cwd == NULL) {
9041 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
9042 818c7501 2019-07-11 stsp goto done;
9043 818c7501 2019-07-11 stsp }
9044 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
9045 fa51e947 2020-03-27 stsp if (error) {
9046 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
9047 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
9048 e600f124 2021-03-21 stsp goto done;
9049 e600f124 2021-03-21 stsp } else {
9050 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9051 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
9052 e600f124 2021-03-21 stsp "rebase", cwd);
9053 e600f124 2021-03-21 stsp goto done;
9054 e600f124 2021-03-21 stsp }
9055 fa51e947 2020-03-27 stsp }
9056 818c7501 2019-07-11 stsp
9057 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
9058 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
9059 818c7501 2019-07-11 stsp if (error != NULL)
9060 818c7501 2019-07-11 stsp goto done;
9061 818c7501 2019-07-11 stsp
9062 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9063 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
9064 7ef62c4e 2020-02-24 stsp if (error)
9065 7ef62c4e 2020-02-24 stsp goto done;
9066 7ef62c4e 2020-02-24 stsp
9067 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
9068 643b85bc 2021-07-16 stsp error = process_backup_refs(
9069 643b85bc 2021-07-16 stsp GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
9070 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
9071 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
9072 e600f124 2021-03-21 stsp }
9073 e600f124 2021-03-21 stsp
9074 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
9075 7ef62c4e 2020-02-24 stsp worktree);
9076 818c7501 2019-07-11 stsp if (error)
9077 7ef62c4e 2020-02-24 stsp goto done;
9078 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
9079 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
9080 10604dce 2021-09-24 thomas goto done;
9081 10604dce 2021-09-24 thomas }
9082 10604dce 2021-09-24 thomas
9083 10604dce 2021-09-24 thomas error = got_worktree_merge_in_progress(&merge_in_progress,
9084 10604dce 2021-09-24 thomas worktree, repo);
9085 10604dce 2021-09-24 thomas if (error)
9086 10604dce 2021-09-24 thomas goto done;
9087 10604dce 2021-09-24 thomas if (merge_in_progress) {
9088 10604dce 2021-09-24 thomas error = got_error(GOT_ERR_MERGE_BUSY);
9089 818c7501 2019-07-11 stsp goto done;
9090 7ef62c4e 2020-02-24 stsp }
9091 818c7501 2019-07-11 stsp
9092 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9093 818c7501 2019-07-11 stsp if (error)
9094 818c7501 2019-07-11 stsp goto done;
9095 818c7501 2019-07-11 stsp
9096 f6794adc 2019-07-23 stsp if (abort_rebase) {
9097 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
9098 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
9099 f6794adc 2019-07-23 stsp goto done;
9100 f6794adc 2019-07-23 stsp }
9101 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
9102 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
9103 3e3a69f1 2019-07-25 stsp worktree, repo);
9104 818c7501 2019-07-11 stsp if (error)
9105 818c7501 2019-07-11 stsp goto done;
9106 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
9107 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
9108 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
9109 d52bac28 2021-10-08 thomas new_base_branch, abort_progress, &upa);
9110 818c7501 2019-07-11 stsp if (error)
9111 818c7501 2019-07-11 stsp goto done;
9112 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
9113 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
9114 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
9115 818c7501 2019-07-11 stsp }
9116 818c7501 2019-07-11 stsp
9117 818c7501 2019-07-11 stsp if (continue_rebase) {
9118 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
9119 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
9120 f6794adc 2019-07-23 stsp goto done;
9121 f6794adc 2019-07-23 stsp }
9122 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
9123 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
9124 3e3a69f1 2019-07-25 stsp worktree, repo);
9125 818c7501 2019-07-11 stsp if (error)
9126 818c7501 2019-07-11 stsp goto done;
9127 818c7501 2019-07-11 stsp
9128 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
9129 01757395 2019-07-12 stsp resume_commit_id, repo);
9130 818c7501 2019-07-11 stsp if (error)
9131 818c7501 2019-07-11 stsp goto done;
9132 818c7501 2019-07-11 stsp
9133 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
9134 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
9135 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
9136 818c7501 2019-07-11 stsp goto done;
9137 818c7501 2019-07-11 stsp }
9138 818c7501 2019-07-11 stsp } else {
9139 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
9140 818c7501 2019-07-11 stsp if (error != NULL)
9141 818c7501 2019-07-11 stsp goto done;
9142 ff0d2220 2019-07-11 stsp }
9143 818c7501 2019-07-11 stsp
9144 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
9145 ff0d2220 2019-07-11 stsp if (error)
9146 ff0d2220 2019-07-11 stsp goto done;
9147 ff0d2220 2019-07-11 stsp
9148 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
9149 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
9150 a51a74b3 2019-07-27 stsp
9151 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
9152 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9153 768705e3 2021-09-27 thomas base_commit_id, branch_head_commit_id, 1, repo,
9154 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
9155 818c7501 2019-07-11 stsp if (error)
9156 818c7501 2019-07-11 stsp goto done;
9157 818c7501 2019-07-11 stsp if (yca_id == NULL) {
9158 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
9159 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
9160 818c7501 2019-07-11 stsp "with work tree's branch");
9161 818c7501 2019-07-11 stsp goto done;
9162 818c7501 2019-07-11 stsp }
9163 818c7501 2019-07-11 stsp
9164 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
9165 a51a74b3 2019-07-27 stsp if (error) {
9166 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
9167 a51a74b3 2019-07-27 stsp goto done;
9168 a51a74b3 2019-07-27 stsp error = NULL;
9169 a51a74b3 2019-07-27 stsp } else {
9170 82d979c5 2021-11-04 thomas struct got_pathlist_head paths;
9171 82d979c5 2021-11-04 thomas printf("%s is already based on %s\n",
9172 df3ed485 2021-01-31 stsp got_ref_get_name(branch),
9173 df3ed485 2021-01-31 stsp got_worktree_get_head_ref_name(worktree));
9174 82d979c5 2021-11-04 thomas error = switch_head_ref(branch, branch_head_commit_id,
9175 82d979c5 2021-11-04 thomas worktree, repo);
9176 82d979c5 2021-11-04 thomas if (error)
9177 82d979c5 2021-11-04 thomas goto done;
9178 82d979c5 2021-11-04 thomas error = got_worktree_set_base_commit_id(worktree, repo,
9179 82d979c5 2021-11-04 thomas branch_head_commit_id);
9180 82d979c5 2021-11-04 thomas if (error)
9181 82d979c5 2021-11-04 thomas goto done;
9182 82d979c5 2021-11-04 thomas TAILQ_INIT(&paths);
9183 82d979c5 2021-11-04 thomas error = got_pathlist_append(&paths, "", NULL);
9184 82d979c5 2021-11-04 thomas if (error)
9185 82d979c5 2021-11-04 thomas goto done;
9186 82d979c5 2021-11-04 thomas error = got_worktree_checkout_files(worktree,
9187 82d979c5 2021-11-04 thomas &paths, repo, update_progress, &upa,
9188 82d979c5 2021-11-04 thomas check_cancelled, NULL);
9189 82d979c5 2021-11-04 thomas got_pathlist_free(&paths);
9190 82d979c5 2021-11-04 thomas if (error)
9191 82d979c5 2021-11-04 thomas goto done;
9192 82d979c5 2021-11-04 thomas if (upa.did_something) {
9193 82d979c5 2021-11-04 thomas char *id_str;
9194 82d979c5 2021-11-04 thomas error = got_object_id_str(&id_str,
9195 82d979c5 2021-11-04 thomas branch_head_commit_id);
9196 82d979c5 2021-11-04 thomas if (error)
9197 82d979c5 2021-11-04 thomas goto done;
9198 82d979c5 2021-11-04 thomas printf("Updated to %s: %s\n",
9199 82d979c5 2021-11-04 thomas got_worktree_get_head_ref_name(worktree),
9200 82d979c5 2021-11-04 thomas id_str);
9201 82d979c5 2021-11-04 thomas free(id_str);
9202 82d979c5 2021-11-04 thomas } else
9203 82d979c5 2021-11-04 thomas printf("Already up-to-date\n");
9204 82d979c5 2021-11-04 thomas print_update_progress_stats(&upa);
9205 a51a74b3 2019-07-27 stsp goto done;
9206 a51a74b3 2019-07-27 stsp }
9207 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
9208 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
9209 818c7501 2019-07-11 stsp if (error)
9210 818c7501 2019-07-11 stsp goto done;
9211 818c7501 2019-07-11 stsp }
9212 818c7501 2019-07-11 stsp
9213 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
9214 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
9215 818c7501 2019-07-11 stsp if (error)
9216 818c7501 2019-07-11 stsp goto done;
9217 818c7501 2019-07-11 stsp
9218 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9219 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
9220 fc66b545 2019-08-12 stsp if (pid == NULL) {
9221 fc66b545 2019-08-12 stsp if (!continue_rebase) {
9222 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
9223 d52bac28 2021-10-08 thomas repo, new_base_branch, abort_progress, &upa);
9224 fc66b545 2019-08-12 stsp if (error)
9225 fc66b545 2019-08-12 stsp goto done;
9226 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
9227 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
9228 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
9229 9627c110 2020-04-18 stsp
9230 fc66b545 2019-08-12 stsp }
9231 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
9232 fc66b545 2019-08-12 stsp goto done;
9233 fc66b545 2019-08-12 stsp }
9234 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
9235 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
9236 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
9237 818c7501 2019-07-11 stsp got_object_commit_close(commit);
9238 818c7501 2019-07-11 stsp commit = NULL;
9239 818c7501 2019-07-11 stsp if (error)
9240 818c7501 2019-07-11 stsp goto done;
9241 64c6d990 2019-07-11 stsp
9242 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
9243 38b0338b 2019-11-29 stsp if (continue_rebase) {
9244 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
9245 e600f124 2021-03-21 stsp branch, new_base_branch, tmp_branch, repo,
9246 e600f124 2021-03-21 stsp create_backup);
9247 38b0338b 2019-11-29 stsp goto done;
9248 38b0338b 2019-11-29 stsp } else {
9249 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
9250 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
9251 38b0338b 2019-11-29 stsp char *id_str;
9252 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
9253 38b0338b 2019-11-29 stsp new_base_branch);
9254 38b0338b 2019-11-29 stsp if (error)
9255 38b0338b 2019-11-29 stsp goto done;
9256 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
9257 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
9258 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
9259 38b0338b 2019-11-29 stsp free(id_str);
9260 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
9261 38b0338b 2019-11-29 stsp new_head_commit_id);
9262 38b0338b 2019-11-29 stsp if (error)
9263 38b0338b 2019-11-29 stsp goto done;
9264 e600f124 2021-03-21 stsp /* No backup needed since objects did not change. */
9265 e600f124 2021-03-21 stsp create_backup = 0;
9266 38b0338b 2019-11-29 stsp }
9267 818c7501 2019-07-11 stsp }
9268 818c7501 2019-07-11 stsp
9269 818c7501 2019-07-11 stsp pid = NULL;
9270 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &commits, entry) {
9271 9627c110 2020-04-18 stsp
9272 818c7501 2019-07-11 stsp commit_id = qid->id;
9273 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
9274 818c7501 2019-07-11 stsp pid = qid;
9275 818c7501 2019-07-11 stsp
9276 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9277 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
9278 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
9279 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
9280 818c7501 2019-07-11 stsp if (error)
9281 818c7501 2019-07-11 stsp goto done;
9282 9627c110 2020-04-18 stsp
9283 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
9284 bb494413 2021-09-28 thomas if (upa.conflicts > 0 || upa.missing > 0 ||
9285 bb494413 2021-09-28 thomas upa.not_deleted > 0 || upa.unversioned > 0) {
9286 bb494413 2021-09-28 thomas if (upa.conflicts > 0) {
9287 bb494413 2021-09-28 thomas error = show_rebase_merge_conflict(qid->id,
9288 bb494413 2021-09-28 thomas repo);
9289 bb494413 2021-09-28 thomas if (error)
9290 bb494413 2021-09-28 thomas goto done;
9291 bb494413 2021-09-28 thomas }
9292 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9293 818c7501 2019-07-11 stsp break;
9294 01757395 2019-07-12 stsp }
9295 818c7501 2019-07-11 stsp
9296 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
9297 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
9298 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9299 818c7501 2019-07-11 stsp if (error)
9300 818c7501 2019-07-11 stsp goto done;
9301 818c7501 2019-07-11 stsp }
9302 818c7501 2019-07-11 stsp
9303 bb494413 2021-09-28 thomas if (upa.conflicts > 0 || upa.missing > 0 ||
9304 bb494413 2021-09-28 thomas upa.not_deleted > 0 || upa.unversioned > 0) {
9305 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
9306 818c7501 2019-07-11 stsp if (error)
9307 818c7501 2019-07-11 stsp goto done;
9308 bb494413 2021-09-28 thomas if (upa.conflicts > 0 && upa.missing == 0 &&
9309 bb494413 2021-09-28 thomas upa.not_deleted == 0 && upa.unversioned == 0) {
9310 bb494413 2021-09-28 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
9311 bb494413 2021-09-28 thomas "conflicts must be resolved before rebasing "
9312 bb494413 2021-09-28 thomas "can continue");
9313 bb494413 2021-09-28 thomas } else if (upa.conflicts > 0) {
9314 bb494413 2021-09-28 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
9315 bb494413 2021-09-28 thomas "conflicts must be resolved before rebasing "
9316 bb494413 2021-09-28 thomas "can continue; changes destined for some "
9317 bb494413 2021-09-28 thomas "files were not yet merged and should be "
9318 bb494413 2021-09-28 thomas "merged manually if required before the "
9319 bb494413 2021-09-28 thomas "rebase operation is continued");
9320 bb494413 2021-09-28 thomas } else {
9321 bb494413 2021-09-28 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
9322 bb494413 2021-09-28 thomas "changes destined for some files were not "
9323 bb494413 2021-09-28 thomas "yet merged and should be merged manually "
9324 bb494413 2021-09-28 thomas "if required before the rebase operation "
9325 bb494413 2021-09-28 thomas "is continued");
9326 bb494413 2021-09-28 thomas }
9327 818c7501 2019-07-11 stsp } else
9328 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
9329 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, repo, create_backup);
9330 818c7501 2019-07-11 stsp done:
9331 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
9332 818c7501 2019-07-11 stsp free(branch_head_commit_id);
9333 818c7501 2019-07-11 stsp free(resume_commit_id);
9334 818c7501 2019-07-11 stsp free(yca_id);
9335 818c7501 2019-07-11 stsp if (commit)
9336 818c7501 2019-07-11 stsp got_object_commit_close(commit);
9337 818c7501 2019-07-11 stsp if (branch)
9338 818c7501 2019-07-11 stsp got_ref_close(branch);
9339 818c7501 2019-07-11 stsp if (new_base_branch)
9340 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
9341 818c7501 2019-07-11 stsp if (tmp_branch)
9342 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
9343 5ef14e63 2019-06-02 stsp if (worktree)
9344 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
9345 1d0f4054 2021-06-17 stsp if (repo) {
9346 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
9347 1d0f4054 2021-06-17 stsp if (error == NULL)
9348 1d0f4054 2021-06-17 stsp error = close_err;
9349 1d0f4054 2021-06-17 stsp }
9350 5ef14e63 2019-06-02 stsp return error;
9351 0ebf8283 2019-07-24 stsp }
9352 0ebf8283 2019-07-24 stsp
9353 0ebf8283 2019-07-24 stsp __dead static void
9354 0ebf8283 2019-07-24 stsp usage_histedit(void)
9355 0ebf8283 2019-07-24 stsp {
9356 c50a7455 2021-10-04 thomas fprintf(stderr, "usage: %s histedit [-a] [-c] [-e] [-f] "
9357 643b85bc 2021-07-16 stsp "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
9358 643b85bc 2021-07-16 stsp getprogname());
9359 0ebf8283 2019-07-24 stsp exit(1);
9360 0ebf8283 2019-07-24 stsp }
9361 0ebf8283 2019-07-24 stsp
9362 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
9363 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
9364 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
9365 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
9366 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
9367 0ebf8283 2019-07-24 stsp
9368 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
9369 0ebf8283 2019-07-24 stsp unsigned char code;
9370 0ebf8283 2019-07-24 stsp const char *name;
9371 0ebf8283 2019-07-24 stsp const char *desc;
9372 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
9373 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
9374 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
9375 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
9376 82997472 2020-01-29 stsp "be used" },
9377 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
9378 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
9379 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
9380 0ebf8283 2019-07-24 stsp };
9381 0ebf8283 2019-07-24 stsp
9382 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
9383 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
9384 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
9385 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
9386 0ebf8283 2019-07-24 stsp char *logmsg;
9387 0ebf8283 2019-07-24 stsp };
9388 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
9389 0ebf8283 2019-07-24 stsp
9390 0ebf8283 2019-07-24 stsp static const struct got_error *
9391 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
9392 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
9393 0ebf8283 2019-07-24 stsp {
9394 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9395 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
9396 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9397 8138f3e1 2019-08-11 stsp int n;
9398 0ebf8283 2019-07-24 stsp
9399 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
9400 0ebf8283 2019-07-24 stsp if (err)
9401 0ebf8283 2019-07-24 stsp goto done;
9402 0ebf8283 2019-07-24 stsp
9403 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
9404 0ebf8283 2019-07-24 stsp if (err)
9405 0ebf8283 2019-07-24 stsp goto done;
9406 0ebf8283 2019-07-24 stsp
9407 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
9408 0ebf8283 2019-07-24 stsp if (err)
9409 0ebf8283 2019-07-24 stsp goto done;
9410 0ebf8283 2019-07-24 stsp
9411 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
9412 0ebf8283 2019-07-24 stsp if (n < 0)
9413 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9414 0ebf8283 2019-07-24 stsp done:
9415 0ebf8283 2019-07-24 stsp if (commit)
9416 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9417 0ebf8283 2019-07-24 stsp free(id_str);
9418 0ebf8283 2019-07-24 stsp free(logmsg);
9419 0ebf8283 2019-07-24 stsp return err;
9420 0ebf8283 2019-07-24 stsp }
9421 0ebf8283 2019-07-24 stsp
9422 0ebf8283 2019-07-24 stsp static const struct got_error *
9423 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
9424 c50a7455 2021-10-04 thomas FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
9425 c50a7455 2021-10-04 thomas struct got_repository *repo)
9426 0ebf8283 2019-07-24 stsp {
9427 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9428 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
9429 466785b9 2020-12-10 jrick const char *histedit_cmd = NULL;
9430 0ebf8283 2019-07-24 stsp
9431 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
9432 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
9433 0ebf8283 2019-07-24 stsp
9434 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
9435 466785b9 2020-12-10 jrick histedit_cmd = got_histedit_cmds[0].name;
9436 c50a7455 2021-10-04 thomas if (edit_only)
9437 c50a7455 2021-10-04 thomas histedit_cmd = "edit";
9438 c50a7455 2021-10-04 thomas else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
9439 466785b9 2020-12-10 jrick histedit_cmd = "fold";
9440 466785b9 2020-12-10 jrick err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
9441 0ebf8283 2019-07-24 stsp if (err)
9442 0ebf8283 2019-07-24 stsp break;
9443 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
9444 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
9445 083957f4 2020-02-24 stsp if (n < 0) {
9446 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
9447 083957f4 2020-02-24 stsp break;
9448 083957f4 2020-02-24 stsp }
9449 083957f4 2020-02-24 stsp }
9450 0ebf8283 2019-07-24 stsp }
9451 0ebf8283 2019-07-24 stsp
9452 0ebf8283 2019-07-24 stsp return err;
9453 0ebf8283 2019-07-24 stsp }
9454 0ebf8283 2019-07-24 stsp
9455 0ebf8283 2019-07-24 stsp static const struct got_error *
9456 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
9457 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
9458 0ebf8283 2019-07-24 stsp {
9459 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9460 6059809a 2020-12-17 stsp size_t i;
9461 6059809a 2020-12-17 stsp int n;
9462 514f2ffe 2020-01-29 stsp char *id_str;
9463 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
9464 514f2ffe 2020-01-29 stsp
9465 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(commits);
9466 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
9467 514f2ffe 2020-01-29 stsp if (err)
9468 514f2ffe 2020-01-29 stsp return err;
9469 514f2ffe 2020-01-29 stsp
9470 514f2ffe 2020-01-29 stsp n = fprintf(f,
9471 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
9472 514f2ffe 2020-01-29 stsp "# commit %s\n"
9473 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
9474 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
9475 514f2ffe 2020-01-29 stsp if (n < 0) {
9476 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
9477 514f2ffe 2020-01-29 stsp goto done;
9478 514f2ffe 2020-01-29 stsp }
9479 0ebf8283 2019-07-24 stsp
9480 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
9481 514f2ffe 2020-01-29 stsp if (n < 0) {
9482 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
9483 514f2ffe 2020-01-29 stsp goto done;
9484 514f2ffe 2020-01-29 stsp }
9485 0ebf8283 2019-07-24 stsp
9486 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
9487 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
9488 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
9489 0ebf8283 2019-07-24 stsp cmd->desc);
9490 0ebf8283 2019-07-24 stsp if (n < 0) {
9491 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9492 0ebf8283 2019-07-24 stsp break;
9493 0ebf8283 2019-07-24 stsp }
9494 0ebf8283 2019-07-24 stsp }
9495 514f2ffe 2020-01-29 stsp done:
9496 514f2ffe 2020-01-29 stsp free(id_str);
9497 0ebf8283 2019-07-24 stsp return err;
9498 0ebf8283 2019-07-24 stsp }
9499 0ebf8283 2019-07-24 stsp
9500 0ebf8283 2019-07-24 stsp static const struct got_error *
9501 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
9502 0ebf8283 2019-07-24 stsp {
9503 0ebf8283 2019-07-24 stsp static char msg[42];
9504 0ebf8283 2019-07-24 stsp int ret;
9505 0ebf8283 2019-07-24 stsp
9506 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
9507 0ebf8283 2019-07-24 stsp lineno);
9508 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
9509 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
9510 0ebf8283 2019-07-24 stsp
9511 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
9512 0ebf8283 2019-07-24 stsp }
9513 0ebf8283 2019-07-24 stsp
9514 0ebf8283 2019-07-24 stsp static const struct got_error *
9515 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
9516 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
9517 0ebf8283 2019-07-24 stsp {
9518 0ebf8283 2019-07-24 stsp const struct got_error *err;
9519 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
9520 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
9521 0ebf8283 2019-07-24 stsp
9522 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9523 0ebf8283 2019-07-24 stsp if (err)
9524 0ebf8283 2019-07-24 stsp return err;
9525 0ebf8283 2019-07-24 stsp
9526 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
9527 0ebf8283 2019-07-24 stsp if (err)
9528 0ebf8283 2019-07-24 stsp goto done;
9529 0ebf8283 2019-07-24 stsp
9530 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
9531 5943eee2 2019-08-13 stsp if (err)
9532 5943eee2 2019-08-13 stsp goto done;
9533 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
9534 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
9535 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
9536 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
9537 0ebf8283 2019-07-24 stsp }
9538 0ebf8283 2019-07-24 stsp done:
9539 0ebf8283 2019-07-24 stsp if (folded_commit)
9540 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
9541 0ebf8283 2019-07-24 stsp free(id_str);
9542 5943eee2 2019-08-13 stsp free(folded_logmsg);
9543 0ebf8283 2019-07-24 stsp return err;
9544 0ebf8283 2019-07-24 stsp }
9545 0ebf8283 2019-07-24 stsp
9546 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
9547 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
9548 0ebf8283 2019-07-24 stsp {
9549 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
9550 0ebf8283 2019-07-24 stsp
9551 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
9552 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
9553 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
9554 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
9555 3f9de99f 2019-07-24 stsp folded = prev;
9556 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
9557 0ebf8283 2019-07-24 stsp }
9558 0ebf8283 2019-07-24 stsp
9559 0ebf8283 2019-07-24 stsp return folded;
9560 0ebf8283 2019-07-24 stsp }
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_logmsg(struct got_histedit_list_entry *hle,
9564 0ebf8283 2019-07-24 stsp struct got_repository *repo)
9565 0ebf8283 2019-07-24 stsp {
9566 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
9567 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
9568 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9569 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9570 1601cb9f 2020-09-11 naddy int logmsg_len;
9571 0ebf8283 2019-07-24 stsp int fd;
9572 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
9573 0ebf8283 2019-07-24 stsp
9574 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9575 0ebf8283 2019-07-24 stsp if (err)
9576 0ebf8283 2019-07-24 stsp return err;
9577 0ebf8283 2019-07-24 stsp
9578 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
9579 0ebf8283 2019-07-24 stsp if (folded) {
9580 0ebf8283 2019-07-24 stsp while (folded != hle) {
9581 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
9582 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
9583 3f9de99f 2019-07-24 stsp continue;
9584 3f9de99f 2019-07-24 stsp }
9585 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
9586 0ebf8283 2019-07-24 stsp logmsg, repo);
9587 0ebf8283 2019-07-24 stsp if (err)
9588 0ebf8283 2019-07-24 stsp goto done;
9589 0ebf8283 2019-07-24 stsp free(logmsg);
9590 0ebf8283 2019-07-24 stsp logmsg = new_msg;
9591 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
9592 0ebf8283 2019-07-24 stsp }
9593 0ebf8283 2019-07-24 stsp }
9594 0ebf8283 2019-07-24 stsp
9595 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9596 0ebf8283 2019-07-24 stsp if (err)
9597 0ebf8283 2019-07-24 stsp goto done;
9598 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
9599 5943eee2 2019-08-13 stsp if (err)
9600 5943eee2 2019-08-13 stsp goto done;
9601 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
9602 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
9603 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
9604 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
9605 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
9606 0ebf8283 2019-07-24 stsp goto done;
9607 0ebf8283 2019-07-24 stsp }
9608 0ebf8283 2019-07-24 stsp free(logmsg);
9609 0ebf8283 2019-07-24 stsp logmsg = new_msg;
9610 0ebf8283 2019-07-24 stsp
9611 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9612 0ebf8283 2019-07-24 stsp if (err)
9613 0ebf8283 2019-07-24 stsp goto done;
9614 0ebf8283 2019-07-24 stsp
9615 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
9616 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
9617 0ebf8283 2019-07-24 stsp if (err)
9618 0ebf8283 2019-07-24 stsp goto done;
9619 0ebf8283 2019-07-24 stsp
9620 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
9621 0ebf8283 2019-07-24 stsp close(fd);
9622 0ebf8283 2019-07-24 stsp
9623 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
9624 0ebf8283 2019-07-24 stsp if (err)
9625 0ebf8283 2019-07-24 stsp goto done;
9626 0ebf8283 2019-07-24 stsp
9627 bfa12d5e 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
9628 0d5bb276 2020-12-15 stsp logmsg_len, 0);
9629 0ebf8283 2019-07-24 stsp if (err) {
9630 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
9631 0ebf8283 2019-07-24 stsp goto done;
9632 71392a05 2020-12-13 stsp err = NULL;
9633 71392a05 2020-12-13 stsp hle->logmsg = strdup(new_msg);
9634 71392a05 2020-12-13 stsp if (hle->logmsg == NULL)
9635 71392a05 2020-12-13 stsp err = got_error_from_errno("strdup");
9636 0ebf8283 2019-07-24 stsp }
9637 0ebf8283 2019-07-24 stsp done:
9638 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
9639 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
9640 0ebf8283 2019-07-24 stsp free(logmsg_path);
9641 0ebf8283 2019-07-24 stsp free(logmsg);
9642 5943eee2 2019-08-13 stsp free(orig_logmsg);
9643 0ebf8283 2019-07-24 stsp free(editor);
9644 0ebf8283 2019-07-24 stsp if (commit)
9645 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9646 0ebf8283 2019-07-24 stsp return err;
9647 5ef14e63 2019-06-02 stsp }
9648 0ebf8283 2019-07-24 stsp
9649 0ebf8283 2019-07-24 stsp static const struct got_error *
9650 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
9651 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
9652 0ebf8283 2019-07-24 stsp {
9653 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9654 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
9655 6059809a 2020-12-17 stsp size_t i, size;
9656 0ebf8283 2019-07-24 stsp ssize_t len;
9657 6059809a 2020-12-17 stsp int lineno = 0;
9658 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
9659 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
9660 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
9661 0ebf8283 2019-07-24 stsp
9662 0ebf8283 2019-07-24 stsp for (;;) {
9663 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
9664 0ebf8283 2019-07-24 stsp if (len == -1) {
9665 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
9666 0ebf8283 2019-07-24 stsp if (feof(f))
9667 0ebf8283 2019-07-24 stsp break;
9668 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
9669 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
9670 0ebf8283 2019-07-24 stsp break;
9671 0ebf8283 2019-07-24 stsp }
9672 0ebf8283 2019-07-24 stsp lineno++;
9673 0ebf8283 2019-07-24 stsp p = line;
9674 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
9675 0ebf8283 2019-07-24 stsp p++;
9676 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
9677 0ebf8283 2019-07-24 stsp free(line);
9678 0ebf8283 2019-07-24 stsp line = NULL;
9679 0ebf8283 2019-07-24 stsp continue;
9680 0ebf8283 2019-07-24 stsp }
9681 0ebf8283 2019-07-24 stsp cmd = NULL;
9682 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
9683 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
9684 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
9685 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
9686 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
9687 0ebf8283 2019-07-24 stsp break;
9688 0ebf8283 2019-07-24 stsp }
9689 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
9690 0ebf8283 2019-07-24 stsp p++;
9691 0ebf8283 2019-07-24 stsp break;
9692 0ebf8283 2019-07-24 stsp }
9693 0ebf8283 2019-07-24 stsp }
9694 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
9695 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
9696 0ebf8283 2019-07-24 stsp break;
9697 0ebf8283 2019-07-24 stsp }
9698 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
9699 0ebf8283 2019-07-24 stsp p++;
9700 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
9701 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
9702 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
9703 0ebf8283 2019-07-24 stsp break;
9704 0ebf8283 2019-07-24 stsp }
9705 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
9706 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
9707 0ebf8283 2019-07-24 stsp if (err)
9708 0ebf8283 2019-07-24 stsp break;
9709 0ebf8283 2019-07-24 stsp } else {
9710 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
9711 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
9712 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
9713 0ebf8283 2019-07-24 stsp break;
9714 0ebf8283 2019-07-24 stsp }
9715 0ebf8283 2019-07-24 stsp }
9716 0ebf8283 2019-07-24 stsp free(line);
9717 0ebf8283 2019-07-24 stsp line = NULL;
9718 0ebf8283 2019-07-24 stsp continue;
9719 0ebf8283 2019-07-24 stsp } else {
9720 0ebf8283 2019-07-24 stsp end = p;
9721 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
9722 0ebf8283 2019-07-24 stsp end++;
9723 0ebf8283 2019-07-24 stsp *end = '\0';
9724 0ebf8283 2019-07-24 stsp
9725 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
9726 0ebf8283 2019-07-24 stsp if (err) {
9727 0ebf8283 2019-07-24 stsp /* override error code */
9728 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
9729 0ebf8283 2019-07-24 stsp break;
9730 0ebf8283 2019-07-24 stsp }
9731 0ebf8283 2019-07-24 stsp }
9732 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
9733 0ebf8283 2019-07-24 stsp if (hle == NULL) {
9734 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
9735 0ebf8283 2019-07-24 stsp break;
9736 0ebf8283 2019-07-24 stsp }
9737 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
9738 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
9739 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
9740 0ebf8283 2019-07-24 stsp commit_id = NULL;
9741 0ebf8283 2019-07-24 stsp free(line);
9742 0ebf8283 2019-07-24 stsp line = NULL;
9743 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
9744 0ebf8283 2019-07-24 stsp }
9745 0ebf8283 2019-07-24 stsp
9746 0ebf8283 2019-07-24 stsp free(line);
9747 0ebf8283 2019-07-24 stsp free(commit_id);
9748 0ebf8283 2019-07-24 stsp return err;
9749 0ebf8283 2019-07-24 stsp }
9750 0ebf8283 2019-07-24 stsp
9751 0ebf8283 2019-07-24 stsp static const struct got_error *
9752 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
9753 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
9754 bfce7f83 2019-07-27 stsp {
9755 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
9756 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
9757 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
9758 5b87815e 2020-03-05 stsp static char msg[92];
9759 bfce7f83 2019-07-27 stsp char *id_str;
9760 bfce7f83 2019-07-27 stsp
9761 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
9762 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
9763 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
9764 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
9765 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
9766 5b87815e 2020-03-05 stsp
9767 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9768 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
9769 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
9770 5b87815e 2020-03-05 stsp if (hle == hle2)
9771 5b87815e 2020-03-05 stsp continue;
9772 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
9773 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
9774 5b87815e 2020-03-05 stsp continue;
9775 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
9776 5b87815e 2020-03-05 stsp if (err)
9777 5b87815e 2020-03-05 stsp return err;
9778 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
9779 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
9780 5b87815e 2020-03-05 stsp free(id_str);
9781 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9782 5b87815e 2020-03-05 stsp }
9783 5b87815e 2020-03-05 stsp }
9784 bfce7f83 2019-07-27 stsp
9785 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
9786 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9787 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
9788 bfce7f83 2019-07-27 stsp break;
9789 bfce7f83 2019-07-27 stsp }
9790 bfce7f83 2019-07-27 stsp if (hle == NULL) {
9791 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
9792 bfce7f83 2019-07-27 stsp if (err)
9793 bfce7f83 2019-07-27 stsp return err;
9794 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
9795 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
9796 bfce7f83 2019-07-27 stsp free(id_str);
9797 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9798 bfce7f83 2019-07-27 stsp }
9799 bfce7f83 2019-07-27 stsp }
9800 bfce7f83 2019-07-27 stsp
9801 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
9802 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
9803 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
9804 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
9805 bfce7f83 2019-07-27 stsp
9806 bfce7f83 2019-07-27 stsp return NULL;
9807 bfce7f83 2019-07-27 stsp }
9808 bfce7f83 2019-07-27 stsp
9809 bfce7f83 2019-07-27 stsp static const struct got_error *
9810 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
9811 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
9812 bfce7f83 2019-07-27 stsp struct got_repository *repo)
9813 0ebf8283 2019-07-24 stsp {
9814 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9815 0ebf8283 2019-07-24 stsp char *editor;
9816 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9817 0ebf8283 2019-07-24 stsp
9818 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
9819 0ebf8283 2019-07-24 stsp if (err)
9820 0ebf8283 2019-07-24 stsp return err;
9821 0ebf8283 2019-07-24 stsp
9822 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
9823 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
9824 0ebf8283 2019-07-24 stsp goto done;
9825 0ebf8283 2019-07-24 stsp }
9826 0ebf8283 2019-07-24 stsp
9827 c56c5d8a 2021-12-31 thomas f = fopen(path, "re");
9828 0ebf8283 2019-07-24 stsp if (f == NULL) {
9829 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
9830 0ebf8283 2019-07-24 stsp goto done;
9831 0ebf8283 2019-07-24 stsp }
9832 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9833 bfce7f83 2019-07-27 stsp if (err)
9834 bfce7f83 2019-07-27 stsp goto done;
9835 bfce7f83 2019-07-27 stsp
9836 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
9837 0ebf8283 2019-07-24 stsp done:
9838 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9839 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9840 0ebf8283 2019-07-24 stsp free(editor);
9841 0ebf8283 2019-07-24 stsp return err;
9842 0ebf8283 2019-07-24 stsp }
9843 0ebf8283 2019-07-24 stsp
9844 0ebf8283 2019-07-24 stsp static const struct got_error *
9845 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
9846 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
9847 514f2ffe 2020-01-29 stsp struct got_repository *);
9848 0ebf8283 2019-07-24 stsp
9849 0ebf8283 2019-07-24 stsp static const struct got_error *
9850 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
9851 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
9852 c50a7455 2021-10-04 thomas int edit_logmsg_only, int fold_only, int edit_only,
9853 c50a7455 2021-10-04 thomas struct got_repository *repo)
9854 0ebf8283 2019-07-24 stsp {
9855 0ebf8283 2019-07-24 stsp const struct got_error *err;
9856 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9857 0ebf8283 2019-07-24 stsp char *path = NULL;
9858 0ebf8283 2019-07-24 stsp
9859 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
9860 0ebf8283 2019-07-24 stsp if (err)
9861 0ebf8283 2019-07-24 stsp return err;
9862 0ebf8283 2019-07-24 stsp
9863 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
9864 0ebf8283 2019-07-24 stsp if (err)
9865 0ebf8283 2019-07-24 stsp goto done;
9866 0ebf8283 2019-07-24 stsp
9867 466785b9 2020-12-10 jrick err = histedit_write_commit_list(commits, f, edit_logmsg_only,
9868 c50a7455 2021-10-04 thomas fold_only, edit_only, repo);
9869 0ebf8283 2019-07-24 stsp if (err)
9870 0ebf8283 2019-07-24 stsp goto done;
9871 0ebf8283 2019-07-24 stsp
9872 c50a7455 2021-10-04 thomas if (edit_logmsg_only || fold_only || edit_only) {
9873 083957f4 2020-02-24 stsp rewind(f);
9874 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9875 083957f4 2020-02-24 stsp } else {
9876 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF) {
9877 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
9878 0ebf8283 2019-07-24 stsp goto done;
9879 083957f4 2020-02-24 stsp }
9880 083957f4 2020-02-24 stsp f = NULL;
9881 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
9882 083957f4 2020-02-24 stsp if (err) {
9883 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9884 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9885 083957f4 2020-02-24 stsp goto done;
9886 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
9887 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
9888 083957f4 2020-02-24 stsp }
9889 0ebf8283 2019-07-24 stsp }
9890 0ebf8283 2019-07-24 stsp done:
9891 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9892 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9893 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
9894 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
9895 0ebf8283 2019-07-24 stsp free(path);
9896 0ebf8283 2019-07-24 stsp return err;
9897 0ebf8283 2019-07-24 stsp }
9898 0ebf8283 2019-07-24 stsp
9899 0ebf8283 2019-07-24 stsp static const struct got_error *
9900 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
9901 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
9902 0ebf8283 2019-07-24 stsp {
9903 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9904 0ebf8283 2019-07-24 stsp char *path = NULL;
9905 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9906 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
9907 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9908 0ebf8283 2019-07-24 stsp
9909 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
9910 0ebf8283 2019-07-24 stsp if (err)
9911 0ebf8283 2019-07-24 stsp return err;
9912 0ebf8283 2019-07-24 stsp
9913 c56c5d8a 2021-12-31 thomas f = fopen(path, "we");
9914 0ebf8283 2019-07-24 stsp if (f == NULL) {
9915 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
9916 0ebf8283 2019-07-24 stsp goto done;
9917 0ebf8283 2019-07-24 stsp }
9918 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9919 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
9920 0ebf8283 2019-07-24 stsp repo);
9921 0ebf8283 2019-07-24 stsp if (err)
9922 0ebf8283 2019-07-24 stsp break;
9923 0ebf8283 2019-07-24 stsp
9924 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
9925 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
9926 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
9927 0ebf8283 2019-07-24 stsp if (n < 0) {
9928 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9929 0ebf8283 2019-07-24 stsp break;
9930 0ebf8283 2019-07-24 stsp }
9931 0ebf8283 2019-07-24 stsp }
9932 0ebf8283 2019-07-24 stsp }
9933 0ebf8283 2019-07-24 stsp done:
9934 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9935 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9936 0ebf8283 2019-07-24 stsp free(path);
9937 0ebf8283 2019-07-24 stsp if (commit)
9938 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9939 0ebf8283 2019-07-24 stsp return err;
9940 0ebf8283 2019-07-24 stsp }
9941 0ebf8283 2019-07-24 stsp
9942 bfce7f83 2019-07-27 stsp void
9943 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
9944 bfce7f83 2019-07-27 stsp {
9945 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
9946 bfce7f83 2019-07-27 stsp
9947 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
9948 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
9949 bfce7f83 2019-07-27 stsp free(hle);
9950 bfce7f83 2019-07-27 stsp }
9951 bfce7f83 2019-07-27 stsp }
9952 bfce7f83 2019-07-27 stsp
9953 0ebf8283 2019-07-24 stsp static const struct got_error *
9954 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
9955 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
9956 0ebf8283 2019-07-24 stsp {
9957 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9958 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9959 0ebf8283 2019-07-24 stsp
9960 c56c5d8a 2021-12-31 thomas f = fopen(path, "re");
9961 0ebf8283 2019-07-24 stsp if (f == NULL) {
9962 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
9963 0ebf8283 2019-07-24 stsp goto done;
9964 0ebf8283 2019-07-24 stsp }
9965 0ebf8283 2019-07-24 stsp
9966 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9967 0ebf8283 2019-07-24 stsp done:
9968 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9969 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9970 0ebf8283 2019-07-24 stsp return err;
9971 0ebf8283 2019-07-24 stsp }
9972 0ebf8283 2019-07-24 stsp
9973 0ebf8283 2019-07-24 stsp static const struct got_error *
9974 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
9975 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
9976 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
9977 0ebf8283 2019-07-24 stsp {
9978 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
9979 0ebf8283 2019-07-24 stsp int resp = ' ';
9980 0ebf8283 2019-07-24 stsp
9981 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
9982 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
9983 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
9984 0ebf8283 2019-07-24 stsp resp = getchar();
9985 a61a4414 2019-08-07 stsp if (resp == '\n')
9986 a61a4414 2019-08-07 stsp resp = getchar();
9987 426ebf2e 2019-07-25 stsp if (resp == 'c') {
9988 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
9989 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
9990 bfce7f83 2019-07-27 stsp repo);
9991 426ebf2e 2019-07-25 stsp if (err) {
9992 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9993 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9994 426ebf2e 2019-07-25 stsp break;
9995 bfce7f83 2019-07-27 stsp prev_err = err;
9996 426ebf2e 2019-07-25 stsp resp = ' ';
9997 426ebf2e 2019-07-25 stsp continue;
9998 426ebf2e 2019-07-25 stsp }
9999 bfce7f83 2019-07-27 stsp break;
10000 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
10001 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
10002 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
10003 c50a7455 2021-10-04 thomas commits, branch_name, 0, 0, 0, repo);
10004 426ebf2e 2019-07-25 stsp if (err) {
10005 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10006 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
10007 426ebf2e 2019-07-25 stsp break;
10008 bfce7f83 2019-07-27 stsp prev_err = err;
10009 426ebf2e 2019-07-25 stsp resp = ' ';
10010 426ebf2e 2019-07-25 stsp continue;
10011 426ebf2e 2019-07-25 stsp }
10012 bfce7f83 2019-07-27 stsp break;
10013 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
10014 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
10015 0ebf8283 2019-07-24 stsp break;
10016 426ebf2e 2019-07-25 stsp } else
10017 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
10018 0ebf8283 2019-07-24 stsp }
10019 0ebf8283 2019-07-24 stsp
10020 0ebf8283 2019-07-24 stsp return err;
10021 0ebf8283 2019-07-24 stsp }
10022 0ebf8283 2019-07-24 stsp
10023 0ebf8283 2019-07-24 stsp static const struct got_error *
10024 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
10025 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
10026 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
10027 0ebf8283 2019-07-24 stsp {
10028 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
10029 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10030 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
10031 3e3a69f1 2019-07-25 stsp branch, repo);
10032 0ebf8283 2019-07-24 stsp }
10033 0ebf8283 2019-07-24 stsp
10034 0ebf8283 2019-07-24 stsp static const struct got_error *
10035 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
10036 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
10037 0ebf8283 2019-07-24 stsp {
10038 0ebf8283 2019-07-24 stsp const struct got_error *err;
10039 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10040 0ebf8283 2019-07-24 stsp
10041 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
10042 0ebf8283 2019-07-24 stsp if (err)
10043 0ebf8283 2019-07-24 stsp goto done;
10044 0ebf8283 2019-07-24 stsp
10045 0ebf8283 2019-07-24 stsp if (new_id) {
10046 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
10047 0ebf8283 2019-07-24 stsp if (err)
10048 0ebf8283 2019-07-24 stsp goto done;
10049 0ebf8283 2019-07-24 stsp }
10050 0ebf8283 2019-07-24 stsp
10051 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
10052 0ebf8283 2019-07-24 stsp if (new_id_str)
10053 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
10054 0ebf8283 2019-07-24 stsp
10055 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
10056 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
10057 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
10058 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
10059 0ebf8283 2019-07-24 stsp goto done;
10060 0ebf8283 2019-07-24 stsp }
10061 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
10062 0ebf8283 2019-07-24 stsp } else {
10063 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
10064 0ebf8283 2019-07-24 stsp if (err)
10065 0ebf8283 2019-07-24 stsp goto done;
10066 0ebf8283 2019-07-24 stsp }
10067 0ebf8283 2019-07-24 stsp
10068 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
10069 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
10070 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
10071 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
10072 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
10073 0ebf8283 2019-07-24 stsp break;
10074 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
10075 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
10076 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
10077 0ebf8283 2019-07-24 stsp logmsg);
10078 0ebf8283 2019-07-24 stsp break;
10079 0ebf8283 2019-07-24 stsp default:
10080 0ebf8283 2019-07-24 stsp break;
10081 0ebf8283 2019-07-24 stsp }
10082 0ebf8283 2019-07-24 stsp done:
10083 0ebf8283 2019-07-24 stsp free(old_id_str);
10084 0ebf8283 2019-07-24 stsp free(new_id_str);
10085 0ebf8283 2019-07-24 stsp return err;
10086 0ebf8283 2019-07-24 stsp }
10087 0ebf8283 2019-07-24 stsp
10088 0ebf8283 2019-07-24 stsp static const struct got_error *
10089 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
10090 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
10091 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
10092 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
10093 0ebf8283 2019-07-24 stsp {
10094 0ebf8283 2019-07-24 stsp const struct got_error *err;
10095 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
10096 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
10097 0ebf8283 2019-07-24 stsp
10098 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
10099 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
10100 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
10101 0ebf8283 2019-07-24 stsp if (err)
10102 0ebf8283 2019-07-24 stsp return err;
10103 0ebf8283 2019-07-24 stsp }
10104 0ebf8283 2019-07-24 stsp
10105 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10106 0ebf8283 2019-07-24 stsp if (err)
10107 0ebf8283 2019-07-24 stsp return err;
10108 0ebf8283 2019-07-24 stsp
10109 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
10110 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
10111 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
10112 0ebf8283 2019-07-24 stsp if (err) {
10113 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
10114 0ebf8283 2019-07-24 stsp goto done;
10115 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
10116 0ebf8283 2019-07-24 stsp } else {
10117 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
10118 0ebf8283 2019-07-24 stsp free(new_commit_id);
10119 0ebf8283 2019-07-24 stsp }
10120 0ebf8283 2019-07-24 stsp done:
10121 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10122 0ebf8283 2019-07-24 stsp return err;
10123 0ebf8283 2019-07-24 stsp }
10124 0ebf8283 2019-07-24 stsp
10125 0ebf8283 2019-07-24 stsp static const struct got_error *
10126 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
10127 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
10128 0ebf8283 2019-07-24 stsp {
10129 0ebf8283 2019-07-24 stsp const struct got_error *error;
10130 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
10131 0ebf8283 2019-07-24 stsp
10132 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
10133 0ebf8283 2019-07-24 stsp repo);
10134 0ebf8283 2019-07-24 stsp if (error)
10135 0ebf8283 2019-07-24 stsp return error;
10136 0ebf8283 2019-07-24 stsp
10137 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
10138 0ebf8283 2019-07-24 stsp if (error)
10139 0ebf8283 2019-07-24 stsp return error;
10140 0ebf8283 2019-07-24 stsp
10141 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
10142 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10143 0ebf8283 2019-07-24 stsp return error;
10144 ab20a43a 2020-01-29 stsp }
10145 ab20a43a 2020-01-29 stsp
10146 ab20a43a 2020-01-29 stsp static const struct got_error *
10147 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
10148 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
10149 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
10150 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
10151 ab20a43a 2020-01-29 stsp {
10152 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
10153 ab20a43a 2020-01-29 stsp
10154 ab20a43a 2020-01-29 stsp switch (status) {
10155 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
10156 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
10157 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
10158 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
10159 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
10160 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
10161 ab20a43a 2020-01-29 stsp default:
10162 ab20a43a 2020-01-29 stsp break;
10163 ab20a43a 2020-01-29 stsp }
10164 ab20a43a 2020-01-29 stsp
10165 ab20a43a 2020-01-29 stsp switch (staged_status) {
10166 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
10167 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
10168 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
10169 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
10170 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
10171 ab20a43a 2020-01-29 stsp default:
10172 ab20a43a 2020-01-29 stsp break;
10173 ab20a43a 2020-01-29 stsp }
10174 ab20a43a 2020-01-29 stsp
10175 ab20a43a 2020-01-29 stsp return NULL;
10176 0ebf8283 2019-07-24 stsp }
10177 0ebf8283 2019-07-24 stsp
10178 0ebf8283 2019-07-24 stsp static const struct got_error *
10179 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
10180 0ebf8283 2019-07-24 stsp {
10181 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
10182 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
10183 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
10184 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
10185 0ebf8283 2019-07-24 stsp char *cwd = NULL;
10186 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
10187 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
10188 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
10189 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
10190 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
10191 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
10192 10604dce 2021-09-24 thomas int ch, rebase_in_progress = 0, merge_in_progress = 0;
10193 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10194 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
10195 c50a7455 2021-10-04 thomas int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
10196 643b85bc 2021-07-16 stsp int list_backups = 0, delete_backups = 0;
10197 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
10198 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
10199 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
10200 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
10201 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
10202 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
10203 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
10204 0ebf8283 2019-07-24 stsp
10205 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
10206 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
10207 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
10208 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10209 0ebf8283 2019-07-24 stsp
10210 c50a7455 2021-10-04 thomas while ((ch = getopt(argc, argv, "acefF:mlX")) != -1) {
10211 0ebf8283 2019-07-24 stsp switch (ch) {
10212 0ebf8283 2019-07-24 stsp case 'a':
10213 0ebf8283 2019-07-24 stsp abort_edit = 1;
10214 0ebf8283 2019-07-24 stsp break;
10215 0ebf8283 2019-07-24 stsp case 'c':
10216 0ebf8283 2019-07-24 stsp continue_edit = 1;
10217 0ebf8283 2019-07-24 stsp break;
10218 c50a7455 2021-10-04 thomas case 'e':
10219 c50a7455 2021-10-04 thomas edit_only = 1;
10220 c50a7455 2021-10-04 thomas break;
10221 466785b9 2020-12-10 jrick case 'f':
10222 466785b9 2020-12-10 jrick fold_only = 1;
10223 466785b9 2020-12-10 jrick break;
10224 0ebf8283 2019-07-24 stsp case 'F':
10225 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
10226 0ebf8283 2019-07-24 stsp break;
10227 083957f4 2020-02-24 stsp case 'm':
10228 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
10229 083957f4 2020-02-24 stsp break;
10230 e600f124 2021-03-21 stsp case 'l':
10231 e600f124 2021-03-21 stsp list_backups = 1;
10232 e600f124 2021-03-21 stsp break;
10233 643b85bc 2021-07-16 stsp case 'X':
10234 643b85bc 2021-07-16 stsp delete_backups = 1;
10235 643b85bc 2021-07-16 stsp break;
10236 0ebf8283 2019-07-24 stsp default:
10237 0ebf8283 2019-07-24 stsp usage_histedit();
10238 0ebf8283 2019-07-24 stsp /* NOTREACHED */
10239 0ebf8283 2019-07-24 stsp }
10240 0ebf8283 2019-07-24 stsp }
10241 0ebf8283 2019-07-24 stsp
10242 0ebf8283 2019-07-24 stsp argc -= optind;
10243 0ebf8283 2019-07-24 stsp argv += optind;
10244 0ebf8283 2019-07-24 stsp
10245 0ebf8283 2019-07-24 stsp #ifndef PROFILE
10246 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10247 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
10248 0ebf8283 2019-07-24 stsp err(1, "pledge");
10249 0ebf8283 2019-07-24 stsp #endif
10250 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
10251 ff69268e 2020-12-13 stsp option_conflict('a', 'c');
10252 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
10253 ff69268e 2020-12-13 stsp option_conflict('F', 'm');
10254 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
10255 ff69268e 2020-12-13 stsp option_conflict('a', 'm');
10256 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
10257 ff69268e 2020-12-13 stsp option_conflict('c', 'm');
10258 466785b9 2020-12-10 jrick if (abort_edit && fold_only)
10259 ff69268e 2020-12-13 stsp option_conflict('a', 'f');
10260 ff69268e 2020-12-13 stsp if (continue_edit && fold_only)
10261 ff69268e 2020-12-13 stsp option_conflict('c', 'f');
10262 466785b9 2020-12-10 jrick if (fold_only && edit_logmsg_only)
10263 ff69268e 2020-12-13 stsp option_conflict('f', 'm');
10264 b3805337 2020-12-13 stsp if (edit_script_path && fold_only)
10265 b3805337 2020-12-13 stsp option_conflict('F', 'f');
10266 c50a7455 2021-10-04 thomas if (abort_edit && edit_only)
10267 c50a7455 2021-10-04 thomas option_conflict('a', 'e');
10268 c50a7455 2021-10-04 thomas if (continue_edit && edit_only)
10269 c50a7455 2021-10-04 thomas option_conflict('c', 'e');
10270 c50a7455 2021-10-04 thomas if (edit_only && edit_logmsg_only)
10271 c50a7455 2021-10-04 thomas option_conflict('e', 'm');
10272 c50a7455 2021-10-04 thomas if (edit_script_path && edit_only)
10273 c50a7455 2021-10-04 thomas option_conflict('F', 'e');
10274 e600f124 2021-03-21 stsp if (list_backups) {
10275 e600f124 2021-03-21 stsp if (abort_edit)
10276 e600f124 2021-03-21 stsp option_conflict('l', 'a');
10277 e600f124 2021-03-21 stsp if (continue_edit)
10278 e600f124 2021-03-21 stsp option_conflict('l', 'c');
10279 e600f124 2021-03-21 stsp if (edit_script_path)
10280 e600f124 2021-03-21 stsp option_conflict('l', 'F');
10281 e600f124 2021-03-21 stsp if (edit_logmsg_only)
10282 e600f124 2021-03-21 stsp option_conflict('l', 'm');
10283 e600f124 2021-03-21 stsp if (fold_only)
10284 e600f124 2021-03-21 stsp option_conflict('l', 'f');
10285 c50a7455 2021-10-04 thomas if (edit_only)
10286 c50a7455 2021-10-04 thomas option_conflict('l', 'e');
10287 643b85bc 2021-07-16 stsp if (delete_backups)
10288 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
10289 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
10290 e600f124 2021-03-21 stsp usage_histedit();
10291 643b85bc 2021-07-16 stsp } else if (delete_backups) {
10292 643b85bc 2021-07-16 stsp if (abort_edit)
10293 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
10294 643b85bc 2021-07-16 stsp if (continue_edit)
10295 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
10296 643b85bc 2021-07-16 stsp if (edit_script_path)
10297 643b85bc 2021-07-16 stsp option_conflict('X', 'F');
10298 643b85bc 2021-07-16 stsp if (edit_logmsg_only)
10299 643b85bc 2021-07-16 stsp option_conflict('X', 'm');
10300 643b85bc 2021-07-16 stsp if (fold_only)
10301 643b85bc 2021-07-16 stsp option_conflict('X', 'f');
10302 c50a7455 2021-10-04 thomas if (edit_only)
10303 c50a7455 2021-10-04 thomas option_conflict('X', 'e');
10304 643b85bc 2021-07-16 stsp if (list_backups)
10305 643b85bc 2021-07-16 stsp option_conflict('X', 'l');
10306 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
10307 643b85bc 2021-07-16 stsp usage_histedit();
10308 e600f124 2021-03-21 stsp } else if (argc != 0)
10309 0ebf8283 2019-07-24 stsp usage_histedit();
10310 0ebf8283 2019-07-24 stsp
10311 0ebf8283 2019-07-24 stsp /*
10312 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
10313 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
10314 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
10315 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
10316 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
10317 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
10318 0ebf8283 2019-07-24 stsp */
10319 0ebf8283 2019-07-24 stsp
10320 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
10321 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
10322 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
10323 0ebf8283 2019-07-24 stsp goto done;
10324 0ebf8283 2019-07-24 stsp }
10325 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
10326 fa51e947 2020-03-27 stsp if (error) {
10327 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
10328 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
10329 e600f124 2021-03-21 stsp goto done;
10330 e600f124 2021-03-21 stsp } else {
10331 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10332 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
10333 e600f124 2021-03-21 stsp "histedit", cwd);
10334 e600f124 2021-03-21 stsp goto done;
10335 e600f124 2021-03-21 stsp }
10336 e600f124 2021-03-21 stsp }
10337 e600f124 2021-03-21 stsp
10338 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
10339 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
10340 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd,
10341 e600f124 2021-03-21 stsp NULL);
10342 e600f124 2021-03-21 stsp if (error != NULL)
10343 e600f124 2021-03-21 stsp goto done;
10344 e600f124 2021-03-21 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10345 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
10346 e600f124 2021-03-21 stsp if (error)
10347 e600f124 2021-03-21 stsp goto done;
10348 643b85bc 2021-07-16 stsp error = process_backup_refs(
10349 e600f124 2021-03-21 stsp GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
10350 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
10351 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
10352 fa51e947 2020-03-27 stsp }
10353 0ebf8283 2019-07-24 stsp
10354 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10355 c9956ddf 2019-09-08 stsp NULL);
10356 0ebf8283 2019-07-24 stsp if (error != NULL)
10357 0ebf8283 2019-07-24 stsp goto done;
10358 0ebf8283 2019-07-24 stsp
10359 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10360 0ebf8283 2019-07-24 stsp if (error)
10361 0ebf8283 2019-07-24 stsp goto done;
10362 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
10363 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
10364 10604dce 2021-09-24 thomas goto done;
10365 10604dce 2021-09-24 thomas }
10366 10604dce 2021-09-24 thomas
10367 10604dce 2021-09-24 thomas error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10368 10604dce 2021-09-24 thomas repo);
10369 10604dce 2021-09-24 thomas if (error)
10370 10604dce 2021-09-24 thomas goto done;
10371 10604dce 2021-09-24 thomas if (merge_in_progress) {
10372 10604dce 2021-09-24 thomas error = got_error(GOT_ERR_MERGE_BUSY);
10373 0ebf8283 2019-07-24 stsp goto done;
10374 0ebf8283 2019-07-24 stsp }
10375 0ebf8283 2019-07-24 stsp
10376 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
10377 0ebf8283 2019-07-24 stsp if (error)
10378 0ebf8283 2019-07-24 stsp goto done;
10379 0ebf8283 2019-07-24 stsp
10380 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
10381 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10382 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
10383 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
10384 083957f4 2020-02-24 stsp "before the -m option can be used");
10385 083957f4 2020-02-24 stsp goto done;
10386 083957f4 2020-02-24 stsp }
10387 466785b9 2020-12-10 jrick if (edit_in_progress && fold_only) {
10388 466785b9 2020-12-10 jrick error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10389 466785b9 2020-12-10 jrick "histedit operation is in progress in this "
10390 466785b9 2020-12-10 jrick "work tree and must be continued or aborted "
10391 466785b9 2020-12-10 jrick "before the -f option can be used");
10392 466785b9 2020-12-10 jrick goto done;
10393 466785b9 2020-12-10 jrick }
10394 c50a7455 2021-10-04 thomas if (edit_in_progress && edit_only) {
10395 c50a7455 2021-10-04 thomas error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10396 c50a7455 2021-10-04 thomas "histedit operation is in progress in this "
10397 c50a7455 2021-10-04 thomas "work tree and must be continued or aborted "
10398 c50a7455 2021-10-04 thomas "before the -e option can be used");
10399 c50a7455 2021-10-04 thomas goto done;
10400 c50a7455 2021-10-04 thomas }
10401 083957f4 2020-02-24 stsp
10402 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
10403 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
10404 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
10405 3e3a69f1 2019-07-25 stsp worktree, repo);
10406 0ebf8283 2019-07-24 stsp if (error)
10407 0ebf8283 2019-07-24 stsp goto done;
10408 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
10409 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10410 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
10411 d52bac28 2021-10-08 thomas branch, base_commit_id, abort_progress, &upa);
10412 0ebf8283 2019-07-24 stsp if (error)
10413 0ebf8283 2019-07-24 stsp goto done;
10414 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
10415 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10416 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
10417 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
10418 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
10419 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
10420 0ebf8283 2019-07-24 stsp goto done;
10421 0ebf8283 2019-07-24 stsp }
10422 0ebf8283 2019-07-24 stsp
10423 0ebf8283 2019-07-24 stsp if (continue_edit) {
10424 0ebf8283 2019-07-24 stsp char *path;
10425 0ebf8283 2019-07-24 stsp
10426 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
10427 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
10428 0ebf8283 2019-07-24 stsp goto done;
10429 0ebf8283 2019-07-24 stsp }
10430 0ebf8283 2019-07-24 stsp
10431 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
10432 0ebf8283 2019-07-24 stsp if (error)
10433 0ebf8283 2019-07-24 stsp goto done;
10434 0ebf8283 2019-07-24 stsp
10435 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
10436 0ebf8283 2019-07-24 stsp free(path);
10437 0ebf8283 2019-07-24 stsp if (error)
10438 0ebf8283 2019-07-24 stsp goto done;
10439 0ebf8283 2019-07-24 stsp
10440 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
10441 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
10442 3e3a69f1 2019-07-25 stsp worktree, repo);
10443 0ebf8283 2019-07-24 stsp if (error)
10444 0ebf8283 2019-07-24 stsp goto done;
10445 0ebf8283 2019-07-24 stsp
10446 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
10447 0ebf8283 2019-07-24 stsp if (error)
10448 0ebf8283 2019-07-24 stsp goto done;
10449 0ebf8283 2019-07-24 stsp
10450 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10451 0ebf8283 2019-07-24 stsp head_commit_id);
10452 0ebf8283 2019-07-24 stsp if (error)
10453 0ebf8283 2019-07-24 stsp goto done;
10454 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10455 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10456 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
10457 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10458 3aac7cf7 2019-07-25 stsp goto done;
10459 3aac7cf7 2019-07-25 stsp }
10460 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
10461 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
10462 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
10463 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10464 0ebf8283 2019-07-24 stsp commit = NULL;
10465 0ebf8283 2019-07-24 stsp if (error)
10466 0ebf8283 2019-07-24 stsp goto done;
10467 0ebf8283 2019-07-24 stsp } else {
10468 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
10469 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
10470 0ebf8283 2019-07-24 stsp goto done;
10471 0ebf8283 2019-07-24 stsp }
10472 0ebf8283 2019-07-24 stsp
10473 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
10474 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
10475 0ebf8283 2019-07-24 stsp if (error != NULL)
10476 0ebf8283 2019-07-24 stsp goto done;
10477 0ebf8283 2019-07-24 stsp
10478 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10479 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10480 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
10481 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
10482 c7d20a3f 2019-07-30 stsp goto done;
10483 c7d20a3f 2019-07-30 stsp }
10484 c7d20a3f 2019-07-30 stsp
10485 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
10486 bfce7f83 2019-07-27 stsp got_ref_close(branch);
10487 bfce7f83 2019-07-27 stsp branch = NULL;
10488 0ebf8283 2019-07-24 stsp if (error)
10489 0ebf8283 2019-07-24 stsp goto done;
10490 0ebf8283 2019-07-24 stsp
10491 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10492 0ebf8283 2019-07-24 stsp head_commit_id);
10493 0ebf8283 2019-07-24 stsp if (error)
10494 0ebf8283 2019-07-24 stsp goto done;
10495 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10496 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10497 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
10498 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10499 3aac7cf7 2019-07-25 stsp goto done;
10500 3aac7cf7 2019-07-25 stsp }
10501 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
10502 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
10503 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
10504 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
10505 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10506 0ebf8283 2019-07-24 stsp commit = NULL;
10507 0ebf8283 2019-07-24 stsp if (error)
10508 0ebf8283 2019-07-24 stsp goto done;
10509 0ebf8283 2019-07-24 stsp
10510 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
10511 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10512 c5996fff 2020-01-29 stsp goto done;
10513 c5996fff 2020-01-29 stsp }
10514 c5996fff 2020-01-29 stsp
10515 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
10516 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
10517 bfce7f83 2019-07-27 stsp if (error)
10518 bfce7f83 2019-07-27 stsp goto done;
10519 bfce7f83 2019-07-27 stsp
10520 0ebf8283 2019-07-24 stsp if (edit_script_path) {
10521 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
10522 0ebf8283 2019-07-24 stsp edit_script_path, repo);
10523 6ba2bea2 2019-07-27 stsp if (error) {
10524 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10525 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10526 d52bac28 2021-10-08 thomas abort_progress, &upa);
10527 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
10528 0ebf8283 2019-07-24 stsp goto done;
10529 6ba2bea2 2019-07-27 stsp }
10530 0ebf8283 2019-07-24 stsp } else {
10531 514f2ffe 2020-01-29 stsp const char *branch_name;
10532 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
10533 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
10534 514f2ffe 2020-01-29 stsp branch_name += 11;
10535 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
10536 c50a7455 2021-10-04 thomas branch_name, edit_logmsg_only, fold_only,
10537 c50a7455 2021-10-04 thomas edit_only, repo);
10538 6ba2bea2 2019-07-27 stsp if (error) {
10539 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10540 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10541 d52bac28 2021-10-08 thomas abort_progress, &upa);
10542 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
10543 0ebf8283 2019-07-24 stsp goto done;
10544 6ba2bea2 2019-07-27 stsp }
10545 0ebf8283 2019-07-24 stsp
10546 0ebf8283 2019-07-24 stsp }
10547 0ebf8283 2019-07-24 stsp
10548 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
10549 0ebf8283 2019-07-24 stsp repo);
10550 6ba2bea2 2019-07-27 stsp if (error) {
10551 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10552 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10553 d52bac28 2021-10-08 thomas abort_progress, &upa);
10554 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
10555 0ebf8283 2019-07-24 stsp goto done;
10556 6ba2bea2 2019-07-27 stsp }
10557 0ebf8283 2019-07-24 stsp
10558 0ebf8283 2019-07-24 stsp }
10559 0ebf8283 2019-07-24 stsp
10560 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
10561 4ec14e60 2019-08-28 hiltjo if (error)
10562 0ebf8283 2019-07-24 stsp goto done;
10563 0ebf8283 2019-07-24 stsp
10564 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
10565 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
10566 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
10567 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
10568 0ebf8283 2019-07-24 stsp continue;
10569 0ebf8283 2019-07-24 stsp
10570 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
10571 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
10572 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
10573 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
10574 62d463ca 2020-10-20 naddy repo);
10575 ab20a43a 2020-01-29 stsp if (error)
10576 ab20a43a 2020-01-29 stsp goto done;
10577 0ebf8283 2019-07-24 stsp } else {
10578 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
10579 ab20a43a 2020-01-29 stsp int have_changes = 0;
10580 ab20a43a 2020-01-29 stsp
10581 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
10582 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
10583 ab20a43a 2020-01-29 stsp if (error)
10584 ab20a43a 2020-01-29 stsp goto done;
10585 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
10586 f6343036 2021-06-22 stsp repo, 0, check_local_changes, &have_changes,
10587 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
10588 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
10589 ab20a43a 2020-01-29 stsp if (error) {
10590 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
10591 ab20a43a 2020-01-29 stsp goto done;
10592 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
10593 ab20a43a 2020-01-29 stsp goto done;
10594 ab20a43a 2020-01-29 stsp }
10595 ab20a43a 2020-01-29 stsp if (have_changes) {
10596 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
10597 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
10598 ab20a43a 2020-01-29 stsp if (error)
10599 ab20a43a 2020-01-29 stsp goto done;
10600 ab20a43a 2020-01-29 stsp } else {
10601 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
10602 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
10603 ab20a43a 2020-01-29 stsp if (error)
10604 ab20a43a 2020-01-29 stsp goto done;
10605 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
10606 ab20a43a 2020-01-29 stsp hle, NULL);
10607 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
10608 ab20a43a 2020-01-29 stsp commit = NULL;
10609 ab20a43a 2020-01-29 stsp if (error)
10610 ab20a43a 2020-01-29 stsp goto done;
10611 ab20a43a 2020-01-29 stsp }
10612 0ebf8283 2019-07-24 stsp }
10613 0ebf8283 2019-07-24 stsp continue;
10614 0ebf8283 2019-07-24 stsp }
10615 0ebf8283 2019-07-24 stsp
10616 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
10617 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
10618 0ebf8283 2019-07-24 stsp if (error)
10619 0ebf8283 2019-07-24 stsp goto done;
10620 0ebf8283 2019-07-24 stsp continue;
10621 0ebf8283 2019-07-24 stsp }
10622 0ebf8283 2019-07-24 stsp
10623 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10624 0ebf8283 2019-07-24 stsp hle->commit_id);
10625 0ebf8283 2019-07-24 stsp if (error)
10626 0ebf8283 2019-07-24 stsp goto done;
10627 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10628 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10629 0ebf8283 2019-07-24 stsp
10630 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
10631 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
10632 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
10633 0ebf8283 2019-07-24 stsp if (error)
10634 0ebf8283 2019-07-24 stsp goto done;
10635 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10636 0ebf8283 2019-07-24 stsp commit = NULL;
10637 0ebf8283 2019-07-24 stsp
10638 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
10639 dddbe150 2021-09-28 thomas if (upa.conflicts > 0 || upa.missing > 0 ||
10640 dddbe150 2021-09-28 thomas upa.not_deleted > 0 || upa.unversioned > 0) {
10641 dddbe150 2021-09-28 thomas if (upa.conflicts > 0) {
10642 dddbe150 2021-09-28 thomas error = show_rebase_merge_conflict(
10643 dddbe150 2021-09-28 thomas hle->commit_id, repo);
10644 dddbe150 2021-09-28 thomas if (error)
10645 dddbe150 2021-09-28 thomas goto done;
10646 dddbe150 2021-09-28 thomas }
10647 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10648 0ebf8283 2019-07-24 stsp break;
10649 0ebf8283 2019-07-24 stsp }
10650 0ebf8283 2019-07-24 stsp
10651 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
10652 0ebf8283 2019-07-24 stsp char *id_str;
10653 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
10654 0ebf8283 2019-07-24 stsp if (error)
10655 0ebf8283 2019-07-24 stsp goto done;
10656 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
10657 0ebf8283 2019-07-24 stsp id_str);
10658 0ebf8283 2019-07-24 stsp free(id_str);
10659 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10660 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
10661 3e3a69f1 2019-07-25 stsp fileindex);
10662 0ebf8283 2019-07-24 stsp goto done;
10663 0ebf8283 2019-07-24 stsp }
10664 0ebf8283 2019-07-24 stsp
10665 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
10666 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
10667 0ebf8283 2019-07-24 stsp if (error)
10668 0ebf8283 2019-07-24 stsp goto done;
10669 0ebf8283 2019-07-24 stsp continue;
10670 0ebf8283 2019-07-24 stsp }
10671 0ebf8283 2019-07-24 stsp
10672 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
10673 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
10674 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10675 0ebf8283 2019-07-24 stsp if (error)
10676 0ebf8283 2019-07-24 stsp goto done;
10677 0ebf8283 2019-07-24 stsp }
10678 0ebf8283 2019-07-24 stsp
10679 dddbe150 2021-09-28 thomas if (upa.conflicts > 0 || upa.missing > 0 ||
10680 dddbe150 2021-09-28 thomas upa.not_deleted > 0 || upa.unversioned > 0) {
10681 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
10682 0ebf8283 2019-07-24 stsp if (error)
10683 0ebf8283 2019-07-24 stsp goto done;
10684 dddbe150 2021-09-28 thomas if (upa.conflicts > 0 && upa.missing == 0 &&
10685 dddbe150 2021-09-28 thomas upa.not_deleted == 0 && upa.unversioned == 0) {
10686 dddbe150 2021-09-28 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
10687 dddbe150 2021-09-28 thomas "conflicts must be resolved before histedit "
10688 dddbe150 2021-09-28 thomas "can continue");
10689 dddbe150 2021-09-28 thomas } else if (upa.conflicts > 0) {
10690 dddbe150 2021-09-28 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
10691 dddbe150 2021-09-28 thomas "conflicts must be resolved before histedit "
10692 dddbe150 2021-09-28 thomas "can continue; changes destined for some "
10693 dddbe150 2021-09-28 thomas "files were not yet merged and should be "
10694 dddbe150 2021-09-28 thomas "merged manually if required before the "
10695 dddbe150 2021-09-28 thomas "histedit operation is continued");
10696 dddbe150 2021-09-28 thomas } else {
10697 dddbe150 2021-09-28 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
10698 dddbe150 2021-09-28 thomas "changes destined for some files were not "
10699 dddbe150 2021-09-28 thomas "yet merged and should be merged manually "
10700 dddbe150 2021-09-28 thomas "if required before the histedit operation "
10701 dddbe150 2021-09-28 thomas "is continued");
10702 dddbe150 2021-09-28 thomas }
10703 0ebf8283 2019-07-24 stsp } else
10704 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
10705 3e3a69f1 2019-07-25 stsp branch, repo);
10706 0ebf8283 2019-07-24 stsp done:
10707 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
10708 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
10709 0ebf8283 2019-07-24 stsp free(head_commit_id);
10710 0ebf8283 2019-07-24 stsp free(base_commit_id);
10711 0ebf8283 2019-07-24 stsp free(resume_commit_id);
10712 0ebf8283 2019-07-24 stsp if (commit)
10713 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10714 0ebf8283 2019-07-24 stsp if (branch)
10715 0ebf8283 2019-07-24 stsp got_ref_close(branch);
10716 0ebf8283 2019-07-24 stsp if (tmp_branch)
10717 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
10718 0ebf8283 2019-07-24 stsp if (worktree)
10719 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
10720 1d0f4054 2021-06-17 stsp if (repo) {
10721 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10722 1d0f4054 2021-06-17 stsp if (error == NULL)
10723 1d0f4054 2021-06-17 stsp error = close_err;
10724 1d0f4054 2021-06-17 stsp }
10725 2822a352 2019-10-15 stsp return error;
10726 2822a352 2019-10-15 stsp }
10727 2822a352 2019-10-15 stsp
10728 2822a352 2019-10-15 stsp __dead static void
10729 2822a352 2019-10-15 stsp usage_integrate(void)
10730 2822a352 2019-10-15 stsp {
10731 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
10732 2822a352 2019-10-15 stsp exit(1);
10733 2822a352 2019-10-15 stsp }
10734 2822a352 2019-10-15 stsp
10735 2822a352 2019-10-15 stsp static const struct got_error *
10736 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
10737 2822a352 2019-10-15 stsp {
10738 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
10739 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
10740 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
10741 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
10742 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
10743 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
10744 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
10745 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
10746 9627c110 2020-04-18 stsp int ch;
10747 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10748 2822a352 2019-10-15 stsp
10749 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
10750 2822a352 2019-10-15 stsp switch (ch) {
10751 2822a352 2019-10-15 stsp default:
10752 2822a352 2019-10-15 stsp usage_integrate();
10753 2822a352 2019-10-15 stsp /* NOTREACHED */
10754 2822a352 2019-10-15 stsp }
10755 2822a352 2019-10-15 stsp }
10756 2822a352 2019-10-15 stsp
10757 2822a352 2019-10-15 stsp argc -= optind;
10758 2822a352 2019-10-15 stsp argv += optind;
10759 2822a352 2019-10-15 stsp
10760 2822a352 2019-10-15 stsp if (argc != 1)
10761 2822a352 2019-10-15 stsp usage_integrate();
10762 2822a352 2019-10-15 stsp branch_arg = argv[0];
10763 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
10764 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10765 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
10766 2822a352 2019-10-15 stsp err(1, "pledge");
10767 b08a0ccd 2020-09-24 stsp #endif
10768 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
10769 2822a352 2019-10-15 stsp if (cwd == NULL) {
10770 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
10771 2822a352 2019-10-15 stsp goto done;
10772 2822a352 2019-10-15 stsp }
10773 2822a352 2019-10-15 stsp
10774 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
10775 fa51e947 2020-03-27 stsp if (error) {
10776 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10777 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
10778 fa51e947 2020-03-27 stsp cwd);
10779 2822a352 2019-10-15 stsp goto done;
10780 fa51e947 2020-03-27 stsp }
10781 2822a352 2019-10-15 stsp
10782 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
10783 2822a352 2019-10-15 stsp if (error)
10784 2822a352 2019-10-15 stsp goto done;
10785 2822a352 2019-10-15 stsp
10786 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10787 2822a352 2019-10-15 stsp NULL);
10788 2822a352 2019-10-15 stsp if (error != NULL)
10789 2822a352 2019-10-15 stsp goto done;
10790 2822a352 2019-10-15 stsp
10791 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10792 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
10793 2822a352 2019-10-15 stsp if (error)
10794 2822a352 2019-10-15 stsp goto done;
10795 2822a352 2019-10-15 stsp
10796 10604dce 2021-09-24 thomas error = check_merge_in_progress(worktree, repo);
10797 10604dce 2021-09-24 thomas if (error)
10798 10604dce 2021-09-24 thomas goto done;
10799 10604dce 2021-09-24 thomas
10800 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
10801 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
10802 2822a352 2019-10-15 stsp goto done;
10803 2822a352 2019-10-15 stsp }
10804 2822a352 2019-10-15 stsp
10805 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
10806 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
10807 2822a352 2019-10-15 stsp if (error)
10808 2822a352 2019-10-15 stsp goto done;
10809 2822a352 2019-10-15 stsp
10810 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
10811 2822a352 2019-10-15 stsp if (refname == NULL) {
10812 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
10813 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10814 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10815 2822a352 2019-10-15 stsp goto done;
10816 2822a352 2019-10-15 stsp }
10817 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
10818 2822a352 2019-10-15 stsp if (base_refname == NULL) {
10819 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
10820 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10821 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10822 2822a352 2019-10-15 stsp goto done;
10823 2822a352 2019-10-15 stsp }
10824 2822a352 2019-10-15 stsp
10825 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
10826 2822a352 2019-10-15 stsp if (error)
10827 2822a352 2019-10-15 stsp goto done;
10828 2822a352 2019-10-15 stsp
10829 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
10830 2822a352 2019-10-15 stsp if (error)
10831 2822a352 2019-10-15 stsp goto done;
10832 2822a352 2019-10-15 stsp
10833 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
10834 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
10835 2822a352 2019-10-15 stsp "specified branch has already been integrated");
10836 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10837 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10838 2822a352 2019-10-15 stsp goto done;
10839 2822a352 2019-10-15 stsp }
10840 2822a352 2019-10-15 stsp
10841 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
10842 2822a352 2019-10-15 stsp if (error) {
10843 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
10844 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
10845 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10846 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10847 2822a352 2019-10-15 stsp goto done;
10848 2822a352 2019-10-15 stsp }
10849 2822a352 2019-10-15 stsp
10850 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10851 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
10852 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
10853 2822a352 2019-10-15 stsp check_cancelled, NULL);
10854 2822a352 2019-10-15 stsp if (error)
10855 2822a352 2019-10-15 stsp goto done;
10856 2822a352 2019-10-15 stsp
10857 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
10858 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10859 2822a352 2019-10-15 stsp done:
10860 1d0f4054 2021-06-17 stsp if (repo) {
10861 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10862 1d0f4054 2021-06-17 stsp if (error == NULL)
10863 1d0f4054 2021-06-17 stsp error = close_err;
10864 1d0f4054 2021-06-17 stsp }
10865 2822a352 2019-10-15 stsp if (worktree)
10866 2822a352 2019-10-15 stsp got_worktree_close(worktree);
10867 2822a352 2019-10-15 stsp free(cwd);
10868 2822a352 2019-10-15 stsp free(base_commit_id);
10869 2822a352 2019-10-15 stsp free(commit_id);
10870 2822a352 2019-10-15 stsp free(refname);
10871 2822a352 2019-10-15 stsp free(base_refname);
10872 10604dce 2021-09-24 thomas return error;
10873 10604dce 2021-09-24 thomas }
10874 10604dce 2021-09-24 thomas
10875 10604dce 2021-09-24 thomas __dead static void
10876 10604dce 2021-09-24 thomas usage_merge(void)
10877 10604dce 2021-09-24 thomas {
10878 ba34626b 2021-09-27 thomas fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
10879 10604dce 2021-09-24 thomas getprogname());
10880 10604dce 2021-09-24 thomas exit(1);
10881 10604dce 2021-09-24 thomas }
10882 10604dce 2021-09-24 thomas
10883 10604dce 2021-09-24 thomas static const struct got_error *
10884 10604dce 2021-09-24 thomas cmd_merge(int argc, char *argv[])
10885 10604dce 2021-09-24 thomas {
10886 10604dce 2021-09-24 thomas const struct got_error *error = NULL;
10887 10604dce 2021-09-24 thomas struct got_worktree *worktree = NULL;
10888 10604dce 2021-09-24 thomas struct got_repository *repo = NULL;
10889 10604dce 2021-09-24 thomas struct got_fileindex *fileindex = NULL;
10890 10604dce 2021-09-24 thomas char *cwd = NULL, *id_str = NULL, *author = NULL;
10891 10604dce 2021-09-24 thomas struct got_reference *branch = NULL, *wt_branch = NULL;
10892 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *yca_id = NULL;
10893 10604dce 2021-09-24 thomas struct got_object_id *wt_branch_tip = NULL;
10894 10604dce 2021-09-24 thomas int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
10895 ba34626b 2021-09-27 thomas int interrupt_merge = 0;
10896 10604dce 2021-09-24 thomas struct got_update_progress_arg upa;
10897 10604dce 2021-09-24 thomas struct got_object_id *merge_commit_id = NULL;
10898 10604dce 2021-09-24 thomas char *branch_name = NULL;
10899 10604dce 2021-09-24 thomas
10900 10604dce 2021-09-24 thomas memset(&upa, 0, sizeof(upa));
10901 10604dce 2021-09-24 thomas
10902 ba34626b 2021-09-27 thomas while ((ch = getopt(argc, argv, "acn")) != -1) {
10903 10604dce 2021-09-24 thomas switch (ch) {
10904 10604dce 2021-09-24 thomas case 'a':
10905 10604dce 2021-09-24 thomas abort_merge = 1;
10906 10604dce 2021-09-24 thomas break;
10907 10604dce 2021-09-24 thomas case 'c':
10908 10604dce 2021-09-24 thomas continue_merge = 1;
10909 10604dce 2021-09-24 thomas break;
10910 ba34626b 2021-09-27 thomas case 'n':
10911 ba34626b 2021-09-27 thomas interrupt_merge = 1;
10912 ba34626b 2021-09-27 thomas break;
10913 10604dce 2021-09-24 thomas default:
10914 10604dce 2021-09-24 thomas usage_rebase();
10915 10604dce 2021-09-24 thomas /* NOTREACHED */
10916 10604dce 2021-09-24 thomas }
10917 10604dce 2021-09-24 thomas }
10918 10604dce 2021-09-24 thomas
10919 10604dce 2021-09-24 thomas argc -= optind;
10920 10604dce 2021-09-24 thomas argv += optind;
10921 10604dce 2021-09-24 thomas
10922 10604dce 2021-09-24 thomas #ifndef PROFILE
10923 10604dce 2021-09-24 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10924 10604dce 2021-09-24 thomas "unveil", NULL) == -1)
10925 10604dce 2021-09-24 thomas err(1, "pledge");
10926 10604dce 2021-09-24 thomas #endif
10927 10604dce 2021-09-24 thomas
10928 10604dce 2021-09-24 thomas if (abort_merge && continue_merge)
10929 10604dce 2021-09-24 thomas option_conflict('a', 'c');
10930 10604dce 2021-09-24 thomas if (abort_merge || continue_merge) {
10931 10604dce 2021-09-24 thomas if (argc != 0)
10932 10604dce 2021-09-24 thomas usage_merge();
10933 10604dce 2021-09-24 thomas } else if (argc != 1)
10934 10604dce 2021-09-24 thomas usage_merge();
10935 10604dce 2021-09-24 thomas
10936 10604dce 2021-09-24 thomas cwd = getcwd(NULL, 0);
10937 10604dce 2021-09-24 thomas if (cwd == NULL) {
10938 10604dce 2021-09-24 thomas error = got_error_from_errno("getcwd");
10939 10604dce 2021-09-24 thomas goto done;
10940 10604dce 2021-09-24 thomas }
10941 10604dce 2021-09-24 thomas
10942 10604dce 2021-09-24 thomas error = got_worktree_open(&worktree, cwd);
10943 10604dce 2021-09-24 thomas if (error) {
10944 10604dce 2021-09-24 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
10945 10604dce 2021-09-24 thomas error = wrap_not_worktree_error(error,
10946 10604dce 2021-09-24 thomas "merge", cwd);
10947 10604dce 2021-09-24 thomas goto done;
10948 10604dce 2021-09-24 thomas }
10949 10604dce 2021-09-24 thomas
10950 10604dce 2021-09-24 thomas error = got_repo_open(&repo,
10951 10604dce 2021-09-24 thomas worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
10952 10604dce 2021-09-24 thomas if (error != NULL)
10953 10604dce 2021-09-24 thomas goto done;
10954 10604dce 2021-09-24 thomas
10955 10604dce 2021-09-24 thomas error = apply_unveil(got_repo_get_path(repo), 0,
10956 10604dce 2021-09-24 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
10957 10604dce 2021-09-24 thomas if (error)
10958 10604dce 2021-09-24 thomas goto done;
10959 10604dce 2021-09-24 thomas
10960 10604dce 2021-09-24 thomas error = check_rebase_or_histedit_in_progress(worktree);
10961 10604dce 2021-09-24 thomas if (error)
10962 10604dce 2021-09-24 thomas goto done;
10963 10604dce 2021-09-24 thomas
10964 10604dce 2021-09-24 thomas error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10965 10604dce 2021-09-24 thomas repo);
10966 10604dce 2021-09-24 thomas if (error)
10967 10604dce 2021-09-24 thomas goto done;
10968 10604dce 2021-09-24 thomas
10969 10604dce 2021-09-24 thomas if (abort_merge) {
10970 10604dce 2021-09-24 thomas if (!merge_in_progress) {
10971 10604dce 2021-09-24 thomas error = got_error(GOT_ERR_NOT_MERGING);
10972 10604dce 2021-09-24 thomas goto done;
10973 10604dce 2021-09-24 thomas }
10974 10604dce 2021-09-24 thomas error = got_worktree_merge_continue(&branch_name,
10975 10604dce 2021-09-24 thomas &branch_tip, &fileindex, worktree, repo);
10976 10604dce 2021-09-24 thomas if (error)
10977 10604dce 2021-09-24 thomas goto done;
10978 10604dce 2021-09-24 thomas error = got_worktree_merge_abort(worktree, fileindex, repo,
10979 d52bac28 2021-10-08 thomas abort_progress, &upa);
10980 10604dce 2021-09-24 thomas if (error)
10981 10604dce 2021-09-24 thomas goto done;
10982 10604dce 2021-09-24 thomas printf("Merge of %s aborted\n", branch_name);
10983 10604dce 2021-09-24 thomas goto done; /* nothing else to do */
10984 10604dce 2021-09-24 thomas }
10985 10604dce 2021-09-24 thomas
10986 10604dce 2021-09-24 thomas error = get_author(&author, repo, worktree);
10987 10604dce 2021-09-24 thomas if (error)
10988 10604dce 2021-09-24 thomas goto done;
10989 10604dce 2021-09-24 thomas
10990 10604dce 2021-09-24 thomas if (continue_merge) {
10991 10604dce 2021-09-24 thomas if (!merge_in_progress) {
10992 10604dce 2021-09-24 thomas error = got_error(GOT_ERR_NOT_MERGING);
10993 10604dce 2021-09-24 thomas goto done;
10994 10604dce 2021-09-24 thomas }
10995 10604dce 2021-09-24 thomas error = got_worktree_merge_continue(&branch_name,
10996 10604dce 2021-09-24 thomas &branch_tip, &fileindex, worktree, repo);
10997 10604dce 2021-09-24 thomas if (error)
10998 10604dce 2021-09-24 thomas goto done;
10999 10604dce 2021-09-24 thomas } else {
11000 10604dce 2021-09-24 thomas error = got_ref_open(&branch, repo, argv[0], 0);
11001 10604dce 2021-09-24 thomas if (error != NULL)
11002 10604dce 2021-09-24 thomas goto done;
11003 10604dce 2021-09-24 thomas branch_name = strdup(got_ref_get_name(branch));
11004 10604dce 2021-09-24 thomas if (branch_name == NULL) {
11005 10604dce 2021-09-24 thomas error = got_error_from_errno("strdup");
11006 10604dce 2021-09-24 thomas goto done;
11007 10604dce 2021-09-24 thomas }
11008 10604dce 2021-09-24 thomas error = got_ref_resolve(&branch_tip, repo, branch);
11009 10604dce 2021-09-24 thomas if (error)
11010 10604dce 2021-09-24 thomas goto done;
11011 10604dce 2021-09-24 thomas }
11012 10604dce 2021-09-24 thomas
11013 10604dce 2021-09-24 thomas error = got_ref_open(&wt_branch, repo,
11014 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(worktree), 0);
11015 10604dce 2021-09-24 thomas if (error)
11016 10604dce 2021-09-24 thomas goto done;
11017 10604dce 2021-09-24 thomas error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
11018 10604dce 2021-09-24 thomas if (error)
11019 10604dce 2021-09-24 thomas goto done;
11020 10604dce 2021-09-24 thomas error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11021 768705e3 2021-09-27 thomas wt_branch_tip, branch_tip, 0, repo,
11022 10604dce 2021-09-24 thomas check_cancelled, NULL);
11023 768705e3 2021-09-27 thomas if (error && error->code != GOT_ERR_ANCESTRY)
11024 10604dce 2021-09-24 thomas goto done;
11025 10604dce 2021-09-24 thomas
11026 10604dce 2021-09-24 thomas if (!continue_merge) {
11027 10604dce 2021-09-24 thomas error = check_path_prefix(wt_branch_tip, branch_tip,
11028 10604dce 2021-09-24 thomas got_worktree_get_path_prefix(worktree),
11029 10604dce 2021-09-24 thomas GOT_ERR_MERGE_PATH, repo);
11030 10604dce 2021-09-24 thomas if (error)
11031 10604dce 2021-09-24 thomas goto done;
11032 768705e3 2021-09-27 thomas if (yca_id) {
11033 768705e3 2021-09-27 thomas error = check_same_branch(wt_branch_tip, branch,
11034 768705e3 2021-09-27 thomas yca_id, repo);
11035 768705e3 2021-09-27 thomas if (error) {
11036 768705e3 2021-09-27 thomas if (error->code != GOT_ERR_ANCESTRY)
11037 768705e3 2021-09-27 thomas goto done;
11038 768705e3 2021-09-27 thomas error = NULL;
11039 768705e3 2021-09-27 thomas } else {
11040 768705e3 2021-09-27 thomas static char msg[512];
11041 768705e3 2021-09-27 thomas snprintf(msg, sizeof(msg),
11042 768705e3 2021-09-27 thomas "cannot create a merge commit because "
11043 768705e3 2021-09-27 thomas "%s is based on %s; %s can be integrated "
11044 768705e3 2021-09-27 thomas "with 'got integrate' instead", branch_name,
11045 768705e3 2021-09-27 thomas got_worktree_get_head_ref_name(worktree),
11046 768705e3 2021-09-27 thomas branch_name);
11047 768705e3 2021-09-27 thomas error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
11048 10604dce 2021-09-24 thomas goto done;
11049 768705e3 2021-09-27 thomas }
11050 10604dce 2021-09-24 thomas }
11051 10604dce 2021-09-24 thomas error = got_worktree_merge_prepare(&fileindex, worktree,
11052 10604dce 2021-09-24 thomas branch, repo);
11053 10604dce 2021-09-24 thomas if (error)
11054 10604dce 2021-09-24 thomas goto done;
11055 10604dce 2021-09-24 thomas
11056 10604dce 2021-09-24 thomas error = got_worktree_merge_branch(worktree, fileindex,
11057 10604dce 2021-09-24 thomas yca_id, branch_tip, repo, update_progress, &upa,
11058 10604dce 2021-09-24 thomas check_cancelled, NULL);
11059 10604dce 2021-09-24 thomas if (error)
11060 10604dce 2021-09-24 thomas goto done;
11061 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
11062 768705e3 2021-09-27 thomas if (!upa.did_something) {
11063 768705e3 2021-09-27 thomas error = got_worktree_merge_abort(worktree, fileindex,
11064 d52bac28 2021-10-08 thomas repo, abort_progress, &upa);
11065 768705e3 2021-09-27 thomas if (error)
11066 768705e3 2021-09-27 thomas goto done;
11067 768705e3 2021-09-27 thomas printf("Already up-to-date\n");
11068 768705e3 2021-09-27 thomas goto done;
11069 768705e3 2021-09-27 thomas }
11070 10604dce 2021-09-24 thomas }
11071 10604dce 2021-09-24 thomas
11072 ba34626b 2021-09-27 thomas if (interrupt_merge) {
11073 10604dce 2021-09-24 thomas error = got_worktree_merge_postpone(worktree, fileindex);
11074 10604dce 2021-09-24 thomas if (error)
11075 10604dce 2021-09-24 thomas goto done;
11076 ba34626b 2021-09-27 thomas printf("Merge of %s interrupted on request\n", branch_name);
11077 ba162991 2021-09-28 thomas } else if (upa.conflicts > 0 || upa.missing > 0 ||
11078 ba162991 2021-09-28 thomas upa.not_deleted > 0 || upa.unversioned > 0) {
11079 ba34626b 2021-09-27 thomas error = got_worktree_merge_postpone(worktree, fileindex);
11080 ba34626b 2021-09-27 thomas if (error)
11081 ba34626b 2021-09-27 thomas goto done;
11082 ba162991 2021-09-28 thomas if (upa.conflicts > 0 && upa.missing == 0 &&
11083 ba162991 2021-09-28 thomas upa.not_deleted == 0 && upa.unversioned == 0) {
11084 10604dce 2021-09-24 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
11085 10604dce 2021-09-24 thomas "conflicts must be resolved before merging "
11086 10604dce 2021-09-24 thomas "can continue");
11087 10604dce 2021-09-24 thomas } else if (upa.conflicts > 0) {
11088 10604dce 2021-09-24 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
11089 10604dce 2021-09-24 thomas "conflicts must be resolved before merging "
11090 ba162991 2021-09-28 thomas "can continue; changes destined for some "
11091 88d249c2 2021-09-24 thomas "files were not yet merged and "
11092 10604dce 2021-09-24 thomas "should be merged manually if required before the "
11093 10604dce 2021-09-24 thomas "merge operation is continued");
11094 10604dce 2021-09-24 thomas } else {
11095 10604dce 2021-09-24 thomas error = got_error_msg(GOT_ERR_CONFLICTS,
11096 ba162991 2021-09-28 thomas "changes destined for some "
11097 10604dce 2021-09-24 thomas "files were not yet merged and should be "
11098 10604dce 2021-09-24 thomas "merged manually if required before the "
11099 10604dce 2021-09-24 thomas "merge operation is continued");
11100 10604dce 2021-09-24 thomas }
11101 10604dce 2021-09-24 thomas goto done;
11102 10604dce 2021-09-24 thomas } else {
11103 10604dce 2021-09-24 thomas error = got_worktree_merge_commit(&merge_commit_id, worktree,
11104 ae1e948a 2021-09-28 thomas fileindex, author, NULL, 1, branch_tip, branch_name,
11105 ae1e948a 2021-09-28 thomas repo, continue_merge ? print_status : NULL, NULL);
11106 10604dce 2021-09-24 thomas if (error)
11107 10604dce 2021-09-24 thomas goto done;
11108 10604dce 2021-09-24 thomas error = got_worktree_merge_complete(worktree, fileindex, repo);
11109 10604dce 2021-09-24 thomas if (error)
11110 10604dce 2021-09-24 thomas goto done;
11111 10604dce 2021-09-24 thomas error = got_object_id_str(&id_str, merge_commit_id);
11112 10604dce 2021-09-24 thomas if (error)
11113 10604dce 2021-09-24 thomas goto done;
11114 10604dce 2021-09-24 thomas printf("Merged %s into %s: %s\n", branch_name,
11115 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(worktree),
11116 10604dce 2021-09-24 thomas id_str);
11117 10604dce 2021-09-24 thomas
11118 10604dce 2021-09-24 thomas }
11119 10604dce 2021-09-24 thomas done:
11120 10604dce 2021-09-24 thomas free(id_str);
11121 10604dce 2021-09-24 thomas free(merge_commit_id);
11122 10604dce 2021-09-24 thomas free(author);
11123 10604dce 2021-09-24 thomas free(branch_tip);
11124 10604dce 2021-09-24 thomas free(branch_name);
11125 10604dce 2021-09-24 thomas free(yca_id);
11126 10604dce 2021-09-24 thomas if (branch)
11127 10604dce 2021-09-24 thomas got_ref_close(branch);
11128 10604dce 2021-09-24 thomas if (wt_branch)
11129 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
11130 10604dce 2021-09-24 thomas if (worktree)
11131 10604dce 2021-09-24 thomas got_worktree_close(worktree);
11132 10604dce 2021-09-24 thomas if (repo) {
11133 10604dce 2021-09-24 thomas const struct got_error *close_err = got_repo_close(repo);
11134 10604dce 2021-09-24 thomas if (error == NULL)
11135 10604dce 2021-09-24 thomas error = close_err;
11136 10604dce 2021-09-24 thomas }
11137 715dc77e 2019-08-03 stsp return error;
11138 715dc77e 2019-08-03 stsp }
11139 715dc77e 2019-08-03 stsp
11140 715dc77e 2019-08-03 stsp __dead static void
11141 715dc77e 2019-08-03 stsp usage_stage(void)
11142 715dc77e 2019-08-03 stsp {
11143 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
11144 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
11145 715dc77e 2019-08-03 stsp getprogname());
11146 715dc77e 2019-08-03 stsp exit(1);
11147 715dc77e 2019-08-03 stsp }
11148 715dc77e 2019-08-03 stsp
11149 715dc77e 2019-08-03 stsp static const struct got_error *
11150 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
11151 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
11152 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
11153 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
11154 408b4ebc 2019-08-03 stsp {
11155 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
11156 408b4ebc 2019-08-03 stsp char *id_str = NULL;
11157 408b4ebc 2019-08-03 stsp
11158 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
11159 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
11160 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
11161 408b4ebc 2019-08-03 stsp return NULL;
11162 408b4ebc 2019-08-03 stsp
11163 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
11164 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
11165 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
11166 408b4ebc 2019-08-03 stsp else
11167 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
11168 408b4ebc 2019-08-03 stsp if (err)
11169 408b4ebc 2019-08-03 stsp return err;
11170 408b4ebc 2019-08-03 stsp
11171 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
11172 408b4ebc 2019-08-03 stsp free(id_str);
11173 dc424a06 2019-08-07 stsp return NULL;
11174 dc424a06 2019-08-07 stsp }
11175 2e1f37b0 2019-08-08 stsp
11176 dc424a06 2019-08-07 stsp static const struct got_error *
11177 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
11178 715dc77e 2019-08-03 stsp {
11179 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
11180 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
11181 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
11182 715dc77e 2019-08-03 stsp char *cwd = NULL;
11183 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
11184 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
11185 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
11186 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
11187 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
11188 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
11189 715dc77e 2019-08-03 stsp
11190 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
11191 715dc77e 2019-08-03 stsp
11192 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
11193 715dc77e 2019-08-03 stsp switch (ch) {
11194 408b4ebc 2019-08-03 stsp case 'l':
11195 408b4ebc 2019-08-03 stsp list_stage = 1;
11196 408b4ebc 2019-08-03 stsp break;
11197 dc424a06 2019-08-07 stsp case 'p':
11198 dc424a06 2019-08-07 stsp pflag = 1;
11199 dc424a06 2019-08-07 stsp break;
11200 dc424a06 2019-08-07 stsp case 'F':
11201 dc424a06 2019-08-07 stsp patch_script_path = optarg;
11202 dc424a06 2019-08-07 stsp break;
11203 35213c7c 2020-07-23 stsp case 'S':
11204 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
11205 35213c7c 2020-07-23 stsp break;
11206 715dc77e 2019-08-03 stsp default:
11207 715dc77e 2019-08-03 stsp usage_stage();
11208 715dc77e 2019-08-03 stsp /* NOTREACHED */
11209 715dc77e 2019-08-03 stsp }
11210 715dc77e 2019-08-03 stsp }
11211 715dc77e 2019-08-03 stsp
11212 715dc77e 2019-08-03 stsp argc -= optind;
11213 715dc77e 2019-08-03 stsp argv += optind;
11214 715dc77e 2019-08-03 stsp
11215 715dc77e 2019-08-03 stsp #ifndef PROFILE
11216 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11217 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
11218 715dc77e 2019-08-03 stsp err(1, "pledge");
11219 715dc77e 2019-08-03 stsp #endif
11220 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
11221 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
11222 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
11223 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
11224 715dc77e 2019-08-03 stsp
11225 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
11226 715dc77e 2019-08-03 stsp if (cwd == NULL) {
11227 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
11228 715dc77e 2019-08-03 stsp goto done;
11229 715dc77e 2019-08-03 stsp }
11230 715dc77e 2019-08-03 stsp
11231 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
11232 fa51e947 2020-03-27 stsp if (error) {
11233 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
11234 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
11235 715dc77e 2019-08-03 stsp goto done;
11236 fa51e947 2020-03-27 stsp }
11237 715dc77e 2019-08-03 stsp
11238 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11239 c9956ddf 2019-09-08 stsp NULL);
11240 715dc77e 2019-08-03 stsp if (error != NULL)
11241 715dc77e 2019-08-03 stsp goto done;
11242 715dc77e 2019-08-03 stsp
11243 dc424a06 2019-08-07 stsp if (patch_script_path) {
11244 c56c5d8a 2021-12-31 thomas patch_script_file = fopen(patch_script_path, "re");
11245 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
11246 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
11247 dc424a06 2019-08-07 stsp patch_script_path);
11248 dc424a06 2019-08-07 stsp goto done;
11249 dc424a06 2019-08-07 stsp }
11250 dc424a06 2019-08-07 stsp }
11251 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
11252 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
11253 715dc77e 2019-08-03 stsp if (error)
11254 715dc77e 2019-08-03 stsp goto done;
11255 715dc77e 2019-08-03 stsp
11256 10604dce 2021-09-24 thomas error = check_merge_in_progress(worktree, repo);
11257 10604dce 2021-09-24 thomas if (error)
11258 10604dce 2021-09-24 thomas goto done;
11259 10604dce 2021-09-24 thomas
11260 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
11261 6d022e97 2019-08-04 stsp if (error)
11262 6d022e97 2019-08-04 stsp goto done;
11263 715dc77e 2019-08-03 stsp
11264 6d022e97 2019-08-04 stsp if (list_stage)
11265 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
11266 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
11267 2e1f37b0 2019-08-08 stsp else {
11268 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
11269 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
11270 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
11271 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
11272 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
11273 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
11274 2e1f37b0 2019-08-08 stsp }
11275 ad493afc 2019-08-03 stsp done:
11276 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
11277 2e1f37b0 2019-08-08 stsp error == NULL)
11278 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
11279 1d0f4054 2021-06-17 stsp if (repo) {
11280 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
11281 1d0f4054 2021-06-17 stsp if (error == NULL)
11282 1d0f4054 2021-06-17 stsp error = close_err;
11283 1d0f4054 2021-06-17 stsp }
11284 ad493afc 2019-08-03 stsp if (worktree)
11285 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
11286 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
11287 ad493afc 2019-08-03 stsp free((char *)pe->path);
11288 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
11289 ad493afc 2019-08-03 stsp free(cwd);
11290 ad493afc 2019-08-03 stsp return error;
11291 ad493afc 2019-08-03 stsp }
11292 ad493afc 2019-08-03 stsp
11293 ad493afc 2019-08-03 stsp __dead static void
11294 ad493afc 2019-08-03 stsp usage_unstage(void)
11295 ad493afc 2019-08-03 stsp {
11296 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
11297 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
11298 ad493afc 2019-08-03 stsp getprogname());
11299 ad493afc 2019-08-03 stsp exit(1);
11300 ad493afc 2019-08-03 stsp }
11301 ad493afc 2019-08-03 stsp
11302 ad493afc 2019-08-03 stsp
11303 ad493afc 2019-08-03 stsp static const struct got_error *
11304 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
11305 ad493afc 2019-08-03 stsp {
11306 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
11307 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
11308 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
11309 ad493afc 2019-08-03 stsp char *cwd = NULL;
11310 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
11311 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
11312 9627c110 2020-04-18 stsp int ch, pflag = 0;
11313 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
11314 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
11315 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
11316 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
11317 ad493afc 2019-08-03 stsp
11318 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
11319 ad493afc 2019-08-03 stsp
11320 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
11321 ad493afc 2019-08-03 stsp switch (ch) {
11322 2e1f37b0 2019-08-08 stsp case 'p':
11323 2e1f37b0 2019-08-08 stsp pflag = 1;
11324 2e1f37b0 2019-08-08 stsp break;
11325 2e1f37b0 2019-08-08 stsp case 'F':
11326 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
11327 2e1f37b0 2019-08-08 stsp break;
11328 ad493afc 2019-08-03 stsp default:
11329 ad493afc 2019-08-03 stsp usage_unstage();
11330 ad493afc 2019-08-03 stsp /* NOTREACHED */
11331 ad493afc 2019-08-03 stsp }
11332 ad493afc 2019-08-03 stsp }
11333 ad493afc 2019-08-03 stsp
11334 ad493afc 2019-08-03 stsp argc -= optind;
11335 ad493afc 2019-08-03 stsp argv += optind;
11336 ad493afc 2019-08-03 stsp
11337 ad493afc 2019-08-03 stsp #ifndef PROFILE
11338 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11339 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
11340 ad493afc 2019-08-03 stsp err(1, "pledge");
11341 ad493afc 2019-08-03 stsp #endif
11342 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
11343 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
11344 2e1f37b0 2019-08-08 stsp
11345 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
11346 ad493afc 2019-08-03 stsp if (cwd == NULL) {
11347 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
11348 ad493afc 2019-08-03 stsp goto done;
11349 ad493afc 2019-08-03 stsp }
11350 ad493afc 2019-08-03 stsp
11351 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
11352 fa51e947 2020-03-27 stsp if (error) {
11353 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
11354 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
11355 ad493afc 2019-08-03 stsp goto done;
11356 fa51e947 2020-03-27 stsp }
11357 ad493afc 2019-08-03 stsp
11358 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11359 c9956ddf 2019-09-08 stsp NULL);
11360 ad493afc 2019-08-03 stsp if (error != NULL)
11361 ad493afc 2019-08-03 stsp goto done;
11362 ad493afc 2019-08-03 stsp
11363 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
11364 c56c5d8a 2021-12-31 thomas patch_script_file = fopen(patch_script_path, "re");
11365 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
11366 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
11367 2e1f37b0 2019-08-08 stsp patch_script_path);
11368 2e1f37b0 2019-08-08 stsp goto done;
11369 2e1f37b0 2019-08-08 stsp }
11370 2e1f37b0 2019-08-08 stsp }
11371 2e1f37b0 2019-08-08 stsp
11372 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
11373 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
11374 ad493afc 2019-08-03 stsp if (error)
11375 ad493afc 2019-08-03 stsp goto done;
11376 ad493afc 2019-08-03 stsp
11377 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
11378 ad493afc 2019-08-03 stsp if (error)
11379 ad493afc 2019-08-03 stsp goto done;
11380 ad493afc 2019-08-03 stsp
11381 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
11382 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
11383 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
11384 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
11385 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
11386 9627c110 2020-04-18 stsp if (!error)
11387 0ef68859 2021-09-28 thomas print_merge_progress_stats(&upa);
11388 715dc77e 2019-08-03 stsp done:
11389 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
11390 2e1f37b0 2019-08-08 stsp error == NULL)
11391 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
11392 1d0f4054 2021-06-17 stsp if (repo) {
11393 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
11394 1d0f4054 2021-06-17 stsp if (error == NULL)
11395 1d0f4054 2021-06-17 stsp error = close_err;
11396 1d0f4054 2021-06-17 stsp }
11397 715dc77e 2019-08-03 stsp if (worktree)
11398 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
11399 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
11400 715dc77e 2019-08-03 stsp free((char *)pe->path);
11401 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
11402 715dc77e 2019-08-03 stsp free(cwd);
11403 01073a5d 2019-08-22 stsp return error;
11404 01073a5d 2019-08-22 stsp }
11405 01073a5d 2019-08-22 stsp
11406 01073a5d 2019-08-22 stsp __dead static void
11407 01073a5d 2019-08-22 stsp usage_cat(void)
11408 01073a5d 2019-08-22 stsp {
11409 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
11410 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
11411 01073a5d 2019-08-22 stsp exit(1);
11412 01073a5d 2019-08-22 stsp }
11413 01073a5d 2019-08-22 stsp
11414 01073a5d 2019-08-22 stsp static const struct got_error *
11415 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11416 01073a5d 2019-08-22 stsp {
11417 01073a5d 2019-08-22 stsp const struct got_error *err;
11418 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
11419 01073a5d 2019-08-22 stsp
11420 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
11421 01073a5d 2019-08-22 stsp if (err)
11422 01073a5d 2019-08-22 stsp return err;
11423 01073a5d 2019-08-22 stsp
11424 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
11425 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
11426 01073a5d 2019-08-22 stsp return err;
11427 01073a5d 2019-08-22 stsp }
11428 01073a5d 2019-08-22 stsp
11429 01073a5d 2019-08-22 stsp static const struct got_error *
11430 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11431 01073a5d 2019-08-22 stsp {
11432 01073a5d 2019-08-22 stsp const struct got_error *err;
11433 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
11434 56e0773d 2019-11-28 stsp int nentries, i;
11435 01073a5d 2019-08-22 stsp
11436 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
11437 01073a5d 2019-08-22 stsp if (err)
11438 01073a5d 2019-08-22 stsp return err;
11439 01073a5d 2019-08-22 stsp
11440 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
11441 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
11442 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
11443 01073a5d 2019-08-22 stsp char *id_str;
11444 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
11445 01073a5d 2019-08-22 stsp break;
11446 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
11447 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
11448 01073a5d 2019-08-22 stsp if (err)
11449 01073a5d 2019-08-22 stsp break;
11450 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
11451 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
11452 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
11453 01073a5d 2019-08-22 stsp free(id_str);
11454 01073a5d 2019-08-22 stsp }
11455 01073a5d 2019-08-22 stsp
11456 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
11457 01073a5d 2019-08-22 stsp return err;
11458 01073a5d 2019-08-22 stsp }
11459 01073a5d 2019-08-22 stsp
11460 01073a5d 2019-08-22 stsp static const struct got_error *
11461 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11462 01073a5d 2019-08-22 stsp {
11463 01073a5d 2019-08-22 stsp const struct got_error *err;
11464 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
11465 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
11466 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
11467 01073a5d 2019-08-22 stsp char *id_str = NULL;
11468 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
11469 01073a5d 2019-08-22 stsp
11470 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
11471 01073a5d 2019-08-22 stsp if (err)
11472 01073a5d 2019-08-22 stsp return err;
11473 01073a5d 2019-08-22 stsp
11474 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
11475 01073a5d 2019-08-22 stsp if (err)
11476 01073a5d 2019-08-22 stsp goto done;
11477 01073a5d 2019-08-22 stsp
11478 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
11479 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
11480 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
11481 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
11482 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
11483 01073a5d 2019-08-22 stsp char *pid_str;
11484 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
11485 01073a5d 2019-08-22 stsp if (err)
11486 01073a5d 2019-08-22 stsp goto done;
11487 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
11488 01073a5d 2019-08-22 stsp free(pid_str);
11489 01073a5d 2019-08-22 stsp }
11490 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
11491 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
11492 1367695b 2020-09-26 naddy (long long)got_object_commit_get_author_time(commit));
11493 01073a5d 2019-08-22 stsp
11494 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
11495 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
11496 1367695b 2020-09-26 naddy (long long)got_object_commit_get_committer_time(commit));
11497 01073a5d 2019-08-22 stsp
11498 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
11499 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
11500 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
11501 01073a5d 2019-08-22 stsp done:
11502 01073a5d 2019-08-22 stsp free(id_str);
11503 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
11504 01073a5d 2019-08-22 stsp return err;
11505 01073a5d 2019-08-22 stsp }
11506 01073a5d 2019-08-22 stsp
11507 01073a5d 2019-08-22 stsp static const struct got_error *
11508 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11509 01073a5d 2019-08-22 stsp {
11510 01073a5d 2019-08-22 stsp const struct got_error *err;
11511 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
11512 01073a5d 2019-08-22 stsp char *id_str = NULL;
11513 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
11514 01073a5d 2019-08-22 stsp
11515 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
11516 01073a5d 2019-08-22 stsp if (err)
11517 01073a5d 2019-08-22 stsp return err;
11518 01073a5d 2019-08-22 stsp
11519 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
11520 01073a5d 2019-08-22 stsp if (err)
11521 01073a5d 2019-08-22 stsp goto done;
11522 01073a5d 2019-08-22 stsp
11523 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
11524 e15d5241 2019-08-22 stsp
11525 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
11526 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
11527 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11528 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
11529 01073a5d 2019-08-22 stsp break;
11530 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
11531 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11532 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
11533 01073a5d 2019-08-22 stsp break;
11534 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
11535 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11536 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
11537 01073a5d 2019-08-22 stsp break;
11538 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
11539 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11540 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
11541 01073a5d 2019-08-22 stsp break;
11542 01073a5d 2019-08-22 stsp default:
11543 01073a5d 2019-08-22 stsp break;
11544 01073a5d 2019-08-22 stsp }
11545 01073a5d 2019-08-22 stsp
11546 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
11547 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
11548 e15d5241 2019-08-22 stsp
11549 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
11550 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
11551 1367695b 2020-09-26 naddy (long long)got_object_tag_get_tagger_time(tag));
11552 01073a5d 2019-08-22 stsp
11553 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
11554 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
11555 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
11556 01073a5d 2019-08-22 stsp done:
11557 01073a5d 2019-08-22 stsp free(id_str);
11558 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
11559 01073a5d 2019-08-22 stsp return err;
11560 01073a5d 2019-08-22 stsp }
11561 01073a5d 2019-08-22 stsp
11562 01073a5d 2019-08-22 stsp static const struct got_error *
11563 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
11564 01073a5d 2019-08-22 stsp {
11565 01073a5d 2019-08-22 stsp const struct got_error *error;
11566 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
11567 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
11568 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
11569 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
11570 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
11571 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
11572 84de9106 2020-12-26 stsp struct got_reflist_head refs;
11573 84de9106 2020-12-26 stsp
11574 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
11575 01073a5d 2019-08-22 stsp
11576 01073a5d 2019-08-22 stsp #ifndef PROFILE
11577 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
11578 01073a5d 2019-08-22 stsp NULL) == -1)
11579 01073a5d 2019-08-22 stsp err(1, "pledge");
11580 01073a5d 2019-08-22 stsp #endif
11581 01073a5d 2019-08-22 stsp
11582 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
11583 01073a5d 2019-08-22 stsp switch (ch) {
11584 896e9b6f 2019-08-26 stsp case 'c':
11585 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
11586 896e9b6f 2019-08-26 stsp break;
11587 01073a5d 2019-08-22 stsp case 'r':
11588 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
11589 01073a5d 2019-08-22 stsp if (repo_path == NULL)
11590 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
11591 9ba1d308 2019-10-21 stsp optarg);
11592 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
11593 01073a5d 2019-08-22 stsp break;
11594 896e9b6f 2019-08-26 stsp case 'P':
11595 896e9b6f 2019-08-26 stsp force_path = 1;
11596 896e9b6f 2019-08-26 stsp break;
11597 01073a5d 2019-08-22 stsp default:
11598 01073a5d 2019-08-22 stsp usage_cat();
11599 01073a5d 2019-08-22 stsp /* NOTREACHED */
11600 01073a5d 2019-08-22 stsp }
11601 01073a5d 2019-08-22 stsp }
11602 01073a5d 2019-08-22 stsp
11603 01073a5d 2019-08-22 stsp argc -= optind;
11604 01073a5d 2019-08-22 stsp argv += optind;
11605 01073a5d 2019-08-22 stsp
11606 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
11607 01073a5d 2019-08-22 stsp if (cwd == NULL) {
11608 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
11609 01073a5d 2019-08-22 stsp goto done;
11610 01073a5d 2019-08-22 stsp }
11611 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
11612 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
11613 01073a5d 2019-08-22 stsp goto done;
11614 01073a5d 2019-08-22 stsp if (worktree) {
11615 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
11616 01073a5d 2019-08-22 stsp repo_path = strdup(
11617 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
11618 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
11619 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
11620 01073a5d 2019-08-22 stsp goto done;
11621 01073a5d 2019-08-22 stsp }
11622 01073a5d 2019-08-22 stsp }
11623 01073a5d 2019-08-22 stsp }
11624 01073a5d 2019-08-22 stsp
11625 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
11626 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
11627 01073a5d 2019-08-22 stsp if (repo_path == NULL)
11628 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
11629 01073a5d 2019-08-22 stsp }
11630 01073a5d 2019-08-22 stsp
11631 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
11632 01073a5d 2019-08-22 stsp free(repo_path);
11633 01073a5d 2019-08-22 stsp if (error != NULL)
11634 01073a5d 2019-08-22 stsp goto done;
11635 01073a5d 2019-08-22 stsp
11636 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
11637 896e9b6f 2019-08-26 stsp if (error)
11638 896e9b6f 2019-08-26 stsp goto done;
11639 896e9b6f 2019-08-26 stsp
11640 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
11641 84de9106 2020-12-26 stsp if (error)
11642 84de9106 2020-12-26 stsp goto done;
11643 84de9106 2020-12-26 stsp
11644 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
11645 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
11646 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
11647 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
11648 01073a5d 2019-08-22 stsp if (error)
11649 01073a5d 2019-08-22 stsp goto done;
11650 01073a5d 2019-08-22 stsp
11651 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
11652 896e9b6f 2019-08-26 stsp if (force_path) {
11653 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
11654 896e9b6f 2019-08-26 stsp argv[i]);
11655 896e9b6f 2019-08-26 stsp if (error)
11656 896e9b6f 2019-08-26 stsp break;
11657 896e9b6f 2019-08-26 stsp } else {
11658 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
11659 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
11660 84de9106 2020-12-26 stsp repo);
11661 896e9b6f 2019-08-26 stsp if (error) {
11662 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
11663 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
11664 896e9b6f 2019-08-26 stsp break;
11665 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
11666 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
11667 896e9b6f 2019-08-26 stsp if (error)
11668 896e9b6f 2019-08-26 stsp break;
11669 896e9b6f 2019-08-26 stsp }
11670 896e9b6f 2019-08-26 stsp }
11671 01073a5d 2019-08-22 stsp
11672 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
11673 01073a5d 2019-08-22 stsp if (error)
11674 01073a5d 2019-08-22 stsp break;
11675 01073a5d 2019-08-22 stsp
11676 01073a5d 2019-08-22 stsp switch (obj_type) {
11677 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
11678 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
11679 01073a5d 2019-08-22 stsp break;
11680 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
11681 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
11682 01073a5d 2019-08-22 stsp break;
11683 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
11684 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
11685 01073a5d 2019-08-22 stsp break;
11686 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
11687 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
11688 01073a5d 2019-08-22 stsp break;
11689 01073a5d 2019-08-22 stsp default:
11690 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
11691 01073a5d 2019-08-22 stsp break;
11692 01073a5d 2019-08-22 stsp }
11693 01073a5d 2019-08-22 stsp if (error)
11694 01073a5d 2019-08-22 stsp break;
11695 01073a5d 2019-08-22 stsp free(label);
11696 01073a5d 2019-08-22 stsp label = NULL;
11697 01073a5d 2019-08-22 stsp free(id);
11698 01073a5d 2019-08-22 stsp id = NULL;
11699 01073a5d 2019-08-22 stsp }
11700 01073a5d 2019-08-22 stsp done:
11701 01073a5d 2019-08-22 stsp free(label);
11702 01073a5d 2019-08-22 stsp free(id);
11703 896e9b6f 2019-08-26 stsp free(commit_id);
11704 01073a5d 2019-08-22 stsp if (worktree)
11705 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
11706 01073a5d 2019-08-22 stsp if (repo) {
11707 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
11708 01073a5d 2019-08-22 stsp if (error == NULL)
11709 1d0f4054 2021-06-17 stsp error = close_err;
11710 b2118c49 2020-07-28 stsp }
11711 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
11712 b2118c49 2020-07-28 stsp return error;
11713 b2118c49 2020-07-28 stsp }
11714 b2118c49 2020-07-28 stsp
11715 b2118c49 2020-07-28 stsp __dead static void
11716 b2118c49 2020-07-28 stsp usage_info(void)
11717 b2118c49 2020-07-28 stsp {
11718 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
11719 b2118c49 2020-07-28 stsp getprogname());
11720 b2118c49 2020-07-28 stsp exit(1);
11721 b2118c49 2020-07-28 stsp }
11722 b2118c49 2020-07-28 stsp
11723 b2118c49 2020-07-28 stsp static const struct got_error *
11724 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
11725 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11726 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
11727 b2118c49 2020-07-28 stsp {
11728 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
11729 b2118c49 2020-07-28 stsp char *id_str = NULL;
11730 b2118c49 2020-07-28 stsp char datebuf[128];
11731 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
11732 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
11733 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11734 b2118c49 2020-07-28 stsp
11735 b2118c49 2020-07-28 stsp /*
11736 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
11737 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
11738 b2118c49 2020-07-28 stsp */
11739 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
11740 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
11741 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
11742 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
11743 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
11744 b2118c49 2020-07-28 stsp }
11745 b2118c49 2020-07-28 stsp
11746 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
11747 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
11748 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
11749 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
11750 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
11751 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
11752 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
11753 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
11754 b2118c49 2020-07-28 stsp else
11755 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
11756 b2118c49 2020-07-28 stsp
11757 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
11758 b2118c49 2020-07-28 stsp if (tm == NULL)
11759 b2118c49 2020-07-28 stsp return NULL;
11760 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
11761 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
11762 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
11763 b2118c49 2020-07-28 stsp
11764 b2118c49 2020-07-28 stsp if (blob_id) {
11765 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
11766 b2118c49 2020-07-28 stsp if (err)
11767 b2118c49 2020-07-28 stsp return err;
11768 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
11769 b2118c49 2020-07-28 stsp free(id_str);
11770 b2118c49 2020-07-28 stsp }
11771 b2118c49 2020-07-28 stsp
11772 b2118c49 2020-07-28 stsp if (staged_blob_id) {
11773 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
11774 b2118c49 2020-07-28 stsp if (err)
11775 b2118c49 2020-07-28 stsp return err;
11776 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
11777 b2118c49 2020-07-28 stsp free(id_str);
11778 b2118c49 2020-07-28 stsp }
11779 b2118c49 2020-07-28 stsp
11780 b2118c49 2020-07-28 stsp if (commit_id) {
11781 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
11782 b2118c49 2020-07-28 stsp if (err)
11783 b2118c49 2020-07-28 stsp return err;
11784 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
11785 b2118c49 2020-07-28 stsp free(id_str);
11786 b2118c49 2020-07-28 stsp }
11787 b2118c49 2020-07-28 stsp
11788 b2118c49 2020-07-28 stsp return NULL;
11789 b2118c49 2020-07-28 stsp }
11790 b2118c49 2020-07-28 stsp
11791 b2118c49 2020-07-28 stsp static const struct got_error *
11792 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
11793 b2118c49 2020-07-28 stsp {
11794 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
11795 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
11796 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
11797 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
11798 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11799 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
11800 b2118c49 2020-07-28 stsp int ch, show_files = 0;
11801 b2118c49 2020-07-28 stsp
11802 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
11803 b2118c49 2020-07-28 stsp
11804 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
11805 b2118c49 2020-07-28 stsp switch (ch) {
11806 b2118c49 2020-07-28 stsp default:
11807 b2118c49 2020-07-28 stsp usage_info();
11808 b2118c49 2020-07-28 stsp /* NOTREACHED */
11809 b2118c49 2020-07-28 stsp }
11810 01073a5d 2019-08-22 stsp }
11811 b2118c49 2020-07-28 stsp
11812 b2118c49 2020-07-28 stsp argc -= optind;
11813 b2118c49 2020-07-28 stsp argv += optind;
11814 b2118c49 2020-07-28 stsp
11815 b2118c49 2020-07-28 stsp #ifndef PROFILE
11816 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
11817 b2118c49 2020-07-28 stsp NULL) == -1)
11818 b2118c49 2020-07-28 stsp err(1, "pledge");
11819 b2118c49 2020-07-28 stsp #endif
11820 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
11821 b2118c49 2020-07-28 stsp if (cwd == NULL) {
11822 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
11823 b2118c49 2020-07-28 stsp goto done;
11824 b2118c49 2020-07-28 stsp }
11825 b2118c49 2020-07-28 stsp
11826 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
11827 b2118c49 2020-07-28 stsp if (error) {
11828 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
11829 8ea5c997 2021-02-07 naddy error = wrap_not_worktree_error(error, "info", cwd);
11830 b2118c49 2020-07-28 stsp goto done;
11831 b2118c49 2020-07-28 stsp }
11832 b2118c49 2020-07-28 stsp
11833 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
11834 b2118c49 2020-07-28 stsp if (error)
11835 b2118c49 2020-07-28 stsp goto done;
11836 b2118c49 2020-07-28 stsp
11837 b2118c49 2020-07-28 stsp if (argc >= 1) {
11838 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
11839 b2118c49 2020-07-28 stsp worktree);
11840 b2118c49 2020-07-28 stsp if (error)
11841 b2118c49 2020-07-28 stsp goto done;
11842 b2118c49 2020-07-28 stsp show_files = 1;
11843 b2118c49 2020-07-28 stsp }
11844 b2118c49 2020-07-28 stsp
11845 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
11846 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
11847 b2118c49 2020-07-28 stsp if (error)
11848 b2118c49 2020-07-28 stsp goto done;
11849 b2118c49 2020-07-28 stsp
11850 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
11851 b2118c49 2020-07-28 stsp if (error)
11852 b2118c49 2020-07-28 stsp goto done;
11853 b2118c49 2020-07-28 stsp
11854 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
11855 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
11856 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
11857 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
11858 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
11859 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
11860 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
11861 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
11862 b2118c49 2020-07-28 stsp
11863 b2118c49 2020-07-28 stsp if (show_files) {
11864 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11865 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
11866 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
11867 b2118c49 2020-07-28 stsp continue;
11868 b2118c49 2020-07-28 stsp /*
11869 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
11870 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
11871 b2118c49 2020-07-28 stsp */
11872 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
11873 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
11874 b2118c49 2020-07-28 stsp }
11875 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
11876 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
11877 b2118c49 2020-07-28 stsp if (error)
11878 b2118c49 2020-07-28 stsp goto done;
11879 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
11880 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
11881 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
11882 b2118c49 2020-07-28 stsp break;
11883 b2118c49 2020-07-28 stsp }
11884 b2118c49 2020-07-28 stsp }
11885 b2118c49 2020-07-28 stsp }
11886 b2118c49 2020-07-28 stsp done:
11887 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
11888 b2118c49 2020-07-28 stsp free((char *)pe->path);
11889 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
11890 b2118c49 2020-07-28 stsp free(cwd);
11891 b2118c49 2020-07-28 stsp free(id_str);
11892 b2118c49 2020-07-28 stsp free(uuidstr);
11893 0ebf8283 2019-07-24 stsp return error;
11894 0ebf8283 2019-07-24 stsp }