Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 93658fb9 2020-03-18 stsp * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 5c860e29 2018-03-12 stsp *
6 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
7 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
8 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
9 5c860e29 2018-03-12 stsp *
10 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 5c860e29 2018-03-12 stsp */
18 5c860e29 2018-03-12 stsp
19 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
22 3c45a30a 2019-05-12 jcs #include <sys/param.h>
23 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
24 f42b1b34 2018-03-12 stsp
25 5c860e29 2018-03-12 stsp #include <err.h>
26 5c860e29 2018-03-12 stsp #include <errno.h>
27 12463d8b 2019-12-13 stsp #include <fcntl.h>
28 12ce7a6c 2019-08-12 stsp #include <limits.h>
29 5c860e29 2018-03-12 stsp #include <locale.h>
30 818c7501 2019-07-11 stsp #include <ctype.h>
31 99437157 2018-11-11 stsp #include <signal.h>
32 5c860e29 2018-03-12 stsp #include <stdio.h>
33 5c860e29 2018-03-12 stsp #include <stdlib.h>
34 5c860e29 2018-03-12 stsp #include <string.h>
35 5c860e29 2018-03-12 stsp #include <unistd.h>
36 c09a553d 2018-03-12 stsp #include <libgen.h>
37 c0768b0f 2018-06-10 stsp #include <time.h>
38 33ad4cbe 2019-05-12 jcs #include <paths.h>
39 6841bf13 2019-11-29 kn #include <regex.h>
40 83cd27f8 2020-01-13 stsp #include <getopt.h>
41 d2cdc636 2020-03-18 stsp #include <util.h>
42 5c860e29 2018-03-12 stsp
43 53ccebc2 2019-07-30 stsp #include "got_version.h"
44 f42b1b34 2018-03-12 stsp #include "got_error.h"
45 f42b1b34 2018-03-12 stsp #include "got_object.h"
46 5261c201 2018-04-01 stsp #include "got_reference.h"
47 f42b1b34 2018-03-12 stsp #include "got_repository.h"
48 1dd54920 2019-05-11 stsp #include "got_path.h"
49 e6209546 2019-08-22 stsp #include "got_cancel.h"
50 c09a553d 2018-03-12 stsp #include "got_worktree.h"
51 79109fed 2018-03-27 stsp #include "got_diff.h"
52 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
53 6f23baec 2020-03-18 stsp #include "got_fetch.h"
54 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 5c860e29 2018-03-12 stsp
59 5c860e29 2018-03-12 stsp #ifndef nitems
60 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 5c860e29 2018-03-12 stsp #endif
62 99437157 2018-11-11 stsp
63 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
64 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
65 99437157 2018-11-11 stsp
66 99437157 2018-11-11 stsp static void
67 99437157 2018-11-11 stsp catch_sigint(int signo)
68 99437157 2018-11-11 stsp {
69 99437157 2018-11-11 stsp sigint_received = 1;
70 99437157 2018-11-11 stsp }
71 99437157 2018-11-11 stsp
72 99437157 2018-11-11 stsp static void
73 99437157 2018-11-11 stsp catch_sigpipe(int signo)
74 99437157 2018-11-11 stsp {
75 99437157 2018-11-11 stsp sigpipe_received = 1;
76 99437157 2018-11-11 stsp }
77 5c860e29 2018-03-12 stsp
78 99437157 2018-11-11 stsp
79 8cfb4057 2019-07-09 stsp struct got_cmd {
80 820059fa 2020-09-25 stsp const char *cmd_name;
81 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
82 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
83 97b3a7be 2019-07-09 stsp const char *cmd_alias;
84 5c860e29 2018-03-12 stsp };
85 5c860e29 2018-03-12 stsp
86 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
87 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
88 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
89 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
90 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
91 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
92 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
93 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
94 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
95 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
96 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
97 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
98 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
99 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
100 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
101 d00136be 2019-03-26 stsp __dead static void usage_add(void);
102 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
103 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
104 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
105 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
106 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
107 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
108 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
109 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
110 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
111 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
112 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
113 b2118c49 2020-07-28 stsp __dead static void usage_info(void);
114 5c860e29 2018-03-12 stsp
115 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
116 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
117 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
118 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
119 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
120 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
121 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
122 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
123 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
124 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
125 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
126 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
127 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
128 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
129 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
130 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
131 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
132 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
133 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
134 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
135 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
136 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
137 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
138 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
139 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
140 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
141 b2118c49 2020-07-28 stsp static const struct got_error* cmd_info(int, char *[]);
142 5c860e29 2018-03-12 stsp
143 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
144 b2118c49 2020-07-28 stsp { "init", cmd_init, usage_init, "" },
145 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
146 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
147 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
148 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
149 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
150 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
151 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
152 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
153 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
154 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
155 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
156 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
157 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
158 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
159 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
160 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
161 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
162 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
163 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
164 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
165 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
166 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
167 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
168 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
169 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
170 b2118c49 2020-07-28 stsp { "info", cmd_info, usage_info, "" },
171 5c860e29 2018-03-12 stsp };
172 ce5b7c56 2019-07-09 stsp
173 ce5b7c56 2019-07-09 stsp static void
174 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
175 ce5b7c56 2019-07-09 stsp {
176 6059809a 2020-12-17 stsp size_t i;
177 ce5b7c56 2019-07-09 stsp
178 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
179 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
180 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
181 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->cmd_name);
182 ce5b7c56 2019-07-09 stsp }
183 6879ba42 2020-10-01 naddy fputc('\n', fp);
184 ce5b7c56 2019-07-09 stsp }
185 5c860e29 2018-03-12 stsp
186 ff69268e 2020-12-13 stsp __dead static void
187 ff69268e 2020-12-13 stsp option_conflict(char a, char b)
188 ff69268e 2020-12-13 stsp {
189 ff69268e 2020-12-13 stsp errx(1, "-%c and -%c options are mutually exclusive", a, b);
190 ff69268e 2020-12-13 stsp }
191 ff69268e 2020-12-13 stsp
192 5c860e29 2018-03-12 stsp int
193 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
194 5c860e29 2018-03-12 stsp {
195 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
196 6059809a 2020-12-17 stsp size_t i;
197 5c860e29 2018-03-12 stsp int ch;
198 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
199 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
200 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
201 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0 }
202 83cd27f8 2020-01-13 stsp };
203 5c860e29 2018-03-12 stsp
204 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
205 5c860e29 2018-03-12 stsp
206 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
207 5c860e29 2018-03-12 stsp switch (ch) {
208 1b6b95a8 2018-03-12 stsp case 'h':
209 1b6b95a8 2018-03-12 stsp hflag = 1;
210 53ccebc2 2019-07-30 stsp break;
211 53ccebc2 2019-07-30 stsp case 'V':
212 53ccebc2 2019-07-30 stsp Vflag = 1;
213 1b6b95a8 2018-03-12 stsp break;
214 5c860e29 2018-03-12 stsp default:
215 6879ba42 2020-10-01 naddy usage(hflag, 1);
216 5c860e29 2018-03-12 stsp /* NOTREACHED */
217 5c860e29 2018-03-12 stsp }
218 5c860e29 2018-03-12 stsp }
219 5c860e29 2018-03-12 stsp
220 5c860e29 2018-03-12 stsp argc -= optind;
221 5c860e29 2018-03-12 stsp argv += optind;
222 9814e6a3 2020-09-27 naddy optind = 1;
223 9814e6a3 2020-09-27 naddy optreset = 1;
224 53ccebc2 2019-07-30 stsp
225 53ccebc2 2019-07-30 stsp if (Vflag) {
226 53ccebc2 2019-07-30 stsp got_version_print_str();
227 6879ba42 2020-10-01 naddy return 0;
228 53ccebc2 2019-07-30 stsp }
229 5c860e29 2018-03-12 stsp
230 5c860e29 2018-03-12 stsp if (argc <= 0)
231 6879ba42 2020-10-01 naddy usage(hflag, hflag ? 0 : 1);
232 5c860e29 2018-03-12 stsp
233 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
234 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
235 99437157 2018-11-11 stsp
236 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
237 d7d4f210 2018-03-12 stsp const struct got_error *error;
238 d7d4f210 2018-03-12 stsp
239 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
240 5c860e29 2018-03-12 stsp
241 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
242 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
243 5c860e29 2018-03-12 stsp continue;
244 5c860e29 2018-03-12 stsp
245 1b6b95a8 2018-03-12 stsp if (hflag)
246 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
247 1b6b95a8 2018-03-12 stsp
248 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
249 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
250 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
251 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
252 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
253 70015d7a 2019-11-08 stsp !(sigint_received &&
254 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
255 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
256 d7d4f210 2018-03-12 stsp return 1;
257 d7d4f210 2018-03-12 stsp }
258 d7d4f210 2018-03-12 stsp
259 d7d4f210 2018-03-12 stsp return 0;
260 5c860e29 2018-03-12 stsp }
261 5c860e29 2018-03-12 stsp
262 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
263 6879ba42 2020-10-01 naddy list_commands(stderr);
264 5c860e29 2018-03-12 stsp return 1;
265 5c860e29 2018-03-12 stsp }
266 5c860e29 2018-03-12 stsp
267 4ed7e80c 2018-05-20 stsp __dead static void
268 6879ba42 2020-10-01 naddy usage(int hflag, int status)
269 5c860e29 2018-03-12 stsp {
270 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
271 6879ba42 2020-10-01 naddy
272 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
273 53ccebc2 2019-07-30 stsp getprogname());
274 ce5b7c56 2019-07-09 stsp if (hflag)
275 6879ba42 2020-10-01 naddy list_commands(fp);
276 6879ba42 2020-10-01 naddy exit(status);
277 5c860e29 2018-03-12 stsp }
278 5c860e29 2018-03-12 stsp
279 0266afb7 2019-01-04 stsp static const struct got_error *
280 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
281 e2ba3d07 2019-05-13 stsp {
282 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
283 e2ba3d07 2019-05-13 stsp const char *editor;
284 8920fa04 2019-08-18 stsp
285 8920fa04 2019-08-18 stsp *abspath = NULL;
286 e2ba3d07 2019-05-13 stsp
287 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
288 e2ba3d07 2019-05-13 stsp if (editor == NULL)
289 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
290 e2ba3d07 2019-05-13 stsp
291 0ee7065d 2019-05-13 stsp if (editor) {
292 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
293 0ee7065d 2019-05-13 stsp if (err)
294 0ee7065d 2019-05-13 stsp return err;
295 0ee7065d 2019-05-13 stsp }
296 e2ba3d07 2019-05-13 stsp
297 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
298 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
299 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
300 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
301 0ee7065d 2019-05-13 stsp }
302 0ee7065d 2019-05-13 stsp
303 e2ba3d07 2019-05-13 stsp return NULL;
304 e2ba3d07 2019-05-13 stsp }
305 e2ba3d07 2019-05-13 stsp
306 e2ba3d07 2019-05-13 stsp static const struct got_error *
307 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
308 c530dc23 2019-07-23 stsp const char *worktree_path)
309 0266afb7 2019-01-04 stsp {
310 163ce85a 2019-05-13 stsp const struct got_error *err;
311 0266afb7 2019-01-04 stsp
312 37c06ea4 2019-07-15 stsp #ifdef PROFILE
313 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
314 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
315 37c06ea4 2019-07-15 stsp #endif
316 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
317 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
318 0266afb7 2019-01-04 stsp
319 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
320 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
321 0266afb7 2019-01-04 stsp
322 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
323 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
324 0266afb7 2019-01-04 stsp
325 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
326 163ce85a 2019-05-13 stsp if (err != NULL)
327 163ce85a 2019-05-13 stsp return err;
328 0266afb7 2019-01-04 stsp
329 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
330 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
331 0266afb7 2019-01-04 stsp
332 0266afb7 2019-01-04 stsp return NULL;
333 2c7829a4 2019-06-17 stsp }
334 2c7829a4 2019-06-17 stsp
335 2c7829a4 2019-06-17 stsp __dead static void
336 2c7829a4 2019-06-17 stsp usage_init(void)
337 2c7829a4 2019-06-17 stsp {
338 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
339 2c7829a4 2019-06-17 stsp exit(1);
340 2c7829a4 2019-06-17 stsp }
341 2c7829a4 2019-06-17 stsp
342 2c7829a4 2019-06-17 stsp static const struct got_error *
343 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
344 2c7829a4 2019-06-17 stsp {
345 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
346 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
347 2c7829a4 2019-06-17 stsp int ch;
348 2c7829a4 2019-06-17 stsp
349 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
350 2c7829a4 2019-06-17 stsp switch (ch) {
351 2c7829a4 2019-06-17 stsp default:
352 2c7829a4 2019-06-17 stsp usage_init();
353 2c7829a4 2019-06-17 stsp /* NOTREACHED */
354 2c7829a4 2019-06-17 stsp }
355 2c7829a4 2019-06-17 stsp }
356 2c7829a4 2019-06-17 stsp
357 2c7829a4 2019-06-17 stsp argc -= optind;
358 2c7829a4 2019-06-17 stsp argv += optind;
359 2c7829a4 2019-06-17 stsp
360 2c7829a4 2019-06-17 stsp #ifndef PROFILE
361 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
362 2c7829a4 2019-06-17 stsp err(1, "pledge");
363 2c7829a4 2019-06-17 stsp #endif
364 2c7829a4 2019-06-17 stsp if (argc != 1)
365 bc20e173 2019-06-17 stsp usage_init();
366 2c7829a4 2019-06-17 stsp
367 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
368 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
369 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
370 2c7829a4 2019-06-17 stsp
371 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
372 2c7829a4 2019-06-17 stsp
373 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
374 2c7829a4 2019-06-17 stsp if (error &&
375 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
376 2c7829a4 2019-06-17 stsp goto done;
377 2c7829a4 2019-06-17 stsp
378 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
379 2c7829a4 2019-06-17 stsp if (error)
380 2c7829a4 2019-06-17 stsp goto done;
381 2c7829a4 2019-06-17 stsp
382 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
383 3ce1b845 2019-07-15 stsp done:
384 3ce1b845 2019-07-15 stsp free(repo_path);
385 3ce1b845 2019-07-15 stsp return error;
386 3ce1b845 2019-07-15 stsp }
387 3ce1b845 2019-07-15 stsp
388 3ce1b845 2019-07-15 stsp __dead static void
389 3ce1b845 2019-07-15 stsp usage_import(void)
390 3ce1b845 2019-07-15 stsp {
391 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
392 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
393 3ce1b845 2019-07-15 stsp exit(1);
394 3ce1b845 2019-07-15 stsp }
395 3ce1b845 2019-07-15 stsp
396 3ce1b845 2019-07-15 stsp int
397 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
398 3ce1b845 2019-07-15 stsp {
399 3ce1b845 2019-07-15 stsp pid_t pid;
400 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
401 3ce1b845 2019-07-15 stsp int st = -1;
402 3ce1b845 2019-07-15 stsp
403 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
404 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
405 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
406 3ce1b845 2019-07-15 stsp
407 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
408 3ce1b845 2019-07-15 stsp case -1:
409 3ce1b845 2019-07-15 stsp goto doneediting;
410 3ce1b845 2019-07-15 stsp case 0:
411 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
412 3ce1b845 2019-07-15 stsp _exit(127);
413 3ce1b845 2019-07-15 stsp }
414 3ce1b845 2019-07-15 stsp
415 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
416 3ce1b845 2019-07-15 stsp if (errno != EINTR)
417 3ce1b845 2019-07-15 stsp break;
418 3ce1b845 2019-07-15 stsp
419 3ce1b845 2019-07-15 stsp doneediting:
420 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
421 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
422 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
423 3ce1b845 2019-07-15 stsp
424 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
425 3ce1b845 2019-07-15 stsp errno = EINTR;
426 3ce1b845 2019-07-15 stsp return -1;
427 3ce1b845 2019-07-15 stsp }
428 3ce1b845 2019-07-15 stsp
429 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
430 3ce1b845 2019-07-15 stsp }
431 3ce1b845 2019-07-15 stsp
432 3ce1b845 2019-07-15 stsp static const struct got_error *
433 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
434 28cf319f 2021-01-28 stsp const char *initial_content, size_t initial_content_len,
435 28cf319f 2021-01-28 stsp int require_modification)
436 3ce1b845 2019-07-15 stsp {
437 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
438 bfa12d5e 2020-09-26 stsp char *line = NULL;
439 bfa12d5e 2020-09-26 stsp size_t linesize = 0;
440 bfa12d5e 2020-09-26 stsp ssize_t linelen;
441 3ce1b845 2019-07-15 stsp struct stat st, st2;
442 bfa12d5e 2020-09-26 stsp FILE *fp = NULL;
443 bfa12d5e 2020-09-26 stsp size_t len, logmsg_len;
444 bfa12d5e 2020-09-26 stsp char *initial_content_stripped = NULL, *buf = NULL, *s;
445 3ce1b845 2019-07-15 stsp
446 3ce1b845 2019-07-15 stsp *logmsg = NULL;
447 3ce1b845 2019-07-15 stsp
448 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
449 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
450 3ce1b845 2019-07-15 stsp
451 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
452 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
453 3ce1b845 2019-07-15 stsp
454 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
455 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
456 3ce1b845 2019-07-15 stsp
457 28cf319f 2021-01-28 stsp if (require_modification &&
458 28cf319f 2021-01-28 stsp st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
459 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
460 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
461 3ce1b845 2019-07-15 stsp
462 bfa12d5e 2020-09-26 stsp /*
463 bfa12d5e 2020-09-26 stsp * Set up a stripped version of the initial content without comments
464 bfa12d5e 2020-09-26 stsp * and blank lines. We need this in order to check if the message
465 bfa12d5e 2020-09-26 stsp * has in fact been edited.
466 bfa12d5e 2020-09-26 stsp */
467 bfa12d5e 2020-09-26 stsp initial_content_stripped = malloc(initial_content_len + 1);
468 bfa12d5e 2020-09-26 stsp if (initial_content_stripped == NULL)
469 bfa12d5e 2020-09-26 stsp return got_error_from_errno("malloc");
470 bfa12d5e 2020-09-26 stsp initial_content_stripped[0] = '\0';
471 bfa12d5e 2020-09-26 stsp
472 bfa12d5e 2020-09-26 stsp buf = strdup(initial_content);
473 bfa12d5e 2020-09-26 stsp if (buf == NULL) {
474 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("strdup");
475 bfa12d5e 2020-09-26 stsp goto done;
476 bfa12d5e 2020-09-26 stsp }
477 bfa12d5e 2020-09-26 stsp s = buf;
478 bfa12d5e 2020-09-26 stsp len = 0;
479 bfa12d5e 2020-09-26 stsp while ((line = strsep(&s, "\n")) != NULL) {
480 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
481 bfa12d5e 2020-09-26 stsp continue; /* remove comments and leading empty lines */
482 bfa12d5e 2020-09-26 stsp len = strlcat(initial_content_stripped, line,
483 bfa12d5e 2020-09-26 stsp initial_content_len + 1);
484 bfa12d5e 2020-09-26 stsp if (len >= initial_content_len + 1) {
485 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
486 bfa12d5e 2020-09-26 stsp goto done;
487 bfa12d5e 2020-09-26 stsp }
488 bfa12d5e 2020-09-26 stsp }
489 bfa12d5e 2020-09-26 stsp while (len > 0 && initial_content_stripped[len - 1] == '\n') {
490 bfa12d5e 2020-09-26 stsp initial_content_stripped[len - 1] = '\0';
491 bfa12d5e 2020-09-26 stsp len--;
492 bfa12d5e 2020-09-26 stsp }
493 bfa12d5e 2020-09-26 stsp
494 bfa12d5e 2020-09-26 stsp logmsg_len = st2.st_size;
495 bfa12d5e 2020-09-26 stsp *logmsg = malloc(logmsg_len + 1);
496 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
497 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
498 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
499 3ce1b845 2019-07-15 stsp
500 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
501 3ce1b845 2019-07-15 stsp if (fp == NULL) {
502 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
503 3ce1b845 2019-07-15 stsp goto done;
504 3ce1b845 2019-07-15 stsp }
505 bfa12d5e 2020-09-26 stsp
506 bfa12d5e 2020-09-26 stsp len = 0;
507 bfa12d5e 2020-09-26 stsp while ((linelen = getline(&line, &linesize, fp)) != -1) {
508 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
509 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
510 bfa12d5e 2020-09-26 stsp len = strlcat(*logmsg, line, logmsg_len + 1);
511 bfa12d5e 2020-09-26 stsp if (len >= logmsg_len + 1) {
512 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
513 bfa12d5e 2020-09-26 stsp goto done;
514 bfa12d5e 2020-09-26 stsp }
515 3ce1b845 2019-07-15 stsp }
516 bfa12d5e 2020-09-26 stsp free(line);
517 bfa12d5e 2020-09-26 stsp if (ferror(fp)) {
518 bfa12d5e 2020-09-26 stsp err = got_ferror(fp, GOT_ERR_IO);
519 bfa12d5e 2020-09-26 stsp goto done;
520 bfa12d5e 2020-09-26 stsp }
521 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
522 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
523 3ce1b845 2019-07-15 stsp len--;
524 3ce1b845 2019-07-15 stsp }
525 3ce1b845 2019-07-15 stsp
526 bfa12d5e 2020-09-26 stsp if (len == 0) {
527 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
528 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
529 bfa12d5e 2020-09-26 stsp goto done;
530 bfa12d5e 2020-09-26 stsp }
531 28cf319f 2021-01-28 stsp if (require_modification &&
532 28cf319f 2021-01-28 stsp strcmp(*logmsg, initial_content_stripped) == 0)
533 bfa12d5e 2020-09-26 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
534 bfa12d5e 2020-09-26 stsp "no changes made to commit message, aborting");
535 3ce1b845 2019-07-15 stsp done:
536 bfa12d5e 2020-09-26 stsp free(initial_content_stripped);
537 bfa12d5e 2020-09-26 stsp free(buf);
538 bfa12d5e 2020-09-26 stsp if (fp && fclose(fp) == EOF && err == NULL)
539 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("fclose");
540 3ce1b845 2019-07-15 stsp if (err) {
541 3ce1b845 2019-07-15 stsp free(*logmsg);
542 3ce1b845 2019-07-15 stsp *logmsg = NULL;
543 3ce1b845 2019-07-15 stsp }
544 3ce1b845 2019-07-15 stsp return err;
545 3ce1b845 2019-07-15 stsp }
546 3ce1b845 2019-07-15 stsp
547 3ce1b845 2019-07-15 stsp static const struct got_error *
548 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
549 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
550 3ce1b845 2019-07-15 stsp {
551 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
552 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
553 1601cb9f 2020-09-11 naddy int initial_content_len;
554 97972933 2020-09-11 stsp int fd = -1;
555 3ce1b845 2019-07-15 stsp
556 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
557 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
558 1601cb9f 2020-09-11 naddy branch_name);
559 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
560 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
561 3ce1b845 2019-07-15 stsp
562 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
563 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
564 3ce1b845 2019-07-15 stsp if (err)
565 3ce1b845 2019-07-15 stsp goto done;
566 3ce1b845 2019-07-15 stsp
567 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
568 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
569 97972933 2020-09-11 stsp goto done;
570 97972933 2020-09-11 stsp }
571 3ce1b845 2019-07-15 stsp
572 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
573 0d5bb276 2020-12-15 stsp initial_content_len, 1);
574 3ce1b845 2019-07-15 stsp done:
575 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
576 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
577 3ce1b845 2019-07-15 stsp free(initial_content);
578 59f86c76 2020-09-11 stsp if (err) {
579 59f86c76 2020-09-11 stsp free(*logmsg_path);
580 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
581 59f86c76 2020-09-11 stsp }
582 3ce1b845 2019-07-15 stsp return err;
583 3ce1b845 2019-07-15 stsp }
584 3ce1b845 2019-07-15 stsp
585 3ce1b845 2019-07-15 stsp static const struct got_error *
586 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
587 3ce1b845 2019-07-15 stsp {
588 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
589 3ce1b845 2019-07-15 stsp return NULL;
590 3ce1b845 2019-07-15 stsp }
591 3ce1b845 2019-07-15 stsp
592 3ce1b845 2019-07-15 stsp static const struct got_error *
593 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
594 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
595 84792843 2019-08-09 stsp {
596 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
597 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
598 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
599 84792843 2019-08-09 stsp
600 84792843 2019-08-09 stsp *author = NULL;
601 aba9c984 2019-09-08 stsp
602 50b0790e 2020-09-11 stsp if (worktree)
603 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
604 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
605 50b0790e 2020-09-11 stsp
606 50b0790e 2020-09-11 stsp /*
607 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
608 50b0790e 2020-09-11 stsp * significant to least significant:
609 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
610 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
611 50b0790e 2020-09-11 stsp * 3) repository's git config file
612 50b0790e 2020-09-11 stsp * 4) environment variables
613 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
614 50b0790e 2020-09-11 stsp */
615 50b0790e 2020-09-11 stsp
616 50b0790e 2020-09-11 stsp if (worktree_conf)
617 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
618 50b0790e 2020-09-11 stsp if (got_author == NULL)
619 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
620 84792843 2019-08-09 stsp if (got_author == NULL) {
621 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
622 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
623 c9956ddf 2019-09-08 stsp if (name && email) {
624 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
625 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
626 c9956ddf 2019-09-08 stsp return NULL;
627 c9956ddf 2019-09-08 stsp }
628 257add31 2020-09-09 stsp
629 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
630 257add31 2020-09-09 stsp if (got_author == NULL) {
631 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
632 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
633 257add31 2020-09-09 stsp repo);
634 257add31 2020-09-09 stsp if (name && email) {
635 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
636 257add31 2020-09-09 stsp == -1)
637 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
638 257add31 2020-09-09 stsp return NULL;
639 257add31 2020-09-09 stsp }
640 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
641 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
642 257add31 2020-09-09 stsp }
643 84792843 2019-08-09 stsp }
644 84792843 2019-08-09 stsp
645 aba9c984 2019-09-08 stsp *author = strdup(got_author);
646 aba9c984 2019-09-08 stsp if (*author == NULL)
647 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
648 84792843 2019-08-09 stsp
649 84792843 2019-08-09 stsp /*
650 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
651 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
652 84792843 2019-08-09 stsp */
653 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
654 84792843 2019-08-09 stsp got_author++;
655 aba9c984 2019-09-08 stsp if (*got_author != '<') {
656 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
657 aba9c984 2019-09-08 stsp goto done;
658 aba9c984 2019-09-08 stsp }
659 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
660 84792843 2019-08-09 stsp got_author++;
661 aba9c984 2019-09-08 stsp if (*got_author != '@') {
662 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
663 aba9c984 2019-09-08 stsp goto done;
664 aba9c984 2019-09-08 stsp }
665 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
666 84792843 2019-08-09 stsp got_author++;
667 84792843 2019-08-09 stsp if (*got_author != '>')
668 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
669 aba9c984 2019-09-08 stsp done:
670 aba9c984 2019-09-08 stsp if (err) {
671 aba9c984 2019-09-08 stsp free(*author);
672 aba9c984 2019-09-08 stsp *author = NULL;
673 aba9c984 2019-09-08 stsp }
674 aba9c984 2019-09-08 stsp return err;
675 c9956ddf 2019-09-08 stsp }
676 c9956ddf 2019-09-08 stsp
677 c9956ddf 2019-09-08 stsp static const struct got_error *
678 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
679 c9956ddf 2019-09-08 stsp {
680 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
681 c9956ddf 2019-09-08 stsp
682 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
683 c9956ddf 2019-09-08 stsp if (homedir) {
684 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
685 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
686 c9956ddf 2019-09-08 stsp
687 c9956ddf 2019-09-08 stsp }
688 c9956ddf 2019-09-08 stsp return NULL;
689 84792843 2019-08-09 stsp }
690 84792843 2019-08-09 stsp
691 84792843 2019-08-09 stsp static const struct got_error *
692 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
693 3ce1b845 2019-07-15 stsp {
694 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
695 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
696 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
697 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
698 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
699 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
700 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
701 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
702 3ce1b845 2019-07-15 stsp int ch;
703 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
704 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
705 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
706 3ce1b845 2019-07-15 stsp
707 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
708 3ce1b845 2019-07-15 stsp
709 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
710 3ce1b845 2019-07-15 stsp switch (ch) {
711 3ce1b845 2019-07-15 stsp case 'b':
712 3ce1b845 2019-07-15 stsp branch_name = optarg;
713 3ce1b845 2019-07-15 stsp break;
714 3ce1b845 2019-07-15 stsp case 'm':
715 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
716 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
717 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
718 3ce1b845 2019-07-15 stsp goto done;
719 3ce1b845 2019-07-15 stsp }
720 3ce1b845 2019-07-15 stsp break;
721 3ce1b845 2019-07-15 stsp case 'r':
722 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
723 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
724 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
725 9ba1d308 2019-10-21 stsp optarg);
726 3ce1b845 2019-07-15 stsp goto done;
727 3ce1b845 2019-07-15 stsp }
728 3ce1b845 2019-07-15 stsp break;
729 3ce1b845 2019-07-15 stsp case 'I':
730 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
731 3ce1b845 2019-07-15 stsp break;
732 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
733 3ce1b845 2019-07-15 stsp NULL);
734 3ce1b845 2019-07-15 stsp if (error)
735 3ce1b845 2019-07-15 stsp goto done;
736 3ce1b845 2019-07-15 stsp break;
737 3ce1b845 2019-07-15 stsp default:
738 b2b65d18 2019-08-22 stsp usage_import();
739 3ce1b845 2019-07-15 stsp /* NOTREACHED */
740 3ce1b845 2019-07-15 stsp }
741 3ce1b845 2019-07-15 stsp }
742 3ce1b845 2019-07-15 stsp
743 3ce1b845 2019-07-15 stsp argc -= optind;
744 3ce1b845 2019-07-15 stsp argv += optind;
745 3ce1b845 2019-07-15 stsp
746 3ce1b845 2019-07-15 stsp #ifndef PROFILE
747 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
748 aba9c984 2019-09-08 stsp "unveil",
749 3ce1b845 2019-07-15 stsp NULL) == -1)
750 3ce1b845 2019-07-15 stsp err(1, "pledge");
751 3ce1b845 2019-07-15 stsp #endif
752 3ce1b845 2019-07-15 stsp if (argc != 1)
753 3ce1b845 2019-07-15 stsp usage_import();
754 2c7829a4 2019-06-17 stsp
755 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
756 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
757 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
758 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
759 3ce1b845 2019-07-15 stsp }
760 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
761 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
762 c9956ddf 2019-09-08 stsp if (error)
763 c9956ddf 2019-09-08 stsp goto done;
764 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
765 3ce1b845 2019-07-15 stsp if (error)
766 3ce1b845 2019-07-15 stsp goto done;
767 aba9c984 2019-09-08 stsp
768 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
769 aba9c984 2019-09-08 stsp if (error)
770 aba9c984 2019-09-08 stsp return error;
771 e560b7e0 2019-11-28 stsp
772 e560b7e0 2019-11-28 stsp /*
773 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
774 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
775 e560b7e0 2019-11-28 stsp * an unintended typo.
776 e560b7e0 2019-11-28 stsp */
777 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
778 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
779 3ce1b845 2019-07-15 stsp
780 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
781 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
782 3ce1b845 2019-07-15 stsp goto done;
783 3ce1b845 2019-07-15 stsp }
784 3ce1b845 2019-07-15 stsp
785 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
786 3ce1b845 2019-07-15 stsp if (error) {
787 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
788 3ce1b845 2019-07-15 stsp goto done;
789 3ce1b845 2019-07-15 stsp } else {
790 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
791 3ce1b845 2019-07-15 stsp "import target branch already exists");
792 3ce1b845 2019-07-15 stsp goto done;
793 3ce1b845 2019-07-15 stsp }
794 3ce1b845 2019-07-15 stsp
795 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
796 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
797 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
798 3ce1b845 2019-07-15 stsp goto done;
799 3ce1b845 2019-07-15 stsp }
800 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
801 3ce1b845 2019-07-15 stsp
802 3ce1b845 2019-07-15 stsp /*
803 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
804 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
805 3ce1b845 2019-07-15 stsp */
806 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
807 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
808 3ce1b845 2019-07-15 stsp if (error)
809 3ce1b845 2019-07-15 stsp goto done;
810 8e158b01 2019-09-22 stsp free(logmsg);
811 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
812 ef293bdd 2019-10-21 stsp path_dir, refname);
813 ef293bdd 2019-10-21 stsp if (error) {
814 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
815 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
816 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
817 3ce1b845 2019-07-15 stsp goto done;
818 ef293bdd 2019-10-21 stsp }
819 3ce1b845 2019-07-15 stsp }
820 3ce1b845 2019-07-15 stsp
821 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
822 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
823 ef293bdd 2019-10-21 stsp if (logmsg_path)
824 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
825 3ce1b845 2019-07-15 stsp goto done;
826 ef293bdd 2019-10-21 stsp }
827 3ce1b845 2019-07-15 stsp
828 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
829 ef293bdd 2019-10-21 stsp if (error) {
830 ef293bdd 2019-10-21 stsp if (logmsg_path)
831 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
832 ef293bdd 2019-10-21 stsp goto done;
833 ef293bdd 2019-10-21 stsp }
834 ef293bdd 2019-10-21 stsp
835 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
836 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
837 ef293bdd 2019-10-21 stsp if (error) {
838 ef293bdd 2019-10-21 stsp if (logmsg_path)
839 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
840 3ce1b845 2019-07-15 stsp goto done;
841 ef293bdd 2019-10-21 stsp }
842 3ce1b845 2019-07-15 stsp
843 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
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_write(branch_ref, repo);
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_object_id_str(&id_str, new_commit_id);
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_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
865 3ce1b845 2019-07-15 stsp if (error) {
866 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
867 ef293bdd 2019-10-21 stsp if (logmsg_path)
868 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
869 3ce1b845 2019-07-15 stsp goto done;
870 ef293bdd 2019-10-21 stsp }
871 3ce1b845 2019-07-15 stsp
872 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
873 3ce1b845 2019-07-15 stsp branch_ref);
874 ef293bdd 2019-10-21 stsp if (error) {
875 ef293bdd 2019-10-21 stsp if (logmsg_path)
876 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
877 3ce1b845 2019-07-15 stsp goto done;
878 ef293bdd 2019-10-21 stsp }
879 3ce1b845 2019-07-15 stsp
880 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
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
888 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
889 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
890 2c7829a4 2019-06-17 stsp done:
891 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
892 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
893 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
894 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
895 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
896 8e158b01 2019-09-22 stsp free(logmsg);
897 ef293bdd 2019-10-21 stsp free(logmsg_path);
898 2c7829a4 2019-06-17 stsp free(repo_path);
899 3ce1b845 2019-07-15 stsp free(editor);
900 3ce1b845 2019-07-15 stsp free(refname);
901 3ce1b845 2019-07-15 stsp free(new_commit_id);
902 3ce1b845 2019-07-15 stsp free(id_str);
903 aba9c984 2019-09-08 stsp free(author);
904 c9956ddf 2019-09-08 stsp free(gitconfig_path);
905 3ce1b845 2019-07-15 stsp if (branch_ref)
906 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
907 3ce1b845 2019-07-15 stsp if (head_ref)
908 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
909 2c7829a4 2019-06-17 stsp return error;
910 93658fb9 2020-03-18 stsp }
911 93658fb9 2020-03-18 stsp
912 93658fb9 2020-03-18 stsp __dead static void
913 93658fb9 2020-03-18 stsp usage_clone(void)
914 93658fb9 2020-03-18 stsp {
915 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
916 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
917 93658fb9 2020-03-18 stsp exit(1);
918 93658fb9 2020-03-18 stsp }
919 892ac3b6 2020-03-18 stsp
920 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
921 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
922 892ac3b6 2020-03-18 stsp int last_p_indexed;
923 892ac3b6 2020-03-18 stsp int last_p_resolved;
924 68999b92 2020-03-18 stsp int verbosity;
925 04d9a9ec 2020-09-24 stsp
926 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
927 04d9a9ec 2020-09-24 stsp
928 04d9a9ec 2020-09-24 stsp int create_configs;
929 04d9a9ec 2020-09-24 stsp int configs_created;
930 04d9a9ec 2020-09-24 stsp struct {
931 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
932 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
933 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs;
934 04d9a9ec 2020-09-24 stsp const char *proto;
935 04d9a9ec 2020-09-24 stsp const char *host;
936 04d9a9ec 2020-09-24 stsp const char *port;
937 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
938 04d9a9ec 2020-09-24 stsp const char *git_url;
939 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
940 04d9a9ec 2020-09-24 stsp int mirror_references;
941 04d9a9ec 2020-09-24 stsp } config_info;
942 892ac3b6 2020-03-18 stsp };
943 93658fb9 2020-03-18 stsp
944 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
945 93658fb9 2020-03-18 stsp static const struct got_error *
946 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
947 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
948 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
949 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
950 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo);
951 04d9a9ec 2020-09-24 stsp
952 04d9a9ec 2020-09-24 stsp static const struct got_error *
953 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
954 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
955 531c3985 2020-03-18 stsp {
956 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
957 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
958 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
959 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
960 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
961 04d9a9ec 2020-09-24 stsp
962 04d9a9ec 2020-09-24 stsp /*
963 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
964 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
965 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
966 04d9a9ec 2020-09-24 stsp * we have all required information.
967 04d9a9ec 2020-09-24 stsp */
968 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
969 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
970 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
971 62d463ca 2020-10-20 naddy a->config_info.host, a->config_info.port,
972 62d463ca 2020-10-20 naddy a->config_info.remote_repo_path,
973 62d463ca 2020-10-20 naddy a->config_info.git_url,
974 62d463ca 2020-10-20 naddy a->config_info.fetch_all_branches,
975 62d463ca 2020-10-20 naddy a->config_info.mirror_references,
976 62d463ca 2020-10-20 naddy a->config_info.symrefs,
977 99495ddb 2021-01-10 stsp a->config_info.wanted_branches,
978 99495ddb 2021-01-10 stsp a->config_info.wanted_refs, a->repo);
979 04d9a9ec 2020-09-24 stsp if (err)
980 04d9a9ec 2020-09-24 stsp return err;
981 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
982 04d9a9ec 2020-09-24 stsp }
983 b2409d58 2020-03-18 stsp
984 68999b92 2020-03-18 stsp if (a->verbosity < 0)
985 68999b92 2020-03-18 stsp return NULL;
986 68999b92 2020-03-18 stsp
987 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
988 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
989 892ac3b6 2020-03-18 stsp fflush(stdout);
990 12d1281e 2020-03-19 stsp return NULL;
991 b2409d58 2020-03-18 stsp }
992 b2409d58 2020-03-18 stsp
993 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
994 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
995 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
996 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
997 892ac3b6 2020-03-18 stsp print_size = 1;
998 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
999 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1000 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
1001 892ac3b6 2020-03-18 stsp }
1002 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
1003 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
1004 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
1005 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
1006 892ac3b6 2020-03-18 stsp print_indexed = 1;
1007 892ac3b6 2020-03-18 stsp print_size = 1;
1008 892ac3b6 2020-03-18 stsp }
1009 61cc1a7a 2020-03-18 stsp }
1010 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
1011 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
1012 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
1013 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
1014 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
1015 892ac3b6 2020-03-18 stsp print_resolved = 1;
1016 892ac3b6 2020-03-18 stsp print_indexed = 1;
1017 892ac3b6 2020-03-18 stsp print_size = 1;
1018 892ac3b6 2020-03-18 stsp }
1019 61cc1a7a 2020-03-18 stsp }
1020 3168e5da 2020-09-10 stsp
1021 d2cdc636 2020-03-18 stsp }
1022 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1023 892ac3b6 2020-03-18 stsp printf("\r");
1024 892ac3b6 2020-03-18 stsp if (print_size)
1025 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1026 d715f13e 2020-03-19 stsp if (print_indexed)
1027 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
1028 d715f13e 2020-03-19 stsp if (print_resolved)
1029 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
1030 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1031 892ac3b6 2020-03-18 stsp fflush(stdout);
1032 892ac3b6 2020-03-18 stsp
1033 531c3985 2020-03-18 stsp return NULL;
1034 531c3985 2020-03-18 stsp }
1035 531c3985 2020-03-18 stsp
1036 531c3985 2020-03-18 stsp static const struct got_error *
1037 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
1038 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
1039 4ba14133 2020-03-20 stsp {
1040 4ba14133 2020-03-20 stsp const struct got_error *err;
1041 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
1042 4ba14133 2020-03-20 stsp
1043 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1044 4ba14133 2020-03-20 stsp if (err)
1045 4ba14133 2020-03-20 stsp return err;
1046 4ba14133 2020-03-20 stsp
1047 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
1048 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
1049 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
1050 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
1051 6338a6a1 2020-03-21 stsp }
1052 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
1053 4ba14133 2020-03-20 stsp return err;
1054 4ba14133 2020-03-20 stsp }
1055 4ba14133 2020-03-20 stsp
1056 4ba14133 2020-03-20 stsp static const struct got_error *
1057 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
1058 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
1059 41b0de12 2020-03-21 stsp {
1060 41b0de12 2020-03-21 stsp const struct got_error *err;
1061 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
1062 41b0de12 2020-03-21 stsp
1063 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1064 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1065 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1066 41b0de12 2020-03-21 stsp
1067 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1068 41b0de12 2020-03-21 stsp }
1069 41b0de12 2020-03-21 stsp
1070 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1071 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1072 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1073 41b0de12 2020-03-21 stsp char *id_str;
1074 41b0de12 2020-03-21 stsp
1075 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1076 41b0de12 2020-03-21 stsp if (err)
1077 41b0de12 2020-03-21 stsp return err;
1078 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1079 41b0de12 2020-03-21 stsp free(id_str);
1080 41b0de12 2020-03-21 stsp }
1081 41b0de12 2020-03-21 stsp
1082 41b0de12 2020-03-21 stsp return NULL;
1083 6338a6a1 2020-03-21 stsp }
1084 6338a6a1 2020-03-21 stsp
1085 6338a6a1 2020-03-21 stsp static const struct got_error *
1086 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1087 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1088 6338a6a1 2020-03-21 stsp {
1089 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1090 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1091 6338a6a1 2020-03-21 stsp char *id_str;
1092 6338a6a1 2020-03-21 stsp
1093 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1094 6338a6a1 2020-03-21 stsp if (err)
1095 6338a6a1 2020-03-21 stsp return err;
1096 6338a6a1 2020-03-21 stsp
1097 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1098 6338a6a1 2020-03-21 stsp if (err)
1099 6338a6a1 2020-03-21 stsp goto done;
1100 6338a6a1 2020-03-21 stsp
1101 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1102 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1103 6338a6a1 2020-03-21 stsp
1104 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1105 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1106 6338a6a1 2020-03-21 stsp done:
1107 6338a6a1 2020-03-21 stsp free(id_str);
1108 6338a6a1 2020-03-21 stsp return err;
1109 0e4002ca 2020-03-21 stsp }
1110 0e4002ca 2020-03-21 stsp
1111 0e4002ca 2020-03-21 stsp static int
1112 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1113 0e4002ca 2020-03-21 stsp {
1114 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1115 0e4002ca 2020-03-21 stsp return 0;
1116 0e4002ca 2020-03-21 stsp refname += 5;
1117 0e4002ca 2020-03-21 stsp
1118 0e4002ca 2020-03-21 stsp /*
1119 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1120 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1121 0e4002ca 2020-03-21 stsp */
1122 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1123 0e4002ca 2020-03-21 stsp return 0;
1124 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1125 0e4002ca 2020-03-21 stsp return 0;
1126 0e4002ca 2020-03-21 stsp
1127 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1128 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1129 0e4002ca 2020-03-21 stsp
1130 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1131 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1132 0e4002ca 2020-03-21 stsp return 1;
1133 0e4002ca 2020-03-21 stsp
1134 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1135 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1136 41b0de12 2020-03-21 stsp }
1137 41b0de12 2020-03-21 stsp
1138 0e4002ca 2020-03-21 stsp static int
1139 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1140 0e4002ca 2020-03-21 stsp {
1141 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1142 0e4002ca 2020-03-21 stsp
1143 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1144 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1145 0e4002ca 2020-03-21 stsp return 1;
1146 0e4002ca 2020-03-21 stsp }
1147 0e4002ca 2020-03-21 stsp
1148 0e4002ca 2020-03-21 stsp return 0;
1149 0e4002ca 2020-03-21 stsp }
1150 0e4002ca 2020-03-21 stsp
1151 41b0de12 2020-03-21 stsp static const struct got_error *
1152 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1153 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1154 0e4002ca 2020-03-21 stsp {
1155 0e4002ca 2020-03-21 stsp const struct got_error *err;
1156 0e4002ca 2020-03-21 stsp char *remote_refname;
1157 0e4002ca 2020-03-21 stsp
1158 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1159 0e4002ca 2020-03-21 stsp refname += 5;
1160 0e4002ca 2020-03-21 stsp
1161 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1162 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1163 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1164 0e4002ca 2020-03-21 stsp
1165 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1166 0e4002ca 2020-03-21 stsp free(remote_refname);
1167 7c0b7f42 2020-09-24 stsp return err;
1168 7c0b7f42 2020-09-24 stsp }
1169 7c0b7f42 2020-09-24 stsp
1170 7c0b7f42 2020-09-24 stsp static const struct got_error *
1171 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1172 15d3c221 2021-01-05 stsp const char *remote_repo_path, const char *default_branch,
1173 0c8b29c5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1174 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1175 99495ddb 2021-01-10 stsp struct got_repository *repo)
1176 7c0b7f42 2020-09-24 stsp {
1177 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1178 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1179 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1180 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1181 15d3c221 2021-01-05 stsp const char *branchname = NULL;
1182 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1183 7c0b7f42 2020-09-24 stsp ssize_t n;
1184 7c0b7f42 2020-09-24 stsp
1185 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1186 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1187 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1188 132af4a5 2021-01-05 stsp char *s;
1189 132af4a5 2021-01-05 stsp branchname = pe->path;
1190 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1191 132af4a5 2021-01-05 stsp branchname += 11;
1192 132af4a5 2021-01-05 stsp if (asprintf(&s, "%s\"%s\" ",
1193 132af4a5 2021-01-05 stsp branches ? branches : "", branchname) == -1) {
1194 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1195 132af4a5 2021-01-05 stsp goto done;
1196 132af4a5 2021-01-05 stsp }
1197 132af4a5 2021-01-05 stsp free(branches);
1198 132af4a5 2021-01-05 stsp branches = s;
1199 132af4a5 2021-01-05 stsp }
1200 0c8b29c5 2021-01-05 stsp } else if (!fetch_all_branches && default_branch) {
1201 15d3c221 2021-01-05 stsp branchname = default_branch;
1202 15d3c221 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1203 15d3c221 2021-01-05 stsp branchname += 11;
1204 132af4a5 2021-01-05 stsp if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1205 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1206 132af4a5 2021-01-05 stsp goto done;
1207 99495ddb 2021-01-10 stsp }
1208 99495ddb 2021-01-10 stsp }
1209 99495ddb 2021-01-10 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1210 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1211 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1212 99495ddb 2021-01-10 stsp char *s;
1213 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1214 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1215 99495ddb 2021-01-10 stsp branchname += 5;
1216 99495ddb 2021-01-10 stsp if (asprintf(&s, "%s\"%s\" ",
1217 99495ddb 2021-01-10 stsp refs ? refs : "", refname) == -1) {
1218 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1219 99495ddb 2021-01-10 stsp goto done;
1220 99495ddb 2021-01-10 stsp }
1221 99495ddb 2021-01-10 stsp free(refs);
1222 99495ddb 2021-01-10 stsp refs = s;
1223 132af4a5 2021-01-05 stsp }
1224 15d3c221 2021-01-05 stsp }
1225 15d3c221 2021-01-05 stsp
1226 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1227 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1228 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1229 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1230 7c0b7f42 2020-09-24 stsp goto done;
1231 7c0b7f42 2020-09-24 stsp }
1232 7c0b7f42 2020-09-24 stsp gotconfig_file = fopen(gotconfig_path, "a");
1233 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1234 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1235 7c0b7f42 2020-09-24 stsp goto done;
1236 7c0b7f42 2020-09-24 stsp }
1237 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1238 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1239 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1240 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1241 7c0b7f42 2020-09-24 stsp "%s%s%s"
1242 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1243 15d3c221 2021-01-05 stsp "%s%s%s"
1244 99495ddb 2021-01-10 stsp "%s%s%s"
1245 7c0b7f42 2020-09-24 stsp "%s"
1246 0c8b29c5 2021-01-05 stsp "%s"
1247 7c0b7f42 2020-09-24 stsp "}\n",
1248 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1249 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1250 132af4a5 2021-01-05 stsp remote_repo_path, branches ? "\tbranch { " : "",
1251 132af4a5 2021-01-05 stsp branches ? branches : "", branches ? "}\n" : "",
1252 99495ddb 2021-01-10 stsp refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1253 0c8b29c5 2021-01-05 stsp mirror_references ? "\tmirror-references yes\n" : "",
1254 0c8b29c5 2021-01-05 stsp fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1255 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1256 7c0b7f42 2020-09-24 stsp goto done;
1257 7c0b7f42 2020-09-24 stsp }
1258 7c0b7f42 2020-09-24 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1259 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1260 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1261 7c0b7f42 2020-09-24 stsp goto done;
1262 7c0b7f42 2020-09-24 stsp }
1263 7c0b7f42 2020-09-24 stsp
1264 7c0b7f42 2020-09-24 stsp done:
1265 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1266 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1267 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1268 132af4a5 2021-01-05 stsp free(branches);
1269 7c0b7f42 2020-09-24 stsp return err;
1270 7c0b7f42 2020-09-24 stsp }
1271 7c0b7f42 2020-09-24 stsp
1272 7c0b7f42 2020-09-24 stsp static const struct got_error *
1273 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1274 132af4a5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1275 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1276 99495ddb 2021-01-10 stsp struct got_repository *repo)
1277 7c0b7f42 2020-09-24 stsp {
1278 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1279 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1280 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1281 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1282 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1283 56d0a753 2021-01-20 stsp const char *branchname;
1284 7c0b7f42 2020-09-24 stsp ssize_t n;
1285 7c0b7f42 2020-09-24 stsp
1286 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1287 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1288 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1289 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1290 7c0b7f42 2020-09-24 stsp goto done;
1291 7c0b7f42 2020-09-24 stsp }
1292 7c0b7f42 2020-09-24 stsp gitconfig_file = fopen(gitconfig_path, "a");
1293 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1294 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1295 7c0b7f42 2020-09-24 stsp goto done;
1296 7c0b7f42 2020-09-24 stsp }
1297 56d0a753 2021-01-20 stsp if (fetch_all_branches) {
1298 56d0a753 2021-01-20 stsp if (mirror_references) {
1299 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1300 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1301 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1302 56d0a753 2021-01-20 stsp goto done;
1303 56d0a753 2021-01-20 stsp }
1304 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1305 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1306 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1307 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1308 7c0b7f42 2020-09-24 stsp goto done;
1309 132af4a5 2021-01-05 stsp }
1310 132af4a5 2021-01-05 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
1311 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1312 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1313 132af4a5 2021-01-05 stsp char *s;
1314 132af4a5 2021-01-05 stsp branchname = pe->path;
1315 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1316 132af4a5 2021-01-05 stsp branchname += 11;
1317 56d0a753 2021-01-20 stsp if (mirror_references) {
1318 56d0a753 2021-01-20 stsp if (asprintf(&s,
1319 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1320 56d0a753 2021-01-20 stsp branches ? branches : "",
1321 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1322 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1323 56d0a753 2021-01-20 stsp goto done;
1324 56d0a753 2021-01-20 stsp }
1325 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1326 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1327 132af4a5 2021-01-05 stsp branches ? branches : "",
1328 132af4a5 2021-01-05 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1329 132af4a5 2021-01-05 stsp branchname) == -1) {
1330 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1331 132af4a5 2021-01-05 stsp goto done;
1332 132af4a5 2021-01-05 stsp }
1333 132af4a5 2021-01-05 stsp free(branches);
1334 132af4a5 2021-01-05 stsp branches = s;
1335 7c0b7f42 2020-09-24 stsp }
1336 7c0b7f42 2020-09-24 stsp } else {
1337 7c0b7f42 2020-09-24 stsp /*
1338 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1339 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1340 7c0b7f42 2020-09-24 stsp */
1341 04d9a9ec 2020-09-24 stsp if (default_branch) {
1342 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1343 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1344 7c0b7f42 2020-09-24 stsp branchname += 11;
1345 7c0b7f42 2020-09-24 stsp } else
1346 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1347 56d0a753 2021-01-20 stsp if (mirror_references) {
1348 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1349 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/heads/%s\n",
1350 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1351 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1352 56d0a753 2021-01-20 stsp goto done;
1353 56d0a753 2021-01-20 stsp }
1354 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1355 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1356 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1357 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1358 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1359 7c0b7f42 2020-09-24 stsp goto done;
1360 99495ddb 2021-01-10 stsp }
1361 99495ddb 2021-01-10 stsp }
1362 56d0a753 2021-01-20 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1363 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1364 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1365 99495ddb 2021-01-10 stsp char *s;
1366 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1367 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1368 99495ddb 2021-01-10 stsp refname += 5;
1369 56d0a753 2021-01-20 stsp if (mirror_references) {
1370 56d0a753 2021-01-20 stsp if (asprintf(&s,
1371 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/%s\n",
1372 56d0a753 2021-01-20 stsp refs ? refs : "", refname, refname) == -1) {
1373 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1374 56d0a753 2021-01-20 stsp goto done;
1375 56d0a753 2021-01-20 stsp }
1376 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1377 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1378 99495ddb 2021-01-10 stsp refs ? refs : "",
1379 99495ddb 2021-01-10 stsp refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1380 99495ddb 2021-01-10 stsp refname) == -1) {
1381 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1382 99495ddb 2021-01-10 stsp goto done;
1383 99495ddb 2021-01-10 stsp }
1384 99495ddb 2021-01-10 stsp free(refs);
1385 99495ddb 2021-01-10 stsp refs = s;
1386 7c0b7f42 2020-09-24 stsp }
1387 132af4a5 2021-01-05 stsp }
1388 99495ddb 2021-01-10 stsp
1389 132af4a5 2021-01-05 stsp if (asprintf(&gitconfig,
1390 132af4a5 2021-01-05 stsp "[remote \"%s\"]\n"
1391 132af4a5 2021-01-05 stsp "\turl = %s\n"
1392 132af4a5 2021-01-05 stsp "%s"
1393 99495ddb 2021-01-10 stsp "%s"
1394 56d0a753 2021-01-20 stsp "\tfetch = refs/tags/*:refs/tags/*\n",
1395 132af4a5 2021-01-05 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1396 56d0a753 2021-01-20 stsp refs ? refs : "") == -1) {
1397 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1398 132af4a5 2021-01-05 stsp goto done;
1399 7c0b7f42 2020-09-24 stsp }
1400 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1401 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1402 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1403 7c0b7f42 2020-09-24 stsp goto done;
1404 7c0b7f42 2020-09-24 stsp }
1405 7c0b7f42 2020-09-24 stsp done:
1406 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1407 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1408 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1409 132af4a5 2021-01-05 stsp free(branches);
1410 0e4002ca 2020-03-21 stsp return err;
1411 0e4002ca 2020-03-21 stsp }
1412 0e4002ca 2020-03-21 stsp
1413 0e4002ca 2020-03-21 stsp static const struct got_error *
1414 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1415 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1416 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1417 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
1418 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1419 04d9a9ec 2020-09-24 stsp {
1420 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1421 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1422 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1423 04d9a9ec 2020-09-24 stsp
1424 04d9a9ec 2020-09-24 stsp /*
1425 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1426 04d9a9ec 2020-09-24 stsp * one of those.
1427 04d9a9ec 2020-09-24 stsp */
1428 62d463ca 2020-10-20 naddy if (!TAILQ_EMPTY(wanted_branches)) {
1429 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1430 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1431 04d9a9ec 2020-09-24 stsp } else {
1432 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1433 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1434 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1435 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1436 04d9a9ec 2020-09-24 stsp
1437 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1438 04d9a9ec 2020-09-24 stsp continue;
1439 04d9a9ec 2020-09-24 stsp
1440 04d9a9ec 2020-09-24 stsp default_branch = target;
1441 04d9a9ec 2020-09-24 stsp break;
1442 04d9a9ec 2020-09-24 stsp }
1443 04d9a9ec 2020-09-24 stsp }
1444 04d9a9ec 2020-09-24 stsp
1445 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1446 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1447 0c8b29c5 2021-01-05 stsp default_branch, fetch_all_branches, wanted_branches,
1448 99495ddb 2021-01-10 stsp wanted_refs, mirror_references, repo);
1449 04d9a9ec 2020-09-24 stsp if (err)
1450 04d9a9ec 2020-09-24 stsp return err;
1451 04d9a9ec 2020-09-24 stsp
1452 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1453 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1454 99495ddb 2021-01-10 stsp wanted_branches, wanted_refs, mirror_references, repo);
1455 04d9a9ec 2020-09-24 stsp }
1456 04d9a9ec 2020-09-24 stsp
1457 04d9a9ec 2020-09-24 stsp static const struct got_error *
1458 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1459 93658fb9 2020-03-18 stsp {
1460 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1461 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1462 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1463 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1464 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1465 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1466 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1467 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1468 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1469 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1470 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1471 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1472 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1473 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1474 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1475 93658fb9 2020-03-18 stsp
1476 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1477 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1478 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1479 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1480 d9b4d0c0 2020-03-18 stsp
1481 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1482 93658fb9 2020-03-18 stsp switch (ch) {
1483 659e7fbd 2020-03-20 stsp case 'a':
1484 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1485 4ba14133 2020-03-20 stsp break;
1486 4ba14133 2020-03-20 stsp case 'b':
1487 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1488 4ba14133 2020-03-20 stsp optarg, NULL);
1489 4ba14133 2020-03-20 stsp if (error)
1490 4ba14133 2020-03-20 stsp return error;
1491 659e7fbd 2020-03-20 stsp break;
1492 41b0de12 2020-03-21 stsp case 'l':
1493 41b0de12 2020-03-21 stsp list_refs_only = 1;
1494 41b0de12 2020-03-21 stsp break;
1495 469dd726 2020-03-20 stsp case 'm':
1496 469dd726 2020-03-20 stsp mirror_references = 1;
1497 469dd726 2020-03-20 stsp break;
1498 68999b92 2020-03-18 stsp case 'v':
1499 68999b92 2020-03-18 stsp if (verbosity < 0)
1500 68999b92 2020-03-18 stsp verbosity = 0;
1501 68999b92 2020-03-18 stsp else if (verbosity < 3)
1502 68999b92 2020-03-18 stsp verbosity++;
1503 68999b92 2020-03-18 stsp break;
1504 68999b92 2020-03-18 stsp case 'q':
1505 68999b92 2020-03-18 stsp verbosity = -1;
1506 68999b92 2020-03-18 stsp break;
1507 0e4002ca 2020-03-21 stsp case 'R':
1508 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1509 0e4002ca 2020-03-21 stsp optarg, NULL);
1510 0e4002ca 2020-03-21 stsp if (error)
1511 0e4002ca 2020-03-21 stsp return error;
1512 0e4002ca 2020-03-21 stsp break;
1513 93658fb9 2020-03-18 stsp default:
1514 93658fb9 2020-03-18 stsp usage_clone();
1515 93658fb9 2020-03-18 stsp break;
1516 93658fb9 2020-03-18 stsp }
1517 93658fb9 2020-03-18 stsp }
1518 93658fb9 2020-03-18 stsp argc -= optind;
1519 93658fb9 2020-03-18 stsp argv += optind;
1520 39c64a6a 2020-03-18 stsp
1521 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1522 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
1523 41b0de12 2020-03-21 stsp if (list_refs_only) {
1524 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1525 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
1526 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1527 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
1528 41b0de12 2020-03-21 stsp if (mirror_references)
1529 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
1530 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1531 ff69268e 2020-12-13 stsp option_conflict('l', 'R');
1532 41b0de12 2020-03-21 stsp }
1533 4ba14133 2020-03-20 stsp
1534 93658fb9 2020-03-18 stsp uri = argv[0];
1535 9df6f38b 2020-03-18 stsp
1536 9df6f38b 2020-03-18 stsp if (argc == 1)
1537 93658fb9 2020-03-18 stsp dirname = NULL;
1538 9df6f38b 2020-03-18 stsp else if (argc == 2)
1539 93658fb9 2020-03-18 stsp dirname = argv[1];
1540 93658fb9 2020-03-18 stsp else
1541 93658fb9 2020-03-18 stsp usage_clone();
1542 09838ffc 2020-03-18 stsp
1543 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1544 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1545 39c64a6a 2020-03-18 stsp if (error)
1546 09f63084 2020-03-20 stsp goto done;
1547 09f63084 2020-03-20 stsp
1548 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1549 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1550 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1551 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1552 09838ffc 2020-03-18 stsp goto done;
1553 09f63084 2020-03-20 stsp }
1554 09838ffc 2020-03-18 stsp
1555 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1556 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1557 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1558 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1559 39c64a6a 2020-03-18 stsp err(1, "pledge");
1560 b46f3e71 2020-03-18 stsp #endif
1561 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1562 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 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 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, "http") == 0 ||
1569 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1570 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1571 39c64a6a 2020-03-18 stsp goto done;
1572 39c64a6a 2020-03-18 stsp } else {
1573 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1574 39c64a6a 2020-03-18 stsp goto done;
1575 39c64a6a 2020-03-18 stsp }
1576 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1577 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1578 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1579 bb64b798 2020-03-18 stsp goto done;
1580 bb64b798 2020-03-18 stsp }
1581 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1582 bb64b798 2020-03-18 stsp } else
1583 bb64b798 2020-03-18 stsp repo_path = dirname;
1584 bb64b798 2020-03-18 stsp
1585 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1586 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1587 2751fe64 2020-09-24 stsp if (error &&
1588 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1589 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1590 41b0de12 2020-03-21 stsp goto done;
1591 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1592 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1593 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1594 41b0de12 2020-03-21 stsp goto done;
1595 2751fe64 2020-09-24 stsp }
1596 41b0de12 2020-03-21 stsp }
1597 bb64b798 2020-03-18 stsp
1598 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1599 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1600 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1601 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1602 ee448f5f 2020-03-18 stsp goto done;
1603 ee448f5f 2020-03-18 stsp }
1604 ee448f5f 2020-03-18 stsp }
1605 f535bcd4 2020-09-30 stsp error = apply_unveil(repo_path, 0, NULL);
1606 ee448f5f 2020-03-18 stsp if (error)
1607 ee448f5f 2020-03-18 stsp goto done;
1608 f79e6490 2020-04-19 stsp
1609 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1610 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1611 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1612 ee448f5f 2020-03-18 stsp
1613 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1614 9c52365f 2020-03-21 stsp server_path, verbosity);
1615 39c64a6a 2020-03-18 stsp if (error)
1616 bb64b798 2020-03-18 stsp goto done;
1617 bb64b798 2020-03-18 stsp
1618 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1619 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1620 2751fe64 2020-09-24 stsp if (error)
1621 2751fe64 2020-09-24 stsp goto done;
1622 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1623 2751fe64 2020-09-24 stsp if (error)
1624 2751fe64 2020-09-24 stsp goto done;
1625 2751fe64 2020-09-24 stsp }
1626 2751fe64 2020-09-24 stsp
1627 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1628 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1629 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1630 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1631 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1632 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1633 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1634 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1635 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1636 99495ddb 2021-01-10 stsp fpa.config_info.wanted_refs = &wanted_refs;
1637 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1638 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1639 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1640 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1641 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1642 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1643 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1644 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1645 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1646 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1647 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1648 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1649 39c64a6a 2020-03-18 stsp if (error)
1650 d9b4d0c0 2020-03-18 stsp goto done;
1651 d9b4d0c0 2020-03-18 stsp
1652 41b0de12 2020-03-21 stsp if (list_refs_only) {
1653 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1654 41b0de12 2020-03-21 stsp goto done;
1655 41b0de12 2020-03-21 stsp }
1656 41b0de12 2020-03-21 stsp
1657 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1658 39c64a6a 2020-03-18 stsp if (error)
1659 d9b4d0c0 2020-03-18 stsp goto done;
1660 68999b92 2020-03-18 stsp if (verbosity >= 0)
1661 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1662 d9b4d0c0 2020-03-18 stsp free(id_str);
1663 d9b4d0c0 2020-03-18 stsp
1664 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1665 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1666 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1667 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1668 7ebc0570 2020-03-18 stsp char *remote_refname;
1669 0e4002ca 2020-03-21 stsp
1670 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1671 0e4002ca 2020-03-21 stsp !mirror_references) {
1672 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1673 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1674 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1675 0e4002ca 2020-03-21 stsp if (error)
1676 0e4002ca 2020-03-21 stsp goto done;
1677 0e4002ca 2020-03-21 stsp continue;
1678 0e4002ca 2020-03-21 stsp }
1679 668a20f6 2020-03-18 stsp
1680 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1681 39c64a6a 2020-03-18 stsp if (error)
1682 d9b4d0c0 2020-03-18 stsp goto done;
1683 d9b4d0c0 2020-03-18 stsp
1684 469dd726 2020-03-20 stsp if (mirror_references)
1685 469dd726 2020-03-20 stsp continue;
1686 469dd726 2020-03-20 stsp
1687 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1688 7ebc0570 2020-03-18 stsp continue;
1689 7ebc0570 2020-03-18 stsp
1690 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1691 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1692 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1693 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1694 7ebc0570 2020-03-18 stsp goto done;
1695 7ebc0570 2020-03-18 stsp }
1696 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1697 f298ae0f 2020-03-25 stsp free(remote_refname);
1698 39c64a6a 2020-03-18 stsp if (error)
1699 d9b4d0c0 2020-03-18 stsp goto done;
1700 d9b4d0c0 2020-03-18 stsp }
1701 d9b4d0c0 2020-03-18 stsp
1702 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1703 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1704 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1705 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1706 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1707 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1708 d9b4d0c0 2020-03-18 stsp
1709 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1710 d9b4d0c0 2020-03-18 stsp continue;
1711 d9b4d0c0 2020-03-18 stsp
1712 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1713 39c64a6a 2020-03-18 stsp if (error) {
1714 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1715 55330abe 2020-03-20 stsp error = NULL;
1716 d9b4d0c0 2020-03-18 stsp continue;
1717 55330abe 2020-03-20 stsp }
1718 d9b4d0c0 2020-03-18 stsp goto done;
1719 d9b4d0c0 2020-03-18 stsp }
1720 d9b4d0c0 2020-03-18 stsp
1721 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1722 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1723 f298ae0f 2020-03-25 stsp if (error)
1724 f298ae0f 2020-03-25 stsp goto done;
1725 f298ae0f 2020-03-25 stsp
1726 f298ae0f 2020-03-25 stsp if (mirror_references)
1727 f298ae0f 2020-03-25 stsp continue;
1728 f298ae0f 2020-03-25 stsp
1729 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1730 f298ae0f 2020-03-25 stsp continue;
1731 f298ae0f 2020-03-25 stsp
1732 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1733 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1734 f298ae0f 2020-03-25 stsp refname) == -1) {
1735 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1736 f298ae0f 2020-03-25 stsp goto done;
1737 f298ae0f 2020-03-25 stsp }
1738 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1739 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1740 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1741 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1742 f298ae0f 2020-03-25 stsp free(remote_refname);
1743 f298ae0f 2020-03-25 stsp goto done;
1744 f298ae0f 2020-03-25 stsp }
1745 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1746 f298ae0f 2020-03-25 stsp if (error) {
1747 f298ae0f 2020-03-25 stsp free(remote_refname);
1748 f298ae0f 2020-03-25 stsp free(remote_target);
1749 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1750 f298ae0f 2020-03-25 stsp error = NULL;
1751 f298ae0f 2020-03-25 stsp continue;
1752 f298ae0f 2020-03-25 stsp }
1753 f298ae0f 2020-03-25 stsp goto done;
1754 f298ae0f 2020-03-25 stsp }
1755 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1756 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1757 f298ae0f 2020-03-25 stsp free(remote_refname);
1758 f298ae0f 2020-03-25 stsp free(remote_target);
1759 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1760 39c64a6a 2020-03-18 stsp if (error)
1761 d9b4d0c0 2020-03-18 stsp goto done;
1762 4ba14133 2020-03-20 stsp }
1763 4ba14133 2020-03-20 stsp if (pe == NULL) {
1764 4ba14133 2020-03-20 stsp /*
1765 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1766 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1767 4ba14133 2020-03-20 stsp * which could be fetched instead.
1768 4ba14133 2020-03-20 stsp */
1769 62d463ca 2020-10-20 naddy TAILQ_FOREACH(pe, &wanted_branches, entry) {
1770 4ba14133 2020-03-20 stsp const char *target = pe->path;
1771 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1772 d9b4d0c0 2020-03-18 stsp
1773 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1774 4ba14133 2020-03-20 stsp if (error) {
1775 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1776 4ba14133 2020-03-20 stsp error = NULL;
1777 4ba14133 2020-03-20 stsp continue;
1778 4ba14133 2020-03-20 stsp }
1779 4ba14133 2020-03-20 stsp goto done;
1780 4ba14133 2020-03-20 stsp }
1781 4ba14133 2020-03-20 stsp
1782 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1783 04d9a9ec 2020-09-24 stsp verbosity, repo);
1784 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1785 4ba14133 2020-03-20 stsp if (error)
1786 4ba14133 2020-03-20 stsp goto done;
1787 4ba14133 2020-03-20 stsp break;
1788 4ba14133 2020-03-20 stsp }
1789 659e7fbd 2020-03-20 stsp }
1790 659e7fbd 2020-03-20 stsp
1791 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1792 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1793 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1794 09838ffc 2020-03-18 stsp done:
1795 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1796 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1797 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1798 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1799 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1800 9c52365f 2020-03-21 stsp }
1801 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1802 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1803 1d0f4054 2021-06-17 stsp if (repo) {
1804 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
1805 1d0f4054 2021-06-17 stsp if (error == NULL)
1806 1d0f4054 2021-06-17 stsp error = close_err;
1807 1d0f4054 2021-06-17 stsp }
1808 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1809 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1810 d9b4d0c0 2020-03-18 stsp free(pe->data);
1811 d9b4d0c0 2020-03-18 stsp }
1812 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1813 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1814 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1815 d9b4d0c0 2020-03-18 stsp free(pe->data);
1816 d9b4d0c0 2020-03-18 stsp }
1817 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1818 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1819 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1820 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1821 09838ffc 2020-03-18 stsp free(proto);
1822 09838ffc 2020-03-18 stsp free(host);
1823 09838ffc 2020-03-18 stsp free(port);
1824 09838ffc 2020-03-18 stsp free(server_path);
1825 09838ffc 2020-03-18 stsp free(repo_name);
1826 bb64b798 2020-03-18 stsp free(default_destdir);
1827 b46f3e71 2020-03-18 stsp free(git_url);
1828 39c64a6a 2020-03-18 stsp return error;
1829 7848a0e1 2020-03-19 stsp }
1830 7848a0e1 2020-03-19 stsp
1831 7848a0e1 2020-03-19 stsp static const struct got_error *
1832 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1833 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1834 7848a0e1 2020-03-19 stsp {
1835 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1836 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1837 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1838 7848a0e1 2020-03-19 stsp
1839 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1840 7848a0e1 2020-03-19 stsp if (err)
1841 7848a0e1 2020-03-19 stsp goto done;
1842 7848a0e1 2020-03-19 stsp
1843 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1844 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1845 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1846 88609724 2020-03-21 stsp if (err)
1847 88609724 2020-03-21 stsp goto done;
1848 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1849 88609724 2020-03-21 stsp goto done;
1850 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1851 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1852 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1853 db6d8ad8 2020-03-21 stsp }
1854 db6d8ad8 2020-03-21 stsp goto done;
1855 db6d8ad8 2020-03-21 stsp }
1856 db6d8ad8 2020-03-21 stsp
1857 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1858 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1859 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1860 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1861 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1862 688f11b3 2020-03-21 stsp }
1863 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1864 7848a0e1 2020-03-19 stsp if (err)
1865 7848a0e1 2020-03-19 stsp goto done;
1866 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1867 e8a967e0 2020-03-21 stsp if (err)
1868 e8a967e0 2020-03-21 stsp goto done;
1869 7848a0e1 2020-03-19 stsp } else {
1870 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1871 7848a0e1 2020-03-19 stsp if (err)
1872 7848a0e1 2020-03-19 stsp goto done;
1873 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1874 6338a6a1 2020-03-21 stsp goto done;
1875 6338a6a1 2020-03-21 stsp
1876 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1877 6338a6a1 2020-03-21 stsp if (err)
1878 6338a6a1 2020-03-21 stsp goto done;
1879 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1880 6338a6a1 2020-03-21 stsp if (err)
1881 6338a6a1 2020-03-21 stsp goto done;
1882 7848a0e1 2020-03-19 stsp }
1883 6338a6a1 2020-03-21 stsp
1884 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1885 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1886 6338a6a1 2020-03-21 stsp new_id_str);
1887 7848a0e1 2020-03-19 stsp done:
1888 7848a0e1 2020-03-19 stsp free(old_id);
1889 7848a0e1 2020-03-19 stsp free(new_id_str);
1890 7848a0e1 2020-03-19 stsp return err;
1891 2ab43947 2020-03-18 stsp }
1892 f1bcca34 2020-03-25 stsp
1893 f1bcca34 2020-03-25 stsp static const struct got_error *
1894 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1895 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1896 f1bcca34 2020-03-25 stsp {
1897 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1898 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1899 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1900 f1bcca34 2020-03-25 stsp
1901 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1902 bcf34b0e 2020-03-26 stsp if (err) {
1903 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1904 bcf34b0e 2020-03-26 stsp return err;
1905 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1906 bcf34b0e 2020-03-26 stsp if (err)
1907 bcf34b0e 2020-03-26 stsp goto done;
1908 2ab43947 2020-03-18 stsp
1909 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1910 bcf34b0e 2020-03-26 stsp if (err)
1911 bcf34b0e 2020-03-26 stsp goto done;
1912 f1bcca34 2020-03-25 stsp
1913 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1914 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1915 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1916 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1917 bcf34b0e 2020-03-26 stsp } else {
1918 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1919 f1bcca34 2020-03-25 stsp
1920 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1921 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1922 bcf34b0e 2020-03-26 stsp goto done;
1923 bcf34b0e 2020-03-26 stsp
1924 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1925 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1926 bcf34b0e 2020-03-26 stsp if (err)
1927 bcf34b0e 2020-03-26 stsp goto done;
1928 bcf34b0e 2020-03-26 stsp
1929 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1930 bcf34b0e 2020-03-26 stsp if (err)
1931 bcf34b0e 2020-03-26 stsp goto done;
1932 bcf34b0e 2020-03-26 stsp
1933 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1934 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1935 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1936 bcf34b0e 2020-03-26 stsp
1937 bcf34b0e 2020-03-26 stsp }
1938 f1bcca34 2020-03-25 stsp done:
1939 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1940 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1941 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1942 bcf34b0e 2020-03-26 stsp err = unlock_err;
1943 bcf34b0e 2020-03-26 stsp }
1944 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1945 f1bcca34 2020-03-25 stsp return err;
1946 f1bcca34 2020-03-25 stsp }
1947 f1bcca34 2020-03-25 stsp
1948 2ab43947 2020-03-18 stsp __dead static void
1949 7848a0e1 2020-03-19 stsp usage_fetch(void)
1950 7848a0e1 2020-03-19 stsp {
1951 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1952 161728eb 2021-07-24 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1953 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1954 13f12b09 2020-03-21 stsp getprogname());
1955 7848a0e1 2020-03-19 stsp exit(1);
1956 7848a0e1 2020-03-19 stsp }
1957 7848a0e1 2020-03-19 stsp
1958 7848a0e1 2020-03-19 stsp static const struct got_error *
1959 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1960 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1961 f21ec2f0 2020-03-21 stsp {
1962 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1963 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1964 3789fd73 2020-03-26 stsp char *id_str = NULL;
1965 3789fd73 2020-03-26 stsp
1966 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1967 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1968 3789fd73 2020-03-26 stsp if (err)
1969 3789fd73 2020-03-26 stsp return err;
1970 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1971 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
1972 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1973 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1974 3789fd73 2020-03-26 stsp }
1975 3789fd73 2020-03-26 stsp } else {
1976 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1977 3789fd73 2020-03-26 stsp if (err)
1978 3789fd73 2020-03-26 stsp return err;
1979 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1980 3789fd73 2020-03-26 stsp if (err)
1981 3789fd73 2020-03-26 stsp goto done;
1982 3168e5da 2020-09-10 stsp
1983 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1984 3789fd73 2020-03-26 stsp if (err)
1985 3789fd73 2020-03-26 stsp goto done;
1986 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1987 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
1988 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1989 3789fd73 2020-03-26 stsp }
1990 3789fd73 2020-03-26 stsp }
1991 3789fd73 2020-03-26 stsp done:
1992 3789fd73 2020-03-26 stsp free(id);
1993 3789fd73 2020-03-26 stsp free(id_str);
1994 3789fd73 2020-03-26 stsp return NULL;
1995 3789fd73 2020-03-26 stsp }
1996 3789fd73 2020-03-26 stsp
1997 3789fd73 2020-03-26 stsp static const struct got_error *
1998 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1999 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
2000 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
2001 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
2002 3789fd73 2020-03-26 stsp {
2003 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
2004 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
2005 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
2006 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
2007 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
2008 3789fd73 2020-03-26 stsp char *local_refname = NULL;
2009 f21ec2f0 2020-03-21 stsp
2010 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&my_refs);
2011 f21ec2f0 2020-03-21 stsp
2012 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2013 3789fd73 2020-03-26 stsp == -1)
2014 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
2015 3789fd73 2020-03-26 stsp
2016 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2017 f21ec2f0 2020-03-21 stsp if (err)
2018 3789fd73 2020-03-26 stsp goto done;
2019 f21ec2f0 2020-03-21 stsp
2020 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &my_refs, entry) {
2021 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
2022 f21ec2f0 2020-03-21 stsp
2023 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
2024 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
2025 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
2026 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
2027 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
2028 3789fd73 2020-03-26 stsp continue;
2029 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
2030 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
2031 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
2032 3789fd73 2020-03-26 stsp goto done;
2033 3789fd73 2020-03-26 stsp }
2034 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
2035 3789fd73 2020-03-26 stsp continue;
2036 3789fd73 2020-03-26 stsp }
2037 f21ec2f0 2020-03-21 stsp
2038 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
2039 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
2040 f21ec2f0 2020-03-21 stsp break;
2041 f21ec2f0 2020-03-21 stsp }
2042 f21ec2f0 2020-03-21 stsp if (pe != NULL)
2043 f21ec2f0 2020-03-21 stsp continue;
2044 f21ec2f0 2020-03-21 stsp
2045 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
2046 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
2047 3789fd73 2020-03-26 stsp break;
2048 3789fd73 2020-03-26 stsp }
2049 3789fd73 2020-03-26 stsp if (pe != NULL)
2050 3789fd73 2020-03-26 stsp continue;
2051 f21ec2f0 2020-03-21 stsp
2052 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
2053 f21ec2f0 2020-03-21 stsp if (err)
2054 f21ec2f0 2020-03-21 stsp break;
2055 3789fd73 2020-03-26 stsp
2056 3789fd73 2020-03-26 stsp if (local_refname) {
2057 3789fd73 2020-03-26 stsp struct got_reference *ref;
2058 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
2059 3789fd73 2020-03-26 stsp if (err) {
2060 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
2061 3789fd73 2020-03-26 stsp break;
2062 3789fd73 2020-03-26 stsp free(local_refname);
2063 3789fd73 2020-03-26 stsp local_refname = NULL;
2064 3789fd73 2020-03-26 stsp continue;
2065 3789fd73 2020-03-26 stsp }
2066 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
2067 3789fd73 2020-03-26 stsp if (err)
2068 3789fd73 2020-03-26 stsp break;
2069 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
2070 3789fd73 2020-03-26 stsp got_ref_close(ref);
2071 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
2072 3789fd73 2020-03-26 stsp err = unlock_err;
2073 3789fd73 2020-03-26 stsp break;
2074 3789fd73 2020-03-26 stsp }
2075 3789fd73 2020-03-26 stsp
2076 3789fd73 2020-03-26 stsp free(local_refname);
2077 3789fd73 2020-03-26 stsp local_refname = NULL;
2078 6338a6a1 2020-03-21 stsp }
2079 f21ec2f0 2020-03-21 stsp }
2080 3789fd73 2020-03-26 stsp done:
2081 3789fd73 2020-03-26 stsp free(remote_namespace);
2082 3789fd73 2020-03-26 stsp free(local_refname);
2083 0e4002ca 2020-03-21 stsp return err;
2084 0e4002ca 2020-03-21 stsp }
2085 0e4002ca 2020-03-21 stsp
2086 0e4002ca 2020-03-21 stsp static const struct got_error *
2087 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
2088 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
2089 0e4002ca 2020-03-21 stsp {
2090 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
2091 0e4002ca 2020-03-21 stsp char *remote_refname;
2092 0e4002ca 2020-03-21 stsp struct got_reference *ref;
2093 0e4002ca 2020-03-21 stsp
2094 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
2095 0e4002ca 2020-03-21 stsp refname += 5;
2096 0e4002ca 2020-03-21 stsp
2097 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2098 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
2099 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
2100 f21ec2f0 2020-03-21 stsp
2101 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
2102 0e4002ca 2020-03-21 stsp if (err) {
2103 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
2104 0e4002ca 2020-03-21 stsp goto done;
2105 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
2106 0e4002ca 2020-03-21 stsp } else {
2107 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
2108 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2109 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
2110 9f142382 2020-03-21 stsp err = unlock_err;
2111 0e4002ca 2020-03-21 stsp got_ref_close(ref);
2112 0e4002ca 2020-03-21 stsp }
2113 0e4002ca 2020-03-21 stsp done:
2114 0e4002ca 2020-03-21 stsp free(remote_refname);
2115 161728eb 2021-07-24 stsp return err;
2116 161728eb 2021-07-24 stsp }
2117 161728eb 2021-07-24 stsp
2118 161728eb 2021-07-24 stsp static const struct got_error *
2119 161728eb 2021-07-24 stsp delete_ref(struct got_repository *repo, struct got_reference *ref)
2120 161728eb 2021-07-24 stsp {
2121 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2122 161728eb 2021-07-24 stsp struct got_object_id *id = NULL;
2123 161728eb 2021-07-24 stsp char *id_str = NULL;
2124 161728eb 2021-07-24 stsp const char *target;
2125 161728eb 2021-07-24 stsp
2126 161728eb 2021-07-24 stsp if (got_ref_is_symbolic(ref)) {
2127 161728eb 2021-07-24 stsp target = got_ref_get_symref_target(ref);
2128 161728eb 2021-07-24 stsp } else {
2129 161728eb 2021-07-24 stsp err = got_ref_resolve(&id, repo, ref);
2130 161728eb 2021-07-24 stsp if (err)
2131 161728eb 2021-07-24 stsp goto done;
2132 161728eb 2021-07-24 stsp err = got_object_id_str(&id_str, id);
2133 161728eb 2021-07-24 stsp if (err)
2134 161728eb 2021-07-24 stsp goto done;
2135 161728eb 2021-07-24 stsp target = id_str;
2136 161728eb 2021-07-24 stsp }
2137 161728eb 2021-07-24 stsp
2138 161728eb 2021-07-24 stsp err = got_ref_delete(ref, repo);
2139 161728eb 2021-07-24 stsp if (err)
2140 161728eb 2021-07-24 stsp goto done;
2141 161728eb 2021-07-24 stsp
2142 161728eb 2021-07-24 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2143 161728eb 2021-07-24 stsp done:
2144 161728eb 2021-07-24 stsp free(id);
2145 161728eb 2021-07-24 stsp free(id_str);
2146 f21ec2f0 2020-03-21 stsp return err;
2147 f21ec2f0 2020-03-21 stsp }
2148 f21ec2f0 2020-03-21 stsp
2149 f21ec2f0 2020-03-21 stsp static const struct got_error *
2150 161728eb 2021-07-24 stsp delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2151 161728eb 2021-07-24 stsp {
2152 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2153 161728eb 2021-07-24 stsp struct got_reflist_head refs;
2154 161728eb 2021-07-24 stsp struct got_reflist_entry *re;
2155 161728eb 2021-07-24 stsp char *prefix;
2156 161728eb 2021-07-24 stsp
2157 161728eb 2021-07-24 stsp TAILQ_INIT(&refs);
2158 161728eb 2021-07-24 stsp
2159 161728eb 2021-07-24 stsp if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2160 161728eb 2021-07-24 stsp err = got_error_from_errno("asprintf");
2161 161728eb 2021-07-24 stsp goto done;
2162 161728eb 2021-07-24 stsp }
2163 161728eb 2021-07-24 stsp err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2164 161728eb 2021-07-24 stsp if (err)
2165 161728eb 2021-07-24 stsp goto done;
2166 161728eb 2021-07-24 stsp
2167 161728eb 2021-07-24 stsp TAILQ_FOREACH(re, &refs, entry)
2168 161728eb 2021-07-24 stsp delete_ref(repo, re->ref);
2169 161728eb 2021-07-24 stsp done:
2170 161728eb 2021-07-24 stsp got_ref_list_free(&refs);
2171 161728eb 2021-07-24 stsp return err;
2172 161728eb 2021-07-24 stsp }
2173 161728eb 2021-07-24 stsp
2174 161728eb 2021-07-24 stsp static const struct got_error *
2175 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
2176 7848a0e1 2020-03-19 stsp {
2177 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
2178 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
2179 7848a0e1 2020-03-19 stsp const char *remote_name;
2180 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
2181 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
2182 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
2183 7848a0e1 2020-03-19 stsp int nremotes;
2184 7848a0e1 2020-03-19 stsp char *id_str = NULL;
2185 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
2186 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
2187 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2188 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2189 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
2190 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
2191 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
2192 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
2193 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
2194 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2195 161728eb 2021-07-24 stsp int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2196 7848a0e1 2020-03-19 stsp
2197 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
2198 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
2199 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
2200 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
2201 7848a0e1 2020-03-19 stsp
2202 161728eb 2021-07-24 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2203 7848a0e1 2020-03-19 stsp switch (ch) {
2204 659e7fbd 2020-03-20 stsp case 'a':
2205 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
2206 4ba14133 2020-03-20 stsp break;
2207 4ba14133 2020-03-20 stsp case 'b':
2208 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
2209 4ba14133 2020-03-20 stsp optarg, NULL);
2210 4ba14133 2020-03-20 stsp if (error)
2211 4ba14133 2020-03-20 stsp return error;
2212 41b0de12 2020-03-21 stsp break;
2213 f21ec2f0 2020-03-21 stsp case 'd':
2214 f21ec2f0 2020-03-21 stsp delete_refs = 1;
2215 f21ec2f0 2020-03-21 stsp break;
2216 41b0de12 2020-03-21 stsp case 'l':
2217 41b0de12 2020-03-21 stsp list_refs_only = 1;
2218 659e7fbd 2020-03-20 stsp break;
2219 7848a0e1 2020-03-19 stsp case 'r':
2220 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
2221 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2222 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
2223 7848a0e1 2020-03-19 stsp optarg);
2224 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
2225 7848a0e1 2020-03-19 stsp break;
2226 db6d8ad8 2020-03-21 stsp case 't':
2227 db6d8ad8 2020-03-21 stsp replace_tags = 1;
2228 db6d8ad8 2020-03-21 stsp break;
2229 7848a0e1 2020-03-19 stsp case 'v':
2230 7848a0e1 2020-03-19 stsp if (verbosity < 0)
2231 7848a0e1 2020-03-19 stsp verbosity = 0;
2232 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
2233 7848a0e1 2020-03-19 stsp verbosity++;
2234 7848a0e1 2020-03-19 stsp break;
2235 7848a0e1 2020-03-19 stsp case 'q':
2236 7848a0e1 2020-03-19 stsp verbosity = -1;
2237 7848a0e1 2020-03-19 stsp break;
2238 0e4002ca 2020-03-21 stsp case 'R':
2239 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
2240 0e4002ca 2020-03-21 stsp optarg, NULL);
2241 0e4002ca 2020-03-21 stsp if (error)
2242 0e4002ca 2020-03-21 stsp return error;
2243 0e4002ca 2020-03-21 stsp break;
2244 161728eb 2021-07-24 stsp case 'X':
2245 161728eb 2021-07-24 stsp delete_remote = 1;
2246 161728eb 2021-07-24 stsp break;
2247 7848a0e1 2020-03-19 stsp default:
2248 7848a0e1 2020-03-19 stsp usage_fetch();
2249 7848a0e1 2020-03-19 stsp break;
2250 7848a0e1 2020-03-19 stsp }
2251 7848a0e1 2020-03-19 stsp }
2252 7848a0e1 2020-03-19 stsp argc -= optind;
2253 7848a0e1 2020-03-19 stsp argv += optind;
2254 7848a0e1 2020-03-19 stsp
2255 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2256 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
2257 41b0de12 2020-03-21 stsp if (list_refs_only) {
2258 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2259 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
2260 ff69268e 2020-12-13 stsp if (fetch_all_branches)
2261 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
2262 f21ec2f0 2020-03-21 stsp if (delete_refs)
2263 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
2264 161728eb 2021-07-24 stsp if (delete_remote)
2265 161728eb 2021-07-24 stsp option_conflict('l', 'X');
2266 41b0de12 2020-03-21 stsp }
2267 161728eb 2021-07-24 stsp if (delete_remote) {
2268 161728eb 2021-07-24 stsp if (fetch_all_branches)
2269 161728eb 2021-07-24 stsp option_conflict('X', 'a');
2270 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_branches))
2271 161728eb 2021-07-24 stsp option_conflict('X', 'b');
2272 161728eb 2021-07-24 stsp if (delete_refs)
2273 161728eb 2021-07-24 stsp option_conflict('X', 'd');
2274 161728eb 2021-07-24 stsp if (replace_tags)
2275 161728eb 2021-07-24 stsp option_conflict('X', 't');
2276 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_refs))
2277 161728eb 2021-07-24 stsp option_conflict('X', 'R');
2278 161728eb 2021-07-24 stsp }
2279 161728eb 2021-07-24 stsp
2280 161728eb 2021-07-24 stsp if (argc == 0) {
2281 161728eb 2021-07-24 stsp if (delete_remote)
2282 161728eb 2021-07-24 stsp errx(1, "-X option requires a remote name");
2283 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2284 161728eb 2021-07-24 stsp } else if (argc == 1)
2285 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2286 7848a0e1 2020-03-19 stsp else
2287 7848a0e1 2020-03-19 stsp usage_fetch();
2288 7848a0e1 2020-03-19 stsp
2289 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2290 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2291 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2292 7848a0e1 2020-03-19 stsp goto done;
2293 7848a0e1 2020-03-19 stsp }
2294 7848a0e1 2020-03-19 stsp
2295 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2296 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2297 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2298 7848a0e1 2020-03-19 stsp goto done;
2299 7848a0e1 2020-03-19 stsp else
2300 7848a0e1 2020-03-19 stsp error = NULL;
2301 7848a0e1 2020-03-19 stsp if (worktree) {
2302 7848a0e1 2020-03-19 stsp repo_path =
2303 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2304 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2305 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2306 7848a0e1 2020-03-19 stsp if (error)
2307 7848a0e1 2020-03-19 stsp goto done;
2308 7848a0e1 2020-03-19 stsp } else {
2309 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2310 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2311 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2312 7848a0e1 2020-03-19 stsp goto done;
2313 7848a0e1 2020-03-19 stsp }
2314 7848a0e1 2020-03-19 stsp }
2315 7848a0e1 2020-03-19 stsp }
2316 7848a0e1 2020-03-19 stsp
2317 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2318 7848a0e1 2020-03-19 stsp if (error)
2319 7848a0e1 2020-03-19 stsp goto done;
2320 7848a0e1 2020-03-19 stsp
2321 161728eb 2021-07-24 stsp if (delete_remote) {
2322 161728eb 2021-07-24 stsp error = delete_refs_for_remote(repo, remote_name);
2323 161728eb 2021-07-24 stsp goto done; /* nothing else to do */
2324 161728eb 2021-07-24 stsp }
2325 161728eb 2021-07-24 stsp
2326 50b0790e 2020-09-11 stsp if (worktree) {
2327 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2328 50b0790e 2020-09-11 stsp if (worktree_conf) {
2329 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2330 50b0790e 2020-09-11 stsp worktree_conf);
2331 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2332 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2333 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2334 50b0790e 2020-09-11 stsp break;
2335 54eb00d5 2020-10-20 stsp }
2336 50b0790e 2020-09-11 stsp }
2337 50b0790e 2020-09-11 stsp }
2338 7848a0e1 2020-03-19 stsp }
2339 50b0790e 2020-09-11 stsp if (remote == NULL) {
2340 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2341 50b0790e 2020-09-11 stsp if (repo_conf) {
2342 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2343 50b0790e 2020-09-11 stsp repo_conf);
2344 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2345 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2346 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2347 50b0790e 2020-09-11 stsp break;
2348 54eb00d5 2020-10-20 stsp }
2349 50b0790e 2020-09-11 stsp }
2350 50b0790e 2020-09-11 stsp }
2351 50b0790e 2020-09-11 stsp }
2352 50b0790e 2020-09-11 stsp if (remote == NULL) {
2353 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2354 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2355 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2356 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2357 257add31 2020-09-09 stsp break;
2358 54eb00d5 2020-10-20 stsp }
2359 257add31 2020-09-09 stsp }
2360 7848a0e1 2020-03-19 stsp }
2361 50b0790e 2020-09-11 stsp if (remote == NULL) {
2362 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2363 50b0790e 2020-09-11 stsp goto done;
2364 b8adfa55 2020-09-25 stsp }
2365 b8adfa55 2020-09-25 stsp
2366 0c8b29c5 2021-01-05 stsp if (TAILQ_EMPTY(&wanted_branches)) {
2367 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches)
2368 0c8b29c5 2021-01-05 stsp fetch_all_branches = remote->fetch_all_branches;
2369 b8adfa55 2020-09-25 stsp for (i = 0; i < remote->nbranches; i++) {
2370 b8adfa55 2020-09-25 stsp got_pathlist_append(&wanted_branches,
2371 b8adfa55 2020-09-25 stsp remote->branches[i], NULL);
2372 99495ddb 2021-01-10 stsp }
2373 99495ddb 2021-01-10 stsp }
2374 99495ddb 2021-01-10 stsp if (TAILQ_EMPTY(&wanted_refs)) {
2375 99495ddb 2021-01-10 stsp for (i = 0; i < remote->nrefs; i++) {
2376 99495ddb 2021-01-10 stsp got_pathlist_append(&wanted_refs,
2377 99495ddb 2021-01-10 stsp remote->refs[i], NULL);
2378 b8adfa55 2020-09-25 stsp }
2379 50b0790e 2020-09-11 stsp }
2380 7848a0e1 2020-03-19 stsp
2381 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2382 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
2383 7848a0e1 2020-03-19 stsp if (error)
2384 7848a0e1 2020-03-19 stsp goto done;
2385 7848a0e1 2020-03-19 stsp
2386 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2387 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2388 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2389 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2390 7848a0e1 2020-03-19 stsp err(1, "pledge");
2391 7848a0e1 2020-03-19 stsp #endif
2392 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2393 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2394 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2395 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2396 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2397 7848a0e1 2020-03-19 stsp err(1, "pledge");
2398 7848a0e1 2020-03-19 stsp #endif
2399 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2400 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2401 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2402 7848a0e1 2020-03-19 stsp goto done;
2403 7848a0e1 2020-03-19 stsp } else {
2404 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2405 7848a0e1 2020-03-19 stsp goto done;
2406 7848a0e1 2020-03-19 stsp }
2407 7848a0e1 2020-03-19 stsp
2408 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2409 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2410 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
2411 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
2412 7848a0e1 2020-03-19 stsp goto done;
2413 7848a0e1 2020-03-19 stsp }
2414 7848a0e1 2020-03-19 stsp }
2415 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2416 7848a0e1 2020-03-19 stsp if (error)
2417 7848a0e1 2020-03-19 stsp goto done;
2418 f79e6490 2020-04-19 stsp
2419 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2420 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2421 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2422 7848a0e1 2020-03-19 stsp
2423 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2424 9c52365f 2020-03-21 stsp server_path, verbosity);
2425 7848a0e1 2020-03-19 stsp if (error)
2426 7848a0e1 2020-03-19 stsp goto done;
2427 7848a0e1 2020-03-19 stsp
2428 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2429 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2430 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2431 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2432 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2433 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2434 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2435 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2436 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2437 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2438 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2439 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2440 7848a0e1 2020-03-19 stsp if (error)
2441 7848a0e1 2020-03-19 stsp goto done;
2442 7848a0e1 2020-03-19 stsp
2443 41b0de12 2020-03-21 stsp if (list_refs_only) {
2444 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2445 41b0de12 2020-03-21 stsp goto done;
2446 41b0de12 2020-03-21 stsp }
2447 41b0de12 2020-03-21 stsp
2448 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2449 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2450 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2451 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2452 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2453 984065c8 2020-03-19 stsp if (error)
2454 984065c8 2020-03-19 stsp goto done;
2455 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2456 984065c8 2020-03-19 stsp free(id_str);
2457 984065c8 2020-03-19 stsp id_str = NULL;
2458 984065c8 2020-03-19 stsp }
2459 7848a0e1 2020-03-19 stsp
2460 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2461 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2462 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2463 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2464 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2465 7848a0e1 2020-03-19 stsp char *remote_refname;
2466 7848a0e1 2020-03-19 stsp
2467 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2468 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2469 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2470 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2471 0e4002ca 2020-03-21 stsp if (error)
2472 0e4002ca 2020-03-21 stsp goto done;
2473 0e4002ca 2020-03-21 stsp continue;
2474 0e4002ca 2020-03-21 stsp }
2475 0e4002ca 2020-03-21 stsp
2476 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2477 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2478 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2479 7848a0e1 2020-03-19 stsp if (error) {
2480 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2481 7848a0e1 2020-03-19 stsp goto done;
2482 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2483 6338a6a1 2020-03-21 stsp repo);
2484 7848a0e1 2020-03-19 stsp if (error)
2485 7848a0e1 2020-03-19 stsp goto done;
2486 7848a0e1 2020-03-19 stsp } else {
2487 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2488 db6d8ad8 2020-03-21 stsp verbosity, repo);
2489 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2490 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2491 9f142382 2020-03-21 stsp error = unlock_err;
2492 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2493 7848a0e1 2020-03-19 stsp if (error)
2494 7848a0e1 2020-03-19 stsp goto done;
2495 7848a0e1 2020-03-19 stsp }
2496 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2497 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2498 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2499 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2500 7848a0e1 2020-03-19 stsp goto done;
2501 7848a0e1 2020-03-19 stsp }
2502 7848a0e1 2020-03-19 stsp
2503 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2504 7848a0e1 2020-03-19 stsp if (error) {
2505 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2506 7848a0e1 2020-03-19 stsp goto done;
2507 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2508 688f11b3 2020-03-21 stsp verbosity, repo);
2509 7848a0e1 2020-03-19 stsp if (error)
2510 7848a0e1 2020-03-19 stsp goto done;
2511 7848a0e1 2020-03-19 stsp } else {
2512 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2513 db6d8ad8 2020-03-21 stsp verbosity, repo);
2514 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2515 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2516 9f142382 2020-03-21 stsp error = unlock_err;
2517 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2518 7848a0e1 2020-03-19 stsp if (error)
2519 7848a0e1 2020-03-19 stsp goto done;
2520 7848a0e1 2020-03-19 stsp }
2521 2ec30c80 2020-03-20 stsp
2522 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2523 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2524 2ec30c80 2020-03-20 stsp if (error) {
2525 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2526 2ec30c80 2020-03-20 stsp goto done;
2527 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2528 6338a6a1 2020-03-21 stsp repo);
2529 2ec30c80 2020-03-20 stsp if (error)
2530 2ec30c80 2020-03-20 stsp goto done;
2531 9f142382 2020-03-21 stsp } else {
2532 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2533 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2534 9f142382 2020-03-21 stsp error = unlock_err;
2535 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2536 9f142382 2020-03-21 stsp }
2537 7848a0e1 2020-03-19 stsp }
2538 7848a0e1 2020-03-19 stsp }
2539 f1bcca34 2020-03-25 stsp if (delete_refs) {
2540 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2541 3789fd73 2020-03-26 stsp verbosity, repo);
2542 f1bcca34 2020-03-25 stsp if (error)
2543 f1bcca34 2020-03-25 stsp goto done;
2544 f1bcca34 2020-03-25 stsp }
2545 f1bcca34 2020-03-25 stsp
2546 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2547 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2548 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2549 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2550 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2551 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2552 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2553 f1bcca34 2020-03-25 stsp
2554 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2555 f1bcca34 2020-03-25 stsp continue;
2556 f1bcca34 2020-03-25 stsp
2557 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2558 f1bcca34 2020-03-25 stsp continue;
2559 f1bcca34 2020-03-25 stsp
2560 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2561 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2562 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2563 f1bcca34 2020-03-25 stsp goto done;
2564 f1bcca34 2020-03-25 stsp }
2565 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2566 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2567 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2568 f1bcca34 2020-03-25 stsp free(remote_refname);
2569 f1bcca34 2020-03-25 stsp goto done;
2570 f1bcca34 2020-03-25 stsp }
2571 f1bcca34 2020-03-25 stsp
2572 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2573 f1bcca34 2020-03-25 stsp 0);
2574 f1bcca34 2020-03-25 stsp if (error) {
2575 f1bcca34 2020-03-25 stsp free(remote_refname);
2576 f1bcca34 2020-03-25 stsp free(remote_target);
2577 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2578 f1bcca34 2020-03-25 stsp error = NULL;
2579 f1bcca34 2020-03-25 stsp continue;
2580 f1bcca34 2020-03-25 stsp }
2581 f1bcca34 2020-03-25 stsp goto done;
2582 f1bcca34 2020-03-25 stsp }
2583 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2584 f1bcca34 2020-03-25 stsp verbosity, repo);
2585 f1bcca34 2020-03-25 stsp free(remote_refname);
2586 f1bcca34 2020-03-25 stsp free(remote_target);
2587 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2588 f1bcca34 2020-03-25 stsp if (error)
2589 f1bcca34 2020-03-25 stsp goto done;
2590 f1bcca34 2020-03-25 stsp }
2591 f1bcca34 2020-03-25 stsp }
2592 7848a0e1 2020-03-19 stsp done:
2593 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2594 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2595 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2596 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2597 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2598 9c52365f 2020-03-21 stsp }
2599 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2600 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2601 1d0f4054 2021-06-17 stsp if (repo) {
2602 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2603 1d0f4054 2021-06-17 stsp if (error == NULL)
2604 1d0f4054 2021-06-17 stsp error = close_err;
2605 1d0f4054 2021-06-17 stsp }
2606 7848a0e1 2020-03-19 stsp if (worktree)
2607 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2608 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2609 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2610 7848a0e1 2020-03-19 stsp free(pe->data);
2611 7848a0e1 2020-03-19 stsp }
2612 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2613 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2614 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2615 7848a0e1 2020-03-19 stsp free(pe->data);
2616 7848a0e1 2020-03-19 stsp }
2617 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2618 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2619 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2620 7848a0e1 2020-03-19 stsp free(id_str);
2621 7848a0e1 2020-03-19 stsp free(cwd);
2622 7848a0e1 2020-03-19 stsp free(repo_path);
2623 7848a0e1 2020-03-19 stsp free(pack_hash);
2624 7848a0e1 2020-03-19 stsp free(proto);
2625 7848a0e1 2020-03-19 stsp free(host);
2626 7848a0e1 2020-03-19 stsp free(port);
2627 7848a0e1 2020-03-19 stsp free(server_path);
2628 7848a0e1 2020-03-19 stsp free(repo_name);
2629 7848a0e1 2020-03-19 stsp return error;
2630 7848a0e1 2020-03-19 stsp }
2631 7848a0e1 2020-03-19 stsp
2632 7848a0e1 2020-03-19 stsp
2633 7848a0e1 2020-03-19 stsp __dead static void
2634 2ab43947 2020-03-18 stsp usage_checkout(void)
2635 2ab43947 2020-03-18 stsp {
2636 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2637 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2638 2ab43947 2020-03-18 stsp exit(1);
2639 2ab43947 2020-03-18 stsp }
2640 2ab43947 2020-03-18 stsp
2641 2ab43947 2020-03-18 stsp static void
2642 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2643 2ab43947 2020-03-18 stsp {
2644 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2645 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2646 e6786710 2021-07-03 stsp "garbage-collected by Git or 'gotadmin cleanup'; making the "
2647 e6786710 2021-07-03 stsp "repository writable and running 'got update' will prevent this\n",
2648 2ab43947 2020-03-18 stsp getprogname());
2649 2ab43947 2020-03-18 stsp }
2650 2ab43947 2020-03-18 stsp
2651 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2652 2ab43947 2020-03-18 stsp const char *worktree_path;
2653 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2654 2ab43947 2020-03-18 stsp };
2655 2ab43947 2020-03-18 stsp
2656 2ab43947 2020-03-18 stsp static const struct got_error *
2657 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2658 2ab43947 2020-03-18 stsp {
2659 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2660 2ab43947 2020-03-18 stsp
2661 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2662 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2663 2ab43947 2020-03-18 stsp return NULL;
2664 2ab43947 2020-03-18 stsp
2665 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2666 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2667 2ab43947 2020-03-18 stsp return NULL;
2668 2ab43947 2020-03-18 stsp }
2669 2ab43947 2020-03-18 stsp
2670 2ab43947 2020-03-18 stsp while (path[0] == '/')
2671 2ab43947 2020-03-18 stsp path++;
2672 2ab43947 2020-03-18 stsp
2673 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2674 2ab43947 2020-03-18 stsp return NULL;
2675 2ab43947 2020-03-18 stsp }
2676 2ab43947 2020-03-18 stsp
2677 2ab43947 2020-03-18 stsp static const struct got_error *
2678 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2679 2ab43947 2020-03-18 stsp {
2680 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2681 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2682 2ab43947 2020-03-18 stsp return NULL;
2683 2ab43947 2020-03-18 stsp }
2684 2ab43947 2020-03-18 stsp
2685 2ab43947 2020-03-18 stsp static const struct got_error *
2686 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2687 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2688 2ab43947 2020-03-18 stsp struct got_repository *repo)
2689 2ab43947 2020-03-18 stsp {
2690 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2691 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2692 2ab43947 2020-03-18 stsp
2693 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2694 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2695 2ab43947 2020-03-18 stsp if (err)
2696 2ab43947 2020-03-18 stsp return err;
2697 2ab43947 2020-03-18 stsp
2698 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2699 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2700 2ab43947 2020-03-18 stsp
2701 2ab43947 2020-03-18 stsp /*
2702 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2703 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2704 2ab43947 2020-03-18 stsp *
2705 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2706 2ab43947 2020-03-18 stsp *
2707 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2708 2ab43947 2020-03-18 stsp * \ /
2709 2ab43947 2020-03-18 stsp * C E
2710 2ab43947 2020-03-18 stsp * \ /
2711 2ab43947 2020-03-18 stsp * B (yca)
2712 2ab43947 2020-03-18 stsp * |
2713 2ab43947 2020-03-18 stsp * A
2714 2ab43947 2020-03-18 stsp *
2715 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2716 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2717 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2718 2ab43947 2020-03-18 stsp */
2719 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2720 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2721 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2722 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2723 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2724 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2725 2ab43947 2020-03-18 stsp
2726 2ab43947 2020-03-18 stsp free(yca_id);
2727 2ab43947 2020-03-18 stsp return NULL;
2728 2ab43947 2020-03-18 stsp }
2729 2ab43947 2020-03-18 stsp
2730 2ab43947 2020-03-18 stsp static const struct got_error *
2731 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2732 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2733 2ab43947 2020-03-18 stsp struct got_repository *repo)
2734 2ab43947 2020-03-18 stsp {
2735 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2736 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2737 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2738 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2739 2ab43947 2020-03-18 stsp
2740 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2741 2ab43947 2020-03-18 stsp if (err)
2742 2ab43947 2020-03-18 stsp goto done;
2743 2ab43947 2020-03-18 stsp
2744 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2745 2ab43947 2020-03-18 stsp is_same_branch = 1;
2746 2ab43947 2020-03-18 stsp goto done;
2747 2ab43947 2020-03-18 stsp }
2748 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2749 2ab43947 2020-03-18 stsp is_same_branch = 1;
2750 2ab43947 2020-03-18 stsp goto done;
2751 2ab43947 2020-03-18 stsp }
2752 2ab43947 2020-03-18 stsp
2753 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2754 2ab43947 2020-03-18 stsp if (err)
2755 2ab43947 2020-03-18 stsp goto done;
2756 2ab43947 2020-03-18 stsp
2757 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2758 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2759 2ab43947 2020-03-18 stsp if (err)
2760 2ab43947 2020-03-18 stsp goto done;
2761 2ab43947 2020-03-18 stsp
2762 2ab43947 2020-03-18 stsp for (;;) {
2763 2ab43947 2020-03-18 stsp struct got_object_id *id;
2764 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2765 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2766 2ab43947 2020-03-18 stsp if (err) {
2767 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2768 2ab43947 2020-03-18 stsp err = NULL;
2769 2ab43947 2020-03-18 stsp break;
2770 2ab43947 2020-03-18 stsp }
2771 2ab43947 2020-03-18 stsp
2772 2ab43947 2020-03-18 stsp if (id) {
2773 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2774 2ab43947 2020-03-18 stsp break;
2775 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2776 2ab43947 2020-03-18 stsp is_same_branch = 1;
2777 2ab43947 2020-03-18 stsp break;
2778 2ab43947 2020-03-18 stsp }
2779 2ab43947 2020-03-18 stsp }
2780 2ab43947 2020-03-18 stsp }
2781 2ab43947 2020-03-18 stsp done:
2782 2ab43947 2020-03-18 stsp if (graph)
2783 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2784 2ab43947 2020-03-18 stsp free(head_commit_id);
2785 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2786 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2787 2ab43947 2020-03-18 stsp return err;
2788 a367ff0f 2019-05-14 stsp }
2789 8069f636 2019-01-12 stsp
2790 4ed7e80c 2018-05-20 stsp static const struct got_error *
2791 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2792 4b6c9460 2020-03-05 stsp {
2793 4b6c9460 2020-03-05 stsp static char msg[512];
2794 4b6c9460 2020-03-05 stsp const char *branch_name;
2795 4b6c9460 2020-03-05 stsp
2796 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2797 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2798 4b6c9460 2020-03-05 stsp else
2799 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2800 4b6c9460 2020-03-05 stsp
2801 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2802 4b6c9460 2020-03-05 stsp branch_name += 11;
2803 4b6c9460 2020-03-05 stsp
2804 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2805 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2806 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2807 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2808 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2809 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2810 4b6c9460 2020-03-05 stsp
2811 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2812 4b6c9460 2020-03-05 stsp }
2813 4b6c9460 2020-03-05 stsp
2814 4b6c9460 2020-03-05 stsp static const struct got_error *
2815 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2816 c09a553d 2018-03-12 stsp {
2817 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2818 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2819 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2820 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2821 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2822 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2823 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2824 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2825 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2826 f0207f6a 2020-10-19 stsp char *cwd = NULL;
2827 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2828 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2829 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2830 f2ea84fa 2019-07-27 stsp
2831 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2832 c09a553d 2018-03-12 stsp
2833 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2834 0bb8a95e 2018-03-12 stsp switch (ch) {
2835 08573d5b 2019-05-14 stsp case 'b':
2836 08573d5b 2019-05-14 stsp branch_name = optarg;
2837 08573d5b 2019-05-14 stsp break;
2838 8069f636 2019-01-12 stsp case 'c':
2839 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2840 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2841 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2842 bb51a5b4 2020-01-13 stsp break;
2843 bb51a5b4 2020-01-13 stsp case 'E':
2844 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2845 8069f636 2019-01-12 stsp break;
2846 0bb8a95e 2018-03-12 stsp case 'p':
2847 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2848 0bb8a95e 2018-03-12 stsp break;
2849 0bb8a95e 2018-03-12 stsp default:
2850 2deda0b9 2019-03-07 stsp usage_checkout();
2851 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2852 0bb8a95e 2018-03-12 stsp }
2853 0bb8a95e 2018-03-12 stsp }
2854 0bb8a95e 2018-03-12 stsp
2855 0bb8a95e 2018-03-12 stsp argc -= optind;
2856 0bb8a95e 2018-03-12 stsp argv += optind;
2857 0bb8a95e 2018-03-12 stsp
2858 6715a751 2018-03-16 stsp #ifndef PROFILE
2859 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2860 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2861 c09a553d 2018-03-12 stsp err(1, "pledge");
2862 6715a751 2018-03-16 stsp #endif
2863 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2864 c47340da 2020-09-20 stsp char *base, *dotgit;
2865 f0207f6a 2020-10-19 stsp const char *path;
2866 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2867 76089277 2018-04-01 stsp if (repo_path == NULL)
2868 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2869 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2870 76089277 2018-04-01 stsp if (cwd == NULL) {
2871 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2872 76089277 2018-04-01 stsp goto done;
2873 76089277 2018-04-01 stsp }
2874 c47340da 2020-09-20 stsp if (path_prefix[0])
2875 f0207f6a 2020-10-19 stsp path = path_prefix;
2876 c47340da 2020-09-20 stsp else
2877 f0207f6a 2020-10-19 stsp path = repo_path;
2878 f0207f6a 2020-10-19 stsp error = got_path_basename(&base, path);
2879 f0207f6a 2020-10-19 stsp if (error)
2880 c47340da 2020-09-20 stsp goto done;
2881 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2882 c09a553d 2018-03-12 stsp if (dotgit)
2883 c09a553d 2018-03-12 stsp *dotgit = '\0';
2884 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2885 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2886 f0207f6a 2020-10-19 stsp free(base);
2887 76089277 2018-04-01 stsp goto done;
2888 c09a553d 2018-03-12 stsp }
2889 f0207f6a 2020-10-19 stsp free(base);
2890 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2891 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2892 76089277 2018-04-01 stsp if (repo_path == NULL) {
2893 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2894 76089277 2018-04-01 stsp goto done;
2895 76089277 2018-04-01 stsp }
2896 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2897 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2898 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2899 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2900 b4b3a7dd 2019-07-22 stsp argv[1]);
2901 b4b3a7dd 2019-07-22 stsp goto done;
2902 b4b3a7dd 2019-07-22 stsp }
2903 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2904 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2905 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2906 b4b3a7dd 2019-07-22 stsp goto done;
2907 b4b3a7dd 2019-07-22 stsp }
2908 76089277 2018-04-01 stsp }
2909 c09a553d 2018-03-12 stsp } else
2910 c09a553d 2018-03-12 stsp usage_checkout();
2911 c09a553d 2018-03-12 stsp
2912 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2913 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2914 13bfb272 2019-05-10 jcs
2915 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2916 c09a553d 2018-03-12 stsp if (error != NULL)
2917 c02c541e 2019-03-29 stsp goto done;
2918 c02c541e 2019-03-29 stsp
2919 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2920 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2921 c530dc23 2019-07-23 stsp if (error) {
2922 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2923 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2924 c530dc23 2019-07-23 stsp goto done;
2925 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2926 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2927 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2928 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2929 c530dc23 2019-07-23 stsp goto done;
2930 c530dc23 2019-07-23 stsp }
2931 c530dc23 2019-07-23 stsp }
2932 c530dc23 2019-07-23 stsp
2933 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2934 c02c541e 2019-03-29 stsp if (error)
2935 c09a553d 2018-03-12 stsp goto done;
2936 8069f636 2019-01-12 stsp
2937 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2938 c09a553d 2018-03-12 stsp if (error != NULL)
2939 c09a553d 2018-03-12 stsp goto done;
2940 c09a553d 2018-03-12 stsp
2941 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2942 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2943 c09a553d 2018-03-12 stsp goto done;
2944 c09a553d 2018-03-12 stsp
2945 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2946 c09a553d 2018-03-12 stsp if (error != NULL)
2947 c09a553d 2018-03-12 stsp goto done;
2948 c09a553d 2018-03-12 stsp
2949 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2950 e5dc7198 2018-12-29 stsp path_prefix);
2951 e5dc7198 2018-12-29 stsp if (error != NULL)
2952 e5dc7198 2018-12-29 stsp goto done;
2953 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2954 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2955 49520a32 2018-12-29 stsp goto done;
2956 49520a32 2018-12-29 stsp }
2957 49520a32 2018-12-29 stsp
2958 8069f636 2019-01-12 stsp if (commit_id_str) {
2959 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2960 84de9106 2020-12-26 stsp struct got_reflist_head refs;
2961 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
2962 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2963 84de9106 2020-12-26 stsp NULL);
2964 84de9106 2020-12-26 stsp if (error)
2965 84de9106 2020-12-26 stsp goto done;
2966 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2967 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2968 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
2969 30837e32 2019-07-25 stsp if (error)
2970 8069f636 2019-01-12 stsp goto done;
2971 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2972 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2973 8069f636 2019-01-12 stsp if (error != NULL) {
2974 8069f636 2019-01-12 stsp free(commit_id);
2975 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2976 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2977 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2978 4b6c9460 2020-03-05 stsp }
2979 8069f636 2019-01-12 stsp goto done;
2980 8069f636 2019-01-12 stsp }
2981 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2982 4b6c9460 2020-03-05 stsp if (error) {
2983 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2984 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2985 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2986 4b6c9460 2020-03-05 stsp }
2987 45d344f6 2019-05-14 stsp goto done;
2988 4b6c9460 2020-03-05 stsp }
2989 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2990 8069f636 2019-01-12 stsp commit_id);
2991 8069f636 2019-01-12 stsp free(commit_id);
2992 8069f636 2019-01-12 stsp if (error)
2993 8069f636 2019-01-12 stsp goto done;
2994 8069f636 2019-01-12 stsp }
2995 8069f636 2019-01-12 stsp
2996 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2997 f2ea84fa 2019-07-27 stsp if (error)
2998 f2ea84fa 2019-07-27 stsp goto done;
2999 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
3000 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
3001 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3002 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
3003 c09a553d 2018-03-12 stsp if (error != NULL)
3004 c09a553d 2018-03-12 stsp goto done;
3005 c09a553d 2018-03-12 stsp
3006 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
3007 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
3008 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
3009 c09a553d 2018-03-12 stsp done:
3010 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3011 8069f636 2019-01-12 stsp free(commit_id_str);
3012 76089277 2018-04-01 stsp free(repo_path);
3013 507dc3bb 2018-12-29 stsp free(worktree_path);
3014 c47340da 2020-09-20 stsp free(cwd);
3015 507dc3bb 2018-12-29 stsp return error;
3016 9627c110 2020-04-18 stsp }
3017 9627c110 2020-04-18 stsp
3018 9627c110 2020-04-18 stsp struct got_update_progress_arg {
3019 9627c110 2020-04-18 stsp int did_something;
3020 9627c110 2020-04-18 stsp int conflicts;
3021 9627c110 2020-04-18 stsp int obstructed;
3022 9627c110 2020-04-18 stsp int not_updated;
3023 9627c110 2020-04-18 stsp };
3024 9627c110 2020-04-18 stsp
3025 9627c110 2020-04-18 stsp void
3026 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
3027 9627c110 2020-04-18 stsp {
3028 9627c110 2020-04-18 stsp if (!upa->did_something)
3029 9627c110 2020-04-18 stsp return;
3030 9627c110 2020-04-18 stsp
3031 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
3032 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
3033 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
3034 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
3035 9627c110 2020-04-18 stsp upa->obstructed);
3036 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
3037 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
3038 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
3039 507dc3bb 2018-12-29 stsp }
3040 507dc3bb 2018-12-29 stsp
3041 507dc3bb 2018-12-29 stsp __dead static void
3042 507dc3bb 2018-12-29 stsp usage_update(void)
3043 507dc3bb 2018-12-29 stsp {
3044 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
3045 507dc3bb 2018-12-29 stsp getprogname());
3046 507dc3bb 2018-12-29 stsp exit(1);
3047 507dc3bb 2018-12-29 stsp }
3048 507dc3bb 2018-12-29 stsp
3049 1ee397ad 2019-07-12 stsp static const struct got_error *
3050 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
3051 507dc3bb 2018-12-29 stsp {
3052 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
3053 784955db 2019-01-12 stsp
3054 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
3055 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
3056 1ee397ad 2019-07-12 stsp return NULL;
3057 507dc3bb 2018-12-29 stsp
3058 9627c110 2020-04-18 stsp upa->did_something = 1;
3059 a484d721 2019-06-10 stsp
3060 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
3061 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
3062 1ee397ad 2019-07-12 stsp return NULL;
3063 a484d721 2019-06-10 stsp
3064 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
3065 9627c110 2020-04-18 stsp upa->conflicts++;
3066 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
3067 9627c110 2020-04-18 stsp upa->obstructed++;
3068 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
3069 9627c110 2020-04-18 stsp upa->not_updated++;
3070 9627c110 2020-04-18 stsp
3071 507dc3bb 2018-12-29 stsp while (path[0] == '/')
3072 507dc3bb 2018-12-29 stsp path++;
3073 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
3074 1ee397ad 2019-07-12 stsp return NULL;
3075 be7061eb 2018-12-30 stsp }
3076 be7061eb 2018-12-30 stsp
3077 be7061eb 2018-12-30 stsp static const struct got_error *
3078 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
3079 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
3080 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
3081 a1fb16d8 2019-05-24 stsp {
3082 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
3083 a1fb16d8 2019-05-24 stsp char *base_id_str;
3084 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
3085 a1fb16d8 2019-05-24 stsp
3086 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
3087 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
3088 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
3089 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
3090 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
3091 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
3092 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
3093 a1fb16d8 2019-05-24 stsp }
3094 a1fb16d8 2019-05-24 stsp
3095 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
3096 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3097 a1fb16d8 2019-05-24 stsp if (err) {
3098 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
3099 a1fb16d8 2019-05-24 stsp return err;
3100 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
3101 a1fb16d8 2019-05-24 stsp }
3102 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
3103 a1fb16d8 2019-05-24 stsp return NULL;
3104 a1fb16d8 2019-05-24 stsp
3105 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
3106 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
3107 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
3108 a1fb16d8 2019-05-24 stsp if (err)
3109 a1fb16d8 2019-05-24 stsp return err;
3110 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
3111 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3112 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
3113 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3114 0ebf8283 2019-07-24 stsp return NULL;
3115 0ebf8283 2019-07-24 stsp }
3116 0ebf8283 2019-07-24 stsp
3117 0ebf8283 2019-07-24 stsp static const struct got_error *
3118 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3119 0ebf8283 2019-07-24 stsp {
3120 0ebf8283 2019-07-24 stsp const struct got_error *err;
3121 0ebf8283 2019-07-24 stsp int in_progress;
3122 0ebf8283 2019-07-24 stsp
3123 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
3124 0ebf8283 2019-07-24 stsp if (err)
3125 0ebf8283 2019-07-24 stsp return err;
3126 0ebf8283 2019-07-24 stsp if (in_progress)
3127 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
3128 0ebf8283 2019-07-24 stsp
3129 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
3130 0ebf8283 2019-07-24 stsp if (err)
3131 0ebf8283 2019-07-24 stsp return err;
3132 0ebf8283 2019-07-24 stsp if (in_progress)
3133 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
3134 0ebf8283 2019-07-24 stsp
3135 a1fb16d8 2019-05-24 stsp return NULL;
3136 a5edda0a 2019-07-27 stsp }
3137 a5edda0a 2019-07-27 stsp
3138 a5edda0a 2019-07-27 stsp static const struct got_error *
3139 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3140 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
3141 a5edda0a 2019-07-27 stsp {
3142 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
3143 a5edda0a 2019-07-27 stsp char *path;
3144 a5edda0a 2019-07-27 stsp int i;
3145 a5edda0a 2019-07-27 stsp
3146 a5edda0a 2019-07-27 stsp if (argc == 0) {
3147 a5edda0a 2019-07-27 stsp path = strdup("");
3148 a5edda0a 2019-07-27 stsp if (path == NULL)
3149 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
3150 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
3151 a5edda0a 2019-07-27 stsp }
3152 a5edda0a 2019-07-27 stsp
3153 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
3154 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
3155 a5edda0a 2019-07-27 stsp if (err)
3156 a5edda0a 2019-07-27 stsp break;
3157 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
3158 a5edda0a 2019-07-27 stsp if (err) {
3159 a5edda0a 2019-07-27 stsp free(path);
3160 a5edda0a 2019-07-27 stsp break;
3161 a5edda0a 2019-07-27 stsp }
3162 a5edda0a 2019-07-27 stsp }
3163 a5edda0a 2019-07-27 stsp
3164 a5edda0a 2019-07-27 stsp return err;
3165 a1fb16d8 2019-05-24 stsp }
3166 a1fb16d8 2019-05-24 stsp
3167 a1fb16d8 2019-05-24 stsp static const struct got_error *
3168 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
3169 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
3170 fa51e947 2020-03-27 stsp {
3171 fa51e947 2020-03-27 stsp const struct got_error *err;
3172 fa51e947 2020-03-27 stsp struct got_repository *repo;
3173 fa51e947 2020-03-27 stsp static char msg[512];
3174 fa51e947 2020-03-27 stsp
3175 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
3176 fa51e947 2020-03-27 stsp if (err)
3177 fa51e947 2020-03-27 stsp return orig_err;
3178 fa51e947 2020-03-27 stsp
3179 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
3180 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
3181 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
3182 fa51e947 2020-03-27 stsp "'got checkout'.\n"
3183 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
3184 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3185 fa51e947 2020-03-27 stsp got_repo_close(repo);
3186 fa51e947 2020-03-27 stsp return err;
3187 fa51e947 2020-03-27 stsp }
3188 fa51e947 2020-03-27 stsp
3189 fa51e947 2020-03-27 stsp static const struct got_error *
3190 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
3191 507dc3bb 2018-12-29 stsp {
3192 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
3193 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
3194 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
3195 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
3196 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
3197 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
3198 024e9686 2019-05-14 stsp const char *branch_name = NULL;
3199 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
3200 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
3201 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
3202 9627c110 2020-04-18 stsp int ch;
3203 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
3204 507dc3bb 2018-12-29 stsp
3205 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
3206 f2ea84fa 2019-07-27 stsp
3207 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
3208 507dc3bb 2018-12-29 stsp switch (ch) {
3209 024e9686 2019-05-14 stsp case 'b':
3210 024e9686 2019-05-14 stsp branch_name = optarg;
3211 024e9686 2019-05-14 stsp break;
3212 507dc3bb 2018-12-29 stsp case 'c':
3213 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
3214 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
3215 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
3216 507dc3bb 2018-12-29 stsp break;
3217 507dc3bb 2018-12-29 stsp default:
3218 2deda0b9 2019-03-07 stsp usage_update();
3219 507dc3bb 2018-12-29 stsp /* NOTREACHED */
3220 507dc3bb 2018-12-29 stsp }
3221 507dc3bb 2018-12-29 stsp }
3222 507dc3bb 2018-12-29 stsp
3223 507dc3bb 2018-12-29 stsp argc -= optind;
3224 507dc3bb 2018-12-29 stsp argv += optind;
3225 507dc3bb 2018-12-29 stsp
3226 507dc3bb 2018-12-29 stsp #ifndef PROFILE
3227 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3228 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
3229 507dc3bb 2018-12-29 stsp err(1, "pledge");
3230 507dc3bb 2018-12-29 stsp #endif
3231 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
3232 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
3233 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3234 c4cdcb68 2019-04-03 stsp goto done;
3235 c4cdcb68 2019-04-03 stsp }
3236 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
3237 fa51e947 2020-03-27 stsp if (error) {
3238 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3239 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
3240 fa51e947 2020-03-27 stsp worktree_path);
3241 7d5807f4 2019-07-11 stsp goto done;
3242 fa51e947 2020-03-27 stsp }
3243 7d5807f4 2019-07-11 stsp
3244 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
3245 f2ea84fa 2019-07-27 stsp if (error)
3246 f2ea84fa 2019-07-27 stsp goto done;
3247 507dc3bb 2018-12-29 stsp
3248 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3249 c9956ddf 2019-09-08 stsp NULL);
3250 507dc3bb 2018-12-29 stsp if (error != NULL)
3251 507dc3bb 2018-12-29 stsp goto done;
3252 507dc3bb 2018-12-29 stsp
3253 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3254 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3255 087fb88c 2019-08-04 stsp if (error)
3256 087fb88c 2019-08-04 stsp goto done;
3257 087fb88c 2019-08-04 stsp
3258 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3259 0266afb7 2019-01-04 stsp if (error)
3260 0266afb7 2019-01-04 stsp goto done;
3261 0266afb7 2019-01-04 stsp
3262 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3263 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
3264 024e9686 2019-05-14 stsp if (error != NULL)
3265 024e9686 2019-05-14 stsp goto done;
3266 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
3267 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3268 9c4b8182 2019-01-02 stsp if (error != NULL)
3269 9c4b8182 2019-01-02 stsp goto done;
3270 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3271 507dc3bb 2018-12-29 stsp if (error != NULL)
3272 507dc3bb 2018-12-29 stsp goto done;
3273 507dc3bb 2018-12-29 stsp } else {
3274 84de9106 2020-12-26 stsp struct got_reflist_head refs;
3275 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3276 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3277 84de9106 2020-12-26 stsp NULL);
3278 84de9106 2020-12-26 stsp if (error)
3279 84de9106 2020-12-26 stsp goto done;
3280 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3281 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3282 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
3283 04f57cb3 2019-07-25 stsp free(commit_id_str);
3284 bb787f09 2019-07-25 stsp commit_id_str = NULL;
3285 30837e32 2019-07-25 stsp if (error)
3286 a297e751 2019-07-11 stsp goto done;
3287 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
3288 a297e751 2019-07-11 stsp if (error)
3289 507dc3bb 2018-12-29 stsp goto done;
3290 507dc3bb 2018-12-29 stsp }
3291 35c965b2 2018-12-29 stsp
3292 a1fb16d8 2019-05-24 stsp if (branch_name) {
3293 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
3294 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3295 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3296 f2ea84fa 2019-07-27 stsp continue;
3297 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3298 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3299 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3300 024e9686 2019-05-14 stsp goto done;
3301 024e9686 2019-05-14 stsp }
3302 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3303 024e9686 2019-05-14 stsp if (error)
3304 024e9686 2019-05-14 stsp goto done;
3305 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3306 3aef623b 2019-10-15 stsp repo);
3307 a367ff0f 2019-05-14 stsp free(head_commit_id);
3308 024e9686 2019-05-14 stsp if (error != NULL)
3309 024e9686 2019-05-14 stsp goto done;
3310 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3311 a367ff0f 2019-05-14 stsp if (error)
3312 a367ff0f 2019-05-14 stsp goto done;
3313 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3314 024e9686 2019-05-14 stsp if (error)
3315 024e9686 2019-05-14 stsp goto done;
3316 024e9686 2019-05-14 stsp } else {
3317 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3318 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3319 a367ff0f 2019-05-14 stsp if (error != NULL) {
3320 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3321 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3322 024e9686 2019-05-14 stsp goto done;
3323 a367ff0f 2019-05-14 stsp }
3324 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3325 a367ff0f 2019-05-14 stsp if (error)
3326 a367ff0f 2019-05-14 stsp goto done;
3327 024e9686 2019-05-14 stsp }
3328 507dc3bb 2018-12-29 stsp
3329 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3330 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3331 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3332 507dc3bb 2018-12-29 stsp commit_id);
3333 507dc3bb 2018-12-29 stsp if (error)
3334 507dc3bb 2018-12-29 stsp goto done;
3335 507dc3bb 2018-12-29 stsp }
3336 507dc3bb 2018-12-29 stsp
3337 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3338 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3339 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3340 507dc3bb 2018-12-29 stsp if (error != NULL)
3341 507dc3bb 2018-12-29 stsp goto done;
3342 9c4b8182 2019-01-02 stsp
3343 9627c110 2020-04-18 stsp if (upa.did_something)
3344 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
3345 784955db 2019-01-12 stsp else
3346 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3347 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3348 507dc3bb 2018-12-29 stsp done:
3349 c09a553d 2018-03-12 stsp free(worktree_path);
3350 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3351 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3352 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3353 507dc3bb 2018-12-29 stsp free(commit_id);
3354 9c4b8182 2019-01-02 stsp free(commit_id_str);
3355 c09a553d 2018-03-12 stsp return error;
3356 c09a553d 2018-03-12 stsp }
3357 c09a553d 2018-03-12 stsp
3358 f42b1b34 2018-03-12 stsp static const struct got_error *
3359 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3360 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3361 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3362 5c860e29 2018-03-12 stsp {
3363 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3364 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3365 79109fed 2018-03-27 stsp
3366 44392932 2019-08-25 stsp if (blob_id1) {
3367 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3368 44392932 2019-08-25 stsp if (err)
3369 44392932 2019-08-25 stsp goto done;
3370 44392932 2019-08-25 stsp }
3371 79109fed 2018-03-27 stsp
3372 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3373 44392932 2019-08-25 stsp if (err)
3374 44392932 2019-08-25 stsp goto done;
3375 79109fed 2018-03-27 stsp
3376 44392932 2019-08-25 stsp while (path[0] == '/')
3377 44392932 2019-08-25 stsp path++;
3378 fe621944 2020-11-10 stsp err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3379 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, stdout);
3380 44392932 2019-08-25 stsp done:
3381 44392932 2019-08-25 stsp if (blob1)
3382 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3383 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3384 44392932 2019-08-25 stsp return err;
3385 44392932 2019-08-25 stsp }
3386 44392932 2019-08-25 stsp
3387 44392932 2019-08-25 stsp static const struct got_error *
3388 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3389 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3390 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3391 44392932 2019-08-25 stsp {
3392 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3393 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3394 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3395 44392932 2019-08-25 stsp
3396 44392932 2019-08-25 stsp if (tree_id1) {
3397 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3398 79109fed 2018-03-27 stsp if (err)
3399 44392932 2019-08-25 stsp goto done;
3400 79109fed 2018-03-27 stsp }
3401 79109fed 2018-03-27 stsp
3402 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3403 0f2b3dca 2018-12-22 stsp if (err)
3404 0f2b3dca 2018-12-22 stsp goto done;
3405 0f2b3dca 2018-12-22 stsp
3406 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3407 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3408 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
3409 aaa13589 2019-06-01 stsp arg.outfile = stdout;
3410 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
3411 fe621944 2020-11-10 stsp arg.nlines = 0;
3412 44392932 2019-08-25 stsp while (path[0] == '/')
3413 44392932 2019-08-25 stsp path++;
3414 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
3415 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3416 0f2b3dca 2018-12-22 stsp done:
3417 79109fed 2018-03-27 stsp if (tree1)
3418 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3419 366e0a5f 2019-10-10 stsp if (tree2)
3420 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3421 44392932 2019-08-25 stsp return err;
3422 44392932 2019-08-25 stsp }
3423 44392932 2019-08-25 stsp
3424 44392932 2019-08-25 stsp static const struct got_error *
3425 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3426 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3427 0208f208 2020-05-05 stsp {
3428 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3429 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3430 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3431 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3432 0208f208 2020-05-05 stsp
3433 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3434 0208f208 2020-05-05 stsp if (qid != NULL) {
3435 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3436 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3437 0208f208 2020-05-05 stsp qid->id);
3438 0208f208 2020-05-05 stsp if (err)
3439 0208f208 2020-05-05 stsp return err;
3440 0208f208 2020-05-05 stsp
3441 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3442 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3443 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3444 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3445 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3446 aa8b5dd0 2021-08-01 stsp }
3447 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3448 0208f208 2020-05-05 stsp
3449 0208f208 2020-05-05 stsp }
3450 0208f208 2020-05-05 stsp
3451 0208f208 2020-05-05 stsp if (tree_id1) {
3452 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3453 0208f208 2020-05-05 stsp if (err)
3454 0208f208 2020-05-05 stsp goto done;
3455 0208f208 2020-05-05 stsp }
3456 0208f208 2020-05-05 stsp
3457 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3458 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3459 0208f208 2020-05-05 stsp if (err)
3460 0208f208 2020-05-05 stsp goto done;
3461 0208f208 2020-05-05 stsp
3462 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3463 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3464 0208f208 2020-05-05 stsp done:
3465 0208f208 2020-05-05 stsp if (tree1)
3466 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3467 0208f208 2020-05-05 stsp if (tree2)
3468 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3469 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3470 0208f208 2020-05-05 stsp return err;
3471 0208f208 2020-05-05 stsp }
3472 0208f208 2020-05-05 stsp
3473 0208f208 2020-05-05 stsp static const struct got_error *
3474 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3475 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3476 44392932 2019-08-25 stsp {
3477 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3478 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3479 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3480 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3481 44392932 2019-08-25 stsp struct got_object_qid *qid;
3482 44392932 2019-08-25 stsp
3483 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3484 44392932 2019-08-25 stsp if (qid != NULL) {
3485 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3486 44392932 2019-08-25 stsp qid->id);
3487 44392932 2019-08-25 stsp if (err)
3488 44392932 2019-08-25 stsp return err;
3489 44392932 2019-08-25 stsp }
3490 44392932 2019-08-25 stsp
3491 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3492 44392932 2019-08-25 stsp int obj_type;
3493 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3494 44392932 2019-08-25 stsp if (err)
3495 44392932 2019-08-25 stsp goto done;
3496 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3497 44392932 2019-08-25 stsp if (err) {
3498 44392932 2019-08-25 stsp free(obj_id2);
3499 44392932 2019-08-25 stsp goto done;
3500 44392932 2019-08-25 stsp }
3501 44392932 2019-08-25 stsp if (pcommit) {
3502 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3503 44392932 2019-08-25 stsp qid->id, path);
3504 44392932 2019-08-25 stsp if (err) {
3505 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3506 2e8c69d1 2020-05-04 stsp free(obj_id2);
3507 2e8c69d1 2020-05-04 stsp goto done;
3508 2e8c69d1 2020-05-04 stsp }
3509 2e8c69d1 2020-05-04 stsp } else {
3510 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3511 2e8c69d1 2020-05-04 stsp if (err) {
3512 2e8c69d1 2020-05-04 stsp free(obj_id2);
3513 2e8c69d1 2020-05-04 stsp goto done;
3514 2e8c69d1 2020-05-04 stsp }
3515 44392932 2019-08-25 stsp }
3516 44392932 2019-08-25 stsp }
3517 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3518 44392932 2019-08-25 stsp if (err) {
3519 44392932 2019-08-25 stsp free(obj_id2);
3520 44392932 2019-08-25 stsp goto done;
3521 44392932 2019-08-25 stsp }
3522 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3523 44392932 2019-08-25 stsp switch (obj_type) {
3524 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3525 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3526 64453f7e 2020-11-21 stsp 0, 0, repo);
3527 44392932 2019-08-25 stsp break;
3528 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3529 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3530 64453f7e 2020-11-21 stsp 0, 0, repo);
3531 44392932 2019-08-25 stsp break;
3532 44392932 2019-08-25 stsp default:
3533 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3534 44392932 2019-08-25 stsp break;
3535 44392932 2019-08-25 stsp }
3536 44392932 2019-08-25 stsp free(obj_id1);
3537 44392932 2019-08-25 stsp free(obj_id2);
3538 44392932 2019-08-25 stsp } else {
3539 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3540 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3541 44392932 2019-08-25 stsp if (err)
3542 44392932 2019-08-25 stsp goto done;
3543 579bd556 2020-10-24 stsp if (pcommit) {
3544 579bd556 2020-10-24 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3545 579bd556 2020-10-24 stsp err = got_object_id_str(&id_str1, obj_id1);
3546 579bd556 2020-10-24 stsp if (err)
3547 579bd556 2020-10-24 stsp goto done;
3548 579bd556 2020-10-24 stsp }
3549 64453f7e 2020-11-21 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3550 64453f7e 2020-11-21 stsp id_str2);
3551 64453f7e 2020-11-21 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3552 64453f7e 2020-11-21 stsp repo);
3553 44392932 2019-08-25 stsp }
3554 44392932 2019-08-25 stsp done:
3555 0f2b3dca 2018-12-22 stsp free(id_str1);
3556 0f2b3dca 2018-12-22 stsp free(id_str2);
3557 44392932 2019-08-25 stsp if (pcommit)
3558 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3559 79109fed 2018-03-27 stsp return err;
3560 79109fed 2018-03-27 stsp }
3561 79109fed 2018-03-27 stsp
3562 4bb494d5 2018-06-16 stsp static char *
3563 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3564 6c281f94 2018-06-11 stsp {
3565 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3566 09867e48 2019-08-13 stsp char *p, *s;
3567 09867e48 2019-08-13 stsp
3568 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3569 09867e48 2019-08-13 stsp if (tm == NULL)
3570 09867e48 2019-08-13 stsp return NULL;
3571 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3572 09867e48 2019-08-13 stsp if (s == NULL)
3573 09867e48 2019-08-13 stsp return NULL;
3574 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3575 6c281f94 2018-06-11 stsp if (p)
3576 6c281f94 2018-06-11 stsp *p = '\0';
3577 6c281f94 2018-06-11 stsp return s;
3578 6c281f94 2018-06-11 stsp }
3579 dc424a06 2019-08-07 stsp
3580 6841bf13 2019-11-29 kn static const struct got_error *
3581 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3582 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3583 6841bf13 2019-11-29 kn {
3584 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3585 6841bf13 2019-11-29 kn regmatch_t regmatch;
3586 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3587 6841bf13 2019-11-29 kn
3588 6841bf13 2019-11-29 kn *have_match = 0;
3589 6841bf13 2019-11-29 kn
3590 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3591 6841bf13 2019-11-29 kn if (err)
3592 6841bf13 2019-11-29 kn return err;
3593 6841bf13 2019-11-29 kn
3594 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3595 6841bf13 2019-11-29 kn if (err)
3596 6841bf13 2019-11-29 kn goto done;
3597 6841bf13 2019-11-29 kn
3598 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3599 6841bf13 2019-11-29 kn *have_match = 1;
3600 6841bf13 2019-11-29 kn done:
3601 6841bf13 2019-11-29 kn free(id_str);
3602 6841bf13 2019-11-29 kn free(logmsg);
3603 6841bf13 2019-11-29 kn return err;
3604 6841bf13 2019-11-29 kn }
3605 6841bf13 2019-11-29 kn
3606 0208f208 2020-05-05 stsp static void
3607 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3608 0208f208 2020-05-05 stsp regex_t *regex)
3609 0208f208 2020-05-05 stsp {
3610 0208f208 2020-05-05 stsp regmatch_t regmatch;
3611 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3612 0208f208 2020-05-05 stsp
3613 0208f208 2020-05-05 stsp *have_match = 0;
3614 0208f208 2020-05-05 stsp
3615 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3616 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3617 0208f208 2020-05-05 stsp *have_match = 1;
3618 0208f208 2020-05-05 stsp break;
3619 0208f208 2020-05-05 stsp }
3620 0208f208 2020-05-05 stsp }
3621 0208f208 2020-05-05 stsp }
3622 0208f208 2020-05-05 stsp
3623 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3624 6c281f94 2018-06-11 stsp
3625 888b7d99 2020-12-26 stsp static const struct got_error*
3626 888b7d99 2020-12-26 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
3627 888b7d99 2020-12-26 stsp struct got_object_id *id, struct got_repository *repo)
3628 888b7d99 2020-12-26 stsp {
3629 888b7d99 2020-12-26 stsp static const struct got_error *err = NULL;
3630 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3631 888b7d99 2020-12-26 stsp char *s;
3632 888b7d99 2020-12-26 stsp const char *name;
3633 5c860e29 2018-03-12 stsp
3634 888b7d99 2020-12-26 stsp *refs_str = NULL;
3635 888b7d99 2020-12-26 stsp
3636 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
3637 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3638 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3639 a436ad14 2019-08-13 stsp int cmp;
3640 a436ad14 2019-08-13 stsp
3641 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3642 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3643 d9498b20 2019-02-05 stsp continue;
3644 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3645 199a4027 2019-02-02 stsp name += 5;
3646 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3647 7143d404 2019-03-12 stsp continue;
3648 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3649 e34f9ed6 2019-02-02 stsp name += 6;
3650 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3651 141c2bff 2019-02-04 stsp name += 8;
3652 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3653 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3654 4343a07f 2020-04-24 stsp continue;
3655 4343a07f 2020-04-24 stsp }
3656 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3657 48cae60d 2020-09-22 stsp if (err)
3658 888b7d99 2020-12-26 stsp break;
3659 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3660 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3661 5d844a1e 2019-08-13 stsp if (err) {
3662 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3663 48cae60d 2020-09-22 stsp free(ref_id);
3664 888b7d99 2020-12-26 stsp break;
3665 48cae60d 2020-09-22 stsp }
3666 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3667 5d844a1e 2019-08-13 stsp err = NULL;
3668 5d844a1e 2019-08-13 stsp tag = NULL;
3669 5d844a1e 2019-08-13 stsp }
3670 a436ad14 2019-08-13 stsp }
3671 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3672 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3673 48cae60d 2020-09-22 stsp free(ref_id);
3674 a436ad14 2019-08-13 stsp if (tag)
3675 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3676 a436ad14 2019-08-13 stsp if (cmp != 0)
3677 a436ad14 2019-08-13 stsp continue;
3678 888b7d99 2020-12-26 stsp s = *refs_str;
3679 888b7d99 2020-12-26 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
3680 888b7d99 2020-12-26 stsp s ? ", " : "", name) == -1) {
3681 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3682 199a4027 2019-02-02 stsp free(s);
3683 888b7d99 2020-12-26 stsp *refs_str = NULL;
3684 888b7d99 2020-12-26 stsp break;
3685 199a4027 2019-02-02 stsp }
3686 199a4027 2019-02-02 stsp free(s);
3687 199a4027 2019-02-02 stsp }
3688 888b7d99 2020-12-26 stsp
3689 888b7d99 2020-12-26 stsp return err;
3690 888b7d99 2020-12-26 stsp }
3691 888b7d99 2020-12-26 stsp
3692 888b7d99 2020-12-26 stsp static const struct got_error *
3693 888b7d99 2020-12-26 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3694 888b7d99 2020-12-26 stsp struct got_repository *repo, const char *path,
3695 888b7d99 2020-12-26 stsp struct got_pathlist_head *changed_paths, int show_patch,
3696 e600f124 2021-03-21 stsp int diff_context, struct got_reflist_object_id_map *refs_idmap,
3697 e600f124 2021-03-21 stsp const char *custom_refs_str)
3698 888b7d99 2020-12-26 stsp {
3699 888b7d99 2020-12-26 stsp const struct got_error *err = NULL;
3700 888b7d99 2020-12-26 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3701 888b7d99 2020-12-26 stsp char datebuf[26];
3702 888b7d99 2020-12-26 stsp time_t committer_time;
3703 888b7d99 2020-12-26 stsp const char *author, *committer;
3704 888b7d99 2020-12-26 stsp char *refs_str = NULL;
3705 888b7d99 2020-12-26 stsp
3706 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3707 8bf5b3c9 2018-03-17 stsp if (err)
3708 8bf5b3c9 2018-03-17 stsp return err;
3709 788c352e 2018-06-16 stsp
3710 e600f124 2021-03-21 stsp if (custom_refs_str == NULL) {
3711 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
3712 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3713 e600f124 2021-03-21 stsp if (refs) {
3714 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, id, repo);
3715 e600f124 2021-03-21 stsp if (err)
3716 e600f124 2021-03-21 stsp goto done;
3717 e600f124 2021-03-21 stsp }
3718 888b7d99 2020-12-26 stsp }
3719 888b7d99 2020-12-26 stsp
3720 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3721 e600f124 2021-03-21 stsp if (custom_refs_str)
3722 e600f124 2021-03-21 stsp printf("commit %s (%s)\n", id_str, custom_refs_str);
3723 e600f124 2021-03-21 stsp else
3724 e600f124 2021-03-21 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3725 e600f124 2021-03-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3726 832c249c 2018-06-10 stsp free(id_str);
3727 d3d493d7 2019-02-21 stsp id_str = NULL;
3728 d3d493d7 2019-02-21 stsp free(refs_str);
3729 d3d493d7 2019-02-21 stsp refs_str = NULL;
3730 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3731 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3732 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3733 09867e48 2019-08-13 stsp if (datestr)
3734 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3735 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3736 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3737 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3738 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3739 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3740 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3741 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3742 3fe1abad 2018-06-10 stsp int n = 1;
3743 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3744 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, parent_ids, entry) {
3745 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3746 a0603db2 2018-06-10 stsp if (err)
3747 888b7d99 2020-12-26 stsp goto done;
3748 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3749 a0603db2 2018-06-10 stsp free(id_str);
3750 888b7d99 2020-12-26 stsp id_str = NULL;
3751 a0603db2 2018-06-10 stsp }
3752 a0603db2 2018-06-10 stsp }
3753 832c249c 2018-06-10 stsp
3754 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3755 5943eee2 2019-08-13 stsp if (err)
3756 888b7d99 2020-12-26 stsp goto done;
3757 8bf5b3c9 2018-03-17 stsp
3758 621015ac 2018-07-23 stsp logmsg = logmsg0;
3759 832c249c 2018-06-10 stsp do {
3760 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3761 832c249c 2018-06-10 stsp if (line)
3762 832c249c 2018-06-10 stsp printf(" %s\n", line);
3763 832c249c 2018-06-10 stsp } while (line);
3764 621015ac 2018-07-23 stsp free(logmsg0);
3765 832c249c 2018-06-10 stsp
3766 0208f208 2020-05-05 stsp if (changed_paths) {
3767 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3768 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3769 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3770 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3771 0208f208 2020-05-05 stsp }
3772 0208f208 2020-05-05 stsp printf("\n");
3773 0208f208 2020-05-05 stsp }
3774 971751ac 2018-03-27 stsp if (show_patch) {
3775 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3776 971751ac 2018-03-27 stsp if (err == 0)
3777 971751ac 2018-03-27 stsp printf("\n");
3778 971751ac 2018-03-27 stsp }
3779 07862c20 2018-09-15 stsp
3780 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3781 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3782 888b7d99 2020-12-26 stsp done:
3783 888b7d99 2020-12-26 stsp free(id_str);
3784 888b7d99 2020-12-26 stsp free(refs_str);
3785 07862c20 2018-09-15 stsp return err;
3786 f42b1b34 2018-03-12 stsp }
3787 5c860e29 2018-03-12 stsp
3788 f42b1b34 2018-03-12 stsp static const struct got_error *
3789 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3790 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3791 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3792 888b7d99 2020-12-26 stsp int log_branches, int reverse_display_order,
3793 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap)
3794 f42b1b34 2018-03-12 stsp {
3795 f42b1b34 2018-03-12 stsp const struct got_error *err;
3796 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3797 6841bf13 2019-11-29 kn regex_t regex;
3798 6841bf13 2019-11-29 kn int have_match;
3799 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3800 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3801 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3802 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3803 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3804 dbec59df 2020-04-18 stsp
3805 dbdddfee 2021-06-23 naddy STAILQ_INIT(&reversed_commits);
3806 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3807 372ccdbb 2018-06-10 stsp
3808 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(&regex, search_pattern,
3809 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3810 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3811 6841bf13 2019-11-29 kn
3812 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3813 f42b1b34 2018-03-12 stsp if (err)
3814 f42b1b34 2018-03-12 stsp return err;
3815 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3816 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3817 372ccdbb 2018-06-10 stsp if (err)
3818 fcc85cad 2018-07-22 stsp goto done;
3819 656b1f76 2019-05-11 jcs for (;;) {
3820 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3821 8bf5b3c9 2018-03-17 stsp
3822 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3823 84453469 2018-11-11 stsp break;
3824 84453469 2018-11-11 stsp
3825 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3826 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3827 372ccdbb 2018-06-10 stsp if (err) {
3828 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3829 9ba79e04 2018-06-11 stsp err = NULL;
3830 ee780d5c 2020-01-04 stsp break;
3831 7e665116 2018-04-02 stsp }
3832 b43fbaa0 2018-06-11 stsp if (id == NULL)
3833 7e665116 2018-04-02 stsp break;
3834 b43fbaa0 2018-06-11 stsp
3835 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3836 b43fbaa0 2018-06-11 stsp if (err)
3837 fcc85cad 2018-07-22 stsp break;
3838 6841bf13 2019-11-29 kn
3839 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3840 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3841 0208f208 2020-05-05 stsp if (err)
3842 0208f208 2020-05-05 stsp break;
3843 0208f208 2020-05-05 stsp }
3844 0208f208 2020-05-05 stsp
3845 6841bf13 2019-11-29 kn if (search_pattern) {
3846 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3847 6841bf13 2019-11-29 kn if (err) {
3848 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3849 6841bf13 2019-11-29 kn break;
3850 6841bf13 2019-11-29 kn }
3851 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3852 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3853 0208f208 2020-05-05 stsp &changed_paths, &regex);
3854 6841bf13 2019-11-29 kn if (have_match == 0) {
3855 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3856 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3857 0208f208 2020-05-05 stsp free((char *)pe->path);
3858 0208f208 2020-05-05 stsp free(pe->data);
3859 0208f208 2020-05-05 stsp }
3860 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3861 6841bf13 2019-11-29 kn continue;
3862 6841bf13 2019-11-29 kn }
3863 6841bf13 2019-11-29 kn }
3864 6841bf13 2019-11-29 kn
3865 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3866 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3867 dbec59df 2020-04-18 stsp if (err)
3868 dbec59df 2020-04-18 stsp break;
3869 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3870 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3871 dbec59df 2020-04-18 stsp } else {
3872 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3873 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3874 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
3875 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3876 dbec59df 2020-04-18 stsp if (err)
3877 dbec59df 2020-04-18 stsp break;
3878 dbec59df 2020-04-18 stsp }
3879 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3880 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3881 7e665116 2018-04-02 stsp break;
3882 0208f208 2020-05-05 stsp
3883 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3884 0208f208 2020-05-05 stsp free((char *)pe->path);
3885 0208f208 2020-05-05 stsp free(pe->data);
3886 0208f208 2020-05-05 stsp }
3887 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3888 31cedeaf 2018-09-15 stsp }
3889 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3890 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &reversed_commits, entry) {
3891 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3892 dbec59df 2020-04-18 stsp if (err)
3893 dbec59df 2020-04-18 stsp break;
3894 502b9684 2020-07-31 stsp if (show_changed_paths) {
3895 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3896 502b9684 2020-07-31 stsp commit, repo);
3897 502b9684 2020-07-31 stsp if (err)
3898 502b9684 2020-07-31 stsp break;
3899 502b9684 2020-07-31 stsp }
3900 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3901 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3902 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
3903 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3904 dbec59df 2020-04-18 stsp if (err)
3905 dbec59df 2020-04-18 stsp break;
3906 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3907 502b9684 2020-07-31 stsp free((char *)pe->path);
3908 502b9684 2020-07-31 stsp free(pe->data);
3909 502b9684 2020-07-31 stsp }
3910 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3911 dbec59df 2020-04-18 stsp }
3912 dbec59df 2020-04-18 stsp }
3913 fcc85cad 2018-07-22 stsp done:
3914 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&reversed_commits)) {
3915 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&reversed_commits);
3916 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&reversed_commits, entry);
3917 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3918 dbec59df 2020-04-18 stsp }
3919 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3920 0208f208 2020-05-05 stsp free((char *)pe->path);
3921 0208f208 2020-05-05 stsp free(pe->data);
3922 0208f208 2020-05-05 stsp }
3923 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3924 6841bf13 2019-11-29 kn if (search_pattern)
3925 6841bf13 2019-11-29 kn regfree(&regex);
3926 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3927 f42b1b34 2018-03-12 stsp return err;
3928 f42b1b34 2018-03-12 stsp }
3929 5c860e29 2018-03-12 stsp
3930 4ed7e80c 2018-05-20 stsp __dead static void
3931 6f3d1eb0 2018-03-12 stsp usage_log(void)
3932 6f3d1eb0 2018-03-12 stsp {
3933 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3934 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3935 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3936 6f3d1eb0 2018-03-12 stsp exit(1);
3937 b1ebc001 2019-08-13 stsp }
3938 b1ebc001 2019-08-13 stsp
3939 b1ebc001 2019-08-13 stsp static int
3940 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3941 b1ebc001 2019-08-13 stsp {
3942 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3943 b1ebc001 2019-08-13 stsp long long n;
3944 b1ebc001 2019-08-13 stsp const char *errstr;
3945 b1ebc001 2019-08-13 stsp
3946 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3947 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3948 b1ebc001 2019-08-13 stsp return 0;
3949 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3950 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3951 b1ebc001 2019-08-13 stsp return 0;
3952 b1ebc001 2019-08-13 stsp return n;
3953 6f3d1eb0 2018-03-12 stsp }
3954 6f3d1eb0 2018-03-12 stsp
3955 4ed7e80c 2018-05-20 stsp static const struct got_error *
3956 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3957 f42b1b34 2018-03-12 stsp {
3958 f42b1b34 2018-03-12 stsp const struct got_error *error;
3959 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3960 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3961 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3962 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3963 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3964 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3965 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3966 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3967 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3968 64a96a6d 2018-04-01 stsp const char *errstr;
3969 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3970 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
3971 1b3893a2 2019-03-18 stsp
3972 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3973 5c860e29 2018-03-12 stsp
3974 6715a751 2018-03-16 stsp #ifndef PROFILE
3975 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3976 6098196c 2019-01-04 stsp NULL)
3977 ad242220 2018-09-08 stsp == -1)
3978 f42b1b34 2018-03-12 stsp err(1, "pledge");
3979 6715a751 2018-03-16 stsp #endif
3980 79109fed 2018-03-27 stsp
3981 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3982 b1ebc001 2019-08-13 stsp
3983 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3984 79109fed 2018-03-27 stsp switch (ch) {
3985 79109fed 2018-03-27 stsp case 'p':
3986 79109fed 2018-03-27 stsp show_patch = 1;
3987 d142fc45 2018-04-01 stsp break;
3988 0208f208 2020-05-05 stsp case 'P':
3989 0208f208 2020-05-05 stsp show_changed_paths = 1;
3990 0208f208 2020-05-05 stsp break;
3991 d142fc45 2018-04-01 stsp case 'c':
3992 d142fc45 2018-04-01 stsp start_commit = optarg;
3993 64a96a6d 2018-04-01 stsp break;
3994 c0cc5c62 2018-10-18 stsp case 'C':
3995 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3996 4a8520aa 2018-10-18 stsp &errstr);
3997 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3998 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3999 c0cc5c62 2018-10-18 stsp break;
4000 64a96a6d 2018-04-01 stsp case 'l':
4001 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
4002 64a96a6d 2018-04-01 stsp if (errstr != NULL)
4003 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
4004 cc54c501 2019-07-15 stsp break;
4005 48c8c60d 2020-01-27 stsp case 'b':
4006 48c8c60d 2020-01-27 stsp log_branches = 1;
4007 a0603db2 2018-06-10 stsp break;
4008 04ca23f4 2018-07-16 stsp case 'r':
4009 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4010 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
4011 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4012 9ba1d308 2019-10-21 stsp optarg);
4013 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4014 04ca23f4 2018-07-16 stsp break;
4015 dbec59df 2020-04-18 stsp case 'R':
4016 dbec59df 2020-04-18 stsp reverse_display_order = 1;
4017 dbec59df 2020-04-18 stsp break;
4018 6841bf13 2019-11-29 kn case 's':
4019 6841bf13 2019-11-29 kn search_pattern = optarg;
4020 6841bf13 2019-11-29 kn break;
4021 d1fe46f9 2020-04-18 stsp case 'x':
4022 d1fe46f9 2020-04-18 stsp end_commit = optarg;
4023 d1fe46f9 2020-04-18 stsp break;
4024 79109fed 2018-03-27 stsp default:
4025 2deda0b9 2019-03-07 stsp usage_log();
4026 79109fed 2018-03-27 stsp /* NOTREACHED */
4027 79109fed 2018-03-27 stsp }
4028 79109fed 2018-03-27 stsp }
4029 79109fed 2018-03-27 stsp
4030 79109fed 2018-03-27 stsp argc -= optind;
4031 79109fed 2018-03-27 stsp argv += optind;
4032 f42b1b34 2018-03-12 stsp
4033 dc1edbfa 2019-11-29 kn if (diff_context == -1)
4034 dc1edbfa 2019-11-29 kn diff_context = 3;
4035 dc1edbfa 2019-11-29 kn else if (!show_patch)
4036 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
4037 dc1edbfa 2019-11-29 kn
4038 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
4039 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
4040 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4041 04ca23f4 2018-07-16 stsp goto done;
4042 04ca23f4 2018-07-16 stsp }
4043 cffc0aa4 2019-02-05 stsp
4044 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
4045 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
4046 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4047 50f2fada 2020-04-24 stsp goto done;
4048 50f2fada 2020-04-24 stsp error = NULL;
4049 50f2fada 2020-04-24 stsp }
4050 cffc0aa4 2019-02-05 stsp
4051 603cdeb0 2020-10-22 stsp if (argc == 1) {
4052 e7301579 2019-03-18 stsp if (worktree) {
4053 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4054 e7301579 2019-03-18 stsp argv[0]);
4055 e7301579 2019-03-18 stsp if (error)
4056 e7301579 2019-03-18 stsp goto done;
4057 e7301579 2019-03-18 stsp } else {
4058 e7301579 2019-03-18 stsp path = strdup(argv[0]);
4059 e7301579 2019-03-18 stsp if (path == NULL) {
4060 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4061 e7301579 2019-03-18 stsp goto done;
4062 e7301579 2019-03-18 stsp }
4063 e7301579 2019-03-18 stsp }
4064 603cdeb0 2020-10-22 stsp } else if (argc != 0)
4065 cbd1af7a 2019-03-18 stsp usage_log();
4066 cbd1af7a 2019-03-18 stsp
4067 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
4068 5486daa2 2019-05-11 stsp repo_path = worktree ?
4069 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4070 5486daa2 2019-05-11 stsp }
4071 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
4072 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4073 cffc0aa4 2019-02-05 stsp goto done;
4074 04ca23f4 2018-07-16 stsp }
4075 6098196c 2019-01-04 stsp
4076 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4077 d7d4f210 2018-03-12 stsp if (error != NULL)
4078 04ca23f4 2018-07-16 stsp goto done;
4079 f42b1b34 2018-03-12 stsp
4080 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4081 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4082 c02c541e 2019-03-29 stsp if (error)
4083 c02c541e 2019-03-29 stsp goto done;
4084 c02c541e 2019-03-29 stsp
4085 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4086 84de9106 2020-12-26 stsp if (error)
4087 84de9106 2020-12-26 stsp goto done;
4088 84de9106 2020-12-26 stsp
4089 888b7d99 2020-12-26 stsp error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4090 888b7d99 2020-12-26 stsp if (error)
4091 888b7d99 2020-12-26 stsp goto done;
4092 888b7d99 2020-12-26 stsp
4093 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
4094 3235492e 2018-04-01 stsp struct got_reference *head_ref;
4095 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
4096 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
4097 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4098 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
4099 3235492e 2018-04-01 stsp if (error != NULL)
4100 d1fe46f9 2020-04-18 stsp goto done;
4101 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
4102 3235492e 2018-04-01 stsp got_ref_close(head_ref);
4103 3235492e 2018-04-01 stsp if (error != NULL)
4104 d1fe46f9 2020-04-18 stsp goto done;
4105 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
4106 d1fe46f9 2020-04-18 stsp start_id);
4107 d1fe46f9 2020-04-18 stsp if (error != NULL)
4108 d1fe46f9 2020-04-18 stsp goto done;
4109 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
4110 3235492e 2018-04-01 stsp } else {
4111 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&start_id, NULL,
4112 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4113 d1fe46f9 2020-04-18 stsp if (error != NULL)
4114 d1fe46f9 2020-04-18 stsp goto done;
4115 3235492e 2018-04-01 stsp }
4116 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
4117 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&end_id, NULL,
4118 84de9106 2020-12-26 stsp end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4119 d1fe46f9 2020-04-18 stsp if (error != NULL)
4120 d1fe46f9 2020-04-18 stsp goto done;
4121 d1fe46f9 2020-04-18 stsp }
4122 04ca23f4 2018-07-16 stsp
4123 deeabeae 2019-08-27 stsp if (worktree) {
4124 603cdeb0 2020-10-22 stsp /*
4125 603cdeb0 2020-10-22 stsp * If a path was specified on the command line it was resolved
4126 603cdeb0 2020-10-22 stsp * to a path in the work tree above. Prepend the work tree's
4127 603cdeb0 2020-10-22 stsp * path prefix to obtain the corresponding in-repository path.
4128 603cdeb0 2020-10-22 stsp */
4129 603cdeb0 2020-10-22 stsp if (path) {
4130 603cdeb0 2020-10-22 stsp const char *prefix;
4131 603cdeb0 2020-10-22 stsp prefix = got_worktree_get_path_prefix(worktree);
4132 603cdeb0 2020-10-22 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4133 603cdeb0 2020-10-22 stsp (path[0] != '\0') ? "/" : "", path) == -1) {
4134 603cdeb0 2020-10-22 stsp error = got_error_from_errno("asprintf");
4135 603cdeb0 2020-10-22 stsp goto done;
4136 603cdeb0 2020-10-22 stsp }
4137 603cdeb0 2020-10-22 stsp }
4138 deeabeae 2019-08-27 stsp } else
4139 603cdeb0 2020-10-22 stsp error = got_repo_map_path(&in_repo_path, repo,
4140 8fa913ec 2020-11-14 stsp path ? path : "");
4141 04ca23f4 2018-07-16 stsp if (error != NULL)
4142 04ca23f4 2018-07-16 stsp goto done;
4143 04ca23f4 2018-07-16 stsp if (in_repo_path) {
4144 04ca23f4 2018-07-16 stsp free(path);
4145 04ca23f4 2018-07-16 stsp path = in_repo_path;
4146 04ca23f4 2018-07-16 stsp }
4147 199a4027 2019-02-02 stsp
4148 603cdeb0 2020-10-22 stsp error = print_commits(start_id, end_id, repo, path ? path : "",
4149 603cdeb0 2020-10-22 stsp show_changed_paths, show_patch, search_pattern, diff_context,
4150 888b7d99 2020-12-26 stsp limit, log_branches, reverse_display_order, refs_idmap);
4151 04ca23f4 2018-07-16 stsp done:
4152 04ca23f4 2018-07-16 stsp free(path);
4153 04ca23f4 2018-07-16 stsp free(repo_path);
4154 04ca23f4 2018-07-16 stsp free(cwd);
4155 cffc0aa4 2019-02-05 stsp if (worktree)
4156 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
4157 ad242220 2018-09-08 stsp if (repo) {
4158 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4159 ad242220 2018-09-08 stsp if (error == NULL)
4160 1d0f4054 2021-06-17 stsp error = close_err;
4161 ad242220 2018-09-08 stsp }
4162 888b7d99 2020-12-26 stsp if (refs_idmap)
4163 888b7d99 2020-12-26 stsp got_reflist_object_id_map_free(refs_idmap);
4164 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4165 8bf5b3c9 2018-03-17 stsp return error;
4166 5c860e29 2018-03-12 stsp }
4167 5c860e29 2018-03-12 stsp
4168 4ed7e80c 2018-05-20 stsp __dead static void
4169 3f8b7d6a 2018-04-01 stsp usage_diff(void)
4170 3f8b7d6a 2018-04-01 stsp {
4171 64453f7e 2020-11-21 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4172 64453f7e 2020-11-21 stsp "[-s] [-w] [object1 object2 | path]\n", getprogname());
4173 3f8b7d6a 2018-04-01 stsp exit(1);
4174 b00d56cd 2018-04-01 stsp }
4175 b00d56cd 2018-04-01 stsp
4176 b72f483a 2019-02-05 stsp struct print_diff_arg {
4177 b72f483a 2019-02-05 stsp struct got_repository *repo;
4178 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
4179 b72f483a 2019-02-05 stsp int diff_context;
4180 3fc0c068 2019-02-10 stsp const char *id_str;
4181 3fc0c068 2019-02-10 stsp int header_shown;
4182 98eaaa12 2019-08-03 stsp int diff_staged;
4183 63035f9f 2019-10-06 stsp int ignore_whitespace;
4184 64453f7e 2020-11-21 stsp int force_text_diff;
4185 b72f483a 2019-02-05 stsp };
4186 39449a05 2020-07-23 stsp
4187 39449a05 2020-07-23 stsp /*
4188 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
4189 39449a05 2020-07-23 stsp * it as content to the diff engine.
4190 39449a05 2020-07-23 stsp */
4191 39449a05 2020-07-23 stsp static const struct got_error *
4192 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4193 39449a05 2020-07-23 stsp const char *abspath)
4194 39449a05 2020-07-23 stsp {
4195 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
4196 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
4197 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
4198 39449a05 2020-07-23 stsp
4199 39449a05 2020-07-23 stsp *fd = -1;
4200 39449a05 2020-07-23 stsp
4201 39449a05 2020-07-23 stsp if (dirfd != -1) {
4202 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4203 39449a05 2020-07-23 stsp if (target_len == -1)
4204 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
4205 39449a05 2020-07-23 stsp } else {
4206 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
4207 39449a05 2020-07-23 stsp if (target_len == -1)
4208 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
4209 39449a05 2020-07-23 stsp }
4210 39449a05 2020-07-23 stsp
4211 39449a05 2020-07-23 stsp *fd = got_opentempfd();
4212 39449a05 2020-07-23 stsp if (*fd == -1)
4213 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
4214 39449a05 2020-07-23 stsp
4215 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
4216 39449a05 2020-07-23 stsp if (outlen == -1) {
4217 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
4218 39449a05 2020-07-23 stsp goto done;
4219 39449a05 2020-07-23 stsp }
4220 39449a05 2020-07-23 stsp
4221 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
4222 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
4223 39449a05 2020-07-23 stsp goto done;
4224 39449a05 2020-07-23 stsp }
4225 39449a05 2020-07-23 stsp done:
4226 39449a05 2020-07-23 stsp if (err) {
4227 39449a05 2020-07-23 stsp close(*fd);
4228 39449a05 2020-07-23 stsp *fd = -1;
4229 39449a05 2020-07-23 stsp }
4230 39449a05 2020-07-23 stsp return err;
4231 39449a05 2020-07-23 stsp }
4232 b72f483a 2019-02-05 stsp
4233 4ed7e80c 2018-05-20 stsp static const struct got_error *
4234 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
4235 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4236 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4237 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4238 b72f483a 2019-02-05 stsp {
4239 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
4240 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
4241 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
4242 12463d8b 2019-12-13 stsp int fd = -1;
4243 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
4244 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
4245 b72f483a 2019-02-05 stsp struct stat sb;
4246 b72f483a 2019-02-05 stsp
4247 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4248 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4249 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4250 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4251 98eaaa12 2019-08-03 stsp return NULL;
4252 98eaaa12 2019-08-03 stsp } else {
4253 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4254 98eaaa12 2019-08-03 stsp return NULL;
4255 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4256 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
4257 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4258 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
4259 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
4260 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
4261 98eaaa12 2019-08-03 stsp return NULL;
4262 98eaaa12 2019-08-03 stsp }
4263 3fc0c068 2019-02-10 stsp
4264 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
4265 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
4266 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
4267 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
4268 3fc0c068 2019-02-10 stsp a->header_shown = 1;
4269 3fc0c068 2019-02-10 stsp }
4270 b72f483a 2019-02-05 stsp
4271 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4272 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
4273 98eaaa12 2019-08-03 stsp switch (staged_status) {
4274 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
4275 98eaaa12 2019-08-03 stsp label1 = path;
4276 98eaaa12 2019-08-03 stsp label2 = path;
4277 98eaaa12 2019-08-03 stsp break;
4278 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
4279 98eaaa12 2019-08-03 stsp label2 = path;
4280 98eaaa12 2019-08-03 stsp break;
4281 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
4282 98eaaa12 2019-08-03 stsp label1 = path;
4283 98eaaa12 2019-08-03 stsp break;
4284 98eaaa12 2019-08-03 stsp default:
4285 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
4286 98eaaa12 2019-08-03 stsp }
4287 fe621944 2020-11-10 stsp return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4288 fe621944 2020-11-10 stsp staged_blob_id, label1, label2, a->diff_context,
4289 64453f7e 2020-11-21 stsp a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4290 98eaaa12 2019-08-03 stsp }
4291 98eaaa12 2019-08-03 stsp
4292 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4293 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
4294 4ce46740 2019-08-08 stsp char *id_str;
4295 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4296 408b4ebc 2019-08-03 stsp 8192);
4297 4ce46740 2019-08-08 stsp if (err)
4298 4ce46740 2019-08-08 stsp goto done;
4299 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4300 4ce46740 2019-08-08 stsp if (err)
4301 4ce46740 2019-08-08 stsp goto done;
4302 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4303 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4304 4ce46740 2019-08-08 stsp free(id_str);
4305 4ce46740 2019-08-08 stsp goto done;
4306 4ce46740 2019-08-08 stsp }
4307 4ce46740 2019-08-08 stsp free(id_str);
4308 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4309 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4310 4ce46740 2019-08-08 stsp if (err)
4311 4ce46740 2019-08-08 stsp goto done;
4312 4ce46740 2019-08-08 stsp }
4313 d00136be 2019-03-26 stsp
4314 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4315 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4316 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4317 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4318 2ec1f75b 2019-03-26 stsp goto done;
4319 2ec1f75b 2019-03-26 stsp }
4320 b72f483a 2019-02-05 stsp
4321 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4322 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4323 12463d8b 2019-12-13 stsp if (fd == -1) {
4324 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4325 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4326 39449a05 2020-07-23 stsp abspath);
4327 39449a05 2020-07-23 stsp goto done;
4328 39449a05 2020-07-23 stsp }
4329 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4330 39449a05 2020-07-23 stsp de_name, abspath);
4331 39449a05 2020-07-23 stsp if (err)
4332 39449a05 2020-07-23 stsp goto done;
4333 12463d8b 2019-12-13 stsp }
4334 12463d8b 2019-12-13 stsp } else {
4335 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4336 12463d8b 2019-12-13 stsp if (fd == -1) {
4337 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4338 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4339 39449a05 2020-07-23 stsp abspath);
4340 39449a05 2020-07-23 stsp goto done;
4341 39449a05 2020-07-23 stsp }
4342 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4343 39449a05 2020-07-23 stsp de_name, abspath);
4344 39449a05 2020-07-23 stsp if (err)
4345 39449a05 2020-07-23 stsp goto done;
4346 12463d8b 2019-12-13 stsp }
4347 12463d8b 2019-12-13 stsp }
4348 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4349 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4350 2ec1f75b 2019-03-26 stsp goto done;
4351 2ec1f75b 2019-03-26 stsp }
4352 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4353 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4354 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4355 2ec1f75b 2019-03-26 stsp goto done;
4356 2ec1f75b 2019-03-26 stsp }
4357 12463d8b 2019-12-13 stsp fd = -1;
4358 2ec1f75b 2019-03-26 stsp } else
4359 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4360 b72f483a 2019-02-05 stsp
4361 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4362 64453f7e 2020-11-21 stsp a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4363 b72f483a 2019-02-05 stsp done:
4364 b72f483a 2019-02-05 stsp if (blob1)
4365 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4366 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4367 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4368 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4369 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4370 b72f483a 2019-02-05 stsp free(abspath);
4371 927df6b7 2019-02-10 stsp return err;
4372 927df6b7 2019-02-10 stsp }
4373 927df6b7 2019-02-10 stsp
4374 927df6b7 2019-02-10 stsp static const struct got_error *
4375 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4376 b00d56cd 2018-04-01 stsp {
4377 b00d56cd 2018-04-01 stsp const struct got_error *error;
4378 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4379 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4380 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4381 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4382 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
4383 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
4384 15a94983 2018-12-23 stsp int type1, type2;
4385 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4386 64453f7e 2020-11-21 stsp int force_text_diff = 0;
4387 c0cc5c62 2018-10-18 stsp const char *errstr;
4388 927df6b7 2019-02-10 stsp char *path = NULL;
4389 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4390 b00d56cd 2018-04-01 stsp
4391 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4392 84de9106 2020-12-26 stsp
4393 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4394 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4395 25eccc22 2019-01-04 stsp NULL) == -1)
4396 b00d56cd 2018-04-01 stsp err(1, "pledge");
4397 b00d56cd 2018-04-01 stsp #endif
4398 b00d56cd 2018-04-01 stsp
4399 64453f7e 2020-11-21 stsp while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4400 b00d56cd 2018-04-01 stsp switch (ch) {
4401 64453f7e 2020-11-21 stsp case 'a':
4402 64453f7e 2020-11-21 stsp force_text_diff = 1;
4403 64453f7e 2020-11-21 stsp break;
4404 c0cc5c62 2018-10-18 stsp case 'C':
4405 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4406 dfcab68b 2019-11-29 kn &errstr);
4407 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4408 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4409 c0cc5c62 2018-10-18 stsp break;
4410 b72f483a 2019-02-05 stsp case 'r':
4411 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4412 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4413 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4414 9ba1d308 2019-10-21 stsp optarg);
4415 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4416 b72f483a 2019-02-05 stsp break;
4417 98eaaa12 2019-08-03 stsp case 's':
4418 98eaaa12 2019-08-03 stsp diff_staged = 1;
4419 63035f9f 2019-10-06 stsp break;
4420 63035f9f 2019-10-06 stsp case 'w':
4421 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4422 98eaaa12 2019-08-03 stsp break;
4423 b00d56cd 2018-04-01 stsp default:
4424 2deda0b9 2019-03-07 stsp usage_diff();
4425 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4426 b00d56cd 2018-04-01 stsp }
4427 b00d56cd 2018-04-01 stsp }
4428 b00d56cd 2018-04-01 stsp
4429 b00d56cd 2018-04-01 stsp argc -= optind;
4430 b00d56cd 2018-04-01 stsp argv += optind;
4431 b00d56cd 2018-04-01 stsp
4432 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4433 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4434 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4435 b72f483a 2019-02-05 stsp goto done;
4436 b72f483a 2019-02-05 stsp }
4437 927df6b7 2019-02-10 stsp if (argc <= 1) {
4438 b72f483a 2019-02-05 stsp if (repo_path)
4439 b72f483a 2019-02-05 stsp errx(1,
4440 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
4441 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4442 fa51e947 2020-03-27 stsp if (error) {
4443 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4444 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
4445 fa51e947 2020-03-27 stsp cwd);
4446 1f03b8da 2020-03-20 stsp goto done;
4447 fa51e947 2020-03-27 stsp }
4448 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4449 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4450 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4451 927df6b7 2019-02-10 stsp goto done;
4452 927df6b7 2019-02-10 stsp }
4453 927df6b7 2019-02-10 stsp if (argc == 1) {
4454 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4455 cbd1af7a 2019-03-18 stsp argv[0]);
4456 927df6b7 2019-02-10 stsp if (error)
4457 927df6b7 2019-02-10 stsp goto done;
4458 927df6b7 2019-02-10 stsp } else {
4459 927df6b7 2019-02-10 stsp path = strdup("");
4460 927df6b7 2019-02-10 stsp if (path == NULL) {
4461 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4462 927df6b7 2019-02-10 stsp goto done;
4463 927df6b7 2019-02-10 stsp }
4464 927df6b7 2019-02-10 stsp }
4465 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4466 98eaaa12 2019-08-03 stsp if (diff_staged)
4467 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4468 98eaaa12 2019-08-03 stsp "objects in repository");
4469 15a94983 2018-12-23 stsp id_str1 = argv[0];
4470 15a94983 2018-12-23 stsp id_str2 = argv[1];
4471 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4472 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4473 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4474 30db809c 2019-06-05 stsp goto done;
4475 1a1242a9 2021-04-01 kn repo_path = strdup(worktree ?
4476 1a1242a9 2021-04-01 kn got_worktree_get_repo_path(worktree) : cwd);
4477 1a1242a9 2021-04-01 kn if (repo_path == NULL) {
4478 1a1242a9 2021-04-01 kn error = got_error_from_errno("strdup");
4479 1a1242a9 2021-04-01 kn goto done;
4480 30db809c 2019-06-05 stsp }
4481 30db809c 2019-06-05 stsp }
4482 b00d56cd 2018-04-01 stsp } else
4483 b00d56cd 2018-04-01 stsp usage_diff();
4484 b00d56cd 2018-04-01 stsp
4485 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4486 76089277 2018-04-01 stsp free(repo_path);
4487 b00d56cd 2018-04-01 stsp if (error != NULL)
4488 b00d56cd 2018-04-01 stsp goto done;
4489 b00d56cd 2018-04-01 stsp
4490 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4491 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4492 c02c541e 2019-03-29 stsp if (error)
4493 c02c541e 2019-03-29 stsp goto done;
4494 c02c541e 2019-03-29 stsp
4495 30db809c 2019-06-05 stsp if (argc <= 1) {
4496 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4497 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4498 b72f483a 2019-02-05 stsp char *id_str;
4499 72ea6654 2019-07-27 stsp
4500 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4501 72ea6654 2019-07-27 stsp
4502 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4503 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4504 b72f483a 2019-02-05 stsp if (error)
4505 b72f483a 2019-02-05 stsp goto done;
4506 b72f483a 2019-02-05 stsp arg.repo = repo;
4507 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4508 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4509 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4510 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4511 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4512 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4513 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
4514 b72f483a 2019-02-05 stsp
4515 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4516 72ea6654 2019-07-27 stsp if (error)
4517 72ea6654 2019-07-27 stsp goto done;
4518 72ea6654 2019-07-27 stsp
4519 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
4520 f6343036 2021-06-22 stsp print_diff, &arg, check_cancelled, NULL);
4521 b72f483a 2019-02-05 stsp free(id_str);
4522 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4523 b72f483a 2019-02-05 stsp goto done;
4524 b72f483a 2019-02-05 stsp }
4525 84de9106 2020-12-26 stsp
4526 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4527 84de9106 2020-12-26 stsp if (error)
4528 84de9106 2020-12-26 stsp return error;
4529 b72f483a 2019-02-05 stsp
4530 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4531 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4532 0adb7196 2019-08-11 stsp if (error)
4533 0adb7196 2019-08-11 stsp goto done;
4534 b00d56cd 2018-04-01 stsp
4535 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4536 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4537 0adb7196 2019-08-11 stsp if (error)
4538 0adb7196 2019-08-11 stsp goto done;
4539 b00d56cd 2018-04-01 stsp
4540 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4541 15a94983 2018-12-23 stsp if (error)
4542 15a94983 2018-12-23 stsp goto done;
4543 15a94983 2018-12-23 stsp
4544 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4545 15a94983 2018-12-23 stsp if (error)
4546 15a94983 2018-12-23 stsp goto done;
4547 15a94983 2018-12-23 stsp
4548 15a94983 2018-12-23 stsp if (type1 != type2) {
4549 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4550 b00d56cd 2018-04-01 stsp goto done;
4551 b00d56cd 2018-04-01 stsp }
4552 b00d56cd 2018-04-01 stsp
4553 15a94983 2018-12-23 stsp switch (type1) {
4554 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4555 fe621944 2020-11-10 stsp error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4556 64453f7e 2020-11-21 stsp NULL, NULL, diff_context, ignore_whitespace,
4557 64453f7e 2020-11-21 stsp force_text_diff, repo, stdout);
4558 b00d56cd 2018-04-01 stsp break;
4559 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4560 fe621944 2020-11-10 stsp error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4561 64453f7e 2020-11-21 stsp "", "", diff_context, ignore_whitespace, force_text_diff,
4562 64453f7e 2020-11-21 stsp repo, stdout);
4563 b00d56cd 2018-04-01 stsp break;
4564 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4565 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4566 fe621944 2020-11-10 stsp error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4567 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, repo,
4568 64453f7e 2020-11-21 stsp stdout);
4569 b00d56cd 2018-04-01 stsp break;
4570 b00d56cd 2018-04-01 stsp default:
4571 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4572 b00d56cd 2018-04-01 stsp }
4573 b00d56cd 2018-04-01 stsp done:
4574 5e70831e 2019-05-28 stsp free(label1);
4575 5e70831e 2019-05-28 stsp free(label2);
4576 15a94983 2018-12-23 stsp free(id1);
4577 15a94983 2018-12-23 stsp free(id2);
4578 927df6b7 2019-02-10 stsp free(path);
4579 b72f483a 2019-02-05 stsp if (worktree)
4580 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4581 ad242220 2018-09-08 stsp if (repo) {
4582 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4583 ad242220 2018-09-08 stsp if (error == NULL)
4584 1d0f4054 2021-06-17 stsp error = close_err;
4585 ad242220 2018-09-08 stsp }
4586 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4587 b00d56cd 2018-04-01 stsp return error;
4588 404c43c4 2018-06-21 stsp }
4589 404c43c4 2018-06-21 stsp
4590 404c43c4 2018-06-21 stsp __dead static void
4591 404c43c4 2018-06-21 stsp usage_blame(void)
4592 404c43c4 2018-06-21 stsp {
4593 9270e621 2019-02-05 stsp fprintf(stderr,
4594 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4595 404c43c4 2018-06-21 stsp getprogname());
4596 404c43c4 2018-06-21 stsp exit(1);
4597 b00d56cd 2018-04-01 stsp }
4598 b00d56cd 2018-04-01 stsp
4599 28315671 2019-08-14 stsp struct blame_line {
4600 28315671 2019-08-14 stsp int annotated;
4601 28315671 2019-08-14 stsp char *id_str;
4602 82f6abb8 2019-08-14 stsp char *committer;
4603 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4604 28315671 2019-08-14 stsp };
4605 28315671 2019-08-14 stsp
4606 28315671 2019-08-14 stsp struct blame_cb_args {
4607 28315671 2019-08-14 stsp struct blame_line *lines;
4608 28315671 2019-08-14 stsp int nlines;
4609 7ef28ff8 2019-08-14 stsp int nlines_prec;
4610 28315671 2019-08-14 stsp int lineno_cur;
4611 28315671 2019-08-14 stsp off_t *line_offsets;
4612 28315671 2019-08-14 stsp FILE *f;
4613 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4614 28315671 2019-08-14 stsp };
4615 28315671 2019-08-14 stsp
4616 404c43c4 2018-06-21 stsp static const struct got_error *
4617 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4618 28315671 2019-08-14 stsp {
4619 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4620 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4621 28315671 2019-08-14 stsp struct blame_line *bline;
4622 82f6abb8 2019-08-14 stsp char *line = NULL;
4623 28315671 2019-08-14 stsp size_t linesize = 0;
4624 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4625 28315671 2019-08-14 stsp off_t offset;
4626 bcb49d15 2019-08-14 stsp struct tm tm;
4627 bcb49d15 2019-08-14 stsp time_t committer_time;
4628 28315671 2019-08-14 stsp
4629 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4630 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4631 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4632 28315671 2019-08-14 stsp
4633 28315671 2019-08-14 stsp if (sigint_received)
4634 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4635 28315671 2019-08-14 stsp
4636 2839f8b9 2019-08-18 stsp if (lineno == -1)
4637 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4638 2839f8b9 2019-08-18 stsp
4639 28315671 2019-08-14 stsp /* Annotate this line. */
4640 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4641 28315671 2019-08-14 stsp if (bline->annotated)
4642 28315671 2019-08-14 stsp return NULL;
4643 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4644 28315671 2019-08-14 stsp if (err)
4645 28315671 2019-08-14 stsp return err;
4646 82f6abb8 2019-08-14 stsp
4647 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4648 82f6abb8 2019-08-14 stsp if (err)
4649 82f6abb8 2019-08-14 stsp goto done;
4650 82f6abb8 2019-08-14 stsp
4651 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4652 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4653 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4654 bcb49d15 2019-08-14 stsp goto done;
4655 bcb49d15 2019-08-14 stsp }
4656 bcb49d15 2019-08-14 stsp
4657 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4658 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
4659 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
4660 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4661 ec6d1a36 2021-03-21 jrick &tm) == 0) {
4662 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4663 82f6abb8 2019-08-14 stsp goto done;
4664 82f6abb8 2019-08-14 stsp }
4665 28315671 2019-08-14 stsp bline->annotated = 1;
4666 28315671 2019-08-14 stsp
4667 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4668 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4669 28315671 2019-08-14 stsp if (!bline->annotated)
4670 82f6abb8 2019-08-14 stsp goto done;
4671 28315671 2019-08-14 stsp
4672 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4673 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4674 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4675 82f6abb8 2019-08-14 stsp goto done;
4676 82f6abb8 2019-08-14 stsp }
4677 28315671 2019-08-14 stsp
4678 28315671 2019-08-14 stsp while (bline->annotated) {
4679 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4680 82f6abb8 2019-08-14 stsp size_t len;
4681 82f6abb8 2019-08-14 stsp
4682 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4683 28315671 2019-08-14 stsp if (ferror(a->f))
4684 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4685 28315671 2019-08-14 stsp break;
4686 28315671 2019-08-14 stsp }
4687 82f6abb8 2019-08-14 stsp
4688 82f6abb8 2019-08-14 stsp committer = bline->committer;
4689 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4690 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4691 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4692 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4693 82f6abb8 2019-08-14 stsp if (at)
4694 82f6abb8 2019-08-14 stsp *at = '\0';
4695 82f6abb8 2019-08-14 stsp len = strlen(committer);
4696 82f6abb8 2019-08-14 stsp if (len >= 9)
4697 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4698 28315671 2019-08-14 stsp
4699 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4700 28315671 2019-08-14 stsp if (nl)
4701 28315671 2019-08-14 stsp *nl = '\0';
4702 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4703 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4704 28315671 2019-08-14 stsp
4705 28315671 2019-08-14 stsp a->lineno_cur++;
4706 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4707 28315671 2019-08-14 stsp }
4708 82f6abb8 2019-08-14 stsp done:
4709 82f6abb8 2019-08-14 stsp if (commit)
4710 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4711 28315671 2019-08-14 stsp free(line);
4712 28315671 2019-08-14 stsp return err;
4713 28315671 2019-08-14 stsp }
4714 28315671 2019-08-14 stsp
4715 28315671 2019-08-14 stsp static const struct got_error *
4716 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4717 404c43c4 2018-06-21 stsp {
4718 404c43c4 2018-06-21 stsp const struct got_error *error;
4719 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4720 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4721 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4722 0587e10c 2020-07-23 stsp char *link_target = NULL;
4723 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4724 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4725 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4726 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4727 28315671 2019-08-14 stsp struct blame_cb_args bca;
4728 28315671 2019-08-14 stsp int ch, obj_type, i;
4729 be659d10 2020-11-18 stsp off_t filesize;
4730 90f3c347 2019-08-19 stsp
4731 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4732 404c43c4 2018-06-21 stsp
4733 404c43c4 2018-06-21 stsp #ifndef PROFILE
4734 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4735 36e2fb66 2019-01-04 stsp NULL) == -1)
4736 404c43c4 2018-06-21 stsp err(1, "pledge");
4737 404c43c4 2018-06-21 stsp #endif
4738 404c43c4 2018-06-21 stsp
4739 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4740 404c43c4 2018-06-21 stsp switch (ch) {
4741 404c43c4 2018-06-21 stsp case 'c':
4742 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4743 404c43c4 2018-06-21 stsp break;
4744 66bea077 2018-08-02 stsp case 'r':
4745 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4746 66bea077 2018-08-02 stsp if (repo_path == NULL)
4747 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4748 9ba1d308 2019-10-21 stsp optarg);
4749 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4750 66bea077 2018-08-02 stsp break;
4751 404c43c4 2018-06-21 stsp default:
4752 2deda0b9 2019-03-07 stsp usage_blame();
4753 404c43c4 2018-06-21 stsp /* NOTREACHED */
4754 404c43c4 2018-06-21 stsp }
4755 404c43c4 2018-06-21 stsp }
4756 404c43c4 2018-06-21 stsp
4757 404c43c4 2018-06-21 stsp argc -= optind;
4758 404c43c4 2018-06-21 stsp argv += optind;
4759 404c43c4 2018-06-21 stsp
4760 a39318fd 2018-08-02 stsp if (argc == 1)
4761 404c43c4 2018-06-21 stsp path = argv[0];
4762 a39318fd 2018-08-02 stsp else
4763 404c43c4 2018-06-21 stsp usage_blame();
4764 404c43c4 2018-06-21 stsp
4765 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4766 66bea077 2018-08-02 stsp if (cwd == NULL) {
4767 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4768 66bea077 2018-08-02 stsp goto done;
4769 66bea077 2018-08-02 stsp }
4770 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4771 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4772 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4773 66bea077 2018-08-02 stsp goto done;
4774 0c06baac 2019-02-05 stsp else
4775 0c06baac 2019-02-05 stsp error = NULL;
4776 0c06baac 2019-02-05 stsp if (worktree) {
4777 0c06baac 2019-02-05 stsp repo_path =
4778 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4779 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4780 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4781 1f03b8da 2020-03-20 stsp if (error)
4782 1f03b8da 2020-03-20 stsp goto done;
4783 1f03b8da 2020-03-20 stsp }
4784 0c06baac 2019-02-05 stsp } else {
4785 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4786 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4787 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4788 0c06baac 2019-02-05 stsp goto done;
4789 0c06baac 2019-02-05 stsp }
4790 66bea077 2018-08-02 stsp }
4791 66bea077 2018-08-02 stsp }
4792 36e2fb66 2019-01-04 stsp
4793 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4794 c02c541e 2019-03-29 stsp if (error != NULL)
4795 404c43c4 2018-06-21 stsp goto done;
4796 404c43c4 2018-06-21 stsp
4797 0c06baac 2019-02-05 stsp if (worktree) {
4798 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4799 01740607 2020-11-04 stsp char *p;
4800 01740607 2020-11-04 stsp
4801 01740607 2020-11-04 stsp error = got_worktree_resolve_path(&p, worktree, path);
4802 01740607 2020-11-04 stsp if (error)
4803 01740607 2020-11-04 stsp goto done;
4804 01740607 2020-11-04 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4805 01740607 2020-11-04 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4806 01740607 2020-11-04 stsp p) == -1) {
4807 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4808 01740607 2020-11-04 stsp free(p);
4809 0c06baac 2019-02-05 stsp goto done;
4810 0c06baac 2019-02-05 stsp }
4811 0c06baac 2019-02-05 stsp free(p);
4812 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4813 0c06baac 2019-02-05 stsp } else {
4814 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4815 01740607 2020-11-04 stsp if (error)
4816 01740607 2020-11-04 stsp goto done;
4817 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
4818 0c06baac 2019-02-05 stsp }
4819 0c06baac 2019-02-05 stsp if (error)
4820 66bea077 2018-08-02 stsp goto done;
4821 66bea077 2018-08-02 stsp
4822 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4823 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4824 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4825 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4826 404c43c4 2018-06-21 stsp if (error != NULL)
4827 66bea077 2018-08-02 stsp goto done;
4828 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4829 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4830 404c43c4 2018-06-21 stsp if (error != NULL)
4831 66bea077 2018-08-02 stsp goto done;
4832 404c43c4 2018-06-21 stsp } else {
4833 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4834 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4835 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4836 84de9106 2020-12-26 stsp NULL);
4837 84de9106 2020-12-26 stsp if (error)
4838 84de9106 2020-12-26 stsp goto done;
4839 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4840 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4841 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4842 30837e32 2019-07-25 stsp if (error)
4843 66bea077 2018-08-02 stsp goto done;
4844 404c43c4 2018-06-21 stsp }
4845 404c43c4 2018-06-21 stsp
4846 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4847 0587e10c 2020-07-23 stsp commit_id, repo);
4848 0587e10c 2020-07-23 stsp if (error)
4849 0587e10c 2020-07-23 stsp goto done;
4850 0587e10c 2020-07-23 stsp
4851 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4852 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4853 28315671 2019-08-14 stsp if (error)
4854 28315671 2019-08-14 stsp goto done;
4855 28315671 2019-08-14 stsp
4856 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4857 28315671 2019-08-14 stsp if (error)
4858 28315671 2019-08-14 stsp goto done;
4859 28315671 2019-08-14 stsp
4860 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4861 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4862 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4863 28315671 2019-08-14 stsp goto done;
4864 28315671 2019-08-14 stsp }
4865 28315671 2019-08-14 stsp
4866 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4867 28315671 2019-08-14 stsp if (error)
4868 28315671 2019-08-14 stsp goto done;
4869 28315671 2019-08-14 stsp bca.f = got_opentemp();
4870 28315671 2019-08-14 stsp if (bca.f == NULL) {
4871 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4872 28315671 2019-08-14 stsp goto done;
4873 28315671 2019-08-14 stsp }
4874 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4875 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4876 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4877 28315671 2019-08-14 stsp goto done;
4878 28315671 2019-08-14 stsp
4879 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4880 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4881 b02560ec 2019-08-19 stsp bca.nlines--;
4882 b02560ec 2019-08-19 stsp
4883 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4884 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4885 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4886 28315671 2019-08-14 stsp goto done;
4887 28315671 2019-08-14 stsp }
4888 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4889 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4890 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4891 7ef28ff8 2019-08-14 stsp while (i > 0) {
4892 7ef28ff8 2019-08-14 stsp i /= 10;
4893 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4894 7ef28ff8 2019-08-14 stsp }
4895 82f6abb8 2019-08-14 stsp bca.repo = repo;
4896 7ef28ff8 2019-08-14 stsp
4897 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4898 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4899 404c43c4 2018-06-21 stsp done:
4900 66bea077 2018-08-02 stsp free(in_repo_path);
4901 0587e10c 2020-07-23 stsp free(link_target);
4902 66bea077 2018-08-02 stsp free(repo_path);
4903 66bea077 2018-08-02 stsp free(cwd);
4904 404c43c4 2018-06-21 stsp free(commit_id);
4905 28315671 2019-08-14 stsp free(obj_id);
4906 28315671 2019-08-14 stsp if (blob)
4907 28315671 2019-08-14 stsp got_object_blob_close(blob);
4908 0c06baac 2019-02-05 stsp if (worktree)
4909 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4910 ad242220 2018-09-08 stsp if (repo) {
4911 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4912 ad242220 2018-09-08 stsp if (error == NULL)
4913 1d0f4054 2021-06-17 stsp error = close_err;
4914 ad242220 2018-09-08 stsp }
4915 3affba96 2019-09-22 stsp if (bca.lines) {
4916 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4917 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4918 3affba96 2019-09-22 stsp free(bline->id_str);
4919 3affba96 2019-09-22 stsp free(bline->committer);
4920 3affba96 2019-09-22 stsp }
4921 3affba96 2019-09-22 stsp free(bca.lines);
4922 28315671 2019-08-14 stsp }
4923 28315671 2019-08-14 stsp free(bca.line_offsets);
4924 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4925 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4926 404c43c4 2018-06-21 stsp return error;
4927 5de5890b 2018-10-18 stsp }
4928 5de5890b 2018-10-18 stsp
4929 5de5890b 2018-10-18 stsp __dead static void
4930 5de5890b 2018-10-18 stsp usage_tree(void)
4931 5de5890b 2018-10-18 stsp {
4932 c1669e2e 2019-01-09 stsp fprintf(stderr,
4933 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4934 5de5890b 2018-10-18 stsp getprogname());
4935 5de5890b 2018-10-18 stsp exit(1);
4936 5de5890b 2018-10-18 stsp }
4937 5de5890b 2018-10-18 stsp
4938 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4939 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4940 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4941 c1669e2e 2019-01-09 stsp {
4942 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4943 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4944 848d6979 2019-08-12 stsp const char *modestr = "";
4945 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4946 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4947 5de5890b 2018-10-18 stsp
4948 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4949 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4950 c1669e2e 2019-01-09 stsp path++;
4951 c1669e2e 2019-01-09 stsp
4952 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4953 63c5ca5d 2019-08-24 stsp modestr = "$";
4954 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4955 0d6c6ee3 2020-05-20 stsp int i;
4956 0d6c6ee3 2020-05-20 stsp
4957 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4958 0d6c6ee3 2020-05-20 stsp if (err)
4959 0d6c6ee3 2020-05-20 stsp return err;
4960 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4961 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4962 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4963 0d6c6ee3 2020-05-20 stsp }
4964 0d6c6ee3 2020-05-20 stsp
4965 848d6979 2019-08-12 stsp modestr = "@";
4966 0d6c6ee3 2020-05-20 stsp }
4967 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4968 848d6979 2019-08-12 stsp modestr = "/";
4969 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4970 848d6979 2019-08-12 stsp modestr = "*";
4971 848d6979 2019-08-12 stsp
4972 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4973 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4974 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4975 0d6c6ee3 2020-05-20 stsp
4976 0d6c6ee3 2020-05-20 stsp free(link_target);
4977 0d6c6ee3 2020-05-20 stsp return NULL;
4978 c1669e2e 2019-01-09 stsp }
4979 c1669e2e 2019-01-09 stsp
4980 5de5890b 2018-10-18 stsp static const struct got_error *
4981 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4982 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4983 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4984 5de5890b 2018-10-18 stsp {
4985 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4986 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4987 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4988 56e0773d 2019-11-28 stsp int nentries, i;
4989 5de5890b 2018-10-18 stsp
4990 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4991 5de5890b 2018-10-18 stsp if (err)
4992 5de5890b 2018-10-18 stsp goto done;
4993 5de5890b 2018-10-18 stsp
4994 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4995 5de5890b 2018-10-18 stsp if (err)
4996 5de5890b 2018-10-18 stsp goto done;
4997 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4998 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4999 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5000 5de5890b 2018-10-18 stsp char *id = NULL;
5001 84453469 2018-11-11 stsp
5002 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
5003 84453469 2018-11-11 stsp break;
5004 84453469 2018-11-11 stsp
5005 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
5006 5de5890b 2018-10-18 stsp if (show_ids) {
5007 5de5890b 2018-10-18 stsp char *id_str;
5008 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5009 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5010 5de5890b 2018-10-18 stsp if (err)
5011 5de5890b 2018-10-18 stsp goto done;
5012 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
5013 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5014 5de5890b 2018-10-18 stsp free(id_str);
5015 5de5890b 2018-10-18 stsp goto done;
5016 5de5890b 2018-10-18 stsp }
5017 5de5890b 2018-10-18 stsp free(id_str);
5018 5de5890b 2018-10-18 stsp }
5019 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
5020 5de5890b 2018-10-18 stsp free(id);
5021 0d6c6ee3 2020-05-20 stsp if (err)
5022 0d6c6ee3 2020-05-20 stsp goto done;
5023 c1669e2e 2019-01-09 stsp
5024 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5025 c1669e2e 2019-01-09 stsp char *child_path;
5026 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
5027 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
5028 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
5029 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5030 c1669e2e 2019-01-09 stsp goto done;
5031 c1669e2e 2019-01-09 stsp }
5032 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
5033 c1669e2e 2019-01-09 stsp root_path, repo);
5034 c1669e2e 2019-01-09 stsp free(child_path);
5035 c1669e2e 2019-01-09 stsp if (err)
5036 c1669e2e 2019-01-09 stsp goto done;
5037 c1669e2e 2019-01-09 stsp }
5038 5de5890b 2018-10-18 stsp }
5039 5de5890b 2018-10-18 stsp done:
5040 5de5890b 2018-10-18 stsp if (tree)
5041 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
5042 5de5890b 2018-10-18 stsp free(tree_id);
5043 5de5890b 2018-10-18 stsp return err;
5044 404c43c4 2018-06-21 stsp }
5045 404c43c4 2018-06-21 stsp
5046 5de5890b 2018-10-18 stsp static const struct got_error *
5047 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
5048 5de5890b 2018-10-18 stsp {
5049 5de5890b 2018-10-18 stsp const struct got_error *error;
5050 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
5051 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
5052 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
5053 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5054 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
5055 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
5056 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
5057 5de5890b 2018-10-18 stsp int ch;
5058 5de5890b 2018-10-18 stsp
5059 5de5890b 2018-10-18 stsp #ifndef PROFILE
5060 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5061 0f8d269b 2019-01-04 stsp NULL) == -1)
5062 5de5890b 2018-10-18 stsp err(1, "pledge");
5063 5de5890b 2018-10-18 stsp #endif
5064 5de5890b 2018-10-18 stsp
5065 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5066 5de5890b 2018-10-18 stsp switch (ch) {
5067 5de5890b 2018-10-18 stsp case 'c':
5068 5de5890b 2018-10-18 stsp commit_id_str = optarg;
5069 5de5890b 2018-10-18 stsp break;
5070 5de5890b 2018-10-18 stsp case 'r':
5071 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
5072 5de5890b 2018-10-18 stsp if (repo_path == NULL)
5073 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5074 9ba1d308 2019-10-21 stsp optarg);
5075 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5076 5de5890b 2018-10-18 stsp break;
5077 5de5890b 2018-10-18 stsp case 'i':
5078 5de5890b 2018-10-18 stsp show_ids = 1;
5079 5de5890b 2018-10-18 stsp break;
5080 c1669e2e 2019-01-09 stsp case 'R':
5081 c1669e2e 2019-01-09 stsp recurse = 1;
5082 c1669e2e 2019-01-09 stsp break;
5083 5de5890b 2018-10-18 stsp default:
5084 2deda0b9 2019-03-07 stsp usage_tree();
5085 5de5890b 2018-10-18 stsp /* NOTREACHED */
5086 5de5890b 2018-10-18 stsp }
5087 5de5890b 2018-10-18 stsp }
5088 5de5890b 2018-10-18 stsp
5089 5de5890b 2018-10-18 stsp argc -= optind;
5090 5de5890b 2018-10-18 stsp argv += optind;
5091 5de5890b 2018-10-18 stsp
5092 5de5890b 2018-10-18 stsp if (argc == 1)
5093 5de5890b 2018-10-18 stsp path = argv[0];
5094 5de5890b 2018-10-18 stsp else if (argc > 1)
5095 5de5890b 2018-10-18 stsp usage_tree();
5096 5de5890b 2018-10-18 stsp else
5097 7a2c19d6 2019-02-05 stsp path = NULL;
5098 7a2c19d6 2019-02-05 stsp
5099 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
5100 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
5101 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5102 9bf7a39b 2019-02-05 stsp goto done;
5103 9bf7a39b 2019-02-05 stsp }
5104 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
5105 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5106 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5107 7a2c19d6 2019-02-05 stsp goto done;
5108 7a2c19d6 2019-02-05 stsp else
5109 7a2c19d6 2019-02-05 stsp error = NULL;
5110 7a2c19d6 2019-02-05 stsp if (worktree) {
5111 7a2c19d6 2019-02-05 stsp repo_path =
5112 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5113 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
5114 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5115 7a2c19d6 2019-02-05 stsp if (error)
5116 7a2c19d6 2019-02-05 stsp goto done;
5117 7a2c19d6 2019-02-05 stsp } else {
5118 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
5119 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
5120 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5121 7a2c19d6 2019-02-05 stsp goto done;
5122 7a2c19d6 2019-02-05 stsp }
5123 5de5890b 2018-10-18 stsp }
5124 5de5890b 2018-10-18 stsp }
5125 5de5890b 2018-10-18 stsp
5126 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5127 5de5890b 2018-10-18 stsp if (error != NULL)
5128 c02c541e 2019-03-29 stsp goto done;
5129 c02c541e 2019-03-29 stsp
5130 4fedbf4c 2020-11-07 stsp if (worktree) {
5131 4fedbf4c 2020-11-07 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
5132 4fedbf4c 2020-11-07 stsp char *p;
5133 5de5890b 2018-10-18 stsp
5134 4fedbf4c 2020-11-07 stsp if (path == NULL)
5135 4fedbf4c 2020-11-07 stsp path = "";
5136 4fedbf4c 2020-11-07 stsp error = got_worktree_resolve_path(&p, worktree, path);
5137 4fedbf4c 2020-11-07 stsp if (error)
5138 4fedbf4c 2020-11-07 stsp goto done;
5139 4fedbf4c 2020-11-07 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
5140 4fedbf4c 2020-11-07 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5141 4fedbf4c 2020-11-07 stsp p) == -1) {
5142 4fedbf4c 2020-11-07 stsp error = got_error_from_errno("asprintf");
5143 9bf7a39b 2019-02-05 stsp free(p);
5144 4fedbf4c 2020-11-07 stsp goto done;
5145 4fedbf4c 2020-11-07 stsp }
5146 4fedbf4c 2020-11-07 stsp free(p);
5147 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5148 4fedbf4c 2020-11-07 stsp if (error)
5149 4fedbf4c 2020-11-07 stsp goto done;
5150 4fedbf4c 2020-11-07 stsp } else {
5151 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5152 4fedbf4c 2020-11-07 stsp if (error)
5153 4fedbf4c 2020-11-07 stsp goto done;
5154 4fedbf4c 2020-11-07 stsp if (path == NULL)
5155 9bf7a39b 2019-02-05 stsp path = "/";
5156 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
5157 9bf7a39b 2019-02-05 stsp if (error != NULL)
5158 9bf7a39b 2019-02-05 stsp goto done;
5159 9bf7a39b 2019-02-05 stsp }
5160 5de5890b 2018-10-18 stsp
5161 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
5162 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
5163 4e0a20a4 2020-03-23 tracey if (worktree)
5164 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
5165 4e0a20a4 2020-03-23 tracey else
5166 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
5167 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
5168 5de5890b 2018-10-18 stsp if (error != NULL)
5169 5de5890b 2018-10-18 stsp goto done;
5170 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5171 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
5172 5de5890b 2018-10-18 stsp if (error != NULL)
5173 5de5890b 2018-10-18 stsp goto done;
5174 5de5890b 2018-10-18 stsp } else {
5175 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5176 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5177 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5178 84de9106 2020-12-26 stsp NULL);
5179 84de9106 2020-12-26 stsp if (error)
5180 84de9106 2020-12-26 stsp goto done;
5181 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5182 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5183 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5184 30837e32 2019-07-25 stsp if (error)
5185 5de5890b 2018-10-18 stsp goto done;
5186 5de5890b 2018-10-18 stsp }
5187 5de5890b 2018-10-18 stsp
5188 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5189 c1669e2e 2019-01-09 stsp in_repo_path, repo);
5190 5de5890b 2018-10-18 stsp done:
5191 5de5890b 2018-10-18 stsp free(in_repo_path);
5192 5de5890b 2018-10-18 stsp free(repo_path);
5193 5de5890b 2018-10-18 stsp free(cwd);
5194 5de5890b 2018-10-18 stsp free(commit_id);
5195 7a2c19d6 2019-02-05 stsp if (worktree)
5196 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
5197 5de5890b 2018-10-18 stsp if (repo) {
5198 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5199 5de5890b 2018-10-18 stsp if (error == NULL)
5200 1d0f4054 2021-06-17 stsp error = close_err;
5201 5de5890b 2018-10-18 stsp }
5202 5de5890b 2018-10-18 stsp return error;
5203 5de5890b 2018-10-18 stsp }
5204 5de5890b 2018-10-18 stsp
5205 6bad629b 2019-02-04 stsp __dead static void
5206 6bad629b 2019-02-04 stsp usage_status(void)
5207 6bad629b 2019-02-04 stsp {
5208 f6343036 2021-06-22 stsp fprintf(stderr, "usage: %s status [-I] [-s status-codes ] [path ...]\n",
5209 081470ac 2020-08-13 stsp getprogname());
5210 6bad629b 2019-02-04 stsp exit(1);
5211 6bad629b 2019-02-04 stsp }
5212 5c860e29 2018-03-12 stsp
5213 b72f483a 2019-02-05 stsp static const struct got_error *
5214 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
5215 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5216 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5217 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5218 6bad629b 2019-02-04 stsp {
5219 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5220 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
5221 081470ac 2020-08-13 stsp if (arg) {
5222 081470ac 2020-08-13 stsp char *status_codes = arg;
5223 081470ac 2020-08-13 stsp size_t ncodes = strlen(status_codes);
5224 081470ac 2020-08-13 stsp int i;
5225 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
5226 081470ac 2020-08-13 stsp if (status == status_codes[i] ||
5227 081470ac 2020-08-13 stsp staged_status == status_codes[i])
5228 081470ac 2020-08-13 stsp break;
5229 081470ac 2020-08-13 stsp }
5230 081470ac 2020-08-13 stsp if (i == ncodes)
5231 081470ac 2020-08-13 stsp return NULL;
5232 081470ac 2020-08-13 stsp }
5233 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
5234 b72f483a 2019-02-05 stsp return NULL;
5235 6bad629b 2019-02-04 stsp }
5236 5c860e29 2018-03-12 stsp
5237 6bad629b 2019-02-04 stsp static const struct got_error *
5238 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
5239 6bad629b 2019-02-04 stsp {
5240 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
5241 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
5242 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
5243 081470ac 2020-08-13 stsp char *cwd = NULL, *status_codes = NULL;;
5244 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
5245 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
5246 f6343036 2021-06-22 stsp int ch, i, no_ignores = 0;
5247 5c860e29 2018-03-12 stsp
5248 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
5249 72ea6654 2019-07-27 stsp
5250 f6343036 2021-06-22 stsp while ((ch = getopt(argc, argv, "Is:")) != -1) {
5251 6bad629b 2019-02-04 stsp switch (ch) {
5252 f6343036 2021-06-22 stsp case 'I':
5253 f6343036 2021-06-22 stsp no_ignores = 1;
5254 f6343036 2021-06-22 stsp break;
5255 081470ac 2020-08-13 stsp case 's':
5256 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
5257 081470ac 2020-08-13 stsp switch (optarg[i]) {
5258 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
5259 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
5260 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
5261 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
5262 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
5263 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
5264 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
5265 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
5266 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
5267 081470ac 2020-08-13 stsp break;
5268 081470ac 2020-08-13 stsp default:
5269 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
5270 081470ac 2020-08-13 stsp optarg[i]);
5271 081470ac 2020-08-13 stsp }
5272 081470ac 2020-08-13 stsp }
5273 081470ac 2020-08-13 stsp status_codes = optarg;
5274 081470ac 2020-08-13 stsp break;
5275 5c860e29 2018-03-12 stsp default:
5276 2deda0b9 2019-03-07 stsp usage_status();
5277 6bad629b 2019-02-04 stsp /* NOTREACHED */
5278 5c860e29 2018-03-12 stsp }
5279 5c860e29 2018-03-12 stsp }
5280 5c860e29 2018-03-12 stsp
5281 6bad629b 2019-02-04 stsp argc -= optind;
5282 6bad629b 2019-02-04 stsp argv += optind;
5283 5c860e29 2018-03-12 stsp
5284 6bad629b 2019-02-04 stsp #ifndef PROFILE
5285 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5286 6bad629b 2019-02-04 stsp NULL) == -1)
5287 6bad629b 2019-02-04 stsp err(1, "pledge");
5288 f42b1b34 2018-03-12 stsp #endif
5289 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
5290 927df6b7 2019-02-10 stsp if (cwd == NULL) {
5291 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5292 927df6b7 2019-02-10 stsp goto done;
5293 927df6b7 2019-02-10 stsp }
5294 927df6b7 2019-02-10 stsp
5295 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
5296 fa51e947 2020-03-27 stsp if (error) {
5297 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5298 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
5299 a5edda0a 2019-07-27 stsp goto done;
5300 fa51e947 2020-03-27 stsp }
5301 6bad629b 2019-02-04 stsp
5302 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5303 c9956ddf 2019-09-08 stsp NULL);
5304 6bad629b 2019-02-04 stsp if (error != NULL)
5305 6bad629b 2019-02-04 stsp goto done;
5306 6bad629b 2019-02-04 stsp
5307 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5308 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5309 087fb88c 2019-08-04 stsp if (error)
5310 087fb88c 2019-08-04 stsp goto done;
5311 087fb88c 2019-08-04 stsp
5312 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5313 6bad629b 2019-02-04 stsp if (error)
5314 6bad629b 2019-02-04 stsp goto done;
5315 6bad629b 2019-02-04 stsp
5316 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, no_ignores,
5317 f6343036 2021-06-22 stsp print_status, status_codes, check_cancelled, NULL);
5318 6bad629b 2019-02-04 stsp done:
5319 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
5320 a5edda0a 2019-07-27 stsp free((char *)pe->path);
5321 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
5322 927df6b7 2019-02-10 stsp free(cwd);
5323 d0eebce4 2019-03-11 stsp return error;
5324 d0eebce4 2019-03-11 stsp }
5325 d0eebce4 2019-03-11 stsp
5326 d0eebce4 2019-03-11 stsp __dead static void
5327 d0eebce4 2019-03-11 stsp usage_ref(void)
5328 d0eebce4 2019-03-11 stsp {
5329 d0eebce4 2019-03-11 stsp fprintf(stderr,
5330 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5331 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
5332 d0eebce4 2019-03-11 stsp getprogname());
5333 d0eebce4 2019-03-11 stsp exit(1);
5334 d0eebce4 2019-03-11 stsp }
5335 d0eebce4 2019-03-11 stsp
5336 d0eebce4 2019-03-11 stsp static const struct got_error *
5337 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
5338 d0eebce4 2019-03-11 stsp {
5339 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
5340 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
5341 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
5342 d0eebce4 2019-03-11 stsp
5343 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5344 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5345 d0eebce4 2019-03-11 stsp if (err)
5346 d0eebce4 2019-03-11 stsp return err;
5347 d0eebce4 2019-03-11 stsp
5348 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
5349 d0eebce4 2019-03-11 stsp char *refstr;
5350 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
5351 d0eebce4 2019-03-11 stsp if (refstr == NULL)
5352 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
5353 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5354 d0eebce4 2019-03-11 stsp free(refstr);
5355 d0eebce4 2019-03-11 stsp }
5356 d0eebce4 2019-03-11 stsp
5357 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5358 d0eebce4 2019-03-11 stsp return NULL;
5359 d0eebce4 2019-03-11 stsp }
5360 d0eebce4 2019-03-11 stsp
5361 d0eebce4 2019-03-11 stsp static const struct got_error *
5362 161728eb 2021-07-24 stsp delete_ref_by_name(struct got_repository *repo, const char *refname)
5363 d0eebce4 2019-03-11 stsp {
5364 161728eb 2021-07-24 stsp const struct got_error *err;
5365 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5366 d0eebce4 2019-03-11 stsp
5367 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5368 d0eebce4 2019-03-11 stsp if (err)
5369 d0eebce4 2019-03-11 stsp return err;
5370 993f033b 2021-07-16 stsp
5371 161728eb 2021-07-24 stsp err = delete_ref(repo, ref);
5372 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5373 d0eebce4 2019-03-11 stsp return err;
5374 d0eebce4 2019-03-11 stsp }
5375 d0eebce4 2019-03-11 stsp
5376 d0eebce4 2019-03-11 stsp static const struct got_error *
5377 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
5378 d0eebce4 2019-03-11 stsp {
5379 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5380 d0eebce4 2019-03-11 stsp struct got_object_id *id;
5381 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
5382 d1644381 2019-07-14 stsp
5383 d1644381 2019-07-14 stsp /*
5384 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5385 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
5386 d1644381 2019-07-14 stsp * an unintended typo.
5387 d1644381 2019-07-14 stsp */
5388 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5389 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5390 d0eebce4 2019-03-11 stsp
5391 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5392 dd88155e 2019-06-29 stsp repo);
5393 d83d9d5c 2019-05-13 stsp if (err) {
5394 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
5395 d0eebce4 2019-03-11 stsp
5396 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5397 d83d9d5c 2019-05-13 stsp return err;
5398 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
5399 d83d9d5c 2019-05-13 stsp if (err)
5400 d83d9d5c 2019-05-13 stsp return err;
5401 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
5402 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
5403 d83d9d5c 2019-05-13 stsp if (err)
5404 d83d9d5c 2019-05-13 stsp return err;
5405 d83d9d5c 2019-05-13 stsp }
5406 d83d9d5c 2019-05-13 stsp
5407 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5408 d0eebce4 2019-03-11 stsp if (err)
5409 d0eebce4 2019-03-11 stsp goto done;
5410 d0eebce4 2019-03-11 stsp
5411 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5412 d0eebce4 2019-03-11 stsp done:
5413 d0eebce4 2019-03-11 stsp if (ref)
5414 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5415 d0eebce4 2019-03-11 stsp free(id);
5416 d1c1ae5f 2019-08-12 stsp return err;
5417 d1c1ae5f 2019-08-12 stsp }
5418 d1c1ae5f 2019-08-12 stsp
5419 d1c1ae5f 2019-08-12 stsp static const struct got_error *
5420 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
5421 d1c1ae5f 2019-08-12 stsp {
5422 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
5423 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
5424 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
5425 d1c1ae5f 2019-08-12 stsp
5426 d1c1ae5f 2019-08-12 stsp /*
5427 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5428 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
5429 d1c1ae5f 2019-08-12 stsp * an unintended typo.
5430 d1c1ae5f 2019-08-12 stsp */
5431 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5432 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5433 d1c1ae5f 2019-08-12 stsp
5434 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5435 d1c1ae5f 2019-08-12 stsp if (err)
5436 d1c1ae5f 2019-08-12 stsp return err;
5437 d1c1ae5f 2019-08-12 stsp
5438 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5439 d1c1ae5f 2019-08-12 stsp if (err)
5440 d1c1ae5f 2019-08-12 stsp goto done;
5441 d1c1ae5f 2019-08-12 stsp
5442 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5443 d1c1ae5f 2019-08-12 stsp done:
5444 d1c1ae5f 2019-08-12 stsp if (target_ref)
5445 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5446 d1c1ae5f 2019-08-12 stsp if (ref)
5447 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5448 d0eebce4 2019-03-11 stsp return err;
5449 d0eebce4 2019-03-11 stsp }
5450 d0eebce4 2019-03-11 stsp
5451 d0eebce4 2019-03-11 stsp static const struct got_error *
5452 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5453 d0eebce4 2019-03-11 stsp {
5454 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
5455 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
5456 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
5457 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
5458 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
5459 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
5460 b2070a3f 2020-03-22 stsp char *refname = NULL;
5461 d0eebce4 2019-03-11 stsp
5462 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5463 d0eebce4 2019-03-11 stsp switch (ch) {
5464 e31abbf2 2020-03-22 stsp case 'c':
5465 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5466 e31abbf2 2020-03-22 stsp break;
5467 d0eebce4 2019-03-11 stsp case 'd':
5468 e31abbf2 2020-03-22 stsp do_delete = 1;
5469 d0eebce4 2019-03-11 stsp break;
5470 d0eebce4 2019-03-11 stsp case 'r':
5471 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5472 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5473 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5474 9ba1d308 2019-10-21 stsp optarg);
5475 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5476 d0eebce4 2019-03-11 stsp break;
5477 d0eebce4 2019-03-11 stsp case 'l':
5478 d0eebce4 2019-03-11 stsp do_list = 1;
5479 d1c1ae5f 2019-08-12 stsp break;
5480 d1c1ae5f 2019-08-12 stsp case 's':
5481 e31abbf2 2020-03-22 stsp symref_target = optarg;
5482 d0eebce4 2019-03-11 stsp break;
5483 d0eebce4 2019-03-11 stsp default:
5484 d0eebce4 2019-03-11 stsp usage_ref();
5485 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5486 d0eebce4 2019-03-11 stsp }
5487 d0eebce4 2019-03-11 stsp }
5488 d0eebce4 2019-03-11 stsp
5489 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5490 ff69268e 2020-12-13 stsp option_conflict('c', 'l');
5491 ff69268e 2020-12-13 stsp if (obj_arg && do_delete)
5492 ff69268e 2020-12-13 stsp option_conflict('c', 'd');
5493 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5494 ff69268e 2020-12-13 stsp option_conflict('c', 's');
5495 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5496 ff69268e 2020-12-13 stsp option_conflict('s', 'd');
5497 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5498 ff69268e 2020-12-13 stsp option_conflict('s', 'l');
5499 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5500 ff69268e 2020-12-13 stsp option_conflict('d', 'l');
5501 d0eebce4 2019-03-11 stsp
5502 d0eebce4 2019-03-11 stsp argc -= optind;
5503 d0eebce4 2019-03-11 stsp argv += optind;
5504 d0eebce4 2019-03-11 stsp
5505 e31abbf2 2020-03-22 stsp if (do_list) {
5506 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5507 d0eebce4 2019-03-11 stsp usage_ref();
5508 b2070a3f 2020-03-22 stsp if (argc == 1) {
5509 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5510 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5511 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5512 b2070a3f 2020-03-22 stsp goto done;
5513 b2070a3f 2020-03-22 stsp }
5514 b2070a3f 2020-03-22 stsp }
5515 e31abbf2 2020-03-22 stsp } else {
5516 e31abbf2 2020-03-22 stsp if (argc != 1)
5517 e31abbf2 2020-03-22 stsp usage_ref();
5518 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5519 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5520 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5521 b2070a3f 2020-03-22 stsp goto done;
5522 b2070a3f 2020-03-22 stsp }
5523 e31abbf2 2020-03-22 stsp }
5524 b2070a3f 2020-03-22 stsp
5525 b2070a3f 2020-03-22 stsp if (refname)
5526 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5527 d0eebce4 2019-03-11 stsp
5528 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5529 e0b57350 2019-03-12 stsp if (do_list) {
5530 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5531 e0b57350 2019-03-12 stsp NULL) == -1)
5532 e0b57350 2019-03-12 stsp err(1, "pledge");
5533 e0b57350 2019-03-12 stsp } else {
5534 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5535 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5536 e0b57350 2019-03-12 stsp err(1, "pledge");
5537 e0b57350 2019-03-12 stsp }
5538 d0eebce4 2019-03-11 stsp #endif
5539 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5540 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5541 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5542 d0eebce4 2019-03-11 stsp goto done;
5543 d0eebce4 2019-03-11 stsp }
5544 d0eebce4 2019-03-11 stsp
5545 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5546 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5547 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5548 d0eebce4 2019-03-11 stsp goto done;
5549 d0eebce4 2019-03-11 stsp else
5550 d0eebce4 2019-03-11 stsp error = NULL;
5551 d0eebce4 2019-03-11 stsp if (worktree) {
5552 d0eebce4 2019-03-11 stsp repo_path =
5553 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5554 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5555 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5556 d0eebce4 2019-03-11 stsp if (error)
5557 d0eebce4 2019-03-11 stsp goto done;
5558 d0eebce4 2019-03-11 stsp } else {
5559 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5560 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5561 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5562 d0eebce4 2019-03-11 stsp goto done;
5563 d0eebce4 2019-03-11 stsp }
5564 d0eebce4 2019-03-11 stsp }
5565 d0eebce4 2019-03-11 stsp }
5566 d0eebce4 2019-03-11 stsp
5567 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5568 d0eebce4 2019-03-11 stsp if (error != NULL)
5569 d0eebce4 2019-03-11 stsp goto done;
5570 d0eebce4 2019-03-11 stsp
5571 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5572 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5573 c02c541e 2019-03-29 stsp if (error)
5574 c02c541e 2019-03-29 stsp goto done;
5575 c02c541e 2019-03-29 stsp
5576 d0eebce4 2019-03-11 stsp if (do_list)
5577 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5578 e31abbf2 2020-03-22 stsp else if (do_delete)
5579 161728eb 2021-07-24 stsp error = delete_ref_by_name(repo, refname);
5580 e31abbf2 2020-03-22 stsp else if (symref_target)
5581 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5582 e31abbf2 2020-03-22 stsp else {
5583 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5584 e31abbf2 2020-03-22 stsp usage_ref();
5585 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5586 e31abbf2 2020-03-22 stsp }
5587 4e759de4 2019-06-26 stsp done:
5588 b2070a3f 2020-03-22 stsp free(refname);
5589 1d0f4054 2021-06-17 stsp if (repo) {
5590 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5591 1d0f4054 2021-06-17 stsp if (error == NULL)
5592 1d0f4054 2021-06-17 stsp error = close_err;
5593 1d0f4054 2021-06-17 stsp }
5594 4e759de4 2019-06-26 stsp if (worktree)
5595 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5596 4e759de4 2019-06-26 stsp free(cwd);
5597 4e759de4 2019-06-26 stsp free(repo_path);
5598 4e759de4 2019-06-26 stsp return error;
5599 4e759de4 2019-06-26 stsp }
5600 4e759de4 2019-06-26 stsp
5601 4e759de4 2019-06-26 stsp __dead static void
5602 4e759de4 2019-06-26 stsp usage_branch(void)
5603 4e759de4 2019-06-26 stsp {
5604 4e759de4 2019-06-26 stsp fprintf(stderr,
5605 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5606 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5607 4e759de4 2019-06-26 stsp exit(1);
5608 4e759de4 2019-06-26 stsp }
5609 4e759de4 2019-06-26 stsp
5610 4e759de4 2019-06-26 stsp static const struct got_error *
5611 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5612 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5613 4e99b47e 2019-10-04 stsp {
5614 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5615 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5616 4e99b47e 2019-10-04 stsp char *refstr;
5617 4e99b47e 2019-10-04 stsp
5618 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5619 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5620 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5621 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5622 4e99b47e 2019-10-04 stsp
5623 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5624 4e99b47e 2019-10-04 stsp if (err)
5625 4e99b47e 2019-10-04 stsp return err;
5626 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5627 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5628 4e99b47e 2019-10-04 stsp marker = "* ";
5629 4e99b47e 2019-10-04 stsp else
5630 4e99b47e 2019-10-04 stsp marker = "~ ";
5631 4e99b47e 2019-10-04 stsp free(id);
5632 4e99b47e 2019-10-04 stsp }
5633 4e99b47e 2019-10-04 stsp
5634 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5635 4e99b47e 2019-10-04 stsp refname += 11;
5636 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5637 4e99b47e 2019-10-04 stsp refname += 18;
5638 34d4e04c 2021-02-08 stsp if (strncmp(refname, "refs/remotes/", 13) == 0)
5639 34d4e04c 2021-02-08 stsp refname += 13;
5640 4e99b47e 2019-10-04 stsp
5641 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5642 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5643 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5644 4e99b47e 2019-10-04 stsp
5645 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5646 4e99b47e 2019-10-04 stsp free(refstr);
5647 4e99b47e 2019-10-04 stsp return NULL;
5648 4e99b47e 2019-10-04 stsp }
5649 4e99b47e 2019-10-04 stsp
5650 4e99b47e 2019-10-04 stsp static const struct got_error *
5651 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5652 ad89fa31 2019-10-04 stsp {
5653 ad89fa31 2019-10-04 stsp const char *refname;
5654 ad89fa31 2019-10-04 stsp
5655 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5656 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5657 ad89fa31 2019-10-04 stsp
5658 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5659 ad89fa31 2019-10-04 stsp
5660 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5661 ad89fa31 2019-10-04 stsp refname += 11;
5662 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5663 ad89fa31 2019-10-04 stsp refname += 18;
5664 ad89fa31 2019-10-04 stsp
5665 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5666 ad89fa31 2019-10-04 stsp
5667 ad89fa31 2019-10-04 stsp return NULL;
5668 ad89fa31 2019-10-04 stsp }
5669 ad89fa31 2019-10-04 stsp
5670 ad89fa31 2019-10-04 stsp static const struct got_error *
5671 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5672 4e759de4 2019-06-26 stsp {
5673 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5674 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5675 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5676 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5677 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5678 4e759de4 2019-06-26 stsp
5679 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5680 ba882ee3 2019-07-11 stsp
5681 4e99b47e 2019-10-04 stsp if (worktree) {
5682 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5683 4e99b47e 2019-10-04 stsp worktree);
5684 4e99b47e 2019-10-04 stsp if (err)
5685 4e99b47e 2019-10-04 stsp return err;
5686 4e99b47e 2019-10-04 stsp
5687 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5688 4e99b47e 2019-10-04 stsp worktree);
5689 4e99b47e 2019-10-04 stsp if (err)
5690 4e99b47e 2019-10-04 stsp return err;
5691 4e99b47e 2019-10-04 stsp
5692 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5693 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5694 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5695 4e99b47e 2019-10-04 stsp if (err)
5696 4e99b47e 2019-10-04 stsp return err;
5697 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5698 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5699 4e99b47e 2019-10-04 stsp }
5700 4e99b47e 2019-10-04 stsp }
5701 4e99b47e 2019-10-04 stsp
5702 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5703 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5704 4e759de4 2019-06-26 stsp if (err)
5705 4e759de4 2019-06-26 stsp return err;
5706 4e759de4 2019-06-26 stsp
5707 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry)
5708 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5709 4e759de4 2019-06-26 stsp
5710 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5711 34d4e04c 2021-02-08 stsp
5712 34d4e04c 2021-02-08 stsp err = got_ref_list(&refs, repo, "refs/remotes",
5713 34d4e04c 2021-02-08 stsp got_ref_cmp_by_name, NULL);
5714 34d4e04c 2021-02-08 stsp if (err)
5715 34d4e04c 2021-02-08 stsp return err;
5716 34d4e04c 2021-02-08 stsp
5717 34d4e04c 2021-02-08 stsp TAILQ_FOREACH(re, &refs, entry)
5718 34d4e04c 2021-02-08 stsp list_branch(repo, worktree, re->ref);
5719 34d4e04c 2021-02-08 stsp
5720 34d4e04c 2021-02-08 stsp got_ref_list_free(&refs);
5721 34d4e04c 2021-02-08 stsp
5722 4e759de4 2019-06-26 stsp return NULL;
5723 4e759de4 2019-06-26 stsp }
5724 4e759de4 2019-06-26 stsp
5725 4e759de4 2019-06-26 stsp static const struct got_error *
5726 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5727 45cd4e47 2019-08-25 stsp const char *branch_name)
5728 4e759de4 2019-06-26 stsp {
5729 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5730 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5731 4e759de4 2019-06-26 stsp char *refname;
5732 4e759de4 2019-06-26 stsp
5733 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5734 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5735 4e759de4 2019-06-26 stsp
5736 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5737 4e759de4 2019-06-26 stsp if (err)
5738 4e759de4 2019-06-26 stsp goto done;
5739 4e759de4 2019-06-26 stsp
5740 45cd4e47 2019-08-25 stsp if (worktree &&
5741 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5742 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5743 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5744 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5745 45cd4e47 2019-08-25 stsp goto done;
5746 45cd4e47 2019-08-25 stsp }
5747 45cd4e47 2019-08-25 stsp
5748 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
5749 4e759de4 2019-06-26 stsp done:
5750 45cd4e47 2019-08-25 stsp if (ref)
5751 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5752 4e759de4 2019-06-26 stsp free(refname);
5753 4e759de4 2019-06-26 stsp return err;
5754 4e759de4 2019-06-26 stsp }
5755 4e759de4 2019-06-26 stsp
5756 4e759de4 2019-06-26 stsp static const struct got_error *
5757 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5758 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5759 4e759de4 2019-06-26 stsp {
5760 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5761 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5762 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5763 d3f84d51 2019-07-11 stsp
5764 d3f84d51 2019-07-11 stsp /*
5765 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5766 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5767 d3f84d51 2019-07-11 stsp * an unintended typo.
5768 d3f84d51 2019-07-11 stsp */
5769 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5770 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5771 4e759de4 2019-06-26 stsp
5772 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5773 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
5774 62d463ca 2020-10-20 naddy goto done;
5775 4e759de4 2019-06-26 stsp }
5776 4e759de4 2019-06-26 stsp
5777 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5778 4e759de4 2019-06-26 stsp if (err == NULL) {
5779 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5780 4e759de4 2019-06-26 stsp goto done;
5781 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5782 4e759de4 2019-06-26 stsp goto done;
5783 4e759de4 2019-06-26 stsp
5784 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5785 4e759de4 2019-06-26 stsp if (err)
5786 4e759de4 2019-06-26 stsp goto done;
5787 4e759de4 2019-06-26 stsp
5788 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5789 d0eebce4 2019-03-11 stsp done:
5790 4e759de4 2019-06-26 stsp if (ref)
5791 4e759de4 2019-06-26 stsp got_ref_close(ref);
5792 4e759de4 2019-06-26 stsp free(base_refname);
5793 4e759de4 2019-06-26 stsp free(refname);
5794 4e759de4 2019-06-26 stsp return err;
5795 4e759de4 2019-06-26 stsp }
5796 4e759de4 2019-06-26 stsp
5797 4e759de4 2019-06-26 stsp static const struct got_error *
5798 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5799 4e759de4 2019-06-26 stsp {
5800 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5801 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5802 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5803 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5804 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5805 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5806 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5807 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5808 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5809 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5810 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5811 4e759de4 2019-06-26 stsp
5812 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5813 da76fce2 2020-02-24 stsp
5814 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5815 4e759de4 2019-06-26 stsp switch (ch) {
5816 a74f7e83 2019-11-10 stsp case 'c':
5817 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5818 a74f7e83 2019-11-10 stsp break;
5819 4e759de4 2019-06-26 stsp case 'd':
5820 4e759de4 2019-06-26 stsp delref = optarg;
5821 4e759de4 2019-06-26 stsp break;
5822 4e759de4 2019-06-26 stsp case 'r':
5823 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5824 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5825 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5826 9ba1d308 2019-10-21 stsp optarg);
5827 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5828 4e759de4 2019-06-26 stsp break;
5829 4e759de4 2019-06-26 stsp case 'l':
5830 4e759de4 2019-06-26 stsp do_list = 1;
5831 da76fce2 2020-02-24 stsp break;
5832 da76fce2 2020-02-24 stsp case 'n':
5833 da76fce2 2020-02-24 stsp do_update = 0;
5834 4e759de4 2019-06-26 stsp break;
5835 4e759de4 2019-06-26 stsp default:
5836 4e759de4 2019-06-26 stsp usage_branch();
5837 4e759de4 2019-06-26 stsp /* NOTREACHED */
5838 4e759de4 2019-06-26 stsp }
5839 4e759de4 2019-06-26 stsp }
5840 4e759de4 2019-06-26 stsp
5841 4e759de4 2019-06-26 stsp if (do_list && delref)
5842 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
5843 4e759de4 2019-06-26 stsp
5844 4e759de4 2019-06-26 stsp argc -= optind;
5845 4e759de4 2019-06-26 stsp argv += optind;
5846 ad89fa31 2019-10-04 stsp
5847 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5848 ad89fa31 2019-10-04 stsp do_show = 1;
5849 4e759de4 2019-06-26 stsp
5850 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5851 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5852 a74f7e83 2019-11-10 stsp
5853 4e759de4 2019-06-26 stsp if (do_list || delref) {
5854 4e759de4 2019-06-26 stsp if (argc > 0)
5855 4e759de4 2019-06-26 stsp usage_branch();
5856 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5857 4e759de4 2019-06-26 stsp usage_branch();
5858 4e759de4 2019-06-26 stsp
5859 4e759de4 2019-06-26 stsp #ifndef PROFILE
5860 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5861 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5862 4e759de4 2019-06-26 stsp NULL) == -1)
5863 4e759de4 2019-06-26 stsp err(1, "pledge");
5864 4e759de4 2019-06-26 stsp } else {
5865 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5866 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5867 4e759de4 2019-06-26 stsp err(1, "pledge");
5868 4e759de4 2019-06-26 stsp }
5869 4e759de4 2019-06-26 stsp #endif
5870 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5871 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5872 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5873 4e759de4 2019-06-26 stsp goto done;
5874 4e759de4 2019-06-26 stsp }
5875 4e759de4 2019-06-26 stsp
5876 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5877 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5878 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5879 4e759de4 2019-06-26 stsp goto done;
5880 4e759de4 2019-06-26 stsp else
5881 4e759de4 2019-06-26 stsp error = NULL;
5882 4e759de4 2019-06-26 stsp if (worktree) {
5883 4e759de4 2019-06-26 stsp repo_path =
5884 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5885 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5886 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5887 4e759de4 2019-06-26 stsp if (error)
5888 4e759de4 2019-06-26 stsp goto done;
5889 4e759de4 2019-06-26 stsp } else {
5890 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5891 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5892 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5893 4e759de4 2019-06-26 stsp goto done;
5894 4e759de4 2019-06-26 stsp }
5895 4e759de4 2019-06-26 stsp }
5896 4e759de4 2019-06-26 stsp }
5897 4e759de4 2019-06-26 stsp
5898 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5899 4e759de4 2019-06-26 stsp if (error != NULL)
5900 4e759de4 2019-06-26 stsp goto done;
5901 4e759de4 2019-06-26 stsp
5902 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5903 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5904 4e759de4 2019-06-26 stsp if (error)
5905 4e759de4 2019-06-26 stsp goto done;
5906 4e759de4 2019-06-26 stsp
5907 ad89fa31 2019-10-04 stsp if (do_show)
5908 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5909 ad89fa31 2019-10-04 stsp else if (do_list)
5910 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5911 4e759de4 2019-06-26 stsp else if (delref)
5912 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5913 4e759de4 2019-06-26 stsp else {
5914 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5915 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5916 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5917 84de9106 2020-12-26 stsp NULL);
5918 84de9106 2020-12-26 stsp if (error)
5919 84de9106 2020-12-26 stsp goto done;
5920 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5921 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5922 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5923 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5924 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5925 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5926 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5927 a74f7e83 2019-11-10 stsp if (error)
5928 a74f7e83 2019-11-10 stsp goto done;
5929 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5930 da76fce2 2020-02-24 stsp if (error)
5931 da76fce2 2020-02-24 stsp goto done;
5932 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5933 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5934 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5935 da76fce2 2020-02-24 stsp
5936 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5937 da76fce2 2020-02-24 stsp if (error)
5938 da76fce2 2020-02-24 stsp goto done;
5939 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5940 da76fce2 2020-02-24 stsp worktree);
5941 da76fce2 2020-02-24 stsp if (error)
5942 da76fce2 2020-02-24 stsp goto done;
5943 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5944 da76fce2 2020-02-24 stsp == -1) {
5945 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5946 da76fce2 2020-02-24 stsp goto done;
5947 da76fce2 2020-02-24 stsp }
5948 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5949 da76fce2 2020-02-24 stsp free(branch_refname);
5950 da76fce2 2020-02-24 stsp if (error)
5951 da76fce2 2020-02-24 stsp goto done;
5952 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5953 da76fce2 2020-02-24 stsp repo);
5954 da76fce2 2020-02-24 stsp if (error)
5955 da76fce2 2020-02-24 stsp goto done;
5956 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5957 da76fce2 2020-02-24 stsp commit_id);
5958 da76fce2 2020-02-24 stsp if (error)
5959 da76fce2 2020-02-24 stsp goto done;
5960 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5961 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5962 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5963 9627c110 2020-04-18 stsp NULL);
5964 da76fce2 2020-02-24 stsp if (error)
5965 da76fce2 2020-02-24 stsp goto done;
5966 9627c110 2020-04-18 stsp if (upa.did_something)
5967 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5968 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
5969 da76fce2 2020-02-24 stsp }
5970 4e759de4 2019-06-26 stsp }
5971 4e759de4 2019-06-26 stsp done:
5972 da76fce2 2020-02-24 stsp if (ref)
5973 da76fce2 2020-02-24 stsp got_ref_close(ref);
5974 1d0f4054 2021-06-17 stsp if (repo) {
5975 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5976 1d0f4054 2021-06-17 stsp if (error == NULL)
5977 1d0f4054 2021-06-17 stsp error = close_err;
5978 1d0f4054 2021-06-17 stsp }
5979 d0eebce4 2019-03-11 stsp if (worktree)
5980 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
5981 d0eebce4 2019-03-11 stsp free(cwd);
5982 d0eebce4 2019-03-11 stsp free(repo_path);
5983 da76fce2 2020-02-24 stsp free(commit_id);
5984 da76fce2 2020-02-24 stsp free(commit_id_str);
5985 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
5986 da76fce2 2020-02-24 stsp free((char *)pe->path);
5987 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
5988 d00136be 2019-03-26 stsp return error;
5989 d00136be 2019-03-26 stsp }
5990 d00136be 2019-03-26 stsp
5991 8e7bd50a 2019-08-22 stsp
5992 d00136be 2019-03-26 stsp __dead static void
5993 8e7bd50a 2019-08-22 stsp usage_tag(void)
5994 8e7bd50a 2019-08-22 stsp {
5995 8e7bd50a 2019-08-22 stsp fprintf(stderr,
5996 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5997 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
5998 8e7bd50a 2019-08-22 stsp exit(1);
5999 b8bad2ba 2019-08-23 stsp }
6000 b8bad2ba 2019-08-23 stsp
6001 b8bad2ba 2019-08-23 stsp #if 0
6002 b8bad2ba 2019-08-23 stsp static const struct got_error *
6003 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6004 b8bad2ba 2019-08-23 stsp {
6005 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
6006 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
6007 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
6008 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
6009 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
6010 b8bad2ba 2019-08-23 stsp
6011 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(re, tags, entry) {
6012 dbdddfee 2021-06-23 naddy se = STAILQ_FIRST(sorted);
6013 b8bad2ba 2019-08-23 stsp if (se == NULL) {
6014 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6015 b8bad2ba 2019-08-23 stsp if (err)
6016 b8bad2ba 2019-08-23 stsp return err;
6017 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(sorted, new, entry);
6018 b8bad2ba 2019-08-23 stsp continue;
6019 b8bad2ba 2019-08-23 stsp } else {
6020 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
6021 b8bad2ba 2019-08-23 stsp if (err)
6022 b8bad2ba 2019-08-23 stsp break;
6023 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
6024 b8bad2ba 2019-08-23 stsp free(re_id);
6025 b8bad2ba 2019-08-23 stsp if (err)
6026 b8bad2ba 2019-08-23 stsp break;
6027 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
6028 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
6029 b8bad2ba 2019-08-23 stsp }
6030 b8bad2ba 2019-08-23 stsp
6031 b8bad2ba 2019-08-23 stsp while (se) {
6032 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
6033 b8bad2ba 2019-08-23 stsp if (err)
6034 b8bad2ba 2019-08-23 stsp break;
6035 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
6036 b8bad2ba 2019-08-23 stsp free(se_id);
6037 b8bad2ba 2019-08-23 stsp if (err)
6038 b8bad2ba 2019-08-23 stsp break;
6039 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
6040 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
6041 b8bad2ba 2019-08-23 stsp
6042 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
6043 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6044 b8bad2ba 2019-08-23 stsp if (err)
6045 b8bad2ba 2019-08-23 stsp return err;
6046 dbdddfee 2021-06-23 naddy STAILQ_INSERT_AFTER(sorted, se, new, entry);
6047 b8bad2ba 2019-08-23 stsp break;
6048 b8bad2ba 2019-08-23 stsp }
6049 dbdddfee 2021-06-23 naddy se = STAILQ_NEXT(se, entry);
6050 b8bad2ba 2019-08-23 stsp continue;
6051 b8bad2ba 2019-08-23 stsp }
6052 b8bad2ba 2019-08-23 stsp }
6053 b8bad2ba 2019-08-23 stsp done:
6054 b8bad2ba 2019-08-23 stsp return err;
6055 8e7bd50a 2019-08-22 stsp }
6056 b8bad2ba 2019-08-23 stsp #endif
6057 b8bad2ba 2019-08-23 stsp
6058 b8bad2ba 2019-08-23 stsp static const struct got_error *
6059 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
6060 8e7bd50a 2019-08-22 stsp {
6061 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
6062 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
6063 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
6064 8e7bd50a 2019-08-22 stsp
6065 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6066 8e7bd50a 2019-08-22 stsp
6067 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6068 8e7bd50a 2019-08-22 stsp if (err)
6069 8e7bd50a 2019-08-22 stsp return err;
6070 8e7bd50a 2019-08-22 stsp
6071 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
6072 8e7bd50a 2019-08-22 stsp const char *refname;
6073 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6074 8e7bd50a 2019-08-22 stsp char datebuf[26];
6075 d4efa91b 2020-01-14 stsp const char *tagger;
6076 8e7bd50a 2019-08-22 stsp time_t tagger_time;
6077 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
6078 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
6079 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
6080 8e7bd50a 2019-08-22 stsp
6081 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
6082 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
6083 8e7bd50a 2019-08-22 stsp continue;
6084 8e7bd50a 2019-08-22 stsp refname += 10;
6085 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
6086 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
6087 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
6088 8e7bd50a 2019-08-22 stsp break;
6089 8e7bd50a 2019-08-22 stsp }
6090 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6091 8e7bd50a 2019-08-22 stsp free(refstr);
6092 8e7bd50a 2019-08-22 stsp
6093 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
6094 8e7bd50a 2019-08-22 stsp if (err)
6095 8e7bd50a 2019-08-22 stsp break;
6096 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
6097 d4efa91b 2020-01-14 stsp if (err) {
6098 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
6099 d4efa91b 2020-01-14 stsp free(id);
6100 d4efa91b 2020-01-14 stsp break;
6101 d4efa91b 2020-01-14 stsp }
6102 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
6103 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
6104 d4efa91b 2020-01-14 stsp if (err) {
6105 d4efa91b 2020-01-14 stsp free(id);
6106 d4efa91b 2020-01-14 stsp break;
6107 d4efa91b 2020-01-14 stsp }
6108 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
6109 d4efa91b 2020-01-14 stsp tagger_time =
6110 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
6111 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
6112 d4efa91b 2020-01-14 stsp free(id);
6113 d4efa91b 2020-01-14 stsp if (err)
6114 d4efa91b 2020-01-14 stsp break;
6115 d4efa91b 2020-01-14 stsp } else {
6116 d4efa91b 2020-01-14 stsp free(id);
6117 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
6118 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
6119 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
6120 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
6121 d4efa91b 2020-01-14 stsp if (err)
6122 d4efa91b 2020-01-14 stsp break;
6123 d4efa91b 2020-01-14 stsp }
6124 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
6125 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
6126 2417344c 2019-08-23 stsp if (datestr)
6127 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
6128 d4efa91b 2020-01-14 stsp if (commit)
6129 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6130 d4efa91b 2020-01-14 stsp else {
6131 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
6132 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
6133 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6134 d4efa91b 2020-01-14 stsp id_str);
6135 d4efa91b 2020-01-14 stsp break;
6136 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
6137 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6138 d4efa91b 2020-01-14 stsp id_str);
6139 d4efa91b 2020-01-14 stsp break;
6140 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
6141 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6142 d4efa91b 2020-01-14 stsp id_str);
6143 d4efa91b 2020-01-14 stsp break;
6144 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
6145 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6146 d4efa91b 2020-01-14 stsp id_str);
6147 d4efa91b 2020-01-14 stsp break;
6148 d4efa91b 2020-01-14 stsp default:
6149 d4efa91b 2020-01-14 stsp break;
6150 d4efa91b 2020-01-14 stsp }
6151 8e7bd50a 2019-08-22 stsp }
6152 8e7bd50a 2019-08-22 stsp free(id_str);
6153 d4efa91b 2020-01-14 stsp if (commit) {
6154 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
6155 d4efa91b 2020-01-14 stsp if (err)
6156 d4efa91b 2020-01-14 stsp break;
6157 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
6158 d4efa91b 2020-01-14 stsp } else {
6159 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
6160 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
6161 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
6162 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
6163 d4efa91b 2020-01-14 stsp break;
6164 d4efa91b 2020-01-14 stsp }
6165 8e7bd50a 2019-08-22 stsp }
6166 8e7bd50a 2019-08-22 stsp
6167 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
6168 8e7bd50a 2019-08-22 stsp do {
6169 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
6170 8e7bd50a 2019-08-22 stsp if (line)
6171 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
6172 8e7bd50a 2019-08-22 stsp } while (line);
6173 8e7bd50a 2019-08-22 stsp free(tagmsg0);
6174 8e7bd50a 2019-08-22 stsp }
6175 8e7bd50a 2019-08-22 stsp
6176 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
6177 8e7bd50a 2019-08-22 stsp return NULL;
6178 8e7bd50a 2019-08-22 stsp }
6179 8e7bd50a 2019-08-22 stsp
6180 8e7bd50a 2019-08-22 stsp static const struct got_error *
6181 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6182 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
6183 8e7bd50a 2019-08-22 stsp {
6184 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6185 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
6186 f372d5cd 2019-10-21 stsp char *editor = NULL;
6187 1601cb9f 2020-09-11 naddy int initial_content_len;
6188 8e7bd50a 2019-08-22 stsp int fd = -1;
6189 8e7bd50a 2019-08-22 stsp
6190 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6191 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6192 8e7bd50a 2019-08-22 stsp goto done;
6193 8e7bd50a 2019-08-22 stsp }
6194 8e7bd50a 2019-08-22 stsp
6195 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6196 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
6197 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
6198 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
6199 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6200 8e7bd50a 2019-08-22 stsp goto done;
6201 8e7bd50a 2019-08-22 stsp }
6202 8e7bd50a 2019-08-22 stsp
6203 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6204 8e7bd50a 2019-08-22 stsp if (err)
6205 8e7bd50a 2019-08-22 stsp goto done;
6206 8e7bd50a 2019-08-22 stsp
6207 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6208 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
6209 97972933 2020-09-11 stsp goto done;
6210 97972933 2020-09-11 stsp }
6211 8e7bd50a 2019-08-22 stsp
6212 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
6213 8e7bd50a 2019-08-22 stsp if (err)
6214 8e7bd50a 2019-08-22 stsp goto done;
6215 bfa12d5e 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6216 0d5bb276 2020-12-15 stsp initial_content_len, 1);
6217 8e7bd50a 2019-08-22 stsp done:
6218 8e7bd50a 2019-08-22 stsp free(initial_content);
6219 8e7bd50a 2019-08-22 stsp free(template);
6220 8e7bd50a 2019-08-22 stsp free(editor);
6221 8e7bd50a 2019-08-22 stsp
6222 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6223 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
6224 97972933 2020-09-11 stsp
6225 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
6226 59f86c76 2020-09-11 stsp if (err == NULL)
6227 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
6228 59f86c76 2020-09-11 stsp if (err) {
6229 59f86c76 2020-09-11 stsp free(*tagmsg);
6230 59f86c76 2020-09-11 stsp *tagmsg = NULL;
6231 8e7bd50a 2019-08-22 stsp }
6232 8e7bd50a 2019-08-22 stsp return err;
6233 8e7bd50a 2019-08-22 stsp }
6234 8e7bd50a 2019-08-22 stsp
6235 8e7bd50a 2019-08-22 stsp static const struct got_error *
6236 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
6237 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6238 8e7bd50a 2019-08-22 stsp {
6239 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6240 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
6241 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
6242 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
6243 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6244 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
6245 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
6246 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6247 8e7bd50a 2019-08-22 stsp
6248 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6249 84de9106 2020-12-26 stsp
6250 8e7bd50a 2019-08-22 stsp /*
6251 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
6252 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
6253 8e7bd50a 2019-08-22 stsp * an unintended typo.
6254 8e7bd50a 2019-08-22 stsp */
6255 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
6256 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6257 8e7bd50a 2019-08-22 stsp
6258 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
6259 8e7bd50a 2019-08-22 stsp if (err)
6260 8e7bd50a 2019-08-22 stsp return err;
6261 8e7bd50a 2019-08-22 stsp
6262 84de9106 2020-12-26 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6263 84de9106 2020-12-26 stsp if (err)
6264 84de9106 2020-12-26 stsp goto done;
6265 84de9106 2020-12-26 stsp
6266 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6267 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &refs, repo);
6268 8e7bd50a 2019-08-22 stsp if (err)
6269 8e7bd50a 2019-08-22 stsp goto done;
6270 8e7bd50a 2019-08-22 stsp
6271 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
6272 8e7bd50a 2019-08-22 stsp if (err)
6273 8e7bd50a 2019-08-22 stsp goto done;
6274 8e7bd50a 2019-08-22 stsp
6275 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
6276 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
6277 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
6278 62d463ca 2020-10-20 naddy err = got_error_from_errno("strdup");
6279 62d463ca 2020-10-20 naddy goto done;
6280 8e7bd50a 2019-08-22 stsp }
6281 8e7bd50a 2019-08-22 stsp tag_name += 10;
6282 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6283 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
6284 62d463ca 2020-10-20 naddy goto done;
6285 8e7bd50a 2019-08-22 stsp }
6286 8e7bd50a 2019-08-22 stsp
6287 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
6288 8e7bd50a 2019-08-22 stsp if (err == NULL) {
6289 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
6290 8e7bd50a 2019-08-22 stsp goto done;
6291 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
6292 8e7bd50a 2019-08-22 stsp goto done;
6293 8e7bd50a 2019-08-22 stsp
6294 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
6295 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6296 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
6297 f372d5cd 2019-10-21 stsp if (err) {
6298 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6299 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
6300 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6301 8e7bd50a 2019-08-22 stsp goto done;
6302 f372d5cd 2019-10-21 stsp }
6303 8e7bd50a 2019-08-22 stsp }
6304 8e7bd50a 2019-08-22 stsp
6305 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
6306 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6307 f372d5cd 2019-10-21 stsp if (err) {
6308 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6309 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6310 8e7bd50a 2019-08-22 stsp goto done;
6311 f372d5cd 2019-10-21 stsp }
6312 8e7bd50a 2019-08-22 stsp
6313 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
6314 f372d5cd 2019-10-21 stsp if (err) {
6315 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6316 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6317 8e7bd50a 2019-08-22 stsp goto done;
6318 f372d5cd 2019-10-21 stsp }
6319 8e7bd50a 2019-08-22 stsp
6320 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
6321 f372d5cd 2019-10-21 stsp if (err) {
6322 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6323 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6324 f372d5cd 2019-10-21 stsp goto done;
6325 f372d5cd 2019-10-21 stsp }
6326 8e7bd50a 2019-08-22 stsp
6327 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
6328 f372d5cd 2019-10-21 stsp if (err) {
6329 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6330 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6331 f372d5cd 2019-10-21 stsp goto done;
6332 8e7bd50a 2019-08-22 stsp }
6333 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
6334 8e7bd50a 2019-08-22 stsp done:
6335 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
6336 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
6337 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
6338 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6339 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
6340 f372d5cd 2019-10-21 stsp free(tag_id_str);
6341 8e7bd50a 2019-08-22 stsp if (ref)
6342 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
6343 8e7bd50a 2019-08-22 stsp free(commit_id);
6344 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6345 8e7bd50a 2019-08-22 stsp free(refname);
6346 8e7bd50a 2019-08-22 stsp free(tagmsg);
6347 f372d5cd 2019-10-21 stsp free(tagmsg_path);
6348 aba9c984 2019-09-08 stsp free(tagger);
6349 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
6350 8e7bd50a 2019-08-22 stsp return err;
6351 8e7bd50a 2019-08-22 stsp }
6352 8e7bd50a 2019-08-22 stsp
6353 8e7bd50a 2019-08-22 stsp static const struct got_error *
6354 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
6355 8e7bd50a 2019-08-22 stsp {
6356 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
6357 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
6358 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
6359 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6360 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
6361 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6362 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
6363 8e7bd50a 2019-08-22 stsp
6364 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6365 8e7bd50a 2019-08-22 stsp switch (ch) {
6366 80106605 2020-02-24 stsp case 'c':
6367 80106605 2020-02-24 stsp commit_id_arg = optarg;
6368 80106605 2020-02-24 stsp break;
6369 8e7bd50a 2019-08-22 stsp case 'm':
6370 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6371 8e7bd50a 2019-08-22 stsp break;
6372 8e7bd50a 2019-08-22 stsp case 'r':
6373 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6374 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6375 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6376 9ba1d308 2019-10-21 stsp optarg);
6377 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6378 8e7bd50a 2019-08-22 stsp break;
6379 8e7bd50a 2019-08-22 stsp case 'l':
6380 8e7bd50a 2019-08-22 stsp do_list = 1;
6381 8e7bd50a 2019-08-22 stsp break;
6382 8e7bd50a 2019-08-22 stsp default:
6383 8e7bd50a 2019-08-22 stsp usage_tag();
6384 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6385 8e7bd50a 2019-08-22 stsp }
6386 8e7bd50a 2019-08-22 stsp }
6387 8e7bd50a 2019-08-22 stsp
6388 8e7bd50a 2019-08-22 stsp argc -= optind;
6389 8e7bd50a 2019-08-22 stsp argv += optind;
6390 8e7bd50a 2019-08-22 stsp
6391 8e7bd50a 2019-08-22 stsp if (do_list) {
6392 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6393 775ce909 2020-03-22 stsp errx(1,
6394 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
6395 8e7bd50a 2019-08-22 stsp if (tagmsg)
6396 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
6397 8e7bd50a 2019-08-22 stsp if (argc > 0)
6398 8e7bd50a 2019-08-22 stsp usage_tag();
6399 80106605 2020-02-24 stsp } else if (argc != 1)
6400 8e7bd50a 2019-08-22 stsp usage_tag();
6401 80106605 2020-02-24 stsp
6402 a2887370 2019-08-23 stsp tag_name = argv[0];
6403 8e7bd50a 2019-08-22 stsp
6404 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
6405 8e7bd50a 2019-08-22 stsp if (do_list) {
6406 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6407 8e7bd50a 2019-08-22 stsp NULL) == -1)
6408 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6409 8e7bd50a 2019-08-22 stsp } else {
6410 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6411 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
6412 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6413 8e7bd50a 2019-08-22 stsp }
6414 8e7bd50a 2019-08-22 stsp #endif
6415 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
6416 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
6417 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
6418 8e7bd50a 2019-08-22 stsp goto done;
6419 8e7bd50a 2019-08-22 stsp }
6420 8e7bd50a 2019-08-22 stsp
6421 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6422 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6423 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6424 8e7bd50a 2019-08-22 stsp goto done;
6425 8e7bd50a 2019-08-22 stsp else
6426 8e7bd50a 2019-08-22 stsp error = NULL;
6427 8e7bd50a 2019-08-22 stsp if (worktree) {
6428 8e7bd50a 2019-08-22 stsp repo_path =
6429 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
6430 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6431 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6432 8e7bd50a 2019-08-22 stsp if (error)
6433 8e7bd50a 2019-08-22 stsp goto done;
6434 8e7bd50a 2019-08-22 stsp } else {
6435 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
6436 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6437 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6438 8e7bd50a 2019-08-22 stsp goto done;
6439 8e7bd50a 2019-08-22 stsp }
6440 8e7bd50a 2019-08-22 stsp }
6441 8e7bd50a 2019-08-22 stsp }
6442 8e7bd50a 2019-08-22 stsp
6443 8e7bd50a 2019-08-22 stsp if (do_list) {
6444 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6445 c9956ddf 2019-09-08 stsp if (error != NULL)
6446 c9956ddf 2019-09-08 stsp goto done;
6447 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6448 8e7bd50a 2019-08-22 stsp if (error)
6449 8e7bd50a 2019-08-22 stsp goto done;
6450 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
6451 8e7bd50a 2019-08-22 stsp } else {
6452 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6453 c9956ddf 2019-09-08 stsp if (error)
6454 c9956ddf 2019-09-08 stsp goto done;
6455 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
6456 c9956ddf 2019-09-08 stsp if (error != NULL)
6457 c9956ddf 2019-09-08 stsp goto done;
6458 c9956ddf 2019-09-08 stsp
6459 8e7bd50a 2019-08-22 stsp if (tagmsg) {
6460 42a285e2 2020-10-01 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6461 8e7bd50a 2019-08-22 stsp if (error)
6462 8e7bd50a 2019-08-22 stsp goto done;
6463 8e7bd50a 2019-08-22 stsp }
6464 8e7bd50a 2019-08-22 stsp
6465 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
6466 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
6467 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
6468 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
6469 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
6470 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
6471 8e7bd50a 2019-08-22 stsp if (error)
6472 8e7bd50a 2019-08-22 stsp goto done;
6473 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6474 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
6475 8e7bd50a 2019-08-22 stsp if (error)
6476 8e7bd50a 2019-08-22 stsp goto done;
6477 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
6478 8e7bd50a 2019-08-22 stsp free(commit_id);
6479 8e7bd50a 2019-08-22 stsp if (error)
6480 8e7bd50a 2019-08-22 stsp goto done;
6481 8e7bd50a 2019-08-22 stsp }
6482 8e7bd50a 2019-08-22 stsp
6483 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
6484 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6485 8e7bd50a 2019-08-22 stsp }
6486 8e7bd50a 2019-08-22 stsp done:
6487 1d0f4054 2021-06-17 stsp if (repo) {
6488 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6489 1d0f4054 2021-06-17 stsp if (error == NULL)
6490 1d0f4054 2021-06-17 stsp error = close_err;
6491 1d0f4054 2021-06-17 stsp }
6492 8e7bd50a 2019-08-22 stsp if (worktree)
6493 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
6494 8e7bd50a 2019-08-22 stsp free(cwd);
6495 8e7bd50a 2019-08-22 stsp free(repo_path);
6496 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6497 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6498 8e7bd50a 2019-08-22 stsp return error;
6499 8e7bd50a 2019-08-22 stsp }
6500 8e7bd50a 2019-08-22 stsp
6501 8e7bd50a 2019-08-22 stsp __dead static void
6502 d00136be 2019-03-26 stsp usage_add(void)
6503 d00136be 2019-03-26 stsp {
6504 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6505 022fae89 2019-12-06 tracey getprogname());
6506 d00136be 2019-03-26 stsp exit(1);
6507 d00136be 2019-03-26 stsp }
6508 d00136be 2019-03-26 stsp
6509 d00136be 2019-03-26 stsp static const struct got_error *
6510 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6511 4e68cba3 2019-11-23 stsp {
6512 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6513 4e68cba3 2019-11-23 stsp path++;
6514 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6515 4e68cba3 2019-11-23 stsp return NULL;
6516 4e68cba3 2019-11-23 stsp }
6517 4e68cba3 2019-11-23 stsp
6518 4e68cba3 2019-11-23 stsp static const struct got_error *
6519 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6520 d00136be 2019-03-26 stsp {
6521 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6522 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6523 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6524 1dd54920 2019-05-11 stsp char *cwd = NULL;
6525 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6526 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6527 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6528 1dd54920 2019-05-11 stsp
6529 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6530 d00136be 2019-03-26 stsp
6531 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6532 d00136be 2019-03-26 stsp switch (ch) {
6533 022fae89 2019-12-06 tracey case 'I':
6534 022fae89 2019-12-06 tracey no_ignores = 1;
6535 022fae89 2019-12-06 tracey break;
6536 4e68cba3 2019-11-23 stsp case 'R':
6537 4e68cba3 2019-11-23 stsp can_recurse = 1;
6538 4e68cba3 2019-11-23 stsp break;
6539 d00136be 2019-03-26 stsp default:
6540 d00136be 2019-03-26 stsp usage_add();
6541 d00136be 2019-03-26 stsp /* NOTREACHED */
6542 d00136be 2019-03-26 stsp }
6543 d00136be 2019-03-26 stsp }
6544 d00136be 2019-03-26 stsp
6545 d00136be 2019-03-26 stsp argc -= optind;
6546 d00136be 2019-03-26 stsp argv += optind;
6547 d00136be 2019-03-26 stsp
6548 43012d58 2019-07-14 stsp #ifndef PROFILE
6549 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6550 43012d58 2019-07-14 stsp NULL) == -1)
6551 43012d58 2019-07-14 stsp err(1, "pledge");
6552 43012d58 2019-07-14 stsp #endif
6553 723c305c 2019-05-11 jcs if (argc < 1)
6554 d00136be 2019-03-26 stsp usage_add();
6555 d00136be 2019-03-26 stsp
6556 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6557 d00136be 2019-03-26 stsp if (cwd == NULL) {
6558 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6559 d00136be 2019-03-26 stsp goto done;
6560 d00136be 2019-03-26 stsp }
6561 723c305c 2019-05-11 jcs
6562 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6563 fa51e947 2020-03-27 stsp if (error) {
6564 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6565 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6566 d00136be 2019-03-26 stsp goto done;
6567 fa51e947 2020-03-27 stsp }
6568 d00136be 2019-03-26 stsp
6569 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6570 c9956ddf 2019-09-08 stsp NULL);
6571 031a5338 2019-03-26 stsp if (error != NULL)
6572 031a5338 2019-03-26 stsp goto done;
6573 031a5338 2019-03-26 stsp
6574 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6575 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6576 d00136be 2019-03-26 stsp if (error)
6577 d00136be 2019-03-26 stsp goto done;
6578 d00136be 2019-03-26 stsp
6579 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6580 6d022e97 2019-08-04 stsp if (error)
6581 022fae89 2019-12-06 tracey goto done;
6582 022fae89 2019-12-06 tracey
6583 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6584 4e68cba3 2019-11-23 stsp char *ondisk_path;
6585 4e68cba3 2019-11-23 stsp struct stat sb;
6586 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6587 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6588 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6589 62d463ca 2020-10-20 naddy pe->path) == -1) {
6590 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6591 4e68cba3 2019-11-23 stsp goto done;
6592 4e68cba3 2019-11-23 stsp }
6593 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6594 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6595 4e68cba3 2019-11-23 stsp free(ondisk_path);
6596 4e68cba3 2019-11-23 stsp continue;
6597 4e68cba3 2019-11-23 stsp }
6598 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6599 4e68cba3 2019-11-23 stsp ondisk_path);
6600 4e68cba3 2019-11-23 stsp free(ondisk_path);
6601 4e68cba3 2019-11-23 stsp goto done;
6602 4e68cba3 2019-11-23 stsp }
6603 4e68cba3 2019-11-23 stsp free(ondisk_path);
6604 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6605 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6606 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6607 4e68cba3 2019-11-23 stsp goto done;
6608 4e68cba3 2019-11-23 stsp }
6609 4e68cba3 2019-11-23 stsp }
6610 4e68cba3 2019-11-23 stsp }
6611 022fae89 2019-12-06 tracey
6612 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6613 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6614 d00136be 2019-03-26 stsp done:
6615 1d0f4054 2021-06-17 stsp if (repo) {
6616 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6617 1d0f4054 2021-06-17 stsp if (error == NULL)
6618 1d0f4054 2021-06-17 stsp error = close_err;
6619 1d0f4054 2021-06-17 stsp }
6620 d00136be 2019-03-26 stsp if (worktree)
6621 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6622 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6623 1dd54920 2019-05-11 stsp free((char *)pe->path);
6624 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6625 2ec1f75b 2019-03-26 stsp free(cwd);
6626 2ec1f75b 2019-03-26 stsp return error;
6627 2ec1f75b 2019-03-26 stsp }
6628 2ec1f75b 2019-03-26 stsp
6629 2ec1f75b 2019-03-26 stsp __dead static void
6630 648e4ef7 2019-07-09 stsp usage_remove(void)
6631 2ec1f75b 2019-03-26 stsp {
6632 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6633 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6634 2ec1f75b 2019-03-26 stsp exit(1);
6635 2a06fe5f 2019-08-24 stsp }
6636 2a06fe5f 2019-08-24 stsp
6637 2a06fe5f 2019-08-24 stsp static const struct got_error *
6638 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6639 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6640 2a06fe5f 2019-08-24 stsp {
6641 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6642 f2a9dc41 2019-12-13 tracey path++;
6643 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6644 2a06fe5f 2019-08-24 stsp return NULL;
6645 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6646 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6647 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6648 2a06fe5f 2019-08-24 stsp return NULL;
6649 2ec1f75b 2019-03-26 stsp }
6650 2ec1f75b 2019-03-26 stsp
6651 2ec1f75b 2019-03-26 stsp static const struct got_error *
6652 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6653 2ec1f75b 2019-03-26 stsp {
6654 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6655 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6656 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6657 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6658 17ed4618 2019-06-02 stsp char *cwd = NULL;
6659 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6660 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6661 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6662 2ec1f75b 2019-03-26 stsp
6663 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6664 17ed4618 2019-06-02 stsp
6665 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6666 2ec1f75b 2019-03-26 stsp switch (ch) {
6667 2ec1f75b 2019-03-26 stsp case 'f':
6668 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6669 2ec1f75b 2019-03-26 stsp break;
6670 70e3e7f5 2019-12-13 tracey case 'k':
6671 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6672 70e3e7f5 2019-12-13 tracey break;
6673 f2a9dc41 2019-12-13 tracey case 'R':
6674 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6675 f2a9dc41 2019-12-13 tracey break;
6676 766841c2 2020-08-13 stsp case 's':
6677 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6678 766841c2 2020-08-13 stsp switch (optarg[i]) {
6679 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6680 766841c2 2020-08-13 stsp delete_local_mods = 1;
6681 766841c2 2020-08-13 stsp break;
6682 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6683 766841c2 2020-08-13 stsp break;
6684 766841c2 2020-08-13 stsp default:
6685 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6686 766841c2 2020-08-13 stsp optarg[i]);
6687 766841c2 2020-08-13 stsp }
6688 766841c2 2020-08-13 stsp }
6689 766841c2 2020-08-13 stsp status_codes = optarg;
6690 766841c2 2020-08-13 stsp break;
6691 2ec1f75b 2019-03-26 stsp default:
6692 f2a9dc41 2019-12-13 tracey usage_remove();
6693 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6694 2ec1f75b 2019-03-26 stsp }
6695 2ec1f75b 2019-03-26 stsp }
6696 2ec1f75b 2019-03-26 stsp
6697 2ec1f75b 2019-03-26 stsp argc -= optind;
6698 2ec1f75b 2019-03-26 stsp argv += optind;
6699 2ec1f75b 2019-03-26 stsp
6700 43012d58 2019-07-14 stsp #ifndef PROFILE
6701 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6702 43012d58 2019-07-14 stsp NULL) == -1)
6703 43012d58 2019-07-14 stsp err(1, "pledge");
6704 43012d58 2019-07-14 stsp #endif
6705 17ed4618 2019-06-02 stsp if (argc < 1)
6706 648e4ef7 2019-07-09 stsp usage_remove();
6707 2ec1f75b 2019-03-26 stsp
6708 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6709 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6710 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6711 2ec1f75b 2019-03-26 stsp goto done;
6712 2ec1f75b 2019-03-26 stsp }
6713 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6714 fa51e947 2020-03-27 stsp if (error) {
6715 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6716 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6717 2ec1f75b 2019-03-26 stsp goto done;
6718 fa51e947 2020-03-27 stsp }
6719 2ec1f75b 2019-03-26 stsp
6720 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6721 c9956ddf 2019-09-08 stsp NULL);
6722 2af4a041 2019-05-11 jcs if (error)
6723 2ec1f75b 2019-03-26 stsp goto done;
6724 2ec1f75b 2019-03-26 stsp
6725 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6726 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6727 2ec1f75b 2019-03-26 stsp if (error)
6728 2ec1f75b 2019-03-26 stsp goto done;
6729 2ec1f75b 2019-03-26 stsp
6730 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6731 6d022e97 2019-08-04 stsp if (error)
6732 6d022e97 2019-08-04 stsp goto done;
6733 17ed4618 2019-06-02 stsp
6734 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6735 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6736 f2a9dc41 2019-12-13 tracey struct stat sb;
6737 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6738 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6739 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6740 62d463ca 2020-10-20 naddy pe->path) == -1) {
6741 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6742 f2a9dc41 2019-12-13 tracey goto done;
6743 f2a9dc41 2019-12-13 tracey }
6744 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6745 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6746 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6747 f2a9dc41 2019-12-13 tracey continue;
6748 f2a9dc41 2019-12-13 tracey }
6749 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6750 f2a9dc41 2019-12-13 tracey ondisk_path);
6751 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6752 f2a9dc41 2019-12-13 tracey goto done;
6753 f2a9dc41 2019-12-13 tracey }
6754 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6755 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6756 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6757 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6758 f2a9dc41 2019-12-13 tracey goto done;
6759 f2a9dc41 2019-12-13 tracey }
6760 f2a9dc41 2019-12-13 tracey }
6761 f2a9dc41 2019-12-13 tracey }
6762 f2a9dc41 2019-12-13 tracey
6763 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6764 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6765 766841c2 2020-08-13 stsp repo, keep_on_disk);
6766 a129376b 2019-03-28 stsp done:
6767 1d0f4054 2021-06-17 stsp if (repo) {
6768 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6769 1d0f4054 2021-06-17 stsp if (error == NULL)
6770 1d0f4054 2021-06-17 stsp error = close_err;
6771 1d0f4054 2021-06-17 stsp }
6772 a129376b 2019-03-28 stsp if (worktree)
6773 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6774 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6775 17ed4618 2019-06-02 stsp free((char *)pe->path);
6776 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6777 a129376b 2019-03-28 stsp free(cwd);
6778 a129376b 2019-03-28 stsp return error;
6779 a129376b 2019-03-28 stsp }
6780 a129376b 2019-03-28 stsp
6781 a129376b 2019-03-28 stsp __dead static void
6782 a129376b 2019-03-28 stsp usage_revert(void)
6783 a129376b 2019-03-28 stsp {
6784 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6785 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6786 a129376b 2019-03-28 stsp exit(1);
6787 a129376b 2019-03-28 stsp }
6788 a129376b 2019-03-28 stsp
6789 1ee397ad 2019-07-12 stsp static const struct got_error *
6790 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6791 a129376b 2019-03-28 stsp {
6792 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6793 fb9704af 2020-01-27 stsp return NULL;
6794 fb9704af 2020-01-27 stsp
6795 a129376b 2019-03-28 stsp while (path[0] == '/')
6796 a129376b 2019-03-28 stsp path++;
6797 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6798 33aa809d 2019-08-08 stsp return NULL;
6799 33aa809d 2019-08-08 stsp }
6800 33aa809d 2019-08-08 stsp
6801 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6802 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6803 33aa809d 2019-08-08 stsp const char *action;
6804 33aa809d 2019-08-08 stsp };
6805 33aa809d 2019-08-08 stsp
6806 33aa809d 2019-08-08 stsp static const struct got_error *
6807 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6808 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6809 33aa809d 2019-08-08 stsp {
6810 33aa809d 2019-08-08 stsp char *line = NULL;
6811 33aa809d 2019-08-08 stsp size_t linesize = 0;
6812 33aa809d 2019-08-08 stsp ssize_t linelen;
6813 33aa809d 2019-08-08 stsp
6814 33aa809d 2019-08-08 stsp switch (status) {
6815 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6816 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6817 33aa809d 2019-08-08 stsp break;
6818 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6819 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6820 33aa809d 2019-08-08 stsp break;
6821 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6822 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6823 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6824 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6825 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6826 33aa809d 2019-08-08 stsp printf("%s", line);
6827 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6828 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6829 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6830 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6831 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6832 33aa809d 2019-08-08 stsp break;
6833 33aa809d 2019-08-08 stsp default:
6834 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6835 33aa809d 2019-08-08 stsp }
6836 33aa809d 2019-08-08 stsp
6837 33aa809d 2019-08-08 stsp return NULL;
6838 33aa809d 2019-08-08 stsp }
6839 33aa809d 2019-08-08 stsp
6840 33aa809d 2019-08-08 stsp static const struct got_error *
6841 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6842 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6843 33aa809d 2019-08-08 stsp {
6844 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6845 33aa809d 2019-08-08 stsp char *line = NULL;
6846 33aa809d 2019-08-08 stsp size_t linesize = 0;
6847 33aa809d 2019-08-08 stsp ssize_t linelen;
6848 33aa809d 2019-08-08 stsp int resp = ' ';
6849 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6850 33aa809d 2019-08-08 stsp
6851 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6852 33aa809d 2019-08-08 stsp
6853 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6854 33aa809d 2019-08-08 stsp char *nl;
6855 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6856 33aa809d 2019-08-08 stsp a->action);
6857 33aa809d 2019-08-08 stsp if (err)
6858 33aa809d 2019-08-08 stsp return err;
6859 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6860 33aa809d 2019-08-08 stsp if (linelen == -1) {
6861 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6862 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6863 33aa809d 2019-08-08 stsp return NULL;
6864 33aa809d 2019-08-08 stsp }
6865 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6866 33aa809d 2019-08-08 stsp if (nl)
6867 33aa809d 2019-08-08 stsp *nl = '\0';
6868 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6869 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6870 33aa809d 2019-08-08 stsp printf("y\n");
6871 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6872 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6873 33aa809d 2019-08-08 stsp printf("n\n");
6874 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6875 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6876 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6877 33aa809d 2019-08-08 stsp printf("q\n");
6878 33aa809d 2019-08-08 stsp } else
6879 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6880 33aa809d 2019-08-08 stsp free(line);
6881 33aa809d 2019-08-08 stsp return NULL;
6882 33aa809d 2019-08-08 stsp }
6883 33aa809d 2019-08-08 stsp
6884 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6885 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6886 33aa809d 2019-08-08 stsp a->action);
6887 33aa809d 2019-08-08 stsp if (err)
6888 33aa809d 2019-08-08 stsp return err;
6889 33aa809d 2019-08-08 stsp resp = getchar();
6890 33aa809d 2019-08-08 stsp if (resp == '\n')
6891 33aa809d 2019-08-08 stsp resp = getchar();
6892 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6893 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6894 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6895 33aa809d 2019-08-08 stsp resp = ' ';
6896 33aa809d 2019-08-08 stsp }
6897 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6898 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6899 33aa809d 2019-08-08 stsp resp = ' ';
6900 33aa809d 2019-08-08 stsp }
6901 33aa809d 2019-08-08 stsp }
6902 33aa809d 2019-08-08 stsp
6903 33aa809d 2019-08-08 stsp if (resp == 'y')
6904 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6905 33aa809d 2019-08-08 stsp else if (resp == 'n')
6906 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6907 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6908 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6909 33aa809d 2019-08-08 stsp
6910 1ee397ad 2019-07-12 stsp return NULL;
6911 a129376b 2019-03-28 stsp }
6912 a129376b 2019-03-28 stsp
6913 33aa809d 2019-08-08 stsp
6914 a129376b 2019-03-28 stsp static const struct got_error *
6915 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6916 a129376b 2019-03-28 stsp {
6917 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6918 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6919 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6920 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6921 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6922 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6923 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6924 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6925 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6926 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6927 a129376b 2019-03-28 stsp
6928 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6929 e20a8b6f 2019-06-04 stsp
6930 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6931 a129376b 2019-03-28 stsp switch (ch) {
6932 33aa809d 2019-08-08 stsp case 'p':
6933 33aa809d 2019-08-08 stsp pflag = 1;
6934 33aa809d 2019-08-08 stsp break;
6935 33aa809d 2019-08-08 stsp case 'F':
6936 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6937 33aa809d 2019-08-08 stsp break;
6938 0f6d7415 2019-08-08 stsp case 'R':
6939 0f6d7415 2019-08-08 stsp can_recurse = 1;
6940 0f6d7415 2019-08-08 stsp break;
6941 a129376b 2019-03-28 stsp default:
6942 a129376b 2019-03-28 stsp usage_revert();
6943 a129376b 2019-03-28 stsp /* NOTREACHED */
6944 a129376b 2019-03-28 stsp }
6945 a129376b 2019-03-28 stsp }
6946 a129376b 2019-03-28 stsp
6947 a129376b 2019-03-28 stsp argc -= optind;
6948 a129376b 2019-03-28 stsp argv += optind;
6949 a129376b 2019-03-28 stsp
6950 43012d58 2019-07-14 stsp #ifndef PROFILE
6951 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6952 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6953 43012d58 2019-07-14 stsp err(1, "pledge");
6954 43012d58 2019-07-14 stsp #endif
6955 e20a8b6f 2019-06-04 stsp if (argc < 1)
6956 a129376b 2019-03-28 stsp usage_revert();
6957 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6958 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6959 a129376b 2019-03-28 stsp
6960 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6961 a129376b 2019-03-28 stsp if (cwd == NULL) {
6962 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6963 a129376b 2019-03-28 stsp goto done;
6964 a129376b 2019-03-28 stsp }
6965 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6966 fa51e947 2020-03-27 stsp if (error) {
6967 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6968 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6969 2ec1f75b 2019-03-26 stsp goto done;
6970 fa51e947 2020-03-27 stsp }
6971 a129376b 2019-03-28 stsp
6972 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6973 c9956ddf 2019-09-08 stsp NULL);
6974 a129376b 2019-03-28 stsp if (error != NULL)
6975 a129376b 2019-03-28 stsp goto done;
6976 a129376b 2019-03-28 stsp
6977 33aa809d 2019-08-08 stsp if (patch_script_path) {
6978 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6979 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
6980 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
6981 33aa809d 2019-08-08 stsp patch_script_path);
6982 33aa809d 2019-08-08 stsp goto done;
6983 33aa809d 2019-08-08 stsp }
6984 33aa809d 2019-08-08 stsp }
6985 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6986 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6987 a129376b 2019-03-28 stsp if (error)
6988 a129376b 2019-03-28 stsp goto done;
6989 a129376b 2019-03-28 stsp
6990 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6991 6d022e97 2019-08-04 stsp if (error)
6992 6d022e97 2019-08-04 stsp goto done;
6993 00db391e 2019-08-03 stsp
6994 0f6d7415 2019-08-08 stsp if (!can_recurse) {
6995 0f6d7415 2019-08-08 stsp char *ondisk_path;
6996 0f6d7415 2019-08-08 stsp struct stat sb;
6997 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
6998 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
6999 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
7000 62d463ca 2020-10-20 naddy pe->path) == -1) {
7001 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
7002 0f6d7415 2019-08-08 stsp goto done;
7003 0f6d7415 2019-08-08 stsp }
7004 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
7005 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
7006 0f6d7415 2019-08-08 stsp free(ondisk_path);
7007 0f6d7415 2019-08-08 stsp continue;
7008 0f6d7415 2019-08-08 stsp }
7009 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
7010 0f6d7415 2019-08-08 stsp ondisk_path);
7011 0f6d7415 2019-08-08 stsp free(ondisk_path);
7012 0f6d7415 2019-08-08 stsp goto done;
7013 0f6d7415 2019-08-08 stsp }
7014 0f6d7415 2019-08-08 stsp free(ondisk_path);
7015 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
7016 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
7017 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
7018 0f6d7415 2019-08-08 stsp goto done;
7019 0f6d7415 2019-08-08 stsp }
7020 0f6d7415 2019-08-08 stsp }
7021 0f6d7415 2019-08-08 stsp }
7022 0f6d7415 2019-08-08 stsp
7023 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7024 33aa809d 2019-08-08 stsp cpa.action = "revert";
7025 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7026 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7027 2ec1f75b 2019-03-26 stsp done:
7028 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7029 33aa809d 2019-08-08 stsp error == NULL)
7030 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7031 1d0f4054 2021-06-17 stsp if (repo) {
7032 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7033 1d0f4054 2021-06-17 stsp if (error == NULL)
7034 1d0f4054 2021-06-17 stsp error = close_err;
7035 1d0f4054 2021-06-17 stsp }
7036 2ec1f75b 2019-03-26 stsp if (worktree)
7037 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
7038 2ec1f75b 2019-03-26 stsp free(path);
7039 d00136be 2019-03-26 stsp free(cwd);
7040 6bad629b 2019-02-04 stsp return error;
7041 c4296144 2019-05-09 stsp }
7042 c4296144 2019-05-09 stsp
7043 c4296144 2019-05-09 stsp __dead static void
7044 c4296144 2019-05-09 stsp usage_commit(void)
7045 c4296144 2019-05-09 stsp {
7046 28cf319f 2021-01-28 stsp fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
7047 28cf319f 2021-01-28 stsp "[path ...]\n", getprogname());
7048 c4296144 2019-05-09 stsp exit(1);
7049 33ad4cbe 2019-05-12 jcs }
7050 33ad4cbe 2019-05-12 jcs
7051 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
7052 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
7053 28cf319f 2021-01-28 stsp const char *prepared_log;
7054 28cf319f 2021-01-28 stsp int non_interactive;
7055 e2ba3d07 2019-05-13 stsp const char *editor;
7056 e0870e44 2019-05-13 stsp const char *worktree_path;
7057 76d98825 2019-06-03 stsp const char *branch_name;
7058 314a6357 2019-05-13 stsp const char *repo_path;
7059 e0870e44 2019-05-13 stsp char *logmsg_path;
7060 e2ba3d07 2019-05-13 stsp
7061 e2ba3d07 2019-05-13 stsp };
7062 28cf319f 2021-01-28 stsp
7063 28cf319f 2021-01-28 stsp static const struct got_error *
7064 28cf319f 2021-01-28 stsp read_prepared_logmsg(char **logmsg, const char *path)
7065 28cf319f 2021-01-28 stsp {
7066 28cf319f 2021-01-28 stsp const struct got_error *err = NULL;
7067 28cf319f 2021-01-28 stsp FILE *f = NULL;
7068 28cf319f 2021-01-28 stsp struct stat sb;
7069 28cf319f 2021-01-28 stsp size_t r;
7070 28cf319f 2021-01-28 stsp
7071 28cf319f 2021-01-28 stsp *logmsg = NULL;
7072 28cf319f 2021-01-28 stsp memset(&sb, 0, sizeof(sb));
7073 28cf319f 2021-01-28 stsp
7074 28cf319f 2021-01-28 stsp f = fopen(path, "r");
7075 28cf319f 2021-01-28 stsp if (f == NULL)
7076 28cf319f 2021-01-28 stsp return got_error_from_errno2("fopen", path);
7077 28cf319f 2021-01-28 stsp
7078 28cf319f 2021-01-28 stsp if (fstat(fileno(f), &sb) == -1) {
7079 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fstat", path);
7080 28cf319f 2021-01-28 stsp goto done;
7081 28cf319f 2021-01-28 stsp }
7082 28cf319f 2021-01-28 stsp if (sb.st_size == 0) {
7083 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7084 28cf319f 2021-01-28 stsp goto done;
7085 28cf319f 2021-01-28 stsp }
7086 28cf319f 2021-01-28 stsp
7087 28cf319f 2021-01-28 stsp *logmsg = malloc(sb.st_size + 1);
7088 28cf319f 2021-01-28 stsp if (*logmsg == NULL) {
7089 28cf319f 2021-01-28 stsp err = got_error_from_errno("malloc");
7090 28cf319f 2021-01-28 stsp goto done;
7091 28cf319f 2021-01-28 stsp }
7092 28cf319f 2021-01-28 stsp
7093 28cf319f 2021-01-28 stsp r = fread(*logmsg, 1, sb.st_size, f);
7094 28cf319f 2021-01-28 stsp if (r != sb.st_size) {
7095 28cf319f 2021-01-28 stsp if (ferror(f))
7096 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fread", path);
7097 28cf319f 2021-01-28 stsp else
7098 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_IO);
7099 28cf319f 2021-01-28 stsp goto done;
7100 28cf319f 2021-01-28 stsp }
7101 28cf319f 2021-01-28 stsp (*logmsg)[sb.st_size] = '\0';
7102 28cf319f 2021-01-28 stsp done:
7103 28cf319f 2021-01-28 stsp if (fclose(f) == EOF && err == NULL)
7104 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fclose", path);
7105 28cf319f 2021-01-28 stsp if (err) {
7106 28cf319f 2021-01-28 stsp free(*logmsg);
7107 28cf319f 2021-01-28 stsp *logmsg = NULL;
7108 28cf319f 2021-01-28 stsp }
7109 28cf319f 2021-01-28 stsp return err;
7110 28cf319f 2021-01-28 stsp
7111 28cf319f 2021-01-28 stsp }
7112 e0870e44 2019-05-13 stsp
7113 33ad4cbe 2019-05-12 jcs static const struct got_error *
7114 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
7115 33ad4cbe 2019-05-12 jcs void *arg)
7116 33ad4cbe 2019-05-12 jcs {
7117 76d98825 2019-06-03 stsp char *initial_content = NULL;
7118 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
7119 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
7120 e0870e44 2019-05-13 stsp char *template = NULL;
7121 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
7122 1601cb9f 2020-09-11 naddy int initial_content_len;
7123 97972933 2020-09-11 stsp int fd = -1;
7124 33ad4cbe 2019-05-12 jcs size_t len;
7125 33ad4cbe 2019-05-12 jcs
7126 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
7127 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
7128 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
7129 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
7130 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
7131 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
7132 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
7133 33ad4cbe 2019-05-12 jcs return NULL;
7134 28cf319f 2021-01-28 stsp } else if (a->prepared_log != NULL && a->non_interactive)
7135 28cf319f 2021-01-28 stsp return read_prepared_logmsg(logmsg, a->prepared_log);
7136 33ad4cbe 2019-05-12 jcs
7137 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7138 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
7139 76d98825 2019-06-03 stsp
7140 28cf319f 2021-01-28 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7141 28cf319f 2021-01-28 stsp if (err)
7142 28cf319f 2021-01-28 stsp goto done;
7143 28cf319f 2021-01-28 stsp
7144 28cf319f 2021-01-28 stsp if (a->prepared_log) {
7145 28cf319f 2021-01-28 stsp char *msg;
7146 28cf319f 2021-01-28 stsp err = read_prepared_logmsg(&msg, a->prepared_log);
7147 28cf319f 2021-01-28 stsp if (err)
7148 28cf319f 2021-01-28 stsp goto done;
7149 28cf319f 2021-01-28 stsp if (write(fd, msg, strlen(msg)) == -1) {
7150 28cf319f 2021-01-28 stsp err = got_error_from_errno2("write", a->logmsg_path);
7151 28cf319f 2021-01-28 stsp free(msg);
7152 28cf319f 2021-01-28 stsp goto done;
7153 28cf319f 2021-01-28 stsp }
7154 28cf319f 2021-01-28 stsp free(msg);
7155 28cf319f 2021-01-28 stsp }
7156 28cf319f 2021-01-28 stsp
7157 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
7158 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
7159 1601cb9f 2020-09-11 naddy a->branch_name);
7160 28cf319f 2021-01-28 stsp if (initial_content_len == -1) {
7161 28cf319f 2021-01-28 stsp err = got_error_from_errno("asprintf");
7162 e0870e44 2019-05-13 stsp goto done;
7163 28cf319f 2021-01-28 stsp }
7164 33ad4cbe 2019-05-12 jcs
7165 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
7166 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
7167 97972933 2020-09-11 stsp goto done;
7168 97972933 2020-09-11 stsp }
7169 33ad4cbe 2019-05-12 jcs
7170 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
7171 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
7172 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
7173 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
7174 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
7175 33ad4cbe 2019-05-12 jcs }
7176 33ad4cbe 2019-05-12 jcs
7177 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7178 28cf319f 2021-01-28 stsp initial_content_len, a->prepared_log ? 0 : 1);
7179 33ad4cbe 2019-05-12 jcs done:
7180 76d98825 2019-06-03 stsp free(initial_content);
7181 e0870e44 2019-05-13 stsp free(template);
7182 314a6357 2019-05-13 stsp
7183 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
7184 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
7185 97972933 2020-09-11 stsp
7186 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
7187 59f86c76 2020-09-11 stsp if (err == NULL)
7188 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
7189 59f86c76 2020-09-11 stsp if (err) {
7190 59f86c76 2020-09-11 stsp free(*logmsg);
7191 59f86c76 2020-09-11 stsp *logmsg = NULL;
7192 3ce1b845 2019-07-15 stsp }
7193 33ad4cbe 2019-05-12 jcs return err;
7194 6bad629b 2019-02-04 stsp }
7195 c4296144 2019-05-09 stsp
7196 c4296144 2019-05-09 stsp static const struct got_error *
7197 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
7198 c4296144 2019-05-09 stsp {
7199 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
7200 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
7201 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
7202 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
7203 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
7204 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
7205 28cf319f 2021-01-28 stsp char *prepared_logmsg = NULL;
7206 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
7207 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7208 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7209 28cf319f 2021-01-28 stsp int allow_bad_symlinks = 0, non_interactive = 0;
7210 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
7211 5c1e53bc 2019-07-28 stsp
7212 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
7213 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
7214 c4296144 2019-05-09 stsp
7215 28cf319f 2021-01-28 stsp while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7216 c4296144 2019-05-09 stsp switch (ch) {
7217 28cf319f 2021-01-28 stsp case 'F':
7218 28cf319f 2021-01-28 stsp if (logmsg != NULL)
7219 28cf319f 2021-01-28 stsp option_conflict('F', 'm');
7220 28cf319f 2021-01-28 stsp prepared_logmsg = realpath(optarg, NULL);
7221 28cf319f 2021-01-28 stsp if (prepared_logmsg == NULL)
7222 28cf319f 2021-01-28 stsp return got_error_from_errno2("realpath",
7223 28cf319f 2021-01-28 stsp optarg);
7224 28cf319f 2021-01-28 stsp break;
7225 c4296144 2019-05-09 stsp case 'm':
7226 28cf319f 2021-01-28 stsp if (prepared_logmsg)
7227 28cf319f 2021-01-28 stsp option_conflict('m', 'F');
7228 c4296144 2019-05-09 stsp logmsg = optarg;
7229 28cf319f 2021-01-28 stsp break;
7230 28cf319f 2021-01-28 stsp case 'N':
7231 28cf319f 2021-01-28 stsp non_interactive = 1;
7232 c4296144 2019-05-09 stsp break;
7233 35213c7c 2020-07-23 stsp case 'S':
7234 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
7235 35213c7c 2020-07-23 stsp break;
7236 c4296144 2019-05-09 stsp default:
7237 c4296144 2019-05-09 stsp usage_commit();
7238 c4296144 2019-05-09 stsp /* NOTREACHED */
7239 c4296144 2019-05-09 stsp }
7240 c4296144 2019-05-09 stsp }
7241 c4296144 2019-05-09 stsp
7242 c4296144 2019-05-09 stsp argc -= optind;
7243 c4296144 2019-05-09 stsp argv += optind;
7244 c4296144 2019-05-09 stsp
7245 43012d58 2019-07-14 stsp #ifndef PROFILE
7246 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7247 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7248 43012d58 2019-07-14 stsp err(1, "pledge");
7249 43012d58 2019-07-14 stsp #endif
7250 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
7251 c4296144 2019-05-09 stsp if (cwd == NULL) {
7252 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7253 c4296144 2019-05-09 stsp goto done;
7254 c4296144 2019-05-09 stsp }
7255 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
7256 fa51e947 2020-03-27 stsp if (error) {
7257 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7258 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
7259 7d5807f4 2019-07-11 stsp goto done;
7260 fa51e947 2020-03-27 stsp }
7261 7d5807f4 2019-07-11 stsp
7262 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7263 c4296144 2019-05-09 stsp if (error)
7264 a698f62e 2019-07-25 stsp goto done;
7265 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
7266 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
7267 c4296144 2019-05-09 stsp goto done;
7268 a698f62e 2019-07-25 stsp }
7269 5c1e53bc 2019-07-28 stsp
7270 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7271 916f288c 2019-07-30 stsp worktree);
7272 5c1e53bc 2019-07-28 stsp if (error)
7273 5c1e53bc 2019-07-28 stsp goto done;
7274 c4296144 2019-05-09 stsp
7275 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
7276 c9956ddf 2019-09-08 stsp if (error)
7277 c9956ddf 2019-09-08 stsp goto done;
7278 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7279 c9956ddf 2019-09-08 stsp gitconfig_path);
7280 c4296144 2019-05-09 stsp if (error != NULL)
7281 c4296144 2019-05-09 stsp goto done;
7282 c4296144 2019-05-09 stsp
7283 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
7284 aba9c984 2019-09-08 stsp if (error)
7285 aba9c984 2019-09-08 stsp return error;
7286 aba9c984 2019-09-08 stsp
7287 314a6357 2019-05-13 stsp /*
7288 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
7289 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
7290 314a6357 2019-05-13 stsp */
7291 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
7292 314a6357 2019-05-13 stsp error = get_editor(&editor);
7293 314a6357 2019-05-13 stsp else
7294 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7295 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7296 c4296144 2019-05-09 stsp if (error)
7297 c4296144 2019-05-09 stsp goto done;
7298 c4296144 2019-05-09 stsp
7299 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7300 087fb88c 2019-08-04 stsp if (error)
7301 087fb88c 2019-08-04 stsp goto done;
7302 087fb88c 2019-08-04 stsp
7303 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
7304 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
7305 28cf319f 2021-01-28 stsp cl_arg.prepared_log = prepared_logmsg;
7306 28cf319f 2021-01-28 stsp cl_arg.non_interactive = non_interactive;
7307 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7308 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7309 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
7310 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7311 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
7312 916f288c 2019-07-30 stsp goto done;
7313 916f288c 2019-07-30 stsp }
7314 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
7315 916f288c 2019-07-30 stsp }
7316 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
7317 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7318 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7319 35213c7c 2020-07-23 stsp print_status, NULL, repo);
7320 e0870e44 2019-05-13 stsp if (error) {
7321 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7322 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
7323 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
7324 c4296144 2019-05-09 stsp goto done;
7325 e0870e44 2019-05-13 stsp }
7326 c4296144 2019-05-09 stsp
7327 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
7328 c4296144 2019-05-09 stsp if (error)
7329 c4296144 2019-05-09 stsp goto done;
7330 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
7331 c4296144 2019-05-09 stsp done:
7332 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
7333 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
7334 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
7335 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7336 7266f21f 2019-10-21 stsp error == NULL)
7337 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7338 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
7339 1d0f4054 2021-06-17 stsp if (repo) {
7340 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7341 1d0f4054 2021-06-17 stsp if (error == NULL)
7342 1d0f4054 2021-06-17 stsp error = close_err;
7343 1d0f4054 2021-06-17 stsp }
7344 c4296144 2019-05-09 stsp if (worktree)
7345 c4296144 2019-05-09 stsp got_worktree_close(worktree);
7346 c4296144 2019-05-09 stsp free(cwd);
7347 c4296144 2019-05-09 stsp free(id_str);
7348 c9956ddf 2019-09-08 stsp free(gitconfig_path);
7349 e2ba3d07 2019-05-13 stsp free(editor);
7350 aba9c984 2019-09-08 stsp free(author);
7351 28cf319f 2021-01-28 stsp free(prepared_logmsg);
7352 234035bc 2019-06-01 stsp return error;
7353 234035bc 2019-06-01 stsp }
7354 234035bc 2019-06-01 stsp
7355 234035bc 2019-06-01 stsp __dead static void
7356 234035bc 2019-06-01 stsp usage_cherrypick(void)
7357 234035bc 2019-06-01 stsp {
7358 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
7359 234035bc 2019-06-01 stsp exit(1);
7360 234035bc 2019-06-01 stsp }
7361 234035bc 2019-06-01 stsp
7362 234035bc 2019-06-01 stsp static const struct got_error *
7363 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
7364 234035bc 2019-06-01 stsp {
7365 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
7366 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
7367 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
7368 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
7369 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
7370 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
7371 234035bc 2019-06-01 stsp struct got_object_qid *pid;
7372 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
7373 9627c110 2020-04-18 stsp int ch;
7374 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7375 234035bc 2019-06-01 stsp
7376 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7377 234035bc 2019-06-01 stsp switch (ch) {
7378 234035bc 2019-06-01 stsp default:
7379 234035bc 2019-06-01 stsp usage_cherrypick();
7380 234035bc 2019-06-01 stsp /* NOTREACHED */
7381 234035bc 2019-06-01 stsp }
7382 234035bc 2019-06-01 stsp }
7383 234035bc 2019-06-01 stsp
7384 234035bc 2019-06-01 stsp argc -= optind;
7385 234035bc 2019-06-01 stsp argv += optind;
7386 234035bc 2019-06-01 stsp
7387 43012d58 2019-07-14 stsp #ifndef PROFILE
7388 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7389 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7390 43012d58 2019-07-14 stsp err(1, "pledge");
7391 43012d58 2019-07-14 stsp #endif
7392 234035bc 2019-06-01 stsp if (argc != 1)
7393 234035bc 2019-06-01 stsp usage_cherrypick();
7394 234035bc 2019-06-01 stsp
7395 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
7396 234035bc 2019-06-01 stsp if (cwd == NULL) {
7397 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
7398 234035bc 2019-06-01 stsp goto done;
7399 234035bc 2019-06-01 stsp }
7400 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
7401 fa51e947 2020-03-27 stsp if (error) {
7402 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7403 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
7404 fa51e947 2020-03-27 stsp cwd);
7405 234035bc 2019-06-01 stsp goto done;
7406 fa51e947 2020-03-27 stsp }
7407 234035bc 2019-06-01 stsp
7408 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7409 c9956ddf 2019-09-08 stsp NULL);
7410 234035bc 2019-06-01 stsp if (error != NULL)
7411 234035bc 2019-06-01 stsp goto done;
7412 234035bc 2019-06-01 stsp
7413 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7414 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7415 234035bc 2019-06-01 stsp if (error)
7416 234035bc 2019-06-01 stsp goto done;
7417 234035bc 2019-06-01 stsp
7418 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7419 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7420 234035bc 2019-06-01 stsp if (error != NULL) {
7421 234035bc 2019-06-01 stsp struct got_reference *ref;
7422 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7423 234035bc 2019-06-01 stsp goto done;
7424 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7425 234035bc 2019-06-01 stsp if (error != NULL)
7426 234035bc 2019-06-01 stsp goto done;
7427 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
7428 234035bc 2019-06-01 stsp got_ref_close(ref);
7429 234035bc 2019-06-01 stsp if (error != NULL)
7430 234035bc 2019-06-01 stsp goto done;
7431 234035bc 2019-06-01 stsp }
7432 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
7433 234035bc 2019-06-01 stsp if (error)
7434 234035bc 2019-06-01 stsp goto done;
7435 234035bc 2019-06-01 stsp
7436 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
7437 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
7438 234035bc 2019-06-01 stsp if (error != NULL)
7439 234035bc 2019-06-01 stsp goto done;
7440 234035bc 2019-06-01 stsp
7441 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7442 234035bc 2019-06-01 stsp if (error) {
7443 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
7444 234035bc 2019-06-01 stsp goto done;
7445 234035bc 2019-06-01 stsp error = NULL;
7446 234035bc 2019-06-01 stsp } else {
7447 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
7448 234035bc 2019-06-01 stsp goto done;
7449 234035bc 2019-06-01 stsp }
7450 234035bc 2019-06-01 stsp
7451 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7452 234035bc 2019-06-01 stsp if (error)
7453 234035bc 2019-06-01 stsp goto done;
7454 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7455 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7456 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7457 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
7458 03415a1a 2019-06-02 stsp NULL);
7459 234035bc 2019-06-01 stsp if (error != NULL)
7460 234035bc 2019-06-01 stsp goto done;
7461 234035bc 2019-06-01 stsp
7462 9627c110 2020-04-18 stsp if (upa.did_something)
7463 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
7464 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7465 234035bc 2019-06-01 stsp done:
7466 234035bc 2019-06-01 stsp if (commit)
7467 234035bc 2019-06-01 stsp got_object_commit_close(commit);
7468 234035bc 2019-06-01 stsp free(commit_id_str);
7469 234035bc 2019-06-01 stsp if (head_ref)
7470 234035bc 2019-06-01 stsp got_ref_close(head_ref);
7471 234035bc 2019-06-01 stsp if (worktree)
7472 234035bc 2019-06-01 stsp got_worktree_close(worktree);
7473 1d0f4054 2021-06-17 stsp if (repo) {
7474 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7475 1d0f4054 2021-06-17 stsp if (error == NULL)
7476 1d0f4054 2021-06-17 stsp error = close_err;
7477 1d0f4054 2021-06-17 stsp }
7478 c4296144 2019-05-09 stsp return error;
7479 c4296144 2019-05-09 stsp }
7480 5ef14e63 2019-06-02 stsp
7481 5ef14e63 2019-06-02 stsp __dead static void
7482 5ef14e63 2019-06-02 stsp usage_backout(void)
7483 5ef14e63 2019-06-02 stsp {
7484 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7485 5ef14e63 2019-06-02 stsp exit(1);
7486 5ef14e63 2019-06-02 stsp }
7487 5ef14e63 2019-06-02 stsp
7488 5ef14e63 2019-06-02 stsp static const struct got_error *
7489 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
7490 5ef14e63 2019-06-02 stsp {
7491 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
7492 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
7493 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
7494 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
7495 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
7496 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
7497 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
7498 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
7499 9627c110 2020-04-18 stsp int ch;
7500 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7501 5ef14e63 2019-06-02 stsp
7502 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7503 5ef14e63 2019-06-02 stsp switch (ch) {
7504 5ef14e63 2019-06-02 stsp default:
7505 5ef14e63 2019-06-02 stsp usage_backout();
7506 5ef14e63 2019-06-02 stsp /* NOTREACHED */
7507 5ef14e63 2019-06-02 stsp }
7508 5ef14e63 2019-06-02 stsp }
7509 5ef14e63 2019-06-02 stsp
7510 5ef14e63 2019-06-02 stsp argc -= optind;
7511 5ef14e63 2019-06-02 stsp argv += optind;
7512 5ef14e63 2019-06-02 stsp
7513 43012d58 2019-07-14 stsp #ifndef PROFILE
7514 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7515 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7516 43012d58 2019-07-14 stsp err(1, "pledge");
7517 43012d58 2019-07-14 stsp #endif
7518 5ef14e63 2019-06-02 stsp if (argc != 1)
7519 5ef14e63 2019-06-02 stsp usage_backout();
7520 5ef14e63 2019-06-02 stsp
7521 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
7522 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
7523 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
7524 5ef14e63 2019-06-02 stsp goto done;
7525 5ef14e63 2019-06-02 stsp }
7526 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
7527 fa51e947 2020-03-27 stsp if (error) {
7528 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7529 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
7530 5ef14e63 2019-06-02 stsp goto done;
7531 fa51e947 2020-03-27 stsp }
7532 5ef14e63 2019-06-02 stsp
7533 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7534 c9956ddf 2019-09-08 stsp NULL);
7535 5ef14e63 2019-06-02 stsp if (error != NULL)
7536 5ef14e63 2019-06-02 stsp goto done;
7537 5ef14e63 2019-06-02 stsp
7538 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7539 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7540 5ef14e63 2019-06-02 stsp if (error)
7541 5ef14e63 2019-06-02 stsp goto done;
7542 5ef14e63 2019-06-02 stsp
7543 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7544 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7545 5ef14e63 2019-06-02 stsp if (error != NULL) {
7546 5ef14e63 2019-06-02 stsp struct got_reference *ref;
7547 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7548 5ef14e63 2019-06-02 stsp goto done;
7549 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7550 5ef14e63 2019-06-02 stsp if (error != NULL)
7551 5ef14e63 2019-06-02 stsp goto done;
7552 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
7553 5ef14e63 2019-06-02 stsp got_ref_close(ref);
7554 5ef14e63 2019-06-02 stsp if (error != NULL)
7555 5ef14e63 2019-06-02 stsp goto done;
7556 5ef14e63 2019-06-02 stsp }
7557 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
7558 5ef14e63 2019-06-02 stsp if (error)
7559 5ef14e63 2019-06-02 stsp goto done;
7560 5ef14e63 2019-06-02 stsp
7561 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
7562 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
7563 5ef14e63 2019-06-02 stsp if (error != NULL)
7564 5ef14e63 2019-06-02 stsp goto done;
7565 5ef14e63 2019-06-02 stsp
7566 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7567 5ef14e63 2019-06-02 stsp if (error)
7568 5ef14e63 2019-06-02 stsp goto done;
7569 5ef14e63 2019-06-02 stsp
7570 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7571 5ef14e63 2019-06-02 stsp if (error)
7572 5ef14e63 2019-06-02 stsp goto done;
7573 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7574 5ef14e63 2019-06-02 stsp if (pid == NULL) {
7575 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
7576 5ef14e63 2019-06-02 stsp goto done;
7577 5ef14e63 2019-06-02 stsp }
7578 5ef14e63 2019-06-02 stsp
7579 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7580 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
7581 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7582 5ef14e63 2019-06-02 stsp if (error != NULL)
7583 5ef14e63 2019-06-02 stsp goto done;
7584 5ef14e63 2019-06-02 stsp
7585 9627c110 2020-04-18 stsp if (upa.did_something)
7586 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
7587 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7588 5ef14e63 2019-06-02 stsp done:
7589 5ef14e63 2019-06-02 stsp if (commit)
7590 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
7591 5ef14e63 2019-06-02 stsp free(commit_id_str);
7592 5ef14e63 2019-06-02 stsp if (head_ref)
7593 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
7594 818c7501 2019-07-11 stsp if (worktree)
7595 818c7501 2019-07-11 stsp got_worktree_close(worktree);
7596 1d0f4054 2021-06-17 stsp if (repo) {
7597 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7598 1d0f4054 2021-06-17 stsp if (error == NULL)
7599 1d0f4054 2021-06-17 stsp error = close_err;
7600 1d0f4054 2021-06-17 stsp }
7601 818c7501 2019-07-11 stsp return error;
7602 818c7501 2019-07-11 stsp }
7603 818c7501 2019-07-11 stsp
7604 818c7501 2019-07-11 stsp __dead static void
7605 818c7501 2019-07-11 stsp usage_rebase(void)
7606 818c7501 2019-07-11 stsp {
7607 643b85bc 2021-07-16 stsp fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
7608 af54c8f8 2019-07-11 stsp getprogname());
7609 818c7501 2019-07-11 stsp exit(1);
7610 0ebf8283 2019-07-24 stsp }
7611 0ebf8283 2019-07-24 stsp
7612 0ebf8283 2019-07-24 stsp void
7613 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
7614 0ebf8283 2019-07-24 stsp {
7615 0ebf8283 2019-07-24 stsp char *nl;
7616 0ebf8283 2019-07-24 stsp size_t len;
7617 0ebf8283 2019-07-24 stsp
7618 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
7619 0ebf8283 2019-07-24 stsp if (len > limit)
7620 0ebf8283 2019-07-24 stsp len = limit;
7621 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
7622 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
7623 0ebf8283 2019-07-24 stsp if (nl)
7624 0ebf8283 2019-07-24 stsp *nl = '\0';
7625 0ebf8283 2019-07-24 stsp }
7626 0ebf8283 2019-07-24 stsp
7627 0ebf8283 2019-07-24 stsp static const struct got_error *
7628 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7629 0ebf8283 2019-07-24 stsp {
7630 5943eee2 2019-08-13 stsp const struct got_error *err;
7631 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
7632 5943eee2 2019-08-13 stsp const char *s;
7633 0ebf8283 2019-07-24 stsp
7634 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7635 5943eee2 2019-08-13 stsp if (err)
7636 5943eee2 2019-08-13 stsp return err;
7637 0ebf8283 2019-07-24 stsp
7638 5943eee2 2019-08-13 stsp s = logmsg0;
7639 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
7640 5943eee2 2019-08-13 stsp s++;
7641 5943eee2 2019-08-13 stsp
7642 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
7643 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
7644 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7645 5943eee2 2019-08-13 stsp goto done;
7646 5943eee2 2019-08-13 stsp }
7647 0ebf8283 2019-07-24 stsp
7648 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
7649 5943eee2 2019-08-13 stsp done:
7650 5943eee2 2019-08-13 stsp free(logmsg0);
7651 a0ea4fc0 2020-02-28 stsp return err;
7652 a0ea4fc0 2020-02-28 stsp }
7653 a0ea4fc0 2020-02-28 stsp
7654 a0ea4fc0 2020-02-28 stsp static const struct got_error *
7655 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7656 a0ea4fc0 2020-02-28 stsp {
7657 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
7658 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
7659 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
7660 a0ea4fc0 2020-02-28 stsp
7661 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
7662 a0ea4fc0 2020-02-28 stsp if (err)
7663 a0ea4fc0 2020-02-28 stsp return err;
7664 a0ea4fc0 2020-02-28 stsp
7665 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
7666 a0ea4fc0 2020-02-28 stsp if (err)
7667 a0ea4fc0 2020-02-28 stsp goto done;
7668 a0ea4fc0 2020-02-28 stsp
7669 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
7670 a0ea4fc0 2020-02-28 stsp
7671 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
7672 a0ea4fc0 2020-02-28 stsp if (err)
7673 a0ea4fc0 2020-02-28 stsp goto done;
7674 a0ea4fc0 2020-02-28 stsp
7675 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
7676 a0ea4fc0 2020-02-28 stsp done:
7677 a0ea4fc0 2020-02-28 stsp free(id_str);
7678 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
7679 a0ea4fc0 2020-02-28 stsp free(logmsg);
7680 5943eee2 2019-08-13 stsp return err;
7681 818c7501 2019-07-11 stsp }
7682 818c7501 2019-07-11 stsp
7683 818c7501 2019-07-11 stsp static const struct got_error *
7684 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
7685 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
7686 818c7501 2019-07-11 stsp {
7687 818c7501 2019-07-11 stsp const struct got_error *err;
7688 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7689 818c7501 2019-07-11 stsp
7690 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
7691 818c7501 2019-07-11 stsp if (err)
7692 818c7501 2019-07-11 stsp goto done;
7693 818c7501 2019-07-11 stsp
7694 ff0d2220 2019-07-11 stsp if (new_id) {
7695 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
7696 ff0d2220 2019-07-11 stsp if (err)
7697 ff0d2220 2019-07-11 stsp goto done;
7698 ff0d2220 2019-07-11 stsp }
7699 818c7501 2019-07-11 stsp
7700 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
7701 ff0d2220 2019-07-11 stsp if (new_id_str)
7702 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
7703 0ebf8283 2019-07-24 stsp
7704 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7705 0ebf8283 2019-07-24 stsp if (err)
7706 0ebf8283 2019-07-24 stsp goto done;
7707 0ebf8283 2019-07-24 stsp
7708 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
7709 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7710 818c7501 2019-07-11 stsp done:
7711 818c7501 2019-07-11 stsp free(old_id_str);
7712 818c7501 2019-07-11 stsp free(new_id_str);
7713 272a1371 2020-02-28 stsp free(logmsg);
7714 818c7501 2019-07-11 stsp return err;
7715 818c7501 2019-07-11 stsp }
7716 818c7501 2019-07-11 stsp
7717 1ee397ad 2019-07-12 stsp static const struct got_error *
7718 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7719 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
7720 e600f124 2021-03-21 stsp struct got_reference *tmp_branch, struct got_repository *repo,
7721 e600f124 2021-03-21 stsp int create_backup)
7722 818c7501 2019-07-11 stsp {
7723 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
7724 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
7725 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, branch, repo, create_backup);
7726 818c7501 2019-07-11 stsp }
7727 818c7501 2019-07-11 stsp
7728 818c7501 2019-07-11 stsp static const struct got_error *
7729 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
7730 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7731 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
7732 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7733 ff0d2220 2019-07-11 stsp {
7734 ff0d2220 2019-07-11 stsp const struct got_error *error;
7735 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
7736 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
7737 ff0d2220 2019-07-11 stsp
7738 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7739 ff0d2220 2019-07-11 stsp if (error)
7740 ff0d2220 2019-07-11 stsp return error;
7741 ff0d2220 2019-07-11 stsp
7742 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7743 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
7744 ff0d2220 2019-07-11 stsp if (error) {
7745 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7746 ff0d2220 2019-07-11 stsp goto done;
7747 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
7748 ff0d2220 2019-07-11 stsp } else {
7749 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
7750 ff0d2220 2019-07-11 stsp free(new_commit_id);
7751 ff0d2220 2019-07-11 stsp }
7752 ff0d2220 2019-07-11 stsp done:
7753 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
7754 ff0d2220 2019-07-11 stsp return error;
7755 64c6d990 2019-07-11 stsp }
7756 64c6d990 2019-07-11 stsp
7757 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
7758 64c6d990 2019-07-11 stsp const char *path_prefix;
7759 64c6d990 2019-07-11 stsp size_t len;
7760 8ca9bd68 2019-07-25 stsp int errcode;
7761 64c6d990 2019-07-11 stsp };
7762 64c6d990 2019-07-11 stsp
7763 64c6d990 2019-07-11 stsp static const struct got_error *
7764 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7765 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
7766 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
7767 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
7768 64c6d990 2019-07-11 stsp {
7769 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
7770 64c6d990 2019-07-11 stsp
7771 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7772 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7773 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
7774 64c6d990 2019-07-11 stsp
7775 64c6d990 2019-07-11 stsp return NULL;
7776 64c6d990 2019-07-11 stsp }
7777 64c6d990 2019-07-11 stsp
7778 64c6d990 2019-07-11 stsp static const struct got_error *
7779 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
7780 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
7781 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
7782 64c6d990 2019-07-11 stsp {
7783 64c6d990 2019-07-11 stsp const struct got_error *err;
7784 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7785 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7786 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
7787 64c6d990 2019-07-11 stsp
7788 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
7789 64c6d990 2019-07-11 stsp return NULL;
7790 64c6d990 2019-07-11 stsp
7791 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7792 64c6d990 2019-07-11 stsp if (err)
7793 64c6d990 2019-07-11 stsp goto done;
7794 64c6d990 2019-07-11 stsp
7795 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7796 64c6d990 2019-07-11 stsp if (err)
7797 64c6d990 2019-07-11 stsp goto done;
7798 64c6d990 2019-07-11 stsp
7799 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
7800 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
7801 64c6d990 2019-07-11 stsp if (err)
7802 64c6d990 2019-07-11 stsp goto done;
7803 64c6d990 2019-07-11 stsp
7804 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
7805 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
7806 64c6d990 2019-07-11 stsp if (err)
7807 64c6d990 2019-07-11 stsp goto done;
7808 64c6d990 2019-07-11 stsp
7809 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
7810 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
7811 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
7812 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7813 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
7814 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7815 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7816 64c6d990 2019-07-11 stsp done:
7817 64c6d990 2019-07-11 stsp if (tree1)
7818 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
7819 64c6d990 2019-07-11 stsp if (tree2)
7820 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
7821 64c6d990 2019-07-11 stsp if (commit)
7822 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
7823 64c6d990 2019-07-11 stsp if (parent_commit)
7824 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
7825 64c6d990 2019-07-11 stsp return err;
7826 ff0d2220 2019-07-11 stsp }
7827 ff0d2220 2019-07-11 stsp
7828 ff0d2220 2019-07-11 stsp static const struct got_error *
7829 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
7830 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
7831 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7832 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
7833 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
7834 af61c510 2019-07-19 stsp {
7835 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
7836 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
7837 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
7838 af61c510 2019-07-19 stsp struct got_object_qid *qid;
7839 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
7840 af61c510 2019-07-19 stsp
7841 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
7842 af61c510 2019-07-19 stsp if (err)
7843 af61c510 2019-07-19 stsp return err;
7844 af61c510 2019-07-19 stsp
7845 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7846 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7847 af61c510 2019-07-19 stsp if (err)
7848 af61c510 2019-07-19 stsp goto done;
7849 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7850 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7851 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
7852 af61c510 2019-07-19 stsp if (err) {
7853 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7854 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7855 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
7856 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
7857 af61c510 2019-07-19 stsp "been reached?!?");
7858 ee780d5c 2020-01-04 stsp }
7859 ee780d5c 2020-01-04 stsp goto done;
7860 af61c510 2019-07-19 stsp } else {
7861 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
7862 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
7863 af61c510 2019-07-19 stsp if (err)
7864 af61c510 2019-07-19 stsp goto done;
7865 af61c510 2019-07-19 stsp
7866 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
7867 af61c510 2019-07-19 stsp if (err)
7868 af61c510 2019-07-19 stsp goto done;
7869 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(commits, qid, entry);
7870 af61c510 2019-07-19 stsp commit_id = parent_id;
7871 af61c510 2019-07-19 stsp }
7872 af61c510 2019-07-19 stsp }
7873 af61c510 2019-07-19 stsp done:
7874 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
7875 af61c510 2019-07-19 stsp return err;
7876 e600f124 2021-03-21 stsp }
7877 e600f124 2021-03-21 stsp
7878 e600f124 2021-03-21 stsp static const struct got_error *
7879 e600f124 2021-03-21 stsp get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
7880 e600f124 2021-03-21 stsp {
7881 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
7882 e600f124 2021-03-21 stsp time_t committer_time;
7883 e600f124 2021-03-21 stsp struct tm tm;
7884 e600f124 2021-03-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
7885 e600f124 2021-03-21 stsp char *author0 = NULL, *author, *smallerthan;
7886 e600f124 2021-03-21 stsp char *logmsg0 = NULL, *logmsg, *newline;
7887 e600f124 2021-03-21 stsp
7888 e600f124 2021-03-21 stsp committer_time = got_object_commit_get_committer_time(commit);
7889 e600f124 2021-03-21 stsp if (localtime_r(&committer_time, &tm) == NULL)
7890 e600f124 2021-03-21 stsp return got_error_from_errno("localtime_r");
7891 e3199de8 2021-03-21 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
7892 e600f124 2021-03-21 stsp return got_error(GOT_ERR_NO_SPACE);
7893 e600f124 2021-03-21 stsp
7894 e600f124 2021-03-21 stsp author0 = strdup(got_object_commit_get_author(commit));
7895 e600f124 2021-03-21 stsp if (author0 == NULL)
7896 e600f124 2021-03-21 stsp return got_error_from_errno("strdup");
7897 e600f124 2021-03-21 stsp author = author0;
7898 e600f124 2021-03-21 stsp smallerthan = strchr(author, '<');
7899 e600f124 2021-03-21 stsp if (smallerthan && smallerthan[1] != '\0')
7900 e600f124 2021-03-21 stsp author = smallerthan + 1;
7901 e600f124 2021-03-21 stsp author[strcspn(author, "@>")] = '\0';
7902 e600f124 2021-03-21 stsp
7903 e600f124 2021-03-21 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7904 e600f124 2021-03-21 stsp if (err)
7905 e600f124 2021-03-21 stsp goto done;
7906 e600f124 2021-03-21 stsp logmsg = logmsg0;
7907 e600f124 2021-03-21 stsp while (*logmsg == '\n')
7908 e600f124 2021-03-21 stsp logmsg++;
7909 e600f124 2021-03-21 stsp newline = strchr(logmsg, '\n');
7910 e600f124 2021-03-21 stsp if (newline)
7911 e600f124 2021-03-21 stsp *newline = '\0';
7912 e600f124 2021-03-21 stsp
7913 e600f124 2021-03-21 stsp if (asprintf(brief_str, "%s %s %s",
7914 e600f124 2021-03-21 stsp datebuf, author, logmsg) == -1)
7915 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7916 e600f124 2021-03-21 stsp done:
7917 e600f124 2021-03-21 stsp free(author0);
7918 e600f124 2021-03-21 stsp free(logmsg0);
7919 643b85bc 2021-07-16 stsp return err;
7920 643b85bc 2021-07-16 stsp }
7921 643b85bc 2021-07-16 stsp
7922 643b85bc 2021-07-16 stsp static const struct got_error *
7923 643b85bc 2021-07-16 stsp delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
7924 643b85bc 2021-07-16 stsp struct got_repository *repo)
7925 643b85bc 2021-07-16 stsp {
7926 643b85bc 2021-07-16 stsp const struct got_error *err;
7927 643b85bc 2021-07-16 stsp char *id_str;
7928 643b85bc 2021-07-16 stsp
7929 643b85bc 2021-07-16 stsp err = got_object_id_str(&id_str, id);
7930 643b85bc 2021-07-16 stsp if (err)
7931 643b85bc 2021-07-16 stsp return err;
7932 643b85bc 2021-07-16 stsp
7933 643b85bc 2021-07-16 stsp err = got_ref_delete(ref, repo);
7934 643b85bc 2021-07-16 stsp if (err)
7935 643b85bc 2021-07-16 stsp goto done;
7936 643b85bc 2021-07-16 stsp
7937 643b85bc 2021-07-16 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
7938 643b85bc 2021-07-16 stsp done:
7939 643b85bc 2021-07-16 stsp free(id_str);
7940 e600f124 2021-03-21 stsp return err;
7941 e600f124 2021-03-21 stsp }
7942 e600f124 2021-03-21 stsp
7943 e600f124 2021-03-21 stsp static const struct got_error *
7944 e600f124 2021-03-21 stsp print_backup_ref(const char *branch_name, const char *new_id_str,
7945 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
7946 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap,
7947 e600f124 2021-03-21 stsp struct got_repository *repo)
7948 e600f124 2021-03-21 stsp {
7949 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
7950 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
7951 e600f124 2021-03-21 stsp char *refs_str = NULL;
7952 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id = NULL;
7953 e600f124 2021-03-21 stsp struct got_commit_object *new_commit = NULL;
7954 e600f124 2021-03-21 stsp char *new_commit_brief_str = NULL;
7955 e600f124 2021-03-21 stsp struct got_object_id *yca_id = NULL;
7956 e600f124 2021-03-21 stsp struct got_commit_object *yca_commit = NULL;
7957 e600f124 2021-03-21 stsp char *yca_id_str = NULL, *yca_brief_str = NULL;
7958 e600f124 2021-03-21 stsp char *custom_refs_str;
7959 e600f124 2021-03-21 stsp
7960 e600f124 2021-03-21 stsp if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
7961 e600f124 2021-03-21 stsp return got_error_from_errno("asprintf");
7962 e600f124 2021-03-21 stsp
7963 e600f124 2021-03-21 stsp err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
7964 e600f124 2021-03-21 stsp 0, 0, refs_idmap, custom_refs_str);
7965 e600f124 2021-03-21 stsp if (err)
7966 e600f124 2021-03-21 stsp goto done;
7967 e600f124 2021-03-21 stsp
7968 e600f124 2021-03-21 stsp err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
7969 e600f124 2021-03-21 stsp if (err)
7970 e600f124 2021-03-21 stsp goto done;
7971 e600f124 2021-03-21 stsp
7972 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
7973 e600f124 2021-03-21 stsp if (refs) {
7974 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, new_commit_id, repo);
7975 e600f124 2021-03-21 stsp if (err)
7976 e600f124 2021-03-21 stsp goto done;
7977 e600f124 2021-03-21 stsp }
7978 e600f124 2021-03-21 stsp
7979 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
7980 e600f124 2021-03-21 stsp if (err)
7981 e600f124 2021-03-21 stsp goto done;
7982 e600f124 2021-03-21 stsp
7983 e600f124 2021-03-21 stsp err = get_commit_brief_str(&new_commit_brief_str, new_commit);
7984 e600f124 2021-03-21 stsp if (err)
7985 e600f124 2021-03-21 stsp goto done;
7986 e600f124 2021-03-21 stsp
7987 e600f124 2021-03-21 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7988 e600f124 2021-03-21 stsp old_commit_id, new_commit_id, repo, check_cancelled, NULL);
7989 e600f124 2021-03-21 stsp if (err)
7990 e600f124 2021-03-21 stsp goto done;
7991 e600f124 2021-03-21 stsp
7992 e600f124 2021-03-21 stsp printf("has become commit %s%s%s%s\n %s\n", new_id_str,
7993 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
7994 e600f124 2021-03-21 stsp refs_str ? ")" : "", new_commit_brief_str);
7995 e600f124 2021-03-21 stsp if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
7996 e600f124 2021-03-21 stsp got_object_id_cmp(yca_id, old_commit_id) != 0) {
7997 e600f124 2021-03-21 stsp free(refs_str);
7998 e600f124 2021-03-21 stsp refs_str = NULL;
7999 e600f124 2021-03-21 stsp
8000 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&yca_commit, repo, yca_id);
8001 e600f124 2021-03-21 stsp if (err)
8002 e600f124 2021-03-21 stsp goto done;
8003 e600f124 2021-03-21 stsp
8004 e600f124 2021-03-21 stsp err = get_commit_brief_str(&yca_brief_str, yca_commit);
8005 e600f124 2021-03-21 stsp if (err)
8006 e600f124 2021-03-21 stsp goto done;
8007 e600f124 2021-03-21 stsp
8008 e600f124 2021-03-21 stsp err = got_object_id_str(&yca_id_str, yca_id);
8009 e600f124 2021-03-21 stsp if (err)
8010 e600f124 2021-03-21 stsp goto done;
8011 e600f124 2021-03-21 stsp
8012 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
8013 e600f124 2021-03-21 stsp if (refs) {
8014 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, yca_id, repo);
8015 e600f124 2021-03-21 stsp if (err)
8016 e600f124 2021-03-21 stsp goto done;
8017 e600f124 2021-03-21 stsp }
8018 e600f124 2021-03-21 stsp printf("history forked at %s%s%s%s\n %s\n",
8019 e600f124 2021-03-21 stsp yca_id_str,
8020 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
8021 e600f124 2021-03-21 stsp refs_str ? ")" : "", yca_brief_str);
8022 e600f124 2021-03-21 stsp }
8023 e600f124 2021-03-21 stsp done:
8024 e600f124 2021-03-21 stsp free(custom_refs_str);
8025 e600f124 2021-03-21 stsp free(new_commit_id);
8026 e600f124 2021-03-21 stsp free(refs_str);
8027 e600f124 2021-03-21 stsp free(yca_id);
8028 e600f124 2021-03-21 stsp free(yca_id_str);
8029 e600f124 2021-03-21 stsp free(yca_brief_str);
8030 e600f124 2021-03-21 stsp if (new_commit)
8031 e600f124 2021-03-21 stsp got_object_commit_close(new_commit);
8032 e600f124 2021-03-21 stsp if (yca_commit)
8033 e600f124 2021-03-21 stsp got_object_commit_close(yca_commit);
8034 e600f124 2021-03-21 stsp
8035 e600f124 2021-03-21 stsp return NULL;
8036 af61c510 2019-07-19 stsp }
8037 af61c510 2019-07-19 stsp
8038 af61c510 2019-07-19 stsp static const struct got_error *
8039 643b85bc 2021-07-16 stsp process_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
8040 643b85bc 2021-07-16 stsp int delete, struct got_repository *repo)
8041 e600f124 2021-03-21 stsp {
8042 e600f124 2021-03-21 stsp const struct got_error *err;
8043 e600f124 2021-03-21 stsp struct got_reflist_head refs, backup_refs;
8044 e600f124 2021-03-21 stsp struct got_reflist_entry *re;
8045 e600f124 2021-03-21 stsp const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
8046 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
8047 e600f124 2021-03-21 stsp char *branch_name = NULL;
8048 e600f124 2021-03-21 stsp struct got_commit_object *old_commit = NULL;
8049 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
8050 9e822917 2021-03-23 stsp int wanted_branch_found = 0;
8051 e600f124 2021-03-21 stsp
8052 e600f124 2021-03-21 stsp TAILQ_INIT(&refs);
8053 e600f124 2021-03-21 stsp TAILQ_INIT(&backup_refs);
8054 e600f124 2021-03-21 stsp
8055 e600f124 2021-03-21 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8056 e600f124 2021-03-21 stsp if (err)
8057 e600f124 2021-03-21 stsp return err;
8058 e600f124 2021-03-21 stsp
8059 e600f124 2021-03-21 stsp err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
8060 e600f124 2021-03-21 stsp if (err)
8061 e600f124 2021-03-21 stsp goto done;
8062 e600f124 2021-03-21 stsp
8063 e600f124 2021-03-21 stsp if (wanted_branch_name) {
8064 e600f124 2021-03-21 stsp if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
8065 e600f124 2021-03-21 stsp wanted_branch_name += 11;
8066 e600f124 2021-03-21 stsp }
8067 e600f124 2021-03-21 stsp
8068 e600f124 2021-03-21 stsp err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
8069 e600f124 2021-03-21 stsp got_ref_cmp_by_commit_timestamp_descending, repo);
8070 e600f124 2021-03-21 stsp if (err)
8071 e600f124 2021-03-21 stsp goto done;
8072 e600f124 2021-03-21 stsp
8073 e600f124 2021-03-21 stsp TAILQ_FOREACH(re, &backup_refs, entry) {
8074 e600f124 2021-03-21 stsp const char *refname = got_ref_get_name(re->ref);
8075 e600f124 2021-03-21 stsp char *slash;
8076 e600f124 2021-03-21 stsp
8077 643b85bc 2021-07-16 stsp err = check_cancelled(NULL);
8078 643b85bc 2021-07-16 stsp if (err)
8079 643b85bc 2021-07-16 stsp break;
8080 643b85bc 2021-07-16 stsp
8081 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, re->ref);
8082 e600f124 2021-03-21 stsp if (err)
8083 e600f124 2021-03-21 stsp break;
8084 e600f124 2021-03-21 stsp
8085 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&old_commit, repo,
8086 e600f124 2021-03-21 stsp old_commit_id);
8087 e600f124 2021-03-21 stsp if (err)
8088 e600f124 2021-03-21 stsp break;
8089 e600f124 2021-03-21 stsp
8090 e600f124 2021-03-21 stsp if (strncmp(backup_ref_prefix, refname,
8091 e600f124 2021-03-21 stsp backup_ref_prefix_len) == 0)
8092 e600f124 2021-03-21 stsp refname += backup_ref_prefix_len;
8093 e600f124 2021-03-21 stsp
8094 e600f124 2021-03-21 stsp while (refname[0] == '/')
8095 e600f124 2021-03-21 stsp refname++;
8096 e600f124 2021-03-21 stsp
8097 e600f124 2021-03-21 stsp branch_name = strdup(refname);
8098 e600f124 2021-03-21 stsp if (branch_name == NULL) {
8099 e600f124 2021-03-21 stsp err = got_error_from_errno("strdup");
8100 e600f124 2021-03-21 stsp break;
8101 e600f124 2021-03-21 stsp }
8102 e600f124 2021-03-21 stsp slash = strrchr(branch_name, '/');
8103 e600f124 2021-03-21 stsp if (slash) {
8104 e600f124 2021-03-21 stsp *slash = '\0';
8105 e600f124 2021-03-21 stsp refname += strlen(branch_name) + 1;
8106 e600f124 2021-03-21 stsp }
8107 e600f124 2021-03-21 stsp
8108 e600f124 2021-03-21 stsp if (wanted_branch_name == NULL ||
8109 e600f124 2021-03-21 stsp strcmp(wanted_branch_name, branch_name) == 0) {
8110 9e822917 2021-03-23 stsp wanted_branch_found = 1;
8111 643b85bc 2021-07-16 stsp if (delete) {
8112 643b85bc 2021-07-16 stsp err = delete_backup_ref(re->ref,
8113 643b85bc 2021-07-16 stsp old_commit_id, repo);
8114 643b85bc 2021-07-16 stsp } else {
8115 643b85bc 2021-07-16 stsp err = print_backup_ref(branch_name, refname,
8116 643b85bc 2021-07-16 stsp old_commit_id, old_commit, refs_idmap,
8117 643b85bc 2021-07-16 stsp repo);
8118 643b85bc 2021-07-16 stsp }
8119 e600f124 2021-03-21 stsp if (err)
8120 e600f124 2021-03-21 stsp break;
8121 e600f124 2021-03-21 stsp }
8122 e600f124 2021-03-21 stsp
8123 e600f124 2021-03-21 stsp free(old_commit_id);
8124 e600f124 2021-03-21 stsp old_commit_id = NULL;
8125 e600f124 2021-03-21 stsp free(branch_name);
8126 e600f124 2021-03-21 stsp branch_name = NULL;
8127 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
8128 e600f124 2021-03-21 stsp old_commit = NULL;
8129 e600f124 2021-03-21 stsp }
8130 9e822917 2021-03-23 stsp
8131 9e822917 2021-03-23 stsp if (wanted_branch_name && !wanted_branch_found) {
8132 9e822917 2021-03-23 stsp err = got_error_fmt(GOT_ERR_NOT_REF,
8133 9e822917 2021-03-23 stsp "%s/%s/", backup_ref_prefix, wanted_branch_name);
8134 9e822917 2021-03-23 stsp }
8135 e600f124 2021-03-21 stsp done:
8136 e600f124 2021-03-21 stsp if (refs_idmap)
8137 e600f124 2021-03-21 stsp got_reflist_object_id_map_free(refs_idmap);
8138 e600f124 2021-03-21 stsp got_ref_list_free(&refs);
8139 e600f124 2021-03-21 stsp got_ref_list_free(&backup_refs);
8140 e600f124 2021-03-21 stsp free(old_commit_id);
8141 e600f124 2021-03-21 stsp free(branch_name);
8142 e600f124 2021-03-21 stsp if (old_commit)
8143 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
8144 e600f124 2021-03-21 stsp return err;
8145 e600f124 2021-03-21 stsp }
8146 e600f124 2021-03-21 stsp
8147 e600f124 2021-03-21 stsp static const struct got_error *
8148 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
8149 818c7501 2019-07-11 stsp {
8150 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
8151 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
8152 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
8153 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8154 818c7501 2019-07-11 stsp char *cwd = NULL;
8155 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
8156 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
8157 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
8158 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
8159 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
8160 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
8161 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
8162 e600f124 2021-03-21 stsp int histedit_in_progress = 0, create_backup = 1, list_backups = 0;
8163 643b85bc 2021-07-16 stsp int delete_backups = 0;
8164 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8165 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
8166 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
8167 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
8168 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
8169 818c7501 2019-07-11 stsp
8170 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
8171 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
8172 818c7501 2019-07-11 stsp
8173 643b85bc 2021-07-16 stsp while ((ch = getopt(argc, argv, "aclX")) != -1) {
8174 818c7501 2019-07-11 stsp switch (ch) {
8175 818c7501 2019-07-11 stsp case 'a':
8176 818c7501 2019-07-11 stsp abort_rebase = 1;
8177 818c7501 2019-07-11 stsp break;
8178 818c7501 2019-07-11 stsp case 'c':
8179 818c7501 2019-07-11 stsp continue_rebase = 1;
8180 818c7501 2019-07-11 stsp break;
8181 e600f124 2021-03-21 stsp case 'l':
8182 e600f124 2021-03-21 stsp list_backups = 1;
8183 e600f124 2021-03-21 stsp break;
8184 643b85bc 2021-07-16 stsp case 'X':
8185 643b85bc 2021-07-16 stsp delete_backups = 1;
8186 643b85bc 2021-07-16 stsp break;
8187 818c7501 2019-07-11 stsp default:
8188 818c7501 2019-07-11 stsp usage_rebase();
8189 818c7501 2019-07-11 stsp /* NOTREACHED */
8190 818c7501 2019-07-11 stsp }
8191 818c7501 2019-07-11 stsp }
8192 818c7501 2019-07-11 stsp
8193 818c7501 2019-07-11 stsp argc -= optind;
8194 818c7501 2019-07-11 stsp argv += optind;
8195 818c7501 2019-07-11 stsp
8196 43012d58 2019-07-14 stsp #ifndef PROFILE
8197 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8198 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8199 43012d58 2019-07-14 stsp err(1, "pledge");
8200 43012d58 2019-07-14 stsp #endif
8201 e600f124 2021-03-21 stsp if (list_backups) {
8202 e600f124 2021-03-21 stsp if (abort_rebase)
8203 e600f124 2021-03-21 stsp option_conflict('l', 'a');
8204 e600f124 2021-03-21 stsp if (continue_rebase)
8205 e600f124 2021-03-21 stsp option_conflict('l', 'c');
8206 643b85bc 2021-07-16 stsp if (delete_backups)
8207 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
8208 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
8209 643b85bc 2021-07-16 stsp usage_rebase();
8210 643b85bc 2021-07-16 stsp } else if (delete_backups) {
8211 643b85bc 2021-07-16 stsp if (abort_rebase)
8212 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
8213 643b85bc 2021-07-16 stsp if (continue_rebase)
8214 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
8215 643b85bc 2021-07-16 stsp if (list_backups)
8216 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
8217 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
8218 818c7501 2019-07-11 stsp usage_rebase();
8219 e600f124 2021-03-21 stsp } else {
8220 e600f124 2021-03-21 stsp if (abort_rebase && continue_rebase)
8221 e600f124 2021-03-21 stsp usage_rebase();
8222 e600f124 2021-03-21 stsp else if (abort_rebase || continue_rebase) {
8223 e600f124 2021-03-21 stsp if (argc != 0)
8224 e600f124 2021-03-21 stsp usage_rebase();
8225 e600f124 2021-03-21 stsp } else if (argc != 1)
8226 e600f124 2021-03-21 stsp usage_rebase();
8227 e600f124 2021-03-21 stsp }
8228 818c7501 2019-07-11 stsp
8229 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
8230 818c7501 2019-07-11 stsp if (cwd == NULL) {
8231 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
8232 818c7501 2019-07-11 stsp goto done;
8233 818c7501 2019-07-11 stsp }
8234 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
8235 fa51e947 2020-03-27 stsp if (error) {
8236 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
8237 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
8238 e600f124 2021-03-21 stsp goto done;
8239 e600f124 2021-03-21 stsp } else {
8240 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8241 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
8242 e600f124 2021-03-21 stsp "rebase", cwd);
8243 e600f124 2021-03-21 stsp goto done;
8244 e600f124 2021-03-21 stsp }
8245 fa51e947 2020-03-27 stsp }
8246 818c7501 2019-07-11 stsp
8247 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
8248 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
8249 818c7501 2019-07-11 stsp if (error != NULL)
8250 818c7501 2019-07-11 stsp goto done;
8251 818c7501 2019-07-11 stsp
8252 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8253 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
8254 7ef62c4e 2020-02-24 stsp if (error)
8255 7ef62c4e 2020-02-24 stsp goto done;
8256 7ef62c4e 2020-02-24 stsp
8257 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
8258 643b85bc 2021-07-16 stsp error = process_backup_refs(
8259 643b85bc 2021-07-16 stsp GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
8260 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
8261 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
8262 e600f124 2021-03-21 stsp }
8263 e600f124 2021-03-21 stsp
8264 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
8265 7ef62c4e 2020-02-24 stsp worktree);
8266 818c7501 2019-07-11 stsp if (error)
8267 7ef62c4e 2020-02-24 stsp goto done;
8268 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
8269 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8270 818c7501 2019-07-11 stsp goto done;
8271 7ef62c4e 2020-02-24 stsp }
8272 818c7501 2019-07-11 stsp
8273 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8274 818c7501 2019-07-11 stsp if (error)
8275 818c7501 2019-07-11 stsp goto done;
8276 818c7501 2019-07-11 stsp
8277 f6794adc 2019-07-23 stsp if (abort_rebase) {
8278 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8279 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
8280 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
8281 f6794adc 2019-07-23 stsp goto done;
8282 f6794adc 2019-07-23 stsp }
8283 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
8284 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
8285 3e3a69f1 2019-07-25 stsp worktree, repo);
8286 818c7501 2019-07-11 stsp if (error)
8287 818c7501 2019-07-11 stsp goto done;
8288 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
8289 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
8290 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8291 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
8292 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
8293 818c7501 2019-07-11 stsp if (error)
8294 818c7501 2019-07-11 stsp goto done;
8295 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
8296 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8297 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
8298 818c7501 2019-07-11 stsp }
8299 818c7501 2019-07-11 stsp
8300 818c7501 2019-07-11 stsp if (continue_rebase) {
8301 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
8302 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
8303 f6794adc 2019-07-23 stsp goto done;
8304 f6794adc 2019-07-23 stsp }
8305 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
8306 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
8307 3e3a69f1 2019-07-25 stsp worktree, repo);
8308 818c7501 2019-07-11 stsp if (error)
8309 818c7501 2019-07-11 stsp goto done;
8310 818c7501 2019-07-11 stsp
8311 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
8312 01757395 2019-07-12 stsp resume_commit_id, repo);
8313 818c7501 2019-07-11 stsp if (error)
8314 818c7501 2019-07-11 stsp goto done;
8315 818c7501 2019-07-11 stsp
8316 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
8317 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
8318 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
8319 818c7501 2019-07-11 stsp goto done;
8320 818c7501 2019-07-11 stsp }
8321 818c7501 2019-07-11 stsp } else {
8322 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
8323 818c7501 2019-07-11 stsp if (error != NULL)
8324 818c7501 2019-07-11 stsp goto done;
8325 ff0d2220 2019-07-11 stsp }
8326 818c7501 2019-07-11 stsp
8327 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
8328 ff0d2220 2019-07-11 stsp if (error)
8329 ff0d2220 2019-07-11 stsp goto done;
8330 ff0d2220 2019-07-11 stsp
8331 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
8332 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
8333 a51a74b3 2019-07-27 stsp
8334 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
8335 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8336 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
8337 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
8338 818c7501 2019-07-11 stsp if (error)
8339 818c7501 2019-07-11 stsp goto done;
8340 818c7501 2019-07-11 stsp if (yca_id == NULL) {
8341 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
8342 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
8343 818c7501 2019-07-11 stsp "with work tree's branch");
8344 818c7501 2019-07-11 stsp goto done;
8345 818c7501 2019-07-11 stsp }
8346 818c7501 2019-07-11 stsp
8347 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
8348 a51a74b3 2019-07-27 stsp if (error) {
8349 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
8350 a51a74b3 2019-07-27 stsp goto done;
8351 a51a74b3 2019-07-27 stsp error = NULL;
8352 a51a74b3 2019-07-27 stsp } else {
8353 df3ed485 2021-01-31 stsp static char msg[128];
8354 df3ed485 2021-01-31 stsp snprintf(msg, sizeof(msg),
8355 df3ed485 2021-01-31 stsp "%s is already based on %s",
8356 df3ed485 2021-01-31 stsp got_ref_get_name(branch),
8357 df3ed485 2021-01-31 stsp got_worktree_get_head_ref_name(worktree));
8358 df3ed485 2021-01-31 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
8359 a51a74b3 2019-07-27 stsp goto done;
8360 a51a74b3 2019-07-27 stsp }
8361 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
8362 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
8363 818c7501 2019-07-11 stsp if (error)
8364 818c7501 2019-07-11 stsp goto done;
8365 818c7501 2019-07-11 stsp }
8366 818c7501 2019-07-11 stsp
8367 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
8368 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8369 818c7501 2019-07-11 stsp if (error)
8370 818c7501 2019-07-11 stsp goto done;
8371 818c7501 2019-07-11 stsp
8372 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8373 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
8374 fc66b545 2019-08-12 stsp if (pid == NULL) {
8375 fc66b545 2019-08-12 stsp if (!continue_rebase) {
8376 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8377 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8378 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
8379 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
8380 fc66b545 2019-08-12 stsp if (error)
8381 fc66b545 2019-08-12 stsp goto done;
8382 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
8383 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
8384 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8385 9627c110 2020-04-18 stsp
8386 fc66b545 2019-08-12 stsp }
8387 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
8388 fc66b545 2019-08-12 stsp goto done;
8389 fc66b545 2019-08-12 stsp }
8390 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
8391 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
8392 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
8393 818c7501 2019-07-11 stsp got_object_commit_close(commit);
8394 818c7501 2019-07-11 stsp commit = NULL;
8395 818c7501 2019-07-11 stsp if (error)
8396 818c7501 2019-07-11 stsp goto done;
8397 64c6d990 2019-07-11 stsp
8398 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
8399 38b0338b 2019-11-29 stsp if (continue_rebase) {
8400 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
8401 e600f124 2021-03-21 stsp branch, new_base_branch, tmp_branch, repo,
8402 e600f124 2021-03-21 stsp create_backup);
8403 38b0338b 2019-11-29 stsp goto done;
8404 38b0338b 2019-11-29 stsp } else {
8405 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
8406 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
8407 38b0338b 2019-11-29 stsp char *id_str;
8408 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
8409 38b0338b 2019-11-29 stsp new_base_branch);
8410 38b0338b 2019-11-29 stsp if (error)
8411 38b0338b 2019-11-29 stsp goto done;
8412 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
8413 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
8414 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
8415 38b0338b 2019-11-29 stsp free(id_str);
8416 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
8417 38b0338b 2019-11-29 stsp new_head_commit_id);
8418 38b0338b 2019-11-29 stsp if (error)
8419 38b0338b 2019-11-29 stsp goto done;
8420 e600f124 2021-03-21 stsp /* No backup needed since objects did not change. */
8421 e600f124 2021-03-21 stsp create_backup = 0;
8422 38b0338b 2019-11-29 stsp }
8423 818c7501 2019-07-11 stsp }
8424 818c7501 2019-07-11 stsp
8425 818c7501 2019-07-11 stsp pid = NULL;
8426 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &commits, entry) {
8427 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8428 9627c110 2020-04-18 stsp
8429 818c7501 2019-07-11 stsp commit_id = qid->id;
8430 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
8431 818c7501 2019-07-11 stsp pid = qid;
8432 818c7501 2019-07-11 stsp
8433 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8434 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
8435 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
8436 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8437 818c7501 2019-07-11 stsp if (error)
8438 818c7501 2019-07-11 stsp goto done;
8439 9627c110 2020-04-18 stsp
8440 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8441 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
8442 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
8443 818c7501 2019-07-11 stsp
8444 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8445 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
8446 a0ea4fc0 2020-02-28 stsp if (error)
8447 a0ea4fc0 2020-02-28 stsp goto done;
8448 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8449 818c7501 2019-07-11 stsp break;
8450 01757395 2019-07-12 stsp }
8451 818c7501 2019-07-11 stsp
8452 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
8453 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
8454 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8455 818c7501 2019-07-11 stsp if (error)
8456 818c7501 2019-07-11 stsp goto done;
8457 818c7501 2019-07-11 stsp }
8458 818c7501 2019-07-11 stsp
8459 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8460 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
8461 818c7501 2019-07-11 stsp if (error)
8462 818c7501 2019-07-11 stsp goto done;
8463 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8464 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
8465 818c7501 2019-07-11 stsp } else
8466 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
8467 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, repo, create_backup);
8468 818c7501 2019-07-11 stsp done:
8469 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
8470 818c7501 2019-07-11 stsp free(branch_head_commit_id);
8471 818c7501 2019-07-11 stsp free(resume_commit_id);
8472 818c7501 2019-07-11 stsp free(yca_id);
8473 818c7501 2019-07-11 stsp if (commit)
8474 818c7501 2019-07-11 stsp got_object_commit_close(commit);
8475 818c7501 2019-07-11 stsp if (branch)
8476 818c7501 2019-07-11 stsp got_ref_close(branch);
8477 818c7501 2019-07-11 stsp if (new_base_branch)
8478 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
8479 818c7501 2019-07-11 stsp if (tmp_branch)
8480 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
8481 5ef14e63 2019-06-02 stsp if (worktree)
8482 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
8483 1d0f4054 2021-06-17 stsp if (repo) {
8484 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8485 1d0f4054 2021-06-17 stsp if (error == NULL)
8486 1d0f4054 2021-06-17 stsp error = close_err;
8487 1d0f4054 2021-06-17 stsp }
8488 5ef14e63 2019-06-02 stsp return error;
8489 0ebf8283 2019-07-24 stsp }
8490 0ebf8283 2019-07-24 stsp
8491 0ebf8283 2019-07-24 stsp __dead static void
8492 0ebf8283 2019-07-24 stsp usage_histedit(void)
8493 0ebf8283 2019-07-24 stsp {
8494 e600f124 2021-03-21 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] "
8495 643b85bc 2021-07-16 stsp "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
8496 643b85bc 2021-07-16 stsp getprogname());
8497 0ebf8283 2019-07-24 stsp exit(1);
8498 0ebf8283 2019-07-24 stsp }
8499 0ebf8283 2019-07-24 stsp
8500 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
8501 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
8502 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
8503 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
8504 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
8505 0ebf8283 2019-07-24 stsp
8506 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
8507 0ebf8283 2019-07-24 stsp unsigned char code;
8508 0ebf8283 2019-07-24 stsp const char *name;
8509 0ebf8283 2019-07-24 stsp const char *desc;
8510 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
8511 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
8512 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
8513 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
8514 82997472 2020-01-29 stsp "be used" },
8515 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
8516 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
8517 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
8518 0ebf8283 2019-07-24 stsp };
8519 0ebf8283 2019-07-24 stsp
8520 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
8521 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
8522 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
8523 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
8524 0ebf8283 2019-07-24 stsp char *logmsg;
8525 0ebf8283 2019-07-24 stsp };
8526 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
8527 0ebf8283 2019-07-24 stsp
8528 0ebf8283 2019-07-24 stsp static const struct got_error *
8529 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
8530 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
8531 0ebf8283 2019-07-24 stsp {
8532 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8533 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
8534 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8535 8138f3e1 2019-08-11 stsp int n;
8536 0ebf8283 2019-07-24 stsp
8537 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8538 0ebf8283 2019-07-24 stsp if (err)
8539 0ebf8283 2019-07-24 stsp goto done;
8540 0ebf8283 2019-07-24 stsp
8541 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
8542 0ebf8283 2019-07-24 stsp if (err)
8543 0ebf8283 2019-07-24 stsp goto done;
8544 0ebf8283 2019-07-24 stsp
8545 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
8546 0ebf8283 2019-07-24 stsp if (err)
8547 0ebf8283 2019-07-24 stsp goto done;
8548 0ebf8283 2019-07-24 stsp
8549 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
8550 0ebf8283 2019-07-24 stsp if (n < 0)
8551 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8552 0ebf8283 2019-07-24 stsp done:
8553 0ebf8283 2019-07-24 stsp if (commit)
8554 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8555 0ebf8283 2019-07-24 stsp free(id_str);
8556 0ebf8283 2019-07-24 stsp free(logmsg);
8557 0ebf8283 2019-07-24 stsp return err;
8558 0ebf8283 2019-07-24 stsp }
8559 0ebf8283 2019-07-24 stsp
8560 0ebf8283 2019-07-24 stsp static const struct got_error *
8561 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
8562 466785b9 2020-12-10 jrick FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
8563 0ebf8283 2019-07-24 stsp {
8564 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8565 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
8566 466785b9 2020-12-10 jrick const char *histedit_cmd = NULL;
8567 0ebf8283 2019-07-24 stsp
8568 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
8569 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
8570 0ebf8283 2019-07-24 stsp
8571 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
8572 466785b9 2020-12-10 jrick histedit_cmd = got_histedit_cmds[0].name;
8573 dbdddfee 2021-06-23 naddy if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
8574 466785b9 2020-12-10 jrick histedit_cmd = "fold";
8575 466785b9 2020-12-10 jrick err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
8576 0ebf8283 2019-07-24 stsp if (err)
8577 0ebf8283 2019-07-24 stsp break;
8578 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
8579 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
8580 083957f4 2020-02-24 stsp if (n < 0) {
8581 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
8582 083957f4 2020-02-24 stsp break;
8583 083957f4 2020-02-24 stsp }
8584 083957f4 2020-02-24 stsp }
8585 0ebf8283 2019-07-24 stsp }
8586 0ebf8283 2019-07-24 stsp
8587 0ebf8283 2019-07-24 stsp return err;
8588 0ebf8283 2019-07-24 stsp }
8589 0ebf8283 2019-07-24 stsp
8590 0ebf8283 2019-07-24 stsp static const struct got_error *
8591 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
8592 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
8593 0ebf8283 2019-07-24 stsp {
8594 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8595 6059809a 2020-12-17 stsp size_t i;
8596 6059809a 2020-12-17 stsp int n;
8597 514f2ffe 2020-01-29 stsp char *id_str;
8598 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
8599 514f2ffe 2020-01-29 stsp
8600 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(commits);
8601 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
8602 514f2ffe 2020-01-29 stsp if (err)
8603 514f2ffe 2020-01-29 stsp return err;
8604 514f2ffe 2020-01-29 stsp
8605 514f2ffe 2020-01-29 stsp n = fprintf(f,
8606 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
8607 514f2ffe 2020-01-29 stsp "# commit %s\n"
8608 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
8609 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
8610 514f2ffe 2020-01-29 stsp if (n < 0) {
8611 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
8612 514f2ffe 2020-01-29 stsp goto done;
8613 514f2ffe 2020-01-29 stsp }
8614 0ebf8283 2019-07-24 stsp
8615 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
8616 514f2ffe 2020-01-29 stsp if (n < 0) {
8617 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
8618 514f2ffe 2020-01-29 stsp goto done;
8619 514f2ffe 2020-01-29 stsp }
8620 0ebf8283 2019-07-24 stsp
8621 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
8622 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
8623 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
8624 0ebf8283 2019-07-24 stsp cmd->desc);
8625 0ebf8283 2019-07-24 stsp if (n < 0) {
8626 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8627 0ebf8283 2019-07-24 stsp break;
8628 0ebf8283 2019-07-24 stsp }
8629 0ebf8283 2019-07-24 stsp }
8630 514f2ffe 2020-01-29 stsp done:
8631 514f2ffe 2020-01-29 stsp free(id_str);
8632 0ebf8283 2019-07-24 stsp return err;
8633 0ebf8283 2019-07-24 stsp }
8634 0ebf8283 2019-07-24 stsp
8635 0ebf8283 2019-07-24 stsp static const struct got_error *
8636 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
8637 0ebf8283 2019-07-24 stsp {
8638 0ebf8283 2019-07-24 stsp static char msg[42];
8639 0ebf8283 2019-07-24 stsp int ret;
8640 0ebf8283 2019-07-24 stsp
8641 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
8642 0ebf8283 2019-07-24 stsp lineno);
8643 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
8644 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
8645 0ebf8283 2019-07-24 stsp
8646 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
8647 0ebf8283 2019-07-24 stsp }
8648 0ebf8283 2019-07-24 stsp
8649 0ebf8283 2019-07-24 stsp static const struct got_error *
8650 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
8651 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
8652 0ebf8283 2019-07-24 stsp {
8653 0ebf8283 2019-07-24 stsp const struct got_error *err;
8654 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
8655 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
8656 0ebf8283 2019-07-24 stsp
8657 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8658 0ebf8283 2019-07-24 stsp if (err)
8659 0ebf8283 2019-07-24 stsp return err;
8660 0ebf8283 2019-07-24 stsp
8661 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
8662 0ebf8283 2019-07-24 stsp if (err)
8663 0ebf8283 2019-07-24 stsp goto done;
8664 0ebf8283 2019-07-24 stsp
8665 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
8666 5943eee2 2019-08-13 stsp if (err)
8667 5943eee2 2019-08-13 stsp goto done;
8668 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
8669 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
8670 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
8671 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
8672 0ebf8283 2019-07-24 stsp }
8673 0ebf8283 2019-07-24 stsp done:
8674 0ebf8283 2019-07-24 stsp if (folded_commit)
8675 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
8676 0ebf8283 2019-07-24 stsp free(id_str);
8677 5943eee2 2019-08-13 stsp free(folded_logmsg);
8678 0ebf8283 2019-07-24 stsp return err;
8679 0ebf8283 2019-07-24 stsp }
8680 0ebf8283 2019-07-24 stsp
8681 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
8682 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
8683 0ebf8283 2019-07-24 stsp {
8684 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
8685 0ebf8283 2019-07-24 stsp
8686 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
8687 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
8688 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
8689 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
8690 3f9de99f 2019-07-24 stsp folded = prev;
8691 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
8692 0ebf8283 2019-07-24 stsp }
8693 0ebf8283 2019-07-24 stsp
8694 0ebf8283 2019-07-24 stsp return folded;
8695 0ebf8283 2019-07-24 stsp }
8696 0ebf8283 2019-07-24 stsp
8697 0ebf8283 2019-07-24 stsp static const struct got_error *
8698 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
8699 0ebf8283 2019-07-24 stsp struct got_repository *repo)
8700 0ebf8283 2019-07-24 stsp {
8701 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
8702 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
8703 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8704 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8705 1601cb9f 2020-09-11 naddy int logmsg_len;
8706 0ebf8283 2019-07-24 stsp int fd;
8707 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
8708 0ebf8283 2019-07-24 stsp
8709 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8710 0ebf8283 2019-07-24 stsp if (err)
8711 0ebf8283 2019-07-24 stsp return err;
8712 0ebf8283 2019-07-24 stsp
8713 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
8714 0ebf8283 2019-07-24 stsp if (folded) {
8715 0ebf8283 2019-07-24 stsp while (folded != hle) {
8716 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
8717 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
8718 3f9de99f 2019-07-24 stsp continue;
8719 3f9de99f 2019-07-24 stsp }
8720 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
8721 0ebf8283 2019-07-24 stsp logmsg, repo);
8722 0ebf8283 2019-07-24 stsp if (err)
8723 0ebf8283 2019-07-24 stsp goto done;
8724 0ebf8283 2019-07-24 stsp free(logmsg);
8725 0ebf8283 2019-07-24 stsp logmsg = new_msg;
8726 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
8727 0ebf8283 2019-07-24 stsp }
8728 0ebf8283 2019-07-24 stsp }
8729 0ebf8283 2019-07-24 stsp
8730 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8731 0ebf8283 2019-07-24 stsp if (err)
8732 0ebf8283 2019-07-24 stsp goto done;
8733 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
8734 5943eee2 2019-08-13 stsp if (err)
8735 5943eee2 2019-08-13 stsp goto done;
8736 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
8737 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
8738 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
8739 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
8740 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
8741 0ebf8283 2019-07-24 stsp goto done;
8742 0ebf8283 2019-07-24 stsp }
8743 0ebf8283 2019-07-24 stsp free(logmsg);
8744 0ebf8283 2019-07-24 stsp logmsg = new_msg;
8745 0ebf8283 2019-07-24 stsp
8746 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8747 0ebf8283 2019-07-24 stsp if (err)
8748 0ebf8283 2019-07-24 stsp goto done;
8749 0ebf8283 2019-07-24 stsp
8750 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
8751 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
8752 0ebf8283 2019-07-24 stsp if (err)
8753 0ebf8283 2019-07-24 stsp goto done;
8754 0ebf8283 2019-07-24 stsp
8755 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
8756 0ebf8283 2019-07-24 stsp close(fd);
8757 0ebf8283 2019-07-24 stsp
8758 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8759 0ebf8283 2019-07-24 stsp if (err)
8760 0ebf8283 2019-07-24 stsp goto done;
8761 0ebf8283 2019-07-24 stsp
8762 bfa12d5e 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
8763 0d5bb276 2020-12-15 stsp logmsg_len, 0);
8764 0ebf8283 2019-07-24 stsp if (err) {
8765 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
8766 0ebf8283 2019-07-24 stsp goto done;
8767 71392a05 2020-12-13 stsp err = NULL;
8768 71392a05 2020-12-13 stsp hle->logmsg = strdup(new_msg);
8769 71392a05 2020-12-13 stsp if (hle->logmsg == NULL)
8770 71392a05 2020-12-13 stsp err = got_error_from_errno("strdup");
8771 0ebf8283 2019-07-24 stsp }
8772 0ebf8283 2019-07-24 stsp done:
8773 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
8774 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
8775 0ebf8283 2019-07-24 stsp free(logmsg_path);
8776 0ebf8283 2019-07-24 stsp free(logmsg);
8777 5943eee2 2019-08-13 stsp free(orig_logmsg);
8778 0ebf8283 2019-07-24 stsp free(editor);
8779 0ebf8283 2019-07-24 stsp if (commit)
8780 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8781 0ebf8283 2019-07-24 stsp return err;
8782 5ef14e63 2019-06-02 stsp }
8783 0ebf8283 2019-07-24 stsp
8784 0ebf8283 2019-07-24 stsp static const struct got_error *
8785 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
8786 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
8787 0ebf8283 2019-07-24 stsp {
8788 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8789 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
8790 6059809a 2020-12-17 stsp size_t i, size;
8791 0ebf8283 2019-07-24 stsp ssize_t len;
8792 6059809a 2020-12-17 stsp int lineno = 0;
8793 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
8794 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
8795 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
8796 0ebf8283 2019-07-24 stsp
8797 0ebf8283 2019-07-24 stsp for (;;) {
8798 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
8799 0ebf8283 2019-07-24 stsp if (len == -1) {
8800 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
8801 0ebf8283 2019-07-24 stsp if (feof(f))
8802 0ebf8283 2019-07-24 stsp break;
8803 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
8804 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
8805 0ebf8283 2019-07-24 stsp break;
8806 0ebf8283 2019-07-24 stsp }
8807 0ebf8283 2019-07-24 stsp lineno++;
8808 0ebf8283 2019-07-24 stsp p = line;
8809 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8810 0ebf8283 2019-07-24 stsp p++;
8811 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
8812 0ebf8283 2019-07-24 stsp free(line);
8813 0ebf8283 2019-07-24 stsp line = NULL;
8814 0ebf8283 2019-07-24 stsp continue;
8815 0ebf8283 2019-07-24 stsp }
8816 0ebf8283 2019-07-24 stsp cmd = NULL;
8817 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
8818 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
8819 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
8820 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
8821 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
8822 0ebf8283 2019-07-24 stsp break;
8823 0ebf8283 2019-07-24 stsp }
8824 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
8825 0ebf8283 2019-07-24 stsp p++;
8826 0ebf8283 2019-07-24 stsp break;
8827 0ebf8283 2019-07-24 stsp }
8828 0ebf8283 2019-07-24 stsp }
8829 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
8830 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8831 0ebf8283 2019-07-24 stsp break;
8832 0ebf8283 2019-07-24 stsp }
8833 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8834 0ebf8283 2019-07-24 stsp p++;
8835 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
8836 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
8837 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
8838 0ebf8283 2019-07-24 stsp break;
8839 0ebf8283 2019-07-24 stsp }
8840 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
8841 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8842 0ebf8283 2019-07-24 stsp if (err)
8843 0ebf8283 2019-07-24 stsp break;
8844 0ebf8283 2019-07-24 stsp } else {
8845 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
8846 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
8847 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8848 0ebf8283 2019-07-24 stsp break;
8849 0ebf8283 2019-07-24 stsp }
8850 0ebf8283 2019-07-24 stsp }
8851 0ebf8283 2019-07-24 stsp free(line);
8852 0ebf8283 2019-07-24 stsp line = NULL;
8853 0ebf8283 2019-07-24 stsp continue;
8854 0ebf8283 2019-07-24 stsp } else {
8855 0ebf8283 2019-07-24 stsp end = p;
8856 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
8857 0ebf8283 2019-07-24 stsp end++;
8858 0ebf8283 2019-07-24 stsp *end = '\0';
8859 0ebf8283 2019-07-24 stsp
8860 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
8861 0ebf8283 2019-07-24 stsp if (err) {
8862 0ebf8283 2019-07-24 stsp /* override error code */
8863 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8864 0ebf8283 2019-07-24 stsp break;
8865 0ebf8283 2019-07-24 stsp }
8866 0ebf8283 2019-07-24 stsp }
8867 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
8868 0ebf8283 2019-07-24 stsp if (hle == NULL) {
8869 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
8870 0ebf8283 2019-07-24 stsp break;
8871 0ebf8283 2019-07-24 stsp }
8872 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
8873 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
8874 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
8875 0ebf8283 2019-07-24 stsp commit_id = NULL;
8876 0ebf8283 2019-07-24 stsp free(line);
8877 0ebf8283 2019-07-24 stsp line = NULL;
8878 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
8879 0ebf8283 2019-07-24 stsp }
8880 0ebf8283 2019-07-24 stsp
8881 0ebf8283 2019-07-24 stsp free(line);
8882 0ebf8283 2019-07-24 stsp free(commit_id);
8883 0ebf8283 2019-07-24 stsp return err;
8884 0ebf8283 2019-07-24 stsp }
8885 0ebf8283 2019-07-24 stsp
8886 0ebf8283 2019-07-24 stsp static const struct got_error *
8887 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
8888 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
8889 bfce7f83 2019-07-27 stsp {
8890 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
8891 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
8892 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8893 5b87815e 2020-03-05 stsp static char msg[92];
8894 bfce7f83 2019-07-27 stsp char *id_str;
8895 bfce7f83 2019-07-27 stsp
8896 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
8897 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
8898 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
8899 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
8900 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
8901 5b87815e 2020-03-05 stsp
8902 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8903 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
8904 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
8905 5b87815e 2020-03-05 stsp if (hle == hle2)
8906 5b87815e 2020-03-05 stsp continue;
8907 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
8908 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
8909 5b87815e 2020-03-05 stsp continue;
8910 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
8911 5b87815e 2020-03-05 stsp if (err)
8912 5b87815e 2020-03-05 stsp return err;
8913 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
8914 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
8915 5b87815e 2020-03-05 stsp free(id_str);
8916 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8917 5b87815e 2020-03-05 stsp }
8918 5b87815e 2020-03-05 stsp }
8919 bfce7f83 2019-07-27 stsp
8920 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
8921 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8922 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8923 bfce7f83 2019-07-27 stsp break;
8924 bfce7f83 2019-07-27 stsp }
8925 bfce7f83 2019-07-27 stsp if (hle == NULL) {
8926 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
8927 bfce7f83 2019-07-27 stsp if (err)
8928 bfce7f83 2019-07-27 stsp return err;
8929 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
8930 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
8931 bfce7f83 2019-07-27 stsp free(id_str);
8932 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8933 bfce7f83 2019-07-27 stsp }
8934 bfce7f83 2019-07-27 stsp }
8935 bfce7f83 2019-07-27 stsp
8936 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8937 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8938 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8939 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
8940 bfce7f83 2019-07-27 stsp
8941 bfce7f83 2019-07-27 stsp return NULL;
8942 bfce7f83 2019-07-27 stsp }
8943 bfce7f83 2019-07-27 stsp
8944 bfce7f83 2019-07-27 stsp static const struct got_error *
8945 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
8946 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
8947 bfce7f83 2019-07-27 stsp struct got_repository *repo)
8948 0ebf8283 2019-07-24 stsp {
8949 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8950 0ebf8283 2019-07-24 stsp char *editor;
8951 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8952 0ebf8283 2019-07-24 stsp
8953 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8954 0ebf8283 2019-07-24 stsp if (err)
8955 0ebf8283 2019-07-24 stsp return err;
8956 0ebf8283 2019-07-24 stsp
8957 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
8958 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
8959 0ebf8283 2019-07-24 stsp goto done;
8960 0ebf8283 2019-07-24 stsp }
8961 0ebf8283 2019-07-24 stsp
8962 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8963 0ebf8283 2019-07-24 stsp if (f == NULL) {
8964 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
8965 0ebf8283 2019-07-24 stsp goto done;
8966 0ebf8283 2019-07-24 stsp }
8967 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8968 bfce7f83 2019-07-27 stsp if (err)
8969 bfce7f83 2019-07-27 stsp goto done;
8970 bfce7f83 2019-07-27 stsp
8971 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
8972 0ebf8283 2019-07-24 stsp done:
8973 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8974 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8975 0ebf8283 2019-07-24 stsp free(editor);
8976 0ebf8283 2019-07-24 stsp return err;
8977 0ebf8283 2019-07-24 stsp }
8978 0ebf8283 2019-07-24 stsp
8979 0ebf8283 2019-07-24 stsp static const struct got_error *
8980 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8981 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
8982 514f2ffe 2020-01-29 stsp struct got_repository *);
8983 0ebf8283 2019-07-24 stsp
8984 0ebf8283 2019-07-24 stsp static const struct got_error *
8985 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
8986 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
8987 466785b9 2020-12-10 jrick int edit_logmsg_only, int fold_only, struct got_repository *repo)
8988 0ebf8283 2019-07-24 stsp {
8989 0ebf8283 2019-07-24 stsp const struct got_error *err;
8990 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8991 0ebf8283 2019-07-24 stsp char *path = NULL;
8992 0ebf8283 2019-07-24 stsp
8993 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
8994 0ebf8283 2019-07-24 stsp if (err)
8995 0ebf8283 2019-07-24 stsp return err;
8996 0ebf8283 2019-07-24 stsp
8997 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
8998 0ebf8283 2019-07-24 stsp if (err)
8999 0ebf8283 2019-07-24 stsp goto done;
9000 0ebf8283 2019-07-24 stsp
9001 466785b9 2020-12-10 jrick err = histedit_write_commit_list(commits, f, edit_logmsg_only,
9002 466785b9 2020-12-10 jrick fold_only, repo);
9003 0ebf8283 2019-07-24 stsp if (err)
9004 0ebf8283 2019-07-24 stsp goto done;
9005 0ebf8283 2019-07-24 stsp
9006 466785b9 2020-12-10 jrick if (edit_logmsg_only || fold_only) {
9007 083957f4 2020-02-24 stsp rewind(f);
9008 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9009 083957f4 2020-02-24 stsp } else {
9010 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF) {
9011 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
9012 0ebf8283 2019-07-24 stsp goto done;
9013 083957f4 2020-02-24 stsp }
9014 083957f4 2020-02-24 stsp f = NULL;
9015 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
9016 083957f4 2020-02-24 stsp if (err) {
9017 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9018 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9019 083957f4 2020-02-24 stsp goto done;
9020 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
9021 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
9022 083957f4 2020-02-24 stsp }
9023 0ebf8283 2019-07-24 stsp }
9024 0ebf8283 2019-07-24 stsp done:
9025 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9026 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9027 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
9028 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
9029 0ebf8283 2019-07-24 stsp free(path);
9030 0ebf8283 2019-07-24 stsp return err;
9031 0ebf8283 2019-07-24 stsp }
9032 0ebf8283 2019-07-24 stsp
9033 0ebf8283 2019-07-24 stsp static const struct got_error *
9034 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
9035 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
9036 0ebf8283 2019-07-24 stsp {
9037 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9038 0ebf8283 2019-07-24 stsp char *path = NULL;
9039 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9040 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
9041 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9042 0ebf8283 2019-07-24 stsp
9043 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
9044 0ebf8283 2019-07-24 stsp if (err)
9045 0ebf8283 2019-07-24 stsp return err;
9046 0ebf8283 2019-07-24 stsp
9047 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
9048 0ebf8283 2019-07-24 stsp if (f == NULL) {
9049 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
9050 0ebf8283 2019-07-24 stsp goto done;
9051 0ebf8283 2019-07-24 stsp }
9052 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9053 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
9054 0ebf8283 2019-07-24 stsp repo);
9055 0ebf8283 2019-07-24 stsp if (err)
9056 0ebf8283 2019-07-24 stsp break;
9057 0ebf8283 2019-07-24 stsp
9058 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
9059 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
9060 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
9061 0ebf8283 2019-07-24 stsp if (n < 0) {
9062 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9063 0ebf8283 2019-07-24 stsp break;
9064 0ebf8283 2019-07-24 stsp }
9065 0ebf8283 2019-07-24 stsp }
9066 0ebf8283 2019-07-24 stsp }
9067 0ebf8283 2019-07-24 stsp done:
9068 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9069 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9070 0ebf8283 2019-07-24 stsp free(path);
9071 0ebf8283 2019-07-24 stsp if (commit)
9072 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9073 0ebf8283 2019-07-24 stsp return err;
9074 0ebf8283 2019-07-24 stsp }
9075 0ebf8283 2019-07-24 stsp
9076 bfce7f83 2019-07-27 stsp void
9077 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
9078 bfce7f83 2019-07-27 stsp {
9079 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
9080 bfce7f83 2019-07-27 stsp
9081 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
9082 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
9083 bfce7f83 2019-07-27 stsp free(hle);
9084 bfce7f83 2019-07-27 stsp }
9085 bfce7f83 2019-07-27 stsp }
9086 bfce7f83 2019-07-27 stsp
9087 0ebf8283 2019-07-24 stsp static const struct got_error *
9088 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
9089 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
9090 0ebf8283 2019-07-24 stsp {
9091 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9092 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9093 0ebf8283 2019-07-24 stsp
9094 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
9095 0ebf8283 2019-07-24 stsp if (f == NULL) {
9096 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
9097 0ebf8283 2019-07-24 stsp goto done;
9098 0ebf8283 2019-07-24 stsp }
9099 0ebf8283 2019-07-24 stsp
9100 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9101 0ebf8283 2019-07-24 stsp done:
9102 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9103 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9104 0ebf8283 2019-07-24 stsp return err;
9105 0ebf8283 2019-07-24 stsp }
9106 0ebf8283 2019-07-24 stsp
9107 0ebf8283 2019-07-24 stsp static const struct got_error *
9108 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
9109 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
9110 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
9111 0ebf8283 2019-07-24 stsp {
9112 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
9113 0ebf8283 2019-07-24 stsp int resp = ' ';
9114 0ebf8283 2019-07-24 stsp
9115 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
9116 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
9117 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
9118 0ebf8283 2019-07-24 stsp resp = getchar();
9119 a61a4414 2019-08-07 stsp if (resp == '\n')
9120 a61a4414 2019-08-07 stsp resp = getchar();
9121 426ebf2e 2019-07-25 stsp if (resp == 'c') {
9122 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
9123 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
9124 bfce7f83 2019-07-27 stsp repo);
9125 426ebf2e 2019-07-25 stsp if (err) {
9126 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9127 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9128 426ebf2e 2019-07-25 stsp break;
9129 bfce7f83 2019-07-27 stsp prev_err = err;
9130 426ebf2e 2019-07-25 stsp resp = ' ';
9131 426ebf2e 2019-07-25 stsp continue;
9132 426ebf2e 2019-07-25 stsp }
9133 bfce7f83 2019-07-27 stsp break;
9134 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
9135 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
9136 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
9137 466785b9 2020-12-10 jrick commits, branch_name, 0, 0, repo);
9138 426ebf2e 2019-07-25 stsp if (err) {
9139 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9140 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9141 426ebf2e 2019-07-25 stsp break;
9142 bfce7f83 2019-07-27 stsp prev_err = err;
9143 426ebf2e 2019-07-25 stsp resp = ' ';
9144 426ebf2e 2019-07-25 stsp continue;
9145 426ebf2e 2019-07-25 stsp }
9146 bfce7f83 2019-07-27 stsp break;
9147 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
9148 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
9149 0ebf8283 2019-07-24 stsp break;
9150 426ebf2e 2019-07-25 stsp } else
9151 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
9152 0ebf8283 2019-07-24 stsp }
9153 0ebf8283 2019-07-24 stsp
9154 0ebf8283 2019-07-24 stsp return err;
9155 0ebf8283 2019-07-24 stsp }
9156 0ebf8283 2019-07-24 stsp
9157 0ebf8283 2019-07-24 stsp static const struct got_error *
9158 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
9159 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
9160 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
9161 0ebf8283 2019-07-24 stsp {
9162 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
9163 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
9164 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
9165 3e3a69f1 2019-07-25 stsp branch, repo);
9166 0ebf8283 2019-07-24 stsp }
9167 0ebf8283 2019-07-24 stsp
9168 0ebf8283 2019-07-24 stsp static const struct got_error *
9169 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
9170 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
9171 0ebf8283 2019-07-24 stsp {
9172 0ebf8283 2019-07-24 stsp const struct got_error *err;
9173 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9174 0ebf8283 2019-07-24 stsp
9175 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
9176 0ebf8283 2019-07-24 stsp if (err)
9177 0ebf8283 2019-07-24 stsp goto done;
9178 0ebf8283 2019-07-24 stsp
9179 0ebf8283 2019-07-24 stsp if (new_id) {
9180 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
9181 0ebf8283 2019-07-24 stsp if (err)
9182 0ebf8283 2019-07-24 stsp goto done;
9183 0ebf8283 2019-07-24 stsp }
9184 0ebf8283 2019-07-24 stsp
9185 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
9186 0ebf8283 2019-07-24 stsp if (new_id_str)
9187 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
9188 0ebf8283 2019-07-24 stsp
9189 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
9190 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
9191 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
9192 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
9193 0ebf8283 2019-07-24 stsp goto done;
9194 0ebf8283 2019-07-24 stsp }
9195 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
9196 0ebf8283 2019-07-24 stsp } else {
9197 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
9198 0ebf8283 2019-07-24 stsp if (err)
9199 0ebf8283 2019-07-24 stsp goto done;
9200 0ebf8283 2019-07-24 stsp }
9201 0ebf8283 2019-07-24 stsp
9202 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
9203 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
9204 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
9205 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
9206 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
9207 0ebf8283 2019-07-24 stsp break;
9208 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
9209 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
9210 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
9211 0ebf8283 2019-07-24 stsp logmsg);
9212 0ebf8283 2019-07-24 stsp break;
9213 0ebf8283 2019-07-24 stsp default:
9214 0ebf8283 2019-07-24 stsp break;
9215 0ebf8283 2019-07-24 stsp }
9216 0ebf8283 2019-07-24 stsp done:
9217 0ebf8283 2019-07-24 stsp free(old_id_str);
9218 0ebf8283 2019-07-24 stsp free(new_id_str);
9219 0ebf8283 2019-07-24 stsp return err;
9220 0ebf8283 2019-07-24 stsp }
9221 0ebf8283 2019-07-24 stsp
9222 0ebf8283 2019-07-24 stsp static const struct got_error *
9223 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
9224 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
9225 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
9226 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
9227 0ebf8283 2019-07-24 stsp {
9228 0ebf8283 2019-07-24 stsp const struct got_error *err;
9229 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
9230 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
9231 0ebf8283 2019-07-24 stsp
9232 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
9233 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
9234 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
9235 0ebf8283 2019-07-24 stsp if (err)
9236 0ebf8283 2019-07-24 stsp return err;
9237 0ebf8283 2019-07-24 stsp }
9238 0ebf8283 2019-07-24 stsp
9239 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9240 0ebf8283 2019-07-24 stsp if (err)
9241 0ebf8283 2019-07-24 stsp return err;
9242 0ebf8283 2019-07-24 stsp
9243 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
9244 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
9245 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
9246 0ebf8283 2019-07-24 stsp if (err) {
9247 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
9248 0ebf8283 2019-07-24 stsp goto done;
9249 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
9250 0ebf8283 2019-07-24 stsp } else {
9251 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
9252 0ebf8283 2019-07-24 stsp free(new_commit_id);
9253 0ebf8283 2019-07-24 stsp }
9254 0ebf8283 2019-07-24 stsp done:
9255 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9256 0ebf8283 2019-07-24 stsp return err;
9257 0ebf8283 2019-07-24 stsp }
9258 0ebf8283 2019-07-24 stsp
9259 0ebf8283 2019-07-24 stsp static const struct got_error *
9260 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
9261 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
9262 0ebf8283 2019-07-24 stsp {
9263 0ebf8283 2019-07-24 stsp const struct got_error *error;
9264 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
9265 0ebf8283 2019-07-24 stsp
9266 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
9267 0ebf8283 2019-07-24 stsp repo);
9268 0ebf8283 2019-07-24 stsp if (error)
9269 0ebf8283 2019-07-24 stsp return error;
9270 0ebf8283 2019-07-24 stsp
9271 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
9272 0ebf8283 2019-07-24 stsp if (error)
9273 0ebf8283 2019-07-24 stsp return error;
9274 0ebf8283 2019-07-24 stsp
9275 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
9276 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9277 0ebf8283 2019-07-24 stsp return error;
9278 ab20a43a 2020-01-29 stsp }
9279 ab20a43a 2020-01-29 stsp
9280 ab20a43a 2020-01-29 stsp static const struct got_error *
9281 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
9282 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
9283 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9284 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9285 ab20a43a 2020-01-29 stsp {
9286 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
9287 ab20a43a 2020-01-29 stsp
9288 ab20a43a 2020-01-29 stsp switch (status) {
9289 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
9290 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
9291 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
9292 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
9293 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
9294 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
9295 ab20a43a 2020-01-29 stsp default:
9296 ab20a43a 2020-01-29 stsp break;
9297 ab20a43a 2020-01-29 stsp }
9298 ab20a43a 2020-01-29 stsp
9299 ab20a43a 2020-01-29 stsp switch (staged_status) {
9300 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
9301 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
9302 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
9303 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
9304 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
9305 ab20a43a 2020-01-29 stsp default:
9306 ab20a43a 2020-01-29 stsp break;
9307 ab20a43a 2020-01-29 stsp }
9308 ab20a43a 2020-01-29 stsp
9309 ab20a43a 2020-01-29 stsp return NULL;
9310 0ebf8283 2019-07-24 stsp }
9311 0ebf8283 2019-07-24 stsp
9312 0ebf8283 2019-07-24 stsp static const struct got_error *
9313 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
9314 0ebf8283 2019-07-24 stsp {
9315 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
9316 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
9317 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
9318 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
9319 0ebf8283 2019-07-24 stsp char *cwd = NULL;
9320 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
9321 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
9322 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
9323 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
9324 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
9325 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9326 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
9327 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9328 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
9329 466785b9 2020-12-10 jrick int edit_logmsg_only = 0, fold_only = 0;
9330 643b85bc 2021-07-16 stsp int list_backups = 0, delete_backups = 0;
9331 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
9332 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
9333 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
9334 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
9335 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
9336 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
9337 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
9338 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
9339 0ebf8283 2019-07-24 stsp
9340 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
9341 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
9342 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
9343 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9344 0ebf8283 2019-07-24 stsp
9345 643b85bc 2021-07-16 stsp while ((ch = getopt(argc, argv, "acfF:mlX")) != -1) {
9346 0ebf8283 2019-07-24 stsp switch (ch) {
9347 0ebf8283 2019-07-24 stsp case 'a':
9348 0ebf8283 2019-07-24 stsp abort_edit = 1;
9349 0ebf8283 2019-07-24 stsp break;
9350 0ebf8283 2019-07-24 stsp case 'c':
9351 0ebf8283 2019-07-24 stsp continue_edit = 1;
9352 0ebf8283 2019-07-24 stsp break;
9353 466785b9 2020-12-10 jrick case 'f':
9354 466785b9 2020-12-10 jrick fold_only = 1;
9355 466785b9 2020-12-10 jrick break;
9356 0ebf8283 2019-07-24 stsp case 'F':
9357 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
9358 0ebf8283 2019-07-24 stsp break;
9359 083957f4 2020-02-24 stsp case 'm':
9360 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
9361 083957f4 2020-02-24 stsp break;
9362 e600f124 2021-03-21 stsp case 'l':
9363 e600f124 2021-03-21 stsp list_backups = 1;
9364 e600f124 2021-03-21 stsp break;
9365 643b85bc 2021-07-16 stsp case 'X':
9366 643b85bc 2021-07-16 stsp delete_backups = 1;
9367 643b85bc 2021-07-16 stsp break;
9368 0ebf8283 2019-07-24 stsp default:
9369 0ebf8283 2019-07-24 stsp usage_histedit();
9370 0ebf8283 2019-07-24 stsp /* NOTREACHED */
9371 0ebf8283 2019-07-24 stsp }
9372 0ebf8283 2019-07-24 stsp }
9373 0ebf8283 2019-07-24 stsp
9374 0ebf8283 2019-07-24 stsp argc -= optind;
9375 0ebf8283 2019-07-24 stsp argv += optind;
9376 0ebf8283 2019-07-24 stsp
9377 0ebf8283 2019-07-24 stsp #ifndef PROFILE
9378 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9379 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
9380 0ebf8283 2019-07-24 stsp err(1, "pledge");
9381 0ebf8283 2019-07-24 stsp #endif
9382 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
9383 ff69268e 2020-12-13 stsp option_conflict('a', 'c');
9384 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
9385 ff69268e 2020-12-13 stsp option_conflict('F', 'm');
9386 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
9387 ff69268e 2020-12-13 stsp option_conflict('a', 'm');
9388 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
9389 ff69268e 2020-12-13 stsp option_conflict('c', 'm');
9390 466785b9 2020-12-10 jrick if (abort_edit && fold_only)
9391 ff69268e 2020-12-13 stsp option_conflict('a', 'f');
9392 ff69268e 2020-12-13 stsp if (continue_edit && fold_only)
9393 ff69268e 2020-12-13 stsp option_conflict('c', 'f');
9394 466785b9 2020-12-10 jrick if (fold_only && edit_logmsg_only)
9395 ff69268e 2020-12-13 stsp option_conflict('f', 'm');
9396 b3805337 2020-12-13 stsp if (edit_script_path && fold_only)
9397 b3805337 2020-12-13 stsp option_conflict('F', 'f');
9398 e600f124 2021-03-21 stsp if (list_backups) {
9399 e600f124 2021-03-21 stsp if (abort_edit)
9400 e600f124 2021-03-21 stsp option_conflict('l', 'a');
9401 e600f124 2021-03-21 stsp if (continue_edit)
9402 e600f124 2021-03-21 stsp option_conflict('l', 'c');
9403 e600f124 2021-03-21 stsp if (edit_script_path)
9404 e600f124 2021-03-21 stsp option_conflict('l', 'F');
9405 e600f124 2021-03-21 stsp if (edit_logmsg_only)
9406 e600f124 2021-03-21 stsp option_conflict('l', 'm');
9407 e600f124 2021-03-21 stsp if (fold_only)
9408 e600f124 2021-03-21 stsp option_conflict('l', 'f');
9409 643b85bc 2021-07-16 stsp if (delete_backups)
9410 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
9411 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
9412 e600f124 2021-03-21 stsp usage_histedit();
9413 643b85bc 2021-07-16 stsp } else if (delete_backups) {
9414 643b85bc 2021-07-16 stsp if (abort_edit)
9415 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
9416 643b85bc 2021-07-16 stsp if (continue_edit)
9417 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
9418 643b85bc 2021-07-16 stsp if (edit_script_path)
9419 643b85bc 2021-07-16 stsp option_conflict('X', 'F');
9420 643b85bc 2021-07-16 stsp if (edit_logmsg_only)
9421 643b85bc 2021-07-16 stsp option_conflict('X', 'm');
9422 643b85bc 2021-07-16 stsp if (fold_only)
9423 643b85bc 2021-07-16 stsp option_conflict('X', 'f');
9424 643b85bc 2021-07-16 stsp if (list_backups)
9425 643b85bc 2021-07-16 stsp option_conflict('X', 'l');
9426 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
9427 643b85bc 2021-07-16 stsp usage_histedit();
9428 e600f124 2021-03-21 stsp } else if (argc != 0)
9429 0ebf8283 2019-07-24 stsp usage_histedit();
9430 0ebf8283 2019-07-24 stsp
9431 0ebf8283 2019-07-24 stsp /*
9432 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
9433 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
9434 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
9435 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
9436 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
9437 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
9438 0ebf8283 2019-07-24 stsp */
9439 0ebf8283 2019-07-24 stsp
9440 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
9441 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
9442 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
9443 0ebf8283 2019-07-24 stsp goto done;
9444 0ebf8283 2019-07-24 stsp }
9445 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
9446 fa51e947 2020-03-27 stsp if (error) {
9447 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
9448 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
9449 e600f124 2021-03-21 stsp goto done;
9450 e600f124 2021-03-21 stsp } else {
9451 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9452 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
9453 e600f124 2021-03-21 stsp "histedit", cwd);
9454 e600f124 2021-03-21 stsp goto done;
9455 e600f124 2021-03-21 stsp }
9456 e600f124 2021-03-21 stsp }
9457 e600f124 2021-03-21 stsp
9458 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
9459 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
9460 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd,
9461 e600f124 2021-03-21 stsp NULL);
9462 e600f124 2021-03-21 stsp if (error != NULL)
9463 e600f124 2021-03-21 stsp goto done;
9464 e600f124 2021-03-21 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9465 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
9466 e600f124 2021-03-21 stsp if (error)
9467 e600f124 2021-03-21 stsp goto done;
9468 643b85bc 2021-07-16 stsp error = process_backup_refs(
9469 e600f124 2021-03-21 stsp GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
9470 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
9471 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
9472 fa51e947 2020-03-27 stsp }
9473 0ebf8283 2019-07-24 stsp
9474 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9475 c9956ddf 2019-09-08 stsp NULL);
9476 0ebf8283 2019-07-24 stsp if (error != NULL)
9477 0ebf8283 2019-07-24 stsp goto done;
9478 0ebf8283 2019-07-24 stsp
9479 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9480 0ebf8283 2019-07-24 stsp if (error)
9481 0ebf8283 2019-07-24 stsp goto done;
9482 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
9483 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
9484 0ebf8283 2019-07-24 stsp goto done;
9485 0ebf8283 2019-07-24 stsp }
9486 0ebf8283 2019-07-24 stsp
9487 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
9488 0ebf8283 2019-07-24 stsp if (error)
9489 0ebf8283 2019-07-24 stsp goto done;
9490 0ebf8283 2019-07-24 stsp
9491 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
9492 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
9493 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
9494 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
9495 083957f4 2020-02-24 stsp "before the -m option can be used");
9496 083957f4 2020-02-24 stsp goto done;
9497 083957f4 2020-02-24 stsp }
9498 466785b9 2020-12-10 jrick if (edit_in_progress && fold_only) {
9499 466785b9 2020-12-10 jrick error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
9500 466785b9 2020-12-10 jrick "histedit operation is in progress in this "
9501 466785b9 2020-12-10 jrick "work tree and must be continued or aborted "
9502 466785b9 2020-12-10 jrick "before the -f option can be used");
9503 466785b9 2020-12-10 jrick goto done;
9504 466785b9 2020-12-10 jrick }
9505 083957f4 2020-02-24 stsp
9506 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
9507 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
9508 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
9509 3e3a69f1 2019-07-25 stsp worktree, repo);
9510 0ebf8283 2019-07-24 stsp if (error)
9511 0ebf8283 2019-07-24 stsp goto done;
9512 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
9513 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
9514 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
9515 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
9516 0ebf8283 2019-07-24 stsp if (error)
9517 0ebf8283 2019-07-24 stsp goto done;
9518 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
9519 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
9520 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9521 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
9522 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
9523 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
9524 0ebf8283 2019-07-24 stsp goto done;
9525 0ebf8283 2019-07-24 stsp }
9526 0ebf8283 2019-07-24 stsp
9527 0ebf8283 2019-07-24 stsp if (continue_edit) {
9528 0ebf8283 2019-07-24 stsp char *path;
9529 0ebf8283 2019-07-24 stsp
9530 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
9531 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
9532 0ebf8283 2019-07-24 stsp goto done;
9533 0ebf8283 2019-07-24 stsp }
9534 0ebf8283 2019-07-24 stsp
9535 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
9536 0ebf8283 2019-07-24 stsp if (error)
9537 0ebf8283 2019-07-24 stsp goto done;
9538 0ebf8283 2019-07-24 stsp
9539 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
9540 0ebf8283 2019-07-24 stsp free(path);
9541 0ebf8283 2019-07-24 stsp if (error)
9542 0ebf8283 2019-07-24 stsp goto done;
9543 0ebf8283 2019-07-24 stsp
9544 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
9545 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
9546 3e3a69f1 2019-07-25 stsp worktree, repo);
9547 0ebf8283 2019-07-24 stsp if (error)
9548 0ebf8283 2019-07-24 stsp goto done;
9549 0ebf8283 2019-07-24 stsp
9550 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
9551 0ebf8283 2019-07-24 stsp if (error)
9552 0ebf8283 2019-07-24 stsp goto done;
9553 0ebf8283 2019-07-24 stsp
9554 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
9555 0ebf8283 2019-07-24 stsp head_commit_id);
9556 0ebf8283 2019-07-24 stsp if (error)
9557 0ebf8283 2019-07-24 stsp goto done;
9558 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9559 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
9560 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
9561 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9562 3aac7cf7 2019-07-25 stsp goto done;
9563 3aac7cf7 2019-07-25 stsp }
9564 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
9565 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
9566 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
9567 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9568 0ebf8283 2019-07-24 stsp commit = NULL;
9569 0ebf8283 2019-07-24 stsp if (error)
9570 0ebf8283 2019-07-24 stsp goto done;
9571 0ebf8283 2019-07-24 stsp } else {
9572 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
9573 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
9574 0ebf8283 2019-07-24 stsp goto done;
9575 0ebf8283 2019-07-24 stsp }
9576 0ebf8283 2019-07-24 stsp
9577 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
9578 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
9579 0ebf8283 2019-07-24 stsp if (error != NULL)
9580 0ebf8283 2019-07-24 stsp goto done;
9581 0ebf8283 2019-07-24 stsp
9582 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
9583 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
9584 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
9585 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
9586 c7d20a3f 2019-07-30 stsp goto done;
9587 c7d20a3f 2019-07-30 stsp }
9588 c7d20a3f 2019-07-30 stsp
9589 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
9590 bfce7f83 2019-07-27 stsp got_ref_close(branch);
9591 bfce7f83 2019-07-27 stsp branch = NULL;
9592 0ebf8283 2019-07-24 stsp if (error)
9593 0ebf8283 2019-07-24 stsp goto done;
9594 0ebf8283 2019-07-24 stsp
9595 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
9596 0ebf8283 2019-07-24 stsp head_commit_id);
9597 0ebf8283 2019-07-24 stsp if (error)
9598 0ebf8283 2019-07-24 stsp goto done;
9599 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9600 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
9601 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
9602 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9603 3aac7cf7 2019-07-25 stsp goto done;
9604 3aac7cf7 2019-07-25 stsp }
9605 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
9606 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
9607 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
9608 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
9609 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9610 0ebf8283 2019-07-24 stsp commit = NULL;
9611 0ebf8283 2019-07-24 stsp if (error)
9612 0ebf8283 2019-07-24 stsp goto done;
9613 0ebf8283 2019-07-24 stsp
9614 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
9615 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9616 c5996fff 2020-01-29 stsp goto done;
9617 c5996fff 2020-01-29 stsp }
9618 c5996fff 2020-01-29 stsp
9619 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
9620 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
9621 bfce7f83 2019-07-27 stsp if (error)
9622 bfce7f83 2019-07-27 stsp goto done;
9623 bfce7f83 2019-07-27 stsp
9624 0ebf8283 2019-07-24 stsp if (edit_script_path) {
9625 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
9626 0ebf8283 2019-07-24 stsp edit_script_path, repo);
9627 6ba2bea2 2019-07-27 stsp if (error) {
9628 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
9629 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
9630 9627c110 2020-04-18 stsp update_progress, &upa);
9631 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9632 0ebf8283 2019-07-24 stsp goto done;
9633 6ba2bea2 2019-07-27 stsp }
9634 0ebf8283 2019-07-24 stsp } else {
9635 514f2ffe 2020-01-29 stsp const char *branch_name;
9636 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
9637 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
9638 514f2ffe 2020-01-29 stsp branch_name += 11;
9639 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
9640 466785b9 2020-12-10 jrick branch_name, edit_logmsg_only, fold_only, repo);
9641 6ba2bea2 2019-07-27 stsp if (error) {
9642 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
9643 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
9644 9627c110 2020-04-18 stsp update_progress, &upa);
9645 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9646 0ebf8283 2019-07-24 stsp goto done;
9647 6ba2bea2 2019-07-27 stsp }
9648 0ebf8283 2019-07-24 stsp
9649 0ebf8283 2019-07-24 stsp }
9650 0ebf8283 2019-07-24 stsp
9651 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
9652 0ebf8283 2019-07-24 stsp repo);
9653 6ba2bea2 2019-07-27 stsp if (error) {
9654 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
9655 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
9656 9627c110 2020-04-18 stsp update_progress, &upa);
9657 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9658 0ebf8283 2019-07-24 stsp goto done;
9659 6ba2bea2 2019-07-27 stsp }
9660 0ebf8283 2019-07-24 stsp
9661 0ebf8283 2019-07-24 stsp }
9662 0ebf8283 2019-07-24 stsp
9663 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
9664 4ec14e60 2019-08-28 hiltjo if (error)
9665 0ebf8283 2019-07-24 stsp goto done;
9666 0ebf8283 2019-07-24 stsp
9667 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
9668 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
9669 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
9670 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
9671 0ebf8283 2019-07-24 stsp continue;
9672 0ebf8283 2019-07-24 stsp
9673 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
9674 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
9675 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
9676 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
9677 62d463ca 2020-10-20 naddy repo);
9678 ab20a43a 2020-01-29 stsp if (error)
9679 ab20a43a 2020-01-29 stsp goto done;
9680 0ebf8283 2019-07-24 stsp } else {
9681 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
9682 ab20a43a 2020-01-29 stsp int have_changes = 0;
9683 ab20a43a 2020-01-29 stsp
9684 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
9685 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
9686 ab20a43a 2020-01-29 stsp if (error)
9687 ab20a43a 2020-01-29 stsp goto done;
9688 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
9689 f6343036 2021-06-22 stsp repo, 0, check_local_changes, &have_changes,
9690 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
9691 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
9692 ab20a43a 2020-01-29 stsp if (error) {
9693 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
9694 ab20a43a 2020-01-29 stsp goto done;
9695 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
9696 ab20a43a 2020-01-29 stsp goto done;
9697 ab20a43a 2020-01-29 stsp }
9698 ab20a43a 2020-01-29 stsp if (have_changes) {
9699 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
9700 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
9701 ab20a43a 2020-01-29 stsp if (error)
9702 ab20a43a 2020-01-29 stsp goto done;
9703 ab20a43a 2020-01-29 stsp } else {
9704 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
9705 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
9706 ab20a43a 2020-01-29 stsp if (error)
9707 ab20a43a 2020-01-29 stsp goto done;
9708 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
9709 ab20a43a 2020-01-29 stsp hle, NULL);
9710 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
9711 ab20a43a 2020-01-29 stsp commit = NULL;
9712 ab20a43a 2020-01-29 stsp if (error)
9713 ab20a43a 2020-01-29 stsp goto done;
9714 ab20a43a 2020-01-29 stsp }
9715 0ebf8283 2019-07-24 stsp }
9716 0ebf8283 2019-07-24 stsp continue;
9717 0ebf8283 2019-07-24 stsp }
9718 0ebf8283 2019-07-24 stsp
9719 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
9720 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
9721 0ebf8283 2019-07-24 stsp if (error)
9722 0ebf8283 2019-07-24 stsp goto done;
9723 0ebf8283 2019-07-24 stsp continue;
9724 0ebf8283 2019-07-24 stsp }
9725 0ebf8283 2019-07-24 stsp
9726 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
9727 0ebf8283 2019-07-24 stsp hle->commit_id);
9728 0ebf8283 2019-07-24 stsp if (error)
9729 0ebf8283 2019-07-24 stsp goto done;
9730 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9731 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
9732 0ebf8283 2019-07-24 stsp
9733 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
9734 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
9735 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
9736 0ebf8283 2019-07-24 stsp if (error)
9737 0ebf8283 2019-07-24 stsp goto done;
9738 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9739 0ebf8283 2019-07-24 stsp commit = NULL;
9740 0ebf8283 2019-07-24 stsp
9741 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9742 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
9743 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
9744 9627c110 2020-04-18 stsp
9745 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
9746 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
9747 a0ea4fc0 2020-02-28 stsp repo);
9748 a0ea4fc0 2020-02-28 stsp if (error)
9749 a0ea4fc0 2020-02-28 stsp goto done;
9750 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9751 0ebf8283 2019-07-24 stsp break;
9752 0ebf8283 2019-07-24 stsp }
9753 0ebf8283 2019-07-24 stsp
9754 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
9755 0ebf8283 2019-07-24 stsp char *id_str;
9756 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
9757 0ebf8283 2019-07-24 stsp if (error)
9758 0ebf8283 2019-07-24 stsp goto done;
9759 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
9760 0ebf8283 2019-07-24 stsp id_str);
9761 0ebf8283 2019-07-24 stsp free(id_str);
9762 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9763 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
9764 3e3a69f1 2019-07-25 stsp fileindex);
9765 0ebf8283 2019-07-24 stsp goto done;
9766 0ebf8283 2019-07-24 stsp }
9767 0ebf8283 2019-07-24 stsp
9768 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
9769 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
9770 0ebf8283 2019-07-24 stsp if (error)
9771 0ebf8283 2019-07-24 stsp goto done;
9772 0ebf8283 2019-07-24 stsp continue;
9773 0ebf8283 2019-07-24 stsp }
9774 0ebf8283 2019-07-24 stsp
9775 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
9776 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
9777 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9778 0ebf8283 2019-07-24 stsp if (error)
9779 0ebf8283 2019-07-24 stsp goto done;
9780 0ebf8283 2019-07-24 stsp }
9781 0ebf8283 2019-07-24 stsp
9782 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
9783 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
9784 0ebf8283 2019-07-24 stsp if (error)
9785 0ebf8283 2019-07-24 stsp goto done;
9786 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
9787 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
9788 0ebf8283 2019-07-24 stsp } else
9789 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
9790 3e3a69f1 2019-07-25 stsp branch, repo);
9791 0ebf8283 2019-07-24 stsp done:
9792 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
9793 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
9794 0ebf8283 2019-07-24 stsp free(head_commit_id);
9795 0ebf8283 2019-07-24 stsp free(base_commit_id);
9796 0ebf8283 2019-07-24 stsp free(resume_commit_id);
9797 0ebf8283 2019-07-24 stsp if (commit)
9798 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9799 0ebf8283 2019-07-24 stsp if (branch)
9800 0ebf8283 2019-07-24 stsp got_ref_close(branch);
9801 0ebf8283 2019-07-24 stsp if (tmp_branch)
9802 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
9803 0ebf8283 2019-07-24 stsp if (worktree)
9804 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
9805 1d0f4054 2021-06-17 stsp if (repo) {
9806 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
9807 1d0f4054 2021-06-17 stsp if (error == NULL)
9808 1d0f4054 2021-06-17 stsp error = close_err;
9809 1d0f4054 2021-06-17 stsp }
9810 2822a352 2019-10-15 stsp return error;
9811 2822a352 2019-10-15 stsp }
9812 2822a352 2019-10-15 stsp
9813 2822a352 2019-10-15 stsp __dead static void
9814 2822a352 2019-10-15 stsp usage_integrate(void)
9815 2822a352 2019-10-15 stsp {
9816 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
9817 2822a352 2019-10-15 stsp exit(1);
9818 2822a352 2019-10-15 stsp }
9819 2822a352 2019-10-15 stsp
9820 2822a352 2019-10-15 stsp static const struct got_error *
9821 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
9822 2822a352 2019-10-15 stsp {
9823 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
9824 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
9825 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
9826 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
9827 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
9828 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
9829 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
9830 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
9831 9627c110 2020-04-18 stsp int ch;
9832 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9833 2822a352 2019-10-15 stsp
9834 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9835 2822a352 2019-10-15 stsp switch (ch) {
9836 2822a352 2019-10-15 stsp default:
9837 2822a352 2019-10-15 stsp usage_integrate();
9838 2822a352 2019-10-15 stsp /* NOTREACHED */
9839 2822a352 2019-10-15 stsp }
9840 2822a352 2019-10-15 stsp }
9841 2822a352 2019-10-15 stsp
9842 2822a352 2019-10-15 stsp argc -= optind;
9843 2822a352 2019-10-15 stsp argv += optind;
9844 2822a352 2019-10-15 stsp
9845 2822a352 2019-10-15 stsp if (argc != 1)
9846 2822a352 2019-10-15 stsp usage_integrate();
9847 2822a352 2019-10-15 stsp branch_arg = argv[0];
9848 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
9849 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9850 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
9851 2822a352 2019-10-15 stsp err(1, "pledge");
9852 b08a0ccd 2020-09-24 stsp #endif
9853 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
9854 2822a352 2019-10-15 stsp if (cwd == NULL) {
9855 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
9856 2822a352 2019-10-15 stsp goto done;
9857 2822a352 2019-10-15 stsp }
9858 2822a352 2019-10-15 stsp
9859 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
9860 fa51e947 2020-03-27 stsp if (error) {
9861 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9862 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
9863 fa51e947 2020-03-27 stsp cwd);
9864 2822a352 2019-10-15 stsp goto done;
9865 fa51e947 2020-03-27 stsp }
9866 2822a352 2019-10-15 stsp
9867 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
9868 2822a352 2019-10-15 stsp if (error)
9869 2822a352 2019-10-15 stsp goto done;
9870 2822a352 2019-10-15 stsp
9871 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9872 2822a352 2019-10-15 stsp NULL);
9873 2822a352 2019-10-15 stsp if (error != NULL)
9874 2822a352 2019-10-15 stsp goto done;
9875 2822a352 2019-10-15 stsp
9876 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9877 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
9878 2822a352 2019-10-15 stsp if (error)
9879 2822a352 2019-10-15 stsp goto done;
9880 2822a352 2019-10-15 stsp
9881 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
9882 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
9883 2822a352 2019-10-15 stsp goto done;
9884 2822a352 2019-10-15 stsp }
9885 2822a352 2019-10-15 stsp
9886 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
9887 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
9888 2822a352 2019-10-15 stsp if (error)
9889 2822a352 2019-10-15 stsp goto done;
9890 2822a352 2019-10-15 stsp
9891 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
9892 2822a352 2019-10-15 stsp if (refname == NULL) {
9893 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9894 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9895 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9896 2822a352 2019-10-15 stsp goto done;
9897 2822a352 2019-10-15 stsp }
9898 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
9899 2822a352 2019-10-15 stsp if (base_refname == NULL) {
9900 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9901 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9902 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9903 2822a352 2019-10-15 stsp goto done;
9904 2822a352 2019-10-15 stsp }
9905 2822a352 2019-10-15 stsp
9906 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
9907 2822a352 2019-10-15 stsp if (error)
9908 2822a352 2019-10-15 stsp goto done;
9909 2822a352 2019-10-15 stsp
9910 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
9911 2822a352 2019-10-15 stsp if (error)
9912 2822a352 2019-10-15 stsp goto done;
9913 2822a352 2019-10-15 stsp
9914 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
9915 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
9916 2822a352 2019-10-15 stsp "specified branch has already been integrated");
9917 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9918 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9919 2822a352 2019-10-15 stsp goto done;
9920 2822a352 2019-10-15 stsp }
9921 2822a352 2019-10-15 stsp
9922 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
9923 2822a352 2019-10-15 stsp if (error) {
9924 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
9925 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
9926 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9927 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9928 2822a352 2019-10-15 stsp goto done;
9929 2822a352 2019-10-15 stsp }
9930 2822a352 2019-10-15 stsp
9931 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9932 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
9933 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
9934 2822a352 2019-10-15 stsp check_cancelled, NULL);
9935 2822a352 2019-10-15 stsp if (error)
9936 2822a352 2019-10-15 stsp goto done;
9937 2822a352 2019-10-15 stsp
9938 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
9939 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9940 2822a352 2019-10-15 stsp done:
9941 1d0f4054 2021-06-17 stsp if (repo) {
9942 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
9943 1d0f4054 2021-06-17 stsp if (error == NULL)
9944 1d0f4054 2021-06-17 stsp error = close_err;
9945 1d0f4054 2021-06-17 stsp }
9946 2822a352 2019-10-15 stsp if (worktree)
9947 2822a352 2019-10-15 stsp got_worktree_close(worktree);
9948 2822a352 2019-10-15 stsp free(cwd);
9949 2822a352 2019-10-15 stsp free(base_commit_id);
9950 2822a352 2019-10-15 stsp free(commit_id);
9951 2822a352 2019-10-15 stsp free(refname);
9952 2822a352 2019-10-15 stsp free(base_refname);
9953 715dc77e 2019-08-03 stsp return error;
9954 715dc77e 2019-08-03 stsp }
9955 715dc77e 2019-08-03 stsp
9956 715dc77e 2019-08-03 stsp __dead static void
9957 715dc77e 2019-08-03 stsp usage_stage(void)
9958 715dc77e 2019-08-03 stsp {
9959 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
9960 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
9961 715dc77e 2019-08-03 stsp getprogname());
9962 715dc77e 2019-08-03 stsp exit(1);
9963 715dc77e 2019-08-03 stsp }
9964 715dc77e 2019-08-03 stsp
9965 715dc77e 2019-08-03 stsp static const struct got_error *
9966 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
9967 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
9968 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
9969 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
9970 408b4ebc 2019-08-03 stsp {
9971 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
9972 408b4ebc 2019-08-03 stsp char *id_str = NULL;
9973 408b4ebc 2019-08-03 stsp
9974 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
9975 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
9976 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
9977 408b4ebc 2019-08-03 stsp return NULL;
9978 408b4ebc 2019-08-03 stsp
9979 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
9980 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
9981 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
9982 408b4ebc 2019-08-03 stsp else
9983 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
9984 408b4ebc 2019-08-03 stsp if (err)
9985 408b4ebc 2019-08-03 stsp return err;
9986 408b4ebc 2019-08-03 stsp
9987 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
9988 408b4ebc 2019-08-03 stsp free(id_str);
9989 dc424a06 2019-08-07 stsp return NULL;
9990 dc424a06 2019-08-07 stsp }
9991 2e1f37b0 2019-08-08 stsp
9992 dc424a06 2019-08-07 stsp static const struct got_error *
9993 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
9994 715dc77e 2019-08-03 stsp {
9995 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
9996 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
9997 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
9998 715dc77e 2019-08-03 stsp char *cwd = NULL;
9999 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
10000 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
10001 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
10002 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
10003 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
10004 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
10005 715dc77e 2019-08-03 stsp
10006 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
10007 715dc77e 2019-08-03 stsp
10008 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
10009 715dc77e 2019-08-03 stsp switch (ch) {
10010 408b4ebc 2019-08-03 stsp case 'l':
10011 408b4ebc 2019-08-03 stsp list_stage = 1;
10012 408b4ebc 2019-08-03 stsp break;
10013 dc424a06 2019-08-07 stsp case 'p':
10014 dc424a06 2019-08-07 stsp pflag = 1;
10015 dc424a06 2019-08-07 stsp break;
10016 dc424a06 2019-08-07 stsp case 'F':
10017 dc424a06 2019-08-07 stsp patch_script_path = optarg;
10018 dc424a06 2019-08-07 stsp break;
10019 35213c7c 2020-07-23 stsp case 'S':
10020 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
10021 35213c7c 2020-07-23 stsp break;
10022 715dc77e 2019-08-03 stsp default:
10023 715dc77e 2019-08-03 stsp usage_stage();
10024 715dc77e 2019-08-03 stsp /* NOTREACHED */
10025 715dc77e 2019-08-03 stsp }
10026 715dc77e 2019-08-03 stsp }
10027 715dc77e 2019-08-03 stsp
10028 715dc77e 2019-08-03 stsp argc -= optind;
10029 715dc77e 2019-08-03 stsp argv += optind;
10030 715dc77e 2019-08-03 stsp
10031 715dc77e 2019-08-03 stsp #ifndef PROFILE
10032 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10033 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
10034 715dc77e 2019-08-03 stsp err(1, "pledge");
10035 715dc77e 2019-08-03 stsp #endif
10036 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
10037 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
10038 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
10039 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
10040 715dc77e 2019-08-03 stsp
10041 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
10042 715dc77e 2019-08-03 stsp if (cwd == NULL) {
10043 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
10044 715dc77e 2019-08-03 stsp goto done;
10045 715dc77e 2019-08-03 stsp }
10046 715dc77e 2019-08-03 stsp
10047 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
10048 fa51e947 2020-03-27 stsp if (error) {
10049 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10050 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
10051 715dc77e 2019-08-03 stsp goto done;
10052 fa51e947 2020-03-27 stsp }
10053 715dc77e 2019-08-03 stsp
10054 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10055 c9956ddf 2019-09-08 stsp NULL);
10056 715dc77e 2019-08-03 stsp if (error != NULL)
10057 715dc77e 2019-08-03 stsp goto done;
10058 715dc77e 2019-08-03 stsp
10059 dc424a06 2019-08-07 stsp if (patch_script_path) {
10060 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
10061 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
10062 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
10063 dc424a06 2019-08-07 stsp patch_script_path);
10064 dc424a06 2019-08-07 stsp goto done;
10065 dc424a06 2019-08-07 stsp }
10066 dc424a06 2019-08-07 stsp }
10067 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10068 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
10069 715dc77e 2019-08-03 stsp if (error)
10070 715dc77e 2019-08-03 stsp goto done;
10071 715dc77e 2019-08-03 stsp
10072 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10073 6d022e97 2019-08-04 stsp if (error)
10074 6d022e97 2019-08-04 stsp goto done;
10075 715dc77e 2019-08-03 stsp
10076 6d022e97 2019-08-04 stsp if (list_stage)
10077 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
10078 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
10079 2e1f37b0 2019-08-08 stsp else {
10080 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
10081 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
10082 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
10083 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
10084 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
10085 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
10086 2e1f37b0 2019-08-08 stsp }
10087 ad493afc 2019-08-03 stsp done:
10088 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
10089 2e1f37b0 2019-08-08 stsp error == NULL)
10090 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
10091 1d0f4054 2021-06-17 stsp if (repo) {
10092 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10093 1d0f4054 2021-06-17 stsp if (error == NULL)
10094 1d0f4054 2021-06-17 stsp error = close_err;
10095 1d0f4054 2021-06-17 stsp }
10096 ad493afc 2019-08-03 stsp if (worktree)
10097 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
10098 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
10099 ad493afc 2019-08-03 stsp free((char *)pe->path);
10100 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
10101 ad493afc 2019-08-03 stsp free(cwd);
10102 ad493afc 2019-08-03 stsp return error;
10103 ad493afc 2019-08-03 stsp }
10104 ad493afc 2019-08-03 stsp
10105 ad493afc 2019-08-03 stsp __dead static void
10106 ad493afc 2019-08-03 stsp usage_unstage(void)
10107 ad493afc 2019-08-03 stsp {
10108 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
10109 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
10110 ad493afc 2019-08-03 stsp getprogname());
10111 ad493afc 2019-08-03 stsp exit(1);
10112 ad493afc 2019-08-03 stsp }
10113 ad493afc 2019-08-03 stsp
10114 ad493afc 2019-08-03 stsp
10115 ad493afc 2019-08-03 stsp static const struct got_error *
10116 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
10117 ad493afc 2019-08-03 stsp {
10118 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
10119 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
10120 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
10121 ad493afc 2019-08-03 stsp char *cwd = NULL;
10122 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
10123 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
10124 9627c110 2020-04-18 stsp int ch, pflag = 0;
10125 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10126 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
10127 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
10128 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
10129 ad493afc 2019-08-03 stsp
10130 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
10131 ad493afc 2019-08-03 stsp
10132 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
10133 ad493afc 2019-08-03 stsp switch (ch) {
10134 2e1f37b0 2019-08-08 stsp case 'p':
10135 2e1f37b0 2019-08-08 stsp pflag = 1;
10136 2e1f37b0 2019-08-08 stsp break;
10137 2e1f37b0 2019-08-08 stsp case 'F':
10138 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
10139 2e1f37b0 2019-08-08 stsp break;
10140 ad493afc 2019-08-03 stsp default:
10141 ad493afc 2019-08-03 stsp usage_unstage();
10142 ad493afc 2019-08-03 stsp /* NOTREACHED */
10143 ad493afc 2019-08-03 stsp }
10144 ad493afc 2019-08-03 stsp }
10145 ad493afc 2019-08-03 stsp
10146 ad493afc 2019-08-03 stsp argc -= optind;
10147 ad493afc 2019-08-03 stsp argv += optind;
10148 ad493afc 2019-08-03 stsp
10149 ad493afc 2019-08-03 stsp #ifndef PROFILE
10150 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10151 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
10152 ad493afc 2019-08-03 stsp err(1, "pledge");
10153 ad493afc 2019-08-03 stsp #endif
10154 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
10155 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
10156 2e1f37b0 2019-08-08 stsp
10157 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
10158 ad493afc 2019-08-03 stsp if (cwd == NULL) {
10159 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
10160 ad493afc 2019-08-03 stsp goto done;
10161 ad493afc 2019-08-03 stsp }
10162 ad493afc 2019-08-03 stsp
10163 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
10164 fa51e947 2020-03-27 stsp if (error) {
10165 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10166 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
10167 ad493afc 2019-08-03 stsp goto done;
10168 fa51e947 2020-03-27 stsp }
10169 ad493afc 2019-08-03 stsp
10170 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10171 c9956ddf 2019-09-08 stsp NULL);
10172 ad493afc 2019-08-03 stsp if (error != NULL)
10173 ad493afc 2019-08-03 stsp goto done;
10174 ad493afc 2019-08-03 stsp
10175 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
10176 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
10177 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
10178 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
10179 2e1f37b0 2019-08-08 stsp patch_script_path);
10180 2e1f37b0 2019-08-08 stsp goto done;
10181 2e1f37b0 2019-08-08 stsp }
10182 2e1f37b0 2019-08-08 stsp }
10183 2e1f37b0 2019-08-08 stsp
10184 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10185 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
10186 ad493afc 2019-08-03 stsp if (error)
10187 ad493afc 2019-08-03 stsp goto done;
10188 ad493afc 2019-08-03 stsp
10189 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10190 ad493afc 2019-08-03 stsp if (error)
10191 ad493afc 2019-08-03 stsp goto done;
10192 ad493afc 2019-08-03 stsp
10193 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
10194 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
10195 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10196 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
10197 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
10198 9627c110 2020-04-18 stsp if (!error)
10199 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10200 715dc77e 2019-08-03 stsp done:
10201 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
10202 2e1f37b0 2019-08-08 stsp error == NULL)
10203 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
10204 1d0f4054 2021-06-17 stsp if (repo) {
10205 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10206 1d0f4054 2021-06-17 stsp if (error == NULL)
10207 1d0f4054 2021-06-17 stsp error = close_err;
10208 1d0f4054 2021-06-17 stsp }
10209 715dc77e 2019-08-03 stsp if (worktree)
10210 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
10211 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
10212 715dc77e 2019-08-03 stsp free((char *)pe->path);
10213 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
10214 715dc77e 2019-08-03 stsp free(cwd);
10215 01073a5d 2019-08-22 stsp return error;
10216 01073a5d 2019-08-22 stsp }
10217 01073a5d 2019-08-22 stsp
10218 01073a5d 2019-08-22 stsp __dead static void
10219 01073a5d 2019-08-22 stsp usage_cat(void)
10220 01073a5d 2019-08-22 stsp {
10221 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
10222 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
10223 01073a5d 2019-08-22 stsp exit(1);
10224 01073a5d 2019-08-22 stsp }
10225 01073a5d 2019-08-22 stsp
10226 01073a5d 2019-08-22 stsp static const struct got_error *
10227 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10228 01073a5d 2019-08-22 stsp {
10229 01073a5d 2019-08-22 stsp const struct got_error *err;
10230 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
10231 01073a5d 2019-08-22 stsp
10232 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
10233 01073a5d 2019-08-22 stsp if (err)
10234 01073a5d 2019-08-22 stsp return err;
10235 01073a5d 2019-08-22 stsp
10236 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
10237 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
10238 01073a5d 2019-08-22 stsp return err;
10239 01073a5d 2019-08-22 stsp }
10240 01073a5d 2019-08-22 stsp
10241 01073a5d 2019-08-22 stsp static const struct got_error *
10242 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10243 01073a5d 2019-08-22 stsp {
10244 01073a5d 2019-08-22 stsp const struct got_error *err;
10245 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
10246 56e0773d 2019-11-28 stsp int nentries, i;
10247 01073a5d 2019-08-22 stsp
10248 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
10249 01073a5d 2019-08-22 stsp if (err)
10250 01073a5d 2019-08-22 stsp return err;
10251 01073a5d 2019-08-22 stsp
10252 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
10253 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
10254 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
10255 01073a5d 2019-08-22 stsp char *id_str;
10256 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
10257 01073a5d 2019-08-22 stsp break;
10258 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
10259 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
10260 01073a5d 2019-08-22 stsp if (err)
10261 01073a5d 2019-08-22 stsp break;
10262 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
10263 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
10264 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
10265 01073a5d 2019-08-22 stsp free(id_str);
10266 01073a5d 2019-08-22 stsp }
10267 01073a5d 2019-08-22 stsp
10268 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
10269 01073a5d 2019-08-22 stsp return err;
10270 01073a5d 2019-08-22 stsp }
10271 01073a5d 2019-08-22 stsp
10272 01073a5d 2019-08-22 stsp static const struct got_error *
10273 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10274 01073a5d 2019-08-22 stsp {
10275 01073a5d 2019-08-22 stsp const struct got_error *err;
10276 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
10277 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
10278 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
10279 01073a5d 2019-08-22 stsp char *id_str = NULL;
10280 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
10281 01073a5d 2019-08-22 stsp
10282 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
10283 01073a5d 2019-08-22 stsp if (err)
10284 01073a5d 2019-08-22 stsp return err;
10285 01073a5d 2019-08-22 stsp
10286 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
10287 01073a5d 2019-08-22 stsp if (err)
10288 01073a5d 2019-08-22 stsp goto done;
10289 01073a5d 2019-08-22 stsp
10290 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
10291 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10292 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
10293 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
10294 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
10295 01073a5d 2019-08-22 stsp char *pid_str;
10296 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
10297 01073a5d 2019-08-22 stsp if (err)
10298 01073a5d 2019-08-22 stsp goto done;
10299 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
10300 01073a5d 2019-08-22 stsp free(pid_str);
10301 01073a5d 2019-08-22 stsp }
10302 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
10303 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
10304 1367695b 2020-09-26 naddy (long long)got_object_commit_get_author_time(commit));
10305 01073a5d 2019-08-22 stsp
10306 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
10307 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
10308 1367695b 2020-09-26 naddy (long long)got_object_commit_get_committer_time(commit));
10309 01073a5d 2019-08-22 stsp
10310 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
10311 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
10312 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
10313 01073a5d 2019-08-22 stsp done:
10314 01073a5d 2019-08-22 stsp free(id_str);
10315 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
10316 01073a5d 2019-08-22 stsp return err;
10317 01073a5d 2019-08-22 stsp }
10318 01073a5d 2019-08-22 stsp
10319 01073a5d 2019-08-22 stsp static const struct got_error *
10320 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10321 01073a5d 2019-08-22 stsp {
10322 01073a5d 2019-08-22 stsp const struct got_error *err;
10323 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
10324 01073a5d 2019-08-22 stsp char *id_str = NULL;
10325 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
10326 01073a5d 2019-08-22 stsp
10327 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
10328 01073a5d 2019-08-22 stsp if (err)
10329 01073a5d 2019-08-22 stsp return err;
10330 01073a5d 2019-08-22 stsp
10331 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
10332 01073a5d 2019-08-22 stsp if (err)
10333 01073a5d 2019-08-22 stsp goto done;
10334 01073a5d 2019-08-22 stsp
10335 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
10336 e15d5241 2019-08-22 stsp
10337 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
10338 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
10339 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10340 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
10341 01073a5d 2019-08-22 stsp break;
10342 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
10343 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10344 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
10345 01073a5d 2019-08-22 stsp break;
10346 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
10347 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10348 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
10349 01073a5d 2019-08-22 stsp break;
10350 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
10351 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10352 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
10353 01073a5d 2019-08-22 stsp break;
10354 01073a5d 2019-08-22 stsp default:
10355 01073a5d 2019-08-22 stsp break;
10356 01073a5d 2019-08-22 stsp }
10357 01073a5d 2019-08-22 stsp
10358 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
10359 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
10360 e15d5241 2019-08-22 stsp
10361 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
10362 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
10363 1367695b 2020-09-26 naddy (long long)got_object_tag_get_tagger_time(tag));
10364 01073a5d 2019-08-22 stsp
10365 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
10366 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
10367 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
10368 01073a5d 2019-08-22 stsp done:
10369 01073a5d 2019-08-22 stsp free(id_str);
10370 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
10371 01073a5d 2019-08-22 stsp return err;
10372 01073a5d 2019-08-22 stsp }
10373 01073a5d 2019-08-22 stsp
10374 01073a5d 2019-08-22 stsp static const struct got_error *
10375 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
10376 01073a5d 2019-08-22 stsp {
10377 01073a5d 2019-08-22 stsp const struct got_error *error;
10378 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
10379 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
10380 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
10381 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
10382 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
10383 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
10384 84de9106 2020-12-26 stsp struct got_reflist_head refs;
10385 84de9106 2020-12-26 stsp
10386 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
10387 01073a5d 2019-08-22 stsp
10388 01073a5d 2019-08-22 stsp #ifndef PROFILE
10389 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
10390 01073a5d 2019-08-22 stsp NULL) == -1)
10391 01073a5d 2019-08-22 stsp err(1, "pledge");
10392 01073a5d 2019-08-22 stsp #endif
10393 01073a5d 2019-08-22 stsp
10394 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
10395 01073a5d 2019-08-22 stsp switch (ch) {
10396 896e9b6f 2019-08-26 stsp case 'c':
10397 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
10398 896e9b6f 2019-08-26 stsp break;
10399 01073a5d 2019-08-22 stsp case 'r':
10400 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
10401 01073a5d 2019-08-22 stsp if (repo_path == NULL)
10402 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
10403 9ba1d308 2019-10-21 stsp optarg);
10404 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
10405 01073a5d 2019-08-22 stsp break;
10406 896e9b6f 2019-08-26 stsp case 'P':
10407 896e9b6f 2019-08-26 stsp force_path = 1;
10408 896e9b6f 2019-08-26 stsp break;
10409 01073a5d 2019-08-22 stsp default:
10410 01073a5d 2019-08-22 stsp usage_cat();
10411 01073a5d 2019-08-22 stsp /* NOTREACHED */
10412 01073a5d 2019-08-22 stsp }
10413 01073a5d 2019-08-22 stsp }
10414 01073a5d 2019-08-22 stsp
10415 01073a5d 2019-08-22 stsp argc -= optind;
10416 01073a5d 2019-08-22 stsp argv += optind;
10417 01073a5d 2019-08-22 stsp
10418 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
10419 01073a5d 2019-08-22 stsp if (cwd == NULL) {
10420 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
10421 01073a5d 2019-08-22 stsp goto done;
10422 01073a5d 2019-08-22 stsp }
10423 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
10424 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
10425 01073a5d 2019-08-22 stsp goto done;
10426 01073a5d 2019-08-22 stsp if (worktree) {
10427 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
10428 01073a5d 2019-08-22 stsp repo_path = strdup(
10429 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
10430 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
10431 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
10432 01073a5d 2019-08-22 stsp goto done;
10433 01073a5d 2019-08-22 stsp }
10434 01073a5d 2019-08-22 stsp }
10435 01073a5d 2019-08-22 stsp }
10436 01073a5d 2019-08-22 stsp
10437 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
10438 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
10439 01073a5d 2019-08-22 stsp if (repo_path == NULL)
10440 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
10441 01073a5d 2019-08-22 stsp }
10442 01073a5d 2019-08-22 stsp
10443 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
10444 01073a5d 2019-08-22 stsp free(repo_path);
10445 01073a5d 2019-08-22 stsp if (error != NULL)
10446 01073a5d 2019-08-22 stsp goto done;
10447 01073a5d 2019-08-22 stsp
10448 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
10449 896e9b6f 2019-08-26 stsp if (error)
10450 896e9b6f 2019-08-26 stsp goto done;
10451 896e9b6f 2019-08-26 stsp
10452 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10453 84de9106 2020-12-26 stsp if (error)
10454 84de9106 2020-12-26 stsp goto done;
10455 84de9106 2020-12-26 stsp
10456 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
10457 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
10458 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
10459 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
10460 01073a5d 2019-08-22 stsp if (error)
10461 01073a5d 2019-08-22 stsp goto done;
10462 01073a5d 2019-08-22 stsp
10463 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
10464 896e9b6f 2019-08-26 stsp if (force_path) {
10465 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
10466 896e9b6f 2019-08-26 stsp argv[i]);
10467 896e9b6f 2019-08-26 stsp if (error)
10468 896e9b6f 2019-08-26 stsp break;
10469 896e9b6f 2019-08-26 stsp } else {
10470 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
10471 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
10472 84de9106 2020-12-26 stsp repo);
10473 896e9b6f 2019-08-26 stsp if (error) {
10474 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
10475 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
10476 896e9b6f 2019-08-26 stsp break;
10477 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
10478 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
10479 896e9b6f 2019-08-26 stsp if (error)
10480 896e9b6f 2019-08-26 stsp break;
10481 896e9b6f 2019-08-26 stsp }
10482 896e9b6f 2019-08-26 stsp }
10483 01073a5d 2019-08-22 stsp
10484 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
10485 01073a5d 2019-08-22 stsp if (error)
10486 01073a5d 2019-08-22 stsp break;
10487 01073a5d 2019-08-22 stsp
10488 01073a5d 2019-08-22 stsp switch (obj_type) {
10489 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
10490 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
10491 01073a5d 2019-08-22 stsp break;
10492 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
10493 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
10494 01073a5d 2019-08-22 stsp break;
10495 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
10496 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
10497 01073a5d 2019-08-22 stsp break;
10498 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
10499 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
10500 01073a5d 2019-08-22 stsp break;
10501 01073a5d 2019-08-22 stsp default:
10502 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
10503 01073a5d 2019-08-22 stsp break;
10504 01073a5d 2019-08-22 stsp }
10505 01073a5d 2019-08-22 stsp if (error)
10506 01073a5d 2019-08-22 stsp break;
10507 01073a5d 2019-08-22 stsp free(label);
10508 01073a5d 2019-08-22 stsp label = NULL;
10509 01073a5d 2019-08-22 stsp free(id);
10510 01073a5d 2019-08-22 stsp id = NULL;
10511 01073a5d 2019-08-22 stsp }
10512 01073a5d 2019-08-22 stsp done:
10513 01073a5d 2019-08-22 stsp free(label);
10514 01073a5d 2019-08-22 stsp free(id);
10515 896e9b6f 2019-08-26 stsp free(commit_id);
10516 01073a5d 2019-08-22 stsp if (worktree)
10517 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
10518 01073a5d 2019-08-22 stsp if (repo) {
10519 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10520 01073a5d 2019-08-22 stsp if (error == NULL)
10521 1d0f4054 2021-06-17 stsp error = close_err;
10522 b2118c49 2020-07-28 stsp }
10523 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
10524 b2118c49 2020-07-28 stsp return error;
10525 b2118c49 2020-07-28 stsp }
10526 b2118c49 2020-07-28 stsp
10527 b2118c49 2020-07-28 stsp __dead static void
10528 b2118c49 2020-07-28 stsp usage_info(void)
10529 b2118c49 2020-07-28 stsp {
10530 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
10531 b2118c49 2020-07-28 stsp getprogname());
10532 b2118c49 2020-07-28 stsp exit(1);
10533 b2118c49 2020-07-28 stsp }
10534 b2118c49 2020-07-28 stsp
10535 b2118c49 2020-07-28 stsp static const struct got_error *
10536 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
10537 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
10538 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
10539 b2118c49 2020-07-28 stsp {
10540 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
10541 b2118c49 2020-07-28 stsp char *id_str = NULL;
10542 b2118c49 2020-07-28 stsp char datebuf[128];
10543 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
10544 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
10545 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
10546 b2118c49 2020-07-28 stsp
10547 b2118c49 2020-07-28 stsp /*
10548 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
10549 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
10550 b2118c49 2020-07-28 stsp */
10551 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
10552 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
10553 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
10554 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
10555 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
10556 b2118c49 2020-07-28 stsp }
10557 b2118c49 2020-07-28 stsp
10558 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
10559 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
10560 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
10561 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
10562 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
10563 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
10564 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
10565 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
10566 b2118c49 2020-07-28 stsp else
10567 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
10568 b2118c49 2020-07-28 stsp
10569 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
10570 b2118c49 2020-07-28 stsp if (tm == NULL)
10571 b2118c49 2020-07-28 stsp return NULL;
10572 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
10573 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
10574 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
10575 b2118c49 2020-07-28 stsp
10576 b2118c49 2020-07-28 stsp if (blob_id) {
10577 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
10578 b2118c49 2020-07-28 stsp if (err)
10579 b2118c49 2020-07-28 stsp return err;
10580 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
10581 b2118c49 2020-07-28 stsp free(id_str);
10582 b2118c49 2020-07-28 stsp }
10583 b2118c49 2020-07-28 stsp
10584 b2118c49 2020-07-28 stsp if (staged_blob_id) {
10585 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
10586 b2118c49 2020-07-28 stsp if (err)
10587 b2118c49 2020-07-28 stsp return err;
10588 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
10589 b2118c49 2020-07-28 stsp free(id_str);
10590 b2118c49 2020-07-28 stsp }
10591 b2118c49 2020-07-28 stsp
10592 b2118c49 2020-07-28 stsp if (commit_id) {
10593 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
10594 b2118c49 2020-07-28 stsp if (err)
10595 b2118c49 2020-07-28 stsp return err;
10596 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
10597 b2118c49 2020-07-28 stsp free(id_str);
10598 b2118c49 2020-07-28 stsp }
10599 b2118c49 2020-07-28 stsp
10600 b2118c49 2020-07-28 stsp return NULL;
10601 b2118c49 2020-07-28 stsp }
10602 b2118c49 2020-07-28 stsp
10603 b2118c49 2020-07-28 stsp static const struct got_error *
10604 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
10605 b2118c49 2020-07-28 stsp {
10606 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
10607 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
10608 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
10609 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
10610 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
10611 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
10612 b2118c49 2020-07-28 stsp int ch, show_files = 0;
10613 b2118c49 2020-07-28 stsp
10614 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
10615 b2118c49 2020-07-28 stsp
10616 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
10617 b2118c49 2020-07-28 stsp switch (ch) {
10618 b2118c49 2020-07-28 stsp default:
10619 b2118c49 2020-07-28 stsp usage_info();
10620 b2118c49 2020-07-28 stsp /* NOTREACHED */
10621 b2118c49 2020-07-28 stsp }
10622 01073a5d 2019-08-22 stsp }
10623 b2118c49 2020-07-28 stsp
10624 b2118c49 2020-07-28 stsp argc -= optind;
10625 b2118c49 2020-07-28 stsp argv += optind;
10626 b2118c49 2020-07-28 stsp
10627 b2118c49 2020-07-28 stsp #ifndef PROFILE
10628 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
10629 b2118c49 2020-07-28 stsp NULL) == -1)
10630 b2118c49 2020-07-28 stsp err(1, "pledge");
10631 b2118c49 2020-07-28 stsp #endif
10632 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
10633 b2118c49 2020-07-28 stsp if (cwd == NULL) {
10634 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
10635 b2118c49 2020-07-28 stsp goto done;
10636 b2118c49 2020-07-28 stsp }
10637 b2118c49 2020-07-28 stsp
10638 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
10639 b2118c49 2020-07-28 stsp if (error) {
10640 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10641 8ea5c997 2021-02-07 naddy error = wrap_not_worktree_error(error, "info", cwd);
10642 b2118c49 2020-07-28 stsp goto done;
10643 b2118c49 2020-07-28 stsp }
10644 b2118c49 2020-07-28 stsp
10645 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
10646 b2118c49 2020-07-28 stsp if (error)
10647 b2118c49 2020-07-28 stsp goto done;
10648 b2118c49 2020-07-28 stsp
10649 b2118c49 2020-07-28 stsp if (argc >= 1) {
10650 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
10651 b2118c49 2020-07-28 stsp worktree);
10652 b2118c49 2020-07-28 stsp if (error)
10653 b2118c49 2020-07-28 stsp goto done;
10654 b2118c49 2020-07-28 stsp show_files = 1;
10655 b2118c49 2020-07-28 stsp }
10656 b2118c49 2020-07-28 stsp
10657 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
10658 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
10659 b2118c49 2020-07-28 stsp if (error)
10660 b2118c49 2020-07-28 stsp goto done;
10661 b2118c49 2020-07-28 stsp
10662 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
10663 b2118c49 2020-07-28 stsp if (error)
10664 b2118c49 2020-07-28 stsp goto done;
10665 b2118c49 2020-07-28 stsp
10666 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
10667 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
10668 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
10669 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
10670 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
10671 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
10672 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
10673 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
10674 b2118c49 2020-07-28 stsp
10675 b2118c49 2020-07-28 stsp if (show_files) {
10676 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
10677 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
10678 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
10679 b2118c49 2020-07-28 stsp continue;
10680 b2118c49 2020-07-28 stsp /*
10681 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
10682 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
10683 b2118c49 2020-07-28 stsp */
10684 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
10685 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
10686 b2118c49 2020-07-28 stsp }
10687 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
10688 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
10689 b2118c49 2020-07-28 stsp if (error)
10690 b2118c49 2020-07-28 stsp goto done;
10691 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
10692 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
10693 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
10694 b2118c49 2020-07-28 stsp break;
10695 b2118c49 2020-07-28 stsp }
10696 b2118c49 2020-07-28 stsp }
10697 b2118c49 2020-07-28 stsp }
10698 b2118c49 2020-07-28 stsp done:
10699 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
10700 b2118c49 2020-07-28 stsp free((char *)pe->path);
10701 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
10702 b2118c49 2020-07-28 stsp free(cwd);
10703 b2118c49 2020-07-28 stsp free(id_str);
10704 b2118c49 2020-07-28 stsp free(uuidstr);
10705 0ebf8283 2019-07-24 stsp return error;
10706 0ebf8283 2019-07-24 stsp }