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 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs;
931 04d9a9ec 2020-09-24 stsp const char *proto;
932 04d9a9ec 2020-09-24 stsp const char *host;
933 04d9a9ec 2020-09-24 stsp const char *port;
934 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
935 04d9a9ec 2020-09-24 stsp const char *git_url;
936 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
937 04d9a9ec 2020-09-24 stsp int mirror_references;
938 04d9a9ec 2020-09-24 stsp } config_info;
939 892ac3b6 2020-03-18 stsp };
940 93658fb9 2020-03-18 stsp
941 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
942 93658fb9 2020-03-18 stsp static const struct got_error *
943 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
944 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
945 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
946 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
947 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo);
948 04d9a9ec 2020-09-24 stsp
949 04d9a9ec 2020-09-24 stsp static const struct got_error *
950 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
951 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
952 531c3985 2020-03-18 stsp {
953 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
954 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
955 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
956 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
957 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
958 04d9a9ec 2020-09-24 stsp
959 04d9a9ec 2020-09-24 stsp /*
960 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
961 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
962 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
963 04d9a9ec 2020-09-24 stsp * we have all required information.
964 04d9a9ec 2020-09-24 stsp */
965 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
966 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
967 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
968 62d463ca 2020-10-20 naddy a->config_info.host, a->config_info.port,
969 62d463ca 2020-10-20 naddy a->config_info.remote_repo_path,
970 62d463ca 2020-10-20 naddy a->config_info.git_url,
971 62d463ca 2020-10-20 naddy a->config_info.fetch_all_branches,
972 62d463ca 2020-10-20 naddy a->config_info.mirror_references,
973 62d463ca 2020-10-20 naddy a->config_info.symrefs,
974 99495ddb 2021-01-10 stsp a->config_info.wanted_branches,
975 99495ddb 2021-01-10 stsp a->config_info.wanted_refs, a->repo);
976 04d9a9ec 2020-09-24 stsp if (err)
977 04d9a9ec 2020-09-24 stsp return err;
978 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
979 04d9a9ec 2020-09-24 stsp }
980 b2409d58 2020-03-18 stsp
981 68999b92 2020-03-18 stsp if (a->verbosity < 0)
982 68999b92 2020-03-18 stsp return NULL;
983 68999b92 2020-03-18 stsp
984 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
985 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
986 892ac3b6 2020-03-18 stsp fflush(stdout);
987 12d1281e 2020-03-19 stsp return NULL;
988 b2409d58 2020-03-18 stsp }
989 b2409d58 2020-03-18 stsp
990 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
991 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
992 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
993 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
994 892ac3b6 2020-03-18 stsp print_size = 1;
995 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
996 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
997 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
998 892ac3b6 2020-03-18 stsp }
999 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
1000 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
1001 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
1002 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
1003 892ac3b6 2020-03-18 stsp print_indexed = 1;
1004 892ac3b6 2020-03-18 stsp print_size = 1;
1005 892ac3b6 2020-03-18 stsp }
1006 61cc1a7a 2020-03-18 stsp }
1007 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
1008 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
1009 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
1010 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
1011 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
1012 892ac3b6 2020-03-18 stsp print_resolved = 1;
1013 892ac3b6 2020-03-18 stsp print_indexed = 1;
1014 892ac3b6 2020-03-18 stsp print_size = 1;
1015 892ac3b6 2020-03-18 stsp }
1016 61cc1a7a 2020-03-18 stsp }
1017 3168e5da 2020-09-10 stsp
1018 d2cdc636 2020-03-18 stsp }
1019 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1020 892ac3b6 2020-03-18 stsp printf("\r");
1021 892ac3b6 2020-03-18 stsp if (print_size)
1022 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1023 d715f13e 2020-03-19 stsp if (print_indexed)
1024 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
1025 d715f13e 2020-03-19 stsp if (print_resolved)
1026 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
1027 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1028 892ac3b6 2020-03-18 stsp fflush(stdout);
1029 892ac3b6 2020-03-18 stsp
1030 531c3985 2020-03-18 stsp return NULL;
1031 531c3985 2020-03-18 stsp }
1032 531c3985 2020-03-18 stsp
1033 531c3985 2020-03-18 stsp static const struct got_error *
1034 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
1035 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
1036 4ba14133 2020-03-20 stsp {
1037 4ba14133 2020-03-20 stsp const struct got_error *err;
1038 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
1039 4ba14133 2020-03-20 stsp
1040 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1041 4ba14133 2020-03-20 stsp if (err)
1042 4ba14133 2020-03-20 stsp return err;
1043 4ba14133 2020-03-20 stsp
1044 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
1045 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
1046 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
1047 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
1048 6338a6a1 2020-03-21 stsp }
1049 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
1050 4ba14133 2020-03-20 stsp return err;
1051 4ba14133 2020-03-20 stsp }
1052 4ba14133 2020-03-20 stsp
1053 4ba14133 2020-03-20 stsp static const struct got_error *
1054 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
1055 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
1056 41b0de12 2020-03-21 stsp {
1057 41b0de12 2020-03-21 stsp const struct got_error *err;
1058 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
1059 41b0de12 2020-03-21 stsp
1060 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1061 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1062 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1063 41b0de12 2020-03-21 stsp
1064 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1065 41b0de12 2020-03-21 stsp }
1066 41b0de12 2020-03-21 stsp
1067 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1068 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1069 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1070 41b0de12 2020-03-21 stsp char *id_str;
1071 41b0de12 2020-03-21 stsp
1072 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1073 41b0de12 2020-03-21 stsp if (err)
1074 41b0de12 2020-03-21 stsp return err;
1075 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1076 41b0de12 2020-03-21 stsp free(id_str);
1077 41b0de12 2020-03-21 stsp }
1078 41b0de12 2020-03-21 stsp
1079 41b0de12 2020-03-21 stsp return NULL;
1080 6338a6a1 2020-03-21 stsp }
1081 6338a6a1 2020-03-21 stsp
1082 6338a6a1 2020-03-21 stsp static const struct got_error *
1083 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1084 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1085 6338a6a1 2020-03-21 stsp {
1086 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1087 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1088 6338a6a1 2020-03-21 stsp char *id_str;
1089 6338a6a1 2020-03-21 stsp
1090 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1091 6338a6a1 2020-03-21 stsp if (err)
1092 6338a6a1 2020-03-21 stsp return err;
1093 6338a6a1 2020-03-21 stsp
1094 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1095 6338a6a1 2020-03-21 stsp if (err)
1096 6338a6a1 2020-03-21 stsp goto done;
1097 6338a6a1 2020-03-21 stsp
1098 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1099 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1100 6338a6a1 2020-03-21 stsp
1101 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1102 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1103 6338a6a1 2020-03-21 stsp done:
1104 6338a6a1 2020-03-21 stsp free(id_str);
1105 6338a6a1 2020-03-21 stsp return err;
1106 0e4002ca 2020-03-21 stsp }
1107 0e4002ca 2020-03-21 stsp
1108 0e4002ca 2020-03-21 stsp static int
1109 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1110 0e4002ca 2020-03-21 stsp {
1111 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1112 0e4002ca 2020-03-21 stsp return 0;
1113 0e4002ca 2020-03-21 stsp refname += 5;
1114 0e4002ca 2020-03-21 stsp
1115 0e4002ca 2020-03-21 stsp /*
1116 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1117 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1118 0e4002ca 2020-03-21 stsp */
1119 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1120 0e4002ca 2020-03-21 stsp return 0;
1121 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1122 0e4002ca 2020-03-21 stsp return 0;
1123 0e4002ca 2020-03-21 stsp
1124 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1125 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1126 0e4002ca 2020-03-21 stsp
1127 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1128 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1129 0e4002ca 2020-03-21 stsp return 1;
1130 0e4002ca 2020-03-21 stsp
1131 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1132 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1133 41b0de12 2020-03-21 stsp }
1134 41b0de12 2020-03-21 stsp
1135 0e4002ca 2020-03-21 stsp static int
1136 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1137 0e4002ca 2020-03-21 stsp {
1138 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1139 0e4002ca 2020-03-21 stsp
1140 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1141 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1142 0e4002ca 2020-03-21 stsp return 1;
1143 0e4002ca 2020-03-21 stsp }
1144 0e4002ca 2020-03-21 stsp
1145 0e4002ca 2020-03-21 stsp return 0;
1146 0e4002ca 2020-03-21 stsp }
1147 0e4002ca 2020-03-21 stsp
1148 41b0de12 2020-03-21 stsp static const struct got_error *
1149 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1150 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1151 0e4002ca 2020-03-21 stsp {
1152 0e4002ca 2020-03-21 stsp const struct got_error *err;
1153 0e4002ca 2020-03-21 stsp char *remote_refname;
1154 0e4002ca 2020-03-21 stsp
1155 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1156 0e4002ca 2020-03-21 stsp refname += 5;
1157 0e4002ca 2020-03-21 stsp
1158 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1159 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1160 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1161 0e4002ca 2020-03-21 stsp
1162 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1163 0e4002ca 2020-03-21 stsp free(remote_refname);
1164 7c0b7f42 2020-09-24 stsp return err;
1165 7c0b7f42 2020-09-24 stsp }
1166 7c0b7f42 2020-09-24 stsp
1167 7c0b7f42 2020-09-24 stsp static const struct got_error *
1168 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1169 15d3c221 2021-01-05 stsp const char *remote_repo_path, const char *default_branch,
1170 0c8b29c5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1171 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1172 99495ddb 2021-01-10 stsp struct got_repository *repo)
1173 7c0b7f42 2020-09-24 stsp {
1174 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1175 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1176 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1177 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1178 15d3c221 2021-01-05 stsp const char *branchname = NULL;
1179 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1180 7c0b7f42 2020-09-24 stsp ssize_t n;
1181 7c0b7f42 2020-09-24 stsp
1182 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1183 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1184 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1185 132af4a5 2021-01-05 stsp char *s;
1186 132af4a5 2021-01-05 stsp branchname = pe->path;
1187 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1188 132af4a5 2021-01-05 stsp branchname += 11;
1189 132af4a5 2021-01-05 stsp if (asprintf(&s, "%s\"%s\" ",
1190 132af4a5 2021-01-05 stsp branches ? branches : "", branchname) == -1) {
1191 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1192 132af4a5 2021-01-05 stsp goto done;
1193 132af4a5 2021-01-05 stsp }
1194 132af4a5 2021-01-05 stsp free(branches);
1195 132af4a5 2021-01-05 stsp branches = s;
1196 132af4a5 2021-01-05 stsp }
1197 0c8b29c5 2021-01-05 stsp } else if (!fetch_all_branches && default_branch) {
1198 15d3c221 2021-01-05 stsp branchname = default_branch;
1199 15d3c221 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1200 15d3c221 2021-01-05 stsp branchname += 11;
1201 132af4a5 2021-01-05 stsp if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1202 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1203 132af4a5 2021-01-05 stsp goto done;
1204 99495ddb 2021-01-10 stsp }
1205 99495ddb 2021-01-10 stsp }
1206 99495ddb 2021-01-10 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1207 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1208 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1209 99495ddb 2021-01-10 stsp char *s;
1210 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1211 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1212 99495ddb 2021-01-10 stsp branchname += 5;
1213 99495ddb 2021-01-10 stsp if (asprintf(&s, "%s\"%s\" ",
1214 99495ddb 2021-01-10 stsp refs ? refs : "", refname) == -1) {
1215 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1216 99495ddb 2021-01-10 stsp goto done;
1217 99495ddb 2021-01-10 stsp }
1218 99495ddb 2021-01-10 stsp free(refs);
1219 99495ddb 2021-01-10 stsp refs = s;
1220 132af4a5 2021-01-05 stsp }
1221 15d3c221 2021-01-05 stsp }
1222 15d3c221 2021-01-05 stsp
1223 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1224 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1225 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1226 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1227 7c0b7f42 2020-09-24 stsp goto done;
1228 7c0b7f42 2020-09-24 stsp }
1229 7c0b7f42 2020-09-24 stsp gotconfig_file = fopen(gotconfig_path, "a");
1230 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1231 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1232 7c0b7f42 2020-09-24 stsp goto done;
1233 7c0b7f42 2020-09-24 stsp }
1234 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1235 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1236 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1237 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1238 7c0b7f42 2020-09-24 stsp "%s%s%s"
1239 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1240 15d3c221 2021-01-05 stsp "%s%s%s"
1241 99495ddb 2021-01-10 stsp "%s%s%s"
1242 7c0b7f42 2020-09-24 stsp "%s"
1243 0c8b29c5 2021-01-05 stsp "%s"
1244 7c0b7f42 2020-09-24 stsp "}\n",
1245 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1246 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1247 132af4a5 2021-01-05 stsp remote_repo_path, branches ? "\tbranch { " : "",
1248 132af4a5 2021-01-05 stsp branches ? branches : "", branches ? "}\n" : "",
1249 99495ddb 2021-01-10 stsp refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1250 0c8b29c5 2021-01-05 stsp mirror_references ? "\tmirror-references yes\n" : "",
1251 0c8b29c5 2021-01-05 stsp fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -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 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1256 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1257 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1258 7c0b7f42 2020-09-24 stsp goto done;
1259 7c0b7f42 2020-09-24 stsp }
1260 7c0b7f42 2020-09-24 stsp
1261 7c0b7f42 2020-09-24 stsp done:
1262 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1263 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1264 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1265 132af4a5 2021-01-05 stsp free(branches);
1266 7c0b7f42 2020-09-24 stsp return err;
1267 7c0b7f42 2020-09-24 stsp }
1268 7c0b7f42 2020-09-24 stsp
1269 7c0b7f42 2020-09-24 stsp static const struct got_error *
1270 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1271 132af4a5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1272 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1273 99495ddb 2021-01-10 stsp struct got_repository *repo)
1274 7c0b7f42 2020-09-24 stsp {
1275 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1276 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1277 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1278 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1279 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1280 56d0a753 2021-01-20 stsp const char *branchname;
1281 7c0b7f42 2020-09-24 stsp ssize_t n;
1282 7c0b7f42 2020-09-24 stsp
1283 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1284 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1285 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1286 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1287 7c0b7f42 2020-09-24 stsp goto done;
1288 7c0b7f42 2020-09-24 stsp }
1289 7c0b7f42 2020-09-24 stsp gitconfig_file = fopen(gitconfig_path, "a");
1290 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1291 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1292 7c0b7f42 2020-09-24 stsp goto done;
1293 7c0b7f42 2020-09-24 stsp }
1294 56d0a753 2021-01-20 stsp if (fetch_all_branches) {
1295 56d0a753 2021-01-20 stsp if (mirror_references) {
1296 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1297 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1298 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1299 56d0a753 2021-01-20 stsp goto done;
1300 56d0a753 2021-01-20 stsp }
1301 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1302 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1303 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1304 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1305 7c0b7f42 2020-09-24 stsp goto done;
1306 132af4a5 2021-01-05 stsp }
1307 132af4a5 2021-01-05 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
1308 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1309 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1310 132af4a5 2021-01-05 stsp char *s;
1311 132af4a5 2021-01-05 stsp branchname = pe->path;
1312 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1313 132af4a5 2021-01-05 stsp branchname += 11;
1314 56d0a753 2021-01-20 stsp if (mirror_references) {
1315 56d0a753 2021-01-20 stsp if (asprintf(&s,
1316 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1317 56d0a753 2021-01-20 stsp branches ? branches : "",
1318 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1319 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1320 56d0a753 2021-01-20 stsp goto done;
1321 56d0a753 2021-01-20 stsp }
1322 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1323 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1324 132af4a5 2021-01-05 stsp branches ? branches : "",
1325 132af4a5 2021-01-05 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1326 132af4a5 2021-01-05 stsp branchname) == -1) {
1327 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1328 132af4a5 2021-01-05 stsp goto done;
1329 132af4a5 2021-01-05 stsp }
1330 132af4a5 2021-01-05 stsp free(branches);
1331 132af4a5 2021-01-05 stsp branches = s;
1332 7c0b7f42 2020-09-24 stsp }
1333 7c0b7f42 2020-09-24 stsp } else {
1334 7c0b7f42 2020-09-24 stsp /*
1335 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1336 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1337 7c0b7f42 2020-09-24 stsp */
1338 04d9a9ec 2020-09-24 stsp if (default_branch) {
1339 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1340 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1341 7c0b7f42 2020-09-24 stsp branchname += 11;
1342 7c0b7f42 2020-09-24 stsp } else
1343 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1344 56d0a753 2021-01-20 stsp if (mirror_references) {
1345 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1346 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/heads/%s\n",
1347 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1348 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1349 56d0a753 2021-01-20 stsp goto done;
1350 56d0a753 2021-01-20 stsp }
1351 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1352 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1353 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1354 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1355 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1356 7c0b7f42 2020-09-24 stsp goto done;
1357 99495ddb 2021-01-10 stsp }
1358 99495ddb 2021-01-10 stsp }
1359 56d0a753 2021-01-20 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1360 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1361 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1362 99495ddb 2021-01-10 stsp char *s;
1363 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1364 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1365 99495ddb 2021-01-10 stsp refname += 5;
1366 56d0a753 2021-01-20 stsp if (mirror_references) {
1367 56d0a753 2021-01-20 stsp if (asprintf(&s,
1368 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/%s\n",
1369 56d0a753 2021-01-20 stsp refs ? refs : "", refname, refname) == -1) {
1370 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1371 56d0a753 2021-01-20 stsp goto done;
1372 56d0a753 2021-01-20 stsp }
1373 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1374 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1375 99495ddb 2021-01-10 stsp refs ? refs : "",
1376 99495ddb 2021-01-10 stsp refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1377 99495ddb 2021-01-10 stsp refname) == -1) {
1378 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1379 99495ddb 2021-01-10 stsp goto done;
1380 99495ddb 2021-01-10 stsp }
1381 99495ddb 2021-01-10 stsp free(refs);
1382 99495ddb 2021-01-10 stsp refs = s;
1383 7c0b7f42 2020-09-24 stsp }
1384 132af4a5 2021-01-05 stsp }
1385 99495ddb 2021-01-10 stsp
1386 132af4a5 2021-01-05 stsp if (asprintf(&gitconfig,
1387 132af4a5 2021-01-05 stsp "[remote \"%s\"]\n"
1388 132af4a5 2021-01-05 stsp "\turl = %s\n"
1389 132af4a5 2021-01-05 stsp "%s"
1390 99495ddb 2021-01-10 stsp "%s"
1391 56d0a753 2021-01-20 stsp "\tfetch = refs/tags/*:refs/tags/*\n",
1392 132af4a5 2021-01-05 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1393 56d0a753 2021-01-20 stsp refs ? refs : "") == -1) {
1394 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1395 132af4a5 2021-01-05 stsp goto done;
1396 7c0b7f42 2020-09-24 stsp }
1397 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1398 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1399 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1400 7c0b7f42 2020-09-24 stsp goto done;
1401 7c0b7f42 2020-09-24 stsp }
1402 7c0b7f42 2020-09-24 stsp done:
1403 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1404 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1405 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1406 132af4a5 2021-01-05 stsp free(branches);
1407 0e4002ca 2020-03-21 stsp return err;
1408 0e4002ca 2020-03-21 stsp }
1409 0e4002ca 2020-03-21 stsp
1410 0e4002ca 2020-03-21 stsp static const struct got_error *
1411 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1412 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1413 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1414 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
1415 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1416 04d9a9ec 2020-09-24 stsp {
1417 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1418 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1419 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1420 04d9a9ec 2020-09-24 stsp
1421 04d9a9ec 2020-09-24 stsp /*
1422 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1423 04d9a9ec 2020-09-24 stsp * one of those.
1424 04d9a9ec 2020-09-24 stsp */
1425 62d463ca 2020-10-20 naddy if (!TAILQ_EMPTY(wanted_branches)) {
1426 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1427 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1428 04d9a9ec 2020-09-24 stsp } else {
1429 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1430 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1431 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1432 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1433 04d9a9ec 2020-09-24 stsp
1434 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1435 04d9a9ec 2020-09-24 stsp continue;
1436 04d9a9ec 2020-09-24 stsp
1437 04d9a9ec 2020-09-24 stsp default_branch = target;
1438 04d9a9ec 2020-09-24 stsp break;
1439 04d9a9ec 2020-09-24 stsp }
1440 04d9a9ec 2020-09-24 stsp }
1441 04d9a9ec 2020-09-24 stsp
1442 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1443 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1444 0c8b29c5 2021-01-05 stsp default_branch, fetch_all_branches, wanted_branches,
1445 99495ddb 2021-01-10 stsp wanted_refs, mirror_references, repo);
1446 04d9a9ec 2020-09-24 stsp if (err)
1447 04d9a9ec 2020-09-24 stsp return err;
1448 04d9a9ec 2020-09-24 stsp
1449 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1450 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1451 99495ddb 2021-01-10 stsp wanted_branches, wanted_refs, mirror_references, repo);
1452 04d9a9ec 2020-09-24 stsp }
1453 04d9a9ec 2020-09-24 stsp
1454 04d9a9ec 2020-09-24 stsp static const struct got_error *
1455 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1456 93658fb9 2020-03-18 stsp {
1457 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1458 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1459 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1460 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1461 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1462 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1463 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1464 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1465 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1466 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1467 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1468 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1469 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1470 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1471 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1472 93658fb9 2020-03-18 stsp
1473 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1474 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1475 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1476 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1477 d9b4d0c0 2020-03-18 stsp
1478 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1479 93658fb9 2020-03-18 stsp switch (ch) {
1480 659e7fbd 2020-03-20 stsp case 'a':
1481 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1482 4ba14133 2020-03-20 stsp break;
1483 4ba14133 2020-03-20 stsp case 'b':
1484 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1485 4ba14133 2020-03-20 stsp optarg, NULL);
1486 4ba14133 2020-03-20 stsp if (error)
1487 4ba14133 2020-03-20 stsp return error;
1488 659e7fbd 2020-03-20 stsp break;
1489 41b0de12 2020-03-21 stsp case 'l':
1490 41b0de12 2020-03-21 stsp list_refs_only = 1;
1491 41b0de12 2020-03-21 stsp break;
1492 469dd726 2020-03-20 stsp case 'm':
1493 469dd726 2020-03-20 stsp mirror_references = 1;
1494 469dd726 2020-03-20 stsp break;
1495 68999b92 2020-03-18 stsp case 'v':
1496 68999b92 2020-03-18 stsp if (verbosity < 0)
1497 68999b92 2020-03-18 stsp verbosity = 0;
1498 68999b92 2020-03-18 stsp else if (verbosity < 3)
1499 68999b92 2020-03-18 stsp verbosity++;
1500 68999b92 2020-03-18 stsp break;
1501 68999b92 2020-03-18 stsp case 'q':
1502 68999b92 2020-03-18 stsp verbosity = -1;
1503 68999b92 2020-03-18 stsp break;
1504 0e4002ca 2020-03-21 stsp case 'R':
1505 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1506 0e4002ca 2020-03-21 stsp optarg, NULL);
1507 0e4002ca 2020-03-21 stsp if (error)
1508 0e4002ca 2020-03-21 stsp return error;
1509 0e4002ca 2020-03-21 stsp break;
1510 93658fb9 2020-03-18 stsp default:
1511 93658fb9 2020-03-18 stsp usage_clone();
1512 93658fb9 2020-03-18 stsp break;
1513 93658fb9 2020-03-18 stsp }
1514 93658fb9 2020-03-18 stsp }
1515 93658fb9 2020-03-18 stsp argc -= optind;
1516 93658fb9 2020-03-18 stsp argv += optind;
1517 39c64a6a 2020-03-18 stsp
1518 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1519 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
1520 41b0de12 2020-03-21 stsp if (list_refs_only) {
1521 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1522 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
1523 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1524 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
1525 41b0de12 2020-03-21 stsp if (mirror_references)
1526 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
1527 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1528 ff69268e 2020-12-13 stsp option_conflict('l', 'R');
1529 41b0de12 2020-03-21 stsp }
1530 4ba14133 2020-03-20 stsp
1531 93658fb9 2020-03-18 stsp uri = argv[0];
1532 9df6f38b 2020-03-18 stsp
1533 9df6f38b 2020-03-18 stsp if (argc == 1)
1534 93658fb9 2020-03-18 stsp dirname = NULL;
1535 9df6f38b 2020-03-18 stsp else if (argc == 2)
1536 93658fb9 2020-03-18 stsp dirname = argv[1];
1537 93658fb9 2020-03-18 stsp else
1538 93658fb9 2020-03-18 stsp usage_clone();
1539 09838ffc 2020-03-18 stsp
1540 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1541 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1542 39c64a6a 2020-03-18 stsp if (error)
1543 09f63084 2020-03-20 stsp goto done;
1544 09f63084 2020-03-20 stsp
1545 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1546 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1547 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1548 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1549 09838ffc 2020-03-18 stsp goto done;
1550 09f63084 2020-03-20 stsp }
1551 09838ffc 2020-03-18 stsp
1552 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1553 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1554 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1555 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1556 39c64a6a 2020-03-18 stsp err(1, "pledge");
1557 b46f3e71 2020-03-18 stsp #endif
1558 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1559 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1560 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1561 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1562 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1563 39c64a6a 2020-03-18 stsp err(1, "pledge");
1564 b46f3e71 2020-03-18 stsp #endif
1565 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1566 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1567 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1568 39c64a6a 2020-03-18 stsp goto done;
1569 39c64a6a 2020-03-18 stsp } else {
1570 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1571 39c64a6a 2020-03-18 stsp goto done;
1572 39c64a6a 2020-03-18 stsp }
1573 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1574 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1575 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1576 bb64b798 2020-03-18 stsp goto done;
1577 bb64b798 2020-03-18 stsp }
1578 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1579 bb64b798 2020-03-18 stsp } else
1580 bb64b798 2020-03-18 stsp repo_path = dirname;
1581 bb64b798 2020-03-18 stsp
1582 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1583 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1584 2751fe64 2020-09-24 stsp if (error &&
1585 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1586 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1587 41b0de12 2020-03-21 stsp goto done;
1588 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1589 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1590 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1591 41b0de12 2020-03-21 stsp goto done;
1592 2751fe64 2020-09-24 stsp }
1593 41b0de12 2020-03-21 stsp }
1594 bb64b798 2020-03-18 stsp
1595 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1596 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1597 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1598 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1599 ee448f5f 2020-03-18 stsp goto done;
1600 ee448f5f 2020-03-18 stsp }
1601 ee448f5f 2020-03-18 stsp }
1602 f535bcd4 2020-09-30 stsp error = apply_unveil(repo_path, 0, NULL);
1603 ee448f5f 2020-03-18 stsp if (error)
1604 ee448f5f 2020-03-18 stsp goto done;
1605 f79e6490 2020-04-19 stsp
1606 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1607 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1608 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1609 ee448f5f 2020-03-18 stsp
1610 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1611 9c52365f 2020-03-21 stsp server_path, verbosity);
1612 39c64a6a 2020-03-18 stsp if (error)
1613 bb64b798 2020-03-18 stsp goto done;
1614 bb64b798 2020-03-18 stsp
1615 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1616 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1617 2751fe64 2020-09-24 stsp if (error)
1618 2751fe64 2020-09-24 stsp goto done;
1619 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1620 2751fe64 2020-09-24 stsp if (error)
1621 2751fe64 2020-09-24 stsp goto done;
1622 2751fe64 2020-09-24 stsp }
1623 2751fe64 2020-09-24 stsp
1624 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1625 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1626 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1627 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1628 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1629 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1630 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1631 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1632 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1633 99495ddb 2021-01-10 stsp fpa.config_info.wanted_refs = &wanted_refs;
1634 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1635 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1636 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1637 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1638 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1639 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1640 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1641 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1642 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1643 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1644 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1645 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1646 39c64a6a 2020-03-18 stsp if (error)
1647 d9b4d0c0 2020-03-18 stsp goto done;
1648 d9b4d0c0 2020-03-18 stsp
1649 41b0de12 2020-03-21 stsp if (list_refs_only) {
1650 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1651 41b0de12 2020-03-21 stsp goto done;
1652 41b0de12 2020-03-21 stsp }
1653 41b0de12 2020-03-21 stsp
1654 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1655 39c64a6a 2020-03-18 stsp if (error)
1656 d9b4d0c0 2020-03-18 stsp goto done;
1657 68999b92 2020-03-18 stsp if (verbosity >= 0)
1658 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1659 d9b4d0c0 2020-03-18 stsp free(id_str);
1660 d9b4d0c0 2020-03-18 stsp
1661 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1662 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1663 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1664 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1665 7ebc0570 2020-03-18 stsp char *remote_refname;
1666 0e4002ca 2020-03-21 stsp
1667 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1668 0e4002ca 2020-03-21 stsp !mirror_references) {
1669 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1670 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1671 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1672 0e4002ca 2020-03-21 stsp if (error)
1673 0e4002ca 2020-03-21 stsp goto done;
1674 0e4002ca 2020-03-21 stsp continue;
1675 0e4002ca 2020-03-21 stsp }
1676 668a20f6 2020-03-18 stsp
1677 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1678 39c64a6a 2020-03-18 stsp if (error)
1679 d9b4d0c0 2020-03-18 stsp goto done;
1680 d9b4d0c0 2020-03-18 stsp
1681 469dd726 2020-03-20 stsp if (mirror_references)
1682 469dd726 2020-03-20 stsp continue;
1683 469dd726 2020-03-20 stsp
1684 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1685 7ebc0570 2020-03-18 stsp continue;
1686 7ebc0570 2020-03-18 stsp
1687 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1688 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1689 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1690 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1691 7ebc0570 2020-03-18 stsp goto done;
1692 7ebc0570 2020-03-18 stsp }
1693 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1694 f298ae0f 2020-03-25 stsp free(remote_refname);
1695 39c64a6a 2020-03-18 stsp if (error)
1696 d9b4d0c0 2020-03-18 stsp goto done;
1697 d9b4d0c0 2020-03-18 stsp }
1698 d9b4d0c0 2020-03-18 stsp
1699 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1700 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1701 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1702 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1703 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1704 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1705 d9b4d0c0 2020-03-18 stsp
1706 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1707 d9b4d0c0 2020-03-18 stsp continue;
1708 d9b4d0c0 2020-03-18 stsp
1709 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1710 39c64a6a 2020-03-18 stsp if (error) {
1711 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1712 55330abe 2020-03-20 stsp error = NULL;
1713 d9b4d0c0 2020-03-18 stsp continue;
1714 55330abe 2020-03-20 stsp }
1715 d9b4d0c0 2020-03-18 stsp goto done;
1716 d9b4d0c0 2020-03-18 stsp }
1717 d9b4d0c0 2020-03-18 stsp
1718 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1719 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1720 f298ae0f 2020-03-25 stsp if (error)
1721 f298ae0f 2020-03-25 stsp goto done;
1722 f298ae0f 2020-03-25 stsp
1723 f298ae0f 2020-03-25 stsp if (mirror_references)
1724 f298ae0f 2020-03-25 stsp continue;
1725 f298ae0f 2020-03-25 stsp
1726 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1727 f298ae0f 2020-03-25 stsp continue;
1728 f298ae0f 2020-03-25 stsp
1729 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1730 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1731 f298ae0f 2020-03-25 stsp refname) == -1) {
1732 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1733 f298ae0f 2020-03-25 stsp goto done;
1734 f298ae0f 2020-03-25 stsp }
1735 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1736 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1737 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1738 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1739 f298ae0f 2020-03-25 stsp free(remote_refname);
1740 f298ae0f 2020-03-25 stsp goto done;
1741 f298ae0f 2020-03-25 stsp }
1742 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1743 f298ae0f 2020-03-25 stsp if (error) {
1744 f298ae0f 2020-03-25 stsp free(remote_refname);
1745 f298ae0f 2020-03-25 stsp free(remote_target);
1746 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1747 f298ae0f 2020-03-25 stsp error = NULL;
1748 f298ae0f 2020-03-25 stsp continue;
1749 f298ae0f 2020-03-25 stsp }
1750 f298ae0f 2020-03-25 stsp goto done;
1751 f298ae0f 2020-03-25 stsp }
1752 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1753 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1754 f298ae0f 2020-03-25 stsp free(remote_refname);
1755 f298ae0f 2020-03-25 stsp free(remote_target);
1756 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1757 39c64a6a 2020-03-18 stsp if (error)
1758 d9b4d0c0 2020-03-18 stsp goto done;
1759 4ba14133 2020-03-20 stsp }
1760 4ba14133 2020-03-20 stsp if (pe == NULL) {
1761 4ba14133 2020-03-20 stsp /*
1762 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1763 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1764 4ba14133 2020-03-20 stsp * which could be fetched instead.
1765 4ba14133 2020-03-20 stsp */
1766 62d463ca 2020-10-20 naddy TAILQ_FOREACH(pe, &wanted_branches, entry) {
1767 4ba14133 2020-03-20 stsp const char *target = pe->path;
1768 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1769 d9b4d0c0 2020-03-18 stsp
1770 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1771 4ba14133 2020-03-20 stsp if (error) {
1772 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1773 4ba14133 2020-03-20 stsp error = NULL;
1774 4ba14133 2020-03-20 stsp continue;
1775 4ba14133 2020-03-20 stsp }
1776 4ba14133 2020-03-20 stsp goto done;
1777 4ba14133 2020-03-20 stsp }
1778 4ba14133 2020-03-20 stsp
1779 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1780 04d9a9ec 2020-09-24 stsp verbosity, repo);
1781 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1782 4ba14133 2020-03-20 stsp if (error)
1783 4ba14133 2020-03-20 stsp goto done;
1784 4ba14133 2020-03-20 stsp break;
1785 4ba14133 2020-03-20 stsp }
1786 659e7fbd 2020-03-20 stsp }
1787 659e7fbd 2020-03-20 stsp
1788 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1789 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1790 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1791 09838ffc 2020-03-18 stsp done:
1792 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1793 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1794 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1795 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1796 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1797 9c52365f 2020-03-21 stsp }
1798 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1799 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1800 bb64b798 2020-03-18 stsp if (repo)
1801 bb64b798 2020-03-18 stsp got_repo_close(repo);
1802 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1803 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1804 d9b4d0c0 2020-03-18 stsp free(pe->data);
1805 d9b4d0c0 2020-03-18 stsp }
1806 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1807 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1808 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1809 d9b4d0c0 2020-03-18 stsp free(pe->data);
1810 d9b4d0c0 2020-03-18 stsp }
1811 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1812 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1813 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1814 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1815 09838ffc 2020-03-18 stsp free(proto);
1816 09838ffc 2020-03-18 stsp free(host);
1817 09838ffc 2020-03-18 stsp free(port);
1818 09838ffc 2020-03-18 stsp free(server_path);
1819 09838ffc 2020-03-18 stsp free(repo_name);
1820 bb64b798 2020-03-18 stsp free(default_destdir);
1821 b46f3e71 2020-03-18 stsp free(git_url);
1822 39c64a6a 2020-03-18 stsp return error;
1823 7848a0e1 2020-03-19 stsp }
1824 7848a0e1 2020-03-19 stsp
1825 7848a0e1 2020-03-19 stsp static const struct got_error *
1826 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1827 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1828 7848a0e1 2020-03-19 stsp {
1829 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1830 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1831 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1832 7848a0e1 2020-03-19 stsp
1833 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1834 7848a0e1 2020-03-19 stsp if (err)
1835 7848a0e1 2020-03-19 stsp goto done;
1836 7848a0e1 2020-03-19 stsp
1837 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1838 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1839 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1840 88609724 2020-03-21 stsp if (err)
1841 88609724 2020-03-21 stsp goto done;
1842 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1843 88609724 2020-03-21 stsp goto done;
1844 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1845 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1846 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1847 db6d8ad8 2020-03-21 stsp }
1848 db6d8ad8 2020-03-21 stsp goto done;
1849 db6d8ad8 2020-03-21 stsp }
1850 db6d8ad8 2020-03-21 stsp
1851 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1852 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1853 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1854 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1855 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1856 688f11b3 2020-03-21 stsp }
1857 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1858 7848a0e1 2020-03-19 stsp if (err)
1859 7848a0e1 2020-03-19 stsp goto done;
1860 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1861 e8a967e0 2020-03-21 stsp if (err)
1862 e8a967e0 2020-03-21 stsp goto done;
1863 7848a0e1 2020-03-19 stsp } else {
1864 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1865 7848a0e1 2020-03-19 stsp if (err)
1866 7848a0e1 2020-03-19 stsp goto done;
1867 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1868 6338a6a1 2020-03-21 stsp goto done;
1869 6338a6a1 2020-03-21 stsp
1870 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1871 6338a6a1 2020-03-21 stsp if (err)
1872 6338a6a1 2020-03-21 stsp goto done;
1873 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1874 6338a6a1 2020-03-21 stsp if (err)
1875 6338a6a1 2020-03-21 stsp goto done;
1876 7848a0e1 2020-03-19 stsp }
1877 6338a6a1 2020-03-21 stsp
1878 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1879 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1880 6338a6a1 2020-03-21 stsp new_id_str);
1881 7848a0e1 2020-03-19 stsp done:
1882 7848a0e1 2020-03-19 stsp free(old_id);
1883 7848a0e1 2020-03-19 stsp free(new_id_str);
1884 7848a0e1 2020-03-19 stsp return err;
1885 2ab43947 2020-03-18 stsp }
1886 f1bcca34 2020-03-25 stsp
1887 f1bcca34 2020-03-25 stsp static const struct got_error *
1888 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1889 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1890 f1bcca34 2020-03-25 stsp {
1891 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1892 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1893 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1894 f1bcca34 2020-03-25 stsp
1895 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1896 bcf34b0e 2020-03-26 stsp if (err) {
1897 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1898 bcf34b0e 2020-03-26 stsp return err;
1899 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1900 bcf34b0e 2020-03-26 stsp if (err)
1901 bcf34b0e 2020-03-26 stsp goto done;
1902 2ab43947 2020-03-18 stsp
1903 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1904 bcf34b0e 2020-03-26 stsp if (err)
1905 bcf34b0e 2020-03-26 stsp goto done;
1906 f1bcca34 2020-03-25 stsp
1907 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1908 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1909 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1910 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1911 bcf34b0e 2020-03-26 stsp } else {
1912 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1913 f1bcca34 2020-03-25 stsp
1914 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1915 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1916 bcf34b0e 2020-03-26 stsp goto done;
1917 bcf34b0e 2020-03-26 stsp
1918 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1919 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1920 bcf34b0e 2020-03-26 stsp if (err)
1921 bcf34b0e 2020-03-26 stsp goto done;
1922 bcf34b0e 2020-03-26 stsp
1923 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1924 bcf34b0e 2020-03-26 stsp if (err)
1925 bcf34b0e 2020-03-26 stsp goto done;
1926 bcf34b0e 2020-03-26 stsp
1927 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1928 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1929 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1930 bcf34b0e 2020-03-26 stsp
1931 bcf34b0e 2020-03-26 stsp }
1932 f1bcca34 2020-03-25 stsp done:
1933 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1934 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1935 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1936 bcf34b0e 2020-03-26 stsp err = unlock_err;
1937 bcf34b0e 2020-03-26 stsp }
1938 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1939 f1bcca34 2020-03-25 stsp return err;
1940 f1bcca34 2020-03-25 stsp }
1941 f1bcca34 2020-03-25 stsp
1942 2ab43947 2020-03-18 stsp __dead static void
1943 7848a0e1 2020-03-19 stsp usage_fetch(void)
1944 7848a0e1 2020-03-19 stsp {
1945 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1946 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1947 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1948 13f12b09 2020-03-21 stsp getprogname());
1949 7848a0e1 2020-03-19 stsp exit(1);
1950 7848a0e1 2020-03-19 stsp }
1951 7848a0e1 2020-03-19 stsp
1952 7848a0e1 2020-03-19 stsp static const struct got_error *
1953 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1954 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1955 f21ec2f0 2020-03-21 stsp {
1956 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1957 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1958 3789fd73 2020-03-26 stsp char *id_str = NULL;
1959 3789fd73 2020-03-26 stsp
1960 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1961 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1962 3789fd73 2020-03-26 stsp if (err)
1963 3789fd73 2020-03-26 stsp return err;
1964 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1965 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1966 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1967 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1968 3789fd73 2020-03-26 stsp }
1969 3789fd73 2020-03-26 stsp } else {
1970 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1971 3789fd73 2020-03-26 stsp if (err)
1972 3789fd73 2020-03-26 stsp return err;
1973 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1974 3789fd73 2020-03-26 stsp if (err)
1975 3789fd73 2020-03-26 stsp goto done;
1976 3168e5da 2020-09-10 stsp
1977 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1978 3789fd73 2020-03-26 stsp if (err)
1979 3789fd73 2020-03-26 stsp goto done;
1980 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1981 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1982 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1983 3789fd73 2020-03-26 stsp }
1984 3789fd73 2020-03-26 stsp }
1985 3789fd73 2020-03-26 stsp done:
1986 3789fd73 2020-03-26 stsp free(id);
1987 3789fd73 2020-03-26 stsp free(id_str);
1988 3789fd73 2020-03-26 stsp return NULL;
1989 3789fd73 2020-03-26 stsp }
1990 3789fd73 2020-03-26 stsp
1991 3789fd73 2020-03-26 stsp static const struct got_error *
1992 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1993 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
1994 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
1995 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
1996 3789fd73 2020-03-26 stsp {
1997 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
1998 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1999 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
2000 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
2001 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
2002 3789fd73 2020-03-26 stsp char *local_refname = NULL;
2003 f21ec2f0 2020-03-21 stsp
2004 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&my_refs);
2005 f21ec2f0 2020-03-21 stsp
2006 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2007 3789fd73 2020-03-26 stsp == -1)
2008 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
2009 3789fd73 2020-03-26 stsp
2010 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2011 f21ec2f0 2020-03-21 stsp if (err)
2012 3789fd73 2020-03-26 stsp goto done;
2013 f21ec2f0 2020-03-21 stsp
2014 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &my_refs, entry) {
2015 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
2016 f21ec2f0 2020-03-21 stsp
2017 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
2018 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
2019 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
2020 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
2021 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
2022 3789fd73 2020-03-26 stsp continue;
2023 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
2024 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
2025 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
2026 3789fd73 2020-03-26 stsp goto done;
2027 3789fd73 2020-03-26 stsp }
2028 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
2029 3789fd73 2020-03-26 stsp continue;
2030 3789fd73 2020-03-26 stsp }
2031 f21ec2f0 2020-03-21 stsp
2032 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
2033 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
2034 f21ec2f0 2020-03-21 stsp break;
2035 f21ec2f0 2020-03-21 stsp }
2036 f21ec2f0 2020-03-21 stsp if (pe != NULL)
2037 f21ec2f0 2020-03-21 stsp continue;
2038 f21ec2f0 2020-03-21 stsp
2039 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
2040 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
2041 3789fd73 2020-03-26 stsp break;
2042 3789fd73 2020-03-26 stsp }
2043 3789fd73 2020-03-26 stsp if (pe != NULL)
2044 3789fd73 2020-03-26 stsp continue;
2045 f21ec2f0 2020-03-21 stsp
2046 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
2047 f21ec2f0 2020-03-21 stsp if (err)
2048 f21ec2f0 2020-03-21 stsp break;
2049 3789fd73 2020-03-26 stsp
2050 3789fd73 2020-03-26 stsp if (local_refname) {
2051 3789fd73 2020-03-26 stsp struct got_reference *ref;
2052 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
2053 3789fd73 2020-03-26 stsp if (err) {
2054 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
2055 3789fd73 2020-03-26 stsp break;
2056 3789fd73 2020-03-26 stsp free(local_refname);
2057 3789fd73 2020-03-26 stsp local_refname = NULL;
2058 3789fd73 2020-03-26 stsp continue;
2059 3789fd73 2020-03-26 stsp }
2060 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
2061 3789fd73 2020-03-26 stsp if (err)
2062 3789fd73 2020-03-26 stsp break;
2063 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
2064 3789fd73 2020-03-26 stsp got_ref_close(ref);
2065 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
2066 3789fd73 2020-03-26 stsp err = unlock_err;
2067 3789fd73 2020-03-26 stsp break;
2068 3789fd73 2020-03-26 stsp }
2069 3789fd73 2020-03-26 stsp
2070 3789fd73 2020-03-26 stsp free(local_refname);
2071 3789fd73 2020-03-26 stsp local_refname = NULL;
2072 6338a6a1 2020-03-21 stsp }
2073 f21ec2f0 2020-03-21 stsp }
2074 3789fd73 2020-03-26 stsp done:
2075 3789fd73 2020-03-26 stsp free(remote_namespace);
2076 3789fd73 2020-03-26 stsp free(local_refname);
2077 0e4002ca 2020-03-21 stsp return err;
2078 0e4002ca 2020-03-21 stsp }
2079 0e4002ca 2020-03-21 stsp
2080 0e4002ca 2020-03-21 stsp static const struct got_error *
2081 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
2082 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
2083 0e4002ca 2020-03-21 stsp {
2084 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
2085 0e4002ca 2020-03-21 stsp char *remote_refname;
2086 0e4002ca 2020-03-21 stsp struct got_reference *ref;
2087 0e4002ca 2020-03-21 stsp
2088 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
2089 0e4002ca 2020-03-21 stsp refname += 5;
2090 0e4002ca 2020-03-21 stsp
2091 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2092 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
2093 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
2094 f21ec2f0 2020-03-21 stsp
2095 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
2096 0e4002ca 2020-03-21 stsp if (err) {
2097 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
2098 0e4002ca 2020-03-21 stsp goto done;
2099 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
2100 0e4002ca 2020-03-21 stsp } else {
2101 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
2102 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2103 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
2104 9f142382 2020-03-21 stsp err = unlock_err;
2105 0e4002ca 2020-03-21 stsp got_ref_close(ref);
2106 0e4002ca 2020-03-21 stsp }
2107 0e4002ca 2020-03-21 stsp done:
2108 0e4002ca 2020-03-21 stsp free(remote_refname);
2109 f21ec2f0 2020-03-21 stsp return err;
2110 f21ec2f0 2020-03-21 stsp }
2111 f21ec2f0 2020-03-21 stsp
2112 f21ec2f0 2020-03-21 stsp static const struct got_error *
2113 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
2114 7848a0e1 2020-03-19 stsp {
2115 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
2116 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
2117 7848a0e1 2020-03-19 stsp const char *remote_name;
2118 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
2119 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
2120 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
2121 7848a0e1 2020-03-19 stsp int nremotes;
2122 7848a0e1 2020-03-19 stsp char *id_str = NULL;
2123 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
2124 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
2125 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2126 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2127 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
2128 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
2129 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
2130 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
2131 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
2132 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2133 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
2134 7848a0e1 2020-03-19 stsp
2135 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
2136 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
2137 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
2138 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
2139 7848a0e1 2020-03-19 stsp
2140 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
2141 7848a0e1 2020-03-19 stsp switch (ch) {
2142 659e7fbd 2020-03-20 stsp case 'a':
2143 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
2144 4ba14133 2020-03-20 stsp break;
2145 4ba14133 2020-03-20 stsp case 'b':
2146 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
2147 4ba14133 2020-03-20 stsp optarg, NULL);
2148 4ba14133 2020-03-20 stsp if (error)
2149 4ba14133 2020-03-20 stsp return error;
2150 41b0de12 2020-03-21 stsp break;
2151 f21ec2f0 2020-03-21 stsp case 'd':
2152 f21ec2f0 2020-03-21 stsp delete_refs = 1;
2153 f21ec2f0 2020-03-21 stsp break;
2154 41b0de12 2020-03-21 stsp case 'l':
2155 41b0de12 2020-03-21 stsp list_refs_only = 1;
2156 659e7fbd 2020-03-20 stsp break;
2157 7848a0e1 2020-03-19 stsp case 'r':
2158 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
2159 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2160 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
2161 7848a0e1 2020-03-19 stsp optarg);
2162 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
2163 7848a0e1 2020-03-19 stsp break;
2164 db6d8ad8 2020-03-21 stsp case 't':
2165 db6d8ad8 2020-03-21 stsp replace_tags = 1;
2166 db6d8ad8 2020-03-21 stsp break;
2167 7848a0e1 2020-03-19 stsp case 'v':
2168 7848a0e1 2020-03-19 stsp if (verbosity < 0)
2169 7848a0e1 2020-03-19 stsp verbosity = 0;
2170 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
2171 7848a0e1 2020-03-19 stsp verbosity++;
2172 7848a0e1 2020-03-19 stsp break;
2173 7848a0e1 2020-03-19 stsp case 'q':
2174 7848a0e1 2020-03-19 stsp verbosity = -1;
2175 7848a0e1 2020-03-19 stsp break;
2176 0e4002ca 2020-03-21 stsp case 'R':
2177 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
2178 0e4002ca 2020-03-21 stsp optarg, NULL);
2179 0e4002ca 2020-03-21 stsp if (error)
2180 0e4002ca 2020-03-21 stsp return error;
2181 0e4002ca 2020-03-21 stsp break;
2182 7848a0e1 2020-03-19 stsp default:
2183 7848a0e1 2020-03-19 stsp usage_fetch();
2184 7848a0e1 2020-03-19 stsp break;
2185 7848a0e1 2020-03-19 stsp }
2186 7848a0e1 2020-03-19 stsp }
2187 7848a0e1 2020-03-19 stsp argc -= optind;
2188 7848a0e1 2020-03-19 stsp argv += optind;
2189 7848a0e1 2020-03-19 stsp
2190 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2191 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
2192 41b0de12 2020-03-21 stsp if (list_refs_only) {
2193 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2194 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
2195 ff69268e 2020-12-13 stsp if (fetch_all_branches)
2196 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
2197 f21ec2f0 2020-03-21 stsp if (delete_refs)
2198 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
2199 41b0de12 2020-03-21 stsp }
2200 4ba14133 2020-03-20 stsp
2201 7848a0e1 2020-03-19 stsp if (argc == 0)
2202 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2203 7848a0e1 2020-03-19 stsp else if (argc == 1)
2204 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2205 7848a0e1 2020-03-19 stsp else
2206 7848a0e1 2020-03-19 stsp usage_fetch();
2207 7848a0e1 2020-03-19 stsp
2208 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2209 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2210 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2211 7848a0e1 2020-03-19 stsp goto done;
2212 7848a0e1 2020-03-19 stsp }
2213 7848a0e1 2020-03-19 stsp
2214 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2215 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2216 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2217 7848a0e1 2020-03-19 stsp goto done;
2218 7848a0e1 2020-03-19 stsp else
2219 7848a0e1 2020-03-19 stsp error = NULL;
2220 7848a0e1 2020-03-19 stsp if (worktree) {
2221 7848a0e1 2020-03-19 stsp repo_path =
2222 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2223 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2224 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2225 7848a0e1 2020-03-19 stsp if (error)
2226 7848a0e1 2020-03-19 stsp goto done;
2227 7848a0e1 2020-03-19 stsp } else {
2228 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2229 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2230 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2231 7848a0e1 2020-03-19 stsp goto done;
2232 7848a0e1 2020-03-19 stsp }
2233 7848a0e1 2020-03-19 stsp }
2234 7848a0e1 2020-03-19 stsp }
2235 7848a0e1 2020-03-19 stsp
2236 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2237 7848a0e1 2020-03-19 stsp if (error)
2238 7848a0e1 2020-03-19 stsp goto done;
2239 7848a0e1 2020-03-19 stsp
2240 50b0790e 2020-09-11 stsp if (worktree) {
2241 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2242 50b0790e 2020-09-11 stsp if (worktree_conf) {
2243 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2244 50b0790e 2020-09-11 stsp worktree_conf);
2245 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2246 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2247 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2248 50b0790e 2020-09-11 stsp break;
2249 54eb00d5 2020-10-20 stsp }
2250 50b0790e 2020-09-11 stsp }
2251 50b0790e 2020-09-11 stsp }
2252 7848a0e1 2020-03-19 stsp }
2253 50b0790e 2020-09-11 stsp if (remote == NULL) {
2254 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2255 50b0790e 2020-09-11 stsp if (repo_conf) {
2256 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2257 50b0790e 2020-09-11 stsp repo_conf);
2258 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2259 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2260 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2261 50b0790e 2020-09-11 stsp break;
2262 54eb00d5 2020-10-20 stsp }
2263 50b0790e 2020-09-11 stsp }
2264 50b0790e 2020-09-11 stsp }
2265 50b0790e 2020-09-11 stsp }
2266 50b0790e 2020-09-11 stsp if (remote == NULL) {
2267 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2268 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2269 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2270 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2271 257add31 2020-09-09 stsp break;
2272 54eb00d5 2020-10-20 stsp }
2273 257add31 2020-09-09 stsp }
2274 7848a0e1 2020-03-19 stsp }
2275 50b0790e 2020-09-11 stsp if (remote == NULL) {
2276 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2277 50b0790e 2020-09-11 stsp goto done;
2278 b8adfa55 2020-09-25 stsp }
2279 b8adfa55 2020-09-25 stsp
2280 0c8b29c5 2021-01-05 stsp if (TAILQ_EMPTY(&wanted_branches)) {
2281 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches)
2282 0c8b29c5 2021-01-05 stsp fetch_all_branches = remote->fetch_all_branches;
2283 b8adfa55 2020-09-25 stsp for (i = 0; i < remote->nbranches; i++) {
2284 b8adfa55 2020-09-25 stsp got_pathlist_append(&wanted_branches,
2285 b8adfa55 2020-09-25 stsp remote->branches[i], NULL);
2286 99495ddb 2021-01-10 stsp }
2287 99495ddb 2021-01-10 stsp }
2288 99495ddb 2021-01-10 stsp if (TAILQ_EMPTY(&wanted_refs)) {
2289 99495ddb 2021-01-10 stsp for (i = 0; i < remote->nrefs; i++) {
2290 99495ddb 2021-01-10 stsp got_pathlist_append(&wanted_refs,
2291 99495ddb 2021-01-10 stsp remote->refs[i], NULL);
2292 b8adfa55 2020-09-25 stsp }
2293 50b0790e 2020-09-11 stsp }
2294 7848a0e1 2020-03-19 stsp
2295 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2296 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
2297 7848a0e1 2020-03-19 stsp if (error)
2298 7848a0e1 2020-03-19 stsp goto done;
2299 7848a0e1 2020-03-19 stsp
2300 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2301 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2302 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2303 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2304 7848a0e1 2020-03-19 stsp err(1, "pledge");
2305 7848a0e1 2020-03-19 stsp #endif
2306 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2307 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2308 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2309 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2310 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2311 7848a0e1 2020-03-19 stsp err(1, "pledge");
2312 7848a0e1 2020-03-19 stsp #endif
2313 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2314 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2315 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2316 7848a0e1 2020-03-19 stsp goto done;
2317 7848a0e1 2020-03-19 stsp } else {
2318 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2319 7848a0e1 2020-03-19 stsp goto done;
2320 7848a0e1 2020-03-19 stsp }
2321 7848a0e1 2020-03-19 stsp
2322 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2323 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2324 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
2325 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
2326 7848a0e1 2020-03-19 stsp goto done;
2327 7848a0e1 2020-03-19 stsp }
2328 7848a0e1 2020-03-19 stsp }
2329 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2330 7848a0e1 2020-03-19 stsp if (error)
2331 7848a0e1 2020-03-19 stsp goto done;
2332 f79e6490 2020-04-19 stsp
2333 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2334 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2335 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2336 7848a0e1 2020-03-19 stsp
2337 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2338 9c52365f 2020-03-21 stsp server_path, verbosity);
2339 7848a0e1 2020-03-19 stsp if (error)
2340 7848a0e1 2020-03-19 stsp goto done;
2341 7848a0e1 2020-03-19 stsp
2342 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2343 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2344 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2345 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2346 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2347 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2348 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2349 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2350 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2351 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2352 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2353 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2354 7848a0e1 2020-03-19 stsp if (error)
2355 7848a0e1 2020-03-19 stsp goto done;
2356 7848a0e1 2020-03-19 stsp
2357 41b0de12 2020-03-21 stsp if (list_refs_only) {
2358 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2359 41b0de12 2020-03-21 stsp goto done;
2360 41b0de12 2020-03-21 stsp }
2361 41b0de12 2020-03-21 stsp
2362 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2363 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2364 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2365 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2366 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2367 984065c8 2020-03-19 stsp if (error)
2368 984065c8 2020-03-19 stsp goto done;
2369 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2370 984065c8 2020-03-19 stsp free(id_str);
2371 984065c8 2020-03-19 stsp id_str = NULL;
2372 984065c8 2020-03-19 stsp }
2373 7848a0e1 2020-03-19 stsp
2374 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2375 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2376 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2377 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2378 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2379 7848a0e1 2020-03-19 stsp char *remote_refname;
2380 7848a0e1 2020-03-19 stsp
2381 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2382 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2383 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2384 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2385 0e4002ca 2020-03-21 stsp if (error)
2386 0e4002ca 2020-03-21 stsp goto done;
2387 0e4002ca 2020-03-21 stsp continue;
2388 0e4002ca 2020-03-21 stsp }
2389 0e4002ca 2020-03-21 stsp
2390 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2391 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2392 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2393 7848a0e1 2020-03-19 stsp if (error) {
2394 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2395 7848a0e1 2020-03-19 stsp goto done;
2396 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2397 6338a6a1 2020-03-21 stsp repo);
2398 7848a0e1 2020-03-19 stsp if (error)
2399 7848a0e1 2020-03-19 stsp goto done;
2400 7848a0e1 2020-03-19 stsp } else {
2401 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2402 db6d8ad8 2020-03-21 stsp verbosity, repo);
2403 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2404 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2405 9f142382 2020-03-21 stsp error = unlock_err;
2406 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2407 7848a0e1 2020-03-19 stsp if (error)
2408 7848a0e1 2020-03-19 stsp goto done;
2409 7848a0e1 2020-03-19 stsp }
2410 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2411 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2412 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2413 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2414 7848a0e1 2020-03-19 stsp goto done;
2415 7848a0e1 2020-03-19 stsp }
2416 7848a0e1 2020-03-19 stsp
2417 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2418 7848a0e1 2020-03-19 stsp if (error) {
2419 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2420 7848a0e1 2020-03-19 stsp goto done;
2421 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2422 688f11b3 2020-03-21 stsp verbosity, repo);
2423 7848a0e1 2020-03-19 stsp if (error)
2424 7848a0e1 2020-03-19 stsp goto done;
2425 7848a0e1 2020-03-19 stsp } else {
2426 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2427 db6d8ad8 2020-03-21 stsp verbosity, repo);
2428 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2429 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2430 9f142382 2020-03-21 stsp error = unlock_err;
2431 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2432 7848a0e1 2020-03-19 stsp if (error)
2433 7848a0e1 2020-03-19 stsp goto done;
2434 7848a0e1 2020-03-19 stsp }
2435 2ec30c80 2020-03-20 stsp
2436 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2437 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2438 2ec30c80 2020-03-20 stsp if (error) {
2439 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2440 2ec30c80 2020-03-20 stsp goto done;
2441 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2442 6338a6a1 2020-03-21 stsp repo);
2443 2ec30c80 2020-03-20 stsp if (error)
2444 2ec30c80 2020-03-20 stsp goto done;
2445 9f142382 2020-03-21 stsp } else {
2446 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2447 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2448 9f142382 2020-03-21 stsp error = unlock_err;
2449 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2450 9f142382 2020-03-21 stsp }
2451 7848a0e1 2020-03-19 stsp }
2452 7848a0e1 2020-03-19 stsp }
2453 f1bcca34 2020-03-25 stsp if (delete_refs) {
2454 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2455 3789fd73 2020-03-26 stsp verbosity, repo);
2456 f1bcca34 2020-03-25 stsp if (error)
2457 f1bcca34 2020-03-25 stsp goto done;
2458 f1bcca34 2020-03-25 stsp }
2459 f1bcca34 2020-03-25 stsp
2460 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2461 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2462 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2463 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2464 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2465 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2466 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2467 f1bcca34 2020-03-25 stsp
2468 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2469 f1bcca34 2020-03-25 stsp continue;
2470 f1bcca34 2020-03-25 stsp
2471 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2472 f1bcca34 2020-03-25 stsp continue;
2473 f1bcca34 2020-03-25 stsp
2474 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2475 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2476 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2477 f1bcca34 2020-03-25 stsp goto done;
2478 f1bcca34 2020-03-25 stsp }
2479 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2480 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2481 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2482 f1bcca34 2020-03-25 stsp free(remote_refname);
2483 f1bcca34 2020-03-25 stsp goto done;
2484 f1bcca34 2020-03-25 stsp }
2485 f1bcca34 2020-03-25 stsp
2486 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2487 f1bcca34 2020-03-25 stsp 0);
2488 f1bcca34 2020-03-25 stsp if (error) {
2489 f1bcca34 2020-03-25 stsp free(remote_refname);
2490 f1bcca34 2020-03-25 stsp free(remote_target);
2491 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2492 f1bcca34 2020-03-25 stsp error = NULL;
2493 f1bcca34 2020-03-25 stsp continue;
2494 f1bcca34 2020-03-25 stsp }
2495 f1bcca34 2020-03-25 stsp goto done;
2496 f1bcca34 2020-03-25 stsp }
2497 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2498 f1bcca34 2020-03-25 stsp verbosity, repo);
2499 f1bcca34 2020-03-25 stsp free(remote_refname);
2500 f1bcca34 2020-03-25 stsp free(remote_target);
2501 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2502 f1bcca34 2020-03-25 stsp if (error)
2503 f1bcca34 2020-03-25 stsp goto done;
2504 f1bcca34 2020-03-25 stsp }
2505 f1bcca34 2020-03-25 stsp }
2506 7848a0e1 2020-03-19 stsp done:
2507 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2508 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2509 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2510 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2511 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2512 9c52365f 2020-03-21 stsp }
2513 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2514 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2515 7848a0e1 2020-03-19 stsp if (repo)
2516 7848a0e1 2020-03-19 stsp got_repo_close(repo);
2517 7848a0e1 2020-03-19 stsp if (worktree)
2518 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2519 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2520 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2521 7848a0e1 2020-03-19 stsp free(pe->data);
2522 7848a0e1 2020-03-19 stsp }
2523 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2524 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2525 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2526 7848a0e1 2020-03-19 stsp free(pe->data);
2527 7848a0e1 2020-03-19 stsp }
2528 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2529 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2530 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2531 7848a0e1 2020-03-19 stsp free(id_str);
2532 7848a0e1 2020-03-19 stsp free(cwd);
2533 7848a0e1 2020-03-19 stsp free(repo_path);
2534 7848a0e1 2020-03-19 stsp free(pack_hash);
2535 7848a0e1 2020-03-19 stsp free(proto);
2536 7848a0e1 2020-03-19 stsp free(host);
2537 7848a0e1 2020-03-19 stsp free(port);
2538 7848a0e1 2020-03-19 stsp free(server_path);
2539 7848a0e1 2020-03-19 stsp free(repo_name);
2540 7848a0e1 2020-03-19 stsp return error;
2541 7848a0e1 2020-03-19 stsp }
2542 7848a0e1 2020-03-19 stsp
2543 7848a0e1 2020-03-19 stsp
2544 7848a0e1 2020-03-19 stsp __dead static void
2545 2ab43947 2020-03-18 stsp usage_checkout(void)
2546 2ab43947 2020-03-18 stsp {
2547 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2548 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2549 2ab43947 2020-03-18 stsp exit(1);
2550 2ab43947 2020-03-18 stsp }
2551 2ab43947 2020-03-18 stsp
2552 2ab43947 2020-03-18 stsp static void
2553 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2554 2ab43947 2020-03-18 stsp {
2555 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2556 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2557 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2558 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2559 2ab43947 2020-03-18 stsp getprogname());
2560 2ab43947 2020-03-18 stsp }
2561 2ab43947 2020-03-18 stsp
2562 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2563 2ab43947 2020-03-18 stsp const char *worktree_path;
2564 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2565 2ab43947 2020-03-18 stsp };
2566 2ab43947 2020-03-18 stsp
2567 2ab43947 2020-03-18 stsp static const struct got_error *
2568 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2569 2ab43947 2020-03-18 stsp {
2570 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2571 2ab43947 2020-03-18 stsp
2572 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2573 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2574 2ab43947 2020-03-18 stsp return NULL;
2575 2ab43947 2020-03-18 stsp
2576 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2577 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2578 2ab43947 2020-03-18 stsp return NULL;
2579 2ab43947 2020-03-18 stsp }
2580 2ab43947 2020-03-18 stsp
2581 2ab43947 2020-03-18 stsp while (path[0] == '/')
2582 2ab43947 2020-03-18 stsp path++;
2583 2ab43947 2020-03-18 stsp
2584 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2585 2ab43947 2020-03-18 stsp return NULL;
2586 2ab43947 2020-03-18 stsp }
2587 2ab43947 2020-03-18 stsp
2588 2ab43947 2020-03-18 stsp static const struct got_error *
2589 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2590 2ab43947 2020-03-18 stsp {
2591 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2592 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2593 2ab43947 2020-03-18 stsp return NULL;
2594 2ab43947 2020-03-18 stsp }
2595 2ab43947 2020-03-18 stsp
2596 2ab43947 2020-03-18 stsp static const struct got_error *
2597 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2598 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2599 2ab43947 2020-03-18 stsp struct got_repository *repo)
2600 2ab43947 2020-03-18 stsp {
2601 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2602 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2603 2ab43947 2020-03-18 stsp
2604 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2605 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2606 2ab43947 2020-03-18 stsp if (err)
2607 2ab43947 2020-03-18 stsp return err;
2608 2ab43947 2020-03-18 stsp
2609 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2610 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2611 2ab43947 2020-03-18 stsp
2612 2ab43947 2020-03-18 stsp /*
2613 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2614 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2615 2ab43947 2020-03-18 stsp *
2616 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2617 2ab43947 2020-03-18 stsp *
2618 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2619 2ab43947 2020-03-18 stsp * \ /
2620 2ab43947 2020-03-18 stsp * C E
2621 2ab43947 2020-03-18 stsp * \ /
2622 2ab43947 2020-03-18 stsp * B (yca)
2623 2ab43947 2020-03-18 stsp * |
2624 2ab43947 2020-03-18 stsp * A
2625 2ab43947 2020-03-18 stsp *
2626 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2627 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2628 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2629 2ab43947 2020-03-18 stsp */
2630 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2631 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2632 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2633 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2634 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2635 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2636 2ab43947 2020-03-18 stsp
2637 2ab43947 2020-03-18 stsp free(yca_id);
2638 2ab43947 2020-03-18 stsp return NULL;
2639 2ab43947 2020-03-18 stsp }
2640 2ab43947 2020-03-18 stsp
2641 2ab43947 2020-03-18 stsp static const struct got_error *
2642 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2643 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2644 2ab43947 2020-03-18 stsp struct got_repository *repo)
2645 2ab43947 2020-03-18 stsp {
2646 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2647 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2648 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2649 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2650 2ab43947 2020-03-18 stsp
2651 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2652 2ab43947 2020-03-18 stsp if (err)
2653 2ab43947 2020-03-18 stsp goto done;
2654 2ab43947 2020-03-18 stsp
2655 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2656 2ab43947 2020-03-18 stsp is_same_branch = 1;
2657 2ab43947 2020-03-18 stsp goto done;
2658 2ab43947 2020-03-18 stsp }
2659 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2660 2ab43947 2020-03-18 stsp is_same_branch = 1;
2661 2ab43947 2020-03-18 stsp goto done;
2662 2ab43947 2020-03-18 stsp }
2663 2ab43947 2020-03-18 stsp
2664 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2665 2ab43947 2020-03-18 stsp if (err)
2666 2ab43947 2020-03-18 stsp goto done;
2667 2ab43947 2020-03-18 stsp
2668 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2669 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2670 2ab43947 2020-03-18 stsp if (err)
2671 2ab43947 2020-03-18 stsp goto done;
2672 2ab43947 2020-03-18 stsp
2673 2ab43947 2020-03-18 stsp for (;;) {
2674 2ab43947 2020-03-18 stsp struct got_object_id *id;
2675 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2676 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2677 2ab43947 2020-03-18 stsp if (err) {
2678 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2679 2ab43947 2020-03-18 stsp err = NULL;
2680 2ab43947 2020-03-18 stsp break;
2681 2ab43947 2020-03-18 stsp }
2682 2ab43947 2020-03-18 stsp
2683 2ab43947 2020-03-18 stsp if (id) {
2684 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2685 2ab43947 2020-03-18 stsp break;
2686 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2687 2ab43947 2020-03-18 stsp is_same_branch = 1;
2688 2ab43947 2020-03-18 stsp break;
2689 2ab43947 2020-03-18 stsp }
2690 2ab43947 2020-03-18 stsp }
2691 2ab43947 2020-03-18 stsp }
2692 2ab43947 2020-03-18 stsp done:
2693 2ab43947 2020-03-18 stsp if (graph)
2694 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2695 2ab43947 2020-03-18 stsp free(head_commit_id);
2696 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2697 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2698 2ab43947 2020-03-18 stsp return err;
2699 a367ff0f 2019-05-14 stsp }
2700 8069f636 2019-01-12 stsp
2701 4ed7e80c 2018-05-20 stsp static const struct got_error *
2702 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2703 4b6c9460 2020-03-05 stsp {
2704 4b6c9460 2020-03-05 stsp static char msg[512];
2705 4b6c9460 2020-03-05 stsp const char *branch_name;
2706 4b6c9460 2020-03-05 stsp
2707 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2708 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2709 4b6c9460 2020-03-05 stsp else
2710 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2711 4b6c9460 2020-03-05 stsp
2712 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2713 4b6c9460 2020-03-05 stsp branch_name += 11;
2714 4b6c9460 2020-03-05 stsp
2715 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2716 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2717 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2718 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2719 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2720 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2721 4b6c9460 2020-03-05 stsp
2722 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2723 4b6c9460 2020-03-05 stsp }
2724 4b6c9460 2020-03-05 stsp
2725 4b6c9460 2020-03-05 stsp static const struct got_error *
2726 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2727 c09a553d 2018-03-12 stsp {
2728 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2729 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2730 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2731 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2732 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2733 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2734 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2735 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2736 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2737 f0207f6a 2020-10-19 stsp char *cwd = NULL;
2738 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2739 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2740 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2741 f2ea84fa 2019-07-27 stsp
2742 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2743 c09a553d 2018-03-12 stsp
2744 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2745 0bb8a95e 2018-03-12 stsp switch (ch) {
2746 08573d5b 2019-05-14 stsp case 'b':
2747 08573d5b 2019-05-14 stsp branch_name = optarg;
2748 08573d5b 2019-05-14 stsp break;
2749 8069f636 2019-01-12 stsp case 'c':
2750 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2751 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2752 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2753 bb51a5b4 2020-01-13 stsp break;
2754 bb51a5b4 2020-01-13 stsp case 'E':
2755 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2756 8069f636 2019-01-12 stsp break;
2757 0bb8a95e 2018-03-12 stsp case 'p':
2758 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2759 0bb8a95e 2018-03-12 stsp break;
2760 0bb8a95e 2018-03-12 stsp default:
2761 2deda0b9 2019-03-07 stsp usage_checkout();
2762 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2763 0bb8a95e 2018-03-12 stsp }
2764 0bb8a95e 2018-03-12 stsp }
2765 0bb8a95e 2018-03-12 stsp
2766 0bb8a95e 2018-03-12 stsp argc -= optind;
2767 0bb8a95e 2018-03-12 stsp argv += optind;
2768 0bb8a95e 2018-03-12 stsp
2769 6715a751 2018-03-16 stsp #ifndef PROFILE
2770 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2771 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2772 c09a553d 2018-03-12 stsp err(1, "pledge");
2773 6715a751 2018-03-16 stsp #endif
2774 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2775 c47340da 2020-09-20 stsp char *base, *dotgit;
2776 f0207f6a 2020-10-19 stsp const char *path;
2777 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2778 76089277 2018-04-01 stsp if (repo_path == NULL)
2779 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2780 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2781 76089277 2018-04-01 stsp if (cwd == NULL) {
2782 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2783 76089277 2018-04-01 stsp goto done;
2784 76089277 2018-04-01 stsp }
2785 c47340da 2020-09-20 stsp if (path_prefix[0])
2786 f0207f6a 2020-10-19 stsp path = path_prefix;
2787 c47340da 2020-09-20 stsp else
2788 f0207f6a 2020-10-19 stsp path = repo_path;
2789 f0207f6a 2020-10-19 stsp error = got_path_basename(&base, path);
2790 f0207f6a 2020-10-19 stsp if (error)
2791 c47340da 2020-09-20 stsp goto done;
2792 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2793 c09a553d 2018-03-12 stsp if (dotgit)
2794 c09a553d 2018-03-12 stsp *dotgit = '\0';
2795 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2796 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2797 f0207f6a 2020-10-19 stsp free(base);
2798 76089277 2018-04-01 stsp goto done;
2799 c09a553d 2018-03-12 stsp }
2800 f0207f6a 2020-10-19 stsp free(base);
2801 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2802 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2803 76089277 2018-04-01 stsp if (repo_path == NULL) {
2804 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2805 76089277 2018-04-01 stsp goto done;
2806 76089277 2018-04-01 stsp }
2807 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2808 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2809 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2810 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2811 b4b3a7dd 2019-07-22 stsp argv[1]);
2812 b4b3a7dd 2019-07-22 stsp goto done;
2813 b4b3a7dd 2019-07-22 stsp }
2814 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2815 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2816 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2817 b4b3a7dd 2019-07-22 stsp goto done;
2818 b4b3a7dd 2019-07-22 stsp }
2819 76089277 2018-04-01 stsp }
2820 c09a553d 2018-03-12 stsp } else
2821 c09a553d 2018-03-12 stsp usage_checkout();
2822 c09a553d 2018-03-12 stsp
2823 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2824 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2825 13bfb272 2019-05-10 jcs
2826 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2827 c09a553d 2018-03-12 stsp if (error != NULL)
2828 c02c541e 2019-03-29 stsp goto done;
2829 c02c541e 2019-03-29 stsp
2830 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2831 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2832 c530dc23 2019-07-23 stsp if (error) {
2833 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2834 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2835 c530dc23 2019-07-23 stsp goto done;
2836 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2837 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2838 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2839 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2840 c530dc23 2019-07-23 stsp goto done;
2841 c530dc23 2019-07-23 stsp }
2842 c530dc23 2019-07-23 stsp }
2843 c530dc23 2019-07-23 stsp
2844 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2845 c02c541e 2019-03-29 stsp if (error)
2846 c09a553d 2018-03-12 stsp goto done;
2847 8069f636 2019-01-12 stsp
2848 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2849 c09a553d 2018-03-12 stsp if (error != NULL)
2850 c09a553d 2018-03-12 stsp goto done;
2851 c09a553d 2018-03-12 stsp
2852 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2853 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2854 c09a553d 2018-03-12 stsp goto done;
2855 c09a553d 2018-03-12 stsp
2856 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2857 c09a553d 2018-03-12 stsp if (error != NULL)
2858 c09a553d 2018-03-12 stsp goto done;
2859 c09a553d 2018-03-12 stsp
2860 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2861 e5dc7198 2018-12-29 stsp path_prefix);
2862 e5dc7198 2018-12-29 stsp if (error != NULL)
2863 e5dc7198 2018-12-29 stsp goto done;
2864 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2865 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2866 49520a32 2018-12-29 stsp goto done;
2867 49520a32 2018-12-29 stsp }
2868 49520a32 2018-12-29 stsp
2869 8069f636 2019-01-12 stsp if (commit_id_str) {
2870 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2871 84de9106 2020-12-26 stsp struct got_reflist_head refs;
2872 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
2873 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2874 84de9106 2020-12-26 stsp NULL);
2875 84de9106 2020-12-26 stsp if (error)
2876 84de9106 2020-12-26 stsp goto done;
2877 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2878 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2879 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
2880 30837e32 2019-07-25 stsp if (error)
2881 8069f636 2019-01-12 stsp goto done;
2882 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2883 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2884 8069f636 2019-01-12 stsp if (error != NULL) {
2885 8069f636 2019-01-12 stsp free(commit_id);
2886 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2887 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2888 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2889 4b6c9460 2020-03-05 stsp }
2890 8069f636 2019-01-12 stsp goto done;
2891 8069f636 2019-01-12 stsp }
2892 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2893 4b6c9460 2020-03-05 stsp if (error) {
2894 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2895 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2896 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2897 4b6c9460 2020-03-05 stsp }
2898 45d344f6 2019-05-14 stsp goto done;
2899 4b6c9460 2020-03-05 stsp }
2900 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2901 8069f636 2019-01-12 stsp commit_id);
2902 8069f636 2019-01-12 stsp free(commit_id);
2903 8069f636 2019-01-12 stsp if (error)
2904 8069f636 2019-01-12 stsp goto done;
2905 8069f636 2019-01-12 stsp }
2906 8069f636 2019-01-12 stsp
2907 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2908 f2ea84fa 2019-07-27 stsp if (error)
2909 f2ea84fa 2019-07-27 stsp goto done;
2910 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2911 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2912 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2913 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2914 c09a553d 2018-03-12 stsp if (error != NULL)
2915 c09a553d 2018-03-12 stsp goto done;
2916 c09a553d 2018-03-12 stsp
2917 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2918 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2919 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2920 c09a553d 2018-03-12 stsp done:
2921 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2922 8069f636 2019-01-12 stsp free(commit_id_str);
2923 76089277 2018-04-01 stsp free(repo_path);
2924 507dc3bb 2018-12-29 stsp free(worktree_path);
2925 c47340da 2020-09-20 stsp free(cwd);
2926 507dc3bb 2018-12-29 stsp return error;
2927 9627c110 2020-04-18 stsp }
2928 9627c110 2020-04-18 stsp
2929 9627c110 2020-04-18 stsp struct got_update_progress_arg {
2930 9627c110 2020-04-18 stsp int did_something;
2931 9627c110 2020-04-18 stsp int conflicts;
2932 9627c110 2020-04-18 stsp int obstructed;
2933 9627c110 2020-04-18 stsp int not_updated;
2934 9627c110 2020-04-18 stsp };
2935 9627c110 2020-04-18 stsp
2936 9627c110 2020-04-18 stsp void
2937 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2938 9627c110 2020-04-18 stsp {
2939 9627c110 2020-04-18 stsp if (!upa->did_something)
2940 9627c110 2020-04-18 stsp return;
2941 9627c110 2020-04-18 stsp
2942 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
2943 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2944 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
2945 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
2946 9627c110 2020-04-18 stsp upa->obstructed);
2947 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
2948 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
2949 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
2950 507dc3bb 2018-12-29 stsp }
2951 507dc3bb 2018-12-29 stsp
2952 507dc3bb 2018-12-29 stsp __dead static void
2953 507dc3bb 2018-12-29 stsp usage_update(void)
2954 507dc3bb 2018-12-29 stsp {
2955 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2956 507dc3bb 2018-12-29 stsp getprogname());
2957 507dc3bb 2018-12-29 stsp exit(1);
2958 507dc3bb 2018-12-29 stsp }
2959 507dc3bb 2018-12-29 stsp
2960 1ee397ad 2019-07-12 stsp static const struct got_error *
2961 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2962 507dc3bb 2018-12-29 stsp {
2963 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
2964 784955db 2019-01-12 stsp
2965 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2966 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2967 1ee397ad 2019-07-12 stsp return NULL;
2968 507dc3bb 2018-12-29 stsp
2969 9627c110 2020-04-18 stsp upa->did_something = 1;
2970 a484d721 2019-06-10 stsp
2971 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2972 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2973 1ee397ad 2019-07-12 stsp return NULL;
2974 a484d721 2019-06-10 stsp
2975 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
2976 9627c110 2020-04-18 stsp upa->conflicts++;
2977 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
2978 9627c110 2020-04-18 stsp upa->obstructed++;
2979 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2980 9627c110 2020-04-18 stsp upa->not_updated++;
2981 9627c110 2020-04-18 stsp
2982 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2983 507dc3bb 2018-12-29 stsp path++;
2984 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2985 1ee397ad 2019-07-12 stsp return NULL;
2986 be7061eb 2018-12-30 stsp }
2987 be7061eb 2018-12-30 stsp
2988 be7061eb 2018-12-30 stsp static const struct got_error *
2989 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2990 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2991 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2992 a1fb16d8 2019-05-24 stsp {
2993 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2994 a1fb16d8 2019-05-24 stsp char *base_id_str;
2995 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2996 a1fb16d8 2019-05-24 stsp
2997 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2998 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2999 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
3000 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
3001 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
3002 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
3003 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
3004 a1fb16d8 2019-05-24 stsp }
3005 a1fb16d8 2019-05-24 stsp
3006 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
3007 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3008 a1fb16d8 2019-05-24 stsp if (err) {
3009 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
3010 a1fb16d8 2019-05-24 stsp return err;
3011 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
3012 a1fb16d8 2019-05-24 stsp }
3013 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
3014 a1fb16d8 2019-05-24 stsp return NULL;
3015 a1fb16d8 2019-05-24 stsp
3016 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
3017 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
3018 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
3019 a1fb16d8 2019-05-24 stsp if (err)
3020 a1fb16d8 2019-05-24 stsp return err;
3021 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
3022 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3023 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
3024 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3025 0ebf8283 2019-07-24 stsp return NULL;
3026 0ebf8283 2019-07-24 stsp }
3027 0ebf8283 2019-07-24 stsp
3028 0ebf8283 2019-07-24 stsp static const struct got_error *
3029 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3030 0ebf8283 2019-07-24 stsp {
3031 0ebf8283 2019-07-24 stsp const struct got_error *err;
3032 0ebf8283 2019-07-24 stsp int in_progress;
3033 0ebf8283 2019-07-24 stsp
3034 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
3035 0ebf8283 2019-07-24 stsp if (err)
3036 0ebf8283 2019-07-24 stsp return err;
3037 0ebf8283 2019-07-24 stsp if (in_progress)
3038 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
3039 0ebf8283 2019-07-24 stsp
3040 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
3041 0ebf8283 2019-07-24 stsp if (err)
3042 0ebf8283 2019-07-24 stsp return err;
3043 0ebf8283 2019-07-24 stsp if (in_progress)
3044 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
3045 0ebf8283 2019-07-24 stsp
3046 a1fb16d8 2019-05-24 stsp return NULL;
3047 a5edda0a 2019-07-27 stsp }
3048 a5edda0a 2019-07-27 stsp
3049 a5edda0a 2019-07-27 stsp static const struct got_error *
3050 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3051 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
3052 a5edda0a 2019-07-27 stsp {
3053 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
3054 a5edda0a 2019-07-27 stsp char *path;
3055 a5edda0a 2019-07-27 stsp int i;
3056 a5edda0a 2019-07-27 stsp
3057 a5edda0a 2019-07-27 stsp if (argc == 0) {
3058 a5edda0a 2019-07-27 stsp path = strdup("");
3059 a5edda0a 2019-07-27 stsp if (path == NULL)
3060 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
3061 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
3062 a5edda0a 2019-07-27 stsp }
3063 a5edda0a 2019-07-27 stsp
3064 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
3065 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
3066 a5edda0a 2019-07-27 stsp if (err)
3067 a5edda0a 2019-07-27 stsp break;
3068 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
3069 a5edda0a 2019-07-27 stsp if (err) {
3070 a5edda0a 2019-07-27 stsp free(path);
3071 a5edda0a 2019-07-27 stsp break;
3072 a5edda0a 2019-07-27 stsp }
3073 a5edda0a 2019-07-27 stsp }
3074 a5edda0a 2019-07-27 stsp
3075 a5edda0a 2019-07-27 stsp return err;
3076 a1fb16d8 2019-05-24 stsp }
3077 a1fb16d8 2019-05-24 stsp
3078 a1fb16d8 2019-05-24 stsp static const struct got_error *
3079 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
3080 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
3081 fa51e947 2020-03-27 stsp {
3082 fa51e947 2020-03-27 stsp const struct got_error *err;
3083 fa51e947 2020-03-27 stsp struct got_repository *repo;
3084 fa51e947 2020-03-27 stsp static char msg[512];
3085 fa51e947 2020-03-27 stsp
3086 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
3087 fa51e947 2020-03-27 stsp if (err)
3088 fa51e947 2020-03-27 stsp return orig_err;
3089 fa51e947 2020-03-27 stsp
3090 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
3091 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
3092 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
3093 fa51e947 2020-03-27 stsp "'got checkout'.\n"
3094 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
3095 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3096 fa51e947 2020-03-27 stsp got_repo_close(repo);
3097 fa51e947 2020-03-27 stsp return err;
3098 fa51e947 2020-03-27 stsp }
3099 fa51e947 2020-03-27 stsp
3100 fa51e947 2020-03-27 stsp static const struct got_error *
3101 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
3102 507dc3bb 2018-12-29 stsp {
3103 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
3104 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
3105 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
3106 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
3107 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
3108 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
3109 024e9686 2019-05-14 stsp const char *branch_name = NULL;
3110 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
3111 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
3112 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
3113 9627c110 2020-04-18 stsp int ch;
3114 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
3115 507dc3bb 2018-12-29 stsp
3116 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
3117 f2ea84fa 2019-07-27 stsp
3118 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
3119 507dc3bb 2018-12-29 stsp switch (ch) {
3120 024e9686 2019-05-14 stsp case 'b':
3121 024e9686 2019-05-14 stsp branch_name = optarg;
3122 024e9686 2019-05-14 stsp break;
3123 507dc3bb 2018-12-29 stsp case 'c':
3124 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
3125 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
3126 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
3127 507dc3bb 2018-12-29 stsp break;
3128 507dc3bb 2018-12-29 stsp default:
3129 2deda0b9 2019-03-07 stsp usage_update();
3130 507dc3bb 2018-12-29 stsp /* NOTREACHED */
3131 507dc3bb 2018-12-29 stsp }
3132 507dc3bb 2018-12-29 stsp }
3133 507dc3bb 2018-12-29 stsp
3134 507dc3bb 2018-12-29 stsp argc -= optind;
3135 507dc3bb 2018-12-29 stsp argv += optind;
3136 507dc3bb 2018-12-29 stsp
3137 507dc3bb 2018-12-29 stsp #ifndef PROFILE
3138 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3139 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
3140 507dc3bb 2018-12-29 stsp err(1, "pledge");
3141 507dc3bb 2018-12-29 stsp #endif
3142 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
3143 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
3144 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3145 c4cdcb68 2019-04-03 stsp goto done;
3146 c4cdcb68 2019-04-03 stsp }
3147 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
3148 fa51e947 2020-03-27 stsp if (error) {
3149 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3150 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
3151 fa51e947 2020-03-27 stsp worktree_path);
3152 7d5807f4 2019-07-11 stsp goto done;
3153 fa51e947 2020-03-27 stsp }
3154 7d5807f4 2019-07-11 stsp
3155 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
3156 f2ea84fa 2019-07-27 stsp if (error)
3157 f2ea84fa 2019-07-27 stsp goto done;
3158 507dc3bb 2018-12-29 stsp
3159 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3160 c9956ddf 2019-09-08 stsp NULL);
3161 507dc3bb 2018-12-29 stsp if (error != NULL)
3162 507dc3bb 2018-12-29 stsp goto done;
3163 507dc3bb 2018-12-29 stsp
3164 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3165 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3166 087fb88c 2019-08-04 stsp if (error)
3167 087fb88c 2019-08-04 stsp goto done;
3168 087fb88c 2019-08-04 stsp
3169 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3170 0266afb7 2019-01-04 stsp if (error)
3171 0266afb7 2019-01-04 stsp goto done;
3172 0266afb7 2019-01-04 stsp
3173 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3174 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
3175 024e9686 2019-05-14 stsp if (error != NULL)
3176 024e9686 2019-05-14 stsp goto done;
3177 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
3178 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3179 9c4b8182 2019-01-02 stsp if (error != NULL)
3180 9c4b8182 2019-01-02 stsp goto done;
3181 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3182 507dc3bb 2018-12-29 stsp if (error != NULL)
3183 507dc3bb 2018-12-29 stsp goto done;
3184 507dc3bb 2018-12-29 stsp } else {
3185 84de9106 2020-12-26 stsp struct got_reflist_head refs;
3186 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3187 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3188 84de9106 2020-12-26 stsp NULL);
3189 84de9106 2020-12-26 stsp if (error)
3190 84de9106 2020-12-26 stsp goto done;
3191 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3192 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3193 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
3194 04f57cb3 2019-07-25 stsp free(commit_id_str);
3195 bb787f09 2019-07-25 stsp commit_id_str = NULL;
3196 30837e32 2019-07-25 stsp if (error)
3197 a297e751 2019-07-11 stsp goto done;
3198 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
3199 a297e751 2019-07-11 stsp if (error)
3200 507dc3bb 2018-12-29 stsp goto done;
3201 507dc3bb 2018-12-29 stsp }
3202 35c965b2 2018-12-29 stsp
3203 a1fb16d8 2019-05-24 stsp if (branch_name) {
3204 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
3205 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3206 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3207 f2ea84fa 2019-07-27 stsp continue;
3208 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3209 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3210 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3211 024e9686 2019-05-14 stsp goto done;
3212 024e9686 2019-05-14 stsp }
3213 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3214 024e9686 2019-05-14 stsp if (error)
3215 024e9686 2019-05-14 stsp goto done;
3216 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3217 3aef623b 2019-10-15 stsp repo);
3218 a367ff0f 2019-05-14 stsp free(head_commit_id);
3219 024e9686 2019-05-14 stsp if (error != NULL)
3220 024e9686 2019-05-14 stsp goto done;
3221 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3222 a367ff0f 2019-05-14 stsp if (error)
3223 a367ff0f 2019-05-14 stsp goto done;
3224 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3225 024e9686 2019-05-14 stsp if (error)
3226 024e9686 2019-05-14 stsp goto done;
3227 024e9686 2019-05-14 stsp } else {
3228 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3229 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3230 a367ff0f 2019-05-14 stsp if (error != NULL) {
3231 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3232 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3233 024e9686 2019-05-14 stsp goto done;
3234 a367ff0f 2019-05-14 stsp }
3235 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3236 a367ff0f 2019-05-14 stsp if (error)
3237 a367ff0f 2019-05-14 stsp goto done;
3238 024e9686 2019-05-14 stsp }
3239 507dc3bb 2018-12-29 stsp
3240 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3241 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3242 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3243 507dc3bb 2018-12-29 stsp commit_id);
3244 507dc3bb 2018-12-29 stsp if (error)
3245 507dc3bb 2018-12-29 stsp goto done;
3246 507dc3bb 2018-12-29 stsp }
3247 507dc3bb 2018-12-29 stsp
3248 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3249 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3250 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3251 507dc3bb 2018-12-29 stsp if (error != NULL)
3252 507dc3bb 2018-12-29 stsp goto done;
3253 9c4b8182 2019-01-02 stsp
3254 9627c110 2020-04-18 stsp if (upa.did_something)
3255 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
3256 784955db 2019-01-12 stsp else
3257 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3258 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3259 507dc3bb 2018-12-29 stsp done:
3260 c09a553d 2018-03-12 stsp free(worktree_path);
3261 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3262 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3263 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3264 507dc3bb 2018-12-29 stsp free(commit_id);
3265 9c4b8182 2019-01-02 stsp free(commit_id_str);
3266 c09a553d 2018-03-12 stsp return error;
3267 c09a553d 2018-03-12 stsp }
3268 c09a553d 2018-03-12 stsp
3269 f42b1b34 2018-03-12 stsp static const struct got_error *
3270 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3271 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3272 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3273 5c860e29 2018-03-12 stsp {
3274 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3275 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3276 79109fed 2018-03-27 stsp
3277 44392932 2019-08-25 stsp if (blob_id1) {
3278 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3279 44392932 2019-08-25 stsp if (err)
3280 44392932 2019-08-25 stsp goto done;
3281 44392932 2019-08-25 stsp }
3282 79109fed 2018-03-27 stsp
3283 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3284 44392932 2019-08-25 stsp if (err)
3285 44392932 2019-08-25 stsp goto done;
3286 79109fed 2018-03-27 stsp
3287 44392932 2019-08-25 stsp while (path[0] == '/')
3288 44392932 2019-08-25 stsp path++;
3289 fe621944 2020-11-10 stsp err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3290 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, stdout);
3291 44392932 2019-08-25 stsp done:
3292 44392932 2019-08-25 stsp if (blob1)
3293 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3294 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3295 44392932 2019-08-25 stsp return err;
3296 44392932 2019-08-25 stsp }
3297 44392932 2019-08-25 stsp
3298 44392932 2019-08-25 stsp static const struct got_error *
3299 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3300 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3301 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3302 44392932 2019-08-25 stsp {
3303 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3304 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3305 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3306 44392932 2019-08-25 stsp
3307 44392932 2019-08-25 stsp if (tree_id1) {
3308 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3309 79109fed 2018-03-27 stsp if (err)
3310 44392932 2019-08-25 stsp goto done;
3311 79109fed 2018-03-27 stsp }
3312 79109fed 2018-03-27 stsp
3313 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3314 0f2b3dca 2018-12-22 stsp if (err)
3315 0f2b3dca 2018-12-22 stsp goto done;
3316 0f2b3dca 2018-12-22 stsp
3317 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3318 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3319 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
3320 aaa13589 2019-06-01 stsp arg.outfile = stdout;
3321 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
3322 fe621944 2020-11-10 stsp arg.nlines = 0;
3323 44392932 2019-08-25 stsp while (path[0] == '/')
3324 44392932 2019-08-25 stsp path++;
3325 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
3326 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3327 0f2b3dca 2018-12-22 stsp done:
3328 79109fed 2018-03-27 stsp if (tree1)
3329 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3330 366e0a5f 2019-10-10 stsp if (tree2)
3331 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3332 44392932 2019-08-25 stsp return err;
3333 44392932 2019-08-25 stsp }
3334 44392932 2019-08-25 stsp
3335 44392932 2019-08-25 stsp static const struct got_error *
3336 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3337 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3338 0208f208 2020-05-05 stsp {
3339 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3340 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3341 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3342 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3343 0208f208 2020-05-05 stsp
3344 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3345 0208f208 2020-05-05 stsp if (qid != NULL) {
3346 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3347 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3348 0208f208 2020-05-05 stsp qid->id);
3349 0208f208 2020-05-05 stsp if (err)
3350 0208f208 2020-05-05 stsp return err;
3351 0208f208 2020-05-05 stsp
3352 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3353 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3354 0208f208 2020-05-05 stsp
3355 0208f208 2020-05-05 stsp }
3356 0208f208 2020-05-05 stsp
3357 0208f208 2020-05-05 stsp if (tree_id1) {
3358 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3359 0208f208 2020-05-05 stsp if (err)
3360 0208f208 2020-05-05 stsp goto done;
3361 0208f208 2020-05-05 stsp }
3362 0208f208 2020-05-05 stsp
3363 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3364 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3365 0208f208 2020-05-05 stsp if (err)
3366 0208f208 2020-05-05 stsp goto done;
3367 0208f208 2020-05-05 stsp
3368 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3369 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3370 0208f208 2020-05-05 stsp done:
3371 0208f208 2020-05-05 stsp if (tree1)
3372 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3373 0208f208 2020-05-05 stsp if (tree2)
3374 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3375 0208f208 2020-05-05 stsp return err;
3376 0208f208 2020-05-05 stsp }
3377 0208f208 2020-05-05 stsp
3378 0208f208 2020-05-05 stsp static const struct got_error *
3379 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3380 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3381 44392932 2019-08-25 stsp {
3382 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3383 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3384 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3385 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3386 44392932 2019-08-25 stsp struct got_object_qid *qid;
3387 44392932 2019-08-25 stsp
3388 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3389 44392932 2019-08-25 stsp if (qid != NULL) {
3390 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3391 44392932 2019-08-25 stsp qid->id);
3392 44392932 2019-08-25 stsp if (err)
3393 44392932 2019-08-25 stsp return err;
3394 44392932 2019-08-25 stsp }
3395 44392932 2019-08-25 stsp
3396 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3397 44392932 2019-08-25 stsp int obj_type;
3398 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3399 44392932 2019-08-25 stsp if (err)
3400 44392932 2019-08-25 stsp goto done;
3401 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3402 44392932 2019-08-25 stsp if (err) {
3403 44392932 2019-08-25 stsp free(obj_id2);
3404 44392932 2019-08-25 stsp goto done;
3405 44392932 2019-08-25 stsp }
3406 44392932 2019-08-25 stsp if (pcommit) {
3407 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3408 44392932 2019-08-25 stsp qid->id, path);
3409 44392932 2019-08-25 stsp if (err) {
3410 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3411 2e8c69d1 2020-05-04 stsp free(obj_id2);
3412 2e8c69d1 2020-05-04 stsp goto done;
3413 2e8c69d1 2020-05-04 stsp }
3414 2e8c69d1 2020-05-04 stsp } else {
3415 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3416 2e8c69d1 2020-05-04 stsp if (err) {
3417 2e8c69d1 2020-05-04 stsp free(obj_id2);
3418 2e8c69d1 2020-05-04 stsp goto done;
3419 2e8c69d1 2020-05-04 stsp }
3420 44392932 2019-08-25 stsp }
3421 44392932 2019-08-25 stsp }
3422 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3423 44392932 2019-08-25 stsp if (err) {
3424 44392932 2019-08-25 stsp free(obj_id2);
3425 44392932 2019-08-25 stsp goto done;
3426 44392932 2019-08-25 stsp }
3427 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3428 44392932 2019-08-25 stsp switch (obj_type) {
3429 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3430 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3431 64453f7e 2020-11-21 stsp 0, 0, repo);
3432 44392932 2019-08-25 stsp break;
3433 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3434 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3435 64453f7e 2020-11-21 stsp 0, 0, repo);
3436 44392932 2019-08-25 stsp break;
3437 44392932 2019-08-25 stsp default:
3438 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3439 44392932 2019-08-25 stsp break;
3440 44392932 2019-08-25 stsp }
3441 44392932 2019-08-25 stsp free(obj_id1);
3442 44392932 2019-08-25 stsp free(obj_id2);
3443 44392932 2019-08-25 stsp } else {
3444 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3445 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3446 44392932 2019-08-25 stsp if (err)
3447 44392932 2019-08-25 stsp goto done;
3448 579bd556 2020-10-24 stsp if (pcommit) {
3449 579bd556 2020-10-24 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3450 579bd556 2020-10-24 stsp err = got_object_id_str(&id_str1, obj_id1);
3451 579bd556 2020-10-24 stsp if (err)
3452 579bd556 2020-10-24 stsp goto done;
3453 579bd556 2020-10-24 stsp }
3454 64453f7e 2020-11-21 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3455 64453f7e 2020-11-21 stsp id_str2);
3456 64453f7e 2020-11-21 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3457 64453f7e 2020-11-21 stsp repo);
3458 44392932 2019-08-25 stsp }
3459 44392932 2019-08-25 stsp done:
3460 0f2b3dca 2018-12-22 stsp free(id_str1);
3461 0f2b3dca 2018-12-22 stsp free(id_str2);
3462 44392932 2019-08-25 stsp if (pcommit)
3463 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3464 79109fed 2018-03-27 stsp return err;
3465 79109fed 2018-03-27 stsp }
3466 79109fed 2018-03-27 stsp
3467 4bb494d5 2018-06-16 stsp static char *
3468 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3469 6c281f94 2018-06-11 stsp {
3470 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3471 09867e48 2019-08-13 stsp char *p, *s;
3472 09867e48 2019-08-13 stsp
3473 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3474 09867e48 2019-08-13 stsp if (tm == NULL)
3475 09867e48 2019-08-13 stsp return NULL;
3476 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3477 09867e48 2019-08-13 stsp if (s == NULL)
3478 09867e48 2019-08-13 stsp return NULL;
3479 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3480 6c281f94 2018-06-11 stsp if (p)
3481 6c281f94 2018-06-11 stsp *p = '\0';
3482 6c281f94 2018-06-11 stsp return s;
3483 6c281f94 2018-06-11 stsp }
3484 dc424a06 2019-08-07 stsp
3485 6841bf13 2019-11-29 kn static const struct got_error *
3486 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3487 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3488 6841bf13 2019-11-29 kn {
3489 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3490 6841bf13 2019-11-29 kn regmatch_t regmatch;
3491 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3492 6841bf13 2019-11-29 kn
3493 6841bf13 2019-11-29 kn *have_match = 0;
3494 6841bf13 2019-11-29 kn
3495 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3496 6841bf13 2019-11-29 kn if (err)
3497 6841bf13 2019-11-29 kn return err;
3498 6841bf13 2019-11-29 kn
3499 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3500 6841bf13 2019-11-29 kn if (err)
3501 6841bf13 2019-11-29 kn goto done;
3502 6841bf13 2019-11-29 kn
3503 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3504 6841bf13 2019-11-29 kn *have_match = 1;
3505 6841bf13 2019-11-29 kn done:
3506 6841bf13 2019-11-29 kn free(id_str);
3507 6841bf13 2019-11-29 kn free(logmsg);
3508 6841bf13 2019-11-29 kn return err;
3509 6841bf13 2019-11-29 kn }
3510 6841bf13 2019-11-29 kn
3511 0208f208 2020-05-05 stsp static void
3512 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3513 0208f208 2020-05-05 stsp regex_t *regex)
3514 0208f208 2020-05-05 stsp {
3515 0208f208 2020-05-05 stsp regmatch_t regmatch;
3516 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3517 0208f208 2020-05-05 stsp
3518 0208f208 2020-05-05 stsp *have_match = 0;
3519 0208f208 2020-05-05 stsp
3520 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3521 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3522 0208f208 2020-05-05 stsp *have_match = 1;
3523 0208f208 2020-05-05 stsp break;
3524 0208f208 2020-05-05 stsp }
3525 0208f208 2020-05-05 stsp }
3526 0208f208 2020-05-05 stsp }
3527 0208f208 2020-05-05 stsp
3528 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3529 6c281f94 2018-06-11 stsp
3530 888b7d99 2020-12-26 stsp static const struct got_error*
3531 888b7d99 2020-12-26 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
3532 888b7d99 2020-12-26 stsp struct got_object_id *id, struct got_repository *repo)
3533 888b7d99 2020-12-26 stsp {
3534 888b7d99 2020-12-26 stsp static const struct got_error *err = NULL;
3535 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3536 888b7d99 2020-12-26 stsp char *s;
3537 888b7d99 2020-12-26 stsp const char *name;
3538 5c860e29 2018-03-12 stsp
3539 888b7d99 2020-12-26 stsp *refs_str = NULL;
3540 888b7d99 2020-12-26 stsp
3541 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
3542 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3543 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3544 a436ad14 2019-08-13 stsp int cmp;
3545 a436ad14 2019-08-13 stsp
3546 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3547 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3548 d9498b20 2019-02-05 stsp continue;
3549 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3550 199a4027 2019-02-02 stsp name += 5;
3551 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3552 7143d404 2019-03-12 stsp continue;
3553 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3554 e34f9ed6 2019-02-02 stsp name += 6;
3555 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3556 141c2bff 2019-02-04 stsp name += 8;
3557 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3558 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3559 4343a07f 2020-04-24 stsp continue;
3560 4343a07f 2020-04-24 stsp }
3561 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3562 48cae60d 2020-09-22 stsp if (err)
3563 888b7d99 2020-12-26 stsp break;
3564 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3565 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3566 5d844a1e 2019-08-13 stsp if (err) {
3567 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3568 48cae60d 2020-09-22 stsp free(ref_id);
3569 888b7d99 2020-12-26 stsp break;
3570 48cae60d 2020-09-22 stsp }
3571 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3572 5d844a1e 2019-08-13 stsp err = NULL;
3573 5d844a1e 2019-08-13 stsp tag = NULL;
3574 5d844a1e 2019-08-13 stsp }
3575 a436ad14 2019-08-13 stsp }
3576 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3577 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3578 48cae60d 2020-09-22 stsp free(ref_id);
3579 a436ad14 2019-08-13 stsp if (tag)
3580 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3581 a436ad14 2019-08-13 stsp if (cmp != 0)
3582 a436ad14 2019-08-13 stsp continue;
3583 888b7d99 2020-12-26 stsp s = *refs_str;
3584 888b7d99 2020-12-26 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
3585 888b7d99 2020-12-26 stsp s ? ", " : "", name) == -1) {
3586 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3587 199a4027 2019-02-02 stsp free(s);
3588 888b7d99 2020-12-26 stsp *refs_str = NULL;
3589 888b7d99 2020-12-26 stsp break;
3590 199a4027 2019-02-02 stsp }
3591 199a4027 2019-02-02 stsp free(s);
3592 199a4027 2019-02-02 stsp }
3593 888b7d99 2020-12-26 stsp
3594 888b7d99 2020-12-26 stsp return err;
3595 888b7d99 2020-12-26 stsp }
3596 888b7d99 2020-12-26 stsp
3597 888b7d99 2020-12-26 stsp static const struct got_error *
3598 888b7d99 2020-12-26 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3599 888b7d99 2020-12-26 stsp struct got_repository *repo, const char *path,
3600 888b7d99 2020-12-26 stsp struct got_pathlist_head *changed_paths, int show_patch,
3601 888b7d99 2020-12-26 stsp int diff_context, struct got_reflist_object_id_map *refs_idmap)
3602 888b7d99 2020-12-26 stsp {
3603 888b7d99 2020-12-26 stsp const struct got_error *err = NULL;
3604 888b7d99 2020-12-26 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3605 888b7d99 2020-12-26 stsp char datebuf[26];
3606 888b7d99 2020-12-26 stsp time_t committer_time;
3607 888b7d99 2020-12-26 stsp const char *author, *committer;
3608 888b7d99 2020-12-26 stsp char *refs_str = NULL;
3609 888b7d99 2020-12-26 stsp struct got_reflist_head *refs;
3610 888b7d99 2020-12-26 stsp
3611 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3612 8bf5b3c9 2018-03-17 stsp if (err)
3613 8bf5b3c9 2018-03-17 stsp return err;
3614 788c352e 2018-06-16 stsp
3615 888b7d99 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3616 888b7d99 2020-12-26 stsp if (refs) {
3617 888b7d99 2020-12-26 stsp err = build_refs_str(&refs_str, refs, id, repo);
3618 888b7d99 2020-12-26 stsp if (err)
3619 888b7d99 2020-12-26 stsp goto done;
3620 888b7d99 2020-12-26 stsp }
3621 888b7d99 2020-12-26 stsp
3622 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3623 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3624 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3625 832c249c 2018-06-10 stsp free(id_str);
3626 d3d493d7 2019-02-21 stsp id_str = NULL;
3627 d3d493d7 2019-02-21 stsp free(refs_str);
3628 d3d493d7 2019-02-21 stsp refs_str = NULL;
3629 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3630 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3631 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3632 09867e48 2019-08-13 stsp if (datestr)
3633 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3634 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3635 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3636 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3637 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3638 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3639 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3640 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3641 3fe1abad 2018-06-10 stsp int n = 1;
3642 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3643 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3644 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3645 a0603db2 2018-06-10 stsp if (err)
3646 888b7d99 2020-12-26 stsp goto done;
3647 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3648 a0603db2 2018-06-10 stsp free(id_str);
3649 888b7d99 2020-12-26 stsp id_str = NULL;
3650 a0603db2 2018-06-10 stsp }
3651 a0603db2 2018-06-10 stsp }
3652 832c249c 2018-06-10 stsp
3653 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3654 5943eee2 2019-08-13 stsp if (err)
3655 888b7d99 2020-12-26 stsp goto done;
3656 8bf5b3c9 2018-03-17 stsp
3657 621015ac 2018-07-23 stsp logmsg = logmsg0;
3658 832c249c 2018-06-10 stsp do {
3659 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3660 832c249c 2018-06-10 stsp if (line)
3661 832c249c 2018-06-10 stsp printf(" %s\n", line);
3662 832c249c 2018-06-10 stsp } while (line);
3663 621015ac 2018-07-23 stsp free(logmsg0);
3664 832c249c 2018-06-10 stsp
3665 0208f208 2020-05-05 stsp if (changed_paths) {
3666 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3667 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3668 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3669 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3670 0208f208 2020-05-05 stsp }
3671 0208f208 2020-05-05 stsp printf("\n");
3672 0208f208 2020-05-05 stsp }
3673 971751ac 2018-03-27 stsp if (show_patch) {
3674 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3675 971751ac 2018-03-27 stsp if (err == 0)
3676 971751ac 2018-03-27 stsp printf("\n");
3677 971751ac 2018-03-27 stsp }
3678 07862c20 2018-09-15 stsp
3679 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3680 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3681 888b7d99 2020-12-26 stsp done:
3682 888b7d99 2020-12-26 stsp free(id_str);
3683 888b7d99 2020-12-26 stsp free(refs_str);
3684 07862c20 2018-09-15 stsp return err;
3685 f42b1b34 2018-03-12 stsp }
3686 5c860e29 2018-03-12 stsp
3687 f42b1b34 2018-03-12 stsp static const struct got_error *
3688 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3689 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3690 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3691 888b7d99 2020-12-26 stsp int log_branches, int reverse_display_order,
3692 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap)
3693 f42b1b34 2018-03-12 stsp {
3694 f42b1b34 2018-03-12 stsp const struct got_error *err;
3695 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3696 6841bf13 2019-11-29 kn regex_t regex;
3697 6841bf13 2019-11-29 kn int have_match;
3698 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3699 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3700 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3701 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3702 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3703 dbec59df 2020-04-18 stsp
3704 dbec59df 2020-04-18 stsp SIMPLEQ_INIT(&reversed_commits);
3705 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3706 372ccdbb 2018-06-10 stsp
3707 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(&regex, search_pattern,
3708 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3709 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3710 6841bf13 2019-11-29 kn
3711 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3712 f42b1b34 2018-03-12 stsp if (err)
3713 f42b1b34 2018-03-12 stsp return err;
3714 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3715 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3716 372ccdbb 2018-06-10 stsp if (err)
3717 fcc85cad 2018-07-22 stsp goto done;
3718 656b1f76 2019-05-11 jcs for (;;) {
3719 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3720 8bf5b3c9 2018-03-17 stsp
3721 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3722 84453469 2018-11-11 stsp break;
3723 84453469 2018-11-11 stsp
3724 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3725 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3726 372ccdbb 2018-06-10 stsp if (err) {
3727 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3728 9ba79e04 2018-06-11 stsp err = NULL;
3729 ee780d5c 2020-01-04 stsp break;
3730 7e665116 2018-04-02 stsp }
3731 b43fbaa0 2018-06-11 stsp if (id == NULL)
3732 7e665116 2018-04-02 stsp break;
3733 b43fbaa0 2018-06-11 stsp
3734 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3735 b43fbaa0 2018-06-11 stsp if (err)
3736 fcc85cad 2018-07-22 stsp break;
3737 6841bf13 2019-11-29 kn
3738 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3739 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3740 0208f208 2020-05-05 stsp if (err)
3741 0208f208 2020-05-05 stsp break;
3742 0208f208 2020-05-05 stsp }
3743 0208f208 2020-05-05 stsp
3744 6841bf13 2019-11-29 kn if (search_pattern) {
3745 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3746 6841bf13 2019-11-29 kn if (err) {
3747 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3748 6841bf13 2019-11-29 kn break;
3749 6841bf13 2019-11-29 kn }
3750 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3751 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3752 0208f208 2020-05-05 stsp &changed_paths, &regex);
3753 6841bf13 2019-11-29 kn if (have_match == 0) {
3754 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3755 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3756 0208f208 2020-05-05 stsp free((char *)pe->path);
3757 0208f208 2020-05-05 stsp free(pe->data);
3758 0208f208 2020-05-05 stsp }
3759 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3760 6841bf13 2019-11-29 kn continue;
3761 6841bf13 2019-11-29 kn }
3762 6841bf13 2019-11-29 kn }
3763 6841bf13 2019-11-29 kn
3764 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3765 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3766 dbec59df 2020-04-18 stsp if (err)
3767 dbec59df 2020-04-18 stsp break;
3768 dbec59df 2020-04-18 stsp SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3769 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3770 dbec59df 2020-04-18 stsp } else {
3771 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3772 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3773 888b7d99 2020-12-26 stsp show_patch, diff_context, refs_idmap);
3774 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3775 dbec59df 2020-04-18 stsp if (err)
3776 dbec59df 2020-04-18 stsp break;
3777 dbec59df 2020-04-18 stsp }
3778 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3779 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3780 7e665116 2018-04-02 stsp break;
3781 0208f208 2020-05-05 stsp
3782 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3783 0208f208 2020-05-05 stsp free((char *)pe->path);
3784 0208f208 2020-05-05 stsp free(pe->data);
3785 0208f208 2020-05-05 stsp }
3786 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3787 31cedeaf 2018-09-15 stsp }
3788 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3789 dbec59df 2020-04-18 stsp SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3790 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3791 dbec59df 2020-04-18 stsp if (err)
3792 dbec59df 2020-04-18 stsp break;
3793 502b9684 2020-07-31 stsp if (show_changed_paths) {
3794 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3795 502b9684 2020-07-31 stsp commit, repo);
3796 502b9684 2020-07-31 stsp if (err)
3797 502b9684 2020-07-31 stsp break;
3798 502b9684 2020-07-31 stsp }
3799 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3800 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3801 888b7d99 2020-12-26 stsp show_patch, diff_context, refs_idmap);
3802 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3803 dbec59df 2020-04-18 stsp if (err)
3804 dbec59df 2020-04-18 stsp break;
3805 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3806 502b9684 2020-07-31 stsp free((char *)pe->path);
3807 502b9684 2020-07-31 stsp free(pe->data);
3808 502b9684 2020-07-31 stsp }
3809 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3810 dbec59df 2020-04-18 stsp }
3811 dbec59df 2020-04-18 stsp }
3812 fcc85cad 2018-07-22 stsp done:
3813 dbec59df 2020-04-18 stsp while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3814 dbec59df 2020-04-18 stsp qid = SIMPLEQ_FIRST(&reversed_commits);
3815 dbec59df 2020-04-18 stsp SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3816 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3817 dbec59df 2020-04-18 stsp }
3818 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3819 0208f208 2020-05-05 stsp free((char *)pe->path);
3820 0208f208 2020-05-05 stsp free(pe->data);
3821 0208f208 2020-05-05 stsp }
3822 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3823 6841bf13 2019-11-29 kn if (search_pattern)
3824 6841bf13 2019-11-29 kn regfree(&regex);
3825 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3826 f42b1b34 2018-03-12 stsp return err;
3827 f42b1b34 2018-03-12 stsp }
3828 5c860e29 2018-03-12 stsp
3829 4ed7e80c 2018-05-20 stsp __dead static void
3830 6f3d1eb0 2018-03-12 stsp usage_log(void)
3831 6f3d1eb0 2018-03-12 stsp {
3832 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3833 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3834 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3835 6f3d1eb0 2018-03-12 stsp exit(1);
3836 b1ebc001 2019-08-13 stsp }
3837 b1ebc001 2019-08-13 stsp
3838 b1ebc001 2019-08-13 stsp static int
3839 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3840 b1ebc001 2019-08-13 stsp {
3841 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3842 b1ebc001 2019-08-13 stsp long long n;
3843 b1ebc001 2019-08-13 stsp const char *errstr;
3844 b1ebc001 2019-08-13 stsp
3845 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3846 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3847 b1ebc001 2019-08-13 stsp return 0;
3848 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3849 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3850 b1ebc001 2019-08-13 stsp return 0;
3851 b1ebc001 2019-08-13 stsp return n;
3852 6f3d1eb0 2018-03-12 stsp }
3853 6f3d1eb0 2018-03-12 stsp
3854 4ed7e80c 2018-05-20 stsp static const struct got_error *
3855 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3856 f42b1b34 2018-03-12 stsp {
3857 f42b1b34 2018-03-12 stsp const struct got_error *error;
3858 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3859 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3860 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3861 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3862 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3863 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3864 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3865 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3866 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3867 64a96a6d 2018-04-01 stsp const char *errstr;
3868 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3869 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
3870 1b3893a2 2019-03-18 stsp
3871 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3872 5c860e29 2018-03-12 stsp
3873 6715a751 2018-03-16 stsp #ifndef PROFILE
3874 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3875 6098196c 2019-01-04 stsp NULL)
3876 ad242220 2018-09-08 stsp == -1)
3877 f42b1b34 2018-03-12 stsp err(1, "pledge");
3878 6715a751 2018-03-16 stsp #endif
3879 79109fed 2018-03-27 stsp
3880 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3881 b1ebc001 2019-08-13 stsp
3882 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3883 79109fed 2018-03-27 stsp switch (ch) {
3884 79109fed 2018-03-27 stsp case 'p':
3885 79109fed 2018-03-27 stsp show_patch = 1;
3886 d142fc45 2018-04-01 stsp break;
3887 0208f208 2020-05-05 stsp case 'P':
3888 0208f208 2020-05-05 stsp show_changed_paths = 1;
3889 0208f208 2020-05-05 stsp break;
3890 d142fc45 2018-04-01 stsp case 'c':
3891 d142fc45 2018-04-01 stsp start_commit = optarg;
3892 64a96a6d 2018-04-01 stsp break;
3893 c0cc5c62 2018-10-18 stsp case 'C':
3894 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3895 4a8520aa 2018-10-18 stsp &errstr);
3896 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3897 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3898 c0cc5c62 2018-10-18 stsp break;
3899 64a96a6d 2018-04-01 stsp case 'l':
3900 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3901 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3902 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3903 cc54c501 2019-07-15 stsp break;
3904 48c8c60d 2020-01-27 stsp case 'b':
3905 48c8c60d 2020-01-27 stsp log_branches = 1;
3906 a0603db2 2018-06-10 stsp break;
3907 04ca23f4 2018-07-16 stsp case 'r':
3908 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3909 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3910 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3911 9ba1d308 2019-10-21 stsp optarg);
3912 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3913 04ca23f4 2018-07-16 stsp break;
3914 dbec59df 2020-04-18 stsp case 'R':
3915 dbec59df 2020-04-18 stsp reverse_display_order = 1;
3916 dbec59df 2020-04-18 stsp break;
3917 6841bf13 2019-11-29 kn case 's':
3918 6841bf13 2019-11-29 kn search_pattern = optarg;
3919 6841bf13 2019-11-29 kn break;
3920 d1fe46f9 2020-04-18 stsp case 'x':
3921 d1fe46f9 2020-04-18 stsp end_commit = optarg;
3922 d1fe46f9 2020-04-18 stsp break;
3923 79109fed 2018-03-27 stsp default:
3924 2deda0b9 2019-03-07 stsp usage_log();
3925 79109fed 2018-03-27 stsp /* NOTREACHED */
3926 79109fed 2018-03-27 stsp }
3927 79109fed 2018-03-27 stsp }
3928 79109fed 2018-03-27 stsp
3929 79109fed 2018-03-27 stsp argc -= optind;
3930 79109fed 2018-03-27 stsp argv += optind;
3931 f42b1b34 2018-03-12 stsp
3932 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3933 dc1edbfa 2019-11-29 kn diff_context = 3;
3934 dc1edbfa 2019-11-29 kn else if (!show_patch)
3935 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
3936 dc1edbfa 2019-11-29 kn
3937 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3938 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3939 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3940 04ca23f4 2018-07-16 stsp goto done;
3941 04ca23f4 2018-07-16 stsp }
3942 cffc0aa4 2019-02-05 stsp
3943 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
3944 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
3945 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3946 50f2fada 2020-04-24 stsp goto done;
3947 50f2fada 2020-04-24 stsp error = NULL;
3948 50f2fada 2020-04-24 stsp }
3949 cffc0aa4 2019-02-05 stsp
3950 603cdeb0 2020-10-22 stsp if (argc == 1) {
3951 e7301579 2019-03-18 stsp if (worktree) {
3952 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3953 e7301579 2019-03-18 stsp argv[0]);
3954 e7301579 2019-03-18 stsp if (error)
3955 e7301579 2019-03-18 stsp goto done;
3956 e7301579 2019-03-18 stsp } else {
3957 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3958 e7301579 2019-03-18 stsp if (path == NULL) {
3959 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3960 e7301579 2019-03-18 stsp goto done;
3961 e7301579 2019-03-18 stsp }
3962 e7301579 2019-03-18 stsp }
3963 603cdeb0 2020-10-22 stsp } else if (argc != 0)
3964 cbd1af7a 2019-03-18 stsp usage_log();
3965 cbd1af7a 2019-03-18 stsp
3966 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3967 5486daa2 2019-05-11 stsp repo_path = worktree ?
3968 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3969 5486daa2 2019-05-11 stsp }
3970 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3971 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3972 cffc0aa4 2019-02-05 stsp goto done;
3973 04ca23f4 2018-07-16 stsp }
3974 6098196c 2019-01-04 stsp
3975 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3976 d7d4f210 2018-03-12 stsp if (error != NULL)
3977 04ca23f4 2018-07-16 stsp goto done;
3978 f42b1b34 2018-03-12 stsp
3979 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3980 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3981 c02c541e 2019-03-29 stsp if (error)
3982 c02c541e 2019-03-29 stsp goto done;
3983 c02c541e 2019-03-29 stsp
3984 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3985 84de9106 2020-12-26 stsp if (error)
3986 84de9106 2020-12-26 stsp goto done;
3987 84de9106 2020-12-26 stsp
3988 888b7d99 2020-12-26 stsp error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
3989 888b7d99 2020-12-26 stsp if (error)
3990 888b7d99 2020-12-26 stsp goto done;
3991 888b7d99 2020-12-26 stsp
3992 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3993 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3994 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
3995 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3996 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3997 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3998 3235492e 2018-04-01 stsp if (error != NULL)
3999 d1fe46f9 2020-04-18 stsp goto done;
4000 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
4001 3235492e 2018-04-01 stsp got_ref_close(head_ref);
4002 3235492e 2018-04-01 stsp if (error != NULL)
4003 d1fe46f9 2020-04-18 stsp goto done;
4004 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
4005 d1fe46f9 2020-04-18 stsp start_id);
4006 d1fe46f9 2020-04-18 stsp if (error != NULL)
4007 d1fe46f9 2020-04-18 stsp goto done;
4008 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
4009 3235492e 2018-04-01 stsp } else {
4010 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&start_id, NULL,
4011 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4012 d1fe46f9 2020-04-18 stsp if (error != NULL)
4013 d1fe46f9 2020-04-18 stsp goto done;
4014 3235492e 2018-04-01 stsp }
4015 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
4016 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&end_id, NULL,
4017 84de9106 2020-12-26 stsp end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4018 d1fe46f9 2020-04-18 stsp if (error != NULL)
4019 d1fe46f9 2020-04-18 stsp goto done;
4020 d1fe46f9 2020-04-18 stsp }
4021 04ca23f4 2018-07-16 stsp
4022 deeabeae 2019-08-27 stsp if (worktree) {
4023 603cdeb0 2020-10-22 stsp /*
4024 603cdeb0 2020-10-22 stsp * If a path was specified on the command line it was resolved
4025 603cdeb0 2020-10-22 stsp * to a path in the work tree above. Prepend the work tree's
4026 603cdeb0 2020-10-22 stsp * path prefix to obtain the corresponding in-repository path.
4027 603cdeb0 2020-10-22 stsp */
4028 603cdeb0 2020-10-22 stsp if (path) {
4029 603cdeb0 2020-10-22 stsp const char *prefix;
4030 603cdeb0 2020-10-22 stsp prefix = got_worktree_get_path_prefix(worktree);
4031 603cdeb0 2020-10-22 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4032 603cdeb0 2020-10-22 stsp (path[0] != '\0') ? "/" : "", path) == -1) {
4033 603cdeb0 2020-10-22 stsp error = got_error_from_errno("asprintf");
4034 603cdeb0 2020-10-22 stsp goto done;
4035 603cdeb0 2020-10-22 stsp }
4036 603cdeb0 2020-10-22 stsp }
4037 deeabeae 2019-08-27 stsp } else
4038 603cdeb0 2020-10-22 stsp error = got_repo_map_path(&in_repo_path, repo,
4039 8fa913ec 2020-11-14 stsp path ? path : "");
4040 04ca23f4 2018-07-16 stsp if (error != NULL)
4041 04ca23f4 2018-07-16 stsp goto done;
4042 04ca23f4 2018-07-16 stsp if (in_repo_path) {
4043 04ca23f4 2018-07-16 stsp free(path);
4044 04ca23f4 2018-07-16 stsp path = in_repo_path;
4045 04ca23f4 2018-07-16 stsp }
4046 199a4027 2019-02-02 stsp
4047 603cdeb0 2020-10-22 stsp error = print_commits(start_id, end_id, repo, path ? path : "",
4048 603cdeb0 2020-10-22 stsp show_changed_paths, show_patch, search_pattern, diff_context,
4049 888b7d99 2020-12-26 stsp limit, log_branches, reverse_display_order, refs_idmap);
4050 04ca23f4 2018-07-16 stsp done:
4051 04ca23f4 2018-07-16 stsp free(path);
4052 04ca23f4 2018-07-16 stsp free(repo_path);
4053 04ca23f4 2018-07-16 stsp free(cwd);
4054 cffc0aa4 2019-02-05 stsp if (worktree)
4055 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
4056 ad242220 2018-09-08 stsp if (repo) {
4057 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4058 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4059 ad242220 2018-09-08 stsp if (error == NULL)
4060 ad242220 2018-09-08 stsp error = repo_error;
4061 ad242220 2018-09-08 stsp }
4062 888b7d99 2020-12-26 stsp if (refs_idmap)
4063 888b7d99 2020-12-26 stsp got_reflist_object_id_map_free(refs_idmap);
4064 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4065 8bf5b3c9 2018-03-17 stsp return error;
4066 5c860e29 2018-03-12 stsp }
4067 5c860e29 2018-03-12 stsp
4068 4ed7e80c 2018-05-20 stsp __dead static void
4069 3f8b7d6a 2018-04-01 stsp usage_diff(void)
4070 3f8b7d6a 2018-04-01 stsp {
4071 64453f7e 2020-11-21 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4072 64453f7e 2020-11-21 stsp "[-s] [-w] [object1 object2 | path]\n", getprogname());
4073 3f8b7d6a 2018-04-01 stsp exit(1);
4074 b00d56cd 2018-04-01 stsp }
4075 b00d56cd 2018-04-01 stsp
4076 b72f483a 2019-02-05 stsp struct print_diff_arg {
4077 b72f483a 2019-02-05 stsp struct got_repository *repo;
4078 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
4079 b72f483a 2019-02-05 stsp int diff_context;
4080 3fc0c068 2019-02-10 stsp const char *id_str;
4081 3fc0c068 2019-02-10 stsp int header_shown;
4082 98eaaa12 2019-08-03 stsp int diff_staged;
4083 63035f9f 2019-10-06 stsp int ignore_whitespace;
4084 64453f7e 2020-11-21 stsp int force_text_diff;
4085 b72f483a 2019-02-05 stsp };
4086 39449a05 2020-07-23 stsp
4087 39449a05 2020-07-23 stsp /*
4088 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
4089 39449a05 2020-07-23 stsp * it as content to the diff engine.
4090 39449a05 2020-07-23 stsp */
4091 39449a05 2020-07-23 stsp static const struct got_error *
4092 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4093 39449a05 2020-07-23 stsp const char *abspath)
4094 39449a05 2020-07-23 stsp {
4095 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
4096 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
4097 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
4098 39449a05 2020-07-23 stsp
4099 39449a05 2020-07-23 stsp *fd = -1;
4100 39449a05 2020-07-23 stsp
4101 39449a05 2020-07-23 stsp if (dirfd != -1) {
4102 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4103 39449a05 2020-07-23 stsp if (target_len == -1)
4104 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
4105 39449a05 2020-07-23 stsp } else {
4106 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
4107 39449a05 2020-07-23 stsp if (target_len == -1)
4108 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
4109 39449a05 2020-07-23 stsp }
4110 39449a05 2020-07-23 stsp
4111 39449a05 2020-07-23 stsp *fd = got_opentempfd();
4112 39449a05 2020-07-23 stsp if (*fd == -1)
4113 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
4114 39449a05 2020-07-23 stsp
4115 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
4116 39449a05 2020-07-23 stsp if (outlen == -1) {
4117 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
4118 39449a05 2020-07-23 stsp goto done;
4119 39449a05 2020-07-23 stsp }
4120 39449a05 2020-07-23 stsp
4121 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
4122 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
4123 39449a05 2020-07-23 stsp goto done;
4124 39449a05 2020-07-23 stsp }
4125 39449a05 2020-07-23 stsp done:
4126 39449a05 2020-07-23 stsp if (err) {
4127 39449a05 2020-07-23 stsp close(*fd);
4128 39449a05 2020-07-23 stsp *fd = -1;
4129 39449a05 2020-07-23 stsp }
4130 39449a05 2020-07-23 stsp return err;
4131 39449a05 2020-07-23 stsp }
4132 b72f483a 2019-02-05 stsp
4133 4ed7e80c 2018-05-20 stsp static const struct got_error *
4134 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
4135 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4136 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4137 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4138 b72f483a 2019-02-05 stsp {
4139 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
4140 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
4141 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
4142 12463d8b 2019-12-13 stsp int fd = -1;
4143 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
4144 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
4145 b72f483a 2019-02-05 stsp struct stat sb;
4146 b72f483a 2019-02-05 stsp
4147 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4148 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4149 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4150 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4151 98eaaa12 2019-08-03 stsp return NULL;
4152 98eaaa12 2019-08-03 stsp } else {
4153 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4154 98eaaa12 2019-08-03 stsp return NULL;
4155 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4156 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
4157 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4158 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
4159 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
4160 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
4161 98eaaa12 2019-08-03 stsp return NULL;
4162 98eaaa12 2019-08-03 stsp }
4163 3fc0c068 2019-02-10 stsp
4164 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
4165 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
4166 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
4167 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
4168 3fc0c068 2019-02-10 stsp a->header_shown = 1;
4169 3fc0c068 2019-02-10 stsp }
4170 b72f483a 2019-02-05 stsp
4171 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4172 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
4173 98eaaa12 2019-08-03 stsp switch (staged_status) {
4174 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
4175 98eaaa12 2019-08-03 stsp label1 = path;
4176 98eaaa12 2019-08-03 stsp label2 = path;
4177 98eaaa12 2019-08-03 stsp break;
4178 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
4179 98eaaa12 2019-08-03 stsp label2 = path;
4180 98eaaa12 2019-08-03 stsp break;
4181 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
4182 98eaaa12 2019-08-03 stsp label1 = path;
4183 98eaaa12 2019-08-03 stsp break;
4184 98eaaa12 2019-08-03 stsp default:
4185 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
4186 98eaaa12 2019-08-03 stsp }
4187 fe621944 2020-11-10 stsp return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4188 fe621944 2020-11-10 stsp staged_blob_id, label1, label2, a->diff_context,
4189 64453f7e 2020-11-21 stsp a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4190 98eaaa12 2019-08-03 stsp }
4191 98eaaa12 2019-08-03 stsp
4192 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4193 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
4194 4ce46740 2019-08-08 stsp char *id_str;
4195 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4196 408b4ebc 2019-08-03 stsp 8192);
4197 4ce46740 2019-08-08 stsp if (err)
4198 4ce46740 2019-08-08 stsp goto done;
4199 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4200 4ce46740 2019-08-08 stsp if (err)
4201 4ce46740 2019-08-08 stsp goto done;
4202 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4203 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4204 4ce46740 2019-08-08 stsp free(id_str);
4205 4ce46740 2019-08-08 stsp goto done;
4206 4ce46740 2019-08-08 stsp }
4207 4ce46740 2019-08-08 stsp free(id_str);
4208 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4209 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4210 4ce46740 2019-08-08 stsp if (err)
4211 4ce46740 2019-08-08 stsp goto done;
4212 4ce46740 2019-08-08 stsp }
4213 d00136be 2019-03-26 stsp
4214 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4215 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4216 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4217 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4218 2ec1f75b 2019-03-26 stsp goto done;
4219 2ec1f75b 2019-03-26 stsp }
4220 b72f483a 2019-02-05 stsp
4221 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4222 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4223 12463d8b 2019-12-13 stsp if (fd == -1) {
4224 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4225 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4226 39449a05 2020-07-23 stsp abspath);
4227 39449a05 2020-07-23 stsp goto done;
4228 39449a05 2020-07-23 stsp }
4229 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4230 39449a05 2020-07-23 stsp de_name, abspath);
4231 39449a05 2020-07-23 stsp if (err)
4232 39449a05 2020-07-23 stsp goto done;
4233 12463d8b 2019-12-13 stsp }
4234 12463d8b 2019-12-13 stsp } else {
4235 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4236 12463d8b 2019-12-13 stsp if (fd == -1) {
4237 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4238 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4239 39449a05 2020-07-23 stsp abspath);
4240 39449a05 2020-07-23 stsp goto done;
4241 39449a05 2020-07-23 stsp }
4242 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4243 39449a05 2020-07-23 stsp de_name, abspath);
4244 39449a05 2020-07-23 stsp if (err)
4245 39449a05 2020-07-23 stsp goto done;
4246 12463d8b 2019-12-13 stsp }
4247 12463d8b 2019-12-13 stsp }
4248 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4249 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4250 2ec1f75b 2019-03-26 stsp goto done;
4251 2ec1f75b 2019-03-26 stsp }
4252 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4253 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4254 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4255 2ec1f75b 2019-03-26 stsp goto done;
4256 2ec1f75b 2019-03-26 stsp }
4257 12463d8b 2019-12-13 stsp fd = -1;
4258 2ec1f75b 2019-03-26 stsp } else
4259 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4260 b72f483a 2019-02-05 stsp
4261 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4262 64453f7e 2020-11-21 stsp a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4263 b72f483a 2019-02-05 stsp done:
4264 b72f483a 2019-02-05 stsp if (blob1)
4265 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4266 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4267 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4268 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4269 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4270 b72f483a 2019-02-05 stsp free(abspath);
4271 927df6b7 2019-02-10 stsp return err;
4272 927df6b7 2019-02-10 stsp }
4273 927df6b7 2019-02-10 stsp
4274 927df6b7 2019-02-10 stsp static const struct got_error *
4275 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4276 b00d56cd 2018-04-01 stsp {
4277 b00d56cd 2018-04-01 stsp const struct got_error *error;
4278 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4279 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4280 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4281 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4282 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
4283 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
4284 15a94983 2018-12-23 stsp int type1, type2;
4285 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4286 64453f7e 2020-11-21 stsp int force_text_diff = 0;
4287 c0cc5c62 2018-10-18 stsp const char *errstr;
4288 927df6b7 2019-02-10 stsp char *path = NULL;
4289 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4290 b00d56cd 2018-04-01 stsp
4291 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4292 84de9106 2020-12-26 stsp
4293 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4294 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4295 25eccc22 2019-01-04 stsp NULL) == -1)
4296 b00d56cd 2018-04-01 stsp err(1, "pledge");
4297 b00d56cd 2018-04-01 stsp #endif
4298 b00d56cd 2018-04-01 stsp
4299 64453f7e 2020-11-21 stsp while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4300 b00d56cd 2018-04-01 stsp switch (ch) {
4301 64453f7e 2020-11-21 stsp case 'a':
4302 64453f7e 2020-11-21 stsp force_text_diff = 1;
4303 64453f7e 2020-11-21 stsp break;
4304 c0cc5c62 2018-10-18 stsp case 'C':
4305 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4306 dfcab68b 2019-11-29 kn &errstr);
4307 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4308 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4309 c0cc5c62 2018-10-18 stsp break;
4310 b72f483a 2019-02-05 stsp case 'r':
4311 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4312 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4313 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4314 9ba1d308 2019-10-21 stsp optarg);
4315 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4316 b72f483a 2019-02-05 stsp break;
4317 98eaaa12 2019-08-03 stsp case 's':
4318 98eaaa12 2019-08-03 stsp diff_staged = 1;
4319 63035f9f 2019-10-06 stsp break;
4320 63035f9f 2019-10-06 stsp case 'w':
4321 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4322 98eaaa12 2019-08-03 stsp break;
4323 b00d56cd 2018-04-01 stsp default:
4324 2deda0b9 2019-03-07 stsp usage_diff();
4325 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4326 b00d56cd 2018-04-01 stsp }
4327 b00d56cd 2018-04-01 stsp }
4328 b00d56cd 2018-04-01 stsp
4329 b00d56cd 2018-04-01 stsp argc -= optind;
4330 b00d56cd 2018-04-01 stsp argv += optind;
4331 b00d56cd 2018-04-01 stsp
4332 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4333 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4334 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4335 b72f483a 2019-02-05 stsp goto done;
4336 b72f483a 2019-02-05 stsp }
4337 927df6b7 2019-02-10 stsp if (argc <= 1) {
4338 b72f483a 2019-02-05 stsp if (repo_path)
4339 b72f483a 2019-02-05 stsp errx(1,
4340 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
4341 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4342 fa51e947 2020-03-27 stsp if (error) {
4343 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4344 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
4345 fa51e947 2020-03-27 stsp cwd);
4346 1f03b8da 2020-03-20 stsp goto done;
4347 fa51e947 2020-03-27 stsp }
4348 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4349 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4350 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4351 927df6b7 2019-02-10 stsp goto done;
4352 927df6b7 2019-02-10 stsp }
4353 927df6b7 2019-02-10 stsp if (argc == 1) {
4354 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4355 cbd1af7a 2019-03-18 stsp argv[0]);
4356 927df6b7 2019-02-10 stsp if (error)
4357 927df6b7 2019-02-10 stsp goto done;
4358 927df6b7 2019-02-10 stsp } else {
4359 927df6b7 2019-02-10 stsp path = strdup("");
4360 927df6b7 2019-02-10 stsp if (path == NULL) {
4361 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4362 927df6b7 2019-02-10 stsp goto done;
4363 927df6b7 2019-02-10 stsp }
4364 927df6b7 2019-02-10 stsp }
4365 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4366 98eaaa12 2019-08-03 stsp if (diff_staged)
4367 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4368 98eaaa12 2019-08-03 stsp "objects in repository");
4369 15a94983 2018-12-23 stsp id_str1 = argv[0];
4370 15a94983 2018-12-23 stsp id_str2 = argv[1];
4371 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4372 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4373 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4374 30db809c 2019-06-05 stsp goto done;
4375 1f03b8da 2020-03-20 stsp if (worktree) {
4376 1f03b8da 2020-03-20 stsp repo_path = strdup(
4377 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
4378 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4379 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4380 1f03b8da 2020-03-20 stsp goto done;
4381 1f03b8da 2020-03-20 stsp }
4382 1f03b8da 2020-03-20 stsp } else {
4383 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
4384 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4385 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4386 1f03b8da 2020-03-20 stsp goto done;
4387 1f03b8da 2020-03-20 stsp }
4388 30db809c 2019-06-05 stsp }
4389 30db809c 2019-06-05 stsp }
4390 b00d56cd 2018-04-01 stsp } else
4391 b00d56cd 2018-04-01 stsp usage_diff();
4392 b00d56cd 2018-04-01 stsp
4393 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4394 76089277 2018-04-01 stsp free(repo_path);
4395 b00d56cd 2018-04-01 stsp if (error != NULL)
4396 b00d56cd 2018-04-01 stsp goto done;
4397 b00d56cd 2018-04-01 stsp
4398 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4399 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4400 c02c541e 2019-03-29 stsp if (error)
4401 c02c541e 2019-03-29 stsp goto done;
4402 c02c541e 2019-03-29 stsp
4403 30db809c 2019-06-05 stsp if (argc <= 1) {
4404 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4405 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4406 b72f483a 2019-02-05 stsp char *id_str;
4407 72ea6654 2019-07-27 stsp
4408 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4409 72ea6654 2019-07-27 stsp
4410 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4411 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4412 b72f483a 2019-02-05 stsp if (error)
4413 b72f483a 2019-02-05 stsp goto done;
4414 b72f483a 2019-02-05 stsp arg.repo = repo;
4415 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4416 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4417 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4418 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4419 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4420 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4421 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
4422 b72f483a 2019-02-05 stsp
4423 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4424 72ea6654 2019-07-27 stsp if (error)
4425 72ea6654 2019-07-27 stsp goto done;
4426 72ea6654 2019-07-27 stsp
4427 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
4428 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
4429 b72f483a 2019-02-05 stsp free(id_str);
4430 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4431 b72f483a 2019-02-05 stsp goto done;
4432 b72f483a 2019-02-05 stsp }
4433 84de9106 2020-12-26 stsp
4434 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4435 84de9106 2020-12-26 stsp if (error)
4436 84de9106 2020-12-26 stsp return error;
4437 b72f483a 2019-02-05 stsp
4438 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4439 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4440 0adb7196 2019-08-11 stsp if (error)
4441 0adb7196 2019-08-11 stsp goto done;
4442 b00d56cd 2018-04-01 stsp
4443 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4444 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4445 0adb7196 2019-08-11 stsp if (error)
4446 0adb7196 2019-08-11 stsp goto done;
4447 b00d56cd 2018-04-01 stsp
4448 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4449 15a94983 2018-12-23 stsp if (error)
4450 15a94983 2018-12-23 stsp goto done;
4451 15a94983 2018-12-23 stsp
4452 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4453 15a94983 2018-12-23 stsp if (error)
4454 15a94983 2018-12-23 stsp goto done;
4455 15a94983 2018-12-23 stsp
4456 15a94983 2018-12-23 stsp if (type1 != type2) {
4457 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4458 b00d56cd 2018-04-01 stsp goto done;
4459 b00d56cd 2018-04-01 stsp }
4460 b00d56cd 2018-04-01 stsp
4461 15a94983 2018-12-23 stsp switch (type1) {
4462 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4463 fe621944 2020-11-10 stsp error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4464 64453f7e 2020-11-21 stsp NULL, NULL, diff_context, ignore_whitespace,
4465 64453f7e 2020-11-21 stsp force_text_diff, repo, stdout);
4466 b00d56cd 2018-04-01 stsp break;
4467 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4468 fe621944 2020-11-10 stsp error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4469 64453f7e 2020-11-21 stsp "", "", diff_context, ignore_whitespace, force_text_diff,
4470 64453f7e 2020-11-21 stsp repo, stdout);
4471 b00d56cd 2018-04-01 stsp break;
4472 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4473 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4474 fe621944 2020-11-10 stsp error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4475 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, repo,
4476 64453f7e 2020-11-21 stsp stdout);
4477 b00d56cd 2018-04-01 stsp break;
4478 b00d56cd 2018-04-01 stsp default:
4479 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4480 b00d56cd 2018-04-01 stsp }
4481 b00d56cd 2018-04-01 stsp done:
4482 5e70831e 2019-05-28 stsp free(label1);
4483 5e70831e 2019-05-28 stsp free(label2);
4484 15a94983 2018-12-23 stsp free(id1);
4485 15a94983 2018-12-23 stsp free(id2);
4486 927df6b7 2019-02-10 stsp free(path);
4487 b72f483a 2019-02-05 stsp if (worktree)
4488 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4489 ad242220 2018-09-08 stsp if (repo) {
4490 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4491 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4492 ad242220 2018-09-08 stsp if (error == NULL)
4493 ad242220 2018-09-08 stsp error = repo_error;
4494 ad242220 2018-09-08 stsp }
4495 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4496 b00d56cd 2018-04-01 stsp return error;
4497 404c43c4 2018-06-21 stsp }
4498 404c43c4 2018-06-21 stsp
4499 404c43c4 2018-06-21 stsp __dead static void
4500 404c43c4 2018-06-21 stsp usage_blame(void)
4501 404c43c4 2018-06-21 stsp {
4502 9270e621 2019-02-05 stsp fprintf(stderr,
4503 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4504 404c43c4 2018-06-21 stsp getprogname());
4505 404c43c4 2018-06-21 stsp exit(1);
4506 b00d56cd 2018-04-01 stsp }
4507 b00d56cd 2018-04-01 stsp
4508 28315671 2019-08-14 stsp struct blame_line {
4509 28315671 2019-08-14 stsp int annotated;
4510 28315671 2019-08-14 stsp char *id_str;
4511 82f6abb8 2019-08-14 stsp char *committer;
4512 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4513 28315671 2019-08-14 stsp };
4514 28315671 2019-08-14 stsp
4515 28315671 2019-08-14 stsp struct blame_cb_args {
4516 28315671 2019-08-14 stsp struct blame_line *lines;
4517 28315671 2019-08-14 stsp int nlines;
4518 7ef28ff8 2019-08-14 stsp int nlines_prec;
4519 28315671 2019-08-14 stsp int lineno_cur;
4520 28315671 2019-08-14 stsp off_t *line_offsets;
4521 28315671 2019-08-14 stsp FILE *f;
4522 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4523 28315671 2019-08-14 stsp };
4524 28315671 2019-08-14 stsp
4525 404c43c4 2018-06-21 stsp static const struct got_error *
4526 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4527 28315671 2019-08-14 stsp {
4528 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4529 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4530 28315671 2019-08-14 stsp struct blame_line *bline;
4531 82f6abb8 2019-08-14 stsp char *line = NULL;
4532 28315671 2019-08-14 stsp size_t linesize = 0;
4533 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4534 28315671 2019-08-14 stsp off_t offset;
4535 bcb49d15 2019-08-14 stsp struct tm tm;
4536 bcb49d15 2019-08-14 stsp time_t committer_time;
4537 28315671 2019-08-14 stsp
4538 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4539 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4540 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4541 28315671 2019-08-14 stsp
4542 28315671 2019-08-14 stsp if (sigint_received)
4543 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4544 28315671 2019-08-14 stsp
4545 2839f8b9 2019-08-18 stsp if (lineno == -1)
4546 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4547 2839f8b9 2019-08-18 stsp
4548 28315671 2019-08-14 stsp /* Annotate this line. */
4549 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4550 28315671 2019-08-14 stsp if (bline->annotated)
4551 28315671 2019-08-14 stsp return NULL;
4552 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4553 28315671 2019-08-14 stsp if (err)
4554 28315671 2019-08-14 stsp return err;
4555 82f6abb8 2019-08-14 stsp
4556 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4557 82f6abb8 2019-08-14 stsp if (err)
4558 82f6abb8 2019-08-14 stsp goto done;
4559 82f6abb8 2019-08-14 stsp
4560 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4561 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4562 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4563 bcb49d15 2019-08-14 stsp goto done;
4564 bcb49d15 2019-08-14 stsp }
4565 bcb49d15 2019-08-14 stsp
4566 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4567 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
4568 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
4569 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4570 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
4571 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4572 82f6abb8 2019-08-14 stsp goto done;
4573 82f6abb8 2019-08-14 stsp }
4574 28315671 2019-08-14 stsp bline->annotated = 1;
4575 28315671 2019-08-14 stsp
4576 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4577 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4578 28315671 2019-08-14 stsp if (!bline->annotated)
4579 82f6abb8 2019-08-14 stsp goto done;
4580 28315671 2019-08-14 stsp
4581 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4582 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4583 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4584 82f6abb8 2019-08-14 stsp goto done;
4585 82f6abb8 2019-08-14 stsp }
4586 28315671 2019-08-14 stsp
4587 28315671 2019-08-14 stsp while (bline->annotated) {
4588 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4589 82f6abb8 2019-08-14 stsp size_t len;
4590 82f6abb8 2019-08-14 stsp
4591 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4592 28315671 2019-08-14 stsp if (ferror(a->f))
4593 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4594 28315671 2019-08-14 stsp break;
4595 28315671 2019-08-14 stsp }
4596 82f6abb8 2019-08-14 stsp
4597 82f6abb8 2019-08-14 stsp committer = bline->committer;
4598 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4599 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4600 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4601 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4602 82f6abb8 2019-08-14 stsp if (at)
4603 82f6abb8 2019-08-14 stsp *at = '\0';
4604 82f6abb8 2019-08-14 stsp len = strlen(committer);
4605 82f6abb8 2019-08-14 stsp if (len >= 9)
4606 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4607 28315671 2019-08-14 stsp
4608 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4609 28315671 2019-08-14 stsp if (nl)
4610 28315671 2019-08-14 stsp *nl = '\0';
4611 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4612 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4613 28315671 2019-08-14 stsp
4614 28315671 2019-08-14 stsp a->lineno_cur++;
4615 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4616 28315671 2019-08-14 stsp }
4617 82f6abb8 2019-08-14 stsp done:
4618 82f6abb8 2019-08-14 stsp if (commit)
4619 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4620 28315671 2019-08-14 stsp free(line);
4621 28315671 2019-08-14 stsp return err;
4622 28315671 2019-08-14 stsp }
4623 28315671 2019-08-14 stsp
4624 28315671 2019-08-14 stsp static const struct got_error *
4625 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4626 404c43c4 2018-06-21 stsp {
4627 404c43c4 2018-06-21 stsp const struct got_error *error;
4628 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4629 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4630 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4631 0587e10c 2020-07-23 stsp char *link_target = NULL;
4632 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4633 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4634 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4635 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4636 28315671 2019-08-14 stsp struct blame_cb_args bca;
4637 28315671 2019-08-14 stsp int ch, obj_type, i;
4638 be659d10 2020-11-18 stsp off_t filesize;
4639 90f3c347 2019-08-19 stsp
4640 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4641 404c43c4 2018-06-21 stsp
4642 404c43c4 2018-06-21 stsp #ifndef PROFILE
4643 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4644 36e2fb66 2019-01-04 stsp NULL) == -1)
4645 404c43c4 2018-06-21 stsp err(1, "pledge");
4646 404c43c4 2018-06-21 stsp #endif
4647 404c43c4 2018-06-21 stsp
4648 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4649 404c43c4 2018-06-21 stsp switch (ch) {
4650 404c43c4 2018-06-21 stsp case 'c':
4651 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4652 404c43c4 2018-06-21 stsp break;
4653 66bea077 2018-08-02 stsp case 'r':
4654 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4655 66bea077 2018-08-02 stsp if (repo_path == NULL)
4656 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4657 9ba1d308 2019-10-21 stsp optarg);
4658 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4659 66bea077 2018-08-02 stsp break;
4660 404c43c4 2018-06-21 stsp default:
4661 2deda0b9 2019-03-07 stsp usage_blame();
4662 404c43c4 2018-06-21 stsp /* NOTREACHED */
4663 404c43c4 2018-06-21 stsp }
4664 404c43c4 2018-06-21 stsp }
4665 404c43c4 2018-06-21 stsp
4666 404c43c4 2018-06-21 stsp argc -= optind;
4667 404c43c4 2018-06-21 stsp argv += optind;
4668 404c43c4 2018-06-21 stsp
4669 a39318fd 2018-08-02 stsp if (argc == 1)
4670 404c43c4 2018-06-21 stsp path = argv[0];
4671 a39318fd 2018-08-02 stsp else
4672 404c43c4 2018-06-21 stsp usage_blame();
4673 404c43c4 2018-06-21 stsp
4674 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4675 66bea077 2018-08-02 stsp if (cwd == NULL) {
4676 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4677 66bea077 2018-08-02 stsp goto done;
4678 66bea077 2018-08-02 stsp }
4679 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4680 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4681 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4682 66bea077 2018-08-02 stsp goto done;
4683 0c06baac 2019-02-05 stsp else
4684 0c06baac 2019-02-05 stsp error = NULL;
4685 0c06baac 2019-02-05 stsp if (worktree) {
4686 0c06baac 2019-02-05 stsp repo_path =
4687 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4688 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4689 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4690 1f03b8da 2020-03-20 stsp if (error)
4691 1f03b8da 2020-03-20 stsp goto done;
4692 1f03b8da 2020-03-20 stsp }
4693 0c06baac 2019-02-05 stsp } else {
4694 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4695 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4696 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4697 0c06baac 2019-02-05 stsp goto done;
4698 0c06baac 2019-02-05 stsp }
4699 66bea077 2018-08-02 stsp }
4700 66bea077 2018-08-02 stsp }
4701 36e2fb66 2019-01-04 stsp
4702 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4703 c02c541e 2019-03-29 stsp if (error != NULL)
4704 404c43c4 2018-06-21 stsp goto done;
4705 404c43c4 2018-06-21 stsp
4706 0c06baac 2019-02-05 stsp if (worktree) {
4707 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4708 01740607 2020-11-04 stsp char *p;
4709 01740607 2020-11-04 stsp
4710 01740607 2020-11-04 stsp error = got_worktree_resolve_path(&p, worktree, path);
4711 01740607 2020-11-04 stsp if (error)
4712 01740607 2020-11-04 stsp goto done;
4713 01740607 2020-11-04 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4714 01740607 2020-11-04 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4715 01740607 2020-11-04 stsp p) == -1) {
4716 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4717 01740607 2020-11-04 stsp free(p);
4718 0c06baac 2019-02-05 stsp goto done;
4719 0c06baac 2019-02-05 stsp }
4720 0c06baac 2019-02-05 stsp free(p);
4721 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4722 0c06baac 2019-02-05 stsp } else {
4723 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4724 01740607 2020-11-04 stsp if (error)
4725 01740607 2020-11-04 stsp goto done;
4726 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
4727 0c06baac 2019-02-05 stsp }
4728 0c06baac 2019-02-05 stsp if (error)
4729 66bea077 2018-08-02 stsp goto done;
4730 66bea077 2018-08-02 stsp
4731 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4732 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4733 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4734 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4735 404c43c4 2018-06-21 stsp if (error != NULL)
4736 66bea077 2018-08-02 stsp goto done;
4737 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4738 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4739 404c43c4 2018-06-21 stsp if (error != NULL)
4740 66bea077 2018-08-02 stsp goto done;
4741 404c43c4 2018-06-21 stsp } else {
4742 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4743 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4744 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4745 84de9106 2020-12-26 stsp NULL);
4746 84de9106 2020-12-26 stsp if (error)
4747 84de9106 2020-12-26 stsp goto done;
4748 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4749 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4750 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4751 30837e32 2019-07-25 stsp if (error)
4752 66bea077 2018-08-02 stsp goto done;
4753 404c43c4 2018-06-21 stsp }
4754 404c43c4 2018-06-21 stsp
4755 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4756 0587e10c 2020-07-23 stsp commit_id, repo);
4757 0587e10c 2020-07-23 stsp if (error)
4758 0587e10c 2020-07-23 stsp goto done;
4759 0587e10c 2020-07-23 stsp
4760 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4761 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4762 28315671 2019-08-14 stsp if (error)
4763 28315671 2019-08-14 stsp goto done;
4764 28315671 2019-08-14 stsp
4765 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4766 28315671 2019-08-14 stsp if (error)
4767 28315671 2019-08-14 stsp goto done;
4768 28315671 2019-08-14 stsp
4769 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4770 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4771 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4772 28315671 2019-08-14 stsp goto done;
4773 28315671 2019-08-14 stsp }
4774 28315671 2019-08-14 stsp
4775 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4776 28315671 2019-08-14 stsp if (error)
4777 28315671 2019-08-14 stsp goto done;
4778 28315671 2019-08-14 stsp bca.f = got_opentemp();
4779 28315671 2019-08-14 stsp if (bca.f == NULL) {
4780 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4781 28315671 2019-08-14 stsp goto done;
4782 28315671 2019-08-14 stsp }
4783 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4784 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4785 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4786 28315671 2019-08-14 stsp goto done;
4787 28315671 2019-08-14 stsp
4788 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4789 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4790 b02560ec 2019-08-19 stsp bca.nlines--;
4791 b02560ec 2019-08-19 stsp
4792 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4793 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4794 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4795 28315671 2019-08-14 stsp goto done;
4796 28315671 2019-08-14 stsp }
4797 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4798 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4799 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4800 7ef28ff8 2019-08-14 stsp while (i > 0) {
4801 7ef28ff8 2019-08-14 stsp i /= 10;
4802 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4803 7ef28ff8 2019-08-14 stsp }
4804 82f6abb8 2019-08-14 stsp bca.repo = repo;
4805 7ef28ff8 2019-08-14 stsp
4806 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4807 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4808 404c43c4 2018-06-21 stsp done:
4809 66bea077 2018-08-02 stsp free(in_repo_path);
4810 0587e10c 2020-07-23 stsp free(link_target);
4811 66bea077 2018-08-02 stsp free(repo_path);
4812 66bea077 2018-08-02 stsp free(cwd);
4813 404c43c4 2018-06-21 stsp free(commit_id);
4814 28315671 2019-08-14 stsp free(obj_id);
4815 28315671 2019-08-14 stsp if (blob)
4816 28315671 2019-08-14 stsp got_object_blob_close(blob);
4817 0c06baac 2019-02-05 stsp if (worktree)
4818 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4819 ad242220 2018-09-08 stsp if (repo) {
4820 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4821 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4822 ad242220 2018-09-08 stsp if (error == NULL)
4823 ad242220 2018-09-08 stsp error = repo_error;
4824 ad242220 2018-09-08 stsp }
4825 3affba96 2019-09-22 stsp if (bca.lines) {
4826 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4827 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4828 3affba96 2019-09-22 stsp free(bline->id_str);
4829 3affba96 2019-09-22 stsp free(bline->committer);
4830 3affba96 2019-09-22 stsp }
4831 3affba96 2019-09-22 stsp free(bca.lines);
4832 28315671 2019-08-14 stsp }
4833 28315671 2019-08-14 stsp free(bca.line_offsets);
4834 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4835 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4836 404c43c4 2018-06-21 stsp return error;
4837 5de5890b 2018-10-18 stsp }
4838 5de5890b 2018-10-18 stsp
4839 5de5890b 2018-10-18 stsp __dead static void
4840 5de5890b 2018-10-18 stsp usage_tree(void)
4841 5de5890b 2018-10-18 stsp {
4842 c1669e2e 2019-01-09 stsp fprintf(stderr,
4843 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4844 5de5890b 2018-10-18 stsp getprogname());
4845 5de5890b 2018-10-18 stsp exit(1);
4846 5de5890b 2018-10-18 stsp }
4847 5de5890b 2018-10-18 stsp
4848 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4849 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4850 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4851 c1669e2e 2019-01-09 stsp {
4852 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4853 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4854 848d6979 2019-08-12 stsp const char *modestr = "";
4855 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4856 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4857 5de5890b 2018-10-18 stsp
4858 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4859 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4860 c1669e2e 2019-01-09 stsp path++;
4861 c1669e2e 2019-01-09 stsp
4862 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4863 63c5ca5d 2019-08-24 stsp modestr = "$";
4864 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4865 0d6c6ee3 2020-05-20 stsp int i;
4866 0d6c6ee3 2020-05-20 stsp
4867 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4868 0d6c6ee3 2020-05-20 stsp if (err)
4869 0d6c6ee3 2020-05-20 stsp return err;
4870 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4871 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4872 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4873 0d6c6ee3 2020-05-20 stsp }
4874 0d6c6ee3 2020-05-20 stsp
4875 848d6979 2019-08-12 stsp modestr = "@";
4876 0d6c6ee3 2020-05-20 stsp }
4877 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4878 848d6979 2019-08-12 stsp modestr = "/";
4879 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4880 848d6979 2019-08-12 stsp modestr = "*";
4881 848d6979 2019-08-12 stsp
4882 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4883 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4884 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4885 0d6c6ee3 2020-05-20 stsp
4886 0d6c6ee3 2020-05-20 stsp free(link_target);
4887 0d6c6ee3 2020-05-20 stsp return NULL;
4888 c1669e2e 2019-01-09 stsp }
4889 c1669e2e 2019-01-09 stsp
4890 5de5890b 2018-10-18 stsp static const struct got_error *
4891 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4892 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4893 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4894 5de5890b 2018-10-18 stsp {
4895 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4896 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4897 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4898 56e0773d 2019-11-28 stsp int nentries, i;
4899 5de5890b 2018-10-18 stsp
4900 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4901 5de5890b 2018-10-18 stsp if (err)
4902 5de5890b 2018-10-18 stsp goto done;
4903 5de5890b 2018-10-18 stsp
4904 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4905 5de5890b 2018-10-18 stsp if (err)
4906 5de5890b 2018-10-18 stsp goto done;
4907 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4908 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4909 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4910 5de5890b 2018-10-18 stsp char *id = NULL;
4911 84453469 2018-11-11 stsp
4912 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4913 84453469 2018-11-11 stsp break;
4914 84453469 2018-11-11 stsp
4915 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4916 5de5890b 2018-10-18 stsp if (show_ids) {
4917 5de5890b 2018-10-18 stsp char *id_str;
4918 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4919 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4920 5de5890b 2018-10-18 stsp if (err)
4921 5de5890b 2018-10-18 stsp goto done;
4922 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4923 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4924 5de5890b 2018-10-18 stsp free(id_str);
4925 5de5890b 2018-10-18 stsp goto done;
4926 5de5890b 2018-10-18 stsp }
4927 5de5890b 2018-10-18 stsp free(id_str);
4928 5de5890b 2018-10-18 stsp }
4929 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
4930 5de5890b 2018-10-18 stsp free(id);
4931 0d6c6ee3 2020-05-20 stsp if (err)
4932 0d6c6ee3 2020-05-20 stsp goto done;
4933 c1669e2e 2019-01-09 stsp
4934 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4935 c1669e2e 2019-01-09 stsp char *child_path;
4936 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4937 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4938 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4939 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4940 c1669e2e 2019-01-09 stsp goto done;
4941 c1669e2e 2019-01-09 stsp }
4942 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4943 c1669e2e 2019-01-09 stsp root_path, repo);
4944 c1669e2e 2019-01-09 stsp free(child_path);
4945 c1669e2e 2019-01-09 stsp if (err)
4946 c1669e2e 2019-01-09 stsp goto done;
4947 c1669e2e 2019-01-09 stsp }
4948 5de5890b 2018-10-18 stsp }
4949 5de5890b 2018-10-18 stsp done:
4950 5de5890b 2018-10-18 stsp if (tree)
4951 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4952 5de5890b 2018-10-18 stsp free(tree_id);
4953 5de5890b 2018-10-18 stsp return err;
4954 404c43c4 2018-06-21 stsp }
4955 404c43c4 2018-06-21 stsp
4956 5de5890b 2018-10-18 stsp static const struct got_error *
4957 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4958 5de5890b 2018-10-18 stsp {
4959 5de5890b 2018-10-18 stsp const struct got_error *error;
4960 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4961 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4962 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4963 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4964 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4965 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4966 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4967 5de5890b 2018-10-18 stsp int ch;
4968 5de5890b 2018-10-18 stsp
4969 5de5890b 2018-10-18 stsp #ifndef PROFILE
4970 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4971 0f8d269b 2019-01-04 stsp NULL) == -1)
4972 5de5890b 2018-10-18 stsp err(1, "pledge");
4973 5de5890b 2018-10-18 stsp #endif
4974 5de5890b 2018-10-18 stsp
4975 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4976 5de5890b 2018-10-18 stsp switch (ch) {
4977 5de5890b 2018-10-18 stsp case 'c':
4978 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4979 5de5890b 2018-10-18 stsp break;
4980 5de5890b 2018-10-18 stsp case 'r':
4981 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4982 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4983 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4984 9ba1d308 2019-10-21 stsp optarg);
4985 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4986 5de5890b 2018-10-18 stsp break;
4987 5de5890b 2018-10-18 stsp case 'i':
4988 5de5890b 2018-10-18 stsp show_ids = 1;
4989 5de5890b 2018-10-18 stsp break;
4990 c1669e2e 2019-01-09 stsp case 'R':
4991 c1669e2e 2019-01-09 stsp recurse = 1;
4992 c1669e2e 2019-01-09 stsp break;
4993 5de5890b 2018-10-18 stsp default:
4994 2deda0b9 2019-03-07 stsp usage_tree();
4995 5de5890b 2018-10-18 stsp /* NOTREACHED */
4996 5de5890b 2018-10-18 stsp }
4997 5de5890b 2018-10-18 stsp }
4998 5de5890b 2018-10-18 stsp
4999 5de5890b 2018-10-18 stsp argc -= optind;
5000 5de5890b 2018-10-18 stsp argv += optind;
5001 5de5890b 2018-10-18 stsp
5002 5de5890b 2018-10-18 stsp if (argc == 1)
5003 5de5890b 2018-10-18 stsp path = argv[0];
5004 5de5890b 2018-10-18 stsp else if (argc > 1)
5005 5de5890b 2018-10-18 stsp usage_tree();
5006 5de5890b 2018-10-18 stsp else
5007 7a2c19d6 2019-02-05 stsp path = NULL;
5008 7a2c19d6 2019-02-05 stsp
5009 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
5010 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
5011 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5012 9bf7a39b 2019-02-05 stsp goto done;
5013 9bf7a39b 2019-02-05 stsp }
5014 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
5015 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5016 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5017 7a2c19d6 2019-02-05 stsp goto done;
5018 7a2c19d6 2019-02-05 stsp else
5019 7a2c19d6 2019-02-05 stsp error = NULL;
5020 7a2c19d6 2019-02-05 stsp if (worktree) {
5021 7a2c19d6 2019-02-05 stsp repo_path =
5022 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5023 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
5024 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5025 7a2c19d6 2019-02-05 stsp if (error)
5026 7a2c19d6 2019-02-05 stsp goto done;
5027 7a2c19d6 2019-02-05 stsp } else {
5028 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
5029 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
5030 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5031 7a2c19d6 2019-02-05 stsp goto done;
5032 7a2c19d6 2019-02-05 stsp }
5033 5de5890b 2018-10-18 stsp }
5034 5de5890b 2018-10-18 stsp }
5035 5de5890b 2018-10-18 stsp
5036 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5037 5de5890b 2018-10-18 stsp if (error != NULL)
5038 c02c541e 2019-03-29 stsp goto done;
5039 c02c541e 2019-03-29 stsp
5040 4fedbf4c 2020-11-07 stsp if (worktree) {
5041 4fedbf4c 2020-11-07 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
5042 4fedbf4c 2020-11-07 stsp char *p;
5043 5de5890b 2018-10-18 stsp
5044 4fedbf4c 2020-11-07 stsp if (path == NULL)
5045 4fedbf4c 2020-11-07 stsp path = "";
5046 4fedbf4c 2020-11-07 stsp error = got_worktree_resolve_path(&p, worktree, path);
5047 4fedbf4c 2020-11-07 stsp if (error)
5048 4fedbf4c 2020-11-07 stsp goto done;
5049 4fedbf4c 2020-11-07 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
5050 4fedbf4c 2020-11-07 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5051 4fedbf4c 2020-11-07 stsp p) == -1) {
5052 4fedbf4c 2020-11-07 stsp error = got_error_from_errno("asprintf");
5053 9bf7a39b 2019-02-05 stsp free(p);
5054 4fedbf4c 2020-11-07 stsp goto done;
5055 4fedbf4c 2020-11-07 stsp }
5056 4fedbf4c 2020-11-07 stsp free(p);
5057 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5058 4fedbf4c 2020-11-07 stsp if (error)
5059 4fedbf4c 2020-11-07 stsp goto done;
5060 4fedbf4c 2020-11-07 stsp } else {
5061 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5062 4fedbf4c 2020-11-07 stsp if (error)
5063 4fedbf4c 2020-11-07 stsp goto done;
5064 4fedbf4c 2020-11-07 stsp if (path == NULL)
5065 9bf7a39b 2019-02-05 stsp path = "/";
5066 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
5067 9bf7a39b 2019-02-05 stsp if (error != NULL)
5068 9bf7a39b 2019-02-05 stsp goto done;
5069 9bf7a39b 2019-02-05 stsp }
5070 5de5890b 2018-10-18 stsp
5071 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
5072 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
5073 4e0a20a4 2020-03-23 tracey if (worktree)
5074 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
5075 4e0a20a4 2020-03-23 tracey else
5076 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
5077 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
5078 5de5890b 2018-10-18 stsp if (error != NULL)
5079 5de5890b 2018-10-18 stsp goto done;
5080 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5081 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
5082 5de5890b 2018-10-18 stsp if (error != NULL)
5083 5de5890b 2018-10-18 stsp goto done;
5084 5de5890b 2018-10-18 stsp } else {
5085 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5086 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5087 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5088 84de9106 2020-12-26 stsp NULL);
5089 84de9106 2020-12-26 stsp if (error)
5090 84de9106 2020-12-26 stsp goto done;
5091 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5092 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5093 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5094 30837e32 2019-07-25 stsp if (error)
5095 5de5890b 2018-10-18 stsp goto done;
5096 5de5890b 2018-10-18 stsp }
5097 5de5890b 2018-10-18 stsp
5098 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5099 c1669e2e 2019-01-09 stsp in_repo_path, repo);
5100 5de5890b 2018-10-18 stsp done:
5101 5de5890b 2018-10-18 stsp free(in_repo_path);
5102 5de5890b 2018-10-18 stsp free(repo_path);
5103 5de5890b 2018-10-18 stsp free(cwd);
5104 5de5890b 2018-10-18 stsp free(commit_id);
5105 7a2c19d6 2019-02-05 stsp if (worktree)
5106 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
5107 5de5890b 2018-10-18 stsp if (repo) {
5108 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
5109 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
5110 5de5890b 2018-10-18 stsp if (error == NULL)
5111 5de5890b 2018-10-18 stsp error = repo_error;
5112 5de5890b 2018-10-18 stsp }
5113 5de5890b 2018-10-18 stsp return error;
5114 5de5890b 2018-10-18 stsp }
5115 5de5890b 2018-10-18 stsp
5116 6bad629b 2019-02-04 stsp __dead static void
5117 6bad629b 2019-02-04 stsp usage_status(void)
5118 6bad629b 2019-02-04 stsp {
5119 081470ac 2020-08-13 stsp fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
5120 081470ac 2020-08-13 stsp getprogname());
5121 6bad629b 2019-02-04 stsp exit(1);
5122 6bad629b 2019-02-04 stsp }
5123 5c860e29 2018-03-12 stsp
5124 b72f483a 2019-02-05 stsp static const struct got_error *
5125 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
5126 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5127 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5128 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5129 6bad629b 2019-02-04 stsp {
5130 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5131 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
5132 081470ac 2020-08-13 stsp if (arg) {
5133 081470ac 2020-08-13 stsp char *status_codes = arg;
5134 081470ac 2020-08-13 stsp size_t ncodes = strlen(status_codes);
5135 081470ac 2020-08-13 stsp int i;
5136 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
5137 081470ac 2020-08-13 stsp if (status == status_codes[i] ||
5138 081470ac 2020-08-13 stsp staged_status == status_codes[i])
5139 081470ac 2020-08-13 stsp break;
5140 081470ac 2020-08-13 stsp }
5141 081470ac 2020-08-13 stsp if (i == ncodes)
5142 081470ac 2020-08-13 stsp return NULL;
5143 081470ac 2020-08-13 stsp }
5144 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
5145 b72f483a 2019-02-05 stsp return NULL;
5146 6bad629b 2019-02-04 stsp }
5147 5c860e29 2018-03-12 stsp
5148 6bad629b 2019-02-04 stsp static const struct got_error *
5149 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
5150 6bad629b 2019-02-04 stsp {
5151 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
5152 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
5153 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
5154 081470ac 2020-08-13 stsp char *cwd = NULL, *status_codes = NULL;;
5155 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
5156 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
5157 081470ac 2020-08-13 stsp int ch, i;
5158 5c860e29 2018-03-12 stsp
5159 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
5160 72ea6654 2019-07-27 stsp
5161 081470ac 2020-08-13 stsp while ((ch = getopt(argc, argv, "s:")) != -1) {
5162 6bad629b 2019-02-04 stsp switch (ch) {
5163 081470ac 2020-08-13 stsp case 's':
5164 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
5165 081470ac 2020-08-13 stsp switch (optarg[i]) {
5166 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
5167 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
5168 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
5169 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
5170 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
5171 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
5172 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
5173 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
5174 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
5175 081470ac 2020-08-13 stsp break;
5176 081470ac 2020-08-13 stsp default:
5177 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
5178 081470ac 2020-08-13 stsp optarg[i]);
5179 081470ac 2020-08-13 stsp }
5180 081470ac 2020-08-13 stsp }
5181 081470ac 2020-08-13 stsp status_codes = optarg;
5182 081470ac 2020-08-13 stsp break;
5183 5c860e29 2018-03-12 stsp default:
5184 2deda0b9 2019-03-07 stsp usage_status();
5185 6bad629b 2019-02-04 stsp /* NOTREACHED */
5186 5c860e29 2018-03-12 stsp }
5187 5c860e29 2018-03-12 stsp }
5188 5c860e29 2018-03-12 stsp
5189 6bad629b 2019-02-04 stsp argc -= optind;
5190 6bad629b 2019-02-04 stsp argv += optind;
5191 5c860e29 2018-03-12 stsp
5192 6bad629b 2019-02-04 stsp #ifndef PROFILE
5193 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5194 6bad629b 2019-02-04 stsp NULL) == -1)
5195 6bad629b 2019-02-04 stsp err(1, "pledge");
5196 f42b1b34 2018-03-12 stsp #endif
5197 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
5198 927df6b7 2019-02-10 stsp if (cwd == NULL) {
5199 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5200 927df6b7 2019-02-10 stsp goto done;
5201 927df6b7 2019-02-10 stsp }
5202 927df6b7 2019-02-10 stsp
5203 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
5204 fa51e947 2020-03-27 stsp if (error) {
5205 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5206 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
5207 a5edda0a 2019-07-27 stsp goto done;
5208 fa51e947 2020-03-27 stsp }
5209 6bad629b 2019-02-04 stsp
5210 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5211 c9956ddf 2019-09-08 stsp NULL);
5212 6bad629b 2019-02-04 stsp if (error != NULL)
5213 6bad629b 2019-02-04 stsp goto done;
5214 6bad629b 2019-02-04 stsp
5215 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5216 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5217 087fb88c 2019-08-04 stsp if (error)
5218 087fb88c 2019-08-04 stsp goto done;
5219 087fb88c 2019-08-04 stsp
5220 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5221 6bad629b 2019-02-04 stsp if (error)
5222 6bad629b 2019-02-04 stsp goto done;
5223 6bad629b 2019-02-04 stsp
5224 081470ac 2020-08-13 stsp error = got_worktree_status(worktree, &paths, repo, print_status,
5225 081470ac 2020-08-13 stsp status_codes, check_cancelled, NULL);
5226 6bad629b 2019-02-04 stsp done:
5227 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
5228 a5edda0a 2019-07-27 stsp free((char *)pe->path);
5229 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
5230 927df6b7 2019-02-10 stsp free(cwd);
5231 d0eebce4 2019-03-11 stsp return error;
5232 d0eebce4 2019-03-11 stsp }
5233 d0eebce4 2019-03-11 stsp
5234 d0eebce4 2019-03-11 stsp __dead static void
5235 d0eebce4 2019-03-11 stsp usage_ref(void)
5236 d0eebce4 2019-03-11 stsp {
5237 d0eebce4 2019-03-11 stsp fprintf(stderr,
5238 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5239 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
5240 d0eebce4 2019-03-11 stsp getprogname());
5241 d0eebce4 2019-03-11 stsp exit(1);
5242 d0eebce4 2019-03-11 stsp }
5243 d0eebce4 2019-03-11 stsp
5244 d0eebce4 2019-03-11 stsp static const struct got_error *
5245 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
5246 d0eebce4 2019-03-11 stsp {
5247 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
5248 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
5249 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
5250 d0eebce4 2019-03-11 stsp
5251 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5252 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5253 d0eebce4 2019-03-11 stsp if (err)
5254 d0eebce4 2019-03-11 stsp return err;
5255 d0eebce4 2019-03-11 stsp
5256 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
5257 d0eebce4 2019-03-11 stsp char *refstr;
5258 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
5259 d0eebce4 2019-03-11 stsp if (refstr == NULL)
5260 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
5261 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5262 d0eebce4 2019-03-11 stsp free(refstr);
5263 d0eebce4 2019-03-11 stsp }
5264 d0eebce4 2019-03-11 stsp
5265 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5266 d0eebce4 2019-03-11 stsp return NULL;
5267 d0eebce4 2019-03-11 stsp }
5268 d0eebce4 2019-03-11 stsp
5269 d0eebce4 2019-03-11 stsp static const struct got_error *
5270 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
5271 d0eebce4 2019-03-11 stsp {
5272 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5273 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5274 d0eebce4 2019-03-11 stsp
5275 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5276 d0eebce4 2019-03-11 stsp if (err)
5277 d0eebce4 2019-03-11 stsp return err;
5278 d0eebce4 2019-03-11 stsp
5279 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
5280 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5281 d0eebce4 2019-03-11 stsp return err;
5282 d0eebce4 2019-03-11 stsp }
5283 d0eebce4 2019-03-11 stsp
5284 d0eebce4 2019-03-11 stsp static const struct got_error *
5285 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
5286 d0eebce4 2019-03-11 stsp {
5287 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5288 d0eebce4 2019-03-11 stsp struct got_object_id *id;
5289 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
5290 d1644381 2019-07-14 stsp
5291 d1644381 2019-07-14 stsp /*
5292 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5293 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
5294 d1644381 2019-07-14 stsp * an unintended typo.
5295 d1644381 2019-07-14 stsp */
5296 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5297 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5298 d0eebce4 2019-03-11 stsp
5299 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5300 dd88155e 2019-06-29 stsp repo);
5301 d83d9d5c 2019-05-13 stsp if (err) {
5302 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
5303 d0eebce4 2019-03-11 stsp
5304 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5305 d83d9d5c 2019-05-13 stsp return err;
5306 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
5307 d83d9d5c 2019-05-13 stsp if (err)
5308 d83d9d5c 2019-05-13 stsp return err;
5309 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
5310 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
5311 d83d9d5c 2019-05-13 stsp if (err)
5312 d83d9d5c 2019-05-13 stsp return err;
5313 d83d9d5c 2019-05-13 stsp }
5314 d83d9d5c 2019-05-13 stsp
5315 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5316 d0eebce4 2019-03-11 stsp if (err)
5317 d0eebce4 2019-03-11 stsp goto done;
5318 d0eebce4 2019-03-11 stsp
5319 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5320 d0eebce4 2019-03-11 stsp done:
5321 d0eebce4 2019-03-11 stsp if (ref)
5322 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5323 d0eebce4 2019-03-11 stsp free(id);
5324 d1c1ae5f 2019-08-12 stsp return err;
5325 d1c1ae5f 2019-08-12 stsp }
5326 d1c1ae5f 2019-08-12 stsp
5327 d1c1ae5f 2019-08-12 stsp static const struct got_error *
5328 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
5329 d1c1ae5f 2019-08-12 stsp {
5330 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
5331 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
5332 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
5333 d1c1ae5f 2019-08-12 stsp
5334 d1c1ae5f 2019-08-12 stsp /*
5335 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5336 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
5337 d1c1ae5f 2019-08-12 stsp * an unintended typo.
5338 d1c1ae5f 2019-08-12 stsp */
5339 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5340 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5341 d1c1ae5f 2019-08-12 stsp
5342 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5343 d1c1ae5f 2019-08-12 stsp if (err)
5344 d1c1ae5f 2019-08-12 stsp return err;
5345 d1c1ae5f 2019-08-12 stsp
5346 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5347 d1c1ae5f 2019-08-12 stsp if (err)
5348 d1c1ae5f 2019-08-12 stsp goto done;
5349 d1c1ae5f 2019-08-12 stsp
5350 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5351 d1c1ae5f 2019-08-12 stsp done:
5352 d1c1ae5f 2019-08-12 stsp if (target_ref)
5353 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5354 d1c1ae5f 2019-08-12 stsp if (ref)
5355 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5356 d0eebce4 2019-03-11 stsp return err;
5357 d0eebce4 2019-03-11 stsp }
5358 d0eebce4 2019-03-11 stsp
5359 d0eebce4 2019-03-11 stsp static const struct got_error *
5360 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5361 d0eebce4 2019-03-11 stsp {
5362 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
5363 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
5364 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
5365 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
5366 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
5367 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
5368 b2070a3f 2020-03-22 stsp char *refname = NULL;
5369 d0eebce4 2019-03-11 stsp
5370 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5371 d0eebce4 2019-03-11 stsp switch (ch) {
5372 e31abbf2 2020-03-22 stsp case 'c':
5373 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5374 e31abbf2 2020-03-22 stsp break;
5375 d0eebce4 2019-03-11 stsp case 'd':
5376 e31abbf2 2020-03-22 stsp do_delete = 1;
5377 d0eebce4 2019-03-11 stsp break;
5378 d0eebce4 2019-03-11 stsp case 'r':
5379 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5380 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5381 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5382 9ba1d308 2019-10-21 stsp optarg);
5383 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5384 d0eebce4 2019-03-11 stsp break;
5385 d0eebce4 2019-03-11 stsp case 'l':
5386 d0eebce4 2019-03-11 stsp do_list = 1;
5387 d1c1ae5f 2019-08-12 stsp break;
5388 d1c1ae5f 2019-08-12 stsp case 's':
5389 e31abbf2 2020-03-22 stsp symref_target = optarg;
5390 d0eebce4 2019-03-11 stsp break;
5391 d0eebce4 2019-03-11 stsp default:
5392 d0eebce4 2019-03-11 stsp usage_ref();
5393 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5394 d0eebce4 2019-03-11 stsp }
5395 d0eebce4 2019-03-11 stsp }
5396 d0eebce4 2019-03-11 stsp
5397 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5398 ff69268e 2020-12-13 stsp option_conflict('c', 'l');
5399 ff69268e 2020-12-13 stsp if (obj_arg && do_delete)
5400 ff69268e 2020-12-13 stsp option_conflict('c', 'd');
5401 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5402 ff69268e 2020-12-13 stsp option_conflict('c', 's');
5403 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5404 ff69268e 2020-12-13 stsp option_conflict('s', 'd');
5405 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5406 ff69268e 2020-12-13 stsp option_conflict('s', 'l');
5407 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5408 ff69268e 2020-12-13 stsp option_conflict('d', 'l');
5409 d0eebce4 2019-03-11 stsp
5410 d0eebce4 2019-03-11 stsp argc -= optind;
5411 d0eebce4 2019-03-11 stsp argv += optind;
5412 d0eebce4 2019-03-11 stsp
5413 e31abbf2 2020-03-22 stsp if (do_list) {
5414 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5415 d0eebce4 2019-03-11 stsp usage_ref();
5416 b2070a3f 2020-03-22 stsp if (argc == 1) {
5417 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5418 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5419 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5420 b2070a3f 2020-03-22 stsp goto done;
5421 b2070a3f 2020-03-22 stsp }
5422 b2070a3f 2020-03-22 stsp }
5423 e31abbf2 2020-03-22 stsp } else {
5424 e31abbf2 2020-03-22 stsp if (argc != 1)
5425 e31abbf2 2020-03-22 stsp usage_ref();
5426 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5427 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5428 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5429 b2070a3f 2020-03-22 stsp goto done;
5430 b2070a3f 2020-03-22 stsp }
5431 e31abbf2 2020-03-22 stsp }
5432 b2070a3f 2020-03-22 stsp
5433 b2070a3f 2020-03-22 stsp if (refname)
5434 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5435 d0eebce4 2019-03-11 stsp
5436 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5437 e0b57350 2019-03-12 stsp if (do_list) {
5438 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5439 e0b57350 2019-03-12 stsp NULL) == -1)
5440 e0b57350 2019-03-12 stsp err(1, "pledge");
5441 e0b57350 2019-03-12 stsp } else {
5442 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5443 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5444 e0b57350 2019-03-12 stsp err(1, "pledge");
5445 e0b57350 2019-03-12 stsp }
5446 d0eebce4 2019-03-11 stsp #endif
5447 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5448 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5449 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5450 d0eebce4 2019-03-11 stsp goto done;
5451 d0eebce4 2019-03-11 stsp }
5452 d0eebce4 2019-03-11 stsp
5453 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5454 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5455 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5456 d0eebce4 2019-03-11 stsp goto done;
5457 d0eebce4 2019-03-11 stsp else
5458 d0eebce4 2019-03-11 stsp error = NULL;
5459 d0eebce4 2019-03-11 stsp if (worktree) {
5460 d0eebce4 2019-03-11 stsp repo_path =
5461 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5462 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5463 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5464 d0eebce4 2019-03-11 stsp if (error)
5465 d0eebce4 2019-03-11 stsp goto done;
5466 d0eebce4 2019-03-11 stsp } else {
5467 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5468 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5469 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5470 d0eebce4 2019-03-11 stsp goto done;
5471 d0eebce4 2019-03-11 stsp }
5472 d0eebce4 2019-03-11 stsp }
5473 d0eebce4 2019-03-11 stsp }
5474 d0eebce4 2019-03-11 stsp
5475 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5476 d0eebce4 2019-03-11 stsp if (error != NULL)
5477 d0eebce4 2019-03-11 stsp goto done;
5478 d0eebce4 2019-03-11 stsp
5479 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5480 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5481 c02c541e 2019-03-29 stsp if (error)
5482 c02c541e 2019-03-29 stsp goto done;
5483 c02c541e 2019-03-29 stsp
5484 d0eebce4 2019-03-11 stsp if (do_list)
5485 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5486 e31abbf2 2020-03-22 stsp else if (do_delete)
5487 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
5488 e31abbf2 2020-03-22 stsp else if (symref_target)
5489 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5490 e31abbf2 2020-03-22 stsp else {
5491 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5492 e31abbf2 2020-03-22 stsp usage_ref();
5493 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5494 e31abbf2 2020-03-22 stsp }
5495 4e759de4 2019-06-26 stsp done:
5496 b2070a3f 2020-03-22 stsp free(refname);
5497 4e759de4 2019-06-26 stsp if (repo)
5498 4e759de4 2019-06-26 stsp got_repo_close(repo);
5499 4e759de4 2019-06-26 stsp if (worktree)
5500 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5501 4e759de4 2019-06-26 stsp free(cwd);
5502 4e759de4 2019-06-26 stsp free(repo_path);
5503 4e759de4 2019-06-26 stsp return error;
5504 4e759de4 2019-06-26 stsp }
5505 4e759de4 2019-06-26 stsp
5506 4e759de4 2019-06-26 stsp __dead static void
5507 4e759de4 2019-06-26 stsp usage_branch(void)
5508 4e759de4 2019-06-26 stsp {
5509 4e759de4 2019-06-26 stsp fprintf(stderr,
5510 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5511 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5512 4e759de4 2019-06-26 stsp exit(1);
5513 4e759de4 2019-06-26 stsp }
5514 4e759de4 2019-06-26 stsp
5515 4e759de4 2019-06-26 stsp static const struct got_error *
5516 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5517 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5518 4e99b47e 2019-10-04 stsp {
5519 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5520 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5521 4e99b47e 2019-10-04 stsp char *refstr;
5522 4e99b47e 2019-10-04 stsp
5523 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5524 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5525 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5526 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5527 4e99b47e 2019-10-04 stsp
5528 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5529 4e99b47e 2019-10-04 stsp if (err)
5530 4e99b47e 2019-10-04 stsp return err;
5531 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5532 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5533 4e99b47e 2019-10-04 stsp marker = "* ";
5534 4e99b47e 2019-10-04 stsp else
5535 4e99b47e 2019-10-04 stsp marker = "~ ";
5536 4e99b47e 2019-10-04 stsp free(id);
5537 4e99b47e 2019-10-04 stsp }
5538 4e99b47e 2019-10-04 stsp
5539 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5540 4e99b47e 2019-10-04 stsp refname += 11;
5541 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5542 4e99b47e 2019-10-04 stsp refname += 18;
5543 4e99b47e 2019-10-04 stsp
5544 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5545 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5546 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5547 4e99b47e 2019-10-04 stsp
5548 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5549 4e99b47e 2019-10-04 stsp free(refstr);
5550 4e99b47e 2019-10-04 stsp return NULL;
5551 4e99b47e 2019-10-04 stsp }
5552 4e99b47e 2019-10-04 stsp
5553 4e99b47e 2019-10-04 stsp static const struct got_error *
5554 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5555 ad89fa31 2019-10-04 stsp {
5556 ad89fa31 2019-10-04 stsp const char *refname;
5557 ad89fa31 2019-10-04 stsp
5558 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5559 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5560 ad89fa31 2019-10-04 stsp
5561 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5562 ad89fa31 2019-10-04 stsp
5563 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5564 ad89fa31 2019-10-04 stsp refname += 11;
5565 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5566 ad89fa31 2019-10-04 stsp refname += 18;
5567 ad89fa31 2019-10-04 stsp
5568 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5569 ad89fa31 2019-10-04 stsp
5570 ad89fa31 2019-10-04 stsp return NULL;
5571 ad89fa31 2019-10-04 stsp }
5572 ad89fa31 2019-10-04 stsp
5573 ad89fa31 2019-10-04 stsp static const struct got_error *
5574 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5575 4e759de4 2019-06-26 stsp {
5576 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5577 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5578 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5579 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5580 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5581 4e759de4 2019-06-26 stsp
5582 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5583 ba882ee3 2019-07-11 stsp
5584 4e99b47e 2019-10-04 stsp if (worktree) {
5585 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5586 4e99b47e 2019-10-04 stsp worktree);
5587 4e99b47e 2019-10-04 stsp if (err)
5588 4e99b47e 2019-10-04 stsp return err;
5589 4e99b47e 2019-10-04 stsp
5590 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5591 4e99b47e 2019-10-04 stsp worktree);
5592 4e99b47e 2019-10-04 stsp if (err)
5593 4e99b47e 2019-10-04 stsp return err;
5594 4e99b47e 2019-10-04 stsp
5595 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5596 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5597 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5598 4e99b47e 2019-10-04 stsp if (err)
5599 4e99b47e 2019-10-04 stsp return err;
5600 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5601 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5602 4e99b47e 2019-10-04 stsp }
5603 4e99b47e 2019-10-04 stsp }
5604 4e99b47e 2019-10-04 stsp
5605 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5606 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5607 4e759de4 2019-06-26 stsp if (err)
5608 4e759de4 2019-06-26 stsp return err;
5609 4e759de4 2019-06-26 stsp
5610 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry)
5611 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5612 4e759de4 2019-06-26 stsp
5613 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5614 4e759de4 2019-06-26 stsp return NULL;
5615 4e759de4 2019-06-26 stsp }
5616 4e759de4 2019-06-26 stsp
5617 4e759de4 2019-06-26 stsp static const struct got_error *
5618 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5619 45cd4e47 2019-08-25 stsp const char *branch_name)
5620 4e759de4 2019-06-26 stsp {
5621 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5622 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5623 4e759de4 2019-06-26 stsp char *refname;
5624 4e759de4 2019-06-26 stsp
5625 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5626 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5627 4e759de4 2019-06-26 stsp
5628 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5629 4e759de4 2019-06-26 stsp if (err)
5630 4e759de4 2019-06-26 stsp goto done;
5631 4e759de4 2019-06-26 stsp
5632 45cd4e47 2019-08-25 stsp if (worktree &&
5633 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5634 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5635 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5636 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5637 45cd4e47 2019-08-25 stsp goto done;
5638 45cd4e47 2019-08-25 stsp }
5639 45cd4e47 2019-08-25 stsp
5640 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
5641 4e759de4 2019-06-26 stsp done:
5642 45cd4e47 2019-08-25 stsp if (ref)
5643 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5644 4e759de4 2019-06-26 stsp free(refname);
5645 4e759de4 2019-06-26 stsp return err;
5646 4e759de4 2019-06-26 stsp }
5647 4e759de4 2019-06-26 stsp
5648 4e759de4 2019-06-26 stsp static const struct got_error *
5649 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5650 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5651 4e759de4 2019-06-26 stsp {
5652 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5653 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5654 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5655 d3f84d51 2019-07-11 stsp
5656 d3f84d51 2019-07-11 stsp /*
5657 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5658 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5659 d3f84d51 2019-07-11 stsp * an unintended typo.
5660 d3f84d51 2019-07-11 stsp */
5661 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5662 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5663 4e759de4 2019-06-26 stsp
5664 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5665 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
5666 62d463ca 2020-10-20 naddy goto done;
5667 4e759de4 2019-06-26 stsp }
5668 4e759de4 2019-06-26 stsp
5669 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5670 4e759de4 2019-06-26 stsp if (err == NULL) {
5671 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5672 4e759de4 2019-06-26 stsp goto done;
5673 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5674 4e759de4 2019-06-26 stsp goto done;
5675 4e759de4 2019-06-26 stsp
5676 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5677 4e759de4 2019-06-26 stsp if (err)
5678 4e759de4 2019-06-26 stsp goto done;
5679 4e759de4 2019-06-26 stsp
5680 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5681 d0eebce4 2019-03-11 stsp done:
5682 4e759de4 2019-06-26 stsp if (ref)
5683 4e759de4 2019-06-26 stsp got_ref_close(ref);
5684 4e759de4 2019-06-26 stsp free(base_refname);
5685 4e759de4 2019-06-26 stsp free(refname);
5686 4e759de4 2019-06-26 stsp return err;
5687 4e759de4 2019-06-26 stsp }
5688 4e759de4 2019-06-26 stsp
5689 4e759de4 2019-06-26 stsp static const struct got_error *
5690 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5691 4e759de4 2019-06-26 stsp {
5692 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5693 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5694 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5695 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5696 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5697 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5698 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5699 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5700 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5701 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5702 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5703 4e759de4 2019-06-26 stsp
5704 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5705 da76fce2 2020-02-24 stsp
5706 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5707 4e759de4 2019-06-26 stsp switch (ch) {
5708 a74f7e83 2019-11-10 stsp case 'c':
5709 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5710 a74f7e83 2019-11-10 stsp break;
5711 4e759de4 2019-06-26 stsp case 'd':
5712 4e759de4 2019-06-26 stsp delref = optarg;
5713 4e759de4 2019-06-26 stsp break;
5714 4e759de4 2019-06-26 stsp case 'r':
5715 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5716 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5717 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5718 9ba1d308 2019-10-21 stsp optarg);
5719 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5720 4e759de4 2019-06-26 stsp break;
5721 4e759de4 2019-06-26 stsp case 'l':
5722 4e759de4 2019-06-26 stsp do_list = 1;
5723 da76fce2 2020-02-24 stsp break;
5724 da76fce2 2020-02-24 stsp case 'n':
5725 da76fce2 2020-02-24 stsp do_update = 0;
5726 4e759de4 2019-06-26 stsp break;
5727 4e759de4 2019-06-26 stsp default:
5728 4e759de4 2019-06-26 stsp usage_branch();
5729 4e759de4 2019-06-26 stsp /* NOTREACHED */
5730 4e759de4 2019-06-26 stsp }
5731 4e759de4 2019-06-26 stsp }
5732 4e759de4 2019-06-26 stsp
5733 4e759de4 2019-06-26 stsp if (do_list && delref)
5734 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
5735 4e759de4 2019-06-26 stsp
5736 4e759de4 2019-06-26 stsp argc -= optind;
5737 4e759de4 2019-06-26 stsp argv += optind;
5738 ad89fa31 2019-10-04 stsp
5739 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5740 ad89fa31 2019-10-04 stsp do_show = 1;
5741 4e759de4 2019-06-26 stsp
5742 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5743 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5744 a74f7e83 2019-11-10 stsp
5745 4e759de4 2019-06-26 stsp if (do_list || delref) {
5746 4e759de4 2019-06-26 stsp if (argc > 0)
5747 4e759de4 2019-06-26 stsp usage_branch();
5748 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5749 4e759de4 2019-06-26 stsp usage_branch();
5750 4e759de4 2019-06-26 stsp
5751 4e759de4 2019-06-26 stsp #ifndef PROFILE
5752 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5753 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5754 4e759de4 2019-06-26 stsp NULL) == -1)
5755 4e759de4 2019-06-26 stsp err(1, "pledge");
5756 4e759de4 2019-06-26 stsp } else {
5757 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5758 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5759 4e759de4 2019-06-26 stsp err(1, "pledge");
5760 4e759de4 2019-06-26 stsp }
5761 4e759de4 2019-06-26 stsp #endif
5762 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5763 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5764 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5765 4e759de4 2019-06-26 stsp goto done;
5766 4e759de4 2019-06-26 stsp }
5767 4e759de4 2019-06-26 stsp
5768 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5769 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5770 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5771 4e759de4 2019-06-26 stsp goto done;
5772 4e759de4 2019-06-26 stsp else
5773 4e759de4 2019-06-26 stsp error = NULL;
5774 4e759de4 2019-06-26 stsp if (worktree) {
5775 4e759de4 2019-06-26 stsp repo_path =
5776 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5777 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5778 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5779 4e759de4 2019-06-26 stsp if (error)
5780 4e759de4 2019-06-26 stsp goto done;
5781 4e759de4 2019-06-26 stsp } else {
5782 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5783 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5784 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5785 4e759de4 2019-06-26 stsp goto done;
5786 4e759de4 2019-06-26 stsp }
5787 4e759de4 2019-06-26 stsp }
5788 4e759de4 2019-06-26 stsp }
5789 4e759de4 2019-06-26 stsp
5790 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5791 4e759de4 2019-06-26 stsp if (error != NULL)
5792 4e759de4 2019-06-26 stsp goto done;
5793 4e759de4 2019-06-26 stsp
5794 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5795 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5796 4e759de4 2019-06-26 stsp if (error)
5797 4e759de4 2019-06-26 stsp goto done;
5798 4e759de4 2019-06-26 stsp
5799 ad89fa31 2019-10-04 stsp if (do_show)
5800 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5801 ad89fa31 2019-10-04 stsp else if (do_list)
5802 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5803 4e759de4 2019-06-26 stsp else if (delref)
5804 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5805 4e759de4 2019-06-26 stsp else {
5806 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5807 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5808 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5809 84de9106 2020-12-26 stsp NULL);
5810 84de9106 2020-12-26 stsp if (error)
5811 84de9106 2020-12-26 stsp goto done;
5812 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5813 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5814 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5815 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5816 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5817 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5818 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5819 a74f7e83 2019-11-10 stsp if (error)
5820 a74f7e83 2019-11-10 stsp goto done;
5821 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5822 da76fce2 2020-02-24 stsp if (error)
5823 da76fce2 2020-02-24 stsp goto done;
5824 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5825 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5826 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5827 da76fce2 2020-02-24 stsp
5828 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5829 da76fce2 2020-02-24 stsp if (error)
5830 da76fce2 2020-02-24 stsp goto done;
5831 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5832 da76fce2 2020-02-24 stsp worktree);
5833 da76fce2 2020-02-24 stsp if (error)
5834 da76fce2 2020-02-24 stsp goto done;
5835 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5836 da76fce2 2020-02-24 stsp == -1) {
5837 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5838 da76fce2 2020-02-24 stsp goto done;
5839 da76fce2 2020-02-24 stsp }
5840 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5841 da76fce2 2020-02-24 stsp free(branch_refname);
5842 da76fce2 2020-02-24 stsp if (error)
5843 da76fce2 2020-02-24 stsp goto done;
5844 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5845 da76fce2 2020-02-24 stsp repo);
5846 da76fce2 2020-02-24 stsp if (error)
5847 da76fce2 2020-02-24 stsp goto done;
5848 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5849 da76fce2 2020-02-24 stsp commit_id);
5850 da76fce2 2020-02-24 stsp if (error)
5851 da76fce2 2020-02-24 stsp goto done;
5852 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5853 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5854 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5855 9627c110 2020-04-18 stsp NULL);
5856 da76fce2 2020-02-24 stsp if (error)
5857 da76fce2 2020-02-24 stsp goto done;
5858 9627c110 2020-04-18 stsp if (upa.did_something)
5859 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5860 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
5861 da76fce2 2020-02-24 stsp }
5862 4e759de4 2019-06-26 stsp }
5863 4e759de4 2019-06-26 stsp done:
5864 da76fce2 2020-02-24 stsp if (ref)
5865 da76fce2 2020-02-24 stsp got_ref_close(ref);
5866 d0eebce4 2019-03-11 stsp if (repo)
5867 d0eebce4 2019-03-11 stsp got_repo_close(repo);
5868 d0eebce4 2019-03-11 stsp if (worktree)
5869 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
5870 d0eebce4 2019-03-11 stsp free(cwd);
5871 d0eebce4 2019-03-11 stsp free(repo_path);
5872 da76fce2 2020-02-24 stsp free(commit_id);
5873 da76fce2 2020-02-24 stsp free(commit_id_str);
5874 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
5875 da76fce2 2020-02-24 stsp free((char *)pe->path);
5876 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
5877 d00136be 2019-03-26 stsp return error;
5878 d00136be 2019-03-26 stsp }
5879 d00136be 2019-03-26 stsp
5880 8e7bd50a 2019-08-22 stsp
5881 d00136be 2019-03-26 stsp __dead static void
5882 8e7bd50a 2019-08-22 stsp usage_tag(void)
5883 8e7bd50a 2019-08-22 stsp {
5884 8e7bd50a 2019-08-22 stsp fprintf(stderr,
5885 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5886 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
5887 8e7bd50a 2019-08-22 stsp exit(1);
5888 b8bad2ba 2019-08-23 stsp }
5889 b8bad2ba 2019-08-23 stsp
5890 b8bad2ba 2019-08-23 stsp #if 0
5891 b8bad2ba 2019-08-23 stsp static const struct got_error *
5892 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5893 b8bad2ba 2019-08-23 stsp {
5894 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
5895 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
5896 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
5897 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
5898 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
5899 b8bad2ba 2019-08-23 stsp
5900 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5901 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
5902 b8bad2ba 2019-08-23 stsp if (se == NULL) {
5903 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5904 b8bad2ba 2019-08-23 stsp if (err)
5905 b8bad2ba 2019-08-23 stsp return err;
5906 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5907 b8bad2ba 2019-08-23 stsp continue;
5908 b8bad2ba 2019-08-23 stsp } else {
5909 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5910 b8bad2ba 2019-08-23 stsp if (err)
5911 b8bad2ba 2019-08-23 stsp break;
5912 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5913 b8bad2ba 2019-08-23 stsp free(re_id);
5914 b8bad2ba 2019-08-23 stsp if (err)
5915 b8bad2ba 2019-08-23 stsp break;
5916 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5917 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
5918 b8bad2ba 2019-08-23 stsp }
5919 b8bad2ba 2019-08-23 stsp
5920 b8bad2ba 2019-08-23 stsp while (se) {
5921 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5922 b8bad2ba 2019-08-23 stsp if (err)
5923 b8bad2ba 2019-08-23 stsp break;
5924 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5925 b8bad2ba 2019-08-23 stsp free(se_id);
5926 b8bad2ba 2019-08-23 stsp if (err)
5927 b8bad2ba 2019-08-23 stsp break;
5928 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5929 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
5930 b8bad2ba 2019-08-23 stsp
5931 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
5932 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5933 b8bad2ba 2019-08-23 stsp if (err)
5934 b8bad2ba 2019-08-23 stsp return err;
5935 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5936 b8bad2ba 2019-08-23 stsp break;
5937 b8bad2ba 2019-08-23 stsp }
5938 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
5939 b8bad2ba 2019-08-23 stsp continue;
5940 b8bad2ba 2019-08-23 stsp }
5941 b8bad2ba 2019-08-23 stsp }
5942 b8bad2ba 2019-08-23 stsp done:
5943 b8bad2ba 2019-08-23 stsp return err;
5944 8e7bd50a 2019-08-22 stsp }
5945 b8bad2ba 2019-08-23 stsp #endif
5946 b8bad2ba 2019-08-23 stsp
5947 b8bad2ba 2019-08-23 stsp static const struct got_error *
5948 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5949 8e7bd50a 2019-08-22 stsp {
5950 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5951 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5952 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
5953 8e7bd50a 2019-08-22 stsp
5954 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5955 8e7bd50a 2019-08-22 stsp
5956 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5957 8e7bd50a 2019-08-22 stsp if (err)
5958 8e7bd50a 2019-08-22 stsp return err;
5959 8e7bd50a 2019-08-22 stsp
5960 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
5961 8e7bd50a 2019-08-22 stsp const char *refname;
5962 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5963 8e7bd50a 2019-08-22 stsp char datebuf[26];
5964 d4efa91b 2020-01-14 stsp const char *tagger;
5965 8e7bd50a 2019-08-22 stsp time_t tagger_time;
5966 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
5967 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
5968 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
5969 8e7bd50a 2019-08-22 stsp
5970 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
5971 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5972 8e7bd50a 2019-08-22 stsp continue;
5973 8e7bd50a 2019-08-22 stsp refname += 10;
5974 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
5975 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
5976 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
5977 8e7bd50a 2019-08-22 stsp break;
5978 8e7bd50a 2019-08-22 stsp }
5979 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5980 8e7bd50a 2019-08-22 stsp free(refstr);
5981 8e7bd50a 2019-08-22 stsp
5982 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
5983 8e7bd50a 2019-08-22 stsp if (err)
5984 8e7bd50a 2019-08-22 stsp break;
5985 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
5986 d4efa91b 2020-01-14 stsp if (err) {
5987 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5988 d4efa91b 2020-01-14 stsp free(id);
5989 d4efa91b 2020-01-14 stsp break;
5990 d4efa91b 2020-01-14 stsp }
5991 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
5992 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
5993 d4efa91b 2020-01-14 stsp if (err) {
5994 d4efa91b 2020-01-14 stsp free(id);
5995 d4efa91b 2020-01-14 stsp break;
5996 d4efa91b 2020-01-14 stsp }
5997 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
5998 d4efa91b 2020-01-14 stsp tagger_time =
5999 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
6000 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
6001 d4efa91b 2020-01-14 stsp free(id);
6002 d4efa91b 2020-01-14 stsp if (err)
6003 d4efa91b 2020-01-14 stsp break;
6004 d4efa91b 2020-01-14 stsp } else {
6005 d4efa91b 2020-01-14 stsp free(id);
6006 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
6007 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
6008 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
6009 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
6010 d4efa91b 2020-01-14 stsp if (err)
6011 d4efa91b 2020-01-14 stsp break;
6012 d4efa91b 2020-01-14 stsp }
6013 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
6014 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
6015 2417344c 2019-08-23 stsp if (datestr)
6016 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
6017 d4efa91b 2020-01-14 stsp if (commit)
6018 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6019 d4efa91b 2020-01-14 stsp else {
6020 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
6021 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
6022 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6023 d4efa91b 2020-01-14 stsp id_str);
6024 d4efa91b 2020-01-14 stsp break;
6025 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
6026 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6027 d4efa91b 2020-01-14 stsp id_str);
6028 d4efa91b 2020-01-14 stsp break;
6029 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
6030 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6031 d4efa91b 2020-01-14 stsp id_str);
6032 d4efa91b 2020-01-14 stsp break;
6033 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
6034 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6035 d4efa91b 2020-01-14 stsp id_str);
6036 d4efa91b 2020-01-14 stsp break;
6037 d4efa91b 2020-01-14 stsp default:
6038 d4efa91b 2020-01-14 stsp break;
6039 d4efa91b 2020-01-14 stsp }
6040 8e7bd50a 2019-08-22 stsp }
6041 8e7bd50a 2019-08-22 stsp free(id_str);
6042 d4efa91b 2020-01-14 stsp if (commit) {
6043 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
6044 d4efa91b 2020-01-14 stsp if (err)
6045 d4efa91b 2020-01-14 stsp break;
6046 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
6047 d4efa91b 2020-01-14 stsp } else {
6048 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
6049 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
6050 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
6051 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
6052 d4efa91b 2020-01-14 stsp break;
6053 d4efa91b 2020-01-14 stsp }
6054 8e7bd50a 2019-08-22 stsp }
6055 8e7bd50a 2019-08-22 stsp
6056 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
6057 8e7bd50a 2019-08-22 stsp do {
6058 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
6059 8e7bd50a 2019-08-22 stsp if (line)
6060 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
6061 8e7bd50a 2019-08-22 stsp } while (line);
6062 8e7bd50a 2019-08-22 stsp free(tagmsg0);
6063 8e7bd50a 2019-08-22 stsp }
6064 8e7bd50a 2019-08-22 stsp
6065 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
6066 8e7bd50a 2019-08-22 stsp return NULL;
6067 8e7bd50a 2019-08-22 stsp }
6068 8e7bd50a 2019-08-22 stsp
6069 8e7bd50a 2019-08-22 stsp static const struct got_error *
6070 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6071 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
6072 8e7bd50a 2019-08-22 stsp {
6073 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6074 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
6075 f372d5cd 2019-10-21 stsp char *editor = NULL;
6076 1601cb9f 2020-09-11 naddy int initial_content_len;
6077 8e7bd50a 2019-08-22 stsp int fd = -1;
6078 8e7bd50a 2019-08-22 stsp
6079 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6080 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6081 8e7bd50a 2019-08-22 stsp goto done;
6082 8e7bd50a 2019-08-22 stsp }
6083 8e7bd50a 2019-08-22 stsp
6084 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6085 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
6086 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
6087 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
6088 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6089 8e7bd50a 2019-08-22 stsp goto done;
6090 8e7bd50a 2019-08-22 stsp }
6091 8e7bd50a 2019-08-22 stsp
6092 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6093 8e7bd50a 2019-08-22 stsp if (err)
6094 8e7bd50a 2019-08-22 stsp goto done;
6095 8e7bd50a 2019-08-22 stsp
6096 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6097 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
6098 97972933 2020-09-11 stsp goto done;
6099 97972933 2020-09-11 stsp }
6100 8e7bd50a 2019-08-22 stsp
6101 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
6102 8e7bd50a 2019-08-22 stsp if (err)
6103 8e7bd50a 2019-08-22 stsp goto done;
6104 bfa12d5e 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6105 0d5bb276 2020-12-15 stsp initial_content_len, 1);
6106 8e7bd50a 2019-08-22 stsp done:
6107 8e7bd50a 2019-08-22 stsp free(initial_content);
6108 8e7bd50a 2019-08-22 stsp free(template);
6109 8e7bd50a 2019-08-22 stsp free(editor);
6110 8e7bd50a 2019-08-22 stsp
6111 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6112 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
6113 97972933 2020-09-11 stsp
6114 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
6115 59f86c76 2020-09-11 stsp if (err == NULL)
6116 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
6117 59f86c76 2020-09-11 stsp if (err) {
6118 59f86c76 2020-09-11 stsp free(*tagmsg);
6119 59f86c76 2020-09-11 stsp *tagmsg = NULL;
6120 8e7bd50a 2019-08-22 stsp }
6121 8e7bd50a 2019-08-22 stsp return err;
6122 8e7bd50a 2019-08-22 stsp }
6123 8e7bd50a 2019-08-22 stsp
6124 8e7bd50a 2019-08-22 stsp static const struct got_error *
6125 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
6126 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6127 8e7bd50a 2019-08-22 stsp {
6128 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6129 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
6130 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
6131 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
6132 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6133 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
6134 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
6135 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6136 8e7bd50a 2019-08-22 stsp
6137 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6138 84de9106 2020-12-26 stsp
6139 8e7bd50a 2019-08-22 stsp /*
6140 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
6141 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
6142 8e7bd50a 2019-08-22 stsp * an unintended typo.
6143 8e7bd50a 2019-08-22 stsp */
6144 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
6145 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6146 8e7bd50a 2019-08-22 stsp
6147 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
6148 8e7bd50a 2019-08-22 stsp if (err)
6149 8e7bd50a 2019-08-22 stsp return err;
6150 8e7bd50a 2019-08-22 stsp
6151 84de9106 2020-12-26 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6152 84de9106 2020-12-26 stsp if (err)
6153 84de9106 2020-12-26 stsp goto done;
6154 84de9106 2020-12-26 stsp
6155 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6156 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &refs, repo);
6157 8e7bd50a 2019-08-22 stsp if (err)
6158 8e7bd50a 2019-08-22 stsp goto done;
6159 8e7bd50a 2019-08-22 stsp
6160 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
6161 8e7bd50a 2019-08-22 stsp if (err)
6162 8e7bd50a 2019-08-22 stsp goto done;
6163 8e7bd50a 2019-08-22 stsp
6164 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
6165 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
6166 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
6167 62d463ca 2020-10-20 naddy err = got_error_from_errno("strdup");
6168 62d463ca 2020-10-20 naddy goto done;
6169 8e7bd50a 2019-08-22 stsp }
6170 8e7bd50a 2019-08-22 stsp tag_name += 10;
6171 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6172 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
6173 62d463ca 2020-10-20 naddy goto done;
6174 8e7bd50a 2019-08-22 stsp }
6175 8e7bd50a 2019-08-22 stsp
6176 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
6177 8e7bd50a 2019-08-22 stsp if (err == NULL) {
6178 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
6179 8e7bd50a 2019-08-22 stsp goto done;
6180 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
6181 8e7bd50a 2019-08-22 stsp goto done;
6182 8e7bd50a 2019-08-22 stsp
6183 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
6184 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6185 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
6186 f372d5cd 2019-10-21 stsp if (err) {
6187 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6188 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
6189 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6190 8e7bd50a 2019-08-22 stsp goto done;
6191 f372d5cd 2019-10-21 stsp }
6192 8e7bd50a 2019-08-22 stsp }
6193 8e7bd50a 2019-08-22 stsp
6194 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
6195 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6196 f372d5cd 2019-10-21 stsp if (err) {
6197 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6198 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6199 8e7bd50a 2019-08-22 stsp goto done;
6200 f372d5cd 2019-10-21 stsp }
6201 8e7bd50a 2019-08-22 stsp
6202 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
6203 f372d5cd 2019-10-21 stsp if (err) {
6204 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6205 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6206 8e7bd50a 2019-08-22 stsp goto done;
6207 f372d5cd 2019-10-21 stsp }
6208 8e7bd50a 2019-08-22 stsp
6209 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
6210 f372d5cd 2019-10-21 stsp if (err) {
6211 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6212 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6213 f372d5cd 2019-10-21 stsp goto done;
6214 f372d5cd 2019-10-21 stsp }
6215 8e7bd50a 2019-08-22 stsp
6216 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
6217 f372d5cd 2019-10-21 stsp if (err) {
6218 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6219 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6220 f372d5cd 2019-10-21 stsp goto done;
6221 8e7bd50a 2019-08-22 stsp }
6222 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
6223 8e7bd50a 2019-08-22 stsp done:
6224 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
6225 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
6226 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
6227 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6228 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
6229 f372d5cd 2019-10-21 stsp free(tag_id_str);
6230 8e7bd50a 2019-08-22 stsp if (ref)
6231 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
6232 8e7bd50a 2019-08-22 stsp free(commit_id);
6233 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6234 8e7bd50a 2019-08-22 stsp free(refname);
6235 8e7bd50a 2019-08-22 stsp free(tagmsg);
6236 f372d5cd 2019-10-21 stsp free(tagmsg_path);
6237 aba9c984 2019-09-08 stsp free(tagger);
6238 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
6239 8e7bd50a 2019-08-22 stsp return err;
6240 8e7bd50a 2019-08-22 stsp }
6241 8e7bd50a 2019-08-22 stsp
6242 8e7bd50a 2019-08-22 stsp static const struct got_error *
6243 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
6244 8e7bd50a 2019-08-22 stsp {
6245 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
6246 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
6247 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
6248 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6249 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
6250 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6251 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
6252 8e7bd50a 2019-08-22 stsp
6253 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6254 8e7bd50a 2019-08-22 stsp switch (ch) {
6255 80106605 2020-02-24 stsp case 'c':
6256 80106605 2020-02-24 stsp commit_id_arg = optarg;
6257 80106605 2020-02-24 stsp break;
6258 8e7bd50a 2019-08-22 stsp case 'm':
6259 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6260 8e7bd50a 2019-08-22 stsp break;
6261 8e7bd50a 2019-08-22 stsp case 'r':
6262 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6263 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6264 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6265 9ba1d308 2019-10-21 stsp optarg);
6266 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6267 8e7bd50a 2019-08-22 stsp break;
6268 8e7bd50a 2019-08-22 stsp case 'l':
6269 8e7bd50a 2019-08-22 stsp do_list = 1;
6270 8e7bd50a 2019-08-22 stsp break;
6271 8e7bd50a 2019-08-22 stsp default:
6272 8e7bd50a 2019-08-22 stsp usage_tag();
6273 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6274 8e7bd50a 2019-08-22 stsp }
6275 8e7bd50a 2019-08-22 stsp }
6276 8e7bd50a 2019-08-22 stsp
6277 8e7bd50a 2019-08-22 stsp argc -= optind;
6278 8e7bd50a 2019-08-22 stsp argv += optind;
6279 8e7bd50a 2019-08-22 stsp
6280 8e7bd50a 2019-08-22 stsp if (do_list) {
6281 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6282 775ce909 2020-03-22 stsp errx(1,
6283 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
6284 8e7bd50a 2019-08-22 stsp if (tagmsg)
6285 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
6286 8e7bd50a 2019-08-22 stsp if (argc > 0)
6287 8e7bd50a 2019-08-22 stsp usage_tag();
6288 80106605 2020-02-24 stsp } else if (argc != 1)
6289 8e7bd50a 2019-08-22 stsp usage_tag();
6290 80106605 2020-02-24 stsp
6291 a2887370 2019-08-23 stsp tag_name = argv[0];
6292 8e7bd50a 2019-08-22 stsp
6293 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
6294 8e7bd50a 2019-08-22 stsp if (do_list) {
6295 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6296 8e7bd50a 2019-08-22 stsp NULL) == -1)
6297 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6298 8e7bd50a 2019-08-22 stsp } else {
6299 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6300 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
6301 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6302 8e7bd50a 2019-08-22 stsp }
6303 8e7bd50a 2019-08-22 stsp #endif
6304 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
6305 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
6306 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
6307 8e7bd50a 2019-08-22 stsp goto done;
6308 8e7bd50a 2019-08-22 stsp }
6309 8e7bd50a 2019-08-22 stsp
6310 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6311 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6312 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6313 8e7bd50a 2019-08-22 stsp goto done;
6314 8e7bd50a 2019-08-22 stsp else
6315 8e7bd50a 2019-08-22 stsp error = NULL;
6316 8e7bd50a 2019-08-22 stsp if (worktree) {
6317 8e7bd50a 2019-08-22 stsp repo_path =
6318 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
6319 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6320 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6321 8e7bd50a 2019-08-22 stsp if (error)
6322 8e7bd50a 2019-08-22 stsp goto done;
6323 8e7bd50a 2019-08-22 stsp } else {
6324 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
6325 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6326 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6327 8e7bd50a 2019-08-22 stsp goto done;
6328 8e7bd50a 2019-08-22 stsp }
6329 8e7bd50a 2019-08-22 stsp }
6330 8e7bd50a 2019-08-22 stsp }
6331 8e7bd50a 2019-08-22 stsp
6332 8e7bd50a 2019-08-22 stsp if (do_list) {
6333 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6334 c9956ddf 2019-09-08 stsp if (error != NULL)
6335 c9956ddf 2019-09-08 stsp goto done;
6336 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6337 8e7bd50a 2019-08-22 stsp if (error)
6338 8e7bd50a 2019-08-22 stsp goto done;
6339 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
6340 8e7bd50a 2019-08-22 stsp } else {
6341 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6342 c9956ddf 2019-09-08 stsp if (error)
6343 c9956ddf 2019-09-08 stsp goto done;
6344 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
6345 c9956ddf 2019-09-08 stsp if (error != NULL)
6346 c9956ddf 2019-09-08 stsp goto done;
6347 c9956ddf 2019-09-08 stsp
6348 8e7bd50a 2019-08-22 stsp if (tagmsg) {
6349 42a285e2 2020-10-01 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6350 8e7bd50a 2019-08-22 stsp if (error)
6351 8e7bd50a 2019-08-22 stsp goto done;
6352 8e7bd50a 2019-08-22 stsp }
6353 8e7bd50a 2019-08-22 stsp
6354 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
6355 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
6356 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
6357 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
6358 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
6359 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
6360 8e7bd50a 2019-08-22 stsp if (error)
6361 8e7bd50a 2019-08-22 stsp goto done;
6362 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6363 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
6364 8e7bd50a 2019-08-22 stsp if (error)
6365 8e7bd50a 2019-08-22 stsp goto done;
6366 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
6367 8e7bd50a 2019-08-22 stsp free(commit_id);
6368 8e7bd50a 2019-08-22 stsp if (error)
6369 8e7bd50a 2019-08-22 stsp goto done;
6370 8e7bd50a 2019-08-22 stsp }
6371 8e7bd50a 2019-08-22 stsp
6372 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
6373 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6374 8e7bd50a 2019-08-22 stsp }
6375 8e7bd50a 2019-08-22 stsp done:
6376 8e7bd50a 2019-08-22 stsp if (repo)
6377 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
6378 8e7bd50a 2019-08-22 stsp if (worktree)
6379 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
6380 8e7bd50a 2019-08-22 stsp free(cwd);
6381 8e7bd50a 2019-08-22 stsp free(repo_path);
6382 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6383 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6384 8e7bd50a 2019-08-22 stsp return error;
6385 8e7bd50a 2019-08-22 stsp }
6386 8e7bd50a 2019-08-22 stsp
6387 8e7bd50a 2019-08-22 stsp __dead static void
6388 d00136be 2019-03-26 stsp usage_add(void)
6389 d00136be 2019-03-26 stsp {
6390 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6391 022fae89 2019-12-06 tracey getprogname());
6392 d00136be 2019-03-26 stsp exit(1);
6393 d00136be 2019-03-26 stsp }
6394 d00136be 2019-03-26 stsp
6395 d00136be 2019-03-26 stsp static const struct got_error *
6396 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6397 4e68cba3 2019-11-23 stsp {
6398 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6399 4e68cba3 2019-11-23 stsp path++;
6400 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6401 4e68cba3 2019-11-23 stsp return NULL;
6402 4e68cba3 2019-11-23 stsp }
6403 4e68cba3 2019-11-23 stsp
6404 4e68cba3 2019-11-23 stsp static const struct got_error *
6405 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6406 d00136be 2019-03-26 stsp {
6407 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6408 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6409 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6410 1dd54920 2019-05-11 stsp char *cwd = NULL;
6411 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6412 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6413 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6414 1dd54920 2019-05-11 stsp
6415 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6416 d00136be 2019-03-26 stsp
6417 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6418 d00136be 2019-03-26 stsp switch (ch) {
6419 022fae89 2019-12-06 tracey case 'I':
6420 022fae89 2019-12-06 tracey no_ignores = 1;
6421 022fae89 2019-12-06 tracey break;
6422 4e68cba3 2019-11-23 stsp case 'R':
6423 4e68cba3 2019-11-23 stsp can_recurse = 1;
6424 4e68cba3 2019-11-23 stsp break;
6425 d00136be 2019-03-26 stsp default:
6426 d00136be 2019-03-26 stsp usage_add();
6427 d00136be 2019-03-26 stsp /* NOTREACHED */
6428 d00136be 2019-03-26 stsp }
6429 d00136be 2019-03-26 stsp }
6430 d00136be 2019-03-26 stsp
6431 d00136be 2019-03-26 stsp argc -= optind;
6432 d00136be 2019-03-26 stsp argv += optind;
6433 d00136be 2019-03-26 stsp
6434 43012d58 2019-07-14 stsp #ifndef PROFILE
6435 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6436 43012d58 2019-07-14 stsp NULL) == -1)
6437 43012d58 2019-07-14 stsp err(1, "pledge");
6438 43012d58 2019-07-14 stsp #endif
6439 723c305c 2019-05-11 jcs if (argc < 1)
6440 d00136be 2019-03-26 stsp usage_add();
6441 d00136be 2019-03-26 stsp
6442 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6443 d00136be 2019-03-26 stsp if (cwd == NULL) {
6444 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6445 d00136be 2019-03-26 stsp goto done;
6446 d00136be 2019-03-26 stsp }
6447 723c305c 2019-05-11 jcs
6448 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6449 fa51e947 2020-03-27 stsp if (error) {
6450 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6451 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6452 d00136be 2019-03-26 stsp goto done;
6453 fa51e947 2020-03-27 stsp }
6454 d00136be 2019-03-26 stsp
6455 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6456 c9956ddf 2019-09-08 stsp NULL);
6457 031a5338 2019-03-26 stsp if (error != NULL)
6458 031a5338 2019-03-26 stsp goto done;
6459 031a5338 2019-03-26 stsp
6460 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6461 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6462 d00136be 2019-03-26 stsp if (error)
6463 d00136be 2019-03-26 stsp goto done;
6464 d00136be 2019-03-26 stsp
6465 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6466 6d022e97 2019-08-04 stsp if (error)
6467 022fae89 2019-12-06 tracey goto done;
6468 022fae89 2019-12-06 tracey
6469 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
6470 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6471 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
6472 6d022e97 2019-08-04 stsp goto done;
6473 723c305c 2019-05-11 jcs
6474 022fae89 2019-12-06 tracey }
6475 022fae89 2019-12-06 tracey
6476 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6477 4e68cba3 2019-11-23 stsp char *ondisk_path;
6478 4e68cba3 2019-11-23 stsp struct stat sb;
6479 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6480 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6481 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6482 62d463ca 2020-10-20 naddy pe->path) == -1) {
6483 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6484 4e68cba3 2019-11-23 stsp goto done;
6485 4e68cba3 2019-11-23 stsp }
6486 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6487 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6488 4e68cba3 2019-11-23 stsp free(ondisk_path);
6489 4e68cba3 2019-11-23 stsp continue;
6490 4e68cba3 2019-11-23 stsp }
6491 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6492 4e68cba3 2019-11-23 stsp ondisk_path);
6493 4e68cba3 2019-11-23 stsp free(ondisk_path);
6494 4e68cba3 2019-11-23 stsp goto done;
6495 4e68cba3 2019-11-23 stsp }
6496 4e68cba3 2019-11-23 stsp free(ondisk_path);
6497 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6498 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6499 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6500 4e68cba3 2019-11-23 stsp goto done;
6501 4e68cba3 2019-11-23 stsp }
6502 4e68cba3 2019-11-23 stsp }
6503 4e68cba3 2019-11-23 stsp }
6504 022fae89 2019-12-06 tracey
6505 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6506 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6507 d00136be 2019-03-26 stsp done:
6508 031a5338 2019-03-26 stsp if (repo)
6509 031a5338 2019-03-26 stsp got_repo_close(repo);
6510 d00136be 2019-03-26 stsp if (worktree)
6511 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6512 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6513 1dd54920 2019-05-11 stsp free((char *)pe->path);
6514 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6515 2ec1f75b 2019-03-26 stsp free(cwd);
6516 2ec1f75b 2019-03-26 stsp return error;
6517 2ec1f75b 2019-03-26 stsp }
6518 2ec1f75b 2019-03-26 stsp
6519 2ec1f75b 2019-03-26 stsp __dead static void
6520 648e4ef7 2019-07-09 stsp usage_remove(void)
6521 2ec1f75b 2019-03-26 stsp {
6522 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6523 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6524 2ec1f75b 2019-03-26 stsp exit(1);
6525 2a06fe5f 2019-08-24 stsp }
6526 2a06fe5f 2019-08-24 stsp
6527 2a06fe5f 2019-08-24 stsp static const struct got_error *
6528 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6529 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6530 2a06fe5f 2019-08-24 stsp {
6531 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6532 f2a9dc41 2019-12-13 tracey path++;
6533 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6534 2a06fe5f 2019-08-24 stsp return NULL;
6535 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6536 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6537 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6538 2a06fe5f 2019-08-24 stsp return NULL;
6539 2ec1f75b 2019-03-26 stsp }
6540 2ec1f75b 2019-03-26 stsp
6541 2ec1f75b 2019-03-26 stsp static const struct got_error *
6542 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6543 2ec1f75b 2019-03-26 stsp {
6544 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6545 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6546 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6547 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6548 17ed4618 2019-06-02 stsp char *cwd = NULL;
6549 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6550 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6551 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6552 2ec1f75b 2019-03-26 stsp
6553 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6554 17ed4618 2019-06-02 stsp
6555 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6556 2ec1f75b 2019-03-26 stsp switch (ch) {
6557 2ec1f75b 2019-03-26 stsp case 'f':
6558 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6559 2ec1f75b 2019-03-26 stsp break;
6560 70e3e7f5 2019-12-13 tracey case 'k':
6561 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6562 70e3e7f5 2019-12-13 tracey break;
6563 f2a9dc41 2019-12-13 tracey case 'R':
6564 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6565 f2a9dc41 2019-12-13 tracey break;
6566 766841c2 2020-08-13 stsp case 's':
6567 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6568 766841c2 2020-08-13 stsp switch (optarg[i]) {
6569 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6570 766841c2 2020-08-13 stsp delete_local_mods = 1;
6571 766841c2 2020-08-13 stsp break;
6572 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6573 766841c2 2020-08-13 stsp break;
6574 766841c2 2020-08-13 stsp default:
6575 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6576 766841c2 2020-08-13 stsp optarg[i]);
6577 766841c2 2020-08-13 stsp }
6578 766841c2 2020-08-13 stsp }
6579 766841c2 2020-08-13 stsp status_codes = optarg;
6580 766841c2 2020-08-13 stsp break;
6581 2ec1f75b 2019-03-26 stsp default:
6582 f2a9dc41 2019-12-13 tracey usage_remove();
6583 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6584 2ec1f75b 2019-03-26 stsp }
6585 2ec1f75b 2019-03-26 stsp }
6586 2ec1f75b 2019-03-26 stsp
6587 2ec1f75b 2019-03-26 stsp argc -= optind;
6588 2ec1f75b 2019-03-26 stsp argv += optind;
6589 2ec1f75b 2019-03-26 stsp
6590 43012d58 2019-07-14 stsp #ifndef PROFILE
6591 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6592 43012d58 2019-07-14 stsp NULL) == -1)
6593 43012d58 2019-07-14 stsp err(1, "pledge");
6594 43012d58 2019-07-14 stsp #endif
6595 17ed4618 2019-06-02 stsp if (argc < 1)
6596 648e4ef7 2019-07-09 stsp usage_remove();
6597 2ec1f75b 2019-03-26 stsp
6598 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6599 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6600 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6601 2ec1f75b 2019-03-26 stsp goto done;
6602 2ec1f75b 2019-03-26 stsp }
6603 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6604 fa51e947 2020-03-27 stsp if (error) {
6605 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6606 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6607 2ec1f75b 2019-03-26 stsp goto done;
6608 fa51e947 2020-03-27 stsp }
6609 2ec1f75b 2019-03-26 stsp
6610 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6611 c9956ddf 2019-09-08 stsp NULL);
6612 2af4a041 2019-05-11 jcs if (error)
6613 2ec1f75b 2019-03-26 stsp goto done;
6614 2ec1f75b 2019-03-26 stsp
6615 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6616 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6617 2ec1f75b 2019-03-26 stsp if (error)
6618 2ec1f75b 2019-03-26 stsp goto done;
6619 2ec1f75b 2019-03-26 stsp
6620 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6621 6d022e97 2019-08-04 stsp if (error)
6622 6d022e97 2019-08-04 stsp goto done;
6623 17ed4618 2019-06-02 stsp
6624 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6625 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6626 f2a9dc41 2019-12-13 tracey struct stat sb;
6627 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6628 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6629 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6630 62d463ca 2020-10-20 naddy pe->path) == -1) {
6631 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6632 f2a9dc41 2019-12-13 tracey goto done;
6633 f2a9dc41 2019-12-13 tracey }
6634 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6635 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6636 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6637 f2a9dc41 2019-12-13 tracey continue;
6638 f2a9dc41 2019-12-13 tracey }
6639 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6640 f2a9dc41 2019-12-13 tracey ondisk_path);
6641 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6642 f2a9dc41 2019-12-13 tracey goto done;
6643 f2a9dc41 2019-12-13 tracey }
6644 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6645 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6646 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6647 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6648 f2a9dc41 2019-12-13 tracey goto done;
6649 f2a9dc41 2019-12-13 tracey }
6650 f2a9dc41 2019-12-13 tracey }
6651 f2a9dc41 2019-12-13 tracey }
6652 f2a9dc41 2019-12-13 tracey
6653 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6654 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6655 766841c2 2020-08-13 stsp repo, keep_on_disk);
6656 a129376b 2019-03-28 stsp done:
6657 a129376b 2019-03-28 stsp if (repo)
6658 a129376b 2019-03-28 stsp got_repo_close(repo);
6659 a129376b 2019-03-28 stsp if (worktree)
6660 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6661 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6662 17ed4618 2019-06-02 stsp free((char *)pe->path);
6663 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6664 a129376b 2019-03-28 stsp free(cwd);
6665 a129376b 2019-03-28 stsp return error;
6666 a129376b 2019-03-28 stsp }
6667 a129376b 2019-03-28 stsp
6668 a129376b 2019-03-28 stsp __dead static void
6669 a129376b 2019-03-28 stsp usage_revert(void)
6670 a129376b 2019-03-28 stsp {
6671 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6672 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6673 a129376b 2019-03-28 stsp exit(1);
6674 a129376b 2019-03-28 stsp }
6675 a129376b 2019-03-28 stsp
6676 1ee397ad 2019-07-12 stsp static const struct got_error *
6677 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6678 a129376b 2019-03-28 stsp {
6679 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6680 fb9704af 2020-01-27 stsp return NULL;
6681 fb9704af 2020-01-27 stsp
6682 a129376b 2019-03-28 stsp while (path[0] == '/')
6683 a129376b 2019-03-28 stsp path++;
6684 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6685 33aa809d 2019-08-08 stsp return NULL;
6686 33aa809d 2019-08-08 stsp }
6687 33aa809d 2019-08-08 stsp
6688 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6689 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6690 33aa809d 2019-08-08 stsp const char *action;
6691 33aa809d 2019-08-08 stsp };
6692 33aa809d 2019-08-08 stsp
6693 33aa809d 2019-08-08 stsp static const struct got_error *
6694 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6695 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6696 33aa809d 2019-08-08 stsp {
6697 33aa809d 2019-08-08 stsp char *line = NULL;
6698 33aa809d 2019-08-08 stsp size_t linesize = 0;
6699 33aa809d 2019-08-08 stsp ssize_t linelen;
6700 33aa809d 2019-08-08 stsp
6701 33aa809d 2019-08-08 stsp switch (status) {
6702 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6703 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6704 33aa809d 2019-08-08 stsp break;
6705 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6706 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6707 33aa809d 2019-08-08 stsp break;
6708 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6709 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6710 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6711 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6712 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6713 33aa809d 2019-08-08 stsp printf("%s", line);
6714 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6715 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6716 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6717 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6718 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6719 33aa809d 2019-08-08 stsp break;
6720 33aa809d 2019-08-08 stsp default:
6721 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6722 33aa809d 2019-08-08 stsp }
6723 33aa809d 2019-08-08 stsp
6724 33aa809d 2019-08-08 stsp return NULL;
6725 33aa809d 2019-08-08 stsp }
6726 33aa809d 2019-08-08 stsp
6727 33aa809d 2019-08-08 stsp static const struct got_error *
6728 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6729 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6730 33aa809d 2019-08-08 stsp {
6731 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6732 33aa809d 2019-08-08 stsp char *line = NULL;
6733 33aa809d 2019-08-08 stsp size_t linesize = 0;
6734 33aa809d 2019-08-08 stsp ssize_t linelen;
6735 33aa809d 2019-08-08 stsp int resp = ' ';
6736 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6737 33aa809d 2019-08-08 stsp
6738 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6739 33aa809d 2019-08-08 stsp
6740 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6741 33aa809d 2019-08-08 stsp char *nl;
6742 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6743 33aa809d 2019-08-08 stsp a->action);
6744 33aa809d 2019-08-08 stsp if (err)
6745 33aa809d 2019-08-08 stsp return err;
6746 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6747 33aa809d 2019-08-08 stsp if (linelen == -1) {
6748 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6749 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6750 33aa809d 2019-08-08 stsp return NULL;
6751 33aa809d 2019-08-08 stsp }
6752 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6753 33aa809d 2019-08-08 stsp if (nl)
6754 33aa809d 2019-08-08 stsp *nl = '\0';
6755 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6756 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6757 33aa809d 2019-08-08 stsp printf("y\n");
6758 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6759 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6760 33aa809d 2019-08-08 stsp printf("n\n");
6761 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6762 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6763 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6764 33aa809d 2019-08-08 stsp printf("q\n");
6765 33aa809d 2019-08-08 stsp } else
6766 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6767 33aa809d 2019-08-08 stsp free(line);
6768 33aa809d 2019-08-08 stsp return NULL;
6769 33aa809d 2019-08-08 stsp }
6770 33aa809d 2019-08-08 stsp
6771 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6772 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6773 33aa809d 2019-08-08 stsp a->action);
6774 33aa809d 2019-08-08 stsp if (err)
6775 33aa809d 2019-08-08 stsp return err;
6776 33aa809d 2019-08-08 stsp resp = getchar();
6777 33aa809d 2019-08-08 stsp if (resp == '\n')
6778 33aa809d 2019-08-08 stsp resp = getchar();
6779 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6780 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6781 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6782 33aa809d 2019-08-08 stsp resp = ' ';
6783 33aa809d 2019-08-08 stsp }
6784 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6785 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6786 33aa809d 2019-08-08 stsp resp = ' ';
6787 33aa809d 2019-08-08 stsp }
6788 33aa809d 2019-08-08 stsp }
6789 33aa809d 2019-08-08 stsp
6790 33aa809d 2019-08-08 stsp if (resp == 'y')
6791 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6792 33aa809d 2019-08-08 stsp else if (resp == 'n')
6793 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6794 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6795 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6796 33aa809d 2019-08-08 stsp
6797 1ee397ad 2019-07-12 stsp return NULL;
6798 a129376b 2019-03-28 stsp }
6799 a129376b 2019-03-28 stsp
6800 33aa809d 2019-08-08 stsp
6801 a129376b 2019-03-28 stsp static const struct got_error *
6802 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6803 a129376b 2019-03-28 stsp {
6804 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6805 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6806 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6807 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6808 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6809 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6810 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6811 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6812 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6813 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6814 a129376b 2019-03-28 stsp
6815 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6816 e20a8b6f 2019-06-04 stsp
6817 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6818 a129376b 2019-03-28 stsp switch (ch) {
6819 33aa809d 2019-08-08 stsp case 'p':
6820 33aa809d 2019-08-08 stsp pflag = 1;
6821 33aa809d 2019-08-08 stsp break;
6822 33aa809d 2019-08-08 stsp case 'F':
6823 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6824 33aa809d 2019-08-08 stsp break;
6825 0f6d7415 2019-08-08 stsp case 'R':
6826 0f6d7415 2019-08-08 stsp can_recurse = 1;
6827 0f6d7415 2019-08-08 stsp break;
6828 a129376b 2019-03-28 stsp default:
6829 a129376b 2019-03-28 stsp usage_revert();
6830 a129376b 2019-03-28 stsp /* NOTREACHED */
6831 a129376b 2019-03-28 stsp }
6832 a129376b 2019-03-28 stsp }
6833 a129376b 2019-03-28 stsp
6834 a129376b 2019-03-28 stsp argc -= optind;
6835 a129376b 2019-03-28 stsp argv += optind;
6836 a129376b 2019-03-28 stsp
6837 43012d58 2019-07-14 stsp #ifndef PROFILE
6838 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6839 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6840 43012d58 2019-07-14 stsp err(1, "pledge");
6841 43012d58 2019-07-14 stsp #endif
6842 e20a8b6f 2019-06-04 stsp if (argc < 1)
6843 a129376b 2019-03-28 stsp usage_revert();
6844 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6845 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6846 a129376b 2019-03-28 stsp
6847 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6848 a129376b 2019-03-28 stsp if (cwd == NULL) {
6849 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6850 a129376b 2019-03-28 stsp goto done;
6851 a129376b 2019-03-28 stsp }
6852 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6853 fa51e947 2020-03-27 stsp if (error) {
6854 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6855 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6856 2ec1f75b 2019-03-26 stsp goto done;
6857 fa51e947 2020-03-27 stsp }
6858 a129376b 2019-03-28 stsp
6859 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6860 c9956ddf 2019-09-08 stsp NULL);
6861 a129376b 2019-03-28 stsp if (error != NULL)
6862 a129376b 2019-03-28 stsp goto done;
6863 a129376b 2019-03-28 stsp
6864 33aa809d 2019-08-08 stsp if (patch_script_path) {
6865 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6866 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
6867 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
6868 33aa809d 2019-08-08 stsp patch_script_path);
6869 33aa809d 2019-08-08 stsp goto done;
6870 33aa809d 2019-08-08 stsp }
6871 33aa809d 2019-08-08 stsp }
6872 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6873 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6874 a129376b 2019-03-28 stsp if (error)
6875 a129376b 2019-03-28 stsp goto done;
6876 a129376b 2019-03-28 stsp
6877 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6878 6d022e97 2019-08-04 stsp if (error)
6879 6d022e97 2019-08-04 stsp goto done;
6880 00db391e 2019-08-03 stsp
6881 0f6d7415 2019-08-08 stsp if (!can_recurse) {
6882 0f6d7415 2019-08-08 stsp char *ondisk_path;
6883 0f6d7415 2019-08-08 stsp struct stat sb;
6884 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
6885 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
6886 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
6887 62d463ca 2020-10-20 naddy pe->path) == -1) {
6888 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
6889 0f6d7415 2019-08-08 stsp goto done;
6890 0f6d7415 2019-08-08 stsp }
6891 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
6892 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
6893 0f6d7415 2019-08-08 stsp free(ondisk_path);
6894 0f6d7415 2019-08-08 stsp continue;
6895 0f6d7415 2019-08-08 stsp }
6896 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
6897 0f6d7415 2019-08-08 stsp ondisk_path);
6898 0f6d7415 2019-08-08 stsp free(ondisk_path);
6899 0f6d7415 2019-08-08 stsp goto done;
6900 0f6d7415 2019-08-08 stsp }
6901 0f6d7415 2019-08-08 stsp free(ondisk_path);
6902 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
6903 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6904 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
6905 0f6d7415 2019-08-08 stsp goto done;
6906 0f6d7415 2019-08-08 stsp }
6907 0f6d7415 2019-08-08 stsp }
6908 0f6d7415 2019-08-08 stsp }
6909 0f6d7415 2019-08-08 stsp
6910 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6911 33aa809d 2019-08-08 stsp cpa.action = "revert";
6912 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6913 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6914 2ec1f75b 2019-03-26 stsp done:
6915 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6916 33aa809d 2019-08-08 stsp error == NULL)
6917 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6918 2ec1f75b 2019-03-26 stsp if (repo)
6919 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
6920 2ec1f75b 2019-03-26 stsp if (worktree)
6921 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
6922 2ec1f75b 2019-03-26 stsp free(path);
6923 d00136be 2019-03-26 stsp free(cwd);
6924 6bad629b 2019-02-04 stsp return error;
6925 c4296144 2019-05-09 stsp }
6926 c4296144 2019-05-09 stsp
6927 c4296144 2019-05-09 stsp __dead static void
6928 c4296144 2019-05-09 stsp usage_commit(void)
6929 c4296144 2019-05-09 stsp {
6930 35213c7c 2020-07-23 stsp fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6931 5c1e53bc 2019-07-28 stsp getprogname());
6932 c4296144 2019-05-09 stsp exit(1);
6933 33ad4cbe 2019-05-12 jcs }
6934 33ad4cbe 2019-05-12 jcs
6935 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
6936 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
6937 e2ba3d07 2019-05-13 stsp const char *editor;
6938 e0870e44 2019-05-13 stsp const char *worktree_path;
6939 76d98825 2019-06-03 stsp const char *branch_name;
6940 314a6357 2019-05-13 stsp const char *repo_path;
6941 e0870e44 2019-05-13 stsp char *logmsg_path;
6942 e2ba3d07 2019-05-13 stsp
6943 e2ba3d07 2019-05-13 stsp };
6944 e0870e44 2019-05-13 stsp
6945 33ad4cbe 2019-05-12 jcs static const struct got_error *
6946 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6947 33ad4cbe 2019-05-12 jcs void *arg)
6948 33ad4cbe 2019-05-12 jcs {
6949 76d98825 2019-06-03 stsp char *initial_content = NULL;
6950 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
6951 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
6952 e0870e44 2019-05-13 stsp char *template = NULL;
6953 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
6954 1601cb9f 2020-09-11 naddy int initial_content_len;
6955 97972933 2020-09-11 stsp int fd = -1;
6956 33ad4cbe 2019-05-12 jcs size_t len;
6957 33ad4cbe 2019-05-12 jcs
6958 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
6959 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6960 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
6961 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
6962 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
6963 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
6964 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
6965 33ad4cbe 2019-05-12 jcs return NULL;
6966 33ad4cbe 2019-05-12 jcs }
6967 33ad4cbe 2019-05-12 jcs
6968 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6969 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
6970 76d98825 2019-06-03 stsp
6971 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6972 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
6973 1601cb9f 2020-09-11 naddy a->branch_name);
6974 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
6975 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6976 e0870e44 2019-05-13 stsp
6977 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6978 793c30b5 2019-05-13 stsp if (err)
6979 e0870e44 2019-05-13 stsp goto done;
6980 33ad4cbe 2019-05-12 jcs
6981 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6982 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
6983 97972933 2020-09-11 stsp goto done;
6984 97972933 2020-09-11 stsp }
6985 33ad4cbe 2019-05-12 jcs
6986 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
6987 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6988 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
6989 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
6990 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
6991 33ad4cbe 2019-05-12 jcs }
6992 33ad4cbe 2019-05-12 jcs
6993 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
6994 0d5bb276 2020-12-15 stsp initial_content_len, 1);
6995 33ad4cbe 2019-05-12 jcs done:
6996 76d98825 2019-06-03 stsp free(initial_content);
6997 e0870e44 2019-05-13 stsp free(template);
6998 314a6357 2019-05-13 stsp
6999 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
7000 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
7001 97972933 2020-09-11 stsp
7002 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
7003 59f86c76 2020-09-11 stsp if (err == NULL)
7004 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
7005 59f86c76 2020-09-11 stsp if (err) {
7006 59f86c76 2020-09-11 stsp free(*logmsg);
7007 59f86c76 2020-09-11 stsp *logmsg = NULL;
7008 3ce1b845 2019-07-15 stsp }
7009 33ad4cbe 2019-05-12 jcs return err;
7010 6bad629b 2019-02-04 stsp }
7011 c4296144 2019-05-09 stsp
7012 c4296144 2019-05-09 stsp static const struct got_error *
7013 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
7014 c4296144 2019-05-09 stsp {
7015 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
7016 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
7017 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
7018 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
7019 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
7020 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
7021 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
7022 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7023 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7024 35213c7c 2020-07-23 stsp int allow_bad_symlinks = 0;
7025 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
7026 5c1e53bc 2019-07-28 stsp
7027 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
7028 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
7029 c4296144 2019-05-09 stsp
7030 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "m:S")) != -1) {
7031 c4296144 2019-05-09 stsp switch (ch) {
7032 c4296144 2019-05-09 stsp case 'm':
7033 c4296144 2019-05-09 stsp logmsg = optarg;
7034 c4296144 2019-05-09 stsp break;
7035 35213c7c 2020-07-23 stsp case 'S':
7036 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
7037 35213c7c 2020-07-23 stsp break;
7038 c4296144 2019-05-09 stsp default:
7039 c4296144 2019-05-09 stsp usage_commit();
7040 c4296144 2019-05-09 stsp /* NOTREACHED */
7041 c4296144 2019-05-09 stsp }
7042 c4296144 2019-05-09 stsp }
7043 c4296144 2019-05-09 stsp
7044 c4296144 2019-05-09 stsp argc -= optind;
7045 c4296144 2019-05-09 stsp argv += optind;
7046 c4296144 2019-05-09 stsp
7047 43012d58 2019-07-14 stsp #ifndef PROFILE
7048 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7049 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7050 43012d58 2019-07-14 stsp err(1, "pledge");
7051 43012d58 2019-07-14 stsp #endif
7052 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
7053 c4296144 2019-05-09 stsp if (cwd == NULL) {
7054 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7055 c4296144 2019-05-09 stsp goto done;
7056 c4296144 2019-05-09 stsp }
7057 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
7058 fa51e947 2020-03-27 stsp if (error) {
7059 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7060 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
7061 7d5807f4 2019-07-11 stsp goto done;
7062 fa51e947 2020-03-27 stsp }
7063 7d5807f4 2019-07-11 stsp
7064 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7065 c4296144 2019-05-09 stsp if (error)
7066 a698f62e 2019-07-25 stsp goto done;
7067 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
7068 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
7069 c4296144 2019-05-09 stsp goto done;
7070 a698f62e 2019-07-25 stsp }
7071 5c1e53bc 2019-07-28 stsp
7072 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7073 916f288c 2019-07-30 stsp worktree);
7074 5c1e53bc 2019-07-28 stsp if (error)
7075 5c1e53bc 2019-07-28 stsp goto done;
7076 c4296144 2019-05-09 stsp
7077 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
7078 c9956ddf 2019-09-08 stsp if (error)
7079 c9956ddf 2019-09-08 stsp goto done;
7080 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7081 c9956ddf 2019-09-08 stsp gitconfig_path);
7082 c4296144 2019-05-09 stsp if (error != NULL)
7083 c4296144 2019-05-09 stsp goto done;
7084 c4296144 2019-05-09 stsp
7085 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
7086 aba9c984 2019-09-08 stsp if (error)
7087 aba9c984 2019-09-08 stsp return error;
7088 aba9c984 2019-09-08 stsp
7089 314a6357 2019-05-13 stsp /*
7090 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
7091 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
7092 314a6357 2019-05-13 stsp */
7093 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
7094 314a6357 2019-05-13 stsp error = get_editor(&editor);
7095 314a6357 2019-05-13 stsp else
7096 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7097 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7098 c4296144 2019-05-09 stsp if (error)
7099 c4296144 2019-05-09 stsp goto done;
7100 c4296144 2019-05-09 stsp
7101 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7102 087fb88c 2019-08-04 stsp if (error)
7103 087fb88c 2019-08-04 stsp goto done;
7104 087fb88c 2019-08-04 stsp
7105 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
7106 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
7107 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7108 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7109 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
7110 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7111 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
7112 916f288c 2019-07-30 stsp goto done;
7113 916f288c 2019-07-30 stsp }
7114 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
7115 916f288c 2019-07-30 stsp }
7116 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
7117 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7118 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7119 35213c7c 2020-07-23 stsp print_status, NULL, repo);
7120 e0870e44 2019-05-13 stsp if (error) {
7121 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7122 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
7123 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
7124 c4296144 2019-05-09 stsp goto done;
7125 e0870e44 2019-05-13 stsp }
7126 c4296144 2019-05-09 stsp
7127 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
7128 c4296144 2019-05-09 stsp if (error)
7129 c4296144 2019-05-09 stsp goto done;
7130 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
7131 c4296144 2019-05-09 stsp done:
7132 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
7133 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
7134 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
7135 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7136 7266f21f 2019-10-21 stsp error == NULL)
7137 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7138 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
7139 c4296144 2019-05-09 stsp if (repo)
7140 c4296144 2019-05-09 stsp got_repo_close(repo);
7141 c4296144 2019-05-09 stsp if (worktree)
7142 c4296144 2019-05-09 stsp got_worktree_close(worktree);
7143 c4296144 2019-05-09 stsp free(cwd);
7144 c4296144 2019-05-09 stsp free(id_str);
7145 c9956ddf 2019-09-08 stsp free(gitconfig_path);
7146 e2ba3d07 2019-05-13 stsp free(editor);
7147 aba9c984 2019-09-08 stsp free(author);
7148 234035bc 2019-06-01 stsp return error;
7149 234035bc 2019-06-01 stsp }
7150 234035bc 2019-06-01 stsp
7151 234035bc 2019-06-01 stsp __dead static void
7152 234035bc 2019-06-01 stsp usage_cherrypick(void)
7153 234035bc 2019-06-01 stsp {
7154 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
7155 234035bc 2019-06-01 stsp exit(1);
7156 234035bc 2019-06-01 stsp }
7157 234035bc 2019-06-01 stsp
7158 234035bc 2019-06-01 stsp static const struct got_error *
7159 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
7160 234035bc 2019-06-01 stsp {
7161 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
7162 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
7163 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
7164 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
7165 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
7166 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
7167 234035bc 2019-06-01 stsp struct got_object_qid *pid;
7168 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
7169 9627c110 2020-04-18 stsp int ch;
7170 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7171 234035bc 2019-06-01 stsp
7172 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7173 234035bc 2019-06-01 stsp switch (ch) {
7174 234035bc 2019-06-01 stsp default:
7175 234035bc 2019-06-01 stsp usage_cherrypick();
7176 234035bc 2019-06-01 stsp /* NOTREACHED */
7177 234035bc 2019-06-01 stsp }
7178 234035bc 2019-06-01 stsp }
7179 234035bc 2019-06-01 stsp
7180 234035bc 2019-06-01 stsp argc -= optind;
7181 234035bc 2019-06-01 stsp argv += optind;
7182 234035bc 2019-06-01 stsp
7183 43012d58 2019-07-14 stsp #ifndef PROFILE
7184 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7185 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7186 43012d58 2019-07-14 stsp err(1, "pledge");
7187 43012d58 2019-07-14 stsp #endif
7188 234035bc 2019-06-01 stsp if (argc != 1)
7189 234035bc 2019-06-01 stsp usage_cherrypick();
7190 234035bc 2019-06-01 stsp
7191 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
7192 234035bc 2019-06-01 stsp if (cwd == NULL) {
7193 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
7194 234035bc 2019-06-01 stsp goto done;
7195 234035bc 2019-06-01 stsp }
7196 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
7197 fa51e947 2020-03-27 stsp if (error) {
7198 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7199 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
7200 fa51e947 2020-03-27 stsp cwd);
7201 234035bc 2019-06-01 stsp goto done;
7202 fa51e947 2020-03-27 stsp }
7203 234035bc 2019-06-01 stsp
7204 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7205 c9956ddf 2019-09-08 stsp NULL);
7206 234035bc 2019-06-01 stsp if (error != NULL)
7207 234035bc 2019-06-01 stsp goto done;
7208 234035bc 2019-06-01 stsp
7209 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7210 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7211 234035bc 2019-06-01 stsp if (error)
7212 234035bc 2019-06-01 stsp goto done;
7213 234035bc 2019-06-01 stsp
7214 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7215 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7216 234035bc 2019-06-01 stsp if (error != NULL) {
7217 234035bc 2019-06-01 stsp struct got_reference *ref;
7218 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7219 234035bc 2019-06-01 stsp goto done;
7220 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7221 234035bc 2019-06-01 stsp if (error != NULL)
7222 234035bc 2019-06-01 stsp goto done;
7223 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
7224 234035bc 2019-06-01 stsp got_ref_close(ref);
7225 234035bc 2019-06-01 stsp if (error != NULL)
7226 234035bc 2019-06-01 stsp goto done;
7227 234035bc 2019-06-01 stsp }
7228 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
7229 234035bc 2019-06-01 stsp if (error)
7230 234035bc 2019-06-01 stsp goto done;
7231 234035bc 2019-06-01 stsp
7232 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
7233 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
7234 234035bc 2019-06-01 stsp if (error != NULL)
7235 234035bc 2019-06-01 stsp goto done;
7236 234035bc 2019-06-01 stsp
7237 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7238 234035bc 2019-06-01 stsp if (error) {
7239 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
7240 234035bc 2019-06-01 stsp goto done;
7241 234035bc 2019-06-01 stsp error = NULL;
7242 234035bc 2019-06-01 stsp } else {
7243 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
7244 234035bc 2019-06-01 stsp goto done;
7245 234035bc 2019-06-01 stsp }
7246 234035bc 2019-06-01 stsp
7247 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7248 234035bc 2019-06-01 stsp if (error)
7249 234035bc 2019-06-01 stsp goto done;
7250 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7251 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7252 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7253 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
7254 03415a1a 2019-06-02 stsp NULL);
7255 234035bc 2019-06-01 stsp if (error != NULL)
7256 234035bc 2019-06-01 stsp goto done;
7257 234035bc 2019-06-01 stsp
7258 9627c110 2020-04-18 stsp if (upa.did_something)
7259 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
7260 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7261 234035bc 2019-06-01 stsp done:
7262 234035bc 2019-06-01 stsp if (commit)
7263 234035bc 2019-06-01 stsp got_object_commit_close(commit);
7264 234035bc 2019-06-01 stsp free(commit_id_str);
7265 234035bc 2019-06-01 stsp if (head_ref)
7266 234035bc 2019-06-01 stsp got_ref_close(head_ref);
7267 234035bc 2019-06-01 stsp if (worktree)
7268 234035bc 2019-06-01 stsp got_worktree_close(worktree);
7269 234035bc 2019-06-01 stsp if (repo)
7270 234035bc 2019-06-01 stsp got_repo_close(repo);
7271 c4296144 2019-05-09 stsp return error;
7272 c4296144 2019-05-09 stsp }
7273 5ef14e63 2019-06-02 stsp
7274 5ef14e63 2019-06-02 stsp __dead static void
7275 5ef14e63 2019-06-02 stsp usage_backout(void)
7276 5ef14e63 2019-06-02 stsp {
7277 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7278 5ef14e63 2019-06-02 stsp exit(1);
7279 5ef14e63 2019-06-02 stsp }
7280 5ef14e63 2019-06-02 stsp
7281 5ef14e63 2019-06-02 stsp static const struct got_error *
7282 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
7283 5ef14e63 2019-06-02 stsp {
7284 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
7285 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
7286 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
7287 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
7288 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
7289 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
7290 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
7291 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
7292 9627c110 2020-04-18 stsp int ch;
7293 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7294 5ef14e63 2019-06-02 stsp
7295 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7296 5ef14e63 2019-06-02 stsp switch (ch) {
7297 5ef14e63 2019-06-02 stsp default:
7298 5ef14e63 2019-06-02 stsp usage_backout();
7299 5ef14e63 2019-06-02 stsp /* NOTREACHED */
7300 5ef14e63 2019-06-02 stsp }
7301 5ef14e63 2019-06-02 stsp }
7302 5ef14e63 2019-06-02 stsp
7303 5ef14e63 2019-06-02 stsp argc -= optind;
7304 5ef14e63 2019-06-02 stsp argv += optind;
7305 5ef14e63 2019-06-02 stsp
7306 43012d58 2019-07-14 stsp #ifndef PROFILE
7307 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7308 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7309 43012d58 2019-07-14 stsp err(1, "pledge");
7310 43012d58 2019-07-14 stsp #endif
7311 5ef14e63 2019-06-02 stsp if (argc != 1)
7312 5ef14e63 2019-06-02 stsp usage_backout();
7313 5ef14e63 2019-06-02 stsp
7314 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
7315 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
7316 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
7317 5ef14e63 2019-06-02 stsp goto done;
7318 5ef14e63 2019-06-02 stsp }
7319 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
7320 fa51e947 2020-03-27 stsp if (error) {
7321 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7322 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
7323 5ef14e63 2019-06-02 stsp goto done;
7324 fa51e947 2020-03-27 stsp }
7325 5ef14e63 2019-06-02 stsp
7326 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7327 c9956ddf 2019-09-08 stsp NULL);
7328 5ef14e63 2019-06-02 stsp if (error != NULL)
7329 5ef14e63 2019-06-02 stsp goto done;
7330 5ef14e63 2019-06-02 stsp
7331 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7332 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7333 5ef14e63 2019-06-02 stsp if (error)
7334 5ef14e63 2019-06-02 stsp goto done;
7335 5ef14e63 2019-06-02 stsp
7336 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7337 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7338 5ef14e63 2019-06-02 stsp if (error != NULL) {
7339 5ef14e63 2019-06-02 stsp struct got_reference *ref;
7340 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7341 5ef14e63 2019-06-02 stsp goto done;
7342 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7343 5ef14e63 2019-06-02 stsp if (error != NULL)
7344 5ef14e63 2019-06-02 stsp goto done;
7345 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
7346 5ef14e63 2019-06-02 stsp got_ref_close(ref);
7347 5ef14e63 2019-06-02 stsp if (error != NULL)
7348 5ef14e63 2019-06-02 stsp goto done;
7349 5ef14e63 2019-06-02 stsp }
7350 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
7351 5ef14e63 2019-06-02 stsp if (error)
7352 5ef14e63 2019-06-02 stsp goto done;
7353 5ef14e63 2019-06-02 stsp
7354 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
7355 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
7356 5ef14e63 2019-06-02 stsp if (error != NULL)
7357 5ef14e63 2019-06-02 stsp goto done;
7358 5ef14e63 2019-06-02 stsp
7359 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7360 5ef14e63 2019-06-02 stsp if (error)
7361 5ef14e63 2019-06-02 stsp goto done;
7362 5ef14e63 2019-06-02 stsp
7363 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7364 5ef14e63 2019-06-02 stsp if (error)
7365 5ef14e63 2019-06-02 stsp goto done;
7366 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7367 5ef14e63 2019-06-02 stsp if (pid == NULL) {
7368 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
7369 5ef14e63 2019-06-02 stsp goto done;
7370 5ef14e63 2019-06-02 stsp }
7371 5ef14e63 2019-06-02 stsp
7372 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7373 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
7374 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7375 5ef14e63 2019-06-02 stsp if (error != NULL)
7376 5ef14e63 2019-06-02 stsp goto done;
7377 5ef14e63 2019-06-02 stsp
7378 9627c110 2020-04-18 stsp if (upa.did_something)
7379 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
7380 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7381 5ef14e63 2019-06-02 stsp done:
7382 5ef14e63 2019-06-02 stsp if (commit)
7383 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
7384 5ef14e63 2019-06-02 stsp free(commit_id_str);
7385 5ef14e63 2019-06-02 stsp if (head_ref)
7386 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
7387 818c7501 2019-07-11 stsp if (worktree)
7388 818c7501 2019-07-11 stsp got_worktree_close(worktree);
7389 818c7501 2019-07-11 stsp if (repo)
7390 818c7501 2019-07-11 stsp got_repo_close(repo);
7391 818c7501 2019-07-11 stsp return error;
7392 818c7501 2019-07-11 stsp }
7393 818c7501 2019-07-11 stsp
7394 818c7501 2019-07-11 stsp __dead static void
7395 818c7501 2019-07-11 stsp usage_rebase(void)
7396 818c7501 2019-07-11 stsp {
7397 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7398 af54c8f8 2019-07-11 stsp getprogname());
7399 818c7501 2019-07-11 stsp exit(1);
7400 0ebf8283 2019-07-24 stsp }
7401 0ebf8283 2019-07-24 stsp
7402 0ebf8283 2019-07-24 stsp void
7403 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
7404 0ebf8283 2019-07-24 stsp {
7405 0ebf8283 2019-07-24 stsp char *nl;
7406 0ebf8283 2019-07-24 stsp size_t len;
7407 0ebf8283 2019-07-24 stsp
7408 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
7409 0ebf8283 2019-07-24 stsp if (len > limit)
7410 0ebf8283 2019-07-24 stsp len = limit;
7411 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
7412 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
7413 0ebf8283 2019-07-24 stsp if (nl)
7414 0ebf8283 2019-07-24 stsp *nl = '\0';
7415 0ebf8283 2019-07-24 stsp }
7416 0ebf8283 2019-07-24 stsp
7417 0ebf8283 2019-07-24 stsp static const struct got_error *
7418 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7419 0ebf8283 2019-07-24 stsp {
7420 5943eee2 2019-08-13 stsp const struct got_error *err;
7421 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
7422 5943eee2 2019-08-13 stsp const char *s;
7423 0ebf8283 2019-07-24 stsp
7424 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7425 5943eee2 2019-08-13 stsp if (err)
7426 5943eee2 2019-08-13 stsp return err;
7427 0ebf8283 2019-07-24 stsp
7428 5943eee2 2019-08-13 stsp s = logmsg0;
7429 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
7430 5943eee2 2019-08-13 stsp s++;
7431 5943eee2 2019-08-13 stsp
7432 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
7433 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
7434 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7435 5943eee2 2019-08-13 stsp goto done;
7436 5943eee2 2019-08-13 stsp }
7437 0ebf8283 2019-07-24 stsp
7438 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
7439 5943eee2 2019-08-13 stsp done:
7440 5943eee2 2019-08-13 stsp free(logmsg0);
7441 a0ea4fc0 2020-02-28 stsp return err;
7442 a0ea4fc0 2020-02-28 stsp }
7443 a0ea4fc0 2020-02-28 stsp
7444 a0ea4fc0 2020-02-28 stsp static const struct got_error *
7445 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7446 a0ea4fc0 2020-02-28 stsp {
7447 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
7448 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
7449 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
7450 a0ea4fc0 2020-02-28 stsp
7451 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
7452 a0ea4fc0 2020-02-28 stsp if (err)
7453 a0ea4fc0 2020-02-28 stsp return err;
7454 a0ea4fc0 2020-02-28 stsp
7455 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
7456 a0ea4fc0 2020-02-28 stsp if (err)
7457 a0ea4fc0 2020-02-28 stsp goto done;
7458 a0ea4fc0 2020-02-28 stsp
7459 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
7460 a0ea4fc0 2020-02-28 stsp
7461 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
7462 a0ea4fc0 2020-02-28 stsp if (err)
7463 a0ea4fc0 2020-02-28 stsp goto done;
7464 a0ea4fc0 2020-02-28 stsp
7465 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
7466 a0ea4fc0 2020-02-28 stsp done:
7467 a0ea4fc0 2020-02-28 stsp free(id_str);
7468 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
7469 a0ea4fc0 2020-02-28 stsp free(logmsg);
7470 5943eee2 2019-08-13 stsp return err;
7471 818c7501 2019-07-11 stsp }
7472 818c7501 2019-07-11 stsp
7473 818c7501 2019-07-11 stsp static const struct got_error *
7474 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
7475 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
7476 818c7501 2019-07-11 stsp {
7477 818c7501 2019-07-11 stsp const struct got_error *err;
7478 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7479 818c7501 2019-07-11 stsp
7480 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
7481 818c7501 2019-07-11 stsp if (err)
7482 818c7501 2019-07-11 stsp goto done;
7483 818c7501 2019-07-11 stsp
7484 ff0d2220 2019-07-11 stsp if (new_id) {
7485 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
7486 ff0d2220 2019-07-11 stsp if (err)
7487 ff0d2220 2019-07-11 stsp goto done;
7488 ff0d2220 2019-07-11 stsp }
7489 818c7501 2019-07-11 stsp
7490 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
7491 ff0d2220 2019-07-11 stsp if (new_id_str)
7492 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
7493 0ebf8283 2019-07-24 stsp
7494 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7495 0ebf8283 2019-07-24 stsp if (err)
7496 0ebf8283 2019-07-24 stsp goto done;
7497 0ebf8283 2019-07-24 stsp
7498 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
7499 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7500 818c7501 2019-07-11 stsp done:
7501 818c7501 2019-07-11 stsp free(old_id_str);
7502 818c7501 2019-07-11 stsp free(new_id_str);
7503 272a1371 2020-02-28 stsp free(logmsg);
7504 818c7501 2019-07-11 stsp return err;
7505 818c7501 2019-07-11 stsp }
7506 818c7501 2019-07-11 stsp
7507 1ee397ad 2019-07-12 stsp static const struct got_error *
7508 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7509 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
7510 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
7511 818c7501 2019-07-11 stsp {
7512 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
7513 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
7514 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
7515 818c7501 2019-07-11 stsp }
7516 818c7501 2019-07-11 stsp
7517 818c7501 2019-07-11 stsp static const struct got_error *
7518 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
7519 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7520 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
7521 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7522 ff0d2220 2019-07-11 stsp {
7523 ff0d2220 2019-07-11 stsp const struct got_error *error;
7524 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
7525 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
7526 ff0d2220 2019-07-11 stsp
7527 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7528 ff0d2220 2019-07-11 stsp if (error)
7529 ff0d2220 2019-07-11 stsp return error;
7530 ff0d2220 2019-07-11 stsp
7531 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7532 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
7533 ff0d2220 2019-07-11 stsp if (error) {
7534 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7535 ff0d2220 2019-07-11 stsp goto done;
7536 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
7537 ff0d2220 2019-07-11 stsp } else {
7538 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
7539 ff0d2220 2019-07-11 stsp free(new_commit_id);
7540 ff0d2220 2019-07-11 stsp }
7541 ff0d2220 2019-07-11 stsp done:
7542 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
7543 ff0d2220 2019-07-11 stsp return error;
7544 64c6d990 2019-07-11 stsp }
7545 64c6d990 2019-07-11 stsp
7546 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
7547 64c6d990 2019-07-11 stsp const char *path_prefix;
7548 64c6d990 2019-07-11 stsp size_t len;
7549 8ca9bd68 2019-07-25 stsp int errcode;
7550 64c6d990 2019-07-11 stsp };
7551 64c6d990 2019-07-11 stsp
7552 64c6d990 2019-07-11 stsp static const struct got_error *
7553 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7554 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
7555 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
7556 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
7557 64c6d990 2019-07-11 stsp {
7558 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
7559 64c6d990 2019-07-11 stsp
7560 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7561 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7562 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
7563 64c6d990 2019-07-11 stsp
7564 64c6d990 2019-07-11 stsp return NULL;
7565 64c6d990 2019-07-11 stsp }
7566 64c6d990 2019-07-11 stsp
7567 64c6d990 2019-07-11 stsp static const struct got_error *
7568 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
7569 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
7570 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
7571 64c6d990 2019-07-11 stsp {
7572 64c6d990 2019-07-11 stsp const struct got_error *err;
7573 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7574 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7575 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
7576 64c6d990 2019-07-11 stsp
7577 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
7578 64c6d990 2019-07-11 stsp return NULL;
7579 64c6d990 2019-07-11 stsp
7580 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7581 64c6d990 2019-07-11 stsp if (err)
7582 64c6d990 2019-07-11 stsp goto done;
7583 64c6d990 2019-07-11 stsp
7584 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7585 64c6d990 2019-07-11 stsp if (err)
7586 64c6d990 2019-07-11 stsp goto done;
7587 64c6d990 2019-07-11 stsp
7588 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
7589 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
7590 64c6d990 2019-07-11 stsp if (err)
7591 64c6d990 2019-07-11 stsp goto done;
7592 64c6d990 2019-07-11 stsp
7593 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
7594 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
7595 64c6d990 2019-07-11 stsp if (err)
7596 64c6d990 2019-07-11 stsp goto done;
7597 64c6d990 2019-07-11 stsp
7598 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
7599 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
7600 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
7601 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7602 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
7603 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7604 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7605 64c6d990 2019-07-11 stsp done:
7606 64c6d990 2019-07-11 stsp if (tree1)
7607 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
7608 64c6d990 2019-07-11 stsp if (tree2)
7609 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
7610 64c6d990 2019-07-11 stsp if (commit)
7611 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
7612 64c6d990 2019-07-11 stsp if (parent_commit)
7613 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
7614 64c6d990 2019-07-11 stsp return err;
7615 ff0d2220 2019-07-11 stsp }
7616 ff0d2220 2019-07-11 stsp
7617 ff0d2220 2019-07-11 stsp static const struct got_error *
7618 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
7619 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
7620 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7621 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
7622 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
7623 af61c510 2019-07-19 stsp {
7624 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
7625 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
7626 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
7627 af61c510 2019-07-19 stsp struct got_object_qid *qid;
7628 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
7629 af61c510 2019-07-19 stsp
7630 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
7631 af61c510 2019-07-19 stsp if (err)
7632 af61c510 2019-07-19 stsp return err;
7633 af61c510 2019-07-19 stsp
7634 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7635 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7636 af61c510 2019-07-19 stsp if (err)
7637 af61c510 2019-07-19 stsp goto done;
7638 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7639 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7640 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
7641 af61c510 2019-07-19 stsp if (err) {
7642 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7643 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7644 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
7645 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
7646 af61c510 2019-07-19 stsp "been reached?!?");
7647 ee780d5c 2020-01-04 stsp }
7648 ee780d5c 2020-01-04 stsp goto done;
7649 af61c510 2019-07-19 stsp } else {
7650 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
7651 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
7652 af61c510 2019-07-19 stsp if (err)
7653 af61c510 2019-07-19 stsp goto done;
7654 af61c510 2019-07-19 stsp
7655 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
7656 af61c510 2019-07-19 stsp if (err)
7657 af61c510 2019-07-19 stsp goto done;
7658 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7659 af61c510 2019-07-19 stsp commit_id = parent_id;
7660 af61c510 2019-07-19 stsp }
7661 af61c510 2019-07-19 stsp }
7662 af61c510 2019-07-19 stsp done:
7663 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
7664 af61c510 2019-07-19 stsp return err;
7665 af61c510 2019-07-19 stsp }
7666 af61c510 2019-07-19 stsp
7667 af61c510 2019-07-19 stsp static const struct got_error *
7668 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
7669 818c7501 2019-07-11 stsp {
7670 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
7671 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
7672 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
7673 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7674 818c7501 2019-07-11 stsp char *cwd = NULL;
7675 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
7676 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7677 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
7678 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
7679 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7680 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
7681 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7682 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
7683 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7684 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
7685 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
7686 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
7687 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
7688 818c7501 2019-07-11 stsp
7689 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
7690 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
7691 818c7501 2019-07-11 stsp
7692 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
7693 818c7501 2019-07-11 stsp switch (ch) {
7694 818c7501 2019-07-11 stsp case 'a':
7695 818c7501 2019-07-11 stsp abort_rebase = 1;
7696 818c7501 2019-07-11 stsp break;
7697 818c7501 2019-07-11 stsp case 'c':
7698 818c7501 2019-07-11 stsp continue_rebase = 1;
7699 818c7501 2019-07-11 stsp break;
7700 818c7501 2019-07-11 stsp default:
7701 818c7501 2019-07-11 stsp usage_rebase();
7702 818c7501 2019-07-11 stsp /* NOTREACHED */
7703 818c7501 2019-07-11 stsp }
7704 818c7501 2019-07-11 stsp }
7705 818c7501 2019-07-11 stsp
7706 818c7501 2019-07-11 stsp argc -= optind;
7707 818c7501 2019-07-11 stsp argv += optind;
7708 818c7501 2019-07-11 stsp
7709 43012d58 2019-07-14 stsp #ifndef PROFILE
7710 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7711 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7712 43012d58 2019-07-14 stsp err(1, "pledge");
7713 43012d58 2019-07-14 stsp #endif
7714 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
7715 818c7501 2019-07-11 stsp usage_rebase();
7716 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
7717 818c7501 2019-07-11 stsp if (argc != 0)
7718 818c7501 2019-07-11 stsp usage_rebase();
7719 818c7501 2019-07-11 stsp } else if (argc != 1)
7720 818c7501 2019-07-11 stsp usage_rebase();
7721 818c7501 2019-07-11 stsp
7722 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
7723 818c7501 2019-07-11 stsp if (cwd == NULL) {
7724 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
7725 818c7501 2019-07-11 stsp goto done;
7726 818c7501 2019-07-11 stsp }
7727 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
7728 fa51e947 2020-03-27 stsp if (error) {
7729 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7730 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "rebase", cwd);
7731 818c7501 2019-07-11 stsp goto done;
7732 fa51e947 2020-03-27 stsp }
7733 818c7501 2019-07-11 stsp
7734 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7735 c9956ddf 2019-09-08 stsp NULL);
7736 818c7501 2019-07-11 stsp if (error != NULL)
7737 818c7501 2019-07-11 stsp goto done;
7738 818c7501 2019-07-11 stsp
7739 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7740 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7741 7ef62c4e 2020-02-24 stsp if (error)
7742 7ef62c4e 2020-02-24 stsp goto done;
7743 7ef62c4e 2020-02-24 stsp
7744 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7745 7ef62c4e 2020-02-24 stsp worktree);
7746 818c7501 2019-07-11 stsp if (error)
7747 7ef62c4e 2020-02-24 stsp goto done;
7748 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
7749 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7750 818c7501 2019-07-11 stsp goto done;
7751 7ef62c4e 2020-02-24 stsp }
7752 818c7501 2019-07-11 stsp
7753 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7754 818c7501 2019-07-11 stsp if (error)
7755 818c7501 2019-07-11 stsp goto done;
7756 818c7501 2019-07-11 stsp
7757 f6794adc 2019-07-23 stsp if (abort_rebase) {
7758 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7759 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7760 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7761 f6794adc 2019-07-23 stsp goto done;
7762 f6794adc 2019-07-23 stsp }
7763 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7764 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7765 3e3a69f1 2019-07-25 stsp worktree, repo);
7766 818c7501 2019-07-11 stsp if (error)
7767 818c7501 2019-07-11 stsp goto done;
7768 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
7769 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
7770 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7771 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
7772 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
7773 818c7501 2019-07-11 stsp if (error)
7774 818c7501 2019-07-11 stsp goto done;
7775 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7776 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7777 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
7778 818c7501 2019-07-11 stsp }
7779 818c7501 2019-07-11 stsp
7780 818c7501 2019-07-11 stsp if (continue_rebase) {
7781 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7782 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7783 f6794adc 2019-07-23 stsp goto done;
7784 f6794adc 2019-07-23 stsp }
7785 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7786 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7787 3e3a69f1 2019-07-25 stsp worktree, repo);
7788 818c7501 2019-07-11 stsp if (error)
7789 818c7501 2019-07-11 stsp goto done;
7790 818c7501 2019-07-11 stsp
7791 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7792 01757395 2019-07-12 stsp resume_commit_id, repo);
7793 818c7501 2019-07-11 stsp if (error)
7794 818c7501 2019-07-11 stsp goto done;
7795 818c7501 2019-07-11 stsp
7796 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
7797 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
7798 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
7799 818c7501 2019-07-11 stsp goto done;
7800 818c7501 2019-07-11 stsp }
7801 818c7501 2019-07-11 stsp } else {
7802 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
7803 818c7501 2019-07-11 stsp if (error != NULL)
7804 818c7501 2019-07-11 stsp goto done;
7805 ff0d2220 2019-07-11 stsp }
7806 818c7501 2019-07-11 stsp
7807 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7808 ff0d2220 2019-07-11 stsp if (error)
7809 ff0d2220 2019-07-11 stsp goto done;
7810 ff0d2220 2019-07-11 stsp
7811 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
7812 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
7813 a51a74b3 2019-07-27 stsp
7814 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
7815 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7816 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
7817 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7818 818c7501 2019-07-11 stsp if (error)
7819 818c7501 2019-07-11 stsp goto done;
7820 818c7501 2019-07-11 stsp if (yca_id == NULL) {
7821 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
7822 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
7823 818c7501 2019-07-11 stsp "with work tree's branch");
7824 818c7501 2019-07-11 stsp goto done;
7825 818c7501 2019-07-11 stsp }
7826 818c7501 2019-07-11 stsp
7827 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
7828 a51a74b3 2019-07-27 stsp if (error) {
7829 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
7830 a51a74b3 2019-07-27 stsp goto done;
7831 a51a74b3 2019-07-27 stsp error = NULL;
7832 a51a74b3 2019-07-27 stsp } else {
7833 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7834 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
7835 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
7836 a51a74b3 2019-07-27 stsp goto done;
7837 a51a74b3 2019-07-27 stsp }
7838 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
7839 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
7840 818c7501 2019-07-11 stsp if (error)
7841 818c7501 2019-07-11 stsp goto done;
7842 818c7501 2019-07-11 stsp }
7843 818c7501 2019-07-11 stsp
7844 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
7845 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7846 818c7501 2019-07-11 stsp if (error)
7847 818c7501 2019-07-11 stsp goto done;
7848 818c7501 2019-07-11 stsp
7849 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7850 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
7851 fc66b545 2019-08-12 stsp if (pid == NULL) {
7852 fc66b545 2019-08-12 stsp if (!continue_rebase) {
7853 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7854 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7855 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
7856 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
7857 fc66b545 2019-08-12 stsp if (error)
7858 fc66b545 2019-08-12 stsp goto done;
7859 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
7860 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
7861 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7862 9627c110 2020-04-18 stsp
7863 fc66b545 2019-08-12 stsp }
7864 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
7865 fc66b545 2019-08-12 stsp goto done;
7866 fc66b545 2019-08-12 stsp }
7867 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
7868 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
7869 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
7870 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7871 818c7501 2019-07-11 stsp commit = NULL;
7872 818c7501 2019-07-11 stsp if (error)
7873 818c7501 2019-07-11 stsp goto done;
7874 64c6d990 2019-07-11 stsp
7875 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
7876 38b0338b 2019-11-29 stsp if (continue_rebase) {
7877 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
7878 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
7879 38b0338b 2019-11-29 stsp goto done;
7880 38b0338b 2019-11-29 stsp } else {
7881 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
7882 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
7883 38b0338b 2019-11-29 stsp char *id_str;
7884 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
7885 38b0338b 2019-11-29 stsp new_base_branch);
7886 38b0338b 2019-11-29 stsp if (error)
7887 38b0338b 2019-11-29 stsp goto done;
7888 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
7889 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
7890 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
7891 38b0338b 2019-11-29 stsp free(id_str);
7892 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
7893 38b0338b 2019-11-29 stsp new_head_commit_id);
7894 38b0338b 2019-11-29 stsp if (error)
7895 38b0338b 2019-11-29 stsp goto done;
7896 38b0338b 2019-11-29 stsp }
7897 818c7501 2019-07-11 stsp }
7898 818c7501 2019-07-11 stsp
7899 818c7501 2019-07-11 stsp pid = NULL;
7900 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
7901 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7902 9627c110 2020-04-18 stsp
7903 818c7501 2019-07-11 stsp commit_id = qid->id;
7904 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
7905 818c7501 2019-07-11 stsp pid = qid;
7906 818c7501 2019-07-11 stsp
7907 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7908 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
7909 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
7910 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7911 818c7501 2019-07-11 stsp if (error)
7912 818c7501 2019-07-11 stsp goto done;
7913 9627c110 2020-04-18 stsp
7914 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7915 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
7916 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
7917 818c7501 2019-07-11 stsp
7918 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7919 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
7920 a0ea4fc0 2020-02-28 stsp if (error)
7921 a0ea4fc0 2020-02-28 stsp goto done;
7922 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7923 818c7501 2019-07-11 stsp break;
7924 01757395 2019-07-12 stsp }
7925 818c7501 2019-07-11 stsp
7926 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7927 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
7928 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7929 818c7501 2019-07-11 stsp if (error)
7930 818c7501 2019-07-11 stsp goto done;
7931 818c7501 2019-07-11 stsp }
7932 818c7501 2019-07-11 stsp
7933 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7934 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7935 818c7501 2019-07-11 stsp if (error)
7936 818c7501 2019-07-11 stsp goto done;
7937 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7938 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
7939 818c7501 2019-07-11 stsp } else
7940 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
7941 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
7942 818c7501 2019-07-11 stsp done:
7943 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
7944 818c7501 2019-07-11 stsp free(branch_head_commit_id);
7945 818c7501 2019-07-11 stsp free(resume_commit_id);
7946 818c7501 2019-07-11 stsp free(yca_id);
7947 818c7501 2019-07-11 stsp if (commit)
7948 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7949 818c7501 2019-07-11 stsp if (branch)
7950 818c7501 2019-07-11 stsp got_ref_close(branch);
7951 818c7501 2019-07-11 stsp if (new_base_branch)
7952 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
7953 818c7501 2019-07-11 stsp if (tmp_branch)
7954 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
7955 5ef14e63 2019-06-02 stsp if (worktree)
7956 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
7957 5ef14e63 2019-06-02 stsp if (repo)
7958 5ef14e63 2019-06-02 stsp got_repo_close(repo);
7959 5ef14e63 2019-06-02 stsp return error;
7960 0ebf8283 2019-07-24 stsp }
7961 0ebf8283 2019-07-24 stsp
7962 0ebf8283 2019-07-24 stsp __dead static void
7963 0ebf8283 2019-07-24 stsp usage_histedit(void)
7964 0ebf8283 2019-07-24 stsp {
7965 466785b9 2020-12-10 jrick fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] [-F histedit-script] [-m]\n",
7966 0ebf8283 2019-07-24 stsp getprogname());
7967 0ebf8283 2019-07-24 stsp exit(1);
7968 0ebf8283 2019-07-24 stsp }
7969 0ebf8283 2019-07-24 stsp
7970 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
7971 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
7972 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
7973 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
7974 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
7975 0ebf8283 2019-07-24 stsp
7976 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
7977 0ebf8283 2019-07-24 stsp unsigned char code;
7978 0ebf8283 2019-07-24 stsp const char *name;
7979 0ebf8283 2019-07-24 stsp const char *desc;
7980 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
7981 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7982 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7983 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7984 82997472 2020-01-29 stsp "be used" },
7985 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7986 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
7987 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
7988 0ebf8283 2019-07-24 stsp };
7989 0ebf8283 2019-07-24 stsp
7990 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
7991 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7992 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
7993 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7994 0ebf8283 2019-07-24 stsp char *logmsg;
7995 0ebf8283 2019-07-24 stsp };
7996 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7997 0ebf8283 2019-07-24 stsp
7998 0ebf8283 2019-07-24 stsp static const struct got_error *
7999 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
8000 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
8001 0ebf8283 2019-07-24 stsp {
8002 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8003 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
8004 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8005 8138f3e1 2019-08-11 stsp int n;
8006 0ebf8283 2019-07-24 stsp
8007 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8008 0ebf8283 2019-07-24 stsp if (err)
8009 0ebf8283 2019-07-24 stsp goto done;
8010 0ebf8283 2019-07-24 stsp
8011 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
8012 0ebf8283 2019-07-24 stsp if (err)
8013 0ebf8283 2019-07-24 stsp goto done;
8014 0ebf8283 2019-07-24 stsp
8015 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
8016 0ebf8283 2019-07-24 stsp if (err)
8017 0ebf8283 2019-07-24 stsp goto done;
8018 0ebf8283 2019-07-24 stsp
8019 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
8020 0ebf8283 2019-07-24 stsp if (n < 0)
8021 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8022 0ebf8283 2019-07-24 stsp done:
8023 0ebf8283 2019-07-24 stsp if (commit)
8024 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8025 0ebf8283 2019-07-24 stsp free(id_str);
8026 0ebf8283 2019-07-24 stsp free(logmsg);
8027 0ebf8283 2019-07-24 stsp return err;
8028 0ebf8283 2019-07-24 stsp }
8029 0ebf8283 2019-07-24 stsp
8030 0ebf8283 2019-07-24 stsp static const struct got_error *
8031 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
8032 466785b9 2020-12-10 jrick FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
8033 0ebf8283 2019-07-24 stsp {
8034 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8035 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
8036 466785b9 2020-12-10 jrick const char *histedit_cmd = NULL;
8037 0ebf8283 2019-07-24 stsp
8038 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
8039 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
8040 0ebf8283 2019-07-24 stsp
8041 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
8042 466785b9 2020-12-10 jrick histedit_cmd = got_histedit_cmds[0].name;
8043 466785b9 2020-12-10 jrick if (fold_only && SIMPLEQ_NEXT(qid, entry) != NULL)
8044 466785b9 2020-12-10 jrick histedit_cmd = "fold";
8045 466785b9 2020-12-10 jrick err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
8046 0ebf8283 2019-07-24 stsp if (err)
8047 0ebf8283 2019-07-24 stsp break;
8048 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
8049 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
8050 083957f4 2020-02-24 stsp if (n < 0) {
8051 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
8052 083957f4 2020-02-24 stsp break;
8053 083957f4 2020-02-24 stsp }
8054 083957f4 2020-02-24 stsp }
8055 0ebf8283 2019-07-24 stsp }
8056 0ebf8283 2019-07-24 stsp
8057 0ebf8283 2019-07-24 stsp return err;
8058 0ebf8283 2019-07-24 stsp }
8059 0ebf8283 2019-07-24 stsp
8060 0ebf8283 2019-07-24 stsp static const struct got_error *
8061 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
8062 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
8063 0ebf8283 2019-07-24 stsp {
8064 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8065 6059809a 2020-12-17 stsp size_t i;
8066 6059809a 2020-12-17 stsp int n;
8067 514f2ffe 2020-01-29 stsp char *id_str;
8068 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
8069 514f2ffe 2020-01-29 stsp
8070 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
8071 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
8072 514f2ffe 2020-01-29 stsp if (err)
8073 514f2ffe 2020-01-29 stsp return err;
8074 514f2ffe 2020-01-29 stsp
8075 514f2ffe 2020-01-29 stsp n = fprintf(f,
8076 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
8077 514f2ffe 2020-01-29 stsp "# commit %s\n"
8078 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
8079 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
8080 514f2ffe 2020-01-29 stsp if (n < 0) {
8081 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
8082 514f2ffe 2020-01-29 stsp goto done;
8083 514f2ffe 2020-01-29 stsp }
8084 0ebf8283 2019-07-24 stsp
8085 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
8086 514f2ffe 2020-01-29 stsp if (n < 0) {
8087 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
8088 514f2ffe 2020-01-29 stsp goto done;
8089 514f2ffe 2020-01-29 stsp }
8090 0ebf8283 2019-07-24 stsp
8091 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
8092 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
8093 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
8094 0ebf8283 2019-07-24 stsp cmd->desc);
8095 0ebf8283 2019-07-24 stsp if (n < 0) {
8096 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8097 0ebf8283 2019-07-24 stsp break;
8098 0ebf8283 2019-07-24 stsp }
8099 0ebf8283 2019-07-24 stsp }
8100 514f2ffe 2020-01-29 stsp done:
8101 514f2ffe 2020-01-29 stsp free(id_str);
8102 0ebf8283 2019-07-24 stsp return err;
8103 0ebf8283 2019-07-24 stsp }
8104 0ebf8283 2019-07-24 stsp
8105 0ebf8283 2019-07-24 stsp static const struct got_error *
8106 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
8107 0ebf8283 2019-07-24 stsp {
8108 0ebf8283 2019-07-24 stsp static char msg[42];
8109 0ebf8283 2019-07-24 stsp int ret;
8110 0ebf8283 2019-07-24 stsp
8111 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
8112 0ebf8283 2019-07-24 stsp lineno);
8113 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
8114 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
8115 0ebf8283 2019-07-24 stsp
8116 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
8117 0ebf8283 2019-07-24 stsp }
8118 0ebf8283 2019-07-24 stsp
8119 0ebf8283 2019-07-24 stsp static const struct got_error *
8120 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
8121 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
8122 0ebf8283 2019-07-24 stsp {
8123 0ebf8283 2019-07-24 stsp const struct got_error *err;
8124 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
8125 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
8126 0ebf8283 2019-07-24 stsp
8127 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8128 0ebf8283 2019-07-24 stsp if (err)
8129 0ebf8283 2019-07-24 stsp return err;
8130 0ebf8283 2019-07-24 stsp
8131 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
8132 0ebf8283 2019-07-24 stsp if (err)
8133 0ebf8283 2019-07-24 stsp goto done;
8134 0ebf8283 2019-07-24 stsp
8135 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
8136 5943eee2 2019-08-13 stsp if (err)
8137 5943eee2 2019-08-13 stsp goto done;
8138 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
8139 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
8140 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
8141 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
8142 0ebf8283 2019-07-24 stsp }
8143 0ebf8283 2019-07-24 stsp done:
8144 0ebf8283 2019-07-24 stsp if (folded_commit)
8145 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
8146 0ebf8283 2019-07-24 stsp free(id_str);
8147 5943eee2 2019-08-13 stsp free(folded_logmsg);
8148 0ebf8283 2019-07-24 stsp return err;
8149 0ebf8283 2019-07-24 stsp }
8150 0ebf8283 2019-07-24 stsp
8151 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
8152 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
8153 0ebf8283 2019-07-24 stsp {
8154 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
8155 0ebf8283 2019-07-24 stsp
8156 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
8157 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
8158 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
8159 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
8160 3f9de99f 2019-07-24 stsp folded = prev;
8161 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
8162 0ebf8283 2019-07-24 stsp }
8163 0ebf8283 2019-07-24 stsp
8164 0ebf8283 2019-07-24 stsp return folded;
8165 0ebf8283 2019-07-24 stsp }
8166 0ebf8283 2019-07-24 stsp
8167 0ebf8283 2019-07-24 stsp static const struct got_error *
8168 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
8169 0ebf8283 2019-07-24 stsp struct got_repository *repo)
8170 0ebf8283 2019-07-24 stsp {
8171 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
8172 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
8173 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8174 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8175 1601cb9f 2020-09-11 naddy int logmsg_len;
8176 0ebf8283 2019-07-24 stsp int fd;
8177 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
8178 0ebf8283 2019-07-24 stsp
8179 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8180 0ebf8283 2019-07-24 stsp if (err)
8181 0ebf8283 2019-07-24 stsp return err;
8182 0ebf8283 2019-07-24 stsp
8183 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
8184 0ebf8283 2019-07-24 stsp if (folded) {
8185 0ebf8283 2019-07-24 stsp while (folded != hle) {
8186 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
8187 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
8188 3f9de99f 2019-07-24 stsp continue;
8189 3f9de99f 2019-07-24 stsp }
8190 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
8191 0ebf8283 2019-07-24 stsp logmsg, repo);
8192 0ebf8283 2019-07-24 stsp if (err)
8193 0ebf8283 2019-07-24 stsp goto done;
8194 0ebf8283 2019-07-24 stsp free(logmsg);
8195 0ebf8283 2019-07-24 stsp logmsg = new_msg;
8196 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
8197 0ebf8283 2019-07-24 stsp }
8198 0ebf8283 2019-07-24 stsp }
8199 0ebf8283 2019-07-24 stsp
8200 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8201 0ebf8283 2019-07-24 stsp if (err)
8202 0ebf8283 2019-07-24 stsp goto done;
8203 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
8204 5943eee2 2019-08-13 stsp if (err)
8205 5943eee2 2019-08-13 stsp goto done;
8206 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
8207 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
8208 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
8209 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
8210 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
8211 0ebf8283 2019-07-24 stsp goto done;
8212 0ebf8283 2019-07-24 stsp }
8213 0ebf8283 2019-07-24 stsp free(logmsg);
8214 0ebf8283 2019-07-24 stsp logmsg = new_msg;
8215 0ebf8283 2019-07-24 stsp
8216 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8217 0ebf8283 2019-07-24 stsp if (err)
8218 0ebf8283 2019-07-24 stsp goto done;
8219 0ebf8283 2019-07-24 stsp
8220 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
8221 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
8222 0ebf8283 2019-07-24 stsp if (err)
8223 0ebf8283 2019-07-24 stsp goto done;
8224 0ebf8283 2019-07-24 stsp
8225 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
8226 0ebf8283 2019-07-24 stsp close(fd);
8227 0ebf8283 2019-07-24 stsp
8228 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8229 0ebf8283 2019-07-24 stsp if (err)
8230 0ebf8283 2019-07-24 stsp goto done;
8231 0ebf8283 2019-07-24 stsp
8232 bfa12d5e 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
8233 0d5bb276 2020-12-15 stsp logmsg_len, 0);
8234 0ebf8283 2019-07-24 stsp if (err) {
8235 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
8236 0ebf8283 2019-07-24 stsp goto done;
8237 71392a05 2020-12-13 stsp err = NULL;
8238 71392a05 2020-12-13 stsp hle->logmsg = strdup(new_msg);
8239 71392a05 2020-12-13 stsp if (hle->logmsg == NULL)
8240 71392a05 2020-12-13 stsp err = got_error_from_errno("strdup");
8241 0ebf8283 2019-07-24 stsp }
8242 0ebf8283 2019-07-24 stsp done:
8243 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
8244 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
8245 0ebf8283 2019-07-24 stsp free(logmsg_path);
8246 0ebf8283 2019-07-24 stsp free(logmsg);
8247 5943eee2 2019-08-13 stsp free(orig_logmsg);
8248 0ebf8283 2019-07-24 stsp free(editor);
8249 0ebf8283 2019-07-24 stsp if (commit)
8250 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8251 0ebf8283 2019-07-24 stsp return err;
8252 5ef14e63 2019-06-02 stsp }
8253 0ebf8283 2019-07-24 stsp
8254 0ebf8283 2019-07-24 stsp static const struct got_error *
8255 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
8256 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
8257 0ebf8283 2019-07-24 stsp {
8258 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8259 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
8260 6059809a 2020-12-17 stsp size_t i, size;
8261 0ebf8283 2019-07-24 stsp ssize_t len;
8262 6059809a 2020-12-17 stsp int lineno = 0;
8263 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
8264 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
8265 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
8266 0ebf8283 2019-07-24 stsp
8267 0ebf8283 2019-07-24 stsp for (;;) {
8268 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
8269 0ebf8283 2019-07-24 stsp if (len == -1) {
8270 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
8271 0ebf8283 2019-07-24 stsp if (feof(f))
8272 0ebf8283 2019-07-24 stsp break;
8273 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
8274 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
8275 0ebf8283 2019-07-24 stsp break;
8276 0ebf8283 2019-07-24 stsp }
8277 0ebf8283 2019-07-24 stsp lineno++;
8278 0ebf8283 2019-07-24 stsp p = line;
8279 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8280 0ebf8283 2019-07-24 stsp p++;
8281 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
8282 0ebf8283 2019-07-24 stsp free(line);
8283 0ebf8283 2019-07-24 stsp line = NULL;
8284 0ebf8283 2019-07-24 stsp continue;
8285 0ebf8283 2019-07-24 stsp }
8286 0ebf8283 2019-07-24 stsp cmd = NULL;
8287 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
8288 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
8289 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
8290 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
8291 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
8292 0ebf8283 2019-07-24 stsp break;
8293 0ebf8283 2019-07-24 stsp }
8294 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
8295 0ebf8283 2019-07-24 stsp p++;
8296 0ebf8283 2019-07-24 stsp break;
8297 0ebf8283 2019-07-24 stsp }
8298 0ebf8283 2019-07-24 stsp }
8299 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
8300 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8301 0ebf8283 2019-07-24 stsp break;
8302 0ebf8283 2019-07-24 stsp }
8303 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8304 0ebf8283 2019-07-24 stsp p++;
8305 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
8306 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
8307 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
8308 0ebf8283 2019-07-24 stsp break;
8309 0ebf8283 2019-07-24 stsp }
8310 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
8311 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8312 0ebf8283 2019-07-24 stsp if (err)
8313 0ebf8283 2019-07-24 stsp break;
8314 0ebf8283 2019-07-24 stsp } else {
8315 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
8316 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
8317 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8318 0ebf8283 2019-07-24 stsp break;
8319 0ebf8283 2019-07-24 stsp }
8320 0ebf8283 2019-07-24 stsp }
8321 0ebf8283 2019-07-24 stsp free(line);
8322 0ebf8283 2019-07-24 stsp line = NULL;
8323 0ebf8283 2019-07-24 stsp continue;
8324 0ebf8283 2019-07-24 stsp } else {
8325 0ebf8283 2019-07-24 stsp end = p;
8326 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
8327 0ebf8283 2019-07-24 stsp end++;
8328 0ebf8283 2019-07-24 stsp *end = '\0';
8329 0ebf8283 2019-07-24 stsp
8330 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
8331 0ebf8283 2019-07-24 stsp if (err) {
8332 0ebf8283 2019-07-24 stsp /* override error code */
8333 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8334 0ebf8283 2019-07-24 stsp break;
8335 0ebf8283 2019-07-24 stsp }
8336 0ebf8283 2019-07-24 stsp }
8337 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
8338 0ebf8283 2019-07-24 stsp if (hle == NULL) {
8339 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
8340 0ebf8283 2019-07-24 stsp break;
8341 0ebf8283 2019-07-24 stsp }
8342 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
8343 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
8344 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
8345 0ebf8283 2019-07-24 stsp commit_id = NULL;
8346 0ebf8283 2019-07-24 stsp free(line);
8347 0ebf8283 2019-07-24 stsp line = NULL;
8348 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
8349 0ebf8283 2019-07-24 stsp }
8350 0ebf8283 2019-07-24 stsp
8351 0ebf8283 2019-07-24 stsp free(line);
8352 0ebf8283 2019-07-24 stsp free(commit_id);
8353 0ebf8283 2019-07-24 stsp return err;
8354 0ebf8283 2019-07-24 stsp }
8355 0ebf8283 2019-07-24 stsp
8356 0ebf8283 2019-07-24 stsp static const struct got_error *
8357 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
8358 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
8359 bfce7f83 2019-07-27 stsp {
8360 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
8361 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
8362 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8363 5b87815e 2020-03-05 stsp static char msg[92];
8364 bfce7f83 2019-07-27 stsp char *id_str;
8365 bfce7f83 2019-07-27 stsp
8366 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
8367 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
8368 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
8369 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
8370 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
8371 5b87815e 2020-03-05 stsp
8372 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8373 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
8374 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
8375 5b87815e 2020-03-05 stsp if (hle == hle2)
8376 5b87815e 2020-03-05 stsp continue;
8377 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
8378 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
8379 5b87815e 2020-03-05 stsp continue;
8380 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
8381 5b87815e 2020-03-05 stsp if (err)
8382 5b87815e 2020-03-05 stsp return err;
8383 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
8384 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
8385 5b87815e 2020-03-05 stsp free(id_str);
8386 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8387 5b87815e 2020-03-05 stsp }
8388 5b87815e 2020-03-05 stsp }
8389 bfce7f83 2019-07-27 stsp
8390 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
8391 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8392 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8393 bfce7f83 2019-07-27 stsp break;
8394 bfce7f83 2019-07-27 stsp }
8395 bfce7f83 2019-07-27 stsp if (hle == NULL) {
8396 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
8397 bfce7f83 2019-07-27 stsp if (err)
8398 bfce7f83 2019-07-27 stsp return err;
8399 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
8400 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
8401 bfce7f83 2019-07-27 stsp free(id_str);
8402 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8403 bfce7f83 2019-07-27 stsp }
8404 bfce7f83 2019-07-27 stsp }
8405 bfce7f83 2019-07-27 stsp
8406 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8407 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8408 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8409 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
8410 bfce7f83 2019-07-27 stsp
8411 bfce7f83 2019-07-27 stsp return NULL;
8412 bfce7f83 2019-07-27 stsp }
8413 bfce7f83 2019-07-27 stsp
8414 bfce7f83 2019-07-27 stsp static const struct got_error *
8415 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
8416 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
8417 bfce7f83 2019-07-27 stsp struct got_repository *repo)
8418 0ebf8283 2019-07-24 stsp {
8419 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8420 0ebf8283 2019-07-24 stsp char *editor;
8421 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8422 0ebf8283 2019-07-24 stsp
8423 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8424 0ebf8283 2019-07-24 stsp if (err)
8425 0ebf8283 2019-07-24 stsp return err;
8426 0ebf8283 2019-07-24 stsp
8427 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
8428 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
8429 0ebf8283 2019-07-24 stsp goto done;
8430 0ebf8283 2019-07-24 stsp }
8431 0ebf8283 2019-07-24 stsp
8432 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8433 0ebf8283 2019-07-24 stsp if (f == NULL) {
8434 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
8435 0ebf8283 2019-07-24 stsp goto done;
8436 0ebf8283 2019-07-24 stsp }
8437 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8438 bfce7f83 2019-07-27 stsp if (err)
8439 bfce7f83 2019-07-27 stsp goto done;
8440 bfce7f83 2019-07-27 stsp
8441 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
8442 0ebf8283 2019-07-24 stsp done:
8443 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8444 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8445 0ebf8283 2019-07-24 stsp free(editor);
8446 0ebf8283 2019-07-24 stsp return err;
8447 0ebf8283 2019-07-24 stsp }
8448 0ebf8283 2019-07-24 stsp
8449 0ebf8283 2019-07-24 stsp static const struct got_error *
8450 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8451 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
8452 514f2ffe 2020-01-29 stsp struct got_repository *);
8453 0ebf8283 2019-07-24 stsp
8454 0ebf8283 2019-07-24 stsp static const struct got_error *
8455 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
8456 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
8457 466785b9 2020-12-10 jrick int edit_logmsg_only, int fold_only, struct got_repository *repo)
8458 0ebf8283 2019-07-24 stsp {
8459 0ebf8283 2019-07-24 stsp const struct got_error *err;
8460 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8461 0ebf8283 2019-07-24 stsp char *path = NULL;
8462 0ebf8283 2019-07-24 stsp
8463 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
8464 0ebf8283 2019-07-24 stsp if (err)
8465 0ebf8283 2019-07-24 stsp return err;
8466 0ebf8283 2019-07-24 stsp
8467 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
8468 0ebf8283 2019-07-24 stsp if (err)
8469 0ebf8283 2019-07-24 stsp goto done;
8470 0ebf8283 2019-07-24 stsp
8471 466785b9 2020-12-10 jrick err = histedit_write_commit_list(commits, f, edit_logmsg_only,
8472 466785b9 2020-12-10 jrick fold_only, repo);
8473 0ebf8283 2019-07-24 stsp if (err)
8474 0ebf8283 2019-07-24 stsp goto done;
8475 0ebf8283 2019-07-24 stsp
8476 466785b9 2020-12-10 jrick if (edit_logmsg_only || fold_only) {
8477 083957f4 2020-02-24 stsp rewind(f);
8478 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8479 083957f4 2020-02-24 stsp } else {
8480 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF) {
8481 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
8482 0ebf8283 2019-07-24 stsp goto done;
8483 083957f4 2020-02-24 stsp }
8484 083957f4 2020-02-24 stsp f = NULL;
8485 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
8486 083957f4 2020-02-24 stsp if (err) {
8487 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8488 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8489 083957f4 2020-02-24 stsp goto done;
8490 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
8491 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
8492 083957f4 2020-02-24 stsp }
8493 0ebf8283 2019-07-24 stsp }
8494 0ebf8283 2019-07-24 stsp done:
8495 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8496 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8497 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
8498 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
8499 0ebf8283 2019-07-24 stsp free(path);
8500 0ebf8283 2019-07-24 stsp return err;
8501 0ebf8283 2019-07-24 stsp }
8502 0ebf8283 2019-07-24 stsp
8503 0ebf8283 2019-07-24 stsp static const struct got_error *
8504 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
8505 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8506 0ebf8283 2019-07-24 stsp {
8507 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8508 0ebf8283 2019-07-24 stsp char *path = NULL;
8509 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8510 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8511 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8512 0ebf8283 2019-07-24 stsp
8513 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
8514 0ebf8283 2019-07-24 stsp if (err)
8515 0ebf8283 2019-07-24 stsp return err;
8516 0ebf8283 2019-07-24 stsp
8517 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
8518 0ebf8283 2019-07-24 stsp if (f == NULL) {
8519 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8520 0ebf8283 2019-07-24 stsp goto done;
8521 0ebf8283 2019-07-24 stsp }
8522 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8523 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8524 0ebf8283 2019-07-24 stsp repo);
8525 0ebf8283 2019-07-24 stsp if (err)
8526 0ebf8283 2019-07-24 stsp break;
8527 0ebf8283 2019-07-24 stsp
8528 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8529 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
8530 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
8531 0ebf8283 2019-07-24 stsp if (n < 0) {
8532 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8533 0ebf8283 2019-07-24 stsp break;
8534 0ebf8283 2019-07-24 stsp }
8535 0ebf8283 2019-07-24 stsp }
8536 0ebf8283 2019-07-24 stsp }
8537 0ebf8283 2019-07-24 stsp done:
8538 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8539 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8540 0ebf8283 2019-07-24 stsp free(path);
8541 0ebf8283 2019-07-24 stsp if (commit)
8542 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8543 0ebf8283 2019-07-24 stsp return err;
8544 0ebf8283 2019-07-24 stsp }
8545 0ebf8283 2019-07-24 stsp
8546 bfce7f83 2019-07-27 stsp void
8547 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
8548 bfce7f83 2019-07-27 stsp {
8549 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8550 bfce7f83 2019-07-27 stsp
8551 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
8552 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
8553 bfce7f83 2019-07-27 stsp free(hle);
8554 bfce7f83 2019-07-27 stsp }
8555 bfce7f83 2019-07-27 stsp }
8556 bfce7f83 2019-07-27 stsp
8557 0ebf8283 2019-07-24 stsp static const struct got_error *
8558 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
8559 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
8560 0ebf8283 2019-07-24 stsp {
8561 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8562 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8563 0ebf8283 2019-07-24 stsp
8564 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8565 0ebf8283 2019-07-24 stsp if (f == NULL) {
8566 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8567 0ebf8283 2019-07-24 stsp goto done;
8568 0ebf8283 2019-07-24 stsp }
8569 0ebf8283 2019-07-24 stsp
8570 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8571 0ebf8283 2019-07-24 stsp done:
8572 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8573 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8574 0ebf8283 2019-07-24 stsp return err;
8575 0ebf8283 2019-07-24 stsp }
8576 0ebf8283 2019-07-24 stsp
8577 0ebf8283 2019-07-24 stsp static const struct got_error *
8578 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8579 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
8580 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
8581 0ebf8283 2019-07-24 stsp {
8582 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
8583 0ebf8283 2019-07-24 stsp int resp = ' ';
8584 0ebf8283 2019-07-24 stsp
8585 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
8586 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8587 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
8588 0ebf8283 2019-07-24 stsp resp = getchar();
8589 a61a4414 2019-08-07 stsp if (resp == '\n')
8590 a61a4414 2019-08-07 stsp resp = getchar();
8591 426ebf2e 2019-07-25 stsp if (resp == 'c') {
8592 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8593 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
8594 bfce7f83 2019-07-27 stsp repo);
8595 426ebf2e 2019-07-25 stsp if (err) {
8596 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8597 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8598 426ebf2e 2019-07-25 stsp break;
8599 bfce7f83 2019-07-27 stsp prev_err = err;
8600 426ebf2e 2019-07-25 stsp resp = ' ';
8601 426ebf2e 2019-07-25 stsp continue;
8602 426ebf2e 2019-07-25 stsp }
8603 bfce7f83 2019-07-27 stsp break;
8604 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
8605 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8606 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
8607 466785b9 2020-12-10 jrick commits, branch_name, 0, 0, repo);
8608 426ebf2e 2019-07-25 stsp if (err) {
8609 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8610 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8611 426ebf2e 2019-07-25 stsp break;
8612 bfce7f83 2019-07-27 stsp prev_err = err;
8613 426ebf2e 2019-07-25 stsp resp = ' ';
8614 426ebf2e 2019-07-25 stsp continue;
8615 426ebf2e 2019-07-25 stsp }
8616 bfce7f83 2019-07-27 stsp break;
8617 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
8618 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8619 0ebf8283 2019-07-24 stsp break;
8620 426ebf2e 2019-07-25 stsp } else
8621 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
8622 0ebf8283 2019-07-24 stsp }
8623 0ebf8283 2019-07-24 stsp
8624 0ebf8283 2019-07-24 stsp return err;
8625 0ebf8283 2019-07-24 stsp }
8626 0ebf8283 2019-07-24 stsp
8627 0ebf8283 2019-07-24 stsp static const struct got_error *
8628 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
8629 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8630 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
8631 0ebf8283 2019-07-24 stsp {
8632 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8633 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8634 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8635 3e3a69f1 2019-07-25 stsp branch, repo);
8636 0ebf8283 2019-07-24 stsp }
8637 0ebf8283 2019-07-24 stsp
8638 0ebf8283 2019-07-24 stsp static const struct got_error *
8639 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
8640 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8641 0ebf8283 2019-07-24 stsp {
8642 0ebf8283 2019-07-24 stsp const struct got_error *err;
8643 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8644 0ebf8283 2019-07-24 stsp
8645 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
8646 0ebf8283 2019-07-24 stsp if (err)
8647 0ebf8283 2019-07-24 stsp goto done;
8648 0ebf8283 2019-07-24 stsp
8649 0ebf8283 2019-07-24 stsp if (new_id) {
8650 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
8651 0ebf8283 2019-07-24 stsp if (err)
8652 0ebf8283 2019-07-24 stsp goto done;
8653 0ebf8283 2019-07-24 stsp }
8654 0ebf8283 2019-07-24 stsp
8655 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
8656 0ebf8283 2019-07-24 stsp if (new_id_str)
8657 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
8658 0ebf8283 2019-07-24 stsp
8659 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8660 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
8661 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
8662 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8663 0ebf8283 2019-07-24 stsp goto done;
8664 0ebf8283 2019-07-24 stsp }
8665 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
8666 0ebf8283 2019-07-24 stsp } else {
8667 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8668 0ebf8283 2019-07-24 stsp if (err)
8669 0ebf8283 2019-07-24 stsp goto done;
8670 0ebf8283 2019-07-24 stsp }
8671 0ebf8283 2019-07-24 stsp
8672 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
8673 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
8674 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
8675 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
8676 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8677 0ebf8283 2019-07-24 stsp break;
8678 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
8679 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
8680 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8681 0ebf8283 2019-07-24 stsp logmsg);
8682 0ebf8283 2019-07-24 stsp break;
8683 0ebf8283 2019-07-24 stsp default:
8684 0ebf8283 2019-07-24 stsp break;
8685 0ebf8283 2019-07-24 stsp }
8686 0ebf8283 2019-07-24 stsp done:
8687 0ebf8283 2019-07-24 stsp free(old_id_str);
8688 0ebf8283 2019-07-24 stsp free(new_id_str);
8689 0ebf8283 2019-07-24 stsp return err;
8690 0ebf8283 2019-07-24 stsp }
8691 0ebf8283 2019-07-24 stsp
8692 0ebf8283 2019-07-24 stsp static const struct got_error *
8693 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
8694 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8695 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8696 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
8697 0ebf8283 2019-07-24 stsp {
8698 0ebf8283 2019-07-24 stsp const struct got_error *err;
8699 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8700 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
8701 0ebf8283 2019-07-24 stsp
8702 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8703 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
8704 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8705 0ebf8283 2019-07-24 stsp if (err)
8706 0ebf8283 2019-07-24 stsp return err;
8707 0ebf8283 2019-07-24 stsp }
8708 0ebf8283 2019-07-24 stsp
8709 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8710 0ebf8283 2019-07-24 stsp if (err)
8711 0ebf8283 2019-07-24 stsp return err;
8712 0ebf8283 2019-07-24 stsp
8713 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8714 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
8715 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
8716 0ebf8283 2019-07-24 stsp if (err) {
8717 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8718 0ebf8283 2019-07-24 stsp goto done;
8719 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
8720 0ebf8283 2019-07-24 stsp } else {
8721 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
8722 0ebf8283 2019-07-24 stsp free(new_commit_id);
8723 0ebf8283 2019-07-24 stsp }
8724 0ebf8283 2019-07-24 stsp done:
8725 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8726 0ebf8283 2019-07-24 stsp return err;
8727 0ebf8283 2019-07-24 stsp }
8728 0ebf8283 2019-07-24 stsp
8729 0ebf8283 2019-07-24 stsp static const struct got_error *
8730 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
8731 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8732 0ebf8283 2019-07-24 stsp {
8733 0ebf8283 2019-07-24 stsp const struct got_error *error;
8734 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8735 0ebf8283 2019-07-24 stsp
8736 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8737 0ebf8283 2019-07-24 stsp repo);
8738 0ebf8283 2019-07-24 stsp if (error)
8739 0ebf8283 2019-07-24 stsp return error;
8740 0ebf8283 2019-07-24 stsp
8741 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8742 0ebf8283 2019-07-24 stsp if (error)
8743 0ebf8283 2019-07-24 stsp return error;
8744 0ebf8283 2019-07-24 stsp
8745 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
8746 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8747 0ebf8283 2019-07-24 stsp return error;
8748 ab20a43a 2020-01-29 stsp }
8749 ab20a43a 2020-01-29 stsp
8750 ab20a43a 2020-01-29 stsp static const struct got_error *
8751 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
8752 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
8753 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8754 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8755 ab20a43a 2020-01-29 stsp {
8756 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
8757 ab20a43a 2020-01-29 stsp
8758 ab20a43a 2020-01-29 stsp switch (status) {
8759 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8760 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8761 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8762 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
8763 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8764 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8765 ab20a43a 2020-01-29 stsp default:
8766 ab20a43a 2020-01-29 stsp break;
8767 ab20a43a 2020-01-29 stsp }
8768 ab20a43a 2020-01-29 stsp
8769 ab20a43a 2020-01-29 stsp switch (staged_status) {
8770 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8771 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8772 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8773 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8774 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8775 ab20a43a 2020-01-29 stsp default:
8776 ab20a43a 2020-01-29 stsp break;
8777 ab20a43a 2020-01-29 stsp }
8778 ab20a43a 2020-01-29 stsp
8779 ab20a43a 2020-01-29 stsp return NULL;
8780 0ebf8283 2019-07-24 stsp }
8781 0ebf8283 2019-07-24 stsp
8782 0ebf8283 2019-07-24 stsp static const struct got_error *
8783 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
8784 0ebf8283 2019-07-24 stsp {
8785 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
8786 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
8787 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8788 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
8789 0ebf8283 2019-07-24 stsp char *cwd = NULL;
8790 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
8791 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
8792 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
8793 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
8794 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
8795 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8796 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
8797 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8798 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8799 466785b9 2020-12-10 jrick int edit_logmsg_only = 0, fold_only = 0;
8800 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
8801 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8802 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
8803 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
8804 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
8805 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
8806 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
8807 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8808 0ebf8283 2019-07-24 stsp
8809 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
8810 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
8811 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
8812 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8813 0ebf8283 2019-07-24 stsp
8814 466785b9 2020-12-10 jrick while ((ch = getopt(argc, argv, "acfF:m")) != -1) {
8815 0ebf8283 2019-07-24 stsp switch (ch) {
8816 0ebf8283 2019-07-24 stsp case 'a':
8817 0ebf8283 2019-07-24 stsp abort_edit = 1;
8818 0ebf8283 2019-07-24 stsp break;
8819 0ebf8283 2019-07-24 stsp case 'c':
8820 0ebf8283 2019-07-24 stsp continue_edit = 1;
8821 0ebf8283 2019-07-24 stsp break;
8822 466785b9 2020-12-10 jrick case 'f':
8823 466785b9 2020-12-10 jrick fold_only = 1;
8824 466785b9 2020-12-10 jrick break;
8825 0ebf8283 2019-07-24 stsp case 'F':
8826 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
8827 0ebf8283 2019-07-24 stsp break;
8828 083957f4 2020-02-24 stsp case 'm':
8829 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
8830 083957f4 2020-02-24 stsp break;
8831 0ebf8283 2019-07-24 stsp default:
8832 0ebf8283 2019-07-24 stsp usage_histedit();
8833 0ebf8283 2019-07-24 stsp /* NOTREACHED */
8834 0ebf8283 2019-07-24 stsp }
8835 0ebf8283 2019-07-24 stsp }
8836 0ebf8283 2019-07-24 stsp
8837 0ebf8283 2019-07-24 stsp argc -= optind;
8838 0ebf8283 2019-07-24 stsp argv += optind;
8839 0ebf8283 2019-07-24 stsp
8840 0ebf8283 2019-07-24 stsp #ifndef PROFILE
8841 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8842 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
8843 0ebf8283 2019-07-24 stsp err(1, "pledge");
8844 0ebf8283 2019-07-24 stsp #endif
8845 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
8846 ff69268e 2020-12-13 stsp option_conflict('a', 'c');
8847 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
8848 ff69268e 2020-12-13 stsp option_conflict('F', 'm');
8849 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
8850 ff69268e 2020-12-13 stsp option_conflict('a', 'm');
8851 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
8852 ff69268e 2020-12-13 stsp option_conflict('c', 'm');
8853 466785b9 2020-12-10 jrick if (abort_edit && fold_only)
8854 ff69268e 2020-12-13 stsp option_conflict('a', 'f');
8855 ff69268e 2020-12-13 stsp if (continue_edit && fold_only)
8856 ff69268e 2020-12-13 stsp option_conflict('c', 'f');
8857 466785b9 2020-12-10 jrick if (fold_only && edit_logmsg_only)
8858 ff69268e 2020-12-13 stsp option_conflict('f', 'm');
8859 b3805337 2020-12-13 stsp if (edit_script_path && fold_only)
8860 b3805337 2020-12-13 stsp option_conflict('F', 'f');
8861 0ebf8283 2019-07-24 stsp if (argc != 0)
8862 0ebf8283 2019-07-24 stsp usage_histedit();
8863 0ebf8283 2019-07-24 stsp
8864 0ebf8283 2019-07-24 stsp /*
8865 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
8866 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
8867 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
8868 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
8869 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
8870 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
8871 0ebf8283 2019-07-24 stsp */
8872 0ebf8283 2019-07-24 stsp
8873 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
8874 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
8875 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
8876 0ebf8283 2019-07-24 stsp goto done;
8877 0ebf8283 2019-07-24 stsp }
8878 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
8879 fa51e947 2020-03-27 stsp if (error) {
8880 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8881 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "histedit", cwd);
8882 0ebf8283 2019-07-24 stsp goto done;
8883 fa51e947 2020-03-27 stsp }
8884 0ebf8283 2019-07-24 stsp
8885 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8886 c9956ddf 2019-09-08 stsp NULL);
8887 0ebf8283 2019-07-24 stsp if (error != NULL)
8888 0ebf8283 2019-07-24 stsp goto done;
8889 0ebf8283 2019-07-24 stsp
8890 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8891 0ebf8283 2019-07-24 stsp if (error)
8892 0ebf8283 2019-07-24 stsp goto done;
8893 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
8894 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
8895 0ebf8283 2019-07-24 stsp goto done;
8896 0ebf8283 2019-07-24 stsp }
8897 0ebf8283 2019-07-24 stsp
8898 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8899 0ebf8283 2019-07-24 stsp if (error)
8900 0ebf8283 2019-07-24 stsp goto done;
8901 0ebf8283 2019-07-24 stsp
8902 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
8903 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8904 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
8905 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
8906 083957f4 2020-02-24 stsp "before the -m option can be used");
8907 083957f4 2020-02-24 stsp goto done;
8908 083957f4 2020-02-24 stsp }
8909 466785b9 2020-12-10 jrick if (edit_in_progress && fold_only) {
8910 466785b9 2020-12-10 jrick error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8911 466785b9 2020-12-10 jrick "histedit operation is in progress in this "
8912 466785b9 2020-12-10 jrick "work tree and must be continued or aborted "
8913 466785b9 2020-12-10 jrick "before the -f option can be used");
8914 466785b9 2020-12-10 jrick goto done;
8915 466785b9 2020-12-10 jrick }
8916 083957f4 2020-02-24 stsp
8917 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
8918 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8919 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8920 3e3a69f1 2019-07-25 stsp worktree, repo);
8921 0ebf8283 2019-07-24 stsp if (error)
8922 0ebf8283 2019-07-24 stsp goto done;
8923 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8924 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8925 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
8926 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
8927 0ebf8283 2019-07-24 stsp if (error)
8928 0ebf8283 2019-07-24 stsp goto done;
8929 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
8930 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8931 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8932 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
8933 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
8934 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8935 0ebf8283 2019-07-24 stsp goto done;
8936 0ebf8283 2019-07-24 stsp }
8937 0ebf8283 2019-07-24 stsp
8938 0ebf8283 2019-07-24 stsp if (continue_edit) {
8939 0ebf8283 2019-07-24 stsp char *path;
8940 0ebf8283 2019-07-24 stsp
8941 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
8942 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8943 0ebf8283 2019-07-24 stsp goto done;
8944 0ebf8283 2019-07-24 stsp }
8945 0ebf8283 2019-07-24 stsp
8946 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
8947 0ebf8283 2019-07-24 stsp if (error)
8948 0ebf8283 2019-07-24 stsp goto done;
8949 0ebf8283 2019-07-24 stsp
8950 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8951 0ebf8283 2019-07-24 stsp free(path);
8952 0ebf8283 2019-07-24 stsp if (error)
8953 0ebf8283 2019-07-24 stsp goto done;
8954 0ebf8283 2019-07-24 stsp
8955 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8956 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8957 3e3a69f1 2019-07-25 stsp worktree, repo);
8958 0ebf8283 2019-07-24 stsp if (error)
8959 0ebf8283 2019-07-24 stsp goto done;
8960 0ebf8283 2019-07-24 stsp
8961 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8962 0ebf8283 2019-07-24 stsp if (error)
8963 0ebf8283 2019-07-24 stsp goto done;
8964 0ebf8283 2019-07-24 stsp
8965 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8966 0ebf8283 2019-07-24 stsp head_commit_id);
8967 0ebf8283 2019-07-24 stsp if (error)
8968 0ebf8283 2019-07-24 stsp goto done;
8969 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8970 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8971 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8972 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8973 3aac7cf7 2019-07-25 stsp goto done;
8974 3aac7cf7 2019-07-25 stsp }
8975 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8976 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8977 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8978 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8979 0ebf8283 2019-07-24 stsp commit = NULL;
8980 0ebf8283 2019-07-24 stsp if (error)
8981 0ebf8283 2019-07-24 stsp goto done;
8982 0ebf8283 2019-07-24 stsp } else {
8983 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
8984 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8985 0ebf8283 2019-07-24 stsp goto done;
8986 0ebf8283 2019-07-24 stsp }
8987 0ebf8283 2019-07-24 stsp
8988 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
8989 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
8990 0ebf8283 2019-07-24 stsp if (error != NULL)
8991 0ebf8283 2019-07-24 stsp goto done;
8992 0ebf8283 2019-07-24 stsp
8993 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8994 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8995 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
8996 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
8997 c7d20a3f 2019-07-30 stsp goto done;
8998 c7d20a3f 2019-07-30 stsp }
8999 c7d20a3f 2019-07-30 stsp
9000 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
9001 bfce7f83 2019-07-27 stsp got_ref_close(branch);
9002 bfce7f83 2019-07-27 stsp branch = NULL;
9003 0ebf8283 2019-07-24 stsp if (error)
9004 0ebf8283 2019-07-24 stsp goto done;
9005 0ebf8283 2019-07-24 stsp
9006 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
9007 0ebf8283 2019-07-24 stsp head_commit_id);
9008 0ebf8283 2019-07-24 stsp if (error)
9009 0ebf8283 2019-07-24 stsp goto done;
9010 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9011 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
9012 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
9013 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9014 3aac7cf7 2019-07-25 stsp goto done;
9015 3aac7cf7 2019-07-25 stsp }
9016 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
9017 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
9018 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
9019 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
9020 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9021 0ebf8283 2019-07-24 stsp commit = NULL;
9022 0ebf8283 2019-07-24 stsp if (error)
9023 0ebf8283 2019-07-24 stsp goto done;
9024 0ebf8283 2019-07-24 stsp
9025 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
9026 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9027 c5996fff 2020-01-29 stsp goto done;
9028 c5996fff 2020-01-29 stsp }
9029 c5996fff 2020-01-29 stsp
9030 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
9031 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
9032 bfce7f83 2019-07-27 stsp if (error)
9033 bfce7f83 2019-07-27 stsp goto done;
9034 bfce7f83 2019-07-27 stsp
9035 0ebf8283 2019-07-24 stsp if (edit_script_path) {
9036 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
9037 0ebf8283 2019-07-24 stsp edit_script_path, repo);
9038 6ba2bea2 2019-07-27 stsp if (error) {
9039 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
9040 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
9041 9627c110 2020-04-18 stsp update_progress, &upa);
9042 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9043 0ebf8283 2019-07-24 stsp goto done;
9044 6ba2bea2 2019-07-27 stsp }
9045 0ebf8283 2019-07-24 stsp } else {
9046 514f2ffe 2020-01-29 stsp const char *branch_name;
9047 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
9048 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
9049 514f2ffe 2020-01-29 stsp branch_name += 11;
9050 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
9051 466785b9 2020-12-10 jrick branch_name, edit_logmsg_only, fold_only, repo);
9052 6ba2bea2 2019-07-27 stsp if (error) {
9053 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
9054 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
9055 9627c110 2020-04-18 stsp update_progress, &upa);
9056 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9057 0ebf8283 2019-07-24 stsp goto done;
9058 6ba2bea2 2019-07-27 stsp }
9059 0ebf8283 2019-07-24 stsp
9060 0ebf8283 2019-07-24 stsp }
9061 0ebf8283 2019-07-24 stsp
9062 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
9063 0ebf8283 2019-07-24 stsp repo);
9064 6ba2bea2 2019-07-27 stsp if (error) {
9065 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
9066 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
9067 9627c110 2020-04-18 stsp update_progress, &upa);
9068 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9069 0ebf8283 2019-07-24 stsp goto done;
9070 6ba2bea2 2019-07-27 stsp }
9071 0ebf8283 2019-07-24 stsp
9072 0ebf8283 2019-07-24 stsp }
9073 0ebf8283 2019-07-24 stsp
9074 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
9075 4ec14e60 2019-08-28 hiltjo if (error)
9076 0ebf8283 2019-07-24 stsp goto done;
9077 0ebf8283 2019-07-24 stsp
9078 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
9079 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
9080 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
9081 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
9082 0ebf8283 2019-07-24 stsp continue;
9083 0ebf8283 2019-07-24 stsp
9084 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
9085 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
9086 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
9087 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
9088 62d463ca 2020-10-20 naddy repo);
9089 ab20a43a 2020-01-29 stsp if (error)
9090 ab20a43a 2020-01-29 stsp goto done;
9091 0ebf8283 2019-07-24 stsp } else {
9092 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
9093 ab20a43a 2020-01-29 stsp int have_changes = 0;
9094 ab20a43a 2020-01-29 stsp
9095 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
9096 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
9097 ab20a43a 2020-01-29 stsp if (error)
9098 ab20a43a 2020-01-29 stsp goto done;
9099 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
9100 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
9101 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
9102 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
9103 ab20a43a 2020-01-29 stsp if (error) {
9104 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
9105 ab20a43a 2020-01-29 stsp goto done;
9106 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
9107 ab20a43a 2020-01-29 stsp goto done;
9108 ab20a43a 2020-01-29 stsp }
9109 ab20a43a 2020-01-29 stsp if (have_changes) {
9110 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
9111 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
9112 ab20a43a 2020-01-29 stsp if (error)
9113 ab20a43a 2020-01-29 stsp goto done;
9114 ab20a43a 2020-01-29 stsp } else {
9115 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
9116 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
9117 ab20a43a 2020-01-29 stsp if (error)
9118 ab20a43a 2020-01-29 stsp goto done;
9119 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
9120 ab20a43a 2020-01-29 stsp hle, NULL);
9121 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
9122 ab20a43a 2020-01-29 stsp commit = NULL;
9123 ab20a43a 2020-01-29 stsp if (error)
9124 ab20a43a 2020-01-29 stsp goto done;
9125 ab20a43a 2020-01-29 stsp }
9126 0ebf8283 2019-07-24 stsp }
9127 0ebf8283 2019-07-24 stsp continue;
9128 0ebf8283 2019-07-24 stsp }
9129 0ebf8283 2019-07-24 stsp
9130 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
9131 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
9132 0ebf8283 2019-07-24 stsp if (error)
9133 0ebf8283 2019-07-24 stsp goto done;
9134 0ebf8283 2019-07-24 stsp continue;
9135 0ebf8283 2019-07-24 stsp }
9136 0ebf8283 2019-07-24 stsp
9137 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
9138 0ebf8283 2019-07-24 stsp hle->commit_id);
9139 0ebf8283 2019-07-24 stsp if (error)
9140 0ebf8283 2019-07-24 stsp goto done;
9141 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9142 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
9143 0ebf8283 2019-07-24 stsp
9144 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
9145 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
9146 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
9147 0ebf8283 2019-07-24 stsp if (error)
9148 0ebf8283 2019-07-24 stsp goto done;
9149 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9150 0ebf8283 2019-07-24 stsp commit = NULL;
9151 0ebf8283 2019-07-24 stsp
9152 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9153 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
9154 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
9155 9627c110 2020-04-18 stsp
9156 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
9157 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
9158 a0ea4fc0 2020-02-28 stsp repo);
9159 a0ea4fc0 2020-02-28 stsp if (error)
9160 a0ea4fc0 2020-02-28 stsp goto done;
9161 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9162 0ebf8283 2019-07-24 stsp break;
9163 0ebf8283 2019-07-24 stsp }
9164 0ebf8283 2019-07-24 stsp
9165 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
9166 0ebf8283 2019-07-24 stsp char *id_str;
9167 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
9168 0ebf8283 2019-07-24 stsp if (error)
9169 0ebf8283 2019-07-24 stsp goto done;
9170 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
9171 0ebf8283 2019-07-24 stsp id_str);
9172 0ebf8283 2019-07-24 stsp free(id_str);
9173 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9174 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
9175 3e3a69f1 2019-07-25 stsp fileindex);
9176 0ebf8283 2019-07-24 stsp goto done;
9177 0ebf8283 2019-07-24 stsp }
9178 0ebf8283 2019-07-24 stsp
9179 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
9180 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
9181 0ebf8283 2019-07-24 stsp if (error)
9182 0ebf8283 2019-07-24 stsp goto done;
9183 0ebf8283 2019-07-24 stsp continue;
9184 0ebf8283 2019-07-24 stsp }
9185 0ebf8283 2019-07-24 stsp
9186 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
9187 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
9188 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9189 0ebf8283 2019-07-24 stsp if (error)
9190 0ebf8283 2019-07-24 stsp goto done;
9191 0ebf8283 2019-07-24 stsp }
9192 0ebf8283 2019-07-24 stsp
9193 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
9194 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
9195 0ebf8283 2019-07-24 stsp if (error)
9196 0ebf8283 2019-07-24 stsp goto done;
9197 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
9198 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
9199 0ebf8283 2019-07-24 stsp } else
9200 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
9201 3e3a69f1 2019-07-25 stsp branch, repo);
9202 0ebf8283 2019-07-24 stsp done:
9203 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
9204 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
9205 0ebf8283 2019-07-24 stsp free(head_commit_id);
9206 0ebf8283 2019-07-24 stsp free(base_commit_id);
9207 0ebf8283 2019-07-24 stsp free(resume_commit_id);
9208 0ebf8283 2019-07-24 stsp if (commit)
9209 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9210 0ebf8283 2019-07-24 stsp if (branch)
9211 0ebf8283 2019-07-24 stsp got_ref_close(branch);
9212 0ebf8283 2019-07-24 stsp if (tmp_branch)
9213 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
9214 0ebf8283 2019-07-24 stsp if (worktree)
9215 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
9216 2822a352 2019-10-15 stsp if (repo)
9217 2822a352 2019-10-15 stsp got_repo_close(repo);
9218 2822a352 2019-10-15 stsp return error;
9219 2822a352 2019-10-15 stsp }
9220 2822a352 2019-10-15 stsp
9221 2822a352 2019-10-15 stsp __dead static void
9222 2822a352 2019-10-15 stsp usage_integrate(void)
9223 2822a352 2019-10-15 stsp {
9224 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
9225 2822a352 2019-10-15 stsp exit(1);
9226 2822a352 2019-10-15 stsp }
9227 2822a352 2019-10-15 stsp
9228 2822a352 2019-10-15 stsp static const struct got_error *
9229 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
9230 2822a352 2019-10-15 stsp {
9231 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
9232 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
9233 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
9234 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
9235 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
9236 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
9237 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
9238 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
9239 9627c110 2020-04-18 stsp int ch;
9240 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9241 2822a352 2019-10-15 stsp
9242 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9243 2822a352 2019-10-15 stsp switch (ch) {
9244 2822a352 2019-10-15 stsp default:
9245 2822a352 2019-10-15 stsp usage_integrate();
9246 2822a352 2019-10-15 stsp /* NOTREACHED */
9247 2822a352 2019-10-15 stsp }
9248 2822a352 2019-10-15 stsp }
9249 2822a352 2019-10-15 stsp
9250 2822a352 2019-10-15 stsp argc -= optind;
9251 2822a352 2019-10-15 stsp argv += optind;
9252 2822a352 2019-10-15 stsp
9253 2822a352 2019-10-15 stsp if (argc != 1)
9254 2822a352 2019-10-15 stsp usage_integrate();
9255 2822a352 2019-10-15 stsp branch_arg = argv[0];
9256 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
9257 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9258 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
9259 2822a352 2019-10-15 stsp err(1, "pledge");
9260 b08a0ccd 2020-09-24 stsp #endif
9261 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
9262 2822a352 2019-10-15 stsp if (cwd == NULL) {
9263 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
9264 2822a352 2019-10-15 stsp goto done;
9265 2822a352 2019-10-15 stsp }
9266 2822a352 2019-10-15 stsp
9267 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
9268 fa51e947 2020-03-27 stsp if (error) {
9269 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9270 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
9271 fa51e947 2020-03-27 stsp cwd);
9272 2822a352 2019-10-15 stsp goto done;
9273 fa51e947 2020-03-27 stsp }
9274 2822a352 2019-10-15 stsp
9275 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
9276 2822a352 2019-10-15 stsp if (error)
9277 2822a352 2019-10-15 stsp goto done;
9278 2822a352 2019-10-15 stsp
9279 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9280 2822a352 2019-10-15 stsp NULL);
9281 2822a352 2019-10-15 stsp if (error != NULL)
9282 2822a352 2019-10-15 stsp goto done;
9283 2822a352 2019-10-15 stsp
9284 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9285 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
9286 2822a352 2019-10-15 stsp if (error)
9287 2822a352 2019-10-15 stsp goto done;
9288 2822a352 2019-10-15 stsp
9289 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
9290 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
9291 2822a352 2019-10-15 stsp goto done;
9292 2822a352 2019-10-15 stsp }
9293 2822a352 2019-10-15 stsp
9294 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
9295 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
9296 2822a352 2019-10-15 stsp if (error)
9297 2822a352 2019-10-15 stsp goto done;
9298 2822a352 2019-10-15 stsp
9299 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
9300 2822a352 2019-10-15 stsp if (refname == NULL) {
9301 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9302 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9303 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9304 2822a352 2019-10-15 stsp goto done;
9305 2822a352 2019-10-15 stsp }
9306 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
9307 2822a352 2019-10-15 stsp if (base_refname == NULL) {
9308 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9309 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9310 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9311 2822a352 2019-10-15 stsp goto done;
9312 2822a352 2019-10-15 stsp }
9313 2822a352 2019-10-15 stsp
9314 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
9315 2822a352 2019-10-15 stsp if (error)
9316 2822a352 2019-10-15 stsp goto done;
9317 2822a352 2019-10-15 stsp
9318 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
9319 2822a352 2019-10-15 stsp if (error)
9320 2822a352 2019-10-15 stsp goto done;
9321 2822a352 2019-10-15 stsp
9322 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
9323 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
9324 2822a352 2019-10-15 stsp "specified branch has already been integrated");
9325 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9326 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9327 2822a352 2019-10-15 stsp goto done;
9328 2822a352 2019-10-15 stsp }
9329 2822a352 2019-10-15 stsp
9330 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
9331 2822a352 2019-10-15 stsp if (error) {
9332 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
9333 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
9334 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9335 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9336 2822a352 2019-10-15 stsp goto done;
9337 2822a352 2019-10-15 stsp }
9338 2822a352 2019-10-15 stsp
9339 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9340 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
9341 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
9342 2822a352 2019-10-15 stsp check_cancelled, NULL);
9343 2822a352 2019-10-15 stsp if (error)
9344 2822a352 2019-10-15 stsp goto done;
9345 2822a352 2019-10-15 stsp
9346 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
9347 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9348 2822a352 2019-10-15 stsp done:
9349 715dc77e 2019-08-03 stsp if (repo)
9350 715dc77e 2019-08-03 stsp got_repo_close(repo);
9351 2822a352 2019-10-15 stsp if (worktree)
9352 2822a352 2019-10-15 stsp got_worktree_close(worktree);
9353 2822a352 2019-10-15 stsp free(cwd);
9354 2822a352 2019-10-15 stsp free(base_commit_id);
9355 2822a352 2019-10-15 stsp free(commit_id);
9356 2822a352 2019-10-15 stsp free(refname);
9357 2822a352 2019-10-15 stsp free(base_refname);
9358 715dc77e 2019-08-03 stsp return error;
9359 715dc77e 2019-08-03 stsp }
9360 715dc77e 2019-08-03 stsp
9361 715dc77e 2019-08-03 stsp __dead static void
9362 715dc77e 2019-08-03 stsp usage_stage(void)
9363 715dc77e 2019-08-03 stsp {
9364 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
9365 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
9366 715dc77e 2019-08-03 stsp getprogname());
9367 715dc77e 2019-08-03 stsp exit(1);
9368 715dc77e 2019-08-03 stsp }
9369 715dc77e 2019-08-03 stsp
9370 715dc77e 2019-08-03 stsp static const struct got_error *
9371 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
9372 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
9373 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
9374 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
9375 408b4ebc 2019-08-03 stsp {
9376 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
9377 408b4ebc 2019-08-03 stsp char *id_str = NULL;
9378 408b4ebc 2019-08-03 stsp
9379 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
9380 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
9381 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
9382 408b4ebc 2019-08-03 stsp return NULL;
9383 408b4ebc 2019-08-03 stsp
9384 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
9385 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
9386 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
9387 408b4ebc 2019-08-03 stsp else
9388 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
9389 408b4ebc 2019-08-03 stsp if (err)
9390 408b4ebc 2019-08-03 stsp return err;
9391 408b4ebc 2019-08-03 stsp
9392 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
9393 408b4ebc 2019-08-03 stsp free(id_str);
9394 dc424a06 2019-08-07 stsp return NULL;
9395 dc424a06 2019-08-07 stsp }
9396 2e1f37b0 2019-08-08 stsp
9397 dc424a06 2019-08-07 stsp static const struct got_error *
9398 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
9399 715dc77e 2019-08-03 stsp {
9400 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
9401 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
9402 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
9403 715dc77e 2019-08-03 stsp char *cwd = NULL;
9404 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
9405 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
9406 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
9407 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
9408 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9409 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9410 715dc77e 2019-08-03 stsp
9411 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
9412 715dc77e 2019-08-03 stsp
9413 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9414 715dc77e 2019-08-03 stsp switch (ch) {
9415 408b4ebc 2019-08-03 stsp case 'l':
9416 408b4ebc 2019-08-03 stsp list_stage = 1;
9417 408b4ebc 2019-08-03 stsp break;
9418 dc424a06 2019-08-07 stsp case 'p':
9419 dc424a06 2019-08-07 stsp pflag = 1;
9420 dc424a06 2019-08-07 stsp break;
9421 dc424a06 2019-08-07 stsp case 'F':
9422 dc424a06 2019-08-07 stsp patch_script_path = optarg;
9423 dc424a06 2019-08-07 stsp break;
9424 35213c7c 2020-07-23 stsp case 'S':
9425 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
9426 35213c7c 2020-07-23 stsp break;
9427 715dc77e 2019-08-03 stsp default:
9428 715dc77e 2019-08-03 stsp usage_stage();
9429 715dc77e 2019-08-03 stsp /* NOTREACHED */
9430 715dc77e 2019-08-03 stsp }
9431 715dc77e 2019-08-03 stsp }
9432 715dc77e 2019-08-03 stsp
9433 715dc77e 2019-08-03 stsp argc -= optind;
9434 715dc77e 2019-08-03 stsp argv += optind;
9435 715dc77e 2019-08-03 stsp
9436 715dc77e 2019-08-03 stsp #ifndef PROFILE
9437 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9438 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
9439 715dc77e 2019-08-03 stsp err(1, "pledge");
9440 715dc77e 2019-08-03 stsp #endif
9441 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
9442 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
9443 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
9444 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
9445 715dc77e 2019-08-03 stsp
9446 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
9447 715dc77e 2019-08-03 stsp if (cwd == NULL) {
9448 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
9449 715dc77e 2019-08-03 stsp goto done;
9450 715dc77e 2019-08-03 stsp }
9451 715dc77e 2019-08-03 stsp
9452 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9453 fa51e947 2020-03-27 stsp if (error) {
9454 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9455 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
9456 715dc77e 2019-08-03 stsp goto done;
9457 fa51e947 2020-03-27 stsp }
9458 715dc77e 2019-08-03 stsp
9459 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9460 c9956ddf 2019-09-08 stsp NULL);
9461 715dc77e 2019-08-03 stsp if (error != NULL)
9462 715dc77e 2019-08-03 stsp goto done;
9463 715dc77e 2019-08-03 stsp
9464 dc424a06 2019-08-07 stsp if (patch_script_path) {
9465 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
9466 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
9467 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
9468 dc424a06 2019-08-07 stsp patch_script_path);
9469 dc424a06 2019-08-07 stsp goto done;
9470 dc424a06 2019-08-07 stsp }
9471 dc424a06 2019-08-07 stsp }
9472 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9473 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
9474 715dc77e 2019-08-03 stsp if (error)
9475 715dc77e 2019-08-03 stsp goto done;
9476 715dc77e 2019-08-03 stsp
9477 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9478 6d022e97 2019-08-04 stsp if (error)
9479 6d022e97 2019-08-04 stsp goto done;
9480 715dc77e 2019-08-03 stsp
9481 6d022e97 2019-08-04 stsp if (list_stage)
9482 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
9483 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
9484 2e1f37b0 2019-08-08 stsp else {
9485 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9486 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
9487 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
9488 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
9489 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
9490 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
9491 2e1f37b0 2019-08-08 stsp }
9492 ad493afc 2019-08-03 stsp done:
9493 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9494 2e1f37b0 2019-08-08 stsp error == NULL)
9495 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9496 ad493afc 2019-08-03 stsp if (repo)
9497 ad493afc 2019-08-03 stsp got_repo_close(repo);
9498 ad493afc 2019-08-03 stsp if (worktree)
9499 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
9500 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9501 ad493afc 2019-08-03 stsp free((char *)pe->path);
9502 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
9503 ad493afc 2019-08-03 stsp free(cwd);
9504 ad493afc 2019-08-03 stsp return error;
9505 ad493afc 2019-08-03 stsp }
9506 ad493afc 2019-08-03 stsp
9507 ad493afc 2019-08-03 stsp __dead static void
9508 ad493afc 2019-08-03 stsp usage_unstage(void)
9509 ad493afc 2019-08-03 stsp {
9510 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9511 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
9512 ad493afc 2019-08-03 stsp getprogname());
9513 ad493afc 2019-08-03 stsp exit(1);
9514 ad493afc 2019-08-03 stsp }
9515 ad493afc 2019-08-03 stsp
9516 ad493afc 2019-08-03 stsp
9517 ad493afc 2019-08-03 stsp static const struct got_error *
9518 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
9519 ad493afc 2019-08-03 stsp {
9520 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
9521 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
9522 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
9523 ad493afc 2019-08-03 stsp char *cwd = NULL;
9524 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
9525 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9526 9627c110 2020-04-18 stsp int ch, pflag = 0;
9527 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9528 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
9529 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9530 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9531 ad493afc 2019-08-03 stsp
9532 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
9533 ad493afc 2019-08-03 stsp
9534 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
9535 ad493afc 2019-08-03 stsp switch (ch) {
9536 2e1f37b0 2019-08-08 stsp case 'p':
9537 2e1f37b0 2019-08-08 stsp pflag = 1;
9538 2e1f37b0 2019-08-08 stsp break;
9539 2e1f37b0 2019-08-08 stsp case 'F':
9540 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
9541 2e1f37b0 2019-08-08 stsp break;
9542 ad493afc 2019-08-03 stsp default:
9543 ad493afc 2019-08-03 stsp usage_unstage();
9544 ad493afc 2019-08-03 stsp /* NOTREACHED */
9545 ad493afc 2019-08-03 stsp }
9546 ad493afc 2019-08-03 stsp }
9547 ad493afc 2019-08-03 stsp
9548 ad493afc 2019-08-03 stsp argc -= optind;
9549 ad493afc 2019-08-03 stsp argv += optind;
9550 ad493afc 2019-08-03 stsp
9551 ad493afc 2019-08-03 stsp #ifndef PROFILE
9552 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9553 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
9554 ad493afc 2019-08-03 stsp err(1, "pledge");
9555 ad493afc 2019-08-03 stsp #endif
9556 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
9557 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
9558 2e1f37b0 2019-08-08 stsp
9559 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
9560 ad493afc 2019-08-03 stsp if (cwd == NULL) {
9561 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
9562 ad493afc 2019-08-03 stsp goto done;
9563 ad493afc 2019-08-03 stsp }
9564 ad493afc 2019-08-03 stsp
9565 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9566 fa51e947 2020-03-27 stsp if (error) {
9567 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9568 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
9569 ad493afc 2019-08-03 stsp goto done;
9570 fa51e947 2020-03-27 stsp }
9571 ad493afc 2019-08-03 stsp
9572 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9573 c9956ddf 2019-09-08 stsp NULL);
9574 ad493afc 2019-08-03 stsp if (error != NULL)
9575 ad493afc 2019-08-03 stsp goto done;
9576 ad493afc 2019-08-03 stsp
9577 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
9578 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
9579 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
9580 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
9581 2e1f37b0 2019-08-08 stsp patch_script_path);
9582 2e1f37b0 2019-08-08 stsp goto done;
9583 2e1f37b0 2019-08-08 stsp }
9584 2e1f37b0 2019-08-08 stsp }
9585 2e1f37b0 2019-08-08 stsp
9586 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9587 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
9588 ad493afc 2019-08-03 stsp if (error)
9589 ad493afc 2019-08-03 stsp goto done;
9590 ad493afc 2019-08-03 stsp
9591 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9592 ad493afc 2019-08-03 stsp if (error)
9593 ad493afc 2019-08-03 stsp goto done;
9594 ad493afc 2019-08-03 stsp
9595 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9596 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
9597 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9598 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
9599 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
9600 9627c110 2020-04-18 stsp if (!error)
9601 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9602 715dc77e 2019-08-03 stsp done:
9603 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9604 2e1f37b0 2019-08-08 stsp error == NULL)
9605 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9606 0ebf8283 2019-07-24 stsp if (repo)
9607 0ebf8283 2019-07-24 stsp got_repo_close(repo);
9608 715dc77e 2019-08-03 stsp if (worktree)
9609 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
9610 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9611 715dc77e 2019-08-03 stsp free((char *)pe->path);
9612 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
9613 715dc77e 2019-08-03 stsp free(cwd);
9614 01073a5d 2019-08-22 stsp return error;
9615 01073a5d 2019-08-22 stsp }
9616 01073a5d 2019-08-22 stsp
9617 01073a5d 2019-08-22 stsp __dead static void
9618 01073a5d 2019-08-22 stsp usage_cat(void)
9619 01073a5d 2019-08-22 stsp {
9620 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9621 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
9622 01073a5d 2019-08-22 stsp exit(1);
9623 01073a5d 2019-08-22 stsp }
9624 01073a5d 2019-08-22 stsp
9625 01073a5d 2019-08-22 stsp static const struct got_error *
9626 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9627 01073a5d 2019-08-22 stsp {
9628 01073a5d 2019-08-22 stsp const struct got_error *err;
9629 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
9630 01073a5d 2019-08-22 stsp
9631 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
9632 01073a5d 2019-08-22 stsp if (err)
9633 01073a5d 2019-08-22 stsp return err;
9634 01073a5d 2019-08-22 stsp
9635 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9636 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
9637 01073a5d 2019-08-22 stsp return err;
9638 01073a5d 2019-08-22 stsp }
9639 01073a5d 2019-08-22 stsp
9640 01073a5d 2019-08-22 stsp static const struct got_error *
9641 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9642 01073a5d 2019-08-22 stsp {
9643 01073a5d 2019-08-22 stsp const struct got_error *err;
9644 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
9645 56e0773d 2019-11-28 stsp int nentries, i;
9646 01073a5d 2019-08-22 stsp
9647 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
9648 01073a5d 2019-08-22 stsp if (err)
9649 01073a5d 2019-08-22 stsp return err;
9650 01073a5d 2019-08-22 stsp
9651 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
9652 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
9653 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
9654 01073a5d 2019-08-22 stsp char *id_str;
9655 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
9656 01073a5d 2019-08-22 stsp break;
9657 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
9658 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9659 01073a5d 2019-08-22 stsp if (err)
9660 01073a5d 2019-08-22 stsp break;
9661 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
9662 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
9663 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
9664 01073a5d 2019-08-22 stsp free(id_str);
9665 01073a5d 2019-08-22 stsp }
9666 01073a5d 2019-08-22 stsp
9667 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
9668 01073a5d 2019-08-22 stsp return err;
9669 01073a5d 2019-08-22 stsp }
9670 01073a5d 2019-08-22 stsp
9671 01073a5d 2019-08-22 stsp static const struct got_error *
9672 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9673 01073a5d 2019-08-22 stsp {
9674 01073a5d 2019-08-22 stsp const struct got_error *err;
9675 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
9676 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
9677 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
9678 01073a5d 2019-08-22 stsp char *id_str = NULL;
9679 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
9680 01073a5d 2019-08-22 stsp
9681 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
9682 01073a5d 2019-08-22 stsp if (err)
9683 01073a5d 2019-08-22 stsp return err;
9684 01073a5d 2019-08-22 stsp
9685 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9686 01073a5d 2019-08-22 stsp if (err)
9687 01073a5d 2019-08-22 stsp goto done;
9688 01073a5d 2019-08-22 stsp
9689 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9690 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9691 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
9692 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
9693 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9694 01073a5d 2019-08-22 stsp char *pid_str;
9695 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
9696 01073a5d 2019-08-22 stsp if (err)
9697 01073a5d 2019-08-22 stsp goto done;
9698 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9699 01073a5d 2019-08-22 stsp free(pid_str);
9700 01073a5d 2019-08-22 stsp }
9701 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9702 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9703 1367695b 2020-09-26 naddy (long long)got_object_commit_get_author_time(commit));
9704 01073a5d 2019-08-22 stsp
9705 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9706 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9707 1367695b 2020-09-26 naddy (long long)got_object_commit_get_committer_time(commit));
9708 01073a5d 2019-08-22 stsp
9709 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
9710 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9711 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
9712 01073a5d 2019-08-22 stsp done:
9713 01073a5d 2019-08-22 stsp free(id_str);
9714 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
9715 01073a5d 2019-08-22 stsp return err;
9716 01073a5d 2019-08-22 stsp }
9717 01073a5d 2019-08-22 stsp
9718 01073a5d 2019-08-22 stsp static const struct got_error *
9719 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9720 01073a5d 2019-08-22 stsp {
9721 01073a5d 2019-08-22 stsp const struct got_error *err;
9722 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
9723 01073a5d 2019-08-22 stsp char *id_str = NULL;
9724 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
9725 01073a5d 2019-08-22 stsp
9726 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
9727 01073a5d 2019-08-22 stsp if (err)
9728 01073a5d 2019-08-22 stsp return err;
9729 01073a5d 2019-08-22 stsp
9730 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9731 01073a5d 2019-08-22 stsp if (err)
9732 01073a5d 2019-08-22 stsp goto done;
9733 01073a5d 2019-08-22 stsp
9734 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9735 e15d5241 2019-08-22 stsp
9736 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
9737 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9738 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9739 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
9740 01073a5d 2019-08-22 stsp break;
9741 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9742 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9743 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
9744 01073a5d 2019-08-22 stsp break;
9745 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9746 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9747 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
9748 01073a5d 2019-08-22 stsp break;
9749 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9750 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9751 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
9752 01073a5d 2019-08-22 stsp break;
9753 01073a5d 2019-08-22 stsp default:
9754 01073a5d 2019-08-22 stsp break;
9755 01073a5d 2019-08-22 stsp }
9756 01073a5d 2019-08-22 stsp
9757 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9758 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
9759 e15d5241 2019-08-22 stsp
9760 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9761 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
9762 1367695b 2020-09-26 naddy (long long)got_object_tag_get_tagger_time(tag));
9763 01073a5d 2019-08-22 stsp
9764 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
9765 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9766 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
9767 01073a5d 2019-08-22 stsp done:
9768 01073a5d 2019-08-22 stsp free(id_str);
9769 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
9770 01073a5d 2019-08-22 stsp return err;
9771 01073a5d 2019-08-22 stsp }
9772 01073a5d 2019-08-22 stsp
9773 01073a5d 2019-08-22 stsp static const struct got_error *
9774 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
9775 01073a5d 2019-08-22 stsp {
9776 01073a5d 2019-08-22 stsp const struct got_error *error;
9777 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
9778 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
9779 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
9780 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
9781 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
9782 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
9783 84de9106 2020-12-26 stsp struct got_reflist_head refs;
9784 84de9106 2020-12-26 stsp
9785 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
9786 01073a5d 2019-08-22 stsp
9787 01073a5d 2019-08-22 stsp #ifndef PROFILE
9788 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9789 01073a5d 2019-08-22 stsp NULL) == -1)
9790 01073a5d 2019-08-22 stsp err(1, "pledge");
9791 01073a5d 2019-08-22 stsp #endif
9792 01073a5d 2019-08-22 stsp
9793 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9794 01073a5d 2019-08-22 stsp switch (ch) {
9795 896e9b6f 2019-08-26 stsp case 'c':
9796 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
9797 896e9b6f 2019-08-26 stsp break;
9798 01073a5d 2019-08-22 stsp case 'r':
9799 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
9800 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9801 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
9802 9ba1d308 2019-10-21 stsp optarg);
9803 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
9804 01073a5d 2019-08-22 stsp break;
9805 896e9b6f 2019-08-26 stsp case 'P':
9806 896e9b6f 2019-08-26 stsp force_path = 1;
9807 896e9b6f 2019-08-26 stsp break;
9808 01073a5d 2019-08-22 stsp default:
9809 01073a5d 2019-08-22 stsp usage_cat();
9810 01073a5d 2019-08-22 stsp /* NOTREACHED */
9811 01073a5d 2019-08-22 stsp }
9812 01073a5d 2019-08-22 stsp }
9813 01073a5d 2019-08-22 stsp
9814 01073a5d 2019-08-22 stsp argc -= optind;
9815 01073a5d 2019-08-22 stsp argv += optind;
9816 01073a5d 2019-08-22 stsp
9817 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
9818 01073a5d 2019-08-22 stsp if (cwd == NULL) {
9819 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
9820 01073a5d 2019-08-22 stsp goto done;
9821 01073a5d 2019-08-22 stsp }
9822 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
9823 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9824 01073a5d 2019-08-22 stsp goto done;
9825 01073a5d 2019-08-22 stsp if (worktree) {
9826 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9827 01073a5d 2019-08-22 stsp repo_path = strdup(
9828 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
9829 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9830 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
9831 01073a5d 2019-08-22 stsp goto done;
9832 01073a5d 2019-08-22 stsp }
9833 01073a5d 2019-08-22 stsp }
9834 01073a5d 2019-08-22 stsp }
9835 01073a5d 2019-08-22 stsp
9836 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9837 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
9838 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9839 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
9840 01073a5d 2019-08-22 stsp }
9841 01073a5d 2019-08-22 stsp
9842 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
9843 01073a5d 2019-08-22 stsp free(repo_path);
9844 01073a5d 2019-08-22 stsp if (error != NULL)
9845 01073a5d 2019-08-22 stsp goto done;
9846 01073a5d 2019-08-22 stsp
9847 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9848 896e9b6f 2019-08-26 stsp if (error)
9849 896e9b6f 2019-08-26 stsp goto done;
9850 896e9b6f 2019-08-26 stsp
9851 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9852 84de9106 2020-12-26 stsp if (error)
9853 84de9106 2020-12-26 stsp goto done;
9854 84de9106 2020-12-26 stsp
9855 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
9856 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
9857 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
9858 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
9859 01073a5d 2019-08-22 stsp if (error)
9860 01073a5d 2019-08-22 stsp goto done;
9861 01073a5d 2019-08-22 stsp
9862 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
9863 896e9b6f 2019-08-26 stsp if (force_path) {
9864 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
9865 896e9b6f 2019-08-26 stsp argv[i]);
9866 896e9b6f 2019-08-26 stsp if (error)
9867 896e9b6f 2019-08-26 stsp break;
9868 896e9b6f 2019-08-26 stsp } else {
9869 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
9870 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
9871 84de9106 2020-12-26 stsp repo);
9872 896e9b6f 2019-08-26 stsp if (error) {
9873 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9874 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
9875 896e9b6f 2019-08-26 stsp break;
9876 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
9877 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
9878 896e9b6f 2019-08-26 stsp if (error)
9879 896e9b6f 2019-08-26 stsp break;
9880 896e9b6f 2019-08-26 stsp }
9881 896e9b6f 2019-08-26 stsp }
9882 01073a5d 2019-08-22 stsp
9883 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
9884 01073a5d 2019-08-22 stsp if (error)
9885 01073a5d 2019-08-22 stsp break;
9886 01073a5d 2019-08-22 stsp
9887 01073a5d 2019-08-22 stsp switch (obj_type) {
9888 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9889 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
9890 01073a5d 2019-08-22 stsp break;
9891 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9892 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
9893 01073a5d 2019-08-22 stsp break;
9894 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9895 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
9896 01073a5d 2019-08-22 stsp break;
9897 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9898 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
9899 01073a5d 2019-08-22 stsp break;
9900 01073a5d 2019-08-22 stsp default:
9901 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
9902 01073a5d 2019-08-22 stsp break;
9903 01073a5d 2019-08-22 stsp }
9904 01073a5d 2019-08-22 stsp if (error)
9905 01073a5d 2019-08-22 stsp break;
9906 01073a5d 2019-08-22 stsp free(label);
9907 01073a5d 2019-08-22 stsp label = NULL;
9908 01073a5d 2019-08-22 stsp free(id);
9909 01073a5d 2019-08-22 stsp id = NULL;
9910 01073a5d 2019-08-22 stsp }
9911 01073a5d 2019-08-22 stsp done:
9912 01073a5d 2019-08-22 stsp free(label);
9913 01073a5d 2019-08-22 stsp free(id);
9914 896e9b6f 2019-08-26 stsp free(commit_id);
9915 01073a5d 2019-08-22 stsp if (worktree)
9916 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
9917 01073a5d 2019-08-22 stsp if (repo) {
9918 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
9919 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
9920 01073a5d 2019-08-22 stsp if (error == NULL)
9921 01073a5d 2019-08-22 stsp error = repo_error;
9922 b2118c49 2020-07-28 stsp }
9923 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
9924 b2118c49 2020-07-28 stsp return error;
9925 b2118c49 2020-07-28 stsp }
9926 b2118c49 2020-07-28 stsp
9927 b2118c49 2020-07-28 stsp __dead static void
9928 b2118c49 2020-07-28 stsp usage_info(void)
9929 b2118c49 2020-07-28 stsp {
9930 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
9931 b2118c49 2020-07-28 stsp getprogname());
9932 b2118c49 2020-07-28 stsp exit(1);
9933 b2118c49 2020-07-28 stsp }
9934 b2118c49 2020-07-28 stsp
9935 b2118c49 2020-07-28 stsp static const struct got_error *
9936 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9937 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9938 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
9939 b2118c49 2020-07-28 stsp {
9940 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
9941 b2118c49 2020-07-28 stsp char *id_str = NULL;
9942 b2118c49 2020-07-28 stsp char datebuf[128];
9943 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
9944 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
9945 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9946 b2118c49 2020-07-28 stsp
9947 b2118c49 2020-07-28 stsp /*
9948 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
9949 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
9950 b2118c49 2020-07-28 stsp */
9951 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
9952 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
9953 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
9954 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
9955 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
9956 b2118c49 2020-07-28 stsp }
9957 b2118c49 2020-07-28 stsp
9958 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
9959 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
9960 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
9961 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
9962 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
9963 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9964 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
9965 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
9966 b2118c49 2020-07-28 stsp else
9967 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
9968 b2118c49 2020-07-28 stsp
9969 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
9970 b2118c49 2020-07-28 stsp if (tm == NULL)
9971 b2118c49 2020-07-28 stsp return NULL;
9972 b2118c49 2020-07-28 stsp if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9973 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
9974 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
9975 b2118c49 2020-07-28 stsp
9976 b2118c49 2020-07-28 stsp if (blob_id) {
9977 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
9978 b2118c49 2020-07-28 stsp if (err)
9979 b2118c49 2020-07-28 stsp return err;
9980 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
9981 b2118c49 2020-07-28 stsp free(id_str);
9982 b2118c49 2020-07-28 stsp }
9983 b2118c49 2020-07-28 stsp
9984 b2118c49 2020-07-28 stsp if (staged_blob_id) {
9985 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
9986 b2118c49 2020-07-28 stsp if (err)
9987 b2118c49 2020-07-28 stsp return err;
9988 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
9989 b2118c49 2020-07-28 stsp free(id_str);
9990 b2118c49 2020-07-28 stsp }
9991 b2118c49 2020-07-28 stsp
9992 b2118c49 2020-07-28 stsp if (commit_id) {
9993 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
9994 b2118c49 2020-07-28 stsp if (err)
9995 b2118c49 2020-07-28 stsp return err;
9996 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
9997 b2118c49 2020-07-28 stsp free(id_str);
9998 b2118c49 2020-07-28 stsp }
9999 b2118c49 2020-07-28 stsp
10000 b2118c49 2020-07-28 stsp return NULL;
10001 b2118c49 2020-07-28 stsp }
10002 b2118c49 2020-07-28 stsp
10003 b2118c49 2020-07-28 stsp static const struct got_error *
10004 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
10005 b2118c49 2020-07-28 stsp {
10006 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
10007 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
10008 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
10009 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
10010 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
10011 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
10012 b2118c49 2020-07-28 stsp int ch, show_files = 0;
10013 b2118c49 2020-07-28 stsp
10014 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
10015 b2118c49 2020-07-28 stsp
10016 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
10017 b2118c49 2020-07-28 stsp switch (ch) {
10018 b2118c49 2020-07-28 stsp default:
10019 b2118c49 2020-07-28 stsp usage_info();
10020 b2118c49 2020-07-28 stsp /* NOTREACHED */
10021 b2118c49 2020-07-28 stsp }
10022 01073a5d 2019-08-22 stsp }
10023 b2118c49 2020-07-28 stsp
10024 b2118c49 2020-07-28 stsp argc -= optind;
10025 b2118c49 2020-07-28 stsp argv += optind;
10026 b2118c49 2020-07-28 stsp
10027 b2118c49 2020-07-28 stsp #ifndef PROFILE
10028 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
10029 b2118c49 2020-07-28 stsp NULL) == -1)
10030 b2118c49 2020-07-28 stsp err(1, "pledge");
10031 b2118c49 2020-07-28 stsp #endif
10032 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
10033 b2118c49 2020-07-28 stsp if (cwd == NULL) {
10034 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
10035 b2118c49 2020-07-28 stsp goto done;
10036 b2118c49 2020-07-28 stsp }
10037 b2118c49 2020-07-28 stsp
10038 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
10039 b2118c49 2020-07-28 stsp if (error) {
10040 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10041 b2118c49 2020-07-28 stsp error = wrap_not_worktree_error(error, "status", cwd);
10042 b2118c49 2020-07-28 stsp goto done;
10043 b2118c49 2020-07-28 stsp }
10044 b2118c49 2020-07-28 stsp
10045 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
10046 b2118c49 2020-07-28 stsp if (error)
10047 b2118c49 2020-07-28 stsp goto done;
10048 b2118c49 2020-07-28 stsp
10049 b2118c49 2020-07-28 stsp if (argc >= 1) {
10050 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
10051 b2118c49 2020-07-28 stsp worktree);
10052 b2118c49 2020-07-28 stsp if (error)
10053 b2118c49 2020-07-28 stsp goto done;
10054 b2118c49 2020-07-28 stsp show_files = 1;
10055 b2118c49 2020-07-28 stsp }
10056 b2118c49 2020-07-28 stsp
10057 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
10058 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
10059 b2118c49 2020-07-28 stsp if (error)
10060 b2118c49 2020-07-28 stsp goto done;
10061 b2118c49 2020-07-28 stsp
10062 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
10063 b2118c49 2020-07-28 stsp if (error)
10064 b2118c49 2020-07-28 stsp goto done;
10065 b2118c49 2020-07-28 stsp
10066 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
10067 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
10068 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
10069 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
10070 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
10071 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
10072 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
10073 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
10074 b2118c49 2020-07-28 stsp
10075 b2118c49 2020-07-28 stsp if (show_files) {
10076 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
10077 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
10078 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
10079 b2118c49 2020-07-28 stsp continue;
10080 b2118c49 2020-07-28 stsp /*
10081 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
10082 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
10083 b2118c49 2020-07-28 stsp */
10084 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
10085 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
10086 b2118c49 2020-07-28 stsp }
10087 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
10088 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
10089 b2118c49 2020-07-28 stsp if (error)
10090 b2118c49 2020-07-28 stsp goto done;
10091 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
10092 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
10093 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
10094 b2118c49 2020-07-28 stsp break;
10095 b2118c49 2020-07-28 stsp }
10096 b2118c49 2020-07-28 stsp }
10097 b2118c49 2020-07-28 stsp }
10098 b2118c49 2020-07-28 stsp done:
10099 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
10100 b2118c49 2020-07-28 stsp free((char *)pe->path);
10101 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
10102 b2118c49 2020-07-28 stsp free(cwd);
10103 b2118c49 2020-07-28 stsp free(id_str);
10104 b2118c49 2020-07-28 stsp free(uuidstr);
10105 0ebf8283 2019-07-24 stsp return error;
10106 0ebf8283 2019-07-24 stsp }