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 0d5bb276 2020-12-15 stsp const char *initial_content, size_t initial_content_len, int check_comments)
435 3ce1b845 2019-07-15 stsp {
436 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
437 bfa12d5e 2020-09-26 stsp char *line = NULL;
438 bfa12d5e 2020-09-26 stsp size_t linesize = 0;
439 bfa12d5e 2020-09-26 stsp ssize_t linelen;
440 3ce1b845 2019-07-15 stsp struct stat st, st2;
441 bfa12d5e 2020-09-26 stsp FILE *fp = NULL;
442 bfa12d5e 2020-09-26 stsp size_t len, logmsg_len;
443 bfa12d5e 2020-09-26 stsp char *initial_content_stripped = NULL, *buf = NULL, *s;
444 3ce1b845 2019-07-15 stsp
445 3ce1b845 2019-07-15 stsp *logmsg = NULL;
446 3ce1b845 2019-07-15 stsp
447 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
448 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
449 3ce1b845 2019-07-15 stsp
450 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
451 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
452 3ce1b845 2019-07-15 stsp
453 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
454 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
455 3ce1b845 2019-07-15 stsp
456 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
457 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
458 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
459 3ce1b845 2019-07-15 stsp
460 bfa12d5e 2020-09-26 stsp /*
461 bfa12d5e 2020-09-26 stsp * Set up a stripped version of the initial content without comments
462 bfa12d5e 2020-09-26 stsp * and blank lines. We need this in order to check if the message
463 bfa12d5e 2020-09-26 stsp * has in fact been edited.
464 bfa12d5e 2020-09-26 stsp */
465 bfa12d5e 2020-09-26 stsp initial_content_stripped = malloc(initial_content_len + 1);
466 bfa12d5e 2020-09-26 stsp if (initial_content_stripped == NULL)
467 bfa12d5e 2020-09-26 stsp return got_error_from_errno("malloc");
468 bfa12d5e 2020-09-26 stsp initial_content_stripped[0] = '\0';
469 bfa12d5e 2020-09-26 stsp
470 bfa12d5e 2020-09-26 stsp buf = strdup(initial_content);
471 bfa12d5e 2020-09-26 stsp if (buf == NULL) {
472 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("strdup");
473 bfa12d5e 2020-09-26 stsp goto done;
474 bfa12d5e 2020-09-26 stsp }
475 bfa12d5e 2020-09-26 stsp s = buf;
476 bfa12d5e 2020-09-26 stsp len = 0;
477 bfa12d5e 2020-09-26 stsp while ((line = strsep(&s, "\n")) != NULL) {
478 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
479 bfa12d5e 2020-09-26 stsp continue; /* remove comments and leading empty lines */
480 bfa12d5e 2020-09-26 stsp len = strlcat(initial_content_stripped, line,
481 bfa12d5e 2020-09-26 stsp initial_content_len + 1);
482 bfa12d5e 2020-09-26 stsp if (len >= initial_content_len + 1) {
483 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
484 bfa12d5e 2020-09-26 stsp goto done;
485 bfa12d5e 2020-09-26 stsp }
486 bfa12d5e 2020-09-26 stsp }
487 bfa12d5e 2020-09-26 stsp while (len > 0 && initial_content_stripped[len - 1] == '\n') {
488 bfa12d5e 2020-09-26 stsp initial_content_stripped[len - 1] = '\0';
489 bfa12d5e 2020-09-26 stsp len--;
490 bfa12d5e 2020-09-26 stsp }
491 bfa12d5e 2020-09-26 stsp
492 bfa12d5e 2020-09-26 stsp logmsg_len = st2.st_size;
493 bfa12d5e 2020-09-26 stsp *logmsg = malloc(logmsg_len + 1);
494 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
495 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
496 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
497 3ce1b845 2019-07-15 stsp
498 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
499 3ce1b845 2019-07-15 stsp if (fp == NULL) {
500 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
501 3ce1b845 2019-07-15 stsp goto done;
502 3ce1b845 2019-07-15 stsp }
503 bfa12d5e 2020-09-26 stsp
504 bfa12d5e 2020-09-26 stsp len = 0;
505 bfa12d5e 2020-09-26 stsp while ((linelen = getline(&line, &linesize, fp)) != -1) {
506 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
507 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
508 bfa12d5e 2020-09-26 stsp len = strlcat(*logmsg, line, logmsg_len + 1);
509 bfa12d5e 2020-09-26 stsp if (len >= logmsg_len + 1) {
510 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
511 bfa12d5e 2020-09-26 stsp goto done;
512 bfa12d5e 2020-09-26 stsp }
513 3ce1b845 2019-07-15 stsp }
514 bfa12d5e 2020-09-26 stsp free(line);
515 bfa12d5e 2020-09-26 stsp if (ferror(fp)) {
516 bfa12d5e 2020-09-26 stsp err = got_ferror(fp, GOT_ERR_IO);
517 bfa12d5e 2020-09-26 stsp goto done;
518 bfa12d5e 2020-09-26 stsp }
519 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
520 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
521 3ce1b845 2019-07-15 stsp len--;
522 3ce1b845 2019-07-15 stsp }
523 3ce1b845 2019-07-15 stsp
524 bfa12d5e 2020-09-26 stsp if (len == 0) {
525 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
526 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
527 bfa12d5e 2020-09-26 stsp goto done;
528 bfa12d5e 2020-09-26 stsp }
529 0d5bb276 2020-12-15 stsp if (check_comments && strcmp(*logmsg, initial_content_stripped) == 0)
530 bfa12d5e 2020-09-26 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
531 bfa12d5e 2020-09-26 stsp "no changes made to commit message, aborting");
532 3ce1b845 2019-07-15 stsp done:
533 bfa12d5e 2020-09-26 stsp free(initial_content_stripped);
534 bfa12d5e 2020-09-26 stsp free(buf);
535 bfa12d5e 2020-09-26 stsp if (fp && fclose(fp) == EOF && err == NULL)
536 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("fclose");
537 3ce1b845 2019-07-15 stsp if (err) {
538 3ce1b845 2019-07-15 stsp free(*logmsg);
539 3ce1b845 2019-07-15 stsp *logmsg = NULL;
540 3ce1b845 2019-07-15 stsp }
541 3ce1b845 2019-07-15 stsp return err;
542 3ce1b845 2019-07-15 stsp }
543 3ce1b845 2019-07-15 stsp
544 3ce1b845 2019-07-15 stsp static const struct got_error *
545 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
546 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
547 3ce1b845 2019-07-15 stsp {
548 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
549 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
550 1601cb9f 2020-09-11 naddy int initial_content_len;
551 97972933 2020-09-11 stsp int fd = -1;
552 3ce1b845 2019-07-15 stsp
553 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
554 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
555 1601cb9f 2020-09-11 naddy branch_name);
556 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
557 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
558 3ce1b845 2019-07-15 stsp
559 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
560 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
561 3ce1b845 2019-07-15 stsp if (err)
562 3ce1b845 2019-07-15 stsp goto done;
563 3ce1b845 2019-07-15 stsp
564 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
565 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
566 97972933 2020-09-11 stsp goto done;
567 97972933 2020-09-11 stsp }
568 3ce1b845 2019-07-15 stsp
569 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
570 0d5bb276 2020-12-15 stsp initial_content_len, 1);
571 3ce1b845 2019-07-15 stsp done:
572 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
573 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
574 3ce1b845 2019-07-15 stsp free(initial_content);
575 59f86c76 2020-09-11 stsp if (err) {
576 59f86c76 2020-09-11 stsp free(*logmsg_path);
577 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
578 59f86c76 2020-09-11 stsp }
579 3ce1b845 2019-07-15 stsp return err;
580 3ce1b845 2019-07-15 stsp }
581 3ce1b845 2019-07-15 stsp
582 3ce1b845 2019-07-15 stsp static const struct got_error *
583 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
584 3ce1b845 2019-07-15 stsp {
585 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
586 3ce1b845 2019-07-15 stsp return NULL;
587 3ce1b845 2019-07-15 stsp }
588 3ce1b845 2019-07-15 stsp
589 3ce1b845 2019-07-15 stsp static const struct got_error *
590 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
591 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
592 84792843 2019-08-09 stsp {
593 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
594 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
595 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
596 84792843 2019-08-09 stsp
597 84792843 2019-08-09 stsp *author = NULL;
598 aba9c984 2019-09-08 stsp
599 50b0790e 2020-09-11 stsp if (worktree)
600 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
601 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
602 50b0790e 2020-09-11 stsp
603 50b0790e 2020-09-11 stsp /*
604 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
605 50b0790e 2020-09-11 stsp * significant to least significant:
606 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
607 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
608 50b0790e 2020-09-11 stsp * 3) repository's git config file
609 50b0790e 2020-09-11 stsp * 4) environment variables
610 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
611 50b0790e 2020-09-11 stsp */
612 50b0790e 2020-09-11 stsp
613 50b0790e 2020-09-11 stsp if (worktree_conf)
614 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
615 50b0790e 2020-09-11 stsp if (got_author == NULL)
616 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
617 84792843 2019-08-09 stsp if (got_author == NULL) {
618 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
619 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
620 c9956ddf 2019-09-08 stsp if (name && email) {
621 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
622 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
623 c9956ddf 2019-09-08 stsp return NULL;
624 c9956ddf 2019-09-08 stsp }
625 257add31 2020-09-09 stsp
626 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
627 257add31 2020-09-09 stsp if (got_author == NULL) {
628 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
629 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
630 257add31 2020-09-09 stsp repo);
631 257add31 2020-09-09 stsp if (name && email) {
632 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
633 257add31 2020-09-09 stsp == -1)
634 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
635 257add31 2020-09-09 stsp return NULL;
636 257add31 2020-09-09 stsp }
637 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
638 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
639 257add31 2020-09-09 stsp }
640 84792843 2019-08-09 stsp }
641 84792843 2019-08-09 stsp
642 aba9c984 2019-09-08 stsp *author = strdup(got_author);
643 aba9c984 2019-09-08 stsp if (*author == NULL)
644 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
645 84792843 2019-08-09 stsp
646 84792843 2019-08-09 stsp /*
647 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
648 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
649 84792843 2019-08-09 stsp */
650 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
651 84792843 2019-08-09 stsp got_author++;
652 aba9c984 2019-09-08 stsp if (*got_author != '<') {
653 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
654 aba9c984 2019-09-08 stsp goto done;
655 aba9c984 2019-09-08 stsp }
656 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
657 84792843 2019-08-09 stsp got_author++;
658 aba9c984 2019-09-08 stsp if (*got_author != '@') {
659 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
660 aba9c984 2019-09-08 stsp goto done;
661 aba9c984 2019-09-08 stsp }
662 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
663 84792843 2019-08-09 stsp got_author++;
664 84792843 2019-08-09 stsp if (*got_author != '>')
665 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
666 aba9c984 2019-09-08 stsp done:
667 aba9c984 2019-09-08 stsp if (err) {
668 aba9c984 2019-09-08 stsp free(*author);
669 aba9c984 2019-09-08 stsp *author = NULL;
670 aba9c984 2019-09-08 stsp }
671 aba9c984 2019-09-08 stsp return err;
672 c9956ddf 2019-09-08 stsp }
673 c9956ddf 2019-09-08 stsp
674 c9956ddf 2019-09-08 stsp static const struct got_error *
675 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
676 c9956ddf 2019-09-08 stsp {
677 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
678 c9956ddf 2019-09-08 stsp
679 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
680 c9956ddf 2019-09-08 stsp if (homedir) {
681 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
682 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
683 c9956ddf 2019-09-08 stsp
684 c9956ddf 2019-09-08 stsp }
685 c9956ddf 2019-09-08 stsp return NULL;
686 84792843 2019-08-09 stsp }
687 84792843 2019-08-09 stsp
688 84792843 2019-08-09 stsp static const struct got_error *
689 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
690 3ce1b845 2019-07-15 stsp {
691 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
692 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
693 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
694 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
695 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
696 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
697 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
698 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
699 3ce1b845 2019-07-15 stsp int ch;
700 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
701 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
702 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
703 3ce1b845 2019-07-15 stsp
704 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
705 3ce1b845 2019-07-15 stsp
706 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
707 3ce1b845 2019-07-15 stsp switch (ch) {
708 3ce1b845 2019-07-15 stsp case 'b':
709 3ce1b845 2019-07-15 stsp branch_name = optarg;
710 3ce1b845 2019-07-15 stsp break;
711 3ce1b845 2019-07-15 stsp case 'm':
712 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
713 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
714 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
715 3ce1b845 2019-07-15 stsp goto done;
716 3ce1b845 2019-07-15 stsp }
717 3ce1b845 2019-07-15 stsp break;
718 3ce1b845 2019-07-15 stsp case 'r':
719 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
720 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
721 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
722 9ba1d308 2019-10-21 stsp optarg);
723 3ce1b845 2019-07-15 stsp goto done;
724 3ce1b845 2019-07-15 stsp }
725 3ce1b845 2019-07-15 stsp break;
726 3ce1b845 2019-07-15 stsp case 'I':
727 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
728 3ce1b845 2019-07-15 stsp break;
729 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
730 3ce1b845 2019-07-15 stsp NULL);
731 3ce1b845 2019-07-15 stsp if (error)
732 3ce1b845 2019-07-15 stsp goto done;
733 3ce1b845 2019-07-15 stsp break;
734 3ce1b845 2019-07-15 stsp default:
735 b2b65d18 2019-08-22 stsp usage_import();
736 3ce1b845 2019-07-15 stsp /* NOTREACHED */
737 3ce1b845 2019-07-15 stsp }
738 3ce1b845 2019-07-15 stsp }
739 3ce1b845 2019-07-15 stsp
740 3ce1b845 2019-07-15 stsp argc -= optind;
741 3ce1b845 2019-07-15 stsp argv += optind;
742 3ce1b845 2019-07-15 stsp
743 3ce1b845 2019-07-15 stsp #ifndef PROFILE
744 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
745 aba9c984 2019-09-08 stsp "unveil",
746 3ce1b845 2019-07-15 stsp NULL) == -1)
747 3ce1b845 2019-07-15 stsp err(1, "pledge");
748 3ce1b845 2019-07-15 stsp #endif
749 3ce1b845 2019-07-15 stsp if (argc != 1)
750 3ce1b845 2019-07-15 stsp usage_import();
751 2c7829a4 2019-06-17 stsp
752 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
753 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
754 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
755 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
756 3ce1b845 2019-07-15 stsp }
757 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
758 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
759 c9956ddf 2019-09-08 stsp if (error)
760 c9956ddf 2019-09-08 stsp goto done;
761 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
762 3ce1b845 2019-07-15 stsp if (error)
763 3ce1b845 2019-07-15 stsp goto done;
764 aba9c984 2019-09-08 stsp
765 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
766 aba9c984 2019-09-08 stsp if (error)
767 aba9c984 2019-09-08 stsp return error;
768 e560b7e0 2019-11-28 stsp
769 e560b7e0 2019-11-28 stsp /*
770 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
771 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
772 e560b7e0 2019-11-28 stsp * an unintended typo.
773 e560b7e0 2019-11-28 stsp */
774 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
775 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
776 3ce1b845 2019-07-15 stsp
777 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
778 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
779 3ce1b845 2019-07-15 stsp goto done;
780 3ce1b845 2019-07-15 stsp }
781 3ce1b845 2019-07-15 stsp
782 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
783 3ce1b845 2019-07-15 stsp if (error) {
784 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
785 3ce1b845 2019-07-15 stsp goto done;
786 3ce1b845 2019-07-15 stsp } else {
787 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
788 3ce1b845 2019-07-15 stsp "import target branch already exists");
789 3ce1b845 2019-07-15 stsp goto done;
790 3ce1b845 2019-07-15 stsp }
791 3ce1b845 2019-07-15 stsp
792 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
793 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
794 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
795 3ce1b845 2019-07-15 stsp goto done;
796 3ce1b845 2019-07-15 stsp }
797 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
798 3ce1b845 2019-07-15 stsp
799 3ce1b845 2019-07-15 stsp /*
800 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
801 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
802 3ce1b845 2019-07-15 stsp */
803 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
804 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
805 3ce1b845 2019-07-15 stsp if (error)
806 3ce1b845 2019-07-15 stsp goto done;
807 8e158b01 2019-09-22 stsp free(logmsg);
808 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
809 ef293bdd 2019-10-21 stsp path_dir, refname);
810 ef293bdd 2019-10-21 stsp if (error) {
811 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
812 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
813 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
814 3ce1b845 2019-07-15 stsp goto done;
815 ef293bdd 2019-10-21 stsp }
816 3ce1b845 2019-07-15 stsp }
817 3ce1b845 2019-07-15 stsp
818 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
819 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
820 ef293bdd 2019-10-21 stsp if (logmsg_path)
821 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
822 3ce1b845 2019-07-15 stsp goto done;
823 ef293bdd 2019-10-21 stsp }
824 3ce1b845 2019-07-15 stsp
825 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
826 ef293bdd 2019-10-21 stsp if (error) {
827 ef293bdd 2019-10-21 stsp if (logmsg_path)
828 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
829 ef293bdd 2019-10-21 stsp goto done;
830 ef293bdd 2019-10-21 stsp }
831 ef293bdd 2019-10-21 stsp
832 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
833 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
834 ef293bdd 2019-10-21 stsp if (error) {
835 ef293bdd 2019-10-21 stsp if (logmsg_path)
836 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
837 3ce1b845 2019-07-15 stsp goto done;
838 ef293bdd 2019-10-21 stsp }
839 3ce1b845 2019-07-15 stsp
840 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
841 ef293bdd 2019-10-21 stsp if (error) {
842 ef293bdd 2019-10-21 stsp if (logmsg_path)
843 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
844 3ce1b845 2019-07-15 stsp goto done;
845 ef293bdd 2019-10-21 stsp }
846 3ce1b845 2019-07-15 stsp
847 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
848 ef293bdd 2019-10-21 stsp if (error) {
849 ef293bdd 2019-10-21 stsp if (logmsg_path)
850 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
851 3ce1b845 2019-07-15 stsp goto done;
852 ef293bdd 2019-10-21 stsp }
853 3ce1b845 2019-07-15 stsp
854 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
855 ef293bdd 2019-10-21 stsp if (error) {
856 ef293bdd 2019-10-21 stsp if (logmsg_path)
857 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
858 3ce1b845 2019-07-15 stsp goto done;
859 ef293bdd 2019-10-21 stsp }
860 3ce1b845 2019-07-15 stsp
861 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
862 3ce1b845 2019-07-15 stsp if (error) {
863 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
864 ef293bdd 2019-10-21 stsp if (logmsg_path)
865 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
866 3ce1b845 2019-07-15 stsp goto done;
867 ef293bdd 2019-10-21 stsp }
868 3ce1b845 2019-07-15 stsp
869 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
870 3ce1b845 2019-07-15 stsp branch_ref);
871 ef293bdd 2019-10-21 stsp if (error) {
872 ef293bdd 2019-10-21 stsp if (logmsg_path)
873 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
874 3ce1b845 2019-07-15 stsp goto done;
875 ef293bdd 2019-10-21 stsp }
876 3ce1b845 2019-07-15 stsp
877 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
878 ef293bdd 2019-10-21 stsp if (error) {
879 ef293bdd 2019-10-21 stsp if (logmsg_path)
880 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
881 3ce1b845 2019-07-15 stsp goto done;
882 ef293bdd 2019-10-21 stsp }
883 3ce1b845 2019-07-15 stsp }
884 3ce1b845 2019-07-15 stsp
885 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
886 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
887 2c7829a4 2019-06-17 stsp done:
888 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
889 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
890 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
891 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
892 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
893 8e158b01 2019-09-22 stsp free(logmsg);
894 ef293bdd 2019-10-21 stsp free(logmsg_path);
895 2c7829a4 2019-06-17 stsp free(repo_path);
896 3ce1b845 2019-07-15 stsp free(editor);
897 3ce1b845 2019-07-15 stsp free(refname);
898 3ce1b845 2019-07-15 stsp free(new_commit_id);
899 3ce1b845 2019-07-15 stsp free(id_str);
900 aba9c984 2019-09-08 stsp free(author);
901 c9956ddf 2019-09-08 stsp free(gitconfig_path);
902 3ce1b845 2019-07-15 stsp if (branch_ref)
903 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
904 3ce1b845 2019-07-15 stsp if (head_ref)
905 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
906 2c7829a4 2019-06-17 stsp return error;
907 93658fb9 2020-03-18 stsp }
908 93658fb9 2020-03-18 stsp
909 93658fb9 2020-03-18 stsp __dead static void
910 93658fb9 2020-03-18 stsp usage_clone(void)
911 93658fb9 2020-03-18 stsp {
912 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
913 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
914 93658fb9 2020-03-18 stsp exit(1);
915 93658fb9 2020-03-18 stsp }
916 892ac3b6 2020-03-18 stsp
917 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
918 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
919 892ac3b6 2020-03-18 stsp int last_p_indexed;
920 892ac3b6 2020-03-18 stsp int last_p_resolved;
921 68999b92 2020-03-18 stsp int verbosity;
922 04d9a9ec 2020-09-24 stsp
923 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
924 04d9a9ec 2020-09-24 stsp
925 04d9a9ec 2020-09-24 stsp int create_configs;
926 04d9a9ec 2020-09-24 stsp int configs_created;
927 04d9a9ec 2020-09-24 stsp struct {
928 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
929 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
930 04d9a9ec 2020-09-24 stsp const char *proto;
931 04d9a9ec 2020-09-24 stsp const char *host;
932 04d9a9ec 2020-09-24 stsp const char *port;
933 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
934 04d9a9ec 2020-09-24 stsp const char *git_url;
935 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
936 04d9a9ec 2020-09-24 stsp int mirror_references;
937 04d9a9ec 2020-09-24 stsp } config_info;
938 892ac3b6 2020-03-18 stsp };
939 93658fb9 2020-03-18 stsp
940 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
941 93658fb9 2020-03-18 stsp static const struct got_error *
942 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
943 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
944 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
945 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches, struct got_repository *repo);
946 04d9a9ec 2020-09-24 stsp
947 04d9a9ec 2020-09-24 stsp static const struct got_error *
948 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
949 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
950 531c3985 2020-03-18 stsp {
951 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
952 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
953 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
954 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
955 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
956 04d9a9ec 2020-09-24 stsp
957 04d9a9ec 2020-09-24 stsp /*
958 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
959 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
960 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
961 04d9a9ec 2020-09-24 stsp * we have all required information.
962 04d9a9ec 2020-09-24 stsp */
963 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
964 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
965 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
966 62d463ca 2020-10-20 naddy a->config_info.host, a->config_info.port,
967 62d463ca 2020-10-20 naddy a->config_info.remote_repo_path,
968 62d463ca 2020-10-20 naddy a->config_info.git_url,
969 62d463ca 2020-10-20 naddy a->config_info.fetch_all_branches,
970 62d463ca 2020-10-20 naddy a->config_info.mirror_references,
971 62d463ca 2020-10-20 naddy a->config_info.symrefs,
972 62d463ca 2020-10-20 naddy a->config_info.wanted_branches, a->repo);
973 04d9a9ec 2020-09-24 stsp if (err)
974 04d9a9ec 2020-09-24 stsp return err;
975 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
976 04d9a9ec 2020-09-24 stsp }
977 b2409d58 2020-03-18 stsp
978 68999b92 2020-03-18 stsp if (a->verbosity < 0)
979 68999b92 2020-03-18 stsp return NULL;
980 68999b92 2020-03-18 stsp
981 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
982 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
983 892ac3b6 2020-03-18 stsp fflush(stdout);
984 12d1281e 2020-03-19 stsp return NULL;
985 b2409d58 2020-03-18 stsp }
986 b2409d58 2020-03-18 stsp
987 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
988 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
989 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
990 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
991 892ac3b6 2020-03-18 stsp print_size = 1;
992 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
993 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
994 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
995 892ac3b6 2020-03-18 stsp }
996 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
997 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
998 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
999 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
1000 892ac3b6 2020-03-18 stsp print_indexed = 1;
1001 892ac3b6 2020-03-18 stsp print_size = 1;
1002 892ac3b6 2020-03-18 stsp }
1003 61cc1a7a 2020-03-18 stsp }
1004 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
1005 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
1006 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
1007 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
1008 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
1009 892ac3b6 2020-03-18 stsp print_resolved = 1;
1010 892ac3b6 2020-03-18 stsp print_indexed = 1;
1011 892ac3b6 2020-03-18 stsp print_size = 1;
1012 892ac3b6 2020-03-18 stsp }
1013 61cc1a7a 2020-03-18 stsp }
1014 3168e5da 2020-09-10 stsp
1015 d2cdc636 2020-03-18 stsp }
1016 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1017 892ac3b6 2020-03-18 stsp printf("\r");
1018 892ac3b6 2020-03-18 stsp if (print_size)
1019 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1020 d715f13e 2020-03-19 stsp if (print_indexed)
1021 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
1022 d715f13e 2020-03-19 stsp if (print_resolved)
1023 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
1024 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1025 892ac3b6 2020-03-18 stsp fflush(stdout);
1026 892ac3b6 2020-03-18 stsp
1027 531c3985 2020-03-18 stsp return NULL;
1028 531c3985 2020-03-18 stsp }
1029 531c3985 2020-03-18 stsp
1030 531c3985 2020-03-18 stsp static const struct got_error *
1031 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
1032 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
1033 4ba14133 2020-03-20 stsp {
1034 4ba14133 2020-03-20 stsp const struct got_error *err;
1035 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
1036 4ba14133 2020-03-20 stsp
1037 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1038 4ba14133 2020-03-20 stsp if (err)
1039 4ba14133 2020-03-20 stsp return err;
1040 4ba14133 2020-03-20 stsp
1041 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
1042 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
1043 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
1044 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
1045 6338a6a1 2020-03-21 stsp }
1046 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
1047 4ba14133 2020-03-20 stsp return err;
1048 4ba14133 2020-03-20 stsp }
1049 4ba14133 2020-03-20 stsp
1050 4ba14133 2020-03-20 stsp static const struct got_error *
1051 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
1052 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
1053 41b0de12 2020-03-21 stsp {
1054 41b0de12 2020-03-21 stsp const struct got_error *err;
1055 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
1056 41b0de12 2020-03-21 stsp
1057 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1058 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1059 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1060 41b0de12 2020-03-21 stsp
1061 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1062 41b0de12 2020-03-21 stsp }
1063 41b0de12 2020-03-21 stsp
1064 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1065 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1066 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1067 41b0de12 2020-03-21 stsp char *id_str;
1068 41b0de12 2020-03-21 stsp
1069 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1070 41b0de12 2020-03-21 stsp if (err)
1071 41b0de12 2020-03-21 stsp return err;
1072 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1073 41b0de12 2020-03-21 stsp free(id_str);
1074 41b0de12 2020-03-21 stsp }
1075 41b0de12 2020-03-21 stsp
1076 41b0de12 2020-03-21 stsp return NULL;
1077 6338a6a1 2020-03-21 stsp }
1078 6338a6a1 2020-03-21 stsp
1079 6338a6a1 2020-03-21 stsp static const struct got_error *
1080 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1081 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1082 6338a6a1 2020-03-21 stsp {
1083 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1084 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1085 6338a6a1 2020-03-21 stsp char *id_str;
1086 6338a6a1 2020-03-21 stsp
1087 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1088 6338a6a1 2020-03-21 stsp if (err)
1089 6338a6a1 2020-03-21 stsp return err;
1090 6338a6a1 2020-03-21 stsp
1091 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1092 6338a6a1 2020-03-21 stsp if (err)
1093 6338a6a1 2020-03-21 stsp goto done;
1094 6338a6a1 2020-03-21 stsp
1095 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1096 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1097 6338a6a1 2020-03-21 stsp
1098 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1099 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1100 6338a6a1 2020-03-21 stsp done:
1101 6338a6a1 2020-03-21 stsp free(id_str);
1102 6338a6a1 2020-03-21 stsp return err;
1103 0e4002ca 2020-03-21 stsp }
1104 0e4002ca 2020-03-21 stsp
1105 0e4002ca 2020-03-21 stsp static int
1106 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1107 0e4002ca 2020-03-21 stsp {
1108 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1109 0e4002ca 2020-03-21 stsp return 0;
1110 0e4002ca 2020-03-21 stsp refname += 5;
1111 0e4002ca 2020-03-21 stsp
1112 0e4002ca 2020-03-21 stsp /*
1113 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1114 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1115 0e4002ca 2020-03-21 stsp */
1116 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1117 0e4002ca 2020-03-21 stsp return 0;
1118 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1119 0e4002ca 2020-03-21 stsp return 0;
1120 0e4002ca 2020-03-21 stsp
1121 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1122 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1123 0e4002ca 2020-03-21 stsp
1124 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1125 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1126 0e4002ca 2020-03-21 stsp return 1;
1127 0e4002ca 2020-03-21 stsp
1128 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1129 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1130 41b0de12 2020-03-21 stsp }
1131 41b0de12 2020-03-21 stsp
1132 0e4002ca 2020-03-21 stsp static int
1133 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1134 0e4002ca 2020-03-21 stsp {
1135 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1136 0e4002ca 2020-03-21 stsp
1137 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1138 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1139 0e4002ca 2020-03-21 stsp return 1;
1140 0e4002ca 2020-03-21 stsp }
1141 0e4002ca 2020-03-21 stsp
1142 0e4002ca 2020-03-21 stsp return 0;
1143 0e4002ca 2020-03-21 stsp }
1144 0e4002ca 2020-03-21 stsp
1145 41b0de12 2020-03-21 stsp static const struct got_error *
1146 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1147 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1148 0e4002ca 2020-03-21 stsp {
1149 0e4002ca 2020-03-21 stsp const struct got_error *err;
1150 0e4002ca 2020-03-21 stsp char *remote_refname;
1151 0e4002ca 2020-03-21 stsp
1152 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1153 0e4002ca 2020-03-21 stsp refname += 5;
1154 0e4002ca 2020-03-21 stsp
1155 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1156 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1157 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1158 0e4002ca 2020-03-21 stsp
1159 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1160 0e4002ca 2020-03-21 stsp free(remote_refname);
1161 7c0b7f42 2020-09-24 stsp return err;
1162 7c0b7f42 2020-09-24 stsp }
1163 7c0b7f42 2020-09-24 stsp
1164 7c0b7f42 2020-09-24 stsp static const struct got_error *
1165 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1166 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, int fetch_all_branches, int mirror_references,
1167 7c0b7f42 2020-09-24 stsp struct got_repository *repo)
1168 7c0b7f42 2020-09-24 stsp {
1169 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1170 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1171 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1172 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1173 7c0b7f42 2020-09-24 stsp ssize_t n;
1174 7c0b7f42 2020-09-24 stsp
1175 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1176 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1177 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1178 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1179 7c0b7f42 2020-09-24 stsp goto done;
1180 7c0b7f42 2020-09-24 stsp }
1181 7c0b7f42 2020-09-24 stsp gotconfig_file = fopen(gotconfig_path, "a");
1182 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1183 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1184 7c0b7f42 2020-09-24 stsp goto done;
1185 7c0b7f42 2020-09-24 stsp }
1186 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1187 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1188 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1189 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1190 7c0b7f42 2020-09-24 stsp "%s%s%s"
1191 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1192 7c0b7f42 2020-09-24 stsp "%s"
1193 7c0b7f42 2020-09-24 stsp "}\n",
1194 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1195 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1196 7c0b7f42 2020-09-24 stsp remote_repo_path,
1197 7c0b7f42 2020-09-24 stsp mirror_references ? "\tmirror-references yes\n" : "") == -1) {
1198 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1199 7c0b7f42 2020-09-24 stsp goto done;
1200 7c0b7f42 2020-09-24 stsp }
1201 7c0b7f42 2020-09-24 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1202 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1203 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1204 7c0b7f42 2020-09-24 stsp goto done;
1205 7c0b7f42 2020-09-24 stsp }
1206 7c0b7f42 2020-09-24 stsp
1207 7c0b7f42 2020-09-24 stsp done:
1208 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1209 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1210 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1211 7c0b7f42 2020-09-24 stsp return err;
1212 7c0b7f42 2020-09-24 stsp }
1213 7c0b7f42 2020-09-24 stsp
1214 7c0b7f42 2020-09-24 stsp static const struct got_error *
1215 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1216 7c0b7f42 2020-09-24 stsp int fetch_all_branches, int mirror_references, struct got_repository *repo)
1217 7c0b7f42 2020-09-24 stsp {
1218 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1219 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1220 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1221 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1222 7c0b7f42 2020-09-24 stsp ssize_t n;
1223 7c0b7f42 2020-09-24 stsp
1224 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1225 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1226 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1227 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1228 7c0b7f42 2020-09-24 stsp goto done;
1229 7c0b7f42 2020-09-24 stsp }
1230 7c0b7f42 2020-09-24 stsp gitconfig_file = fopen(gitconfig_path, "a");
1231 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1232 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1233 7c0b7f42 2020-09-24 stsp goto done;
1234 7c0b7f42 2020-09-24 stsp }
1235 7c0b7f42 2020-09-24 stsp if (mirror_references) {
1236 7c0b7f42 2020-09-24 stsp if (asprintf(&gitconfig,
1237 7c0b7f42 2020-09-24 stsp "[remote \"%s\"]\n"
1238 7c0b7f42 2020-09-24 stsp "\turl = %s\n"
1239 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/*:refs/*\n"
1240 7c0b7f42 2020-09-24 stsp "\tmirror = true\n",
1241 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1242 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1243 7c0b7f42 2020-09-24 stsp goto done;
1244 7c0b7f42 2020-09-24 stsp }
1245 7c0b7f42 2020-09-24 stsp } else if (fetch_all_branches) {
1246 7c0b7f42 2020-09-24 stsp if (asprintf(&gitconfig,
1247 7c0b7f42 2020-09-24 stsp "[remote \"%s\"]\n"
1248 7c0b7f42 2020-09-24 stsp "\turl = %s\n"
1249 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1250 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1251 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1252 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1253 7c0b7f42 2020-09-24 stsp goto done;
1254 7c0b7f42 2020-09-24 stsp }
1255 7c0b7f42 2020-09-24 stsp } else {
1256 7c0b7f42 2020-09-24 stsp const char *branchname;
1257 7c0b7f42 2020-09-24 stsp
1258 7c0b7f42 2020-09-24 stsp /*
1259 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1260 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1261 7c0b7f42 2020-09-24 stsp */
1262 04d9a9ec 2020-09-24 stsp if (default_branch) {
1263 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1264 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1265 7c0b7f42 2020-09-24 stsp branchname += 11;
1266 7c0b7f42 2020-09-24 stsp } else
1267 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1268 7c0b7f42 2020-09-24 stsp if (asprintf(&gitconfig,
1269 7c0b7f42 2020-09-24 stsp "[remote \"%s\"]\n"
1270 7c0b7f42 2020-09-24 stsp "\turl = %s\n"
1271 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1272 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1273 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1274 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1275 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1276 7c0b7f42 2020-09-24 stsp goto done;
1277 7c0b7f42 2020-09-24 stsp }
1278 7c0b7f42 2020-09-24 stsp }
1279 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1280 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1281 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1282 7c0b7f42 2020-09-24 stsp goto done;
1283 7c0b7f42 2020-09-24 stsp }
1284 7c0b7f42 2020-09-24 stsp done:
1285 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1286 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1287 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1288 0e4002ca 2020-03-21 stsp return err;
1289 0e4002ca 2020-03-21 stsp }
1290 0e4002ca 2020-03-21 stsp
1291 0e4002ca 2020-03-21 stsp static const struct got_error *
1292 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1293 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1294 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1295 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches, struct got_repository *repo)
1296 04d9a9ec 2020-09-24 stsp {
1297 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1298 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1299 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1300 04d9a9ec 2020-09-24 stsp
1301 04d9a9ec 2020-09-24 stsp /*
1302 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1303 04d9a9ec 2020-09-24 stsp * one of those.
1304 04d9a9ec 2020-09-24 stsp */
1305 62d463ca 2020-10-20 naddy if (!TAILQ_EMPTY(wanted_branches)) {
1306 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1307 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1308 04d9a9ec 2020-09-24 stsp } else {
1309 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1310 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1311 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1312 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1313 04d9a9ec 2020-09-24 stsp
1314 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1315 04d9a9ec 2020-09-24 stsp continue;
1316 04d9a9ec 2020-09-24 stsp
1317 04d9a9ec 2020-09-24 stsp default_branch = target;
1318 04d9a9ec 2020-09-24 stsp break;
1319 04d9a9ec 2020-09-24 stsp }
1320 04d9a9ec 2020-09-24 stsp }
1321 04d9a9ec 2020-09-24 stsp
1322 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1323 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1324 04d9a9ec 2020-09-24 stsp fetch_all_branches, mirror_references, repo);
1325 04d9a9ec 2020-09-24 stsp if (err)
1326 04d9a9ec 2020-09-24 stsp return err;
1327 04d9a9ec 2020-09-24 stsp
1328 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1329 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1330 04d9a9ec 2020-09-24 stsp mirror_references, repo);
1331 04d9a9ec 2020-09-24 stsp }
1332 04d9a9ec 2020-09-24 stsp
1333 04d9a9ec 2020-09-24 stsp static const struct got_error *
1334 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1335 93658fb9 2020-03-18 stsp {
1336 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1337 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1338 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1339 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1340 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1341 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1342 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1343 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1344 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1345 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1346 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1347 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1348 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1349 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1350 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1351 93658fb9 2020-03-18 stsp
1352 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1353 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1354 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1355 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1356 d9b4d0c0 2020-03-18 stsp
1357 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1358 93658fb9 2020-03-18 stsp switch (ch) {
1359 659e7fbd 2020-03-20 stsp case 'a':
1360 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1361 4ba14133 2020-03-20 stsp break;
1362 4ba14133 2020-03-20 stsp case 'b':
1363 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1364 4ba14133 2020-03-20 stsp optarg, NULL);
1365 4ba14133 2020-03-20 stsp if (error)
1366 4ba14133 2020-03-20 stsp return error;
1367 659e7fbd 2020-03-20 stsp break;
1368 41b0de12 2020-03-21 stsp case 'l':
1369 41b0de12 2020-03-21 stsp list_refs_only = 1;
1370 41b0de12 2020-03-21 stsp break;
1371 469dd726 2020-03-20 stsp case 'm':
1372 469dd726 2020-03-20 stsp mirror_references = 1;
1373 469dd726 2020-03-20 stsp break;
1374 68999b92 2020-03-18 stsp case 'v':
1375 68999b92 2020-03-18 stsp if (verbosity < 0)
1376 68999b92 2020-03-18 stsp verbosity = 0;
1377 68999b92 2020-03-18 stsp else if (verbosity < 3)
1378 68999b92 2020-03-18 stsp verbosity++;
1379 68999b92 2020-03-18 stsp break;
1380 68999b92 2020-03-18 stsp case 'q':
1381 68999b92 2020-03-18 stsp verbosity = -1;
1382 68999b92 2020-03-18 stsp break;
1383 0e4002ca 2020-03-21 stsp case 'R':
1384 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1385 0e4002ca 2020-03-21 stsp optarg, NULL);
1386 0e4002ca 2020-03-21 stsp if (error)
1387 0e4002ca 2020-03-21 stsp return error;
1388 0e4002ca 2020-03-21 stsp break;
1389 93658fb9 2020-03-18 stsp default:
1390 93658fb9 2020-03-18 stsp usage_clone();
1391 93658fb9 2020-03-18 stsp break;
1392 93658fb9 2020-03-18 stsp }
1393 93658fb9 2020-03-18 stsp }
1394 93658fb9 2020-03-18 stsp argc -= optind;
1395 93658fb9 2020-03-18 stsp argv += optind;
1396 39c64a6a 2020-03-18 stsp
1397 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1398 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
1399 41b0de12 2020-03-21 stsp if (list_refs_only) {
1400 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1401 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
1402 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1403 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
1404 41b0de12 2020-03-21 stsp if (mirror_references)
1405 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
1406 41b0de12 2020-03-21 stsp if (verbosity == -1)
1407 ff69268e 2020-12-13 stsp option_conflict('l', 'q');
1408 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1409 ff69268e 2020-12-13 stsp option_conflict('l', 'R');
1410 41b0de12 2020-03-21 stsp }
1411 4ba14133 2020-03-20 stsp
1412 93658fb9 2020-03-18 stsp uri = argv[0];
1413 9df6f38b 2020-03-18 stsp
1414 9df6f38b 2020-03-18 stsp if (argc == 1)
1415 93658fb9 2020-03-18 stsp dirname = NULL;
1416 9df6f38b 2020-03-18 stsp else if (argc == 2)
1417 93658fb9 2020-03-18 stsp dirname = argv[1];
1418 93658fb9 2020-03-18 stsp else
1419 93658fb9 2020-03-18 stsp usage_clone();
1420 09838ffc 2020-03-18 stsp
1421 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1422 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1423 39c64a6a 2020-03-18 stsp if (error)
1424 09f63084 2020-03-20 stsp goto done;
1425 09f63084 2020-03-20 stsp
1426 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1427 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1428 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1429 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1430 09838ffc 2020-03-18 stsp goto done;
1431 09f63084 2020-03-20 stsp }
1432 09838ffc 2020-03-18 stsp
1433 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1434 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1435 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1436 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1437 39c64a6a 2020-03-18 stsp err(1, "pledge");
1438 b46f3e71 2020-03-18 stsp #endif
1439 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1440 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1441 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1442 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1443 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1444 39c64a6a 2020-03-18 stsp err(1, "pledge");
1445 b46f3e71 2020-03-18 stsp #endif
1446 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1447 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1448 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1449 39c64a6a 2020-03-18 stsp goto done;
1450 39c64a6a 2020-03-18 stsp } else {
1451 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1452 39c64a6a 2020-03-18 stsp goto done;
1453 39c64a6a 2020-03-18 stsp }
1454 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1455 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1456 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1457 bb64b798 2020-03-18 stsp goto done;
1458 bb64b798 2020-03-18 stsp }
1459 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1460 bb64b798 2020-03-18 stsp } else
1461 bb64b798 2020-03-18 stsp repo_path = dirname;
1462 bb64b798 2020-03-18 stsp
1463 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1464 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1465 2751fe64 2020-09-24 stsp if (error &&
1466 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1467 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1468 41b0de12 2020-03-21 stsp goto done;
1469 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1470 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1471 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1472 41b0de12 2020-03-21 stsp goto done;
1473 2751fe64 2020-09-24 stsp }
1474 41b0de12 2020-03-21 stsp }
1475 bb64b798 2020-03-18 stsp
1476 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1477 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1478 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1479 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1480 ee448f5f 2020-03-18 stsp goto done;
1481 ee448f5f 2020-03-18 stsp }
1482 ee448f5f 2020-03-18 stsp }
1483 f535bcd4 2020-09-30 stsp error = apply_unveil(repo_path, 0, NULL);
1484 ee448f5f 2020-03-18 stsp if (error)
1485 ee448f5f 2020-03-18 stsp goto done;
1486 f79e6490 2020-04-19 stsp
1487 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1488 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1489 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1490 ee448f5f 2020-03-18 stsp
1491 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1492 9c52365f 2020-03-21 stsp server_path, verbosity);
1493 39c64a6a 2020-03-18 stsp if (error)
1494 bb64b798 2020-03-18 stsp goto done;
1495 bb64b798 2020-03-18 stsp
1496 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1497 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1498 2751fe64 2020-09-24 stsp if (error)
1499 2751fe64 2020-09-24 stsp goto done;
1500 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1501 2751fe64 2020-09-24 stsp if (error)
1502 2751fe64 2020-09-24 stsp goto done;
1503 2751fe64 2020-09-24 stsp }
1504 2751fe64 2020-09-24 stsp
1505 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1506 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1507 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1508 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1509 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1510 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1511 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1512 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1513 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1514 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1515 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1516 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1517 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1518 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1519 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1520 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1521 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1522 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1523 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1524 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1525 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1526 39c64a6a 2020-03-18 stsp if (error)
1527 d9b4d0c0 2020-03-18 stsp goto done;
1528 d9b4d0c0 2020-03-18 stsp
1529 41b0de12 2020-03-21 stsp if (list_refs_only) {
1530 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1531 41b0de12 2020-03-21 stsp goto done;
1532 41b0de12 2020-03-21 stsp }
1533 41b0de12 2020-03-21 stsp
1534 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1535 39c64a6a 2020-03-18 stsp if (error)
1536 d9b4d0c0 2020-03-18 stsp goto done;
1537 68999b92 2020-03-18 stsp if (verbosity >= 0)
1538 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1539 d9b4d0c0 2020-03-18 stsp free(id_str);
1540 d9b4d0c0 2020-03-18 stsp
1541 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1542 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1543 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1544 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1545 7ebc0570 2020-03-18 stsp char *remote_refname;
1546 0e4002ca 2020-03-21 stsp
1547 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1548 0e4002ca 2020-03-21 stsp !mirror_references) {
1549 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1550 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1551 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1552 0e4002ca 2020-03-21 stsp if (error)
1553 0e4002ca 2020-03-21 stsp goto done;
1554 0e4002ca 2020-03-21 stsp continue;
1555 0e4002ca 2020-03-21 stsp }
1556 668a20f6 2020-03-18 stsp
1557 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1558 39c64a6a 2020-03-18 stsp if (error)
1559 d9b4d0c0 2020-03-18 stsp goto done;
1560 d9b4d0c0 2020-03-18 stsp
1561 469dd726 2020-03-20 stsp if (mirror_references)
1562 469dd726 2020-03-20 stsp continue;
1563 469dd726 2020-03-20 stsp
1564 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1565 7ebc0570 2020-03-18 stsp continue;
1566 7ebc0570 2020-03-18 stsp
1567 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1568 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1569 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1570 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1571 7ebc0570 2020-03-18 stsp goto done;
1572 7ebc0570 2020-03-18 stsp }
1573 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1574 f298ae0f 2020-03-25 stsp free(remote_refname);
1575 39c64a6a 2020-03-18 stsp if (error)
1576 d9b4d0c0 2020-03-18 stsp goto done;
1577 d9b4d0c0 2020-03-18 stsp }
1578 d9b4d0c0 2020-03-18 stsp
1579 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1580 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1581 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1582 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1583 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1584 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1585 d9b4d0c0 2020-03-18 stsp
1586 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1587 d9b4d0c0 2020-03-18 stsp continue;
1588 d9b4d0c0 2020-03-18 stsp
1589 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1590 39c64a6a 2020-03-18 stsp if (error) {
1591 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1592 55330abe 2020-03-20 stsp error = NULL;
1593 d9b4d0c0 2020-03-18 stsp continue;
1594 55330abe 2020-03-20 stsp }
1595 d9b4d0c0 2020-03-18 stsp goto done;
1596 d9b4d0c0 2020-03-18 stsp }
1597 d9b4d0c0 2020-03-18 stsp
1598 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1599 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1600 f298ae0f 2020-03-25 stsp if (error)
1601 f298ae0f 2020-03-25 stsp goto done;
1602 f298ae0f 2020-03-25 stsp
1603 f298ae0f 2020-03-25 stsp if (mirror_references)
1604 f298ae0f 2020-03-25 stsp continue;
1605 f298ae0f 2020-03-25 stsp
1606 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1607 f298ae0f 2020-03-25 stsp continue;
1608 f298ae0f 2020-03-25 stsp
1609 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1610 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1611 f298ae0f 2020-03-25 stsp refname) == -1) {
1612 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1613 f298ae0f 2020-03-25 stsp goto done;
1614 f298ae0f 2020-03-25 stsp }
1615 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1616 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1617 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1618 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1619 f298ae0f 2020-03-25 stsp free(remote_refname);
1620 f298ae0f 2020-03-25 stsp goto done;
1621 f298ae0f 2020-03-25 stsp }
1622 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1623 f298ae0f 2020-03-25 stsp if (error) {
1624 f298ae0f 2020-03-25 stsp free(remote_refname);
1625 f298ae0f 2020-03-25 stsp free(remote_target);
1626 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1627 f298ae0f 2020-03-25 stsp error = NULL;
1628 f298ae0f 2020-03-25 stsp continue;
1629 f298ae0f 2020-03-25 stsp }
1630 f298ae0f 2020-03-25 stsp goto done;
1631 f298ae0f 2020-03-25 stsp }
1632 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1633 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1634 f298ae0f 2020-03-25 stsp free(remote_refname);
1635 f298ae0f 2020-03-25 stsp free(remote_target);
1636 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1637 39c64a6a 2020-03-18 stsp if (error)
1638 d9b4d0c0 2020-03-18 stsp goto done;
1639 4ba14133 2020-03-20 stsp }
1640 4ba14133 2020-03-20 stsp if (pe == NULL) {
1641 4ba14133 2020-03-20 stsp /*
1642 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1643 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1644 4ba14133 2020-03-20 stsp * which could be fetched instead.
1645 4ba14133 2020-03-20 stsp */
1646 62d463ca 2020-10-20 naddy TAILQ_FOREACH(pe, &wanted_branches, entry) {
1647 4ba14133 2020-03-20 stsp const char *target = pe->path;
1648 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1649 d9b4d0c0 2020-03-18 stsp
1650 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1651 4ba14133 2020-03-20 stsp if (error) {
1652 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1653 4ba14133 2020-03-20 stsp error = NULL;
1654 4ba14133 2020-03-20 stsp continue;
1655 4ba14133 2020-03-20 stsp }
1656 4ba14133 2020-03-20 stsp goto done;
1657 4ba14133 2020-03-20 stsp }
1658 4ba14133 2020-03-20 stsp
1659 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1660 04d9a9ec 2020-09-24 stsp verbosity, repo);
1661 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1662 4ba14133 2020-03-20 stsp if (error)
1663 4ba14133 2020-03-20 stsp goto done;
1664 4ba14133 2020-03-20 stsp break;
1665 4ba14133 2020-03-20 stsp }
1666 659e7fbd 2020-03-20 stsp }
1667 659e7fbd 2020-03-20 stsp
1668 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1669 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1670 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1671 09838ffc 2020-03-18 stsp done:
1672 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1673 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1674 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1675 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1676 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1677 9c52365f 2020-03-21 stsp }
1678 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1679 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1680 bb64b798 2020-03-18 stsp if (repo)
1681 bb64b798 2020-03-18 stsp got_repo_close(repo);
1682 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1683 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1684 d9b4d0c0 2020-03-18 stsp free(pe->data);
1685 d9b4d0c0 2020-03-18 stsp }
1686 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1687 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1688 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1689 d9b4d0c0 2020-03-18 stsp free(pe->data);
1690 d9b4d0c0 2020-03-18 stsp }
1691 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1692 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1693 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1694 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1695 09838ffc 2020-03-18 stsp free(proto);
1696 09838ffc 2020-03-18 stsp free(host);
1697 09838ffc 2020-03-18 stsp free(port);
1698 09838ffc 2020-03-18 stsp free(server_path);
1699 09838ffc 2020-03-18 stsp free(repo_name);
1700 bb64b798 2020-03-18 stsp free(default_destdir);
1701 b46f3e71 2020-03-18 stsp free(git_url);
1702 39c64a6a 2020-03-18 stsp return error;
1703 7848a0e1 2020-03-19 stsp }
1704 7848a0e1 2020-03-19 stsp
1705 7848a0e1 2020-03-19 stsp static const struct got_error *
1706 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1707 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1708 7848a0e1 2020-03-19 stsp {
1709 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1710 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1711 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1712 7848a0e1 2020-03-19 stsp
1713 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1714 7848a0e1 2020-03-19 stsp if (err)
1715 7848a0e1 2020-03-19 stsp goto done;
1716 7848a0e1 2020-03-19 stsp
1717 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1718 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1719 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1720 88609724 2020-03-21 stsp if (err)
1721 88609724 2020-03-21 stsp goto done;
1722 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1723 88609724 2020-03-21 stsp goto done;
1724 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1725 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1726 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1727 db6d8ad8 2020-03-21 stsp }
1728 db6d8ad8 2020-03-21 stsp goto done;
1729 db6d8ad8 2020-03-21 stsp }
1730 db6d8ad8 2020-03-21 stsp
1731 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1732 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1733 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1734 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1735 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1736 688f11b3 2020-03-21 stsp }
1737 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1738 7848a0e1 2020-03-19 stsp if (err)
1739 7848a0e1 2020-03-19 stsp goto done;
1740 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1741 e8a967e0 2020-03-21 stsp if (err)
1742 e8a967e0 2020-03-21 stsp goto done;
1743 7848a0e1 2020-03-19 stsp } else {
1744 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1745 7848a0e1 2020-03-19 stsp if (err)
1746 7848a0e1 2020-03-19 stsp goto done;
1747 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1748 6338a6a1 2020-03-21 stsp goto done;
1749 6338a6a1 2020-03-21 stsp
1750 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1751 6338a6a1 2020-03-21 stsp if (err)
1752 6338a6a1 2020-03-21 stsp goto done;
1753 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1754 6338a6a1 2020-03-21 stsp if (err)
1755 6338a6a1 2020-03-21 stsp goto done;
1756 7848a0e1 2020-03-19 stsp }
1757 6338a6a1 2020-03-21 stsp
1758 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1759 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1760 6338a6a1 2020-03-21 stsp new_id_str);
1761 7848a0e1 2020-03-19 stsp done:
1762 7848a0e1 2020-03-19 stsp free(old_id);
1763 7848a0e1 2020-03-19 stsp free(new_id_str);
1764 7848a0e1 2020-03-19 stsp return err;
1765 2ab43947 2020-03-18 stsp }
1766 f1bcca34 2020-03-25 stsp
1767 f1bcca34 2020-03-25 stsp static const struct got_error *
1768 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1769 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1770 f1bcca34 2020-03-25 stsp {
1771 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1772 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1773 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1774 f1bcca34 2020-03-25 stsp
1775 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1776 bcf34b0e 2020-03-26 stsp if (err) {
1777 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1778 bcf34b0e 2020-03-26 stsp return err;
1779 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1780 bcf34b0e 2020-03-26 stsp if (err)
1781 bcf34b0e 2020-03-26 stsp goto done;
1782 2ab43947 2020-03-18 stsp
1783 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1784 bcf34b0e 2020-03-26 stsp if (err)
1785 bcf34b0e 2020-03-26 stsp goto done;
1786 f1bcca34 2020-03-25 stsp
1787 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1788 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1789 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1790 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1791 bcf34b0e 2020-03-26 stsp } else {
1792 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1793 f1bcca34 2020-03-25 stsp
1794 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1795 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1796 bcf34b0e 2020-03-26 stsp goto done;
1797 bcf34b0e 2020-03-26 stsp
1798 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1799 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1800 bcf34b0e 2020-03-26 stsp if (err)
1801 bcf34b0e 2020-03-26 stsp goto done;
1802 bcf34b0e 2020-03-26 stsp
1803 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1804 bcf34b0e 2020-03-26 stsp if (err)
1805 bcf34b0e 2020-03-26 stsp goto done;
1806 bcf34b0e 2020-03-26 stsp
1807 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1808 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1809 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1810 bcf34b0e 2020-03-26 stsp
1811 bcf34b0e 2020-03-26 stsp }
1812 f1bcca34 2020-03-25 stsp done:
1813 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1814 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1815 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1816 bcf34b0e 2020-03-26 stsp err = unlock_err;
1817 bcf34b0e 2020-03-26 stsp }
1818 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1819 f1bcca34 2020-03-25 stsp return err;
1820 f1bcca34 2020-03-25 stsp }
1821 f1bcca34 2020-03-25 stsp
1822 2ab43947 2020-03-18 stsp __dead static void
1823 7848a0e1 2020-03-19 stsp usage_fetch(void)
1824 7848a0e1 2020-03-19 stsp {
1825 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1826 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1827 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1828 13f12b09 2020-03-21 stsp getprogname());
1829 7848a0e1 2020-03-19 stsp exit(1);
1830 7848a0e1 2020-03-19 stsp }
1831 7848a0e1 2020-03-19 stsp
1832 7848a0e1 2020-03-19 stsp static const struct got_error *
1833 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1834 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1835 f21ec2f0 2020-03-21 stsp {
1836 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1837 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1838 3789fd73 2020-03-26 stsp char *id_str = NULL;
1839 3789fd73 2020-03-26 stsp
1840 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1841 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1842 3789fd73 2020-03-26 stsp if (err)
1843 3789fd73 2020-03-26 stsp return err;
1844 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1845 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1846 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1847 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1848 3789fd73 2020-03-26 stsp }
1849 3789fd73 2020-03-26 stsp } else {
1850 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1851 3789fd73 2020-03-26 stsp if (err)
1852 3789fd73 2020-03-26 stsp return err;
1853 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1854 3789fd73 2020-03-26 stsp if (err)
1855 3789fd73 2020-03-26 stsp goto done;
1856 3168e5da 2020-09-10 stsp
1857 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1858 3789fd73 2020-03-26 stsp if (err)
1859 3789fd73 2020-03-26 stsp goto done;
1860 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1861 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1862 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1863 3789fd73 2020-03-26 stsp }
1864 3789fd73 2020-03-26 stsp }
1865 3789fd73 2020-03-26 stsp done:
1866 3789fd73 2020-03-26 stsp free(id);
1867 3789fd73 2020-03-26 stsp free(id_str);
1868 3789fd73 2020-03-26 stsp return NULL;
1869 3789fd73 2020-03-26 stsp }
1870 3789fd73 2020-03-26 stsp
1871 3789fd73 2020-03-26 stsp static const struct got_error *
1872 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1873 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
1874 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
1875 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
1876 3789fd73 2020-03-26 stsp {
1877 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
1878 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1879 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1880 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1881 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
1882 3789fd73 2020-03-26 stsp char *local_refname = NULL;
1883 f21ec2f0 2020-03-21 stsp
1884 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&my_refs);
1885 f21ec2f0 2020-03-21 stsp
1886 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1887 3789fd73 2020-03-26 stsp == -1)
1888 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
1889 3789fd73 2020-03-26 stsp
1890 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1891 f21ec2f0 2020-03-21 stsp if (err)
1892 3789fd73 2020-03-26 stsp goto done;
1893 f21ec2f0 2020-03-21 stsp
1894 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &my_refs, entry) {
1895 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1896 f21ec2f0 2020-03-21 stsp
1897 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
1898 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
1899 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
1900 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
1901 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
1902 3789fd73 2020-03-26 stsp continue;
1903 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
1904 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
1905 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
1906 3789fd73 2020-03-26 stsp goto done;
1907 3789fd73 2020-03-26 stsp }
1908 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
1909 3789fd73 2020-03-26 stsp continue;
1910 3789fd73 2020-03-26 stsp }
1911 f21ec2f0 2020-03-21 stsp
1912 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1913 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1914 f21ec2f0 2020-03-21 stsp break;
1915 f21ec2f0 2020-03-21 stsp }
1916 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1917 f21ec2f0 2020-03-21 stsp continue;
1918 f21ec2f0 2020-03-21 stsp
1919 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
1920 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1921 3789fd73 2020-03-26 stsp break;
1922 3789fd73 2020-03-26 stsp }
1923 3789fd73 2020-03-26 stsp if (pe != NULL)
1924 3789fd73 2020-03-26 stsp continue;
1925 f21ec2f0 2020-03-21 stsp
1926 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
1927 f21ec2f0 2020-03-21 stsp if (err)
1928 f21ec2f0 2020-03-21 stsp break;
1929 3789fd73 2020-03-26 stsp
1930 3789fd73 2020-03-26 stsp if (local_refname) {
1931 3789fd73 2020-03-26 stsp struct got_reference *ref;
1932 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
1933 3789fd73 2020-03-26 stsp if (err) {
1934 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1935 3789fd73 2020-03-26 stsp break;
1936 3789fd73 2020-03-26 stsp free(local_refname);
1937 3789fd73 2020-03-26 stsp local_refname = NULL;
1938 3789fd73 2020-03-26 stsp continue;
1939 3789fd73 2020-03-26 stsp }
1940 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
1941 3789fd73 2020-03-26 stsp if (err)
1942 3789fd73 2020-03-26 stsp break;
1943 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
1944 3789fd73 2020-03-26 stsp got_ref_close(ref);
1945 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
1946 3789fd73 2020-03-26 stsp err = unlock_err;
1947 3789fd73 2020-03-26 stsp break;
1948 3789fd73 2020-03-26 stsp }
1949 3789fd73 2020-03-26 stsp
1950 3789fd73 2020-03-26 stsp free(local_refname);
1951 3789fd73 2020-03-26 stsp local_refname = NULL;
1952 6338a6a1 2020-03-21 stsp }
1953 f21ec2f0 2020-03-21 stsp }
1954 3789fd73 2020-03-26 stsp done:
1955 3789fd73 2020-03-26 stsp free(remote_namespace);
1956 3789fd73 2020-03-26 stsp free(local_refname);
1957 0e4002ca 2020-03-21 stsp return err;
1958 0e4002ca 2020-03-21 stsp }
1959 0e4002ca 2020-03-21 stsp
1960 0e4002ca 2020-03-21 stsp static const struct got_error *
1961 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1962 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1963 0e4002ca 2020-03-21 stsp {
1964 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
1965 0e4002ca 2020-03-21 stsp char *remote_refname;
1966 0e4002ca 2020-03-21 stsp struct got_reference *ref;
1967 0e4002ca 2020-03-21 stsp
1968 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1969 0e4002ca 2020-03-21 stsp refname += 5;
1970 0e4002ca 2020-03-21 stsp
1971 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1972 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1973 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1974 f21ec2f0 2020-03-21 stsp
1975 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1976 0e4002ca 2020-03-21 stsp if (err) {
1977 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
1978 0e4002ca 2020-03-21 stsp goto done;
1979 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1980 0e4002ca 2020-03-21 stsp } else {
1981 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
1982 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1983 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
1984 9f142382 2020-03-21 stsp err = unlock_err;
1985 0e4002ca 2020-03-21 stsp got_ref_close(ref);
1986 0e4002ca 2020-03-21 stsp }
1987 0e4002ca 2020-03-21 stsp done:
1988 0e4002ca 2020-03-21 stsp free(remote_refname);
1989 f21ec2f0 2020-03-21 stsp return err;
1990 f21ec2f0 2020-03-21 stsp }
1991 f21ec2f0 2020-03-21 stsp
1992 f21ec2f0 2020-03-21 stsp static const struct got_error *
1993 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1994 7848a0e1 2020-03-19 stsp {
1995 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
1996 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1997 7848a0e1 2020-03-19 stsp const char *remote_name;
1998 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1999 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
2000 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
2001 7848a0e1 2020-03-19 stsp int nremotes;
2002 7848a0e1 2020-03-19 stsp char *id_str = NULL;
2003 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
2004 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
2005 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2006 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2007 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
2008 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
2009 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
2010 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
2011 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
2012 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2013 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
2014 7848a0e1 2020-03-19 stsp
2015 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
2016 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
2017 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
2018 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
2019 7848a0e1 2020-03-19 stsp
2020 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
2021 7848a0e1 2020-03-19 stsp switch (ch) {
2022 659e7fbd 2020-03-20 stsp case 'a':
2023 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
2024 4ba14133 2020-03-20 stsp break;
2025 4ba14133 2020-03-20 stsp case 'b':
2026 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
2027 4ba14133 2020-03-20 stsp optarg, NULL);
2028 4ba14133 2020-03-20 stsp if (error)
2029 4ba14133 2020-03-20 stsp return error;
2030 41b0de12 2020-03-21 stsp break;
2031 f21ec2f0 2020-03-21 stsp case 'd':
2032 f21ec2f0 2020-03-21 stsp delete_refs = 1;
2033 f21ec2f0 2020-03-21 stsp break;
2034 41b0de12 2020-03-21 stsp case 'l':
2035 41b0de12 2020-03-21 stsp list_refs_only = 1;
2036 659e7fbd 2020-03-20 stsp break;
2037 7848a0e1 2020-03-19 stsp case 'r':
2038 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
2039 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2040 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
2041 7848a0e1 2020-03-19 stsp optarg);
2042 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
2043 7848a0e1 2020-03-19 stsp break;
2044 db6d8ad8 2020-03-21 stsp case 't':
2045 db6d8ad8 2020-03-21 stsp replace_tags = 1;
2046 db6d8ad8 2020-03-21 stsp break;
2047 7848a0e1 2020-03-19 stsp case 'v':
2048 7848a0e1 2020-03-19 stsp if (verbosity < 0)
2049 7848a0e1 2020-03-19 stsp verbosity = 0;
2050 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
2051 7848a0e1 2020-03-19 stsp verbosity++;
2052 7848a0e1 2020-03-19 stsp break;
2053 7848a0e1 2020-03-19 stsp case 'q':
2054 7848a0e1 2020-03-19 stsp verbosity = -1;
2055 7848a0e1 2020-03-19 stsp break;
2056 0e4002ca 2020-03-21 stsp case 'R':
2057 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
2058 0e4002ca 2020-03-21 stsp optarg, NULL);
2059 0e4002ca 2020-03-21 stsp if (error)
2060 0e4002ca 2020-03-21 stsp return error;
2061 0e4002ca 2020-03-21 stsp break;
2062 7848a0e1 2020-03-19 stsp default:
2063 7848a0e1 2020-03-19 stsp usage_fetch();
2064 7848a0e1 2020-03-19 stsp break;
2065 7848a0e1 2020-03-19 stsp }
2066 7848a0e1 2020-03-19 stsp }
2067 7848a0e1 2020-03-19 stsp argc -= optind;
2068 7848a0e1 2020-03-19 stsp argv += optind;
2069 7848a0e1 2020-03-19 stsp
2070 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2071 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
2072 41b0de12 2020-03-21 stsp if (list_refs_only) {
2073 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2074 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
2075 ff69268e 2020-12-13 stsp if (fetch_all_branches)
2076 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
2077 f21ec2f0 2020-03-21 stsp if (delete_refs)
2078 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
2079 41b0de12 2020-03-21 stsp if (verbosity == -1)
2080 ff69268e 2020-12-13 stsp option_conflict('l', 'q');
2081 41b0de12 2020-03-21 stsp }
2082 4ba14133 2020-03-20 stsp
2083 7848a0e1 2020-03-19 stsp if (argc == 0)
2084 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2085 7848a0e1 2020-03-19 stsp else if (argc == 1)
2086 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2087 7848a0e1 2020-03-19 stsp else
2088 7848a0e1 2020-03-19 stsp usage_fetch();
2089 7848a0e1 2020-03-19 stsp
2090 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2091 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2092 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2093 7848a0e1 2020-03-19 stsp goto done;
2094 7848a0e1 2020-03-19 stsp }
2095 7848a0e1 2020-03-19 stsp
2096 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2097 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2098 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2099 7848a0e1 2020-03-19 stsp goto done;
2100 7848a0e1 2020-03-19 stsp else
2101 7848a0e1 2020-03-19 stsp error = NULL;
2102 7848a0e1 2020-03-19 stsp if (worktree) {
2103 7848a0e1 2020-03-19 stsp repo_path =
2104 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2105 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2106 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2107 7848a0e1 2020-03-19 stsp if (error)
2108 7848a0e1 2020-03-19 stsp goto done;
2109 7848a0e1 2020-03-19 stsp } else {
2110 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2111 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2112 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2113 7848a0e1 2020-03-19 stsp goto done;
2114 7848a0e1 2020-03-19 stsp }
2115 7848a0e1 2020-03-19 stsp }
2116 7848a0e1 2020-03-19 stsp }
2117 7848a0e1 2020-03-19 stsp
2118 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2119 7848a0e1 2020-03-19 stsp if (error)
2120 7848a0e1 2020-03-19 stsp goto done;
2121 7848a0e1 2020-03-19 stsp
2122 50b0790e 2020-09-11 stsp if (worktree) {
2123 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2124 50b0790e 2020-09-11 stsp if (worktree_conf) {
2125 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2126 50b0790e 2020-09-11 stsp worktree_conf);
2127 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2128 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2129 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2130 50b0790e 2020-09-11 stsp break;
2131 54eb00d5 2020-10-20 stsp }
2132 50b0790e 2020-09-11 stsp }
2133 50b0790e 2020-09-11 stsp }
2134 7848a0e1 2020-03-19 stsp }
2135 50b0790e 2020-09-11 stsp if (remote == NULL) {
2136 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2137 50b0790e 2020-09-11 stsp if (repo_conf) {
2138 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2139 50b0790e 2020-09-11 stsp repo_conf);
2140 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2141 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2142 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2143 50b0790e 2020-09-11 stsp break;
2144 54eb00d5 2020-10-20 stsp }
2145 50b0790e 2020-09-11 stsp }
2146 50b0790e 2020-09-11 stsp }
2147 50b0790e 2020-09-11 stsp }
2148 50b0790e 2020-09-11 stsp if (remote == NULL) {
2149 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2150 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2151 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2152 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2153 257add31 2020-09-09 stsp break;
2154 54eb00d5 2020-10-20 stsp }
2155 257add31 2020-09-09 stsp }
2156 7848a0e1 2020-03-19 stsp }
2157 50b0790e 2020-09-11 stsp if (remote == NULL) {
2158 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2159 50b0790e 2020-09-11 stsp goto done;
2160 b8adfa55 2020-09-25 stsp }
2161 b8adfa55 2020-09-25 stsp
2162 b8adfa55 2020-09-25 stsp if (TAILQ_EMPTY(&wanted_branches) && remote->nbranches > 0) {
2163 b8adfa55 2020-09-25 stsp for (i = 0; i < remote->nbranches; i++) {
2164 b8adfa55 2020-09-25 stsp got_pathlist_append(&wanted_branches,
2165 b8adfa55 2020-09-25 stsp remote->branches[i], NULL);
2166 b8adfa55 2020-09-25 stsp }
2167 b8adfa55 2020-09-25 stsp
2168 50b0790e 2020-09-11 stsp }
2169 7848a0e1 2020-03-19 stsp
2170 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2171 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
2172 7848a0e1 2020-03-19 stsp if (error)
2173 7848a0e1 2020-03-19 stsp goto done;
2174 7848a0e1 2020-03-19 stsp
2175 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2176 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2177 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2178 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2179 7848a0e1 2020-03-19 stsp err(1, "pledge");
2180 7848a0e1 2020-03-19 stsp #endif
2181 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2182 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2183 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2184 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2185 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2186 7848a0e1 2020-03-19 stsp err(1, "pledge");
2187 7848a0e1 2020-03-19 stsp #endif
2188 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2189 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2190 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2191 7848a0e1 2020-03-19 stsp goto done;
2192 7848a0e1 2020-03-19 stsp } else {
2193 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2194 7848a0e1 2020-03-19 stsp goto done;
2195 7848a0e1 2020-03-19 stsp }
2196 7848a0e1 2020-03-19 stsp
2197 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2198 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2199 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
2200 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
2201 7848a0e1 2020-03-19 stsp goto done;
2202 7848a0e1 2020-03-19 stsp }
2203 7848a0e1 2020-03-19 stsp }
2204 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2205 7848a0e1 2020-03-19 stsp if (error)
2206 7848a0e1 2020-03-19 stsp goto done;
2207 f79e6490 2020-04-19 stsp
2208 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2209 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2210 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2211 7848a0e1 2020-03-19 stsp
2212 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2213 9c52365f 2020-03-21 stsp server_path, verbosity);
2214 7848a0e1 2020-03-19 stsp if (error)
2215 7848a0e1 2020-03-19 stsp goto done;
2216 7848a0e1 2020-03-19 stsp
2217 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2218 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2219 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2220 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2221 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2222 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2223 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2224 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2225 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2226 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2227 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2228 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2229 7848a0e1 2020-03-19 stsp if (error)
2230 7848a0e1 2020-03-19 stsp goto done;
2231 7848a0e1 2020-03-19 stsp
2232 41b0de12 2020-03-21 stsp if (list_refs_only) {
2233 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2234 41b0de12 2020-03-21 stsp goto done;
2235 41b0de12 2020-03-21 stsp }
2236 41b0de12 2020-03-21 stsp
2237 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2238 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2239 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2240 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2241 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2242 984065c8 2020-03-19 stsp if (error)
2243 984065c8 2020-03-19 stsp goto done;
2244 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2245 984065c8 2020-03-19 stsp free(id_str);
2246 984065c8 2020-03-19 stsp id_str = NULL;
2247 984065c8 2020-03-19 stsp }
2248 7848a0e1 2020-03-19 stsp
2249 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2250 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2251 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2252 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2253 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2254 7848a0e1 2020-03-19 stsp char *remote_refname;
2255 7848a0e1 2020-03-19 stsp
2256 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2257 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2258 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2259 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2260 0e4002ca 2020-03-21 stsp if (error)
2261 0e4002ca 2020-03-21 stsp goto done;
2262 0e4002ca 2020-03-21 stsp continue;
2263 0e4002ca 2020-03-21 stsp }
2264 0e4002ca 2020-03-21 stsp
2265 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2266 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2267 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2268 7848a0e1 2020-03-19 stsp if (error) {
2269 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2270 7848a0e1 2020-03-19 stsp goto done;
2271 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2272 6338a6a1 2020-03-21 stsp repo);
2273 7848a0e1 2020-03-19 stsp if (error)
2274 7848a0e1 2020-03-19 stsp goto done;
2275 7848a0e1 2020-03-19 stsp } else {
2276 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2277 db6d8ad8 2020-03-21 stsp verbosity, repo);
2278 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2279 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2280 9f142382 2020-03-21 stsp error = unlock_err;
2281 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2282 7848a0e1 2020-03-19 stsp if (error)
2283 7848a0e1 2020-03-19 stsp goto done;
2284 7848a0e1 2020-03-19 stsp }
2285 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2286 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2287 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2288 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2289 7848a0e1 2020-03-19 stsp goto done;
2290 7848a0e1 2020-03-19 stsp }
2291 7848a0e1 2020-03-19 stsp
2292 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2293 7848a0e1 2020-03-19 stsp if (error) {
2294 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2295 7848a0e1 2020-03-19 stsp goto done;
2296 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2297 688f11b3 2020-03-21 stsp verbosity, repo);
2298 7848a0e1 2020-03-19 stsp if (error)
2299 7848a0e1 2020-03-19 stsp goto done;
2300 7848a0e1 2020-03-19 stsp } else {
2301 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2302 db6d8ad8 2020-03-21 stsp verbosity, repo);
2303 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2304 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2305 9f142382 2020-03-21 stsp error = unlock_err;
2306 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2307 7848a0e1 2020-03-19 stsp if (error)
2308 7848a0e1 2020-03-19 stsp goto done;
2309 7848a0e1 2020-03-19 stsp }
2310 2ec30c80 2020-03-20 stsp
2311 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2312 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2313 2ec30c80 2020-03-20 stsp if (error) {
2314 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2315 2ec30c80 2020-03-20 stsp goto done;
2316 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2317 6338a6a1 2020-03-21 stsp repo);
2318 2ec30c80 2020-03-20 stsp if (error)
2319 2ec30c80 2020-03-20 stsp goto done;
2320 9f142382 2020-03-21 stsp } else {
2321 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2322 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2323 9f142382 2020-03-21 stsp error = unlock_err;
2324 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2325 9f142382 2020-03-21 stsp }
2326 7848a0e1 2020-03-19 stsp }
2327 7848a0e1 2020-03-19 stsp }
2328 f1bcca34 2020-03-25 stsp if (delete_refs) {
2329 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2330 3789fd73 2020-03-26 stsp verbosity, repo);
2331 f1bcca34 2020-03-25 stsp if (error)
2332 f1bcca34 2020-03-25 stsp goto done;
2333 f1bcca34 2020-03-25 stsp }
2334 f1bcca34 2020-03-25 stsp
2335 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2336 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2337 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2338 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2339 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2340 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2341 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2342 f1bcca34 2020-03-25 stsp
2343 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2344 f1bcca34 2020-03-25 stsp continue;
2345 f1bcca34 2020-03-25 stsp
2346 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2347 f1bcca34 2020-03-25 stsp continue;
2348 f1bcca34 2020-03-25 stsp
2349 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2350 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2351 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2352 f1bcca34 2020-03-25 stsp goto done;
2353 f1bcca34 2020-03-25 stsp }
2354 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2355 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2356 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2357 f1bcca34 2020-03-25 stsp free(remote_refname);
2358 f1bcca34 2020-03-25 stsp goto done;
2359 f1bcca34 2020-03-25 stsp }
2360 f1bcca34 2020-03-25 stsp
2361 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2362 f1bcca34 2020-03-25 stsp 0);
2363 f1bcca34 2020-03-25 stsp if (error) {
2364 f1bcca34 2020-03-25 stsp free(remote_refname);
2365 f1bcca34 2020-03-25 stsp free(remote_target);
2366 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2367 f1bcca34 2020-03-25 stsp error = NULL;
2368 f1bcca34 2020-03-25 stsp continue;
2369 f1bcca34 2020-03-25 stsp }
2370 f1bcca34 2020-03-25 stsp goto done;
2371 f1bcca34 2020-03-25 stsp }
2372 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2373 f1bcca34 2020-03-25 stsp verbosity, repo);
2374 f1bcca34 2020-03-25 stsp free(remote_refname);
2375 f1bcca34 2020-03-25 stsp free(remote_target);
2376 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2377 f1bcca34 2020-03-25 stsp if (error)
2378 f1bcca34 2020-03-25 stsp goto done;
2379 f1bcca34 2020-03-25 stsp }
2380 f1bcca34 2020-03-25 stsp }
2381 7848a0e1 2020-03-19 stsp done:
2382 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2383 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2384 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2385 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2386 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2387 9c52365f 2020-03-21 stsp }
2388 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2389 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2390 7848a0e1 2020-03-19 stsp if (repo)
2391 7848a0e1 2020-03-19 stsp got_repo_close(repo);
2392 7848a0e1 2020-03-19 stsp if (worktree)
2393 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2394 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2395 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2396 7848a0e1 2020-03-19 stsp free(pe->data);
2397 7848a0e1 2020-03-19 stsp }
2398 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2399 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2400 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2401 7848a0e1 2020-03-19 stsp free(pe->data);
2402 7848a0e1 2020-03-19 stsp }
2403 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2404 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2405 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2406 7848a0e1 2020-03-19 stsp free(id_str);
2407 7848a0e1 2020-03-19 stsp free(cwd);
2408 7848a0e1 2020-03-19 stsp free(repo_path);
2409 7848a0e1 2020-03-19 stsp free(pack_hash);
2410 7848a0e1 2020-03-19 stsp free(proto);
2411 7848a0e1 2020-03-19 stsp free(host);
2412 7848a0e1 2020-03-19 stsp free(port);
2413 7848a0e1 2020-03-19 stsp free(server_path);
2414 7848a0e1 2020-03-19 stsp free(repo_name);
2415 7848a0e1 2020-03-19 stsp return error;
2416 7848a0e1 2020-03-19 stsp }
2417 7848a0e1 2020-03-19 stsp
2418 7848a0e1 2020-03-19 stsp
2419 7848a0e1 2020-03-19 stsp __dead static void
2420 2ab43947 2020-03-18 stsp usage_checkout(void)
2421 2ab43947 2020-03-18 stsp {
2422 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2423 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2424 2ab43947 2020-03-18 stsp exit(1);
2425 2ab43947 2020-03-18 stsp }
2426 2ab43947 2020-03-18 stsp
2427 2ab43947 2020-03-18 stsp static void
2428 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2429 2ab43947 2020-03-18 stsp {
2430 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2431 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2432 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2433 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2434 2ab43947 2020-03-18 stsp getprogname());
2435 2ab43947 2020-03-18 stsp }
2436 2ab43947 2020-03-18 stsp
2437 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2438 2ab43947 2020-03-18 stsp const char *worktree_path;
2439 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2440 2ab43947 2020-03-18 stsp };
2441 2ab43947 2020-03-18 stsp
2442 2ab43947 2020-03-18 stsp static const struct got_error *
2443 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2444 2ab43947 2020-03-18 stsp {
2445 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2446 2ab43947 2020-03-18 stsp
2447 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2448 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2449 2ab43947 2020-03-18 stsp return NULL;
2450 2ab43947 2020-03-18 stsp
2451 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2452 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2453 2ab43947 2020-03-18 stsp return NULL;
2454 2ab43947 2020-03-18 stsp }
2455 2ab43947 2020-03-18 stsp
2456 2ab43947 2020-03-18 stsp while (path[0] == '/')
2457 2ab43947 2020-03-18 stsp path++;
2458 2ab43947 2020-03-18 stsp
2459 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2460 2ab43947 2020-03-18 stsp return NULL;
2461 2ab43947 2020-03-18 stsp }
2462 2ab43947 2020-03-18 stsp
2463 2ab43947 2020-03-18 stsp static const struct got_error *
2464 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2465 2ab43947 2020-03-18 stsp {
2466 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2467 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2468 2ab43947 2020-03-18 stsp return NULL;
2469 2ab43947 2020-03-18 stsp }
2470 2ab43947 2020-03-18 stsp
2471 2ab43947 2020-03-18 stsp static const struct got_error *
2472 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2473 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2474 2ab43947 2020-03-18 stsp struct got_repository *repo)
2475 2ab43947 2020-03-18 stsp {
2476 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2477 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2478 2ab43947 2020-03-18 stsp
2479 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2480 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2481 2ab43947 2020-03-18 stsp if (err)
2482 2ab43947 2020-03-18 stsp return err;
2483 2ab43947 2020-03-18 stsp
2484 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2485 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2486 2ab43947 2020-03-18 stsp
2487 2ab43947 2020-03-18 stsp /*
2488 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2489 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2490 2ab43947 2020-03-18 stsp *
2491 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2492 2ab43947 2020-03-18 stsp *
2493 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2494 2ab43947 2020-03-18 stsp * \ /
2495 2ab43947 2020-03-18 stsp * C E
2496 2ab43947 2020-03-18 stsp * \ /
2497 2ab43947 2020-03-18 stsp * B (yca)
2498 2ab43947 2020-03-18 stsp * |
2499 2ab43947 2020-03-18 stsp * A
2500 2ab43947 2020-03-18 stsp *
2501 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2502 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2503 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2504 2ab43947 2020-03-18 stsp */
2505 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2506 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2507 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2508 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2509 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2510 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2511 2ab43947 2020-03-18 stsp
2512 2ab43947 2020-03-18 stsp free(yca_id);
2513 2ab43947 2020-03-18 stsp return NULL;
2514 2ab43947 2020-03-18 stsp }
2515 2ab43947 2020-03-18 stsp
2516 2ab43947 2020-03-18 stsp static const struct got_error *
2517 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2518 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2519 2ab43947 2020-03-18 stsp struct got_repository *repo)
2520 2ab43947 2020-03-18 stsp {
2521 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2522 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2523 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2524 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2525 2ab43947 2020-03-18 stsp
2526 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2527 2ab43947 2020-03-18 stsp if (err)
2528 2ab43947 2020-03-18 stsp goto done;
2529 2ab43947 2020-03-18 stsp
2530 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2531 2ab43947 2020-03-18 stsp is_same_branch = 1;
2532 2ab43947 2020-03-18 stsp goto done;
2533 2ab43947 2020-03-18 stsp }
2534 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2535 2ab43947 2020-03-18 stsp is_same_branch = 1;
2536 2ab43947 2020-03-18 stsp goto done;
2537 2ab43947 2020-03-18 stsp }
2538 2ab43947 2020-03-18 stsp
2539 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2540 2ab43947 2020-03-18 stsp if (err)
2541 2ab43947 2020-03-18 stsp goto done;
2542 2ab43947 2020-03-18 stsp
2543 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2544 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2545 2ab43947 2020-03-18 stsp if (err)
2546 2ab43947 2020-03-18 stsp goto done;
2547 2ab43947 2020-03-18 stsp
2548 2ab43947 2020-03-18 stsp for (;;) {
2549 2ab43947 2020-03-18 stsp struct got_object_id *id;
2550 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2551 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2552 2ab43947 2020-03-18 stsp if (err) {
2553 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2554 2ab43947 2020-03-18 stsp err = NULL;
2555 2ab43947 2020-03-18 stsp break;
2556 2ab43947 2020-03-18 stsp }
2557 2ab43947 2020-03-18 stsp
2558 2ab43947 2020-03-18 stsp if (id) {
2559 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2560 2ab43947 2020-03-18 stsp break;
2561 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2562 2ab43947 2020-03-18 stsp is_same_branch = 1;
2563 2ab43947 2020-03-18 stsp break;
2564 2ab43947 2020-03-18 stsp }
2565 2ab43947 2020-03-18 stsp }
2566 2ab43947 2020-03-18 stsp }
2567 2ab43947 2020-03-18 stsp done:
2568 2ab43947 2020-03-18 stsp if (graph)
2569 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2570 2ab43947 2020-03-18 stsp free(head_commit_id);
2571 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2572 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2573 2ab43947 2020-03-18 stsp return err;
2574 a367ff0f 2019-05-14 stsp }
2575 8069f636 2019-01-12 stsp
2576 4ed7e80c 2018-05-20 stsp static const struct got_error *
2577 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2578 4b6c9460 2020-03-05 stsp {
2579 4b6c9460 2020-03-05 stsp static char msg[512];
2580 4b6c9460 2020-03-05 stsp const char *branch_name;
2581 4b6c9460 2020-03-05 stsp
2582 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2583 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2584 4b6c9460 2020-03-05 stsp else
2585 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2586 4b6c9460 2020-03-05 stsp
2587 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2588 4b6c9460 2020-03-05 stsp branch_name += 11;
2589 4b6c9460 2020-03-05 stsp
2590 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2591 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2592 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2593 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2594 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2595 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2596 4b6c9460 2020-03-05 stsp
2597 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2598 4b6c9460 2020-03-05 stsp }
2599 4b6c9460 2020-03-05 stsp
2600 4b6c9460 2020-03-05 stsp static const struct got_error *
2601 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2602 c09a553d 2018-03-12 stsp {
2603 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2604 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2605 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2606 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2607 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2608 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2609 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2610 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2611 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2612 f0207f6a 2020-10-19 stsp char *cwd = NULL;
2613 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2614 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2615 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2616 f2ea84fa 2019-07-27 stsp
2617 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2618 c09a553d 2018-03-12 stsp
2619 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2620 0bb8a95e 2018-03-12 stsp switch (ch) {
2621 08573d5b 2019-05-14 stsp case 'b':
2622 08573d5b 2019-05-14 stsp branch_name = optarg;
2623 08573d5b 2019-05-14 stsp break;
2624 8069f636 2019-01-12 stsp case 'c':
2625 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2626 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2627 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2628 bb51a5b4 2020-01-13 stsp break;
2629 bb51a5b4 2020-01-13 stsp case 'E':
2630 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2631 8069f636 2019-01-12 stsp break;
2632 0bb8a95e 2018-03-12 stsp case 'p':
2633 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2634 0bb8a95e 2018-03-12 stsp break;
2635 0bb8a95e 2018-03-12 stsp default:
2636 2deda0b9 2019-03-07 stsp usage_checkout();
2637 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2638 0bb8a95e 2018-03-12 stsp }
2639 0bb8a95e 2018-03-12 stsp }
2640 0bb8a95e 2018-03-12 stsp
2641 0bb8a95e 2018-03-12 stsp argc -= optind;
2642 0bb8a95e 2018-03-12 stsp argv += optind;
2643 0bb8a95e 2018-03-12 stsp
2644 6715a751 2018-03-16 stsp #ifndef PROFILE
2645 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2646 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2647 c09a553d 2018-03-12 stsp err(1, "pledge");
2648 6715a751 2018-03-16 stsp #endif
2649 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2650 c47340da 2020-09-20 stsp char *base, *dotgit;
2651 f0207f6a 2020-10-19 stsp const char *path;
2652 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2653 76089277 2018-04-01 stsp if (repo_path == NULL)
2654 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2655 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2656 76089277 2018-04-01 stsp if (cwd == NULL) {
2657 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2658 76089277 2018-04-01 stsp goto done;
2659 76089277 2018-04-01 stsp }
2660 c47340da 2020-09-20 stsp if (path_prefix[0])
2661 f0207f6a 2020-10-19 stsp path = path_prefix;
2662 c47340da 2020-09-20 stsp else
2663 f0207f6a 2020-10-19 stsp path = repo_path;
2664 f0207f6a 2020-10-19 stsp error = got_path_basename(&base, path);
2665 f0207f6a 2020-10-19 stsp if (error)
2666 c47340da 2020-09-20 stsp goto done;
2667 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2668 c09a553d 2018-03-12 stsp if (dotgit)
2669 c09a553d 2018-03-12 stsp *dotgit = '\0';
2670 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2671 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2672 f0207f6a 2020-10-19 stsp free(base);
2673 76089277 2018-04-01 stsp goto done;
2674 c09a553d 2018-03-12 stsp }
2675 f0207f6a 2020-10-19 stsp free(base);
2676 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2677 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2678 76089277 2018-04-01 stsp if (repo_path == NULL) {
2679 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2680 76089277 2018-04-01 stsp goto done;
2681 76089277 2018-04-01 stsp }
2682 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2683 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2684 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2685 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2686 b4b3a7dd 2019-07-22 stsp argv[1]);
2687 b4b3a7dd 2019-07-22 stsp goto done;
2688 b4b3a7dd 2019-07-22 stsp }
2689 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2690 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2691 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2692 b4b3a7dd 2019-07-22 stsp goto done;
2693 b4b3a7dd 2019-07-22 stsp }
2694 76089277 2018-04-01 stsp }
2695 c09a553d 2018-03-12 stsp } else
2696 c09a553d 2018-03-12 stsp usage_checkout();
2697 c09a553d 2018-03-12 stsp
2698 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2699 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2700 13bfb272 2019-05-10 jcs
2701 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2702 c09a553d 2018-03-12 stsp if (error != NULL)
2703 c02c541e 2019-03-29 stsp goto done;
2704 c02c541e 2019-03-29 stsp
2705 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2706 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2707 c530dc23 2019-07-23 stsp if (error) {
2708 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2709 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2710 c530dc23 2019-07-23 stsp goto done;
2711 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2712 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2713 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2714 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2715 c530dc23 2019-07-23 stsp goto done;
2716 c530dc23 2019-07-23 stsp }
2717 c530dc23 2019-07-23 stsp }
2718 c530dc23 2019-07-23 stsp
2719 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2720 c02c541e 2019-03-29 stsp if (error)
2721 c09a553d 2018-03-12 stsp goto done;
2722 8069f636 2019-01-12 stsp
2723 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2724 c09a553d 2018-03-12 stsp if (error != NULL)
2725 c09a553d 2018-03-12 stsp goto done;
2726 c09a553d 2018-03-12 stsp
2727 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2728 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2729 c09a553d 2018-03-12 stsp goto done;
2730 c09a553d 2018-03-12 stsp
2731 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2732 c09a553d 2018-03-12 stsp if (error != NULL)
2733 c09a553d 2018-03-12 stsp goto done;
2734 c09a553d 2018-03-12 stsp
2735 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2736 e5dc7198 2018-12-29 stsp path_prefix);
2737 e5dc7198 2018-12-29 stsp if (error != NULL)
2738 e5dc7198 2018-12-29 stsp goto done;
2739 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2740 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2741 49520a32 2018-12-29 stsp goto done;
2742 49520a32 2018-12-29 stsp }
2743 49520a32 2018-12-29 stsp
2744 8069f636 2019-01-12 stsp if (commit_id_str) {
2745 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2746 84de9106 2020-12-26 stsp struct got_reflist_head refs;
2747 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
2748 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2749 84de9106 2020-12-26 stsp NULL);
2750 84de9106 2020-12-26 stsp if (error)
2751 84de9106 2020-12-26 stsp goto done;
2752 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2753 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2754 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
2755 30837e32 2019-07-25 stsp if (error)
2756 8069f636 2019-01-12 stsp goto done;
2757 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2758 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2759 8069f636 2019-01-12 stsp if (error != NULL) {
2760 8069f636 2019-01-12 stsp free(commit_id);
2761 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2762 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2763 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2764 4b6c9460 2020-03-05 stsp }
2765 8069f636 2019-01-12 stsp goto done;
2766 8069f636 2019-01-12 stsp }
2767 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2768 4b6c9460 2020-03-05 stsp if (error) {
2769 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2770 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2771 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2772 4b6c9460 2020-03-05 stsp }
2773 45d344f6 2019-05-14 stsp goto done;
2774 4b6c9460 2020-03-05 stsp }
2775 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2776 8069f636 2019-01-12 stsp commit_id);
2777 8069f636 2019-01-12 stsp free(commit_id);
2778 8069f636 2019-01-12 stsp if (error)
2779 8069f636 2019-01-12 stsp goto done;
2780 8069f636 2019-01-12 stsp }
2781 8069f636 2019-01-12 stsp
2782 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2783 f2ea84fa 2019-07-27 stsp if (error)
2784 f2ea84fa 2019-07-27 stsp goto done;
2785 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2786 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2787 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2788 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2789 c09a553d 2018-03-12 stsp if (error != NULL)
2790 c09a553d 2018-03-12 stsp goto done;
2791 c09a553d 2018-03-12 stsp
2792 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2793 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2794 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2795 c09a553d 2018-03-12 stsp done:
2796 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2797 8069f636 2019-01-12 stsp free(commit_id_str);
2798 76089277 2018-04-01 stsp free(repo_path);
2799 507dc3bb 2018-12-29 stsp free(worktree_path);
2800 c47340da 2020-09-20 stsp free(cwd);
2801 507dc3bb 2018-12-29 stsp return error;
2802 9627c110 2020-04-18 stsp }
2803 9627c110 2020-04-18 stsp
2804 9627c110 2020-04-18 stsp struct got_update_progress_arg {
2805 9627c110 2020-04-18 stsp int did_something;
2806 9627c110 2020-04-18 stsp int conflicts;
2807 9627c110 2020-04-18 stsp int obstructed;
2808 9627c110 2020-04-18 stsp int not_updated;
2809 9627c110 2020-04-18 stsp };
2810 9627c110 2020-04-18 stsp
2811 9627c110 2020-04-18 stsp void
2812 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2813 9627c110 2020-04-18 stsp {
2814 9627c110 2020-04-18 stsp if (!upa->did_something)
2815 9627c110 2020-04-18 stsp return;
2816 9627c110 2020-04-18 stsp
2817 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
2818 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2819 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
2820 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
2821 9627c110 2020-04-18 stsp upa->obstructed);
2822 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
2823 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
2824 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
2825 507dc3bb 2018-12-29 stsp }
2826 507dc3bb 2018-12-29 stsp
2827 507dc3bb 2018-12-29 stsp __dead static void
2828 507dc3bb 2018-12-29 stsp usage_update(void)
2829 507dc3bb 2018-12-29 stsp {
2830 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2831 507dc3bb 2018-12-29 stsp getprogname());
2832 507dc3bb 2018-12-29 stsp exit(1);
2833 507dc3bb 2018-12-29 stsp }
2834 507dc3bb 2018-12-29 stsp
2835 1ee397ad 2019-07-12 stsp static const struct got_error *
2836 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2837 507dc3bb 2018-12-29 stsp {
2838 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
2839 784955db 2019-01-12 stsp
2840 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2841 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2842 1ee397ad 2019-07-12 stsp return NULL;
2843 507dc3bb 2018-12-29 stsp
2844 9627c110 2020-04-18 stsp upa->did_something = 1;
2845 a484d721 2019-06-10 stsp
2846 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2847 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2848 1ee397ad 2019-07-12 stsp return NULL;
2849 a484d721 2019-06-10 stsp
2850 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
2851 9627c110 2020-04-18 stsp upa->conflicts++;
2852 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
2853 9627c110 2020-04-18 stsp upa->obstructed++;
2854 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2855 9627c110 2020-04-18 stsp upa->not_updated++;
2856 9627c110 2020-04-18 stsp
2857 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2858 507dc3bb 2018-12-29 stsp path++;
2859 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2860 1ee397ad 2019-07-12 stsp return NULL;
2861 be7061eb 2018-12-30 stsp }
2862 be7061eb 2018-12-30 stsp
2863 be7061eb 2018-12-30 stsp static const struct got_error *
2864 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2865 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2866 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2867 a1fb16d8 2019-05-24 stsp {
2868 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2869 a1fb16d8 2019-05-24 stsp char *base_id_str;
2870 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2871 a1fb16d8 2019-05-24 stsp
2872 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2873 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2874 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2875 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2876 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2877 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2878 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2879 a1fb16d8 2019-05-24 stsp }
2880 a1fb16d8 2019-05-24 stsp
2881 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2882 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2883 a1fb16d8 2019-05-24 stsp if (err) {
2884 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2885 a1fb16d8 2019-05-24 stsp return err;
2886 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2887 a1fb16d8 2019-05-24 stsp }
2888 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2889 a1fb16d8 2019-05-24 stsp return NULL;
2890 a1fb16d8 2019-05-24 stsp
2891 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2892 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2893 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2894 a1fb16d8 2019-05-24 stsp if (err)
2895 a1fb16d8 2019-05-24 stsp return err;
2896 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2897 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2898 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2899 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2900 0ebf8283 2019-07-24 stsp return NULL;
2901 0ebf8283 2019-07-24 stsp }
2902 0ebf8283 2019-07-24 stsp
2903 0ebf8283 2019-07-24 stsp static const struct got_error *
2904 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2905 0ebf8283 2019-07-24 stsp {
2906 0ebf8283 2019-07-24 stsp const struct got_error *err;
2907 0ebf8283 2019-07-24 stsp int in_progress;
2908 0ebf8283 2019-07-24 stsp
2909 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2910 0ebf8283 2019-07-24 stsp if (err)
2911 0ebf8283 2019-07-24 stsp return err;
2912 0ebf8283 2019-07-24 stsp if (in_progress)
2913 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2914 0ebf8283 2019-07-24 stsp
2915 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2916 0ebf8283 2019-07-24 stsp if (err)
2917 0ebf8283 2019-07-24 stsp return err;
2918 0ebf8283 2019-07-24 stsp if (in_progress)
2919 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2920 0ebf8283 2019-07-24 stsp
2921 a1fb16d8 2019-05-24 stsp return NULL;
2922 a5edda0a 2019-07-27 stsp }
2923 a5edda0a 2019-07-27 stsp
2924 a5edda0a 2019-07-27 stsp static const struct got_error *
2925 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2926 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2927 a5edda0a 2019-07-27 stsp {
2928 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2929 a5edda0a 2019-07-27 stsp char *path;
2930 a5edda0a 2019-07-27 stsp int i;
2931 a5edda0a 2019-07-27 stsp
2932 a5edda0a 2019-07-27 stsp if (argc == 0) {
2933 a5edda0a 2019-07-27 stsp path = strdup("");
2934 a5edda0a 2019-07-27 stsp if (path == NULL)
2935 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2936 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2937 a5edda0a 2019-07-27 stsp }
2938 a5edda0a 2019-07-27 stsp
2939 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2940 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2941 a5edda0a 2019-07-27 stsp if (err)
2942 a5edda0a 2019-07-27 stsp break;
2943 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2944 a5edda0a 2019-07-27 stsp if (err) {
2945 a5edda0a 2019-07-27 stsp free(path);
2946 a5edda0a 2019-07-27 stsp break;
2947 a5edda0a 2019-07-27 stsp }
2948 a5edda0a 2019-07-27 stsp }
2949 a5edda0a 2019-07-27 stsp
2950 a5edda0a 2019-07-27 stsp return err;
2951 a1fb16d8 2019-05-24 stsp }
2952 a1fb16d8 2019-05-24 stsp
2953 a1fb16d8 2019-05-24 stsp static const struct got_error *
2954 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
2955 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
2956 fa51e947 2020-03-27 stsp {
2957 fa51e947 2020-03-27 stsp const struct got_error *err;
2958 fa51e947 2020-03-27 stsp struct got_repository *repo;
2959 fa51e947 2020-03-27 stsp static char msg[512];
2960 fa51e947 2020-03-27 stsp
2961 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
2962 fa51e947 2020-03-27 stsp if (err)
2963 fa51e947 2020-03-27 stsp return orig_err;
2964 fa51e947 2020-03-27 stsp
2965 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
2966 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
2967 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
2968 fa51e947 2020-03-27 stsp "'got checkout'.\n"
2969 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
2970 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2971 fa51e947 2020-03-27 stsp got_repo_close(repo);
2972 fa51e947 2020-03-27 stsp return err;
2973 fa51e947 2020-03-27 stsp }
2974 fa51e947 2020-03-27 stsp
2975 fa51e947 2020-03-27 stsp static const struct got_error *
2976 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2977 507dc3bb 2018-12-29 stsp {
2978 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2979 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2980 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2981 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2982 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2983 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2984 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2985 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2986 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2987 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2988 9627c110 2020-04-18 stsp int ch;
2989 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
2990 507dc3bb 2018-12-29 stsp
2991 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2992 f2ea84fa 2019-07-27 stsp
2993 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2994 507dc3bb 2018-12-29 stsp switch (ch) {
2995 024e9686 2019-05-14 stsp case 'b':
2996 024e9686 2019-05-14 stsp branch_name = optarg;
2997 024e9686 2019-05-14 stsp break;
2998 507dc3bb 2018-12-29 stsp case 'c':
2999 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
3000 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
3001 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
3002 507dc3bb 2018-12-29 stsp break;
3003 507dc3bb 2018-12-29 stsp default:
3004 2deda0b9 2019-03-07 stsp usage_update();
3005 507dc3bb 2018-12-29 stsp /* NOTREACHED */
3006 507dc3bb 2018-12-29 stsp }
3007 507dc3bb 2018-12-29 stsp }
3008 507dc3bb 2018-12-29 stsp
3009 507dc3bb 2018-12-29 stsp argc -= optind;
3010 507dc3bb 2018-12-29 stsp argv += optind;
3011 507dc3bb 2018-12-29 stsp
3012 507dc3bb 2018-12-29 stsp #ifndef PROFILE
3013 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3014 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
3015 507dc3bb 2018-12-29 stsp err(1, "pledge");
3016 507dc3bb 2018-12-29 stsp #endif
3017 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
3018 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
3019 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3020 c4cdcb68 2019-04-03 stsp goto done;
3021 c4cdcb68 2019-04-03 stsp }
3022 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
3023 fa51e947 2020-03-27 stsp if (error) {
3024 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3025 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
3026 fa51e947 2020-03-27 stsp worktree_path);
3027 7d5807f4 2019-07-11 stsp goto done;
3028 fa51e947 2020-03-27 stsp }
3029 7d5807f4 2019-07-11 stsp
3030 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
3031 f2ea84fa 2019-07-27 stsp if (error)
3032 f2ea84fa 2019-07-27 stsp goto done;
3033 507dc3bb 2018-12-29 stsp
3034 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3035 c9956ddf 2019-09-08 stsp NULL);
3036 507dc3bb 2018-12-29 stsp if (error != NULL)
3037 507dc3bb 2018-12-29 stsp goto done;
3038 507dc3bb 2018-12-29 stsp
3039 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3040 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3041 087fb88c 2019-08-04 stsp if (error)
3042 087fb88c 2019-08-04 stsp goto done;
3043 087fb88c 2019-08-04 stsp
3044 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3045 0266afb7 2019-01-04 stsp if (error)
3046 0266afb7 2019-01-04 stsp goto done;
3047 0266afb7 2019-01-04 stsp
3048 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3049 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
3050 024e9686 2019-05-14 stsp if (error != NULL)
3051 024e9686 2019-05-14 stsp goto done;
3052 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
3053 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3054 9c4b8182 2019-01-02 stsp if (error != NULL)
3055 9c4b8182 2019-01-02 stsp goto done;
3056 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3057 507dc3bb 2018-12-29 stsp if (error != NULL)
3058 507dc3bb 2018-12-29 stsp goto done;
3059 507dc3bb 2018-12-29 stsp } else {
3060 84de9106 2020-12-26 stsp struct got_reflist_head refs;
3061 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3062 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3063 84de9106 2020-12-26 stsp NULL);
3064 84de9106 2020-12-26 stsp if (error)
3065 84de9106 2020-12-26 stsp goto done;
3066 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3067 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3068 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
3069 04f57cb3 2019-07-25 stsp free(commit_id_str);
3070 bb787f09 2019-07-25 stsp commit_id_str = NULL;
3071 30837e32 2019-07-25 stsp if (error)
3072 a297e751 2019-07-11 stsp goto done;
3073 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
3074 a297e751 2019-07-11 stsp if (error)
3075 507dc3bb 2018-12-29 stsp goto done;
3076 507dc3bb 2018-12-29 stsp }
3077 35c965b2 2018-12-29 stsp
3078 a1fb16d8 2019-05-24 stsp if (branch_name) {
3079 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
3080 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3081 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3082 f2ea84fa 2019-07-27 stsp continue;
3083 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3084 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3085 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3086 024e9686 2019-05-14 stsp goto done;
3087 024e9686 2019-05-14 stsp }
3088 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3089 024e9686 2019-05-14 stsp if (error)
3090 024e9686 2019-05-14 stsp goto done;
3091 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3092 3aef623b 2019-10-15 stsp repo);
3093 a367ff0f 2019-05-14 stsp free(head_commit_id);
3094 024e9686 2019-05-14 stsp if (error != NULL)
3095 024e9686 2019-05-14 stsp goto done;
3096 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3097 a367ff0f 2019-05-14 stsp if (error)
3098 a367ff0f 2019-05-14 stsp goto done;
3099 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3100 024e9686 2019-05-14 stsp if (error)
3101 024e9686 2019-05-14 stsp goto done;
3102 024e9686 2019-05-14 stsp } else {
3103 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3104 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3105 a367ff0f 2019-05-14 stsp if (error != NULL) {
3106 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3107 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3108 024e9686 2019-05-14 stsp goto done;
3109 a367ff0f 2019-05-14 stsp }
3110 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3111 a367ff0f 2019-05-14 stsp if (error)
3112 a367ff0f 2019-05-14 stsp goto done;
3113 024e9686 2019-05-14 stsp }
3114 507dc3bb 2018-12-29 stsp
3115 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3116 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3117 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3118 507dc3bb 2018-12-29 stsp commit_id);
3119 507dc3bb 2018-12-29 stsp if (error)
3120 507dc3bb 2018-12-29 stsp goto done;
3121 507dc3bb 2018-12-29 stsp }
3122 507dc3bb 2018-12-29 stsp
3123 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3124 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3125 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3126 507dc3bb 2018-12-29 stsp if (error != NULL)
3127 507dc3bb 2018-12-29 stsp goto done;
3128 9c4b8182 2019-01-02 stsp
3129 9627c110 2020-04-18 stsp if (upa.did_something)
3130 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
3131 784955db 2019-01-12 stsp else
3132 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3133 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3134 507dc3bb 2018-12-29 stsp done:
3135 c09a553d 2018-03-12 stsp free(worktree_path);
3136 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3137 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3138 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3139 507dc3bb 2018-12-29 stsp free(commit_id);
3140 9c4b8182 2019-01-02 stsp free(commit_id_str);
3141 c09a553d 2018-03-12 stsp return error;
3142 c09a553d 2018-03-12 stsp }
3143 c09a553d 2018-03-12 stsp
3144 f42b1b34 2018-03-12 stsp static const struct got_error *
3145 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3146 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3147 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3148 5c860e29 2018-03-12 stsp {
3149 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3150 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3151 79109fed 2018-03-27 stsp
3152 44392932 2019-08-25 stsp if (blob_id1) {
3153 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3154 44392932 2019-08-25 stsp if (err)
3155 44392932 2019-08-25 stsp goto done;
3156 44392932 2019-08-25 stsp }
3157 79109fed 2018-03-27 stsp
3158 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3159 44392932 2019-08-25 stsp if (err)
3160 44392932 2019-08-25 stsp goto done;
3161 79109fed 2018-03-27 stsp
3162 44392932 2019-08-25 stsp while (path[0] == '/')
3163 44392932 2019-08-25 stsp path++;
3164 fe621944 2020-11-10 stsp err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3165 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, stdout);
3166 44392932 2019-08-25 stsp done:
3167 44392932 2019-08-25 stsp if (blob1)
3168 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3169 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3170 44392932 2019-08-25 stsp return err;
3171 44392932 2019-08-25 stsp }
3172 44392932 2019-08-25 stsp
3173 44392932 2019-08-25 stsp static const struct got_error *
3174 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3175 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3176 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3177 44392932 2019-08-25 stsp {
3178 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3179 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3180 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3181 44392932 2019-08-25 stsp
3182 44392932 2019-08-25 stsp if (tree_id1) {
3183 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3184 79109fed 2018-03-27 stsp if (err)
3185 44392932 2019-08-25 stsp goto done;
3186 79109fed 2018-03-27 stsp }
3187 79109fed 2018-03-27 stsp
3188 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3189 0f2b3dca 2018-12-22 stsp if (err)
3190 0f2b3dca 2018-12-22 stsp goto done;
3191 0f2b3dca 2018-12-22 stsp
3192 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3193 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3194 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
3195 aaa13589 2019-06-01 stsp arg.outfile = stdout;
3196 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
3197 fe621944 2020-11-10 stsp arg.nlines = 0;
3198 44392932 2019-08-25 stsp while (path[0] == '/')
3199 44392932 2019-08-25 stsp path++;
3200 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
3201 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3202 0f2b3dca 2018-12-22 stsp done:
3203 79109fed 2018-03-27 stsp if (tree1)
3204 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3205 366e0a5f 2019-10-10 stsp if (tree2)
3206 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3207 44392932 2019-08-25 stsp return err;
3208 44392932 2019-08-25 stsp }
3209 44392932 2019-08-25 stsp
3210 44392932 2019-08-25 stsp static const struct got_error *
3211 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3212 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3213 0208f208 2020-05-05 stsp {
3214 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3215 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3216 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3217 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3218 0208f208 2020-05-05 stsp
3219 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3220 0208f208 2020-05-05 stsp if (qid != NULL) {
3221 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3222 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3223 0208f208 2020-05-05 stsp qid->id);
3224 0208f208 2020-05-05 stsp if (err)
3225 0208f208 2020-05-05 stsp return err;
3226 0208f208 2020-05-05 stsp
3227 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3228 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3229 0208f208 2020-05-05 stsp
3230 0208f208 2020-05-05 stsp }
3231 0208f208 2020-05-05 stsp
3232 0208f208 2020-05-05 stsp if (tree_id1) {
3233 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3234 0208f208 2020-05-05 stsp if (err)
3235 0208f208 2020-05-05 stsp goto done;
3236 0208f208 2020-05-05 stsp }
3237 0208f208 2020-05-05 stsp
3238 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3239 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3240 0208f208 2020-05-05 stsp if (err)
3241 0208f208 2020-05-05 stsp goto done;
3242 0208f208 2020-05-05 stsp
3243 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3244 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3245 0208f208 2020-05-05 stsp done:
3246 0208f208 2020-05-05 stsp if (tree1)
3247 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3248 0208f208 2020-05-05 stsp if (tree2)
3249 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3250 0208f208 2020-05-05 stsp return err;
3251 0208f208 2020-05-05 stsp }
3252 0208f208 2020-05-05 stsp
3253 0208f208 2020-05-05 stsp static const struct got_error *
3254 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3255 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3256 44392932 2019-08-25 stsp {
3257 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3258 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3259 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3260 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3261 44392932 2019-08-25 stsp struct got_object_qid *qid;
3262 44392932 2019-08-25 stsp
3263 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3264 44392932 2019-08-25 stsp if (qid != NULL) {
3265 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3266 44392932 2019-08-25 stsp qid->id);
3267 44392932 2019-08-25 stsp if (err)
3268 44392932 2019-08-25 stsp return err;
3269 44392932 2019-08-25 stsp }
3270 44392932 2019-08-25 stsp
3271 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3272 44392932 2019-08-25 stsp int obj_type;
3273 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3274 44392932 2019-08-25 stsp if (err)
3275 44392932 2019-08-25 stsp goto done;
3276 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3277 44392932 2019-08-25 stsp if (err) {
3278 44392932 2019-08-25 stsp free(obj_id2);
3279 44392932 2019-08-25 stsp goto done;
3280 44392932 2019-08-25 stsp }
3281 44392932 2019-08-25 stsp if (pcommit) {
3282 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3283 44392932 2019-08-25 stsp qid->id, path);
3284 44392932 2019-08-25 stsp if (err) {
3285 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3286 2e8c69d1 2020-05-04 stsp free(obj_id2);
3287 2e8c69d1 2020-05-04 stsp goto done;
3288 2e8c69d1 2020-05-04 stsp }
3289 2e8c69d1 2020-05-04 stsp } else {
3290 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3291 2e8c69d1 2020-05-04 stsp if (err) {
3292 2e8c69d1 2020-05-04 stsp free(obj_id2);
3293 2e8c69d1 2020-05-04 stsp goto done;
3294 2e8c69d1 2020-05-04 stsp }
3295 44392932 2019-08-25 stsp }
3296 44392932 2019-08-25 stsp }
3297 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3298 44392932 2019-08-25 stsp if (err) {
3299 44392932 2019-08-25 stsp free(obj_id2);
3300 44392932 2019-08-25 stsp goto done;
3301 44392932 2019-08-25 stsp }
3302 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3303 44392932 2019-08-25 stsp switch (obj_type) {
3304 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3305 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3306 64453f7e 2020-11-21 stsp 0, 0, repo);
3307 44392932 2019-08-25 stsp break;
3308 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3309 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3310 64453f7e 2020-11-21 stsp 0, 0, repo);
3311 44392932 2019-08-25 stsp break;
3312 44392932 2019-08-25 stsp default:
3313 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3314 44392932 2019-08-25 stsp break;
3315 44392932 2019-08-25 stsp }
3316 44392932 2019-08-25 stsp free(obj_id1);
3317 44392932 2019-08-25 stsp free(obj_id2);
3318 44392932 2019-08-25 stsp } else {
3319 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3320 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3321 44392932 2019-08-25 stsp if (err)
3322 44392932 2019-08-25 stsp goto done;
3323 579bd556 2020-10-24 stsp if (pcommit) {
3324 579bd556 2020-10-24 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3325 579bd556 2020-10-24 stsp err = got_object_id_str(&id_str1, obj_id1);
3326 579bd556 2020-10-24 stsp if (err)
3327 579bd556 2020-10-24 stsp goto done;
3328 579bd556 2020-10-24 stsp }
3329 64453f7e 2020-11-21 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3330 64453f7e 2020-11-21 stsp id_str2);
3331 64453f7e 2020-11-21 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3332 64453f7e 2020-11-21 stsp repo);
3333 44392932 2019-08-25 stsp }
3334 44392932 2019-08-25 stsp done:
3335 0f2b3dca 2018-12-22 stsp free(id_str1);
3336 0f2b3dca 2018-12-22 stsp free(id_str2);
3337 44392932 2019-08-25 stsp if (pcommit)
3338 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3339 79109fed 2018-03-27 stsp return err;
3340 79109fed 2018-03-27 stsp }
3341 79109fed 2018-03-27 stsp
3342 4bb494d5 2018-06-16 stsp static char *
3343 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3344 6c281f94 2018-06-11 stsp {
3345 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3346 09867e48 2019-08-13 stsp char *p, *s;
3347 09867e48 2019-08-13 stsp
3348 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3349 09867e48 2019-08-13 stsp if (tm == NULL)
3350 09867e48 2019-08-13 stsp return NULL;
3351 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3352 09867e48 2019-08-13 stsp if (s == NULL)
3353 09867e48 2019-08-13 stsp return NULL;
3354 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3355 6c281f94 2018-06-11 stsp if (p)
3356 6c281f94 2018-06-11 stsp *p = '\0';
3357 6c281f94 2018-06-11 stsp return s;
3358 6c281f94 2018-06-11 stsp }
3359 dc424a06 2019-08-07 stsp
3360 6841bf13 2019-11-29 kn static const struct got_error *
3361 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3362 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3363 6841bf13 2019-11-29 kn {
3364 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3365 6841bf13 2019-11-29 kn regmatch_t regmatch;
3366 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3367 6841bf13 2019-11-29 kn
3368 6841bf13 2019-11-29 kn *have_match = 0;
3369 6841bf13 2019-11-29 kn
3370 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3371 6841bf13 2019-11-29 kn if (err)
3372 6841bf13 2019-11-29 kn return err;
3373 6841bf13 2019-11-29 kn
3374 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3375 6841bf13 2019-11-29 kn if (err)
3376 6841bf13 2019-11-29 kn goto done;
3377 6841bf13 2019-11-29 kn
3378 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3379 6841bf13 2019-11-29 kn *have_match = 1;
3380 6841bf13 2019-11-29 kn done:
3381 6841bf13 2019-11-29 kn free(id_str);
3382 6841bf13 2019-11-29 kn free(logmsg);
3383 6841bf13 2019-11-29 kn return err;
3384 6841bf13 2019-11-29 kn }
3385 6841bf13 2019-11-29 kn
3386 0208f208 2020-05-05 stsp static void
3387 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3388 0208f208 2020-05-05 stsp regex_t *regex)
3389 0208f208 2020-05-05 stsp {
3390 0208f208 2020-05-05 stsp regmatch_t regmatch;
3391 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3392 0208f208 2020-05-05 stsp
3393 0208f208 2020-05-05 stsp *have_match = 0;
3394 0208f208 2020-05-05 stsp
3395 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3396 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3397 0208f208 2020-05-05 stsp *have_match = 1;
3398 0208f208 2020-05-05 stsp break;
3399 0208f208 2020-05-05 stsp }
3400 0208f208 2020-05-05 stsp }
3401 0208f208 2020-05-05 stsp }
3402 0208f208 2020-05-05 stsp
3403 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3404 6c281f94 2018-06-11 stsp
3405 888b7d99 2020-12-26 stsp static const struct got_error*
3406 888b7d99 2020-12-26 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
3407 888b7d99 2020-12-26 stsp struct got_object_id *id, struct got_repository *repo)
3408 888b7d99 2020-12-26 stsp {
3409 888b7d99 2020-12-26 stsp static const struct got_error *err = NULL;
3410 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3411 888b7d99 2020-12-26 stsp char *s;
3412 888b7d99 2020-12-26 stsp const char *name;
3413 5c860e29 2018-03-12 stsp
3414 888b7d99 2020-12-26 stsp *refs_str = NULL;
3415 888b7d99 2020-12-26 stsp
3416 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
3417 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3418 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3419 a436ad14 2019-08-13 stsp int cmp;
3420 a436ad14 2019-08-13 stsp
3421 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3422 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3423 d9498b20 2019-02-05 stsp continue;
3424 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3425 199a4027 2019-02-02 stsp name += 5;
3426 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3427 7143d404 2019-03-12 stsp continue;
3428 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3429 e34f9ed6 2019-02-02 stsp name += 6;
3430 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3431 141c2bff 2019-02-04 stsp name += 8;
3432 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3433 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3434 4343a07f 2020-04-24 stsp continue;
3435 4343a07f 2020-04-24 stsp }
3436 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3437 48cae60d 2020-09-22 stsp if (err)
3438 888b7d99 2020-12-26 stsp break;
3439 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3440 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3441 5d844a1e 2019-08-13 stsp if (err) {
3442 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3443 48cae60d 2020-09-22 stsp free(ref_id);
3444 888b7d99 2020-12-26 stsp break;
3445 48cae60d 2020-09-22 stsp }
3446 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3447 5d844a1e 2019-08-13 stsp err = NULL;
3448 5d844a1e 2019-08-13 stsp tag = NULL;
3449 5d844a1e 2019-08-13 stsp }
3450 a436ad14 2019-08-13 stsp }
3451 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3452 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3453 48cae60d 2020-09-22 stsp free(ref_id);
3454 a436ad14 2019-08-13 stsp if (tag)
3455 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3456 a436ad14 2019-08-13 stsp if (cmp != 0)
3457 a436ad14 2019-08-13 stsp continue;
3458 888b7d99 2020-12-26 stsp s = *refs_str;
3459 888b7d99 2020-12-26 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
3460 888b7d99 2020-12-26 stsp s ? ", " : "", name) == -1) {
3461 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3462 199a4027 2019-02-02 stsp free(s);
3463 888b7d99 2020-12-26 stsp *refs_str = NULL;
3464 888b7d99 2020-12-26 stsp break;
3465 199a4027 2019-02-02 stsp }
3466 199a4027 2019-02-02 stsp free(s);
3467 199a4027 2019-02-02 stsp }
3468 888b7d99 2020-12-26 stsp
3469 888b7d99 2020-12-26 stsp return err;
3470 888b7d99 2020-12-26 stsp }
3471 888b7d99 2020-12-26 stsp
3472 888b7d99 2020-12-26 stsp static const struct got_error *
3473 888b7d99 2020-12-26 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3474 888b7d99 2020-12-26 stsp struct got_repository *repo, const char *path,
3475 888b7d99 2020-12-26 stsp struct got_pathlist_head *changed_paths, int show_patch,
3476 888b7d99 2020-12-26 stsp int diff_context, struct got_reflist_object_id_map *refs_idmap)
3477 888b7d99 2020-12-26 stsp {
3478 888b7d99 2020-12-26 stsp const struct got_error *err = NULL;
3479 888b7d99 2020-12-26 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3480 888b7d99 2020-12-26 stsp char datebuf[26];
3481 888b7d99 2020-12-26 stsp time_t committer_time;
3482 888b7d99 2020-12-26 stsp const char *author, *committer;
3483 888b7d99 2020-12-26 stsp char *refs_str = NULL;
3484 888b7d99 2020-12-26 stsp struct got_reflist_head *refs;
3485 888b7d99 2020-12-26 stsp
3486 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3487 8bf5b3c9 2018-03-17 stsp if (err)
3488 8bf5b3c9 2018-03-17 stsp return err;
3489 788c352e 2018-06-16 stsp
3490 888b7d99 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3491 888b7d99 2020-12-26 stsp if (refs) {
3492 888b7d99 2020-12-26 stsp err = build_refs_str(&refs_str, refs, id, repo);
3493 888b7d99 2020-12-26 stsp if (err)
3494 888b7d99 2020-12-26 stsp goto done;
3495 888b7d99 2020-12-26 stsp }
3496 888b7d99 2020-12-26 stsp
3497 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3498 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3499 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3500 832c249c 2018-06-10 stsp free(id_str);
3501 d3d493d7 2019-02-21 stsp id_str = NULL;
3502 d3d493d7 2019-02-21 stsp free(refs_str);
3503 d3d493d7 2019-02-21 stsp refs_str = NULL;
3504 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3505 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3506 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3507 09867e48 2019-08-13 stsp if (datestr)
3508 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3509 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3510 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3511 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3512 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3513 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3514 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3515 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3516 3fe1abad 2018-06-10 stsp int n = 1;
3517 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3518 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3519 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3520 a0603db2 2018-06-10 stsp if (err)
3521 888b7d99 2020-12-26 stsp goto done;
3522 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3523 a0603db2 2018-06-10 stsp free(id_str);
3524 888b7d99 2020-12-26 stsp id_str = NULL;
3525 a0603db2 2018-06-10 stsp }
3526 a0603db2 2018-06-10 stsp }
3527 832c249c 2018-06-10 stsp
3528 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3529 5943eee2 2019-08-13 stsp if (err)
3530 888b7d99 2020-12-26 stsp goto done;
3531 8bf5b3c9 2018-03-17 stsp
3532 621015ac 2018-07-23 stsp logmsg = logmsg0;
3533 832c249c 2018-06-10 stsp do {
3534 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3535 832c249c 2018-06-10 stsp if (line)
3536 832c249c 2018-06-10 stsp printf(" %s\n", line);
3537 832c249c 2018-06-10 stsp } while (line);
3538 621015ac 2018-07-23 stsp free(logmsg0);
3539 832c249c 2018-06-10 stsp
3540 0208f208 2020-05-05 stsp if (changed_paths) {
3541 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3542 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3543 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3544 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3545 0208f208 2020-05-05 stsp }
3546 0208f208 2020-05-05 stsp printf("\n");
3547 0208f208 2020-05-05 stsp }
3548 971751ac 2018-03-27 stsp if (show_patch) {
3549 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3550 971751ac 2018-03-27 stsp if (err == 0)
3551 971751ac 2018-03-27 stsp printf("\n");
3552 971751ac 2018-03-27 stsp }
3553 07862c20 2018-09-15 stsp
3554 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3555 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3556 888b7d99 2020-12-26 stsp done:
3557 888b7d99 2020-12-26 stsp free(id_str);
3558 888b7d99 2020-12-26 stsp free(refs_str);
3559 07862c20 2018-09-15 stsp return err;
3560 f42b1b34 2018-03-12 stsp }
3561 5c860e29 2018-03-12 stsp
3562 f42b1b34 2018-03-12 stsp static const struct got_error *
3563 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3564 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3565 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3566 888b7d99 2020-12-26 stsp int log_branches, int reverse_display_order,
3567 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap)
3568 f42b1b34 2018-03-12 stsp {
3569 f42b1b34 2018-03-12 stsp const struct got_error *err;
3570 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3571 6841bf13 2019-11-29 kn regex_t regex;
3572 6841bf13 2019-11-29 kn int have_match;
3573 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3574 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3575 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3576 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3577 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3578 dbec59df 2020-04-18 stsp
3579 dbec59df 2020-04-18 stsp SIMPLEQ_INIT(&reversed_commits);
3580 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3581 372ccdbb 2018-06-10 stsp
3582 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(&regex, search_pattern,
3583 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3584 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3585 6841bf13 2019-11-29 kn
3586 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3587 f42b1b34 2018-03-12 stsp if (err)
3588 f42b1b34 2018-03-12 stsp return err;
3589 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3590 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3591 372ccdbb 2018-06-10 stsp if (err)
3592 fcc85cad 2018-07-22 stsp goto done;
3593 656b1f76 2019-05-11 jcs for (;;) {
3594 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3595 8bf5b3c9 2018-03-17 stsp
3596 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3597 84453469 2018-11-11 stsp break;
3598 84453469 2018-11-11 stsp
3599 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3600 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3601 372ccdbb 2018-06-10 stsp if (err) {
3602 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3603 9ba79e04 2018-06-11 stsp err = NULL;
3604 ee780d5c 2020-01-04 stsp break;
3605 7e665116 2018-04-02 stsp }
3606 b43fbaa0 2018-06-11 stsp if (id == NULL)
3607 7e665116 2018-04-02 stsp break;
3608 b43fbaa0 2018-06-11 stsp
3609 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3610 b43fbaa0 2018-06-11 stsp if (err)
3611 fcc85cad 2018-07-22 stsp break;
3612 6841bf13 2019-11-29 kn
3613 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3614 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3615 0208f208 2020-05-05 stsp if (err)
3616 0208f208 2020-05-05 stsp break;
3617 0208f208 2020-05-05 stsp }
3618 0208f208 2020-05-05 stsp
3619 6841bf13 2019-11-29 kn if (search_pattern) {
3620 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3621 6841bf13 2019-11-29 kn if (err) {
3622 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3623 6841bf13 2019-11-29 kn break;
3624 6841bf13 2019-11-29 kn }
3625 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3626 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3627 0208f208 2020-05-05 stsp &changed_paths, &regex);
3628 6841bf13 2019-11-29 kn if (have_match == 0) {
3629 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3630 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3631 0208f208 2020-05-05 stsp free((char *)pe->path);
3632 0208f208 2020-05-05 stsp free(pe->data);
3633 0208f208 2020-05-05 stsp }
3634 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3635 6841bf13 2019-11-29 kn continue;
3636 6841bf13 2019-11-29 kn }
3637 6841bf13 2019-11-29 kn }
3638 6841bf13 2019-11-29 kn
3639 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3640 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3641 dbec59df 2020-04-18 stsp if (err)
3642 dbec59df 2020-04-18 stsp break;
3643 dbec59df 2020-04-18 stsp SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3644 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3645 dbec59df 2020-04-18 stsp } else {
3646 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3647 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3648 888b7d99 2020-12-26 stsp show_patch, diff_context, refs_idmap);
3649 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3650 dbec59df 2020-04-18 stsp if (err)
3651 dbec59df 2020-04-18 stsp break;
3652 dbec59df 2020-04-18 stsp }
3653 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3654 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3655 7e665116 2018-04-02 stsp break;
3656 0208f208 2020-05-05 stsp
3657 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3658 0208f208 2020-05-05 stsp free((char *)pe->path);
3659 0208f208 2020-05-05 stsp free(pe->data);
3660 0208f208 2020-05-05 stsp }
3661 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3662 31cedeaf 2018-09-15 stsp }
3663 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3664 dbec59df 2020-04-18 stsp SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3665 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3666 dbec59df 2020-04-18 stsp if (err)
3667 dbec59df 2020-04-18 stsp break;
3668 502b9684 2020-07-31 stsp if (show_changed_paths) {
3669 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3670 502b9684 2020-07-31 stsp commit, repo);
3671 502b9684 2020-07-31 stsp if (err)
3672 502b9684 2020-07-31 stsp break;
3673 502b9684 2020-07-31 stsp }
3674 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3675 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3676 888b7d99 2020-12-26 stsp show_patch, diff_context, refs_idmap);
3677 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3678 dbec59df 2020-04-18 stsp if (err)
3679 dbec59df 2020-04-18 stsp break;
3680 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3681 502b9684 2020-07-31 stsp free((char *)pe->path);
3682 502b9684 2020-07-31 stsp free(pe->data);
3683 502b9684 2020-07-31 stsp }
3684 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3685 dbec59df 2020-04-18 stsp }
3686 dbec59df 2020-04-18 stsp }
3687 fcc85cad 2018-07-22 stsp done:
3688 dbec59df 2020-04-18 stsp while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3689 dbec59df 2020-04-18 stsp qid = SIMPLEQ_FIRST(&reversed_commits);
3690 dbec59df 2020-04-18 stsp SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3691 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3692 dbec59df 2020-04-18 stsp }
3693 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3694 0208f208 2020-05-05 stsp free((char *)pe->path);
3695 0208f208 2020-05-05 stsp free(pe->data);
3696 0208f208 2020-05-05 stsp }
3697 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3698 6841bf13 2019-11-29 kn if (search_pattern)
3699 6841bf13 2019-11-29 kn regfree(&regex);
3700 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3701 f42b1b34 2018-03-12 stsp return err;
3702 f42b1b34 2018-03-12 stsp }
3703 5c860e29 2018-03-12 stsp
3704 4ed7e80c 2018-05-20 stsp __dead static void
3705 6f3d1eb0 2018-03-12 stsp usage_log(void)
3706 6f3d1eb0 2018-03-12 stsp {
3707 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3708 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3709 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3710 6f3d1eb0 2018-03-12 stsp exit(1);
3711 b1ebc001 2019-08-13 stsp }
3712 b1ebc001 2019-08-13 stsp
3713 b1ebc001 2019-08-13 stsp static int
3714 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3715 b1ebc001 2019-08-13 stsp {
3716 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3717 b1ebc001 2019-08-13 stsp long long n;
3718 b1ebc001 2019-08-13 stsp const char *errstr;
3719 b1ebc001 2019-08-13 stsp
3720 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3721 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3722 b1ebc001 2019-08-13 stsp return 0;
3723 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3724 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3725 b1ebc001 2019-08-13 stsp return 0;
3726 b1ebc001 2019-08-13 stsp return n;
3727 6f3d1eb0 2018-03-12 stsp }
3728 6f3d1eb0 2018-03-12 stsp
3729 4ed7e80c 2018-05-20 stsp static const struct got_error *
3730 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3731 f42b1b34 2018-03-12 stsp {
3732 f42b1b34 2018-03-12 stsp const struct got_error *error;
3733 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3734 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3735 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3736 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3737 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3738 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3739 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3740 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3741 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3742 64a96a6d 2018-04-01 stsp const char *errstr;
3743 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3744 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
3745 1b3893a2 2019-03-18 stsp
3746 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3747 5c860e29 2018-03-12 stsp
3748 6715a751 2018-03-16 stsp #ifndef PROFILE
3749 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3750 6098196c 2019-01-04 stsp NULL)
3751 ad242220 2018-09-08 stsp == -1)
3752 f42b1b34 2018-03-12 stsp err(1, "pledge");
3753 6715a751 2018-03-16 stsp #endif
3754 79109fed 2018-03-27 stsp
3755 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3756 b1ebc001 2019-08-13 stsp
3757 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3758 79109fed 2018-03-27 stsp switch (ch) {
3759 79109fed 2018-03-27 stsp case 'p':
3760 79109fed 2018-03-27 stsp show_patch = 1;
3761 d142fc45 2018-04-01 stsp break;
3762 0208f208 2020-05-05 stsp case 'P':
3763 0208f208 2020-05-05 stsp show_changed_paths = 1;
3764 0208f208 2020-05-05 stsp break;
3765 d142fc45 2018-04-01 stsp case 'c':
3766 d142fc45 2018-04-01 stsp start_commit = optarg;
3767 64a96a6d 2018-04-01 stsp break;
3768 c0cc5c62 2018-10-18 stsp case 'C':
3769 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3770 4a8520aa 2018-10-18 stsp &errstr);
3771 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3772 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3773 c0cc5c62 2018-10-18 stsp break;
3774 64a96a6d 2018-04-01 stsp case 'l':
3775 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3776 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3777 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3778 cc54c501 2019-07-15 stsp break;
3779 48c8c60d 2020-01-27 stsp case 'b':
3780 48c8c60d 2020-01-27 stsp log_branches = 1;
3781 a0603db2 2018-06-10 stsp break;
3782 04ca23f4 2018-07-16 stsp case 'r':
3783 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3784 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3785 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3786 9ba1d308 2019-10-21 stsp optarg);
3787 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3788 04ca23f4 2018-07-16 stsp break;
3789 dbec59df 2020-04-18 stsp case 'R':
3790 dbec59df 2020-04-18 stsp reverse_display_order = 1;
3791 dbec59df 2020-04-18 stsp break;
3792 6841bf13 2019-11-29 kn case 's':
3793 6841bf13 2019-11-29 kn search_pattern = optarg;
3794 6841bf13 2019-11-29 kn break;
3795 d1fe46f9 2020-04-18 stsp case 'x':
3796 d1fe46f9 2020-04-18 stsp end_commit = optarg;
3797 d1fe46f9 2020-04-18 stsp break;
3798 79109fed 2018-03-27 stsp default:
3799 2deda0b9 2019-03-07 stsp usage_log();
3800 79109fed 2018-03-27 stsp /* NOTREACHED */
3801 79109fed 2018-03-27 stsp }
3802 79109fed 2018-03-27 stsp }
3803 79109fed 2018-03-27 stsp
3804 79109fed 2018-03-27 stsp argc -= optind;
3805 79109fed 2018-03-27 stsp argv += optind;
3806 f42b1b34 2018-03-12 stsp
3807 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3808 dc1edbfa 2019-11-29 kn diff_context = 3;
3809 dc1edbfa 2019-11-29 kn else if (!show_patch)
3810 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
3811 dc1edbfa 2019-11-29 kn
3812 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3813 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3814 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3815 04ca23f4 2018-07-16 stsp goto done;
3816 04ca23f4 2018-07-16 stsp }
3817 cffc0aa4 2019-02-05 stsp
3818 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
3819 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
3820 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3821 50f2fada 2020-04-24 stsp goto done;
3822 50f2fada 2020-04-24 stsp error = NULL;
3823 50f2fada 2020-04-24 stsp }
3824 cffc0aa4 2019-02-05 stsp
3825 603cdeb0 2020-10-22 stsp if (argc == 1) {
3826 e7301579 2019-03-18 stsp if (worktree) {
3827 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3828 e7301579 2019-03-18 stsp argv[0]);
3829 e7301579 2019-03-18 stsp if (error)
3830 e7301579 2019-03-18 stsp goto done;
3831 e7301579 2019-03-18 stsp } else {
3832 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3833 e7301579 2019-03-18 stsp if (path == NULL) {
3834 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3835 e7301579 2019-03-18 stsp goto done;
3836 e7301579 2019-03-18 stsp }
3837 e7301579 2019-03-18 stsp }
3838 603cdeb0 2020-10-22 stsp } else if (argc != 0)
3839 cbd1af7a 2019-03-18 stsp usage_log();
3840 cbd1af7a 2019-03-18 stsp
3841 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3842 5486daa2 2019-05-11 stsp repo_path = worktree ?
3843 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3844 5486daa2 2019-05-11 stsp }
3845 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3846 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3847 cffc0aa4 2019-02-05 stsp goto done;
3848 04ca23f4 2018-07-16 stsp }
3849 6098196c 2019-01-04 stsp
3850 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3851 d7d4f210 2018-03-12 stsp if (error != NULL)
3852 04ca23f4 2018-07-16 stsp goto done;
3853 f42b1b34 2018-03-12 stsp
3854 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3855 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3856 c02c541e 2019-03-29 stsp if (error)
3857 c02c541e 2019-03-29 stsp goto done;
3858 c02c541e 2019-03-29 stsp
3859 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3860 84de9106 2020-12-26 stsp if (error)
3861 84de9106 2020-12-26 stsp goto done;
3862 84de9106 2020-12-26 stsp
3863 888b7d99 2020-12-26 stsp error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
3864 888b7d99 2020-12-26 stsp if (error)
3865 888b7d99 2020-12-26 stsp goto done;
3866 888b7d99 2020-12-26 stsp
3867 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3868 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3869 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
3870 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3871 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3872 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3873 3235492e 2018-04-01 stsp if (error != NULL)
3874 d1fe46f9 2020-04-18 stsp goto done;
3875 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
3876 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3877 3235492e 2018-04-01 stsp if (error != NULL)
3878 d1fe46f9 2020-04-18 stsp goto done;
3879 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
3880 d1fe46f9 2020-04-18 stsp start_id);
3881 d1fe46f9 2020-04-18 stsp if (error != NULL)
3882 d1fe46f9 2020-04-18 stsp goto done;
3883 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
3884 3235492e 2018-04-01 stsp } else {
3885 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&start_id, NULL,
3886 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3887 d1fe46f9 2020-04-18 stsp if (error != NULL)
3888 d1fe46f9 2020-04-18 stsp goto done;
3889 3235492e 2018-04-01 stsp }
3890 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
3891 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&end_id, NULL,
3892 84de9106 2020-12-26 stsp end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3893 d1fe46f9 2020-04-18 stsp if (error != NULL)
3894 d1fe46f9 2020-04-18 stsp goto done;
3895 d1fe46f9 2020-04-18 stsp }
3896 04ca23f4 2018-07-16 stsp
3897 deeabeae 2019-08-27 stsp if (worktree) {
3898 603cdeb0 2020-10-22 stsp /*
3899 603cdeb0 2020-10-22 stsp * If a path was specified on the command line it was resolved
3900 603cdeb0 2020-10-22 stsp * to a path in the work tree above. Prepend the work tree's
3901 603cdeb0 2020-10-22 stsp * path prefix to obtain the corresponding in-repository path.
3902 603cdeb0 2020-10-22 stsp */
3903 603cdeb0 2020-10-22 stsp if (path) {
3904 603cdeb0 2020-10-22 stsp const char *prefix;
3905 603cdeb0 2020-10-22 stsp prefix = got_worktree_get_path_prefix(worktree);
3906 603cdeb0 2020-10-22 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
3907 603cdeb0 2020-10-22 stsp (path[0] != '\0') ? "/" : "", path) == -1) {
3908 603cdeb0 2020-10-22 stsp error = got_error_from_errno("asprintf");
3909 603cdeb0 2020-10-22 stsp goto done;
3910 603cdeb0 2020-10-22 stsp }
3911 603cdeb0 2020-10-22 stsp }
3912 deeabeae 2019-08-27 stsp } else
3913 603cdeb0 2020-10-22 stsp error = got_repo_map_path(&in_repo_path, repo,
3914 8fa913ec 2020-11-14 stsp path ? path : "");
3915 04ca23f4 2018-07-16 stsp if (error != NULL)
3916 04ca23f4 2018-07-16 stsp goto done;
3917 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3918 04ca23f4 2018-07-16 stsp free(path);
3919 04ca23f4 2018-07-16 stsp path = in_repo_path;
3920 04ca23f4 2018-07-16 stsp }
3921 199a4027 2019-02-02 stsp
3922 603cdeb0 2020-10-22 stsp error = print_commits(start_id, end_id, repo, path ? path : "",
3923 603cdeb0 2020-10-22 stsp show_changed_paths, show_patch, search_pattern, diff_context,
3924 888b7d99 2020-12-26 stsp limit, log_branches, reverse_display_order, refs_idmap);
3925 04ca23f4 2018-07-16 stsp done:
3926 04ca23f4 2018-07-16 stsp free(path);
3927 04ca23f4 2018-07-16 stsp free(repo_path);
3928 04ca23f4 2018-07-16 stsp free(cwd);
3929 cffc0aa4 2019-02-05 stsp if (worktree)
3930 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3931 ad242220 2018-09-08 stsp if (repo) {
3932 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3933 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3934 ad242220 2018-09-08 stsp if (error == NULL)
3935 ad242220 2018-09-08 stsp error = repo_error;
3936 ad242220 2018-09-08 stsp }
3937 888b7d99 2020-12-26 stsp if (refs_idmap)
3938 888b7d99 2020-12-26 stsp got_reflist_object_id_map_free(refs_idmap);
3939 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3940 8bf5b3c9 2018-03-17 stsp return error;
3941 5c860e29 2018-03-12 stsp }
3942 5c860e29 2018-03-12 stsp
3943 4ed7e80c 2018-05-20 stsp __dead static void
3944 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3945 3f8b7d6a 2018-04-01 stsp {
3946 64453f7e 2020-11-21 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3947 64453f7e 2020-11-21 stsp "[-s] [-w] [object1 object2 | path]\n", getprogname());
3948 3f8b7d6a 2018-04-01 stsp exit(1);
3949 b00d56cd 2018-04-01 stsp }
3950 b00d56cd 2018-04-01 stsp
3951 b72f483a 2019-02-05 stsp struct print_diff_arg {
3952 b72f483a 2019-02-05 stsp struct got_repository *repo;
3953 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3954 b72f483a 2019-02-05 stsp int diff_context;
3955 3fc0c068 2019-02-10 stsp const char *id_str;
3956 3fc0c068 2019-02-10 stsp int header_shown;
3957 98eaaa12 2019-08-03 stsp int diff_staged;
3958 63035f9f 2019-10-06 stsp int ignore_whitespace;
3959 64453f7e 2020-11-21 stsp int force_text_diff;
3960 b72f483a 2019-02-05 stsp };
3961 39449a05 2020-07-23 stsp
3962 39449a05 2020-07-23 stsp /*
3963 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
3964 39449a05 2020-07-23 stsp * it as content to the diff engine.
3965 39449a05 2020-07-23 stsp */
3966 39449a05 2020-07-23 stsp static const struct got_error *
3967 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3968 39449a05 2020-07-23 stsp const char *abspath)
3969 39449a05 2020-07-23 stsp {
3970 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
3971 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
3972 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
3973 39449a05 2020-07-23 stsp
3974 39449a05 2020-07-23 stsp *fd = -1;
3975 39449a05 2020-07-23 stsp
3976 39449a05 2020-07-23 stsp if (dirfd != -1) {
3977 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3978 39449a05 2020-07-23 stsp if (target_len == -1)
3979 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
3980 39449a05 2020-07-23 stsp } else {
3981 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
3982 39449a05 2020-07-23 stsp if (target_len == -1)
3983 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
3984 39449a05 2020-07-23 stsp }
3985 39449a05 2020-07-23 stsp
3986 39449a05 2020-07-23 stsp *fd = got_opentempfd();
3987 39449a05 2020-07-23 stsp if (*fd == -1)
3988 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
3989 39449a05 2020-07-23 stsp
3990 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
3991 39449a05 2020-07-23 stsp if (outlen == -1) {
3992 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
3993 39449a05 2020-07-23 stsp goto done;
3994 39449a05 2020-07-23 stsp }
3995 39449a05 2020-07-23 stsp
3996 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
3997 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
3998 39449a05 2020-07-23 stsp goto done;
3999 39449a05 2020-07-23 stsp }
4000 39449a05 2020-07-23 stsp done:
4001 39449a05 2020-07-23 stsp if (err) {
4002 39449a05 2020-07-23 stsp close(*fd);
4003 39449a05 2020-07-23 stsp *fd = -1;
4004 39449a05 2020-07-23 stsp }
4005 39449a05 2020-07-23 stsp return err;
4006 39449a05 2020-07-23 stsp }
4007 b72f483a 2019-02-05 stsp
4008 4ed7e80c 2018-05-20 stsp static const struct got_error *
4009 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
4010 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4011 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4012 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4013 b72f483a 2019-02-05 stsp {
4014 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
4015 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
4016 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
4017 12463d8b 2019-12-13 stsp int fd = -1;
4018 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
4019 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
4020 b72f483a 2019-02-05 stsp struct stat sb;
4021 b72f483a 2019-02-05 stsp
4022 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4023 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4024 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4025 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4026 98eaaa12 2019-08-03 stsp return NULL;
4027 98eaaa12 2019-08-03 stsp } else {
4028 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4029 98eaaa12 2019-08-03 stsp return NULL;
4030 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4031 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
4032 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4033 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
4034 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
4035 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
4036 98eaaa12 2019-08-03 stsp return NULL;
4037 98eaaa12 2019-08-03 stsp }
4038 3fc0c068 2019-02-10 stsp
4039 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
4040 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
4041 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
4042 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
4043 3fc0c068 2019-02-10 stsp a->header_shown = 1;
4044 3fc0c068 2019-02-10 stsp }
4045 b72f483a 2019-02-05 stsp
4046 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4047 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
4048 98eaaa12 2019-08-03 stsp switch (staged_status) {
4049 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
4050 98eaaa12 2019-08-03 stsp label1 = path;
4051 98eaaa12 2019-08-03 stsp label2 = path;
4052 98eaaa12 2019-08-03 stsp break;
4053 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
4054 98eaaa12 2019-08-03 stsp label2 = path;
4055 98eaaa12 2019-08-03 stsp break;
4056 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
4057 98eaaa12 2019-08-03 stsp label1 = path;
4058 98eaaa12 2019-08-03 stsp break;
4059 98eaaa12 2019-08-03 stsp default:
4060 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
4061 98eaaa12 2019-08-03 stsp }
4062 fe621944 2020-11-10 stsp return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4063 fe621944 2020-11-10 stsp staged_blob_id, label1, label2, a->diff_context,
4064 64453f7e 2020-11-21 stsp a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4065 98eaaa12 2019-08-03 stsp }
4066 98eaaa12 2019-08-03 stsp
4067 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4068 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
4069 4ce46740 2019-08-08 stsp char *id_str;
4070 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4071 408b4ebc 2019-08-03 stsp 8192);
4072 4ce46740 2019-08-08 stsp if (err)
4073 4ce46740 2019-08-08 stsp goto done;
4074 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4075 4ce46740 2019-08-08 stsp if (err)
4076 4ce46740 2019-08-08 stsp goto done;
4077 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4078 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4079 4ce46740 2019-08-08 stsp free(id_str);
4080 4ce46740 2019-08-08 stsp goto done;
4081 4ce46740 2019-08-08 stsp }
4082 4ce46740 2019-08-08 stsp free(id_str);
4083 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4084 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4085 4ce46740 2019-08-08 stsp if (err)
4086 4ce46740 2019-08-08 stsp goto done;
4087 4ce46740 2019-08-08 stsp }
4088 d00136be 2019-03-26 stsp
4089 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4090 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4091 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4092 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4093 2ec1f75b 2019-03-26 stsp goto done;
4094 2ec1f75b 2019-03-26 stsp }
4095 b72f483a 2019-02-05 stsp
4096 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4097 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4098 12463d8b 2019-12-13 stsp if (fd == -1) {
4099 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4100 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4101 39449a05 2020-07-23 stsp abspath);
4102 39449a05 2020-07-23 stsp goto done;
4103 39449a05 2020-07-23 stsp }
4104 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4105 39449a05 2020-07-23 stsp de_name, abspath);
4106 39449a05 2020-07-23 stsp if (err)
4107 39449a05 2020-07-23 stsp goto done;
4108 12463d8b 2019-12-13 stsp }
4109 12463d8b 2019-12-13 stsp } else {
4110 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4111 12463d8b 2019-12-13 stsp if (fd == -1) {
4112 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4113 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4114 39449a05 2020-07-23 stsp abspath);
4115 39449a05 2020-07-23 stsp goto done;
4116 39449a05 2020-07-23 stsp }
4117 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4118 39449a05 2020-07-23 stsp de_name, abspath);
4119 39449a05 2020-07-23 stsp if (err)
4120 39449a05 2020-07-23 stsp goto done;
4121 12463d8b 2019-12-13 stsp }
4122 12463d8b 2019-12-13 stsp }
4123 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4124 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4125 2ec1f75b 2019-03-26 stsp goto done;
4126 2ec1f75b 2019-03-26 stsp }
4127 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4128 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4129 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4130 2ec1f75b 2019-03-26 stsp goto done;
4131 2ec1f75b 2019-03-26 stsp }
4132 12463d8b 2019-12-13 stsp fd = -1;
4133 2ec1f75b 2019-03-26 stsp } else
4134 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4135 b72f483a 2019-02-05 stsp
4136 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4137 64453f7e 2020-11-21 stsp a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4138 b72f483a 2019-02-05 stsp done:
4139 b72f483a 2019-02-05 stsp if (blob1)
4140 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4141 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4142 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4143 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4144 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4145 b72f483a 2019-02-05 stsp free(abspath);
4146 927df6b7 2019-02-10 stsp return err;
4147 927df6b7 2019-02-10 stsp }
4148 927df6b7 2019-02-10 stsp
4149 927df6b7 2019-02-10 stsp static const struct got_error *
4150 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4151 b00d56cd 2018-04-01 stsp {
4152 b00d56cd 2018-04-01 stsp const struct got_error *error;
4153 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4154 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4155 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4156 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4157 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
4158 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
4159 15a94983 2018-12-23 stsp int type1, type2;
4160 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4161 64453f7e 2020-11-21 stsp int force_text_diff = 0;
4162 c0cc5c62 2018-10-18 stsp const char *errstr;
4163 927df6b7 2019-02-10 stsp char *path = NULL;
4164 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4165 b00d56cd 2018-04-01 stsp
4166 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4167 84de9106 2020-12-26 stsp
4168 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4169 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4170 25eccc22 2019-01-04 stsp NULL) == -1)
4171 b00d56cd 2018-04-01 stsp err(1, "pledge");
4172 b00d56cd 2018-04-01 stsp #endif
4173 b00d56cd 2018-04-01 stsp
4174 64453f7e 2020-11-21 stsp while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4175 b00d56cd 2018-04-01 stsp switch (ch) {
4176 64453f7e 2020-11-21 stsp case 'a':
4177 64453f7e 2020-11-21 stsp force_text_diff = 1;
4178 64453f7e 2020-11-21 stsp break;
4179 c0cc5c62 2018-10-18 stsp case 'C':
4180 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4181 dfcab68b 2019-11-29 kn &errstr);
4182 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4183 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4184 c0cc5c62 2018-10-18 stsp break;
4185 b72f483a 2019-02-05 stsp case 'r':
4186 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4187 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4188 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4189 9ba1d308 2019-10-21 stsp optarg);
4190 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4191 b72f483a 2019-02-05 stsp break;
4192 98eaaa12 2019-08-03 stsp case 's':
4193 98eaaa12 2019-08-03 stsp diff_staged = 1;
4194 63035f9f 2019-10-06 stsp break;
4195 63035f9f 2019-10-06 stsp case 'w':
4196 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4197 98eaaa12 2019-08-03 stsp break;
4198 b00d56cd 2018-04-01 stsp default:
4199 2deda0b9 2019-03-07 stsp usage_diff();
4200 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4201 b00d56cd 2018-04-01 stsp }
4202 b00d56cd 2018-04-01 stsp }
4203 b00d56cd 2018-04-01 stsp
4204 b00d56cd 2018-04-01 stsp argc -= optind;
4205 b00d56cd 2018-04-01 stsp argv += optind;
4206 b00d56cd 2018-04-01 stsp
4207 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4208 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4209 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4210 b72f483a 2019-02-05 stsp goto done;
4211 b72f483a 2019-02-05 stsp }
4212 927df6b7 2019-02-10 stsp if (argc <= 1) {
4213 b72f483a 2019-02-05 stsp if (repo_path)
4214 b72f483a 2019-02-05 stsp errx(1,
4215 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
4216 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4217 fa51e947 2020-03-27 stsp if (error) {
4218 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4219 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
4220 fa51e947 2020-03-27 stsp cwd);
4221 1f03b8da 2020-03-20 stsp goto done;
4222 fa51e947 2020-03-27 stsp }
4223 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4224 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4225 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4226 927df6b7 2019-02-10 stsp goto done;
4227 927df6b7 2019-02-10 stsp }
4228 927df6b7 2019-02-10 stsp if (argc == 1) {
4229 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4230 cbd1af7a 2019-03-18 stsp argv[0]);
4231 927df6b7 2019-02-10 stsp if (error)
4232 927df6b7 2019-02-10 stsp goto done;
4233 927df6b7 2019-02-10 stsp } else {
4234 927df6b7 2019-02-10 stsp path = strdup("");
4235 927df6b7 2019-02-10 stsp if (path == NULL) {
4236 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4237 927df6b7 2019-02-10 stsp goto done;
4238 927df6b7 2019-02-10 stsp }
4239 927df6b7 2019-02-10 stsp }
4240 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4241 98eaaa12 2019-08-03 stsp if (diff_staged)
4242 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4243 98eaaa12 2019-08-03 stsp "objects in repository");
4244 15a94983 2018-12-23 stsp id_str1 = argv[0];
4245 15a94983 2018-12-23 stsp id_str2 = argv[1];
4246 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4247 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4248 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4249 30db809c 2019-06-05 stsp goto done;
4250 1f03b8da 2020-03-20 stsp if (worktree) {
4251 1f03b8da 2020-03-20 stsp repo_path = strdup(
4252 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
4253 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4254 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4255 1f03b8da 2020-03-20 stsp goto done;
4256 1f03b8da 2020-03-20 stsp }
4257 1f03b8da 2020-03-20 stsp } else {
4258 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
4259 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4260 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4261 1f03b8da 2020-03-20 stsp goto done;
4262 1f03b8da 2020-03-20 stsp }
4263 30db809c 2019-06-05 stsp }
4264 30db809c 2019-06-05 stsp }
4265 b00d56cd 2018-04-01 stsp } else
4266 b00d56cd 2018-04-01 stsp usage_diff();
4267 b00d56cd 2018-04-01 stsp
4268 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4269 76089277 2018-04-01 stsp free(repo_path);
4270 b00d56cd 2018-04-01 stsp if (error != NULL)
4271 b00d56cd 2018-04-01 stsp goto done;
4272 b00d56cd 2018-04-01 stsp
4273 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4274 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4275 c02c541e 2019-03-29 stsp if (error)
4276 c02c541e 2019-03-29 stsp goto done;
4277 c02c541e 2019-03-29 stsp
4278 30db809c 2019-06-05 stsp if (argc <= 1) {
4279 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4280 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4281 b72f483a 2019-02-05 stsp char *id_str;
4282 72ea6654 2019-07-27 stsp
4283 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4284 72ea6654 2019-07-27 stsp
4285 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4286 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4287 b72f483a 2019-02-05 stsp if (error)
4288 b72f483a 2019-02-05 stsp goto done;
4289 b72f483a 2019-02-05 stsp arg.repo = repo;
4290 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4291 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4292 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4293 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4294 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4295 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4296 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
4297 b72f483a 2019-02-05 stsp
4298 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4299 72ea6654 2019-07-27 stsp if (error)
4300 72ea6654 2019-07-27 stsp goto done;
4301 72ea6654 2019-07-27 stsp
4302 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
4303 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
4304 b72f483a 2019-02-05 stsp free(id_str);
4305 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4306 b72f483a 2019-02-05 stsp goto done;
4307 b72f483a 2019-02-05 stsp }
4308 84de9106 2020-12-26 stsp
4309 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4310 84de9106 2020-12-26 stsp if (error)
4311 84de9106 2020-12-26 stsp return error;
4312 b72f483a 2019-02-05 stsp
4313 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4314 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4315 0adb7196 2019-08-11 stsp if (error)
4316 0adb7196 2019-08-11 stsp goto done;
4317 b00d56cd 2018-04-01 stsp
4318 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4319 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4320 0adb7196 2019-08-11 stsp if (error)
4321 0adb7196 2019-08-11 stsp goto done;
4322 b00d56cd 2018-04-01 stsp
4323 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4324 15a94983 2018-12-23 stsp if (error)
4325 15a94983 2018-12-23 stsp goto done;
4326 15a94983 2018-12-23 stsp
4327 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4328 15a94983 2018-12-23 stsp if (error)
4329 15a94983 2018-12-23 stsp goto done;
4330 15a94983 2018-12-23 stsp
4331 15a94983 2018-12-23 stsp if (type1 != type2) {
4332 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4333 b00d56cd 2018-04-01 stsp goto done;
4334 b00d56cd 2018-04-01 stsp }
4335 b00d56cd 2018-04-01 stsp
4336 15a94983 2018-12-23 stsp switch (type1) {
4337 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4338 fe621944 2020-11-10 stsp error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4339 64453f7e 2020-11-21 stsp NULL, NULL, diff_context, ignore_whitespace,
4340 64453f7e 2020-11-21 stsp force_text_diff, repo, stdout);
4341 b00d56cd 2018-04-01 stsp break;
4342 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4343 fe621944 2020-11-10 stsp error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4344 64453f7e 2020-11-21 stsp "", "", diff_context, ignore_whitespace, force_text_diff,
4345 64453f7e 2020-11-21 stsp repo, stdout);
4346 b00d56cd 2018-04-01 stsp break;
4347 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4348 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4349 fe621944 2020-11-10 stsp error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4350 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, repo,
4351 64453f7e 2020-11-21 stsp stdout);
4352 b00d56cd 2018-04-01 stsp break;
4353 b00d56cd 2018-04-01 stsp default:
4354 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4355 b00d56cd 2018-04-01 stsp }
4356 b00d56cd 2018-04-01 stsp done:
4357 5e70831e 2019-05-28 stsp free(label1);
4358 5e70831e 2019-05-28 stsp free(label2);
4359 15a94983 2018-12-23 stsp free(id1);
4360 15a94983 2018-12-23 stsp free(id2);
4361 927df6b7 2019-02-10 stsp free(path);
4362 b72f483a 2019-02-05 stsp if (worktree)
4363 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4364 ad242220 2018-09-08 stsp if (repo) {
4365 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4366 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4367 ad242220 2018-09-08 stsp if (error == NULL)
4368 ad242220 2018-09-08 stsp error = repo_error;
4369 ad242220 2018-09-08 stsp }
4370 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4371 b00d56cd 2018-04-01 stsp return error;
4372 404c43c4 2018-06-21 stsp }
4373 404c43c4 2018-06-21 stsp
4374 404c43c4 2018-06-21 stsp __dead static void
4375 404c43c4 2018-06-21 stsp usage_blame(void)
4376 404c43c4 2018-06-21 stsp {
4377 9270e621 2019-02-05 stsp fprintf(stderr,
4378 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4379 404c43c4 2018-06-21 stsp getprogname());
4380 404c43c4 2018-06-21 stsp exit(1);
4381 b00d56cd 2018-04-01 stsp }
4382 b00d56cd 2018-04-01 stsp
4383 28315671 2019-08-14 stsp struct blame_line {
4384 28315671 2019-08-14 stsp int annotated;
4385 28315671 2019-08-14 stsp char *id_str;
4386 82f6abb8 2019-08-14 stsp char *committer;
4387 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4388 28315671 2019-08-14 stsp };
4389 28315671 2019-08-14 stsp
4390 28315671 2019-08-14 stsp struct blame_cb_args {
4391 28315671 2019-08-14 stsp struct blame_line *lines;
4392 28315671 2019-08-14 stsp int nlines;
4393 7ef28ff8 2019-08-14 stsp int nlines_prec;
4394 28315671 2019-08-14 stsp int lineno_cur;
4395 28315671 2019-08-14 stsp off_t *line_offsets;
4396 28315671 2019-08-14 stsp FILE *f;
4397 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4398 28315671 2019-08-14 stsp };
4399 28315671 2019-08-14 stsp
4400 404c43c4 2018-06-21 stsp static const struct got_error *
4401 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4402 28315671 2019-08-14 stsp {
4403 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4404 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4405 28315671 2019-08-14 stsp struct blame_line *bline;
4406 82f6abb8 2019-08-14 stsp char *line = NULL;
4407 28315671 2019-08-14 stsp size_t linesize = 0;
4408 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4409 28315671 2019-08-14 stsp off_t offset;
4410 bcb49d15 2019-08-14 stsp struct tm tm;
4411 bcb49d15 2019-08-14 stsp time_t committer_time;
4412 28315671 2019-08-14 stsp
4413 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4414 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4415 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4416 28315671 2019-08-14 stsp
4417 28315671 2019-08-14 stsp if (sigint_received)
4418 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4419 28315671 2019-08-14 stsp
4420 2839f8b9 2019-08-18 stsp if (lineno == -1)
4421 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4422 2839f8b9 2019-08-18 stsp
4423 28315671 2019-08-14 stsp /* Annotate this line. */
4424 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4425 28315671 2019-08-14 stsp if (bline->annotated)
4426 28315671 2019-08-14 stsp return NULL;
4427 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4428 28315671 2019-08-14 stsp if (err)
4429 28315671 2019-08-14 stsp return err;
4430 82f6abb8 2019-08-14 stsp
4431 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4432 82f6abb8 2019-08-14 stsp if (err)
4433 82f6abb8 2019-08-14 stsp goto done;
4434 82f6abb8 2019-08-14 stsp
4435 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4436 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4437 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4438 bcb49d15 2019-08-14 stsp goto done;
4439 bcb49d15 2019-08-14 stsp }
4440 bcb49d15 2019-08-14 stsp
4441 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4442 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
4443 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
4444 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4445 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
4446 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4447 82f6abb8 2019-08-14 stsp goto done;
4448 82f6abb8 2019-08-14 stsp }
4449 28315671 2019-08-14 stsp bline->annotated = 1;
4450 28315671 2019-08-14 stsp
4451 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4452 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4453 28315671 2019-08-14 stsp if (!bline->annotated)
4454 82f6abb8 2019-08-14 stsp goto done;
4455 28315671 2019-08-14 stsp
4456 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4457 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4458 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4459 82f6abb8 2019-08-14 stsp goto done;
4460 82f6abb8 2019-08-14 stsp }
4461 28315671 2019-08-14 stsp
4462 28315671 2019-08-14 stsp while (bline->annotated) {
4463 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4464 82f6abb8 2019-08-14 stsp size_t len;
4465 82f6abb8 2019-08-14 stsp
4466 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4467 28315671 2019-08-14 stsp if (ferror(a->f))
4468 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4469 28315671 2019-08-14 stsp break;
4470 28315671 2019-08-14 stsp }
4471 82f6abb8 2019-08-14 stsp
4472 82f6abb8 2019-08-14 stsp committer = bline->committer;
4473 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4474 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4475 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4476 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4477 82f6abb8 2019-08-14 stsp if (at)
4478 82f6abb8 2019-08-14 stsp *at = '\0';
4479 82f6abb8 2019-08-14 stsp len = strlen(committer);
4480 82f6abb8 2019-08-14 stsp if (len >= 9)
4481 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4482 28315671 2019-08-14 stsp
4483 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4484 28315671 2019-08-14 stsp if (nl)
4485 28315671 2019-08-14 stsp *nl = '\0';
4486 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4487 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4488 28315671 2019-08-14 stsp
4489 28315671 2019-08-14 stsp a->lineno_cur++;
4490 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4491 28315671 2019-08-14 stsp }
4492 82f6abb8 2019-08-14 stsp done:
4493 82f6abb8 2019-08-14 stsp if (commit)
4494 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4495 28315671 2019-08-14 stsp free(line);
4496 28315671 2019-08-14 stsp return err;
4497 28315671 2019-08-14 stsp }
4498 28315671 2019-08-14 stsp
4499 28315671 2019-08-14 stsp static const struct got_error *
4500 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4501 404c43c4 2018-06-21 stsp {
4502 404c43c4 2018-06-21 stsp const struct got_error *error;
4503 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4504 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4505 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4506 0587e10c 2020-07-23 stsp char *link_target = NULL;
4507 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4508 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4509 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4510 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4511 28315671 2019-08-14 stsp struct blame_cb_args bca;
4512 28315671 2019-08-14 stsp int ch, obj_type, i;
4513 be659d10 2020-11-18 stsp off_t filesize;
4514 90f3c347 2019-08-19 stsp
4515 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4516 404c43c4 2018-06-21 stsp
4517 404c43c4 2018-06-21 stsp #ifndef PROFILE
4518 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4519 36e2fb66 2019-01-04 stsp NULL) == -1)
4520 404c43c4 2018-06-21 stsp err(1, "pledge");
4521 404c43c4 2018-06-21 stsp #endif
4522 404c43c4 2018-06-21 stsp
4523 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4524 404c43c4 2018-06-21 stsp switch (ch) {
4525 404c43c4 2018-06-21 stsp case 'c':
4526 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4527 404c43c4 2018-06-21 stsp break;
4528 66bea077 2018-08-02 stsp case 'r':
4529 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4530 66bea077 2018-08-02 stsp if (repo_path == NULL)
4531 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4532 9ba1d308 2019-10-21 stsp optarg);
4533 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4534 66bea077 2018-08-02 stsp break;
4535 404c43c4 2018-06-21 stsp default:
4536 2deda0b9 2019-03-07 stsp usage_blame();
4537 404c43c4 2018-06-21 stsp /* NOTREACHED */
4538 404c43c4 2018-06-21 stsp }
4539 404c43c4 2018-06-21 stsp }
4540 404c43c4 2018-06-21 stsp
4541 404c43c4 2018-06-21 stsp argc -= optind;
4542 404c43c4 2018-06-21 stsp argv += optind;
4543 404c43c4 2018-06-21 stsp
4544 a39318fd 2018-08-02 stsp if (argc == 1)
4545 404c43c4 2018-06-21 stsp path = argv[0];
4546 a39318fd 2018-08-02 stsp else
4547 404c43c4 2018-06-21 stsp usage_blame();
4548 404c43c4 2018-06-21 stsp
4549 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4550 66bea077 2018-08-02 stsp if (cwd == NULL) {
4551 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4552 66bea077 2018-08-02 stsp goto done;
4553 66bea077 2018-08-02 stsp }
4554 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4555 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4556 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4557 66bea077 2018-08-02 stsp goto done;
4558 0c06baac 2019-02-05 stsp else
4559 0c06baac 2019-02-05 stsp error = NULL;
4560 0c06baac 2019-02-05 stsp if (worktree) {
4561 0c06baac 2019-02-05 stsp repo_path =
4562 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4563 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4564 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4565 1f03b8da 2020-03-20 stsp if (error)
4566 1f03b8da 2020-03-20 stsp goto done;
4567 1f03b8da 2020-03-20 stsp }
4568 0c06baac 2019-02-05 stsp } else {
4569 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4570 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4571 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4572 0c06baac 2019-02-05 stsp goto done;
4573 0c06baac 2019-02-05 stsp }
4574 66bea077 2018-08-02 stsp }
4575 66bea077 2018-08-02 stsp }
4576 36e2fb66 2019-01-04 stsp
4577 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4578 c02c541e 2019-03-29 stsp if (error != NULL)
4579 404c43c4 2018-06-21 stsp goto done;
4580 404c43c4 2018-06-21 stsp
4581 0c06baac 2019-02-05 stsp if (worktree) {
4582 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4583 01740607 2020-11-04 stsp char *p;
4584 01740607 2020-11-04 stsp
4585 01740607 2020-11-04 stsp error = got_worktree_resolve_path(&p, worktree, path);
4586 01740607 2020-11-04 stsp if (error)
4587 01740607 2020-11-04 stsp goto done;
4588 01740607 2020-11-04 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4589 01740607 2020-11-04 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4590 01740607 2020-11-04 stsp p) == -1) {
4591 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4592 01740607 2020-11-04 stsp free(p);
4593 0c06baac 2019-02-05 stsp goto done;
4594 0c06baac 2019-02-05 stsp }
4595 0c06baac 2019-02-05 stsp free(p);
4596 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4597 0c06baac 2019-02-05 stsp } else {
4598 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4599 01740607 2020-11-04 stsp if (error)
4600 01740607 2020-11-04 stsp goto done;
4601 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
4602 0c06baac 2019-02-05 stsp }
4603 0c06baac 2019-02-05 stsp if (error)
4604 66bea077 2018-08-02 stsp goto done;
4605 66bea077 2018-08-02 stsp
4606 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4607 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4608 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4609 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4610 404c43c4 2018-06-21 stsp if (error != NULL)
4611 66bea077 2018-08-02 stsp goto done;
4612 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4613 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4614 404c43c4 2018-06-21 stsp if (error != NULL)
4615 66bea077 2018-08-02 stsp goto done;
4616 404c43c4 2018-06-21 stsp } else {
4617 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4618 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4619 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4620 84de9106 2020-12-26 stsp NULL);
4621 84de9106 2020-12-26 stsp if (error)
4622 84de9106 2020-12-26 stsp goto done;
4623 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4624 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4625 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4626 30837e32 2019-07-25 stsp if (error)
4627 66bea077 2018-08-02 stsp goto done;
4628 404c43c4 2018-06-21 stsp }
4629 404c43c4 2018-06-21 stsp
4630 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4631 0587e10c 2020-07-23 stsp commit_id, repo);
4632 0587e10c 2020-07-23 stsp if (error)
4633 0587e10c 2020-07-23 stsp goto done;
4634 0587e10c 2020-07-23 stsp
4635 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4636 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4637 28315671 2019-08-14 stsp if (error)
4638 28315671 2019-08-14 stsp goto done;
4639 28315671 2019-08-14 stsp
4640 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4641 28315671 2019-08-14 stsp if (error)
4642 28315671 2019-08-14 stsp goto done;
4643 28315671 2019-08-14 stsp
4644 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4645 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4646 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4647 28315671 2019-08-14 stsp goto done;
4648 28315671 2019-08-14 stsp }
4649 28315671 2019-08-14 stsp
4650 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4651 28315671 2019-08-14 stsp if (error)
4652 28315671 2019-08-14 stsp goto done;
4653 28315671 2019-08-14 stsp bca.f = got_opentemp();
4654 28315671 2019-08-14 stsp if (bca.f == NULL) {
4655 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4656 28315671 2019-08-14 stsp goto done;
4657 28315671 2019-08-14 stsp }
4658 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4659 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4660 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4661 28315671 2019-08-14 stsp goto done;
4662 28315671 2019-08-14 stsp
4663 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4664 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4665 b02560ec 2019-08-19 stsp bca.nlines--;
4666 b02560ec 2019-08-19 stsp
4667 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4668 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4669 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4670 28315671 2019-08-14 stsp goto done;
4671 28315671 2019-08-14 stsp }
4672 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4673 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4674 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4675 7ef28ff8 2019-08-14 stsp while (i > 0) {
4676 7ef28ff8 2019-08-14 stsp i /= 10;
4677 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4678 7ef28ff8 2019-08-14 stsp }
4679 82f6abb8 2019-08-14 stsp bca.repo = repo;
4680 7ef28ff8 2019-08-14 stsp
4681 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4682 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4683 404c43c4 2018-06-21 stsp done:
4684 66bea077 2018-08-02 stsp free(in_repo_path);
4685 0587e10c 2020-07-23 stsp free(link_target);
4686 66bea077 2018-08-02 stsp free(repo_path);
4687 66bea077 2018-08-02 stsp free(cwd);
4688 404c43c4 2018-06-21 stsp free(commit_id);
4689 28315671 2019-08-14 stsp free(obj_id);
4690 28315671 2019-08-14 stsp if (blob)
4691 28315671 2019-08-14 stsp got_object_blob_close(blob);
4692 0c06baac 2019-02-05 stsp if (worktree)
4693 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4694 ad242220 2018-09-08 stsp if (repo) {
4695 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4696 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4697 ad242220 2018-09-08 stsp if (error == NULL)
4698 ad242220 2018-09-08 stsp error = repo_error;
4699 ad242220 2018-09-08 stsp }
4700 3affba96 2019-09-22 stsp if (bca.lines) {
4701 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4702 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4703 3affba96 2019-09-22 stsp free(bline->id_str);
4704 3affba96 2019-09-22 stsp free(bline->committer);
4705 3affba96 2019-09-22 stsp }
4706 3affba96 2019-09-22 stsp free(bca.lines);
4707 28315671 2019-08-14 stsp }
4708 28315671 2019-08-14 stsp free(bca.line_offsets);
4709 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4710 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4711 404c43c4 2018-06-21 stsp return error;
4712 5de5890b 2018-10-18 stsp }
4713 5de5890b 2018-10-18 stsp
4714 5de5890b 2018-10-18 stsp __dead static void
4715 5de5890b 2018-10-18 stsp usage_tree(void)
4716 5de5890b 2018-10-18 stsp {
4717 c1669e2e 2019-01-09 stsp fprintf(stderr,
4718 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4719 5de5890b 2018-10-18 stsp getprogname());
4720 5de5890b 2018-10-18 stsp exit(1);
4721 5de5890b 2018-10-18 stsp }
4722 5de5890b 2018-10-18 stsp
4723 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4724 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4725 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4726 c1669e2e 2019-01-09 stsp {
4727 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4728 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4729 848d6979 2019-08-12 stsp const char *modestr = "";
4730 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4731 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4732 5de5890b 2018-10-18 stsp
4733 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4734 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4735 c1669e2e 2019-01-09 stsp path++;
4736 c1669e2e 2019-01-09 stsp
4737 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4738 63c5ca5d 2019-08-24 stsp modestr = "$";
4739 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4740 0d6c6ee3 2020-05-20 stsp int i;
4741 0d6c6ee3 2020-05-20 stsp
4742 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4743 0d6c6ee3 2020-05-20 stsp if (err)
4744 0d6c6ee3 2020-05-20 stsp return err;
4745 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4746 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4747 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4748 0d6c6ee3 2020-05-20 stsp }
4749 0d6c6ee3 2020-05-20 stsp
4750 848d6979 2019-08-12 stsp modestr = "@";
4751 0d6c6ee3 2020-05-20 stsp }
4752 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4753 848d6979 2019-08-12 stsp modestr = "/";
4754 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4755 848d6979 2019-08-12 stsp modestr = "*";
4756 848d6979 2019-08-12 stsp
4757 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4758 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4759 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4760 0d6c6ee3 2020-05-20 stsp
4761 0d6c6ee3 2020-05-20 stsp free(link_target);
4762 0d6c6ee3 2020-05-20 stsp return NULL;
4763 c1669e2e 2019-01-09 stsp }
4764 c1669e2e 2019-01-09 stsp
4765 5de5890b 2018-10-18 stsp static const struct got_error *
4766 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4767 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4768 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4769 5de5890b 2018-10-18 stsp {
4770 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4771 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4772 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4773 56e0773d 2019-11-28 stsp int nentries, i;
4774 5de5890b 2018-10-18 stsp
4775 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4776 5de5890b 2018-10-18 stsp if (err)
4777 5de5890b 2018-10-18 stsp goto done;
4778 5de5890b 2018-10-18 stsp
4779 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4780 5de5890b 2018-10-18 stsp if (err)
4781 5de5890b 2018-10-18 stsp goto done;
4782 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4783 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4784 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4785 5de5890b 2018-10-18 stsp char *id = NULL;
4786 84453469 2018-11-11 stsp
4787 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4788 84453469 2018-11-11 stsp break;
4789 84453469 2018-11-11 stsp
4790 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4791 5de5890b 2018-10-18 stsp if (show_ids) {
4792 5de5890b 2018-10-18 stsp char *id_str;
4793 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4794 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4795 5de5890b 2018-10-18 stsp if (err)
4796 5de5890b 2018-10-18 stsp goto done;
4797 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4798 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4799 5de5890b 2018-10-18 stsp free(id_str);
4800 5de5890b 2018-10-18 stsp goto done;
4801 5de5890b 2018-10-18 stsp }
4802 5de5890b 2018-10-18 stsp free(id_str);
4803 5de5890b 2018-10-18 stsp }
4804 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
4805 5de5890b 2018-10-18 stsp free(id);
4806 0d6c6ee3 2020-05-20 stsp if (err)
4807 0d6c6ee3 2020-05-20 stsp goto done;
4808 c1669e2e 2019-01-09 stsp
4809 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4810 c1669e2e 2019-01-09 stsp char *child_path;
4811 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4812 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4813 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4814 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4815 c1669e2e 2019-01-09 stsp goto done;
4816 c1669e2e 2019-01-09 stsp }
4817 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4818 c1669e2e 2019-01-09 stsp root_path, repo);
4819 c1669e2e 2019-01-09 stsp free(child_path);
4820 c1669e2e 2019-01-09 stsp if (err)
4821 c1669e2e 2019-01-09 stsp goto done;
4822 c1669e2e 2019-01-09 stsp }
4823 5de5890b 2018-10-18 stsp }
4824 5de5890b 2018-10-18 stsp done:
4825 5de5890b 2018-10-18 stsp if (tree)
4826 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4827 5de5890b 2018-10-18 stsp free(tree_id);
4828 5de5890b 2018-10-18 stsp return err;
4829 404c43c4 2018-06-21 stsp }
4830 404c43c4 2018-06-21 stsp
4831 5de5890b 2018-10-18 stsp static const struct got_error *
4832 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4833 5de5890b 2018-10-18 stsp {
4834 5de5890b 2018-10-18 stsp const struct got_error *error;
4835 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4836 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4837 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4838 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4839 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4840 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4841 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4842 5de5890b 2018-10-18 stsp int ch;
4843 5de5890b 2018-10-18 stsp
4844 5de5890b 2018-10-18 stsp #ifndef PROFILE
4845 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4846 0f8d269b 2019-01-04 stsp NULL) == -1)
4847 5de5890b 2018-10-18 stsp err(1, "pledge");
4848 5de5890b 2018-10-18 stsp #endif
4849 5de5890b 2018-10-18 stsp
4850 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4851 5de5890b 2018-10-18 stsp switch (ch) {
4852 5de5890b 2018-10-18 stsp case 'c':
4853 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4854 5de5890b 2018-10-18 stsp break;
4855 5de5890b 2018-10-18 stsp case 'r':
4856 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4857 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4858 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4859 9ba1d308 2019-10-21 stsp optarg);
4860 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4861 5de5890b 2018-10-18 stsp break;
4862 5de5890b 2018-10-18 stsp case 'i':
4863 5de5890b 2018-10-18 stsp show_ids = 1;
4864 5de5890b 2018-10-18 stsp break;
4865 c1669e2e 2019-01-09 stsp case 'R':
4866 c1669e2e 2019-01-09 stsp recurse = 1;
4867 c1669e2e 2019-01-09 stsp break;
4868 5de5890b 2018-10-18 stsp default:
4869 2deda0b9 2019-03-07 stsp usage_tree();
4870 5de5890b 2018-10-18 stsp /* NOTREACHED */
4871 5de5890b 2018-10-18 stsp }
4872 5de5890b 2018-10-18 stsp }
4873 5de5890b 2018-10-18 stsp
4874 5de5890b 2018-10-18 stsp argc -= optind;
4875 5de5890b 2018-10-18 stsp argv += optind;
4876 5de5890b 2018-10-18 stsp
4877 5de5890b 2018-10-18 stsp if (argc == 1)
4878 5de5890b 2018-10-18 stsp path = argv[0];
4879 5de5890b 2018-10-18 stsp else if (argc > 1)
4880 5de5890b 2018-10-18 stsp usage_tree();
4881 5de5890b 2018-10-18 stsp else
4882 7a2c19d6 2019-02-05 stsp path = NULL;
4883 7a2c19d6 2019-02-05 stsp
4884 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
4885 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
4886 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4887 9bf7a39b 2019-02-05 stsp goto done;
4888 9bf7a39b 2019-02-05 stsp }
4889 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4890 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4891 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4892 7a2c19d6 2019-02-05 stsp goto done;
4893 7a2c19d6 2019-02-05 stsp else
4894 7a2c19d6 2019-02-05 stsp error = NULL;
4895 7a2c19d6 2019-02-05 stsp if (worktree) {
4896 7a2c19d6 2019-02-05 stsp repo_path =
4897 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4898 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
4899 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4900 7a2c19d6 2019-02-05 stsp if (error)
4901 7a2c19d6 2019-02-05 stsp goto done;
4902 7a2c19d6 2019-02-05 stsp } else {
4903 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
4904 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
4905 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4906 7a2c19d6 2019-02-05 stsp goto done;
4907 7a2c19d6 2019-02-05 stsp }
4908 5de5890b 2018-10-18 stsp }
4909 5de5890b 2018-10-18 stsp }
4910 5de5890b 2018-10-18 stsp
4911 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4912 5de5890b 2018-10-18 stsp if (error != NULL)
4913 c02c541e 2019-03-29 stsp goto done;
4914 c02c541e 2019-03-29 stsp
4915 4fedbf4c 2020-11-07 stsp if (worktree) {
4916 4fedbf4c 2020-11-07 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4917 4fedbf4c 2020-11-07 stsp char *p;
4918 5de5890b 2018-10-18 stsp
4919 4fedbf4c 2020-11-07 stsp if (path == NULL)
4920 4fedbf4c 2020-11-07 stsp path = "";
4921 4fedbf4c 2020-11-07 stsp error = got_worktree_resolve_path(&p, worktree, path);
4922 4fedbf4c 2020-11-07 stsp if (error)
4923 4fedbf4c 2020-11-07 stsp goto done;
4924 4fedbf4c 2020-11-07 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4925 4fedbf4c 2020-11-07 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4926 4fedbf4c 2020-11-07 stsp p) == -1) {
4927 4fedbf4c 2020-11-07 stsp error = got_error_from_errno("asprintf");
4928 9bf7a39b 2019-02-05 stsp free(p);
4929 4fedbf4c 2020-11-07 stsp goto done;
4930 4fedbf4c 2020-11-07 stsp }
4931 4fedbf4c 2020-11-07 stsp free(p);
4932 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4933 4fedbf4c 2020-11-07 stsp if (error)
4934 4fedbf4c 2020-11-07 stsp goto done;
4935 4fedbf4c 2020-11-07 stsp } else {
4936 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4937 4fedbf4c 2020-11-07 stsp if (error)
4938 4fedbf4c 2020-11-07 stsp goto done;
4939 4fedbf4c 2020-11-07 stsp if (path == NULL)
4940 9bf7a39b 2019-02-05 stsp path = "/";
4941 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
4942 9bf7a39b 2019-02-05 stsp if (error != NULL)
4943 9bf7a39b 2019-02-05 stsp goto done;
4944 9bf7a39b 2019-02-05 stsp }
4945 5de5890b 2018-10-18 stsp
4946 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
4947 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
4948 4e0a20a4 2020-03-23 tracey if (worktree)
4949 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
4950 4e0a20a4 2020-03-23 tracey else
4951 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
4952 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
4953 5de5890b 2018-10-18 stsp if (error != NULL)
4954 5de5890b 2018-10-18 stsp goto done;
4955 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4956 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
4957 5de5890b 2018-10-18 stsp if (error != NULL)
4958 5de5890b 2018-10-18 stsp goto done;
4959 5de5890b 2018-10-18 stsp } else {
4960 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4961 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4962 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4963 84de9106 2020-12-26 stsp NULL);
4964 84de9106 2020-12-26 stsp if (error)
4965 84de9106 2020-12-26 stsp goto done;
4966 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4967 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4968 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4969 30837e32 2019-07-25 stsp if (error)
4970 5de5890b 2018-10-18 stsp goto done;
4971 5de5890b 2018-10-18 stsp }
4972 5de5890b 2018-10-18 stsp
4973 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4974 c1669e2e 2019-01-09 stsp in_repo_path, repo);
4975 5de5890b 2018-10-18 stsp done:
4976 5de5890b 2018-10-18 stsp free(in_repo_path);
4977 5de5890b 2018-10-18 stsp free(repo_path);
4978 5de5890b 2018-10-18 stsp free(cwd);
4979 5de5890b 2018-10-18 stsp free(commit_id);
4980 7a2c19d6 2019-02-05 stsp if (worktree)
4981 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
4982 5de5890b 2018-10-18 stsp if (repo) {
4983 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
4984 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
4985 5de5890b 2018-10-18 stsp if (error == NULL)
4986 5de5890b 2018-10-18 stsp error = repo_error;
4987 5de5890b 2018-10-18 stsp }
4988 5de5890b 2018-10-18 stsp return error;
4989 5de5890b 2018-10-18 stsp }
4990 5de5890b 2018-10-18 stsp
4991 6bad629b 2019-02-04 stsp __dead static void
4992 6bad629b 2019-02-04 stsp usage_status(void)
4993 6bad629b 2019-02-04 stsp {
4994 081470ac 2020-08-13 stsp fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
4995 081470ac 2020-08-13 stsp getprogname());
4996 6bad629b 2019-02-04 stsp exit(1);
4997 6bad629b 2019-02-04 stsp }
4998 5c860e29 2018-03-12 stsp
4999 b72f483a 2019-02-05 stsp static const struct got_error *
5000 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
5001 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5002 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5003 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5004 6bad629b 2019-02-04 stsp {
5005 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5006 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
5007 081470ac 2020-08-13 stsp if (arg) {
5008 081470ac 2020-08-13 stsp char *status_codes = arg;
5009 081470ac 2020-08-13 stsp size_t ncodes = strlen(status_codes);
5010 081470ac 2020-08-13 stsp int i;
5011 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
5012 081470ac 2020-08-13 stsp if (status == status_codes[i] ||
5013 081470ac 2020-08-13 stsp staged_status == status_codes[i])
5014 081470ac 2020-08-13 stsp break;
5015 081470ac 2020-08-13 stsp }
5016 081470ac 2020-08-13 stsp if (i == ncodes)
5017 081470ac 2020-08-13 stsp return NULL;
5018 081470ac 2020-08-13 stsp }
5019 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
5020 b72f483a 2019-02-05 stsp return NULL;
5021 6bad629b 2019-02-04 stsp }
5022 5c860e29 2018-03-12 stsp
5023 6bad629b 2019-02-04 stsp static const struct got_error *
5024 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
5025 6bad629b 2019-02-04 stsp {
5026 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
5027 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
5028 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
5029 081470ac 2020-08-13 stsp char *cwd = NULL, *status_codes = NULL;;
5030 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
5031 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
5032 081470ac 2020-08-13 stsp int ch, i;
5033 5c860e29 2018-03-12 stsp
5034 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
5035 72ea6654 2019-07-27 stsp
5036 081470ac 2020-08-13 stsp while ((ch = getopt(argc, argv, "s:")) != -1) {
5037 6bad629b 2019-02-04 stsp switch (ch) {
5038 081470ac 2020-08-13 stsp case 's':
5039 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
5040 081470ac 2020-08-13 stsp switch (optarg[i]) {
5041 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
5042 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
5043 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
5044 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
5045 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
5046 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
5047 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
5048 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
5049 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
5050 081470ac 2020-08-13 stsp break;
5051 081470ac 2020-08-13 stsp default:
5052 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
5053 081470ac 2020-08-13 stsp optarg[i]);
5054 081470ac 2020-08-13 stsp }
5055 081470ac 2020-08-13 stsp }
5056 081470ac 2020-08-13 stsp status_codes = optarg;
5057 081470ac 2020-08-13 stsp break;
5058 5c860e29 2018-03-12 stsp default:
5059 2deda0b9 2019-03-07 stsp usage_status();
5060 6bad629b 2019-02-04 stsp /* NOTREACHED */
5061 5c860e29 2018-03-12 stsp }
5062 5c860e29 2018-03-12 stsp }
5063 5c860e29 2018-03-12 stsp
5064 6bad629b 2019-02-04 stsp argc -= optind;
5065 6bad629b 2019-02-04 stsp argv += optind;
5066 5c860e29 2018-03-12 stsp
5067 6bad629b 2019-02-04 stsp #ifndef PROFILE
5068 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5069 6bad629b 2019-02-04 stsp NULL) == -1)
5070 6bad629b 2019-02-04 stsp err(1, "pledge");
5071 f42b1b34 2018-03-12 stsp #endif
5072 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
5073 927df6b7 2019-02-10 stsp if (cwd == NULL) {
5074 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5075 927df6b7 2019-02-10 stsp goto done;
5076 927df6b7 2019-02-10 stsp }
5077 927df6b7 2019-02-10 stsp
5078 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
5079 fa51e947 2020-03-27 stsp if (error) {
5080 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5081 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
5082 a5edda0a 2019-07-27 stsp goto done;
5083 fa51e947 2020-03-27 stsp }
5084 6bad629b 2019-02-04 stsp
5085 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5086 c9956ddf 2019-09-08 stsp NULL);
5087 6bad629b 2019-02-04 stsp if (error != NULL)
5088 6bad629b 2019-02-04 stsp goto done;
5089 6bad629b 2019-02-04 stsp
5090 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5091 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5092 087fb88c 2019-08-04 stsp if (error)
5093 087fb88c 2019-08-04 stsp goto done;
5094 087fb88c 2019-08-04 stsp
5095 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5096 6bad629b 2019-02-04 stsp if (error)
5097 6bad629b 2019-02-04 stsp goto done;
5098 6bad629b 2019-02-04 stsp
5099 081470ac 2020-08-13 stsp error = got_worktree_status(worktree, &paths, repo, print_status,
5100 081470ac 2020-08-13 stsp status_codes, check_cancelled, NULL);
5101 6bad629b 2019-02-04 stsp done:
5102 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
5103 a5edda0a 2019-07-27 stsp free((char *)pe->path);
5104 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
5105 927df6b7 2019-02-10 stsp free(cwd);
5106 d0eebce4 2019-03-11 stsp return error;
5107 d0eebce4 2019-03-11 stsp }
5108 d0eebce4 2019-03-11 stsp
5109 d0eebce4 2019-03-11 stsp __dead static void
5110 d0eebce4 2019-03-11 stsp usage_ref(void)
5111 d0eebce4 2019-03-11 stsp {
5112 d0eebce4 2019-03-11 stsp fprintf(stderr,
5113 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5114 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
5115 d0eebce4 2019-03-11 stsp getprogname());
5116 d0eebce4 2019-03-11 stsp exit(1);
5117 d0eebce4 2019-03-11 stsp }
5118 d0eebce4 2019-03-11 stsp
5119 d0eebce4 2019-03-11 stsp static const struct got_error *
5120 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
5121 d0eebce4 2019-03-11 stsp {
5122 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
5123 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
5124 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
5125 d0eebce4 2019-03-11 stsp
5126 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5127 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5128 d0eebce4 2019-03-11 stsp if (err)
5129 d0eebce4 2019-03-11 stsp return err;
5130 d0eebce4 2019-03-11 stsp
5131 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
5132 d0eebce4 2019-03-11 stsp char *refstr;
5133 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
5134 d0eebce4 2019-03-11 stsp if (refstr == NULL)
5135 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
5136 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5137 d0eebce4 2019-03-11 stsp free(refstr);
5138 d0eebce4 2019-03-11 stsp }
5139 d0eebce4 2019-03-11 stsp
5140 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5141 d0eebce4 2019-03-11 stsp return NULL;
5142 d0eebce4 2019-03-11 stsp }
5143 d0eebce4 2019-03-11 stsp
5144 d0eebce4 2019-03-11 stsp static const struct got_error *
5145 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
5146 d0eebce4 2019-03-11 stsp {
5147 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5148 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5149 d0eebce4 2019-03-11 stsp
5150 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5151 d0eebce4 2019-03-11 stsp if (err)
5152 d0eebce4 2019-03-11 stsp return err;
5153 d0eebce4 2019-03-11 stsp
5154 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
5155 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5156 d0eebce4 2019-03-11 stsp return err;
5157 d0eebce4 2019-03-11 stsp }
5158 d0eebce4 2019-03-11 stsp
5159 d0eebce4 2019-03-11 stsp static const struct got_error *
5160 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
5161 d0eebce4 2019-03-11 stsp {
5162 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5163 d0eebce4 2019-03-11 stsp struct got_object_id *id;
5164 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
5165 d1644381 2019-07-14 stsp
5166 d1644381 2019-07-14 stsp /*
5167 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5168 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
5169 d1644381 2019-07-14 stsp * an unintended typo.
5170 d1644381 2019-07-14 stsp */
5171 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5172 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5173 d0eebce4 2019-03-11 stsp
5174 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5175 dd88155e 2019-06-29 stsp repo);
5176 d83d9d5c 2019-05-13 stsp if (err) {
5177 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
5178 d0eebce4 2019-03-11 stsp
5179 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5180 d83d9d5c 2019-05-13 stsp return err;
5181 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
5182 d83d9d5c 2019-05-13 stsp if (err)
5183 d83d9d5c 2019-05-13 stsp return err;
5184 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
5185 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
5186 d83d9d5c 2019-05-13 stsp if (err)
5187 d83d9d5c 2019-05-13 stsp return err;
5188 d83d9d5c 2019-05-13 stsp }
5189 d83d9d5c 2019-05-13 stsp
5190 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5191 d0eebce4 2019-03-11 stsp if (err)
5192 d0eebce4 2019-03-11 stsp goto done;
5193 d0eebce4 2019-03-11 stsp
5194 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5195 d0eebce4 2019-03-11 stsp done:
5196 d0eebce4 2019-03-11 stsp if (ref)
5197 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5198 d0eebce4 2019-03-11 stsp free(id);
5199 d1c1ae5f 2019-08-12 stsp return err;
5200 d1c1ae5f 2019-08-12 stsp }
5201 d1c1ae5f 2019-08-12 stsp
5202 d1c1ae5f 2019-08-12 stsp static const struct got_error *
5203 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
5204 d1c1ae5f 2019-08-12 stsp {
5205 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
5206 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
5207 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
5208 d1c1ae5f 2019-08-12 stsp
5209 d1c1ae5f 2019-08-12 stsp /*
5210 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5211 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
5212 d1c1ae5f 2019-08-12 stsp * an unintended typo.
5213 d1c1ae5f 2019-08-12 stsp */
5214 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5215 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5216 d1c1ae5f 2019-08-12 stsp
5217 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5218 d1c1ae5f 2019-08-12 stsp if (err)
5219 d1c1ae5f 2019-08-12 stsp return err;
5220 d1c1ae5f 2019-08-12 stsp
5221 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5222 d1c1ae5f 2019-08-12 stsp if (err)
5223 d1c1ae5f 2019-08-12 stsp goto done;
5224 d1c1ae5f 2019-08-12 stsp
5225 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5226 d1c1ae5f 2019-08-12 stsp done:
5227 d1c1ae5f 2019-08-12 stsp if (target_ref)
5228 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5229 d1c1ae5f 2019-08-12 stsp if (ref)
5230 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5231 d0eebce4 2019-03-11 stsp return err;
5232 d0eebce4 2019-03-11 stsp }
5233 d0eebce4 2019-03-11 stsp
5234 d0eebce4 2019-03-11 stsp static const struct got_error *
5235 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5236 d0eebce4 2019-03-11 stsp {
5237 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
5238 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
5239 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
5240 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
5241 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
5242 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
5243 b2070a3f 2020-03-22 stsp char *refname = NULL;
5244 d0eebce4 2019-03-11 stsp
5245 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5246 d0eebce4 2019-03-11 stsp switch (ch) {
5247 e31abbf2 2020-03-22 stsp case 'c':
5248 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5249 e31abbf2 2020-03-22 stsp break;
5250 d0eebce4 2019-03-11 stsp case 'd':
5251 e31abbf2 2020-03-22 stsp do_delete = 1;
5252 d0eebce4 2019-03-11 stsp break;
5253 d0eebce4 2019-03-11 stsp case 'r':
5254 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5255 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5256 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5257 9ba1d308 2019-10-21 stsp optarg);
5258 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5259 d0eebce4 2019-03-11 stsp break;
5260 d0eebce4 2019-03-11 stsp case 'l':
5261 d0eebce4 2019-03-11 stsp do_list = 1;
5262 d1c1ae5f 2019-08-12 stsp break;
5263 d1c1ae5f 2019-08-12 stsp case 's':
5264 e31abbf2 2020-03-22 stsp symref_target = optarg;
5265 d0eebce4 2019-03-11 stsp break;
5266 d0eebce4 2019-03-11 stsp default:
5267 d0eebce4 2019-03-11 stsp usage_ref();
5268 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5269 d0eebce4 2019-03-11 stsp }
5270 d0eebce4 2019-03-11 stsp }
5271 d0eebce4 2019-03-11 stsp
5272 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5273 ff69268e 2020-12-13 stsp option_conflict('c', 'l');
5274 ff69268e 2020-12-13 stsp if (obj_arg && do_delete)
5275 ff69268e 2020-12-13 stsp option_conflict('c', 'd');
5276 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5277 ff69268e 2020-12-13 stsp option_conflict('c', 's');
5278 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5279 ff69268e 2020-12-13 stsp option_conflict('s', 'd');
5280 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5281 ff69268e 2020-12-13 stsp option_conflict('s', 'l');
5282 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5283 ff69268e 2020-12-13 stsp option_conflict('d', 'l');
5284 d0eebce4 2019-03-11 stsp
5285 d0eebce4 2019-03-11 stsp argc -= optind;
5286 d0eebce4 2019-03-11 stsp argv += optind;
5287 d0eebce4 2019-03-11 stsp
5288 e31abbf2 2020-03-22 stsp if (do_list) {
5289 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5290 d0eebce4 2019-03-11 stsp usage_ref();
5291 b2070a3f 2020-03-22 stsp if (argc == 1) {
5292 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5293 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5294 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5295 b2070a3f 2020-03-22 stsp goto done;
5296 b2070a3f 2020-03-22 stsp }
5297 b2070a3f 2020-03-22 stsp }
5298 e31abbf2 2020-03-22 stsp } else {
5299 e31abbf2 2020-03-22 stsp if (argc != 1)
5300 e31abbf2 2020-03-22 stsp usage_ref();
5301 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5302 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5303 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5304 b2070a3f 2020-03-22 stsp goto done;
5305 b2070a3f 2020-03-22 stsp }
5306 e31abbf2 2020-03-22 stsp }
5307 b2070a3f 2020-03-22 stsp
5308 b2070a3f 2020-03-22 stsp if (refname)
5309 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5310 d0eebce4 2019-03-11 stsp
5311 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5312 e0b57350 2019-03-12 stsp if (do_list) {
5313 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5314 e0b57350 2019-03-12 stsp NULL) == -1)
5315 e0b57350 2019-03-12 stsp err(1, "pledge");
5316 e0b57350 2019-03-12 stsp } else {
5317 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5318 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5319 e0b57350 2019-03-12 stsp err(1, "pledge");
5320 e0b57350 2019-03-12 stsp }
5321 d0eebce4 2019-03-11 stsp #endif
5322 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5323 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5324 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5325 d0eebce4 2019-03-11 stsp goto done;
5326 d0eebce4 2019-03-11 stsp }
5327 d0eebce4 2019-03-11 stsp
5328 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5329 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5330 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5331 d0eebce4 2019-03-11 stsp goto done;
5332 d0eebce4 2019-03-11 stsp else
5333 d0eebce4 2019-03-11 stsp error = NULL;
5334 d0eebce4 2019-03-11 stsp if (worktree) {
5335 d0eebce4 2019-03-11 stsp repo_path =
5336 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5337 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5338 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5339 d0eebce4 2019-03-11 stsp if (error)
5340 d0eebce4 2019-03-11 stsp goto done;
5341 d0eebce4 2019-03-11 stsp } else {
5342 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5343 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5344 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5345 d0eebce4 2019-03-11 stsp goto done;
5346 d0eebce4 2019-03-11 stsp }
5347 d0eebce4 2019-03-11 stsp }
5348 d0eebce4 2019-03-11 stsp }
5349 d0eebce4 2019-03-11 stsp
5350 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5351 d0eebce4 2019-03-11 stsp if (error != NULL)
5352 d0eebce4 2019-03-11 stsp goto done;
5353 d0eebce4 2019-03-11 stsp
5354 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5355 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5356 c02c541e 2019-03-29 stsp if (error)
5357 c02c541e 2019-03-29 stsp goto done;
5358 c02c541e 2019-03-29 stsp
5359 d0eebce4 2019-03-11 stsp if (do_list)
5360 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5361 e31abbf2 2020-03-22 stsp else if (do_delete)
5362 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
5363 e31abbf2 2020-03-22 stsp else if (symref_target)
5364 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5365 e31abbf2 2020-03-22 stsp else {
5366 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5367 e31abbf2 2020-03-22 stsp usage_ref();
5368 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5369 e31abbf2 2020-03-22 stsp }
5370 4e759de4 2019-06-26 stsp done:
5371 b2070a3f 2020-03-22 stsp free(refname);
5372 4e759de4 2019-06-26 stsp if (repo)
5373 4e759de4 2019-06-26 stsp got_repo_close(repo);
5374 4e759de4 2019-06-26 stsp if (worktree)
5375 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5376 4e759de4 2019-06-26 stsp free(cwd);
5377 4e759de4 2019-06-26 stsp free(repo_path);
5378 4e759de4 2019-06-26 stsp return error;
5379 4e759de4 2019-06-26 stsp }
5380 4e759de4 2019-06-26 stsp
5381 4e759de4 2019-06-26 stsp __dead static void
5382 4e759de4 2019-06-26 stsp usage_branch(void)
5383 4e759de4 2019-06-26 stsp {
5384 4e759de4 2019-06-26 stsp fprintf(stderr,
5385 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5386 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5387 4e759de4 2019-06-26 stsp exit(1);
5388 4e759de4 2019-06-26 stsp }
5389 4e759de4 2019-06-26 stsp
5390 4e759de4 2019-06-26 stsp static const struct got_error *
5391 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5392 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5393 4e99b47e 2019-10-04 stsp {
5394 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5395 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5396 4e99b47e 2019-10-04 stsp char *refstr;
5397 4e99b47e 2019-10-04 stsp
5398 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5399 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5400 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5401 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5402 4e99b47e 2019-10-04 stsp
5403 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5404 4e99b47e 2019-10-04 stsp if (err)
5405 4e99b47e 2019-10-04 stsp return err;
5406 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5407 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5408 4e99b47e 2019-10-04 stsp marker = "* ";
5409 4e99b47e 2019-10-04 stsp else
5410 4e99b47e 2019-10-04 stsp marker = "~ ";
5411 4e99b47e 2019-10-04 stsp free(id);
5412 4e99b47e 2019-10-04 stsp }
5413 4e99b47e 2019-10-04 stsp
5414 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5415 4e99b47e 2019-10-04 stsp refname += 11;
5416 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5417 4e99b47e 2019-10-04 stsp refname += 18;
5418 4e99b47e 2019-10-04 stsp
5419 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5420 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5421 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5422 4e99b47e 2019-10-04 stsp
5423 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5424 4e99b47e 2019-10-04 stsp free(refstr);
5425 4e99b47e 2019-10-04 stsp return NULL;
5426 4e99b47e 2019-10-04 stsp }
5427 4e99b47e 2019-10-04 stsp
5428 4e99b47e 2019-10-04 stsp static const struct got_error *
5429 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5430 ad89fa31 2019-10-04 stsp {
5431 ad89fa31 2019-10-04 stsp const char *refname;
5432 ad89fa31 2019-10-04 stsp
5433 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5434 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5435 ad89fa31 2019-10-04 stsp
5436 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5437 ad89fa31 2019-10-04 stsp
5438 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5439 ad89fa31 2019-10-04 stsp refname += 11;
5440 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5441 ad89fa31 2019-10-04 stsp refname += 18;
5442 ad89fa31 2019-10-04 stsp
5443 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5444 ad89fa31 2019-10-04 stsp
5445 ad89fa31 2019-10-04 stsp return NULL;
5446 ad89fa31 2019-10-04 stsp }
5447 ad89fa31 2019-10-04 stsp
5448 ad89fa31 2019-10-04 stsp static const struct got_error *
5449 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5450 4e759de4 2019-06-26 stsp {
5451 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5452 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5453 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5454 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5455 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5456 4e759de4 2019-06-26 stsp
5457 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5458 ba882ee3 2019-07-11 stsp
5459 4e99b47e 2019-10-04 stsp if (worktree) {
5460 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5461 4e99b47e 2019-10-04 stsp worktree);
5462 4e99b47e 2019-10-04 stsp if (err)
5463 4e99b47e 2019-10-04 stsp return err;
5464 4e99b47e 2019-10-04 stsp
5465 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5466 4e99b47e 2019-10-04 stsp worktree);
5467 4e99b47e 2019-10-04 stsp if (err)
5468 4e99b47e 2019-10-04 stsp return err;
5469 4e99b47e 2019-10-04 stsp
5470 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5471 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5472 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5473 4e99b47e 2019-10-04 stsp if (err)
5474 4e99b47e 2019-10-04 stsp return err;
5475 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5476 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5477 4e99b47e 2019-10-04 stsp }
5478 4e99b47e 2019-10-04 stsp }
5479 4e99b47e 2019-10-04 stsp
5480 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5481 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5482 4e759de4 2019-06-26 stsp if (err)
5483 4e759de4 2019-06-26 stsp return err;
5484 4e759de4 2019-06-26 stsp
5485 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry)
5486 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5487 4e759de4 2019-06-26 stsp
5488 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5489 4e759de4 2019-06-26 stsp return NULL;
5490 4e759de4 2019-06-26 stsp }
5491 4e759de4 2019-06-26 stsp
5492 4e759de4 2019-06-26 stsp static const struct got_error *
5493 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5494 45cd4e47 2019-08-25 stsp const char *branch_name)
5495 4e759de4 2019-06-26 stsp {
5496 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5497 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5498 4e759de4 2019-06-26 stsp char *refname;
5499 4e759de4 2019-06-26 stsp
5500 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5501 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5502 4e759de4 2019-06-26 stsp
5503 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5504 4e759de4 2019-06-26 stsp if (err)
5505 4e759de4 2019-06-26 stsp goto done;
5506 4e759de4 2019-06-26 stsp
5507 45cd4e47 2019-08-25 stsp if (worktree &&
5508 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5509 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5510 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5511 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5512 45cd4e47 2019-08-25 stsp goto done;
5513 45cd4e47 2019-08-25 stsp }
5514 45cd4e47 2019-08-25 stsp
5515 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
5516 4e759de4 2019-06-26 stsp done:
5517 45cd4e47 2019-08-25 stsp if (ref)
5518 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5519 4e759de4 2019-06-26 stsp free(refname);
5520 4e759de4 2019-06-26 stsp return err;
5521 4e759de4 2019-06-26 stsp }
5522 4e759de4 2019-06-26 stsp
5523 4e759de4 2019-06-26 stsp static const struct got_error *
5524 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5525 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5526 4e759de4 2019-06-26 stsp {
5527 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5528 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5529 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5530 d3f84d51 2019-07-11 stsp
5531 d3f84d51 2019-07-11 stsp /*
5532 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5533 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5534 d3f84d51 2019-07-11 stsp * an unintended typo.
5535 d3f84d51 2019-07-11 stsp */
5536 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5537 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5538 4e759de4 2019-06-26 stsp
5539 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5540 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
5541 62d463ca 2020-10-20 naddy goto done;
5542 4e759de4 2019-06-26 stsp }
5543 4e759de4 2019-06-26 stsp
5544 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5545 4e759de4 2019-06-26 stsp if (err == NULL) {
5546 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5547 4e759de4 2019-06-26 stsp goto done;
5548 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5549 4e759de4 2019-06-26 stsp goto done;
5550 4e759de4 2019-06-26 stsp
5551 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5552 4e759de4 2019-06-26 stsp if (err)
5553 4e759de4 2019-06-26 stsp goto done;
5554 4e759de4 2019-06-26 stsp
5555 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5556 d0eebce4 2019-03-11 stsp done:
5557 4e759de4 2019-06-26 stsp if (ref)
5558 4e759de4 2019-06-26 stsp got_ref_close(ref);
5559 4e759de4 2019-06-26 stsp free(base_refname);
5560 4e759de4 2019-06-26 stsp free(refname);
5561 4e759de4 2019-06-26 stsp return err;
5562 4e759de4 2019-06-26 stsp }
5563 4e759de4 2019-06-26 stsp
5564 4e759de4 2019-06-26 stsp static const struct got_error *
5565 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5566 4e759de4 2019-06-26 stsp {
5567 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5568 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5569 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5570 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5571 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5572 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5573 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5574 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5575 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5576 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5577 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5578 4e759de4 2019-06-26 stsp
5579 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5580 da76fce2 2020-02-24 stsp
5581 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5582 4e759de4 2019-06-26 stsp switch (ch) {
5583 a74f7e83 2019-11-10 stsp case 'c':
5584 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5585 a74f7e83 2019-11-10 stsp break;
5586 4e759de4 2019-06-26 stsp case 'd':
5587 4e759de4 2019-06-26 stsp delref = optarg;
5588 4e759de4 2019-06-26 stsp break;
5589 4e759de4 2019-06-26 stsp case 'r':
5590 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5591 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5592 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5593 9ba1d308 2019-10-21 stsp optarg);
5594 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5595 4e759de4 2019-06-26 stsp break;
5596 4e759de4 2019-06-26 stsp case 'l':
5597 4e759de4 2019-06-26 stsp do_list = 1;
5598 da76fce2 2020-02-24 stsp break;
5599 da76fce2 2020-02-24 stsp case 'n':
5600 da76fce2 2020-02-24 stsp do_update = 0;
5601 4e759de4 2019-06-26 stsp break;
5602 4e759de4 2019-06-26 stsp default:
5603 4e759de4 2019-06-26 stsp usage_branch();
5604 4e759de4 2019-06-26 stsp /* NOTREACHED */
5605 4e759de4 2019-06-26 stsp }
5606 4e759de4 2019-06-26 stsp }
5607 4e759de4 2019-06-26 stsp
5608 4e759de4 2019-06-26 stsp if (do_list && delref)
5609 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
5610 4e759de4 2019-06-26 stsp
5611 4e759de4 2019-06-26 stsp argc -= optind;
5612 4e759de4 2019-06-26 stsp argv += optind;
5613 ad89fa31 2019-10-04 stsp
5614 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5615 ad89fa31 2019-10-04 stsp do_show = 1;
5616 4e759de4 2019-06-26 stsp
5617 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5618 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5619 a74f7e83 2019-11-10 stsp
5620 4e759de4 2019-06-26 stsp if (do_list || delref) {
5621 4e759de4 2019-06-26 stsp if (argc > 0)
5622 4e759de4 2019-06-26 stsp usage_branch();
5623 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5624 4e759de4 2019-06-26 stsp usage_branch();
5625 4e759de4 2019-06-26 stsp
5626 4e759de4 2019-06-26 stsp #ifndef PROFILE
5627 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5628 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5629 4e759de4 2019-06-26 stsp NULL) == -1)
5630 4e759de4 2019-06-26 stsp err(1, "pledge");
5631 4e759de4 2019-06-26 stsp } else {
5632 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5633 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5634 4e759de4 2019-06-26 stsp err(1, "pledge");
5635 4e759de4 2019-06-26 stsp }
5636 4e759de4 2019-06-26 stsp #endif
5637 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5638 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5639 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5640 4e759de4 2019-06-26 stsp goto done;
5641 4e759de4 2019-06-26 stsp }
5642 4e759de4 2019-06-26 stsp
5643 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5644 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5645 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5646 4e759de4 2019-06-26 stsp goto done;
5647 4e759de4 2019-06-26 stsp else
5648 4e759de4 2019-06-26 stsp error = NULL;
5649 4e759de4 2019-06-26 stsp if (worktree) {
5650 4e759de4 2019-06-26 stsp repo_path =
5651 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5652 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5653 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5654 4e759de4 2019-06-26 stsp if (error)
5655 4e759de4 2019-06-26 stsp goto done;
5656 4e759de4 2019-06-26 stsp } else {
5657 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5658 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5659 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5660 4e759de4 2019-06-26 stsp goto done;
5661 4e759de4 2019-06-26 stsp }
5662 4e759de4 2019-06-26 stsp }
5663 4e759de4 2019-06-26 stsp }
5664 4e759de4 2019-06-26 stsp
5665 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5666 4e759de4 2019-06-26 stsp if (error != NULL)
5667 4e759de4 2019-06-26 stsp goto done;
5668 4e759de4 2019-06-26 stsp
5669 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5670 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5671 4e759de4 2019-06-26 stsp if (error)
5672 4e759de4 2019-06-26 stsp goto done;
5673 4e759de4 2019-06-26 stsp
5674 ad89fa31 2019-10-04 stsp if (do_show)
5675 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5676 ad89fa31 2019-10-04 stsp else if (do_list)
5677 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5678 4e759de4 2019-06-26 stsp else if (delref)
5679 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5680 4e759de4 2019-06-26 stsp else {
5681 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5682 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5683 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5684 84de9106 2020-12-26 stsp NULL);
5685 84de9106 2020-12-26 stsp if (error)
5686 84de9106 2020-12-26 stsp goto done;
5687 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5688 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5689 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5690 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5691 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5692 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5693 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5694 a74f7e83 2019-11-10 stsp if (error)
5695 a74f7e83 2019-11-10 stsp goto done;
5696 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5697 da76fce2 2020-02-24 stsp if (error)
5698 da76fce2 2020-02-24 stsp goto done;
5699 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5700 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5701 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5702 da76fce2 2020-02-24 stsp
5703 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5704 da76fce2 2020-02-24 stsp if (error)
5705 da76fce2 2020-02-24 stsp goto done;
5706 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5707 da76fce2 2020-02-24 stsp worktree);
5708 da76fce2 2020-02-24 stsp if (error)
5709 da76fce2 2020-02-24 stsp goto done;
5710 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5711 da76fce2 2020-02-24 stsp == -1) {
5712 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5713 da76fce2 2020-02-24 stsp goto done;
5714 da76fce2 2020-02-24 stsp }
5715 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5716 da76fce2 2020-02-24 stsp free(branch_refname);
5717 da76fce2 2020-02-24 stsp if (error)
5718 da76fce2 2020-02-24 stsp goto done;
5719 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5720 da76fce2 2020-02-24 stsp repo);
5721 da76fce2 2020-02-24 stsp if (error)
5722 da76fce2 2020-02-24 stsp goto done;
5723 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5724 da76fce2 2020-02-24 stsp commit_id);
5725 da76fce2 2020-02-24 stsp if (error)
5726 da76fce2 2020-02-24 stsp goto done;
5727 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5728 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5729 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5730 9627c110 2020-04-18 stsp NULL);
5731 da76fce2 2020-02-24 stsp if (error)
5732 da76fce2 2020-02-24 stsp goto done;
5733 9627c110 2020-04-18 stsp if (upa.did_something)
5734 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5735 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
5736 da76fce2 2020-02-24 stsp }
5737 4e759de4 2019-06-26 stsp }
5738 4e759de4 2019-06-26 stsp done:
5739 da76fce2 2020-02-24 stsp if (ref)
5740 da76fce2 2020-02-24 stsp got_ref_close(ref);
5741 d0eebce4 2019-03-11 stsp if (repo)
5742 d0eebce4 2019-03-11 stsp got_repo_close(repo);
5743 d0eebce4 2019-03-11 stsp if (worktree)
5744 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
5745 d0eebce4 2019-03-11 stsp free(cwd);
5746 d0eebce4 2019-03-11 stsp free(repo_path);
5747 da76fce2 2020-02-24 stsp free(commit_id);
5748 da76fce2 2020-02-24 stsp free(commit_id_str);
5749 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
5750 da76fce2 2020-02-24 stsp free((char *)pe->path);
5751 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
5752 d00136be 2019-03-26 stsp return error;
5753 d00136be 2019-03-26 stsp }
5754 d00136be 2019-03-26 stsp
5755 8e7bd50a 2019-08-22 stsp
5756 d00136be 2019-03-26 stsp __dead static void
5757 8e7bd50a 2019-08-22 stsp usage_tag(void)
5758 8e7bd50a 2019-08-22 stsp {
5759 8e7bd50a 2019-08-22 stsp fprintf(stderr,
5760 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5761 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
5762 8e7bd50a 2019-08-22 stsp exit(1);
5763 b8bad2ba 2019-08-23 stsp }
5764 b8bad2ba 2019-08-23 stsp
5765 b8bad2ba 2019-08-23 stsp #if 0
5766 b8bad2ba 2019-08-23 stsp static const struct got_error *
5767 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5768 b8bad2ba 2019-08-23 stsp {
5769 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
5770 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
5771 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
5772 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
5773 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
5774 b8bad2ba 2019-08-23 stsp
5775 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5776 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
5777 b8bad2ba 2019-08-23 stsp if (se == NULL) {
5778 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5779 b8bad2ba 2019-08-23 stsp if (err)
5780 b8bad2ba 2019-08-23 stsp return err;
5781 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5782 b8bad2ba 2019-08-23 stsp continue;
5783 b8bad2ba 2019-08-23 stsp } else {
5784 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5785 b8bad2ba 2019-08-23 stsp if (err)
5786 b8bad2ba 2019-08-23 stsp break;
5787 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5788 b8bad2ba 2019-08-23 stsp free(re_id);
5789 b8bad2ba 2019-08-23 stsp if (err)
5790 b8bad2ba 2019-08-23 stsp break;
5791 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5792 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
5793 b8bad2ba 2019-08-23 stsp }
5794 b8bad2ba 2019-08-23 stsp
5795 b8bad2ba 2019-08-23 stsp while (se) {
5796 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5797 b8bad2ba 2019-08-23 stsp if (err)
5798 b8bad2ba 2019-08-23 stsp break;
5799 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5800 b8bad2ba 2019-08-23 stsp free(se_id);
5801 b8bad2ba 2019-08-23 stsp if (err)
5802 b8bad2ba 2019-08-23 stsp break;
5803 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5804 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
5805 b8bad2ba 2019-08-23 stsp
5806 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
5807 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5808 b8bad2ba 2019-08-23 stsp if (err)
5809 b8bad2ba 2019-08-23 stsp return err;
5810 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5811 b8bad2ba 2019-08-23 stsp break;
5812 b8bad2ba 2019-08-23 stsp }
5813 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
5814 b8bad2ba 2019-08-23 stsp continue;
5815 b8bad2ba 2019-08-23 stsp }
5816 b8bad2ba 2019-08-23 stsp }
5817 b8bad2ba 2019-08-23 stsp done:
5818 b8bad2ba 2019-08-23 stsp return err;
5819 8e7bd50a 2019-08-22 stsp }
5820 b8bad2ba 2019-08-23 stsp #endif
5821 b8bad2ba 2019-08-23 stsp
5822 b8bad2ba 2019-08-23 stsp static const struct got_error *
5823 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5824 8e7bd50a 2019-08-22 stsp {
5825 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5826 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5827 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
5828 8e7bd50a 2019-08-22 stsp
5829 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5830 8e7bd50a 2019-08-22 stsp
5831 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5832 8e7bd50a 2019-08-22 stsp if (err)
5833 8e7bd50a 2019-08-22 stsp return err;
5834 8e7bd50a 2019-08-22 stsp
5835 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
5836 8e7bd50a 2019-08-22 stsp const char *refname;
5837 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5838 8e7bd50a 2019-08-22 stsp char datebuf[26];
5839 d4efa91b 2020-01-14 stsp const char *tagger;
5840 8e7bd50a 2019-08-22 stsp time_t tagger_time;
5841 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
5842 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
5843 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
5844 8e7bd50a 2019-08-22 stsp
5845 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
5846 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5847 8e7bd50a 2019-08-22 stsp continue;
5848 8e7bd50a 2019-08-22 stsp refname += 10;
5849 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
5850 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
5851 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
5852 8e7bd50a 2019-08-22 stsp break;
5853 8e7bd50a 2019-08-22 stsp }
5854 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5855 8e7bd50a 2019-08-22 stsp free(refstr);
5856 8e7bd50a 2019-08-22 stsp
5857 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
5858 8e7bd50a 2019-08-22 stsp if (err)
5859 8e7bd50a 2019-08-22 stsp break;
5860 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
5861 d4efa91b 2020-01-14 stsp if (err) {
5862 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5863 d4efa91b 2020-01-14 stsp free(id);
5864 d4efa91b 2020-01-14 stsp break;
5865 d4efa91b 2020-01-14 stsp }
5866 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
5867 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
5868 d4efa91b 2020-01-14 stsp if (err) {
5869 d4efa91b 2020-01-14 stsp free(id);
5870 d4efa91b 2020-01-14 stsp break;
5871 d4efa91b 2020-01-14 stsp }
5872 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
5873 d4efa91b 2020-01-14 stsp tagger_time =
5874 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
5875 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
5876 d4efa91b 2020-01-14 stsp free(id);
5877 d4efa91b 2020-01-14 stsp if (err)
5878 d4efa91b 2020-01-14 stsp break;
5879 d4efa91b 2020-01-14 stsp } else {
5880 d4efa91b 2020-01-14 stsp free(id);
5881 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
5882 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5883 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
5884 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
5885 d4efa91b 2020-01-14 stsp if (err)
5886 d4efa91b 2020-01-14 stsp break;
5887 d4efa91b 2020-01-14 stsp }
5888 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
5889 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
5890 2417344c 2019-08-23 stsp if (datestr)
5891 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
5892 d4efa91b 2020-01-14 stsp if (commit)
5893 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5894 d4efa91b 2020-01-14 stsp else {
5895 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
5896 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
5897 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5898 d4efa91b 2020-01-14 stsp id_str);
5899 d4efa91b 2020-01-14 stsp break;
5900 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
5901 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5902 d4efa91b 2020-01-14 stsp id_str);
5903 d4efa91b 2020-01-14 stsp break;
5904 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
5905 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5906 d4efa91b 2020-01-14 stsp id_str);
5907 d4efa91b 2020-01-14 stsp break;
5908 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
5909 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5910 d4efa91b 2020-01-14 stsp id_str);
5911 d4efa91b 2020-01-14 stsp break;
5912 d4efa91b 2020-01-14 stsp default:
5913 d4efa91b 2020-01-14 stsp break;
5914 d4efa91b 2020-01-14 stsp }
5915 8e7bd50a 2019-08-22 stsp }
5916 8e7bd50a 2019-08-22 stsp free(id_str);
5917 d4efa91b 2020-01-14 stsp if (commit) {
5918 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5919 d4efa91b 2020-01-14 stsp if (err)
5920 d4efa91b 2020-01-14 stsp break;
5921 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
5922 d4efa91b 2020-01-14 stsp } else {
5923 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5924 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
5925 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
5926 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
5927 d4efa91b 2020-01-14 stsp break;
5928 d4efa91b 2020-01-14 stsp }
5929 8e7bd50a 2019-08-22 stsp }
5930 8e7bd50a 2019-08-22 stsp
5931 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
5932 8e7bd50a 2019-08-22 stsp do {
5933 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
5934 8e7bd50a 2019-08-22 stsp if (line)
5935 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
5936 8e7bd50a 2019-08-22 stsp } while (line);
5937 8e7bd50a 2019-08-22 stsp free(tagmsg0);
5938 8e7bd50a 2019-08-22 stsp }
5939 8e7bd50a 2019-08-22 stsp
5940 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
5941 8e7bd50a 2019-08-22 stsp return NULL;
5942 8e7bd50a 2019-08-22 stsp }
5943 8e7bd50a 2019-08-22 stsp
5944 8e7bd50a 2019-08-22 stsp static const struct got_error *
5945 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5946 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
5947 8e7bd50a 2019-08-22 stsp {
5948 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5949 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
5950 f372d5cd 2019-10-21 stsp char *editor = NULL;
5951 1601cb9f 2020-09-11 naddy int initial_content_len;
5952 8e7bd50a 2019-08-22 stsp int fd = -1;
5953 8e7bd50a 2019-08-22 stsp
5954 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5955 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5956 8e7bd50a 2019-08-22 stsp goto done;
5957 8e7bd50a 2019-08-22 stsp }
5958 8e7bd50a 2019-08-22 stsp
5959 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
5960 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
5961 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
5962 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
5963 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5964 8e7bd50a 2019-08-22 stsp goto done;
5965 8e7bd50a 2019-08-22 stsp }
5966 8e7bd50a 2019-08-22 stsp
5967 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5968 8e7bd50a 2019-08-22 stsp if (err)
5969 8e7bd50a 2019-08-22 stsp goto done;
5970 8e7bd50a 2019-08-22 stsp
5971 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
5972 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
5973 97972933 2020-09-11 stsp goto done;
5974 97972933 2020-09-11 stsp }
5975 8e7bd50a 2019-08-22 stsp
5976 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
5977 8e7bd50a 2019-08-22 stsp if (err)
5978 8e7bd50a 2019-08-22 stsp goto done;
5979 bfa12d5e 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
5980 0d5bb276 2020-12-15 stsp initial_content_len, 1);
5981 8e7bd50a 2019-08-22 stsp done:
5982 8e7bd50a 2019-08-22 stsp free(initial_content);
5983 8e7bd50a 2019-08-22 stsp free(template);
5984 8e7bd50a 2019-08-22 stsp free(editor);
5985 8e7bd50a 2019-08-22 stsp
5986 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5987 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
5988 97972933 2020-09-11 stsp
5989 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
5990 59f86c76 2020-09-11 stsp if (err == NULL)
5991 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
5992 59f86c76 2020-09-11 stsp if (err) {
5993 59f86c76 2020-09-11 stsp free(*tagmsg);
5994 59f86c76 2020-09-11 stsp *tagmsg = NULL;
5995 8e7bd50a 2019-08-22 stsp }
5996 8e7bd50a 2019-08-22 stsp return err;
5997 8e7bd50a 2019-08-22 stsp }
5998 8e7bd50a 2019-08-22 stsp
5999 8e7bd50a 2019-08-22 stsp static const struct got_error *
6000 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
6001 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6002 8e7bd50a 2019-08-22 stsp {
6003 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6004 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
6005 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
6006 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
6007 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6008 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
6009 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
6010 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6011 8e7bd50a 2019-08-22 stsp
6012 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6013 84de9106 2020-12-26 stsp
6014 8e7bd50a 2019-08-22 stsp /*
6015 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
6016 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
6017 8e7bd50a 2019-08-22 stsp * an unintended typo.
6018 8e7bd50a 2019-08-22 stsp */
6019 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
6020 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6021 8e7bd50a 2019-08-22 stsp
6022 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
6023 8e7bd50a 2019-08-22 stsp if (err)
6024 8e7bd50a 2019-08-22 stsp return err;
6025 8e7bd50a 2019-08-22 stsp
6026 84de9106 2020-12-26 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6027 84de9106 2020-12-26 stsp if (err)
6028 84de9106 2020-12-26 stsp goto done;
6029 84de9106 2020-12-26 stsp
6030 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6031 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &refs, repo);
6032 8e7bd50a 2019-08-22 stsp if (err)
6033 8e7bd50a 2019-08-22 stsp goto done;
6034 8e7bd50a 2019-08-22 stsp
6035 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
6036 8e7bd50a 2019-08-22 stsp if (err)
6037 8e7bd50a 2019-08-22 stsp goto done;
6038 8e7bd50a 2019-08-22 stsp
6039 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
6040 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
6041 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
6042 62d463ca 2020-10-20 naddy err = got_error_from_errno("strdup");
6043 62d463ca 2020-10-20 naddy goto done;
6044 8e7bd50a 2019-08-22 stsp }
6045 8e7bd50a 2019-08-22 stsp tag_name += 10;
6046 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6047 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
6048 62d463ca 2020-10-20 naddy goto done;
6049 8e7bd50a 2019-08-22 stsp }
6050 8e7bd50a 2019-08-22 stsp
6051 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
6052 8e7bd50a 2019-08-22 stsp if (err == NULL) {
6053 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
6054 8e7bd50a 2019-08-22 stsp goto done;
6055 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
6056 8e7bd50a 2019-08-22 stsp goto done;
6057 8e7bd50a 2019-08-22 stsp
6058 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
6059 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6060 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
6061 f372d5cd 2019-10-21 stsp if (err) {
6062 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6063 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
6064 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6065 8e7bd50a 2019-08-22 stsp goto done;
6066 f372d5cd 2019-10-21 stsp }
6067 8e7bd50a 2019-08-22 stsp }
6068 8e7bd50a 2019-08-22 stsp
6069 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
6070 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6071 f372d5cd 2019-10-21 stsp if (err) {
6072 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6073 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6074 8e7bd50a 2019-08-22 stsp goto done;
6075 f372d5cd 2019-10-21 stsp }
6076 8e7bd50a 2019-08-22 stsp
6077 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
6078 f372d5cd 2019-10-21 stsp if (err) {
6079 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6080 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6081 8e7bd50a 2019-08-22 stsp goto done;
6082 f372d5cd 2019-10-21 stsp }
6083 8e7bd50a 2019-08-22 stsp
6084 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
6085 f372d5cd 2019-10-21 stsp if (err) {
6086 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6087 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6088 f372d5cd 2019-10-21 stsp goto done;
6089 f372d5cd 2019-10-21 stsp }
6090 8e7bd50a 2019-08-22 stsp
6091 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
6092 f372d5cd 2019-10-21 stsp if (err) {
6093 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6094 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6095 f372d5cd 2019-10-21 stsp goto done;
6096 8e7bd50a 2019-08-22 stsp }
6097 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
6098 8e7bd50a 2019-08-22 stsp done:
6099 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
6100 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
6101 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
6102 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6103 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
6104 f372d5cd 2019-10-21 stsp free(tag_id_str);
6105 8e7bd50a 2019-08-22 stsp if (ref)
6106 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
6107 8e7bd50a 2019-08-22 stsp free(commit_id);
6108 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6109 8e7bd50a 2019-08-22 stsp free(refname);
6110 8e7bd50a 2019-08-22 stsp free(tagmsg);
6111 f372d5cd 2019-10-21 stsp free(tagmsg_path);
6112 aba9c984 2019-09-08 stsp free(tagger);
6113 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
6114 8e7bd50a 2019-08-22 stsp return err;
6115 8e7bd50a 2019-08-22 stsp }
6116 8e7bd50a 2019-08-22 stsp
6117 8e7bd50a 2019-08-22 stsp static const struct got_error *
6118 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
6119 8e7bd50a 2019-08-22 stsp {
6120 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
6121 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
6122 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
6123 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6124 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
6125 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6126 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
6127 8e7bd50a 2019-08-22 stsp
6128 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6129 8e7bd50a 2019-08-22 stsp switch (ch) {
6130 80106605 2020-02-24 stsp case 'c':
6131 80106605 2020-02-24 stsp commit_id_arg = optarg;
6132 80106605 2020-02-24 stsp break;
6133 8e7bd50a 2019-08-22 stsp case 'm':
6134 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6135 8e7bd50a 2019-08-22 stsp break;
6136 8e7bd50a 2019-08-22 stsp case 'r':
6137 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6138 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6139 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6140 9ba1d308 2019-10-21 stsp optarg);
6141 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6142 8e7bd50a 2019-08-22 stsp break;
6143 8e7bd50a 2019-08-22 stsp case 'l':
6144 8e7bd50a 2019-08-22 stsp do_list = 1;
6145 8e7bd50a 2019-08-22 stsp break;
6146 8e7bd50a 2019-08-22 stsp default:
6147 8e7bd50a 2019-08-22 stsp usage_tag();
6148 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6149 8e7bd50a 2019-08-22 stsp }
6150 8e7bd50a 2019-08-22 stsp }
6151 8e7bd50a 2019-08-22 stsp
6152 8e7bd50a 2019-08-22 stsp argc -= optind;
6153 8e7bd50a 2019-08-22 stsp argv += optind;
6154 8e7bd50a 2019-08-22 stsp
6155 8e7bd50a 2019-08-22 stsp if (do_list) {
6156 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6157 775ce909 2020-03-22 stsp errx(1,
6158 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
6159 8e7bd50a 2019-08-22 stsp if (tagmsg)
6160 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
6161 8e7bd50a 2019-08-22 stsp if (argc > 0)
6162 8e7bd50a 2019-08-22 stsp usage_tag();
6163 80106605 2020-02-24 stsp } else if (argc != 1)
6164 8e7bd50a 2019-08-22 stsp usage_tag();
6165 80106605 2020-02-24 stsp
6166 a2887370 2019-08-23 stsp tag_name = argv[0];
6167 8e7bd50a 2019-08-22 stsp
6168 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
6169 8e7bd50a 2019-08-22 stsp if (do_list) {
6170 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6171 8e7bd50a 2019-08-22 stsp NULL) == -1)
6172 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6173 8e7bd50a 2019-08-22 stsp } else {
6174 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6175 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
6176 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6177 8e7bd50a 2019-08-22 stsp }
6178 8e7bd50a 2019-08-22 stsp #endif
6179 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
6180 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
6181 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
6182 8e7bd50a 2019-08-22 stsp goto done;
6183 8e7bd50a 2019-08-22 stsp }
6184 8e7bd50a 2019-08-22 stsp
6185 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6186 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6187 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6188 8e7bd50a 2019-08-22 stsp goto done;
6189 8e7bd50a 2019-08-22 stsp else
6190 8e7bd50a 2019-08-22 stsp error = NULL;
6191 8e7bd50a 2019-08-22 stsp if (worktree) {
6192 8e7bd50a 2019-08-22 stsp repo_path =
6193 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
6194 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6195 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6196 8e7bd50a 2019-08-22 stsp if (error)
6197 8e7bd50a 2019-08-22 stsp goto done;
6198 8e7bd50a 2019-08-22 stsp } else {
6199 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
6200 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6201 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6202 8e7bd50a 2019-08-22 stsp goto done;
6203 8e7bd50a 2019-08-22 stsp }
6204 8e7bd50a 2019-08-22 stsp }
6205 8e7bd50a 2019-08-22 stsp }
6206 8e7bd50a 2019-08-22 stsp
6207 8e7bd50a 2019-08-22 stsp if (do_list) {
6208 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6209 c9956ddf 2019-09-08 stsp if (error != NULL)
6210 c9956ddf 2019-09-08 stsp goto done;
6211 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6212 8e7bd50a 2019-08-22 stsp if (error)
6213 8e7bd50a 2019-08-22 stsp goto done;
6214 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
6215 8e7bd50a 2019-08-22 stsp } else {
6216 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6217 c9956ddf 2019-09-08 stsp if (error)
6218 c9956ddf 2019-09-08 stsp goto done;
6219 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
6220 c9956ddf 2019-09-08 stsp if (error != NULL)
6221 c9956ddf 2019-09-08 stsp goto done;
6222 c9956ddf 2019-09-08 stsp
6223 8e7bd50a 2019-08-22 stsp if (tagmsg) {
6224 42a285e2 2020-10-01 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6225 8e7bd50a 2019-08-22 stsp if (error)
6226 8e7bd50a 2019-08-22 stsp goto done;
6227 8e7bd50a 2019-08-22 stsp }
6228 8e7bd50a 2019-08-22 stsp
6229 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
6230 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
6231 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
6232 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
6233 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
6234 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
6235 8e7bd50a 2019-08-22 stsp if (error)
6236 8e7bd50a 2019-08-22 stsp goto done;
6237 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6238 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
6239 8e7bd50a 2019-08-22 stsp if (error)
6240 8e7bd50a 2019-08-22 stsp goto done;
6241 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
6242 8e7bd50a 2019-08-22 stsp free(commit_id);
6243 8e7bd50a 2019-08-22 stsp if (error)
6244 8e7bd50a 2019-08-22 stsp goto done;
6245 8e7bd50a 2019-08-22 stsp }
6246 8e7bd50a 2019-08-22 stsp
6247 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
6248 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6249 8e7bd50a 2019-08-22 stsp }
6250 8e7bd50a 2019-08-22 stsp done:
6251 8e7bd50a 2019-08-22 stsp if (repo)
6252 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
6253 8e7bd50a 2019-08-22 stsp if (worktree)
6254 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
6255 8e7bd50a 2019-08-22 stsp free(cwd);
6256 8e7bd50a 2019-08-22 stsp free(repo_path);
6257 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6258 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6259 8e7bd50a 2019-08-22 stsp return error;
6260 8e7bd50a 2019-08-22 stsp }
6261 8e7bd50a 2019-08-22 stsp
6262 8e7bd50a 2019-08-22 stsp __dead static void
6263 d00136be 2019-03-26 stsp usage_add(void)
6264 d00136be 2019-03-26 stsp {
6265 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6266 022fae89 2019-12-06 tracey getprogname());
6267 d00136be 2019-03-26 stsp exit(1);
6268 d00136be 2019-03-26 stsp }
6269 d00136be 2019-03-26 stsp
6270 d00136be 2019-03-26 stsp static const struct got_error *
6271 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6272 4e68cba3 2019-11-23 stsp {
6273 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6274 4e68cba3 2019-11-23 stsp path++;
6275 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6276 4e68cba3 2019-11-23 stsp return NULL;
6277 4e68cba3 2019-11-23 stsp }
6278 4e68cba3 2019-11-23 stsp
6279 4e68cba3 2019-11-23 stsp static const struct got_error *
6280 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6281 d00136be 2019-03-26 stsp {
6282 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6283 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6284 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6285 1dd54920 2019-05-11 stsp char *cwd = NULL;
6286 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6287 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6288 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6289 1dd54920 2019-05-11 stsp
6290 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6291 d00136be 2019-03-26 stsp
6292 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6293 d00136be 2019-03-26 stsp switch (ch) {
6294 022fae89 2019-12-06 tracey case 'I':
6295 022fae89 2019-12-06 tracey no_ignores = 1;
6296 022fae89 2019-12-06 tracey break;
6297 4e68cba3 2019-11-23 stsp case 'R':
6298 4e68cba3 2019-11-23 stsp can_recurse = 1;
6299 4e68cba3 2019-11-23 stsp break;
6300 d00136be 2019-03-26 stsp default:
6301 d00136be 2019-03-26 stsp usage_add();
6302 d00136be 2019-03-26 stsp /* NOTREACHED */
6303 d00136be 2019-03-26 stsp }
6304 d00136be 2019-03-26 stsp }
6305 d00136be 2019-03-26 stsp
6306 d00136be 2019-03-26 stsp argc -= optind;
6307 d00136be 2019-03-26 stsp argv += optind;
6308 d00136be 2019-03-26 stsp
6309 43012d58 2019-07-14 stsp #ifndef PROFILE
6310 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6311 43012d58 2019-07-14 stsp NULL) == -1)
6312 43012d58 2019-07-14 stsp err(1, "pledge");
6313 43012d58 2019-07-14 stsp #endif
6314 723c305c 2019-05-11 jcs if (argc < 1)
6315 d00136be 2019-03-26 stsp usage_add();
6316 d00136be 2019-03-26 stsp
6317 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6318 d00136be 2019-03-26 stsp if (cwd == NULL) {
6319 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6320 d00136be 2019-03-26 stsp goto done;
6321 d00136be 2019-03-26 stsp }
6322 723c305c 2019-05-11 jcs
6323 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6324 fa51e947 2020-03-27 stsp if (error) {
6325 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6326 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6327 d00136be 2019-03-26 stsp goto done;
6328 fa51e947 2020-03-27 stsp }
6329 d00136be 2019-03-26 stsp
6330 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6331 c9956ddf 2019-09-08 stsp NULL);
6332 031a5338 2019-03-26 stsp if (error != NULL)
6333 031a5338 2019-03-26 stsp goto done;
6334 031a5338 2019-03-26 stsp
6335 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6336 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6337 d00136be 2019-03-26 stsp if (error)
6338 d00136be 2019-03-26 stsp goto done;
6339 d00136be 2019-03-26 stsp
6340 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6341 6d022e97 2019-08-04 stsp if (error)
6342 022fae89 2019-12-06 tracey goto done;
6343 022fae89 2019-12-06 tracey
6344 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
6345 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6346 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
6347 6d022e97 2019-08-04 stsp goto done;
6348 723c305c 2019-05-11 jcs
6349 022fae89 2019-12-06 tracey }
6350 022fae89 2019-12-06 tracey
6351 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6352 4e68cba3 2019-11-23 stsp char *ondisk_path;
6353 4e68cba3 2019-11-23 stsp struct stat sb;
6354 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6355 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6356 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6357 62d463ca 2020-10-20 naddy pe->path) == -1) {
6358 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6359 4e68cba3 2019-11-23 stsp goto done;
6360 4e68cba3 2019-11-23 stsp }
6361 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6362 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6363 4e68cba3 2019-11-23 stsp free(ondisk_path);
6364 4e68cba3 2019-11-23 stsp continue;
6365 4e68cba3 2019-11-23 stsp }
6366 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6367 4e68cba3 2019-11-23 stsp ondisk_path);
6368 4e68cba3 2019-11-23 stsp free(ondisk_path);
6369 4e68cba3 2019-11-23 stsp goto done;
6370 4e68cba3 2019-11-23 stsp }
6371 4e68cba3 2019-11-23 stsp free(ondisk_path);
6372 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6373 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6374 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6375 4e68cba3 2019-11-23 stsp goto done;
6376 4e68cba3 2019-11-23 stsp }
6377 4e68cba3 2019-11-23 stsp }
6378 4e68cba3 2019-11-23 stsp }
6379 022fae89 2019-12-06 tracey
6380 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6381 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6382 d00136be 2019-03-26 stsp done:
6383 031a5338 2019-03-26 stsp if (repo)
6384 031a5338 2019-03-26 stsp got_repo_close(repo);
6385 d00136be 2019-03-26 stsp if (worktree)
6386 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6387 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6388 1dd54920 2019-05-11 stsp free((char *)pe->path);
6389 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6390 2ec1f75b 2019-03-26 stsp free(cwd);
6391 2ec1f75b 2019-03-26 stsp return error;
6392 2ec1f75b 2019-03-26 stsp }
6393 2ec1f75b 2019-03-26 stsp
6394 2ec1f75b 2019-03-26 stsp __dead static void
6395 648e4ef7 2019-07-09 stsp usage_remove(void)
6396 2ec1f75b 2019-03-26 stsp {
6397 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6398 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6399 2ec1f75b 2019-03-26 stsp exit(1);
6400 2a06fe5f 2019-08-24 stsp }
6401 2a06fe5f 2019-08-24 stsp
6402 2a06fe5f 2019-08-24 stsp static const struct got_error *
6403 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6404 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6405 2a06fe5f 2019-08-24 stsp {
6406 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6407 f2a9dc41 2019-12-13 tracey path++;
6408 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6409 2a06fe5f 2019-08-24 stsp return NULL;
6410 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6411 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6412 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6413 2a06fe5f 2019-08-24 stsp return NULL;
6414 2ec1f75b 2019-03-26 stsp }
6415 2ec1f75b 2019-03-26 stsp
6416 2ec1f75b 2019-03-26 stsp static const struct got_error *
6417 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6418 2ec1f75b 2019-03-26 stsp {
6419 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6420 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6421 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6422 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6423 17ed4618 2019-06-02 stsp char *cwd = NULL;
6424 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6425 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6426 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6427 2ec1f75b 2019-03-26 stsp
6428 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6429 17ed4618 2019-06-02 stsp
6430 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6431 2ec1f75b 2019-03-26 stsp switch (ch) {
6432 2ec1f75b 2019-03-26 stsp case 'f':
6433 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6434 2ec1f75b 2019-03-26 stsp break;
6435 70e3e7f5 2019-12-13 tracey case 'k':
6436 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6437 70e3e7f5 2019-12-13 tracey break;
6438 f2a9dc41 2019-12-13 tracey case 'R':
6439 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6440 f2a9dc41 2019-12-13 tracey break;
6441 766841c2 2020-08-13 stsp case 's':
6442 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6443 766841c2 2020-08-13 stsp switch (optarg[i]) {
6444 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6445 766841c2 2020-08-13 stsp delete_local_mods = 1;
6446 766841c2 2020-08-13 stsp break;
6447 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6448 766841c2 2020-08-13 stsp break;
6449 766841c2 2020-08-13 stsp default:
6450 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6451 766841c2 2020-08-13 stsp optarg[i]);
6452 766841c2 2020-08-13 stsp }
6453 766841c2 2020-08-13 stsp }
6454 766841c2 2020-08-13 stsp status_codes = optarg;
6455 766841c2 2020-08-13 stsp break;
6456 2ec1f75b 2019-03-26 stsp default:
6457 f2a9dc41 2019-12-13 tracey usage_remove();
6458 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6459 2ec1f75b 2019-03-26 stsp }
6460 2ec1f75b 2019-03-26 stsp }
6461 2ec1f75b 2019-03-26 stsp
6462 2ec1f75b 2019-03-26 stsp argc -= optind;
6463 2ec1f75b 2019-03-26 stsp argv += optind;
6464 2ec1f75b 2019-03-26 stsp
6465 43012d58 2019-07-14 stsp #ifndef PROFILE
6466 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6467 43012d58 2019-07-14 stsp NULL) == -1)
6468 43012d58 2019-07-14 stsp err(1, "pledge");
6469 43012d58 2019-07-14 stsp #endif
6470 17ed4618 2019-06-02 stsp if (argc < 1)
6471 648e4ef7 2019-07-09 stsp usage_remove();
6472 2ec1f75b 2019-03-26 stsp
6473 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6474 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6475 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6476 2ec1f75b 2019-03-26 stsp goto done;
6477 2ec1f75b 2019-03-26 stsp }
6478 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6479 fa51e947 2020-03-27 stsp if (error) {
6480 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6481 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6482 2ec1f75b 2019-03-26 stsp goto done;
6483 fa51e947 2020-03-27 stsp }
6484 2ec1f75b 2019-03-26 stsp
6485 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6486 c9956ddf 2019-09-08 stsp NULL);
6487 2af4a041 2019-05-11 jcs if (error)
6488 2ec1f75b 2019-03-26 stsp goto done;
6489 2ec1f75b 2019-03-26 stsp
6490 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6491 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6492 2ec1f75b 2019-03-26 stsp if (error)
6493 2ec1f75b 2019-03-26 stsp goto done;
6494 2ec1f75b 2019-03-26 stsp
6495 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6496 6d022e97 2019-08-04 stsp if (error)
6497 6d022e97 2019-08-04 stsp goto done;
6498 17ed4618 2019-06-02 stsp
6499 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6500 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6501 f2a9dc41 2019-12-13 tracey struct stat sb;
6502 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6503 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6504 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6505 62d463ca 2020-10-20 naddy pe->path) == -1) {
6506 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6507 f2a9dc41 2019-12-13 tracey goto done;
6508 f2a9dc41 2019-12-13 tracey }
6509 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6510 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6511 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6512 f2a9dc41 2019-12-13 tracey continue;
6513 f2a9dc41 2019-12-13 tracey }
6514 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6515 f2a9dc41 2019-12-13 tracey ondisk_path);
6516 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6517 f2a9dc41 2019-12-13 tracey goto done;
6518 f2a9dc41 2019-12-13 tracey }
6519 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6520 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6521 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6522 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6523 f2a9dc41 2019-12-13 tracey goto done;
6524 f2a9dc41 2019-12-13 tracey }
6525 f2a9dc41 2019-12-13 tracey }
6526 f2a9dc41 2019-12-13 tracey }
6527 f2a9dc41 2019-12-13 tracey
6528 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6529 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6530 766841c2 2020-08-13 stsp repo, keep_on_disk);
6531 a129376b 2019-03-28 stsp done:
6532 a129376b 2019-03-28 stsp if (repo)
6533 a129376b 2019-03-28 stsp got_repo_close(repo);
6534 a129376b 2019-03-28 stsp if (worktree)
6535 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6536 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6537 17ed4618 2019-06-02 stsp free((char *)pe->path);
6538 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6539 a129376b 2019-03-28 stsp free(cwd);
6540 a129376b 2019-03-28 stsp return error;
6541 a129376b 2019-03-28 stsp }
6542 a129376b 2019-03-28 stsp
6543 a129376b 2019-03-28 stsp __dead static void
6544 a129376b 2019-03-28 stsp usage_revert(void)
6545 a129376b 2019-03-28 stsp {
6546 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6547 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6548 a129376b 2019-03-28 stsp exit(1);
6549 a129376b 2019-03-28 stsp }
6550 a129376b 2019-03-28 stsp
6551 1ee397ad 2019-07-12 stsp static const struct got_error *
6552 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6553 a129376b 2019-03-28 stsp {
6554 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6555 fb9704af 2020-01-27 stsp return NULL;
6556 fb9704af 2020-01-27 stsp
6557 a129376b 2019-03-28 stsp while (path[0] == '/')
6558 a129376b 2019-03-28 stsp path++;
6559 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6560 33aa809d 2019-08-08 stsp return NULL;
6561 33aa809d 2019-08-08 stsp }
6562 33aa809d 2019-08-08 stsp
6563 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6564 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6565 33aa809d 2019-08-08 stsp const char *action;
6566 33aa809d 2019-08-08 stsp };
6567 33aa809d 2019-08-08 stsp
6568 33aa809d 2019-08-08 stsp static const struct got_error *
6569 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6570 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6571 33aa809d 2019-08-08 stsp {
6572 33aa809d 2019-08-08 stsp char *line = NULL;
6573 33aa809d 2019-08-08 stsp size_t linesize = 0;
6574 33aa809d 2019-08-08 stsp ssize_t linelen;
6575 33aa809d 2019-08-08 stsp
6576 33aa809d 2019-08-08 stsp switch (status) {
6577 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6578 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6579 33aa809d 2019-08-08 stsp break;
6580 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6581 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6582 33aa809d 2019-08-08 stsp break;
6583 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6584 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6585 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6586 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6587 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6588 33aa809d 2019-08-08 stsp printf("%s", line);
6589 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6590 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6591 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6592 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6593 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6594 33aa809d 2019-08-08 stsp break;
6595 33aa809d 2019-08-08 stsp default:
6596 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6597 33aa809d 2019-08-08 stsp }
6598 33aa809d 2019-08-08 stsp
6599 33aa809d 2019-08-08 stsp return NULL;
6600 33aa809d 2019-08-08 stsp }
6601 33aa809d 2019-08-08 stsp
6602 33aa809d 2019-08-08 stsp static const struct got_error *
6603 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6604 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6605 33aa809d 2019-08-08 stsp {
6606 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6607 33aa809d 2019-08-08 stsp char *line = NULL;
6608 33aa809d 2019-08-08 stsp size_t linesize = 0;
6609 33aa809d 2019-08-08 stsp ssize_t linelen;
6610 33aa809d 2019-08-08 stsp int resp = ' ';
6611 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6612 33aa809d 2019-08-08 stsp
6613 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6614 33aa809d 2019-08-08 stsp
6615 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6616 33aa809d 2019-08-08 stsp char *nl;
6617 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6618 33aa809d 2019-08-08 stsp a->action);
6619 33aa809d 2019-08-08 stsp if (err)
6620 33aa809d 2019-08-08 stsp return err;
6621 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6622 33aa809d 2019-08-08 stsp if (linelen == -1) {
6623 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6624 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6625 33aa809d 2019-08-08 stsp return NULL;
6626 33aa809d 2019-08-08 stsp }
6627 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6628 33aa809d 2019-08-08 stsp if (nl)
6629 33aa809d 2019-08-08 stsp *nl = '\0';
6630 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6631 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6632 33aa809d 2019-08-08 stsp printf("y\n");
6633 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6634 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6635 33aa809d 2019-08-08 stsp printf("n\n");
6636 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6637 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6638 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6639 33aa809d 2019-08-08 stsp printf("q\n");
6640 33aa809d 2019-08-08 stsp } else
6641 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6642 33aa809d 2019-08-08 stsp free(line);
6643 33aa809d 2019-08-08 stsp return NULL;
6644 33aa809d 2019-08-08 stsp }
6645 33aa809d 2019-08-08 stsp
6646 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6647 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6648 33aa809d 2019-08-08 stsp a->action);
6649 33aa809d 2019-08-08 stsp if (err)
6650 33aa809d 2019-08-08 stsp return err;
6651 33aa809d 2019-08-08 stsp resp = getchar();
6652 33aa809d 2019-08-08 stsp if (resp == '\n')
6653 33aa809d 2019-08-08 stsp resp = getchar();
6654 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6655 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6656 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6657 33aa809d 2019-08-08 stsp resp = ' ';
6658 33aa809d 2019-08-08 stsp }
6659 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6660 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6661 33aa809d 2019-08-08 stsp resp = ' ';
6662 33aa809d 2019-08-08 stsp }
6663 33aa809d 2019-08-08 stsp }
6664 33aa809d 2019-08-08 stsp
6665 33aa809d 2019-08-08 stsp if (resp == 'y')
6666 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6667 33aa809d 2019-08-08 stsp else if (resp == 'n')
6668 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6669 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6670 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6671 33aa809d 2019-08-08 stsp
6672 1ee397ad 2019-07-12 stsp return NULL;
6673 a129376b 2019-03-28 stsp }
6674 a129376b 2019-03-28 stsp
6675 33aa809d 2019-08-08 stsp
6676 a129376b 2019-03-28 stsp static const struct got_error *
6677 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6678 a129376b 2019-03-28 stsp {
6679 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6680 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6681 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6682 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6683 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6684 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6685 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6686 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6687 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6688 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6689 a129376b 2019-03-28 stsp
6690 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6691 e20a8b6f 2019-06-04 stsp
6692 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6693 a129376b 2019-03-28 stsp switch (ch) {
6694 33aa809d 2019-08-08 stsp case 'p':
6695 33aa809d 2019-08-08 stsp pflag = 1;
6696 33aa809d 2019-08-08 stsp break;
6697 33aa809d 2019-08-08 stsp case 'F':
6698 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6699 33aa809d 2019-08-08 stsp break;
6700 0f6d7415 2019-08-08 stsp case 'R':
6701 0f6d7415 2019-08-08 stsp can_recurse = 1;
6702 0f6d7415 2019-08-08 stsp break;
6703 a129376b 2019-03-28 stsp default:
6704 a129376b 2019-03-28 stsp usage_revert();
6705 a129376b 2019-03-28 stsp /* NOTREACHED */
6706 a129376b 2019-03-28 stsp }
6707 a129376b 2019-03-28 stsp }
6708 a129376b 2019-03-28 stsp
6709 a129376b 2019-03-28 stsp argc -= optind;
6710 a129376b 2019-03-28 stsp argv += optind;
6711 a129376b 2019-03-28 stsp
6712 43012d58 2019-07-14 stsp #ifndef PROFILE
6713 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6714 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6715 43012d58 2019-07-14 stsp err(1, "pledge");
6716 43012d58 2019-07-14 stsp #endif
6717 e20a8b6f 2019-06-04 stsp if (argc < 1)
6718 a129376b 2019-03-28 stsp usage_revert();
6719 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6720 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6721 a129376b 2019-03-28 stsp
6722 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6723 a129376b 2019-03-28 stsp if (cwd == NULL) {
6724 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6725 a129376b 2019-03-28 stsp goto done;
6726 a129376b 2019-03-28 stsp }
6727 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6728 fa51e947 2020-03-27 stsp if (error) {
6729 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6730 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6731 2ec1f75b 2019-03-26 stsp goto done;
6732 fa51e947 2020-03-27 stsp }
6733 a129376b 2019-03-28 stsp
6734 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6735 c9956ddf 2019-09-08 stsp NULL);
6736 a129376b 2019-03-28 stsp if (error != NULL)
6737 a129376b 2019-03-28 stsp goto done;
6738 a129376b 2019-03-28 stsp
6739 33aa809d 2019-08-08 stsp if (patch_script_path) {
6740 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6741 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
6742 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
6743 33aa809d 2019-08-08 stsp patch_script_path);
6744 33aa809d 2019-08-08 stsp goto done;
6745 33aa809d 2019-08-08 stsp }
6746 33aa809d 2019-08-08 stsp }
6747 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6748 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6749 a129376b 2019-03-28 stsp if (error)
6750 a129376b 2019-03-28 stsp goto done;
6751 a129376b 2019-03-28 stsp
6752 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6753 6d022e97 2019-08-04 stsp if (error)
6754 6d022e97 2019-08-04 stsp goto done;
6755 00db391e 2019-08-03 stsp
6756 0f6d7415 2019-08-08 stsp if (!can_recurse) {
6757 0f6d7415 2019-08-08 stsp char *ondisk_path;
6758 0f6d7415 2019-08-08 stsp struct stat sb;
6759 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
6760 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
6761 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
6762 62d463ca 2020-10-20 naddy pe->path) == -1) {
6763 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
6764 0f6d7415 2019-08-08 stsp goto done;
6765 0f6d7415 2019-08-08 stsp }
6766 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
6767 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
6768 0f6d7415 2019-08-08 stsp free(ondisk_path);
6769 0f6d7415 2019-08-08 stsp continue;
6770 0f6d7415 2019-08-08 stsp }
6771 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
6772 0f6d7415 2019-08-08 stsp ondisk_path);
6773 0f6d7415 2019-08-08 stsp free(ondisk_path);
6774 0f6d7415 2019-08-08 stsp goto done;
6775 0f6d7415 2019-08-08 stsp }
6776 0f6d7415 2019-08-08 stsp free(ondisk_path);
6777 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
6778 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6779 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
6780 0f6d7415 2019-08-08 stsp goto done;
6781 0f6d7415 2019-08-08 stsp }
6782 0f6d7415 2019-08-08 stsp }
6783 0f6d7415 2019-08-08 stsp }
6784 0f6d7415 2019-08-08 stsp
6785 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6786 33aa809d 2019-08-08 stsp cpa.action = "revert";
6787 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6788 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6789 2ec1f75b 2019-03-26 stsp done:
6790 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6791 33aa809d 2019-08-08 stsp error == NULL)
6792 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6793 2ec1f75b 2019-03-26 stsp if (repo)
6794 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
6795 2ec1f75b 2019-03-26 stsp if (worktree)
6796 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
6797 2ec1f75b 2019-03-26 stsp free(path);
6798 d00136be 2019-03-26 stsp free(cwd);
6799 6bad629b 2019-02-04 stsp return error;
6800 c4296144 2019-05-09 stsp }
6801 c4296144 2019-05-09 stsp
6802 c4296144 2019-05-09 stsp __dead static void
6803 c4296144 2019-05-09 stsp usage_commit(void)
6804 c4296144 2019-05-09 stsp {
6805 35213c7c 2020-07-23 stsp fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6806 5c1e53bc 2019-07-28 stsp getprogname());
6807 c4296144 2019-05-09 stsp exit(1);
6808 33ad4cbe 2019-05-12 jcs }
6809 33ad4cbe 2019-05-12 jcs
6810 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
6811 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
6812 e2ba3d07 2019-05-13 stsp const char *editor;
6813 e0870e44 2019-05-13 stsp const char *worktree_path;
6814 76d98825 2019-06-03 stsp const char *branch_name;
6815 314a6357 2019-05-13 stsp const char *repo_path;
6816 e0870e44 2019-05-13 stsp char *logmsg_path;
6817 e2ba3d07 2019-05-13 stsp
6818 e2ba3d07 2019-05-13 stsp };
6819 e0870e44 2019-05-13 stsp
6820 33ad4cbe 2019-05-12 jcs static const struct got_error *
6821 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6822 33ad4cbe 2019-05-12 jcs void *arg)
6823 33ad4cbe 2019-05-12 jcs {
6824 76d98825 2019-06-03 stsp char *initial_content = NULL;
6825 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
6826 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
6827 e0870e44 2019-05-13 stsp char *template = NULL;
6828 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
6829 1601cb9f 2020-09-11 naddy int initial_content_len;
6830 97972933 2020-09-11 stsp int fd = -1;
6831 33ad4cbe 2019-05-12 jcs size_t len;
6832 33ad4cbe 2019-05-12 jcs
6833 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
6834 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6835 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
6836 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
6837 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
6838 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
6839 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
6840 33ad4cbe 2019-05-12 jcs return NULL;
6841 33ad4cbe 2019-05-12 jcs }
6842 33ad4cbe 2019-05-12 jcs
6843 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6844 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
6845 76d98825 2019-06-03 stsp
6846 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6847 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
6848 1601cb9f 2020-09-11 naddy a->branch_name);
6849 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
6850 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6851 e0870e44 2019-05-13 stsp
6852 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6853 793c30b5 2019-05-13 stsp if (err)
6854 e0870e44 2019-05-13 stsp goto done;
6855 33ad4cbe 2019-05-12 jcs
6856 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6857 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
6858 97972933 2020-09-11 stsp goto done;
6859 97972933 2020-09-11 stsp }
6860 33ad4cbe 2019-05-12 jcs
6861 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
6862 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6863 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
6864 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
6865 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
6866 33ad4cbe 2019-05-12 jcs }
6867 33ad4cbe 2019-05-12 jcs
6868 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
6869 0d5bb276 2020-12-15 stsp initial_content_len, 1);
6870 33ad4cbe 2019-05-12 jcs done:
6871 76d98825 2019-06-03 stsp free(initial_content);
6872 e0870e44 2019-05-13 stsp free(template);
6873 314a6357 2019-05-13 stsp
6874 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6875 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
6876 97972933 2020-09-11 stsp
6877 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
6878 59f86c76 2020-09-11 stsp if (err == NULL)
6879 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6880 59f86c76 2020-09-11 stsp if (err) {
6881 59f86c76 2020-09-11 stsp free(*logmsg);
6882 59f86c76 2020-09-11 stsp *logmsg = NULL;
6883 3ce1b845 2019-07-15 stsp }
6884 33ad4cbe 2019-05-12 jcs return err;
6885 6bad629b 2019-02-04 stsp }
6886 c4296144 2019-05-09 stsp
6887 c4296144 2019-05-09 stsp static const struct got_error *
6888 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
6889 c4296144 2019-05-09 stsp {
6890 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
6891 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
6892 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
6893 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
6894 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
6895 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
6896 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
6897 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6898 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6899 35213c7c 2020-07-23 stsp int allow_bad_symlinks = 0;
6900 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
6901 5c1e53bc 2019-07-28 stsp
6902 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
6903 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
6904 c4296144 2019-05-09 stsp
6905 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "m:S")) != -1) {
6906 c4296144 2019-05-09 stsp switch (ch) {
6907 c4296144 2019-05-09 stsp case 'm':
6908 c4296144 2019-05-09 stsp logmsg = optarg;
6909 c4296144 2019-05-09 stsp break;
6910 35213c7c 2020-07-23 stsp case 'S':
6911 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
6912 35213c7c 2020-07-23 stsp break;
6913 c4296144 2019-05-09 stsp default:
6914 c4296144 2019-05-09 stsp usage_commit();
6915 c4296144 2019-05-09 stsp /* NOTREACHED */
6916 c4296144 2019-05-09 stsp }
6917 c4296144 2019-05-09 stsp }
6918 c4296144 2019-05-09 stsp
6919 c4296144 2019-05-09 stsp argc -= optind;
6920 c4296144 2019-05-09 stsp argv += optind;
6921 c4296144 2019-05-09 stsp
6922 43012d58 2019-07-14 stsp #ifndef PROFILE
6923 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6924 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6925 43012d58 2019-07-14 stsp err(1, "pledge");
6926 43012d58 2019-07-14 stsp #endif
6927 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
6928 c4296144 2019-05-09 stsp if (cwd == NULL) {
6929 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6930 c4296144 2019-05-09 stsp goto done;
6931 c4296144 2019-05-09 stsp }
6932 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
6933 fa51e947 2020-03-27 stsp if (error) {
6934 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6935 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
6936 7d5807f4 2019-07-11 stsp goto done;
6937 fa51e947 2020-03-27 stsp }
6938 7d5807f4 2019-07-11 stsp
6939 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6940 c4296144 2019-05-09 stsp if (error)
6941 a698f62e 2019-07-25 stsp goto done;
6942 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
6943 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
6944 c4296144 2019-05-09 stsp goto done;
6945 a698f62e 2019-07-25 stsp }
6946 5c1e53bc 2019-07-28 stsp
6947 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6948 916f288c 2019-07-30 stsp worktree);
6949 5c1e53bc 2019-07-28 stsp if (error)
6950 5c1e53bc 2019-07-28 stsp goto done;
6951 c4296144 2019-05-09 stsp
6952 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6953 c9956ddf 2019-09-08 stsp if (error)
6954 c9956ddf 2019-09-08 stsp goto done;
6955 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6956 c9956ddf 2019-09-08 stsp gitconfig_path);
6957 c4296144 2019-05-09 stsp if (error != NULL)
6958 c4296144 2019-05-09 stsp goto done;
6959 c4296144 2019-05-09 stsp
6960 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
6961 aba9c984 2019-09-08 stsp if (error)
6962 aba9c984 2019-09-08 stsp return error;
6963 aba9c984 2019-09-08 stsp
6964 314a6357 2019-05-13 stsp /*
6965 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
6966 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
6967 314a6357 2019-05-13 stsp */
6968 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6969 314a6357 2019-05-13 stsp error = get_editor(&editor);
6970 314a6357 2019-05-13 stsp else
6971 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6972 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6973 c4296144 2019-05-09 stsp if (error)
6974 c4296144 2019-05-09 stsp goto done;
6975 c4296144 2019-05-09 stsp
6976 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6977 087fb88c 2019-08-04 stsp if (error)
6978 087fb88c 2019-08-04 stsp goto done;
6979 087fb88c 2019-08-04 stsp
6980 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
6981 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
6982 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6983 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6984 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
6985 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6986 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6987 916f288c 2019-07-30 stsp goto done;
6988 916f288c 2019-07-30 stsp }
6989 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
6990 916f288c 2019-07-30 stsp }
6991 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
6992 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6993 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6994 35213c7c 2020-07-23 stsp print_status, NULL, repo);
6995 e0870e44 2019-05-13 stsp if (error) {
6996 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6997 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
6998 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
6999 c4296144 2019-05-09 stsp goto done;
7000 e0870e44 2019-05-13 stsp }
7001 c4296144 2019-05-09 stsp
7002 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
7003 c4296144 2019-05-09 stsp if (error)
7004 c4296144 2019-05-09 stsp goto done;
7005 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
7006 c4296144 2019-05-09 stsp done:
7007 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
7008 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
7009 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
7010 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7011 7266f21f 2019-10-21 stsp error == NULL)
7012 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7013 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
7014 c4296144 2019-05-09 stsp if (repo)
7015 c4296144 2019-05-09 stsp got_repo_close(repo);
7016 c4296144 2019-05-09 stsp if (worktree)
7017 c4296144 2019-05-09 stsp got_worktree_close(worktree);
7018 c4296144 2019-05-09 stsp free(cwd);
7019 c4296144 2019-05-09 stsp free(id_str);
7020 c9956ddf 2019-09-08 stsp free(gitconfig_path);
7021 e2ba3d07 2019-05-13 stsp free(editor);
7022 aba9c984 2019-09-08 stsp free(author);
7023 234035bc 2019-06-01 stsp return error;
7024 234035bc 2019-06-01 stsp }
7025 234035bc 2019-06-01 stsp
7026 234035bc 2019-06-01 stsp __dead static void
7027 234035bc 2019-06-01 stsp usage_cherrypick(void)
7028 234035bc 2019-06-01 stsp {
7029 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
7030 234035bc 2019-06-01 stsp exit(1);
7031 234035bc 2019-06-01 stsp }
7032 234035bc 2019-06-01 stsp
7033 234035bc 2019-06-01 stsp static const struct got_error *
7034 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
7035 234035bc 2019-06-01 stsp {
7036 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
7037 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
7038 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
7039 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
7040 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
7041 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
7042 234035bc 2019-06-01 stsp struct got_object_qid *pid;
7043 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
7044 9627c110 2020-04-18 stsp int ch;
7045 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7046 234035bc 2019-06-01 stsp
7047 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7048 234035bc 2019-06-01 stsp switch (ch) {
7049 234035bc 2019-06-01 stsp default:
7050 234035bc 2019-06-01 stsp usage_cherrypick();
7051 234035bc 2019-06-01 stsp /* NOTREACHED */
7052 234035bc 2019-06-01 stsp }
7053 234035bc 2019-06-01 stsp }
7054 234035bc 2019-06-01 stsp
7055 234035bc 2019-06-01 stsp argc -= optind;
7056 234035bc 2019-06-01 stsp argv += optind;
7057 234035bc 2019-06-01 stsp
7058 43012d58 2019-07-14 stsp #ifndef PROFILE
7059 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7060 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7061 43012d58 2019-07-14 stsp err(1, "pledge");
7062 43012d58 2019-07-14 stsp #endif
7063 234035bc 2019-06-01 stsp if (argc != 1)
7064 234035bc 2019-06-01 stsp usage_cherrypick();
7065 234035bc 2019-06-01 stsp
7066 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
7067 234035bc 2019-06-01 stsp if (cwd == NULL) {
7068 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
7069 234035bc 2019-06-01 stsp goto done;
7070 234035bc 2019-06-01 stsp }
7071 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
7072 fa51e947 2020-03-27 stsp if (error) {
7073 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7074 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
7075 fa51e947 2020-03-27 stsp cwd);
7076 234035bc 2019-06-01 stsp goto done;
7077 fa51e947 2020-03-27 stsp }
7078 234035bc 2019-06-01 stsp
7079 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7080 c9956ddf 2019-09-08 stsp NULL);
7081 234035bc 2019-06-01 stsp if (error != NULL)
7082 234035bc 2019-06-01 stsp goto done;
7083 234035bc 2019-06-01 stsp
7084 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7085 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7086 234035bc 2019-06-01 stsp if (error)
7087 234035bc 2019-06-01 stsp goto done;
7088 234035bc 2019-06-01 stsp
7089 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7090 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7091 234035bc 2019-06-01 stsp if (error != NULL) {
7092 234035bc 2019-06-01 stsp struct got_reference *ref;
7093 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7094 234035bc 2019-06-01 stsp goto done;
7095 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7096 234035bc 2019-06-01 stsp if (error != NULL)
7097 234035bc 2019-06-01 stsp goto done;
7098 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
7099 234035bc 2019-06-01 stsp got_ref_close(ref);
7100 234035bc 2019-06-01 stsp if (error != NULL)
7101 234035bc 2019-06-01 stsp goto done;
7102 234035bc 2019-06-01 stsp }
7103 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
7104 234035bc 2019-06-01 stsp if (error)
7105 234035bc 2019-06-01 stsp goto done;
7106 234035bc 2019-06-01 stsp
7107 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
7108 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
7109 234035bc 2019-06-01 stsp if (error != NULL)
7110 234035bc 2019-06-01 stsp goto done;
7111 234035bc 2019-06-01 stsp
7112 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7113 234035bc 2019-06-01 stsp if (error) {
7114 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
7115 234035bc 2019-06-01 stsp goto done;
7116 234035bc 2019-06-01 stsp error = NULL;
7117 234035bc 2019-06-01 stsp } else {
7118 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
7119 234035bc 2019-06-01 stsp goto done;
7120 234035bc 2019-06-01 stsp }
7121 234035bc 2019-06-01 stsp
7122 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7123 234035bc 2019-06-01 stsp if (error)
7124 234035bc 2019-06-01 stsp goto done;
7125 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7126 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7127 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7128 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
7129 03415a1a 2019-06-02 stsp NULL);
7130 234035bc 2019-06-01 stsp if (error != NULL)
7131 234035bc 2019-06-01 stsp goto done;
7132 234035bc 2019-06-01 stsp
7133 9627c110 2020-04-18 stsp if (upa.did_something)
7134 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
7135 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7136 234035bc 2019-06-01 stsp done:
7137 234035bc 2019-06-01 stsp if (commit)
7138 234035bc 2019-06-01 stsp got_object_commit_close(commit);
7139 234035bc 2019-06-01 stsp free(commit_id_str);
7140 234035bc 2019-06-01 stsp if (head_ref)
7141 234035bc 2019-06-01 stsp got_ref_close(head_ref);
7142 234035bc 2019-06-01 stsp if (worktree)
7143 234035bc 2019-06-01 stsp got_worktree_close(worktree);
7144 234035bc 2019-06-01 stsp if (repo)
7145 234035bc 2019-06-01 stsp got_repo_close(repo);
7146 c4296144 2019-05-09 stsp return error;
7147 c4296144 2019-05-09 stsp }
7148 5ef14e63 2019-06-02 stsp
7149 5ef14e63 2019-06-02 stsp __dead static void
7150 5ef14e63 2019-06-02 stsp usage_backout(void)
7151 5ef14e63 2019-06-02 stsp {
7152 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7153 5ef14e63 2019-06-02 stsp exit(1);
7154 5ef14e63 2019-06-02 stsp }
7155 5ef14e63 2019-06-02 stsp
7156 5ef14e63 2019-06-02 stsp static const struct got_error *
7157 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
7158 5ef14e63 2019-06-02 stsp {
7159 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
7160 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
7161 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
7162 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
7163 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
7164 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
7165 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
7166 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
7167 9627c110 2020-04-18 stsp int ch;
7168 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7169 5ef14e63 2019-06-02 stsp
7170 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7171 5ef14e63 2019-06-02 stsp switch (ch) {
7172 5ef14e63 2019-06-02 stsp default:
7173 5ef14e63 2019-06-02 stsp usage_backout();
7174 5ef14e63 2019-06-02 stsp /* NOTREACHED */
7175 5ef14e63 2019-06-02 stsp }
7176 5ef14e63 2019-06-02 stsp }
7177 5ef14e63 2019-06-02 stsp
7178 5ef14e63 2019-06-02 stsp argc -= optind;
7179 5ef14e63 2019-06-02 stsp argv += optind;
7180 5ef14e63 2019-06-02 stsp
7181 43012d58 2019-07-14 stsp #ifndef PROFILE
7182 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7183 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7184 43012d58 2019-07-14 stsp err(1, "pledge");
7185 43012d58 2019-07-14 stsp #endif
7186 5ef14e63 2019-06-02 stsp if (argc != 1)
7187 5ef14e63 2019-06-02 stsp usage_backout();
7188 5ef14e63 2019-06-02 stsp
7189 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
7190 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
7191 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
7192 5ef14e63 2019-06-02 stsp goto done;
7193 5ef14e63 2019-06-02 stsp }
7194 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
7195 fa51e947 2020-03-27 stsp if (error) {
7196 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7197 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
7198 5ef14e63 2019-06-02 stsp goto done;
7199 fa51e947 2020-03-27 stsp }
7200 5ef14e63 2019-06-02 stsp
7201 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7202 c9956ddf 2019-09-08 stsp NULL);
7203 5ef14e63 2019-06-02 stsp if (error != NULL)
7204 5ef14e63 2019-06-02 stsp goto done;
7205 5ef14e63 2019-06-02 stsp
7206 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7207 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7208 5ef14e63 2019-06-02 stsp if (error)
7209 5ef14e63 2019-06-02 stsp goto done;
7210 5ef14e63 2019-06-02 stsp
7211 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7212 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7213 5ef14e63 2019-06-02 stsp if (error != NULL) {
7214 5ef14e63 2019-06-02 stsp struct got_reference *ref;
7215 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7216 5ef14e63 2019-06-02 stsp goto done;
7217 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7218 5ef14e63 2019-06-02 stsp if (error != NULL)
7219 5ef14e63 2019-06-02 stsp goto done;
7220 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
7221 5ef14e63 2019-06-02 stsp got_ref_close(ref);
7222 5ef14e63 2019-06-02 stsp if (error != NULL)
7223 5ef14e63 2019-06-02 stsp goto done;
7224 5ef14e63 2019-06-02 stsp }
7225 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
7226 5ef14e63 2019-06-02 stsp if (error)
7227 5ef14e63 2019-06-02 stsp goto done;
7228 5ef14e63 2019-06-02 stsp
7229 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
7230 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
7231 5ef14e63 2019-06-02 stsp if (error != NULL)
7232 5ef14e63 2019-06-02 stsp goto done;
7233 5ef14e63 2019-06-02 stsp
7234 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7235 5ef14e63 2019-06-02 stsp if (error)
7236 5ef14e63 2019-06-02 stsp goto done;
7237 5ef14e63 2019-06-02 stsp
7238 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7239 5ef14e63 2019-06-02 stsp if (error)
7240 5ef14e63 2019-06-02 stsp goto done;
7241 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7242 5ef14e63 2019-06-02 stsp if (pid == NULL) {
7243 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
7244 5ef14e63 2019-06-02 stsp goto done;
7245 5ef14e63 2019-06-02 stsp }
7246 5ef14e63 2019-06-02 stsp
7247 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7248 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
7249 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7250 5ef14e63 2019-06-02 stsp if (error != NULL)
7251 5ef14e63 2019-06-02 stsp goto done;
7252 5ef14e63 2019-06-02 stsp
7253 9627c110 2020-04-18 stsp if (upa.did_something)
7254 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
7255 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7256 5ef14e63 2019-06-02 stsp done:
7257 5ef14e63 2019-06-02 stsp if (commit)
7258 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
7259 5ef14e63 2019-06-02 stsp free(commit_id_str);
7260 5ef14e63 2019-06-02 stsp if (head_ref)
7261 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
7262 818c7501 2019-07-11 stsp if (worktree)
7263 818c7501 2019-07-11 stsp got_worktree_close(worktree);
7264 818c7501 2019-07-11 stsp if (repo)
7265 818c7501 2019-07-11 stsp got_repo_close(repo);
7266 818c7501 2019-07-11 stsp return error;
7267 818c7501 2019-07-11 stsp }
7268 818c7501 2019-07-11 stsp
7269 818c7501 2019-07-11 stsp __dead static void
7270 818c7501 2019-07-11 stsp usage_rebase(void)
7271 818c7501 2019-07-11 stsp {
7272 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7273 af54c8f8 2019-07-11 stsp getprogname());
7274 818c7501 2019-07-11 stsp exit(1);
7275 0ebf8283 2019-07-24 stsp }
7276 0ebf8283 2019-07-24 stsp
7277 0ebf8283 2019-07-24 stsp void
7278 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
7279 0ebf8283 2019-07-24 stsp {
7280 0ebf8283 2019-07-24 stsp char *nl;
7281 0ebf8283 2019-07-24 stsp size_t len;
7282 0ebf8283 2019-07-24 stsp
7283 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
7284 0ebf8283 2019-07-24 stsp if (len > limit)
7285 0ebf8283 2019-07-24 stsp len = limit;
7286 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
7287 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
7288 0ebf8283 2019-07-24 stsp if (nl)
7289 0ebf8283 2019-07-24 stsp *nl = '\0';
7290 0ebf8283 2019-07-24 stsp }
7291 0ebf8283 2019-07-24 stsp
7292 0ebf8283 2019-07-24 stsp static const struct got_error *
7293 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7294 0ebf8283 2019-07-24 stsp {
7295 5943eee2 2019-08-13 stsp const struct got_error *err;
7296 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
7297 5943eee2 2019-08-13 stsp const char *s;
7298 0ebf8283 2019-07-24 stsp
7299 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7300 5943eee2 2019-08-13 stsp if (err)
7301 5943eee2 2019-08-13 stsp return err;
7302 0ebf8283 2019-07-24 stsp
7303 5943eee2 2019-08-13 stsp s = logmsg0;
7304 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
7305 5943eee2 2019-08-13 stsp s++;
7306 5943eee2 2019-08-13 stsp
7307 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
7308 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
7309 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7310 5943eee2 2019-08-13 stsp goto done;
7311 5943eee2 2019-08-13 stsp }
7312 0ebf8283 2019-07-24 stsp
7313 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
7314 5943eee2 2019-08-13 stsp done:
7315 5943eee2 2019-08-13 stsp free(logmsg0);
7316 a0ea4fc0 2020-02-28 stsp return err;
7317 a0ea4fc0 2020-02-28 stsp }
7318 a0ea4fc0 2020-02-28 stsp
7319 a0ea4fc0 2020-02-28 stsp static const struct got_error *
7320 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7321 a0ea4fc0 2020-02-28 stsp {
7322 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
7323 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
7324 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
7325 a0ea4fc0 2020-02-28 stsp
7326 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
7327 a0ea4fc0 2020-02-28 stsp if (err)
7328 a0ea4fc0 2020-02-28 stsp return err;
7329 a0ea4fc0 2020-02-28 stsp
7330 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
7331 a0ea4fc0 2020-02-28 stsp if (err)
7332 a0ea4fc0 2020-02-28 stsp goto done;
7333 a0ea4fc0 2020-02-28 stsp
7334 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
7335 a0ea4fc0 2020-02-28 stsp
7336 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
7337 a0ea4fc0 2020-02-28 stsp if (err)
7338 a0ea4fc0 2020-02-28 stsp goto done;
7339 a0ea4fc0 2020-02-28 stsp
7340 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
7341 a0ea4fc0 2020-02-28 stsp done:
7342 a0ea4fc0 2020-02-28 stsp free(id_str);
7343 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
7344 a0ea4fc0 2020-02-28 stsp free(logmsg);
7345 5943eee2 2019-08-13 stsp return err;
7346 818c7501 2019-07-11 stsp }
7347 818c7501 2019-07-11 stsp
7348 818c7501 2019-07-11 stsp static const struct got_error *
7349 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
7350 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
7351 818c7501 2019-07-11 stsp {
7352 818c7501 2019-07-11 stsp const struct got_error *err;
7353 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7354 818c7501 2019-07-11 stsp
7355 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
7356 818c7501 2019-07-11 stsp if (err)
7357 818c7501 2019-07-11 stsp goto done;
7358 818c7501 2019-07-11 stsp
7359 ff0d2220 2019-07-11 stsp if (new_id) {
7360 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
7361 ff0d2220 2019-07-11 stsp if (err)
7362 ff0d2220 2019-07-11 stsp goto done;
7363 ff0d2220 2019-07-11 stsp }
7364 818c7501 2019-07-11 stsp
7365 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
7366 ff0d2220 2019-07-11 stsp if (new_id_str)
7367 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
7368 0ebf8283 2019-07-24 stsp
7369 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7370 0ebf8283 2019-07-24 stsp if (err)
7371 0ebf8283 2019-07-24 stsp goto done;
7372 0ebf8283 2019-07-24 stsp
7373 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
7374 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7375 818c7501 2019-07-11 stsp done:
7376 818c7501 2019-07-11 stsp free(old_id_str);
7377 818c7501 2019-07-11 stsp free(new_id_str);
7378 272a1371 2020-02-28 stsp free(logmsg);
7379 818c7501 2019-07-11 stsp return err;
7380 818c7501 2019-07-11 stsp }
7381 818c7501 2019-07-11 stsp
7382 1ee397ad 2019-07-12 stsp static const struct got_error *
7383 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7384 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
7385 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
7386 818c7501 2019-07-11 stsp {
7387 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
7388 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
7389 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
7390 818c7501 2019-07-11 stsp }
7391 818c7501 2019-07-11 stsp
7392 818c7501 2019-07-11 stsp static const struct got_error *
7393 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
7394 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7395 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
7396 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7397 ff0d2220 2019-07-11 stsp {
7398 ff0d2220 2019-07-11 stsp const struct got_error *error;
7399 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
7400 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
7401 ff0d2220 2019-07-11 stsp
7402 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7403 ff0d2220 2019-07-11 stsp if (error)
7404 ff0d2220 2019-07-11 stsp return error;
7405 ff0d2220 2019-07-11 stsp
7406 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7407 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
7408 ff0d2220 2019-07-11 stsp if (error) {
7409 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7410 ff0d2220 2019-07-11 stsp goto done;
7411 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
7412 ff0d2220 2019-07-11 stsp } else {
7413 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
7414 ff0d2220 2019-07-11 stsp free(new_commit_id);
7415 ff0d2220 2019-07-11 stsp }
7416 ff0d2220 2019-07-11 stsp done:
7417 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
7418 ff0d2220 2019-07-11 stsp return error;
7419 64c6d990 2019-07-11 stsp }
7420 64c6d990 2019-07-11 stsp
7421 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
7422 64c6d990 2019-07-11 stsp const char *path_prefix;
7423 64c6d990 2019-07-11 stsp size_t len;
7424 8ca9bd68 2019-07-25 stsp int errcode;
7425 64c6d990 2019-07-11 stsp };
7426 64c6d990 2019-07-11 stsp
7427 64c6d990 2019-07-11 stsp static const struct got_error *
7428 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7429 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
7430 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
7431 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
7432 64c6d990 2019-07-11 stsp {
7433 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
7434 64c6d990 2019-07-11 stsp
7435 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7436 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7437 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
7438 64c6d990 2019-07-11 stsp
7439 64c6d990 2019-07-11 stsp return NULL;
7440 64c6d990 2019-07-11 stsp }
7441 64c6d990 2019-07-11 stsp
7442 64c6d990 2019-07-11 stsp static const struct got_error *
7443 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
7444 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
7445 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
7446 64c6d990 2019-07-11 stsp {
7447 64c6d990 2019-07-11 stsp const struct got_error *err;
7448 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7449 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7450 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
7451 64c6d990 2019-07-11 stsp
7452 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
7453 64c6d990 2019-07-11 stsp return NULL;
7454 64c6d990 2019-07-11 stsp
7455 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7456 64c6d990 2019-07-11 stsp if (err)
7457 64c6d990 2019-07-11 stsp goto done;
7458 64c6d990 2019-07-11 stsp
7459 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7460 64c6d990 2019-07-11 stsp if (err)
7461 64c6d990 2019-07-11 stsp goto done;
7462 64c6d990 2019-07-11 stsp
7463 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
7464 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
7465 64c6d990 2019-07-11 stsp if (err)
7466 64c6d990 2019-07-11 stsp goto done;
7467 64c6d990 2019-07-11 stsp
7468 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
7469 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
7470 64c6d990 2019-07-11 stsp if (err)
7471 64c6d990 2019-07-11 stsp goto done;
7472 64c6d990 2019-07-11 stsp
7473 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
7474 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
7475 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
7476 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7477 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
7478 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7479 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7480 64c6d990 2019-07-11 stsp done:
7481 64c6d990 2019-07-11 stsp if (tree1)
7482 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
7483 64c6d990 2019-07-11 stsp if (tree2)
7484 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
7485 64c6d990 2019-07-11 stsp if (commit)
7486 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
7487 64c6d990 2019-07-11 stsp if (parent_commit)
7488 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
7489 64c6d990 2019-07-11 stsp return err;
7490 ff0d2220 2019-07-11 stsp }
7491 ff0d2220 2019-07-11 stsp
7492 ff0d2220 2019-07-11 stsp static const struct got_error *
7493 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
7494 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
7495 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7496 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
7497 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
7498 af61c510 2019-07-19 stsp {
7499 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
7500 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
7501 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
7502 af61c510 2019-07-19 stsp struct got_object_qid *qid;
7503 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
7504 af61c510 2019-07-19 stsp
7505 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
7506 af61c510 2019-07-19 stsp if (err)
7507 af61c510 2019-07-19 stsp return err;
7508 af61c510 2019-07-19 stsp
7509 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7510 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7511 af61c510 2019-07-19 stsp if (err)
7512 af61c510 2019-07-19 stsp goto done;
7513 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7514 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7515 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
7516 af61c510 2019-07-19 stsp if (err) {
7517 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7518 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7519 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
7520 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
7521 af61c510 2019-07-19 stsp "been reached?!?");
7522 ee780d5c 2020-01-04 stsp }
7523 ee780d5c 2020-01-04 stsp goto done;
7524 af61c510 2019-07-19 stsp } else {
7525 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
7526 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
7527 af61c510 2019-07-19 stsp if (err)
7528 af61c510 2019-07-19 stsp goto done;
7529 af61c510 2019-07-19 stsp
7530 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
7531 af61c510 2019-07-19 stsp if (err)
7532 af61c510 2019-07-19 stsp goto done;
7533 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7534 af61c510 2019-07-19 stsp commit_id = parent_id;
7535 af61c510 2019-07-19 stsp }
7536 af61c510 2019-07-19 stsp }
7537 af61c510 2019-07-19 stsp done:
7538 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
7539 af61c510 2019-07-19 stsp return err;
7540 af61c510 2019-07-19 stsp }
7541 af61c510 2019-07-19 stsp
7542 af61c510 2019-07-19 stsp static const struct got_error *
7543 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
7544 818c7501 2019-07-11 stsp {
7545 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
7546 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
7547 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
7548 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7549 818c7501 2019-07-11 stsp char *cwd = NULL;
7550 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
7551 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7552 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
7553 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
7554 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7555 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
7556 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7557 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
7558 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7559 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
7560 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
7561 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
7562 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
7563 818c7501 2019-07-11 stsp
7564 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
7565 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
7566 818c7501 2019-07-11 stsp
7567 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
7568 818c7501 2019-07-11 stsp switch (ch) {
7569 818c7501 2019-07-11 stsp case 'a':
7570 818c7501 2019-07-11 stsp abort_rebase = 1;
7571 818c7501 2019-07-11 stsp break;
7572 818c7501 2019-07-11 stsp case 'c':
7573 818c7501 2019-07-11 stsp continue_rebase = 1;
7574 818c7501 2019-07-11 stsp break;
7575 818c7501 2019-07-11 stsp default:
7576 818c7501 2019-07-11 stsp usage_rebase();
7577 818c7501 2019-07-11 stsp /* NOTREACHED */
7578 818c7501 2019-07-11 stsp }
7579 818c7501 2019-07-11 stsp }
7580 818c7501 2019-07-11 stsp
7581 818c7501 2019-07-11 stsp argc -= optind;
7582 818c7501 2019-07-11 stsp argv += optind;
7583 818c7501 2019-07-11 stsp
7584 43012d58 2019-07-14 stsp #ifndef PROFILE
7585 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7586 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7587 43012d58 2019-07-14 stsp err(1, "pledge");
7588 43012d58 2019-07-14 stsp #endif
7589 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
7590 818c7501 2019-07-11 stsp usage_rebase();
7591 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
7592 818c7501 2019-07-11 stsp if (argc != 0)
7593 818c7501 2019-07-11 stsp usage_rebase();
7594 818c7501 2019-07-11 stsp } else if (argc != 1)
7595 818c7501 2019-07-11 stsp usage_rebase();
7596 818c7501 2019-07-11 stsp
7597 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
7598 818c7501 2019-07-11 stsp if (cwd == NULL) {
7599 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
7600 818c7501 2019-07-11 stsp goto done;
7601 818c7501 2019-07-11 stsp }
7602 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
7603 fa51e947 2020-03-27 stsp if (error) {
7604 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7605 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "rebase", cwd);
7606 818c7501 2019-07-11 stsp goto done;
7607 fa51e947 2020-03-27 stsp }
7608 818c7501 2019-07-11 stsp
7609 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7610 c9956ddf 2019-09-08 stsp NULL);
7611 818c7501 2019-07-11 stsp if (error != NULL)
7612 818c7501 2019-07-11 stsp goto done;
7613 818c7501 2019-07-11 stsp
7614 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7615 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7616 7ef62c4e 2020-02-24 stsp if (error)
7617 7ef62c4e 2020-02-24 stsp goto done;
7618 7ef62c4e 2020-02-24 stsp
7619 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7620 7ef62c4e 2020-02-24 stsp worktree);
7621 818c7501 2019-07-11 stsp if (error)
7622 7ef62c4e 2020-02-24 stsp goto done;
7623 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
7624 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7625 818c7501 2019-07-11 stsp goto done;
7626 7ef62c4e 2020-02-24 stsp }
7627 818c7501 2019-07-11 stsp
7628 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7629 818c7501 2019-07-11 stsp if (error)
7630 818c7501 2019-07-11 stsp goto done;
7631 818c7501 2019-07-11 stsp
7632 f6794adc 2019-07-23 stsp if (abort_rebase) {
7633 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7634 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7635 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7636 f6794adc 2019-07-23 stsp goto done;
7637 f6794adc 2019-07-23 stsp }
7638 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7639 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7640 3e3a69f1 2019-07-25 stsp worktree, repo);
7641 818c7501 2019-07-11 stsp if (error)
7642 818c7501 2019-07-11 stsp goto done;
7643 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
7644 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
7645 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7646 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
7647 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
7648 818c7501 2019-07-11 stsp if (error)
7649 818c7501 2019-07-11 stsp goto done;
7650 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7651 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7652 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
7653 818c7501 2019-07-11 stsp }
7654 818c7501 2019-07-11 stsp
7655 818c7501 2019-07-11 stsp if (continue_rebase) {
7656 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7657 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7658 f6794adc 2019-07-23 stsp goto done;
7659 f6794adc 2019-07-23 stsp }
7660 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7661 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7662 3e3a69f1 2019-07-25 stsp worktree, repo);
7663 818c7501 2019-07-11 stsp if (error)
7664 818c7501 2019-07-11 stsp goto done;
7665 818c7501 2019-07-11 stsp
7666 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7667 01757395 2019-07-12 stsp resume_commit_id, repo);
7668 818c7501 2019-07-11 stsp if (error)
7669 818c7501 2019-07-11 stsp goto done;
7670 818c7501 2019-07-11 stsp
7671 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
7672 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
7673 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
7674 818c7501 2019-07-11 stsp goto done;
7675 818c7501 2019-07-11 stsp }
7676 818c7501 2019-07-11 stsp } else {
7677 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
7678 818c7501 2019-07-11 stsp if (error != NULL)
7679 818c7501 2019-07-11 stsp goto done;
7680 ff0d2220 2019-07-11 stsp }
7681 818c7501 2019-07-11 stsp
7682 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7683 ff0d2220 2019-07-11 stsp if (error)
7684 ff0d2220 2019-07-11 stsp goto done;
7685 ff0d2220 2019-07-11 stsp
7686 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
7687 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
7688 a51a74b3 2019-07-27 stsp
7689 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
7690 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7691 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
7692 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7693 818c7501 2019-07-11 stsp if (error)
7694 818c7501 2019-07-11 stsp goto done;
7695 818c7501 2019-07-11 stsp if (yca_id == NULL) {
7696 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
7697 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
7698 818c7501 2019-07-11 stsp "with work tree's branch");
7699 818c7501 2019-07-11 stsp goto done;
7700 818c7501 2019-07-11 stsp }
7701 818c7501 2019-07-11 stsp
7702 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
7703 a51a74b3 2019-07-27 stsp if (error) {
7704 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
7705 a51a74b3 2019-07-27 stsp goto done;
7706 a51a74b3 2019-07-27 stsp error = NULL;
7707 a51a74b3 2019-07-27 stsp } else {
7708 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7709 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
7710 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
7711 a51a74b3 2019-07-27 stsp goto done;
7712 a51a74b3 2019-07-27 stsp }
7713 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
7714 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
7715 818c7501 2019-07-11 stsp if (error)
7716 818c7501 2019-07-11 stsp goto done;
7717 818c7501 2019-07-11 stsp }
7718 818c7501 2019-07-11 stsp
7719 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
7720 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7721 818c7501 2019-07-11 stsp if (error)
7722 818c7501 2019-07-11 stsp goto done;
7723 818c7501 2019-07-11 stsp
7724 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7725 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
7726 fc66b545 2019-08-12 stsp if (pid == NULL) {
7727 fc66b545 2019-08-12 stsp if (!continue_rebase) {
7728 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7729 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7730 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
7731 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
7732 fc66b545 2019-08-12 stsp if (error)
7733 fc66b545 2019-08-12 stsp goto done;
7734 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
7735 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
7736 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7737 9627c110 2020-04-18 stsp
7738 fc66b545 2019-08-12 stsp }
7739 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
7740 fc66b545 2019-08-12 stsp goto done;
7741 fc66b545 2019-08-12 stsp }
7742 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
7743 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
7744 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
7745 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7746 818c7501 2019-07-11 stsp commit = NULL;
7747 818c7501 2019-07-11 stsp if (error)
7748 818c7501 2019-07-11 stsp goto done;
7749 64c6d990 2019-07-11 stsp
7750 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
7751 38b0338b 2019-11-29 stsp if (continue_rebase) {
7752 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
7753 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
7754 38b0338b 2019-11-29 stsp goto done;
7755 38b0338b 2019-11-29 stsp } else {
7756 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
7757 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
7758 38b0338b 2019-11-29 stsp char *id_str;
7759 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
7760 38b0338b 2019-11-29 stsp new_base_branch);
7761 38b0338b 2019-11-29 stsp if (error)
7762 38b0338b 2019-11-29 stsp goto done;
7763 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
7764 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
7765 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
7766 38b0338b 2019-11-29 stsp free(id_str);
7767 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
7768 38b0338b 2019-11-29 stsp new_head_commit_id);
7769 38b0338b 2019-11-29 stsp if (error)
7770 38b0338b 2019-11-29 stsp goto done;
7771 38b0338b 2019-11-29 stsp }
7772 818c7501 2019-07-11 stsp }
7773 818c7501 2019-07-11 stsp
7774 818c7501 2019-07-11 stsp pid = NULL;
7775 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
7776 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7777 9627c110 2020-04-18 stsp
7778 818c7501 2019-07-11 stsp commit_id = qid->id;
7779 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
7780 818c7501 2019-07-11 stsp pid = qid;
7781 818c7501 2019-07-11 stsp
7782 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7783 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
7784 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
7785 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7786 818c7501 2019-07-11 stsp if (error)
7787 818c7501 2019-07-11 stsp goto done;
7788 9627c110 2020-04-18 stsp
7789 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7790 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
7791 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
7792 818c7501 2019-07-11 stsp
7793 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7794 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
7795 a0ea4fc0 2020-02-28 stsp if (error)
7796 a0ea4fc0 2020-02-28 stsp goto done;
7797 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7798 818c7501 2019-07-11 stsp break;
7799 01757395 2019-07-12 stsp }
7800 818c7501 2019-07-11 stsp
7801 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7802 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
7803 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7804 818c7501 2019-07-11 stsp if (error)
7805 818c7501 2019-07-11 stsp goto done;
7806 818c7501 2019-07-11 stsp }
7807 818c7501 2019-07-11 stsp
7808 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7809 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7810 818c7501 2019-07-11 stsp if (error)
7811 818c7501 2019-07-11 stsp goto done;
7812 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7813 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
7814 818c7501 2019-07-11 stsp } else
7815 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
7816 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
7817 818c7501 2019-07-11 stsp done:
7818 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
7819 818c7501 2019-07-11 stsp free(branch_head_commit_id);
7820 818c7501 2019-07-11 stsp free(resume_commit_id);
7821 818c7501 2019-07-11 stsp free(yca_id);
7822 818c7501 2019-07-11 stsp if (commit)
7823 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7824 818c7501 2019-07-11 stsp if (branch)
7825 818c7501 2019-07-11 stsp got_ref_close(branch);
7826 818c7501 2019-07-11 stsp if (new_base_branch)
7827 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
7828 818c7501 2019-07-11 stsp if (tmp_branch)
7829 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
7830 5ef14e63 2019-06-02 stsp if (worktree)
7831 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
7832 5ef14e63 2019-06-02 stsp if (repo)
7833 5ef14e63 2019-06-02 stsp got_repo_close(repo);
7834 5ef14e63 2019-06-02 stsp return error;
7835 0ebf8283 2019-07-24 stsp }
7836 0ebf8283 2019-07-24 stsp
7837 0ebf8283 2019-07-24 stsp __dead static void
7838 0ebf8283 2019-07-24 stsp usage_histedit(void)
7839 0ebf8283 2019-07-24 stsp {
7840 466785b9 2020-12-10 jrick fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] [-F histedit-script] [-m]\n",
7841 0ebf8283 2019-07-24 stsp getprogname());
7842 0ebf8283 2019-07-24 stsp exit(1);
7843 0ebf8283 2019-07-24 stsp }
7844 0ebf8283 2019-07-24 stsp
7845 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
7846 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
7847 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
7848 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
7849 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
7850 0ebf8283 2019-07-24 stsp
7851 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
7852 0ebf8283 2019-07-24 stsp unsigned char code;
7853 0ebf8283 2019-07-24 stsp const char *name;
7854 0ebf8283 2019-07-24 stsp const char *desc;
7855 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
7856 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7857 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7858 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7859 82997472 2020-01-29 stsp "be used" },
7860 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7861 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
7862 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
7863 0ebf8283 2019-07-24 stsp };
7864 0ebf8283 2019-07-24 stsp
7865 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
7866 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7867 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
7868 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7869 0ebf8283 2019-07-24 stsp char *logmsg;
7870 0ebf8283 2019-07-24 stsp };
7871 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7872 0ebf8283 2019-07-24 stsp
7873 0ebf8283 2019-07-24 stsp static const struct got_error *
7874 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7875 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7876 0ebf8283 2019-07-24 stsp {
7877 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7878 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
7879 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7880 8138f3e1 2019-08-11 stsp int n;
7881 0ebf8283 2019-07-24 stsp
7882 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7883 0ebf8283 2019-07-24 stsp if (err)
7884 0ebf8283 2019-07-24 stsp goto done;
7885 0ebf8283 2019-07-24 stsp
7886 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
7887 0ebf8283 2019-07-24 stsp if (err)
7888 0ebf8283 2019-07-24 stsp goto done;
7889 0ebf8283 2019-07-24 stsp
7890 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
7891 0ebf8283 2019-07-24 stsp if (err)
7892 0ebf8283 2019-07-24 stsp goto done;
7893 0ebf8283 2019-07-24 stsp
7894 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7895 0ebf8283 2019-07-24 stsp if (n < 0)
7896 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7897 0ebf8283 2019-07-24 stsp done:
7898 0ebf8283 2019-07-24 stsp if (commit)
7899 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7900 0ebf8283 2019-07-24 stsp free(id_str);
7901 0ebf8283 2019-07-24 stsp free(logmsg);
7902 0ebf8283 2019-07-24 stsp return err;
7903 0ebf8283 2019-07-24 stsp }
7904 0ebf8283 2019-07-24 stsp
7905 0ebf8283 2019-07-24 stsp static const struct got_error *
7906 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7907 466785b9 2020-12-10 jrick FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
7908 0ebf8283 2019-07-24 stsp {
7909 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7910 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
7911 466785b9 2020-12-10 jrick const char *histedit_cmd = NULL;
7912 0ebf8283 2019-07-24 stsp
7913 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
7914 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7915 0ebf8283 2019-07-24 stsp
7916 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7917 466785b9 2020-12-10 jrick histedit_cmd = got_histedit_cmds[0].name;
7918 466785b9 2020-12-10 jrick if (fold_only && SIMPLEQ_NEXT(qid, entry) != NULL)
7919 466785b9 2020-12-10 jrick histedit_cmd = "fold";
7920 466785b9 2020-12-10 jrick err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
7921 0ebf8283 2019-07-24 stsp if (err)
7922 0ebf8283 2019-07-24 stsp break;
7923 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7924 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7925 083957f4 2020-02-24 stsp if (n < 0) {
7926 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
7927 083957f4 2020-02-24 stsp break;
7928 083957f4 2020-02-24 stsp }
7929 083957f4 2020-02-24 stsp }
7930 0ebf8283 2019-07-24 stsp }
7931 0ebf8283 2019-07-24 stsp
7932 0ebf8283 2019-07-24 stsp return err;
7933 0ebf8283 2019-07-24 stsp }
7934 0ebf8283 2019-07-24 stsp
7935 0ebf8283 2019-07-24 stsp static const struct got_error *
7936 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
7937 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
7938 0ebf8283 2019-07-24 stsp {
7939 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7940 6059809a 2020-12-17 stsp size_t i;
7941 6059809a 2020-12-17 stsp int n;
7942 514f2ffe 2020-01-29 stsp char *id_str;
7943 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
7944 514f2ffe 2020-01-29 stsp
7945 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
7946 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
7947 514f2ffe 2020-01-29 stsp if (err)
7948 514f2ffe 2020-01-29 stsp return err;
7949 514f2ffe 2020-01-29 stsp
7950 514f2ffe 2020-01-29 stsp n = fprintf(f,
7951 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
7952 514f2ffe 2020-01-29 stsp "# commit %s\n"
7953 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
7954 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
7955 514f2ffe 2020-01-29 stsp if (n < 0) {
7956 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7957 514f2ffe 2020-01-29 stsp goto done;
7958 514f2ffe 2020-01-29 stsp }
7959 0ebf8283 2019-07-24 stsp
7960 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
7961 514f2ffe 2020-01-29 stsp if (n < 0) {
7962 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7963 514f2ffe 2020-01-29 stsp goto done;
7964 514f2ffe 2020-01-29 stsp }
7965 0ebf8283 2019-07-24 stsp
7966 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7967 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7968 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7969 0ebf8283 2019-07-24 stsp cmd->desc);
7970 0ebf8283 2019-07-24 stsp if (n < 0) {
7971 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7972 0ebf8283 2019-07-24 stsp break;
7973 0ebf8283 2019-07-24 stsp }
7974 0ebf8283 2019-07-24 stsp }
7975 514f2ffe 2020-01-29 stsp done:
7976 514f2ffe 2020-01-29 stsp free(id_str);
7977 0ebf8283 2019-07-24 stsp return err;
7978 0ebf8283 2019-07-24 stsp }
7979 0ebf8283 2019-07-24 stsp
7980 0ebf8283 2019-07-24 stsp static const struct got_error *
7981 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
7982 0ebf8283 2019-07-24 stsp {
7983 0ebf8283 2019-07-24 stsp static char msg[42];
7984 0ebf8283 2019-07-24 stsp int ret;
7985 0ebf8283 2019-07-24 stsp
7986 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7987 0ebf8283 2019-07-24 stsp lineno);
7988 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
7989 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7990 0ebf8283 2019-07-24 stsp
7991 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7992 0ebf8283 2019-07-24 stsp }
7993 0ebf8283 2019-07-24 stsp
7994 0ebf8283 2019-07-24 stsp static const struct got_error *
7995 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7996 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
7997 0ebf8283 2019-07-24 stsp {
7998 0ebf8283 2019-07-24 stsp const struct got_error *err;
7999 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
8000 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
8001 0ebf8283 2019-07-24 stsp
8002 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8003 0ebf8283 2019-07-24 stsp if (err)
8004 0ebf8283 2019-07-24 stsp return err;
8005 0ebf8283 2019-07-24 stsp
8006 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
8007 0ebf8283 2019-07-24 stsp if (err)
8008 0ebf8283 2019-07-24 stsp goto done;
8009 0ebf8283 2019-07-24 stsp
8010 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
8011 5943eee2 2019-08-13 stsp if (err)
8012 5943eee2 2019-08-13 stsp goto done;
8013 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
8014 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
8015 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
8016 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
8017 0ebf8283 2019-07-24 stsp }
8018 0ebf8283 2019-07-24 stsp done:
8019 0ebf8283 2019-07-24 stsp if (folded_commit)
8020 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
8021 0ebf8283 2019-07-24 stsp free(id_str);
8022 5943eee2 2019-08-13 stsp free(folded_logmsg);
8023 0ebf8283 2019-07-24 stsp return err;
8024 0ebf8283 2019-07-24 stsp }
8025 0ebf8283 2019-07-24 stsp
8026 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
8027 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
8028 0ebf8283 2019-07-24 stsp {
8029 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
8030 0ebf8283 2019-07-24 stsp
8031 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
8032 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
8033 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
8034 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
8035 3f9de99f 2019-07-24 stsp folded = prev;
8036 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
8037 0ebf8283 2019-07-24 stsp }
8038 0ebf8283 2019-07-24 stsp
8039 0ebf8283 2019-07-24 stsp return folded;
8040 0ebf8283 2019-07-24 stsp }
8041 0ebf8283 2019-07-24 stsp
8042 0ebf8283 2019-07-24 stsp static const struct got_error *
8043 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
8044 0ebf8283 2019-07-24 stsp struct got_repository *repo)
8045 0ebf8283 2019-07-24 stsp {
8046 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
8047 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
8048 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8049 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8050 1601cb9f 2020-09-11 naddy int logmsg_len;
8051 0ebf8283 2019-07-24 stsp int fd;
8052 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
8053 0ebf8283 2019-07-24 stsp
8054 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8055 0ebf8283 2019-07-24 stsp if (err)
8056 0ebf8283 2019-07-24 stsp return err;
8057 0ebf8283 2019-07-24 stsp
8058 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
8059 0ebf8283 2019-07-24 stsp if (folded) {
8060 0ebf8283 2019-07-24 stsp while (folded != hle) {
8061 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
8062 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
8063 3f9de99f 2019-07-24 stsp continue;
8064 3f9de99f 2019-07-24 stsp }
8065 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
8066 0ebf8283 2019-07-24 stsp logmsg, repo);
8067 0ebf8283 2019-07-24 stsp if (err)
8068 0ebf8283 2019-07-24 stsp goto done;
8069 0ebf8283 2019-07-24 stsp free(logmsg);
8070 0ebf8283 2019-07-24 stsp logmsg = new_msg;
8071 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
8072 0ebf8283 2019-07-24 stsp }
8073 0ebf8283 2019-07-24 stsp }
8074 0ebf8283 2019-07-24 stsp
8075 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8076 0ebf8283 2019-07-24 stsp if (err)
8077 0ebf8283 2019-07-24 stsp goto done;
8078 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
8079 5943eee2 2019-08-13 stsp if (err)
8080 5943eee2 2019-08-13 stsp goto done;
8081 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
8082 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
8083 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
8084 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
8085 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
8086 0ebf8283 2019-07-24 stsp goto done;
8087 0ebf8283 2019-07-24 stsp }
8088 0ebf8283 2019-07-24 stsp free(logmsg);
8089 0ebf8283 2019-07-24 stsp logmsg = new_msg;
8090 0ebf8283 2019-07-24 stsp
8091 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8092 0ebf8283 2019-07-24 stsp if (err)
8093 0ebf8283 2019-07-24 stsp goto done;
8094 0ebf8283 2019-07-24 stsp
8095 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
8096 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
8097 0ebf8283 2019-07-24 stsp if (err)
8098 0ebf8283 2019-07-24 stsp goto done;
8099 0ebf8283 2019-07-24 stsp
8100 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
8101 0ebf8283 2019-07-24 stsp close(fd);
8102 0ebf8283 2019-07-24 stsp
8103 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8104 0ebf8283 2019-07-24 stsp if (err)
8105 0ebf8283 2019-07-24 stsp goto done;
8106 0ebf8283 2019-07-24 stsp
8107 bfa12d5e 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
8108 0d5bb276 2020-12-15 stsp logmsg_len, 0);
8109 0ebf8283 2019-07-24 stsp if (err) {
8110 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
8111 0ebf8283 2019-07-24 stsp goto done;
8112 71392a05 2020-12-13 stsp err = NULL;
8113 71392a05 2020-12-13 stsp hle->logmsg = strdup(new_msg);
8114 71392a05 2020-12-13 stsp if (hle->logmsg == NULL)
8115 71392a05 2020-12-13 stsp err = got_error_from_errno("strdup");
8116 0ebf8283 2019-07-24 stsp }
8117 0ebf8283 2019-07-24 stsp done:
8118 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
8119 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
8120 0ebf8283 2019-07-24 stsp free(logmsg_path);
8121 0ebf8283 2019-07-24 stsp free(logmsg);
8122 5943eee2 2019-08-13 stsp free(orig_logmsg);
8123 0ebf8283 2019-07-24 stsp free(editor);
8124 0ebf8283 2019-07-24 stsp if (commit)
8125 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8126 0ebf8283 2019-07-24 stsp return err;
8127 5ef14e63 2019-06-02 stsp }
8128 0ebf8283 2019-07-24 stsp
8129 0ebf8283 2019-07-24 stsp static const struct got_error *
8130 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
8131 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
8132 0ebf8283 2019-07-24 stsp {
8133 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8134 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
8135 6059809a 2020-12-17 stsp size_t i, size;
8136 0ebf8283 2019-07-24 stsp ssize_t len;
8137 6059809a 2020-12-17 stsp int lineno = 0;
8138 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
8139 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
8140 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
8141 0ebf8283 2019-07-24 stsp
8142 0ebf8283 2019-07-24 stsp for (;;) {
8143 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
8144 0ebf8283 2019-07-24 stsp if (len == -1) {
8145 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
8146 0ebf8283 2019-07-24 stsp if (feof(f))
8147 0ebf8283 2019-07-24 stsp break;
8148 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
8149 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
8150 0ebf8283 2019-07-24 stsp break;
8151 0ebf8283 2019-07-24 stsp }
8152 0ebf8283 2019-07-24 stsp lineno++;
8153 0ebf8283 2019-07-24 stsp p = line;
8154 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8155 0ebf8283 2019-07-24 stsp p++;
8156 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
8157 0ebf8283 2019-07-24 stsp free(line);
8158 0ebf8283 2019-07-24 stsp line = NULL;
8159 0ebf8283 2019-07-24 stsp continue;
8160 0ebf8283 2019-07-24 stsp }
8161 0ebf8283 2019-07-24 stsp cmd = NULL;
8162 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
8163 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
8164 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
8165 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
8166 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
8167 0ebf8283 2019-07-24 stsp break;
8168 0ebf8283 2019-07-24 stsp }
8169 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
8170 0ebf8283 2019-07-24 stsp p++;
8171 0ebf8283 2019-07-24 stsp break;
8172 0ebf8283 2019-07-24 stsp }
8173 0ebf8283 2019-07-24 stsp }
8174 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
8175 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8176 0ebf8283 2019-07-24 stsp break;
8177 0ebf8283 2019-07-24 stsp }
8178 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8179 0ebf8283 2019-07-24 stsp p++;
8180 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
8181 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
8182 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
8183 0ebf8283 2019-07-24 stsp break;
8184 0ebf8283 2019-07-24 stsp }
8185 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
8186 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8187 0ebf8283 2019-07-24 stsp if (err)
8188 0ebf8283 2019-07-24 stsp break;
8189 0ebf8283 2019-07-24 stsp } else {
8190 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
8191 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
8192 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8193 0ebf8283 2019-07-24 stsp break;
8194 0ebf8283 2019-07-24 stsp }
8195 0ebf8283 2019-07-24 stsp }
8196 0ebf8283 2019-07-24 stsp free(line);
8197 0ebf8283 2019-07-24 stsp line = NULL;
8198 0ebf8283 2019-07-24 stsp continue;
8199 0ebf8283 2019-07-24 stsp } else {
8200 0ebf8283 2019-07-24 stsp end = p;
8201 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
8202 0ebf8283 2019-07-24 stsp end++;
8203 0ebf8283 2019-07-24 stsp *end = '\0';
8204 0ebf8283 2019-07-24 stsp
8205 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
8206 0ebf8283 2019-07-24 stsp if (err) {
8207 0ebf8283 2019-07-24 stsp /* override error code */
8208 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8209 0ebf8283 2019-07-24 stsp break;
8210 0ebf8283 2019-07-24 stsp }
8211 0ebf8283 2019-07-24 stsp }
8212 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
8213 0ebf8283 2019-07-24 stsp if (hle == NULL) {
8214 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
8215 0ebf8283 2019-07-24 stsp break;
8216 0ebf8283 2019-07-24 stsp }
8217 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
8218 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
8219 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
8220 0ebf8283 2019-07-24 stsp commit_id = NULL;
8221 0ebf8283 2019-07-24 stsp free(line);
8222 0ebf8283 2019-07-24 stsp line = NULL;
8223 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
8224 0ebf8283 2019-07-24 stsp }
8225 0ebf8283 2019-07-24 stsp
8226 0ebf8283 2019-07-24 stsp free(line);
8227 0ebf8283 2019-07-24 stsp free(commit_id);
8228 0ebf8283 2019-07-24 stsp return err;
8229 0ebf8283 2019-07-24 stsp }
8230 0ebf8283 2019-07-24 stsp
8231 0ebf8283 2019-07-24 stsp static const struct got_error *
8232 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
8233 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
8234 bfce7f83 2019-07-27 stsp {
8235 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
8236 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
8237 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8238 5b87815e 2020-03-05 stsp static char msg[92];
8239 bfce7f83 2019-07-27 stsp char *id_str;
8240 bfce7f83 2019-07-27 stsp
8241 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
8242 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
8243 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
8244 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
8245 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
8246 5b87815e 2020-03-05 stsp
8247 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8248 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
8249 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
8250 5b87815e 2020-03-05 stsp if (hle == hle2)
8251 5b87815e 2020-03-05 stsp continue;
8252 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
8253 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
8254 5b87815e 2020-03-05 stsp continue;
8255 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
8256 5b87815e 2020-03-05 stsp if (err)
8257 5b87815e 2020-03-05 stsp return err;
8258 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
8259 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
8260 5b87815e 2020-03-05 stsp free(id_str);
8261 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8262 5b87815e 2020-03-05 stsp }
8263 5b87815e 2020-03-05 stsp }
8264 bfce7f83 2019-07-27 stsp
8265 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
8266 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8267 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8268 bfce7f83 2019-07-27 stsp break;
8269 bfce7f83 2019-07-27 stsp }
8270 bfce7f83 2019-07-27 stsp if (hle == NULL) {
8271 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
8272 bfce7f83 2019-07-27 stsp if (err)
8273 bfce7f83 2019-07-27 stsp return err;
8274 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
8275 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
8276 bfce7f83 2019-07-27 stsp free(id_str);
8277 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8278 bfce7f83 2019-07-27 stsp }
8279 bfce7f83 2019-07-27 stsp }
8280 bfce7f83 2019-07-27 stsp
8281 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8282 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8283 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8284 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
8285 bfce7f83 2019-07-27 stsp
8286 bfce7f83 2019-07-27 stsp return NULL;
8287 bfce7f83 2019-07-27 stsp }
8288 bfce7f83 2019-07-27 stsp
8289 bfce7f83 2019-07-27 stsp static const struct got_error *
8290 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
8291 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
8292 bfce7f83 2019-07-27 stsp struct got_repository *repo)
8293 0ebf8283 2019-07-24 stsp {
8294 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8295 0ebf8283 2019-07-24 stsp char *editor;
8296 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8297 0ebf8283 2019-07-24 stsp
8298 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8299 0ebf8283 2019-07-24 stsp if (err)
8300 0ebf8283 2019-07-24 stsp return err;
8301 0ebf8283 2019-07-24 stsp
8302 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
8303 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
8304 0ebf8283 2019-07-24 stsp goto done;
8305 0ebf8283 2019-07-24 stsp }
8306 0ebf8283 2019-07-24 stsp
8307 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8308 0ebf8283 2019-07-24 stsp if (f == NULL) {
8309 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
8310 0ebf8283 2019-07-24 stsp goto done;
8311 0ebf8283 2019-07-24 stsp }
8312 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8313 bfce7f83 2019-07-27 stsp if (err)
8314 bfce7f83 2019-07-27 stsp goto done;
8315 bfce7f83 2019-07-27 stsp
8316 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
8317 0ebf8283 2019-07-24 stsp done:
8318 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8319 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8320 0ebf8283 2019-07-24 stsp free(editor);
8321 0ebf8283 2019-07-24 stsp return err;
8322 0ebf8283 2019-07-24 stsp }
8323 0ebf8283 2019-07-24 stsp
8324 0ebf8283 2019-07-24 stsp static const struct got_error *
8325 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8326 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
8327 514f2ffe 2020-01-29 stsp struct got_repository *);
8328 0ebf8283 2019-07-24 stsp
8329 0ebf8283 2019-07-24 stsp static const struct got_error *
8330 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
8331 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
8332 466785b9 2020-12-10 jrick int edit_logmsg_only, int fold_only, struct got_repository *repo)
8333 0ebf8283 2019-07-24 stsp {
8334 0ebf8283 2019-07-24 stsp const struct got_error *err;
8335 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8336 0ebf8283 2019-07-24 stsp char *path = NULL;
8337 0ebf8283 2019-07-24 stsp
8338 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
8339 0ebf8283 2019-07-24 stsp if (err)
8340 0ebf8283 2019-07-24 stsp return err;
8341 0ebf8283 2019-07-24 stsp
8342 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
8343 0ebf8283 2019-07-24 stsp if (err)
8344 0ebf8283 2019-07-24 stsp goto done;
8345 0ebf8283 2019-07-24 stsp
8346 466785b9 2020-12-10 jrick err = histedit_write_commit_list(commits, f, edit_logmsg_only,
8347 466785b9 2020-12-10 jrick fold_only, repo);
8348 0ebf8283 2019-07-24 stsp if (err)
8349 0ebf8283 2019-07-24 stsp goto done;
8350 0ebf8283 2019-07-24 stsp
8351 466785b9 2020-12-10 jrick if (edit_logmsg_only || fold_only) {
8352 083957f4 2020-02-24 stsp rewind(f);
8353 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8354 083957f4 2020-02-24 stsp } else {
8355 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
8356 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
8357 0ebf8283 2019-07-24 stsp goto done;
8358 083957f4 2020-02-24 stsp }
8359 083957f4 2020-02-24 stsp f = NULL;
8360 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
8361 083957f4 2020-02-24 stsp if (err) {
8362 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8363 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8364 083957f4 2020-02-24 stsp goto done;
8365 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
8366 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
8367 083957f4 2020-02-24 stsp }
8368 0ebf8283 2019-07-24 stsp }
8369 0ebf8283 2019-07-24 stsp done:
8370 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8371 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8372 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
8373 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
8374 0ebf8283 2019-07-24 stsp free(path);
8375 0ebf8283 2019-07-24 stsp return err;
8376 0ebf8283 2019-07-24 stsp }
8377 0ebf8283 2019-07-24 stsp
8378 0ebf8283 2019-07-24 stsp static const struct got_error *
8379 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
8380 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8381 0ebf8283 2019-07-24 stsp {
8382 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8383 0ebf8283 2019-07-24 stsp char *path = NULL;
8384 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8385 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8386 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8387 0ebf8283 2019-07-24 stsp
8388 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
8389 0ebf8283 2019-07-24 stsp if (err)
8390 0ebf8283 2019-07-24 stsp return err;
8391 0ebf8283 2019-07-24 stsp
8392 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
8393 0ebf8283 2019-07-24 stsp if (f == NULL) {
8394 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8395 0ebf8283 2019-07-24 stsp goto done;
8396 0ebf8283 2019-07-24 stsp }
8397 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8398 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8399 0ebf8283 2019-07-24 stsp repo);
8400 0ebf8283 2019-07-24 stsp if (err)
8401 0ebf8283 2019-07-24 stsp break;
8402 0ebf8283 2019-07-24 stsp
8403 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8404 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
8405 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
8406 0ebf8283 2019-07-24 stsp if (n < 0) {
8407 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8408 0ebf8283 2019-07-24 stsp break;
8409 0ebf8283 2019-07-24 stsp }
8410 0ebf8283 2019-07-24 stsp }
8411 0ebf8283 2019-07-24 stsp }
8412 0ebf8283 2019-07-24 stsp done:
8413 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8414 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8415 0ebf8283 2019-07-24 stsp free(path);
8416 0ebf8283 2019-07-24 stsp if (commit)
8417 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8418 0ebf8283 2019-07-24 stsp return err;
8419 0ebf8283 2019-07-24 stsp }
8420 0ebf8283 2019-07-24 stsp
8421 bfce7f83 2019-07-27 stsp void
8422 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
8423 bfce7f83 2019-07-27 stsp {
8424 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8425 bfce7f83 2019-07-27 stsp
8426 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
8427 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
8428 bfce7f83 2019-07-27 stsp free(hle);
8429 bfce7f83 2019-07-27 stsp }
8430 bfce7f83 2019-07-27 stsp }
8431 bfce7f83 2019-07-27 stsp
8432 0ebf8283 2019-07-24 stsp static const struct got_error *
8433 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
8434 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
8435 0ebf8283 2019-07-24 stsp {
8436 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8437 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8438 0ebf8283 2019-07-24 stsp
8439 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8440 0ebf8283 2019-07-24 stsp if (f == NULL) {
8441 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8442 0ebf8283 2019-07-24 stsp goto done;
8443 0ebf8283 2019-07-24 stsp }
8444 0ebf8283 2019-07-24 stsp
8445 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8446 0ebf8283 2019-07-24 stsp done:
8447 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8448 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8449 0ebf8283 2019-07-24 stsp return err;
8450 0ebf8283 2019-07-24 stsp }
8451 0ebf8283 2019-07-24 stsp
8452 0ebf8283 2019-07-24 stsp static const struct got_error *
8453 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8454 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
8455 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
8456 0ebf8283 2019-07-24 stsp {
8457 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
8458 0ebf8283 2019-07-24 stsp int resp = ' ';
8459 0ebf8283 2019-07-24 stsp
8460 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
8461 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8462 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
8463 0ebf8283 2019-07-24 stsp resp = getchar();
8464 a61a4414 2019-08-07 stsp if (resp == '\n')
8465 a61a4414 2019-08-07 stsp resp = getchar();
8466 426ebf2e 2019-07-25 stsp if (resp == 'c') {
8467 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8468 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
8469 bfce7f83 2019-07-27 stsp repo);
8470 426ebf2e 2019-07-25 stsp if (err) {
8471 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8472 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8473 426ebf2e 2019-07-25 stsp break;
8474 bfce7f83 2019-07-27 stsp prev_err = err;
8475 426ebf2e 2019-07-25 stsp resp = ' ';
8476 426ebf2e 2019-07-25 stsp continue;
8477 426ebf2e 2019-07-25 stsp }
8478 bfce7f83 2019-07-27 stsp break;
8479 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
8480 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8481 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
8482 466785b9 2020-12-10 jrick commits, branch_name, 0, 0, repo);
8483 426ebf2e 2019-07-25 stsp if (err) {
8484 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8485 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8486 426ebf2e 2019-07-25 stsp break;
8487 bfce7f83 2019-07-27 stsp prev_err = err;
8488 426ebf2e 2019-07-25 stsp resp = ' ';
8489 426ebf2e 2019-07-25 stsp continue;
8490 426ebf2e 2019-07-25 stsp }
8491 bfce7f83 2019-07-27 stsp break;
8492 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
8493 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8494 0ebf8283 2019-07-24 stsp break;
8495 426ebf2e 2019-07-25 stsp } else
8496 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
8497 0ebf8283 2019-07-24 stsp }
8498 0ebf8283 2019-07-24 stsp
8499 0ebf8283 2019-07-24 stsp return err;
8500 0ebf8283 2019-07-24 stsp }
8501 0ebf8283 2019-07-24 stsp
8502 0ebf8283 2019-07-24 stsp static const struct got_error *
8503 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
8504 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8505 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
8506 0ebf8283 2019-07-24 stsp {
8507 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8508 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8509 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8510 3e3a69f1 2019-07-25 stsp branch, repo);
8511 0ebf8283 2019-07-24 stsp }
8512 0ebf8283 2019-07-24 stsp
8513 0ebf8283 2019-07-24 stsp static const struct got_error *
8514 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
8515 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8516 0ebf8283 2019-07-24 stsp {
8517 0ebf8283 2019-07-24 stsp const struct got_error *err;
8518 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8519 0ebf8283 2019-07-24 stsp
8520 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
8521 0ebf8283 2019-07-24 stsp if (err)
8522 0ebf8283 2019-07-24 stsp goto done;
8523 0ebf8283 2019-07-24 stsp
8524 0ebf8283 2019-07-24 stsp if (new_id) {
8525 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
8526 0ebf8283 2019-07-24 stsp if (err)
8527 0ebf8283 2019-07-24 stsp goto done;
8528 0ebf8283 2019-07-24 stsp }
8529 0ebf8283 2019-07-24 stsp
8530 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
8531 0ebf8283 2019-07-24 stsp if (new_id_str)
8532 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
8533 0ebf8283 2019-07-24 stsp
8534 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8535 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
8536 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
8537 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8538 0ebf8283 2019-07-24 stsp goto done;
8539 0ebf8283 2019-07-24 stsp }
8540 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
8541 0ebf8283 2019-07-24 stsp } else {
8542 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8543 0ebf8283 2019-07-24 stsp if (err)
8544 0ebf8283 2019-07-24 stsp goto done;
8545 0ebf8283 2019-07-24 stsp }
8546 0ebf8283 2019-07-24 stsp
8547 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
8548 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
8549 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
8550 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
8551 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8552 0ebf8283 2019-07-24 stsp break;
8553 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
8554 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
8555 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8556 0ebf8283 2019-07-24 stsp logmsg);
8557 0ebf8283 2019-07-24 stsp break;
8558 0ebf8283 2019-07-24 stsp default:
8559 0ebf8283 2019-07-24 stsp break;
8560 0ebf8283 2019-07-24 stsp }
8561 0ebf8283 2019-07-24 stsp done:
8562 0ebf8283 2019-07-24 stsp free(old_id_str);
8563 0ebf8283 2019-07-24 stsp free(new_id_str);
8564 0ebf8283 2019-07-24 stsp return err;
8565 0ebf8283 2019-07-24 stsp }
8566 0ebf8283 2019-07-24 stsp
8567 0ebf8283 2019-07-24 stsp static const struct got_error *
8568 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
8569 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8570 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8571 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
8572 0ebf8283 2019-07-24 stsp {
8573 0ebf8283 2019-07-24 stsp const struct got_error *err;
8574 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8575 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
8576 0ebf8283 2019-07-24 stsp
8577 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8578 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
8579 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8580 0ebf8283 2019-07-24 stsp if (err)
8581 0ebf8283 2019-07-24 stsp return err;
8582 0ebf8283 2019-07-24 stsp }
8583 0ebf8283 2019-07-24 stsp
8584 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8585 0ebf8283 2019-07-24 stsp if (err)
8586 0ebf8283 2019-07-24 stsp return err;
8587 0ebf8283 2019-07-24 stsp
8588 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8589 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
8590 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
8591 0ebf8283 2019-07-24 stsp if (err) {
8592 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8593 0ebf8283 2019-07-24 stsp goto done;
8594 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
8595 0ebf8283 2019-07-24 stsp } else {
8596 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
8597 0ebf8283 2019-07-24 stsp free(new_commit_id);
8598 0ebf8283 2019-07-24 stsp }
8599 0ebf8283 2019-07-24 stsp done:
8600 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8601 0ebf8283 2019-07-24 stsp return err;
8602 0ebf8283 2019-07-24 stsp }
8603 0ebf8283 2019-07-24 stsp
8604 0ebf8283 2019-07-24 stsp static const struct got_error *
8605 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
8606 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8607 0ebf8283 2019-07-24 stsp {
8608 0ebf8283 2019-07-24 stsp const struct got_error *error;
8609 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8610 0ebf8283 2019-07-24 stsp
8611 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8612 0ebf8283 2019-07-24 stsp repo);
8613 0ebf8283 2019-07-24 stsp if (error)
8614 0ebf8283 2019-07-24 stsp return error;
8615 0ebf8283 2019-07-24 stsp
8616 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8617 0ebf8283 2019-07-24 stsp if (error)
8618 0ebf8283 2019-07-24 stsp return error;
8619 0ebf8283 2019-07-24 stsp
8620 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
8621 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8622 0ebf8283 2019-07-24 stsp return error;
8623 ab20a43a 2020-01-29 stsp }
8624 ab20a43a 2020-01-29 stsp
8625 ab20a43a 2020-01-29 stsp static const struct got_error *
8626 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
8627 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
8628 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8629 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8630 ab20a43a 2020-01-29 stsp {
8631 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
8632 ab20a43a 2020-01-29 stsp
8633 ab20a43a 2020-01-29 stsp switch (status) {
8634 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8635 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8636 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8637 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
8638 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8639 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8640 ab20a43a 2020-01-29 stsp default:
8641 ab20a43a 2020-01-29 stsp break;
8642 ab20a43a 2020-01-29 stsp }
8643 ab20a43a 2020-01-29 stsp
8644 ab20a43a 2020-01-29 stsp switch (staged_status) {
8645 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8646 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8647 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8648 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8649 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8650 ab20a43a 2020-01-29 stsp default:
8651 ab20a43a 2020-01-29 stsp break;
8652 ab20a43a 2020-01-29 stsp }
8653 ab20a43a 2020-01-29 stsp
8654 ab20a43a 2020-01-29 stsp return NULL;
8655 0ebf8283 2019-07-24 stsp }
8656 0ebf8283 2019-07-24 stsp
8657 0ebf8283 2019-07-24 stsp static const struct got_error *
8658 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
8659 0ebf8283 2019-07-24 stsp {
8660 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
8661 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
8662 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8663 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
8664 0ebf8283 2019-07-24 stsp char *cwd = NULL;
8665 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
8666 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
8667 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
8668 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
8669 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
8670 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8671 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
8672 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8673 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8674 466785b9 2020-12-10 jrick int edit_logmsg_only = 0, fold_only = 0;
8675 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
8676 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8677 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
8678 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
8679 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
8680 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
8681 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
8682 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8683 0ebf8283 2019-07-24 stsp
8684 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
8685 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
8686 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
8687 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8688 0ebf8283 2019-07-24 stsp
8689 466785b9 2020-12-10 jrick while ((ch = getopt(argc, argv, "acfF:m")) != -1) {
8690 0ebf8283 2019-07-24 stsp switch (ch) {
8691 0ebf8283 2019-07-24 stsp case 'a':
8692 0ebf8283 2019-07-24 stsp abort_edit = 1;
8693 0ebf8283 2019-07-24 stsp break;
8694 0ebf8283 2019-07-24 stsp case 'c':
8695 0ebf8283 2019-07-24 stsp continue_edit = 1;
8696 0ebf8283 2019-07-24 stsp break;
8697 466785b9 2020-12-10 jrick case 'f':
8698 466785b9 2020-12-10 jrick fold_only = 1;
8699 466785b9 2020-12-10 jrick break;
8700 0ebf8283 2019-07-24 stsp case 'F':
8701 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
8702 0ebf8283 2019-07-24 stsp break;
8703 083957f4 2020-02-24 stsp case 'm':
8704 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
8705 083957f4 2020-02-24 stsp break;
8706 0ebf8283 2019-07-24 stsp default:
8707 0ebf8283 2019-07-24 stsp usage_histedit();
8708 0ebf8283 2019-07-24 stsp /* NOTREACHED */
8709 0ebf8283 2019-07-24 stsp }
8710 0ebf8283 2019-07-24 stsp }
8711 0ebf8283 2019-07-24 stsp
8712 0ebf8283 2019-07-24 stsp argc -= optind;
8713 0ebf8283 2019-07-24 stsp argv += optind;
8714 0ebf8283 2019-07-24 stsp
8715 0ebf8283 2019-07-24 stsp #ifndef PROFILE
8716 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8717 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
8718 0ebf8283 2019-07-24 stsp err(1, "pledge");
8719 0ebf8283 2019-07-24 stsp #endif
8720 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
8721 ff69268e 2020-12-13 stsp option_conflict('a', 'c');
8722 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
8723 ff69268e 2020-12-13 stsp option_conflict('F', 'm');
8724 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
8725 ff69268e 2020-12-13 stsp option_conflict('a', 'm');
8726 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
8727 ff69268e 2020-12-13 stsp option_conflict('c', 'm');
8728 466785b9 2020-12-10 jrick if (abort_edit && fold_only)
8729 ff69268e 2020-12-13 stsp option_conflict('a', 'f');
8730 ff69268e 2020-12-13 stsp if (continue_edit && fold_only)
8731 ff69268e 2020-12-13 stsp option_conflict('c', 'f');
8732 466785b9 2020-12-10 jrick if (fold_only && edit_logmsg_only)
8733 ff69268e 2020-12-13 stsp option_conflict('f', 'm');
8734 b3805337 2020-12-13 stsp if (edit_script_path && fold_only)
8735 b3805337 2020-12-13 stsp option_conflict('F', 'f');
8736 0ebf8283 2019-07-24 stsp if (argc != 0)
8737 0ebf8283 2019-07-24 stsp usage_histedit();
8738 0ebf8283 2019-07-24 stsp
8739 0ebf8283 2019-07-24 stsp /*
8740 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
8741 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
8742 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
8743 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
8744 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
8745 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
8746 0ebf8283 2019-07-24 stsp */
8747 0ebf8283 2019-07-24 stsp
8748 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
8749 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
8750 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
8751 0ebf8283 2019-07-24 stsp goto done;
8752 0ebf8283 2019-07-24 stsp }
8753 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
8754 fa51e947 2020-03-27 stsp if (error) {
8755 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8756 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "histedit", cwd);
8757 0ebf8283 2019-07-24 stsp goto done;
8758 fa51e947 2020-03-27 stsp }
8759 0ebf8283 2019-07-24 stsp
8760 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8761 c9956ddf 2019-09-08 stsp NULL);
8762 0ebf8283 2019-07-24 stsp if (error != NULL)
8763 0ebf8283 2019-07-24 stsp goto done;
8764 0ebf8283 2019-07-24 stsp
8765 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8766 0ebf8283 2019-07-24 stsp if (error)
8767 0ebf8283 2019-07-24 stsp goto done;
8768 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
8769 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
8770 0ebf8283 2019-07-24 stsp goto done;
8771 0ebf8283 2019-07-24 stsp }
8772 0ebf8283 2019-07-24 stsp
8773 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8774 0ebf8283 2019-07-24 stsp if (error)
8775 0ebf8283 2019-07-24 stsp goto done;
8776 0ebf8283 2019-07-24 stsp
8777 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
8778 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8779 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
8780 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
8781 083957f4 2020-02-24 stsp "before the -m option can be used");
8782 083957f4 2020-02-24 stsp goto done;
8783 083957f4 2020-02-24 stsp }
8784 466785b9 2020-12-10 jrick if (edit_in_progress && fold_only) {
8785 466785b9 2020-12-10 jrick error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8786 466785b9 2020-12-10 jrick "histedit operation is in progress in this "
8787 466785b9 2020-12-10 jrick "work tree and must be continued or aborted "
8788 466785b9 2020-12-10 jrick "before the -f option can be used");
8789 466785b9 2020-12-10 jrick goto done;
8790 466785b9 2020-12-10 jrick }
8791 083957f4 2020-02-24 stsp
8792 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
8793 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8794 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8795 3e3a69f1 2019-07-25 stsp worktree, repo);
8796 0ebf8283 2019-07-24 stsp if (error)
8797 0ebf8283 2019-07-24 stsp goto done;
8798 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8799 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8800 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
8801 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
8802 0ebf8283 2019-07-24 stsp if (error)
8803 0ebf8283 2019-07-24 stsp goto done;
8804 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
8805 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8806 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8807 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
8808 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
8809 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8810 0ebf8283 2019-07-24 stsp goto done;
8811 0ebf8283 2019-07-24 stsp }
8812 0ebf8283 2019-07-24 stsp
8813 0ebf8283 2019-07-24 stsp if (continue_edit) {
8814 0ebf8283 2019-07-24 stsp char *path;
8815 0ebf8283 2019-07-24 stsp
8816 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
8817 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8818 0ebf8283 2019-07-24 stsp goto done;
8819 0ebf8283 2019-07-24 stsp }
8820 0ebf8283 2019-07-24 stsp
8821 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
8822 0ebf8283 2019-07-24 stsp if (error)
8823 0ebf8283 2019-07-24 stsp goto done;
8824 0ebf8283 2019-07-24 stsp
8825 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8826 0ebf8283 2019-07-24 stsp free(path);
8827 0ebf8283 2019-07-24 stsp if (error)
8828 0ebf8283 2019-07-24 stsp goto done;
8829 0ebf8283 2019-07-24 stsp
8830 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8831 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8832 3e3a69f1 2019-07-25 stsp worktree, repo);
8833 0ebf8283 2019-07-24 stsp if (error)
8834 0ebf8283 2019-07-24 stsp goto done;
8835 0ebf8283 2019-07-24 stsp
8836 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8837 0ebf8283 2019-07-24 stsp if (error)
8838 0ebf8283 2019-07-24 stsp goto done;
8839 0ebf8283 2019-07-24 stsp
8840 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8841 0ebf8283 2019-07-24 stsp head_commit_id);
8842 0ebf8283 2019-07-24 stsp if (error)
8843 0ebf8283 2019-07-24 stsp goto done;
8844 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8845 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8846 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8847 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8848 3aac7cf7 2019-07-25 stsp goto done;
8849 3aac7cf7 2019-07-25 stsp }
8850 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8851 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8852 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8853 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8854 0ebf8283 2019-07-24 stsp commit = NULL;
8855 0ebf8283 2019-07-24 stsp if (error)
8856 0ebf8283 2019-07-24 stsp goto done;
8857 0ebf8283 2019-07-24 stsp } else {
8858 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
8859 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8860 0ebf8283 2019-07-24 stsp goto done;
8861 0ebf8283 2019-07-24 stsp }
8862 0ebf8283 2019-07-24 stsp
8863 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
8864 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
8865 0ebf8283 2019-07-24 stsp if (error != NULL)
8866 0ebf8283 2019-07-24 stsp goto done;
8867 0ebf8283 2019-07-24 stsp
8868 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8869 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8870 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
8871 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
8872 c7d20a3f 2019-07-30 stsp goto done;
8873 c7d20a3f 2019-07-30 stsp }
8874 c7d20a3f 2019-07-30 stsp
8875 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8876 bfce7f83 2019-07-27 stsp got_ref_close(branch);
8877 bfce7f83 2019-07-27 stsp branch = NULL;
8878 0ebf8283 2019-07-24 stsp if (error)
8879 0ebf8283 2019-07-24 stsp goto done;
8880 0ebf8283 2019-07-24 stsp
8881 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8882 0ebf8283 2019-07-24 stsp head_commit_id);
8883 0ebf8283 2019-07-24 stsp if (error)
8884 0ebf8283 2019-07-24 stsp goto done;
8885 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8886 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8887 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8888 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8889 3aac7cf7 2019-07-25 stsp goto done;
8890 3aac7cf7 2019-07-25 stsp }
8891 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8892 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
8893 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
8894 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8895 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8896 0ebf8283 2019-07-24 stsp commit = NULL;
8897 0ebf8283 2019-07-24 stsp if (error)
8898 0ebf8283 2019-07-24 stsp goto done;
8899 0ebf8283 2019-07-24 stsp
8900 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
8901 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8902 c5996fff 2020-01-29 stsp goto done;
8903 c5996fff 2020-01-29 stsp }
8904 c5996fff 2020-01-29 stsp
8905 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8906 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
8907 bfce7f83 2019-07-27 stsp if (error)
8908 bfce7f83 2019-07-27 stsp goto done;
8909 bfce7f83 2019-07-27 stsp
8910 0ebf8283 2019-07-24 stsp if (edit_script_path) {
8911 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
8912 0ebf8283 2019-07-24 stsp edit_script_path, repo);
8913 6ba2bea2 2019-07-27 stsp if (error) {
8914 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8915 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8916 9627c110 2020-04-18 stsp update_progress, &upa);
8917 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8918 0ebf8283 2019-07-24 stsp goto done;
8919 6ba2bea2 2019-07-27 stsp }
8920 0ebf8283 2019-07-24 stsp } else {
8921 514f2ffe 2020-01-29 stsp const char *branch_name;
8922 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
8923 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8924 514f2ffe 2020-01-29 stsp branch_name += 11;
8925 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8926 466785b9 2020-12-10 jrick branch_name, edit_logmsg_only, fold_only, repo);
8927 6ba2bea2 2019-07-27 stsp if (error) {
8928 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8929 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8930 9627c110 2020-04-18 stsp update_progress, &upa);
8931 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8932 0ebf8283 2019-07-24 stsp goto done;
8933 6ba2bea2 2019-07-27 stsp }
8934 0ebf8283 2019-07-24 stsp
8935 0ebf8283 2019-07-24 stsp }
8936 0ebf8283 2019-07-24 stsp
8937 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
8938 0ebf8283 2019-07-24 stsp repo);
8939 6ba2bea2 2019-07-27 stsp if (error) {
8940 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8941 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8942 9627c110 2020-04-18 stsp update_progress, &upa);
8943 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8944 0ebf8283 2019-07-24 stsp goto done;
8945 6ba2bea2 2019-07-27 stsp }
8946 0ebf8283 2019-07-24 stsp
8947 0ebf8283 2019-07-24 stsp }
8948 0ebf8283 2019-07-24 stsp
8949 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
8950 4ec14e60 2019-08-28 hiltjo if (error)
8951 0ebf8283 2019-07-24 stsp goto done;
8952 0ebf8283 2019-07-24 stsp
8953 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8954 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
8955 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
8956 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
8957 0ebf8283 2019-07-24 stsp continue;
8958 0ebf8283 2019-07-24 stsp
8959 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
8960 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8961 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8962 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
8963 62d463ca 2020-10-20 naddy repo);
8964 ab20a43a 2020-01-29 stsp if (error)
8965 ab20a43a 2020-01-29 stsp goto done;
8966 0ebf8283 2019-07-24 stsp } else {
8967 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
8968 ab20a43a 2020-01-29 stsp int have_changes = 0;
8969 ab20a43a 2020-01-29 stsp
8970 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
8971 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
8972 ab20a43a 2020-01-29 stsp if (error)
8973 ab20a43a 2020-01-29 stsp goto done;
8974 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
8975 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
8976 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
8977 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
8978 ab20a43a 2020-01-29 stsp if (error) {
8979 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
8980 ab20a43a 2020-01-29 stsp goto done;
8981 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
8982 ab20a43a 2020-01-29 stsp goto done;
8983 ab20a43a 2020-01-29 stsp }
8984 ab20a43a 2020-01-29 stsp if (have_changes) {
8985 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
8986 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
8987 ab20a43a 2020-01-29 stsp if (error)
8988 ab20a43a 2020-01-29 stsp goto done;
8989 ab20a43a 2020-01-29 stsp } else {
8990 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
8991 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
8992 ab20a43a 2020-01-29 stsp if (error)
8993 ab20a43a 2020-01-29 stsp goto done;
8994 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
8995 ab20a43a 2020-01-29 stsp hle, NULL);
8996 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
8997 ab20a43a 2020-01-29 stsp commit = NULL;
8998 ab20a43a 2020-01-29 stsp if (error)
8999 ab20a43a 2020-01-29 stsp goto done;
9000 ab20a43a 2020-01-29 stsp }
9001 0ebf8283 2019-07-24 stsp }
9002 0ebf8283 2019-07-24 stsp continue;
9003 0ebf8283 2019-07-24 stsp }
9004 0ebf8283 2019-07-24 stsp
9005 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
9006 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
9007 0ebf8283 2019-07-24 stsp if (error)
9008 0ebf8283 2019-07-24 stsp goto done;
9009 0ebf8283 2019-07-24 stsp continue;
9010 0ebf8283 2019-07-24 stsp }
9011 0ebf8283 2019-07-24 stsp
9012 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
9013 0ebf8283 2019-07-24 stsp hle->commit_id);
9014 0ebf8283 2019-07-24 stsp if (error)
9015 0ebf8283 2019-07-24 stsp goto done;
9016 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9017 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
9018 0ebf8283 2019-07-24 stsp
9019 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
9020 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
9021 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
9022 0ebf8283 2019-07-24 stsp if (error)
9023 0ebf8283 2019-07-24 stsp goto done;
9024 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9025 0ebf8283 2019-07-24 stsp commit = NULL;
9026 0ebf8283 2019-07-24 stsp
9027 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9028 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
9029 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
9030 9627c110 2020-04-18 stsp
9031 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
9032 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
9033 a0ea4fc0 2020-02-28 stsp repo);
9034 a0ea4fc0 2020-02-28 stsp if (error)
9035 a0ea4fc0 2020-02-28 stsp goto done;
9036 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9037 0ebf8283 2019-07-24 stsp break;
9038 0ebf8283 2019-07-24 stsp }
9039 0ebf8283 2019-07-24 stsp
9040 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
9041 0ebf8283 2019-07-24 stsp char *id_str;
9042 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
9043 0ebf8283 2019-07-24 stsp if (error)
9044 0ebf8283 2019-07-24 stsp goto done;
9045 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
9046 0ebf8283 2019-07-24 stsp id_str);
9047 0ebf8283 2019-07-24 stsp free(id_str);
9048 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9049 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
9050 3e3a69f1 2019-07-25 stsp fileindex);
9051 0ebf8283 2019-07-24 stsp goto done;
9052 0ebf8283 2019-07-24 stsp }
9053 0ebf8283 2019-07-24 stsp
9054 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
9055 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
9056 0ebf8283 2019-07-24 stsp if (error)
9057 0ebf8283 2019-07-24 stsp goto done;
9058 0ebf8283 2019-07-24 stsp continue;
9059 0ebf8283 2019-07-24 stsp }
9060 0ebf8283 2019-07-24 stsp
9061 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
9062 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
9063 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9064 0ebf8283 2019-07-24 stsp if (error)
9065 0ebf8283 2019-07-24 stsp goto done;
9066 0ebf8283 2019-07-24 stsp }
9067 0ebf8283 2019-07-24 stsp
9068 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
9069 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
9070 0ebf8283 2019-07-24 stsp if (error)
9071 0ebf8283 2019-07-24 stsp goto done;
9072 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
9073 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
9074 0ebf8283 2019-07-24 stsp } else
9075 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
9076 3e3a69f1 2019-07-25 stsp branch, repo);
9077 0ebf8283 2019-07-24 stsp done:
9078 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
9079 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
9080 0ebf8283 2019-07-24 stsp free(head_commit_id);
9081 0ebf8283 2019-07-24 stsp free(base_commit_id);
9082 0ebf8283 2019-07-24 stsp free(resume_commit_id);
9083 0ebf8283 2019-07-24 stsp if (commit)
9084 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9085 0ebf8283 2019-07-24 stsp if (branch)
9086 0ebf8283 2019-07-24 stsp got_ref_close(branch);
9087 0ebf8283 2019-07-24 stsp if (tmp_branch)
9088 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
9089 0ebf8283 2019-07-24 stsp if (worktree)
9090 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
9091 2822a352 2019-10-15 stsp if (repo)
9092 2822a352 2019-10-15 stsp got_repo_close(repo);
9093 2822a352 2019-10-15 stsp return error;
9094 2822a352 2019-10-15 stsp }
9095 2822a352 2019-10-15 stsp
9096 2822a352 2019-10-15 stsp __dead static void
9097 2822a352 2019-10-15 stsp usage_integrate(void)
9098 2822a352 2019-10-15 stsp {
9099 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
9100 2822a352 2019-10-15 stsp exit(1);
9101 2822a352 2019-10-15 stsp }
9102 2822a352 2019-10-15 stsp
9103 2822a352 2019-10-15 stsp static const struct got_error *
9104 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
9105 2822a352 2019-10-15 stsp {
9106 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
9107 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
9108 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
9109 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
9110 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
9111 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
9112 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
9113 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
9114 9627c110 2020-04-18 stsp int ch;
9115 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9116 2822a352 2019-10-15 stsp
9117 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9118 2822a352 2019-10-15 stsp switch (ch) {
9119 2822a352 2019-10-15 stsp default:
9120 2822a352 2019-10-15 stsp usage_integrate();
9121 2822a352 2019-10-15 stsp /* NOTREACHED */
9122 2822a352 2019-10-15 stsp }
9123 2822a352 2019-10-15 stsp }
9124 2822a352 2019-10-15 stsp
9125 2822a352 2019-10-15 stsp argc -= optind;
9126 2822a352 2019-10-15 stsp argv += optind;
9127 2822a352 2019-10-15 stsp
9128 2822a352 2019-10-15 stsp if (argc != 1)
9129 2822a352 2019-10-15 stsp usage_integrate();
9130 2822a352 2019-10-15 stsp branch_arg = argv[0];
9131 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
9132 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9133 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
9134 2822a352 2019-10-15 stsp err(1, "pledge");
9135 b08a0ccd 2020-09-24 stsp #endif
9136 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
9137 2822a352 2019-10-15 stsp if (cwd == NULL) {
9138 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
9139 2822a352 2019-10-15 stsp goto done;
9140 2822a352 2019-10-15 stsp }
9141 2822a352 2019-10-15 stsp
9142 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
9143 fa51e947 2020-03-27 stsp if (error) {
9144 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9145 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
9146 fa51e947 2020-03-27 stsp cwd);
9147 2822a352 2019-10-15 stsp goto done;
9148 fa51e947 2020-03-27 stsp }
9149 2822a352 2019-10-15 stsp
9150 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
9151 2822a352 2019-10-15 stsp if (error)
9152 2822a352 2019-10-15 stsp goto done;
9153 2822a352 2019-10-15 stsp
9154 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9155 2822a352 2019-10-15 stsp NULL);
9156 2822a352 2019-10-15 stsp if (error != NULL)
9157 2822a352 2019-10-15 stsp goto done;
9158 2822a352 2019-10-15 stsp
9159 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9160 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
9161 2822a352 2019-10-15 stsp if (error)
9162 2822a352 2019-10-15 stsp goto done;
9163 2822a352 2019-10-15 stsp
9164 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
9165 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
9166 2822a352 2019-10-15 stsp goto done;
9167 2822a352 2019-10-15 stsp }
9168 2822a352 2019-10-15 stsp
9169 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
9170 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
9171 2822a352 2019-10-15 stsp if (error)
9172 2822a352 2019-10-15 stsp goto done;
9173 2822a352 2019-10-15 stsp
9174 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
9175 2822a352 2019-10-15 stsp if (refname == NULL) {
9176 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9177 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9178 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9179 2822a352 2019-10-15 stsp goto done;
9180 2822a352 2019-10-15 stsp }
9181 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
9182 2822a352 2019-10-15 stsp if (base_refname == NULL) {
9183 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9184 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9185 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9186 2822a352 2019-10-15 stsp goto done;
9187 2822a352 2019-10-15 stsp }
9188 2822a352 2019-10-15 stsp
9189 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
9190 2822a352 2019-10-15 stsp if (error)
9191 2822a352 2019-10-15 stsp goto done;
9192 2822a352 2019-10-15 stsp
9193 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
9194 2822a352 2019-10-15 stsp if (error)
9195 2822a352 2019-10-15 stsp goto done;
9196 2822a352 2019-10-15 stsp
9197 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
9198 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
9199 2822a352 2019-10-15 stsp "specified branch has already been integrated");
9200 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9201 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9202 2822a352 2019-10-15 stsp goto done;
9203 2822a352 2019-10-15 stsp }
9204 2822a352 2019-10-15 stsp
9205 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
9206 2822a352 2019-10-15 stsp if (error) {
9207 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
9208 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
9209 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9210 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9211 2822a352 2019-10-15 stsp goto done;
9212 2822a352 2019-10-15 stsp }
9213 2822a352 2019-10-15 stsp
9214 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9215 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
9216 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
9217 2822a352 2019-10-15 stsp check_cancelled, NULL);
9218 2822a352 2019-10-15 stsp if (error)
9219 2822a352 2019-10-15 stsp goto done;
9220 2822a352 2019-10-15 stsp
9221 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
9222 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9223 2822a352 2019-10-15 stsp done:
9224 715dc77e 2019-08-03 stsp if (repo)
9225 715dc77e 2019-08-03 stsp got_repo_close(repo);
9226 2822a352 2019-10-15 stsp if (worktree)
9227 2822a352 2019-10-15 stsp got_worktree_close(worktree);
9228 2822a352 2019-10-15 stsp free(cwd);
9229 2822a352 2019-10-15 stsp free(base_commit_id);
9230 2822a352 2019-10-15 stsp free(commit_id);
9231 2822a352 2019-10-15 stsp free(refname);
9232 2822a352 2019-10-15 stsp free(base_refname);
9233 715dc77e 2019-08-03 stsp return error;
9234 715dc77e 2019-08-03 stsp }
9235 715dc77e 2019-08-03 stsp
9236 715dc77e 2019-08-03 stsp __dead static void
9237 715dc77e 2019-08-03 stsp usage_stage(void)
9238 715dc77e 2019-08-03 stsp {
9239 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
9240 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
9241 715dc77e 2019-08-03 stsp getprogname());
9242 715dc77e 2019-08-03 stsp exit(1);
9243 715dc77e 2019-08-03 stsp }
9244 715dc77e 2019-08-03 stsp
9245 715dc77e 2019-08-03 stsp static const struct got_error *
9246 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
9247 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
9248 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
9249 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
9250 408b4ebc 2019-08-03 stsp {
9251 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
9252 408b4ebc 2019-08-03 stsp char *id_str = NULL;
9253 408b4ebc 2019-08-03 stsp
9254 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
9255 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
9256 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
9257 408b4ebc 2019-08-03 stsp return NULL;
9258 408b4ebc 2019-08-03 stsp
9259 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
9260 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
9261 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
9262 408b4ebc 2019-08-03 stsp else
9263 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
9264 408b4ebc 2019-08-03 stsp if (err)
9265 408b4ebc 2019-08-03 stsp return err;
9266 408b4ebc 2019-08-03 stsp
9267 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
9268 408b4ebc 2019-08-03 stsp free(id_str);
9269 dc424a06 2019-08-07 stsp return NULL;
9270 dc424a06 2019-08-07 stsp }
9271 2e1f37b0 2019-08-08 stsp
9272 dc424a06 2019-08-07 stsp static const struct got_error *
9273 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
9274 715dc77e 2019-08-03 stsp {
9275 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
9276 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
9277 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
9278 715dc77e 2019-08-03 stsp char *cwd = NULL;
9279 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
9280 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
9281 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
9282 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
9283 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9284 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9285 715dc77e 2019-08-03 stsp
9286 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
9287 715dc77e 2019-08-03 stsp
9288 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9289 715dc77e 2019-08-03 stsp switch (ch) {
9290 408b4ebc 2019-08-03 stsp case 'l':
9291 408b4ebc 2019-08-03 stsp list_stage = 1;
9292 408b4ebc 2019-08-03 stsp break;
9293 dc424a06 2019-08-07 stsp case 'p':
9294 dc424a06 2019-08-07 stsp pflag = 1;
9295 dc424a06 2019-08-07 stsp break;
9296 dc424a06 2019-08-07 stsp case 'F':
9297 dc424a06 2019-08-07 stsp patch_script_path = optarg;
9298 dc424a06 2019-08-07 stsp break;
9299 35213c7c 2020-07-23 stsp case 'S':
9300 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
9301 35213c7c 2020-07-23 stsp break;
9302 715dc77e 2019-08-03 stsp default:
9303 715dc77e 2019-08-03 stsp usage_stage();
9304 715dc77e 2019-08-03 stsp /* NOTREACHED */
9305 715dc77e 2019-08-03 stsp }
9306 715dc77e 2019-08-03 stsp }
9307 715dc77e 2019-08-03 stsp
9308 715dc77e 2019-08-03 stsp argc -= optind;
9309 715dc77e 2019-08-03 stsp argv += optind;
9310 715dc77e 2019-08-03 stsp
9311 715dc77e 2019-08-03 stsp #ifndef PROFILE
9312 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9313 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
9314 715dc77e 2019-08-03 stsp err(1, "pledge");
9315 715dc77e 2019-08-03 stsp #endif
9316 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
9317 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
9318 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
9319 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
9320 715dc77e 2019-08-03 stsp
9321 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
9322 715dc77e 2019-08-03 stsp if (cwd == NULL) {
9323 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
9324 715dc77e 2019-08-03 stsp goto done;
9325 715dc77e 2019-08-03 stsp }
9326 715dc77e 2019-08-03 stsp
9327 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9328 fa51e947 2020-03-27 stsp if (error) {
9329 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9330 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
9331 715dc77e 2019-08-03 stsp goto done;
9332 fa51e947 2020-03-27 stsp }
9333 715dc77e 2019-08-03 stsp
9334 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9335 c9956ddf 2019-09-08 stsp NULL);
9336 715dc77e 2019-08-03 stsp if (error != NULL)
9337 715dc77e 2019-08-03 stsp goto done;
9338 715dc77e 2019-08-03 stsp
9339 dc424a06 2019-08-07 stsp if (patch_script_path) {
9340 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
9341 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
9342 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
9343 dc424a06 2019-08-07 stsp patch_script_path);
9344 dc424a06 2019-08-07 stsp goto done;
9345 dc424a06 2019-08-07 stsp }
9346 dc424a06 2019-08-07 stsp }
9347 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9348 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
9349 715dc77e 2019-08-03 stsp if (error)
9350 715dc77e 2019-08-03 stsp goto done;
9351 715dc77e 2019-08-03 stsp
9352 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9353 6d022e97 2019-08-04 stsp if (error)
9354 6d022e97 2019-08-04 stsp goto done;
9355 715dc77e 2019-08-03 stsp
9356 6d022e97 2019-08-04 stsp if (list_stage)
9357 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
9358 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
9359 2e1f37b0 2019-08-08 stsp else {
9360 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9361 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
9362 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
9363 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
9364 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
9365 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
9366 2e1f37b0 2019-08-08 stsp }
9367 ad493afc 2019-08-03 stsp done:
9368 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9369 2e1f37b0 2019-08-08 stsp error == NULL)
9370 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9371 ad493afc 2019-08-03 stsp if (repo)
9372 ad493afc 2019-08-03 stsp got_repo_close(repo);
9373 ad493afc 2019-08-03 stsp if (worktree)
9374 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
9375 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9376 ad493afc 2019-08-03 stsp free((char *)pe->path);
9377 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
9378 ad493afc 2019-08-03 stsp free(cwd);
9379 ad493afc 2019-08-03 stsp return error;
9380 ad493afc 2019-08-03 stsp }
9381 ad493afc 2019-08-03 stsp
9382 ad493afc 2019-08-03 stsp __dead static void
9383 ad493afc 2019-08-03 stsp usage_unstage(void)
9384 ad493afc 2019-08-03 stsp {
9385 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9386 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
9387 ad493afc 2019-08-03 stsp getprogname());
9388 ad493afc 2019-08-03 stsp exit(1);
9389 ad493afc 2019-08-03 stsp }
9390 ad493afc 2019-08-03 stsp
9391 ad493afc 2019-08-03 stsp
9392 ad493afc 2019-08-03 stsp static const struct got_error *
9393 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
9394 ad493afc 2019-08-03 stsp {
9395 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
9396 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
9397 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
9398 ad493afc 2019-08-03 stsp char *cwd = NULL;
9399 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
9400 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9401 9627c110 2020-04-18 stsp int ch, pflag = 0;
9402 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9403 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
9404 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9405 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9406 ad493afc 2019-08-03 stsp
9407 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
9408 ad493afc 2019-08-03 stsp
9409 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
9410 ad493afc 2019-08-03 stsp switch (ch) {
9411 2e1f37b0 2019-08-08 stsp case 'p':
9412 2e1f37b0 2019-08-08 stsp pflag = 1;
9413 2e1f37b0 2019-08-08 stsp break;
9414 2e1f37b0 2019-08-08 stsp case 'F':
9415 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
9416 2e1f37b0 2019-08-08 stsp break;
9417 ad493afc 2019-08-03 stsp default:
9418 ad493afc 2019-08-03 stsp usage_unstage();
9419 ad493afc 2019-08-03 stsp /* NOTREACHED */
9420 ad493afc 2019-08-03 stsp }
9421 ad493afc 2019-08-03 stsp }
9422 ad493afc 2019-08-03 stsp
9423 ad493afc 2019-08-03 stsp argc -= optind;
9424 ad493afc 2019-08-03 stsp argv += optind;
9425 ad493afc 2019-08-03 stsp
9426 ad493afc 2019-08-03 stsp #ifndef PROFILE
9427 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9428 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
9429 ad493afc 2019-08-03 stsp err(1, "pledge");
9430 ad493afc 2019-08-03 stsp #endif
9431 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
9432 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
9433 2e1f37b0 2019-08-08 stsp
9434 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
9435 ad493afc 2019-08-03 stsp if (cwd == NULL) {
9436 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
9437 ad493afc 2019-08-03 stsp goto done;
9438 ad493afc 2019-08-03 stsp }
9439 ad493afc 2019-08-03 stsp
9440 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9441 fa51e947 2020-03-27 stsp if (error) {
9442 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9443 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
9444 ad493afc 2019-08-03 stsp goto done;
9445 fa51e947 2020-03-27 stsp }
9446 ad493afc 2019-08-03 stsp
9447 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9448 c9956ddf 2019-09-08 stsp NULL);
9449 ad493afc 2019-08-03 stsp if (error != NULL)
9450 ad493afc 2019-08-03 stsp goto done;
9451 ad493afc 2019-08-03 stsp
9452 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
9453 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
9454 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
9455 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
9456 2e1f37b0 2019-08-08 stsp patch_script_path);
9457 2e1f37b0 2019-08-08 stsp goto done;
9458 2e1f37b0 2019-08-08 stsp }
9459 2e1f37b0 2019-08-08 stsp }
9460 2e1f37b0 2019-08-08 stsp
9461 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9462 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
9463 ad493afc 2019-08-03 stsp if (error)
9464 ad493afc 2019-08-03 stsp goto done;
9465 ad493afc 2019-08-03 stsp
9466 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9467 ad493afc 2019-08-03 stsp if (error)
9468 ad493afc 2019-08-03 stsp goto done;
9469 ad493afc 2019-08-03 stsp
9470 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9471 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
9472 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9473 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
9474 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
9475 9627c110 2020-04-18 stsp if (!error)
9476 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9477 715dc77e 2019-08-03 stsp done:
9478 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9479 2e1f37b0 2019-08-08 stsp error == NULL)
9480 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9481 0ebf8283 2019-07-24 stsp if (repo)
9482 0ebf8283 2019-07-24 stsp got_repo_close(repo);
9483 715dc77e 2019-08-03 stsp if (worktree)
9484 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
9485 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9486 715dc77e 2019-08-03 stsp free((char *)pe->path);
9487 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
9488 715dc77e 2019-08-03 stsp free(cwd);
9489 01073a5d 2019-08-22 stsp return error;
9490 01073a5d 2019-08-22 stsp }
9491 01073a5d 2019-08-22 stsp
9492 01073a5d 2019-08-22 stsp __dead static void
9493 01073a5d 2019-08-22 stsp usage_cat(void)
9494 01073a5d 2019-08-22 stsp {
9495 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9496 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
9497 01073a5d 2019-08-22 stsp exit(1);
9498 01073a5d 2019-08-22 stsp }
9499 01073a5d 2019-08-22 stsp
9500 01073a5d 2019-08-22 stsp static const struct got_error *
9501 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9502 01073a5d 2019-08-22 stsp {
9503 01073a5d 2019-08-22 stsp const struct got_error *err;
9504 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
9505 01073a5d 2019-08-22 stsp
9506 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
9507 01073a5d 2019-08-22 stsp if (err)
9508 01073a5d 2019-08-22 stsp return err;
9509 01073a5d 2019-08-22 stsp
9510 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9511 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
9512 01073a5d 2019-08-22 stsp return err;
9513 01073a5d 2019-08-22 stsp }
9514 01073a5d 2019-08-22 stsp
9515 01073a5d 2019-08-22 stsp static const struct got_error *
9516 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9517 01073a5d 2019-08-22 stsp {
9518 01073a5d 2019-08-22 stsp const struct got_error *err;
9519 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
9520 56e0773d 2019-11-28 stsp int nentries, i;
9521 01073a5d 2019-08-22 stsp
9522 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
9523 01073a5d 2019-08-22 stsp if (err)
9524 01073a5d 2019-08-22 stsp return err;
9525 01073a5d 2019-08-22 stsp
9526 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
9527 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
9528 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
9529 01073a5d 2019-08-22 stsp char *id_str;
9530 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
9531 01073a5d 2019-08-22 stsp break;
9532 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
9533 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9534 01073a5d 2019-08-22 stsp if (err)
9535 01073a5d 2019-08-22 stsp break;
9536 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
9537 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
9538 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
9539 01073a5d 2019-08-22 stsp free(id_str);
9540 01073a5d 2019-08-22 stsp }
9541 01073a5d 2019-08-22 stsp
9542 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
9543 01073a5d 2019-08-22 stsp return err;
9544 01073a5d 2019-08-22 stsp }
9545 01073a5d 2019-08-22 stsp
9546 01073a5d 2019-08-22 stsp static const struct got_error *
9547 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9548 01073a5d 2019-08-22 stsp {
9549 01073a5d 2019-08-22 stsp const struct got_error *err;
9550 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
9551 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
9552 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
9553 01073a5d 2019-08-22 stsp char *id_str = NULL;
9554 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
9555 01073a5d 2019-08-22 stsp
9556 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
9557 01073a5d 2019-08-22 stsp if (err)
9558 01073a5d 2019-08-22 stsp return err;
9559 01073a5d 2019-08-22 stsp
9560 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9561 01073a5d 2019-08-22 stsp if (err)
9562 01073a5d 2019-08-22 stsp goto done;
9563 01073a5d 2019-08-22 stsp
9564 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9565 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9566 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
9567 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
9568 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9569 01073a5d 2019-08-22 stsp char *pid_str;
9570 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
9571 01073a5d 2019-08-22 stsp if (err)
9572 01073a5d 2019-08-22 stsp goto done;
9573 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9574 01073a5d 2019-08-22 stsp free(pid_str);
9575 01073a5d 2019-08-22 stsp }
9576 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9577 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9578 1367695b 2020-09-26 naddy (long long)got_object_commit_get_author_time(commit));
9579 01073a5d 2019-08-22 stsp
9580 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9581 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9582 1367695b 2020-09-26 naddy (long long)got_object_commit_get_committer_time(commit));
9583 01073a5d 2019-08-22 stsp
9584 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
9585 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9586 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
9587 01073a5d 2019-08-22 stsp done:
9588 01073a5d 2019-08-22 stsp free(id_str);
9589 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
9590 01073a5d 2019-08-22 stsp return err;
9591 01073a5d 2019-08-22 stsp }
9592 01073a5d 2019-08-22 stsp
9593 01073a5d 2019-08-22 stsp static const struct got_error *
9594 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9595 01073a5d 2019-08-22 stsp {
9596 01073a5d 2019-08-22 stsp const struct got_error *err;
9597 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
9598 01073a5d 2019-08-22 stsp char *id_str = NULL;
9599 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
9600 01073a5d 2019-08-22 stsp
9601 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
9602 01073a5d 2019-08-22 stsp if (err)
9603 01073a5d 2019-08-22 stsp return err;
9604 01073a5d 2019-08-22 stsp
9605 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9606 01073a5d 2019-08-22 stsp if (err)
9607 01073a5d 2019-08-22 stsp goto done;
9608 01073a5d 2019-08-22 stsp
9609 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9610 e15d5241 2019-08-22 stsp
9611 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
9612 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9613 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9614 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
9615 01073a5d 2019-08-22 stsp break;
9616 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9617 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9618 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
9619 01073a5d 2019-08-22 stsp break;
9620 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9621 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9622 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
9623 01073a5d 2019-08-22 stsp break;
9624 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9625 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9626 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
9627 01073a5d 2019-08-22 stsp break;
9628 01073a5d 2019-08-22 stsp default:
9629 01073a5d 2019-08-22 stsp break;
9630 01073a5d 2019-08-22 stsp }
9631 01073a5d 2019-08-22 stsp
9632 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9633 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
9634 e15d5241 2019-08-22 stsp
9635 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9636 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
9637 1367695b 2020-09-26 naddy (long long)got_object_tag_get_tagger_time(tag));
9638 01073a5d 2019-08-22 stsp
9639 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
9640 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9641 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
9642 01073a5d 2019-08-22 stsp done:
9643 01073a5d 2019-08-22 stsp free(id_str);
9644 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
9645 01073a5d 2019-08-22 stsp return err;
9646 01073a5d 2019-08-22 stsp }
9647 01073a5d 2019-08-22 stsp
9648 01073a5d 2019-08-22 stsp static const struct got_error *
9649 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
9650 01073a5d 2019-08-22 stsp {
9651 01073a5d 2019-08-22 stsp const struct got_error *error;
9652 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
9653 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
9654 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
9655 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
9656 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
9657 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
9658 84de9106 2020-12-26 stsp struct got_reflist_head refs;
9659 84de9106 2020-12-26 stsp
9660 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
9661 01073a5d 2019-08-22 stsp
9662 01073a5d 2019-08-22 stsp #ifndef PROFILE
9663 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9664 01073a5d 2019-08-22 stsp NULL) == -1)
9665 01073a5d 2019-08-22 stsp err(1, "pledge");
9666 01073a5d 2019-08-22 stsp #endif
9667 01073a5d 2019-08-22 stsp
9668 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9669 01073a5d 2019-08-22 stsp switch (ch) {
9670 896e9b6f 2019-08-26 stsp case 'c':
9671 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
9672 896e9b6f 2019-08-26 stsp break;
9673 01073a5d 2019-08-22 stsp case 'r':
9674 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
9675 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9676 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
9677 9ba1d308 2019-10-21 stsp optarg);
9678 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
9679 01073a5d 2019-08-22 stsp break;
9680 896e9b6f 2019-08-26 stsp case 'P':
9681 896e9b6f 2019-08-26 stsp force_path = 1;
9682 896e9b6f 2019-08-26 stsp break;
9683 01073a5d 2019-08-22 stsp default:
9684 01073a5d 2019-08-22 stsp usage_cat();
9685 01073a5d 2019-08-22 stsp /* NOTREACHED */
9686 01073a5d 2019-08-22 stsp }
9687 01073a5d 2019-08-22 stsp }
9688 01073a5d 2019-08-22 stsp
9689 01073a5d 2019-08-22 stsp argc -= optind;
9690 01073a5d 2019-08-22 stsp argv += optind;
9691 01073a5d 2019-08-22 stsp
9692 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
9693 01073a5d 2019-08-22 stsp if (cwd == NULL) {
9694 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
9695 01073a5d 2019-08-22 stsp goto done;
9696 01073a5d 2019-08-22 stsp }
9697 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
9698 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9699 01073a5d 2019-08-22 stsp goto done;
9700 01073a5d 2019-08-22 stsp if (worktree) {
9701 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9702 01073a5d 2019-08-22 stsp repo_path = strdup(
9703 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
9704 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9705 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
9706 01073a5d 2019-08-22 stsp goto done;
9707 01073a5d 2019-08-22 stsp }
9708 01073a5d 2019-08-22 stsp }
9709 01073a5d 2019-08-22 stsp }
9710 01073a5d 2019-08-22 stsp
9711 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9712 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
9713 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9714 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
9715 01073a5d 2019-08-22 stsp }
9716 01073a5d 2019-08-22 stsp
9717 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
9718 01073a5d 2019-08-22 stsp free(repo_path);
9719 01073a5d 2019-08-22 stsp if (error != NULL)
9720 01073a5d 2019-08-22 stsp goto done;
9721 01073a5d 2019-08-22 stsp
9722 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9723 896e9b6f 2019-08-26 stsp if (error)
9724 896e9b6f 2019-08-26 stsp goto done;
9725 896e9b6f 2019-08-26 stsp
9726 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9727 84de9106 2020-12-26 stsp if (error)
9728 84de9106 2020-12-26 stsp goto done;
9729 84de9106 2020-12-26 stsp
9730 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
9731 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
9732 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
9733 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
9734 01073a5d 2019-08-22 stsp if (error)
9735 01073a5d 2019-08-22 stsp goto done;
9736 01073a5d 2019-08-22 stsp
9737 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
9738 896e9b6f 2019-08-26 stsp if (force_path) {
9739 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
9740 896e9b6f 2019-08-26 stsp argv[i]);
9741 896e9b6f 2019-08-26 stsp if (error)
9742 896e9b6f 2019-08-26 stsp break;
9743 896e9b6f 2019-08-26 stsp } else {
9744 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
9745 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
9746 84de9106 2020-12-26 stsp repo);
9747 896e9b6f 2019-08-26 stsp if (error) {
9748 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9749 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
9750 896e9b6f 2019-08-26 stsp break;
9751 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
9752 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
9753 896e9b6f 2019-08-26 stsp if (error)
9754 896e9b6f 2019-08-26 stsp break;
9755 896e9b6f 2019-08-26 stsp }
9756 896e9b6f 2019-08-26 stsp }
9757 01073a5d 2019-08-22 stsp
9758 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
9759 01073a5d 2019-08-22 stsp if (error)
9760 01073a5d 2019-08-22 stsp break;
9761 01073a5d 2019-08-22 stsp
9762 01073a5d 2019-08-22 stsp switch (obj_type) {
9763 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9764 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
9765 01073a5d 2019-08-22 stsp break;
9766 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9767 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
9768 01073a5d 2019-08-22 stsp break;
9769 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9770 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
9771 01073a5d 2019-08-22 stsp break;
9772 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9773 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
9774 01073a5d 2019-08-22 stsp break;
9775 01073a5d 2019-08-22 stsp default:
9776 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
9777 01073a5d 2019-08-22 stsp break;
9778 01073a5d 2019-08-22 stsp }
9779 01073a5d 2019-08-22 stsp if (error)
9780 01073a5d 2019-08-22 stsp break;
9781 01073a5d 2019-08-22 stsp free(label);
9782 01073a5d 2019-08-22 stsp label = NULL;
9783 01073a5d 2019-08-22 stsp free(id);
9784 01073a5d 2019-08-22 stsp id = NULL;
9785 01073a5d 2019-08-22 stsp }
9786 01073a5d 2019-08-22 stsp done:
9787 01073a5d 2019-08-22 stsp free(label);
9788 01073a5d 2019-08-22 stsp free(id);
9789 896e9b6f 2019-08-26 stsp free(commit_id);
9790 01073a5d 2019-08-22 stsp if (worktree)
9791 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
9792 01073a5d 2019-08-22 stsp if (repo) {
9793 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
9794 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
9795 01073a5d 2019-08-22 stsp if (error == NULL)
9796 01073a5d 2019-08-22 stsp error = repo_error;
9797 b2118c49 2020-07-28 stsp }
9798 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
9799 b2118c49 2020-07-28 stsp return error;
9800 b2118c49 2020-07-28 stsp }
9801 b2118c49 2020-07-28 stsp
9802 b2118c49 2020-07-28 stsp __dead static void
9803 b2118c49 2020-07-28 stsp usage_info(void)
9804 b2118c49 2020-07-28 stsp {
9805 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
9806 b2118c49 2020-07-28 stsp getprogname());
9807 b2118c49 2020-07-28 stsp exit(1);
9808 b2118c49 2020-07-28 stsp }
9809 b2118c49 2020-07-28 stsp
9810 b2118c49 2020-07-28 stsp static const struct got_error *
9811 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9812 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9813 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
9814 b2118c49 2020-07-28 stsp {
9815 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
9816 b2118c49 2020-07-28 stsp char *id_str = NULL;
9817 b2118c49 2020-07-28 stsp char datebuf[128];
9818 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
9819 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
9820 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9821 b2118c49 2020-07-28 stsp
9822 b2118c49 2020-07-28 stsp /*
9823 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
9824 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
9825 b2118c49 2020-07-28 stsp */
9826 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
9827 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
9828 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
9829 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
9830 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
9831 b2118c49 2020-07-28 stsp }
9832 b2118c49 2020-07-28 stsp
9833 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
9834 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
9835 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
9836 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
9837 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
9838 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9839 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
9840 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
9841 b2118c49 2020-07-28 stsp else
9842 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
9843 b2118c49 2020-07-28 stsp
9844 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
9845 b2118c49 2020-07-28 stsp if (tm == NULL)
9846 b2118c49 2020-07-28 stsp return NULL;
9847 b2118c49 2020-07-28 stsp if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9848 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
9849 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
9850 b2118c49 2020-07-28 stsp
9851 b2118c49 2020-07-28 stsp if (blob_id) {
9852 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
9853 b2118c49 2020-07-28 stsp if (err)
9854 b2118c49 2020-07-28 stsp return err;
9855 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
9856 b2118c49 2020-07-28 stsp free(id_str);
9857 b2118c49 2020-07-28 stsp }
9858 b2118c49 2020-07-28 stsp
9859 b2118c49 2020-07-28 stsp if (staged_blob_id) {
9860 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
9861 b2118c49 2020-07-28 stsp if (err)
9862 b2118c49 2020-07-28 stsp return err;
9863 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
9864 b2118c49 2020-07-28 stsp free(id_str);
9865 b2118c49 2020-07-28 stsp }
9866 b2118c49 2020-07-28 stsp
9867 b2118c49 2020-07-28 stsp if (commit_id) {
9868 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
9869 b2118c49 2020-07-28 stsp if (err)
9870 b2118c49 2020-07-28 stsp return err;
9871 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
9872 b2118c49 2020-07-28 stsp free(id_str);
9873 b2118c49 2020-07-28 stsp }
9874 b2118c49 2020-07-28 stsp
9875 b2118c49 2020-07-28 stsp return NULL;
9876 b2118c49 2020-07-28 stsp }
9877 b2118c49 2020-07-28 stsp
9878 b2118c49 2020-07-28 stsp static const struct got_error *
9879 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
9880 b2118c49 2020-07-28 stsp {
9881 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
9882 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
9883 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
9884 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
9885 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9886 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
9887 b2118c49 2020-07-28 stsp int ch, show_files = 0;
9888 b2118c49 2020-07-28 stsp
9889 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
9890 b2118c49 2020-07-28 stsp
9891 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9892 b2118c49 2020-07-28 stsp switch (ch) {
9893 b2118c49 2020-07-28 stsp default:
9894 b2118c49 2020-07-28 stsp usage_info();
9895 b2118c49 2020-07-28 stsp /* NOTREACHED */
9896 b2118c49 2020-07-28 stsp }
9897 01073a5d 2019-08-22 stsp }
9898 b2118c49 2020-07-28 stsp
9899 b2118c49 2020-07-28 stsp argc -= optind;
9900 b2118c49 2020-07-28 stsp argv += optind;
9901 b2118c49 2020-07-28 stsp
9902 b2118c49 2020-07-28 stsp #ifndef PROFILE
9903 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9904 b2118c49 2020-07-28 stsp NULL) == -1)
9905 b2118c49 2020-07-28 stsp err(1, "pledge");
9906 b2118c49 2020-07-28 stsp #endif
9907 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
9908 b2118c49 2020-07-28 stsp if (cwd == NULL) {
9909 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
9910 b2118c49 2020-07-28 stsp goto done;
9911 b2118c49 2020-07-28 stsp }
9912 b2118c49 2020-07-28 stsp
9913 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
9914 b2118c49 2020-07-28 stsp if (error) {
9915 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9916 b2118c49 2020-07-28 stsp error = wrap_not_worktree_error(error, "status", cwd);
9917 b2118c49 2020-07-28 stsp goto done;
9918 b2118c49 2020-07-28 stsp }
9919 b2118c49 2020-07-28 stsp
9920 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9921 b2118c49 2020-07-28 stsp if (error)
9922 b2118c49 2020-07-28 stsp goto done;
9923 b2118c49 2020-07-28 stsp
9924 b2118c49 2020-07-28 stsp if (argc >= 1) {
9925 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
9926 b2118c49 2020-07-28 stsp worktree);
9927 b2118c49 2020-07-28 stsp if (error)
9928 b2118c49 2020-07-28 stsp goto done;
9929 b2118c49 2020-07-28 stsp show_files = 1;
9930 b2118c49 2020-07-28 stsp }
9931 b2118c49 2020-07-28 stsp
9932 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
9933 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
9934 b2118c49 2020-07-28 stsp if (error)
9935 b2118c49 2020-07-28 stsp goto done;
9936 b2118c49 2020-07-28 stsp
9937 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
9938 b2118c49 2020-07-28 stsp if (error)
9939 b2118c49 2020-07-28 stsp goto done;
9940 b2118c49 2020-07-28 stsp
9941 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9942 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
9943 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
9944 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
9945 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
9946 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
9947 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
9948 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9949 b2118c49 2020-07-28 stsp
9950 b2118c49 2020-07-28 stsp if (show_files) {
9951 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9952 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9953 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
9954 b2118c49 2020-07-28 stsp continue;
9955 b2118c49 2020-07-28 stsp /*
9956 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
9957 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
9958 b2118c49 2020-07-28 stsp */
9959 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
9960 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
9961 b2118c49 2020-07-28 stsp }
9962 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
9963 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
9964 b2118c49 2020-07-28 stsp if (error)
9965 b2118c49 2020-07-28 stsp goto done;
9966 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9967 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
9968 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
9969 b2118c49 2020-07-28 stsp break;
9970 b2118c49 2020-07-28 stsp }
9971 b2118c49 2020-07-28 stsp }
9972 b2118c49 2020-07-28 stsp }
9973 b2118c49 2020-07-28 stsp done:
9974 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
9975 b2118c49 2020-07-28 stsp free((char *)pe->path);
9976 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
9977 b2118c49 2020-07-28 stsp free(cwd);
9978 b2118c49 2020-07-28 stsp free(id_str);
9979 b2118c49 2020-07-28 stsp free(uuidstr);
9980 0ebf8283 2019-07-24 stsp return error;
9981 0ebf8283 2019-07-24 stsp }