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 5c860e29 2018-03-12 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 ce5b7c56 2019-07-09 stsp __dead static void usage(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 ce5b7c56 2019-07-09 stsp list_commands(void)
175 ce5b7c56 2019-07-09 stsp {
176 ce5b7c56 2019-07-09 stsp int i;
177 ce5b7c56 2019-07-09 stsp
178 ce5b7c56 2019-07-09 stsp fprintf(stderr, "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 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
182 ce5b7c56 2019-07-09 stsp }
183 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
184 ce5b7c56 2019-07-09 stsp }
185 5c860e29 2018-03-12 stsp
186 5c860e29 2018-03-12 stsp int
187 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
188 5c860e29 2018-03-12 stsp {
189 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
190 5c860e29 2018-03-12 stsp unsigned int i;
191 5c860e29 2018-03-12 stsp int ch;
192 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
193 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
194 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
195 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
196 83cd27f8 2020-01-13 stsp };
197 5c860e29 2018-03-12 stsp
198 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
199 5c860e29 2018-03-12 stsp
200 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
201 5c860e29 2018-03-12 stsp switch (ch) {
202 1b6b95a8 2018-03-12 stsp case 'h':
203 1b6b95a8 2018-03-12 stsp hflag = 1;
204 53ccebc2 2019-07-30 stsp break;
205 53ccebc2 2019-07-30 stsp case 'V':
206 53ccebc2 2019-07-30 stsp Vflag = 1;
207 1b6b95a8 2018-03-12 stsp break;
208 5c860e29 2018-03-12 stsp default:
209 ce5b7c56 2019-07-09 stsp usage(hflag);
210 5c860e29 2018-03-12 stsp /* NOTREACHED */
211 5c860e29 2018-03-12 stsp }
212 5c860e29 2018-03-12 stsp }
213 5c860e29 2018-03-12 stsp
214 5c860e29 2018-03-12 stsp argc -= optind;
215 5c860e29 2018-03-12 stsp argv += optind;
216 1e70621d 2018-03-27 stsp optind = 0;
217 53ccebc2 2019-07-30 stsp
218 53ccebc2 2019-07-30 stsp if (Vflag) {
219 53ccebc2 2019-07-30 stsp got_version_print_str();
220 53ccebc2 2019-07-30 stsp return 1;
221 53ccebc2 2019-07-30 stsp }
222 5c860e29 2018-03-12 stsp
223 5c860e29 2018-03-12 stsp if (argc <= 0)
224 ce5b7c56 2019-07-09 stsp usage(hflag);
225 5c860e29 2018-03-12 stsp
226 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
227 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
228 99437157 2018-11-11 stsp
229 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
230 d7d4f210 2018-03-12 stsp const struct got_error *error;
231 d7d4f210 2018-03-12 stsp
232 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
233 5c860e29 2018-03-12 stsp
234 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
235 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
236 5c860e29 2018-03-12 stsp continue;
237 5c860e29 2018-03-12 stsp
238 1b6b95a8 2018-03-12 stsp if (hflag)
239 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
240 1b6b95a8 2018-03-12 stsp
241 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
242 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
243 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
244 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
245 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
246 70015d7a 2019-11-08 stsp !(sigint_received &&
247 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
248 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
249 d7d4f210 2018-03-12 stsp return 1;
250 d7d4f210 2018-03-12 stsp }
251 d7d4f210 2018-03-12 stsp
252 d7d4f210 2018-03-12 stsp return 0;
253 5c860e29 2018-03-12 stsp }
254 5c860e29 2018-03-12 stsp
255 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
256 ce5b7c56 2019-07-09 stsp list_commands();
257 5c860e29 2018-03-12 stsp return 1;
258 5c860e29 2018-03-12 stsp }
259 5c860e29 2018-03-12 stsp
260 4ed7e80c 2018-05-20 stsp __dead static void
261 ce5b7c56 2019-07-09 stsp usage(int hflag)
262 5c860e29 2018-03-12 stsp {
263 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
264 53ccebc2 2019-07-30 stsp getprogname());
265 ce5b7c56 2019-07-09 stsp if (hflag)
266 ce5b7c56 2019-07-09 stsp list_commands();
267 5c860e29 2018-03-12 stsp exit(1);
268 5c860e29 2018-03-12 stsp }
269 5c860e29 2018-03-12 stsp
270 0266afb7 2019-01-04 stsp static const struct got_error *
271 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
272 e2ba3d07 2019-05-13 stsp {
273 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
274 e2ba3d07 2019-05-13 stsp const char *editor;
275 8920fa04 2019-08-18 stsp
276 8920fa04 2019-08-18 stsp *abspath = NULL;
277 e2ba3d07 2019-05-13 stsp
278 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
279 e2ba3d07 2019-05-13 stsp if (editor == NULL)
280 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
281 e2ba3d07 2019-05-13 stsp
282 0ee7065d 2019-05-13 stsp if (editor) {
283 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
284 0ee7065d 2019-05-13 stsp if (err)
285 0ee7065d 2019-05-13 stsp return err;
286 0ee7065d 2019-05-13 stsp }
287 e2ba3d07 2019-05-13 stsp
288 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
289 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
290 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
291 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
292 0ee7065d 2019-05-13 stsp }
293 0ee7065d 2019-05-13 stsp
294 e2ba3d07 2019-05-13 stsp return NULL;
295 e2ba3d07 2019-05-13 stsp }
296 e2ba3d07 2019-05-13 stsp
297 e2ba3d07 2019-05-13 stsp static const struct got_error *
298 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
299 c530dc23 2019-07-23 stsp const char *worktree_path)
300 0266afb7 2019-01-04 stsp {
301 163ce85a 2019-05-13 stsp const struct got_error *err;
302 0266afb7 2019-01-04 stsp
303 37c06ea4 2019-07-15 stsp #ifdef PROFILE
304 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
305 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
306 37c06ea4 2019-07-15 stsp #endif
307 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
308 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
309 0266afb7 2019-01-04 stsp
310 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
311 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
312 0266afb7 2019-01-04 stsp
313 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
314 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
315 0266afb7 2019-01-04 stsp
316 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
317 163ce85a 2019-05-13 stsp if (err != NULL)
318 163ce85a 2019-05-13 stsp return err;
319 0266afb7 2019-01-04 stsp
320 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
321 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
322 0266afb7 2019-01-04 stsp
323 0266afb7 2019-01-04 stsp return NULL;
324 2c7829a4 2019-06-17 stsp }
325 2c7829a4 2019-06-17 stsp
326 2c7829a4 2019-06-17 stsp __dead static void
327 2c7829a4 2019-06-17 stsp usage_init(void)
328 2c7829a4 2019-06-17 stsp {
329 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
330 2c7829a4 2019-06-17 stsp exit(1);
331 2c7829a4 2019-06-17 stsp }
332 2c7829a4 2019-06-17 stsp
333 2c7829a4 2019-06-17 stsp static const struct got_error *
334 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
335 2c7829a4 2019-06-17 stsp {
336 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
337 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
338 2c7829a4 2019-06-17 stsp int ch;
339 2c7829a4 2019-06-17 stsp
340 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
341 2c7829a4 2019-06-17 stsp switch (ch) {
342 2c7829a4 2019-06-17 stsp default:
343 2c7829a4 2019-06-17 stsp usage_init();
344 2c7829a4 2019-06-17 stsp /* NOTREACHED */
345 2c7829a4 2019-06-17 stsp }
346 2c7829a4 2019-06-17 stsp }
347 2c7829a4 2019-06-17 stsp
348 2c7829a4 2019-06-17 stsp argc -= optind;
349 2c7829a4 2019-06-17 stsp argv += optind;
350 2c7829a4 2019-06-17 stsp
351 2c7829a4 2019-06-17 stsp #ifndef PROFILE
352 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
353 2c7829a4 2019-06-17 stsp err(1, "pledge");
354 2c7829a4 2019-06-17 stsp #endif
355 2c7829a4 2019-06-17 stsp if (argc != 1)
356 bc20e173 2019-06-17 stsp usage_init();
357 2c7829a4 2019-06-17 stsp
358 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
359 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
360 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
361 2c7829a4 2019-06-17 stsp
362 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
363 2c7829a4 2019-06-17 stsp
364 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
365 2c7829a4 2019-06-17 stsp if (error &&
366 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
367 2c7829a4 2019-06-17 stsp goto done;
368 2c7829a4 2019-06-17 stsp
369 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
370 2c7829a4 2019-06-17 stsp if (error)
371 2c7829a4 2019-06-17 stsp goto done;
372 2c7829a4 2019-06-17 stsp
373 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
374 3ce1b845 2019-07-15 stsp done:
375 3ce1b845 2019-07-15 stsp free(repo_path);
376 3ce1b845 2019-07-15 stsp return error;
377 3ce1b845 2019-07-15 stsp }
378 3ce1b845 2019-07-15 stsp
379 3ce1b845 2019-07-15 stsp __dead static void
380 3ce1b845 2019-07-15 stsp usage_import(void)
381 3ce1b845 2019-07-15 stsp {
382 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
383 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
384 3ce1b845 2019-07-15 stsp exit(1);
385 3ce1b845 2019-07-15 stsp }
386 3ce1b845 2019-07-15 stsp
387 3ce1b845 2019-07-15 stsp int
388 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
389 3ce1b845 2019-07-15 stsp {
390 3ce1b845 2019-07-15 stsp pid_t pid;
391 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
392 3ce1b845 2019-07-15 stsp int st = -1;
393 3ce1b845 2019-07-15 stsp
394 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
395 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
396 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
397 3ce1b845 2019-07-15 stsp
398 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
399 3ce1b845 2019-07-15 stsp case -1:
400 3ce1b845 2019-07-15 stsp goto doneediting;
401 3ce1b845 2019-07-15 stsp case 0:
402 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
403 3ce1b845 2019-07-15 stsp _exit(127);
404 3ce1b845 2019-07-15 stsp }
405 3ce1b845 2019-07-15 stsp
406 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
407 3ce1b845 2019-07-15 stsp if (errno != EINTR)
408 3ce1b845 2019-07-15 stsp break;
409 3ce1b845 2019-07-15 stsp
410 3ce1b845 2019-07-15 stsp doneediting:
411 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
412 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
413 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
414 3ce1b845 2019-07-15 stsp
415 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
416 3ce1b845 2019-07-15 stsp errno = EINTR;
417 3ce1b845 2019-07-15 stsp return -1;
418 3ce1b845 2019-07-15 stsp }
419 3ce1b845 2019-07-15 stsp
420 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
421 3ce1b845 2019-07-15 stsp }
422 3ce1b845 2019-07-15 stsp
423 3ce1b845 2019-07-15 stsp static const struct got_error *
424 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
425 3ce1b845 2019-07-15 stsp const char *initial_content)
426 3ce1b845 2019-07-15 stsp {
427 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
428 3ce1b845 2019-07-15 stsp char buf[1024];
429 3ce1b845 2019-07-15 stsp struct stat st, st2;
430 3ce1b845 2019-07-15 stsp FILE *fp;
431 3ce1b845 2019-07-15 stsp int content_changed = 0;
432 3ce1b845 2019-07-15 stsp size_t len;
433 3ce1b845 2019-07-15 stsp
434 3ce1b845 2019-07-15 stsp *logmsg = NULL;
435 3ce1b845 2019-07-15 stsp
436 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
437 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
438 3ce1b845 2019-07-15 stsp
439 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
440 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
441 3ce1b845 2019-07-15 stsp
442 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
443 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
444 3ce1b845 2019-07-15 stsp
445 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
446 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
447 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
448 3ce1b845 2019-07-15 stsp
449 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
450 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
451 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
452 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
453 3ce1b845 2019-07-15 stsp len = 0;
454 3ce1b845 2019-07-15 stsp
455 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
456 3ce1b845 2019-07-15 stsp if (fp == NULL) {
457 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
458 3ce1b845 2019-07-15 stsp goto done;
459 3ce1b845 2019-07-15 stsp }
460 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
461 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
462 3ce1b845 2019-07-15 stsp content_changed = 1;
463 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
464 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
465 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
466 3ce1b845 2019-07-15 stsp }
467 3ce1b845 2019-07-15 stsp fclose(fp);
468 3ce1b845 2019-07-15 stsp
469 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
470 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
471 3ce1b845 2019-07-15 stsp len--;
472 3ce1b845 2019-07-15 stsp }
473 3ce1b845 2019-07-15 stsp
474 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
475 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
476 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
477 3ce1b845 2019-07-15 stsp done:
478 3ce1b845 2019-07-15 stsp if (err) {
479 3ce1b845 2019-07-15 stsp free(*logmsg);
480 3ce1b845 2019-07-15 stsp *logmsg = NULL;
481 3ce1b845 2019-07-15 stsp }
482 3ce1b845 2019-07-15 stsp return err;
483 3ce1b845 2019-07-15 stsp }
484 3ce1b845 2019-07-15 stsp
485 3ce1b845 2019-07-15 stsp static const struct got_error *
486 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
487 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
488 3ce1b845 2019-07-15 stsp {
489 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
490 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
491 1601cb9f 2020-09-11 naddy int initial_content_len;
492 97972933 2020-09-11 stsp int fd = -1;
493 3ce1b845 2019-07-15 stsp
494 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
495 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
496 1601cb9f 2020-09-11 naddy branch_name);
497 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
498 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
499 3ce1b845 2019-07-15 stsp
500 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
501 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
502 3ce1b845 2019-07-15 stsp if (err)
503 3ce1b845 2019-07-15 stsp goto done;
504 3ce1b845 2019-07-15 stsp
505 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
506 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
507 97972933 2020-09-11 stsp goto done;
508 97972933 2020-09-11 stsp }
509 3ce1b845 2019-07-15 stsp
510 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
511 3ce1b845 2019-07-15 stsp done:
512 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
513 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
514 3ce1b845 2019-07-15 stsp free(initial_content);
515 3ce1b845 2019-07-15 stsp return err;
516 3ce1b845 2019-07-15 stsp }
517 3ce1b845 2019-07-15 stsp
518 3ce1b845 2019-07-15 stsp static const struct got_error *
519 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
520 3ce1b845 2019-07-15 stsp {
521 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
522 3ce1b845 2019-07-15 stsp return NULL;
523 3ce1b845 2019-07-15 stsp }
524 3ce1b845 2019-07-15 stsp
525 3ce1b845 2019-07-15 stsp static const struct got_error *
526 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
527 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
528 84792843 2019-08-09 stsp {
529 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
530 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
531 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
532 84792843 2019-08-09 stsp
533 84792843 2019-08-09 stsp *author = NULL;
534 aba9c984 2019-09-08 stsp
535 50b0790e 2020-09-11 stsp if (worktree)
536 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
537 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
538 50b0790e 2020-09-11 stsp
539 50b0790e 2020-09-11 stsp /*
540 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
541 50b0790e 2020-09-11 stsp * significant to least significant:
542 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
543 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
544 50b0790e 2020-09-11 stsp * 3) repository's git config file
545 50b0790e 2020-09-11 stsp * 4) environment variables
546 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
547 50b0790e 2020-09-11 stsp */
548 50b0790e 2020-09-11 stsp
549 50b0790e 2020-09-11 stsp if (worktree_conf)
550 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
551 50b0790e 2020-09-11 stsp if (got_author == NULL)
552 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
553 84792843 2019-08-09 stsp if (got_author == NULL) {
554 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
555 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
556 c9956ddf 2019-09-08 stsp if (name && email) {
557 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
558 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
559 c9956ddf 2019-09-08 stsp return NULL;
560 c9956ddf 2019-09-08 stsp }
561 257add31 2020-09-09 stsp
562 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
563 257add31 2020-09-09 stsp if (got_author == NULL) {
564 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
565 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
566 257add31 2020-09-09 stsp repo);
567 257add31 2020-09-09 stsp if (name && email) {
568 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
569 257add31 2020-09-09 stsp == -1)
570 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
571 257add31 2020-09-09 stsp return NULL;
572 257add31 2020-09-09 stsp }
573 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
574 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
575 257add31 2020-09-09 stsp }
576 84792843 2019-08-09 stsp }
577 84792843 2019-08-09 stsp
578 aba9c984 2019-09-08 stsp *author = strdup(got_author);
579 aba9c984 2019-09-08 stsp if (*author == NULL)
580 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
581 84792843 2019-08-09 stsp
582 84792843 2019-08-09 stsp /*
583 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
584 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
585 84792843 2019-08-09 stsp */
586 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
587 84792843 2019-08-09 stsp got_author++;
588 aba9c984 2019-09-08 stsp if (*got_author != '<') {
589 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
590 aba9c984 2019-09-08 stsp goto done;
591 aba9c984 2019-09-08 stsp }
592 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
593 84792843 2019-08-09 stsp got_author++;
594 aba9c984 2019-09-08 stsp if (*got_author != '@') {
595 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
596 aba9c984 2019-09-08 stsp goto done;
597 aba9c984 2019-09-08 stsp }
598 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
599 84792843 2019-08-09 stsp got_author++;
600 84792843 2019-08-09 stsp if (*got_author != '>')
601 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
602 aba9c984 2019-09-08 stsp done:
603 aba9c984 2019-09-08 stsp if (err) {
604 aba9c984 2019-09-08 stsp free(*author);
605 aba9c984 2019-09-08 stsp *author = NULL;
606 aba9c984 2019-09-08 stsp }
607 aba9c984 2019-09-08 stsp return err;
608 c9956ddf 2019-09-08 stsp }
609 c9956ddf 2019-09-08 stsp
610 c9956ddf 2019-09-08 stsp static const struct got_error *
611 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
612 c9956ddf 2019-09-08 stsp {
613 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
614 c9956ddf 2019-09-08 stsp
615 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
616 c9956ddf 2019-09-08 stsp if (homedir) {
617 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
618 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
619 c9956ddf 2019-09-08 stsp
620 c9956ddf 2019-09-08 stsp }
621 c9956ddf 2019-09-08 stsp return NULL;
622 84792843 2019-08-09 stsp }
623 84792843 2019-08-09 stsp
624 84792843 2019-08-09 stsp static const struct got_error *
625 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
626 3ce1b845 2019-07-15 stsp {
627 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
628 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
629 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
630 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
631 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
632 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
633 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
634 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
635 3ce1b845 2019-07-15 stsp int ch;
636 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
637 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
638 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
639 3ce1b845 2019-07-15 stsp
640 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
641 3ce1b845 2019-07-15 stsp
642 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
643 3ce1b845 2019-07-15 stsp switch (ch) {
644 3ce1b845 2019-07-15 stsp case 'b':
645 3ce1b845 2019-07-15 stsp branch_name = optarg;
646 3ce1b845 2019-07-15 stsp break;
647 3ce1b845 2019-07-15 stsp case 'm':
648 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
649 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
650 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
651 3ce1b845 2019-07-15 stsp goto done;
652 3ce1b845 2019-07-15 stsp }
653 3ce1b845 2019-07-15 stsp break;
654 3ce1b845 2019-07-15 stsp case 'r':
655 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
656 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
657 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
658 9ba1d308 2019-10-21 stsp optarg);
659 3ce1b845 2019-07-15 stsp goto done;
660 3ce1b845 2019-07-15 stsp }
661 3ce1b845 2019-07-15 stsp break;
662 3ce1b845 2019-07-15 stsp case 'I':
663 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
664 3ce1b845 2019-07-15 stsp break;
665 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
666 3ce1b845 2019-07-15 stsp NULL);
667 3ce1b845 2019-07-15 stsp if (error)
668 3ce1b845 2019-07-15 stsp goto done;
669 3ce1b845 2019-07-15 stsp break;
670 3ce1b845 2019-07-15 stsp default:
671 b2b65d18 2019-08-22 stsp usage_import();
672 3ce1b845 2019-07-15 stsp /* NOTREACHED */
673 3ce1b845 2019-07-15 stsp }
674 3ce1b845 2019-07-15 stsp }
675 3ce1b845 2019-07-15 stsp
676 3ce1b845 2019-07-15 stsp argc -= optind;
677 3ce1b845 2019-07-15 stsp argv += optind;
678 3ce1b845 2019-07-15 stsp
679 3ce1b845 2019-07-15 stsp #ifndef PROFILE
680 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
681 aba9c984 2019-09-08 stsp "unveil",
682 3ce1b845 2019-07-15 stsp NULL) == -1)
683 3ce1b845 2019-07-15 stsp err(1, "pledge");
684 3ce1b845 2019-07-15 stsp #endif
685 3ce1b845 2019-07-15 stsp if (argc != 1)
686 3ce1b845 2019-07-15 stsp usage_import();
687 2c7829a4 2019-06-17 stsp
688 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
689 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
690 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
691 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
692 3ce1b845 2019-07-15 stsp }
693 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
694 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
695 c9956ddf 2019-09-08 stsp if (error)
696 c9956ddf 2019-09-08 stsp goto done;
697 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
698 3ce1b845 2019-07-15 stsp if (error)
699 3ce1b845 2019-07-15 stsp goto done;
700 aba9c984 2019-09-08 stsp
701 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
702 aba9c984 2019-09-08 stsp if (error)
703 aba9c984 2019-09-08 stsp return error;
704 e560b7e0 2019-11-28 stsp
705 e560b7e0 2019-11-28 stsp /*
706 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
707 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
708 e560b7e0 2019-11-28 stsp * an unintended typo.
709 e560b7e0 2019-11-28 stsp */
710 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
711 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
712 3ce1b845 2019-07-15 stsp
713 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
714 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
715 3ce1b845 2019-07-15 stsp goto done;
716 3ce1b845 2019-07-15 stsp }
717 3ce1b845 2019-07-15 stsp
718 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
719 3ce1b845 2019-07-15 stsp if (error) {
720 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
721 3ce1b845 2019-07-15 stsp goto done;
722 3ce1b845 2019-07-15 stsp } else {
723 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
724 3ce1b845 2019-07-15 stsp "import target branch already exists");
725 3ce1b845 2019-07-15 stsp goto done;
726 3ce1b845 2019-07-15 stsp }
727 3ce1b845 2019-07-15 stsp
728 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
729 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
730 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
731 3ce1b845 2019-07-15 stsp goto done;
732 3ce1b845 2019-07-15 stsp }
733 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
734 3ce1b845 2019-07-15 stsp
735 3ce1b845 2019-07-15 stsp /*
736 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
737 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
738 3ce1b845 2019-07-15 stsp */
739 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
740 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
741 3ce1b845 2019-07-15 stsp if (error)
742 3ce1b845 2019-07-15 stsp goto done;
743 8e158b01 2019-09-22 stsp free(logmsg);
744 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
745 ef293bdd 2019-10-21 stsp path_dir, refname);
746 ef293bdd 2019-10-21 stsp if (error) {
747 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
748 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
749 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
750 3ce1b845 2019-07-15 stsp goto done;
751 ef293bdd 2019-10-21 stsp }
752 3ce1b845 2019-07-15 stsp }
753 3ce1b845 2019-07-15 stsp
754 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
755 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
756 ef293bdd 2019-10-21 stsp if (logmsg_path)
757 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
758 3ce1b845 2019-07-15 stsp goto done;
759 ef293bdd 2019-10-21 stsp }
760 3ce1b845 2019-07-15 stsp
761 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
762 ef293bdd 2019-10-21 stsp if (error) {
763 ef293bdd 2019-10-21 stsp if (logmsg_path)
764 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
765 ef293bdd 2019-10-21 stsp goto done;
766 ef293bdd 2019-10-21 stsp }
767 ef293bdd 2019-10-21 stsp
768 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
769 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
770 ef293bdd 2019-10-21 stsp if (error) {
771 ef293bdd 2019-10-21 stsp if (logmsg_path)
772 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
773 3ce1b845 2019-07-15 stsp goto done;
774 ef293bdd 2019-10-21 stsp }
775 3ce1b845 2019-07-15 stsp
776 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
777 ef293bdd 2019-10-21 stsp if (error) {
778 ef293bdd 2019-10-21 stsp if (logmsg_path)
779 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
780 3ce1b845 2019-07-15 stsp goto done;
781 ef293bdd 2019-10-21 stsp }
782 3ce1b845 2019-07-15 stsp
783 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
784 ef293bdd 2019-10-21 stsp if (error) {
785 ef293bdd 2019-10-21 stsp if (logmsg_path)
786 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
787 3ce1b845 2019-07-15 stsp goto done;
788 ef293bdd 2019-10-21 stsp }
789 3ce1b845 2019-07-15 stsp
790 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
791 ef293bdd 2019-10-21 stsp if (error) {
792 ef293bdd 2019-10-21 stsp if (logmsg_path)
793 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
794 3ce1b845 2019-07-15 stsp goto done;
795 ef293bdd 2019-10-21 stsp }
796 3ce1b845 2019-07-15 stsp
797 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
798 3ce1b845 2019-07-15 stsp if (error) {
799 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
800 ef293bdd 2019-10-21 stsp if (logmsg_path)
801 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
802 3ce1b845 2019-07-15 stsp goto done;
803 ef293bdd 2019-10-21 stsp }
804 3ce1b845 2019-07-15 stsp
805 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
806 3ce1b845 2019-07-15 stsp branch_ref);
807 ef293bdd 2019-10-21 stsp if (error) {
808 ef293bdd 2019-10-21 stsp if (logmsg_path)
809 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
810 3ce1b845 2019-07-15 stsp goto done;
811 ef293bdd 2019-10-21 stsp }
812 3ce1b845 2019-07-15 stsp
813 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
814 ef293bdd 2019-10-21 stsp if (error) {
815 ef293bdd 2019-10-21 stsp if (logmsg_path)
816 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
817 3ce1b845 2019-07-15 stsp goto done;
818 ef293bdd 2019-10-21 stsp }
819 3ce1b845 2019-07-15 stsp }
820 3ce1b845 2019-07-15 stsp
821 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
822 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
823 2c7829a4 2019-06-17 stsp done:
824 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
825 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
826 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
827 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
828 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
829 8e158b01 2019-09-22 stsp free(logmsg);
830 ef293bdd 2019-10-21 stsp free(logmsg_path);
831 2c7829a4 2019-06-17 stsp free(repo_path);
832 3ce1b845 2019-07-15 stsp free(editor);
833 3ce1b845 2019-07-15 stsp free(refname);
834 3ce1b845 2019-07-15 stsp free(new_commit_id);
835 3ce1b845 2019-07-15 stsp free(id_str);
836 aba9c984 2019-09-08 stsp free(author);
837 c9956ddf 2019-09-08 stsp free(gitconfig_path);
838 3ce1b845 2019-07-15 stsp if (branch_ref)
839 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
840 3ce1b845 2019-07-15 stsp if (head_ref)
841 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
842 2c7829a4 2019-06-17 stsp return error;
843 93658fb9 2020-03-18 stsp }
844 93658fb9 2020-03-18 stsp
845 93658fb9 2020-03-18 stsp __dead static void
846 93658fb9 2020-03-18 stsp usage_clone(void)
847 93658fb9 2020-03-18 stsp {
848 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
849 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
850 93658fb9 2020-03-18 stsp exit(1);
851 93658fb9 2020-03-18 stsp }
852 892ac3b6 2020-03-18 stsp
853 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
854 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
855 892ac3b6 2020-03-18 stsp int last_p_indexed;
856 892ac3b6 2020-03-18 stsp int last_p_resolved;
857 68999b92 2020-03-18 stsp int verbosity;
858 892ac3b6 2020-03-18 stsp };
859 93658fb9 2020-03-18 stsp
860 93658fb9 2020-03-18 stsp static const struct got_error *
861 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
862 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
863 531c3985 2020-03-18 stsp {
864 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
865 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
866 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
867 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
868 b2409d58 2020-03-18 stsp
869 68999b92 2020-03-18 stsp if (a->verbosity < 0)
870 68999b92 2020-03-18 stsp return NULL;
871 68999b92 2020-03-18 stsp
872 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
873 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
874 892ac3b6 2020-03-18 stsp fflush(stdout);
875 12d1281e 2020-03-19 stsp return NULL;
876 b2409d58 2020-03-18 stsp }
877 b2409d58 2020-03-18 stsp
878 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
879 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
880 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
881 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
882 892ac3b6 2020-03-18 stsp print_size = 1;
883 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
884 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
885 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
886 892ac3b6 2020-03-18 stsp }
887 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
888 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
889 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
890 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
891 892ac3b6 2020-03-18 stsp print_indexed = 1;
892 892ac3b6 2020-03-18 stsp print_size = 1;
893 892ac3b6 2020-03-18 stsp }
894 61cc1a7a 2020-03-18 stsp }
895 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
896 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
897 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
898 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
899 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
900 892ac3b6 2020-03-18 stsp print_resolved = 1;
901 892ac3b6 2020-03-18 stsp print_indexed = 1;
902 892ac3b6 2020-03-18 stsp print_size = 1;
903 892ac3b6 2020-03-18 stsp }
904 61cc1a7a 2020-03-18 stsp }
905 3168e5da 2020-09-10 stsp
906 d2cdc636 2020-03-18 stsp }
907 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
908 892ac3b6 2020-03-18 stsp printf("\r");
909 892ac3b6 2020-03-18 stsp if (print_size)
910 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
911 d715f13e 2020-03-19 stsp if (print_indexed)
912 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
913 d715f13e 2020-03-19 stsp if (print_resolved)
914 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
915 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
916 892ac3b6 2020-03-18 stsp fflush(stdout);
917 892ac3b6 2020-03-18 stsp
918 531c3985 2020-03-18 stsp return NULL;
919 531c3985 2020-03-18 stsp }
920 531c3985 2020-03-18 stsp
921 531c3985 2020-03-18 stsp static const struct got_error *
922 f298ae0f 2020-03-25 stsp create_symref(const char *refname, struct got_reference *target_ref,
923 f298ae0f 2020-03-25 stsp int verbosity, struct got_repository *repo)
924 4ba14133 2020-03-20 stsp {
925 4ba14133 2020-03-20 stsp const struct got_error *err;
926 4ba14133 2020-03-20 stsp struct got_reference *head_symref;
927 4ba14133 2020-03-20 stsp
928 f298ae0f 2020-03-25 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
929 4ba14133 2020-03-20 stsp if (err)
930 4ba14133 2020-03-20 stsp return err;
931 4ba14133 2020-03-20 stsp
932 4ba14133 2020-03-20 stsp err = got_ref_write(head_symref, repo);
933 4ba14133 2020-03-20 stsp got_ref_close(head_symref);
934 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
935 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
936 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
937 6338a6a1 2020-03-21 stsp }
938 4ba14133 2020-03-20 stsp return err;
939 4ba14133 2020-03-20 stsp }
940 4ba14133 2020-03-20 stsp
941 4ba14133 2020-03-20 stsp static const struct got_error *
942 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
943 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
944 41b0de12 2020-03-21 stsp {
945 41b0de12 2020-03-21 stsp const struct got_error *err;
946 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
947 41b0de12 2020-03-21 stsp
948 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
949 41b0de12 2020-03-21 stsp const char *refname = pe->path;
950 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
951 41b0de12 2020-03-21 stsp
952 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
953 41b0de12 2020-03-21 stsp }
954 41b0de12 2020-03-21 stsp
955 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
956 41b0de12 2020-03-21 stsp const char *refname = pe->path;
957 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
958 41b0de12 2020-03-21 stsp char *id_str;
959 41b0de12 2020-03-21 stsp
960 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
961 41b0de12 2020-03-21 stsp if (err)
962 41b0de12 2020-03-21 stsp return err;
963 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
964 41b0de12 2020-03-21 stsp free(id_str);
965 41b0de12 2020-03-21 stsp }
966 41b0de12 2020-03-21 stsp
967 41b0de12 2020-03-21 stsp return NULL;
968 6338a6a1 2020-03-21 stsp }
969 6338a6a1 2020-03-21 stsp
970 6338a6a1 2020-03-21 stsp static const struct got_error *
971 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
972 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
973 6338a6a1 2020-03-21 stsp {
974 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
975 6338a6a1 2020-03-21 stsp struct got_reference *ref;
976 6338a6a1 2020-03-21 stsp char *id_str;
977 6338a6a1 2020-03-21 stsp
978 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
979 6338a6a1 2020-03-21 stsp if (err)
980 6338a6a1 2020-03-21 stsp return err;
981 6338a6a1 2020-03-21 stsp
982 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
983 6338a6a1 2020-03-21 stsp if (err)
984 6338a6a1 2020-03-21 stsp goto done;
985 6338a6a1 2020-03-21 stsp
986 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
987 6338a6a1 2020-03-21 stsp got_ref_close(ref);
988 6338a6a1 2020-03-21 stsp
989 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
990 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
991 6338a6a1 2020-03-21 stsp done:
992 6338a6a1 2020-03-21 stsp free(id_str);
993 6338a6a1 2020-03-21 stsp return err;
994 0e4002ca 2020-03-21 stsp }
995 0e4002ca 2020-03-21 stsp
996 0e4002ca 2020-03-21 stsp static int
997 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
998 0e4002ca 2020-03-21 stsp {
999 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1000 0e4002ca 2020-03-21 stsp return 0;
1001 0e4002ca 2020-03-21 stsp refname += 5;
1002 0e4002ca 2020-03-21 stsp
1003 0e4002ca 2020-03-21 stsp /*
1004 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1005 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1006 0e4002ca 2020-03-21 stsp */
1007 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1008 0e4002ca 2020-03-21 stsp return 0;
1009 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1010 0e4002ca 2020-03-21 stsp return 0;
1011 0e4002ca 2020-03-21 stsp
1012 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1013 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1014 0e4002ca 2020-03-21 stsp
1015 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1016 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1017 0e4002ca 2020-03-21 stsp return 1;
1018 0e4002ca 2020-03-21 stsp
1019 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1020 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1021 41b0de12 2020-03-21 stsp }
1022 41b0de12 2020-03-21 stsp
1023 0e4002ca 2020-03-21 stsp static int
1024 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1025 0e4002ca 2020-03-21 stsp {
1026 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1027 0e4002ca 2020-03-21 stsp
1028 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1029 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1030 0e4002ca 2020-03-21 stsp return 1;
1031 0e4002ca 2020-03-21 stsp }
1032 0e4002ca 2020-03-21 stsp
1033 0e4002ca 2020-03-21 stsp return 0;
1034 0e4002ca 2020-03-21 stsp }
1035 0e4002ca 2020-03-21 stsp
1036 41b0de12 2020-03-21 stsp static const struct got_error *
1037 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1038 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1039 0e4002ca 2020-03-21 stsp {
1040 0e4002ca 2020-03-21 stsp const struct got_error *err;
1041 0e4002ca 2020-03-21 stsp char *remote_refname;
1042 0e4002ca 2020-03-21 stsp
1043 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1044 0e4002ca 2020-03-21 stsp refname += 5;
1045 0e4002ca 2020-03-21 stsp
1046 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1047 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1048 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1049 0e4002ca 2020-03-21 stsp
1050 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1051 0e4002ca 2020-03-21 stsp free(remote_refname);
1052 0e4002ca 2020-03-21 stsp return err;
1053 0e4002ca 2020-03-21 stsp }
1054 0e4002ca 2020-03-21 stsp
1055 0e4002ca 2020-03-21 stsp static const struct got_error *
1056 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1057 93658fb9 2020-03-18 stsp {
1058 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1059 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1060 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1061 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1062 bb64b798 2020-03-18 stsp const char *repo_path;
1063 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1064 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1065 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1066 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1067 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1068 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1069 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1070 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1071 257add31 2020-09-09 stsp char *gitconfig_path = NULL, *gotconfig_path = NULL;
1072 257add31 2020-09-09 stsp char *gitconfig = NULL, *gotconfig = NULL;
1073 257add31 2020-09-09 stsp FILE *gitconfig_file = NULL, *gotconfig_file = NULL;
1074 b46f3e71 2020-03-18 stsp ssize_t n;
1075 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1076 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1077 659e7fbd 2020-03-20 stsp struct got_reference *head_symref = NULL;
1078 93658fb9 2020-03-18 stsp
1079 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1080 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1081 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1082 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1083 d9b4d0c0 2020-03-18 stsp
1084 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1085 93658fb9 2020-03-18 stsp switch (ch) {
1086 659e7fbd 2020-03-20 stsp case 'a':
1087 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1088 4ba14133 2020-03-20 stsp break;
1089 4ba14133 2020-03-20 stsp case 'b':
1090 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1091 4ba14133 2020-03-20 stsp optarg, NULL);
1092 4ba14133 2020-03-20 stsp if (error)
1093 4ba14133 2020-03-20 stsp return error;
1094 659e7fbd 2020-03-20 stsp break;
1095 41b0de12 2020-03-21 stsp case 'l':
1096 41b0de12 2020-03-21 stsp list_refs_only = 1;
1097 41b0de12 2020-03-21 stsp break;
1098 469dd726 2020-03-20 stsp case 'm':
1099 469dd726 2020-03-20 stsp mirror_references = 1;
1100 469dd726 2020-03-20 stsp break;
1101 68999b92 2020-03-18 stsp case 'v':
1102 68999b92 2020-03-18 stsp if (verbosity < 0)
1103 68999b92 2020-03-18 stsp verbosity = 0;
1104 68999b92 2020-03-18 stsp else if (verbosity < 3)
1105 68999b92 2020-03-18 stsp verbosity++;
1106 68999b92 2020-03-18 stsp break;
1107 68999b92 2020-03-18 stsp case 'q':
1108 68999b92 2020-03-18 stsp verbosity = -1;
1109 68999b92 2020-03-18 stsp break;
1110 0e4002ca 2020-03-21 stsp case 'R':
1111 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1112 0e4002ca 2020-03-21 stsp optarg, NULL);
1113 0e4002ca 2020-03-21 stsp if (error)
1114 0e4002ca 2020-03-21 stsp return error;
1115 0e4002ca 2020-03-21 stsp break;
1116 93658fb9 2020-03-18 stsp default:
1117 93658fb9 2020-03-18 stsp usage_clone();
1118 93658fb9 2020-03-18 stsp break;
1119 93658fb9 2020-03-18 stsp }
1120 93658fb9 2020-03-18 stsp }
1121 93658fb9 2020-03-18 stsp argc -= optind;
1122 93658fb9 2020-03-18 stsp argv += optind;
1123 39c64a6a 2020-03-18 stsp
1124 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1125 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1126 41b0de12 2020-03-21 stsp if (list_refs_only) {
1127 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1128 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1129 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1130 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1131 41b0de12 2020-03-21 stsp if (mirror_references)
1132 41b0de12 2020-03-21 stsp errx(1, "-l and -m options are mutually exclusive");
1133 41b0de12 2020-03-21 stsp if (verbosity == -1)
1134 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1135 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1136 0e4002ca 2020-03-21 stsp errx(1, "-l and -R options are mutually exclusive");
1137 41b0de12 2020-03-21 stsp }
1138 4ba14133 2020-03-20 stsp
1139 93658fb9 2020-03-18 stsp uri = argv[0];
1140 9df6f38b 2020-03-18 stsp
1141 9df6f38b 2020-03-18 stsp if (argc == 1)
1142 93658fb9 2020-03-18 stsp dirname = NULL;
1143 9df6f38b 2020-03-18 stsp else if (argc == 2)
1144 93658fb9 2020-03-18 stsp dirname = argv[1];
1145 93658fb9 2020-03-18 stsp else
1146 93658fb9 2020-03-18 stsp usage_clone();
1147 09838ffc 2020-03-18 stsp
1148 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1149 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1150 39c64a6a 2020-03-18 stsp if (error)
1151 09f63084 2020-03-20 stsp goto done;
1152 09f63084 2020-03-20 stsp
1153 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1154 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1155 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1156 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1157 09838ffc 2020-03-18 stsp goto done;
1158 09f63084 2020-03-20 stsp }
1159 09838ffc 2020-03-18 stsp
1160 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1161 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1162 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1163 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1164 39c64a6a 2020-03-18 stsp err(1, "pledge");
1165 b46f3e71 2020-03-18 stsp #endif
1166 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1167 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1168 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1169 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1170 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1171 39c64a6a 2020-03-18 stsp err(1, "pledge");
1172 b46f3e71 2020-03-18 stsp #endif
1173 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1174 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1175 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1176 39c64a6a 2020-03-18 stsp goto done;
1177 39c64a6a 2020-03-18 stsp } else {
1178 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1179 39c64a6a 2020-03-18 stsp goto done;
1180 39c64a6a 2020-03-18 stsp }
1181 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1182 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1183 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1184 bb64b798 2020-03-18 stsp goto done;
1185 bb64b798 2020-03-18 stsp }
1186 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1187 bb64b798 2020-03-18 stsp } else
1188 bb64b798 2020-03-18 stsp repo_path = dirname;
1189 bb64b798 2020-03-18 stsp
1190 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1191 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1192 41b0de12 2020-03-21 stsp if (error)
1193 41b0de12 2020-03-21 stsp goto done;
1194 bb64b798 2020-03-18 stsp
1195 41b0de12 2020-03-21 stsp error = got_repo_init(repo_path);
1196 41b0de12 2020-03-21 stsp if (error)
1197 41b0de12 2020-03-21 stsp goto done;
1198 41b0de12 2020-03-21 stsp error = got_repo_open(&repo, repo_path, NULL);
1199 41b0de12 2020-03-21 stsp if (error)
1200 41b0de12 2020-03-21 stsp goto done;
1201 41b0de12 2020-03-21 stsp }
1202 bb64b798 2020-03-18 stsp
1203 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1204 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1205 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1206 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1207 ee448f5f 2020-03-18 stsp goto done;
1208 ee448f5f 2020-03-18 stsp }
1209 ee448f5f 2020-03-18 stsp }
1210 41b0de12 2020-03-21 stsp error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1211 ee448f5f 2020-03-18 stsp if (error)
1212 ee448f5f 2020-03-18 stsp goto done;
1213 f79e6490 2020-04-19 stsp
1214 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1215 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1216 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1217 ee448f5f 2020-03-18 stsp
1218 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1219 9c52365f 2020-03-21 stsp server_path, verbosity);
1220 39c64a6a 2020-03-18 stsp if (error)
1221 bb64b798 2020-03-18 stsp goto done;
1222 bb64b798 2020-03-18 stsp
1223 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1224 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1225 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1226 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1227 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1228 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1229 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1230 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1231 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1232 39c64a6a 2020-03-18 stsp if (error)
1233 d9b4d0c0 2020-03-18 stsp goto done;
1234 d9b4d0c0 2020-03-18 stsp
1235 41b0de12 2020-03-21 stsp if (list_refs_only) {
1236 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1237 41b0de12 2020-03-21 stsp goto done;
1238 41b0de12 2020-03-21 stsp }
1239 41b0de12 2020-03-21 stsp
1240 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1241 39c64a6a 2020-03-18 stsp if (error)
1242 d9b4d0c0 2020-03-18 stsp goto done;
1243 68999b92 2020-03-18 stsp if (verbosity >= 0)
1244 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1245 d9b4d0c0 2020-03-18 stsp free(id_str);
1246 d9b4d0c0 2020-03-18 stsp
1247 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1248 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1249 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1250 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1251 7ebc0570 2020-03-18 stsp char *remote_refname;
1252 0e4002ca 2020-03-21 stsp
1253 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1254 0e4002ca 2020-03-21 stsp !mirror_references) {
1255 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1256 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1257 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1258 0e4002ca 2020-03-21 stsp if (error)
1259 0e4002ca 2020-03-21 stsp goto done;
1260 0e4002ca 2020-03-21 stsp continue;
1261 0e4002ca 2020-03-21 stsp }
1262 668a20f6 2020-03-18 stsp
1263 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1264 39c64a6a 2020-03-18 stsp if (error)
1265 d9b4d0c0 2020-03-18 stsp goto done;
1266 d9b4d0c0 2020-03-18 stsp
1267 469dd726 2020-03-20 stsp if (mirror_references)
1268 469dd726 2020-03-20 stsp continue;
1269 469dd726 2020-03-20 stsp
1270 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1271 7ebc0570 2020-03-18 stsp continue;
1272 7ebc0570 2020-03-18 stsp
1273 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1274 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1275 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1276 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1277 7ebc0570 2020-03-18 stsp goto done;
1278 7ebc0570 2020-03-18 stsp }
1279 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1280 f298ae0f 2020-03-25 stsp free(remote_refname);
1281 39c64a6a 2020-03-18 stsp if (error)
1282 d9b4d0c0 2020-03-18 stsp goto done;
1283 d9b4d0c0 2020-03-18 stsp }
1284 d9b4d0c0 2020-03-18 stsp
1285 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1286 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1287 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1288 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1289 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1290 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1291 d9b4d0c0 2020-03-18 stsp
1292 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1293 d9b4d0c0 2020-03-18 stsp continue;
1294 d9b4d0c0 2020-03-18 stsp
1295 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1296 39c64a6a 2020-03-18 stsp if (error) {
1297 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1298 55330abe 2020-03-20 stsp error = NULL;
1299 d9b4d0c0 2020-03-18 stsp continue;
1300 55330abe 2020-03-20 stsp }
1301 d9b4d0c0 2020-03-18 stsp goto done;
1302 d9b4d0c0 2020-03-18 stsp }
1303 d9b4d0c0 2020-03-18 stsp
1304 f298ae0f 2020-03-25 stsp error = create_symref(refname, target_ref, verbosity, repo);
1305 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1306 f298ae0f 2020-03-25 stsp if (error)
1307 f298ae0f 2020-03-25 stsp goto done;
1308 f298ae0f 2020-03-25 stsp
1309 f298ae0f 2020-03-25 stsp if (mirror_references)
1310 f298ae0f 2020-03-25 stsp continue;
1311 f298ae0f 2020-03-25 stsp
1312 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1313 f298ae0f 2020-03-25 stsp continue;
1314 f298ae0f 2020-03-25 stsp
1315 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1316 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1317 f298ae0f 2020-03-25 stsp refname) == -1) {
1318 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1319 f298ae0f 2020-03-25 stsp goto done;
1320 f298ae0f 2020-03-25 stsp }
1321 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1322 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1323 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1324 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1325 f298ae0f 2020-03-25 stsp free(remote_refname);
1326 f298ae0f 2020-03-25 stsp goto done;
1327 f298ae0f 2020-03-25 stsp }
1328 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1329 f298ae0f 2020-03-25 stsp if (error) {
1330 f298ae0f 2020-03-25 stsp free(remote_refname);
1331 f298ae0f 2020-03-25 stsp free(remote_target);
1332 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1333 f298ae0f 2020-03-25 stsp error = NULL;
1334 f298ae0f 2020-03-25 stsp continue;
1335 f298ae0f 2020-03-25 stsp }
1336 f298ae0f 2020-03-25 stsp goto done;
1337 f298ae0f 2020-03-25 stsp }
1338 f298ae0f 2020-03-25 stsp error = create_symref(remote_refname, target_ref,
1339 f298ae0f 2020-03-25 stsp verbosity - 1, repo);
1340 f298ae0f 2020-03-25 stsp free(remote_refname);
1341 f298ae0f 2020-03-25 stsp free(remote_target);
1342 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1343 39c64a6a 2020-03-18 stsp if (error)
1344 d9b4d0c0 2020-03-18 stsp goto done;
1345 4ba14133 2020-03-20 stsp }
1346 4ba14133 2020-03-20 stsp if (pe == NULL) {
1347 4ba14133 2020-03-20 stsp /*
1348 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1349 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1350 4ba14133 2020-03-20 stsp * which could be fetched instead.
1351 4ba14133 2020-03-20 stsp */
1352 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1353 4ba14133 2020-03-20 stsp const char *target = pe->path;
1354 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1355 d9b4d0c0 2020-03-18 stsp
1356 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1357 4ba14133 2020-03-20 stsp if (error) {
1358 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1359 4ba14133 2020-03-20 stsp error = NULL;
1360 4ba14133 2020-03-20 stsp continue;
1361 4ba14133 2020-03-20 stsp }
1362 4ba14133 2020-03-20 stsp goto done;
1363 4ba14133 2020-03-20 stsp }
1364 4ba14133 2020-03-20 stsp
1365 f298ae0f 2020-03-25 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1366 f298ae0f 2020-03-25 stsp verbosity, repo);
1367 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1368 4ba14133 2020-03-20 stsp if (error)
1369 4ba14133 2020-03-20 stsp goto done;
1370 4ba14133 2020-03-20 stsp break;
1371 4ba14133 2020-03-20 stsp }
1372 659e7fbd 2020-03-20 stsp }
1373 659e7fbd 2020-03-20 stsp
1374 257add31 2020-09-09 stsp /* Create got.conf(5). */
1375 257add31 2020-09-09 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1376 257add31 2020-09-09 stsp if (gotconfig_path == NULL) {
1377 257add31 2020-09-09 stsp error = got_error_from_errno("got_repo_get_path_gotconfig");
1378 257add31 2020-09-09 stsp goto done;
1379 257add31 2020-09-09 stsp }
1380 257add31 2020-09-09 stsp gotconfig_file = fopen(gotconfig_path, "a");
1381 257add31 2020-09-09 stsp if (gotconfig_file == NULL) {
1382 257add31 2020-09-09 stsp error = got_error_from_errno2("fopen", gotconfig_path);
1383 257add31 2020-09-09 stsp goto done;
1384 257add31 2020-09-09 stsp }
1385 257add31 2020-09-09 stsp got_path_strip_trailing_slashes(server_path);
1386 257add31 2020-09-09 stsp if (asprintf(&gotconfig,
1387 257add31 2020-09-09 stsp "remote \"%s\" {\n"
1388 257add31 2020-09-09 stsp "\tserver %s\n"
1389 257add31 2020-09-09 stsp "\tprotocol %s\n"
1390 257add31 2020-09-09 stsp "%s%s%s"
1391 257add31 2020-09-09 stsp "\trepository \"%s\"\n"
1392 257add31 2020-09-09 stsp "%s"
1393 257add31 2020-09-09 stsp "}\n",
1394 257add31 2020-09-09 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1395 257add31 2020-09-09 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1396 257add31 2020-09-09 stsp server_path,
1397 257add31 2020-09-09 stsp mirror_references ? "\tmirror-references yes\n" : "") == -1) {
1398 257add31 2020-09-09 stsp error = got_error_from_errno("asprintf");
1399 257add31 2020-09-09 stsp goto done;
1400 257add31 2020-09-09 stsp }
1401 257add31 2020-09-09 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1402 257add31 2020-09-09 stsp if (n != strlen(gotconfig)) {
1403 257add31 2020-09-09 stsp error = got_ferror(gotconfig_file, GOT_ERR_IO);
1404 257add31 2020-09-09 stsp goto done;
1405 257add31 2020-09-09 stsp }
1406 257add31 2020-09-09 stsp
1407 257add31 2020-09-09 stsp /* Create a config file Git can understand. */
1408 659e7fbd 2020-03-20 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1409 659e7fbd 2020-03-20 stsp if (gitconfig_path == NULL) {
1410 659e7fbd 2020-03-20 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1411 659e7fbd 2020-03-20 stsp goto done;
1412 659e7fbd 2020-03-20 stsp }
1413 659e7fbd 2020-03-20 stsp gitconfig_file = fopen(gitconfig_path, "a");
1414 659e7fbd 2020-03-20 stsp if (gitconfig_file == NULL) {
1415 659e7fbd 2020-03-20 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1416 659e7fbd 2020-03-20 stsp goto done;
1417 b46f3e71 2020-03-18 stsp }
1418 659e7fbd 2020-03-20 stsp if (mirror_references) {
1419 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1420 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1421 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1422 8ceee112 2020-03-20 stsp "\tfetch = +refs/*:refs/*\n"
1423 659e7fbd 2020-03-20 stsp "\tmirror = true\n",
1424 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1425 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1426 659e7fbd 2020-03-20 stsp goto done;
1427 659e7fbd 2020-03-20 stsp }
1428 659e7fbd 2020-03-20 stsp } else if (fetch_all_branches) {
1429 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1430 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1431 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1432 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1433 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1434 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1435 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1436 659e7fbd 2020-03-20 stsp goto done;
1437 659e7fbd 2020-03-20 stsp }
1438 659e7fbd 2020-03-20 stsp } else {
1439 659e7fbd 2020-03-20 stsp const char *branchname;
1440 d715f13e 2020-03-19 stsp
1441 659e7fbd 2020-03-20 stsp /*
1442 659e7fbd 2020-03-20 stsp * If the server specified a default branch, use just that one.
1443 659e7fbd 2020-03-20 stsp * Otherwise fall back to fetching all branches on next fetch.
1444 659e7fbd 2020-03-20 stsp */
1445 659e7fbd 2020-03-20 stsp if (head_symref) {
1446 659e7fbd 2020-03-20 stsp branchname = got_ref_get_symref_target(head_symref);
1447 659e7fbd 2020-03-20 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1448 659e7fbd 2020-03-20 stsp branchname += 11;
1449 659e7fbd 2020-03-20 stsp } else
1450 659e7fbd 2020-03-20 stsp branchname = "*"; /* fall back to all branches */
1451 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1452 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1453 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1454 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1455 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1456 659e7fbd 2020-03-20 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1457 659e7fbd 2020-03-20 stsp branchname) == -1) {
1458 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1459 659e7fbd 2020-03-20 stsp goto done;
1460 659e7fbd 2020-03-20 stsp }
1461 659e7fbd 2020-03-20 stsp }
1462 659e7fbd 2020-03-20 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1463 659e7fbd 2020-03-20 stsp if (n != strlen(gitconfig)) {
1464 659e7fbd 2020-03-20 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1465 659e7fbd 2020-03-20 stsp goto done;
1466 659e7fbd 2020-03-20 stsp }
1467 659e7fbd 2020-03-20 stsp
1468 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1469 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1470 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1471 09838ffc 2020-03-18 stsp done:
1472 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1473 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1474 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1475 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1476 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1477 9c52365f 2020-03-21 stsp }
1478 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1479 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1480 257add31 2020-09-09 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && error == NULL)
1481 257add31 2020-09-09 stsp error = got_error_from_errno("fclose");
1482 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1483 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1484 bb64b798 2020-03-18 stsp if (repo)
1485 bb64b798 2020-03-18 stsp got_repo_close(repo);
1486 659e7fbd 2020-03-20 stsp if (head_symref)
1487 659e7fbd 2020-03-20 stsp got_ref_close(head_symref);
1488 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1489 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1490 d9b4d0c0 2020-03-18 stsp free(pe->data);
1491 d9b4d0c0 2020-03-18 stsp }
1492 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1493 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1494 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1495 d9b4d0c0 2020-03-18 stsp free(pe->data);
1496 d9b4d0c0 2020-03-18 stsp }
1497 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1498 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1499 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1500 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1501 09838ffc 2020-03-18 stsp free(proto);
1502 09838ffc 2020-03-18 stsp free(host);
1503 09838ffc 2020-03-18 stsp free(port);
1504 09838ffc 2020-03-18 stsp free(server_path);
1505 09838ffc 2020-03-18 stsp free(repo_name);
1506 bb64b798 2020-03-18 stsp free(default_destdir);
1507 257add31 2020-09-09 stsp free(gotconfig);
1508 257add31 2020-09-09 stsp free(gitconfig);
1509 257add31 2020-09-09 stsp free(gotconfig_path);
1510 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1511 b46f3e71 2020-03-18 stsp free(git_url);
1512 39c64a6a 2020-03-18 stsp return error;
1513 7848a0e1 2020-03-19 stsp }
1514 7848a0e1 2020-03-19 stsp
1515 7848a0e1 2020-03-19 stsp static const struct got_error *
1516 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1517 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1518 7848a0e1 2020-03-19 stsp {
1519 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1520 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1521 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1522 7848a0e1 2020-03-19 stsp
1523 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1524 7848a0e1 2020-03-19 stsp if (err)
1525 7848a0e1 2020-03-19 stsp goto done;
1526 7848a0e1 2020-03-19 stsp
1527 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1528 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1529 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1530 88609724 2020-03-21 stsp if (err)
1531 88609724 2020-03-21 stsp goto done;
1532 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1533 88609724 2020-03-21 stsp goto done;
1534 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1535 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1536 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1537 db6d8ad8 2020-03-21 stsp }
1538 db6d8ad8 2020-03-21 stsp goto done;
1539 db6d8ad8 2020-03-21 stsp }
1540 db6d8ad8 2020-03-21 stsp
1541 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1542 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1543 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1544 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1545 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1546 688f11b3 2020-03-21 stsp }
1547 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1548 7848a0e1 2020-03-19 stsp if (err)
1549 7848a0e1 2020-03-19 stsp goto done;
1550 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1551 e8a967e0 2020-03-21 stsp if (err)
1552 e8a967e0 2020-03-21 stsp goto done;
1553 7848a0e1 2020-03-19 stsp } else {
1554 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1555 7848a0e1 2020-03-19 stsp if (err)
1556 7848a0e1 2020-03-19 stsp goto done;
1557 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1558 6338a6a1 2020-03-21 stsp goto done;
1559 6338a6a1 2020-03-21 stsp
1560 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1561 6338a6a1 2020-03-21 stsp if (err)
1562 6338a6a1 2020-03-21 stsp goto done;
1563 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1564 6338a6a1 2020-03-21 stsp if (err)
1565 6338a6a1 2020-03-21 stsp goto done;
1566 7848a0e1 2020-03-19 stsp }
1567 6338a6a1 2020-03-21 stsp
1568 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1569 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1570 6338a6a1 2020-03-21 stsp new_id_str);
1571 7848a0e1 2020-03-19 stsp done:
1572 7848a0e1 2020-03-19 stsp free(old_id);
1573 7848a0e1 2020-03-19 stsp free(new_id_str);
1574 7848a0e1 2020-03-19 stsp return err;
1575 2ab43947 2020-03-18 stsp }
1576 f1bcca34 2020-03-25 stsp
1577 f1bcca34 2020-03-25 stsp static const struct got_error *
1578 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1579 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1580 f1bcca34 2020-03-25 stsp {
1581 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1582 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1583 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1584 f1bcca34 2020-03-25 stsp
1585 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1586 bcf34b0e 2020-03-26 stsp if (err) {
1587 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1588 bcf34b0e 2020-03-26 stsp return err;
1589 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1590 bcf34b0e 2020-03-26 stsp if (err)
1591 bcf34b0e 2020-03-26 stsp goto done;
1592 2ab43947 2020-03-18 stsp
1593 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1594 bcf34b0e 2020-03-26 stsp if (err)
1595 bcf34b0e 2020-03-26 stsp goto done;
1596 f1bcca34 2020-03-25 stsp
1597 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1598 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1599 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1600 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1601 bcf34b0e 2020-03-26 stsp } else {
1602 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1603 f1bcca34 2020-03-25 stsp
1604 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1605 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1606 bcf34b0e 2020-03-26 stsp goto done;
1607 bcf34b0e 2020-03-26 stsp
1608 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1609 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1610 bcf34b0e 2020-03-26 stsp if (err)
1611 bcf34b0e 2020-03-26 stsp goto done;
1612 bcf34b0e 2020-03-26 stsp
1613 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1614 bcf34b0e 2020-03-26 stsp if (err)
1615 bcf34b0e 2020-03-26 stsp goto done;
1616 bcf34b0e 2020-03-26 stsp
1617 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1618 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1619 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1620 bcf34b0e 2020-03-26 stsp
1621 bcf34b0e 2020-03-26 stsp }
1622 f1bcca34 2020-03-25 stsp done:
1623 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1624 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1625 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1626 bcf34b0e 2020-03-26 stsp err = unlock_err;
1627 bcf34b0e 2020-03-26 stsp }
1628 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1629 f1bcca34 2020-03-25 stsp return err;
1630 f1bcca34 2020-03-25 stsp }
1631 f1bcca34 2020-03-25 stsp
1632 2ab43947 2020-03-18 stsp __dead static void
1633 7848a0e1 2020-03-19 stsp usage_fetch(void)
1634 7848a0e1 2020-03-19 stsp {
1635 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1636 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1637 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1638 13f12b09 2020-03-21 stsp getprogname());
1639 7848a0e1 2020-03-19 stsp exit(1);
1640 7848a0e1 2020-03-19 stsp }
1641 7848a0e1 2020-03-19 stsp
1642 7848a0e1 2020-03-19 stsp static const struct got_error *
1643 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1644 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1645 f21ec2f0 2020-03-21 stsp {
1646 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1647 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1648 3789fd73 2020-03-26 stsp char *id_str = NULL;
1649 3789fd73 2020-03-26 stsp
1650 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1651 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1652 3789fd73 2020-03-26 stsp if (err)
1653 3789fd73 2020-03-26 stsp return err;
1654 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1655 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1656 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1657 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1658 3789fd73 2020-03-26 stsp }
1659 3789fd73 2020-03-26 stsp } else {
1660 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1661 3789fd73 2020-03-26 stsp if (err)
1662 3789fd73 2020-03-26 stsp return err;
1663 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1664 3789fd73 2020-03-26 stsp if (err)
1665 3789fd73 2020-03-26 stsp goto done;
1666 3168e5da 2020-09-10 stsp
1667 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1668 3789fd73 2020-03-26 stsp if (err)
1669 3789fd73 2020-03-26 stsp goto done;
1670 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1671 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1672 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1673 3789fd73 2020-03-26 stsp }
1674 3789fd73 2020-03-26 stsp }
1675 3789fd73 2020-03-26 stsp done:
1676 3789fd73 2020-03-26 stsp free(id);
1677 3789fd73 2020-03-26 stsp free(id_str);
1678 3789fd73 2020-03-26 stsp return NULL;
1679 3789fd73 2020-03-26 stsp }
1680 3789fd73 2020-03-26 stsp
1681 3789fd73 2020-03-26 stsp static const struct got_error *
1682 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1683 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
1684 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
1685 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
1686 3789fd73 2020-03-26 stsp {
1687 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
1688 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1689 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1690 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1691 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
1692 3789fd73 2020-03-26 stsp char *local_refname = NULL;
1693 f21ec2f0 2020-03-21 stsp
1694 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1695 f21ec2f0 2020-03-21 stsp
1696 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1697 3789fd73 2020-03-26 stsp == -1)
1698 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
1699 3789fd73 2020-03-26 stsp
1700 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1701 f21ec2f0 2020-03-21 stsp if (err)
1702 3789fd73 2020-03-26 stsp goto done;
1703 f21ec2f0 2020-03-21 stsp
1704 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1705 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1706 f21ec2f0 2020-03-21 stsp
1707 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
1708 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
1709 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
1710 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
1711 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
1712 3789fd73 2020-03-26 stsp continue;
1713 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
1714 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
1715 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
1716 3789fd73 2020-03-26 stsp goto done;
1717 3789fd73 2020-03-26 stsp }
1718 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
1719 3789fd73 2020-03-26 stsp continue;
1720 3789fd73 2020-03-26 stsp }
1721 f21ec2f0 2020-03-21 stsp
1722 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1723 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1724 f21ec2f0 2020-03-21 stsp break;
1725 f21ec2f0 2020-03-21 stsp }
1726 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1727 f21ec2f0 2020-03-21 stsp continue;
1728 f21ec2f0 2020-03-21 stsp
1729 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
1730 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1731 3789fd73 2020-03-26 stsp break;
1732 3789fd73 2020-03-26 stsp }
1733 3789fd73 2020-03-26 stsp if (pe != NULL)
1734 3789fd73 2020-03-26 stsp continue;
1735 f21ec2f0 2020-03-21 stsp
1736 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
1737 f21ec2f0 2020-03-21 stsp if (err)
1738 f21ec2f0 2020-03-21 stsp break;
1739 3789fd73 2020-03-26 stsp
1740 3789fd73 2020-03-26 stsp if (local_refname) {
1741 3789fd73 2020-03-26 stsp struct got_reference *ref;
1742 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
1743 3789fd73 2020-03-26 stsp if (err) {
1744 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1745 3789fd73 2020-03-26 stsp break;
1746 3789fd73 2020-03-26 stsp free(local_refname);
1747 3789fd73 2020-03-26 stsp local_refname = NULL;
1748 3789fd73 2020-03-26 stsp continue;
1749 3789fd73 2020-03-26 stsp }
1750 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
1751 3789fd73 2020-03-26 stsp if (err)
1752 3789fd73 2020-03-26 stsp break;
1753 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
1754 3789fd73 2020-03-26 stsp got_ref_close(ref);
1755 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
1756 3789fd73 2020-03-26 stsp err = unlock_err;
1757 3789fd73 2020-03-26 stsp break;
1758 3789fd73 2020-03-26 stsp }
1759 3789fd73 2020-03-26 stsp
1760 3789fd73 2020-03-26 stsp free(local_refname);
1761 3789fd73 2020-03-26 stsp local_refname = NULL;
1762 6338a6a1 2020-03-21 stsp }
1763 f21ec2f0 2020-03-21 stsp }
1764 3789fd73 2020-03-26 stsp done:
1765 3789fd73 2020-03-26 stsp free(remote_namespace);
1766 3789fd73 2020-03-26 stsp free(local_refname);
1767 0e4002ca 2020-03-21 stsp return err;
1768 0e4002ca 2020-03-21 stsp }
1769 0e4002ca 2020-03-21 stsp
1770 0e4002ca 2020-03-21 stsp static const struct got_error *
1771 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1772 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1773 0e4002ca 2020-03-21 stsp {
1774 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
1775 0e4002ca 2020-03-21 stsp char *remote_refname;
1776 0e4002ca 2020-03-21 stsp struct got_reference *ref;
1777 0e4002ca 2020-03-21 stsp
1778 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1779 0e4002ca 2020-03-21 stsp refname += 5;
1780 0e4002ca 2020-03-21 stsp
1781 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1782 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1783 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1784 f21ec2f0 2020-03-21 stsp
1785 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1786 0e4002ca 2020-03-21 stsp if (err) {
1787 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
1788 0e4002ca 2020-03-21 stsp goto done;
1789 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1790 0e4002ca 2020-03-21 stsp } else {
1791 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
1792 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1793 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
1794 9f142382 2020-03-21 stsp err = unlock_err;
1795 0e4002ca 2020-03-21 stsp got_ref_close(ref);
1796 0e4002ca 2020-03-21 stsp }
1797 0e4002ca 2020-03-21 stsp done:
1798 0e4002ca 2020-03-21 stsp free(remote_refname);
1799 f21ec2f0 2020-03-21 stsp return err;
1800 f21ec2f0 2020-03-21 stsp }
1801 f21ec2f0 2020-03-21 stsp
1802 f21ec2f0 2020-03-21 stsp static const struct got_error *
1803 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1804 7848a0e1 2020-03-19 stsp {
1805 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
1806 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1807 7848a0e1 2020-03-19 stsp const char *remote_name;
1808 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1809 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1810 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
1811 7848a0e1 2020-03-19 stsp int nremotes;
1812 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1813 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1814 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1815 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
1816 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1817 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1818 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1819 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
1820 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1821 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1822 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1823 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
1824 7848a0e1 2020-03-19 stsp
1825 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1826 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1827 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1828 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1829 7848a0e1 2020-03-19 stsp
1830 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1831 7848a0e1 2020-03-19 stsp switch (ch) {
1832 659e7fbd 2020-03-20 stsp case 'a':
1833 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1834 4ba14133 2020-03-20 stsp break;
1835 4ba14133 2020-03-20 stsp case 'b':
1836 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1837 4ba14133 2020-03-20 stsp optarg, NULL);
1838 4ba14133 2020-03-20 stsp if (error)
1839 4ba14133 2020-03-20 stsp return error;
1840 41b0de12 2020-03-21 stsp break;
1841 f21ec2f0 2020-03-21 stsp case 'd':
1842 f21ec2f0 2020-03-21 stsp delete_refs = 1;
1843 f21ec2f0 2020-03-21 stsp break;
1844 41b0de12 2020-03-21 stsp case 'l':
1845 41b0de12 2020-03-21 stsp list_refs_only = 1;
1846 659e7fbd 2020-03-20 stsp break;
1847 7848a0e1 2020-03-19 stsp case 'r':
1848 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1849 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1850 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1851 7848a0e1 2020-03-19 stsp optarg);
1852 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1853 7848a0e1 2020-03-19 stsp break;
1854 db6d8ad8 2020-03-21 stsp case 't':
1855 db6d8ad8 2020-03-21 stsp replace_tags = 1;
1856 db6d8ad8 2020-03-21 stsp break;
1857 7848a0e1 2020-03-19 stsp case 'v':
1858 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1859 7848a0e1 2020-03-19 stsp verbosity = 0;
1860 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1861 7848a0e1 2020-03-19 stsp verbosity++;
1862 7848a0e1 2020-03-19 stsp break;
1863 7848a0e1 2020-03-19 stsp case 'q':
1864 7848a0e1 2020-03-19 stsp verbosity = -1;
1865 7848a0e1 2020-03-19 stsp break;
1866 0e4002ca 2020-03-21 stsp case 'R':
1867 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1868 0e4002ca 2020-03-21 stsp optarg, NULL);
1869 0e4002ca 2020-03-21 stsp if (error)
1870 0e4002ca 2020-03-21 stsp return error;
1871 0e4002ca 2020-03-21 stsp break;
1872 7848a0e1 2020-03-19 stsp default:
1873 7848a0e1 2020-03-19 stsp usage_fetch();
1874 7848a0e1 2020-03-19 stsp break;
1875 7848a0e1 2020-03-19 stsp }
1876 7848a0e1 2020-03-19 stsp }
1877 7848a0e1 2020-03-19 stsp argc -= optind;
1878 7848a0e1 2020-03-19 stsp argv += optind;
1879 7848a0e1 2020-03-19 stsp
1880 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1881 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1882 41b0de12 2020-03-21 stsp if (list_refs_only) {
1883 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1884 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1885 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1886 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1887 f21ec2f0 2020-03-21 stsp if (delete_refs)
1888 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
1889 41b0de12 2020-03-21 stsp if (verbosity == -1)
1890 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1891 41b0de12 2020-03-21 stsp }
1892 4ba14133 2020-03-20 stsp
1893 7848a0e1 2020-03-19 stsp if (argc == 0)
1894 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1895 7848a0e1 2020-03-19 stsp else if (argc == 1)
1896 7848a0e1 2020-03-19 stsp remote_name = argv[0];
1897 7848a0e1 2020-03-19 stsp else
1898 7848a0e1 2020-03-19 stsp usage_fetch();
1899 7848a0e1 2020-03-19 stsp
1900 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
1901 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
1902 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
1903 7848a0e1 2020-03-19 stsp goto done;
1904 7848a0e1 2020-03-19 stsp }
1905 7848a0e1 2020-03-19 stsp
1906 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1907 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
1908 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1909 7848a0e1 2020-03-19 stsp goto done;
1910 7848a0e1 2020-03-19 stsp else
1911 7848a0e1 2020-03-19 stsp error = NULL;
1912 7848a0e1 2020-03-19 stsp if (worktree) {
1913 7848a0e1 2020-03-19 stsp repo_path =
1914 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
1915 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1916 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1917 7848a0e1 2020-03-19 stsp if (error)
1918 7848a0e1 2020-03-19 stsp goto done;
1919 7848a0e1 2020-03-19 stsp } else {
1920 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
1921 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1922 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1923 7848a0e1 2020-03-19 stsp goto done;
1924 7848a0e1 2020-03-19 stsp }
1925 7848a0e1 2020-03-19 stsp }
1926 7848a0e1 2020-03-19 stsp }
1927 7848a0e1 2020-03-19 stsp
1928 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
1929 7848a0e1 2020-03-19 stsp if (error)
1930 7848a0e1 2020-03-19 stsp goto done;
1931 7848a0e1 2020-03-19 stsp
1932 50b0790e 2020-09-11 stsp if (worktree) {
1933 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
1934 50b0790e 2020-09-11 stsp if (worktree_conf) {
1935 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
1936 50b0790e 2020-09-11 stsp worktree_conf);
1937 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
1938 50b0790e 2020-09-11 stsp remote = &remotes[i];
1939 50b0790e 2020-09-11 stsp if (strcmp(remote->name, remote_name) == 0)
1940 50b0790e 2020-09-11 stsp break;
1941 50b0790e 2020-09-11 stsp }
1942 50b0790e 2020-09-11 stsp }
1943 7848a0e1 2020-03-19 stsp }
1944 50b0790e 2020-09-11 stsp if (remote == NULL) {
1945 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
1946 50b0790e 2020-09-11 stsp if (repo_conf) {
1947 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
1948 50b0790e 2020-09-11 stsp repo_conf);
1949 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
1950 50b0790e 2020-09-11 stsp remote = &remotes[i];
1951 50b0790e 2020-09-11 stsp if (strcmp(remote->name, remote_name) == 0)
1952 50b0790e 2020-09-11 stsp break;
1953 50b0790e 2020-09-11 stsp }
1954 50b0790e 2020-09-11 stsp }
1955 50b0790e 2020-09-11 stsp }
1956 50b0790e 2020-09-11 stsp if (remote == NULL) {
1957 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1958 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
1959 257add31 2020-09-09 stsp remote = &remotes[i];
1960 257add31 2020-09-09 stsp if (strcmp(remote->name, remote_name) == 0)
1961 257add31 2020-09-09 stsp break;
1962 257add31 2020-09-09 stsp }
1963 7848a0e1 2020-03-19 stsp }
1964 50b0790e 2020-09-11 stsp if (remote == NULL) {
1965 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1966 50b0790e 2020-09-11 stsp goto done;
1967 50b0790e 2020-09-11 stsp }
1968 7848a0e1 2020-03-19 stsp
1969 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1970 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
1971 7848a0e1 2020-03-19 stsp if (error)
1972 7848a0e1 2020-03-19 stsp goto done;
1973 7848a0e1 2020-03-19 stsp
1974 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
1975 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1976 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1977 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
1978 7848a0e1 2020-03-19 stsp err(1, "pledge");
1979 7848a0e1 2020-03-19 stsp #endif
1980 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1981 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
1982 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1983 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1984 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
1985 7848a0e1 2020-03-19 stsp err(1, "pledge");
1986 7848a0e1 2020-03-19 stsp #endif
1987 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
1988 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
1989 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1990 7848a0e1 2020-03-19 stsp goto done;
1991 7848a0e1 2020-03-19 stsp } else {
1992 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1993 7848a0e1 2020-03-19 stsp goto done;
1994 7848a0e1 2020-03-19 stsp }
1995 7848a0e1 2020-03-19 stsp
1996 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1997 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1998 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
1999 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
2000 7848a0e1 2020-03-19 stsp goto done;
2001 7848a0e1 2020-03-19 stsp }
2002 7848a0e1 2020-03-19 stsp }
2003 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2004 7848a0e1 2020-03-19 stsp if (error)
2005 7848a0e1 2020-03-19 stsp goto done;
2006 f79e6490 2020-04-19 stsp
2007 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2008 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2009 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2010 7848a0e1 2020-03-19 stsp
2011 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2012 9c52365f 2020-03-21 stsp server_path, verbosity);
2013 7848a0e1 2020-03-19 stsp if (error)
2014 7848a0e1 2020-03-19 stsp goto done;
2015 7848a0e1 2020-03-19 stsp
2016 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2017 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2018 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2019 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2020 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2021 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2022 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2023 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2024 7848a0e1 2020-03-19 stsp if (error)
2025 7848a0e1 2020-03-19 stsp goto done;
2026 7848a0e1 2020-03-19 stsp
2027 41b0de12 2020-03-21 stsp if (list_refs_only) {
2028 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2029 41b0de12 2020-03-21 stsp goto done;
2030 41b0de12 2020-03-21 stsp }
2031 41b0de12 2020-03-21 stsp
2032 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2033 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2034 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2035 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2036 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2037 984065c8 2020-03-19 stsp if (error)
2038 984065c8 2020-03-19 stsp goto done;
2039 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2040 984065c8 2020-03-19 stsp free(id_str);
2041 984065c8 2020-03-19 stsp id_str = NULL;
2042 984065c8 2020-03-19 stsp }
2043 7848a0e1 2020-03-19 stsp
2044 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2045 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2046 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2047 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2048 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2049 7848a0e1 2020-03-19 stsp char *remote_refname;
2050 7848a0e1 2020-03-19 stsp
2051 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2052 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2053 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2054 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2055 0e4002ca 2020-03-21 stsp if (error)
2056 0e4002ca 2020-03-21 stsp goto done;
2057 0e4002ca 2020-03-21 stsp continue;
2058 0e4002ca 2020-03-21 stsp }
2059 0e4002ca 2020-03-21 stsp
2060 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2061 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2062 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2063 7848a0e1 2020-03-19 stsp if (error) {
2064 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2065 7848a0e1 2020-03-19 stsp goto done;
2066 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2067 6338a6a1 2020-03-21 stsp repo);
2068 7848a0e1 2020-03-19 stsp if (error)
2069 7848a0e1 2020-03-19 stsp goto done;
2070 7848a0e1 2020-03-19 stsp } else {
2071 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2072 db6d8ad8 2020-03-21 stsp verbosity, repo);
2073 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2074 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2075 9f142382 2020-03-21 stsp error = unlock_err;
2076 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2077 7848a0e1 2020-03-19 stsp if (error)
2078 7848a0e1 2020-03-19 stsp goto done;
2079 7848a0e1 2020-03-19 stsp }
2080 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2081 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2082 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2083 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2084 7848a0e1 2020-03-19 stsp goto done;
2085 7848a0e1 2020-03-19 stsp }
2086 7848a0e1 2020-03-19 stsp
2087 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2088 7848a0e1 2020-03-19 stsp if (error) {
2089 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2090 7848a0e1 2020-03-19 stsp goto done;
2091 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2092 688f11b3 2020-03-21 stsp verbosity, repo);
2093 7848a0e1 2020-03-19 stsp if (error)
2094 7848a0e1 2020-03-19 stsp goto done;
2095 7848a0e1 2020-03-19 stsp } else {
2096 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2097 db6d8ad8 2020-03-21 stsp verbosity, repo);
2098 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2099 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2100 9f142382 2020-03-21 stsp error = unlock_err;
2101 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2102 7848a0e1 2020-03-19 stsp if (error)
2103 7848a0e1 2020-03-19 stsp goto done;
2104 7848a0e1 2020-03-19 stsp }
2105 2ec30c80 2020-03-20 stsp
2106 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2107 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2108 2ec30c80 2020-03-20 stsp if (error) {
2109 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2110 2ec30c80 2020-03-20 stsp goto done;
2111 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2112 6338a6a1 2020-03-21 stsp repo);
2113 2ec30c80 2020-03-20 stsp if (error)
2114 2ec30c80 2020-03-20 stsp goto done;
2115 9f142382 2020-03-21 stsp } else {
2116 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2117 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2118 9f142382 2020-03-21 stsp error = unlock_err;
2119 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2120 9f142382 2020-03-21 stsp }
2121 7848a0e1 2020-03-19 stsp }
2122 7848a0e1 2020-03-19 stsp }
2123 f1bcca34 2020-03-25 stsp if (delete_refs) {
2124 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2125 3789fd73 2020-03-26 stsp verbosity, repo);
2126 f1bcca34 2020-03-25 stsp if (error)
2127 f1bcca34 2020-03-25 stsp goto done;
2128 f1bcca34 2020-03-25 stsp }
2129 f1bcca34 2020-03-25 stsp
2130 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2131 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2132 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2133 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2134 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2135 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2136 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2137 f1bcca34 2020-03-25 stsp
2138 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2139 f1bcca34 2020-03-25 stsp continue;
2140 f1bcca34 2020-03-25 stsp
2141 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2142 f1bcca34 2020-03-25 stsp continue;
2143 f1bcca34 2020-03-25 stsp
2144 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2145 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2146 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2147 f1bcca34 2020-03-25 stsp goto done;
2148 f1bcca34 2020-03-25 stsp }
2149 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2150 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2151 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2152 f1bcca34 2020-03-25 stsp free(remote_refname);
2153 f1bcca34 2020-03-25 stsp goto done;
2154 f1bcca34 2020-03-25 stsp }
2155 f1bcca34 2020-03-25 stsp
2156 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2157 f1bcca34 2020-03-25 stsp 0);
2158 f1bcca34 2020-03-25 stsp if (error) {
2159 f1bcca34 2020-03-25 stsp free(remote_refname);
2160 f1bcca34 2020-03-25 stsp free(remote_target);
2161 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2162 f1bcca34 2020-03-25 stsp error = NULL;
2163 f1bcca34 2020-03-25 stsp continue;
2164 f1bcca34 2020-03-25 stsp }
2165 f1bcca34 2020-03-25 stsp goto done;
2166 f1bcca34 2020-03-25 stsp }
2167 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2168 f1bcca34 2020-03-25 stsp verbosity, repo);
2169 f1bcca34 2020-03-25 stsp free(remote_refname);
2170 f1bcca34 2020-03-25 stsp free(remote_target);
2171 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2172 f1bcca34 2020-03-25 stsp if (error)
2173 f1bcca34 2020-03-25 stsp goto done;
2174 f1bcca34 2020-03-25 stsp }
2175 f1bcca34 2020-03-25 stsp }
2176 7848a0e1 2020-03-19 stsp done:
2177 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2178 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2179 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2180 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2181 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2182 9c52365f 2020-03-21 stsp }
2183 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2184 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2185 7848a0e1 2020-03-19 stsp if (repo)
2186 7848a0e1 2020-03-19 stsp got_repo_close(repo);
2187 7848a0e1 2020-03-19 stsp if (worktree)
2188 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2189 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2190 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2191 7848a0e1 2020-03-19 stsp free(pe->data);
2192 7848a0e1 2020-03-19 stsp }
2193 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2194 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2195 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2196 7848a0e1 2020-03-19 stsp free(pe->data);
2197 7848a0e1 2020-03-19 stsp }
2198 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2199 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2200 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2201 7848a0e1 2020-03-19 stsp free(id_str);
2202 7848a0e1 2020-03-19 stsp free(cwd);
2203 7848a0e1 2020-03-19 stsp free(repo_path);
2204 7848a0e1 2020-03-19 stsp free(pack_hash);
2205 7848a0e1 2020-03-19 stsp free(proto);
2206 7848a0e1 2020-03-19 stsp free(host);
2207 7848a0e1 2020-03-19 stsp free(port);
2208 7848a0e1 2020-03-19 stsp free(server_path);
2209 7848a0e1 2020-03-19 stsp free(repo_name);
2210 7848a0e1 2020-03-19 stsp return error;
2211 7848a0e1 2020-03-19 stsp }
2212 7848a0e1 2020-03-19 stsp
2213 7848a0e1 2020-03-19 stsp
2214 7848a0e1 2020-03-19 stsp __dead static void
2215 2ab43947 2020-03-18 stsp usage_checkout(void)
2216 2ab43947 2020-03-18 stsp {
2217 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2218 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2219 2ab43947 2020-03-18 stsp exit(1);
2220 2ab43947 2020-03-18 stsp }
2221 2ab43947 2020-03-18 stsp
2222 2ab43947 2020-03-18 stsp static void
2223 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2224 2ab43947 2020-03-18 stsp {
2225 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2226 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2227 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2228 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2229 2ab43947 2020-03-18 stsp getprogname());
2230 2ab43947 2020-03-18 stsp }
2231 2ab43947 2020-03-18 stsp
2232 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2233 2ab43947 2020-03-18 stsp const char *worktree_path;
2234 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2235 2ab43947 2020-03-18 stsp };
2236 2ab43947 2020-03-18 stsp
2237 2ab43947 2020-03-18 stsp static const struct got_error *
2238 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2239 2ab43947 2020-03-18 stsp {
2240 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2241 2ab43947 2020-03-18 stsp
2242 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2243 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2244 2ab43947 2020-03-18 stsp return NULL;
2245 2ab43947 2020-03-18 stsp
2246 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2247 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2248 2ab43947 2020-03-18 stsp return NULL;
2249 2ab43947 2020-03-18 stsp }
2250 2ab43947 2020-03-18 stsp
2251 2ab43947 2020-03-18 stsp while (path[0] == '/')
2252 2ab43947 2020-03-18 stsp path++;
2253 2ab43947 2020-03-18 stsp
2254 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2255 2ab43947 2020-03-18 stsp return NULL;
2256 2ab43947 2020-03-18 stsp }
2257 2ab43947 2020-03-18 stsp
2258 2ab43947 2020-03-18 stsp static const struct got_error *
2259 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2260 2ab43947 2020-03-18 stsp {
2261 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2262 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2263 2ab43947 2020-03-18 stsp return NULL;
2264 2ab43947 2020-03-18 stsp }
2265 2ab43947 2020-03-18 stsp
2266 2ab43947 2020-03-18 stsp static const struct got_error *
2267 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2268 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2269 2ab43947 2020-03-18 stsp struct got_repository *repo)
2270 2ab43947 2020-03-18 stsp {
2271 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2272 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2273 2ab43947 2020-03-18 stsp
2274 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2275 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2276 2ab43947 2020-03-18 stsp if (err)
2277 2ab43947 2020-03-18 stsp return err;
2278 2ab43947 2020-03-18 stsp
2279 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2280 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2281 2ab43947 2020-03-18 stsp
2282 2ab43947 2020-03-18 stsp /*
2283 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2284 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2285 2ab43947 2020-03-18 stsp *
2286 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2287 2ab43947 2020-03-18 stsp *
2288 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2289 2ab43947 2020-03-18 stsp * \ /
2290 2ab43947 2020-03-18 stsp * C E
2291 2ab43947 2020-03-18 stsp * \ /
2292 2ab43947 2020-03-18 stsp * B (yca)
2293 2ab43947 2020-03-18 stsp * |
2294 2ab43947 2020-03-18 stsp * A
2295 2ab43947 2020-03-18 stsp *
2296 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2297 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2298 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2299 2ab43947 2020-03-18 stsp */
2300 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2301 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2302 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2303 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2304 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2305 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2306 2ab43947 2020-03-18 stsp
2307 2ab43947 2020-03-18 stsp free(yca_id);
2308 2ab43947 2020-03-18 stsp return NULL;
2309 2ab43947 2020-03-18 stsp }
2310 2ab43947 2020-03-18 stsp
2311 2ab43947 2020-03-18 stsp static const struct got_error *
2312 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2313 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2314 2ab43947 2020-03-18 stsp struct got_repository *repo)
2315 2ab43947 2020-03-18 stsp {
2316 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2317 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2318 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2319 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2320 2ab43947 2020-03-18 stsp
2321 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2322 2ab43947 2020-03-18 stsp if (err)
2323 2ab43947 2020-03-18 stsp goto done;
2324 2ab43947 2020-03-18 stsp
2325 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2326 2ab43947 2020-03-18 stsp is_same_branch = 1;
2327 2ab43947 2020-03-18 stsp goto done;
2328 2ab43947 2020-03-18 stsp }
2329 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2330 2ab43947 2020-03-18 stsp is_same_branch = 1;
2331 2ab43947 2020-03-18 stsp goto done;
2332 2ab43947 2020-03-18 stsp }
2333 2ab43947 2020-03-18 stsp
2334 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2335 2ab43947 2020-03-18 stsp if (err)
2336 2ab43947 2020-03-18 stsp goto done;
2337 2ab43947 2020-03-18 stsp
2338 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2339 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2340 2ab43947 2020-03-18 stsp if (err)
2341 2ab43947 2020-03-18 stsp goto done;
2342 2ab43947 2020-03-18 stsp
2343 2ab43947 2020-03-18 stsp for (;;) {
2344 2ab43947 2020-03-18 stsp struct got_object_id *id;
2345 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2346 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2347 2ab43947 2020-03-18 stsp if (err) {
2348 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2349 2ab43947 2020-03-18 stsp err = NULL;
2350 2ab43947 2020-03-18 stsp break;
2351 2ab43947 2020-03-18 stsp }
2352 2ab43947 2020-03-18 stsp
2353 2ab43947 2020-03-18 stsp if (id) {
2354 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2355 2ab43947 2020-03-18 stsp break;
2356 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2357 2ab43947 2020-03-18 stsp is_same_branch = 1;
2358 2ab43947 2020-03-18 stsp break;
2359 2ab43947 2020-03-18 stsp }
2360 2ab43947 2020-03-18 stsp }
2361 2ab43947 2020-03-18 stsp }
2362 2ab43947 2020-03-18 stsp done:
2363 2ab43947 2020-03-18 stsp if (graph)
2364 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2365 2ab43947 2020-03-18 stsp free(head_commit_id);
2366 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2367 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2368 2ab43947 2020-03-18 stsp return err;
2369 a367ff0f 2019-05-14 stsp }
2370 8069f636 2019-01-12 stsp
2371 4ed7e80c 2018-05-20 stsp static const struct got_error *
2372 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2373 4b6c9460 2020-03-05 stsp {
2374 4b6c9460 2020-03-05 stsp static char msg[512];
2375 4b6c9460 2020-03-05 stsp const char *branch_name;
2376 4b6c9460 2020-03-05 stsp
2377 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2378 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2379 4b6c9460 2020-03-05 stsp else
2380 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2381 4b6c9460 2020-03-05 stsp
2382 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2383 4b6c9460 2020-03-05 stsp branch_name += 11;
2384 4b6c9460 2020-03-05 stsp
2385 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2386 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2387 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2388 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2389 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2390 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2391 4b6c9460 2020-03-05 stsp
2392 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2393 4b6c9460 2020-03-05 stsp }
2394 4b6c9460 2020-03-05 stsp
2395 4b6c9460 2020-03-05 stsp static const struct got_error *
2396 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2397 c09a553d 2018-03-12 stsp {
2398 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2399 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2400 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2401 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2402 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2403 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2404 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2405 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2406 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2407 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2408 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2409 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2410 f2ea84fa 2019-07-27 stsp
2411 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2412 c09a553d 2018-03-12 stsp
2413 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2414 0bb8a95e 2018-03-12 stsp switch (ch) {
2415 08573d5b 2019-05-14 stsp case 'b':
2416 08573d5b 2019-05-14 stsp branch_name = optarg;
2417 08573d5b 2019-05-14 stsp break;
2418 8069f636 2019-01-12 stsp case 'c':
2419 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2420 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2421 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2422 bb51a5b4 2020-01-13 stsp break;
2423 bb51a5b4 2020-01-13 stsp case 'E':
2424 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2425 8069f636 2019-01-12 stsp break;
2426 0bb8a95e 2018-03-12 stsp case 'p':
2427 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2428 0bb8a95e 2018-03-12 stsp break;
2429 0bb8a95e 2018-03-12 stsp default:
2430 2deda0b9 2019-03-07 stsp usage_checkout();
2431 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2432 0bb8a95e 2018-03-12 stsp }
2433 0bb8a95e 2018-03-12 stsp }
2434 0bb8a95e 2018-03-12 stsp
2435 0bb8a95e 2018-03-12 stsp argc -= optind;
2436 0bb8a95e 2018-03-12 stsp argv += optind;
2437 0bb8a95e 2018-03-12 stsp
2438 6715a751 2018-03-16 stsp #ifndef PROFILE
2439 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2440 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2441 c09a553d 2018-03-12 stsp err(1, "pledge");
2442 6715a751 2018-03-16 stsp #endif
2443 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2444 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
2445 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2446 76089277 2018-04-01 stsp if (repo_path == NULL)
2447 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2448 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2449 76089277 2018-04-01 stsp if (cwd == NULL) {
2450 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2451 76089277 2018-04-01 stsp goto done;
2452 76089277 2018-04-01 stsp }
2453 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
2454 230a42bd 2019-05-11 jcs base = basename(path_prefix);
2455 230a42bd 2019-05-11 jcs if (base == NULL) {
2456 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2457 230a42bd 2019-05-11 jcs path_prefix);
2458 230a42bd 2019-05-11 jcs goto done;
2459 230a42bd 2019-05-11 jcs }
2460 230a42bd 2019-05-11 jcs } else {
2461 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
2462 230a42bd 2019-05-11 jcs if (base == NULL) {
2463 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2464 230a42bd 2019-05-11 jcs repo_path);
2465 230a42bd 2019-05-11 jcs goto done;
2466 230a42bd 2019-05-11 jcs }
2467 76089277 2018-04-01 stsp }
2468 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2469 c09a553d 2018-03-12 stsp if (dotgit)
2470 c09a553d 2018-03-12 stsp *dotgit = '\0';
2471 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2472 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2473 c09a553d 2018-03-12 stsp free(cwd);
2474 76089277 2018-04-01 stsp goto done;
2475 c09a553d 2018-03-12 stsp }
2476 c09a553d 2018-03-12 stsp free(cwd);
2477 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2478 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2479 76089277 2018-04-01 stsp if (repo_path == NULL) {
2480 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2481 76089277 2018-04-01 stsp goto done;
2482 76089277 2018-04-01 stsp }
2483 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2484 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2485 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2486 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2487 b4b3a7dd 2019-07-22 stsp argv[1]);
2488 b4b3a7dd 2019-07-22 stsp goto done;
2489 b4b3a7dd 2019-07-22 stsp }
2490 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2491 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2492 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2493 b4b3a7dd 2019-07-22 stsp goto done;
2494 b4b3a7dd 2019-07-22 stsp }
2495 76089277 2018-04-01 stsp }
2496 c09a553d 2018-03-12 stsp } else
2497 c09a553d 2018-03-12 stsp usage_checkout();
2498 c09a553d 2018-03-12 stsp
2499 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2500 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2501 13bfb272 2019-05-10 jcs
2502 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2503 c09a553d 2018-03-12 stsp if (error != NULL)
2504 c02c541e 2019-03-29 stsp goto done;
2505 c02c541e 2019-03-29 stsp
2506 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2507 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2508 c530dc23 2019-07-23 stsp if (error) {
2509 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2510 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2511 c530dc23 2019-07-23 stsp goto done;
2512 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2513 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2514 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2515 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2516 c530dc23 2019-07-23 stsp goto done;
2517 c530dc23 2019-07-23 stsp }
2518 c530dc23 2019-07-23 stsp }
2519 c530dc23 2019-07-23 stsp
2520 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2521 c02c541e 2019-03-29 stsp if (error)
2522 c09a553d 2018-03-12 stsp goto done;
2523 8069f636 2019-01-12 stsp
2524 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2525 c09a553d 2018-03-12 stsp if (error != NULL)
2526 c09a553d 2018-03-12 stsp goto done;
2527 c09a553d 2018-03-12 stsp
2528 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2529 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2530 c09a553d 2018-03-12 stsp goto done;
2531 c09a553d 2018-03-12 stsp
2532 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2533 c09a553d 2018-03-12 stsp if (error != NULL)
2534 c09a553d 2018-03-12 stsp goto done;
2535 c09a553d 2018-03-12 stsp
2536 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2537 e5dc7198 2018-12-29 stsp path_prefix);
2538 e5dc7198 2018-12-29 stsp if (error != NULL)
2539 e5dc7198 2018-12-29 stsp goto done;
2540 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2541 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2542 49520a32 2018-12-29 stsp goto done;
2543 49520a32 2018-12-29 stsp }
2544 49520a32 2018-12-29 stsp
2545 8069f636 2019-01-12 stsp if (commit_id_str) {
2546 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2547 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2548 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2549 30837e32 2019-07-25 stsp if (error)
2550 8069f636 2019-01-12 stsp goto done;
2551 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2552 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2553 8069f636 2019-01-12 stsp if (error != NULL) {
2554 8069f636 2019-01-12 stsp free(commit_id);
2555 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2556 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2557 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2558 4b6c9460 2020-03-05 stsp }
2559 8069f636 2019-01-12 stsp goto done;
2560 8069f636 2019-01-12 stsp }
2561 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2562 4b6c9460 2020-03-05 stsp if (error) {
2563 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2564 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2565 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2566 4b6c9460 2020-03-05 stsp }
2567 45d344f6 2019-05-14 stsp goto done;
2568 4b6c9460 2020-03-05 stsp }
2569 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2570 8069f636 2019-01-12 stsp commit_id);
2571 8069f636 2019-01-12 stsp free(commit_id);
2572 8069f636 2019-01-12 stsp if (error)
2573 8069f636 2019-01-12 stsp goto done;
2574 8069f636 2019-01-12 stsp }
2575 8069f636 2019-01-12 stsp
2576 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2577 f2ea84fa 2019-07-27 stsp if (error)
2578 f2ea84fa 2019-07-27 stsp goto done;
2579 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2580 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2581 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2582 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2583 c09a553d 2018-03-12 stsp if (error != NULL)
2584 c09a553d 2018-03-12 stsp goto done;
2585 c09a553d 2018-03-12 stsp
2586 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2587 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2588 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2589 c09a553d 2018-03-12 stsp done:
2590 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2591 8069f636 2019-01-12 stsp free(commit_id_str);
2592 76089277 2018-04-01 stsp free(repo_path);
2593 507dc3bb 2018-12-29 stsp free(worktree_path);
2594 507dc3bb 2018-12-29 stsp return error;
2595 9627c110 2020-04-18 stsp }
2596 9627c110 2020-04-18 stsp
2597 9627c110 2020-04-18 stsp struct got_update_progress_arg {
2598 9627c110 2020-04-18 stsp int did_something;
2599 9627c110 2020-04-18 stsp int conflicts;
2600 9627c110 2020-04-18 stsp int obstructed;
2601 9627c110 2020-04-18 stsp int not_updated;
2602 9627c110 2020-04-18 stsp };
2603 9627c110 2020-04-18 stsp
2604 9627c110 2020-04-18 stsp void
2605 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2606 9627c110 2020-04-18 stsp {
2607 9627c110 2020-04-18 stsp if (!upa->did_something)
2608 9627c110 2020-04-18 stsp return;
2609 9627c110 2020-04-18 stsp
2610 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
2611 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2612 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
2613 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
2614 9627c110 2020-04-18 stsp upa->obstructed);
2615 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
2616 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
2617 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
2618 507dc3bb 2018-12-29 stsp }
2619 507dc3bb 2018-12-29 stsp
2620 507dc3bb 2018-12-29 stsp __dead static void
2621 507dc3bb 2018-12-29 stsp usage_update(void)
2622 507dc3bb 2018-12-29 stsp {
2623 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2624 507dc3bb 2018-12-29 stsp getprogname());
2625 507dc3bb 2018-12-29 stsp exit(1);
2626 507dc3bb 2018-12-29 stsp }
2627 507dc3bb 2018-12-29 stsp
2628 1ee397ad 2019-07-12 stsp static const struct got_error *
2629 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2630 507dc3bb 2018-12-29 stsp {
2631 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
2632 784955db 2019-01-12 stsp
2633 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2634 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2635 1ee397ad 2019-07-12 stsp return NULL;
2636 507dc3bb 2018-12-29 stsp
2637 9627c110 2020-04-18 stsp upa->did_something = 1;
2638 a484d721 2019-06-10 stsp
2639 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2640 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2641 1ee397ad 2019-07-12 stsp return NULL;
2642 a484d721 2019-06-10 stsp
2643 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
2644 9627c110 2020-04-18 stsp upa->conflicts++;
2645 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
2646 9627c110 2020-04-18 stsp upa->obstructed++;
2647 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2648 9627c110 2020-04-18 stsp upa->not_updated++;
2649 9627c110 2020-04-18 stsp
2650 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2651 507dc3bb 2018-12-29 stsp path++;
2652 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2653 1ee397ad 2019-07-12 stsp return NULL;
2654 be7061eb 2018-12-30 stsp }
2655 be7061eb 2018-12-30 stsp
2656 be7061eb 2018-12-30 stsp static const struct got_error *
2657 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2658 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2659 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2660 a1fb16d8 2019-05-24 stsp {
2661 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2662 a1fb16d8 2019-05-24 stsp char *base_id_str;
2663 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2664 a1fb16d8 2019-05-24 stsp
2665 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2666 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2667 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2668 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2669 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2670 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2671 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2672 a1fb16d8 2019-05-24 stsp }
2673 a1fb16d8 2019-05-24 stsp
2674 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2675 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2676 a1fb16d8 2019-05-24 stsp if (err) {
2677 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2678 a1fb16d8 2019-05-24 stsp return err;
2679 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2680 a1fb16d8 2019-05-24 stsp }
2681 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2682 a1fb16d8 2019-05-24 stsp return NULL;
2683 a1fb16d8 2019-05-24 stsp
2684 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2685 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2686 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2687 a1fb16d8 2019-05-24 stsp if (err)
2688 a1fb16d8 2019-05-24 stsp return err;
2689 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2690 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2691 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2692 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2693 0ebf8283 2019-07-24 stsp return NULL;
2694 0ebf8283 2019-07-24 stsp }
2695 0ebf8283 2019-07-24 stsp
2696 0ebf8283 2019-07-24 stsp static const struct got_error *
2697 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2698 0ebf8283 2019-07-24 stsp {
2699 0ebf8283 2019-07-24 stsp const struct got_error *err;
2700 0ebf8283 2019-07-24 stsp int in_progress;
2701 0ebf8283 2019-07-24 stsp
2702 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2703 0ebf8283 2019-07-24 stsp if (err)
2704 0ebf8283 2019-07-24 stsp return err;
2705 0ebf8283 2019-07-24 stsp if (in_progress)
2706 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2707 0ebf8283 2019-07-24 stsp
2708 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2709 0ebf8283 2019-07-24 stsp if (err)
2710 0ebf8283 2019-07-24 stsp return err;
2711 0ebf8283 2019-07-24 stsp if (in_progress)
2712 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2713 0ebf8283 2019-07-24 stsp
2714 a1fb16d8 2019-05-24 stsp return NULL;
2715 a5edda0a 2019-07-27 stsp }
2716 a5edda0a 2019-07-27 stsp
2717 a5edda0a 2019-07-27 stsp static const struct got_error *
2718 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2719 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2720 a5edda0a 2019-07-27 stsp {
2721 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2722 a5edda0a 2019-07-27 stsp char *path;
2723 a5edda0a 2019-07-27 stsp int i;
2724 a5edda0a 2019-07-27 stsp
2725 a5edda0a 2019-07-27 stsp if (argc == 0) {
2726 a5edda0a 2019-07-27 stsp path = strdup("");
2727 a5edda0a 2019-07-27 stsp if (path == NULL)
2728 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2729 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2730 a5edda0a 2019-07-27 stsp }
2731 a5edda0a 2019-07-27 stsp
2732 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2733 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2734 a5edda0a 2019-07-27 stsp if (err)
2735 a5edda0a 2019-07-27 stsp break;
2736 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2737 a5edda0a 2019-07-27 stsp if (err) {
2738 a5edda0a 2019-07-27 stsp free(path);
2739 a5edda0a 2019-07-27 stsp break;
2740 a5edda0a 2019-07-27 stsp }
2741 a5edda0a 2019-07-27 stsp }
2742 a5edda0a 2019-07-27 stsp
2743 a5edda0a 2019-07-27 stsp return err;
2744 a1fb16d8 2019-05-24 stsp }
2745 a1fb16d8 2019-05-24 stsp
2746 a1fb16d8 2019-05-24 stsp static const struct got_error *
2747 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
2748 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
2749 fa51e947 2020-03-27 stsp {
2750 fa51e947 2020-03-27 stsp const struct got_error *err;
2751 fa51e947 2020-03-27 stsp struct got_repository *repo;
2752 fa51e947 2020-03-27 stsp static char msg[512];
2753 fa51e947 2020-03-27 stsp
2754 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
2755 fa51e947 2020-03-27 stsp if (err)
2756 fa51e947 2020-03-27 stsp return orig_err;
2757 fa51e947 2020-03-27 stsp
2758 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
2759 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
2760 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
2761 fa51e947 2020-03-27 stsp "'got checkout'.\n"
2762 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
2763 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2764 fa51e947 2020-03-27 stsp got_repo_close(repo);
2765 fa51e947 2020-03-27 stsp return err;
2766 fa51e947 2020-03-27 stsp }
2767 fa51e947 2020-03-27 stsp
2768 fa51e947 2020-03-27 stsp static const struct got_error *
2769 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2770 507dc3bb 2018-12-29 stsp {
2771 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2772 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2773 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2774 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2775 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2776 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2777 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2778 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2779 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2780 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2781 9627c110 2020-04-18 stsp int ch;
2782 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
2783 507dc3bb 2018-12-29 stsp
2784 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2785 f2ea84fa 2019-07-27 stsp
2786 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2787 507dc3bb 2018-12-29 stsp switch (ch) {
2788 024e9686 2019-05-14 stsp case 'b':
2789 024e9686 2019-05-14 stsp branch_name = optarg;
2790 024e9686 2019-05-14 stsp break;
2791 507dc3bb 2018-12-29 stsp case 'c':
2792 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2793 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2794 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2795 507dc3bb 2018-12-29 stsp break;
2796 507dc3bb 2018-12-29 stsp default:
2797 2deda0b9 2019-03-07 stsp usage_update();
2798 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2799 507dc3bb 2018-12-29 stsp }
2800 507dc3bb 2018-12-29 stsp }
2801 507dc3bb 2018-12-29 stsp
2802 507dc3bb 2018-12-29 stsp argc -= optind;
2803 507dc3bb 2018-12-29 stsp argv += optind;
2804 507dc3bb 2018-12-29 stsp
2805 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2806 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2807 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2808 507dc3bb 2018-12-29 stsp err(1, "pledge");
2809 507dc3bb 2018-12-29 stsp #endif
2810 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2811 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2812 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2813 c4cdcb68 2019-04-03 stsp goto done;
2814 c4cdcb68 2019-04-03 stsp }
2815 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2816 fa51e947 2020-03-27 stsp if (error) {
2817 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
2818 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
2819 fa51e947 2020-03-27 stsp worktree_path);
2820 7d5807f4 2019-07-11 stsp goto done;
2821 fa51e947 2020-03-27 stsp }
2822 7d5807f4 2019-07-11 stsp
2823 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2824 f2ea84fa 2019-07-27 stsp if (error)
2825 f2ea84fa 2019-07-27 stsp goto done;
2826 507dc3bb 2018-12-29 stsp
2827 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2828 c9956ddf 2019-09-08 stsp NULL);
2829 507dc3bb 2018-12-29 stsp if (error != NULL)
2830 507dc3bb 2018-12-29 stsp goto done;
2831 507dc3bb 2018-12-29 stsp
2832 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2833 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2834 087fb88c 2019-08-04 stsp if (error)
2835 087fb88c 2019-08-04 stsp goto done;
2836 087fb88c 2019-08-04 stsp
2837 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2838 0266afb7 2019-01-04 stsp if (error)
2839 0266afb7 2019-01-04 stsp goto done;
2840 0266afb7 2019-01-04 stsp
2841 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2842 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2843 024e9686 2019-05-14 stsp if (error != NULL)
2844 024e9686 2019-05-14 stsp goto done;
2845 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2846 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2847 9c4b8182 2019-01-02 stsp if (error != NULL)
2848 9c4b8182 2019-01-02 stsp goto done;
2849 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2850 507dc3bb 2018-12-29 stsp if (error != NULL)
2851 507dc3bb 2018-12-29 stsp goto done;
2852 507dc3bb 2018-12-29 stsp } else {
2853 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2854 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2855 04f57cb3 2019-07-25 stsp free(commit_id_str);
2856 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2857 30837e32 2019-07-25 stsp if (error)
2858 a297e751 2019-07-11 stsp goto done;
2859 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2860 a297e751 2019-07-11 stsp if (error)
2861 507dc3bb 2018-12-29 stsp goto done;
2862 507dc3bb 2018-12-29 stsp }
2863 35c965b2 2018-12-29 stsp
2864 a1fb16d8 2019-05-24 stsp if (branch_name) {
2865 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2866 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
2867 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
2868 f2ea84fa 2019-07-27 stsp continue;
2869 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2870 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
2871 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
2872 024e9686 2019-05-14 stsp goto done;
2873 024e9686 2019-05-14 stsp }
2874 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2875 024e9686 2019-05-14 stsp if (error)
2876 024e9686 2019-05-14 stsp goto done;
2877 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2878 3aef623b 2019-10-15 stsp repo);
2879 a367ff0f 2019-05-14 stsp free(head_commit_id);
2880 024e9686 2019-05-14 stsp if (error != NULL)
2881 024e9686 2019-05-14 stsp goto done;
2882 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2883 a367ff0f 2019-05-14 stsp if (error)
2884 a367ff0f 2019-05-14 stsp goto done;
2885 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2886 024e9686 2019-05-14 stsp if (error)
2887 024e9686 2019-05-14 stsp goto done;
2888 024e9686 2019-05-14 stsp } else {
2889 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2890 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2891 a367ff0f 2019-05-14 stsp if (error != NULL) {
2892 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
2893 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2894 024e9686 2019-05-14 stsp goto done;
2895 a367ff0f 2019-05-14 stsp }
2896 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2897 a367ff0f 2019-05-14 stsp if (error)
2898 a367ff0f 2019-05-14 stsp goto done;
2899 024e9686 2019-05-14 stsp }
2900 507dc3bb 2018-12-29 stsp
2901 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2902 507dc3bb 2018-12-29 stsp commit_id) != 0) {
2903 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2904 507dc3bb 2018-12-29 stsp commit_id);
2905 507dc3bb 2018-12-29 stsp if (error)
2906 507dc3bb 2018-12-29 stsp goto done;
2907 507dc3bb 2018-12-29 stsp }
2908 507dc3bb 2018-12-29 stsp
2909 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
2910 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2911 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
2912 507dc3bb 2018-12-29 stsp if (error != NULL)
2913 507dc3bb 2018-12-29 stsp goto done;
2914 9c4b8182 2019-01-02 stsp
2915 9627c110 2020-04-18 stsp if (upa.did_something)
2916 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
2917 784955db 2019-01-12 stsp else
2918 784955db 2019-01-12 stsp printf("Already up-to-date\n");
2919 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
2920 507dc3bb 2018-12-29 stsp done:
2921 c09a553d 2018-03-12 stsp free(worktree_path);
2922 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2923 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
2924 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2925 507dc3bb 2018-12-29 stsp free(commit_id);
2926 9c4b8182 2019-01-02 stsp free(commit_id_str);
2927 c09a553d 2018-03-12 stsp return error;
2928 c09a553d 2018-03-12 stsp }
2929 c09a553d 2018-03-12 stsp
2930 f42b1b34 2018-03-12 stsp static const struct got_error *
2931 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2932 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2933 63035f9f 2019-10-06 stsp struct got_repository *repo)
2934 5c860e29 2018-03-12 stsp {
2935 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
2936 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2937 79109fed 2018-03-27 stsp
2938 44392932 2019-08-25 stsp if (blob_id1) {
2939 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2940 44392932 2019-08-25 stsp if (err)
2941 44392932 2019-08-25 stsp goto done;
2942 44392932 2019-08-25 stsp }
2943 79109fed 2018-03-27 stsp
2944 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2945 44392932 2019-08-25 stsp if (err)
2946 44392932 2019-08-25 stsp goto done;
2947 79109fed 2018-03-27 stsp
2948 44392932 2019-08-25 stsp while (path[0] == '/')
2949 44392932 2019-08-25 stsp path++;
2950 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2951 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
2952 44392932 2019-08-25 stsp done:
2953 44392932 2019-08-25 stsp if (blob1)
2954 44392932 2019-08-25 stsp got_object_blob_close(blob1);
2955 44392932 2019-08-25 stsp got_object_blob_close(blob2);
2956 44392932 2019-08-25 stsp return err;
2957 44392932 2019-08-25 stsp }
2958 44392932 2019-08-25 stsp
2959 44392932 2019-08-25 stsp static const struct got_error *
2960 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2961 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2962 63035f9f 2019-10-06 stsp struct got_repository *repo)
2963 44392932 2019-08-25 stsp {
2964 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2965 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2966 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
2967 44392932 2019-08-25 stsp
2968 44392932 2019-08-25 stsp if (tree_id1) {
2969 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2970 79109fed 2018-03-27 stsp if (err)
2971 44392932 2019-08-25 stsp goto done;
2972 79109fed 2018-03-27 stsp }
2973 79109fed 2018-03-27 stsp
2974 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2975 0f2b3dca 2018-12-22 stsp if (err)
2976 0f2b3dca 2018-12-22 stsp goto done;
2977 0f2b3dca 2018-12-22 stsp
2978 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
2979 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2980 aaa13589 2019-06-01 stsp arg.outfile = stdout;
2981 44392932 2019-08-25 stsp while (path[0] == '/')
2982 44392932 2019-08-25 stsp path++;
2983 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2984 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
2985 0f2b3dca 2018-12-22 stsp done:
2986 79109fed 2018-03-27 stsp if (tree1)
2987 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
2988 366e0a5f 2019-10-10 stsp if (tree2)
2989 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
2990 44392932 2019-08-25 stsp return err;
2991 44392932 2019-08-25 stsp }
2992 44392932 2019-08-25 stsp
2993 44392932 2019-08-25 stsp static const struct got_error *
2994 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
2995 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
2996 0208f208 2020-05-05 stsp {
2997 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
2998 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2999 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3000 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3001 0208f208 2020-05-05 stsp
3002 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3003 0208f208 2020-05-05 stsp if (qid != NULL) {
3004 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3005 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3006 0208f208 2020-05-05 stsp qid->id);
3007 0208f208 2020-05-05 stsp if (err)
3008 0208f208 2020-05-05 stsp return err;
3009 0208f208 2020-05-05 stsp
3010 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3011 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3012 0208f208 2020-05-05 stsp
3013 0208f208 2020-05-05 stsp }
3014 0208f208 2020-05-05 stsp
3015 0208f208 2020-05-05 stsp if (tree_id1) {
3016 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3017 0208f208 2020-05-05 stsp if (err)
3018 0208f208 2020-05-05 stsp goto done;
3019 0208f208 2020-05-05 stsp }
3020 0208f208 2020-05-05 stsp
3021 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3022 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3023 0208f208 2020-05-05 stsp if (err)
3024 0208f208 2020-05-05 stsp goto done;
3025 0208f208 2020-05-05 stsp
3026 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3027 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3028 0208f208 2020-05-05 stsp done:
3029 0208f208 2020-05-05 stsp if (tree1)
3030 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3031 0208f208 2020-05-05 stsp if (tree2)
3032 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3033 0208f208 2020-05-05 stsp return err;
3034 0208f208 2020-05-05 stsp }
3035 0208f208 2020-05-05 stsp
3036 0208f208 2020-05-05 stsp static const struct got_error *
3037 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3038 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3039 44392932 2019-08-25 stsp {
3040 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3041 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3042 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3043 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3044 44392932 2019-08-25 stsp struct got_object_qid *qid;
3045 44392932 2019-08-25 stsp
3046 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3047 44392932 2019-08-25 stsp if (qid != NULL) {
3048 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3049 44392932 2019-08-25 stsp qid->id);
3050 44392932 2019-08-25 stsp if (err)
3051 44392932 2019-08-25 stsp return err;
3052 44392932 2019-08-25 stsp }
3053 44392932 2019-08-25 stsp
3054 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3055 44392932 2019-08-25 stsp int obj_type;
3056 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3057 44392932 2019-08-25 stsp if (err)
3058 44392932 2019-08-25 stsp goto done;
3059 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3060 44392932 2019-08-25 stsp if (err) {
3061 44392932 2019-08-25 stsp free(obj_id2);
3062 44392932 2019-08-25 stsp goto done;
3063 44392932 2019-08-25 stsp }
3064 44392932 2019-08-25 stsp if (pcommit) {
3065 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3066 44392932 2019-08-25 stsp qid->id, path);
3067 44392932 2019-08-25 stsp if (err) {
3068 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3069 2e8c69d1 2020-05-04 stsp free(obj_id2);
3070 2e8c69d1 2020-05-04 stsp goto done;
3071 2e8c69d1 2020-05-04 stsp }
3072 2e8c69d1 2020-05-04 stsp } else {
3073 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3074 2e8c69d1 2020-05-04 stsp if (err) {
3075 2e8c69d1 2020-05-04 stsp free(obj_id2);
3076 2e8c69d1 2020-05-04 stsp goto done;
3077 2e8c69d1 2020-05-04 stsp }
3078 44392932 2019-08-25 stsp }
3079 44392932 2019-08-25 stsp }
3080 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3081 44392932 2019-08-25 stsp if (err) {
3082 44392932 2019-08-25 stsp free(obj_id2);
3083 44392932 2019-08-25 stsp goto done;
3084 44392932 2019-08-25 stsp }
3085 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3086 44392932 2019-08-25 stsp switch (obj_type) {
3087 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3088 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3089 63035f9f 2019-10-06 stsp 0, repo);
3090 44392932 2019-08-25 stsp break;
3091 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3092 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3093 63035f9f 2019-10-06 stsp 0, repo);
3094 44392932 2019-08-25 stsp break;
3095 44392932 2019-08-25 stsp default:
3096 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3097 44392932 2019-08-25 stsp break;
3098 44392932 2019-08-25 stsp }
3099 44392932 2019-08-25 stsp free(obj_id1);
3100 44392932 2019-08-25 stsp free(obj_id2);
3101 44392932 2019-08-25 stsp } else {
3102 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3103 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3104 44392932 2019-08-25 stsp if (err)
3105 44392932 2019-08-25 stsp goto done;
3106 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3107 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
3108 44392932 2019-08-25 stsp if (err)
3109 44392932 2019-08-25 stsp goto done;
3110 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3111 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3112 44392932 2019-08-25 stsp }
3113 44392932 2019-08-25 stsp done:
3114 0f2b3dca 2018-12-22 stsp free(id_str1);
3115 0f2b3dca 2018-12-22 stsp free(id_str2);
3116 44392932 2019-08-25 stsp if (pcommit)
3117 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3118 79109fed 2018-03-27 stsp return err;
3119 79109fed 2018-03-27 stsp }
3120 79109fed 2018-03-27 stsp
3121 4bb494d5 2018-06-16 stsp static char *
3122 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3123 6c281f94 2018-06-11 stsp {
3124 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3125 09867e48 2019-08-13 stsp char *p, *s;
3126 09867e48 2019-08-13 stsp
3127 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3128 09867e48 2019-08-13 stsp if (tm == NULL)
3129 09867e48 2019-08-13 stsp return NULL;
3130 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3131 09867e48 2019-08-13 stsp if (s == NULL)
3132 09867e48 2019-08-13 stsp return NULL;
3133 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3134 6c281f94 2018-06-11 stsp if (p)
3135 6c281f94 2018-06-11 stsp *p = '\0';
3136 6c281f94 2018-06-11 stsp return s;
3137 6c281f94 2018-06-11 stsp }
3138 dc424a06 2019-08-07 stsp
3139 6841bf13 2019-11-29 kn static const struct got_error *
3140 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3141 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3142 6841bf13 2019-11-29 kn {
3143 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3144 6841bf13 2019-11-29 kn regmatch_t regmatch;
3145 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3146 6841bf13 2019-11-29 kn
3147 6841bf13 2019-11-29 kn *have_match = 0;
3148 6841bf13 2019-11-29 kn
3149 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3150 6841bf13 2019-11-29 kn if (err)
3151 6841bf13 2019-11-29 kn return err;
3152 6841bf13 2019-11-29 kn
3153 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3154 6841bf13 2019-11-29 kn if (err)
3155 6841bf13 2019-11-29 kn goto done;
3156 6841bf13 2019-11-29 kn
3157 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3158 6841bf13 2019-11-29 kn *have_match = 1;
3159 6841bf13 2019-11-29 kn done:
3160 6841bf13 2019-11-29 kn free(id_str);
3161 6841bf13 2019-11-29 kn free(logmsg);
3162 6841bf13 2019-11-29 kn return err;
3163 6841bf13 2019-11-29 kn }
3164 6841bf13 2019-11-29 kn
3165 0208f208 2020-05-05 stsp static void
3166 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3167 0208f208 2020-05-05 stsp regex_t *regex)
3168 0208f208 2020-05-05 stsp {
3169 0208f208 2020-05-05 stsp regmatch_t regmatch;
3170 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3171 0208f208 2020-05-05 stsp
3172 0208f208 2020-05-05 stsp *have_match = 0;
3173 0208f208 2020-05-05 stsp
3174 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3175 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3176 0208f208 2020-05-05 stsp *have_match = 1;
3177 0208f208 2020-05-05 stsp break;
3178 0208f208 2020-05-05 stsp }
3179 0208f208 2020-05-05 stsp }
3180 0208f208 2020-05-05 stsp }
3181 0208f208 2020-05-05 stsp
3182 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3183 6c281f94 2018-06-11 stsp
3184 79109fed 2018-03-27 stsp static const struct got_error *
3185 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3186 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path,
3187 0208f208 2020-05-05 stsp struct got_pathlist_head *changed_paths, int show_patch,
3188 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
3189 79109fed 2018-03-27 stsp {
3190 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
3191 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3192 6c281f94 2018-06-11 stsp char datebuf[26];
3193 45d799e2 2018-12-23 stsp time_t committer_time;
3194 45d799e2 2018-12-23 stsp const char *author, *committer;
3195 199a4027 2019-02-02 stsp char *refs_str = NULL;
3196 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3197 5c860e29 2018-03-12 stsp
3198 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
3199 199a4027 2019-02-02 stsp char *s;
3200 199a4027 2019-02-02 stsp const char *name;
3201 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3202 a436ad14 2019-08-13 stsp int cmp;
3203 a436ad14 2019-08-13 stsp
3204 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3205 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3206 d9498b20 2019-02-05 stsp continue;
3207 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3208 199a4027 2019-02-02 stsp name += 5;
3209 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3210 7143d404 2019-03-12 stsp continue;
3211 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3212 e34f9ed6 2019-02-02 stsp name += 6;
3213 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3214 141c2bff 2019-02-04 stsp name += 8;
3215 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3216 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3217 4343a07f 2020-04-24 stsp continue;
3218 4343a07f 2020-04-24 stsp }
3219 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3220 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
3221 5d844a1e 2019-08-13 stsp if (err) {
3222 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
3223 5d844a1e 2019-08-13 stsp return err;
3224 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3225 5d844a1e 2019-08-13 stsp err = NULL;
3226 5d844a1e 2019-08-13 stsp tag = NULL;
3227 5d844a1e 2019-08-13 stsp }
3228 a436ad14 2019-08-13 stsp }
3229 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3230 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
3231 a436ad14 2019-08-13 stsp if (tag)
3232 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3233 a436ad14 2019-08-13 stsp if (cmp != 0)
3234 a436ad14 2019-08-13 stsp continue;
3235 199a4027 2019-02-02 stsp s = refs_str;
3236 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3237 199a4027 2019-02-02 stsp name) == -1) {
3238 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3239 199a4027 2019-02-02 stsp free(s);
3240 34ca4898 2019-08-13 stsp return err;
3241 199a4027 2019-02-02 stsp }
3242 199a4027 2019-02-02 stsp free(s);
3243 199a4027 2019-02-02 stsp }
3244 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3245 8bf5b3c9 2018-03-17 stsp if (err)
3246 8bf5b3c9 2018-03-17 stsp return err;
3247 788c352e 2018-06-16 stsp
3248 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3249 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3250 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3251 832c249c 2018-06-10 stsp free(id_str);
3252 d3d493d7 2019-02-21 stsp id_str = NULL;
3253 d3d493d7 2019-02-21 stsp free(refs_str);
3254 d3d493d7 2019-02-21 stsp refs_str = NULL;
3255 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3256 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3257 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3258 09867e48 2019-08-13 stsp if (datestr)
3259 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3260 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3261 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3262 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3263 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3264 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3265 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3266 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3267 3fe1abad 2018-06-10 stsp int n = 1;
3268 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3269 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3270 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3271 a0603db2 2018-06-10 stsp if (err)
3272 a0603db2 2018-06-10 stsp return err;
3273 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3274 a0603db2 2018-06-10 stsp free(id_str);
3275 a0603db2 2018-06-10 stsp }
3276 a0603db2 2018-06-10 stsp }
3277 832c249c 2018-06-10 stsp
3278 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3279 5943eee2 2019-08-13 stsp if (err)
3280 5943eee2 2019-08-13 stsp return err;
3281 8bf5b3c9 2018-03-17 stsp
3282 621015ac 2018-07-23 stsp logmsg = logmsg0;
3283 832c249c 2018-06-10 stsp do {
3284 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3285 832c249c 2018-06-10 stsp if (line)
3286 832c249c 2018-06-10 stsp printf(" %s\n", line);
3287 832c249c 2018-06-10 stsp } while (line);
3288 621015ac 2018-07-23 stsp free(logmsg0);
3289 832c249c 2018-06-10 stsp
3290 0208f208 2020-05-05 stsp if (changed_paths) {
3291 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3292 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3293 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3294 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3295 0208f208 2020-05-05 stsp }
3296 0208f208 2020-05-05 stsp printf("\n");
3297 0208f208 2020-05-05 stsp }
3298 971751ac 2018-03-27 stsp if (show_patch) {
3299 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3300 971751ac 2018-03-27 stsp if (err == 0)
3301 971751ac 2018-03-27 stsp printf("\n");
3302 971751ac 2018-03-27 stsp }
3303 07862c20 2018-09-15 stsp
3304 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3305 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3306 07862c20 2018-09-15 stsp return err;
3307 f42b1b34 2018-03-12 stsp }
3308 5c860e29 2018-03-12 stsp
3309 f42b1b34 2018-03-12 stsp static const struct got_error *
3310 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3311 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3312 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3313 0208f208 2020-05-05 stsp int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3314 f42b1b34 2018-03-12 stsp {
3315 f42b1b34 2018-03-12 stsp const struct got_error *err;
3316 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3317 6841bf13 2019-11-29 kn regex_t regex;
3318 6841bf13 2019-11-29 kn int have_match;
3319 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3320 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3321 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3322 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3323 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3324 dbec59df 2020-04-18 stsp
3325 dbec59df 2020-04-18 stsp SIMPLEQ_INIT(&reversed_commits);
3326 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3327 372ccdbb 2018-06-10 stsp
3328 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(&regex, search_pattern,
3329 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3330 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3331 6841bf13 2019-11-29 kn
3332 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3333 f42b1b34 2018-03-12 stsp if (err)
3334 f42b1b34 2018-03-12 stsp return err;
3335 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3336 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3337 372ccdbb 2018-06-10 stsp if (err)
3338 fcc85cad 2018-07-22 stsp goto done;
3339 656b1f76 2019-05-11 jcs for (;;) {
3340 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3341 8bf5b3c9 2018-03-17 stsp
3342 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3343 84453469 2018-11-11 stsp break;
3344 84453469 2018-11-11 stsp
3345 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3346 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3347 372ccdbb 2018-06-10 stsp if (err) {
3348 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3349 9ba79e04 2018-06-11 stsp err = NULL;
3350 ee780d5c 2020-01-04 stsp break;
3351 7e665116 2018-04-02 stsp }
3352 b43fbaa0 2018-06-11 stsp if (id == NULL)
3353 7e665116 2018-04-02 stsp break;
3354 b43fbaa0 2018-06-11 stsp
3355 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3356 b43fbaa0 2018-06-11 stsp if (err)
3357 fcc85cad 2018-07-22 stsp break;
3358 6841bf13 2019-11-29 kn
3359 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3360 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3361 0208f208 2020-05-05 stsp if (err)
3362 0208f208 2020-05-05 stsp break;
3363 0208f208 2020-05-05 stsp }
3364 0208f208 2020-05-05 stsp
3365 6841bf13 2019-11-29 kn if (search_pattern) {
3366 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3367 6841bf13 2019-11-29 kn if (err) {
3368 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3369 6841bf13 2019-11-29 kn break;
3370 6841bf13 2019-11-29 kn }
3371 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3372 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3373 0208f208 2020-05-05 stsp &changed_paths, &regex);
3374 6841bf13 2019-11-29 kn if (have_match == 0) {
3375 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3376 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3377 0208f208 2020-05-05 stsp free((char *)pe->path);
3378 0208f208 2020-05-05 stsp free(pe->data);
3379 0208f208 2020-05-05 stsp }
3380 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3381 6841bf13 2019-11-29 kn continue;
3382 6841bf13 2019-11-29 kn }
3383 6841bf13 2019-11-29 kn }
3384 6841bf13 2019-11-29 kn
3385 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3386 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3387 dbec59df 2020-04-18 stsp if (err)
3388 dbec59df 2020-04-18 stsp break;
3389 dbec59df 2020-04-18 stsp SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3390 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3391 dbec59df 2020-04-18 stsp } else {
3392 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3393 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3394 0208f208 2020-05-05 stsp show_patch, diff_context, refs);
3395 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3396 dbec59df 2020-04-18 stsp if (err)
3397 dbec59df 2020-04-18 stsp break;
3398 dbec59df 2020-04-18 stsp }
3399 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3400 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3401 7e665116 2018-04-02 stsp break;
3402 0208f208 2020-05-05 stsp
3403 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3404 0208f208 2020-05-05 stsp free((char *)pe->path);
3405 0208f208 2020-05-05 stsp free(pe->data);
3406 0208f208 2020-05-05 stsp }
3407 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3408 31cedeaf 2018-09-15 stsp }
3409 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3410 dbec59df 2020-04-18 stsp SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3411 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3412 dbec59df 2020-04-18 stsp if (err)
3413 dbec59df 2020-04-18 stsp break;
3414 502b9684 2020-07-31 stsp if (show_changed_paths) {
3415 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3416 502b9684 2020-07-31 stsp commit, repo);
3417 502b9684 2020-07-31 stsp if (err)
3418 502b9684 2020-07-31 stsp break;
3419 502b9684 2020-07-31 stsp }
3420 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3421 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3422 dbec59df 2020-04-18 stsp show_patch, diff_context, refs);
3423 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3424 dbec59df 2020-04-18 stsp if (err)
3425 dbec59df 2020-04-18 stsp break;
3426 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3427 502b9684 2020-07-31 stsp free((char *)pe->path);
3428 502b9684 2020-07-31 stsp free(pe->data);
3429 502b9684 2020-07-31 stsp }
3430 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3431 dbec59df 2020-04-18 stsp }
3432 dbec59df 2020-04-18 stsp }
3433 fcc85cad 2018-07-22 stsp done:
3434 dbec59df 2020-04-18 stsp while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3435 dbec59df 2020-04-18 stsp qid = SIMPLEQ_FIRST(&reversed_commits);
3436 dbec59df 2020-04-18 stsp SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3437 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3438 dbec59df 2020-04-18 stsp }
3439 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3440 0208f208 2020-05-05 stsp free((char *)pe->path);
3441 0208f208 2020-05-05 stsp free(pe->data);
3442 0208f208 2020-05-05 stsp }
3443 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3444 6841bf13 2019-11-29 kn if (search_pattern)
3445 6841bf13 2019-11-29 kn regfree(&regex);
3446 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3447 f42b1b34 2018-03-12 stsp return err;
3448 f42b1b34 2018-03-12 stsp }
3449 5c860e29 2018-03-12 stsp
3450 4ed7e80c 2018-05-20 stsp __dead static void
3451 6f3d1eb0 2018-03-12 stsp usage_log(void)
3452 6f3d1eb0 2018-03-12 stsp {
3453 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3454 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3455 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3456 6f3d1eb0 2018-03-12 stsp exit(1);
3457 b1ebc001 2019-08-13 stsp }
3458 b1ebc001 2019-08-13 stsp
3459 b1ebc001 2019-08-13 stsp static int
3460 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3461 b1ebc001 2019-08-13 stsp {
3462 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3463 b1ebc001 2019-08-13 stsp long long n;
3464 b1ebc001 2019-08-13 stsp const char *errstr;
3465 b1ebc001 2019-08-13 stsp
3466 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3467 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3468 b1ebc001 2019-08-13 stsp return 0;
3469 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3470 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3471 b1ebc001 2019-08-13 stsp return 0;
3472 b1ebc001 2019-08-13 stsp return n;
3473 d1fe46f9 2020-04-18 stsp }
3474 d1fe46f9 2020-04-18 stsp
3475 d1fe46f9 2020-04-18 stsp static const struct got_error *
3476 d1fe46f9 2020-04-18 stsp resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3477 d1fe46f9 2020-04-18 stsp struct got_repository *repo)
3478 d1fe46f9 2020-04-18 stsp {
3479 d1fe46f9 2020-04-18 stsp const struct got_error *err = NULL;
3480 d1fe46f9 2020-04-18 stsp struct got_reference *ref;
3481 d1fe46f9 2020-04-18 stsp
3482 d1fe46f9 2020-04-18 stsp *id = NULL;
3483 d1fe46f9 2020-04-18 stsp
3484 d1fe46f9 2020-04-18 stsp err = got_ref_open(&ref, repo, commit_arg, 0);
3485 d1fe46f9 2020-04-18 stsp if (err == NULL) {
3486 d1fe46f9 2020-04-18 stsp int obj_type;
3487 d1fe46f9 2020-04-18 stsp err = got_ref_resolve(id, repo, ref);
3488 d1fe46f9 2020-04-18 stsp got_ref_close(ref);
3489 d1fe46f9 2020-04-18 stsp if (err)
3490 d1fe46f9 2020-04-18 stsp return err;
3491 d1fe46f9 2020-04-18 stsp err = got_object_get_type(&obj_type, repo, *id);
3492 d1fe46f9 2020-04-18 stsp if (err)
3493 d1fe46f9 2020-04-18 stsp return err;
3494 d1fe46f9 2020-04-18 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3495 d1fe46f9 2020-04-18 stsp struct got_tag_object *tag;
3496 d1fe46f9 2020-04-18 stsp err = got_object_open_as_tag(&tag, repo, *id);
3497 d1fe46f9 2020-04-18 stsp if (err)
3498 d1fe46f9 2020-04-18 stsp return err;
3499 d1fe46f9 2020-04-18 stsp if (got_object_tag_get_object_type(tag) !=
3500 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT) {
3501 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3502 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3503 d1fe46f9 2020-04-18 stsp }
3504 d1fe46f9 2020-04-18 stsp free(*id);
3505 d1fe46f9 2020-04-18 stsp *id = got_object_id_dup(
3506 d1fe46f9 2020-04-18 stsp got_object_tag_get_object_id(tag));
3507 d1fe46f9 2020-04-18 stsp if (*id == NULL)
3508 d1fe46f9 2020-04-18 stsp err = got_error_from_errno(
3509 d1fe46f9 2020-04-18 stsp "got_object_id_dup");
3510 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3511 d1fe46f9 2020-04-18 stsp if (err)
3512 d1fe46f9 2020-04-18 stsp return err;
3513 d1fe46f9 2020-04-18 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3514 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3515 d1fe46f9 2020-04-18 stsp } else {
3516 d1fe46f9 2020-04-18 stsp err = got_repo_match_object_id_prefix(id, commit_arg,
3517 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT, repo);
3518 d1fe46f9 2020-04-18 stsp }
3519 d1fe46f9 2020-04-18 stsp
3520 d1fe46f9 2020-04-18 stsp return err;
3521 6f3d1eb0 2018-03-12 stsp }
3522 6f3d1eb0 2018-03-12 stsp
3523 4ed7e80c 2018-05-20 stsp static const struct got_error *
3524 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3525 f42b1b34 2018-03-12 stsp {
3526 f42b1b34 2018-03-12 stsp const struct got_error *error;
3527 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3528 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3529 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3530 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3531 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3532 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3533 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3534 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3535 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3536 64a96a6d 2018-04-01 stsp const char *errstr;
3537 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3538 1b3893a2 2019-03-18 stsp
3539 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
3540 5c860e29 2018-03-12 stsp
3541 6715a751 2018-03-16 stsp #ifndef PROFILE
3542 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3543 6098196c 2019-01-04 stsp NULL)
3544 ad242220 2018-09-08 stsp == -1)
3545 f42b1b34 2018-03-12 stsp err(1, "pledge");
3546 6715a751 2018-03-16 stsp #endif
3547 79109fed 2018-03-27 stsp
3548 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3549 b1ebc001 2019-08-13 stsp
3550 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3551 79109fed 2018-03-27 stsp switch (ch) {
3552 79109fed 2018-03-27 stsp case 'p':
3553 79109fed 2018-03-27 stsp show_patch = 1;
3554 d142fc45 2018-04-01 stsp break;
3555 0208f208 2020-05-05 stsp case 'P':
3556 0208f208 2020-05-05 stsp show_changed_paths = 1;
3557 0208f208 2020-05-05 stsp break;
3558 d142fc45 2018-04-01 stsp case 'c':
3559 d142fc45 2018-04-01 stsp start_commit = optarg;
3560 64a96a6d 2018-04-01 stsp break;
3561 c0cc5c62 2018-10-18 stsp case 'C':
3562 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3563 4a8520aa 2018-10-18 stsp &errstr);
3564 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3565 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3566 c0cc5c62 2018-10-18 stsp break;
3567 64a96a6d 2018-04-01 stsp case 'l':
3568 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3569 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3570 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3571 cc54c501 2019-07-15 stsp break;
3572 48c8c60d 2020-01-27 stsp case 'b':
3573 48c8c60d 2020-01-27 stsp log_branches = 1;
3574 a0603db2 2018-06-10 stsp break;
3575 04ca23f4 2018-07-16 stsp case 'r':
3576 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3577 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3578 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3579 9ba1d308 2019-10-21 stsp optarg);
3580 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3581 04ca23f4 2018-07-16 stsp break;
3582 dbec59df 2020-04-18 stsp case 'R':
3583 dbec59df 2020-04-18 stsp reverse_display_order = 1;
3584 dbec59df 2020-04-18 stsp break;
3585 6841bf13 2019-11-29 kn case 's':
3586 6841bf13 2019-11-29 kn search_pattern = optarg;
3587 6841bf13 2019-11-29 kn break;
3588 d1fe46f9 2020-04-18 stsp case 'x':
3589 d1fe46f9 2020-04-18 stsp end_commit = optarg;
3590 d1fe46f9 2020-04-18 stsp break;
3591 79109fed 2018-03-27 stsp default:
3592 2deda0b9 2019-03-07 stsp usage_log();
3593 79109fed 2018-03-27 stsp /* NOTREACHED */
3594 79109fed 2018-03-27 stsp }
3595 79109fed 2018-03-27 stsp }
3596 79109fed 2018-03-27 stsp
3597 79109fed 2018-03-27 stsp argc -= optind;
3598 79109fed 2018-03-27 stsp argv += optind;
3599 f42b1b34 2018-03-12 stsp
3600 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3601 dc1edbfa 2019-11-29 kn diff_context = 3;
3602 dc1edbfa 2019-11-29 kn else if (!show_patch)
3603 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
3604 dc1edbfa 2019-11-29 kn
3605 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3606 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3607 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3608 04ca23f4 2018-07-16 stsp goto done;
3609 04ca23f4 2018-07-16 stsp }
3610 cffc0aa4 2019-02-05 stsp
3611 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
3612 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
3613 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3614 50f2fada 2020-04-24 stsp goto done;
3615 50f2fada 2020-04-24 stsp error = NULL;
3616 50f2fada 2020-04-24 stsp }
3617 cffc0aa4 2019-02-05 stsp
3618 e7301579 2019-03-18 stsp if (argc == 0) {
3619 cbd1af7a 2019-03-18 stsp path = strdup("");
3620 e7301579 2019-03-18 stsp if (path == NULL) {
3621 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3622 cbd1af7a 2019-03-18 stsp goto done;
3623 e7301579 2019-03-18 stsp }
3624 e7301579 2019-03-18 stsp } else if (argc == 1) {
3625 e7301579 2019-03-18 stsp if (worktree) {
3626 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3627 e7301579 2019-03-18 stsp argv[0]);
3628 e7301579 2019-03-18 stsp if (error)
3629 e7301579 2019-03-18 stsp goto done;
3630 e7301579 2019-03-18 stsp } else {
3631 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3632 e7301579 2019-03-18 stsp if (path == NULL) {
3633 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3634 e7301579 2019-03-18 stsp goto done;
3635 e7301579 2019-03-18 stsp }
3636 e7301579 2019-03-18 stsp }
3637 cbd1af7a 2019-03-18 stsp } else
3638 cbd1af7a 2019-03-18 stsp usage_log();
3639 cbd1af7a 2019-03-18 stsp
3640 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3641 5486daa2 2019-05-11 stsp repo_path = worktree ?
3642 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3643 5486daa2 2019-05-11 stsp }
3644 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3645 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3646 cffc0aa4 2019-02-05 stsp goto done;
3647 04ca23f4 2018-07-16 stsp }
3648 6098196c 2019-01-04 stsp
3649 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3650 d7d4f210 2018-03-12 stsp if (error != NULL)
3651 04ca23f4 2018-07-16 stsp goto done;
3652 f42b1b34 2018-03-12 stsp
3653 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3654 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3655 c02c541e 2019-03-29 stsp if (error)
3656 c02c541e 2019-03-29 stsp goto done;
3657 c02c541e 2019-03-29 stsp
3658 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3659 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3660 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
3661 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3662 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3663 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3664 3235492e 2018-04-01 stsp if (error != NULL)
3665 d1fe46f9 2020-04-18 stsp goto done;
3666 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
3667 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3668 3235492e 2018-04-01 stsp if (error != NULL)
3669 d1fe46f9 2020-04-18 stsp goto done;
3670 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
3671 d1fe46f9 2020-04-18 stsp start_id);
3672 d1fe46f9 2020-04-18 stsp if (error != NULL)
3673 d1fe46f9 2020-04-18 stsp goto done;
3674 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
3675 3235492e 2018-04-01 stsp } else {
3676 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&start_id, start_commit, repo);
3677 d1fe46f9 2020-04-18 stsp if (error != NULL)
3678 d1fe46f9 2020-04-18 stsp goto done;
3679 3235492e 2018-04-01 stsp }
3680 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
3681 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&end_id, end_commit, repo);
3682 d1fe46f9 2020-04-18 stsp if (error != NULL)
3683 d1fe46f9 2020-04-18 stsp goto done;
3684 d1fe46f9 2020-04-18 stsp }
3685 04ca23f4 2018-07-16 stsp
3686 deeabeae 2019-08-27 stsp if (worktree) {
3687 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3688 deeabeae 2019-08-27 stsp char *p;
3689 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
3690 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3691 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
3692 deeabeae 2019-08-27 stsp goto done;
3693 deeabeae 2019-08-27 stsp }
3694 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3695 deeabeae 2019-08-27 stsp free(p);
3696 deeabeae 2019-08-27 stsp } else
3697 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3698 04ca23f4 2018-07-16 stsp if (error != NULL)
3699 04ca23f4 2018-07-16 stsp goto done;
3700 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3701 04ca23f4 2018-07-16 stsp free(path);
3702 04ca23f4 2018-07-16 stsp path = in_repo_path;
3703 04ca23f4 2018-07-16 stsp }
3704 199a4027 2019-02-02 stsp
3705 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3706 199a4027 2019-02-02 stsp if (error)
3707 199a4027 2019-02-02 stsp goto done;
3708 04ca23f4 2018-07-16 stsp
3709 0208f208 2020-05-05 stsp error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3710 0208f208 2020-05-05 stsp show_patch, search_pattern, diff_context, limit, log_branches,
3711 dbec59df 2020-04-18 stsp reverse_display_order, &refs);
3712 04ca23f4 2018-07-16 stsp done:
3713 04ca23f4 2018-07-16 stsp free(path);
3714 04ca23f4 2018-07-16 stsp free(repo_path);
3715 04ca23f4 2018-07-16 stsp free(cwd);
3716 cffc0aa4 2019-02-05 stsp if (worktree)
3717 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3718 ad242220 2018-09-08 stsp if (repo) {
3719 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3720 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3721 ad242220 2018-09-08 stsp if (error == NULL)
3722 ad242220 2018-09-08 stsp error = repo_error;
3723 ad242220 2018-09-08 stsp }
3724 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3725 8bf5b3c9 2018-03-17 stsp return error;
3726 5c860e29 2018-03-12 stsp }
3727 5c860e29 2018-03-12 stsp
3728 4ed7e80c 2018-05-20 stsp __dead static void
3729 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3730 3f8b7d6a 2018-04-01 stsp {
3731 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3732 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3733 3f8b7d6a 2018-04-01 stsp exit(1);
3734 b00d56cd 2018-04-01 stsp }
3735 b00d56cd 2018-04-01 stsp
3736 b72f483a 2019-02-05 stsp struct print_diff_arg {
3737 b72f483a 2019-02-05 stsp struct got_repository *repo;
3738 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3739 b72f483a 2019-02-05 stsp int diff_context;
3740 3fc0c068 2019-02-10 stsp const char *id_str;
3741 3fc0c068 2019-02-10 stsp int header_shown;
3742 98eaaa12 2019-08-03 stsp int diff_staged;
3743 63035f9f 2019-10-06 stsp int ignore_whitespace;
3744 b72f483a 2019-02-05 stsp };
3745 39449a05 2020-07-23 stsp
3746 39449a05 2020-07-23 stsp /*
3747 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
3748 39449a05 2020-07-23 stsp * it as content to the diff engine.
3749 39449a05 2020-07-23 stsp */
3750 39449a05 2020-07-23 stsp static const struct got_error *
3751 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3752 39449a05 2020-07-23 stsp const char *abspath)
3753 39449a05 2020-07-23 stsp {
3754 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
3755 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
3756 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
3757 39449a05 2020-07-23 stsp
3758 39449a05 2020-07-23 stsp *fd = -1;
3759 39449a05 2020-07-23 stsp
3760 39449a05 2020-07-23 stsp if (dirfd != -1) {
3761 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3762 39449a05 2020-07-23 stsp if (target_len == -1)
3763 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
3764 39449a05 2020-07-23 stsp } else {
3765 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
3766 39449a05 2020-07-23 stsp if (target_len == -1)
3767 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
3768 39449a05 2020-07-23 stsp }
3769 39449a05 2020-07-23 stsp
3770 39449a05 2020-07-23 stsp *fd = got_opentempfd();
3771 39449a05 2020-07-23 stsp if (*fd == -1)
3772 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
3773 39449a05 2020-07-23 stsp
3774 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
3775 39449a05 2020-07-23 stsp if (outlen == -1) {
3776 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
3777 39449a05 2020-07-23 stsp goto done;
3778 39449a05 2020-07-23 stsp }
3779 39449a05 2020-07-23 stsp
3780 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
3781 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
3782 39449a05 2020-07-23 stsp goto done;
3783 39449a05 2020-07-23 stsp }
3784 39449a05 2020-07-23 stsp done:
3785 39449a05 2020-07-23 stsp if (err) {
3786 39449a05 2020-07-23 stsp close(*fd);
3787 39449a05 2020-07-23 stsp *fd = -1;
3788 39449a05 2020-07-23 stsp }
3789 39449a05 2020-07-23 stsp return err;
3790 39449a05 2020-07-23 stsp }
3791 b72f483a 2019-02-05 stsp
3792 4ed7e80c 2018-05-20 stsp static const struct got_error *
3793 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3794 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3795 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3796 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3797 b72f483a 2019-02-05 stsp {
3798 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3799 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3800 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
3801 12463d8b 2019-12-13 stsp int fd = -1;
3802 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
3803 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
3804 b72f483a 2019-02-05 stsp struct stat sb;
3805 b72f483a 2019-02-05 stsp
3806 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3807 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3808 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3809 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3810 98eaaa12 2019-08-03 stsp return NULL;
3811 98eaaa12 2019-08-03 stsp } else {
3812 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3813 98eaaa12 2019-08-03 stsp return NULL;
3814 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3815 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
3816 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3817 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
3818 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
3819 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
3820 98eaaa12 2019-08-03 stsp return NULL;
3821 98eaaa12 2019-08-03 stsp }
3822 3fc0c068 2019-02-10 stsp
3823 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
3824 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
3825 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
3826 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
3827 3fc0c068 2019-02-10 stsp a->header_shown = 1;
3828 3fc0c068 2019-02-10 stsp }
3829 b72f483a 2019-02-05 stsp
3830 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3831 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
3832 98eaaa12 2019-08-03 stsp switch (staged_status) {
3833 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
3834 98eaaa12 2019-08-03 stsp label1 = path;
3835 98eaaa12 2019-08-03 stsp label2 = path;
3836 98eaaa12 2019-08-03 stsp break;
3837 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
3838 98eaaa12 2019-08-03 stsp label2 = path;
3839 98eaaa12 2019-08-03 stsp break;
3840 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
3841 98eaaa12 2019-08-03 stsp label1 = path;
3842 98eaaa12 2019-08-03 stsp break;
3843 98eaaa12 2019-08-03 stsp default:
3844 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
3845 98eaaa12 2019-08-03 stsp }
3846 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3847 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3848 63035f9f 2019-10-06 stsp a->repo, stdout);
3849 98eaaa12 2019-08-03 stsp }
3850 98eaaa12 2019-08-03 stsp
3851 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3852 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
3853 4ce46740 2019-08-08 stsp char *id_str;
3854 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3855 408b4ebc 2019-08-03 stsp 8192);
3856 4ce46740 2019-08-08 stsp if (err)
3857 4ce46740 2019-08-08 stsp goto done;
3858 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
3859 4ce46740 2019-08-08 stsp if (err)
3860 4ce46740 2019-08-08 stsp goto done;
3861 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3862 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
3863 4ce46740 2019-08-08 stsp free(id_str);
3864 4ce46740 2019-08-08 stsp goto done;
3865 4ce46740 2019-08-08 stsp }
3866 4ce46740 2019-08-08 stsp free(id_str);
3867 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
3868 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3869 4ce46740 2019-08-08 stsp if (err)
3870 4ce46740 2019-08-08 stsp goto done;
3871 4ce46740 2019-08-08 stsp }
3872 d00136be 2019-03-26 stsp
3873 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
3874 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
3875 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
3876 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3877 2ec1f75b 2019-03-26 stsp goto done;
3878 2ec1f75b 2019-03-26 stsp }
3879 b72f483a 2019-02-05 stsp
3880 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3881 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3882 12463d8b 2019-12-13 stsp if (fd == -1) {
3883 39449a05 2020-07-23 stsp if (errno != ELOOP) {
3884 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
3885 39449a05 2020-07-23 stsp abspath);
3886 39449a05 2020-07-23 stsp goto done;
3887 39449a05 2020-07-23 stsp }
3888 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
3889 39449a05 2020-07-23 stsp de_name, abspath);
3890 39449a05 2020-07-23 stsp if (err)
3891 39449a05 2020-07-23 stsp goto done;
3892 12463d8b 2019-12-13 stsp }
3893 12463d8b 2019-12-13 stsp } else {
3894 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3895 12463d8b 2019-12-13 stsp if (fd == -1) {
3896 39449a05 2020-07-23 stsp if (errno != ELOOP) {
3897 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
3898 39449a05 2020-07-23 stsp abspath);
3899 39449a05 2020-07-23 stsp goto done;
3900 39449a05 2020-07-23 stsp }
3901 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
3902 39449a05 2020-07-23 stsp de_name, abspath);
3903 39449a05 2020-07-23 stsp if (err)
3904 39449a05 2020-07-23 stsp goto done;
3905 12463d8b 2019-12-13 stsp }
3906 12463d8b 2019-12-13 stsp }
3907 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
3908 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
3909 2ec1f75b 2019-03-26 stsp goto done;
3910 2ec1f75b 2019-03-26 stsp }
3911 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
3912 12463d8b 2019-12-13 stsp if (f2 == NULL) {
3913 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
3914 2ec1f75b 2019-03-26 stsp goto done;
3915 2ec1f75b 2019-03-26 stsp }
3916 12463d8b 2019-12-13 stsp fd = -1;
3917 2ec1f75b 2019-03-26 stsp } else
3918 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
3919 b72f483a 2019-02-05 stsp
3920 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3921 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
3922 b72f483a 2019-02-05 stsp done:
3923 b72f483a 2019-02-05 stsp if (blob1)
3924 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
3925 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3926 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3927 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3928 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
3929 b72f483a 2019-02-05 stsp free(abspath);
3930 927df6b7 2019-02-10 stsp return err;
3931 927df6b7 2019-02-10 stsp }
3932 927df6b7 2019-02-10 stsp
3933 927df6b7 2019-02-10 stsp static const struct got_error *
3934 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
3935 b00d56cd 2018-04-01 stsp {
3936 b00d56cd 2018-04-01 stsp const struct got_error *error;
3937 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
3938 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
3939 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
3940 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3941 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
3942 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
3943 15a94983 2018-12-23 stsp int type1, type2;
3944 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3945 c0cc5c62 2018-10-18 stsp const char *errstr;
3946 927df6b7 2019-02-10 stsp char *path = NULL;
3947 b00d56cd 2018-04-01 stsp
3948 b00d56cd 2018-04-01 stsp #ifndef PROFILE
3949 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3950 25eccc22 2019-01-04 stsp NULL) == -1)
3951 b00d56cd 2018-04-01 stsp err(1, "pledge");
3952 b00d56cd 2018-04-01 stsp #endif
3953 b00d56cd 2018-04-01 stsp
3954 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3955 b00d56cd 2018-04-01 stsp switch (ch) {
3956 c0cc5c62 2018-10-18 stsp case 'C':
3957 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3958 dfcab68b 2019-11-29 kn &errstr);
3959 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3960 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3961 c0cc5c62 2018-10-18 stsp break;
3962 b72f483a 2019-02-05 stsp case 'r':
3963 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
3964 b72f483a 2019-02-05 stsp if (repo_path == NULL)
3965 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3966 9ba1d308 2019-10-21 stsp optarg);
3967 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3968 b72f483a 2019-02-05 stsp break;
3969 98eaaa12 2019-08-03 stsp case 's':
3970 98eaaa12 2019-08-03 stsp diff_staged = 1;
3971 63035f9f 2019-10-06 stsp break;
3972 63035f9f 2019-10-06 stsp case 'w':
3973 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
3974 98eaaa12 2019-08-03 stsp break;
3975 b00d56cd 2018-04-01 stsp default:
3976 2deda0b9 2019-03-07 stsp usage_diff();
3977 b00d56cd 2018-04-01 stsp /* NOTREACHED */
3978 b00d56cd 2018-04-01 stsp }
3979 b00d56cd 2018-04-01 stsp }
3980 b00d56cd 2018-04-01 stsp
3981 b00d56cd 2018-04-01 stsp argc -= optind;
3982 b00d56cd 2018-04-01 stsp argv += optind;
3983 b00d56cd 2018-04-01 stsp
3984 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
3985 b72f483a 2019-02-05 stsp if (cwd == NULL) {
3986 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3987 b72f483a 2019-02-05 stsp goto done;
3988 b72f483a 2019-02-05 stsp }
3989 927df6b7 2019-02-10 stsp if (argc <= 1) {
3990 b72f483a 2019-02-05 stsp if (repo_path)
3991 b72f483a 2019-02-05 stsp errx(1,
3992 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
3993 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3994 fa51e947 2020-03-27 stsp if (error) {
3995 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3996 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
3997 fa51e947 2020-03-27 stsp cwd);
3998 1f03b8da 2020-03-20 stsp goto done;
3999 fa51e947 2020-03-27 stsp }
4000 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4001 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4002 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4003 927df6b7 2019-02-10 stsp goto done;
4004 927df6b7 2019-02-10 stsp }
4005 927df6b7 2019-02-10 stsp if (argc == 1) {
4006 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4007 cbd1af7a 2019-03-18 stsp argv[0]);
4008 927df6b7 2019-02-10 stsp if (error)
4009 927df6b7 2019-02-10 stsp goto done;
4010 927df6b7 2019-02-10 stsp } else {
4011 927df6b7 2019-02-10 stsp path = strdup("");
4012 927df6b7 2019-02-10 stsp if (path == NULL) {
4013 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4014 927df6b7 2019-02-10 stsp goto done;
4015 927df6b7 2019-02-10 stsp }
4016 927df6b7 2019-02-10 stsp }
4017 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4018 98eaaa12 2019-08-03 stsp if (diff_staged)
4019 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4020 98eaaa12 2019-08-03 stsp "objects in repository");
4021 15a94983 2018-12-23 stsp id_str1 = argv[0];
4022 15a94983 2018-12-23 stsp id_str2 = argv[1];
4023 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4024 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4025 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4026 30db809c 2019-06-05 stsp goto done;
4027 1f03b8da 2020-03-20 stsp if (worktree) {
4028 1f03b8da 2020-03-20 stsp repo_path = strdup(
4029 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
4030 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4031 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4032 1f03b8da 2020-03-20 stsp goto done;
4033 1f03b8da 2020-03-20 stsp }
4034 1f03b8da 2020-03-20 stsp } else {
4035 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
4036 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4037 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4038 1f03b8da 2020-03-20 stsp goto done;
4039 1f03b8da 2020-03-20 stsp }
4040 30db809c 2019-06-05 stsp }
4041 30db809c 2019-06-05 stsp }
4042 b00d56cd 2018-04-01 stsp } else
4043 b00d56cd 2018-04-01 stsp usage_diff();
4044 b00d56cd 2018-04-01 stsp
4045 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4046 76089277 2018-04-01 stsp free(repo_path);
4047 b00d56cd 2018-04-01 stsp if (error != NULL)
4048 b00d56cd 2018-04-01 stsp goto done;
4049 b00d56cd 2018-04-01 stsp
4050 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4051 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4052 c02c541e 2019-03-29 stsp if (error)
4053 c02c541e 2019-03-29 stsp goto done;
4054 c02c541e 2019-03-29 stsp
4055 30db809c 2019-06-05 stsp if (argc <= 1) {
4056 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4057 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4058 b72f483a 2019-02-05 stsp char *id_str;
4059 72ea6654 2019-07-27 stsp
4060 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4061 72ea6654 2019-07-27 stsp
4062 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4063 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4064 b72f483a 2019-02-05 stsp if (error)
4065 b72f483a 2019-02-05 stsp goto done;
4066 b72f483a 2019-02-05 stsp arg.repo = repo;
4067 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4068 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4069 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4070 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4071 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4072 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4073 b72f483a 2019-02-05 stsp
4074 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4075 72ea6654 2019-07-27 stsp if (error)
4076 72ea6654 2019-07-27 stsp goto done;
4077 72ea6654 2019-07-27 stsp
4078 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
4079 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
4080 b72f483a 2019-02-05 stsp free(id_str);
4081 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4082 b72f483a 2019-02-05 stsp goto done;
4083 b72f483a 2019-02-05 stsp }
4084 b72f483a 2019-02-05 stsp
4085 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4086 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4087 0adb7196 2019-08-11 stsp if (error)
4088 0adb7196 2019-08-11 stsp goto done;
4089 b00d56cd 2018-04-01 stsp
4090 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4091 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4092 0adb7196 2019-08-11 stsp if (error)
4093 0adb7196 2019-08-11 stsp goto done;
4094 b00d56cd 2018-04-01 stsp
4095 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4096 15a94983 2018-12-23 stsp if (error)
4097 15a94983 2018-12-23 stsp goto done;
4098 15a94983 2018-12-23 stsp
4099 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4100 15a94983 2018-12-23 stsp if (error)
4101 15a94983 2018-12-23 stsp goto done;
4102 15a94983 2018-12-23 stsp
4103 15a94983 2018-12-23 stsp if (type1 != type2) {
4104 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4105 b00d56cd 2018-04-01 stsp goto done;
4106 b00d56cd 2018-04-01 stsp }
4107 b00d56cd 2018-04-01 stsp
4108 15a94983 2018-12-23 stsp switch (type1) {
4109 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4110 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4111 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4112 b00d56cd 2018-04-01 stsp break;
4113 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4114 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
4115 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4116 b00d56cd 2018-04-01 stsp break;
4117 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4118 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4119 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
4120 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
4121 b00d56cd 2018-04-01 stsp break;
4122 b00d56cd 2018-04-01 stsp default:
4123 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4124 b00d56cd 2018-04-01 stsp }
4125 b00d56cd 2018-04-01 stsp done:
4126 5e70831e 2019-05-28 stsp free(label1);
4127 5e70831e 2019-05-28 stsp free(label2);
4128 15a94983 2018-12-23 stsp free(id1);
4129 15a94983 2018-12-23 stsp free(id2);
4130 927df6b7 2019-02-10 stsp free(path);
4131 b72f483a 2019-02-05 stsp if (worktree)
4132 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4133 ad242220 2018-09-08 stsp if (repo) {
4134 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4135 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4136 ad242220 2018-09-08 stsp if (error == NULL)
4137 ad242220 2018-09-08 stsp error = repo_error;
4138 ad242220 2018-09-08 stsp }
4139 b00d56cd 2018-04-01 stsp return error;
4140 404c43c4 2018-06-21 stsp }
4141 404c43c4 2018-06-21 stsp
4142 404c43c4 2018-06-21 stsp __dead static void
4143 404c43c4 2018-06-21 stsp usage_blame(void)
4144 404c43c4 2018-06-21 stsp {
4145 9270e621 2019-02-05 stsp fprintf(stderr,
4146 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4147 404c43c4 2018-06-21 stsp getprogname());
4148 404c43c4 2018-06-21 stsp exit(1);
4149 b00d56cd 2018-04-01 stsp }
4150 b00d56cd 2018-04-01 stsp
4151 28315671 2019-08-14 stsp struct blame_line {
4152 28315671 2019-08-14 stsp int annotated;
4153 28315671 2019-08-14 stsp char *id_str;
4154 82f6abb8 2019-08-14 stsp char *committer;
4155 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4156 28315671 2019-08-14 stsp };
4157 28315671 2019-08-14 stsp
4158 28315671 2019-08-14 stsp struct blame_cb_args {
4159 28315671 2019-08-14 stsp struct blame_line *lines;
4160 28315671 2019-08-14 stsp int nlines;
4161 7ef28ff8 2019-08-14 stsp int nlines_prec;
4162 28315671 2019-08-14 stsp int lineno_cur;
4163 28315671 2019-08-14 stsp off_t *line_offsets;
4164 28315671 2019-08-14 stsp FILE *f;
4165 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4166 28315671 2019-08-14 stsp };
4167 28315671 2019-08-14 stsp
4168 404c43c4 2018-06-21 stsp static const struct got_error *
4169 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4170 28315671 2019-08-14 stsp {
4171 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4172 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4173 28315671 2019-08-14 stsp struct blame_line *bline;
4174 82f6abb8 2019-08-14 stsp char *line = NULL;
4175 28315671 2019-08-14 stsp size_t linesize = 0;
4176 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4177 28315671 2019-08-14 stsp off_t offset;
4178 bcb49d15 2019-08-14 stsp struct tm tm;
4179 bcb49d15 2019-08-14 stsp time_t committer_time;
4180 28315671 2019-08-14 stsp
4181 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4182 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4183 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4184 28315671 2019-08-14 stsp
4185 28315671 2019-08-14 stsp if (sigint_received)
4186 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4187 28315671 2019-08-14 stsp
4188 2839f8b9 2019-08-18 stsp if (lineno == -1)
4189 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4190 2839f8b9 2019-08-18 stsp
4191 28315671 2019-08-14 stsp /* Annotate this line. */
4192 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4193 28315671 2019-08-14 stsp if (bline->annotated)
4194 28315671 2019-08-14 stsp return NULL;
4195 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4196 28315671 2019-08-14 stsp if (err)
4197 28315671 2019-08-14 stsp return err;
4198 82f6abb8 2019-08-14 stsp
4199 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4200 82f6abb8 2019-08-14 stsp if (err)
4201 82f6abb8 2019-08-14 stsp goto done;
4202 82f6abb8 2019-08-14 stsp
4203 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4204 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4205 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4206 bcb49d15 2019-08-14 stsp goto done;
4207 bcb49d15 2019-08-14 stsp }
4208 bcb49d15 2019-08-14 stsp
4209 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4210 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
4211 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
4212 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4213 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
4214 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4215 82f6abb8 2019-08-14 stsp goto done;
4216 82f6abb8 2019-08-14 stsp }
4217 28315671 2019-08-14 stsp bline->annotated = 1;
4218 28315671 2019-08-14 stsp
4219 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4220 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4221 28315671 2019-08-14 stsp if (!bline->annotated)
4222 82f6abb8 2019-08-14 stsp goto done;
4223 28315671 2019-08-14 stsp
4224 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4225 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4226 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4227 82f6abb8 2019-08-14 stsp goto done;
4228 82f6abb8 2019-08-14 stsp }
4229 28315671 2019-08-14 stsp
4230 28315671 2019-08-14 stsp while (bline->annotated) {
4231 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4232 82f6abb8 2019-08-14 stsp size_t len;
4233 82f6abb8 2019-08-14 stsp
4234 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4235 28315671 2019-08-14 stsp if (ferror(a->f))
4236 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4237 28315671 2019-08-14 stsp break;
4238 28315671 2019-08-14 stsp }
4239 82f6abb8 2019-08-14 stsp
4240 82f6abb8 2019-08-14 stsp committer = bline->committer;
4241 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4242 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4243 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4244 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4245 82f6abb8 2019-08-14 stsp if (at)
4246 82f6abb8 2019-08-14 stsp *at = '\0';
4247 82f6abb8 2019-08-14 stsp len = strlen(committer);
4248 82f6abb8 2019-08-14 stsp if (len >= 9)
4249 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4250 28315671 2019-08-14 stsp
4251 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4252 28315671 2019-08-14 stsp if (nl)
4253 28315671 2019-08-14 stsp *nl = '\0';
4254 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4255 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4256 28315671 2019-08-14 stsp
4257 28315671 2019-08-14 stsp a->lineno_cur++;
4258 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4259 28315671 2019-08-14 stsp }
4260 82f6abb8 2019-08-14 stsp done:
4261 82f6abb8 2019-08-14 stsp if (commit)
4262 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4263 28315671 2019-08-14 stsp free(line);
4264 28315671 2019-08-14 stsp return err;
4265 28315671 2019-08-14 stsp }
4266 28315671 2019-08-14 stsp
4267 28315671 2019-08-14 stsp static const struct got_error *
4268 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4269 404c43c4 2018-06-21 stsp {
4270 404c43c4 2018-06-21 stsp const struct got_error *error;
4271 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4272 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4273 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4274 0587e10c 2020-07-23 stsp char *link_target = NULL;
4275 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4276 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4277 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4278 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4279 28315671 2019-08-14 stsp struct blame_cb_args bca;
4280 28315671 2019-08-14 stsp int ch, obj_type, i;
4281 b02560ec 2019-08-19 stsp size_t filesize;
4282 90f3c347 2019-08-19 stsp
4283 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4284 404c43c4 2018-06-21 stsp
4285 404c43c4 2018-06-21 stsp #ifndef PROFILE
4286 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4287 36e2fb66 2019-01-04 stsp NULL) == -1)
4288 404c43c4 2018-06-21 stsp err(1, "pledge");
4289 404c43c4 2018-06-21 stsp #endif
4290 404c43c4 2018-06-21 stsp
4291 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4292 404c43c4 2018-06-21 stsp switch (ch) {
4293 404c43c4 2018-06-21 stsp case 'c':
4294 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4295 404c43c4 2018-06-21 stsp break;
4296 66bea077 2018-08-02 stsp case 'r':
4297 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4298 66bea077 2018-08-02 stsp if (repo_path == NULL)
4299 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4300 9ba1d308 2019-10-21 stsp optarg);
4301 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4302 66bea077 2018-08-02 stsp break;
4303 404c43c4 2018-06-21 stsp default:
4304 2deda0b9 2019-03-07 stsp usage_blame();
4305 404c43c4 2018-06-21 stsp /* NOTREACHED */
4306 404c43c4 2018-06-21 stsp }
4307 404c43c4 2018-06-21 stsp }
4308 404c43c4 2018-06-21 stsp
4309 404c43c4 2018-06-21 stsp argc -= optind;
4310 404c43c4 2018-06-21 stsp argv += optind;
4311 404c43c4 2018-06-21 stsp
4312 a39318fd 2018-08-02 stsp if (argc == 1)
4313 404c43c4 2018-06-21 stsp path = argv[0];
4314 a39318fd 2018-08-02 stsp else
4315 404c43c4 2018-06-21 stsp usage_blame();
4316 404c43c4 2018-06-21 stsp
4317 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4318 66bea077 2018-08-02 stsp if (cwd == NULL) {
4319 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4320 66bea077 2018-08-02 stsp goto done;
4321 66bea077 2018-08-02 stsp }
4322 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4323 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4324 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4325 66bea077 2018-08-02 stsp goto done;
4326 0c06baac 2019-02-05 stsp else
4327 0c06baac 2019-02-05 stsp error = NULL;
4328 0c06baac 2019-02-05 stsp if (worktree) {
4329 0c06baac 2019-02-05 stsp repo_path =
4330 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4331 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4332 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4333 1f03b8da 2020-03-20 stsp if (error)
4334 1f03b8da 2020-03-20 stsp goto done;
4335 1f03b8da 2020-03-20 stsp }
4336 0c06baac 2019-02-05 stsp } else {
4337 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4338 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4339 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4340 0c06baac 2019-02-05 stsp goto done;
4341 0c06baac 2019-02-05 stsp }
4342 66bea077 2018-08-02 stsp }
4343 66bea077 2018-08-02 stsp }
4344 36e2fb66 2019-01-04 stsp
4345 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4346 c02c541e 2019-03-29 stsp if (error != NULL)
4347 36e2fb66 2019-01-04 stsp goto done;
4348 66bea077 2018-08-02 stsp
4349 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4350 c02c541e 2019-03-29 stsp if (error)
4351 404c43c4 2018-06-21 stsp goto done;
4352 404c43c4 2018-06-21 stsp
4353 0c06baac 2019-02-05 stsp if (worktree) {
4354 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4355 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4356 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4357 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
4358 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4359 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4360 6efaaa2d 2019-02-05 stsp path) == -1) {
4361 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4362 0c06baac 2019-02-05 stsp goto done;
4363 0c06baac 2019-02-05 stsp }
4364 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4365 0c06baac 2019-02-05 stsp free(p);
4366 0c06baac 2019-02-05 stsp } else {
4367 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4368 0c06baac 2019-02-05 stsp }
4369 0c06baac 2019-02-05 stsp if (error)
4370 66bea077 2018-08-02 stsp goto done;
4371 66bea077 2018-08-02 stsp
4372 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4373 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4374 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4375 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4376 404c43c4 2018-06-21 stsp if (error != NULL)
4377 66bea077 2018-08-02 stsp goto done;
4378 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4379 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4380 404c43c4 2018-06-21 stsp if (error != NULL)
4381 66bea077 2018-08-02 stsp goto done;
4382 404c43c4 2018-06-21 stsp } else {
4383 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4384 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4385 30837e32 2019-07-25 stsp if (error)
4386 66bea077 2018-08-02 stsp goto done;
4387 404c43c4 2018-06-21 stsp }
4388 404c43c4 2018-06-21 stsp
4389 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4390 0587e10c 2020-07-23 stsp commit_id, repo);
4391 0587e10c 2020-07-23 stsp if (error)
4392 0587e10c 2020-07-23 stsp goto done;
4393 0587e10c 2020-07-23 stsp
4394 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4395 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4396 28315671 2019-08-14 stsp if (error)
4397 28315671 2019-08-14 stsp goto done;
4398 28315671 2019-08-14 stsp
4399 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4400 28315671 2019-08-14 stsp if (error)
4401 28315671 2019-08-14 stsp goto done;
4402 28315671 2019-08-14 stsp
4403 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4404 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4405 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4406 28315671 2019-08-14 stsp goto done;
4407 28315671 2019-08-14 stsp }
4408 28315671 2019-08-14 stsp
4409 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4410 28315671 2019-08-14 stsp if (error)
4411 28315671 2019-08-14 stsp goto done;
4412 28315671 2019-08-14 stsp bca.f = got_opentemp();
4413 28315671 2019-08-14 stsp if (bca.f == NULL) {
4414 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4415 28315671 2019-08-14 stsp goto done;
4416 28315671 2019-08-14 stsp }
4417 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4418 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4419 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4420 28315671 2019-08-14 stsp goto done;
4421 28315671 2019-08-14 stsp
4422 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4423 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4424 b02560ec 2019-08-19 stsp bca.nlines--;
4425 b02560ec 2019-08-19 stsp
4426 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4427 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4428 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4429 28315671 2019-08-14 stsp goto done;
4430 28315671 2019-08-14 stsp }
4431 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4432 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4433 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4434 7ef28ff8 2019-08-14 stsp while (i > 0) {
4435 7ef28ff8 2019-08-14 stsp i /= 10;
4436 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4437 7ef28ff8 2019-08-14 stsp }
4438 82f6abb8 2019-08-14 stsp bca.repo = repo;
4439 7ef28ff8 2019-08-14 stsp
4440 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4441 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4442 404c43c4 2018-06-21 stsp done:
4443 66bea077 2018-08-02 stsp free(in_repo_path);
4444 0587e10c 2020-07-23 stsp free(link_target);
4445 66bea077 2018-08-02 stsp free(repo_path);
4446 66bea077 2018-08-02 stsp free(cwd);
4447 404c43c4 2018-06-21 stsp free(commit_id);
4448 28315671 2019-08-14 stsp free(obj_id);
4449 28315671 2019-08-14 stsp if (blob)
4450 28315671 2019-08-14 stsp got_object_blob_close(blob);
4451 0c06baac 2019-02-05 stsp if (worktree)
4452 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4453 ad242220 2018-09-08 stsp if (repo) {
4454 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4455 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4456 ad242220 2018-09-08 stsp if (error == NULL)
4457 ad242220 2018-09-08 stsp error = repo_error;
4458 ad242220 2018-09-08 stsp }
4459 3affba96 2019-09-22 stsp if (bca.lines) {
4460 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4461 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4462 3affba96 2019-09-22 stsp free(bline->id_str);
4463 3affba96 2019-09-22 stsp free(bline->committer);
4464 3affba96 2019-09-22 stsp }
4465 3affba96 2019-09-22 stsp free(bca.lines);
4466 28315671 2019-08-14 stsp }
4467 28315671 2019-08-14 stsp free(bca.line_offsets);
4468 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4469 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4470 404c43c4 2018-06-21 stsp return error;
4471 5de5890b 2018-10-18 stsp }
4472 5de5890b 2018-10-18 stsp
4473 5de5890b 2018-10-18 stsp __dead static void
4474 5de5890b 2018-10-18 stsp usage_tree(void)
4475 5de5890b 2018-10-18 stsp {
4476 c1669e2e 2019-01-09 stsp fprintf(stderr,
4477 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4478 5de5890b 2018-10-18 stsp getprogname());
4479 5de5890b 2018-10-18 stsp exit(1);
4480 5de5890b 2018-10-18 stsp }
4481 5de5890b 2018-10-18 stsp
4482 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4483 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4484 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4485 c1669e2e 2019-01-09 stsp {
4486 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4487 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4488 848d6979 2019-08-12 stsp const char *modestr = "";
4489 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4490 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4491 5de5890b 2018-10-18 stsp
4492 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4493 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4494 c1669e2e 2019-01-09 stsp path++;
4495 c1669e2e 2019-01-09 stsp
4496 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4497 63c5ca5d 2019-08-24 stsp modestr = "$";
4498 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4499 0d6c6ee3 2020-05-20 stsp int i;
4500 0d6c6ee3 2020-05-20 stsp
4501 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4502 0d6c6ee3 2020-05-20 stsp if (err)
4503 0d6c6ee3 2020-05-20 stsp return err;
4504 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4505 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4506 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4507 0d6c6ee3 2020-05-20 stsp }
4508 0d6c6ee3 2020-05-20 stsp
4509 848d6979 2019-08-12 stsp modestr = "@";
4510 0d6c6ee3 2020-05-20 stsp }
4511 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4512 848d6979 2019-08-12 stsp modestr = "/";
4513 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4514 848d6979 2019-08-12 stsp modestr = "*";
4515 848d6979 2019-08-12 stsp
4516 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4517 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4518 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4519 0d6c6ee3 2020-05-20 stsp
4520 0d6c6ee3 2020-05-20 stsp free(link_target);
4521 0d6c6ee3 2020-05-20 stsp return NULL;
4522 c1669e2e 2019-01-09 stsp }
4523 c1669e2e 2019-01-09 stsp
4524 5de5890b 2018-10-18 stsp static const struct got_error *
4525 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4526 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4527 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4528 5de5890b 2018-10-18 stsp {
4529 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4530 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4531 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4532 56e0773d 2019-11-28 stsp int nentries, i;
4533 5de5890b 2018-10-18 stsp
4534 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4535 5de5890b 2018-10-18 stsp if (err)
4536 5de5890b 2018-10-18 stsp goto done;
4537 5de5890b 2018-10-18 stsp
4538 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4539 5de5890b 2018-10-18 stsp if (err)
4540 5de5890b 2018-10-18 stsp goto done;
4541 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4542 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4543 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4544 5de5890b 2018-10-18 stsp char *id = NULL;
4545 84453469 2018-11-11 stsp
4546 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4547 84453469 2018-11-11 stsp break;
4548 84453469 2018-11-11 stsp
4549 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4550 5de5890b 2018-10-18 stsp if (show_ids) {
4551 5de5890b 2018-10-18 stsp char *id_str;
4552 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4553 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4554 5de5890b 2018-10-18 stsp if (err)
4555 5de5890b 2018-10-18 stsp goto done;
4556 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4557 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4558 5de5890b 2018-10-18 stsp free(id_str);
4559 5de5890b 2018-10-18 stsp goto done;
4560 5de5890b 2018-10-18 stsp }
4561 5de5890b 2018-10-18 stsp free(id_str);
4562 5de5890b 2018-10-18 stsp }
4563 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
4564 5de5890b 2018-10-18 stsp free(id);
4565 0d6c6ee3 2020-05-20 stsp if (err)
4566 0d6c6ee3 2020-05-20 stsp goto done;
4567 c1669e2e 2019-01-09 stsp
4568 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4569 c1669e2e 2019-01-09 stsp char *child_path;
4570 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4571 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4572 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4573 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4574 c1669e2e 2019-01-09 stsp goto done;
4575 c1669e2e 2019-01-09 stsp }
4576 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4577 c1669e2e 2019-01-09 stsp root_path, repo);
4578 c1669e2e 2019-01-09 stsp free(child_path);
4579 c1669e2e 2019-01-09 stsp if (err)
4580 c1669e2e 2019-01-09 stsp goto done;
4581 c1669e2e 2019-01-09 stsp }
4582 5de5890b 2018-10-18 stsp }
4583 5de5890b 2018-10-18 stsp done:
4584 5de5890b 2018-10-18 stsp if (tree)
4585 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4586 5de5890b 2018-10-18 stsp free(tree_id);
4587 5de5890b 2018-10-18 stsp return err;
4588 404c43c4 2018-06-21 stsp }
4589 404c43c4 2018-06-21 stsp
4590 5de5890b 2018-10-18 stsp static const struct got_error *
4591 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4592 5de5890b 2018-10-18 stsp {
4593 5de5890b 2018-10-18 stsp const struct got_error *error;
4594 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4595 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4596 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4597 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4598 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4599 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4600 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4601 5de5890b 2018-10-18 stsp int ch;
4602 5de5890b 2018-10-18 stsp
4603 5de5890b 2018-10-18 stsp #ifndef PROFILE
4604 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4605 0f8d269b 2019-01-04 stsp NULL) == -1)
4606 5de5890b 2018-10-18 stsp err(1, "pledge");
4607 5de5890b 2018-10-18 stsp #endif
4608 5de5890b 2018-10-18 stsp
4609 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4610 5de5890b 2018-10-18 stsp switch (ch) {
4611 5de5890b 2018-10-18 stsp case 'c':
4612 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4613 5de5890b 2018-10-18 stsp break;
4614 5de5890b 2018-10-18 stsp case 'r':
4615 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4616 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4617 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4618 9ba1d308 2019-10-21 stsp optarg);
4619 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4620 5de5890b 2018-10-18 stsp break;
4621 5de5890b 2018-10-18 stsp case 'i':
4622 5de5890b 2018-10-18 stsp show_ids = 1;
4623 5de5890b 2018-10-18 stsp break;
4624 c1669e2e 2019-01-09 stsp case 'R':
4625 c1669e2e 2019-01-09 stsp recurse = 1;
4626 c1669e2e 2019-01-09 stsp break;
4627 5de5890b 2018-10-18 stsp default:
4628 2deda0b9 2019-03-07 stsp usage_tree();
4629 5de5890b 2018-10-18 stsp /* NOTREACHED */
4630 5de5890b 2018-10-18 stsp }
4631 5de5890b 2018-10-18 stsp }
4632 5de5890b 2018-10-18 stsp
4633 5de5890b 2018-10-18 stsp argc -= optind;
4634 5de5890b 2018-10-18 stsp argv += optind;
4635 5de5890b 2018-10-18 stsp
4636 5de5890b 2018-10-18 stsp if (argc == 1)
4637 5de5890b 2018-10-18 stsp path = argv[0];
4638 5de5890b 2018-10-18 stsp else if (argc > 1)
4639 5de5890b 2018-10-18 stsp usage_tree();
4640 5de5890b 2018-10-18 stsp else
4641 7a2c19d6 2019-02-05 stsp path = NULL;
4642 7a2c19d6 2019-02-05 stsp
4643 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
4644 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
4645 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4646 9bf7a39b 2019-02-05 stsp goto done;
4647 9bf7a39b 2019-02-05 stsp }
4648 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4649 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4650 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4651 7a2c19d6 2019-02-05 stsp goto done;
4652 7a2c19d6 2019-02-05 stsp else
4653 7a2c19d6 2019-02-05 stsp error = NULL;
4654 7a2c19d6 2019-02-05 stsp if (worktree) {
4655 7a2c19d6 2019-02-05 stsp repo_path =
4656 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4657 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
4658 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4659 7a2c19d6 2019-02-05 stsp if (error)
4660 7a2c19d6 2019-02-05 stsp goto done;
4661 7a2c19d6 2019-02-05 stsp } else {
4662 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
4663 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
4664 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4665 7a2c19d6 2019-02-05 stsp goto done;
4666 7a2c19d6 2019-02-05 stsp }
4667 5de5890b 2018-10-18 stsp }
4668 5de5890b 2018-10-18 stsp }
4669 5de5890b 2018-10-18 stsp
4670 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4671 5de5890b 2018-10-18 stsp if (error != NULL)
4672 c02c541e 2019-03-29 stsp goto done;
4673 c02c541e 2019-03-29 stsp
4674 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4675 c02c541e 2019-03-29 stsp if (error)
4676 5de5890b 2018-10-18 stsp goto done;
4677 5de5890b 2018-10-18 stsp
4678 9bf7a39b 2019-02-05 stsp if (path == NULL) {
4679 9bf7a39b 2019-02-05 stsp if (worktree) {
4680 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4681 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4682 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
4683 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
4684 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
4685 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4686 9bf7a39b 2019-02-05 stsp goto done;
4687 9bf7a39b 2019-02-05 stsp }
4688 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4689 9bf7a39b 2019-02-05 stsp free(p);
4690 9bf7a39b 2019-02-05 stsp if (error)
4691 9bf7a39b 2019-02-05 stsp goto done;
4692 9bf7a39b 2019-02-05 stsp } else
4693 9bf7a39b 2019-02-05 stsp path = "/";
4694 9bf7a39b 2019-02-05 stsp }
4695 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
4696 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4697 9bf7a39b 2019-02-05 stsp if (error != NULL)
4698 9bf7a39b 2019-02-05 stsp goto done;
4699 9bf7a39b 2019-02-05 stsp }
4700 5de5890b 2018-10-18 stsp
4701 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
4702 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
4703 4e0a20a4 2020-03-23 tracey if (worktree)
4704 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
4705 4e0a20a4 2020-03-23 tracey else
4706 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
4707 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
4708 5de5890b 2018-10-18 stsp if (error != NULL)
4709 5de5890b 2018-10-18 stsp goto done;
4710 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4711 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
4712 5de5890b 2018-10-18 stsp if (error != NULL)
4713 5de5890b 2018-10-18 stsp goto done;
4714 5de5890b 2018-10-18 stsp } else {
4715 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4716 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4717 30837e32 2019-07-25 stsp if (error)
4718 5de5890b 2018-10-18 stsp goto done;
4719 5de5890b 2018-10-18 stsp }
4720 5de5890b 2018-10-18 stsp
4721 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4722 c1669e2e 2019-01-09 stsp in_repo_path, repo);
4723 5de5890b 2018-10-18 stsp done:
4724 5de5890b 2018-10-18 stsp free(in_repo_path);
4725 5de5890b 2018-10-18 stsp free(repo_path);
4726 5de5890b 2018-10-18 stsp free(cwd);
4727 5de5890b 2018-10-18 stsp free(commit_id);
4728 7a2c19d6 2019-02-05 stsp if (worktree)
4729 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
4730 5de5890b 2018-10-18 stsp if (repo) {
4731 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
4732 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
4733 5de5890b 2018-10-18 stsp if (error == NULL)
4734 5de5890b 2018-10-18 stsp error = repo_error;
4735 5de5890b 2018-10-18 stsp }
4736 5de5890b 2018-10-18 stsp return error;
4737 5de5890b 2018-10-18 stsp }
4738 5de5890b 2018-10-18 stsp
4739 6bad629b 2019-02-04 stsp __dead static void
4740 6bad629b 2019-02-04 stsp usage_status(void)
4741 6bad629b 2019-02-04 stsp {
4742 081470ac 2020-08-13 stsp fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
4743 081470ac 2020-08-13 stsp getprogname());
4744 6bad629b 2019-02-04 stsp exit(1);
4745 6bad629b 2019-02-04 stsp }
4746 5c860e29 2018-03-12 stsp
4747 b72f483a 2019-02-05 stsp static const struct got_error *
4748 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4749 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4750 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4751 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4752 6bad629b 2019-02-04 stsp {
4753 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4754 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
4755 081470ac 2020-08-13 stsp if (arg) {
4756 081470ac 2020-08-13 stsp char *status_codes = arg;
4757 081470ac 2020-08-13 stsp size_t ncodes = strlen(status_codes);
4758 081470ac 2020-08-13 stsp int i;
4759 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4760 081470ac 2020-08-13 stsp if (status == status_codes[i] ||
4761 081470ac 2020-08-13 stsp staged_status == status_codes[i])
4762 081470ac 2020-08-13 stsp break;
4763 081470ac 2020-08-13 stsp }
4764 081470ac 2020-08-13 stsp if (i == ncodes)
4765 081470ac 2020-08-13 stsp return NULL;
4766 081470ac 2020-08-13 stsp }
4767 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
4768 b72f483a 2019-02-05 stsp return NULL;
4769 6bad629b 2019-02-04 stsp }
4770 5c860e29 2018-03-12 stsp
4771 6bad629b 2019-02-04 stsp static const struct got_error *
4772 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
4773 6bad629b 2019-02-04 stsp {
4774 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
4775 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
4776 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
4777 081470ac 2020-08-13 stsp char *cwd = NULL, *status_codes = NULL;;
4778 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4779 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
4780 081470ac 2020-08-13 stsp int ch, i;
4781 5c860e29 2018-03-12 stsp
4782 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4783 72ea6654 2019-07-27 stsp
4784 081470ac 2020-08-13 stsp while ((ch = getopt(argc, argv, "s:")) != -1) {
4785 6bad629b 2019-02-04 stsp switch (ch) {
4786 081470ac 2020-08-13 stsp case 's':
4787 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
4788 081470ac 2020-08-13 stsp switch (optarg[i]) {
4789 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
4790 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
4791 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
4792 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
4793 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
4794 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
4795 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
4796 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
4797 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
4798 081470ac 2020-08-13 stsp break;
4799 081470ac 2020-08-13 stsp default:
4800 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
4801 081470ac 2020-08-13 stsp optarg[i]);
4802 081470ac 2020-08-13 stsp }
4803 081470ac 2020-08-13 stsp }
4804 081470ac 2020-08-13 stsp status_codes = optarg;
4805 081470ac 2020-08-13 stsp break;
4806 5c860e29 2018-03-12 stsp default:
4807 2deda0b9 2019-03-07 stsp usage_status();
4808 6bad629b 2019-02-04 stsp /* NOTREACHED */
4809 5c860e29 2018-03-12 stsp }
4810 5c860e29 2018-03-12 stsp }
4811 5c860e29 2018-03-12 stsp
4812 6bad629b 2019-02-04 stsp argc -= optind;
4813 6bad629b 2019-02-04 stsp argv += optind;
4814 5c860e29 2018-03-12 stsp
4815 6bad629b 2019-02-04 stsp #ifndef PROFILE
4816 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4817 6bad629b 2019-02-04 stsp NULL) == -1)
4818 6bad629b 2019-02-04 stsp err(1, "pledge");
4819 f42b1b34 2018-03-12 stsp #endif
4820 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
4821 927df6b7 2019-02-10 stsp if (cwd == NULL) {
4822 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4823 927df6b7 2019-02-10 stsp goto done;
4824 927df6b7 2019-02-10 stsp }
4825 927df6b7 2019-02-10 stsp
4826 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
4827 fa51e947 2020-03-27 stsp if (error) {
4828 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4829 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
4830 a5edda0a 2019-07-27 stsp goto done;
4831 fa51e947 2020-03-27 stsp }
4832 6bad629b 2019-02-04 stsp
4833 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4834 c9956ddf 2019-09-08 stsp NULL);
4835 6bad629b 2019-02-04 stsp if (error != NULL)
4836 6bad629b 2019-02-04 stsp goto done;
4837 6bad629b 2019-02-04 stsp
4838 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4839 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4840 087fb88c 2019-08-04 stsp if (error)
4841 087fb88c 2019-08-04 stsp goto done;
4842 087fb88c 2019-08-04 stsp
4843 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4844 6bad629b 2019-02-04 stsp if (error)
4845 6bad629b 2019-02-04 stsp goto done;
4846 6bad629b 2019-02-04 stsp
4847 081470ac 2020-08-13 stsp error = got_worktree_status(worktree, &paths, repo, print_status,
4848 081470ac 2020-08-13 stsp status_codes, check_cancelled, NULL);
4849 6bad629b 2019-02-04 stsp done:
4850 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
4851 a5edda0a 2019-07-27 stsp free((char *)pe->path);
4852 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4853 927df6b7 2019-02-10 stsp free(cwd);
4854 d0eebce4 2019-03-11 stsp return error;
4855 d0eebce4 2019-03-11 stsp }
4856 d0eebce4 2019-03-11 stsp
4857 d0eebce4 2019-03-11 stsp __dead static void
4858 d0eebce4 2019-03-11 stsp usage_ref(void)
4859 d0eebce4 2019-03-11 stsp {
4860 d0eebce4 2019-03-11 stsp fprintf(stderr,
4861 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4862 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
4863 d0eebce4 2019-03-11 stsp getprogname());
4864 d0eebce4 2019-03-11 stsp exit(1);
4865 d0eebce4 2019-03-11 stsp }
4866 d0eebce4 2019-03-11 stsp
4867 d0eebce4 2019-03-11 stsp static const struct got_error *
4868 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
4869 d0eebce4 2019-03-11 stsp {
4870 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
4871 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
4872 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
4873 d0eebce4 2019-03-11 stsp
4874 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
4875 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4876 d0eebce4 2019-03-11 stsp if (err)
4877 d0eebce4 2019-03-11 stsp return err;
4878 d0eebce4 2019-03-11 stsp
4879 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4880 d0eebce4 2019-03-11 stsp char *refstr;
4881 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
4882 d0eebce4 2019-03-11 stsp if (refstr == NULL)
4883 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
4884 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4885 d0eebce4 2019-03-11 stsp free(refstr);
4886 d0eebce4 2019-03-11 stsp }
4887 d0eebce4 2019-03-11 stsp
4888 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4889 d0eebce4 2019-03-11 stsp return NULL;
4890 d0eebce4 2019-03-11 stsp }
4891 d0eebce4 2019-03-11 stsp
4892 d0eebce4 2019-03-11 stsp static const struct got_error *
4893 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
4894 d0eebce4 2019-03-11 stsp {
4895 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4896 d0eebce4 2019-03-11 stsp struct got_reference *ref;
4897 d0eebce4 2019-03-11 stsp
4898 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
4899 d0eebce4 2019-03-11 stsp if (err)
4900 d0eebce4 2019-03-11 stsp return err;
4901 d0eebce4 2019-03-11 stsp
4902 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
4903 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4904 d0eebce4 2019-03-11 stsp return err;
4905 d0eebce4 2019-03-11 stsp }
4906 d0eebce4 2019-03-11 stsp
4907 d0eebce4 2019-03-11 stsp static const struct got_error *
4908 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
4909 d0eebce4 2019-03-11 stsp {
4910 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4911 d0eebce4 2019-03-11 stsp struct got_object_id *id;
4912 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
4913 d1644381 2019-07-14 stsp
4914 d1644381 2019-07-14 stsp /*
4915 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4916 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
4917 d1644381 2019-07-14 stsp * an unintended typo.
4918 d1644381 2019-07-14 stsp */
4919 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4920 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4921 d0eebce4 2019-03-11 stsp
4922 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4923 dd88155e 2019-06-29 stsp repo);
4924 d83d9d5c 2019-05-13 stsp if (err) {
4925 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
4926 d0eebce4 2019-03-11 stsp
4927 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4928 d83d9d5c 2019-05-13 stsp return err;
4929 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
4930 d83d9d5c 2019-05-13 stsp if (err)
4931 d83d9d5c 2019-05-13 stsp return err;
4932 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
4933 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
4934 d83d9d5c 2019-05-13 stsp if (err)
4935 d83d9d5c 2019-05-13 stsp return err;
4936 d83d9d5c 2019-05-13 stsp }
4937 d83d9d5c 2019-05-13 stsp
4938 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
4939 d0eebce4 2019-03-11 stsp if (err)
4940 d0eebce4 2019-03-11 stsp goto done;
4941 d0eebce4 2019-03-11 stsp
4942 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
4943 d0eebce4 2019-03-11 stsp done:
4944 d0eebce4 2019-03-11 stsp if (ref)
4945 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4946 d0eebce4 2019-03-11 stsp free(id);
4947 d1c1ae5f 2019-08-12 stsp return err;
4948 d1c1ae5f 2019-08-12 stsp }
4949 d1c1ae5f 2019-08-12 stsp
4950 d1c1ae5f 2019-08-12 stsp static const struct got_error *
4951 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
4952 d1c1ae5f 2019-08-12 stsp {
4953 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
4954 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
4955 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
4956 d1c1ae5f 2019-08-12 stsp
4957 d1c1ae5f 2019-08-12 stsp /*
4958 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4959 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
4960 d1c1ae5f 2019-08-12 stsp * an unintended typo.
4961 d1c1ae5f 2019-08-12 stsp */
4962 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4963 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4964 d1c1ae5f 2019-08-12 stsp
4965 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
4966 d1c1ae5f 2019-08-12 stsp if (err)
4967 d1c1ae5f 2019-08-12 stsp return err;
4968 d1c1ae5f 2019-08-12 stsp
4969 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
4970 d1c1ae5f 2019-08-12 stsp if (err)
4971 d1c1ae5f 2019-08-12 stsp goto done;
4972 d1c1ae5f 2019-08-12 stsp
4973 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
4974 d1c1ae5f 2019-08-12 stsp done:
4975 d1c1ae5f 2019-08-12 stsp if (target_ref)
4976 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
4977 d1c1ae5f 2019-08-12 stsp if (ref)
4978 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
4979 d0eebce4 2019-03-11 stsp return err;
4980 d0eebce4 2019-03-11 stsp }
4981 d0eebce4 2019-03-11 stsp
4982 d0eebce4 2019-03-11 stsp static const struct got_error *
4983 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
4984 d0eebce4 2019-03-11 stsp {
4985 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
4986 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
4987 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
4988 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
4989 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
4990 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
4991 b2070a3f 2020-03-22 stsp char *refname = NULL;
4992 d0eebce4 2019-03-11 stsp
4993 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4994 d0eebce4 2019-03-11 stsp switch (ch) {
4995 e31abbf2 2020-03-22 stsp case 'c':
4996 e31abbf2 2020-03-22 stsp obj_arg = optarg;
4997 e31abbf2 2020-03-22 stsp break;
4998 d0eebce4 2019-03-11 stsp case 'd':
4999 e31abbf2 2020-03-22 stsp do_delete = 1;
5000 d0eebce4 2019-03-11 stsp break;
5001 d0eebce4 2019-03-11 stsp case 'r':
5002 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5003 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5004 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5005 9ba1d308 2019-10-21 stsp optarg);
5006 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5007 d0eebce4 2019-03-11 stsp break;
5008 d0eebce4 2019-03-11 stsp case 'l':
5009 d0eebce4 2019-03-11 stsp do_list = 1;
5010 d1c1ae5f 2019-08-12 stsp break;
5011 d1c1ae5f 2019-08-12 stsp case 's':
5012 e31abbf2 2020-03-22 stsp symref_target = optarg;
5013 d0eebce4 2019-03-11 stsp break;
5014 d0eebce4 2019-03-11 stsp default:
5015 d0eebce4 2019-03-11 stsp usage_ref();
5016 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5017 d0eebce4 2019-03-11 stsp }
5018 d0eebce4 2019-03-11 stsp }
5019 d0eebce4 2019-03-11 stsp
5020 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5021 907f15e2 2020-03-22 stsp errx(1, "-c and -l options are mutually exclusive");
5022 e31abbf2 2020-03-22 stsp if (obj_arg && do_delete)
5023 907f15e2 2020-03-22 stsp errx(1, "-c and -d options are mutually exclusive");
5024 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5025 907f15e2 2020-03-22 stsp errx(1, "-c and -s options are mutually exclusive");
5026 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5027 907f15e2 2020-03-22 stsp errx(1, "-s and -d options are mutually exclusive");
5028 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5029 907f15e2 2020-03-22 stsp errx(1, "-s and -l options are mutually exclusive");
5030 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5031 907f15e2 2020-03-22 stsp errx(1, "-d and -l options are mutually exclusive");
5032 d0eebce4 2019-03-11 stsp
5033 d0eebce4 2019-03-11 stsp argc -= optind;
5034 d0eebce4 2019-03-11 stsp argv += optind;
5035 d0eebce4 2019-03-11 stsp
5036 e31abbf2 2020-03-22 stsp if (do_list) {
5037 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5038 d0eebce4 2019-03-11 stsp usage_ref();
5039 b2070a3f 2020-03-22 stsp if (argc == 1) {
5040 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5041 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5042 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5043 b2070a3f 2020-03-22 stsp goto done;
5044 b2070a3f 2020-03-22 stsp }
5045 b2070a3f 2020-03-22 stsp }
5046 e31abbf2 2020-03-22 stsp } else {
5047 e31abbf2 2020-03-22 stsp if (argc != 1)
5048 e31abbf2 2020-03-22 stsp usage_ref();
5049 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5050 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5051 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5052 b2070a3f 2020-03-22 stsp goto done;
5053 b2070a3f 2020-03-22 stsp }
5054 e31abbf2 2020-03-22 stsp }
5055 b2070a3f 2020-03-22 stsp
5056 b2070a3f 2020-03-22 stsp if (refname)
5057 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5058 d0eebce4 2019-03-11 stsp
5059 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5060 e0b57350 2019-03-12 stsp if (do_list) {
5061 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5062 e0b57350 2019-03-12 stsp NULL) == -1)
5063 e0b57350 2019-03-12 stsp err(1, "pledge");
5064 e0b57350 2019-03-12 stsp } else {
5065 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5066 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5067 e0b57350 2019-03-12 stsp err(1, "pledge");
5068 e0b57350 2019-03-12 stsp }
5069 d0eebce4 2019-03-11 stsp #endif
5070 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5071 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5072 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5073 d0eebce4 2019-03-11 stsp goto done;
5074 d0eebce4 2019-03-11 stsp }
5075 d0eebce4 2019-03-11 stsp
5076 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5077 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5078 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5079 d0eebce4 2019-03-11 stsp goto done;
5080 d0eebce4 2019-03-11 stsp else
5081 d0eebce4 2019-03-11 stsp error = NULL;
5082 d0eebce4 2019-03-11 stsp if (worktree) {
5083 d0eebce4 2019-03-11 stsp repo_path =
5084 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5085 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5086 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5087 d0eebce4 2019-03-11 stsp if (error)
5088 d0eebce4 2019-03-11 stsp goto done;
5089 d0eebce4 2019-03-11 stsp } else {
5090 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5091 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5092 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5093 d0eebce4 2019-03-11 stsp goto done;
5094 d0eebce4 2019-03-11 stsp }
5095 d0eebce4 2019-03-11 stsp }
5096 d0eebce4 2019-03-11 stsp }
5097 d0eebce4 2019-03-11 stsp
5098 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5099 d0eebce4 2019-03-11 stsp if (error != NULL)
5100 d0eebce4 2019-03-11 stsp goto done;
5101 d0eebce4 2019-03-11 stsp
5102 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5103 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5104 c02c541e 2019-03-29 stsp if (error)
5105 c02c541e 2019-03-29 stsp goto done;
5106 c02c541e 2019-03-29 stsp
5107 d0eebce4 2019-03-11 stsp if (do_list)
5108 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5109 e31abbf2 2020-03-22 stsp else if (do_delete)
5110 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
5111 e31abbf2 2020-03-22 stsp else if (symref_target)
5112 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5113 e31abbf2 2020-03-22 stsp else {
5114 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5115 e31abbf2 2020-03-22 stsp usage_ref();
5116 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5117 e31abbf2 2020-03-22 stsp }
5118 4e759de4 2019-06-26 stsp done:
5119 b2070a3f 2020-03-22 stsp free(refname);
5120 4e759de4 2019-06-26 stsp if (repo)
5121 4e759de4 2019-06-26 stsp got_repo_close(repo);
5122 4e759de4 2019-06-26 stsp if (worktree)
5123 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5124 4e759de4 2019-06-26 stsp free(cwd);
5125 4e759de4 2019-06-26 stsp free(repo_path);
5126 4e759de4 2019-06-26 stsp return error;
5127 4e759de4 2019-06-26 stsp }
5128 4e759de4 2019-06-26 stsp
5129 4e759de4 2019-06-26 stsp __dead static void
5130 4e759de4 2019-06-26 stsp usage_branch(void)
5131 4e759de4 2019-06-26 stsp {
5132 4e759de4 2019-06-26 stsp fprintf(stderr,
5133 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5134 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5135 4e759de4 2019-06-26 stsp exit(1);
5136 4e759de4 2019-06-26 stsp }
5137 4e759de4 2019-06-26 stsp
5138 4e759de4 2019-06-26 stsp static const struct got_error *
5139 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5140 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5141 4e99b47e 2019-10-04 stsp {
5142 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5143 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5144 4e99b47e 2019-10-04 stsp char *refstr;
5145 4e99b47e 2019-10-04 stsp
5146 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5147 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5148 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5149 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5150 4e99b47e 2019-10-04 stsp
5151 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5152 4e99b47e 2019-10-04 stsp if (err)
5153 4e99b47e 2019-10-04 stsp return err;
5154 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5155 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5156 4e99b47e 2019-10-04 stsp marker = "* ";
5157 4e99b47e 2019-10-04 stsp else
5158 4e99b47e 2019-10-04 stsp marker = "~ ";
5159 4e99b47e 2019-10-04 stsp free(id);
5160 4e99b47e 2019-10-04 stsp }
5161 4e99b47e 2019-10-04 stsp
5162 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5163 4e99b47e 2019-10-04 stsp refname += 11;
5164 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5165 4e99b47e 2019-10-04 stsp refname += 18;
5166 4e99b47e 2019-10-04 stsp
5167 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5168 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5169 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5170 4e99b47e 2019-10-04 stsp
5171 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5172 4e99b47e 2019-10-04 stsp free(refstr);
5173 4e99b47e 2019-10-04 stsp return NULL;
5174 4e99b47e 2019-10-04 stsp }
5175 4e99b47e 2019-10-04 stsp
5176 4e99b47e 2019-10-04 stsp static const struct got_error *
5177 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5178 ad89fa31 2019-10-04 stsp {
5179 ad89fa31 2019-10-04 stsp const char *refname;
5180 ad89fa31 2019-10-04 stsp
5181 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5182 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5183 ad89fa31 2019-10-04 stsp
5184 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5185 ad89fa31 2019-10-04 stsp
5186 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5187 ad89fa31 2019-10-04 stsp refname += 11;
5188 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5189 ad89fa31 2019-10-04 stsp refname += 18;
5190 ad89fa31 2019-10-04 stsp
5191 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5192 ad89fa31 2019-10-04 stsp
5193 ad89fa31 2019-10-04 stsp return NULL;
5194 ad89fa31 2019-10-04 stsp }
5195 ad89fa31 2019-10-04 stsp
5196 ad89fa31 2019-10-04 stsp static const struct got_error *
5197 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5198 4e759de4 2019-06-26 stsp {
5199 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5200 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5201 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5202 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5203 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5204 4e759de4 2019-06-26 stsp
5205 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
5206 ba882ee3 2019-07-11 stsp
5207 4e99b47e 2019-10-04 stsp if (worktree) {
5208 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5209 4e99b47e 2019-10-04 stsp worktree);
5210 4e99b47e 2019-10-04 stsp if (err)
5211 4e99b47e 2019-10-04 stsp return err;
5212 4e99b47e 2019-10-04 stsp
5213 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5214 4e99b47e 2019-10-04 stsp worktree);
5215 4e99b47e 2019-10-04 stsp if (err)
5216 4e99b47e 2019-10-04 stsp return err;
5217 4e99b47e 2019-10-04 stsp
5218 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5219 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5220 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5221 4e99b47e 2019-10-04 stsp if (err)
5222 4e99b47e 2019-10-04 stsp return err;
5223 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5224 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5225 4e99b47e 2019-10-04 stsp }
5226 4e99b47e 2019-10-04 stsp }
5227 4e99b47e 2019-10-04 stsp
5228 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5229 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5230 4e759de4 2019-06-26 stsp if (err)
5231 4e759de4 2019-06-26 stsp return err;
5232 4e759de4 2019-06-26 stsp
5233 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
5234 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5235 4e759de4 2019-06-26 stsp
5236 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5237 4e759de4 2019-06-26 stsp return NULL;
5238 4e759de4 2019-06-26 stsp }
5239 4e759de4 2019-06-26 stsp
5240 4e759de4 2019-06-26 stsp static const struct got_error *
5241 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5242 45cd4e47 2019-08-25 stsp const char *branch_name)
5243 4e759de4 2019-06-26 stsp {
5244 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5245 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5246 4e759de4 2019-06-26 stsp char *refname;
5247 4e759de4 2019-06-26 stsp
5248 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5249 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5250 4e759de4 2019-06-26 stsp
5251 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5252 4e759de4 2019-06-26 stsp if (err)
5253 4e759de4 2019-06-26 stsp goto done;
5254 4e759de4 2019-06-26 stsp
5255 45cd4e47 2019-08-25 stsp if (worktree &&
5256 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5257 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5258 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5259 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5260 45cd4e47 2019-08-25 stsp goto done;
5261 45cd4e47 2019-08-25 stsp }
5262 45cd4e47 2019-08-25 stsp
5263 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
5264 4e759de4 2019-06-26 stsp done:
5265 45cd4e47 2019-08-25 stsp if (ref)
5266 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5267 4e759de4 2019-06-26 stsp free(refname);
5268 4e759de4 2019-06-26 stsp return err;
5269 4e759de4 2019-06-26 stsp }
5270 4e759de4 2019-06-26 stsp
5271 4e759de4 2019-06-26 stsp static const struct got_error *
5272 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5273 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5274 4e759de4 2019-06-26 stsp {
5275 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5276 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5277 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5278 d3f84d51 2019-07-11 stsp
5279 d3f84d51 2019-07-11 stsp /*
5280 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5281 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5282 d3f84d51 2019-07-11 stsp * an unintended typo.
5283 d3f84d51 2019-07-11 stsp */
5284 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5285 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5286 4e759de4 2019-06-26 stsp
5287 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5288 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
5289 4e759de4 2019-06-26 stsp goto done;
5290 4e759de4 2019-06-26 stsp }
5291 4e759de4 2019-06-26 stsp
5292 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5293 4e759de4 2019-06-26 stsp if (err == NULL) {
5294 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5295 4e759de4 2019-06-26 stsp goto done;
5296 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5297 4e759de4 2019-06-26 stsp goto done;
5298 4e759de4 2019-06-26 stsp
5299 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5300 4e759de4 2019-06-26 stsp if (err)
5301 4e759de4 2019-06-26 stsp goto done;
5302 4e759de4 2019-06-26 stsp
5303 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5304 d0eebce4 2019-03-11 stsp done:
5305 4e759de4 2019-06-26 stsp if (ref)
5306 4e759de4 2019-06-26 stsp got_ref_close(ref);
5307 4e759de4 2019-06-26 stsp free(base_refname);
5308 4e759de4 2019-06-26 stsp free(refname);
5309 4e759de4 2019-06-26 stsp return err;
5310 4e759de4 2019-06-26 stsp }
5311 4e759de4 2019-06-26 stsp
5312 4e759de4 2019-06-26 stsp static const struct got_error *
5313 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5314 4e759de4 2019-06-26 stsp {
5315 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5316 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5317 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5318 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5319 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5320 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5321 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5322 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5323 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5324 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5325 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5326 4e759de4 2019-06-26 stsp
5327 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5328 da76fce2 2020-02-24 stsp
5329 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5330 4e759de4 2019-06-26 stsp switch (ch) {
5331 a74f7e83 2019-11-10 stsp case 'c':
5332 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5333 a74f7e83 2019-11-10 stsp break;
5334 4e759de4 2019-06-26 stsp case 'd':
5335 4e759de4 2019-06-26 stsp delref = optarg;
5336 4e759de4 2019-06-26 stsp break;
5337 4e759de4 2019-06-26 stsp case 'r':
5338 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5339 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5340 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5341 9ba1d308 2019-10-21 stsp optarg);
5342 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5343 4e759de4 2019-06-26 stsp break;
5344 4e759de4 2019-06-26 stsp case 'l':
5345 4e759de4 2019-06-26 stsp do_list = 1;
5346 da76fce2 2020-02-24 stsp break;
5347 da76fce2 2020-02-24 stsp case 'n':
5348 da76fce2 2020-02-24 stsp do_update = 0;
5349 4e759de4 2019-06-26 stsp break;
5350 4e759de4 2019-06-26 stsp default:
5351 4e759de4 2019-06-26 stsp usage_branch();
5352 4e759de4 2019-06-26 stsp /* NOTREACHED */
5353 4e759de4 2019-06-26 stsp }
5354 4e759de4 2019-06-26 stsp }
5355 4e759de4 2019-06-26 stsp
5356 4e759de4 2019-06-26 stsp if (do_list && delref)
5357 907f15e2 2020-03-22 stsp errx(1, "-l and -d options are mutually exclusive");
5358 4e759de4 2019-06-26 stsp
5359 4e759de4 2019-06-26 stsp argc -= optind;
5360 4e759de4 2019-06-26 stsp argv += optind;
5361 ad89fa31 2019-10-04 stsp
5362 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5363 ad89fa31 2019-10-04 stsp do_show = 1;
5364 4e759de4 2019-06-26 stsp
5365 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5366 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5367 a74f7e83 2019-11-10 stsp
5368 4e759de4 2019-06-26 stsp if (do_list || delref) {
5369 4e759de4 2019-06-26 stsp if (argc > 0)
5370 4e759de4 2019-06-26 stsp usage_branch();
5371 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5372 4e759de4 2019-06-26 stsp usage_branch();
5373 4e759de4 2019-06-26 stsp
5374 4e759de4 2019-06-26 stsp #ifndef PROFILE
5375 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5376 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5377 4e759de4 2019-06-26 stsp NULL) == -1)
5378 4e759de4 2019-06-26 stsp err(1, "pledge");
5379 4e759de4 2019-06-26 stsp } else {
5380 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5381 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5382 4e759de4 2019-06-26 stsp err(1, "pledge");
5383 4e759de4 2019-06-26 stsp }
5384 4e759de4 2019-06-26 stsp #endif
5385 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5386 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5387 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5388 4e759de4 2019-06-26 stsp goto done;
5389 4e759de4 2019-06-26 stsp }
5390 4e759de4 2019-06-26 stsp
5391 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5392 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5393 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5394 4e759de4 2019-06-26 stsp goto done;
5395 4e759de4 2019-06-26 stsp else
5396 4e759de4 2019-06-26 stsp error = NULL;
5397 4e759de4 2019-06-26 stsp if (worktree) {
5398 4e759de4 2019-06-26 stsp repo_path =
5399 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5400 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5401 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5402 4e759de4 2019-06-26 stsp if (error)
5403 4e759de4 2019-06-26 stsp goto done;
5404 4e759de4 2019-06-26 stsp } else {
5405 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5406 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5407 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5408 4e759de4 2019-06-26 stsp goto done;
5409 4e759de4 2019-06-26 stsp }
5410 4e759de4 2019-06-26 stsp }
5411 4e759de4 2019-06-26 stsp }
5412 4e759de4 2019-06-26 stsp
5413 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5414 4e759de4 2019-06-26 stsp if (error != NULL)
5415 4e759de4 2019-06-26 stsp goto done;
5416 4e759de4 2019-06-26 stsp
5417 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5418 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5419 4e759de4 2019-06-26 stsp if (error)
5420 4e759de4 2019-06-26 stsp goto done;
5421 4e759de4 2019-06-26 stsp
5422 ad89fa31 2019-10-04 stsp if (do_show)
5423 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5424 ad89fa31 2019-10-04 stsp else if (do_list)
5425 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5426 4e759de4 2019-06-26 stsp else if (delref)
5427 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5428 4e759de4 2019-06-26 stsp else {
5429 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5430 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5431 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5432 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5433 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5434 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5435 a74f7e83 2019-11-10 stsp if (error)
5436 a74f7e83 2019-11-10 stsp goto done;
5437 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5438 da76fce2 2020-02-24 stsp if (error)
5439 da76fce2 2020-02-24 stsp goto done;
5440 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5441 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5442 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5443 da76fce2 2020-02-24 stsp
5444 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5445 da76fce2 2020-02-24 stsp if (error)
5446 da76fce2 2020-02-24 stsp goto done;
5447 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5448 da76fce2 2020-02-24 stsp worktree);
5449 da76fce2 2020-02-24 stsp if (error)
5450 da76fce2 2020-02-24 stsp goto done;
5451 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5452 da76fce2 2020-02-24 stsp == -1) {
5453 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5454 da76fce2 2020-02-24 stsp goto done;
5455 da76fce2 2020-02-24 stsp }
5456 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5457 da76fce2 2020-02-24 stsp free(branch_refname);
5458 da76fce2 2020-02-24 stsp if (error)
5459 da76fce2 2020-02-24 stsp goto done;
5460 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5461 da76fce2 2020-02-24 stsp repo);
5462 da76fce2 2020-02-24 stsp if (error)
5463 da76fce2 2020-02-24 stsp goto done;
5464 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5465 da76fce2 2020-02-24 stsp commit_id);
5466 da76fce2 2020-02-24 stsp if (error)
5467 da76fce2 2020-02-24 stsp goto done;
5468 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5469 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5470 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5471 9627c110 2020-04-18 stsp NULL);
5472 da76fce2 2020-02-24 stsp if (error)
5473 da76fce2 2020-02-24 stsp goto done;
5474 9627c110 2020-04-18 stsp if (upa.did_something)
5475 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5476 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
5477 da76fce2 2020-02-24 stsp }
5478 4e759de4 2019-06-26 stsp }
5479 4e759de4 2019-06-26 stsp done:
5480 da76fce2 2020-02-24 stsp if (ref)
5481 da76fce2 2020-02-24 stsp got_ref_close(ref);
5482 d0eebce4 2019-03-11 stsp if (repo)
5483 d0eebce4 2019-03-11 stsp got_repo_close(repo);
5484 d0eebce4 2019-03-11 stsp if (worktree)
5485 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
5486 d0eebce4 2019-03-11 stsp free(cwd);
5487 d0eebce4 2019-03-11 stsp free(repo_path);
5488 da76fce2 2020-02-24 stsp free(commit_id);
5489 da76fce2 2020-02-24 stsp free(commit_id_str);
5490 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
5491 da76fce2 2020-02-24 stsp free((char *)pe->path);
5492 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
5493 d00136be 2019-03-26 stsp return error;
5494 d00136be 2019-03-26 stsp }
5495 d00136be 2019-03-26 stsp
5496 8e7bd50a 2019-08-22 stsp
5497 d00136be 2019-03-26 stsp __dead static void
5498 8e7bd50a 2019-08-22 stsp usage_tag(void)
5499 8e7bd50a 2019-08-22 stsp {
5500 8e7bd50a 2019-08-22 stsp fprintf(stderr,
5501 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5502 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
5503 8e7bd50a 2019-08-22 stsp exit(1);
5504 b8bad2ba 2019-08-23 stsp }
5505 b8bad2ba 2019-08-23 stsp
5506 b8bad2ba 2019-08-23 stsp #if 0
5507 b8bad2ba 2019-08-23 stsp static const struct got_error *
5508 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5509 b8bad2ba 2019-08-23 stsp {
5510 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
5511 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
5512 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
5513 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
5514 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
5515 b8bad2ba 2019-08-23 stsp
5516 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5517 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
5518 b8bad2ba 2019-08-23 stsp if (se == NULL) {
5519 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5520 b8bad2ba 2019-08-23 stsp if (err)
5521 b8bad2ba 2019-08-23 stsp return err;
5522 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5523 b8bad2ba 2019-08-23 stsp continue;
5524 b8bad2ba 2019-08-23 stsp } else {
5525 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5526 b8bad2ba 2019-08-23 stsp if (err)
5527 b8bad2ba 2019-08-23 stsp break;
5528 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5529 b8bad2ba 2019-08-23 stsp free(re_id);
5530 b8bad2ba 2019-08-23 stsp if (err)
5531 b8bad2ba 2019-08-23 stsp break;
5532 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5533 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
5534 b8bad2ba 2019-08-23 stsp }
5535 b8bad2ba 2019-08-23 stsp
5536 b8bad2ba 2019-08-23 stsp while (se) {
5537 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5538 b8bad2ba 2019-08-23 stsp if (err)
5539 b8bad2ba 2019-08-23 stsp break;
5540 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5541 b8bad2ba 2019-08-23 stsp free(se_id);
5542 b8bad2ba 2019-08-23 stsp if (err)
5543 b8bad2ba 2019-08-23 stsp break;
5544 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5545 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
5546 b8bad2ba 2019-08-23 stsp
5547 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
5548 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5549 b8bad2ba 2019-08-23 stsp if (err)
5550 b8bad2ba 2019-08-23 stsp return err;
5551 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5552 b8bad2ba 2019-08-23 stsp break;
5553 b8bad2ba 2019-08-23 stsp }
5554 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
5555 b8bad2ba 2019-08-23 stsp continue;
5556 b8bad2ba 2019-08-23 stsp }
5557 b8bad2ba 2019-08-23 stsp }
5558 b8bad2ba 2019-08-23 stsp done:
5559 b8bad2ba 2019-08-23 stsp return err;
5560 8e7bd50a 2019-08-22 stsp }
5561 b8bad2ba 2019-08-23 stsp #endif
5562 b8bad2ba 2019-08-23 stsp
5563 b8bad2ba 2019-08-23 stsp static const struct got_error *
5564 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5565 8e7bd50a 2019-08-22 stsp {
5566 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5567 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5568 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
5569 8e7bd50a 2019-08-22 stsp
5570 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
5571 8e7bd50a 2019-08-22 stsp
5572 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5573 8e7bd50a 2019-08-22 stsp if (err)
5574 8e7bd50a 2019-08-22 stsp return err;
5575 8e7bd50a 2019-08-22 stsp
5576 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5577 8e7bd50a 2019-08-22 stsp const char *refname;
5578 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5579 8e7bd50a 2019-08-22 stsp char datebuf[26];
5580 d4efa91b 2020-01-14 stsp const char *tagger;
5581 8e7bd50a 2019-08-22 stsp time_t tagger_time;
5582 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
5583 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
5584 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
5585 8e7bd50a 2019-08-22 stsp
5586 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
5587 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5588 8e7bd50a 2019-08-22 stsp continue;
5589 8e7bd50a 2019-08-22 stsp refname += 10;
5590 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
5591 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
5592 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
5593 8e7bd50a 2019-08-22 stsp break;
5594 8e7bd50a 2019-08-22 stsp }
5595 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5596 8e7bd50a 2019-08-22 stsp free(refstr);
5597 8e7bd50a 2019-08-22 stsp
5598 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
5599 8e7bd50a 2019-08-22 stsp if (err)
5600 8e7bd50a 2019-08-22 stsp break;
5601 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
5602 d4efa91b 2020-01-14 stsp if (err) {
5603 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5604 d4efa91b 2020-01-14 stsp free(id);
5605 d4efa91b 2020-01-14 stsp break;
5606 d4efa91b 2020-01-14 stsp }
5607 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
5608 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
5609 d4efa91b 2020-01-14 stsp if (err) {
5610 d4efa91b 2020-01-14 stsp free(id);
5611 d4efa91b 2020-01-14 stsp break;
5612 d4efa91b 2020-01-14 stsp }
5613 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
5614 d4efa91b 2020-01-14 stsp tagger_time =
5615 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
5616 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
5617 d4efa91b 2020-01-14 stsp free(id);
5618 d4efa91b 2020-01-14 stsp if (err)
5619 d4efa91b 2020-01-14 stsp break;
5620 d4efa91b 2020-01-14 stsp } else {
5621 d4efa91b 2020-01-14 stsp free(id);
5622 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
5623 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5624 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
5625 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
5626 d4efa91b 2020-01-14 stsp if (err)
5627 d4efa91b 2020-01-14 stsp break;
5628 d4efa91b 2020-01-14 stsp }
5629 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
5630 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
5631 2417344c 2019-08-23 stsp if (datestr)
5632 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
5633 d4efa91b 2020-01-14 stsp if (commit)
5634 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5635 d4efa91b 2020-01-14 stsp else {
5636 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
5637 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
5638 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5639 d4efa91b 2020-01-14 stsp id_str);
5640 d4efa91b 2020-01-14 stsp break;
5641 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
5642 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5643 d4efa91b 2020-01-14 stsp id_str);
5644 d4efa91b 2020-01-14 stsp break;
5645 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
5646 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5647 d4efa91b 2020-01-14 stsp id_str);
5648 d4efa91b 2020-01-14 stsp break;
5649 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
5650 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5651 d4efa91b 2020-01-14 stsp id_str);
5652 d4efa91b 2020-01-14 stsp break;
5653 d4efa91b 2020-01-14 stsp default:
5654 d4efa91b 2020-01-14 stsp break;
5655 d4efa91b 2020-01-14 stsp }
5656 8e7bd50a 2019-08-22 stsp }
5657 8e7bd50a 2019-08-22 stsp free(id_str);
5658 d4efa91b 2020-01-14 stsp if (commit) {
5659 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5660 d4efa91b 2020-01-14 stsp if (err)
5661 d4efa91b 2020-01-14 stsp break;
5662 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
5663 d4efa91b 2020-01-14 stsp } else {
5664 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5665 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
5666 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
5667 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
5668 d4efa91b 2020-01-14 stsp break;
5669 d4efa91b 2020-01-14 stsp }
5670 8e7bd50a 2019-08-22 stsp }
5671 8e7bd50a 2019-08-22 stsp
5672 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
5673 8e7bd50a 2019-08-22 stsp do {
5674 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
5675 8e7bd50a 2019-08-22 stsp if (line)
5676 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
5677 8e7bd50a 2019-08-22 stsp } while (line);
5678 8e7bd50a 2019-08-22 stsp free(tagmsg0);
5679 8e7bd50a 2019-08-22 stsp }
5680 8e7bd50a 2019-08-22 stsp
5681 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
5682 8e7bd50a 2019-08-22 stsp return NULL;
5683 8e7bd50a 2019-08-22 stsp }
5684 8e7bd50a 2019-08-22 stsp
5685 8e7bd50a 2019-08-22 stsp static const struct got_error *
5686 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5687 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
5688 8e7bd50a 2019-08-22 stsp {
5689 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5690 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
5691 f372d5cd 2019-10-21 stsp char *editor = NULL;
5692 1601cb9f 2020-09-11 naddy int initial_content_len;
5693 8e7bd50a 2019-08-22 stsp int fd = -1;
5694 8e7bd50a 2019-08-22 stsp
5695 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5696 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5697 8e7bd50a 2019-08-22 stsp goto done;
5698 8e7bd50a 2019-08-22 stsp }
5699 8e7bd50a 2019-08-22 stsp
5700 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
5701 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
5702 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
5703 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
5704 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5705 8e7bd50a 2019-08-22 stsp goto done;
5706 8e7bd50a 2019-08-22 stsp }
5707 8e7bd50a 2019-08-22 stsp
5708 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5709 8e7bd50a 2019-08-22 stsp if (err)
5710 8e7bd50a 2019-08-22 stsp goto done;
5711 8e7bd50a 2019-08-22 stsp
5712 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
5713 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
5714 97972933 2020-09-11 stsp goto done;
5715 97972933 2020-09-11 stsp }
5716 8e7bd50a 2019-08-22 stsp
5717 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
5718 8e7bd50a 2019-08-22 stsp if (err)
5719 8e7bd50a 2019-08-22 stsp goto done;
5720 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5721 8e7bd50a 2019-08-22 stsp done:
5722 8e7bd50a 2019-08-22 stsp free(initial_content);
5723 8e7bd50a 2019-08-22 stsp free(template);
5724 8e7bd50a 2019-08-22 stsp free(editor);
5725 8e7bd50a 2019-08-22 stsp
5726 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5727 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
5728 97972933 2020-09-11 stsp
5729 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
5730 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5731 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
5732 8e7bd50a 2019-08-22 stsp if (err) {
5733 8e7bd50a 2019-08-22 stsp free(*tagmsg);
5734 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
5735 8e7bd50a 2019-08-22 stsp }
5736 8e7bd50a 2019-08-22 stsp }
5737 8e7bd50a 2019-08-22 stsp return err;
5738 8e7bd50a 2019-08-22 stsp }
5739 8e7bd50a 2019-08-22 stsp
5740 8e7bd50a 2019-08-22 stsp static const struct got_error *
5741 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
5742 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
5743 8e7bd50a 2019-08-22 stsp {
5744 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5745 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5746 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
5747 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
5748 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5749 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5750 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
5751 8e7bd50a 2019-08-22 stsp
5752 8e7bd50a 2019-08-22 stsp /*
5753 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
5754 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
5755 8e7bd50a 2019-08-22 stsp * an unintended typo.
5756 8e7bd50a 2019-08-22 stsp */
5757 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
5758 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5759 8e7bd50a 2019-08-22 stsp
5760 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
5761 8e7bd50a 2019-08-22 stsp if (err)
5762 8e7bd50a 2019-08-22 stsp return err;
5763 8e7bd50a 2019-08-22 stsp
5764 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5765 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5766 8e7bd50a 2019-08-22 stsp if (err)
5767 8e7bd50a 2019-08-22 stsp goto done;
5768 8e7bd50a 2019-08-22 stsp
5769 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
5770 8e7bd50a 2019-08-22 stsp if (err)
5771 8e7bd50a 2019-08-22 stsp goto done;
5772 8e7bd50a 2019-08-22 stsp
5773 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5774 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
5775 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
5776 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
5777 8e7bd50a 2019-08-22 stsp goto done;
5778 8e7bd50a 2019-08-22 stsp }
5779 8e7bd50a 2019-08-22 stsp tag_name += 10;
5780 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5781 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5782 8e7bd50a 2019-08-22 stsp goto done;
5783 8e7bd50a 2019-08-22 stsp }
5784 8e7bd50a 2019-08-22 stsp
5785 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
5786 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5787 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5788 8e7bd50a 2019-08-22 stsp goto done;
5789 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
5790 8e7bd50a 2019-08-22 stsp goto done;
5791 8e7bd50a 2019-08-22 stsp
5792 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
5793 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5794 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
5795 f372d5cd 2019-10-21 stsp if (err) {
5796 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5797 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
5798 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5799 8e7bd50a 2019-08-22 stsp goto done;
5800 f372d5cd 2019-10-21 stsp }
5801 8e7bd50a 2019-08-22 stsp }
5802 8e7bd50a 2019-08-22 stsp
5803 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5804 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5805 f372d5cd 2019-10-21 stsp if (err) {
5806 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5807 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5808 8e7bd50a 2019-08-22 stsp goto done;
5809 f372d5cd 2019-10-21 stsp }
5810 8e7bd50a 2019-08-22 stsp
5811 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
5812 f372d5cd 2019-10-21 stsp if (err) {
5813 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5814 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5815 8e7bd50a 2019-08-22 stsp goto done;
5816 f372d5cd 2019-10-21 stsp }
5817 8e7bd50a 2019-08-22 stsp
5818 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
5819 f372d5cd 2019-10-21 stsp if (err) {
5820 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5821 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5822 f372d5cd 2019-10-21 stsp goto done;
5823 f372d5cd 2019-10-21 stsp }
5824 8e7bd50a 2019-08-22 stsp
5825 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
5826 f372d5cd 2019-10-21 stsp if (err) {
5827 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5828 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5829 f372d5cd 2019-10-21 stsp goto done;
5830 8e7bd50a 2019-08-22 stsp }
5831 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
5832 8e7bd50a 2019-08-22 stsp done:
5833 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
5834 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
5835 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
5836 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5837 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
5838 f372d5cd 2019-10-21 stsp free(tag_id_str);
5839 8e7bd50a 2019-08-22 stsp if (ref)
5840 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
5841 8e7bd50a 2019-08-22 stsp free(commit_id);
5842 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5843 8e7bd50a 2019-08-22 stsp free(refname);
5844 8e7bd50a 2019-08-22 stsp free(tagmsg);
5845 f372d5cd 2019-10-21 stsp free(tagmsg_path);
5846 aba9c984 2019-09-08 stsp free(tagger);
5847 8e7bd50a 2019-08-22 stsp return err;
5848 8e7bd50a 2019-08-22 stsp }
5849 8e7bd50a 2019-08-22 stsp
5850 8e7bd50a 2019-08-22 stsp static const struct got_error *
5851 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
5852 8e7bd50a 2019-08-22 stsp {
5853 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
5854 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
5855 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
5856 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5857 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
5858 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5859 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
5860 8e7bd50a 2019-08-22 stsp
5861 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5862 8e7bd50a 2019-08-22 stsp switch (ch) {
5863 80106605 2020-02-24 stsp case 'c':
5864 80106605 2020-02-24 stsp commit_id_arg = optarg;
5865 80106605 2020-02-24 stsp break;
5866 8e7bd50a 2019-08-22 stsp case 'm':
5867 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
5868 8e7bd50a 2019-08-22 stsp break;
5869 8e7bd50a 2019-08-22 stsp case 'r':
5870 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
5871 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5872 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5873 9ba1d308 2019-10-21 stsp optarg);
5874 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
5875 8e7bd50a 2019-08-22 stsp break;
5876 8e7bd50a 2019-08-22 stsp case 'l':
5877 8e7bd50a 2019-08-22 stsp do_list = 1;
5878 8e7bd50a 2019-08-22 stsp break;
5879 8e7bd50a 2019-08-22 stsp default:
5880 8e7bd50a 2019-08-22 stsp usage_tag();
5881 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
5882 8e7bd50a 2019-08-22 stsp }
5883 8e7bd50a 2019-08-22 stsp }
5884 8e7bd50a 2019-08-22 stsp
5885 8e7bd50a 2019-08-22 stsp argc -= optind;
5886 8e7bd50a 2019-08-22 stsp argv += optind;
5887 8e7bd50a 2019-08-22 stsp
5888 8e7bd50a 2019-08-22 stsp if (do_list) {
5889 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
5890 775ce909 2020-03-22 stsp errx(1,
5891 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
5892 8e7bd50a 2019-08-22 stsp if (tagmsg)
5893 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
5894 8e7bd50a 2019-08-22 stsp if (argc > 0)
5895 8e7bd50a 2019-08-22 stsp usage_tag();
5896 80106605 2020-02-24 stsp } else if (argc != 1)
5897 8e7bd50a 2019-08-22 stsp usage_tag();
5898 80106605 2020-02-24 stsp
5899 a2887370 2019-08-23 stsp tag_name = argv[0];
5900 8e7bd50a 2019-08-22 stsp
5901 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
5902 8e7bd50a 2019-08-22 stsp if (do_list) {
5903 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5904 8e7bd50a 2019-08-22 stsp NULL) == -1)
5905 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5906 8e7bd50a 2019-08-22 stsp } else {
5907 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5908 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
5909 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5910 8e7bd50a 2019-08-22 stsp }
5911 8e7bd50a 2019-08-22 stsp #endif
5912 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
5913 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
5914 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
5915 8e7bd50a 2019-08-22 stsp goto done;
5916 8e7bd50a 2019-08-22 stsp }
5917 8e7bd50a 2019-08-22 stsp
5918 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5919 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
5920 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5921 8e7bd50a 2019-08-22 stsp goto done;
5922 8e7bd50a 2019-08-22 stsp else
5923 8e7bd50a 2019-08-22 stsp error = NULL;
5924 8e7bd50a 2019-08-22 stsp if (worktree) {
5925 8e7bd50a 2019-08-22 stsp repo_path =
5926 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
5927 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5928 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5929 8e7bd50a 2019-08-22 stsp if (error)
5930 8e7bd50a 2019-08-22 stsp goto done;
5931 8e7bd50a 2019-08-22 stsp } else {
5932 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
5933 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5934 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5935 8e7bd50a 2019-08-22 stsp goto done;
5936 8e7bd50a 2019-08-22 stsp }
5937 8e7bd50a 2019-08-22 stsp }
5938 8e7bd50a 2019-08-22 stsp }
5939 8e7bd50a 2019-08-22 stsp
5940 8e7bd50a 2019-08-22 stsp if (do_list) {
5941 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5942 c9956ddf 2019-09-08 stsp if (error != NULL)
5943 c9956ddf 2019-09-08 stsp goto done;
5944 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5945 8e7bd50a 2019-08-22 stsp if (error)
5946 8e7bd50a 2019-08-22 stsp goto done;
5947 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
5948 8e7bd50a 2019-08-22 stsp } else {
5949 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5950 c9956ddf 2019-09-08 stsp if (error)
5951 c9956ddf 2019-09-08 stsp goto done;
5952 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
5953 c9956ddf 2019-09-08 stsp if (error != NULL)
5954 c9956ddf 2019-09-08 stsp goto done;
5955 c9956ddf 2019-09-08 stsp
5956 8e7bd50a 2019-08-22 stsp if (tagmsg) {
5957 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5958 8e7bd50a 2019-08-22 stsp if (error)
5959 8e7bd50a 2019-08-22 stsp goto done;
5960 8e7bd50a 2019-08-22 stsp }
5961 8e7bd50a 2019-08-22 stsp
5962 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
5963 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
5964 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
5965 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
5966 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
5967 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
5968 8e7bd50a 2019-08-22 stsp if (error)
5969 8e7bd50a 2019-08-22 stsp goto done;
5970 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5971 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
5972 8e7bd50a 2019-08-22 stsp if (error)
5973 8e7bd50a 2019-08-22 stsp goto done;
5974 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
5975 8e7bd50a 2019-08-22 stsp free(commit_id);
5976 8e7bd50a 2019-08-22 stsp if (error)
5977 8e7bd50a 2019-08-22 stsp goto done;
5978 8e7bd50a 2019-08-22 stsp }
5979 8e7bd50a 2019-08-22 stsp
5980 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
5981 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5982 8e7bd50a 2019-08-22 stsp }
5983 8e7bd50a 2019-08-22 stsp done:
5984 8e7bd50a 2019-08-22 stsp if (repo)
5985 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
5986 8e7bd50a 2019-08-22 stsp if (worktree)
5987 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
5988 8e7bd50a 2019-08-22 stsp free(cwd);
5989 8e7bd50a 2019-08-22 stsp free(repo_path);
5990 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5991 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5992 8e7bd50a 2019-08-22 stsp return error;
5993 8e7bd50a 2019-08-22 stsp }
5994 8e7bd50a 2019-08-22 stsp
5995 8e7bd50a 2019-08-22 stsp __dead static void
5996 d00136be 2019-03-26 stsp usage_add(void)
5997 d00136be 2019-03-26 stsp {
5998 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5999 022fae89 2019-12-06 tracey getprogname());
6000 d00136be 2019-03-26 stsp exit(1);
6001 d00136be 2019-03-26 stsp }
6002 d00136be 2019-03-26 stsp
6003 d00136be 2019-03-26 stsp static const struct got_error *
6004 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6005 4e68cba3 2019-11-23 stsp {
6006 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6007 4e68cba3 2019-11-23 stsp path++;
6008 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6009 4e68cba3 2019-11-23 stsp return NULL;
6010 4e68cba3 2019-11-23 stsp }
6011 4e68cba3 2019-11-23 stsp
6012 4e68cba3 2019-11-23 stsp static const struct got_error *
6013 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6014 d00136be 2019-03-26 stsp {
6015 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6016 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6017 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6018 1dd54920 2019-05-11 stsp char *cwd = NULL;
6019 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6020 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6021 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6022 1dd54920 2019-05-11 stsp
6023 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6024 d00136be 2019-03-26 stsp
6025 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6026 d00136be 2019-03-26 stsp switch (ch) {
6027 022fae89 2019-12-06 tracey case 'I':
6028 022fae89 2019-12-06 tracey no_ignores = 1;
6029 022fae89 2019-12-06 tracey break;
6030 4e68cba3 2019-11-23 stsp case 'R':
6031 4e68cba3 2019-11-23 stsp can_recurse = 1;
6032 4e68cba3 2019-11-23 stsp break;
6033 d00136be 2019-03-26 stsp default:
6034 d00136be 2019-03-26 stsp usage_add();
6035 d00136be 2019-03-26 stsp /* NOTREACHED */
6036 d00136be 2019-03-26 stsp }
6037 d00136be 2019-03-26 stsp }
6038 d00136be 2019-03-26 stsp
6039 d00136be 2019-03-26 stsp argc -= optind;
6040 d00136be 2019-03-26 stsp argv += optind;
6041 d00136be 2019-03-26 stsp
6042 43012d58 2019-07-14 stsp #ifndef PROFILE
6043 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6044 43012d58 2019-07-14 stsp NULL) == -1)
6045 43012d58 2019-07-14 stsp err(1, "pledge");
6046 43012d58 2019-07-14 stsp #endif
6047 723c305c 2019-05-11 jcs if (argc < 1)
6048 d00136be 2019-03-26 stsp usage_add();
6049 d00136be 2019-03-26 stsp
6050 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6051 d00136be 2019-03-26 stsp if (cwd == NULL) {
6052 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6053 d00136be 2019-03-26 stsp goto done;
6054 d00136be 2019-03-26 stsp }
6055 723c305c 2019-05-11 jcs
6056 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6057 fa51e947 2020-03-27 stsp if (error) {
6058 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6059 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6060 d00136be 2019-03-26 stsp goto done;
6061 fa51e947 2020-03-27 stsp }
6062 d00136be 2019-03-26 stsp
6063 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6064 c9956ddf 2019-09-08 stsp NULL);
6065 031a5338 2019-03-26 stsp if (error != NULL)
6066 031a5338 2019-03-26 stsp goto done;
6067 031a5338 2019-03-26 stsp
6068 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6069 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6070 d00136be 2019-03-26 stsp if (error)
6071 d00136be 2019-03-26 stsp goto done;
6072 d00136be 2019-03-26 stsp
6073 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6074 6d022e97 2019-08-04 stsp if (error)
6075 022fae89 2019-12-06 tracey goto done;
6076 022fae89 2019-12-06 tracey
6077 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
6078 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6079 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
6080 6d022e97 2019-08-04 stsp goto done;
6081 723c305c 2019-05-11 jcs
6082 022fae89 2019-12-06 tracey }
6083 022fae89 2019-12-06 tracey
6084 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6085 4e68cba3 2019-11-23 stsp char *ondisk_path;
6086 4e68cba3 2019-11-23 stsp struct stat sb;
6087 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6088 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6089 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6090 4e68cba3 2019-11-23 stsp pe->path) == -1) {
6091 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6092 4e68cba3 2019-11-23 stsp goto done;
6093 4e68cba3 2019-11-23 stsp }
6094 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6095 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6096 4e68cba3 2019-11-23 stsp free(ondisk_path);
6097 4e68cba3 2019-11-23 stsp continue;
6098 4e68cba3 2019-11-23 stsp }
6099 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6100 4e68cba3 2019-11-23 stsp ondisk_path);
6101 4e68cba3 2019-11-23 stsp free(ondisk_path);
6102 4e68cba3 2019-11-23 stsp goto done;
6103 4e68cba3 2019-11-23 stsp }
6104 4e68cba3 2019-11-23 stsp free(ondisk_path);
6105 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6106 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6107 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6108 4e68cba3 2019-11-23 stsp goto done;
6109 4e68cba3 2019-11-23 stsp }
6110 4e68cba3 2019-11-23 stsp }
6111 4e68cba3 2019-11-23 stsp }
6112 022fae89 2019-12-06 tracey
6113 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6114 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6115 d00136be 2019-03-26 stsp done:
6116 031a5338 2019-03-26 stsp if (repo)
6117 031a5338 2019-03-26 stsp got_repo_close(repo);
6118 d00136be 2019-03-26 stsp if (worktree)
6119 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6120 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6121 1dd54920 2019-05-11 stsp free((char *)pe->path);
6122 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6123 2ec1f75b 2019-03-26 stsp free(cwd);
6124 2ec1f75b 2019-03-26 stsp return error;
6125 2ec1f75b 2019-03-26 stsp }
6126 2ec1f75b 2019-03-26 stsp
6127 2ec1f75b 2019-03-26 stsp __dead static void
6128 648e4ef7 2019-07-09 stsp usage_remove(void)
6129 2ec1f75b 2019-03-26 stsp {
6130 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6131 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6132 2ec1f75b 2019-03-26 stsp exit(1);
6133 2a06fe5f 2019-08-24 stsp }
6134 2a06fe5f 2019-08-24 stsp
6135 2a06fe5f 2019-08-24 stsp static const struct got_error *
6136 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6137 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6138 2a06fe5f 2019-08-24 stsp {
6139 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6140 f2a9dc41 2019-12-13 tracey path++;
6141 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6142 2a06fe5f 2019-08-24 stsp return NULL;
6143 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6144 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6145 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6146 2a06fe5f 2019-08-24 stsp return NULL;
6147 2ec1f75b 2019-03-26 stsp }
6148 2ec1f75b 2019-03-26 stsp
6149 2ec1f75b 2019-03-26 stsp static const struct got_error *
6150 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6151 2ec1f75b 2019-03-26 stsp {
6152 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6153 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6154 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6155 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6156 17ed4618 2019-06-02 stsp char *cwd = NULL;
6157 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6158 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6159 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6160 2ec1f75b 2019-03-26 stsp
6161 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6162 17ed4618 2019-06-02 stsp
6163 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6164 2ec1f75b 2019-03-26 stsp switch (ch) {
6165 2ec1f75b 2019-03-26 stsp case 'f':
6166 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6167 2ec1f75b 2019-03-26 stsp break;
6168 70e3e7f5 2019-12-13 tracey case 'k':
6169 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6170 70e3e7f5 2019-12-13 tracey break;
6171 f2a9dc41 2019-12-13 tracey case 'R':
6172 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6173 f2a9dc41 2019-12-13 tracey break;
6174 766841c2 2020-08-13 stsp case 's':
6175 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6176 766841c2 2020-08-13 stsp switch (optarg[i]) {
6177 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6178 766841c2 2020-08-13 stsp delete_local_mods = 1;
6179 766841c2 2020-08-13 stsp break;
6180 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6181 766841c2 2020-08-13 stsp break;
6182 766841c2 2020-08-13 stsp default:
6183 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6184 766841c2 2020-08-13 stsp optarg[i]);
6185 766841c2 2020-08-13 stsp }
6186 766841c2 2020-08-13 stsp }
6187 766841c2 2020-08-13 stsp status_codes = optarg;
6188 766841c2 2020-08-13 stsp break;
6189 2ec1f75b 2019-03-26 stsp default:
6190 f2a9dc41 2019-12-13 tracey usage_remove();
6191 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6192 2ec1f75b 2019-03-26 stsp }
6193 2ec1f75b 2019-03-26 stsp }
6194 2ec1f75b 2019-03-26 stsp
6195 2ec1f75b 2019-03-26 stsp argc -= optind;
6196 2ec1f75b 2019-03-26 stsp argv += optind;
6197 2ec1f75b 2019-03-26 stsp
6198 43012d58 2019-07-14 stsp #ifndef PROFILE
6199 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6200 43012d58 2019-07-14 stsp NULL) == -1)
6201 43012d58 2019-07-14 stsp err(1, "pledge");
6202 43012d58 2019-07-14 stsp #endif
6203 17ed4618 2019-06-02 stsp if (argc < 1)
6204 648e4ef7 2019-07-09 stsp usage_remove();
6205 2ec1f75b 2019-03-26 stsp
6206 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6207 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6208 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6209 2ec1f75b 2019-03-26 stsp goto done;
6210 2ec1f75b 2019-03-26 stsp }
6211 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6212 fa51e947 2020-03-27 stsp if (error) {
6213 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6214 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6215 2ec1f75b 2019-03-26 stsp goto done;
6216 fa51e947 2020-03-27 stsp }
6217 2ec1f75b 2019-03-26 stsp
6218 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6219 c9956ddf 2019-09-08 stsp NULL);
6220 2af4a041 2019-05-11 jcs if (error)
6221 2ec1f75b 2019-03-26 stsp goto done;
6222 2ec1f75b 2019-03-26 stsp
6223 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6224 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6225 2ec1f75b 2019-03-26 stsp if (error)
6226 2ec1f75b 2019-03-26 stsp goto done;
6227 2ec1f75b 2019-03-26 stsp
6228 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6229 6d022e97 2019-08-04 stsp if (error)
6230 6d022e97 2019-08-04 stsp goto done;
6231 17ed4618 2019-06-02 stsp
6232 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6233 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6234 f2a9dc41 2019-12-13 tracey struct stat sb;
6235 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6236 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6237 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6238 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
6239 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6240 f2a9dc41 2019-12-13 tracey goto done;
6241 f2a9dc41 2019-12-13 tracey }
6242 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6243 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6244 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6245 f2a9dc41 2019-12-13 tracey continue;
6246 f2a9dc41 2019-12-13 tracey }
6247 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6248 f2a9dc41 2019-12-13 tracey ondisk_path);
6249 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6250 f2a9dc41 2019-12-13 tracey goto done;
6251 f2a9dc41 2019-12-13 tracey }
6252 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6253 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6254 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6255 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6256 f2a9dc41 2019-12-13 tracey goto done;
6257 f2a9dc41 2019-12-13 tracey }
6258 f2a9dc41 2019-12-13 tracey }
6259 f2a9dc41 2019-12-13 tracey }
6260 f2a9dc41 2019-12-13 tracey
6261 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6262 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6263 766841c2 2020-08-13 stsp repo, keep_on_disk);
6264 a129376b 2019-03-28 stsp done:
6265 a129376b 2019-03-28 stsp if (repo)
6266 a129376b 2019-03-28 stsp got_repo_close(repo);
6267 a129376b 2019-03-28 stsp if (worktree)
6268 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6269 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6270 17ed4618 2019-06-02 stsp free((char *)pe->path);
6271 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6272 a129376b 2019-03-28 stsp free(cwd);
6273 a129376b 2019-03-28 stsp return error;
6274 a129376b 2019-03-28 stsp }
6275 a129376b 2019-03-28 stsp
6276 a129376b 2019-03-28 stsp __dead static void
6277 a129376b 2019-03-28 stsp usage_revert(void)
6278 a129376b 2019-03-28 stsp {
6279 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6280 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6281 a129376b 2019-03-28 stsp exit(1);
6282 a129376b 2019-03-28 stsp }
6283 a129376b 2019-03-28 stsp
6284 1ee397ad 2019-07-12 stsp static const struct got_error *
6285 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6286 a129376b 2019-03-28 stsp {
6287 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6288 fb9704af 2020-01-27 stsp return NULL;
6289 fb9704af 2020-01-27 stsp
6290 a129376b 2019-03-28 stsp while (path[0] == '/')
6291 a129376b 2019-03-28 stsp path++;
6292 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6293 33aa809d 2019-08-08 stsp return NULL;
6294 33aa809d 2019-08-08 stsp }
6295 33aa809d 2019-08-08 stsp
6296 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6297 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6298 33aa809d 2019-08-08 stsp const char *action;
6299 33aa809d 2019-08-08 stsp };
6300 33aa809d 2019-08-08 stsp
6301 33aa809d 2019-08-08 stsp static const struct got_error *
6302 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6303 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6304 33aa809d 2019-08-08 stsp {
6305 33aa809d 2019-08-08 stsp char *line = NULL;
6306 33aa809d 2019-08-08 stsp size_t linesize = 0;
6307 33aa809d 2019-08-08 stsp ssize_t linelen;
6308 33aa809d 2019-08-08 stsp
6309 33aa809d 2019-08-08 stsp switch (status) {
6310 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6311 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6312 33aa809d 2019-08-08 stsp break;
6313 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6314 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6315 33aa809d 2019-08-08 stsp break;
6316 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6317 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6318 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6319 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6320 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6321 33aa809d 2019-08-08 stsp printf("%s", line);
6322 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6323 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6324 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6325 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6326 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6327 33aa809d 2019-08-08 stsp break;
6328 33aa809d 2019-08-08 stsp default:
6329 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6330 33aa809d 2019-08-08 stsp }
6331 33aa809d 2019-08-08 stsp
6332 33aa809d 2019-08-08 stsp return NULL;
6333 33aa809d 2019-08-08 stsp }
6334 33aa809d 2019-08-08 stsp
6335 33aa809d 2019-08-08 stsp static const struct got_error *
6336 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6337 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6338 33aa809d 2019-08-08 stsp {
6339 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6340 33aa809d 2019-08-08 stsp char *line = NULL;
6341 33aa809d 2019-08-08 stsp size_t linesize = 0;
6342 33aa809d 2019-08-08 stsp ssize_t linelen;
6343 33aa809d 2019-08-08 stsp int resp = ' ';
6344 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6345 33aa809d 2019-08-08 stsp
6346 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6347 33aa809d 2019-08-08 stsp
6348 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6349 33aa809d 2019-08-08 stsp char *nl;
6350 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6351 33aa809d 2019-08-08 stsp a->action);
6352 33aa809d 2019-08-08 stsp if (err)
6353 33aa809d 2019-08-08 stsp return err;
6354 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6355 33aa809d 2019-08-08 stsp if (linelen == -1) {
6356 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6357 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6358 33aa809d 2019-08-08 stsp return NULL;
6359 33aa809d 2019-08-08 stsp }
6360 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6361 33aa809d 2019-08-08 stsp if (nl)
6362 33aa809d 2019-08-08 stsp *nl = '\0';
6363 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6364 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6365 33aa809d 2019-08-08 stsp printf("y\n");
6366 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6367 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6368 33aa809d 2019-08-08 stsp printf("n\n");
6369 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6370 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6371 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6372 33aa809d 2019-08-08 stsp printf("q\n");
6373 33aa809d 2019-08-08 stsp } else
6374 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6375 33aa809d 2019-08-08 stsp free(line);
6376 33aa809d 2019-08-08 stsp return NULL;
6377 33aa809d 2019-08-08 stsp }
6378 33aa809d 2019-08-08 stsp
6379 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6380 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6381 33aa809d 2019-08-08 stsp a->action);
6382 33aa809d 2019-08-08 stsp if (err)
6383 33aa809d 2019-08-08 stsp return err;
6384 33aa809d 2019-08-08 stsp resp = getchar();
6385 33aa809d 2019-08-08 stsp if (resp == '\n')
6386 33aa809d 2019-08-08 stsp resp = getchar();
6387 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6388 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6389 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6390 33aa809d 2019-08-08 stsp resp = ' ';
6391 33aa809d 2019-08-08 stsp }
6392 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6393 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6394 33aa809d 2019-08-08 stsp resp = ' ';
6395 33aa809d 2019-08-08 stsp }
6396 33aa809d 2019-08-08 stsp }
6397 33aa809d 2019-08-08 stsp
6398 33aa809d 2019-08-08 stsp if (resp == 'y')
6399 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6400 33aa809d 2019-08-08 stsp else if (resp == 'n')
6401 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6402 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6403 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6404 33aa809d 2019-08-08 stsp
6405 1ee397ad 2019-07-12 stsp return NULL;
6406 a129376b 2019-03-28 stsp }
6407 a129376b 2019-03-28 stsp
6408 33aa809d 2019-08-08 stsp
6409 a129376b 2019-03-28 stsp static const struct got_error *
6410 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6411 a129376b 2019-03-28 stsp {
6412 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6413 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6414 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6415 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6416 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6417 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6418 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6419 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6420 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6421 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6422 a129376b 2019-03-28 stsp
6423 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6424 e20a8b6f 2019-06-04 stsp
6425 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6426 a129376b 2019-03-28 stsp switch (ch) {
6427 33aa809d 2019-08-08 stsp case 'p':
6428 33aa809d 2019-08-08 stsp pflag = 1;
6429 33aa809d 2019-08-08 stsp break;
6430 33aa809d 2019-08-08 stsp case 'F':
6431 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6432 33aa809d 2019-08-08 stsp break;
6433 0f6d7415 2019-08-08 stsp case 'R':
6434 0f6d7415 2019-08-08 stsp can_recurse = 1;
6435 0f6d7415 2019-08-08 stsp break;
6436 a129376b 2019-03-28 stsp default:
6437 a129376b 2019-03-28 stsp usage_revert();
6438 a129376b 2019-03-28 stsp /* NOTREACHED */
6439 a129376b 2019-03-28 stsp }
6440 a129376b 2019-03-28 stsp }
6441 a129376b 2019-03-28 stsp
6442 a129376b 2019-03-28 stsp argc -= optind;
6443 a129376b 2019-03-28 stsp argv += optind;
6444 a129376b 2019-03-28 stsp
6445 43012d58 2019-07-14 stsp #ifndef PROFILE
6446 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6447 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6448 43012d58 2019-07-14 stsp err(1, "pledge");
6449 43012d58 2019-07-14 stsp #endif
6450 e20a8b6f 2019-06-04 stsp if (argc < 1)
6451 a129376b 2019-03-28 stsp usage_revert();
6452 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6453 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6454 a129376b 2019-03-28 stsp
6455 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6456 a129376b 2019-03-28 stsp if (cwd == NULL) {
6457 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6458 a129376b 2019-03-28 stsp goto done;
6459 a129376b 2019-03-28 stsp }
6460 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6461 fa51e947 2020-03-27 stsp if (error) {
6462 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6463 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6464 2ec1f75b 2019-03-26 stsp goto done;
6465 fa51e947 2020-03-27 stsp }
6466 a129376b 2019-03-28 stsp
6467 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6468 c9956ddf 2019-09-08 stsp NULL);
6469 a129376b 2019-03-28 stsp if (error != NULL)
6470 a129376b 2019-03-28 stsp goto done;
6471 a129376b 2019-03-28 stsp
6472 33aa809d 2019-08-08 stsp if (patch_script_path) {
6473 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6474 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
6475 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
6476 33aa809d 2019-08-08 stsp patch_script_path);
6477 33aa809d 2019-08-08 stsp goto done;
6478 33aa809d 2019-08-08 stsp }
6479 33aa809d 2019-08-08 stsp }
6480 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6481 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6482 a129376b 2019-03-28 stsp if (error)
6483 a129376b 2019-03-28 stsp goto done;
6484 a129376b 2019-03-28 stsp
6485 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6486 6d022e97 2019-08-04 stsp if (error)
6487 6d022e97 2019-08-04 stsp goto done;
6488 00db391e 2019-08-03 stsp
6489 0f6d7415 2019-08-08 stsp if (!can_recurse) {
6490 0f6d7415 2019-08-08 stsp char *ondisk_path;
6491 0f6d7415 2019-08-08 stsp struct stat sb;
6492 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
6493 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
6494 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
6495 0f6d7415 2019-08-08 stsp pe->path) == -1) {
6496 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
6497 0f6d7415 2019-08-08 stsp goto done;
6498 0f6d7415 2019-08-08 stsp }
6499 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
6500 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
6501 0f6d7415 2019-08-08 stsp free(ondisk_path);
6502 0f6d7415 2019-08-08 stsp continue;
6503 0f6d7415 2019-08-08 stsp }
6504 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
6505 0f6d7415 2019-08-08 stsp ondisk_path);
6506 0f6d7415 2019-08-08 stsp free(ondisk_path);
6507 0f6d7415 2019-08-08 stsp goto done;
6508 0f6d7415 2019-08-08 stsp }
6509 0f6d7415 2019-08-08 stsp free(ondisk_path);
6510 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
6511 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6512 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
6513 0f6d7415 2019-08-08 stsp goto done;
6514 0f6d7415 2019-08-08 stsp }
6515 0f6d7415 2019-08-08 stsp }
6516 0f6d7415 2019-08-08 stsp }
6517 0f6d7415 2019-08-08 stsp
6518 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6519 33aa809d 2019-08-08 stsp cpa.action = "revert";
6520 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6521 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6522 2ec1f75b 2019-03-26 stsp done:
6523 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6524 33aa809d 2019-08-08 stsp error == NULL)
6525 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6526 2ec1f75b 2019-03-26 stsp if (repo)
6527 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
6528 2ec1f75b 2019-03-26 stsp if (worktree)
6529 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
6530 2ec1f75b 2019-03-26 stsp free(path);
6531 d00136be 2019-03-26 stsp free(cwd);
6532 6bad629b 2019-02-04 stsp return error;
6533 c4296144 2019-05-09 stsp }
6534 c4296144 2019-05-09 stsp
6535 c4296144 2019-05-09 stsp __dead static void
6536 c4296144 2019-05-09 stsp usage_commit(void)
6537 c4296144 2019-05-09 stsp {
6538 35213c7c 2020-07-23 stsp fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6539 5c1e53bc 2019-07-28 stsp getprogname());
6540 c4296144 2019-05-09 stsp exit(1);
6541 33ad4cbe 2019-05-12 jcs }
6542 33ad4cbe 2019-05-12 jcs
6543 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
6544 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
6545 e2ba3d07 2019-05-13 stsp const char *editor;
6546 e0870e44 2019-05-13 stsp const char *worktree_path;
6547 76d98825 2019-06-03 stsp const char *branch_name;
6548 314a6357 2019-05-13 stsp const char *repo_path;
6549 e0870e44 2019-05-13 stsp char *logmsg_path;
6550 e2ba3d07 2019-05-13 stsp
6551 e2ba3d07 2019-05-13 stsp };
6552 e0870e44 2019-05-13 stsp
6553 33ad4cbe 2019-05-12 jcs static const struct got_error *
6554 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6555 33ad4cbe 2019-05-12 jcs void *arg)
6556 33ad4cbe 2019-05-12 jcs {
6557 76d98825 2019-06-03 stsp char *initial_content = NULL;
6558 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
6559 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
6560 e0870e44 2019-05-13 stsp char *template = NULL;
6561 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
6562 1601cb9f 2020-09-11 naddy int initial_content_len;
6563 97972933 2020-09-11 stsp int fd = -1;
6564 33ad4cbe 2019-05-12 jcs size_t len;
6565 33ad4cbe 2019-05-12 jcs
6566 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
6567 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6568 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
6569 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
6570 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
6571 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
6572 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
6573 33ad4cbe 2019-05-12 jcs return NULL;
6574 33ad4cbe 2019-05-12 jcs }
6575 33ad4cbe 2019-05-12 jcs
6576 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6577 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
6578 76d98825 2019-06-03 stsp
6579 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6580 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
6581 1601cb9f 2020-09-11 naddy a->branch_name);
6582 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
6583 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6584 e0870e44 2019-05-13 stsp
6585 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6586 793c30b5 2019-05-13 stsp if (err)
6587 e0870e44 2019-05-13 stsp goto done;
6588 33ad4cbe 2019-05-12 jcs
6589 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6590 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
6591 97972933 2020-09-11 stsp goto done;
6592 97972933 2020-09-11 stsp }
6593 33ad4cbe 2019-05-12 jcs
6594 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
6595 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6596 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
6597 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
6598 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
6599 33ad4cbe 2019-05-12 jcs }
6600 33ad4cbe 2019-05-12 jcs
6601 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6602 33ad4cbe 2019-05-12 jcs done:
6603 76d98825 2019-06-03 stsp free(initial_content);
6604 e0870e44 2019-05-13 stsp free(template);
6605 314a6357 2019-05-13 stsp
6606 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6607 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
6608 97972933 2020-09-11 stsp
6609 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
6610 3ce1b845 2019-07-15 stsp if (err == NULL) {
6611 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6612 3ce1b845 2019-07-15 stsp if (err) {
6613 3ce1b845 2019-07-15 stsp free(*logmsg);
6614 3ce1b845 2019-07-15 stsp *logmsg = NULL;
6615 3ce1b845 2019-07-15 stsp }
6616 3ce1b845 2019-07-15 stsp }
6617 33ad4cbe 2019-05-12 jcs return err;
6618 6bad629b 2019-02-04 stsp }
6619 c4296144 2019-05-09 stsp
6620 c4296144 2019-05-09 stsp static const struct got_error *
6621 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
6622 c4296144 2019-05-09 stsp {
6623 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
6624 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
6625 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
6626 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
6627 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
6628 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
6629 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
6630 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6631 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6632 35213c7c 2020-07-23 stsp int allow_bad_symlinks = 0;
6633 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
6634 5c1e53bc 2019-07-28 stsp
6635 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
6636 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
6637 c4296144 2019-05-09 stsp
6638 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "m:S")) != -1) {
6639 c4296144 2019-05-09 stsp switch (ch) {
6640 c4296144 2019-05-09 stsp case 'm':
6641 c4296144 2019-05-09 stsp logmsg = optarg;
6642 c4296144 2019-05-09 stsp break;
6643 35213c7c 2020-07-23 stsp case 'S':
6644 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
6645 35213c7c 2020-07-23 stsp break;
6646 c4296144 2019-05-09 stsp default:
6647 c4296144 2019-05-09 stsp usage_commit();
6648 c4296144 2019-05-09 stsp /* NOTREACHED */
6649 c4296144 2019-05-09 stsp }
6650 c4296144 2019-05-09 stsp }
6651 c4296144 2019-05-09 stsp
6652 c4296144 2019-05-09 stsp argc -= optind;
6653 c4296144 2019-05-09 stsp argv += optind;
6654 c4296144 2019-05-09 stsp
6655 43012d58 2019-07-14 stsp #ifndef PROFILE
6656 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6657 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6658 43012d58 2019-07-14 stsp err(1, "pledge");
6659 43012d58 2019-07-14 stsp #endif
6660 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
6661 c4296144 2019-05-09 stsp if (cwd == NULL) {
6662 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6663 c4296144 2019-05-09 stsp goto done;
6664 c4296144 2019-05-09 stsp }
6665 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
6666 fa51e947 2020-03-27 stsp if (error) {
6667 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6668 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
6669 7d5807f4 2019-07-11 stsp goto done;
6670 fa51e947 2020-03-27 stsp }
6671 7d5807f4 2019-07-11 stsp
6672 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6673 c4296144 2019-05-09 stsp if (error)
6674 a698f62e 2019-07-25 stsp goto done;
6675 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
6676 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
6677 c4296144 2019-05-09 stsp goto done;
6678 a698f62e 2019-07-25 stsp }
6679 5c1e53bc 2019-07-28 stsp
6680 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6681 916f288c 2019-07-30 stsp worktree);
6682 5c1e53bc 2019-07-28 stsp if (error)
6683 5c1e53bc 2019-07-28 stsp goto done;
6684 c4296144 2019-05-09 stsp
6685 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6686 c9956ddf 2019-09-08 stsp if (error)
6687 c9956ddf 2019-09-08 stsp goto done;
6688 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6689 c9956ddf 2019-09-08 stsp gitconfig_path);
6690 c4296144 2019-05-09 stsp if (error != NULL)
6691 c4296144 2019-05-09 stsp goto done;
6692 c4296144 2019-05-09 stsp
6693 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
6694 aba9c984 2019-09-08 stsp if (error)
6695 aba9c984 2019-09-08 stsp return error;
6696 aba9c984 2019-09-08 stsp
6697 314a6357 2019-05-13 stsp /*
6698 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
6699 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
6700 314a6357 2019-05-13 stsp */
6701 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6702 314a6357 2019-05-13 stsp error = get_editor(&editor);
6703 314a6357 2019-05-13 stsp else
6704 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6705 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6706 c4296144 2019-05-09 stsp if (error)
6707 c4296144 2019-05-09 stsp goto done;
6708 c4296144 2019-05-09 stsp
6709 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6710 087fb88c 2019-08-04 stsp if (error)
6711 087fb88c 2019-08-04 stsp goto done;
6712 087fb88c 2019-08-04 stsp
6713 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
6714 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
6715 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6716 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6717 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
6718 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6719 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6720 916f288c 2019-07-30 stsp goto done;
6721 916f288c 2019-07-30 stsp }
6722 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
6723 916f288c 2019-07-30 stsp }
6724 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
6725 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6726 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6727 35213c7c 2020-07-23 stsp print_status, NULL, repo);
6728 e0870e44 2019-05-13 stsp if (error) {
6729 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6730 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
6731 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
6732 c4296144 2019-05-09 stsp goto done;
6733 e0870e44 2019-05-13 stsp }
6734 c4296144 2019-05-09 stsp
6735 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
6736 c4296144 2019-05-09 stsp if (error)
6737 c4296144 2019-05-09 stsp goto done;
6738 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
6739 c4296144 2019-05-09 stsp done:
6740 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
6741 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6742 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
6743 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6744 7266f21f 2019-10-21 stsp error == NULL)
6745 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6746 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
6747 c4296144 2019-05-09 stsp if (repo)
6748 c4296144 2019-05-09 stsp got_repo_close(repo);
6749 c4296144 2019-05-09 stsp if (worktree)
6750 c4296144 2019-05-09 stsp got_worktree_close(worktree);
6751 c4296144 2019-05-09 stsp free(cwd);
6752 c4296144 2019-05-09 stsp free(id_str);
6753 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6754 e2ba3d07 2019-05-13 stsp free(editor);
6755 aba9c984 2019-09-08 stsp free(author);
6756 234035bc 2019-06-01 stsp return error;
6757 234035bc 2019-06-01 stsp }
6758 234035bc 2019-06-01 stsp
6759 234035bc 2019-06-01 stsp __dead static void
6760 234035bc 2019-06-01 stsp usage_cherrypick(void)
6761 234035bc 2019-06-01 stsp {
6762 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6763 234035bc 2019-06-01 stsp exit(1);
6764 234035bc 2019-06-01 stsp }
6765 234035bc 2019-06-01 stsp
6766 234035bc 2019-06-01 stsp static const struct got_error *
6767 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
6768 234035bc 2019-06-01 stsp {
6769 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
6770 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
6771 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
6772 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
6773 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
6774 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
6775 234035bc 2019-06-01 stsp struct got_object_qid *pid;
6776 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
6777 9627c110 2020-04-18 stsp int ch;
6778 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6779 234035bc 2019-06-01 stsp
6780 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6781 234035bc 2019-06-01 stsp switch (ch) {
6782 234035bc 2019-06-01 stsp default:
6783 234035bc 2019-06-01 stsp usage_cherrypick();
6784 234035bc 2019-06-01 stsp /* NOTREACHED */
6785 234035bc 2019-06-01 stsp }
6786 234035bc 2019-06-01 stsp }
6787 234035bc 2019-06-01 stsp
6788 234035bc 2019-06-01 stsp argc -= optind;
6789 234035bc 2019-06-01 stsp argv += optind;
6790 234035bc 2019-06-01 stsp
6791 43012d58 2019-07-14 stsp #ifndef PROFILE
6792 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6793 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6794 43012d58 2019-07-14 stsp err(1, "pledge");
6795 43012d58 2019-07-14 stsp #endif
6796 234035bc 2019-06-01 stsp if (argc != 1)
6797 234035bc 2019-06-01 stsp usage_cherrypick();
6798 234035bc 2019-06-01 stsp
6799 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
6800 234035bc 2019-06-01 stsp if (cwd == NULL) {
6801 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
6802 234035bc 2019-06-01 stsp goto done;
6803 234035bc 2019-06-01 stsp }
6804 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
6805 fa51e947 2020-03-27 stsp if (error) {
6806 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6807 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
6808 fa51e947 2020-03-27 stsp cwd);
6809 234035bc 2019-06-01 stsp goto done;
6810 fa51e947 2020-03-27 stsp }
6811 234035bc 2019-06-01 stsp
6812 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6813 c9956ddf 2019-09-08 stsp NULL);
6814 234035bc 2019-06-01 stsp if (error != NULL)
6815 234035bc 2019-06-01 stsp goto done;
6816 234035bc 2019-06-01 stsp
6817 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6818 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6819 234035bc 2019-06-01 stsp if (error)
6820 234035bc 2019-06-01 stsp goto done;
6821 234035bc 2019-06-01 stsp
6822 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6823 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6824 234035bc 2019-06-01 stsp if (error != NULL) {
6825 234035bc 2019-06-01 stsp struct got_reference *ref;
6826 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6827 234035bc 2019-06-01 stsp goto done;
6828 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6829 234035bc 2019-06-01 stsp if (error != NULL)
6830 234035bc 2019-06-01 stsp goto done;
6831 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
6832 234035bc 2019-06-01 stsp got_ref_close(ref);
6833 234035bc 2019-06-01 stsp if (error != NULL)
6834 234035bc 2019-06-01 stsp goto done;
6835 234035bc 2019-06-01 stsp }
6836 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
6837 234035bc 2019-06-01 stsp if (error)
6838 234035bc 2019-06-01 stsp goto done;
6839 234035bc 2019-06-01 stsp
6840 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
6841 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
6842 234035bc 2019-06-01 stsp if (error != NULL)
6843 234035bc 2019-06-01 stsp goto done;
6844 234035bc 2019-06-01 stsp
6845 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6846 234035bc 2019-06-01 stsp if (error) {
6847 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
6848 234035bc 2019-06-01 stsp goto done;
6849 234035bc 2019-06-01 stsp error = NULL;
6850 234035bc 2019-06-01 stsp } else {
6851 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
6852 234035bc 2019-06-01 stsp goto done;
6853 234035bc 2019-06-01 stsp }
6854 234035bc 2019-06-01 stsp
6855 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6856 234035bc 2019-06-01 stsp if (error)
6857 234035bc 2019-06-01 stsp goto done;
6858 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6859 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6860 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6861 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
6862 03415a1a 2019-06-02 stsp NULL);
6863 234035bc 2019-06-01 stsp if (error != NULL)
6864 234035bc 2019-06-01 stsp goto done;
6865 234035bc 2019-06-01 stsp
6866 9627c110 2020-04-18 stsp if (upa.did_something)
6867 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
6868 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6869 234035bc 2019-06-01 stsp done:
6870 234035bc 2019-06-01 stsp if (commit)
6871 234035bc 2019-06-01 stsp got_object_commit_close(commit);
6872 234035bc 2019-06-01 stsp free(commit_id_str);
6873 234035bc 2019-06-01 stsp if (head_ref)
6874 234035bc 2019-06-01 stsp got_ref_close(head_ref);
6875 234035bc 2019-06-01 stsp if (worktree)
6876 234035bc 2019-06-01 stsp got_worktree_close(worktree);
6877 234035bc 2019-06-01 stsp if (repo)
6878 234035bc 2019-06-01 stsp got_repo_close(repo);
6879 c4296144 2019-05-09 stsp return error;
6880 c4296144 2019-05-09 stsp }
6881 5ef14e63 2019-06-02 stsp
6882 5ef14e63 2019-06-02 stsp __dead static void
6883 5ef14e63 2019-06-02 stsp usage_backout(void)
6884 5ef14e63 2019-06-02 stsp {
6885 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6886 5ef14e63 2019-06-02 stsp exit(1);
6887 5ef14e63 2019-06-02 stsp }
6888 5ef14e63 2019-06-02 stsp
6889 5ef14e63 2019-06-02 stsp static const struct got_error *
6890 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
6891 5ef14e63 2019-06-02 stsp {
6892 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
6893 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
6894 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
6895 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
6896 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
6897 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
6898 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
6899 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
6900 9627c110 2020-04-18 stsp int ch;
6901 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6902 5ef14e63 2019-06-02 stsp
6903 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6904 5ef14e63 2019-06-02 stsp switch (ch) {
6905 5ef14e63 2019-06-02 stsp default:
6906 5ef14e63 2019-06-02 stsp usage_backout();
6907 5ef14e63 2019-06-02 stsp /* NOTREACHED */
6908 5ef14e63 2019-06-02 stsp }
6909 5ef14e63 2019-06-02 stsp }
6910 5ef14e63 2019-06-02 stsp
6911 5ef14e63 2019-06-02 stsp argc -= optind;
6912 5ef14e63 2019-06-02 stsp argv += optind;
6913 5ef14e63 2019-06-02 stsp
6914 43012d58 2019-07-14 stsp #ifndef PROFILE
6915 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6916 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6917 43012d58 2019-07-14 stsp err(1, "pledge");
6918 43012d58 2019-07-14 stsp #endif
6919 5ef14e63 2019-06-02 stsp if (argc != 1)
6920 5ef14e63 2019-06-02 stsp usage_backout();
6921 5ef14e63 2019-06-02 stsp
6922 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
6923 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
6924 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
6925 5ef14e63 2019-06-02 stsp goto done;
6926 5ef14e63 2019-06-02 stsp }
6927 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
6928 fa51e947 2020-03-27 stsp if (error) {
6929 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6930 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
6931 5ef14e63 2019-06-02 stsp goto done;
6932 fa51e947 2020-03-27 stsp }
6933 5ef14e63 2019-06-02 stsp
6934 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6935 c9956ddf 2019-09-08 stsp NULL);
6936 5ef14e63 2019-06-02 stsp if (error != NULL)
6937 5ef14e63 2019-06-02 stsp goto done;
6938 5ef14e63 2019-06-02 stsp
6939 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6940 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6941 5ef14e63 2019-06-02 stsp if (error)
6942 5ef14e63 2019-06-02 stsp goto done;
6943 5ef14e63 2019-06-02 stsp
6944 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6945 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6946 5ef14e63 2019-06-02 stsp if (error != NULL) {
6947 5ef14e63 2019-06-02 stsp struct got_reference *ref;
6948 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6949 5ef14e63 2019-06-02 stsp goto done;
6950 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6951 5ef14e63 2019-06-02 stsp if (error != NULL)
6952 5ef14e63 2019-06-02 stsp goto done;
6953 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
6954 5ef14e63 2019-06-02 stsp got_ref_close(ref);
6955 5ef14e63 2019-06-02 stsp if (error != NULL)
6956 5ef14e63 2019-06-02 stsp goto done;
6957 5ef14e63 2019-06-02 stsp }
6958 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
6959 5ef14e63 2019-06-02 stsp if (error)
6960 5ef14e63 2019-06-02 stsp goto done;
6961 5ef14e63 2019-06-02 stsp
6962 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
6963 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
6964 5ef14e63 2019-06-02 stsp if (error != NULL)
6965 5ef14e63 2019-06-02 stsp goto done;
6966 5ef14e63 2019-06-02 stsp
6967 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6968 5ef14e63 2019-06-02 stsp if (error)
6969 5ef14e63 2019-06-02 stsp goto done;
6970 5ef14e63 2019-06-02 stsp
6971 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6972 5ef14e63 2019-06-02 stsp if (error)
6973 5ef14e63 2019-06-02 stsp goto done;
6974 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6975 5ef14e63 2019-06-02 stsp if (pid == NULL) {
6976 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
6977 5ef14e63 2019-06-02 stsp goto done;
6978 5ef14e63 2019-06-02 stsp }
6979 5ef14e63 2019-06-02 stsp
6980 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6981 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6982 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
6983 5ef14e63 2019-06-02 stsp if (error != NULL)
6984 5ef14e63 2019-06-02 stsp goto done;
6985 5ef14e63 2019-06-02 stsp
6986 9627c110 2020-04-18 stsp if (upa.did_something)
6987 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
6988 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6989 5ef14e63 2019-06-02 stsp done:
6990 5ef14e63 2019-06-02 stsp if (commit)
6991 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
6992 5ef14e63 2019-06-02 stsp free(commit_id_str);
6993 5ef14e63 2019-06-02 stsp if (head_ref)
6994 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
6995 818c7501 2019-07-11 stsp if (worktree)
6996 818c7501 2019-07-11 stsp got_worktree_close(worktree);
6997 818c7501 2019-07-11 stsp if (repo)
6998 818c7501 2019-07-11 stsp got_repo_close(repo);
6999 818c7501 2019-07-11 stsp return error;
7000 818c7501 2019-07-11 stsp }
7001 818c7501 2019-07-11 stsp
7002 818c7501 2019-07-11 stsp __dead static void
7003 818c7501 2019-07-11 stsp usage_rebase(void)
7004 818c7501 2019-07-11 stsp {
7005 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7006 af54c8f8 2019-07-11 stsp getprogname());
7007 818c7501 2019-07-11 stsp exit(1);
7008 0ebf8283 2019-07-24 stsp }
7009 0ebf8283 2019-07-24 stsp
7010 0ebf8283 2019-07-24 stsp void
7011 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
7012 0ebf8283 2019-07-24 stsp {
7013 0ebf8283 2019-07-24 stsp char *nl;
7014 0ebf8283 2019-07-24 stsp size_t len;
7015 0ebf8283 2019-07-24 stsp
7016 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
7017 0ebf8283 2019-07-24 stsp if (len > limit)
7018 0ebf8283 2019-07-24 stsp len = limit;
7019 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
7020 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
7021 0ebf8283 2019-07-24 stsp if (nl)
7022 0ebf8283 2019-07-24 stsp *nl = '\0';
7023 0ebf8283 2019-07-24 stsp }
7024 0ebf8283 2019-07-24 stsp
7025 0ebf8283 2019-07-24 stsp static const struct got_error *
7026 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7027 0ebf8283 2019-07-24 stsp {
7028 5943eee2 2019-08-13 stsp const struct got_error *err;
7029 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
7030 5943eee2 2019-08-13 stsp const char *s;
7031 0ebf8283 2019-07-24 stsp
7032 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7033 5943eee2 2019-08-13 stsp if (err)
7034 5943eee2 2019-08-13 stsp return err;
7035 0ebf8283 2019-07-24 stsp
7036 5943eee2 2019-08-13 stsp s = logmsg0;
7037 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
7038 5943eee2 2019-08-13 stsp s++;
7039 5943eee2 2019-08-13 stsp
7040 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
7041 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
7042 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7043 5943eee2 2019-08-13 stsp goto done;
7044 5943eee2 2019-08-13 stsp }
7045 0ebf8283 2019-07-24 stsp
7046 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
7047 5943eee2 2019-08-13 stsp done:
7048 5943eee2 2019-08-13 stsp free(logmsg0);
7049 a0ea4fc0 2020-02-28 stsp return err;
7050 a0ea4fc0 2020-02-28 stsp }
7051 a0ea4fc0 2020-02-28 stsp
7052 a0ea4fc0 2020-02-28 stsp static const struct got_error *
7053 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7054 a0ea4fc0 2020-02-28 stsp {
7055 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
7056 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
7057 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
7058 a0ea4fc0 2020-02-28 stsp
7059 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
7060 a0ea4fc0 2020-02-28 stsp if (err)
7061 a0ea4fc0 2020-02-28 stsp return err;
7062 a0ea4fc0 2020-02-28 stsp
7063 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
7064 a0ea4fc0 2020-02-28 stsp if (err)
7065 a0ea4fc0 2020-02-28 stsp goto done;
7066 a0ea4fc0 2020-02-28 stsp
7067 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
7068 a0ea4fc0 2020-02-28 stsp
7069 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
7070 a0ea4fc0 2020-02-28 stsp if (err)
7071 a0ea4fc0 2020-02-28 stsp goto done;
7072 a0ea4fc0 2020-02-28 stsp
7073 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
7074 a0ea4fc0 2020-02-28 stsp done:
7075 a0ea4fc0 2020-02-28 stsp free(id_str);
7076 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
7077 a0ea4fc0 2020-02-28 stsp free(logmsg);
7078 5943eee2 2019-08-13 stsp return err;
7079 818c7501 2019-07-11 stsp }
7080 818c7501 2019-07-11 stsp
7081 818c7501 2019-07-11 stsp static const struct got_error *
7082 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
7083 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
7084 818c7501 2019-07-11 stsp {
7085 818c7501 2019-07-11 stsp const struct got_error *err;
7086 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7087 818c7501 2019-07-11 stsp
7088 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
7089 818c7501 2019-07-11 stsp if (err)
7090 818c7501 2019-07-11 stsp goto done;
7091 818c7501 2019-07-11 stsp
7092 ff0d2220 2019-07-11 stsp if (new_id) {
7093 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
7094 ff0d2220 2019-07-11 stsp if (err)
7095 ff0d2220 2019-07-11 stsp goto done;
7096 ff0d2220 2019-07-11 stsp }
7097 818c7501 2019-07-11 stsp
7098 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
7099 ff0d2220 2019-07-11 stsp if (new_id_str)
7100 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
7101 0ebf8283 2019-07-24 stsp
7102 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7103 0ebf8283 2019-07-24 stsp if (err)
7104 0ebf8283 2019-07-24 stsp goto done;
7105 0ebf8283 2019-07-24 stsp
7106 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
7107 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7108 818c7501 2019-07-11 stsp done:
7109 818c7501 2019-07-11 stsp free(old_id_str);
7110 818c7501 2019-07-11 stsp free(new_id_str);
7111 272a1371 2020-02-28 stsp free(logmsg);
7112 818c7501 2019-07-11 stsp return err;
7113 818c7501 2019-07-11 stsp }
7114 818c7501 2019-07-11 stsp
7115 1ee397ad 2019-07-12 stsp static const struct got_error *
7116 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7117 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
7118 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
7119 818c7501 2019-07-11 stsp {
7120 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
7121 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
7122 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
7123 818c7501 2019-07-11 stsp }
7124 818c7501 2019-07-11 stsp
7125 818c7501 2019-07-11 stsp static const struct got_error *
7126 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
7127 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7128 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
7129 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7130 ff0d2220 2019-07-11 stsp {
7131 ff0d2220 2019-07-11 stsp const struct got_error *error;
7132 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
7133 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
7134 ff0d2220 2019-07-11 stsp
7135 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7136 ff0d2220 2019-07-11 stsp if (error)
7137 ff0d2220 2019-07-11 stsp return error;
7138 ff0d2220 2019-07-11 stsp
7139 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7140 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
7141 ff0d2220 2019-07-11 stsp if (error) {
7142 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7143 ff0d2220 2019-07-11 stsp goto done;
7144 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
7145 ff0d2220 2019-07-11 stsp } else {
7146 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
7147 ff0d2220 2019-07-11 stsp free(new_commit_id);
7148 ff0d2220 2019-07-11 stsp }
7149 ff0d2220 2019-07-11 stsp done:
7150 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
7151 ff0d2220 2019-07-11 stsp return error;
7152 64c6d990 2019-07-11 stsp }
7153 64c6d990 2019-07-11 stsp
7154 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
7155 64c6d990 2019-07-11 stsp const char *path_prefix;
7156 64c6d990 2019-07-11 stsp size_t len;
7157 8ca9bd68 2019-07-25 stsp int errcode;
7158 64c6d990 2019-07-11 stsp };
7159 64c6d990 2019-07-11 stsp
7160 64c6d990 2019-07-11 stsp static const struct got_error *
7161 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7162 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
7163 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
7164 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
7165 64c6d990 2019-07-11 stsp {
7166 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
7167 64c6d990 2019-07-11 stsp
7168 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7169 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7170 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
7171 64c6d990 2019-07-11 stsp
7172 64c6d990 2019-07-11 stsp return NULL;
7173 64c6d990 2019-07-11 stsp }
7174 64c6d990 2019-07-11 stsp
7175 64c6d990 2019-07-11 stsp static const struct got_error *
7176 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
7177 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
7178 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
7179 64c6d990 2019-07-11 stsp {
7180 64c6d990 2019-07-11 stsp const struct got_error *err;
7181 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7182 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7183 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
7184 64c6d990 2019-07-11 stsp
7185 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
7186 64c6d990 2019-07-11 stsp return NULL;
7187 64c6d990 2019-07-11 stsp
7188 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7189 64c6d990 2019-07-11 stsp if (err)
7190 64c6d990 2019-07-11 stsp goto done;
7191 64c6d990 2019-07-11 stsp
7192 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7193 64c6d990 2019-07-11 stsp if (err)
7194 64c6d990 2019-07-11 stsp goto done;
7195 64c6d990 2019-07-11 stsp
7196 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
7197 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
7198 64c6d990 2019-07-11 stsp if (err)
7199 64c6d990 2019-07-11 stsp goto done;
7200 64c6d990 2019-07-11 stsp
7201 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
7202 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
7203 64c6d990 2019-07-11 stsp if (err)
7204 64c6d990 2019-07-11 stsp goto done;
7205 64c6d990 2019-07-11 stsp
7206 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
7207 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
7208 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
7209 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7210 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
7211 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7212 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7213 64c6d990 2019-07-11 stsp done:
7214 64c6d990 2019-07-11 stsp if (tree1)
7215 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
7216 64c6d990 2019-07-11 stsp if (tree2)
7217 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
7218 64c6d990 2019-07-11 stsp if (commit)
7219 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
7220 64c6d990 2019-07-11 stsp if (parent_commit)
7221 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
7222 64c6d990 2019-07-11 stsp return err;
7223 ff0d2220 2019-07-11 stsp }
7224 ff0d2220 2019-07-11 stsp
7225 ff0d2220 2019-07-11 stsp static const struct got_error *
7226 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
7227 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
7228 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7229 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
7230 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
7231 af61c510 2019-07-19 stsp {
7232 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
7233 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
7234 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
7235 af61c510 2019-07-19 stsp struct got_object_qid *qid;
7236 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
7237 af61c510 2019-07-19 stsp
7238 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
7239 af61c510 2019-07-19 stsp if (err)
7240 af61c510 2019-07-19 stsp return err;
7241 af61c510 2019-07-19 stsp
7242 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7243 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7244 af61c510 2019-07-19 stsp if (err)
7245 af61c510 2019-07-19 stsp goto done;
7246 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7247 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7248 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
7249 af61c510 2019-07-19 stsp if (err) {
7250 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7251 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7252 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
7253 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
7254 af61c510 2019-07-19 stsp "been reached?!?");
7255 ee780d5c 2020-01-04 stsp }
7256 ee780d5c 2020-01-04 stsp goto done;
7257 af61c510 2019-07-19 stsp } else {
7258 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
7259 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
7260 af61c510 2019-07-19 stsp if (err)
7261 af61c510 2019-07-19 stsp goto done;
7262 af61c510 2019-07-19 stsp
7263 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
7264 af61c510 2019-07-19 stsp if (err)
7265 af61c510 2019-07-19 stsp goto done;
7266 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7267 af61c510 2019-07-19 stsp commit_id = parent_id;
7268 af61c510 2019-07-19 stsp }
7269 af61c510 2019-07-19 stsp }
7270 af61c510 2019-07-19 stsp done:
7271 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
7272 af61c510 2019-07-19 stsp return err;
7273 af61c510 2019-07-19 stsp }
7274 af61c510 2019-07-19 stsp
7275 af61c510 2019-07-19 stsp static const struct got_error *
7276 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
7277 818c7501 2019-07-11 stsp {
7278 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
7279 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
7280 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
7281 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7282 818c7501 2019-07-11 stsp char *cwd = NULL;
7283 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
7284 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7285 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
7286 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
7287 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7288 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
7289 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7290 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
7291 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7292 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
7293 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
7294 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
7295 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
7296 818c7501 2019-07-11 stsp
7297 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
7298 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
7299 818c7501 2019-07-11 stsp
7300 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
7301 818c7501 2019-07-11 stsp switch (ch) {
7302 818c7501 2019-07-11 stsp case 'a':
7303 818c7501 2019-07-11 stsp abort_rebase = 1;
7304 818c7501 2019-07-11 stsp break;
7305 818c7501 2019-07-11 stsp case 'c':
7306 818c7501 2019-07-11 stsp continue_rebase = 1;
7307 818c7501 2019-07-11 stsp break;
7308 818c7501 2019-07-11 stsp default:
7309 818c7501 2019-07-11 stsp usage_rebase();
7310 818c7501 2019-07-11 stsp /* NOTREACHED */
7311 818c7501 2019-07-11 stsp }
7312 818c7501 2019-07-11 stsp }
7313 818c7501 2019-07-11 stsp
7314 818c7501 2019-07-11 stsp argc -= optind;
7315 818c7501 2019-07-11 stsp argv += optind;
7316 818c7501 2019-07-11 stsp
7317 43012d58 2019-07-14 stsp #ifndef PROFILE
7318 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7319 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7320 43012d58 2019-07-14 stsp err(1, "pledge");
7321 43012d58 2019-07-14 stsp #endif
7322 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
7323 818c7501 2019-07-11 stsp usage_rebase();
7324 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
7325 818c7501 2019-07-11 stsp if (argc != 0)
7326 818c7501 2019-07-11 stsp usage_rebase();
7327 818c7501 2019-07-11 stsp } else if (argc != 1)
7328 818c7501 2019-07-11 stsp usage_rebase();
7329 818c7501 2019-07-11 stsp
7330 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
7331 818c7501 2019-07-11 stsp if (cwd == NULL) {
7332 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
7333 818c7501 2019-07-11 stsp goto done;
7334 818c7501 2019-07-11 stsp }
7335 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
7336 fa51e947 2020-03-27 stsp if (error) {
7337 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7338 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "rebase", cwd);
7339 818c7501 2019-07-11 stsp goto done;
7340 fa51e947 2020-03-27 stsp }
7341 818c7501 2019-07-11 stsp
7342 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7343 c9956ddf 2019-09-08 stsp NULL);
7344 818c7501 2019-07-11 stsp if (error != NULL)
7345 818c7501 2019-07-11 stsp goto done;
7346 818c7501 2019-07-11 stsp
7347 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7348 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7349 7ef62c4e 2020-02-24 stsp if (error)
7350 7ef62c4e 2020-02-24 stsp goto done;
7351 7ef62c4e 2020-02-24 stsp
7352 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7353 7ef62c4e 2020-02-24 stsp worktree);
7354 818c7501 2019-07-11 stsp if (error)
7355 7ef62c4e 2020-02-24 stsp goto done;
7356 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
7357 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7358 818c7501 2019-07-11 stsp goto done;
7359 7ef62c4e 2020-02-24 stsp }
7360 818c7501 2019-07-11 stsp
7361 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7362 818c7501 2019-07-11 stsp if (error)
7363 818c7501 2019-07-11 stsp goto done;
7364 818c7501 2019-07-11 stsp
7365 f6794adc 2019-07-23 stsp if (abort_rebase) {
7366 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7367 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7368 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7369 f6794adc 2019-07-23 stsp goto done;
7370 f6794adc 2019-07-23 stsp }
7371 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7372 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7373 3e3a69f1 2019-07-25 stsp worktree, repo);
7374 818c7501 2019-07-11 stsp if (error)
7375 818c7501 2019-07-11 stsp goto done;
7376 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
7377 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
7378 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7379 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
7380 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
7381 818c7501 2019-07-11 stsp if (error)
7382 818c7501 2019-07-11 stsp goto done;
7383 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7384 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7385 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
7386 818c7501 2019-07-11 stsp }
7387 818c7501 2019-07-11 stsp
7388 818c7501 2019-07-11 stsp if (continue_rebase) {
7389 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7390 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7391 f6794adc 2019-07-23 stsp goto done;
7392 f6794adc 2019-07-23 stsp }
7393 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7394 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7395 3e3a69f1 2019-07-25 stsp worktree, repo);
7396 818c7501 2019-07-11 stsp if (error)
7397 818c7501 2019-07-11 stsp goto done;
7398 818c7501 2019-07-11 stsp
7399 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7400 01757395 2019-07-12 stsp resume_commit_id, repo);
7401 818c7501 2019-07-11 stsp if (error)
7402 818c7501 2019-07-11 stsp goto done;
7403 818c7501 2019-07-11 stsp
7404 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
7405 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
7406 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
7407 818c7501 2019-07-11 stsp goto done;
7408 818c7501 2019-07-11 stsp }
7409 818c7501 2019-07-11 stsp } else {
7410 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
7411 818c7501 2019-07-11 stsp if (error != NULL)
7412 818c7501 2019-07-11 stsp goto done;
7413 ff0d2220 2019-07-11 stsp }
7414 818c7501 2019-07-11 stsp
7415 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7416 ff0d2220 2019-07-11 stsp if (error)
7417 ff0d2220 2019-07-11 stsp goto done;
7418 ff0d2220 2019-07-11 stsp
7419 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
7420 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
7421 a51a74b3 2019-07-27 stsp
7422 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
7423 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7424 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
7425 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7426 818c7501 2019-07-11 stsp if (error)
7427 818c7501 2019-07-11 stsp goto done;
7428 818c7501 2019-07-11 stsp if (yca_id == NULL) {
7429 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
7430 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
7431 818c7501 2019-07-11 stsp "with work tree's branch");
7432 818c7501 2019-07-11 stsp goto done;
7433 818c7501 2019-07-11 stsp }
7434 818c7501 2019-07-11 stsp
7435 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
7436 a51a74b3 2019-07-27 stsp if (error) {
7437 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
7438 a51a74b3 2019-07-27 stsp goto done;
7439 a51a74b3 2019-07-27 stsp error = NULL;
7440 a51a74b3 2019-07-27 stsp } else {
7441 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7442 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
7443 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
7444 a51a74b3 2019-07-27 stsp goto done;
7445 a51a74b3 2019-07-27 stsp }
7446 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
7447 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
7448 818c7501 2019-07-11 stsp if (error)
7449 818c7501 2019-07-11 stsp goto done;
7450 818c7501 2019-07-11 stsp }
7451 818c7501 2019-07-11 stsp
7452 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
7453 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7454 818c7501 2019-07-11 stsp if (error)
7455 818c7501 2019-07-11 stsp goto done;
7456 818c7501 2019-07-11 stsp
7457 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7458 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
7459 fc66b545 2019-08-12 stsp if (pid == NULL) {
7460 fc66b545 2019-08-12 stsp if (!continue_rebase) {
7461 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7462 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7463 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
7464 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
7465 fc66b545 2019-08-12 stsp if (error)
7466 fc66b545 2019-08-12 stsp goto done;
7467 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
7468 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
7469 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7470 9627c110 2020-04-18 stsp
7471 fc66b545 2019-08-12 stsp }
7472 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
7473 fc66b545 2019-08-12 stsp goto done;
7474 fc66b545 2019-08-12 stsp }
7475 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
7476 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
7477 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
7478 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7479 818c7501 2019-07-11 stsp commit = NULL;
7480 818c7501 2019-07-11 stsp if (error)
7481 818c7501 2019-07-11 stsp goto done;
7482 64c6d990 2019-07-11 stsp
7483 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
7484 38b0338b 2019-11-29 stsp if (continue_rebase) {
7485 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
7486 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
7487 38b0338b 2019-11-29 stsp goto done;
7488 38b0338b 2019-11-29 stsp } else {
7489 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
7490 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
7491 38b0338b 2019-11-29 stsp char *id_str;
7492 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
7493 38b0338b 2019-11-29 stsp new_base_branch);
7494 38b0338b 2019-11-29 stsp if (error)
7495 38b0338b 2019-11-29 stsp goto done;
7496 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
7497 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
7498 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
7499 38b0338b 2019-11-29 stsp free(id_str);
7500 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
7501 38b0338b 2019-11-29 stsp new_head_commit_id);
7502 38b0338b 2019-11-29 stsp if (error)
7503 38b0338b 2019-11-29 stsp goto done;
7504 38b0338b 2019-11-29 stsp }
7505 818c7501 2019-07-11 stsp }
7506 818c7501 2019-07-11 stsp
7507 818c7501 2019-07-11 stsp pid = NULL;
7508 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
7509 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7510 9627c110 2020-04-18 stsp
7511 818c7501 2019-07-11 stsp commit_id = qid->id;
7512 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
7513 818c7501 2019-07-11 stsp pid = qid;
7514 818c7501 2019-07-11 stsp
7515 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7516 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
7517 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
7518 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7519 818c7501 2019-07-11 stsp if (error)
7520 818c7501 2019-07-11 stsp goto done;
7521 9627c110 2020-04-18 stsp
7522 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7523 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
7524 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
7525 818c7501 2019-07-11 stsp
7526 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7527 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
7528 a0ea4fc0 2020-02-28 stsp if (error)
7529 a0ea4fc0 2020-02-28 stsp goto done;
7530 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7531 818c7501 2019-07-11 stsp break;
7532 01757395 2019-07-12 stsp }
7533 818c7501 2019-07-11 stsp
7534 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7535 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
7536 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7537 818c7501 2019-07-11 stsp if (error)
7538 818c7501 2019-07-11 stsp goto done;
7539 818c7501 2019-07-11 stsp }
7540 818c7501 2019-07-11 stsp
7541 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7542 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7543 818c7501 2019-07-11 stsp if (error)
7544 818c7501 2019-07-11 stsp goto done;
7545 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7546 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
7547 818c7501 2019-07-11 stsp } else
7548 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
7549 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
7550 818c7501 2019-07-11 stsp done:
7551 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
7552 818c7501 2019-07-11 stsp free(branch_head_commit_id);
7553 818c7501 2019-07-11 stsp free(resume_commit_id);
7554 818c7501 2019-07-11 stsp free(yca_id);
7555 818c7501 2019-07-11 stsp if (commit)
7556 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7557 818c7501 2019-07-11 stsp if (branch)
7558 818c7501 2019-07-11 stsp got_ref_close(branch);
7559 818c7501 2019-07-11 stsp if (new_base_branch)
7560 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
7561 818c7501 2019-07-11 stsp if (tmp_branch)
7562 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
7563 5ef14e63 2019-06-02 stsp if (worktree)
7564 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
7565 5ef14e63 2019-06-02 stsp if (repo)
7566 5ef14e63 2019-06-02 stsp got_repo_close(repo);
7567 5ef14e63 2019-06-02 stsp return error;
7568 0ebf8283 2019-07-24 stsp }
7569 0ebf8283 2019-07-24 stsp
7570 0ebf8283 2019-07-24 stsp __dead static void
7571 0ebf8283 2019-07-24 stsp usage_histedit(void)
7572 0ebf8283 2019-07-24 stsp {
7573 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7574 0ebf8283 2019-07-24 stsp getprogname());
7575 0ebf8283 2019-07-24 stsp exit(1);
7576 0ebf8283 2019-07-24 stsp }
7577 0ebf8283 2019-07-24 stsp
7578 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
7579 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
7580 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
7581 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
7582 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
7583 0ebf8283 2019-07-24 stsp
7584 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
7585 0ebf8283 2019-07-24 stsp unsigned char code;
7586 0ebf8283 2019-07-24 stsp const char *name;
7587 0ebf8283 2019-07-24 stsp const char *desc;
7588 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
7589 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7590 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7591 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7592 82997472 2020-01-29 stsp "be used" },
7593 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7594 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
7595 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
7596 0ebf8283 2019-07-24 stsp };
7597 0ebf8283 2019-07-24 stsp
7598 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
7599 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7600 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
7601 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7602 0ebf8283 2019-07-24 stsp char *logmsg;
7603 0ebf8283 2019-07-24 stsp };
7604 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7605 0ebf8283 2019-07-24 stsp
7606 0ebf8283 2019-07-24 stsp static const struct got_error *
7607 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7608 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7609 0ebf8283 2019-07-24 stsp {
7610 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7611 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
7612 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7613 8138f3e1 2019-08-11 stsp int n;
7614 0ebf8283 2019-07-24 stsp
7615 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7616 0ebf8283 2019-07-24 stsp if (err)
7617 0ebf8283 2019-07-24 stsp goto done;
7618 0ebf8283 2019-07-24 stsp
7619 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
7620 0ebf8283 2019-07-24 stsp if (err)
7621 0ebf8283 2019-07-24 stsp goto done;
7622 0ebf8283 2019-07-24 stsp
7623 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
7624 0ebf8283 2019-07-24 stsp if (err)
7625 0ebf8283 2019-07-24 stsp goto done;
7626 0ebf8283 2019-07-24 stsp
7627 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7628 0ebf8283 2019-07-24 stsp if (n < 0)
7629 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7630 0ebf8283 2019-07-24 stsp done:
7631 0ebf8283 2019-07-24 stsp if (commit)
7632 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7633 0ebf8283 2019-07-24 stsp free(id_str);
7634 0ebf8283 2019-07-24 stsp free(logmsg);
7635 0ebf8283 2019-07-24 stsp return err;
7636 0ebf8283 2019-07-24 stsp }
7637 0ebf8283 2019-07-24 stsp
7638 0ebf8283 2019-07-24 stsp static const struct got_error *
7639 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7640 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
7641 0ebf8283 2019-07-24 stsp {
7642 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7643 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
7644 0ebf8283 2019-07-24 stsp
7645 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
7646 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7647 0ebf8283 2019-07-24 stsp
7648 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7649 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7650 0ebf8283 2019-07-24 stsp f, repo);
7651 0ebf8283 2019-07-24 stsp if (err)
7652 0ebf8283 2019-07-24 stsp break;
7653 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7654 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7655 083957f4 2020-02-24 stsp if (n < 0) {
7656 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
7657 083957f4 2020-02-24 stsp break;
7658 083957f4 2020-02-24 stsp }
7659 083957f4 2020-02-24 stsp }
7660 0ebf8283 2019-07-24 stsp }
7661 0ebf8283 2019-07-24 stsp
7662 0ebf8283 2019-07-24 stsp return err;
7663 0ebf8283 2019-07-24 stsp }
7664 0ebf8283 2019-07-24 stsp
7665 0ebf8283 2019-07-24 stsp static const struct got_error *
7666 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
7667 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
7668 0ebf8283 2019-07-24 stsp {
7669 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7670 0ebf8283 2019-07-24 stsp int n, i;
7671 514f2ffe 2020-01-29 stsp char *id_str;
7672 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
7673 514f2ffe 2020-01-29 stsp
7674 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
7675 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
7676 514f2ffe 2020-01-29 stsp if (err)
7677 514f2ffe 2020-01-29 stsp return err;
7678 514f2ffe 2020-01-29 stsp
7679 514f2ffe 2020-01-29 stsp n = fprintf(f,
7680 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
7681 514f2ffe 2020-01-29 stsp "# commit %s\n"
7682 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
7683 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
7684 514f2ffe 2020-01-29 stsp if (n < 0) {
7685 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7686 514f2ffe 2020-01-29 stsp goto done;
7687 514f2ffe 2020-01-29 stsp }
7688 0ebf8283 2019-07-24 stsp
7689 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
7690 514f2ffe 2020-01-29 stsp if (n < 0) {
7691 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7692 514f2ffe 2020-01-29 stsp goto done;
7693 514f2ffe 2020-01-29 stsp }
7694 0ebf8283 2019-07-24 stsp
7695 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7696 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7697 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7698 0ebf8283 2019-07-24 stsp cmd->desc);
7699 0ebf8283 2019-07-24 stsp if (n < 0) {
7700 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7701 0ebf8283 2019-07-24 stsp break;
7702 0ebf8283 2019-07-24 stsp }
7703 0ebf8283 2019-07-24 stsp }
7704 514f2ffe 2020-01-29 stsp done:
7705 514f2ffe 2020-01-29 stsp free(id_str);
7706 0ebf8283 2019-07-24 stsp return err;
7707 0ebf8283 2019-07-24 stsp }
7708 0ebf8283 2019-07-24 stsp
7709 0ebf8283 2019-07-24 stsp static const struct got_error *
7710 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
7711 0ebf8283 2019-07-24 stsp {
7712 0ebf8283 2019-07-24 stsp static char msg[42];
7713 0ebf8283 2019-07-24 stsp int ret;
7714 0ebf8283 2019-07-24 stsp
7715 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7716 0ebf8283 2019-07-24 stsp lineno);
7717 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
7718 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7719 0ebf8283 2019-07-24 stsp
7720 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7721 0ebf8283 2019-07-24 stsp }
7722 0ebf8283 2019-07-24 stsp
7723 0ebf8283 2019-07-24 stsp static const struct got_error *
7724 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7725 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
7726 0ebf8283 2019-07-24 stsp {
7727 0ebf8283 2019-07-24 stsp const struct got_error *err;
7728 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
7729 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
7730 0ebf8283 2019-07-24 stsp
7731 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7732 0ebf8283 2019-07-24 stsp if (err)
7733 0ebf8283 2019-07-24 stsp return err;
7734 0ebf8283 2019-07-24 stsp
7735 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7736 0ebf8283 2019-07-24 stsp if (err)
7737 0ebf8283 2019-07-24 stsp goto done;
7738 0ebf8283 2019-07-24 stsp
7739 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7740 5943eee2 2019-08-13 stsp if (err)
7741 5943eee2 2019-08-13 stsp goto done;
7742 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7743 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7744 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
7745 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7746 0ebf8283 2019-07-24 stsp }
7747 0ebf8283 2019-07-24 stsp done:
7748 0ebf8283 2019-07-24 stsp if (folded_commit)
7749 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
7750 0ebf8283 2019-07-24 stsp free(id_str);
7751 5943eee2 2019-08-13 stsp free(folded_logmsg);
7752 0ebf8283 2019-07-24 stsp return err;
7753 0ebf8283 2019-07-24 stsp }
7754 0ebf8283 2019-07-24 stsp
7755 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
7756 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7757 0ebf8283 2019-07-24 stsp {
7758 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7759 0ebf8283 2019-07-24 stsp
7760 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7761 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7762 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7763 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7764 3f9de99f 2019-07-24 stsp folded = prev;
7765 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7766 0ebf8283 2019-07-24 stsp }
7767 0ebf8283 2019-07-24 stsp
7768 0ebf8283 2019-07-24 stsp return folded;
7769 0ebf8283 2019-07-24 stsp }
7770 0ebf8283 2019-07-24 stsp
7771 0ebf8283 2019-07-24 stsp static const struct got_error *
7772 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7773 0ebf8283 2019-07-24 stsp struct got_repository *repo)
7774 0ebf8283 2019-07-24 stsp {
7775 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7776 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7777 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7778 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7779 1601cb9f 2020-09-11 naddy int logmsg_len;
7780 0ebf8283 2019-07-24 stsp int fd;
7781 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
7782 0ebf8283 2019-07-24 stsp
7783 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7784 0ebf8283 2019-07-24 stsp if (err)
7785 0ebf8283 2019-07-24 stsp return err;
7786 0ebf8283 2019-07-24 stsp
7787 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
7788 0ebf8283 2019-07-24 stsp if (folded) {
7789 0ebf8283 2019-07-24 stsp while (folded != hle) {
7790 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7791 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7792 3f9de99f 2019-07-24 stsp continue;
7793 3f9de99f 2019-07-24 stsp }
7794 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
7795 0ebf8283 2019-07-24 stsp logmsg, repo);
7796 0ebf8283 2019-07-24 stsp if (err)
7797 0ebf8283 2019-07-24 stsp goto done;
7798 0ebf8283 2019-07-24 stsp free(logmsg);
7799 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7800 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7801 0ebf8283 2019-07-24 stsp }
7802 0ebf8283 2019-07-24 stsp }
7803 0ebf8283 2019-07-24 stsp
7804 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7805 0ebf8283 2019-07-24 stsp if (err)
7806 0ebf8283 2019-07-24 stsp goto done;
7807 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7808 5943eee2 2019-08-13 stsp if (err)
7809 5943eee2 2019-08-13 stsp goto done;
7810 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
7811 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
7812 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
7813 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
7814 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7815 0ebf8283 2019-07-24 stsp goto done;
7816 0ebf8283 2019-07-24 stsp }
7817 0ebf8283 2019-07-24 stsp free(logmsg);
7818 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7819 0ebf8283 2019-07-24 stsp
7820 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7821 0ebf8283 2019-07-24 stsp if (err)
7822 0ebf8283 2019-07-24 stsp goto done;
7823 0ebf8283 2019-07-24 stsp
7824 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7825 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
7826 0ebf8283 2019-07-24 stsp if (err)
7827 0ebf8283 2019-07-24 stsp goto done;
7828 0ebf8283 2019-07-24 stsp
7829 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
7830 0ebf8283 2019-07-24 stsp close(fd);
7831 0ebf8283 2019-07-24 stsp
7832 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7833 0ebf8283 2019-07-24 stsp if (err)
7834 0ebf8283 2019-07-24 stsp goto done;
7835 0ebf8283 2019-07-24 stsp
7836 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7837 0ebf8283 2019-07-24 stsp if (err) {
7838 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7839 0ebf8283 2019-07-24 stsp goto done;
7840 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7841 0ebf8283 2019-07-24 stsp }
7842 0ebf8283 2019-07-24 stsp done:
7843 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7844 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
7845 0ebf8283 2019-07-24 stsp free(logmsg_path);
7846 0ebf8283 2019-07-24 stsp free(logmsg);
7847 5943eee2 2019-08-13 stsp free(orig_logmsg);
7848 0ebf8283 2019-07-24 stsp free(editor);
7849 0ebf8283 2019-07-24 stsp if (commit)
7850 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7851 0ebf8283 2019-07-24 stsp return err;
7852 5ef14e63 2019-06-02 stsp }
7853 0ebf8283 2019-07-24 stsp
7854 0ebf8283 2019-07-24 stsp static const struct got_error *
7855 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
7856 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7857 0ebf8283 2019-07-24 stsp {
7858 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7859 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
7860 0ebf8283 2019-07-24 stsp size_t size;
7861 0ebf8283 2019-07-24 stsp ssize_t len;
7862 0ebf8283 2019-07-24 stsp int lineno = 0, i;
7863 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7864 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7865 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
7866 0ebf8283 2019-07-24 stsp
7867 0ebf8283 2019-07-24 stsp for (;;) {
7868 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
7869 0ebf8283 2019-07-24 stsp if (len == -1) {
7870 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
7871 0ebf8283 2019-07-24 stsp if (feof(f))
7872 0ebf8283 2019-07-24 stsp break;
7873 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
7874 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
7875 0ebf8283 2019-07-24 stsp break;
7876 0ebf8283 2019-07-24 stsp }
7877 0ebf8283 2019-07-24 stsp lineno++;
7878 0ebf8283 2019-07-24 stsp p = line;
7879 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7880 0ebf8283 2019-07-24 stsp p++;
7881 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
7882 0ebf8283 2019-07-24 stsp free(line);
7883 0ebf8283 2019-07-24 stsp line = NULL;
7884 0ebf8283 2019-07-24 stsp continue;
7885 0ebf8283 2019-07-24 stsp }
7886 0ebf8283 2019-07-24 stsp cmd = NULL;
7887 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7888 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
7889 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7890 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
7891 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
7892 0ebf8283 2019-07-24 stsp break;
7893 0ebf8283 2019-07-24 stsp }
7894 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7895 0ebf8283 2019-07-24 stsp p++;
7896 0ebf8283 2019-07-24 stsp break;
7897 0ebf8283 2019-07-24 stsp }
7898 0ebf8283 2019-07-24 stsp }
7899 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
7900 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7901 0ebf8283 2019-07-24 stsp break;
7902 0ebf8283 2019-07-24 stsp }
7903 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7904 0ebf8283 2019-07-24 stsp p++;
7905 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
7906 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
7907 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
7908 0ebf8283 2019-07-24 stsp break;
7909 0ebf8283 2019-07-24 stsp }
7910 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
7911 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7912 0ebf8283 2019-07-24 stsp if (err)
7913 0ebf8283 2019-07-24 stsp break;
7914 0ebf8283 2019-07-24 stsp } else {
7915 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
7916 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
7917 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7918 0ebf8283 2019-07-24 stsp break;
7919 0ebf8283 2019-07-24 stsp }
7920 0ebf8283 2019-07-24 stsp }
7921 0ebf8283 2019-07-24 stsp free(line);
7922 0ebf8283 2019-07-24 stsp line = NULL;
7923 0ebf8283 2019-07-24 stsp continue;
7924 0ebf8283 2019-07-24 stsp } else {
7925 0ebf8283 2019-07-24 stsp end = p;
7926 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
7927 0ebf8283 2019-07-24 stsp end++;
7928 0ebf8283 2019-07-24 stsp *end = '\0';
7929 0ebf8283 2019-07-24 stsp
7930 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
7931 0ebf8283 2019-07-24 stsp if (err) {
7932 0ebf8283 2019-07-24 stsp /* override error code */
7933 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7934 0ebf8283 2019-07-24 stsp break;
7935 0ebf8283 2019-07-24 stsp }
7936 0ebf8283 2019-07-24 stsp }
7937 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
7938 0ebf8283 2019-07-24 stsp if (hle == NULL) {
7939 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
7940 0ebf8283 2019-07-24 stsp break;
7941 0ebf8283 2019-07-24 stsp }
7942 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
7943 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
7944 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
7945 0ebf8283 2019-07-24 stsp commit_id = NULL;
7946 0ebf8283 2019-07-24 stsp free(line);
7947 0ebf8283 2019-07-24 stsp line = NULL;
7948 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7949 0ebf8283 2019-07-24 stsp }
7950 0ebf8283 2019-07-24 stsp
7951 0ebf8283 2019-07-24 stsp free(line);
7952 0ebf8283 2019-07-24 stsp free(commit_id);
7953 0ebf8283 2019-07-24 stsp return err;
7954 0ebf8283 2019-07-24 stsp }
7955 0ebf8283 2019-07-24 stsp
7956 0ebf8283 2019-07-24 stsp static const struct got_error *
7957 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
7958 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
7959 bfce7f83 2019-07-27 stsp {
7960 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
7961 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
7962 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7963 5b87815e 2020-03-05 stsp static char msg[92];
7964 bfce7f83 2019-07-27 stsp char *id_str;
7965 bfce7f83 2019-07-27 stsp
7966 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
7967 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7968 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
7969 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
7970 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7971 5b87815e 2020-03-05 stsp
7972 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7973 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
7974 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7975 5b87815e 2020-03-05 stsp if (hle == hle2)
7976 5b87815e 2020-03-05 stsp continue;
7977 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
7978 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
7979 5b87815e 2020-03-05 stsp continue;
7980 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
7981 5b87815e 2020-03-05 stsp if (err)
7982 5b87815e 2020-03-05 stsp return err;
7983 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
7984 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
7985 5b87815e 2020-03-05 stsp free(id_str);
7986 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7987 5b87815e 2020-03-05 stsp }
7988 5b87815e 2020-03-05 stsp }
7989 bfce7f83 2019-07-27 stsp
7990 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7991 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7992 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7993 bfce7f83 2019-07-27 stsp break;
7994 bfce7f83 2019-07-27 stsp }
7995 bfce7f83 2019-07-27 stsp if (hle == NULL) {
7996 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
7997 bfce7f83 2019-07-27 stsp if (err)
7998 bfce7f83 2019-07-27 stsp return err;
7999 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
8000 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
8001 bfce7f83 2019-07-27 stsp free(id_str);
8002 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8003 bfce7f83 2019-07-27 stsp }
8004 bfce7f83 2019-07-27 stsp }
8005 bfce7f83 2019-07-27 stsp
8006 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8007 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8008 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8009 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
8010 bfce7f83 2019-07-27 stsp
8011 bfce7f83 2019-07-27 stsp return NULL;
8012 bfce7f83 2019-07-27 stsp }
8013 bfce7f83 2019-07-27 stsp
8014 bfce7f83 2019-07-27 stsp static const struct got_error *
8015 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
8016 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
8017 bfce7f83 2019-07-27 stsp struct got_repository *repo)
8018 0ebf8283 2019-07-24 stsp {
8019 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8020 0ebf8283 2019-07-24 stsp char *editor;
8021 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8022 0ebf8283 2019-07-24 stsp
8023 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8024 0ebf8283 2019-07-24 stsp if (err)
8025 0ebf8283 2019-07-24 stsp return err;
8026 0ebf8283 2019-07-24 stsp
8027 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
8028 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
8029 0ebf8283 2019-07-24 stsp goto done;
8030 0ebf8283 2019-07-24 stsp }
8031 0ebf8283 2019-07-24 stsp
8032 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8033 0ebf8283 2019-07-24 stsp if (f == NULL) {
8034 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
8035 0ebf8283 2019-07-24 stsp goto done;
8036 0ebf8283 2019-07-24 stsp }
8037 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8038 bfce7f83 2019-07-27 stsp if (err)
8039 bfce7f83 2019-07-27 stsp goto done;
8040 bfce7f83 2019-07-27 stsp
8041 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
8042 0ebf8283 2019-07-24 stsp done:
8043 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8044 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8045 0ebf8283 2019-07-24 stsp free(editor);
8046 0ebf8283 2019-07-24 stsp return err;
8047 0ebf8283 2019-07-24 stsp }
8048 0ebf8283 2019-07-24 stsp
8049 0ebf8283 2019-07-24 stsp static const struct got_error *
8050 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8051 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
8052 514f2ffe 2020-01-29 stsp struct got_repository *);
8053 0ebf8283 2019-07-24 stsp
8054 0ebf8283 2019-07-24 stsp static const struct got_error *
8055 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
8056 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
8057 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
8058 0ebf8283 2019-07-24 stsp {
8059 0ebf8283 2019-07-24 stsp const struct got_error *err;
8060 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8061 0ebf8283 2019-07-24 stsp char *path = NULL;
8062 0ebf8283 2019-07-24 stsp
8063 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
8064 0ebf8283 2019-07-24 stsp if (err)
8065 0ebf8283 2019-07-24 stsp return err;
8066 0ebf8283 2019-07-24 stsp
8067 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
8068 0ebf8283 2019-07-24 stsp if (err)
8069 0ebf8283 2019-07-24 stsp goto done;
8070 0ebf8283 2019-07-24 stsp
8071 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
8072 0ebf8283 2019-07-24 stsp if (err)
8073 0ebf8283 2019-07-24 stsp goto done;
8074 0ebf8283 2019-07-24 stsp
8075 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
8076 083957f4 2020-02-24 stsp rewind(f);
8077 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8078 083957f4 2020-02-24 stsp } else {
8079 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
8080 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
8081 0ebf8283 2019-07-24 stsp goto done;
8082 083957f4 2020-02-24 stsp }
8083 083957f4 2020-02-24 stsp f = NULL;
8084 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
8085 083957f4 2020-02-24 stsp if (err) {
8086 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8087 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8088 083957f4 2020-02-24 stsp goto done;
8089 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
8090 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
8091 083957f4 2020-02-24 stsp }
8092 0ebf8283 2019-07-24 stsp }
8093 0ebf8283 2019-07-24 stsp done:
8094 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8095 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8096 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
8097 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
8098 0ebf8283 2019-07-24 stsp free(path);
8099 0ebf8283 2019-07-24 stsp return err;
8100 0ebf8283 2019-07-24 stsp }
8101 0ebf8283 2019-07-24 stsp
8102 0ebf8283 2019-07-24 stsp static const struct got_error *
8103 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
8104 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8105 0ebf8283 2019-07-24 stsp {
8106 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8107 0ebf8283 2019-07-24 stsp char *path = NULL;
8108 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8109 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8110 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8111 0ebf8283 2019-07-24 stsp
8112 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
8113 0ebf8283 2019-07-24 stsp if (err)
8114 0ebf8283 2019-07-24 stsp return err;
8115 0ebf8283 2019-07-24 stsp
8116 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
8117 0ebf8283 2019-07-24 stsp if (f == NULL) {
8118 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8119 0ebf8283 2019-07-24 stsp goto done;
8120 0ebf8283 2019-07-24 stsp }
8121 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8122 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8123 0ebf8283 2019-07-24 stsp repo);
8124 0ebf8283 2019-07-24 stsp if (err)
8125 0ebf8283 2019-07-24 stsp break;
8126 0ebf8283 2019-07-24 stsp
8127 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8128 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
8129 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
8130 0ebf8283 2019-07-24 stsp if (n < 0) {
8131 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8132 0ebf8283 2019-07-24 stsp break;
8133 0ebf8283 2019-07-24 stsp }
8134 0ebf8283 2019-07-24 stsp }
8135 0ebf8283 2019-07-24 stsp }
8136 0ebf8283 2019-07-24 stsp done:
8137 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8138 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8139 0ebf8283 2019-07-24 stsp free(path);
8140 0ebf8283 2019-07-24 stsp if (commit)
8141 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8142 0ebf8283 2019-07-24 stsp return err;
8143 0ebf8283 2019-07-24 stsp }
8144 0ebf8283 2019-07-24 stsp
8145 bfce7f83 2019-07-27 stsp void
8146 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
8147 bfce7f83 2019-07-27 stsp {
8148 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8149 bfce7f83 2019-07-27 stsp
8150 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
8151 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
8152 bfce7f83 2019-07-27 stsp free(hle);
8153 bfce7f83 2019-07-27 stsp }
8154 bfce7f83 2019-07-27 stsp }
8155 bfce7f83 2019-07-27 stsp
8156 0ebf8283 2019-07-24 stsp static const struct got_error *
8157 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
8158 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
8159 0ebf8283 2019-07-24 stsp {
8160 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8161 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8162 0ebf8283 2019-07-24 stsp
8163 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8164 0ebf8283 2019-07-24 stsp if (f == NULL) {
8165 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8166 0ebf8283 2019-07-24 stsp goto done;
8167 0ebf8283 2019-07-24 stsp }
8168 0ebf8283 2019-07-24 stsp
8169 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8170 0ebf8283 2019-07-24 stsp done:
8171 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8172 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8173 0ebf8283 2019-07-24 stsp return err;
8174 0ebf8283 2019-07-24 stsp }
8175 0ebf8283 2019-07-24 stsp
8176 0ebf8283 2019-07-24 stsp static const struct got_error *
8177 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8178 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
8179 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
8180 0ebf8283 2019-07-24 stsp {
8181 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
8182 0ebf8283 2019-07-24 stsp int resp = ' ';
8183 0ebf8283 2019-07-24 stsp
8184 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
8185 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8186 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
8187 0ebf8283 2019-07-24 stsp resp = getchar();
8188 a61a4414 2019-08-07 stsp if (resp == '\n')
8189 a61a4414 2019-08-07 stsp resp = getchar();
8190 426ebf2e 2019-07-25 stsp if (resp == 'c') {
8191 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8192 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
8193 bfce7f83 2019-07-27 stsp repo);
8194 426ebf2e 2019-07-25 stsp if (err) {
8195 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8196 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8197 426ebf2e 2019-07-25 stsp break;
8198 bfce7f83 2019-07-27 stsp prev_err = err;
8199 426ebf2e 2019-07-25 stsp resp = ' ';
8200 426ebf2e 2019-07-25 stsp continue;
8201 426ebf2e 2019-07-25 stsp }
8202 bfce7f83 2019-07-27 stsp break;
8203 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
8204 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8205 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
8206 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
8207 426ebf2e 2019-07-25 stsp if (err) {
8208 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8209 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8210 426ebf2e 2019-07-25 stsp break;
8211 bfce7f83 2019-07-27 stsp prev_err = err;
8212 426ebf2e 2019-07-25 stsp resp = ' ';
8213 426ebf2e 2019-07-25 stsp continue;
8214 426ebf2e 2019-07-25 stsp }
8215 bfce7f83 2019-07-27 stsp break;
8216 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
8217 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8218 0ebf8283 2019-07-24 stsp break;
8219 426ebf2e 2019-07-25 stsp } else
8220 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
8221 0ebf8283 2019-07-24 stsp }
8222 0ebf8283 2019-07-24 stsp
8223 0ebf8283 2019-07-24 stsp return err;
8224 0ebf8283 2019-07-24 stsp }
8225 0ebf8283 2019-07-24 stsp
8226 0ebf8283 2019-07-24 stsp static const struct got_error *
8227 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
8228 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8229 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
8230 0ebf8283 2019-07-24 stsp {
8231 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8232 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8233 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8234 3e3a69f1 2019-07-25 stsp branch, repo);
8235 0ebf8283 2019-07-24 stsp }
8236 0ebf8283 2019-07-24 stsp
8237 0ebf8283 2019-07-24 stsp static const struct got_error *
8238 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
8239 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8240 0ebf8283 2019-07-24 stsp {
8241 0ebf8283 2019-07-24 stsp const struct got_error *err;
8242 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8243 0ebf8283 2019-07-24 stsp
8244 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
8245 0ebf8283 2019-07-24 stsp if (err)
8246 0ebf8283 2019-07-24 stsp goto done;
8247 0ebf8283 2019-07-24 stsp
8248 0ebf8283 2019-07-24 stsp if (new_id) {
8249 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
8250 0ebf8283 2019-07-24 stsp if (err)
8251 0ebf8283 2019-07-24 stsp goto done;
8252 0ebf8283 2019-07-24 stsp }
8253 0ebf8283 2019-07-24 stsp
8254 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
8255 0ebf8283 2019-07-24 stsp if (new_id_str)
8256 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
8257 0ebf8283 2019-07-24 stsp
8258 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8259 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
8260 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
8261 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8262 0ebf8283 2019-07-24 stsp goto done;
8263 0ebf8283 2019-07-24 stsp }
8264 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
8265 0ebf8283 2019-07-24 stsp } else {
8266 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8267 0ebf8283 2019-07-24 stsp if (err)
8268 0ebf8283 2019-07-24 stsp goto done;
8269 0ebf8283 2019-07-24 stsp }
8270 0ebf8283 2019-07-24 stsp
8271 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
8272 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
8273 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
8274 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
8275 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8276 0ebf8283 2019-07-24 stsp break;
8277 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
8278 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
8279 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8280 0ebf8283 2019-07-24 stsp logmsg);
8281 0ebf8283 2019-07-24 stsp break;
8282 0ebf8283 2019-07-24 stsp default:
8283 0ebf8283 2019-07-24 stsp break;
8284 0ebf8283 2019-07-24 stsp }
8285 0ebf8283 2019-07-24 stsp done:
8286 0ebf8283 2019-07-24 stsp free(old_id_str);
8287 0ebf8283 2019-07-24 stsp free(new_id_str);
8288 0ebf8283 2019-07-24 stsp return err;
8289 0ebf8283 2019-07-24 stsp }
8290 0ebf8283 2019-07-24 stsp
8291 0ebf8283 2019-07-24 stsp static const struct got_error *
8292 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
8293 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8294 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8295 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
8296 0ebf8283 2019-07-24 stsp {
8297 0ebf8283 2019-07-24 stsp const struct got_error *err;
8298 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8299 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
8300 0ebf8283 2019-07-24 stsp
8301 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8302 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
8303 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8304 0ebf8283 2019-07-24 stsp if (err)
8305 0ebf8283 2019-07-24 stsp return err;
8306 0ebf8283 2019-07-24 stsp }
8307 0ebf8283 2019-07-24 stsp
8308 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8309 0ebf8283 2019-07-24 stsp if (err)
8310 0ebf8283 2019-07-24 stsp return err;
8311 0ebf8283 2019-07-24 stsp
8312 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8313 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
8314 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
8315 0ebf8283 2019-07-24 stsp if (err) {
8316 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8317 0ebf8283 2019-07-24 stsp goto done;
8318 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
8319 0ebf8283 2019-07-24 stsp } else {
8320 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
8321 0ebf8283 2019-07-24 stsp free(new_commit_id);
8322 0ebf8283 2019-07-24 stsp }
8323 0ebf8283 2019-07-24 stsp done:
8324 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8325 0ebf8283 2019-07-24 stsp return err;
8326 0ebf8283 2019-07-24 stsp }
8327 0ebf8283 2019-07-24 stsp
8328 0ebf8283 2019-07-24 stsp static const struct got_error *
8329 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
8330 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8331 0ebf8283 2019-07-24 stsp {
8332 0ebf8283 2019-07-24 stsp const struct got_error *error;
8333 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8334 0ebf8283 2019-07-24 stsp
8335 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8336 0ebf8283 2019-07-24 stsp repo);
8337 0ebf8283 2019-07-24 stsp if (error)
8338 0ebf8283 2019-07-24 stsp return error;
8339 0ebf8283 2019-07-24 stsp
8340 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8341 0ebf8283 2019-07-24 stsp if (error)
8342 0ebf8283 2019-07-24 stsp return error;
8343 0ebf8283 2019-07-24 stsp
8344 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
8345 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8346 0ebf8283 2019-07-24 stsp return error;
8347 ab20a43a 2020-01-29 stsp }
8348 ab20a43a 2020-01-29 stsp
8349 ab20a43a 2020-01-29 stsp static const struct got_error *
8350 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
8351 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
8352 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8353 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8354 ab20a43a 2020-01-29 stsp {
8355 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
8356 ab20a43a 2020-01-29 stsp
8357 ab20a43a 2020-01-29 stsp switch (status) {
8358 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8359 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8360 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8361 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
8362 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8363 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8364 ab20a43a 2020-01-29 stsp default:
8365 ab20a43a 2020-01-29 stsp break;
8366 ab20a43a 2020-01-29 stsp }
8367 ab20a43a 2020-01-29 stsp
8368 ab20a43a 2020-01-29 stsp switch (staged_status) {
8369 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8370 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8371 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8372 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8373 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8374 ab20a43a 2020-01-29 stsp default:
8375 ab20a43a 2020-01-29 stsp break;
8376 ab20a43a 2020-01-29 stsp }
8377 ab20a43a 2020-01-29 stsp
8378 ab20a43a 2020-01-29 stsp return NULL;
8379 0ebf8283 2019-07-24 stsp }
8380 0ebf8283 2019-07-24 stsp
8381 0ebf8283 2019-07-24 stsp static const struct got_error *
8382 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
8383 0ebf8283 2019-07-24 stsp {
8384 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
8385 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
8386 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8387 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
8388 0ebf8283 2019-07-24 stsp char *cwd = NULL;
8389 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
8390 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
8391 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
8392 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
8393 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
8394 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8395 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
8396 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8397 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8398 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
8399 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
8400 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8401 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
8402 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
8403 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
8404 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
8405 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
8406 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8407 0ebf8283 2019-07-24 stsp
8408 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
8409 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
8410 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
8411 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8412 0ebf8283 2019-07-24 stsp
8413 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8414 0ebf8283 2019-07-24 stsp switch (ch) {
8415 0ebf8283 2019-07-24 stsp case 'a':
8416 0ebf8283 2019-07-24 stsp abort_edit = 1;
8417 0ebf8283 2019-07-24 stsp break;
8418 0ebf8283 2019-07-24 stsp case 'c':
8419 0ebf8283 2019-07-24 stsp continue_edit = 1;
8420 0ebf8283 2019-07-24 stsp break;
8421 0ebf8283 2019-07-24 stsp case 'F':
8422 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
8423 0ebf8283 2019-07-24 stsp break;
8424 083957f4 2020-02-24 stsp case 'm':
8425 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
8426 083957f4 2020-02-24 stsp break;
8427 0ebf8283 2019-07-24 stsp default:
8428 0ebf8283 2019-07-24 stsp usage_histedit();
8429 0ebf8283 2019-07-24 stsp /* NOTREACHED */
8430 0ebf8283 2019-07-24 stsp }
8431 0ebf8283 2019-07-24 stsp }
8432 0ebf8283 2019-07-24 stsp
8433 0ebf8283 2019-07-24 stsp argc -= optind;
8434 0ebf8283 2019-07-24 stsp argv += optind;
8435 0ebf8283 2019-07-24 stsp
8436 0ebf8283 2019-07-24 stsp #ifndef PROFILE
8437 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8438 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
8439 0ebf8283 2019-07-24 stsp err(1, "pledge");
8440 0ebf8283 2019-07-24 stsp #endif
8441 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
8442 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
8443 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
8444 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
8445 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
8446 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
8447 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
8448 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
8449 0ebf8283 2019-07-24 stsp if (argc != 0)
8450 0ebf8283 2019-07-24 stsp usage_histedit();
8451 0ebf8283 2019-07-24 stsp
8452 0ebf8283 2019-07-24 stsp /*
8453 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
8454 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
8455 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
8456 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
8457 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
8458 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
8459 0ebf8283 2019-07-24 stsp */
8460 0ebf8283 2019-07-24 stsp
8461 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
8462 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
8463 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
8464 0ebf8283 2019-07-24 stsp goto done;
8465 0ebf8283 2019-07-24 stsp }
8466 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
8467 fa51e947 2020-03-27 stsp if (error) {
8468 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8469 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "histedit", cwd);
8470 0ebf8283 2019-07-24 stsp goto done;
8471 fa51e947 2020-03-27 stsp }
8472 0ebf8283 2019-07-24 stsp
8473 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8474 c9956ddf 2019-09-08 stsp NULL);
8475 0ebf8283 2019-07-24 stsp if (error != NULL)
8476 0ebf8283 2019-07-24 stsp goto done;
8477 0ebf8283 2019-07-24 stsp
8478 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8479 0ebf8283 2019-07-24 stsp if (error)
8480 0ebf8283 2019-07-24 stsp goto done;
8481 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
8482 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
8483 0ebf8283 2019-07-24 stsp goto done;
8484 0ebf8283 2019-07-24 stsp }
8485 0ebf8283 2019-07-24 stsp
8486 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8487 0ebf8283 2019-07-24 stsp if (error)
8488 0ebf8283 2019-07-24 stsp goto done;
8489 0ebf8283 2019-07-24 stsp
8490 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
8491 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8492 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
8493 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
8494 083957f4 2020-02-24 stsp "before the -m option can be used");
8495 083957f4 2020-02-24 stsp goto done;
8496 083957f4 2020-02-24 stsp }
8497 083957f4 2020-02-24 stsp
8498 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
8499 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8500 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8501 3e3a69f1 2019-07-25 stsp worktree, repo);
8502 0ebf8283 2019-07-24 stsp if (error)
8503 0ebf8283 2019-07-24 stsp goto done;
8504 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8505 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8506 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
8507 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
8508 0ebf8283 2019-07-24 stsp if (error)
8509 0ebf8283 2019-07-24 stsp goto done;
8510 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
8511 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8512 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8513 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
8514 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
8515 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8516 0ebf8283 2019-07-24 stsp goto done;
8517 0ebf8283 2019-07-24 stsp }
8518 0ebf8283 2019-07-24 stsp
8519 0ebf8283 2019-07-24 stsp if (continue_edit) {
8520 0ebf8283 2019-07-24 stsp char *path;
8521 0ebf8283 2019-07-24 stsp
8522 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
8523 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8524 0ebf8283 2019-07-24 stsp goto done;
8525 0ebf8283 2019-07-24 stsp }
8526 0ebf8283 2019-07-24 stsp
8527 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
8528 0ebf8283 2019-07-24 stsp if (error)
8529 0ebf8283 2019-07-24 stsp goto done;
8530 0ebf8283 2019-07-24 stsp
8531 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8532 0ebf8283 2019-07-24 stsp free(path);
8533 0ebf8283 2019-07-24 stsp if (error)
8534 0ebf8283 2019-07-24 stsp goto done;
8535 0ebf8283 2019-07-24 stsp
8536 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8537 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8538 3e3a69f1 2019-07-25 stsp worktree, repo);
8539 0ebf8283 2019-07-24 stsp if (error)
8540 0ebf8283 2019-07-24 stsp goto done;
8541 0ebf8283 2019-07-24 stsp
8542 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8543 0ebf8283 2019-07-24 stsp if (error)
8544 0ebf8283 2019-07-24 stsp goto done;
8545 0ebf8283 2019-07-24 stsp
8546 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8547 0ebf8283 2019-07-24 stsp head_commit_id);
8548 0ebf8283 2019-07-24 stsp if (error)
8549 0ebf8283 2019-07-24 stsp goto done;
8550 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8551 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8552 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8553 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8554 3aac7cf7 2019-07-25 stsp goto done;
8555 3aac7cf7 2019-07-25 stsp }
8556 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8557 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8558 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8559 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8560 0ebf8283 2019-07-24 stsp commit = NULL;
8561 0ebf8283 2019-07-24 stsp if (error)
8562 0ebf8283 2019-07-24 stsp goto done;
8563 0ebf8283 2019-07-24 stsp } else {
8564 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
8565 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8566 0ebf8283 2019-07-24 stsp goto done;
8567 0ebf8283 2019-07-24 stsp }
8568 0ebf8283 2019-07-24 stsp
8569 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
8570 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
8571 0ebf8283 2019-07-24 stsp if (error != NULL)
8572 0ebf8283 2019-07-24 stsp goto done;
8573 0ebf8283 2019-07-24 stsp
8574 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8575 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8576 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
8577 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
8578 c7d20a3f 2019-07-30 stsp goto done;
8579 c7d20a3f 2019-07-30 stsp }
8580 c7d20a3f 2019-07-30 stsp
8581 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8582 bfce7f83 2019-07-27 stsp got_ref_close(branch);
8583 bfce7f83 2019-07-27 stsp branch = NULL;
8584 0ebf8283 2019-07-24 stsp if (error)
8585 0ebf8283 2019-07-24 stsp goto done;
8586 0ebf8283 2019-07-24 stsp
8587 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8588 0ebf8283 2019-07-24 stsp head_commit_id);
8589 0ebf8283 2019-07-24 stsp if (error)
8590 0ebf8283 2019-07-24 stsp goto done;
8591 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8592 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8593 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8594 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8595 3aac7cf7 2019-07-25 stsp goto done;
8596 3aac7cf7 2019-07-25 stsp }
8597 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8598 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
8599 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
8600 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8601 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8602 0ebf8283 2019-07-24 stsp commit = NULL;
8603 0ebf8283 2019-07-24 stsp if (error)
8604 0ebf8283 2019-07-24 stsp goto done;
8605 0ebf8283 2019-07-24 stsp
8606 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
8607 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8608 c5996fff 2020-01-29 stsp goto done;
8609 c5996fff 2020-01-29 stsp }
8610 c5996fff 2020-01-29 stsp
8611 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8612 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
8613 bfce7f83 2019-07-27 stsp if (error)
8614 bfce7f83 2019-07-27 stsp goto done;
8615 bfce7f83 2019-07-27 stsp
8616 0ebf8283 2019-07-24 stsp if (edit_script_path) {
8617 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
8618 0ebf8283 2019-07-24 stsp edit_script_path, repo);
8619 6ba2bea2 2019-07-27 stsp if (error) {
8620 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8621 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8622 9627c110 2020-04-18 stsp update_progress, &upa);
8623 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8624 0ebf8283 2019-07-24 stsp goto done;
8625 6ba2bea2 2019-07-27 stsp }
8626 0ebf8283 2019-07-24 stsp } else {
8627 514f2ffe 2020-01-29 stsp const char *branch_name;
8628 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
8629 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8630 514f2ffe 2020-01-29 stsp branch_name += 11;
8631 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8632 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
8633 6ba2bea2 2019-07-27 stsp if (error) {
8634 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8635 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8636 9627c110 2020-04-18 stsp update_progress, &upa);
8637 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8638 0ebf8283 2019-07-24 stsp goto done;
8639 6ba2bea2 2019-07-27 stsp }
8640 0ebf8283 2019-07-24 stsp
8641 0ebf8283 2019-07-24 stsp }
8642 0ebf8283 2019-07-24 stsp
8643 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
8644 0ebf8283 2019-07-24 stsp repo);
8645 6ba2bea2 2019-07-27 stsp if (error) {
8646 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8647 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8648 9627c110 2020-04-18 stsp update_progress, &upa);
8649 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8650 0ebf8283 2019-07-24 stsp goto done;
8651 6ba2bea2 2019-07-27 stsp }
8652 0ebf8283 2019-07-24 stsp
8653 0ebf8283 2019-07-24 stsp }
8654 0ebf8283 2019-07-24 stsp
8655 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
8656 4ec14e60 2019-08-28 hiltjo if (error)
8657 0ebf8283 2019-07-24 stsp goto done;
8658 0ebf8283 2019-07-24 stsp
8659 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8660 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
8661 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
8662 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
8663 0ebf8283 2019-07-24 stsp continue;
8664 0ebf8283 2019-07-24 stsp
8665 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
8666 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8667 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8668 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
8669 0ebf8283 2019-07-24 stsp repo);
8670 ab20a43a 2020-01-29 stsp if (error)
8671 ab20a43a 2020-01-29 stsp goto done;
8672 0ebf8283 2019-07-24 stsp } else {
8673 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
8674 ab20a43a 2020-01-29 stsp int have_changes = 0;
8675 ab20a43a 2020-01-29 stsp
8676 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
8677 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
8678 ab20a43a 2020-01-29 stsp if (error)
8679 ab20a43a 2020-01-29 stsp goto done;
8680 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
8681 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
8682 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
8683 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
8684 ab20a43a 2020-01-29 stsp if (error) {
8685 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
8686 ab20a43a 2020-01-29 stsp goto done;
8687 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
8688 ab20a43a 2020-01-29 stsp goto done;
8689 ab20a43a 2020-01-29 stsp }
8690 ab20a43a 2020-01-29 stsp if (have_changes) {
8691 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
8692 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
8693 ab20a43a 2020-01-29 stsp if (error)
8694 ab20a43a 2020-01-29 stsp goto done;
8695 ab20a43a 2020-01-29 stsp } else {
8696 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
8697 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
8698 ab20a43a 2020-01-29 stsp if (error)
8699 ab20a43a 2020-01-29 stsp goto done;
8700 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
8701 ab20a43a 2020-01-29 stsp hle, NULL);
8702 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
8703 ab20a43a 2020-01-29 stsp commit = NULL;
8704 ab20a43a 2020-01-29 stsp if (error)
8705 ab20a43a 2020-01-29 stsp goto done;
8706 ab20a43a 2020-01-29 stsp }
8707 0ebf8283 2019-07-24 stsp }
8708 0ebf8283 2019-07-24 stsp continue;
8709 0ebf8283 2019-07-24 stsp }
8710 0ebf8283 2019-07-24 stsp
8711 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8712 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8713 0ebf8283 2019-07-24 stsp if (error)
8714 0ebf8283 2019-07-24 stsp goto done;
8715 0ebf8283 2019-07-24 stsp continue;
8716 0ebf8283 2019-07-24 stsp }
8717 0ebf8283 2019-07-24 stsp
8718 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8719 0ebf8283 2019-07-24 stsp hle->commit_id);
8720 0ebf8283 2019-07-24 stsp if (error)
8721 0ebf8283 2019-07-24 stsp goto done;
8722 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8723 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8724 0ebf8283 2019-07-24 stsp
8725 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8726 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8727 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8728 0ebf8283 2019-07-24 stsp if (error)
8729 0ebf8283 2019-07-24 stsp goto done;
8730 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8731 0ebf8283 2019-07-24 stsp commit = NULL;
8732 0ebf8283 2019-07-24 stsp
8733 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8734 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
8735 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
8736 9627c110 2020-04-18 stsp
8737 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8738 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
8739 a0ea4fc0 2020-02-28 stsp repo);
8740 a0ea4fc0 2020-02-28 stsp if (error)
8741 a0ea4fc0 2020-02-28 stsp goto done;
8742 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8743 0ebf8283 2019-07-24 stsp break;
8744 0ebf8283 2019-07-24 stsp }
8745 0ebf8283 2019-07-24 stsp
8746 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8747 0ebf8283 2019-07-24 stsp char *id_str;
8748 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
8749 0ebf8283 2019-07-24 stsp if (error)
8750 0ebf8283 2019-07-24 stsp goto done;
8751 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
8752 0ebf8283 2019-07-24 stsp id_str);
8753 0ebf8283 2019-07-24 stsp free(id_str);
8754 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8755 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
8756 3e3a69f1 2019-07-25 stsp fileindex);
8757 0ebf8283 2019-07-24 stsp goto done;
8758 0ebf8283 2019-07-24 stsp }
8759 0ebf8283 2019-07-24 stsp
8760 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8761 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8762 0ebf8283 2019-07-24 stsp if (error)
8763 0ebf8283 2019-07-24 stsp goto done;
8764 0ebf8283 2019-07-24 stsp continue;
8765 0ebf8283 2019-07-24 stsp }
8766 0ebf8283 2019-07-24 stsp
8767 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8768 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
8769 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8770 0ebf8283 2019-07-24 stsp if (error)
8771 0ebf8283 2019-07-24 stsp goto done;
8772 0ebf8283 2019-07-24 stsp }
8773 0ebf8283 2019-07-24 stsp
8774 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8775 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8776 0ebf8283 2019-07-24 stsp if (error)
8777 0ebf8283 2019-07-24 stsp goto done;
8778 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8779 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
8780 0ebf8283 2019-07-24 stsp } else
8781 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8782 3e3a69f1 2019-07-25 stsp branch, repo);
8783 0ebf8283 2019-07-24 stsp done:
8784 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
8785 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
8786 0ebf8283 2019-07-24 stsp free(head_commit_id);
8787 0ebf8283 2019-07-24 stsp free(base_commit_id);
8788 0ebf8283 2019-07-24 stsp free(resume_commit_id);
8789 0ebf8283 2019-07-24 stsp if (commit)
8790 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8791 0ebf8283 2019-07-24 stsp if (branch)
8792 0ebf8283 2019-07-24 stsp got_ref_close(branch);
8793 0ebf8283 2019-07-24 stsp if (tmp_branch)
8794 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
8795 0ebf8283 2019-07-24 stsp if (worktree)
8796 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
8797 2822a352 2019-10-15 stsp if (repo)
8798 2822a352 2019-10-15 stsp got_repo_close(repo);
8799 2822a352 2019-10-15 stsp return error;
8800 2822a352 2019-10-15 stsp }
8801 2822a352 2019-10-15 stsp
8802 2822a352 2019-10-15 stsp __dead static void
8803 2822a352 2019-10-15 stsp usage_integrate(void)
8804 2822a352 2019-10-15 stsp {
8805 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8806 2822a352 2019-10-15 stsp exit(1);
8807 2822a352 2019-10-15 stsp }
8808 2822a352 2019-10-15 stsp
8809 2822a352 2019-10-15 stsp static const struct got_error *
8810 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
8811 2822a352 2019-10-15 stsp {
8812 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
8813 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
8814 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
8815 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8816 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
8817 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8818 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
8819 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8820 9627c110 2020-04-18 stsp int ch;
8821 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8822 2822a352 2019-10-15 stsp
8823 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8824 2822a352 2019-10-15 stsp switch (ch) {
8825 2822a352 2019-10-15 stsp default:
8826 2822a352 2019-10-15 stsp usage_integrate();
8827 2822a352 2019-10-15 stsp /* NOTREACHED */
8828 2822a352 2019-10-15 stsp }
8829 2822a352 2019-10-15 stsp }
8830 2822a352 2019-10-15 stsp
8831 2822a352 2019-10-15 stsp argc -= optind;
8832 2822a352 2019-10-15 stsp argv += optind;
8833 2822a352 2019-10-15 stsp
8834 2822a352 2019-10-15 stsp if (argc != 1)
8835 2822a352 2019-10-15 stsp usage_integrate();
8836 2822a352 2019-10-15 stsp branch_arg = argv[0];
8837 2822a352 2019-10-15 stsp
8838 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8839 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
8840 2822a352 2019-10-15 stsp err(1, "pledge");
8841 2822a352 2019-10-15 stsp
8842 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
8843 2822a352 2019-10-15 stsp if (cwd == NULL) {
8844 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
8845 2822a352 2019-10-15 stsp goto done;
8846 2822a352 2019-10-15 stsp }
8847 2822a352 2019-10-15 stsp
8848 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
8849 fa51e947 2020-03-27 stsp if (error) {
8850 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8851 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
8852 fa51e947 2020-03-27 stsp cwd);
8853 2822a352 2019-10-15 stsp goto done;
8854 fa51e947 2020-03-27 stsp }
8855 2822a352 2019-10-15 stsp
8856 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
8857 2822a352 2019-10-15 stsp if (error)
8858 2822a352 2019-10-15 stsp goto done;
8859 2822a352 2019-10-15 stsp
8860 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8861 2822a352 2019-10-15 stsp NULL);
8862 2822a352 2019-10-15 stsp if (error != NULL)
8863 2822a352 2019-10-15 stsp goto done;
8864 2822a352 2019-10-15 stsp
8865 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8866 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
8867 2822a352 2019-10-15 stsp if (error)
8868 2822a352 2019-10-15 stsp goto done;
8869 2822a352 2019-10-15 stsp
8870 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8871 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
8872 2822a352 2019-10-15 stsp goto done;
8873 2822a352 2019-10-15 stsp }
8874 2822a352 2019-10-15 stsp
8875 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8876 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
8877 2822a352 2019-10-15 stsp if (error)
8878 2822a352 2019-10-15 stsp goto done;
8879 2822a352 2019-10-15 stsp
8880 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
8881 2822a352 2019-10-15 stsp if (refname == NULL) {
8882 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8883 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8884 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8885 2822a352 2019-10-15 stsp goto done;
8886 2822a352 2019-10-15 stsp }
8887 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
8888 2822a352 2019-10-15 stsp if (base_refname == NULL) {
8889 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8890 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8891 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8892 2822a352 2019-10-15 stsp goto done;
8893 2822a352 2019-10-15 stsp }
8894 2822a352 2019-10-15 stsp
8895 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
8896 2822a352 2019-10-15 stsp if (error)
8897 2822a352 2019-10-15 stsp goto done;
8898 2822a352 2019-10-15 stsp
8899 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8900 2822a352 2019-10-15 stsp if (error)
8901 2822a352 2019-10-15 stsp goto done;
8902 2822a352 2019-10-15 stsp
8903 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8904 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
8905 2822a352 2019-10-15 stsp "specified branch has already been integrated");
8906 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8907 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8908 2822a352 2019-10-15 stsp goto done;
8909 2822a352 2019-10-15 stsp }
8910 2822a352 2019-10-15 stsp
8911 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8912 2822a352 2019-10-15 stsp if (error) {
8913 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
8914 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
8915 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8916 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8917 2822a352 2019-10-15 stsp goto done;
8918 2822a352 2019-10-15 stsp }
8919 2822a352 2019-10-15 stsp
8920 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8921 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
8922 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
8923 2822a352 2019-10-15 stsp check_cancelled, NULL);
8924 2822a352 2019-10-15 stsp if (error)
8925 2822a352 2019-10-15 stsp goto done;
8926 2822a352 2019-10-15 stsp
8927 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
8928 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8929 2822a352 2019-10-15 stsp done:
8930 715dc77e 2019-08-03 stsp if (repo)
8931 715dc77e 2019-08-03 stsp got_repo_close(repo);
8932 2822a352 2019-10-15 stsp if (worktree)
8933 2822a352 2019-10-15 stsp got_worktree_close(worktree);
8934 2822a352 2019-10-15 stsp free(cwd);
8935 2822a352 2019-10-15 stsp free(base_commit_id);
8936 2822a352 2019-10-15 stsp free(commit_id);
8937 2822a352 2019-10-15 stsp free(refname);
8938 2822a352 2019-10-15 stsp free(base_refname);
8939 715dc77e 2019-08-03 stsp return error;
8940 715dc77e 2019-08-03 stsp }
8941 715dc77e 2019-08-03 stsp
8942 715dc77e 2019-08-03 stsp __dead static void
8943 715dc77e 2019-08-03 stsp usage_stage(void)
8944 715dc77e 2019-08-03 stsp {
8945 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8946 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
8947 715dc77e 2019-08-03 stsp getprogname());
8948 715dc77e 2019-08-03 stsp exit(1);
8949 715dc77e 2019-08-03 stsp }
8950 715dc77e 2019-08-03 stsp
8951 715dc77e 2019-08-03 stsp static const struct got_error *
8952 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
8953 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
8954 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8955 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
8956 408b4ebc 2019-08-03 stsp {
8957 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
8958 408b4ebc 2019-08-03 stsp char *id_str = NULL;
8959 408b4ebc 2019-08-03 stsp
8960 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
8961 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
8962 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
8963 408b4ebc 2019-08-03 stsp return NULL;
8964 408b4ebc 2019-08-03 stsp
8965 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
8966 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
8967 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
8968 408b4ebc 2019-08-03 stsp else
8969 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
8970 408b4ebc 2019-08-03 stsp if (err)
8971 408b4ebc 2019-08-03 stsp return err;
8972 408b4ebc 2019-08-03 stsp
8973 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
8974 408b4ebc 2019-08-03 stsp free(id_str);
8975 dc424a06 2019-08-07 stsp return NULL;
8976 dc424a06 2019-08-07 stsp }
8977 2e1f37b0 2019-08-08 stsp
8978 dc424a06 2019-08-07 stsp static const struct got_error *
8979 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
8980 715dc77e 2019-08-03 stsp {
8981 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
8982 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
8983 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
8984 715dc77e 2019-08-03 stsp char *cwd = NULL;
8985 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
8986 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
8987 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
8988 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
8989 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8990 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8991 715dc77e 2019-08-03 stsp
8992 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
8993 715dc77e 2019-08-03 stsp
8994 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
8995 715dc77e 2019-08-03 stsp switch (ch) {
8996 408b4ebc 2019-08-03 stsp case 'l':
8997 408b4ebc 2019-08-03 stsp list_stage = 1;
8998 408b4ebc 2019-08-03 stsp break;
8999 dc424a06 2019-08-07 stsp case 'p':
9000 dc424a06 2019-08-07 stsp pflag = 1;
9001 dc424a06 2019-08-07 stsp break;
9002 dc424a06 2019-08-07 stsp case 'F':
9003 dc424a06 2019-08-07 stsp patch_script_path = optarg;
9004 dc424a06 2019-08-07 stsp break;
9005 35213c7c 2020-07-23 stsp case 'S':
9006 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
9007 35213c7c 2020-07-23 stsp break;
9008 715dc77e 2019-08-03 stsp default:
9009 715dc77e 2019-08-03 stsp usage_stage();
9010 715dc77e 2019-08-03 stsp /* NOTREACHED */
9011 715dc77e 2019-08-03 stsp }
9012 715dc77e 2019-08-03 stsp }
9013 715dc77e 2019-08-03 stsp
9014 715dc77e 2019-08-03 stsp argc -= optind;
9015 715dc77e 2019-08-03 stsp argv += optind;
9016 715dc77e 2019-08-03 stsp
9017 715dc77e 2019-08-03 stsp #ifndef PROFILE
9018 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9019 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
9020 715dc77e 2019-08-03 stsp err(1, "pledge");
9021 715dc77e 2019-08-03 stsp #endif
9022 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
9023 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
9024 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
9025 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
9026 715dc77e 2019-08-03 stsp
9027 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
9028 715dc77e 2019-08-03 stsp if (cwd == NULL) {
9029 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
9030 715dc77e 2019-08-03 stsp goto done;
9031 715dc77e 2019-08-03 stsp }
9032 715dc77e 2019-08-03 stsp
9033 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9034 fa51e947 2020-03-27 stsp if (error) {
9035 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9036 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
9037 715dc77e 2019-08-03 stsp goto done;
9038 fa51e947 2020-03-27 stsp }
9039 715dc77e 2019-08-03 stsp
9040 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9041 c9956ddf 2019-09-08 stsp NULL);
9042 715dc77e 2019-08-03 stsp if (error != NULL)
9043 715dc77e 2019-08-03 stsp goto done;
9044 715dc77e 2019-08-03 stsp
9045 dc424a06 2019-08-07 stsp if (patch_script_path) {
9046 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
9047 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
9048 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
9049 dc424a06 2019-08-07 stsp patch_script_path);
9050 dc424a06 2019-08-07 stsp goto done;
9051 dc424a06 2019-08-07 stsp }
9052 dc424a06 2019-08-07 stsp }
9053 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9054 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
9055 715dc77e 2019-08-03 stsp if (error)
9056 715dc77e 2019-08-03 stsp goto done;
9057 715dc77e 2019-08-03 stsp
9058 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9059 6d022e97 2019-08-04 stsp if (error)
9060 6d022e97 2019-08-04 stsp goto done;
9061 715dc77e 2019-08-03 stsp
9062 6d022e97 2019-08-04 stsp if (list_stage)
9063 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
9064 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
9065 2e1f37b0 2019-08-08 stsp else {
9066 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9067 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
9068 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
9069 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
9070 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
9071 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
9072 2e1f37b0 2019-08-08 stsp }
9073 ad493afc 2019-08-03 stsp done:
9074 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9075 2e1f37b0 2019-08-08 stsp error == NULL)
9076 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9077 ad493afc 2019-08-03 stsp if (repo)
9078 ad493afc 2019-08-03 stsp got_repo_close(repo);
9079 ad493afc 2019-08-03 stsp if (worktree)
9080 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
9081 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9082 ad493afc 2019-08-03 stsp free((char *)pe->path);
9083 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
9084 ad493afc 2019-08-03 stsp free(cwd);
9085 ad493afc 2019-08-03 stsp return error;
9086 ad493afc 2019-08-03 stsp }
9087 ad493afc 2019-08-03 stsp
9088 ad493afc 2019-08-03 stsp __dead static void
9089 ad493afc 2019-08-03 stsp usage_unstage(void)
9090 ad493afc 2019-08-03 stsp {
9091 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9092 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
9093 ad493afc 2019-08-03 stsp getprogname());
9094 ad493afc 2019-08-03 stsp exit(1);
9095 ad493afc 2019-08-03 stsp }
9096 ad493afc 2019-08-03 stsp
9097 ad493afc 2019-08-03 stsp
9098 ad493afc 2019-08-03 stsp static const struct got_error *
9099 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
9100 ad493afc 2019-08-03 stsp {
9101 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
9102 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
9103 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
9104 ad493afc 2019-08-03 stsp char *cwd = NULL;
9105 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
9106 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9107 9627c110 2020-04-18 stsp int ch, pflag = 0;
9108 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9109 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
9110 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9111 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9112 ad493afc 2019-08-03 stsp
9113 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
9114 ad493afc 2019-08-03 stsp
9115 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
9116 ad493afc 2019-08-03 stsp switch (ch) {
9117 2e1f37b0 2019-08-08 stsp case 'p':
9118 2e1f37b0 2019-08-08 stsp pflag = 1;
9119 2e1f37b0 2019-08-08 stsp break;
9120 2e1f37b0 2019-08-08 stsp case 'F':
9121 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
9122 2e1f37b0 2019-08-08 stsp break;
9123 ad493afc 2019-08-03 stsp default:
9124 ad493afc 2019-08-03 stsp usage_unstage();
9125 ad493afc 2019-08-03 stsp /* NOTREACHED */
9126 ad493afc 2019-08-03 stsp }
9127 ad493afc 2019-08-03 stsp }
9128 ad493afc 2019-08-03 stsp
9129 ad493afc 2019-08-03 stsp argc -= optind;
9130 ad493afc 2019-08-03 stsp argv += optind;
9131 ad493afc 2019-08-03 stsp
9132 ad493afc 2019-08-03 stsp #ifndef PROFILE
9133 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9134 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
9135 ad493afc 2019-08-03 stsp err(1, "pledge");
9136 ad493afc 2019-08-03 stsp #endif
9137 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
9138 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
9139 2e1f37b0 2019-08-08 stsp
9140 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
9141 ad493afc 2019-08-03 stsp if (cwd == NULL) {
9142 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
9143 ad493afc 2019-08-03 stsp goto done;
9144 ad493afc 2019-08-03 stsp }
9145 ad493afc 2019-08-03 stsp
9146 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9147 fa51e947 2020-03-27 stsp if (error) {
9148 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9149 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
9150 ad493afc 2019-08-03 stsp goto done;
9151 fa51e947 2020-03-27 stsp }
9152 ad493afc 2019-08-03 stsp
9153 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9154 c9956ddf 2019-09-08 stsp NULL);
9155 ad493afc 2019-08-03 stsp if (error != NULL)
9156 ad493afc 2019-08-03 stsp goto done;
9157 ad493afc 2019-08-03 stsp
9158 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
9159 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
9160 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
9161 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
9162 2e1f37b0 2019-08-08 stsp patch_script_path);
9163 2e1f37b0 2019-08-08 stsp goto done;
9164 2e1f37b0 2019-08-08 stsp }
9165 2e1f37b0 2019-08-08 stsp }
9166 2e1f37b0 2019-08-08 stsp
9167 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9168 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
9169 ad493afc 2019-08-03 stsp if (error)
9170 ad493afc 2019-08-03 stsp goto done;
9171 ad493afc 2019-08-03 stsp
9172 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9173 ad493afc 2019-08-03 stsp if (error)
9174 ad493afc 2019-08-03 stsp goto done;
9175 ad493afc 2019-08-03 stsp
9176 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9177 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
9178 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9179 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
9180 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
9181 9627c110 2020-04-18 stsp if (!error)
9182 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9183 715dc77e 2019-08-03 stsp done:
9184 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9185 2e1f37b0 2019-08-08 stsp error == NULL)
9186 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9187 0ebf8283 2019-07-24 stsp if (repo)
9188 0ebf8283 2019-07-24 stsp got_repo_close(repo);
9189 715dc77e 2019-08-03 stsp if (worktree)
9190 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
9191 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9192 715dc77e 2019-08-03 stsp free((char *)pe->path);
9193 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
9194 715dc77e 2019-08-03 stsp free(cwd);
9195 01073a5d 2019-08-22 stsp return error;
9196 01073a5d 2019-08-22 stsp }
9197 01073a5d 2019-08-22 stsp
9198 01073a5d 2019-08-22 stsp __dead static void
9199 01073a5d 2019-08-22 stsp usage_cat(void)
9200 01073a5d 2019-08-22 stsp {
9201 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9202 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
9203 01073a5d 2019-08-22 stsp exit(1);
9204 01073a5d 2019-08-22 stsp }
9205 01073a5d 2019-08-22 stsp
9206 01073a5d 2019-08-22 stsp static const struct got_error *
9207 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9208 01073a5d 2019-08-22 stsp {
9209 01073a5d 2019-08-22 stsp const struct got_error *err;
9210 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
9211 01073a5d 2019-08-22 stsp
9212 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
9213 01073a5d 2019-08-22 stsp if (err)
9214 01073a5d 2019-08-22 stsp return err;
9215 01073a5d 2019-08-22 stsp
9216 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9217 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
9218 01073a5d 2019-08-22 stsp return err;
9219 01073a5d 2019-08-22 stsp }
9220 01073a5d 2019-08-22 stsp
9221 01073a5d 2019-08-22 stsp static const struct got_error *
9222 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9223 01073a5d 2019-08-22 stsp {
9224 01073a5d 2019-08-22 stsp const struct got_error *err;
9225 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
9226 56e0773d 2019-11-28 stsp int nentries, i;
9227 01073a5d 2019-08-22 stsp
9228 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
9229 01073a5d 2019-08-22 stsp if (err)
9230 01073a5d 2019-08-22 stsp return err;
9231 01073a5d 2019-08-22 stsp
9232 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
9233 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
9234 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
9235 01073a5d 2019-08-22 stsp char *id_str;
9236 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
9237 01073a5d 2019-08-22 stsp break;
9238 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
9239 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9240 01073a5d 2019-08-22 stsp if (err)
9241 01073a5d 2019-08-22 stsp break;
9242 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
9243 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
9244 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
9245 01073a5d 2019-08-22 stsp free(id_str);
9246 01073a5d 2019-08-22 stsp }
9247 01073a5d 2019-08-22 stsp
9248 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
9249 01073a5d 2019-08-22 stsp return err;
9250 01073a5d 2019-08-22 stsp }
9251 01073a5d 2019-08-22 stsp
9252 01073a5d 2019-08-22 stsp static const struct got_error *
9253 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9254 01073a5d 2019-08-22 stsp {
9255 01073a5d 2019-08-22 stsp const struct got_error *err;
9256 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
9257 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
9258 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
9259 01073a5d 2019-08-22 stsp char *id_str = NULL;
9260 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
9261 01073a5d 2019-08-22 stsp
9262 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
9263 01073a5d 2019-08-22 stsp if (err)
9264 01073a5d 2019-08-22 stsp return err;
9265 01073a5d 2019-08-22 stsp
9266 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9267 01073a5d 2019-08-22 stsp if (err)
9268 01073a5d 2019-08-22 stsp goto done;
9269 01073a5d 2019-08-22 stsp
9270 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9271 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9272 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
9273 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
9274 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9275 01073a5d 2019-08-22 stsp char *pid_str;
9276 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
9277 01073a5d 2019-08-22 stsp if (err)
9278 01073a5d 2019-08-22 stsp goto done;
9279 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9280 01073a5d 2019-08-22 stsp free(pid_str);
9281 01073a5d 2019-08-22 stsp }
9282 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9283 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9284 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
9285 01073a5d 2019-08-22 stsp
9286 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9287 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9288 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
9289 01073a5d 2019-08-22 stsp
9290 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
9291 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9292 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
9293 01073a5d 2019-08-22 stsp done:
9294 01073a5d 2019-08-22 stsp free(id_str);
9295 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
9296 01073a5d 2019-08-22 stsp return err;
9297 01073a5d 2019-08-22 stsp }
9298 01073a5d 2019-08-22 stsp
9299 01073a5d 2019-08-22 stsp static const struct got_error *
9300 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9301 01073a5d 2019-08-22 stsp {
9302 01073a5d 2019-08-22 stsp const struct got_error *err;
9303 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
9304 01073a5d 2019-08-22 stsp char *id_str = NULL;
9305 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
9306 01073a5d 2019-08-22 stsp
9307 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
9308 01073a5d 2019-08-22 stsp if (err)
9309 01073a5d 2019-08-22 stsp return err;
9310 01073a5d 2019-08-22 stsp
9311 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9312 01073a5d 2019-08-22 stsp if (err)
9313 01073a5d 2019-08-22 stsp goto done;
9314 01073a5d 2019-08-22 stsp
9315 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9316 e15d5241 2019-08-22 stsp
9317 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
9318 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9319 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9320 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
9321 01073a5d 2019-08-22 stsp break;
9322 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9323 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9324 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
9325 01073a5d 2019-08-22 stsp break;
9326 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9327 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9328 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
9329 01073a5d 2019-08-22 stsp break;
9330 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9331 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9332 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
9333 01073a5d 2019-08-22 stsp break;
9334 01073a5d 2019-08-22 stsp default:
9335 01073a5d 2019-08-22 stsp break;
9336 01073a5d 2019-08-22 stsp }
9337 01073a5d 2019-08-22 stsp
9338 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9339 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
9340 e15d5241 2019-08-22 stsp
9341 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9342 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
9343 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
9344 01073a5d 2019-08-22 stsp
9345 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
9346 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9347 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
9348 01073a5d 2019-08-22 stsp done:
9349 01073a5d 2019-08-22 stsp free(id_str);
9350 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
9351 01073a5d 2019-08-22 stsp return err;
9352 01073a5d 2019-08-22 stsp }
9353 01073a5d 2019-08-22 stsp
9354 01073a5d 2019-08-22 stsp static const struct got_error *
9355 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
9356 01073a5d 2019-08-22 stsp {
9357 01073a5d 2019-08-22 stsp const struct got_error *error;
9358 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
9359 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
9360 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
9361 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
9362 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
9363 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
9364 01073a5d 2019-08-22 stsp
9365 01073a5d 2019-08-22 stsp #ifndef PROFILE
9366 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9367 01073a5d 2019-08-22 stsp NULL) == -1)
9368 01073a5d 2019-08-22 stsp err(1, "pledge");
9369 01073a5d 2019-08-22 stsp #endif
9370 01073a5d 2019-08-22 stsp
9371 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9372 01073a5d 2019-08-22 stsp switch (ch) {
9373 896e9b6f 2019-08-26 stsp case 'c':
9374 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
9375 896e9b6f 2019-08-26 stsp break;
9376 01073a5d 2019-08-22 stsp case 'r':
9377 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
9378 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9379 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
9380 9ba1d308 2019-10-21 stsp optarg);
9381 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
9382 01073a5d 2019-08-22 stsp break;
9383 896e9b6f 2019-08-26 stsp case 'P':
9384 896e9b6f 2019-08-26 stsp force_path = 1;
9385 896e9b6f 2019-08-26 stsp break;
9386 01073a5d 2019-08-22 stsp default:
9387 01073a5d 2019-08-22 stsp usage_cat();
9388 01073a5d 2019-08-22 stsp /* NOTREACHED */
9389 01073a5d 2019-08-22 stsp }
9390 01073a5d 2019-08-22 stsp }
9391 01073a5d 2019-08-22 stsp
9392 01073a5d 2019-08-22 stsp argc -= optind;
9393 01073a5d 2019-08-22 stsp argv += optind;
9394 01073a5d 2019-08-22 stsp
9395 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
9396 01073a5d 2019-08-22 stsp if (cwd == NULL) {
9397 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
9398 01073a5d 2019-08-22 stsp goto done;
9399 01073a5d 2019-08-22 stsp }
9400 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
9401 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9402 01073a5d 2019-08-22 stsp goto done;
9403 01073a5d 2019-08-22 stsp if (worktree) {
9404 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9405 01073a5d 2019-08-22 stsp repo_path = strdup(
9406 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
9407 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9408 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
9409 01073a5d 2019-08-22 stsp goto done;
9410 01073a5d 2019-08-22 stsp }
9411 01073a5d 2019-08-22 stsp }
9412 01073a5d 2019-08-22 stsp }
9413 01073a5d 2019-08-22 stsp
9414 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9415 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
9416 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9417 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
9418 01073a5d 2019-08-22 stsp }
9419 01073a5d 2019-08-22 stsp
9420 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
9421 01073a5d 2019-08-22 stsp free(repo_path);
9422 01073a5d 2019-08-22 stsp if (error != NULL)
9423 01073a5d 2019-08-22 stsp goto done;
9424 01073a5d 2019-08-22 stsp
9425 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9426 896e9b6f 2019-08-26 stsp if (error)
9427 896e9b6f 2019-08-26 stsp goto done;
9428 896e9b6f 2019-08-26 stsp
9429 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
9430 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
9431 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
9432 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9433 01073a5d 2019-08-22 stsp if (error)
9434 01073a5d 2019-08-22 stsp goto done;
9435 01073a5d 2019-08-22 stsp
9436 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
9437 896e9b6f 2019-08-26 stsp if (force_path) {
9438 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
9439 896e9b6f 2019-08-26 stsp argv[i]);
9440 896e9b6f 2019-08-26 stsp if (error)
9441 896e9b6f 2019-08-26 stsp break;
9442 896e9b6f 2019-08-26 stsp } else {
9443 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
9444 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
9445 896e9b6f 2019-08-26 stsp if (error) {
9446 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9447 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
9448 896e9b6f 2019-08-26 stsp break;
9449 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
9450 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
9451 896e9b6f 2019-08-26 stsp if (error)
9452 896e9b6f 2019-08-26 stsp break;
9453 896e9b6f 2019-08-26 stsp }
9454 896e9b6f 2019-08-26 stsp }
9455 01073a5d 2019-08-22 stsp
9456 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
9457 01073a5d 2019-08-22 stsp if (error)
9458 01073a5d 2019-08-22 stsp break;
9459 01073a5d 2019-08-22 stsp
9460 01073a5d 2019-08-22 stsp switch (obj_type) {
9461 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9462 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
9463 01073a5d 2019-08-22 stsp break;
9464 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9465 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
9466 01073a5d 2019-08-22 stsp break;
9467 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9468 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
9469 01073a5d 2019-08-22 stsp break;
9470 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9471 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
9472 01073a5d 2019-08-22 stsp break;
9473 01073a5d 2019-08-22 stsp default:
9474 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
9475 01073a5d 2019-08-22 stsp break;
9476 01073a5d 2019-08-22 stsp }
9477 01073a5d 2019-08-22 stsp if (error)
9478 01073a5d 2019-08-22 stsp break;
9479 01073a5d 2019-08-22 stsp free(label);
9480 01073a5d 2019-08-22 stsp label = NULL;
9481 01073a5d 2019-08-22 stsp free(id);
9482 01073a5d 2019-08-22 stsp id = NULL;
9483 01073a5d 2019-08-22 stsp }
9484 01073a5d 2019-08-22 stsp done:
9485 01073a5d 2019-08-22 stsp free(label);
9486 01073a5d 2019-08-22 stsp free(id);
9487 896e9b6f 2019-08-26 stsp free(commit_id);
9488 01073a5d 2019-08-22 stsp if (worktree)
9489 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
9490 01073a5d 2019-08-22 stsp if (repo) {
9491 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
9492 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
9493 01073a5d 2019-08-22 stsp if (error == NULL)
9494 01073a5d 2019-08-22 stsp error = repo_error;
9495 b2118c49 2020-07-28 stsp }
9496 b2118c49 2020-07-28 stsp return error;
9497 b2118c49 2020-07-28 stsp }
9498 b2118c49 2020-07-28 stsp
9499 b2118c49 2020-07-28 stsp __dead static void
9500 b2118c49 2020-07-28 stsp usage_info(void)
9501 b2118c49 2020-07-28 stsp {
9502 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
9503 b2118c49 2020-07-28 stsp getprogname());
9504 b2118c49 2020-07-28 stsp exit(1);
9505 b2118c49 2020-07-28 stsp }
9506 b2118c49 2020-07-28 stsp
9507 b2118c49 2020-07-28 stsp static const struct got_error *
9508 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9509 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9510 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
9511 b2118c49 2020-07-28 stsp {
9512 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
9513 b2118c49 2020-07-28 stsp char *id_str = NULL;
9514 b2118c49 2020-07-28 stsp char datebuf[128];
9515 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
9516 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
9517 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9518 b2118c49 2020-07-28 stsp
9519 b2118c49 2020-07-28 stsp /*
9520 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
9521 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
9522 b2118c49 2020-07-28 stsp */
9523 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
9524 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
9525 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
9526 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
9527 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
9528 b2118c49 2020-07-28 stsp }
9529 b2118c49 2020-07-28 stsp
9530 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
9531 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
9532 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
9533 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
9534 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
9535 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9536 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
9537 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
9538 b2118c49 2020-07-28 stsp else
9539 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
9540 b2118c49 2020-07-28 stsp
9541 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
9542 b2118c49 2020-07-28 stsp if (tm == NULL)
9543 b2118c49 2020-07-28 stsp return NULL;
9544 b2118c49 2020-07-28 stsp if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9545 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
9546 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
9547 b2118c49 2020-07-28 stsp
9548 b2118c49 2020-07-28 stsp if (blob_id) {
9549 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
9550 b2118c49 2020-07-28 stsp if (err)
9551 b2118c49 2020-07-28 stsp return err;
9552 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
9553 b2118c49 2020-07-28 stsp free(id_str);
9554 b2118c49 2020-07-28 stsp }
9555 b2118c49 2020-07-28 stsp
9556 b2118c49 2020-07-28 stsp if (staged_blob_id) {
9557 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
9558 b2118c49 2020-07-28 stsp if (err)
9559 b2118c49 2020-07-28 stsp return err;
9560 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
9561 b2118c49 2020-07-28 stsp free(id_str);
9562 b2118c49 2020-07-28 stsp }
9563 b2118c49 2020-07-28 stsp
9564 b2118c49 2020-07-28 stsp if (commit_id) {
9565 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
9566 b2118c49 2020-07-28 stsp if (err)
9567 b2118c49 2020-07-28 stsp return err;
9568 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
9569 b2118c49 2020-07-28 stsp free(id_str);
9570 b2118c49 2020-07-28 stsp }
9571 b2118c49 2020-07-28 stsp
9572 b2118c49 2020-07-28 stsp return NULL;
9573 b2118c49 2020-07-28 stsp }
9574 b2118c49 2020-07-28 stsp
9575 b2118c49 2020-07-28 stsp static const struct got_error *
9576 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
9577 b2118c49 2020-07-28 stsp {
9578 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
9579 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
9580 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
9581 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
9582 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9583 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
9584 b2118c49 2020-07-28 stsp int ch, show_files = 0;
9585 b2118c49 2020-07-28 stsp
9586 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
9587 b2118c49 2020-07-28 stsp
9588 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9589 b2118c49 2020-07-28 stsp switch (ch) {
9590 b2118c49 2020-07-28 stsp default:
9591 b2118c49 2020-07-28 stsp usage_info();
9592 b2118c49 2020-07-28 stsp /* NOTREACHED */
9593 b2118c49 2020-07-28 stsp }
9594 01073a5d 2019-08-22 stsp }
9595 b2118c49 2020-07-28 stsp
9596 b2118c49 2020-07-28 stsp argc -= optind;
9597 b2118c49 2020-07-28 stsp argv += optind;
9598 b2118c49 2020-07-28 stsp
9599 b2118c49 2020-07-28 stsp #ifndef PROFILE
9600 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9601 b2118c49 2020-07-28 stsp NULL) == -1)
9602 b2118c49 2020-07-28 stsp err(1, "pledge");
9603 b2118c49 2020-07-28 stsp #endif
9604 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
9605 b2118c49 2020-07-28 stsp if (cwd == NULL) {
9606 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
9607 b2118c49 2020-07-28 stsp goto done;
9608 b2118c49 2020-07-28 stsp }
9609 b2118c49 2020-07-28 stsp
9610 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
9611 b2118c49 2020-07-28 stsp if (error) {
9612 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9613 b2118c49 2020-07-28 stsp error = wrap_not_worktree_error(error, "status", cwd);
9614 b2118c49 2020-07-28 stsp goto done;
9615 b2118c49 2020-07-28 stsp }
9616 b2118c49 2020-07-28 stsp
9617 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9618 b2118c49 2020-07-28 stsp if (error)
9619 b2118c49 2020-07-28 stsp goto done;
9620 b2118c49 2020-07-28 stsp
9621 b2118c49 2020-07-28 stsp if (argc >= 1) {
9622 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
9623 b2118c49 2020-07-28 stsp worktree);
9624 b2118c49 2020-07-28 stsp if (error)
9625 b2118c49 2020-07-28 stsp goto done;
9626 b2118c49 2020-07-28 stsp show_files = 1;
9627 b2118c49 2020-07-28 stsp }
9628 b2118c49 2020-07-28 stsp
9629 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
9630 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
9631 b2118c49 2020-07-28 stsp if (error)
9632 b2118c49 2020-07-28 stsp goto done;
9633 b2118c49 2020-07-28 stsp
9634 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
9635 b2118c49 2020-07-28 stsp if (error)
9636 b2118c49 2020-07-28 stsp goto done;
9637 b2118c49 2020-07-28 stsp
9638 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9639 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
9640 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
9641 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
9642 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
9643 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
9644 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
9645 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9646 b2118c49 2020-07-28 stsp
9647 b2118c49 2020-07-28 stsp if (show_files) {
9648 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9649 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9650 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
9651 b2118c49 2020-07-28 stsp continue;
9652 b2118c49 2020-07-28 stsp /*
9653 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
9654 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
9655 b2118c49 2020-07-28 stsp */
9656 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
9657 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
9658 b2118c49 2020-07-28 stsp }
9659 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
9660 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
9661 b2118c49 2020-07-28 stsp if (error)
9662 b2118c49 2020-07-28 stsp goto done;
9663 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9664 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
9665 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
9666 b2118c49 2020-07-28 stsp break;
9667 b2118c49 2020-07-28 stsp }
9668 b2118c49 2020-07-28 stsp }
9669 b2118c49 2020-07-28 stsp }
9670 b2118c49 2020-07-28 stsp done:
9671 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
9672 b2118c49 2020-07-28 stsp free((char *)pe->path);
9673 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
9674 b2118c49 2020-07-28 stsp free(cwd);
9675 b2118c49 2020-07-28 stsp free(id_str);
9676 b2118c49 2020-07-28 stsp free(uuidstr);
9677 0ebf8283 2019-07-24 stsp return error;
9678 0ebf8283 2019-07-24 stsp }